altos/cc1111: Hack on USB driver to make Windows happy
[fw/altos] / src / cc1111 / ao_usb.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19 #include "ao_usb.h"
20
21 struct ao_task __xdata ao_usb_task;
22
23 static __xdata uint16_t ao_usb_in_bytes;
24 static __pdata uint16_t ao_usb_in_bytes_last;
25 static __xdata uint16_t ao_usb_out_bytes;
26 static __pdata uint8_t  ao_usb_iif;
27 static __pdata uint8_t  ao_usb_running;
28
29 static void
30 ao_usb_set_interrupts(void)
31 {
32         /* IN interrupts on the control an IN endpoints */
33         USBIIE = (1 << AO_USB_CONTROL_EP) | (1 << AO_USB_IN_EP);
34
35         /* OUT interrupts on the OUT endpoint */
36         USBOIE = (1 << AO_USB_OUT_EP);
37
38         /* Only care about reset */
39         USBCIE = USBCIE_RSTIE;
40 }
41
42 /* This interrupt is shared with port 2,
43  * so when we hook that up, fix this
44  */
45 void
46 ao_usb_isr(void) __interrupt 6
47 {
48         USBIF = 0;
49         ao_usb_iif |= USBIIF;
50         if (ao_usb_iif & 1)
51                 ao_wakeup(&ao_usb_task);
52         if (ao_usb_iif & (1 << AO_USB_IN_EP))
53                 ao_wakeup(&ao_usb_in_bytes);
54
55         if (USBOIF & (1 << AO_USB_OUT_EP))
56                 ao_wakeup(&ao_stdin_ready);
57
58         if (USBCIF & USBCIF_RSTIF)
59                 ao_usb_set_interrupts();
60 #if HAS_BTM
61 #if BT_LINK_ON_P2
62         ao_btm_isr();
63 #endif
64 #endif
65 #if HAS_P2_ISR
66         ao_p2_isr();
67 #endif
68 }
69
70 struct ao_usb_setup {
71         uint8_t         dir_type_recip;
72         uint8_t         request;
73         uint16_t        value;
74         uint16_t        index;
75         uint16_t        length;
76 } __xdata ao_usb_setup;
77
78 __pdata uint8_t ao_usb_ep0_state;
79 uint8_t * __pdata ao_usb_ep0_in_data;
80 __pdata uint8_t ao_usb_ep0_in_len;
81 __pdata uint8_t ao_usb_ep0_in_buf[2];
82 __pdata uint8_t ao_usb_ep0_out_len;
83 __xdata uint8_t *__pdata ao_usb_ep0_out_data;
84 __pdata uint8_t ao_usb_configuration;
85
86 /* Send an IN data packet */
87 static void
88 ao_usb_ep0_flush(void)
89 {
90         __pdata uint8_t this_len;
91         __pdata uint8_t cs0;
92
93         /* If the IN packet hasn't been picked up, just return */
94         USBINDEX = 0;
95         cs0 = USBCS0;
96         if (cs0 & USBCS0_INPKT_RDY)
97                 return;
98
99         this_len = ao_usb_ep0_in_len;
100         if (this_len > AO_USB_CONTROL_SIZE)
101                 this_len = AO_USB_CONTROL_SIZE;
102         cs0 = USBCS0_INPKT_RDY;
103         if (this_len != AO_USB_CONTROL_SIZE) {
104                 cs0 = USBCS0_INPKT_RDY | USBCS0_DATA_END;
105                 ao_usb_ep0_state = AO_USB_EP0_IDLE;
106         }
107         ao_usb_ep0_in_len -= this_len;
108         while (this_len--)
109                 USBFIFO[0] = *ao_usb_ep0_in_data++;
110         USBINDEX = 0;
111         USBCS0 = cs0;
112 }
113
114 __xdata static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
115
116 /* Walk through the list of descriptors and find a match
117  */
118 static void
119 ao_usb_get_descriptor(uint16_t value)
120 {
121         __code uint8_t          *__pdata descriptor;
122         __pdata uint8_t         type = value >> 8;
123         __pdata uint8_t         index = value;
124
125         descriptor = ao_usb_descriptors;
126         while (descriptor[0] != 0) {
127                 if (descriptor[1] == type && index-- == 0) {
128                         if (type == AO_USB_DESC_CONFIGURATION)
129                                 ao_usb_ep0_in_len = descriptor[2];
130                         else
131                                 ao_usb_ep0_in_len = descriptor[0];
132                         ao_usb_ep0_in_data = descriptor;
133                         break;
134                 }
135                 descriptor += descriptor[0];
136         }
137 }
138
139 /* Read data from the ep0 OUT fifo
140  */
141 static void
142 ao_usb_ep0_fill(void)
143 {
144         __pdata uint8_t len;
145
146         USBINDEX = 0;
147         len = USBCNT0;
148         if (len > ao_usb_ep0_out_len)
149                 len = ao_usb_ep0_out_len;
150         ao_usb_ep0_out_len -= len;
151         while (len--)
152                 *ao_usb_ep0_out_data++ = USBFIFO[0];
153 }
154
155 static void
156 ao_usb_ep0_queue_byte(uint8_t a)
157 {
158         ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
159 }
160
161 static void
162 ao_usb_set_address(uint8_t address)
163 {
164         ao_usb_running = 1;
165         USBADDR = address;
166 }
167
168 static void
169 ao_usb_set_configuration(void)
170 {
171         /* Set the IN max packet size, double buffered */
172         USBINDEX = AO_USB_IN_EP;
173         USBMAXI = AO_USB_IN_SIZE >> 3;
174         USBCSIH |= USBCSIH_IN_DBL_BUF;
175
176         /* Set the OUT max packet size, double buffered */
177         USBINDEX = AO_USB_OUT_EP;
178         USBMAXO = AO_USB_OUT_SIZE >> 3;
179         USBCSOH = USBCSOH_OUT_DBL_BUF;
180 }
181
182 static void
183 ao_usb_ep0_setup(void)
184 {
185         /* Pull the setup packet out of the fifo */
186         ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_setup;
187         ao_usb_ep0_out_len = 8;
188         ao_usb_ep0_fill();
189         if (ao_usb_ep0_out_len != 0)
190                 return;
191
192         ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
193         ao_usb_ep0_in_len = 0;
194         switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
195         case AO_USB_TYPE_STANDARD:
196                 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
197                 case AO_USB_RECIP_DEVICE:
198                         switch(ao_usb_setup.request) {
199                         case AO_USB_REQ_GET_STATUS:
200                                 ao_usb_ep0_queue_byte(0);
201                                 ao_usb_ep0_queue_byte(0);
202                                 break;
203                         case AO_USB_REQ_SET_ADDRESS:
204                                 ao_usb_set_address(ao_usb_setup.value);
205                                 break;
206                         case AO_USB_REQ_GET_DESCRIPTOR:
207                                 ao_usb_get_descriptor(ao_usb_setup.value);
208                                 break;
209                         case AO_USB_REQ_GET_CONFIGURATION:
210                                 ao_usb_ep0_queue_byte(ao_usb_configuration);
211                                 break;
212                         case AO_USB_REQ_SET_CONFIGURATION:
213                                 ao_usb_configuration = ao_usb_setup.value;
214                                 ao_usb_set_configuration();
215                                 break;
216                         }
217                         break;
218                 case AO_USB_RECIP_INTERFACE:
219                         #pragma disable_warning 110
220                         switch(ao_usb_setup.request) {
221                         case AO_USB_REQ_GET_STATUS:
222                                 ao_usb_ep0_queue_byte(0);
223                                 ao_usb_ep0_queue_byte(0);
224                                 break;
225                         case AO_USB_REQ_GET_INTERFACE:
226                                 ao_usb_ep0_queue_byte(0);
227                                 break;
228                         case AO_USB_REQ_SET_INTERFACE:
229                                 break;
230                         }
231                         break;
232                 case AO_USB_RECIP_ENDPOINT:
233                         switch(ao_usb_setup.request) {
234                         case AO_USB_REQ_GET_STATUS:
235                                 ao_usb_ep0_queue_byte(0);
236                                 ao_usb_ep0_queue_byte(0);
237                                 break;
238                         }
239                         break;
240                 }
241                 break;
242         case AO_USB_TYPE_CLASS:
243                 switch (ao_usb_setup.request) {
244                 case AO_USB_SET_LINE_CODING:
245                         ao_usb_ep0_out_len = 7;
246                         ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_line_coding;
247                         break;
248                 case AO_USB_GET_LINE_CODING:
249                         ao_usb_ep0_in_len = 7;
250                         ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
251                         break;
252                 case AO_USB_SET_CONTROL_LINE_STATE:
253                         break;
254                 }
255                 break;
256         }
257
258         /* Figure out how to ACK the setup packet and the
259          * next state
260          */
261         USBINDEX = 0;
262         if (ao_usb_ep0_in_len) {
263
264                 /* Sending data back to the host
265                  */
266                 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
267                 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
268                 if (ao_usb_setup.length < ao_usb_ep0_in_len)
269                         ao_usb_ep0_in_len = ao_usb_setup.length;
270                 ao_usb_ep0_flush();
271         } else if (ao_usb_ep0_out_len) {
272                 
273                 /* Receiving data from the host
274                  */
275                 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
276                 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
277         } else if (ao_usb_setup.length) {
278
279                 /* Uh-oh, the host expected to send or receive data
280                  * and we don't know what to do.
281                  */
282                 ao_usb_ep0_state = AO_USB_EP0_STALL;
283                 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_SEND_STALL;
284         } else {
285
286                 /* Simple setup packet with no data
287                  */
288                 ao_usb_ep0_state = AO_USB_EP0_IDLE;
289                 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
290         }
291 }
292
293 /* End point 0 receives all of the control messages. */
294 static void
295 ao_usb_ep0(void)
296 {
297         __pdata uint8_t cs0;
298
299         ao_usb_ep0_state = AO_USB_EP0_IDLE;
300         for (;;) {
301                 __critical for (;;) {
302                         if (ao_usb_iif & 1) {
303                                 ao_usb_iif &= ~1;
304                                 break;
305                         }
306                         ao_sleep(&ao_usb_task);
307                 }
308                 USBINDEX = 0;
309                 cs0 = USBCS0;
310                 if (cs0 & USBCS0_SETUP_END) {
311                         USBCS0 = USBCS0_CLR_SETUP_END;
312                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
313                 }
314                 if (cs0 & USBCS0_SENT_STALL) {
315                         USBCS0 = 0;
316                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
317                 }
318                 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN &&
319                     (cs0 & USBCS0_INPKT_RDY) == 0)
320                 {
321                         ao_usb_ep0_flush();
322                 }
323                 if (cs0 & USBCS0_OUTPKT_RDY) {
324                         switch (ao_usb_ep0_state) {
325                         case AO_USB_EP0_IDLE:
326                                 ao_usb_ep0_setup();
327                                 break;
328                         case AO_USB_EP0_DATA_OUT:
329                                 ao_usb_ep0_fill();
330                                 USBINDEX = 0;
331                                 if (ao_usb_ep0_out_len == 0) {
332                                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
333                                         USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
334                                 } else
335                                         USBCS0 = USBCS0_CLR_OUTPKT_RDY;
336                                 break;
337                         }
338                 }
339         }
340 }
341
342 /* Wait for a free IN buffer */
343 static void
344 ao_usb_in_wait(void)
345 {
346         for (;;) {
347                 USBINDEX = AO_USB_IN_EP;
348                 if ((USBCSIL & USBCSIL_INPKT_RDY) == 0)
349                         break;
350                 ao_sleep(&ao_usb_in_bytes);
351         }
352 }
353
354 /* Send the current IN packet */
355 static void
356 ao_usb_in_send(void)
357 {
358         USBINDEX = AO_USB_IN_EP;
359         USBCSIL |= USBCSIL_INPKT_RDY;
360         ao_usb_in_bytes_last = ao_usb_in_bytes;
361         ao_usb_in_bytes = 0;
362 }
363
364 void
365 ao_usb_flush(void) __critical
366 {
367         if (!ao_usb_running)
368                 return;
369
370         /* If there are pending bytes, or if the last packet was full,
371          * send another IN packet
372          */
373         if (ao_usb_in_bytes || (ao_usb_in_bytes_last == AO_USB_IN_SIZE)) {
374                 ao_usb_in_wait();
375                 ao_usb_in_send();
376         }
377 }
378
379 void
380 ao_usb_putchar(char c) __critical __reentrant
381 {
382         if (!ao_usb_running)
383                 return;
384
385         ao_usb_in_wait();
386
387         /* Queue a byte, sending the packet when full */
388         USBFIFO[AO_USB_IN_EP << 1] = c;
389         if (++ao_usb_in_bytes == AO_USB_IN_SIZE)
390                 ao_usb_in_send();
391 }
392
393 int
394 _ao_usb_pollchar(void)
395 {
396         uint8_t c;
397         if (ao_usb_out_bytes == 0) {
398                 USBINDEX = AO_USB_OUT_EP;
399                 if ((USBCSOL & USBCSOL_OUTPKT_RDY) == 0)
400                         return AO_READ_AGAIN;
401                 ao_usb_out_bytes = (USBCNTH << 8) | USBCNTL;
402                 if (ao_usb_out_bytes == 0) {
403                         USBINDEX = AO_USB_OUT_EP;
404                         USBCSOL &= ~USBCSOL_OUTPKT_RDY;
405                         return AO_READ_AGAIN;
406                 }
407         }
408         --ao_usb_out_bytes;
409         c = USBFIFO[AO_USB_OUT_EP << 1];
410         if (ao_usb_out_bytes == 0) {
411                 USBINDEX = AO_USB_OUT_EP;
412                 USBCSOL &= ~USBCSOL_OUTPKT_RDY;
413         }
414         return c;
415 }
416
417 char
418 ao_usb_getchar(void)
419 {
420         int     c;
421
422         ao_arch_block_interrupts();
423         while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
424                 ao_sleep(&ao_stdin_ready);
425         ao_arch_release_interrupts();
426         return c;
427 }
428
429 void
430 ao_usb_enable(void)
431 {
432         /* Turn on the USB controller */
433         SLEEP |= SLEEP_USB_EN;
434
435         ao_usb_set_configuration();
436
437         ao_usb_set_interrupts();
438
439         /* enable USB interrupts */
440         IEN2 |= IEN2_USBIE;
441
442         /* Clear any pending interrupts */
443         USBCIF = 0;
444         USBOIF = 0;
445         USBIIF = 0;
446 }
447
448 void
449 ao_usb_disable(void)
450 {
451         /* Disable USB interrupts */
452         USBIIE = 0;
453         USBOIE = 0;
454         USBCIE = 0;
455         IEN2 &= ~IEN2_USBIE;
456
457         /* Clear any pending interrupts */
458         USBCIF = 0;
459         USBOIF = 0;
460         USBIIF = 0;
461
462         /* Turn off the USB controller */
463         SLEEP &= ~SLEEP_USB_EN;
464 }
465
466 void
467 ao_usb_init(void)
468 {
469         ao_usb_enable();
470
471         ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
472         ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
473 }