Linux 2.4.0-test10pre4
[davej-history.git] / drivers / usb / usb-ohci.c
blob0d2957ca95979467d229fab97e25877e8de68b74
1 /*
2 * URB OHCI HCD (Host Controller Driver) for USB.
4 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5 * (C) Copyright 2000 David Brownell <david-b@pacbell.net>
6 *
7 * [ Initialisation is based on Linus' ]
8 * [ uhci code and gregs ohci fragments ]
9 * [ (C) Copyright 1999 Linus Torvalds ]
10 * [ (C) Copyright 1999 Gregory P. Smith]
13 * History:
15 * 2000/09/26 fixed races in removing the private portion of the urb
16 * 2000/09/07 disable bulk and control lists when unlinking the last
17 * endpoint descriptor in order to avoid unrecoverable errors on
18 * the Lucent chips.
19 * 2000/08/29 use bandwidth claiming hooks (thanks Randy!), fix some
20 * urb unlink probs, indentation fixes
21 * 2000/08/11 various oops fixes mostly affecting iso and cleanup from
22 * device unplugs.
23 * 2000/06/28 use PCI hotplug framework, for better power management
24 * and for Cardbus support (David Brownell)
25 * 2000/earlier: fixes for NEC/Lucent chips; suspend/resume handling
26 * when the controller loses power; handle UE; cleanup; ...
28 * v5.2 1999/12/07 URB 3rd preview,
29 * v5.1 1999/11/30 URB 2nd preview, cpia, (usb-scsi)
30 * v5.0 1999/11/22 URB Technical preview, Paul Mackerras powerbook susp/resume
31 * i386: HUB, Keyboard, Mouse, Printer
33 * v4.3 1999/10/27 multiple HCs, bulk_request
34 * v4.2 1999/09/05 ISO API alpha, new dev alloc, neg Error-codes
35 * v4.1 1999/08/27 Randy Dunlap's - ISO API first impl.
36 * v4.0 1999/08/18
37 * v3.0 1999/06/25
38 * v2.1 1999/05/09 code clean up
39 * v2.0 1999/05/04
40 * v1.0 1999/04/27 initial release
43 #include <linux/config.h>
44 #include <linux/module.h>
45 #include <linux/pci.h>
46 #include <linux/kernel.h>
47 #include <linux/delay.h>
48 #include <linux/ioport.h>
49 #include <linux/sched.h>
50 #include <linux/malloc.h>
51 #include <linux/smp_lock.h>
52 #include <linux/errno.h>
53 #include <linux/init.h>
54 #include <linux/timer.h>
55 #include <linux/list.h>
56 #include <linux/interrupt.h> /* for in_interrupt() */
57 #undef DEBUG
58 #include <linux/usb.h>
60 #include <asm/io.h>
61 #include <asm/irq.h>
62 #include <asm/system.h>
63 #include <asm/unaligned.h>
65 #define OHCI_USE_NPS // force NoPowerSwitching mode
66 // #define OHCI_VERBOSE_DEBUG /* not always helpful */
67 #define OHCI_MEM_SLAB
68 // #define OHCI_MEM_FLAGS SLAB_POISON /* no redzones; see mm/slab.c */
70 #include "usb-ohci.h"
73 #ifdef CONFIG_PMAC_PBOOK
74 /* All this PMAC_PBOOK stuff should disappear when those
75 * platforms fully support the 2.4 kernel PCI APIs.
77 #include <linux/adb.h>
78 #include <linux/pmu.h>
79 #ifndef CONFIG_PM
80 #define CONFIG_PM
81 #endif
82 #endif
85 /* For initializing controller (mask in an HCFS mode too) */
86 #define OHCI_CONTROL_INIT \
87 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
89 #define OHCI_UNLINK_TIMEOUT (HZ / 10)
91 static LIST_HEAD (ohci_hcd_list);
92 static spinlock_t usb_ed_lock = SPIN_LOCK_UNLOCKED;
94 /*-------------------------------------------------------------------------*
95 * URB support functions
96 *-------------------------------------------------------------------------*/
98 /* free HCD-private data associated with this URB */
100 static void urb_free_priv (struct ohci *hc, urb_priv_t * urb_priv)
102 int i;
104 for (i = 0; i < urb_priv->length; i++) {
105 if (urb_priv->td [i]) {
106 td_free (hc, urb_priv->td [i]);
110 kfree (urb_priv);
113 static void urb_rm_priv_locked (urb_t * urb)
115 urb_priv_t * urb_priv = urb->hcpriv;
117 if (urb_priv) {
118 urb->hcpriv = NULL;
120 /* Release int/iso bandwidth */
121 if (urb->bandwidth) {
122 switch (usb_pipetype(urb->pipe)) {
123 case PIPE_INTERRUPT:
124 usb_release_bandwidth (urb->dev, urb, 0);
125 break;
126 case PIPE_ISOCHRONOUS:
127 usb_release_bandwidth (urb->dev, urb, 1);
128 break;
129 default:
130 break;
134 urb_free_priv ((struct ohci *)urb->dev->bus, urb_priv);
135 usb_dec_dev_use (urb->dev);
136 urb->dev = NULL;
140 static void urb_rm_priv (urb_t * urb)
142 unsigned long flags;
144 spin_lock_irqsave (&usb_ed_lock, flags);
145 urb_rm_priv_locked (urb);
146 spin_unlock_irqrestore (&usb_ed_lock, flags);
149 /*-------------------------------------------------------------------------*/
151 #ifdef DEBUG
152 static int sohci_get_current_frame_number (struct usb_device * dev);
154 /* debug| print the main components of an URB
155 * small: 0) header + data packets 1) just header */
157 static void urb_print (urb_t * urb, char * str, int small)
159 unsigned int pipe= urb->pipe;
161 if (!urb->dev || !urb->dev->bus) {
162 dbg("%s URB: no dev", str);
163 return;
166 #ifndef OHCI_VERBOSE_DEBUG
167 if (urb->status != 0)
168 #endif
169 dbg("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,flags:%4x,len:%d/%d,stat:%d(%x)",
170 str,
171 sohci_get_current_frame_number (urb->dev),
172 usb_pipedevice (pipe),
173 usb_pipeendpoint (pipe),
174 usb_pipeout (pipe)? 'O': 'I',
175 usb_pipetype (pipe) < 2? (usb_pipeint (pipe)? "INTR": "ISOC"):
176 (usb_pipecontrol (pipe)? "CTRL": "BULK"),
177 urb->transfer_flags,
178 urb->actual_length,
179 urb->transfer_buffer_length,
180 urb->status, urb->status);
181 #ifdef OHCI_VERBOSE_DEBUG
182 if (!small) {
183 int i, len;
185 if (usb_pipecontrol (pipe)) {
186 printk (KERN_DEBUG __FILE__ ": cmd(8):");
187 for (i = 0; i < 8 ; i++)
188 printk (" %02x", ((__u8 *) urb->setup_packet) [i]);
189 printk ("\n");
191 if (urb->transfer_buffer_length > 0 && urb->transfer_buffer) {
192 printk (KERN_DEBUG __FILE__ ": data(%d/%d):",
193 urb->actual_length,
194 urb->transfer_buffer_length);
195 len = usb_pipeout (pipe)?
196 urb->transfer_buffer_length: urb->actual_length;
197 for (i = 0; i < 16 && i < len; i++)
198 printk (" %02x", ((__u8 *) urb->transfer_buffer) [i]);
199 printk ("%s stat:%d\n", i < len? "...": "", urb->status);
202 #endif
205 /* just for debugging; prints non-empty branches of the int ed tree inclusive iso eds*/
206 void ep_print_int_eds (ohci_t * ohci, char * str) {
207 int i, j;
208 __u32 * ed_p;
209 for (i= 0; i < 32; i++) {
210 j = 5;
211 ed_p = &(ohci->hcca.int_table [i]);
212 if (*ed_p == 0)
213 continue;
214 printk (KERN_DEBUG __FILE__ ": %s branch int %2d(%2x):", str, i, i);
215 while (*ed_p != 0 && j--) {
216 ed_t *ed = (ed_t *) bus_to_virt(le32_to_cpup(ed_p));
217 printk (" ed: %4x;", ed->hwINFO);
218 ed_p = &ed->hwNextED;
220 printk ("\n");
225 static void ohci_dump_intr_mask (char *label, __u32 mask)
227 dbg ("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
228 label,
229 mask,
230 (mask & OHCI_INTR_MIE) ? " MIE" : "",
231 (mask & OHCI_INTR_OC) ? " OC" : "",
232 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
233 (mask & OHCI_INTR_FNO) ? " FNO" : "",
234 (mask & OHCI_INTR_UE) ? " UE" : "",
235 (mask & OHCI_INTR_RD) ? " RD" : "",
236 (mask & OHCI_INTR_SF) ? " SF" : "",
237 (mask & OHCI_INTR_WDH) ? " WDH" : "",
238 (mask & OHCI_INTR_SO) ? " SO" : ""
242 static void maybe_print_eds (char *label, __u32 value)
244 if (value)
245 dbg ("%s %08x", label, value);
248 static char *hcfs2string (int state)
250 switch (state) {
251 case OHCI_USB_RESET: return "reset";
252 case OHCI_USB_RESUME: return "resume";
253 case OHCI_USB_OPER: return "operational";
254 case OHCI_USB_SUSPEND: return "suspend";
256 return "?";
259 // dump control and status registers
260 static void ohci_dump_status (ohci_t *controller)
262 struct ohci_regs *regs = controller->regs;
263 __u32 temp;
265 temp = readl (&regs->revision) & 0xff;
266 if (temp != 0x10)
267 dbg ("spec %d.%d", (temp >> 4), (temp & 0x0f));
269 temp = readl (&regs->control);
270 dbg ("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
271 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
272 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
273 (temp & OHCI_CTRL_IR) ? " IR" : "",
274 hcfs2string (temp & OHCI_CTRL_HCFS),
275 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
276 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
277 (temp & OHCI_CTRL_IE) ? " IE" : "",
278 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
279 temp & OHCI_CTRL_CBSR
282 temp = readl (&regs->cmdstatus);
283 dbg ("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
284 (temp & OHCI_SOC) >> 16,
285 (temp & OHCI_OCR) ? " OCR" : "",
286 (temp & OHCI_BLF) ? " BLF" : "",
287 (temp & OHCI_CLF) ? " CLF" : "",
288 (temp & OHCI_HCR) ? " HCR" : ""
291 ohci_dump_intr_mask ("intrstatus", readl (&regs->intrstatus));
292 ohci_dump_intr_mask ("intrenable", readl (&regs->intrenable));
293 // intrdisable always same as intrenable
294 // ohci_dump_intr_mask ("intrdisable", readl (&regs->intrdisable));
296 maybe_print_eds ("ed_periodcurrent", readl (&regs->ed_periodcurrent));
298 maybe_print_eds ("ed_controlhead", readl (&regs->ed_controlhead));
299 maybe_print_eds ("ed_controlcurrent", readl (&regs->ed_controlcurrent));
301 maybe_print_eds ("ed_bulkhead", readl (&regs->ed_bulkhead));
302 maybe_print_eds ("ed_bulkcurrent", readl (&regs->ed_bulkcurrent));
304 maybe_print_eds ("donehead", readl (&regs->donehead));
307 static void ohci_dump_roothub (ohci_t *controller, int verbose)
309 struct ohci_regs *regs = controller->regs;
310 __u32 temp, ndp, i;
312 temp = readl (&regs->roothub.a);
313 ndp = (temp & RH_A_NDP);
315 if (verbose) {
316 dbg ("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
317 ((temp & RH_A_POTPGT) >> 24) & 0xff,
318 (temp & RH_A_NOCP) ? " NOCP" : "",
319 (temp & RH_A_OCPM) ? " OCPM" : "",
320 (temp & RH_A_DT) ? " DT" : "",
321 (temp & RH_A_NPS) ? " NPS" : "",
322 (temp & RH_A_PSM) ? " PSM" : "",
325 temp = readl (&regs->roothub.b);
326 dbg ("roothub.b: %08x PPCM=%04x DR=%04x",
327 temp,
328 (temp & RH_B_PPCM) >> 16,
329 (temp & RH_B_DR)
331 temp = readl (&regs->roothub.status);
332 dbg ("roothub.status: %08x%s%s%s%s%s%s",
333 temp,
334 (temp & RH_HS_CRWE) ? " CRWE" : "",
335 (temp & RH_HS_OCIC) ? " OCIC" : "",
336 (temp & RH_HS_LPSC) ? " LPSC" : "",
337 (temp & RH_HS_DRWE) ? " DRWE" : "",
338 (temp & RH_HS_OCI) ? " OCI" : "",
339 (temp & RH_HS_LPS) ? " LPS" : ""
343 for (i = 0; i < ndp; i++) {
344 temp = readl (&regs->roothub.portstatus [i]);
345 dbg ("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
347 temp,
348 (temp & RH_PS_PRSC) ? " PRSC" : "",
349 (temp & RH_PS_OCIC) ? " OCIC" : "",
350 (temp & RH_PS_PSSC) ? " PSSC" : "",
351 (temp & RH_PS_PESC) ? " PESC" : "",
352 (temp & RH_PS_CSC) ? " CSC" : "",
354 (temp & RH_PS_LSDA) ? " LSDA" : "",
355 (temp & RH_PS_PPS) ? " PPS" : "",
356 (temp & RH_PS_PRS) ? " PRS" : "",
357 (temp & RH_PS_POCI) ? " POCI" : "",
358 (temp & RH_PS_PSS) ? " PSS" : "",
360 (temp & RH_PS_PES) ? " PES" : "",
361 (temp & RH_PS_CCS) ? " CCS" : ""
366 static void ohci_dump (ohci_t *controller, int verbose)
368 dbg ("OHCI controller usb-%s state", controller->ohci_dev->slot_name);
370 // dumps some of the state we know about
371 ohci_dump_status (controller);
372 if (verbose)
373 ep_print_int_eds (controller, "hcca");
374 dbg ("hcca frame #%04x", controller->hcca.frame_no);
375 ohci_dump_roothub (controller, 1);
379 #endif
381 /*-------------------------------------------------------------------------*
382 * Interface functions (URB)
383 *-------------------------------------------------------------------------*/
385 /* return a request to the completion handler */
387 static int sohci_return_urb (urb_t * urb)
389 urb_priv_t * urb_priv = urb->hcpriv;
390 urb_t * urbt;
391 unsigned long flags;
392 int i;
394 if (!urb_priv)
395 return -1; /* urb already unlinked */
397 /* just to be sure */
398 if (!urb->complete) {
399 urb_rm_priv (urb);
400 return -1;
403 #ifdef DEBUG
404 urb_print (urb, "RET", usb_pipeout (urb->pipe));
405 #endif
407 switch (usb_pipetype (urb->pipe)) {
408 case PIPE_INTERRUPT:
409 urb->complete (urb); /* call complete and requeue URB */
410 urb->actual_length = 0;
411 urb->status = USB_ST_URB_PENDING;
412 if (urb_priv->state != URB_DEL)
413 td_submit_urb (urb);
414 break;
416 case PIPE_ISOCHRONOUS:
417 for (urbt = urb->next; urbt && (urbt != urb); urbt = urbt->next);
418 if (urbt) { /* send the reply and requeue URB */
419 urb->complete (urb);
421 spin_lock_irqsave (&usb_ed_lock, flags);
422 urb->actual_length = 0;
423 urb->status = USB_ST_URB_PENDING;
424 urb->start_frame = urb_priv->ed->last_iso + 1;
425 if (urb_priv->state != URB_DEL) {
426 for (i = 0; i < urb->number_of_packets; i++) {
427 urb->iso_frame_desc[i].actual_length = 0;
428 urb->iso_frame_desc[i].status = -EXDEV;
430 td_submit_urb (urb);
432 spin_unlock_irqrestore (&usb_ed_lock, flags);
434 } else { /* unlink URB, call complete */
435 urb_rm_priv (urb);
436 urb->complete (urb);
438 break;
440 case PIPE_BULK:
441 case PIPE_CONTROL: /* unlink URB, call complete */
442 urb_rm_priv (urb);
443 urb->complete (urb);
444 break;
446 return 0;
449 /*-------------------------------------------------------------------------*/
451 /* get a transfer request */
453 static int sohci_submit_urb (urb_t * urb)
455 ohci_t * ohci;
456 ed_t * ed;
457 urb_priv_t * urb_priv;
458 unsigned int pipe = urb->pipe;
459 int i, size = 0;
460 unsigned long flags;
461 int bustime = 0;
463 if (!urb->dev || !urb->dev->bus)
464 return -ENODEV;
466 if (urb->hcpriv) /* urb already in use */
467 return -EINVAL;
469 // if(usb_endpoint_halted (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)))
470 // return -EPIPE;
472 usb_inc_dev_use (urb->dev);
473 ohci = (ohci_t *) urb->dev->bus->hcpriv;
475 #ifdef DEBUG
476 urb_print (urb, "SUB", usb_pipein (pipe));
477 #endif
479 /* handle a request to the virtual root hub */
480 if (usb_pipedevice (pipe) == ohci->rh.devnum)
481 return rh_submit_urb (urb);
483 /* when controller's hung, permit only roothub cleanup attempts
484 * such as powering down ports */
485 if (ohci->disabled) {
486 usb_dec_dev_use (urb->dev);
487 return -ESHUTDOWN;
490 /* every endpoint has a ed, locate and fill it */
491 if (!(ed = ep_add_ed (urb->dev, pipe, urb->interval, 1))) {
492 usb_dec_dev_use (urb->dev);
493 return -ENOMEM;
496 /* for the private part of the URB we need the number of TDs (size) */
497 switch (usb_pipetype (pipe)) {
498 case PIPE_BULK: /* one TD for every 4096 Byte */
499 size = (urb->transfer_buffer_length - 1) / 4096 + 1;
500 break;
501 case PIPE_ISOCHRONOUS: /* number of packets from URB */
502 size = urb->number_of_packets;
503 if (size <= 0) {
504 usb_dec_dev_use (urb->dev);
505 return -EINVAL;
507 for (i = 0; i < urb->number_of_packets; i++) {
508 urb->iso_frame_desc[i].actual_length = 0;
509 urb->iso_frame_desc[i].status = -EXDEV;
511 break;
512 case PIPE_CONTROL: /* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
513 size = (urb->transfer_buffer_length == 0)? 2:
514 (urb->transfer_buffer_length - 1) / 4096 + 3;
515 break;
516 case PIPE_INTERRUPT: /* one TD */
517 size = 1;
518 break;
521 /* allocate the private part of the URB */
522 urb_priv = kmalloc (sizeof (urb_priv_t) + size * sizeof (td_t *),
523 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
524 if (!urb_priv) {
525 usb_dec_dev_use (urb->dev);
526 return -ENOMEM;
528 memset (urb_priv, 0, sizeof (urb_priv_t) + size * sizeof (td_t *));
530 /* fill the private part of the URB */
531 urb_priv->length = size;
532 urb_priv->ed = ed;
534 /* allocate the TDs */
535 for (i = 0; i < size; i++) {
536 urb_priv->td[i] = td_alloc (ohci);
537 if (!urb_priv->td[i]) {
538 urb_free_priv (ohci, urb_priv);
539 usb_dec_dev_use (urb->dev);
540 return -ENOMEM;
544 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
545 urb_free_priv (ohci, urb_priv);
546 usb_dec_dev_use (urb->dev);
547 return -EINVAL;
550 /* allocate and claim bandwidth if needed; ISO
551 * needs start frame index if it was't provided.
553 switch (usb_pipetype (pipe)) {
554 case PIPE_ISOCHRONOUS:
555 if (urb->transfer_flags & USB_ISO_ASAP) {
556 urb->start_frame = ((ed->state == ED_OPER)
557 ? (ed->last_iso + 1)
558 : (le16_to_cpu (ohci->hcca.frame_no) + 10)) & 0xffff;
560 /* FALLTHROUGH */
561 case PIPE_INTERRUPT:
562 if (urb->bandwidth == 0) {
563 bustime = usb_check_bandwidth (urb->dev, urb);
565 if (bustime < 0) {
566 urb_free_priv (ohci, urb_priv);
567 usb_dec_dev_use (urb->dev);
568 return bustime;
570 usb_claim_bandwidth (urb->dev, urb, bustime, usb_pipeisoc (urb->pipe));
573 spin_lock_irqsave (&usb_ed_lock, flags);
574 urb->actual_length = 0;
575 urb->hcpriv = urb_priv;
576 urb->status = USB_ST_URB_PENDING;
578 /* link the ed into a chain if is not already */
579 if (ed->state != ED_OPER)
580 ep_link (ohci, ed);
582 /* fill the TDs and link it to the ed */
583 td_submit_urb (urb);
584 spin_unlock_irqrestore (&usb_ed_lock, flags);
586 return 0;
589 /*-------------------------------------------------------------------------*/
591 /* deactivate all TDs and remove the private part of the URB */
592 /* interrupt callers must use async unlink mode */
594 static int sohci_unlink_urb (urb_t * urb)
596 unsigned long flags;
597 ohci_t * ohci;
599 if (!urb) /* just to be sure */
600 return -EINVAL;
602 if (!urb->dev || !urb->dev->bus)
603 return -ENODEV;
605 ohci = (ohci_t *) urb->dev->bus->hcpriv;
607 #ifdef DEBUG
608 urb_print (urb, "UNLINK", 1);
609 #endif
611 /* handle a request to the virtual root hub */
612 if (usb_pipedevice (urb->pipe) == ohci->rh.devnum)
613 return rh_unlink_urb (urb);
615 if (urb->hcpriv && (urb->status == USB_ST_URB_PENDING)) {
616 if (!ohci->disabled) {
617 urb_priv_t * urb_priv;
619 /* interrupt code may not sleep; it must use
620 * async status return to unlink pending urbs.
622 if (!(urb->transfer_flags & USB_ASYNC_UNLINK)
623 && in_interrupt ()) {
624 err ("bug in call from %p; use async!",
625 __builtin_return_address(0));
626 return -EWOULDBLOCK;
629 /* flag the urb and its TDs for deletion in some
630 * upcoming SF interrupt delete list processing
632 spin_lock_irqsave (&usb_ed_lock, flags);
633 urb_priv = urb->hcpriv;
635 if (!urb_priv || (urb_priv->state == URB_DEL)) {
636 spin_unlock_irqrestore (&usb_ed_lock, flags);
637 return 0;
640 urb_priv->state = URB_DEL;
641 ep_rm_ed (urb->dev, urb_priv->ed);
642 urb_priv->ed->state |= ED_URB_DEL;
644 if (!(urb->transfer_flags & USB_ASYNC_UNLINK)) {
645 DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup);
646 DECLARE_WAITQUEUE (wait, current);
647 int timeout = OHCI_UNLINK_TIMEOUT;
649 add_wait_queue (&unlink_wakeup, &wait);
650 urb_priv->wait = &unlink_wakeup;
651 spin_unlock_irqrestore (&usb_ed_lock, flags);
653 /* wait until all TDs are deleted */
654 set_current_state(TASK_UNINTERRUPTIBLE);
655 while (timeout && (urb->status == USB_ST_URB_PENDING))
656 timeout = schedule_timeout (timeout);
657 remove_wait_queue (&unlink_wakeup, &wait);
658 if (urb->status == USB_ST_URB_PENDING) {
659 err ("unlink URB timeout");
660 return -ETIMEDOUT;
662 } else {
663 /* usb_dec_dev_use done in dl_del_list() */
664 urb->status = -EINPROGRESS;
665 spin_unlock_irqrestore (&usb_ed_lock, flags);
667 } else {
668 urb_rm_priv (urb);
669 if (urb->transfer_flags & USB_ASYNC_UNLINK) {
670 urb->status = -ECONNRESET;
671 if (urb->complete)
672 urb->complete (urb);
673 } else
674 urb->status = -ENOENT;
677 return 0;
680 /*-------------------------------------------------------------------------*/
682 /* allocate private data space for a usb device */
684 static int sohci_alloc_dev (struct usb_device *usb_dev)
686 struct ohci_device * dev;
688 /* FIXME: ED allocation with pci_consistent memory
689 * must know the controller ... either pass it in here,
690 * or decouple ED allocation from dev allocation.
692 dev = dev_alloc (NULL);
693 if (!dev)
694 return -ENOMEM;
696 memset (dev, 0, sizeof (*dev));
698 usb_dev->hcpriv = dev;
700 return 0;
703 /*-------------------------------------------------------------------------*/
705 /* may be called from interrupt context */
706 /* frees private data space of usb device */
708 static int sohci_free_dev (struct usb_device * usb_dev)
710 unsigned long flags;
711 int i, cnt = 0;
712 ed_t * ed;
713 struct ohci_device * dev = usb_to_ohci (usb_dev);
714 ohci_t * ohci = usb_dev->bus->hcpriv;
716 if (!dev)
717 return 0;
719 if (usb_dev->devnum >= 0) {
721 /* driver disconnects should have unlinked all urbs
722 * (freeing all the TDs, unlinking EDs) but we need
723 * to defend against bugs that prevent that.
725 spin_lock_irqsave (&usb_ed_lock, flags);
726 for(i = 0; i < NUM_EDS; i++) {
727 ed = &(dev->ed[i]);
728 if (ed->state != ED_NEW) {
729 if (ed->state == ED_OPER) {
730 /* driver on that interface didn't unlink an urb */
731 dbg ("driver usb-%s dev %d ed 0x%x unfreed URB",
732 ohci->ohci_dev->slot_name, usb_dev->devnum, i);
733 ep_unlink (ohci, ed);
735 ep_rm_ed (usb_dev, ed);
736 ed->state = ED_DEL;
737 cnt++;
740 spin_unlock_irqrestore (&usb_ed_lock, flags);
742 /* if the controller is running, tds for those unlinked
743 * urbs get freed by dl_del_list at the next SF interrupt
745 if (cnt > 0) {
747 if (ohci->disabled) {
748 /* FIXME: Something like this should kick in,
749 * though it's currently an exotic case ...
750 * the controller won't ever be touching
751 * these lists again!!
752 dl_del_list (ohci,
753 le16_to_cpu (ohci->hcca.frame_no) & 1);
755 warn ("TD leak, %d", cnt);
757 } else if (!in_interrupt ()) {
758 DECLARE_WAIT_QUEUE_HEAD (freedev_wakeup);
759 DECLARE_WAITQUEUE (wait, current);
760 int timeout = OHCI_UNLINK_TIMEOUT;
762 /* SF interrupt handler calls dl_del_list */
763 add_wait_queue (&freedev_wakeup, &wait);
764 dev->wait = &freedev_wakeup;
765 set_current_state(TASK_UNINTERRUPTIBLE);
766 while (timeout && dev->ed_cnt)
767 timeout = schedule_timeout (timeout);
768 remove_wait_queue (&freedev_wakeup, &wait);
769 if (dev->ed_cnt) {
770 err ("free device %d timeout", usb_dev->devnum);
771 return -ETIMEDOUT;
773 } else {
774 /* likely some interface's driver has a refcount bug */
775 err ("bus %s devnum %d deletion in interrupt",
776 ohci->ohci_dev->slot_name, usb_dev->devnum);
777 BUG ();
782 /* free device, and associated EDs */
783 dev_free (dev);
785 return 0;
788 /*-------------------------------------------------------------------------*/
790 /* tell us the current USB frame number */
792 static int sohci_get_current_frame_number (struct usb_device *usb_dev)
794 ohci_t * ohci = usb_dev->bus->hcpriv;
796 return le16_to_cpu (ohci->hcca.frame_no);
799 /*-------------------------------------------------------------------------*/
801 struct usb_operations sohci_device_operations = {
802 sohci_alloc_dev,
803 sohci_free_dev,
804 sohci_get_current_frame_number,
805 sohci_submit_urb,
806 sohci_unlink_urb
809 /*-------------------------------------------------------------------------*
810 * ED handling functions
811 *-------------------------------------------------------------------------*/
813 /* search for the right branch to insert an interrupt ed into the int tree
814 * do some load ballancing;
815 * returns the branch and
816 * sets the interval to interval = 2^integer (ld (interval)) */
818 static int ep_int_ballance (ohci_t * ohci, int interval, int load)
820 int i, branch = 0;
822 /* search for the least loaded interrupt endpoint branch of all 32 branches */
823 for (i = 0; i < 32; i++)
824 if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i]) branch = i;
826 branch = branch % interval;
827 for (i = branch; i < 32; i += interval) ohci->ohci_int_load [i] += load;
829 return branch;
832 /*-------------------------------------------------------------------------*/
834 /* 2^int( ld (inter)) */
836 static int ep_2_n_interval (int inter)
838 int i;
839 for (i = 0; ((inter >> i) > 1 ) && (i < 5); i++);
840 return 1 << i;
843 /*-------------------------------------------------------------------------*/
845 /* the int tree is a binary tree
846 * in order to process it sequentially the indexes of the branches have to be mapped
847 * the mapping reverses the bits of a word of num_bits length */
849 static int ep_rev (int num_bits, int word)
851 int i, wout = 0;
853 for (i = 0; i < num_bits; i++) wout |= (((word >> i) & 1) << (num_bits - i - 1));
854 return wout;
857 /*-------------------------------------------------------------------------*/
859 /* link an ed into one of the HC chains */
861 static int ep_link (ohci_t * ohci, ed_t * edi)
863 int int_branch;
864 int i;
865 int inter;
866 int interval;
867 int load;
868 __u32 * ed_p;
869 volatile ed_t * ed = edi;
871 ed->state = ED_OPER;
873 switch (ed->type) {
874 case PIPE_CONTROL:
875 ed->hwNextED = 0;
876 if (ohci->ed_controltail == NULL) {
877 writel (virt_to_bus (ed), &ohci->regs->ed_controlhead);
878 } else {
879 ohci->ed_controltail->hwNextED = cpu_to_le32 (virt_to_bus (ed));
881 ed->ed_prev = ohci->ed_controltail;
882 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
883 !ohci->ed_rm_list[1]) {
884 ohci->hc_control |= OHCI_CTRL_CLE;
885 writel (ohci->hc_control, &ohci->regs->control);
887 ohci->ed_controltail = edi;
888 break;
890 case PIPE_BULK:
891 ed->hwNextED = 0;
892 if (ohci->ed_bulktail == NULL) {
893 writel (virt_to_bus (ed), &ohci->regs->ed_bulkhead);
894 } else {
895 ohci->ed_bulktail->hwNextED = cpu_to_le32 (virt_to_bus (ed));
897 ed->ed_prev = ohci->ed_bulktail;
898 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
899 !ohci->ed_rm_list[1]) {
900 ohci->hc_control |= OHCI_CTRL_BLE;
901 writel (ohci->hc_control, &ohci->regs->control);
903 ohci->ed_bulktail = edi;
904 break;
906 case PIPE_INTERRUPT:
907 load = ed->int_load;
908 interval = ep_2_n_interval (ed->int_period);
909 ed->int_interval = interval;
910 int_branch = ep_int_ballance (ohci, interval, load);
911 ed->int_branch = int_branch;
913 for (i = 0; i < ep_rev (6, interval); i += inter) {
914 inter = 1;
915 for (ed_p = &(ohci->hcca.int_table[ep_rev (5, i) + int_branch]);
916 (*ed_p != 0) && (((ed_t *) bus_to_virt (le32_to_cpup (ed_p)))->int_interval >= interval);
917 ed_p = &(((ed_t *) bus_to_virt (le32_to_cpup (ed_p)))->hwNextED))
918 inter = ep_rev (6, ((ed_t *) bus_to_virt (le32_to_cpup (ed_p)))->int_interval);
919 ed->hwNextED = *ed_p;
920 *ed_p = cpu_to_le32 (virt_to_bus (ed));
922 #ifdef DEBUG
923 ep_print_int_eds (ohci, "LINK_INT");
924 #endif
925 break;
927 case PIPE_ISOCHRONOUS:
928 ed->hwNextED = 0;
929 ed->int_interval = 1;
930 if (ohci->ed_isotail != NULL) {
931 ohci->ed_isotail->hwNextED = cpu_to_le32 (virt_to_bus (ed));
932 ed->ed_prev = ohci->ed_isotail;
933 } else {
934 for ( i = 0; i < 32; i += inter) {
935 inter = 1;
936 for (ed_p = &(ohci->hcca.int_table[ep_rev (5, i)]);
937 *ed_p != 0;
938 ed_p = &(((ed_t *) bus_to_virt (le32_to_cpup (ed_p)))->hwNextED))
939 inter = ep_rev (6, ((ed_t *) bus_to_virt (le32_to_cpup (ed_p)))->int_interval);
940 *ed_p = cpu_to_le32 (virt_to_bus (ed));
942 ed->ed_prev = NULL;
944 ohci->ed_isotail = edi;
945 #ifdef DEBUG
946 ep_print_int_eds (ohci, "LINK_ISO");
947 #endif
948 break;
950 return 0;
953 /*-------------------------------------------------------------------------*/
955 /* unlink an ed from one of the HC chains.
956 * just the link to the ed is unlinked.
957 * the link from the ed still points to another operational ed or 0
958 * so the HC can eventually finish the processing of the unlinked ed */
960 static int ep_unlink (ohci_t * ohci, ed_t * ed)
962 int int_branch;
963 int i;
964 int inter;
965 int interval;
966 __u32 * ed_p;
968 ed->hwINFO |= cpu_to_le32 (OHCI_ED_SKIP);
970 switch (ed->type) {
971 case PIPE_CONTROL:
972 if (ed->ed_prev == NULL) {
973 if (!ed->hwNextED) {
974 ohci->hc_control &= ~OHCI_CTRL_CLE;
975 writel (ohci->hc_control, &ohci->regs->control);
977 writel (le32_to_cpup (&ed->hwNextED), &ohci->regs->ed_controlhead);
978 } else {
979 ed->ed_prev->hwNextED = ed->hwNextED;
981 if (ohci->ed_controltail == ed) {
982 ohci->ed_controltail = ed->ed_prev;
983 } else {
984 ((ed_t *) bus_to_virt (le32_to_cpup (&ed->hwNextED)))->ed_prev = ed->ed_prev;
986 break;
988 case PIPE_BULK:
989 if (ed->ed_prev == NULL) {
990 if (!ed->hwNextED) {
991 ohci->hc_control &= ~OHCI_CTRL_BLE;
992 writel (ohci->hc_control, &ohci->regs->control);
994 writel (le32_to_cpup (&ed->hwNextED), &ohci->regs->ed_bulkhead);
995 } else {
996 ed->ed_prev->hwNextED = ed->hwNextED;
998 if (ohci->ed_bulktail == ed) {
999 ohci->ed_bulktail = ed->ed_prev;
1000 } else {
1001 ((ed_t *) bus_to_virt (le32_to_cpup (&ed->hwNextED)))->ed_prev = ed->ed_prev;
1003 break;
1005 case PIPE_INTERRUPT:
1006 int_branch = ed->int_branch;
1007 interval = ed->int_interval;
1009 for (i = 0; i < ep_rev (6, interval); i += inter) {
1010 for (ed_p = &(ohci->hcca.int_table[ep_rev (5, i) + int_branch]), inter = 1;
1011 (*ed_p != 0) && (*ed_p != ed->hwNextED);
1012 ed_p = &(((ed_t *) bus_to_virt (le32_to_cpup (ed_p)))->hwNextED),
1013 inter = ep_rev (6, ((ed_t *) bus_to_virt (le32_to_cpup (ed_p)))->int_interval)) {
1014 if(((ed_t *) bus_to_virt (le32_to_cpup (ed_p))) == ed) {
1015 *ed_p = ed->hwNextED;
1016 break;
1020 for (i = int_branch; i < 32; i += interval)
1021 ohci->ohci_int_load[i] -= ed->int_load;
1022 #ifdef DEBUG
1023 ep_print_int_eds (ohci, "UNLINK_INT");
1024 #endif
1025 break;
1027 case PIPE_ISOCHRONOUS:
1028 if (ohci->ed_isotail == ed)
1029 ohci->ed_isotail = ed->ed_prev;
1030 if (ed->hwNextED != 0)
1031 ((ed_t *) bus_to_virt (le32_to_cpup (&ed->hwNextED)))->ed_prev = ed->ed_prev;
1033 if (ed->ed_prev != NULL) {
1034 ed->ed_prev->hwNextED = ed->hwNextED;
1035 } else {
1036 for (i = 0; i < 32; i++) {
1037 for (ed_p = &(ohci->hcca.int_table[ep_rev (5, i)]);
1038 *ed_p != 0;
1039 ed_p = &(((ed_t *) bus_to_virt (le32_to_cpup (ed_p)))->hwNextED)) {
1040 // inter = ep_rev (6, ((ed_t *) bus_to_virt (le32_to_cpup (ed_p)))->int_interval);
1041 if(((ed_t *) bus_to_virt (le32_to_cpup (ed_p))) == ed) {
1042 *ed_p = ed->hwNextED;
1043 break;
1048 #ifdef DEBUG
1049 ep_print_int_eds (ohci, "UNLINK_ISO");
1050 #endif
1051 break;
1053 ed->state = ED_UNLINK;
1054 return 0;
1058 /*-------------------------------------------------------------------------*/
1060 /* add/reinit an endpoint; this should be done once at the usb_set_configuration command,
1061 * but the USB stack is a little bit stateless so we do it at every transaction
1062 * if the state of the ed is ED_NEW then a dummy td is added and the state is changed to ED_UNLINK
1063 * in all other cases the state is left unchanged
1064 * the ed info fields are setted anyway even though most of them should not change */
1066 static ed_t * ep_add_ed (struct usb_device * usb_dev, unsigned int pipe, int interval, int load)
1068 ohci_t * ohci = usb_dev->bus->hcpriv;
1069 td_t * td;
1070 ed_t * ed_ret;
1071 volatile ed_t * ed;
1072 unsigned long flags;
1075 spin_lock_irqsave (&usb_ed_lock, flags);
1077 ed = ed_ret = &(usb_to_ohci (usb_dev)->ed[(usb_pipeendpoint (pipe) << 1) |
1078 (usb_pipecontrol (pipe)? 0: usb_pipeout (pipe))]);
1080 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
1081 /* pending delete request */
1082 spin_unlock_irqrestore (&usb_ed_lock, flags);
1083 return NULL;
1086 if (ed->state == ED_NEW) {
1087 ed->hwINFO = cpu_to_le32 (OHCI_ED_SKIP); /* skip ed */
1088 /* dummy td; end of td list for ed */
1089 td = td_alloc (ohci);
1090 if (!td) {
1091 /* out of memory */
1092 spin_unlock_irqrestore (&usb_ed_lock, flags);
1093 return NULL;
1095 ed->hwTailP = cpu_to_le32 (virt_to_bus (td));
1096 ed->hwHeadP = ed->hwTailP;
1097 ed->state = ED_UNLINK;
1098 ed->type = usb_pipetype (pipe);
1099 usb_to_ohci (usb_dev)->ed_cnt++;
1102 ohci->dev[usb_pipedevice (pipe)] = usb_dev;
1104 ed->hwINFO = cpu_to_le32 (usb_pipedevice (pipe)
1105 | usb_pipeendpoint (pipe) << 7
1106 | (usb_pipeisoc (pipe)? 0x8000: 0)
1107 | (usb_pipecontrol (pipe)? 0: (usb_pipeout (pipe)? 0x800: 0x1000))
1108 | usb_pipeslow (pipe) << 13
1109 | usb_maxpacket (usb_dev, pipe, usb_pipeout (pipe)) << 16);
1111 if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
1112 ed->int_period = interval;
1113 ed->int_load = load;
1116 spin_unlock_irqrestore (&usb_ed_lock, flags);
1117 return ed_ret;
1120 /*-------------------------------------------------------------------------*/
1122 /* request the removal of an endpoint
1123 * put the ep on the rm_list and request a stop of the bulk or ctrl list
1124 * real removal is done at the next start frame (SF) hardware interrupt */
1126 static void ep_rm_ed (struct usb_device * usb_dev, ed_t * ed)
1128 unsigned int frame;
1129 ohci_t * ohci = usb_dev->bus->hcpriv;
1131 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL))
1132 return;
1134 ed->hwINFO |= cpu_to_le32 (OHCI_ED_SKIP);
1136 if (!ohci->disabled) {
1137 switch (ed->type) {
1138 case PIPE_CONTROL: /* stop control list */
1139 ohci->hc_control &= ~OHCI_CTRL_CLE;
1140 writel (ohci->hc_control, &ohci->regs->control);
1141 break;
1142 case PIPE_BULK: /* stop bulk list */
1143 ohci->hc_control &= ~OHCI_CTRL_BLE;
1144 writel (ohci->hc_control, &ohci->regs->control);
1145 break;
1149 frame = le16_to_cpu (ohci->hcca.frame_no) & 0x1;
1150 ed->ed_rm_list = ohci->ed_rm_list[frame];
1151 ohci->ed_rm_list[frame] = ed;
1153 if (!ohci->disabled) {
1154 /* enable SOF interrupt */
1155 writel (OHCI_INTR_SF, &ohci->regs->intrstatus);
1156 writel (OHCI_INTR_SF, &ohci->regs->intrenable);
1160 /*-------------------------------------------------------------------------*
1161 * TD handling functions
1162 *-------------------------------------------------------------------------*/
1164 /* prepare a TD */
1166 static void td_fill (unsigned int info, void * data, int len, urb_t * urb, int index)
1168 volatile td_t * td, * td_pt;
1169 urb_priv_t * urb_priv = urb->hcpriv;
1171 if (index >= urb_priv->length) {
1172 err("internal OHCI error: TD index > length");
1173 return;
1176 td_pt = urb_priv->td [index];
1177 /* fill the old dummy TD */
1178 td = urb_priv->td [index] = (td_t *)
1179 bus_to_virt (le32_to_cpup (&urb_priv->ed->hwTailP) & 0xfffffff0);
1180 td->ed = urb_priv->ed;
1181 td->next_dl_td = NULL;
1182 td->index = index;
1183 td->urb = urb;
1184 td->hwINFO = cpu_to_le32 (info);
1185 if ((td->ed->type) == PIPE_ISOCHRONOUS) {
1186 td->hwCBP = cpu_to_le32 (((!data || !len)
1188 : virt_to_bus (data)) & 0xFFFFF000);
1189 td->ed->last_iso = info & 0xffff;
1190 } else {
1191 td->hwCBP = cpu_to_le32 (((!data || !len)
1193 : virt_to_bus (data)));
1195 td->hwBE = cpu_to_le32 ((!data || !len )
1197 : virt_to_bus (data + len - 1));
1198 td->hwNextTD = cpu_to_le32 (virt_to_bus (td_pt));
1199 td->hwPSW [0] = cpu_to_le16 ((virt_to_bus (data) & 0x0FFF) | 0xE000);
1200 td_pt->hwNextTD = 0;
1201 td->ed->hwTailP = td->hwNextTD;
1204 /*-------------------------------------------------------------------------*/
1206 /* prepare all TDs of a transfer */
1208 static void td_submit_urb (urb_t * urb)
1210 urb_priv_t * urb_priv = urb->hcpriv;
1211 ohci_t * ohci = (ohci_t *) urb->dev->bus->hcpriv;
1212 void * ctrl = urb->setup_packet;
1213 void * data = urb->transfer_buffer;
1214 int data_len = urb->transfer_buffer_length;
1215 int cnt = 0;
1216 __u32 info = 0;
1217 unsigned int toggle = 0;
1219 /* OHCI handles the DATA-toggles itself, we just use the USB-toggle bits for reseting */
1220 if(usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe))) {
1221 toggle = TD_T_TOGGLE;
1222 } else {
1223 toggle = TD_T_DATA0;
1224 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 1);
1227 urb_priv->td_cnt = 0;
1229 switch (usb_pipetype (urb->pipe)) {
1230 case PIPE_BULK:
1231 info = usb_pipeout (urb->pipe)?
1232 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
1233 while(data_len > 4096) {
1234 td_fill (info | (cnt? TD_T_TOGGLE:toggle), data, 4096, urb, cnt);
1235 data += 4096; data_len -= 4096; cnt++;
1237 info = usb_pipeout (urb->pipe)?
1238 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
1239 td_fill (info | (cnt? TD_T_TOGGLE:toggle), data, data_len, urb, cnt);
1240 cnt++;
1241 writel (OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
1242 break;
1244 case PIPE_INTERRUPT:
1245 info = usb_pipeout (urb->pipe)?
1246 TD_CC | TD_DP_OUT | toggle: TD_CC | TD_R | TD_DP_IN | toggle;
1247 td_fill (info, data, data_len, urb, cnt++);
1248 break;
1250 case PIPE_CONTROL:
1251 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
1252 td_fill (info, ctrl, 8, urb, cnt++);
1253 if (data_len > 0) {
1254 info = usb_pipeout (urb->pipe)?
1255 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 : TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
1256 td_fill (info, data, data_len, urb, cnt++);
1258 info = usb_pipeout (urb->pipe)?
1259 TD_CC | TD_DP_IN | TD_T_DATA1: TD_CC | TD_DP_OUT | TD_T_DATA1;
1260 td_fill (info, NULL, 0, urb, cnt++);
1261 writel (OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
1262 break;
1264 case PIPE_ISOCHRONOUS:
1265 for (cnt = 0; cnt < urb->number_of_packets; cnt++) {
1266 td_fill (TD_CC|TD_ISO | ((urb->start_frame + cnt) & 0xffff),
1267 (__u8 *) data + urb->iso_frame_desc[cnt].offset,
1268 urb->iso_frame_desc[cnt].length, urb, cnt);
1270 break;
1272 if (urb_priv->length != cnt)
1273 dbg("TD LENGTH %d != CNT %d", urb_priv->length, cnt);
1276 /*-------------------------------------------------------------------------*
1277 * Done List handling functions
1278 *-------------------------------------------------------------------------*/
1281 /* calculate the transfer length and update the urb */
1283 static void dl_transfer_length(td_t * td)
1285 __u32 tdINFO, tdBE, tdCBP;
1286 __u16 tdPSW;
1287 urb_t * urb = td->urb;
1288 urb_priv_t * urb_priv = urb->hcpriv;
1289 int dlen = 0;
1290 int cc = 0;
1292 tdINFO = le32_to_cpup (&td->hwINFO);
1293 tdBE = le32_to_cpup (&td->hwBE);
1294 tdCBP = le32_to_cpup (&td->hwCBP);
1297 if (tdINFO & TD_ISO) {
1298 tdPSW = le16_to_cpu (td->hwPSW[0]);
1299 cc = (tdPSW >> 12) & 0xF;
1300 if (cc < 0xE) {
1301 if (usb_pipeout(urb->pipe)) {
1302 dlen = urb->iso_frame_desc[td->index].length;
1303 } else {
1304 dlen = tdPSW & 0x3ff;
1306 urb->actual_length += dlen;
1307 urb->iso_frame_desc[td->index].actual_length = dlen;
1308 if (!(urb->transfer_flags & USB_DISABLE_SPD) && (cc == TD_DATAUNDERRUN))
1309 cc = TD_CC_NOERROR;
1311 urb->iso_frame_desc[td->index].status = cc_to_error[cc];
1313 } else { /* BULK, INT, CONTROL DATA */
1314 if (!(usb_pipetype (urb->pipe) == PIPE_CONTROL &&
1315 ((td->index == 0) || (td->index == urb_priv->length - 1)))) {
1316 if (tdBE != 0) {
1317 if (td->hwCBP == 0)
1318 urb->actual_length = bus_to_virt (tdBE) - urb->transfer_buffer + 1;
1319 else
1320 urb->actual_length = bus_to_virt (tdCBP) - urb->transfer_buffer;
1326 /* handle an urb that is being unlinked */
1328 static void dl_del_urb (urb_t * urb)
1330 wait_queue_head_t * wait_head = ((urb_priv_t *)(urb->hcpriv))->wait;
1332 urb_rm_priv_locked (urb);
1334 if (urb->transfer_flags & USB_ASYNC_UNLINK) {
1335 urb->status = -ECONNRESET;
1336 if (urb->complete)
1337 urb->complete (urb);
1338 } else {
1339 urb->status = -ENOENT;
1341 /* unblock sohci_unlink_urb */
1342 if (wait_head)
1343 wake_up (wait_head);
1347 /*-------------------------------------------------------------------------*/
1349 /* replies to the request have to be on a FIFO basis so
1350 * we reverse the reversed done-list */
1352 static td_t * dl_reverse_done_list (ohci_t * ohci)
1354 __u32 td_list_hc;
1355 td_t * td_rev = NULL;
1356 td_t * td_list = NULL;
1357 urb_priv_t * urb_priv = NULL;
1358 unsigned long flags;
1360 spin_lock_irqsave (&usb_ed_lock, flags);
1362 td_list_hc = le32_to_cpup (&ohci->hcca.done_head) & 0xfffffff0;
1363 ohci->hcca.done_head = 0;
1365 while (td_list_hc) {
1366 td_list = (td_t *) bus_to_virt (td_list_hc);
1368 if (TD_CC_GET (le32_to_cpup (&td_list->hwINFO))) {
1369 urb_priv = (urb_priv_t *) td_list->urb->hcpriv;
1370 dbg(" USB-error/status: %x : %p",
1371 TD_CC_GET (le32_to_cpup (&td_list->hwINFO)), td_list);
1372 if (td_list->ed->hwHeadP & cpu_to_le32 (0x1)) {
1373 if (urb_priv && ((td_list->index + 1) < urb_priv->length)) {
1374 td_list->ed->hwHeadP =
1375 (urb_priv->td[urb_priv->length - 1]->hwNextTD & cpu_to_le32 (0xfffffff0)) |
1376 (td_list->ed->hwHeadP & cpu_to_le32 (0x2));
1377 urb_priv->td_cnt += urb_priv->length - td_list->index - 1;
1378 } else
1379 td_list->ed->hwHeadP &= cpu_to_le32 (0xfffffff2);
1383 td_list->next_dl_td = td_rev;
1384 td_rev = td_list;
1385 td_list_hc = le32_to_cpup (&td_list->hwNextTD) & 0xfffffff0;
1387 spin_unlock_irqrestore (&usb_ed_lock, flags);
1388 return td_list;
1391 /*-------------------------------------------------------------------------*/
1393 /* there are some pending requests to remove
1394 * - some of the eds (if ed->state & ED_DEL (set by sohci_free_dev)
1395 * - some URBs/TDs if urb_priv->state == URB_DEL */
1397 static void dl_del_list (ohci_t * ohci, unsigned int frame)
1399 unsigned long flags;
1400 ed_t * ed;
1401 __u32 edINFO;
1402 __u32 tdINFO;
1403 td_t * td = NULL, * td_next = NULL, * tdHeadP = NULL, * tdTailP;
1404 __u32 * td_p;
1405 int ctrl = 0, bulk = 0;
1407 spin_lock_irqsave (&usb_ed_lock, flags);
1409 for (ed = ohci->ed_rm_list[frame]; ed != NULL; ed = ed->ed_rm_list) {
1411 tdTailP = bus_to_virt (le32_to_cpup (&ed->hwTailP) & 0xfffffff0);
1412 tdHeadP = bus_to_virt (le32_to_cpup (&ed->hwHeadP) & 0xfffffff0);
1413 edINFO = le32_to_cpup (&ed->hwINFO);
1414 td_p = &ed->hwHeadP;
1416 for (td = tdHeadP; td != tdTailP; td = td_next) {
1417 urb_t * urb = td->urb;
1418 urb_priv_t * urb_priv = td->urb->hcpriv;
1420 td_next = bus_to_virt (le32_to_cpup (&td->hwNextTD) & 0xfffffff0);
1421 if ((urb_priv->state == URB_DEL) || (ed->state & ED_DEL)) {
1422 tdINFO = le32_to_cpup (&td->hwINFO);
1423 if (TD_CC_GET (tdINFO) < 0xE)
1424 dl_transfer_length (td);
1425 *td_p = td->hwNextTD | (*td_p & cpu_to_le32 (0x3));
1427 /* URB is done; clean up */
1428 if (++(urb_priv->td_cnt) == urb_priv->length)
1429 dl_del_urb (urb);
1430 } else {
1431 td_p = &td->hwNextTD;
1435 if (ed->state & ED_DEL) { /* set by sohci_free_dev */
1436 struct ohci_device * dev = usb_to_ohci (ohci->dev[edINFO & 0x7F]);
1437 td_free (ohci, tdTailP); /* free dummy td */
1438 ed->hwINFO = cpu_to_le32 (OHCI_ED_SKIP);
1439 ed->state = ED_NEW;
1440 /* if all eds are removed wake up sohci_free_dev */
1441 if (!--dev->ed_cnt) {
1442 wait_queue_head_t *wait_head = dev->wait;
1444 dev->wait = 0;
1445 if (wait_head)
1446 wake_up (wait_head);
1448 } else {
1449 ed->state &= ~ED_URB_DEL;
1450 tdHeadP = bus_to_virt (le32_to_cpup (&ed->hwHeadP) & 0xfffffff0);
1452 if (tdHeadP == tdTailP) {
1453 if (ed->state == ED_OPER)
1454 ep_unlink(ohci, ed);
1455 td_free (ohci, tdTailP);
1456 ed->hwINFO = cpu_to_le32 (OHCI_ED_SKIP);
1457 ed->state = ED_NEW;
1458 --(usb_to_ohci (ohci->dev[edINFO & 0x7F]))->ed_cnt;
1459 } else
1460 ed->hwINFO &= ~cpu_to_le32 (OHCI_ED_SKIP);
1463 switch (ed->type) {
1464 case PIPE_CONTROL:
1465 ctrl = 1;
1466 break;
1467 case PIPE_BULK:
1468 bulk = 1;
1469 break;
1473 /* maybe reenable control and bulk lists */
1474 if (!ohci->disabled) {
1475 if (ctrl) /* reset control list */
1476 writel (0, &ohci->regs->ed_controlcurrent);
1477 if (bulk) /* reset bulk list */
1478 writel (0, &ohci->regs->ed_bulkcurrent);
1479 if (!ohci->ed_rm_list[!frame]) {
1480 if (ohci->ed_controltail)
1481 ohci->hc_control |= OHCI_CTRL_CLE;
1482 if (ohci->ed_bulktail)
1483 ohci->hc_control |= OHCI_CTRL_BLE;
1484 writel (ohci->hc_control, &ohci->regs->control);
1488 ohci->ed_rm_list[frame] = NULL;
1489 spin_unlock_irqrestore (&usb_ed_lock, flags);
1494 /*-------------------------------------------------------------------------*/
1496 /* td done list */
1498 static void dl_done_list (ohci_t * ohci, td_t * td_list)
1500 td_t * td_list_next = NULL;
1501 ed_t * ed;
1502 int cc = 0;
1503 urb_t * urb;
1504 urb_priv_t * urb_priv;
1505 __u32 tdINFO, edHeadP, edTailP;
1507 unsigned long flags;
1509 while (td_list) {
1510 td_list_next = td_list->next_dl_td;
1512 urb = td_list->urb;
1513 urb_priv = urb->hcpriv;
1514 tdINFO = le32_to_cpup (&td_list->hwINFO);
1516 ed = td_list->ed;
1518 dl_transfer_length(td_list);
1520 /* error code of transfer */
1521 cc = TD_CC_GET (tdINFO);
1522 if (cc == TD_CC_STALL)
1523 usb_endpoint_halt(urb->dev,
1524 usb_pipeendpoint(urb->pipe),
1525 usb_pipeout(urb->pipe));
1527 if (!(urb->transfer_flags & USB_DISABLE_SPD)
1528 && (cc == TD_DATAUNDERRUN))
1529 cc = TD_CC_NOERROR;
1531 if (++(urb_priv->td_cnt) == urb_priv->length) {
1532 if ((ed->state & (ED_OPER | ED_UNLINK))
1533 && (urb_priv->state != URB_DEL)) {
1534 urb->status = cc_to_error[cc];
1535 sohci_return_urb (urb);
1536 } else {
1537 spin_lock_irqsave (&usb_ed_lock, flags);
1538 dl_del_urb (urb);
1539 spin_unlock_irqrestore (&usb_ed_lock, flags);
1543 spin_lock_irqsave (&usb_ed_lock, flags);
1544 if (ed->state != ED_NEW) {
1545 edHeadP = le32_to_cpup (&ed->hwHeadP) & 0xfffffff0;
1546 edTailP = le32_to_cpup (&ed->hwTailP);
1548 /* unlink eds if they are not busy */
1549 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
1550 ep_unlink (ohci, ed);
1552 spin_unlock_irqrestore (&usb_ed_lock, flags);
1554 td_list = td_list_next;
1561 /*-------------------------------------------------------------------------*
1562 * Virtual Root Hub
1563 *-------------------------------------------------------------------------*/
1565 /* Device descriptor */
1566 static __u8 root_hub_dev_des[] =
1568 0x12, /* __u8 bLength; */
1569 0x01, /* __u8 bDescriptorType; Device */
1570 0x10, /* __u16 bcdUSB; v1.1 */
1571 0x01,
1572 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1573 0x00, /* __u8 bDeviceSubClass; */
1574 0x00, /* __u8 bDeviceProtocol; */
1575 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1576 0x00, /* __u16 idVendor; */
1577 0x00,
1578 0x00, /* __u16 idProduct; */
1579 0x00,
1580 0x00, /* __u16 bcdDevice; */
1581 0x00,
1582 0x00, /* __u8 iManufacturer; */
1583 0x02, /* __u8 iProduct; */
1584 0x01, /* __u8 iSerialNumber; */
1585 0x01 /* __u8 bNumConfigurations; */
1589 /* Configuration descriptor */
1590 static __u8 root_hub_config_des[] =
1592 0x09, /* __u8 bLength; */
1593 0x02, /* __u8 bDescriptorType; Configuration */
1594 0x19, /* __u16 wTotalLength; */
1595 0x00,
1596 0x01, /* __u8 bNumInterfaces; */
1597 0x01, /* __u8 bConfigurationValue; */
1598 0x00, /* __u8 iConfiguration; */
1599 0x40, /* __u8 bmAttributes;
1600 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
1601 0x00, /* __u8 MaxPower; */
1603 /* interface */
1604 0x09, /* __u8 if_bLength; */
1605 0x04, /* __u8 if_bDescriptorType; Interface */
1606 0x00, /* __u8 if_bInterfaceNumber; */
1607 0x00, /* __u8 if_bAlternateSetting; */
1608 0x01, /* __u8 if_bNumEndpoints; */
1609 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1610 0x00, /* __u8 if_bInterfaceSubClass; */
1611 0x00, /* __u8 if_bInterfaceProtocol; */
1612 0x00, /* __u8 if_iInterface; */
1614 /* endpoint */
1615 0x07, /* __u8 ep_bLength; */
1616 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1617 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1618 0x03, /* __u8 ep_bmAttributes; Interrupt */
1619 0x02, /* __u16 ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
1620 0x00,
1621 0xff /* __u8 ep_bInterval; 255 ms */
1624 /* Hub class-specific descriptor is constructed dynamically */
1627 /*-------------------------------------------------------------------------*/
1629 /* prepare Interrupt pipe data; HUB INTERRUPT ENDPOINT */
1631 static int rh_send_irq (ohci_t * ohci, void * rh_data, int rh_len)
1633 int num_ports;
1634 int i;
1635 int ret;
1636 int len;
1638 __u8 data[8];
1640 num_ports = readl (&ohci->regs->roothub.a) & RH_A_NDP;
1641 if (num_ports > MAX_ROOT_PORTS) {
1642 err ("bogus NDP=%d for OHCI usb-%s", num_ports,
1643 ohci->ohci_dev->slot_name);
1644 err ("rereads as NDP=%d",
1645 readl (&ohci->regs->roothub.a) & RH_A_NDP);
1646 /* retry later; "should not happen" */
1647 return 0;
1649 *(__u8 *) data = (readl (&ohci->regs->roothub.status) & (RH_HS_LPSC | RH_HS_OCIC))
1650 ? 1: 0;
1651 ret = *(__u8 *) data;
1653 for ( i = 0; i < num_ports; i++) {
1654 *(__u8 *) (data + (i + 1) / 8) |=
1655 ((readl (&ohci->regs->roothub.portstatus[i]) &
1656 (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC | RH_PS_OCIC | RH_PS_PRSC))
1657 ? 1: 0) << ((i + 1) % 8);
1658 ret += *(__u8 *) (data + (i + 1) / 8);
1660 len = i/8 + 1;
1662 if (ret > 0) {
1663 memcpy (rh_data, data, min (len, min (rh_len, sizeof(data))));
1664 return len;
1666 return 0;
1669 /*-------------------------------------------------------------------------*/
1671 /* Virtual Root Hub INTs are polled by this timer every "interval" ms */
1673 static void rh_int_timer_do (unsigned long ptr)
1675 int len;
1677 urb_t * urb = (urb_t *) ptr;
1678 ohci_t * ohci = urb->dev->bus->hcpriv;
1680 if (ohci->disabled)
1681 return;
1683 /* ignore timers firing during PM suspend, etc */
1684 if ((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER)
1685 goto out;
1687 if(ohci->rh.send) {
1688 len = rh_send_irq (ohci, urb->transfer_buffer, urb->transfer_buffer_length);
1689 if (len > 0) {
1690 urb->actual_length = len;
1691 #ifdef DEBUG
1692 urb_print (urb, "RET-t(rh)", usb_pipeout (urb->pipe));
1693 #endif
1694 if (urb->complete)
1695 urb->complete (urb);
1698 out:
1699 rh_init_int_timer (urb);
1702 /*-------------------------------------------------------------------------*/
1704 /* Root Hub INTs are polled by this timer */
1706 static int rh_init_int_timer (urb_t * urb)
1708 ohci_t * ohci = urb->dev->bus->hcpriv;
1710 ohci->rh.interval = urb->interval;
1711 init_timer (&ohci->rh.rh_int_timer);
1712 ohci->rh.rh_int_timer.function = rh_int_timer_do;
1713 ohci->rh.rh_int_timer.data = (unsigned long) urb;
1714 ohci->rh.rh_int_timer.expires =
1715 jiffies + (HZ * (urb->interval < 30? 30: urb->interval)) / 1000;
1716 add_timer (&ohci->rh.rh_int_timer);
1718 return 0;
1721 /*-------------------------------------------------------------------------*/
1723 #define OK(x) len = (x); break
1724 #define WR_RH_STAT(x) writel((x), &ohci->regs->roothub.status)
1725 #define WR_RH_PORTSTAT(x) writel((x), &ohci->regs->roothub.portstatus[wIndex-1])
1726 #define RD_RH_STAT readl(&ohci->regs->roothub.status)
1727 #define RD_RH_PORTSTAT readl(&ohci->regs->roothub.portstatus[wIndex-1])
1729 /* request to virtual root hub */
1731 static int rh_submit_urb (urb_t * urb)
1733 struct usb_device * usb_dev = urb->dev;
1734 ohci_t * ohci = usb_dev->bus->hcpriv;
1735 unsigned int pipe = urb->pipe;
1736 devrequest * cmd = (devrequest *) urb->setup_packet;
1737 void * data = urb->transfer_buffer;
1738 int leni = urb->transfer_buffer_length;
1739 int len = 0;
1740 int status = TD_CC_NOERROR;
1742 __u32 datab[4];
1743 __u8 * data_buf = (__u8 *) datab;
1745 __u16 bmRType_bReq;
1746 __u16 wValue;
1747 __u16 wIndex;
1748 __u16 wLength;
1750 if (usb_pipeint(pipe)) {
1751 ohci->rh.urb = urb;
1752 ohci->rh.send = 1;
1753 ohci->rh.interval = urb->interval;
1754 rh_init_int_timer(urb);
1755 urb->status = cc_to_error [TD_CC_NOERROR];
1757 return 0;
1760 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
1761 wValue = le16_to_cpu (cmd->value);
1762 wIndex = le16_to_cpu (cmd->index);
1763 wLength = le16_to_cpu (cmd->length);
1765 dbg ("rh_submit_urb, req = %d(%x) len=%d", bmRType_bReq,
1766 bmRType_bReq, wLength);
1768 switch (bmRType_bReq) {
1769 /* Request Destination:
1770 without flags: Device,
1771 RH_INTERFACE: interface,
1772 RH_ENDPOINT: endpoint,
1773 RH_CLASS means HUB here,
1774 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1777 case RH_GET_STATUS:
1778 *(__u16 *) data_buf = cpu_to_le16 (1); OK (2);
1779 case RH_GET_STATUS | RH_INTERFACE:
1780 *(__u16 *) data_buf = cpu_to_le16 (0); OK (2);
1781 case RH_GET_STATUS | RH_ENDPOINT:
1782 *(__u16 *) data_buf = cpu_to_le16 (0); OK (2);
1783 case RH_GET_STATUS | RH_CLASS:
1784 *(__u32 *) data_buf = cpu_to_le32 (
1785 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
1786 OK (4);
1787 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1788 *(__u32 *) data_buf = cpu_to_le32 (RD_RH_PORTSTAT); OK (4);
1790 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1791 switch (wValue) {
1792 case (RH_ENDPOINT_STALL): OK (0);
1794 break;
1796 case RH_CLEAR_FEATURE | RH_CLASS:
1797 switch (wValue) {
1798 case RH_C_HUB_LOCAL_POWER:
1799 OK(0);
1800 case (RH_C_HUB_OVER_CURRENT):
1801 WR_RH_STAT(RH_HS_OCIC); OK (0);
1803 break;
1805 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1806 switch (wValue) {
1807 case (RH_PORT_ENABLE):
1808 WR_RH_PORTSTAT (RH_PS_CCS ); OK (0);
1809 case (RH_PORT_SUSPEND):
1810 WR_RH_PORTSTAT (RH_PS_POCI); OK (0);
1811 case (RH_PORT_POWER):
1812 WR_RH_PORTSTAT (RH_PS_LSDA); OK (0);
1813 case (RH_C_PORT_CONNECTION):
1814 WR_RH_PORTSTAT (RH_PS_CSC ); OK (0);
1815 case (RH_C_PORT_ENABLE):
1816 WR_RH_PORTSTAT (RH_PS_PESC); OK (0);
1817 case (RH_C_PORT_SUSPEND):
1818 WR_RH_PORTSTAT (RH_PS_PSSC); OK (0);
1819 case (RH_C_PORT_OVER_CURRENT):
1820 WR_RH_PORTSTAT (RH_PS_OCIC); OK (0);
1821 case (RH_C_PORT_RESET):
1822 WR_RH_PORTSTAT (RH_PS_PRSC); OK (0);
1824 break;
1826 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1827 switch (wValue) {
1828 case (RH_PORT_SUSPEND):
1829 WR_RH_PORTSTAT (RH_PS_PSS ); OK (0);
1830 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
1831 if (RD_RH_PORTSTAT & RH_PS_CCS)
1832 WR_RH_PORTSTAT (RH_PS_PRS);
1833 OK (0);
1834 case (RH_PORT_POWER):
1835 WR_RH_PORTSTAT (RH_PS_PPS ); OK (0);
1836 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
1837 if (RD_RH_PORTSTAT & RH_PS_CCS)
1838 WR_RH_PORTSTAT (RH_PS_PES );
1839 OK (0);
1841 break;
1843 case RH_SET_ADDRESS: ohci->rh.devnum = wValue; OK(0);
1845 case RH_GET_DESCRIPTOR:
1846 switch ((wValue & 0xff00) >> 8) {
1847 case (0x01): /* device descriptor */
1848 len = min (leni, min (sizeof (root_hub_dev_des), wLength));
1849 data_buf = root_hub_dev_des; OK(len);
1850 case (0x02): /* configuration descriptor */
1851 len = min (leni, min (sizeof (root_hub_config_des), wLength));
1852 data_buf = root_hub_config_des; OK(len);
1853 case (0x03): /* string descriptors */
1854 len = usb_root_hub_string (wValue & 0xff,
1855 (int)(long) ohci->regs, "OHCI",
1856 data, wLength);
1857 if (len > 0) {
1858 data_buf = data;
1859 OK (min (leni, len));
1861 // else fallthrough
1862 default:
1863 status = TD_CC_STALL;
1865 break;
1867 case RH_GET_DESCRIPTOR | RH_CLASS:
1869 __u32 temp = readl (&ohci->regs->roothub.a);
1871 data_buf [0] = 9; // min length;
1872 data_buf [1] = 0x29;
1873 data_buf [2] = temp & RH_A_NDP;
1874 data_buf [3] = 0;
1875 if (temp & RH_A_PSM) /* per-port power switching? */
1876 data_buf [3] |= 0x1;
1877 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
1878 data_buf [3] |= 0x10;
1879 else if (temp & RH_A_OCPM) /* per-port overcurrent reporting? */
1880 data_buf [3] |= 0x8;
1882 datab [1] = 0;
1883 data_buf [5] = (temp & RH_A_POTPGT) >> 24;
1884 temp = readl (&ohci->regs->roothub.b);
1885 data_buf [7] = temp & RH_B_DR;
1886 if (data_buf [2] < 7) {
1887 data_buf [8] = 0xff;
1888 } else {
1889 data_buf [0] += 2;
1890 data_buf [8] = (temp & RH_B_DR) >> 8;
1891 data_buf [10] = data_buf [9] = 0xff;
1894 len = min (leni, min (data_buf [0], wLength));
1895 OK (len);
1898 case RH_GET_CONFIGURATION: *(__u8 *) data_buf = 0x01; OK (1);
1900 case RH_SET_CONFIGURATION: WR_RH_STAT (0x10000); OK (0);
1902 default:
1903 dbg ("unsupported root hub command");
1904 status = TD_CC_STALL;
1907 #ifdef DEBUG
1908 // ohci_dump_roothub (ohci, 0);
1909 #endif
1911 len = min(len, leni);
1912 if (data != data_buf)
1913 memcpy (data, data_buf, len);
1914 urb->actual_length = len;
1915 urb->status = cc_to_error [status];
1917 #ifdef DEBUG
1918 urb_print (urb, "RET(rh)", usb_pipeout (urb->pipe));
1919 #endif
1921 urb->hcpriv = NULL;
1922 usb_dec_dev_use (usb_dev);
1923 urb->dev = NULL;
1924 if (urb->complete)
1925 urb->complete (urb);
1926 return 0;
1929 /*-------------------------------------------------------------------------*/
1931 static int rh_unlink_urb (urb_t * urb)
1933 ohci_t * ohci = urb->dev->bus->hcpriv;
1935 if (ohci->rh.urb == urb) {
1936 ohci->rh.send = 0;
1937 del_timer (&ohci->rh.rh_int_timer);
1938 ohci->rh.urb = NULL;
1940 urb->hcpriv = NULL;
1941 usb_dec_dev_use(urb->dev);
1942 urb->dev = NULL;
1943 if (urb->transfer_flags & USB_ASYNC_UNLINK) {
1944 urb->status = -ECONNRESET;
1945 if (urb->complete)
1946 urb->complete (urb);
1947 } else
1948 urb->status = -ENOENT;
1950 return 0;
1953 /*-------------------------------------------------------------------------*
1954 * HC functions
1955 *-------------------------------------------------------------------------*/
1957 /* reset the HC and BUS */
1959 static int hc_reset (ohci_t * ohci)
1961 int timeout = 30;
1962 int smm_timeout = 50; /* 0,5 sec */
1964 if (readl (&ohci->regs->control) & OHCI_CTRL_IR) { /* SMM owns the HC */
1965 writel (OHCI_OCR, &ohci->regs->cmdstatus); /* request ownership */
1966 dbg("USB HC TakeOver from SMM");
1967 while (readl (&ohci->regs->control) & OHCI_CTRL_IR) {
1968 wait_ms (10);
1969 if (--smm_timeout == 0) {
1970 err("USB HC TakeOver failed!");
1971 return -1;
1976 /* Disable HC interrupts */
1977 writel (OHCI_INTR_MIE, &ohci->regs->intrdisable);
1979 dbg("USB HC reset_hc usb-%s: ctrl = 0x%x ;",
1980 ohci->ohci_dev->slot_name,
1981 readl (&ohci->regs->control));
1983 /* Reset USB (needed by some controllers) */
1984 writel (0, &ohci->regs->control);
1986 /* HC Reset requires max 10 ms delay */
1987 writel (OHCI_HCR, &ohci->regs->cmdstatus);
1988 while ((readl (&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
1989 if (--timeout == 0) {
1990 err("USB HC reset timed out!");
1991 return -1;
1993 udelay (1);
1995 return 0;
1998 /*-------------------------------------------------------------------------*/
2000 /* Start an OHCI controller, set the BUS operational
2001 * enable interrupts
2002 * connect the virtual root hub */
2004 static int hc_start (ohci_t * ohci)
2006 __u32 mask;
2007 unsigned int fminterval;
2008 struct usb_device * usb_dev;
2009 struct ohci_device * dev;
2011 ohci->disabled = 1;
2013 /* Tell the controller where the control and bulk lists are
2014 * The lists are empty now. */
2016 writel (0, &ohci->regs->ed_controlhead);
2017 writel (0, &ohci->regs->ed_bulkhead);
2019 writel (virt_to_bus (&ohci->hcca), &ohci->regs->hcca); /* a reset clears this */
2021 fminterval = 0x2edf;
2022 writel ((fminterval * 9) / 10, &ohci->regs->periodicstart);
2023 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
2024 writel (fminterval, &ohci->regs->fminterval);
2025 writel (0x628, &ohci->regs->lsthresh);
2027 /* start controller operations */
2028 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
2029 ohci->disabled = 0;
2030 writel (ohci->hc_control, &ohci->regs->control);
2032 /* Choose the interrupts we care about now, others later on demand */
2033 mask = OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
2034 writel (mask, &ohci->regs->intrenable);
2035 writel (mask, &ohci->regs->intrstatus);
2037 #ifdef OHCI_USE_NPS
2038 writel ((readl(&ohci->regs->roothub.a) | RH_A_NPS) & ~RH_A_PSM,
2039 &ohci->regs->roothub.a);
2040 writel (RH_HS_LPSC, &ohci->regs->roothub.status);
2041 #endif /* OHCI_USE_NPS */
2043 // POTPGT delay is bits 24-31, in 2 ms units.
2044 mdelay ((readl(&ohci->regs->roothub.a) >> 23) & 0x1fe);
2046 /* connect the virtual root hub */
2047 ohci->rh.devnum = 0;
2048 usb_dev = usb_alloc_dev (NULL, ohci->bus);
2049 if (!usb_dev) {
2050 ohci->disabled = 1;
2051 return -ENOMEM;
2054 dev = usb_to_ohci (usb_dev);
2055 ohci->bus->root_hub = usb_dev;
2056 usb_connect (usb_dev);
2057 if (usb_new_device (usb_dev) != 0) {
2058 usb_free_dev (usb_dev);
2059 ohci->disabled = 1;
2060 return -ENODEV;
2063 return 0;
2066 /*-------------------------------------------------------------------------*/
2068 /* an interrupt happens */
2070 static void hc_interrupt (int irq, void * __ohci, struct pt_regs * r)
2072 ohci_t * ohci = __ohci;
2073 struct ohci_regs * regs = ohci->regs;
2074 int ints;
2076 if ((ohci->hcca.done_head != 0) && !(le32_to_cpup (&ohci->hcca.done_head) & 0x01)) {
2077 ints = OHCI_INTR_WDH;
2078 } else if ((ints = (readl (&regs->intrstatus) & readl (&regs->intrenable))) == 0) {
2079 return;
2082 // dbg("Interrupt: %x frame: %x", ints, le16_to_cpu (ohci->hcca.frame_no));
2084 if (ints & OHCI_INTR_UE) {
2085 ohci->disabled++;
2086 err ("OHCI Unrecoverable Error, controller usb-%s disabled",
2087 ohci->ohci_dev->slot_name);
2088 // e.g. due to PCI Master/Target Abort
2090 #ifdef DEBUG
2091 ohci_dump (ohci, 1);
2092 #else
2093 // FIXME: be optimistic, hope that bug won't repeat often.
2094 // Make some non-interrupt context restart the controller.
2095 // Count and limit the retries though; either hardware or
2096 // software errors can go forever...
2097 #endif
2100 if (ints & OHCI_INTR_WDH) {
2101 writel (OHCI_INTR_WDH, &regs->intrdisable);
2102 dl_done_list (ohci, dl_reverse_done_list (ohci));
2103 writel (OHCI_INTR_WDH, &regs->intrenable);
2106 if (ints & OHCI_INTR_SO) {
2107 dbg("USB Schedule overrun");
2108 writel (OHCI_INTR_SO, &regs->intrenable);
2111 if (ints & OHCI_INTR_SF) {
2112 unsigned int frame = le16_to_cpu (ohci->hcca.frame_no) & 1;
2113 writel (OHCI_INTR_SF, &regs->intrdisable);
2114 if (ohci->ed_rm_list[!frame] != NULL) {
2115 dl_del_list (ohci, !frame);
2117 if (ohci->ed_rm_list[frame] != NULL)
2118 writel (OHCI_INTR_SF, &regs->intrenable);
2120 writel (ints, &regs->intrstatus);
2121 writel (OHCI_INTR_MIE, &regs->intrenable);
2123 /* FIXME: check URB timeouts */
2126 /*-------------------------------------------------------------------------*/
2128 /* allocate OHCI */
2130 static ohci_t * __devinit hc_alloc_ohci (struct pci_dev *dev, void * mem_base)
2132 ohci_t * ohci;
2133 struct usb_bus * bus;
2135 ohci = (ohci_t *) kmalloc (sizeof *ohci, GFP_KERNEL);
2136 if (!ohci)
2137 return NULL;
2139 memset (ohci, 0, sizeof (ohci_t));
2141 ohci->disabled = 1;
2142 ohci->irq = -1;
2143 ohci->regs = mem_base;
2145 ohci->ohci_dev = dev;
2146 dev->driver_data = ohci;
2148 INIT_LIST_HEAD (&ohci->ohci_hcd_list);
2149 list_add (&ohci->ohci_hcd_list, &ohci_hcd_list);
2151 bus = usb_alloc_bus (&sohci_device_operations);
2152 if (!bus) {
2153 kfree (ohci);
2154 return NULL;
2157 ohci->bus = bus;
2158 bus->hcpriv = (void *) ohci;
2160 return ohci;
2164 /*-------------------------------------------------------------------------*/
2166 /* De-allocate all resources.. */
2168 static void hc_release_ohci (ohci_t * ohci)
2170 dbg ("USB HC release ohci usb-%s", ohci->ohci_dev->slot_name);
2172 /* disconnect all devices */
2173 if (ohci->bus->root_hub)
2174 usb_disconnect (&ohci->bus->root_hub);
2176 if (!ohci->disabled)
2177 hc_reset (ohci);
2179 if (ohci->irq >= 0) {
2180 free_irq (ohci->irq, ohci);
2181 ohci->irq = -1;
2183 ohci->ohci_dev->driver_data = 0;
2185 usb_deregister_bus (ohci->bus);
2186 usb_free_bus (ohci->bus);
2188 list_del (&ohci->ohci_hcd_list);
2189 INIT_LIST_HEAD (&ohci->ohci_hcd_list);
2191 /* unmap the IO address space */
2192 iounmap (ohci->regs);
2194 kfree (ohci);
2197 /*-------------------------------------------------------------------------*/
2199 /* Increment the module usage count, start the control thread and
2200 * return success. */
2202 static struct pci_driver ohci_pci_driver;
2204 static int __devinit
2205 hc_found_ohci (struct pci_dev *dev, int irq, void * mem_base)
2207 ohci_t * ohci;
2208 u8 latency, limit;
2209 char buf[8], *bufp = buf;
2211 #ifndef __sparc__
2212 sprintf(buf, "%d", irq);
2213 #else
2214 bufp = __irq_itoa(irq);
2215 #endif
2216 printk(KERN_INFO __FILE__ ": USB OHCI at membase 0x%lx, IRQ %s\n",
2217 (unsigned long) mem_base, bufp);
2218 printk(KERN_INFO __FILE__ ": usb-%s, %s\n", dev->slot_name, dev->name);
2220 ohci = hc_alloc_ohci (dev, mem_base);
2221 if (!ohci) {
2222 return -ENOMEM;
2225 /* bad pci latencies can contribute to overruns */
2226 pci_read_config_byte (dev, PCI_LATENCY_TIMER, &latency);
2227 if (latency) {
2228 pci_read_config_byte (dev, PCI_MAX_LAT, &limit);
2229 if (limit && limit < latency) {
2230 dbg ("PCI latency reduced to max %d", limit);
2231 pci_write_config_byte (dev, PCI_LATENCY_TIMER, limit);
2232 ohci->pci_latency = limit;
2233 } else {
2234 /* it might already have been reduced */
2235 ohci->pci_latency = latency;
2239 if (hc_reset (ohci) < 0) {
2240 hc_release_ohci (ohci);
2241 return -ENODEV;
2244 /* FIXME this is a second HC reset; why?? */
2245 writel (ohci->hc_control = OHCI_USB_RESET, &ohci->regs->control);
2246 wait_ms (10);
2248 usb_register_bus (ohci->bus);
2250 if (request_irq (irq, hc_interrupt, SA_SHIRQ,
2251 ohci_pci_driver.name, ohci) != 0) {
2252 err ("request interrupt %s failed", bufp);
2253 hc_release_ohci (ohci);
2254 return -EBUSY;
2256 ohci->irq = irq;
2258 if (hc_start (ohci) < 0) {
2259 err ("can't start usb-%s", dev->slot_name);
2260 hc_release_ohci (ohci);
2261 return -EBUSY;
2264 #ifdef DEBUG
2265 ohci_dump (ohci, 1);
2266 #endif
2267 return 0;
2270 /*-------------------------------------------------------------------------*/
2272 #ifdef CONFIG_PM
2274 /* controller died; cleanup debris, then restart */
2275 /* must not be called from interrupt context */
2277 static void hc_restart (ohci_t *ohci)
2279 int temp;
2280 int i;
2282 if (ohci->pci_latency)
2283 pci_write_config_byte (ohci->ohci_dev, PCI_LATENCY_TIMER, ohci->pci_latency);
2285 ohci->disabled = 1;
2286 if (ohci->bus->root_hub)
2287 usb_disconnect (&ohci->bus->root_hub);
2289 /* empty the interrupt branches */
2290 for (i = 0; i < NUM_INTS; i++) ohci->ohci_int_load[i] = 0;
2291 for (i = 0; i < NUM_INTS; i++) ohci->hcca.int_table[i] = 0;
2293 /* no EDs to remove */
2294 ohci->ed_rm_list [0] = NULL;
2295 ohci->ed_rm_list [1] = NULL;
2297 /* empty control and bulk lists */
2298 ohci->ed_isotail = NULL;
2299 ohci->ed_controltail = NULL;
2300 ohci->ed_bulktail = NULL;
2302 if ((temp = hc_reset (ohci)) < 0 || (temp = hc_start (ohci)) < 0) {
2303 err ("can't restart usb-%s, %d", ohci->ohci_dev->slot_name, temp);
2304 } else
2305 dbg ("restart usb-%s completed", ohci->ohci_dev->slot_name);
2308 #endif /* CONFIG_PM */
2310 /*-------------------------------------------------------------------------*/
2312 /* configured so that an OHCI device is always provided */
2313 /* always called with process context; sleeping is OK */
2315 static int __devinit
2316 ohci_pci_probe (struct pci_dev *dev, const struct pci_device_id *id)
2318 unsigned long mem_resource, mem_len;
2319 void *mem_base;
2321 if (pci_enable_device(dev) < 0)
2322 return -ENODEV;
2324 /* we read its hardware registers as memory */
2325 mem_resource = pci_resource_start(dev, 0);
2326 mem_len = pci_resource_len(dev, 0);
2327 if (!request_mem_region (mem_resource, mem_len, ohci_pci_driver.name)) {
2328 dbg ("controller already in use");
2329 return -EBUSY;
2332 mem_base = ioremap_nocache (mem_resource, mem_len);
2333 if (!mem_base) {
2334 err("Error mapping OHCI memory");
2335 return -EFAULT;
2338 /* controller writes into our memory */
2339 pci_set_master (dev);
2341 return hc_found_ohci (dev, dev->irq, mem_base);
2344 /*-------------------------------------------------------------------------*/
2346 /* may be called from interrupt context [interface spec] */
2347 /* may be called without controller present */
2348 /* may be called with controller, bus, and devices active */
2350 static void __devexit
2351 ohci_pci_remove (struct pci_dev *dev)
2353 ohci_t *ohci = (ohci_t *) dev->driver_data;
2355 dbg ("remove %s controller usb-%s%s%s",
2356 hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS),
2357 dev->slot_name,
2358 ohci->disabled ? " (disabled)" : "",
2359 in_interrupt () ? " in interrupt" : ""
2361 #ifdef DEBUG
2362 ohci_dump (ohci, 1);
2363 #endif
2365 /* don't wake up sleeping controllers, or block in interrupt context */
2366 if ((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER || in_interrupt ()) {
2367 dbg ("controller being disabled");
2368 ohci->disabled = 1;
2371 /* on return, USB will always be reset (if present) */
2372 if (ohci->disabled)
2373 writel (ohci->hc_control = OHCI_USB_RESET,
2374 &ohci->regs->control);
2376 hc_release_ohci (ohci);
2378 release_mem_region (pci_resource_start (dev, 0), pci_resource_len (dev, 0));
2382 #ifdef CONFIG_PM
2384 /*-------------------------------------------------------------------------*/
2386 static void
2387 ohci_pci_suspend (struct pci_dev *dev)
2389 ohci_t *ohci = (ohci_t *) dev->driver_data;
2391 if ((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER) {
2392 dbg ("can't suspend usb-%s (state is %s)", dev->slot_name,
2393 hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS));
2394 return;
2397 /* act as if usb suspend can always be used */
2398 info ("USB suspend: usb-%s", dev->slot_name);
2399 #ifdef CONFIG_PMAC_PBOOK
2400 disable_irq (ohci->irq);
2401 /* else, 2.4 assumes shared irqs -- don't disable */
2402 #endif
2403 ohci->hc_control = OHCI_USB_SUSPEND;
2404 writel (ohci->hc_control, &ohci->regs->control);
2405 wait_ms (10);
2408 /*-------------------------------------------------------------------------*/
2410 static void
2411 ohci_pci_resume (struct pci_dev *dev)
2413 ohci_t *ohci = (ohci_t *) dev->driver_data;
2414 int temp;
2416 /* guard against multiple resumes */
2417 atomic_inc (&ohci->resume_count);
2418 if (atomic_read (&ohci->resume_count) != 1) {
2419 err ("concurrent PCI resumes for usb-%s", dev->slot_name);
2420 atomic_dec (&ohci->resume_count);
2421 return;
2424 /* did we suspend, or were we powered off? */
2425 ohci->hc_control = readl (&ohci->regs->control);
2426 temp = ohci->hc_control & OHCI_CTRL_HCFS;
2428 #ifdef DEBUG
2429 /* the registers may look crazy here */
2430 ohci_dump_status (ohci);
2431 #endif
2433 switch (temp) {
2435 case OHCI_USB_RESET: // lost power
2436 info ("USB restart: usb-%s", dev->slot_name);
2437 hc_restart (ohci);
2438 break;
2440 case OHCI_USB_SUSPEND: // host wakeup
2441 case OHCI_USB_RESUME: // remote wakeup
2442 info ("USB continue: usb-%s from %s wakeup", dev->slot_name,
2443 (temp == OHCI_USB_SUSPEND)
2444 ? "host" : "remote");
2445 ohci->hc_control = OHCI_USB_RESUME;
2446 writel (ohci->hc_control, &ohci->regs->control);
2447 wait_ms (20);
2449 temp = readl (&ohci->regs->control);
2450 temp = ohci->hc_control & OHCI_CTRL_HCFS;
2451 if (temp != OHCI_USB_RESUME) {
2452 err ("controller usb-%s won't resume", dev->slot_name);
2453 ohci->disabled = 1;
2454 return;
2457 ohci->disabled = 0;
2458 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
2459 if (!ohci->ed_rm_list[0] & !ohci->ed_rm_list[1]) {
2460 if (ohci->ed_controltail)
2461 ohci->hc_control |= OHCI_CTRL_CLE;
2462 if (ohci->ed_bulktail)
2463 ohci->hc_control |= OHCI_CTRL_BLE;
2465 writel (ohci->hc_control, &ohci->regs->control);
2466 #ifdef CONFIG_PMAC_PBOOK
2467 enable_irq (ohci->irq);
2468 #endif
2469 break;
2471 default:
2472 warn ("odd PCI resume for usb-%s", dev->slot_name);
2475 /* controller is operational, extra resumes are harmless */
2476 atomic_dec (&ohci->resume_count);
2479 #endif /* CONFIG_PM */
2482 /*-------------------------------------------------------------------------*/
2484 static const struct pci_device_id __devinitdata ohci_pci_ids [] = { {
2486 /* handle any USB OHCI controller */
2487 class: ((PCI_CLASS_SERIAL_USB << 8) | 0x10),
2488 class_mask: ~0,
2490 /* no matter who makes it */
2491 vendor: PCI_ANY_ID,
2492 device: PCI_ANY_ID,
2493 subvendor: PCI_ANY_ID,
2494 subdevice: PCI_ANY_ID,
2496 }, { /* end: all zeroes */ }
2499 MODULE_DEVICE_TABLE (pci, ohci_pci_ids);
2501 static struct pci_driver ohci_pci_driver = {
2502 name: "usb-ohci",
2503 id_table: &ohci_pci_ids [0],
2505 probe: ohci_pci_probe,
2506 remove: ohci_pci_remove,
2508 #ifdef CONFIG_PMAC_PBOOK
2509 /* pbook PCI thinks different ... for now :-) */
2510 #else
2511 #ifdef CONFIG_PM
2512 suspend: ohci_pci_suspend,
2513 resume: ohci_pci_resume,
2514 #endif /* PM */
2515 #endif /* PBOOK */
2519 #ifdef CONFIG_PMAC_PBOOK
2521 /*-------------------------------------------------------------------------*/
2523 static int ohci_sleep_notify (struct pmu_sleep_notifier * self, int when)
2525 struct list_head * ohci_l;
2526 ohci_t * ohci;
2528 for (ohci_l = ohci_hcd_list.next; ohci_l != &ohci_hcd_list; ohci_l = ohci_l->next) {
2529 ohci = list_entry (ohci_l, ohci_t, ohci_hcd_list);
2531 switch (when) {
2532 case PBOOK_SLEEP_NOW:
2533 ohci_pci_suspend (ohci->ohci_dev);
2534 break;
2535 case PBOOK_WAKE:
2536 ohci_pci_resume (ohci->ohci_dev);
2537 break;
2540 return PBOOK_SLEEP_OK;
2543 static struct pmu_sleep_notifier ohci_sleep_notifier = {
2544 ohci_sleep_notify, SLEEP_LEVEL_MISC,
2546 #endif /* CONFIG_PMAC_PBOOK */
2549 /*-------------------------------------------------------------------------*/
2551 static int __init ohci_hcd_init (void)
2553 int ret;
2555 if ((ret = ohci_mem_init ()) < 0)
2556 return ret;
2558 if ((ret = pci_module_init (&ohci_pci_driver)) < 0) {
2559 ohci_mem_cleanup ();
2560 return ret;
2563 #ifdef CONFIG_PMAC_PBOOK
2564 pmu_register_sleep_notifier (&ohci_sleep_notifier);
2565 #endif
2566 return ret;
2569 /*-------------------------------------------------------------------------*/
2571 static void __exit ohci_hcd_cleanup (void)
2573 #ifdef CONFIG_PMAC_PBOOK
2574 pmu_unregister_sleep_notifier (&ohci_sleep_notifier);
2575 #endif
2576 pci_unregister_driver (&ohci_pci_driver);
2577 ohci_mem_cleanup ();
2580 module_init (ohci_hcd_init);
2581 module_exit (ohci_hcd_cleanup);
2584 MODULE_AUTHOR ("Roman Weissgaerber <weissg@vienna.at>, David Brownell");
2585 MODULE_DESCRIPTION ("USB OHCI Host Controller Driver");