Move CACHEALIGN_BITS to cpu headers
[kugel-rb.git] / firmware / ifp_usb_serial.c
blob06b286ab850f64c249a40064f41f483326f4a84b
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 TYPE_BULK 2
68 #define STATE_DEFAULT 0
69 #define STATE_ADDRESS 1
70 #define STATE_CONFIGURED 2
72 #define N_ENDPOINTS 2
74 struct usb_endpoint
76 unsigned char *out_buf;
77 short out_len;
78 short out_ptr;
79 void (*out_done)(int, unsigned char *, int);
80 unsigned char out_in_progress;
82 unsigned char *in_buf;
83 short in_min_len;
84 short in_max_len;
85 short in_ptr;
86 void (*in_done)(int, unsigned char *, int);
87 unsigned char in_ack;
89 unsigned char halt[2];
90 unsigned char enabled[2];
91 short max_pkt_size[2];
94 static char usb_connect_state;
96 static struct usb_endpoint endpoints[N_ENDPOINTS];
98 static unsigned char setup_pkt_buf[8];
99 static unsigned char setup_out_buf[8];
100 static unsigned char usb_state;
101 static unsigned char usb_remote_wakeup;
103 #ifdef LCD_DEBUG
104 static unsigned char int_count[32];
106 static int log_pos_x = 0;
107 static int log_pos_y = 3;
108 #endif
110 static void nop_f(void)
114 #ifdef LCD_DEBUG
115 static void log_char(char c)
117 char s[2];
119 s[0] = c;
120 s[1] = 0;
122 lcd_puts(log_pos_x, log_pos_y, s);
123 lcd_update();
124 log_pos_x++;
125 if (log_pos_x >= 16)
127 log_pos_x = 0;
128 log_pos_y++;
129 if (log_pos_y > 5)
130 log_pos_y = 3;
133 #else
134 #define log_char(c)
135 #endif
137 #define SERIAL_BUF_SIZE 1024
139 struct serial_fifo
141 unsigned char buf[SERIAL_BUF_SIZE];
142 short head, tail;
145 static struct serial_fifo serial_in_fifo;
146 static struct serial_fifo serial_out_fifo;
147 static unsigned char serial_in_pkt[64];
149 static unsigned char device_descriptor[18] = {
150 0x12, /* length */
151 0x01, /* descriptor type */
152 0x10, 0x01, /* USB version (1.1) */
153 0xff, 0xff, /* class and subclass */
154 0xff, /* protocol */
155 0x40, /* max packet size 0 */
156 0x02, 0x41, /* vendor (iRiver) */
157 0x07, 0xee, /* product (0xee07) */
158 0x01, 0x00, /* device version */
159 0x01, /* manufacturer string */
160 0x02, /* product string */
161 0x00, /* serial number string */
162 0x01 /* number of configurations */
165 static unsigned char cfg_descriptor[32] = {
166 0x09, /* length */
167 0x02, /* descriptor type */
168 0x20, 0x00, /* total length */
169 0x01, /* number of interfaces */
170 0x01, /* configuration value */
171 0x00, /* configuration string */
172 0x80, /* attributes (none) */
173 0x32, /* max power (100 mA) */
174 /* interface descriptor */
175 0x09, /* length */
176 0x04, /* descriptor type */
177 0x00, /* interface number */
178 0x00, /* alternate setting */
179 0x02, /* number of endpoints */
180 0xff, /* interface class */
181 0xff, /* interface subclass */
182 0xff, /* interface protocol */
183 0x00, /* interface string */
184 /* endpoint IN */
185 0x07, /* length */
186 0x05, /* descriptor type */
187 0x81, /* endpoint 1 IN */
188 0x02, /* attributes (bulk) */
189 0x40, 0x00, /* max packet size */
190 0x00, /* interval */
191 /* endpoint OUT */
192 0x07, /* length */
193 0x05, /* descriptor type */
194 0x01, /* endpoint 1 IN */
195 0x02, /* attributes (bulk) */
196 0x40, 0x00, /* max packet size */
197 0x00 /* interval */
200 static unsigned char lang_descriptor[4] = {
201 0x04, /* length */
202 0x03, /* descriptor type */
203 0x09, 0x04 /* English (US) */
206 #define N_STRING_DESCRIPTORS 2
208 static unsigned char string_descriptor_vendor[] = {
209 0x2e, 0x03,
210 'i', 0, 'R', 0, 'i', 0, 'v', 0, 'e', 0, 'r', 0, ' ', 0, 'L', 0,
211 't', 0, 'd', 0, ' ', 0, 'a', 0, 'n', 0, 'd', 0, ' ', 0, 'R', 0,
212 'o', 0, 'c', 0, 'k', 0, 'b', 0, 'o', 0, 'x', 0};
214 static unsigned char string_descriptor_product[] = {
215 0x1c, 0x03,
216 'i', 0, 'R', 0, 'i', 0, 'v', 0, 'e', 0, 'r', 0, ' ', 0, 'i', 0,
217 'F', 0, 'P', 0, '7', 0, '0', 0, '0', 0};
219 static unsigned char *string_descriptor[N_STRING_DESCRIPTORS] = {
220 string_descriptor_vendor,
221 string_descriptor_product
224 static inline int ep_index(int n, int dir)
226 return (n << 1) | dir;
229 static inline int epidx_dir(int idx)
231 return idx & 1;
234 static inline int epidx_n(int idx)
236 return idx >> 1;
239 int usb_connected(void)
241 return GPIO7_READ & 1;
244 static inline void usb_select_endpoint(int idx)
246 ISP1582_EPINDEX = idx;
249 static inline void usb_select_setup_endpoint(void)
251 ISP1582_EPINDEX = 0x20;
254 void usb_setup_endpoint(int idx, int max_pkt_size, int type)
256 struct usb_endpoint *ep;
258 usb_select_endpoint(idx);
259 ISP1582_MAXPKSZ = max_pkt_size;
260 /* |= is in the original firmware */
261 ISP1582_EPTYPE |= 0x1c | type;
262 /* clear buffer */
263 ISP1582_CTRLFUN |= 0x10;
264 ISP1582_INTEN |= (1 << (10 + idx));
266 ep = &(endpoints[epidx_n(idx)]);
267 ep->halt[epidx_dir(idx)] = 0;
268 ep->enabled[epidx_dir(idx)] = 1;
269 ep->out_in_progress = 0;
270 ep->in_min_len = -1;
271 ep->in_ack = 0;
272 ep->max_pkt_size[epidx_dir(idx)] = max_pkt_size;
275 void usb_disable_endpoint(int idx)
277 usb_select_endpoint(idx);
278 ISP1582_EPTYPE &= 8;
279 ISP1582_INTEN &= ~(1 << (10 + idx));
280 endpoints[epidx_n(idx)].enabled[epidx_dir(idx)] = 1;
283 void usb_reconnect(void)
285 int i;
286 ISP1582_MODE &= ~1; /* SOFTCT off */
287 for (i = 0; i < 10000; i++)
288 nop_f();
289 ISP1582_MODE |= 1; /* SOFTCT on */
292 void usb_cleanup(void)
294 ISP1582_MODE &= ~1; /* SOFTCT off */
297 void usb_setup(int reset)
299 int i;
301 for (i = 0; i < N_ENDPOINTS; i++)
302 endpoints[i].enabled[0] = endpoints[i].enabled[1] = 0;
304 ISP1582_UNLOCK = ISP1582_UNLOCK_CODE;
305 if (!reset)
306 ISP1582_MODE = 0x88; /* CLKAON | GLINTENA */
307 ISP1582_INTCONF = 0x57;
308 ISP1582_INTEN = 0xd39;
310 ISP1582_ADDRESS = reset ? 0x80: 0;
312 usb_setup_endpoint(ep_index(0, DIR_RX), 64, 0);
313 usb_setup_endpoint(ep_index(0, DIR_TX), 64, 0);
315 ISP1582_MODE |= 1; /* SOFTCT on */
317 usb_state = STATE_DEFAULT;
318 usb_remote_wakeup = 0;
321 static int usb_get_packet(unsigned char *buf, int max_len)
323 int len, i;
324 len = ISP1582_BUFLEN;
326 if (max_len < 0 || max_len > len)
327 max_len = len;
329 i = 0;
330 while (i < len)
332 unsigned short d = ISP1582_DATA;
333 if (i < max_len)
334 buf[i] = d & 0xff;
335 i++;
336 if (i < max_len)
337 buf[i] = (d >> 8) & 0xff;
338 i++;
340 return max_len;
343 static void usb_receive(int n)
345 int len;
347 if (endpoints[n].halt[DIR_RX]
348 || !endpoints[n].enabled[DIR_RX]
349 || endpoints[n].in_min_len < 0
350 || !endpoints[n].in_ack)
351 return;
353 endpoints[n].in_ack = 0;
355 usb_select_endpoint(ep_index(n, DIR_RX));
357 len = usb_get_packet(endpoints[n].in_buf + endpoints[n].in_ptr,
358 endpoints[n].in_max_len - endpoints[n].in_ptr);
359 endpoints[n].in_ptr += len;
360 if (endpoints[n].in_ptr >= endpoints[n].in_min_len) {
361 endpoints[n].in_min_len = -1;
362 if (endpoints[n].in_done)
363 (*(endpoints[n].in_done))(n, endpoints[n].in_buf,
364 endpoints[n].in_ptr);
368 static int usb_out_buffer_full(int ep)
370 usb_select_endpoint(ep_index(ep, DIR_TX));
371 if (ISP1582_EPTYPE & 4)
372 return (ISP1582_BUFSTAT & 3) == 3;
373 else
374 return (ISP1582_BUFSTAT & 3) != 0;
377 static void usb_send(int n)
379 int max_pkt_size, len;
380 int i;
381 unsigned char *p;
383 #ifdef LCD_DEBUG
384 if (endpoints[n].halt[DIR_TX])
385 log_char('H');
386 if (!endpoints[n].out_in_progress)
387 log_char('$');
388 #endif
390 if (endpoints[n].halt[DIR_TX]
391 || !endpoints[n].enabled[DIR_TX]
392 || !endpoints[n].out_in_progress)
393 return;
395 if (endpoints[n].out_ptr < 0)
397 endpoints[n].out_in_progress = 0;
398 if (endpoints[n].out_done)
399 (*(endpoints[n].out_done))(n, endpoints[n].out_buf,
400 endpoints[n].out_len);
401 return;
404 if (usb_out_buffer_full(n))
406 log_char('F');
407 return;
410 usb_select_endpoint(ep_index(n, DIR_TX));
411 max_pkt_size = endpoints[n].max_pkt_size[DIR_TX];
412 len = endpoints[n].out_len - endpoints[n].out_ptr;
413 if (len > max_pkt_size)
414 len = max_pkt_size;
416 log_char('0' + (len % 10));
417 ISP1582_BUFLEN = len;
418 p = endpoints[n].out_buf + endpoints[n].out_ptr;
419 i = 0;
420 while (len - i >= 2) {
421 ISP1582_DATA = p[i] | (p[i + 1] << 8);
422 i += 2;
424 if (i < len)
425 ISP1582_DATA = p[i];
427 endpoints[n].out_ptr += len;
430 if (endpoints[n].out_ptr == endpoints[n].out_len
431 && len < max_pkt_size)
433 if (endpoints[n].out_ptr == endpoints[n].out_len)
434 endpoints[n].out_ptr = -1;
437 static void usb_stall_endpoint(int idx)
439 usb_select_endpoint(idx);
440 ISP1582_CTRLFUN |= 1;
441 endpoints[epidx_n(idx)].halt[epidx_dir(idx)] = 1;
444 static void usb_unstall_endpoint(int idx)
446 usb_select_endpoint(idx);
447 ISP1582_CTRLFUN &= ~1;
448 ISP1582_EPTYPE &= ~8;
449 ISP1582_EPTYPE |= 8;
450 ISP1582_CTRLFUN |= 0x10;
451 if (epidx_dir(idx) == DIR_TX)
452 endpoints[epidx_n(idx)].out_in_progress = 0;
453 else
455 endpoints[epidx_n(idx)].in_min_len = -1;
456 endpoints[epidx_n(idx)].in_ack = 0;
458 endpoints[epidx_n(idx)].halt[epidx_dir(idx)] = 0;
461 static void usb_status_ack(int dir)
463 log_char(dir ? '@' : '#');
464 usb_select_endpoint(ep_index(0, dir));
465 ISP1582_CTRLFUN |= 2;
468 static void usb_set_address(int adr)
470 ISP1582_ADDRESS = adr | 0x80;
473 static void usb_data_stage_enable(int dir)
475 usb_select_endpoint(ep_index(0, dir));
476 ISP1582_CTRLFUN |= 4;
479 static void usb_request_error(void)
481 usb_stall_endpoint(ep_index(0, DIR_TX));
482 usb_stall_endpoint(ep_index(0, DIR_RX));
485 static void usb_receive_block(unsigned char *buf, int min_len,
486 int max_len,
487 void (*in_done)(int, unsigned char *, int),
488 int ep)
490 endpoints[ep].in_done = in_done;
491 endpoints[ep].in_buf = buf;
492 endpoints[ep].in_max_len = max_len;
493 endpoints[ep].in_min_len = min_len;
494 endpoints[ep].in_ptr = 0;
495 usb_receive(ep);
498 static void usb_send_block(unsigned char *buf, int len,
499 void (*done)(int, unsigned char *, int),
500 int ep)
502 endpoints[ep].out_done = done;
503 endpoints[ep].out_buf = buf;
504 endpoints[ep].out_len = len;
505 endpoints[ep].out_ptr = 0;
506 endpoints[ep].out_in_progress = 1;
507 usb_send(ep);
510 static void out_send_status(int n, unsigned char *buf, int len)
512 (void)n;
513 (void)buf;
514 (void)len;
515 usb_status_ack(DIR_RX);
518 static void usb_send_block_and_status(unsigned char *buf, int len, int ep)
520 usb_send_block(buf, len, out_send_status, ep);
523 static void usb_setup_set_address(int adr)
525 usb_set_address(adr);
526 usb_state = adr ? STATE_ADDRESS : STATE_DEFAULT;
527 usb_status_ack(DIR_TX);
530 static void usb_send_descriptor(unsigned char *device_descriptor,
531 int descriptor_len, int buffer_len)
533 int len = descriptor_len < buffer_len ? descriptor_len : buffer_len;
534 usb_send_block_and_status(device_descriptor, len, 0);
537 static void usb_setup_get_descriptor(int type, int index, int lang, int len)
539 (void)lang;
540 usb_data_stage_enable(DIR_TX);
541 switch (type)
543 case 1:
544 if (index == 0)
545 usb_send_descriptor(device_descriptor,
546 sizeof(device_descriptor), len);
547 else
548 usb_request_error();
549 break;
550 case 2:
551 if (index == 0)
552 usb_send_descriptor(cfg_descriptor,
553 sizeof(cfg_descriptor), len);
554 else
555 usb_request_error();
556 break;
557 case 3:
558 if (index == 0)
559 usb_send_descriptor(lang_descriptor,
560 sizeof(lang_descriptor), len);
561 else if (index <= N_STRING_DESCRIPTORS)
562 usb_send_descriptor(string_descriptor[index - 1],
563 string_descriptor[index - 1][0],
564 len);
565 else
566 usb_request_error();
567 break;
568 default:
569 usb_request_error();
573 static void usb_setup_get_configuration(void)
575 setup_out_buf[0] = (usb_state == STATE_CONFIGURED) ? 1 : 0;
576 usb_data_stage_enable(DIR_TX);
577 usb_send_block_and_status(setup_out_buf, 1, 0);
580 static void usb_setup_interface(void)
582 usb_setup_endpoint(ep_index(1, DIR_RX), 64, TYPE_BULK);
583 usb_setup_endpoint(ep_index(1, DIR_TX), 64, TYPE_BULK);
586 static void usb_setup_set_configuration(int value)
588 switch (value)
590 case 0:
591 usb_disable_endpoint(ep_index(1, DIR_RX));
592 usb_disable_endpoint(ep_index(1, DIR_TX));
593 usb_state = STATE_ADDRESS;
594 usb_status_ack(DIR_TX);
595 break;
596 case 1:
597 usb_setup_interface();
598 usb_state = STATE_CONFIGURED;
599 usb_status_ack(DIR_TX);
600 break;
601 default:
602 usb_request_error();
606 static void usb_send_status(void)
608 usb_data_stage_enable(DIR_TX);
609 usb_send_block_and_status(setup_out_buf, 2, 0);
612 static void usb_setup_get_device_status(void)
614 setup_out_buf[0] = (usb_remote_wakeup != 0) ? 2 : 0;
615 setup_out_buf[1] = 0;
616 usb_send_status();
619 static void usb_setup_clear_device_feature(int feature)
621 if (feature == 1) {
622 usb_remote_wakeup = 0;
623 usb_status_ack(DIR_TX);
624 } else
625 usb_request_error();
628 static void usb_setup_set_device_feature(int feature)
630 if (feature == 1) {
631 usb_remote_wakeup = 1;
632 usb_status_ack(DIR_TX);
633 } else
634 usb_request_error();
637 static void usb_setup_clear_endpoint_feature(int endpoint, int feature)
639 if (usb_state != STATE_CONFIGURED || feature != 0)
640 usb_request_error();
641 else if ((endpoint & 0xf) == 1)
643 usb_unstall_endpoint(ep_index(endpoint & 0xf, endpoint >> 7));
644 usb_status_ack(DIR_TX);
646 else
647 usb_request_error();
650 static void usb_setup_set_endpoint_feature(int endpoint, int feature)
652 if (usb_state != STATE_CONFIGURED || feature != 0)
653 usb_request_error();
654 else if ((endpoint & 0xf) == 1)
656 usb_stall_endpoint(ep_index(endpoint & 0xf, endpoint >> 7));
657 usb_status_ack(DIR_TX);
659 else
660 usb_request_error();
663 static void usb_setup_get_interface_status(int interface)
665 if (usb_state != STATE_CONFIGURED || interface != 0)
666 usb_request_error();
667 else
669 setup_out_buf[0] = setup_out_buf[1] = 0;
670 usb_send_status();
674 static void usb_setup_get_endpoint_status(int endpoint)
676 if ((usb_state == STATE_CONFIGURED && (endpoint & 0xf) <= 1)
677 || (usb_state == STATE_ADDRESS && (endpoint & 0xf) == 0))
679 setup_out_buf[0] = endpoints[endpoint & 0xf].halt[endpoint >> 7];
680 setup_out_buf[1] = 0;
681 usb_send_status();
683 else
684 usb_request_error();
687 static void usb_setup_get_interface(int interface)
689 if (usb_state != STATE_CONFIGURED || interface != 0)
690 usb_request_error();
691 else
693 setup_out_buf[0] = 0;
694 usb_data_stage_enable(DIR_TX);
695 usb_send_block_and_status(setup_out_buf, 1, 0);
699 static void usb_setup_set_interface(int interface, int setting)
701 if (usb_state != STATE_CONFIGURED || interface != 0 || setting != 0)
702 usb_request_error();
703 else
705 usb_setup_interface();
706 usb_status_ack(DIR_TX);
710 static void usb_handle_setup_pkt(unsigned char *pkt)
712 switch ((pkt[0] << 8) | pkt[1])
714 case 0x0005:
715 log_char('A');
716 usb_setup_set_address(pkt[2]);
717 break;
718 case 0x8006:
719 log_char('D');
720 usb_setup_get_descriptor(pkt[3], pkt[2], (pkt[5] << 8) | pkt[4],
721 (pkt[7] << 8) | pkt[6]);
722 break;
723 case 0x8008:
724 usb_setup_get_configuration();
725 break;
726 case 0x0009:
727 usb_setup_set_configuration(pkt[2]);
728 break;
729 case 0x8000:
730 usb_setup_get_device_status();
731 break;
732 case 0x8100:
733 usb_setup_get_interface_status(pkt[4]);
734 break;
735 case 0x8200:
736 usb_setup_get_endpoint_status(pkt[4]);
737 break;
738 case 0x0001:
739 usb_setup_clear_device_feature(pkt[2]);
740 break;
741 case 0x0201:
742 usb_setup_clear_endpoint_feature(pkt[4], pkt[2]);
743 break;
744 case 0x0003:
745 usb_setup_set_device_feature(pkt[2]);
746 break;
747 case 0x0203:
748 usb_setup_set_endpoint_feature(pkt[4], pkt[2]);
749 break;
750 case 0x810a:
751 usb_setup_get_interface(pkt[4]);
752 break;
753 case 0x010b:
754 usb_setup_set_interface(pkt[4], pkt[2]);
755 break;
756 case 0x0103:
757 /* set interface feature */
758 case 0x0101:
759 /* clear interface feature */
760 case 0x0007:
761 /* set descriptor */
762 case 0x820c:
763 /* synch frame */
764 default:
765 usb_request_error();
769 static void usb_handle_setup_rx(void)
771 int len;
772 #ifdef LCD_DEBUG
773 char s[20];
774 int i;
775 #endif
776 usb_select_setup_endpoint();
777 len = usb_get_packet(setup_pkt_buf, 8);
779 if (len == 8)
780 usb_handle_setup_pkt(setup_pkt_buf);
782 #ifdef LCD_DEBUG
784 snprintf(s, 10, "l%02x", len);
785 lcd_puts(0, 5, s);
787 for (i = 0; i < 8; i++)
788 snprintf(s + i * 2, 3, "%02x", setup_pkt_buf[i]);
789 lcd_puts(0, 0, s);
790 lcd_update();
791 #endif
794 static void usb_handle_data_int(int ep, int dir)
796 if (dir == DIR_TX)
797 usb_send(ep);
798 else
800 endpoints[ep].in_ack = 1;
801 usb_receive(ep);
805 static void usb_handle_int(int i)
807 #ifdef LCD_DEBUG
809 char s[10];
810 snprintf(s, sizeof(s), "%02d", i);
811 lcd_puts(0, 2, s);
812 lcd_update();
814 int_count[i]++;
815 if (i == 10)
816 log_char('o');
817 if (i == 11)
818 log_char('i');
819 if (i == 12)
820 log_char('O');
821 if (i == 13)
822 log_char('I');
823 #endif
825 if (i >= 10)
826 usb_handle_data_int((i - 10) / 2, i % 2);
827 else
829 switch (i)
831 case 0:
832 log_char('r');
833 usb_setup(1);
834 break;
835 case 8:
836 log_char('s');
837 usb_handle_setup_rx();
838 break;
844 void usb_handle_interrupts(void)
846 #ifdef LCD_DEBUG
847 char s[20];
848 #endif
850 while (1)
852 unsigned long ints;
853 int i;
855 #ifdef LCD_DEBUG
857 snprintf(s, sizeof(s), "i%08lx", ISP1582_INT);
858 lcd_puts(0, 2, s);
860 #endif
862 ints = ISP1582_INT & ISP1582_INTEN;
863 if (!ints) break;
865 i = 0;
866 while (!(ints & (1 << i)))
867 i++;
868 ISP1582_INT = 1 << i;
869 usb_handle_int(i);
871 #ifdef LCD_DEBUG
872 for (i = 0; i < 8; i++)
873 snprintf(s + i * 2, 3, "%02x", int_count[i]);
874 lcd_puts(0, 6, s);
875 for (i = 0; i < 8; i++)
876 snprintf(s + i * 2, 3, "%02x", int_count[i + 8]);
877 lcd_puts(0, 7, s);
878 #endif
880 #ifdef LCD_DEBUG
882 lcd_puts(0, 3, usb_connected() ? "C" : "N");
883 lcd_update();
885 #endif
888 static inline int fifo_mod(int n)
890 return (n >= SERIAL_BUF_SIZE) ? n - SERIAL_BUF_SIZE : n;
893 static void fifo_init(struct serial_fifo *fifo)
895 fifo->head = fifo->tail = 0;
898 static int fifo_empty(struct serial_fifo *fifo)
900 return fifo->head == fifo->tail;
903 static int fifo_full(struct serial_fifo *fifo)
905 return fifo_mod(fifo->head + 1) == fifo->tail;
908 static void fifo_remove(struct serial_fifo *fifo, int n)
910 fifo->tail = fifo_mod(fifo->tail + n);
914 Not used:
915 static void fifo_add(struct serial_fifo *fifo, int n)
917 fifo->head = fifo_mod(fifo->head + n);
920 static void fifo_free_block(struct serial_fifo *fifo,
921 unsigned char **ptr, int *len)
923 *ptr = fifo->buf + fifo->head;
924 if (fifo->head >= fifo->tail)
926 int l = SERIAL_BUF_SIZE - fifo->head;
927 if (fifo->tail == 0)
928 l--;
929 *len = l;
931 else
932 *len = fifo->tail - fifo->head - 1;
936 static int fifo_free_space(struct serial_fifo *fifo)
938 if (fifo->head >= fifo->tail)
939 return SERIAL_BUF_SIZE - (fifo->head - fifo->tail) - 1;
940 else
941 return fifo->tail - fifo->head - 1;
944 static int fifo_get_byte(struct serial_fifo *fifo)
946 int r = fifo->buf[fifo->tail];
947 fifo->tail = fifo_mod(fifo->tail + 1);
948 return r;
951 static void fifo_put_byte(struct serial_fifo *fifo, int b)
953 fifo->buf[fifo->head] = b;
954 fifo->head = fifo_mod(fifo->head + 1);
957 static void fifo_full_block(struct serial_fifo *fifo,
958 unsigned char **ptr, int *len)
960 *ptr = fifo->buf + fifo->tail;
961 if (fifo->head >= fifo->tail)
962 *len = fifo->head - fifo->tail;
963 else
964 *len = SERIAL_BUF_SIZE - fifo->tail;
967 static void serial_fill_in_fifo(int ep, unsigned char *buf, int len);
968 static void serial_free_out_fifo(int ep, unsigned char *buf, int len);
970 static void serial_restart_input(int ep)
972 if (fifo_free_space(&serial_in_fifo) >= 64)
973 usb_receive_block(serial_in_pkt, 1, 64, serial_fill_in_fifo, ep);
976 static void serial_fill_in_fifo(int ep, unsigned char *buf, int len)
978 int i;
979 for (i = 0; i < len; i++)
980 fifo_put_byte(&serial_in_fifo, buf[i]);
981 serial_restart_input(ep);
984 static void serial_restart_output(int ep)
986 unsigned char *block;
987 int blen;
988 fifo_full_block(&serial_out_fifo, &block, &blen);
989 if (blen)
991 #ifdef LCD_DEBUG
992 char s[20];
993 snprintf(s, sizeof(s), "o%03lx/%03x", block - serial_out_fifo.buf,
994 blen);
995 lcd_puts(0, 2, s);
996 lcd_update();
997 #endif
998 usb_send_block(block, blen, serial_free_out_fifo, ep);
1002 static void serial_free_out_fifo(int ep, unsigned char *buf, int len)
1004 (void)buf;
1005 fifo_remove(&serial_out_fifo, len);
1006 serial_restart_output(ep);
1009 void usb_serial_handle(void)
1011 #ifdef BUTTONS
1012 static int t = 0;
1014 t++;
1015 if (t >= 1000)
1017 int b;
1018 t = 0;
1019 yield();
1020 b = button_get(false);
1021 if (b == BUTTON_PLAY)
1022 system_reboot();
1023 else if (b & BUTTON_REL)
1024 usb_reconnect();
1026 #endif
1029 if (!usb_connect_state)
1031 if (usb_connected())
1033 int i;
1034 GPIO3_SET = 4;
1035 (*(volatile unsigned long *)0x80005004) = 2;
1036 (*(volatile unsigned long *)0x80005008) = 0;
1037 for (i = 0; i < 100000; i++)
1038 nop_f();
1039 usb_setup(0);
1040 usb_connect_state = 1;
1043 else
1045 if (!usb_connected())
1047 usb_connect_state = 0;
1048 usb_cleanup();
1050 else
1052 usb_handle_interrupts();
1054 if (usb_state == STATE_CONFIGURED)
1056 if (endpoints[1].in_min_len < 0)
1057 serial_restart_input(1);
1058 if (!endpoints[1].out_in_progress)
1059 serial_restart_output(1);
1067 Not used:
1068 static int usb_serial_in_empty(void)
1070 return fifo_empty(&serial_in_fifo);
1074 int usb_serial_get_byte(void)
1076 while (fifo_empty(&serial_in_fifo))
1077 usb_serial_handle();
1078 return fifo_get_byte(&serial_in_fifo);
1081 int usb_serial_try_get_byte(void)
1083 int r;
1084 if (fifo_empty(&serial_in_fifo))
1085 r = -1;
1086 else
1087 r = fifo_get_byte(&serial_in_fifo);
1088 usb_serial_handle();
1089 return r;
1093 Not used:
1094 static int usb_serial_out_full(void)
1096 return fifo_full(&serial_out_fifo);
1100 void usb_serial_put_byte(int b)
1102 while (fifo_full(&serial_out_fifo))
1103 usb_serial_handle();
1104 fifo_put_byte(&serial_out_fifo, b);
1105 usb_serial_handle();
1108 int usb_serial_try_put_byte(int b)
1110 int r = -1;
1111 if (!fifo_full(&serial_out_fifo))
1113 fifo_put_byte(&serial_out_fifo, b);
1114 r = 0;
1116 usb_serial_handle();
1117 return r;
1120 void usb_serial_init(void)
1122 fifo_init(&serial_in_fifo);
1123 fifo_init(&serial_out_fifo);
1124 usb_connect_state = 0;