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