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 details 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.80 1999/09/30 06:32:17 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>
40 #include <linux/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_tds_to_ed(struct ohci_td
*td
,
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 t
->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_tds_to_ed() */
155 void ohci_start_control(struct ohci
*ohci
)
157 /* tell the HC to start processing the control list */
158 writel_set(OHCI_USB_CLE
, &ohci
->regs
->control
);
159 writel_set(OHCI_CMDSTAT_CLF
, &ohci
->regs
->cmdstatus
);
162 void ohci_start_bulk(struct ohci
*ohci
)
164 /* tell the HC to start processing the bulk list */
165 writel_set(OHCI_USB_BLE
, &ohci
->regs
->control
);
166 writel_set(OHCI_CMDSTAT_BLF
, &ohci
->regs
->cmdstatus
);
169 void ohci_start_periodic(struct ohci
*ohci
)
171 /* enable processing periodic (intr) transfers starting next frame */
172 writel_set(OHCI_USB_PLE
, &ohci
->regs
->control
);
175 void ohci_start_isoc(struct ohci
*ohci
)
177 /* enable processing isoc. transfers starting next frame */
178 writel_set(OHCI_USB_IE
, &ohci
->regs
->control
);
182 * Add an ED to the hardware register ED list pointed to by hw_listhead_p
183 * This function only makes sense for Control and Bulk EDs.
185 static void ohci_add_ed_to_hw(struct ohci_ed
*ed
, void* hw_listhead_p
)
190 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
192 listhead
= readl(hw_listhead_p
);
194 /* if the list is not empty, insert this ED at the front */
195 /* XXX should they go on the end? */
196 ed
->next_ed
= cpu_to_le32(listhead
);
198 /* update the hardware listhead pointer */
199 writel(virt_to_bus(ed
), hw_listhead_p
);
201 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
202 } /* ohci_add_ed_to_hw() */
205 * Put a control ED on the controller's list
207 void ohci_add_control_ed(struct ohci
*ohci
, struct ohci_ed
*ed
)
209 ohci_add_ed_to_hw(ed
, &ohci
->regs
->ed_controlhead
);
210 ohci_start_control(ohci
);
211 } /* ohci_add_control_ed() */
214 * Put a bulk ED on the controller's list
216 void ohci_add_bulk_ed(struct ohci
*ohci
, struct ohci_ed
*ed
)
218 ohci_add_ed_to_hw(ed
, &ohci
->regs
->ed_bulkhead
);
219 ohci_start_bulk(ohci
);
220 } /* ohci_add_bulk_ed() */
223 * Put a periodic ED on the appropriate list given the period.
225 void ohci_add_periodic_ed(struct ohci
*ohci
, struct ohci_ed
*ed
, int period
)
227 struct ohci_ed
*int_ed
;
228 struct ohci_device
*root_hub
=usb_to_ohci(ohci
->bus
->root_hub
);
232 * Pick a good frequency endpoint based on the requested period
234 int_ed
= &root_hub
->ed
[ms_to_ed_int(period
)];
237 printk(KERN_DEBUG
"usb-ohci: Using INT ED queue %d for %dms period\n",
238 ms_to_ed_int(period
), period
);
241 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
243 * Insert this ED at the front of the list.
245 ed
->next_ed
= int_ed
->next_ed
;
246 int_ed
->next_ed
= cpu_to_le32(virt_to_bus(ed
));
248 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
250 ohci_start_periodic(ohci
);
251 } /* ohci_add_periodic_ed() */
254 * Locate the periodic ED for a given interrupt endpoint.
256 struct ohci_ed
*ohci_get_periodic_ed(struct ohci_device
*dev
, int period
,
257 unsigned int pipe
, int isoc
)
259 struct ohci_device
*root_hub
= usb_to_ohci(dev
->ohci
->bus
->root_hub
);
261 struct ohci_ed
*int_ed
;
262 unsigned int status
, req_status
;
264 /* get the dummy ED before the EDs for this period */
265 int_ed
= &root_hub
->ed
[ms_to_ed_int(period
)];
267 /* decide on what the status field should look like */
268 req_status
= ed_set_maxpacket(usb_maxpacket(ohci_to_usb(dev
), pipe
, usb_pipeout(pipe
)))
269 | ed_set_speed(usb_pipeslow(pipe
))
270 | (usb_pipe_endpdev(pipe
) & 0x7ff)
271 | ed_set_type_isoc(isoc
);
273 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
275 int_ed
= bus_to_virt(le32_to_cpup(&int_ed
->next_ed
));
276 /* stop if we get to the end or to another dummy ED. */
278 break; /* return NULL */
279 status
= le32_to_cpup(&int_ed
->status
);
280 if ((status
& OHCI_ED_FA
) == 0) {
282 break; /* return NULL */
284 /* check whether all the appropriate fields match */
285 if ((status
& 0x7ffa7ff) == req_status
)
286 break; /* return int_ed */
288 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
294 * This will be used for the interrupt to wake us up on the next SOF
296 DECLARE_WAIT_QUEUE_HEAD(start_of_frame_wakeup
);
298 static void ohci_wait_sof(struct ohci_regs
*regs
)
300 DECLARE_WAITQUEUE(wait
, current
);
302 add_wait_queue(&start_of_frame_wakeup
, &wait
);
304 /* clear the SOF interrupt status and enable it */
305 writel(OHCI_INTR_SF
, ®s
->intrstatus
);
306 writel(OHCI_INTR_SF
, ®s
->intrenable
);
308 schedule_timeout(HZ
/10);
310 remove_wait_queue(&start_of_frame_wakeup
, &wait
);
314 * Guarantee that an ED is safe to be modified by the HCD (us).
316 * This function can NOT be called from an interrupt.
318 * TODO: If we're waiting for the ED to be safe so that it can be
319 * destroyed, a similar "ohci_schedule_ed_free" function that just
320 * adds it to a list of EDs to destroy during the SOF interrupt
321 * processing would be useful (and be callable from an interrupt).
323 void ohci_wait_for_ed_safe(struct ohci_regs
*regs
, struct ohci_ed
*ed
)
325 __u32
*hw_listcurrent
;
327 /* tell the controller to skip this ED */
328 ed
->status
|= cpu_to_le32(OHCI_ED_SKIP
);
330 switch (ohci_ed_hcdtype(ed
)) {
332 hw_listcurrent
= ®s
->ed_controlcurrent
;
335 hw_listcurrent
= ®s
->ed_bulkcurrent
;
339 hw_listcurrent
= ®s
->ed_periodcurrent
;
346 * If the HC is processing this ED we need to wait until the
347 * at least the next frame.
349 if (virt_to_bus(ed
) == readl(hw_listcurrent
)) {
351 printk(KERN_INFO
"Waiting a frame for OHC to finish with ED %p [%x %x %x %x]\n", ed
, FIELDS_OF_ED(ed
));
357 return; /* The ED is now safe */
358 } /* ohci_wait_for_ed_safe() */
362 * Remove an ED from the HC's list.
363 * This function can ONLY be used for Control or Bulk EDs.
365 * Note that the SKIP bit is left on in the removed ED.
367 void ohci_remove_norm_ed_from_hw(struct ohci
*ohci
, struct ohci_ed
*ed
)
370 struct ohci_regs
*regs
= ohci
->regs
;
372 __u32 bus_ed
= virt_to_bus(ed
);
374 __u32
*hw_listhead_p
;
375 __u32 ed_type
= ohci_ed_hcdtype(ed
);
377 if (ed
== NULL
|| !bus_ed
)
379 ed
->status
|= cpu_to_le32(OHCI_ED_SKIP
);
383 hw_listhead_p
= ®s
->ed_controlhead
;
386 hw_listhead_p
= ®s
->ed_bulkhead
;
390 printk(KERN_ERR
"Wrong HCD ED type: %d.\n", ed_type
);
395 bus_cur
= readl(hw_listhead_p
);
398 return; /* the list is already empty */
400 cur
= bus_to_virt(bus_cur
);
402 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
404 /* if its the head ED, move the head */
405 if (bus_cur
== bus_ed
) {
406 writel(le32_to_cpup(&cur
->next_ed
), hw_listhead_p
);
407 } else if (cur
->next_ed
!= 0) {
408 struct ohci_ed
*prev
;
410 /* walk the list and unlink the ED if found */
413 cur
= bus_to_virt(le32_to_cpup(&cur
->next_ed
));
416 /* unlink from the list */
417 prev
->next_ed
= cur
->next_ed
;
420 } while (cur
->next_ed
!= 0);
424 * Make sure this ED is not being accessed by the HC as we speak.
426 ohci_wait_for_ed_safe(regs
, ed
);
428 /* clear any links from the ED for safety */
431 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
432 } /* ohci_remove_norm_ed_from_hw() */
436 * Remove a periodic ED from the host controller
438 void ohci_remove_periodic_ed(struct ohci
*ohci
, struct ohci_ed
*ed
)
440 struct ohci_device
*root_hub
= usb_to_ohci(ohci
->bus
->root_hub
);
441 struct ohci_ed
*cur_ed
= NULL
, *prev_ed
;
444 /* FIXME: this will need to up fixed when add_periodic_ed()
445 * is updated to spread similar polling rate EDs out over
446 * multiple periodic queues. Currently this assumes that the
447 * 32ms (slowest) polling queue links to all others... */
449 /* search the periodic EDs, skipping the first one which is
450 * only a placeholder. */
451 prev_ed
= &root_hub
->ed
[ED_INT_32
];
452 if (prev_ed
->next_ed
)
453 cur_ed
= bus_to_virt(le32_to_cpup(&prev_ed
->next_ed
));
456 if (ed
== cur_ed
) { /* remove the ED */
457 /* set its SKIP bit and be sure its not in use */
458 ohci_wait_for_ed_safe(ohci
->regs
, ed
);
461 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
462 prev_ed
->next_ed
= ed
->next_ed
;
463 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
469 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
470 if (cur_ed
->next_ed
) {
472 cur_ed
= bus_to_virt(le32_to_cpup(&cur_ed
->next_ed
));
473 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
475 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
477 /* if multiple polling queues need to be checked,
478 * here is where you'd advance to the next one */
480 printk("usb-ohci: ed %p not found on periodic queue\n", ed
);
484 } /* ohci_remove_periodic_ed() */
488 * Remove an ED from the host controller, choosing
490 void ohci_remove_ed(struct ohci
*ohci
, struct ohci_ed
*ed
)
492 /* Remove the ED from the HC */
493 if (ohci_ed_hcdtype(ed
) & HCD_ED_NORMAL
) {
494 ohci_remove_norm_ed_from_hw(ohci
, ed
);
496 ohci_remove_periodic_ed(ohci
, ed
);
498 } /* ohci_remove_ed() */
502 * Remove all the EDs which have a given device address from a list.
503 * Used when the device is unplugged.
504 * Returns 1 if anything was changed.
506 static int ohci_remove_device_list(__u32
*headp
, int devnum
)
509 __u32
*prevp
= headp
;
512 while (*prevp
!= 0) {
513 ed
= bus_to_virt(le32_to_cpup(prevp
));
514 if ((le32_to_cpup(&ed
->status
) & OHCI_ED_FA
) == devnum
) {
515 /* set the controller to skip this one
516 and remove it from the list */
517 ed
->status
|= cpu_to_le32(OHCI_ED_SKIP
);
518 /* XXX should call td->completed for each td */
519 *prevp
= ed
->next_ed
;
522 prevp
= &ed
->next_ed
;
531 * Remove all the EDs for a given device from all lists.
533 void ohci_remove_device(struct ohci
*ohci
, int devnum
)
537 struct ohci_regs
*regs
= ohci
->regs
;
538 struct ohci_device
*root_hub
=usb_to_ohci(ohci
->bus
->root_hub
);
540 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
543 head
= cpu_to_le32(readl(®s
->ed_controlhead
));
544 if (ohci_remove_device_list(&head
, devnum
))
545 writel(le32_to_cpup(&head
), ®s
->ed_controlhead
);
548 head
= cpu_to_le32(readl(®s
->ed_bulkhead
));
549 if (ohci_remove_device_list(&head
, devnum
))
550 writel(le32_to_cpup(&head
), ®s
->ed_bulkhead
);
552 /* Interrupt/iso list */
553 head
= cpu_to_le32(virt_to_bus(&root_hub
->ed
[ED_INT_32
]));
554 ohci_remove_device_list(&head
, devnum
);
557 * Wait until the start of the next frame to ensure
558 * that the HC has seen any changes.
560 ohci_wait_sof(ohci
->regs
);
562 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
566 * Remove a TD from the given EDs TD list. The TD is freed as well.
567 * (so far this function hasn't been needed)
569 void ohci_remove_td_from_ed(struct ohci_td
*td
, struct ohci_ed
*ed
)
572 struct ohci_td
*head_td
;
574 if ((td
== NULL
) || (ed
== NULL
))
577 if (ed_head_td(ed
) == 0)
580 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
582 /* set the "skip me bit" in this ED */
583 ed
->status
|= cpu_to_le32(OHCI_ED_SKIP
);
585 /* XXX Assuming this list will never be circular */
587 head_td
= bus_to_virt(ed_head_td(ed
));
588 if (virt_to_bus(td
) == ed_head_td(ed
)) {
589 /* It's the first TD, remove it. */
590 set_ed_head_td(ed
, head_td
->next_td
);
592 struct ohci_td
*prev_td
, *cur_td
;
594 /* FIXME: collapse this into a nice simple loop :) */
595 if (head_td
->next_td
!= 0) {
597 cur_td
= bus_to_virt(le32_to_cpup(&head_td
->next_td
));
601 prev_td
->next_td
= cur_td
->next_td
;
604 if (cur_td
->next_td
== 0)
607 cur_td
= bus_to_virt(le32_to_cpup(&cur_td
->next_td
));
612 td
->next_td
= 0; /* remove the TDs links */
615 /* return this TD to the pool of free TDs */
618 /* unset the "skip me bit" in this ED */
619 ed
->status
&= cpu_to_le32(~OHCI_ED_SKIP
);
621 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
622 } /* ohci_remove_td_from_ed() */
626 * Get a pointer (virtual) to an available TD from the given device's
627 * pool. Return NULL if none are left.
629 static struct ohci_td
*ohci_get_free_td(struct ohci_device
*dev
)
634 printk(KERN_DEBUG
"in ohci_get_free_td()\n");
637 /* FIXME: this is horribly inefficient */
638 for (idx
=0; idx
< NUM_TDS
; idx
++) {
640 show_ohci_td(&dev
->td
[idx
]);
642 if (!td_allocated(dev
->td
[idx
])) {
643 struct ohci_td
*new_td
= &dev
->td
[idx
];
644 /* zero out the TD */
645 memset(new_td
, 0, sizeof(*new_td
));
646 /* mark the new TDs as unaccessed */
647 new_td
->info
= cpu_to_le32(OHCI_TD_CC_NEW
);
648 /* mark it as allocated */
650 /* record the device that its on */
651 new_td
->usb_dev
= ohci_to_usb(dev
);
656 printk(KERN_ERR
"usb-ohci: unable to allocate a TD\n");
658 } /* ohci_get_free_td() */
662 * Get a pointer (virtual) to an available TD from the given device's
663 * pool. Return NULL if none are left.
665 * NOTE: This function does not allocate and attach the dummy_td.
666 * That is done in ohci_fill_ed(). [should we really do that here?]
668 static struct ohci_ed
*ohci_get_free_ed(struct ohci_device
*dev
, __u32 ed_type
)
672 /* FIXME: this is horribly inefficient */
673 for (idx
=0; idx
< NUM_EDS
; idx
++) {
674 if (!ed_allocated(dev
->ed
[idx
])) {
675 struct ohci_ed
*new_ed
= &dev
->ed
[idx
];
676 /* zero out the ED */
677 memset(new_ed
, 0, sizeof(*new_ed
));
678 /* all new EDs start with the SKIP bit set */
679 new_ed
->status
|= cpu_to_le32(OHCI_ED_SKIP
);
680 /* mark it as allocated */
682 ohci_ed_set_hcdtype(new_ed
, ed_type
);
683 new_ed
->ohci_dev
= dev
;
688 printk(KERN_ERR
"usb-ohci: unable to allocate an ED\n");
690 } /* ohci_get_free_ed() */
694 * Free an OHCI ED and all of the TDs on its list. It is assumed that
695 * this ED is not active. You should call ohci_wait_for_ed_safe()
696 * beforehand if you can't guarantee that.
698 void ohci_free_ed(struct ohci_ed
*ed
)
703 if (ed_head_td(ed
) != 0) {
704 struct ohci_td
*td
, *tail_td
, *next_td
;
706 td
= bus_to_virt(ed_head_td(ed
));
707 tail_td
= bus_to_virt(ed_tail_td(ed
));
709 next_td
= bus_to_virt(le32_to_cpup(&td
->next_td
));
717 ed
->status
&= cpu_to_le32(~(__u32
)ED_ALLOCATED
);
718 } /* ohci_free_ed() */
724 * dir = OHCI_TD_D_IN, OHCI_TD_D_OUT, or OHCI_TD_D_SETUP
725 * toggle = TOGGLE_AUTO, TOGGLE_DATA0, TOGGLE_DATA1
727 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
)
729 /* hardware fields */
730 td
->info
= cpu_to_le32(OHCI_TD_CC_NEW
|
732 (toggle
& OHCI_TD_DT
) |
734 td
->cur_buf
= (data
== NULL
) ? 0 : cpu_to_le32(virt_to_bus(data
));
735 td
->buf_end
= (len
== 0) ? 0 :
736 cpu_to_le32(virt_to_bus((char *)data
+ len
- 1));
741 td
->completed
= completed
;
744 printk(KERN_DEBUG
"ohci_fill_new_td created:\n");
749 } /* ohci_fill_new_td() */
753 * Initialize a new ED on device dev, including allocating and putting the
754 * dummy tail_td on its queue if it doesn't already have one. Any
755 * TDs on this ED other than the dummy will be lost (so there better
756 * not be any!). This assumes that the ED is Allocated and will
757 * force the Allocated bit on.
759 struct ohci_ed
*ohci_fill_ed(struct ohci_device
*dev
, struct ohci_ed
*ed
,
760 int maxpacketsize
, int lowspeed
, int endp_id
)
762 struct ohci_td
*dummy_td
;
764 if (ed_head_td(ed
) != ed_tail_td(ed
))
765 printk(KERN_ERR
"Reusing a non-empty ED %p!\n", ed
);
768 dummy_td
= ohci_get_free_td(dev
);
769 if (dummy_td
== NULL
) {
770 printk(KERN_ERR
"Error allocating dummy TD for ED %p\n", ed
);
771 return NULL
; /* no dummy available! */
773 make_dumb_td(dummy_td
); /* flag it as a dummy */
774 ed
->tail_td
= cpu_to_le32(virt_to_bus(dummy_td
));
776 dummy_td
= bus_to_virt(ed_tail_td(ed
));
777 if (!td_dummy(*dummy_td
))
778 printk(KERN_ERR
"ED %p's dummy %p is screwy\n", ed
, dummy_td
);
781 /* set the head TD to the dummy and clear the Carry & Halted bits */
782 ed
->_head_td
= ed
->tail_td
;
784 ed
->status
&= cpu_to_le32(OHCI_ED_HCD_MASK
);
785 ed
->status
|= cpu_to_le32(
786 ed_set_maxpacket(maxpacketsize
) |
787 ed_set_speed(lowspeed
) |
789 ((ohci_ed_hcdtype(ed
) != HCD_ED_ISOC
) ? OHCI_ED_F_NORM
: OHCI_ED_F_ISOC
));
794 } /* ohci_fill_ed() */
798 * Create a chain of Normal TDs to be used for a large data transfer
801 * The next_td parameter should be OHCI byte order bus address of the
802 * next TD to follow this chain or 0 if there is none.
804 * Returns the head TD in the chain.
806 struct ohci_td
*ohci_build_td_chain(struct ohci_device
*dev
,
807 void *data
, unsigned int len
, int dir
, __u32 toggle
,
808 int round
, int auto_free
, void* dev_id
,
809 usb_device_irq handler
, __u32 next_td
)
811 struct ohci_td
*head
, *cur_td
;
814 if (!data
|| (len
== 0))
817 /* Get the first TD */
818 head
= ohci_get_free_td(dev
);
820 printk(KERN_ERR
"usb-ohci: out of TDs\n");
826 /* AFICT, that the OHCI controller takes care of the innards of
827 * bulk & control data transfers by sending zero length
828 * packets as necessary if the transfer falls on an even packet
829 * size boundary, we don't need a special TD for that. */
831 /* check the 4096 byte alignment of the start of the data */
832 max_len
= 0x2000 - ((unsigned long)data
& 0xfff);
834 /* check if the remaining data occupies more than two pages */
835 while (len
> max_len
) {
836 struct ohci_td
*new_td
;
838 /* TODO lookup effect of rounding bit on
839 * individual TDs vs. whole TD chain transfers;
840 * disable cur_td's rounding bit here if needed. */
842 ohci_fill_new_td(cur_td
,
845 (round
? OHCI_TD_ROUND
: 0),
849 noauto_free_td(head
);
851 /* adjust the data pointer & remaining length */
855 /* allocate another td */
856 new_td
= ohci_get_free_td(dev
);
857 if (new_td
== NULL
) {
858 printk(KERN_ERR
"usb-ohci: out of TDs\n");
859 /* FIXME: free any allocated TDs */
863 /* Link the new TD to the chain & advance */
864 cur_td
->next_td
= cpu_to_le32(virt_to_bus(new_td
));
867 /* address is page-aligned now */
869 toggle
= TOGGLE_AUTO
; /* toggle Data0/1 via the ED */
872 ohci_fill_new_td(cur_td
,
875 (round
? OHCI_TD_ROUND
: 0),
879 noauto_free_td(head
);
881 /* link the given next_td to the end of this chain */
882 cur_td
->next_td
= next_td
;
884 set_td_endofchain(cur_td
);
887 } /* ohci_build_td_chain() */
891 * Compute the number of bytes that have been transferred on a given
892 * TD. Do not call this on TDs that are active on the host
895 static __u16
ohci_td_bytes_done(struct ohci_td
*td
)
898 __u32 bus_data_start
, bus_data_end
;
900 bus_data_start
= virt_to_bus(td
->data
);
901 if (!td
->data
|| !bus_data_start
)
904 /* if cur_buf is 0, all data has been transferred */
906 return le32_to_cpup(&td
->buf_end
) - bus_data_start
+ 1;
909 bus_data_end
= le32_to_cpup(&td
->cur_buf
);
911 /* is it on the same page? */
912 if ((bus_data_start
& ~0xfff) == (bus_data_end
& ~0xfff)) {
913 result
= bus_data_end
- bus_data_start
;
915 /* compute the amount transferred on the first page */
916 result
= 0x1000 - (bus_data_start
& 0xfff);
917 /* add the amount done in the second page */
918 result
+= (bus_data_end
& 0xfff);
922 } /* ohci_td_bytes_done() */
925 /**********************************
926 * OHCI interrupt list operations *
927 **********************************/
930 * Request an interrupt handler for one "pipe" of a USB device.
931 * (this function is pretty minimal right now)
933 * At the moment this is only good for input interrupts. (ie: for a
936 * Period is desired polling interval in ms. The closest, shorter
937 * match will be used. Powers of two from 1-32 are supported by OHCI.
939 * Returns: success (0) or failure (< 0).
940 * Also sets the "handle pointer" that release_irq can use to stop this
941 * interrupt. (It's really a pointer to the TD).
943 static int ohci_request_irq(struct usb_device
*usb
, unsigned int pipe
,
944 usb_device_irq handler
, int period
, void *dev_id
, void **handle
, long bustime
)
946 struct ohci_device
*dev
= usb_to_ohci(usb
);
948 struct ohci_ed
*interrupt_ed
; /* endpoint descriptor for this irq */
949 int maxps
= usb_maxpacket(usb
, pipe
, usb_pipeout(pipe
));
952 /* Get an ED and TD */
953 interrupt_ed
= ohci_get_periodic_ed(dev
, period
, pipe
, 0);
954 if (interrupt_ed
== 0) {
955 interrupt_ed
= ohci_get_free_ed(dev
, HCD_ED_INT
);
957 printk(KERN_ERR
"Out of EDs on device %p in ohci_request_irq\n", dev
);
962 * Set the max packet size, device speed, endpoint number, usb
963 * device number (function address), and type of TD.
965 ohci_fill_ed(dev
, interrupt_ed
, maxps
, usb_pipeslow(pipe
),
966 usb_pipe_endpdev(pipe
) );
967 interrupt_ed
->status
&= cpu_to_le32(~OHCI_ED_SKIP
);
969 /* Assimilate the new ED into the collective */
970 ohci_add_periodic_ed(dev
->ohci
, interrupt_ed
, period
);
974 printk(KERN_DEBUG
"ohci_request irq: using ED %p [%x %x %x %x]\n",
975 interrupt_ed
, FIELDS_OF_ED(interrupt_ed
));
976 printk(KERN_DEBUG
" for dev %d pipe %x period %d\n", usb
->devnum
,
981 td
= ohci_get_free_td(dev
);
983 printk(KERN_ERR
"Out of TDs in ohci_request_irq\n");
984 ohci_free_ed(interrupt_ed
);
989 if (maxps
> sizeof(dev
->data
))
990 maxps
= sizeof(dev
->data
);
991 ohci_fill_new_td(td
, td_set_dir_out(usb_pipeout(pipe
)),
996 set_td_endofchain(td
);
999 * Put the TD onto our ED and make sure its ready to run
1002 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
1003 td
= ohci_add_tds_to_ed(td
, interrupt_ed
);
1004 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
1006 *handle
= (void*)td
;
1008 } /* ohci_request_irq() */
1011 * Release an interrupt handler previously allocated using
1012 * ohci_request_irq. This function does no validity checking, so make
1013 * sure you're not releasing an already released handle as it may be
1014 * in use by something else..
1016 * This function can NOT be called from an interrupt.
1018 int ohci_release_irq(struct usb_device
*usb
, void* handle
)
1020 struct ohci_device
*dev
;
1021 struct ohci_td
*int_td
;
1022 struct ohci_ed
*int_ed
;
1026 printk("usb-ohci: Releasing irq handle %p\n", handle
);
1029 int_td
= (struct ohci_td
*)handle
;
1031 return USB_ST_INTERNALERROR
;
1033 dev
= usb_to_ohci(int_td
->usb_dev
);
1034 int_ed
= int_td
->ed
;
1036 ohci_remove_periodic_ed(dev
->ohci
, int_ed
);
1038 /* Tell the driver that the IRQ has been killed. */
1039 /* Passing NULL in the "buffer" void* along with the
1040 * USB_ST_REMOVED status is the signal. */
1041 if (int_td
->completed
!= NULL
)
1042 int_td
->completed(USB_ST_REMOVED
, NULL
, 0, int_td
->dev_id
);
1044 /* Free the ED (& TD) */
1045 ohci_free_ed(int_ed
);
1047 return USB_ST_NOERROR
;
1048 } /* ohci_release_irq() */
1051 /**********************************************************************
1052 * Generic bulk/control/isochronous transfer processing
1053 **********************************************************************/
1057 * Queue a generic OHCI data transfer, specifying the type in ed_type.
1059 * - data_td_toggle specifies the data toggle value to start with.
1060 * - round specifies if the transfer should be allowed to fall short.
1061 * - autofree determines if the data TDs are automatically freed.
1062 * - setup_td and status_td will be prepended and appended to the TD
1063 * chain respectively. Control transfers need these..
1065 * - handler will be called upon completion of every data TD it
1066 * needs to check if the transfer is really done or not.
1068 * A handle to the transfer is returned.
1070 static void * ohci_generic_trans(struct usb_device
*usb_dev
, int pipe
,
1071 int data_td_toggle
, int round
, int autofree
,
1072 void *dev_id
, usb_device_irq handler
, void *data
, int len
,
1073 __u32 ed_type
, struct ohci_td
*setup_td
, struct ohci_td
*status_td
)
1075 struct ohci_device
*dev
= usb_to_ohci(usb_dev
);
1076 struct ohci_ed
*trans_ed
;
1077 struct ohci_td
*data_td
, *head_td
;
1078 unsigned long flags
;
1082 printk(KERN_DEBUG
"ohci_request_trans()\n");
1085 trans_ed
= ohci_get_free_ed(dev
, ed_type
);
1087 printk("usb-ohci: couldn't get ED for dev %p\n", dev
);
1091 /* If this transfer has a data phase, allocate TDs for it */
1093 __u32 next_td
= status_td
? cpu_to_le32(virt_to_bus(status_td
)) : 0;
1094 /* allocate & fill in the TDs for this request */
1095 data_td
= ohci_build_td_chain( dev
, data
, len
,
1104 printk(KERN_ERR
"usb-ohci: build_td_chain failed for dev %p, pipe 0x%x\n", dev
, pipe
);
1105 goto gt_free_and_exit
;
1108 data_td
= status_td
;
1111 /* start with the setup TD if there is one */
1114 setup_td
->next_td
= cpu_to_le32(virt_to_bus(data_td
));
1118 printk(KERN_ERR
"usb-ohci: lonely setup_td detected\n");
1120 goto gt_free_and_exit
;
1128 printk(KERN_ERR
"usb-ohci: no TDs in transfer\n");
1130 goto gt_free_and_exit
; /* nothing to do */
1133 /* Set the max packet size, device speed, endpoint number, usb
1134 * device number (function address), and type of TD. */
1135 ohci_fill_ed(dev
, trans_ed
,
1136 usb_maxpacket(usb_dev
, pipe
, usb_pipeout(pipe
)),
1138 usb_pipe_endpdev(pipe
) );
1140 /* initialize the toggle carry flag in the ED */
1141 if (usb_gettoggle(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
)))
1142 ohci_ed_set_carry(trans_ed
);
1145 * Add the TDs to the ED, remove the skip flag
1147 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
1148 head_td
= ohci_add_tds_to_ed(head_td
, trans_ed
);
1149 trans_ed
->status
&= cpu_to_le32(~OHCI_ED_SKIP
);
1150 /* ohci_unhalt_ed(trans_ed); */
1151 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
1155 /* complete transaction debugging output (before) */
1156 printk(KERN_DEBUG
" Trans ED %lx:\n", virt_to_bus(trans_ed
));
1157 show_ohci_ed(trans_ed
);
1158 printk(KERN_DEBUG
" Trans TD chain:\n");
1159 show_ohci_td_chain(head_td
);
1163 /* Give the ED to the HC on the appropriate list */
1166 /* Isochronous transfers have a 1ms period */
1167 ohci_add_periodic_ed(dev
->ohci
, trans_ed
, 1);
1169 case HCD_ED_CONTROL
:
1170 ohci_add_control_ed(dev
->ohci
, trans_ed
);
1173 ohci_add_bulk_ed(dev
->ohci
, trans_ed
);
1177 printk(KERN_ERR
"usb-ohci: bad ED type %d\n", ed_type
);
1179 goto gt_free_and_exit
;
1187 ohci_free_ed(trans_ed
);
1189 } /* ohci_generic_trans() */
1193 * Terminate a transfer initiated by ohci_generic_trans()
1195 * This function is NOT safe to call from an interrupt.
1197 static int ohci_terminate_trans(struct usb_device
*usb_dev
, void *handle
)
1199 struct ohci_ed
*req_ed
= (struct ohci_ed
*) handle
;
1200 struct ohci_device
*dev
= usb_to_ohci(usb_dev
);
1201 struct ohci_regs
*regs
= dev
->ohci
->regs
;
1206 /* stop the transfer & collect the number of bytes */
1207 /* (this is the non-interrupt safe function call) */
1208 ohci_wait_for_ed_safe(regs
, req_ed
);
1210 /* Remove the ED from the HC */
1211 ohci_remove_ed(dev
->ohci
, req_ed
);
1213 ohci_free_ed(req_ed
); /* return it to the pool */
1216 } /* ohci_terminate_trans() */
1219 /**********************************************************************
1220 * Control transfer processing
1221 **********************************************************************/
1224 static DECLARE_WAIT_QUEUE_HEAD(control_wakeup
);
1227 * This is the handler that gets called when a control transaction
1230 * This function is called from the interrupt handler.
1232 static int ohci_control_completed(int stats
, void *buffer
, int len
, void *dev_id
)
1234 /* pass the TDs completion status back to control_msg */
1236 int *completion_status
= (int *)dev_id
;
1239 case USB_ST_NOERROR
:
1240 case USB_ST_DATAOVERRUN
:
1241 *completion_status
= len
;
1244 *completion_status
= -stats
;
1248 wake_up(&control_wakeup
);
1250 } /* ohci_control_completed() */
1254 * Send or receive a control message on a "pipe"
1256 * The cmd parameter is a pointer to the 8 byte setup command to be
1259 * A control message contains:
1260 * - The command itself
1261 * - An optional data phase (if len > 0)
1262 * - Status complete phase
1264 * This function can NOT be called from an interrupt.
1266 static int ohci_control_msg(struct usb_device
*usb_dev
, unsigned int pipe
,
1267 devrequest
*cmd
, void *data
, int len
, int timeout
)
1269 struct ohci_device
*dev
= usb_to_ohci(usb_dev
);
1271 struct ohci_td
*setup_td
, *status_td
;
1272 DECLARE_WAITQUEUE(wait
, current
);
1273 int completion_status
= USB_ST_TIMEOUT
;
1276 /* byte-swap fields of cmd if necessary */
1278 cpu_to_le16s(&our_cmd
.value
);
1279 cpu_to_le16s(&our_cmd
.index
);
1280 cpu_to_le16s(&our_cmd
.length
);
1284 printk(KERN_DEBUG
"ohci_control_msg %p (ohci_dev: %p) pipe %x, cmd %p, data %p, len %d\n", usb_dev
, dev
, pipe
, cmd
, data
, len
);
1287 /* get a TD to send this control message with */
1288 setup_td
= ohci_get_free_td(dev
);
1290 printk(KERN_ERR
"usb-ohci: couldn't get TD for dev %p [cntl setup]\n", dev
);
1291 return USB_ST_INTERNALERROR
;
1295 * Set the not accessed condition code, allow odd sized data,
1296 * and set the data transfer type to SETUP. Setup DATA always
1297 * uses a DATA0 packet.
1299 * The setup packet contains a devrequest (usb.h) which
1300 * will always be 8 bytes long.
1302 ohci_fill_new_td(setup_td
, OHCI_TD_D_SETUP
, TOGGLE_DATA0
,
1304 &our_cmd
, 8, /* cmd is always 8 bytes long */
1305 &completion_status
, NULL
);
1307 /* Allocate a TD for the control xfer status */
1308 status_td
= ohci_get_free_td(dev
);
1310 printk("usb-ohci: couldn't get TD for dev %p [cntl status]\n", dev
);
1311 ohci_free_td(setup_td
);
1312 return USB_ST_INTERNALERROR
;
1315 /* The control status packet always uses a DATA1
1316 * Give "dev_id" the address of completion_status so that the
1317 * TDs status can be passed back to us from the IRQ. */
1318 ohci_fill_new_td(status_td
,
1319 td_set_dir_in(usb_pipeout(pipe
) | (len
== 0)),
1322 NULL
/* data */, 0 /* data len */,
1323 &completion_status
, ohci_control_completed
);
1324 set_td_endofchain(status_td
);
1325 status_td
->next_td
= 0; /* end of TDs */
1328 * Start the control transaction..
1329 * XXX should this come so soon? or... XXX
1330 * XXX should it be put into generic_trans? XXX
1332 current
->state
= TASK_UNINTERRUPTIBLE
;
1333 add_wait_queue(&control_wakeup
, &wait
);
1335 trans_handle
= ohci_generic_trans(usb_dev
, pipe
,
1336 TOGGLE_DATA1
, 1 /* round */, 1 /* autofree */,
1337 &completion_status
, NULL
/* no data td handler */,
1338 data
, len
, HCD_ED_CONTROL
,
1339 setup_td
, status_td
);
1341 /* did something go wrong? return an error */
1342 if (!trans_handle
) {
1343 remove_wait_queue(&control_wakeup
, &wait
);
1344 ohci_free_td(setup_td
);
1345 ohci_free_td(status_td
);
1346 return USB_ST_INTERNALERROR
;
1349 /* wait a user defined amount of time for it to complete */
1350 schedule_timeout(timeout
);
1352 remove_wait_queue(&control_wakeup
, &wait
);
1356 * NOTE! this debug code blatently assumes that the handle returned
1357 * by ohci_generic_trans is the pointer to this transfers ED.
1358 * (which it is at the moment)
1359 * Also, since the TDs were autofreed, there is no guarantee that
1360 * they haven't been reclaimed by another transfer by now...
1362 if (completion_status
< 0) {
1364 if (completion_status
== USB_ST_TIMEOUT
)
1367 what
= cc_names
[(-completion_status
) & 0xf];
1368 printk(KERN_ERR
"ohci_control_msg: %s on pipe %x cmd %x %x %x %x %x",
1369 what
, pipe
, cmd
->requesttype
, cmd
->request
,
1370 cmd
->value
, cmd
->index
, cmd
->length
);
1371 if (usb_pipeout(pipe
) && len
> 0) {
1374 for (i
= 0; i
< 16 && i
< len
; ++i
)
1375 printk(" %.2x", ((unsigned char *)data
)[i
]);
1380 if (MegaDebug
&& completion_status
< 0) {
1381 struct ohci_ed
*control_ed
= (struct ohci_ed
*) trans_handle
;
1382 printk(KERN_DEBUG
"control_ed at %p:\n", control_ed
);
1383 show_ohci_ed(control_ed
);
1384 if (ed_head_td(control_ed
) != ed_tail_td(control_ed
))
1385 show_ohci_td_chain(bus_to_virt(ed_head_td(control_ed
)));
1386 printk(KERN_DEBUG
"setup TD at %p:\n", setup_td
);
1387 show_ohci_td(setup_td
);
1389 } else if (!usb_pipeout(pipe
)) {
1390 unsigned char *q
= data
;
1392 printk(KERN_DEBUG
"ctrl msg %x %x %x %x %x on pipe %x returned:",
1393 cmd
->requesttype
, cmd
->request
, cmd
->value
, cmd
->index
,
1395 for (i
= 0; i
< len
; ++i
) {
1397 printk("\n" KERN_DEBUG
);
1398 printk(" %x", q
[i
]);
1404 struct ohci_ed
*control_ed
= (struct ohci_ed
*) trans_handle
;
1405 /* complete transaction debugging output (after) */
1406 printk(KERN_DEBUG
" *after* Control ED %lx:\n", virt_to_bus(control_ed
));
1407 show_ohci_ed(control_ed
);
1408 printk(KERN_DEBUG
" *after* Control TD chain:\n");
1409 show_ohci_td_chain(setup_td
);
1410 printk(KERN_DEBUG
" *after* OHCI Controller Status:\n");
1411 show_ohci_status(dev
->ohci
);
1415 /* no TD cleanup, the TDs were auto-freed as they finished */
1417 /* remove the generic_trans ED from the HC and free it */
1418 ohci_terminate_trans(usb_dev
, trans_handle
);
1420 return completion_status
;
1421 } /* ohci_control_msg() */
1424 /**********************************************************************
1425 * Bulk transfer processing
1426 **********************************************************************/
1430 * Request to send or receive bulk data. The handler() function
1431 * will be called as each OHCI TD in the transfer completes or is
1432 * aborted due to an error.
1434 * IMPORTANT NOTE: This means the handler may be called multiple
1435 * times for a large (more than one 4096 byte page) request until
1436 * the transfer is finished.
1438 * Returns: a pointer to the ED being used for this request as the
1439 * bulk request handle.
1441 static void * ohci_request_bulk(struct usb_device
*usb_dev
, unsigned int pipe
, usb_device_irq handler
, void* data
, int len
, void* dev_id
)
1444 struct ohci_device
*dev
= usb_to_ohci(usb_dev
);
1446 printk(KERN_DEBUG
"ohci_request_bulk() ohci_dev %p, handler %p, pipe %x, data %p, len %d, dev_id %p\n", dev
, handler
, pipe
, data
, len
, dev_id
);
1449 return ohci_generic_trans(usb_dev
, pipe
,
1451 0 /* round */, 1 /* autofree */,
1452 dev_id
, handler
, data
, len
,
1454 NULL
/* no setup_td */, NULL
/* no status_td */ );
1455 } /* ohci_request_bulk() */
1459 * Terminate a bulk transfer requested using ohci_request_bulk.
1461 * This function is NOT safe to call from an interrupt.
1463 static int ohci_terminate_bulk(struct usb_device
*usb_dev
, void * handle
)
1465 return ohci_terminate_trans(usb_dev
, handle
);
1466 } /* ohci_terminate_bulk() */
1470 * Internal state for an ohci_bulk_request_msg
1472 struct ohci_bulk_msg_request_state
{
1473 struct usb_device
*usb_dev
;
1474 unsigned int pipe
; /* usb "pipe" */
1475 void *data
; /* ptr to data */
1476 int length
; /* length to transfer */
1477 int _bytes_done
; /* bytes transferred so far */
1478 unsigned long *bytes_transferred_p
; /* where to increment */
1479 void *dev_id
; /* pass to the completion handler */
1480 usb_device_irq completion
; /* completion handler */
1484 * this handles the individual TDs of a (possibly) larger bulk
1485 * request. It keeps track of the total bytes transferred, calls the
1486 * final completion handler, etc.
1488 static int ohci_bulk_msg_td_handler(int stats
, void *buffer
, int len
, void *dev_id
)
1490 struct ohci_bulk_msg_request_state
*req
;
1492 req
= (struct ohci_bulk_msg_request_state
*) dev_id
;
1496 printk(KERN_DEBUG
"ohci_bulk_td_handler stats %x, buffer %p, len %d, req %p\n", stats
, buffer
, len
, req
);
1499 /* only count TDs that were completed successfully */
1500 if (stats
== USB_ST_NOERROR
|| stats
== USB_ST_DATAUNDERRUN
) /*DEN*/
1501 req
->_bytes_done
+= len
;
1504 if (MegaDebug
&& req
->_bytes_done
) {
1506 printk(KERN_DEBUG
" %d bytes, bulk data:", req
->_bytes_done
);
1507 for (i
= 0; i
< 16 && i
< req
->_bytes_done
; ++i
)
1508 printk(" %.2x", ((unsigned char *)buffer
)[i
]);
1509 if (i
< req
->_bytes_done
)
1515 /* call the real completion handler when done or on an error */
1516 if ((stats
!= USB_ST_NOERROR
) ||
1517 (req
->_bytes_done
>= req
->length
&& req
->completion
!= NULL
)) {
1518 *req
->bytes_transferred_p
+= req
->_bytes_done
;
1521 printk(KERN_DEBUG
"usb-ohci: bulk request %p ending\n", req
);
1523 req
->completion(stats
, buffer
, req
->_bytes_done
, req
->dev_id
);
1526 return 0; /* do not re-queue the TD */
1527 } /* ohci_bulk_msg_td_handler() */
1532 static DECLARE_WAIT_QUEUE_HEAD(bulk_wakeup
);
1535 static int ohci_bulk_msg_completed(int stats
, void *buffer
, int len
, void *dev_id
)
1538 printk("ohci_bulk_msg_completed %x, %p, %d, %p\n", stats
, buffer
, len
, dev_id
);
1540 if (dev_id
!= NULL
) {
1541 int *completion_status
= (int *)dev_id
;
1542 *completion_status
= stats
;
1545 wake_up(&bulk_wakeup
);
1546 return 0; /* don't requeue the TD */
1547 } /* ohci_bulk_msg_completed() */
1550 static int ohci_bulk_msg(struct usb_device
*usb_dev
, unsigned int pipe
, void *data
, int len
, unsigned long *bytes_transferred_p
, int timeout
)
1552 DECLARE_WAITQUEUE(wait
, current
);
1553 int completion_status
= USB_ST_INTERNALERROR
;
1554 struct ohci_bulk_msg_request_state req
;
1555 struct ohci_ed
*req_ed
;
1559 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
);
1562 /* initialize bytes transferred to nothing */
1563 *bytes_transferred_p
= 0;
1565 /* Hopefully this is similar to the "URP" (USB Request Packet) code
1566 * that michael gee is working on... */
1567 req
.usb_dev
= usb_dev
;
1571 req
.bytes_transferred_p
= bytes_transferred_p
;
1572 req
.dev_id
= &completion_status
;
1573 req
.completion
= ohci_bulk_msg_completed
;
1574 req
._bytes_done
= 0;
1575 if (bytes_transferred_p
)
1576 *bytes_transferred_p
= 0;
1578 if (usb_endpoint_halted(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
))
1579 && usb_clear_halt(usb_dev
, usb_pipeendpoint(pipe
) | (pipe
& 0x80)))
1580 return USB_ST_STALL
;
1583 * Start the transaction..
1585 current
->state
= TASK_UNINTERRUPTIBLE
;
1586 add_wait_queue(&bulk_wakeup
, &wait
);
1589 * Request to send or receive bulk data for a blocking bulk_msg call.
1591 * req.bytes_transferred_p is a pointer to an integer that
1592 * will be set to the number of bytes that have been successfully
1593 * transferred upon completion. The interrupt handler will update it
1594 * after each internal TD completes successfully.
1596 req_ed
= ohci_request_bulk(usb_dev
, pipe
,
1597 ohci_bulk_msg_td_handler
,
1600 /* wait for a caller specified time... */
1601 schedule_timeout(timeout
);
1603 /* completion_status will only stay in this state of the
1604 * request never finished */
1605 if (completion_status
== USB_ST_INTERNALERROR
) {
1606 struct ohci_device
*dev
= usb_to_ohci(usb_dev
);
1607 struct ohci_regs
*regs
= dev
->ohci
->regs
;
1610 printk(KERN_DEBUG
"ohci_bulk_msg timing out\n");
1612 /* XXX This code should go into a function used to stop
1613 * a previously requested bulk transfer. -greg */
1615 /* stop the transfer & collect the number of bytes */
1616 ohci_wait_for_ed_safe(regs
, req_ed
);
1618 /* Get the number of bytes transferred out of the head TD
1619 * on the ED if it didn't finish while we were waiting. */
1620 if ( ed_head_td(req_ed
) &&
1621 (ed_head_td(req_ed
) != ed_tail_td(req_ed
)) ) {
1622 struct ohci_td
*partial_td
;
1623 partial_td
= bus_to_virt(ed_head_td(req_ed
));
1627 show_ohci_td(partial_td
);
1630 /* Record the bytes as transferred */
1631 *bytes_transferred_p
+= ohci_td_bytes_done(partial_td
);
1633 /* If there was an unreported error, return it.
1634 * Otherwise return a timeout */
1635 completion_status
= OHCI_TD_CC_GET(partial_td
->info
);
1636 if (completion_status
== USB_ST_NOERROR
) {
1637 completion_status
= USB_ST_TIMEOUT
;
1643 remove_wait_queue(&bulk_wakeup
, &wait
);
1645 /* remove the ED from the HC */
1646 ohci_remove_norm_ed_from_hw(usb_to_ohci(usb_dev
)->ohci
, req_ed
);
1648 /* save the toggle value back into the usb_dev */
1649 usb_settoggle(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
),
1650 ohci_ed_carry(req_ed
));
1652 ohci_free_ed(req_ed
); /* return it to the pool */
1655 if (completion_status
!= 0 || MegaDebug
)
1656 printk(KERN_DEBUG
"ohci_bulk_msg done, status %x (bytes_transferred = %ld).\n", completion_status
, *bytes_transferred_p
);
1659 return completion_status
;
1660 } /* ohci_bulk_msg() */
1668 * Allocate a new USB device attached to this OHCI controller
1670 static int ohci_dev_allocate(struct usb_device
*usb_dev
)
1672 struct ohci_device
*dev
;
1676 * Allocate an OHCI device (EDs and TDs for this device)
1678 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
1682 memset(dev
, 0, sizeof(*dev
));
1684 /* Initialize all EDs in a new device with the skip flag so that
1685 * they are ignored by the controller until set otherwise. */
1686 for (idx
= 0; idx
< NUM_EDS
; ++idx
) {
1687 dev
->ed
[idx
].status
= cpu_to_le32(OHCI_ED_SKIP
);
1691 * Link them together
1693 usb_dev
->hcpriv
= dev
;
1696 if (usb_dev
->parent
)
1697 dev
->ohci
= usb_to_ohci(usb_dev
->parent
)->ohci
;
1700 } /* ohci_dev_allocate() */
1704 * Free a usb device on this ohci controller.
1706 static int ohci_dev_deallocate(struct usb_device
*usb_dev
)
1708 struct ohci_device
*dev
= usb_to_ohci(usb_dev
);
1710 ohci_remove_device(dev
->ohci
, usb_dev
->devnum
);
1712 kfree(usb_to_ohci(usb_dev
));
1716 static int ohci_get_current_frame_number(struct usb_device
*usb_dev
)
1718 return USB_ST_NOTSUPPORTED
;
1721 static int ohci_alloc_isochronous (struct usb_device
*usb_dev
,
1722 unsigned int pipe
, int frame_count
, void *context
,
1723 struct usb_isoc_desc
**isocdesc
)
1725 return USB_ST_NOTSUPPORTED
;
1728 static void ohci_delete_isochronous (struct usb_isoc_desc
*isocdesc
)
1733 static int ohci_sched_isochronous (struct usb_isoc_desc
*isocdesc
,
1734 struct usb_isoc_desc
*pr_isocdesc
)
1736 return USB_ST_NOTSUPPORTED
;
1739 static int ohci_unsched_isochronous (struct usb_isoc_desc
*isocdesc
)
1741 return USB_ST_NOTSUPPORTED
;
1745 * functions for the generic USB driver
1747 struct usb_operations ohci_device_operations
= {
1749 ohci_dev_deallocate
,
1755 ohci_terminate_bulk
,
1756 ohci_get_current_frame_number
,
1757 ohci_alloc_isochronous
,
1758 ohci_delete_isochronous
,
1759 ohci_sched_isochronous
,
1760 ohci_unsched_isochronous
1765 * Reset an OHCI controller. Returns >= 0 on success.
1767 * Afterwards the HC will be in the "suspend" state which prevents you
1768 * from writing to some registers. Bring it to the operational state
1771 static int reset_hc(struct ohci
*ohci
)
1773 int timeout
= 10000; /* prevent an infinite loop */
1776 printk(KERN_INFO
"usb-ohci: resetting HC %p\n", ohci
);
1779 writel(~0x0, &ohci
->regs
->intrdisable
); /* Disable HC interrupts */
1780 writel(1, &ohci
->regs
->cmdstatus
); /* HC Reset */
1781 writel_mask(0x3f, &ohci
->regs
->control
); /* move to UsbReset state */
1783 while ((readl(&ohci
->regs
->cmdstatus
) & OHCI_CMDSTAT_HCR
) != 0) {
1785 printk(KERN_ERR
"usb-ohci: USB HC reset timed out!\n");
1791 printk(KERN_DEBUG
"usb-ohci: HC %p reset.\n", ohci
);
1798 * Reset and start an OHCI controller. Returns >= 0 on success.
1800 static int start_hc(struct ohci
*ohci
)
1804 __u32 what_to_enable
;
1806 struct ohci_device
*root_hub
=usb_to_ohci(ohci
->bus
->root_hub
);
1809 printk(KERN_DEBUG
"entering start_hc %p\n", ohci
);
1812 if (reset_hc(ohci
) < 0)
1815 /* restore registers cleared by the reset */
1816 writel(virt_to_bus(root_hub
->hcca
), &ohci
->regs
->hcca
);
1819 * fminterval has to be 11999 (it can be adjusted +/- 1
1820 * to sync with other things if necessary).
1824 /* Start periodic transfers at 90% of fminterval (fmremaining
1825 * counts down; this will put them in the first 10% of the
1827 writel((fminterval
* 9) / 10, &ohci
->regs
->periodicstart
);
1829 /* Set largest data packet counter and frame interval. */
1830 fminterval
|= ((fminterval
- 210) * 6 / 7) << 16;
1831 writel(fminterval
, &ohci
->regs
->fminterval
);
1833 /* Set low-speed threshold (value from MacOS) */
1834 writel(1576, &ohci
->regs
->lsthresh
);
1837 * FNO (frame number overflow) could be enabled... they
1838 * occur every 32768 frames (every 32-33 seconds). This is
1839 * useful for debugging and as a bus heartbeat. -greg
1841 /* Choose the interrupts we care about */
1842 what_to_enable
= OHCI_INTR_MIE
|
1843 #ifdef OHCI_RHSC_INT
1846 /* | OHCI_INTR_FNO */
1848 writel( what_to_enable
, &ohci
->regs
->intrenable
);
1850 /* Enter the USB Operational state & start the frames a flowing.. */
1851 writel_set(OHCI_USB_OPER
, &ohci
->regs
->control
);
1853 /* Enable control lists, claim the host controller */
1855 OHCI_USB_IE
| OHCI_USB_CLE
| OHCI_USB_BLE
,
1856 &ohci
->regs
->control
);
1858 /* Force global power enable -gal@cs.uni-magdeburg.de */
1860 * This turns on global power switching for all the ports
1861 * and tells the HC that all of the ports should be powered on
1864 * TODO: This could be battery draining for laptops.. We
1865 * should implement power switching.
1867 writel_set( OHCI_ROOT_A_NPS
, &ohci
->regs
->roothub
.a
);
1868 writel_mask( ~((__u32
)OHCI_ROOT_A_PSM
), &ohci
->regs
->roothub
.a
);
1870 /* Turn on power to the root hub ports (thanks Roman!) */
1871 writel( OHCI_ROOT_LPSC
, &ohci
->regs
->roothub
.status
);
1873 printk(KERN_INFO
"usb-ohci: host controller operational\n");
1880 * Reset a root hub port
1882 static void ohci_reset_port(struct ohci
*ohci
, unsigned int port
)
1886 /* Don't allow overflows. */
1887 if (port
>= MAX_ROOT_PORTS
) {
1888 printk(KERN_ERR
"usb-ohci: bad port #%d in ohci_reset_port\n", port
);
1889 port
= MAX_ROOT_PORTS
-1;
1892 writel(PORT_PRS
, &ohci
->regs
->roothub
.portstatus
[port
]); /* Reset */
1895 * Wait for the reset to complete.
1899 /* check port status to see that the reset completed */
1900 status
= readl(&ohci
->regs
->roothub
.portstatus
[port
]);
1901 if (status
& PORT_PRS
) {
1902 /* reset failed, try harder? */
1903 printk(KERN_ERR
"usb-ohci: port %d reset failed, retrying\n", port
);
1904 writel(PORT_PRS
, &ohci
->regs
->roothub
.portstatus
[port
]);
1908 /* TODO we might need to re-enable the port here or is that
1909 * done elsewhere? */
1911 } /* ohci_reset_port */
1915 * This gets called if the connect status on the root hub changes.
1917 static void ohci_connect_change(struct ohci
* ohci
, int port
)
1919 struct usb_device
*usb_dev
;
1920 struct ohci_device
*dev
;
1921 struct ohci_device
*root_hub
=usb_to_ohci(ohci
->bus
->root_hub
);
1922 /* memory I/O address of the port status register */
1923 __u32
*portaddr
= &ohci
->regs
->roothub
.portstatus
[port
];
1927 printk(KERN_DEBUG
"ohci_connect_change on port %d\n", port
);
1931 * Because of the status change we have to forget
1932 * everything we think we know about the device
1933 * on this root hub port. It may have changed.
1935 usb_disconnect(root_hub
->usb
->children
+ port
);
1937 portstatus
= readl(portaddr
);
1939 /* disable the port if nothing is connected */
1940 if (!(portstatus
& PORT_CCS
)) {
1941 writel(PORT_CCS
, portaddr
);
1942 /* We need to reset the CSC bit -after- disabling the
1943 * port because it causes the CSC bit to come on
1946 writel(PORT_CSC
, portaddr
);
1948 printk(KERN_DEBUG
"ohci port %d disabled, nothing connected.\n", port
);
1954 * Allocate a device for the new thingy that's been attached
1956 usb_dev
= usb_alloc_dev(root_hub
->usb
, root_hub
->usb
->bus
);
1960 dev
= usb_dev
->hcpriv
;
1962 usb_connect(dev
->usb
);
1964 /* link it into the bus's device tree */
1965 root_hub
->usb
->children
[port
] = usb_dev
;
1967 wait_ms(200); /* wait for powerup; XXX is this needed? */
1968 ohci_reset_port(ohci
, port
);
1970 /* Get information on speed by using LSD */
1971 usb_dev
->slow
= readl(portaddr
) & PORT_LSDA
? 1 : 0;
1974 * Do generic USB device tree processing on the new device.
1976 usb_new_device(usb_dev
);
1978 } /* ohci_connect_change() */
1982 * This gets called when the root hub configuration
1983 * has changed. Just go through each port, seeing if
1984 * there is something interesting happening.
1986 static void ohci_check_configuration(struct ohci
*ohci
)
1988 struct ohci_regs
*regs
= ohci
->regs
;
1990 int maxport
= readl(&ohci
->regs
->roothub
) & 0xff;
1991 __u32 rh_change_flags
= PORT_CSC
| PORT_PESC
; /* root hub status changes */
1994 printk(KERN_DEBUG
"entering ohci_check_configuration %p\n", ohci
);
1998 __u32
*portstatus_p
= ®s
->roothub
.portstatus
[num
];
1999 if (readl(portstatus_p
) & rh_change_flags
) {
2000 /* acknowledge the root hub status changes */
2001 writel_set(rh_change_flags
, portstatus_p
);
2002 /* disable the port if nothing is on it */
2003 /* check the port for a nifty device */
2004 ohci_connect_change(ohci
, num
);
2006 } while (++num
< maxport
);
2009 printk(KERN_DEBUG
"leaving ohci_check_configuration %p\n", ohci
);
2011 } /* ohci_check_configuration() */
2016 * Check root hub port status and wake the control thread up if
2017 * anything has changed.
2019 * This function is called from the interrupt handler.
2021 static void ohci_root_hub_events(struct ohci
*ohci
)
2024 struct ohci_device
*root_hub
=usb_to_ohci(ohci
->bus
->root_hub
);
2025 int maxport
= root_hub
->usb
->maxchild
;
2027 if (!waitqueue_active(&ohci_configure
))
2030 __u32
*portstatus_p
= &ohci
->regs
->roothub
.portstatus
[num
];
2031 if (readl(portstatus_p
) & PORT_CSC
) {
2032 if (waitqueue_active(&ohci_configure
))
2033 wake_up(&ohci_configure
);
2036 } while (++num
< maxport
);
2038 } /* ohci_root_hub_events() */
2042 * The done list is in reverse order; we need to process TDs in the
2043 * order they were finished (FIFO). This function builds the FIFO
2044 * list using the next_dl_td pointer.
2046 * This function originally by Roman Weissgaerber (weissg@vienna.at)
2048 * This function is called from the interrupt handler.
2050 static struct ohci_td
* ohci_reverse_donelist(struct ohci
* ohci
)
2053 struct ohci_device
*root_hub
=usb_to_ohci(ohci
->bus
->root_hub
);
2054 struct ohci_hcca
*hcca
= root_hub
->hcca
;
2055 struct ohci_td
*td_list
= NULL
;
2056 struct ohci_td
*td_rev
= NULL
;
2058 td_list_hc
= le32_to_cpup(&hcca
->donehead
) & 0xfffffff0;
2062 td_list
= (struct ohci_td
*) bus_to_virt(td_list_hc
);
2063 td_list
->next_dl_td
= td_rev
;
2065 td_list_hc
= le32_to_cpup(&td_list
->next_td
) & 0xfffffff0;
2069 } /* ohci_reverse_donelist() */
2072 * Look at the ed (and td if necessary)
2073 * and return its direction as 0 = IN, 1 = OUT.
2075 int ed_get_dir (struct ohci_ed
*ed
, struct ohci_td
*td
)
2079 status
= le32_to_cpu(ed
->status
) & OHCI_ED_D
; /* keep only the Direction bits */
2080 if (status
== OHCI_ED_D_IN
) return 0;
2081 if (status
== OHCI_ED_D_OUT
) return 1;
2083 /* but if status == 0 or 3, look at the td for the Direction */
2084 status
= le32_to_cpu(td
->info
) & OHCI_TD_D
; /* keep only the Direction bits */
2085 if (status
== OHCI_TD_D_IN
) return 0;
2090 * Collect this interrupt's goodies off of the list of finished TDs
2091 * that the OHCI controller is kind enough to setup for us.
2093 * This function is called from the interrupt handler.
2095 static void ohci_reap_donelist(struct ohci
*ohci
)
2097 struct ohci_td
*td
; /* used for walking the list */
2099 /* um... isn't this dangerous to do in an interrupt handler? -greg */
2101 spin_lock(&ohci_edtd_lock
);
2103 /* create the FIFO ordered donelist */
2104 td
= ohci_reverse_donelist(ohci
);
2106 while (td
!= NULL
) {
2107 struct ohci_td
*next_td
= td
->next_dl_td
;
2108 int cc
= OHCI_TD_CC_GET(le32_to_cpup(&td
->info
));
2109 struct ohci_ed
*ed
= td
->ed
;
2112 printk(KERN_ERR
"usb-ohci: yikes! reaping dummy TD\n");
2115 if (cc
!= 0 && MegaDebug
) {
2116 printk("cc=%s on td %p (ed %p)\n", cc_names
[cc
], td
, ed
);
2119 if (ed_head_td(ed
) != ed_tail_td(ed
))
2120 show_ohci_td_chain(bus_to_virt(ed_head_td(ed
)));
2124 if (cc
== USB_ST_STALL
) {
2125 /* mark endpoint as halted */
2126 usb_endpoint_halt(ed
->ohci_dev
->usb
, ed_get_en(ed
), ed_get_dir(ed
, td
));
2129 if (cc
!= 0 && ohci_ed_halted(ed
) && !td_endofchain(*td
)) {
2131 * There was an error on this TD and the ED
2132 * is halted, and this was not the last TD
2133 * of the transaction, so there will be TDs
2134 * to clean off the ED.
2136 struct ohci_td
*tail_td
= bus_to_virt(ed_tail_td(ed
));
2137 struct ohci_td
*ntd
;
2140 td
= ntd
= bus_to_virt(ed_head_td(ed
));
2141 while (td
!= tail_td
) {
2142 ntd
= bus_to_virt(le32_to_cpup(&td
->next_td
));
2143 if (td_endofchain(*td
))
2146 printk(KERN_DEBUG
"usb-ohci: skipping TD %p\n", td
);
2151 /* Set the ED head past the ones we cleaned
2152 off, and clear the halted flag */
2153 printk(KERN_DEBUG
"usb-ohci: restarting ED %p at TD %p\n", ed
, ntd
);
2154 set_ed_head_td(ed
, virt_to_bus(ntd
));
2156 /* If we didn't find an endofchain TD, give up */
2157 if (td
== tail_td
) {
2163 /* Check if TD should be re-queued */
2164 if ((td
->completed
!= NULL
) &&
2165 (td
->completed(cc
, td
->data
, ohci_td_bytes_done(td
), td
->dev_id
))) {
2166 /* Mark the TD as active again:
2167 * Set the not accessed condition code
2168 * Reset the Error count
2170 td
->info
|= cpu_to_le32(OHCI_TD_CC_NEW
);
2171 clear_td_errorcount(td
);
2172 /* reset the toggle field to TOGGLE_AUTO (0) */
2173 td
->info
&= cpu_to_le32(~OHCI_TD_DT
);
2175 /* point it back to the start of the data buffer */
2176 td
->cur_buf
= cpu_to_le32(virt_to_bus(td
->data
));
2178 /* insert it back on its ED */
2180 ohci_add_tds_to_ed(td
, ed
);
2181 /* ohci_unhalt_ed(td->ed); */
2183 /* return it to the pool of free TDs */
2184 if (can_auto_free(*td
))
2191 spin_unlock(&ohci_edtd_lock
);
2192 } /* ohci_reap_donelist() */
2196 * Get annoyed at the controller for bothering us.
2197 * This pretty much follows the OHCI v1.0a spec, section 5.3.
2199 static void ohci_interrupt(int irq
, void *__ohci
, struct pt_regs
*r
)
2201 struct ohci
*ohci
= __ohci
;
2202 struct ohci_regs
*regs
= ohci
->regs
;
2203 struct ohci_device
*root_hub
=usb_to_ohci(ohci
->bus
->root_hub
);
2204 struct ohci_hcca
*hcca
= root_hub
->hcca
;
2205 __u32 status
, context
;
2207 /* Save the status of the interrupts that are enabled */
2208 status
= readl(®s
->intrstatus
);
2209 status
&= readl(®s
->intrenable
);
2211 /* make context = the interrupt status bits that we care about */
2212 if (hcca
->donehead
!= 0) {
2213 context
= OHCI_INTR_WDH
; /* hcca donehead needs processing */
2214 if (hcca
->donehead
& cpu_to_le32(1)) {
2215 context
|= status
; /* other status change to check */
2220 /* TODO increment a useless interrupt counter here */
2225 /* Disable HC interrupts */ /* why? - paulus */
2226 writel(OHCI_INTR_MIE
, ®s
->intrdisable
);
2229 /* Only do this for SERIOUS debugging, be sure kern.debug logs
2230 * are not going to the console as this can cause your
2231 * machine to lock up if so... -greg */
2232 show_ohci_status(ohci
);
2235 /* Process the done list */
2236 if (context
& OHCI_INTR_WDH
) {
2237 /* See which TD's completed.. */
2238 ohci_reap_donelist(ohci
);
2240 /* reset the done queue and tell the controller */
2241 hcca
->donehead
= 0; /* XXX already done in ohci_reverse_donelist */
2242 writel(OHCI_INTR_WDH
, ®s
->intrstatus
);
2244 context
&= ~OHCI_INTR_WDH
; /* mark this as checked */
2247 #ifdef OHCI_RHSC_INT
2248 /* NOTE: this is very funky on some USB controllers (ie: it
2249 * doesn't work right). Using the ohci_timer instead to poll
2250 * the root hub is a much better choice. */
2251 /* Process any root hub status changes */
2252 if (context
& OHCI_INTR_RHSC
) {
2253 /* Wake the thread to process root hub events */
2254 if (waitqueue_active(&ohci_configure
))
2255 wake_up(&ohci_configure
);
2257 writel(OHCI_INTR_RHSC
, ®s
->intrstatus
);
2259 * Don't unset RHSC in context; it should be disabled.
2260 * The control thread will re-enable it after it has
2261 * checked the root hub status.
2266 /* Start of Frame interrupts, used during safe ED removal */
2267 if (context
& (OHCI_INTR_SF
)) {
2268 writel(OHCI_INTR_SF
, ®s
->intrstatus
);
2269 if (waitqueue_active(&start_of_frame_wakeup
))
2270 wake_up(&start_of_frame_wakeup
);
2271 /* Do NOT mark the frame start interrupt as checked
2272 * as we don't want to receive any more of them until
2276 /* Check those "other" pesky bits */
2277 if (context
& (OHCI_INTR_FNO
)) {
2278 writel(OHCI_INTR_FNO
, ®s
->intrstatus
);
2279 context
&= ~OHCI_INTR_FNO
; /* mark this as checked */
2281 if (context
& OHCI_INTR_SO
) {
2282 writel(OHCI_INTR_SO
, ®s
->intrstatus
);
2283 context
&= ~OHCI_INTR_SO
; /* mark this as checked */
2285 if (context
& OHCI_INTR_RD
) {
2286 writel(OHCI_INTR_RD
, ®s
->intrstatus
);
2287 context
&= ~OHCI_INTR_RD
; /* mark this as checked */
2289 if (context
& OHCI_INTR_UE
) {
2290 /* TODO: need to have the control thread reset the
2291 * controller now and keep a count of unrecoverable
2292 * errors. If there are too many, it should just shut
2293 * the broken controller down entirely. */
2294 writel(OHCI_INTR_UE
, ®s
->intrstatus
);
2295 context
&= ~OHCI_INTR_UE
; /* mark this as checked */
2297 if (context
& OHCI_INTR_OC
) {
2298 writel(OHCI_INTR_OC
, ®s
->intrstatus
);
2299 context
&= ~OHCI_INTR_OC
; /* mark this as checked */
2302 /* Mask out any remaining unprocessed or unmasked interrupts
2303 * so that we don't get any more of them. */
2304 if (context
& ~OHCI_INTR_MIE
) {
2305 writel(context
, ®s
->intrdisable
);
2308 /* Re-enable HC interrupts */
2309 writel(OHCI_INTR_MIE
, ®s
->intrenable
);
2311 } /* ohci_interrupt() */
2315 * Allocate the resources required for running an OHCI controller.
2316 * Host controller interrupts must not be running while calling this
2317 * function or the penguins will get angry.
2319 * The mem_base parameter must be the usable -virtual- address of the
2320 * host controller's memory mapped I/O registers.
2322 static struct ohci
*alloc_ohci(void* mem_base
)
2326 struct usb_bus
*bus
;
2327 struct ohci_device
*dev
;
2328 struct usb_device
*usb
;
2329 struct ohci_hcca
*hcca
;
2333 printk(KERN_DEBUG
"entering alloc_ohci %p\n", mem_base
);
2336 ohci
= kmalloc(sizeof(*ohci
), GFP_KERNEL
);
2339 memset(ohci
, 0, sizeof(*ohci
));
2342 ohci
->regs
= mem_base
;
2343 INIT_LIST_HEAD(&ohci
->interrupt_list
);
2346 * Allocate the Host Controller Communications Area on a 256
2347 * byte boundary. XXX take the easy way out and just grab a
2348 * page as that's guaranteed to have a nice boundary.
2350 hcca
= (struct ohci_hcca
*) __get_free_page(GFP_KERNEL
);
2353 memset(hcca
, 0, sizeof(struct ohci_hcca
));
2355 bus
= usb_alloc_bus(&ohci_device_operations
);
2363 * Allocate the USB device structure and root hub.
2365 * Here we allocate our own root hub and TDs as well as the
2366 * OHCI host controller communications area. The HCCA is just
2367 * a nice pool of memory with pointers to endpoint descriptors
2368 * for the different interrupts.
2370 usb
= usb_alloc_dev(NULL
, bus
);
2376 dev
= usb_to_ohci(usb
);
2380 ohci
->bus
->root_hub
= ohci_to_usb(dev
);
2382 /* Initialize the root hub */
2383 dev
->ohci
= ohci
; /* link back to the controller */
2385 /* Tell the controller where the HCCA is */
2386 writel(virt_to_bus(dev
->hcca
), &ohci
->regs
->hcca
);
2389 printk(KERN_DEBUG
"usb-ohci: HCCA allocated at %p (bus %p)\n", dev
->hcca
, (void*)virt_to_bus(dev
->hcca
));
2392 /* Get the number of ports on the root hub */
2393 usb
->maxchild
= readl(&ohci
->regs
->roothub
.a
) & 0xff;
2394 if (usb
->maxchild
> MAX_ROOT_PORTS
) {
2395 printk(KERN_INFO
"usb-ohci: Limited to %d ports\n", MAX_ROOT_PORTS
);
2396 usb
->maxchild
= MAX_ROOT_PORTS
;
2398 if (usb
->maxchild
< 1) {
2399 printk(KERN_ERR
"usb-ohci: Less than one root hub port? Impossible!\n");
2402 printk(KERN_DEBUG
"usb-ohci: %d root hub ports found\n", usb
->maxchild
);
2406 * Fix up legacy mode
2409 ctrl
= readl(&ohci
->regs
->control
);
2410 if(ctrl
&OHCI_USB_IR
)
2413 /* Ask SMM for the controls */
2414 writel(8, &ohci
->regs
->cmdstatus
);
2415 printk(KERN_INFO
"usb-ohci: switching interface from legacy mode.\n");
2416 while((readl(&ohci
->regs
->control
)&OHCI_USB_IR
) && ct
< 250)
2424 * Initialize the ED polling "tree" (for simplicity's sake in
2425 * this driver many nodes in the tree will be identical)
2427 dev
->ed
[ED_INT_32
].next_ed
= cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_16
]));
2428 dev
->ed
[ED_INT_16
].next_ed
= cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_8
]));
2429 dev
->ed
[ED_INT_8
].next_ed
= cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_4
]));
2430 dev
->ed
[ED_INT_4
].next_ed
= cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_2
]));
2431 dev
->ed
[ED_INT_2
].next_ed
= cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_1
]));
2434 * Initialize the polling table to call interrupts at the
2435 * intended intervals. Note that these EDs are just
2436 * placeholders. They have their SKIP bit set and are used as
2437 * list heads to insert real EDs onto.
2439 dev
->hcca
->int_table
[0] = cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_1
]));
2440 for (i
= 1; i
< NUM_INTS
; i
++) {
2442 dev
->hcca
->int_table
[i
] =
2443 cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_32
]));
2445 dev
->hcca
->int_table
[i
] =
2446 cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_16
]));
2448 dev
->hcca
->int_table
[i
] =
2449 cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_8
]));
2451 dev
->hcca
->int_table
[i
] =
2452 cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_4
]));
2454 dev
->hcca
->int_table
[i
] =
2455 cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_2
]));
2459 * Tell the controller where the control and bulk lists are.
2460 * The lists start out empty.
2462 writel(0, &ohci
->regs
->ed_controlhead
);
2463 writel(0, &ohci
->regs
->ed_bulkhead
);
2467 printk(KERN_DEBUG
"alloc_ohci(): controller\n");
2468 show_ohci_status(ohci
);
2473 printk(KERN_DEBUG
"leaving alloc_ohci %p\n", ohci
);
2481 free_page((unsigned long)hcca
);
2486 } /* alloc_ohci() */
2490 * De-allocate all resoueces..
2492 static void release_ohci(struct ohci
*ohci
)
2494 printk(KERN_INFO
"Releasing OHCI controller 0x%p\n", ohci
);
2497 /* stop our timer */
2498 del_timer(&ohci_timer
);
2500 if (ohci
->irq
>= 0) {
2501 free_irq(ohci
->irq
, ohci
);
2505 /* stop all OHCI interrupts */
2506 writel(~0x0, &ohci
->regs
->intrdisable
);
2508 if (ohci
->bus
->root_hub
) {
2509 struct ohci_device
*root_hub
=usb_to_ohci(ohci
->bus
->root_hub
);
2510 /* ensure that HC is stopped before releasing the HCCA */
2511 writel(OHCI_USB_SUSPEND
, &ohci
->regs
->control
);
2512 free_page((unsigned long) root_hub
->hcca
);
2513 kfree(ohci
->bus
->root_hub
);
2514 ohci
->bus
->root_hub
= NULL
;
2517 /* unmap the IO address space */
2518 iounmap(ohci
->regs
);
2524 /* If the ohci itself were dynamic we'd free it here */
2526 printk(KERN_DEBUG
"usb-ohci: HC resources released.\n");
2527 } /* release_ohci() */
2531 * USB OHCI control thread
2533 static int ohci_control_thread(void * __ohci
)
2535 struct ohci
*ohci
= (struct ohci
*)__ohci
;
2538 * I'm unfamiliar with the SMP kernel locking.. where should
2539 * this be released and what does it do? -greg
2544 * This thread doesn't need any user-level access,
2545 * so get rid of all of our resources..
2547 printk(KERN_DEBUG
"ohci-control thread code for 0x%p code at 0x%p\n", __ohci
, &ohci_control_thread
);
2549 exit_files(current
);
2550 /*exit_fs(current);*/ /* can't do kernel_thread if we do this */
2552 strcpy(current
->comm
, "ohci-control");
2554 usb_register_bus(ohci
->bus
);
2557 * Damn the torpedoes, full speed ahead
2559 if (start_hc(ohci
) < 0) {
2560 printk(KERN_ERR
"usb-ohci: failed to start the controller\n");
2562 usb_deregister_bus(ohci
->bus
);
2563 printk(KERN_INFO
"leaving ohci_control_thread %p\n", __ohci
);
2569 int unsigned long signr
;
2573 /* check the root hub configuration for changes. */
2574 ohci_check_configuration(ohci
);
2576 /* re-enable root hub status change interrupts. */
2577 #ifdef OHCI_RHSC_INT
2578 writel(OHCI_INTR_RHSC
, &ohci
->regs
->intrenable
);
2581 printk(KERN_DEBUG
"ohci-control thread sleeping\n");
2582 interruptible_sleep_on(&ohci_configure
);
2586 if (start_hc(ohci
) < 0)
2593 * If we were woken up by a signal, see if its useful,
2596 if (signal_pending(current
)) {
2597 /* sending SIGUSR1 makes us print out some info */
2598 spin_lock_irq(¤t
->sigmask_lock
);
2599 signr
= dequeue_signal(¤t
->blocked
, &info
);
2600 spin_unlock_irq(¤t
->sigmask_lock
);
2602 if(signr
== SIGUSR1
) {
2603 printk(KERN_DEBUG
"OHCI status dump:\n");
2604 show_ohci_status(ohci
);
2605 } else if (signr
== SIGUSR2
) {
2606 /* toggle mega TD/ED debugging output */
2608 MegaDebug
= !MegaDebug
;
2609 printk(KERN_DEBUG
"usb-ohci: Mega debugging %sabled.\n",
2610 MegaDebug
? "en" : "dis");
2613 /* unknown signal, exit the thread */
2614 printk(KERN_DEBUG
"usb-ohci: control thread for %p exiting on signal %ld\n", __ohci
, signr
);
2622 usb_deregister_bus(ohci
->bus
);
2625 } /* ohci_control_thread() */
2629 static int handle_apm_event(apm_event_t event
)
2631 static int down
= 0;
2634 case APM_SYS_SUSPEND
:
2635 case APM_USER_SUSPEND
:
2637 printk(KERN_DEBUG
"usb-ohci: received extra suspend event\n");
2642 case APM_NORMAL_RESUME
:
2643 case APM_CRITICAL_RESUME
:
2645 printk(KERN_DEBUG
"usb-ohci: received bogus resume event\n");
2649 if (waitqueue_active(&ohci_configure
)) {
2651 wake_up(&ohci_configure
);
2656 } /* handle_apm_event() */
2662 * Inspired by Iñaky's driver. This function is a timer routine that
2663 * is called every OHCI_TIMER_FREQ ms. It polls the root hub for
2664 * status changes as on my system the RHSC interrupt just doesn't
2665 * play well with others.. (so RHSC is turned off by default in this
2667 * [my controller is a "SiS 7001 USB (rev 16)"]
2670 static void ohci_timer_func (unsigned long ohci_ptr
)
2672 struct ohci
*ohci
= (struct ohci
*)ohci_ptr
;
2674 ohci_root_hub_events(ohci
);
2676 /* set the next timer */
2677 mod_timer(&ohci_timer
, jiffies
+ ((OHCI_TIMER_FREQ
*HZ
)/1000));
2679 } /* ohci_timer_func() */
2684 * Increment the module usage count, start the control thread and
2685 * return success if the controller is good.
2687 static int found_ohci(int irq
, void* mem_base
)
2693 printk(KERN_DEBUG
"entering found_ohci %d %p\n", irq
, mem_base
);
2696 /* Allocate the running OHCI structures */
2697 ohci
= alloc_ohci(mem_base
);
2703 init_timer(&ohci_timer
);
2704 ohci_timer
.expires
= jiffies
+ ((OHCI_TIMER_FREQ
*HZ
)/1000);
2705 ohci_timer
.data
= (unsigned long)ohci
;
2706 ohci_timer
.function
= ohci_timer_func
;
2707 add_timer(&ohci_timer
);
2711 if (request_irq(irq
, ohci_interrupt
, SA_SHIRQ
, "usb-ohci", ohci
) == 0) {
2717 printk(KERN_DEBUG
"usb-ohci: forking ohci-control thread for 0x%p\n", ohci
);
2720 /* fork off the handler */
2721 pid
= kernel_thread(ohci_control_thread
, ohci
,
2722 CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
);
2729 printk(KERN_ERR
"usb-ohci: Couldn't allocate interrupt %d\n", irq
);
2734 printk(KERN_DEBUG
"leaving found_ohci %d %p\n", irq
, mem_base
);
2738 } /* found_ohci() */
2742 * If this controller is for real, map the IO memory and proceed
2744 static int init_ohci(struct pci_dev
*dev
)
2746 unsigned long mem_base
= dev
->resource
[0].flags
;
2748 /* If its OHCI, its memory */
2749 if (mem_base
& PCI_BASE_ADDRESS_SPACE_IO
)
2752 pci_set_master(dev
);
2754 /* Get the memory address and map it for IO */
2755 mem_base
= dev
->resource
[0].start
;
2757 /* no interrupt won't work... */
2758 if (dev
->irq
== 0) {
2759 printk(KERN_ERR
"usb-ohci: no irq assigned? check your BIOS settings.\n");
2764 * FIXME ioremap_nocache isn't implemented on all CPUs (such
2765 * as the Alpha) [?] What should I use instead...
2767 * The iounmap() is done on in release_ohci.
2769 mem_base
= (unsigned long) ioremap_nocache(mem_base
, 4096);
2772 printk(KERN_ERR
"Error mapping OHCI memory\n");
2778 printk(KERN_INFO
"usb-ohci: Warning! Gobs of debugging output has been enabled.\n");
2779 printk(KERN_INFO
" Check your kern.debug logs for the bulk of it.\n");
2782 if (found_ohci(dev
->irq
, (void *) mem_base
) < 0) {
2790 /* TODO this should be named following Linux convention and go in pci.h */
2791 #define PCI_CLASS_SERIAL_USB_OHCI ((PCI_CLASS_SERIAL_USB << 8) | 0x0010)
2794 * Search the PCI bus for an OHCI USB controller and set it up
2796 * If anyone wants multiple controllers this will need to be
2797 * updated.. Right now, it just picks the first one it finds.
2802 struct pci_dev
*dev
= NULL
;
2805 if (sizeof(struct ohci_device
) > 4096) {
2806 printk(KERN_ERR
"usb-ohci: struct ohci_device to large\n");
2810 printk(KERN_INFO
"OHCI USB Driver loading\n");
2814 /* Find an OHCI USB controller */
2815 dev
= pci_find_class(PCI_CLASS_SERIAL_USB_OHCI
, dev
);
2819 /* Verify that its OpenHCI by checking for MMIO */
2820 /* pci_read_config_byte(dev, PCI_CLASS_PROG, &type);
2825 retval
= init_ohci(dev
);
2830 apm_register_callback(&handle_apm_event
);
2833 return 0; /* no error */
2841 * Clean up when unloading the module
2843 void cleanup_module(void){
2845 apm_unregister_callback(&handle_apm_event
);
2847 printk(KERN_ERR
"usb-ohci: module unloaded\n");
2850 int init_module(void){