clean up some debugging output.
[Rockbox.git] / firmware / ifp_usb_serial.c
blobe6c8dadfc52fd6bbf51f0a1f085128baa64b87d0
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Tomasz Malesinski
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 #define LCD_DEBUG
24 #define BUTTONS
27 /* #include "config.h" */
28 #include <stdlib.h>
29 #include "pnx0101.h"
30 #include "ifp_usb_serial.h"
32 #ifdef BUTTONS
33 #include "kernel.h"
34 #include "button.h"
35 #include "system.h"
36 #endif
38 #ifdef LCD_DEBUG
39 #include "lcd.h"
40 #include "sprintf.h"
41 #endif
44 #define ISP1582_BASE (0x24100000)
45 #define ISP1582_ADDRESS (*(volatile unsigned char *)ISP1582_BASE)
46 #define ISP1582_MODE (*(volatile unsigned short *)(ISP1582_BASE + 0xc))
47 #define ISP1582_INTCONF (*(volatile unsigned char *)(ISP1582_BASE + 0x10))
48 #define ISP1582_OTG (*(volatile unsigned char *)(ISP1582_BASE + 0x12))
49 #define ISP1582_INTEN (*(volatile unsigned long *)(ISP1582_BASE + 0x14))
51 #define ISP1582_EPINDEX (*(volatile unsigned char *)(ISP1582_BASE + 0x2c))
52 #define ISP1582_CTRLFUN (*(volatile unsigned char *)(ISP1582_BASE + 0x28))
53 #define ISP1582_DATA (*(volatile unsigned short *)(ISP1582_BASE + 0x20))
54 #define ISP1582_BUFLEN (*(volatile unsigned short *)(ISP1582_BASE + 0x1c))
55 #define ISP1582_BUFSTAT (*(volatile unsigned char *)(ISP1582_BASE + 0x1e))
56 #define ISP1582_MAXPKSZ (*(volatile unsigned short *)(ISP1582_BASE + 0x04))
57 #define ISP1582_EPTYPE (*(volatile unsigned short *)(ISP1582_BASE + 0x08))
59 #define ISP1582_INT (*(volatile unsigned long *)(ISP1582_BASE + 0x18))
60 #define ISP1582_CHIPID (*(volatile unsigned long *)(ISP1582_BASE + 0x70))
61 #define ISP1582_FRAMENO (*(volatile unsigned short *)(ISP1582_BASE + 0x74))
62 #define ISP1582_UNLOCK (*(volatile unsigned short *)(ISP1582_BASE + 0x7c))
64 #define ISP1582_UNLOCK_CODE 0xaa37
66 #define DIR_RX 0
67 #define DIR_TX 1
69 #define TYPE_BULK 2
71 #define STATE_DEFAULT 0
72 #define STATE_ADDRESS 1
73 #define STATE_CONFIGURED 2
75 #define N_ENDPOINTS 2
77 struct usb_endpoint
79 unsigned char *out_buf;
80 short out_len;
81 short out_ptr;
82 void (*out_done)(int, unsigned char *, int);
83 unsigned char out_in_progress;
85 unsigned char *in_buf;
86 short in_min_len;
87 short in_max_len;
88 short in_ptr;
89 void (*in_done)(int, unsigned char *, int);
90 unsigned char in_ack;
92 unsigned char halt[2];
93 unsigned char enabled[2];
94 short max_pkt_size[2];
97 static char usb_connect_state;
99 static struct usb_endpoint endpoints[N_ENDPOINTS];
101 static unsigned char setup_pkt_buf[8];
102 static unsigned char setup_out_buf[8];
103 static unsigned char usb_state;
104 static unsigned char usb_remote_wakeup;
106 #ifdef LCD_DEBUG
107 static unsigned char int_count[32];
109 static int log_pos_x = 0;
110 static int log_pos_y = 3;
111 #endif
113 static void nop_f(void)
117 #ifdef LCD_DEBUG
118 static void log_char(char c)
120 char s[2];
122 s[0] = c;
123 s[1] = 0;
125 lcd_puts(log_pos_x, log_pos_y, s);
126 lcd_update();
127 log_pos_x++;
128 if (log_pos_x >= 16)
130 log_pos_x = 0;
131 log_pos_y++;
132 if (log_pos_y > 5)
133 log_pos_y = 3;
136 #else
137 #define log_char(c)
138 #endif
140 #define SERIAL_BUF_SIZE 1024
142 struct serial_fifo
144 unsigned char buf[SERIAL_BUF_SIZE];
145 short head, tail;
148 static struct serial_fifo serial_in_fifo;
149 static struct serial_fifo serial_out_fifo;
150 static unsigned char serial_in_pkt[64];
152 static unsigned char device_descriptor[18] = {
153 0x12, /* length */
154 0x01, /* descriptor type */
155 0x10, 0x01, /* USB version (1.1) */
156 0xff, 0xff, /* class and subclass */
157 0xff, /* protocol */
158 0x40, /* max packet size 0 */
159 0x02, 0x41, /* vendor (iRiver) */
160 0x07, 0xee, /* product (0xee07) */
161 0x01, 0x00, /* device version */
162 0x01, /* manufacturer string */
163 0x02, /* product string */
164 0x00, /* serial number string */
165 0x01 /* number of configurations */
168 static unsigned char cfg_descriptor[32] = {
169 0x09, /* length */
170 0x02, /* descriptor type */
171 0x20, 0x00, /* total length */
172 0x01, /* number of interfaces */
173 0x01, /* configuration value */
174 0x00, /* configuration string */
175 0x80, /* attributes (none) */
176 0x32, /* max power (100 mA) */
177 /* interface descriptor */
178 0x09, /* length */
179 0x04, /* descriptor type */
180 0x00, /* interface number */
181 0x00, /* alternate setting */
182 0x02, /* number of endpoints */
183 0xff, /* interface class */
184 0xff, /* interface subclass */
185 0xff, /* interface protocol */
186 0x00, /* interface string */
187 /* endpoint IN */
188 0x07, /* length */
189 0x05, /* descriptor type */
190 0x81, /* endpoint 1 IN */
191 0x02, /* attributes (bulk) */
192 0x40, 0x00, /* max packet size */
193 0x00, /* interval */
194 /* endpoint OUT */
195 0x07, /* length */
196 0x05, /* descriptor type */
197 0x01, /* endpoint 1 IN */
198 0x02, /* attributes (bulk) */
199 0x40, 0x00, /* max packet size */
200 0x00 /* interval */
203 static unsigned char lang_descriptor[4] = {
204 0x04, /* length */
205 0x03, /* descriptor type */
206 0x09, 0x04 /* English (US) */
209 #define N_STRING_DESCRIPTORS 2
211 static unsigned char string_descriptor_vendor[] = {
212 0x2e, 0x03,
213 'i', 0, 'R', 0, 'i', 0, 'v', 0, 'e', 0, 'r', 0, ' ', 0, 'L', 0,
214 't', 0, 'd', 0, ' ', 0, 'a', 0, 'n', 0, 'd', 0, ' ', 0, 'R', 0,
215 'o', 0, 'c', 0, 'k', 0, 'b', 0, 'o', 0, 'x', 0};
217 static unsigned char string_descriptor_product[] = {
218 0x1c, 0x03,
219 'i', 0, 'R', 0, 'i', 0, 'v', 0, 'e', 0, 'r', 0, ' ', 0, 'i', 0,
220 'F', 0, 'P', 0, '7', 0, '0', 0, '0', 0};
222 static unsigned char *string_descriptor[N_STRING_DESCRIPTORS] = {
223 string_descriptor_vendor,
224 string_descriptor_product
227 static inline int ep_index(int n, int dir)
229 return (n << 1) | dir;
232 static inline int epidx_dir(int idx)
234 return idx & 1;
237 static inline int epidx_n(int idx)
239 return idx >> 1;
242 int usb_connected(void)
244 return GPIO7_READ & 1;
247 static inline void usb_select_endpoint(int idx)
249 ISP1582_EPINDEX = idx;
252 static inline void usb_select_setup_endpoint(void)
254 ISP1582_EPINDEX = 0x20;
257 void usb_setup_endpoint(int idx, int max_pkt_size, int type)
259 struct usb_endpoint *ep;
261 usb_select_endpoint(idx);
262 ISP1582_MAXPKSZ = max_pkt_size;
263 /* |= is in the original firmware */
264 ISP1582_EPTYPE |= 0x1c | type;
265 /* clear buffer */
266 ISP1582_CTRLFUN |= 0x10;
267 ISP1582_INTEN |= (1 << (10 + idx));
269 ep = &(endpoints[epidx_n(idx)]);
270 ep->halt[epidx_dir(idx)] = 0;
271 ep->enabled[epidx_dir(idx)] = 1;
272 ep->out_in_progress = 0;
273 ep->in_min_len = -1;
274 ep->in_ack = 0;
275 ep->max_pkt_size[epidx_dir(idx)] = max_pkt_size;
278 void usb_disable_endpoint(int idx)
280 usb_select_endpoint(idx);
281 ISP1582_EPTYPE &= 8;
282 ISP1582_INTEN &= ~(1 << (10 + idx));
283 endpoints[epidx_n(idx)].enabled[epidx_dir(idx)] = 1;
286 void usb_reconnect(void)
288 int i;
289 ISP1582_MODE &= ~1; /* SOFTCT off */
290 for (i = 0; i < 10000; i++)
291 nop_f();
292 ISP1582_MODE |= 1; /* SOFTCT on */
295 void usb_cleanup(void)
297 ISP1582_MODE &= ~1; /* SOFTCT off */
300 void usb_setup(int reset)
302 int i;
304 for (i = 0; i < N_ENDPOINTS; i++)
305 endpoints[i].enabled[0] = endpoints[i].enabled[1] = 0;
307 ISP1582_UNLOCK = ISP1582_UNLOCK_CODE;
308 if (!reset)
309 ISP1582_MODE = 0x88; /* CLKAON | GLINTENA */
310 ISP1582_INTCONF = 0x57;
311 ISP1582_INTEN = 0xd39;
313 ISP1582_ADDRESS = reset ? 0x80: 0;
315 usb_setup_endpoint(ep_index(0, DIR_RX), 64, 0);
316 usb_setup_endpoint(ep_index(0, DIR_TX), 64, 0);
318 ISP1582_MODE |= 1; /* SOFTCT on */
320 usb_state = STATE_DEFAULT;
321 usb_remote_wakeup = 0;
324 static int usb_get_packet(unsigned char *buf, int max_len)
326 int len, i;
327 len = ISP1582_BUFLEN;
329 if (max_len < 0 || max_len > len)
330 max_len = len;
332 i = 0;
333 while (i < len)
335 unsigned short d = ISP1582_DATA;
336 if (i < max_len)
337 buf[i] = d & 0xff;
338 i++;
339 if (i < max_len)
340 buf[i] = (d >> 8) & 0xff;
341 i++;
343 return max_len;
346 static void usb_receive(int n)
348 int len;
350 if (endpoints[n].halt[DIR_RX]
351 || !endpoints[n].enabled[DIR_RX]
352 || endpoints[n].in_min_len < 0
353 || !endpoints[n].in_ack)
354 return;
356 endpoints[n].in_ack = 0;
358 usb_select_endpoint(ep_index(n, DIR_RX));
360 len = usb_get_packet(endpoints[n].in_buf + endpoints[n].in_ptr,
361 endpoints[n].in_max_len - endpoints[n].in_ptr);
362 endpoints[n].in_ptr += len;
363 if (endpoints[n].in_ptr >= endpoints[n].in_min_len) {
364 endpoints[n].in_min_len = -1;
365 if (endpoints[n].in_done)
366 (*(endpoints[n].in_done))(n, endpoints[n].in_buf,
367 endpoints[n].in_ptr);
371 static int usb_out_buffer_full(int ep)
373 usb_select_endpoint(ep_index(ep, DIR_TX));
374 if (ISP1582_EPTYPE & 4)
375 return (ISP1582_BUFSTAT & 3) == 3;
376 else
377 return (ISP1582_BUFSTAT & 3) != 0;
380 static void usb_send(int n)
382 int max_pkt_size, len;
383 int i;
384 unsigned char *p;
386 #ifdef LCD_DEBUG
387 if (endpoints[n].halt[DIR_TX])
388 log_char('H');
389 if (!endpoints[n].out_in_progress)
390 log_char('$');
391 #endif
393 if (endpoints[n].halt[DIR_TX]
394 || !endpoints[n].enabled[DIR_TX]
395 || !endpoints[n].out_in_progress)
396 return;
398 if (endpoints[n].out_ptr < 0)
400 endpoints[n].out_in_progress = 0;
401 if (endpoints[n].out_done)
402 (*(endpoints[n].out_done))(n, endpoints[n].out_buf,
403 endpoints[n].out_len);
404 return;
407 if (usb_out_buffer_full(n))
409 log_char('F');
410 return;
413 usb_select_endpoint(ep_index(n, DIR_TX));
414 max_pkt_size = endpoints[n].max_pkt_size[DIR_TX];
415 len = endpoints[n].out_len - endpoints[n].out_ptr;
416 if (len > max_pkt_size)
417 len = max_pkt_size;
419 log_char('0' + (len % 10));
420 ISP1582_BUFLEN = len;
421 p = endpoints[n].out_buf + endpoints[n].out_ptr;
422 i = 0;
423 while (len - i >= 2) {
424 ISP1582_DATA = p[i] | (p[i + 1] << 8);
425 i += 2;
427 if (i < len)
428 ISP1582_DATA = p[i];
430 endpoints[n].out_ptr += len;
433 if (endpoints[n].out_ptr == endpoints[n].out_len
434 && len < max_pkt_size)
436 if (endpoints[n].out_ptr == endpoints[n].out_len)
437 endpoints[n].out_ptr = -1;
440 static void usb_stall_endpoint(int idx)
442 usb_select_endpoint(idx);
443 ISP1582_CTRLFUN |= 1;
444 endpoints[epidx_n(idx)].halt[epidx_dir(idx)] = 1;
447 static void usb_unstall_endpoint(int idx)
449 usb_select_endpoint(idx);
450 ISP1582_CTRLFUN &= ~1;
451 ISP1582_EPTYPE &= ~8;
452 ISP1582_EPTYPE |= 8;
453 ISP1582_CTRLFUN |= 0x10;
454 if (epidx_dir(idx) == DIR_TX)
455 endpoints[epidx_n(idx)].out_in_progress = 0;
456 else
458 endpoints[epidx_n(idx)].in_min_len = -1;
459 endpoints[epidx_n(idx)].in_ack = 0;
461 endpoints[epidx_n(idx)].halt[epidx_dir(idx)] = 0;
464 static void usb_status_ack(int dir)
466 log_char(dir ? '@' : '#');
467 usb_select_endpoint(ep_index(0, dir));
468 ISP1582_CTRLFUN |= 2;
471 static void usb_set_address(int adr)
473 ISP1582_ADDRESS = adr | 0x80;
476 static void usb_data_stage_enable(int dir)
478 usb_select_endpoint(ep_index(0, dir));
479 ISP1582_CTRLFUN |= 4;
482 static void usb_request_error(void)
484 usb_stall_endpoint(ep_index(0, DIR_TX));
485 usb_stall_endpoint(ep_index(0, DIR_RX));
488 static void usb_receive_block(unsigned char *buf, int min_len,
489 int max_len,
490 void (*in_done)(int, unsigned char *, int),
491 int ep)
493 endpoints[ep].in_done = in_done;
494 endpoints[ep].in_buf = buf;
495 endpoints[ep].in_max_len = max_len;
496 endpoints[ep].in_min_len = min_len;
497 endpoints[ep].in_ptr = 0;
498 usb_receive(ep);
501 static void usb_send_block(unsigned char *buf, int len,
502 void (*done)(int, unsigned char *, int),
503 int ep)
505 endpoints[ep].out_done = done;
506 endpoints[ep].out_buf = buf;
507 endpoints[ep].out_len = len;
508 endpoints[ep].out_ptr = 0;
509 endpoints[ep].out_in_progress = 1;
510 usb_send(ep);
513 static void out_send_status(int n, unsigned char *buf, int len)
515 (void)n;
516 (void)buf;
517 (void)len;
518 usb_status_ack(DIR_RX);
521 static void usb_send_block_and_status(unsigned char *buf, int len, int ep)
523 usb_send_block(buf, len, out_send_status, ep);
526 static void usb_setup_set_address(int adr)
528 usb_set_address(adr);
529 usb_state = adr ? STATE_ADDRESS : STATE_DEFAULT;
530 usb_status_ack(DIR_TX);
533 static void usb_send_descriptor(unsigned char *device_descriptor,
534 int descriptor_len, int buffer_len)
536 int len = descriptor_len < buffer_len ? descriptor_len : buffer_len;
537 usb_send_block_and_status(device_descriptor, len, 0);
540 static void usb_setup_get_descriptor(int type, int index, int lang, int len)
542 (void)lang;
543 usb_data_stage_enable(DIR_TX);
544 switch (type)
546 case 1:
547 if (index == 0)
548 usb_send_descriptor(device_descriptor,
549 sizeof(device_descriptor), len);
550 else
551 usb_request_error();
552 break;
553 case 2:
554 if (index == 0)
555 usb_send_descriptor(cfg_descriptor,
556 sizeof(cfg_descriptor), len);
557 else
558 usb_request_error();
559 break;
560 case 3:
561 if (index == 0)
562 usb_send_descriptor(lang_descriptor,
563 sizeof(lang_descriptor), len);
564 else if (index <= N_STRING_DESCRIPTORS)
565 usb_send_descriptor(string_descriptor[index - 1],
566 string_descriptor[index - 1][0],
567 len);
568 else
569 usb_request_error();
570 break;
571 default:
572 usb_request_error();
576 static void usb_setup_get_configuration(void)
578 setup_out_buf[0] = (usb_state == STATE_CONFIGURED) ? 1 : 0;
579 usb_data_stage_enable(DIR_TX);
580 usb_send_block_and_status(setup_out_buf, 1, 0);
583 static void usb_setup_interface(void)
585 usb_setup_endpoint(ep_index(1, DIR_RX), 64, TYPE_BULK);
586 usb_setup_endpoint(ep_index(1, DIR_TX), 64, TYPE_BULK);
589 static void usb_setup_set_configuration(int value)
591 switch (value)
593 case 0:
594 usb_disable_endpoint(ep_index(1, DIR_RX));
595 usb_disable_endpoint(ep_index(1, DIR_TX));
596 usb_state = STATE_ADDRESS;
597 usb_status_ack(DIR_TX);
598 break;
599 case 1:
600 usb_setup_interface();
601 usb_state = STATE_CONFIGURED;
602 usb_status_ack(DIR_TX);
603 break;
604 default:
605 usb_request_error();
609 static void usb_send_status(void)
611 usb_data_stage_enable(DIR_TX);
612 usb_send_block_and_status(setup_out_buf, 2, 0);
615 static void usb_setup_get_device_status(void)
617 setup_out_buf[0] = (usb_remote_wakeup != 0) ? 2 : 0;
618 setup_out_buf[1] = 0;
619 usb_send_status();
622 static void usb_setup_clear_device_feature(int feature)
624 if (feature == 1) {
625 usb_remote_wakeup = 0;
626 usb_status_ack(DIR_TX);
627 } else
628 usb_request_error();
631 static void usb_setup_set_device_feature(int feature)
633 if (feature == 1) {
634 usb_remote_wakeup = 1;
635 usb_status_ack(DIR_TX);
636 } else
637 usb_request_error();
640 static void usb_setup_clear_endpoint_feature(int endpoint, int feature)
642 if (usb_state != STATE_CONFIGURED || feature != 0)
643 usb_request_error();
644 else if ((endpoint & 0xf) == 1)
646 usb_unstall_endpoint(ep_index(endpoint & 0xf, endpoint >> 7));
647 usb_status_ack(DIR_TX);
649 else
650 usb_request_error();
653 static void usb_setup_set_endpoint_feature(int endpoint, int feature)
655 if (usb_state != STATE_CONFIGURED || feature != 0)
656 usb_request_error();
657 else if ((endpoint & 0xf) == 1)
659 usb_stall_endpoint(ep_index(endpoint & 0xf, endpoint >> 7));
660 usb_status_ack(DIR_TX);
662 else
663 usb_request_error();
666 static void usb_setup_get_interface_status(int interface)
668 if (usb_state != STATE_CONFIGURED || interface != 0)
669 usb_request_error();
670 else
672 setup_out_buf[0] = setup_out_buf[1] = 0;
673 usb_send_status();
677 static void usb_setup_get_endpoint_status(int endpoint)
679 if ((usb_state == STATE_CONFIGURED && (endpoint & 0xf) <= 1)
680 || (usb_state == STATE_ADDRESS && (endpoint & 0xf) == 0))
682 setup_out_buf[0] = endpoints[endpoint & 0xf].halt[endpoint >> 7];
683 setup_out_buf[1] = 0;
684 usb_send_status();
686 else
687 usb_request_error();
690 static void usb_setup_get_interface(int interface)
692 if (usb_state != STATE_CONFIGURED || interface != 0)
693 usb_request_error();
694 else
696 setup_out_buf[0] = 0;
697 usb_data_stage_enable(DIR_TX);
698 usb_send_block_and_status(setup_out_buf, 1, 0);
702 static void usb_setup_set_interface(int interface, int setting)
704 if (usb_state != STATE_CONFIGURED || interface != 0 || setting != 0)
705 usb_request_error();
706 else
708 usb_setup_interface();
709 usb_status_ack(DIR_TX);
713 static void usb_handle_setup_pkt(unsigned char *pkt)
715 switch ((pkt[0] << 8) | pkt[1])
717 case 0x0005:
718 log_char('A');
719 usb_setup_set_address(pkt[2]);
720 break;
721 case 0x8006:
722 log_char('D');
723 usb_setup_get_descriptor(pkt[3], pkt[2], (pkt[5] << 8) | pkt[4],
724 (pkt[7] << 8) | pkt[6]);
725 break;
726 case 0x8008:
727 usb_setup_get_configuration();
728 break;
729 case 0x0009:
730 usb_setup_set_configuration(pkt[2]);
731 break;
732 case 0x8000:
733 usb_setup_get_device_status();
734 break;
735 case 0x8100:
736 usb_setup_get_interface_status(pkt[4]);
737 break;
738 case 0x8200:
739 usb_setup_get_endpoint_status(pkt[4]);
740 break;
741 case 0x0001:
742 usb_setup_clear_device_feature(pkt[2]);
743 break;
744 case 0x0201:
745 usb_setup_clear_endpoint_feature(pkt[4], pkt[2]);
746 break;
747 case 0x0003:
748 usb_setup_set_device_feature(pkt[2]);
749 break;
750 case 0x0203:
751 usb_setup_set_endpoint_feature(pkt[4], pkt[2]);
752 break;
753 case 0x810a:
754 usb_setup_get_interface(pkt[4]);
755 break;
756 case 0x010b:
757 usb_setup_set_interface(pkt[4], pkt[2]);
758 break;
759 case 0x0103:
760 /* set interface feature */
761 case 0x0101:
762 /* clear interface feature */
763 case 0x0007:
764 /* set descriptor */
765 case 0x820c:
766 /* synch frame */
767 default:
768 usb_request_error();
772 static void usb_handle_setup_rx(void)
774 int len;
775 #ifdef LCD_DEBUG
776 char s[20];
777 int i;
778 #endif
779 usb_select_setup_endpoint();
780 len = usb_get_packet(setup_pkt_buf, 8);
782 if (len == 8)
783 usb_handle_setup_pkt(setup_pkt_buf);
785 #ifdef LCD_DEBUG
787 snprintf(s, 10, "l%02x", len);
788 lcd_puts(0, 5, s);
790 for (i = 0; i < 8; i++)
791 snprintf(s + i * 2, 3, "%02x", setup_pkt_buf[i]);
792 lcd_puts(0, 0, s);
793 lcd_update();
794 #endif
797 static void usb_handle_data_int(int ep, int dir)
799 if (dir == DIR_TX)
800 usb_send(ep);
801 else
803 endpoints[ep].in_ack = 1;
804 usb_receive(ep);
808 static void usb_handle_int(int i)
810 #ifdef LCD_DEBUG
812 char s[10];
813 snprintf(s, sizeof(s), "%02d", i);
814 lcd_puts(0, 2, s);
815 lcd_update();
817 int_count[i]++;
818 if (i == 10)
819 log_char('o');
820 if (i == 11)
821 log_char('i');
822 if (i == 12)
823 log_char('O');
824 if (i == 13)
825 log_char('I');
826 #endif
828 if (i >= 10)
829 usb_handle_data_int((i - 10) / 2, i % 2);
830 else
832 switch (i)
834 case 0:
835 log_char('r');
836 usb_setup(1);
837 break;
838 case 8:
839 log_char('s');
840 usb_handle_setup_rx();
841 break;
847 void usb_handle_interrupts(void)
849 #ifdef LCD_DEBUG
850 char s[20];
851 #endif
853 while (1)
855 unsigned long ints;
856 int i;
858 #ifdef LCD_DEBUG
860 snprintf(s, sizeof(s), "i%08lx", ISP1582_INT);
861 lcd_puts(0, 2, s);
863 #endif
865 ints = ISP1582_INT & ISP1582_INTEN;
866 if (!ints) break;
868 i = 0;
869 while (!(ints & (1 << i)))
870 i++;
871 ISP1582_INT = 1 << i;
872 usb_handle_int(i);
874 #ifdef LCD_DEBUG
875 for (i = 0; i < 8; i++)
876 snprintf(s + i * 2, 3, "%02x", int_count[i]);
877 lcd_puts(0, 6, s);
878 for (i = 0; i < 8; i++)
879 snprintf(s + i * 2, 3, "%02x", int_count[i + 8]);
880 lcd_puts(0, 7, s);
881 #endif
883 #ifdef LCD_DEBUG
885 lcd_puts(0, 3, usb_connected() ? "C" : "N");
886 lcd_update();
888 #endif
891 static inline int fifo_mod(int n)
893 return (n >= SERIAL_BUF_SIZE) ? n - SERIAL_BUF_SIZE : n;
896 static void fifo_init(struct serial_fifo *fifo)
898 fifo->head = fifo->tail = 0;
901 static int fifo_empty(struct serial_fifo *fifo)
903 return fifo->head == fifo->tail;
906 static int fifo_full(struct serial_fifo *fifo)
908 return fifo_mod(fifo->head + 1) == fifo->tail;
911 static void fifo_remove(struct serial_fifo *fifo, int n)
913 fifo->tail = fifo_mod(fifo->tail + n);
917 Not used:
918 static void fifo_add(struct serial_fifo *fifo, int n)
920 fifo->head = fifo_mod(fifo->head + n);
923 static void fifo_free_block(struct serial_fifo *fifo,
924 unsigned char **ptr, int *len)
926 *ptr = fifo->buf + fifo->head;
927 if (fifo->head >= fifo->tail)
929 int l = SERIAL_BUF_SIZE - fifo->head;
930 if (fifo->tail == 0)
931 l--;
932 *len = l;
934 else
935 *len = fifo->tail - fifo->head - 1;
939 static int fifo_free_space(struct serial_fifo *fifo)
941 if (fifo->head >= fifo->tail)
942 return SERIAL_BUF_SIZE - (fifo->head - fifo->tail) - 1;
943 else
944 return fifo->tail - fifo->head - 1;
947 static int fifo_get_byte(struct serial_fifo *fifo)
949 int r = fifo->buf[fifo->tail];
950 fifo->tail = fifo_mod(fifo->tail + 1);
951 return r;
954 static void fifo_put_byte(struct serial_fifo *fifo, int b)
956 fifo->buf[fifo->head] = b;
957 fifo->head = fifo_mod(fifo->head + 1);
960 static void fifo_full_block(struct serial_fifo *fifo,
961 unsigned char **ptr, int *len)
963 *ptr = fifo->buf + fifo->tail;
964 if (fifo->head >= fifo->tail)
965 *len = fifo->head - fifo->tail;
966 else
967 *len = SERIAL_BUF_SIZE - fifo->tail;
970 static void serial_fill_in_fifo(int ep, unsigned char *buf, int len);
971 static void serial_free_out_fifo(int ep, unsigned char *buf, int len);
973 static void serial_restart_input(int ep)
975 if (fifo_free_space(&serial_in_fifo) >= 64)
976 usb_receive_block(serial_in_pkt, 1, 64, serial_fill_in_fifo, ep);
979 static void serial_fill_in_fifo(int ep, unsigned char *buf, int len)
981 int i;
982 for (i = 0; i < len; i++)
983 fifo_put_byte(&serial_in_fifo, buf[i]);
984 serial_restart_input(ep);
987 static void serial_restart_output(int ep)
989 unsigned char *block;
990 int blen;
991 fifo_full_block(&serial_out_fifo, &block, &blen);
992 if (blen)
994 #ifdef LCD_DEBUG
995 char s[20];
996 snprintf(s, sizeof(s), "o%03lx/%03x", block - serial_out_fifo.buf,
997 blen);
998 lcd_puts(0, 2, s);
999 lcd_update();
1000 #endif
1001 usb_send_block(block, blen, serial_free_out_fifo, ep);
1005 static void serial_free_out_fifo(int ep, unsigned char *buf, int len)
1007 (void)buf;
1008 fifo_remove(&serial_out_fifo, len);
1009 serial_restart_output(ep);
1012 void usb_serial_handle(void)
1014 #ifdef BUTTONS
1015 static int t = 0;
1017 t++;
1018 if (t >= 1000)
1020 int b;
1021 t = 0;
1022 yield();
1023 b = button_get(false);
1024 if (b == BUTTON_PLAY)
1025 system_reboot();
1026 else if (b & BUTTON_REL)
1027 usb_reconnect();
1029 #endif
1032 if (!usb_connect_state)
1034 if (usb_connected())
1036 int i;
1037 GPIO3_SET = 4;
1038 (*(volatile unsigned long *)0x80005004) = 2;
1039 (*(volatile unsigned long *)0x80005008) = 0;
1040 for (i = 0; i < 100000; i++)
1041 nop_f();
1042 usb_setup(0);
1043 usb_connect_state = 1;
1046 else
1048 if (!usb_connected())
1050 usb_connect_state = 0;
1051 usb_cleanup();
1053 else
1055 usb_handle_interrupts();
1057 if (usb_state == STATE_CONFIGURED)
1059 if (endpoints[1].in_min_len < 0)
1060 serial_restart_input(1);
1061 if (!endpoints[1].out_in_progress)
1062 serial_restart_output(1);
1070 Not used:
1071 static int usb_serial_in_empty(void)
1073 return fifo_empty(&serial_in_fifo);
1077 int usb_serial_get_byte(void)
1079 while (fifo_empty(&serial_in_fifo))
1080 usb_serial_handle();
1081 return fifo_get_byte(&serial_in_fifo);
1084 int usb_serial_try_get_byte(void)
1086 int r;
1087 if (fifo_empty(&serial_in_fifo))
1088 r = -1;
1089 else
1090 r = fifo_get_byte(&serial_in_fifo);
1091 usb_serial_handle();
1092 return r;
1096 Not used:
1097 static int usb_serial_out_full(void)
1099 return fifo_full(&serial_out_fifo);
1103 void usb_serial_put_byte(int b)
1105 while (fifo_full(&serial_out_fifo))
1106 usb_serial_handle();
1107 fifo_put_byte(&serial_out_fifo, b);
1108 usb_serial_handle();
1111 int usb_serial_try_put_byte(int b)
1113 int r = -1;
1114 if (!fifo_full(&serial_out_fifo))
1116 fifo_put_byte(&serial_out_fifo, b);
1117 r = 0;
1119 usb_serial_handle();
1120 return r;
1123 void usb_serial_init(void)
1125 fifo_init(&serial_in_fifo);
1126 fifo_init(&serial_out_fifo);
1127 usb_connect_state = 0;