Redo r29168. Use similar but existing mechanism in the action system which seems...
[kugel-rb.git] / firmware / ifp_usb_serial.c
blob5392de0d52f2bb966301b5928a0d580dd7ae3d39
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 #endif
43 #define ISP1582_BASE (0x24100000)
44 #define ISP1582_ADDRESS (*(volatile unsigned char *)ISP1582_BASE)
45 #define ISP1582_MODE (*(volatile unsigned short *)(ISP1582_BASE + 0xc))
46 #define ISP1582_INTCONF (*(volatile unsigned char *)(ISP1582_BASE + 0x10))
47 #define ISP1582_OTG (*(volatile unsigned char *)(ISP1582_BASE + 0x12))
48 #define ISP1582_INTEN (*(volatile unsigned long *)(ISP1582_BASE + 0x14))
50 #define ISP1582_EPINDEX (*(volatile unsigned char *)(ISP1582_BASE + 0x2c))
51 #define ISP1582_CTRLFUN (*(volatile unsigned char *)(ISP1582_BASE + 0x28))
52 #define ISP1582_DATA (*(volatile unsigned short *)(ISP1582_BASE + 0x20))
53 #define ISP1582_BUFLEN (*(volatile unsigned short *)(ISP1582_BASE + 0x1c))
54 #define ISP1582_BUFSTAT (*(volatile unsigned char *)(ISP1582_BASE + 0x1e))
55 #define ISP1582_MAXPKSZ (*(volatile unsigned short *)(ISP1582_BASE + 0x04))
56 #define ISP1582_EPTYPE (*(volatile unsigned short *)(ISP1582_BASE + 0x08))
58 #define ISP1582_INT (*(volatile unsigned long *)(ISP1582_BASE + 0x18))
59 #define ISP1582_CHIPID (*(volatile unsigned long *)(ISP1582_BASE + 0x70))
60 #define ISP1582_FRAMENO (*(volatile unsigned short *)(ISP1582_BASE + 0x74))
61 #define ISP1582_UNLOCK (*(volatile unsigned short *)(ISP1582_BASE + 0x7c))
63 #define ISP1582_UNLOCK_CODE 0xaa37
65 #define TYPE_BULK 2
67 #define STATE_DEFAULT 0
68 #define STATE_ADDRESS 1
69 #define STATE_CONFIGURED 2
71 #define N_ENDPOINTS 2
73 struct usb_endpoint
75 unsigned char *out_buf;
76 short out_len;
77 short out_ptr;
78 void (*out_done)(int, unsigned char *, int);
79 unsigned char out_in_progress;
81 unsigned char *in_buf;
82 short in_min_len;
83 short in_max_len;
84 short in_ptr;
85 void (*in_done)(int, unsigned char *, int);
86 unsigned char in_ack;
88 unsigned char halt[2];
89 unsigned char enabled[2];
90 short max_pkt_size[2];
93 static char usb_connect_state;
95 static struct usb_endpoint endpoints[N_ENDPOINTS];
97 static unsigned char setup_pkt_buf[8];
98 static unsigned char setup_out_buf[8];
99 static unsigned char usb_state;
100 static unsigned char usb_remote_wakeup;
102 #ifdef LCD_DEBUG
103 static unsigned char int_count[32];
105 static int log_pos_x = 0;
106 static int log_pos_y = 3;
107 #endif
109 static void nop_f(void)
113 #ifdef LCD_DEBUG
114 static void log_char(char c)
116 char s[2];
118 s[0] = c;
119 s[1] = 0;
121 lcd_puts(log_pos_x, log_pos_y, s);
122 lcd_update();
123 log_pos_x++;
124 if (log_pos_x >= 16)
126 log_pos_x = 0;
127 log_pos_y++;
128 if (log_pos_y > 5)
129 log_pos_y = 3;
132 #else
133 #define log_char(c)
134 #endif
136 #define SERIAL_BUF_SIZE 1024
138 struct serial_fifo
140 unsigned char buf[SERIAL_BUF_SIZE];
141 short head, tail;
144 static struct serial_fifo serial_in_fifo;
145 static struct serial_fifo serial_out_fifo;
146 static unsigned char serial_in_pkt[64];
148 static unsigned char device_descriptor[18] = {
149 0x12, /* length */
150 0x01, /* descriptor type */
151 0x10, 0x01, /* USB version (1.1) */
152 0xff, 0xff, /* class and subclass */
153 0xff, /* protocol */
154 0x40, /* max packet size 0 */
155 0x02, 0x41, /* vendor (iRiver) */
156 0x07, 0xee, /* product (0xee07) */
157 0x01, 0x00, /* device version */
158 0x01, /* manufacturer string */
159 0x02, /* product string */
160 0x00, /* serial number string */
161 0x01 /* number of configurations */
164 static unsigned char cfg_descriptor[32] = {
165 0x09, /* length */
166 0x02, /* descriptor type */
167 0x20, 0x00, /* total length */
168 0x01, /* number of interfaces */
169 0x01, /* configuration value */
170 0x00, /* configuration string */
171 0x80, /* attributes (none) */
172 0x32, /* max power (100 mA) */
173 /* interface descriptor */
174 0x09, /* length */
175 0x04, /* descriptor type */
176 0x00, /* interface number */
177 0x00, /* alternate setting */
178 0x02, /* number of endpoints */
179 0xff, /* interface class */
180 0xff, /* interface subclass */
181 0xff, /* interface protocol */
182 0x00, /* interface string */
183 /* endpoint IN */
184 0x07, /* length */
185 0x05, /* descriptor type */
186 0x81, /* endpoint 1 IN */
187 0x02, /* attributes (bulk) */
188 0x40, 0x00, /* max packet size */
189 0x00, /* interval */
190 /* endpoint OUT */
191 0x07, /* length */
192 0x05, /* descriptor type */
193 0x01, /* endpoint 1 IN */
194 0x02, /* attributes (bulk) */
195 0x40, 0x00, /* max packet size */
196 0x00 /* interval */
199 static unsigned char lang_descriptor[4] = {
200 0x04, /* length */
201 0x03, /* descriptor type */
202 0x09, 0x04 /* English (US) */
205 #define N_STRING_DESCRIPTORS 2
207 static unsigned char string_descriptor_vendor[] = {
208 0x2e, 0x03,
209 'i', 0, 'R', 0, 'i', 0, 'v', 0, 'e', 0, 'r', 0, ' ', 0, 'L', 0,
210 't', 0, 'd', 0, ' ', 0, 'a', 0, 'n', 0, 'd', 0, ' ', 0, 'R', 0,
211 'o', 0, 'c', 0, 'k', 0, 'b', 0, 'o', 0, 'x', 0};
213 static unsigned char string_descriptor_product[] = {
214 0x1c, 0x03,
215 'i', 0, 'R', 0, 'i', 0, 'v', 0, 'e', 0, 'r', 0, ' ', 0, 'i', 0,
216 'F', 0, 'P', 0, '7', 0, '0', 0, '0', 0};
218 static unsigned char *string_descriptor[N_STRING_DESCRIPTORS] = {
219 string_descriptor_vendor,
220 string_descriptor_product
223 static inline int ep_index(int n, int dir)
225 return (n << 1) | dir;
228 static inline int epidx_dir(int idx)
230 return idx & 1;
233 static inline int epidx_n(int idx)
235 return idx >> 1;
238 int usb_connected(void)
240 return GPIO7_READ & 1;
243 static inline void usb_select_endpoint(int idx)
245 ISP1582_EPINDEX = idx;
248 static inline void usb_select_setup_endpoint(void)
250 ISP1582_EPINDEX = 0x20;
253 void usb_setup_endpoint(int idx, int max_pkt_size, int type)
255 struct usb_endpoint *ep;
257 usb_select_endpoint(idx);
258 ISP1582_MAXPKSZ = max_pkt_size;
259 /* |= is in the original firmware */
260 ISP1582_EPTYPE |= 0x1c | type;
261 /* clear buffer */
262 ISP1582_CTRLFUN |= 0x10;
263 ISP1582_INTEN |= (1 << (10 + idx));
265 ep = &(endpoints[epidx_n(idx)]);
266 ep->halt[epidx_dir(idx)] = 0;
267 ep->enabled[epidx_dir(idx)] = 1;
268 ep->out_in_progress = 0;
269 ep->in_min_len = -1;
270 ep->in_ack = 0;
271 ep->max_pkt_size[epidx_dir(idx)] = max_pkt_size;
274 void usb_disable_endpoint(int idx)
276 usb_select_endpoint(idx);
277 ISP1582_EPTYPE &= 8;
278 ISP1582_INTEN &= ~(1 << (10 + idx));
279 endpoints[epidx_n(idx)].enabled[epidx_dir(idx)] = 1;
282 void usb_reconnect(void)
284 int i;
285 ISP1582_MODE &= ~1; /* SOFTCT off */
286 for (i = 0; i < 10000; i++)
287 nop_f();
288 ISP1582_MODE |= 1; /* SOFTCT on */
291 void usb_cleanup(void)
293 ISP1582_MODE &= ~1; /* SOFTCT off */
296 void usb_setup(int reset)
298 int i;
300 for (i = 0; i < N_ENDPOINTS; i++)
301 endpoints[i].enabled[0] = endpoints[i].enabled[1] = 0;
303 ISP1582_UNLOCK = ISP1582_UNLOCK_CODE;
304 if (!reset)
305 ISP1582_MODE = 0x88; /* CLKAON | GLINTENA */
306 ISP1582_INTCONF = 0x57;
307 ISP1582_INTEN = 0xd39;
309 ISP1582_ADDRESS = reset ? 0x80: 0;
311 usb_setup_endpoint(ep_index(0, DIR_RX), 64, 0);
312 usb_setup_endpoint(ep_index(0, DIR_TX), 64, 0);
314 ISP1582_MODE |= 1; /* SOFTCT on */
316 usb_state = STATE_DEFAULT;
317 usb_remote_wakeup = 0;
320 static int usb_get_packet(unsigned char *buf, int max_len)
322 int len, i;
323 len = ISP1582_BUFLEN;
325 if (max_len < 0 || max_len > len)
326 max_len = len;
328 i = 0;
329 while (i < len)
331 unsigned short d = ISP1582_DATA;
332 if (i < max_len)
333 buf[i] = d & 0xff;
334 i++;
335 if (i < max_len)
336 buf[i] = (d >> 8) & 0xff;
337 i++;
339 return max_len;
342 static void usb_receive(int n)
344 int len;
346 if (endpoints[n].halt[DIR_RX]
347 || !endpoints[n].enabled[DIR_RX]
348 || endpoints[n].in_min_len < 0
349 || !endpoints[n].in_ack)
350 return;
352 endpoints[n].in_ack = 0;
354 usb_select_endpoint(ep_index(n, DIR_RX));
356 len = usb_get_packet(endpoints[n].in_buf + endpoints[n].in_ptr,
357 endpoints[n].in_max_len - endpoints[n].in_ptr);
358 endpoints[n].in_ptr += len;
359 if (endpoints[n].in_ptr >= endpoints[n].in_min_len) {
360 endpoints[n].in_min_len = -1;
361 if (endpoints[n].in_done)
362 (*(endpoints[n].in_done))(n, endpoints[n].in_buf,
363 endpoints[n].in_ptr);
367 static int usb_out_buffer_full(int ep)
369 usb_select_endpoint(ep_index(ep, DIR_TX));
370 if (ISP1582_EPTYPE & 4)
371 return (ISP1582_BUFSTAT & 3) == 3;
372 else
373 return (ISP1582_BUFSTAT & 3) != 0;
376 static void usb_send(int n)
378 int max_pkt_size, len;
379 int i;
380 unsigned char *p;
382 #ifdef LCD_DEBUG
383 if (endpoints[n].halt[DIR_TX])
384 log_char('H');
385 if (!endpoints[n].out_in_progress)
386 log_char('$');
387 #endif
389 if (endpoints[n].halt[DIR_TX]
390 || !endpoints[n].enabled[DIR_TX]
391 || !endpoints[n].out_in_progress)
392 return;
394 if (endpoints[n].out_ptr < 0)
396 endpoints[n].out_in_progress = 0;
397 if (endpoints[n].out_done)
398 (*(endpoints[n].out_done))(n, endpoints[n].out_buf,
399 endpoints[n].out_len);
400 return;
403 if (usb_out_buffer_full(n))
405 log_char('F');
406 return;
409 usb_select_endpoint(ep_index(n, DIR_TX));
410 max_pkt_size = endpoints[n].max_pkt_size[DIR_TX];
411 len = endpoints[n].out_len - endpoints[n].out_ptr;
412 if (len > max_pkt_size)
413 len = max_pkt_size;
415 log_char('0' + (len % 10));
416 ISP1582_BUFLEN = len;
417 p = endpoints[n].out_buf + endpoints[n].out_ptr;
418 i = 0;
419 while (len - i >= 2) {
420 ISP1582_DATA = p[i] | (p[i + 1] << 8);
421 i += 2;
423 if (i < len)
424 ISP1582_DATA = p[i];
426 endpoints[n].out_ptr += len;
429 if (endpoints[n].out_ptr == endpoints[n].out_len
430 && len < max_pkt_size)
432 if (endpoints[n].out_ptr == endpoints[n].out_len)
433 endpoints[n].out_ptr = -1;
436 static void usb_stall_endpoint(int idx)
438 usb_select_endpoint(idx);
439 ISP1582_CTRLFUN |= 1;
440 endpoints[epidx_n(idx)].halt[epidx_dir(idx)] = 1;
443 static void usb_unstall_endpoint(int idx)
445 usb_select_endpoint(idx);
446 ISP1582_CTRLFUN &= ~1;
447 ISP1582_EPTYPE &= ~8;
448 ISP1582_EPTYPE |= 8;
449 ISP1582_CTRLFUN |= 0x10;
450 if (epidx_dir(idx) == DIR_TX)
451 endpoints[epidx_n(idx)].out_in_progress = 0;
452 else
454 endpoints[epidx_n(idx)].in_min_len = -1;
455 endpoints[epidx_n(idx)].in_ack = 0;
457 endpoints[epidx_n(idx)].halt[epidx_dir(idx)] = 0;
460 static void usb_status_ack(int dir)
462 log_char(dir ? '@' : '#');
463 usb_select_endpoint(ep_index(0, dir));
464 ISP1582_CTRLFUN |= 2;
467 static void usb_set_address(int adr)
469 ISP1582_ADDRESS = adr | 0x80;
472 static void usb_data_stage_enable(int dir)
474 usb_select_endpoint(ep_index(0, dir));
475 ISP1582_CTRLFUN |= 4;
478 static void usb_request_error(void)
480 usb_stall_endpoint(ep_index(0, DIR_TX));
481 usb_stall_endpoint(ep_index(0, DIR_RX));
484 static void usb_receive_block(unsigned char *buf, int min_len,
485 int max_len,
486 void (*in_done)(int, unsigned char *, int),
487 int ep)
489 endpoints[ep].in_done = in_done;
490 endpoints[ep].in_buf = buf;
491 endpoints[ep].in_max_len = max_len;
492 endpoints[ep].in_min_len = min_len;
493 endpoints[ep].in_ptr = 0;
494 usb_receive(ep);
497 static void usb_send_block(unsigned char *buf, int len,
498 void (*done)(int, unsigned char *, int),
499 int ep)
501 endpoints[ep].out_done = done;
502 endpoints[ep].out_buf = buf;
503 endpoints[ep].out_len = len;
504 endpoints[ep].out_ptr = 0;
505 endpoints[ep].out_in_progress = 1;
506 usb_send(ep);
509 static void out_send_status(int n, unsigned char *buf, int len)
511 (void)n;
512 (void)buf;
513 (void)len;
514 usb_status_ack(DIR_RX);
517 static void usb_send_block_and_status(unsigned char *buf, int len, int ep)
519 usb_send_block(buf, len, out_send_status, ep);
522 static void usb_setup_set_address(int adr)
524 usb_set_address(adr);
525 usb_state = adr ? STATE_ADDRESS : STATE_DEFAULT;
526 usb_status_ack(DIR_TX);
529 static void usb_send_descriptor(unsigned char *device_descriptor,
530 int descriptor_len, int buffer_len)
532 int len = descriptor_len < buffer_len ? descriptor_len : buffer_len;
533 usb_send_block_and_status(device_descriptor, len, 0);
536 static void usb_setup_get_descriptor(int type, int index, int lang, int len)
538 (void)lang;
539 usb_data_stage_enable(DIR_TX);
540 switch (type)
542 case 1:
543 if (index == 0)
544 usb_send_descriptor(device_descriptor,
545 sizeof(device_descriptor), len);
546 else
547 usb_request_error();
548 break;
549 case 2:
550 if (index == 0)
551 usb_send_descriptor(cfg_descriptor,
552 sizeof(cfg_descriptor), len);
553 else
554 usb_request_error();
555 break;
556 case 3:
557 if (index == 0)
558 usb_send_descriptor(lang_descriptor,
559 sizeof(lang_descriptor), len);
560 else if (index <= N_STRING_DESCRIPTORS)
561 usb_send_descriptor(string_descriptor[index - 1],
562 string_descriptor[index - 1][0],
563 len);
564 else
565 usb_request_error();
566 break;
567 default:
568 usb_request_error();
572 static void usb_setup_get_configuration(void)
574 setup_out_buf[0] = (usb_state == STATE_CONFIGURED) ? 1 : 0;
575 usb_data_stage_enable(DIR_TX);
576 usb_send_block_and_status(setup_out_buf, 1, 0);
579 static void usb_setup_interface(void)
581 usb_setup_endpoint(ep_index(1, DIR_RX), 64, TYPE_BULK);
582 usb_setup_endpoint(ep_index(1, DIR_TX), 64, TYPE_BULK);
585 static void usb_setup_set_configuration(int value)
587 switch (value)
589 case 0:
590 usb_disable_endpoint(ep_index(1, DIR_RX));
591 usb_disable_endpoint(ep_index(1, DIR_TX));
592 usb_state = STATE_ADDRESS;
593 usb_status_ack(DIR_TX);
594 break;
595 case 1:
596 usb_setup_interface();
597 usb_state = STATE_CONFIGURED;
598 usb_status_ack(DIR_TX);
599 break;
600 default:
601 usb_request_error();
605 static void usb_send_status(void)
607 usb_data_stage_enable(DIR_TX);
608 usb_send_block_and_status(setup_out_buf, 2, 0);
611 static void usb_setup_get_device_status(void)
613 setup_out_buf[0] = (usb_remote_wakeup != 0) ? 2 : 0;
614 setup_out_buf[1] = 0;
615 usb_send_status();
618 static void usb_setup_clear_device_feature(int feature)
620 if (feature == 1) {
621 usb_remote_wakeup = 0;
622 usb_status_ack(DIR_TX);
623 } else
624 usb_request_error();
627 static void usb_setup_set_device_feature(int feature)
629 if (feature == 1) {
630 usb_remote_wakeup = 1;
631 usb_status_ack(DIR_TX);
632 } else
633 usb_request_error();
636 static void usb_setup_clear_endpoint_feature(int endpoint, int feature)
638 if (usb_state != STATE_CONFIGURED || feature != 0)
639 usb_request_error();
640 else if ((endpoint & 0xf) == 1)
642 usb_unstall_endpoint(ep_index(endpoint & 0xf, endpoint >> 7));
643 usb_status_ack(DIR_TX);
645 else
646 usb_request_error();
649 static void usb_setup_set_endpoint_feature(int endpoint, int feature)
651 if (usb_state != STATE_CONFIGURED || feature != 0)
652 usb_request_error();
653 else if ((endpoint & 0xf) == 1)
655 usb_stall_endpoint(ep_index(endpoint & 0xf, endpoint >> 7));
656 usb_status_ack(DIR_TX);
658 else
659 usb_request_error();
662 static void usb_setup_get_interface_status(int interface)
664 if (usb_state != STATE_CONFIGURED || interface != 0)
665 usb_request_error();
666 else
668 setup_out_buf[0] = setup_out_buf[1] = 0;
669 usb_send_status();
673 static void usb_setup_get_endpoint_status(int endpoint)
675 if ((usb_state == STATE_CONFIGURED && (endpoint & 0xf) <= 1)
676 || (usb_state == STATE_ADDRESS && (endpoint & 0xf) == 0))
678 setup_out_buf[0] = endpoints[endpoint & 0xf].halt[endpoint >> 7];
679 setup_out_buf[1] = 0;
680 usb_send_status();
682 else
683 usb_request_error();
686 static void usb_setup_get_interface(int interface)
688 if (usb_state != STATE_CONFIGURED || interface != 0)
689 usb_request_error();
690 else
692 setup_out_buf[0] = 0;
693 usb_data_stage_enable(DIR_TX);
694 usb_send_block_and_status(setup_out_buf, 1, 0);
698 static void usb_setup_set_interface(int interface, int setting)
700 if (usb_state != STATE_CONFIGURED || interface != 0 || setting != 0)
701 usb_request_error();
702 else
704 usb_setup_interface();
705 usb_status_ack(DIR_TX);
709 static void usb_handle_setup_pkt(unsigned char *pkt)
711 switch ((pkt[0] << 8) | pkt[1])
713 case 0x0005:
714 log_char('A');
715 usb_setup_set_address(pkt[2]);
716 break;
717 case 0x8006:
718 log_char('D');
719 usb_setup_get_descriptor(pkt[3], pkt[2], (pkt[5] << 8) | pkt[4],
720 (pkt[7] << 8) | pkt[6]);
721 break;
722 case 0x8008:
723 usb_setup_get_configuration();
724 break;
725 case 0x0009:
726 usb_setup_set_configuration(pkt[2]);
727 break;
728 case 0x8000:
729 usb_setup_get_device_status();
730 break;
731 case 0x8100:
732 usb_setup_get_interface_status(pkt[4]);
733 break;
734 case 0x8200:
735 usb_setup_get_endpoint_status(pkt[4]);
736 break;
737 case 0x0001:
738 usb_setup_clear_device_feature(pkt[2]);
739 break;
740 case 0x0201:
741 usb_setup_clear_endpoint_feature(pkt[4], pkt[2]);
742 break;
743 case 0x0003:
744 usb_setup_set_device_feature(pkt[2]);
745 break;
746 case 0x0203:
747 usb_setup_set_endpoint_feature(pkt[4], pkt[2]);
748 break;
749 case 0x810a:
750 usb_setup_get_interface(pkt[4]);
751 break;
752 case 0x010b:
753 usb_setup_set_interface(pkt[4], pkt[2]);
754 break;
755 case 0x0103:
756 /* set interface feature */
757 case 0x0101:
758 /* clear interface feature */
759 case 0x0007:
760 /* set descriptor */
761 case 0x820c:
762 /* synch frame */
763 default:
764 usb_request_error();
768 static void usb_handle_setup_rx(void)
770 int len;
771 #ifdef LCD_DEBUG
772 char s[20];
773 int i;
774 #endif
775 usb_select_setup_endpoint();
776 len = usb_get_packet(setup_pkt_buf, 8);
778 if (len == 8)
779 usb_handle_setup_pkt(setup_pkt_buf);
781 #ifdef LCD_DEBUG
783 snprintf(s, 10, "l%02x", len);
784 lcd_puts(0, 5, s);
786 for (i = 0; i < 8; i++)
787 snprintf(s + i * 2, 3, "%02x", setup_pkt_buf[i]);
788 lcd_puts(0, 0, s);
789 lcd_update();
790 #endif
793 static void usb_handle_data_int(int ep, int dir)
795 if (dir == DIR_TX)
796 usb_send(ep);
797 else
799 endpoints[ep].in_ack = 1;
800 usb_receive(ep);
804 static void usb_handle_int(int i)
806 #ifdef LCD_DEBUG
808 char s[10];
809 snprintf(s, sizeof(s), "%02d", i);
810 lcd_puts(0, 2, s);
811 lcd_update();
813 int_count[i]++;
814 if (i == 10)
815 log_char('o');
816 if (i == 11)
817 log_char('i');
818 if (i == 12)
819 log_char('O');
820 if (i == 13)
821 log_char('I');
822 #endif
824 if (i >= 10)
825 usb_handle_data_int((i - 10) / 2, i % 2);
826 else
828 switch (i)
830 case 0:
831 log_char('r');
832 usb_setup(1);
833 break;
834 case 8:
835 log_char('s');
836 usb_handle_setup_rx();
837 break;
843 void usb_handle_interrupts(void)
845 #ifdef LCD_DEBUG
846 char s[20];
847 #endif
849 while (1)
851 unsigned long ints;
852 int i;
854 #ifdef LCD_DEBUG
856 snprintf(s, sizeof(s), "i%08lx", ISP1582_INT);
857 lcd_puts(0, 2, s);
859 #endif
861 ints = ISP1582_INT & ISP1582_INTEN;
862 if (!ints) break;
864 i = 0;
865 while (!(ints & (1 << i)))
866 i++;
867 ISP1582_INT = 1 << i;
868 usb_handle_int(i);
870 #ifdef LCD_DEBUG
871 for (i = 0; i < 8; i++)
872 snprintf(s + i * 2, 3, "%02x", int_count[i]);
873 lcd_puts(0, 6, s);
874 for (i = 0; i < 8; i++)
875 snprintf(s + i * 2, 3, "%02x", int_count[i + 8]);
876 lcd_puts(0, 7, s);
877 #endif
879 #ifdef LCD_DEBUG
881 lcd_puts(0, 3, usb_connected() ? "C" : "N");
882 lcd_update();
884 #endif
887 static inline int fifo_mod(int n)
889 return (n >= SERIAL_BUF_SIZE) ? n - SERIAL_BUF_SIZE : n;
892 static void fifo_init(struct serial_fifo *fifo)
894 fifo->head = fifo->tail = 0;
897 static int fifo_empty(struct serial_fifo *fifo)
899 return fifo->head == fifo->tail;
902 static int fifo_full(struct serial_fifo *fifo)
904 return fifo_mod(fifo->head + 1) == fifo->tail;
907 static void fifo_remove(struct serial_fifo *fifo, int n)
909 fifo->tail = fifo_mod(fifo->tail + n);
913 Not used:
914 static void fifo_add(struct serial_fifo *fifo, int n)
916 fifo->head = fifo_mod(fifo->head + n);
919 static void fifo_free_block(struct serial_fifo *fifo,
920 unsigned char **ptr, int *len)
922 *ptr = fifo->buf + fifo->head;
923 if (fifo->head >= fifo->tail)
925 int l = SERIAL_BUF_SIZE - fifo->head;
926 if (fifo->tail == 0)
927 l--;
928 *len = l;
930 else
931 *len = fifo->tail - fifo->head - 1;
935 static int fifo_free_space(struct serial_fifo *fifo)
937 if (fifo->head >= fifo->tail)
938 return SERIAL_BUF_SIZE - (fifo->head - fifo->tail) - 1;
939 else
940 return fifo->tail - fifo->head - 1;
943 static int fifo_get_byte(struct serial_fifo *fifo)
945 int r = fifo->buf[fifo->tail];
946 fifo->tail = fifo_mod(fifo->tail + 1);
947 return r;
950 static void fifo_put_byte(struct serial_fifo *fifo, int b)
952 fifo->buf[fifo->head] = b;
953 fifo->head = fifo_mod(fifo->head + 1);
956 static void fifo_full_block(struct serial_fifo *fifo,
957 unsigned char **ptr, int *len)
959 *ptr = fifo->buf + fifo->tail;
960 if (fifo->head >= fifo->tail)
961 *len = fifo->head - fifo->tail;
962 else
963 *len = SERIAL_BUF_SIZE - fifo->tail;
966 static void serial_fill_in_fifo(int ep, unsigned char *buf, int len);
967 static void serial_free_out_fifo(int ep, unsigned char *buf, int len);
969 static void serial_restart_input(int ep)
971 if (fifo_free_space(&serial_in_fifo) >= 64)
972 usb_receive_block(serial_in_pkt, 1, 64, serial_fill_in_fifo, ep);
975 static void serial_fill_in_fifo(int ep, unsigned char *buf, int len)
977 int i;
978 for (i = 0; i < len; i++)
979 fifo_put_byte(&serial_in_fifo, buf[i]);
980 serial_restart_input(ep);
983 static void serial_restart_output(int ep)
985 unsigned char *block;
986 int blen;
987 fifo_full_block(&serial_out_fifo, &block, &blen);
988 if (blen)
990 #ifdef LCD_DEBUG
991 lcd_putsf(0, 2, "o%03lx/%03x", block - serial_out_fifo.buf, blen);
992 lcd_update();
993 #endif
994 usb_send_block(block, blen, serial_free_out_fifo, ep);
998 static void serial_free_out_fifo(int ep, unsigned char *buf, int len)
1000 (void)buf;
1001 fifo_remove(&serial_out_fifo, len);
1002 serial_restart_output(ep);
1005 void usb_serial_handle(void)
1007 #ifdef BUTTONS
1008 static int t = 0;
1010 t++;
1011 if (t >= 1000)
1013 int b;
1014 t = 0;
1015 yield();
1016 b = button_get(false);
1017 if (b == BUTTON_PLAY)
1018 system_reboot();
1019 else if (b & BUTTON_REL)
1020 usb_reconnect();
1022 #endif
1025 if (!usb_connect_state)
1027 if (usb_connected())
1029 int i;
1030 GPIO3_SET = 4;
1031 (*(volatile unsigned long *)0x80005004) = 2;
1032 (*(volatile unsigned long *)0x80005008) = 0;
1033 for (i = 0; i < 100000; i++)
1034 nop_f();
1035 usb_setup(0);
1036 usb_connect_state = 1;
1039 else
1041 if (!usb_connected())
1043 usb_connect_state = 0;
1044 usb_cleanup();
1046 else
1048 usb_handle_interrupts();
1050 if (usb_state == STATE_CONFIGURED)
1052 if (endpoints[1].in_min_len < 0)
1053 serial_restart_input(1);
1054 if (!endpoints[1].out_in_progress)
1055 serial_restart_output(1);
1063 Not used:
1064 static int usb_serial_in_empty(void)
1066 return fifo_empty(&serial_in_fifo);
1070 int usb_serial_get_byte(void)
1072 while (fifo_empty(&serial_in_fifo))
1073 usb_serial_handle();
1074 return fifo_get_byte(&serial_in_fifo);
1077 int usb_serial_try_get_byte(void)
1079 int r;
1080 if (fifo_empty(&serial_in_fifo))
1081 r = -1;
1082 else
1083 r = fifo_get_byte(&serial_in_fifo);
1084 usb_serial_handle();
1085 return r;
1089 Not used:
1090 static int usb_serial_out_full(void)
1092 return fifo_full(&serial_out_fifo);
1096 void usb_serial_put_byte(int b)
1098 while (fifo_full(&serial_out_fifo))
1099 usb_serial_handle();
1100 fifo_put_byte(&serial_out_fifo, b);
1101 usb_serial_handle();
1104 int usb_serial_try_put_byte(int b)
1106 int r = -1;
1107 if (!fifo_full(&serial_out_fifo))
1109 fifo_put_byte(&serial_out_fifo, b);
1110 r = 0;
1112 usb_serial_handle();
1113 return r;
1116 void usb_serial_init(void)
1118 fifo_init(&serial_in_fifo);
1119 fifo_init(&serial_out_fifo);
1120 usb_connect_state = 0;