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