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