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>
44 #include <asm/system.h>
49 #include <linux/apm_bios.h>
50 static int handle_apm_event(apm_event_t event
);
51 static int apm_resume
= 0;
54 static DECLARE_WAIT_QUEUE_HEAD(ohci_configure
);
56 #ifdef CONFIG_USB_OHCI_DEBUG
57 #define OHCI_DEBUG /* to make typing it easier.. */
60 int MegaDebug
= 0; /* SIGUSR2 to the control thread toggles this */
64 static struct timer_list ohci_timer
; /* timer for root hub polling */
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)
75 static const char *cc_names
[16] = {
79 "data toggle mismatch",
81 "device not responding",
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
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
;
120 if (ed
->tail_td
== 0) {
121 printk(KERN_ERR
"eek! an ED without a dummy_td\n");
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
))) {
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 */
142 /* Turn the first TD into a dummy */
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
;
164 /* Find the last TD in this chain, storing its pointer in cur_td */
167 __u32 next_td
= cur_td
->next_td
;
169 /* advance to the next td, exit if there isn't one */
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() */
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
)
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
);
260 * Pick a good frequency endpoint based on the requested period
262 int_ed
= &root_hub
->ed
[ms_to_ed_int(period
)];
264 printk(KERN_DEBUG
"usb-ohci: Using INT ED queue %d for %dms period\n",
265 ms_to_ed_int(period
), period
);
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
, ®s
->intrstatus
);
302 writel(OHCI_INTR_SF
, ®s
->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
);
323 hw_listcurrent
= ®s
->ed_controlcurrent
;
326 hw_listcurrent
= ®s
->ed_bulkcurrent
;
330 hw_listcurrent
= ®s
->ed_periodcurrent
;
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
)) {
342 printk(KERN_INFO
"Waiting a frame for OHC to finish with ED %p [%x %x %x %x]\n", ed
, FIELDS_OF_ED(ed
));
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
)
361 struct ohci_regs
*regs
= ohci
->regs
;
363 __u32 bus_ed
= virt_to_bus(ed
);
365 __u32
*hw_listhead_p
;
367 if (ed
== NULL
|| !bus_ed
)
369 ed
->status
|= cpu_to_le32(OHCI_ED_SKIP
);
373 hw_listhead_p
= ®s
->ed_controlhead
;
376 hw_listhead_p
= ®s
->ed_bulkhead
;
379 printk(KERN_ERR
"Unknown HCD ED type %d.\n", ed_type
);
383 bus_cur
= readl(hw_listhead_p
);
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 */
401 cur
= bus_to_virt(le32_to_cpup(&cur
->next_ed
));
404 /* unlink from the list */
405 prev
->next_ed
= cur
->next_ed
;
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 */
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
;
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
));
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
);
467 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
468 prev_ed
->next_ed
= ed
->next_ed
;
469 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
475 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
476 if (cur_ed
->next_ed
) {
478 cur_ed
= bus_to_virt(le32_to_cpup(&cur_ed
->next_ed
));
479 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
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
);
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
)
501 __u32
*prevp
= headp
;
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
;
513 prevp
= &ed
->next_ed
;
522 * Remove all the EDs for a given device from all lists.
524 void ohci_remove_device(struct ohci
*ohci
, int devnum
)
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
);
534 head
= cpu_to_le32(readl(®s
->ed_controlhead
));
535 if (ohci_remove_device_list(&head
, devnum
))
536 writel(le32_to_cpup(&head
), ®s
->ed_controlhead
);
539 head
= cpu_to_le32(readl(®s
->ed_bulkhead
));
540 if (ohci_remove_device_list(&head
, devnum
))
541 writel(le32_to_cpup(&head
), ®s
->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
)
562 struct ohci_td
*head_td
;
564 if ((td
== NULL
) || (ed
== NULL
))
567 if (ed_head_td(ed
) == 0)
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
);
582 struct ohci_td
*prev_td
, *cur_td
;
584 /* FIXME: collapse this into a nice simple loop :) */
585 if (head_td
->next_td
!= 0) {
587 cur_td
= bus_to_virt(le32_to_cpup(&head_td
->next_td
));
591 prev_td
->next_td
= cur_td
->next_td
;
594 if (cur_td
->next_td
== 0)
597 cur_td
= bus_to_virt(le32_to_cpup(&cur_td
->next_td
));
602 td
->next_td
= 0; /* remove the TDs links */
605 /* return this TD to the pool of free TDs */
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
)
624 printk(KERN_DEBUG
"in ohci_get_free_td()\n");
627 /* FIXME: this is horribly inefficient */
628 for (idx
=0; idx
< NUM_TDS
; idx
++) {
630 show_ohci_td(&dev
->td
[idx
]);
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 */
640 /* record the device that its on */
641 new_td
->usb_dev
= ohci_to_usb(dev
);
646 printk(KERN_ERR
"usb-ohci: unable to allocate a TD\n");
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
659 static struct ohci_ed
*ohci_get_free_ed(struct ohci_device
*dev
)
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 */
677 printk(KERN_ERR
"usb-ohci: unable to allocate an ED\n");
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
)
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
));
698 next_td
= bus_to_virt(le32_to_cpup(&td
->next_td
));
706 ed
->status
&= cpu_to_le32(~(__u32
)ED_ALLOCATED
);
707 } /* ohci_free_ed() */
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
|
721 (toggle
& OHCI_TD_DT
) |
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);
730 td
->completed
= completed
;
733 printk(KERN_DEBUG
"ohci_fill_new_td created:\n");
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
,
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
);
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
));
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
) |
778 ((isoc_tds
== 0) ? OHCI_ED_F_NORM
: OHCI_ED_F_ISOC
));
783 } /* ohci_fill_ed() */
787 * Create a chain of Normal TDs to be used for a large data transfer
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))
801 /* Setup the first TD, leaving buf_end = 0 */
802 head
= ohci_get_free_td(dev
);
804 printk(KERN_ERR
"usb-ohci: out of TDs\n");
808 ohci_fill_new_td(head
,
811 (round
? OHCI_TD_ROUND
: 0),
815 noauto_free_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. */
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
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 */
859 ohci_fill_new_td(new_td
,
861 TOGGLE_AUTO
, /* toggle Data0/1 via the ED */
862 round
? OHCI_TD_ROUND
: 0,
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
);
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
);
882 /* link the given next_td to the end of this chain */
883 cur_td
->next_td
= next_td
;
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
894 static __u16
ohci_td_bytes_done(struct ohci_td
*td
)
897 __u32 bus_data_start
, bus_data_end
;
899 bus_data_start
= virt_to_bus(td
->data
);
900 if (!td
->data
|| !bus_data_start
)
903 /* if cur_buf is 0, all data has been transferred */
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
;
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);
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
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
);
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
);
952 printk(KERN_ERR
"Out of EDs on device %p in ohci_request_irq\n", dev
);
956 td
= ohci_get_free_td(dev
);
958 printk(KERN_ERR
"Out of TDs in ohci_request_irq\n");
959 ohci_free_ed(interrupt_ed
);
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 */);
971 if (maxps
> sizeof(dev
->data
))
972 maxps
= sizeof(dev
->data
);
973 ohci_fill_new_td(td
, td_set_dir_out(usb_pipeout(pipe
)),
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. */
990 /* Assimilate the new ED into the collective */
991 ohci_add_periodic_ed(dev
->ohci
, interrupt_ed
, period
);
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
;
1012 printk("usb-ohci: Releasing irq handle %p\n", handle
);
1015 int_td
= (struct ohci_td
*)handle
;
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
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 */
1053 int *completion_status
= (int *)dev_id
;
1054 *completion_status
= stats
;
1057 wake_up(&control_wakeup
);
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
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;
1085 /* byte-swap fields of cmd if necessary */
1087 cpu_to_le16s(&our_cmd
.value
);
1088 cpu_to_le16s(&our_cmd
.index
);
1089 cpu_to_le16s(&our_cmd
.length
);
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
);
1096 printk(KERN_ERR
"usb-ohci: couldn't get ED for dev %p\n", dev
);
1100 /* get a TD to send this control message with */
1101 setup_td
= ohci_get_free_td(dev
);
1103 printk(KERN_ERR
"usb-ohci: couldn't get TD for dev %p [cntl setup]\n", dev
);
1104 ohci_free_ed(control_ed
);
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
,
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
);
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
);
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)),
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. */
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
) );
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
);
1169 /* link the to the data & status TDs */
1170 setup_td
->next_td
= virt_to_bus(data_td
);
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
);
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
);
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
);
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
);
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 */
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
;
1236 printk(KERN_DEBUG
"ctrl msg %x %x %x %x %x on pipe %x returned:",
1237 cmd
->requesttype
, cmd
->request
, cmd
->value
, cmd
->index
,
1239 for (i
= 0; i
< len
; ++i
) {
1241 printk("\n" KERN_DEBUG
);
1242 printk(" %x", q
[i
]);
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
;
1281 printk(KERN_DEBUG
"ohci_bulk_td_handler stats %x, buffer %p, len %d, req %p\n", stats
, buffer
, len
, req
);
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
;
1293 printk(KERN_DEBUG
"usb-ohci: bulk request %p ending\n", req
);
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
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
;
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
);
1335 bulk_ed
= ohci_get_free_ed(dev
);
1337 printk("usb-ohci: couldn't get ED for dev %p\n", dev
);
1341 /* allocate & fill in the TDs for this request */
1342 head_td
= ohci_build_td_chain(dev
, data
, len
, usb_pipeout(pipe
),
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 */);
1349 printk("usb-ohci: couldn't get TDs for dev %p\n", dev
);
1350 ohci_free_ed(bulk_ed
);
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
),
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
);
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
);
1385 /* Give the ED to the HC */
1386 ohci_add_bulk_ed(dev
->ohci
, 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
)
1398 printk("ohci_bulk_msg_completed %x, %p, %d, %p\n", stats
, buffer
, len
, dev_id
);
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
;
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
);
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
;
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
;
1451 printk(KERN_DEBUG
"ohci_bulk_msg timing out\n");
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
));
1468 show_ohci_td(partial_td
);
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 */
1491 printk(KERN_DEBUG
"ohci_bulk_msg done, status %x (bytes_transferred = %ld).\n", completion_status
, *bytes_transferred_p
);
1494 return completion_status
;
1495 } /* ohci_bulk_msg() */
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
;
1512 * Allocate the generic USB device
1514 usb_dev
= kmalloc(sizeof(*usb_dev
), GFP_KERNEL
);
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
);
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
;
1544 * Link the device to its parent (hub, etc..) if any.
1546 usb_dev
->parent
= parent
;
1549 usb_dev
->bus
= parent
->bus
;
1550 dev
->ohci
= usb_to_ohci(parent
)->ohci
;
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); */
1575 * functions for the generic USB driver
1577 struct usb_operations ohci_device_operations
= {
1579 ohci_usb_deallocate
,
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
1594 static int reset_hc(struct ohci
*ohci
)
1596 int timeout
= 10000; /* prevent an infinite loop */
1599 printk(KERN_INFO
"usb-ohci: resetting HC %p\n", ohci
);
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) {
1608 printk(KERN_ERR
"usb-ohci: USB HC reset timed out!\n");
1614 printk(KERN_DEBUG
"usb-ohci: HC %p reset.\n", ohci
);
1621 * Reset and start an OHCI controller. Returns >= 0 on success.
1623 static int start_hc(struct ohci
*ohci
)
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;
1633 printk(KERN_DEBUG
"entering start_hc %p\n", ohci
);
1636 if (reset_hc(ohci
) < 0)
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
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
1664 /* | OHCI_INTR_FNO */
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
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");
1696 * Reset a root hub port
1698 static void ohci_reset_port(struct ohci
*ohci
, unsigned int port
)
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.
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
]);
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
];
1743 printk(KERN_DEBUG
"ohci_connect_change on port %d\n", port
);
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
1762 writel(PORT_CSC
, portaddr
);
1764 printk(KERN_DEBUG
"ohci port %d disabled, nothing connected.\n", port
);
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
;
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
;
1805 int maxport
= readl(&ohci
->regs
->roothub
) & 0xff;
1806 __u32 rh_change_flags
= PORT_CSC
| PORT_PESC
; /* root hub status changes */
1809 printk(KERN_DEBUG
"entering ohci_check_configuration %p\n", ohci
);
1813 __u32
*portstatus_p
= ®s
->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
);
1824 printk(KERN_DEBUG
"leaving ohci_check_configuration %p\n", ohci
);
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
)
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
))
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
);
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
)
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;
1877 td_list
= (struct ohci_td
*) bus_to_virt(td_list_hc
);
1878 td_list
->next_dl_td
= td_rev
;
1880 td_list_hc
= le32_to_cpup(&td_list
->next_td
) & 0xfffffff0;
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
));
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
;
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.. */
1931 if (td_endofchain(*td
))
1934 /* FIXME: unlink this TD from the
1935 * reverse donelist! */
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
));
1945 /* If we didn't find an endofchain TD, give up */
1946 if (td
== tail_td
) {
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
);
1971 /* return it to the pool of free TDs */
1972 if (can_auto_free(*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(®s
->intrstatus
);
1997 status
&= readl(®s
->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 */
2008 /* TODO increment a useless interrupt counter here */
2013 /* Disable HC interrupts */ /* why? - paulus */
2014 writel(OHCI_INTR_MIE
, ®s
->intrdisable
);
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
);
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
, ®s
->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
, ®s
->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.
2054 /* Start of Frame interrupts, used during safe ED removal */
2055 if (context
& (OHCI_INTR_SF
)) {
2056 writel(OHCI_INTR_SF
, ®s
->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
2064 /* Check those "other" pesky bits */
2065 if (context
& (OHCI_INTR_FNO
)) {
2066 writel(OHCI_INTR_FNO
, ®s
->intrstatus
);
2067 context
&= ~OHCI_INTR_FNO
; /* mark this as checked */
2069 if (context
& OHCI_INTR_SO
) {
2070 writel(OHCI_INTR_SO
, ®s
->intrstatus
);
2071 context
&= ~OHCI_INTR_SO
; /* mark this as checked */
2073 if (context
& OHCI_INTR_RD
) {
2074 writel(OHCI_INTR_RD
, ®s
->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
, ®s
->intrstatus
);
2083 context
&= ~OHCI_INTR_UE
; /* mark this as checked */
2085 if (context
& OHCI_INTR_OC
) {
2086 writel(OHCI_INTR_OC
, ®s
->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
, ®s
->intrdisable
);
2096 /* Re-enable HC interrupts */
2097 writel(OHCI_INTR_MIE
, ®s
->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
)
2114 struct usb_bus
*bus
;
2115 struct ohci_device
*dev
;
2116 struct usb_device
*usb
;
2119 printk(KERN_DEBUG
"entering alloc_ohci %p\n", mem_base
);
2122 ohci
= kmalloc(sizeof(*ohci
), GFP_KERNEL
);
2126 memset(ohci
, 0, sizeof(*ohci
));
2129 ohci
->regs
= mem_base
;
2130 INIT_LIST_HEAD(&ohci
->interrupt_list
);
2132 bus
= kmalloc(sizeof(*bus
), GFP_KERNEL
);
2136 memset(bus
, 0, sizeof(*bus
));
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
);
2154 dev
= usb_to_ohci(usb
);
2155 ohci
->bus
->root_hub
= ohci_to_usb(dev
);
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
);
2173 printk(KERN_DEBUG
"usb-ohci: HCCA allocated at %p (bus %p)\n", dev
->hcca
, (void*)virt_to_bus(dev
->hcca
));
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");
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
++) {
2207 dev
->hcca
->int_table
[i
] =
2208 cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_32
]));
2210 dev
->hcca
->int_table
[i
] =
2211 cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_16
]));
2213 dev
->hcca
->int_table
[i
] =
2214 cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_8
]));
2216 dev
->hcca
->int_table
[i
] =
2217 cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_4
]));
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
);
2231 printk(KERN_DEBUG
"alloc_ohci(): controller\n");
2232 show_ohci_status(ohci
);
2236 printk(KERN_DEBUG
"leaving alloc_ohci %p\n", 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
);
2251 /* stop our timer */
2252 del_timer(&ohci_timer
);
2254 if (ohci
->irq
>= 0) {
2255 free_irq(ohci
->irq
, ohci
);
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
);
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
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
);
2304 exit_files(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");
2317 usb_deregister_bus(ohci
->bus
);
2318 printk(KERN_INFO
"leaving ohci_control_thread %p\n", __ohci
);
2324 int unsigned long signr
;
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
);
2336 printk(KERN_DEBUG
"ohci-control thread sleeping\n");
2337 interruptible_sleep_on(&ohci_configure
);
2341 if (start_hc(ohci
) < 0)
2348 * If we were woken up by a signal, see if its useful,
2351 if (signal_pending(current
)) {
2352 /* sending SIGUSR1 makes us print out some info */
2353 spin_lock_irq(¤t
->sigmask_lock
);
2354 signr
= dequeue_signal(¤t
->blocked
, &info
);
2355 spin_unlock_irq(¤t
->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 */
2364 MegaDebug
= !MegaDebug
;
2365 printk(KERN_DEBUG
"usb-ohci: Mega debugging %sabled.\n",
2366 MegaDebug
? "en" : "dis");
2369 /* unknown signal, exit the thread */
2377 usb_deregister_bus(ohci
->bus
);
2378 printk(KERN_DEBUG
"ohci-control thread for 0x%p exiting\n", __ohci
);
2381 } /* ohci_control_thread() */
2385 static int handle_apm_event(apm_event_t event
)
2387 static int down
= 0;
2390 case APM_SYS_SUSPEND
:
2391 case APM_USER_SUSPEND
:
2393 printk(KERN_DEBUG
"usb-ohci: received extra suspend event\n");
2398 case APM_NORMAL_RESUME
:
2399 case APM_CRITICAL_RESUME
:
2401 printk(KERN_DEBUG
"usb-ohci: received bogus resume event\n");
2405 if (waitqueue_active(&ohci_configure
)) {
2407 wake_up(&ohci_configure
);
2412 } /* handle_apm_event() */
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
2423 * [my controller is a "SiS 7001 USB (rev 16)"]
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() */
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
)
2449 printk(KERN_DEBUG
"entering found_ohci %d %p\n", irq
, mem_base
);
2452 /* Allocate the running OHCI structures */
2453 ohci
= alloc_ohci(mem_base
);
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
);
2467 if (request_irq(irq
, ohci_interrupt
, SA_SHIRQ
, "usb-ohci", ohci
) == 0) {
2473 printk(KERN_DEBUG
"usb-ohci: forking ohci-control thread for 0x%p\n", ohci
);
2476 /* fork off the handler */
2477 pid
= kernel_thread(ohci_control_thread
, ohci
,
2478 CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
);
2485 printk(KERN_ERR
"usb-ohci: Couldn't allocate interrupt %d\n", irq
);
2490 printk(KERN_DEBUG
"leaving found_ohci %d %p\n", irq
, mem_base
);
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
)
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");
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);
2526 printk(KERN_ERR
"Error mapping OHCI memory\n");
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");
2536 if (found_ohci(dev
->irq
, (void *) mem_base
) < 0) {
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.
2556 struct pci_dev
*dev
= NULL
;
2559 if (sizeof(struct ohci_device
) > 4096) {
2560 printk(KERN_ERR
"usb-ohci: struct ohci_device to large\n");
2564 printk(KERN_INFO
"OHCI USB Driver loading\n");
2568 /* Find an OHCI USB controller */
2569 dev
= pci_find_class(PCI_CLASS_SERIAL_USB_OHCI
, dev
);
2573 /* Verify that its OpenHCI by checking for MMIO */
2574 /* pci_read_config_byte(dev, PCI_CLASS_PROG, &type);
2579 retval
= init_ohci(dev
);
2584 apm_register_callback(&handle_apm_event
);
2587 return 0; /* no error */
2595 * Clean up when unloading the module
2597 void cleanup_module(void){
2599 apm_unregister_callback(&handle_apm_event
);
2601 printk(KERN_ERR
"usb-ohci: module unloaded\n");
2604 int init_module(void){