Import 2.3.4pre3
[davej-history.git] / drivers / usb / ohci-hcd.c
blob820efc5dcb49f9e21e97e4a5f34c2dd7679c44fc
1 /*
2 * OHCI HCD (Host Controller Driver) for USB.
4 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
6 * The OHCI HCD layer is a simple but nearly complete implementation of what
7 * the USB people would call a HCD for the OHCI.
8 * (ISO comming soon, Bulk disabled, INT u. CTRL transfers enabled)
9 * The layer on top of it, is for interfacing to the alternate-usb
10 * device-drivers.
12 * [ This is based on Linus' UHCI code and gregs OHCI fragments
13 * (0.03c source tree). ]
14 * [ Open Host Controller Interface driver for USB. ]
15 * [ (C) Copyright 1999 Linus Torvalds (uhci.c) ]
16 * [ (C) Copyright 1999 Gregory P. Smith <greg@electricrain.com> ]
17 * [ $Log: ohci.c,v $ ]
18 * [ Revision 1.1 1999/04/05 08:32:30 greg ]
21 * v2.1 1999/05/09 ep_addr correction, code clean up
22 * v2.0 1999/05/04
23 * virtual root hub is now an option,
24 * memory allocation based on kmalloc and kfree now, Bus error handling,
25 * INT and CTRL transfers enabled, Bulk included but disabled, ISO needs completion
27 * from Linus Torvalds (uhci.c) (APM not tested; hub, usb_device, bus and related stuff)
28 * from Greg Smith (ohci.c) (reset controller handling, hub)
30 * v1.0 1999/04/27 initial release
31 * ohci-hcd.c
34 /* #define OHCI_DBG */ /* printk some debug information */
37 #include <linux/config.h>
38 #include <linux/module.h>
39 #include <linux/pci.h>
40 #include <linux/kernel.h>
41 #include <linux/delay.h>
42 #include <linux/ioport.h>
43 #include <linux/sched.h>
44 #include <linux/malloc.h>
45 #include <linux/smp_lock.h>
46 #include <linux/errno.h>
47 #include <linux/timer.h>
49 #include <asm/spinlock.h>
50 #include <asm/io.h>
51 #include <asm/irq.h>
52 #include <asm/system.h>
54 #include "usb.h"
55 #include "ohci-hcd.h"
57 #ifdef CONFIG_APM
58 #include <linux/apm_bios.h>
59 static int handle_apm_event(apm_event_t event);
60 static int apm_resume = 0;
61 #endif
64 static DECLARE_WAIT_QUEUE_HEAD(bulk_wakeup);
65 static DECLARE_WAIT_QUEUE_HEAD(control_wakeup);
66 static DECLARE_WAIT_QUEUE_HEAD(root_hub);
68 static __u8 cc_to_status[16] = { /* mapping of the OHCI CC to the UHCI status codes; first guess */
69 /* Activ, Stalled, Data Buffer Err, Babble Detected : NAK recvd, CRC/Timeout, Bitstuff, reservd */
70 /* No Error */ 0x00,
71 /* CRC Error */ 0x04,
72 /* Bit Stuff */ 0x02,
73 /* Data Togg */ 0x40,
74 /* Stall */ 0x40,
75 /* DevNotResp */ 0x04,
76 /* PIDCheck */ 0x04,
77 /* UnExpPID */ 0x40,
78 /* DataOver */ 0x20,
79 /* DataUnder */ 0x20,
80 /* reservd */ 0x40,
81 /* reservd */ 0x40,
82 /* BufferOver */ 0x20,
83 /* BuffUnder */ 0x20,
84 /* Not Access */ 0x80,
85 /* Not Access */ 0x80
89 /********
90 **** Interface functions
91 ***********************************************/
93 static int sohci_int_handler(void * ohci_in, unsigned int ep_addr, int ctrl_len, void * ctrl, void * data, int data_len, int status, __OHCI_BAG lw0, __OHCI_BAG lw1)
96 struct ohci * ohci = ohci_in;
97 usb_device_irq handler=(void *) lw0;
98 void *dev_id = (void *) lw1;
99 int ret;
101 OHCI_DEBUG({ int i; printk("USB HC IRQ <<<: %x: data(%d):", ep_addr, data_len);)
102 OHCI_DEBUG( for(i=0; i < data_len; i++ ) printk(" %02x", ((__u8 *) data)[i]);)
103 OHCI_DEBUG( printk(" ret_status: %x\n", status); })
105 ret = handler(cc_to_status[status & 0xf], data, dev_id);
106 if(ret == 0) return 0; /* 0 .. do not requeue */
107 if(status > 0) return -1; /* error occured do not requeue ? */
108 ohci_trans_req(ohci, ep_addr, 0, NULL, data, 8, (__OHCI_BAG) handler, (__OHCI_BAG) dev_id); /* requeue int request */
109 return 0;
112 static int sohci_ctrl_handler(void * ohci_in, unsigned int ep_addr, int ctrl_len, void * ctrl, void * data, int data_len, int status, __OHCI_BAG lw0, __OHCI_BAG lw)
114 *(int * )lw0 = status;
115 wake_up(&control_wakeup);
117 OHCI_DEBUG( { int i; printk("USB HC CTRL<<<: %x: ctrl(%d):", ep_addr, ctrl_len);)
118 OHCI_DEBUG( for(i=0; i < 8; i++ ) printk(" %02x", ((__u8 *) ctrl)[i]);)
119 OHCI_DEBUG( printk(" data(%d):", data_len);)
120 OHCI_DEBUG( for(i=0; i < data_len; i++ ) printk(" %02x", ((__u8 *) data)[i]);)
121 OHCI_DEBUG( printk(" ret_status: %x\n", status); })
122 return 0;
125 static int sohci_bulk_handler(void * ohci_in, unsigned int ep_addr, int ctrl_len, void * ctrl, void * data, int data_len, int status, __OHCI_BAG lw0, __OHCI_BAG lw)
127 *(int * )lw0 = status;
128 wake_up(&bulk_wakeup);
130 OHCI_DEBUG( { int i; printk("USB HC BULK<<<: %x:", ep_addr, ctrl_len);)
131 OHCI_DEBUG( printk(" data(%d):", data_len);)
132 OHCI_DEBUG( for(i=0; i < data_len; i++ ) printk(" %02x", ((__u8 *) data)[i]);)
133 OHCI_DEBUG( printk(" ret_status: %x\n", status); })
134 return 0;
137 static int sohci_request_irq(struct usb_device *usb_dev, unsigned int pipe, usb_device_irq handler, int period, void *dev_id)
139 struct ohci * ohci = usb_dev->bus->hcpriv;
140 union ep_addr_ ep_addr;
142 ep_addr.iep = 0;
143 ep_addr.bep.ep = ((pipe >> 15) & 0x0f) /* endpoint address */
144 | (pipe & 0x80) /* direction */
145 | (1 << 5); /* type = int*/
146 ep_addr.bep.fa = ((pipe >> 8) & 0x7f); /* device address */
148 OHCI_DEBUG( printk("USB HC IRQ >>>: %x: every %d ms\n", ep_addr.iep, period);)
150 usb_ohci_add_ep(ohci, ep_addr.iep, period, 1, sohci_int_handler, 1 << ((pipe & 0x03) + 3) , (pipe >> 26) & 0x01);
152 ohci_trans_req(ohci, ep_addr.iep, 0, NULL, ((struct ohci_device *) usb_dev->hcpriv)->data, 8, (__OHCI_BAG) handler, (__OHCI_BAG) dev_id);
153 return 0;
157 static int sohci_control_msg(struct usb_device *usb_dev, unsigned int pipe, void *cmd, void *data, int len)
159 DECLARE_WAITQUEUE(wait, current);
160 struct ohci * ohci = usb_dev->bus->hcpriv;
161 int status;
162 union ep_addr_ ep_addr;
164 ep_addr.iep = 0;
165 ep_addr.bep.ep = ((pipe >> 15) & 0x0f) /* endpoint address */
166 | (pipe & 0x80) /* direction */
167 | (1 << 6); /* type = ctrl*/
168 ep_addr.bep.fa = ((pipe >> 8) & 0x7f); /* device address */
170 status = 0xf; /* CC not Accessed */
171 OHCI_DEBUG( { int i; printk("USB HC CTRL>>>: %x: ctrl(%d):", ep_addr.iep, 8);)
172 OHCI_DEBUG( for(i=0; i < 8; i++ ) printk(" %02x", ((__u8 *) cmd)[i]);)
173 OHCI_DEBUG( printk(" data(%d):", len);)
174 OHCI_DEBUG( for(i=0; i < len; i++ ) printk(" %02x", ((__u8 *) data)[i]);)
175 OHCI_DEBUG( printk("\n"); })
177 usb_ohci_add_ep(ohci, ep_addr.iep, 0, 1, sohci_ctrl_handler, 1 << ((pipe & 0x03) + 3) , (pipe >> 26) & 0x01);
179 current->state = TASK_UNINTERRUPTIBLE;
180 add_wait_queue(&control_wakeup, &wait);
182 ohci_trans_req(ohci, ep_addr.iep, 8, cmd, data, len, (__OHCI_BAG) &status, 0);
184 schedule_timeout(HZ/10);
186 remove_wait_queue(&control_wakeup, &wait);
188 OHCI_DEBUG(printk("USB HC status::: %x\n", cc_to_status[status & 0x0f]);)
190 return cc_to_status[status & 0x0f];
193 static int sohci_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, void *data, int len)
195 DECLARE_WAITQUEUE(wait, current);
196 struct ohci * ohci = usb_dev->bus->hcpriv;
197 int status;
198 union ep_addr_ ep_addr;
200 ep_addr.iep = 0;
201 ep_addr.bep.ep = ((pipe >> 15) & 0x0f) /* endpoint address */
202 | (pipe & 0x80) /* direction */
203 | (11 << 5); /* type = bulk*/
204 ep_addr.bep.fa = ((pipe >> 8) & 0x7f); /* device address */
206 status = 0xf; /* CC not Accessed */
207 OHCI_DEBUG( { int i; printk("USB HC BULK>>>: %x:", ep_addr.iep);)
208 OHCI_DEBUG( printk(" data(%d):", len);)
209 OHCI_DEBUG( for(i=0; i < len; i++ ) printk(" %02x", ((__u8 *) data)[i]);)
210 OHCI_DEBUG( printk("\n"); })
212 usb_ohci_add_ep(ohci, ep_addr.iep, 0, 1, sohci_bulk_handler, 1 << ((pipe & 0x03) + 3) , (pipe >> 26) & 0x01);
214 current->state = TASK_UNINTERRUPTIBLE;
215 add_wait_queue(&bulk_wakeup, &wait);
217 ohci_trans_req(ohci, ep_addr.iep, 0, NULL, data, len, (__OHCI_BAG) &status, 0);
219 schedule_timeout(HZ/10);
221 remove_wait_queue(&bulk_wakeup, &wait);
223 OHCI_DEBUG(printk("USB HC status::: %x\n", cc_to_status[status & 0x0f]);)
225 return cc_to_status[status & 0x0f];
228 static int sohci_usb_deallocate(struct usb_device *usb_dev) {
229 struct ohci_device *dev = usb_to_ohci(usb_dev);
230 union ep_addr_ ep_addr;
232 ep_addr.iep = 0;
234 OHCI_DEBUG(printk("USB HC dealloc %x\n", usb_dev->devnum);)
236 /* wait_ms(20); */
238 if(usb_dev->devnum >=0) {
239 ep_addr.bep.fa = usb_dev->devnum;
240 usb_ohci_rm_function(((struct ohci_device *)usb_dev->hcpriv)->ohci, ep_addr.iep);
243 USB_FREE(dev);
244 USB_FREE(usb_dev);
246 return 0;
249 static struct usb_device *sohci_usb_allocate(struct usb_device *parent) {
251 struct usb_device *usb_dev;
252 struct ohci_device *dev;
255 USB_ALLOC(usb_dev, sizeof(*usb_dev));
256 if (!usb_dev)
257 return NULL;
259 memset(usb_dev, 0, sizeof(*usb_dev));
261 USB_ALLOC(dev, sizeof(*dev));
262 if (!dev) {
263 USB_FREE(usb_dev);
264 return NULL;
267 /* Initialize "dev" */
268 memset(dev, 0, sizeof(*dev));
270 usb_dev->hcpriv = dev;
271 dev->usb = usb_dev;
273 usb_dev->parent = parent;
275 if (parent) {
276 usb_dev->bus = parent->bus;
277 dev->ohci = usb_to_ohci(parent)->ohci;
279 return usb_dev;
283 struct usb_operations sohci_device_operations = {
284 sohci_usb_allocate,
285 sohci_usb_deallocate,
286 sohci_control_msg,
287 sohci_bulk_msg,
288 sohci_request_irq,
292 /******
293 *** ED handling functions
294 ************************************/
299 * search for the right place to insert an interrupt ed into the int tree
300 * do some load ballancing
301 * */
303 static int usb_ohci_int_ballance(struct ohci * ohci, int interval, int load) {
305 int i,j;
307 j = 0; /* search for the least loaded interrupt endpoint branch of all 32 branches */
308 for(i=0; i< 32; i++) if(ohci->ohci_int_load[j] > ohci->ohci_int_load[i]) j=i;
310 if(interval < 1) interval = 1;
311 if(interval > 32) interval = 32;
312 for(i= 0; ((interval >> i) > 1 ); interval &= (0xfffe << i++ )); /* interval = 2^int(ld(interval)) */
315 for(i=j%interval; i< 32; i+=interval) ohci->ohci_int_load[i] += load;
316 j = interval + (j % interval);
318 OHCI_DEBUG(printk("USB HC new int ed on pos : %x \n",j);)
320 return j;
323 /* get the ed from the endpoint / device adress */
325 struct usb_ohci_ed * ohci_find_ep(struct ohci *ohci, unsigned int ep_addr_in) {
327 union ep_addr_ ep_addr;
328 struct usb_ohci_ed *tmp;
329 unsigned int mask;
331 mask = 0;
332 ep_addr.iep = ep_addr_in;
334 #ifdef VROOTHUB
335 if(ep_addr.bep.fa == ohci->root_hub_funct_addr) {
336 if((ep_addr.bep.ep & 0x0f) == 0)
337 return &ohci->ed_rh_ep0; /* root hub ep0 */
338 else
339 return &ohci->ed_rh_epi; /* root hub int ep */
341 #endif
343 tmp = ohci->ed_func_ep0[ep_addr.bep.fa];
344 mask = ((((ep_addr.bep.ep >> 5) & 0x03)==2)?0x7f:0xff);
345 ep_addr.bep.ep &= mask; /* mask out direction of ctrl ep */
347 while (tmp != NULL) {
348 if (tmp->ep_addr.iep == ep_addr.iep)
349 return tmp;
350 tmp = tmp->ed_list;
352 return NULL;
355 spinlock_t usb_ed_lock = SPIN_LOCK_UNLOCKED;
356 /* add a new endpoint ep_addr */
357 struct usb_ohci_ed *usb_ohci_add_ep(struct ohci * ohci, unsigned int ep_addr_in, int interval, int load, f_handler handler, int ep_size, int speed) {
359 struct usb_ohci_ed * ed;
360 struct usb_ohci_td * td;
361 union ep_addr_ ep_addr;
364 int int_junk;
366 struct usb_ohci_ed *tmp;
368 ep_addr.iep = ep_addr_in ;
369 ep_addr.bep.ep &= ((((ep_addr.bep.ep >> 5) & 0x03)==2)?0x7f:0xff); /* mask out direction of ctrl ep */
371 spin_lock(&usb_ed_lock);
373 tmp = ohci_find_ep(ohci, ep_addr.iep);
374 if (tmp != NULL) {
376 #ifdef VROOTHUB
377 if(ep_addr.bep.fa == ohci->root_hub_funct_addr) {
378 if((ep_addr.bep.ep & 0x0f) != 0) { /* root hub int ep */
379 ohci->ed_rh_epi.handler = handler;
380 ohci_init_rh_int_timer(ohci, interval);
382 else { /* root hub ep0 */
383 ohci->ed_rh_ep0.handler = handler;
387 else
388 #endif
391 tmp->hw.info = ep_addr.bep.fa | ((ep_addr.bep.ep & 0xf) <<7)
393 | (((ep_addr.bep.ep & 0x60) == 0)? 0x8000 : 0)
394 | (speed << 13)
395 | ep_size <<16;
397 tmp->handler = handler;
399 spin_unlock(&usb_ed_lock);
400 return tmp; /* ed already in use */
404 OHCI_ALLOC(td, sizeof(td)); /* dummy td; end of td list for ed */
405 OHCI_ALLOC(ed, sizeof(ed));
406 td->prev_td = NULL;
408 ed->hw.tail_td = virt_to_bus(&td->hw);
409 ed->hw.head_td = ed->hw.tail_td;
410 ed->hw.info = ep_addr.bep.fa | ((ep_addr.bep.ep & 0xf) <<7)
411 /* | ((ep_addr.bep.port & 0x80)? 0x1000 : 0x0800 ) */
412 | (((ep_addr.bep.ep & 0x60) == 0)? 0x8000 : 0)
413 | (speed << 13)
414 | ep_size <<16;
416 ed->handler = handler;
418 switch((ep_addr.bep.ep >> 5) & 0x03) {
419 case CTRL:
420 ed->hw.next_ed = 0;
421 if(ohci->ed_controltail == NULL) {
422 writel(virt_to_bus(&ed->hw), &ohci->regs->ed_controlhead);
424 else {
425 ohci->ed_controltail->hw.next_ed = virt_to_bus(&ed->hw);
427 ed->ed_prev = ohci->ed_controltail;
428 ohci->ed_controltail = ed;
429 break;
430 case BULK:
431 ed->hw.next_ed = 0;
432 if(ohci->ed_bulktail == NULL) {
433 writel(virt_to_bus(&ed->hw), &ohci->regs->ed_bulkhead);
435 else {
436 ohci->ed_bulktail->hw.next_ed = virt_to_bus(&ed->hw);
438 ed->ed_prev = ohci->ed_bulktail;
439 ohci->ed_bulktail = ed;
440 break;
441 case INT:
442 int_junk = usb_ohci_int_ballance(ohci, interval, load);
443 ed->hw.next_ed = ohci->hc_area->ed[int_junk].next_ed;
444 ohci->hc_area->ed[int_junk].next_ed = virt_to_bus(&ed->hw);
445 ed->ed_prev = (struct usb_ohci_ed *) &ohci->hc_area->ed[int_junk];
446 break;
447 case ISO:
448 ed->hw.next_ed = 0;
449 ohci->ed_isotail->hw.next_ed = virt_to_bus(&ed->hw);
450 ed->ed_prev = ohci->ed_isotail;
451 ohci->ed_isotail = ed;
452 break;
454 ed->ep_addr = ep_addr;
456 /* Add it to the "hash"-table of known endpoint descriptors */
458 ed->ed_list = ohci->ed_func_ep0[ed->ep_addr.bep.fa];
459 ohci->ed_func_ep0[ed->ep_addr.bep.fa] = ed;
461 spin_unlock(&usb_ed_lock);
463 OHCI_DEBUG(printk("USB HC new ed %x: %x :", ep_addr.iep, (unsigned int ) ed); )
464 OHCI_DEBUG({ int i; for( i= 0; i<8 ;i++) printk(" %4x", ((unsigned int *) ed)[i]) ; printk("\n"); }; )
465 return 0;
468 /*****
469 * Request the removal of an endpoint
471 * put the ep on the rm_list and request a stop of the bulk or ctrl list
472 * real removal is done at the next start of frame hardware interrupt
474 int usb_ohci_rm_ep(struct ohci * ohci, struct usb_ohci_ed *ed)
476 unsigned int flags;
477 struct usb_ohci_ed *tmp;
479 OHCI_DEBUG(printk("USB HC remove ed %x: %x :\n", ed->ep_addr.iep, (unsigned int ) ed); )
481 spin_lock_irqsave(&usb_ed_lock, flags);
483 tmp = ohci->ed_func_ep0[ed->ep_addr.bep.fa];
484 if (tmp == NULL) {
485 spin_unlock_irqrestore(&usb_ed_lock, flags);
486 return 0;
489 if(tmp == ed) {
490 ohci->ed_func_ep0[ed->ep_addr.bep.fa] = ed->ed_list;
492 else {
493 while (tmp->ed_list != ed) {
494 if (tmp->ed_list == NULL) {
495 spin_unlock_irqrestore(&usb_ed_lock, flags);
496 return 0;
498 tmp = tmp->ed_list;
500 tmp->ed_list = ed->ed_list;
502 ed->ed_list = ohci->ed_rm_list;
503 ohci->ed_rm_list = ed;
504 ed->hw.info |= OHCI_ED_SKIP;
506 switch((ed->ep_addr.bep.ep >> 5) & 0x03) {
507 case CTRL:
508 writel_mask(~(0x01<<4), &ohci->regs->control); /* stop CTRL list */
509 break;
510 case BULK:
511 writel_mask(~(0x01<<5), &ohci->regs->control); /* stop BULK list */
512 break;
516 writel( OHCI_INTR_SF, &ohci->regs->intrenable); /* enable sof interrupt */
518 spin_unlock_irqrestore(&usb_ed_lock, flags);
520 return 1;
523 /* we have requested to stop the bulk or CTRL list,
524 * now we can remove the eds on the rm_list */
526 static int ohci_rm_eds(struct ohci * ohci) {
528 unsigned int flags;
529 struct usb_ohci_ed *ed;
530 struct usb_ohci_ed *ed_tmp;
531 struct usb_ohci_td *td;
532 __u32 td_hw_tmp;
533 __u32 td_hw;
535 spin_lock_irqsave(&usb_ed_lock, flags);
537 ed = ohci->ed_rm_list;
539 while (ed != NULL) {
541 switch((ed->ep_addr.bep.ep >> 5) & 0x03) {
542 case CTRL:
543 if(ed->ed_prev == NULL) {
544 writel(ed->hw.next_ed, &ohci->regs->ed_controlhead);
546 else {
547 ed->ed_prev->hw.next_ed = ed->hw.next_ed;
549 if(ohci->ed_controltail == ed) {
550 ohci->ed_controltail = ed->ed_prev;
552 break;
553 case BULK:
554 if(ed->ed_prev == NULL) {
555 writel(ed->hw.next_ed, &ohci->regs->ed_bulkhead);
557 else {
558 ed->ed_prev->hw.next_ed = ed->hw.next_ed;
560 if(ohci->ed_bulktail == ed) {
561 ohci->ed_bulktail = ed->ed_prev;
563 break;
564 case INT:
565 ed->ed_prev->hw.next_ed = ed->hw.next_ed;
566 break;
567 case ISO:
568 ed->ed_prev->hw.next_ed = ed->hw.next_ed;
569 if(ohci->ed_isotail == ed) {
570 ohci->ed_isotail = ed->ed_prev;
572 break;
575 if(ed->hw.next_ed != 0) ((struct usb_ohci_ed *) bus_to_virt(ed->hw.next_ed))->ed_prev = ed->ed_prev;
578 /* tds directly connected to ed */
580 td_hw = ed->hw.head_td & 0xfffffff0;
581 while(td_hw != 0) {
582 td = bus_to_virt(td_hw);
583 td_hw_tmp = td_hw;
584 td_hw = td->hw.next_td;
585 OHCI_FREE(td); /* free pending tds */
586 if(td_hw_tmp == ed->hw.tail_td) break;
590 /* mark TDs on the hc done list (if there are any) */
591 td_hw = readl(&ohci->regs->donehead) & 0xfffffff0;
592 while(td_hw != 0) {
593 td = bus_to_virt(td_hw);
594 td_hw = td->hw.next_td;
595 if(td->ep == ed) td->ep = 0;
598 /* mark TDs on the hcca done list (if there are any) */
599 td_hw = ohci->hc_area->hcca.done_head & 0xfffffff0 ;
601 while(td_hw != 0) {
602 td = bus_to_virt(td_hw);
603 td_hw = td->hw.next_td;
604 if(td->ep == ed) td->ep = 0;
607 ed_tmp = ed;
608 ed = ed->ed_list;
609 OHCI_FREE(ed_tmp); /* free ed */
611 writel(0, &ohci->regs->ed_controlcurrent); /* reset CTRL list */
612 writel(0, &ohci->regs->ed_bulkcurrent); /* reset BULK list */
613 writel_set((0x03<<4), &ohci->regs->control); /* start CTRL u. (BULK list) */
615 spin_unlock_irqrestore(&usb_ed_lock, flags);
617 ohci->ed_rm_list = NULL;
618 OHCI_DEBUG(printk("USB HC after rm ed control: %4x intrstat: %4x intrenable: %4x\n", readl(&ohci->regs->control),readl(&ohci->regs->intrstatus),readl(&ohci->regs->intrenable));)
621 return 0;
624 /* remove all endpoints of a function (device) */
625 int usb_ohci_rm_function( struct ohci * ohci, unsigned int ep_addr_in)
627 struct usb_ohci_ed *ed;
628 struct usb_ohci_ed *tmp;
629 union ep_addr_ ep_addr;
633 ep_addr.iep = ep_addr_in;
635 for(ed = ohci->ed_func_ep0[ep_addr.bep.fa]; ed != NULL;) {
636 tmp = ed;
637 ed = ed->ed_list;
638 usb_ohci_rm_ep(ohci, tmp);
643 return 1;
650 /******
651 *** TD handling functions
652 ************************************/
655 #define FILL_TD(TD_PT, HANDLER, INFO, DATA, LEN, LW0, LW1) \
656 td_pt = (TD_PT); \
657 td_pt1 = (struct usb_ohci_td *) bus_to_virt(usb_ep->hw.tail_td); \
658 td_pt1->ep = usb_ep; \
659 td_pt1->handler = (HANDLER); \
660 td_pt1->buffer_start = (DATA); \
661 td_pt1->lw0 = (LW0); \
662 td_pt1->lw1 = (LW1); \
663 td_pt1->hw.info = (INFO); \
664 td_pt1->hw.cur_buf = virt_to_bus(DATA); \
665 td_pt1->hw.buf_end = td_pt1->hw.cur_buf + (LEN) - 1; \
666 td_pt1->hw.next_td = virt_to_bus(td_pt); \
667 usb_ep->hw.tail_td = virt_to_bus(td_pt); \
668 td_pt->prev_td = td_pt1; \
669 td_pt->hw.next_td = 0
671 spinlock_t usb_req_lock = SPIN_LOCK_UNLOCKED;
673 int ohci_trans_req(struct ohci * ohci, unsigned int ep_addr, int ctrl_len, void *ctrl, void * data, int data_len, __OHCI_BAG lw0, __OHCI_BAG lw1) {
675 int ed_type;
676 unsigned int flags;
677 struct usb_ohci_td *td_pt;
678 struct usb_ohci_td *td_pt1;
679 struct usb_ohci_td *td_pt_a1, *td_pt_a2, *td_pt_a3;
680 struct usb_ohci_ed *usb_ep;
681 f_handler handler;
684 td_pt_a1 =NULL;
685 td_pt_a2 =NULL;
686 td_pt_a3 =NULL;
688 usb_ep = ohci_find_ep(ohci, ep_addr);
689 if(usb_ep == NULL ) return -1; /* not known ep */
691 handler = usb_ep->handler;
693 #ifdef VROOTHUB
694 if(usb_ep == &ohci->ed_rh_ep0) { /* root hub ep 0 control endpoint */
695 root_hub_control_msg(ohci, 8, ctrl, data, data_len, lw0, lw1, handler);
696 return 0;
699 if(usb_ep == &ohci->ed_rh_epi) { /* root hub interrupt endpoint */
701 root_hub_int_req(ohci, 8, ctrl, data, data_len, lw0, lw1, handler);
702 return 0;
704 #endif
705 /* struct usb_ohci_ed * usb_ep = usb_ohci_add_ep(pipe, ohci, interval, 1); */
707 ed_type = ((((union ep_addr_)ep_addr).bep.ep >> 5) & 0x07);
709 switch(ed_type) {
710 case BULK_IN:
711 case BULK_OUT:
712 case INT_IN:
713 case INT_OUT:
714 OHCI_ALLOC(td_pt_a1, sizeof(td_pt_a1));
715 break;
717 case CTRL_IN:
718 case CTRL_OUT:
719 OHCI_ALLOC(td_pt_a1, sizeof(td_pt_a1));
720 OHCI_ALLOC(td_pt_a3, sizeof(td_pt_a3));
721 if(data_len > 0) {
722 OHCI_ALLOC(td_pt_a2, sizeof(td_pt_a2));
724 break;
726 case ISO_IN:
727 case ISO_OUT:
731 spin_lock_irqsave(&usb_req_lock, flags);
733 switch(ed_type) {
734 case BULK_IN:
735 FILL_TD( td_pt_a1, handler, TD_CC | TD_R | TD_DP_IN | TD_T_TOGGLE, data, data_len, lw0, lw1 );
736 writel( OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
737 break;
739 case BULK_OUT:
740 FILL_TD( td_pt_a1, handler, TD_CC | TD_DP_OUT | TD_T_TOGGLE, data, data_len, lw0, lw1 );
741 writel( OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
742 break;
744 case INT_IN:
745 FILL_TD( td_pt_a1, handler, TD_CC | TD_R | TD_DP_IN | TD_T_TOGGLE, data, data_len, lw0, lw1 );
746 break;
748 case INT_OUT:
749 FILL_TD( td_pt_a1, handler, TD_CC | TD_DP_OUT | TD_T_TOGGLE, data, data_len, lw0, lw1 );
750 break;
752 case CTRL_IN:
753 FILL_TD( td_pt_a1, NULL, TD_CC | TD_DP_SETUP | TD_T_DATA0, ctrl, ctrl_len, 0, 0 );
754 if(data_len > 0) {
755 FILL_TD( td_pt_a2, NULL, TD_CC | TD_R | TD_DP_IN | TD_T_DATA1, data, data_len, 0, 0 );
757 FILL_TD( td_pt_a3, handler, TD_CC | TD_DP_OUT | TD_T_DATA1, NULL, 0, lw0, lw1 );
758 writel( OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
759 break;
761 case CTRL_OUT:
762 FILL_TD( td_pt_a1, NULL, TD_CC | TD_DP_SETUP | TD_T_DATA0, ctrl, ctrl_len, 0, 0 );
763 if(data_len > 0) {
764 FILL_TD( td_pt_a2, NULL, TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1, data, data_len, 0, 0 );
766 FILL_TD( td_pt_a3, handler, TD_CC | TD_DP_IN | TD_T_DATA1, NULL, 0, lw0, lw1 );
767 writel( OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
768 break;
770 case ISO_IN:
771 case ISO_OUT:
772 break;
778 td_pt1 = (struct usb_ohci_td *) bus_to_virt(usb_ep->hw.tail_td);
781 if(td_pt_a3 != NULL) td_pt_a3->prev_td = NULL;
782 else if (td_pt_a2 != NULL) td_pt_a2->prev_td = NULL;
783 else if (td_pt_a1 != NULL) td_pt_a1->prev_td = NULL;
785 spin_unlock_irqrestore(&usb_req_lock, flags);
786 return 0;
790 /******
791 *** Done List handling functions
792 ************************************/
794 /* replies to the request have to be on a FIFO basis so
795 * we reverse the reversed done-list */
797 static struct usb_ohci_td * ohci_reverse_done_list(struct ohci * ohci) {
799 __u32 td_list_hc;
800 struct usb_ohci_td * td_list = NULL;
801 struct usb_ohci_td * td_rev = NULL;
803 td_list_hc = ohci->hc_area->hcca.done_head & 0xfffffff0;
804 ohci->hc_area->hcca.done_head = 0;
806 while(td_list_hc) {
808 td_list = (struct usb_ohci_td *) bus_to_virt(td_list_hc);
809 td_list->next_dl_td = td_rev;
811 td_rev = td_list;
812 td_list_hc = td_list->hw.next_td & 0xfffffff0;
814 return td_list;
817 /* all done requests are replied here */
818 static int usb_ohci_done_list(struct ohci * ohci) {
820 struct usb_ohci_td * td = NULL;
821 struct usb_ohci_td * td_list;
822 struct usb_ohci_td * td_list_next = NULL;
823 struct usb_ohci_td * td_err = NULL;
824 __u32 td_hw;
827 td_list = ohci_reverse_done_list(ohci);
829 while(td_list) {
830 td_list_next = td_list->next_dl_td;
831 td = td_list;
833 if(td->ep == NULL) { /* removed ep */
834 OHCI_FREE(td_list);
835 break;
838 /* the HC halts an ED if an error occurs; put all pendings TDs of an halted ED on the
839 * done list; they are marked with an 0xf CC_error code
842 if(TD_CC_GET(td_list->hw.info) != TD_CC_NOERROR) { /* on error move all pending tds of an ed into the done list */
843 printk("******* USB BUS error %x @ep %x\n", TD_CC_GET(td_list->hw.info), td_list->ep->ep_addr.iep);
844 td_err= td_list;
845 td_hw = td_list->ep->hw.head_td & 0xfffffff0;
846 while(td_hw != 0) {
847 if(td_hw == td_list->ep->hw.tail_td) break;
848 td = bus_to_virt(td_hw);
849 td_err->next_dl_td = td;
850 td_err= td;
851 td_hw = td->hw.next_td;
853 td_list->ep->hw.head_td = td_list->ep->hw.tail_td;
854 td->next_dl_td = td_list_next;
855 td_list_next = td_list->next_dl_td;
858 /* send the reply */
859 if(td_list->handler != NULL) {
860 if(td_list->prev_td == NULL) {
861 td_list->handler((void *) ohci,
862 td_list->ep->ep_addr.iep,
864 NULL,
865 td_list->buffer_start,
866 td_list->hw.buf_end-virt_to_bus(td_list->buffer_start)+1,
867 TD_CC_GET(td_list->hw.info),
868 td_list->lw0,
869 td_list->lw1);
870 OHCI_FREE(td_list);
872 else {
873 if(td_list->prev_td->prev_td == NULL) { /* cntrl 2 Transactions dataless */
874 td_list->handler((void *) ohci,
875 td_list->ep->ep_addr.iep,
876 td_list->prev_td->hw.buf_end-virt_to_bus(td_list->prev_td->buffer_start)+1,
877 td_list->prev_td->buffer_start,
878 NULL,
880 (TD_CC_GET(td_list->prev_td->hw.info) > 0) ? TD_CC_GET(td_list->prev_td->hw.info) : TD_CC_GET(td_list->hw.info),
881 td_list->lw0,
882 td_list->lw1);
883 OHCI_FREE(td_list->prev_td);
884 OHCI_FREE(td_list);
886 else { /* cntrl 3 Transactions */
887 td_list->handler((void *) ohci,
888 td_list->ep->ep_addr.iep,
889 td_list->prev_td->prev_td->hw.buf_end-virt_to_bus(td_list->prev_td->prev_td->buffer_start)+1,
890 td_list->prev_td->prev_td->buffer_start,
891 td_list->prev_td->buffer_start,
892 td_list->prev_td->hw.buf_end-virt_to_bus(td_list->prev_td->buffer_start)+1,
893 (TD_CC_GET(td_list->prev_td->prev_td->hw.info) > 0) ? TD_CC_GET(td_list->prev_td->prev_td->hw.info)
894 : (TD_CC_GET(td_list->prev_td->hw.info) > 0) ? TD_CC_GET(td_list->prev_td->hw.info) : TD_CC_GET(td_list->hw.info),
895 td_list->lw0,
896 td_list->lw1);
897 OHCI_FREE(td_list->prev_td->prev_td);
898 OHCI_FREE(td_list->prev_td);
899 OHCI_FREE(td_list);
905 td_list = td_list_next;
907 return 0;
912 /******
913 *** HC functions
914 ************************************/
918 void reset_hc(struct ohci *ohci) {
919 int retries = 5;
920 int timeout = 30;
921 int fminterval;
923 if(readl(&ohci->regs->control) & 0x100) { /* SMM owns the HC */
924 writel(0x08, &ohci->regs->cmdstatus); /* request ownership */
925 printk("USB HC TakeOver from SMM\n");
926 do {
927 wait_ms(100);
928 if(--retries) {
929 printk("USB HC TakeOver timed out!\n");
930 break;
933 while(readl(&ohci->regs->control) & 0x100);
936 writel((1<<31), &ohci->regs->intrdisable); /* Disable HC interrupts */
937 OHCI_DEBUG(printk("USB HC reset_hc: %x ; retries: %d\n", readl(&ohci->regs->control), 5-retries);)
938 fminterval = readl(&ohci->regs->fminterval) & 0x3fff;
939 writel(1, &ohci->regs->cmdstatus); /* HC Reset */
940 while ((readl(&ohci->regs->cmdstatus) & 0x01) != 0) { /* 10us Reset */
941 if (--timeout == 0) {
942 printk("USB HC reset timed out!\n");
943 return;
945 udelay(1);
947 /* set the timing */
948 fminterval |= (((fminterval -210) * 6)/7)<<16;
949 writel(fminterval, &ohci->regs->fminterval);
950 writel(((fminterval&0x3fff)*9)/10, &ohci->regs->periodicstart);
955 * Reset and start an OHCI controller
957 void start_hc(struct ohci *ohci)
959 /* int fminterval; */
960 unsigned int mask;
961 int port_nr;
962 /* fminterval = readl(&ohci->regs->fminterval) & 0x3fff;
963 reset_hc(ohci); */
966 writel(virt_to_bus(&ohci->hc_area->hcca), &ohci->regs->hcca); /* a reset clears this */
968 /* Choose the interrupts we care about now, others later on demand */
969 mask = OHCI_INTR_MIE | OHCI_INTR_WDH;
970 /* | OHCI_INTR_SO | OHCI_INTR_UE |OHCI_INTR_RHSC |OHCI_INTR_SF|
971 OHCI_INTR_FNO */
973 if(readl(&ohci->regs->roothub.a) & 0x100) /* global power on */
974 writel( 0x10000, &ohci->regs->roothub.status); /* root hub power on */
975 else { /* port power on */
976 for(port_nr=0; port_nr < (ohci->regs->roothub.a & 0xff); port_nr++)
977 writel(0x100, &ohci->regs->roothub.portstatus[port_nr]);
979 wait_ms(50);
981 writel((0x00), &ohci->regs->control); /* USB Reset BUS */
982 wait_ms(10);
984 writel((0xB7), &ohci->regs->control); /* USB Operational */
986 OHCI_DEBUG(printk("USB HC rstart_hc_operational: %x\n", readl(&ohci->regs->control)); )
987 OHCI_DEBUG(printk("USB HC roothubstata: %x \n", readl( &(ohci->regs->roothub.a) )); )
988 OHCI_DEBUG(printk("USB HC roothubstatb: %x \n", readl( &(ohci->regs->roothub.b) )); )
989 OHCI_DEBUG(printk("USB HC roothubstatu: %x \n", readl( &(ohci->regs->roothub.status) )); )
990 OHCI_DEBUG(printk("USB HC roothubstat1: %x \n", readl( &(ohci->regs->roothub.portstatus[0]) )); )
991 OHCI_DEBUG(printk("USB HC roothubstat2: %x \n", readl( &(ohci->regs->roothub.portstatus[1]) )); )
993 /* control_wakeup = NULL; */
994 writel(mask, &ohci->regs->intrenable);
995 writel(mask, &ohci->regs->intrstatus);
997 #ifdef VROOTHUB
1000 struct usb_device * usb_dev;
1001 struct ohci_device *dev;
1002 struct ohci_device *tmp_root_hub= usb_to_ohci(ohci->bus->root_hub);
1003 usb_dev = sohci_usb_allocate(tmp_root_hub->usb);
1004 dev = usb_dev->hcpriv;
1006 dev->ohci = ohci;
1008 usb_connect(usb_dev);
1010 tmp_root_hub->usb->children[0] = usb_dev;
1012 usb_new_device(usb_dev);
1014 #endif
1023 static void ohci_interrupt(int irq, void *__ohci, struct pt_regs *r)
1025 struct ohci *ohci = __ohci;
1026 struct ohci_regs *regs = ohci->regs;
1028 int ints;
1031 if((ohci->hc_area->hcca.done_head != 0) && !(ohci->hc_area->hcca.done_head & 0x01)) {
1032 ints = OHCI_INTR_WDH;
1034 else {
1035 if((ints = (readl(&regs->intrstatus) & readl(&regs->intrenable))) == 0)
1036 return;
1039 ohci->intrstatus |= ints;
1040 OHCI_DEBUG(printk("USB HC interrupt: %x (%x) \n", ints, readl(&ohci->regs->intrstatus));)
1042 /* ints &= ~(OHCI_INTR_WDH); WH Bit will be set by done list subroutine */
1043 /* if(ints & OHCI_INTR_FNO) {
1044 writel(OHCI_INTR_FNO, &regs->intrstatus);
1045 if (waitqueue_active(&ohci_tasks)) wake_up(&ohci_tasks);
1046 } */
1048 if(ints & OHCI_INTR_WDH) {
1049 writel(OHCI_INTR_WDH, &regs->intrdisable);
1050 ohci->intrstatus &= (~OHCI_INTR_WDH);
1051 usb_ohci_done_list(ohci); /* prepare out channel list */
1052 writel(OHCI_INTR_WDH, &ohci->regs->intrstatus);
1053 writel(OHCI_INTR_WDH, &ohci->regs->intrenable);
1057 if(ints & OHCI_INTR_SF) {
1058 writel(OHCI_INTR_SF, &regs->intrdisable);
1059 writel(OHCI_INTR_SF, &ohci->regs->intrstatus);
1060 ohci->intrstatus &= (~OHCI_INTR_SF);
1061 if(ohci->ed_rm_list != NULL) {
1062 ohci_rm_eds(ohci);
1065 #ifndef VROOTHUB
1066 if(ints & OHCI_INTR_RHSC) {
1067 writel(OHCI_INTR_RHSC, &regs->intrdisable);
1068 writel(OHCI_INTR_RHSC, &ohci->regs->intrstatus);
1069 wake_up(&root_hub);
1072 #endif
1074 writel(OHCI_INTR_MIE, &regs->intrenable);
1078 #ifndef VROOTHUB
1080 * This gets called if the connect status on the root
1081 * hub (and the root hub only) changes.
1083 static void ohci_connect_change(struct ohci *ohci, unsigned int port_nr)
1085 struct usb_device *usb_dev;
1086 struct ohci_device *dev;
1087 struct ohci_device *tmp_root_hub=usb_to_ohci(ohci->bus->root_hub);
1088 OHCI_DEBUG(printk("uhci_connect_change: called for %d stat %x\n", port_nr,readl(&ohci->regs->roothub.portstatus[port_nr]) );)
1091 * Even if the status says we're connected,
1092 * the fact that the status bits changed may
1093 * that we got disconnected and then reconnected.
1095 * So start off by getting rid of any old devices..
1097 usb_disconnect(&tmp_root_hub->usb->children[port_nr]);
1099 if(!(readl(&ohci->regs->roothub.portstatus[port_nr]) & RH_PS_CCS)) {
1100 writel(RH_PS_CCS, &ohci->regs->roothub.portstatus[port_nr]);
1101 return; /* nothing connected */
1104 * Ok, we got a new connection. Allocate a device to it,
1105 * and find out what it wants to do..
1107 usb_dev = sohci_usb_allocate(tmp_root_hub->usb);
1108 dev = usb_dev->hcpriv;
1109 dev->ohci = ohci;
1110 usb_connect(dev->usb);
1111 tmp_root_hub->usb->children[port_nr] = usb_dev;
1112 wait_ms(200); /* wait for powerup */
1113 /* reset port/device */
1114 writel(RH_PS_PRS, &ohci->regs->roothub.portstatus[port_nr]); /* reset port */
1115 while(!(readl( &ohci->regs->roothub.portstatus[port_nr]) & RH_PS_PRSC)) wait_ms(10); /* reset active ? */
1116 writel(RH_PS_PES, &ohci->regs->roothub.portstatus[port_nr]); /* enable port */
1117 wait_ms(10);
1118 /* Get speed information */
1119 usb_dev->slow = (readl( &ohci->regs->roothub.portstatus[port_nr]) & RH_PS_LSDA) ? 1 : 0;
1122 * Ok, all the stuff specific to the root hub has been done.
1123 * The rest is generic for any new USB attach, regardless of
1124 * hub type.
1126 usb_new_device(usb_dev);
1128 #endif
1133 * Allocate the resources required for running an OHCI controller.
1134 * Host controller interrupts must not be running while calling this
1135 * function.
1137 * The mem_base parameter must be the usable -virtual- address of the
1138 * host controller's memory mapped I/O registers.
1140 * This is where OHCI triumphs over UHCI, because good is dumb.
1141 * Note how much simpler this function is than in uhci.c.
1143 * OHCI hardware takes care of most of the scheduling of different
1144 * transfer types with the correct prioritization for us.
1148 static struct ohci *alloc_ohci(void* mem_base)
1150 int i,j;
1151 struct ohci *ohci;
1152 struct ohci_hc_area *hc_area;
1153 struct usb_bus *bus;
1154 struct ohci_device *dev;
1155 struct usb_device *usb;
1158 * Here we allocate some dummy EDs as well as the
1159 * OHCI host controller communications area. The HCCA is just
1160 * a nice pool of memory with pointers to endpoint descriptors
1161 * for the different interrupts.
1163 * The first page of memory contains the HCCA and ohci structure
1165 hc_area = (struct ohci_hc_area *) __get_free_pages(GFP_KERNEL, 1);
1166 if (!hc_area)
1167 return NULL;
1168 memset(hc_area, 0, sizeof(*hc_area));
1169 ohci = &hc_area->ohci;
1170 ohci->irq = -1;
1171 ohci->regs = mem_base;
1173 ohci->hc_area = hc_area;
1174 /* Tell the controller where the HCCA is */
1175 writel(virt_to_bus(&hc_area->hcca), &ohci->regs->hcca);
1179 * Initialize the ED polling "tree", full tree;
1180 * dummy eds ed[i] (hc should skip them)
1181 * i == 0 is the end of the iso list;
1182 * 1 is the 1ms node,
1183 * 2,3 2ms nodes,
1184 * 4,5,6,7 4ms nodes,
1185 * 8 ... 15 8ms nodes,
1186 * 16 ... 31 16ms nodes,
1187 * 32 ... 63 32ms nodes
1188 * Sequenzes:
1189 * 32-16- 8-4-2-1-0
1190 * 33-17- 9-5-3-1-0
1191 * 34-18-10-6-2-1-0
1192 * 35-19-11-7-3-1-0
1193 * 36-20-12-4-2-1-0
1194 * 37-21-13-5-3-1-0
1195 * 38-22-14-6-2-1-0
1196 * 39-23-15-7-3-1-0
1197 * 40-24- 8-4-2-1-0
1198 * 41-25- 9-5-3-1-0
1199 * 42-26-10-6-2-1-0
1200 * : :
1201 * 63-31-15-7-3-1-0
1203 hc_area->ed[ED_ISO].info |= OHCI_ED_SKIP; /* place holder, so skip it */
1204 hc_area->ed[ED_ISO].next_ed = 0x0000; /* end of iso list */
1206 hc_area->ed[1].next_ed = virt_to_bus(&(hc_area->ed[ED_ISO]));
1207 hc_area->ed[1].info |= OHCI_ED_SKIP; /* place holder, skip it */
1209 j=1;
1210 for (i = 2; i < (NUM_INTS * 2); i++) {
1211 if (i >= NUM_INTS)
1212 hc_area->hcca.int_table[i - NUM_INTS] = virt_to_bus(&(hc_area->ed[i]));
1214 if( i == j*4) j *= 2;
1215 hc_area->ed[i].next_ed = virt_to_bus(&(hc_area->ed[j+ i%j]));
1216 hc_area->ed[i].info |= OHCI_ED_SKIP; /* place holder, skip it */
1221 * for load ballancing of the interrupt branches
1223 for (i = 0; i < NUM_INTS; i++) ohci->ohci_int_load[i] = 0;
1226 * Store the end of control and bulk list eds. So, we know where we can add
1227 * elements to these lists.
1229 ohci->ed_isotail = (struct usb_ohci_ed *) &(hc_area->ed[ED_ISO]);
1230 ohci->ed_controltail = NULL;
1231 ohci->ed_bulktail = NULL;
1234 * Tell the controller where the control and bulk lists are
1235 * The lists are empty now.
1237 writel(0, &ohci->regs->ed_controlhead);
1238 writel(0, &ohci->regs->ed_bulkhead);
1241 USB_ALLOC(bus, sizeof(*bus));
1242 if (!bus)
1243 return NULL;
1245 memset(bus, 0, sizeof(*bus));
1247 ohci->bus = bus;
1248 bus->hcpriv = (void *) ohci;
1249 bus->op = &sohci_device_operations;
1252 usb = sohci_usb_allocate(NULL);
1253 if (!usb)
1254 return NULL;
1256 dev = usb_to_ohci(usb);
1257 usb->bus = bus;
1258 bus->root_hub = usb;
1259 dev->ohci = ohci;
1261 /* Initialize the root hub */
1263 usb->maxchild = readl(&ohci->regs->roothub.a) & 0xff;
1264 usb_init_root_hub(usb);
1266 return ohci;
1271 * De-allocate all resources..
1274 static void release_ohci(struct ohci *ohci)
1276 int i;
1277 struct ohci_device *tmp_root_hub=usb_to_ohci(ohci->bus->root_hub);
1278 union ep_addr_ ep_addr;
1279 ep_addr.iep = 0;
1281 OHCI_DEBUG(printk("USB HC release ohci \n"););
1283 if (ohci->irq >= 0) {
1284 free_irq(ohci->irq, ohci);
1285 ohci->irq = -1;
1288 /* stop hc */
1289 writel(OHCI_USB_SUSPEND, &ohci->regs->control);
1291 /* deallocate all EDs and TDs */
1292 for(i=0; i < 128; i ++) {
1293 ep_addr.bep.fa = i;
1294 usb_ohci_rm_function(ohci, ep_addr.iep);
1296 ohci_rm_eds(ohci); /* remove eds */
1298 /* disconnect all devices */
1299 if(ohci->bus->root_hub)
1300 for(i = 0; i < tmp_root_hub->usb->maxchild; i++)
1301 usb_disconnect(tmp_root_hub->usb->children + i);
1303 usb_deregister_bus(ohci->bus);
1304 USB_FREE(tmp_root_hub->usb);
1305 USB_FREE(tmp_root_hub);
1306 USB_FREE(ohci->bus);
1308 /* unmap the IO address space */
1309 iounmap(ohci->regs);
1311 free_pages((unsigned int) ohci->hc_area, 1);
1315 static int ohci_roothub_thread(void * __ohci)
1317 struct ohci *ohci = (struct ohci *)__ohci;
1318 lock_kernel();
1321 * This thread doesn't need any user-level access,
1322 * so get rid of all our resources..
1324 printk("ohci_roothub_thread at %p\n", &ohci_roothub_thread);
1325 exit_mm(current);
1326 exit_files(current);
1327 exit_fs(current);
1330 strcpy(current->comm, "root-hub");
1333 start_hc(ohci);
1334 writel( 0x10000, &ohci->regs->roothub.status);
1335 wait_ms(50); /* root hub power on */
1336 usb_register_bus(ohci->bus);
1337 do {
1338 #ifdef CONFIG_APM
1339 if (apm_resume) {
1340 apm_resume = 0;
1341 start_hc(ohci);
1342 continue;
1344 #endif
1346 OHCI_DEBUG(printk("USB RH tasks: int: %x\n",ohci->intrstatus););
1347 #ifndef VROOTHUB
1348 /* if (ohci->intrstatus & OHCI_INTR_RHSC) */
1350 int port_nr;
1351 for(port_nr=0; port_nr< ohci->root_hub->usb->maxchild; port_nr++)
1352 if(readl(&ohci->regs->roothub.portstatus[port_nr]) & (RH_PS_CSC | RH_PS_PRSC)) {
1353 ohci_connect_change(ohci, port_nr);
1354 writel(0xffff0000, &ohci->regs->roothub.portstatus[port_nr]);
1356 ohci->intrstatus &= ~(OHCI_INTR_RHSC);
1357 writel(OHCI_INTR_RHSC, &ohci->regs->intrenable);
1359 #endif
1361 interruptible_sleep_on(&root_hub);
1363 } while (!signal_pending(current));
1365 #ifdef VROOTHUB
1366 ohci_del_rh_int_timer(ohci);
1367 #endif
1370 cleanup_drivers();
1371 /* reset_hc(ohci); */
1373 release_ohci(ohci);
1374 MOD_DEC_USE_COUNT;
1376 printk("ohci_control_thread exiting\n");
1378 return 0;
1382 * Increment the module usage count, start the control thread and
1383 * return success.
1385 static int found_ohci(int irq, void* mem_base)
1387 int retval;
1388 struct ohci *ohci;
1389 OHCI_DEBUG(printk("USB HC found ohci: irq= %d membase= %x \n", irq, (int)mem_base);)
1390 /* Allocate the running OHCI structures */
1391 ohci = alloc_ohci(mem_base);
1392 if (!ohci) {
1393 return -ENOMEM;
1396 reset_hc(ohci);
1398 retval = -EBUSY;
1399 if (request_irq(irq, ohci_interrupt, SA_SHIRQ, "ohci-usb", ohci) == 0) {
1400 int pid;
1402 MOD_INC_USE_COUNT;
1403 ohci->irq = irq;
1405 pid = kernel_thread(ohci_roothub_thread, ohci, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
1406 if (pid >= 0)
1407 return 0;
1410 MOD_DEC_USE_COUNT;
1411 retval = pid;
1414 release_ohci(ohci);
1415 return retval;
1418 static int start_ohci(struct pci_dev *dev)
1420 unsigned int mem_base = dev->base_address[0];
1422 /* If its OHCI, its memory */
1423 if (mem_base & PCI_BASE_ADDRESS_SPACE_IO)
1424 return -ENODEV;
1426 /* Get the memory address and map it for IO */
1427 mem_base &= PCI_BASE_ADDRESS_MEM_MASK;
1430 * FIXME ioremap_nocache isn't implemented on all CPUs (such
1431 * as the Alpha) [?] What should I use instead...
1433 * The iounmap() is done on in release_ohci.
1435 mem_base = (unsigned int) ioremap_nocache(mem_base, 4096);
1437 if (!mem_base) {
1438 printk("Error mapping OHCI memory\n");
1439 return -EFAULT;
1442 return found_ohci(dev->irq, (void *) mem_base);
1447 #ifdef CONFIG_APM
1448 static int handle_apm_event(apm_event_t event)
1450 static int down = 0;
1452 switch (event) {
1453 case APM_SYS_SUSPEND:
1454 case APM_USER_SUSPEND:
1455 if (down) {
1456 printk(KERN_DEBUG "ohci: received extra suspend event\n");
1457 break;
1459 down = 1;
1460 break;
1461 case APM_NORMAL_RESUME:
1462 case APM_CRITICAL_RESUME:
1463 if (!down) {
1464 printk(KERN_DEBUG "ohci: received bogus resume event\n");
1465 break;
1467 down = 0;
1468 if (waitqueue_active(&root_hub)) {
1469 apm_resume = 1;
1470 wake_up(&root_hub);
1472 break;
1474 return 0;
1476 #endif
1478 #define PCI_CLASS_SERIAL_USB_OHCI 0x0C0310
1479 #define PCI_CLASS_SERIAL_USB_OHCI_PG 0x10
1482 int ohci_hcd_init(void)
1484 int retval;
1485 struct pci_dev *dev = NULL;
1487 retval = -ENODEV;
1489 dev = NULL;
1490 while((dev = pci_find_class(PCI_CLASS_SERIAL_USB_OHCI, dev))) { /* OHCI */
1491 retval = start_ohci(dev);
1492 if (retval < 0) break;
1495 #ifdef CONFIG_APM
1496 apm_register_callback(&handle_apm_event);
1497 #endif
1499 return 0;
1501 return retval;
1504 #ifdef MODULE
1505 int init_module(void){
1506 return ohci_hcd_init();
1509 void cleanup_module(void)
1511 # ifdef CONFIG_APM
1512 apm_unregister_callback(&handle_apm_event);
1513 # endif
1515 #endif //MODULE