* better
[mascara-docs.git] / i386 / linux-2.3.21 / drivers / usb / ohci-hcd.c
blobeeace6d617aa65a8b48cd00fd9b3b589d2ce9c4b
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-hcd.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 int sohci_request_irq(struct usb_device *usb_dev, unsigned int pipe, usb_device_irq handler, int period, void *dev_id, void **handle)
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, handle);
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 *handle = ed;
203 return 0;
207 static int sohci_release_irq(struct usb_device *usb_dev, void * ed)
209 // struct usb_device *usb_dev = ((struct ohci_device *) ((unsigned int)ed & 0xfffff000))->usb;
210 struct ohci * ohci = usb_dev->bus->hcpriv;
212 OHCI_DEBUG( printk("USB HC ***** RM_IRQ>>>:%4x\n", (unsigned int) ed);)
214 if(ed == NULL) return 0;
216 #ifdef VROOTHUB
217 if(ed == ohci->rh.int_addr)
218 return root_hub_release_irq(usb_dev, ed);
219 #endif
220 ED_setSTATE((struct usb_ohci_ed *)ed, ED_STOP);
222 usb_ohci_rm_ep(usb_dev, (struct usb_ohci_ed *) ed, NULL, NULL, NULL, 0);
224 return 0;
227 static int sohci_control_msg(struct usb_device *usb_dev, unsigned int pipe, devrequest *cmd, void *data, int len, int timeout)
229 DECLARE_WAITQUEUE(wait, current);
230 struct ohci_state state = {0, TD_NOTACCESSED};
231 struct ohci * ohci = usb_dev->bus->hcpriv;
232 struct usb_hcd_ed hcd_ed;
233 struct usb_ohci_ed *ed;
235 #ifdef VROOTHUB
236 if(usb_pipedevice(pipe) == ohci->rh.devnum)
237 return root_hub_control_msg(usb_dev, pipe, cmd, data, len);
238 #endif
240 usb_pipe_to_hcd_ed(usb_dev, pipe, &hcd_ed);
241 hcd_ed.type = CTRL;
243 ed = usb_ohci_add_ep(usb_dev, &hcd_ed, 0, 1);
245 OHCI_DEBUG( { int i; printk("USB HC CTRL>>>: ed:%x-%x: ctrl(%d):", (unsigned int) ed, ed->hwINFO, 8);)
246 OHCI_DEBUG( for(i=0; i < 8; i++ ) printk(" %02x", ((__u8 *) cmd)[i]);)
247 OHCI_DEBUG( printk(" data(%d):", len);)
248 OHCI_DEBUG( for(i=0; i < len; i++ ) printk(" %02x", ((__u8 *) data)[i]);)
249 OHCI_DEBUG( printk("\n"); })
250 current->state = TASK_UNINTERRUPTIBLE;
252 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);
254 OHCI_DEBUG(printk("USB HC trans req ed %x: %x :", ed->hwINFO, (unsigned int ) ed); )
255 OHCI_DEBUG({ int i; for( i= 0; i<8 ;i++) printk(" %4x", ((unsigned int *) ed)[i]) ; printk("\n"); }; )
256 if (ED_STATE(ed) != ED_OPER) ohci_link_ed(ohci, ed);
257 schedule_timeout(timeout);
259 if(state.status == TD_NOTACCESSED) {
260 current->state = TASK_UNINTERRUPTIBLE;
261 usb_ohci_rm_ep(usb_dev, ed, sohci_blocking_handler, NULL, NULL, 0);
262 schedule();
263 state.status = USB_ST_TIMEOUT;
265 remove_wait_queue(&op_wakeup, &wait);
266 return state.status;
269 static int sohci_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, void *data, int len, unsigned long *rval, int timeout)
271 DECLARE_WAITQUEUE(wait, current);
272 struct ohci_state state = {0, TD_NOTACCESSED};
273 struct ohci * ohci = usb_dev->bus->hcpriv;
274 struct usb_hcd_ed hcd_ed;
275 struct usb_ohci_ed *ed;
277 usb_pipe_to_hcd_ed(usb_dev, pipe, &hcd_ed);
278 hcd_ed.type = BULK;
279 ed = usb_ohci_add_ep(usb_dev, &hcd_ed, 0, 1);
280 OHCI_DEBUG( { int i; printk("USB HC BULK>>>: %x: ", ed->hwINFO);)
281 OHCI_DEBUG( printk(" data(%d):", len);)
282 OHCI_DEBUG( for(i=0; i < len; i++ ) printk(" %02x", ((__u8 *) data)[i]);)
283 OHCI_DEBUG( printk("\n"); })
284 current->state = TASK_UNINTERRUPTIBLE;
286 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);
287 if (ED_STATE(ed) != ED_OPER) ohci_link_ed(ohci, ed);
289 schedule_timeout(timeout);
291 if(state.status == TD_NOTACCESSED) {
292 current->state = TASK_UNINTERRUPTIBLE;
293 usb_ohci_rm_ep(usb_dev, ed, sohci_blocking_handler, NULL, NULL, 0);
294 schedule();
295 state.status = USB_ST_TIMEOUT;
297 remove_wait_queue(&op_wakeup, &wait);
298 *rval = state.len;
299 return state.status;
302 static void * sohci_request_bulk(struct usb_device *usb_dev, unsigned int pipe, usb_device_irq handler, void *data, int len, void *dev_id)
304 struct ohci * ohci = usb_dev->bus->hcpriv;
305 struct usb_hcd_ed hcd_ed;
306 struct usb_ohci_ed * ed;
308 usb_pipe_to_hcd_ed(usb_dev, pipe, &hcd_ed);
309 hcd_ed.type = BULK;
310 ed = usb_ohci_add_ep(usb_dev, &hcd_ed, 0, 1);
312 OHCI_DEBUG( printk("USB HC BULK_RQ>>>: %x \n", ed->hwINFO);)
314 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);
315 if (ED_STATE(ed) != ED_OPER) ohci_link_ed(ohci, ed);
317 return ed;
320 static int sohci_terminate_bulk(struct usb_device *usb_dev, void * ed)
322 DECLARE_WAITQUEUE(wait, current);
324 OHCI_DEBUG( printk("USB HC TERM_BULK>>>:%4x\n", (unsigned int) ed);)
326 current->state = TASK_UNINTERRUPTIBLE;
327 usb_ohci_rm_ep(usb_dev, (struct usb_ohci_ed *) ed, sohci_blocking_handler, NULL, &wait, SEND);
328 schedule();
329 remove_wait_queue(&op_wakeup, &wait);
330 return 1;
333 static int sohci_alloc_dev(struct usb_device *usb_dev)
335 struct ohci_device *dev;
337 /* Allocate the OHCI_HCD device private data */
338 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
339 if (!dev)
340 return -1;
342 /* Initialize "dev" */
343 memset(dev, 0, sizeof(*dev));
345 usb_dev->hcpriv = dev;
346 dev->usb = usb_dev;
347 atomic_set(&dev->refcnt, 1);
349 if (usb_dev->parent)
350 dev->ohci = usb_to_ohci(usb_dev->parent)->ohci;
352 return 0;
355 static int sohci_free_dev(struct usb_device *usb_dev)
357 int cnt;
358 DECLARE_WAITQUEUE(wait, current);
359 struct ohci_device *dev = usb_to_ohci(usb_dev);
361 OHCI_DEBUG(printk("USB HC ***** free %x\n", usb_dev->devnum);)
363 if(usb_dev->devnum >= 0) {
364 current->state = TASK_UNINTERRUPTIBLE;
365 cnt = usb_ohci_rm_function(usb_dev, sohci_blocking_handler, NULL, &wait);
366 if(cnt > 0) {
367 schedule();
368 remove_wait_queue(&op_wakeup, &wait);
370 current->state = TASK_INTERRUPTIBLE;
373 if (atomic_dec_and_test(&dev->refcnt))
374 kfree(dev);
376 return 0;
382 * ISO Interface designed by Randy Dunlap
385 static int sohci_get_current_frame_number(struct usb_device *usb_dev) {
387 struct ohci * ohci = usb_dev->bus->hcpriv;
389 return readl(&ohci->regs->fmnumber) & 0xffff;
393 static int sohci_init_isoc(struct usb_device *usb_dev, unsigned int pipe, int frame_count, void *context, struct usb_isoc_desc **idp) {
395 struct usb_isoc_desc *id;
397 *idp = NULL;
399 id = kmalloc (sizeof (struct usb_isoc_desc) + (sizeof (struct isoc_frame_desc) * frame_count), GFP_KERNEL);
400 if(!id) return -ENOMEM;
401 memset (id, 0, sizeof (struct usb_isoc_desc) + (sizeof (struct isoc_frame_desc) * frame_count));
402 OHCI_DEBUG(printk("ISO alloc id: %p, size: %d\n", id, sizeof (struct usb_isoc_desc) + (sizeof (struct isoc_frame_desc) * frame_count));)
403 id->td = kmalloc (sizeof (void *) * frame_count, GFP_KERNEL);
404 if(!id->td) {
405 kfree (id);
406 return -ENOMEM;
408 memset (id->td, 0, sizeof (void *) * frame_count);
409 OHCI_DEBUG(printk("ISO alloc id->td: %p, size: %d\n", id->td, sizeof (void *) * frame_count);)
411 id->frame_count = frame_count;
412 id->frame_size = usb_maxpacket (usb_dev, pipe, usb_pipeout(pipe));
413 id->start_frame = -1;
414 id->end_frame = -1;
415 id->usb_dev = usb_dev;
416 id->pipe = pipe;
417 id->context = context;
419 *idp = id;
420 return 0;
423 void print_int_eds(struct ohci * ohci);
425 static int sohci_run_isoc(struct usb_isoc_desc *id, struct usb_isoc_desc *pr_id) {
427 struct ohci * ohci = id->usb_dev->bus->hcpriv;
428 struct usb_ohci_td ** tdp = (struct usb_ohci_td **) id->td;
429 struct usb_hcd_ed hcd_ed;
430 struct usb_ohci_ed * ed;
431 int ix, frlen;
432 unsigned char *bufptr;
434 if(pr_id)
435 id->start_frame = pr_id->end_frame + 1;
436 else {
437 switch(id->start_type) {
438 case START_ABSOLUTE:
439 break;
441 case START_RELATIVE:
442 if(id->start_frame < START_FRAME_FUDGE) id->start_frame = START_FRAME_FUDGE;
443 id->start_frame += sohci_get_current_frame_number(id->usb_dev);
444 break;
446 case START_ASAP:
447 id->start_frame = START_FRAME_FUDGE + sohci_get_current_frame_number(id->usb_dev);
448 break;
452 id->start_frame &= 0xffff;
453 id->end_frame = (id->start_frame + id->frame_count - 1) & 0xffff;
455 id->prev_completed_frame = 0;
456 id->cur_completed_frame = 0;
457 if (id->frame_spacing <= 0) id->frame_spacing = 1;
459 bufptr = id->data;
461 usb_pipe_to_hcd_ed(id->usb_dev, id->pipe, &hcd_ed);
462 hcd_ed.type = ISO;
463 ed = usb_ohci_add_ep(id->usb_dev, &hcd_ed, 1, 1);
464 OHCI_DEBUG( printk("USB HC ISO>>>: ed: %x-%x: (%d tds)\n", (unsigned int) ed, ed->hwINFO, id->frame_count);)
466 for (ix = 0; ix < id->frame_count; ix++) {
467 frlen = (id->frames[ix].frame_length > id->frame_size)? id->frame_size : id->frames[ix].frame_length;
468 printk("ISO run id->td: %p \n", &tdp[ix]);
469 tdp[ix] = ohci_trans_req(ohci, ed, id->start_frame + ix , NULL, bufptr, frlen, (__OHCI_BAG) ix, (__OHCI_BAG) id,
470 (usb_pipeout(id->pipe))?ISO_OUT:ISO_IN,
471 sohci_iso_handler);
472 bufptr += frlen;
475 if (ED_STATE(ed) != ED_OPER) ohci_link_ed(ohci, ed);
476 print_int_eds(ohci);
477 return 0;
480 static int sohci_kill_isoc(struct usb_isoc_desc *id) {
482 struct usb_ohci_ed *ed = NULL;
483 struct usb_ohci_td **td = id->td;
484 int i;
485 printk("KILL_ISOC***:\n");
486 for (i = 0; i < id->frame_count; i++) {
487 if(td[i]) {
488 td[i]->type |= DEL;
489 ed = td[i]->ed; printk(" %d", i);
492 if(ed) usb_ohci_rm_ep(id->usb_dev, ed, NULL, NULL, NULL, TD_RM);
493 printk(": end KILL_ISOC***: %p\n", ed);
494 id->start_frame = -1;
495 return 0;
499 static void sohci_free_isoc(struct usb_isoc_desc *id) {
500 printk("FREE_ISOC***\n");
501 wait_ms(2000);
502 if(id->start_frame >= 0) sohci_kill_isoc(id);
503 printk("FREE_ISOC2***\n");
504 wait_ms(2000);
505 kfree(id->td);
506 kfree(id);
507 printk("FREE_ISOC3***\n");
508 wait_ms(2000);
511 struct usb_operations sohci_device_operations = {
512 sohci_alloc_dev,
513 sohci_free_dev,
514 sohci_control_msg,
515 sohci_bulk_msg,
516 sohci_request_irq,
517 sohci_release_irq,
518 sohci_request_bulk,
519 sohci_terminate_bulk,
520 sohci_get_current_frame_number,
521 sohci_init_isoc,
522 sohci_free_isoc,
523 sohci_run_isoc,
524 sohci_kill_isoc
528 /******
529 *** ED handling functions
530 ************************************/
532 /* just for debugging; prints all 32 branches of the int ed tree inclusive iso eds*/
533 void print_int_eds(struct ohci * ohci) {int i; __u32 * ed_p;
534 for(i= 0; i < 32; i++) {
535 OHCI_DEBUG(printk("branch int %2d(%2x): ", i,i); )
536 ed_p = &(ohci->hc_area->hcca.int_table[i]);
537 while (*ed_p != 0) {
538 OHCI_DEBUG(printk("ed: %4x; ", (((struct usb_ohci_ed *)bus_to_virt(*ed_p))->hwINFO));)
539 ed_p = &(((struct usb_ohci_ed *)bus_to_virt(*ed_p))->hwNextED);
541 OHCI_DEBUG(printk("\n");)
546 * search for the right branch to insert an interrupt ed into the int tree
547 * do some load ballancing
548 * returns the branch and
549 * sets the interval to interval = 2^integer(ld(interval))
550 * */
552 static int usb_ohci_int_ballance(struct ohci * ohci, int * interval, int load) {
554 int i,j;
556 j = 0; /* search for the least loaded interrupt endpoint branch of all 32 branches */
557 for(i=0; i< 32; i++) if(ohci->ohci_int_load[j] > ohci->ohci_int_load[i]) j=i;
559 for(i= 0; ((*interval >> i) > 1 ) && (i < 5); i++ ); /* interval = 2^int(ld(interval)) */
560 *interval = 1 << i;
562 j = j % (*interval);
563 for(i=j; i< 32; i+=(*interval)) ohci->ohci_int_load[i] += load;
566 OHCI_DEBUG(printk("USB HC new int ed on pos %d of interval %d \n",j, *interval);)
568 return j;
571 /* the int tree is a binary tree
572 * in order to process it sequentially the indexes of the branches have to be mapped
573 * the mapping reverses the bits of a word of num_bits length
574 * */
575 static int rev(int num_bits, int word) {
576 int i;
577 int wout = 0;
579 for(i = 0; i < num_bits; i++) wout |= (((word >> i) & 1) << (num_bits - i - 1));
580 return wout;
583 /* get the ed from the endpoint / usb_device address */
585 struct usb_ohci_ed * ohci_find_ep(struct usb_device *usb_dev, struct usb_hcd_ed *hcd_ed) {
586 return &(usb_to_ohci(usb_dev)->ed[(hcd_ed->endpoint << 1) | ((hcd_ed->type == CTRL)? 0:hcd_ed->out)]);
589 /* link an ed into one of the HC chains */
590 static int ohci_link_ed(struct ohci * ohci, struct usb_ohci_ed *ed) {
592 int int_branch;
593 int i;
594 int inter;
595 int interval;
596 int load;
597 __u32 * ed_p;
599 ED_setSTATE(ed, ED_OPER);
601 switch(ED_TYPE(ed)) {
602 case CTRL:
603 ed->hwNextED = 0;
604 if(ohci->ed_controltail == NULL) {
605 writel(virt_to_bus(ed), &ohci->regs->ed_controlhead);
607 else {
608 ohci->ed_controltail->hwNextED = virt_to_bus(ed);
610 ed->ed_prev = ohci->ed_controltail;
611 ohci->ed_controltail = ed;
612 break;
614 case BULK:
615 ed->hwNextED = 0;
616 if(ohci->ed_bulktail == NULL) {
617 writel(virt_to_bus(ed), &ohci->regs->ed_bulkhead);
619 else {
620 ohci->ed_bulktail->hwNextED = virt_to_bus(ed);
622 ed->ed_prev = ohci->ed_bulktail;
623 ohci->ed_bulktail = ed;
624 break;
626 case INT:
627 interval = ed->int_period;
628 load = ed->int_load;
629 int_branch = usb_ohci_int_ballance(ohci, &interval, load);
630 ed->int_interval = interval;
631 ed->int_branch = int_branch;
633 for( i= 0; i < rev(6, interval); i += inter) {
634 inter = 1;
635 for(ed_p = &(ohci->hc_area->hcca.int_table[rev(5,i)+int_branch]);
636 (*ed_p != 0) && (((struct usb_ohci_ed *)bus_to_virt(*ed_p))->int_interval >= interval);
637 ed_p = &(((struct usb_ohci_ed *)bus_to_virt(*ed_p))->hwNextED))
638 inter = rev(6,((struct usb_ohci_ed *)bus_to_virt(*ed_p))->int_interval);
639 ed->hwNextED = *ed_p;
640 *ed_p = virt_to_bus(ed);
641 OHCI_DEBUG(printk("int_link i: %2x, inter: %2x, ed: %4x\n", i, inter, ed->hwINFO);)
643 break;
645 case ISO:
646 if(ohci->ed_isotail != NULL) {
647 ohci->ed_isotail->hwNextED = virt_to_bus(ed);
648 ed->ed_prev = ohci->ed_isotail;
650 else {
651 for( i= 0; i < 32; i += inter) {
652 inter = 1;
653 for(ed_p = &(ohci->hc_area->hcca.int_table[rev(5,i)]);
654 *ed_p != 0;
655 ed_p = &(((struct usb_ohci_ed *)bus_to_virt(*ed_p))->hwNextED))
656 inter = rev(6,((struct usb_ohci_ed *)bus_to_virt(*ed_p))->int_interval);
657 *ed_p = virt_to_bus(ed);
659 ed->ed_prev = NULL;
661 ed->hwNextED = 0;
662 ohci->ed_isotail = ed;
663 break;
667 return 1;
670 /* unlink an ed from one of the HC chains.
671 * just the link to the ed is unlinked.
672 * the link from the ed still points to another operational ed or 0
673 * so the HC can eventually finish the processing of the unlinked ed
674 * */
675 static int ohci_unlink_ed(struct ohci * ohci, struct usb_ohci_ed *ed) {
677 int int_branch;
678 int i;
679 int inter;
680 int interval;
681 __u32 * ed_p;
684 switch(ED_TYPE(ed)) {
685 case CTRL:
686 if(ed->ed_prev == NULL) {
687 writel(ed->hwNextED, &ohci->regs->ed_controlhead);
689 else {
690 ed->ed_prev->hwNextED = ed->hwNextED;
692 if(ohci->ed_controltail == ed) {
693 ohci->ed_controltail = ed->ed_prev;
695 else {
696 ((struct usb_ohci_ed *)bus_to_virt(ed->hwNextED))->ed_prev = ed->ed_prev;
698 break;
700 case BULK:
701 if(ed->ed_prev == NULL) {
702 writel(ed->hwNextED, &ohci->regs->ed_bulkhead);
704 else {
705 ed->ed_prev->hwNextED = ed->hwNextED;
707 if(ohci->ed_bulktail == ed) {
708 ohci->ed_bulktail = ed->ed_prev;
710 else {
711 ((struct usb_ohci_ed *)bus_to_virt(ed->hwNextED))->ed_prev = ed->ed_prev;
713 break;
715 case INT:
716 int_branch = ed->int_branch;
717 interval = ed->int_interval;
719 for( i= 0; i < rev(6,interval); i += inter) {
720 for(ed_p = &(ohci->hc_area->hcca.int_table[rev(5,i)+int_branch]), inter = 1;
721 (*ed_p != 0) && (*ed_p != ed->hwNextED);
722 ed_p = &(((struct usb_ohci_ed *)bus_to_virt(*ed_p))->hwNextED),
723 inter = rev(6, ((struct usb_ohci_ed *)bus_to_virt(*ed_p))->int_interval)) {
724 if(((struct usb_ohci_ed *)bus_to_virt(*ed_p)) == ed) {
725 *ed_p = ed->hwNextED;
726 break;
729 OHCI_DEBUG(printk("int_link i: %2x, inter: %2x, ed: %4x\n", i, inter, ed->hwINFO);)
731 for(i=int_branch; i< 32; i+=interval) ohci->ohci_int_load[i] -= ed->int_load;
732 break;
734 case ISO:
735 if(ohci->ed_isotail == ed)
736 ohci->ed_isotail = ed->ed_prev;
737 if(ed->hwNextED != 0)
738 ((struct usb_ohci_ed *)bus_to_virt(ed->hwNextED))->ed_prev = ed->ed_prev;
740 if(ed->ed_prev != NULL) {
741 ed->ed_prev->hwNextED = ed->hwNextED;
743 else {
744 for( i= 0; i < 32; i += inter) {
745 inter = 1;
746 for(ed_p = &(ohci->hc_area->hcca.int_table[rev(5,i)]);
747 *ed_p != 0;
748 ed_p = &(((struct usb_ohci_ed *)bus_to_virt(*ed_p))->hwNextED)) {
749 inter = rev(6,((struct usb_ohci_ed *)bus_to_virt(*ed_p))->int_interval);
750 if(((struct usb_ohci_ed *)bus_to_virt(*ed_p)) == ed) {
751 *ed_p = ed->hwNextED;
752 break;
757 break;
759 ED_setSTATE(ed, ED_UNLINK);
760 return 1;
763 spinlock_t usb_ed_lock = SPIN_LOCK_UNLOCKED;
765 /* add/reinit an endpoint; this should be done once at the usb_set_configuration command,
766 * but the USB stack is a little bit stateless so we do it at every transaction
767 * if the state of the ed is ED_NEW then a dummy td is added and the state is changed to ED_UNLINK
768 * in all other cases the state is left unchanged
769 * the ed info fields are setted anyway even though they should not change
770 * */
771 struct usb_ohci_ed *usb_ohci_add_ep(struct usb_device * usb_dev, struct usb_hcd_ed * hcd_ed, int interval, int load) {
773 // struct ohci * ohci = usb_dev->bus->hcpriv;
774 struct usb_ohci_td * td;
775 struct usb_ohci_ed * ed, *ed1;
777 int ed_state, ed_state1;
779 spin_lock(&usb_ed_lock);
781 ed = ohci_find_ep(usb_dev, hcd_ed);
784 ed1 = ((void *) ed) + 0x40; ed_state1 = ED_STATE(ed1);
785 OHCI_DEBUG(printk("++++ USB HC add 60 ed1 %x: %x :state: %x\n", ed1->hwINFO, (unsigned int ) ed1, ed_state1); )
786 ed_state = ED_STATE(ed); /* store state of ed */
787 OHCI_DEBUG(printk("USB HC add ed %x: %x :state: %x\n", ed->hwINFO, (unsigned int ) ed, ed_state); )
788 if (ed_state == ED_NEW) {
789 OHCI_ALLOC(td, sizeof(*td)); /* dummy td; end of td list for ed */
790 ed->hwTailP = virt_to_bus(td);
791 ed->hwHeadP = ed->hwTailP;
792 ed_state = ED_UNLINK;
795 ed->hwINFO = hcd_ed->function
796 | hcd_ed->endpoint << 7
797 | (hcd_ed->type == ISO? 0x8000 : 0)
798 | (hcd_ed->type == CTRL? 0:(hcd_ed->out == 1? 0x800 : 0x1000 ))
799 | hcd_ed->slow << 13
800 | hcd_ed->maxpack << 16
801 | hcd_ed->type << 27
802 // | 1 << 14
803 | ed_state << 29 ;
805 if (ED_TYPE(ed) == INT && ed_state == ED_UNLINK) {
806 ed->int_period = interval;
807 ed->int_load = load;
810 spin_unlock(&usb_ed_lock);
811 return ed;
816 /*****
817 * Request the removal of an endpoint
819 * put the ep on the rm_list and request a stop of the bulk or ctrl list
820 * real removal is done at the next start of frame (SOF) hardware interrupt
821 * the dummy td carries the essential information (handler, proc queue, ...)
822 * if(send & TD_RM) then just the TD witch have (TD->type & DEL) set will be removed
823 * otherwise all TDs including the dummy TD of the ED will be removed
825 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)
827 unsigned int flags;
829 struct ohci * ohci = usb_dev->bus->hcpriv;
830 struct usb_ohci_td *td;
832 OHCI_DEBUG(printk("USB HC remove ed %x: %x :\n", ed->hwINFO, (unsigned int ) ed); )
834 spin_lock_irqsave(&usb_ed_lock, flags);
835 ed->hwINFO |= OHCI_ED_SKIP;
836 writel( OHCI_INTR_SF, &ohci->regs->intrenable); /* enable sof interrupt */
839 if(send & TD_RM) { /* delete selected TDs */
840 ED_setSTATE(ed, ED_TD_DEL);
842 else { /* delete all TDS */
843 if(ED_STATE(ed) == ED_OPER) ohci_unlink_ed(ohci, ed);
844 td = (struct usb_ohci_td *) bus_to_virt(ed->hwTailP);
845 td->lw0 = lw0;
846 td->lw1 = lw1;
847 td->ed = ed;
848 td->hwINFO = TD_CC;
849 td->handler = handler;
850 td->type = send;
851 ED_setSTATE(ed, ED_DEL);
853 ed->ed_prev = ohci->ed_rm_list;
854 ohci->ed_rm_list = ed;
856 switch(ED_TYPE(ed)) {
857 case CTRL:
858 writel_mask(~(0x01<<4), &ohci->regs->control); /* stop CTRL list */
859 break;
860 case BULK:
861 writel_mask(~(0x01<<5), &ohci->regs->control); /* stop BULK list */
862 break;
865 spin_unlock_irqrestore(&usb_ed_lock, flags);
867 return 1;
871 /* remove all endpoints of a function (device)
872 * just the last ed sends a reply
873 * the last ed is ed0 as there always should be an ep0
874 * */
875 int usb_ohci_rm_function(struct usb_device * usb_dev, f_handler handler,__OHCI_BAG tw0, __OHCI_BAG tw1)
877 struct usb_ohci_ed *ed;
878 int i;
879 int cnt = 0;
882 for(i = NUM_EDS - 1 ; i >= 0; i--) {
883 ed = &(usb_to_ohci(usb_dev)->ed[i]);
884 if(ED_STATE(ed) != ED_NEW) {
885 OHCI_DEBUG(printk("USB RM FUNCTION ed: %4x;\n", ed->hwINFO);)
886 usb_ohci_rm_ep(usb_dev, ed, handler, tw0, tw1, i==0?SEND:0);
887 cnt++;
890 OHCI_DEBUG(printk("USB RM FUNCTION %d eds removed;\n", cnt);)
891 return cnt;
896 /******
897 *** TD handling functions
898 ************************************/
901 #define FILL_TD(INFO, DATA, LEN, LW0, LW1, TYPE, HANDLER) \
902 OHCI_ALLOC(td_pt, sizeof(*td_pt)); \
903 td_ret = (struct usb_ohci_td *) bus_to_virt(usb_ed->hwTailP & 0xfffffff0); \
904 td_pt1 = td_ret; \
905 td_pt1->ed = ed; \
906 td_pt1->buffer_start = (DATA); \
907 td_pt1->type = (TYPE); \
908 td_pt1->handler = (HANDLER); \
909 td_pt1->lw0 = (LW0); \
910 td_pt1->lw1 = (LW1); \
911 td_pt1->hwINFO = (INFO); \
912 td_pt1->hwCBP = (((DATA)==NULL)||((LEN)==0))?0:virt_to_bus(DATA); \
913 td_pt1->hwBE = (((DATA)==NULL)||((LEN)==0))?0:virt_to_bus((DATA) + (LEN) - 1); \
914 td_pt1->hwNextTD = virt_to_bus(td_pt); \
915 td_pt->hwNextTD = 0; \
916 usb_ed->hwTailP = td_pt1->hwNextTD
918 #define FILL_ISO_TD(INFO, DATA, LEN, LW0, LW1, TYPE, HANDLER) \
919 OHCI_ALLOC(td_pt, sizeof(*td_pt)); \
920 td_ret = (struct usb_ohci_td *) bus_to_virt(usb_ed->hwTailP & 0xfffffff0); \
921 td_pt1 = td_ret; \
922 td_pt1->ed = ed; \
923 td_pt1->buffer_start = (DATA); \
924 td_pt1->type = (TYPE); \
925 td_pt1->handler = (HANDLER); \
926 td_pt1->lw0 = (LW0); \
927 td_pt1->lw1 = (LW1); \
928 td_pt1->hwINFO = (INFO); \
929 td_pt1->hwCBP = (((DATA)==NULL)||((LEN)==0))?0:(virt_to_bus(DATA) & 0xfffff000); \
930 td_pt1->hwBE = (((DATA)==NULL)||((LEN)==0))?0:virt_to_bus((DATA) + (LEN) - 1); \
931 td_pt1->hwNextTD = virt_to_bus(td_pt); \
932 td_pt1->hwPSW[0] = (virt_to_bus(DATA) & 0xfff) | 0xe000; \
933 td_pt->hwNextTD = 0; \
934 usb_ed->hwTailP = td_pt1->hwNextTD
937 spinlock_t usb_req_lock = SPIN_LOCK_UNLOCKED;
939 struct usb_ohci_td * ohci_trans_req(struct ohci * ohci, struct usb_ohci_ed * ed, int ctrl_len,
940 void *ctrl, void * data, int data_len, __OHCI_BAG lw0, __OHCI_BAG lw1, unsigned int ed_type, f_handler handler) {
943 unsigned int flags;
944 volatile struct usb_ohci_ed *usb_ed = ed;
945 volatile struct usb_ohci_td *td_pt;
946 volatile struct usb_ohci_td *td_pt1 = NULL;
948 struct usb_ohci_td *td_ret = NULL;
950 int cnt = 0;
953 if(usb_ed == NULL ) return NULL; /* not known ep */
956 spin_lock_irqsave(&usb_req_lock, flags);
958 switch(ed_type) {
959 case BULK_IN:
960 while(data_len > 4096)
962 FILL_TD( TD_CC | TD_DP_IN | TD_T_TOGGLE, data, 4096, NULL, NULL, BULK_IN | ADD_LEN|(cnt++?0:ST_ADDR), NULL);
963 data += 4096; data_len -= 4096;
965 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);
966 writel( OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
967 break;
969 case BULK_OUT:
970 while(data_len > 4096)
972 FILL_TD( TD_CC | TD_DP_OUT | TD_T_TOGGLE, data, 4096, NULL, NULL, BULK_OUT | ADD_LEN|(cnt++?0:ST_ADDR), NULL);
973 data += 4096; data_len -= 4096;
975 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);
976 writel( OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
977 break;
979 case INT_IN:
980 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);
981 break;
983 case INT_OUT:
984 FILL_TD( TD_CC | TD_DP_OUT | TD_T_TOGGLE, data, data_len, lw0, lw1, INT_OUT | ST_ADDR|ADD_LEN|SEND, handler);
985 break;
987 case CTRL_IN:
988 FILL_TD( TD_CC | TD_DP_SETUP | TD_T_DATA0, ctrl, ctrl_len, 0, 0, CTRL_SETUP |ST_ADDR, NULL);
989 if(data_len > 0) {
990 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);
992 FILL_TD( TD_CC | TD_DP_OUT | TD_T_DATA1, NULL, 0, lw0, lw1, CTRL_STATUS_OUT |SEND, handler);
993 writel( OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
994 break;
996 case CTRL_OUT:
997 FILL_TD( TD_CC | TD_DP_SETUP | TD_T_DATA0, ctrl, ctrl_len, 0, 0, CTRL_SETUP |ST_ADDR, NULL);
998 if(data_len > 0) {
999 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);
1001 FILL_TD( TD_CC | TD_DP_IN | TD_T_DATA1, NULL, 0, lw0, lw1, CTRL_STATUS_IN |SEND, handler);
1002 writel( OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
1003 break;
1005 case ISO_IN:
1006 FILL_ISO_TD( TD_CC|TD_ISO|(ctrl_len & 0xffff), data, data_len, lw0, lw1, ISO_IN | ST_ADDR|ADD_LEN|SEND, handler);
1007 break;
1009 case ISO_OUT:
1010 FILL_ISO_TD( TD_CC|TD_ISO|(ctrl_len & 0xffff), data, data_len, lw0, lw1, ISO_OUT | ST_ADDR|ADD_LEN|SEND, handler);
1011 break;
1014 spin_unlock_irqrestore(&usb_req_lock, flags);
1015 return td_ret;
1019 /******
1020 *** Done List handling functions
1021 ************************************/
1022 /* the HC halts an ED if an error occurs;
1023 * on error move all pending tds of a transaction (from ed) onto the done list
1024 * * */
1025 static struct usb_ohci_td * ohci_append_error_tds(struct usb_ohci_td * td_list, struct usb_ohci_td * td_rev) {
1027 struct usb_ohci_td * tdl;
1028 struct usb_ohci_td * tdx;
1029 struct usb_ohci_td * tdt;
1030 int cc;
1032 tdl = (struct usb_ohci_td *) bus_to_virt( td_list->ed->hwHeadP & 0xfffffff0);
1033 tdt = (struct usb_ohci_td *) bus_to_virt( td_list->ed->hwTailP);
1034 cc = TD_CC_GET(td_list->hwINFO);
1036 for ( tdx = tdl; tdx != tdt; tdx = tdx->next_dl_td) {
1037 if(tdx->type & SEND) break;
1038 tdx->next_dl_td = bus_to_virt(tdx->hwNextTD & 0xfffffff0);
1040 tdx->next_dl_td = td_rev;
1041 td_list->ed->hwHeadP = (tdx->hwNextTD & 0xfffffff0) | (td_list->ed->hwHeadP & 0x2);
1042 TD_CC_SET(tdx->hwINFO, cc);
1043 return tdl;
1046 /* replies to the request have to be on a FIFO basis so
1047 * we reverse the reversed done-list */
1049 static struct usb_ohci_td * ohci_reverse_done_list(struct ohci * ohci) {
1051 __u32 td_list_hc;
1052 struct usb_ohci_td * td_rev = NULL;
1053 struct usb_ohci_td * td_list = NULL;
1055 td_list_hc = ohci->hc_area->hcca.done_head & 0xfffffff0;
1056 ohci->hc_area->hcca.done_head = 0;
1059 while(td_list_hc) {
1061 td_list = (struct usb_ohci_td *) bus_to_virt(td_list_hc);
1062 if(TD_CC_GET(td_list->hwINFO) && !(td_list->type & SEND))
1063 td_list->next_dl_td = ohci_append_error_tds(td_list, td_rev);
1064 else
1065 td_list->next_dl_td = td_rev;
1067 td_rev = td_list;
1068 td_list_hc = td_list->hwNextTD & 0xfffffff0;
1072 return td_list;
1075 /* there are some pending requests to remove some of the eds
1076 * we either process every td including the dummy td of these eds
1077 * or just those marked with TD->type&DEL
1078 * and link them to a list
1079 * */
1080 static struct usb_ohci_td * usb_ohci_del_list(struct ohci * ohci) {
1081 struct usb_ohci_ed * ed;
1082 struct usb_ohci_td * td;
1083 struct usb_ohci_td * td_tmp = NULL;
1084 struct usb_ohci_td * td_list = NULL;
1085 __u32 * td_hw;
1087 for(ed = ohci->ed_rm_list; ed != NULL; ed = ed->ed_prev) {
1088 OHCI_DEBUG(printk("USB HC ed_rm_list: %4x :\n", ed->hwINFO);)
1091 for( td_hw = &(ed->hwHeadP); (*td_hw & 0xfffffff0) != ed->hwTailP; td_hw = &(td->hwNextTD)) {
1092 td = (struct usb_ohci_td *)bus_to_virt(*td_hw & 0xfffffff0);
1093 if((ED_STATE(ed) == ED_DEL) || (td->type & DEL)) {
1094 *td_hw = td->hwNextTD;
1095 if(td_list == NULL)
1096 td_list = td;
1097 else
1098 td_tmp->next_dl_td = td;
1099 td_tmp = td;
1100 OHCI_DEBUG(printk("USB HC ed_rm_list: td: %4x\n", (unsigned int) td);)
1101 td->next_dl_td = NULL;
1104 if(ED_STATE(ed) == ED_DEL) { /* send dummy td */
1105 td = (struct usb_ohci_td *)bus_to_virt(*td_hw & 0xfffffff0);
1106 td->ed = ed;
1107 if(td_list == NULL)
1108 td_list = td;
1109 else
1110 td_tmp->next_dl_td = td;
1111 td_tmp = td;
1112 td->type |= DEL_ED;
1113 OHCI_DEBUG(printk("USB HC ed_rm_list: dummy (ED_DEL) td: %4x\n", (unsigned int) td);)
1114 ED_setSTATE(td->ed, ED_DEL| ED_NEW);
1115 td->next_dl_td = NULL;
1117 if(ED_TYPE(ed) == CTRL) writel(0, &ohci->regs->ed_controlcurrent); /* reset CTRL list */
1118 if(ED_TYPE(ed) == BULK) writel(0, &ohci->regs->ed_bulkcurrent); /* reset BULK list */
1122 writel_set((0x03<<4), &ohci->regs->control); /* start CTRL u. BULK list */
1124 ohci->ed_rm_list = NULL;
1127 return td_list;
1131 /* all tds ever alive go through this loop
1132 * requests are replied here
1133 * the handler is the
1134 * interface handler (blocking/non blocking) the real reply-handler
1135 * is called in the non blocking interface routine
1136 * */
1137 static int usb_ohci_done_list(struct ohci * ohci, struct usb_ohci_td * td_list) {
1139 struct usb_ohci_td * td_list_next = NULL;
1141 int cc;
1142 int i;
1144 while(td_list) {
1145 td_list_next = td_list->next_dl_td;
1147 if(td_list->type & ST_ADDR) { /* remember start address of data buffer */
1148 td_list->ed->buffer_start = td_list->buffer_start;
1149 td_list->ed->len = 0;
1152 if(td_list->type & ADD_LEN) { /* accumulate length of multi td transfers */
1153 if(td_list->hwINFO & TD_ISO) {
1154 for(i= 0; i <= ((td_list->hwINFO >> 24) & 0x7); i++)
1155 if((td_list->hwPSW[i] >> 12) < 0xE) td_list->ed->len += (td_list->hwPSW[i] & 0x3ff);
1157 else {
1158 if(td_list->hwBE != 0) {
1159 if(td_list->hwCBP == 0)
1160 td_list->ed->len += (bus_to_virt(td_list->hwBE) - td_list->buffer_start + 1);
1161 else
1162 td_list->ed->len += (bus_to_virt(td_list->hwCBP) - td_list->buffer_start);
1166 /* error code of transfer */
1167 cc = (ED_STATE(td_list->ed) == ED_DEL || (td_list->type & DEL))? USB_ST_REMOVED : (USB_ST_CRC*TD_CC_GET(td_list->hwINFO));
1169 if(td_list->type & DEL_ED) ED_setSTATE(td_list->ed, ED_NEW); /* remove ed */
1171 if((td_list->type & SEND) && (ED_STATE(td_list->ed) != ED_STOP) && (td_list->handler)) { /* send the reply */
1172 td_list->handler((void *) ohci,
1173 td_list->ed,
1174 td_list->ed->buffer_start,
1175 td_list->ed->len,
1177 td_list->lw0,
1178 td_list->lw1);
1179 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);)
1182 if(ED_STATE(td_list->ed) == ED_TD_DEL) td_list->ed->hwINFO &= ~OHCI_ED_SKIP;
1184 if(((td_list->ed->hwHeadP & 0xfffffff0) == td_list->ed->hwTailP) && (ED_STATE(td_list->ed) > ED_UNLINK))
1185 ohci_unlink_ed(ohci, td_list->ed); /* unlink eds if they are not busy */
1187 OHCI_FREE(td_list);
1188 td_list = td_list_next;
1190 return 0;
1195 /******
1196 *** HC functions
1197 ************************************/
1200 /* reset the HC not the BUS */
1201 void reset_hc(struct ohci *ohci) {
1203 int timeout = 30;
1204 int smm_timeout = 50; /* 0,5 sec */
1206 if(readl(&ohci->regs->control) & 0x100) { /* SMM owns the HC */
1207 writel(0x08, &ohci->regs->cmdstatus); /* request ownership */
1208 printk("USB HC TakeOver from SMM\n");
1209 while(readl(&ohci->regs->control) & 0x100) {
1210 wait_ms(10);
1211 if (--smm_timeout == 0) {
1212 printk("USB HC TakeOver failed!\n");
1213 break;
1218 writel((1<<31), &ohci->regs->intrdisable); /* Disable HC interrupts */
1219 OHCI_DEBUG(printk("USB HC reset_hc: %x ; \n", readl(&ohci->regs->control));)
1221 writel(1, &ohci->regs->cmdstatus); /* HC Reset */
1222 while ((readl(&ohci->regs->cmdstatus) & 0x01) != 0) { /* 10us Reset */
1223 if (--timeout == 0) {
1224 printk("USB HC reset timed out!\n");
1225 return;
1227 udelay(1);
1230 static struct ohci *__ohci;
1233 * Start an OHCI controller, set the BUS operational
1234 * set and enable interrupts
1235 * connect the virtual root hub
1236 * * */
1238 int start_hc(struct ohci *ohci)
1240 unsigned int mask;
1241 int fminterval;
1244 * Tell the controller where the control and bulk lists are
1245 * The lists are empty now.
1247 writel(0, &ohci->regs->ed_controlhead);
1248 writel(0, &ohci->regs->ed_bulkhead);
1250 writel(virt_to_bus(&ohci->hc_area->hcca), &ohci->regs->hcca); /* a reset clears this */
1252 writel((0xBF), &ohci->regs->control); /* USB Operational */
1254 fminterval = 0x2edf;
1255 writel(((fminterval)*9)/10, &ohci->regs->periodicstart);
1256 fminterval |= ((((fminterval -210) * 6)/7)<<16);
1257 writel(fminterval, &ohci->regs->fminterval);
1258 writel(0x628, &ohci->regs->lsthresh);
1259 OHCI_DEBUG(printk("USB HC fminterval: %x \n", readl( &(ohci->regs->fminterval) )); )
1260 OHCI_DEBUG(printk("USB HC periodicstart: %x \n", readl( &(ohci->regs->periodicstart) )); )
1261 OHCI_DEBUG(printk("USB HC lsthresh: %x \n", readl( &(ohci->regs->lsthresh) )); )
1262 OHCI_DEBUG(printk("USB HC control: %x\n", readl(&ohci->regs->control)); )
1263 OHCI_DEBUG(printk("USB HC roothubstata: %x \n", readl( &(ohci->regs->roothub.a) )); )
1264 OHCI_DEBUG(printk("USB HC roothubstatb: %x \n", readl( &(ohci->regs->roothub.b) )); )
1265 OHCI_DEBUG(printk("USB HC roothubstatu: %x \n", readl( &(ohci->regs->roothub.status) )); )
1266 OHCI_DEBUG(printk("USB HC roothubstat1: %x \n", readl( &(ohci->regs->roothub.portstatus[0]) )); )
1267 OHCI_DEBUG(printk("USB HC roothubstat2: %x \n", readl( &(ohci->regs->roothub.portstatus[1]) )); )
1268 /* Choose the interrupts we care about now, others later on demand */
1269 mask = OHCI_INTR_MIE | OHCI_INTR_WDH | OHCI_INTR_SO;
1271 writel(mask, &ohci->regs->intrenable);
1272 writel(mask, &ohci->regs->intrstatus);
1274 #ifdef VROOTHUB
1276 /* connect the virtual root hub */
1277 struct usb_device * usb_dev;
1278 struct ohci_device *dev;
1281 usb_dev = usb_alloc_dev(NULL, ohci->bus);
1282 if (!usb_dev) return -1;
1284 dev = usb_to_ohci(usb_dev);
1285 // usb_dev->bus = ohci->bus;
1286 ohci->bus->root_hub = usb_dev;
1287 dev->ohci = ohci;
1288 usb_connect(usb_dev);
1289 if(usb_new_device(usb_dev) != 0) {
1290 usb_free_dev(usb_dev);
1291 return -1;
1295 #endif
1296 return 0;
1299 /* an interrupt happens */
1300 static void ohci_interrupt(int irq, void *__ohci, struct pt_regs *r)
1302 struct ohci *ohci = __ohci;
1303 struct ohci_regs *regs = ohci->regs;
1304 int ints;
1306 if((ohci->hc_area->hcca.done_head != 0) && !(ohci->hc_area->hcca.done_head & 0x01)) {
1307 ints = OHCI_INTR_WDH;
1309 else {
1310 if((ints = (readl(&regs->intrstatus) & readl(&regs->intrenable))) == 0)
1311 return;
1314 OHCI_DEBUG(printk("USB HC interrupt: %x (%x) \n", ints, readl(&regs->intrstatus));)
1317 if(ints & OHCI_INTR_WDH) {
1318 writel(OHCI_INTR_WDH, &regs->intrdisable);
1319 usb_ohci_done_list(ohci, ohci_reverse_done_list(ohci));
1320 writel(OHCI_INTR_WDH, &regs->intrenable);
1323 if(ints & OHCI_INTR_SO) {
1324 printk("**** USB Schedule overrun ");
1325 writel(OHCI_INTR_SO, &regs->intrenable);
1328 if(ints & OHCI_INTR_SF) {
1329 writel(OHCI_INTR_SF, &regs->intrdisable);
1330 if(ohci->ed_rm_list != NULL) {
1331 usb_ohci_done_list(ohci, usb_ohci_del_list(ohci));
1334 writel(ints, &regs->intrstatus);
1335 writel(OHCI_INTR_MIE, &regs->intrenable);
1338 /* allocate OHCI */
1340 static struct ohci *alloc_ohci(void* mem_base)
1342 int i;
1343 struct ohci *ohci;
1344 struct ohci_hc_area *hc_area;
1345 struct usb_bus *bus;
1347 hc_area = (struct ohci_hc_area *) __get_free_pages(GFP_KERNEL, 1);
1348 if (!hc_area)
1349 return NULL;
1351 memset(hc_area, 0, sizeof(*hc_area));
1352 ohci = &hc_area->ohci;
1353 ohci->irq = -1;
1354 ohci->regs = mem_base;
1355 ohci->hc_area = hc_area;
1356 __ohci = ohci;
1358 * for load ballancing of the interrupt branches
1360 for (i = 0; i < NUM_INTS; i++) ohci->ohci_int_load[i] = 0;
1361 for (i = 0; i < NUM_INTS; i++) hc_area->hcca.int_table[i] = 0;
1364 * Store the end of control and bulk list eds. So, we know where we can add
1365 * elements to these lists.
1367 ohci->ed_isotail = NULL;
1368 ohci->ed_controltail = NULL;
1369 ohci->ed_bulktail = NULL;
1371 bus = usb_alloc_bus(&sohci_device_operations);
1372 if (!bus) {
1373 free_pages((unsigned int) ohci->hc_area, 1);
1374 return NULL;
1377 ohci->bus = bus;
1378 bus->hcpriv = (void *) ohci;
1380 return ohci;
1385 * De-allocate all resources..
1386 * */
1387 static void release_ohci(struct ohci *ohci)
1390 OHCI_DEBUG(printk("USB HC release ohci \n"););
1391 /* stop hc */
1395 /* disconnect all devices */
1396 if(ohci->bus->root_hub) usb_disconnect(&ohci->bus->root_hub);
1398 reset_hc(__ohci);
1399 writel(OHCI_USB_RESET, &ohci->regs->control);
1400 wait_ms(10);
1402 if (ohci->irq >= 0) {
1403 free_irq(ohci->irq, ohci);
1404 ohci->irq = -1;
1407 usb_deregister_bus(ohci->bus);
1408 usb_free_bus(ohci->bus);
1410 /* unmap the IO address space */
1411 iounmap(ohci->regs);
1413 free_pages((unsigned int) ohci->hc_area, 1);
1418 * Increment the module usage count, start the control thread and
1419 * return success.
1420 * */
1421 static int found_ohci(int irq, void* mem_base)
1423 struct ohci *ohci;
1424 OHCI_DEBUG(printk("USB HC found ohci: irq= %d membase= %x \n", irq, (int)mem_base);)
1426 ohci = alloc_ohci(mem_base);
1427 if (!ohci) {
1428 return -ENOMEM;
1431 reset_hc(ohci);
1432 writel(OHCI_USB_RESET, &ohci->regs->control);
1433 wait_ms(10);
1434 usb_register_bus(ohci->bus);
1436 if (request_irq(irq, ohci_interrupt, SA_SHIRQ, "ohci-usb", ohci) == 0) {
1437 ohci->irq = irq;
1438 start_hc(ohci);
1439 return 0;
1441 release_ohci(ohci);
1442 return -EBUSY;
1445 static int start_ohci(struct pci_dev *dev)
1447 unsigned int mem_base = dev->resource[0].start;
1449 mem_base = (unsigned int) ioremap_nocache(mem_base, 4096);
1451 if (!mem_base) {
1452 printk("Error mapping OHCI memory\n");
1453 return -EFAULT;
1455 return found_ohci(dev->irq, (void *) mem_base);
1459 #ifdef CONFIG_APM
1460 static int handle_apm_event(apm_event_t event)
1462 static int down = 0;
1464 switch (event) {
1465 case APM_SYS_SUSPEND:
1466 case APM_USER_SUSPEND:
1467 if (down) {
1468 printk(KERN_DEBUG "ohci: received extra suspend event\n");
1469 break;
1471 down = 1;
1472 break;
1473 case APM_NORMAL_RESUME:
1474 case APM_CRITICAL_RESUME:
1475 if (!down) {
1476 printk(KERN_DEBUG "ohci: received bogus resume event\n");
1477 break;
1479 down = 0;
1480 // start_hc(ohci);
1481 break;
1483 return 0;
1485 #endif
1487 #define PCI_CLASS_SERIAL_USB_OHCI 0x0C0310
1489 int ohci_hcd_init(void)
1491 struct pci_dev *dev = NULL;
1493 while((dev = pci_find_class(PCI_CLASS_SERIAL_USB_OHCI, dev))) {
1494 if (start_ohci(dev) < 0) return -ENODEV;
1497 #ifdef CONFIG_APM
1498 apm_register_callback(&handle_apm_event);
1499 #endif
1501 return 0;
1504 #ifdef MODULE
1505 int init_module(void)
1507 return ohci_hcd_init();
1510 void cleanup_module(void)
1512 # ifdef CONFIG_APM
1513 apm_unregister_callback(&handle_apm_event);
1514 # endif
1515 release_ohci(__ohci);
1517 #endif //MODULE