pre-2.3.4..
[davej-history.git] / drivers / usb / ohci-hcd.c
blobd02efe49cc0a5b1b6fc42c89581b274d8e0ec8c5
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 the
7 * 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 device-drivers.
11 * [ This is based on Linus' UHCI code and gregs OHCI fragments (0.03c source tree). ]
12 * [ Open Host Controller Interface driver for USB. ]
13 * [ (C) Copyright 1999 Linus Torvalds (uhci.c) ]
14 * [ (C) Copyright 1999 Gregory P. Smith <greg@electricrain.com> ]
15 * [ $Log: ohci.c,v $ ]
16 * [ Revision 1.1 1999/04/05 08:32:30 greg ]
19 * v2.1 1999/05/09 ep_addr correction, code clean up
20 * v2.0 1999/05/04
21 * virtual root hub is now an option,
22 * memory allocation based on kmalloc and kfree now, Bus error handling,
23 * INT and CTRL transfers enabled, Bulk included but disabled, ISO needs completion
25 * from Linus Torvalds (uhci.c) (APM not tested; hub, usb_device, bus and related stuff)
26 * from Greg Smith (ohci.c) (reset controller handling, hub)
28 * v1.0 1999/04/27 initial release
29 * ohci-hcd.c
32 /* #define OHCI_DBG */ /* printk some debug information */
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>
45 #include <linux/timer.h>
47 #include <asm/spinlock.h>
48 #include <asm/io.h>
49 #include <asm/irq.h>
50 #include <asm/system.h>
52 #include "usb.h"
53 #include "ohci-hcd.h"
54 #include "inits.h"
58 #ifdef CONFIG_APM
59 #include <linux/apm_bios.h>
60 static int handle_apm_event(apm_event_t event);
61 static int apm_resume = 0;
62 #endif
65 static DECLARE_WAIT_QUEUE_HEAD(bulk_wakeup);
66 static DECLARE_WAIT_QUEUE_HEAD(control_wakeup);
67 static DECLARE_WAIT_QUEUE_HEAD(root_hub);
69 static __u8 cc_to_status[16] = { /* mapping of the OHCI CC to the UHCI status codes; first guess */
70 /* Activ, Stalled, Data Buffer Err, Babble Detected : NAK recvd, CRC/Timeout, Bitstuff, reservd */
71 /* No Error */ 0x00,
72 /* CRC Error */ 0x04,
73 /* Bit Stuff */ 0x02,
74 /* Data Togg */ 0x40,
75 /* Stall */ 0x40,
76 /* DevNotResp */ 0x04,
77 /* PIDCheck */ 0x04,
78 /* UnExpPID */ 0x40,
79 /* DataOver */ 0x20,
80 /* DataUnder */ 0x20,
81 /* reservd */ 0x40,
82 /* reservd */ 0x40,
83 /* BufferOver */ 0x20,
84 /* BuffUnder */ 0x20,
85 /* Not Access */ 0x80,
86 /* Not Access */ 0x80
90 /********
91 **** Interface functions
92 ***********************************************/
94 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)
97 struct ohci * ohci = ohci_in;
98 usb_device_irq handler=(void *) lw0;
99 void *dev_id = (void *) lw1;
100 int ret;
102 OHCI_DEBUG({ int i; printk("USB HC IRQ <<<: %x: data(%d):", ep_addr, data_len);)
103 OHCI_DEBUG( for(i=0; i < data_len; i++ ) printk(" %02x", ((__u8 *) data)[i]);)
104 OHCI_DEBUG( printk(" ret_status: %x\n", status); })
106 ret = handler(cc_to_status[status & 0xf], data, dev_id);
107 if(ret == 0) return 0; /* 0 .. do not requeue */
108 if(status > 0) return -1; /* error occured do not requeue ? */
109 ohci_trans_req(ohci, ep_addr, 0, NULL, data, 8, (__OHCI_BAG) handler, (__OHCI_BAG) dev_id); /* requeue int request */
110 return 0;
113 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)
115 *(int * )lw0 = status;
116 wake_up(&control_wakeup);
118 OHCI_DEBUG( { int i; printk("USB HC CTRL<<<: %x: ctrl(%d):", ep_addr, ctrl_len);)
119 OHCI_DEBUG( for(i=0; i < 8; i++ ) printk(" %02x", ((__u8 *) ctrl)[i]);)
120 OHCI_DEBUG( printk(" data(%d):", data_len);)
121 OHCI_DEBUG( for(i=0; i < data_len; i++ ) printk(" %02x", ((__u8 *) data)[i]);)
122 OHCI_DEBUG( printk(" ret_status: %x\n", status); })
123 return 0;
126 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)
128 *(int * )lw0 = status;
129 wake_up(&bulk_wakeup);
131 OHCI_DEBUG( { int i; printk("USB HC BULK<<<: %x:", ep_addr, ctrl_len);)
132 OHCI_DEBUG( printk(" data(%d):", data_len);)
133 OHCI_DEBUG( for(i=0; i < data_len; i++ ) printk(" %02x", ((__u8 *) data)[i]);)
134 OHCI_DEBUG( printk(" ret_status: %x\n", status); })
135 return 0;
138 static int sohci_request_irq(struct usb_device *usb_dev, unsigned int pipe, usb_device_irq handler, int period, void *dev_id)
140 struct ohci * ohci = usb_dev->bus->hcpriv;
141 union ep_addr_ ep_addr;
143 ep_addr.iep = 0;
144 ep_addr.bep.ep = ((pipe >> 15) & 0x0f) /* endpoint address */
145 | (pipe & 0x80) /* direction */
146 | (1 << 5); /* type = int*/
147 ep_addr.bep.fa = ((pipe >> 8) & 0x7f); /* device address */
149 OHCI_DEBUG( printk("USB HC IRQ >>>: %x: every %d ms\n", ep_addr.iep, period);)
151 usb_ohci_add_ep(ohci, ep_addr.iep, period, 1, sohci_int_handler, 1 << ((pipe & 0x03) + 3) , (pipe >> 26) & 0x01);
153 ohci_trans_req(ohci, ep_addr.iep, 0, NULL, ((struct ohci_device *) usb_dev->hcpriv)->data, 8, (__OHCI_BAG) handler, (__OHCI_BAG) dev_id);
154 return 0;
158 static int sohci_control_msg(struct usb_device *usb_dev, unsigned int pipe, void *cmd, void *data, int len)
160 DECLARE_WAITQUEUE(wait, current);
161 struct ohci * ohci = usb_dev->bus->hcpriv;
162 int status;
163 union ep_addr_ ep_addr;
165 ep_addr.iep = 0;
166 ep_addr.bep.ep = ((pipe >> 15) & 0x0f) /* endpoint address */
167 | (pipe & 0x80) /* direction */
168 | (1 << 6); /* type = ctrl*/
169 ep_addr.bep.fa = ((pipe >> 8) & 0x7f); /* device address */
171 status = 0xf; /* CC not Accessed */
172 OHCI_DEBUG( { int i; printk("USB HC CTRL>>>: %x: ctrl(%d):", ep_addr.iep, 8);)
173 OHCI_DEBUG( for(i=0; i < 8; i++ ) printk(" %02x", ((__u8 *) cmd)[i]);)
174 OHCI_DEBUG( printk(" data(%d):", len);)
175 OHCI_DEBUG( for(i=0; i < len; i++ ) printk(" %02x", ((__u8 *) data)[i]);)
176 OHCI_DEBUG( printk("\n"); })
178 usb_ohci_add_ep(ohci, ep_addr.iep, 0, 1, sohci_ctrl_handler, 1 << ((pipe & 0x03) + 3) , (pipe >> 26) & 0x01);
180 current->state = TASK_UNINTERRUPTIBLE;
181 add_wait_queue(&control_wakeup, &wait);
183 ohci_trans_req(ohci, ep_addr.iep, 8, cmd, data, len, (__OHCI_BAG) &status, 0);
185 schedule_timeout(HZ/10);
187 remove_wait_queue(&control_wakeup, &wait);
189 OHCI_DEBUG(printk("USB HC status::: %x\n", cc_to_status[status & 0x0f]);)
191 return cc_to_status[status & 0x0f];
194 static int sohci_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, void *data, int len)
196 DECLARE_WAITQUEUE(wait, current);
197 struct ohci * ohci = usb_dev->bus->hcpriv;
198 int status;
199 union ep_addr_ ep_addr;
201 ep_addr.iep = 0;
202 ep_addr.bep.ep = ((pipe >> 15) & 0x0f) /* endpoint address */
203 | (pipe & 0x80) /* direction */
204 | (11 << 5); /* type = bulk*/
205 ep_addr.bep.fa = ((pipe >> 8) & 0x7f); /* device address */
207 status = 0xf; /* CC not Accessed */
208 OHCI_DEBUG( { int i; printk("USB HC BULK>>>: %x:", ep_addr.iep);)
209 OHCI_DEBUG( printk(" data(%d):", len);)
210 OHCI_DEBUG( for(i=0; i < len; i++ ) printk(" %02x", ((__u8 *) data)[i]);)
211 OHCI_DEBUG( printk("\n"); })
213 usb_ohci_add_ep(ohci, ep_addr.iep, 0, 1, sohci_bulk_handler, 1 << ((pipe & 0x03) + 3) , (pipe >> 26) & 0x01);
215 current->state = TASK_UNINTERRUPTIBLE;
216 add_wait_queue(&bulk_wakeup, &wait);
218 ohci_trans_req(ohci, ep_addr.iep, 0, NULL, data, len, (__OHCI_BAG) &status, 0);
220 schedule_timeout(HZ/10);
222 remove_wait_queue(&bulk_wakeup, &wait);
224 OHCI_DEBUG(printk("USB HC status::: %x\n", cc_to_status[status & 0x0f]);)
226 return cc_to_status[status & 0x0f];
229 static int sohci_usb_deallocate(struct usb_device *usb_dev) {
230 struct ohci_device *dev = usb_to_ohci(usb_dev);
231 union ep_addr_ ep_addr;
233 ep_addr.iep = 0;
235 OHCI_DEBUG(printk("USB HC dealloc %x\n", usb_dev->devnum);)
237 /* wait_ms(20); */
239 if(usb_dev->devnum >=0) {
240 ep_addr.bep.fa = usb_dev->devnum;
241 usb_ohci_rm_function(((struct ohci_device *)usb_dev->hcpriv)->ohci, ep_addr.iep);
244 USB_FREE(dev);
245 USB_FREE(usb_dev);
247 return 0;
250 static struct usb_device *sohci_usb_allocate(struct usb_device *parent) {
252 struct usb_device *usb_dev;
253 struct ohci_device *dev;
256 USB_ALLOC(usb_dev, sizeof(*usb_dev));
257 if (!usb_dev)
258 return NULL;
260 memset(usb_dev, 0, sizeof(*usb_dev));
262 USB_ALLOC(dev, sizeof(*dev));
263 if (!dev) {
264 USB_FREE(usb_dev);
265 return NULL;
268 /* Initialize "dev" */
269 memset(dev, 0, sizeof(*dev));
271 usb_dev->hcpriv = dev;
272 dev->usb = usb_dev;
274 usb_dev->parent = parent;
276 if (parent) {
277 usb_dev->bus = parent->bus;
278 dev->ohci = usb_to_ohci(parent)->ohci;
280 return usb_dev;
284 struct usb_operations sohci_device_operations = {
285 sohci_usb_allocate,
286 sohci_usb_deallocate,
287 sohci_control_msg,
288 sohci_bulk_msg,
289 sohci_request_irq,
293 /******
294 *** ED handling functions
295 ************************************/
300 * search for the right place to insert an interrupt ed into the int tree
301 * do some load ballancing
302 * */
304 static int usb_ohci_int_ballance(struct ohci * ohci, int interval, int load) {
306 int i,j;
308 j = 0; /* search for the least loaded interrupt endpoint branch of all 32 branches */
309 for(i=0; i< 32; i++) if(ohci->ohci_int_load[j] > ohci->ohci_int_load[i]) j=i;
311 if(interval < 1) interval = 1;
312 if(interval > 32) interval = 32;
313 for(i= 0; ((interval >> i) > 1 ); interval &= (0xfffe << i++ )); /* interval = 2^int(ld(interval)) */
316 for(i=j%interval; i< 32; i+=interval) ohci->ohci_int_load[i] += load;
317 j = interval + (j % interval);
319 OHCI_DEBUG(printk("USB HC new int ed on pos : %x \n",j);)
321 return j;
324 /* get the ed from the endpoint / device adress */
326 struct usb_ohci_ed * ohci_find_ep(struct ohci *ohci, unsigned int ep_addr_in) {
328 union ep_addr_ ep_addr;
329 struct usb_ohci_ed *tmp;
330 unsigned int mask;
332 mask = 0;
333 ep_addr.iep = ep_addr_in;
335 #ifdef VROOTHUB
336 if(ep_addr.bep.fa == ohci->root_hub_funct_addr) {
337 if((ep_addr.bep.ep & 0x0f) == 0)
338 return &ohci->ed_rh_ep0; /* root hub ep0 */
339 else
340 return &ohci->ed_rh_epi; /* root hub int ep */
342 #endif
344 tmp = ohci->ed_func_ep0[ep_addr.bep.fa];
345 mask = ((((ep_addr.bep.ep >> 5) & 0x03)==2)?0x7f:0xff);
346 ep_addr.bep.ep &= mask; /* mask out direction of ctrl ep */
348 while (tmp != NULL) {
349 if (tmp->ep_addr.iep == ep_addr.iep)
350 return tmp;
351 tmp = tmp->ed_list;
353 return NULL;
356 spinlock_t usb_ed_lock = SPIN_LOCK_UNLOCKED;
357 /* add a new endpoint ep_addr */
358 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) {
360 struct usb_ohci_ed * ed;
361 struct usb_ohci_td * td;
362 union ep_addr_ ep_addr;
365 int int_junk;
367 struct usb_ohci_ed *tmp;
369 ep_addr.iep = ep_addr_in ;
370 ep_addr.bep.ep &= ((((ep_addr.bep.ep >> 5) & 0x03)==2)?0x7f:0xff); /* mask out direction of ctrl ep */
372 spin_lock(&usb_ed_lock);
374 tmp = ohci_find_ep(ohci, ep_addr.iep);
375 if (tmp != NULL) {
377 #ifdef VROOTHUB
378 if(ep_addr.bep.fa == ohci->root_hub_funct_addr) {
379 if((ep_addr.bep.ep & 0x0f) != 0) { /* root hub int ep */
380 ohci->ed_rh_epi.handler = handler;
381 ohci_init_rh_int_timer(ohci, interval);
383 else { /* root hub ep0 */
384 ohci->ed_rh_ep0.handler = handler;
388 else
389 #endif
392 tmp->hw.info = ep_addr.bep.fa | ((ep_addr.bep.ep & 0xf) <<7)
394 | (((ep_addr.bep.ep & 0x60) == 0)? 0x8000 : 0)
395 | (speed << 13)
396 | ep_size <<16;
398 tmp->handler = handler;
400 spin_unlock(&usb_ed_lock);
401 return tmp; /* ed already in use */
405 OHCI_ALLOC(td, sizeof(td)); /* dummy td; end of td list for ed */
406 OHCI_ALLOC(ed, sizeof(ed));
407 td->prev_td = NULL;
409 ed->hw.tail_td = virt_to_bus(&td->hw);
410 ed->hw.head_td = ed->hw.tail_td;
411 ed->hw.info = ep_addr.bep.fa | ((ep_addr.bep.ep & 0xf) <<7)
412 /* | ((ep_addr.bep.port & 0x80)? 0x1000 : 0x0800 ) */
413 | (((ep_addr.bep.ep & 0x60) == 0)? 0x8000 : 0)
414 | (speed << 13)
415 | ep_size <<16;
417 ed->handler = handler;
419 switch((ep_addr.bep.ep >> 5) & 0x03) {
420 case CTRL:
421 ed->hw.next_ed = 0;
422 if(ohci->ed_controltail == NULL) {
423 writel(virt_to_bus(&ed->hw), &ohci->regs->ed_controlhead);
425 else {
426 ohci->ed_controltail->hw.next_ed = virt_to_bus(&ed->hw);
428 ed->ed_prev = ohci->ed_controltail;
429 ohci->ed_controltail = ed;
430 break;
431 case BULK:
432 ed->hw.next_ed = 0;
433 if(ohci->ed_bulktail == NULL) {
434 writel(virt_to_bus(&ed->hw), &ohci->regs->ed_bulkhead);
436 else {
437 ohci->ed_bulktail->hw.next_ed = virt_to_bus(&ed->hw);
439 ed->ed_prev = ohci->ed_bulktail;
440 ohci->ed_bulktail = ed;
441 break;
442 case INT:
443 int_junk = usb_ohci_int_ballance(ohci, interval, load);
444 ed->hw.next_ed = ohci->hc_area->ed[int_junk].next_ed;
445 ohci->hc_area->ed[int_junk].next_ed = virt_to_bus(&ed->hw);
446 ed->ed_prev = (struct usb_ohci_ed *) &ohci->hc_area->ed[int_junk];
447 break;
448 case ISO:
449 ed->hw.next_ed = 0;
450 ohci->ed_isotail->hw.next_ed = virt_to_bus(&ed->hw);
451 ed->ed_prev = ohci->ed_isotail;
452 ohci->ed_isotail = ed;
453 break;
455 ed->ep_addr = ep_addr;
457 /* Add it to the "hash"-table of known endpoint descriptors */
459 ed->ed_list = ohci->ed_func_ep0[ed->ep_addr.bep.fa];
460 ohci->ed_func_ep0[ed->ep_addr.bep.fa] = ed;
462 spin_unlock(&usb_ed_lock);
464 OHCI_DEBUG(printk("USB HC new ed %x: %x :", ep_addr.iep, (unsigned int ) ed); )
465 OHCI_DEBUG({ int i; for( i= 0; i<8 ;i++) printk(" %4x", ((unsigned int *) ed)[i]) ; printk("\n"); }; )
466 return 0;
469 /*****
470 * Request the removal of an endpoint
472 * put the ep on the rm_list and request a stop of the bulk or ctrl list
473 * real removal is done at the next start of frame hardware interrupt
475 int usb_ohci_rm_ep(struct ohci * ohci, struct usb_ohci_ed *ed)
477 unsigned int flags;
478 struct usb_ohci_ed *tmp;
480 OHCI_DEBUG(printk("USB HC remove ed %x: %x :\n", ed->ep_addr.iep, (unsigned int ) ed); )
482 spin_lock_irqsave(&usb_ed_lock, flags);
484 tmp = ohci->ed_func_ep0[ed->ep_addr.bep.fa];
485 if (tmp == NULL) {
486 spin_unlock_irqrestore(&usb_ed_lock, flags);
487 return 0;
490 if(tmp == ed) {
491 ohci->ed_func_ep0[ed->ep_addr.bep.fa] = ed->ed_list;
493 else {
494 while (tmp->ed_list != ed) {
495 if (tmp->ed_list == NULL) {
496 spin_unlock_irqrestore(&usb_ed_lock, flags);
497 return 0;
499 tmp = tmp->ed_list;
501 tmp->ed_list = ed->ed_list;
503 ed->ed_list = ohci->ed_rm_list;
504 ohci->ed_rm_list = ed;
505 ed->hw.info |= OHCI_ED_SKIP;
507 switch((ed->ep_addr.bep.ep >> 5) & 0x03) {
508 case CTRL:
509 writel_mask(~(0x01<<4), &ohci->regs->control); /* stop CTRL list */
510 break;
511 case BULK:
512 writel_mask(~(0x01<<5), &ohci->regs->control); /* stop BULK list */
513 break;
517 writel( OHCI_INTR_SF, &ohci->regs->intrenable); /* enable sof interrupt */
519 spin_unlock_irqrestore(&usb_ed_lock, flags);
521 return 1;
524 /* we have requested to stop the bulk or CTRL list,
525 * now we can remove the eds on the rm_list */
527 static int ohci_rm_eds(struct ohci * ohci) {
529 unsigned int flags;
530 struct usb_ohci_ed *ed;
531 struct usb_ohci_ed *ed_tmp;
532 struct usb_ohci_td *td;
533 __u32 td_hw_tmp;
534 __u32 td_hw;
536 spin_lock_irqsave(&usb_ed_lock, flags);
538 ed = ohci->ed_rm_list;
540 while (ed != NULL) {
542 switch((ed->ep_addr.bep.ep >> 5) & 0x03) {
543 case CTRL:
544 if(ed->ed_prev == NULL) {
545 writel(ed->hw.next_ed, &ohci->regs->ed_controlhead);
547 else {
548 ed->ed_prev->hw.next_ed = ed->hw.next_ed;
550 if(ohci->ed_controltail == ed) {
551 ohci->ed_controltail = ed->ed_prev;
553 break;
554 case BULK:
555 if(ed->ed_prev == NULL) {
556 writel(ed->hw.next_ed, &ohci->regs->ed_bulkhead);
558 else {
559 ed->ed_prev->hw.next_ed = ed->hw.next_ed;
561 if(ohci->ed_bulktail == ed) {
562 ohci->ed_bulktail = ed->ed_prev;
564 break;
565 case INT:
566 ed->ed_prev->hw.next_ed = ed->hw.next_ed;
567 break;
568 case ISO:
569 ed->ed_prev->hw.next_ed = ed->hw.next_ed;
570 if(ohci->ed_isotail == ed) {
571 ohci->ed_isotail = ed->ed_prev;
573 break;
576 if(ed->hw.next_ed != 0) ((struct usb_ohci_ed *) bus_to_virt(ed->hw.next_ed))->ed_prev = ed->ed_prev;
579 /* tds directly connected to ed */
581 td_hw = ed->hw.head_td & 0xfffffff0;
582 while(td_hw != 0) {
583 td = bus_to_virt(td_hw);
584 td_hw_tmp = td_hw;
585 td_hw = td->hw.next_td;
586 OHCI_FREE(td); /* free pending tds */
587 if(td_hw_tmp == ed->hw.tail_td) break;
591 /* mark TDs on the hc done list (if there are any) */
592 td_hw = readl(&ohci->regs->donehead) & 0xfffffff0;
593 while(td_hw != 0) {
594 td = bus_to_virt(td_hw);
595 td_hw = td->hw.next_td;
596 if(td->ep == ed) td->ep = 0;
599 /* mark TDs on the hcca done list (if there are any) */
600 td_hw = ohci->hc_area->hcca.done_head & 0xfffffff0 ;
602 while(td_hw != 0) {
603 td = bus_to_virt(td_hw);
604 td_hw = td->hw.next_td;
605 if(td->ep == ed) td->ep = 0;
608 ed_tmp = ed;
609 ed = ed->ed_list;
610 OHCI_FREE(ed_tmp); /* free ed */
612 writel(0, &ohci->regs->ed_controlcurrent); /* reset CTRL list */
613 writel(0, &ohci->regs->ed_bulkcurrent); /* reset BULK list */
614 writel_set((0x03<<4), &ohci->regs->control); /* start CTRL u. (BULK list) */
616 spin_unlock_irqrestore(&usb_ed_lock, flags);
618 ohci->ed_rm_list = NULL;
619 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));)
622 return 0;
625 /* remove all endpoints of a function (device) */
626 int usb_ohci_rm_function( struct ohci * ohci, unsigned int ep_addr_in)
628 struct usb_ohci_ed *ed;
629 struct usb_ohci_ed *tmp;
630 union ep_addr_ ep_addr;
634 ep_addr.iep = ep_addr_in;
636 for(ed = ohci->ed_func_ep0[ep_addr.bep.fa]; ed != NULL;) {
637 tmp = ed;
638 ed = ed->ed_list;
639 usb_ohci_rm_ep(ohci, tmp);
644 return 1;
651 /******
652 *** TD handling functions
653 ************************************/
656 #define FILL_TD(TD_PT, HANDLER, INFO, DATA, LEN, LW0, LW1) \
657 td_pt = (TD_PT); \
658 td_pt1 = (struct usb_ohci_td *) bus_to_virt(usb_ep->hw.tail_td); \
659 td_pt1->ep = usb_ep; \
660 td_pt1->handler = (HANDLER); \
661 td_pt1->buffer_start = (DATA); \
662 td_pt1->lw0 = (LW0); \
663 td_pt1->lw1 = (LW1); \
664 td_pt1->hw.info = (INFO); \
665 td_pt1->hw.cur_buf = virt_to_bus(DATA); \
666 td_pt1->hw.buf_end = td_pt1->hw.cur_buf + (LEN) - 1; \
667 td_pt1->hw.next_td = virt_to_bus(td_pt); \
668 usb_ep->hw.tail_td = virt_to_bus(td_pt); \
669 td_pt->prev_td = td_pt1; \
670 td_pt->hw.next_td = 0
672 spinlock_t usb_req_lock = SPIN_LOCK_UNLOCKED;
674 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) {
676 int ed_type;
677 unsigned int flags;
678 struct usb_ohci_td *td_pt;
679 struct usb_ohci_td *td_pt1;
680 struct usb_ohci_td *td_pt_a1, *td_pt_a2, *td_pt_a3;
681 struct usb_ohci_ed *usb_ep;
682 f_handler handler;
685 td_pt_a1 =NULL;
686 td_pt_a2 =NULL;
687 td_pt_a3 =NULL;
689 usb_ep = ohci_find_ep(ohci, ep_addr);
690 if(usb_ep == NULL ) return -1; /* not known ep */
692 handler = usb_ep->handler;
694 #ifdef VROOTHUB
695 if(usb_ep == &ohci->ed_rh_ep0) { /* root hub ep 0 control endpoint */
696 root_hub_control_msg(ohci, 8, ctrl, data, data_len, lw0, lw1, handler);
697 return 0;
700 if(usb_ep == &ohci->ed_rh_epi) { /* root hub interrupt endpoint */
702 root_hub_int_req(ohci, 8, ctrl, data, data_len, lw0, lw1, handler);
703 return 0;
705 #endif
706 /* struct usb_ohci_ed * usb_ep = usb_ohci_add_ep(pipe, ohci, interval, 1); */
708 ed_type = ((((union ep_addr_)ep_addr).bep.ep >> 5) & 0x07);
710 switch(ed_type) {
711 case BULK_IN:
712 case BULK_OUT:
713 case INT_IN:
714 case INT_OUT:
715 OHCI_ALLOC(td_pt_a1, sizeof(td_pt_a1));
716 break;
718 case CTRL_IN:
719 case CTRL_OUT:
720 OHCI_ALLOC(td_pt_a1, sizeof(td_pt_a1));
721 OHCI_ALLOC(td_pt_a3, sizeof(td_pt_a3));
722 if(data_len > 0) {
723 OHCI_ALLOC(td_pt_a2, sizeof(td_pt_a2));
725 break;
727 case ISO_IN:
728 case ISO_OUT:
732 spin_lock_irqsave(&usb_req_lock, flags);
734 switch(ed_type) {
735 case BULK_IN:
736 FILL_TD( td_pt_a1, handler, TD_CC | TD_R | TD_DP_IN | TD_T_TOGGLE, data, data_len, lw0, lw1 );
737 writel( OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
738 break;
740 case BULK_OUT:
741 FILL_TD( td_pt_a1, handler, TD_CC | TD_DP_OUT | TD_T_TOGGLE, data, data_len, lw0, lw1 );
742 writel( OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
743 break;
745 case INT_IN:
746 FILL_TD( td_pt_a1, handler, TD_CC | TD_R | TD_DP_IN | TD_T_TOGGLE, data, data_len, lw0, lw1 );
747 break;
749 case INT_OUT:
750 FILL_TD( td_pt_a1, handler, TD_CC | TD_DP_OUT | TD_T_TOGGLE, data, data_len, lw0, lw1 );
751 break;
753 case CTRL_IN:
754 FILL_TD( td_pt_a1, NULL, TD_CC | TD_DP_SETUP | TD_T_DATA0, ctrl, ctrl_len, 0, 0 );
755 if(data_len > 0) {
756 FILL_TD( td_pt_a2, NULL, TD_CC | TD_R | TD_DP_IN | TD_T_DATA1, data, data_len, 0, 0 );
758 FILL_TD( td_pt_a3, handler, TD_CC | TD_DP_OUT | TD_T_DATA1, NULL, 0, lw0, lw1 );
759 writel( OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
760 break;
762 case CTRL_OUT:
763 FILL_TD( td_pt_a1, NULL, TD_CC | TD_DP_SETUP | TD_T_DATA0, ctrl, ctrl_len, 0, 0 );
764 if(data_len > 0) {
765 FILL_TD( td_pt_a2, NULL, TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1, data, data_len, 0, 0 );
767 FILL_TD( td_pt_a3, handler, TD_CC | TD_DP_IN | TD_T_DATA1, NULL, 0, lw0, lw1 );
768 writel( OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
769 break;
771 case ISO_IN:
772 case ISO_OUT:
773 break;
779 td_pt1 = (struct usb_ohci_td *) bus_to_virt(usb_ep->hw.tail_td);
782 if(td_pt_a3 != NULL) td_pt_a3->prev_td = NULL;
783 else if (td_pt_a2 != NULL) td_pt_a2->prev_td = NULL;
784 else if (td_pt_a1 != NULL) td_pt_a1->prev_td = NULL;
786 spin_unlock_irqrestore(&usb_req_lock, flags);
787 return 0;
791 /******
792 *** Done List handling functions
793 ************************************/
795 /* replies to the request have to be on a FIFO basis so
796 * we reverse the reversed done-list */
798 static struct usb_ohci_td * ohci_reverse_done_list(struct ohci * ohci) {
800 __u32 td_list_hc;
801 struct usb_ohci_td * td_list = NULL;
802 struct usb_ohci_td * td_rev = NULL;
804 td_list_hc = ohci->hc_area->hcca.done_head & 0xfffffff0;
805 ohci->hc_area->hcca.done_head = 0;
807 while(td_list_hc) {
809 td_list = (struct usb_ohci_td *) bus_to_virt(td_list_hc);
810 td_list->next_dl_td = td_rev;
812 td_rev = td_list;
813 td_list_hc = td_list->hw.next_td & 0xfffffff0;
815 return td_list;
818 /* all done requests are replied here */
819 static int usb_ohci_done_list(struct ohci * ohci) {
821 struct usb_ohci_td * td = NULL;
822 struct usb_ohci_td * td_list;
823 struct usb_ohci_td * td_list_next = NULL;
824 struct usb_ohci_td * td_err = NULL;
825 __u32 td_hw;
828 td_list = ohci_reverse_done_list(ohci);
830 while(td_list) {
831 td_list_next = td_list->next_dl_td;
832 td = td_list;
834 if(td->ep == NULL) { /* removed ep */
835 OHCI_FREE(td_list);
836 break;
839 /* the HC halts an ED if an error occurs; put all pendings TDs of an halted ED on the
840 * done list; they are marked with an 0xf CC_error code
843 if(TD_CC_GET(td_list->hw.info) != TD_CC_NOERROR) { /* on error move all pending tds of an ed into the done list */
844 printk("******* USB BUS error %x @ep %x\n", TD_CC_GET(td_list->hw.info), td_list->ep->ep_addr.iep);
845 td_err= td_list;
846 td_hw = td_list->ep->hw.head_td & 0xfffffff0;
847 while(td_hw != 0) {
848 if(td_hw == td_list->ep->hw.tail_td) break;
849 td = bus_to_virt(td_hw);
850 td_err->next_dl_td = td;
851 td_err= td;
852 td_hw = td->hw.next_td;
854 td_list->ep->hw.head_td = td_list->ep->hw.tail_td;
855 td->next_dl_td = td_list_next;
856 td_list_next = td_list->next_dl_td;
859 /* send the reply */
860 if(td_list->handler != NULL) {
861 if(td_list->prev_td == NULL) {
862 td_list->handler((void *) ohci,
863 td_list->ep->ep_addr.iep,
865 NULL,
866 td_list->buffer_start,
867 td_list->hw.buf_end-virt_to_bus(td_list->buffer_start)+1,
868 TD_CC_GET(td_list->hw.info),
869 td_list->lw0,
870 td_list->lw1);
871 OHCI_FREE(td_list);
873 else {
874 if(td_list->prev_td->prev_td == NULL) { /* cntrl 2 Transactions dataless */
875 td_list->handler((void *) ohci,
876 td_list->ep->ep_addr.iep,
877 td_list->prev_td->hw.buf_end-virt_to_bus(td_list->prev_td->buffer_start)+1,
878 td_list->prev_td->buffer_start,
879 NULL,
881 (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),
882 td_list->lw0,
883 td_list->lw1);
884 OHCI_FREE(td_list->prev_td);
885 OHCI_FREE(td_list);
887 else { /* cntrl 3 Transactions */
888 td_list->handler((void *) ohci,
889 td_list->ep->ep_addr.iep,
890 td_list->prev_td->prev_td->hw.buf_end-virt_to_bus(td_list->prev_td->prev_td->buffer_start)+1,
891 td_list->prev_td->prev_td->buffer_start,
892 td_list->prev_td->buffer_start,
893 td_list->prev_td->hw.buf_end-virt_to_bus(td_list->prev_td->buffer_start)+1,
894 (TD_CC_GET(td_list->prev_td->prev_td->hw.info) > 0) ? TD_CC_GET(td_list->prev_td->prev_td->hw.info)
895 : (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),
896 td_list->lw0,
897 td_list->lw1);
898 OHCI_FREE(td_list->prev_td->prev_td);
899 OHCI_FREE(td_list->prev_td);
900 OHCI_FREE(td_list);
906 td_list = td_list_next;
908 return 0;
913 /******
914 *** HC functions
915 ************************************/
919 void reset_hc(struct ohci *ohci) {
920 int retries = 5;
921 int timeout = 30;
922 int fminterval;
924 if(readl(&ohci->regs->control) & 0x100) { /* SMM owns the HC */
925 writel(0x08, &ohci->regs->cmdstatus); /* request ownership */
926 printk("USB HC TakeOver from SMM\n");
927 do {
928 wait_ms(100);
929 if(--retries) {
930 printk("USB HC TakeOver timed out!\n");
931 break;
934 while(readl(&ohci->regs->control) & 0x100);
937 writel((1<<31), &ohci->regs->intrdisable); /* Disable HC interrupts */
938 OHCI_DEBUG(printk("USB HC reset_hc: %x ; retries: %d\n", readl(&ohci->regs->control), 5-retries);)
939 fminterval = readl(&ohci->regs->fminterval) & 0x3fff;
940 writel(1, &ohci->regs->cmdstatus); /* HC Reset */
941 while ((readl(&ohci->regs->cmdstatus) & 0x01) != 0) { /* 10us Reset */
942 if (--timeout == 0) {
943 printk("USB HC reset timed out!\n");
944 return;
946 udelay(1);
948 /* set the timing */
949 fminterval |= (((fminterval -210) * 6)/7)<<16;
950 writel(fminterval, &ohci->regs->fminterval);
951 writel(((fminterval&0x3fff)*9)/10, &ohci->regs->periodicstart);
956 * Reset and start an OHCI controller
958 void start_hc(struct ohci *ohci)
960 /* int fminterval; */
961 unsigned int mask;
962 int port_nr;
963 /* fminterval = readl(&ohci->regs->fminterval) & 0x3fff;
964 reset_hc(ohci); */
967 writel(virt_to_bus(&ohci->hc_area->hcca), &ohci->regs->hcca); /* a reset clears this */
969 /* Choose the interrupts we care about now, others later on demand */
970 mask = OHCI_INTR_MIE | OHCI_INTR_WDH;
971 /* | OHCI_INTR_SO | OHCI_INTR_UE |OHCI_INTR_RHSC |OHCI_INTR_SF|
972 OHCI_INTR_FNO */
974 if(readl(&ohci->regs->roothub.a) & 0x100) /* global power on */
975 writel( 0x10000, &ohci->regs->roothub.status); /* root hub power on */
976 else { /* port power on */
977 for(port_nr=0; port_nr < (ohci->regs->roothub.a & 0xff); port_nr++)
978 writel(0x100, &ohci->regs->roothub.portstatus[port_nr]);
980 wait_ms(50);
982 writel((0x00), &ohci->regs->control); /* USB Reset BUS */
983 wait_ms(10);
985 writel((0xB7), &ohci->regs->control); /* USB Operational */
987 OHCI_DEBUG(printk("USB HC rstart_hc_operational: %x\n", readl(&ohci->regs->control)); )
988 OHCI_DEBUG(printk("USB HC roothubstata: %x \n", readl( &(ohci->regs->roothub.a) )); )
989 OHCI_DEBUG(printk("USB HC roothubstatb: %x \n", readl( &(ohci->regs->roothub.b) )); )
990 OHCI_DEBUG(printk("USB HC roothubstatu: %x \n", readl( &(ohci->regs->roothub.status) )); )
991 OHCI_DEBUG(printk("USB HC roothubstat1: %x \n", readl( &(ohci->regs->roothub.portstatus[0]) )); )
992 OHCI_DEBUG(printk("USB HC roothubstat2: %x \n", readl( &(ohci->regs->roothub.portstatus[1]) )); )
994 /* control_wakeup = NULL; */
995 writel(mask, &ohci->regs->intrenable);
996 writel(mask, &ohci->regs->intrstatus);
998 #ifdef VROOTHUB
1001 struct usb_device * usb_dev;
1002 struct ohci_device *dev;
1004 usb_dev = sohci_usb_allocate(ohci->root_hub->usb);
1005 dev = usb_dev->hcpriv;
1007 dev->ohci = ohci;
1009 usb_connect(usb_dev);
1011 ohci->root_hub->usb->children[0] = usb_dev;
1013 usb_new_device(usb_dev);
1015 #endif
1024 static void ohci_interrupt(int irq, void *__ohci, struct pt_regs *r)
1026 struct ohci *ohci = __ohci;
1027 struct ohci_regs *regs = ohci->regs;
1029 int ints;
1032 if((ohci->hc_area->hcca.done_head != 0) && !(ohci->hc_area->hcca.done_head & 0x01)) {
1033 ints = OHCI_INTR_WDH;
1035 else {
1036 if((ints = (readl(&regs->intrstatus) & readl(&regs->intrenable))) == 0)
1037 return;
1040 ohci->intrstatus |= ints;
1041 OHCI_DEBUG(printk("USB HC interrupt: %x (%x) \n", ints, readl(&ohci->regs->intrstatus));)
1043 /* ints &= ~(OHCI_INTR_WDH); WH Bit will be set by done list subroutine */
1044 /* if(ints & OHCI_INTR_FNO) {
1045 writel(OHCI_INTR_FNO, &regs->intrstatus);
1046 if (waitqueue_active(&ohci_tasks)) wake_up(&ohci_tasks);
1047 } */
1049 if(ints & OHCI_INTR_WDH) {
1050 writel(OHCI_INTR_WDH, &regs->intrdisable);
1051 ohci->intrstatus &= (~OHCI_INTR_WDH);
1052 usb_ohci_done_list(ohci); /* prepare out channel list */
1053 writel(OHCI_INTR_WDH, &ohci->regs->intrstatus);
1054 writel(OHCI_INTR_WDH, &ohci->regs->intrenable);
1058 if(ints & OHCI_INTR_SF) {
1059 writel(OHCI_INTR_SF, &regs->intrdisable);
1060 writel(OHCI_INTR_SF, &ohci->regs->intrstatus);
1061 ohci->intrstatus &= (~OHCI_INTR_SF);
1062 if(ohci->ed_rm_list != NULL) {
1063 ohci_rm_eds(ohci);
1066 #ifndef VROOTHUB
1067 if(ints & OHCI_INTR_RHSC) {
1068 writel(OHCI_INTR_RHSC, &regs->intrdisable);
1069 writel(OHCI_INTR_RHSC, &ohci->regs->intrstatus);
1070 wake_up(&root_hub);
1073 #endif
1075 writel(OHCI_INTR_MIE, &regs->intrenable);
1079 #ifndef VROOTHUB
1081 * This gets called if the connect status on the root
1082 * hub (and the root hub only) changes.
1084 static void ohci_connect_change(struct ohci *ohci, unsigned int port_nr)
1086 struct usb_device *usb_dev;
1087 struct ohci_device *dev;
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(&ohci->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(ohci->root_hub->usb);
1108 dev = usb_dev->hcpriv;
1109 dev->ohci = ohci;
1110 usb_connect(dev->usb);
1111 ohci->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 = ohci->root_hub = usb_to_ohci(usb);
1258 usb->bus = bus;
1259 /* bus->root_hub = ohci_to_usb(ohci->root_hub); */
1260 dev->ohci = ohci;
1262 /* Initialize the root hub */
1264 usb->maxchild = readl(&ohci->regs->roothub.a) & 0xff;
1265 usb_init_root_hub(usb);
1267 return ohci;
1272 * De-allocate all resources..
1275 static void release_ohci(struct ohci *ohci)
1277 int i;
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->root_hub)
1300 for(i = 0; i < ohci->root_hub->usb->maxchild; i++)
1301 usb_disconnect(ohci->root_hub->usb->children + i);
1303 USB_FREE(ohci->root_hub->usb);
1304 USB_FREE(ohci->root_hub);
1305 USB_FREE(ohci->bus);
1307 /* unmap the IO address space */
1308 iounmap(ohci->regs);
1311 free_pages((unsigned int) ohci->hc_area, 1);
1316 void cleanup_drivers(void);
1318 static int ohci_roothub_thread(void * __ohci)
1320 struct ohci *ohci = (struct ohci *)__ohci;
1321 lock_kernel();
1324 * This thread doesn't need any user-level access,
1325 * so get rid of all our resources..
1327 printk("ohci_roothub_thread at %p\n", &ohci_roothub_thread);
1328 exit_mm(current);
1329 exit_files(current);
1330 exit_fs(current);
1333 strcpy(current->comm, "root-hub");
1336 start_hc(ohci);
1337 writel( 0x10000, &ohci->regs->roothub.status);
1338 wait_ms(50); /* root hub power on */
1339 do {
1340 #ifdef CONFIG_APM
1341 if (apm_resume) {
1342 apm_resume = 0;
1343 start_hc(ohci);
1344 continue;
1346 #endif
1348 OHCI_DEBUG(printk("USB RH tasks: int: %x\n", ohci->intrstatus); )
1349 #ifndef VROOTHUB
1350 /* if (ohci->intrstatus & OHCI_INTR_RHSC) */
1352 int port_nr;
1353 for(port_nr=0; port_nr< ohci->root_hub->usb->maxchild; port_nr++)
1354 if(readl(&ohci->regs->roothub.portstatus[port_nr]) & (RH_PS_CSC | RH_PS_PRSC)) {
1355 ohci_connect_change(ohci, port_nr);
1356 writel(0xffff0000, &ohci->regs->roothub.portstatus[port_nr]);
1358 ohci->intrstatus &= ~(OHCI_INTR_RHSC);
1359 writel(OHCI_INTR_RHSC, &ohci->regs->intrenable);
1361 #endif
1363 interruptible_sleep_on(&root_hub);
1365 } while (!signal_pending(current));
1367 #ifdef VROOTHUB
1368 ohci_del_rh_int_timer(ohci);
1369 #endif
1372 cleanup_drivers();
1373 /* reset_hc(ohci); */
1375 release_ohci(ohci);
1376 MOD_DEC_USE_COUNT;
1378 printk("ohci_control_thread exiting\n");
1380 return 0;
1387 * Increment the module usage count, start the control thread and
1388 * return success.
1390 static int found_ohci(int irq, void* mem_base)
1392 int retval;
1393 struct ohci *ohci;
1394 OHCI_DEBUG(printk("USB HC found ohci: irq= %d membase= %x \n", irq, (int)mem_base);)
1395 /* Allocate the running OHCI structures */
1396 ohci = alloc_ohci(mem_base);
1397 if (!ohci) {
1398 return -ENOMEM;
1401 reset_hc(ohci);
1403 retval = -EBUSY;
1404 if (request_irq(irq, ohci_interrupt, SA_SHIRQ, "ohci-usb", ohci) == 0) {
1405 int pid;
1407 MOD_INC_USE_COUNT;
1408 ohci->irq = irq;
1410 pid = kernel_thread(ohci_roothub_thread, ohci, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
1411 if (pid >= 0)
1412 return 0;
1415 MOD_DEC_USE_COUNT;
1416 retval = pid;
1419 release_ohci(ohci);
1420 return retval;
1423 static int start_ohci(struct pci_dev *dev)
1425 unsigned int mem_base = dev->base_address[0];
1427 /* If its OHCI, its memory */
1428 if (mem_base & PCI_BASE_ADDRESS_SPACE_IO)
1429 return -ENODEV;
1431 /* Get the memory address and map it for IO */
1432 mem_base &= PCI_BASE_ADDRESS_MEM_MASK;
1435 * FIXME ioremap_nocache isn't implemented on all CPUs (such
1436 * as the Alpha) [?] What should I use instead...
1438 * The iounmap() is done on in release_ohci.
1440 mem_base = (unsigned int) ioremap_nocache(mem_base, 4096);
1442 if (!mem_base) {
1443 printk("Error mapping OHCI memory\n");
1444 return -EFAULT;
1447 return found_ohci(dev->irq, (void *) mem_base);
1452 #ifdef CONFIG_APM
1453 static int handle_apm_event(apm_event_t event)
1455 static int down = 0;
1457 switch (event) {
1458 case APM_SYS_SUSPEND:
1459 case APM_USER_SUSPEND:
1460 if (down) {
1461 printk(KERN_DEBUG "ohci: received extra suspend event\n");
1462 break;
1464 down = 1;
1465 break;
1466 case APM_NORMAL_RESUME:
1467 case APM_CRITICAL_RESUME:
1468 if (!down) {
1469 printk(KERN_DEBUG "ohci: received bogus resume event\n");
1470 break;
1472 down = 0;
1473 if (waitqueue_active(&root_hub)) {
1474 apm_resume = 1;
1475 wake_up(&root_hub);
1477 break;
1479 return 0;
1481 #endif
1484 #ifdef MODULE
1486 void cleanup_module(void)
1488 #ifdef CONFIG_APM
1489 apm_unregister_callback(&handle_apm_event);
1490 #endif
1493 #define ohci_hcd_init init_module
1495 #endif
1497 #define PCI_CLASS_SERIAL_USB_OHCI 0x0C0310
1498 #define PCI_CLASS_SERIAL_USB_OHCI_PG 0x10
1501 int ohci_hcd_init(void)
1503 int retval;
1504 struct pci_dev *dev = NULL;
1506 retval = -ENODEV;
1508 dev = NULL;
1509 while((dev = pci_find_class(PCI_CLASS_SERIAL_USB_OHCI, dev))) { /* OHCI */
1510 retval = start_ohci(dev);
1511 if (retval < 0) break;
1514 #ifdef CONFIG_APM
1515 apm_register_callback(&handle_apm_event);
1516 #endif
1518 return 0;
1520 return retval;