1 diff -Nurb linux-mips-2.4.27/drivers/usb/host/Config.in linux/drivers/usb/host/Config.in
2 --- linux-mips-2.4.27/drivers/usb/host/Config.in 2003-11-17 02:07:42.000000000 +0100
3 +++ linux/drivers/usb/host/Config.in 2004-11-23 11:24:10.599627448 +0100
5 dep_tristate ' SL811HS Alternate (x86, StrongARM, isosynchronous mode)' CONFIG_USB_SL811HS_ALT $CONFIG_USB $CONFIG_EXPERIMENTAL
6 dep_tristate ' SL811HS (x86, StrongARM) support, old driver' CONFIG_USB_SL811HS $CONFIG_USB $CONFIG_EXPERIMENTAL
8 +dep_tristate ' Non-PCI OHCI support' CONFIG_USB_NON_PCI_OHCI $CONFIG_USB_OHCI
9 diff -Nurb linux-mips-2.4.27/drivers/usb/host/usb-ohci.c linux/drivers/usb/host/usb-ohci.c
10 --- linux-mips-2.4.27/drivers/usb/host/usb-ohci.c 2004-04-16 05:14:18.000000000 +0200
11 +++ linux/drivers/usb/host/usb-ohci.c 2004-11-23 11:24:10.602626992 +0100
13 hc_release_ohci (ohci);
16 +#ifndef CONFIG_USB_NON_PCI_OHCI
17 ohci->flags = id->driver_data;
19 /* Check for NSC87560. We have to look at the bridge (fn1) to identify
21 printk (KERN_INFO __FILE__ ": Using NSC SuperIO setup\n");
22 if (ohci->flags & OHCI_QUIRK_AMD756)
23 printk (KERN_INFO __FILE__ ": AMD756 erratum 4 workaround\n");
26 if (hc_reset (ohci) < 0) {
27 hc_release_ohci (ohci);
28 @@ -2627,8 +2629,10 @@
32 +#ifndef CONFIG_USB_NON_PCI_OHCI
33 if (ohci->pci_latency)
34 pci_write_config_byte (ohci->ohci_dev, PCI_LATENCY_TIMER, ohci->pci_latency);
41 /*-------------------------------------------------------------------------*/
43 +#ifndef CONFIG_USB_NON_PCI_OHCI
44 /* configured so that an OHCI device is always provided */
45 /* always called with process context; sleeping is OK */
47 @@ -2705,6 +2710,88 @@
51 +#else /* CONFIG_USB_NON_PCI_OHCI */
54 +static int ohci_base=0, ohci_len=0;
55 +static int ohci_irq=-1;
57 +MODULE_PARM(ohci_base, "i");
58 +MODULE_PARM(ohci_len, "i");
59 +MODULE_PARM(ohci_irq, "i");
60 +MODULE_PARM_DESC(ohci_base, "IO Base address of OHCI Oper. registers");
61 +MODULE_PARM_DESC(ohci_len, "IO length of OHCI Oper. registers");
62 +MODULE_PARM_DESC(ohci_irq, "IRQ for OHCI interrupts");
65 +static struct pci_dev bogus_pcidev;
67 +static struct pci_driver ohci_pci_driver = {
72 +ohci_non_pci_init (void)
76 + if (!ohci_base || !ohci_len || (ohci_irq < 0))
79 + if (!request_mem_region (ohci_base, ohci_len, ohci_pci_driver.name)) {
80 + dbg ("controller already in use");
84 + mem_base = ioremap_nocache (ohci_base, ohci_len);
86 + err("Error mapping OHCI memory");
91 + * Fill in the bogus pci_dev. Only those members actually
92 + * dereferenced in this driver are initialized.
94 + memset(&bogus_pcidev, 0, sizeof(struct pci_dev));
95 + strcpy(bogus_pcidev.name, "non-PCI OHCI");
96 + strcpy(bogus_pcidev.slot_name, "builtin");
97 + bogus_pcidev.resource[0].name = "OHCI Operational Registers";
98 + bogus_pcidev.resource[0].start = ohci_base;
99 + bogus_pcidev.resource[0].end = ohci_base + ohci_len;
100 + bogus_pcidev.resource[0].flags = 0;
101 + bogus_pcidev.irq = ohci_irq;
103 + return hc_found_ohci (&bogus_pcidev, bogus_pcidev.irq, mem_base, NULL);
109 +ohci_setup (char* options)
113 + if (!options || !*options)
116 + for(this_opt=strtok(options,",");this_opt;this_opt=strtok(NULL,",")) {
117 + if (!strncmp(this_opt, "base:", 5)) {
118 + ohci_base = simple_strtoul(this_opt+5, NULL, 0);
119 + } else if (!strncmp(this_opt, "len:", 4)) {
120 + ohci_len = simple_strtoul(this_opt+4, NULL, 0);
121 + } else if (!strncmp(this_opt, "irq:", 4)) {
122 + ohci_irq = simple_strtoul(this_opt+4, NULL, 0);
128 +__setup("usb_ohci=", ohci_setup);
130 +#endif /* !MODULE */
132 +#endif /* CONFIG_USB_NON_PCI_OHCI */
134 /*-------------------------------------------------------------------------*/
136 @@ -2745,6 +2832,7 @@
140 +#ifndef CONFIG_USB_NON_PCI_OHCI
143 /*-------------------------------------------------------------------------*/
144 @@ -2983,20 +3071,29 @@
145 resume: ohci_pci_resume,
148 +#endif /* CONFIG_USB_NON_PCI_OHCI */
151 /*-------------------------------------------------------------------------*/
153 static int __init ohci_hcd_init (void)
155 +#ifndef CONFIG_USB_NON_PCI_OHCI
156 return pci_module_init (&ohci_pci_driver);
158 + return ohci_non_pci_init();
162 /*-------------------------------------------------------------------------*/
164 static void __exit ohci_hcd_cleanup (void)
166 +#ifndef CONFIG_USB_NON_PCI_OHCI
167 pci_unregister_driver (&ohci_pci_driver);
169 + ohci_pci_remove(&bogus_pcidev);
173 module_init (ohci_hcd_init);
174 diff -Nurb linux-mips-2.4.27/drivers/usb/host/usb-ohci.c.orig linux/drivers/usb/host/usb-ohci.c.orig
175 --- linux-mips-2.4.27/drivers/usb/host/usb-ohci.c.orig 1970-01-01 01:00:00.000000000 +0100
176 +++ linux/drivers/usb/host/usb-ohci.c.orig 2004-11-23 11:21:54.984244120 +0100
179 + * URB OHCI HCD (Host Controller Driver) for USB.
181 + * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
182 + * (C) Copyright 2000-2001 David Brownell <dbrownell@users.sourceforge.net>
184 + * [ Initialisation is based on Linus' ]
185 + * [ uhci code and gregs ohci fragments ]
186 + * [ (C) Copyright 1999 Linus Torvalds ]
187 + * [ (C) Copyright 1999 Gregory P. Smith]
192 + * 2002/10/22 OHCI_USB_OPER for ALi lockup in IBM i1200 (ALEX <thchou@ali>)
193 + * 2002/03/08 interrupt unlink fix (Matt Hughes), better cleanup on
194 + * load failure (Matthew Frederickson)
195 + * 2002/01/20 async unlink fixes: return -EINPROGRESS (per spec) and
196 + * make interrupt unlink-in-completion work (db)
197 + * 2001/09/19 USB_ZERO_PACKET support (Jean Tourrilhes)
198 + * 2001/07/17 power management and pmac cleanup (Benjamin Herrenschmidt)
199 + * 2001/03/24 td/ed hashing to remove bus_to_virt (Steve Longerbeam);
200 + pci_map_single (db)
201 + * 2001/03/21 td and dev/ed allocation uses new pci_pool API (db)
202 + * 2001/03/07 hcca allocation uses pci_alloc_consistent (Steve Longerbeam)
204 + * 2000/09/26 fixed races in removing the private portion of the urb
205 + * 2000/09/07 disable bulk and control lists when unlinking the last
206 + * endpoint descriptor in order to avoid unrecoverable errors on
207 + * the Lucent chips. (rwc@sgi)
208 + * 2000/08/29 use bandwidth claiming hooks (thanks Randy!), fix some
209 + * urb unlink probs, indentation fixes
210 + * 2000/08/11 various oops fixes mostly affecting iso and cleanup from
212 + * 2000/06/28 use PCI hotplug framework, for better power management
213 + * and for Cardbus support (David Brownell)
214 + * 2000/earlier: fixes for NEC/Lucent chips; suspend/resume handling
215 + * when the controller loses power; handle UE; cleanup; ...
217 + * v5.2 1999/12/07 URB 3rd preview,
218 + * v5.1 1999/11/30 URB 2nd preview, cpia, (usb-scsi)
219 + * v5.0 1999/11/22 URB Technical preview, Paul Mackerras powerbook susp/resume
220 + * i386: HUB, Keyboard, Mouse, Printer
222 + * v4.3 1999/10/27 multiple HCs, bulk_request
223 + * v4.2 1999/09/05 ISO API alpha, new dev alloc, neg Error-codes
224 + * v4.1 1999/08/27 Randy Dunlap's - ISO API first impl.
227 + * v2.1 1999/05/09 code clean up
229 + * v1.0 1999/04/27 initial release
232 +#include <linux/config.h>
233 +#include <linux/module.h>
234 +#include <linux/pci.h>
235 +#include <linux/kernel.h>
236 +#include <linux/delay.h>
237 +#include <linux/ioport.h>
238 +#include <linux/sched.h>
239 +#include <linux/slab.h>
240 +#include <linux/smp_lock.h>
241 +#include <linux/errno.h>
242 +#include <linux/init.h>
243 +#include <linux/timer.h>
244 +#include <linux/list.h>
245 +#include <linux/interrupt.h> /* for in_interrupt() */
247 +#include <linux/usb.h>
250 +#include <asm/irq.h>
251 +#include <asm/system.h>
252 +#include <asm/unaligned.h>
254 +#define OHCI_USE_NPS // force NoPowerSwitching mode
255 +// #define OHCI_VERBOSE_DEBUG /* not always helpful */
257 +#include "usb-ohci.h"
261 +#ifdef CONFIG_PMAC_PBOOK
262 +#include <asm/machdep.h>
263 +#include <asm/pmac_feature.h>
264 +#include <asm/pci-bridge.h>
272 + * Version Information
274 +#define DRIVER_VERSION "v5.3"
275 +#define DRIVER_AUTHOR "Roman Weissgaerber <weissg@vienna.at>, David Brownell"
276 +#define DRIVER_DESC "USB OHCI Host Controller Driver"
278 +/* For initializing controller (mask in an HCFS mode too) */
279 +#define OHCI_CONTROL_INIT \
280 + (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
282 +#define OHCI_UNLINK_TIMEOUT (HZ / 10)
284 +/*-------------------------------------------------------------------------*/
286 +/* AMD-756 (D2 rev) reports corrupt register contents in some cases.
287 + * The erratum (#4) description is incorrect. AMD's workaround waits
288 + * till some bits (mostly reserved) are clear; ok for all revs.
290 +#define read_roothub(hc, register, mask) ({ \
291 + u32 temp = readl (&hc->regs->roothub.register); \
292 + if (hc->flags & OHCI_QUIRK_AMD756) \
293 + while (temp & mask) \
294 + temp = readl (&hc->regs->roothub.register); \
297 +static u32 roothub_a (struct ohci *hc)
298 + { return read_roothub (hc, a, 0xfc0fe000); }
299 +static inline u32 roothub_b (struct ohci *hc)
300 + { return readl (&hc->regs->roothub.b); }
301 +static inline u32 roothub_status (struct ohci *hc)
302 + { return readl (&hc->regs->roothub.status); }
303 +static u32 roothub_portstatus (struct ohci *hc, int i)
304 + { return read_roothub (hc, portstatus [i], 0xffe0fce0); }
307 +/*-------------------------------------------------------------------------*
308 + * URB support functions
309 + *-------------------------------------------------------------------------*/
311 +static void ohci_complete_add(struct ohci *ohci, struct urb *urb)
314 + if (urb->hcpriv != NULL) {
315 + printk("completing with non-null priv!\n");
319 + if (ohci->complete_tail == NULL) {
320 + ohci->complete_head = urb;
321 + ohci->complete_tail = urb;
323 + ohci->complete_head->hcpriv = urb;
324 + ohci->complete_tail = urb;
328 +static inline struct urb *ohci_complete_get(struct ohci *ohci)
332 + if ((urb = ohci->complete_head) == NULL)
334 + if (urb == ohci->complete_tail) {
335 + ohci->complete_tail = NULL;
336 + ohci->complete_head = NULL;
338 + ohci->complete_head = urb->hcpriv;
340 + urb->hcpriv = NULL;
344 +static inline void ohci_complete(struct ohci *ohci)
348 + spin_lock(&ohci->ohci_lock);
349 + while ((urb = ohci_complete_get(ohci)) != NULL) {
350 + spin_unlock(&ohci->ohci_lock);
352 + usb_dec_dev_use (urb->dev);
356 + (*urb->complete)(urb);
357 + spin_lock(&ohci->ohci_lock);
359 + spin_unlock(&ohci->ohci_lock);
362 +/* free HCD-private data associated with this URB */
364 +static void urb_free_priv (struct ohci *hc, urb_priv_t * urb_priv)
367 + int last = urb_priv->length - 1;
374 + /* ISOC, BULK, INTR data buffer starts at td 0
375 + * CTRL setup starts at td 0 */
376 + td = urb_priv->td [0];
378 + len = td->urb->transfer_buffer_length,
379 + dir = usb_pipeout (td->urb->pipe)
381 + : PCI_DMA_FROMDEVICE;
383 + /* unmap CTRL URB setup */
384 + if (usb_pipecontrol (td->urb->pipe)) {
385 + pci_unmap_single (hc->ohci_dev,
386 + td->data_dma, 8, PCI_DMA_TODEVICE);
388 + /* CTRL data buffer starts at td 1 if len > 0 */
389 + if (len && last > 0)
390 + td = urb_priv->td [1];
393 + /* unmap data buffer */
394 + if (len && td->data_dma)
395 + pci_unmap_single (hc->ohci_dev, td->data_dma, len, dir);
397 + for (i = 0; i <= last; i++) {
398 + td = urb_priv->td [i];
407 +static void urb_rm_priv_locked (struct urb * urb)
409 + urb_priv_t * urb_priv = urb->hcpriv;
412 + urb->hcpriv = NULL;
415 + if (urb->timeout) {
416 + list_del (&urb->urb_list);
417 + urb->timeout -= jiffies;
421 + /* Release int/iso bandwidth */
422 + if (urb->bandwidth) {
423 + switch (usb_pipetype(urb->pipe)) {
424 + case PIPE_INTERRUPT:
425 + usb_release_bandwidth (urb->dev, urb, 0);
427 + case PIPE_ISOCHRONOUS:
428 + usb_release_bandwidth (urb->dev, urb, 1);
435 + urb_free_priv ((struct ohci *)urb->dev->bus->hcpriv, urb_priv);
437 + if (urb->dev != NULL) {
438 + err ("Non-null dev at rm_priv time");
439 + // urb->dev = NULL;
444 +/*-------------------------------------------------------------------------*/
447 +static int sohci_get_current_frame_number (struct usb_device * dev);
449 +/* debug| print the main components of an URB
450 + * small: 0) header + data packets 1) just header */
452 +static void urb_print (struct urb * urb, char * str, int small)
454 + unsigned int pipe= urb->pipe;
456 + if (!urb->dev || !urb->dev->bus) {
457 + dbg("%s URB: no dev", str);
461 +#ifndef OHCI_VERBOSE_DEBUG
462 + if (urb->status != 0)
464 + dbg("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,flags:%4x,len:%d/%d,stat:%d(%x)",
466 + sohci_get_current_frame_number (urb->dev),
467 + usb_pipedevice (pipe),
468 + usb_pipeendpoint (pipe),
469 + usb_pipeout (pipe)? 'O': 'I',
470 + usb_pipetype (pipe) < 2? (usb_pipeint (pipe)? "INTR": "ISOC"):
471 + (usb_pipecontrol (pipe)? "CTRL": "BULK"),
472 + urb->transfer_flags,
473 + urb->actual_length,
474 + urb->transfer_buffer_length,
475 + urb->status, urb->status);
476 +#ifdef OHCI_VERBOSE_DEBUG
480 + if (usb_pipecontrol (pipe)) {
481 + printk (KERN_DEBUG __FILE__ ": cmd(8):");
482 + for (i = 0; i < 8 ; i++)
483 + printk (" %02x", ((__u8 *) urb->setup_packet) [i]);
486 + if (urb->transfer_buffer_length > 0 && urb->transfer_buffer) {
487 + printk (KERN_DEBUG __FILE__ ": data(%d/%d):",
488 + urb->actual_length,
489 + urb->transfer_buffer_length);
490 + len = usb_pipeout (pipe)?
491 + urb->transfer_buffer_length: urb->actual_length;
492 + for (i = 0; i < 16 && i < len; i++)
493 + printk (" %02x", ((__u8 *) urb->transfer_buffer) [i]);
494 + printk ("%s stat:%d\n", i < len? "...": "", urb->status);
500 +/* just for debugging; prints non-empty branches of the int ed tree inclusive iso eds*/
501 +void ep_print_int_eds (ohci_t * ohci, char * str) {
504 + for (i= 0; i < 32; i++) {
506 + ed_p = &(ohci->hcca->int_table [i]);
509 + printk (KERN_DEBUG __FILE__ ": %s branch int %2d(%2x):", str, i, i);
510 + while (*ed_p != 0 && j--) {
511 + ed_t *ed = dma_to_ed (ohci, le32_to_cpup(ed_p));
512 + printk (" ed: %4x;", ed->hwINFO);
513 + ed_p = &ed->hwNextED;
520 +static void ohci_dump_intr_mask (char *label, __u32 mask)
522 + dbg ("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
525 + (mask & OHCI_INTR_MIE) ? " MIE" : "",
526 + (mask & OHCI_INTR_OC) ? " OC" : "",
527 + (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
528 + (mask & OHCI_INTR_FNO) ? " FNO" : "",
529 + (mask & OHCI_INTR_UE) ? " UE" : "",
530 + (mask & OHCI_INTR_RD) ? " RD" : "",
531 + (mask & OHCI_INTR_SF) ? " SF" : "",
532 + (mask & OHCI_INTR_WDH) ? " WDH" : "",
533 + (mask & OHCI_INTR_SO) ? " SO" : ""
537 +static void maybe_print_eds (char *label, __u32 value)
540 + dbg ("%s %08x", label, value);
543 +static char *hcfs2string (int state)
546 + case OHCI_USB_RESET: return "reset";
547 + case OHCI_USB_RESUME: return "resume";
548 + case OHCI_USB_OPER: return "operational";
549 + case OHCI_USB_SUSPEND: return "suspend";
554 +// dump control and status registers
555 +static void ohci_dump_status (ohci_t *controller)
557 + struct ohci_regs *regs = controller->regs;
560 + temp = readl (®s->revision) & 0xff;
562 + dbg ("spec %d.%d", (temp >> 4), (temp & 0x0f));
564 + temp = readl (®s->control);
565 + dbg ("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
566 + (temp & OHCI_CTRL_RWE) ? " RWE" : "",
567 + (temp & OHCI_CTRL_RWC) ? " RWC" : "",
568 + (temp & OHCI_CTRL_IR) ? " IR" : "",
569 + hcfs2string (temp & OHCI_CTRL_HCFS),
570 + (temp & OHCI_CTRL_BLE) ? " BLE" : "",
571 + (temp & OHCI_CTRL_CLE) ? " CLE" : "",
572 + (temp & OHCI_CTRL_IE) ? " IE" : "",
573 + (temp & OHCI_CTRL_PLE) ? " PLE" : "",
574 + temp & OHCI_CTRL_CBSR
577 + temp = readl (®s->cmdstatus);
578 + dbg ("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
579 + (temp & OHCI_SOC) >> 16,
580 + (temp & OHCI_OCR) ? " OCR" : "",
581 + (temp & OHCI_BLF) ? " BLF" : "",
582 + (temp & OHCI_CLF) ? " CLF" : "",
583 + (temp & OHCI_HCR) ? " HCR" : ""
586 + ohci_dump_intr_mask ("intrstatus", readl (®s->intrstatus));
587 + ohci_dump_intr_mask ("intrenable", readl (®s->intrenable));
588 + // intrdisable always same as intrenable
589 + // ohci_dump_intr_mask ("intrdisable", readl (®s->intrdisable));
591 + maybe_print_eds ("ed_periodcurrent", readl (®s->ed_periodcurrent));
593 + maybe_print_eds ("ed_controlhead", readl (®s->ed_controlhead));
594 + maybe_print_eds ("ed_controlcurrent", readl (®s->ed_controlcurrent));
596 + maybe_print_eds ("ed_bulkhead", readl (®s->ed_bulkhead));
597 + maybe_print_eds ("ed_bulkcurrent", readl (®s->ed_bulkcurrent));
599 + maybe_print_eds ("donehead", readl (®s->donehead));
602 +static void ohci_dump_roothub (ohci_t *controller, int verbose)
604 + __u32 temp, ndp, i;
606 + temp = roothub_a (controller);
607 + if (temp == ~(u32)0)
609 + ndp = (temp & RH_A_NDP);
612 + dbg ("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
613 + ((temp & RH_A_POTPGT) >> 24) & 0xff,
614 + (temp & RH_A_NOCP) ? " NOCP" : "",
615 + (temp & RH_A_OCPM) ? " OCPM" : "",
616 + (temp & RH_A_DT) ? " DT" : "",
617 + (temp & RH_A_NPS) ? " NPS" : "",
618 + (temp & RH_A_PSM) ? " PSM" : "",
621 + temp = roothub_b (controller);
622 + dbg ("roothub.b: %08x PPCM=%04x DR=%04x",
624 + (temp & RH_B_PPCM) >> 16,
627 + temp = roothub_status (controller);
628 + dbg ("roothub.status: %08x%s%s%s%s%s%s",
630 + (temp & RH_HS_CRWE) ? " CRWE" : "",
631 + (temp & RH_HS_OCIC) ? " OCIC" : "",
632 + (temp & RH_HS_LPSC) ? " LPSC" : "",
633 + (temp & RH_HS_DRWE) ? " DRWE" : "",
634 + (temp & RH_HS_OCI) ? " OCI" : "",
635 + (temp & RH_HS_LPS) ? " LPS" : ""
639 + for (i = 0; i < ndp; i++) {
640 + temp = roothub_portstatus (controller, i);
641 + dbg ("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
644 + (temp & RH_PS_PRSC) ? " PRSC" : "",
645 + (temp & RH_PS_OCIC) ? " OCIC" : "",
646 + (temp & RH_PS_PSSC) ? " PSSC" : "",
647 + (temp & RH_PS_PESC) ? " PESC" : "",
648 + (temp & RH_PS_CSC) ? " CSC" : "",
650 + (temp & RH_PS_LSDA) ? " LSDA" : "",
651 + (temp & RH_PS_PPS) ? " PPS" : "",
652 + (temp & RH_PS_PRS) ? " PRS" : "",
653 + (temp & RH_PS_POCI) ? " POCI" : "",
654 + (temp & RH_PS_PSS) ? " PSS" : "",
656 + (temp & RH_PS_PES) ? " PES" : "",
657 + (temp & RH_PS_CCS) ? " CCS" : ""
662 +static void ohci_dump (ohci_t *controller, int verbose)
664 + dbg ("OHCI controller usb-%s state", controller->ohci_dev->slot_name);
666 + // dumps some of the state we know about
667 + ohci_dump_status (controller);
669 + ep_print_int_eds (controller, "hcca");
670 + dbg ("hcca frame #%04x", controller->hcca->frame_no);
671 + ohci_dump_roothub (controller, 1);
677 +/*-------------------------------------------------------------------------*
678 + * Interface functions (URB)
679 + *-------------------------------------------------------------------------*/
681 +/* return a request to the completion handler */
683 +static int sohci_return_urb (struct ohci *hc, struct urb * urb)
685 + urb_priv_t * urb_priv = urb->hcpriv;
690 + return -1; /* urb already unlinked */
692 + /* just to be sure */
693 + if (!urb->complete) {
694 + urb_rm_priv_locked (urb);
695 + ohci_complete_add(hc, urb); /* Just usb_dec_dev_use */
700 + urb_print (urb, "RET", usb_pipeout (urb->pipe));
703 + switch (usb_pipetype (urb->pipe)) {
704 + case PIPE_INTERRUPT:
705 + pci_unmap_single (hc->ohci_dev,
706 + urb_priv->td [0]->data_dma,
707 + urb->transfer_buffer_length,
708 + usb_pipeout (urb->pipe)
710 + : PCI_DMA_FROMDEVICE);
711 + if (urb->interval) {
712 + urb->complete (urb);
714 + /* implicitly requeued */
715 + urb->actual_length = 0;
716 + urb->status = -EINPROGRESS;
717 + td_submit_urb (urb);
719 + urb_rm_priv_locked (urb);
720 + ohci_complete_add(hc, urb);
724 + case PIPE_ISOCHRONOUS:
725 + for (urbt = urb->next; urbt && (urbt != urb); urbt = urbt->next);
726 + if (urbt) { /* send the reply and requeue URB */
727 + pci_unmap_single (hc->ohci_dev,
728 + urb_priv->td [0]->data_dma,
729 + urb->transfer_buffer_length,
730 + usb_pipeout (urb->pipe)
732 + : PCI_DMA_FROMDEVICE);
733 + urb->complete (urb);
734 + urb->actual_length = 0;
735 + urb->status = USB_ST_URB_PENDING;
736 + urb->start_frame = urb_priv->ed->last_iso + 1;
737 + if (urb_priv->state != URB_DEL) {
738 + for (i = 0; i < urb->number_of_packets; i++) {
739 + urb->iso_frame_desc[i].actual_length = 0;
740 + urb->iso_frame_desc[i].status = -EXDEV;
742 + td_submit_urb (urb);
745 + } else { /* unlink URB, call complete */
746 + urb_rm_priv_locked (urb);
747 + ohci_complete_add(hc, urb);
752 + case PIPE_CONTROL: /* unlink URB, call complete */
753 + urb_rm_priv_locked (urb);
754 + ohci_complete_add(hc, urb);
760 +/*-------------------------------------------------------------------------*/
762 +/* get a transfer request */
764 +static int sohci_submit_urb (struct urb * urb)
768 + urb_priv_t * urb_priv;
769 + unsigned int pipe = urb->pipe;
770 + int maxps = usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe));
772 + unsigned long flags;
774 + int mem_flags = GFP_ATOMIC;
776 + if (!urb->dev || !urb->dev->bus)
779 + if (urb->hcpriv) /* urb already in use */
782 +// if(usb_endpoint_halted (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)))
785 + usb_inc_dev_use (urb->dev);
786 + ohci = (ohci_t *) urb->dev->bus->hcpriv;
789 + urb_print (urb, "SUB", usb_pipein (pipe));
792 + /* handle a request to the virtual root hub */
793 + if (usb_pipedevice (pipe) == ohci->rh.devnum)
794 + return rh_submit_urb (urb);
796 + spin_lock_irqsave(&ohci->ohci_lock, flags);
798 + /* when controller's hung, permit only roothub cleanup attempts
799 + * such as powering down ports */
800 + if (ohci->disabled) {
801 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
802 + usb_dec_dev_use (urb->dev);
806 + /* every endpoint has a ed, locate and fill it */
807 + if (!(ed = ep_add_ed (urb->dev, pipe, urb->interval, 1, mem_flags))) {
808 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
809 + usb_dec_dev_use (urb->dev);
813 + /* for the private part of the URB we need the number of TDs (size) */
814 + switch (usb_pipetype (pipe)) {
815 + case PIPE_BULK: /* one TD for every 4096 Byte */
816 + size = (urb->transfer_buffer_length - 1) / 4096 + 1;
818 + /* If the transfer size is multiple of the pipe mtu,
819 + * we may need an extra TD to create a empty frame
821 + if ((urb->transfer_flags & USB_ZERO_PACKET) &&
822 + usb_pipeout (pipe) &&
823 + (urb->transfer_buffer_length != 0) &&
824 + ((urb->transfer_buffer_length % maxps) == 0))
827 + case PIPE_ISOCHRONOUS: /* number of packets from URB */
828 + size = urb->number_of_packets;
830 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
831 + usb_dec_dev_use (urb->dev);
834 + for (i = 0; i < urb->number_of_packets; i++) {
835 + urb->iso_frame_desc[i].actual_length = 0;
836 + urb->iso_frame_desc[i].status = -EXDEV;
839 + case PIPE_CONTROL: /* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
840 + size = (urb->transfer_buffer_length == 0)? 2:
841 + (urb->transfer_buffer_length - 1) / 4096 + 3;
843 + case PIPE_INTERRUPT: /* one TD */
848 + /* allocate the private part of the URB */
849 + urb_priv = kmalloc (sizeof (urb_priv_t) + size * sizeof (td_t *),
852 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
853 + usb_dec_dev_use (urb->dev);
856 + memset (urb_priv, 0, sizeof (urb_priv_t) + size * sizeof (td_t *));
858 + /* fill the private part of the URB */
859 + urb_priv->length = size;
862 + /* allocate the TDs (updating hash chains) */
863 + for (i = 0; i < size; i++) {
864 + urb_priv->td[i] = td_alloc (ohci, SLAB_ATOMIC);
865 + if (!urb_priv->td[i]) {
866 + urb_priv->length = i;
867 + urb_free_priv (ohci, urb_priv);
868 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
869 + usb_dec_dev_use (urb->dev);
874 + if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
875 + urb_free_priv (ohci, urb_priv);
876 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
877 + usb_dec_dev_use (urb->dev);
881 + /* allocate and claim bandwidth if needed; ISO
882 + * needs start frame index if it was't provided.
884 + switch (usb_pipetype (pipe)) {
885 + case PIPE_ISOCHRONOUS:
886 + if (urb->transfer_flags & USB_ISO_ASAP) {
887 + urb->start_frame = ((ed->state == ED_OPER)
888 + ? (ed->last_iso + 1)
889 + : (le16_to_cpu (ohci->hcca->frame_no) + 10)) & 0xffff;
892 + case PIPE_INTERRUPT:
893 + if (urb->bandwidth == 0) {
894 + bustime = usb_check_bandwidth (urb->dev, urb);
897 + urb_free_priv (ohci, urb_priv);
898 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
899 + usb_dec_dev_use (urb->dev);
902 + usb_claim_bandwidth (urb->dev, urb, bustime, usb_pipeisoc (urb->pipe));
908 + urb->actual_length = 0;
909 + urb->hcpriv = urb_priv;
910 + urb->status = USB_ST_URB_PENDING;
912 + /* link the ed into a chain if is not already */
913 + if (ed->state != ED_OPER)
914 + ep_link (ohci, ed);
916 + /* fill the TDs and link it to the ed */
917 + td_submit_urb (urb);
920 + /* maybe add to ordered list of timeouts */
921 + if (urb->timeout) {
922 + struct list_head *entry;
924 + urb->timeout += jiffies;
926 + list_for_each (entry, &ohci->timeout_list) {
927 + struct urb *next_urb;
929 + next_urb = list_entry (entry, struct urb, urb_list);
930 + if (time_after_eq (urb->timeout, next_urb->timeout))
933 + list_add (&urb->urb_list, entry);
935 + /* drive timeouts by SF (messy, but works) */
936 + writel (OHCI_INTR_SF, &ohci->regs->intrenable);
937 + (void)readl (&ohci->regs->intrdisable); /* PCI posting flush */
941 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
946 +/*-------------------------------------------------------------------------*/
948 +/* deactivate all TDs and remove the private part of the URB */
949 +/* interrupt callers must use async unlink mode */
951 +static int sohci_unlink_urb (struct urb * urb)
953 + unsigned long flags;
956 + if (!urb) /* just to be sure */
959 + if (!urb->dev || !urb->dev->bus)
962 + ohci = (ohci_t *) urb->dev->bus->hcpriv;
965 + urb_print (urb, "UNLINK", 1);
968 + /* handle a request to the virtual root hub */
969 + if (usb_pipedevice (urb->pipe) == ohci->rh.devnum)
970 + return rh_unlink_urb (urb);
972 + spin_lock_irqsave(&ohci->ohci_lock, flags);
973 + if (urb->hcpriv && (urb->status == USB_ST_URB_PENDING)) {
974 + if (!ohci->disabled) {
975 + urb_priv_t * urb_priv;
977 + /* interrupt code may not sleep; it must use
978 + * async status return to unlink pending urbs.
980 + if (!(urb->transfer_flags & USB_ASYNC_UNLINK)
981 + && in_interrupt ()) {
982 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
983 + err ("bug in call from %p; use async!",
984 + __builtin_return_address(0));
985 + return -EWOULDBLOCK;
988 + /* flag the urb and its TDs for deletion in some
989 + * upcoming SF interrupt delete list processing
991 + urb_priv = urb->hcpriv;
993 + if (!urb_priv || (urb_priv->state == URB_DEL)) {
994 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
998 + urb_priv->state = URB_DEL;
999 + ep_rm_ed (urb->dev, urb_priv->ed);
1000 + urb_priv->ed->state |= ED_URB_DEL;
1002 + if (!(urb->transfer_flags & USB_ASYNC_UNLINK)) {
1003 + DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup);
1004 + DECLARE_WAITQUEUE (wait, current);
1005 + int timeout = OHCI_UNLINK_TIMEOUT;
1007 + add_wait_queue (&unlink_wakeup, &wait);
1008 + urb_priv->wait = &unlink_wakeup;
1009 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
1011 + /* wait until all TDs are deleted */
1012 + set_current_state(TASK_UNINTERRUPTIBLE);
1013 + while (timeout && (urb->status == USB_ST_URB_PENDING)) {
1014 + timeout = schedule_timeout (timeout);
1015 + set_current_state(TASK_UNINTERRUPTIBLE);
1017 + set_current_state(TASK_RUNNING);
1018 + remove_wait_queue (&unlink_wakeup, &wait);
1019 + if (urb->status == USB_ST_URB_PENDING) {
1020 + err ("unlink URB timeout");
1021 + return -ETIMEDOUT;
1024 + usb_dec_dev_use (urb->dev);
1026 + if (urb->complete)
1027 + urb->complete (urb);
1029 + /* usb_dec_dev_use done in dl_del_list() */
1030 + urb->status = -EINPROGRESS;
1031 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
1032 + return -EINPROGRESS;
1035 + urb_rm_priv_locked (urb);
1036 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
1037 + usb_dec_dev_use (urb->dev);
1039 + if (urb->transfer_flags & USB_ASYNC_UNLINK) {
1040 + urb->status = -ECONNRESET;
1041 + if (urb->complete)
1042 + urb->complete (urb);
1044 + urb->status = -ENOENT;
1047 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
1052 +/*-------------------------------------------------------------------------*/
1054 +/* allocate private data space for a usb device */
1056 +static int sohci_alloc_dev (struct usb_device *usb_dev)
1058 + struct ohci_device * dev;
1060 + dev = dev_alloc ((struct ohci *) usb_dev->bus->hcpriv, ALLOC_FLAGS);
1064 + usb_dev->hcpriv = dev;
1068 +/*-------------------------------------------------------------------------*/
1070 +/* may be called from interrupt context */
1071 +/* frees private data space of usb device */
1073 +static int sohci_free_dev (struct usb_device * usb_dev)
1075 + unsigned long flags;
1078 + struct ohci_device * dev = usb_to_ohci (usb_dev);
1079 + ohci_t * ohci = usb_dev->bus->hcpriv;
1084 + if (usb_dev->devnum >= 0) {
1086 + /* driver disconnects should have unlinked all urbs
1087 + * (freeing all the TDs, unlinking EDs) but we need
1088 + * to defend against bugs that prevent that.
1090 + spin_lock_irqsave(&ohci->ohci_lock, flags);
1091 + for(i = 0; i < NUM_EDS; i++) {
1092 + ed = &(dev->ed[i]);
1093 + if (ed->state != ED_NEW) {
1094 + if (ed->state == ED_OPER) {
1095 + /* driver on that interface didn't unlink an urb */
1096 + dbg ("driver usb-%s dev %d ed 0x%x unfreed URB",
1097 + ohci->ohci_dev->slot_name, usb_dev->devnum, i);
1098 + ep_unlink (ohci, ed);
1100 + ep_rm_ed (usb_dev, ed);
1101 + ed->state = ED_DEL;
1105 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
1107 + /* if the controller is running, tds for those unlinked
1108 + * urbs get freed by dl_del_list at the next SF interrupt
1112 + if (ohci->disabled) {
1113 + /* FIXME: Something like this should kick in,
1114 + * though it's currently an exotic case ...
1115 + * the controller won't ever be touching
1116 + * these lists again!!
1117 + dl_del_list (ohci,
1118 + le16_to_cpu (ohci->hcca->frame_no) & 1);
1120 + warn ("TD leak, %d", cnt);
1122 + } else if (!in_interrupt ()) {
1123 + DECLARE_WAIT_QUEUE_HEAD (freedev_wakeup);
1124 + DECLARE_WAITQUEUE (wait, current);
1125 + int timeout = OHCI_UNLINK_TIMEOUT;
1127 + /* SF interrupt handler calls dl_del_list */
1128 + add_wait_queue (&freedev_wakeup, &wait);
1129 + dev->wait = &freedev_wakeup;
1130 + set_current_state(TASK_UNINTERRUPTIBLE);
1131 + while (timeout && dev->ed_cnt)
1132 + timeout = schedule_timeout (timeout);
1133 + set_current_state(TASK_RUNNING);
1134 + remove_wait_queue (&freedev_wakeup, &wait);
1135 + if (dev->ed_cnt) {
1136 + err ("free device %d timeout", usb_dev->devnum);
1137 + return -ETIMEDOUT;
1140 + /* likely some interface's driver has a refcount bug */
1141 + err ("bus %s devnum %d deletion in interrupt",
1142 + ohci->ohci_dev->slot_name, usb_dev->devnum);
1148 + /* free device, and associated EDs */
1149 + dev_free (ohci, dev);
1154 +/*-------------------------------------------------------------------------*/
1156 +/* tell us the current USB frame number */
1158 +static int sohci_get_current_frame_number (struct usb_device *usb_dev)
1160 + ohci_t * ohci = usb_dev->bus->hcpriv;
1162 + return le16_to_cpu (ohci->hcca->frame_no);
1165 +/*-------------------------------------------------------------------------*/
1167 +struct usb_operations sohci_device_operations = {
1170 + sohci_get_current_frame_number,
1175 +/*-------------------------------------------------------------------------*
1176 + * ED handling functions
1177 + *-------------------------------------------------------------------------*/
1179 +/* search for the right branch to insert an interrupt ed into the int tree
1180 + * do some load ballancing;
1181 + * returns the branch and
1182 + * sets the interval to interval = 2^integer (ld (interval)) */
1184 +static int ep_int_ballance (ohci_t * ohci, int interval, int load)
1186 + int i, branch = 0;
1188 + /* search for the least loaded interrupt endpoint branch of all 32 branches */
1189 + for (i = 0; i < 32; i++)
1190 + if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i]) branch = i;
1192 + branch = branch % interval;
1193 + for (i = branch; i < 32; i += interval) ohci->ohci_int_load [i] += load;
1198 +/*-------------------------------------------------------------------------*/
1200 +/* 2^int( ld (inter)) */
1202 +static int ep_2_n_interval (int inter)
1205 + for (i = 0; ((inter >> i) > 1 ) && (i < 5); i++);
1209 +/*-------------------------------------------------------------------------*/
1211 +/* the int tree is a binary tree
1212 + * in order to process it sequentially the indexes of the branches have to be mapped
1213 + * the mapping reverses the bits of a word of num_bits length */
1215 +static int ep_rev (int num_bits, int word)
1219 + for (i = 0; i < num_bits; i++) wout |= (((word >> i) & 1) << (num_bits - i - 1));
1223 +/*-------------------------------------------------------------------------*/
1225 +/* link an ed into one of the HC chains */
1227 +static int ep_link (ohci_t * ohci, ed_t * edi)
1235 + volatile ed_t * ed = edi;
1237 + ed->state = ED_OPER;
1239 + switch (ed->type) {
1240 + case PIPE_CONTROL:
1242 + if (ohci->ed_controltail == NULL) {
1243 + writel (ed->dma, &ohci->regs->ed_controlhead);
1245 + ohci->ed_controltail->hwNextED = cpu_to_le32 (ed->dma);
1247 + ed->ed_prev = ohci->ed_controltail;
1248 + if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
1249 + !ohci->ed_rm_list[1] && !ohci->sleeping) {
1250 + ohci->hc_control |= OHCI_CTRL_CLE;
1251 + writel (ohci->hc_control, &ohci->regs->control);
1253 + ohci->ed_controltail = edi;
1258 + if (ohci->ed_bulktail == NULL) {
1259 + writel (ed->dma, &ohci->regs->ed_bulkhead);
1261 + ohci->ed_bulktail->hwNextED = cpu_to_le32 (ed->dma);
1263 + ed->ed_prev = ohci->ed_bulktail;
1264 + if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
1265 + !ohci->ed_rm_list[1] && !ohci->sleeping) {
1266 + ohci->hc_control |= OHCI_CTRL_BLE;
1267 + writel (ohci->hc_control, &ohci->regs->control);
1269 + ohci->ed_bulktail = edi;
1272 + case PIPE_INTERRUPT:
1273 + load = ed->int_load;
1274 + interval = ep_2_n_interval (ed->int_period);
1275 + ed->int_interval = interval;
1276 + int_branch = ep_int_ballance (ohci, interval, load);
1277 + ed->int_branch = int_branch;
1279 + for (i = 0; i < ep_rev (6, interval); i += inter) {
1281 + for (ed_p = &(ohci->hcca->int_table[ep_rev (5, i) + int_branch]);
1282 + (*ed_p != 0) && ((dma_to_ed (ohci, le32_to_cpup (ed_p)))->int_interval >= interval);
1283 + ed_p = &((dma_to_ed (ohci, le32_to_cpup (ed_p)))->hwNextED))
1284 + inter = ep_rev (6, (dma_to_ed (ohci, le32_to_cpup (ed_p)))->int_interval);
1285 + ed->hwNextED = *ed_p;
1286 + *ed_p = cpu_to_le32 (ed->dma);
1289 + ep_print_int_eds (ohci, "LINK_INT");
1293 + case PIPE_ISOCHRONOUS:
1295 + ed->int_interval = 1;
1296 + if (ohci->ed_isotail != NULL) {
1297 + ohci->ed_isotail->hwNextED = cpu_to_le32 (ed->dma);
1298 + ed->ed_prev = ohci->ed_isotail;
1300 + for ( i = 0; i < 32; i += inter) {
1302 + for (ed_p = &(ohci->hcca->int_table[ep_rev (5, i)]);
1304 + ed_p = &((dma_to_ed (ohci, le32_to_cpup (ed_p)))->hwNextED))
1305 + inter = ep_rev (6, (dma_to_ed (ohci, le32_to_cpup (ed_p)))->int_interval);
1306 + *ed_p = cpu_to_le32 (ed->dma);
1308 + ed->ed_prev = NULL;
1310 + ohci->ed_isotail = edi;
1312 + ep_print_int_eds (ohci, "LINK_ISO");
1319 +/*-------------------------------------------------------------------------*/
1321 +/* scan the periodic table to find and unlink this ED */
1322 +static void periodic_unlink (
1323 + struct ohci *ohci,
1328 + for (; index < NUM_INTS; index += period) {
1329 + __u32 *ed_p = &ohci->hcca->int_table [index];
1331 + /* ED might have been unlinked through another path */
1332 + while (*ed_p != 0) {
1333 + if ((dma_to_ed (ohci, le32_to_cpup (ed_p))) == ed) {
1334 + *ed_p = ed->hwNextED;
1337 + ed_p = & ((dma_to_ed (ohci,
1338 + le32_to_cpup (ed_p)))->hwNextED);
1343 +/* unlink an ed from one of the HC chains.
1344 + * just the link to the ed is unlinked.
1345 + * the link from the ed still points to another operational ed or 0
1346 + * so the HC can eventually finish the processing of the unlinked ed */
1348 +static int ep_unlink (ohci_t * ohci, ed_t * ed)
1352 + ed->hwINFO |= cpu_to_le32 (OHCI_ED_SKIP);
1354 + switch (ed->type) {
1355 + case PIPE_CONTROL:
1356 + if (ed->ed_prev == NULL) {
1357 + if (!ed->hwNextED) {
1358 + ohci->hc_control &= ~OHCI_CTRL_CLE;
1359 + writel (ohci->hc_control, &ohci->regs->control);
1361 + writel (le32_to_cpup (&ed->hwNextED), &ohci->regs->ed_controlhead);
1363 + ed->ed_prev->hwNextED = ed->hwNextED;
1365 + if (ohci->ed_controltail == ed) {
1366 + ohci->ed_controltail = ed->ed_prev;
1368 + (dma_to_ed (ohci, le32_to_cpup (&ed->hwNextED)))->ed_prev = ed->ed_prev;
1373 + if (ed->ed_prev == NULL) {
1374 + if (!ed->hwNextED) {
1375 + ohci->hc_control &= ~OHCI_CTRL_BLE;
1376 + writel (ohci->hc_control, &ohci->regs->control);
1378 + writel (le32_to_cpup (&ed->hwNextED), &ohci->regs->ed_bulkhead);
1380 + ed->ed_prev->hwNextED = ed->hwNextED;
1382 + if (ohci->ed_bulktail == ed) {
1383 + ohci->ed_bulktail = ed->ed_prev;
1385 + (dma_to_ed (ohci, le32_to_cpup (&ed->hwNextED)))->ed_prev = ed->ed_prev;
1389 + case PIPE_INTERRUPT:
1390 + periodic_unlink (ohci, ed, 0, 1);
1391 + for (i = ed->int_branch; i < 32; i += ed->int_interval)
1392 + ohci->ohci_int_load[i] -= ed->int_load;
1394 + ep_print_int_eds (ohci, "UNLINK_INT");
1398 + case PIPE_ISOCHRONOUS:
1399 + if (ohci->ed_isotail == ed)
1400 + ohci->ed_isotail = ed->ed_prev;
1401 + if (ed->hwNextED != 0)
1402 + (dma_to_ed (ohci, le32_to_cpup (&ed->hwNextED)))
1403 + ->ed_prev = ed->ed_prev;
1405 + if (ed->ed_prev != NULL)
1406 + ed->ed_prev->hwNextED = ed->hwNextED;
1408 + periodic_unlink (ohci, ed, 0, 1);
1410 + ep_print_int_eds (ohci, "UNLINK_ISO");
1414 + ed->state = ED_UNLINK;
1419 +/*-------------------------------------------------------------------------*/
1421 +/* add/reinit an endpoint; this should be done once at the usb_set_configuration command,
1422 + * but the USB stack is a little bit stateless so we do it at every transaction
1423 + * if the state of the ed is ED_NEW then a dummy td is added and the state is changed to ED_UNLINK
1424 + * in all other cases the state is left unchanged
1425 + * the ed info fields are setted anyway even though most of them should not change */
1427 +static ed_t * ep_add_ed (
1428 + struct usb_device * usb_dev,
1429 + unsigned int pipe,
1435 + ohci_t * ohci = usb_dev->bus->hcpriv;
1438 + volatile ed_t * ed;
1440 + ed = ed_ret = &(usb_to_ohci (usb_dev)->ed[(usb_pipeendpoint (pipe) << 1) |
1441 + (usb_pipecontrol (pipe)? 0: usb_pipeout (pipe))]);
1443 + if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
1444 + /* pending delete request */
1448 + if (ed->state == ED_NEW) {
1449 + ed->hwINFO = cpu_to_le32 (OHCI_ED_SKIP); /* skip ed */
1450 + /* dummy td; end of td list for ed */
1451 + td = td_alloc (ohci, SLAB_ATOMIC);
1452 + /* hash the ed for later reverse mapping */
1453 + if (!td || !hash_add_ed (ohci, (ed_t *)ed)) {
1454 + /* out of memory */
1456 + td_free(ohci, td);
1459 + ed->hwTailP = cpu_to_le32 (td->td_dma);
1460 + ed->hwHeadP = ed->hwTailP;
1461 + ed->state = ED_UNLINK;
1462 + ed->type = usb_pipetype (pipe);
1463 + usb_to_ohci (usb_dev)->ed_cnt++;
1466 + ohci->dev[usb_pipedevice (pipe)] = usb_dev;
1468 + ed->hwINFO = cpu_to_le32 (usb_pipedevice (pipe)
1469 + | usb_pipeendpoint (pipe) << 7
1470 + | (usb_pipeisoc (pipe)? 0x8000: 0)
1471 + | (usb_pipecontrol (pipe)? 0: (usb_pipeout (pipe)? 0x800: 0x1000))
1472 + | usb_pipeslow (pipe) << 13
1473 + | usb_maxpacket (usb_dev, pipe, usb_pipeout (pipe)) << 16);
1475 + if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
1476 + ed->int_period = interval;
1477 + ed->int_load = load;
1483 +/*-------------------------------------------------------------------------*/
1485 +/* request the removal of an endpoint
1486 + * put the ep on the rm_list and request a stop of the bulk or ctrl list
1487 + * real removal is done at the next start frame (SF) hardware interrupt */
1489 +static void ep_rm_ed (struct usb_device * usb_dev, ed_t * ed)
1491 + unsigned int frame;
1492 + ohci_t * ohci = usb_dev->bus->hcpriv;
1494 + if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL))
1497 + ed->hwINFO |= cpu_to_le32 (OHCI_ED_SKIP);
1499 + if (!ohci->disabled) {
1500 + switch (ed->type) {
1501 + case PIPE_CONTROL: /* stop control list */
1502 + ohci->hc_control &= ~OHCI_CTRL_CLE;
1503 + writel (ohci->hc_control, &ohci->regs->control);
1505 + case PIPE_BULK: /* stop bulk list */
1506 + ohci->hc_control &= ~OHCI_CTRL_BLE;
1507 + writel (ohci->hc_control, &ohci->regs->control);
1512 + frame = le16_to_cpu (ohci->hcca->frame_no) & 0x1;
1513 + ed->ed_rm_list = ohci->ed_rm_list[frame];
1514 + ohci->ed_rm_list[frame] = ed;
1516 + if (!ohci->disabled && !ohci->sleeping) {
1517 + /* enable SOF interrupt */
1518 + writel (OHCI_INTR_SF, &ohci->regs->intrstatus);
1519 + writel (OHCI_INTR_SF, &ohci->regs->intrenable);
1520 + (void)readl (&ohci->regs->intrdisable); /* PCI posting flush */
1524 +/*-------------------------------------------------------------------------*
1525 + * TD handling functions
1526 + *-------------------------------------------------------------------------*/
1528 +/* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
1531 +td_fill (ohci_t * ohci, unsigned int info,
1532 + dma_addr_t data, int len,
1533 + struct urb * urb, int index)
1535 + volatile td_t * td, * td_pt;
1536 + urb_priv_t * urb_priv = urb->hcpriv;
1538 + if (index >= urb_priv->length) {
1539 + err("internal OHCI error: TD index > length");
1543 + /* use this td as the next dummy */
1544 + td_pt = urb_priv->td [index];
1545 + td_pt->hwNextTD = 0;
1547 + /* fill the old dummy TD */
1548 + td = urb_priv->td [index] = dma_to_td (ohci,
1549 + le32_to_cpup (&urb_priv->ed->hwTailP) & ~0xf);
1551 + td->ed = urb_priv->ed;
1552 + td->next_dl_td = NULL;
1553 + td->index = index;
1555 + td->data_dma = data;
1559 + td->hwINFO = cpu_to_le32 (info);
1560 + if ((td->ed->type) == PIPE_ISOCHRONOUS) {
1561 + td->hwCBP = cpu_to_le32 (data & 0xFFFFF000);
1562 + td->ed->last_iso = info & 0xffff;
1564 + td->hwCBP = cpu_to_le32 (data);
1567 + td->hwBE = cpu_to_le32 (data + len - 1);
1570 + td->hwNextTD = cpu_to_le32 (td_pt->td_dma);
1571 + td->hwPSW [0] = cpu_to_le16 ((data & 0x0FFF) | 0xE000);
1573 + /* append to queue */
1575 + td->ed->hwTailP = td->hwNextTD;
1578 +/*-------------------------------------------------------------------------*/
1580 +/* prepare all TDs of a transfer */
1582 +static void td_submit_urb (struct urb * urb)
1584 + urb_priv_t * urb_priv = urb->hcpriv;
1585 + ohci_t * ohci = (ohci_t *) urb->dev->bus->hcpriv;
1587 + int data_len = urb->transfer_buffer_length;
1588 + int maxps = usb_maxpacket (urb->dev, urb->pipe, usb_pipeout (urb->pipe));
1591 + unsigned int toggle = 0;
1593 + /* OHCI handles the DATA-toggles itself, we just use the USB-toggle bits for reseting */
1594 + if(usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe))) {
1595 + toggle = TD_T_TOGGLE;
1597 + toggle = TD_T_DATA0;
1598 + usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 1);
1601 + urb_priv->td_cnt = 0;
1604 + data = pci_map_single (ohci->ohci_dev,
1605 + urb->transfer_buffer, data_len,
1606 + usb_pipeout (urb->pipe)
1607 + ? PCI_DMA_TODEVICE
1608 + : PCI_DMA_FROMDEVICE
1613 + switch (usb_pipetype (urb->pipe)) {
1615 + info = usb_pipeout (urb->pipe)?
1616 + TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
1617 + while(data_len > 4096) {
1618 + td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), data, 4096, urb, cnt);
1619 + data += 4096; data_len -= 4096; cnt++;
1621 + info = usb_pipeout (urb->pipe)?
1622 + TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
1623 + td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), data, data_len, urb, cnt);
1626 + /* If the transfer size is multiple of the pipe mtu,
1627 + * we may need an extra TD to create a empty frame
1628 + * Note : another way to check this condition is
1629 + * to test if(urb_priv->length > cnt) - Jean II */
1630 + if ((urb->transfer_flags & USB_ZERO_PACKET) &&
1631 + usb_pipeout (urb->pipe) &&
1632 + (urb->transfer_buffer_length != 0) &&
1633 + ((urb->transfer_buffer_length % maxps) == 0)) {
1634 + td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), 0, 0, urb, cnt);
1638 + if (!ohci->sleeping) {
1640 + writel (OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
1641 + (void)readl (&ohci->regs->intrdisable); /* PCI posting flush */
1645 + case PIPE_INTERRUPT:
1646 + info = usb_pipeout (urb->pipe)?
1647 + TD_CC | TD_DP_OUT | toggle: TD_CC | TD_R | TD_DP_IN | toggle;
1648 + td_fill (ohci, info, data, data_len, urb, cnt++);
1651 + case PIPE_CONTROL:
1652 + info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
1653 + td_fill (ohci, info,
1654 + pci_map_single (ohci->ohci_dev,
1655 + urb->setup_packet, 8,
1656 + PCI_DMA_TODEVICE),
1658 + if (data_len > 0) {
1659 + info = usb_pipeout (urb->pipe)?
1660 + TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 : TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
1661 + /* NOTE: mishandles transfers >8K, some >4K */
1662 + td_fill (ohci, info, data, data_len, urb, cnt++);
1664 + info = usb_pipeout (urb->pipe)?
1665 + TD_CC | TD_DP_IN | TD_T_DATA1: TD_CC | TD_DP_OUT | TD_T_DATA1;
1666 + td_fill (ohci, info, data, 0, urb, cnt++);
1667 + if (!ohci->sleeping) {
1669 + writel (OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
1670 + (void)readl (&ohci->regs->intrdisable); /* PCI posting flush */
1674 + case PIPE_ISOCHRONOUS:
1675 + for (cnt = 0; cnt < urb->number_of_packets; cnt++) {
1676 + td_fill (ohci, TD_CC|TD_ISO | ((urb->start_frame + cnt) & 0xffff),
1677 + data + urb->iso_frame_desc[cnt].offset,
1678 + urb->iso_frame_desc[cnt].length, urb, cnt);
1682 + if (urb_priv->length != cnt)
1683 + dbg("TD LENGTH %d != CNT %d", urb_priv->length, cnt);
1686 +/*-------------------------------------------------------------------------*
1687 + * Done List handling functions
1688 + *-------------------------------------------------------------------------*/
1691 +/* calculate the transfer length and update the urb */
1693 +static void dl_transfer_length(td_t * td)
1695 + __u32 tdINFO, tdBE, tdCBP;
1697 + struct urb * urb = td->urb;
1698 + urb_priv_t * urb_priv = urb->hcpriv;
1702 + tdINFO = le32_to_cpup (&td->hwINFO);
1703 + tdBE = le32_to_cpup (&td->hwBE);
1704 + tdCBP = le32_to_cpup (&td->hwCBP);
1707 + if (tdINFO & TD_ISO) {
1708 + tdPSW = le16_to_cpu (td->hwPSW[0]);
1709 + cc = (tdPSW >> 12) & 0xF;
1711 + if (usb_pipeout(urb->pipe)) {
1712 + dlen = urb->iso_frame_desc[td->index].length;
1714 + dlen = tdPSW & 0x3ff;
1716 + urb->actual_length += dlen;
1717 + urb->iso_frame_desc[td->index].actual_length = dlen;
1718 + if (!(urb->transfer_flags & USB_DISABLE_SPD) && (cc == TD_DATAUNDERRUN))
1719 + cc = TD_CC_NOERROR;
1721 + urb->iso_frame_desc[td->index].status = cc_to_error[cc];
1723 + } else { /* BULK, INT, CONTROL DATA */
1724 + if (!(usb_pipetype (urb->pipe) == PIPE_CONTROL &&
1725 + ((td->index == 0) || (td->index == urb_priv->length - 1)))) {
1727 + if (td->hwCBP == 0)
1728 + urb->actual_length += tdBE - td->data_dma + 1;
1730 + urb->actual_length += tdCBP - td->data_dma;
1736 +/* handle an urb that is being unlinked */
1738 +static void dl_del_urb (ohci_t *ohci, struct urb * urb)
1740 + wait_queue_head_t * wait_head = ((urb_priv_t *)(urb->hcpriv))->wait;
1742 + urb_rm_priv_locked (urb);
1744 + if (urb->transfer_flags & USB_ASYNC_UNLINK) {
1745 + urb->status = -ECONNRESET;
1746 + ohci_complete_add(ohci, urb);
1748 + urb->status = -ENOENT;
1750 + /* unblock sohci_unlink_urb */
1752 + wake_up (wait_head);
1756 +/*-------------------------------------------------------------------------*/
1758 +/* replies to the request have to be on a FIFO basis so
1759 + * we reverse the reversed done-list */
1761 +static td_t * dl_reverse_done_list (ohci_t * ohci)
1764 + td_t * td_rev = NULL;
1765 + td_t * td_list = NULL;
1766 + urb_priv_t * urb_priv = NULL;
1768 + td_list_hc = le32_to_cpup (&ohci->hcca->done_head) & 0xfffffff0;
1769 + ohci->hcca->done_head = 0;
1771 + while (td_list_hc) {
1772 + td_list = dma_to_td (ohci, td_list_hc);
1774 + if (TD_CC_GET (le32_to_cpup (&td_list->hwINFO))) {
1775 + urb_priv = (urb_priv_t *) td_list->urb->hcpriv;
1776 + dbg(" USB-error/status: %x : %p",
1777 + TD_CC_GET (le32_to_cpup (&td_list->hwINFO)), td_list);
1778 + if (td_list->ed->hwHeadP & cpu_to_le32 (0x1)) {
1779 + if (urb_priv && ((td_list->index + 1) < urb_priv->length)) {
1780 + td_list->ed->hwHeadP =
1781 + (urb_priv->td[urb_priv->length - 1]->hwNextTD & cpu_to_le32 (0xfffffff0)) |
1782 + (td_list->ed->hwHeadP & cpu_to_le32 (0x2));
1783 + urb_priv->td_cnt += urb_priv->length - td_list->index - 1;
1785 + td_list->ed->hwHeadP &= cpu_to_le32 (0xfffffff2);
1789 + td_list->next_dl_td = td_rev;
1791 + td_list_hc = le32_to_cpup (&td_list->hwNextTD) & 0xfffffff0;
1796 +/*-------------------------------------------------------------------------*/
1798 +/* there are some pending requests to remove
1799 + * - some of the eds (if ed->state & ED_DEL (set by sohci_free_dev)
1800 + * - some URBs/TDs if urb_priv->state == URB_DEL */
1802 +static void dl_del_list (ohci_t * ohci, unsigned int frame)
1807 + td_t * td = NULL, * td_next = NULL, * tdHeadP = NULL, * tdTailP;
1809 + int ctrl = 0, bulk = 0;
1811 + for (ed = ohci->ed_rm_list[frame]; ed != NULL; ed = ed->ed_rm_list) {
1813 + tdTailP = dma_to_td (ohci, le32_to_cpup (&ed->hwTailP) & 0xfffffff0);
1814 + tdHeadP = dma_to_td (ohci, le32_to_cpup (&ed->hwHeadP) & 0xfffffff0);
1815 + edINFO = le32_to_cpup (&ed->hwINFO);
1816 + td_p = &ed->hwHeadP;
1818 + for (td = tdHeadP; td != tdTailP; td = td_next) {
1819 + struct urb * urb = td->urb;
1820 + urb_priv_t * urb_priv = td->urb->hcpriv;
1822 + td_next = dma_to_td (ohci, le32_to_cpup (&td->hwNextTD) & 0xfffffff0);
1823 + if ((urb_priv->state == URB_DEL) || (ed->state & ED_DEL)) {
1824 + tdINFO = le32_to_cpup (&td->hwINFO);
1825 + if (TD_CC_GET (tdINFO) < 0xE)
1826 + dl_transfer_length (td);
1827 + *td_p = td->hwNextTD | (*td_p & cpu_to_le32 (0x3));
1829 + /* URB is done; clean up */
1830 + if (++(urb_priv->td_cnt) == urb_priv->length)
1831 + dl_del_urb (ohci, urb);
1833 + td_p = &td->hwNextTD;
1837 + if (ed->state & ED_DEL) { /* set by sohci_free_dev */
1838 + struct ohci_device * dev = usb_to_ohci (ohci->dev[edINFO & 0x7F]);
1839 + td_free (ohci, tdTailP); /* free dummy td */
1840 + ed->hwINFO = cpu_to_le32 (OHCI_ED_SKIP);
1841 + ed->state = ED_NEW;
1842 + hash_free_ed(ohci, ed);
1843 + /* if all eds are removed wake up sohci_free_dev */
1844 + if (!--dev->ed_cnt) {
1845 + wait_queue_head_t *wait_head = dev->wait;
1849 + wake_up (wait_head);
1852 + ed->state &= ~ED_URB_DEL;
1853 + tdHeadP = dma_to_td (ohci, le32_to_cpup (&ed->hwHeadP) & 0xfffffff0);
1855 + if (tdHeadP == tdTailP) {
1856 + if (ed->state == ED_OPER)
1857 + ep_unlink(ohci, ed);
1859 + ed->hwINFO &= ~cpu_to_le32 (OHCI_ED_SKIP);
1862 + switch (ed->type) {
1863 + case PIPE_CONTROL:
1872 + /* maybe reenable control and bulk lists */
1873 + if (!ohci->disabled) {
1874 + if (ctrl) /* reset control list */
1875 + writel (0, &ohci->regs->ed_controlcurrent);
1876 + if (bulk) /* reset bulk list */
1877 + writel (0, &ohci->regs->ed_bulkcurrent);
1878 + if (!ohci->ed_rm_list[!frame] && !ohci->sleeping) {
1879 + if (ohci->ed_controltail)
1880 + ohci->hc_control |= OHCI_CTRL_CLE;
1881 + if (ohci->ed_bulktail)
1882 + ohci->hc_control |= OHCI_CTRL_BLE;
1883 + writel (ohci->hc_control, &ohci->regs->control);
1887 + ohci->ed_rm_list[frame] = NULL;
1892 +/*-------------------------------------------------------------------------*/
1896 +static void dl_done_list (ohci_t * ohci, td_t * td_list)
1898 + td_t * td_list_next = NULL;
1902 + urb_priv_t * urb_priv;
1903 + __u32 tdINFO, edHeadP, edTailP;
1906 + td_list_next = td_list->next_dl_td;
1908 + urb = td_list->urb;
1909 + urb_priv = urb->hcpriv;
1910 + tdINFO = le32_to_cpup (&td_list->hwINFO);
1914 + dl_transfer_length(td_list);
1916 + /* error code of transfer */
1917 + cc = TD_CC_GET (tdINFO);
1918 + if (cc == TD_CC_STALL)
1919 + usb_endpoint_halt(urb->dev,
1920 + usb_pipeendpoint(urb->pipe),
1921 + usb_pipeout(urb->pipe));
1923 + if (!(urb->transfer_flags & USB_DISABLE_SPD)
1924 + && (cc == TD_DATAUNDERRUN))
1925 + cc = TD_CC_NOERROR;
1927 + if (++(urb_priv->td_cnt) == urb_priv->length) {
1928 + if ((ed->state & (ED_OPER | ED_UNLINK))
1929 + && (urb_priv->state != URB_DEL)) {
1930 + urb->status = cc_to_error[cc];
1931 + sohci_return_urb (ohci, urb);
1933 + dl_del_urb (ohci, urb);
1937 + if (ed->state != ED_NEW) {
1938 + edHeadP = le32_to_cpup (&ed->hwHeadP) & 0xfffffff0;
1939 + edTailP = le32_to_cpup (&ed->hwTailP);
1941 + /* unlink eds if they are not busy */
1942 + if ((edHeadP == edTailP) && (ed->state == ED_OPER))
1943 + ep_unlink (ohci, ed);
1946 + td_list = td_list_next;
1953 +/*-------------------------------------------------------------------------*
1954 + * Virtual Root Hub
1955 + *-------------------------------------------------------------------------*/
1957 +/* Device descriptor */
1958 +static __u8 root_hub_dev_des[] =
1960 + 0x12, /* __u8 bLength; */
1961 + 0x01, /* __u8 bDescriptorType; Device */
1962 + 0x10, /* __u16 bcdUSB; v1.1 */
1964 + 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1965 + 0x00, /* __u8 bDeviceSubClass; */
1966 + 0x00, /* __u8 bDeviceProtocol; */
1967 + 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1968 + 0x00, /* __u16 idVendor; */
1970 + 0x00, /* __u16 idProduct; */
1972 + 0x00, /* __u16 bcdDevice; */
1974 + 0x00, /* __u8 iManufacturer; */
1975 + 0x02, /* __u8 iProduct; */
1976 + 0x01, /* __u8 iSerialNumber; */
1977 + 0x01 /* __u8 bNumConfigurations; */
1981 +/* Configuration descriptor */
1982 +static __u8 root_hub_config_des[] =
1984 + 0x09, /* __u8 bLength; */
1985 + 0x02, /* __u8 bDescriptorType; Configuration */
1986 + 0x19, /* __u16 wTotalLength; */
1988 + 0x01, /* __u8 bNumInterfaces; */
1989 + 0x01, /* __u8 bConfigurationValue; */
1990 + 0x00, /* __u8 iConfiguration; */
1991 + 0x40, /* __u8 bmAttributes;
1992 + Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
1993 + 0x00, /* __u8 MaxPower; */
1996 + 0x09, /* __u8 if_bLength; */
1997 + 0x04, /* __u8 if_bDescriptorType; Interface */
1998 + 0x00, /* __u8 if_bInterfaceNumber; */
1999 + 0x00, /* __u8 if_bAlternateSetting; */
2000 + 0x01, /* __u8 if_bNumEndpoints; */
2001 + 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
2002 + 0x00, /* __u8 if_bInterfaceSubClass; */
2003 + 0x00, /* __u8 if_bInterfaceProtocol; */
2004 + 0x00, /* __u8 if_iInterface; */
2007 + 0x07, /* __u8 ep_bLength; */
2008 + 0x05, /* __u8 ep_bDescriptorType; Endpoint */
2009 + 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
2010 + 0x03, /* __u8 ep_bmAttributes; Interrupt */
2011 + 0x02, /* __u16 ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
2013 + 0xff /* __u8 ep_bInterval; 255 ms */
2016 +/* Hub class-specific descriptor is constructed dynamically */
2019 +/*-------------------------------------------------------------------------*/
2021 +/* prepare Interrupt pipe data; HUB INTERRUPT ENDPOINT */
2023 +static int rh_send_irq (ohci_t * ohci, void * rh_data, int rh_len)
2032 + num_ports = roothub_a (ohci) & RH_A_NDP;
2033 + if (num_ports > MAX_ROOT_PORTS) {
2034 + err ("bogus NDP=%d for OHCI usb-%s", num_ports,
2035 + ohci->ohci_dev->slot_name);
2036 + err ("rereads as NDP=%d",
2037 + readl (&ohci->regs->roothub.a) & RH_A_NDP);
2038 + /* retry later; "should not happen" */
2041 + *(__u8 *) data = (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
2043 + ret = *(__u8 *) data;
2045 + for ( i = 0; i < num_ports; i++) {
2046 + *(__u8 *) (data + (i + 1) / 8) |=
2047 + ((roothub_portstatus (ohci, i) &
2048 + (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC | RH_PS_OCIC | RH_PS_PRSC))
2049 + ? 1: 0) << ((i + 1) % 8);
2050 + ret += *(__u8 *) (data + (i + 1) / 8);
2055 + memcpy(rh_data, data,
2056 + min_t(unsigned int, len,
2057 + min_t(unsigned int, rh_len, sizeof(data))));
2063 +/*-------------------------------------------------------------------------*/
2065 +/* Virtual Root Hub INTs are polled by this timer every "interval" ms */
2067 +static void rh_int_timer_do (unsigned long ptr)
2071 + struct urb * urb = (struct urb *) ptr;
2072 + ohci_t * ohci = urb->dev->bus->hcpriv;
2074 + if (ohci->disabled)
2077 + /* ignore timers firing during PM suspend, etc */
2078 + if ((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER)
2081 + if(ohci->rh.send) {
2082 + len = rh_send_irq (ohci, urb->transfer_buffer, urb->transfer_buffer_length);
2084 + urb->actual_length = len;
2086 + urb_print (urb, "RET-t(rh)", usb_pipeout (urb->pipe));
2088 + if (urb->complete)
2089 + urb->complete (urb);
2093 + rh_init_int_timer (urb);
2096 +/*-------------------------------------------------------------------------*/
2098 +/* Root Hub INTs are polled by this timer */
2100 +static int rh_init_int_timer (struct urb * urb)
2102 + ohci_t * ohci = urb->dev->bus->hcpriv;
2104 + ohci->rh.interval = urb->interval;
2105 + init_timer (&ohci->rh.rh_int_timer);
2106 + ohci->rh.rh_int_timer.function = rh_int_timer_do;
2107 + ohci->rh.rh_int_timer.data = (unsigned long) urb;
2108 + ohci->rh.rh_int_timer.expires =
2109 + jiffies + (HZ * (urb->interval < 30? 30: urb->interval)) / 1000;
2110 + add_timer (&ohci->rh.rh_int_timer);
2115 +/*-------------------------------------------------------------------------*/
2117 +#define OK(x) len = (x); break
2118 +#define WR_RH_STAT(x) writel((x), &ohci->regs->roothub.status)
2119 +#define WR_RH_PORTSTAT(x) writel((x), &ohci->regs->roothub.portstatus[wIndex-1])
2120 +#define RD_RH_STAT roothub_status(ohci)
2121 +#define RD_RH_PORTSTAT roothub_portstatus(ohci,wIndex-1)
2123 +/* request to virtual root hub */
2125 +static int rh_submit_urb (struct urb * urb)
2127 + struct usb_device * usb_dev = urb->dev;
2128 + ohci_t * ohci = usb_dev->bus->hcpriv;
2129 + unsigned int pipe = urb->pipe;
2130 + struct usb_ctrlrequest * cmd = (struct usb_ctrlrequest *) urb->setup_packet;
2131 + void * data = urb->transfer_buffer;
2132 + int leni = urb->transfer_buffer_length;
2134 + int status = TD_CC_NOERROR;
2135 + unsigned long flags;
2138 + __u8 * data_buf = (__u8 *) datab;
2140 + __u16 bmRType_bReq;
2145 + spin_lock_irqsave(&ohci->ohci_lock, flags);
2147 + if (usb_pipeint(pipe)) {
2148 + ohci->rh.urb = urb;
2149 + ohci->rh.send = 1;
2150 + ohci->rh.interval = urb->interval;
2151 + rh_init_int_timer(urb);
2152 + urb->status = cc_to_error [TD_CC_NOERROR];
2154 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
2158 + bmRType_bReq = cmd->bRequestType | (cmd->bRequest << 8);
2159 + wValue = le16_to_cpu (cmd->wValue);
2160 + wIndex = le16_to_cpu (cmd->wIndex);
2161 + wLength = le16_to_cpu (cmd->wLength);
2163 + switch (bmRType_bReq) {
2164 + /* Request Destination:
2165 + without flags: Device,
2166 + RH_INTERFACE: interface,
2167 + RH_ENDPOINT: endpoint,
2168 + RH_CLASS means HUB here,
2169 + RH_OTHER | RH_CLASS almost ever means HUB_PORT here
2172 + case RH_GET_STATUS:
2173 + *(__u16 *) data_buf = cpu_to_le16 (1); OK (2);
2174 + case RH_GET_STATUS | RH_INTERFACE:
2175 + *(__u16 *) data_buf = cpu_to_le16 (0); OK (2);
2176 + case RH_GET_STATUS | RH_ENDPOINT:
2177 + *(__u16 *) data_buf = cpu_to_le16 (0); OK (2);
2178 + case RH_GET_STATUS | RH_CLASS:
2179 + *(__u32 *) data_buf = cpu_to_le32 (
2180 + RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
2182 + case RH_GET_STATUS | RH_OTHER | RH_CLASS:
2183 + *(__u32 *) data_buf = cpu_to_le32 (RD_RH_PORTSTAT); OK (4);
2185 + case RH_CLEAR_FEATURE | RH_ENDPOINT:
2187 + case (RH_ENDPOINT_STALL): OK (0);
2191 + case RH_CLEAR_FEATURE | RH_CLASS:
2193 + case RH_C_HUB_LOCAL_POWER:
2195 + case (RH_C_HUB_OVER_CURRENT):
2196 + WR_RH_STAT(RH_HS_OCIC); OK (0);
2200 + case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
2202 + case (RH_PORT_ENABLE):
2203 + WR_RH_PORTSTAT (RH_PS_CCS ); OK (0);
2204 + case (RH_PORT_SUSPEND):
2205 + WR_RH_PORTSTAT (RH_PS_POCI); OK (0);
2206 + case (RH_PORT_POWER):
2207 + WR_RH_PORTSTAT (RH_PS_LSDA); OK (0);
2208 + case (RH_C_PORT_CONNECTION):
2209 + WR_RH_PORTSTAT (RH_PS_CSC ); OK (0);
2210 + case (RH_C_PORT_ENABLE):
2211 + WR_RH_PORTSTAT (RH_PS_PESC); OK (0);
2212 + case (RH_C_PORT_SUSPEND):
2213 + WR_RH_PORTSTAT (RH_PS_PSSC); OK (0);
2214 + case (RH_C_PORT_OVER_CURRENT):
2215 + WR_RH_PORTSTAT (RH_PS_OCIC); OK (0);
2216 + case (RH_C_PORT_RESET):
2217 + WR_RH_PORTSTAT (RH_PS_PRSC); OK (0);
2221 + case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
2223 + case (RH_PORT_SUSPEND):
2224 + WR_RH_PORTSTAT (RH_PS_PSS ); OK (0);
2225 + case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
2226 + if (RD_RH_PORTSTAT & RH_PS_CCS)
2227 + WR_RH_PORTSTAT (RH_PS_PRS);
2229 + case (RH_PORT_POWER):
2230 + WR_RH_PORTSTAT (RH_PS_PPS ); OK (0);
2231 + case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
2232 + if (RD_RH_PORTSTAT & RH_PS_CCS)
2233 + WR_RH_PORTSTAT (RH_PS_PES );
2238 + case RH_SET_ADDRESS: ohci->rh.devnum = wValue; OK(0);
2240 + case RH_GET_DESCRIPTOR:
2241 + switch ((wValue & 0xff00) >> 8) {
2242 + case (0x01): /* device descriptor */
2243 + len = min_t(unsigned int,
2245 + min_t(unsigned int,
2246 + sizeof (root_hub_dev_des),
2248 + data_buf = root_hub_dev_des; OK(len);
2249 + case (0x02): /* configuration descriptor */
2250 + len = min_t(unsigned int,
2252 + min_t(unsigned int,
2253 + sizeof (root_hub_config_des),
2255 + data_buf = root_hub_config_des; OK(len);
2256 + case (0x03): /* string descriptors */
2257 + len = usb_root_hub_string (wValue & 0xff,
2258 + (int)(long) ohci->regs, "OHCI",
2262 + OK(min_t(int, leni, len));
2264 + // else fallthrough
2266 + status = TD_CC_STALL;
2270 + case RH_GET_DESCRIPTOR | RH_CLASS:
2272 + __u32 temp = roothub_a (ohci);
2274 + data_buf [0] = 9; // min length;
2275 + data_buf [1] = 0x29;
2276 + data_buf [2] = temp & RH_A_NDP;
2278 + if (temp & RH_A_PSM) /* per-port power switching? */
2279 + data_buf [3] |= 0x1;
2280 + if (temp & RH_A_NOCP) /* no overcurrent reporting? */
2281 + data_buf [3] |= 0x10;
2282 + else if (temp & RH_A_OCPM) /* per-port overcurrent reporting? */
2283 + data_buf [3] |= 0x8;
2286 + data_buf [5] = (temp & RH_A_POTPGT) >> 24;
2287 + temp = roothub_b (ohci);
2288 + data_buf [7] = temp & RH_B_DR;
2289 + if (data_buf [2] < 7) {
2290 + data_buf [8] = 0xff;
2292 + data_buf [0] += 2;
2293 + data_buf [8] = (temp & RH_B_DR) >> 8;
2294 + data_buf [10] = data_buf [9] = 0xff;
2297 + len = min_t(unsigned int, leni,
2298 + min_t(unsigned int, data_buf [0], wLength));
2302 + case RH_GET_CONFIGURATION: *(__u8 *) data_buf = 0x01; OK (1);
2304 + case RH_SET_CONFIGURATION: WR_RH_STAT (0x10000); OK (0);
2307 + dbg ("unsupported root hub command");
2308 + status = TD_CC_STALL;
2312 + // ohci_dump_roothub (ohci, 0);
2315 + len = min_t(int, len, leni);
2316 + if (data != data_buf)
2317 + memcpy (data, data_buf, len);
2318 + urb->actual_length = len;
2319 + urb->status = cc_to_error [status];
2322 + urb_print (urb, "RET(rh)", usb_pipeout (urb->pipe));
2325 + urb->hcpriv = NULL;
2326 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
2327 + usb_dec_dev_use (usb_dev);
2329 + if (urb->complete)
2330 + urb->complete (urb);
2334 +/*-------------------------------------------------------------------------*/
2336 +static int rh_unlink_urb (struct urb * urb)
2338 + ohci_t * ohci = urb->dev->bus->hcpriv;
2339 + unsigned int flags;
2341 + spin_lock_irqsave(&ohci->ohci_lock, flags);
2342 + if (ohci->rh.urb == urb) {
2343 + ohci->rh.send = 0;
2344 + del_timer (&ohci->rh.rh_int_timer);
2345 + ohci->rh.urb = NULL;
2347 + urb->hcpriv = NULL;
2348 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
2349 + usb_dec_dev_use(urb->dev);
2351 + if (urb->transfer_flags & USB_ASYNC_UNLINK) {
2352 + urb->status = -ECONNRESET;
2353 + if (urb->complete)
2354 + urb->complete (urb);
2356 + urb->status = -ENOENT;
2358 + spin_unlock_irqrestore(&ohci->ohci_lock, flags);
2363 +/*-------------------------------------------------------------------------*
2365 + *-------------------------------------------------------------------------*/
2367 +/* reset the HC and BUS */
2369 +static int hc_reset (ohci_t * ohci)
2372 + int smm_timeout = 50; /* 0,5 sec */
2375 + /* PA-RISC doesn't have SMM, but PDC might leave IR set */
2376 + if (readl (&ohci->regs->control) & OHCI_CTRL_IR) { /* SMM owns the HC */
2377 + writel (OHCI_OCR, &ohci->regs->cmdstatus); /* request ownership */
2378 + dbg("USB HC TakeOver from SMM");
2379 + while (readl (&ohci->regs->control) & OHCI_CTRL_IR) {
2381 + if (--smm_timeout == 0) {
2382 + err("USB HC TakeOver failed!");
2389 + /* Disable HC interrupts */
2390 + writel (OHCI_INTR_MIE, &ohci->regs->intrdisable);
2392 + dbg("USB HC reset_hc usb-%s: ctrl = 0x%x ;",
2393 + ohci->ohci_dev->slot_name,
2394 + readl (&ohci->regs->control));
2396 + /* Reset USB (needed by some controllers) */
2397 + writel (0, &ohci->regs->control);
2399 + /* Force a state change from USBRESET to USBOPERATIONAL for ALi */
2400 + (void) readl (&ohci->regs->control); /* PCI posting */
2401 + writel (ohci->hc_control = OHCI_USB_OPER, &ohci->regs->control);
2403 + /* HC Reset requires max 10 ms delay */
2404 + writel (OHCI_HCR, &ohci->regs->cmdstatus);
2405 + while ((readl (&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
2406 + if (--timeout == 0) {
2407 + err("USB HC reset timed out!");
2415 +/*-------------------------------------------------------------------------*/
2417 +/* Start an OHCI controller, set the BUS operational
2418 + * enable interrupts
2419 + * connect the virtual root hub */
2421 +static int hc_start (ohci_t * ohci)
2424 + unsigned int fminterval;
2425 + struct usb_device * usb_dev;
2426 + struct ohci_device * dev;
2428 + ohci->disabled = 1;
2430 + /* Tell the controller where the control and bulk lists are
2431 + * The lists are empty now. */
2433 + writel (0, &ohci->regs->ed_controlhead);
2434 + writel (0, &ohci->regs->ed_bulkhead);
2436 + writel (ohci->hcca_dma, &ohci->regs->hcca); /* a reset clears this */
2438 + fminterval = 0x2edf;
2439 + writel ((fminterval * 9) / 10, &ohci->regs->periodicstart);
2440 + fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
2441 + writel (fminterval, &ohci->regs->fminterval);
2442 + writel (0x628, &ohci->regs->lsthresh);
2444 + /* start controller operations */
2445 + ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
2446 + ohci->disabled = 0;
2447 + writel (ohci->hc_control, &ohci->regs->control);
2449 + /* Choose the interrupts we care about now, others later on demand */
2450 + mask = OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
2451 + writel (mask, &ohci->regs->intrenable);
2452 + writel (mask, &ohci->regs->intrstatus);
2454 +#ifdef OHCI_USE_NPS
2455 + if(ohci->flags & OHCI_QUIRK_SUCKYIO)
2457 + /* NSC 87560 at least requires different setup .. */
2458 + writel ((roothub_a (ohci) | RH_A_NOCP) &
2459 + ~(RH_A_OCPM | RH_A_POTPGT | RH_A_PSM | RH_A_NPS),
2460 + &ohci->regs->roothub.a);
2464 + /* required for AMD-756 and some Mac platforms */
2465 + writel ((roothub_a (ohci) | RH_A_NPS) & ~RH_A_PSM,
2466 + &ohci->regs->roothub.a);
2468 + writel (RH_HS_LPSC, &ohci->regs->roothub.status);
2469 +#endif /* OHCI_USE_NPS */
2471 + (void)readl (&ohci->regs->intrdisable); /* PCI posting flush */
2473 + // POTPGT delay is bits 24-31, in 2 ms units.
2474 + mdelay ((roothub_a (ohci) >> 23) & 0x1fe);
2476 + /* connect the virtual root hub */
2477 + ohci->rh.devnum = 0;
2478 + usb_dev = usb_alloc_dev (NULL, ohci->bus);
2480 + ohci->disabled = 1;
2484 + dev = usb_to_ohci (usb_dev);
2485 + ohci->bus->root_hub = usb_dev;
2486 + usb_connect (usb_dev);
2487 + if (usb_new_device (usb_dev) != 0) {
2488 + usb_free_dev (usb_dev);
2489 + ohci->disabled = 1;
2496 +/*-------------------------------------------------------------------------*/
2498 +/* called only from interrupt handler */
2500 +static void check_timeouts (struct ohci *ohci)
2502 + spin_lock (&ohci->ohci_lock);
2503 + while (!list_empty (&ohci->timeout_list)) {
2506 + urb = list_entry (ohci->timeout_list.next, struct urb, urb_list);
2507 + if (time_after (jiffies, urb->timeout))
2510 + list_del_init (&urb->urb_list);
2511 + if (urb->status != -EINPROGRESS)
2514 + urb->transfer_flags |= USB_TIMEOUT_KILLED | USB_ASYNC_UNLINK;
2515 + spin_unlock (&ohci->ohci_lock);
2517 + // outside the interrupt handler (in a timer...)
2518 + // this reference would race interrupts
2519 + sohci_unlink_urb (urb);
2521 + spin_lock (&ohci->ohci_lock);
2523 + spin_unlock (&ohci->ohci_lock);
2527 +/*-------------------------------------------------------------------------*/
2529 +/* an interrupt happens */
2531 +static void hc_interrupt (int irq, void * __ohci, struct pt_regs * r)
2533 + ohci_t * ohci = __ohci;
2534 + struct ohci_regs * regs = ohci->regs;
2537 + spin_lock (&ohci->ohci_lock);
2539 + /* avoid (slow) readl if only WDH happened */
2540 + if ((ohci->hcca->done_head != 0)
2541 + && !(le32_to_cpup (&ohci->hcca->done_head) & 0x01)) {
2542 + ints = OHCI_INTR_WDH;
2544 + /* cardbus/... hardware gone before remove() */
2545 + } else if ((ints = readl (®s->intrstatus)) == ~(u32)0) {
2547 + spin_unlock (&ohci->ohci_lock);
2548 + err ("%s device removed!", ohci->ohci_dev->slot_name);
2551 + /* interrupt for some other device? */
2552 + } else if ((ints &= readl (®s->intrenable)) == 0) {
2553 + spin_unlock (&ohci->ohci_lock);
2557 + // dbg("Interrupt: %x frame: %x", ints, le16_to_cpu (ohci->hcca->frame_no));
2559 + if (ints & OHCI_INTR_UE) {
2561 + err ("OHCI Unrecoverable Error, controller usb-%s disabled",
2562 + ohci->ohci_dev->slot_name);
2563 + // e.g. due to PCI Master/Target Abort
2566 + ohci_dump (ohci, 1);
2568 + // FIXME: be optimistic, hope that bug won't repeat often.
2569 + // Make some non-interrupt context restart the controller.
2570 + // Count and limit the retries though; either hardware or
2571 + // software errors can go forever...
2576 + if (ints & OHCI_INTR_WDH) {
2577 + writel (OHCI_INTR_WDH, ®s->intrdisable);
2578 + (void)readl (®s->intrdisable); /* PCI posting flush */
2579 + dl_done_list (ohci, dl_reverse_done_list (ohci));
2580 + writel (OHCI_INTR_WDH, ®s->intrenable);
2581 + (void)readl (®s->intrdisable); /* PCI posting flush */
2584 + if (ints & OHCI_INTR_SO) {
2585 + dbg("USB Schedule overrun");
2586 + writel (OHCI_INTR_SO, ®s->intrenable);
2587 + (void)readl (®s->intrdisable); /* PCI posting flush */
2590 + // FIXME: this assumes SOF (1/ms) interrupts don't get lost...
2591 + if (ints & OHCI_INTR_SF) {
2592 + unsigned int frame = le16_to_cpu (ohci->hcca->frame_no) & 1;
2593 + writel (OHCI_INTR_SF, ®s->intrdisable);
2594 + (void)readl (®s->intrdisable); /* PCI posting flush */
2595 + if (ohci->ed_rm_list[!frame] != NULL) {
2596 + dl_del_list (ohci, !frame);
2598 + if (ohci->ed_rm_list[frame] != NULL) {
2599 + writel (OHCI_INTR_SF, ®s->intrenable);
2600 + (void)readl (®s->intrdisable); /* PCI posting flush */
2605 + * Finally, we are done with trashing about our hardware lists
2606 + * and other CPUs are allowed in. The festive flipping of the lock
2607 + * ensues as we struggle with the check_timeouts disaster.
2609 + spin_unlock (&ohci->ohci_lock);
2611 + if (!list_empty (&ohci->timeout_list)) {
2612 + check_timeouts (ohci);
2613 +// FIXME: enable SF as needed in a timer;
2614 +// don't make lots of 1ms interrupts
2615 +// On unloaded USB, think 4k ~= 4-5msec
2616 + if (!list_empty (&ohci->timeout_list))
2617 + writel (OHCI_INTR_SF, ®s->intrenable);
2620 + writel (ints, ®s->intrstatus);
2621 + writel (OHCI_INTR_MIE, ®s->intrenable);
2622 + (void)readl (®s->intrdisable); /* PCI posting flush */
2624 + ohci_complete(ohci);
2627 +/*-------------------------------------------------------------------------*/
2629 +/* allocate OHCI */
2631 +static ohci_t * __devinit hc_alloc_ohci (struct pci_dev *dev, void * mem_base)
2635 + ohci = (ohci_t *) kmalloc (sizeof *ohci, GFP_KERNEL);
2639 + memset (ohci, 0, sizeof (ohci_t));
2641 + ohci->hcca = pci_alloc_consistent (dev, sizeof *ohci->hcca,
2643 + if (!ohci->hcca) {
2647 + memset (ohci->hcca, 0, sizeof (struct ohci_hcca));
2649 + ohci->disabled = 1;
2650 + ohci->sleeping = 0;
2652 + ohci->regs = mem_base;
2654 + ohci->ohci_dev = dev;
2655 + pci_set_drvdata(dev, ohci);
2657 + INIT_LIST_HEAD (&ohci->timeout_list);
2658 + spin_lock_init(&ohci->ohci_lock);
2660 + ohci->bus = usb_alloc_bus (&sohci_device_operations);
2662 + pci_set_drvdata (dev, NULL);
2663 + pci_free_consistent (ohci->ohci_dev, sizeof *ohci->hcca,
2664 + ohci->hcca, ohci->hcca_dma);
2668 + ohci->bus->bus_name = dev->slot_name;
2669 + ohci->bus->hcpriv = (void *) ohci;
2675 +/*-------------------------------------------------------------------------*/
2677 +/* De-allocate all resources.. */
2679 +static void hc_release_ohci (ohci_t * ohci)
2681 + dbg ("USB HC release ohci usb-%s", ohci->ohci_dev->slot_name);
2683 + /* disconnect all devices */
2684 + if (ohci->bus->root_hub)
2685 + usb_disconnect (&ohci->bus->root_hub);
2687 + if (!ohci->disabled)
2690 + if (ohci->irq >= 0) {
2691 + free_irq (ohci->irq, ohci);
2694 + pci_set_drvdata(ohci->ohci_dev, NULL);
2696 + if (ohci->bus->busnum != -1)
2697 + usb_deregister_bus (ohci->bus);
2699 + usb_free_bus (ohci->bus);
2702 + ohci_mem_cleanup (ohci);
2704 + /* unmap the IO address space */
2705 + iounmap (ohci->regs);
2707 + pci_free_consistent (ohci->ohci_dev, sizeof *ohci->hcca,
2708 + ohci->hcca, ohci->hcca_dma);
2712 +/*-------------------------------------------------------------------------*/
2714 +/* Increment the module usage count, start the control thread and
2715 + * return success. */
2717 +static struct pci_driver ohci_pci_driver;
2719 +static int __devinit
2720 +hc_found_ohci (struct pci_dev *dev, int irq,
2721 + void *mem_base, const struct pci_device_id *id)
2724 + char buf[8], *bufp = buf;
2728 + sprintf(buf, "%d", irq);
2730 + bufp = __irq_itoa(irq);
2732 + printk(KERN_INFO __FILE__ ": USB OHCI at membase 0x%lx, IRQ %s\n",
2733 + (unsigned long) mem_base, bufp);
2734 + printk(KERN_INFO __FILE__ ": usb-%s, %s\n", dev->slot_name, dev->name);
2736 + ohci = hc_alloc_ohci (dev, mem_base);
2740 + if ((ret = ohci_mem_init (ohci)) < 0) {
2741 + hc_release_ohci (ohci);
2744 + ohci->flags = id->driver_data;
2746 + /* Check for NSC87560. We have to look at the bridge (fn1) to identify
2747 + the USB (fn2). This quirk might apply to more or even all NSC stuff
2750 + if(dev->vendor == PCI_VENDOR_ID_NS)
2752 + struct pci_dev *fn1 = pci_find_slot(dev->bus->number, PCI_DEVFN(PCI_SLOT(dev->devfn), 1));
2753 + if(fn1 && fn1->vendor == PCI_VENDOR_ID_NS && fn1->device == PCI_DEVICE_ID_NS_87560_LIO)
2754 + ohci->flags |= OHCI_QUIRK_SUCKYIO;
2758 + if (ohci->flags & OHCI_QUIRK_SUCKYIO)
2759 + printk (KERN_INFO __FILE__ ": Using NSC SuperIO setup\n");
2760 + if (ohci->flags & OHCI_QUIRK_AMD756)
2761 + printk (KERN_INFO __FILE__ ": AMD756 erratum 4 workaround\n");
2763 + if (hc_reset (ohci) < 0) {
2764 + hc_release_ohci (ohci);
2768 + /* FIXME this is a second HC reset; why?? */
2769 + writel (ohci->hc_control = OHCI_USB_RESET, &ohci->regs->control);
2770 + (void)readl (&ohci->regs->intrdisable); /* PCI posting flush */
2773 + usb_register_bus (ohci->bus);
2775 + if (request_irq (irq, hc_interrupt, SA_SHIRQ,
2776 + ohci_pci_driver.name, ohci) != 0) {
2777 + err ("request interrupt %s failed", bufp);
2778 + hc_release_ohci (ohci);
2783 + if (hc_start (ohci) < 0) {
2784 + err ("can't start usb-%s", dev->slot_name);
2785 + hc_release_ohci (ohci);
2790 + ohci_dump (ohci, 1);
2795 +/*-------------------------------------------------------------------------*/
2799 +/* controller died; cleanup debris, then restart */
2800 +/* must not be called from interrupt context */
2802 +static void hc_restart (ohci_t *ohci)
2807 + if (ohci->pci_latency)
2808 + pci_write_config_byte (ohci->ohci_dev, PCI_LATENCY_TIMER, ohci->pci_latency);
2810 + ohci->disabled = 1;
2811 + ohci->sleeping = 0;
2812 + if (ohci->bus->root_hub)
2813 + usb_disconnect (&ohci->bus->root_hub);
2815 + /* empty the interrupt branches */
2816 + for (i = 0; i < NUM_INTS; i++) ohci->ohci_int_load[i] = 0;
2817 + for (i = 0; i < NUM_INTS; i++) ohci->hcca->int_table[i] = 0;
2819 + /* no EDs to remove */
2820 + ohci->ed_rm_list [0] = NULL;
2821 + ohci->ed_rm_list [1] = NULL;
2823 + /* empty control and bulk lists */
2824 + ohci->ed_isotail = NULL;
2825 + ohci->ed_controltail = NULL;
2826 + ohci->ed_bulktail = NULL;
2828 + if ((temp = hc_reset (ohci)) < 0 || (temp = hc_start (ohci)) < 0) {
2829 + err ("can't restart usb-%s, %d", ohci->ohci_dev->slot_name, temp);
2831 + dbg ("restart usb-%s completed", ohci->ohci_dev->slot_name);
2834 +#endif /* CONFIG_PM */
2836 +/*-------------------------------------------------------------------------*/
2838 +/* configured so that an OHCI device is always provided */
2839 +/* always called with process context; sleeping is OK */
2841 +static int __devinit
2842 +ohci_pci_probe (struct pci_dev *dev, const struct pci_device_id *id)
2844 + unsigned long mem_resource, mem_len;
2848 + if (pci_enable_device(dev) < 0)
2852 + err("found OHCI device with no IRQ assigned. check BIOS settings!");
2853 + pci_disable_device (dev);
2857 + /* we read its hardware registers as memory */
2858 + mem_resource = pci_resource_start(dev, 0);
2859 + mem_len = pci_resource_len(dev, 0);
2860 + if (!request_mem_region (mem_resource, mem_len, ohci_pci_driver.name)) {
2861 + dbg ("controller already in use");
2862 + pci_disable_device (dev);
2866 + mem_base = ioremap_nocache (mem_resource, mem_len);
2868 + err("Error mapping OHCI memory");
2869 + release_mem_region (mem_resource, mem_len);
2870 + pci_disable_device (dev);
2874 + /* controller writes into our memory */
2875 + pci_set_master (dev);
2877 + status = hc_found_ohci (dev, dev->irq, mem_base, id);
2879 + iounmap (mem_base);
2880 + release_mem_region (mem_resource, mem_len);
2881 + pci_disable_device (dev);
2886 +/*-------------------------------------------------------------------------*/
2888 +/* may be called from interrupt context [interface spec] */
2889 +/* may be called without controller present */
2890 +/* may be called with controller, bus, and devices active */
2892 +static void __devexit
2893 +ohci_pci_remove (struct pci_dev *dev)
2895 + ohci_t *ohci = pci_get_drvdata(dev);
2897 + dbg ("remove %s controller usb-%s%s%s",
2898 + hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS),
2900 + ohci->disabled ? " (disabled)" : "",
2901 + in_interrupt () ? " in interrupt" : ""
2904 + ohci_dump (ohci, 1);
2907 + /* don't wake up sleeping controllers, or block in interrupt context */
2908 + if ((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER || in_interrupt ()) {
2909 + dbg ("controller being disabled");
2910 + ohci->disabled = 1;
2913 + /* on return, USB will always be reset (if present) */
2914 + if (ohci->disabled)
2915 + writel (ohci->hc_control = OHCI_USB_RESET,
2916 + &ohci->regs->control);
2918 + hc_release_ohci (ohci);
2920 + release_mem_region (pci_resource_start (dev, 0), pci_resource_len (dev, 0));
2921 + pci_disable_device (dev);
2927 +/*-------------------------------------------------------------------------*/
2930 +ohci_pci_suspend (struct pci_dev *dev, u32 state)
2932 + ohci_t *ohci = pci_get_drvdata(dev);
2933 + unsigned long flags;
2936 + if ((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER) {
2937 + dbg ("can't suspend usb-%s (state is %s)", dev->slot_name,
2938 + hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS));
2942 + /* act as if usb suspend can always be used */
2943 + info ("USB suspend: usb-%s", dev->slot_name);
2944 + ohci->sleeping = 1;
2946 + /* First stop processing */
2947 + spin_lock_irqsave (&ohci->ohci_lock, flags);
2948 + ohci->hc_control &= ~(OHCI_CTRL_PLE|OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_IE);
2949 + writel (ohci->hc_control, &ohci->regs->control);
2950 + writel (OHCI_INTR_SF, &ohci->regs->intrstatus);
2951 + (void) readl (&ohci->regs->intrstatus);
2952 + spin_unlock_irqrestore (&ohci->ohci_lock, flags);
2954 + /* Wait a frame or two */
2956 + if (!readl (&ohci->regs->intrstatus) & OHCI_INTR_SF)
2959 +#ifdef CONFIG_PMAC_PBOOK
2960 + if (_machine == _MACH_Pmac)
2961 + disable_irq (ohci->irq);
2962 + /* else, 2.4 assumes shared irqs -- don't disable */
2964 + /* Enable remote wakeup */
2965 + writel (readl(&ohci->regs->intrenable) | OHCI_INTR_RD, &ohci->regs->intrenable);
2967 + /* Suspend chip and let things settle down a bit */
2968 + ohci->hc_control = OHCI_USB_SUSPEND;
2969 + writel (ohci->hc_control, &ohci->regs->control);
2970 + (void) readl (&ohci->regs->control);
2971 + mdelay (500); /* No schedule here ! */
2972 + switch (readl (&ohci->regs->control) & OHCI_CTRL_HCFS) {
2973 + case OHCI_USB_RESET:
2974 + dbg("Bus in reset phase ???");
2976 + case OHCI_USB_RESUME:
2977 + dbg("Bus in resume phase ???");
2979 + case OHCI_USB_OPER:
2980 + dbg("Bus in operational phase ???");
2982 + case OHCI_USB_SUSPEND:
2983 + dbg("Bus suspended");
2986 + /* In some rare situations, Apple's OHCI have happily trashed
2987 + * memory during sleep. We disable it's bus master bit during
2990 + pci_read_config_word (dev, PCI_COMMAND, &cmd);
2991 + cmd &= ~PCI_COMMAND_MASTER;
2992 + pci_write_config_word (dev, PCI_COMMAND, cmd);
2993 +#ifdef CONFIG_PMAC_PBOOK
2995 + struct device_node *of_node;
2997 + /* Disable USB PAD & cell clock */
2998 + of_node = pci_device_to_OF_node (ohci->ohci_dev);
3000 + pmac_call_feature(PMAC_FTR_USB_ENABLE, of_node, 0, 0);
3006 +/*-------------------------------------------------------------------------*/
3009 +ohci_pci_resume (struct pci_dev *dev)
3011 + ohci_t *ohci = pci_get_drvdata(dev);
3013 + unsigned long flags;
3015 + /* guard against multiple resumes */
3016 + atomic_inc (&ohci->resume_count);
3017 + if (atomic_read (&ohci->resume_count) != 1) {
3018 + err ("concurrent PCI resumes for usb-%s", dev->slot_name);
3019 + atomic_dec (&ohci->resume_count);
3023 +#ifdef CONFIG_PMAC_PBOOK
3025 + struct device_node *of_node;
3027 + /* Re-enable USB PAD & cell clock */
3028 + of_node = pci_device_to_OF_node (ohci->ohci_dev);
3030 + pmac_call_feature(PMAC_FTR_USB_ENABLE, of_node, 0, 1);
3034 + /* did we suspend, or were we powered off? */
3035 + ohci->hc_control = readl (&ohci->regs->control);
3036 + temp = ohci->hc_control & OHCI_CTRL_HCFS;
3039 + /* the registers may look crazy here */
3040 + ohci_dump_status (ohci);
3043 + /* Re-enable bus mastering */
3044 + pci_set_master(ohci->ohci_dev);
3048 + case OHCI_USB_RESET: // lost power
3049 + info ("USB restart: usb-%s", dev->slot_name);
3050 + hc_restart (ohci);
3053 + case OHCI_USB_SUSPEND: // host wakeup
3054 + case OHCI_USB_RESUME: // remote wakeup
3055 + info ("USB continue: usb-%s from %s wakeup", dev->slot_name,
3056 + (temp == OHCI_USB_SUSPEND)
3057 + ? "host" : "remote");
3058 + ohci->hc_control = OHCI_USB_RESUME;
3059 + writel (ohci->hc_control, &ohci->regs->control);
3060 + (void) readl (&ohci->regs->control);
3061 + mdelay (20); /* no schedule here ! */
3062 + /* Some controllers (lucent) need a longer delay here */
3064 + temp = readl (&ohci->regs->control);
3065 + temp = ohci->hc_control & OHCI_CTRL_HCFS;
3066 + if (temp != OHCI_USB_RESUME) {
3067 + err ("controller usb-%s won't resume", dev->slot_name);
3068 + ohci->disabled = 1;
3072 + /* Some chips likes being resumed first */
3073 + writel (OHCI_USB_OPER, &ohci->regs->control);
3074 + (void) readl (&ohci->regs->control);
3077 + /* Then re-enable operations */
3078 + spin_lock_irqsave (&ohci->ohci_lock, flags);
3079 + ohci->disabled = 0;
3080 + ohci->sleeping = 0;
3081 + ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
3082 + if (!ohci->ed_rm_list[0] && !ohci->ed_rm_list[1]) {
3083 + if (ohci->ed_controltail)
3084 + ohci->hc_control |= OHCI_CTRL_CLE;
3085 + if (ohci->ed_bulktail)
3086 + ohci->hc_control |= OHCI_CTRL_BLE;
3088 + writel (ohci->hc_control, &ohci->regs->control);
3089 + writel (OHCI_INTR_SF, &ohci->regs->intrstatus);
3090 + writel (OHCI_INTR_SF, &ohci->regs->intrenable);
3091 + /* Check for a pending done list */
3092 + writel (OHCI_INTR_WDH, &ohci->regs->intrdisable);
3093 + (void) readl (&ohci->regs->intrdisable);
3094 +#ifdef CONFIG_PMAC_PBOOK
3095 + if (_machine == _MACH_Pmac)
3096 + enable_irq (ohci->irq);
3098 + if (ohci->hcca->done_head)
3099 + dl_done_list (ohci, dl_reverse_done_list (ohci));
3100 + writel (OHCI_INTR_WDH, &ohci->regs->intrenable);
3101 + writel (OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
3102 + writel (OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
3103 + spin_unlock_irqrestore (&ohci->ohci_lock, flags);
3107 + warn ("odd PCI resume for usb-%s", dev->slot_name);
3110 + /* controller is operational, extra resumes are harmless */
3111 + atomic_dec (&ohci->resume_count);
3116 +#endif /* CONFIG_PM */
3119 +/*-------------------------------------------------------------------------*/
3121 +static const struct pci_device_id __devinitdata ohci_pci_ids [] = { {
3124 + * AMD-756 [Viper] USB has a serious erratum when used with
3125 + * lowspeed devices like mice.
3129 + subvendor: PCI_ANY_ID,
3130 + subdevice: PCI_ANY_ID,
3132 + driver_data: OHCI_QUIRK_AMD756,
3136 + /* handle any USB OHCI controller */
3137 + class: ((PCI_CLASS_SERIAL_USB << 8) | 0x10),
3140 + /* no matter who makes it */
3141 + vendor: PCI_ANY_ID,
3142 + device: PCI_ANY_ID,
3143 + subvendor: PCI_ANY_ID,
3144 + subdevice: PCI_ANY_ID,
3146 + }, { /* end: all zeroes */ }
3149 +MODULE_DEVICE_TABLE (pci, ohci_pci_ids);
3151 +static struct pci_driver ohci_pci_driver = {
3153 + id_table: &ohci_pci_ids [0],
3155 + probe: ohci_pci_probe,
3156 + remove: __devexit_p(ohci_pci_remove),
3159 + suspend: ohci_pci_suspend,
3160 + resume: ohci_pci_resume,
3165 +/*-------------------------------------------------------------------------*/
3167 +static int __init ohci_hcd_init (void)
3169 + return pci_module_init (&ohci_pci_driver);
3172 +/*-------------------------------------------------------------------------*/
3174 +static void __exit ohci_hcd_cleanup (void)
3176 + pci_unregister_driver (&ohci_pci_driver);
3179 +module_init (ohci_hcd_init);
3180 +module_exit (ohci_hcd_cleanup);
3183 +MODULE_AUTHOR( DRIVER_AUTHOR );
3184 +MODULE_DESCRIPTION( DRIVER_DESC );
3185 +MODULE_LICENSE("GPL");