summaryrefslogtreecommitdiff
path: root/firmware/ifp_usb_serial.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/ifp_usb_serial.c')
-rw-r--r--firmware/ifp_usb_serial.c1126
1 files changed, 1126 insertions, 0 deletions
diff --git a/firmware/ifp_usb_serial.c b/firmware/ifp_usb_serial.c
new file mode 100644
index 0000000000..635123baa9
--- /dev/null
+++ b/firmware/ifp_usb_serial.c
@@ -0,0 +1,1126 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006 by Tomasz Malesinski
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20/*
21#define LCD_DEBUG
22#define BUTTONS
23*/
24
25/* #include "config.h" */
26#include <stdlib.h>
27#include "pnx0101.h"
28#include "ifp_usb_serial.h"
29
30#ifdef BUTTONS
31#include "kernel.h"
32#include "button.h"
33#include "system.h"
34#endif
35
36#ifdef LCD_DEBUG
37#include "lcd.h"
38#include "sprintf.h"
39#endif
40
41
42#define ISP1582_BASE (0x24100000)
43#define ISP1582_ADDRESS (*(volatile unsigned char *)ISP1582_BASE)
44#define ISP1582_MODE (*(volatile unsigned short *)(ISP1582_BASE + 0xc))
45#define ISP1582_INTCONF (*(volatile unsigned char *)(ISP1582_BASE + 0x10))
46#define ISP1582_OTG (*(volatile unsigned char *)(ISP1582_BASE + 0x12))
47#define ISP1582_INTEN (*(volatile unsigned long *)(ISP1582_BASE + 0x14))
48
49#define ISP1582_EPINDEX (*(volatile unsigned char *)(ISP1582_BASE + 0x2c))
50#define ISP1582_CTRLFUN (*(volatile unsigned char *)(ISP1582_BASE + 0x28))
51#define ISP1582_DATA (*(volatile unsigned short *)(ISP1582_BASE + 0x20))
52#define ISP1582_BUFLEN (*(volatile unsigned short *)(ISP1582_BASE + 0x1c))
53#define ISP1582_BUFSTAT (*(volatile unsigned char *)(ISP1582_BASE + 0x1e))
54#define ISP1582_MAXPKSZ (*(volatile unsigned short *)(ISP1582_BASE + 0x04))
55#define ISP1582_EPTYPE (*(volatile unsigned short *)(ISP1582_BASE + 0x08))
56
57#define ISP1582_INT (*(volatile unsigned long *)(ISP1582_BASE + 0x18))
58#define ISP1582_CHIPID (*(volatile unsigned long *)(ISP1582_BASE + 0x70))
59#define ISP1582_FRAMENO (*(volatile unsigned short *)(ISP1582_BASE + 0x74))
60#define ISP1582_UNLOCK (*(volatile unsigned short *)(ISP1582_BASE + 0x7c))
61
62#define ISP1582_UNLOCK_CODE 0xaa37
63
64#define DIR_RX 0
65#define DIR_TX 1
66
67#define TYPE_BULK 2
68
69#define STATE_DEFAULT 0
70#define STATE_ADDRESS 1
71#define STATE_CONFIGURED 2
72
73#define N_ENDPOINTS 2
74
75struct usb_endpoint
76{
77 unsigned char *out_buf;
78 short out_len;
79 short out_ptr;
80 void (*out_done)(int, unsigned char *, int);
81 unsigned char out_in_progress;
82
83 unsigned char *in_buf;
84 short in_min_len;
85 short in_max_len;
86 short in_ptr;
87 void (*in_done)(int, unsigned char *, int);
88 unsigned char in_ack;
89
90 unsigned char halt[2];
91 unsigned char enabled[2];
92 short max_pkt_size[2];
93};
94
95static char usb_connect_state;
96
97static struct usb_endpoint endpoints[N_ENDPOINTS];
98
99static unsigned char setup_pkt_buf[8];
100static unsigned char setup_out_buf[8];
101static unsigned char usb_state;
102static unsigned char usb_remote_wakeup;
103
104#ifdef LCD_DEBUG
105static unsigned char int_count[32];
106
107static int log_pos_x = 0;
108static int log_pos_y = 3;
109#endif
110
111static void nop_f(void)
112{
113}
114
115#ifdef LCD_DEBUG
116static void log_char(char c)
117{
118 char s[2];
119
120 s[0] = c;
121 s[1] = 0;
122
123 lcd_puts(log_pos_x, log_pos_y, s);
124 lcd_update();
125 log_pos_x++;
126 if (log_pos_x >= 16)
127 {
128 log_pos_x = 0;
129 log_pos_y++;
130 if (log_pos_y > 5)
131 log_pos_y = 3;
132 }
133}
134#else
135#define log_char(c)
136#endif
137
138#define SERIAL_BUF_SIZE 1024
139
140struct serial_fifo
141{
142 unsigned char buf[SERIAL_BUF_SIZE];
143 short head, tail;
144};
145
146static struct serial_fifo serial_in_fifo;
147static struct serial_fifo serial_out_fifo;
148static unsigned char serial_in_pkt[64];
149
150static unsigned char device_descriptor[18] = {
151 0x12, /* length */
152 0x01, /* descriptor type */
153 0x10, 0x01, /* USB version (1.1) */
154 0xff, 0xff, /* class and subclass */
155 0xff, /* protocol */
156 0x40, /* max packet size 0 */
157 0x02, 0x41, /* vendor (iRiver) */
158 0x07, 0xee, /* product (0xee07) */
159 0x01, 0x00, /* device version */
160 0x01, /* manufacturer string */
161 0x02, /* product string */
162 0x00, /* serial number string */
163 0x01 /* number of configurations */
164};
165
166static unsigned char cfg_descriptor[32] = {
167 0x09, /* length */
168 0x02, /* descriptor type */
169 0x20, 0x00, /* total length */
170 0x01, /* number of interfaces */
171 0x01, /* configuration value */
172 0x00, /* configuration string */
173 0x80, /* attributes (none) */
174 0x32, /* max power (100 mA) */
175 /* interface descriptor */
176 0x09, /* length */
177 0x04, /* descriptor type */
178 0x00, /* interface number */
179 0x00, /* alternate setting */
180 0x02, /* number of endpoints */
181 0xff, /* interface class */
182 0xff, /* interface subclass */
183 0xff, /* interface protocol */
184 0x00, /* interface string */
185 /* endpoint IN */
186 0x07, /* length */
187 0x05, /* descriptor type */
188 0x81, /* endpoint 1 IN */
189 0x02, /* attributes (bulk) */
190 0x40, 0x00, /* max packet size */
191 0x00, /* interval */
192 /* endpoint OUT */
193 0x07, /* length */
194 0x05, /* descriptor type */
195 0x01, /* endpoint 1 IN */
196 0x02, /* attributes (bulk) */
197 0x40, 0x00, /* max packet size */
198 0x00 /* interval */
199};
200
201static unsigned char lang_descriptor[4] = {
202 0x04, /* length */
203 0x03, /* descriptor type */
204 0x09, 0x04 /* English (US) */
205};
206
207#define N_STRING_DESCRIPTORS 2
208
209static unsigned char string_descriptor_vendor[] = {
210 0x2e, 0x03,
211 'i', 0, 'R', 0, 'i', 0, 'v', 0, 'e', 0, 'r', 0, ' ', 0, 'L', 0,
212 't', 0, 'd', 0, ' ', 0, 'a', 0, 'n', 0, 'd', 0, ' ', 0, 'R', 0,
213 'o', 0, 'c', 0, 'k', 0, 'b', 0, 'o', 0, 'x', 0};
214
215static unsigned char string_descriptor_product[] = {
216 0x1c, 0x03,
217 'i', 0, 'R', 0, 'i', 0, 'v', 0, 'e', 0, 'r', 0, ' ', 0, 'i', 0,
218 'F', 0, 'P', 0, '7', 0, '0', 0, '0', 0};
219
220static unsigned char *string_descriptor[N_STRING_DESCRIPTORS] = {
221 string_descriptor_vendor,
222 string_descriptor_product
223};
224
225static inline int ep_index(int n, int dir)
226{
227 return (n << 1) | dir;
228}
229
230static inline int epidx_dir(int idx)
231{
232 return idx & 1;
233}
234
235static inline int epidx_n(int idx)
236{
237 return idx >> 1;
238}
239
240int usb_connected(void)
241{
242 return GPIO7_READ & 1;
243}
244
245static inline void usb_select_endpoint(int idx)
246{
247 ISP1582_EPINDEX = idx;
248}
249
250static inline void usb_select_setup_endpoint(void)
251{
252 ISP1582_EPINDEX = 0x20;
253}
254
255void usb_setup_endpoint(int idx, int max_pkt_size, int type)
256{
257 struct usb_endpoint *ep;
258
259 usb_select_endpoint(idx);
260 ISP1582_MAXPKSZ = max_pkt_size;
261 /* |= is in the original firmware */
262 ISP1582_EPTYPE |= 0x1c | type;
263 /* clear buffer */
264 ISP1582_CTRLFUN |= 0x10;
265 ISP1582_INTEN |= (1 << (10 + idx));
266
267 ep = &(endpoints[epidx_n(idx)]);
268 ep->halt[epidx_dir(idx)] = 0;
269 ep->enabled[epidx_dir(idx)] = 1;
270 ep->out_in_progress = 0;
271 ep->in_min_len = -1;
272 ep->in_ack = 0;
273 ep->max_pkt_size[epidx_dir(idx)] = max_pkt_size;
274}
275
276void usb_disable_endpoint(int idx)
277{
278 usb_select_endpoint(idx);
279 ISP1582_EPTYPE &= 8;
280 ISP1582_INTEN &= ~(1 << (10 + idx));
281 endpoints[epidx_n(idx)].enabled[epidx_dir(idx)] = 1;
282}
283
284void usb_reconnect(void)
285{
286 int i;
287 ISP1582_MODE &= ~1; /* SOFTCT off */
288 for (i = 0; i < 10000; i++)
289 nop_f();
290 ISP1582_MODE |= 1; /* SOFTCT on */
291}
292
293void usb_cleanup(void)
294{
295 ISP1582_MODE &= ~1; /* SOFTCT off */
296}
297
298void usb_setup(int reset)
299{
300 int i;
301
302 for (i = 0; i < N_ENDPOINTS; i++)
303 endpoints[i].enabled[0] = endpoints[i].enabled[1] = 0;
304
305 ISP1582_UNLOCK = ISP1582_UNLOCK_CODE;
306 if (!reset)
307 ISP1582_MODE = 0x88; /* CLKAON | GLINTENA */
308 ISP1582_INTCONF = 0x57;
309 ISP1582_INTEN = 0xd39;
310
311 ISP1582_ADDRESS = reset ? 0x80: 0;
312
313 usb_setup_endpoint(ep_index(0, DIR_RX), 64, 0);
314 usb_setup_endpoint(ep_index(0, DIR_TX), 64, 0);
315
316 ISP1582_MODE |= 1; /* SOFTCT on */
317
318 usb_state = STATE_DEFAULT;
319 usb_remote_wakeup = 0;
320}
321
322static int usb_get_packet(unsigned char *buf, int max_len)
323{
324 int len, i;
325 len = ISP1582_BUFLEN;
326
327 if (max_len < 0 || max_len > len)
328 max_len = len;
329
330 i = 0;
331 while (i < len)
332 {
333 unsigned short d = ISP1582_DATA;
334 if (i < max_len)
335 buf[i] = d & 0xff;
336 i++;
337 if (i < max_len)
338 buf[i] = (d >> 8) & 0xff;
339 i++;
340 }
341 return max_len;
342}
343
344static void usb_receive(int n)
345{
346 int len;
347
348 if (endpoints[n].halt[DIR_RX]
349 || !endpoints[n].enabled[DIR_RX]
350 || endpoints[n].in_min_len < 0
351 || !endpoints[n].in_ack)
352 return;
353
354 endpoints[n].in_ack = 0;
355
356 usb_select_endpoint(ep_index(n, DIR_RX));
357
358 len = usb_get_packet(endpoints[n].in_buf + endpoints[n].in_ptr,
359 endpoints[n].in_max_len - endpoints[n].in_ptr);
360 endpoints[n].in_ptr += len;
361 if (endpoints[n].in_ptr >= endpoints[n].in_min_len) {
362 endpoints[n].in_min_len = -1;
363 if (endpoints[n].in_done)
364 (*(endpoints[n].in_done))(n, endpoints[n].in_buf,
365 endpoints[n].in_ptr);
366 }
367}
368
369static int usb_out_buffer_full(int ep)
370{
371 usb_select_endpoint(ep_index(ep, DIR_TX));
372 if (ISP1582_EPTYPE & 4)
373 return (ISP1582_BUFSTAT & 3) == 3;
374 else
375 return (ISP1582_BUFSTAT & 3) != 0;
376}
377
378static void usb_send(int n)
379{
380 int max_pkt_size, len;
381 int i;
382 unsigned char *p;
383
384#ifdef LCD_DEBUG
385 if (endpoints[n].halt[DIR_TX])
386 log_char('H');
387 if (!endpoints[n].out_in_progress)
388 log_char('$');
389#endif
390
391 if (endpoints[n].halt[DIR_TX]
392 || !endpoints[n].enabled[DIR_TX]
393 || !endpoints[n].out_in_progress)
394 return;
395
396 if (endpoints[n].out_ptr < 0)
397 {
398 endpoints[n].out_in_progress = 0;
399 if (endpoints[n].out_done)
400 (*(endpoints[n].out_done))(n, endpoints[n].out_buf,
401 endpoints[n].out_len);
402 return;
403 }
404
405 if (usb_out_buffer_full(n))
406 {
407 log_char('F');
408 return;
409 }
410
411 usb_select_endpoint(ep_index(n, DIR_TX));
412 max_pkt_size = endpoints[n].max_pkt_size[DIR_TX];
413 len = endpoints[n].out_len - endpoints[n].out_ptr;
414 if (len > max_pkt_size)
415 len = max_pkt_size;
416
417 log_char('0' + (len % 10));
418 ISP1582_BUFLEN = len;
419 p = endpoints[n].out_buf + endpoints[n].out_ptr;
420 i = 0;
421 while (len - i >= 2) {
422 ISP1582_DATA = p[i] | (p[i + 1] << 8);
423 i += 2;
424 }
425 if (i < len)
426 ISP1582_DATA = p[i];
427
428 endpoints[n].out_ptr += len;
429
430/*
431 if (endpoints[n].out_ptr == endpoints[n].out_len
432 && len < max_pkt_size)
433*/
434 if (endpoints[n].out_ptr == endpoints[n].out_len)
435 endpoints[n].out_ptr = -1;
436}
437
438static void usb_stall_endpoint(int idx)
439{
440 usb_select_endpoint(idx);
441 ISP1582_CTRLFUN |= 1;
442 endpoints[epidx_n(idx)].halt[epidx_dir(idx)] = 1;
443}
444
445static void usb_unstall_endpoint(int idx)
446{
447 usb_select_endpoint(idx);
448 ISP1582_CTRLFUN &= ~1;
449 ISP1582_EPTYPE &= ~8;
450 ISP1582_EPTYPE |= 8;
451 ISP1582_CTRLFUN |= 0x10;
452 if (epidx_dir(idx) == DIR_TX)
453 endpoints[epidx_n(idx)].out_in_progress = 0;
454 else
455 {
456 endpoints[epidx_n(idx)].in_min_len = -1;
457 endpoints[epidx_n(idx)].in_ack = 0;
458 }
459 endpoints[epidx_n(idx)].halt[epidx_dir(idx)] = 0;
460}
461
462static void usb_status_ack(int dir)
463{
464 log_char(dir ? '@' : '#');
465 usb_select_endpoint(ep_index(0, dir));
466 ISP1582_CTRLFUN |= 2;
467}
468
469static void usb_set_address(int adr)
470{
471 ISP1582_ADDRESS = adr | 0x80;
472}
473
474static void usb_data_stage_enable(int dir)
475{
476 usb_select_endpoint(ep_index(0, dir));
477 ISP1582_CTRLFUN |= 4;
478}
479
480static void usb_request_error(void)
481{
482 usb_stall_endpoint(ep_index(0, DIR_TX));
483 usb_stall_endpoint(ep_index(0, DIR_RX));
484}
485
486static void usb_receive_block(unsigned char *buf, int min_len,
487 int max_len,
488 void (*in_done)(int, unsigned char *, int),
489 int ep)
490{
491 endpoints[ep].in_done = in_done;
492 endpoints[ep].in_buf = buf;
493 endpoints[ep].in_max_len = max_len;
494 endpoints[ep].in_min_len = min_len;
495 endpoints[ep].in_ptr = 0;
496 usb_receive(ep);
497}
498
499static void usb_send_block(unsigned char *buf, int len,
500 void (*done)(int, unsigned char *, int),
501 int ep)
502{
503 endpoints[ep].out_done = done;
504 endpoints[ep].out_buf = buf;
505 endpoints[ep].out_len = len;
506 endpoints[ep].out_ptr = 0;
507 endpoints[ep].out_in_progress = 1;
508 usb_send(ep);
509}
510
511static void out_send_status(int n, unsigned char *buf, int len)
512{
513 (void)n;
514 (void)buf;
515 (void)len;
516 usb_status_ack(DIR_RX);
517}
518
519static void usb_send_block_and_status(unsigned char *buf, int len, int ep)
520{
521 usb_send_block(buf, len, out_send_status, ep);
522}
523
524static void usb_setup_set_address(int adr)
525{
526 usb_set_address(adr);
527 usb_state = adr ? STATE_ADDRESS : STATE_DEFAULT;
528 usb_status_ack(DIR_TX);
529}
530
531static void usb_send_descriptor(unsigned char *device_descriptor,
532 int descriptor_len, int buffer_len)
533{
534 int len = descriptor_len < buffer_len ? descriptor_len : buffer_len;
535 usb_send_block_and_status(device_descriptor, len, 0);
536}
537
538static void usb_setup_get_descriptor(int type, int index, int lang, int len)
539{
540 (void)lang;
541 usb_data_stage_enable(DIR_TX);
542 switch (type)
543 {
544 case 1:
545 if (index == 0)
546 usb_send_descriptor(device_descriptor,
547 sizeof(device_descriptor), len);
548 else
549 usb_request_error();
550 break;
551 case 2:
552 if (index == 0)
553 usb_send_descriptor(cfg_descriptor,
554 sizeof(cfg_descriptor), len);
555 else
556 usb_request_error();
557 break;
558 case 3:
559 if (index == 0)
560 usb_send_descriptor(lang_descriptor,
561 sizeof(lang_descriptor), len);
562 else if (index <= N_STRING_DESCRIPTORS)
563 usb_send_descriptor(string_descriptor[index - 1],
564 string_descriptor[index - 1][0],
565 len);
566 else
567 usb_request_error();
568 break;
569 default:
570 usb_request_error();
571 }
572}
573
574static void usb_setup_get_configuration(void)
575{
576 setup_out_buf[0] = (usb_state == STATE_CONFIGURED) ? 1 : 0;
577 usb_data_stage_enable(DIR_TX);
578 usb_send_block_and_status(setup_out_buf, 1, 0);
579}
580
581static void usb_setup_interface(void)
582{
583 usb_setup_endpoint(ep_index(1, DIR_RX), 64, TYPE_BULK);
584 usb_setup_endpoint(ep_index(1, DIR_TX), 64, TYPE_BULK);
585}
586
587static void usb_setup_set_configuration(int value)
588{
589 switch (value)
590 {
591 case 0:
592 usb_disable_endpoint(ep_index(1, DIR_RX));
593 usb_disable_endpoint(ep_index(1, DIR_TX));
594 usb_state = STATE_ADDRESS;
595 usb_status_ack(DIR_TX);
596 break;
597 case 1:
598 usb_setup_interface();
599 usb_state = STATE_CONFIGURED;
600 usb_status_ack(DIR_TX);
601 break;
602 default:
603 usb_request_error();
604 }
605}
606
607static void usb_send_status(void)
608{
609 usb_data_stage_enable(DIR_TX);
610 usb_send_block_and_status(setup_out_buf, 2, 0);
611}
612
613static void usb_setup_get_device_status(void)
614{
615 setup_out_buf[0] = (usb_remote_wakeup != 0) ? 2 : 0;
616 setup_out_buf[1] = 0;
617 usb_send_status();
618}
619
620static void usb_setup_clear_device_feature(int feature)
621{
622 if (feature == 1) {
623 usb_remote_wakeup = 0;
624 usb_status_ack(DIR_TX);
625 } else
626 usb_request_error();
627}
628
629static void usb_setup_set_device_feature(int feature)
630{
631 if (feature == 1) {
632 usb_remote_wakeup = 1;
633 usb_status_ack(DIR_TX);
634 } else
635 usb_request_error();
636}
637
638static void usb_setup_clear_endpoint_feature(int endpoint, int feature)
639{
640 if (usb_state != STATE_CONFIGURED || feature != 0)
641 usb_request_error();
642 else if ((endpoint & 0xf) == 1)
643 {
644 usb_unstall_endpoint(ep_index(endpoint & 0xf, endpoint >> 7));
645 usb_status_ack(DIR_TX);
646 }
647 else
648 usb_request_error();
649}
650
651static void usb_setup_set_endpoint_feature(int endpoint, int feature)
652{
653 if (usb_state != STATE_CONFIGURED || feature != 0)
654 usb_request_error();
655 else if ((endpoint & 0xf) == 1)
656 {
657 usb_stall_endpoint(ep_index(endpoint & 0xf, endpoint >> 7));
658 usb_status_ack(DIR_TX);
659 }
660 else
661 usb_request_error();
662}
663
664static void usb_setup_get_interface_status(int interface)
665{
666 if (usb_state != STATE_CONFIGURED || interface != 0)
667 usb_request_error();
668 else
669 {
670 setup_out_buf[0] = setup_out_buf[1] = 0;
671 usb_send_status();
672 }
673}
674
675static void usb_setup_get_endpoint_status(int endpoint)
676{
677 if ((usb_state == STATE_CONFIGURED && (endpoint & 0xf) <= 1)
678 || (usb_state == STATE_ADDRESS && (endpoint & 0xf) == 0))
679 {
680 setup_out_buf[0] = endpoints[endpoint & 0xf].halt[endpoint >> 7];
681 setup_out_buf[1] = 0;
682 usb_send_status();
683 }
684 else
685 usb_request_error();
686}
687
688static void usb_setup_get_interface(int interface)
689{
690 if (usb_state != STATE_CONFIGURED || interface != 0)
691 usb_request_error();
692 else
693 {
694 setup_out_buf[0] = 0;
695 usb_data_stage_enable(DIR_TX);
696 usb_send_block_and_status(setup_out_buf, 1, 0);
697 }
698}
699
700static void usb_setup_set_interface(int interface, int setting)
701{
702 if (usb_state != STATE_CONFIGURED || interface != 0 || setting != 0)
703 usb_request_error();
704 else
705 {
706 usb_setup_interface();
707 usb_status_ack(DIR_TX);
708 }
709}
710
711static void usb_handle_setup_pkt(unsigned char *pkt)
712{
713 switch ((pkt[0] << 8) | pkt[1])
714 {
715 case 0x0005:
716 log_char('A');
717 usb_setup_set_address(pkt[2]);
718 break;
719 case 0x8006:
720 log_char('D');
721 usb_setup_get_descriptor(pkt[3], pkt[2], (pkt[5] << 8) | pkt[4],
722 (pkt[7] << 8) | pkt[6]);
723 break;
724 case 0x8008:
725 usb_setup_get_configuration();
726 break;
727 case 0x0009:
728 usb_setup_set_configuration(pkt[2]);
729 break;
730 case 0x8000:
731 usb_setup_get_device_status();
732 break;
733 case 0x8100:
734 usb_setup_get_interface_status(pkt[4]);
735 break;
736 case 0x8200:
737 usb_setup_get_endpoint_status(pkt[4]);
738 break;
739 case 0x0001:
740 usb_setup_clear_device_feature(pkt[2]);
741 break;
742 case 0x0201:
743 usb_setup_clear_endpoint_feature(pkt[4], pkt[2]);
744 break;
745 case 0x0003:
746 usb_setup_set_device_feature(pkt[2]);
747 break;
748 case 0x0203:
749 usb_setup_set_endpoint_feature(pkt[4], pkt[2]);
750 break;
751 case 0x810a:
752 usb_setup_get_interface(pkt[4]);
753 break;
754 case 0x010b:
755 usb_setup_set_interface(pkt[4], pkt[2]);
756 break;
757 case 0x0103:
758 /* set interface feature */
759 case 0x0101:
760 /* clear interface feature */
761 case 0x0007:
762 /* set descriptor */
763 case 0x820c:
764 /* synch frame */
765 default:
766 usb_request_error();
767 }
768}
769
770static void usb_handle_setup_rx(void)
771{
772 int len;
773#ifdef LCD_DEBUG
774 char s[20];
775 int i;
776#endif
777 usb_select_setup_endpoint();
778 len = usb_get_packet(setup_pkt_buf, 8);
779
780 if (len == 8)
781 usb_handle_setup_pkt(setup_pkt_buf);
782
783#ifdef LCD_DEBUG
784/*
785 snprintf(s, 10, "l%02x", len);
786 lcd_puts(0, 5, s);
787*/
788 for (i = 0; i < 8; i++)
789 snprintf(s + i * 2, 3, "%02x", setup_pkt_buf[i]);
790 lcd_puts(0, 0, s);
791 lcd_update();
792#endif
793}
794
795static void usb_handle_data_int(int ep, int dir)
796{
797 if (dir == DIR_TX)
798 usb_send(ep);
799 else
800 {
801 endpoints[ep].in_ack = 1;
802 usb_receive(ep);
803 }
804}
805
806static void usb_handle_int(int i)
807{
808#ifdef LCD_DEBUG
809/*
810 char s[10];
811 snprintf(s, sizeof(s), "%02d", i);
812 lcd_puts(0, 2, s);
813 lcd_update();
814*/
815 int_count[i]++;
816 if (i == 10)
817 log_char('o');
818 if (i == 11)
819 log_char('i');
820 if (i == 12)
821 log_char('O');
822 if (i == 13)
823 log_char('I');
824#endif
825
826 if (i >= 10)
827 usb_handle_data_int((i - 10) / 2, i % 2);
828 else
829 {
830 switch (i)
831 {
832 case 0:
833 log_char('r');
834 usb_setup(1);
835 break;
836 case 8:
837 log_char('s');
838 usb_handle_setup_rx();
839 break;
840 }
841 }
842
843}
844
845void usb_handle_interrupts(void)
846{
847#ifdef LCD_DEBUG
848 char s[20];
849#endif
850
851 while (1)
852 {
853 unsigned long ints;
854 int i;
855
856#ifdef LCD_DEBUG
857 /*
858 snprintf(s, sizeof(s), "i%08lx", ISP1582_INT);
859 lcd_puts(0, 2, s);
860 */
861#endif
862
863 ints = ISP1582_INT & ISP1582_INTEN;
864 if (!ints) break;
865
866 i = 0;
867 while (!(ints & (1 << i)))
868 i++;
869 ISP1582_INT = 1 << i;
870 usb_handle_int(i);
871
872#ifdef LCD_DEBUG
873 for (i = 0; i < 8; i++)
874 snprintf(s + i * 2, 3, "%02x", int_count[i]);
875 lcd_puts(0, 6, s);
876 for (i = 0; i < 8; i++)
877 snprintf(s + i * 2, 3, "%02x", int_count[i + 8]);
878 lcd_puts(0, 7, s);
879#endif
880 }
881#ifdef LCD_DEBUG
882/*
883 lcd_puts(0, 3, usb_connected() ? "C" : "N");
884 lcd_update();
885*/
886#endif
887}
888
889static inline int fifo_mod(int n)
890{
891 return (n >= SERIAL_BUF_SIZE) ? n - SERIAL_BUF_SIZE : n;
892}
893
894static void fifo_init(struct serial_fifo *fifo)
895{
896 fifo->head = fifo->tail = 0;
897}
898
899static int fifo_empty(struct serial_fifo *fifo)
900{
901 return fifo->head == fifo->tail;
902}
903
904static int fifo_full(struct serial_fifo *fifo)
905{
906 return fifo_mod(fifo->head + 1) == fifo->tail;
907}
908
909static void fifo_remove(struct serial_fifo *fifo, int n)
910{
911 fifo->tail = fifo_mod(fifo->tail + n);
912}
913
914/*
915 Not used:
916static void fifo_add(struct serial_fifo *fifo, int n)
917{
918 fifo->head = fifo_mod(fifo->head + n);
919}
920
921static void fifo_free_block(struct serial_fifo *fifo,
922 unsigned char **ptr, int *len)
923{
924 *ptr = fifo->buf + fifo->head;
925 if (fifo->head >= fifo->tail)
926 {
927 int l = SERIAL_BUF_SIZE - fifo->head;
928 if (fifo->tail == 0)
929 l--;
930 *len = l;
931 }
932 else
933 *len = fifo->tail - fifo->head - 1;
934}
935*/
936
937static int fifo_free_space(struct serial_fifo *fifo)
938{
939 if (fifo->head >= fifo->tail)
940 return SERIAL_BUF_SIZE - (fifo->head - fifo->tail) - 1;
941 else
942 return fifo->tail - fifo->head - 1;
943}
944
945static int fifo_get_byte(struct serial_fifo *fifo)
946{
947 int r = fifo->buf[fifo->tail];
948 fifo->tail = fifo_mod(fifo->tail + 1);
949 return r;
950}
951
952static void fifo_put_byte(struct serial_fifo *fifo, int b)
953{
954 fifo->buf[fifo->head] = b;
955 fifo->head = fifo_mod(fifo->head + 1);
956}
957
958static void fifo_full_block(struct serial_fifo *fifo,
959 unsigned char **ptr, int *len)
960{
961 *ptr = fifo->buf + fifo->tail;
962 if (fifo->head >= fifo->tail)
963 *len = fifo->head - fifo->tail;
964 else
965 *len = SERIAL_BUF_SIZE - fifo->tail;
966}
967
968static void serial_fill_in_fifo(int ep, unsigned char *buf, int len);
969static void serial_free_out_fifo(int ep, unsigned char *buf, int len);
970
971static void serial_restart_input(int ep)
972{
973 if (fifo_free_space(&serial_in_fifo) >= 64)
974 usb_receive_block(serial_in_pkt, 1, 64, serial_fill_in_fifo, ep);
975}
976
977static void serial_fill_in_fifo(int ep, unsigned char *buf, int len)
978{
979 int i;
980 for (i = 0; i < len; i++)
981 fifo_put_byte(&serial_in_fifo, buf[i]);
982 serial_restart_input(ep);
983}
984
985static void serial_restart_output(int ep)
986{
987 unsigned char *block;
988 int blen;
989 fifo_full_block(&serial_out_fifo, &block, &blen);
990 if (blen)
991 {
992#ifdef LCD_DEBUG
993 char s[20];
994 snprintf(s, sizeof(s), "o%03lx/%03x", block - serial_out_fifo.buf,
995 blen);
996 lcd_puts(0, 2, s);
997 lcd_update();
998#endif
999 usb_send_block(block, blen, serial_free_out_fifo, ep);
1000 }
1001}
1002
1003static void serial_free_out_fifo(int ep, unsigned char *buf, int len)
1004{
1005 (void)buf;
1006 fifo_remove(&serial_out_fifo, len);
1007 serial_restart_output(ep);
1008}
1009
1010void usb_serial_handle(void)
1011{
1012#ifdef BUTTONS
1013 static int t = 0;
1014
1015 t++;
1016 if (t >= 1000)
1017 {
1018 int b;
1019 t = 0;
1020 yield();
1021 b = button_get(false);
1022 if (b == BUTTON_PLAY)
1023 system_reboot();
1024 else if (b & BUTTON_REL)
1025 usb_reconnect();
1026 }
1027#endif
1028
1029
1030 if (!usb_connect_state)
1031 {
1032 if (usb_connected())
1033 {
1034 int i;
1035 GPIO3_SET = 4;
1036 (*(volatile unsigned long *)0x80005004) = 2;
1037 (*(volatile unsigned long *)0x80005008) = 0;
1038 for (i = 0; i < 100000; i++)
1039 nop_f();
1040 usb_setup(0);
1041 usb_connect_state = 1;
1042 }
1043 }
1044 else
1045 {
1046 if (!usb_connected())
1047 {
1048 usb_connect_state = 0;
1049 usb_cleanup();
1050 }
1051 else
1052 {
1053 usb_handle_interrupts();
1054
1055 if (usb_state == STATE_CONFIGURED)
1056 {
1057 if (endpoints[1].in_min_len < 0)
1058 serial_restart_input(1);
1059 if (!endpoints[1].out_in_progress)
1060 serial_restart_output(1);
1061 }
1062 }
1063 }
1064}
1065
1066
1067/*
1068 Not used:
1069static int usb_serial_in_empty(void)
1070{
1071 return fifo_empty(&serial_in_fifo);
1072}
1073*/
1074
1075int usb_serial_get_byte(void)
1076{
1077 while (fifo_empty(&serial_in_fifo))
1078 usb_serial_handle();
1079 return fifo_get_byte(&serial_in_fifo);
1080}
1081
1082int usb_serial_try_get_byte(void)
1083{
1084 int r;
1085 if (fifo_empty(&serial_in_fifo))
1086 r = -1;
1087 else
1088 r = fifo_get_byte(&serial_in_fifo);
1089 usb_serial_handle();
1090 return r;
1091}
1092
1093/*
1094 Not used:
1095static int usb_serial_out_full(void)
1096{
1097 return fifo_full(&serial_out_fifo);
1098}
1099*/
1100
1101void usb_serial_put_byte(int b)
1102{
1103 while (fifo_full(&serial_out_fifo))
1104 usb_serial_handle();
1105 fifo_put_byte(&serial_out_fifo, b);
1106 usb_serial_handle();
1107}
1108
1109int usb_serial_try_put_byte(int b)
1110{
1111 int r = -1;
1112 if (!fifo_full(&serial_out_fifo))
1113 {
1114 fifo_put_byte(&serial_out_fifo, b);
1115 r = 0;
1116 }
1117 usb_serial_handle();
1118 return r;
1119}
1120
1121void usb_serial_init(void)
1122{
1123 fifo_init(&serial_in_fifo);
1124 fifo_init(&serial_out_fifo);
1125 usb_connect_state = 0;
1126}