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>
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() */
206 * Put a control ED on the controller's list
208 void ohci_add_control_ed(struct ohci
*ohci
, struct ohci_ed
*ed
)
210 ohci_add_ed_to_hw(ed
, &ohci
->regs
->ed_controlhead
);
211 ohci_start_control(ohci
);
212 } /* ohci_add_control_ed() */
215 * Put a bulk ED on the controller's list
217 void ohci_add_bulk_ed(struct ohci
*ohci
, struct ohci_ed
*ed
)
219 ohci_add_ed_to_hw(ed
, &ohci
->regs
->ed_bulkhead
);
220 ohci_start_bulk(ohci
);
221 } /* ohci_add_bulk_ed() */
224 * Put a periodic ED on the appropriate list given the period.
226 void ohci_add_periodic_ed(struct ohci
*ohci
, struct ohci_ed
*ed
, int period
)
228 struct ohci_ed
*int_ed
;
229 struct ohci_device
*root_hub
=usb_to_ohci(ohci
->bus
->root_hub
);
233 * Pick a good frequency endpoint based on the requested period
235 int_ed
= &root_hub
->ed
[ms_to_ed_int(period
)];
238 printk(KERN_DEBUG
"usb-ohci: Using INT ED queue %d for %dms period\n",
239 ms_to_ed_int(period
), period
);
242 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
244 * Insert this ED at the front of the list.
246 ed
->next_ed
= int_ed
->next_ed
;
247 int_ed
->next_ed
= cpu_to_le32(virt_to_bus(ed
));
249 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
251 ohci_start_periodic(ohci
);
252 } /* ohci_add_periodic_ed() */
255 * Locate the periodic ED for a given interrupt endpoint.
257 struct ohci_ed
*ohci_get_periodic_ed(struct ohci_device
*dev
, int period
,
258 unsigned int pipe
, int isoc
)
260 struct ohci_device
*root_hub
= usb_to_ohci(dev
->ohci
->bus
->root_hub
);
262 struct ohci_ed
*int_ed
;
263 unsigned int status
, req_status
;
265 /* get the dummy ED before the EDs for this period */
266 int_ed
= &root_hub
->ed
[ms_to_ed_int(period
)];
268 /* decide on what the status field should look like */
269 req_status
= ed_set_maxpacket(usb_maxpacket(ohci_to_usb(dev
), pipe
, usb_pipeout(pipe
)))
270 | ed_set_speed(usb_pipeslow(pipe
))
271 | (usb_pipe_endpdev(pipe
) & 0x7ff)
272 | ed_set_type_isoc(isoc
);
274 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
276 int_ed
= bus_to_virt(le32_to_cpup(&int_ed
->next_ed
));
277 /* stop if we get to the end or to another dummy ED. */
280 status
= le32_to_cpup(&int_ed
->status
);
281 if ((status
& OHCI_ED_FA
) == 0)
283 /* check whether all the appropriate fields match */
284 if ((status
& 0x7ffa7ff) == req_status
)
291 * Put an isochronous ED on the controller's list
293 inline void ohci_add_isoc_ed(struct ohci
*ohci
, struct ohci_ed
*ed
)
295 ohci_add_periodic_ed(ohci
, ed
, 1);
300 * This will be used for the interrupt to wake us up on the next SOF
302 DECLARE_WAIT_QUEUE_HEAD(start_of_frame_wakeup
);
304 static void ohci_wait_sof(struct ohci_regs
*regs
)
306 DECLARE_WAITQUEUE(wait
, current
);
308 add_wait_queue(&start_of_frame_wakeup
, &wait
);
310 /* clear the SOF interrupt status and enable it */
311 writel(OHCI_INTR_SF
, ®s
->intrstatus
);
312 writel(OHCI_INTR_SF
, ®s
->intrenable
);
314 schedule_timeout(HZ
/10);
316 remove_wait_queue(&start_of_frame_wakeup
, &wait
);
320 * Guarantee that an ED is safe to be modified by the HCD (us).
322 * This function can NOT be called from an interrupt.
324 * TODO: If we're waiting for the ED to be safe so that it can be
325 * destroyed, a similar "ohci_schedule_ed_free" function that just
326 * adds it to a list of EDs to destroy during the SOF interrupt
327 * processing would be useful (and be callable from an interrupt).
329 void ohci_wait_for_ed_safe(struct ohci_regs
*regs
, struct ohci_ed
*ed
, int ed_type
)
331 __u32
*hw_listcurrent
;
333 /* tell the controller to skip this ED */
334 ed
->status
|= cpu_to_le32(OHCI_ED_SKIP
);
338 hw_listcurrent
= ®s
->ed_controlcurrent
;
341 hw_listcurrent
= ®s
->ed_bulkcurrent
;
345 hw_listcurrent
= ®s
->ed_periodcurrent
;
352 * If the HC is processing this ED we need to wait until the
353 * at least the next frame.
355 if (virt_to_bus(ed
) == readl(hw_listcurrent
)) {
357 printk(KERN_INFO
"Waiting a frame for OHC to finish with ED %p [%x %x %x %x]\n", ed
, FIELDS_OF_ED(ed
));
363 return; /* The ED is now safe */
364 } /* ohci_wait_for_ed_safe() */
368 * Remove an ED from the HC's list.
369 * This function can ONLY be used for Control or Bulk EDs.
371 * Note that the SKIP bit is left on in the removed ED.
373 void ohci_remove_norm_ed_from_hw(struct ohci
*ohci
, struct ohci_ed
*ed
, int ed_type
)
376 struct ohci_regs
*regs
= ohci
->regs
;
378 __u32 bus_ed
= virt_to_bus(ed
);
380 __u32
*hw_listhead_p
;
382 if (ed
== NULL
|| !bus_ed
)
384 ed
->status
|= cpu_to_le32(OHCI_ED_SKIP
);
388 hw_listhead_p
= ®s
->ed_controlhead
;
391 hw_listhead_p
= ®s
->ed_bulkhead
;
394 printk(KERN_ERR
"Unknown HCD ED type %d.\n", ed_type
);
398 bus_cur
= readl(hw_listhead_p
);
401 return; /* the list is already empty */
403 cur
= bus_to_virt(bus_cur
);
405 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
407 /* if its the head ED, move the head */
408 if (bus_cur
== bus_ed
) {
409 writel(le32_to_cpup(&cur
->next_ed
), hw_listhead_p
);
410 } else if (cur
->next_ed
!= 0) {
411 struct ohci_ed
*prev
;
413 /* walk the list and unlink the ED if found */
416 cur
= bus_to_virt(le32_to_cpup(&cur
->next_ed
));
419 /* unlink from the list */
420 prev
->next_ed
= cur
->next_ed
;
423 } while (cur
->next_ed
!= 0);
427 * Make sure this ED is not being accessed by the HC as we speak.
429 ohci_wait_for_ed_safe(regs
, ed
, ed_type
);
431 /* clear any links from the ED for safety */
434 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
435 } /* ohci_remove_norm_ed_from_hw() */
438 * Remove an ED from the controller's control list. Note that the SKIP bit
439 * is left on in the removed ED.
441 inline void ohci_remove_control_ed(struct ohci
*ohci
, struct ohci_ed
*ed
)
443 ohci_remove_norm_ed_from_hw(ohci
, ed
, HCD_ED_CONTROL
);
447 * Remove an ED from the controller's bulk list. Note that the SKIP bit
448 * is left on in the removed ED.
450 inline void ohci_remove_bulk_ed(struct ohci
*ohci
, struct ohci_ed
*ed
)
452 ohci_remove_norm_ed_from_hw(ohci
, ed
, HCD_ED_BULK
);
457 * Remove a periodic ED from the host controller
459 void ohci_remove_periodic_ed(struct ohci
*ohci
, struct ohci_ed
*ed
)
461 struct ohci_device
*root_hub
= usb_to_ohci(ohci
->bus
->root_hub
);
462 struct ohci_ed
*cur_ed
= NULL
, *prev_ed
;
465 /* FIXME: this will need to up fixed when add_periodic_ed()
466 * is updated to spread similar polling rate EDs out over
467 * multiple periodic queues. Currently this assumes that the
468 * 32ms (slowest) polling queue links to all others... */
470 /* search the periodic EDs, skipping the first one which is
471 * only a placeholder. */
472 prev_ed
= &root_hub
->ed
[ED_INT_32
];
473 if (prev_ed
->next_ed
)
474 cur_ed
= bus_to_virt(le32_to_cpup(&prev_ed
->next_ed
));
477 if (ed
== cur_ed
) { /* remove the ED */
478 /* set its SKIP bit and be sure its not in use */
479 ohci_wait_for_ed_safe(ohci
->regs
, ed
, HCD_ED_INT
);
482 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
483 prev_ed
->next_ed
= ed
->next_ed
;
484 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
490 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
491 if (cur_ed
->next_ed
) {
493 cur_ed
= bus_to_virt(le32_to_cpup(&cur_ed
->next_ed
));
494 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
496 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
498 /* if multiple polling queues need to be checked,
499 * here is where you'd advance to the next one */
501 printk("usb-ohci: ed %p not found on periodic queue\n", ed
);
505 } /* ohci_remove_periodic_ed() */
509 * Remove all the EDs which have a given device address from a list.
510 * Used when the device is unplugged.
511 * Returns 1 if anything was changed.
513 static int ohci_remove_device_list(__u32
*headp
, int devnum
)
516 __u32
*prevp
= headp
;
519 while (*prevp
!= 0) {
520 ed
= bus_to_virt(le32_to_cpup(prevp
));
521 if ((le32_to_cpup(&ed
->status
) & OHCI_ED_FA
) == devnum
) {
522 /* set the controller to skip this one
523 and remove it from the list */
524 ed
->status
|= cpu_to_le32(OHCI_ED_SKIP
);
525 /* XXX should call td->completed for each td */
526 *prevp
= ed
->next_ed
;
529 prevp
= &ed
->next_ed
;
538 * Remove all the EDs for a given device from all lists.
540 void ohci_remove_device(struct ohci
*ohci
, int devnum
)
544 struct ohci_regs
*regs
= ohci
->regs
;
545 struct ohci_device
*root_hub
=usb_to_ohci(ohci
->bus
->root_hub
);
547 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
550 head
= cpu_to_le32(readl(®s
->ed_controlhead
));
551 if (ohci_remove_device_list(&head
, devnum
))
552 writel(le32_to_cpup(&head
), ®s
->ed_controlhead
);
555 head
= cpu_to_le32(readl(®s
->ed_bulkhead
));
556 if (ohci_remove_device_list(&head
, devnum
))
557 writel(le32_to_cpup(&head
), ®s
->ed_bulkhead
);
559 /* Interrupt/iso list */
560 head
= cpu_to_le32(virt_to_bus(&root_hub
->ed
[ED_INT_32
]));
561 ohci_remove_device_list(&head
, devnum
);
564 * Wait until the start of the next frame to ensure
565 * that the HC has seen any changes.
567 ohci_wait_sof(ohci
->regs
);
569 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
573 * Remove a TD from the given EDs TD list. The TD is freed as well.
574 * (so far this function hasn't been needed)
576 static void ohci_remove_td_from_ed(struct ohci_td
*td
, struct ohci_ed
*ed
)
579 struct ohci_td
*head_td
;
581 if ((td
== NULL
) || (ed
== NULL
))
584 if (ed_head_td(ed
) == 0)
587 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
589 /* set the "skip me bit" in this ED */
590 ed
->status
|= cpu_to_le32(OHCI_ED_SKIP
);
592 /* XXX Assuming this list will never be circular */
594 head_td
= bus_to_virt(ed_head_td(ed
));
595 if (virt_to_bus(td
) == ed_head_td(ed
)) {
596 /* It's the first TD, remove it. */
597 set_ed_head_td(ed
, head_td
->next_td
);
599 struct ohci_td
*prev_td
, *cur_td
;
601 /* FIXME: collapse this into a nice simple loop :) */
602 if (head_td
->next_td
!= 0) {
604 cur_td
= bus_to_virt(le32_to_cpup(&head_td
->next_td
));
608 prev_td
->next_td
= cur_td
->next_td
;
611 if (cur_td
->next_td
== 0)
614 cur_td
= bus_to_virt(le32_to_cpup(&cur_td
->next_td
));
619 td
->next_td
= 0; /* remove the TDs links */
622 /* return this TD to the pool of free TDs */
625 /* unset the "skip me bit" in this ED */
626 ed
->status
&= cpu_to_le32(~OHCI_ED_SKIP
);
628 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
629 } /* ohci_remove_td_from_ed() */
633 * Get a pointer (virtual) to an available TD from the given device's
634 * pool. Return NULL if none are left.
636 static struct ohci_td
*ohci_get_free_td(struct ohci_device
*dev
)
641 printk(KERN_DEBUG
"in ohci_get_free_td()\n");
644 /* FIXME: this is horribly inefficient */
645 for (idx
=0; idx
< NUM_TDS
; idx
++) {
647 show_ohci_td(&dev
->td
[idx
]);
649 if (!td_allocated(dev
->td
[idx
])) {
650 struct ohci_td
*new_td
= &dev
->td
[idx
];
651 /* zero out the TD */
652 memset(new_td
, 0, sizeof(*new_td
));
653 /* mark the new TDs as unaccessed */
654 new_td
->info
= cpu_to_le32(OHCI_TD_CC_NEW
);
655 /* mark it as allocated */
657 /* record the device that its on */
658 new_td
->usb_dev
= ohci_to_usb(dev
);
663 printk(KERN_ERR
"usb-ohci: unable to allocate a TD\n");
665 } /* ohci_get_free_td() */
669 * Get a pointer (virtual) to an available TD from the given device's
670 * pool. Return NULL if none are left.
672 * NOTE: This function does not allocate and attach the dummy_td.
673 * That is done in ohci_fill_ed(). FIXME: it should probably be moved
676 static struct ohci_ed
*ohci_get_free_ed(struct ohci_device
*dev
)
680 /* FIXME: this is horribly inefficient */
681 for (idx
=0; idx
< NUM_EDS
; idx
++) {
682 if (!ed_allocated(dev
->ed
[idx
])) {
683 struct ohci_ed
*new_ed
= &dev
->ed
[idx
];
684 /* zero out the ED */
685 memset(new_ed
, 0, sizeof(*new_ed
));
686 /* all new EDs start with the SKIP bit set */
687 new_ed
->status
|= cpu_to_le32(OHCI_ED_SKIP
);
688 /* mark it as allocated */
690 new_ed
->ohci_dev
= dev
;
695 printk(KERN_ERR
"usb-ohci: unable to allocate an ED\n");
697 } /* ohci_get_free_ed() */
701 * Free an OHCI ED and all of the TDs on its list. It is assumed that
702 * this ED is not active. You should call ohci_wait_for_ed_safe()
703 * beforehand if you can't guarantee that.
705 void ohci_free_ed(struct ohci_ed
*ed
)
710 if (ed_head_td(ed
) != 0) {
711 struct ohci_td
*td
, *tail_td
, *next_td
;
713 td
= bus_to_virt(ed_head_td(ed
));
714 tail_td
= bus_to_virt(ed_tail_td(ed
));
716 next_td
= bus_to_virt(le32_to_cpup(&td
->next_td
));
724 ed
->status
&= cpu_to_le32(~(__u32
)ED_ALLOCATED
);
725 } /* ohci_free_ed() */
731 * dir = OHCI_TD_D_IN, OHCI_TD_D_OUT, or OHCI_TD_D_SETUP
732 * toggle = TOGGLE_AUTO, TOGGLE_DATA0, TOGGLE_DATA1
734 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
)
736 /* hardware fields */
737 td
->info
= cpu_to_le32(OHCI_TD_CC_NEW
|
739 (toggle
& OHCI_TD_DT
) |
741 td
->cur_buf
= (data
== NULL
) ? 0 : cpu_to_le32(virt_to_bus(data
));
742 td
->buf_end
= (len
== 0) ? 0 :
743 cpu_to_le32(virt_to_bus((char *)data
+ len
- 1));
748 td
->completed
= completed
;
751 printk(KERN_DEBUG
"ohci_fill_new_td created:\n");
756 } /* ohci_fill_new_td() */
760 * Initialize a new ED on device dev, including allocating and putting the
761 * dummy tail_td on its queue if it doesn't already have one. Any
762 * TDs on this ED other than the dummy will be lost (so there better
763 * not be any!). This assumes that the ED is Allocated and will
764 * force the Allocated bit on.
766 struct ohci_ed
*ohci_fill_ed(struct ohci_device
*dev
, struct ohci_ed
*ed
,
767 int maxpacketsize
, int lowspeed
, int endp_id
,
770 struct ohci_td
*dummy_td
;
772 if (ed_head_td(ed
) != ed_tail_td(ed
))
773 printk(KERN_ERR
"Reusing a non-empty ED %p!\n", ed
);
776 dummy_td
= ohci_get_free_td(dev
);
777 if (dummy_td
== NULL
) {
778 printk(KERN_ERR
"Error allocating dummy TD for ED %p\n", ed
);
779 return NULL
; /* no dummy available! */
781 make_dumb_td(dummy_td
); /* flag it as a dummy */
782 ed
->tail_td
= cpu_to_le32(virt_to_bus(dummy_td
));
784 dummy_td
= bus_to_virt(ed_tail_td(ed
));
785 if (!td_dummy(*dummy_td
))
786 printk(KERN_ERR
"ED %p's dummy %p is screwy\n", ed
, dummy_td
);
789 /* set the head TD to the dummy and clear the Carry & Halted bits */
790 ed
->_head_td
= ed
->tail_td
;
792 ed
->status
= cpu_to_le32(
793 ed_set_maxpacket(maxpacketsize
) |
794 ed_set_speed(lowspeed
) |
796 ((isoc_tds
== 0) ? OHCI_ED_F_NORM
: OHCI_ED_F_ISOC
));
801 } /* ohci_fill_ed() */
805 * Create a chain of Normal TDs to be used for a large data transfer
808 * The next_td parameter should be OHCI byte order bus address of the
809 * next TD to follow this chain or 0 if there is none.
811 * Returns the head TD in the chain.
813 struct ohci_td
*ohci_build_td_chain(struct ohci_device
*dev
,
814 void *data
, unsigned int len
, int dir
, __u32 toggle
,
815 int round
, int auto_free
, void* dev_id
,
816 usb_device_irq handler
, __u32 next_td
)
818 struct ohci_td
*head
, *cur_td
;
821 if (!data
|| (len
== 0))
824 /* Get the first TD */
825 head
= ohci_get_free_td(dev
);
827 printk(KERN_ERR
"usb-ohci: out of TDs\n");
833 /* AFICT, that the OHCI controller takes care of the innards of
834 * bulk & control data transfers by sending zero length
835 * packets as necessary if the transfer falls on an even packet
836 * size boundary, we don't need a special TD for that. */
838 /* check the 4096 byte alignment of the start of the data */
839 max_len
= 0x2000 - ((unsigned long)data
& 0xfff);
841 /* check if the remaining data occupies more than two pages */
842 while (len
> max_len
) {
843 struct ohci_td
*new_td
;
845 /* TODO lookup effect of rounding bit on
846 * individual TDs vs. whole TD chain transfers;
847 * disable cur_td's rounding bit here if needed. */
849 ohci_fill_new_td(cur_td
,
852 (round
? OHCI_TD_ROUND
: 0),
856 noauto_free_td(head
);
858 /* adjust the data pointer & remaining length */
862 /* allocate another td */
863 new_td
= ohci_get_free_td(dev
);
864 if (new_td
== NULL
) {
865 printk(KERN_ERR
"usb-ohci: out of TDs\n");
866 /* FIXME: free any allocated TDs */
870 /* Link the new TD to the chain & advance */
871 cur_td
->next_td
= cpu_to_le32(virt_to_bus(new_td
));
874 /* address is page-aligned now */
876 toggle
= TOGGLE_AUTO
; /* toggle Data0/1 via the ED */
879 ohci_fill_new_td(cur_td
,
882 (round
? OHCI_TD_ROUND
: 0),
886 noauto_free_td(head
);
888 /* link the given next_td to the end of this chain */
889 cur_td
->next_td
= next_td
;
891 set_td_endofchain(cur_td
);
894 } /* ohci_build_td_chain() */
898 * Compute the number of bytes that have been transferred on a given
899 * TD. Do not call this on TDs that are active on the host
902 static __u16
ohci_td_bytes_done(struct ohci_td
*td
)
905 __u32 bus_data_start
, bus_data_end
;
907 bus_data_start
= virt_to_bus(td
->data
);
908 if (!td
->data
|| !bus_data_start
)
911 /* if cur_buf is 0, all data has been transferred */
913 return le32_to_cpup(&td
->buf_end
) - bus_data_start
+ 1;
916 bus_data_end
= le32_to_cpup(&td
->cur_buf
);
918 /* is it on the same page? */
919 if ((bus_data_start
& ~0xfff) == (bus_data_end
& ~0xfff)) {
920 result
= bus_data_end
- bus_data_start
;
922 /* compute the amount transferred on the first page */
923 result
= 0x1000 - (bus_data_start
& 0xfff);
924 /* add the amount done in the second page */
925 result
+= (bus_data_end
& 0xfff);
929 } /* ohci_td_bytes_done() */
932 /**********************************
933 * OHCI interrupt list operations *
934 **********************************/
937 * Request an interrupt handler for one "pipe" of a USB device.
938 * (this function is pretty minimal right now)
940 * At the moment this is only good for input interrupts. (ie: for a
943 * Period is desired polling interval in ms. The closest, shorter
944 * match will be used. Powers of two from 1-32 are supported by OHCI.
946 * Returns: a "handle pointer" that release_irq can use to stop this
947 * interrupt. (It's really a pointer to the TD). NULL = error.
949 static void* ohci_request_irq(struct usb_device
*usb
, unsigned int pipe
,
950 usb_device_irq handler
, int period
, void *dev_id
)
952 struct ohci_device
*dev
= usb_to_ohci(usb
);
954 struct ohci_ed
*interrupt_ed
; /* endpoint descriptor for this irq */
955 int maxps
= usb_maxpacket(usb
, pipe
, usb_pipeout(pipe
));
958 /* Get an ED and TD */
959 interrupt_ed
= ohci_get_periodic_ed(dev
, period
, pipe
, 0);
960 if (interrupt_ed
== 0) {
961 interrupt_ed
= ohci_get_free_ed(dev
);
963 printk(KERN_ERR
"Out of EDs on device %p in ohci_request_irq\n", dev
);
968 * Set the max packet size, device speed, endpoint number, usb
969 * device number (function address), and type of TD.
971 ohci_fill_ed(dev
, interrupt_ed
, maxps
, usb_pipeslow(pipe
),
972 usb_pipe_endpdev(pipe
), 0 /* normal TDs */);
973 interrupt_ed
->status
&= cpu_to_le32(~OHCI_ED_SKIP
);
975 /* Assimilate the new ED into the collective */
976 ohci_add_periodic_ed(dev
->ohci
, interrupt_ed
, period
);
980 printk(KERN_DEBUG
"ohci_request irq: using ED %p [%x %x %x %x]\n",
981 interrupt_ed
, FIELDS_OF_ED(interrupt_ed
));
982 printk(KERN_DEBUG
" for dev %d pipe %x period %d\n", usb
->devnum
,
987 td
= ohci_get_free_td(dev
);
989 printk(KERN_ERR
"Out of TDs in ohci_request_irq\n");
990 ohci_free_ed(interrupt_ed
);
995 if (maxps
> sizeof(dev
->data
))
996 maxps
= sizeof(dev
->data
);
997 ohci_fill_new_td(td
, td_set_dir_out(usb_pipeout(pipe
)),
1002 set_td_endofchain(td
);
1005 * Put the TD onto our ED and make sure its ready to run
1008 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
1009 td
= ohci_add_tds_to_ed(td
, interrupt_ed
);
1010 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
1013 } /* ohci_request_irq() */
1016 * Release an interrupt handler previously allocated using
1017 * ohci_request_irq. This function does no validity checking, so make
1018 * sure you're not releasing an already released handle as it may be
1019 * in use by something else..
1021 * This function can NOT be called from an interrupt.
1023 int ohci_release_irq(struct usb_device
*usb
, void* handle
)
1025 struct ohci_device
*dev
;
1026 struct ohci_td
*int_td
;
1027 struct ohci_ed
*int_ed
;
1031 printk("usb-ohci: Releasing irq handle %p\n", handle
);
1034 int_td
= (struct ohci_td
*)handle
;
1036 return USB_ST_INTERNALERROR
;
1038 dev
= usb_to_ohci(int_td
->usb_dev
);
1039 int_ed
= int_td
->ed
;
1041 ohci_remove_periodic_ed(dev
->ohci
, int_ed
);
1043 /* Tell the driver that the IRQ has been killed. */
1044 /* Passing NULL in the "buffer" void* along with the
1045 * USB_ST_REMOVED status is the signal. */
1046 if (int_td
->completed
!= NULL
)
1047 int_td
->completed(USB_ST_REMOVED
, NULL
, 0, int_td
->dev_id
);
1049 /* Free the ED (& TD) */
1050 ohci_free_ed(int_ed
);
1052 return USB_ST_NOERROR
;
1053 } /* ohci_release_irq() */
1056 /**********************************************************************
1057 * Generic bulk/control/isochronous transfer processing
1058 **********************************************************************/
1062 * Queue a generic OHCI data transfer, specifying the type in ed_type.
1064 * - data_td_toggle specifies the data toggle value to start with.
1065 * - round specifies if the transfer should be allowed to fall short.
1066 * - autofree determines if the data TDs are automatically freed.
1067 * - setup_td and status_td will be prepended and appended to the TD
1068 * chain respectively. Control transfers need these..
1070 * - handler will be called upon completion of every data TD it
1071 * needs to check if the transfer is really done or not.
1073 * A handle to the transfer is returned.
1075 static void * ohci_generic_trans(struct usb_device
*usb_dev
, int pipe
,
1076 int data_td_toggle
, int round
, int autofree
,
1077 void *dev_id
, usb_device_irq handler
, void *data
, int len
,
1078 int ed_type
, struct ohci_td
*setup_td
, struct ohci_td
*status_td
)
1080 struct ohci_device
*dev
= usb_to_ohci(usb_dev
);
1081 struct ohci_ed
*trans_ed
;
1082 struct ohci_td
*data_td
, *head_td
;
1083 unsigned long flags
;
1087 printk(KERN_DEBUG
"ohci_request_trans()\n");
1090 trans_ed
= ohci_get_free_ed(dev
);
1092 printk("usb-ohci: couldn't get ED for dev %p\n", dev
);
1096 /* If this transfer has a data phase, allocate TDs for it */
1098 __u32 next_td
= status_td
? cpu_to_le32(virt_to_bus(status_td
)) : 0;
1099 /* allocate & fill in the TDs for this request */
1100 data_td
= ohci_build_td_chain( dev
, data
, len
,
1109 printk(KERN_ERR
"usb-ohci: build_td_chain failed for dev %p, pipe 0x%x\n", dev
, pipe
);
1110 goto gt_free_and_exit
;
1113 data_td
= status_td
;
1116 /* start with the setup TD if there is one */
1119 setup_td
->next_td
= cpu_to_le32(virt_to_bus(data_td
));
1123 printk(KERN_ERR
"usb-ohci: lonely setup_td detected\n");
1125 goto gt_free_and_exit
;
1133 printk(KERN_ERR
"usb-ohci: no TDs in transfer\n");
1135 goto gt_free_and_exit
; /* nothing to do */
1138 /* Set the max packet size, device speed, endpoint number, usb
1139 * device number (function address), and type of TD. */
1140 ohci_fill_ed(dev
, trans_ed
,
1141 usb_maxpacket(usb_dev
, pipe
, usb_pipeout(pipe
)),
1143 usb_pipe_endpdev(pipe
),
1144 (ed_type
== HCD_ED_ISOC
) );
1146 /* initialize the toggle carry flag in the ED */
1147 if (usb_gettoggle(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
)))
1148 ohci_ed_set_carry(trans_ed
);
1151 * Add the TDs to the ED, remove the skip flag
1153 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
1154 head_td
= ohci_add_tds_to_ed(head_td
, trans_ed
);
1155 trans_ed
->status
&= cpu_to_le32(~OHCI_ED_SKIP
);
1156 /* ohci_unhalt_ed(trans_ed); */
1157 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
1161 /* complete transaction debugging output (before) */
1162 printk(KERN_DEBUG
" Trans ED %lx:\n", virt_to_bus(trans_ed
));
1163 show_ohci_ed(trans_ed
);
1164 printk(KERN_DEBUG
" Trans TD chain:\n");
1165 show_ohci_td_chain(head_td
);
1169 /* Give the ED to the HC on the appropriate list */
1172 /* Isochronous transfers have a 1ms period */
1173 ohci_add_periodic_ed(dev
->ohci
, trans_ed
, 1);
1175 case HCD_ED_CONTROL
:
1176 ohci_add_control_ed(dev
->ohci
, trans_ed
);
1179 ohci_add_bulk_ed(dev
->ohci
, trans_ed
);
1183 printk(KERN_ERR
"usb-ohci: bad ED type %d\n", ed_type
);
1185 goto gt_free_and_exit
;
1193 ohci_free_ed(trans_ed
);
1195 } /* ohci_generic_trans() */
1199 * Terminate a transfer initiated by ohci_generic_trans()
1201 * This function is NOT safe to call from an interrupt.
1203 static int ohci_terminate_trans(struct usb_device
*usb_dev
, void *handle
, int ed_type
)
1205 struct ohci_ed
*req_ed
= (struct ohci_ed
*) handle
;
1206 struct ohci_device
*dev
= usb_to_ohci(usb_dev
);
1207 struct ohci_regs
*regs
= dev
->ohci
->regs
;
1212 /* stop the transfer & collect the number of bytes */
1213 /* (this is the non-interrupt safe function call) */
1214 ohci_wait_for_ed_safe(regs
, req_ed
, ed_type
);
1216 /* Remove the ED from the appropriate HC list */
1219 ohci_remove_periodic_ed(dev
->ohci
, req_ed
);
1221 case HCD_ED_CONTROL
:
1222 ohci_remove_control_ed(dev
->ohci
, req_ed
);
1225 ohci_remove_bulk_ed(dev
->ohci
, req_ed
);
1229 ohci_free_ed(req_ed
); /* return it to the pool */
1232 } /* ohci_terminate_trans() */
1235 /**********************************************************************
1236 * Control transfer processing
1237 **********************************************************************/
1240 static DECLARE_WAIT_QUEUE_HEAD(control_wakeup
);
1243 * This is the handler that gets called when a control transaction
1246 * This function is called from the interrupt handler.
1248 static int ohci_control_completed(int stats
, void *buffer
, int len
, void *dev_id
)
1250 /* pass the TDs completion status back to control_msg */
1252 int *completion_status
= (int *)dev_id
;
1253 *completion_status
= stats
;
1256 wake_up(&control_wakeup
);
1258 } /* ohci_control_completed() */
1262 * Send or receive a control message on a "pipe"
1264 * The cmd parameter is a pointer to the 8 byte setup command to be
1267 * A control message contains:
1268 * - The command itself
1269 * - An optional data phase (if len > 0)
1270 * - Status complete phase
1272 * This function can NOT be called from an interrupt.
1274 static int ohci_control_msg(struct usb_device
*usb_dev
, unsigned int pipe
,
1275 devrequest
*cmd
, void *data
, int len
)
1277 struct ohci_device
*dev
= usb_to_ohci(usb_dev
);
1279 struct ohci_td
*setup_td
, *status_td
;
1280 DECLARE_WAITQUEUE(wait
, current
);
1281 int completion_status
= -1;
1284 /* byte-swap fields of cmd if necessary */
1286 cpu_to_le16s(&our_cmd
.value
);
1287 cpu_to_le16s(&our_cmd
.index
);
1288 cpu_to_le16s(&our_cmd
.length
);
1292 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
);
1295 /* get a TD to send this control message with */
1296 setup_td
= ohci_get_free_td(dev
);
1298 printk(KERN_ERR
"usb-ohci: couldn't get TD for dev %p [cntl setup]\n", dev
);
1299 return USB_ST_INTERNALERROR
;
1303 * Set the not accessed condition code, allow odd sized data,
1304 * and set the data transfer type to SETUP. Setup DATA always
1305 * uses a DATA0 packet.
1307 * The setup packet contains a devrequest (usb.h) which
1308 * will always be 8 bytes long.
1310 ohci_fill_new_td(setup_td
, OHCI_TD_D_SETUP
, TOGGLE_DATA0
,
1312 &our_cmd
, 8, /* cmd is always 8 bytes long */
1313 &completion_status
, NULL
);
1315 /* Allocate a TD for the control xfer status */
1316 status_td
= ohci_get_free_td(dev
);
1318 printk("usb-ohci: couldn't get TD for dev %p [cntl status]\n", dev
);
1319 ohci_free_td(setup_td
);
1320 return USB_ST_INTERNALERROR
;
1323 /* The control status packet always uses a DATA1
1324 * Give "dev_id" the address of completion_status so that the
1325 * TDs status can be passed back to us from the IRQ. */
1326 ohci_fill_new_td(status_td
,
1327 td_set_dir_in(usb_pipeout(pipe
) | (len
== 0)),
1330 NULL
/* data */, 0 /* data len */,
1331 &completion_status
, ohci_control_completed
);
1332 set_td_endofchain(status_td
);
1333 status_td
->next_td
= 0; /* end of TDs */
1336 * Start the control transaction..
1337 * XXX should this come so soon? or... XXX
1338 * XXX should it be put into generic_trans? XXX
1340 current
->state
= TASK_UNINTERRUPTIBLE
;
1341 add_wait_queue(&control_wakeup
, &wait
);
1343 trans_handle
= ohci_generic_trans(usb_dev
, pipe
,
1344 TOGGLE_DATA1
, 1 /* round */, 1 /* autofree */,
1345 &completion_status
, NULL
/* no data td handler */,
1346 data
, len
, HCD_ED_CONTROL
,
1347 setup_td
, status_td
);
1349 /* did something go wrong? return an error */
1350 if (!trans_handle
) {
1351 remove_wait_queue(&control_wakeup
, &wait
);
1352 ohci_free_td(setup_td
);
1353 ohci_free_td(status_td
);
1354 return USB_ST_INTERNALERROR
;
1357 /* wait a "reasonable" amount of time for it to complete */
1358 schedule_timeout(HZ
);
1360 remove_wait_queue(&control_wakeup
, &wait
);
1364 * NOTE! this debug code blatently assumes that the handle returned
1365 * by ohci_generic_trans is the pointer to this transfers ED.
1366 * (which it is at the moment)
1367 * Also, since the TDs were autofreed, there is no guarantee that
1368 * they haven't been reclaimed by another transfer by now...
1370 if (completion_status
!= 0) {
1371 const char *what
= (completion_status
< 0)? "timed out":
1372 cc_names
[completion_status
& 0xf];
1373 printk(KERN_ERR
"ohci_control_msg: %s on pipe %x cmd %x %x %x %x %x",
1374 what
, pipe
, cmd
->requesttype
, cmd
->request
,
1375 cmd
->value
, cmd
->index
, cmd
->length
);
1376 if (usb_pipeout(pipe
) && len
> 0) {
1379 for (i
= 0; i
< 16 && i
< len
; ++i
)
1380 printk(" %.2x", ((unsigned char *)data
)[i
]);
1385 if (MegaDebug
&& completion_status
< 0) {
1386 struct ohci_ed
*control_ed
= (struct ohci_ed
*) trans_handle
;
1387 printk(KERN_DEBUG
"control_ed at %p:\n", control_ed
);
1388 show_ohci_ed(control_ed
);
1389 if (ed_head_td(control_ed
) != ed_tail_td(control_ed
))
1390 show_ohci_td_chain(bus_to_virt(ed_head_td(control_ed
)));
1391 printk(KERN_DEBUG
"setup TD at %p:\n", setup_td
);
1392 show_ohci_td(setup_td
);
1394 } else if (!usb_pipeout(pipe
)) {
1395 unsigned char *q
= data
;
1397 printk(KERN_DEBUG
"ctrl msg %x %x %x %x %x on pipe %x returned:",
1398 cmd
->requesttype
, cmd
->request
, cmd
->value
, cmd
->index
,
1400 for (i
= 0; i
< len
; ++i
) {
1402 printk("\n" KERN_DEBUG
);
1403 printk(" %x", q
[i
]);
1409 struct ohci_ed
*control_ed
= (struct ohci_ed
*) trans_handle
;
1410 /* complete transaction debugging output (after) */
1411 printk(KERN_DEBUG
" *after* Control ED %lx:\n", virt_to_bus(control_ed
));
1412 show_ohci_ed(control_ed
);
1413 printk(KERN_DEBUG
" *after* Control TD chain:\n");
1414 show_ohci_td_chain(setup_td
);
1415 printk(KERN_DEBUG
" *after* OHCI Controller Status:\n");
1416 show_ohci_status(dev
->ohci
);
1420 /* no TD cleanup, the TDs were auto-freed as they finished */
1422 /* remove the generic_trans ED from the HC and free it */
1423 ohci_terminate_trans(usb_dev
, trans_handle
, HCD_ED_CONTROL
);
1425 if (completion_status
< 0)
1426 completion_status
= USB_ST_TIMEOUT
;
1427 return completion_status
;
1428 } /* ohci_control_msg() */
1431 /**********************************************************************
1432 * Bulk transfer processing
1433 **********************************************************************/
1437 * Request to send or receive bulk data. The handler() function
1438 * will be called as each OHCI TD in the transfer completes or is
1439 * aborted due to an error.
1441 * IMPORTANT NOTE: This means the handler may be called multiple
1442 * times for a large (more than one 4096 byte page) request until
1443 * the transfer is finished.
1445 * Returns: a pointer to the ED being used for this request as the
1446 * bulk request handle.
1448 static void * ohci_request_bulk(struct usb_device
*usb_dev
, unsigned int pipe
, usb_device_irq handler
, void* data
, int len
, void* dev_id
)
1451 struct ohci_device
*dev
= usb_to_ohci(usb_dev
);
1453 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
);
1456 return ohci_generic_trans(usb_dev
, pipe
,
1458 0 /* round */, 1 /* autofree */,
1459 dev_id
, handler
, data
, len
,
1461 NULL
/* no setup_td */, NULL
/* no status_td */ );
1462 } /* ohci_request_bulk() */
1466 * Terminate a bulk transfer requested using ohci_request_bulk.
1468 * This function is NOT safe to call from an interrupt.
1470 static int ohci_terminate_bulk(struct usb_device
*usb_dev
, void * handle
)
1472 return ohci_terminate_trans(usb_dev
, handle
, HCD_ED_CONTROL
);
1473 } /* ohci_terminate_bulk() */
1477 * Internal state for an ohci_bulk_request_msg
1479 struct ohci_bulk_msg_request_state
{
1480 struct usb_device
*usb_dev
;
1481 unsigned int pipe
; /* usb "pipe" */
1482 void *data
; /* ptr to data */
1483 int length
; /* length to transfer */
1484 int _bytes_done
; /* bytes transferred so far */
1485 unsigned long *bytes_transferred_p
; /* where to increment */
1486 void *dev_id
; /* pass to the completion handler */
1487 usb_device_irq completion
; /* completion handler */
1491 * this handles the individual TDs of a (possibly) larger bulk
1492 * request. It keeps track of the total bytes transferred, calls the
1493 * final completion handler, etc.
1495 static int ohci_bulk_msg_td_handler(int stats
, void *buffer
, int len
, void *dev_id
)
1497 struct ohci_bulk_msg_request_state
*req
;
1499 req
= (struct ohci_bulk_msg_request_state
*) dev_id
;
1503 printk(KERN_DEBUG
"ohci_bulk_td_handler stats %x, buffer %p, len %d, req %p\n", stats
, buffer
, len
, req
);
1506 /* only count TDs that were completed successfully */
1507 if (stats
== USB_ST_NOERROR
|| stats
== USB_ST_DATAUNDERRUN
) /*DEN*/
1508 req
->_bytes_done
+= len
;
1511 if (MegaDebug
&& req
->_bytes_done
) {
1513 printk(KERN_DEBUG
" %d bytes, bulk data:", req
->_bytes_done
);
1514 for (i
= 0; i
< 16 && i
< req
->_bytes_done
; ++i
)
1515 printk(" %.2x", ((unsigned char *)buffer
)[i
]);
1516 if (i
< req
->_bytes_done
)
1522 /* call the real completion handler when done or on an error */
1523 if ((stats
!= USB_ST_NOERROR
) ||
1524 (req
->_bytes_done
>= req
->length
&& req
->completion
!= NULL
)) {
1525 *req
->bytes_transferred_p
+= req
->_bytes_done
;
1528 printk(KERN_DEBUG
"usb-ohci: bulk request %p ending\n", req
);
1530 req
->completion(stats
, buffer
, req
->_bytes_done
, req
->dev_id
);
1533 return 0; /* do not re-queue the TD */
1534 } /* ohci_bulk_msg_td_handler() */
1538 * Request to send or receive bulk data for a blocking bulk_msg call.
1540 * bulk_request->bytes_transferred_p is a pointer to an integer that
1541 * will be set to the number of bytes that have been successfully
1542 * transferred upon completion. The interrupt handler will update it
1543 * after each internal TD completes successfully.
1545 static struct ohci_ed
* ohci_request_bulk_msg(struct ohci_bulk_msg_request_state
*bulk_request
)
1547 /* initialize the internal counter */
1548 bulk_request
->_bytes_done
= 0;
1550 return ohci_request_bulk(bulk_request
->usb_dev
, bulk_request
->pipe
,
1551 ohci_bulk_msg_td_handler
,
1552 bulk_request
->data
, bulk_request
->length
,
1554 } /* ohci_request_bulk_msg() */
1557 static DECLARE_WAIT_QUEUE_HEAD(bulk_wakeup
);
1560 static int ohci_bulk_msg_completed(int stats
, void *buffer
, int len
, void *dev_id
)
1563 printk("ohci_bulk_msg_completed %x, %p, %d, %p\n", stats
, buffer
, len
, dev_id
);
1565 if (dev_id
!= NULL
) {
1566 int *completion_status
= (int *)dev_id
;
1567 *completion_status
= stats
;
1570 wake_up(&bulk_wakeup
);
1571 return 0; /* don't requeue the TD */
1572 } /* ohci_bulk_msg_completed() */
1575 static int ohci_bulk_msg(struct usb_device
*usb_dev
, unsigned int pipe
, void *data
, int len
, unsigned long *bytes_transferred_p
)
1577 DECLARE_WAITQUEUE(wait
, current
);
1578 int completion_status
= USB_ST_INTERNALERROR
;
1579 struct ohci_bulk_msg_request_state req
;
1580 struct ohci_ed
*req_ed
;
1584 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
);
1587 /* initialize bytes transferred to nothing */
1588 *bytes_transferred_p
= 0;
1590 /* Hopefully this is similar to the "URP" (USB Request Packet) code
1591 * that michael gee is working on... */
1592 req
.usb_dev
= usb_dev
;
1596 req
.bytes_transferred_p
= bytes_transferred_p
;
1597 req
.dev_id
= &completion_status
;
1598 req
.completion
= ohci_bulk_msg_completed
;
1599 if (bytes_transferred_p
)
1600 *bytes_transferred_p
= 0;
1602 if (usb_endpoint_halted(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
))
1603 && usb_clear_halt(usb_dev
, usb_pipeendpoint(pipe
) | (pipe
& 0x80)))
1604 return USB_ST_STALL
;
1607 * Start the transaction..
1609 current
->state
= TASK_UNINTERRUPTIBLE
;
1610 add_wait_queue(&bulk_wakeup
, &wait
);
1612 req_ed
= ohci_request_bulk_msg(&req
);
1614 /* FIXME this should to wait for a caller specified time... */
1615 schedule_timeout(HZ
*5);
1617 /* completion_status will only stay in this state of the
1618 * request never finished */
1619 if (completion_status
== USB_ST_INTERNALERROR
) {
1620 struct ohci_device
*dev
= usb_to_ohci(usb_dev
);
1621 struct ohci_regs
*regs
= dev
->ohci
->regs
;
1624 printk(KERN_DEBUG
"ohci_bulk_msg timing out\n");
1626 /* XXX This code should go into a function used to stop
1627 * a previously requested bulk transfer. -greg */
1629 /* stop the transfer & collect the number of bytes */
1630 ohci_wait_for_ed_safe(regs
, req_ed
, HCD_ED_BULK
);
1632 /* Get the number of bytes transferred out of the head TD
1633 * on the ED if it didn't finish while we were waiting. */
1634 if ( ed_head_td(req_ed
) &&
1635 (ed_head_td(req_ed
) != ed_tail_td(req_ed
)) ) {
1636 struct ohci_td
*partial_td
;
1637 partial_td
= bus_to_virt(ed_head_td(req_ed
));
1641 show_ohci_td(partial_td
);
1644 /* Record the bytes as transferred */
1645 *bytes_transferred_p
+= ohci_td_bytes_done(partial_td
);
1647 /* If there was an unreported error, return it.
1648 * Otherwise return a timeout */
1649 completion_status
= OHCI_TD_CC_GET(partial_td
->info
);
1650 if (completion_status
== USB_ST_NOERROR
) {
1651 completion_status
= USB_ST_TIMEOUT
;
1657 remove_wait_queue(&bulk_wakeup
, &wait
);
1659 /* remove the ED from the HC */
1660 ohci_remove_bulk_ed(usb_to_ohci(usb_dev
)->ohci
, req_ed
);
1662 /* save the toggle value back into the usb_dev */
1663 usb_settoggle(usb_dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
),
1664 ohci_ed_carry(req_ed
));
1666 ohci_free_ed(req_ed
); /* return it to the pool */
1669 if (completion_status
!= 0 || MegaDebug
)
1670 printk(KERN_DEBUG
"ohci_bulk_msg done, status %x (bytes_transferred = %ld).\n", completion_status
, *bytes_transferred_p
);
1673 return completion_status
;
1674 } /* ohci_bulk_msg() */
1682 * Allocate a new USB device to be attached to an OHCI controller
1684 static struct usb_device
*ohci_usb_allocate(struct usb_device
*parent
)
1686 struct usb_device
*usb_dev
;
1687 struct ohci_device
*dev
;
1691 * Allocate the generic USB device
1693 usb_dev
= kmalloc(sizeof(*usb_dev
), GFP_KERNEL
);
1697 memset(usb_dev
, 0, sizeof(*usb_dev
));
1700 * Allocate an OHCI device (EDs and TDs for this device)
1702 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
1708 memset(dev
, 0, sizeof(*dev
));
1710 /* Initialize all EDs in a new device with the skip flag so that
1711 * they are ignored by the controller until set otherwise. */
1712 for (idx
= 0; idx
< NUM_EDS
; ++idx
) {
1713 dev
->ed
[idx
].status
= cpu_to_le32(OHCI_ED_SKIP
);
1717 * Link them together
1719 usb_dev
->hcpriv
= dev
;
1723 * Link the device to its parent (hub, etc..) if any.
1725 usb_dev
->parent
= parent
;
1728 usb_dev
->bus
= parent
->bus
;
1729 dev
->ohci
= usb_to_ohci(parent
)->ohci
;
1733 } /* ohci_usb_allocate() */
1737 * Free a usb device.
1739 * TODO This function needs to take better care of the EDs and TDs, etc.
1741 static int ohci_usb_deallocate(struct usb_device
*usb_dev
)
1743 struct ohci_device
*dev
= usb_to_ohci(usb_dev
);
1745 ohci_remove_device(dev
->ohci
, usb_dev
->devnum
);
1747 /* kfree(usb_to_ohci(usb_dev)); */
1748 /* kfree(usb_dev); */
1753 static void *ohci_alloc_isochronous (struct usb_device
*usb_dev
, unsigned int pipe
, void *data
, int len
, int maxsze
, usb_device_irq completed
, void *dev_id
)
1758 static void ohci_delete_isochronous (struct usb_device
*dev
, void *_isodesc
)
1763 static int ohci_sched_isochronous (struct usb_device
*usb_dev
, void *_isodesc
, void *_pisodesc
)
1765 return USB_ST_NOTSUPPORTED
;
1768 static int ohci_unsched_isochronous (struct usb_device
*usb_dev
, void *_isodesc
)
1770 return USB_ST_NOTSUPPORTED
;
1773 static int ohci_compress_isochronous (struct usb_device
*usb_dev
, void *_isodesc
)
1775 return USB_ST_NOTSUPPORTED
;
1780 * functions for the generic USB driver
1782 struct usb_operations ohci_device_operations
= {
1784 ohci_usb_deallocate
,
1790 ohci_terminate_bulk
,
1791 ohci_alloc_isochronous
,
1792 ohci_delete_isochronous
,
1793 ohci_sched_isochronous
,
1794 ohci_unsched_isochronous
,
1795 ohci_compress_isochronous
1800 * Reset an OHCI controller. Returns >= 0 on success.
1802 * Afterwards the HC will be in the "suspend" state which prevents you
1803 * from writing to some registers. Bring it to the operational state
1806 static int reset_hc(struct ohci
*ohci
)
1808 int timeout
= 10000; /* prevent an infinite loop */
1811 printk(KERN_INFO
"usb-ohci: resetting HC %p\n", ohci
);
1814 writel(~0x0, &ohci
->regs
->intrdisable
); /* Disable HC interrupts */
1815 writel(1, &ohci
->regs
->cmdstatus
); /* HC Reset */
1816 writel_mask(0x3f, &ohci
->regs
->control
); /* move to UsbReset state */
1818 while ((readl(&ohci
->regs
->cmdstatus
) & OHCI_CMDSTAT_HCR
) != 0) {
1820 printk(KERN_ERR
"usb-ohci: USB HC reset timed out!\n");
1826 printk(KERN_DEBUG
"usb-ohci: HC %p reset.\n", ohci
);
1833 * Reset and start an OHCI controller. Returns >= 0 on success.
1835 static int start_hc(struct ohci
*ohci
)
1839 __u32 what_to_enable
;
1841 struct ohci_device
*root_hub
=usb_to_ohci(ohci
->bus
->root_hub
);
1844 printk(KERN_DEBUG
"entering start_hc %p\n", ohci
);
1847 if (reset_hc(ohci
) < 0)
1850 /* restore registers cleared by the reset */
1851 writel(virt_to_bus(root_hub
->hcca
), &ohci
->regs
->hcca
);
1854 * fminterval has to be 11999 (it can be adjusted +/- 1
1855 * to sync with other things if necessary).
1859 /* Start periodic transfers at 90% of fminterval (fmremaining
1860 * counts down; this will put them in the first 10% of the
1862 writel((fminterval
* 9) / 10, &ohci
->regs
->periodicstart
);
1864 /* Set largest data packet counter and frame interval. */
1865 fminterval
|= ((fminterval
- 210) * 6 / 7) << 16;
1866 writel(fminterval
, &ohci
->regs
->fminterval
);
1868 /* Set low-speed threshold (value from MacOS) */
1869 writel(1576, &ohci
->regs
->lsthresh
);
1872 * FNO (frame number overflow) could be enabled... they
1873 * occur every 32768 frames (every 32-33 seconds). This is
1874 * useful for debugging and as a bus heartbeat. -greg
1876 /* Choose the interrupts we care about */
1877 what_to_enable
= OHCI_INTR_MIE
|
1878 #ifdef OHCI_RHSC_INT
1881 /* | OHCI_INTR_FNO */
1883 writel( what_to_enable
, &ohci
->regs
->intrenable
);
1885 /* Enter the USB Operational state & start the frames a flowing.. */
1886 writel_set(OHCI_USB_OPER
, &ohci
->regs
->control
);
1888 /* Enable control lists */
1889 writel_set(OHCI_USB_IE
| OHCI_USB_CLE
| OHCI_USB_BLE
, &ohci
->regs
->control
);
1891 /* Force global power enable -gal@cs.uni-magdeburg.de */
1893 * This turns on global power switching for all the ports
1894 * and tells the HC that all of the ports should be powered on
1897 * TODO: This could be battery draining for laptops.. We
1898 * should implement power switching.
1900 writel_set( OHCI_ROOT_A_NPS
, &ohci
->regs
->roothub
.a
);
1901 writel_mask( ~((__u32
)OHCI_ROOT_A_PSM
), &ohci
->regs
->roothub
.a
);
1903 /* Turn on power to the root hub ports (thanks Roman!) */
1904 writel( OHCI_ROOT_LPSC
, &ohci
->regs
->roothub
.status
);
1906 printk(KERN_INFO
"usb-ohci: host controller operational\n");
1913 * Reset a root hub port
1915 static void ohci_reset_port(struct ohci
*ohci
, unsigned int port
)
1919 /* Don't allow overflows. */
1920 if (port
>= MAX_ROOT_PORTS
) {
1921 printk(KERN_ERR
"usb-ohci: bad port #%d in ohci_reset_port\n", port
);
1922 port
= MAX_ROOT_PORTS
-1;
1925 writel(PORT_PRS
, &ohci
->regs
->roothub
.portstatus
[port
]); /* Reset */
1928 * Wait for the reset to complete.
1932 /* check port status to see that the reset completed */
1933 status
= readl(&ohci
->regs
->roothub
.portstatus
[port
]);
1934 if (status
& PORT_PRS
) {
1935 /* reset failed, try harder? */
1936 printk(KERN_ERR
"usb-ohci: port %d reset failed, retrying\n", port
);
1937 writel(PORT_PRS
, &ohci
->regs
->roothub
.portstatus
[port
]);
1941 /* TODO we might need to re-enable the port here or is that
1942 * done elsewhere? */
1944 } /* ohci_reset_port */
1948 * This gets called if the connect status on the root hub changes.
1950 static void ohci_connect_change(struct ohci
* ohci
, int port
)
1952 struct usb_device
*usb_dev
;
1953 struct ohci_device
*dev
;
1954 struct ohci_device
*root_hub
=usb_to_ohci(ohci
->bus
->root_hub
);
1955 /* memory I/O address of the port status register */
1956 __u32
*portaddr
= &ohci
->regs
->roothub
.portstatus
[port
];
1960 printk(KERN_DEBUG
"ohci_connect_change on port %d\n", port
);
1964 * Because of the status change we have to forget
1965 * everything we think we know about the device
1966 * on this root hub port. It may have changed.
1968 usb_disconnect(root_hub
->usb
->children
+ port
);
1970 portstatus
= readl(portaddr
);
1972 /* disable the port if nothing is connected */
1973 if (!(portstatus
& PORT_CCS
)) {
1974 writel(PORT_CCS
, portaddr
);
1975 /* We need to reset the CSC bit -after- disabling the
1976 * port because it causes the CSC bit to come on
1979 writel(PORT_CSC
, portaddr
);
1981 printk(KERN_DEBUG
"ohci port %d disabled, nothing connected.\n", port
);
1987 * Allocate a device for the new thingy that's been attached
1989 usb_dev
= ohci_usb_allocate(root_hub
->usb
);
1990 dev
= usb_dev
->hcpriv
;
1994 usb_connect(dev
->usb
);
1996 /* link it into the bus's device tree */
1997 root_hub
->usb
->children
[port
] = usb_dev
;
1999 wait_ms(200); /* wait for powerup; XXX is this needed? */
2000 ohci_reset_port(ohci
, port
);
2002 /* Get information on speed by using LSD */
2003 usb_dev
->slow
= readl(portaddr
) & PORT_LSDA
? 1 : 0;
2006 * Do generic USB device tree processing on the new device.
2008 usb_new_device(usb_dev
);
2010 } /* ohci_connect_change() */
2014 * This gets called when the root hub configuration
2015 * has changed. Just go through each port, seeing if
2016 * there is something interesting happening.
2018 static void ohci_check_configuration(struct ohci
*ohci
)
2020 struct ohci_regs
*regs
= ohci
->regs
;
2022 int maxport
= readl(&ohci
->regs
->roothub
) & 0xff;
2023 __u32 rh_change_flags
= PORT_CSC
| PORT_PESC
; /* root hub status changes */
2026 printk(KERN_DEBUG
"entering ohci_check_configuration %p\n", ohci
);
2030 __u32
*portstatus_p
= ®s
->roothub
.portstatus
[num
];
2031 if (readl(portstatus_p
) & rh_change_flags
) {
2032 /* acknowledge the root hub status changes */
2033 writel_set(rh_change_flags
, portstatus_p
);
2034 /* disable the port if nothing is on it */
2035 /* check the port for a nifty device */
2036 ohci_connect_change(ohci
, num
);
2038 } while (++num
< maxport
);
2041 printk(KERN_DEBUG
"leaving ohci_check_configuration %p\n", ohci
);
2043 } /* ohci_check_configuration() */
2048 * Check root hub port status and wake the control thread up if
2049 * anything has changed.
2051 * This function is called from the interrupt handler.
2053 static void ohci_root_hub_events(struct ohci
*ohci
)
2056 struct ohci_device
*root_hub
=usb_to_ohci(ohci
->bus
->root_hub
);
2057 int maxport
= root_hub
->usb
->maxchild
;
2059 if (!waitqueue_active(&ohci_configure
))
2062 __u32
*portstatus_p
= &ohci
->regs
->roothub
.portstatus
[num
];
2063 if (readl(portstatus_p
) & PORT_CSC
) {
2064 if (waitqueue_active(&ohci_configure
))
2065 wake_up(&ohci_configure
);
2068 } while (++num
< maxport
);
2070 } /* ohci_root_hub_events() */
2074 * The done list is in reverse order; we need to process TDs in the
2075 * order they were finished (FIFO). This function builds the FIFO
2076 * list using the next_dl_td pointer.
2078 * This function originally by Roman Weissgaerber (weissg@vienna.at)
2080 * This function is called from the interrupt handler.
2082 static struct ohci_td
* ohci_reverse_donelist(struct ohci
* ohci
)
2085 struct ohci_device
*root_hub
=usb_to_ohci(ohci
->bus
->root_hub
);
2086 struct ohci_hcca
*hcca
= root_hub
->hcca
;
2087 struct ohci_td
*td_list
= NULL
;
2088 struct ohci_td
*td_rev
= NULL
;
2090 td_list_hc
= le32_to_cpup(&hcca
->donehead
) & 0xfffffff0;
2094 td_list
= (struct ohci_td
*) bus_to_virt(td_list_hc
);
2095 td_list
->next_dl_td
= td_rev
;
2097 td_list_hc
= le32_to_cpup(&td_list
->next_td
) & 0xfffffff0;
2101 } /* ohci_reverse_donelist() */
2104 * Look at the ed (and td if necessary)
2105 * and return its direction as 0 = IN, 1 = OUT.
2107 int ed_get_dir (struct ohci_ed
*ed
, struct ohci_td
*td
)
2111 status
= le32_to_cpu(ed
->status
) & OHCI_ED_D
; /* keep only the Direction bits */
2112 if (status
== OHCI_ED_D_IN
) return 0;
2113 if (status
== OHCI_ED_D_OUT
) return 1;
2115 /* but if status == 0 or 3, look at the td for the Direction */
2116 status
= le32_to_cpu(td
->info
) & OHCI_TD_D
; /* keep only the Direction bits */
2117 if (status
== OHCI_TD_D_IN
) return 0;
2122 * Collect this interrupt's goodies off of the list of finished TDs
2123 * that the OHCI controller is kind enough to setup for us.
2125 * This function is called from the interrupt handler.
2127 static void ohci_reap_donelist(struct ohci
*ohci
)
2129 struct ohci_td
*td
; /* used for walking the list */
2131 /* um... isn't this dangerous to do in an interrupt handler? -greg */
2133 spin_lock(&ohci_edtd_lock
);
2135 /* create the FIFO ordered donelist */
2136 td
= ohci_reverse_donelist(ohci
);
2138 while (td
!= NULL
) {
2139 struct ohci_td
*next_td
= td
->next_dl_td
;
2140 int cc
= OHCI_TD_CC_GET(le32_to_cpup(&td
->info
));
2141 struct ohci_ed
*ed
= td
->ed
;
2144 printk(KERN_ERR
"usb-ohci: yikes! reaping dummy TD\n");
2147 if (cc
!= 0 && MegaDebug
) {
2148 printk("cc=%s on td %p (ed %p)\n", cc_names
[cc
], td
, ed
);
2151 if (ed_head_td(ed
) != ed_tail_td(ed
))
2152 show_ohci_td_chain(bus_to_virt(ed_head_td(ed
)));
2156 if (cc
== USB_ST_STALL
) {
2157 /* mark endpoint as halted */
2158 usb_endpoint_halt(ed
->ohci_dev
->usb
, ed_get_en(ed
), ed_get_dir(ed
, td
));
2161 if (cc
!= 0 && ohci_ed_halted(ed
) && !td_endofchain(*td
)) {
2163 * There was an error on this TD and the ED
2164 * is halted, and this was not the last TD
2165 * of the transaction, so there will be TDs
2166 * to clean off the ED.
2168 struct ohci_td
*tail_td
= bus_to_virt(ed_tail_td(ed
));
2169 struct ohci_td
*ntd
;
2172 td
= ntd
= bus_to_virt(ed_head_td(ed
));
2173 while (td
!= tail_td
) {
2174 ntd
= bus_to_virt(le32_to_cpup(&td
->next_td
));
2175 if (td_endofchain(*td
))
2178 printk(KERN_DEBUG
"usb-ohci: skipping TD %p\n", td
);
2183 /* Set the ED head past the ones we cleaned
2184 off, and clear the halted flag */
2185 printk(KERN_DEBUG
"usb-ohci: restarting ED %p at TD %p\n", ed
, ntd
);
2186 set_ed_head_td(ed
, virt_to_bus(ntd
));
2188 /* If we didn't find an endofchain TD, give up */
2189 if (td
== tail_td
) {
2195 /* Check if TD should be re-queued */
2196 if ((td
->completed
!= NULL
) &&
2197 (td
->completed(cc
, td
->data
, ohci_td_bytes_done(td
), td
->dev_id
))) {
2198 /* Mark the TD as active again:
2199 * Set the not accessed condition code
2200 * Reset the Error count
2202 td
->info
|= cpu_to_le32(OHCI_TD_CC_NEW
);
2203 clear_td_errorcount(td
);
2204 /* reset the toggle field to TOGGLE_AUTO (0) */
2205 td
->info
&= cpu_to_le32(~OHCI_TD_DT
);
2207 /* point it back to the start of the data buffer */
2208 td
->cur_buf
= cpu_to_le32(virt_to_bus(td
->data
));
2210 /* insert it back on its ED */
2212 ohci_add_tds_to_ed(td
, ed
);
2213 /* ohci_unhalt_ed(td->ed); */
2215 /* return it to the pool of free TDs */
2216 if (can_auto_free(*td
))
2223 spin_unlock(&ohci_edtd_lock
);
2224 } /* ohci_reap_donelist() */
2228 * Get annoyed at the controller for bothering us.
2229 * This pretty much follows the OHCI v1.0a spec, section 5.3.
2231 static void ohci_interrupt(int irq
, void *__ohci
, struct pt_regs
*r
)
2233 struct ohci
*ohci
= __ohci
;
2234 struct ohci_regs
*regs
= ohci
->regs
;
2235 struct ohci_device
*root_hub
=usb_to_ohci(ohci
->bus
->root_hub
);
2236 struct ohci_hcca
*hcca
= root_hub
->hcca
;
2237 __u32 status
, context
;
2239 /* Save the status of the interrupts that are enabled */
2240 status
= readl(®s
->intrstatus
);
2241 status
&= readl(®s
->intrenable
);
2243 /* make context = the interrupt status bits that we care about */
2244 if (hcca
->donehead
!= 0) {
2245 context
= OHCI_INTR_WDH
; /* hcca donehead needs processing */
2246 if (hcca
->donehead
& cpu_to_le32(1)) {
2247 context
|= status
; /* other status change to check */
2252 /* TODO increment a useless interrupt counter here */
2257 /* Disable HC interrupts */ /* why? - paulus */
2258 writel(OHCI_INTR_MIE
, ®s
->intrdisable
);
2261 /* Only do this for SERIOUS debugging, be sure kern.debug logs
2262 * are not going to the console as this can cause your
2263 * machine to lock up if so... -greg */
2264 show_ohci_status(ohci
);
2267 /* Process the done list */
2268 if (context
& OHCI_INTR_WDH
) {
2269 /* See which TD's completed.. */
2270 ohci_reap_donelist(ohci
);
2272 /* reset the done queue and tell the controller */
2273 hcca
->donehead
= 0; /* XXX already done in ohci_reverse_donelist */
2274 writel(OHCI_INTR_WDH
, ®s
->intrstatus
);
2276 context
&= ~OHCI_INTR_WDH
; /* mark this as checked */
2279 #ifdef OHCI_RHSC_INT
2280 /* NOTE: this is very funky on some USB controllers (ie: it
2281 * doesn't work right). Using the ohci_timer instead to poll
2282 * the root hub is a much better choice. */
2283 /* Process any root hub status changes */
2284 if (context
& OHCI_INTR_RHSC
) {
2285 /* Wake the thread to process root hub events */
2286 if (waitqueue_active(&ohci_configure
))
2287 wake_up(&ohci_configure
);
2289 writel(OHCI_INTR_RHSC
, ®s
->intrstatus
);
2291 * Don't unset RHSC in context; it should be disabled.
2292 * The control thread will re-enable it after it has
2293 * checked the root hub status.
2298 /* Start of Frame interrupts, used during safe ED removal */
2299 if (context
& (OHCI_INTR_SF
)) {
2300 writel(OHCI_INTR_SF
, ®s
->intrstatus
);
2301 if (waitqueue_active(&start_of_frame_wakeup
))
2302 wake_up(&start_of_frame_wakeup
);
2303 /* Do NOT mark the frame start interrupt as checked
2304 * as we don't want to receive any more of them until
2308 /* Check those "other" pesky bits */
2309 if (context
& (OHCI_INTR_FNO
)) {
2310 writel(OHCI_INTR_FNO
, ®s
->intrstatus
);
2311 context
&= ~OHCI_INTR_FNO
; /* mark this as checked */
2313 if (context
& OHCI_INTR_SO
) {
2314 writel(OHCI_INTR_SO
, ®s
->intrstatus
);
2315 context
&= ~OHCI_INTR_SO
; /* mark this as checked */
2317 if (context
& OHCI_INTR_RD
) {
2318 writel(OHCI_INTR_RD
, ®s
->intrstatus
);
2319 context
&= ~OHCI_INTR_RD
; /* mark this as checked */
2321 if (context
& OHCI_INTR_UE
) {
2322 /* TODO: need to have the control thread reset the
2323 * controller now and keep a count of unrecoverable
2324 * errors. If there are too many, it should just shut
2325 * the broken controller down entirely. */
2326 writel(OHCI_INTR_UE
, ®s
->intrstatus
);
2327 context
&= ~OHCI_INTR_UE
; /* mark this as checked */
2329 if (context
& OHCI_INTR_OC
) {
2330 writel(OHCI_INTR_OC
, ®s
->intrstatus
);
2331 context
&= ~OHCI_INTR_OC
; /* mark this as checked */
2334 /* Mask out any remaining unprocessed or unmasked interrupts
2335 * so that we don't get any more of them. */
2336 if (context
& ~OHCI_INTR_MIE
) {
2337 writel(context
, ®s
->intrdisable
);
2340 /* Re-enable HC interrupts */
2341 writel(OHCI_INTR_MIE
, ®s
->intrenable
);
2343 } /* ohci_interrupt() */
2347 * Allocate the resources required for running an OHCI controller.
2348 * Host controller interrupts must not be running while calling this
2349 * function or the penguins will get angry.
2351 * The mem_base parameter must be the usable -virtual- address of the
2352 * host controller's memory mapped I/O registers.
2354 static struct ohci
*alloc_ohci(void* mem_base
)
2358 struct usb_bus
*bus
;
2359 struct ohci_device
*dev
;
2360 struct usb_device
*usb
;
2363 printk(KERN_DEBUG
"entering alloc_ohci %p\n", mem_base
);
2366 ohci
= kmalloc(sizeof(*ohci
), GFP_KERNEL
);
2370 memset(ohci
, 0, sizeof(*ohci
));
2373 ohci
->regs
= mem_base
;
2374 INIT_LIST_HEAD(&ohci
->interrupt_list
);
2376 bus
= kmalloc(sizeof(*bus
), GFP_KERNEL
);
2380 memset(bus
, 0, sizeof(*bus
));
2384 bus
->op
= &ohci_device_operations
;
2387 * Allocate the USB device structure and root hub.
2389 * Here we allocate our own root hub and TDs as well as the
2390 * OHCI host controller communications area. The HCCA is just
2391 * a nice pool of memory with pointers to endpoint descriptors
2392 * for the different interrupts.
2394 usb
= ohci_usb_allocate(NULL
);
2398 dev
= usb_to_ohci(usb
);
2399 ohci
->bus
->root_hub
= ohci_to_usb(dev
);
2402 /* Initialize the root hub */
2403 dev
->ohci
= ohci
; /* link back to the controller */
2406 * Allocate the Host Controller Communications Area on a 256
2407 * byte boundary. XXX take the easy way out and just grab a
2408 * page as that's guaranteed to have a nice boundary.
2410 dev
->hcca
= (struct ohci_hcca
*) __get_free_page(GFP_KERNEL
);
2411 memset(dev
->hcca
, 0, sizeof(struct ohci_hcca
));
2413 /* Tell the controller where the HCCA is */
2414 writel(virt_to_bus(dev
->hcca
), &ohci
->regs
->hcca
);
2417 printk(KERN_DEBUG
"usb-ohci: HCCA allocated at %p (bus %p)\n", dev
->hcca
, (void*)virt_to_bus(dev
->hcca
));
2420 /* Get the number of ports on the root hub */
2421 usb
->maxchild
= readl(&ohci
->regs
->roothub
.a
) & 0xff;
2422 if (usb
->maxchild
> MAX_ROOT_PORTS
) {
2423 printk(KERN_INFO
"usb-ohci: Limited to %d ports\n", MAX_ROOT_PORTS
);
2424 usb
->maxchild
= MAX_ROOT_PORTS
;
2426 if (usb
->maxchild
< 1) {
2427 printk(KERN_ERR
"usb-ohci: Less than one root hub port? Impossible!\n");
2430 printk(KERN_DEBUG
"usb-ohci: %d root hub ports found\n", usb
->maxchild
);
2433 * Initialize the ED polling "tree" (for simplicity's sake in
2434 * this driver many nodes in the tree will be identical)
2436 dev
->ed
[ED_INT_32
].next_ed
= cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_16
]));
2437 dev
->ed
[ED_INT_16
].next_ed
= cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_8
]));
2438 dev
->ed
[ED_INT_8
].next_ed
= cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_4
]));
2439 dev
->ed
[ED_INT_4
].next_ed
= cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_2
]));
2440 dev
->ed
[ED_INT_2
].next_ed
= cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_1
]));
2443 * Initialize the polling table to call interrupts at the
2444 * intended intervals. Note that these EDs are just
2445 * placeholders. They have their SKIP bit set and are used as
2446 * list heads to insert real EDs onto.
2448 dev
->hcca
->int_table
[0] = cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_1
]));
2449 for (i
= 1; i
< NUM_INTS
; i
++) {
2451 dev
->hcca
->int_table
[i
] =
2452 cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_32
]));
2454 dev
->hcca
->int_table
[i
] =
2455 cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_16
]));
2457 dev
->hcca
->int_table
[i
] =
2458 cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_8
]));
2460 dev
->hcca
->int_table
[i
] =
2461 cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_4
]));
2463 dev
->hcca
->int_table
[i
] =
2464 cpu_to_le32(virt_to_bus(&dev
->ed
[ED_INT_2
]));
2468 * Tell the controller where the control and bulk lists are.
2469 * The lists start out empty.
2471 writel(0, &ohci
->regs
->ed_controlhead
);
2472 writel(0, &ohci
->regs
->ed_bulkhead
);
2476 printk(KERN_DEBUG
"alloc_ohci(): controller\n");
2477 show_ohci_status(ohci
);
2482 printk(KERN_DEBUG
"leaving alloc_ohci %p\n", ohci
);
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 root_hub
->hcca
= NULL
;
2515 ohci
->bus
->root_hub
= NULL
;
2518 /* unmap the IO address space */
2519 iounmap(ohci
->regs
);
2525 /* If the ohci itself were dynamic we'd free it here */
2527 printk(KERN_DEBUG
"usb-ohci: HC resources released.\n");
2528 } /* release_ohci() */
2532 * USB OHCI control thread
2534 static int ohci_control_thread(void * __ohci
)
2536 struct ohci
*ohci
= (struct ohci
*)__ohci
;
2539 * I'm unfamiliar with the SMP kernel locking.. where should
2540 * this be released and what does it do? -greg
2545 * This thread doesn't need any user-level access,
2546 * so get rid of all of our resources..
2548 printk(KERN_DEBUG
"ohci-control thread code for 0x%p code at 0x%p\n", __ohci
, &ohci_control_thread
);
2550 exit_files(current
);
2551 /*exit_fs(current);*/ /* can't do kernel_thread if we do this */
2553 strcpy(current
->comm
, "ohci-control");
2555 usb_register_bus(ohci
->bus
);
2558 * Damn the torpedoes, full speed ahead
2560 if (start_hc(ohci
) < 0) {
2561 printk(KERN_ERR
"usb-ohci: failed to start the controller\n");
2563 usb_deregister_bus(ohci
->bus
);
2564 printk(KERN_INFO
"leaving ohci_control_thread %p\n", __ohci
);
2570 int unsigned long signr
;
2574 /* check the root hub configuration for changes. */
2575 ohci_check_configuration(ohci
);
2577 /* re-enable root hub status change interrupts. */
2578 #ifdef OHCI_RHSC_INT
2579 writel(OHCI_INTR_RHSC
, &ohci
->regs
->intrenable
);
2582 printk(KERN_DEBUG
"ohci-control thread sleeping\n");
2583 interruptible_sleep_on(&ohci_configure
);
2587 if (start_hc(ohci
) < 0)
2594 * If we were woken up by a signal, see if its useful,
2597 if (signal_pending(current
)) {
2598 /* sending SIGUSR1 makes us print out some info */
2599 spin_lock_irq(¤t
->sigmask_lock
);
2600 signr
= dequeue_signal(¤t
->blocked
, &info
);
2601 spin_unlock_irq(¤t
->sigmask_lock
);
2603 if(signr
== SIGUSR1
) {
2604 /* TODO: have it do a full ed/td queue dump? */
2605 printk(KERN_DEBUG
"OHCI status dump:\n");
2606 show_ohci_status(ohci
);
2607 } else if (signr
== SIGUSR2
) {
2608 /* toggle mega TD/ED debugging output */
2610 MegaDebug
= !MegaDebug
;
2611 printk(KERN_DEBUG
"usb-ohci: Mega debugging %sabled.\n",
2612 MegaDebug
? "en" : "dis");
2615 /* unknown signal, exit the thread */
2616 printk(KERN_DEBUG
"usb-ohci: control thread for %p exiting on signal %ld\n", __ohci
, signr
);
2624 usb_deregister_bus(ohci
->bus
);
2627 } /* ohci_control_thread() */
2631 static int handle_apm_event(apm_event_t event
)
2633 static int down
= 0;
2636 case APM_SYS_SUSPEND
:
2637 case APM_USER_SUSPEND
:
2639 printk(KERN_DEBUG
"usb-ohci: received extra suspend event\n");
2644 case APM_NORMAL_RESUME
:
2645 case APM_CRITICAL_RESUME
:
2647 printk(KERN_DEBUG
"usb-ohci: received bogus resume event\n");
2651 if (waitqueue_active(&ohci_configure
)) {
2653 wake_up(&ohci_configure
);
2658 } /* handle_apm_event() */
2664 * Inspired by Iñaky's driver. This function is a timer routine that
2665 * is called every OHCI_TIMER_FREQ ms. It polls the root hub for
2666 * status changes as on my system the RHSC interrupt just doesn't
2667 * play well with others.. (so RHSC is turned off by default in this
2669 * [my controller is a "SiS 7001 USB (rev 16)"]
2672 static void ohci_timer_func (unsigned long ohci_ptr
)
2674 struct ohci
*ohci
= (struct ohci
*)ohci_ptr
;
2676 ohci_root_hub_events(ohci
);
2678 /* set the next timer */
2679 mod_timer(&ohci_timer
, jiffies
+ ((OHCI_TIMER_FREQ
*HZ
)/1000));
2681 } /* ohci_timer_func() */
2686 * Increment the module usage count, start the control thread and
2687 * return success if the controller is good.
2689 static int found_ohci(int irq
, void* mem_base
)
2695 printk(KERN_DEBUG
"entering found_ohci %d %p\n", irq
, mem_base
);
2698 /* Allocate the running OHCI structures */
2699 ohci
= alloc_ohci(mem_base
);
2705 init_timer(&ohci_timer
);
2706 ohci_timer
.expires
= jiffies
+ ((OHCI_TIMER_FREQ
*HZ
)/1000);
2707 ohci_timer
.data
= (unsigned long)ohci
;
2708 ohci_timer
.function
= ohci_timer_func
;
2709 add_timer(&ohci_timer
);
2713 if (request_irq(irq
, ohci_interrupt
, SA_SHIRQ
, "usb-ohci", ohci
) == 0) {
2719 printk(KERN_DEBUG
"usb-ohci: forking ohci-control thread for 0x%p\n", ohci
);
2722 /* fork off the handler */
2723 pid
= kernel_thread(ohci_control_thread
, ohci
,
2724 CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
);
2731 printk(KERN_ERR
"usb-ohci: Couldn't allocate interrupt %d\n", irq
);
2736 printk(KERN_DEBUG
"leaving found_ohci %d %p\n", irq
, mem_base
);
2740 } /* found_ohci() */
2744 * If this controller is for real, map the IO memory and proceed
2746 static int init_ohci(struct pci_dev
*dev
)
2748 unsigned long mem_base
= dev
->resource
[0].flags
;
2750 /* If its OHCI, its memory */
2751 if (mem_base
& PCI_BASE_ADDRESS_SPACE_IO
)
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){