mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / usb / host / ohci-q.c
blob4e9f6a45f4e46412ec451a42ad11f368ae217f46
1 /*
2 * OHCI HCD (Host Controller Driver) for USB.
4 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5 * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
7 * This file is licenced under the GPL.
8 */
10 #include <linux/irq.h>
11 #include <linux/slab.h>
13 static void urb_free_priv (struct ohci_hcd *hc, urb_priv_t *urb_priv)
15 int last = urb_priv->length - 1;
17 if (last >= 0) {
18 int i;
19 struct td *td;
21 for (i = 0; i <= last; i++) {
22 td = urb_priv->td [i];
23 if (td)
24 td_free (hc, td);
28 list_del (&urb_priv->pending);
29 kfree (urb_priv);
32 /*-------------------------------------------------------------------------*/
35 * URB goes back to driver, and isn't reissued.
36 * It's completely gone from HC data structures.
37 * PRECONDITION: ohci lock held, irqs blocked.
39 static void
40 finish_urb(struct ohci_hcd *ohci, struct urb *urb, int status)
41 __releases(ohci->lock)
42 __acquires(ohci->lock)
44 struct device *dev = ohci_to_hcd(ohci)->self.controller;
45 struct usb_host_endpoint *ep = urb->ep;
46 struct urb_priv *urb_priv;
48 // ASSERT (urb->hcpriv != 0);
50 restart:
51 urb_free_priv (ohci, urb->hcpriv);
52 urb->hcpriv = NULL;
53 if (likely(status == -EINPROGRESS))
54 status = 0;
56 switch (usb_pipetype (urb->pipe)) {
57 case PIPE_ISOCHRONOUS:
58 ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs--;
59 if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0) {
60 if (quirk_amdiso(ohci))
61 usb_amd_quirk_pll_enable();
62 if (quirk_amdprefetch(ohci))
63 sb800_prefetch(dev, 0);
65 break;
66 case PIPE_INTERRUPT:
67 ohci_to_hcd(ohci)->self.bandwidth_int_reqs--;
68 break;
71 #ifdef OHCI_VERBOSE_DEBUG
72 urb_print(urb, "RET", usb_pipeout (urb->pipe), status);
73 #endif
75 /* urb->complete() can reenter this HCD */
76 usb_hcd_unlink_urb_from_ep(ohci_to_hcd(ohci), urb);
77 spin_unlock (&ohci->lock);
78 usb_hcd_giveback_urb(ohci_to_hcd(ohci), urb, status);
79 spin_lock (&ohci->lock);
81 /* stop periodic dma if it's not needed */
82 if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0
83 && ohci_to_hcd(ohci)->self.bandwidth_int_reqs == 0) {
84 ohci->hc_control &= ~(OHCI_CTRL_PLE|OHCI_CTRL_IE);
85 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
89 * An isochronous URB that is sumitted too late won't have any TDs
90 * (marked by the fact that the td_cnt value is larger than the
91 * actual number of TDs). If the next URB on this endpoint is like
92 * that, give it back now.
94 if (!list_empty(&ep->urb_list)) {
95 urb = list_first_entry(&ep->urb_list, struct urb, urb_list);
96 urb_priv = urb->hcpriv;
97 if (urb_priv->td_cnt > urb_priv->length) {
98 status = 0;
99 goto restart;
105 /*-------------------------------------------------------------------------*
106 * ED handling functions
107 *-------------------------------------------------------------------------*/
109 /* search for the right schedule branch to use for a periodic ed.
110 * does some load balancing; returns the branch, or negative errno.
112 static int balance (struct ohci_hcd *ohci, int interval, int load)
114 int i, branch = -ENOSPC;
116 /* iso periods can be huge; iso tds specify frame numbers */
117 if (interval > NUM_INTS)
118 interval = NUM_INTS;
120 /* search for the least loaded schedule branch of that period
121 * that has enough bandwidth left unreserved.
123 for (i = 0; i < interval ; i++) {
124 if (branch < 0 || ohci->load [branch] > ohci->load [i]) {
125 int j;
127 /* usb 1.1 says 90% of one frame */
128 for (j = i; j < NUM_INTS; j += interval) {
129 if ((ohci->load [j] + load) > 900)
130 break;
132 if (j < NUM_INTS)
133 continue;
134 branch = i;
137 return branch;
140 /*-------------------------------------------------------------------------*/
142 /* both iso and interrupt requests have periods; this routine puts them
143 * into the schedule tree in the apppropriate place. most iso devices use
144 * 1msec periods, but that's not required.
146 static void periodic_link (struct ohci_hcd *ohci, struct ed *ed)
148 unsigned i;
150 ohci_vdbg (ohci, "link %sed %p branch %d [%dus.], interval %d\n",
151 (ed->hwINFO & cpu_to_hc32 (ohci, ED_ISO)) ? "iso " : "",
152 ed, ed->branch, ed->load, ed->interval);
154 for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
155 struct ed **prev = &ohci->periodic [i];
156 __hc32 *prev_p = &ohci->hcca->int_table [i];
157 struct ed *here = *prev;
159 /* sorting each branch by period (slow before fast)
160 * lets us share the faster parts of the tree.
161 * (plus maybe: put interrupt eds before iso)
163 while (here && ed != here) {
164 if (ed->interval > here->interval)
165 break;
166 prev = &here->ed_next;
167 prev_p = &here->hwNextED;
168 here = *prev;
170 if (ed != here) {
171 ed->ed_next = here;
172 if (here)
173 ed->hwNextED = *prev_p;
174 wmb ();
175 *prev = ed;
176 *prev_p = cpu_to_hc32(ohci, ed->dma);
177 wmb();
179 ohci->load [i] += ed->load;
181 ohci_to_hcd(ohci)->self.bandwidth_allocated += ed->load / ed->interval;
184 /* link an ed into one of the HC chains */
186 static int ed_schedule (struct ohci_hcd *ohci, struct ed *ed)
188 int branch;
190 ed->state = ED_OPER;
191 ed->ed_prev = NULL;
192 ed->ed_next = NULL;
193 ed->hwNextED = 0;
194 if (quirk_zfmicro(ohci)
195 && (ed->type == PIPE_INTERRUPT)
196 && !(ohci->eds_scheduled++))
197 mod_timer(&ohci->unlink_watchdog, round_jiffies(jiffies + HZ));
198 wmb ();
200 /* we care about rm_list when setting CLE/BLE in case the HC was at
201 * work on some TD when CLE/BLE was turned off, and isn't quiesced
202 * yet. finish_unlinks() restarts as needed, some upcoming INTR_SF.
204 * control and bulk EDs are doubly linked (ed_next, ed_prev), but
205 * periodic ones are singly linked (ed_next). that's because the
206 * periodic schedule encodes a tree like figure 3-5 in the ohci
207 * spec: each qh can have several "previous" nodes, and the tree
208 * doesn't have unused/idle descriptors.
210 switch (ed->type) {
211 case PIPE_CONTROL:
212 if (ohci->ed_controltail == NULL) {
213 WARN_ON (ohci->hc_control & OHCI_CTRL_CLE);
214 ohci_writel (ohci, ed->dma,
215 &ohci->regs->ed_controlhead);
216 } else {
217 ohci->ed_controltail->ed_next = ed;
218 ohci->ed_controltail->hwNextED = cpu_to_hc32 (ohci,
219 ed->dma);
221 ed->ed_prev = ohci->ed_controltail;
222 if (!ohci->ed_controltail && !ohci->ed_rm_list) {
223 wmb();
224 ohci->hc_control |= OHCI_CTRL_CLE;
225 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
226 ohci_writel (ohci, ohci->hc_control,
227 &ohci->regs->control);
229 ohci->ed_controltail = ed;
230 break;
232 case PIPE_BULK:
233 if (ohci->ed_bulktail == NULL) {
234 WARN_ON (ohci->hc_control & OHCI_CTRL_BLE);
235 ohci_writel (ohci, ed->dma, &ohci->regs->ed_bulkhead);
236 } else {
237 ohci->ed_bulktail->ed_next = ed;
238 ohci->ed_bulktail->hwNextED = cpu_to_hc32 (ohci,
239 ed->dma);
241 ed->ed_prev = ohci->ed_bulktail;
242 if (!ohci->ed_bulktail && !ohci->ed_rm_list) {
243 wmb();
244 ohci->hc_control |= OHCI_CTRL_BLE;
245 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
246 ohci_writel (ohci, ohci->hc_control,
247 &ohci->regs->control);
249 ohci->ed_bulktail = ed;
250 break;
252 // case PIPE_INTERRUPT:
253 // case PIPE_ISOCHRONOUS:
254 default:
255 branch = balance (ohci, ed->interval, ed->load);
256 if (branch < 0) {
257 ohci_dbg (ohci,
258 "ERR %d, interval %d msecs, load %d\n",
259 branch, ed->interval, ed->load);
260 // FIXME if there are TDs queued, fail them!
261 return branch;
263 ed->branch = branch;
264 periodic_link (ohci, ed);
267 /* the HC may not see the schedule updates yet, but if it does
268 * then they'll be properly ordered.
270 return 0;
273 /*-------------------------------------------------------------------------*/
275 /* scan the periodic table to find and unlink this ED */
276 static void periodic_unlink (struct ohci_hcd *ohci, struct ed *ed)
278 int i;
280 for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
281 struct ed *temp;
282 struct ed **prev = &ohci->periodic [i];
283 __hc32 *prev_p = &ohci->hcca->int_table [i];
285 while (*prev && (temp = *prev) != ed) {
286 prev_p = &temp->hwNextED;
287 prev = &temp->ed_next;
289 if (*prev) {
290 *prev_p = ed->hwNextED;
291 *prev = ed->ed_next;
293 ohci->load [i] -= ed->load;
295 ohci_to_hcd(ohci)->self.bandwidth_allocated -= ed->load / ed->interval;
297 ohci_vdbg (ohci, "unlink %sed %p branch %d [%dus.], interval %d\n",
298 (ed->hwINFO & cpu_to_hc32 (ohci, ED_ISO)) ? "iso " : "",
299 ed, ed->branch, ed->load, ed->interval);
302 /* unlink an ed from one of the HC chains.
303 * just the link to the ed is unlinked.
304 * the link from the ed still points to another operational ed or 0
305 * so the HC can eventually finish the processing of the unlinked ed
306 * (assuming it already started that, which needn't be true).
308 * ED_UNLINK is a transient state: the HC may still see this ED, but soon
309 * it won't. ED_SKIP means the HC will finish its current transaction,
310 * but won't start anything new. The TD queue may still grow; device
311 * drivers don't know about this HCD-internal state.
313 * When the HC can't see the ED, something changes ED_UNLINK to one of:
315 * - ED_OPER: when there's any request queued, the ED gets rescheduled
316 * immediately. HC should be working on them.
318 * - ED_IDLE: when there's no TD queue or the HC isn't running.
320 * When finish_unlinks() runs later, after SOF interrupt, it will often
321 * complete one or more URB unlinks before making that state change.
323 static void ed_deschedule (struct ohci_hcd *ohci, struct ed *ed)
325 ed->hwINFO |= cpu_to_hc32 (ohci, ED_SKIP);
326 wmb ();
327 ed->state = ED_UNLINK;
329 /* To deschedule something from the control or bulk list, just
330 * clear CLE/BLE and wait. There's no safe way to scrub out list
331 * head/current registers until later, and "later" isn't very
332 * tightly specified. Figure 6-5 and Section 6.4.2.2 show how
333 * the HC is reading the ED queues (while we modify them).
335 * For now, ed_schedule() is "later". It might be good paranoia
336 * to scrub those registers in finish_unlinks(), in case of bugs
337 * that make the HC try to use them.
339 switch (ed->type) {
340 case PIPE_CONTROL:
341 /* remove ED from the HC's list: */
342 if (ed->ed_prev == NULL) {
343 if (!ed->hwNextED) {
344 ohci->hc_control &= ~OHCI_CTRL_CLE;
345 ohci_writel (ohci, ohci->hc_control,
346 &ohci->regs->control);
347 // a ohci_readl() later syncs CLE with the HC
348 } else
349 ohci_writel (ohci,
350 hc32_to_cpup (ohci, &ed->hwNextED),
351 &ohci->regs->ed_controlhead);
352 } else {
353 ed->ed_prev->ed_next = ed->ed_next;
354 ed->ed_prev->hwNextED = ed->hwNextED;
356 /* remove ED from the HCD's list: */
357 if (ohci->ed_controltail == ed) {
358 ohci->ed_controltail = ed->ed_prev;
359 if (ohci->ed_controltail)
360 ohci->ed_controltail->ed_next = NULL;
361 } else if (ed->ed_next) {
362 ed->ed_next->ed_prev = ed->ed_prev;
364 break;
366 case PIPE_BULK:
367 /* remove ED from the HC's list: */
368 if (ed->ed_prev == NULL) {
369 if (!ed->hwNextED) {
370 ohci->hc_control &= ~OHCI_CTRL_BLE;
371 ohci_writel (ohci, ohci->hc_control,
372 &ohci->regs->control);
373 // a ohci_readl() later syncs BLE with the HC
374 } else
375 ohci_writel (ohci,
376 hc32_to_cpup (ohci, &ed->hwNextED),
377 &ohci->regs->ed_bulkhead);
378 } else {
379 ed->ed_prev->ed_next = ed->ed_next;
380 ed->ed_prev->hwNextED = ed->hwNextED;
382 /* remove ED from the HCD's list: */
383 if (ohci->ed_bulktail == ed) {
384 ohci->ed_bulktail = ed->ed_prev;
385 if (ohci->ed_bulktail)
386 ohci->ed_bulktail->ed_next = NULL;
387 } else if (ed->ed_next) {
388 ed->ed_next->ed_prev = ed->ed_prev;
390 break;
392 // case PIPE_INTERRUPT:
393 // case PIPE_ISOCHRONOUS:
394 default:
395 periodic_unlink (ohci, ed);
396 break;
401 /*-------------------------------------------------------------------------*/
403 /* get and maybe (re)init an endpoint. init _should_ be done only as part
404 * of enumeration, usb_set_configuration() or usb_set_interface().
406 static struct ed *ed_get (
407 struct ohci_hcd *ohci,
408 struct usb_host_endpoint *ep,
409 struct usb_device *udev,
410 unsigned int pipe,
411 int interval
413 struct ed *ed;
414 unsigned long flags;
416 spin_lock_irqsave (&ohci->lock, flags);
418 if (!(ed = ep->hcpriv)) {
419 struct td *td;
420 int is_out;
421 u32 info;
423 ed = ed_alloc (ohci, GFP_ATOMIC);
424 if (!ed) {
425 /* out of memory */
426 goto done;
429 /* dummy td; end of td list for ed */
430 td = td_alloc (ohci, GFP_ATOMIC);
431 if (!td) {
432 /* out of memory */
433 ed_free (ohci, ed);
434 ed = NULL;
435 goto done;
437 ed->dummy = td;
438 ed->hwTailP = cpu_to_hc32 (ohci, td->td_dma);
439 ed->hwHeadP = ed->hwTailP; /* ED_C, ED_H zeroed */
440 ed->state = ED_IDLE;
442 is_out = !(ep->desc.bEndpointAddress & USB_DIR_IN);
444 /* FIXME usbcore changes dev->devnum before SET_ADDRESS
445 * succeeds ... otherwise we wouldn't need "pipe".
447 info = usb_pipedevice (pipe);
448 ed->type = usb_pipetype(pipe);
450 info |= (ep->desc.bEndpointAddress & ~USB_DIR_IN) << 7;
451 info |= usb_endpoint_maxp(&ep->desc) << 16;
452 if (udev->speed == USB_SPEED_LOW)
453 info |= ED_LOWSPEED;
454 /* only control transfers store pids in tds */
455 if (ed->type != PIPE_CONTROL) {
456 info |= is_out ? ED_OUT : ED_IN;
457 if (ed->type != PIPE_BULK) {
458 /* periodic transfers... */
459 if (ed->type == PIPE_ISOCHRONOUS)
460 info |= ED_ISO;
461 else if (interval > 32) /* iso can be bigger */
462 interval = 32;
463 ed->interval = interval;
464 ed->load = usb_calc_bus_time (
465 udev->speed, !is_out,
466 ed->type == PIPE_ISOCHRONOUS,
467 usb_endpoint_maxp(&ep->desc))
468 / 1000;
471 ed->hwINFO = cpu_to_hc32(ohci, info);
473 ep->hcpriv = ed;
476 done:
477 spin_unlock_irqrestore (&ohci->lock, flags);
478 return ed;
481 /*-------------------------------------------------------------------------*/
483 /* request unlinking of an endpoint from an operational HC.
484 * put the ep on the rm_list
485 * real work is done at the next start frame (SF) hardware interrupt
486 * caller guarantees HCD is running, so hardware access is safe,
487 * and that ed->state is ED_OPER
489 static void start_ed_unlink (struct ohci_hcd *ohci, struct ed *ed)
491 ed->hwINFO |= cpu_to_hc32 (ohci, ED_DEQUEUE);
492 ed_deschedule (ohci, ed);
494 /* rm_list is just singly linked, for simplicity */
495 ed->ed_next = ohci->ed_rm_list;
496 ed->ed_prev = NULL;
497 ohci->ed_rm_list = ed;
499 /* enable SOF interrupt */
500 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
501 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
502 // flush those writes, and get latest HCCA contents
503 (void) ohci_readl (ohci, &ohci->regs->control);
505 /* SF interrupt might get delayed; record the frame counter value that
506 * indicates when the HC isn't looking at it, so concurrent unlinks
507 * behave. frame_no wraps every 2^16 msec, and changes right before
508 * SF is triggered.
510 ed->tick = ohci_frame_no(ohci) + 1;
514 /*-------------------------------------------------------------------------*
515 * TD handling functions
516 *-------------------------------------------------------------------------*/
518 /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
520 static void
521 td_fill (struct ohci_hcd *ohci, u32 info,
522 dma_addr_t data, int len,
523 struct urb *urb, int index)
525 struct td *td, *td_pt;
526 struct urb_priv *urb_priv = urb->hcpriv;
527 int is_iso = info & TD_ISO;
528 int hash;
530 // ASSERT (index < urb_priv->length);
532 /* aim for only one interrupt per urb. mostly applies to control
533 * and iso; other urbs rarely need more than one TD per urb.
534 * this way, only final tds (or ones with an error) cause IRQs.
535 * at least immediately; use DI=6 in case any control request is
536 * tempted to die part way through. (and to force the hc to flush
537 * its donelist soonish, even on unlink paths.)
539 * NOTE: could delay interrupts even for the last TD, and get fewer
540 * interrupts ... increasing per-urb latency by sharing interrupts.
541 * Drivers that queue bulk urbs may request that behavior.
543 if (index != (urb_priv->length - 1)
544 || (urb->transfer_flags & URB_NO_INTERRUPT))
545 info |= TD_DI_SET (6);
547 /* use this td as the next dummy */
548 td_pt = urb_priv->td [index];
550 /* fill the old dummy TD */
551 td = urb_priv->td [index] = urb_priv->ed->dummy;
552 urb_priv->ed->dummy = td_pt;
554 td->ed = urb_priv->ed;
555 td->next_dl_td = NULL;
556 td->index = index;
557 td->urb = urb;
558 td->data_dma = data;
559 if (!len)
560 data = 0;
562 td->hwINFO = cpu_to_hc32 (ohci, info);
563 if (is_iso) {
564 td->hwCBP = cpu_to_hc32 (ohci, data & 0xFFFFF000);
565 *ohci_hwPSWp(ohci, td, 0) = cpu_to_hc16 (ohci,
566 (data & 0x0FFF) | 0xE000);
567 } else {
568 td->hwCBP = cpu_to_hc32 (ohci, data);
570 if (data)
571 td->hwBE = cpu_to_hc32 (ohci, data + len - 1);
572 else
573 td->hwBE = 0;
574 td->hwNextTD = cpu_to_hc32 (ohci, td_pt->td_dma);
576 /* append to queue */
577 list_add_tail (&td->td_list, &td->ed->td_list);
579 /* hash it for later reverse mapping */
580 hash = TD_HASH_FUNC (td->td_dma);
581 td->td_hash = ohci->td_hash [hash];
582 ohci->td_hash [hash] = td;
584 /* HC might read the TD (or cachelines) right away ... */
585 wmb ();
586 td->ed->hwTailP = td->hwNextTD;
589 /*-------------------------------------------------------------------------*/
591 /* Prepare all TDs of a transfer, and queue them onto the ED.
592 * Caller guarantees HC is active.
593 * Usually the ED is already on the schedule, so TDs might be
594 * processed as soon as they're queued.
596 static void td_submit_urb (
597 struct ohci_hcd *ohci,
598 struct urb *urb
600 struct urb_priv *urb_priv = urb->hcpriv;
601 struct device *dev = ohci_to_hcd(ohci)->self.controller;
602 dma_addr_t data;
603 int data_len = urb->transfer_buffer_length;
604 int cnt = 0;
605 u32 info = 0;
606 int is_out = usb_pipeout (urb->pipe);
607 int periodic = 0;
609 /* OHCI handles the bulk/interrupt data toggles itself. We just
610 * use the device toggle bits for resetting, and rely on the fact
611 * that resetting toggle is meaningless if the endpoint is active.
613 if (!usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe), is_out)) {
614 usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe),
615 is_out, 1);
616 urb_priv->ed->hwHeadP &= ~cpu_to_hc32 (ohci, ED_C);
619 list_add (&urb_priv->pending, &ohci->pending);
621 if (data_len)
622 data = urb->transfer_dma;
623 else
624 data = 0;
626 /* NOTE: TD_CC is set so we can tell which TDs the HC processed by
627 * using TD_CC_GET, as well as by seeing them on the done list.
628 * (CC = NotAccessed ... 0x0F, or 0x0E in PSWs for ISO.)
630 switch (urb_priv->ed->type) {
632 /* Bulk and interrupt are identical except for where in the schedule
633 * their EDs live.
635 case PIPE_INTERRUPT:
636 /* ... and periodic urbs have extra accounting */
637 periodic = ohci_to_hcd(ohci)->self.bandwidth_int_reqs++ == 0
638 && ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0;
639 /* FALLTHROUGH */
640 case PIPE_BULK:
641 info = is_out
642 ? TD_T_TOGGLE | TD_CC | TD_DP_OUT
643 : TD_T_TOGGLE | TD_CC | TD_DP_IN;
644 /* TDs _could_ transfer up to 8K each */
645 while (data_len > 4096) {
646 td_fill (ohci, info, data, 4096, urb, cnt);
647 data += 4096;
648 data_len -= 4096;
649 cnt++;
651 /* maybe avoid ED halt on final TD short read */
652 if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
653 info |= TD_R;
654 td_fill (ohci, info, data, data_len, urb, cnt);
655 cnt++;
656 if ((urb->transfer_flags & URB_ZERO_PACKET)
657 && cnt < urb_priv->length) {
658 td_fill (ohci, info, 0, 0, urb, cnt);
659 cnt++;
661 /* maybe kickstart bulk list */
662 if (urb_priv->ed->type == PIPE_BULK) {
663 wmb ();
664 ohci_writel (ohci, OHCI_BLF, &ohci->regs->cmdstatus);
666 break;
668 /* control manages DATA0/DATA1 toggle per-request; SETUP resets it,
669 * any DATA phase works normally, and the STATUS ack is special.
671 case PIPE_CONTROL:
672 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
673 td_fill (ohci, info, urb->setup_dma, 8, urb, cnt++);
674 if (data_len > 0) {
675 info = TD_CC | TD_R | TD_T_DATA1;
676 info |= is_out ? TD_DP_OUT : TD_DP_IN;
677 /* NOTE: mishandles transfers >8K, some >4K */
678 td_fill (ohci, info, data, data_len, urb, cnt++);
680 info = (is_out || data_len == 0)
681 ? TD_CC | TD_DP_IN | TD_T_DATA1
682 : TD_CC | TD_DP_OUT | TD_T_DATA1;
683 td_fill (ohci, info, data, 0, urb, cnt++);
684 /* maybe kickstart control list */
685 wmb ();
686 ohci_writel (ohci, OHCI_CLF, &ohci->regs->cmdstatus);
687 break;
689 /* ISO has no retransmit, so no toggle; and it uses special TDs.
690 * Each TD could handle multiple consecutive frames (interval 1);
691 * we could often reduce the number of TDs here.
693 case PIPE_ISOCHRONOUS:
694 for (cnt = urb_priv->td_cnt; cnt < urb->number_of_packets;
695 cnt++) {
696 int frame = urb->start_frame;
698 // FIXME scheduling should handle frame counter
699 // roll-around ... exotic case (and OHCI has
700 // a 2^16 iso range, vs other HCs max of 2^10)
701 frame += cnt * urb->interval;
702 frame &= 0xffff;
703 td_fill (ohci, TD_CC | TD_ISO | frame,
704 data + urb->iso_frame_desc [cnt].offset,
705 urb->iso_frame_desc [cnt].length, urb, cnt);
707 if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0) {
708 if (quirk_amdiso(ohci))
709 usb_amd_quirk_pll_disable();
710 if (quirk_amdprefetch(ohci))
711 sb800_prefetch(dev, 1);
713 periodic = ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs++ == 0
714 && ohci_to_hcd(ohci)->self.bandwidth_int_reqs == 0;
715 break;
718 /* start periodic dma if needed */
719 if (periodic) {
720 wmb ();
721 ohci->hc_control |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
722 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
725 // ASSERT (urb_priv->length == cnt);
728 /*-------------------------------------------------------------------------*
729 * Done List handling functions
730 *-------------------------------------------------------------------------*/
732 /* calculate transfer length/status and update the urb */
733 static int td_done(struct ohci_hcd *ohci, struct urb *urb, struct td *td)
735 u32 tdINFO = hc32_to_cpup (ohci, &td->hwINFO);
736 int cc = 0;
737 int status = -EINPROGRESS;
739 list_del (&td->td_list);
741 /* ISO ... drivers see per-TD length/status */
742 if (tdINFO & TD_ISO) {
743 u16 tdPSW = ohci_hwPSW(ohci, td, 0);
744 int dlen = 0;
746 /* NOTE: assumes FC in tdINFO == 0, and that
747 * only the first of 0..MAXPSW psws is used.
750 cc = (tdPSW >> 12) & 0xF;
751 if (tdINFO & TD_CC) /* hc didn't touch? */
752 return status;
754 if (usb_pipeout (urb->pipe))
755 dlen = urb->iso_frame_desc [td->index].length;
756 else {
757 /* short reads are always OK for ISO */
758 if (cc == TD_DATAUNDERRUN)
759 cc = TD_CC_NOERROR;
760 dlen = tdPSW & 0x3ff;
762 urb->actual_length += dlen;
763 urb->iso_frame_desc [td->index].actual_length = dlen;
764 urb->iso_frame_desc [td->index].status = cc_to_error [cc];
766 if (cc != TD_CC_NOERROR)
767 ohci_vdbg (ohci,
768 "urb %p iso td %p (%d) len %d cc %d\n",
769 urb, td, 1 + td->index, dlen, cc);
771 /* BULK, INT, CONTROL ... drivers see aggregate length/status,
772 * except that "setup" bytes aren't counted and "short" transfers
773 * might not be reported as errors.
775 } else {
776 int type = usb_pipetype (urb->pipe);
777 u32 tdBE = hc32_to_cpup (ohci, &td->hwBE);
779 cc = TD_CC_GET (tdINFO);
781 /* update packet status if needed (short is normally ok) */
782 if (cc == TD_DATAUNDERRUN
783 && !(urb->transfer_flags & URB_SHORT_NOT_OK))
784 cc = TD_CC_NOERROR;
785 if (cc != TD_CC_NOERROR && cc < 0x0E)
786 status = cc_to_error[cc];
788 /* count all non-empty packets except control SETUP packet */
789 if ((type != PIPE_CONTROL || td->index != 0) && tdBE != 0) {
790 if (td->hwCBP == 0)
791 urb->actual_length += tdBE - td->data_dma + 1;
792 else
793 urb->actual_length +=
794 hc32_to_cpup (ohci, &td->hwCBP)
795 - td->data_dma;
798 if (cc != TD_CC_NOERROR && cc < 0x0E)
799 ohci_vdbg (ohci,
800 "urb %p td %p (%d) cc %d, len=%d/%d\n",
801 urb, td, 1 + td->index, cc,
802 urb->actual_length,
803 urb->transfer_buffer_length);
805 return status;
808 /*-------------------------------------------------------------------------*/
810 static void ed_halted(struct ohci_hcd *ohci, struct td *td, int cc)
812 struct urb *urb = td->urb;
813 urb_priv_t *urb_priv = urb->hcpriv;
814 struct ed *ed = td->ed;
815 struct list_head *tmp = td->td_list.next;
816 __hc32 toggle = ed->hwHeadP & cpu_to_hc32 (ohci, ED_C);
818 /* clear ed halt; this is the td that caused it, but keep it inactive
819 * until its urb->complete() has a chance to clean up.
821 ed->hwINFO |= cpu_to_hc32 (ohci, ED_SKIP);
822 wmb ();
823 ed->hwHeadP &= ~cpu_to_hc32 (ohci, ED_H);
825 /* Get rid of all later tds from this urb. We don't have
826 * to be careful: no errors and nothing was transferred.
827 * Also patch the ed so it looks as if those tds completed normally.
829 while (tmp != &ed->td_list) {
830 struct td *next;
832 next = list_entry (tmp, struct td, td_list);
833 tmp = next->td_list.next;
835 if (next->urb != urb)
836 break;
838 /* NOTE: if multi-td control DATA segments get supported,
839 * this urb had one of them, this td wasn't the last td
840 * in that segment (TD_R clear), this ed halted because
841 * of a short read, _and_ URB_SHORT_NOT_OK is clear ...
842 * then we need to leave the control STATUS packet queued
843 * and clear ED_SKIP.
846 list_del(&next->td_list);
847 urb_priv->td_cnt++;
848 ed->hwHeadP = next->hwNextTD | toggle;
851 /* help for troubleshooting: report anything that
852 * looks odd ... that doesn't include protocol stalls
853 * (or maybe some other things)
855 switch (cc) {
856 case TD_DATAUNDERRUN:
857 if ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)
858 break;
859 /* fallthrough */
860 case TD_CC_STALL:
861 if (usb_pipecontrol (urb->pipe))
862 break;
863 /* fallthrough */
864 default:
865 ohci_dbg (ohci,
866 "urb %p path %s ep%d%s %08x cc %d --> status %d\n",
867 urb, urb->dev->devpath,
868 usb_pipeendpoint (urb->pipe),
869 usb_pipein (urb->pipe) ? "in" : "out",
870 hc32_to_cpu (ohci, td->hwINFO),
871 cc, cc_to_error [cc]);
875 /* replies to the request have to be on a FIFO basis so
876 * we unreverse the hc-reversed done-list
878 static struct td *dl_reverse_done_list (struct ohci_hcd *ohci)
880 u32 td_dma;
881 struct td *td_rev = NULL;
882 struct td *td = NULL;
884 td_dma = hc32_to_cpup (ohci, &ohci->hcca->done_head);
885 ohci->hcca->done_head = 0;
886 wmb();
888 /* get TD from hc's singly linked list, and
889 * prepend to ours. ed->td_list changes later.
891 while (td_dma) {
892 int cc;
894 td = dma_to_td (ohci, td_dma);
895 if (!td) {
896 ohci_err (ohci, "bad entry %8x\n", td_dma);
897 break;
900 td->hwINFO |= cpu_to_hc32 (ohci, TD_DONE);
901 cc = TD_CC_GET (hc32_to_cpup (ohci, &td->hwINFO));
903 /* Non-iso endpoints can halt on error; un-halt,
904 * and dequeue any other TDs from this urb.
905 * No other TD could have caused the halt.
907 if (cc != TD_CC_NOERROR
908 && (td->ed->hwHeadP & cpu_to_hc32 (ohci, ED_H)))
909 ed_halted(ohci, td, cc);
911 td->next_dl_td = td_rev;
912 td_rev = td;
913 td_dma = hc32_to_cpup (ohci, &td->hwNextTD);
915 return td_rev;
918 /*-------------------------------------------------------------------------*/
920 /* there are some urbs/eds to unlink; called in_irq(), with HCD locked */
921 static void
922 finish_unlinks (struct ohci_hcd *ohci, u16 tick)
924 struct ed *ed, **last;
926 rescan_all:
927 for (last = &ohci->ed_rm_list, ed = *last; ed != NULL; ed = *last) {
928 struct list_head *entry, *tmp;
929 int completed, modified;
930 __hc32 *prev;
932 /* Is this ED already invisible to the hardware? */
933 if (ed->state == ED_IDLE)
934 goto ed_idle;
936 /* only take off EDs that the HC isn't using, accounting for
937 * frame counter wraps and EDs with partially retired TDs
939 if (likely(ohci->rh_state == OHCI_RH_RUNNING)) {
940 if (tick_before (tick, ed->tick)) {
941 skip_ed:
942 last = &ed->ed_next;
943 continue;
946 if (!list_empty (&ed->td_list)) {
947 struct td *td;
948 u32 head;
950 td = list_entry (ed->td_list.next, struct td,
951 td_list);
952 head = hc32_to_cpu (ohci, ed->hwHeadP) &
953 TD_MASK;
955 /* INTR_WDH may need to clean up first */
956 if (td->td_dma != head) {
957 if (ed == ohci->ed_to_check)
958 ohci->ed_to_check = NULL;
959 else
960 goto skip_ed;
965 /* ED's now officially unlinked, hc doesn't see */
966 ed->state = ED_IDLE;
967 if (quirk_zfmicro(ohci) && ed->type == PIPE_INTERRUPT)
968 ohci->eds_scheduled--;
969 ed->hwHeadP &= ~cpu_to_hc32(ohci, ED_H);
970 ed->hwNextED = 0;
971 wmb();
972 ed->hwINFO &= ~cpu_to_hc32(ohci, ED_SKIP | ED_DEQUEUE);
973 ed_idle:
975 /* reentrancy: if we drop the schedule lock, someone might
976 * have modified this list. normally it's just prepending
977 * entries (which we'd ignore), but paranoia won't hurt.
979 modified = 0;
981 /* unlink urbs as requested, but rescan the list after
982 * we call a completion since it might have unlinked
983 * another (earlier) urb
985 * When we get here, the HC doesn't see this ed. But it
986 * must not be rescheduled until all completed URBs have
987 * been given back to the driver.
989 rescan_this:
990 completed = 0;
991 prev = &ed->hwHeadP;
992 list_for_each_safe (entry, tmp, &ed->td_list) {
993 struct td *td;
994 struct urb *urb;
995 urb_priv_t *urb_priv;
996 __hc32 savebits;
997 u32 tdINFO;
999 td = list_entry (entry, struct td, td_list);
1000 urb = td->urb;
1001 urb_priv = td->urb->hcpriv;
1003 if (!urb->unlinked) {
1004 prev = &td->hwNextTD;
1005 continue;
1008 /* patch pointer hc uses */
1009 savebits = *prev & ~cpu_to_hc32 (ohci, TD_MASK);
1010 *prev = td->hwNextTD | savebits;
1012 /* If this was unlinked, the TD may not have been
1013 * retired ... so manually save the data toggle.
1014 * The controller ignores the value we save for
1015 * control and ISO endpoints.
1017 tdINFO = hc32_to_cpup(ohci, &td->hwINFO);
1018 if ((tdINFO & TD_T) == TD_T_DATA0)
1019 ed->hwHeadP &= ~cpu_to_hc32(ohci, ED_C);
1020 else if ((tdINFO & TD_T) == TD_T_DATA1)
1021 ed->hwHeadP |= cpu_to_hc32(ohci, ED_C);
1023 /* HC may have partly processed this TD */
1024 td_done (ohci, urb, td);
1025 urb_priv->td_cnt++;
1027 /* if URB is done, clean up */
1028 if (urb_priv->td_cnt >= urb_priv->length) {
1029 modified = completed = 1;
1030 finish_urb(ohci, urb, 0);
1033 if (completed && !list_empty (&ed->td_list))
1034 goto rescan_this;
1037 * If no TDs are queued, take ED off the ed_rm_list.
1038 * Otherwise, if the HC is running, reschedule.
1039 * If not, leave it on the list for further dequeues.
1041 if (list_empty(&ed->td_list)) {
1042 *last = ed->ed_next;
1043 ed->ed_next = NULL;
1044 } else if (ohci->rh_state == OHCI_RH_RUNNING) {
1045 *last = ed->ed_next;
1046 ed->ed_next = NULL;
1047 ed_schedule(ohci, ed);
1048 } else {
1049 last = &ed->ed_next;
1052 if (modified)
1053 goto rescan_all;
1056 /* maybe reenable control and bulk lists */
1057 if (ohci->rh_state == OHCI_RH_RUNNING && !ohci->ed_rm_list) {
1058 u32 command = 0, control = 0;
1060 if (ohci->ed_controltail) {
1061 command |= OHCI_CLF;
1062 if (quirk_zfmicro(ohci))
1063 mdelay(1);
1064 if (!(ohci->hc_control & OHCI_CTRL_CLE)) {
1065 control |= OHCI_CTRL_CLE;
1066 ohci_writel (ohci, 0,
1067 &ohci->regs->ed_controlcurrent);
1070 if (ohci->ed_bulktail) {
1071 command |= OHCI_BLF;
1072 if (quirk_zfmicro(ohci))
1073 mdelay(1);
1074 if (!(ohci->hc_control & OHCI_CTRL_BLE)) {
1075 control |= OHCI_CTRL_BLE;
1076 ohci_writel (ohci, 0,
1077 &ohci->regs->ed_bulkcurrent);
1081 /* CLE/BLE to enable, CLF/BLF to (maybe) kickstart */
1082 if (control) {
1083 ohci->hc_control |= control;
1084 if (quirk_zfmicro(ohci))
1085 mdelay(1);
1086 ohci_writel (ohci, ohci->hc_control,
1087 &ohci->regs->control);
1089 if (command) {
1090 if (quirk_zfmicro(ohci))
1091 mdelay(1);
1092 ohci_writel (ohci, command, &ohci->regs->cmdstatus);
1099 /*-------------------------------------------------------------------------*/
1102 * Used to take back a TD from the host controller. This would normally be
1103 * called from within dl_done_list, however it may be called directly if the
1104 * HC no longer sees the TD and it has not appeared on the donelist (after
1105 * two frames). This bug has been observed on ZF Micro systems.
1107 static void takeback_td(struct ohci_hcd *ohci, struct td *td)
1109 struct urb *urb = td->urb;
1110 urb_priv_t *urb_priv = urb->hcpriv;
1111 struct ed *ed = td->ed;
1112 int status;
1114 /* update URB's length and status from TD */
1115 status = td_done(ohci, urb, td);
1116 urb_priv->td_cnt++;
1118 /* If all this urb's TDs are done, call complete() */
1119 if (urb_priv->td_cnt >= urb_priv->length)
1120 finish_urb(ohci, urb, status);
1122 /* clean schedule: unlink EDs that are no longer busy */
1123 if (list_empty(&ed->td_list)) {
1124 if (ed->state == ED_OPER)
1125 start_ed_unlink(ohci, ed);
1127 /* ... reenabling halted EDs only after fault cleanup */
1128 } else if ((ed->hwINFO & cpu_to_hc32(ohci, ED_SKIP | ED_DEQUEUE))
1129 == cpu_to_hc32(ohci, ED_SKIP)) {
1130 td = list_entry(ed->td_list.next, struct td, td_list);
1131 if (!(td->hwINFO & cpu_to_hc32(ohci, TD_DONE))) {
1132 ed->hwINFO &= ~cpu_to_hc32(ohci, ED_SKIP);
1133 /* ... hc may need waking-up */
1134 switch (ed->type) {
1135 case PIPE_CONTROL:
1136 ohci_writel(ohci, OHCI_CLF,
1137 &ohci->regs->cmdstatus);
1138 break;
1139 case PIPE_BULK:
1140 ohci_writel(ohci, OHCI_BLF,
1141 &ohci->regs->cmdstatus);
1142 break;
1149 * Process normal completions (error or success) and clean the schedules.
1151 * This is the main path for handing urbs back to drivers. The only other
1152 * normal path is finish_unlinks(), which unlinks URBs using ed_rm_list,
1153 * instead of scanning the (re-reversed) donelist as this does. There's
1154 * an abnormal path too, handling a quirk in some Compaq silicon: URBs
1155 * with TDs that appear to be orphaned are directly reclaimed.
1157 static void
1158 dl_done_list (struct ohci_hcd *ohci)
1160 struct td *td = dl_reverse_done_list (ohci);
1162 while (td) {
1163 struct td *td_next = td->next_dl_td;
1164 struct ed *ed = td->ed;
1167 * Some OHCI controllers (NVIDIA for sure, maybe others)
1168 * occasionally forget to add TDs to the done queue. Since
1169 * TDs for a given endpoint are always processed in order,
1170 * if we find a TD on the donelist then all of its
1171 * predecessors must be finished as well.
1173 for (;;) {
1174 struct td *td2;
1176 td2 = list_first_entry(&ed->td_list, struct td,
1177 td_list);
1178 if (td2 == td)
1179 break;
1180 takeback_td(ohci, td2);
1183 takeback_td(ohci, td);
1184 td = td_next;