Import 2.3.11pre5
[davej-history.git] / drivers / usb / ohci.c
blobf35479b8a1546dbc1be9fb8625984c7813df316b
1 /*
2 * Open Host Controller Interface driver for USB.
4 * (C) Copyright 1999 Gregory P. Smith <greg@electricrain.com>
5 * Significant code from the following individuals has also been used:
6 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at> [ohci-hcd.c]
7 * (C) Copyright 1999 Linus Torvalds [uhci.c]
9 * This is the "other" host controller interface for USB. You will
10 * find this on many non-Intel based motherboards, and of course the
11 * Mac. As Linus hacked his UHCI driver together first, I originally
12 * modeled this after his.. (it should be obvious)
14 * To get started in USB, I used the "Universal Serial Bus System
15 * Architecture" book by Mindshare, Inc. It was a reasonable introduction
16 * and overview of USB and the two dominant host controller interfaces
17 * however you're better off just reading the real specs available
18 * from www.usb.org as you'll need them to get enough detailt to
19 * actually implement a HCD. The book has many typos and omissions
20 * Beware, the specs are the victim of a committee.
22 * This code was written with Guinness on the brain, xsnow on the desktop
23 * and Orbital, Orb, Enya & Massive Attack on the CD player. What a life! ;)
25 * No filesystems were harmed in the development of this code.
27 * $Id: ohci.c,v 1.43 1999/05/16 22:35:24 greg Exp $
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/pci.h>
33 #include <linux/kernel.h>
34 #include <linux/delay.h>
35 #include <linux/ioport.h>
36 #include <linux/sched.h>
37 #include <linux/malloc.h>
38 #include <linux/smp_lock.h>
39 #include <linux/errno.h>
41 #include <asm/spinlock.h>
42 #include <asm/io.h>
43 #include <asm/irq.h>
44 #include <asm/system.h>
46 #include "ohci.h"
48 #ifdef CONFIG_APM
49 #include <linux/apm_bios.h>
50 static int handle_apm_event(apm_event_t event);
51 static int apm_resume = 0;
52 #endif
54 static DECLARE_WAIT_QUEUE_HEAD(ohci_configure);
56 #ifdef CONFIG_USB_OHCI_DEBUG
57 #define OHCI_DEBUG /* to make typing it easier.. */
58 #endif
60 int MegaDebug = 0; /* SIGUSR2 to the control thread toggles this */
63 #ifdef OHCI_TIMER
64 static struct timer_list ohci_timer; /* timer for root hub polling */
65 #endif
67 static spinlock_t ohci_edtd_lock = SPIN_LOCK_UNLOCKED;
69 #define FIELDS_OF_ED(e) le32_to_cpup(&e->status), le32_to_cpup(&e->tail_td), \
70 le32_to_cpup(&e->_head_td), le32_to_cpup(&e->next_ed)
71 #define FIELDS_OF_TD(t) le32_to_cpup(&t->info), le32_to_cpup(&t->cur_buf), \
72 le32_to_cpup(&t->next_td), le32_to_cpup(&t->buf_end)
74 #ifdef OHCI_DEBUG
75 static const char *cc_names[16] = {
76 "no error",
77 "CRC error",
78 "bit stuff error",
79 "data toggle mismatch",
80 "stall",
81 "device not responding",
82 "PID check failed",
83 "unexpected PID",
84 "data overrun",
85 "data underrun",
86 "reserved (10)",
87 "reserved (11)",
88 "buffer overrun",
89 "buffer underrun",
90 "not accessed (14)",
91 "not accessed"
93 #endif
96 * Add a chain of TDs to the end of the TD list on a given ED.
98 * This function uses the first TD of the chain as the new dummy TD
99 * for the ED, and uses the old dummy TD instead of the first TD
100 * of the chain. The reason for this is that this makes it possible
101 * to update the TD chain without needing any locking between the
102 * CPU and the OHCI controller.
104 * The return value is the pointer to the new first TD (the old
105 * dummy TD).
107 * Important! This function is not re-entrant w.r.t. each ED.
108 * Locking ohci_edtd_lock while using the function is a must
109 * if there is any possibility of another CPU or an interrupt routine
110 * calling this function with the same ED.
112 * This function can be called by the interrupt handler.
114 static struct ohci_td *ohci_add_td_to_ed(struct ohci_td *td,
115 struct ohci_td *last_td, struct ohci_ed *ed)
117 struct ohci_td *t, *dummy_td;
118 u32 new_dummy;
120 if (ed->tail_td == 0) {
121 printk(KERN_ERR "eek! an ED without a dummy_td\n");
122 return td;
125 /* Get a pointer to the current dummy TD. */
126 dummy_td = bus_to_virt(ed_tail_td(ed));
128 for (t = td; ; t = bus_to_virt(le32_to_cpup(&t->next_td))) {
129 t->ed = ed;
130 if (t == last_td)
131 break;
134 /* Make the last TD point back to the first, since it
135 * will become the new dummy TD. */
136 new_dummy = cpu_to_le32(virt_to_bus(td));
137 last_td->next_td = new_dummy;
139 /* Copy the contents of the first TD into the dummy */
140 *dummy_td = *td;
142 /* Turn the first TD into a dummy */
143 make_dumb_td(td);
145 /* Set the HC's tail pointer to the new dummy */
146 ed->tail_td = new_dummy;
148 return dummy_td; /* replacement head of chain */
149 } /* ohci_add_td_to_ed() */
153 * Add a whole chain of TDs to an ED using the above function.
154 * The same restrictions apply.
156 * XXX This function is being removed in the future! XXX
158 static struct ohci_td *ohci_add_td_chain_to_ed(struct ohci_td *td, struct ohci_ed *ed)
160 struct ohci_td *cur_td;
161 if (!td)
162 return NULL;
164 /* Find the last TD in this chain, storing its pointer in cur_td */
165 cur_td = td;
166 for (;;) {
167 __u32 next_td = cur_td->next_td;
169 /* advance to the next td, exit if there isn't one */
170 if (!next_td)
171 break;
172 cur_td = bus_to_virt(le32_to_cpup(&next_td));
175 return td = ohci_add_td_to_ed(td, cur_td, ed);
176 } /* ohci_add_td_chain_to_ed() */
179 /* .......... */
182 void ohci_start_control(struct ohci *ohci)
184 /* tell the HC to start processing the control list */
185 writel_set(OHCI_USB_CLE, &ohci->regs->control);
186 writel_set(OHCI_CMDSTAT_CLF, &ohci->regs->cmdstatus);
189 void ohci_start_bulk(struct ohci *ohci)
191 /* tell the HC to start processing the bulk list */
192 writel_set(OHCI_USB_BLE, &ohci->regs->control);
193 writel_set(OHCI_CMDSTAT_BLF, &ohci->regs->cmdstatus);
196 void ohci_start_periodic(struct ohci *ohci)
198 /* enable processing periodic (intr) transfers starting next frame */
199 writel_set(OHCI_USB_PLE, &ohci->regs->control);
202 void ohci_start_isoc(struct ohci *ohci)
204 /* enable processing isoc. transfers starting next frame */
205 writel_set(OHCI_USB_IE, &ohci->regs->control);
209 * Add an ED to the hardware register ED list pointed to by hw_listhead_p
210 * This function only makes sense for Control and Bulk EDs.
212 static void ohci_add_ed_to_hw(struct ohci_ed *ed, void* hw_listhead_p)
214 __u32 listhead;
215 unsigned long flags;
217 spin_lock_irqsave(&ohci_edtd_lock, flags);
219 listhead = readl(hw_listhead_p);
221 /* if the list is not empty, insert this ED at the front */
222 /* XXX should they go on the end? */
223 ed->next_ed = cpu_to_le32(listhead);
225 /* update the hardware listhead pointer */
226 writel(virt_to_bus(ed), hw_listhead_p);
228 spin_unlock_irqrestore(&ohci_edtd_lock, flags);
229 } /* ohci_add_ed_to_hw() */
233 * Put a control ED on the controller's list
235 void ohci_add_control_ed(struct ohci *ohci, struct ohci_ed *ed)
237 ohci_add_ed_to_hw(ed, &ohci->regs->ed_controlhead);
238 ohci_start_control(ohci);
239 } /* ohci_add_control_ed() */
242 * Put a bulk ED on the controller's list
244 void ohci_add_bulk_ed(struct ohci *ohci, struct ohci_ed *ed)
246 ohci_add_ed_to_hw(ed, &ohci->regs->ed_bulkhead);
247 ohci_start_bulk(ohci);
248 } /* ohci_add_bulk_ed() */
251 * Put a periodic ED on the appropriate list given the period.
253 void ohci_add_periodic_ed(struct ohci *ohci, struct ohci_ed *ed, int period)
255 struct ohci_ed *int_ed;
256 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
257 unsigned long flags;
260 * Pick a good frequency endpoint based on the requested period
262 int_ed = &root_hub->ed[ms_to_ed_int(period)];
263 #ifdef OHCI_DEBUG
264 printk(KERN_DEBUG "usb-ohci: Using INT ED queue %d for %dms period\n",
265 ms_to_ed_int(period), period);
266 #endif
268 spin_lock_irqsave(&ohci_edtd_lock, flags);
270 * Insert this ED at the front of the list.
272 ed->next_ed = int_ed->next_ed;
273 int_ed->next_ed = cpu_to_le32(virt_to_bus(ed));
275 spin_unlock_irqrestore(&ohci_edtd_lock, flags);
277 ohci_start_periodic(ohci);
278 } /* ohci_add_periodic_ed() */
281 * Put an isochronous ED on the controller's list
283 inline void ohci_add_isoc_ed(struct ohci *ohci, struct ohci_ed *ed)
285 ohci_add_periodic_ed(ohci, ed, 1);
290 * This will be used for the interrupt to wake us up on the next SOF
292 DECLARE_WAIT_QUEUE_HEAD(start_of_frame_wakeup);
294 static void ohci_wait_sof(struct ohci_regs *regs)
296 DECLARE_WAITQUEUE(wait, current);
298 add_wait_queue(&start_of_frame_wakeup, &wait);
300 /* clear the SOF interrupt status and enable it */
301 writel(OHCI_INTR_SF, &regs->intrstatus);
302 writel(OHCI_INTR_SF, &regs->intrenable);
304 schedule_timeout(HZ/10);
306 remove_wait_queue(&start_of_frame_wakeup, &wait);
310 * Guarantee that an ED is safe to be modified by the HCD (us).
312 * This function can NOT be called from an interrupt.
314 void ohci_wait_for_ed_safe(struct ohci_regs *regs, struct ohci_ed *ed, int ed_type)
316 __u32 *hw_listcurrent;
318 /* tell the controller to skip this ED */
319 ed->status |= cpu_to_le32(OHCI_ED_SKIP);
321 switch (ed_type) {
322 case HCD_ED_CONTROL:
323 hw_listcurrent = &regs->ed_controlcurrent;
324 break;
325 case HCD_ED_BULK:
326 hw_listcurrent = &regs->ed_bulkcurrent;
327 break;
328 case HCD_ED_ISOC:
329 case HCD_ED_INT:
330 hw_listcurrent = &regs->ed_periodcurrent;
331 break;
332 default:
333 return;
337 * If the HC is processing this ED we need to wait until the
338 * at least the next frame.
340 if (virt_to_bus(ed) == readl(hw_listcurrent)) {
341 #ifdef OHCI_DEBUG
342 printk(KERN_INFO "Waiting a frame for OHC to finish with ED %p [%x %x %x %x]\n", ed, FIELDS_OF_ED(ed));
343 #endif
344 ohci_wait_sof(regs);
348 return; /* The ED is now safe */
349 } /* ohci_wait_for_ed_safe() */
353 * Remove an ED from the HC's list.
354 * This function can ONLY be used for Control or Bulk EDs.
356 * Note that the SKIP bit is left on in the removed ED.
358 void ohci_remove_norm_ed_from_hw(struct ohci *ohci, struct ohci_ed *ed, int ed_type)
360 unsigned long flags;
361 struct ohci_regs *regs = ohci->regs;
362 struct ohci_ed *cur;
363 __u32 bus_ed = virt_to_bus(ed);
364 __u32 bus_cur;
365 __u32 *hw_listhead_p;
367 if (ed == NULL || !bus_ed)
368 return;
369 ed->status |= cpu_to_le32(OHCI_ED_SKIP);
371 switch (ed_type) {
372 case HCD_ED_CONTROL:
373 hw_listhead_p = &regs->ed_controlhead;
374 break;
375 case HCD_ED_BULK:
376 hw_listhead_p = &regs->ed_bulkhead;
377 break;
378 default:
379 printk(KERN_ERR "Unknown HCD ED type %d.\n", ed_type);
380 return;
383 bus_cur = readl(hw_listhead_p);
385 if (bus_cur == 0)
386 return; /* the list is already empty */
388 cur = bus_to_virt(bus_cur);
390 spin_lock_irqsave(&ohci_edtd_lock, flags);
392 /* if its the head ED, move the head */
393 if (bus_cur == bus_ed) {
394 writel(le32_to_cpup(&cur->next_ed), hw_listhead_p);
395 } else if (cur->next_ed != 0) {
396 struct ohci_ed *prev;
398 /* walk the list and unlink the ED if found */
399 do {
400 prev = cur;
401 cur = bus_to_virt(le32_to_cpup(&cur->next_ed));
403 if (cur == ed) {
404 /* unlink from the list */
405 prev->next_ed = cur->next_ed;
406 break;
408 } while (cur->next_ed != 0);
412 * Make sure this ED is not being accessed by the HC as we speak.
414 ohci_wait_for_ed_safe(regs, ed, ed_type);
416 /* clear any links from the ED for safety */
417 ed->next_ed = 0;
419 spin_unlock_irqrestore(&ohci_edtd_lock, flags);
420 } /* ohci_remove_norm_ed_from_hw() */
423 * Remove an ED from the controller's control list. Note that the SKIP bit
424 * is left on in the removed ED.
426 inline void ohci_remove_control_ed(struct ohci *ohci, struct ohci_ed *ed)
428 ohci_remove_norm_ed_from_hw(ohci, ed, HCD_ED_CONTROL);
432 * Remove an ED from the controller's bulk list. Note that the SKIP bit
433 * is left on in the removed ED.
435 inline void ohci_remove_bulk_ed(struct ohci *ohci, struct ohci_ed *ed)
437 ohci_remove_norm_ed_from_hw(ohci, ed, HCD_ED_BULK);
442 * Remove a periodic ED from the host controller
444 void ohci_remove_periodic_ed(struct ohci *ohci, struct ohci_ed *ed)
446 struct ohci_device *root_hub = usb_to_ohci(ohci->bus->root_hub);
447 struct ohci_ed *cur_ed = NULL, *prev_ed;
448 unsigned long flags;
450 /* FIXME: this will need to up fixed when add_periodic_ed()
451 * is updated to spread similar polling rate EDs out over
452 * multiple periodic queues. Currently this assumes that the
453 * 32ms (slowest) polling queue links to all others... */
455 /* search the periodic EDs, skipping the first one which is
456 * only a placeholder. */
457 prev_ed = &root_hub->ed[ED_INT_32];
458 if (prev_ed->next_ed)
459 cur_ed = bus_to_virt(le32_to_cpup(&prev_ed->next_ed));
461 while (cur_ed) {
462 if (ed == cur_ed) { /* remove the ED */
463 /* set its SKIP bit and be sure its not in use */
464 ohci_wait_for_ed_safe(ohci->regs, ed, HCD_ED_INT);
466 /* unlink it */
467 spin_lock_irqsave(&ohci_edtd_lock, flags);
468 prev_ed->next_ed = ed->next_ed;
469 spin_unlock_irqrestore(&ohci_edtd_lock, flags);
470 ed->next_ed = 0;
472 break;
475 spin_lock_irqsave(&ohci_edtd_lock, flags);
476 if (cur_ed->next_ed) {
477 prev_ed = cur_ed;
478 cur_ed = bus_to_virt(le32_to_cpup(&cur_ed->next_ed));
479 spin_unlock_irqrestore(&ohci_edtd_lock, flags);
480 } else {
481 spin_unlock_irqrestore(&ohci_edtd_lock, flags);
483 /* if multiple polling queues need to be checked,
484 * here is where you'd advance to the next one */
486 printk("usb-ohci: ed %p not found on periodic queue\n", ed);
487 break;
490 } /* ohci_remove_periodic_ed() */
494 * Remove all the EDs which have a given device address from a list.
495 * Used when the device is unplugged.
496 * Returns 1 if anything was changed.
498 static int ohci_remove_device_list(__u32 *headp, int devnum)
500 struct ohci_ed *ed;
501 __u32 *prevp = headp;
502 int removed = 0;
504 while (*prevp != 0) {
505 ed = bus_to_virt(le32_to_cpup(prevp));
506 if ((le32_to_cpup(&ed->status) & OHCI_ED_FA) == devnum) {
507 /* set the controller to skip this one
508 and remove it from the list */
509 ed->status |= cpu_to_le32(OHCI_ED_SKIP);
510 *prevp = ed->next_ed;
511 removed = 1;
512 } else {
513 prevp = &ed->next_ed;
516 wmb();
518 return removed;
522 * Remove all the EDs for a given device from all lists.
524 void ohci_remove_device(struct ohci *ohci, int devnum)
526 unsigned long flags;
527 __u32 head;
528 struct ohci_regs *regs = ohci->regs;
529 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
531 spin_lock_irqsave(&ohci_edtd_lock, flags);
533 /* Control list */
534 head = cpu_to_le32(readl(&regs->ed_controlhead));
535 if (ohci_remove_device_list(&head, devnum))
536 writel(le32_to_cpup(&head), &regs->ed_controlhead);
538 /* Bulk list */
539 head = cpu_to_le32(readl(&regs->ed_bulkhead));
540 if (ohci_remove_device_list(&head, devnum))
541 writel(le32_to_cpup(&head), &regs->ed_bulkhead);
543 /* Interrupt/iso list */
544 head = cpu_to_le32(virt_to_bus(&root_hub->ed[ED_INT_32]));
545 ohci_remove_device_list(&head, devnum);
548 * Wait until the start of the next frame to ensure
549 * that the HC has seen any changes.
551 ohci_wait_sof(ohci->regs);
553 spin_unlock_irqrestore(&ohci_edtd_lock, flags);
557 * Remove a TD from the given EDs TD list. The TD is freed as well.
559 static void ohci_remove_td_from_ed(struct ohci_td *td, struct ohci_ed *ed)
561 unsigned long flags;
562 struct ohci_td *head_td;
564 if ((td == NULL) || (ed == NULL))
565 return;
567 if (ed_head_td(ed) == 0)
568 return;
570 spin_lock_irqsave(&ohci_edtd_lock, flags);
572 /* set the "skip me bit" in this ED */
573 ed->status |= cpu_to_le32(OHCI_ED_SKIP);
575 /* XXX Assuming this list will never be circular */
577 head_td = bus_to_virt(ed_head_td(ed));
578 if (virt_to_bus(td) == ed_head_td(ed)) {
579 /* It's the first TD, remove it. */
580 set_ed_head_td(ed, head_td->next_td);
581 } else {
582 struct ohci_td *prev_td, *cur_td;
584 /* FIXME: collapse this into a nice simple loop :) */
585 if (head_td->next_td != 0) {
586 prev_td = head_td;
587 cur_td = bus_to_virt(le32_to_cpup(&head_td->next_td));
588 for (;;) {
589 if (td == cur_td) {
590 /* remove it */
591 prev_td->next_td = cur_td->next_td;
592 break;
594 if (cur_td->next_td == 0)
595 break;
596 prev_td = cur_td;
597 cur_td = bus_to_virt(le32_to_cpup(&cur_td->next_td));
602 td->next_td = 0; /* remove the TDs links */
603 td->ed = NULL;
605 /* return this TD to the pool of free TDs */
606 ohci_free_td(td);
608 /* unset the "skip me bit" in this ED */
609 ed->status &= cpu_to_le32(~OHCI_ED_SKIP);
611 spin_unlock_irqrestore(&ohci_edtd_lock, flags);
612 } /* ohci_remove_td_from_ed() */
616 * Get a pointer (virtual) to an available TD from the given device's
617 * pool. Return NULL if none are left.
619 static struct ohci_td *ohci_get_free_td(struct ohci_device *dev)
621 int idx;
623 #if 0
624 printk(KERN_DEBUG "in ohci_get_free_td()\n");
625 #endif
627 /* FIXME: this is horribly inefficient */
628 for (idx=0; idx < NUM_TDS; idx++) {
629 #if 0
630 show_ohci_td(&dev->td[idx]);
631 #endif
632 if (!td_allocated(dev->td[idx])) {
633 struct ohci_td *new_td = &dev->td[idx];
634 /* zero out the TD */
635 memset(new_td, 0, sizeof(*new_td));
636 /* mark the new TDs as unaccessed */
637 new_td->info = cpu_to_le32(OHCI_TD_CC_NEW);
638 /* mark it as allocated */
639 allocate_td(new_td);
640 /* record the device that its on */
641 new_td->usb_dev = ohci_to_usb(dev);
642 return new_td;
646 printk(KERN_ERR "usb-ohci: unable to allocate a TD\n");
647 return NULL;
648 } /* ohci_get_free_td() */
652 * Get a pointer (virtual) to an available TD from the given device's
653 * pool. Return NULL if none are left.
655 * NOTE: This function does not allocate and attach the dummy_td.
656 * That is done in ohci_fill_ed(). FIXME: it should probably be moved
657 * into here.
659 static struct ohci_ed *ohci_get_free_ed(struct ohci_device *dev)
661 int idx;
663 /* FIXME: this is horribly inefficient */
664 for (idx=0; idx < NUM_EDS; idx++) {
665 if (!ed_allocated(dev->ed[idx])) {
666 struct ohci_ed *new_ed = &dev->ed[idx];
667 /* zero out the ED */
668 memset(new_ed, 0, sizeof(*new_ed));
669 /* all new EDs start with the SKIP bit set */
670 new_ed->status |= cpu_to_le32(OHCI_ED_SKIP);
671 /* mark it as allocated */
672 allocate_ed(new_ed);
673 return new_ed;
677 printk(KERN_ERR "usb-ohci: unable to allocate an ED\n");
678 return NULL;
679 } /* ohci_get_free_ed() */
683 * Free an OHCI ED and all of the TDs on its list. It is assumed that
684 * this ED is not active. You should call ohci_wait_for_ed_safe()
685 * beforehand if you can't guarantee that.
687 void ohci_free_ed(struct ohci_ed *ed)
689 if (!ed)
690 return;
692 if (ed_head_td(ed) != 0) {
693 struct ohci_td *td, *tail_td, *next_td;
695 td = bus_to_virt(ed_head_td(ed));
696 tail_td = bus_to_virt(ed_tail_td(ed));
697 for (;;) {
698 next_td = bus_to_virt(le32_to_cpup(&td->next_td));
699 ohci_free_td(td);
700 if (td == tail_td)
701 break;
702 td = next_td;
706 ed->status &= cpu_to_le32(~(__u32)ED_ALLOCATED);
707 } /* ohci_free_ed() */
711 * Initialize a TD
713 * dir = OHCI_TD_D_IN, OHCI_TD_D_OUT, or OHCI_TD_D_SETUP
714 * toggle = TOGGLE_AUTO, TOGGLE_DATA0, TOGGLE_DATA1
716 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)
718 /* hardware fields */
719 td->info = cpu_to_le32(OHCI_TD_CC_NEW |
720 (dir & OHCI_TD_D) |
721 (toggle & OHCI_TD_DT) |
722 flags);
723 td->cur_buf = (data == NULL) ? 0 : cpu_to_le32(virt_to_bus(data));
724 td->buf_end = (len == 0) ? 0 :
725 cpu_to_le32(le32_to_cpup(&td->cur_buf) + len - 1);
727 /* driver fields */
728 td->data = data;
729 td->dev_id = dev_id;
730 td->completed = completed;
732 #if 0
733 printk(KERN_DEBUG "ohci_fill_new_td created:\n");
734 show_ohci_td(td);
735 #endif
737 return td;
738 } /* ohci_fill_new_td() */
742 * Initialize a new ED on device dev, including allocating and putting the
743 * dummy tail_td on its queue if it doesn't already have one. Any
744 * TDs on this ED other than the dummy will be lost (so there better
745 * not be any!). This assumes that the ED is Allocated and will
746 * force the Allocated bit on.
748 struct ohci_ed *ohci_fill_ed(struct ohci_device *dev, struct ohci_ed *ed,
749 int maxpacketsize, int lowspeed, int endp_id,
750 int isoc_tds)
752 struct ohci_td *dummy_td;
754 if (ed_head_td(ed) != ed_tail_td(ed))
755 printk(KERN_ERR "Reusing a non-empty ED %p!\n", ed);
757 if (!ed->tail_td) {
758 dummy_td = ohci_get_free_td(dev);
759 if (dummy_td == NULL) {
760 printk(KERN_ERR "Error allocating dummy TD for ED %p\n", ed);
761 return NULL; /* no dummy available! */
763 make_dumb_td(dummy_td); /* flag it as a dummy */
764 ed->tail_td = cpu_to_le32(virt_to_bus(dummy_td));
765 } else {
766 dummy_td = bus_to_virt(ed_tail_td(ed));
767 if (!td_dummy(*dummy_td))
768 printk(KERN_ERR "ED %p's dummy %p is screwy\n", ed, dummy_td);
771 /* set the head TD to the dummy and clear the Carry & Halted bits */
772 ed->_head_td = ed->tail_td;
774 ed->status = cpu_to_le32(
775 ed_set_maxpacket(maxpacketsize) |
776 ed_set_speed(lowspeed) |
777 (endp_id & 0x7ff) |
778 ((isoc_tds == 0) ? OHCI_ED_F_NORM : OHCI_ED_F_ISOC));
779 allocate_ed(ed);
780 ed->next_ed = 0;
782 return ed;
783 } /* ohci_fill_ed() */
787 * Create a chain of Normal TDs to be used for a large data transfer
788 * (bulk or control).
790 * Returns the head TD in the chain.
792 struct ohci_td *ohci_build_td_chain(struct ohci_device *dev, void *data, unsigned int len, int dir, __u32 toggle, int round, int auto_free, void* dev_id, usb_device_irq handler, __u32 next_td)
794 struct ohci_td *head, *cur_td;
795 __u32 bus_data_start, bus_data_end;
796 unsigned short max_page0_len;
798 if (!data || (len == 0))
799 return NULL;
801 /* Setup the first TD, leaving buf_end = 0 */
802 head = ohci_get_free_td(dev);
803 if (head == NULL) {
804 printk(KERN_ERR "usb-ohci: out of TDs\n");
805 return NULL;
808 ohci_fill_new_td(head,
809 td_set_dir_out(dir),
810 toggle & OHCI_TD_DT,
811 (round ? OHCI_TD_ROUND : 0),
812 data, 0,
813 dev_id, handler);
814 if (!auto_free)
815 noauto_free_td(head);
817 cur_td = head;
819 /* AFICT, that the OHCI controller takes care of the innards of
820 * bulk & control data transfers by sending zero length
821 * packets as necessary if the transfer falls on an even packet
822 * size boundary, we don't need a special TD for that. */
824 while (len > 0) {
825 bus_data_start = virt_to_bus(data);
826 bus_data_end = virt_to_bus(data+(len-1));
828 /* check the 4096 byte alignment of the start of the data */
829 max_page0_len = 0x1000 - (bus_data_start & 0xfff);
831 /* check if the remaining data occupies more than two pages */
832 if ((max_page0_len < len) && (len - max_page0_len > 0x1000)) {
833 struct ohci_td *new_td;
835 /* Point this TD to data up through the end of
836 * the second page */
837 cur_td->buf_end = bus_data_start +
838 (max_page0_len + 0xfff);
840 /* adjust the data pointer & remaining length */
841 data += (max_page0_len + 0x1000);
842 len -= (max_page0_len + 0x1000);
844 /* TODO lookup effect of rounding bit on
845 * individual TDs vs. whole TD chain transfers;
846 * disable cur_td's rounding bit here if needed. */
848 /* mark that this is not the last TD... */
849 clear_td_endofchain(cur_td);
851 /* allocate another td */
852 new_td = ohci_get_free_td(dev);
853 if (new_td == NULL) {
854 printk(KERN_ERR "usb-ohci: out of TDs\n");
855 /* FIXME: free any allocated TDs */
856 return NULL;
859 ohci_fill_new_td(new_td,
860 td_set_dir_out(dir),
861 TOGGLE_AUTO, /* toggle Data0/1 via the ED */
862 round ? OHCI_TD_ROUND : 0,
863 data, 0,
864 dev_id, handler);
865 if (!auto_free)
866 noauto_free_td(new_td);
868 /* Link the new TD to the chain & advance */
869 cur_td->next_td = virt_to_bus(new_td);
870 cur_td = new_td;
871 } else {
872 /* Last TD in this chain, normal buf_end is fine */
873 cur_td->buf_end = bus_data_end;
875 set_td_endofchain(cur_td);
877 len = 0;
878 break;
880 } /* while */
882 /* link the given next_td to the end of this chain */
883 cur_td->next_td = next_td;
885 return head;
886 } /* ohci_build_td_chain() */
890 * Compute the number of bytes that have been transferred on a given
891 * TD. Do not call this on TDs that are active on the host
892 * controller.
894 static __u16 ohci_td_bytes_done(struct ohci_td *td)
896 __u16 result;
897 __u32 bus_data_start, bus_data_end;
899 bus_data_start = virt_to_bus(td->data);
900 if (!td->data || !bus_data_start)
901 return 0;
903 /* if cur_buf is 0, all data has been transferred */
904 if (!td->cur_buf) {
905 return td->buf_end - bus_data_start + 1;
908 bus_data_end = td->cur_buf;
910 /* is it on the same page? */
911 if ((bus_data_start & ~0xfff) == (bus_data_end & ~0xfff)) {
912 result = bus_data_end - bus_data_start;
913 } else {
914 /* compute the amount transferred on the first page */
915 result = 0x1000 - (bus_data_start & 0xfff);
916 /* add the amount done in the second page */
917 result += (bus_data_end & 0xfff);
920 return result;
921 } /* ohci_td_bytes_done() */
924 /**********************************
925 * OHCI interrupt list operations *
926 **********************************/
929 * Request an interrupt handler for one "pipe" of a USB device.
930 * (this function is pretty minimal right now)
932 * At the moment this is only good for input interrupts. (ie: for a
933 * mouse or keyboard)
935 * Period is desired polling interval in ms. The closest, shorter
936 * match will be used. Powers of two from 1-32 are supported by OHCI.
938 * Returns: a "handle pointer" that release_irq can use to stop this
939 * interrupt. (It's really a pointer to the TD). NULL = error.
941 static void* ohci_request_irq(struct usb_device *usb, unsigned int pipe,
942 usb_device_irq handler, int period, void *dev_id)
944 struct ohci_device *dev = usb_to_ohci(usb);
945 struct ohci_td *td;
946 struct ohci_ed *interrupt_ed; /* endpoint descriptor for this irq */
947 int maxps = usb_maxpacket(usb, pipe);
949 /* Get an ED and TD */
950 interrupt_ed = ohci_get_free_ed(dev);
951 if (!interrupt_ed) {
952 printk(KERN_ERR "Out of EDs on device %p in ohci_request_irq\n", dev);
953 return NULL;
956 td = ohci_get_free_td(dev);
957 if (!td) {
958 printk(KERN_ERR "Out of TDs in ohci_request_irq\n");
959 ohci_free_ed(interrupt_ed);
960 return NULL;
964 * Set the max packet size, device speed, endpoint number, usb
965 * device number (function address), and type of TD.
967 ohci_fill_ed(dev, interrupt_ed, maxps, usb_pipeslow(pipe),
968 usb_pipe_endpdev(pipe), 0 /* normal TDs */);
970 /* Fill in the TD */
971 if (maxps > sizeof(dev->data))
972 maxps = sizeof(dev->data);
973 ohci_fill_new_td(td, td_set_dir_out(usb_pipeout(pipe)),
974 TOGGLE_AUTO,
975 OHCI_TD_ROUND,
976 dev->data, maxps,
977 dev_id, handler);
980 * Put the TD onto our ED and make sure its ready to run
982 td = ohci_add_td_to_ed(td, td, interrupt_ed);
983 interrupt_ed->status &= cpu_to_le32(~OHCI_ED_SKIP);
984 ohci_unhalt_ed(interrupt_ed);
986 /* Make sure all the stores above get done before
987 * the store which tells the OHCI about the new ed. */
988 wmb();
990 /* Assimilate the new ED into the collective */
991 ohci_add_periodic_ed(dev->ohci, interrupt_ed, period);
993 return (void*)td;
994 } /* ohci_request_irq() */
997 * Release an interrupt handler previously allocated using
998 * ohci_request_irq. This function does no validity checking, so make
999 * sure you're not releasing an already released handle as it may be
1000 * in use by something else..
1002 * This function can NOT be called from an interrupt.
1004 int ohci_release_irq(void* handle)
1006 struct ohci_device *dev;
1007 struct ohci_td *int_td;
1008 struct ohci_ed *int_ed;
1010 #ifdef OHCI_DEBUG
1011 if (handle)
1012 printk("usb-ohci: Releasing irq handle %p\n", handle);
1013 #endif
1015 int_td = (struct ohci_td*)handle;
1016 if (int_td == NULL)
1017 return USB_ST_INTERNALERROR;
1019 dev = usb_to_ohci(int_td->usb_dev);
1020 int_ed = int_td->ed;
1022 ohci_remove_periodic_ed(dev->ohci, int_ed);
1024 /* Tell the driver that the IRQ has been killed. */
1025 /* Passing NULL in the "buffer" void* along with the
1026 * USB_ST_REMOVED status is the signal. */
1027 if (int_td->completed != NULL)
1028 int_td->completed(USB_ST_REMOVED, NULL, 0, int_td->dev_id);
1030 /* Free the ED (& TD) */
1031 ohci_free_ed(int_ed);
1033 return USB_ST_NOERROR;
1034 } /* ohci_release_irq() */
1037 /************************************
1038 * OHCI control transfer operations *
1039 ************************************/
1041 static DECLARE_WAIT_QUEUE_HEAD(control_wakeup);
1044 * This is the handler that gets called when a control transaction
1045 * completes.
1047 * This function is called from the interrupt handler.
1049 static int ohci_control_completed(int stats, void *buffer, int len, void *dev_id)
1051 /* pass the TDs completion status back to control_msg */
1052 if (dev_id) {
1053 int *completion_status = (int *)dev_id;
1054 *completion_status = stats;
1057 wake_up(&control_wakeup);
1058 return 0;
1059 } /* ohci_control_completed() */
1063 * Send or receive a control message on a "pipe"
1065 * The cmd parameter is a pointer to the 8 byte setup command to be
1066 * sent.
1068 * A control message contains:
1069 * - The command itself
1070 * - An optional data phase (if len > 0)
1071 * - Status complete phase
1073 * This function can NOT be called from an interrupt.
1075 static int ohci_control_msg(struct usb_device *usb, unsigned int pipe,
1076 devrequest *cmd, void *data, int len)
1078 struct ohci_device *dev = usb_to_ohci(usb);
1079 struct ohci_ed *control_ed = ohci_get_free_ed(dev);
1080 struct ohci_td *setup_td, *data_td, *status_td;
1081 DECLARE_WAITQUEUE(wait, current);
1082 int completion_status = -1;
1083 devrequest our_cmd;
1085 /* byte-swap fields of cmd if necessary */
1086 our_cmd = *cmd;
1087 cpu_to_le16s(&our_cmd.value);
1088 cpu_to_le16s(&our_cmd.index);
1089 cpu_to_le16s(&our_cmd.length);
1091 #ifdef OHCI_DEBUG
1092 if (MegaDebug)
1093 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);
1094 #endif
1095 if (!control_ed) {
1096 printk(KERN_ERR "usb-ohci: couldn't get ED for dev %p\n", dev);
1097 return -1;
1100 /* get a TD to send this control message with */
1101 setup_td = ohci_get_free_td(dev);
1102 if (!setup_td) {
1103 printk(KERN_ERR "usb-ohci: couldn't get TD for dev %p [cntl setup]\n", dev);
1104 ohci_free_ed(control_ed);
1105 return -1;
1109 * Set the max packet size, device speed, endpoint number, usb
1110 * device number (function address), and type of TD.
1113 ohci_fill_ed(dev, control_ed, usb_maxpacket(usb,pipe), usb_pipeslow(pipe),
1114 usb_pipe_endpdev(pipe), 0 /* normal TDs */);
1117 * Build the control TD
1121 * Set the not accessed condition code, allow odd sized data,
1122 * and set the data transfer type to SETUP. Setup DATA always
1123 * uses a DATA0 packet.
1125 * The setup packet contains a devrequest (usb.h) which
1126 * will always be 8 bytes long.
1128 ohci_fill_new_td(setup_td, OHCI_TD_D_SETUP, TOGGLE_DATA0,
1129 OHCI_TD_IOC_OFF,
1130 &our_cmd, 8, /* cmd is always 8 bytes long */
1131 &completion_status, NULL);
1133 /* Allocate a TD for the control xfer status */
1134 status_td = ohci_get_free_td(dev);
1135 if (!status_td) {
1136 printk("usb-ohci: couldn't get TD for dev %p [cntl status]\n", dev);
1137 ohci_free_td(setup_td);
1138 ohci_free_ed(control_ed);
1139 return -1;
1142 /* The control status packet always uses a DATA1
1143 * Give "dev_id" the address of completion_status so that the
1144 * TDs status can be passed back to us from the IRQ. */
1145 ohci_fill_new_td(status_td,
1146 td_set_dir_in(usb_pipeout(pipe) | (len == 0)),
1147 TOGGLE_DATA1,
1148 0 /* flags */,
1149 NULL /* data */, 0 /* data len */,
1150 &completion_status, ohci_control_completed);
1151 status_td->next_td = 0; /* end of TDs */
1153 /* If there is data to transfer, create the chain of data TDs
1154 * followed by the status TD. */
1155 if (len > 0) {
1156 data_td = ohci_build_td_chain( dev, data, len,
1157 usb_pipeout(pipe), TOGGLE_DATA1,
1158 1 /* round */, 1 /* autofree */,
1159 &completion_status, NULL /* no handler here */,
1160 virt_to_bus(status_td) );
1161 if (!data_td) {
1162 printk(KERN_ERR "usb-ohci: couldn't allocate control data TDs for dev %p\n", dev);
1163 ohci_free_td(setup_td);
1164 ohci_free_td(status_td);
1165 ohci_free_ed(control_ed);
1166 return -1;
1169 /* link the to the data & status TDs */
1170 setup_td->next_td = virt_to_bus(data_td);
1171 } else {
1172 /* no data TDs, link to the status TD */
1173 setup_td->next_td = virt_to_bus(status_td);
1177 * Add the control TDs to the control ED (setup_td is the first)
1179 setup_td = ohci_add_td_chain_to_ed(setup_td, control_ed);
1180 control_ed->status &= ~OHCI_ED_SKIP;
1181 ohci_unhalt_ed(control_ed);
1183 #ifdef OHCI_DEBUG
1184 if (MegaDebug) {
1185 /* complete transaction debugging output (before) */
1186 printk(KERN_DEBUG " Control ED %lx:\n", virt_to_bus(control_ed));
1187 show_ohci_ed(control_ed);
1188 printk(KERN_DEBUG " Control TD chain:\n");
1189 show_ohci_td_chain(setup_td);
1190 printk(KERN_DEBUG " OHCI Controller Status:\n");
1191 show_ohci_status(dev->ohci);
1193 #endif
1196 * Start the control transaction..
1198 current->state = TASK_UNINTERRUPTIBLE;
1199 add_wait_queue(&control_wakeup, &wait);
1201 /* Give the ED to the HC */
1202 ohci_add_control_ed(dev->ohci, control_ed);
1204 schedule_timeout(HZ/10);
1206 remove_wait_queue(&control_wakeup, &wait);
1208 #ifdef OHCI_DEBUG
1209 if (MegaDebug) {
1210 /* complete transaction debugging output (after) */
1211 printk(KERN_DEBUG " *after* Control ED %lx:\n", virt_to_bus(control_ed));
1212 show_ohci_ed(control_ed);
1213 printk(KERN_DEBUG " *after* Control TD chain:\n");
1214 show_ohci_td_chain(setup_td);
1215 printk(KERN_DEBUG " *after* OHCI Controller Status:\n");
1216 show_ohci_status(dev->ohci);
1218 #endif
1220 /* no TD cleanup, the TDs were auto-freed as they finished */
1222 /* remove the control ED from the HC */
1223 ohci_remove_control_ed(dev->ohci, control_ed);
1224 ohci_free_ed(control_ed); /* return it to the pool */
1226 #ifdef OHCI_DEBUG
1227 if (completion_status != 0) {
1228 const char *what = (completion_status < 0)? "timed out":
1229 cc_names[completion_status & 0xf];
1230 printk(KERN_ERR "ohci_control_msg: %s on pipe %x cmd %x %x %x %x %x\n",
1231 what, pipe, cmd->requesttype, cmd->request,
1232 cmd->value, cmd->index, cmd->length);
1233 } else if (!usb_pipeout(pipe)) {
1234 unsigned char *q = data;
1235 int i;
1236 printk(KERN_DEBUG "ctrl msg %x %x %x %x %x on pipe %x returned:",
1237 cmd->requesttype, cmd->request, cmd->value, cmd->index,
1238 cmd->length, pipe);
1239 for (i = 0; i < len; ++i) {
1240 if (i % 16 == 0)
1241 printk("\n" KERN_DEBUG);
1242 printk(" %x", q[i]);
1244 printk("\n");
1246 #endif
1247 return completion_status;
1248 } /* ohci_control_msg() */
1251 /**********************************************************************
1252 * Bulk transfer processing
1253 **********************************************************************/
1256 * Internal state for an ohci_bulk_request
1258 struct ohci_bulk_request_state {
1259 struct usb_device *usb_dev;
1260 unsigned int pipe; /* usb "pipe" */
1261 void *data; /* ptr to data */
1262 int length; /* length to transfer */
1263 int _bytes_done; /* bytes transferred so far */
1264 unsigned long *bytes_transferred_p; /* where to increment */
1265 void *dev_id; /* pass to the completion handler */
1266 usb_device_irq completion; /* completion handler */
1270 * this handles the individual TDs of a (possibly) larger bulk
1271 * request. It keeps track of the total bytes transferred, calls the
1272 * final completion handler, etc.
1274 static int ohci_bulk_td_handler(int stats, void *buffer, int len, void *dev_id)
1276 struct ohci_bulk_request_state *req;
1278 req = (struct ohci_bulk_request_state *) dev_id;
1280 #ifdef OHCI_DEBUG
1281 printk(KERN_DEBUG "ohci_bulk_td_handler stats %x, buffer %p, len %d, req %p\n", stats, buffer, len, req);
1282 #endif
1284 /* only count TDs that were completed successfully */
1285 if (stats == USB_ST_NOERROR)
1286 req->_bytes_done += len;
1288 /* call the real completion handler when done or on an error */
1289 if ((stats != USB_ST_NOERROR) ||
1290 (req->_bytes_done >= req->length && req->completion != NULL)) {
1291 *req->bytes_transferred_p += req->_bytes_done;
1292 #ifdef OHCI_DEBUG
1293 printk(KERN_DEBUG "usb-ohci: bulk request %p ending\n", req);
1294 #endif
1295 req->completion(stats, buffer, req->_bytes_done, req->dev_id);
1298 return 0; /* do not re-queue the TD */
1299 } /* ohci_bulk_td_handler() */
1303 * Request to send or receive bulk data. The completion() function
1304 * will be called when the transfer has completed or been aborted due
1305 * to an error.
1307 * bytes_transferred_p is a pointer to an integer that will be
1308 * -incremented- by the number of bytes that have been successfully
1309 * transferred. The interrupt handler will update it after each
1310 * internal TD completes successfully.
1312 * This function can NOT be called from an interrupt (?)
1313 * (TODO: verify & fix this if needed).
1315 * Returns: a pointer to the ED being used for this request. At the
1316 * moment, removing & freeing it is the responsibilty of the caller.
1318 static struct ohci_ed* ohci_request_bulk(struct ohci_bulk_request_state *bulk_request)
1320 /* local names for the readonly fields */
1321 struct usb_device *usb_dev = bulk_request->usb_dev;
1322 unsigned int pipe = bulk_request->pipe;
1323 void *data = bulk_request->data;
1324 int len = bulk_request->length;
1326 struct ohci_device *dev = usb_to_ohci(usb_dev);
1327 struct ohci_ed *bulk_ed;
1328 struct ohci_td *head_td;
1329 unsigned long flags;
1331 #ifdef OHCI_DEBUG
1332 printk(KERN_DEBUG "ohci_request_bulk(%p) ohci_dev %p, completion %p, pipe %x, data %p, len %d\n", bulk_request, dev, bulk_request->completion, pipe, data, len);
1333 #endif
1335 bulk_ed = ohci_get_free_ed(dev);
1336 if (!bulk_ed) {
1337 printk("usb-ohci: couldn't get ED for dev %p\n", dev);
1338 return NULL;
1341 /* allocate & fill in the TDs for this request */
1342 head_td = ohci_build_td_chain(dev, data, len, usb_pipeout(pipe),
1343 TOGGLE_AUTO,
1344 0 /* round not required */, 1 /* autofree */,
1345 bulk_request, /* dev_id: the bulk_request */
1346 ohci_bulk_td_handler,
1347 0 /* no additional TDs */);
1348 if (!head_td) {
1349 printk("usb-ohci: couldn't get TDs for dev %p\n", dev);
1350 ohci_free_ed(bulk_ed);
1351 return NULL;
1354 /* Set the max packet size, device speed, endpoint number, usb
1355 * device number (function address), and type of TD. */
1356 ohci_fill_ed(dev, bulk_ed,
1357 usb_maxpacket(usb_dev, pipe),
1358 usb_pipeslow(pipe),
1359 usb_pipe_endpdev(pipe), 0 /* bulk uses normal TDs */);
1361 /* initialize the internal counter */
1362 bulk_request->_bytes_done = 0;
1365 * Add the TDs to the ED
1367 spin_lock_irqsave(&ohci_edtd_lock, flags);
1368 bulk_ed->status |= OHCI_ED_SKIP;
1369 head_td = ohci_add_td_chain_to_ed(head_td, bulk_ed);
1370 bulk_ed->status &= ~OHCI_ED_SKIP;
1371 ohci_unhalt_ed(bulk_ed);
1372 spin_unlock_irqrestore(&ohci_edtd_lock, flags);
1375 #ifdef OHCI_DEBUG
1376 if (MegaDebug) {
1377 /* complete request debugging output (before) */
1378 printk(KERN_DEBUG " Bulk ED %lx:\n", virt_to_bus(bulk_ed));
1379 show_ohci_ed(bulk_ed);
1380 printk(KERN_DEBUG " Bulk TDs %lx:\n", virt_to_bus(head_td));
1381 show_ohci_td_chain(head_td);
1383 #endif
1385 /* Give the ED to the HC */
1386 ohci_add_bulk_ed(dev->ohci, bulk_ed);
1388 return bulk_ed;
1389 } /* ohci_request_bulk() */
1392 static DECLARE_WAIT_QUEUE_HEAD(bulk_wakeup);
1395 static int ohci_bulk_msg_completed(int stats, void *buffer, int len, void *dev_id)
1397 #ifdef OHCI_DEBUG
1398 printk("ohci_bulk_msg_completed %x, %p, %d, %p\n", stats, buffer, len, dev_id);
1399 #endif
1400 if (dev_id != NULL) {
1401 int *completion_status = (int *)dev_id;
1402 *completion_status = stats;
1405 wake_up(&bulk_wakeup);
1406 return 0; /* don't requeue the TD */
1407 } /* ohci_bulk_msg_completed() */
1410 static int ohci_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, void *data, int len, unsigned long *bytes_transferred_p)
1412 DECLARE_WAITQUEUE(wait, current);
1413 int completion_status = USB_ST_INTERNALERROR;
1414 struct ohci_bulk_request_state req;
1415 struct ohci_ed *req_ed;
1417 #ifdef OHCI_DEBUG
1418 printk(KERN_DEBUG "ohci_bulk_msg %p pipe %x, data %p, len %d, bytes_transferred %p\n", usb_dev, pipe, data, len, bytes_transferred_p);
1419 #endif
1421 /* initialize bytes transferred to nothing */
1422 *bytes_transferred_p = 0;
1424 /* Hopefully this is similar to the "URP" (USB Request Packet) code
1425 * that michael gee is working on... */
1426 req.usb_dev = usb_dev;
1427 req.pipe = pipe;
1428 req.data = data;
1429 req.length = len;
1430 req.bytes_transferred_p = bytes_transferred_p;
1431 req.dev_id = &completion_status;
1432 req.completion = ohci_bulk_msg_completed;
1435 * Start the transaction..
1437 current->state = TASK_UNINTERRUPTIBLE;
1438 add_wait_queue(&bulk_wakeup, &wait);
1440 req_ed = ohci_request_bulk(&req);
1442 /* FIXME this should to wait for a caller specified time... */
1443 schedule_timeout(HZ*5);
1445 /* it'll only stay in this state of the request never finished */
1446 if (completion_status == USB_ST_INTERNALERROR) {
1447 struct ohci_device *dev = usb_to_ohci(usb_dev);
1448 struct ohci_regs *regs = dev->ohci->regs;
1450 #ifdef OHCI_DEBUG
1451 printk(KERN_DEBUG "ohci_bulk_msg timing out\n");
1452 #endif
1453 /* XXX This code should go into a function used to stop
1454 * a previously requested bulk transfer. -greg */
1456 /* stop the transfer & collect the number of bytes */
1457 ohci_wait_for_ed_safe(regs, req_ed, HCD_ED_BULK);
1459 /* Get the number of bytes transferred out of the head TD
1460 * on the ED if it didn't finish while we were waiting. */
1461 if ( ed_head_td(req_ed) &&
1462 (ed_head_td(req_ed) != ed_tail_td(req_ed)) ) {
1463 struct ohci_td *partial_td;
1464 partial_td = bus_to_virt(ed_head_td(req_ed));
1466 #ifdef OHCI_DEBUG
1467 if (MegaDebug) {
1468 show_ohci_td(partial_td);
1470 #endif
1471 /* Record the bytes as transferred */
1472 *bytes_transferred_p += ohci_td_bytes_done(partial_td);
1474 /* If there was an unreported error, return it.
1475 * Otherwise return a timeout */
1476 completion_status = OHCI_TD_CC_GET(partial_td->info);
1477 if (completion_status == USB_ST_NOERROR) {
1478 completion_status = USB_ST_TIMEOUT;
1484 remove_wait_queue(&bulk_wakeup, &wait);
1486 /* remove the ED from the HC */
1487 ohci_remove_bulk_ed(usb_to_ohci(usb_dev)->ohci, req_ed);
1488 ohci_free_ed(req_ed); /* return it to the pool */
1490 #ifdef OHCI_DEBUG
1491 printk(KERN_DEBUG "ohci_bulk_msg done, status %x (bytes_transferred = %ld).\n", completion_status, *bytes_transferred_p);
1492 #endif
1494 return completion_status;
1495 } /* ohci_bulk_msg() */
1498 /* .......... */
1503 * Allocate a new USB device to be attached to an OHCI controller
1505 static struct usb_device *ohci_usb_allocate(struct usb_device *parent)
1507 struct usb_device *usb_dev;
1508 struct ohci_device *dev;
1509 int idx;
1512 * Allocate the generic USB device
1514 usb_dev = kmalloc(sizeof(*usb_dev), GFP_KERNEL);
1515 if (!usb_dev)
1516 return NULL;
1518 memset(usb_dev, 0, sizeof(*usb_dev));
1521 * Allocate an OHCI device (EDs and TDs for this device)
1523 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1524 if (!dev) {
1525 kfree(usb_dev);
1526 return NULL;
1529 memset(dev, 0, sizeof(*dev));
1531 /* Initialize all EDs in a new device with the skip flag so that
1532 * they are ignored by the controller until set otherwise. */
1533 for (idx = 0; idx < NUM_EDS; ++idx) {
1534 dev->ed[idx].status = cpu_to_le32(OHCI_ED_SKIP);
1538 * Link them together
1540 usb_dev->hcpriv = dev;
1541 dev->usb = usb_dev;
1544 * Link the device to its parent (hub, etc..) if any.
1546 usb_dev->parent = parent;
1548 if (parent) {
1549 usb_dev->bus = parent->bus;
1550 dev->ohci = usb_to_ohci(parent)->ohci;
1553 return usb_dev;
1554 } /* ohci_usb_allocate() */
1558 * Free a usb device.
1560 * TODO This function needs to take better care of the EDs and TDs, etc.
1562 static int ohci_usb_deallocate(struct usb_device *usb_dev)
1564 struct ohci_device *dev = usb_to_ohci(usb_dev);
1566 ohci_remove_device(dev->ohci, usb_dev->devnum);
1568 /* kfree(usb_to_ohci(usb_dev)); */
1569 /* kfree(usb_dev); */
1570 return 0;
1575 * functions for the generic USB driver
1577 struct usb_operations ohci_device_operations = {
1578 ohci_usb_allocate,
1579 ohci_usb_deallocate,
1580 ohci_control_msg,
1581 ohci_bulk_msg,
1582 ohci_request_irq,
1583 ohci_release_irq,
1588 * Reset an OHCI controller. Returns >= 0 on success.
1590 * Afterwards the HC will be in the "suspend" state which prevents you
1591 * from writing to some registers. Bring it to the operational state
1592 * ASAP.
1594 static int reset_hc(struct ohci *ohci)
1596 int timeout = 10000; /* prevent an infinite loop */
1598 #if 0
1599 printk(KERN_INFO "usb-ohci: resetting HC %p\n", ohci);
1600 #endif
1602 writel(~0x0, &ohci->regs->intrdisable); /* Disable HC interrupts */
1603 writel(1, &ohci->regs->cmdstatus); /* HC Reset */
1604 writel_mask(0x3f, &ohci->regs->control); /* move to UsbReset state */
1606 while ((readl(&ohci->regs->cmdstatus) & OHCI_CMDSTAT_HCR) != 0) {
1607 if (!--timeout) {
1608 printk(KERN_ERR "usb-ohci: USB HC reset timed out!\n");
1609 return -1;
1611 udelay(1);
1614 printk(KERN_DEBUG "usb-ohci: HC %p reset.\n", ohci);
1616 return 0;
1617 } /* reset_hc() */
1621 * Reset and start an OHCI controller. Returns >= 0 on success.
1623 static int start_hc(struct ohci *ohci)
1625 int ret = 0;
1626 int fminterval;
1627 __u32 what_to_enable;
1629 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
1631 fminterval = readl(&ohci->regs->fminterval) & 0x3fff;
1632 #if 0
1633 printk(KERN_DEBUG "entering start_hc %p\n", ohci);
1634 #endif
1636 if (reset_hc(ohci) < 0)
1637 return -1;
1639 /* restore registers cleared by the reset */
1640 writel(virt_to_bus(root_hub->hcca), &ohci->regs->hcca);
1643 * XXX Should fminterval also be set here?
1644 * The spec suggests 0x2edf [11,999]. (FIXME: make this a constant)
1646 /* fminterval |= (0x2edf << 16); */
1647 fminterval = (10240 << 16) | 11999;
1648 writel(fminterval, &ohci->regs->fminterval);
1649 /* Start periodic transfers at 90% of fminterval (fmremaining
1650 * counts down; this will put them in the first 10% of the
1651 * frame). */
1652 writel((0x2edf*9)/10, &ohci->regs->periodicstart);
1655 * FNO (frame number overflow) could be enabled... they
1656 * occur every 32768 frames (every 32-33 seconds). This is
1657 * useful for debugging and as a bus heartbeat. -greg
1659 /* Choose the interrupts we care about */
1660 what_to_enable = OHCI_INTR_MIE |
1661 #ifdef OHCI_RHSC_INT
1662 OHCI_INTR_RHSC |
1663 #endif
1664 /* | OHCI_INTR_FNO */
1665 OHCI_INTR_WDH;
1666 writel( what_to_enable, &ohci->regs->intrenable);
1668 /* Enter the USB Operational state & start the frames a flowing.. */
1669 writel_set(OHCI_USB_OPER, &ohci->regs->control);
1671 /* Enable control lists */
1672 writel_set(OHCI_USB_IE | OHCI_USB_CLE | OHCI_USB_BLE, &ohci->regs->control);
1674 /* Force global power enable -gal@cs.uni-magdeburg.de */
1676 * This turns on global power switching for all the ports
1677 * and tells the HC that all of the ports should be powered on
1678 * all of the time.
1680 * TODO: This could be battery draining for laptops.. We
1681 * should implement power switching.
1683 writel_set( OHCI_ROOT_A_NPS, &ohci->regs->roothub.a );
1684 writel_mask( ~((__u32)OHCI_ROOT_A_PSM), &ohci->regs->roothub.a );
1686 /* Turn on power to the root hub ports (thanks Roman!) */
1687 writel( OHCI_ROOT_LPSC, &ohci->regs->roothub.status );
1689 printk(KERN_INFO "usb-ohci: host controller operational\n");
1691 return ret;
1692 } /* start_hc() */
1696 * Reset a root hub port
1698 static void ohci_reset_port(struct ohci *ohci, unsigned int port)
1700 int status;
1702 /* Don't allow overflows. */
1703 if (port >= MAX_ROOT_PORTS) {
1704 printk(KERN_ERR "usb-ohci: bad port #%d in ohci_reset_port\n", port);
1705 port = MAX_ROOT_PORTS-1;
1708 writel(PORT_PRS, &ohci->regs->roothub.portstatus[port]); /* Reset */
1711 * Wait for the reset to complete.
1713 wait_ms(20);
1715 /* check port status to see that the reset completed */
1716 status = readl(&ohci->regs->roothub.portstatus[port]);
1717 if (status & PORT_PRS) {
1718 /* reset failed, try harder? */
1719 printk(KERN_ERR "usb-ohci: port %d reset failed, retrying\n", port);
1720 writel(PORT_PRS, &ohci->regs->roothub.portstatus[port]);
1721 wait_ms(50);
1724 /* TODO we might need to re-enable the port here or is that
1725 * done elsewhere? */
1727 } /* ohci_reset_port */
1731 * This gets called if the connect status on the root hub changes.
1733 static void ohci_connect_change(struct ohci * ohci, int port)
1735 struct usb_device *usb_dev;
1736 struct ohci_device *dev;
1737 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
1738 /* memory I/O address of the port status register */
1739 __u32 *portaddr = &ohci->regs->roothub.portstatus[port];
1740 int portstatus;
1742 #ifdef OHCI_DEBUG
1743 printk(KERN_DEBUG "ohci_connect_change on port %d\n", port);
1744 #endif
1747 * Because of the status change we have to forget
1748 * everything we think we know about the device
1749 * on this root hub port. It may have changed.
1751 usb_disconnect(root_hub->usb->children + port);
1753 portstatus = readl(portaddr);
1755 /* disable the port if nothing is connected */
1756 if (!(portstatus & PORT_CCS)) {
1757 writel(PORT_CCS, portaddr);
1758 /* We need to reset the CSC bit -after- disabling the
1759 * port because it causes the CSC bit to come on
1760 * again... */
1761 wait_ms(20);
1762 writel(PORT_CSC, portaddr);
1763 #ifdef OHCI_DEBUG
1764 printk(KERN_DEBUG "ohci port %d disabled, nothing connected.\n", port);
1765 #endif
1766 return;
1770 * Allocate a device for the new thingy that's been attached
1772 usb_dev = ohci_usb_allocate(root_hub->usb);
1773 dev = usb_dev->hcpriv;
1775 dev->ohci = ohci;
1777 usb_connect(dev->usb);
1779 /* link it into the bus's device tree */
1780 root_hub->usb->children[port] = usb_dev;
1782 wait_ms(200); /* wait for powerup; XXX is this needed? */
1783 ohci_reset_port(ohci, port);
1785 /* Get information on speed by using LSD */
1786 usb_dev->slow = readl(portaddr) & PORT_LSDA ? 1 : 0;
1789 * Do generic USB device tree processing on the new device.
1791 usb_new_device(usb_dev);
1793 } /* ohci_connect_change() */
1797 * This gets called when the root hub configuration
1798 * has changed. Just go through each port, seeing if
1799 * there is something interesting happening.
1801 static void ohci_check_configuration(struct ohci *ohci)
1803 struct ohci_regs *regs = ohci->regs;
1804 int num = 0;
1805 int maxport = readl(&ohci->regs->roothub) & 0xff;
1806 __u32 rh_change_flags = PORT_CSC | PORT_PESC; /* root hub status changes */
1808 #ifdef OHCI_DEBUG
1809 printk(KERN_DEBUG "entering ohci_check_configuration %p\n", ohci);
1810 #endif
1812 do {
1813 __u32 *portstatus_p = &regs->roothub.portstatus[num];
1814 if (readl(portstatus_p) & rh_change_flags) {
1815 /* acknowledge the root hub status changes */
1816 writel_set(rh_change_flags, portstatus_p);
1817 /* disable the port if nothing is on it */
1818 /* check the port for a nifty device */
1819 ohci_connect_change(ohci, num);
1821 } while (++num < maxport);
1823 #if 0
1824 printk(KERN_DEBUG "leaving ohci_check_configuration %p\n", ohci);
1825 #endif
1826 } /* ohci_check_configuration() */
1831 * Check root hub port status and wake the control thread up if
1832 * anything has changed.
1834 * This function is called from the interrupt handler.
1836 static void ohci_root_hub_events(struct ohci *ohci)
1838 int num = 0;
1839 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
1840 int maxport = root_hub->usb->maxchild;
1842 if (!waitqueue_active(&ohci_configure))
1843 return;
1844 do {
1845 __u32 *portstatus_p = &ohci->regs->roothub.portstatus[num];
1846 if (readl(portstatus_p) & PORT_CSC) {
1847 if (waitqueue_active(&ohci_configure))
1848 wake_up(&ohci_configure);
1849 return;
1851 } while (++num < maxport);
1853 } /* ohci_root_hub_events() */
1857 * The done list is in reverse order; we need to process TDs in the
1858 * order they were finished (FIFO). This function builds the FIFO
1859 * list using the next_dl_td pointer.
1861 * This function originally by Roman Weissgaerber (weissg@vienna.at)
1863 * This function is called from the interrupt handler.
1865 static struct ohci_td * ohci_reverse_donelist(struct ohci * ohci)
1867 __u32 td_list_hc;
1868 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
1869 struct ohci_hcca *hcca = root_hub->hcca;
1870 struct ohci_td *td_list = NULL;
1871 struct ohci_td *td_rev = NULL;
1873 td_list_hc = le32_to_cpup(&hcca->donehead) & 0xfffffff0;
1874 hcca->donehead = 0;
1876 while(td_list_hc) {
1877 td_list = (struct ohci_td *) bus_to_virt(td_list_hc);
1878 td_list->next_dl_td = td_rev;
1879 td_rev = td_list;
1880 td_list_hc = le32_to_cpup(&td_list->next_td) & 0xfffffff0;
1883 return td_list;
1884 } /* ohci_reverse_donelist() */
1888 * Collect this interrupt's goodies off of the list of finished TDs
1889 * that the OHCI controller is kind enough to setup for us.
1891 * This function is called from the interrupt handler.
1893 static void ohci_reap_donelist(struct ohci *ohci)
1895 struct ohci_td *td; /* used for walking the list */
1897 /* um... isn't this dangerous to do in an interrupt handler? -greg */
1898 // spin_lock(&ohci_edtd_lock);
1900 /* create the FIFO ordered donelist */
1901 td = ohci_reverse_donelist(ohci);
1903 while (td != NULL) {
1904 struct ohci_td *next_td = td->next_dl_td;
1905 int cc = OHCI_TD_CC_GET(le32_to_cpup(&td->info));
1907 if (td_dummy(*td))
1908 printk(KERN_ERR "yikes! reaping a dummy TD\n");
1910 if (cc != 0 && ohci_ed_halted(td->ed) && !td_endofchain(*td)) {
1912 * There was an error on this TD and the ED
1913 * is halted, and this was not the last TD
1914 * of the transaction, so there will be TDs
1915 * to clean off the ED.
1917 struct ohci_ed *ed = td->ed;
1918 struct ohci_td *tail_td = bus_to_virt(ed_tail_td(ed));
1919 struct ohci_td *ntd;
1921 ohci_free_td(td);
1922 td = ntd = bus_to_virt(ed_head_td(ed));
1923 while (td != tail_td) {
1924 ntd = bus_to_virt(le32_to_cpup(&td->next_td));
1926 /* only deal with TDs from this ED,
1927 * the host controller could have
1928 * processed other endpoints at the
1929 * same time as this one.. */
1930 if (td->ed == ed) {
1931 if (td_endofchain(*td))
1932 break;
1934 /* FIXME: unlink this TD from the
1935 * reverse donelist! */
1936 ohci_free_td(td);
1939 td = ntd;
1941 /* Set the ED head past the ones we cleaned
1942 off, and clear the halted flag */
1943 set_ed_head_td(ed, virt_to_bus(ntd));
1944 ohci_unhalt_ed(ed);
1945 /* If we didn't find an endofchain TD, give up */
1946 if (td == tail_td) {
1947 td = next_td;
1948 continue;
1952 /* Check if TD should be re-queued */
1953 if ((td->completed != NULL) &&
1954 (td->completed(cc, td->data, ohci_td_bytes_done(td), td->dev_id))) {
1955 /* Mark the TD as active again:
1956 * Set the not accessed condition code
1957 * Reset the Error count
1959 td->info |= cpu_to_le32(OHCI_TD_CC_NEW);
1960 clear_td_errorcount(td);
1961 /* reset the toggle field to TOGGLE_AUTO (0) */
1962 td->info &= cpu_to_le32(~OHCI_TD_DT);
1964 /* point it back to the start of the data buffer */
1965 td->cur_buf = cpu_to_le32(virt_to_bus(td->data));
1967 /* insert it back on its ED */
1968 ohci_add_td_to_ed(td, td, td->ed);
1969 ohci_unhalt_ed(td->ed);
1970 } else {
1971 /* return it to the pool of free TDs */
1972 if (can_auto_free(*td))
1973 ohci_free_td(td);
1976 td = next_td;
1979 // spin_unlock(&ohci_edtd_lock);
1980 } /* ohci_reap_donelist() */
1984 * Get annoyed at the controller for bothering us.
1985 * This pretty much follows the OHCI v1.0a spec, section 5.3.
1987 static void ohci_interrupt(int irq, void *__ohci, struct pt_regs *r)
1989 struct ohci *ohci = __ohci;
1990 struct ohci_regs *regs = ohci->regs;
1991 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
1992 struct ohci_hcca *hcca = root_hub->hcca;
1993 __u32 status, context;
1995 /* Save the status of the interrupts that are enabled */
1996 status = readl(&regs->intrstatus);
1997 status &= readl(&regs->intrenable);
1999 /* make context = the interrupt status bits that we care about */
2000 if (hcca->donehead != 0) {
2001 context = OHCI_INTR_WDH; /* hcca donehead needs processing */
2002 if (hcca->donehead & cpu_to_le32(1)) {
2003 context |= status; /* other status change to check */
2005 } else {
2006 context = status;
2007 if (!context) {
2008 /* TODO increment a useless interrupt counter here */
2009 return;
2013 /* Disable HC interrupts */ /* why? - paulus */
2014 writel(OHCI_INTR_MIE, &regs->intrdisable);
2016 #if 0
2017 /* Only do this for SERIOUS debugging, be sure kern.debug logs
2018 * are not going to the console as this can cause your
2019 * machine to lock up if so... -greg */
2020 show_ohci_status(ohci);
2021 #endif
2023 /* Process the done list */
2024 if (context & OHCI_INTR_WDH) {
2025 /* See which TD's completed.. */
2026 ohci_reap_donelist(ohci);
2028 /* reset the done queue and tell the controller */
2029 hcca->donehead = 0; /* XXX already done in ohci_reverse_donelist */
2030 writel(OHCI_INTR_WDH, &regs->intrstatus);
2032 context &= ~OHCI_INTR_WDH; /* mark this as checked */
2035 #ifdef OHCI_RHSC_INT
2036 /* NOTE: this is very funky on some USB controllers (ie: it
2037 * doesn't work right). Using the ohci_timer instead to poll
2038 * the root hub is a much better choice. */
2039 /* Process any root hub status changes */
2040 if (context & OHCI_INTR_RHSC) {
2041 /* Wake the thread to process root hub events */
2042 if (waitqueue_active(&ohci_configure))
2043 wake_up(&ohci_configure);
2045 writel(OHCI_INTR_RHSC, &regs->intrstatus);
2047 * Don't unset RHSC in context; it should be disabled.
2048 * The control thread will re-enable it after it has
2049 * checked the root hub status.
2052 #endif
2054 /* Start of Frame interrupts, used during safe ED removal */
2055 if (context & (OHCI_INTR_SF)) {
2056 writel(OHCI_INTR_SF, &regs->intrstatus);
2057 if (waitqueue_active(&start_of_frame_wakeup))
2058 wake_up(&start_of_frame_wakeup);
2059 /* Do NOT mark the frame start interrupt as checked
2060 * as we don't want to receive any more of them until
2061 * asked. */
2064 /* Check those "other" pesky bits */
2065 if (context & (OHCI_INTR_FNO)) {
2066 writel(OHCI_INTR_FNO, &regs->intrstatus);
2067 context &= ~OHCI_INTR_FNO; /* mark this as checked */
2069 if (context & OHCI_INTR_SO) {
2070 writel(OHCI_INTR_SO, &regs->intrstatus);
2071 context &= ~OHCI_INTR_SO; /* mark this as checked */
2073 if (context & OHCI_INTR_RD) {
2074 writel(OHCI_INTR_RD, &regs->intrstatus);
2075 context &= ~OHCI_INTR_RD; /* mark this as checked */
2077 if (context & OHCI_INTR_UE) {
2078 /* FIXME: need to have the control thread reset the
2079 * controller now and keep a count of unrecoverable
2080 * errors. If there are too many, it should just shut
2081 * the broken controller down entirely. */
2082 writel(OHCI_INTR_UE, &regs->intrstatus);
2083 context &= ~OHCI_INTR_UE; /* mark this as checked */
2085 if (context & OHCI_INTR_OC) {
2086 writel(OHCI_INTR_OC, &regs->intrstatus);
2087 context &= ~OHCI_INTR_OC; /* mark this as checked */
2090 /* Mask out any remaining unprocessed or unmasked interrupts
2091 * so that we don't get any more of them. */
2092 if (context & ~OHCI_INTR_MIE) {
2093 writel(context, &regs->intrdisable);
2096 /* Re-enable HC interrupts */
2097 writel(OHCI_INTR_MIE, &regs->intrenable);
2099 } /* ohci_interrupt() */
2103 * Allocate the resources required for running an OHCI controller.
2104 * Host controller interrupts must not be running while calling this
2105 * function or the penguins will get angry.
2107 * The mem_base parameter must be the usable -virtual- address of the
2108 * host controller's memory mapped I/O registers.
2110 static struct ohci *alloc_ohci(void* mem_base)
2112 int i;
2113 struct ohci *ohci;
2114 struct usb_bus *bus;
2115 struct ohci_device *dev;
2116 struct usb_device *usb;
2118 #if 0
2119 printk(KERN_DEBUG "entering alloc_ohci %p\n", mem_base);
2120 #endif
2122 ohci = kmalloc(sizeof(*ohci), GFP_KERNEL);
2123 if (!ohci)
2124 return NULL;
2126 memset(ohci, 0, sizeof(*ohci));
2128 ohci->irq = -1;
2129 ohci->regs = mem_base;
2130 INIT_LIST_HEAD(&ohci->interrupt_list);
2132 bus = kmalloc(sizeof(*bus), GFP_KERNEL);
2133 if (!bus)
2134 return NULL;
2136 memset(bus, 0, sizeof(*bus));
2138 ohci->bus = bus;
2139 bus->hcpriv = ohci;
2140 bus->op = &ohci_device_operations;
2143 * Allocate the USB device structure and root hub.
2145 * Here we allocate our own root hub and TDs as well as the
2146 * OHCI host controller communications area. The HCCA is just
2147 * a nice pool of memory with pointers to endpoint descriptors
2148 * for the different interrupts.
2150 usb = ohci_usb_allocate(NULL);
2151 if (!usb)
2152 return NULL;
2154 dev = usb_to_ohci(usb);
2155 ohci->bus->root_hub= ohci_to_usb(dev);
2156 usb->bus = bus;
2158 /* Initialize the root hub */
2159 dev->ohci = ohci; /* link back to the controller */
2162 * Allocate the Host Controller Communications Area on a 256
2163 * byte boundary. XXX take the easy way out and just grab a
2164 * page as that's guaranteed to have a nice boundary.
2166 dev->hcca = (struct ohci_hcca *) __get_free_page(GFP_KERNEL);
2167 memset(dev->hcca, 0, sizeof(struct ohci_hcca));
2169 /* Tell the controller where the HCCA is */
2170 writel(virt_to_bus(dev->hcca), &ohci->regs->hcca);
2172 #if 0
2173 printk(KERN_DEBUG "usb-ohci: HCCA allocated at %p (bus %p)\n", dev->hcca, (void*)virt_to_bus(dev->hcca));
2174 #endif
2176 /* Get the number of ports on the root hub */
2177 usb->maxchild = readl(&ohci->regs->roothub.a) & 0xff;
2178 if (usb->maxchild > MAX_ROOT_PORTS) {
2179 printk(KERN_INFO "usb-ohci: Limited to %d ports\n", MAX_ROOT_PORTS);
2180 usb->maxchild = MAX_ROOT_PORTS;
2182 if (usb->maxchild < 1) {
2183 printk(KERN_ERR "usb-ohci: Less than one root hub port? Impossible!\n");
2184 usb->maxchild = 1;
2186 printk(KERN_DEBUG "usb-ohci: %d root hub ports found\n", usb->maxchild);
2189 * Initialize the ED polling "tree" (for simplicity's sake in
2190 * this driver many nodes in the tree will be identical)
2192 dev->ed[ED_INT_32].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_16]));
2193 dev->ed[ED_INT_16].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_8]));
2194 dev->ed[ED_INT_8].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_4]));
2195 dev->ed[ED_INT_4].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_2]));
2196 dev->ed[ED_INT_2].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_1]));
2199 * Initialize the polling table to call interrupts at the
2200 * intended intervals. Note that these EDs are just
2201 * placeholders. They have their SKIP bit set and are used as
2202 * list heads to insert real EDs onto.
2204 dev->hcca->int_table[0] = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_1]));
2205 for (i = 1; i < NUM_INTS; i++) {
2206 if (i & 16)
2207 dev->hcca->int_table[i] =
2208 cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_32]));
2209 if (i & 8)
2210 dev->hcca->int_table[i] =
2211 cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_16]));
2212 if (i & 4)
2213 dev->hcca->int_table[i] =
2214 cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_8]));
2215 if (i & 2)
2216 dev->hcca->int_table[i] =
2217 cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_4]));
2218 if (i & 1)
2219 dev->hcca->int_table[i] =
2220 cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_2]));
2224 * Tell the controller where the control and bulk lists are.
2225 * The lists start out empty.
2227 writel(0, &ohci->regs->ed_controlhead);
2228 writel(0, &ohci->regs->ed_bulkhead);
2230 #ifdef OHCI_DEBUG
2231 printk(KERN_DEBUG "alloc_ohci(): controller\n");
2232 show_ohci_status(ohci);
2233 #endif
2235 #if 0
2236 printk(KERN_DEBUG "leaving alloc_ohci %p\n", ohci);
2237 #endif
2239 return ohci;
2240 } /* alloc_ohci() */
2244 * De-allocate all resoueces..
2246 static void release_ohci(struct ohci *ohci)
2248 printk(KERN_INFO "Releasing OHCI controller 0x%p\n", ohci);
2250 #ifdef OHCI_TIMER
2251 /* stop our timer */
2252 del_timer(&ohci_timer);
2253 #endif
2254 if (ohci->irq >= 0) {
2255 free_irq(ohci->irq, ohci);
2256 ohci->irq = -1;
2259 /* stop all OHCI interrupts */
2260 writel(~0x0, &ohci->regs->intrdisable);
2262 if (ohci->bus->root_hub) {
2263 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
2264 /* ensure that HC is stopped before releasing the HCCA */
2265 writel(OHCI_USB_SUSPEND, &ohci->regs->control);
2266 free_page((unsigned long) root_hub->hcca);
2267 kfree(ohci->bus->root_hub);
2268 root_hub->hcca = NULL;
2269 ohci->bus->root_hub = NULL;
2272 /* unmap the IO address space */
2273 iounmap(ohci->regs);
2275 kfree(ohci);
2277 MOD_DEC_USE_COUNT;
2279 /* If the ohci itself were dynamic we'd free it here */
2281 printk(KERN_DEBUG "usb-ohci: HC resources released.\n");
2282 } /* release_ohci() */
2286 * USB OHCI control thread
2288 static int ohci_control_thread(void * __ohci)
2290 struct ohci *ohci = (struct ohci *)__ohci;
2293 * I'm unfamiliar with the SMP kernel locking.. where should
2294 * this be released and what does it do? -greg
2296 lock_kernel();
2299 * This thread doesn't need any user-level access,
2300 * so get rid of all of our resources..
2302 printk(KERN_DEBUG "ohci-control thread code for 0x%p code at 0x%p\n", __ohci, &ohci_control_thread);
2303 exit_mm(current);
2304 exit_files(current);
2305 exit_fs(current);
2307 strcpy(current->comm, "ohci-control");
2309 usb_register_bus(ohci->bus);
2312 * Damn the torpedoes, full speed ahead
2314 if (start_hc(ohci) < 0) {
2315 printk(KERN_ERR "usb-ohci: failed to start the controller\n");
2316 release_ohci(ohci);
2317 usb_deregister_bus(ohci->bus);
2318 printk(KERN_INFO "leaving ohci_control_thread %p\n", __ohci);
2319 return 0;
2322 for(;;) {
2323 siginfo_t info;
2324 int unsigned long signr;
2326 wait_ms(200);
2328 /* check the root hub configuration for changes. */
2329 ohci_check_configuration(ohci);
2331 /* re-enable root hub status change interrupts. */
2332 #ifdef OHCI_RHSC_INT
2333 writel(OHCI_INTR_RHSC, &ohci->regs->intrenable);
2334 #endif
2336 printk(KERN_DEBUG "ohci-control thread sleeping\n");
2337 interruptible_sleep_on(&ohci_configure);
2338 #ifdef CONFIG_APM
2339 if (apm_resume) {
2340 apm_resume = 0;
2341 if (start_hc(ohci) < 0)
2342 break;
2343 continue;
2345 #endif
2348 * If we were woken up by a signal, see if its useful,
2349 * otherwise exit.
2351 if (signal_pending(current)) {
2352 /* sending SIGUSR1 makes us print out some info */
2353 spin_lock_irq(&current->sigmask_lock);
2354 signr = dequeue_signal(&current->blocked, &info);
2355 spin_unlock_irq(&current->sigmask_lock);
2357 if(signr == SIGUSR1) {
2358 /* TODO: have it do a full ed/td queue dump? */
2359 printk(KERN_DEBUG "OHCI status dump:\n");
2360 show_ohci_status(ohci);
2361 } else if (signr == SIGUSR2) {
2362 /* toggle mega TD/ED debugging output */
2363 #ifdef OHCI_DEBUG
2364 MegaDebug = !MegaDebug;
2365 printk(KERN_DEBUG "usb-ohci: Mega debugging %sabled.\n",
2366 MegaDebug ? "en" : "dis");
2367 #endif
2368 } else {
2369 /* unknown signal, exit the thread */
2370 break;
2373 } /* for (;;) */
2375 reset_hc(ohci);
2376 release_ohci(ohci);
2377 usb_deregister_bus(ohci->bus);
2378 printk(KERN_DEBUG "ohci-control thread for 0x%p exiting\n", __ohci);
2380 return 0;
2381 } /* ohci_control_thread() */
2384 #ifdef CONFIG_APM
2385 static int handle_apm_event(apm_event_t event)
2387 static int down = 0;
2389 switch (event) {
2390 case APM_SYS_SUSPEND:
2391 case APM_USER_SUSPEND:
2392 if (down) {
2393 printk(KERN_DEBUG "usb-ohci: received extra suspend event\n");
2394 break;
2396 down = 1;
2397 break;
2398 case APM_NORMAL_RESUME:
2399 case APM_CRITICAL_RESUME:
2400 if (!down) {
2401 printk(KERN_DEBUG "usb-ohci: received bogus resume event\n");
2402 break;
2404 down = 0;
2405 if (waitqueue_active(&ohci_configure)) {
2406 apm_resume = 1;
2407 wake_up(&ohci_configure);
2409 break;
2411 return 0;
2412 } /* handle_apm_event() */
2413 #endif
2416 #ifdef OHCI_TIMER
2418 * Inspired by Iñaky's driver. This function is a timer routine that
2419 * is called every OHCI_TIMER_FREQ ms. It polls the root hub for
2420 * status changes as on my system the RHSC interrupt just doesn't
2421 * play well with others.. (so RHSC is turned off by default in this
2422 * driver)
2423 * [my controller is a "SiS 7001 USB (rev 16)"]
2424 * -greg
2426 static void ohci_timer_func (unsigned long ohci_ptr)
2428 struct ohci *ohci = (struct ohci*)ohci_ptr;
2430 ohci_root_hub_events(ohci);
2432 /* set the next timer */
2433 mod_timer(&ohci_timer, jiffies + ((OHCI_TIMER_FREQ*HZ)/1000));
2435 } /* ohci_timer_func() */
2436 #endif
2440 * Increment the module usage count, start the control thread and
2441 * return success if the controller is good.
2443 static int found_ohci(int irq, void* mem_base)
2445 int retval;
2446 struct ohci *ohci;
2448 #if 0
2449 printk(KERN_DEBUG "entering found_ohci %d %p\n", irq, mem_base);
2450 #endif
2452 /* Allocate the running OHCI structures */
2453 ohci = alloc_ohci(mem_base);
2454 if (!ohci) {
2455 return -ENOMEM;
2458 #ifdef OHCI_TIMER
2459 init_timer(&ohci_timer);
2460 ohci_timer.expires = jiffies + ((OHCI_TIMER_FREQ*HZ)/1000);
2461 ohci_timer.data = (unsigned long)ohci;
2462 ohci_timer.function = ohci_timer_func;
2463 add_timer(&ohci_timer);
2464 #endif
2466 retval = -EBUSY;
2467 if (request_irq(irq, ohci_interrupt, SA_SHIRQ, "usb-ohci", ohci) == 0) {
2468 int pid;
2470 ohci->irq = irq;
2472 #ifdef OHCI_DEBUG
2473 printk(KERN_DEBUG "usb-ohci: forking ohci-control thread for 0x%p\n", ohci);
2474 #endif
2476 /* fork off the handler */
2477 pid = kernel_thread(ohci_control_thread, ohci,
2478 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
2479 if (pid >= 0) {
2480 return 0;
2483 retval = pid;
2484 } else {
2485 printk(KERN_ERR "usb-ohci: Couldn't allocate interrupt %d\n", irq);
2487 release_ohci(ohci);
2489 #ifdef OHCI_DEBUG
2490 printk(KERN_DEBUG "leaving found_ohci %d %p\n", irq, mem_base);
2491 #endif
2493 return retval;
2494 } /* found_ohci() */
2498 * If this controller is for real, map the IO memory and proceed
2500 static int init_ohci(struct pci_dev *dev)
2502 unsigned long mem_base = dev->base_address[0];
2504 /* If its OHCI, its memory */
2505 if (mem_base & PCI_BASE_ADDRESS_SPACE_IO)
2506 return -ENODEV;
2508 /* Get the memory address and map it for IO */
2509 mem_base &= PCI_BASE_ADDRESS_MEM_MASK;
2511 /* no interrupt won't work... */
2512 if (dev->irq == 0) {
2513 printk(KERN_ERR "usb-ohci: no irq assigned? check your BIOS settings.\n");
2514 return -ENODEV;
2518 * FIXME ioremap_nocache isn't implemented on all CPUs (such
2519 * as the Alpha) [?] What should I use instead...
2521 * The iounmap() is done on in release_ohci.
2523 mem_base = (unsigned long) ioremap_nocache(mem_base, 4096);
2525 if (!mem_base) {
2526 printk(KERN_ERR "Error mapping OHCI memory\n");
2527 return -EFAULT;
2529 MOD_INC_USE_COUNT;
2531 #ifdef OHCI_DEBUG
2532 printk(KERN_INFO "usb-ohci: Warning! Gobs of debugging output has been enabled.\n");
2533 printk(KERN_INFO " Check your kern.debug logs for the bulk of it.\n");
2534 #endif
2536 if (found_ohci(dev->irq, (void *) mem_base) < 0) {
2537 MOD_DEC_USE_COUNT;
2538 return -1;
2541 return 0;
2542 } /* init_ohci() */
2544 /* TODO this should be named following Linux convention and go in pci.h */
2545 #define PCI_CLASS_SERIAL_USB_OHCI ((PCI_CLASS_SERIAL_USB << 8) | 0x0010)
2548 * Search the PCI bus for an OHCI USB controller and set it up
2550 * If anyone wants multiple controllers this will need to be
2551 * updated.. Right now, it just picks the first one it finds.
2553 int ohci_init(void)
2555 int retval;
2556 struct pci_dev *dev = NULL;
2557 /*u8 type;*/
2559 if (sizeof(struct ohci_device) > 4096) {
2560 printk(KERN_ERR "usb-ohci: struct ohci_device to large\n");
2561 return -ENODEV;
2564 printk(KERN_INFO "OHCI USB Driver loading\n");
2566 retval = -ENODEV;
2567 for (;;) {
2568 /* Find an OHCI USB controller */
2569 dev = pci_find_class(PCI_CLASS_SERIAL_USB_OHCI, dev);
2570 if (!dev)
2571 break;
2573 /* Verify that its OpenHCI by checking for MMIO */
2574 /* pci_read_config_byte(dev, PCI_CLASS_PROG, &type);
2575 if (!type)
2576 continue; */
2578 /* Ok, set it up */
2579 retval = init_ohci(dev);
2580 if (retval < 0)
2581 continue;
2583 #ifdef CONFIG_APM
2584 apm_register_callback(&handle_apm_event);
2585 #endif
2587 return 0; /* no error */
2589 return retval;
2590 } /* ohci_init */
2593 #ifdef MODULE
2595 * Clean up when unloading the module
2597 void cleanup_module(void){
2598 # ifdef CONFIG_APM
2599 apm_unregister_callback(&handle_apm_event);
2600 # endif
2601 printk(KERN_ERR "usb-ohci: module unloaded\n");
2604 int init_module(void){
2605 return ohci_init();
2607 #endif //MODULE
2609 /* vim:sw=8