altos: Massive product config cleanup
[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 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 void
162 ao_usb_set_address(uint8_t address)
163 {
164         ao_usb_running = 1;
165         USBADDR = address | 0x80;
166         while (USBADDR & 0x80)
167                 ;
168 }
169
170 static void
171 ao_usb_set_configuration(void)
172 {
173         /* Set the IN max packet size, double buffered */
174         USBINDEX = AO_USB_IN_EP;
175         USBMAXI = AO_USB_IN_SIZE >> 3;
176         USBCSIH |= USBCSIH_IN_DBL_BUF;
177
178         /* Set the OUT max packet size, double buffered */
179         USBINDEX = AO_USB_OUT_EP;
180         USBMAXO = AO_USB_OUT_SIZE >> 3;
181         USBCSOH = USBCSOH_OUT_DBL_BUF;
182 }
183
184 static void
185 ao_usb_ep0_setup(void)
186 {
187         /* Pull the setup packet out of the fifo */
188         ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_setup;
189         ao_usb_ep0_out_len = 8;
190         ao_usb_ep0_fill();
191         if (ao_usb_ep0_out_len != 0)
192                 return;
193
194         /* Figure out how to ACK the setup packet */
195         if (ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) {
196                 if (ao_usb_setup.length)
197                         ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
198                 else
199                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
200         } else {
201                 if (ao_usb_setup.length)
202                         ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
203                 else
204                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
205         }
206         USBINDEX = 0;
207         if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
208                 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
209         else
210                 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
211
212         ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
213         ao_usb_ep0_in_len = 0;
214         switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
215         case AO_USB_TYPE_STANDARD:
216                 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
217                 case AO_USB_RECIP_DEVICE:
218                         switch(ao_usb_setup.request) {
219                         case AO_USB_REQ_GET_STATUS:
220                                 ao_usb_ep0_queue_byte(0);
221                                 ao_usb_ep0_queue_byte(0);
222                                 break;
223                         case AO_USB_REQ_SET_ADDRESS:
224                                 ao_usb_set_address(ao_usb_setup.value);
225                                 break;
226                         case AO_USB_REQ_GET_DESCRIPTOR:
227                                 ao_usb_get_descriptor(ao_usb_setup.value);
228                                 break;
229                         case AO_USB_REQ_GET_CONFIGURATION:
230                                 ao_usb_ep0_queue_byte(ao_usb_configuration);
231                                 break;
232                         case AO_USB_REQ_SET_CONFIGURATION:
233                                 ao_usb_configuration = ao_usb_setup.value;
234                                 ao_usb_set_configuration();
235                                 break;
236                         }
237                         break;
238                 case AO_USB_RECIP_INTERFACE:
239                         #pragma disable_warning 110
240                         switch(ao_usb_setup.request) {
241                         case AO_USB_REQ_GET_STATUS:
242                                 ao_usb_ep0_queue_byte(0);
243                                 ao_usb_ep0_queue_byte(0);
244                                 break;
245                         case AO_USB_REQ_GET_INTERFACE:
246                                 ao_usb_ep0_queue_byte(0);
247                                 break;
248                         case AO_USB_REQ_SET_INTERFACE:
249                                 break;
250                         }
251                         break;
252                 case AO_USB_RECIP_ENDPOINT:
253                         switch(ao_usb_setup.request) {
254                         case AO_USB_REQ_GET_STATUS:
255                                 ao_usb_ep0_queue_byte(0);
256                                 ao_usb_ep0_queue_byte(0);
257                                 break;
258                         }
259                         break;
260                 }
261                 break;
262         case AO_USB_TYPE_CLASS:
263                 switch (ao_usb_setup.request) {
264                 case AO_USB_SET_LINE_CODING:
265                         ao_usb_ep0_out_len = 7;
266                         ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_line_coding;
267                         break;
268                 case AO_USB_GET_LINE_CODING:
269                         ao_usb_ep0_in_len = 7;
270                         ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
271                         break;
272                 case AO_USB_SET_CONTROL_LINE_STATE:
273                         break;
274                 }
275                 break;
276         }
277         if (ao_usb_ep0_state != AO_USB_EP0_DATA_OUT) {
278                 if (ao_usb_setup.length < ao_usb_ep0_in_len)
279                         ao_usb_ep0_in_len = ao_usb_setup.length;
280                 ao_usb_ep0_flush();
281         }
282 }
283
284 /* End point 0 receives all of the control messages. */
285 static void
286 ao_usb_ep0(void)
287 {
288         __pdata uint8_t cs0;
289
290         ao_usb_ep0_state = AO_USB_EP0_IDLE;
291         for (;;) {
292                 __critical for (;;) {
293                         if (ao_usb_iif & 1) {
294                                 ao_usb_iif &= ~1;
295                                 break;
296                         }
297                         ao_sleep(&ao_usb_task);
298                 }
299                 USBINDEX = 0;
300                 cs0 = USBCS0;
301                 if (cs0 & USBCS0_SETUP_END) {
302                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
303                         USBCS0 = USBCS0_CLR_SETUP_END;
304                 }
305                 if (cs0 & USBCS0_SENT_STALL) {
306                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
307                         USBCS0 &= ~USBCS0_SENT_STALL;
308                 }
309                 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN &&
310                     (cs0 & USBCS0_INPKT_RDY) == 0)
311                 {
312                         ao_usb_ep0_flush();
313                 }
314                 if (cs0 & USBCS0_OUTPKT_RDY) {
315                         switch (ao_usb_ep0_state) {
316                         case AO_USB_EP0_IDLE:
317                                 ao_usb_ep0_setup();
318                                 break;
319                         case AO_USB_EP0_DATA_OUT:
320                                 ao_usb_ep0_fill();
321                                 if (ao_usb_ep0_out_len == 0)
322                                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
323                                 USBINDEX = 0;
324                                 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
325                                         USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
326                                 else
327                                         USBCS0 = USBCS0_CLR_OUTPKT_RDY;
328                                 break;
329                         }
330                 }
331         }
332 }
333
334 /* Wait for a free IN buffer */
335 static void
336 ao_usb_in_wait(void)
337 {
338         for (;;) {
339                 USBINDEX = AO_USB_IN_EP;
340                 if ((USBCSIL & USBCSIL_INPKT_RDY) == 0)
341                         break;
342                 ao_sleep(&ao_usb_in_bytes);
343         }
344 }
345
346 /* Send the current IN packet */
347 static void
348 ao_usb_in_send(void)
349 {
350         USBINDEX = AO_USB_IN_EP;
351         USBCSIL |= USBCSIL_INPKT_RDY;
352         ao_usb_in_bytes_last = ao_usb_in_bytes;
353         ao_usb_in_bytes = 0;
354 }
355
356 void
357 ao_usb_flush(void) __critical
358 {
359         if (!ao_usb_running)
360                 return;
361
362         /* If there are pending bytes, or if the last packet was full,
363          * send another IN packet
364          */
365         if (ao_usb_in_bytes || (ao_usb_in_bytes_last == AO_USB_IN_SIZE)) {
366                 ao_usb_in_wait();
367                 ao_usb_in_send();
368         }
369 }
370
371 void
372 ao_usb_putchar(char c) __critical __reentrant
373 {
374         if (!ao_usb_running)
375                 return;
376
377         ao_usb_in_wait();
378
379         /* Queue a byte, sending the packet when full */
380         USBFIFO[AO_USB_IN_EP << 1] = c;
381         if (++ao_usb_in_bytes == AO_USB_IN_SIZE)
382                 ao_usb_in_send();
383 }
384
385 char
386 ao_usb_pollchar(void) __critical
387 {
388         char c;
389         if (ao_usb_out_bytes == 0) {
390                 USBINDEX = AO_USB_OUT_EP;
391                 if ((USBCSOL & USBCSOL_OUTPKT_RDY) == 0)
392                         return AO_READ_AGAIN;
393                 ao_usb_out_bytes = (USBCNTH << 8) | USBCNTL;
394                 if (ao_usb_out_bytes == 0) {
395                         USBINDEX = AO_USB_OUT_EP;
396                         USBCSOL &= ~USBCSOL_OUTPKT_RDY;
397                         return AO_READ_AGAIN;
398                 }
399         }
400         --ao_usb_out_bytes;
401         c = USBFIFO[AO_USB_OUT_EP << 1];
402         if (ao_usb_out_bytes == 0) {
403                 USBINDEX = AO_USB_OUT_EP;
404                 USBCSOL &= ~USBCSOL_OUTPKT_RDY;
405         }
406         return c;
407 }
408
409 char
410 ao_usb_getchar(void) __critical
411 {
412         char    c;
413
414         while ((c = ao_usb_pollchar()) == AO_READ_AGAIN)
415                 ao_sleep(&ao_stdin_ready);
416         return c;
417 }
418
419 void
420 ao_usb_enable(void)
421 {
422         /* Turn on the USB controller */
423         SLEEP |= SLEEP_USB_EN;
424
425         ao_usb_set_configuration();
426
427         ao_usb_set_interrupts();
428
429         /* enable USB interrupts */
430         IEN2 |= IEN2_USBIE;
431
432         /* Clear any pending interrupts */
433         USBCIF = 0;
434         USBOIF = 0;
435         USBIIF = 0;
436 }
437
438 void
439 ao_usb_disable(void)
440 {
441         /* Disable USB interrupts */
442         USBIIE = 0;
443         USBOIE = 0;
444         USBCIE = 0;
445         IEN2 &= ~IEN2_USBIE;
446
447         /* Clear any pending interrupts */
448         USBCIF = 0;
449         USBOIF = 0;
450         USBIIF = 0;
451
452         /* Turn off the USB controller */
453         SLEEP &= ~SLEEP_USB_EN;
454 }
455
456 void
457 ao_usb_init(void)
458 {
459         ao_usb_enable();
460
461         ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
462         ao_add_stdio(ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
463 }