Import 2.3.7pre8
[davej-history.git] / drivers / usb / ohci.c
blob22b46a3969cc1eb6e38dd644211337fba52a8a1d
1 /*
2 * Open Host Controller Interface driver for USB.
4 * (C) Copyright 1999 Gregory P. Smith <greg@electricrain.com>
6 * This is the "other" host controller interface for USB. You will
7 * find this on many non-Intel based motherboards, and of course the
8 * Mac. As Linus hacked his UHCI driver together first, I modeled
9 * this after his.. (it should be obvious)
11 * From the programming standpoint the OHCI interface seems a little
12 * prettier and potentially less CPU intensive. This remains to be
13 * proven. In reality, I don't believe it'll make one darn bit of
14 * difference. USB v1.1 is a slow bus by today's standards.
16 * OHCI hardware takes care of most of the scheduling of different
17 * transfer types with the correct prioritization for us.
19 * To get started in USB, I used the "Universal Serial Bus System
20 * Architecture" book by Mindshare, Inc. It was a reasonable introduction
21 * and overview of USB and the two dominant host controller interfaces
22 * however you're better off just reading the real specs available
23 * from www.usb.org as you'll need them to get enough detailt to
24 * actually implement a HCD. The book has many typos and omissions
25 * Beware, the specs are the victim of a committee.
27 * This code was written with Guinness on the brain, xsnow on the desktop
28 * and Orbital, Orb, Enya & Massive Attack on the CD player. What a life! ;)
30 * No filesystems were harmed in the development of this code.
32 * $Id: ohci.c,v 1.43 1999/05/16 22:35:24 greg Exp $
35 #include <linux/config.h>
36 #include <linux/module.h>
37 #include <linux/pci.h>
38 #include <linux/kernel.h>
39 #include <linux/delay.h>
40 #include <linux/ioport.h>
41 #include <linux/sched.h>
42 #include <linux/malloc.h>
43 #include <linux/smp_lock.h>
44 #include <linux/errno.h>
46 #include <asm/spinlock.h>
47 #include <asm/io.h>
48 #include <asm/irq.h>
49 #include <asm/system.h>
51 #include "ohci.h"
53 #ifdef CONFIG_APM
54 #include <linux/apm_bios.h>
55 static int handle_apm_event(apm_event_t event);
56 static int apm_resume = 0;
57 #endif
59 static DECLARE_WAIT_QUEUE_HEAD(ohci_configure);
61 #ifdef CONFIG_USB_OHCI_DEBUG
62 #define OHCI_DEBUG /* to make typing it easier.. */
63 #endif
65 int MegaDebug = 0; /* SIGUSR2 to the control thread toggles this */
68 #ifdef OHCI_TIMER
69 static struct timer_list ohci_timer; /* timer for root hub polling */
70 #endif
72 static spinlock_t ohci_edtd_lock = SPIN_LOCK_UNLOCKED;
74 #define FIELDS_OF_ED(e) le32_to_cpup(&e->status), le32_to_cpup(&e->tail_td), \
75 le32_to_cpup(&e->_head_td), le32_to_cpup(&e->next_ed)
76 #define FIELDS_OF_TD(t) le32_to_cpup(&t->info), le32_to_cpup(&t->cur_buf), \
77 le32_to_cpup(&t->next_td), le32_to_cpup(&t->buf_end)
79 static const char *cc_names[16] = {
80 "no error",
81 "CRC error",
82 "bit stuff error",
83 "data toggle mismatch",
84 "stall",
85 "device not responding",
86 "PID check failed",
87 "unexpected PID",
88 "data overrun",
89 "data underrun",
90 "reserved (10)",
91 "reserved (11)",
92 "buffer overrun",
93 "buffer underrun",
94 "not accessed (14)",
95 "not accessed"
99 * Add a chain of TDs to the end of the TD list on a given ED.
101 * This function uses the first TD of the chain as the new dummy TD
102 * for the ED, and uses the old dummy TD instead of the first TD
103 * of the chain. The reason for this is that this makes it possible
104 * to update the TD chain without needing any locking between the
105 * CPU and the OHCI controller.
107 * The return value is the pointer to the new first TD (the old
108 * dummy TD).
110 * Important! This function is not re-entrant w.r.t. each ED.
111 * Locking ohci_edtd_lock while using the function is a must
112 * if there is any possibility of another CPU or an interrupt routine
113 * calling this function with the same ED.
115 * This function can be called by the interrupt handler.
117 static struct ohci_td *ohci_add_td_to_ed(struct ohci_td *td,
118 struct ohci_td *last_td, struct ohci_ed *ed)
120 struct ohci_td *t, *dummy_td;
121 u32 new_dummy;
123 if (ed->tail_td == 0) {
124 printk(KERN_ERR "eek! an ED without a dummy_td\n");
125 return td;
128 /* Get a pointer to the current dummy TD. */
129 dummy_td = bus_to_virt(ed_tail_td(ed));
131 for (t = td; ; t = bus_to_virt(le32_to_cpup(&t->next_td))) {
132 t->ed = ed;
133 if (t == last_td)
134 break;
137 /* Make the last TD point back to the first, since it
138 * will become the new dummy TD. */
139 new_dummy = cpu_to_le32(virt_to_bus(td));
140 last_td->next_td = new_dummy;
142 /* Copy the contents of the first TD into the dummy */
143 *dummy_td = *td;
145 /* Turn the first TD into a dummy */
146 make_dumb_td(td);
148 /* Set the HC's tail pointer to the new dummy */
149 ed->tail_td = new_dummy;
151 return dummy_td; /* replacement head of chain */
152 } /* ohci_add_td_to_ed() */
155 inline void ohci_start_control(struct ohci *ohci)
157 /* tell the HC to start processing the control list */
158 writel_set(OHCI_USB_CLE, &ohci->regs->control);
159 writel_set(OHCI_CMDSTAT_CLF, &ohci->regs->cmdstatus);
162 inline void ohci_start_bulk(struct ohci *ohci)
164 /* tell the HC to start processing the bulk list */
165 writel_set(OHCI_USB_BLE, &ohci->regs->control);
166 writel_set(OHCI_CMDSTAT_BLF, &ohci->regs->cmdstatus);
169 inline void ohci_start_periodic(struct ohci *ohci)
171 /* enable processing periodic (intr) transfers starting next frame */
172 writel_set(OHCI_USB_PLE, &ohci->regs->control);
175 inline void ohci_start_isoc(struct ohci *ohci)
177 /* enable processing isoc. transfers starting next frame */
178 writel_set(OHCI_USB_IE, &ohci->regs->control);
182 * Add an ED to the hardware register ED list pointed to by hw_listhead_p
183 * This function only makes sense for Control and Bulk EDs.
185 static void ohci_add_ed_to_hw(struct ohci_ed *ed, void* hw_listhead_p)
187 __u32 listhead;
188 unsigned long flags;
190 spin_lock_irqsave(&ohci_edtd_lock, flags);
192 listhead = readl(hw_listhead_p);
194 /* if the list is not empty, insert this ED at the front */
195 /* XXX should they go on the end? */
196 ed->next_ed = cpu_to_le32(listhead);
198 /* update the hardware listhead pointer */
199 writel(virt_to_bus(ed), hw_listhead_p);
201 spin_unlock_irqrestore(&ohci_edtd_lock, flags);
202 } /* ohci_add_ed_to_hw() */
206 * Put a control ED on the controller's list
208 void ohci_add_control_ed(struct ohci *ohci, struct ohci_ed *ed)
210 ohci_add_ed_to_hw(ed, &ohci->regs->ed_controlhead);
211 ohci_start_control(ohci);
212 } /* ohci_add_control_ed() */
215 * Put a bulk ED on the controller's list
217 void ohci_add_bulk_ed(struct ohci *ohci, struct ohci_ed *ed)
219 ohci_add_ed_to_hw(ed, &ohci->regs->ed_bulkhead);
220 ohci_start_bulk(ohci);
221 } /* ohci_add_bulk_ed() */
224 * Put a periodic ED on the appropriate list given the period.
226 void ohci_add_periodic_ed(struct ohci *ohci, struct ohci_ed *ed, int period)
228 struct ohci_ed *int_ed;
229 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
230 unsigned long flags;
233 * Pick a good frequency endpoint based on the requested period
235 int_ed = &root_hub->ed[ms_to_ed_int(period)];
236 #ifdef OHCI_DEBUG
237 printk(KERN_DEBUG "usb-ohci: Using INT ED queue %d for %dms period\n",
238 ms_to_ed_int(period), period);
239 #endif
241 spin_lock_irqsave(&ohci_edtd_lock, flags);
243 * Insert this ED at the front of the list.
245 ed->next_ed = int_ed->next_ed;
246 int_ed->next_ed = cpu_to_le32(virt_to_bus(ed));
248 spin_unlock_irqrestore(&ohci_edtd_lock, flags);
250 ohci_start_periodic(ohci);
251 } /* ohci_add_periodic_ed() */
254 * Put an isochronous ED on the controller's list
256 inline void ohci_add_isoc_ed(struct ohci *ohci, struct ohci_ed *ed)
258 ohci_add_periodic_ed(ohci, ed, 1);
263 * This will be used for the interrupt to wake us up on the next SOF
265 DECLARE_WAIT_QUEUE_HEAD(start_of_frame_wakeup);
267 static void ohci_wait_sof(struct ohci_regs *regs)
269 DECLARE_WAITQUEUE(wait, current);
271 add_wait_queue(&start_of_frame_wakeup, &wait);
273 /* clear the SOF interrupt status and enable it */
274 writel(OHCI_INTR_SF, &regs->intrstatus);
275 writel(OHCI_INTR_SF, &regs->intrenable);
277 schedule_timeout(HZ/10);
279 remove_wait_queue(&start_of_frame_wakeup, &wait);
283 * Guarantee that an ED is safe to be modified by the HCD (us).
285 * This function can NOT be called from an interrupt.
287 void ohci_wait_for_ed_safe(struct ohci_regs *regs, struct ohci_ed *ed, int ed_type)
289 __u32 *hw_listcurrent;
291 /* tell the controller to skip this ED */
292 ed->status |= cpu_to_le32(OHCI_ED_SKIP);
294 switch (ed_type) {
295 case HCD_ED_CONTROL:
296 hw_listcurrent = &regs->ed_controlcurrent;
297 break;
298 case HCD_ED_BULK:
299 hw_listcurrent = &regs->ed_bulkcurrent;
300 break;
301 case HCD_ED_ISOC:
302 case HCD_ED_INT:
303 hw_listcurrent = &regs->ed_periodcurrent;
304 break;
305 default:
306 return;
310 * If the HC is processing this ED we need to wait until the
311 * at least the next frame.
313 if (virt_to_bus(ed) == readl(hw_listcurrent)) {
314 #ifdef OHCI_DEBUG
315 printk(KERN_INFO "Waiting a frame for OHC to finish with ED %p [%x %x %x %x]\n", ed, FIELDS_OF_ED(ed));
316 #endif
317 ohci_wait_sof(regs);
321 return; /* The ED is now safe */
322 } /* ohci_wait_for_ed_safe() */
326 * Remove an ED from the HC's list.
327 * This function can ONLY be used for Control or Bulk EDs.
329 * Note that the SKIP bit is left on in the removed ED.
331 void ohci_remove_norm_ed_from_hw(struct ohci *ohci, struct ohci_ed *ed, int ed_type)
333 unsigned long flags;
334 struct ohci_regs *regs = ohci->regs;
335 struct ohci_ed *cur;
336 __u32 bus_ed = virt_to_bus(ed);
337 __u32 bus_cur;
338 __u32 *hw_listhead_p;
340 if (ed == NULL || !bus_ed)
341 return;
342 ed->status |= cpu_to_le32(OHCI_ED_SKIP);
344 switch (ed_type) {
345 case HCD_ED_CONTROL:
346 hw_listhead_p = &regs->ed_controlhead;
347 break;
348 case HCD_ED_BULK:
349 hw_listhead_p = &regs->ed_bulkhead;
350 break;
351 default:
352 printk(KERN_ERR "Unknown HCD ED type %d.\n", ed_type);
353 return;
356 bus_cur = readl(hw_listhead_p);
358 if (bus_cur == 0)
359 return; /* the list is already empty */
361 cur = bus_to_virt(bus_cur);
363 spin_lock_irqsave(&ohci_edtd_lock, flags);
365 /* if its the head ED, move the head */
366 if (bus_cur == bus_ed) {
367 writel(le32_to_cpup(&cur->next_ed), hw_listhead_p);
368 } else if (cur->next_ed != 0) {
369 struct ohci_ed *prev;
371 /* walk the list and unlink the ED if found */
372 do {
373 prev = cur;
374 cur = bus_to_virt(le32_to_cpup(&cur->next_ed));
376 if (cur == ed) {
377 /* unlink from the list */
378 prev->next_ed = cur->next_ed;
379 break;
381 } while (cur->next_ed != 0);
385 * Make sure this ED is not being accessed by the HC as we speak.
387 ohci_wait_for_ed_safe(regs, ed, ed_type);
389 /* clear any links from the ED for safety */
390 ed->next_ed = 0;
392 spin_unlock_irqrestore(&ohci_edtd_lock, flags);
393 } /* ohci_remove_norm_ed_from_hw() */
396 * Remove an ED from the controller's control list. Note that the SKIP bit
397 * is left on in the removed ED.
399 inline void ohci_remove_control_ed(struct ohci *ohci, struct ohci_ed *ed)
401 ohci_remove_norm_ed_from_hw(ohci, ed, HCD_ED_CONTROL);
405 * Remove an ED from the controller's bulk list. Note that the SKIP bit
406 * is left on in the removed ED.
408 inline void ohci_remove_bulk_ed(struct ohci *ohci, struct ohci_ed *ed)
410 ohci_remove_norm_ed_from_hw(ohci, ed, HCD_ED_BULK);
414 * Remove all the EDs which have a given device address from a list.
415 * Used when the device is unplugged.
416 * Returns 1 if anything was changed.
418 static int ohci_remove_device_list(__u32 *headp, int devnum)
420 struct ohci_ed *ed;
421 __u32 *prevp = headp;
422 int removed = 0;
424 while (*prevp != 0) {
425 ed = bus_to_virt(le32_to_cpup(prevp));
426 if ((le32_to_cpup(&ed->status) & OHCI_ED_FA) == devnum) {
427 /* set the controller to skip this one
428 and remove it from the list */
429 ed->status |= cpu_to_le32(OHCI_ED_SKIP);
430 *prevp = ed->next_ed;
431 removed = 1;
432 } else {
433 prevp = &ed->next_ed;
436 wmb();
438 return removed;
442 * Remove all the EDs for a given device from all lists.
444 void ohci_remove_device(struct ohci *ohci, int devnum)
446 unsigned long flags;
447 __u32 head;
448 struct ohci_regs *regs = ohci->regs;
449 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
451 spin_lock_irqsave(&ohci_edtd_lock, flags);
453 /* Control list */
454 head = cpu_to_le32(readl(&regs->ed_controlhead));
455 if (ohci_remove_device_list(&head, devnum))
456 writel(le32_to_cpup(&head), &regs->ed_controlhead);
458 /* Bulk list */
459 head = cpu_to_le32(readl(&regs->ed_bulkhead));
460 if (ohci_remove_device_list(&head, devnum))
461 writel(le32_to_cpup(&head), &regs->ed_bulkhead);
463 /* Interrupt/iso list */
464 head = cpu_to_le32(virt_to_bus(&root_hub->ed[ED_INT_32]));
465 ohci_remove_device_list(&head, devnum);
468 * Wait until the start of the next frame to ensure
469 * that the HC has seen any changes.
471 ohci_wait_sof(ohci->regs);
473 spin_unlock_irqrestore(&ohci_edtd_lock, flags);
477 * Remove a TD from the given EDs TD list.
479 static void ohci_remove_td_from_ed(struct ohci_td *td, struct ohci_ed *ed)
481 unsigned long flags;
482 struct ohci_td *head_td;
484 if ((td == NULL) || (ed == NULL))
485 return;
487 spin_lock_irqsave(&ohci_edtd_lock, flags);
489 if (ed_head_td(ed) == 0)
490 return;
492 /* set the "skip me bit" in this ED */
493 ed->status |= cpu_to_le32(OHCI_ED_SKIP);
495 /* XXX Assuming this list will never be circular */
497 head_td = bus_to_virt(ed_head_td(ed));
498 if (virt_to_bus(td) == ed_head_td(ed)) {
499 /* It's the first TD, remove it. */
500 set_ed_head_td(ed, head_td->next_td);
501 } else {
502 struct ohci_td *prev_td, *cur_td;
504 /* FIXME: collapse this into a nice simple loop :) */
505 if (head_td->next_td != 0) {
506 prev_td = head_td;
507 cur_td = bus_to_virt(le32_to_cpup(&head_td->next_td));
508 for (;;) {
509 if (td == cur_td) {
510 /* remove it */
511 prev_td->next_td = cur_td->next_td;
512 break;
514 if (cur_td->next_td == 0)
515 break;
516 prev_td = cur_td;
517 cur_td = bus_to_virt(le32_to_cpup(&cur_td->next_td));
522 td->next_td = 0; /* remove the TDs links */
523 td->ed = NULL;
525 /* return this TD to the pool of free TDs */
526 ohci_free_td(td);
528 /* unset the "skip me bit" in this ED */
529 ed->status &= cpu_to_le32(~OHCI_ED_SKIP);
531 spin_unlock_irqrestore(&ohci_edtd_lock, flags);
532 } /* ohci_remove_td_from_ed() */
536 * Get a pointer (virtual) to an available TD from the given device's
537 * pool. Return NULL if none are left.
539 static struct ohci_td *ohci_get_free_td(struct ohci_device *dev)
541 int idx;
543 #if 0
544 printk(KERN_DEBUG "in ohci_get_free_td()\n");
545 #endif
547 /* FIXME: this is horribly inefficient */
548 for (idx=0; idx < NUM_TDS; idx++) {
549 #if 0
550 show_ohci_td(&dev->td[idx]);
551 #endif
552 if (!td_allocated(dev->td[idx])) {
553 struct ohci_td *new_td = &dev->td[idx];
554 /* zero out the TD */
555 memset(new_td, 0, sizeof(*new_td));
556 /* mark the new TDs as unaccessed */
557 new_td->info = cpu_to_le32(OHCI_TD_CC_NEW);
558 /* mark it as allocated */
559 allocate_td(new_td);
560 return new_td;
564 printk(KERN_ERR "usb-ohci: unable to allocate a TD\n");
565 return NULL;
566 } /* ohci_get_free_td() */
570 * Get a pointer (virtual) to an available TD from the given device's
571 * pool. Return NULL if none are left.
573 static struct ohci_ed *ohci_get_free_ed(struct ohci_device *dev)
575 int idx;
577 /* FIXME: this is horribly inefficient */
578 for (idx=0; idx < NUM_EDS; idx++) {
579 if (!ed_allocated(dev->ed[idx])) {
580 struct ohci_ed *new_ed = &dev->ed[idx];
581 /* zero out the ED */
582 memset(new_ed, 0, sizeof(*new_ed));
583 /* all new EDs start with the SKIP bit set */
584 new_ed->status |= cpu_to_le32(OHCI_ED_SKIP);
585 /* mark it as allocated */
586 allocate_ed(new_ed);
587 return new_ed;
591 printk(KERN_ERR "usb-ohci: unable to allocate an ED\n");
592 return NULL;
593 } /* ohci_get_free_ed() */
596 void ohci_free_ed(struct ohci_ed *ed)
598 if (!ed)
599 return;
601 if (ed_head_td(ed) != 0) {
602 struct ohci_td *td, *tail_td, *next_td;
604 td = bus_to_virt(ed_head_td(ed));
605 tail_td = bus_to_virt(ed_tail_td(ed));
606 for (;;) {
607 next_td = bus_to_virt(le32_to_cpup(&td->next_td));
608 ohci_free_td(td);
609 if (td == tail_td)
610 break;
611 td = next_td;
615 ed->status &= cpu_to_le32(~(__u32)ED_ALLOCATED);
616 } /* ohci_free_ed() */
620 * Initialize a TD
622 * dir = OHCI_TD_D_IN, OHCI_TD_D_OUT, or OHCI_TD_D_SETUP
623 * toggle = TOGGLE_AUTO, TOGGLE_DATA0, TOGGLE_DATA1
625 inline struct ohci_td *ohci_fill_new_td(struct ohci_td *td, int dir, int toggle, __u32 flags, void *data, __u32 len, void *dev_id, usb_device_irq completed)
627 /* hardware fields */
628 td->info = cpu_to_le32(OHCI_TD_CC_NEW |
629 (dir & OHCI_TD_D) |
630 (toggle & OHCI_TD_DT) |
631 flags);
632 td->cur_buf = (data == NULL) ? 0 : cpu_to_le32(virt_to_bus(data));
633 td->buf_end = (len == 0) ? 0 :
634 cpu_to_le32(le32_to_cpup(&td->cur_buf) + len - 1);
636 /* driver fields */
637 td->data = data;
638 td->dev_id = dev_id;
639 td->completed = completed;
641 #if 0
642 printk(KERN_DEBUG "ohci_fill_new_td created:\n");
643 show_ohci_td(td);
644 #endif
646 return td;
647 } /* ohci_fill_new_td() */
651 * Initialize a new ED on device dev, including allocating and putting the
652 * dummy tail_td on its queue if it doesn't already have one. Any
653 * TDs on this ED other than the dummy will be lost (so there better
654 * not be any!). This assumes that the ED is Allocated and will
655 * force the Allocated bit on.
657 struct ohci_ed *ohci_fill_ed(struct ohci_device *dev, struct ohci_ed *ed,
658 int maxpacketsize, int lowspeed, int endp_id,
659 int isoc_tds)
661 struct ohci_td *dummy_td;
663 if (ed_head_td(ed) != ed_tail_td(ed))
664 printk(KERN_ERR "Reusing a non-empty ED %p!\n", ed);
666 if (!ed->tail_td) {
667 dummy_td = ohci_get_free_td(dev);
668 if (dummy_td == NULL) {
669 printk(KERN_ERR "Error allocating dummy TD for ED %p\n", ed);
670 return NULL; /* no dummy available! */
672 make_dumb_td(dummy_td); /* flag it as a dummy */
673 ed->tail_td = cpu_to_le32(virt_to_bus(dummy_td));
674 } else {
675 dummy_td = bus_to_virt(ed_tail_td(ed));
676 if (!td_dummy(*dummy_td))
677 printk(KERN_ERR "ED %p's dummy %p is screwy\n", ed, dummy_td);
680 /* set the head TD to the dummy and clear the Carry & Halted bits */
681 ed->_head_td = ed->tail_td;
683 ed->status = cpu_to_le32(
684 ed_set_maxpacket(maxpacketsize) |
685 ed_set_speed(lowspeed) |
686 (endp_id & 0x7ff) |
687 ((isoc_tds == 0) ? OHCI_ED_F_NORM : OHCI_ED_F_ISOC));
688 allocate_ed(ed);
689 ed->next_ed = 0;
691 return ed;
692 } /* ohci_fill_ed() */
695 /**********************************
696 * OHCI interrupt list operations *
697 **********************************/
700 * Request an interrupt handler for one "pipe" of a USB device.
701 * (this function is pretty minimal right now)
703 * At the moment this is only good for input interrupts. (ie: for a
704 * mouse or keyboard)
706 * Period is desired polling interval in ms. The closest, shorter
707 * match will be used. Powers of two from 1-32 are supported by OHCI.
709 static int ohci_request_irq(struct usb_device *usb, unsigned int pipe,
710 usb_device_irq handler, int period, void *dev_id)
712 struct ohci_device *dev = usb_to_ohci(usb);
713 struct ohci_td *td;
714 struct ohci_ed *interrupt_ed; /* endpoint descriptor for this irq */
715 int maxps = usb_maxpacket(usb, pipe);
717 /* Get an ED and TD */
718 interrupt_ed = ohci_get_free_ed(dev);
719 if (!interrupt_ed) {
720 printk(KERN_ERR "Out of EDs on device %p in ohci_request_irq\n", dev);
721 return -1;
724 td = ohci_get_free_td(dev);
725 if (!td) {
726 printk(KERN_ERR "Out of TDs in ohci_request_irq\n");
727 ohci_free_ed(interrupt_ed);
728 return -1;
732 * Set the max packet size, device speed, endpoint number, usb
733 * device number (function address), and type of TD.
735 ohci_fill_ed(dev, interrupt_ed, maxps, usb_pipeslow(pipe),
736 usb_pipe_endpdev(pipe), 0 /* normal TDs */);
738 /* Fill in the TD */
739 if (maxps > sizeof(dev->data))
740 maxps = sizeof(dev->data);
741 ohci_fill_new_td(td, td_set_dir_out(usb_pipeout(pipe)),
742 TOGGLE_AUTO,
743 OHCI_TD_ROUND,
744 dev->data, maxps,
745 dev_id, handler);
747 * TODO: be aware of how the OHCI controller deals with DMA
748 * spanning more than one page.
752 * Put the TD onto our ED and make sure its ready to run
754 td = ohci_add_td_to_ed(td, td, interrupt_ed);
755 interrupt_ed->status &= cpu_to_le32(~OHCI_ED_SKIP);
756 ohci_unhalt_ed(interrupt_ed);
758 /* Make sure all the stores above get done before
759 * the store which tells the OHCI about the new ed. */
760 wmb();
762 /* Assimilate the new ED into the collective */
763 ohci_add_periodic_ed(dev->ohci, interrupt_ed, period);
765 return 0;
766 } /* ohci_request_irq() */
770 * Control thread operations:
772 static DECLARE_WAIT_QUEUE_HEAD(control_wakeup);
775 * This is the handler that gets called when a control transaction
776 * completes.
778 * This function is called from the interrupt handler.
780 static int ohci_control_completed(int stats, void *buffer, void *dev_id)
782 /* pass the TDs completion status back to control_msg */
783 if (dev_id) {
784 int *completion_status = (int *)dev_id;
785 *completion_status = stats;
788 wake_up(&control_wakeup);
789 return 0;
790 } /* ohci_control_completed() */
794 * Send or receive a control message on a "pipe"
796 * The cmd parameter is a pointer to the 8 byte setup command to be
797 * sent. FIXME: This is a devrequest in usb.h. The function
798 * should be updated to accept a devrequest* instead of void*..
800 * A control message contains:
801 * - The command itself
802 * - An optional data phase (if len > 0)
803 * - Status complete phase
805 * This function can NOT be called from an interrupt.
807 static int ohci_control_msg(struct usb_device *usb, unsigned int pipe,
808 devrequest *cmd, void *data, int len)
810 struct ohci_device *dev = usb_to_ohci(usb);
811 struct ohci_ed *control_ed = ohci_get_free_ed(dev);
812 struct ohci_td *setup_td, *data_td, *status_td;
813 DECLARE_WAITQUEUE(wait, current);
814 unsigned long flags;
815 int completion_status = -1;
816 devrequest our_cmd;
818 /* byte-swap fields of cmd if necessary */
819 our_cmd = *cmd;
820 cpu_to_le16s(&our_cmd.value);
821 cpu_to_le16s(&our_cmd.index);
822 cpu_to_le16s(&our_cmd.length);
824 #ifdef OHCI_DEBUG
825 if (MegaDebug)
826 printk(KERN_DEBUG "ohci_control_msg %p (ohci_dev: %p) pipe %x, cmd %p, data %p, len %d\n", usb, dev, pipe, cmd, data, len);
827 #endif
828 if (!control_ed) {
829 printk(KERN_ERR "usb-ohci: couldn't get ED for dev %p\n", dev);
830 return -1;
833 /* get a TD to send this control message with */
834 setup_td = ohci_get_free_td(dev);
835 if (!setup_td) {
836 printk(KERN_ERR "usb-ohci: couldn't get TD for dev %p [cntl setup]\n", dev);
837 ohci_free_ed(control_ed);
838 return -1;
842 * Set the max packet size, device speed, endpoint number, usb
843 * device number (function address), and type of TD.
846 ohci_fill_ed(dev, control_ed, usb_maxpacket(usb,pipe), usb_pipeslow(pipe),
847 usb_pipe_endpdev(pipe), 0 /* normal TDs */);
850 * Build the control TD
854 * Set the not accessed condition code, allow odd sized data,
855 * and set the data transfer type to SETUP. Setup DATA always
856 * uses a DATA0 packet.
858 * The setup packet contains a devrequest (usb.h) which
859 * will always be 8 bytes long.
861 ohci_fill_new_td(setup_td, OHCI_TD_D_SETUP, TOGGLE_DATA0,
862 OHCI_TD_IOC_OFF,
863 &our_cmd, 8, /* cmd is always 8 bytes long */
864 NULL, NULL);
866 /* allocate the next TD */
867 data_td = ohci_get_free_td(dev);
868 if (!data_td) {
869 printk(KERN_ERR "usb-ohci: couldn't get TD for dev %p [cntl data]\n", dev);
870 ohci_free_td(setup_td);
871 ohci_free_ed(control_ed);
872 return -1;
875 /* link to the next TD */
876 setup_td->next_td = cpu_to_le32(virt_to_bus(data_td));
878 if (len > 0) {
880 /* build the Control DATA TD, it starts with a DATA1. */
881 ohci_fill_new_td(data_td, td_set_dir_out(usb_pipeout(pipe)),
882 TOGGLE_DATA1,
883 OHCI_TD_ROUND | OHCI_TD_IOC_OFF,
884 data, len,
885 NULL, NULL);
888 * TODO: Normal TDs can transfer up to 8192 bytes on OHCI.
889 * However, for that to happen, the data must -start-
890 * on a nice 4kb page. We need to check for data
891 * sizes > 4096 and, if they cross more than two 4096
892 * byte pages of memory one or more additional TDs
893 * will need to be created. (repeat doing this in a
894 * loop until all of the DATA is on a TD)
896 * Control transfers are -highly unlikely- to need to
897 * transfer this much data.. but who knows.. sadistic
898 * hardware is sure to exist.
901 status_td = ohci_get_free_td(dev); /* TODO check for NULL */
902 if (!status_td) {
903 printk(KERN_ERR "usb-ohci: couldn't get TD for dev %p [cntl status]\n", dev);
904 ohci_free_td(setup_td);
905 ohci_free_td(data_td);
906 ohci_free_ed(control_ed);
907 return -1;
910 data_td->next_td = cpu_to_le32(virt_to_bus(status_td));
911 } else {
912 status_td = data_td; /* no data_td, use it for status */
915 /* The control status packet always uses a DATA1
916 * Give "dev_id" the address of completion_status so that the
917 * TDs status can be passed back to us from the IRQ. */
918 ohci_fill_new_td(status_td,
919 td_set_dir_in(usb_pipeout(pipe) | (len == 0)),
920 TOGGLE_DATA1,
921 0 /* flags */,
922 NULL /* data */, 0 /* data len */,
923 &completion_status, ohci_control_completed);
924 status_td->next_td = 0; /* end of TDs */
927 * Add the chain of 2-3 control TDs to the control ED's TD list
929 spin_lock_irqsave(&ohci_edtd_lock, flags);
930 setup_td = ohci_add_td_to_ed(setup_td, status_td, control_ed);
931 spin_unlock_irqrestore(&ohci_edtd_lock, flags);
933 #ifdef OHCI_DEBUG
934 if (MegaDebug) {
935 /* complete transaction debugging output (before) */
936 printk(KERN_DEBUG " Control ED %lx:\n", virt_to_bus(control_ed));
937 show_ohci_ed(control_ed);
938 printk(KERN_DEBUG " Setup TD %lx:\n", virt_to_bus(setup_td));
939 show_ohci_td(setup_td);
940 if (data_td != status_td) {
941 printk(KERN_DEBUG " Data TD %lx:\n", virt_to_bus(data_td));
942 show_ohci_td(data_td);
944 printk(KERN_DEBUG " Status TD %lx:\n", virt_to_bus(status_td));
945 show_ohci_td(status_td);
946 printk(KERN_DEBUG " Controller Status:\n");
947 show_ohci_status(dev->ohci);
949 #endif
952 * Start the control transaction..
954 current->state = TASK_UNINTERRUPTIBLE;
955 add_wait_queue(&control_wakeup, &wait);
957 /* Give the ED to the HC */
958 ohci_add_control_ed(dev->ohci, control_ed);
960 schedule_timeout(HZ/10);
962 remove_wait_queue(&control_wakeup, &wait);
964 #ifdef OHCI_DEBUG
965 if (MegaDebug) {
966 /* complete transaction debugging output (after) */
967 printk(KERN_DEBUG " *after* Control ED %lx:\n", virt_to_bus(control_ed));
968 show_ohci_ed(control_ed);
969 printk(KERN_DEBUG " *after* Setup TD %lx:\n", virt_to_bus(setup_td));
970 show_ohci_td(setup_td);
971 if (data_td != status_td) {
972 printk(KERN_DEBUG " *after* Data TD %lx:\n", virt_to_bus(data_td));
973 show_ohci_td(data_td);
975 printk(KERN_DEBUG " *after* Status TD %lx:\n", virt_to_bus(status_td));
976 show_ohci_td(status_td);
977 printk(KERN_DEBUG " *after* Controller Status:\n");
978 show_ohci_status(dev->ohci);
980 #endif
982 /* remove the control ED from the HC */
983 ohci_remove_control_ed(dev->ohci, control_ed);
984 ohci_free_ed(control_ed); /* return it to the pool */
986 #ifdef OHCI_DEBUG
987 if (completion_status != 0) {
988 char *what = (completion_status < 0)? "timed out":
989 cc_names[completion_status & 0xf];
990 printk(KERN_ERR "ohci_control_msg: %s on pipe %x cmd %x %x %x %x %x\n",
991 what, pipe, cmd->requesttype, cmd->request,
992 cmd->value, cmd->index, cmd->length);
993 } else if (!usb_pipeout(pipe)) {
994 unsigned char *q = data;
995 int i;
996 printk(KERN_DEBUG "ctrl msg %x %x %x %x %x on pipe %x returned:",
997 cmd->requesttype, cmd->request, cmd->value, cmd->index,
998 cmd->length, pipe);
999 for (i = 0; i < len; ++i) {
1000 if (i % 16 == 0)
1001 printk("\n" KERN_DEBUG);
1002 printk(" %x", q[i]);
1004 printk("\n");
1006 #endif
1007 return completion_status;
1008 } /* ohci_control_msg() */
1012 * Allocate a new USB device to be attached to an OHCI controller
1014 static struct usb_device *ohci_usb_allocate(struct usb_device *parent)
1016 struct usb_device *usb_dev;
1017 struct ohci_device *dev;
1018 int idx;
1021 * Allocate the generic USB device
1023 usb_dev = kmalloc(sizeof(*usb_dev), GFP_KERNEL);
1024 if (!usb_dev)
1025 return NULL;
1027 memset(usb_dev, 0, sizeof(*usb_dev));
1030 * Allocate an OHCI device (EDs and TDs for this device)
1032 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1033 if (!dev) {
1034 kfree(usb_dev);
1035 return NULL;
1038 memset(dev, 0, sizeof(*dev));
1040 /* Initialize all EDs in a new device with the skip flag so that
1041 * they are ignored by the controller until set otherwise. */
1042 for (idx = 0; idx < NUM_EDS; ++idx) {
1043 dev->ed[idx].status = cpu_to_le32(OHCI_ED_SKIP);
1047 * Link them together
1049 usb_dev->hcpriv = dev;
1050 dev->usb = usb_dev;
1053 * Link the device to its parent (hub, etc..) if any.
1055 usb_dev->parent = parent;
1057 if (parent) {
1058 usb_dev->bus = parent->bus;
1059 dev->ohci = usb_to_ohci(parent)->ohci;
1062 return usb_dev;
1063 } /* ohci_usb_allocate() */
1067 * Free a usb device.
1069 * TODO This function needs to take better care of the EDs and TDs, etc.
1071 static int ohci_usb_deallocate(struct usb_device *usb_dev)
1073 struct ohci_device *dev = usb_to_ohci(usb_dev);
1075 ohci_remove_device(dev->ohci, usb_dev->devnum);
1077 /* kfree(usb_to_ohci(usb_dev)); */
1078 /* kfree(usb_dev); */
1079 return 0;
1082 /* FIXME! */
1083 #define ohci_bulk_msg NULL
1086 * functions for the generic USB driver
1088 struct usb_operations ohci_device_operations = {
1089 ohci_usb_allocate,
1090 ohci_usb_deallocate,
1091 ohci_control_msg,
1092 ohci_bulk_msg,
1093 ohci_request_irq,
1098 * Reset an OHCI controller. Returns >= 0 on success.
1100 * Afterwards the HC will be in the "suspend" state which prevents you
1101 * from writing to some registers. Bring it to the operational state
1102 * ASAP.
1104 static int reset_hc(struct ohci *ohci)
1106 int timeout = 10000; /* prevent an infinite loop */
1108 #if 0
1109 printk(KERN_INFO "usb-ohci: resetting HC %p\n", ohci);
1110 #endif
1112 writel(~0x0, &ohci->regs->intrdisable); /* Disable HC interrupts */
1113 writel(1, &ohci->regs->cmdstatus); /* HC Reset */
1114 writel_mask(0x3f, &ohci->regs->control); /* move to UsbReset state */
1116 while ((readl(&ohci->regs->cmdstatus) & OHCI_CMDSTAT_HCR) != 0) {
1117 if (!--timeout) {
1118 printk(KERN_ERR "usb-ohci: USB HC reset timed out!\n");
1119 return -1;
1121 udelay(1);
1124 printk(KERN_DEBUG "usb-ohci: HC %p reset.\n", ohci);
1126 return 0;
1127 } /* reset_hc() */
1131 * Reset and start an OHCI controller. Returns >= 0 on success.
1133 static int start_hc(struct ohci *ohci)
1135 int ret = 0;
1136 int fminterval;
1137 __u32 what_to_enable;
1139 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
1141 fminterval = readl(&ohci->regs->fminterval) & 0x3fff;
1142 #if 0
1143 printk(KERN_DEBUG "entering start_hc %p\n", ohci);
1144 #endif
1146 if (reset_hc(ohci) < 0)
1147 return -1;
1149 /* restore registers cleared by the reset */
1150 writel(virt_to_bus(root_hub->hcca), &ohci->regs->hcca);
1153 * XXX Should fminterval also be set here?
1154 * The spec suggests 0x2edf [11,999]. (FIXME: make this a constant)
1156 /* fminterval |= (0x2edf << 16); */
1157 fminterval = (10240 << 16) | 11999;
1158 writel(fminterval, &ohci->regs->fminterval);
1159 /* Start periodic transfers at 90% of fminterval (fmremaining
1160 * counts down; this will put them in the first 10% of the
1161 * frame). */
1162 writel((0x2edf*9)/10, &ohci->regs->periodicstart);
1165 * FNO (frame number overflow) could be enabled... they
1166 * occur every 32768 frames (every 32-33 seconds). This is
1167 * useful for debugging and as a bus heartbeat. -greg
1169 /* Choose the interrupts we care about */
1170 what_to_enable = OHCI_INTR_MIE |
1171 #ifdef OHCI_RHSC_INT
1172 OHCI_INTR_RHSC |
1173 #endif
1174 /* | OHCI_INTR_FNO */
1175 OHCI_INTR_WDH;
1176 writel( what_to_enable, &ohci->regs->intrenable);
1178 /* Enter the USB Operational state & start the frames a flowing.. */
1179 writel_set(OHCI_USB_OPER, &ohci->regs->control);
1181 /* Enable control lists */
1182 writel_set(OHCI_USB_IE | OHCI_USB_CLE | OHCI_USB_BLE, &ohci->regs->control);
1184 /* Force global power enable -gal@cs.uni-magdeburg.de */
1186 * This turns on global power switching for all the ports
1187 * and tells the HC that all of the ports should be powered on
1188 * all of the time.
1190 * TODO: This could be battery draining for laptops.. We
1191 * should implement power switching.
1193 writel_set( OHCI_ROOT_A_NPS, &ohci->regs->roothub.a );
1194 writel_mask( ~((__u32)OHCI_ROOT_A_PSM), &ohci->regs->roothub.a );
1196 /* Turn on power to the root hub ports (thanks Roman!) */
1197 writel( OHCI_ROOT_LPSC, &ohci->regs->roothub.status );
1199 printk(KERN_INFO "usb-ohci: host controller operational\n");
1201 return ret;
1202 } /* start_hc() */
1206 * Reset a root hub port
1208 static void ohci_reset_port(struct ohci *ohci, unsigned int port)
1210 int status;
1212 /* Don't allow overflows. */
1213 if (port >= MAX_ROOT_PORTS) {
1214 printk(KERN_ERR "usb-ohci: bad port #%d in ohci_reset_port\n", port);
1215 port = MAX_ROOT_PORTS-1;
1218 writel(PORT_PRS, &ohci->regs->roothub.portstatus[port]); /* Reset */
1221 * Wait for the reset to complete.
1223 wait_ms(20);
1225 /* check port status to see that the reset completed */
1226 status = readl(&ohci->regs->roothub.portstatus[port]);
1227 if (status & PORT_PRS) {
1228 /* reset failed, try harder? */
1229 printk(KERN_ERR "usb-ohci: port %d reset failed, retrying\n", port);
1230 writel(PORT_PRS, &ohci->regs->roothub.portstatus[port]);
1231 wait_ms(50);
1234 /* TODO we might need to re-enable the port here or is that
1235 * done elsewhere? */
1237 } /* ohci_reset_port */
1241 * This gets called if the connect status on the root hub changes.
1243 static void ohci_connect_change(struct ohci * ohci, int port)
1245 struct usb_device *usb_dev;
1246 struct ohci_device *dev;
1247 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
1248 /* memory I/O address of the port status register */
1249 __u32 *portaddr = &ohci->regs->roothub.portstatus[port];
1250 int portstatus;
1252 #ifdef OHCI_DEBUG
1253 printk(KERN_DEBUG "ohci_connect_change on port %d\n", port);
1254 #endif
1257 * Because of the status change we have to forget
1258 * everything we think we know about the device
1259 * on this root hub port. It may have changed.
1261 usb_disconnect(root_hub->usb->children + port);
1263 portstatus = readl(portaddr);
1265 /* disable the port if nothing is connected */
1266 if (!(portstatus & PORT_CCS)) {
1267 writel(PORT_CCS, portaddr);
1268 /* We need to reset the CSC bit -after- disabling the
1269 * port because it causes the CSC bit to come on
1270 * again... */
1271 wait_ms(20);
1272 writel(PORT_CSC, portaddr);
1273 #ifdef OHCI_DEBUG
1274 printk(KERN_DEBUG "ohci port %d disabled, nothing connected.\n", port);
1275 #endif
1276 return;
1280 * Allocate a device for the new thingy that's been attached
1282 usb_dev = ohci_usb_allocate(root_hub->usb);
1283 dev = usb_dev->hcpriv;
1285 dev->ohci = ohci;
1287 usb_connect(dev->usb);
1289 /* link it into the bus's device tree */
1290 root_hub->usb->children[port] = usb_dev;
1292 wait_ms(200); /* wait for powerup; XXX is this needed? */
1293 ohci_reset_port(ohci, port);
1295 /* Get information on speed by using LSD */
1296 usb_dev->slow = readl(portaddr) & PORT_LSDA ? 1 : 0;
1299 * Do generic USB device tree processing on the new device.
1301 usb_new_device(usb_dev);
1303 } /* ohci_connect_change() */
1307 * This gets called when the root hub configuration
1308 * has changed. Just go through each port, seeing if
1309 * there is something interesting happening.
1311 static void ohci_check_configuration(struct ohci *ohci)
1313 struct ohci_regs *regs = ohci->regs;
1314 int num = 0;
1315 int maxport = readl(&ohci->regs->roothub) & 0xff;
1316 __u32 rh_change_flags = PORT_CSC | PORT_PESC; /* root hub status changes */
1318 #ifdef OHCI_DEBUG
1319 printk(KERN_DEBUG "entering ohci_check_configuration %p\n", ohci);
1320 #endif
1322 do {
1323 __u32 *portstatus_p = &regs->roothub.portstatus[num];
1324 if (readl(portstatus_p) & rh_change_flags) {
1325 /* acknowledge the root hub status changes */
1326 writel_set(rh_change_flags, portstatus_p);
1327 /* disable the port if nothing is on it */
1328 /* check the port for a nifty device */
1329 ohci_connect_change(ohci, num);
1331 } while (++num < maxport);
1333 #if 0
1334 printk(KERN_DEBUG "leaving ohci_check_configuration %p\n", ohci);
1335 #endif
1336 } /* ohci_check_configuration() */
1341 * Check root hub port status and wake the control thread up if
1342 * anything has changed.
1344 * This function is called from the interrupt handler.
1346 static void ohci_root_hub_events(struct ohci *ohci)
1348 int num = 0;
1349 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
1350 int maxport = root_hub->usb->maxchild;
1352 if (!waitqueue_active(&ohci_configure))
1353 return;
1354 do {
1355 __u32 *portstatus_p = &ohci->regs->roothub.portstatus[num];
1356 if (readl(portstatus_p) & PORT_CSC) {
1357 if (waitqueue_active(&ohci_configure))
1358 wake_up(&ohci_configure);
1359 return;
1361 } while (++num < maxport);
1363 } /* ohci_root_hub_events() */
1367 * The done list is in reverse order; we need to process TDs in the
1368 * order they were finished (FIFO). This function builds the FIFO
1369 * list using the next_dl_td pointer.
1371 * This function originally by Roman Weissgaerber (weissg@vienna.at)
1373 * This function is called from the interrupt handler.
1375 static struct ohci_td * ohci_reverse_donelist(struct ohci * ohci)
1377 __u32 td_list_hc;
1378 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
1379 struct ohci_hcca *hcca = root_hub->hcca;
1380 struct ohci_td *td_list = NULL;
1381 struct ohci_td *td_rev = NULL;
1383 td_list_hc = le32_to_cpup(&hcca->donehead) & 0xfffffff0;
1384 hcca->donehead = 0;
1386 while(td_list_hc) {
1387 td_list = (struct ohci_td *) bus_to_virt(td_list_hc);
1388 td_list->next_dl_td = td_rev;
1389 td_rev = td_list;
1390 td_list_hc = le32_to_cpup(&td_list->next_td) & 0xfffffff0;
1393 return td_list;
1394 } /* ohci_reverse_donelist() */
1398 * Collect this interrupt's goodies off of the list of finished TDs
1399 * that the OHCI controller is kind enough to setup for us.
1401 * This function is called from the interrupt handler.
1403 static void ohci_reap_donelist(struct ohci *ohci)
1405 struct ohci_td *td; /* used for walking the list */
1407 spin_lock(&ohci_edtd_lock);
1409 /* create the FIFO ordered donelist */
1410 td = ohci_reverse_donelist(ohci);
1412 while (td != NULL) {
1413 struct ohci_td *next_td = td->next_dl_td;
1414 int cc = OHCI_TD_CC_GET(le32_to_cpup(&td->info));
1416 if (td_dummy(*td))
1417 printk(KERN_ERR "yikes! reaping a dummy TD\n");
1419 /* FIXME: munge td->info into a future standard status format */
1421 if (cc != 0 && ohci_ed_halted(td->ed) && td->completed == 0) {
1423 * There was an error on this TD and the ED
1424 * is halted, and this was not the last TD
1425 * of the transaction, so there will be TDs
1426 * to clean off the ED.
1427 * (We assume that a TD with a non-NULL completed
1428 * field is the last one of a transaction.
1429 * Ultimately we should have a flag in the TD
1430 * to say that it is the last one.)
1432 struct ohci_ed *ed = td->ed;
1433 struct ohci_td *tail_td = bus_to_virt(ed_tail_td(ed));
1434 struct ohci_td *ntd;
1436 ohci_free_td(td);
1437 td = ntd = bus_to_virt(ed_head_td(ed));
1438 while (td != tail_td) {
1439 ntd = bus_to_virt(le32_to_cpup(&td->next_td));
1440 if (td->completed != 0)
1441 break;
1442 ohci_free_td(td);
1443 td = ntd;
1445 /* Set the ED head past the ones we cleaned
1446 off, and clear the halted flag */
1447 set_ed_head_td(ed, virt_to_bus(ntd));
1448 ohci_unhalt_ed(ed);
1449 /* If we didn't find a TD with a completion
1450 routine, give up */
1451 if (td == tail_td) {
1452 td = next_td;
1453 continue;
1457 /* Check if TD should be re-queued */
1458 if ((td->completed != NULL) &&
1459 (td->completed(cc, td->data, td->dev_id))) {
1460 /* Mark the TD as active again:
1461 * Set the not accessed condition code
1462 * Reset the Error count
1464 td->info |= cpu_to_le32(OHCI_TD_CC_NEW);
1465 clear_td_errorcount(td);
1466 /* reset the toggle field to TOGGLE_AUTO (0) */
1467 td->info &= cpu_to_le32(~OHCI_TD_DT);
1469 /* point it back to the start of the data buffer */
1470 td->cur_buf = cpu_to_le32(virt_to_bus(td->data));
1472 /* insert it back on its ED */
1473 ohci_add_td_to_ed(td, td, td->ed);
1474 } else {
1475 /* return it to the pool of free TDs */
1476 ohci_free_td(td);
1479 td = next_td;
1482 spin_unlock(&ohci_edtd_lock);
1483 } /* ohci_reap_donelist() */
1487 * Get annoyed at the controller for bothering us.
1488 * This pretty much follows the OHCI v1.0a spec, section 5.3.
1490 static void ohci_interrupt(int irq, void *__ohci, struct pt_regs *r)
1492 struct ohci *ohci = __ohci;
1493 struct ohci_regs *regs = ohci->regs;
1494 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
1495 struct ohci_hcca *hcca = root_hub->hcca;
1496 __u32 status, context;
1498 /* Save the status of the interrupts that are enabled */
1499 status = readl(&regs->intrstatus);
1500 status &= readl(&regs->intrenable);
1502 /* make context = the interrupt status bits that we care about */
1503 if (hcca->donehead != 0) {
1504 context = OHCI_INTR_WDH; /* hcca donehead needs processing */
1505 if (hcca->donehead & cpu_to_le32(1)) {
1506 context |= status; /* other status change to check */
1508 } else {
1509 context = status;
1510 if (!context) {
1511 /* TODO increment a useless interrupt counter here */
1512 return;
1516 /* Disable HC interrupts */ /* why? - paulus */
1517 writel(OHCI_INTR_MIE, &regs->intrdisable);
1519 /* Process the done list */
1520 if (context & OHCI_INTR_WDH) {
1521 /* See which TD's completed.. */
1522 ohci_reap_donelist(ohci);
1524 /* reset the done queue and tell the controller */
1525 hcca->donehead = 0; /* XXX already done in ohci_reverse_donelist */
1526 writel(OHCI_INTR_WDH, &regs->intrstatus);
1528 context &= ~OHCI_INTR_WDH; /* mark this as checked */
1531 #ifdef OHCI_RHSC_INT
1532 /* NOTE: this is very funky on some USB controllers (ie: it
1533 * doesn't work right). Using the ohci_timer instead to poll
1534 * the root hub is a much better choice. */
1535 /* Process any root hub status changes */
1536 if (context & OHCI_INTR_RHSC) {
1537 /* Wake the thread to process root hub events */
1538 if (waitqueue_active(&ohci_configure))
1539 wake_up(&ohci_configure);
1541 writel(OHCI_INTR_RHSC, &regs->intrstatus);
1543 * Don't unset RHSC in context; it should be disabled.
1544 * The control thread will re-enable it after it has
1545 * checked the root hub status.
1548 #endif
1550 /* Start of Frame interrupts, used during safe ED removal */
1551 if (context & (OHCI_INTR_SF)) {
1552 writel(OHCI_INTR_SF, &regs->intrstatus);
1553 if (waitqueue_active(&start_of_frame_wakeup))
1554 wake_up(&start_of_frame_wakeup);
1555 /* Do NOT mark the frame start interrupt as checked
1556 * as we don't want to receive any more of them until
1557 * asked. */
1560 /* Check those "other" pesky bits */
1561 if (context & (OHCI_INTR_FNO)) {
1562 writel(OHCI_INTR_FNO, &regs->intrstatus);
1563 context &= ~OHCI_INTR_FNO; /* mark this as checked */
1565 if (context & OHCI_INTR_SO) {
1566 writel(OHCI_INTR_SO, &regs->intrstatus);
1567 context &= ~OHCI_INTR_SO; /* mark this as checked */
1569 if (context & OHCI_INTR_RD) {
1570 writel(OHCI_INTR_RD, &regs->intrstatus);
1571 context &= ~OHCI_INTR_RD; /* mark this as checked */
1573 if (context & OHCI_INTR_UE) {
1574 /* FIXME: need to have the control thread reset the
1575 * controller now and keep a count of unrecoverable
1576 * errors. If there are too many, it should just shut
1577 * the broken controller down entirely. */
1578 writel(OHCI_INTR_UE, &regs->intrstatus);
1579 context &= ~OHCI_INTR_UE; /* mark this as checked */
1581 if (context & OHCI_INTR_OC) {
1582 writel(OHCI_INTR_OC, &regs->intrstatus);
1583 context &= ~OHCI_INTR_OC; /* mark this as checked */
1586 /* Mask out any remaining unprocessed or unmasked interrupts
1587 * so that we don't get any more of them. */
1588 if (context & ~OHCI_INTR_MIE) {
1589 writel(context, &regs->intrdisable);
1592 /* Re-enable HC interrupts */
1593 writel(OHCI_INTR_MIE, &regs->intrenable);
1595 } /* ohci_interrupt() */
1599 * Allocate the resources required for running an OHCI controller.
1600 * Host controller interrupts must not be running while calling this
1601 * function or the penguins will get angry.
1603 * The mem_base parameter must be the usable -virtual- address of the
1604 * host controller's memory mapped I/O registers.
1606 static struct ohci *alloc_ohci(void* mem_base)
1608 int i;
1609 struct ohci *ohci;
1610 struct usb_bus *bus;
1611 struct ohci_device *dev;
1612 struct usb_device *usb;
1614 #if 0
1615 printk(KERN_DEBUG "entering alloc_ohci %p\n", mem_base);
1616 #endif
1618 ohci = kmalloc(sizeof(*ohci), GFP_KERNEL);
1619 if (!ohci)
1620 return NULL;
1622 memset(ohci, 0, sizeof(*ohci));
1624 ohci->irq = -1;
1625 ohci->regs = mem_base;
1626 INIT_LIST_HEAD(&ohci->interrupt_list);
1628 bus = kmalloc(sizeof(*bus), GFP_KERNEL);
1629 if (!bus)
1630 return NULL;
1632 memset(bus, 0, sizeof(*bus));
1634 ohci->bus = bus;
1635 bus->hcpriv = ohci;
1636 bus->op = &ohci_device_operations;
1639 * Allocate the USB device structure and root hub.
1641 * Here we allocate our own root hub and TDs as well as the
1642 * OHCI host controller communications area. The HCCA is just
1643 * a nice pool of memory with pointers to endpoint descriptors
1644 * for the different interrupts.
1646 usb = ohci_usb_allocate(NULL);
1647 if (!usb)
1648 return NULL;
1650 dev = usb_to_ohci(usb);
1651 ohci->bus->root_hub= ohci_to_usb(dev);
1652 usb->bus = bus;
1654 /* Initialize the root hub */
1655 dev->ohci = ohci; /* link back to the controller */
1658 * Allocate the Host Controller Communications Area on a 256
1659 * byte boundary. XXX take the easy way out and just grab a
1660 * page as that's guaranteed to have a nice boundary.
1662 dev->hcca = (struct ohci_hcca *) __get_free_page(GFP_KERNEL);
1663 memset(dev->hcca, 0, sizeof(struct ohci_hcca));
1665 /* Tell the controller where the HCCA is */
1666 writel(virt_to_bus(dev->hcca), &ohci->regs->hcca);
1668 #if 0
1669 printk(KERN_DEBUG "usb-ohci: HCCA allocated at %p (bus %p)\n", dev->hcca, (void*)virt_to_bus(dev->hcca));
1670 #endif
1672 /* Get the number of ports on the root hub */
1673 usb->maxchild = readl(&ohci->regs->roothub.a) & 0xff;
1674 if (usb->maxchild > MAX_ROOT_PORTS) {
1675 printk(KERN_INFO "usb-ohci: Limited to %d ports\n", MAX_ROOT_PORTS);
1676 usb->maxchild = MAX_ROOT_PORTS;
1678 if (usb->maxchild < 1) {
1679 printk(KERN_ERR "usb-ohci: Less than one root hub port? Impossible!\n");
1680 usb->maxchild = 1;
1682 printk(KERN_DEBUG "usb-ohci: %d root hub ports found\n", usb->maxchild);
1685 * Initialize the ED polling "tree" (for simplicity's sake in
1686 * this driver many nodes in the tree will be identical)
1688 dev->ed[ED_INT_32].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_16]));
1689 dev->ed[ED_INT_16].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_8]));
1690 dev->ed[ED_INT_8].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_4]));
1691 dev->ed[ED_INT_4].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_2]));
1692 dev->ed[ED_INT_2].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_1]));
1695 * Initialize the polling table to call interrupts at the
1696 * intended intervals. Note that these EDs are just
1697 * placeholders. They have their SKIP bit set and are used as
1698 * list heads to insert real EDs onto.
1700 dev->hcca->int_table[0] = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_1]));
1701 for (i = 1; i < NUM_INTS; i++) {
1702 if (i & 16)
1703 dev->hcca->int_table[i] =
1704 cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_32]));
1705 if (i & 8)
1706 dev->hcca->int_table[i] =
1707 cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_16]));
1708 if (i & 4)
1709 dev->hcca->int_table[i] =
1710 cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_8]));
1711 if (i & 2)
1712 dev->hcca->int_table[i] =
1713 cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_4]));
1714 if (i & 1)
1715 dev->hcca->int_table[i] =
1716 cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_2]));
1720 * Tell the controller where the control and bulk lists are.
1721 * The lists start out empty.
1723 writel(0, &ohci->regs->ed_controlhead);
1724 writel(0, &ohci->regs->ed_bulkhead);
1726 #ifdef OHCI_DEBUG
1727 printk(KERN_DEBUG "alloc_ohci(): controller\n");
1728 show_ohci_status(ohci);
1729 #endif
1731 #if 0
1732 printk(KERN_DEBUG "leaving alloc_ohci %p\n", ohci);
1733 #endif
1735 return ohci;
1736 } /* alloc_ohci() */
1740 * De-allocate all resoueces..
1742 static void release_ohci(struct ohci *ohci)
1744 printk(KERN_INFO "Releasing OHCI controller 0x%p\n", ohci);
1746 #ifdef OHCI_TIMER
1747 /* stop our timer */
1748 del_timer(&ohci_timer);
1749 #endif
1750 if (ohci->irq >= 0) {
1751 free_irq(ohci->irq, ohci);
1752 ohci->irq = -1;
1755 /* stop all OHCI interrupts */
1756 writel(~0x0, &ohci->regs->intrdisable);
1758 if (ohci->bus->root_hub) {
1759 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
1760 /* ensure that HC is stopped before releasing the HCCA */
1761 writel(OHCI_USB_SUSPEND, &ohci->regs->control);
1762 free_page((unsigned long) root_hub->hcca);
1763 kfree(ohci->bus->root_hub);
1764 root_hub->hcca = NULL;
1765 ohci->bus->root_hub = NULL;
1768 /* unmap the IO address space */
1769 iounmap(ohci->regs);
1771 kfree(ohci);
1773 MOD_DEC_USE_COUNT;
1775 /* If the ohci itself were dynamic we'd free it here */
1777 printk(KERN_DEBUG "usb-ohci: HC resources released.\n");
1778 } /* release_ohci() */
1782 * USB OHCI control thread
1784 static int ohci_control_thread(void * __ohci)
1786 struct ohci *ohci = (struct ohci *)__ohci;
1789 * I'm unfamiliar with the SMP kernel locking.. where should
1790 * this be released and what does it do? -greg
1792 lock_kernel();
1795 * This thread doesn't need any user-level access,
1796 * so get rid of all of our resources..
1798 printk(KERN_DEBUG "ohci-control thread code for 0x%p code at 0x%p\n", __ohci, &ohci_control_thread);
1799 exit_mm(current);
1800 exit_files(current);
1801 exit_fs(current);
1803 strcpy(current->comm, "ohci-control");
1805 usb_register_bus(ohci->bus);
1808 * Damn the torpedoes, full speed ahead
1810 if (start_hc(ohci) < 0) {
1811 printk(KERN_ERR "usb-ohci: failed to start the controller\n");
1812 release_ohci(ohci);
1813 usb_deregister_bus(ohci->bus);
1814 printk(KERN_INFO "leaving ohci_control_thread %p\n", __ohci);
1815 return 0;
1818 for(;;) {
1819 siginfo_t info;
1820 int unsigned long signr;
1822 wait_ms(200);
1824 /* check the root hub configuration for changes. */
1825 ohci_check_configuration(ohci);
1827 /* re-enable root hub status change interrupts. */
1828 #ifdef OHCI_RHSC_INT
1829 writel(OHCI_INTR_RHSC, &ohci->regs->intrenable);
1830 #endif
1832 printk(KERN_DEBUG "ohci-control thread sleeping\n");
1833 interruptible_sleep_on(&ohci_configure);
1834 #ifdef CONFIG_APM
1835 if (apm_resume) {
1836 apm_resume = 0;
1837 if (start_hc(ohci) < 0)
1838 break;
1839 continue;
1841 #endif
1844 * If we were woken up by a signal, see if its useful,
1845 * otherwise exit.
1847 if (signal_pending(current)) {
1848 /* sending SIGUSR1 makes us print out some info */
1849 spin_lock_irq(&current->sigmask_lock);
1850 signr = dequeue_signal(&current->blocked, &info);
1851 spin_unlock_irq(&current->sigmask_lock);
1853 if(signr == SIGUSR1) {
1854 /* TODO: have it do a full ed/td queue dump? */
1855 printk(KERN_DEBUG "OHCI status dump:\n");
1856 show_ohci_status(ohci);
1857 } else if (signr == SIGUSR2) {
1858 /* toggle mega TD/ED debugging output */
1859 MegaDebug = !MegaDebug;
1860 printk(KERN_DEBUG "usb-ohci: Mega debugging %sabled.\n",
1861 MegaDebug ? "en" : "dis");
1862 } else {
1863 /* unknown signal, exit the thread */
1864 break;
1867 } /* for (;;) */
1869 reset_hc(ohci);
1870 release_ohci(ohci);
1871 usb_deregister_bus(ohci->bus);
1872 printk(KERN_DEBUG "ohci-control thread for 0x%p exiting\n", __ohci);
1874 return 0;
1875 } /* ohci_control_thread() */
1878 #ifdef CONFIG_APM
1879 static int handle_apm_event(apm_event_t event)
1881 static int down = 0;
1883 switch (event) {
1884 case APM_SYS_SUSPEND:
1885 case APM_USER_SUSPEND:
1886 if (down) {
1887 printk(KERN_DEBUG "usb-ohci: received extra suspend event\n");
1888 break;
1890 down = 1;
1891 break;
1892 case APM_NORMAL_RESUME:
1893 case APM_CRITICAL_RESUME:
1894 if (!down) {
1895 printk(KERN_DEBUG "usb-ohci: received bogus resume event\n");
1896 break;
1898 down = 0;
1899 if (waitqueue_active(&ohci_configure)) {
1900 apm_resume = 1;
1901 wake_up(&ohci_configure);
1903 break;
1905 return 0;
1906 } /* handle_apm_event() */
1907 #endif
1910 #ifdef OHCI_TIMER
1912 * Inspired by Iñaky's driver. This function is a timer routine that
1913 * is called every OHCI_TIMER_FREQ ms. It polls the root hub for
1914 * status changes as on my system the RHSC interrupt just doesn't
1915 * play well with others.. (so RHSC is turned off by default in this
1916 * driver)
1917 * [my controller is a "SiS 7001 USB (rev 16)"]
1918 * -greg
1920 static void ohci_timer_func (unsigned long ohci_ptr)
1922 struct ohci *ohci = (struct ohci*)ohci_ptr;
1924 ohci_root_hub_events(ohci);
1926 /* set the next timer */
1927 mod_timer(&ohci_timer, jiffies + ((OHCI_TIMER_FREQ*HZ)/1000));
1929 } /* ohci_timer_func() */
1930 #endif
1934 * Increment the module usage count, start the control thread and
1935 * return success if the controller is good.
1937 static int found_ohci(int irq, void* mem_base)
1939 int retval;
1940 struct ohci *ohci;
1942 #if 0
1943 printk(KERN_DEBUG "entering found_ohci %d %p\n", irq, mem_base);
1944 #endif
1946 /* Allocate the running OHCI structures */
1947 ohci = alloc_ohci(mem_base);
1948 if (!ohci) {
1949 return -ENOMEM;
1952 #ifdef OHCI_TIMER
1953 init_timer(&ohci_timer);
1954 ohci_timer.expires = jiffies + ((OHCI_TIMER_FREQ*HZ)/1000);
1955 ohci_timer.data = (unsigned long)ohci;
1956 ohci_timer.function = ohci_timer_func;
1957 add_timer(&ohci_timer);
1958 #endif
1960 retval = -EBUSY;
1961 if (request_irq(irq, ohci_interrupt, SA_SHIRQ, "usb-ohci", ohci) == 0) {
1962 int pid;
1964 ohci->irq = irq;
1966 #ifdef OHCI_DEBUG
1967 printk(KERN_DEBUG "usb-ohci: forking ohci-control thread for 0x%p\n", ohci);
1968 #endif
1970 /* fork off the handler */
1971 pid = kernel_thread(ohci_control_thread, ohci,
1972 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
1973 if (pid >= 0) {
1974 return 0;
1977 retval = pid;
1978 } else {
1979 printk(KERN_ERR "usb-ohci: Couldn't allocate interrupt %d\n", irq);
1981 release_ohci(ohci);
1983 #ifdef OHCI_DEBUG
1984 printk(KERN_DEBUG "leaving found_ohci %d %p\n", irq, mem_base);
1985 #endif
1987 return retval;
1988 } /* found_ohci() */
1992 * If this controller is for real, map the IO memory and proceed
1994 static int init_ohci(struct pci_dev *dev)
1996 unsigned long mem_base = dev->base_address[0];
1998 /* If its OHCI, its memory */
1999 if (mem_base & PCI_BASE_ADDRESS_SPACE_IO)
2000 return -ENODEV;
2002 /* Get the memory address and map it for IO */
2003 mem_base &= PCI_BASE_ADDRESS_MEM_MASK;
2005 /* no interrupt won't work... */
2006 if (dev->irq == 0) {
2007 printk(KERN_ERR "usb-ohci: no irq assigned? check your BIOS settings.\n");
2008 return -ENODEV;
2012 * FIXME ioremap_nocache isn't implemented on all CPUs (such
2013 * as the Alpha) [?] What should I use instead...
2015 * The iounmap() is done on in release_ohci.
2017 mem_base = (unsigned long) ioremap_nocache(mem_base, 4096);
2019 if (!mem_base) {
2020 printk(KERN_ERR "Error mapping OHCI memory\n");
2021 return -EFAULT;
2023 MOD_INC_USE_COUNT;
2025 #ifdef OHCI_DEBUG
2026 printk(KERN_INFO "usb-ohci: Warning! Gobs of debugging output has been enabled.\n");
2027 printk(KERN_INFO " Check your kern.debug logs for the bulk of it.\n");
2028 #endif
2030 if (found_ohci(dev->irq, (void *) mem_base) < 0) {
2031 MOD_DEC_USE_COUNT;
2032 return -1;
2035 return 0;
2036 } /* init_ohci() */
2038 /* TODO this should be named following Linux convention and go in pci.h */
2039 #define PCI_CLASS_SERIAL_USB_OHCI ((PCI_CLASS_SERIAL_USB << 8) | 0x0010)
2042 * Search the PCI bus for an OHCI USB controller and set it up
2044 * If anyone wants multiple controllers this will need to be
2045 * updated.. Right now, it just picks the first one it finds.
2047 int ohci_init(void)
2049 int retval;
2050 struct pci_dev *dev = NULL;
2051 /*u8 type;*/
2053 if (sizeof(struct ohci_device) > 4096) {
2054 printk(KERN_ERR "usb-ohci: struct ohci_device to large\n");
2055 return -ENODEV;
2058 printk(KERN_INFO "OHCI USB Driver loading\n");
2060 retval = -ENODEV;
2061 for (;;) {
2062 /* Find an OHCI USB controller */
2063 dev = pci_find_class(PCI_CLASS_SERIAL_USB_OHCI, dev);
2064 if (!dev)
2065 break;
2067 /* Verify that its OpenHCI by checking for MMIO */
2068 /* pci_read_config_byte(dev, PCI_CLASS_PROG, &type);
2069 if (!type)
2070 continue; */
2072 /* Ok, set it up */
2073 retval = init_ohci(dev);
2074 if (retval < 0)
2075 continue;
2077 #ifdef CONFIG_APM
2078 apm_register_callback(&handle_apm_event);
2079 #endif
2081 return 0; /* no error */
2083 return retval;
2084 } /* ohci_init */
2087 /* vim:sw=8
2090 #ifdef MODULE
2092 * Clean up when unloading the module
2094 void cleanup_module(void){
2095 # ifdef CONFIG_APM
2096 apm_unregister_callback(&handle_apm_event);
2097 # endif
2098 printk(KERN_ERR "usb-ohci: module unloaded\n");
2101 int init_module(void){
2102 return ohci_init();
2104 #endif //MODULE