diff options
author | Solomon Peachy <pizza@shaftnet.org> | 2021-04-23 11:31:58 -0400 |
---|---|---|
committer | Solomon Peachy <pizza@shaftnet.org> | 2021-04-26 07:42:58 -0400 |
commit | 7d0d32dbc65a8332a49aeed3a97e24cb109958b2 (patch) | |
tree | 36ce03b1b8564bedfd8e3df0ec088a0fc6bdc06f /firmware/ifp_usb_serial.c | |
parent | 3ba2f6e5c7383c5d7a6f8a1c38b6b5403a8a5dcb (diff) | |
download | rockbox-7d0d32dbc65a8332a49aeed3a97e24cb109958b2.tar.gz rockbox-7d0d32dbc65a8332a49aeed3a97e24cb109958b2.zip |
Nuke the never-functional iriver ifp-7xx port
It never worked, and hasn't compiled in something like a decade, Given
the HW capabilities (limited onboard flash, no expandability) there's
really no point in trying to fix/complete it.
Change-Id: I7d175089840396f8891645bd10010d730dd5bfdc
Diffstat (limited to 'firmware/ifp_usb_serial.c')
-rw-r--r-- | firmware/ifp_usb_serial.c | 1121 |
1 files changed, 0 insertions, 1121 deletions
diff --git a/firmware/ifp_usb_serial.c b/firmware/ifp_usb_serial.c deleted file mode 100644 index f7e3a51858..0000000000 --- a/firmware/ifp_usb_serial.c +++ /dev/null | |||
@@ -1,1121 +0,0 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2006 by Tomasz Malesinski | ||
11 | * | ||
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. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | /* | ||
23 | #define LCD_DEBUG | ||
24 | #define BUTTONS | ||
25 | */ | ||
26 | |||
27 | /* #include "config.h" */ | ||
28 | #include <stdlib.h> | ||
29 | #include "pnx0101.h" | ||
30 | #include "ifp_usb_serial.h" | ||
31 | |||
32 | #ifdef BUTTONS | ||
33 | #include "kernel.h" | ||
34 | #include "button.h" | ||
35 | #include "system.h" | ||
36 | #endif | ||
37 | |||
38 | #ifdef LCD_DEBUG | ||
39 | #include "lcd.h" | ||
40 | #endif | ||
41 | |||
42 | |||
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)) | ||
49 | |||
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)) | ||
57 | |||
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)) | ||
62 | |||
63 | #define ISP1582_UNLOCK_CODE 0xaa37 | ||
64 | |||
65 | #define TYPE_BULK 2 | ||
66 | |||
67 | #define STATE_DEFAULT 0 | ||
68 | #define STATE_ADDRESS 1 | ||
69 | #define STATE_CONFIGURED 2 | ||
70 | |||
71 | #define N_ENDPOINTS 2 | ||
72 | |||
73 | struct usb_endpoint | ||
74 | { | ||
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; | ||
80 | |||
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; | ||
87 | |||
88 | unsigned char halt[2]; | ||
89 | unsigned char enabled[2]; | ||
90 | short max_pkt_size[2]; | ||
91 | }; | ||
92 | |||
93 | static char usb_connect_state; | ||
94 | |||
95 | static struct usb_endpoint endpoints[N_ENDPOINTS]; | ||
96 | |||
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; | ||
101 | |||
102 | #ifdef LCD_DEBUG | ||
103 | static unsigned char int_count[32]; | ||
104 | |||
105 | static int log_pos_x = 0; | ||
106 | static int log_pos_y = 3; | ||
107 | #endif | ||
108 | |||
109 | static void nop_f(void) | ||
110 | { | ||
111 | } | ||
112 | |||
113 | #ifdef LCD_DEBUG | ||
114 | static void log_char(char c) | ||
115 | { | ||
116 | char s[2]; | ||
117 | |||
118 | s[0] = c; | ||
119 | s[1] = 0; | ||
120 | |||
121 | lcd_puts(log_pos_x, log_pos_y, s); | ||
122 | lcd_update(); | ||
123 | log_pos_x++; | ||
124 | if (log_pos_x >= 16) | ||
125 | { | ||
126 | log_pos_x = 0; | ||
127 | log_pos_y++; | ||
128 | if (log_pos_y > 5) | ||
129 | log_pos_y = 3; | ||
130 | } | ||
131 | } | ||
132 | #else | ||
133 | #define log_char(c) | ||
134 | #endif | ||
135 | |||
136 | #define SERIAL_BUF_SIZE 1024 | ||
137 | |||
138 | struct serial_fifo | ||
139 | { | ||
140 | unsigned char buf[SERIAL_BUF_SIZE]; | ||
141 | short head, tail; | ||
142 | }; | ||
143 | |||
144 | static struct serial_fifo serial_in_fifo; | ||
145 | static struct serial_fifo serial_out_fifo; | ||
146 | static unsigned char serial_in_pkt[64]; | ||
147 | |||
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 */ | ||
162 | }; | ||
163 | |||
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 */ | ||
197 | }; | ||
198 | |||
199 | static unsigned char lang_descriptor[4] = { | ||
200 | 0x04, /* length */ | ||
201 | 0x03, /* descriptor type */ | ||
202 | 0x09, 0x04 /* English (US) */ | ||
203 | }; | ||
204 | |||
205 | #define N_STRING_DESCRIPTORS 2 | ||
206 | |||
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}; | ||
212 | |||
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}; | ||
217 | |||
218 | static unsigned char *string_descriptor[N_STRING_DESCRIPTORS] = { | ||
219 | string_descriptor_vendor, | ||
220 | string_descriptor_product | ||
221 | }; | ||
222 | |||
223 | static inline int ep_index(int n, int dir) | ||
224 | { | ||
225 | return (n << 1) | dir; | ||
226 | } | ||
227 | |||
228 | static inline int epidx_dir(int idx) | ||
229 | { | ||
230 | return idx & 1; | ||
231 | } | ||
232 | |||
233 | static inline int epidx_n(int idx) | ||
234 | { | ||
235 | return idx >> 1; | ||
236 | } | ||
237 | |||
238 | int usb_connected(void) | ||
239 | { | ||
240 | return GPIO7_READ & 1; | ||
241 | } | ||
242 | |||
243 | static inline void usb_select_endpoint(int idx) | ||
244 | { | ||
245 | ISP1582_EPINDEX = idx; | ||
246 | } | ||
247 | |||
248 | static inline void usb_select_setup_endpoint(void) | ||
249 | { | ||
250 | ISP1582_EPINDEX = 0x20; | ||
251 | } | ||
252 | |||
253 | static void usb_setup_endpoint(int idx, int max_pkt_size, int type) | ||
254 | { | ||
255 | struct usb_endpoint *ep; | ||
256 | |||
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)); | ||
264 | |||
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; | ||
272 | } | ||
273 | |||
274 | static void usb_disable_endpoint(int idx) | ||
275 | { | ||
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; | ||
280 | } | ||
281 | |||
282 | void usb_reconnect(void) | ||
283 | { | ||
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 */ | ||
289 | } | ||
290 | |||
291 | static void usb_cleanup(void) | ||
292 | { | ||
293 | ISP1582_MODE &= ~1; /* SOFTCT off */ | ||
294 | } | ||
295 | |||
296 | static void usb_setup(int reset) | ||
297 | { | ||
298 | int i; | ||
299 | |||
300 | for (i = 0; i < N_ENDPOINTS; i++) | ||
301 | endpoints[i].enabled[0] = endpoints[i].enabled[1] = 0; | ||
302 | |||
303 | ISP1582_UNLOCK = ISP1582_UNLOCK_CODE; | ||
304 | if (!reset) | ||
305 | ISP1582_MODE = 0x88; /* CLKAON | GLINTENA */ | ||
306 | ISP1582_INTCONF = 0x57; | ||
307 | ISP1582_INTEN = 0xd39; | ||
308 | |||
309 | ISP1582_ADDRESS = reset ? 0x80: 0; | ||
310 | |||
311 | usb_setup_endpoint(ep_index(0, DIR_RX), 64, 0); | ||
312 | usb_setup_endpoint(ep_index(0, DIR_TX), 64, 0); | ||
313 | |||
314 | ISP1582_MODE |= 1; /* SOFTCT on */ | ||
315 | |||
316 | usb_state = STATE_DEFAULT; | ||
317 | usb_remote_wakeup = 0; | ||
318 | } | ||
319 | |||
320 | static int usb_get_packet(unsigned char *buf, int max_len) | ||
321 | { | ||
322 | int len, i; | ||
323 | len = ISP1582_BUFLEN; | ||
324 | |||
325 | if (max_len < 0 || max_len > len) | ||
326 | max_len = len; | ||
327 | |||
328 | i = 0; | ||
329 | while (i < len) | ||
330 | { | ||
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++; | ||
338 | } | ||
339 | return max_len; | ||
340 | } | ||
341 | |||
342 | static void usb_receive(int n) | ||
343 | { | ||
344 | int len; | ||
345 | |||
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; | ||
351 | |||
352 | endpoints[n].in_ack = 0; | ||
353 | |||
354 | usb_select_endpoint(ep_index(n, DIR_RX)); | ||
355 | |||
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); | ||
364 | } | ||
365 | } | ||
366 | |||
367 | static int usb_out_buffer_full(int ep) | ||
368 | { | ||
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; | ||
374 | } | ||
375 | |||
376 | static void usb_send(int n) | ||
377 | { | ||
378 | int max_pkt_size, len; | ||
379 | int i; | ||
380 | unsigned char *p; | ||
381 | |||
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 | ||
388 | |||
389 | if (endpoints[n].halt[DIR_TX] | ||
390 | || !endpoints[n].enabled[DIR_TX] | ||
391 | || !endpoints[n].out_in_progress) | ||
392 | return; | ||
393 | |||
394 | if (endpoints[n].out_ptr < 0) | ||
395 | { | ||
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; | ||
401 | } | ||
402 | |||
403 | if (usb_out_buffer_full(n)) | ||
404 | { | ||
405 | log_char('F'); | ||
406 | return; | ||
407 | } | ||
408 | |||
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; | ||
414 | |||
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; | ||
422 | } | ||
423 | if (i < len) | ||
424 | ISP1582_DATA = p[i]; | ||
425 | |||
426 | endpoints[n].out_ptr += len; | ||
427 | |||
428 | /* | ||
429 | if (endpoints[n].out_ptr == endpoints[n].out_len | ||
430 | && len < max_pkt_size) | ||
431 | */ | ||
432 | if (endpoints[n].out_ptr == endpoints[n].out_len) | ||
433 | endpoints[n].out_ptr = -1; | ||
434 | } | ||
435 | |||
436 | static void usb_stall_endpoint(int idx) | ||
437 | { | ||
438 | usb_select_endpoint(idx); | ||
439 | ISP1582_CTRLFUN |= 1; | ||
440 | endpoints[epidx_n(idx)].halt[epidx_dir(idx)] = 1; | ||
441 | } | ||
442 | |||
443 | static void usb_unstall_endpoint(int idx) | ||
444 | { | ||
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 | ||
453 | { | ||
454 | endpoints[epidx_n(idx)].in_min_len = -1; | ||
455 | endpoints[epidx_n(idx)].in_ack = 0; | ||
456 | } | ||
457 | endpoints[epidx_n(idx)].halt[epidx_dir(idx)] = 0; | ||
458 | } | ||
459 | |||
460 | static void usb_status_ack(int dir) | ||
461 | { | ||
462 | log_char(dir ? '@' : '#'); | ||
463 | usb_select_endpoint(ep_index(0, dir)); | ||
464 | ISP1582_CTRLFUN |= 2; | ||
465 | } | ||
466 | |||
467 | static void usb_set_address(int adr) | ||
468 | { | ||
469 | ISP1582_ADDRESS = adr | 0x80; | ||
470 | } | ||
471 | |||
472 | static void usb_data_stage_enable(int dir) | ||
473 | { | ||
474 | usb_select_endpoint(ep_index(0, dir)); | ||
475 | ISP1582_CTRLFUN |= 4; | ||
476 | } | ||
477 | |||
478 | static void usb_request_error(void) | ||
479 | { | ||
480 | usb_stall_endpoint(ep_index(0, DIR_TX)); | ||
481 | usb_stall_endpoint(ep_index(0, DIR_RX)); | ||
482 | } | ||
483 | |||
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) | ||
488 | { | ||
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); | ||
495 | } | ||
496 | |||
497 | static void usb_send_block(unsigned char *buf, int len, | ||
498 | void (*done)(int, unsigned char *, int), | ||
499 | int ep) | ||
500 | { | ||
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); | ||
507 | } | ||
508 | |||
509 | static void out_send_status(int n, unsigned char *buf, int len) | ||
510 | { | ||
511 | (void)n; | ||
512 | (void)buf; | ||
513 | (void)len; | ||
514 | usb_status_ack(DIR_RX); | ||
515 | } | ||
516 | |||
517 | static void usb_send_block_and_status(unsigned char *buf, int len, int ep) | ||
518 | { | ||
519 | usb_send_block(buf, len, out_send_status, ep); | ||
520 | } | ||
521 | |||
522 | static void usb_setup_set_address(int adr) | ||
523 | { | ||
524 | usb_set_address(adr); | ||
525 | usb_state = adr ? STATE_ADDRESS : STATE_DEFAULT; | ||
526 | usb_status_ack(DIR_TX); | ||
527 | } | ||
528 | |||
529 | static void usb_send_descriptor(unsigned char *device_descriptor, | ||
530 | int descriptor_len, int buffer_len) | ||
531 | { | ||
532 | int len = descriptor_len < buffer_len ? descriptor_len : buffer_len; | ||
533 | usb_send_block_and_status(device_descriptor, len, 0); | ||
534 | } | ||
535 | |||
536 | static void usb_setup_get_descriptor(int type, int index, int lang, int len) | ||
537 | { | ||
538 | (void)lang; | ||
539 | usb_data_stage_enable(DIR_TX); | ||
540 | switch (type) | ||
541 | { | ||
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(); | ||
569 | } | ||
570 | } | ||
571 | |||
572 | static void usb_setup_get_configuration(void) | ||
573 | { | ||
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); | ||
577 | } | ||
578 | |||
579 | static void usb_setup_interface(void) | ||
580 | { | ||
581 | usb_setup_endpoint(ep_index(1, DIR_RX), 64, TYPE_BULK); | ||
582 | usb_setup_endpoint(ep_index(1, DIR_TX), 64, TYPE_BULK); | ||
583 | } | ||
584 | |||
585 | static void usb_setup_set_configuration(int value) | ||
586 | { | ||
587 | switch (value) | ||
588 | { | ||
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(); | ||
602 | } | ||
603 | } | ||
604 | |||
605 | static void usb_send_status(void) | ||
606 | { | ||
607 | usb_data_stage_enable(DIR_TX); | ||
608 | usb_send_block_and_status(setup_out_buf, 2, 0); | ||
609 | } | ||
610 | |||
611 | static void usb_setup_get_device_status(void) | ||
612 | { | ||
613 | setup_out_buf[0] = (usb_remote_wakeup != 0) ? 2 : 0; | ||
614 | setup_out_buf[1] = 0; | ||
615 | usb_send_status(); | ||
616 | } | ||
617 | |||
618 | static void usb_setup_clear_device_feature(int feature) | ||
619 | { | ||
620 | if (feature == 1) { | ||
621 | usb_remote_wakeup = 0; | ||
622 | usb_status_ack(DIR_TX); | ||
623 | } else | ||
624 | usb_request_error(); | ||
625 | } | ||
626 | |||
627 | static void usb_setup_set_device_feature(int feature) | ||
628 | { | ||
629 | if (feature == 1) { | ||
630 | usb_remote_wakeup = 1; | ||
631 | usb_status_ack(DIR_TX); | ||
632 | } else | ||
633 | usb_request_error(); | ||
634 | } | ||
635 | |||
636 | static void usb_setup_clear_endpoint_feature(int endpoint, int feature) | ||
637 | { | ||
638 | if (usb_state != STATE_CONFIGURED || feature != 0) | ||
639 | usb_request_error(); | ||
640 | else if ((endpoint & 0xf) == 1) | ||
641 | { | ||
642 | usb_unstall_endpoint(ep_index(endpoint & 0xf, endpoint >> 7)); | ||
643 | usb_status_ack(DIR_TX); | ||
644 | } | ||
645 | else | ||
646 | usb_request_error(); | ||
647 | } | ||
648 | |||
649 | static void usb_setup_set_endpoint_feature(int endpoint, int feature) | ||
650 | { | ||
651 | if (usb_state != STATE_CONFIGURED || feature != 0) | ||
652 | usb_request_error(); | ||
653 | else if ((endpoint & 0xf) == 1) | ||
654 | { | ||
655 | usb_stall_endpoint(ep_index(endpoint & 0xf, endpoint >> 7)); | ||
656 | usb_status_ack(DIR_TX); | ||
657 | } | ||
658 | else | ||
659 | usb_request_error(); | ||
660 | } | ||
661 | |||
662 | static void usb_setup_get_interface_status(int interface) | ||
663 | { | ||
664 | if (usb_state != STATE_CONFIGURED || interface != 0) | ||
665 | usb_request_error(); | ||
666 | else | ||
667 | { | ||
668 | setup_out_buf[0] = setup_out_buf[1] = 0; | ||
669 | usb_send_status(); | ||
670 | } | ||
671 | } | ||
672 | |||
673 | static void usb_setup_get_endpoint_status(int endpoint) | ||
674 | { | ||
675 | if ((usb_state == STATE_CONFIGURED && (endpoint & 0xf) <= 1) | ||
676 | || (usb_state == STATE_ADDRESS && (endpoint & 0xf) == 0)) | ||
677 | { | ||
678 | setup_out_buf[0] = endpoints[endpoint & 0xf].halt[endpoint >> 7]; | ||
679 | setup_out_buf[1] = 0; | ||
680 | usb_send_status(); | ||
681 | } | ||
682 | else | ||
683 | usb_request_error(); | ||
684 | } | ||
685 | |||
686 | static void usb_setup_get_interface(int interface) | ||
687 | { | ||
688 | if (usb_state != STATE_CONFIGURED || interface != 0) | ||
689 | usb_request_error(); | ||
690 | else | ||
691 | { | ||
692 | setup_out_buf[0] = 0; | ||
693 | usb_data_stage_enable(DIR_TX); | ||
694 | usb_send_block_and_status(setup_out_buf, 1, 0); | ||
695 | } | ||
696 | } | ||
697 | |||
698 | static void usb_setup_set_interface(int interface, int setting) | ||
699 | { | ||
700 | if (usb_state != STATE_CONFIGURED || interface != 0 || setting != 0) | ||
701 | usb_request_error(); | ||
702 | else | ||
703 | { | ||
704 | usb_setup_interface(); | ||
705 | usb_status_ack(DIR_TX); | ||
706 | } | ||
707 | } | ||
708 | |||
709 | static void usb_handle_setup_pkt(unsigned char *pkt) | ||
710 | { | ||
711 | switch ((pkt[0] << 8) | pkt[1]) | ||
712 | { | ||
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(); | ||
765 | } | ||
766 | } | ||
767 | |||
768 | static void usb_handle_setup_rx(void) | ||
769 | { | ||
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); | ||
777 | |||
778 | if (len == 8) | ||
779 | usb_handle_setup_pkt(setup_pkt_buf); | ||
780 | |||
781 | #ifdef LCD_DEBUG | ||
782 | /* | ||
783 | snprintf(s, 10, "l%02x", len); | ||
784 | lcd_puts(0, 5, s); | ||
785 | */ | ||
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 | ||
791 | } | ||
792 | |||
793 | static void usb_handle_data_int(int ep, int dir) | ||
794 | { | ||
795 | if (dir == DIR_TX) | ||
796 | usb_send(ep); | ||
797 | else | ||
798 | { | ||
799 | endpoints[ep].in_ack = 1; | ||
800 | usb_receive(ep); | ||
801 | } | ||
802 | } | ||
803 | |||
804 | static void usb_handle_int(int i) | ||
805 | { | ||
806 | #ifdef LCD_DEBUG | ||
807 | /* | ||
808 | char s[10]; | ||
809 | snprintf(s, sizeof(s), "%02d", i); | ||
810 | lcd_puts(0, 2, s); | ||
811 | lcd_update(); | ||
812 | */ | ||
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 | ||
823 | |||
824 | if (i >= 10) | ||
825 | usb_handle_data_int((i - 10) / 2, i % 2); | ||
826 | else | ||
827 | { | ||
828 | switch (i) | ||
829 | { | ||
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; | ||
838 | } | ||
839 | } | ||
840 | |||
841 | } | ||
842 | |||
843 | static void usb_handle_interrupts(void) | ||
844 | { | ||
845 | #ifdef LCD_DEBUG | ||
846 | char s[20]; | ||
847 | #endif | ||
848 | |||
849 | while (1) | ||
850 | { | ||
851 | unsigned long ints; | ||
852 | int i; | ||
853 | |||
854 | #ifdef LCD_DEBUG | ||
855 | /* | ||
856 | snprintf(s, sizeof(s), "i%08lx", ISP1582_INT); | ||
857 | lcd_puts(0, 2, s); | ||
858 | */ | ||
859 | #endif | ||
860 | |||
861 | ints = ISP1582_INT & ISP1582_INTEN; | ||
862 | if (!ints) break; | ||
863 | |||
864 | i = 0; | ||
865 | while (!(ints & (1 << i))) | ||
866 | i++; | ||
867 | ISP1582_INT = 1 << i; | ||
868 | usb_handle_int(i); | ||
869 | |||
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 | ||
878 | } | ||
879 | #ifdef LCD_DEBUG | ||
880 | /* | ||
881 | lcd_puts(0, 3, usb_connected() ? "C" : "N"); | ||
882 | lcd_update(); | ||
883 | */ | ||
884 | #endif | ||
885 | } | ||
886 | |||
887 | static inline int fifo_mod(int n) | ||
888 | { | ||
889 | return (n >= SERIAL_BUF_SIZE) ? n - SERIAL_BUF_SIZE : n; | ||
890 | } | ||
891 | |||
892 | static void fifo_init(struct serial_fifo *fifo) | ||
893 | { | ||
894 | fifo->head = fifo->tail = 0; | ||
895 | } | ||
896 | |||
897 | static int fifo_empty(struct serial_fifo *fifo) | ||
898 | { | ||
899 | return fifo->head == fifo->tail; | ||
900 | } | ||
901 | |||
902 | static int fifo_full(struct serial_fifo *fifo) | ||
903 | { | ||
904 | return fifo_mod(fifo->head + 1) == fifo->tail; | ||
905 | } | ||
906 | |||
907 | static void fifo_remove(struct serial_fifo *fifo, int n) | ||
908 | { | ||
909 | fifo->tail = fifo_mod(fifo->tail + n); | ||
910 | } | ||
911 | |||
912 | /* | ||
913 | Not used: | ||
914 | static void fifo_add(struct serial_fifo *fifo, int n) | ||
915 | { | ||
916 | fifo->head = fifo_mod(fifo->head + n); | ||
917 | } | ||
918 | |||
919 | static void fifo_free_block(struct serial_fifo *fifo, | ||
920 | unsigned char **ptr, int *len) | ||
921 | { | ||
922 | *ptr = fifo->buf + fifo->head; | ||
923 | if (fifo->head >= fifo->tail) | ||
924 | { | ||
925 | int l = SERIAL_BUF_SIZE - fifo->head; | ||
926 | if (fifo->tail == 0) | ||
927 | l--; | ||
928 | *len = l; | ||
929 | } | ||
930 | else | ||
931 | *len = fifo->tail - fifo->head - 1; | ||
932 | } | ||
933 | */ | ||
934 | |||
935 | static int fifo_free_space(struct serial_fifo *fifo) | ||
936 | { | ||
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; | ||
941 | } | ||
942 | |||
943 | static int fifo_get_byte(struct serial_fifo *fifo) | ||
944 | { | ||
945 | int r = fifo->buf[fifo->tail]; | ||
946 | fifo->tail = fifo_mod(fifo->tail + 1); | ||
947 | return r; | ||
948 | } | ||
949 | |||
950 | static void fifo_put_byte(struct serial_fifo *fifo, int b) | ||
951 | { | ||
952 | fifo->buf[fifo->head] = b; | ||
953 | fifo->head = fifo_mod(fifo->head + 1); | ||
954 | } | ||
955 | |||
956 | static void fifo_full_block(struct serial_fifo *fifo, | ||
957 | unsigned char **ptr, int *len) | ||
958 | { | ||
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; | ||
964 | } | ||
965 | |||
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); | ||
968 | |||
969 | static void serial_restart_input(int ep) | ||
970 | { | ||
971 | if (fifo_free_space(&serial_in_fifo) >= 64) | ||
972 | usb_receive_block(serial_in_pkt, 1, 64, serial_fill_in_fifo, ep); | ||
973 | } | ||
974 | |||
975 | static void serial_fill_in_fifo(int ep, unsigned char *buf, int len) | ||
976 | { | ||
977 | int i; | ||
978 | for (i = 0; i < len; i++) | ||
979 | fifo_put_byte(&serial_in_fifo, buf[i]); | ||
980 | serial_restart_input(ep); | ||
981 | } | ||
982 | |||
983 | static void serial_restart_output(int ep) | ||
984 | { | ||
985 | unsigned char *block; | ||
986 | int blen; | ||
987 | fifo_full_block(&serial_out_fifo, &block, &blen); | ||
988 | if (blen) | ||
989 | { | ||
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); | ||
995 | } | ||
996 | } | ||
997 | |||
998 | static void serial_free_out_fifo(int ep, unsigned char *buf, int len) | ||
999 | { | ||
1000 | (void)buf; | ||
1001 | fifo_remove(&serial_out_fifo, len); | ||
1002 | serial_restart_output(ep); | ||
1003 | } | ||
1004 | |||
1005 | static void usb_serial_handle(void) | ||
1006 | { | ||
1007 | #ifdef BUTTONS | ||
1008 | static int t = 0; | ||
1009 | |||
1010 | t++; | ||
1011 | if (t >= 1000) | ||
1012 | { | ||
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(); | ||
1021 | } | ||
1022 | #endif | ||
1023 | |||
1024 | |||
1025 | if (!usb_connect_state) | ||
1026 | { | ||
1027 | if (usb_connected()) | ||
1028 | { | ||
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; | ||
1037 | } | ||
1038 | } | ||
1039 | else | ||
1040 | { | ||
1041 | if (!usb_connected()) | ||
1042 | { | ||
1043 | usb_connect_state = 0; | ||
1044 | usb_cleanup(); | ||
1045 | } | ||
1046 | else | ||
1047 | { | ||
1048 | usb_handle_interrupts(); | ||
1049 | |||
1050 | if (usb_state == STATE_CONFIGURED) | ||
1051 | { | ||
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); | ||
1056 | } | ||
1057 | } | ||
1058 | } | ||
1059 | } | ||
1060 | |||
1061 | |||
1062 | /* | ||
1063 | Not used: | ||
1064 | static int usb_serial_in_empty(void) | ||
1065 | { | ||
1066 | return fifo_empty(&serial_in_fifo); | ||
1067 | } | ||
1068 | */ | ||
1069 | |||
1070 | int usb_serial_get_byte(void) | ||
1071 | { | ||
1072 | while (fifo_empty(&serial_in_fifo)) | ||
1073 | usb_serial_handle(); | ||
1074 | return fifo_get_byte(&serial_in_fifo); | ||
1075 | } | ||
1076 | |||
1077 | int usb_serial_try_get_byte(void) | ||
1078 | { | ||
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; | ||
1086 | } | ||
1087 | |||
1088 | /* | ||
1089 | Not used: | ||
1090 | static int usb_serial_out_full(void) | ||
1091 | { | ||
1092 | return fifo_full(&serial_out_fifo); | ||
1093 | } | ||
1094 | */ | ||
1095 | |||
1096 | void usb_serial_put_byte(int b) | ||
1097 | { | ||
1098 | while (fifo_full(&serial_out_fifo)) | ||
1099 | usb_serial_handle(); | ||
1100 | fifo_put_byte(&serial_out_fifo, b); | ||
1101 | usb_serial_handle(); | ||
1102 | } | ||
1103 | |||
1104 | int usb_serial_try_put_byte(int b) | ||
1105 | { | ||
1106 | int r = -1; | ||
1107 | if (!fifo_full(&serial_out_fifo)) | ||
1108 | { | ||
1109 | fifo_put_byte(&serial_out_fifo, b); | ||
1110 | r = 0; | ||
1111 | } | ||
1112 | usb_serial_handle(); | ||
1113 | return r; | ||
1114 | } | ||
1115 | |||
1116 | void usb_serial_init(void) | ||
1117 | { | ||
1118 | fifo_init(&serial_in_fifo); | ||
1119 | fifo_init(&serial_out_fifo); | ||
1120 | usb_connect_state = 0; | ||
1121 | } | ||