11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * OHCI HCD (Host Controller Driver) for USB. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at> 51da177e4SLinus Torvalds * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This file is licenced under the GPL. 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds 107d12e780SDavid Howells #include <linux/irq.h> 115a0e3ad6STejun Heo #include <linux/slab.h> 127d12e780SDavid Howells 131da177e4SLinus Torvalds static void urb_free_priv (struct ohci_hcd *hc, urb_priv_t *urb_priv) 141da177e4SLinus Torvalds { 151da177e4SLinus Torvalds int last = urb_priv->length - 1; 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds if (last >= 0) { 181da177e4SLinus Torvalds int i; 191da177e4SLinus Torvalds struct td *td; 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds for (i = 0; i <= last; i++) { 221da177e4SLinus Torvalds td = urb_priv->td [i]; 231da177e4SLinus Torvalds if (td) 241da177e4SLinus Torvalds td_free (hc, td); 251da177e4SLinus Torvalds } 261da177e4SLinus Torvalds } 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds list_del (&urb_priv->pending); 291da177e4SLinus Torvalds kfree (urb_priv); 301da177e4SLinus Torvalds } 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds /* 351da177e4SLinus Torvalds * URB goes back to driver, and isn't reissued. 361da177e4SLinus Torvalds * It's completely gone from HC data structures. 371da177e4SLinus Torvalds * PRECONDITION: ohci lock held, irqs blocked. 381da177e4SLinus Torvalds */ 391da177e4SLinus Torvalds static void 4055d84968SAlan Stern finish_urb(struct ohci_hcd *ohci, struct urb *urb, int status) 411da177e4SLinus Torvalds __releases(ohci->lock) 421da177e4SLinus Torvalds __acquires(ohci->lock) 431da177e4SLinus Torvalds { 442621d011SManjunath Goudar struct device *dev = ohci_to_hcd(ohci)->self.controller; 45a8693424SAlan Stern struct usb_host_endpoint *ep = urb->ep; 46a8693424SAlan Stern struct urb_priv *urb_priv; 47a8693424SAlan Stern 481da177e4SLinus Torvalds // ASSERT (urb->hcpriv != 0); 491da177e4SLinus Torvalds 50a8693424SAlan Stern restart: 511da177e4SLinus Torvalds urb_free_priv (ohci, urb->hcpriv); 52ece1d77eSChen Gang urb->hcpriv = NULL; 5355d84968SAlan Stern if (likely(status == -EINPROGRESS)) 5455d84968SAlan Stern status = 0; 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds switch (usb_pipetype (urb->pipe)) { 571da177e4SLinus Torvalds case PIPE_ISOCHRONOUS: 581da177e4SLinus Torvalds ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs--; 59a1f17a87SLibin Yang if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0) { 60a1f17a87SLibin Yang if (quirk_amdiso(ohci)) 61ad93562bSAndiry Xu usb_amd_quirk_pll_enable(); 62a1f17a87SLibin Yang if (quirk_amdprefetch(ohci)) 632621d011SManjunath Goudar sb800_prefetch(dev, 0); 64a1f17a87SLibin Yang } 651da177e4SLinus Torvalds break; 661da177e4SLinus Torvalds case PIPE_INTERRUPT: 671da177e4SLinus Torvalds ohci_to_hcd(ohci)->self.bandwidth_int_reqs--; 681da177e4SLinus Torvalds break; 691da177e4SLinus Torvalds } 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds /* urb->complete() can reenter this HCD */ 72e9df41c5SAlan Stern usb_hcd_unlink_urb_from_ep(ohci_to_hcd(ohci), urb); 731da177e4SLinus Torvalds spin_unlock (&ohci->lock); 744a00027dSAlan Stern usb_hcd_giveback_urb(ohci_to_hcd(ohci), urb, status); 751da177e4SLinus Torvalds spin_lock (&ohci->lock); 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds /* stop periodic dma if it's not needed */ 781da177e4SLinus Torvalds if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0 791da177e4SLinus Torvalds && ohci_to_hcd(ohci)->self.bandwidth_int_reqs == 0) { 801da177e4SLinus Torvalds ohci->hc_control &= ~(OHCI_CTRL_PLE|OHCI_CTRL_IE); 811da177e4SLinus Torvalds ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); 821da177e4SLinus Torvalds } 83a8693424SAlan Stern 84a8693424SAlan Stern /* 85a8693424SAlan Stern * An isochronous URB that is sumitted too late won't have any TDs 86a8693424SAlan Stern * (marked by the fact that the td_cnt value is larger than the 87a8693424SAlan Stern * actual number of TDs). If the next URB on this endpoint is like 88a8693424SAlan Stern * that, give it back now. 89a8693424SAlan Stern */ 90a8693424SAlan Stern if (!list_empty(&ep->urb_list)) { 91a8693424SAlan Stern urb = list_first_entry(&ep->urb_list, struct urb, urb_list); 92a8693424SAlan Stern urb_priv = urb->hcpriv; 93a8693424SAlan Stern if (urb_priv->td_cnt > urb_priv->length) { 94a8693424SAlan Stern status = 0; 95a8693424SAlan Stern goto restart; 96a8693424SAlan Stern } 97a8693424SAlan Stern } 981da177e4SLinus Torvalds } 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds /*-------------------------------------------------------------------------* 1021da177e4SLinus Torvalds * ED handling functions 1031da177e4SLinus Torvalds *-------------------------------------------------------------------------*/ 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds /* search for the right schedule branch to use for a periodic ed. 1061da177e4SLinus Torvalds * does some load balancing; returns the branch, or negative errno. 1071da177e4SLinus Torvalds */ 1081da177e4SLinus Torvalds static int balance (struct ohci_hcd *ohci, int interval, int load) 1091da177e4SLinus Torvalds { 1101da177e4SLinus Torvalds int i, branch = -ENOSPC; 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds /* iso periods can be huge; iso tds specify frame numbers */ 1131da177e4SLinus Torvalds if (interval > NUM_INTS) 1141da177e4SLinus Torvalds interval = NUM_INTS; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds /* search for the least loaded schedule branch of that period 1171da177e4SLinus Torvalds * that has enough bandwidth left unreserved. 1181da177e4SLinus Torvalds */ 1191da177e4SLinus Torvalds for (i = 0; i < interval ; i++) { 1201da177e4SLinus Torvalds if (branch < 0 || ohci->load [branch] > ohci->load [i]) { 1211da177e4SLinus Torvalds int j; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds /* usb 1.1 says 90% of one frame */ 1241da177e4SLinus Torvalds for (j = i; j < NUM_INTS; j += interval) { 1251da177e4SLinus Torvalds if ((ohci->load [j] + load) > 900) 1261da177e4SLinus Torvalds break; 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds if (j < NUM_INTS) 1291da177e4SLinus Torvalds continue; 1301da177e4SLinus Torvalds branch = i; 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds return branch; 1341da177e4SLinus Torvalds } 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds /* both iso and interrupt requests have periods; this routine puts them 1391da177e4SLinus Torvalds * into the schedule tree in the apppropriate place. most iso devices use 1401da177e4SLinus Torvalds * 1msec periods, but that's not required. 1411da177e4SLinus Torvalds */ 1421da177e4SLinus Torvalds static void periodic_link (struct ohci_hcd *ohci, struct ed *ed) 1431da177e4SLinus Torvalds { 1441da177e4SLinus Torvalds unsigned i; 1451da177e4SLinus Torvalds 146d2c4254fSOliver Neukum ohci_dbg(ohci, "link %sed %p branch %d [%dus.], interval %d\n", 1471da177e4SLinus Torvalds (ed->hwINFO & cpu_to_hc32 (ohci, ED_ISO)) ? "iso " : "", 1481da177e4SLinus Torvalds ed, ed->branch, ed->load, ed->interval); 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds for (i = ed->branch; i < NUM_INTS; i += ed->interval) { 1511da177e4SLinus Torvalds struct ed **prev = &ohci->periodic [i]; 1521da177e4SLinus Torvalds __hc32 *prev_p = &ohci->hcca->int_table [i]; 1531da177e4SLinus Torvalds struct ed *here = *prev; 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds /* sorting each branch by period (slow before fast) 1561da177e4SLinus Torvalds * lets us share the faster parts of the tree. 1571da177e4SLinus Torvalds * (plus maybe: put interrupt eds before iso) 1581da177e4SLinus Torvalds */ 1591da177e4SLinus Torvalds while (here && ed != here) { 1601da177e4SLinus Torvalds if (ed->interval > here->interval) 1611da177e4SLinus Torvalds break; 1621da177e4SLinus Torvalds prev = &here->ed_next; 1631da177e4SLinus Torvalds prev_p = &here->hwNextED; 1641da177e4SLinus Torvalds here = *prev; 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds if (ed != here) { 1671da177e4SLinus Torvalds ed->ed_next = here; 1681da177e4SLinus Torvalds if (here) 1691da177e4SLinus Torvalds ed->hwNextED = *prev_p; 1701da177e4SLinus Torvalds wmb (); 1711da177e4SLinus Torvalds *prev = ed; 1721da177e4SLinus Torvalds *prev_p = cpu_to_hc32(ohci, ed->dma); 1731da177e4SLinus Torvalds wmb(); 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds ohci->load [i] += ed->load; 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds ohci_to_hcd(ohci)->self.bandwidth_allocated += ed->load / ed->interval; 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds /* link an ed into one of the HC chains */ 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds static int ed_schedule (struct ohci_hcd *ohci, struct ed *ed) 1831da177e4SLinus Torvalds { 1841da177e4SLinus Torvalds int branch; 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds ed->state = ED_OPER; 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 */ 2621da177e4SLinus Torvalds return 0; 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds /* scan the periodic table to find and unlink this ED */ 2681da177e4SLinus Torvalds static void periodic_unlink (struct ohci_hcd *ohci, struct ed *ed) 2691da177e4SLinus Torvalds { 2701da177e4SLinus Torvalds int i; 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds for (i = ed->branch; i < NUM_INTS; i += ed->interval) { 2731da177e4SLinus Torvalds struct ed *temp; 2741da177e4SLinus Torvalds struct ed **prev = &ohci->periodic [i]; 2751da177e4SLinus Torvalds __hc32 *prev_p = &ohci->hcca->int_table [i]; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds while (*prev && (temp = *prev) != ed) { 2781da177e4SLinus Torvalds prev_p = &temp->hwNextED; 2791da177e4SLinus Torvalds prev = &temp->ed_next; 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds if (*prev) { 2821da177e4SLinus Torvalds *prev_p = ed->hwNextED; 2831da177e4SLinus Torvalds *prev = ed->ed_next; 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds ohci->load [i] -= ed->load; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds ohci_to_hcd(ohci)->self.bandwidth_allocated -= ed->load / ed->interval; 2881da177e4SLinus Torvalds 289d2c4254fSOliver Neukum ohci_dbg(ohci, "unlink %sed %p branch %d [%dus.], interval %d\n", 2901da177e4SLinus Torvalds (ed->hwINFO & cpu_to_hc32 (ohci, ED_ISO)) ? "iso " : "", 2911da177e4SLinus Torvalds ed, ed->branch, ed->load, ed->interval); 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds /* unlink an ed from one of the HC chains. 2951da177e4SLinus Torvalds * just the link to the ed is unlinked. 2961da177e4SLinus Torvalds * the link from the ed still points to another operational ed or 0 2971da177e4SLinus Torvalds * so the HC can eventually finish the processing of the unlinked ed 2981da177e4SLinus Torvalds * (assuming it already started that, which needn't be true). 2991da177e4SLinus Torvalds * 3001da177e4SLinus Torvalds * ED_UNLINK is a transient state: the HC may still see this ED, but soon 3011da177e4SLinus Torvalds * it won't. ED_SKIP means the HC will finish its current transaction, 3021da177e4SLinus Torvalds * but won't start anything new. The TD queue may still grow; device 3031da177e4SLinus Torvalds * drivers don't know about this HCD-internal state. 3041da177e4SLinus Torvalds * 3051da177e4SLinus Torvalds * When the HC can't see the ED, something changes ED_UNLINK to one of: 3061da177e4SLinus Torvalds * 3071da177e4SLinus Torvalds * - ED_OPER: when there's any request queued, the ED gets rescheduled 3081da177e4SLinus Torvalds * immediately. HC should be working on them. 3091da177e4SLinus Torvalds * 310977dcfdcSAlan Stern * - ED_IDLE: when there's no TD queue or the HC isn't running. 3111da177e4SLinus Torvalds * 3121da177e4SLinus Torvalds * When finish_unlinks() runs later, after SOF interrupt, it will often 3131da177e4SLinus Torvalds * complete one or more URB unlinks before making that state change. 3141da177e4SLinus Torvalds */ 3151da177e4SLinus Torvalds static void ed_deschedule (struct ohci_hcd *ohci, struct ed *ed) 3161da177e4SLinus Torvalds { 3171da177e4SLinus Torvalds ed->hwINFO |= cpu_to_hc32 (ohci, ED_SKIP); 3181da177e4SLinus Torvalds wmb (); 3191da177e4SLinus Torvalds ed->state = ED_UNLINK; 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds /* To deschedule something from the control or bulk list, just 3221da177e4SLinus Torvalds * clear CLE/BLE and wait. There's no safe way to scrub out list 3231da177e4SLinus Torvalds * head/current registers until later, and "later" isn't very 3241da177e4SLinus Torvalds * tightly specified. Figure 6-5 and Section 6.4.2.2 show how 3251da177e4SLinus Torvalds * the HC is reading the ED queues (while we modify them). 3261da177e4SLinus Torvalds * 3271da177e4SLinus Torvalds * For now, ed_schedule() is "later". It might be good paranoia 3281da177e4SLinus Torvalds * to scrub those registers in finish_unlinks(), in case of bugs 3291da177e4SLinus Torvalds * that make the HC try to use them. 3301da177e4SLinus Torvalds */ 3311da177e4SLinus Torvalds switch (ed->type) { 3321da177e4SLinus Torvalds case PIPE_CONTROL: 3331da177e4SLinus Torvalds /* remove ED from the HC's list: */ 3341da177e4SLinus Torvalds if (ed->ed_prev == NULL) { 3351da177e4SLinus Torvalds if (!ed->hwNextED) { 3361da177e4SLinus Torvalds ohci->hc_control &= ~OHCI_CTRL_CLE; 3371da177e4SLinus Torvalds ohci_writel (ohci, ohci->hc_control, 3381da177e4SLinus Torvalds &ohci->regs->control); 3391da177e4SLinus Torvalds // a ohci_readl() later syncs CLE with the HC 3401da177e4SLinus Torvalds } else 3411da177e4SLinus Torvalds ohci_writel (ohci, 3421da177e4SLinus Torvalds hc32_to_cpup (ohci, &ed->hwNextED), 3431da177e4SLinus Torvalds &ohci->regs->ed_controlhead); 3441da177e4SLinus Torvalds } else { 3451da177e4SLinus Torvalds ed->ed_prev->ed_next = ed->ed_next; 3461da177e4SLinus Torvalds ed->ed_prev->hwNextED = ed->hwNextED; 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds /* remove ED from the HCD's list: */ 3491da177e4SLinus Torvalds if (ohci->ed_controltail == ed) { 3501da177e4SLinus Torvalds ohci->ed_controltail = ed->ed_prev; 3511da177e4SLinus Torvalds if (ohci->ed_controltail) 3521da177e4SLinus Torvalds ohci->ed_controltail->ed_next = NULL; 3531da177e4SLinus Torvalds } else if (ed->ed_next) { 3541da177e4SLinus Torvalds ed->ed_next->ed_prev = ed->ed_prev; 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds break; 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds case PIPE_BULK: 3591da177e4SLinus Torvalds /* remove ED from the HC's list: */ 3601da177e4SLinus Torvalds if (ed->ed_prev == NULL) { 3611da177e4SLinus Torvalds if (!ed->hwNextED) { 3621da177e4SLinus Torvalds ohci->hc_control &= ~OHCI_CTRL_BLE; 3631da177e4SLinus Torvalds ohci_writel (ohci, ohci->hc_control, 3641da177e4SLinus Torvalds &ohci->regs->control); 3651da177e4SLinus Torvalds // a ohci_readl() later syncs BLE with the HC 3661da177e4SLinus Torvalds } else 3671da177e4SLinus Torvalds ohci_writel (ohci, 3681da177e4SLinus Torvalds hc32_to_cpup (ohci, &ed->hwNextED), 3691da177e4SLinus Torvalds &ohci->regs->ed_bulkhead); 3701da177e4SLinus Torvalds } else { 3711da177e4SLinus Torvalds ed->ed_prev->ed_next = ed->ed_next; 3721da177e4SLinus Torvalds ed->ed_prev->hwNextED = ed->hwNextED; 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds /* remove ED from the HCD's list: */ 3751da177e4SLinus Torvalds if (ohci->ed_bulktail == ed) { 3761da177e4SLinus Torvalds ohci->ed_bulktail = ed->ed_prev; 3771da177e4SLinus Torvalds if (ohci->ed_bulktail) 3781da177e4SLinus Torvalds ohci->ed_bulktail->ed_next = NULL; 3791da177e4SLinus Torvalds } else if (ed->ed_next) { 3801da177e4SLinus Torvalds ed->ed_next->ed_prev = ed->ed_prev; 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds break; 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds // case PIPE_INTERRUPT: 3851da177e4SLinus Torvalds // case PIPE_ISOCHRONOUS: 3861da177e4SLinus Torvalds default: 3871da177e4SLinus Torvalds periodic_unlink (ohci, ed); 3881da177e4SLinus Torvalds break; 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds /* get and maybe (re)init an endpoint. init _should_ be done only as part 3961da177e4SLinus Torvalds * of enumeration, usb_set_configuration() or usb_set_interface(). 3971da177e4SLinus Torvalds */ 3981da177e4SLinus Torvalds static struct ed *ed_get ( 3991da177e4SLinus Torvalds struct ohci_hcd *ohci, 4001da177e4SLinus Torvalds struct usb_host_endpoint *ep, 4011da177e4SLinus Torvalds struct usb_device *udev, 4021da177e4SLinus Torvalds unsigned int pipe, 4031da177e4SLinus Torvalds int interval 4041da177e4SLinus Torvalds ) { 4051da177e4SLinus Torvalds struct ed *ed; 4061da177e4SLinus Torvalds unsigned long flags; 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds spin_lock_irqsave (&ohci->lock, flags); 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds if (!(ed = ep->hcpriv)) { 4111da177e4SLinus Torvalds struct td *td; 4121da177e4SLinus Torvalds int is_out; 4131da177e4SLinus Torvalds u32 info; 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds ed = ed_alloc (ohci, GFP_ATOMIC); 4161da177e4SLinus Torvalds if (!ed) { 4171da177e4SLinus Torvalds /* out of memory */ 4181da177e4SLinus Torvalds goto done; 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds /* dummy td; end of td list for ed */ 4221da177e4SLinus Torvalds td = td_alloc (ohci, GFP_ATOMIC); 4231da177e4SLinus Torvalds if (!td) { 4241da177e4SLinus Torvalds /* out of memory */ 4251da177e4SLinus Torvalds ed_free (ohci, ed); 4261da177e4SLinus Torvalds ed = NULL; 4271da177e4SLinus Torvalds goto done; 4281da177e4SLinus Torvalds } 4291da177e4SLinus Torvalds ed->dummy = td; 4301da177e4SLinus Torvalds ed->hwTailP = cpu_to_hc32 (ohci, td->td_dma); 4311da177e4SLinus Torvalds ed->hwHeadP = ed->hwTailP; /* ED_C, ED_H zeroed */ 4321da177e4SLinus Torvalds ed->state = ED_IDLE; 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds is_out = !(ep->desc.bEndpointAddress & USB_DIR_IN); 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds /* FIXME usbcore changes dev->devnum before SET_ADDRESS 4374b26d50bSAnand Gadiyar * succeeds ... otherwise we wouldn't need "pipe". 4381da177e4SLinus Torvalds */ 4391da177e4SLinus Torvalds info = usb_pipedevice (pipe); 4401da177e4SLinus Torvalds ed->type = usb_pipetype(pipe); 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds info |= (ep->desc.bEndpointAddress & ~USB_DIR_IN) << 7; 44329cc8897SKuninori Morimoto info |= usb_endpoint_maxp(&ep->desc) << 16; 4441da177e4SLinus Torvalds if (udev->speed == USB_SPEED_LOW) 4451da177e4SLinus Torvalds info |= ED_LOWSPEED; 4461da177e4SLinus Torvalds /* only control transfers store pids in tds */ 4471da177e4SLinus Torvalds if (ed->type != PIPE_CONTROL) { 4481da177e4SLinus Torvalds info |= is_out ? ED_OUT : ED_IN; 4491da177e4SLinus Torvalds if (ed->type != PIPE_BULK) { 4501da177e4SLinus Torvalds /* periodic transfers... */ 4511da177e4SLinus Torvalds if (ed->type == PIPE_ISOCHRONOUS) 4521da177e4SLinus Torvalds info |= ED_ISO; 4531da177e4SLinus Torvalds else if (interval > 32) /* iso can be bigger */ 4541da177e4SLinus Torvalds interval = 32; 4551da177e4SLinus Torvalds ed->interval = interval; 4561da177e4SLinus Torvalds ed->load = usb_calc_bus_time ( 4571da177e4SLinus Torvalds udev->speed, !is_out, 4581da177e4SLinus Torvalds ed->type == PIPE_ISOCHRONOUS, 45929cc8897SKuninori Morimoto usb_endpoint_maxp(&ep->desc)) 4601da177e4SLinus Torvalds / 1000; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds ed->hwINFO = cpu_to_hc32(ohci, info); 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds ep->hcpriv = ed; 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds done: 4691da177e4SLinus Torvalds spin_unlock_irqrestore (&ohci->lock, flags); 4701da177e4SLinus Torvalds return ed; 4711da177e4SLinus Torvalds } 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds /* request unlinking of an endpoint from an operational HC. 4761da177e4SLinus Torvalds * put the ep on the rm_list 4771da177e4SLinus Torvalds * real work is done at the next start frame (SF) hardware interrupt 4781da177e4SLinus Torvalds * caller guarantees HCD is running, so hardware access is safe, 4791da177e4SLinus Torvalds * and that ed->state is ED_OPER 4801da177e4SLinus Torvalds */ 4811da177e4SLinus Torvalds static void start_ed_unlink (struct ohci_hcd *ohci, struct ed *ed) 4821da177e4SLinus Torvalds { 4831da177e4SLinus Torvalds ed->hwINFO |= cpu_to_hc32 (ohci, ED_DEQUEUE); 4841da177e4SLinus Torvalds ed_deschedule (ohci, ed); 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds /* rm_list is just singly linked, for simplicity */ 4871da177e4SLinus Torvalds ed->ed_next = ohci->ed_rm_list; 4881da177e4SLinus Torvalds ed->ed_prev = NULL; 4891da177e4SLinus Torvalds ohci->ed_rm_list = ed; 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds /* enable SOF interrupt */ 4921da177e4SLinus Torvalds ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus); 4931da177e4SLinus Torvalds ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable); 4941da177e4SLinus Torvalds // flush those writes, and get latest HCCA contents 4951da177e4SLinus Torvalds (void) ohci_readl (ohci, &ohci->regs->control); 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds /* SF interrupt might get delayed; record the frame counter value that 4981da177e4SLinus Torvalds * indicates when the HC isn't looking at it, so concurrent unlinks 4991da177e4SLinus Torvalds * behave. frame_no wraps every 2^16 msec, and changes right before 5001da177e4SLinus Torvalds * SF is triggered. 5011da177e4SLinus Torvalds */ 5021da177e4SLinus Torvalds ed->tick = ohci_frame_no(ohci) + 1; 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds /*-------------------------------------------------------------------------* 5071da177e4SLinus Torvalds * TD handling functions 5081da177e4SLinus Torvalds *-------------------------------------------------------------------------*/ 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */ 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds static void 5131da177e4SLinus Torvalds td_fill (struct ohci_hcd *ohci, u32 info, 5141da177e4SLinus Torvalds dma_addr_t data, int len, 5151da177e4SLinus Torvalds struct urb *urb, int index) 5161da177e4SLinus Torvalds { 5171da177e4SLinus Torvalds struct td *td, *td_pt; 5181da177e4SLinus Torvalds struct urb_priv *urb_priv = urb->hcpriv; 5191da177e4SLinus Torvalds int is_iso = info & TD_ISO; 5201da177e4SLinus Torvalds int hash; 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds // ASSERT (index < urb_priv->length); 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds /* aim for only one interrupt per urb. mostly applies to control 5251da177e4SLinus Torvalds * and iso; other urbs rarely need more than one TD per urb. 5261da177e4SLinus Torvalds * this way, only final tds (or ones with an error) cause IRQs. 5271da177e4SLinus Torvalds * at least immediately; use DI=6 in case any control request is 5281da177e4SLinus Torvalds * tempted to die part way through. (and to force the hc to flush 5291da177e4SLinus Torvalds * its donelist soonish, even on unlink paths.) 5301da177e4SLinus Torvalds * 5311da177e4SLinus Torvalds * NOTE: could delay interrupts even for the last TD, and get fewer 5321da177e4SLinus Torvalds * interrupts ... increasing per-urb latency by sharing interrupts. 5331da177e4SLinus Torvalds * Drivers that queue bulk urbs may request that behavior. 5341da177e4SLinus Torvalds */ 5351da177e4SLinus Torvalds if (index != (urb_priv->length - 1) 5361da177e4SLinus Torvalds || (urb->transfer_flags & URB_NO_INTERRUPT)) 5371da177e4SLinus Torvalds info |= TD_DI_SET (6); 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds /* use this td as the next dummy */ 5401da177e4SLinus Torvalds td_pt = urb_priv->td [index]; 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds /* fill the old dummy TD */ 5431da177e4SLinus Torvalds td = urb_priv->td [index] = urb_priv->ed->dummy; 5441da177e4SLinus Torvalds urb_priv->ed->dummy = td_pt; 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds td->ed = urb_priv->ed; 5471da177e4SLinus Torvalds td->next_dl_td = NULL; 5481da177e4SLinus Torvalds td->index = index; 5491da177e4SLinus Torvalds td->urb = urb; 5501da177e4SLinus Torvalds td->data_dma = data; 5511da177e4SLinus Torvalds if (!len) 5521da177e4SLinus Torvalds data = 0; 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds td->hwINFO = cpu_to_hc32 (ohci, info); 5551da177e4SLinus Torvalds if (is_iso) { 5561da177e4SLinus Torvalds td->hwCBP = cpu_to_hc32 (ohci, data & 0xFFFFF000); 5571da177e4SLinus Torvalds *ohci_hwPSWp(ohci, td, 0) = cpu_to_hc16 (ohci, 5581da177e4SLinus Torvalds (data & 0x0FFF) | 0xE000); 5591da177e4SLinus Torvalds } else { 5601da177e4SLinus Torvalds td->hwCBP = cpu_to_hc32 (ohci, data); 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds if (data) 5631da177e4SLinus Torvalds td->hwBE = cpu_to_hc32 (ohci, data + len - 1); 5641da177e4SLinus Torvalds else 5651da177e4SLinus Torvalds td->hwBE = 0; 5661da177e4SLinus Torvalds td->hwNextTD = cpu_to_hc32 (ohci, td_pt->td_dma); 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds /* append to queue */ 5691da177e4SLinus Torvalds list_add_tail (&td->td_list, &td->ed->td_list); 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds /* hash it for later reverse mapping */ 5721da177e4SLinus Torvalds hash = TD_HASH_FUNC (td->td_dma); 5731da177e4SLinus Torvalds td->td_hash = ohci->td_hash [hash]; 5741da177e4SLinus Torvalds ohci->td_hash [hash] = td; 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds /* HC might read the TD (or cachelines) right away ... */ 5771da177e4SLinus Torvalds wmb (); 5781da177e4SLinus Torvalds td->ed->hwTailP = td->hwNextTD; 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds /* Prepare all TDs of a transfer, and queue them onto the ED. 5841da177e4SLinus Torvalds * Caller guarantees HC is active. 5851da177e4SLinus Torvalds * Usually the ED is already on the schedule, so TDs might be 5861da177e4SLinus Torvalds * processed as soon as they're queued. 5871da177e4SLinus Torvalds */ 5881da177e4SLinus Torvalds static void td_submit_urb ( 5891da177e4SLinus Torvalds struct ohci_hcd *ohci, 5901da177e4SLinus Torvalds struct urb *urb 5911da177e4SLinus Torvalds ) { 5921da177e4SLinus Torvalds struct urb_priv *urb_priv = urb->hcpriv; 5932621d011SManjunath Goudar struct device *dev = ohci_to_hcd(ohci)->self.controller; 5941da177e4SLinus Torvalds dma_addr_t data; 5951da177e4SLinus Torvalds int data_len = urb->transfer_buffer_length; 5961da177e4SLinus Torvalds int cnt = 0; 5971da177e4SLinus Torvalds u32 info = 0; 5981da177e4SLinus Torvalds int is_out = usb_pipeout (urb->pipe); 5991da177e4SLinus Torvalds int periodic = 0; 6006f65126cSAlan Stern int i, this_sg_len, n; 6016f65126cSAlan Stern struct scatterlist *sg; 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds /* OHCI handles the bulk/interrupt data toggles itself. We just 6041da177e4SLinus Torvalds * use the device toggle bits for resetting, and rely on the fact 6051da177e4SLinus Torvalds * that resetting toggle is meaningless if the endpoint is active. 6061da177e4SLinus Torvalds */ 6071da177e4SLinus Torvalds if (!usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe), is_out)) { 6081da177e4SLinus Torvalds usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe), 6091da177e4SLinus Torvalds is_out, 1); 6101da177e4SLinus Torvalds urb_priv->ed->hwHeadP &= ~cpu_to_hc32 (ohci, ED_C); 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds list_add (&urb_priv->pending, &ohci->pending); 6141da177e4SLinus Torvalds 6156f65126cSAlan Stern i = urb->num_mapped_sgs; 6166f65126cSAlan Stern if (data_len > 0 && i > 0) { 6176f65126cSAlan Stern sg = urb->sg; 6186f65126cSAlan Stern data = sg_dma_address(sg); 6196f65126cSAlan Stern 6206f65126cSAlan Stern /* 6216f65126cSAlan Stern * urb->transfer_buffer_length may be smaller than the 6226f65126cSAlan Stern * size of the scatterlist (or vice versa) 6236f65126cSAlan Stern */ 6246f65126cSAlan Stern this_sg_len = min_t(int, sg_dma_len(sg), data_len); 6256f65126cSAlan Stern } else { 6266f65126cSAlan Stern sg = NULL; 6271da177e4SLinus Torvalds if (data_len) 6281da177e4SLinus Torvalds data = urb->transfer_dma; 6291da177e4SLinus Torvalds else 6301da177e4SLinus Torvalds data = 0; 6316f65126cSAlan Stern this_sg_len = data_len; 6326f65126cSAlan Stern } 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds /* NOTE: TD_CC is set so we can tell which TDs the HC processed by 6351da177e4SLinus Torvalds * using TD_CC_GET, as well as by seeing them on the done list. 6361da177e4SLinus Torvalds * (CC = NotAccessed ... 0x0F, or 0x0E in PSWs for ISO.) 6371da177e4SLinus Torvalds */ 6381da177e4SLinus Torvalds switch (urb_priv->ed->type) { 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds /* Bulk and interrupt are identical except for where in the schedule 6411da177e4SLinus Torvalds * their EDs live. 6421da177e4SLinus Torvalds */ 6431da177e4SLinus Torvalds case PIPE_INTERRUPT: 6441da177e4SLinus Torvalds /* ... and periodic urbs have extra accounting */ 6451da177e4SLinus Torvalds periodic = ohci_to_hcd(ohci)->self.bandwidth_int_reqs++ == 0 6461da177e4SLinus Torvalds && ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0; 6471da177e4SLinus Torvalds /* FALLTHROUGH */ 6481da177e4SLinus Torvalds case PIPE_BULK: 6491da177e4SLinus Torvalds info = is_out 6501da177e4SLinus Torvalds ? TD_T_TOGGLE | TD_CC | TD_DP_OUT 6511da177e4SLinus Torvalds : TD_T_TOGGLE | TD_CC | TD_DP_IN; 6521da177e4SLinus Torvalds /* TDs _could_ transfer up to 8K each */ 6536f65126cSAlan Stern for (;;) { 6546f65126cSAlan Stern n = min(this_sg_len, 4096); 6556f65126cSAlan Stern 6561da177e4SLinus Torvalds /* maybe avoid ED halt on final TD short read */ 6576f65126cSAlan Stern if (n >= data_len || (i == 1 && n >= this_sg_len)) { 6581da177e4SLinus Torvalds if (!(urb->transfer_flags & URB_SHORT_NOT_OK)) 6591da177e4SLinus Torvalds info |= TD_R; 6606f65126cSAlan Stern } 6616f65126cSAlan Stern td_fill(ohci, info, data, n, urb, cnt); 6626f65126cSAlan Stern this_sg_len -= n; 6636f65126cSAlan Stern data_len -= n; 6646f65126cSAlan Stern data += n; 6651da177e4SLinus Torvalds cnt++; 6666f65126cSAlan Stern 6676f65126cSAlan Stern if (this_sg_len <= 0) { 6686f65126cSAlan Stern if (--i <= 0 || data_len <= 0) 6696f65126cSAlan Stern break; 6706f65126cSAlan Stern sg = sg_next(sg); 6716f65126cSAlan Stern data = sg_dma_address(sg); 6726f65126cSAlan Stern this_sg_len = min_t(int, sg_dma_len(sg), 6736f65126cSAlan Stern data_len); 6746f65126cSAlan Stern } 6756f65126cSAlan Stern } 6761da177e4SLinus Torvalds if ((urb->transfer_flags & URB_ZERO_PACKET) 6771da177e4SLinus Torvalds && cnt < urb_priv->length) { 6781da177e4SLinus Torvalds td_fill (ohci, info, 0, 0, urb, cnt); 6791da177e4SLinus Torvalds cnt++; 6801da177e4SLinus Torvalds } 6811da177e4SLinus Torvalds /* maybe kickstart bulk list */ 6821da177e4SLinus Torvalds if (urb_priv->ed->type == PIPE_BULK) { 6831da177e4SLinus Torvalds wmb (); 6841da177e4SLinus Torvalds ohci_writel (ohci, OHCI_BLF, &ohci->regs->cmdstatus); 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds break; 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds /* control manages DATA0/DATA1 toggle per-request; SETUP resets it, 6891da177e4SLinus Torvalds * any DATA phase works normally, and the STATUS ack is special. 6901da177e4SLinus Torvalds */ 6911da177e4SLinus Torvalds case PIPE_CONTROL: 6921da177e4SLinus Torvalds info = TD_CC | TD_DP_SETUP | TD_T_DATA0; 6931da177e4SLinus Torvalds td_fill (ohci, info, urb->setup_dma, 8, urb, cnt++); 6941da177e4SLinus Torvalds if (data_len > 0) { 6951da177e4SLinus Torvalds info = TD_CC | TD_R | TD_T_DATA1; 6961da177e4SLinus Torvalds info |= is_out ? TD_DP_OUT : TD_DP_IN; 6971da177e4SLinus Torvalds /* NOTE: mishandles transfers >8K, some >4K */ 6981da177e4SLinus Torvalds td_fill (ohci, info, data, data_len, urb, cnt++); 6991da177e4SLinus Torvalds } 7001da177e4SLinus Torvalds info = (is_out || data_len == 0) 7011da177e4SLinus Torvalds ? TD_CC | TD_DP_IN | TD_T_DATA1 7021da177e4SLinus Torvalds : TD_CC | TD_DP_OUT | TD_T_DATA1; 7031da177e4SLinus Torvalds td_fill (ohci, info, data, 0, urb, cnt++); 7041da177e4SLinus Torvalds /* maybe kickstart control list */ 7051da177e4SLinus Torvalds wmb (); 7061da177e4SLinus Torvalds ohci_writel (ohci, OHCI_CLF, &ohci->regs->cmdstatus); 7071da177e4SLinus Torvalds break; 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds /* ISO has no retransmit, so no toggle; and it uses special TDs. 7101da177e4SLinus Torvalds * Each TD could handle multiple consecutive frames (interval 1); 7111da177e4SLinus Torvalds * we could often reduce the number of TDs here. 7121da177e4SLinus Torvalds */ 7131da177e4SLinus Torvalds case PIPE_ISOCHRONOUS: 7146a41b4d3SAlan Stern for (cnt = urb_priv->td_cnt; cnt < urb->number_of_packets; 7156a41b4d3SAlan Stern cnt++) { 7161da177e4SLinus Torvalds int frame = urb->start_frame; 7171da177e4SLinus Torvalds 7181da177e4SLinus Torvalds // FIXME scheduling should handle frame counter 7191da177e4SLinus Torvalds // roll-around ... exotic case (and OHCI has 7201da177e4SLinus Torvalds // a 2^16 iso range, vs other HCs max of 2^10) 7211da177e4SLinus Torvalds frame += cnt * urb->interval; 7221da177e4SLinus Torvalds frame &= 0xffff; 7231da177e4SLinus Torvalds td_fill (ohci, TD_CC | TD_ISO | frame, 7241da177e4SLinus Torvalds data + urb->iso_frame_desc [cnt].offset, 7251da177e4SLinus Torvalds urb->iso_frame_desc [cnt].length, urb, cnt); 7261da177e4SLinus Torvalds } 727a1f17a87SLibin Yang if (ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs == 0) { 728a1f17a87SLibin Yang if (quirk_amdiso(ohci)) 729ad93562bSAndiry Xu usb_amd_quirk_pll_disable(); 730a1f17a87SLibin Yang if (quirk_amdprefetch(ohci)) 7312621d011SManjunath Goudar sb800_prefetch(dev, 1); 732a1f17a87SLibin Yang } 7331da177e4SLinus Torvalds periodic = ohci_to_hcd(ohci)->self.bandwidth_isoc_reqs++ == 0 7341da177e4SLinus Torvalds && ohci_to_hcd(ohci)->self.bandwidth_int_reqs == 0; 7351da177e4SLinus Torvalds break; 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds /* start periodic dma if needed */ 7391da177e4SLinus Torvalds if (periodic) { 7401da177e4SLinus Torvalds wmb (); 7411da177e4SLinus Torvalds ohci->hc_control |= OHCI_CTRL_PLE|OHCI_CTRL_IE; 7421da177e4SLinus Torvalds ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds // ASSERT (urb_priv->length == cnt); 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds /*-------------------------------------------------------------------------* 7491da177e4SLinus Torvalds * Done List handling functions 7501da177e4SLinus Torvalds *-------------------------------------------------------------------------*/ 7511da177e4SLinus Torvalds 75255d84968SAlan Stern /* calculate transfer length/status and update the urb */ 75355d84968SAlan Stern static int td_done(struct ohci_hcd *ohci, struct urb *urb, struct td *td) 7541da177e4SLinus Torvalds { 7551da177e4SLinus Torvalds u32 tdINFO = hc32_to_cpup (ohci, &td->hwINFO); 7561da177e4SLinus Torvalds int cc = 0; 75755d84968SAlan Stern int status = -EINPROGRESS; 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds list_del (&td->td_list); 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds /* ISO ... drivers see per-TD length/status */ 7621da177e4SLinus Torvalds if (tdINFO & TD_ISO) { 7631da177e4SLinus Torvalds u16 tdPSW = ohci_hwPSW(ohci, td, 0); 7641da177e4SLinus Torvalds int dlen = 0; 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds /* NOTE: assumes FC in tdINFO == 0, and that 7671da177e4SLinus Torvalds * only the first of 0..MAXPSW psws is used. 7681da177e4SLinus Torvalds */ 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds cc = (tdPSW >> 12) & 0xF; 7711da177e4SLinus Torvalds if (tdINFO & TD_CC) /* hc didn't touch? */ 77255d84968SAlan Stern return status; 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds if (usb_pipeout (urb->pipe)) 7751da177e4SLinus Torvalds dlen = urb->iso_frame_desc [td->index].length; 7761da177e4SLinus Torvalds else { 7771da177e4SLinus Torvalds /* short reads are always OK for ISO */ 7781da177e4SLinus Torvalds if (cc == TD_DATAUNDERRUN) 7791da177e4SLinus Torvalds cc = TD_CC_NOERROR; 7801da177e4SLinus Torvalds dlen = tdPSW & 0x3ff; 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds urb->actual_length += dlen; 7831da177e4SLinus Torvalds urb->iso_frame_desc [td->index].actual_length = dlen; 7841da177e4SLinus Torvalds urb->iso_frame_desc [td->index].status = cc_to_error [cc]; 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds if (cc != TD_CC_NOERROR) 787d2c4254fSOliver Neukum ohci_dbg(ohci, 7881da177e4SLinus Torvalds "urb %p iso td %p (%d) len %d cc %d\n", 7891da177e4SLinus Torvalds urb, td, 1 + td->index, dlen, cc); 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds /* BULK, INT, CONTROL ... drivers see aggregate length/status, 7921da177e4SLinus Torvalds * except that "setup" bytes aren't counted and "short" transfers 7931da177e4SLinus Torvalds * might not be reported as errors. 7941da177e4SLinus Torvalds */ 7951da177e4SLinus Torvalds } else { 7961da177e4SLinus Torvalds int type = usb_pipetype (urb->pipe); 7971da177e4SLinus Torvalds u32 tdBE = hc32_to_cpup (ohci, &td->hwBE); 7981da177e4SLinus Torvalds 7991da177e4SLinus Torvalds cc = TD_CC_GET (tdINFO); 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds /* update packet status if needed (short is normally ok) */ 8021da177e4SLinus Torvalds if (cc == TD_DATAUNDERRUN 8031da177e4SLinus Torvalds && !(urb->transfer_flags & URB_SHORT_NOT_OK)) 8041da177e4SLinus Torvalds cc = TD_CC_NOERROR; 80555d84968SAlan Stern if (cc != TD_CC_NOERROR && cc < 0x0E) 80655d84968SAlan Stern status = cc_to_error[cc]; 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds /* count all non-empty packets except control SETUP packet */ 8091da177e4SLinus Torvalds if ((type != PIPE_CONTROL || td->index != 0) && tdBE != 0) { 8101da177e4SLinus Torvalds if (td->hwCBP == 0) 8111da177e4SLinus Torvalds urb->actual_length += tdBE - td->data_dma + 1; 8121da177e4SLinus Torvalds else 8131da177e4SLinus Torvalds urb->actual_length += 8141da177e4SLinus Torvalds hc32_to_cpup (ohci, &td->hwCBP) 8151da177e4SLinus Torvalds - td->data_dma; 8161da177e4SLinus Torvalds } 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds if (cc != TD_CC_NOERROR && cc < 0x0E) 819d2c4254fSOliver Neukum ohci_dbg(ohci, 8201da177e4SLinus Torvalds "urb %p td %p (%d) cc %d, len=%d/%d\n", 8211da177e4SLinus Torvalds urb, td, 1 + td->index, cc, 8221da177e4SLinus Torvalds urb->actual_length, 8231da177e4SLinus Torvalds urb->transfer_buffer_length); 8241da177e4SLinus Torvalds } 82555d84968SAlan Stern return status; 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 8291da177e4SLinus Torvalds 8306e8fe43bSAlan Stern static void ed_halted(struct ohci_hcd *ohci, struct td *td, int cc) 8311da177e4SLinus Torvalds { 8321da177e4SLinus Torvalds struct urb *urb = td->urb; 8336e8fe43bSAlan Stern urb_priv_t *urb_priv = urb->hcpriv; 8341da177e4SLinus Torvalds struct ed *ed = td->ed; 8351da177e4SLinus Torvalds struct list_head *tmp = td->td_list.next; 8361da177e4SLinus Torvalds __hc32 toggle = ed->hwHeadP & cpu_to_hc32 (ohci, ED_C); 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds /* clear ed halt; this is the td that caused it, but keep it inactive 8391da177e4SLinus Torvalds * until its urb->complete() has a chance to clean up. 8401da177e4SLinus Torvalds */ 8411da177e4SLinus Torvalds ed->hwINFO |= cpu_to_hc32 (ohci, ED_SKIP); 8421da177e4SLinus Torvalds wmb (); 8431da177e4SLinus Torvalds ed->hwHeadP &= ~cpu_to_hc32 (ohci, ED_H); 8441da177e4SLinus Torvalds 8456e8fe43bSAlan Stern /* Get rid of all later tds from this urb. We don't have 8466e8fe43bSAlan Stern * to be careful: no errors and nothing was transferred. 8476e8fe43bSAlan Stern * Also patch the ed so it looks as if those tds completed normally. 8481da177e4SLinus Torvalds */ 8491da177e4SLinus Torvalds while (tmp != &ed->td_list) { 8501da177e4SLinus Torvalds struct td *next; 8511da177e4SLinus Torvalds 8521da177e4SLinus Torvalds next = list_entry (tmp, struct td, td_list); 8531da177e4SLinus Torvalds tmp = next->td_list.next; 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds if (next->urb != urb) 8561da177e4SLinus Torvalds break; 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds /* NOTE: if multi-td control DATA segments get supported, 8591da177e4SLinus Torvalds * this urb had one of them, this td wasn't the last td 8601da177e4SLinus Torvalds * in that segment (TD_R clear), this ed halted because 8611da177e4SLinus Torvalds * of a short read, _and_ URB_SHORT_NOT_OK is clear ... 8621da177e4SLinus Torvalds * then we need to leave the control STATUS packet queued 8631da177e4SLinus Torvalds * and clear ED_SKIP. 8641da177e4SLinus Torvalds */ 8651da177e4SLinus Torvalds 8666e8fe43bSAlan Stern list_del(&next->td_list); 8676e8fe43bSAlan Stern urb_priv->td_cnt++; 8681da177e4SLinus Torvalds ed->hwHeadP = next->hwNextTD | toggle; 8691da177e4SLinus Torvalds } 8701da177e4SLinus Torvalds 8711da177e4SLinus Torvalds /* help for troubleshooting: report anything that 8721da177e4SLinus Torvalds * looks odd ... that doesn't include protocol stalls 8731da177e4SLinus Torvalds * (or maybe some other things) 8741da177e4SLinus Torvalds */ 8751da177e4SLinus Torvalds switch (cc) { 8761da177e4SLinus Torvalds case TD_DATAUNDERRUN: 8771da177e4SLinus Torvalds if ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0) 8781da177e4SLinus Torvalds break; 8791da177e4SLinus Torvalds /* fallthrough */ 8801da177e4SLinus Torvalds case TD_CC_STALL: 8811da177e4SLinus Torvalds if (usb_pipecontrol (urb->pipe)) 8821da177e4SLinus Torvalds break; 8831da177e4SLinus Torvalds /* fallthrough */ 8841da177e4SLinus Torvalds default: 8851da177e4SLinus Torvalds ohci_dbg (ohci, 8861da177e4SLinus Torvalds "urb %p path %s ep%d%s %08x cc %d --> status %d\n", 8871da177e4SLinus Torvalds urb, urb->dev->devpath, 8881da177e4SLinus Torvalds usb_pipeendpoint (urb->pipe), 8891da177e4SLinus Torvalds usb_pipein (urb->pipe) ? "in" : "out", 8901da177e4SLinus Torvalds hc32_to_cpu (ohci, td->hwINFO), 8911da177e4SLinus Torvalds cc, cc_to_error [cc]); 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds } 8941da177e4SLinus Torvalds 895c6fcb85eSAlan Stern /* Add a TD to the done list */ 896c6fcb85eSAlan Stern static void add_to_done_list(struct ohci_hcd *ohci, struct td *td) 897c6fcb85eSAlan Stern { 898c6fcb85eSAlan Stern struct td *td2, *td_prev; 899c6fcb85eSAlan Stern struct ed *ed; 900c6fcb85eSAlan Stern 901c6fcb85eSAlan Stern if (td->next_dl_td) 902c6fcb85eSAlan Stern return; /* Already on the list */ 903c6fcb85eSAlan Stern 904c6fcb85eSAlan Stern /* Add all the TDs going back until we reach one that's on the list */ 905c6fcb85eSAlan Stern ed = td->ed; 906c6fcb85eSAlan Stern td2 = td_prev = td; 907c6fcb85eSAlan Stern list_for_each_entry_continue_reverse(td2, &ed->td_list, td_list) { 908c6fcb85eSAlan Stern if (td2->next_dl_td) 909c6fcb85eSAlan Stern break; 910c6fcb85eSAlan Stern td2->next_dl_td = td_prev; 911c6fcb85eSAlan Stern td_prev = td2; 912c6fcb85eSAlan Stern } 913c6fcb85eSAlan Stern 914c6fcb85eSAlan Stern if (ohci->dl_end) 915c6fcb85eSAlan Stern ohci->dl_end->next_dl_td = td_prev; 916c6fcb85eSAlan Stern else 917c6fcb85eSAlan Stern ohci->dl_start = td_prev; 918c6fcb85eSAlan Stern 919c6fcb85eSAlan Stern /* 920c6fcb85eSAlan Stern * Make td->next_dl_td point to td itself, to mark the fact 921c6fcb85eSAlan Stern * that td is on the done list. 9221da177e4SLinus Torvalds */ 923c6fcb85eSAlan Stern ohci->dl_end = td->next_dl_td = td; 924*81e38333SAlan Stern 925*81e38333SAlan Stern /* Did we just add the latest pending TD? */ 926*81e38333SAlan Stern td2 = ed->pending_td; 927*81e38333SAlan Stern if (td2 && td2->next_dl_td) 928*81e38333SAlan Stern ed->pending_td = NULL; 929c6fcb85eSAlan Stern } 930c6fcb85eSAlan Stern 931c6fcb85eSAlan Stern /* Get the entries on the hardware done queue and put them on our list */ 932c6fcb85eSAlan Stern static void update_done_list(struct ohci_hcd *ohci) 9331da177e4SLinus Torvalds { 9341da177e4SLinus Torvalds u32 td_dma; 9351da177e4SLinus Torvalds struct td *td = NULL; 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds td_dma = hc32_to_cpup (ohci, &ohci->hcca->done_head); 9381da177e4SLinus Torvalds ohci->hcca->done_head = 0; 9391da177e4SLinus Torvalds wmb(); 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds /* get TD from hc's singly linked list, and 942c6fcb85eSAlan Stern * add to ours. ed->td_list changes later. 9431da177e4SLinus Torvalds */ 9441da177e4SLinus Torvalds while (td_dma) { 9451da177e4SLinus Torvalds int cc; 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds td = dma_to_td (ohci, td_dma); 9481da177e4SLinus Torvalds if (!td) { 9491da177e4SLinus Torvalds ohci_err (ohci, "bad entry %8x\n", td_dma); 9501da177e4SLinus Torvalds break; 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds td->hwINFO |= cpu_to_hc32 (ohci, TD_DONE); 9541da177e4SLinus Torvalds cc = TD_CC_GET (hc32_to_cpup (ohci, &td->hwINFO)); 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds /* Non-iso endpoints can halt on error; un-halt, 9571da177e4SLinus Torvalds * and dequeue any other TDs from this urb. 9581da177e4SLinus Torvalds * No other TD could have caused the halt. 9591da177e4SLinus Torvalds */ 9601da177e4SLinus Torvalds if (cc != TD_CC_NOERROR 9611da177e4SLinus Torvalds && (td->ed->hwHeadP & cpu_to_hc32 (ohci, ED_H))) 9626e8fe43bSAlan Stern ed_halted(ohci, td, cc); 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds td_dma = hc32_to_cpup (ohci, &td->hwNextTD); 965c6fcb85eSAlan Stern add_to_done_list(ohci, td); 9661da177e4SLinus Torvalds } 9671da177e4SLinus Torvalds } 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds /* there are some urbs/eds to unlink; called in_irq(), with HCD locked */ 972cdb4dd15SAlan Stern static void finish_unlinks(struct ohci_hcd *ohci) 9731da177e4SLinus Torvalds { 974cdb4dd15SAlan Stern unsigned tick = ohci_frame_no(ohci); 9751da177e4SLinus Torvalds struct ed *ed, **last; 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds rescan_all: 9781da177e4SLinus Torvalds for (last = &ohci->ed_rm_list, ed = *last; ed != NULL; ed = *last) { 9791da177e4SLinus Torvalds struct list_head *entry, *tmp; 9801da177e4SLinus Torvalds int completed, modified; 9811da177e4SLinus Torvalds __hc32 *prev; 9821da177e4SLinus Torvalds 983977dcfdcSAlan Stern /* Is this ED already invisible to the hardware? */ 984977dcfdcSAlan Stern if (ed->state == ED_IDLE) 985977dcfdcSAlan Stern goto ed_idle; 986977dcfdcSAlan Stern 9871da177e4SLinus Torvalds /* only take off EDs that the HC isn't using, accounting for 9881da177e4SLinus Torvalds * frame counter wraps and EDs with partially retired TDs 9891da177e4SLinus Torvalds */ 990c6fcb85eSAlan Stern if (likely(ohci->rh_state == OHCI_RH_RUNNING) && 991c6fcb85eSAlan Stern tick_before(tick, ed->tick)) { 9921da177e4SLinus Torvalds skip_ed: 9931da177e4SLinus Torvalds last = &ed->ed_next; 9941da177e4SLinus Torvalds continue; 9951da177e4SLinus Torvalds } 9961da177e4SLinus Torvalds if (!list_empty(&ed->td_list)) { 9971da177e4SLinus Torvalds struct td *td; 9981da177e4SLinus Torvalds u32 head; 9991da177e4SLinus Torvalds 1000c6fcb85eSAlan Stern td = list_first_entry(&ed->td_list, struct td, td_list); 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds /* INTR_WDH may need to clean up first */ 1003c6fcb85eSAlan Stern head = hc32_to_cpu(ohci, ed->hwHeadP) & TD_MASK; 1004c6fcb85eSAlan Stern if (td->td_dma != head && 1005c6fcb85eSAlan Stern ohci->rh_state == OHCI_RH_RUNNING) 10061da177e4SLinus Torvalds goto skip_ed; 1007c6fcb85eSAlan Stern 1008c6fcb85eSAlan Stern /* Don't mess up anything already on the done list */ 1009c6fcb85eSAlan Stern if (td->next_dl_td) 1010c6fcb85eSAlan Stern goto skip_ed; 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds 1013977dcfdcSAlan Stern /* ED's now officially unlinked, hc doesn't see */ 1014977dcfdcSAlan Stern ed->state = ED_IDLE; 1015977dcfdcSAlan Stern ed->hwHeadP &= ~cpu_to_hc32(ohci, ED_H); 1016977dcfdcSAlan Stern ed->hwNextED = 0; 1017977dcfdcSAlan Stern wmb(); 1018977dcfdcSAlan Stern ed->hwINFO &= ~cpu_to_hc32(ohci, ED_SKIP | ED_DEQUEUE); 1019977dcfdcSAlan Stern ed_idle: 1020977dcfdcSAlan Stern 10211da177e4SLinus Torvalds /* reentrancy: if we drop the schedule lock, someone might 10221da177e4SLinus Torvalds * have modified this list. normally it's just prepending 10231da177e4SLinus Torvalds * entries (which we'd ignore), but paranoia won't hurt. 10241da177e4SLinus Torvalds */ 10251da177e4SLinus Torvalds modified = 0; 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds /* unlink urbs as requested, but rescan the list after 10281da177e4SLinus Torvalds * we call a completion since it might have unlinked 10291da177e4SLinus Torvalds * another (earlier) urb 10301da177e4SLinus Torvalds * 10311da177e4SLinus Torvalds * When we get here, the HC doesn't see this ed. But it 10321da177e4SLinus Torvalds * must not be rescheduled until all completed URBs have 10331da177e4SLinus Torvalds * been given back to the driver. 10341da177e4SLinus Torvalds */ 10351da177e4SLinus Torvalds rescan_this: 10361da177e4SLinus Torvalds completed = 0; 10371da177e4SLinus Torvalds prev = &ed->hwHeadP; 10381da177e4SLinus Torvalds list_for_each_safe (entry, tmp, &ed->td_list) { 10391da177e4SLinus Torvalds struct td *td; 10401da177e4SLinus Torvalds struct urb *urb; 10411da177e4SLinus Torvalds urb_priv_t *urb_priv; 10421da177e4SLinus Torvalds __hc32 savebits; 104329c8f6a7SDavid Brownell u32 tdINFO; 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds td = list_entry (entry, struct td, td_list); 10461da177e4SLinus Torvalds urb = td->urb; 10471da177e4SLinus Torvalds urb_priv = td->urb->hcpriv; 10481da177e4SLinus Torvalds 1049eb231054SAlan Stern if (!urb->unlinked) { 10501da177e4SLinus Torvalds prev = &td->hwNextTD; 10511da177e4SLinus Torvalds continue; 10521da177e4SLinus Torvalds } 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds /* patch pointer hc uses */ 10551da177e4SLinus Torvalds savebits = *prev & ~cpu_to_hc32 (ohci, TD_MASK); 10561da177e4SLinus Torvalds *prev = td->hwNextTD | savebits; 10571da177e4SLinus Torvalds 105829c8f6a7SDavid Brownell /* If this was unlinked, the TD may not have been 105929c8f6a7SDavid Brownell * retired ... so manually save the data toggle. 106029c8f6a7SDavid Brownell * The controller ignores the value we save for 106129c8f6a7SDavid Brownell * control and ISO endpoints. 106229c8f6a7SDavid Brownell */ 106329c8f6a7SDavid Brownell tdINFO = hc32_to_cpup(ohci, &td->hwINFO); 106429c8f6a7SDavid Brownell if ((tdINFO & TD_T) == TD_T_DATA0) 106529c8f6a7SDavid Brownell ed->hwHeadP &= ~cpu_to_hc32(ohci, ED_C); 106629c8f6a7SDavid Brownell else if ((tdINFO & TD_T) == TD_T_DATA1) 106729c8f6a7SDavid Brownell ed->hwHeadP |= cpu_to_hc32(ohci, ED_C); 106829c8f6a7SDavid Brownell 10691da177e4SLinus Torvalds /* HC may have partly processed this TD */ 10701da177e4SLinus Torvalds td_done (ohci, urb, td); 10711da177e4SLinus Torvalds urb_priv->td_cnt++; 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds /* if URB is done, clean up */ 1074a8693424SAlan Stern if (urb_priv->td_cnt >= urb_priv->length) { 10751da177e4SLinus Torvalds modified = completed = 1; 107655d84968SAlan Stern finish_urb(ohci, urb, 0); 10771da177e4SLinus Torvalds } 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds if (completed && !list_empty (&ed->td_list)) 10801da177e4SLinus Torvalds goto rescan_this; 10811da177e4SLinus Torvalds 1082977dcfdcSAlan Stern /* 1083977dcfdcSAlan Stern * If no TDs are queued, take ED off the ed_rm_list. 1084977dcfdcSAlan Stern * Otherwise, if the HC is running, reschedule. 1085977dcfdcSAlan Stern * If not, leave it on the list for further dequeues. 1086977dcfdcSAlan Stern */ 1087977dcfdcSAlan Stern if (list_empty(&ed->td_list)) { 1088977dcfdcSAlan Stern *last = ed->ed_next; 1089977dcfdcSAlan Stern ed->ed_next = NULL; 1090*81e38333SAlan Stern list_del(&ed->in_use_list); 1091977dcfdcSAlan Stern } else if (ohci->rh_state == OHCI_RH_RUNNING) { 1092977dcfdcSAlan Stern *last = ed->ed_next; 1093977dcfdcSAlan Stern ed->ed_next = NULL; 10941da177e4SLinus Torvalds ed_schedule(ohci, ed); 1095977dcfdcSAlan Stern } else { 1096977dcfdcSAlan Stern last = &ed->ed_next; 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds if (modified) 11001da177e4SLinus Torvalds goto rescan_all; 11011da177e4SLinus Torvalds } 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds /* maybe reenable control and bulk lists */ 1104b7463c71SAlan Stern if (ohci->rh_state == OHCI_RH_RUNNING && !ohci->ed_rm_list) { 11051da177e4SLinus Torvalds u32 command = 0, control = 0; 11061da177e4SLinus Torvalds 11071da177e4SLinus Torvalds if (ohci->ed_controltail) { 11081da177e4SLinus Torvalds command |= OHCI_CLF; 110989a0fd18SMike Nuss if (quirk_zfmicro(ohci)) 11100e498763SDavid Brownell mdelay(1); 11111da177e4SLinus Torvalds if (!(ohci->hc_control & OHCI_CTRL_CLE)) { 11121da177e4SLinus Torvalds control |= OHCI_CTRL_CLE; 11131da177e4SLinus Torvalds ohci_writel (ohci, 0, 11141da177e4SLinus Torvalds &ohci->regs->ed_controlcurrent); 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds if (ohci->ed_bulktail) { 11181da177e4SLinus Torvalds command |= OHCI_BLF; 111989a0fd18SMike Nuss if (quirk_zfmicro(ohci)) 11200e498763SDavid Brownell mdelay(1); 11211da177e4SLinus Torvalds if (!(ohci->hc_control & OHCI_CTRL_BLE)) { 11221da177e4SLinus Torvalds control |= OHCI_CTRL_BLE; 11231da177e4SLinus Torvalds ohci_writel (ohci, 0, 11241da177e4SLinus Torvalds &ohci->regs->ed_bulkcurrent); 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds /* CLE/BLE to enable, CLF/BLF to (maybe) kickstart */ 11291da177e4SLinus Torvalds if (control) { 11301da177e4SLinus Torvalds ohci->hc_control |= control; 113189a0fd18SMike Nuss if (quirk_zfmicro(ohci)) 11320e498763SDavid Brownell mdelay(1); 11331da177e4SLinus Torvalds ohci_writel (ohci, ohci->hc_control, 11341da177e4SLinus Torvalds &ohci->regs->control); 11351da177e4SLinus Torvalds } 11360e498763SDavid Brownell if (command) { 113789a0fd18SMike Nuss if (quirk_zfmicro(ohci)) 11380e498763SDavid Brownell mdelay(1); 11391da177e4SLinus Torvalds ohci_writel (ohci, command, &ohci->regs->cmdstatus); 11401da177e4SLinus Torvalds } 11411da177e4SLinus Torvalds } 11420e498763SDavid Brownell } 11431da177e4SLinus Torvalds 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 11471da177e4SLinus Torvalds 114895d9a01dSAlan Stern /* Take back a TD from the host controller */ 114989a0fd18SMike Nuss static void takeback_td(struct ohci_hcd *ohci, struct td *td) 11501da177e4SLinus Torvalds { 11511da177e4SLinus Torvalds struct urb *urb = td->urb; 11521da177e4SLinus Torvalds urb_priv_t *urb_priv = urb->hcpriv; 11531da177e4SLinus Torvalds struct ed *ed = td->ed; 115455d84968SAlan Stern int status; 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds /* update URB's length and status from TD */ 115755d84968SAlan Stern status = td_done(ohci, urb, td); 11581da177e4SLinus Torvalds urb_priv->td_cnt++; 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds /* If all this urb's TDs are done, call complete() */ 1161a8693424SAlan Stern if (urb_priv->td_cnt >= urb_priv->length) 116255d84968SAlan Stern finish_urb(ohci, urb, status); 11631da177e4SLinus Torvalds 11641da177e4SLinus Torvalds /* clean schedule: unlink EDs that are no longer busy */ 11651da177e4SLinus Torvalds if (list_empty(&ed->td_list)) { 11661da177e4SLinus Torvalds if (ed->state == ED_OPER) 11671da177e4SLinus Torvalds start_ed_unlink(ohci, ed); 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds /* ... reenabling halted EDs only after fault cleanup */ 117089a0fd18SMike Nuss } else if ((ed->hwINFO & cpu_to_hc32(ohci, ED_SKIP | ED_DEQUEUE)) 11711da177e4SLinus Torvalds == cpu_to_hc32(ohci, ED_SKIP)) { 11721da177e4SLinus Torvalds td = list_entry(ed->td_list.next, struct td, td_list); 11731da177e4SLinus Torvalds if (!(td->hwINFO & cpu_to_hc32(ohci, TD_DONE))) { 11741da177e4SLinus Torvalds ed->hwINFO &= ~cpu_to_hc32(ohci, ED_SKIP); 11751da177e4SLinus Torvalds /* ... hc may need waking-up */ 11761da177e4SLinus Torvalds switch (ed->type) { 11771da177e4SLinus Torvalds case PIPE_CONTROL: 11781da177e4SLinus Torvalds ohci_writel(ohci, OHCI_CLF, 11791da177e4SLinus Torvalds &ohci->regs->cmdstatus); 11801da177e4SLinus Torvalds break; 11811da177e4SLinus Torvalds case PIPE_BULK: 11821da177e4SLinus Torvalds ohci_writel(ohci, OHCI_BLF, 11831da177e4SLinus Torvalds &ohci->regs->cmdstatus); 11841da177e4SLinus Torvalds break; 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds } 118889a0fd18SMike Nuss } 11891da177e4SLinus Torvalds 119089a0fd18SMike Nuss /* 119189a0fd18SMike Nuss * Process normal completions (error or success) and clean the schedules. 119289a0fd18SMike Nuss * 119389a0fd18SMike Nuss * This is the main path for handing urbs back to drivers. The only other 119489a0fd18SMike Nuss * normal path is finish_unlinks(), which unlinks URBs using ed_rm_list, 119595d9a01dSAlan Stern * instead of scanning the (re-reversed) donelist as this does. 119689a0fd18SMike Nuss */ 1197c6fcb85eSAlan Stern static void process_done_list(struct ohci_hcd *ohci) 119889a0fd18SMike Nuss { 1199c6fcb85eSAlan Stern struct td *td; 120089a0fd18SMike Nuss 1201c6fcb85eSAlan Stern while (ohci->dl_start) { 1202c6fcb85eSAlan Stern td = ohci->dl_start; 1203c6fcb85eSAlan Stern if (td == ohci->dl_end) 1204c6fcb85eSAlan Stern ohci->dl_start = ohci->dl_end = NULL; 1205c6fcb85eSAlan Stern else 1206c6fcb85eSAlan Stern ohci->dl_start = td->next_dl_td; 120750ce5c06SAlan Stern 120889a0fd18SMike Nuss takeback_td(ohci, td); 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds } 1211cdb4dd15SAlan Stern 1212cdb4dd15SAlan Stern /* 1213cdb4dd15SAlan Stern * TD takeback and URB giveback must be single-threaded. 1214cdb4dd15SAlan Stern * This routine takes care of it all. 1215cdb4dd15SAlan Stern */ 1216cdb4dd15SAlan Stern static void ohci_work(struct ohci_hcd *ohci) 1217cdb4dd15SAlan Stern { 1218cdb4dd15SAlan Stern if (ohci->working) { 1219cdb4dd15SAlan Stern ohci->restart_work = 1; 1220cdb4dd15SAlan Stern return; 1221cdb4dd15SAlan Stern } 1222cdb4dd15SAlan Stern ohci->working = 1; 1223cdb4dd15SAlan Stern 1224cdb4dd15SAlan Stern restart: 1225cdb4dd15SAlan Stern process_done_list(ohci); 1226cdb4dd15SAlan Stern if (ohci->ed_rm_list) 1227cdb4dd15SAlan Stern finish_unlinks(ohci); 1228cdb4dd15SAlan Stern 1229cdb4dd15SAlan Stern if (ohci->restart_work) { 1230cdb4dd15SAlan Stern ohci->restart_work = 0; 1231cdb4dd15SAlan Stern goto restart; 1232cdb4dd15SAlan Stern } 1233cdb4dd15SAlan Stern ohci->working = 0; 1234cdb4dd15SAlan Stern } 1235