Import 2.3.9pre5
[davej-history.git] / drivers / usb / ohci.c
blob261306949d997c4d6dd90d45e455cd0e7f9015d9
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 inline 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 inline 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 inline 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 inline 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);
441 * Remove all the EDs which have a given device address from a list.
442 * Used when the device is unplugged.
443 * Returns 1 if anything was changed.
445 static int ohci_remove_device_list(__u32 *headp, int devnum)
447 struct ohci_ed *ed;
448 __u32 *prevp = headp;
449 int removed = 0;
451 while (*prevp != 0) {
452 ed = bus_to_virt(le32_to_cpup(prevp));
453 if ((le32_to_cpup(&ed->status) & OHCI_ED_FA) == devnum) {
454 /* set the controller to skip this one
455 and remove it from the list */
456 ed->status |= cpu_to_le32(OHCI_ED_SKIP);
457 *prevp = ed->next_ed;
458 removed = 1;
459 } else {
460 prevp = &ed->next_ed;
463 wmb();
465 return removed;
469 * Remove all the EDs for a given device from all lists.
471 void ohci_remove_device(struct ohci *ohci, int devnum)
473 unsigned long flags;
474 __u32 head;
475 struct ohci_regs *regs = ohci->regs;
476 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
478 spin_lock_irqsave(&ohci_edtd_lock, flags);
480 /* Control list */
481 head = cpu_to_le32(readl(&regs->ed_controlhead));
482 if (ohci_remove_device_list(&head, devnum))
483 writel(le32_to_cpup(&head), &regs->ed_controlhead);
485 /* Bulk list */
486 head = cpu_to_le32(readl(&regs->ed_bulkhead));
487 if (ohci_remove_device_list(&head, devnum))
488 writel(le32_to_cpup(&head), &regs->ed_bulkhead);
490 /* Interrupt/iso list */
491 head = cpu_to_le32(virt_to_bus(&root_hub->ed[ED_INT_32]));
492 ohci_remove_device_list(&head, devnum);
495 * Wait until the start of the next frame to ensure
496 * that the HC has seen any changes.
498 ohci_wait_sof(ohci->regs);
500 spin_unlock_irqrestore(&ohci_edtd_lock, flags);
504 * Remove a TD from the given EDs TD list. The TD is freed as well.
506 static void ohci_remove_td_from_ed(struct ohci_td *td, struct ohci_ed *ed)
508 unsigned long flags;
509 struct ohci_td *head_td;
511 if ((td == NULL) || (ed == NULL))
512 return;
514 if (ed_head_td(ed) == 0)
515 return;
517 spin_lock_irqsave(&ohci_edtd_lock, flags);
519 /* set the "skip me bit" in this ED */
520 ed->status |= cpu_to_le32(OHCI_ED_SKIP);
522 /* XXX Assuming this list will never be circular */
524 head_td = bus_to_virt(ed_head_td(ed));
525 if (virt_to_bus(td) == ed_head_td(ed)) {
526 /* It's the first TD, remove it. */
527 set_ed_head_td(ed, head_td->next_td);
528 } else {
529 struct ohci_td *prev_td, *cur_td;
531 /* FIXME: collapse this into a nice simple loop :) */
532 if (head_td->next_td != 0) {
533 prev_td = head_td;
534 cur_td = bus_to_virt(le32_to_cpup(&head_td->next_td));
535 for (;;) {
536 if (td == cur_td) {
537 /* remove it */
538 prev_td->next_td = cur_td->next_td;
539 break;
541 if (cur_td->next_td == 0)
542 break;
543 prev_td = cur_td;
544 cur_td = bus_to_virt(le32_to_cpup(&cur_td->next_td));
549 td->next_td = 0; /* remove the TDs links */
550 td->ed = NULL;
552 /* return this TD to the pool of free TDs */
553 ohci_free_td(td);
555 /* unset the "skip me bit" in this ED */
556 ed->status &= cpu_to_le32(~OHCI_ED_SKIP);
558 spin_unlock_irqrestore(&ohci_edtd_lock, flags);
559 } /* ohci_remove_td_from_ed() */
563 * Get a pointer (virtual) to an available TD from the given device's
564 * pool. Return NULL if none are left.
566 static struct ohci_td *ohci_get_free_td(struct ohci_device *dev)
568 int idx;
570 #if 0
571 printk(KERN_DEBUG "in ohci_get_free_td()\n");
572 #endif
574 /* FIXME: this is horribly inefficient */
575 for (idx=0; idx < NUM_TDS; idx++) {
576 #if 0
577 show_ohci_td(&dev->td[idx]);
578 #endif
579 if (!td_allocated(dev->td[idx])) {
580 struct ohci_td *new_td = &dev->td[idx];
581 /* zero out the TD */
582 memset(new_td, 0, sizeof(*new_td));
583 /* mark the new TDs as unaccessed */
584 new_td->info = cpu_to_le32(OHCI_TD_CC_NEW);
585 /* mark it as allocated */
586 allocate_td(new_td);
587 return new_td;
591 printk(KERN_ERR "usb-ohci: unable to allocate a TD\n");
592 return NULL;
593 } /* ohci_get_free_td() */
597 * Get a pointer (virtual) to an available TD from the given device's
598 * pool. Return NULL if none are left.
600 * NOTE: This function does not allocate and attach the dummy_td.
601 * That is done in ohci_fill_ed(). FIXME: it should probably be moved
602 * into here.
604 static struct ohci_ed *ohci_get_free_ed(struct ohci_device *dev)
606 int idx;
608 /* FIXME: this is horribly inefficient */
609 for (idx=0; idx < NUM_EDS; idx++) {
610 if (!ed_allocated(dev->ed[idx])) {
611 struct ohci_ed *new_ed = &dev->ed[idx];
612 /* zero out the ED */
613 memset(new_ed, 0, sizeof(*new_ed));
614 /* all new EDs start with the SKIP bit set */
615 new_ed->status |= cpu_to_le32(OHCI_ED_SKIP);
616 /* mark it as allocated */
617 allocate_ed(new_ed);
618 return new_ed;
622 printk(KERN_ERR "usb-ohci: unable to allocate an ED\n");
623 return NULL;
624 } /* ohci_get_free_ed() */
628 * Free an OHCI ED and all of the TDs on its list. It is assumed that
629 * this ED is not active. You should call ohci_wait_for_ed_safe()
630 * beforehand if you can't guarantee that.
632 void ohci_free_ed(struct ohci_ed *ed)
634 if (!ed)
635 return;
637 if (ed_head_td(ed) != 0) {
638 struct ohci_td *td, *tail_td, *next_td;
640 td = bus_to_virt(ed_head_td(ed));
641 tail_td = bus_to_virt(ed_tail_td(ed));
642 for (;;) {
643 next_td = bus_to_virt(le32_to_cpup(&td->next_td));
644 ohci_free_td(td);
645 if (td == tail_td)
646 break;
647 td = next_td;
651 ed->status &= cpu_to_le32(~(__u32)ED_ALLOCATED);
652 } /* ohci_free_ed() */
656 * Initialize a TD
658 * dir = OHCI_TD_D_IN, OHCI_TD_D_OUT, or OHCI_TD_D_SETUP
659 * toggle = TOGGLE_AUTO, TOGGLE_DATA0, TOGGLE_DATA1
661 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)
663 /* hardware fields */
664 td->info = cpu_to_le32(OHCI_TD_CC_NEW |
665 (dir & OHCI_TD_D) |
666 (toggle & OHCI_TD_DT) |
667 flags);
668 td->cur_buf = (data == NULL) ? 0 : cpu_to_le32(virt_to_bus(data));
669 td->buf_end = (len == 0) ? 0 :
670 cpu_to_le32(le32_to_cpup(&td->cur_buf) + len - 1);
672 /* driver fields */
673 td->data = data;
674 td->dev_id = dev_id;
675 td->completed = completed;
677 #if 0
678 printk(KERN_DEBUG "ohci_fill_new_td created:\n");
679 show_ohci_td(td);
680 #endif
682 return td;
683 } /* ohci_fill_new_td() */
687 * Initialize a new ED on device dev, including allocating and putting the
688 * dummy tail_td on its queue if it doesn't already have one. Any
689 * TDs on this ED other than the dummy will be lost (so there better
690 * not be any!). This assumes that the ED is Allocated and will
691 * force the Allocated bit on.
693 struct ohci_ed *ohci_fill_ed(struct ohci_device *dev, struct ohci_ed *ed,
694 int maxpacketsize, int lowspeed, int endp_id,
695 int isoc_tds)
697 struct ohci_td *dummy_td;
699 if (ed_head_td(ed) != ed_tail_td(ed))
700 printk(KERN_ERR "Reusing a non-empty ED %p!\n", ed);
702 if (!ed->tail_td) {
703 dummy_td = ohci_get_free_td(dev);
704 if (dummy_td == NULL) {
705 printk(KERN_ERR "Error allocating dummy TD for ED %p\n", ed);
706 return NULL; /* no dummy available! */
708 make_dumb_td(dummy_td); /* flag it as a dummy */
709 ed->tail_td = cpu_to_le32(virt_to_bus(dummy_td));
710 } else {
711 dummy_td = bus_to_virt(ed_tail_td(ed));
712 if (!td_dummy(*dummy_td))
713 printk(KERN_ERR "ED %p's dummy %p is screwy\n", ed, dummy_td);
716 /* set the head TD to the dummy and clear the Carry & Halted bits */
717 ed->_head_td = ed->tail_td;
719 ed->status = cpu_to_le32(
720 ed_set_maxpacket(maxpacketsize) |
721 ed_set_speed(lowspeed) |
722 (endp_id & 0x7ff) |
723 ((isoc_tds == 0) ? OHCI_ED_F_NORM : OHCI_ED_F_ISOC));
724 allocate_ed(ed);
725 ed->next_ed = 0;
727 return ed;
728 } /* ohci_fill_ed() */
732 * Create a chain of Normal TDs to be used for a large data transfer
733 * (bulk or control).
735 * Returns the head TD in the chain.
737 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)
739 struct ohci_td *head, *cur_td;
740 __u32 bus_data_start, bus_data_end;
741 unsigned short max_page0_len;
743 if (!data || (len == 0))
744 return NULL;
746 /* Setup the first TD, leaving buf_end = 0 */
747 head = ohci_get_free_td(dev);
748 if (head == NULL) {
749 printk(KERN_ERR "usb-ohci: out of TDs\n");
750 return NULL;
753 ohci_fill_new_td(head,
754 td_set_dir_out(dir),
755 toggle & OHCI_TD_DT,
756 (round ? OHCI_TD_ROUND : 0),
757 data, 0,
758 dev_id, handler);
759 if (!auto_free)
760 noauto_free_td(head);
762 cur_td = head;
764 /* AFICT, that the OHCI controller takes care of the innards of
765 * bulk & control data transfers by sending zero length
766 * packets as necessary if the transfer falls on an even packet
767 * size boundary, we don't need a special TD for that. */
769 while (len > 0) {
770 bus_data_start = virt_to_bus(data);
771 bus_data_end = virt_to_bus(data+(len-1));
773 /* check the 4096 byte alignment of the start of the data */
774 max_page0_len = 0x1000 - (bus_data_start & 0xfff);
776 /* check if the remaining data occupies more than two pages */
777 if ((max_page0_len < len) && (len - max_page0_len > 0x1000)) {
778 struct ohci_td *new_td;
780 /* Point this TD to data up through the end of
781 * the second page */
782 cur_td->buf_end = bus_data_start +
783 (max_page0_len + 0xfff);
785 /* adjust the data pointer & remaining length */
786 data += (max_page0_len + 0x1000);
787 len -= (max_page0_len + 0x1000);
789 /* TODO lookup effect of rounding bit on
790 * individual TDs vs. whole TD chain transfers;
791 * disable cur_td's rounding bit here if needed. */
793 /* mark that this is not the last TD... */
794 clear_td_endofchain(cur_td);
796 /* allocate another td */
797 new_td = ohci_get_free_td(dev);
798 if (new_td == NULL) {
799 printk(KERN_ERR "usb-ohci: out of TDs\n");
800 /* FIXME: free any allocated TDs */
801 return NULL;
804 ohci_fill_new_td(new_td,
805 td_set_dir_out(dir),
806 TOGGLE_AUTO, /* toggle Data0/1 via the ED */
807 round ? OHCI_TD_ROUND : 0,
808 data, 0,
809 dev_id, handler);
810 if (!auto_free)
811 noauto_free_td(new_td);
813 /* Link the new TD to the chain & advance */
814 cur_td->next_td = virt_to_bus(new_td);
815 cur_td = new_td;
816 } else {
817 /* Last TD in this chain, normal buf_end is fine */
818 cur_td->buf_end = bus_data_end;
820 set_td_endofchain(cur_td);
822 len = 0;
823 break;
825 } /* while */
827 /* link the given next_td to the end of this chain */
828 cur_td->next_td = next_td;
830 return head;
831 } /* ohci_build_td_chain() */
835 * Compute the number of bytes that have been transferred on a given
836 * TD. Do not call this on TDs that are active on the host
837 * controller.
839 static __u16 ohci_td_bytes_done(struct ohci_td *td)
841 __u16 result;
842 __u32 bus_data_start, bus_data_end;
844 bus_data_start = virt_to_bus(td->data);
845 if (!td->data || !bus_data_start)
846 return 0;
848 /* if cur_buf is 0, all data has been transferred */
849 bus_data_end = td->cur_buf ? td->cur_buf : td->buf_end;
851 /* is it on the same page? */
852 if ((bus_data_start & ~0xfff) == (bus_data_end & ~0xfff)) {
853 result = bus_data_end - bus_data_start + 1;
854 } else {
855 /* compute the amount transferred on the first page */
856 result = 0x1000 - (bus_data_start & 0xfff);
857 /* add the amount done in the second page */
858 result += (bus_data_end & 0xfff) + 1;
861 return result;
862 } /* ohci_td_bytes_done() */
865 /**********************************
866 * OHCI interrupt list operations *
867 **********************************/
870 * Request an interrupt handler for one "pipe" of a USB device.
871 * (this function is pretty minimal right now)
873 * At the moment this is only good for input interrupts. (ie: for a
874 * mouse or keyboard)
876 * Period is desired polling interval in ms. The closest, shorter
877 * match will be used. Powers of two from 1-32 are supported by OHCI.
879 static int ohci_request_irq(struct usb_device *usb, unsigned int pipe,
880 usb_device_irq handler, int period, void *dev_id)
882 struct ohci_device *dev = usb_to_ohci(usb);
883 struct ohci_td *td;
884 struct ohci_ed *interrupt_ed; /* endpoint descriptor for this irq */
885 int maxps = usb_maxpacket(usb, pipe);
887 /* Get an ED and TD */
888 interrupt_ed = ohci_get_free_ed(dev);
889 if (!interrupt_ed) {
890 printk(KERN_ERR "Out of EDs on device %p in ohci_request_irq\n", dev);
891 return -1;
894 td = ohci_get_free_td(dev);
895 if (!td) {
896 printk(KERN_ERR "Out of TDs in ohci_request_irq\n");
897 ohci_free_ed(interrupt_ed);
898 return -1;
902 * Set the max packet size, device speed, endpoint number, usb
903 * device number (function address), and type of TD.
905 ohci_fill_ed(dev, interrupt_ed, maxps, usb_pipeslow(pipe),
906 usb_pipe_endpdev(pipe), 0 /* normal TDs */);
908 /* Fill in the TD */
909 if (maxps > sizeof(dev->data))
910 maxps = sizeof(dev->data);
911 ohci_fill_new_td(td, td_set_dir_out(usb_pipeout(pipe)),
912 TOGGLE_AUTO,
913 OHCI_TD_ROUND,
914 dev->data, maxps,
915 dev_id, handler);
917 * TODO: be aware of how the OHCI controller deals with DMA
918 * spanning more than one page.
922 * Put the TD onto our ED and make sure its ready to run
924 td = ohci_add_td_to_ed(td, td, interrupt_ed);
925 interrupt_ed->status &= cpu_to_le32(~OHCI_ED_SKIP);
926 ohci_unhalt_ed(interrupt_ed);
928 /* Make sure all the stores above get done before
929 * the store which tells the OHCI about the new ed. */
930 wmb();
932 /* Assimilate the new ED into the collective */
933 ohci_add_periodic_ed(dev->ohci, interrupt_ed, period);
935 /* FIXME: return a request handle that can be used by the
936 * caller to cancel this request. Be sure its guaranteed not
937 * to be re-used until the caller is guaranteed to know that
938 * the transfer has ended or been cancelled */
939 return 0;
940 } /* ohci_request_irq() */
944 * Control thread operations:
946 static DECLARE_WAIT_QUEUE_HEAD(control_wakeup);
949 * This is the handler that gets called when a control transaction
950 * completes.
952 * This function is called from the interrupt handler.
954 static int ohci_control_completed(int stats, void *buffer, int len, void *dev_id)
956 /* pass the TDs completion status back to control_msg */
957 if (dev_id) {
958 int *completion_status = (int *)dev_id;
959 *completion_status = stats;
962 wake_up(&control_wakeup);
963 return 0;
964 } /* ohci_control_completed() */
968 * Send or receive a control message on a "pipe"
970 * The cmd parameter is a pointer to the 8 byte setup command to be
971 * sent.
973 * A control message contains:
974 * - The command itself
975 * - An optional data phase (if len > 0)
976 * - Status complete phase
978 * This function can NOT be called from an interrupt.
980 static int ohci_control_msg(struct usb_device *usb, unsigned int pipe,
981 devrequest *cmd, void *data, int len)
983 struct ohci_device *dev = usb_to_ohci(usb);
984 struct ohci_ed *control_ed = ohci_get_free_ed(dev);
985 struct ohci_td *setup_td, *data_td, *status_td;
986 DECLARE_WAITQUEUE(wait, current);
987 int completion_status = -1;
988 devrequest our_cmd;
990 /* byte-swap fields of cmd if necessary */
991 our_cmd = *cmd;
992 cpu_to_le16s(&our_cmd.value);
993 cpu_to_le16s(&our_cmd.index);
994 cpu_to_le16s(&our_cmd.length);
996 #ifdef OHCI_DEBUG
997 if (MegaDebug)
998 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);
999 #endif
1000 if (!control_ed) {
1001 printk(KERN_ERR "usb-ohci: couldn't get ED for dev %p\n", dev);
1002 return -1;
1005 /* get a TD to send this control message with */
1006 setup_td = ohci_get_free_td(dev);
1007 if (!setup_td) {
1008 printk(KERN_ERR "usb-ohci: couldn't get TD for dev %p [cntl setup]\n", dev);
1009 ohci_free_ed(control_ed);
1010 return -1;
1014 * Set the max packet size, device speed, endpoint number, usb
1015 * device number (function address), and type of TD.
1018 ohci_fill_ed(dev, control_ed, usb_maxpacket(usb,pipe), usb_pipeslow(pipe),
1019 usb_pipe_endpdev(pipe), 0 /* normal TDs */);
1022 * Build the control TD
1026 * Set the not accessed condition code, allow odd sized data,
1027 * and set the data transfer type to SETUP. Setup DATA always
1028 * uses a DATA0 packet.
1030 * The setup packet contains a devrequest (usb.h) which
1031 * will always be 8 bytes long.
1033 ohci_fill_new_td(setup_td, OHCI_TD_D_SETUP, TOGGLE_DATA0,
1034 OHCI_TD_IOC_OFF,
1035 &our_cmd, 8, /* cmd is always 8 bytes long */
1036 &completion_status, NULL);
1038 /* Allocate a TD for the control xfer status */
1039 status_td = ohci_get_free_td(dev);
1040 if (!status_td) {
1041 printk("usb-ohci: couldn't get TD for dev %p [cntl status]\n", dev);
1042 ohci_free_td(setup_td);
1043 ohci_free_ed(control_ed);
1044 return -1;
1047 /* The control status packet always uses a DATA1
1048 * Give "dev_id" the address of completion_status so that the
1049 * TDs status can be passed back to us from the IRQ. */
1050 ohci_fill_new_td(status_td,
1051 td_set_dir_in(usb_pipeout(pipe) | (len == 0)),
1052 TOGGLE_DATA1,
1053 0 /* flags */,
1054 NULL /* data */, 0 /* data len */,
1055 &completion_status, ohci_control_completed);
1056 status_td->next_td = 0; /* end of TDs */
1058 /* If there is data to transfer, create the chain of data TDs
1059 * followed by the status TD. */
1060 if (len > 0) {
1061 data_td = ohci_build_td_chain( dev, data, len,
1062 usb_pipeout(pipe), TOGGLE_DATA1,
1063 1 /* round */, 1 /* autofree */,
1064 &completion_status, NULL /* no handler here */,
1065 virt_to_bus(status_td) );
1066 if (!data_td) {
1067 printk(KERN_ERR "usb-ohci: couldn't allocate control data TDs for dev %p\n", dev);
1068 ohci_free_td(setup_td);
1069 ohci_free_td(status_td);
1070 ohci_free_ed(control_ed);
1071 return -1;
1074 /* link the to the data & status TDs */
1075 setup_td->next_td = virt_to_bus(data_td);
1076 } else {
1077 /* no data TDs, link to the status TD */
1078 setup_td->next_td = virt_to_bus(status_td);
1082 * Add the control TDs to the control ED (setup_td is the first)
1084 setup_td = ohci_add_td_chain_to_ed(setup_td, control_ed);
1085 control_ed->status &= ~OHCI_ED_SKIP;
1086 ohci_unhalt_ed(control_ed);
1088 #ifdef OHCI_DEBUG
1089 if (MegaDebug) {
1090 /* complete transaction debugging output (before) */
1091 printk(KERN_DEBUG " Control ED %lx:\n", virt_to_bus(control_ed));
1092 show_ohci_ed(control_ed);
1093 printk(KERN_DEBUG " Control TD chain:\n");
1094 show_ohci_td_chain(setup_td);
1095 printk(KERN_DEBUG " OHCI Controller Status:\n");
1096 show_ohci_status(dev->ohci);
1098 #endif
1101 * Start the control transaction..
1103 current->state = TASK_UNINTERRUPTIBLE;
1104 add_wait_queue(&control_wakeup, &wait);
1106 /* Give the ED to the HC */
1107 ohci_add_control_ed(dev->ohci, control_ed);
1109 schedule_timeout(HZ/10);
1111 remove_wait_queue(&control_wakeup, &wait);
1113 #ifdef OHCI_DEBUG
1114 if (MegaDebug) {
1115 /* complete transaction debugging output (after) */
1116 printk(KERN_DEBUG " *after* Control ED %lx:\n", virt_to_bus(control_ed));
1117 show_ohci_ed(control_ed);
1118 printk(KERN_DEBUG " *after* Control TD chain:\n");
1119 show_ohci_td_chain(setup_td);
1120 printk(KERN_DEBUG " *after* OHCI Controller Status:\n");
1121 show_ohci_status(dev->ohci);
1123 #endif
1125 /* no TD cleanup, the TDs were auto-freed as they finished */
1127 /* remove the control ED from the HC */
1128 ohci_remove_control_ed(dev->ohci, control_ed);
1129 ohci_free_ed(control_ed); /* return it to the pool */
1131 #ifdef OHCI_DEBUG
1132 if (completion_status != 0) {
1133 char *what = (completion_status < 0)? "timed out":
1134 cc_names[completion_status & 0xf];
1135 printk(KERN_ERR "ohci_control_msg: %s on pipe %x cmd %x %x %x %x %x\n",
1136 what, pipe, cmd->requesttype, cmd->request,
1137 cmd->value, cmd->index, cmd->length);
1138 } else if (!usb_pipeout(pipe)) {
1139 unsigned char *q = data;
1140 int i;
1141 printk(KERN_DEBUG "ctrl msg %x %x %x %x %x on pipe %x returned:",
1142 cmd->requesttype, cmd->request, cmd->value, cmd->index,
1143 cmd->length, pipe);
1144 for (i = 0; i < len; ++i) {
1145 if (i % 16 == 0)
1146 printk("\n" KERN_DEBUG);
1147 printk(" %x", q[i]);
1149 printk("\n");
1151 #endif
1152 return completion_status;
1153 } /* ohci_control_msg() */
1156 /**********************************************************************
1157 * Bulk transfer processing
1158 **********************************************************************/
1161 * Internal state for an ohci_bulk_request
1163 struct ohci_bulk_request_state {
1164 struct usb_device *usb_dev;
1165 unsigned int pipe; /* usb "pipe" */
1166 void *data; /* ptr to data */
1167 int length; /* length to transfer */
1168 int _bytes_done; /* bytes transferred so far */
1169 unsigned long *bytes_transferred_p; /* where to increment */
1170 void *dev_id; /* pass to the completion handler */
1171 usb_device_irq completion; /* completion handler */
1175 * this handles the individual TDs of a (possibly) larger bulk
1176 * request. It keeps track of the total bytes transferred, calls the
1177 * final completion handler, etc.
1179 static int ohci_bulk_td_handler(int stats, void *buffer, int len, void *dev_id)
1181 struct ohci_bulk_request_state *req;
1183 req = (struct ohci_bulk_request_state *) dev_id;
1185 #ifdef OHCI_DEBUG
1186 printk(KERN_DEBUG "ohci_bulk_td_handler stats %x, buffer %p, len %d, req %p\n", stats, buffer, len, req);
1187 #endif
1189 /* only count TDs that were completed successfully */
1190 if (stats == USB_ST_NOERROR)
1191 req->_bytes_done += len;
1193 #ifdef OHCI_DEBUG
1194 printk(KERN_DEBUG "ohci_bulk_td_handler %d bytes done\n", req->_bytes_done);
1195 #endif
1197 /* call the real completion handler when done or on an error */
1198 if ((stats != USB_ST_NOERROR) ||
1199 (req->_bytes_done >= req->length && req->completion != NULL)) {
1200 *req->bytes_transferred_p += req->_bytes_done;
1201 #ifdef OHCI_DEBUG
1202 printk(KERN_DEBUG "usb-ohci: bulk request %p ending after %d bytes\n", req, req->_bytes_done);
1203 #endif
1204 req->completion(stats, buffer, req->_bytes_done, req->dev_id);
1207 return 0; /* do not re-queue the TD */
1208 } /* ohci_bulk_td_handler() */
1212 * Request to send or receive bulk data. The completion() function
1213 * will be called when the transfer has completed or been aborted due
1214 * to an error.
1216 * bytes_transferred_p is a pointer to an integer that will be
1217 * -incremented- by the number of bytes that have been successfully
1218 * transferred. The interrupt handler will update it after each
1219 * internal TD completes successfully.
1221 * This function can NOT be called from an interrupt (?)
1222 * (TODO: verify & fix this if needed).
1224 * Returns: a pointer to the ED being used for this request. At the
1225 * moment, removing & freeing it is the responsibilty of the caller.
1227 static struct ohci_ed* ohci_request_bulk(struct ohci_bulk_request_state *bulk_request)
1229 /* local names for the readonly fields */
1230 struct usb_device *usb_dev = bulk_request->usb_dev;
1231 unsigned int pipe = bulk_request->pipe;
1232 void *data = bulk_request->data;
1233 int len = bulk_request->length;
1235 struct ohci_device *dev = usb_to_ohci(usb_dev);
1236 struct ohci_ed *bulk_ed;
1237 struct ohci_td *head_td;
1238 unsigned long flags;
1240 #ifdef OHCI_DEBUG
1241 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);
1242 #endif
1244 bulk_ed = ohci_get_free_ed(dev);
1245 if (!bulk_ed) {
1246 printk("usb-ohci: couldn't get ED for dev %p\n", dev);
1247 return NULL;
1250 /* allocate & fill in the TDs for this request */
1251 head_td = ohci_build_td_chain(dev, data, len, usb_pipeout(pipe),
1252 TOGGLE_AUTO,
1253 0 /* round not required */, 1 /* autofree */,
1254 bulk_request, /* dev_id: the bulk_request */
1255 ohci_bulk_td_handler,
1256 0 /* no additional TDs */);
1257 if (!head_td) {
1258 printk("usb-ohci: couldn't get TDs for dev %p\n", dev);
1259 ohci_free_ed(bulk_ed);
1260 return NULL;
1263 /* Set the max packet size, device speed, endpoint number, usb
1264 * device number (function address), and type of TD. */
1265 ohci_fill_ed(dev, bulk_ed,
1266 usb_maxpacket(usb_dev, pipe),
1267 usb_pipeslow(pipe),
1268 usb_pipe_endpdev(pipe), 0 /* bulk uses normal TDs */);
1270 /* initialize the internal counter */
1271 bulk_request->_bytes_done = 0;
1274 * Add the TDs to the ED
1276 spin_lock_irqsave(&ohci_edtd_lock, flags);
1277 bulk_ed->status |= OHCI_ED_SKIP;
1278 head_td = ohci_add_td_chain_to_ed(head_td, bulk_ed);
1279 bulk_ed->status &= ~OHCI_ED_SKIP;
1280 ohci_unhalt_ed(bulk_ed);
1281 spin_unlock_irqrestore(&ohci_edtd_lock, flags);
1284 #ifdef OHCI_DEBUG
1285 /* if (MegaDebug) { */
1286 /* complete transaction debugging output (before) */
1287 printk(KERN_DEBUG " Bulk ED %lx:\n", virt_to_bus(bulk_ed));
1288 show_ohci_ed(bulk_ed);
1289 printk(KERN_DEBUG " Bulk TDs %lx:\n", virt_to_bus(head_td));
1290 show_ohci_td_chain(head_td);
1291 /* } */
1292 #endif
1294 /* Give the ED to the HC */
1295 ohci_add_bulk_ed(dev->ohci, bulk_ed);
1297 return bulk_ed;
1298 } /* ohci_request_bulk() */
1301 static DECLARE_WAIT_QUEUE_HEAD(bulk_wakeup);
1304 static int ohci_bulk_msg_completed(int stats, void *buffer, int len, void *dev_id)
1306 if (dev_id != NULL) {
1307 int *completion_status = (int *)dev_id;
1308 *completion_status = stats;
1311 wake_up(&bulk_wakeup);
1312 return 0; /* don't requeue the TD */
1313 } /* ohci_bulk_msg_completed() */
1316 static int ohci_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, void *data, int len, unsigned long *bytes_transferred_p)
1318 DECLARE_WAITQUEUE(wait, current);
1319 int completion_status = USB_ST_INTERNALERROR;
1320 struct ohci_bulk_request_state req;
1321 struct ohci_ed *req_ed;
1323 /* ....... */
1325 #ifdef OHCI_DEBUG
1326 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);
1327 #endif
1329 req.usb_dev = usb_dev;
1330 req.pipe = pipe;
1331 req.data = data;
1332 req.length = len;
1333 req.bytes_transferred_p = bytes_transferred_p;
1334 req.dev_id = &completion_status;
1335 req.completion = ohci_bulk_msg_completed;
1338 * Start the transaction..
1340 current->state = TASK_UNINTERRUPTIBLE;
1341 add_wait_queue(&bulk_wakeup, &wait);
1343 req_ed = ohci_request_bulk(&req);
1345 /* FIXME this should to wait for a caller specified time... */
1346 schedule_timeout(HZ*5);
1348 #ifdef OHCI_DEBUG
1349 printk(KERN_DEBUG "ohci_bulk_msg request completed or timed out w/ status %x\n", completion_status);
1350 #endif
1352 remove_wait_queue(&bulk_wakeup, &wait);
1354 /* remove the ED from the HC */
1355 ohci_remove_bulk_ed(usb_to_ohci(usb_dev)->ohci, req_ed);
1356 ohci_free_ed(req_ed); /* return it to the pool */
1358 #ifdef OHCI_DEBUG
1359 printk(KERN_DEBUG "ohci_bulk_msg done.\n");
1360 #endif
1362 return completion_status;
1363 } /* ohci_bulk_msg() */
1366 /* .......... */
1370 * Allocate a new USB device to be attached to an OHCI controller
1372 static struct usb_device *ohci_usb_allocate(struct usb_device *parent)
1374 struct usb_device *usb_dev;
1375 struct ohci_device *dev;
1376 int idx;
1379 * Allocate the generic USB device
1381 usb_dev = kmalloc(sizeof(*usb_dev), GFP_KERNEL);
1382 if (!usb_dev)
1383 return NULL;
1385 memset(usb_dev, 0, sizeof(*usb_dev));
1388 * Allocate an OHCI device (EDs and TDs for this device)
1390 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1391 if (!dev) {
1392 kfree(usb_dev);
1393 return NULL;
1396 memset(dev, 0, sizeof(*dev));
1398 /* Initialize all EDs in a new device with the skip flag so that
1399 * they are ignored by the controller until set otherwise. */
1400 for (idx = 0; idx < NUM_EDS; ++idx) {
1401 dev->ed[idx].status = cpu_to_le32(OHCI_ED_SKIP);
1405 * Link them together
1407 usb_dev->hcpriv = dev;
1408 dev->usb = usb_dev;
1411 * Link the device to its parent (hub, etc..) if any.
1413 usb_dev->parent = parent;
1415 if (parent) {
1416 usb_dev->bus = parent->bus;
1417 dev->ohci = usb_to_ohci(parent)->ohci;
1420 return usb_dev;
1421 } /* ohci_usb_allocate() */
1425 * Free a usb device.
1427 * TODO This function needs to take better care of the EDs and TDs, etc.
1429 static int ohci_usb_deallocate(struct usb_device *usb_dev)
1431 struct ohci_device *dev = usb_to_ohci(usb_dev);
1433 ohci_remove_device(dev->ohci, usb_dev->devnum);
1435 /* kfree(usb_to_ohci(usb_dev)); */
1436 /* kfree(usb_dev); */
1437 return 0;
1442 * functions for the generic USB driver
1444 struct usb_operations ohci_device_operations = {
1445 ohci_usb_allocate,
1446 ohci_usb_deallocate,
1447 ohci_control_msg,
1448 ohci_bulk_msg,
1449 ohci_request_irq,
1454 * Reset an OHCI controller. Returns >= 0 on success.
1456 * Afterwards the HC will be in the "suspend" state which prevents you
1457 * from writing to some registers. Bring it to the operational state
1458 * ASAP.
1460 static int reset_hc(struct ohci *ohci)
1462 int timeout = 10000; /* prevent an infinite loop */
1464 #if 0
1465 printk(KERN_INFO "usb-ohci: resetting HC %p\n", ohci);
1466 #endif
1468 writel(~0x0, &ohci->regs->intrdisable); /* Disable HC interrupts */
1469 writel(1, &ohci->regs->cmdstatus); /* HC Reset */
1470 writel_mask(0x3f, &ohci->regs->control); /* move to UsbReset state */
1472 while ((readl(&ohci->regs->cmdstatus) & OHCI_CMDSTAT_HCR) != 0) {
1473 if (!--timeout) {
1474 printk(KERN_ERR "usb-ohci: USB HC reset timed out!\n");
1475 return -1;
1477 udelay(1);
1480 printk(KERN_DEBUG "usb-ohci: HC %p reset.\n", ohci);
1482 return 0;
1483 } /* reset_hc() */
1487 * Reset and start an OHCI controller. Returns >= 0 on success.
1489 static int start_hc(struct ohci *ohci)
1491 int ret = 0;
1492 int fminterval;
1493 __u32 what_to_enable;
1495 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
1497 fminterval = readl(&ohci->regs->fminterval) & 0x3fff;
1498 #if 0
1499 printk(KERN_DEBUG "entering start_hc %p\n", ohci);
1500 #endif
1502 if (reset_hc(ohci) < 0)
1503 return -1;
1505 /* restore registers cleared by the reset */
1506 writel(virt_to_bus(root_hub->hcca), &ohci->regs->hcca);
1509 * XXX Should fminterval also be set here?
1510 * The spec suggests 0x2edf [11,999]. (FIXME: make this a constant)
1512 /* fminterval |= (0x2edf << 16); */
1513 fminterval = (10240 << 16) | 11999;
1514 writel(fminterval, &ohci->regs->fminterval);
1515 /* Start periodic transfers at 90% of fminterval (fmremaining
1516 * counts down; this will put them in the first 10% of the
1517 * frame). */
1518 writel((0x2edf*9)/10, &ohci->regs->periodicstart);
1521 * FNO (frame number overflow) could be enabled... they
1522 * occur every 32768 frames (every 32-33 seconds). This is
1523 * useful for debugging and as a bus heartbeat. -greg
1525 /* Choose the interrupts we care about */
1526 what_to_enable = OHCI_INTR_MIE |
1527 #ifdef OHCI_RHSC_INT
1528 OHCI_INTR_RHSC |
1529 #endif
1530 /* | OHCI_INTR_FNO */
1531 OHCI_INTR_WDH;
1532 writel( what_to_enable, &ohci->regs->intrenable);
1534 /* Enter the USB Operational state & start the frames a flowing.. */
1535 writel_set(OHCI_USB_OPER, &ohci->regs->control);
1537 /* Enable control lists */
1538 writel_set(OHCI_USB_IE | OHCI_USB_CLE | OHCI_USB_BLE, &ohci->regs->control);
1540 /* Force global power enable -gal@cs.uni-magdeburg.de */
1542 * This turns on global power switching for all the ports
1543 * and tells the HC that all of the ports should be powered on
1544 * all of the time.
1546 * TODO: This could be battery draining for laptops.. We
1547 * should implement power switching.
1549 writel_set( OHCI_ROOT_A_NPS, &ohci->regs->roothub.a );
1550 writel_mask( ~((__u32)OHCI_ROOT_A_PSM), &ohci->regs->roothub.a );
1552 /* Turn on power to the root hub ports (thanks Roman!) */
1553 writel( OHCI_ROOT_LPSC, &ohci->regs->roothub.status );
1555 printk(KERN_INFO "usb-ohci: host controller operational\n");
1557 return ret;
1558 } /* start_hc() */
1562 * Reset a root hub port
1564 static void ohci_reset_port(struct ohci *ohci, unsigned int port)
1566 int status;
1568 /* Don't allow overflows. */
1569 if (port >= MAX_ROOT_PORTS) {
1570 printk(KERN_ERR "usb-ohci: bad port #%d in ohci_reset_port\n", port);
1571 port = MAX_ROOT_PORTS-1;
1574 writel(PORT_PRS, &ohci->regs->roothub.portstatus[port]); /* Reset */
1577 * Wait for the reset to complete.
1579 wait_ms(20);
1581 /* check port status to see that the reset completed */
1582 status = readl(&ohci->regs->roothub.portstatus[port]);
1583 if (status & PORT_PRS) {
1584 /* reset failed, try harder? */
1585 printk(KERN_ERR "usb-ohci: port %d reset failed, retrying\n", port);
1586 writel(PORT_PRS, &ohci->regs->roothub.portstatus[port]);
1587 wait_ms(50);
1590 /* TODO we might need to re-enable the port here or is that
1591 * done elsewhere? */
1593 } /* ohci_reset_port */
1597 * This gets called if the connect status on the root hub changes.
1599 static void ohci_connect_change(struct ohci * ohci, int port)
1601 struct usb_device *usb_dev;
1602 struct ohci_device *dev;
1603 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
1604 /* memory I/O address of the port status register */
1605 __u32 *portaddr = &ohci->regs->roothub.portstatus[port];
1606 int portstatus;
1608 #ifdef OHCI_DEBUG
1609 printk(KERN_DEBUG "ohci_connect_change on port %d\n", port);
1610 #endif
1613 * Because of the status change we have to forget
1614 * everything we think we know about the device
1615 * on this root hub port. It may have changed.
1617 usb_disconnect(root_hub->usb->children + port);
1619 portstatus = readl(portaddr);
1621 /* disable the port if nothing is connected */
1622 if (!(portstatus & PORT_CCS)) {
1623 writel(PORT_CCS, portaddr);
1624 /* We need to reset the CSC bit -after- disabling the
1625 * port because it causes the CSC bit to come on
1626 * again... */
1627 wait_ms(20);
1628 writel(PORT_CSC, portaddr);
1629 #ifdef OHCI_DEBUG
1630 printk(KERN_DEBUG "ohci port %d disabled, nothing connected.\n", port);
1631 #endif
1632 return;
1636 * Allocate a device for the new thingy that's been attached
1638 usb_dev = ohci_usb_allocate(root_hub->usb);
1639 dev = usb_dev->hcpriv;
1641 dev->ohci = ohci;
1643 usb_connect(dev->usb);
1645 /* link it into the bus's device tree */
1646 root_hub->usb->children[port] = usb_dev;
1648 wait_ms(200); /* wait for powerup; XXX is this needed? */
1649 ohci_reset_port(ohci, port);
1651 /* Get information on speed by using LSD */
1652 usb_dev->slow = readl(portaddr) & PORT_LSDA ? 1 : 0;
1655 * Do generic USB device tree processing on the new device.
1657 usb_new_device(usb_dev);
1659 } /* ohci_connect_change() */
1663 * This gets called when the root hub configuration
1664 * has changed. Just go through each port, seeing if
1665 * there is something interesting happening.
1667 static void ohci_check_configuration(struct ohci *ohci)
1669 struct ohci_regs *regs = ohci->regs;
1670 int num = 0;
1671 int maxport = readl(&ohci->regs->roothub) & 0xff;
1672 __u32 rh_change_flags = PORT_CSC | PORT_PESC; /* root hub status changes */
1674 #ifdef OHCI_DEBUG
1675 printk(KERN_DEBUG "entering ohci_check_configuration %p\n", ohci);
1676 #endif
1678 do {
1679 __u32 *portstatus_p = &regs->roothub.portstatus[num];
1680 if (readl(portstatus_p) & rh_change_flags) {
1681 /* acknowledge the root hub status changes */
1682 writel_set(rh_change_flags, portstatus_p);
1683 /* disable the port if nothing is on it */
1684 /* check the port for a nifty device */
1685 ohci_connect_change(ohci, num);
1687 } while (++num < maxport);
1689 #if 0
1690 printk(KERN_DEBUG "leaving ohci_check_configuration %p\n", ohci);
1691 #endif
1692 } /* ohci_check_configuration() */
1697 * Check root hub port status and wake the control thread up if
1698 * anything has changed.
1700 * This function is called from the interrupt handler.
1702 static void ohci_root_hub_events(struct ohci *ohci)
1704 int num = 0;
1705 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
1706 int maxport = root_hub->usb->maxchild;
1708 if (!waitqueue_active(&ohci_configure))
1709 return;
1710 do {
1711 __u32 *portstatus_p = &ohci->regs->roothub.portstatus[num];
1712 if (readl(portstatus_p) & PORT_CSC) {
1713 if (waitqueue_active(&ohci_configure))
1714 wake_up(&ohci_configure);
1715 return;
1717 } while (++num < maxport);
1719 } /* ohci_root_hub_events() */
1723 * The done list is in reverse order; we need to process TDs in the
1724 * order they were finished (FIFO). This function builds the FIFO
1725 * list using the next_dl_td pointer.
1727 * This function originally by Roman Weissgaerber (weissg@vienna.at)
1729 * This function is called from the interrupt handler.
1731 static struct ohci_td * ohci_reverse_donelist(struct ohci * ohci)
1733 __u32 td_list_hc;
1734 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
1735 struct ohci_hcca *hcca = root_hub->hcca;
1736 struct ohci_td *td_list = NULL;
1737 struct ohci_td *td_rev = NULL;
1739 td_list_hc = le32_to_cpup(&hcca->donehead) & 0xfffffff0;
1740 hcca->donehead = 0;
1742 while(td_list_hc) {
1743 td_list = (struct ohci_td *) bus_to_virt(td_list_hc);
1744 td_list->next_dl_td = td_rev;
1745 td_rev = td_list;
1746 td_list_hc = le32_to_cpup(&td_list->next_td) & 0xfffffff0;
1749 return td_list;
1750 } /* ohci_reverse_donelist() */
1754 * Collect this interrupt's goodies off of the list of finished TDs
1755 * that the OHCI controller is kind enough to setup for us.
1757 * This function is called from the interrupt handler.
1759 static void ohci_reap_donelist(struct ohci *ohci)
1761 struct ohci_td *td; /* used for walking the list */
1763 /* um... isn't this dangerous to do in an interrupt handler? -greg */
1764 spin_lock(&ohci_edtd_lock);
1766 /* create the FIFO ordered donelist */
1767 td = ohci_reverse_donelist(ohci);
1769 while (td != NULL) {
1770 struct ohci_td *next_td = td->next_dl_td;
1771 int cc = OHCI_TD_CC_GET(le32_to_cpup(&td->info));
1773 if (td_dummy(*td))
1774 printk(KERN_ERR "yikes! reaping a dummy TD\n");
1776 if (cc != 0 && ohci_ed_halted(td->ed) && !td_endofchain(*td)) {
1778 * There was an error on this TD and the ED
1779 * is halted, and this was not the last TD
1780 * of the transaction, so there will be TDs
1781 * to clean off the ED.
1783 struct ohci_ed *ed = td->ed;
1784 struct ohci_td *tail_td = bus_to_virt(ed_tail_td(ed));
1785 struct ohci_td *ntd;
1787 ohci_free_td(td);
1788 td = ntd = bus_to_virt(ed_head_td(ed));
1789 while (td != tail_td) {
1790 ntd = bus_to_virt(le32_to_cpup(&td->next_td));
1792 /* only deal with TDs from this ED,
1793 * the host controller could have
1794 * processed other endpoints at the
1795 * same time as this one.. */
1796 if (td->ed == ed) {
1797 if (td_endofchain(*td))
1798 break;
1800 /* FIXME: unlink this TD from the
1801 * reverse donelist! */
1802 ohci_free_td(td);
1805 td = ntd;
1807 /* Set the ED head past the ones we cleaned
1808 off, and clear the halted flag */
1809 set_ed_head_td(ed, virt_to_bus(ntd));
1810 ohci_unhalt_ed(ed);
1811 /* If we didn't find an endofchain TD, give up */
1812 if (td == tail_td) {
1813 td = next_td;
1814 continue;
1818 /* Check if TD should be re-queued */
1819 if ((td->completed != NULL) &&
1820 (td->completed(cc, td->data, ohci_td_bytes_done(td), td->dev_id))) {
1821 /* Mark the TD as active again:
1822 * Set the not accessed condition code
1823 * Reset the Error count
1825 td->info |= cpu_to_le32(OHCI_TD_CC_NEW);
1826 clear_td_errorcount(td);
1827 /* reset the toggle field to TOGGLE_AUTO (0) */
1828 td->info &= cpu_to_le32(~OHCI_TD_DT);
1830 /* point it back to the start of the data buffer */
1831 td->cur_buf = cpu_to_le32(virt_to_bus(td->data));
1833 /* insert it back on its ED */
1834 ohci_add_td_to_ed(td, td, td->ed);
1835 } else {
1836 /* return it to the pool of free TDs */
1837 if (can_auto_free(*td))
1838 ohci_free_td(td);
1841 td = next_td;
1844 spin_unlock(&ohci_edtd_lock);
1845 } /* ohci_reap_donelist() */
1849 * Get annoyed at the controller for bothering us.
1850 * This pretty much follows the OHCI v1.0a spec, section 5.3.
1852 static void ohci_interrupt(int irq, void *__ohci, struct pt_regs *r)
1854 struct ohci *ohci = __ohci;
1855 struct ohci_regs *regs = ohci->regs;
1856 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
1857 struct ohci_hcca *hcca = root_hub->hcca;
1858 __u32 status, context;
1860 /* Save the status of the interrupts that are enabled */
1861 status = readl(&regs->intrstatus);
1862 status &= readl(&regs->intrenable);
1864 /* make context = the interrupt status bits that we care about */
1865 if (hcca->donehead != 0) {
1866 context = OHCI_INTR_WDH; /* hcca donehead needs processing */
1867 if (hcca->donehead & cpu_to_le32(1)) {
1868 context |= status; /* other status change to check */
1870 } else {
1871 context = status;
1872 if (!context) {
1873 /* TODO increment a useless interrupt counter here */
1874 return;
1878 /* Disable HC interrupts */ /* why? - paulus */
1879 writel(OHCI_INTR_MIE, &regs->intrdisable);
1881 #if 0
1882 /* Only do this for SERIOUS debugging, be sure kern.debug logs
1883 * are not going to the console as this can cause your
1884 * machine to lock up if so... -greg */
1885 show_ohci_status(ohci);
1886 #endif
1888 /* Process the done list */
1889 if (context & OHCI_INTR_WDH) {
1890 /* See which TD's completed.. */
1891 ohci_reap_donelist(ohci);
1893 /* reset the done queue and tell the controller */
1894 hcca->donehead = 0; /* XXX already done in ohci_reverse_donelist */
1895 writel(OHCI_INTR_WDH, &regs->intrstatus);
1897 context &= ~OHCI_INTR_WDH; /* mark this as checked */
1900 #ifdef OHCI_RHSC_INT
1901 /* NOTE: this is very funky on some USB controllers (ie: it
1902 * doesn't work right). Using the ohci_timer instead to poll
1903 * the root hub is a much better choice. */
1904 /* Process any root hub status changes */
1905 if (context & OHCI_INTR_RHSC) {
1906 /* Wake the thread to process root hub events */
1907 if (waitqueue_active(&ohci_configure))
1908 wake_up(&ohci_configure);
1910 writel(OHCI_INTR_RHSC, &regs->intrstatus);
1912 * Don't unset RHSC in context; it should be disabled.
1913 * The control thread will re-enable it after it has
1914 * checked the root hub status.
1917 #endif
1919 /* Start of Frame interrupts, used during safe ED removal */
1920 if (context & (OHCI_INTR_SF)) {
1921 writel(OHCI_INTR_SF, &regs->intrstatus);
1922 if (waitqueue_active(&start_of_frame_wakeup))
1923 wake_up(&start_of_frame_wakeup);
1924 /* Do NOT mark the frame start interrupt as checked
1925 * as we don't want to receive any more of them until
1926 * asked. */
1929 /* Check those "other" pesky bits */
1930 if (context & (OHCI_INTR_FNO)) {
1931 writel(OHCI_INTR_FNO, &regs->intrstatus);
1932 context &= ~OHCI_INTR_FNO; /* mark this as checked */
1934 if (context & OHCI_INTR_SO) {
1935 writel(OHCI_INTR_SO, &regs->intrstatus);
1936 context &= ~OHCI_INTR_SO; /* mark this as checked */
1938 if (context & OHCI_INTR_RD) {
1939 writel(OHCI_INTR_RD, &regs->intrstatus);
1940 context &= ~OHCI_INTR_RD; /* mark this as checked */
1942 if (context & OHCI_INTR_UE) {
1943 /* FIXME: need to have the control thread reset the
1944 * controller now and keep a count of unrecoverable
1945 * errors. If there are too many, it should just shut
1946 * the broken controller down entirely. */
1947 writel(OHCI_INTR_UE, &regs->intrstatus);
1948 context &= ~OHCI_INTR_UE; /* mark this as checked */
1950 if (context & OHCI_INTR_OC) {
1951 writel(OHCI_INTR_OC, &regs->intrstatus);
1952 context &= ~OHCI_INTR_OC; /* mark this as checked */
1955 /* Mask out any remaining unprocessed or unmasked interrupts
1956 * so that we don't get any more of them. */
1957 if (context & ~OHCI_INTR_MIE) {
1958 writel(context, &regs->intrdisable);
1961 /* Re-enable HC interrupts */
1962 writel(OHCI_INTR_MIE, &regs->intrenable);
1964 } /* ohci_interrupt() */
1968 * Allocate the resources required for running an OHCI controller.
1969 * Host controller interrupts must not be running while calling this
1970 * function or the penguins will get angry.
1972 * The mem_base parameter must be the usable -virtual- address of the
1973 * host controller's memory mapped I/O registers.
1975 static struct ohci *alloc_ohci(void* mem_base)
1977 int i;
1978 struct ohci *ohci;
1979 struct usb_bus *bus;
1980 struct ohci_device *dev;
1981 struct usb_device *usb;
1983 #if 0
1984 printk(KERN_DEBUG "entering alloc_ohci %p\n", mem_base);
1985 #endif
1987 ohci = kmalloc(sizeof(*ohci), GFP_KERNEL);
1988 if (!ohci)
1989 return NULL;
1991 memset(ohci, 0, sizeof(*ohci));
1993 ohci->irq = -1;
1994 ohci->regs = mem_base;
1995 INIT_LIST_HEAD(&ohci->interrupt_list);
1997 bus = kmalloc(sizeof(*bus), GFP_KERNEL);
1998 if (!bus)
1999 return NULL;
2001 memset(bus, 0, sizeof(*bus));
2003 ohci->bus = bus;
2004 bus->hcpriv = ohci;
2005 bus->op = &ohci_device_operations;
2008 * Allocate the USB device structure and root hub.
2010 * Here we allocate our own root hub and TDs as well as the
2011 * OHCI host controller communications area. The HCCA is just
2012 * a nice pool of memory with pointers to endpoint descriptors
2013 * for the different interrupts.
2015 usb = ohci_usb_allocate(NULL);
2016 if (!usb)
2017 return NULL;
2019 dev = usb_to_ohci(usb);
2020 ohci->bus->root_hub= ohci_to_usb(dev);
2021 usb->bus = bus;
2023 /* Initialize the root hub */
2024 dev->ohci = ohci; /* link back to the controller */
2027 * Allocate the Host Controller Communications Area on a 256
2028 * byte boundary. XXX take the easy way out and just grab a
2029 * page as that's guaranteed to have a nice boundary.
2031 dev->hcca = (struct ohci_hcca *) __get_free_page(GFP_KERNEL);
2032 memset(dev->hcca, 0, sizeof(struct ohci_hcca));
2034 /* Tell the controller where the HCCA is */
2035 writel(virt_to_bus(dev->hcca), &ohci->regs->hcca);
2037 #if 0
2038 printk(KERN_DEBUG "usb-ohci: HCCA allocated at %p (bus %p)\n", dev->hcca, (void*)virt_to_bus(dev->hcca));
2039 #endif
2041 /* Get the number of ports on the root hub */
2042 usb->maxchild = readl(&ohci->regs->roothub.a) & 0xff;
2043 if (usb->maxchild > MAX_ROOT_PORTS) {
2044 printk(KERN_INFO "usb-ohci: Limited to %d ports\n", MAX_ROOT_PORTS);
2045 usb->maxchild = MAX_ROOT_PORTS;
2047 if (usb->maxchild < 1) {
2048 printk(KERN_ERR "usb-ohci: Less than one root hub port? Impossible!\n");
2049 usb->maxchild = 1;
2051 printk(KERN_DEBUG "usb-ohci: %d root hub ports found\n", usb->maxchild);
2054 * Initialize the ED polling "tree" (for simplicity's sake in
2055 * this driver many nodes in the tree will be identical)
2057 dev->ed[ED_INT_32].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_16]));
2058 dev->ed[ED_INT_16].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_8]));
2059 dev->ed[ED_INT_8].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_4]));
2060 dev->ed[ED_INT_4].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_2]));
2061 dev->ed[ED_INT_2].next_ed = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_1]));
2064 * Initialize the polling table to call interrupts at the
2065 * intended intervals. Note that these EDs are just
2066 * placeholders. They have their SKIP bit set and are used as
2067 * list heads to insert real EDs onto.
2069 dev->hcca->int_table[0] = cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_1]));
2070 for (i = 1; i < NUM_INTS; i++) {
2071 if (i & 16)
2072 dev->hcca->int_table[i] =
2073 cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_32]));
2074 if (i & 8)
2075 dev->hcca->int_table[i] =
2076 cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_16]));
2077 if (i & 4)
2078 dev->hcca->int_table[i] =
2079 cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_8]));
2080 if (i & 2)
2081 dev->hcca->int_table[i] =
2082 cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_4]));
2083 if (i & 1)
2084 dev->hcca->int_table[i] =
2085 cpu_to_le32(virt_to_bus(&dev->ed[ED_INT_2]));
2089 * Tell the controller where the control and bulk lists are.
2090 * The lists start out empty.
2092 writel(0, &ohci->regs->ed_controlhead);
2093 writel(0, &ohci->regs->ed_bulkhead);
2095 #ifdef OHCI_DEBUG
2096 printk(KERN_DEBUG "alloc_ohci(): controller\n");
2097 show_ohci_status(ohci);
2098 #endif
2100 #if 0
2101 printk(KERN_DEBUG "leaving alloc_ohci %p\n", ohci);
2102 #endif
2104 return ohci;
2105 } /* alloc_ohci() */
2109 * De-allocate all resoueces..
2111 static void release_ohci(struct ohci *ohci)
2113 printk(KERN_INFO "Releasing OHCI controller 0x%p\n", ohci);
2115 #ifdef OHCI_TIMER
2116 /* stop our timer */
2117 del_timer(&ohci_timer);
2118 #endif
2119 if (ohci->irq >= 0) {
2120 free_irq(ohci->irq, ohci);
2121 ohci->irq = -1;
2124 /* stop all OHCI interrupts */
2125 writel(~0x0, &ohci->regs->intrdisable);
2127 if (ohci->bus->root_hub) {
2128 struct ohci_device *root_hub=usb_to_ohci(ohci->bus->root_hub);
2129 /* ensure that HC is stopped before releasing the HCCA */
2130 writel(OHCI_USB_SUSPEND, &ohci->regs->control);
2131 free_page((unsigned long) root_hub->hcca);
2132 kfree(ohci->bus->root_hub);
2133 root_hub->hcca = NULL;
2134 ohci->bus->root_hub = NULL;
2137 /* unmap the IO address space */
2138 iounmap(ohci->regs);
2140 kfree(ohci);
2142 MOD_DEC_USE_COUNT;
2144 /* If the ohci itself were dynamic we'd free it here */
2146 printk(KERN_DEBUG "usb-ohci: HC resources released.\n");
2147 } /* release_ohci() */
2151 * USB OHCI control thread
2153 static int ohci_control_thread(void * __ohci)
2155 struct ohci *ohci = (struct ohci *)__ohci;
2158 * I'm unfamiliar with the SMP kernel locking.. where should
2159 * this be released and what does it do? -greg
2161 lock_kernel();
2164 * This thread doesn't need any user-level access,
2165 * so get rid of all of our resources..
2167 printk(KERN_DEBUG "ohci-control thread code for 0x%p code at 0x%p\n", __ohci, &ohci_control_thread);
2168 exit_mm(current);
2169 exit_files(current);
2170 exit_fs(current);
2172 strcpy(current->comm, "ohci-control");
2174 usb_register_bus(ohci->bus);
2177 * Damn the torpedoes, full speed ahead
2179 if (start_hc(ohci) < 0) {
2180 printk(KERN_ERR "usb-ohci: failed to start the controller\n");
2181 release_ohci(ohci);
2182 usb_deregister_bus(ohci->bus);
2183 printk(KERN_INFO "leaving ohci_control_thread %p\n", __ohci);
2184 return 0;
2187 for(;;) {
2188 siginfo_t info;
2189 int unsigned long signr;
2191 wait_ms(200);
2193 /* check the root hub configuration for changes. */
2194 ohci_check_configuration(ohci);
2196 /* re-enable root hub status change interrupts. */
2197 #ifdef OHCI_RHSC_INT
2198 writel(OHCI_INTR_RHSC, &ohci->regs->intrenable);
2199 #endif
2201 printk(KERN_DEBUG "ohci-control thread sleeping\n");
2202 interruptible_sleep_on(&ohci_configure);
2203 #ifdef CONFIG_APM
2204 if (apm_resume) {
2205 apm_resume = 0;
2206 if (start_hc(ohci) < 0)
2207 break;
2208 continue;
2210 #endif
2213 * If we were woken up by a signal, see if its useful,
2214 * otherwise exit.
2216 if (signal_pending(current)) {
2217 /* sending SIGUSR1 makes us print out some info */
2218 spin_lock_irq(&current->sigmask_lock);
2219 signr = dequeue_signal(&current->blocked, &info);
2220 spin_unlock_irq(&current->sigmask_lock);
2222 if(signr == SIGUSR1) {
2223 /* TODO: have it do a full ed/td queue dump? */
2224 printk(KERN_DEBUG "OHCI status dump:\n");
2225 show_ohci_status(ohci);
2226 } else if (signr == SIGUSR2) {
2227 /* toggle mega TD/ED debugging output */
2228 #ifdef OHCI_DEBUG
2229 MegaDebug = !MegaDebug;
2230 printk(KERN_DEBUG "usb-ohci: Mega debugging %sabled.\n",
2231 MegaDebug ? "en" : "dis");
2232 #endif
2233 } else {
2234 /* unknown signal, exit the thread */
2235 break;
2238 } /* for (;;) */
2240 reset_hc(ohci);
2241 release_ohci(ohci);
2242 usb_deregister_bus(ohci->bus);
2243 printk(KERN_DEBUG "ohci-control thread for 0x%p exiting\n", __ohci);
2245 return 0;
2246 } /* ohci_control_thread() */
2249 #ifdef CONFIG_APM
2250 static int handle_apm_event(apm_event_t event)
2252 static int down = 0;
2254 switch (event) {
2255 case APM_SYS_SUSPEND:
2256 case APM_USER_SUSPEND:
2257 if (down) {
2258 printk(KERN_DEBUG "usb-ohci: received extra suspend event\n");
2259 break;
2261 down = 1;
2262 break;
2263 case APM_NORMAL_RESUME:
2264 case APM_CRITICAL_RESUME:
2265 if (!down) {
2266 printk(KERN_DEBUG "usb-ohci: received bogus resume event\n");
2267 break;
2269 down = 0;
2270 if (waitqueue_active(&ohci_configure)) {
2271 apm_resume = 1;
2272 wake_up(&ohci_configure);
2274 break;
2276 return 0;
2277 } /* handle_apm_event() */
2278 #endif
2281 #ifdef OHCI_TIMER
2283 * Inspired by Iñaky's driver. This function is a timer routine that
2284 * is called every OHCI_TIMER_FREQ ms. It polls the root hub for
2285 * status changes as on my system the RHSC interrupt just doesn't
2286 * play well with others.. (so RHSC is turned off by default in this
2287 * driver)
2288 * [my controller is a "SiS 7001 USB (rev 16)"]
2289 * -greg
2291 static void ohci_timer_func (unsigned long ohci_ptr)
2293 struct ohci *ohci = (struct ohci*)ohci_ptr;
2295 ohci_root_hub_events(ohci);
2297 /* set the next timer */
2298 mod_timer(&ohci_timer, jiffies + ((OHCI_TIMER_FREQ*HZ)/1000));
2300 } /* ohci_timer_func() */
2301 #endif
2305 * Increment the module usage count, start the control thread and
2306 * return success if the controller is good.
2308 static int found_ohci(int irq, void* mem_base)
2310 int retval;
2311 struct ohci *ohci;
2313 #if 0
2314 printk(KERN_DEBUG "entering found_ohci %d %p\n", irq, mem_base);
2315 #endif
2317 /* Allocate the running OHCI structures */
2318 ohci = alloc_ohci(mem_base);
2319 if (!ohci) {
2320 return -ENOMEM;
2323 #ifdef OHCI_TIMER
2324 init_timer(&ohci_timer);
2325 ohci_timer.expires = jiffies + ((OHCI_TIMER_FREQ*HZ)/1000);
2326 ohci_timer.data = (unsigned long)ohci;
2327 ohci_timer.function = ohci_timer_func;
2328 add_timer(&ohci_timer);
2329 #endif
2331 retval = -EBUSY;
2332 if (request_irq(irq, ohci_interrupt, SA_SHIRQ, "usb-ohci", ohci) == 0) {
2333 int pid;
2335 ohci->irq = irq;
2337 #ifdef OHCI_DEBUG
2338 printk(KERN_DEBUG "usb-ohci: forking ohci-control thread for 0x%p\n", ohci);
2339 #endif
2341 /* fork off the handler */
2342 pid = kernel_thread(ohci_control_thread, ohci,
2343 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
2344 if (pid >= 0) {
2345 return 0;
2348 retval = pid;
2349 } else {
2350 printk(KERN_ERR "usb-ohci: Couldn't allocate interrupt %d\n", irq);
2352 release_ohci(ohci);
2354 #ifdef OHCI_DEBUG
2355 printk(KERN_DEBUG "leaving found_ohci %d %p\n", irq, mem_base);
2356 #endif
2358 return retval;
2359 } /* found_ohci() */
2363 * If this controller is for real, map the IO memory and proceed
2365 static int init_ohci(struct pci_dev *dev)
2367 unsigned long mem_base = dev->base_address[0];
2369 /* If its OHCI, its memory */
2370 if (mem_base & PCI_BASE_ADDRESS_SPACE_IO)
2371 return -ENODEV;
2373 /* Get the memory address and map it for IO */
2374 mem_base &= PCI_BASE_ADDRESS_MEM_MASK;
2376 /* no interrupt won't work... */
2377 if (dev->irq == 0) {
2378 printk(KERN_ERR "usb-ohci: no irq assigned? check your BIOS settings.\n");
2379 return -ENODEV;
2383 * FIXME ioremap_nocache isn't implemented on all CPUs (such
2384 * as the Alpha) [?] What should I use instead...
2386 * The iounmap() is done on in release_ohci.
2388 mem_base = (unsigned long) ioremap_nocache(mem_base, 4096);
2390 if (!mem_base) {
2391 printk(KERN_ERR "Error mapping OHCI memory\n");
2392 return -EFAULT;
2394 MOD_INC_USE_COUNT;
2396 #ifdef OHCI_DEBUG
2397 printk(KERN_INFO "usb-ohci: Warning! Gobs of debugging output has been enabled.\n");
2398 printk(KERN_INFO " Check your kern.debug logs for the bulk of it.\n");
2399 #endif
2401 if (found_ohci(dev->irq, (void *) mem_base) < 0) {
2402 MOD_DEC_USE_COUNT;
2403 return -1;
2406 return 0;
2407 } /* init_ohci() */
2409 /* TODO this should be named following Linux convention and go in pci.h */
2410 #define PCI_CLASS_SERIAL_USB_OHCI ((PCI_CLASS_SERIAL_USB << 8) | 0x0010)
2413 * Search the PCI bus for an OHCI USB controller and set it up
2415 * If anyone wants multiple controllers this will need to be
2416 * updated.. Right now, it just picks the first one it finds.
2418 int ohci_init(void)
2420 int retval;
2421 struct pci_dev *dev = NULL;
2422 /*u8 type;*/
2424 if (sizeof(struct ohci_device) > 4096) {
2425 printk(KERN_ERR "usb-ohci: struct ohci_device to large\n");
2426 return -ENODEV;
2429 printk(KERN_INFO "OHCI USB Driver loading\n");
2431 retval = -ENODEV;
2432 for (;;) {
2433 /* Find an OHCI USB controller */
2434 dev = pci_find_class(PCI_CLASS_SERIAL_USB_OHCI, dev);
2435 if (!dev)
2436 break;
2438 /* Verify that its OpenHCI by checking for MMIO */
2439 /* pci_read_config_byte(dev, PCI_CLASS_PROG, &type);
2440 if (!type)
2441 continue; */
2443 /* Ok, set it up */
2444 retval = init_ohci(dev);
2445 if (retval < 0)
2446 continue;
2448 #ifdef CONFIG_APM
2449 apm_register_callback(&handle_apm_event);
2450 #endif
2452 return 0; /* no error */
2454 return retval;
2455 } /* ohci_init */
2458 #ifdef MODULE
2460 * Clean up when unloading the module
2462 void cleanup_module(void){
2463 # ifdef CONFIG_APM
2464 apm_unregister_callback(&handle_apm_event);
2465 # endif
2466 printk(KERN_ERR "usb-ohci: module unloaded\n");
2469 int init_module(void){
2470 return ohci_init();
2472 #endif //MODULE
2474 /* vim:sw=8