Import 2.3.18pre1
[davej-history.git] / drivers / usb / ohci-hcd.c
blob2812fb75c39aea0d137b53d732d77c9720c2d523
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 alpha , Bulk, 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 ]
20 * v4.2 1999/09/05 ISO API alpha, new dev alloc, neg Error-codes
21 * v4.1 1999/08/27 Randy Dunlap's - ISO API first impl.
22 * v4.0 1999/08/18
23 * v3.0 1999/06/25
24 * v2.1 1999/05/09 code clean up
25 * v2.0 1999/05/04
26 * virtual root hub is now enabled,
27 * memory allocation based on kmalloc and kfree now, Bus error handling,
28 * INT, CTRL and BULK transfers enabled, ISO needs testing (alpha)
30 * from Linus Torvalds (uhci.c) (APM not tested; hub, usb_device, bus and related stuff)
31 * from Greg Smith (ohci.c) (reset controller handling, hub)
33 * v1.0 1999/04/27 initial release
34 * ohci-hcd.c
39 #include <linux/config.h>
40 #include <linux/module.h>
41 #include <linux/pci.h>
42 #include <linux/kernel.h>
43 #include <linux/delay.h>
44 #include <linux/ioport.h>
45 #include <linux/sched.h>
46 #include <linux/malloc.h>
47 #include <linux/smp_lock.h>
48 #include <linux/errno.h>
49 #include <linux/timer.h>
50 #include <linux/spinlock.h>
52 #include <asm/io.h>
53 #include <asm/irq.h>
54 #include <asm/system.h>
56 #include "usb.h"
57 #include "ohci-hcd.h"
59 #ifdef CONFIG_APM
60 #include <linux/apm_bios.h>
61 static int handle_apm_event(apm_event_t event);
62 static int apm_resume = 0;
63 #endif
65 static int ohci_link_ed(struct ohci * ohci, struct usb_ohci_ed *ed);
66 static int sohci_kill_isoc (struct usb_isoc_desc *id);
67 static int sohci_get_current_frame_number (struct usb_device *usb_dev);
68 static int sohci_run_isoc(struct usb_isoc_desc *id, struct usb_isoc_desc *pr_id);
69 static DECLARE_WAIT_QUEUE_HEAD(op_wakeup);
71 void usb_pipe_to_hcd_ed(struct usb_device *usb_dev, unsigned int pipe, struct usb_hcd_ed *hcd_ed)
73 hcd_ed->endpoint = usb_pipeendpoint(pipe);
74 hcd_ed->out = usb_pipeout(pipe);
75 hcd_ed->function = usb_pipedevice(pipe);
76 hcd_ed->type = usb_pipetype(pipe);
77 hcd_ed->slow = usb_pipeslow(pipe);
78 hcd_ed->maxpack = usb_maxpacket(usb_dev, pipe, usb_pipeout(pipe));
79 OHCI_DEBUG(printk("******* hcd_ed: pipe: %8x, endpoint: %4x, function: %4x, out: %4x, type: %4x, slow: %4x, maxpack: %4x\n", pipe, hcd_ed->endpoint, hcd_ed->function, hcd_ed->out, hcd_ed->type, hcd_ed->slow, hcd_ed->maxpack); )
80 OHCI_DEBUG(printk("******* dev: devnum: %4x, slow: %4x, maxpacketsize: %4x\n",usb_dev->devnum, usb_dev->slow, usb_dev->maxpacketsize); )
84 /********
85 **** Interface functions
86 ***********************************************/
88 static int sohci_blocking_handler(void * ohci_in, struct usb_ohci_ed *ed, void * data, int data_len, int status, __OHCI_BAG lw0, __OHCI_BAG lw1)
90 if(lw0 != NULL) {
91 if(USB_ST_CRC < 0 && (status == USB_ST_DATAUNDERRUN || status == USB_ST_NOERROR))
92 ((struct ohci_state * )lw0)->status = data_len;
93 else
94 ((struct ohci_state * )lw0)->status = status;
95 ((struct ohci_state * )lw0)->len = data_len;
97 if(lw1 != NULL) {
98 add_wait_queue(&op_wakeup, lw1);
99 wake_up(&op_wakeup);
102 OHCI_DEBUG( { int i; printk("USB HC bh <<<: %x: ", ed->hwINFO);)
103 OHCI_DEBUG( printk(" data(%d):", data_len);)
104 OHCI_DEBUG( for(i=0; i < data_len; i++ ) printk(" %02x", ((__u8 *) data)[i]);)
105 OHCI_DEBUG( printk(" ret_status: %x\n", status); })
107 return 0;
110 static int sohci_int_handler(void * ohci_in, struct usb_ohci_ed *ed, void * data, int data_len, int status, __OHCI_BAG lw0, __OHCI_BAG lw1)
113 struct ohci * ohci = ohci_in;
114 usb_device_irq handler=(void *) lw0;
115 void *dev_id = (void *) lw1;
116 int ret;
118 OHCI_DEBUG({ int i; printk("USB HC IRQ <<<: %x: data(%d):", ed->hwINFO, data_len);)
119 OHCI_DEBUG( for(i=0; i < data_len; i++ ) printk(" %02x", ((__u8 *) data)[i]);)
120 OHCI_DEBUG( printk(" ret_status: %x\n", status); })
122 ret = handler(status, data, data_len, dev_id);
123 if(ret == 0) return 0; /* 0 .. do not requeue */
124 if(status > 0) return -1; /* error occured do not requeue ? */
125 ohci_trans_req(ohci, ed, 0, NULL, data, (ed->hwINFO >> 16) & 0x3f, (__OHCI_BAG) handler, (__OHCI_BAG) dev_id, INT_IN, sohci_int_handler); /* requeue int request */
127 return 0;
130 static int sohci_iso_handler(void * ohci_in, struct usb_ohci_ed *ed, void * data, int data_len, int status, __OHCI_BAG lw0, __OHCI_BAG lw1) {
132 // struct ohci * ohci = ohci_in;
133 unsigned int ix = (unsigned int) lw0;
134 struct usb_isoc_desc * id = (struct usb_isoc_desc *) lw1;
135 struct usb_ohci_td **tdp = id->td;
136 int ret = 0;
137 int fx;
139 OHCI_DEBUG({ int i; printk("USB HC ISO |||: %x: data(%d):", ed->hwINFO, data_len);)
140 OHCI_DEBUG( for(i=0; i < 16 ; i++ ) printk(" %02x", ((__u8 *) data)[i]);)
141 OHCI_DEBUG( printk(" ... ret_status: %x\n", status); })
143 tdp[ix] = NULL;
144 id->frames[ix].frame_length = data_len;
145 id->frames[ix].frame_status = status;
146 id->total_length += data_len;
147 if(status) id->error_count++;
149 id->cur_completed_frame++;
150 id->total_completed_frames++;
152 if(id->cur_completed_frame == id->callback_frames) {
153 id->prev_completed_frame = id->cur_completed_frame;
154 id->cur_completed_frame = 0;
155 OHCI_DEBUG(printk("USB HC ISO <<<: %x: \n", ed->hwINFO);)
156 ret = id->callback_fn(id->error_count, id->data, id->total_length, id);
157 switch (ret) {
158 case CB_CONT_RUN:
159 for (fx = 0; fx < id->frame_count; fx++)
160 id->frames[fx].frame_length = id->frame_size;
161 sohci_run_isoc(id, id->prev_isocdesc);
162 break;
164 case CB_CONTINUE:
165 break;
167 case CB_REUSE:
168 break;
170 case CB_RESTART:
171 break;
173 case CB_ABORT:
174 sohci_kill_isoc(id);
175 break;
179 return 0;
182 static void * sohci_request_irq(struct usb_device *usb_dev, unsigned int pipe, usb_device_irq handler, int period, void *dev_id)
184 struct ohci * ohci = usb_dev->bus->hcpriv;
185 struct ohci_device * dev = usb_to_ohci(usb_dev);
186 struct usb_hcd_ed hcd_ed;
187 struct usb_ohci_ed * ed;
189 #ifdef VROOTHUB
190 if(usb_pipedevice(pipe) == ohci->rh.devnum)
191 return root_hub_request_irq(usb_dev, pipe, handler, period, dev_id);
192 #endif
194 usb_pipe_to_hcd_ed(usb_dev, pipe, &hcd_ed);
195 hcd_ed.type = INT;
196 ed = usb_ohci_add_ep(usb_dev, &hcd_ed, period, 1);
198 OHCI_DEBUG( printk("USB HC IRQ>>>: %x: every %d ms\n", ed->hwINFO, period);)
200 ohci_trans_req(ohci, ed, 0, NULL, dev->data, hcd_ed.maxpack, (__OHCI_BAG) handler, (__OHCI_BAG) dev_id, INT_IN, sohci_int_handler);
201 if (ED_STATE(ed) != ED_OPER) ohci_link_ed(ohci, ed);
202 return ed;
206 static int sohci_release_irq(struct usb_device *usb_dev, void * ed)
208 // struct usb_device *usb_dev = ((struct ohci_device *) ((unsigned int)ed & 0xfffff000))->usb;
209 struct ohci * ohci = usb_dev->bus->hcpriv;
211 OHCI_DEBUG( printk("USB HC ***** RM_IRQ>>>:%4x\n", (unsigned int) ed);)
213 if(ed == NULL) return 0;
215 #ifdef VROOTHUB
216 if(ed == ohci->rh.int_addr)
217 return root_hub_release_irq(usb_dev, ed);
218 #endif
219 ED_setSTATE((struct usb_ohci_ed *)ed, ED_STOP);
221 usb_ohci_rm_ep(usb_dev, (struct usb_ohci_ed *) ed, NULL, NULL, NULL, 0);
223 return 0;
226 static int sohci_control_msg(struct usb_device *usb_dev, unsigned int pipe, devrequest *cmd, void *data, int len)
228 DECLARE_WAITQUEUE(wait, current);
229 struct ohci_state state = {0, TD_NOTACCESSED};
230 struct ohci * ohci = usb_dev->bus->hcpriv;
231 struct usb_hcd_ed hcd_ed;
232 struct usb_ohci_ed *ed;
234 #ifdef VROOTHUB
235 if(usb_pipedevice(pipe) == ohci->rh.devnum)
236 return root_hub_control_msg(usb_dev, pipe, cmd, data, len);
237 #endif
239 usb_pipe_to_hcd_ed(usb_dev, pipe, &hcd_ed);
240 hcd_ed.type = CTRL;
242 ed = usb_ohci_add_ep(usb_dev, &hcd_ed, 0, 1);
244 OHCI_DEBUG( { int i; printk("USB HC CTRL>>>: ed:%x-%x: ctrl(%d):", (unsigned int) ed, ed->hwINFO, 8);)
245 OHCI_DEBUG( for(i=0; i < 8; i++ ) printk(" %02x", ((__u8 *) cmd)[i]);)
246 OHCI_DEBUG( printk(" data(%d):", len);)
247 OHCI_DEBUG( for(i=0; i < len; i++ ) printk(" %02x", ((__u8 *) data)[i]);)
248 OHCI_DEBUG( printk("\n"); })
249 current->state = TASK_UNINTERRUPTIBLE;
251 ohci_trans_req(ohci, ed, 8, cmd, data, len, (__OHCI_BAG) &state, (__OHCI_BAG) &wait, (usb_pipeout(pipe))?CTRL_OUT:CTRL_IN, sohci_blocking_handler);
253 OHCI_DEBUG(printk("USB HC trans req ed %x: %x :", ed->hwINFO, (unsigned int ) ed); )
254 OHCI_DEBUG({ int i; for( i= 0; i<8 ;i++) printk(" %4x", ((unsigned int *) ed)[i]) ; printk("\n"); }; )
255 if (ED_STATE(ed) != ED_OPER) ohci_link_ed(ohci, ed);
256 schedule_timeout(HZ*5);
258 if(state.status == TD_NOTACCESSED) {
259 current->state = TASK_UNINTERRUPTIBLE;
260 usb_ohci_rm_ep(usb_dev, ed, sohci_blocking_handler, NULL, NULL, 0);
261 schedule();
262 state.status = USB_ST_TIMEOUT;
264 remove_wait_queue(&op_wakeup, &wait);
265 return state.status;
268 static int sohci_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, void *data, int len, unsigned long *rval)
270 DECLARE_WAITQUEUE(wait, current);
271 struct ohci_state state = {0, TD_NOTACCESSED};
272 struct ohci * ohci = usb_dev->bus->hcpriv;
273 struct usb_hcd_ed hcd_ed;
274 struct usb_ohci_ed *ed;
276 usb_pipe_to_hcd_ed(usb_dev, pipe, &hcd_ed);
277 hcd_ed.type = BULK;
278 ed = usb_ohci_add_ep(usb_dev, &hcd_ed, 0, 1);
279 OHCI_DEBUG( { int i; printk("USB HC BULK>>>: %x: ", ed->hwINFO);)
280 OHCI_DEBUG( printk(" data(%d):", len);)
281 OHCI_DEBUG( for(i=0; i < len; i++ ) printk(" %02x", ((__u8 *) data)[i]);)
282 OHCI_DEBUG( printk("\n"); })
283 current->state = TASK_UNINTERRUPTIBLE;
285 ohci_trans_req(ohci, ed, 0, NULL, data, len, (__OHCI_BAG) &state, (__OHCI_BAG) &wait,(usb_pipeout(pipe))?BULK_OUT:BULK_IN, sohci_blocking_handler);
286 if (ED_STATE(ed) != ED_OPER) ohci_link_ed(ohci, ed);
288 schedule_timeout(HZ*5);
290 if(state.status == TD_NOTACCESSED) {
291 current->state = TASK_UNINTERRUPTIBLE;
292 usb_ohci_rm_ep(usb_dev, ed, sohci_blocking_handler, NULL, NULL, 0);
293 schedule();
294 state.status = USB_ST_TIMEOUT;
296 remove_wait_queue(&op_wakeup, &wait);
297 *rval = state.len;
298 return state.status;
301 static void * sohci_request_bulk(struct usb_device *usb_dev, unsigned int pipe, usb_device_irq handler, void *data, int len, void *dev_id)
303 struct ohci * ohci = usb_dev->bus->hcpriv;
304 struct usb_hcd_ed hcd_ed;
305 struct usb_ohci_ed * ed;
307 usb_pipe_to_hcd_ed(usb_dev, pipe, &hcd_ed);
308 hcd_ed.type = BULK;
309 ed = usb_ohci_add_ep(usb_dev, &hcd_ed, 0, 1);
311 OHCI_DEBUG( printk("USB HC BULK_RQ>>>: %x \n", ed->hwINFO);)
313 ohci_trans_req(ohci, ed, 0, NULL, data, len, (__OHCI_BAG) handler, (__OHCI_BAG) dev_id, (usb_pipeout(pipe))?BULK_OUT:BULK_IN, sohci_int_handler);
314 if (ED_STATE(ed) != ED_OPER) ohci_link_ed(ohci, ed);
316 return ed;
319 static int sohci_terminate_bulk(struct usb_device *usb_dev, void * ed)
321 DECLARE_WAITQUEUE(wait, current);
323 OHCI_DEBUG( printk("USB HC TERM_BULK>>>:%4x\n", (unsigned int) ed);)
325 current->state = TASK_UNINTERRUPTIBLE;
326 usb_ohci_rm_ep(usb_dev, (struct usb_ohci_ed *) ed, sohci_blocking_handler, NULL, &wait, SEND);
327 schedule();
328 remove_wait_queue(&op_wakeup, &wait);
329 return 1;
332 static int sohci_alloc_dev(struct usb_device *usb_dev)
334 struct ohci_device *dev;
336 /* Allocate the OHCI_HCD device private data */
337 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
338 if (!dev)
339 return -1;
341 /* Initialize "dev" */
342 memset(dev, 0, sizeof(*dev));
344 usb_dev->hcpriv = dev;
345 dev->usb = usb_dev;
346 atomic_set(&dev->refcnt, 1);
348 if (usb_dev->parent)
349 dev->ohci = usb_to_ohci(usb_dev->parent)->ohci;
351 return 0;
354 static int sohci_free_dev(struct usb_device *usb_dev)
356 int cnt;
357 DECLARE_WAITQUEUE(wait, current);
358 struct ohci_device *dev = usb_to_ohci(usb_dev);
360 OHCI_DEBUG(printk("USB HC ***** free %x\n", usb_dev->devnum);)
362 if(usb_dev->devnum >= 0) {
363 current->state = TASK_UNINTERRUPTIBLE;
364 cnt = usb_ohci_rm_function(usb_dev, sohci_blocking_handler, NULL, &wait);
365 if(cnt > 0) {
366 schedule();
367 remove_wait_queue(&op_wakeup, &wait);
369 current->state = TASK_INTERRUPTIBLE;
372 if (atomic_dec_and_test(&dev->refcnt))
373 kfree(dev);
375 return 0;
381 * ISO Interface designed by Randy Dunlap
384 static int sohci_get_current_frame_number(struct usb_device *usb_dev) {
386 struct ohci * ohci = usb_dev->bus->hcpriv;
388 return readl(&ohci->regs->fmnumber) & 0xffff;
392 static int sohci_init_isoc(struct usb_device *usb_dev, unsigned int pipe, int frame_count, void *context, struct usb_isoc_desc **idp) {
394 struct usb_isoc_desc *id;
396 *idp = NULL;
398 id = kmalloc (sizeof (struct usb_isoc_desc) + (sizeof (struct isoc_frame_desc) * frame_count), GFP_KERNEL);
399 if(!id) return -ENOMEM;
400 memset (id, 0, sizeof (struct usb_isoc_desc) + (sizeof (struct isoc_frame_desc) * frame_count));
401 OHCI_DEBUG(printk("ISO alloc id: %p, size: %d\n", id, sizeof (struct usb_isoc_desc) + (sizeof (struct isoc_frame_desc) * frame_count));)
402 id->td = kmalloc (sizeof (void *) * frame_count, GFP_KERNEL);
403 if(!id->td) {
404 kfree (id);
405 return -ENOMEM;
407 memset (id->td, 0, sizeof (void *) * frame_count);
408 OHCI_DEBUG(printk("ISO alloc id->td: %p, size: %d\n", id->td, sizeof (void *) * frame_count);)
410 id->frame_count = frame_count;
411 id->frame_size = usb_maxpacket (usb_dev, pipe, usb_pipeout(pipe));
412 id->start_frame = -1;
413 id->end_frame = -1;
414 id->usb_dev = usb_dev;
415 id->pipe = pipe;
416 id->context = context;
418 *idp = id;
419 return 0;
422 void print_int_eds(struct ohci * ohci);
424 static int sohci_run_isoc(struct usb_isoc_desc *id, struct usb_isoc_desc *pr_id) {
426 struct ohci * ohci = id->usb_dev->bus->hcpriv;
427 struct usb_ohci_td ** tdp = (struct usb_ohci_td **) id->td;
428 struct usb_hcd_ed hcd_ed;
429 struct usb_ohci_ed * ed;
430 int ix, frlen;
431 unsigned char *bufptr;
433 if(pr_id)
434 id->start_frame = pr_id->end_frame + 1;
435 else {
436 switch(id->start_type) {
437 case START_ABSOLUTE:
438 break;
440 case START_RELATIVE:
441 if(id->start_frame < START_FRAME_FUDGE) id->start_frame = START_FRAME_FUDGE;
442 id->start_frame += sohci_get_current_frame_number(id->usb_dev);
443 break;
445 case START_ASAP:
446 id->start_frame = START_FRAME_FUDGE + sohci_get_current_frame_number(id->usb_dev);
447 break;
451 id->start_frame &= 0xffff;
452 id->end_frame = (id->start_frame + id->frame_count - 1) & 0xffff;
454 id->prev_completed_frame = 0;
455 id->cur_completed_frame = 0;
456 if (id->frame_spacing <= 0) id->frame_spacing = 1;
458 bufptr = id->data;
460 usb_pipe_to_hcd_ed(id->usb_dev, id->pipe, &hcd_ed);
461 hcd_ed.type = ISO;
462 ed = usb_ohci_add_ep(id->usb_dev, &hcd_ed, 1, 1);
463 OHCI_DEBUG( printk("USB HC ISO>>>: ed: %x-%x: (%d tds)\n", (unsigned int) ed, ed->hwINFO, id->frame_count);)
465 for (ix = 0; ix < id->frame_count; ix++) {
466 frlen = (id->frames[ix].frame_length > id->frame_size)? id->frame_size : id->frames[ix].frame_length;
467 printk("ISO run id->td: %p \n", &tdp[ix]);
468 tdp[ix] = ohci_trans_req(ohci, ed, id->start_frame + ix , NULL, bufptr, frlen, (__OHCI_BAG) ix, (__OHCI_BAG) id,
469 (usb_pipeout(id->pipe))?ISO_OUT:ISO_IN,
470 sohci_iso_handler);
471 bufptr += frlen;
474 if (ED_STATE(ed) != ED_OPER) ohci_link_ed(ohci, ed);
475 print_int_eds(ohci);
476 return 0;
479 static int sohci_kill_isoc(struct usb_isoc_desc *id) {
481 struct usb_ohci_ed *ed = NULL;
482 struct usb_ohci_td **td = id->td;
483 int i;
484 printk("KILL_ISOC***:\n");
485 for (i = 0; i < id->frame_count; i++) {
486 if(td[i]) {
487 td[i]->type |= DEL;
488 ed = td[i]->ed; printk(" %d", i);
491 if(ed) usb_ohci_rm_ep(id->usb_dev, ed, NULL, NULL, NULL, TD_RM);
492 printk(": end KILL_ISOC***: %p\n", ed);
493 id->start_frame = -1;
494 return 0;
498 static void sohci_free_isoc(struct usb_isoc_desc *id) {
499 printk("FREE_ISOC***\n");
500 wait_ms(2000);
501 if(id->start_frame >= 0) sohci_kill_isoc(id);
502 printk("FREE_ISOC2***\n");
503 wait_ms(2000);
504 kfree(id->td);
505 kfree(id);
506 printk("FREE_ISOC3***\n");
507 wait_ms(2000);
510 struct usb_operations sohci_device_operations = {
511 sohci_alloc_dev,
512 sohci_free_dev,
513 sohci_control_msg,
514 sohci_bulk_msg,
515 sohci_request_irq,
516 sohci_release_irq,
517 sohci_request_bulk,
518 sohci_terminate_bulk,
519 sohci_get_current_frame_number,
520 sohci_init_isoc,
521 sohci_free_isoc,
522 sohci_run_isoc,
523 sohci_kill_isoc
527 /******
528 *** ED handling functions
529 ************************************/
531 /* just for debugging; prints all 32 branches of the int ed tree inclusive iso eds*/
532 void print_int_eds(struct ohci * ohci) {int i; __u32 * ed_p;
533 for(i= 0; i < 32; i++) {
534 OHCI_DEBUG(printk("branch int %2d(%2x): ", i,i); )
535 ed_p = &(ohci->hc_area->hcca.int_table[i]);
536 while (*ed_p != 0) {
537 OHCI_DEBUG(printk("ed: %4x; ", (((struct usb_ohci_ed *)bus_to_virt(*ed_p))->hwINFO));)
538 ed_p = &(((struct usb_ohci_ed *)bus_to_virt(*ed_p))->hwNextED);
540 OHCI_DEBUG(printk("\n");)
545 * search for the right branch to insert an interrupt ed into the int tree
546 * do some load ballancing
547 * returns the branch and
548 * sets the interval to interval = 2^integer(ld(interval))
549 * */
551 static int usb_ohci_int_ballance(struct ohci * ohci, int * interval, int load) {
553 int i,j;
555 j = 0; /* search for the least loaded interrupt endpoint branch of all 32 branches */
556 for(i=0; i< 32; i++) if(ohci->ohci_int_load[j] > ohci->ohci_int_load[i]) j=i;
558 for(i= 0; ((*interval >> i) > 1 ) && (i < 5); i++ ); /* interval = 2^int(ld(interval)) */
559 *interval = 1 << i;
561 j = j % (*interval);
562 for(i=j; i< 32; i+=(*interval)) ohci->ohci_int_load[i] += load;
565 OHCI_DEBUG(printk("USB HC new int ed on pos %d of interval %d \n",j, *interval);)
567 return j;
570 /* the int tree is a binary tree
571 * in order to process it sequentially the indexes of the branches have to be mapped
572 * the mapping reverses the bits of a word of num_bits length
573 * */
574 static int rev(int num_bits, int word) {
575 int i;
576 int wout = 0;
578 for(i = 0; i < num_bits; i++) wout |= (((word >> i) & 1) << (num_bits - i - 1));
579 return wout;
582 /* get the ed from the endpoint / usb_device address */
584 struct usb_ohci_ed * ohci_find_ep(struct usb_device *usb_dev, struct usb_hcd_ed *hcd_ed) {
585 return &(usb_to_ohci(usb_dev)->ed[(hcd_ed->endpoint << 1) | ((hcd_ed->type == CTRL)? 0:hcd_ed->out)]);
588 /* link an ed into one of the HC chains */
589 static int ohci_link_ed(struct ohci * ohci, struct usb_ohci_ed *ed) {
591 int int_branch;
592 int i;
593 int inter;
594 int interval;
595 int load;
596 __u32 * ed_p;
598 ED_setSTATE(ed, ED_OPER);
600 switch(ED_TYPE(ed)) {
601 case CTRL:
602 ed->hwNextED = 0;
603 if(ohci->ed_controltail == NULL) {
604 writel(virt_to_bus(ed), &ohci->regs->ed_controlhead);
606 else {
607 ohci->ed_controltail->hwNextED = virt_to_bus(ed);
609 ed->ed_prev = ohci->ed_controltail;
610 ohci->ed_controltail = ed;
611 break;
613 case BULK:
614 ed->hwNextED = 0;
615 if(ohci->ed_bulktail == NULL) {
616 writel(virt_to_bus(ed), &ohci->regs->ed_bulkhead);
618 else {
619 ohci->ed_bulktail->hwNextED = virt_to_bus(ed);
621 ed->ed_prev = ohci->ed_bulktail;
622 ohci->ed_bulktail = ed;
623 break;
625 case INT:
626 interval = ed->int_period;
627 load = ed->int_load;
628 int_branch = usb_ohci_int_ballance(ohci, &interval, load);
629 ed->int_interval = interval;
630 ed->int_branch = int_branch;
632 for( i= 0; i < rev(6, interval); i += inter) {
633 inter = 1;
634 for(ed_p = &(ohci->hc_area->hcca.int_table[rev(5,i)+int_branch]);
635 (*ed_p != 0) && (((struct usb_ohci_ed *)bus_to_virt(*ed_p))->int_interval >= interval);
636 ed_p = &(((struct usb_ohci_ed *)bus_to_virt(*ed_p))->hwNextED))
637 inter = rev(6,((struct usb_ohci_ed *)bus_to_virt(*ed_p))->int_interval);
638 ed->hwNextED = *ed_p;
639 *ed_p = virt_to_bus(ed);
640 OHCI_DEBUG(printk("int_link i: %2x, inter: %2x, ed: %4x\n", i, inter, ed->hwINFO);)
642 break;
644 case ISO:
645 if(ohci->ed_isotail != NULL) {
646 ohci->ed_isotail->hwNextED = virt_to_bus(ed);
647 ed->ed_prev = ohci->ed_isotail;
649 else {
650 for( i= 0; i < 32; i += inter) {
651 inter = 1;
652 for(ed_p = &(ohci->hc_area->hcca.int_table[rev(5,i)]);
653 *ed_p != 0;
654 ed_p = &(((struct usb_ohci_ed *)bus_to_virt(*ed_p))->hwNextED))
655 inter = rev(6,((struct usb_ohci_ed *)bus_to_virt(*ed_p))->int_interval);
656 *ed_p = virt_to_bus(ed);
658 ed->ed_prev = NULL;
660 ed->hwNextED = 0;
661 ohci->ed_isotail = ed;
662 break;
666 return 1;
669 /* unlink an ed from one of the HC chains.
670 * just the link to the ed is unlinked.
671 * the link from the ed still points to another operational ed or 0
672 * so the HC can eventually finish the processing of the unlinked ed
673 * */
674 static int ohci_unlink_ed(struct ohci * ohci, struct usb_ohci_ed *ed) {
676 int int_branch;
677 int i;
678 int inter;
679 int interval;
680 __u32 * ed_p;
683 switch(ED_TYPE(ed)) {
684 case CTRL:
685 if(ed->ed_prev == NULL) {
686 writel(ed->hwNextED, &ohci->regs->ed_controlhead);
688 else {
689 ed->ed_prev->hwNextED = ed->hwNextED;
691 if(ohci->ed_controltail == ed) {
692 ohci->ed_controltail = ed->ed_prev;
694 else {
695 ((struct usb_ohci_ed *)bus_to_virt(ed->hwNextED))->ed_prev = ed->ed_prev;
697 break;
699 case BULK:
700 if(ed->ed_prev == NULL) {
701 writel(ed->hwNextED, &ohci->regs->ed_bulkhead);
703 else {
704 ed->ed_prev->hwNextED = ed->hwNextED;
706 if(ohci->ed_bulktail == ed) {
707 ohci->ed_bulktail = ed->ed_prev;
709 else {
710 ((struct usb_ohci_ed *)bus_to_virt(ed->hwNextED))->ed_prev = ed->ed_prev;
712 break;
714 case INT:
715 int_branch = ed->int_branch;
716 interval = ed->int_interval;
718 for( i= 0; i < rev(6,interval); i += inter) {
719 for(ed_p = &(ohci->hc_area->hcca.int_table[rev(5,i)+int_branch]), inter = 1;
720 (*ed_p != 0) && (*ed_p != ed->hwNextED);
721 ed_p = &(((struct usb_ohci_ed *)bus_to_virt(*ed_p))->hwNextED),
722 inter = rev(6, ((struct usb_ohci_ed *)bus_to_virt(*ed_p))->int_interval)) {
723 if(((struct usb_ohci_ed *)bus_to_virt(*ed_p)) == ed) {
724 *ed_p = ed->hwNextED;
725 break;
728 OHCI_DEBUG(printk("int_link i: %2x, inter: %2x, ed: %4x\n", i, inter, ed->hwINFO);)
730 for(i=int_branch; i< 32; i+=interval) ohci->ohci_int_load[i] -= ed->int_load;
731 break;
733 case ISO:
734 if(ohci->ed_isotail == ed)
735 ohci->ed_isotail = ed->ed_prev;
736 if(ed->hwNextED != 0)
737 ((struct usb_ohci_ed *)bus_to_virt(ed->hwNextED))->ed_prev = ed->ed_prev;
739 if(ed->ed_prev != NULL) {
740 ed->ed_prev->hwNextED = ed->hwNextED;
742 else {
743 for( i= 0; i < 32; i += inter) {
744 inter = 1;
745 for(ed_p = &(ohci->hc_area->hcca.int_table[rev(5,i)]);
746 *ed_p != 0;
747 ed_p = &(((struct usb_ohci_ed *)bus_to_virt(*ed_p))->hwNextED)) {
748 inter = rev(6,((struct usb_ohci_ed *)bus_to_virt(*ed_p))->int_interval);
749 if(((struct usb_ohci_ed *)bus_to_virt(*ed_p)) == ed) {
750 *ed_p = ed->hwNextED;
751 break;
756 break;
758 ED_setSTATE(ed, ED_UNLINK);
759 return 1;
762 spinlock_t usb_ed_lock = SPIN_LOCK_UNLOCKED;
764 /* add/reinit an endpoint; this should be done once at the usb_set_configuration command,
765 * but the USB stack is a little bit stateless so we do it at every transaction
766 * if the state of the ed is ED_NEW then a dummy td is added and the state is changed to ED_UNLINK
767 * in all other cases the state is left unchanged
768 * the ed info fields are setted anyway even though they should not change
769 * */
770 struct usb_ohci_ed *usb_ohci_add_ep(struct usb_device * usb_dev, struct usb_hcd_ed * hcd_ed, int interval, int load) {
772 // struct ohci * ohci = usb_dev->bus->hcpriv;
773 struct usb_ohci_td * td;
774 struct usb_ohci_ed * ed, *ed1;
776 int ed_state, ed_state1;
778 spin_lock(&usb_ed_lock);
780 ed = ohci_find_ep(usb_dev, hcd_ed);
783 ed1 = ((void *) ed) + 0x40; ed_state1 = ED_STATE(ed1);
784 OHCI_DEBUG(printk("++++ USB HC add 60 ed1 %x: %x :state: %x\n", ed1->hwINFO, (unsigned int ) ed1, ed_state1); )
785 ed_state = ED_STATE(ed); /* store state of ed */
786 OHCI_DEBUG(printk("USB HC add ed %x: %x :state: %x\n", ed->hwINFO, (unsigned int ) ed, ed_state); )
787 if (ed_state == ED_NEW) {
788 OHCI_ALLOC(td, sizeof(*td)); /* dummy td; end of td list for ed */
789 ed->hwTailP = virt_to_bus(td);
790 ed->hwHeadP = ed->hwTailP;
791 ed_state = ED_UNLINK;
794 ed->hwINFO = hcd_ed->function
795 | hcd_ed->endpoint << 7
796 | (hcd_ed->type == ISO? 0x8000 : 0)
797 | (hcd_ed->type == CTRL? 0:(hcd_ed->out == 1? 0x800 : 0x1000 ))
798 | hcd_ed->slow << 13
799 | hcd_ed->maxpack << 16
800 | hcd_ed->type << 27
801 // | 1 << 14
802 | ed_state << 29 ;
804 if (ED_TYPE(ed) == INT && ed_state == ED_UNLINK) {
805 ed->int_period = interval;
806 ed->int_load = load;
809 spin_unlock(&usb_ed_lock);
810 return ed;
815 /*****
816 * Request the removal of an endpoint
818 * put the ep on the rm_list and request a stop of the bulk or ctrl list
819 * real removal is done at the next start of frame (SOF) hardware interrupt
820 * the dummy td carries the essential information (handler, proc queue, ...)
821 * if(send & TD_RM) then just the TD witch have (TD->type & DEL) set will be removed
822 * otherwise all TDs including the dummy TD of the ED will be removed
824 int usb_ohci_rm_ep(struct usb_device * usb_dev, struct usb_ohci_ed *ed, f_handler handler, __OHCI_BAG lw0, __OHCI_BAG lw1, int send)
826 unsigned int flags;
828 struct ohci * ohci = usb_dev->bus->hcpriv;
829 struct usb_ohci_td *td;
831 OHCI_DEBUG(printk("USB HC remove ed %x: %x :\n", ed->hwINFO, (unsigned int ) ed); )
833 spin_lock_irqsave(&usb_ed_lock, flags);
834 ed->hwINFO |= OHCI_ED_SKIP;
835 writel( OHCI_INTR_SF, &ohci->regs->intrenable); /* enable sof interrupt */
838 if(send & TD_RM) { /* delete selected TDs */
839 ED_setSTATE(ed, ED_TD_DEL);
841 else { /* delete all TDS */
842 if(ED_STATE(ed) == ED_OPER) ohci_unlink_ed(ohci, ed);
843 td = (struct usb_ohci_td *) bus_to_virt(ed->hwTailP);
844 td->lw0 = lw0;
845 td->lw1 = lw1;
846 td->ed = ed;
847 td->hwINFO = TD_CC;
848 td->handler = handler;
849 td->type = send;
850 ED_setSTATE(ed, ED_DEL);
852 ed->ed_prev = ohci->ed_rm_list;
853 ohci->ed_rm_list = ed;
855 switch(ED_TYPE(ed)) {
856 case CTRL:
857 writel_mask(~(0x01<<4), &ohci->regs->control); /* stop CTRL list */
858 break;
859 case BULK:
860 writel_mask(~(0x01<<5), &ohci->regs->control); /* stop BULK list */
861 break;
864 spin_unlock_irqrestore(&usb_ed_lock, flags);
866 return 1;
870 /* remove all endpoints of a function (device)
871 * just the last ed sends a reply
872 * the last ed is ed0 as there always should be an ep0
873 * */
874 int usb_ohci_rm_function(struct usb_device * usb_dev, f_handler handler,__OHCI_BAG tw0, __OHCI_BAG tw1)
876 struct usb_ohci_ed *ed;
877 int i;
878 int cnt = 0;
881 for(i = NUM_EDS - 1 ; i >= 0; i--) {
882 ed = &(usb_to_ohci(usb_dev)->ed[i]);
883 if(ED_STATE(ed) != ED_NEW) {
884 OHCI_DEBUG(printk("USB RM FUNCTION ed: %4x;\n", ed->hwINFO);)
885 usb_ohci_rm_ep(usb_dev, ed, handler, tw0, tw1, i==0?SEND:0);
886 cnt++;
889 OHCI_DEBUG(printk("USB RM FUNCTION %d eds removed;\n", cnt);)
890 return cnt;
895 /******
896 *** TD handling functions
897 ************************************/
900 #define FILL_TD(INFO, DATA, LEN, LW0, LW1, TYPE, HANDLER) \
901 OHCI_ALLOC(td_pt, sizeof(*td_pt)); \
902 td_ret = (struct usb_ohci_td *) bus_to_virt(usb_ed->hwTailP & 0xfffffff0); \
903 td_pt1 = td_ret; \
904 td_pt1->ed = ed; \
905 td_pt1->buffer_start = (DATA); \
906 td_pt1->type = (TYPE); \
907 td_pt1->handler = (HANDLER); \
908 td_pt1->lw0 = (LW0); \
909 td_pt1->lw1 = (LW1); \
910 td_pt1->hwINFO = (INFO); \
911 td_pt1->hwCBP = (((DATA)==NULL)||((LEN)==0))?0:virt_to_bus(DATA); \
912 td_pt1->hwBE = (((DATA)==NULL)||((LEN)==0))?0:virt_to_bus((DATA) + (LEN) - 1); \
913 td_pt1->hwNextTD = virt_to_bus(td_pt); \
914 td_pt->hwNextTD = 0; \
915 usb_ed->hwTailP = td_pt1->hwNextTD
917 #define FILL_ISO_TD(INFO, DATA, LEN, LW0, LW1, TYPE, HANDLER) \
918 OHCI_ALLOC(td_pt, sizeof(*td_pt)); \
919 td_ret = (struct usb_ohci_td *) bus_to_virt(usb_ed->hwTailP & 0xfffffff0); \
920 td_pt1 = td_ret; \
921 td_pt1->ed = ed; \
922 td_pt1->buffer_start = (DATA); \
923 td_pt1->type = (TYPE); \
924 td_pt1->handler = (HANDLER); \
925 td_pt1->lw0 = (LW0); \
926 td_pt1->lw1 = (LW1); \
927 td_pt1->hwINFO = (INFO); \
928 td_pt1->hwCBP = (((DATA)==NULL)||((LEN)==0))?0:(virt_to_bus(DATA) & 0xfffff000); \
929 td_pt1->hwBE = (((DATA)==NULL)||((LEN)==0))?0:virt_to_bus((DATA) + (LEN) - 1); \
930 td_pt1->hwNextTD = virt_to_bus(td_pt); \
931 td_pt1->hwPSW[0] = (virt_to_bus(DATA) & 0xfff) | 0xe000; \
932 td_pt->hwNextTD = 0; \
933 usb_ed->hwTailP = td_pt1->hwNextTD
936 spinlock_t usb_req_lock = SPIN_LOCK_UNLOCKED;
938 struct usb_ohci_td * ohci_trans_req(struct ohci * ohci, struct usb_ohci_ed * ed, int ctrl_len,
939 void *ctrl, void * data, int data_len, __OHCI_BAG lw0, __OHCI_BAG lw1, unsigned int ed_type, f_handler handler) {
942 unsigned int flags;
943 volatile struct usb_ohci_ed *usb_ed = ed;
944 volatile struct usb_ohci_td *td_pt;
945 volatile struct usb_ohci_td *td_pt1 = NULL;
947 struct usb_ohci_td *td_ret = NULL;
949 int cnt = 0;
952 if(usb_ed == NULL ) return NULL; /* not known ep */
955 spin_lock_irqsave(&usb_req_lock, flags);
957 switch(ed_type) {
958 case BULK_IN:
959 while(data_len > 4096)
961 FILL_TD( TD_CC | TD_DP_IN | TD_T_TOGGLE, data, 4096, NULL, NULL, BULK_IN | ADD_LEN|(cnt++?0:ST_ADDR), NULL);
962 data += 4096; data_len -= 4096;
964 FILL_TD( TD_CC | TD_R | TD_DP_IN | TD_T_TOGGLE, data, data_len, lw0, lw1, BULK_IN |ADD_LEN|SEND|(cnt++?0:ST_ADDR), handler);
965 writel( OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
966 break;
968 case BULK_OUT:
969 while(data_len > 4096)
971 FILL_TD( TD_CC | TD_DP_OUT | TD_T_TOGGLE, data, 4096, NULL, NULL, BULK_OUT | ADD_LEN|(cnt++?0:ST_ADDR), NULL);
972 data += 4096; data_len -= 4096;
974 FILL_TD( TD_CC | TD_DP_OUT | TD_T_TOGGLE, data, data_len, lw0, lw1, BULK_OUT |ADD_LEN|SEND|(cnt++?0:ST_ADDR), handler);
975 writel( OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
976 break;
978 case INT_IN:
979 FILL_TD( TD_CC | TD_R | TD_DP_IN | TD_T_TOGGLE, data, data_len, lw0, lw1, INT_IN | ST_ADDR|ADD_LEN|SEND, handler);
980 break;
982 case INT_OUT:
983 FILL_TD( TD_CC | TD_DP_OUT | TD_T_TOGGLE, data, data_len, lw0, lw1, INT_OUT | ST_ADDR|ADD_LEN|SEND, handler);
984 break;
986 case CTRL_IN:
987 FILL_TD( TD_CC | TD_DP_SETUP | TD_T_DATA0, ctrl, ctrl_len, 0, 0, CTRL_SETUP |ST_ADDR, NULL);
988 if(data_len > 0) {
989 FILL_TD( TD_CC | TD_R | TD_DP_IN | TD_T_DATA1, data, data_len, 0, 0, CTRL_DATA_IN | ST_ADDR|ADD_LEN, NULL);
991 FILL_TD( TD_CC | TD_DP_OUT | TD_T_DATA1, NULL, 0, lw0, lw1, CTRL_STATUS_OUT |SEND, handler);
992 writel( OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
993 break;
995 case CTRL_OUT:
996 FILL_TD( TD_CC | TD_DP_SETUP | TD_T_DATA0, ctrl, ctrl_len, 0, 0, CTRL_SETUP |ST_ADDR, NULL);
997 if(data_len > 0) {
998 FILL_TD( TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1, data, data_len, 0, 0, CTRL_DATA_OUT | ST_ADDR|ADD_LEN, NULL);
1000 FILL_TD( TD_CC | TD_DP_IN | TD_T_DATA1, NULL, 0, lw0, lw1, CTRL_STATUS_IN |SEND, handler);
1001 writel( OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
1002 break;
1004 case ISO_IN:
1005 FILL_ISO_TD( TD_CC|TD_ISO|(ctrl_len & 0xffff), data, data_len, lw0, lw1, ISO_IN | ST_ADDR|ADD_LEN|SEND, handler);
1006 break;
1008 case ISO_OUT:
1009 FILL_ISO_TD( TD_CC|TD_ISO|(ctrl_len & 0xffff), data, data_len, lw0, lw1, ISO_OUT | ST_ADDR|ADD_LEN|SEND, handler);
1010 break;
1013 spin_unlock_irqrestore(&usb_req_lock, flags);
1014 return td_ret;
1018 /******
1019 *** Done List handling functions
1020 ************************************/
1021 /* the HC halts an ED if an error occurs;
1022 * on error move all pending tds of a transaction (from ed) onto the done list
1023 * * */
1024 static struct usb_ohci_td * ohci_append_error_tds(struct usb_ohci_td * td_list, struct usb_ohci_td * td_rev) {
1026 struct usb_ohci_td * tdl;
1027 struct usb_ohci_td * tdx;
1028 struct usb_ohci_td * tdt;
1029 int cc;
1031 tdl = (struct usb_ohci_td *) bus_to_virt( td_list->ed->hwHeadP & 0xfffffff0);
1032 tdt = (struct usb_ohci_td *) bus_to_virt( td_list->ed->hwTailP);
1033 cc = TD_CC_GET(td_list->hwINFO);
1035 for ( tdx = tdl; tdx != tdt; tdx = tdx->next_dl_td) {
1036 if(tdx->type & SEND) break;
1037 tdx->next_dl_td = bus_to_virt(tdx->hwNextTD & 0xfffffff0);
1039 tdx->next_dl_td = td_rev;
1040 td_list->ed->hwHeadP = (tdx->hwNextTD & 0xfffffff0) | (td_list->ed->hwHeadP & 0x2);
1041 TD_CC_SET(tdx->hwINFO, cc);
1042 return tdl;
1045 /* replies to the request have to be on a FIFO basis so
1046 * we reverse the reversed done-list */
1048 static struct usb_ohci_td * ohci_reverse_done_list(struct ohci * ohci) {
1050 __u32 td_list_hc;
1051 struct usb_ohci_td * td_rev = NULL;
1052 struct usb_ohci_td * td_list = NULL;
1054 td_list_hc = ohci->hc_area->hcca.done_head & 0xfffffff0;
1055 ohci->hc_area->hcca.done_head = 0;
1058 while(td_list_hc) {
1060 td_list = (struct usb_ohci_td *) bus_to_virt(td_list_hc);
1061 if(TD_CC_GET(td_list->hwINFO) && !(td_list->type & SEND))
1062 td_list->next_dl_td = ohci_append_error_tds(td_list, td_rev);
1063 else
1064 td_list->next_dl_td = td_rev;
1066 td_rev = td_list;
1067 td_list_hc = td_list->hwNextTD & 0xfffffff0;
1071 return td_list;
1074 /* there are some pending requests to remove some of the eds
1075 * we either process every td including the dummy td of these eds
1076 * or just those marked with TD->type&DEL
1077 * and link them to a list
1078 * */
1079 static struct usb_ohci_td * usb_ohci_del_list(struct ohci * ohci) {
1080 struct usb_ohci_ed * ed;
1081 struct usb_ohci_td * td;
1082 struct usb_ohci_td * td_tmp = NULL;
1083 struct usb_ohci_td * td_list = NULL;
1084 __u32 * td_hw;
1086 for(ed = ohci->ed_rm_list; ed != NULL; ed = ed->ed_prev) {
1087 OHCI_DEBUG(printk("USB HC ed_rm_list: %4x :\n", ed->hwINFO);)
1090 for( td_hw = &(ed->hwHeadP); (*td_hw & 0xfffffff0) != ed->hwTailP; td_hw = &(td->hwNextTD)) {
1091 td = (struct usb_ohci_td *)bus_to_virt(*td_hw & 0xfffffff0);
1092 if((ED_STATE(ed) == ED_DEL) || (td->type & DEL)) {
1093 *td_hw = td->hwNextTD;
1094 if(td_list == NULL)
1095 td_list = td;
1096 else
1097 td_tmp->next_dl_td = td;
1098 td_tmp = td;
1099 OHCI_DEBUG(printk("USB HC ed_rm_list: td: %4x\n", (unsigned int) td);)
1100 td->next_dl_td = NULL;
1103 if(ED_STATE(ed) == ED_DEL) { /* send dummy td */
1104 td = (struct usb_ohci_td *)bus_to_virt(*td_hw & 0xfffffff0);
1105 td->ed = ed;
1106 if(td_list == NULL)
1107 td_list = td;
1108 else
1109 td_tmp->next_dl_td = td;
1110 td_tmp = td;
1111 td->type |= DEL_ED;
1112 OHCI_DEBUG(printk("USB HC ed_rm_list: dummy (ED_DEL) td: %4x\n", (unsigned int) td);)
1113 ED_setSTATE(td->ed, ED_DEL| ED_NEW);
1114 td->next_dl_td = NULL;
1116 if(ED_TYPE(ed) == CTRL) writel(0, &ohci->regs->ed_controlcurrent); /* reset CTRL list */
1117 if(ED_TYPE(ed) == BULK) writel(0, &ohci->regs->ed_bulkcurrent); /* reset BULK list */
1121 writel_set((0x03<<4), &ohci->regs->control); /* start CTRL u. BULK list */
1123 ohci->ed_rm_list = NULL;
1126 return td_list;
1130 /* all tds ever alive go through this loop
1131 * requests are replied here
1132 * the handler is the
1133 * interface handler (blocking/non blocking) the real reply-handler
1134 * is called in the non blocking interface routine
1135 * */
1136 static int usb_ohci_done_list(struct ohci * ohci, struct usb_ohci_td * td_list) {
1138 struct usb_ohci_td * td_list_next = NULL;
1140 int cc;
1141 int i;
1143 while(td_list) {
1144 td_list_next = td_list->next_dl_td;
1146 if(td_list->type & ST_ADDR) { /* remember start address of data buffer */
1147 td_list->ed->buffer_start = td_list->buffer_start;
1148 td_list->ed->len = 0;
1151 if(td_list->type & ADD_LEN) { /* accumulate length of multi td transfers */
1152 if(td_list->hwINFO & TD_ISO) {
1153 for(i= 0; i <= ((td_list->hwINFO >> 24) & 0x7); i++)
1154 if((td_list->hwPSW[i] >> 12) < 0xE) td_list->ed->len += (td_list->hwPSW[i] & 0x3ff);
1156 else {
1157 if(td_list->hwBE != 0) {
1158 if(td_list->hwCBP == 0)
1159 td_list->ed->len += (bus_to_virt(td_list->hwBE) - td_list->buffer_start + 1);
1160 else
1161 td_list->ed->len += (bus_to_virt(td_list->hwCBP) - td_list->buffer_start);
1165 /* error code of transfer */
1166 cc = (ED_STATE(td_list->ed) == ED_DEL || (td_list->type & DEL))? USB_ST_REMOVED : (USB_ST_CRC*TD_CC_GET(td_list->hwINFO));
1168 if(td_list->type & DEL_ED) ED_setSTATE(td_list->ed, ED_NEW); /* remove ed */
1170 if((td_list->type & SEND) && (ED_STATE(td_list->ed) != ED_STOP) && (td_list->handler)) { /* send the reply */
1171 td_list->handler((void *) ohci,
1172 td_list->ed,
1173 td_list->ed->buffer_start,
1174 td_list->ed->len,
1176 td_list->lw0,
1177 td_list->lw1);
1178 OHCI_DEBUG(if(cc != TD_CC_NOERROR) printk("******* USB BUS error %x @ep %x\n", TD_CC_GET(td_list->hwINFO), td_list->ed->hwINFO);)
1181 if(ED_STATE(td_list->ed) == ED_TD_DEL) td_list->ed->hwINFO &= ~OHCI_ED_SKIP;
1183 if(((td_list->ed->hwHeadP & 0xfffffff0) == td_list->ed->hwTailP) && (ED_STATE(td_list->ed) > ED_UNLINK))
1184 ohci_unlink_ed(ohci, td_list->ed); /* unlink eds if they are not busy */
1186 OHCI_FREE(td_list);
1187 td_list = td_list_next;
1189 return 0;
1194 /******
1195 *** HC functions
1196 ************************************/
1199 /* reset the HC not the BUS */
1200 void reset_hc(struct ohci *ohci) {
1202 int timeout = 30;
1203 int smm_timeout = 50; /* 0,5 sec */
1205 if(readl(&ohci->regs->control) & 0x100) { /* SMM owns the HC */
1206 writel(0x08, &ohci->regs->cmdstatus); /* request ownership */
1207 printk("USB HC TakeOver from SMM\n");
1208 while(readl(&ohci->regs->control) & 0x100) {
1209 wait_ms(10);
1210 if (--smm_timeout == 0) {
1211 printk("USB HC TakeOver failed!\n");
1212 break;
1217 writel((1<<31), &ohci->regs->intrdisable); /* Disable HC interrupts */
1218 OHCI_DEBUG(printk("USB HC reset_hc: %x ; \n", readl(&ohci->regs->control));)
1220 writel(1, &ohci->regs->cmdstatus); /* HC Reset */
1221 while ((readl(&ohci->regs->cmdstatus) & 0x01) != 0) { /* 10us Reset */
1222 if (--timeout == 0) {
1223 printk("USB HC reset timed out!\n");
1224 return;
1226 udelay(1);
1229 static struct ohci *__ohci;
1232 * Start an OHCI controller, set the BUS operational
1233 * set and enable interrupts
1234 * connect the virtual root hub
1235 * * */
1237 int start_hc(struct ohci *ohci)
1239 unsigned int mask;
1240 int fminterval;
1243 * Tell the controller where the control and bulk lists are
1244 * The lists are empty now.
1246 writel(0, &ohci->regs->ed_controlhead);
1247 writel(0, &ohci->regs->ed_bulkhead);
1249 writel(virt_to_bus(&ohci->hc_area->hcca), &ohci->regs->hcca); /* a reset clears this */
1251 writel((0xBF), &ohci->regs->control); /* USB Operational */
1253 fminterval = 0x2edf;
1254 writel(((fminterval)*9)/10, &ohci->regs->periodicstart);
1255 fminterval |= ((((fminterval -210) * 6)/7)<<16);
1256 writel(fminterval, &ohci->regs->fminterval);
1257 writel(0x628, &ohci->regs->lsthresh);
1258 OHCI_DEBUG(printk("USB HC fminterval: %x \n", readl( &(ohci->regs->fminterval) )); )
1259 OHCI_DEBUG(printk("USB HC periodicstart: %x \n", readl( &(ohci->regs->periodicstart) )); )
1260 OHCI_DEBUG(printk("USB HC lsthresh: %x \n", readl( &(ohci->regs->lsthresh) )); )
1261 OHCI_DEBUG(printk("USB HC control: %x\n", readl(&ohci->regs->control)); )
1262 OHCI_DEBUG(printk("USB HC roothubstata: %x \n", readl( &(ohci->regs->roothub.a) )); )
1263 OHCI_DEBUG(printk("USB HC roothubstatb: %x \n", readl( &(ohci->regs->roothub.b) )); )
1264 OHCI_DEBUG(printk("USB HC roothubstatu: %x \n", readl( &(ohci->regs->roothub.status) )); )
1265 OHCI_DEBUG(printk("USB HC roothubstat1: %x \n", readl( &(ohci->regs->roothub.portstatus[0]) )); )
1266 OHCI_DEBUG(printk("USB HC roothubstat2: %x \n", readl( &(ohci->regs->roothub.portstatus[1]) )); )
1267 /* Choose the interrupts we care about now, others later on demand */
1268 mask = OHCI_INTR_MIE | OHCI_INTR_WDH | OHCI_INTR_SO;
1270 writel(mask, &ohci->regs->intrenable);
1271 writel(mask, &ohci->regs->intrstatus);
1273 #ifdef VROOTHUB
1275 /* connect the virtual root hub */
1276 struct usb_device * usb_dev;
1277 struct ohci_device *dev;
1280 usb_dev = usb_alloc_dev(NULL, ohci->bus);
1281 if (!usb_dev) return -1;
1283 dev = usb_to_ohci(usb_dev);
1284 // usb_dev->bus = ohci->bus;
1285 ohci->bus->root_hub = usb_dev;
1286 dev->ohci = ohci;
1287 usb_connect(usb_dev);
1288 if(usb_new_device(usb_dev) != 0) {
1289 usb_free_dev(usb_dev);
1290 return -1;
1294 #endif
1295 return 0;
1298 /* an interrupt happens */
1299 static void ohci_interrupt(int irq, void *__ohci, struct pt_regs *r)
1301 struct ohci *ohci = __ohci;
1302 struct ohci_regs *regs = ohci->regs;
1303 int ints;
1305 if((ohci->hc_area->hcca.done_head != 0) && !(ohci->hc_area->hcca.done_head & 0x01)) {
1306 ints = OHCI_INTR_WDH;
1308 else {
1309 if((ints = (readl(&regs->intrstatus) & readl(&regs->intrenable))) == 0)
1310 return;
1313 OHCI_DEBUG(printk("USB HC interrupt: %x (%x) \n", ints, readl(&regs->intrstatus));)
1316 if(ints & OHCI_INTR_WDH) {
1317 writel(OHCI_INTR_WDH, &regs->intrdisable);
1318 usb_ohci_done_list(ohci, ohci_reverse_done_list(ohci));
1319 writel(OHCI_INTR_WDH, &regs->intrenable);
1322 if(ints & OHCI_INTR_SO) {
1323 printk("**** USB Schedule overrun ");
1324 writel(OHCI_INTR_SO, &regs->intrenable);
1327 if(ints & OHCI_INTR_SF) {
1328 writel(OHCI_INTR_SF, &regs->intrdisable);
1329 if(ohci->ed_rm_list != NULL) {
1330 usb_ohci_done_list(ohci, usb_ohci_del_list(ohci));
1333 writel(ints, &regs->intrstatus);
1334 writel(OHCI_INTR_MIE, &regs->intrenable);
1337 /* allocate OHCI */
1339 static struct ohci *alloc_ohci(void* mem_base)
1341 int i;
1342 struct ohci *ohci;
1343 struct ohci_hc_area *hc_area;
1344 struct usb_bus *bus;
1346 hc_area = (struct ohci_hc_area *) __get_free_pages(GFP_KERNEL, 1);
1347 if (!hc_area)
1348 return NULL;
1350 memset(hc_area, 0, sizeof(*hc_area));
1351 ohci = &hc_area->ohci;
1352 ohci->irq = -1;
1353 ohci->regs = mem_base;
1354 ohci->hc_area = hc_area;
1355 __ohci = ohci;
1357 * for load ballancing of the interrupt branches
1359 for (i = 0; i < NUM_INTS; i++) ohci->ohci_int_load[i] = 0;
1360 for (i = 0; i < NUM_INTS; i++) hc_area->hcca.int_table[i] = 0;
1363 * Store the end of control and bulk list eds. So, we know where we can add
1364 * elements to these lists.
1366 ohci->ed_isotail = NULL;
1367 ohci->ed_controltail = NULL;
1368 ohci->ed_bulktail = NULL;
1370 bus = usb_alloc_bus(&sohci_device_operations);
1371 if (!bus) {
1372 free_pages((unsigned int) ohci->hc_area, 1);
1373 return NULL;
1376 ohci->bus = bus;
1377 bus->hcpriv = (void *) ohci;
1379 return ohci;
1384 * De-allocate all resources..
1385 * */
1386 static void release_ohci(struct ohci *ohci)
1389 OHCI_DEBUG(printk("USB HC release ohci \n"););
1390 /* stop hc */
1394 /* disconnect all devices */
1395 if(ohci->bus->root_hub) usb_disconnect(&ohci->bus->root_hub);
1397 reset_hc(__ohci);
1398 writel(OHCI_USB_RESET, &ohci->regs->control);
1399 wait_ms(10);
1401 if (ohci->irq >= 0) {
1402 free_irq(ohci->irq, ohci);
1403 ohci->irq = -1;
1406 usb_deregister_bus(ohci->bus);
1407 usb_free_bus(ohci->bus);
1409 /* unmap the IO address space */
1410 iounmap(ohci->regs);
1412 free_pages((unsigned int) ohci->hc_area, 1);
1417 * Increment the module usage count, start the control thread and
1418 * return success.
1419 * */
1420 static int found_ohci(int irq, void* mem_base)
1422 struct ohci *ohci;
1423 OHCI_DEBUG(printk("USB HC found ohci: irq= %d membase= %x \n", irq, (int)mem_base);)
1425 ohci = alloc_ohci(mem_base);
1426 if (!ohci) {
1427 return -ENOMEM;
1430 reset_hc(ohci);
1431 writel(OHCI_USB_RESET, &ohci->regs->control);
1432 wait_ms(10);
1433 usb_register_bus(ohci->bus);
1435 if (request_irq(irq, ohci_interrupt, SA_SHIRQ, "ohci-usb", ohci) == 0) {
1436 ohci->irq = irq;
1437 start_hc(ohci);
1438 return 0;
1440 release_ohci(ohci);
1441 return -EBUSY;
1444 static int start_ohci(struct pci_dev *dev)
1446 unsigned int mem_base = dev->resource[0].start;
1448 mem_base = (unsigned int) ioremap_nocache(mem_base, 4096);
1450 if (!mem_base) {
1451 printk("Error mapping OHCI memory\n");
1452 return -EFAULT;
1454 return found_ohci(dev->irq, (void *) mem_base);
1458 #ifdef CONFIG_APM
1459 static int handle_apm_event(apm_event_t event)
1461 static int down = 0;
1463 switch (event) {
1464 case APM_SYS_SUSPEND:
1465 case APM_USER_SUSPEND:
1466 if (down) {
1467 printk(KERN_DEBUG "ohci: received extra suspend event\n");
1468 break;
1470 down = 1;
1471 break;
1472 case APM_NORMAL_RESUME:
1473 case APM_CRITICAL_RESUME:
1474 if (!down) {
1475 printk(KERN_DEBUG "ohci: received bogus resume event\n");
1476 break;
1478 down = 0;
1479 // start_hc(ohci);
1480 break;
1482 return 0;
1484 #endif
1486 #define PCI_CLASS_SERIAL_USB_OHCI 0x0C0310
1488 int ohci_hcd_init(void)
1490 struct pci_dev *dev = NULL;
1492 while((dev = pci_find_class(PCI_CLASS_SERIAL_USB_OHCI, dev))) {
1493 if (start_ohci(dev) < 0) return -ENODEV;
1496 #ifdef CONFIG_APM
1497 apm_register_callback(&handle_apm_event);
1498 #endif
1500 return 0;
1503 #ifdef MODULE
1504 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
1514 release_ohci(__ohci);
1516 #endif //MODULE