diff options
author | Tomasz Malesinski <tomal@rockbox.org> | 2006-01-25 01:37:39 +0000 |
---|---|---|
committer | Tomasz Malesinski <tomal@rockbox.org> | 2006-01-25 01:37:39 +0000 |
commit | 4bf024f872a0fce71b8861ef83a151fd758f3a6c (patch) | |
tree | 16a3308c7bf382fa8e77279b36ac7afdab485da2 | |
parent | e8e0b241bdaa20a6745c845c5a027b608524d8fb (diff) | |
download | rockbox-4bf024f872a0fce71b8861ef83a151fd758f3a6c.tar.gz rockbox-4bf024f872a0fce71b8861ef83a151fd758f3a6c.zip |
Generic USB serial driver on ISP1582
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@8445 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r-- | firmware/export/usb_serial.h | 26 | ||||
-rw-r--r-- | firmware/usb_serial.c | 1105 |
2 files changed, 1131 insertions, 0 deletions
diff --git a/firmware/export/usb_serial.h b/firmware/export/usb_serial.h new file mode 100644 index 0000000000..5daefeeff2 --- /dev/null +++ b/firmware/export/usb_serial.h | |||
@@ -0,0 +1,26 @@ | |||
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 | #ifndef _USB_SERIAL_H_ | ||
20 | #define _USB_SERIAL_H_ | ||
21 | |||
22 | void usb_serial_init(void); | ||
23 | void usb_serial_put_byte(int b); | ||
24 | int usb_serial_get_byte(void); | ||
25 | |||
26 | #endif | ||
diff --git a/firmware/usb_serial.c b/firmware/usb_serial.c new file mode 100644 index 0000000000..acf19e3b1a --- /dev/null +++ b/firmware/usb_serial.c | |||
@@ -0,0 +1,1105 @@ | |||
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 "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 | |||
75 | struct 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 | |||
95 | static char usb_connect_state; | ||
96 | |||
97 | static struct usb_endpoint endpoints[N_ENDPOINTS]; | ||
98 | |||
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; | ||
103 | |||
104 | #ifdef LCD_DEBUG | ||
105 | static unsigned char int_count[32]; | ||
106 | |||
107 | static int log_pos_x = 0; | ||
108 | static int log_pos_y = 3; | ||
109 | #endif | ||
110 | |||
111 | static void nop_f(void) | ||
112 | { | ||
113 | } | ||
114 | |||
115 | #ifdef LCD_DEBUG | ||
116 | static 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 | |||
140 | struct serial_fifo | ||
141 | { | ||
142 | unsigned char buf[SERIAL_BUF_SIZE]; | ||
143 | short head, tail; | ||
144 | }; | ||
145 | |||
146 | static struct serial_fifo serial_in_fifo; | ||
147 | static struct serial_fifo serial_out_fifo; | ||
148 | static unsigned char serial_in_pkt[64]; | ||
149 | |||
150 | static 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 | 0x08, /* 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 | |||
166 | static 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 | |||
201 | static 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 | |||
209 | static 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 | |||
215 | static 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 | |||
220 | static unsigned char *string_descriptor[N_STRING_DESCRIPTORS] = { | ||
221 | string_descriptor_vendor, | ||
222 | string_descriptor_product | ||
223 | }; | ||
224 | |||
225 | static inline int ep_index(int n, int dir) | ||
226 | { | ||
227 | return (n << 1) | dir; | ||
228 | } | ||
229 | |||
230 | static inline int epidx_dir(int idx) | ||
231 | { | ||
232 | return idx & 1; | ||
233 | } | ||
234 | |||
235 | static inline int epidx_n(int idx) | ||
236 | { | ||
237 | return idx >> 1; | ||
238 | } | ||
239 | |||
240 | int usb_connected(void) | ||
241 | { | ||
242 | return GPIO7_READ & 1; | ||
243 | } | ||
244 | |||
245 | static inline void usb_select_endpoint(int idx) | ||
246 | { | ||
247 | ISP1582_EPINDEX = idx; | ||
248 | } | ||
249 | |||
250 | static inline void usb_select_setup_endpoint(void) | ||
251 | { | ||
252 | ISP1582_EPINDEX = 0x20; | ||
253 | } | ||
254 | |||
255 | void 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 | |||
276 | void 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 | |||
284 | void 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 | |||
293 | void usb_cleanup(void) | ||
294 | { | ||
295 | ISP1582_MODE &= ~1; /* SOFTCT off */ | ||
296 | } | ||
297 | |||
298 | void 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), 8, 0); | ||
314 | usb_setup_endpoint(ep_index(0, DIR_TX), 8, 0); | ||
315 | |||
316 | ISP1582_MODE |= 1; /* SOFTCT on */ | ||
317 | |||
318 | usb_state = STATE_DEFAULT; | ||
319 | usb_remote_wakeup = 0; | ||
320 | } | ||
321 | |||
322 | static 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 | |||
344 | static 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 | |||
369 | static 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 | |||
378 | static 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 | |||
438 | static 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 | |||
445 | static 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 | |||
462 | static 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 | |||
469 | static void usb_set_address(int adr) | ||
470 | { | ||
471 | ISP1582_ADDRESS = adr | 0x80; | ||
472 | } | ||
473 | |||
474 | static void usb_data_stage_enable(int dir) | ||
475 | { | ||
476 | usb_select_endpoint(ep_index(0, dir)); | ||
477 | ISP1582_CTRLFUN |= 4; | ||
478 | } | ||
479 | |||
480 | static 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 | |||
486 | static 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 | |||
499 | static 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 | |||
511 | static 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 | |||
519 | static 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 | |||
524 | static 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 | |||
531 | static 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 | |||
538 | static 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 | |||
574 | static 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 | |||
581 | static 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 | |||
587 | static 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 | |||
607 | static 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 | |||
613 | static 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 | |||
620 | static 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 | |||
629 | static 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 | |||
638 | static 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 | |||
651 | static 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 | |||
664 | static 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 | |||
675 | static 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 | |||
688 | static 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 | |||
700 | static 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 | |||
711 | static 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 | |||
770 | static 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 | |||
795 | static 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 | |||
806 | static void usb_handle_int(int i) | ||
807 | { | ||
808 | #ifdef LCD_DEBUG | ||
809 | /* | ||
810 | char s[10]; | ||
811 | int_count[i]++; | ||
812 | snprintf(s, sizeof(s), "%02d", i); | ||
813 | lcd_puts(0, 2, s); | ||
814 | lcd_update(); | ||
815 | */ | ||
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 | |||
845 | void 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 | /* | ||
874 | for (i = 0; i < 8; i++) | ||
875 | snprintf(s + i * 2, 3, "%02x", int_count[i]); | ||
876 | lcd_puts(0, 6, s); | ||
877 | for (i = 0; i < 8; i++) | ||
878 | snprintf(s + i * 2, 3, "%02x", int_count[i + 8]); | ||
879 | lcd_puts(0, 7, s); | ||
880 | */ | ||
881 | #endif | ||
882 | } | ||
883 | #ifdef LCD_DEBUG | ||
884 | /* | ||
885 | lcd_puts(0, 3, usb_connected() ? "C" : "N"); | ||
886 | lcd_update(); | ||
887 | */ | ||
888 | #endif | ||
889 | } | ||
890 | |||
891 | static inline int fifo_mod(int n) | ||
892 | { | ||
893 | return (n >= SERIAL_BUF_SIZE) ? n - SERIAL_BUF_SIZE : n; | ||
894 | } | ||
895 | |||
896 | static void fifo_init(struct serial_fifo *fifo) | ||
897 | { | ||
898 | fifo->head = fifo->tail = 0; | ||
899 | } | ||
900 | |||
901 | static int fifo_empty(struct serial_fifo *fifo) | ||
902 | { | ||
903 | return fifo->head == fifo->tail; | ||
904 | } | ||
905 | |||
906 | static int fifo_full(struct serial_fifo *fifo) | ||
907 | { | ||
908 | return fifo_mod(fifo->head + 1) == fifo->tail; | ||
909 | } | ||
910 | |||
911 | static void fifo_remove(struct serial_fifo *fifo, int n) | ||
912 | { | ||
913 | fifo->tail = fifo_mod(fifo->tail + n); | ||
914 | } | ||
915 | |||
916 | /* | ||
917 | Not used: | ||
918 | static void fifo_add(struct serial_fifo *fifo, int n) | ||
919 | { | ||
920 | fifo->head = fifo_mod(fifo->head + n); | ||
921 | } | ||
922 | |||
923 | static void fifo_free_block(struct serial_fifo *fifo, | ||
924 | unsigned char **ptr, int *len) | ||
925 | { | ||
926 | *ptr = fifo->buf + fifo->head; | ||
927 | if (fifo->head >= fifo->tail) | ||
928 | { | ||
929 | int l = SERIAL_BUF_SIZE - fifo->head; | ||
930 | if (fifo->tail == 0) | ||
931 | l--; | ||
932 | *len = l; | ||
933 | } | ||
934 | else | ||
935 | *len = fifo->tail - fifo->head - 1; | ||
936 | } | ||
937 | */ | ||
938 | |||
939 | static int fifo_free_space(struct serial_fifo *fifo) | ||
940 | { | ||
941 | if (fifo->head >= fifo->tail) | ||
942 | return SERIAL_BUF_SIZE - (fifo->head - fifo->tail) - 1; | ||
943 | else | ||
944 | return fifo->tail - fifo->head - 1; | ||
945 | } | ||
946 | |||
947 | static int fifo_get_byte(struct serial_fifo *fifo) | ||
948 | { | ||
949 | int r = fifo->buf[fifo->tail]; | ||
950 | fifo->tail = fifo_mod(fifo->tail + 1); | ||
951 | return r; | ||
952 | } | ||
953 | |||
954 | static void fifo_put_byte(struct serial_fifo *fifo, int b) | ||
955 | { | ||
956 | fifo->buf[fifo->head] = b; | ||
957 | fifo->head = fifo_mod(fifo->head + 1); | ||
958 | } | ||
959 | |||
960 | static void fifo_full_block(struct serial_fifo *fifo, | ||
961 | unsigned char **ptr, int *len) | ||
962 | { | ||
963 | *ptr = fifo->buf + fifo->tail; | ||
964 | if (fifo->head >= fifo->tail) | ||
965 | *len = fifo->head - fifo->tail; | ||
966 | else | ||
967 | *len = SERIAL_BUF_SIZE - fifo->tail; | ||
968 | } | ||
969 | |||
970 | static void serial_fill_in_fifo(int ep, unsigned char *buf, int len); | ||
971 | static void serial_free_out_fifo(int ep, unsigned char *buf, int len); | ||
972 | |||
973 | static void serial_restart_input(int ep) | ||
974 | { | ||
975 | if (fifo_free_space(&serial_in_fifo) >= 64) | ||
976 | usb_receive_block(serial_in_pkt, 1, 64, serial_fill_in_fifo, ep); | ||
977 | } | ||
978 | |||
979 | static void serial_fill_in_fifo(int ep, unsigned char *buf, int len) | ||
980 | { | ||
981 | int i; | ||
982 | for (i = 0; i < len; i++) | ||
983 | fifo_put_byte(&serial_in_fifo, buf[i]); | ||
984 | serial_restart_input(ep); | ||
985 | } | ||
986 | |||
987 | static void serial_restart_output(int ep) | ||
988 | { | ||
989 | unsigned char *block; | ||
990 | int blen; | ||
991 | fifo_full_block(&serial_out_fifo, &block, &blen); | ||
992 | if (blen) | ||
993 | { | ||
994 | #ifdef LCD_DEBUG | ||
995 | char s[20]; | ||
996 | snprintf(s, sizeof(s), "o%03lx/%03x", block - serial_out_fifo.buf, | ||
997 | blen); | ||
998 | lcd_puts(0, 2, s); | ||
999 | lcd_update(); | ||
1000 | #endif | ||
1001 | usb_send_block(block, blen, serial_free_out_fifo, ep); | ||
1002 | } | ||
1003 | } | ||
1004 | |||
1005 | static void serial_free_out_fifo(int ep, unsigned char *buf, int len) | ||
1006 | { | ||
1007 | (void)buf; | ||
1008 | fifo_remove(&serial_out_fifo, len); | ||
1009 | serial_restart_output(ep); | ||
1010 | } | ||
1011 | |||
1012 | void usb_serial_handle(void) | ||
1013 | { | ||
1014 | #ifdef BUTTONS | ||
1015 | static int t = 0; | ||
1016 | |||
1017 | t++; | ||
1018 | if (t >= 1000) | ||
1019 | { | ||
1020 | int b; | ||
1021 | t = 0; | ||
1022 | yield(); | ||
1023 | b = button_get(false); | ||
1024 | if (b == BUTTON_PLAY) | ||
1025 | system_reboot(); | ||
1026 | else if (b & BUTTON_REL) | ||
1027 | usb_reconnect(); | ||
1028 | } | ||
1029 | #endif | ||
1030 | |||
1031 | |||
1032 | if (!usb_connect_state) | ||
1033 | { | ||
1034 | if (usb_connected()) | ||
1035 | { | ||
1036 | int i; | ||
1037 | GPIO3_SET = 4; | ||
1038 | (*(volatile unsigned long *)0x80005004) = 2; | ||
1039 | (*(volatile unsigned long *)0x80005008) = 0; | ||
1040 | for (i = 0; i < 100000; i++) | ||
1041 | nop_f(); | ||
1042 | usb_setup(0); | ||
1043 | usb_connect_state = 1; | ||
1044 | } | ||
1045 | } | ||
1046 | else | ||
1047 | { | ||
1048 | if (!usb_connected()) | ||
1049 | { | ||
1050 | usb_connect_state = 0; | ||
1051 | usb_cleanup(); | ||
1052 | } | ||
1053 | else | ||
1054 | { | ||
1055 | usb_handle_interrupts(); | ||
1056 | |||
1057 | if (usb_state == STATE_CONFIGURED) | ||
1058 | { | ||
1059 | if (endpoints[1].in_min_len < 0) | ||
1060 | serial_restart_input(1); | ||
1061 | if (!endpoints[1].out_in_progress) | ||
1062 | serial_restart_output(1); | ||
1063 | } | ||
1064 | } | ||
1065 | } | ||
1066 | } | ||
1067 | |||
1068 | |||
1069 | /* | ||
1070 | Not used: | ||
1071 | static int usb_serial_in_empty(void) | ||
1072 | { | ||
1073 | return fifo_empty(&serial_in_fifo); | ||
1074 | } | ||
1075 | */ | ||
1076 | |||
1077 | int usb_serial_get_byte(void) | ||
1078 | { | ||
1079 | while (fifo_empty(&serial_in_fifo)) | ||
1080 | usb_serial_handle(); | ||
1081 | return fifo_get_byte(&serial_in_fifo); | ||
1082 | } | ||
1083 | |||
1084 | /* | ||
1085 | Not used: | ||
1086 | static int usb_serial_out_full(void) | ||
1087 | { | ||
1088 | return fifo_full(&serial_out_fifo); | ||
1089 | } | ||
1090 | */ | ||
1091 | |||
1092 | void usb_serial_put_byte(int b) | ||
1093 | { | ||
1094 | while (fifo_full(&serial_out_fifo)) | ||
1095 | usb_serial_handle(); | ||
1096 | fifo_put_byte(&serial_out_fifo, b); | ||
1097 | usb_serial_handle(); | ||
1098 | } | ||
1099 | |||
1100 | void usb_serial_init(void) | ||
1101 | { | ||
1102 | fifo_init(&serial_in_fifo); | ||
1103 | fifo_init(&serial_out_fifo); | ||
1104 | usb_connect_state = 0; | ||
1105 | } | ||