1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Tomasz Malesinski
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
25 /* #include "config.h" */
28 #include "ifp_usb_serial.h"
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))
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))
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))
62 #define ISP1582_UNLOCK_CODE 0xaa37
69 #define STATE_DEFAULT 0
70 #define STATE_ADDRESS 1
71 #define STATE_CONFIGURED 2
77 unsigned char *out_buf
;
80 void (*out_done
)(int, unsigned char *, int);
81 unsigned char out_in_progress
;
83 unsigned char *in_buf
;
87 void (*in_done
)(int, unsigned char *, int);
90 unsigned char halt
[2];
91 unsigned char enabled
[2];
92 short max_pkt_size
[2];
95 static char usb_connect_state
;
97 static struct usb_endpoint endpoints
[N_ENDPOINTS
];
99 static unsigned char setup_pkt_buf
[8];
100 static unsigned char setup_out_buf
[8];
101 static unsigned char usb_state
;
102 static unsigned char usb_remote_wakeup
;
105 static unsigned char int_count
[32];
107 static int log_pos_x
= 0;
108 static int log_pos_y
= 3;
111 static void nop_f(void)
116 static void log_char(char c
)
123 lcd_puts(log_pos_x
, log_pos_y
, s
);
138 #define SERIAL_BUF_SIZE 1024
142 unsigned char buf
[SERIAL_BUF_SIZE
];
146 static struct serial_fifo serial_in_fifo
;
147 static struct serial_fifo serial_out_fifo
;
148 static unsigned char serial_in_pkt
[64];
150 static unsigned char device_descriptor
[18] = {
152 0x01, /* descriptor type */
153 0x10, 0x01, /* USB version (1.1) */
154 0xff, 0xff, /* class and subclass */
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 */
166 static unsigned char cfg_descriptor
[32] = {
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 */
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 */
187 0x05, /* descriptor type */
188 0x81, /* endpoint 1 IN */
189 0x02, /* attributes (bulk) */
190 0x40, 0x00, /* max packet size */
194 0x05, /* descriptor type */
195 0x01, /* endpoint 1 IN */
196 0x02, /* attributes (bulk) */
197 0x40, 0x00, /* max packet size */
201 static unsigned char lang_descriptor
[4] = {
203 0x03, /* descriptor type */
204 0x09, 0x04 /* English (US) */
207 #define N_STRING_DESCRIPTORS 2
209 static unsigned char string_descriptor_vendor
[] = {
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};
215 static unsigned char string_descriptor_product
[] = {
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};
220 static unsigned char *string_descriptor
[N_STRING_DESCRIPTORS
] = {
221 string_descriptor_vendor
,
222 string_descriptor_product
225 static inline int ep_index(int n
, int dir
)
227 return (n
<< 1) | dir
;
230 static inline int epidx_dir(int idx
)
235 static inline int epidx_n(int idx
)
240 int usb_connected(void)
242 return GPIO7_READ
& 1;
245 static inline void usb_select_endpoint(int idx
)
247 ISP1582_EPINDEX
= idx
;
250 static inline void usb_select_setup_endpoint(void)
252 ISP1582_EPINDEX
= 0x20;
255 void usb_setup_endpoint(int idx
, int max_pkt_size
, int type
)
257 struct usb_endpoint
*ep
;
259 usb_select_endpoint(idx
);
260 ISP1582_MAXPKSZ
= max_pkt_size
;
261 /* |= is in the original firmware */
262 ISP1582_EPTYPE
|= 0x1c | type
;
264 ISP1582_CTRLFUN
|= 0x10;
265 ISP1582_INTEN
|= (1 << (10 + idx
));
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;
273 ep
->max_pkt_size
[epidx_dir(idx
)] = max_pkt_size
;
276 void usb_disable_endpoint(int idx
)
278 usb_select_endpoint(idx
);
280 ISP1582_INTEN
&= ~(1 << (10 + idx
));
281 endpoints
[epidx_n(idx
)].enabled
[epidx_dir(idx
)] = 1;
284 void usb_reconnect(void)
287 ISP1582_MODE
&= ~1; /* SOFTCT off */
288 for (i
= 0; i
< 10000; i
++)
290 ISP1582_MODE
|= 1; /* SOFTCT on */
293 void usb_cleanup(void)
295 ISP1582_MODE
&= ~1; /* SOFTCT off */
298 void usb_setup(int reset
)
302 for (i
= 0; i
< N_ENDPOINTS
; i
++)
303 endpoints
[i
].enabled
[0] = endpoints
[i
].enabled
[1] = 0;
305 ISP1582_UNLOCK
= ISP1582_UNLOCK_CODE
;
307 ISP1582_MODE
= 0x88; /* CLKAON | GLINTENA */
308 ISP1582_INTCONF
= 0x57;
309 ISP1582_INTEN
= 0xd39;
311 ISP1582_ADDRESS
= reset
? 0x80: 0;
313 usb_setup_endpoint(ep_index(0, DIR_RX
), 64, 0);
314 usb_setup_endpoint(ep_index(0, DIR_TX
), 64, 0);
316 ISP1582_MODE
|= 1; /* SOFTCT on */
318 usb_state
= STATE_DEFAULT
;
319 usb_remote_wakeup
= 0;
322 static int usb_get_packet(unsigned char *buf
, int max_len
)
325 len
= ISP1582_BUFLEN
;
327 if (max_len
< 0 || max_len
> len
)
333 unsigned short d
= ISP1582_DATA
;
338 buf
[i
] = (d
>> 8) & 0xff;
344 static void usb_receive(int n
)
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
)
354 endpoints
[n
].in_ack
= 0;
356 usb_select_endpoint(ep_index(n
, DIR_RX
));
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
);
369 static int usb_out_buffer_full(int ep
)
371 usb_select_endpoint(ep_index(ep
, DIR_TX
));
372 if (ISP1582_EPTYPE
& 4)
373 return (ISP1582_BUFSTAT
& 3) == 3;
375 return (ISP1582_BUFSTAT
& 3) != 0;
378 static void usb_send(int n
)
380 int max_pkt_size
, len
;
385 if (endpoints
[n
].halt
[DIR_TX
])
387 if (!endpoints
[n
].out_in_progress
)
391 if (endpoints
[n
].halt
[DIR_TX
]
392 || !endpoints
[n
].enabled
[DIR_TX
]
393 || !endpoints
[n
].out_in_progress
)
396 if (endpoints
[n
].out_ptr
< 0)
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
);
405 if (usb_out_buffer_full(n
))
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
)
417 log_char('0' + (len
% 10));
418 ISP1582_BUFLEN
= len
;
419 p
= endpoints
[n
].out_buf
+ endpoints
[n
].out_ptr
;
421 while (len
- i
>= 2) {
422 ISP1582_DATA
= p
[i
] | (p
[i
+ 1] << 8);
428 endpoints
[n
].out_ptr
+= len
;
431 if (endpoints[n].out_ptr == endpoints[n].out_len
432 && len < max_pkt_size)
434 if (endpoints
[n
].out_ptr
== endpoints
[n
].out_len
)
435 endpoints
[n
].out_ptr
= -1;
438 static void usb_stall_endpoint(int idx
)
440 usb_select_endpoint(idx
);
441 ISP1582_CTRLFUN
|= 1;
442 endpoints
[epidx_n(idx
)].halt
[epidx_dir(idx
)] = 1;
445 static void usb_unstall_endpoint(int idx
)
447 usb_select_endpoint(idx
);
448 ISP1582_CTRLFUN
&= ~1;
449 ISP1582_EPTYPE
&= ~8;
451 ISP1582_CTRLFUN
|= 0x10;
452 if (epidx_dir(idx
) == DIR_TX
)
453 endpoints
[epidx_n(idx
)].out_in_progress
= 0;
456 endpoints
[epidx_n(idx
)].in_min_len
= -1;
457 endpoints
[epidx_n(idx
)].in_ack
= 0;
459 endpoints
[epidx_n(idx
)].halt
[epidx_dir(idx
)] = 0;
462 static void usb_status_ack(int dir
)
464 log_char(dir
? '@' : '#');
465 usb_select_endpoint(ep_index(0, dir
));
466 ISP1582_CTRLFUN
|= 2;
469 static void usb_set_address(int adr
)
471 ISP1582_ADDRESS
= adr
| 0x80;
474 static void usb_data_stage_enable(int dir
)
476 usb_select_endpoint(ep_index(0, dir
));
477 ISP1582_CTRLFUN
|= 4;
480 static void usb_request_error(void)
482 usb_stall_endpoint(ep_index(0, DIR_TX
));
483 usb_stall_endpoint(ep_index(0, DIR_RX
));
486 static void usb_receive_block(unsigned char *buf
, int min_len
,
488 void (*in_done
)(int, unsigned char *, int),
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;
499 static void usb_send_block(unsigned char *buf
, int len
,
500 void (*done
)(int, unsigned char *, int),
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;
511 static void out_send_status(int n
, unsigned char *buf
, int len
)
516 usb_status_ack(DIR_RX
);
519 static void usb_send_block_and_status(unsigned char *buf
, int len
, int ep
)
521 usb_send_block(buf
, len
, out_send_status
, ep
);
524 static void usb_setup_set_address(int adr
)
526 usb_set_address(adr
);
527 usb_state
= adr
? STATE_ADDRESS
: STATE_DEFAULT
;
528 usb_status_ack(DIR_TX
);
531 static void usb_send_descriptor(unsigned char *device_descriptor
,
532 int descriptor_len
, int buffer_len
)
534 int len
= descriptor_len
< buffer_len
? descriptor_len
: buffer_len
;
535 usb_send_block_and_status(device_descriptor
, len
, 0);
538 static void usb_setup_get_descriptor(int type
, int index
, int lang
, int len
)
541 usb_data_stage_enable(DIR_TX
);
546 usb_send_descriptor(device_descriptor
,
547 sizeof(device_descriptor
), len
);
553 usb_send_descriptor(cfg_descriptor
,
554 sizeof(cfg_descriptor
), len
);
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],
574 static void usb_setup_get_configuration(void)
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);
581 static void usb_setup_interface(void)
583 usb_setup_endpoint(ep_index(1, DIR_RX
), 64, TYPE_BULK
);
584 usb_setup_endpoint(ep_index(1, DIR_TX
), 64, TYPE_BULK
);
587 static void usb_setup_set_configuration(int value
)
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
);
598 usb_setup_interface();
599 usb_state
= STATE_CONFIGURED
;
600 usb_status_ack(DIR_TX
);
607 static void usb_send_status(void)
609 usb_data_stage_enable(DIR_TX
);
610 usb_send_block_and_status(setup_out_buf
, 2, 0);
613 static void usb_setup_get_device_status(void)
615 setup_out_buf
[0] = (usb_remote_wakeup
!= 0) ? 2 : 0;
616 setup_out_buf
[1] = 0;
620 static void usb_setup_clear_device_feature(int feature
)
623 usb_remote_wakeup
= 0;
624 usb_status_ack(DIR_TX
);
629 static void usb_setup_set_device_feature(int feature
)
632 usb_remote_wakeup
= 1;
633 usb_status_ack(DIR_TX
);
638 static void usb_setup_clear_endpoint_feature(int endpoint
, int feature
)
640 if (usb_state
!= STATE_CONFIGURED
|| feature
!= 0)
642 else if ((endpoint
& 0xf) == 1)
644 usb_unstall_endpoint(ep_index(endpoint
& 0xf, endpoint
>> 7));
645 usb_status_ack(DIR_TX
);
651 static void usb_setup_set_endpoint_feature(int endpoint
, int feature
)
653 if (usb_state
!= STATE_CONFIGURED
|| feature
!= 0)
655 else if ((endpoint
& 0xf) == 1)
657 usb_stall_endpoint(ep_index(endpoint
& 0xf, endpoint
>> 7));
658 usb_status_ack(DIR_TX
);
664 static void usb_setup_get_interface_status(int interface
)
666 if (usb_state
!= STATE_CONFIGURED
|| interface
!= 0)
670 setup_out_buf
[0] = setup_out_buf
[1] = 0;
675 static void usb_setup_get_endpoint_status(int endpoint
)
677 if ((usb_state
== STATE_CONFIGURED
&& (endpoint
& 0xf) <= 1)
678 || (usb_state
== STATE_ADDRESS
&& (endpoint
& 0xf) == 0))
680 setup_out_buf
[0] = endpoints
[endpoint
& 0xf].halt
[endpoint
>> 7];
681 setup_out_buf
[1] = 0;
688 static void usb_setup_get_interface(int interface
)
690 if (usb_state
!= STATE_CONFIGURED
|| interface
!= 0)
694 setup_out_buf
[0] = 0;
695 usb_data_stage_enable(DIR_TX
);
696 usb_send_block_and_status(setup_out_buf
, 1, 0);
700 static void usb_setup_set_interface(int interface
, int setting
)
702 if (usb_state
!= STATE_CONFIGURED
|| interface
!= 0 || setting
!= 0)
706 usb_setup_interface();
707 usb_status_ack(DIR_TX
);
711 static void usb_handle_setup_pkt(unsigned char *pkt
)
713 switch ((pkt
[0] << 8) | pkt
[1])
717 usb_setup_set_address(pkt
[2]);
721 usb_setup_get_descriptor(pkt
[3], pkt
[2], (pkt
[5] << 8) | pkt
[4],
722 (pkt
[7] << 8) | pkt
[6]);
725 usb_setup_get_configuration();
728 usb_setup_set_configuration(pkt
[2]);
731 usb_setup_get_device_status();
734 usb_setup_get_interface_status(pkt
[4]);
737 usb_setup_get_endpoint_status(pkt
[4]);
740 usb_setup_clear_device_feature(pkt
[2]);
743 usb_setup_clear_endpoint_feature(pkt
[4], pkt
[2]);
746 usb_setup_set_device_feature(pkt
[2]);
749 usb_setup_set_endpoint_feature(pkt
[4], pkt
[2]);
752 usb_setup_get_interface(pkt
[4]);
755 usb_setup_set_interface(pkt
[4], pkt
[2]);
758 /* set interface feature */
760 /* clear interface feature */
770 static void usb_handle_setup_rx(void)
777 usb_select_setup_endpoint();
778 len
= usb_get_packet(setup_pkt_buf
, 8);
781 usb_handle_setup_pkt(setup_pkt_buf
);
785 snprintf(s, 10, "l%02x", len);
788 for (i
= 0; i
< 8; i
++)
789 snprintf(s
+ i
* 2, 3, "%02x", setup_pkt_buf
[i
]);
795 static void usb_handle_data_int(int ep
, int dir
)
801 endpoints
[ep
].in_ack
= 1;
806 static void usb_handle_int(int i
)
811 snprintf(s, sizeof(s), "%02d", i);
827 usb_handle_data_int((i
- 10) / 2, i
% 2);
838 usb_handle_setup_rx();
845 void usb_handle_interrupts(void)
858 snprintf(s, sizeof(s), "i%08lx", ISP1582_INT);
863 ints
= ISP1582_INT
& ISP1582_INTEN
;
867 while (!(ints
& (1 << i
)))
869 ISP1582_INT
= 1 << i
;
873 for (i
= 0; i
< 8; i
++)
874 snprintf(s
+ i
* 2, 3, "%02x", int_count
[i
]);
876 for (i
= 0; i
< 8; i
++)
877 snprintf(s
+ i
* 2, 3, "%02x", int_count
[i
+ 8]);
883 lcd_puts(0, 3, usb_connected() ? "C" : "N");
889 static inline int fifo_mod(int n
)
891 return (n
>= SERIAL_BUF_SIZE
) ? n
- SERIAL_BUF_SIZE
: n
;
894 static void fifo_init(struct serial_fifo
*fifo
)
896 fifo
->head
= fifo
->tail
= 0;
899 static int fifo_empty(struct serial_fifo
*fifo
)
901 return fifo
->head
== fifo
->tail
;
904 static int fifo_full(struct serial_fifo
*fifo
)
906 return fifo_mod(fifo
->head
+ 1) == fifo
->tail
;
909 static void fifo_remove(struct serial_fifo
*fifo
, int n
)
911 fifo
->tail
= fifo_mod(fifo
->tail
+ n
);
916 static void fifo_add(struct serial_fifo *fifo, int n)
918 fifo->head = fifo_mod(fifo->head + n);
921 static void fifo_free_block(struct serial_fifo *fifo,
922 unsigned char **ptr, int *len)
924 *ptr = fifo->buf + fifo->head;
925 if (fifo->head >= fifo->tail)
927 int l = SERIAL_BUF_SIZE - fifo->head;
933 *len = fifo->tail - fifo->head - 1;
937 static int fifo_free_space(struct serial_fifo
*fifo
)
939 if (fifo
->head
>= fifo
->tail
)
940 return SERIAL_BUF_SIZE
- (fifo
->head
- fifo
->tail
) - 1;
942 return fifo
->tail
- fifo
->head
- 1;
945 static int fifo_get_byte(struct serial_fifo
*fifo
)
947 int r
= fifo
->buf
[fifo
->tail
];
948 fifo
->tail
= fifo_mod(fifo
->tail
+ 1);
952 static void fifo_put_byte(struct serial_fifo
*fifo
, int b
)
954 fifo
->buf
[fifo
->head
] = b
;
955 fifo
->head
= fifo_mod(fifo
->head
+ 1);
958 static void fifo_full_block(struct serial_fifo
*fifo
,
959 unsigned char **ptr
, int *len
)
961 *ptr
= fifo
->buf
+ fifo
->tail
;
962 if (fifo
->head
>= fifo
->tail
)
963 *len
= fifo
->head
- fifo
->tail
;
965 *len
= SERIAL_BUF_SIZE
- fifo
->tail
;
968 static void serial_fill_in_fifo(int ep
, unsigned char *buf
, int len
);
969 static void serial_free_out_fifo(int ep
, unsigned char *buf
, int len
);
971 static void serial_restart_input(int ep
)
973 if (fifo_free_space(&serial_in_fifo
) >= 64)
974 usb_receive_block(serial_in_pkt
, 1, 64, serial_fill_in_fifo
, ep
);
977 static void serial_fill_in_fifo(int ep
, unsigned char *buf
, int len
)
980 for (i
= 0; i
< len
; i
++)
981 fifo_put_byte(&serial_in_fifo
, buf
[i
]);
982 serial_restart_input(ep
);
985 static void serial_restart_output(int ep
)
987 unsigned char *block
;
989 fifo_full_block(&serial_out_fifo
, &block
, &blen
);
994 snprintf(s
, sizeof(s
), "o%03lx/%03x", block
- serial_out_fifo
.buf
,
999 usb_send_block(block
, blen
, serial_free_out_fifo
, ep
);
1003 static void serial_free_out_fifo(int ep
, unsigned char *buf
, int len
)
1006 fifo_remove(&serial_out_fifo
, len
);
1007 serial_restart_output(ep
);
1010 void usb_serial_handle(void)
1021 b
= button_get(false);
1022 if (b
== BUTTON_PLAY
)
1024 else if (b
& BUTTON_REL
)
1030 if (!usb_connect_state
)
1032 if (usb_connected())
1036 (*(volatile unsigned long *)0x80005004) = 2;
1037 (*(volatile unsigned long *)0x80005008) = 0;
1038 for (i
= 0; i
< 100000; i
++)
1041 usb_connect_state
= 1;
1046 if (!usb_connected())
1048 usb_connect_state
= 0;
1053 usb_handle_interrupts();
1055 if (usb_state
== STATE_CONFIGURED
)
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);
1069 static int usb_serial_in_empty(void)
1071 return fifo_empty(&serial_in_fifo);
1075 int usb_serial_get_byte(void)
1077 while (fifo_empty(&serial_in_fifo
))
1078 usb_serial_handle();
1079 return fifo_get_byte(&serial_in_fifo
);
1082 int usb_serial_try_get_byte(void)
1085 if (fifo_empty(&serial_in_fifo
))
1088 r
= fifo_get_byte(&serial_in_fifo
);
1089 usb_serial_handle();
1095 static int usb_serial_out_full(void)
1097 return fifo_full(&serial_out_fifo);
1101 void usb_serial_put_byte(int b
)
1103 while (fifo_full(&serial_out_fifo
))
1104 usb_serial_handle();
1105 fifo_put_byte(&serial_out_fifo
, b
);
1106 usb_serial_handle();
1109 int usb_serial_try_put_byte(int b
)
1112 if (!fifo_full(&serial_out_fifo
))
1114 fifo_put_byte(&serial_out_fifo
, b
);
1117 usb_serial_handle();
1121 void usb_serial_init(void)
1123 fifo_init(&serial_in_fifo
);
1124 fifo_init(&serial_out_fifo
);
1125 usb_connect_state
= 0;