File: /usr/src/linux/drivers/usb/uhci.c
1 /*
2 * Universal Host Controller Interface driver for USB.
3 *
4 * Maintainer: Johannes Erdfelt <johannes@erdfelt.com>
5 *
6 * (C) Copyright 1999 Linus Torvalds
7 * (C) Copyright 1999-2001 Johannes Erdfelt, johannes@erdfelt.com
8 * (C) Copyright 1999 Randy Dunlap
9 * (C) Copyright 1999 Georg Acher, acher@in.tum.de
10 * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de
11 * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch
12 * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at
13 * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface
14 * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
15 * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c)
16 *
17 * Intel documents this fairly well, and as far as I know there
18 * are no royalties or anything like that, but even so there are
19 * people who decided that they want to do the same thing in a
20 * completely different way.
21 *
22 * WARNING! The USB documentation is downright evil. Most of it
23 * is just crap, written by a committee. You're better off ignoring
24 * most of it, the important stuff is:
25 * - the low-level protocol (fairly simple but lots of small details)
26 * - working around the horridness of the rest
27 */
28
29 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <linux/pci.h>
32 #include <linux/kernel.h>
33 #include <linux/init.h>
34 #include <linux/delay.h>
35 #include <linux/ioport.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
38 #include <linux/smp_lock.h>
39 #include <linux/errno.h>
40 #include <linux/unistd.h>
41 #include <linux/interrupt.h>
42 #include <linux/spinlock.h>
43 #include <linux/proc_fs.h>
44 #ifdef CONFIG_USB_DEBUG
45 #define DEBUG
46 #else
47 #undef DEBUG
48 #endif
49 #include <linux/usb.h>
50
51 #include <asm/uaccess.h>
52 #include <asm/io.h>
53 #include <asm/irq.h>
54 #include <asm/system.h>
55
56 #include "uhci.h"
57
58 #include <linux/pm.h>
59
60
61 /*
62 * Version Information
63 */
64 #define DRIVER_VERSION ""
65 #define DRIVER_AUTHOR "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber"
66 #define DRIVER_DESC "USB Universal Host Controller Interface driver"
67
68
69 /*
70 * debug = 0, no debugging messages
71 * debug = 1, dump failed URB's except for stalls
72 * debug = 2, dump all failed URB's (including stalls)
73 * show all queues in /proc/uhci/hc*
74 * debug = 3, show all TD's in URB's when dumping
75 */
76 #ifdef DEBUG
77 static int debug = 1;
78 #else
79 static int debug = 0;
80 #endif
81 MODULE_PARM(debug, "i");
82 MODULE_PARM_DESC(debug, "Debug level");
83 static char *errbuf;
84 #define ERRBUF_LEN (PAGE_SIZE * 8)
85
86 #include "uhci-debug.h"
87
88 static kmem_cache_t *uhci_up_cachep; /* urb_priv */
89
90 static int rh_submit_urb(struct urb *urb);
91 static int rh_unlink_urb(struct urb *urb);
92 static int uhci_get_current_frame_number(struct usb_device *dev);
93 static int uhci_unlink_urb(struct urb *urb);
94 static void uhci_unlink_generic(struct uhci *uhci, struct urb *urb);
95 static void uhci_call_completion(struct urb *urb);
96
97 static int ports_active(struct uhci *uhci);
98 static void suspend_hc(struct uhci *uhci);
99 static void wakeup_hc(struct uhci *uhci);
100
101 /* If a transfer is still active after this much time, turn off FSBR */
102 #define IDLE_TIMEOUT (HZ / 20) /* 50 ms */
103
104 #define MAX_URB_LOOP 2048 /* Maximum number of linked URB's */
105
106 /*
107 * Only the USB core should call uhci_alloc_dev and uhci_free_dev
108 */
109 static int uhci_alloc_dev(struct usb_device *dev)
110 {
111 return 0;
112 }
113
114 static int uhci_free_dev(struct usb_device *dev)
115 {
116 struct uhci *uhci = (struct uhci *)dev->bus->hcpriv;
117 struct list_head list, *tmp, *head;
118 unsigned long flags;
119
120 /* Walk through the entire URB list and forcefully remove any */
121 /* URBs that are still active for that device */
122
123 /* Two stage unlink so we don't deadlock on urb_list_lock */
124 INIT_LIST_HEAD(&list);
125
126 spin_lock_irqsave(&uhci->urb_list_lock, flags);
127 head = &uhci->urb_list;
128 tmp = head->next;
129 while (tmp != head) {
130 struct urb *urb = list_entry(tmp, struct urb, urb_list);
131
132 tmp = tmp->next;
133
134 if (urb->dev == dev) {
135 list_del(&urb->urb_list);
136 list_add(&urb->urb_list, &list);
137 }
138 }
139 spin_unlock_irqrestore(&uhci->urb_list_lock, flags);
140
141 head = &list;
142 tmp = head->next;
143 while (tmp != head) {
144 struct urb *urb = list_entry(tmp, struct urb, urb_list);
145 tmp = tmp->next;
146
147 /* Make sure we block waiting on these to die */
148 urb->transfer_flags &= ~USB_ASYNC_UNLINK;
149
150 /* uhci_unlink_urb will unlink from the temp list */
151 uhci_unlink_urb(urb);
152 }
153
154 return 0;
155 }
156
157 static inline void uhci_set_next_interrupt(struct uhci *uhci)
158 {
159 unsigned long flags;
160
161 spin_lock_irqsave(&uhci->frame_list_lock, flags);
162 uhci->skel_term_td->status |= TD_CTRL_IOC;
163 spin_unlock_irqrestore(&uhci->frame_list_lock, flags);
164 }
165
166 static inline void uhci_clear_next_interrupt(struct uhci *uhci)
167 {
168 unsigned long flags;
169
170 spin_lock_irqsave(&uhci->frame_list_lock, flags);
171 uhci->skel_term_td->status &= ~TD_CTRL_IOC;
172 spin_unlock_irqrestore(&uhci->frame_list_lock, flags);
173 }
174
175 static inline void uhci_add_complete(struct urb *urb)
176 {
177 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
178 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
179 unsigned long flags;
180
181 spin_lock_irqsave(&uhci->complete_list_lock, flags);
182 list_add(&urbp->complete_list, &uhci->complete_list);
183 spin_unlock_irqrestore(&uhci->complete_list_lock, flags);
184 }
185
186 static struct uhci_td *uhci_alloc_td(struct uhci *uhci, struct usb_device *dev)
187 {
188 dma_addr_t dma_handle;
189 struct uhci_td *td;
190
191 td = pci_pool_alloc(uhci->td_pool, GFP_DMA | GFP_ATOMIC, &dma_handle);
192 if (!td)
193 return NULL;
194
195 td->dma_handle = dma_handle;
196
197 td->link = UHCI_PTR_TERM;
198 td->buffer = 0;
199
200 td->frame = -1;
201 td->dev = dev;
202
203 INIT_LIST_HEAD(&td->list);
204 INIT_LIST_HEAD(&td->fl_list);
205
206 usb_inc_dev_use(dev);
207
208 return td;
209 }
210
211 static void inline uhci_fill_td(struct uhci_td *td, __u32 status,
212 __u32 info, __u32 buffer)
213 {
214 td->status = status;
215 td->info = info;
216 td->buffer = buffer;
217 }
218
219 static void uhci_insert_td(struct uhci *uhci, struct uhci_td *skeltd, struct uhci_td *td)
220 {
221 unsigned long flags;
222 struct uhci_td *ltd;
223
224 spin_lock_irqsave(&uhci->frame_list_lock, flags);
225
226 ltd = list_entry(skeltd->fl_list.prev, struct uhci_td, fl_list);
227
228 td->link = ltd->link;
229 mb();
230 ltd->link = td->dma_handle;
231
232 list_add_tail(&td->fl_list, &skeltd->fl_list);
233
234 spin_unlock_irqrestore(&uhci->frame_list_lock, flags);
235 }
236
237 /*
238 * We insert Isochronous transfers directly into the frame list at the
239 * beginning
240 * The layout looks as follows:
241 * frame list pointer -> iso td's (if any) ->
242 * periodic interrupt td (if frame 0) -> irq td's -> control qh -> bulk qh
243 */
244 static void uhci_insert_td_frame_list(struct uhci *uhci, struct uhci_td *td, unsigned framenum)
245 {
246 unsigned long flags;
247
248 framenum %= UHCI_NUMFRAMES;
249
250 spin_lock_irqsave(&uhci->frame_list_lock, flags);
251
252 td->frame = framenum;
253
254 /* Is there a TD already mapped there? */
255 if (uhci->fl->frame_cpu[framenum]) {
256 struct uhci_td *ftd, *ltd;
257
258 ftd = uhci->fl->frame_cpu[framenum];
259 ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list);
260
261 list_add_tail(&td->fl_list, &ftd->fl_list);
262
263 td->link = ltd->link;
264 mb();
265 ltd->link = td->dma_handle;
266 } else {
267 td->link = uhci->fl->frame[framenum];
268 mb();
269 uhci->fl->frame[framenum] = td->dma_handle;
270 uhci->fl->frame_cpu[framenum] = td;
271 }
272
273 spin_unlock_irqrestore(&uhci->frame_list_lock, flags);
274 }
275
276 static void uhci_remove_td(struct uhci *uhci, struct uhci_td *td)
277 {
278 unsigned long flags;
279
280 /* If it's not inserted, don't remove it */
281 if (td->frame == -1 && list_empty(&td->fl_list))
282 return;
283
284 spin_lock_irqsave(&uhci->frame_list_lock, flags);
285 if (td->frame != -1 && uhci->fl->frame_cpu[td->frame] == td) {
286 if (list_empty(&td->fl_list)) {
287 uhci->fl->frame[td->frame] = td->link;
288 uhci->fl->frame_cpu[td->frame] = NULL;
289 } else {
290 struct uhci_td *ntd;
291
292 ntd = list_entry(td->fl_list.next, struct uhci_td, fl_list);
293 uhci->fl->frame[td->frame] = ntd->dma_handle;
294 uhci->fl->frame_cpu[td->frame] = ntd;
295 }
296 } else {
297 struct uhci_td *ptd;
298
299 ptd = list_entry(td->fl_list.prev, struct uhci_td, fl_list);
300 ptd->link = td->link;
301 }
302
303 mb();
304 td->link = UHCI_PTR_TERM;
305
306 list_del_init(&td->fl_list);
307 td->frame = -1;
308
309 spin_unlock_irqrestore(&uhci->frame_list_lock, flags);
310 }
311
312 /*
313 * Inserts a td into qh list at the top.
314 */
315 static void uhci_insert_tds_in_qh(struct uhci_qh *qh, struct urb *urb, int breadth)
316 {
317 struct list_head *tmp, *head;
318 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
319 struct uhci_td *td, *ptd;
320
321 if (list_empty(&urbp->td_list))
322 return;
323
324 head = &urbp->td_list;
325 tmp = head->next;
326
327 /* Ordering isn't important here yet since the QH hasn't been */
328 /* inserted into the schedule yet */
329 td = list_entry(tmp, struct uhci_td, list);
330
331 /* Add the first TD to the QH element pointer */
332 qh->element = td->dma_handle | (breadth ? 0 : UHCI_PTR_DEPTH);
333
334 ptd = td;
335
336 /* Then link the rest of the TD's */
337 tmp = tmp->next;
338 while (tmp != head) {
339 td = list_entry(tmp, struct uhci_td, list);
340
341 tmp = tmp->next;
342
343 ptd->link = td->dma_handle | (breadth ? 0 : UHCI_PTR_DEPTH);
344
345 ptd = td;
346 }
347
348 ptd->link = UHCI_PTR_TERM;
349 }
350
351 static void uhci_free_td(struct uhci *uhci, struct uhci_td *td)
352 {
353 if (!list_empty(&td->list) || !list_empty(&td->fl_list))
354 dbg("td is still in URB list!");
355
356 if (td->dev)
357 usb_dec_dev_use(td->dev);
358
359 pci_pool_free(uhci->td_pool, td, td->dma_handle);
360 }
361
362 static struct uhci_qh *uhci_alloc_qh(struct uhci *uhci, struct usb_device *dev)
363 {
364 dma_addr_t dma_handle;
365 struct uhci_qh *qh;
366
367 qh = pci_pool_alloc(uhci->qh_pool, GFP_DMA | GFP_ATOMIC, &dma_handle);
368 if (!qh)
369 return NULL;
370
371 qh->dma_handle = dma_handle;
372
373 qh->element = UHCI_PTR_TERM;
374 qh->link = UHCI_PTR_TERM;
375
376 qh->dev = dev;
377
378 INIT_LIST_HEAD(&qh->list);
379 INIT_LIST_HEAD(&qh->remove_list);
380
381 usb_inc_dev_use(dev);
382
383 return qh;
384 }
385
386 static void uhci_free_qh(struct uhci *uhci, struct uhci_qh *qh)
387 {
388 if (!list_empty(&qh->list))
389 dbg("qh list not empty!");
390 if (!list_empty(&qh->remove_list))
391 dbg("qh still in remove_list!");
392
393 if (qh->dev)
394 usb_dec_dev_use(qh->dev);
395
396 pci_pool_free(uhci->qh_pool, qh, qh->dma_handle);
397 }
398
399 static void uhci_insert_qh(struct uhci *uhci, struct uhci_qh *skelqh, struct uhci_qh *qh)
400 {
401 struct uhci_qh *lqh;
402 unsigned long flags;
403
404 spin_lock_irqsave(&uhci->frame_list_lock, flags);
405
406 /* Grab the last QH */
407 lqh = list_entry(skelqh->list.prev, struct uhci_qh, list);
408
409 qh->link = lqh->link;
410 mb(); /* Ordering is important */
411 lqh->link = qh->dma_handle | UHCI_PTR_QH;
412
413 list_add_tail(&qh->list, &skelqh->list);
414
415 spin_unlock_irqrestore(&uhci->frame_list_lock, flags);
416 }
417
418 static void uhci_remove_qh(struct uhci *uhci, struct uhci_qh *qh)
419 {
420 unsigned long flags;
421 struct uhci_qh *prevqh;
422
423 /* Only go through the hoops if it's actually linked in */
424 if (list_empty(&qh->list)) {
425 goto list;
426 }
427
428 qh->urbp = NULL;
429
430 spin_lock_irqsave(&uhci->frame_list_lock, flags);
431
432 prevqh = list_entry(qh->list.prev, struct uhci_qh, list);
433
434 prevqh->link = qh->link;
435 mb();
436 qh->element = qh->link = UHCI_PTR_TERM;
437
438 list_del_init(&qh->list);
439
440 spin_unlock_irqrestore(&uhci->frame_list_lock, flags);
441
442 list:
443 spin_lock_irqsave(&uhci->qh_remove_list_lock, flags);
444
445 /* Check to see if the remove list is empty. Set the IOC bit */
446 /* to force an interrupt so we can remove the QH */
447 if (list_empty(&uhci->qh_remove_list))
448 uhci_set_next_interrupt(uhci);
449
450 list_add(&qh->remove_list, &uhci->qh_remove_list);
451
452 spin_unlock_irqrestore(&uhci->qh_remove_list_lock, flags);
453 }
454
455 static int uhci_fixup_toggle(struct urb *urb, unsigned int toggle)
456 {
457 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
458 struct list_head *head, *tmp;
459
460 head = &urbp->td_list;
461 tmp = head->next;
462 while (head != tmp) {
463 struct uhci_td *td = list_entry(tmp, struct uhci_td, list);
464
465 tmp = tmp->next;
466
467 td->info &= ~(1 << TD_TOKEN_TOGGLE);
468 if (toggle)
469 td->info |= (1 << TD_TOKEN_TOGGLE);
470
471 toggle ^= 1;
472 }
473
474 return toggle;
475 }
476
477 /* This function will append one URB's QH to another URB's QH. This is for */
478 /* USB_QUEUE_BULK support for bulk transfers and soon implicitily for */
479 /* control transfers */
480 static void uhci_append_queued_urb(struct uhci *uhci, struct urb *eurb, struct urb *urb)
481 {
482 struct urb_priv *eurbp, *urbp, *furbp, *lurbp;
483 struct list_head *tmp;
484 struct uhci_td *ftd, *lltd;
485 unsigned long flags;
486
487 eurbp = eurb->hcpriv;
488 urbp = urb->hcpriv;
489
490 spin_lock_irqsave(&uhci->frame_list_lock, flags);
491
492 /* Find the first URB in the queue */
493 if (eurbp->queued) {
494 struct list_head *head = &eurbp->queue_list;
495
496 tmp = head->next;
497 while (tmp != head) {
498 struct urb_priv *turbp =
499 list_entry(tmp, struct urb_priv, queue_list);
500
501 if (!turbp->queued)
502 break;
503
504 tmp = tmp->next;
505 }
506 } else
507 tmp = &eurbp->queue_list;
508
509 furbp = list_entry(tmp, struct urb_priv, queue_list);
510 lurbp = list_entry(furbp->queue_list.prev, struct urb_priv, queue_list);
511
512 lltd = list_entry(lurbp->td_list.prev, struct uhci_td, list);
513 ftd = list_entry(urbp->td_list.next, struct uhci_td, list);
514
515 uhci_fixup_toggle(urb, uhci_toggle(lltd->info) ^ 1);
516
517 mb(); /* Make sure we flush everything */
518 /* Only support bulk right now, so no depth */
519 lltd->link = ftd->dma_handle;
520
521 list_add_tail(&urbp->queue_list, &furbp->queue_list);
522
523 urbp->queued = 1;
524
525 spin_unlock_irqrestore(&uhci->frame_list_lock, flags);
526 }
527
528 static void uhci_delete_queued_urb(struct uhci *uhci, struct urb *urb)
529 {
530 struct urb_priv *urbp, *nurbp;
531 struct list_head *head, *tmp;
532 struct urb_priv *purbp;
533 struct uhci_td *pltd;
534 unsigned int toggle;
535 unsigned long flags;
536
537 urbp = urb->hcpriv;
538
539 spin_lock_irqsave(&uhci->frame_list_lock, flags);
540
541 if (list_empty(&urbp->queue_list))
542 goto out;
543
544 nurbp = list_entry(urbp->queue_list.next, struct urb_priv, queue_list);
545
546 /* Fix up the toggle for the next URB's */
547 if (!urbp->queued)
548 toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
549 else {
550 /* If we're in the middle of the queue, grab the toggle */
551 /* from the TD previous to us */
552 purbp = list_entry(urbp->queue_list.prev, struct urb_priv,
553 queue_list);
554
555 pltd = list_entry(purbp->td_list.prev, struct uhci_td, list);
556
557 toggle = uhci_toggle(pltd->info) ^ 1;
558 }
559
560 head = &urbp->queue_list;
561 tmp = head->next;
562 while (head != tmp) {
563 struct urb_priv *turbp;
564
565 turbp = list_entry(tmp, struct urb_priv, queue_list);
566
567 tmp = tmp->next;
568
569 if (!turbp->queued)
570 break;
571
572 toggle = uhci_fixup_toggle(turbp->urb, toggle);
573 }
574
575 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
576 usb_pipeout(urb->pipe), toggle);
577
578 if (!urbp->queued) {
579 int status;
580
581 /* The HC may continue using the current QH if it finished */
582 /* all of the TD's in this URB and may have started on the */
583 /* next URB's TD's already, so we'll take over ownership */
584 /* of this QH and use it instead. Don't forget to delete */
585 /* the old QH first */
586 uhci_free_qh(uhci, nurbp->qh);
587
588 nurbp->qh = urbp->qh;
589 nurbp->qh->urbp = nurbp;
590 urbp->qh = NULL;
591
592 /* If the last TD from the first (this) urb didn't */
593 /* complete, reset qh->element to the first TD in the */
594 /* next urb */
595 pltd = list_entry(urbp->td_list.prev, struct uhci_td, list);
596 status = uhci_status_bits(pltd->status);
597 if ((status & TD_CTRL_ACTIVE) || uhci_actual_length(pltd->status) < uhci_expected_length(pltd->info)) {
598 struct uhci_td *ftd = list_entry(nurbp->td_list.next, struct uhci_td, list);
599 nurbp->qh->element = ftd->dma_handle;
600 }
601
602 nurbp->queued = 0;
603 } else {
604 /* We're somewhere in the middle (or end). A bit trickier */
605 /* than the head scenario */
606 purbp = list_entry(urbp->queue_list.prev, struct urb_priv,
607 queue_list);
608
609 pltd = list_entry(purbp->td_list.prev, struct uhci_td, list);
610 if (nurbp->queued) {
611 struct uhci_td *nftd;
612
613 /* Close the gap between the two */
614 nftd = list_entry(nurbp->td_list.next, struct uhci_td,
615 list);
616 pltd->link = nftd->dma_handle;
617 } else
618 /* The next URB happens to be the beggining, so */
619 /* we're the last, end the chain */
620 pltd->link = UHCI_PTR_TERM;
621 }
622
623 list_del_init(&urbp->queue_list);
624
625 out:
626 spin_unlock_irqrestore(&uhci->frame_list_lock, flags);
627 }
628
629 static struct urb_priv *uhci_alloc_urb_priv(struct uhci *uhci, struct urb *urb)
630 {
631 struct urb_priv *urbp;
632
633 urbp = kmem_cache_alloc(uhci_up_cachep, in_interrupt() ? SLAB_ATOMIC : SLAB_KERNEL);
634 if (!urbp) {
635 err("uhci_alloc_urb_priv: couldn't allocate memory for urb_priv\n");
636 return NULL;
637 }
638
639 memset((void *)urbp, 0, sizeof(*urbp));
640
641 urbp->inserttime = jiffies;
642 urbp->urb = urb;
643 urbp->dev = urb->dev;
644
645 INIT_LIST_HEAD(&urbp->td_list);
646 INIT_LIST_HEAD(&urbp->queue_list);
647 INIT_LIST_HEAD(&urbp->complete_list);
648
649 urb->hcpriv = urbp;
650
651 if (urb->dev != uhci->rh.dev) {
652 if (urb->transfer_buffer_length) {
653 urbp->transfer_buffer_dma_handle = pci_map_single(uhci->dev,
654 urb->transfer_buffer, urb->transfer_buffer_length,
655 usb_pipein(urb->pipe) ? PCI_DMA_FROMDEVICE :
656 PCI_DMA_TODEVICE);
657 if (!urbp->transfer_buffer_dma_handle)
658 return NULL;
659 }
660
661 if (usb_pipetype(urb->pipe) == PIPE_CONTROL && urb->setup_packet) {
662 urbp->setup_packet_dma_handle = pci_map_single(uhci->dev,
663 urb->setup_packet, sizeof(devrequest),
664 PCI_DMA_TODEVICE);
665 if (!urbp->setup_packet_dma_handle)
666 return NULL;
667 }
668 }
669
670 return urbp;
671 }
672
673 static void uhci_add_td_to_urb(struct urb *urb, struct uhci_td *td)
674 {
675 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
676
677 td->urb = urb;
678
679 list_add_tail(&td->list, &urbp->td_list);
680 }
681
682 static void uhci_remove_td_from_urb(struct uhci_td *td)
683 {
684 if (list_empty(&td->list))
685 return;
686
687 list_del_init(&td->list);
688
689 td->urb = NULL;
690 }
691
692 static void uhci_destroy_urb_priv(struct urb *urb)
693 {
694 struct list_head *head, *tmp;
695 struct urb_priv *urbp;
696 struct uhci *uhci;
697 unsigned long flags;
698
699 spin_lock_irqsave(&urb->lock, flags);
700
701 urbp = (struct urb_priv *)urb->hcpriv;
702 if (!urbp)
703 goto out;
704
705 if (!urbp->dev || !urbp->dev->bus || !urbp->dev->bus->hcpriv) {
706 warn("uhci_destroy_urb_priv: urb %p belongs to disconnected device or bus?", urb);
707 goto out;
708 }
709
710 if (!list_empty(&urb->urb_list))
711 warn("uhci_destroy_urb_priv: urb %p still on uhci->urb_list or uhci->remove_list", urb);
712
713 if (!list_empty(&urbp->complete_list))
714 warn("uhci_destroy_urb_priv: urb %p still on uhci->complete_list", urb);
715
716 uhci = urbp->dev->bus->hcpriv;
717
718 head = &urbp->td_list;
719 tmp = head->next;
720 while (tmp != head) {
721 struct uhci_td *td = list_entry(tmp, struct uhci_td, list);
722
723 tmp = tmp->next;
724
725 uhci_remove_td_from_urb(td);
726 uhci_remove_td(uhci, td);
727 uhci_free_td(uhci, td);
728 }
729
730 if (urbp->setup_packet_dma_handle)
731 pci_unmap_single(uhci->dev, urbp->setup_packet_dma_handle,
732 sizeof(devrequest), PCI_DMA_TODEVICE);
733
734 if (urbp->transfer_buffer_dma_handle)
735 pci_unmap_single(uhci->dev, urbp->transfer_buffer_dma_handle,
736 urb->transfer_buffer_length, usb_pipein(urb->pipe) ?
737 PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
738
739 urb->hcpriv = NULL;
740 kmem_cache_free(uhci_up_cachep, urbp);
741
742 out:
743 spin_unlock_irqrestore(&urb->lock, flags);
744 }
745
746 static void uhci_inc_fsbr(struct uhci *uhci, struct urb *urb)
747 {
748 unsigned long flags;
749 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
750
751 spin_lock_irqsave(&uhci->frame_list_lock, flags);
752
753 if ((!(urb->transfer_flags & USB_NO_FSBR)) && !urbp->fsbr) {
754 urbp->fsbr = 1;
755 if (!uhci->fsbr++)
756 uhci->skel_term_qh->link = uhci->skel_hs_control_qh->dma_handle | UHCI_PTR_QH;
757 }
758
759 spin_unlock_irqrestore(&uhci->frame_list_lock, flags);
760 }
761
762 static void uhci_dec_fsbr(struct uhci *uhci, struct urb *urb)
763 {
764 unsigned long flags;
765 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
766
767 spin_lock_irqsave(&uhci->frame_list_lock, flags);
768
769 if ((!(urb->transfer_flags & USB_NO_FSBR)) && urbp->fsbr) {
770 urbp->fsbr = 0;
771 if (!--uhci->fsbr)
772 uhci->skel_term_qh->link = UHCI_PTR_TERM;
773 }
774
775 spin_unlock_irqrestore(&uhci->frame_list_lock, flags);
776 }
777
778 /*
779 * Map status to standard result codes
780 *
781 * <status> is (td->status & 0xFE0000) [a.k.a. uhci_status_bits(td->status)]
782 * <dir_out> is True for output TDs and False for input TDs.
783 */
784 static int uhci_map_status(int status, int dir_out)
785 {
786 if (!status)
787 return 0;
788 if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
789 return -EPROTO;
790 if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
791 if (dir_out)
792 return -ETIMEDOUT;
793 else
794 return -EILSEQ;
795 }
796 if (status & TD_CTRL_NAK) /* NAK */
797 return -ETIMEDOUT;
798 if (status & TD_CTRL_BABBLE) /* Babble */
799 return -EPIPE;
800 if (status & TD_CTRL_DBUFERR) /* Buffer error */
801 return -ENOSR;
802 if (status & TD_CTRL_STALLED) /* Stalled */
803 return -EPIPE;
804 if (status & TD_CTRL_ACTIVE) /* Active */
805 return 0;
806
807 return -EINVAL;
808 }
809
810 /*
811 * Control transfers
812 */
813 static int uhci_submit_control(struct urb *urb)
814 {
815 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
816 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
817 struct uhci_td *td;
818 struct uhci_qh *qh;
819 unsigned long destination, status;
820 int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
821 int len = urb->transfer_buffer_length;
822 dma_addr_t data = urbp->transfer_buffer_dma_handle;
823
824 /* The "pipe" thing contains the destination in bits 8--18 */
825 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
826
827 /* 3 errors */
828 status = (urb->pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | (3 << 27);
829
830 /*
831 * Build the TD for the control request
832 */
833 td = uhci_alloc_td(uhci, urb->dev);
834 if (!td)
835 return -ENOMEM;
836
837 uhci_add_td_to_urb(urb, td);
838 uhci_fill_td(td, status, destination | (7 << 21),
839 urbp->setup_packet_dma_handle);
840
841 /*
842 * If direction is "send", change the frame from SETUP (0x2D)
843 * to OUT (0xE1). Else change it from SETUP to IN (0x69).
844 */
845 destination ^= (USB_PID_SETUP ^ usb_packetid(urb->pipe));
846
847 if (!(urb->transfer_flags & USB_DISABLE_SPD))
848 status |= TD_CTRL_SPD;
849
850 /*
851 * Build the DATA TD's
852 */
853 while (len > 0) {
854 int pktsze = len;
855
856 if (pktsze > maxsze)
857 pktsze = maxsze;
858
859 td = uhci_alloc_td(uhci, urb->dev);
860 if (!td)
861 return -ENOMEM;
862
863 /* Alternate Data0/1 (start with Data1) */
864 destination ^= 1 << TD_TOKEN_TOGGLE;
865
866 uhci_add_td_to_urb(urb, td);
867 uhci_fill_td(td, status, destination | ((pktsze - 1) << 21),
868 data);
869
870 data += pktsze;
871 len -= pktsze;
872 }
873
874 /*
875 * Build the final TD for control status
876 */
877 td = uhci_alloc_td(uhci, urb->dev);
878 if (!td)
879 return -ENOMEM;
880
881 /*
882 * It's IN if the pipe is an output pipe or we're not expecting
883 * data back.
884 */
885 destination &= ~TD_PID;
886 if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length)
887 destination |= USB_PID_IN;
888 else
889 destination |= USB_PID_OUT;
890
891 destination |= 1 << TD_TOKEN_TOGGLE; /* End in Data1 */
892
893 status &= ~TD_CTRL_SPD;
894
895 uhci_add_td_to_urb(urb, td);
896 uhci_fill_td(td, status | TD_CTRL_IOC,
897 destination | (UHCI_NULL_DATA_SIZE << 21), 0);
898
899 qh = uhci_alloc_qh(uhci, urb->dev);
900 if (!qh)
901 return -ENOMEM;
902
903 /* Low speed or small transfers gets a different queue and treatment */
904 if (urb->pipe & TD_CTRL_LS) {
905 uhci_insert_tds_in_qh(qh, urb, 0);
906 uhci_insert_qh(uhci, uhci->skel_ls_control_qh, qh);
907 } else {
908 uhci_insert_tds_in_qh(qh, urb, 1);
909 uhci_insert_qh(uhci, uhci->skel_hs_control_qh, qh);
910 uhci_inc_fsbr(uhci, urb);
911 }
912
913 urbp->qh = qh;
914 qh->urbp = urbp;
915
916 return -EINPROGRESS;
917 }
918
919 static int usb_control_retrigger_status(struct urb *urb);
920
921 static int uhci_result_control(struct urb *urb)
922 {
923 struct list_head *tmp, *head;
924 struct urb_priv *urbp = urb->hcpriv;
925 struct uhci_td *td;
926 unsigned int status;
927 int ret = 0;
928
929 if (list_empty(&urbp->td_list))
930 return -EINVAL;
931
932 head = &urbp->td_list;
933
934 if (urbp->short_control_packet) {
935 tmp = head->prev;
936 goto status_phase;
937 }
938
939 tmp = head->next;
940 td = list_entry(tmp, struct uhci_td, list);
941
942 /* The first TD is the SETUP phase, check the status, but skip */
943 /* the count */
944 status = uhci_status_bits(td->status);
945 if (status & TD_CTRL_ACTIVE)
946 return -EINPROGRESS;
947
948 if (status)
949 goto td_error;
950
951 urb->actual_length = 0;
952
953 /* The rest of the TD's (but the last) are data */
954 tmp = tmp->next;
955 while (tmp != head && tmp->next != head) {
956 td = list_entry(tmp, struct uhci_td, list);
957
958 tmp = tmp->next;
959
960 if (urbp->fsbr_timeout && (td->status & TD_CTRL_IOC) &&
961 !(td->status & TD_CTRL_ACTIVE)) {
962 uhci_inc_fsbr(urb->dev->bus->hcpriv, urb);
963 urbp->fsbr_timeout = 0;
964 td->status &= ~TD_CTRL_IOC;
965 }
966
967 status = uhci_status_bits(td->status);
968 if (status & TD_CTRL_ACTIVE)
969 return -EINPROGRESS;
970
971 urb->actual_length += uhci_actual_length(td->status);
972
973 if (status)
974 goto td_error;
975
976 /* Check to see if we received a short packet */
977 if (uhci_actual_length(td->status) < uhci_expected_length(td->info)) {
978 if (urb->transfer_flags & USB_DISABLE_SPD) {
979 ret = -EREMOTEIO;
980 goto err;
981 }
982
983 if (uhci_packetid(td->info) == USB_PID_IN)
984 return usb_control_retrigger_status(urb);
985 else
986 return 0;
987 }
988 }
989
990 status_phase:
991 td = list_entry(tmp, struct uhci_td, list);
992
993 /* Control status phase */
994 status = uhci_status_bits(td->status);
995
996 #ifdef I_HAVE_BUGGY_APC_BACKUPS
997 /* APC BackUPS Pro kludge */
998 /* It tries to send all of the descriptor instead of the amount */
999 /* we requested */
1000 if (td->status & TD_CTRL_IOC && /* IOC is masked out by uhci_status_bits */
1001 status & TD_CTRL_ACTIVE &&
1002 status & TD_CTRL_NAK)
1003 return 0;
1004 #endif
1005
1006 if (status & TD_CTRL_ACTIVE)
1007 return -EINPROGRESS;
1008
1009 if (status)
1010 goto td_error;
1011
1012 return 0;
1013
1014 td_error:
1015 ret = uhci_map_status(status, uhci_packetout(td->info));
1016 if (ret == -EPIPE)
1017 /* endpoint has stalled - mark it halted */
1018 usb_endpoint_halt(urb->dev, uhci_endpoint(td->info),
1019 uhci_packetout(td->info));
1020
1021 err:
1022 if ((debug == 1 && ret != -EPIPE) || debug > 1) {
1023 /* Some debugging code */
1024 dbg("uhci_result_control() failed with status %x", status);
1025
1026 if (errbuf) {
1027 /* Print the chain for debugging purposes */
1028 uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0);
1029
1030 lprintk(errbuf);
1031 }
1032 }
1033
1034 return ret;
1035 }
1036
1037 static int usb_control_retrigger_status(struct urb *urb)
1038 {
1039 struct list_head *tmp, *head;
1040 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1041 struct uhci *uhci = urb->dev->bus->hcpriv;
1042
1043 urbp->short_control_packet = 1;
1044
1045 /* Create a new QH to avoid pointer overwriting problems */
1046 uhci_remove_qh(uhci, urbp->qh);
1047
1048 /* Delete all of the TD's except for the status TD at the end */
1049 head = &urbp->td_list;
1050 tmp = head->next;
1051 while (tmp != head && tmp->next != head) {
1052 struct uhci_td *td = list_entry(tmp, struct uhci_td, list);
1053
1054 tmp = tmp->next;
1055
1056 uhci_remove_td_from_urb(td);
1057 uhci_remove_td(uhci, td);
1058 uhci_free_td(uhci, td);
1059 }
1060
1061 urbp->qh = uhci_alloc_qh(uhci, urb->dev);
1062 if (!urbp->qh) {
1063 err("unable to allocate new QH for control retrigger");
1064 return -ENOMEM;
1065 }
1066
1067 urbp->qh->urbp = urbp;
1068
1069 /* One TD, who cares about Breadth first? */
1070 uhci_insert_tds_in_qh(urbp->qh, urb, 0);
1071
1072 /* Low speed or small transfers gets a different queue and treatment */
1073 if (urb->pipe & TD_CTRL_LS)
1074 uhci_insert_qh(uhci, uhci->skel_ls_control_qh, urbp->qh);
1075 else
1076 uhci_insert_qh(uhci, uhci->skel_hs_control_qh, urbp->qh);
1077
1078 return -EINPROGRESS;
1079 }
1080
1081 /*
1082 * Interrupt transfers
1083 */
1084 static int uhci_submit_interrupt(struct urb *urb)
1085 {
1086 struct uhci_td *td;
1087 unsigned long destination, status;
1088 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1089 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1090
1091 if (urb->transfer_buffer_length > usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)))
1092 return -EINVAL;
1093
1094 /* The "pipe" thing contains the destination in bits 8--18 */
1095 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1096
1097 status = (urb->pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC;
1098
1099 td = uhci_alloc_td(uhci, urb->dev);
1100 if (!td)
1101 return -ENOMEM;
1102
1103 destination |= (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE);
1104 destination |= ((urb->transfer_buffer_length - 1) << 21);
1105
1106 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
1107
1108 uhci_add_td_to_urb(urb, td);
1109 uhci_fill_td(td, status, destination, urbp->transfer_buffer_dma_handle);
1110
1111 uhci_insert_td(uhci, uhci->skeltd[__interval_to_skel(urb->interval)], td);
1112
1113 return -EINPROGRESS;
1114 }
1115
1116 static int uhci_result_interrupt(struct urb *urb)
1117 {
1118 struct list_head *tmp, *head;
1119 struct urb_priv *urbp = urb->hcpriv;
1120 struct uhci_td *td;
1121 unsigned int status;
1122 int ret = 0;
1123
1124 urb->actual_length = 0;
1125
1126 head = &urbp->td_list;
1127 tmp = head->next;
1128 while (tmp != head) {
1129 td = list_entry(tmp, struct uhci_td, list);
1130
1131 tmp = tmp->next;
1132
1133 if (urbp->fsbr_timeout && (td->status & TD_CTRL_IOC) &&
1134 !(td->status & TD_CTRL_ACTIVE)) {
1135 uhci_inc_fsbr(urb->dev->bus->hcpriv, urb);
1136 urbp->fsbr_timeout = 0;
1137 td->status &= ~TD_CTRL_IOC;
1138 }
1139
1140 status = uhci_status_bits(td->status);
1141 if (status & TD_CTRL_ACTIVE)
1142 return -EINPROGRESS;
1143
1144 urb->actual_length += uhci_actual_length(td->status);
1145
1146 if (status)
1147 goto td_error;
1148
1149 if (uhci_actual_length(td->status) < uhci_expected_length(td->info)) {
1150 usb_settoggle(urb->dev, uhci_endpoint(td->info),
1151 uhci_packetout(td->info),
1152 uhci_toggle(td->info) ^ 1);
1153
1154 if (urb->transfer_flags & USB_DISABLE_SPD) {
1155 ret = -EREMOTEIO;
1156 goto err;
1157 } else
1158 return 0;
1159 }
1160 }
1161
1162 return 0;
1163
1164 td_error:
1165 ret = uhci_map_status(status, uhci_packetout(td->info));
1166 if (ret == -EPIPE)
1167 /* endpoint has stalled - mark it halted */
1168 usb_endpoint_halt(urb->dev, uhci_endpoint(td->info),
1169 uhci_packetout(td->info));
1170
1171 err:
1172 if ((debug == 1 && ret != -EPIPE) || debug > 1) {
1173 /* Some debugging code */
1174 dbg("uhci_result_interrupt/bulk() failed with status %x",
1175 status);
1176
1177 if (errbuf) {
1178 /* Print the chain for debugging purposes */
1179 if (urbp->qh)
1180 uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0);
1181 else
1182 uhci_show_td(td, errbuf, ERRBUF_LEN, 0);
1183
1184 lprintk(errbuf);
1185 }
1186 }
1187
1188 return ret;
1189 }
1190
1191 static void uhci_reset_interrupt(struct urb *urb)
1192 {
1193 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1194 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1195 struct uhci_td *td;
1196 unsigned long flags;
1197
1198 spin_lock_irqsave(&urb->lock, flags);
1199
1200 /* Root hub is special */
1201 if (urb->dev == uhci->rh.dev)
1202 goto out;
1203
1204 td = list_entry(urbp->td_list.next, struct uhci_td, list);
1205
1206 td->status = (td->status & 0x2F000000) | TD_CTRL_ACTIVE | TD_CTRL_IOC;
1207 td->info &= ~(1 << TD_TOKEN_TOGGLE);
1208 td->info |= (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE);
1209 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
1210
1211 out:
1212 urb->status = -EINPROGRESS;
1213
1214 spin_unlock_irqrestore(&urb->lock, flags);
1215 }
1216
1217 /*
1218 * Bulk transfers
1219 */
1220 static int uhci_submit_bulk(struct urb *urb, struct urb *eurb)
1221 {
1222 struct uhci_td *td;
1223 struct uhci_qh *qh;
1224 unsigned long destination, status;
1225 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1226 int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
1227 int len = urb->transfer_buffer_length;
1228 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1229 dma_addr_t data = urbp->transfer_buffer_dma_handle;
1230
1231 if (len < 0)
1232 return -EINVAL;
1233
1234 /* Can't have low speed bulk transfers */
1235 if (urb->pipe & TD_CTRL_LS)
1236 return -EINVAL;
1237
1238 /* The "pipe" thing contains the destination in bits 8--18 */
1239 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1240
1241 /* 3 errors */
1242 status = TD_CTRL_ACTIVE | (3 << TD_CTRL_C_ERR_SHIFT);
1243
1244 if (!(urb->transfer_flags & USB_DISABLE_SPD))
1245 status |= TD_CTRL_SPD;
1246
1247 /*
1248 * Build the DATA TD's
1249 */
1250 do { /* Allow zero length packets */
1251 int pktsze = len;
1252
1253 if (pktsze > maxsze)
1254 pktsze = maxsze;
1255
1256 td = uhci_alloc_td(uhci, urb->dev);
1257 if (!td)
1258 return -ENOMEM;
1259
1260 uhci_add_td_to_urb(urb, td);
1261 uhci_fill_td(td, status, destination | ((pktsze - 1) << 21) |
1262 (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1263 usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE),
1264 data);
1265
1266 data += pktsze;
1267 len -= maxsze;
1268
1269 if (len <= 0)
1270 td->status |= TD_CTRL_IOC;
1271
1272 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1273 usb_pipeout(urb->pipe));
1274 } while (len > 0);
1275
1276 qh = uhci_alloc_qh(uhci, urb->dev);
1277 if (!qh)
1278 return -ENOMEM;
1279
1280 urbp->qh = qh;
1281 qh->urbp = urbp;
1282
1283 /* Always assume breadth first */
1284 uhci_insert_tds_in_qh(qh, urb, 1);
1285
1286 if (urb->transfer_flags & USB_QUEUE_BULK && eurb)
1287 uhci_append_queued_urb(uhci, eurb, urb);
1288 else
1289 uhci_insert_qh(uhci, uhci->skel_bulk_qh, qh);
1290
1291 uhci_inc_fsbr(uhci, urb);
1292
1293 return -EINPROGRESS;
1294 }
1295
1296 /* We can use the result interrupt since they're identical */
1297 #define uhci_result_bulk uhci_result_interrupt
1298
1299 /*
1300 * Isochronous transfers
1301 */
1302 static int isochronous_find_limits(struct urb *urb, unsigned int *start, unsigned int *end)
1303 {
1304 struct urb *last_urb = NULL;
1305 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1306 struct list_head *tmp, *head;
1307 int ret = 0;
1308 unsigned long flags;
1309
1310 spin_lock_irqsave(&uhci->urb_list_lock, flags);
1311 head = &uhci->urb_list;
1312 tmp = head->next;
1313 while (tmp != head) {
1314 struct urb *u = list_entry(tmp, struct urb, urb_list);
1315
1316 tmp = tmp->next;
1317
1318 /* look for pending URB's with identical pipe handle */
1319 if ((urb->pipe == u->pipe) && (urb->dev == u->dev) &&
1320 (u->status == -EINPROGRESS) && (u != urb)) {
1321 if (!last_urb)
1322 *start = u->start_frame;
1323 last_urb = u;
1324 }
1325 }
1326
1327 if (last_urb) {
1328 *end = (last_urb->start_frame + last_urb->number_of_packets) & 1023;
1329 ret = 0;
1330 } else
1331 ret = -1; /* no previous urb found */
1332
1333 spin_unlock_irqrestore(&uhci->urb_list_lock, flags);
1334
1335 return ret;
1336 }
1337
1338 static int isochronous_find_start(struct urb *urb)
1339 {
1340 int limits;
1341 unsigned int start = 0, end = 0;
1342
1343 if (urb->number_of_packets > 900) /* 900? Why? */
1344 return -EFBIG;
1345
1346 limits = isochronous_find_limits(urb, &start, &end);
1347
1348 if (urb->transfer_flags & USB_ISO_ASAP) {
1349 if (limits) {
1350 int curframe;
1351
1352 curframe = uhci_get_current_frame_number(urb->dev) % UHCI_NUMFRAMES;
1353 urb->start_frame = (curframe + 10) % UHCI_NUMFRAMES;
1354 } else
1355 urb->start_frame = end;
1356 } else {
1357 urb->start_frame %= UHCI_NUMFRAMES;
1358 /* FIXME: Sanity check */
1359 }
1360
1361 return 0;
1362 }
1363
1364 /*
1365 * Isochronous transfers
1366 */
1367 static int uhci_submit_isochronous(struct urb *urb)
1368 {
1369 struct uhci_td *td;
1370 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1371 int i, ret, framenum;
1372 int status, destination;
1373 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1374
1375 status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
1376 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1377
1378 ret = isochronous_find_start(urb);
1379 if (ret)
1380 return ret;
1381
1382 framenum = urb->start_frame;
1383 for (i = 0; i < urb->number_of_packets; i++, framenum++) {
1384 if (!urb->iso_frame_desc[i].length)
1385 continue;
1386
1387 td = uhci_alloc_td(uhci, urb->dev);
1388 if (!td)
1389 return -ENOMEM;
1390
1391 uhci_add_td_to_urb(urb, td);
1392 uhci_fill_td(td, status, destination | ((urb->iso_frame_desc[i].length - 1) << 21),
1393 urbp->transfer_buffer_dma_handle + urb->iso_frame_desc[i].offset);
1394
1395 if (i + 1 >= urb->number_of_packets)
1396 td->status |= TD_CTRL_IOC;
1397
1398 uhci_insert_td_frame_list(uhci, td, framenum);
1399 }
1400
1401 return -EINPROGRESS;
1402 }
1403
1404 static int uhci_result_isochronous(struct urb *urb)
1405 {
1406 struct list_head *tmp, *head;
1407 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1408 int status;
1409 int i, ret = 0;
1410
1411 urb->actual_length = 0;
1412
1413 i = 0;
1414 head = &urbp->td_list;
1415 tmp = head->next;
1416 while (tmp != head) {
1417 struct uhci_td *td = list_entry(tmp, struct uhci_td, list);
1418 int actlength;
1419
1420 tmp = tmp->next;
1421
1422 if (td->status & TD_CTRL_ACTIVE)
1423 return -EINPROGRESS;
1424
1425 actlength = uhci_actual_length(td->status);
1426 urb->iso_frame_desc[i].actual_length = actlength;
1427 urb->actual_length += actlength;
1428
1429 status = uhci_map_status(uhci_status_bits(td->status), usb_pipeout(urb->pipe));
1430 urb->iso_frame_desc[i].status = status;
1431 if (status) {
1432 urb->error_count++;
1433 ret = status;
1434 }
1435
1436 i++;
1437 }
1438
1439 return ret;
1440 }
1441
1442 static struct urb *uhci_find_urb_ep(struct uhci *uhci, struct urb *urb)
1443 {
1444 struct list_head *tmp, *head;
1445 unsigned long flags;
1446 struct urb *u = NULL;
1447
1448 /* We don't match Isoc transfers since they are special */
1449 if (usb_pipeisoc(urb->pipe))
1450 return NULL;
1451
1452 spin_lock_irqsave(&uhci->urb_list_lock, flags);
1453 head = &uhci->urb_list;
1454 tmp = head->next;
1455 while (tmp != head) {
1456 u = list_entry(tmp, struct urb, urb_list);
1457
1458 tmp = tmp->next;
1459
1460 if (u->dev == urb->dev && u->pipe == urb->pipe &&
1461 u->status == -EINPROGRESS)
1462 goto out;
1463 }
1464 u = NULL;
1465
1466 out:
1467 spin_unlock_irqrestore(&uhci->urb_list_lock, flags);
1468
1469 return u;
1470 }
1471
1472 static int uhci_submit_urb(struct urb *urb)
1473 {
1474 int ret = -EINVAL;
1475 struct uhci *uhci;
1476 unsigned long flags;
1477 struct urb *eurb;
1478 int bustime;
1479
1480 if (!urb)
1481 return -EINVAL;
1482
1483 if (!urb->dev || !urb->dev->bus || !urb->dev->bus->hcpriv) {
1484 warn("uhci_submit_urb: urb %p belongs to disconnected device or bus?", urb);
1485 return -ENODEV;
1486 }
1487
1488 uhci = (struct uhci *)urb->dev->bus->hcpriv;
1489
1490 INIT_LIST_HEAD(&urb->urb_list);
1491 usb_inc_dev_use(urb->dev);
1492
1493 spin_lock_irqsave(&urb->lock, flags);
1494
1495 if (urb->status == -EINPROGRESS || urb->status == -ECONNRESET ||
1496 urb->status == -ECONNABORTED) {
1497 dbg("uhci_submit_urb: urb not available to submit (status = %d)", urb->status);
1498 /* Since we can have problems on the out path */
1499 spin_unlock_irqrestore(&urb->lock, flags);
1500 usb_dec_dev_use(urb->dev);
1501
1502 return ret;
1503 }
1504
1505 if (!uhci_alloc_urb_priv(uhci, urb)) {
1506 ret = -ENOMEM;
1507
1508 goto out;
1509 }
1510
1511 eurb = uhci_find_urb_ep(uhci, urb);
1512 if (eurb && !(urb->transfer_flags & USB_QUEUE_BULK)) {
1513 ret = -ENXIO;
1514
1515 goto out;
1516 }
1517
1518 /* Short circuit the virtual root hub */
1519 if (urb->dev == uhci->rh.dev) {
1520 ret = rh_submit_urb(urb);
1521
1522 goto out;
1523 }
1524
1525 switch (usb_pipetype(urb->pipe)) {
1526 case PIPE_CONTROL:
1527 ret = uhci_submit_control(urb);
1528 break;
1529 case PIPE_INTERRUPT:
1530 if (urb->bandwidth == 0) { /* not yet checked/allocated */
1531 bustime = usb_check_bandwidth(urb->dev, urb);
1532 if (bustime < 0)
1533 ret = bustime;
1534 else {
1535 ret = uhci_submit_interrupt(urb);
1536 if (ret == -EINPROGRESS)
1537 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1538 }
1539 } else /* bandwidth is already set */
1540 ret = uhci_submit_interrupt(urb);
1541 break;
1542 case PIPE_BULK:
1543 ret = uhci_submit_bulk(urb, eurb);
1544 break;
1545 case PIPE_ISOCHRONOUS:
1546 if (urb->bandwidth == 0) { /* not yet checked/allocated */
1547 if (urb->number_of_packets <= 0) {
1548 ret = -EINVAL;
1549 break;
1550 }
1551 bustime = usb_check_bandwidth(urb->dev, urb);
1552 if (bustime < 0) {
1553 ret = bustime;
1554 break;
1555 }
1556
1557 ret = uhci_submit_isochronous(urb);
1558 if (ret == -EINPROGRESS)
1559 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
1560 } else /* bandwidth is already set */
1561 ret = uhci_submit_isochronous(urb);
1562 break;
1563 }
1564
1565 out:
1566 urb->status = ret;
1567
1568 spin_unlock_irqrestore(&urb->lock, flags);
1569
1570 if (ret == -EINPROGRESS) {
1571 spin_lock_irqsave(&uhci->urb_list_lock, flags);
1572 /* We use _tail to make find_urb_ep more efficient */
1573 list_add_tail(&urb->urb_list, &uhci->urb_list);
1574 spin_unlock_irqrestore(&uhci->urb_list_lock, flags);
1575
1576 return 0;
1577 }
1578
1579 uhci_unlink_generic(uhci, urb);
1580 uhci_destroy_urb_priv(urb);
1581
1582 usb_dec_dev_use(urb->dev);
1583
1584 return ret;
1585 }
1586
1587 /*
1588 * Return the result of a transfer
1589 *
1590 * Must be called with urb_list_lock acquired
1591 */
1592 static void uhci_transfer_result(struct uhci *uhci, struct urb *urb)
1593 {
1594 int ret = -EINVAL;
1595 unsigned long flags;
1596 struct urb_priv *urbp;
1597
1598 /* The root hub is special */
1599 if (urb->dev == uhci->rh.dev)
1600 return;
1601
1602 spin_lock_irqsave(&urb->lock, flags);
1603
1604 urbp = (struct urb_priv *)urb->hcpriv;
1605
1606 if (urb->status != -EINPROGRESS) {
1607 info("uhci_transfer_result: called for URB %p not in flight?", urb);
1608 spin_unlock_irqrestore(&urb->lock, flags);
1609 return;
1610 }
1611
1612 switch (usb_pipetype(urb->pipe)) {
1613 case PIPE_CONTROL:
1614 ret = uhci_result_control(urb);
1615 break;
1616 case PIPE_INTERRUPT:
1617 ret = uhci_result_interrupt(urb);
1618 break;
1619 case PIPE_BULK:
1620 ret = uhci_result_bulk(urb);
1621 break;
1622 case PIPE_ISOCHRONOUS:
1623 ret = uhci_result_isochronous(urb);
1624 break;
1625 }
1626
1627 urbp->status = ret;
1628
1629 spin_unlock_irqrestore(&urb->lock, flags);
1630
1631 if (ret == -EINPROGRESS)
1632 return;
1633
1634 switch (usb_pipetype(urb->pipe)) {
1635 case PIPE_CONTROL:
1636 case PIPE_BULK:
1637 case PIPE_ISOCHRONOUS:
1638 /* Release bandwidth for Interrupt or Isoc. transfers */
1639 /* Spinlock needed ? */
1640 if (urb->bandwidth)
1641 usb_release_bandwidth(urb->dev, urb, 1);
1642 uhci_unlink_generic(uhci, urb);
1643 break;
1644 case PIPE_INTERRUPT:
1645 /* Interrupts are an exception */
1646 if (urb->interval) {
1647 uhci_add_complete(urb);
1648 return; /* <-- note return */
1649 }
1650
1651 /* Release bandwidth for Interrupt or Isoc. transfers */
1652 /* Spinlock needed ? */
1653 if (urb->bandwidth)
1654 usb_release_bandwidth(urb->dev, urb, 0);
1655 uhci_unlink_generic(uhci, urb);
1656 break;
1657 default:
1658 info("uhci_transfer_result: unknown pipe type %d for urb %p\n",
1659 usb_pipetype(urb->pipe), urb);
1660 }
1661
1662 list_del_init(&urb->urb_list);
1663
1664 uhci_add_complete(urb);
1665 }
1666
1667 static void uhci_unlink_generic(struct uhci *uhci, struct urb *urb)
1668 {
1669 struct urb_priv *urbp = urb->hcpriv;
1670
1671 /* We can get called when urbp allocation fails, so check */
1672 if (!urbp)
1673 return;
1674
1675 uhci_dec_fsbr(uhci, urb); /* Safe since it checks */
1676
1677 uhci_delete_queued_urb(uhci, urb);
1678
1679 if (urbp->qh)
1680 /* The interrupt loop will reclaim the QH's */
1681 uhci_remove_qh(uhci, urbp->qh);
1682 }
1683
1684 /* FIXME: If we forcefully unlink an urb, we should reset the toggle for */
1685 /* that pipe to match what actually completed */
1686 static int uhci_unlink_urb(struct urb *urb)
1687 {
1688 struct uhci *uhci;
1689 unsigned long flags;
1690 struct urb_priv *urbp = urb->hcpriv;
1691
1692 if (!urb)
1693 return -EINVAL;
1694
1695 if (!urb->dev || !urb->dev->bus || !urb->dev->bus->hcpriv)
1696 return -ENODEV;
1697
1698 uhci = (struct uhci *)urb->dev->bus->hcpriv;
1699
1700 /* Release bandwidth for Interrupt or Isoc. transfers */
1701 /* Spinlock needed ? */
1702 if (urb->bandwidth) {
1703 switch (usb_pipetype(urb->pipe)) {
1704 case PIPE_INTERRUPT:
1705 usb_release_bandwidth(urb->dev, urb, 0);
1706 break;
1707 case PIPE_ISOCHRONOUS:
1708 usb_release_bandwidth(urb->dev, urb, 1);
1709 break;
1710 default:
1711 break;
1712 }
1713 }
1714
1715 if (urb->status != -EINPROGRESS)
1716 return 0;
1717
1718 spin_lock_irqsave(&uhci->urb_list_lock, flags);
1719 list_del_init(&urb->urb_list);
1720 spin_unlock_irqrestore(&uhci->urb_list_lock, flags);
1721
1722 uhci_unlink_generic(uhci, urb);
1723
1724 /* Short circuit the virtual root hub */
1725 if (urb->dev == uhci->rh.dev) {
1726 rh_unlink_urb(urb);
1727 uhci_call_completion(urb);
1728 } else {
1729 if (urb->transfer_flags & USB_ASYNC_UNLINK) {
1730 /* urb_list is available now since we called */
1731 /* uhci_unlink_generic already */
1732
1733 urbp->status = urb->status = -ECONNABORTED;
1734
1735 spin_lock_irqsave(&uhci->urb_remove_list_lock, flags);
1736
1737 /* Check to see if the remove list is empty */
1738 if (list_empty(&uhci->urb_remove_list))
1739 uhci_set_next_interrupt(uhci);
1740
1741 list_add(&urb->urb_list, &uhci->urb_remove_list);
1742
1743 spin_unlock_irqrestore(&uhci->urb_remove_list_lock, flags);
1744 } else {
1745 urb->status = -ENOENT;
1746
1747 if (in_interrupt()) { /* wait at least 1 frame */
1748 static int errorcount = 10;
1749
1750 if (errorcount--)
1751 dbg("uhci_unlink_urb called from interrupt for urb %p", urb);
1752 udelay(1000);
1753 } else
1754 schedule_timeout(1+1*HZ/1000);
1755
1756 uhci_call_completion(urb);
1757 }
1758 }
1759
1760 return 0;
1761 }
1762
1763 static int uhci_fsbr_timeout(struct uhci *uhci, struct urb *urb)
1764 {
1765 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1766 struct list_head *head, *tmp;
1767
1768 uhci_dec_fsbr(uhci, urb);
1769
1770 /* There is a race with updating IOC in here, but it's not worth */
1771 /* trying to fix since this is merely an optimization. The only */
1772 /* time we'd lose is if the status of the packet got updated */
1773 /* and we'd be turning on FSBR next frame anyway, so it's a wash */
1774 urbp->fsbr_timeout = 1;
1775
1776 head = &urbp->td_list;
1777 tmp = head->next;
1778 while (tmp != head) {
1779 struct uhci_td *td = list_entry(tmp, struct uhci_td, list);
1780
1781 tmp = tmp->next;
1782
1783 if (td->status & TD_CTRL_ACTIVE) {
1784 td->status |= TD_CTRL_IOC;
1785 break;
1786 }
1787 }
1788
1789 return 0;
1790 }
1791
1792 /*
1793 * uhci_get_current_frame_number()
1794 *
1795 * returns the current frame number for a USB bus/controller.
1796 */
1797 static int uhci_get_current_frame_number(struct usb_device *dev)
1798 {
1799 struct uhci *uhci = (struct uhci *)dev->bus->hcpriv;
1800
1801 return inw(uhci->io_addr + USBFRNUM);
1802 }
1803
1804 struct usb_operations uhci_device_operations = {
1805 uhci_alloc_dev,
1806 uhci_free_dev,
1807 uhci_get_current_frame_number,
1808 uhci_submit_urb,
1809 uhci_unlink_urb
1810 };
1811
1812 /* Virtual Root Hub */
1813
1814 static __u8 root_hub_dev_des[] =
1815 {
1816 0x12, /* __u8 bLength; */
1817 0x01, /* __u8 bDescriptorType; Device */
1818 0x00, /* __u16 bcdUSB; v1.0 */
1819 0x01,
1820 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1821 0x00, /* __u8 bDeviceSubClass; */
1822 0x00, /* __u8 bDeviceProtocol; */
1823 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1824 0x00, /* __u16 idVendor; */
1825 0x00,
1826 0x00, /* __u16 idProduct; */
1827 0x00,
1828 0x00, /* __u16 bcdDevice; */
1829 0x00,
1830 0x00, /* __u8 iManufacturer; */
1831 0x02, /* __u8 iProduct; */
1832 0x01, /* __u8 iSerialNumber; */
1833 0x01 /* __u8 bNumConfigurations; */
1834 };
1835
1836
1837 /* Configuration descriptor */
1838 static __u8 root_hub_config_des[] =
1839 {
1840 0x09, /* __u8 bLength; */
1841 0x02, /* __u8 bDescriptorType; Configuration */
1842 0x19, /* __u16 wTotalLength; */
1843 0x00,
1844 0x01, /* __u8 bNumInterfaces; */
1845 0x01, /* __u8 bConfigurationValue; */
1846 0x00, /* __u8 iConfiguration; */
1847 0x40, /* __u8 bmAttributes;
1848 Bit 7: Bus-powered, 6: Self-powered,
1849 Bit 5 Remote-wakeup, 4..0: resvd */
1850 0x00, /* __u8 MaxPower; */
1851
1852 /* interface */
1853 0x09, /* __u8 if_bLength; */
1854 0x04, /* __u8 if_bDescriptorType; Interface */
1855 0x00, /* __u8 if_bInterfaceNumber; */
1856 0x00, /* __u8 if_bAlternateSetting; */
1857 0x01, /* __u8 if_bNumEndpoints; */
1858 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1859 0x00, /* __u8 if_bInterfaceSubClass; */
1860 0x00, /* __u8 if_bInterfaceProtocol; */
1861 0x00, /* __u8 if_iInterface; */
1862
1863 /* endpoint */
1864 0x07, /* __u8 ep_bLength; */
1865 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1866 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1867 0x03, /* __u8 ep_bmAttributes; Interrupt */
1868 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
1869 0x00,
1870 0xff /* __u8 ep_bInterval; 255 ms */
1871 };
1872
1873 static __u8 root_hub_hub_des[] =
1874 {
1875 0x09, /* __u8 bLength; */
1876 0x29, /* __u8 bDescriptorType; Hub-descriptor */
1877 0x02, /* __u8 bNbrPorts; */
1878 0x00, /* __u16 wHubCharacteristics; */
1879 0x00,
1880 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
1881 0x00, /* __u8 bHubContrCurrent; 0 mA */
1882 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
1883 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
1884 };
1885
1886 /* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */
1887 static int rh_send_irq(struct urb *urb)
1888 {
1889 int i, len = 1;
1890 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1891 unsigned int io_addr = uhci->io_addr;
1892 __u16 data = 0;
1893 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1894
1895 for (i = 0; i < uhci->rh.numports; i++) {
1896 data |= ((inw(io_addr + USBPORTSC1 + i * 2) & 0xa) > 0 ? (1 << (i + 1)) : 0);
1897 len = (i + 1) / 8 + 1;
1898 }
1899
1900 *(__u16 *) urb->transfer_buffer = cpu_to_le16(data);
1901 urb->actual_length = len;
1902 urbp->status = 0;
1903
1904 if ((data > 0) && (uhci->rh.send != 0)) {
1905 dbg("root-hub INT complete: port1: %x port2: %x data: %x",
1906 inw(io_addr + USBPORTSC1), inw(io_addr + USBPORTSC2), data);
1907 uhci_call_completion(urb);
1908 }
1909
1910 return 0;
1911 }
1912
1913 /* Virtual Root Hub INTs are polled by this timer every "interval" ms */
1914 static int rh_init_int_timer(struct urb *urb);
1915
1916 static void rh_int_timer_do(unsigned long ptr)
1917 {
1918 struct urb *urb = (struct urb *)ptr;
1919 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1920 struct list_head list, *tmp, *head;
1921 unsigned long flags;
1922
1923 if (uhci->rh.send)
1924 rh_send_irq(urb);
1925
1926 INIT_LIST_HEAD(&list);
1927
1928 spin_lock_irqsave(&uhci->urb_list_lock, flags);
1929 head = &uhci->urb_list;
1930
1931 tmp = head->next;
1932 while (tmp != head) {
1933 struct urb *u = list_entry(tmp, struct urb, urb_list);
1934 struct urb_priv *urbp = (struct urb_priv *)u->hcpriv;
1935
1936 tmp = tmp->next;
1937
1938 /* Check if the FSBR timed out */
1939 if (urbp->fsbr && time_after_eq(jiffies, urbp->inserttime + IDLE_TIMEOUT))
1940 uhci_fsbr_timeout(uhci, u);
1941
1942 /* Check if the URB timed out */
1943 if (u->timeout && time_after_eq(jiffies, u->timeout)) {
1944 list_del(&u->urb_list);
1945 list_add_tail(&u->urb_list, &list);
1946 }
1947 }
1948 spin_unlock_irqrestore(&uhci->urb_list_lock, flags);
1949
1950 head = &list;
1951 tmp = head->next;
1952 while (tmp != head) {
1953 struct urb *u = list_entry(tmp, struct urb, urb_list);
1954
1955 tmp = tmp->next;
1956
1957 u->transfer_flags |= USB_ASYNC_UNLINK | USB_TIMEOUT_KILLED;
1958 uhci_unlink_urb(u);
1959 }
1960
1961 /* enter global suspend if nothing connected */
1962 if (!uhci->is_suspended && !ports_active(uhci))
1963 suspend_hc(uhci);
1964
1965 rh_init_int_timer(urb);
1966 }
1967
1968 /* Root Hub INTs are polled by this timer */
1969 static int rh_init_int_timer(struct urb *urb)
1970 {
1971 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
1972
1973 uhci->rh.interval = urb->interval;
1974 init_timer(&uhci->rh.rh_int_timer);
1975 uhci->rh.rh_int_timer.function = rh_int_timer_do;
1976 uhci->rh.rh_int_timer.data = (unsigned long)urb;
1977 uhci->rh.rh_int_timer.expires = jiffies + (HZ * (urb->interval < 30 ? 30 : urb->interval)) / 1000;
1978 add_timer(&uhci->rh.rh_int_timer);
1979
1980 return 0;
1981 }
1982
1983 #define OK(x) len = (x); break
1984
1985 #define CLR_RH_PORTSTAT(x) \
1986 status = inw(io_addr + USBPORTSC1 + 2 * (wIndex-1)); \
1987 status = (status & 0xfff5) & ~(x); \
1988 outw(status, io_addr + USBPORTSC1 + 2 * (wIndex-1))
1989
1990 #define SET_RH_PORTSTAT(x) \
1991 status = inw(io_addr + USBPORTSC1 + 2 * (wIndex-1)); \
1992 status = (status & 0xfff5) | (x); \
1993 outw(status, io_addr + USBPORTSC1 + 2 * (wIndex-1))
1994
1995
1996 /* Root Hub Control Pipe */
1997 static int rh_submit_urb(struct urb *urb)
1998 {
1999 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
2000 unsigned int pipe = urb->pipe;
2001 devrequest *cmd = (devrequest *)urb->setup_packet;
2002 void *data = urb->transfer_buffer;
2003 int leni = urb->transfer_buffer_length;
2004 int len = 0;
2005 int status = 0;
2006 int stat = 0;
2007 int i;
2008 unsigned int io_addr = uhci->io_addr;
2009 __u16 cstatus;
2010 __u16 bmRType_bReq;
2011 __u16 wValue;
2012 __u16 wIndex;
2013 __u16 wLength;
2014
2015 if (usb_pipetype(pipe) == PIPE_INTERRUPT) {
2016 uhci->rh.urb = urb;
2017 uhci->rh.send = 1;
2018 uhci->rh.interval = urb->interval;
2019 rh_init_int_timer(urb);
2020
2021 return -EINPROGRESS;
2022 }
2023
2024 bmRType_bReq = cmd->requesttype | cmd->request << 8;
2025 wValue = le16_to_cpu(cmd->value);
2026 wIndex = le16_to_cpu(cmd->index);
2027 wLength = le16_to_cpu(cmd->length);
2028
2029 for (i = 0; i < 8; i++)
2030 uhci->rh.c_p_r[i] = 0;
2031
2032 switch (bmRType_bReq) {
2033 /* Request Destination:
2034 without flags: Device,
2035 RH_INTERFACE: interface,
2036 RH_ENDPOINT: endpoint,
2037 RH_CLASS means HUB here,
2038 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
2039 */
2040
2041 case RH_GET_STATUS:
2042 *(__u16 *)data = cpu_to_le16(1);
2043 OK(2);
2044 case RH_GET_STATUS | RH_INTERFACE:
2045 *(__u16 *)data = cpu_to_le16(0);
2046 OK(2);
2047 case RH_GET_STATUS | RH_ENDPOINT:
2048 *(__u16 *)data = cpu_to_le16(0);
2049 OK(2);
2050 case RH_GET_STATUS | RH_CLASS:
2051 *(__u32 *)data = cpu_to_le32(0);
2052 OK(4); /* hub power */
2053 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
2054 status = inw(io_addr + USBPORTSC1 + 2 * (wIndex - 1));
2055 cstatus = ((status & USBPORTSC_CSC) >> (1 - 0)) |
2056 ((status & USBPORTSC_PEC) >> (3 - 1)) |
2057 (uhci->rh.c_p_r[wIndex - 1] << (0 + 4));
2058 status = (status & USBPORTSC_CCS) |
2059 ((status & USBPORTSC_PE) >> (2 - 1)) |
2060 ((status & USBPORTSC_SUSP) >> (12 - 2)) |
2061 ((status & USBPORTSC_PR) >> (9 - 4)) |
2062 (1 << 8) | /* power on */
2063 ((status & USBPORTSC_LSDA) << (-8 + 9));
2064
2065 *(__u16 *)data = cpu_to_le16(status);
2066 *(__u16 *)(data + 2) = cpu_to_le16(cstatus);
2067 OK(4);
2068 case RH_CLEAR_FEATURE | RH_ENDPOINT:
2069 switch (wValue) {
2070 case RH_ENDPOINT_STALL:
2071 OK(0);
2072 }
2073 break;
2074 case RH_CLEAR_FEATURE | RH_CLASS:
2075 switch (wValue) {
2076 case RH_C_HUB_OVER_CURRENT:
2077 OK(0); /* hub power over current */
2078 }
2079 break;
2080 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
2081 switch (wValue) {
2082 case RH_PORT_ENABLE:
2083 CLR_RH_PORTSTAT(USBPORTSC_PE);
2084 OK(0);
2085 case RH_PORT_SUSPEND:
2086 CLR_RH_PORTSTAT(USBPORTSC_SUSP);
2087 OK(0);
2088 case RH_PORT_POWER:
2089 OK(0); /* port power */
2090 case RH_C_PORT_CONNECTION:
2091 SET_RH_PORTSTAT(USBPORTSC_CSC);
2092 OK(0);
2093 case RH_C_PORT_ENABLE:
2094 SET_RH_PORTSTAT(USBPORTSC_PEC);
2095 OK(0);
2096 case RH_C_PORT_SUSPEND:
2097 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
2098 OK(0);
2099 case RH_C_PORT_OVER_CURRENT:
2100 OK(0); /* port power over current */
2101 case RH_C_PORT_RESET:
2102 uhci->rh.c_p_r[wIndex - 1] = 0;
2103 OK(0);
2104 }
2105 break;
2106 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
2107 switch (wValue) {
2108 case RH_PORT_SUSPEND:
2109 SET_RH_PORTSTAT(USBPORTSC_SUSP);
2110 OK(0);
2111 case RH_PORT_RESET:
2112 SET_RH_PORTSTAT(USBPORTSC_PR);
2113 wait_ms(50); /* USB v1.1 7.1.7.3 */
2114 uhci->rh.c_p_r[wIndex - 1] = 1;
2115 CLR_RH_PORTSTAT(USBPORTSC_PR);
2116 udelay(10);
2117 SET_RH_PORTSTAT(USBPORTSC_PE);
2118 wait_ms(10);
2119 SET_RH_PORTSTAT(0xa);
2120 OK(0);
2121 case RH_PORT_POWER:
2122 OK(0); /* port power ** */
2123 case RH_PORT_ENABLE:
2124 SET_RH_PORTSTAT(USBPORTSC_PE);
2125 OK(0);
2126 }
2127 break;
2128 case RH_SET_ADDRESS:
2129 uhci->rh.devnum = wValue;
2130 OK(0);
2131 case RH_GET_DESCRIPTOR:
2132 switch ((wValue & 0xff00) >> 8) {
2133 case 0x01: /* device descriptor */
2134 len = min_t(unsigned int, leni,
2135 min_t(unsigned int,
2136 sizeof(root_hub_dev_des), wLength));
2137 memcpy(data, root_hub_dev_des, len);
2138 OK(len);
2139 case 0x02: /* configuration descriptor */
2140 len = min_t(unsigned int, leni,
2141 min_t(unsigned int,
2142 sizeof(root_hub_config_des), wLength));
2143 memcpy (data, root_hub_config_des, len);
2144 OK(len);
2145 case 0x03: /* string descriptors */
2146 len = usb_root_hub_string (wValue & 0xff,
2147 uhci->io_addr, "UHCI-alt",
2148 data, wLength);
2149 if (len > 0) {
2150 OK(min_t(int, leni, len));
2151 } else
2152 stat = -EPIPE;
2153 }
2154 break;
2155 case RH_GET_DESCRIPTOR | RH_CLASS:
2156 root_hub_hub_des[2] = uhci->rh.numports;
2157 len = min_t(unsigned int, leni,
2158 min_t(unsigned int, sizeof(root_hub_hub_des), wLength));
2159 memcpy(data, root_hub_hub_des, len);
2160 OK(len);
2161 case RH_GET_CONFIGURATION:
2162 *(__u8 *)data = 0x01;
2163 OK(1);
2164 case RH_SET_CONFIGURATION:
2165 OK(0);
2166 case RH_GET_INTERFACE | RH_INTERFACE:
2167 *(__u8 *)data = 0x00;
2168 OK(1);
2169 case RH_SET_INTERFACE | RH_INTERFACE:
2170 OK(0);
2171 default:
2172 stat = -EPIPE;
2173 }
2174
2175 urb->actual_length = len;
2176
2177 return stat;
2178 }
2179
2180 static int rh_unlink_urb(struct urb *urb)
2181 {
2182 struct uhci *uhci = (struct uhci *)urb->dev->bus->hcpriv;
2183
2184 if (uhci->rh.urb == urb) {
2185 urb->status = -ENOENT;
2186 uhci->rh.send = 0;
2187 uhci->rh.urb = NULL;
2188 del_timer(&uhci->rh.rh_int_timer);
2189 }
2190 return 0;
2191 }
2192
2193 static void uhci_free_pending_qhs(struct uhci *uhci)
2194 {
2195 struct list_head *tmp, *head;
2196 unsigned long flags;
2197
2198 spin_lock_irqsave(&uhci->qh_remove_list_lock, flags);
2199 head = &uhci->qh_remove_list;
2200 tmp = head->next;
2201 while (tmp != head) {
2202 struct uhci_qh *qh = list_entry(tmp, struct uhci_qh, remove_list);
2203
2204 tmp = tmp->next;
2205
2206 list_del_init(&qh->remove_list);
2207
2208 uhci_free_qh(uhci, qh);
2209 }
2210 spin_unlock_irqrestore(&uhci->qh_remove_list_lock, flags);
2211 }
2212
2213 static void uhci_call_completion(struct urb *urb)
2214 {
2215 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
2216 struct usb_device *dev = urb->dev;
2217 struct uhci *uhci = (struct uhci *)dev->bus->hcpriv;
2218 int is_ring = 0, killed, resubmit_interrupt, status;
2219 struct urb *nurb;
2220
2221 killed = (urb->status == -ENOENT || urb->status == -ECONNABORTED ||
2222 urb->status == -ECONNRESET);
2223 resubmit_interrupt = (usb_pipetype(urb->pipe) == PIPE_INTERRUPT &&
2224 urb->interval && !killed);
2225
2226 nurb = urb->next;
2227 if (nurb && !killed) {
2228 int count = 0;
2229
2230 while (nurb && nurb != urb && count < MAX_URB_LOOP) {
2231 if (nurb->status == -ENOENT ||
2232 nurb->status == -ECONNABORTED ||
2233 nurb->status == -ECONNRESET) {
2234 killed = 1;
2235 break;
2236 }
2237
2238 nurb = nurb->next;
2239 count++;
2240 }
2241
2242 if (count == MAX_URB_LOOP)
2243 err("uhci_call_completion: too many linked URB's, loop? (first loop)");
2244
2245 /* Check to see if chain is a ring */
2246 is_ring = (nurb == urb);
2247 }
2248
2249 status = urbp->status;
2250 if (!resubmit_interrupt)
2251 /* We don't need urb_priv anymore */
2252 uhci_destroy_urb_priv(urb);
2253
2254 if (!killed)
2255 urb->status = status;
2256
2257 if (urbp->transfer_buffer_dma_handle)
2258 pci_dma_sync_single(uhci->dev, urbp->transfer_buffer_dma_handle,
2259 urb->transfer_buffer_length, usb_pipein(urb->pipe) ?
2260 PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
2261
2262 if (urbp->setup_packet_dma_handle)
2263 pci_dma_sync_single(uhci->dev, urbp->setup_packet_dma_handle,
2264 sizeof(devrequest), PCI_DMA_TODEVICE);
2265
2266 urb->dev = NULL;
2267 if (urb->complete)
2268 urb->complete(urb);
2269
2270 if (resubmit_interrupt) {
2271 urb->dev = dev;
2272 uhci_reset_interrupt(urb);
2273 } else {
2274 if (is_ring && !killed) {
2275 urb->dev = dev;
2276 uhci_submit_urb(urb);
2277 } else {
2278 /* We decrement the usage count after we're done */
2279 /* with everything */
2280 usb_dec_dev_use(dev);
2281 }
2282 }
2283 }
2284
2285 static void uhci_finish_completion(struct uhci *uhci)
2286 {
2287 struct list_head *tmp, *head;
2288 unsigned long flags;
2289
2290 spin_lock_irqsave(&uhci->complete_list_lock, flags);
2291 head = &uhci->complete_list;
2292 tmp = head->next;
2293 while (tmp != head) {
2294 struct urb_priv *urbp = list_entry(tmp, struct urb_priv, complete_list);
2295 struct urb *urb = urbp->urb;
2296
2297 tmp = tmp->next;
2298
2299 list_del_init(&urbp->complete_list);
2300
2301 uhci_call_completion(urb);
2302 }
2303 spin_unlock_irqrestore(&uhci->complete_list_lock, flags);
2304 }
2305
2306 static void uhci_remove_pending_qhs(struct uhci *uhci)
2307 {
2308 struct list_head *tmp, *head;
2309 unsigned long flags;
2310
2311 spin_lock_irqsave(&uhci->urb_remove_list_lock, flags);
2312 head = &uhci->urb_remove_list;
2313 tmp = head->next;
2314 while (tmp != head) {
2315 struct urb *urb = list_entry(tmp, struct urb, urb_list);
2316 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
2317
2318 tmp = tmp->next;
2319
2320 list_del_init(&urb->urb_list);
2321
2322 urbp->status = urb->status = -ECONNRESET;
2323 uhci_call_completion(urb);
2324 }
2325 spin_unlock_irqrestore(&uhci->urb_remove_list_lock, flags);
2326 }
2327
2328 static void uhci_interrupt(int irq, void *__uhci, struct pt_regs *regs)
2329 {
2330 struct uhci *uhci = __uhci;
2331 unsigned int io_addr = uhci->io_addr;
2332 unsigned short status;
2333 struct list_head *tmp, *head;
2334
2335 /*
2336 * Read the interrupt status, and write it back to clear the
2337 * interrupt cause
2338 */
2339 status = inw(io_addr + USBSTS);
2340 if (!status) /* shared interrupt, not mine */
2341 return;
2342 outw(status, io_addr + USBSTS); /* Clear it */
2343
2344 if (status & ~(USBSTS_USBINT | USBSTS_ERROR | USBSTS_RD)) {
2345 if (status & USBSTS_HSE)
2346 printk(KERN_ERR "uhci: host system error, PCI problems?\n");
2347 if (status & USBSTS_HCPE)
2348 printk(KERN_ERR "uhci: host controller process error. something bad happened\n");
2349 if ((status & USBSTS_HCH) && !uhci->is_suspended) {
2350 printk(KERN_ERR "uhci: host controller halted. very bad\n");
2351 /* FIXME: Reset the controller, fix the offending TD */
2352 }
2353 }
2354
2355 if (status & USBSTS_RD)
2356 wakeup_hc(uhci);
2357
2358 uhci_free_pending_qhs(uhci);
2359
2360 uhci_remove_pending_qhs(uhci);
2361
2362 uhci_clear_next_interrupt(uhci);
2363
2364 /* Walk the list of pending URB's to see which ones completed */
2365 spin_lock(&uhci->urb_list_lock);
2366 head = &uhci->urb_list;
2367 tmp = head->next;
2368 while (tmp != head) {
2369 struct urb *urb = list_entry(tmp, struct urb, urb_list);
2370
2371 tmp = tmp->next;
2372
2373 /* Checks the status and does all of the magic necessary */
2374 uhci_transfer_result(uhci, urb);
2375 }
2376 spin_unlock(&uhci->urb_list_lock);
2377
2378 uhci_finish_completion(uhci);
2379 }
2380
2381 static void reset_hc(struct uhci *uhci)
2382 {
2383 unsigned int io_addr = uhci->io_addr;
2384
2385 /* Global reset for 50ms */
2386 outw(USBCMD_GRESET, io_addr + USBCMD);
2387 wait_ms(50);
2388 outw(0, io_addr + USBCMD);
2389 wait_ms(10);
2390 }
2391
2392 static void suspend_hc(struct uhci *uhci)
2393 {
2394 unsigned int io_addr = uhci->io_addr;
2395
2396 dbg("suspend_hc");
2397
2398 outw(USBCMD_EGSM, io_addr + USBCMD);
2399
2400 uhci->is_suspended = 1;
2401 }
2402
2403 static void wakeup_hc(struct uhci *uhci)
2404 {
2405 unsigned int io_addr = uhci->io_addr;
2406 unsigned int status;
2407
2408 dbg("wakeup_hc");
2409
2410 outw(0, io_addr + USBCMD);
2411
2412 /* wait for EOP to be sent */
2413 status = inw(io_addr + USBCMD);
2414 while (status & USBCMD_FGR)
2415 status = inw(io_addr + USBCMD);
2416
2417 uhci->is_suspended = 0;
2418
2419 /* Run and mark it configured with a 64-byte max packet */
2420 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, io_addr + USBCMD);
2421 }
2422
2423 static int ports_active(struct uhci *uhci)
2424 {
2425 unsigned int io_addr = uhci->io_addr;
2426 int connection = 0;
2427 int i;
2428
2429 for (i = 0; i < uhci->rh.numports; i++)
2430 connection |= (inw(io_addr + USBPORTSC1 + i * 2) & 0x1);
2431
2432 return connection;
2433 }
2434
2435 static void start_hc(struct uhci *uhci)
2436 {
2437 unsigned int io_addr = uhci->io_addr;
2438 int timeout = 1000;
2439
2440 /*
2441 * Reset the HC - this will force us to get a
2442 * new notification of any already connected
2443 * ports due to the virtual disconnect that it
2444 * implies.
2445 */
2446 outw(USBCMD_HCRESET, io_addr + USBCMD);
2447 while (inw(io_addr + USBCMD) & USBCMD_HCRESET) {
2448 if (!--timeout) {
2449 printk(KERN_ERR "uhci: USBCMD_HCRESET timed out!\n");
2450 break;
2451 }
2452 }
2453
2454 /* Turn on all interrupts */
2455 outw(USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP,
2456 io_addr + USBINTR);
2457
2458 /* Start at frame 0 */
2459 outw(0, io_addr + USBFRNUM);
2460 outl(uhci->fl->dma_handle, io_addr + USBFLBASEADD);
2461
2462 /* Run and mark it configured with a 64-byte max packet */
2463 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, io_addr + USBCMD);
2464 }
2465
2466 static int uhci_alloc_root_hub(struct uhci *uhci)
2467 {
2468 struct usb_device *dev;
2469
2470 dev = usb_alloc_dev(NULL, uhci->bus);
2471 if (!dev)
2472 return -1;
2473
2474 uhci->bus->root_hub = dev;
2475 uhci->rh.dev = dev;
2476
2477 return 0;
2478 }
2479
2480 static int uhci_start_root_hub(struct uhci *uhci)
2481 {
2482 usb_connect(uhci->rh.dev);
2483
2484 if (usb_new_device(uhci->rh.dev) != 0) {
2485 usb_free_dev(uhci->rh.dev);
2486
2487 return -1;
2488 }
2489
2490 return 0;
2491 }
2492
2493 #ifdef CONFIG_PROC_FS
2494 static int uhci_num = 0;
2495 #endif
2496
2497 /*
2498 * Allocate a frame list, and then setup the skeleton
2499 *
2500 * The hardware doesn't really know any difference
2501 * in the queues, but the order does matter for the
2502 * protocols higher up. The order is:
2503 *
2504 * - any isochronous events handled before any
2505 * of the queues. We don't do that here, because
2506 * we'll create the actual TD entries on demand.
2507 * - The first queue is the "interrupt queue".
2508 * - The second queue is the "control queue", split into low and high speed
2509 * - The third queue is "bulk data".
2510 */
2511 static struct uhci *alloc_uhci(struct pci_dev *dev, unsigned int io_addr, unsigned int io_size)
2512 {
2513 int i, port;
2514 struct uhci *uhci;
2515 struct usb_bus *bus;
2516 dma_addr_t dma_handle;
2517
2518 uhci = kmalloc(sizeof(*uhci), GFP_KERNEL);
2519 if (!uhci)
2520 return NULL;
2521
2522 memset(uhci, 0, sizeof(*uhci));
2523
2524 uhci->dev = dev;
2525 uhci->irq = -1;
2526 uhci->io_addr = io_addr;
2527 uhci->io_size = io_size;
2528
2529 spin_lock_init(&uhci->qh_remove_list_lock);
2530 INIT_LIST_HEAD(&uhci->qh_remove_list);
2531
2532 spin_lock_init(&uhci->urb_remove_list_lock);
2533 INIT_LIST_HEAD(&uhci->urb_remove_list);
2534
2535 spin_lock_init(&uhci->urb_list_lock);
2536 INIT_LIST_HEAD(&uhci->urb_list);
2537
2538 spin_lock_init(&uhci->complete_list_lock);
2539 INIT_LIST_HEAD(&uhci->complete_list);
2540
2541 spin_lock_init(&uhci->frame_list_lock);
2542
2543 /* We need exactly one page (per UHCI specs), how convenient */
2544 /* We assume that one page is atleast 4k (1024 frames * 4 bytes) */
2545 uhci->fl = pci_alloc_consistent(uhci->dev, sizeof(*uhci->fl), &dma_handle);
2546 if (!uhci->fl) {
2547 printk(KERN_ERR "Unable to allocate consistent memory for frame list\n");
2548 goto free_uhci;
2549 }
2550
2551 memset((void *)uhci->fl, 0, sizeof(*uhci->fl));
2552
2553 uhci->fl->dma_handle = dma_handle;
2554
2555 uhci->td_pool = pci_pool_create("uhci_td", uhci->dev,
2556 sizeof(struct uhci_td), 16, 0, GFP_DMA | GFP_ATOMIC);
2557 if (!uhci->td_pool) {
2558 printk(KERN_ERR "Unable to create td pci_pool\n");
2559 goto free_fl;
2560 }
2561
2562 uhci->qh_pool = pci_pool_create("uhci_qh", uhci->dev,
2563 sizeof(struct uhci_qh), 16, 0, GFP_DMA | GFP_ATOMIC);
2564 if (!uhci->qh_pool) {
2565 printk(KERN_ERR "Unable to create qh pci_pool\n");
2566 goto free_td_pool;
2567 }
2568
2569 bus = usb_alloc_bus(&uhci_device_operations);
2570 if (!bus)
2571 goto free_qh_pool;
2572
2573 uhci->bus = bus;
2574 bus->hcpriv = uhci;
2575
2576 /* Initialize the root hub */
2577
2578 /* UHCI specs says devices must have 2 ports, but goes on to say */
2579 /* they may have more but give no way to determine how many they */
2580 /* have. However, according to the UHCI spec, Bit 7 is always set */
2581 /* to 1. So we try to use this to our advantage */
2582 for (port = 0; port < (io_size - 0x10) / 2; port++) {
2583 unsigned int portstatus;
2584
2585 portstatus = inw(io_addr + 0x10 + (port * 2));
2586 if (!(portstatus & 0x0080))
2587 break;
2588 }
2589 if (debug)
2590 info("detected %d ports", port);
2591
2592 /* This is experimental so anything less than 2 or greater than 8 is */
2593 /* something weird and we'll ignore it */
2594 if (port < 2 || port > 8) {
2595 info("port count misdetected? forcing to 2 ports");
2596 port = 2;
2597 }
2598
2599 uhci->rh.numports = port;
2600
2601 if (uhci_alloc_root_hub(uhci)) {
2602 err("unable to allocate root hub");
2603 goto free_fl;
2604 }
2605
2606 uhci->skeltd[0] = uhci_alloc_td(uhci, uhci->rh.dev);
2607 if (!uhci->skeltd[0]) {
2608 err("unable to allocate TD 0");
2609 goto free_fl;
2610 }
2611
2612 /*
2613 * 9 Interrupt queues; link int2 to int1, int4 to int2, etc
2614 * then link int1 to control and control to bulk
2615 */
2616 for (i = 1; i < 9; i++) {
2617 struct uhci_td *td;
2618
2619 td = uhci->skeltd[i] = uhci_alloc_td(uhci, uhci->rh.dev);
2620 if (!td) {
2621 err("unable to allocate TD %d", i);
2622 goto free_tds;
2623 }
2624
2625 uhci_fill_td(td, 0, (UHCI_NULL_DATA_SIZE << 21) | (0x7f << 8) | USB_PID_IN, 0);
2626 td->link = uhci->skeltd[i - 1]->dma_handle;
2627 }
2628
2629 uhci->skel_term_td = uhci_alloc_td(uhci, uhci->rh.dev);
2630 if (!uhci->skel_term_td) {
2631 err("unable to allocate TD 0");
2632 goto free_fl;
2633 }
2634
2635 for (i = 0; i < UHCI_NUM_SKELQH; i++) {
2636 uhci->skelqh[i] = uhci_alloc_qh(uhci, uhci->rh.dev);
2637 if (!uhci->skelqh[i]) {
2638 err("unable to allocate QH %d", i);
2639 goto free_qhs;
2640 }
2641 }
2642
2643 uhci_fill_td(uhci->skel_int1_td, 0, (UHCI_NULL_DATA_SIZE << 21) | (0x7f << 8) | USB_PID_IN, 0);
2644 uhci->skel_int1_td->link = uhci->skel_ls_control_qh->dma_handle | UHCI_PTR_QH;
2645
2646 uhci->skel_ls_control_qh->link = uhci->skel_hs_control_qh->dma_handle | UHCI_PTR_QH;
2647 uhci->skel_ls_control_qh->element = UHCI_PTR_TERM;
2648
2649 uhci->skel_hs_control_qh->link = uhci->skel_bulk_qh->dma_handle | UHCI_PTR_QH;
2650 uhci->skel_hs_control_qh->element = UHCI_PTR_TERM;
2651
2652 uhci->skel_bulk_qh->link = uhci->skel_term_qh->dma_handle | UHCI_PTR_QH;
2653 uhci->skel_bulk_qh->element = UHCI_PTR_TERM;
2654
2655 /* This dummy TD is to work around a bug in Intel PIIX controllers */
2656 uhci_fill_td(uhci->skel_term_td, 0, (UHCI_NULL_DATA_SIZE << 21) | (0x7f << 8) | USB_PID_IN, 0);
2657 uhci->skel_term_td->link = uhci->skel_term_td->dma_handle;
2658
2659 uhci->skel_term_qh->link = UHCI_PTR_TERM;
2660 uhci->skel_term_qh->element = uhci->skel_term_td->dma_handle;
2661
2662 /*
2663 * Fill the frame list: make all entries point to
2664 * the proper interrupt queue.
2665 *
2666 * This is probably silly, but it's a simple way to
2667 * scatter the interrupt queues in a way that gives
2668 * us a reasonable dynamic range for irq latencies.
2669 */
2670 for (i = 0; i < UHCI_NUMFRAMES; i++) {
2671 int irq = 0;
2672
2673 if (i & 1) {
2674 irq++;
2675 if (i & 2) {
2676 irq++;
2677 if (i & 4) {
2678 irq++;
2679 if (i & 8) {
2680 irq++;
2681 if (i & 16) {
2682 irq++;
2683 if (i & 32) {
2684 irq++;
2685 if (i & 64)
2686 irq++;
2687 }
2688 }
2689 }
2690 }
2691 }
2692 }
2693
2694 /* Only place we don't use the frame list routines */
2695 uhci->fl->frame[i] = uhci->skeltd[irq]->dma_handle;
2696 }
2697
2698 return uhci;
2699
2700 /*
2701 * error exits:
2702 */
2703 free_qhs:
2704 for (i = 0; i < UHCI_NUM_SKELQH; i++)
2705 if (uhci->skelqh[i]) {
2706 uhci_free_qh(uhci, uhci->skelqh[i]);
2707 uhci->skelqh[i] = NULL;
2708 }
2709
2710 free_tds:
2711 for (i = 0; i < UHCI_NUM_SKELTD; i++)
2712 if (uhci->skeltd[i]) {
2713 uhci_free_td(uhci, uhci->skeltd[i]);
2714 uhci->skeltd[i] = NULL;
2715 }
2716
2717 free_qh_pool:
2718 pci_pool_destroy(uhci->qh_pool);
2719
2720 free_td_pool:
2721 pci_pool_destroy(uhci->td_pool);
2722
2723 free_fl:
2724 pci_free_consistent(uhci->dev, sizeof(*uhci->fl), uhci->fl, uhci->fl->dma_handle);
2725
2726 free_uhci:
2727 kfree(uhci);
2728
2729 return NULL;
2730 }
2731
2732 /*
2733 * De-allocate all resources..
2734 */
2735 static void release_uhci(struct uhci *uhci)
2736 {
2737 int i;
2738 #ifdef CONFIG_PROC_FS
2739 char buf[8];
2740 #endif
2741
2742 if (uhci->irq >= 0) {
2743 free_irq(uhci->irq, uhci);
2744 uhci->irq = -1;
2745 }
2746
2747 for (i = 0; i < UHCI_NUM_SKELQH; i++)
2748 if (uhci->skelqh[i]) {
2749 uhci_free_qh(uhci, uhci->skelqh[i]);
2750 uhci->skelqh[i] = NULL;
2751 }
2752
2753 for (i = 0; i < UHCI_NUM_SKELTD; i++)
2754 if (uhci->skeltd[i]) {
2755 uhci_free_td(uhci, uhci->skeltd[i]);
2756 uhci->skeltd[i] = NULL;
2757 }
2758
2759 if (uhci->qh_pool) {
2760 pci_pool_destroy(uhci->qh_pool);
2761 uhci->qh_pool = NULL;
2762 }
2763
2764 if (uhci->td_pool) {
2765 pci_pool_destroy(uhci->td_pool);
2766 uhci->td_pool = NULL;
2767 }
2768
2769 if (uhci->fl) {
2770 pci_free_consistent(uhci->dev, sizeof(*uhci->fl), uhci->fl, uhci->fl->dma_handle);
2771 uhci->fl = NULL;
2772 }
2773
2774 usb_free_bus(uhci->bus);
2775
2776 #ifdef CONFIG_PROC_FS
2777 sprintf(buf, "hc%d", uhci->num);
2778
2779 remove_proc_entry(buf, uhci_proc_root);
2780 uhci->proc_entry = NULL;
2781 #endif
2782
2783 kfree(uhci);
2784 }
2785
2786 /*
2787 * If we've successfully found a UHCI, now is the time to return success..
2788 */
2789 static int setup_uhci(struct pci_dev *dev, int irq, unsigned int io_addr, unsigned int io_size)
2790 {
2791 int retval;
2792 struct uhci *uhci;
2793 char buf[8], *bufp = buf;
2794 #ifdef CONFIG_PROC_FS
2795 struct proc_dir_entry *ent;
2796 #endif
2797
2798 #ifndef __sparc__
2799 sprintf(buf, "%d", irq);
2800 #else
2801 bufp = __irq_itoa(irq);
2802 #endif
2803 printk(KERN_INFO __FILE__ ": USB UHCI at I/O 0x%x, IRQ %s\n",
2804 io_addr, bufp);
2805
2806 uhci = alloc_uhci(dev, io_addr, io_size);
2807 if (!uhci)
2808 return -ENOMEM;
2809
2810 dev->driver_data = uhci;
2811
2812 #ifdef CONFIG_PROC_FS
2813 uhci->num = uhci_num++;
2814
2815 sprintf(buf, "hc%d", uhci->num);
2816
2817 ent = create_proc_entry(buf, S_IFREG|S_IRUGO|S_IWUSR, uhci_proc_root);
2818 if (!ent)
2819 return -ENOMEM;
2820
2821 ent->data = uhci;
2822 ent->proc_fops = &uhci_proc_operations;
2823 ent->size = 0;
2824 uhci->proc_entry = ent;
2825 #endif
2826
2827 request_region(uhci->io_addr, io_size, "usb-uhci");
2828
2829 reset_hc(uhci);
2830
2831 usb_register_bus(uhci->bus);
2832 start_hc(uhci);
2833
2834 retval = -EBUSY;
2835 if (request_irq(irq, uhci_interrupt, SA_SHIRQ, "usb-uhci", uhci) == 0) {
2836 uhci->irq = irq;
2837
2838 pci_write_config_word(dev, USBLEGSUP, USBLEGSUP_DEFAULT);
2839
2840 if (!uhci_start_root_hub(uhci))
2841 return 0;
2842 }
2843
2844 /* Couldn't allocate IRQ if we got here */
2845
2846 reset_hc(uhci);
2847 release_region(uhci->io_addr, uhci->io_size);
2848 release_uhci(uhci);
2849
2850 return retval;
2851 }
2852
2853 static int __devinit uhci_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
2854 {
2855 int i;
2856
2857 if (!pci_dma_supported(dev, 0xFFFFFFFF)) {
2858 err("PCI subsystem doesn't support 32 bit addressing?");
2859 return -ENODEV;
2860 }
2861 dev->dma_mask = 0xFFFFFFFF;
2862
2863 /* disable legacy emulation */
2864 pci_write_config_word(dev, USBLEGSUP, 0);
2865
2866 if (pci_enable_device(dev) < 0)
2867 return -ENODEV;
2868
2869 if (!dev->irq) {
2870 err("found UHCI device with no IRQ assigned. check BIOS settings!");
2871 return -ENODEV;
2872 }
2873
2874 /* Search for the IO base address.. */
2875 for (i = 0; i < 6; i++) {
2876 unsigned int io_addr = pci_resource_start(dev, i);
2877 unsigned int io_size = pci_resource_len(dev, i);
2878
2879 /* IO address? */
2880 if (!(pci_resource_flags(dev, i) & IORESOURCE_IO))
2881 continue;
2882
2883 /* Is it already in use? */
2884 if (check_region(io_addr, io_size))
2885 break;
2886
2887 pci_set_master(dev);
2888 return setup_uhci(dev, dev->irq, io_addr, io_size);
2889 }
2890
2891 return -ENODEV;
2892 }
2893
2894 static void __devexit uhci_pci_remove(struct pci_dev *dev)
2895 {
2896 struct uhci *uhci = dev->driver_data;
2897
2898 if (uhci->bus->root_hub)
2899 usb_disconnect(&uhci->bus->root_hub);
2900
2901 usb_deregister_bus(uhci->bus);
2902
2903 /*
2904 * At this point, we're guaranteed that no new connects can be made
2905 * to this bus since there are no more parents
2906 */
2907 uhci_free_pending_qhs(uhci);
2908 uhci_remove_pending_qhs(uhci);
2909
2910 reset_hc(uhci);
2911 release_region(uhci->io_addr, uhci->io_size);
2912
2913 uhci_free_pending_qhs(uhci);
2914
2915 release_uhci(uhci);
2916 }
2917
2918 #ifdef CONFIG_PM
2919 static int uhci_pci_suspend(struct pci_dev *dev, u32 state)
2920 {
2921 reset_hc((struct uhci *) dev->driver_data);
2922 return 0;
2923 }
2924
2925 static int uhci_pci_resume(struct pci_dev *dev)
2926 {
2927 reset_hc((struct uhci *) dev->driver_data);
2928 start_hc((struct uhci *) dev->driver_data);
2929 return 0;
2930 }
2931 #endif
2932
2933 static const struct pci_device_id __devinitdata uhci_pci_ids[] = { {
2934
2935 /* handle any USB UHCI controller */
2936 class: ((PCI_CLASS_SERIAL_USB << 8) | 0x00),
2937 class_mask: ~0,
2938
2939 /* no matter who makes it */
2940 vendor: PCI_ANY_ID,
2941 device: PCI_ANY_ID,
2942 subvendor: PCI_ANY_ID,
2943 subdevice: PCI_ANY_ID,
2944
2945 }, { /* end: all zeroes */ }
2946 };
2947
2948 MODULE_DEVICE_TABLE (pci, uhci_pci_ids);
2949
2950 static struct pci_driver uhci_pci_driver = {
2951 name: "usb-uhci",
2952 id_table: &uhci_pci_ids [0],
2953
2954 probe: uhci_pci_probe,
2955 remove: uhci_pci_remove,
2956
2957 #ifdef CONFIG_PM
2958 suspend: uhci_pci_suspend,
2959 resume: uhci_pci_resume,
2960 #endif /* PM */
2961 };
2962
2963
2964 static int __init uhci_hcd_init(void)
2965 {
2966 int retval = -ENOMEM;
2967
2968 if (debug) {
2969 errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
2970 if (!errbuf)
2971 goto errbuf_failed;
2972 }
2973
2974 #ifdef CONFIG_PROC_FS
2975 uhci_proc_root = create_proc_entry("driver/uhci", S_IFDIR, 0);
2976 if (!uhci_proc_root)
2977 goto proc_failed;
2978 #endif
2979
2980 uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
2981 sizeof(struct urb_priv), 0, 0, NULL, NULL);
2982 if (!uhci_up_cachep)
2983 goto up_failed;
2984
2985 retval = pci_module_init(&uhci_pci_driver);
2986 if (retval)
2987 goto init_failed;
2988
2989 info(DRIVER_VERSION ":" DRIVER_DESC);
2990
2991 return 0;
2992
2993 init_failed:
2994 if (kmem_cache_destroy(uhci_up_cachep))
2995 printk(KERN_INFO "uhci: not all urb_priv's were freed\n");
2996
2997 up_failed:
2998
2999 #ifdef CONFIG_PROC_FS
3000 remove_proc_entry("uhci", 0);
3001
3002 proc_failed:
3003 #endif
3004 if (errbuf)
3005 kfree(errbuf);
3006
3007 errbuf_failed:
3008
3009 return retval;
3010 }
3011
3012 static void __exit uhci_hcd_cleanup (void)
3013 {
3014 pci_unregister_driver(&uhci_pci_driver);
3015
3016 if (kmem_cache_destroy(uhci_up_cachep))
3017 printk(KERN_INFO "uhci: not all urb_priv's were freed\n");
3018
3019 #ifdef CONFIG_PROC_FS
3020 remove_proc_entry("uhci", 0);
3021 #endif
3022
3023 if (errbuf)
3024 kfree(errbuf);
3025 }
3026
3027 module_init(uhci_hcd_init);
3028 module_exit(uhci_hcd_cleanup);
3029
3030 MODULE_AUTHOR( DRIVER_AUTHOR );
3031 MODULE_DESCRIPTION( DRIVER_DESC );
3032
3033