dd7521525f4a08da5e833296e5afb917297bdc6a
[fw/altos] / src / 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 __xdata uint16_t ao_usb_in_bytes_last;
25 static __xdata uint16_t ao_usb_out_bytes;
26 static __xdata uint8_t  ao_usb_iif;
27 static __xdata 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         IRCON2 &= ~IRCON2_USBIF;
50         ao_usb_iif |= USBIIF;
51         if (ao_usb_iif & 1)
52                 ao_wakeup(&ao_usb_task);
53         if (ao_usb_iif & (1 << AO_USB_IN_EP))
54                 ao_wakeup(&ao_usb_in_bytes);
55
56         if (USBOIF & (1 << AO_USB_OUT_EP))
57                 ao_wakeup(&ao_stdin_ready);
58
59         if (USBCIF & USBCIF_RSTIF)
60                 ao_usb_set_interrupts();
61 #if HAS_BTM
62 #if BT_LINK_ON_P2
63         ao_btm_isr();
64 #endif
65 #endif
66 }
67
68 struct ao_usb_setup {
69         uint8_t         dir_type_recip;
70         uint8_t         request;
71         uint16_t        value;
72         uint16_t        index;
73         uint16_t        length;
74 } __xdata ao_usb_setup;
75
76 __xdata uint8_t ao_usb_ep0_state;
77 uint8_t * __xdata ao_usb_ep0_in_data;
78 __xdata uint8_t ao_usb_ep0_in_len;
79 __xdata uint8_t ao_usb_ep0_in_buf[2];
80 __xdata uint8_t ao_usb_ep0_out_len;
81 __xdata uint8_t *__xdata ao_usb_ep0_out_data;
82 __xdata uint8_t ao_usb_configuration;
83
84 /* Send an IN data packet */
85 static void
86 ao_usb_ep0_flush(void)
87 {
88         __xdata uint8_t this_len;
89         __xdata uint8_t cs0;
90
91         /* If the IN packet hasn't been picked up, just return */
92         USBINDEX = 0;
93         cs0 = USBCS0;
94         if (cs0 & USBCS0_INPKT_RDY)
95                 return;
96
97         this_len = ao_usb_ep0_in_len;
98         if (this_len > AO_USB_CONTROL_SIZE)
99                 this_len = AO_USB_CONTROL_SIZE;
100         cs0 = USBCS0_INPKT_RDY;
101         if (this_len != AO_USB_CONTROL_SIZE) {
102                 cs0 = USBCS0_INPKT_RDY | USBCS0_DATA_END;
103                 ao_usb_ep0_state = AO_USB_EP0_IDLE;
104         }
105         ao_usb_ep0_in_len -= this_len;
106         while (this_len--)
107                 USBFIFO[0] = *ao_usb_ep0_in_data++;
108         USBINDEX = 0;
109         USBCS0 = cs0;
110 }
111
112 __xdata static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
113
114 /* Walk through the list of descriptors and find a match
115  */
116 static void
117 ao_usb_get_descriptor(uint16_t value)
118 {
119         const uint8_t           *__xdata descriptor;
120         __xdata uint8_t         type = value >> 8;
121         __xdata uint8_t         index = value;
122
123         descriptor = ao_usb_descriptors;
124         while (descriptor[0] != 0) {
125                 if (descriptor[1] == type && index-- == 0) {
126                         if (type == AO_USB_DESC_CONFIGURATION)
127                                 ao_usb_ep0_in_len = descriptor[2];
128                         else
129                                 ao_usb_ep0_in_len = descriptor[0];
130                         ao_usb_ep0_in_data = descriptor;
131                         break;
132                 }
133                 descriptor += descriptor[0];
134         }
135 }
136
137 /* Read data from the ep0 OUT fifo
138  */
139 static void
140 ao_usb_ep0_fill(void)
141 {
142         __xdata uint8_t len;
143
144         USBINDEX = 0;
145         len = USBCNT0;
146         if (len > ao_usb_ep0_out_len)
147                 len = ao_usb_ep0_out_len;
148         ao_usb_ep0_out_len -= len;
149         while (len--)
150                 *ao_usb_ep0_out_data++ = USBFIFO[0];
151 }
152
153 void
154 ao_usb_ep0_queue_byte(uint8_t a)
155 {
156         ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
157 }
158
159 void
160 ao_usb_set_address(uint8_t address)
161 {
162         ao_usb_running = 1;
163         USBADDR = address | 0x80;
164         while (USBADDR & 0x80)
165                 ;
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         /* Figure out how to ACK the setup packet */
193         if (ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) {
194                 if (ao_usb_setup.length)
195                         ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
196                 else
197                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
198         } else {
199                 if (ao_usb_setup.length)
200                         ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
201                 else
202                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
203         }
204         USBINDEX = 0;
205         if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
206                 USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
207         else
208                 USBCS0 = USBCS0_CLR_OUTPKT_RDY;
209
210         ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
211         ao_usb_ep0_in_len = 0;
212         switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
213         case AO_USB_TYPE_STANDARD:
214                 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
215                 case AO_USB_RECIP_DEVICE:
216                         switch(ao_usb_setup.request) {
217                         case AO_USB_REQ_GET_STATUS:
218                                 ao_usb_ep0_queue_byte(0);
219                                 ao_usb_ep0_queue_byte(0);
220                                 break;
221                         case AO_USB_REQ_SET_ADDRESS:
222                                 ao_usb_set_address(ao_usb_setup.value);
223                                 break;
224                         case AO_USB_REQ_GET_DESCRIPTOR:
225                                 ao_usb_get_descriptor(ao_usb_setup.value);
226                                 break;
227                         case AO_USB_REQ_GET_CONFIGURATION:
228                                 ao_usb_ep0_queue_byte(ao_usb_configuration);
229                                 break;
230                         case AO_USB_REQ_SET_CONFIGURATION:
231                                 ao_usb_configuration = ao_usb_setup.value;
232                                 ao_usb_set_configuration();
233                                 break;
234                         }
235                         break;
236                 case AO_USB_RECIP_INTERFACE:
237                         #pragma disable_warning 110
238                         switch(ao_usb_setup.request) {
239                         case AO_USB_REQ_GET_STATUS:
240                                 ao_usb_ep0_queue_byte(0);
241                                 ao_usb_ep0_queue_byte(0);
242                                 break;
243                         case AO_USB_REQ_GET_INTERFACE:
244                                 ao_usb_ep0_queue_byte(0);
245                                 break;
246                         case AO_USB_REQ_SET_INTERFACE:
247                                 break;
248                         }
249                         break;
250                 case AO_USB_RECIP_ENDPOINT:
251                         switch(ao_usb_setup.request) {
252                         case AO_USB_REQ_GET_STATUS:
253                                 ao_usb_ep0_queue_byte(0);
254                                 ao_usb_ep0_queue_byte(0);
255                                 break;
256                         }
257                         break;
258                 }
259                 break;
260         case AO_USB_TYPE_CLASS:
261                 switch (ao_usb_setup.request) {
262                 case SET_LINE_CODING:
263                         ao_usb_ep0_out_len = 7;
264                         ao_usb_ep0_out_data = (__xdata uint8_t *) &ao_usb_line_coding;
265                         break;
266                 case GET_LINE_CODING:
267                         ao_usb_ep0_in_len = 7;
268                         ao_usb_ep0_in_data = (uint8_t *) &ao_usb_line_coding;
269                         break;
270                 case SET_CONTROL_LINE_STATE:
271                         break;
272                 }
273                 break;
274         }
275         if (ao_usb_ep0_state != AO_USB_EP0_DATA_OUT) {
276                 if (ao_usb_setup.length < ao_usb_ep0_in_len)
277                         ao_usb_ep0_in_len = ao_usb_setup.length;
278                 ao_usb_ep0_flush();
279         }
280 }
281
282 /* End point 0 receives all of the control messages. */
283 static void
284 ao_usb_ep0(void)
285 {
286         __xdata uint8_t cs0;
287
288         ao_usb_ep0_state = AO_USB_EP0_IDLE;
289         for (;;) {
290                 __critical for (;;) {
291                         if (ao_usb_iif & 1) {
292                                 ao_usb_iif &= ~1;
293                                 break;
294                         }
295                         ao_sleep(&ao_usb_task);
296                 }
297                 USBINDEX = 0;
298                 cs0 = USBCS0;
299                 if (cs0 & USBCS0_SETUP_END) {
300                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
301                         USBCS0 = USBCS0_CLR_SETUP_END;
302                 }
303                 if (cs0 & USBCS0_SENT_STALL) {
304                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
305                         USBCS0 &= ~USBCS0_SENT_STALL;
306                 }
307                 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN &&
308                     (cs0 & USBCS0_INPKT_RDY) == 0)
309                 {
310                         ao_usb_ep0_flush();
311                 }
312                 if (cs0 & USBCS0_OUTPKT_RDY) {
313                         switch (ao_usb_ep0_state) {
314                         case AO_USB_EP0_IDLE:
315                                 ao_usb_ep0_setup();
316                                 break;
317                         case AO_USB_EP0_DATA_OUT:
318                                 ao_usb_ep0_fill();
319                                 if (ao_usb_ep0_out_len == 0)
320                                         ao_usb_ep0_state = AO_USB_EP0_IDLE;
321                                 USBINDEX = 0;
322                                 if (ao_usb_ep0_state == AO_USB_EP0_IDLE)
323                                         USBCS0 = USBCS0_CLR_OUTPKT_RDY | USBCS0_DATA_END;
324                                 else
325                                         USBCS0 = USBCS0_CLR_OUTPKT_RDY;
326                                 break;
327                         }
328                 }
329         }
330 }
331
332 /* Wait for a free IN buffer */
333 static void
334 ao_usb_in_wait(void)
335 {
336         for (;;) {
337                 USBINDEX = AO_USB_IN_EP;
338                 if ((USBCSIL & USBCSIL_INPKT_RDY) == 0)
339                         break;
340                 ao_sleep(&ao_usb_in_bytes);
341         }
342 }
343
344 /* Send the current IN packet */
345 static void
346 ao_usb_in_send(void)
347 {
348         USBINDEX = AO_USB_IN_EP;
349         USBCSIL |= USBCSIL_INPKT_RDY;
350         ao_usb_in_bytes_last = ao_usb_in_bytes;
351         ao_usb_in_bytes = 0;
352 }
353
354 void
355 ao_usb_flush(void) __critical
356 {
357         if (!ao_usb_running)
358                 return;
359
360         /* If there are pending bytes, or if the last packet was full,
361          * send another IN packet
362          */
363         if (ao_usb_in_bytes || (ao_usb_in_bytes_last == AO_USB_IN_SIZE)) {
364                 ao_usb_in_wait();
365                 ao_usb_in_send();
366         }
367 }
368
369 void
370 ao_usb_putchar(char c) __critical __reentrant
371 {
372         if (!ao_usb_running)
373                 return;
374
375         ao_usb_in_wait();
376
377         /* Queue a byte, sending the packet when full */
378         USBFIFO[AO_USB_IN_EP << 1] = c;
379         if (++ao_usb_in_bytes == AO_USB_IN_SIZE)
380                 ao_usb_in_send();
381 }
382
383 char
384 ao_usb_pollchar(void) __critical
385 {
386         char c;
387         if (ao_usb_out_bytes == 0) {
388                 USBINDEX = AO_USB_OUT_EP;
389                 if ((USBCSOL & USBCSOL_OUTPKT_RDY) == 0)
390                         return AO_READ_AGAIN;
391                 ao_usb_out_bytes = (USBCNTH << 8) | USBCNTL;
392                 if (ao_usb_out_bytes == 0) {
393                         USBINDEX = AO_USB_OUT_EP;
394                         USBCSOL &= ~USBCSOL_OUTPKT_RDY;
395                         return AO_READ_AGAIN;
396                 }
397         }
398         --ao_usb_out_bytes;
399         c = USBFIFO[AO_USB_OUT_EP << 1];
400         if (ao_usb_out_bytes == 0) {
401                 USBINDEX = AO_USB_OUT_EP;
402                 USBCSOL &= ~USBCSOL_OUTPKT_RDY;
403         }
404         return c;
405 }
406
407 char
408 ao_usb_getchar(void) __critical
409 {
410         char    c;
411
412         while ((c = ao_usb_pollchar()) == AO_READ_AGAIN)
413                 ao_sleep(&ao_stdin_ready);
414         return c;
415 }
416
417 void
418 ao_usb_enable(void)
419 {
420         /* Turn on the USB controller */
421         SLEEP |= SLEEP_USB_EN;
422
423         ao_usb_set_configuration();
424
425         ao_usb_set_interrupts();
426
427         /* enable USB interrupts */
428         IEN2 |= IEN2_USBIE;
429
430         /* Clear any pending interrupts */
431         USBCIF = 0;
432         USBOIF = 0;
433         USBIIF = 0;
434 }
435
436 void
437 ao_usb_disable(void)
438 {
439         /* Disable USB interrupts */
440         USBIIE = 0;
441         USBOIE = 0;
442         USBCIE = 0;
443         IEN2 &= ~IEN2_USBIE;
444
445         /* Clear any pending interrupts */
446         USBCIF = 0;
447         USBOIF = 0;
448         USBIIF = 0;
449
450         /* Turn off the USB controller */
451         SLEEP &= ~SLEEP_USB_EN;
452 }
453
454 void
455 ao_usb_init(void)
456 {
457         ao_usb_enable();
458
459         ao_add_task(&ao_usb_task, ao_usb_ep0, "usb");
460         ao_add_stdio(ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
461 }