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
));
732 #define ohci_bulk_msg NULL
735 * functions for the generic USB driver
737 struct usb_operations ohci_device_operations
= {
747 * Reset an OHCI controller. Returns >= 0 on success.
749 * Afterwards the HC will be in the "suspend" state which prevents you
750 * from writing to some registers. Bring it to the operational state
753 static int reset_hc(struct ohci
*ohci
)
755 int timeout
= 1000; /* prevent an infinite loop */
758 printk(KERN_DEBUG
"usb-ohci: resetting HC %p\n", ohci
);
761 writel(~0x0, &ohci
->regs
->intrdisable
); /* Disable HC interrupts */
762 writel(1, &ohci
->regs
->cmdstatus
); /* HC Reset */
763 writel_mask(0x3f, &ohci
->regs
->control
); /* move to UsbReset state */
765 while ((readl(&ohci
->regs
->cmdstatus
) & OHCI_CMDSTAT_HCR
) != 0) {
767 printk("usb-ohci: USB HC reset timed out!\n");
773 printk(KERN_DEBUG
"usb-ohci: HC %p reset.\n", ohci
);
780 * Reset and start an OHCI controller. Returns >= 0 on success.
782 static int start_hc(struct ohci
*ohci
)
787 fminterval
= readl(&ohci
->regs
->fminterval
) & 0x3fff;
789 printk(KERN_DEBUG
"entering start_hc %p\n", ohci
);
792 if (reset_hc(ohci
) < 0)
795 /* restore registers cleared by the reset */
796 writel(virt_to_bus(ohci
->root_hub
->hcca
), &ohci
->regs
->hcca
);
799 * XXX Should fminterval also be set here?
800 * The spec suggests 0x2edf [11,999]. (FIXME: make this a constant)
802 fminterval
|= (0x2edf << 16);
803 writel(fminterval
, &ohci
->regs
->fminterval
);
804 /* Start periodic transfers at 90% of fminterval (fmremaining
805 * counts down; this will put them in the first 10% of the
807 writel((0x2edf*9)/10, &ohci
->regs
->periodicstart
);
810 * FNO (frame number overflow) could be enabled... they
811 * occur every 32768 frames (every 32-33 seconds). This is
812 * useful for debugging and as a bus heartbeat. -greg
814 /* Choose the interrupts we care about */
815 writel( OHCI_INTR_MIE
| /* OHCI_INTR_RHSC | */
816 OHCI_INTR_WDH
| OHCI_INTR_FNO
,
817 &ohci
->regs
->intrenable
);
819 /* Enter the USB Operational state & start the frames a flowing.. */
820 writel_set(OHCI_USB_OPER
, &ohci
->regs
->control
);
822 /* Enable control lists */
823 writel_set(OHCI_USB_IE
| OHCI_USB_CLE
| OHCI_USB_BLE
, &ohci
->regs
->control
);
825 /* Force global power enable -gal@cs.uni-magdeburg.de */
827 * This turns on global power switching for all the ports
828 * and tells the HC that all of the ports should be powered on
831 * TODO: This could be battery draining for laptops.. We
832 * should implement power switching.
834 writel_set( OHCI_ROOT_A_NPS
, &ohci
->regs
->roothub
.a
);
835 writel_mask( ~((__u32
)OHCI_ROOT_A_PSM
), &ohci
->regs
->roothub
.a
);
837 /* Turn on power to the root hub ports (thanks Roman!) */
838 writel( OHCI_ROOT_LPSC
, &ohci
->regs
->roothub
.status
);
840 printk("usb-ohci: host controller operational\n");
847 * Reset a root hub port
849 static void ohci_reset_port(struct ohci
*ohci
, unsigned int port
)
853 /* Don't allow overflows. */
854 if (port
>= MAX_ROOT_PORTS
) {
855 printk("usb-ohci: bad port #%d in ohci_reset_port\n", port
);
856 port
= MAX_ROOT_PORTS
-1;
859 writel(PORT_PRS
, &ohci
->regs
->roothub
.portstatus
[port
]); /* Reset */
862 * Wait for the reset to complete.
866 /* check port status to see that the reset completed */
867 status
= readl(&ohci
->regs
->roothub
.portstatus
[port
]);
868 if (status
& PORT_PRS
) {
869 /* reset failed, try harder? */
870 printk("usb-ohci: port %d reset failed, retrying\n", port
);
871 writel(PORT_PRS
, &ohci
->regs
->roothub
.portstatus
[port
]);
875 /* TODO we might need to re-enable the port here or is that
878 } /* ohci_reset_port */
882 * This gets called if the connect status on the root hub changes.
884 static void ohci_connect_change(struct ohci
* ohci
, int port
)
886 struct usb_device
*usb_dev
;
887 struct ohci_device
*dev
;
888 /* memory I/O address of the port status register */
889 void *portaddr
= &ohci
->regs
->roothub
.portstatus
[port
];
892 printk(KERN_DEBUG
"ohci_connect_change(%p, %d)\n", ohci
, port
);
895 * Because of the status change we have to forget
896 * everything we think we know about the device
897 * on this root hub port. It may have changed.
899 usb_disconnect(ohci
->root_hub
->usb
->children
+ port
);
901 portstatus
= readl(portaddr
);
903 /* disable the port if nothing is connected */
904 if (!(portstatus
& PORT_CCS
)) {
905 writel(PORT_CCS
, portaddr
);
910 * Allocate a device for the new thingy that's been attached
912 usb_dev
= ohci_usb_allocate(ohci
->root_hub
->usb
);
913 dev
= usb_dev
->hcpriv
;
917 usb_connect(dev
->usb
);
919 /* link it into the bus's device tree */
920 ohci
->root_hub
->usb
->children
[port
] = usb_dev
;
922 wait_ms(200); /* wait for powerup; XXX is this needed? */
923 ohci_reset_port(ohci
, port
);
925 /* Get information on speed by using LSD */
926 usb_dev
->slow
= readl(portaddr
) & PORT_LSDA
? 1 : 0;
929 * Do generic USB device tree processing on the new device.
931 usb_new_device(usb_dev
);
933 } /* ohci_connect_change() */
937 * This gets called when the root hub configuration
938 * has changed. Just go through each port, seeing if
939 * there is something interesting happening.
941 static void ohci_check_configuration(struct ohci
*ohci
)
943 struct ohci_regs
*regs
= ohci
->regs
;
945 int maxport
= readl(&ohci
->regs
->roothub
) & 0xff;
948 printk(KERN_DEBUG
"entering ohci_check_configuration %p\n", ohci
);
952 if (readl(®s
->roothub
.portstatus
[num
]) & PORT_CSC
) {
953 /* reset the connect status change bit */
954 writel(PORT_CSC
, ®s
->roothub
.portstatus
[num
]);
955 /* check the port for a nifty device */
956 ohci_connect_change(ohci
, num
);
958 } while (++num
< maxport
);
961 printk(KERN_DEBUG
"leaving ohci_check_configuration %p\n", ohci
);
963 } /* ohci_check_configuration() */
968 * Check root hub port status and wake the control thread up if
969 * anything has changed.
971 * This function is called from the interrupt handler.
973 static void ohci_root_hub_events(struct ohci
*ohci
)
975 if (waitqueue_active(&ohci_configure
)) {
977 int maxport
= ohci
->root_hub
->usb
->maxchild
;
980 if (readl(&ohci
->regs
->roothub
.portstatus
[num
]) &
982 if (waitqueue_active(&ohci_configure
))
983 wake_up(&ohci_configure
);
986 } while (++num
< maxport
);
988 } /* ohci_root_hub_events() */
992 * The done list is in reverse order; we need to process TDs in the
993 * order they were finished (FIFO). This function builds the FIFO
994 * list using the next_dl_td pointer.
996 * This function originally by Roman Weissgaerber (weissg@vienna.at)
998 * This function is called from the interrupt handler.
1000 static struct ohci_td
* ohci_reverse_donelist(struct ohci
* ohci
)
1003 struct ohci_hcca
*hcca
= ohci
->root_hub
->hcca
;
1004 struct ohci_td
*td_list
= NULL
;
1005 struct ohci_td
*td_rev
= NULL
;
1007 td_list_hc
= hcca
->donehead
& 0xfffffff0;
1011 td_list
= (struct ohci_td
*) bus_to_virt(td_list_hc
);
1012 td_list
->next_dl_td
= td_rev
;
1015 td_list_hc
= td_list
->next_td
& 0xfffffff0;
1019 } /* ohci_reverse_donelist() */
1023 * Collect this interrupt's goodies off of the list of finished TDs
1024 * that the OHCI controller is kind enough to setup for us.
1026 * This function is called from the interrupt handler.
1028 static void ohci_reap_donelist(struct ohci
*ohci
)
1030 struct ohci_td
*td
; /* used for walking the list */
1032 spin_lock(&ohci_edtd_lock
);
1034 /* create the FIFO ordered donelist */
1035 td
= ohci_reverse_donelist(ohci
);
1037 while (td
!= NULL
) {
1038 struct ohci_td
*next_td
= td
->next_dl_td
;
1040 /* FIXME: munge td->info into a future standard status format */
1041 /* Check if TD should be re-queued */
1042 if ((td
->completed
!= NULL
) &&
1043 (td
->completed(OHCI_TD_CC_GET(td
->info
), td
->data
, td
->dev_id
)))
1045 /* Mark the TD as active again:
1046 * Set the not accessed condition code
1047 * FIXME: should this reset OHCI_TD_ERRCNT?
1049 td
->info
|= OHCI_TD_CC_NEW
;
1051 /* point it back to the start of the data buffer */
1052 td
->cur_buf
= virt_to_bus(td
->data
);
1054 /* XXX disabled for debugging reasons right now.. */
1055 /* insert it back on its ED */
1056 ohci_add_td_to_ed(td
, td
->ed
);
1058 /* return it to the pool of free TDs */
1065 spin_unlock(&ohci_edtd_lock
);
1066 } /* ohci_reap_donelist() */
1070 static int in_int
= 0;
1073 * Get annoyed at the controller for bothering us.
1074 * This pretty much follows the OHCI v1.0a spec, section 5.3.
1076 static void ohci_interrupt(int irq
, void *__ohci
, struct pt_regs
*r
)
1078 struct ohci
*ohci
= __ohci
;
1079 struct ohci_regs
*regs
= ohci
->regs
;
1080 struct ohci_hcca
*hcca
= ohci
->root_hub
->hcca
;
1081 __u32 status
, context
;
1084 /* for debugging to keep IRQs from running away. */
1091 /* Save the status of the interrupts that are enabled */
1092 status
= readl(®s
->intrstatus
);
1093 status
&= readl(®s
->intrenable
);
1096 /* make context = the interrupt status bits that we care about */
1097 if (hcca
->donehead
!= 0) {
1098 context
= OHCI_INTR_WDH
; /* hcca donehead needs processing */
1099 if (hcca
->donehead
& 1) {
1100 context
|= status
; /* other status change to check */
1105 /* TODO increment a useless interrupt counter here */
1110 /* Disable HC interrupts */
1111 writel(OHCI_INTR_MIE
, ®s
->intrdisable
);
1113 /* Process the done list */
1114 if (context
& OHCI_INTR_WDH
) {
1115 /* See which TD's completed.. */
1116 ohci_reap_donelist(ohci
);
1118 /* reset the done queue and tell the controller */
1120 writel(OHCI_INTR_WDH
, ®s
->intrstatus
);
1122 context
&= ~OHCI_INTR_WDH
; /* mark this as checked */
1125 /* Process any root hub status changes */
1126 if (context
& OHCI_INTR_RHSC
) {
1127 /* Wake the thread to process root hub events */
1128 if (waitqueue_active(&ohci_configure
))
1129 wake_up(&ohci_configure
);
1131 writel(OHCI_INTR_RHSC
, ®s
->intrstatus
);
1133 * Don't unset RHSC in context; it should be disabled.
1134 * The control thread will re-enable it after it has
1135 * checked the root hub status.
1138 /* check the root hub status anyways. Some controllers
1139 * might not generate the interrupt properly. (?) */
1140 ohci_root_hub_events(ohci
);
1143 /* Check those "other" pesky bits */
1144 if (context
& (OHCI_INTR_FNO
)) {
1145 writel(OHCI_INTR_FNO
, ®s
->intrstatus
);
1146 context
&= ~OHCI_INTR_FNO
; /* mark this as checked */
1148 if (context
& OHCI_INTR_SO
) {
1149 writel(OHCI_INTR_SO
, ®s
->intrstatus
);
1150 context
&= ~OHCI_INTR_SO
; /* mark this as checked */
1152 if (context
& OHCI_INTR_RD
) {
1153 writel(OHCI_INTR_RD
, ®s
->intrstatus
);
1154 context
&= ~OHCI_INTR_RD
; /* mark this as checked */
1156 if (context
& OHCI_INTR_UE
) {
1157 /* FIXME: need to have the control thread reset the
1158 * controller now and keep a count of unrecoverable
1159 * errors. If there are too many, it should just shut
1160 * the broken controller down entirely. */
1161 writel(OHCI_INTR_UE
, ®s
->intrstatus
);
1162 context
&= ~OHCI_INTR_UE
; /* mark this as checked */
1164 if (context
& OHCI_INTR_OC
) {
1165 writel(OHCI_INTR_OC
, ®s
->intrstatus
);
1166 context
&= ~OHCI_INTR_OC
; /* mark this as checked */
1169 /* Mask out any remaining unprocessed interrupts so we don't
1170 * get any more of them. */
1171 if (context
& ~OHCI_INTR_MIE
) {
1172 writel(context
, ®s
->intrdisable
);
1175 /* Re-enable HC interrupts */
1176 writel(OHCI_INTR_MIE
, ®s
->intrenable
);
1178 } /* ohci_interrupt() */
1182 * Allocate the resources required for running an OHCI controller.
1183 * Host controller interrupts must not be running while calling this
1184 * function or the penguins will get angry.
1186 * The mem_base parameter must be the usable -virtual- address of the
1187 * host controller's memory mapped I/O registers.
1189 static struct ohci
*alloc_ohci(void* mem_base
)
1193 struct usb_bus
*bus
;
1194 struct ohci_device
*dev
;
1195 struct usb_device
*usb
;
1198 printk(KERN_DEBUG
"entering alloc_ohci %p\n", mem_base
);
1201 ohci
= kmalloc(sizeof(*ohci
), GFP_KERNEL
);
1205 memset(ohci
, 0, sizeof(*ohci
));
1208 ohci
->regs
= mem_base
;
1209 INIT_LIST_HEAD(&ohci
->interrupt_list
);
1211 bus
= kmalloc(sizeof(*bus
), GFP_KERNEL
);
1215 memset(bus
, 0, sizeof(*bus
));
1219 bus
->op
= &ohci_device_operations
;
1222 * Allocate the USB device structure and root hub.
1224 * Here we allocate our own root hub and TDs as well as the
1225 * OHCI host controller communications area. The HCCA is just
1226 * a nice pool of memory with pointers to endpoint descriptors
1227 * for the different interrupts.
1229 usb
= ohci_usb_allocate(NULL
);
1233 dev
= ohci
->root_hub
= usb_to_ohci(usb
);
1237 /* Initialize the root hub */
1238 dev
->ohci
= ohci
; /* link back to the controller */
1241 * Allocate the Host Controller Communications Area on a 256
1242 * byte boundary. XXX take the easy way out and just grab a
1243 * page as that's guaranteed to have a nice boundary.
1245 dev
->hcca
= (struct ohci_hcca
*) __get_free_page(GFP_KERNEL
);
1247 /* Tell the controller where the HCCA is */
1248 writel(virt_to_bus(dev
->hcca
), &ohci
->regs
->hcca
);
1251 printk(KERN_DEBUG
"usb-ohci: HCCA allocated at %p (bus %p)\n", dev
->hcca
, (void*)virt_to_bus(dev
->hcca
));
1254 /* Get the number of ports on the root hub */
1255 usb
->maxchild
= readl(&ohci
->regs
->roothub
.a
) & 0xff;
1256 if (usb
->maxchild
> MAX_ROOT_PORTS
) {
1257 printk("usb-ohci: Limited to %d ports\n", MAX_ROOT_PORTS
);
1258 usb
->maxchild
= MAX_ROOT_PORTS
;
1260 if (usb
->maxchild
< 1) {
1261 printk("usb-ohci: Less than one root hub port? Impossible!\n");
1264 printk("usb-ohci: %d root hub ports found\n", usb
->maxchild
);
1267 * Initialize the ED polling "tree" (for simplicity's sake in
1268 * this driver many nodes in the tree will be identical)
1270 dev
->ed
[ED_INT_32
].next_ed
= virt_to_bus(&dev
->ed
[ED_INT_16
]);
1271 dev
->ed
[ED_INT_16
].next_ed
= virt_to_bus(&dev
->ed
[ED_INT_8
]);
1272 dev
->ed
[ED_INT_8
].next_ed
= virt_to_bus(&dev
->ed
[ED_INT_4
]);
1273 dev
->ed
[ED_INT_4
].next_ed
= virt_to_bus(&dev
->ed
[ED_INT_2
]);
1274 dev
->ed
[ED_INT_2
].next_ed
= virt_to_bus(&dev
->ed
[ED_INT_1
]);
1277 * Initialize the polling table to call interrupts at the
1278 * intended intervals.
1280 dev
->hcca
->int_table
[0] = virt_to_bus(&dev
->ed
[ED_INT_1
]);
1281 for (i
= 1; i
< NUM_INTS
; i
++) {
1283 dev
->hcca
->int_table
[i
] =
1284 virt_to_bus(&dev
->ed
[ED_INT_32
]);
1286 dev
->hcca
->int_table
[i
] =
1287 virt_to_bus(&dev
->ed
[ED_INT_16
]);
1289 dev
->hcca
->int_table
[i
] =
1290 virt_to_bus(&dev
->ed
[ED_INT_8
]);
1292 dev
->hcca
->int_table
[i
] =
1293 virt_to_bus(&dev
->ed
[ED_INT_4
]);
1295 dev
->hcca
->int_table
[i
] =
1296 virt_to_bus(&dev
->ed
[ED_INT_2
]);
1300 * Tell the controller where the control and bulk lists are
1301 * The lists start out empty.
1303 writel(0, &ohci
->regs
->ed_controlhead
);
1304 writel(0, &ohci
->regs
->ed_bulkhead
);
1306 writel(virt_to_bus(&dev->ed[ED_CONTROL]), &ohci->regs->ed_controlhead);
1307 writel(virt_to_bus(&dev->ed[ED_BULK]), &ohci->regs->ed_bulkhead);
1311 printk(KERN_DEBUG
"alloc_ohci(): controller\n");
1312 show_ohci_status(ohci
);
1316 printk(KERN_DEBUG
"leaving alloc_ohci %p\n", ohci
);
1320 } /* alloc_ohci() */
1324 * De-allocate all resoueces..
1326 static void release_ohci(struct ohci
*ohci
)
1328 printk(KERN_DEBUG
"entering release_ohci %p\n", ohci
);
1331 /* stop our timer */
1332 del_timer(&ohci_timer
);
1334 if (ohci
->irq
>= 0) {
1335 free_irq(ohci
->irq
, ohci
);
1339 /* stop all OHCI interrupts */
1340 writel(~0x0, &ohci
->regs
->intrdisable
);
1342 if (ohci
->root_hub
) {
1343 /* ensure that HC is stopped before releasing the HCCA */
1344 writel(OHCI_USB_SUSPEND
, &ohci
->regs
->control
);
1345 free_page((unsigned long) ohci
->root_hub
->hcca
);
1346 kfree(ohci
->root_hub
);
1347 ohci
->root_hub
->hcca
= NULL
;
1348 ohci
->root_hub
= NULL
;
1351 /* unmap the IO address space */
1352 iounmap(ohci
->regs
);
1358 /* If the ohci itself were dynamic we'd free it here */
1360 printk(KERN_DEBUG
"usb-ohci: HC resources released.\n");
1361 } /* release_ohci() */
1365 * USB OHCI control thread
1367 static int ohci_control_thread(void * __ohci
)
1369 struct ohci
*ohci
= (struct ohci
*)__ohci
;
1372 * I'm unfamiliar with the SMP kernel locking.. where should
1373 * this be released and what does it do? -greg
1378 * This thread doesn't need any user-level access,
1379 * so get rid of all of our resources..
1381 printk("ohci_control_thread code at %p\n", &ohci_control_thread
);
1383 exit_files(current
);
1386 strcpy(current
->comm
, "ohci-control");
1389 * Damn the torpedoes, full speed ahead
1391 if (start_hc(ohci
) < 0) {
1392 printk("usb-ohci: failed to start the controller\n");
1394 printk(KERN_DEBUG
"leaving ohci_control_thread %p\n", __ohci
);
1400 int unsigned long signr
;
1404 /* check the root hub configuration for changes. */
1405 ohci_check_configuration(ohci
);
1407 /* re-enable root hub status change interrupts. */
1409 writel(OHCI_INTR_RHSC
, &ohci
->regs
->intrenable
);
1412 printk(KERN_DEBUG
"ohci-control thread sleeping\n");
1413 interruptible_sleep_on(&ohci_configure
);
1417 if (start_hc(ohci
) < 0)
1424 * If we were woken up by a signal, see if its useful,
1427 if (signal_pending(current
)) {
1428 /* sending SIGUSR1 makes us print out some info */
1429 spin_lock_irq(¤t
->sigmask_lock
);
1430 signr
= dequeue_signal(¤t
->blocked
, &info
);
1431 spin_unlock_irq(¤t
->sigmask_lock
);
1433 if(signr
== SIGUSR1
) {
1434 /* FIXME: have it do a full ed/td queue dump */
1435 printk(KERN_DEBUG
"OHCI status dump:\n");
1436 show_ohci_status(ohci
);
1438 /* unknown signal, exit the thread */
1447 printk(KERN_DEBUG
"leaving ohci_control_thread %p\n", __ohci
);
1450 } /* ohci_control_thread() */
1454 static int handle_apm_event(apm_event_t event
)
1456 static int down
= 0;
1459 case APM_SYS_SUSPEND
:
1460 case APM_USER_SUSPEND
:
1462 printk(KERN_DEBUG
"usb-ohci: received extra suspend event\n");
1467 case APM_NORMAL_RESUME
:
1468 case APM_CRITICAL_RESUME
:
1470 printk(KERN_DEBUG
"usb-ohci: received bogus resume event\n");
1474 if (waitqueue_active(&ohci_configure
)) {
1476 wake_up(&ohci_configure
);
1481 } /* handle_apm_event() */
1487 * Inspired by Iñaky's driver. This function is a timer routine that
1488 * is called every OHCI_TIMER_FREQ ms. It polls the root hub for
1489 * status changes as on my system the RHSC interrupt just doesn't
1490 * play well with others.. (so RHSC is turned off by default in this
1492 * [my controller is a "SiS 7001 USB (rev 16)"]
1495 static void ohci_timer_func (unsigned long ohci_ptr
)
1497 struct ohci
*ohci
= (struct ohci
*)ohci_ptr
;
1499 ohci_root_hub_events(ohci
);
1501 /* set the next timer */
1502 mod_timer(&ohci_timer
, jiffies
+ ((OHCI_TIMER_FREQ
*HZ
)/1000));
1504 } /* ohci_timer_func() */
1509 * Increment the module usage count, start the control thread and
1510 * return success if the controller is good.
1512 static int found_ohci(int irq
, void* mem_base
)
1518 printk(KERN_DEBUG
"entering found_ohci %d %p\n", irq
, mem_base
);
1521 /* Allocate the running OHCI structures */
1522 ohci
= alloc_ohci(mem_base
);
1528 init_timer(&ohci_timer
);
1529 ohci_timer
.expires
= jiffies
+ ((OHCI_TIMER_FREQ
*HZ
)/1000);
1530 ohci_timer
.data
= (unsigned long)ohci
;
1531 ohci_timer
.function
= ohci_timer_func
;
1532 add_timer(&ohci_timer
);
1536 if (request_irq(irq
, ohci_interrupt
, SA_SHIRQ
, "usb-ohci", ohci
) == 0) {
1542 printk(KERN_DEBUG
"usb-ohci: starting ohci-control thread\n");
1545 /* fork off the handler */
1546 pid
= kernel_thread(ohci_control_thread
, ohci
,
1547 CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
);
1554 printk("usb-ohci: Couldn't allocate interrupt %d\n", irq
);
1559 printk(KERN_DEBUG
"leaving found_ohci %d %p\n", irq
, mem_base
);
1563 } /* found_ohci() */
1567 * If this controller is for real, map the IO memory and proceed
1569 static int init_ohci(struct pci_dev
*dev
)
1571 unsigned long mem_base
= dev
->base_address
[0];
1573 /* If its OHCI, its memory */
1574 if (mem_base
& PCI_BASE_ADDRESS_SPACE_IO
)
1577 /* Get the memory address and map it for IO */
1578 mem_base
&= PCI_BASE_ADDRESS_MEM_MASK
;
1580 /* no interrupt won't work... */
1581 if (dev
->irq
== 0) {
1582 printk("usb-ohci: no irq assigned? check your BIOS settings.\n");
1587 * FIXME ioremap_nocache isn't implemented on all CPUs (such
1588 * as the Alpha) [?] What should I use instead...
1590 * The iounmap() is done on in release_ohci.
1592 mem_base
= (unsigned long) ioremap_nocache(mem_base
, 4096);
1595 printk("Error mapping OHCI memory\n");
1600 if (found_ohci(dev
->irq
, (void *) mem_base
) < 0) {
1610 * Clean up when unloading the module
1612 void cleanup_module(void)
1615 apm_unregister_callback(&handle_apm_event
);
1617 #ifdef CONFIG_USB_MOUSE
1618 usb_mouse_cleanup();
1620 printk("usb-ohci: module unloaded\n");
1623 #define ohci_init init_module
1628 /* TODO this should be named following Linux convention and go in pci.h */
1629 #define PCI_CLASS_SERIAL_USB_OHCI ((PCI_CLASS_SERIAL_USB << 8) | 0x0010)
1632 * Search the PCI bus for an OHCI USB controller and set it up
1634 * If anyone wants multiple controllers this will need to be
1635 * updated.. Right now, it just picks the first one it finds.
1640 struct pci_dev
*dev
= NULL
;
1643 if (sizeof(struct ohci_device
) > 4096) {
1644 printk("usb-ohci: struct ohci_device to large\n");
1648 printk("OHCI USB Driver loading\n");
1652 /* Find an OHCI USB controller */
1653 dev
= pci_find_class(PCI_CLASS_SERIAL_USB_OHCI
, dev
);
1657 /* Verify that its OpenHCI by checking for MMIO */
1658 /* pci_read_config_byte(dev, PCI_CLASS_PROG, &type);
1663 retval
= init_ohci(dev
);
1668 apm_register_callback(&handle_apm_event
);
1671 return 0; /* no error */