1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
27 /* #include "config.h" */
30 #include "ifp_usb_serial.h"
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
68 #define STATE_DEFAULT 0
69 #define STATE_ADDRESS 1
70 #define STATE_CONFIGURED 2
76 unsigned char *out_buf
;
79 void (*out_done
)(int, unsigned char *, int);
80 unsigned char out_in_progress
;
82 unsigned char *in_buf
;
86 void (*in_done
)(int, unsigned char *, int);
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
;
104 static unsigned char int_count
[32];
106 static int log_pos_x
= 0;
107 static int log_pos_y
= 3;
110 static void nop_f(void)
115 static void log_char(char c
)
122 lcd_puts(log_pos_x
, log_pos_y
, s
);
137 #define SERIAL_BUF_SIZE 1024
141 unsigned char buf
[SERIAL_BUF_SIZE
];
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] = {
151 0x01, /* descriptor type */
152 0x10, 0x01, /* USB version (1.1) */
153 0xff, 0xff, /* class and subclass */
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] = {
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 */
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 */
186 0x05, /* descriptor type */
187 0x81, /* endpoint 1 IN */
188 0x02, /* attributes (bulk) */
189 0x40, 0x00, /* max packet size */
193 0x05, /* descriptor type */
194 0x01, /* endpoint 1 IN */
195 0x02, /* attributes (bulk) */
196 0x40, 0x00, /* max packet size */
200 static unsigned char lang_descriptor
[4] = {
202 0x03, /* descriptor type */
203 0x09, 0x04 /* English (US) */
206 #define N_STRING_DESCRIPTORS 2
208 static unsigned char string_descriptor_vendor
[] = {
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
[] = {
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
)
234 static inline int epidx_n(int idx
)
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
;
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;
272 ep
->max_pkt_size
[epidx_dir(idx
)] = max_pkt_size
;
275 void usb_disable_endpoint(int idx
)
277 usb_select_endpoint(idx
);
279 ISP1582_INTEN
&= ~(1 << (10 + idx
));
280 endpoints
[epidx_n(idx
)].enabled
[epidx_dir(idx
)] = 1;
283 void usb_reconnect(void)
286 ISP1582_MODE
&= ~1; /* SOFTCT off */
287 for (i
= 0; i
< 10000; i
++)
289 ISP1582_MODE
|= 1; /* SOFTCT on */
292 void usb_cleanup(void)
294 ISP1582_MODE
&= ~1; /* SOFTCT off */
297 void usb_setup(int reset
)
301 for (i
= 0; i
< N_ENDPOINTS
; i
++)
302 endpoints
[i
].enabled
[0] = endpoints
[i
].enabled
[1] = 0;
304 ISP1582_UNLOCK
= ISP1582_UNLOCK_CODE
;
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
)
324 len
= ISP1582_BUFLEN
;
326 if (max_len
< 0 || max_len
> len
)
332 unsigned short d
= ISP1582_DATA
;
337 buf
[i
] = (d
>> 8) & 0xff;
343 static void usb_receive(int n
)
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
)
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;
374 return (ISP1582_BUFSTAT
& 3) != 0;
377 static void usb_send(int n
)
379 int max_pkt_size
, len
;
384 if (endpoints
[n
].halt
[DIR_TX
])
386 if (!endpoints
[n
].out_in_progress
)
390 if (endpoints
[n
].halt
[DIR_TX
]
391 || !endpoints
[n
].enabled
[DIR_TX
]
392 || !endpoints
[n
].out_in_progress
)
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
);
404 if (usb_out_buffer_full(n
))
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
)
416 log_char('0' + (len
% 10));
417 ISP1582_BUFLEN
= len
;
418 p
= endpoints
[n
].out_buf
+ endpoints
[n
].out_ptr
;
420 while (len
- i
>= 2) {
421 ISP1582_DATA
= p
[i
] | (p
[i
+ 1] << 8);
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;
450 ISP1582_CTRLFUN
|= 0x10;
451 if (epidx_dir(idx
) == DIR_TX
)
452 endpoints
[epidx_n(idx
)].out_in_progress
= 0;
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
,
487 void (*in_done
)(int, unsigned char *, int),
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;
498 static void usb_send_block(unsigned char *buf
, int len
,
499 void (*done
)(int, unsigned char *, int),
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;
510 static void out_send_status(int n
, unsigned char *buf
, int 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
)
540 usb_data_stage_enable(DIR_TX
);
545 usb_send_descriptor(device_descriptor
,
546 sizeof(device_descriptor
), len
);
552 usb_send_descriptor(cfg_descriptor
,
553 sizeof(cfg_descriptor
), len
);
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],
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
)
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
);
597 usb_setup_interface();
598 usb_state
= STATE_CONFIGURED
;
599 usb_status_ack(DIR_TX
);
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;
619 static void usb_setup_clear_device_feature(int feature
)
622 usb_remote_wakeup
= 0;
623 usb_status_ack(DIR_TX
);
628 static void usb_setup_set_device_feature(int feature
)
631 usb_remote_wakeup
= 1;
632 usb_status_ack(DIR_TX
);
637 static void usb_setup_clear_endpoint_feature(int endpoint
, int feature
)
639 if (usb_state
!= STATE_CONFIGURED
|| feature
!= 0)
641 else if ((endpoint
& 0xf) == 1)
643 usb_unstall_endpoint(ep_index(endpoint
& 0xf, endpoint
>> 7));
644 usb_status_ack(DIR_TX
);
650 static void usb_setup_set_endpoint_feature(int endpoint
, int feature
)
652 if (usb_state
!= STATE_CONFIGURED
|| feature
!= 0)
654 else if ((endpoint
& 0xf) == 1)
656 usb_stall_endpoint(ep_index(endpoint
& 0xf, endpoint
>> 7));
657 usb_status_ack(DIR_TX
);
663 static void usb_setup_get_interface_status(int interface
)
665 if (usb_state
!= STATE_CONFIGURED
|| interface
!= 0)
669 setup_out_buf
[0] = setup_out_buf
[1] = 0;
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;
687 static void usb_setup_get_interface(int interface
)
689 if (usb_state
!= STATE_CONFIGURED
|| interface
!= 0)
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)
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])
716 usb_setup_set_address(pkt
[2]);
720 usb_setup_get_descriptor(pkt
[3], pkt
[2], (pkt
[5] << 8) | pkt
[4],
721 (pkt
[7] << 8) | pkt
[6]);
724 usb_setup_get_configuration();
727 usb_setup_set_configuration(pkt
[2]);
730 usb_setup_get_device_status();
733 usb_setup_get_interface_status(pkt
[4]);
736 usb_setup_get_endpoint_status(pkt
[4]);
739 usb_setup_clear_device_feature(pkt
[2]);
742 usb_setup_clear_endpoint_feature(pkt
[4], pkt
[2]);
745 usb_setup_set_device_feature(pkt
[2]);
748 usb_setup_set_endpoint_feature(pkt
[4], pkt
[2]);
751 usb_setup_get_interface(pkt
[4]);
754 usb_setup_set_interface(pkt
[4], pkt
[2]);
757 /* set interface feature */
759 /* clear interface feature */
769 static void usb_handle_setup_rx(void)
776 usb_select_setup_endpoint();
777 len
= usb_get_packet(setup_pkt_buf
, 8);
780 usb_handle_setup_pkt(setup_pkt_buf
);
784 snprintf(s, 10, "l%02x", len);
787 for (i
= 0; i
< 8; i
++)
788 snprintf(s
+ i
* 2, 3, "%02x", setup_pkt_buf
[i
]);
794 static void usb_handle_data_int(int ep
, int dir
)
800 endpoints
[ep
].in_ack
= 1;
805 static void usb_handle_int(int i
)
810 snprintf(s, sizeof(s), "%02d", i);
826 usb_handle_data_int((i
- 10) / 2, i
% 2);
837 usb_handle_setup_rx();
844 void usb_handle_interrupts(void)
857 snprintf(s, sizeof(s), "i%08lx", ISP1582_INT);
862 ints
= ISP1582_INT
& ISP1582_INTEN
;
866 while (!(ints
& (1 << i
)))
868 ISP1582_INT
= 1 << i
;
872 for (i
= 0; i
< 8; i
++)
873 snprintf(s
+ i
* 2, 3, "%02x", int_count
[i
]);
875 for (i
= 0; i
< 8; i
++)
876 snprintf(s
+ i
* 2, 3, "%02x", int_count
[i
+ 8]);
882 lcd_puts(0, 3, usb_connected() ? "C" : "N");
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
);
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;
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;
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);
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
;
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
)
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
;
988 fifo_full_block(&serial_out_fifo
, &block
, &blen
);
993 snprintf(s
, sizeof(s
), "o%03lx/%03x", block
- serial_out_fifo
.buf
,
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
)
1005 fifo_remove(&serial_out_fifo
, len
);
1006 serial_restart_output(ep
);
1009 void usb_serial_handle(void)
1020 b
= button_get(false);
1021 if (b
== BUTTON_PLAY
)
1023 else if (b
& BUTTON_REL
)
1029 if (!usb_connect_state
)
1031 if (usb_connected())
1035 (*(volatile unsigned long *)0x80005004) = 2;
1036 (*(volatile unsigned long *)0x80005008) = 0;
1037 for (i
= 0; i
< 100000; i
++)
1040 usb_connect_state
= 1;
1045 if (!usb_connected())
1047 usb_connect_state
= 0;
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);
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)
1084 if (fifo_empty(&serial_in_fifo
))
1087 r
= fifo_get_byte(&serial_in_fifo
);
1088 usb_serial_handle();
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
)
1111 if (!fifo_full(&serial_out_fifo
))
1113 fifo_put_byte(&serial_out_fifo
, b
);
1116 usb_serial_handle();
1120 void usb_serial_init(void)
1122 fifo_init(&serial_in_fifo
);
1123 fifo_init(&serial_out_fifo
);
1124 usb_connect_state
= 0;