2 * Open Host Controller Interface driver for USB.
4 * (C) Copyright 1999 Gregory P. Smith <greg@electricrain.com>
6 * This is the "other" host controller interface for USB. You will
7 * find this on many non-Intel based motherboards, and of course the
8 * Mac. As Linus hacked his UHCI driver together first, I modeled
9 * this after his.. (it should be obvious)
11 * From the programming standpoint the OHCI interface seems a little
12 * prettier and potentially less CPU intensive. This remains to be
13 * proven. In reality, I don't believe it'll make one darn bit of
14 * difference. USB v1.1 is a slow bus by today's standards.
16 * OHCI hardware takes care of most of the scheduling of different
17 * transfer types with the correct prioritization for us.
19 * To get started in USB, I used the "Universal Serial Bus System
20 * Architecture" book by Mindshare, Inc. It was a reasonable introduction
21 * and overview of USB and the two dominant host controller interfaces
22 * however you're better off just reading the real specs available
23 * from www.usb.org as you'll need them to get enough detailt to
24 * actually implement a HCD. The book has many typos and omissions
25 * Beware, the specs are the victim of a committee.
27 * This code was written with Guinness on the brain, xsnow on the desktop
28 * and Orbital, Orb, Enya & Massive Attack on the CD player. What a life! ;)
30 * No filesystems were harmed in the development of this code.
32 * $Id: ohci.c,v 1.26 1999/05/11 07:34:47 greg Exp $
35 #include <linux/config.h>
36 #include <linux/module.h>
37 #include <linux/pci.h>
38 #include <linux/kernel.h>
39 #include <linux/delay.h>
40 #include <linux/ioport.h>
41 #include <linux/sched.h>
42 #include <linux/malloc.h>
43 #include <linux/smp_lock.h>
44 #include <linux/errno.h>
46 #include <asm/spinlock.h>
49 #include <asm/system.h>
55 #include <linux/apm_bios.h>
56 static int handle_apm_event(apm_event_t event
);
57 static int apm_resume
= 0;
60 static DECLARE_WAIT_QUEUE_HEAD(ohci_configure
);
63 static struct timer_list ohci_timer
; /* timer for root hub polling */
67 static int ohci_td_result(struct ohci_device
*dev
, struct ohci_td
*td
)
71 status
= td
->info
& OHCI_TD_CC
;
73 /* TODO Debugging code for TD failures goes here */
76 } /* ohci_td_result() */
79 static spinlock_t ohci_edtd_lock
= SPIN_LOCK_UNLOCKED
;
82 * Add a TD to the end of the TD list on a given ED. If td->next_td
83 * points to any more TDs, they will be added as well (naturally).
84 * Otherwise td->next_td must be 0.
86 * The SKIP flag will be cleared after this function.
88 * Important! This function needs locking and atomicity as it works
89 * in parallel with the HC's DMA. Locking ohci_edtd_lock while using
90 * the function is a must.
92 * This function can be called by the interrupt handler.
94 static void ohci_add_td_to_ed(struct ohci_td
*td
, struct ohci_ed
*ed
)
96 /* don't let the HC pull anything from underneath us */
97 ed
->status
|= OHCI_ED_SKIP
;
99 if (ed_head_td(ed
) == 0) { /* empty list, put it on the head */
100 set_ed_head_td(ed
, virt_to_bus(td
));
103 struct ohci_td
*tail
, *head
;
104 head
= (ed_head_td(ed
) == 0) ? NULL
: bus_to_virt(ed_head_td(ed
));
105 tail
= (ed
->tail_td
== 0) ? NULL
: bus_to_virt(ed
->tail_td
);
106 if (!tail
) { /* no tail, single element list */
107 td
->next_td
= head
->next_td
;
108 head
->next_td
= virt_to_bus(td
);
109 ed
->tail_td
= virt_to_bus(td
);
110 } else { /* append to the list */
111 td
->next_td
= tail
->next_td
;
112 tail
->next_td
= virt_to_bus(td
);
113 ed
->tail_td
= virt_to_bus(td
);
117 /* save the ED link in each of the TDs added */
119 while (td
->next_td
!= 0) {
120 td
= bus_to_virt(td
->next_td
);
124 /* turn off the SKIP flag */
125 ed
->status
&= ~OHCI_ED_SKIP
;
126 } /* ohci_add_td_to_ed() */
129 inline void ohci_start_control(struct ohci
*ohci
)
131 /* tell the HC to start processing the control list */
132 writel(OHCI_CMDSTAT_CLF
, &ohci
->regs
->cmdstatus
);
135 inline void ohci_start_bulk(struct ohci
*ohci
)
137 /* tell the HC to start processing the bulk list */
138 writel(OHCI_CMDSTAT_BLF
, &ohci
->regs
->cmdstatus
);
141 inline void ohci_start_periodic(struct ohci
*ohci
)
143 /* enable processing periodc transfers starting next frame */
144 writel_set(OHCI_USB_PLE
, &ohci
->regs
->control
);
147 inline void ohci_start_isoc(struct ohci
*ohci
)
149 /* enable processing isoc. transfers starting next frame */
150 writel_set(OHCI_USB_IE
, &ohci
->regs
->control
);
154 * Add an ED to the hardware register ED list pointed to by hw_listhead_p
156 static void ohci_add_ed_to_hw(struct ohci_ed
*ed
, void* hw_listhead_p
)
161 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
163 listhead
= readl(hw_listhead_p
);
165 /* if the list is not empty, insert this ED at the front */
166 /* XXX should they go on the end? */
168 ed
->next_ed
= listhead
;
171 /* update the hardware listhead pointer */
172 writel(virt_to_bus(ed
), hw_listhead_p
);
174 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
175 } /* ohci_add_ed() */
179 * Put another control ED on the controller's list
181 void ohci_add_control_ed(struct ohci
*ohci
, struct ohci_ed
*ed
)
183 ohci_add_ed_to_hw(ed
, &ohci
->regs
->ed_controlhead
);
184 ohci_start_control(ohci
);
185 } /* ohci_add_control_ed() */
190 * Put another control ED on the controller's list
192 void ohci_add_periodic_ed(struct ohci
*ohci
, struct ohci_ed
*ed
, int period
)
194 ohci_add_ed_to_hw(ed
, /* XXX */);
195 ohci_start_periodic(ohci
);
196 } /* ohci_add_control_ed() */
201 * Remove an ED from the HC list whos bus headpointer is pointed to
204 * Note that the SKIP bit is left on in the removed ED.
206 void ohci_remove_ed_from_hw(struct ohci_ed
*ed
, __u32
* hw_listhead_p
)
210 __u32 bus_ed
= virt_to_bus(ed
);
213 if (ed
== NULL
|| !bus_ed
)
216 /* tell the controller this skip ED */
217 ed
->status
|= OHCI_ED_SKIP
;
219 bus_cur
= readl(hw_listhead_p
);
222 return; /* the list is already empty */
224 cur
= bus_to_virt(bus_cur
);
226 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
228 /* if its the head ED, move the head */
229 if (bus_cur
== bus_ed
) {
230 writel(cur
->next_ed
, hw_listhead_p
);
231 } else if (cur
->next_ed
!= 0) {
232 struct ohci_ed
*prev
;
234 /* walk the list and unlink the ED if found */
237 cur
= bus_to_virt(cur
->next_ed
);
239 if (virt_to_bus(cur
) == bus_ed
) {
240 /* unlink from the list */
241 prev
->next_ed
= cur
->next_ed
;
245 if (cur
->next_ed
== 0)
250 /* clear any links from the ED for safety */
253 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
254 } /* ohci_remove_ed_from_hw() */
257 * Remove an ED from the controller's control list. Note that the SKIP bit
258 * is left on in the removed ED.
260 inline void ohci_remove_control_ed(struct ohci
*ohci
, struct ohci_ed
*ed
)
262 ohci_remove_ed_from_hw(ed
, &ohci
->regs
->ed_controlhead
);
266 * Remove an ED from the controller's bulk list. Note that the SKIP bit
267 * is left on in the removed ED.
269 inline void ohci_remove_bulk_ed(struct ohci
*ohci
, struct ohci_ed
*ed
)
271 ohci_remove_ed_from_hw(ed
, &ohci
->regs
->ed_bulkhead
);
276 * Remove a TD from the given EDs TD list.
278 static void ohci_remove_td_from_ed(struct ohci_td
*td
, struct ohci_ed
*ed
)
281 struct ohci_td
*head_td
;
283 if ((td
== NULL
) || (ed
== NULL
))
286 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
288 if (ed_head_td(ed
) == 0)
291 /* set the "skip me bit" in this ED */
292 ed
->status
|= OHCI_ED_SKIP
;
294 /* XXX Assuming this list will never be circular */
296 head_td
= bus_to_virt(ed_head_td(ed
));
297 if (virt_to_bus(td
) == ed_head_td(ed
)) {
298 /* It's the first TD, remove it. */
299 set_ed_head_td(ed
, head_td
->next_td
);
301 struct ohci_td
*prev_td
, *cur_td
;
303 /* FIXME: collapse this into a nice simple loop :) */
304 if (head_td
->next_td
!= 0) {
306 cur_td
= bus_to_virt(head_td
->next_td
);
310 prev_td
->next_td
= cur_td
->next_td
;
313 if (cur_td
->next_td
== 0)
316 cur_td
= bus_to_virt(cur_td
->next_td
);
321 td
->next_td
= 0; /* remove the TDs links */
324 /* TODO return this TD to the pool of free TDs */
326 /* unset the "skip me bit" in this ED */
327 ed
->status
&= ~OHCI_ED_SKIP
;
329 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
330 } /* ohci_remove_td_from_ed() */
334 * Get a pointer (virtual) to an available TD from the given device's
337 * Return NULL if none are left.
339 static struct ohci_td
*ohci_get_free_td(struct ohci_device
*dev
)
343 for (idx
=0; idx
< NUM_TDS
; idx
++) {
344 if (!td_allocated(dev
->td
[idx
])) {
345 struct ohci_td
*new_td
= &dev
->td
[idx
];
346 /* zero out the TD */
347 memset(new_td
, 0, sizeof(*new_td
));
348 /* mark the new TDs as unaccessed */
349 new_td
->info
= OHCI_TD_CC_NEW
;
350 /* mark it as allocated */
356 printk("usb-ohci error: unable to allocate a TD\n");
358 } /* ohci_get_free_td() */
364 * dir = OHCI_TD_D_IN, OHCI_TD_D_OUT, or OHCI_TD_D_SETUP
365 * toggle = TOGGLE_AUTO, TOGGLE_DATA0, TOGGLE_DATA1
367 inline struct ohci_td
*ohci_fill_new_td(struct ohci_td
*td
, int dir
, int toggle
, __u32 flags
, void *data
, __u32 len
, void *dev_id
, usb_device_irq completed
)
369 /* hardware fields */
370 td
->info
= OHCI_TD_CC_NEW
|
372 (toggle
& OHCI_TD_DT
) |
374 td
->cur_buf
= (data
== NULL
) ? 0 : virt_to_bus(data
);
375 td
->buf_end
= (len
== 0) ? 0 : td
->cur_buf
+ len
- 1;
380 td
->completed
= completed
;
383 } /* ohci_fill_new_td() */
386 /**********************************
387 * OHCI interrupt list operations *
388 **********************************/
391 * Request an interrupt handler for one "pipe" of a USB device.
392 * (this function is pretty minimal right now)
394 * At the moment this is only good for input interrupts. (ie: for a
397 * Period is desired polling interval in ms. The closest, shorter
398 * match will be used. Powers of two from 1-32 are supported by OHCI.
400 static int ohci_request_irq(struct usb_device
*usb
, unsigned int pipe
,
401 usb_device_irq handler
, int period
, void *dev_id
)
403 struct ohci_device
*dev
= usb_to_ohci(usb
);
405 struct ohci_ed
*interrupt_ed
; /* endpoint descriptor for this irq */
408 * Pick a good frequency endpoint based on the requested period
410 interrupt_ed
= &dev
->ohci
->root_hub
->ed
[ms_to_ed_int(period
)];
413 * Set the max packet size, device speed, endpoint number, usb
414 * device number (function address), and type of TD.
416 * FIXME: Isochronous transfers need a pool of special 32 byte
417 * TDs (32 byte aligned) in order to be supported.
419 interrupt_ed
->status
= \
420 ed_set_maxpacket(usb_maxpacket(pipe
)) |
421 ed_set_speed(usb_pipeslow(pipe
)) |
422 usb_pipe_endpdev(pipe
) |
425 td
= ohci_get_free_td(dev
);
426 /* FIXME: check for NULL */
429 ohci_fill_new_td(td
, td_set_dir_out(usb_pipeout(pipe
)),
432 dev
->data
, DATA_BUF_LEN
,
435 * TODO: be aware that OHCI won't advance out of the 4kb
436 * page cur_buf started in. It'll wrap around to the start
437 * of the page... annoying or useful? you decide.
439 * We should make sure dev->data doesn't cross a page...
442 /* FIXME: this just guarantees that its the end of the list */
445 /* Linus did this. see asm/system.h; scary concept... I don't
446 * know if its needed here or not but it won't hurt. */
450 * Put the TD onto our ED
454 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
455 ohci_add_td_to_ed(td
, interrupt_ed
);
456 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
460 /* Assimilate the new ED into the collective */
462 * When dynamic ED allocation is done, this call will be
463 * useful. For now, the correct ED already on the
464 * controller's proper periodic ED lists was chosen above.
466 ohci_add_periodic_ed(dev
->ohci
, interrupt_ed
, period
);
468 /* enable periodic (interrupt) transfers on the HC */
469 ohci_start_periodic(dev
->ohci
);
473 } /* ohci_request_irq() */
477 * Control thread operations:
479 static DECLARE_WAIT_QUEUE_HEAD(control_wakeup
);
482 * This is the handler that gets called when a control transaction
485 * This function is called from the interrupt handler.
487 static int ohci_control_completed(int stats
, void *buffer
, void *dev_id
)
489 wake_up(&control_wakeup
);
491 } /* ohci_control_completed() */
495 * Send or receive a control message on a "pipe"
497 * The cmd parameter is a pointer to the 8 byte setup command to be
498 * sent. FIXME: This is a devrequest in usb.h. The function
499 * should be updated to accept a devrequest* instead of void*..
501 * A control message contains:
502 * - The command itself
503 * - An optional data phase (if len > 0)
504 * - Status complete phase
506 static int ohci_control_msg(struct usb_device
*usb
, unsigned int pipe
, void *cmd
, void *data
, int len
)
508 struct ohci_device
*dev
= usb_to_ohci(usb
);
510 * ideally dev->ed should be linked into the root hub's
511 * control_ed list and used instead of just using it directly.
512 * This could present a problem as is with more than one
513 * device. (but who wants to use a keyboard AND a mouse
516 struct ohci_ed
*control_ed
= &dev
->ohci
->root_hub
->ed
[ED_CONTROL
];
517 struct ohci_td
*setup_td
, *data_td
, *status_td
;
518 DECLARE_WAITQUEUE(wait
, current
);
521 printk(KERN_DEBUG
"entering ohci_control_msg %p (ohci_dev: %p) pipe 0x%x, cmd %p, data %p, len %d\n", usb
, dev
, pipe
, cmd
, data
, len
);
525 * Set the max packet size, device speed, endpoint number, usb
526 * device number (function address), and type of TD.
529 control_ed
->status
= \
530 ed_set_maxpacket(usb_maxpacket(pipe
)) |
531 ed_set_speed(usb_pipeslow(pipe
)) |
532 usb_pipe_endpdev(pipe
) |
536 * Build the control TD
539 /* get a TD to send this control message with */
540 setup_td
= ohci_get_free_td(dev
);
541 /* TODO check for NULL */
544 * Set the not accessed condition code, allow odd sized data,
545 * and set the data transfer type to SETUP. Setup DATA always
546 * uses a DATA0 packet.
548 * The setup packet contains a devrequest (usb.h) which
549 * will always be 8 bytes long. FIXME: the cmd parameter
550 * should be a pointer to one of these instead of a void* !!!
552 ohci_fill_new_td(setup_td
, OHCI_TD_D_SETUP
, TOGGLE_DATA0
,
554 cmd
, 8, /* cmd is always 8 bytes long */
557 /* allocate the next TD */
558 data_td
= ohci_get_free_td(dev
); /* TODO check for NULL */
560 /* link to the next TD */
561 setup_td
->next_td
= virt_to_bus(data_td
);
565 /* build the Control DATA TD, it starts with a DATA1. */
566 ohci_fill_new_td(data_td
, td_set_dir_out(usb_pipeout(pipe
)),
568 OHCI_TD_ROUND
| OHCI_TD_IOC_OFF
,
573 * XXX we should check that the data buffer doesn't
574 * cross a 4096 byte boundary. If so, it needs to be
575 * copied into a single 4096 byte aligned area for the
576 * OHCI's TD logic to see it all, or multiple TDs need
577 * to be made for each page.
579 * It's not likely a control transfer will run into
580 * this problem.. (famous last words)
583 status_td
= ohci_get_free_td(dev
); /* TODO check for NULL */
584 data_td
->next_td
= virt_to_bus(status_td
);
586 status_td
= data_td
; /* no data_td, use it for status */
589 /* The control status packet always uses a DATA1 */
590 ohci_fill_new_td(status_td
,
591 td_set_dir_in(usb_pipeout(pipe
) | (len
== 0)),
595 NULL
, ohci_control_completed
);
596 status_td
->next_td
= 0; /* end of TDs */
599 * Start the control transaction..
601 current
->state
= TASK_UNINTERRUPTIBLE
;
602 add_wait_queue(&control_wakeup
, &wait
);
605 * Add the chain of 2-3 control TDs to the control ED's TD list
609 spin_lock_irqsave(&ohci_edtd_lock
, flags
);
610 ohci_add_td_to_ed(setup_td
, control_ed
);
611 spin_unlock_irqrestore(&ohci_edtd_lock
, flags
);
615 /* complete transaction debugging output (before) */
616 printk(KERN_DEBUG
" Control ED %lx:\n", virt_to_bus(control_ed
));
617 show_ohci_ed(control_ed
);
618 printk(KERN_DEBUG
" Setup TD %lx:\n", virt_to_bus(setup_td
));
619 show_ohci_td(setup_td
);
620 if (data_td
!= status_td
) {
621 printk(KERN_DEBUG
" Data TD %lx:\n", virt_to_bus(data_td
));
622 show_ohci_td(data_td
);
624 printk(KERN_DEBUG
" Status TD %lx:\n", virt_to_bus(status_td
));
625 show_ohci_td(status_td
);
628 /* Give the ED to the HC */
629 ohci_add_control_ed(dev
->ohci
, control_ed
);
632 * this should really check to see that the transaction completed.
634 schedule_timeout(HZ
/10);
636 remove_wait_queue(&control_wakeup
, &wait
);
639 /* complete transaction debugging output (after) */
640 printk(KERN_DEBUG
" (after) Control ED:\n");
641 show_ohci_ed(control_ed
);
642 printk(KERN_DEBUG
" (after) Setup TD:\n");
643 show_ohci_td(setup_td
);
644 if (data_td
!= status_td
) {
645 printk(KERN_DEBUG
" (after) Data TD:\n");
646 show_ohci_td(data_td
);
648 printk(KERN_DEBUG
" (after) Status TD:\n");
649 show_ohci_td(status_td
);
652 /* clean up incase it failed */
653 /* XXX only do this if their ed pointer still points to control_ed
654 * incase they've been reclaimed and used by something else
656 ohci_remove_td_from_ed(setup_td
, control_ed
);
657 ohci_remove_td_from_ed(data_td
, control_ed
);
658 ohci_remove_td_from_ed(status_td
, control_ed
);
660 /* remove the control ED */
661 ohci_remove_control_ed(dev
->ohci
, control_ed
);
664 printk(KERN_DEBUG
"leaving ohci_control_msg\n");
667 return ohci_td_result(dev
, status_td
);
668 } /* ohci_control_msg() */
672 * Allocate a new USB device to be attached to an OHCI controller
674 static struct usb_device
*ohci_usb_allocate(struct usb_device
*parent
)
676 struct usb_device
*usb_dev
;
677 struct ohci_device
*dev
;
680 * Allocate the generic USB device
682 usb_dev
= kmalloc(sizeof(*usb_dev
), GFP_KERNEL
);
686 memset(usb_dev
, 0, sizeof(*usb_dev
));
689 * Allocate an OHCI device (EDs and TDs for this device)
691 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
697 memset(dev
, 0, sizeof(*dev
));
702 usb_dev
->hcpriv
= dev
;
706 * Link the device to its parent (hub, etc..) if any.
708 usb_dev
->parent
= parent
;
711 usb_dev
->bus
= parent
->bus
;
712 dev
->ohci
= usb_to_ohci(parent
)->ohci
;
716 } /* ohci_usb_allocate() */
722 * TODO This function needs to take better care of the EDs and TDs, etc.
724 static int ohci_usb_deallocate(struct usb_device
*usb_dev
)
726 kfree(usb_to_ohci(usb_dev
));
733 * functions for the generic USB driver
735 struct usb_operations ohci_device_operations
= {
744 * Reset an OHCI controller. Returns >= 0 on success.
746 * Afterwards the HC will be in the "suspend" state which prevents you
747 * from writing to some registers. Bring it to the operational state
750 static int reset_hc(struct ohci
*ohci
)
752 int timeout
= 1000; /* prevent an infinite loop */
755 printk(KERN_DEBUG
"usb-ohci: resetting HC %p\n", ohci
);
758 writel(~0x0, &ohci
->regs
->intrdisable
); /* Disable HC interrupts */
759 writel(1, &ohci
->regs
->cmdstatus
); /* HC Reset */
760 writel_mask(0x3f, &ohci
->regs
->control
); /* move to UsbReset state */
762 while ((readl(&ohci
->regs
->cmdstatus
) & OHCI_CMDSTAT_HCR
) != 0) {
764 printk("usb-ohci: USB HC reset timed out!\n");
770 printk(KERN_DEBUG
"usb-ohci: HC %p reset.\n", ohci
);
777 * Reset and start an OHCI controller. Returns >= 0 on success.
779 static int start_hc(struct ohci
*ohci
)
784 fminterval
= readl(&ohci
->regs
->fminterval
) & 0x3fff;
786 printk(KERN_DEBUG
"entering start_hc %p\n", ohci
);
789 if (reset_hc(ohci
) < 0)
792 /* restore registers cleared by the reset */
793 writel(virt_to_bus(ohci
->root_hub
->hcca
), &ohci
->regs
->hcca
);
796 * XXX Should fminterval also be set here?
797 * The spec suggests 0x2edf [11,999]. (FIXME: make this a constant)
799 fminterval
|= (0x2edf << 16);
800 writel(fminterval
, &ohci
->regs
->fminterval
);
801 /* Start periodic transfers at 90% of fminterval (fmremaining
802 * counts down; this will put them in the first 10% of the
804 writel((0x2edf*9)/10, &ohci
->regs
->periodicstart
);
807 * FNO (frame number overflow) could be enabled... they
808 * occur every 32768 frames (every 32-33 seconds). This is
809 * useful for debugging and as a bus heartbeat. -greg
811 /* Choose the interrupts we care about */
812 writel( OHCI_INTR_MIE
| /* OHCI_INTR_RHSC | */
813 OHCI_INTR_WDH
| OHCI_INTR_FNO
,
814 &ohci
->regs
->intrenable
);
816 /* Enter the USB Operational state & start the frames a flowing.. */
817 writel_set(OHCI_USB_OPER
, &ohci
->regs
->control
);
819 /* Enable control lists */
820 writel_set(OHCI_USB_IE
| OHCI_USB_CLE
| OHCI_USB_BLE
, &ohci
->regs
->control
);
822 /* Turn on power to the root hub ports (thanks Roman!) */
823 writel( OHCI_ROOT_LPSC
, &ohci
->regs
->roothub
.status
);
825 printk("usb-ohci: host controller operational\n");
832 * Reset a root hub port
834 static void ohci_reset_port(struct ohci
*ohci
, unsigned int port
)
838 /* Don't allow overflows. */
839 if (port
>= MAX_ROOT_PORTS
) {
840 printk("usb-ohci: bad port #%d in ohci_reset_port\n", port
);
841 port
= MAX_ROOT_PORTS
-1;
844 writel(PORT_PRS
, &ohci
->regs
->roothub
.portstatus
[port
]); /* Reset */
847 * Wait for the reset to complete.
851 /* check port status to see that the reset completed */
852 status
= readl(&ohci
->regs
->roothub
.portstatus
[port
]);
853 if (status
& PORT_PRS
) {
854 /* reset failed, try harder? */
855 printk("usb-ohci: port %d reset failed, retrying\n", port
);
856 writel(PORT_PRS
, &ohci
->regs
->roothub
.portstatus
[port
]);
860 /* TODO we might need to re-enable the port here or is that
863 } /* ohci_reset_port */
867 * This gets called if the connect status on the root hub changes.
869 static void ohci_connect_change(struct ohci
* ohci
, int port
)
871 struct usb_device
*usb_dev
;
872 struct ohci_device
*dev
;
873 /* memory I/O address of the port status register */
874 void *portaddr
= &ohci
->regs
->roothub
.portstatus
[port
];
877 printk(KERN_DEBUG
"ohci_connect_change(%p, %d)\n", ohci
, port
);
880 * Because of the status change we have to forget
881 * everything we think we know about the device
882 * on this root hub port. It may have changed.
884 usb_disconnect(ohci
->root_hub
->usb
->children
+ port
);
886 portstatus
= readl(portaddr
);
888 /* disable the port if nothing is connected */
889 if (!(portstatus
& PORT_CCS
)) {
890 writel(PORT_CCS
, portaddr
);
895 * Allocate a device for the new thingy that's been attached
897 usb_dev
= ohci_usb_allocate(ohci
->root_hub
->usb
);
898 dev
= usb_dev
->hcpriv
;
902 usb_connect(dev
->usb
);
904 /* link it into the bus's device tree */
905 ohci
->root_hub
->usb
->children
[port
] = usb_dev
;
907 wait_ms(200); /* wait for powerup; XXX is this needed? */
908 ohci_reset_port(ohci
, port
);
910 /* Get information on speed by using LSD */
911 usb_dev
->slow
= readl(portaddr
) & PORT_LSDA
? 1 : 0;
914 * Do generic USB device tree processing on the new device.
916 usb_new_device(usb_dev
);
918 } /* ohci_connect_change() */
922 * This gets called when the root hub configuration
923 * has changed. Just go through each port, seeing if
924 * there is something interesting happening.
926 static void ohci_check_configuration(struct ohci
*ohci
)
928 struct ohci_regs
*regs
= ohci
->regs
;
930 int maxport
= readl(&ohci
->regs
->roothub
) & 0xff;
933 printk(KERN_DEBUG
"entering ohci_check_configuration %p\n", ohci
);
937 if (readl(®s
->roothub
.portstatus
[num
]) & PORT_CSC
) {
938 /* reset the connect status change bit */
939 writel(PORT_CSC
, ®s
->roothub
.portstatus
[num
]);
940 /* check the port for a nifty device */
941 ohci_connect_change(ohci
, num
);
943 } while (++num
< maxport
);
946 printk(KERN_DEBUG
"leaving ohci_check_configuration %p\n", ohci
);
948 } /* ohci_check_configuration() */
953 * Check root hub port status and wake the control thread up if
954 * anything has changed.
956 * This function is called from the interrupt handler.
958 static void ohci_root_hub_events(struct ohci
*ohci
)
960 if (waitqueue_active(&ohci_configure
)) {
962 int maxport
= ohci
->root_hub
->usb
->maxchild
;
965 if (readl(&ohci
->regs
->roothub
.portstatus
[num
]) &
967 if (waitqueue_active(&ohci_configure
))
968 wake_up(&ohci_configure
);
971 } while (++num
< maxport
);
973 } /* ohci_root_hub_events() */
977 * The done list is in reverse order; we need to process TDs in the
978 * order they were finished (FIFO). This function builds the FIFO
979 * list using the next_dl_td pointer.
981 * This function originally by Roman Weissgaerber (weissg@vienna.at)
983 * This function is called from the interrupt handler.
985 static struct ohci_td
* ohci_reverse_donelist(struct ohci
* ohci
)
988 struct ohci_hcca
*hcca
= ohci
->root_hub
->hcca
;
989 struct ohci_td
*td_list
= NULL
;
990 struct ohci_td
*td_rev
= NULL
;
992 td_list_hc
= hcca
->donehead
& 0xfffffff0;
996 td_list
= (struct ohci_td
*) bus_to_virt(td_list_hc
);
997 td_list
->next_dl_td
= td_rev
;
1000 td_list_hc
= td_list
->next_td
& 0xfffffff0;
1004 } /* ohci_reverse_donelist() */
1008 * Collect this interrupt's goodies off of the list of finished TDs
1009 * that the OHCI controller is kind enough to setup for us.
1011 * This function is called from the interrupt handler.
1013 static void ohci_reap_donelist(struct ohci
*ohci
)
1015 struct ohci_td
*td
; /* used for walking the list */
1017 spin_lock(&ohci_edtd_lock
);
1019 /* create the FIFO ordered donelist */
1020 td
= ohci_reverse_donelist(ohci
);
1022 while (td
!= NULL
) {
1023 struct ohci_td
*next_td
= td
->next_dl_td
;
1025 /* FIXME: munge td->info into a future standard status format */
1026 /* Check if TD should be re-queued */
1027 if ((td
->completed
!= NULL
) &&
1028 (td
->completed(OHCI_TD_CC_GET(td
->info
), td
->data
, td
->dev_id
)))
1030 /* Mark the TD as active again:
1031 * Set the not accessed condition code
1032 * FIXME: should this reset OHCI_TD_ERRCNT?
1034 td
->info
|= OHCI_TD_CC_NEW
;
1036 /* point it back to the start of the data buffer */
1037 td
->cur_buf
= virt_to_bus(td
->data
);
1039 /* XXX disabled for debugging reasons right now.. */
1040 /* insert it back on its ED */
1041 ohci_add_td_to_ed(td
, td
->ed
);
1043 /* return it to the pool of free TDs */
1050 spin_unlock(&ohci_edtd_lock
);
1051 } /* ohci_reap_donelist() */
1055 static int in_int
= 0;
1058 * Get annoyed at the controller for bothering us.
1059 * This pretty much follows the OHCI v1.0a spec, section 5.3.
1061 static void ohci_interrupt(int irq
, void *__ohci
, struct pt_regs
*r
)
1063 struct ohci
*ohci
= __ohci
;
1064 struct ohci_regs
*regs
= ohci
->regs
;
1065 struct ohci_hcca
*hcca
= ohci
->root_hub
->hcca
;
1066 __u32 status
, context
;
1069 /* for debugging to keep IRQs from running away. */
1076 /* Save the status of the interrupts that are enabled */
1077 status
= readl(®s
->intrstatus
);
1078 status
&= readl(®s
->intrenable
);
1081 /* make context = the interrupt status bits that we care about */
1082 if (hcca
->donehead
!= 0) {
1083 context
= OHCI_INTR_WDH
; /* hcca donehead needs processing */
1084 if (hcca
->donehead
& 1) {
1085 context
|= status
; /* other status change to check */
1090 /* TODO increment a useless interrupt counter here */
1095 /* Disable HC interrupts */
1096 writel(OHCI_INTR_MIE
, ®s
->intrdisable
);
1098 /* Process the done list */
1099 if (context
& OHCI_INTR_WDH
) {
1100 /* See which TD's completed.. */
1101 ohci_reap_donelist(ohci
);
1103 /* reset the done queue and tell the controller */
1105 writel(OHCI_INTR_WDH
, ®s
->intrstatus
);
1107 context
&= ~OHCI_INTR_WDH
; /* mark this as checked */
1110 /* Process any root hub status changes */
1111 if (context
& OHCI_INTR_RHSC
) {
1112 /* Wake the thread to process root hub events */
1113 if (waitqueue_active(&ohci_configure
))
1114 wake_up(&ohci_configure
);
1116 writel(OHCI_INTR_RHSC
, ®s
->intrstatus
);
1118 * Don't unset RHSC in context; it should be disabled.
1119 * The control thread will re-enable it after it has
1120 * checked the root hub status.
1123 /* check the root hub status anyways. Some controllers
1124 * might not generate the interrupt properly. (?) */
1125 ohci_root_hub_events(ohci
);
1128 /* Check those "other" pesky bits */
1129 if (context
& (OHCI_INTR_FNO
)) {
1130 writel(OHCI_INTR_FNO
, ®s
->intrstatus
);
1131 context
&= ~OHCI_INTR_FNO
; /* mark this as checked */
1133 if (context
& OHCI_INTR_SO
) {
1134 writel(OHCI_INTR_SO
, ®s
->intrstatus
);
1135 context
&= ~OHCI_INTR_SO
; /* mark this as checked */
1137 if (context
& OHCI_INTR_RD
) {
1138 writel(OHCI_INTR_RD
, ®s
->intrstatus
);
1139 context
&= ~OHCI_INTR_RD
; /* mark this as checked */
1141 if (context
& OHCI_INTR_UE
) {
1142 /* FIXME: need to have the control thread reset the
1143 * controller now and keep a count of unrecoverable
1144 * errors. If there are too many, it should just shut
1145 * the broken controller down entirely. */
1146 writel(OHCI_INTR_UE
, ®s
->intrstatus
);
1147 context
&= ~OHCI_INTR_UE
; /* mark this as checked */
1149 if (context
& OHCI_INTR_OC
) {
1150 writel(OHCI_INTR_OC
, ®s
->intrstatus
);
1151 context
&= ~OHCI_INTR_OC
; /* mark this as checked */
1154 /* Mask out any remaining unprocessed interrupts so we don't
1155 * get any more of them. */
1156 if (context
& ~OHCI_INTR_MIE
) {
1157 writel(context
, ®s
->intrdisable
);
1160 /* Re-enable HC interrupts */
1161 writel(OHCI_INTR_MIE
, ®s
->intrenable
);
1163 } /* ohci_interrupt() */
1167 * Allocate the resources required for running an OHCI controller.
1168 * Host controller interrupts must not be running while calling this
1169 * function or the penguins will get angry.
1171 * The mem_base parameter must be the usable -virtual- address of the
1172 * host controller's memory mapped I/O registers.
1174 static struct ohci
*alloc_ohci(void* mem_base
)
1178 struct usb_bus
*bus
;
1179 struct ohci_device
*dev
;
1180 struct usb_device
*usb
;
1183 printk(KERN_DEBUG
"entering alloc_ohci %p\n", mem_base
);
1186 ohci
= kmalloc(sizeof(*ohci
), GFP_KERNEL
);
1190 memset(ohci
, 0, sizeof(*ohci
));
1193 ohci
->regs
= mem_base
;
1194 INIT_LIST_HEAD(&ohci
->interrupt_list
);
1196 bus
= kmalloc(sizeof(*bus
), GFP_KERNEL
);
1200 memset(bus
, 0, sizeof(*bus
));
1204 bus
->op
= &ohci_device_operations
;
1207 * Allocate the USB device structure and root hub.
1209 * Here we allocate our own root hub and TDs as well as the
1210 * OHCI host controller communications area. The HCCA is just
1211 * a nice pool of memory with pointers to endpoint descriptors
1212 * for the different interrupts.
1214 usb
= ohci_usb_allocate(NULL
);
1218 dev
= ohci
->root_hub
= usb_to_ohci(usb
);
1222 /* Initialize the root hub */
1223 dev
->ohci
= ohci
; /* link back to the controller */
1226 * Allocate the Host Controller Communications Area on a 256
1227 * byte boundary. XXX take the easy way out and just grab a
1228 * page as that's guaranteed to have a nice boundary.
1230 dev
->hcca
= (struct ohci_hcca
*) __get_free_page(GFP_KERNEL
);
1232 /* Tell the controller where the HCCA is */
1233 writel(virt_to_bus(dev
->hcca
), &ohci
->regs
->hcca
);
1236 printk(KERN_DEBUG
"usb-ohci: HCCA allocated at %p (bus %p)\n", dev
->hcca
, (void*)virt_to_bus(dev
->hcca
));
1239 /* Get the number of ports on the root hub */
1240 usb
->maxchild
= readl(&ohci
->regs
->roothub
.a
) & 0xff;
1241 if (usb
->maxchild
> MAX_ROOT_PORTS
) {
1242 printk("usb-ohci: Limited to %d ports\n", MAX_ROOT_PORTS
);
1243 usb
->maxchild
= MAX_ROOT_PORTS
;
1245 if (usb
->maxchild
< 1) {
1246 printk("usb-ohci: Less than one root hub port? Impossible!\n");
1249 printk("usb-ohci: %d root hub ports found\n", usb
->maxchild
);
1252 * Initialize the ED polling "tree" (for simplicity's sake in
1253 * this driver many nodes in the tree will be identical)
1255 dev
->ed
[ED_INT_32
].next_ed
= virt_to_bus(&dev
->ed
[ED_INT_16
]);
1256 dev
->ed
[ED_INT_16
].next_ed
= virt_to_bus(&dev
->ed
[ED_INT_8
]);
1257 dev
->ed
[ED_INT_8
].next_ed
= virt_to_bus(&dev
->ed
[ED_INT_4
]);
1258 dev
->ed
[ED_INT_4
].next_ed
= virt_to_bus(&dev
->ed
[ED_INT_2
]);
1259 dev
->ed
[ED_INT_2
].next_ed
= virt_to_bus(&dev
->ed
[ED_INT_1
]);
1262 * Initialize the polling table to call interrupts at the
1263 * intended intervals.
1265 dev
->hcca
->int_table
[0] = virt_to_bus(&dev
->ed
[ED_INT_32
]);
1266 for (i
= 1; i
< NUM_INTS
; i
++) {
1268 dev
->hcca
->int_table
[i
] =
1269 virt_to_bus(&dev
->ed
[ED_INT_16
]);
1271 dev
->hcca
->int_table
[i
] =
1272 virt_to_bus(&dev
->ed
[ED_INT_8
]);
1274 dev
->hcca
->int_table
[i
] =
1275 virt_to_bus(&dev
->ed
[ED_INT_4
]);
1277 dev
->hcca
->int_table
[i
] =
1278 virt_to_bus(&dev
->ed
[ED_INT_2
]);
1280 dev
->hcca
->int_table
[i
] =
1281 virt_to_bus(&dev
->ed
[ED_INT_1
]);
1285 * Tell the controller where the control and bulk lists are
1286 * The lists start out empty.
1288 writel(0, &ohci
->regs
->ed_controlhead
);
1289 writel(0, &ohci
->regs
->ed_bulkhead
);
1291 writel(virt_to_bus(&dev->ed[ED_CONTROL]), &ohci->regs->ed_controlhead);
1292 writel(virt_to_bus(&dev->ed[ED_BULK]), &ohci->regs->ed_bulkhead);
1296 printk(KERN_DEBUG
"alloc_ohci(): controller\n");
1297 show_ohci_status(ohci
);
1301 printk(KERN_DEBUG
"leaving alloc_ohci %p\n", ohci
);
1305 } /* alloc_ohci() */
1309 * De-allocate all resoueces..
1311 static void release_ohci(struct ohci
*ohci
)
1313 printk(KERN_DEBUG
"entering release_ohci %p\n", ohci
);
1316 /* stop our timer */
1317 del_timer(&ohci_timer
);
1319 if (ohci
->irq
>= 0) {
1320 free_irq(ohci
->irq
, ohci
);
1324 /* stop all OHCI interrupts */
1325 writel(~0x0, &ohci
->regs
->intrdisable
);
1327 if (ohci
->root_hub
) {
1328 /* ensure that HC is stopped before releasing the HCCA */
1329 writel(OHCI_USB_SUSPEND
, &ohci
->regs
->control
);
1330 free_page((unsigned long) ohci
->root_hub
->hcca
);
1331 kfree(ohci
->root_hub
);
1332 ohci
->root_hub
->hcca
= NULL
;
1333 ohci
->root_hub
= NULL
;
1336 /* unmap the IO address space */
1337 iounmap(ohci
->regs
);
1343 /* If the ohci itself were dynamic we'd free it here */
1345 printk(KERN_DEBUG
"usb-ohci: HC resources released.\n");
1346 } /* release_ohci() */
1350 * USB OHCI control thread
1352 static int ohci_control_thread(void * __ohci
)
1354 struct ohci
*ohci
= (struct ohci
*)__ohci
;
1357 * I'm unfamiliar with the SMP kernel locking.. where should
1358 * this be released and what does it do? -greg
1363 * This thread doesn't need any user-level access,
1364 * so get rid of all of our resources..
1366 printk("ohci_control_thread code at %p\n", &ohci_control_thread
);
1368 exit_files(current
);
1371 strcpy(current
->comm
, "ohci-control");
1374 * Damn the torpedoes, full speed ahead
1376 if (start_hc(ohci
) < 0) {
1377 printk("usb-ohci: failed to start the controller\n");
1379 printk(KERN_DEBUG
"leaving ohci_control_thread %p\n", __ohci
);
1385 int unsigned long signr
;
1389 /* check the root hub configuration for changes. */
1390 ohci_check_configuration(ohci
);
1392 /* re-enable root hub status change interrupts. */
1394 writel(OHCI_INTR_RHSC
, &ohci
->regs
->intrenable
);
1397 printk(KERN_DEBUG
"ohci-control thread sleeping\n");
1398 interruptible_sleep_on(&ohci_configure
);
1402 if (start_hc(ohci
) < 0)
1409 * If we were woken up by a signal, see if its useful,
1412 if (signal_pending(current
)) {
1413 /* sending SIGUSR1 makes us print out some info */
1414 spin_lock_irq(¤t
->sigmask_lock
);
1415 signr
= dequeue_signal(¤t
->blocked
, &info
);
1416 spin_unlock_irq(¤t
->sigmask_lock
);
1418 if(signr
== SIGUSR1
) {
1419 /* FIXME: have it do a full ed/td queue dump */
1420 printk(KERN_DEBUG
"OHCI status dump:\n");
1421 show_ohci_status(ohci
);
1423 /* unknown signal, exit the thread */
1432 printk(KERN_DEBUG
"leaving ohci_control_thread %p\n", __ohci
);
1435 } /* ohci_control_thread() */
1439 static int handle_apm_event(apm_event_t event
)
1441 static int down
= 0;
1444 case APM_SYS_SUSPEND
:
1445 case APM_USER_SUSPEND
:
1447 printk(KERN_DEBUG
"usb-ohci: received extra suspend event\n");
1452 case APM_NORMAL_RESUME
:
1453 case APM_CRITICAL_RESUME
:
1455 printk(KERN_DEBUG
"usb-ohci: received bogus resume event\n");
1459 if (waitqueue_active(&ohci_configure
)) {
1461 wake_up(&ohci_configure
);
1466 } /* handle_apm_event() */
1472 * Inspired by Iñaky's driver. This function is a timer routine that
1473 * is called OHCI_TIMER_FREQ times per second. It polls the root hub
1474 * for status changes as on my system things are acting a bit odd at
1477 static void ohci_timer_func (unsigned long ohci_ptr
)
1479 struct ohci
*ohci
= (struct ohci
*)ohci_ptr
;
1481 ohci_root_hub_events(ohci
);
1483 /* press the snooze button... */
1484 mod_timer(&ohci_timer
, jiffies
+ (OHCI_TIMER_FREQ
*HZ
));
1485 } /* ohci_timer_func() */
1490 * Increment the module usage count, start the control thread and
1491 * return success if the controller is good.
1493 static int found_ohci(int irq
, void* mem_base
)
1499 printk(KERN_DEBUG
"entering found_ohci %d %p\n", irq
, mem_base
);
1502 /* Allocate the running OHCI structures */
1503 ohci
= alloc_ohci(mem_base
);
1509 init_timer(&ohci_timer
);
1510 ohci_timer
.expires
= jiffies
+ (OHCI_TIMER_FREQ
*HZ
);
1511 ohci_timer
.data
= (unsigned long)ohci
;
1512 ohci_timer
.function
= ohci_timer_func
;
1516 if (request_irq(irq
, ohci_interrupt
, SA_SHIRQ
, "usb-ohci", ohci
) == 0) {
1522 printk(KERN_DEBUG
"usb-ohci: starting ohci-control thread\n");
1525 /* fork off the handler */
1526 pid
= kernel_thread(ohci_control_thread
, ohci
,
1527 CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
);
1534 printk("usb-ohci: Couldn't allocate interrupt %d\n", irq
);
1539 printk(KERN_DEBUG
"leaving found_ohci %d %p\n", irq
, mem_base
);
1543 } /* found_ohci() */
1547 * If this controller is for real, map the IO memory and proceed
1549 static int init_ohci(struct pci_dev
*dev
)
1551 unsigned long mem_base
= dev
->base_address
[0];
1553 /* If its OHCI, its memory */
1554 if (mem_base
& PCI_BASE_ADDRESS_SPACE_IO
)
1557 /* Get the memory address and map it for IO */
1558 mem_base
&= PCI_BASE_ADDRESS_MEM_MASK
;
1560 /* no interrupt won't work... */
1561 if (dev
->irq
== 0) {
1562 printk("usb-ohci: no irq assigned? check your BIOS settings.\n");
1567 * FIXME ioremap_nocache isn't implemented on all CPUs (such
1568 * as the Alpha) [?] What should I use instead...
1570 * The iounmap() is done on in release_ohci.
1572 mem_base
= (unsigned long) ioremap_nocache(mem_base
, 4096);
1575 printk("Error mapping OHCI memory\n");
1580 if (found_ohci(dev
->irq
, (void *) mem_base
) < 0) {
1590 * Clean up when unloading the module
1592 void cleanup_module(void)
1595 apm_unregister_callback(&handle_apm_event
);
1597 #ifdef CONFIG_USB_MOUSE
1598 usb_mouse_cleanup();
1600 printk("usb-ohci: module unloaded\n");
1603 #define ohci_init init_module
1608 /* TODO this should be named following Linux convention and go in pci.h */
1609 #define PCI_CLASS_SERIAL_USB_OHCI ((PCI_CLASS_SERIAL_USB << 8) | 0x0010)
1612 * Search the PCI bus for an OHCI USB controller and set it up
1614 * If anyone wants multiple controllers this will need to be
1615 * updated.. Right now, it just picks the first one it finds.
1620 struct pci_dev
*dev
= NULL
;
1623 if (sizeof(struct ohci_device
) > 4096) {
1624 printk("usb-ohci: struct ohci_device to large\n");
1628 printk("OHCI USB Driver loading\n");
1632 /* Find an OHCI USB controller */
1633 dev
= pci_find_class(PCI_CLASS_SERIAL_USB_OHCI
, dev
);
1637 /* Verify that its OpenHCI by checking for MMIO */
1638 /* pci_read_config_byte(dev, PCI_CLASS_PROG, &type);
1643 retval
= init_ohci(dev
);
1647 /* TODO check module params here to determine what to load */
1649 #ifdef CONFIG_USB_MOUSE
1652 #ifdef CONFIG_USB_KBD
1656 #ifdef CONFIG_USB_AUDIO
1660 apm_register_callback(&handle_apm_event
);
1663 return 0; /* no error */