15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-1.0+
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * OHCI HCD (Host Controller Driver) for USB.
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
61da177e4SLinus Torvalds * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
71da177e4SLinus Torvalds *
81da177e4SLinus Torvalds * This file is licenced under the GPL.
91da177e4SLinus Torvalds */
101da177e4SLinus Torvalds
117d12e780SDavid Howells #include <linux/irq.h>
125a0e3ad6STejun Heo #include <linux/slab.h>
137d12e780SDavid Howells
urb_free_priv(struct ohci_hcd * hc,urb_priv_t * urb_priv)141da177e4SLinus Torvalds static void urb_free_priv (struct ohci_hcd *hc, urb_priv_t *urb_priv)
151da177e4SLinus Torvalds {
161da177e4SLinus Torvalds int last = urb_priv->length - 1;
171da177e4SLinus Torvalds
181da177e4SLinus Torvalds if (last >= 0) {
191da177e4SLinus Torvalds int i;
201da177e4SLinus Torvalds struct td *td;
211da177e4SLinus Torvalds
221da177e4SLinus Torvalds for (i = 0; i <= last; i++) {
231da177e4SLinus Torvalds td = urb_priv->td [i];
241da177e4SLinus Torvalds if (td)
251da177e4SLinus Torvalds td_free (hc, td);
261da177e4SLinus Torvalds }
271da177e4SLinus Torvalds }
281da177e4SLinus Torvalds
291da177e4SLinus Torvalds list_del (&urb_priv->pending);
301da177e4SLinus Torvalds kfree (urb_priv);
311da177e4SLinus Torvalds }
321da177e4SLinus Torvalds
331da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
341da177e4SLinus Torvalds
351da177e4SLinus Torvalds /*
361da177e4SLinus Torvalds * URB goes back to driver, and isn't reissued.
371da177e4SLinus Torvalds * It's completely gone from HC data structures.
381da177e4SLinus Torvalds * PRECONDITION: ohci lock held, irqs blocked.
391da177e4SLinus Torvalds */
401da177e4SLinus Torvalds static void
finish_urb(struct ohci_hcd * ohci,struct urb * urb,int status)4155d84968SAlan Stern finish_urb(struct ohci_hcd *ohci, struct urb *urb, int status)
421da177e4SLinus Torvalds __releases(ohci->lock)
431da177e4SLinus Torvalds __acquires(ohci->lock)
441da177e4SLinus Torvalds {
452621d011SManjunath Goudar struct device *dev = ohci_to_hcd(ohci)->self.controller;
46a8693424SAlan Stern struct usb_host_endpoint *ep = urb->ep;
47a8693424SAlan Stern struct urb_priv *urb_priv;
48a8693424SAlan Stern
491da177e4SLinus Torvalds // ASSERT (urb->hcpriv != 0);
501da177e4SLinus Torvalds
51a8693424SAlan Stern restart:
521da177e4SLinus Torvalds urb_free_priv (ohci, urb->hcpriv);
53ece1d77eSChen Gang urb->hcpriv = NULL;
5455d84968SAlan Stern if (likely(status == -EINPROGRESS))
5555d84968SAlan Stern status = 0;
561da177e4SLinus Torvalds
571da177e4SLinus Torvalds switch (usb_pipetype (urb->pipe)) {
581da177e4SLinus Torvalds case PIPE_ISOCHRONOUS:
591da177e4SLinus Torvalds ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs--;
60a1f17a87SLibin Yang if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0) {
61a1f17a87SLibin Yang if (quirk_amdiso(ohci))
62ad93562bSAndiry Xu usb_amd_quirk_pll_enable();
63a1f17a87SLibin Yang if (quirk_amdprefetch(ohci))
642621d011SManjunath Goudar sb800_prefetch(dev, 0);
65a1f17a87SLibin Yang }
661da177e4SLinus Torvalds break;
671da177e4SLinus Torvalds case PIPE_INTERRUPT:
681da177e4SLinus Torvalds ohci_to_hcd(ohci)->self.bandwidth_int_reqs--;
691da177e4SLinus Torvalds break;
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds
721da177e4SLinus Torvalds /* urb->complete() can reenter this HCD */
73e9df41c5SAlan Stern usb_hcd_unlink_urb_from_ep(ohci_to_hcd(ohci), urb);
741da177e4SLinus Torvalds spin_unlock (&ohci->lock);
754a00027dSAlan Stern usb_hcd_giveback_urb(ohci_to_hcd(ohci), urb, status);
761da177e4SLinus Torvalds spin_lock (&ohci->lock);
771da177e4SLinus Torvalds
781da177e4SLinus Torvalds /* stop periodic dma if it's not needed */
791da177e4SLinus Torvalds if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0
801da177e4SLinus Torvalds && ohci_to_hcd(ohci)->self.bandwidth_int_reqs == 0) {
811da177e4SLinus Torvalds ohci->hc_control &= ~(OHCI_CTRL_PLE|OHCI_CTRL_IE);
821da177e4SLinus Torvalds ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
831da177e4SLinus Torvalds }
84a8693424SAlan Stern
85a8693424SAlan Stern /*
86a8693424SAlan Stern * An isochronous URB that is sumitted too late won't have any TDs
87a8693424SAlan Stern * (marked by the fact that the td_cnt value is larger than the
88a8693424SAlan Stern * actual number of TDs). If the next URB on this endpoint is like
89a8693424SAlan Stern * that, give it back now.
90a8693424SAlan Stern */
91a8693424SAlan Stern if (!list_empty(&ep->urb_list)) {
92a8693424SAlan Stern urb = list_first_entry(&ep->urb_list, struct urb, urb_list);
93a8693424SAlan Stern urb_priv = urb->hcpriv;
94a8693424SAlan Stern if (urb_priv->td_cnt > urb_priv->length) {
95a8693424SAlan Stern status = 0;
96a8693424SAlan Stern goto restart;
97a8693424SAlan Stern }
98a8693424SAlan Stern }
991da177e4SLinus Torvalds }
1001da177e4SLinus Torvalds
1011da177e4SLinus Torvalds
1021da177e4SLinus Torvalds /*-------------------------------------------------------------------------*
1031da177e4SLinus Torvalds * ED handling functions
1041da177e4SLinus Torvalds *-------------------------------------------------------------------------*/
1051da177e4SLinus Torvalds
1061da177e4SLinus Torvalds /* search for the right schedule branch to use for a periodic ed.
1071da177e4SLinus Torvalds * does some load balancing; returns the branch, or negative errno.
1081da177e4SLinus Torvalds */
balance(struct ohci_hcd * ohci,int interval,int load)1091da177e4SLinus Torvalds static int balance (struct ohci_hcd *ohci, int interval, int load)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds int i, branch = -ENOSPC;
1121da177e4SLinus Torvalds
1131da177e4SLinus Torvalds /* iso periods can be huge; iso tds specify frame numbers */
1141da177e4SLinus Torvalds if (interval > NUM_INTS)
1151da177e4SLinus Torvalds interval = NUM_INTS;
1161da177e4SLinus Torvalds
1171da177e4SLinus Torvalds /* search for the least loaded schedule branch of that period
1181da177e4SLinus Torvalds * that has enough bandwidth left unreserved.
1191da177e4SLinus Torvalds */
1201da177e4SLinus Torvalds for (i = 0; i < interval ; i++) {
1211da177e4SLinus Torvalds if (branch < 0 || ohci->load [branch] > ohci->load [i]) {
1221da177e4SLinus Torvalds int j;
1231da177e4SLinus Torvalds
1241da177e4SLinus Torvalds /* usb 1.1 says 90% of one frame */
1251da177e4SLinus Torvalds for (j = i; j < NUM_INTS; j += interval) {
1261da177e4SLinus Torvalds if ((ohci->load [j] + load) > 900)
1271da177e4SLinus Torvalds break;
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds if (j < NUM_INTS)
1301da177e4SLinus Torvalds continue;
1311da177e4SLinus Torvalds branch = i;
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds return branch;
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds
1371da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1381da177e4SLinus Torvalds
1391da177e4SLinus Torvalds /* both iso and interrupt requests have periods; this routine puts them
1401da177e4SLinus Torvalds * into the schedule tree in the apppropriate place. most iso devices use
1411da177e4SLinus Torvalds * 1msec periods, but that's not required.
1421da177e4SLinus Torvalds */
periodic_link(struct ohci_hcd * ohci,struct ed * ed)1431da177e4SLinus Torvalds static void periodic_link (struct ohci_hcd *ohci, struct ed *ed)
1441da177e4SLinus Torvalds {
1451da177e4SLinus Torvalds unsigned i;
1461da177e4SLinus Torvalds
147d2c4254fSOliver Neukum ohci_dbg(ohci, "link %sed %p branch %d [%dus.], interval %d\n",
1481da177e4SLinus Torvalds (ed->hwINFO & cpu_to_hc32 (ohci, ED_ISO)) ? "iso " : "",
1491da177e4SLinus Torvalds ed, ed->branch, ed->load, ed->interval);
1501da177e4SLinus Torvalds
1511da177e4SLinus Torvalds for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
1521da177e4SLinus Torvalds struct ed **prev = &ohci->periodic [i];
1531da177e4SLinus Torvalds __hc32 *prev_p = &ohci->hcca->int_table [i];
1541da177e4SLinus Torvalds struct ed *here = *prev;
1551da177e4SLinus Torvalds
1561da177e4SLinus Torvalds /* sorting each branch by period (slow before fast)
1571da177e4SLinus Torvalds * lets us share the faster parts of the tree.
1581da177e4SLinus Torvalds * (plus maybe: put interrupt eds before iso)
1591da177e4SLinus Torvalds */
1601da177e4SLinus Torvalds while (here && ed != here) {
1611da177e4SLinus Torvalds if (ed->interval > here->interval)
1621da177e4SLinus Torvalds break;
1631da177e4SLinus Torvalds prev = &here->ed_next;
1641da177e4SLinus Torvalds prev_p = &here->hwNextED;
1651da177e4SLinus Torvalds here = *prev;
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds if (ed != here) {
1681da177e4SLinus Torvalds ed->ed_next = here;
1691da177e4SLinus Torvalds if (here)
1701da177e4SLinus Torvalds ed->hwNextED = *prev_p;
1711da177e4SLinus Torvalds wmb ();
1721da177e4SLinus Torvalds *prev = ed;
1731da177e4SLinus Torvalds *prev_p = cpu_to_hc32(ohci, ed->dma);
1741da177e4SLinus Torvalds wmb();
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds ohci->load [i] += ed->load;
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds ohci_to_hcd(ohci)->self.bandwidth_allocated += ed->load / ed->interval;
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds
1811da177e4SLinus Torvalds /* link an ed into one of the HC chains */
1821da177e4SLinus Torvalds
ed_schedule(struct ohci_hcd * ohci,struct ed * ed)1831da177e4SLinus Torvalds static int ed_schedule (struct ohci_hcd *ohci, struct ed *ed)
1841da177e4SLinus Torvalds {
1851da177e4SLinus Torvalds int branch;
1861da177e4SLinus Torvalds
1871da177e4SLinus Torvalds ed->ed_prev = NULL;
1881da177e4SLinus Torvalds ed->ed_next = NULL;
1891da177e4SLinus Torvalds ed->hwNextED = 0;
1901da177e4SLinus Torvalds wmb ();
1911da177e4SLinus Torvalds
1921da177e4SLinus Torvalds /* we care about rm_list when setting CLE/BLE in case the HC was at
1931da177e4SLinus Torvalds * work on some TD when CLE/BLE was turned off, and isn't quiesced
1941da177e4SLinus Torvalds * yet. finish_unlinks() restarts as needed, some upcoming INTR_SF.
1951da177e4SLinus Torvalds *
1961da177e4SLinus Torvalds * control and bulk EDs are doubly linked (ed_next, ed_prev), but
1971da177e4SLinus Torvalds * periodic ones are singly linked (ed_next). that's because the
1981da177e4SLinus Torvalds * periodic schedule encodes a tree like figure 3-5 in the ohci
1991da177e4SLinus Torvalds * spec: each qh can have several "previous" nodes, and the tree
2001da177e4SLinus Torvalds * doesn't have unused/idle descriptors.
2011da177e4SLinus Torvalds */
2021da177e4SLinus Torvalds switch (ed->type) {
2031da177e4SLinus Torvalds case PIPE_CONTROL:
2041da177e4SLinus Torvalds if (ohci->ed_controltail == NULL) {
2051da177e4SLinus Torvalds WARN_ON (ohci->hc_control & OHCI_CTRL_CLE);
2061da177e4SLinus Torvalds ohci_writel (ohci, ed->dma,
2071da177e4SLinus Torvalds &ohci->regs->ed_controlhead);
2081da177e4SLinus Torvalds } else {
2091da177e4SLinus Torvalds ohci->ed_controltail->ed_next = ed;
2101da177e4SLinus Torvalds ohci->ed_controltail->hwNextED = cpu_to_hc32 (ohci,
2111da177e4SLinus Torvalds ed->dma);
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds ed->ed_prev = ohci->ed_controltail;
2141da177e4SLinus Torvalds if (!ohci->ed_controltail && !ohci->ed_rm_list) {
2151da177e4SLinus Torvalds wmb();
2161da177e4SLinus Torvalds ohci->hc_control |= OHCI_CTRL_CLE;
2171da177e4SLinus Torvalds ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
2181da177e4SLinus Torvalds ohci_writel (ohci, ohci->hc_control,
2191da177e4SLinus Torvalds &ohci->regs->control);
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds ohci->ed_controltail = ed;
2221da177e4SLinus Torvalds break;
2231da177e4SLinus Torvalds
2241da177e4SLinus Torvalds case PIPE_BULK:
2251da177e4SLinus Torvalds if (ohci->ed_bulktail == NULL) {
2261da177e4SLinus Torvalds WARN_ON (ohci->hc_control & OHCI_CTRL_BLE);
2271da177e4SLinus Torvalds ohci_writel (ohci, ed->dma, &ohci->regs->ed_bulkhead);
2281da177e4SLinus Torvalds } else {
2291da177e4SLinus Torvalds ohci->ed_bulktail->ed_next = ed;
2301da177e4SLinus Torvalds ohci->ed_bulktail->hwNextED = cpu_to_hc32 (ohci,
2311da177e4SLinus Torvalds ed->dma);
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds ed->ed_prev = ohci->ed_bulktail;
2341da177e4SLinus Torvalds if (!ohci->ed_bulktail && !ohci->ed_rm_list) {
2351da177e4SLinus Torvalds wmb();
2361da177e4SLinus Torvalds ohci->hc_control |= OHCI_CTRL_BLE;
2371da177e4SLinus Torvalds ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
2381da177e4SLinus Torvalds ohci_writel (ohci, ohci->hc_control,
2391da177e4SLinus Torvalds &ohci->regs->control);
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds ohci->ed_bulktail = ed;
2421da177e4SLinus Torvalds break;
2431da177e4SLinus Torvalds
2441da177e4SLinus Torvalds // case PIPE_INTERRUPT:
2451da177e4SLinus Torvalds // case PIPE_ISOCHRONOUS:
2461da177e4SLinus Torvalds default:
2471da177e4SLinus Torvalds branch = balance (ohci, ed->interval, ed->load);
2481da177e4SLinus Torvalds if (branch < 0) {
2491da177e4SLinus Torvalds ohci_dbg (ohci,
2501da177e4SLinus Torvalds "ERR %d, interval %d msecs, load %d\n",
2511da177e4SLinus Torvalds branch, ed->interval, ed->load);
2521da177e4SLinus Torvalds // FIXME if there are TDs queued, fail them!
2531da177e4SLinus Torvalds return branch;
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds ed->branch = branch;
2561da177e4SLinus Torvalds periodic_link (ohci, ed);
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds
2591da177e4SLinus Torvalds /* the HC may not see the schedule updates yet, but if it does
2601da177e4SLinus Torvalds * then they'll be properly ordered.
2611da177e4SLinus Torvalds */
262c66f59eeSMichał Pecio
263c66f59eeSMichał Pecio ed->state = ED_OPER;
2641da177e4SLinus Torvalds return 0;
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds
2671da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
2681da177e4SLinus Torvalds
2691da177e4SLinus Torvalds /* scan the periodic table to find and unlink this ED */
periodic_unlink(struct ohci_hcd * ohci,struct ed * ed)2701da177e4SLinus Torvalds static void periodic_unlink (struct ohci_hcd *ohci, struct ed *ed)
2711da177e4SLinus Torvalds {
2721da177e4SLinus Torvalds int i;
2731da177e4SLinus Torvalds
2741da177e4SLinus Torvalds for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
2751da177e4SLinus Torvalds struct ed *temp;
2761da177e4SLinus Torvalds struct ed **prev = &ohci->periodic [i];
2771da177e4SLinus Torvalds __hc32 *prev_p = &ohci->hcca->int_table [i];
2781da177e4SLinus Torvalds
2791da177e4SLinus Torvalds while (*prev && (temp = *prev) != ed) {
2801da177e4SLinus Torvalds prev_p = &temp->hwNextED;
2811da177e4SLinus Torvalds prev = &temp->ed_next;
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds if (*prev) {
2841da177e4SLinus Torvalds *prev_p = ed->hwNextED;
2851da177e4SLinus Torvalds *prev = ed->ed_next;
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds ohci->load [i] -= ed->load;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds ohci_to_hcd(ohci)->self.bandwidth_allocated -= ed->load / ed->interval;
2901da177e4SLinus Torvalds
291d2c4254fSOliver Neukum ohci_dbg(ohci, "unlink %sed %p branch %d [%dus.], interval %d\n",
2921da177e4SLinus Torvalds (ed->hwINFO & cpu_to_hc32 (ohci, ED_ISO)) ? "iso " : "",
2931da177e4SLinus Torvalds ed, ed->branch, ed->load, ed->interval);
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds
2961da177e4SLinus Torvalds /* unlink an ed from one of the HC chains.
2971da177e4SLinus Torvalds * just the link to the ed is unlinked.
2981da177e4SLinus Torvalds * the link from the ed still points to another operational ed or 0
2991da177e4SLinus Torvalds * so the HC can eventually finish the processing of the unlinked ed
3001da177e4SLinus Torvalds * (assuming it already started that, which needn't be true).
3011da177e4SLinus Torvalds *
3021da177e4SLinus Torvalds * ED_UNLINK is a transient state: the HC may still see this ED, but soon
3031da177e4SLinus Torvalds * it won't. ED_SKIP means the HC will finish its current transaction,
3041da177e4SLinus Torvalds * but won't start anything new. The TD queue may still grow; device
3051da177e4SLinus Torvalds * drivers don't know about this HCD-internal state.
3061da177e4SLinus Torvalds *
3071da177e4SLinus Torvalds * When the HC can't see the ED, something changes ED_UNLINK to one of:
3081da177e4SLinus Torvalds *
3091da177e4SLinus Torvalds * - ED_OPER: when there's any request queued, the ED gets rescheduled
3101da177e4SLinus Torvalds * immediately. HC should be working on them.
3111da177e4SLinus Torvalds *
312977dcfdcSAlan Stern * - ED_IDLE: when there's no TD queue or the HC isn't running.
3131da177e4SLinus Torvalds *
3141da177e4SLinus Torvalds * When finish_unlinks() runs later, after SOF interrupt, it will often
3151da177e4SLinus Torvalds * complete one or more URB unlinks before making that state change.
3161da177e4SLinus Torvalds */
ed_deschedule(struct ohci_hcd * ohci,struct ed * ed)3171da177e4SLinus Torvalds static void ed_deschedule (struct ohci_hcd *ohci, struct ed *ed)
3181da177e4SLinus Torvalds {
3191da177e4SLinus Torvalds ed->hwINFO |= cpu_to_hc32 (ohci, ED_SKIP);
3201da177e4SLinus Torvalds wmb ();
3211da177e4SLinus Torvalds ed->state = ED_UNLINK;
3221da177e4SLinus Torvalds
3231da177e4SLinus Torvalds /* To deschedule something from the control or bulk list, just
3241da177e4SLinus Torvalds * clear CLE/BLE and wait. There's no safe way to scrub out list
3251da177e4SLinus Torvalds * head/current registers until later, and "later" isn't very
3261da177e4SLinus Torvalds * tightly specified. Figure 6-5 and Section 6.4.2.2 show how
3271da177e4SLinus Torvalds * the HC is reading the ED queues (while we modify them).
3281da177e4SLinus Torvalds *
3291da177e4SLinus Torvalds * For now, ed_schedule() is "later". It might be good paranoia
3301da177e4SLinus Torvalds * to scrub those registers in finish_unlinks(), in case of bugs
3311da177e4SLinus Torvalds * that make the HC try to use them.
3321da177e4SLinus Torvalds */
3331da177e4SLinus Torvalds switch (ed->type) {
3341da177e4SLinus Torvalds case PIPE_CONTROL:
3351da177e4SLinus Torvalds /* remove ED from the HC's list: */
3361da177e4SLinus Torvalds if (ed->ed_prev == NULL) {
3371da177e4SLinus Torvalds if (!ed->hwNextED) {
3381da177e4SLinus Torvalds ohci->hc_control &= ~OHCI_CTRL_CLE;
3391da177e4SLinus Torvalds ohci_writel (ohci, ohci->hc_control,
3401da177e4SLinus Torvalds &ohci->regs->control);
3411da177e4SLinus Torvalds // a ohci_readl() later syncs CLE with the HC
3421da177e4SLinus Torvalds } else
3431da177e4SLinus Torvalds ohci_writel (ohci,
3441da177e4SLinus Torvalds hc32_to_cpup (ohci, &ed->hwNextED),
3451da177e4SLinus Torvalds &ohci->regs->ed_controlhead);
3461da177e4SLinus Torvalds } else {
3471da177e4SLinus Torvalds ed->ed_prev->ed_next = ed->ed_next;
3481da177e4SLinus Torvalds ed->ed_prev->hwNextED = ed->hwNextED;
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds /* remove ED from the HCD's list: */
3511da177e4SLinus Torvalds if (ohci->ed_controltail == ed) {
3521da177e4SLinus Torvalds ohci->ed_controltail = ed->ed_prev;
3531da177e4SLinus Torvalds if (ohci->ed_controltail)
3541da177e4SLinus Torvalds ohci->ed_controltail->ed_next = NULL;
3551da177e4SLinus Torvalds } else if (ed->ed_next) {
3561da177e4SLinus Torvalds ed->ed_next->ed_prev = ed->ed_prev;
3571da177e4SLinus Torvalds }
3581da177e4SLinus Torvalds break;
3591da177e4SLinus Torvalds
3601da177e4SLinus Torvalds case PIPE_BULK:
3611da177e4SLinus Torvalds /* remove ED from the HC's list: */
3621da177e4SLinus Torvalds if (ed->ed_prev == NULL) {
3631da177e4SLinus Torvalds if (!ed->hwNextED) {
3641da177e4SLinus Torvalds ohci->hc_control &= ~OHCI_CTRL_BLE;
3651da177e4SLinus Torvalds ohci_writel (ohci, ohci->hc_control,
3661da177e4SLinus Torvalds &ohci->regs->control);
3671da177e4SLinus Torvalds // a ohci_readl() later syncs BLE with the HC
3681da177e4SLinus Torvalds } else
3691da177e4SLinus Torvalds ohci_writel (ohci,
3701da177e4SLinus Torvalds hc32_to_cpup (ohci, &ed->hwNextED),
3711da177e4SLinus Torvalds &ohci->regs->ed_bulkhead);
3721da177e4SLinus Torvalds } else {
3731da177e4SLinus Torvalds ed->ed_prev->ed_next = ed->ed_next;
3741da177e4SLinus Torvalds ed->ed_prev->hwNextED = ed->hwNextED;
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds /* remove ED from the HCD's list: */
3771da177e4SLinus Torvalds if (ohci->ed_bulktail == ed) {
3781da177e4SLinus Torvalds ohci->ed_bulktail = ed->ed_prev;
3791da177e4SLinus Torvalds if (ohci->ed_bulktail)
3801da177e4SLinus Torvalds ohci->ed_bulktail->ed_next = NULL;
3811da177e4SLinus Torvalds } else if (ed->ed_next) {
3821da177e4SLinus Torvalds ed->ed_next->ed_prev = ed->ed_prev;
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds break;
3851da177e4SLinus Torvalds
3861da177e4SLinus Torvalds // case PIPE_INTERRUPT:
3871da177e4SLinus Torvalds // case PIPE_ISOCHRONOUS:
3881da177e4SLinus Torvalds default:
3891da177e4SLinus Torvalds periodic_unlink (ohci, ed);
3901da177e4SLinus Torvalds break;
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds
3941da177e4SLinus Torvalds
3951da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
3961da177e4SLinus Torvalds
3971da177e4SLinus Torvalds /* get and maybe (re)init an endpoint. init _should_ be done only as part
3981da177e4SLinus Torvalds * of enumeration, usb_set_configuration() or usb_set_interface().
3991da177e4SLinus Torvalds */
ed_get(struct ohci_hcd * ohci,struct usb_host_endpoint * ep,struct usb_device * udev,unsigned int pipe,int interval)4001da177e4SLinus Torvalds static struct ed *ed_get (
4011da177e4SLinus Torvalds struct ohci_hcd *ohci,
4021da177e4SLinus Torvalds struct usb_host_endpoint *ep,
4031da177e4SLinus Torvalds struct usb_device *udev,
4041da177e4SLinus Torvalds unsigned int pipe,
4051da177e4SLinus Torvalds int interval
4061da177e4SLinus Torvalds ) {
4071da177e4SLinus Torvalds struct ed *ed;
4081da177e4SLinus Torvalds unsigned long flags;
4091da177e4SLinus Torvalds
4101da177e4SLinus Torvalds spin_lock_irqsave (&ohci->lock, flags);
4111da177e4SLinus Torvalds
4128af93191SGreg Kroah-Hartman ed = ep->hcpriv;
4138af93191SGreg Kroah-Hartman if (!ed) {
4141da177e4SLinus Torvalds struct td *td;
4151da177e4SLinus Torvalds int is_out;
4161da177e4SLinus Torvalds u32 info;
4171da177e4SLinus Torvalds
4181da177e4SLinus Torvalds ed = ed_alloc (ohci, GFP_ATOMIC);
4191da177e4SLinus Torvalds if (!ed) {
4201da177e4SLinus Torvalds /* out of memory */
4211da177e4SLinus Torvalds goto done;
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds
4241da177e4SLinus Torvalds /* dummy td; end of td list for ed */
4251da177e4SLinus Torvalds td = td_alloc (ohci, GFP_ATOMIC);
4261da177e4SLinus Torvalds if (!td) {
4271da177e4SLinus Torvalds /* out of memory */
4281da177e4SLinus Torvalds ed_free (ohci, ed);
4291da177e4SLinus Torvalds ed = NULL;
4301da177e4SLinus Torvalds goto done;
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds ed->dummy = td;
4331da177e4SLinus Torvalds ed->hwTailP = cpu_to_hc32 (ohci, td->td_dma);
4341da177e4SLinus Torvalds ed->hwHeadP = ed->hwTailP; /* ED_C, ED_H zeroed */
4351da177e4SLinus Torvalds ed->state = ED_IDLE;
4361da177e4SLinus Torvalds
4371da177e4SLinus Torvalds is_out = !(ep->desc.bEndpointAddress & USB_DIR_IN);
4381da177e4SLinus Torvalds
4391da177e4SLinus Torvalds /* FIXME usbcore changes dev->devnum before SET_ADDRESS
4404b26d50bSAnand Gadiyar * succeeds ... otherwise we wouldn't need "pipe".
4411da177e4SLinus Torvalds */
4421da177e4SLinus Torvalds info = usb_pipedevice (pipe);
4431da177e4SLinus Torvalds ed->type = usb_pipetype(pipe);
4441da177e4SLinus Torvalds
4451da177e4SLinus Torvalds info |= (ep->desc.bEndpointAddress & ~USB_DIR_IN) << 7;
44629cc8897SKuninori Morimoto info |= usb_endpoint_maxp(&ep->desc) << 16;
4471da177e4SLinus Torvalds if (udev->speed == USB_SPEED_LOW)
4481da177e4SLinus Torvalds info |= ED_LOWSPEED;
4491da177e4SLinus Torvalds /* only control transfers store pids in tds */
4501da177e4SLinus Torvalds if (ed->type != PIPE_CONTROL) {
4511da177e4SLinus Torvalds info |= is_out ? ED_OUT : ED_IN;
4521da177e4SLinus Torvalds if (ed->type != PIPE_BULK) {
4531da177e4SLinus Torvalds /* periodic transfers... */
4541da177e4SLinus Torvalds if (ed->type == PIPE_ISOCHRONOUS)
4551da177e4SLinus Torvalds info |= ED_ISO;
4561da177e4SLinus Torvalds else if (interval > 32) /* iso can be bigger */
4571da177e4SLinus Torvalds interval = 32;
4581da177e4SLinus Torvalds ed->interval = interval;
4591da177e4SLinus Torvalds ed->load = usb_calc_bus_time (
4601da177e4SLinus Torvalds udev->speed, !is_out,
4611da177e4SLinus Torvalds ed->type == PIPE_ISOCHRONOUS,
46229cc8897SKuninori Morimoto usb_endpoint_maxp(&ep->desc))
4631da177e4SLinus Torvalds / 1000;
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds ed->hwINFO = cpu_to_hc32(ohci, info);
4671da177e4SLinus Torvalds
4681da177e4SLinus Torvalds ep->hcpriv = ed;
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds
4711da177e4SLinus Torvalds done:
4721da177e4SLinus Torvalds spin_unlock_irqrestore (&ohci->lock, flags);
4731da177e4SLinus Torvalds return ed;
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds
4761da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
4771da177e4SLinus Torvalds
4781da177e4SLinus Torvalds /* request unlinking of an endpoint from an operational HC.
4791da177e4SLinus Torvalds * put the ep on the rm_list
4801da177e4SLinus Torvalds * real work is done at the next start frame (SF) hardware interrupt
4811da177e4SLinus Torvalds * caller guarantees HCD is running, so hardware access is safe,
4821da177e4SLinus Torvalds * and that ed->state is ED_OPER
4831da177e4SLinus Torvalds */
start_ed_unlink(struct ohci_hcd * ohci,struct ed * ed)4841da177e4SLinus Torvalds static void start_ed_unlink (struct ohci_hcd *ohci, struct ed *ed)
4851da177e4SLinus Torvalds {
4861da177e4SLinus Torvalds ed->hwINFO |= cpu_to_hc32 (ohci, ED_DEQUEUE);
4871da177e4SLinus Torvalds ed_deschedule (ohci, ed);
4881da177e4SLinus Torvalds
4891da177e4SLinus Torvalds /* rm_list is just singly linked, for simplicity */
4901da177e4SLinus Torvalds ed->ed_next = ohci->ed_rm_list;
4911da177e4SLinus Torvalds ed->ed_prev = NULL;
4921da177e4SLinus Torvalds ohci->ed_rm_list = ed;
4931da177e4SLinus Torvalds
4941da177e4SLinus Torvalds /* enable SOF interrupt */
4951da177e4SLinus Torvalds ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
4961da177e4SLinus Torvalds ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
4971da177e4SLinus Torvalds // flush those writes, and get latest HCCA contents
4981da177e4SLinus Torvalds (void) ohci_readl (ohci, &ohci->regs->control);
4991da177e4SLinus Torvalds
5001da177e4SLinus Torvalds /* SF interrupt might get delayed; record the frame counter value that
5011da177e4SLinus Torvalds * indicates when the HC isn't looking at it, so concurrent unlinks
5021da177e4SLinus Torvalds * behave. frame_no wraps every 2^16 msec, and changes right before
5031da177e4SLinus Torvalds * SF is triggered.
5041da177e4SLinus Torvalds */
5051da177e4SLinus Torvalds ed->tick = ohci_frame_no(ohci) + 1;
5061da177e4SLinus Torvalds
5071da177e4SLinus Torvalds }
5081da177e4SLinus Torvalds
5091da177e4SLinus Torvalds /*-------------------------------------------------------------------------*
5101da177e4SLinus Torvalds * TD handling functions
5111da177e4SLinus Torvalds *-------------------------------------------------------------------------*/
5121da177e4SLinus Torvalds
5131da177e4SLinus Torvalds /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
5141da177e4SLinus Torvalds
5151da177e4SLinus Torvalds static void
td_fill(struct ohci_hcd * ohci,u32 info,dma_addr_t data,int len,struct urb * urb,int index)5161da177e4SLinus Torvalds td_fill (struct ohci_hcd *ohci, u32 info,
5171da177e4SLinus Torvalds dma_addr_t data, int len,
5181da177e4SLinus Torvalds struct urb *urb, int index)
5191da177e4SLinus Torvalds {
5201da177e4SLinus Torvalds struct td *td, *td_pt;
5211da177e4SLinus Torvalds struct urb_priv *urb_priv = urb->hcpriv;
5221da177e4SLinus Torvalds int is_iso = info & TD_ISO;
5231da177e4SLinus Torvalds int hash;
5241da177e4SLinus Torvalds
5251da177e4SLinus Torvalds // ASSERT (index < urb_priv->length);
5261da177e4SLinus Torvalds
5271da177e4SLinus Torvalds /* aim for only one interrupt per urb. mostly applies to control
5281da177e4SLinus Torvalds * and iso; other urbs rarely need more than one TD per urb.
5291da177e4SLinus Torvalds * this way, only final tds (or ones with an error) cause IRQs.
5301da177e4SLinus Torvalds * at least immediately; use DI=6 in case any control request is
5311da177e4SLinus Torvalds * tempted to die part way through. (and to force the hc to flush
5321da177e4SLinus Torvalds * its donelist soonish, even on unlink paths.)
5331da177e4SLinus Torvalds *
5341da177e4SLinus Torvalds * NOTE: could delay interrupts even for the last TD, and get fewer
5351da177e4SLinus Torvalds * interrupts ... increasing per-urb latency by sharing interrupts.
5361da177e4SLinus Torvalds * Drivers that queue bulk urbs may request that behavior.
5371da177e4SLinus Torvalds */
5381da177e4SLinus Torvalds if (index != (urb_priv->length - 1)
5391da177e4SLinus Torvalds || (urb->transfer_flags & URB_NO_INTERRUPT))
5401da177e4SLinus Torvalds info |= TD_DI_SET (6);
5411da177e4SLinus Torvalds
5421da177e4SLinus Torvalds /* use this td as the next dummy */
5431da177e4SLinus Torvalds td_pt = urb_priv->td [index];
5441da177e4SLinus Torvalds
5451da177e4SLinus Torvalds /* fill the old dummy TD */
5461da177e4SLinus Torvalds td = urb_priv->td [index] = urb_priv->ed->dummy;
5471da177e4SLinus Torvalds urb_priv->ed->dummy = td_pt;
5481da177e4SLinus Torvalds
5491da177e4SLinus Torvalds td->ed = urb_priv->ed;
5501da177e4SLinus Torvalds td->next_dl_td = NULL;
5511da177e4SLinus Torvalds td->index = index;
5521da177e4SLinus Torvalds td->urb = urb;
5531da177e4SLinus Torvalds td->data_dma = data;
5541da177e4SLinus Torvalds if (!len)
5551da177e4SLinus Torvalds data = 0;
5561da177e4SLinus Torvalds
5571da177e4SLinus Torvalds td->hwINFO = cpu_to_hc32 (ohci, info);
5581da177e4SLinus Torvalds if (is_iso) {
5591da177e4SLinus Torvalds td->hwCBP = cpu_to_hc32 (ohci, data & 0xFFFFF000);
5601da177e4SLinus Torvalds *ohci_hwPSWp(ohci, td, 0) = cpu_to_hc16 (ohci,
5611da177e4SLinus Torvalds (data & 0x0FFF) | 0xE000);
5621da177e4SLinus Torvalds } else {
5631da177e4SLinus Torvalds td->hwCBP = cpu_to_hc32 (ohci, data);
5641da177e4SLinus Torvalds }
5651da177e4SLinus Torvalds if (data)
5661da177e4SLinus Torvalds td->hwBE = cpu_to_hc32 (ohci, data + len - 1);
5671da177e4SLinus Torvalds else
5681da177e4SLinus Torvalds td->hwBE = 0;
5691da177e4SLinus Torvalds td->hwNextTD = cpu_to_hc32 (ohci, td_pt->td_dma);
5701da177e4SLinus Torvalds
5711da177e4SLinus Torvalds /* append to queue */
5721da177e4SLinus Torvalds list_add_tail (&td->td_list, &td->ed->td_list);
5731da177e4SLinus Torvalds
5741da177e4SLinus Torvalds /* hash it for later reverse mapping */
5751da177e4SLinus Torvalds hash = TD_HASH_FUNC (td->td_dma);
5761da177e4SLinus Torvalds td->td_hash = ohci->td_hash [hash];
5771da177e4SLinus Torvalds ohci->td_hash [hash] = td;
5781da177e4SLinus Torvalds
5791da177e4SLinus Torvalds /* HC might read the TD (or cachelines) right away ... */
5801da177e4SLinus Torvalds wmb ();
5811da177e4SLinus Torvalds td->ed->hwTailP = td->hwNextTD;
5821da177e4SLinus Torvalds }
5831da177e4SLinus Torvalds
5841da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
5851da177e4SLinus Torvalds
5861da177e4SLinus Torvalds /* Prepare all TDs of a transfer, and queue them onto the ED.
5871da177e4SLinus Torvalds * Caller guarantees HC is active.
5881da177e4SLinus Torvalds * Usually the ED is already on the schedule, so TDs might be
5891da177e4SLinus Torvalds * processed as soon as they're queued.
5901da177e4SLinus Torvalds */
td_submit_urb(struct ohci_hcd * ohci,struct urb * urb)5911da177e4SLinus Torvalds static void td_submit_urb (
5921da177e4SLinus Torvalds struct ohci_hcd *ohci,
5931da177e4SLinus Torvalds struct urb *urb
5941da177e4SLinus Torvalds ) {
5951da177e4SLinus Torvalds struct urb_priv *urb_priv = urb->hcpriv;
5962621d011SManjunath Goudar struct device *dev = ohci_to_hcd(ohci)->self.controller;
5971da177e4SLinus Torvalds dma_addr_t data;
5981da177e4SLinus Torvalds int data_len = urb->transfer_buffer_length;
5991da177e4SLinus Torvalds int cnt = 0;
6001da177e4SLinus Torvalds u32 info = 0;
6011da177e4SLinus Torvalds int is_out = usb_pipeout (urb->pipe);
6021da177e4SLinus Torvalds int periodic = 0;
6036f65126cSAlan Stern int i, this_sg_len, n;
6046f65126cSAlan Stern struct scatterlist *sg;
6051da177e4SLinus Torvalds
6061da177e4SLinus Torvalds /* OHCI handles the bulk/interrupt data toggles itself. We just
6071da177e4SLinus Torvalds * use the device toggle bits for resetting, and rely on the fact
6081da177e4SLinus Torvalds * that resetting toggle is meaningless if the endpoint is active.
6091da177e4SLinus Torvalds */
6101da177e4SLinus Torvalds if (!usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe), is_out)) {
6111da177e4SLinus Torvalds usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe),
6121da177e4SLinus Torvalds is_out, 1);
6131da177e4SLinus Torvalds urb_priv->ed->hwHeadP &= ~cpu_to_hc32 (ohci, ED_C);
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds
6161da177e4SLinus Torvalds list_add (&urb_priv->pending, &ohci->pending);
6171da177e4SLinus Torvalds
6186f65126cSAlan Stern i = urb->num_mapped_sgs;
6196f65126cSAlan Stern if (data_len > 0 && i > 0) {
6206f65126cSAlan Stern sg = urb->sg;
6216f65126cSAlan Stern data = sg_dma_address(sg);
6226f65126cSAlan Stern
6236f65126cSAlan Stern /*
6246f65126cSAlan Stern * urb->transfer_buffer_length may be smaller than the
6256f65126cSAlan Stern * size of the scatterlist (or vice versa)
6266f65126cSAlan Stern */
6276f65126cSAlan Stern this_sg_len = min_t(int, sg_dma_len(sg), data_len);
6286f65126cSAlan Stern } else {
6296f65126cSAlan Stern sg = NULL;
6301da177e4SLinus Torvalds if (data_len)
6311da177e4SLinus Torvalds data = urb->transfer_dma;
6321da177e4SLinus Torvalds else
6331da177e4SLinus Torvalds data = 0;
6346f65126cSAlan Stern this_sg_len = data_len;
6356f65126cSAlan Stern }
6361da177e4SLinus Torvalds
6371da177e4SLinus Torvalds /* NOTE: TD_CC is set so we can tell which TDs the HC processed by
6381da177e4SLinus Torvalds * using TD_CC_GET, as well as by seeing them on the done list.
6391da177e4SLinus Torvalds * (CC = NotAccessed ... 0x0F, or 0x0E in PSWs for ISO.)
6401da177e4SLinus Torvalds */
6411da177e4SLinus Torvalds switch (urb_priv->ed->type) {
6421da177e4SLinus Torvalds
6431da177e4SLinus Torvalds /* Bulk and interrupt are identical except for where in the schedule
6441da177e4SLinus Torvalds * their EDs live.
6451da177e4SLinus Torvalds */
6461da177e4SLinus Torvalds case PIPE_INTERRUPT:
6471da177e4SLinus Torvalds /* ... and periodic urbs have extra accounting */
6481da177e4SLinus Torvalds periodic = ohci_to_hcd(ohci)->self.bandwidth_int_reqs++ == 0
6491da177e4SLinus Torvalds && ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0;
650*e288fc98SGustavo A. R. Silva fallthrough;
6511da177e4SLinus Torvalds case PIPE_BULK:
6521da177e4SLinus Torvalds info = is_out
6531da177e4SLinus Torvalds ? TD_T_TOGGLE | TD_CC | TD_DP_OUT
6541da177e4SLinus Torvalds : TD_T_TOGGLE | TD_CC | TD_DP_IN;
6551da177e4SLinus Torvalds /* TDs _could_ transfer up to 8K each */
6566f65126cSAlan Stern for (;;) {
6576f65126cSAlan Stern n = min(this_sg_len, 4096);
6586f65126cSAlan Stern
6591da177e4SLinus Torvalds /* maybe avoid ED halt on final TD short read */
6606f65126cSAlan Stern if (n >= data_len || (i == 1 && n >= this_sg_len)) {
6611da177e4SLinus Torvalds if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
6621da177e4SLinus Torvalds info |= TD_R;
6636f65126cSAlan Stern }
6646f65126cSAlan Stern td_fill(ohci, info, data, n, urb, cnt);
6656f65126cSAlan Stern this_sg_len -= n;
6666f65126cSAlan Stern data_len -= n;
6676f65126cSAlan Stern data += n;
6681da177e4SLinus Torvalds cnt++;
6696f65126cSAlan Stern
6706f65126cSAlan Stern if (this_sg_len <= 0) {
6716f65126cSAlan Stern if (--i <= 0 || data_len <= 0)
6726f65126cSAlan Stern break;
6736f65126cSAlan Stern sg = sg_next(sg);
6746f65126cSAlan Stern data = sg_dma_address(sg);
6756f65126cSAlan Stern this_sg_len = min_t(int, sg_dma_len(sg),
6766f65126cSAlan Stern data_len);
6776f65126cSAlan Stern }
6786f65126cSAlan Stern }
6791da177e4SLinus Torvalds if ((urb->transfer_flags & URB_ZERO_PACKET)
6801da177e4SLinus Torvalds && cnt < urb_priv->length) {
6811da177e4SLinus Torvalds td_fill (ohci, info, 0, 0, urb, cnt);
6821da177e4SLinus Torvalds cnt++;
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds /* maybe kickstart bulk list */
6851da177e4SLinus Torvalds if (urb_priv->ed->type == PIPE_BULK) {
6861da177e4SLinus Torvalds wmb ();
6871da177e4SLinus Torvalds ohci_writel (ohci, OHCI_BLF, &ohci->regs->cmdstatus);
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds break;
6901da177e4SLinus Torvalds
6911da177e4SLinus Torvalds /* control manages DATA0/DATA1 toggle per-request; SETUP resets it,
6921da177e4SLinus Torvalds * any DATA phase works normally, and the STATUS ack is special.
6931da177e4SLinus Torvalds */
6941da177e4SLinus Torvalds case PIPE_CONTROL:
6951da177e4SLinus Torvalds info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
6961da177e4SLinus Torvalds td_fill (ohci, info, urb->setup_dma, 8, urb, cnt++);
6971da177e4SLinus Torvalds if (data_len > 0) {
6981da177e4SLinus Torvalds info = TD_CC | TD_R | TD_T_DATA1;
6991da177e4SLinus Torvalds info |= is_out ? TD_DP_OUT : TD_DP_IN;
7001da177e4SLinus Torvalds /* NOTE: mishandles transfers >8K, some >4K */
7011da177e4SLinus Torvalds td_fill (ohci, info, data, data_len, urb, cnt++);
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds info = (is_out || data_len == 0)
7041da177e4SLinus Torvalds ? TD_CC | TD_DP_IN | TD_T_DATA1
7051da177e4SLinus Torvalds : TD_CC | TD_DP_OUT | TD_T_DATA1;
7061da177e4SLinus Torvalds td_fill (ohci, info, data, 0, urb, cnt++);
7071da177e4SLinus Torvalds /* maybe kickstart control list */
7081da177e4SLinus Torvalds wmb ();
7091da177e4SLinus Torvalds ohci_writel (ohci, OHCI_CLF, &ohci->regs->cmdstatus);
7101da177e4SLinus Torvalds break;
7111da177e4SLinus Torvalds
7121da177e4SLinus Torvalds /* ISO has no retransmit, so no toggle; and it uses special TDs.
7131da177e4SLinus Torvalds * Each TD could handle multiple consecutive frames (interval 1);
7141da177e4SLinus Torvalds * we could often reduce the number of TDs here.
7151da177e4SLinus Torvalds */
7161da177e4SLinus Torvalds case PIPE_ISOCHRONOUS:
7176a41b4d3SAlan Stern for (cnt = urb_priv->td_cnt; cnt < urb->number_of_packets;
7186a41b4d3SAlan Stern cnt++) {
7191da177e4SLinus Torvalds int frame = urb->start_frame;
7201da177e4SLinus Torvalds
7211da177e4SLinus Torvalds // FIXME scheduling should handle frame counter
7221da177e4SLinus Torvalds // roll-around ... exotic case (and OHCI has
7231da177e4SLinus Torvalds // a 2^16 iso range, vs other HCs max of 2^10)
7241da177e4SLinus Torvalds frame += cnt * urb->interval;
7251da177e4SLinus Torvalds frame &= 0xffff;
7261da177e4SLinus Torvalds td_fill (ohci, TD_CC | TD_ISO | frame,
7271da177e4SLinus Torvalds data + urb->iso_frame_desc [cnt].offset,
7281da177e4SLinus Torvalds urb->iso_frame_desc [cnt].length, urb, cnt);
7291da177e4SLinus Torvalds }
730a1f17a87SLibin Yang if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0) {
731a1f17a87SLibin Yang if (quirk_amdiso(ohci))
732ad93562bSAndiry Xu usb_amd_quirk_pll_disable();
733a1f17a87SLibin Yang if (quirk_amdprefetch(ohci))
7342621d011SManjunath Goudar sb800_prefetch(dev, 1);
735a1f17a87SLibin Yang }
7361da177e4SLinus Torvalds periodic = ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs++ == 0
7371da177e4SLinus Torvalds && ohci_to_hcd(ohci)->self.bandwidth_int_reqs == 0;
7381da177e4SLinus Torvalds break;
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds
7411da177e4SLinus Torvalds /* start periodic dma if needed */
7421da177e4SLinus Torvalds if (periodic) {
7431da177e4SLinus Torvalds wmb ();
7441da177e4SLinus Torvalds ohci->hc_control |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
7451da177e4SLinus Torvalds ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
7461da177e4SLinus Torvalds }
7471da177e4SLinus Torvalds
7481da177e4SLinus Torvalds // ASSERT (urb_priv->length == cnt);
7491da177e4SLinus Torvalds }
7501da177e4SLinus Torvalds
7511da177e4SLinus Torvalds /*-------------------------------------------------------------------------*
7521da177e4SLinus Torvalds * Done List handling functions
7531da177e4SLinus Torvalds *-------------------------------------------------------------------------*/
7541da177e4SLinus Torvalds
75555d84968SAlan Stern /* calculate transfer length/status and update the urb */
td_done(struct ohci_hcd * ohci,struct urb * urb,struct td * td)75655d84968SAlan Stern static int td_done(struct ohci_hcd *ohci, struct urb *urb, struct td *td)
7571da177e4SLinus Torvalds {
7581da177e4SLinus Torvalds u32 tdINFO = hc32_to_cpup (ohci, &td->hwINFO);
7591da177e4SLinus Torvalds int cc = 0;
76055d84968SAlan Stern int status = -EINPROGRESS;
7611da177e4SLinus Torvalds
7621da177e4SLinus Torvalds list_del (&td->td_list);
7631da177e4SLinus Torvalds
7641da177e4SLinus Torvalds /* ISO ... drivers see per-TD length/status */
7651da177e4SLinus Torvalds if (tdINFO & TD_ISO) {
7661da177e4SLinus Torvalds u16 tdPSW = ohci_hwPSW(ohci, td, 0);
7671da177e4SLinus Torvalds int dlen = 0;
7681da177e4SLinus Torvalds
7691da177e4SLinus Torvalds /* NOTE: assumes FC in tdINFO == 0, and that
7701da177e4SLinus Torvalds * only the first of 0..MAXPSW psws is used.
7711da177e4SLinus Torvalds */
7721da177e4SLinus Torvalds
7731da177e4SLinus Torvalds cc = (tdPSW >> 12) & 0xF;
7741da177e4SLinus Torvalds if (tdINFO & TD_CC) /* hc didn't touch? */
77555d84968SAlan Stern return status;
7761da177e4SLinus Torvalds
7771da177e4SLinus Torvalds if (usb_pipeout (urb->pipe))
7781da177e4SLinus Torvalds dlen = urb->iso_frame_desc [td->index].length;
7791da177e4SLinus Torvalds else {
7801da177e4SLinus Torvalds /* short reads are always OK for ISO */
7811da177e4SLinus Torvalds if (cc == TD_DATAUNDERRUN)
7821da177e4SLinus Torvalds cc = TD_CC_NOERROR;
7831da177e4SLinus Torvalds dlen = tdPSW & 0x3ff;
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds urb->actual_length += dlen;
7861da177e4SLinus Torvalds urb->iso_frame_desc [td->index].actual_length = dlen;
7871da177e4SLinus Torvalds urb->iso_frame_desc [td->index].status = cc_to_error [cc];
7881da177e4SLinus Torvalds
7891da177e4SLinus Torvalds if (cc != TD_CC_NOERROR)
790d2c4254fSOliver Neukum ohci_dbg(ohci,
7911da177e4SLinus Torvalds "urb %p iso td %p (%d) len %d cc %d\n",
7921da177e4SLinus Torvalds urb, td, 1 + td->index, dlen, cc);
7931da177e4SLinus Torvalds
7941da177e4SLinus Torvalds /* BULK, INT, CONTROL ... drivers see aggregate length/status,
7951da177e4SLinus Torvalds * except that "setup" bytes aren't counted and "short" transfers
7961da177e4SLinus Torvalds * might not be reported as errors.
7971da177e4SLinus Torvalds */
7981da177e4SLinus Torvalds } else {
7991da177e4SLinus Torvalds int type = usb_pipetype (urb->pipe);
8001da177e4SLinus Torvalds u32 tdBE = hc32_to_cpup (ohci, &td->hwBE);
8011da177e4SLinus Torvalds
8021da177e4SLinus Torvalds cc = TD_CC_GET (tdINFO);
8031da177e4SLinus Torvalds
8041da177e4SLinus Torvalds /* update packet status if needed (short is normally ok) */
8051da177e4SLinus Torvalds if (cc == TD_DATAUNDERRUN
8061da177e4SLinus Torvalds && !(urb->transfer_flags & URB_SHORT_NOT_OK))
8071da177e4SLinus Torvalds cc = TD_CC_NOERROR;
80855d84968SAlan Stern if (cc != TD_CC_NOERROR && cc < 0x0E)
80955d84968SAlan Stern status = cc_to_error[cc];
8101da177e4SLinus Torvalds
8111da177e4SLinus Torvalds /* count all non-empty packets except control SETUP packet */
8121da177e4SLinus Torvalds if ((type != PIPE_CONTROL || td->index != 0) && tdBE != 0) {
8131da177e4SLinus Torvalds if (td->hwCBP == 0)
8141da177e4SLinus Torvalds urb->actual_length += tdBE - td->data_dma + 1;
8151da177e4SLinus Torvalds else
8161da177e4SLinus Torvalds urb->actual_length +=
8171da177e4SLinus Torvalds hc32_to_cpup (ohci, &td->hwCBP)
8181da177e4SLinus Torvalds - td->data_dma;
8191da177e4SLinus Torvalds }
8201da177e4SLinus Torvalds
8211da177e4SLinus Torvalds if (cc != TD_CC_NOERROR && cc < 0x0E)
822d2c4254fSOliver Neukum ohci_dbg(ohci,
8231da177e4SLinus Torvalds "urb %p td %p (%d) cc %d, len=%d/%d\n",
8241da177e4SLinus Torvalds urb, td, 1 + td->index, cc,
8251da177e4SLinus Torvalds urb->actual_length,
8261da177e4SLinus Torvalds urb->transfer_buffer_length);
8271da177e4SLinus Torvalds }
82855d84968SAlan Stern return status;
8291da177e4SLinus Torvalds }
8301da177e4SLinus Torvalds
8311da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
8321da177e4SLinus Torvalds
ed_halted(struct ohci_hcd * ohci,struct td * td,int cc)8336e8fe43bSAlan Stern static void ed_halted(struct ohci_hcd *ohci, struct td *td, int cc)
8341da177e4SLinus Torvalds {
8351da177e4SLinus Torvalds struct urb *urb = td->urb;
8366e8fe43bSAlan Stern urb_priv_t *urb_priv = urb->hcpriv;
8371da177e4SLinus Torvalds struct ed *ed = td->ed;
8381da177e4SLinus Torvalds struct list_head *tmp = td->td_list.next;
8391da177e4SLinus Torvalds __hc32 toggle = ed->hwHeadP & cpu_to_hc32 (ohci, ED_C);
8401da177e4SLinus Torvalds
8411da177e4SLinus Torvalds /* clear ed halt; this is the td that caused it, but keep it inactive
8421da177e4SLinus Torvalds * until its urb->complete() has a chance to clean up.
8431da177e4SLinus Torvalds */
8441da177e4SLinus Torvalds ed->hwINFO |= cpu_to_hc32 (ohci, ED_SKIP);
8451da177e4SLinus Torvalds wmb ();
8461da177e4SLinus Torvalds ed->hwHeadP &= ~cpu_to_hc32 (ohci, ED_H);
8471da177e4SLinus Torvalds
8486e8fe43bSAlan Stern /* Get rid of all later tds from this urb. We don't have
8496e8fe43bSAlan Stern * to be careful: no errors and nothing was transferred.
8506e8fe43bSAlan Stern * Also patch the ed so it looks as if those tds completed normally.
8511da177e4SLinus Torvalds */
8521da177e4SLinus Torvalds while (tmp != &ed->td_list) {
8531da177e4SLinus Torvalds struct td *next;
8541da177e4SLinus Torvalds
8551da177e4SLinus Torvalds next = list_entry (tmp, struct td, td_list);
8561da177e4SLinus Torvalds tmp = next->td_list.next;
8571da177e4SLinus Torvalds
8581da177e4SLinus Torvalds if (next->urb != urb)
8591da177e4SLinus Torvalds break;
8601da177e4SLinus Torvalds
8611da177e4SLinus Torvalds /* NOTE: if multi-td control DATA segments get supported,
8621da177e4SLinus Torvalds * this urb had one of them, this td wasn't the last td
8631da177e4SLinus Torvalds * in that segment (TD_R clear), this ed halted because
8641da177e4SLinus Torvalds * of a short read, _and_ URB_SHORT_NOT_OK is clear ...
8651da177e4SLinus Torvalds * then we need to leave the control STATUS packet queued
8661da177e4SLinus Torvalds * and clear ED_SKIP.
8671da177e4SLinus Torvalds */
8681da177e4SLinus Torvalds
8696e8fe43bSAlan Stern list_del(&next->td_list);
8706e8fe43bSAlan Stern urb_priv->td_cnt++;
8711da177e4SLinus Torvalds ed->hwHeadP = next->hwNextTD | toggle;
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds
8741da177e4SLinus Torvalds /* help for troubleshooting: report anything that
8751da177e4SLinus Torvalds * looks odd ... that doesn't include protocol stalls
8761da177e4SLinus Torvalds * (or maybe some other things)
8771da177e4SLinus Torvalds */
8781da177e4SLinus Torvalds switch (cc) {
8791da177e4SLinus Torvalds case TD_DATAUNDERRUN:
8801da177e4SLinus Torvalds if ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)
8811da177e4SLinus Torvalds break;
882*e288fc98SGustavo A. R. Silva fallthrough;
8831da177e4SLinus Torvalds case TD_CC_STALL:
8841da177e4SLinus Torvalds if (usb_pipecontrol (urb->pipe))
8851da177e4SLinus Torvalds break;
886*e288fc98SGustavo A. R. Silva fallthrough;
8871da177e4SLinus Torvalds default:
8881da177e4SLinus Torvalds ohci_dbg (ohci,
8891da177e4SLinus Torvalds "urb %p path %s ep%d%s %08x cc %d --> status %d\n",
8901da177e4SLinus Torvalds urb, urb->dev->devpath,
8911da177e4SLinus Torvalds usb_pipeendpoint (urb->pipe),
8921da177e4SLinus Torvalds usb_pipein (urb->pipe) ? "in" : "out",
8931da177e4SLinus Torvalds hc32_to_cpu (ohci, td->hwINFO),
8941da177e4SLinus Torvalds cc, cc_to_error [cc]);
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds
898c6fcb85eSAlan Stern /* Add a TD to the done list */
add_to_done_list(struct ohci_hcd * ohci,struct td * td)899c6fcb85eSAlan Stern static void add_to_done_list(struct ohci_hcd *ohci, struct td *td)
900c6fcb85eSAlan Stern {
901c6fcb85eSAlan Stern struct td *td2, *td_prev;
902c6fcb85eSAlan Stern struct ed *ed;
903c6fcb85eSAlan Stern
904c6fcb85eSAlan Stern if (td->next_dl_td)
905c6fcb85eSAlan Stern return; /* Already on the list */
906c6fcb85eSAlan Stern
907c6fcb85eSAlan Stern /* Add all the TDs going back until we reach one that's on the list */
908c6fcb85eSAlan Stern ed = td->ed;
909c6fcb85eSAlan Stern td2 = td_prev = td;
910c6fcb85eSAlan Stern list_for_each_entry_continue_reverse(td2, &ed->td_list, td_list) {
911c6fcb85eSAlan Stern if (td2->next_dl_td)
912c6fcb85eSAlan Stern break;
913c6fcb85eSAlan Stern td2->next_dl_td = td_prev;
914c6fcb85eSAlan Stern td_prev = td2;
915c6fcb85eSAlan Stern }
916c6fcb85eSAlan Stern
917c6fcb85eSAlan Stern if (ohci->dl_end)
918c6fcb85eSAlan Stern ohci->dl_end->next_dl_td = td_prev;
919c6fcb85eSAlan Stern else
920c6fcb85eSAlan Stern ohci->dl_start = td_prev;
921c6fcb85eSAlan Stern
922c6fcb85eSAlan Stern /*
923c6fcb85eSAlan Stern * Make td->next_dl_td point to td itself, to mark the fact
924c6fcb85eSAlan Stern * that td is on the done list.
9251da177e4SLinus Torvalds */
926c6fcb85eSAlan Stern ohci->dl_end = td->next_dl_td = td;
92781e38333SAlan Stern
92881e38333SAlan Stern /* Did we just add the latest pending TD? */
92981e38333SAlan Stern td2 = ed->pending_td;
93081e38333SAlan Stern if (td2 && td2->next_dl_td)
93181e38333SAlan Stern ed->pending_td = NULL;
932c6fcb85eSAlan Stern }
933c6fcb85eSAlan Stern
934c6fcb85eSAlan Stern /* Get the entries on the hardware done queue and put them on our list */
update_done_list(struct ohci_hcd * ohci)935c6fcb85eSAlan Stern static void update_done_list(struct ohci_hcd *ohci)
9361da177e4SLinus Torvalds {
9371da177e4SLinus Torvalds u32 td_dma;
9381da177e4SLinus Torvalds struct td *td = NULL;
9391da177e4SLinus Torvalds
9401da177e4SLinus Torvalds td_dma = hc32_to_cpup (ohci, &ohci->hcca->done_head);
9411da177e4SLinus Torvalds ohci->hcca->done_head = 0;
9421da177e4SLinus Torvalds wmb();
9431da177e4SLinus Torvalds
9441da177e4SLinus Torvalds /* get TD from hc's singly linked list, and
945c6fcb85eSAlan Stern * add to ours. ed->td_list changes later.
9461da177e4SLinus Torvalds */
9471da177e4SLinus Torvalds while (td_dma) {
9481da177e4SLinus Torvalds int cc;
9491da177e4SLinus Torvalds
9501da177e4SLinus Torvalds td = dma_to_td (ohci, td_dma);
9511da177e4SLinus Torvalds if (!td) {
9521da177e4SLinus Torvalds ohci_err (ohci, "bad entry %8x\n", td_dma);
9531da177e4SLinus Torvalds break;
9541da177e4SLinus Torvalds }
9551da177e4SLinus Torvalds
9561da177e4SLinus Torvalds td->hwINFO |= cpu_to_hc32 (ohci, TD_DONE);
9571da177e4SLinus Torvalds cc = TD_CC_GET (hc32_to_cpup (ohci, &td->hwINFO));
9581da177e4SLinus Torvalds
9591da177e4SLinus Torvalds /* Non-iso endpoints can halt on error; un-halt,
9601da177e4SLinus Torvalds * and dequeue any other TDs from this urb.
9611da177e4SLinus Torvalds * No other TD could have caused the halt.
9621da177e4SLinus Torvalds */
9631da177e4SLinus Torvalds if (cc != TD_CC_NOERROR
9641da177e4SLinus Torvalds && (td->ed->hwHeadP & cpu_to_hc32 (ohci, ED_H)))
9656e8fe43bSAlan Stern ed_halted(ohci, td, cc);
9661da177e4SLinus Torvalds
9671da177e4SLinus Torvalds td_dma = hc32_to_cpup (ohci, &td->hwNextTD);
968c6fcb85eSAlan Stern add_to_done_list(ohci, td);
9691da177e4SLinus Torvalds }
9701da177e4SLinus Torvalds }
9711da177e4SLinus Torvalds
9721da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
9731da177e4SLinus Torvalds
9741da177e4SLinus Torvalds /* there are some urbs/eds to unlink; called in_irq(), with HCD locked */
finish_unlinks(struct ohci_hcd * ohci)975cdb4dd15SAlan Stern static void finish_unlinks(struct ohci_hcd *ohci)
9761da177e4SLinus Torvalds {
977cdb4dd15SAlan Stern unsigned tick = ohci_frame_no(ohci);
9781da177e4SLinus Torvalds struct ed *ed, **last;
9791da177e4SLinus Torvalds
9801da177e4SLinus Torvalds rescan_all:
9811da177e4SLinus Torvalds for (last = &ohci->ed_rm_list, ed = *last; ed != NULL; ed = *last) {
9821da177e4SLinus Torvalds struct list_head *entry, *tmp;
9831da177e4SLinus Torvalds int completed, modified;
9841da177e4SLinus Torvalds __hc32 *prev;
9851da177e4SLinus Torvalds
9861da177e4SLinus Torvalds /* only take off EDs that the HC isn't using, accounting for
9871da177e4SLinus Torvalds * frame counter wraps and EDs with partially retired TDs
9881da177e4SLinus Torvalds */
989c6fcb85eSAlan Stern if (likely(ohci->rh_state == OHCI_RH_RUNNING) &&
990c6fcb85eSAlan Stern tick_before(tick, ed->tick)) {
9911da177e4SLinus Torvalds skip_ed:
9921da177e4SLinus Torvalds last = &ed->ed_next;
9931da177e4SLinus Torvalds continue;
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds if (!list_empty(&ed->td_list)) {
9961da177e4SLinus Torvalds struct td *td;
9971da177e4SLinus Torvalds u32 head;
9981da177e4SLinus Torvalds
999c6fcb85eSAlan Stern td = list_first_entry(&ed->td_list, struct td, td_list);
10001da177e4SLinus Torvalds
10011da177e4SLinus Torvalds /* INTR_WDH may need to clean up first */
1002c6fcb85eSAlan Stern head = hc32_to_cpu(ohci, ed->hwHeadP) & TD_MASK;
1003c6fcb85eSAlan Stern if (td->td_dma != head &&
1004c6fcb85eSAlan Stern ohci->rh_state == OHCI_RH_RUNNING)
10051da177e4SLinus Torvalds goto skip_ed;
1006c6fcb85eSAlan Stern
1007c6fcb85eSAlan Stern /* Don't mess up anything already on the done list */
1008c6fcb85eSAlan Stern if (td->next_dl_td)
1009c6fcb85eSAlan Stern goto skip_ed;
10101da177e4SLinus Torvalds }
10111da177e4SLinus Torvalds
1012977dcfdcSAlan Stern /* ED's now officially unlinked, hc doesn't see */
1013977dcfdcSAlan Stern ed->hwHeadP &= ~cpu_to_hc32(ohci, ED_H);
1014977dcfdcSAlan Stern ed->hwNextED = 0;
1015977dcfdcSAlan Stern wmb();
1016977dcfdcSAlan Stern ed->hwINFO &= ~cpu_to_hc32(ohci, ED_SKIP | ED_DEQUEUE);
1017977dcfdcSAlan Stern
10181da177e4SLinus Torvalds /* reentrancy: if we drop the schedule lock, someone might
10191da177e4SLinus Torvalds * have modified this list. normally it's just prepending
10201da177e4SLinus Torvalds * entries (which we'd ignore), but paranoia won't hurt.
10211da177e4SLinus Torvalds */
102246408ea5SAMAN DEEP *last = ed->ed_next;
102346408ea5SAMAN DEEP ed->ed_next = NULL;
10241da177e4SLinus Torvalds modified = 0;
10251da177e4SLinus Torvalds
10261da177e4SLinus Torvalds /* unlink urbs as requested, but rescan the list after
10271da177e4SLinus Torvalds * we call a completion since it might have unlinked
10281da177e4SLinus Torvalds * another (earlier) urb
10291da177e4SLinus Torvalds *
10301da177e4SLinus Torvalds * When we get here, the HC doesn't see this ed. But it
10311da177e4SLinus Torvalds * must not be rescheduled until all completed URBs have
10321da177e4SLinus Torvalds * been given back to the driver.
10331da177e4SLinus Torvalds */
10341da177e4SLinus Torvalds rescan_this:
10351da177e4SLinus Torvalds completed = 0;
10361da177e4SLinus Torvalds prev = &ed->hwHeadP;
10371da177e4SLinus Torvalds list_for_each_safe (entry, tmp, &ed->td_list) {
10381da177e4SLinus Torvalds struct td *td;
10391da177e4SLinus Torvalds struct urb *urb;
10401da177e4SLinus Torvalds urb_priv_t *urb_priv;
10411da177e4SLinus Torvalds __hc32 savebits;
104229c8f6a7SDavid Brownell u32 tdINFO;
10431da177e4SLinus Torvalds
10441da177e4SLinus Torvalds td = list_entry (entry, struct td, td_list);
10451da177e4SLinus Torvalds urb = td->urb;
10461da177e4SLinus Torvalds urb_priv = td->urb->hcpriv;
10471da177e4SLinus Torvalds
1048eb231054SAlan Stern if (!urb->unlinked) {
10491da177e4SLinus Torvalds prev = &td->hwNextTD;
10501da177e4SLinus Torvalds continue;
10511da177e4SLinus Torvalds }
10521da177e4SLinus Torvalds
10531da177e4SLinus Torvalds /* patch pointer hc uses */
10541da177e4SLinus Torvalds savebits = *prev & ~cpu_to_hc32 (ohci, TD_MASK);
10551da177e4SLinus Torvalds *prev = td->hwNextTD | savebits;
10561da177e4SLinus Torvalds
105729c8f6a7SDavid Brownell /* If this was unlinked, the TD may not have been
105829c8f6a7SDavid Brownell * retired ... so manually save the data toggle.
105929c8f6a7SDavid Brownell * The controller ignores the value we save for
106029c8f6a7SDavid Brownell * control and ISO endpoints.
106129c8f6a7SDavid Brownell */
106229c8f6a7SDavid Brownell tdINFO = hc32_to_cpup(ohci, &td->hwINFO);
106329c8f6a7SDavid Brownell if ((tdINFO & TD_T) == TD_T_DATA0)
106429c8f6a7SDavid Brownell ed->hwHeadP &= ~cpu_to_hc32(ohci, ED_C);
106529c8f6a7SDavid Brownell else if ((tdINFO & TD_T) == TD_T_DATA1)
106629c8f6a7SDavid Brownell ed->hwHeadP |= cpu_to_hc32(ohci, ED_C);
106729c8f6a7SDavid Brownell
10681da177e4SLinus Torvalds /* HC may have partly processed this TD */
10691da177e4SLinus Torvalds td_done (ohci, urb, td);
10701da177e4SLinus Torvalds urb_priv->td_cnt++;
10711da177e4SLinus Torvalds
10721da177e4SLinus Torvalds /* if URB is done, clean up */
1073a8693424SAlan Stern if (urb_priv->td_cnt >= urb_priv->length) {
10741da177e4SLinus Torvalds modified = completed = 1;
107555d84968SAlan Stern finish_urb(ohci, urb, 0);
10761da177e4SLinus Torvalds }
10771da177e4SLinus Torvalds }
10781da177e4SLinus Torvalds if (completed && !list_empty (&ed->td_list))
10791da177e4SLinus Torvalds goto rescan_this;
10801da177e4SLinus Torvalds
1081977dcfdcSAlan Stern /*
108246408ea5SAMAN DEEP * If no TDs are queued, ED is now idle.
1083977dcfdcSAlan Stern * Otherwise, if the HC is running, reschedule.
108446408ea5SAMAN DEEP * If the HC isn't running, add ED back to the
108546408ea5SAMAN DEEP * start of the list for later processing.
1086977dcfdcSAlan Stern */
1087977dcfdcSAlan Stern if (list_empty(&ed->td_list)) {
10887d8021c9SAlan Stern ed->state = ED_IDLE;
108981e38333SAlan Stern list_del(&ed->in_use_list);
1090977dcfdcSAlan Stern } else if (ohci->rh_state == OHCI_RH_RUNNING) {
10911da177e4SLinus Torvalds ed_schedule(ohci, ed);
1092977dcfdcSAlan Stern } else {
109346408ea5SAMAN DEEP ed->ed_next = ohci->ed_rm_list;
109446408ea5SAMAN DEEP ohci->ed_rm_list = ed;
109546408ea5SAMAN DEEP /* Don't loop on the same ED */
109646408ea5SAMAN DEEP if (last == &ohci->ed_rm_list)
1097977dcfdcSAlan Stern last = &ed->ed_next;
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds
11001da177e4SLinus Torvalds if (modified)
11011da177e4SLinus Torvalds goto rescan_all;
11021da177e4SLinus Torvalds }
11031da177e4SLinus Torvalds
11041da177e4SLinus Torvalds /* maybe reenable control and bulk lists */
1105b7463c71SAlan Stern if (ohci->rh_state == OHCI_RH_RUNNING && !ohci->ed_rm_list) {
11061da177e4SLinus Torvalds u32 command = 0, control = 0;
11071da177e4SLinus Torvalds
11081da177e4SLinus Torvalds if (ohci->ed_controltail) {
11091da177e4SLinus Torvalds command |= OHCI_CLF;
111089a0fd18SMike Nuss if (quirk_zfmicro(ohci))
11110e498763SDavid Brownell mdelay(1);
11121da177e4SLinus Torvalds if (!(ohci->hc_control & OHCI_CTRL_CLE)) {
11131da177e4SLinus Torvalds control |= OHCI_CTRL_CLE;
11141da177e4SLinus Torvalds ohci_writel (ohci, 0,
11151da177e4SLinus Torvalds &ohci->regs->ed_controlcurrent);
11161da177e4SLinus Torvalds }
11171da177e4SLinus Torvalds }
11181da177e4SLinus Torvalds if (ohci->ed_bulktail) {
11191da177e4SLinus Torvalds command |= OHCI_BLF;
112089a0fd18SMike Nuss if (quirk_zfmicro(ohci))
11210e498763SDavid Brownell mdelay(1);
11221da177e4SLinus Torvalds if (!(ohci->hc_control & OHCI_CTRL_BLE)) {
11231da177e4SLinus Torvalds control |= OHCI_CTRL_BLE;
11241da177e4SLinus Torvalds ohci_writel (ohci, 0,
11251da177e4SLinus Torvalds &ohci->regs->ed_bulkcurrent);
11261da177e4SLinus Torvalds }
11271da177e4SLinus Torvalds }
11281da177e4SLinus Torvalds
11291da177e4SLinus Torvalds /* CLE/BLE to enable, CLF/BLF to (maybe) kickstart */
11301da177e4SLinus Torvalds if (control) {
11311da177e4SLinus Torvalds ohci->hc_control |= control;
113289a0fd18SMike Nuss if (quirk_zfmicro(ohci))
11330e498763SDavid Brownell mdelay(1);
11341da177e4SLinus Torvalds ohci_writel (ohci, ohci->hc_control,
11351da177e4SLinus Torvalds &ohci->regs->control);
11361da177e4SLinus Torvalds }
11370e498763SDavid Brownell if (command) {
113889a0fd18SMike Nuss if (quirk_zfmicro(ohci))
11390e498763SDavid Brownell mdelay(1);
11401da177e4SLinus Torvalds ohci_writel (ohci, command, &ohci->regs->cmdstatus);
11411da177e4SLinus Torvalds }
11421da177e4SLinus Torvalds }
11430e498763SDavid Brownell }
11441da177e4SLinus Torvalds
11451da177e4SLinus Torvalds
11461da177e4SLinus Torvalds
11471da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
11481da177e4SLinus Torvalds
114995d9a01dSAlan Stern /* Take back a TD from the host controller */
takeback_td(struct ohci_hcd * ohci,struct td * td)115089a0fd18SMike Nuss static void takeback_td(struct ohci_hcd *ohci, struct td *td)
11511da177e4SLinus Torvalds {
11521da177e4SLinus Torvalds struct urb *urb = td->urb;
11531da177e4SLinus Torvalds urb_priv_t *urb_priv = urb->hcpriv;
11541da177e4SLinus Torvalds struct ed *ed = td->ed;
115555d84968SAlan Stern int status;
11561da177e4SLinus Torvalds
11571da177e4SLinus Torvalds /* update URB's length and status from TD */
115855d84968SAlan Stern status = td_done(ohci, urb, td);
11591da177e4SLinus Torvalds urb_priv->td_cnt++;
11601da177e4SLinus Torvalds
11611da177e4SLinus Torvalds /* If all this urb's TDs are done, call complete() */
1162a8693424SAlan Stern if (urb_priv->td_cnt >= urb_priv->length)
116355d84968SAlan Stern finish_urb(ohci, urb, status);
11641da177e4SLinus Torvalds
11651da177e4SLinus Torvalds /* clean schedule: unlink EDs that are no longer busy */
11661da177e4SLinus Torvalds if (list_empty(&ed->td_list)) {
11671da177e4SLinus Torvalds if (ed->state == ED_OPER)
11681da177e4SLinus Torvalds start_ed_unlink(ohci, ed);
11691da177e4SLinus Torvalds
11701da177e4SLinus Torvalds /* ... reenabling halted EDs only after fault cleanup */
117189a0fd18SMike Nuss } else if ((ed->hwINFO & cpu_to_hc32(ohci, ED_SKIP | ED_DEQUEUE))
11721da177e4SLinus Torvalds == cpu_to_hc32(ohci, ED_SKIP)) {
11731da177e4SLinus Torvalds td = list_entry(ed->td_list.next, struct td, td_list);
11741da177e4SLinus Torvalds if (!(td->hwINFO & cpu_to_hc32(ohci, TD_DONE))) {
11751da177e4SLinus Torvalds ed->hwINFO &= ~cpu_to_hc32(ohci, ED_SKIP);
11761da177e4SLinus Torvalds /* ... hc may need waking-up */
11771da177e4SLinus Torvalds switch (ed->type) {
11781da177e4SLinus Torvalds case PIPE_CONTROL:
11791da177e4SLinus Torvalds ohci_writel(ohci, OHCI_CLF,
11801da177e4SLinus Torvalds &ohci->regs->cmdstatus);
11811da177e4SLinus Torvalds break;
11821da177e4SLinus Torvalds case PIPE_BULK:
11831da177e4SLinus Torvalds ohci_writel(ohci, OHCI_BLF,
11841da177e4SLinus Torvalds &ohci->regs->cmdstatus);
11851da177e4SLinus Torvalds break;
11861da177e4SLinus Torvalds }
11871da177e4SLinus Torvalds }
11881da177e4SLinus Torvalds }
118989a0fd18SMike Nuss }
11901da177e4SLinus Torvalds
119189a0fd18SMike Nuss /*
119289a0fd18SMike Nuss * Process normal completions (error or success) and clean the schedules.
119389a0fd18SMike Nuss *
119489a0fd18SMike Nuss * This is the main path for handing urbs back to drivers. The only other
119589a0fd18SMike Nuss * normal path is finish_unlinks(), which unlinks URBs using ed_rm_list,
119695d9a01dSAlan Stern * instead of scanning the (re-reversed) donelist as this does.
119789a0fd18SMike Nuss */
process_done_list(struct ohci_hcd * ohci)1198c6fcb85eSAlan Stern static void process_done_list(struct ohci_hcd *ohci)
119989a0fd18SMike Nuss {
1200c6fcb85eSAlan Stern struct td *td;
120189a0fd18SMike Nuss
1202c6fcb85eSAlan Stern while (ohci->dl_start) {
1203c6fcb85eSAlan Stern td = ohci->dl_start;
1204c6fcb85eSAlan Stern if (td == ohci->dl_end)
1205c6fcb85eSAlan Stern ohci->dl_start = ohci->dl_end = NULL;
1206c6fcb85eSAlan Stern else
1207c6fcb85eSAlan Stern ohci->dl_start = td->next_dl_td;
120850ce5c06SAlan Stern
120989a0fd18SMike Nuss takeback_td(ohci, td);
12101da177e4SLinus Torvalds }
12111da177e4SLinus Torvalds }
1212cdb4dd15SAlan Stern
1213cdb4dd15SAlan Stern /*
1214cdb4dd15SAlan Stern * TD takeback and URB giveback must be single-threaded.
1215cdb4dd15SAlan Stern * This routine takes care of it all.
1216cdb4dd15SAlan Stern */
ohci_work(struct ohci_hcd * ohci)1217cdb4dd15SAlan Stern static void ohci_work(struct ohci_hcd *ohci)
1218cdb4dd15SAlan Stern {
1219cdb4dd15SAlan Stern if (ohci->working) {
1220cdb4dd15SAlan Stern ohci->restart_work = 1;
1221cdb4dd15SAlan Stern return;
1222cdb4dd15SAlan Stern }
1223cdb4dd15SAlan Stern ohci->working = 1;
1224cdb4dd15SAlan Stern
1225cdb4dd15SAlan Stern restart:
1226cdb4dd15SAlan Stern process_done_list(ohci);
1227cdb4dd15SAlan Stern if (ohci->ed_rm_list)
1228cdb4dd15SAlan Stern finish_unlinks(ohci);
1229cdb4dd15SAlan Stern
1230cdb4dd15SAlan Stern if (ohci->restart_work) {
1231cdb4dd15SAlan Stern ohci->restart_work = 0;
1232cdb4dd15SAlan Stern goto restart;
1233cdb4dd15SAlan Stern }
1234cdb4dd15SAlan Stern ohci->working = 0;
1235cdb4dd15SAlan Stern }
1236