LibXenon
Bare-metal Xbox 360 homebrew library
Loading...
Searching...
No Matches
ehci.c
Go to the documentation of this file.
1/* simplest usb-ehci driver which features:
2
3 control and bulk transfers only
4 only one transfer pending
5 driver is synchronous (waiting for the end of the transfer)
6 endianess independant
7 no uncached memory allocation needed
8
9 this driver is originally based on the GPL linux ehci-hcd driver
10
11 * Original Copyright (c) 2001 by David Brownell
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28
29
30/* magic numbers that can affect system performance */
31#define EHCI_TUNE_CERR 0 /* 0-3 qtd retries; 0 == don't stop */ /* by Hermes: i have replaced 3 by 0 and now it don�t hang when i extract the device*/
32#define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
33#define EHCI_TUNE_RL_TT 0
34#define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
35#define EHCI_TUNE_MULT_TT 1
36#define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
37
38static void
39dbg_qtd(struct ehci_hcd * ehci, const char *label, struct ehci_qtd *qtd) {
40 ehci_dbg("%s td %p n%08x %08x t%08x p0=%08x\n", label, qtd,
41 hc32_to_cpup(&qtd->hw_next),
44 hc32_to_cpup(&qtd->hw_buf [0]));
45 if (qtd->hw_buf [1])
46 ehci_dbg(" p1=%08x p2=%08x p3=%08x p4=%08x\n",
47 hc32_to_cpup(&qtd->hw_buf[1]),
48 hc32_to_cpup(&qtd->hw_buf[2]),
49 hc32_to_cpup(&qtd->hw_buf[3]),
50 hc32_to_cpup(&qtd->hw_buf[4]));
51}
52
53static void
54dbg_qh(struct ehci_hcd * ehci, const char *label, struct ehci_qh *qh) {
55 ehci_dbg("%s qh %p n%08x info %x %x qtd %x\n", label,
56 qh,
61 dbg_qtd(ehci, "overlay", (struct ehci_qtd *) &qh->hw_qtd_next);
62}
63
64static void
65dbg_command(struct ehci_hcd * ehci) {
66#ifdef DEBUG
68 u32 async = ehci_readl(&ehci->regs->async_next);
69
70 ehci_dbg("async_next: %08x\n", async);
72 "command %06x %s=%d ithresh=%d%s%s%s%s %s %s\n",
73 command,
74 (command & CMD_PARK) ? "park" : "(park)",
76 (command >> 16) & 0x3f,
77 (command & CMD_LRESET) ? " LReset" : "",
78 (command & CMD_IAAD) ? " IAAD" : "",
79 (command & CMD_ASE) ? " Async" : "",
80 (command & CMD_PSE) ? " Periodic" : "",
81 (command & CMD_RESET) ? " Reset" : "",
82 (command & CMD_RUN) ? "RUN" : "HALT"
83 );
84#endif
85}
86
87static void
88dbg_status(struct ehci_hcd * ehci) {
89#ifdef DEBUG
92 "status %04x%s%s%s%s%s%s%s%s%s%s\n",
93 status,
94 (status & STS_ASS) ? " Async" : "",
95 (status & STS_PSS) ? " Periodic" : "",
96 (status & STS_RECL) ? " Recl" : "",
97 (status & STS_HALT) ? " Halt" : "",
98 (status & STS_IAA) ? " IAA" : "",
99 (status & STS_FATAL) ? " FATAL" : "",
100 (status & STS_FLR) ? " FLR" : "",
101 (status & STS_PCD) ? " PCD" : "",
102 (status & STS_ERR) ? " ERR" : "",
103 (status & STS_INT) ? " INT" : ""
104 );
105#endif
106}
107
108void debug_qtds(struct ehci_hcd * ehci) {
109 struct ehci_qh *qh = ehci->async;
110 struct ehci_qtd *qtd;
111 dbg_qh(ehci, "qh", qh);
112 dbg_command(ehci);
113 dbg_status(ehci);
114 for (qtd = qh->qtd_head; qtd; qtd = qtd->next) {
115 ehci_dma_unmap_bidir(qtd->qtd_dma, sizeof (struct ehci_qtd));
116 dbg_qtd(ehci, "qtd", qtd);
117 ehci_dma_map_bidir(qtd, sizeof (struct ehci_qtd));
118 }
119
120}
121
122void dump_qh(struct ehci_hcd * ehci, struct ehci_qh *qh) {
123 struct ehci_qtd *qtd;
124 dbg_command(ehci);
125 dbg_status(ehci);
126 ehci_dma_unmap_bidir(qh->qh_dma, sizeof (struct ehci_qh));
127 dbg_qh(ehci, "qh", qh);
128 print_hex_dump_bytes("qh:", DUMP_PREFIX_OFFSET, (void*) qh, 12 * 4);
129 for (qtd = qh->qtd_head; qtd; qtd = qtd->next) {
130 u32 *buf;
131 ehci_dma_unmap_bidir(qtd->qtd_dma, sizeof (struct ehci_qtd));
132 dbg_qtd(ehci, "qtd", qtd);
133 print_hex_dump_bytes("qtd:", DUMP_PREFIX_OFFSET, (void*) qtd, 8 * 4);
134 buf = (u32*) hc32_to_cpu(qtd->hw_buf[0]);
135 if (buf)
136 print_hex_dump_bytes("qtd buf:", DUMP_PREFIX_OFFSET, (void*) (buf), 8 * 4);
137
138 }
139}
140
141/*-------------------------------------------------------------------------*/
142
143/*
144 * handshake - spin reading hc until handshake completes or fails
145 * @ptr: address of hc register to be read
146 * @mask: bits to look at in result of read
147 * @done: value of those bits when handshake succeeds
148 * @usec: timeout in microseconds
149 *
150 * Returns negative errno, or zero on success
151 *
152 * Success happens when the "mask" bits have the specified value (hardware
153 * handshake done). There are two failure modes: "usec" have passed (major
154 * hardware flakeout), or the register reads as all-ones (hardware removed).
155 *
156 * That last failure should_only happen in cases like physical cardbus eject
157 * before driver shutdown. But it also seems to be caused by bugs in cardbus
158 * bridge shutdown: shutting down the bridge before the devices using it.
159 */
161
162#define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
163
164
165
167
168static int handshake(struct ehci_hcd * ehci, void __iomem *pstatus, void __iomem *ptr,
169 u32 mask, u32 done, int usec) {
170
171 u32 result, status, g_status, g_mstatus, ret = 0;
172
173 u32 tmr, temp;
174
175 tmr = get_timer();
176 usec <<= 1;
177
178 do {
179 ehci_usleep(10); /* Hermes: 100 microseconds is a good time to response and better for multithread (i think).
180 the new timer uses syscalls and queues to release the thread focus */
181 //usec-=100;
182
183 status = ehci_readl(pstatus);
184 result = ehci_readl(ptr);
185 g_status = ehci_readl(&ehci->regs->status);
186 g_mstatus = g_status & INTR_MASK;
187
188// printf("sts %08x %08x %08x\n", status, result, g_status);
189
190 if ((g_status == ~(u32) 0) || (PORT_OWNER & status) || (g_status & STS_FATAL)) /* card removed */ {
191 unplug_device = 1;
192 return -ENODEV;
193 }
194
195
196 if (/*(g_status == ~(u32)0) || */ !(PORT_CONNECT & status)) /* card removed */ {
197 unplug_device = 1;
198 ret = -ENODEV;
199 goto handshake_exit;
200 }
201 result &= mask;
202
203
204 if (g_status & STS_ERR) {
205 if (handshake_mode) ret = -ETIMEDOUT;
206 else {
207 unplug_device = 1;
208 ret = -ENODEV;
209 }
210 goto handshake_exit;
211
212 }
213
214 if (result == done) {
215 ret = 0;
216 goto handshake_exit;
217 }
218 temp = get_timer() - tmr;
219 } while (temp < usec/*usec > 0*/);
220
221
222
223 if (handshake_mode) ret = -ETIMEDOUT;
224 else {
225 unplug_device = 1;
226 ret = -ENODEV; /* Hermes: with ENODEV works the unplugin method receiving datas (fatal error)
227 ENODEV return without retries and unplug_device can works without interferences.
228 i think is no a good idea too much retries when is possible the device needs one drastic action
229 */
230 }
231handshake_exit:
232
233 ehci_writel(g_mstatus, &ehci->regs->status);
234 /* complete the unlinking of some qh [4.15.2.3] */
236 if (g_status & STS_IAA) {
237 /* guard against (alleged) silicon errata */
238 if (command & CMD_IAAD) {
240 &ehci->regs->command);
241
242 }
243 }
244
245 return ret;
246}
247
248#include "ehci-mem.c"
249
250/* one-time init, only for memory state */
251static int ehci_init(struct ehci_hcd * ehci, int default_ctrlr) {
252 int retval;
253
254 if ((retval = ehci_mem_init(ehci)) < 0)
255 return retval;
256
257 /*
258 * dedicate a qh for the async ring head, since we couldn't unlink
259 * a 'real' qh without stopping the async schedule [4.8]. use it
260 * as the 'reclamation list head' too.
261 * its dummy is used in hw_alt_next of many tds, to prevent the qh
262 * from automatically advancing to the next td after short reads.
263 */
268 ehci->async->hw_alt_next = EHCI_LIST_END(); //QTD_NEXT( ehci->async->dummy->qtd_dma);
269
271 ehci->command = 0;
272
273 ehci_writel( 0x000000000, &ehci->regs->intr_enable);
278
279 return 0;
280}
281
282/* fill a qtd, returning how much of the buffer we were able to queue up */
283static int
284qtd_fill(struct ehci_qtd *qtd, dma_addr_t buf,
285 size_t len, int token, int maxpacket) {
286 int i, count;
287 u64 addr = buf;
288 //ehci_dbg("fill qtd with dma %X len %X\n",buf,len);
289 /* one buffer entry per 4K ... first might be short or unaligned */
290 qtd->hw_buf[0] = cpu_to_hc32((u32) addr);
291 qtd->hw_buf_hi[0] = 0;
292 count = 0x1000 - (buf & 0x0fff); /* rest of that page */
293 if (likely(len < count)) /* ... iff needed */
294 count = len;
295 else {
296 buf += 0x1000;
297 buf &= ~0x0fff;
298
299 /* per-qtd limit: from 16K to 20K (best alignment) */
300 for (i = 1; count < len && i < 5; i++) {
301 addr = buf;
302 qtd->hw_buf[i] = cpu_to_hc32((u32) addr);
303 qtd->hw_buf_hi[i] = cpu_to_hc32(
304 (u32) (addr >> 32));
305 buf += 0x1000;
306 if ((count + 0x1000) < len)
307 count += 0x1000;
308 else
309 count = len;
310 }
311
312 /* short packets may only terminate transfers */
313 if (count != len)
314 count -= (count % maxpacket);
315 }
316 qtd->hw_token = cpu_to_hc32((count << 16) | token);
317 qtd->length = count;
318
319 return count;
320}
321
322// high bandwidth multiplier, as encoded in highspeed endpoint descriptors
323#define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
324// ... and packet size, for any kind of endpoint descriptor
325#define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
326
327/*
328 * reverse of qh_urb_transaction: free a list of TDs.
329 * also count the actual transfer length.
330 *
331 */
332static void qh_end_transfer(struct ehci_hcd * ehci) {
333 struct ehci_qtd *qtd;
334 struct ehci_qh *qh = ehci->asyncqh;
335 u32 token;
336 int error = 0;
337 for (qtd = qh->qtd_head; qtd; qtd = qtd->next) {
338 token = hc32_to_cpu(qtd->hw_token);
339 if (likely(QTD_PID(token) != 2))
340 qtd->urb->actual_length += qtd->length - QTD_LENGTH(token);
341 if (!(qtd->length == 0 && ((token & 0xff) == QTD_STS_HALT)) &&
342 (token & QTD_STS_HALT)) {
343 ehci_dbg("\nqtd error!:");
344 if (token & QTD_STS_BABBLE) {
345 ehci_dbg(" BABBLE");
346 }
347 if (token & QTD_STS_MMF) {
348 /* fs/ls interrupt xfer missed the complete-split */
349 ehci_dbg(" missed micro frame");
350 }
351 if (token & QTD_STS_DBE) {
352 ehci_dbg(" databuffer error");
353 }
354 if (token & QTD_STS_XACT) {
355 ehci_dbg(" wrong ack");
356 }
357 if (QTD_CERR(token) == 0)
358 ehci_dbg(" toomany errors");
359 ehci_dbg("\n");
360 error = -1;
361 }
362 }
363 if (error) {
364 //dump_qh(ehci->asyncqh);
365 qtd->urb->actual_length = error;
366 }
367 ehci->qtd_used = 0;
368}
369
370/*
371 * create a list of filled qtds for this URB; won't link into qh.
372 */
374 struct ehci_hcd * ehci, struct ehci_urb *urb
375 ) {
376 struct ehci_qtd *qtd, *qtd_prev;
377 struct ehci_qtd *head;
378 dma_addr_t buf;
379 int len, maxpacket;
380 int is_input;
381 u32 token;
382
383 /*
384 * URBs map to sequences of QTDs: one logical transaction
385 */
386 head = qtd = ehci_qtd_alloc(ehci);
387
388 if (!head) return NULL;
389
390 qtd->urb = urb;
391
392 urb->actual_length = 0;
393 token = QTD_STS_ACTIVE;
394 token |= (EHCI_TUNE_CERR << 10);
395 /* for split transactions, SplitXState initialized to zero */
396
398 is_input = urb->input;
399 if (urb->ep == 0) {/* is control */
400 /* SETUP pid */
401 qtd_fill(qtd, urb->setup_dma,
402 sizeof (usbctrlrequest),
403 token | (2 /* "setup" */ << 8), 8);
404
405 /* ... and always at least one more pid */
406 token ^= QTD_TOGGLE;
407 qtd_prev = qtd;
408 qtd = ehci_qtd_alloc(ehci);
409 if (!qtd) goto cleanup;
410 qtd->urb = urb;
411 qtd_prev->hw_next = QTD_NEXT(qtd->qtd_dma);
412 qtd_prev->next = qtd;
413
414 /* for zero length DATA stages, STATUS is always IN */
415 if (len == 0)
416 token |= (1 /* "in" */ << 8);
417 }
418
419 /*
420 * data transfer stage: buffer setup
421 */
422 buf = urb->transfer_dma;
423
424 if (is_input)
425 token |= (1 /* "in" */ << 8);
426 /* else it's already initted to "out" pid (0 << 8) */
427
428 maxpacket = max_packet(urb->maxpacket);
429
430 /*
431 * buffer gets wrapped in one or more qtds;
432 * last one may be "short" (including zero len)
433 * and may serve as a control status ack
434 */
435 for (;;) {
436 int this_qtd_len;
437
438 this_qtd_len = qtd_fill(qtd, buf, len, token, maxpacket);
439 len -= this_qtd_len;
440 buf += this_qtd_len;
441
442 /*
443 * short reads advance to a "magic" dummy instead of the next
444 * qtd ... that forces the queue to stop, for manual cleanup.
445 * (this will usually be overridden later.)
446 */
447 if (is_input)
449
450 /* qh makes control packets use qtd toggle; maybe switch it */
451 if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
452 token ^= QTD_TOGGLE;
453
454 if (likely(len <= 0))
455 break;
456
457 qtd_prev = qtd;
458 qtd = ehci_qtd_alloc(ehci);
459 if (!qtd) goto cleanup;
460 qtd->urb = urb;
461 qtd_prev->hw_next = QTD_NEXT(qtd->qtd_dma);
462 qtd_prev->next = qtd;
463 }
464
465 qtd->hw_alt_next = EHCI_LIST_END();
466
467 /*
468 * control requests may need a terminating data "status" ack;
469 * bulk ones may need a terminating short packet (zero length).
470 */
471 if (likely(urb->transfer_buffer_length != 0)) {
472 int one_more = 0;
473
474 if (urb->ep == 0) {
475 one_more = 1;
476 token ^= 0x0100; /* "in" <--> "out" */
477 token |= QTD_TOGGLE; /* force DATA1 */
478 } else if (!(urb->transfer_buffer_length % maxpacket)) {
479 //one_more = 1;
480 }
481 if (one_more) {
482 qtd_prev = qtd;
483 qtd = ehci_qtd_alloc(ehci);
484 if (!qtd) goto cleanup;
485 qtd->urb = urb;
486 qtd_prev->hw_next = QTD_NEXT(qtd->qtd_dma);
487 qtd_prev->next = qtd;
488
489 /* never any data in such packets */
490 qtd_fill(qtd, 0, 0, token, 0);
491 }
492 }
493
494 /* by default, enable interrupt on urb completion */
496 return head;
497
498cleanup:
499 return NULL;
500}
501
502u32 usb_timeout = 1000 * 1000;
503
505 struct ehci_hcd * ehci, struct ehci_device *dev,
506 struct ehci_urb *urb) {
507 struct ehci_qh *qh;
508 struct ehci_qtd *qtd;
509 u32 info1 = 0, info2 = 0;
510 // int is_input;
511 int maxp = 0;
512 int retval = 0;
513
514 //writel (INTR_MASK, &ehci->regs->intr_enable); // try interrupt
515
516 //ehci_dbg("do urb %X %X ep %X\n", urb->setup_buffer, urb->transfer_buffer, urb->ep);
517
518 if (urb->ep == 0) //control message
520
522 if (urb->input)
524 else
526 }
527 qh = ehci->asyncqh;
528 memset(qh, 0, 12 * 4);
530
531 if (!qtd) {
532 ehci->qtd_used = 0;
533 return -ENOMEM;
534 }
535
536 qh ->qtd_head = qtd;
537
538 info1 |= ((urb->ep)&0xf) << 8;
539 info1 |= dev->id;
540 // is_input = urb->input;
541 maxp = urb->maxpacket;
542
543 info1 |= (2 << 12); /* EPS "high" */
544 if (urb->ep == 0)// control
545 {
546 info1 |= (EHCI_TUNE_RL_HS << 28);
547 info1 |= 64 << 16; /* usb2 fixed maxpacket */
548 info1 |= 1 << 14; /* toggle from qtd */
549 info2 |= (EHCI_TUNE_MULT_HS << 30);
550 } else//bulk
551 {
552 info1 |= (EHCI_TUNE_RL_HS << 28);
553 /* The USB spec says that high speed bulk endpoints
554 * always use 512 byte maxpacket. But some device
555 * vendors decided to ignore that, and MSFT is happy
556 * to help them do so. So now people expect to use
557 * such nonconformant devices with Linux too; sigh.
558 */
559 info1 |= max_packet(maxp) << 16;
560 info2 |= (EHCI_TUNE_MULT_HS << 30);
561
562 }
563 //ehci_dbg("HW info: %08X\n",info1);
564 qh->hw_info1 = cpu_to_hc32(info1);
565 qh->hw_info2 = cpu_to_hc32(info2);
566
567 qh->hw_next = QH_NEXT(qh->qh_dma);
568 qh->hw_qtd_next = QTD_NEXT(qtd->qtd_dma);
570
571 if (urb->ep != 0) {
572 if (get_toggle(dev, urb->ep))
574 else
575 qh->hw_token &= ~cpu_to_hc32(QTD_TOGGLE);
576 ehci_dbg("toggle for ep %x: %d %x\n",urb->ep,get_toggle(dev,urb->ep),qh->hw_token);
577 }
578
580
582
583
584 ehci_dma_map_bidir(qh, sizeof (struct ehci_qh));
585 for (qtd = qh->qtd_head; qtd; qtd = qtd->next)
586 ehci_dma_map_bidir(qtd, sizeof (struct ehci_qtd));
587#if 0
588 if (urb->ep != 0) {
591 }
592#endif
593 // start (link qh)
595 ehci_dma_map_bidir(ehci->async, sizeof (struct ehci_qh));
596
597 retval = handshake/*_interrupt*/(ehci, &ehci->regs->port_status[dev->port], &ehci->regs->status, STS_INT, STS_INT, usb_timeout);
598
599 ehci_dbg("urb res %d %d %d %d\n", retval, usb_timeout, handshake_mode,dev->port);
600
601 //print_hex_dump_bytes ("qh mem",0,(void*)qh,17*4);
602 //retval = poll_transfer_end(1000*1000);
603 ehci_dma_unmap_bidir(ehci->async->qh_dma, sizeof (struct ehci_qh));
604 ehci_dma_unmap_bidir(qh->qh_dma, sizeof (struct ehci_qh));
605 for (qtd = qh->qtd_head; qtd; qtd = qtd->next)
606 ehci_dma_unmap_bidir(qtd->qtd_dma, sizeof (struct ehci_qtd));
607
608 // stop (unlink qh)
610 ehci_dma_map_bidir(ehci->async, sizeof (struct ehci_qh));
611 ehci_dma_unmap_bidir(ehci->async->qh_dma, sizeof (struct ehci_qh));
612
613
614 // ack
615 //ehci_writel( STS_RECL|STS_IAA|STS_INT, &ehci->regs->status);
616
617
618 if (urb->ep != 0) {
619 set_toggle(dev, urb->ep, (qh->hw_token & cpu_to_hc32(QTD_TOGGLE)) ? 1 : 0);
620 //ehci_dbg("toggle for ep %x: %d %d %x %X\n",urb->ep,get_toggle(dev,urb->ep),(qh->hw_token & cpu_to_hc32(QTD_TOGGLE)),qh->hw_token,dev->toggles);
621 }
622
623 if (retval >= 0)
624 // wait hc really stopped
625 retval = handshake(ehci, &ehci->regs->port_status[dev->port], &ehci->regs->async_next, ~0, ehci->async->qh_dma, 20 * 1000);
626 //release memory, and actualise urb->actual_length
627 qh_end_transfer(ehci);
628
629 //writel (0, &ehci->regs->intr_enable); // try interrupt
630
631
633 if (urb->input)
635 else
637 }
638 if (urb->ep == 0) //control message
640
641
642 ehci_dbg("urb res end %d\n", retval);
643
644
645 if (retval == 0) {
646 return urb->actual_length;
647 }
648 return retval;
649}
650
651s32 ehci_control_message(struct ehci_hcd * ehci, struct ehci_device *dev, u8 bmRequestType, u8 bmRequest, u16 wValue, u16 wIndex, u16 wLength, void *buf) {
652 struct ehci_urb urb;
654 ehci_dbg("control msg: rt%02X r%02X v%04X i%04X s%04x %p\n", bmRequestType, bmRequest, wValue, wIndex, wLength, buf);
655 req->bRequestType = bmRequestType;
656 req->bRequest = bmRequest;
657 req->wValue = bswap_16(wValue);
658 req->wIndex = bswap_16(wIndex);
659 req->wLength = bswap_16(wLength);
660 urb.setup_buffer = req;
661 urb.ep = 0;
662 urb.input = (bmRequestType & USB_CTRLTYPE_DIR_DEVICE2HOST) != 0;
663 urb.maxpacket = 64;
664 urb.transfer_buffer_length = wLength;
665/* if (((u32)buf) > 0x13880000){// HW cannot access this buffer, we allow this for convenience
666 int ret;
667 urb.transfer_buffer = USB_Alloc(wLength);
668 ehci_dbg("alloc another buffer %p %p\n",buf,urb.transfer_buffer);
669 memcpy(urb.transfer_buffer,buf,wLength);
670 ret = ehci_do_urb(ehci, dev,&urb);
671 memcpy(buf,urb.transfer_buffer,wLength);
672 USB_Free(urb.transfer_buffer);
673 return ret;
674 }
675 else*/
676 {
677 urb.transfer_buffer = buf;
678 return ehci_do_urb(ehci, dev, &urb);
679 }
680}
681
682s32 ehci_bulk_message(struct ehci_hcd * ehci, struct ehci_device *dev, u8 bEndpoint, u16 wLength, void *rpData) {
683 struct ehci_urb urb;
684 s32 ret;
686 urb.ep = bEndpoint;
687 urb.input = (bEndpoint & 0x80) != 0;
688 urb.maxpacket = 512;
689 urb.transfer_buffer_length = wLength;
690 urb.transfer_buffer = rpData;
691 ehci_dbg("bulk msg: ep:%02X size:%02X addr:%04X", bEndpoint, wLength, rpData);
692 ret = ehci_do_urb(ehci, dev, &urb);
693 ehci_dbg("==>%d\n", ret);
694 return ret;
695}
696
697int ehci_reset_port_old(struct ehci_hcd * ehci, int port) {
698 u32 __iomem *status_reg = &ehci->regs->port_status[port];
699 struct ehci_device *dev = &ehci->devices[port];
700 u32 status = ehci_readl(status_reg);
701 int retval = 0, i, f;
702 dev->id = 0;
703
704
705 if ((PORT_OWNER & status) || !(PORT_CONNECT & status)) {
706 ehci_writel(PORT_OWNER, status_reg);
707 ehci_dbg("port %d had no usb2 device connected at startup %X \n", port, ehci_readl(status_reg));
708 return -ENODEV; // no USB2 device connected
709 }
710 ehci_dbg("port %d has usb2 device connected! reset it...\n", port);
711
712 f = handshake_mode;
713 handshake_mode = 1;
714
715 for (i = 0; i < 4; i++) //4 retries
716 {
717 u32 status = ehci_readl(status_reg);
718 status &= ~PORT_PE;
720 ehci_writel(status, status_reg);
721 ehci_msleep(60); // wait 60ms for the reset sequence
722 status = ehci_readl(status_reg);
723 status &= ~(PORT_RWC_BITS | PORT_RESET); /* force reset to complete */
724 ehci_writel(status, status_reg);
725 ehci_msleep(100);
726 status = ehci_readl(status_reg);
727 if ((PORT_OWNER & status) || !(PORT_CONNECT & status) || !(status & PORT_PE) || !(status & PORT_POWER)) {
728 /*ehci_writel( 0x1803,status_reg);
729 ehci_msleep(100);
730 ehci_writel( 0x1903,status_reg);
731 ehci_msleep(100);// wait 50ms for the reset sequence
732 ehci_writel( 0x1001,status_reg);
733 ehci_msleep(50);*/
734 retval = -1;
735 continue;
736 }
737 //ehci_writel( PORT_OWNER|PORT_POWER|PORT_RESET,status_reg);
738
739
740 //ehci_writel( ehci_readl(status_reg)& (~PORT_RESET),status_reg);
741 retval = handshake(ehci, status_reg, status_reg,
742 PORT_RESET, 0, 750);
743 if (retval == 0) {
744 /* ehci_dbg ( "port %d reset error %d\n",
745 port, retval);*/
746
747 ehci_dbg("port %d reseted status:%04x...\n", port, ehci_readl(status_reg));
748 ehci_msleep(100);
749 // now the device has the default device id
751 USB_REQ_GETDESCRIPTOR, USB_DT_DEVICE << 8, 0, sizeof (dev->desc), &dev->desc);
752
753 if (retval >= 0) {
754
755
757 USB_REQ_SETADDRESS, port + 1, 0, 0, 0);
758
759 if (retval >= 0) break;
760 }
761 }
762 }
763
764 handshake_mode = f;
765
766 if (retval < 0) {
767
768 return retval;
769 }
770
771 dev->toggles = 0;
772
773 dev->id = port + 1;
774 ehci_dbg("device %d: %X %X...\n", dev->id, le16_to_cpu(dev->desc.idVendor), le16_to_cpu(dev->desc.idProduct));
775 return retval;
776}
777
778void ehci_adquire_port(struct ehci_hcd * ehci, int port) {
779 u32 __iomem *status_reg = &ehci->regs->port_status[port];
780 u32 status = ehci_readl(status_reg);
781
782 //change owner, port disabled
785 ehci_writel(status, status_reg);
786 ehci_msleep(5);
787 status = ehci_readl(status_reg);
790 ehci_writel(status, status_reg);
791 ehci_msleep(5);
792
793
794 //enable port
795 ehci_writel(0x1001, status_reg);
796 ehci_msleep(5);
797}
798
800 u32 __iomem *status_reg = &ehci->regs->port_status[port];
801 u32 status = ehci_readl(status_reg);
802
803 int i, f, retval = 0;
804
805 ehci_dbg("ehci_reset_usb_port\n");
806 dbg_status(ehci);
807
808 if ((PORT_OWNER & status) || !(PORT_CONNECT & status)) {
809
811
812 ehci_msleep(100);
813
814 status = ehci_readl(status_reg);
815
816// ehci_writel(PORT_OWNER, status_reg);
817// return -ENODEV; // no USB2 device connected
818 }
819
820 f = handshake_mode;
821 handshake_mode = 1;
822
823 for (i = 0; i < 4; i++) //4 retries
824 {
825 status &= ~PORT_PE;
827 ehci_writel(status, status_reg);
828 ehci_msleep(60); // wait 60ms for the reset sequence
829 status = ehci_readl(status_reg);
830 status &= ~(PORT_RWC_BITS | PORT_RESET); /* force reset to complete */
831 ehci_writel(status, status_reg);
832 ehci_msleep(50);
833 retval = handshake(ehci, status_reg, status_reg,
834 PORT_RESET, 0, 5*1000);
835
836 ehci_dbg("ehci_reset_usb_port handshake retval %d\n",retval);
837
838 if (retval != 0) {
839 status = ehci_readl(status_reg);
840 handshake_mode = f;
841 return -2000;
842 }
843 status = ehci_readl(status_reg);
844 if (status & PORT_PE) break; //port enabled
845 }
846 dbg_status(ehci);
847 handshake_mode = f;
848 if (!(status & PORT_PE)) {
849 printf("EHCI bus %d port %d: low speed, releasing to OHCI\n",ehci->bus_id,port+1);
850
851 // that means is low speed device so release
853 status &= ~PORT_RWC_BITS;
854 ehci_writel(status, status_reg);
855 ehci_msleep(10);
856 status = ehci_readl(status_reg);
857 }
858 dbg_status(ehci);
859 return retval;
860}
861
862int ehci_init_port(struct ehci_hcd * ehci, int port) {
863 struct ehci_device *dev = &ehci->devices[port];
864 int retval = 0;
865 dev->id = 0;
866 int i;
867 ehci_msleep(50);
868 for (i = 0; i < 3; i++) {
869 // now the device has the default device id
871 USB_REQ_GETDESCRIPTOR, USB_DT_DEVICE << 8, 0, sizeof (dev->desc), &dev->desc);
872 //retval=-1;
873 if (retval < 0) {
874 ehci_dbg("unable to get device desc (1)...\n");
875 retval = -2201;
876 ehci_msleep(100);
877 //return retval;
878 } else break;
879 }
880
881#if 0
882 if (retval < 0) {
883 for (i = 0; i < 3; i++) {
884
886 ehci_msleep(100);
887 // now the device has the default device id
889 USB_REQ_GETDESCRIPTOR, USB_DT_DEVICE << 8, 0, sizeof (dev->desc), &dev->desc);
890 if (retval < 0) {
891 ehci_dbg("unable to get device desc (2)...\n");
892 retval = -2201;
893
894 //return retval;
895 } else break;
896 }
897 }
898
899 if (retval < 0) {
900 for (i = 0; i < 3; i++) {
902 ehci_msleep(100);
904 ehci_msleep(100);
905 // now the device has the default device id
907 USB_REQ_GETDESCRIPTOR, USB_DT_DEVICE << 8, 0, sizeof (dev->desc), &dev->desc);
908 if (retval < 0) {
909 ehci_dbg("unable to get device desc (3)...\n");
910 retval = -2201;
911
912 //return retval;
913 } else break;
914 }
915 }
916#endif
917
918 if (retval < 0) return -2201;
919
920 int cnt = 0;
921 do {
922 ehci_msleep(50);
924 USB_REQ_SETADDRESS, port+1, 0, 0, 0);
925 if (retval < 0) {
926 ehci_dbg("unable to set device addr...\n");
927 retval = -8000 - cnt;
928 //return retval;
929 cnt++;
930 }
931
932 dev->toggles = 0;
933
934 dev->id = port+1;
935
936 if(retval>=0) break;
937
938 USB_ClearHalt(ehci, dev, 0);
939 //USB_ClearHalt(dev, 0x80);
940 ehci_msleep(50);
941
943 USB_REQ_GETDESCRIPTOR, USB_DT_DEVICE << 8, 0, sizeof (dev->desc), &dev->desc);
944
945
946 if (retval < 0) {
947 ehci_dbg("unable to get device desc...\n");
948 retval = -2242;
949 dev->id = 0;
950 //return retval;
951 }
952 else {retval=-1;continue;}
953
954 } while (retval < 0 && cnt < 5);
955
956 return retval;
957}
958
959int ehci_reset_port(struct ehci_hcd * ehci, int port) {
960 int retval;
961 retval = ehci_reset_usb_port(ehci, port);
962 if (retval >= 0) retval = ehci_init_port(ehci, port);
963 return retval;
964}
965
966int ehci_reset_port2(struct ehci_hcd * ehci, int port) {
967 u32 __iomem *status_reg = &ehci->regs->port_status[port];
968 int ret = ehci_reset_port_old(ehci, port);
969 if (ret < 0/*==-ENODEV || ret==-ETIMEDOUT*/) {
970 u32 status = ehci_readl(status_reg);
971 if (status & 1) {
972 ehci_msleep(500); // power off
973 ehci_writel(0x1001, status_reg); // power on
974 }
975 }
976 return ret;
977}
978
979int ehci_reset_device(struct ehci_hcd * ehci, struct ehci_device *dev) {
980 return ehci_reset_port(ehci, dev->port);
981}
982#include "usbstorage.h"
983
985 int i;
986 // precondition: the ehci should be halted
987 for (i = 0; i < ehci->num_port; i++) {
988 struct ehci_device *dev = &ehci->devices[i];
989 dev->port = i;
991 }
992 return 0;
993}
994
995/* wii: quickly release non ehci or not connected ports,
996 as we can't kick OHCI drivers laters if we discover a device for them.
997 */
999 int i;
1000 u32 __iomem *status_reg = &ehci->regs->port_status[0];
1001 while (ehci_readl(status_reg) == 0x1000) ehci_usleep(100); // wait port 0 to init
1002
1003 ehci_msleep(100); // wait another msec..
1004 for (i = 0; i < ehci->num_port; i++) {
1005 status_reg = &ehci->regs->port_status[i];
1006 u32 status = ehci_readl(status_reg);
1007 if (i == 1 || i == 2 || !(PORT_CONNECT & status) || PORT_USB11(status))
1008 ehci_writel(PORT_OWNER, status_reg); // release port.
1009 }
1010 return 0;
1011}
1012
1013int ehci_open_device(struct ehci_hcd * ehci, int vid, int pid, int fd) {
1014 int i;
1015 for (i = 0; i < ehci->num_port; i++) {
1016 //ehci_dbg("try device: %d\n",i);
1017 if (ehci->devices[i].fd == 0 &&
1018 le16_to_cpu(ehci->devices[i].desc.idVendor) == vid &&
1020 //ehci_dbg("found device: %x %x\n",vid,pid);
1021 ehci->devices[i].fd = fd;
1022 return fd;
1023 }
1024 }
1025 return -6;
1026}
1027
1029 if (dev)
1030 dev->fd = 0;
1031 return 0;
1032}
1033
1034void * ehci_fd_to_dev(struct ehci_hcd * ehci, int fd) {
1035 int i;
1036 for (i = 0; i < ehci->num_port; i++) {
1037 struct ehci_device *dev = &ehci->devices[i];
1038 //ehci_dbg ( "device %d:fd:%d %X %X...\n", dev->id,dev->fd,le16_to_cpu(dev->desc.idVendor),le16_to_cpu(dev->desc.idProduct));
1039 if (dev->fd == fd) {
1040 return dev;
1041 }
1042 }
1043 ehci_dbg("unkown fd! %d\n", fd);
1044 return 0;
1045}
1046#define g_ehci #error
1047
1048int ehci_get_device_list(struct ehci_hcd * ehci, u8 maxdev, u8 b0, u8*num, u16*buf) {
1049 int i, j = 0;
1050 for (i = 0; i < ehci->num_port && j < maxdev; i++) {
1051 struct ehci_device *dev = &ehci->devices[i];
1052 if (dev->id != 0) {
1053 ehci_dbg ( "device %d: %X %X...\n", dev->id,le16_to_cpu(dev->desc.idVendor),le16_to_cpu(dev->desc.idProduct));
1054 buf[j * 4] = 0;
1055 buf[j * 4 + 1] = 0;
1056 buf[j * 4 + 2] = le16_to_cpu(dev->desc.idVendor);
1057 buf[j * 4 + 3] = le16_to_cpu(dev->desc.idProduct);
1058 j++;
1059 }
1060 }
1061 ehci_dbg("found %d devices\n",j);
1062 *num = j;
1063 return 0;
1064}
1065
1066#include "usb2.c"
1067#include "usbstorage.c"
#define NULL
Definition: def.h:47
int ehci_mem_init(struct ehci_hcd *ehci)
Definition: ehci-mem.c:42
int ehci_release_ports(struct ehci_hcd *ehci)
Definition: ehci.c:998
s32 ehci_control_message(struct ehci_hcd *ehci, struct ehci_device *dev, u8 bmRequestType, u8 bmRequest, u16 wValue, u16 wIndex, u16 wLength, void *buf)
Definition: ehci.c:651
void ehci_adquire_port(struct ehci_hcd *ehci, int port)
Definition: ehci.c:778
int ehci_do_urb(struct ehci_hcd *ehci, struct ehci_device *dev, struct ehci_urb *urb)
Definition: ehci.c:504
void dump_qh(struct ehci_hcd *ehci, struct ehci_qh *qh)
Definition: ehci.c:122
int ehci_open_device(struct ehci_hcd *ehci, int vid, int pid, int fd)
Definition: ehci.c:1013
void * ehci_fd_to_dev(struct ehci_hcd *ehci, int fd)
Definition: ehci.c:1034
int ehci_close_device(struct ehci_device *dev)
Definition: ehci.c:1028
int ehci_reset_device(struct ehci_hcd *ehci, struct ehci_device *dev)
Definition: ehci.c:979
int ehci_reset_port_old(struct ehci_hcd *ehci, int port)
Definition: ehci.c:697
#define EHCI_TUNE_RL_HS
Definition: ehci.c:32
int ehci_reset_port(struct ehci_hcd *ehci, int port)
Definition: ehci.c:959
int unplug_device
Definition: ehci.c:160
int ehci_init_port(struct ehci_hcd *ehci, int port)
Definition: ehci.c:862
struct ehci_qtd * qh_urb_transaction(struct ehci_hcd *ehci, struct ehci_urb *urb)
Definition: ehci.c:373
#define EHCI_TUNE_MULT_HS
Definition: ehci.c:34
int ehci_reset_usb_port(struct ehci_hcd *ehci, int port)
Definition: ehci.c:799
int ehci_reset_port2(struct ehci_hcd *ehci, int port)
Definition: ehci.c:966
#define EHCI_TUNE_CERR
Definition: ehci.c:31
int ehci_get_device_list(struct ehci_hcd *ehci, u8 maxdev, u8 b0, u8 *num, u16 *buf)
Definition: ehci.c:1048
u32 usb_timeout
Definition: ehci.c:502
#define max_packet(wMaxPacketSize)
Definition: ehci.c:325
int ehci_discover(struct ehci_hcd *ehci)
Definition: ehci.c:984
void debug_qtds(struct ehci_hcd *ehci)
Definition: ehci.c:108
#define INTR_MASK
Definition: ehci.c:162
int handshake_mode
Definition: ehci.c:166
s32 ehci_bulk_message(struct ehci_hcd *ehci, struct ehci_device *dev, u8 bEndpoint, u16 wLength, void *rpData)
Definition: ehci.c:682
#define cpu_to_hc32(b)
Definition: ehci.h:244
#define QTD_STS_HALT
Definition: ehci.h:10
struct ehci_urb * urb
Definition: ehci.h:28
dma_addr_t ehci_dma_map_bidir(void *buf, size_t len)
Definition: usb_os.c:84
#define hc32_to_cpup(b)
Definition: ehci.h:246
#define QTD_STS_ACTIVE
Definition: ehci.h:9
#define EHCI_LIST_END()
Definition: ehci.h:179
dma_addr_t ehci_dma_map_to(void *buf, size_t len)
Definition: usb_os.c:72
#define QTD_LENGTH(tok)
Definition: ehci.h:5
void ehci_dma_unmap_bidir(dma_addr_t buf, size_t len)
Definition: usb_os.c:100
#define set_toggle(dev, ep, v)
Definition: ehci.h:59
#define hc32_to_cpu(b)
Definition: ehci.h:245
#define QTD_STS_DBE
Definition: ehci.h:11
#define QTD_NEXT(dma)
Definition: ehci.h:109
dma_addr_t ehci_dma_map_from(void *buf, size_t len)
Definition: usb_os.c:78
#define QH_HEAD
Definition: ehci.h:3
#define QTD_STS_XACT
Definition: ehci.h:13
#define QTD_STS_BABBLE
Definition: ehci.h:12
#define QTD_STS_MMF
Definition: ehci.h:14
#define QTD_PID(tok)
Definition: ehci.h:8
#define QTD_CERR(tok)
Definition: ehci.h:7
void ehci_msleep(int time)
Definition: usb_os.c:110
#define QTD_TOGGLE
Definition: ehci.h:4
void ehci_usleep(int time)
Definition: usb_os.c:105
void ehci_dma_unmap_from(dma_addr_t buf, size_t len)
Definition: usb_os.c:95
#define get_toggle(dev, ep)
Definition: ehci.h:58
#define QH_NEXT(dma)
Definition: ehci.h:176
struct ehci_hcd * ehci
Definition: ehci.h:23
#define QTD_IOC
Definition: ehci.h:6
#define QTD_STS_PING
Definition: ehci.h:16
void ehci_dma_unmap_to(dma_addr_t buf, size_t len)
Definition: usb_os.c:90
u32 status
Definition: ehci_defs.h:15
#define PORT_POWER
Definition: ehci_defs.h:59
#define PORT_RWC_BITS
Definition: ehci_defs.h:72
#define STS_ERR
Definition: ehci_defs.h:26
#define CMD_IAAD
Definition: ehci_defs.h:7
#define PORT_RESET
Definition: ehci_defs.h:63
#define CMD_PSE
Definition: ehci_defs.h:9
#define STS_FLR
Definition: ehci_defs.h:24
#define STS_PSS
Definition: ehci_defs.h:17
#define STS_FATAL
Definition: ehci_defs.h:23
#define CMD_RUN
Definition: ehci_defs.h:12
#define STS_ASS
Definition: ehci_defs.h:16
#define CMD_ASE
Definition: ehci_defs.h:8
#define CMD_LRESET
Definition: ehci_defs.h:6
#define STS_HALT
Definition: ehci_defs.h:19
#define PORT_CONNECT
Definition: ehci_defs.h:71
#define STS_PCD
Definition: ehci_defs.h:25
u32 command
Definition: ehci_defs.h:2
#define STS_INT
Definition: ehci_defs.h:27
#define PORT_PE
Definition: ehci_defs.h:69
#define PORT_USB11(x)
Definition: ehci_defs.h:60
#define STS_RECL
Definition: ehci_defs.h:18
#define STS_IAA
Definition: ehci_defs.h:22
#define CMD_PARK_CNT(c)
Definition: ehci_defs.h:5
#define PORT_OWNER
Definition: ehci_defs.h:58
#define CMD_PARK
Definition: ehci_defs.h:4
#define CMD_RESET
Definition: ehci_defs.h:11
#define likely(x)
Definition: ehci_types.h:25
#define dma_addr_t
Definition: ehci_types.h:12
#define __iomem
Definition: ehci_types.h:4
int cnt
Definition: enet.c:409
u32 ptr
Definition: iso9660.c:536
u16 idProduct
Definition: usb.h:117
u16 idVendor
Definition: usb.h:116
usb_devdesc desc
Definition: ehci.h:50
int fd
Definition: ehci.h:53
int port
Definition: ehci.h:52
int id
Definition: ehci.h:51
u32 toggles
Definition: ehci.h:54
Definition: ehci.h:75
void * ctrl_buffer
Definition: ehci.h:100
struct ehci_qh * asyncqh
Definition: ehci.h:86
int bus_id
Definition: ehci.h:102
struct ehci_device devices[EHCI_MAX_ROOT_PORTS]
Definition: ehci.h:99
struct ehci_regs __iomem * regs
Definition: ehci.h:79
struct ehci_qh * async
Definition: ehci.h:85
u8 num_port
Definition: ehci.h:98
int qtd_used
Definition: ehci.h:89
u32 command
Definition: ehci.h:91
Definition: ehci.h:208
__hc32 hw_token
Definition: ehci.h:224
dma_addr_t qh_dma
Definition: ehci.h:229
struct ehci_qtd * qtd_head
Definition: ehci.h:230
__hc32 hw_alt_next
Definition: ehci.h:223
__hc32 hw_info1
Definition: ehci.h:211
__hc32 hw_current
Definition: ehci.h:219
__hc32 hw_next
Definition: ehci.h:210
__hc32 hw_qtd_next
Definition: ehci.h:222
__hc32 hw_info2
Definition: ehci.h:213
Definition: ehci.h:119
struct ehci_urb * urb
Definition: ehci.h:148
size_t length
Definition: ehci.h:149
__hc32 hw_token
Definition: ehci.h:123
__hc32 hw_alt_next
Definition: ehci.h:122
__hc32 hw_buf[5]
Definition: ehci.h:142
__hc32 hw_buf_hi[5]
Definition: ehci.h:143
struct ehci_qtd * next
Definition: ehci.h:147
__hc32 hw_next
Definition: ehci.h:121
dma_addr_t qtd_dma
Definition: ehci.h:146
u32 intr_enable
Definition: ehci_defs.h:84
u32 configured_flag
Definition: ehci_defs.h:98
u32 status
Definition: ehci_defs.h:69
u32 async_next
Definition: ehci_defs.h:93
u32 port_status[0]
Definition: ehci_defs.h:102
u32 command
Definition: ehci_defs.h:56
Definition: ehci.h:61
void * transfer_buffer
Definition: ehci.h:65
dma_addr_t setup_dma
Definition: ehci.h:63
u32 transfer_buffer_length
Definition: ehci.h:67
u8 ep
Definition: ehci.h:70
u8 input
Definition: ehci.h:71
u32 maxpacket
Definition: ehci.h:72
u32 actual_length
Definition: ehci.h:68
void * setup_buffer
Definition: ehci.h:62
dma_addr_t transfer_dma
Definition: ehci.h:66
#define ehci_writel(v, a)
Definition: tinyehci.c:60
#define get_timer()
Definition: tinyehci.c:61
#define ehci_dbg(a...)
Definition: tinyehci.c:49
#define ehci_readl(a)
Definition: tinyehci.c:59
#define print_hex_dump_bytes(a, b, c, d)
Definition: tinyehci.c:47
#define le16_to_cpu(a)
Definition: tinyehci.c:56
#define USB_CTRLTYPE_DIR_HOST2DEVICE
Definition: usb.h:38
#define USB_CTRLTYPE_DIR_DEVICE2HOST
Definition: usb.h:39
void * USB_Alloc(int size)
Definition: usb_os.c:116
#define USB_DT_DEVICE
Definition: usb.h:10
#define USB_REQ_GETDESCRIPTOR
Definition: usb.h:21
s32 USB_ClearHalt(struct ehci_hcd *ehci, struct ehci_device *fd, u8 endpointAddress)
Definition: usb2.c:213
#define USB_REQ_SETADDRESS
Definition: usb.h:20
#define pid
Definition: xenonsprs.h:6
u8 j
Definition: xenos_edid.h:10
uint64_t u64
64bit unsigned integer
Definition: xetypes.h:15
uint8_t u8
8bit unsigned integer
Definition: xetypes.h:12
uint16_t u16
16bit unsigned integer
Definition: xetypes.h:13
int32_t s32
32bit signed integer
Definition: xetypes.h:19
uint32_t u32
32bit unsigned integer
Definition: xetypes.h:14