11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Universal Host Controller Interface driver for USB. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Maintainer: Alan Stern <stern@rowland.harvard.edu> 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * (C) Copyright 1999 Linus Torvalds 71da177e4SLinus Torvalds * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com 81da177e4SLinus Torvalds * (C) Copyright 1999 Randy Dunlap 91da177e4SLinus Torvalds * (C) Copyright 1999 Georg Acher, acher@in.tum.de 101da177e4SLinus Torvalds * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de 111da177e4SLinus Torvalds * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch 121da177e4SLinus Torvalds * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at 131da177e4SLinus Torvalds * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface 141da177e4SLinus Torvalds * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com). 151da177e4SLinus Torvalds * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c) 16*b761d9d8SAlan Stern * (C) Copyright 2004-2006 Alan Stern, stern@rowland.harvard.edu 171da177e4SLinus Torvalds */ 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds /* 211da177e4SLinus Torvalds * Technically, updating td->status here is a race, but it's not really a 221da177e4SLinus Torvalds * problem. The worst that can happen is that we set the IOC bit again 231da177e4SLinus Torvalds * generating a spurious interrupt. We could fix this by creating another 241da177e4SLinus Torvalds * QH and leaving the IOC bit always set, but then we would have to play 251da177e4SLinus Torvalds * games with the FSBR code to make sure we get the correct order in all 261da177e4SLinus Torvalds * the cases. I don't think it's worth the effort 271da177e4SLinus Torvalds */ 28dccf4a48SAlan Stern static void uhci_set_next_interrupt(struct uhci_hcd *uhci) 291da177e4SLinus Torvalds { 306c1b445cSAlan Stern if (uhci->is_stopped) 311f09df8bSAlan Stern mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies); 321da177e4SLinus Torvalds uhci->term_td->status |= cpu_to_le32(TD_CTRL_IOC); 331da177e4SLinus Torvalds } 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds static inline void uhci_clear_next_interrupt(struct uhci_hcd *uhci) 361da177e4SLinus Torvalds { 371da177e4SLinus Torvalds uhci->term_td->status &= ~cpu_to_le32(TD_CTRL_IOC); 381da177e4SLinus Torvalds } 391da177e4SLinus Torvalds 4084afddd7SAlan Stern 4184afddd7SAlan Stern /* 4284afddd7SAlan Stern * Full-Speed Bandwidth Reclamation (FSBR). 4384afddd7SAlan Stern * We turn on FSBR whenever a queue that wants it is advancing, 4484afddd7SAlan Stern * and leave it on for a short time thereafter. 4584afddd7SAlan Stern */ 4684afddd7SAlan Stern static void uhci_fsbr_on(struct uhci_hcd *uhci) 4784afddd7SAlan Stern { 4884afddd7SAlan Stern uhci->fsbr_is_on = 1; 4984afddd7SAlan Stern uhci->skel_term_qh->link = cpu_to_le32( 5084afddd7SAlan Stern uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH; 5184afddd7SAlan Stern } 5284afddd7SAlan Stern 5384afddd7SAlan Stern static void uhci_fsbr_off(struct uhci_hcd *uhci) 5484afddd7SAlan Stern { 5584afddd7SAlan Stern uhci->fsbr_is_on = 0; 5684afddd7SAlan Stern uhci->skel_term_qh->link = UHCI_PTR_TERM; 5784afddd7SAlan Stern } 5884afddd7SAlan Stern 5984afddd7SAlan Stern static void uhci_add_fsbr(struct uhci_hcd *uhci, struct urb *urb) 6084afddd7SAlan Stern { 6184afddd7SAlan Stern struct urb_priv *urbp = urb->hcpriv; 6284afddd7SAlan Stern 6384afddd7SAlan Stern if (!(urb->transfer_flags & URB_NO_FSBR)) 6484afddd7SAlan Stern urbp->fsbr = 1; 6584afddd7SAlan Stern } 6684afddd7SAlan Stern 6784afddd7SAlan Stern static void uhci_qh_wants_fsbr(struct uhci_hcd *uhci, struct uhci_qh *qh) 6884afddd7SAlan Stern { 6984afddd7SAlan Stern struct urb_priv *urbp = 7084afddd7SAlan Stern list_entry(qh->queue.next, struct urb_priv, node); 7184afddd7SAlan Stern 7284afddd7SAlan Stern if (urbp->fsbr) { 7384afddd7SAlan Stern uhci->fsbr_jiffies = jiffies; 7484afddd7SAlan Stern if (!uhci->fsbr_is_on) 7584afddd7SAlan Stern uhci_fsbr_on(uhci); 7684afddd7SAlan Stern } 7784afddd7SAlan Stern } 7884afddd7SAlan Stern 7984afddd7SAlan Stern 802532178aSAlan Stern static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci) 811da177e4SLinus Torvalds { 821da177e4SLinus Torvalds dma_addr_t dma_handle; 831da177e4SLinus Torvalds struct uhci_td *td; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds td = dma_pool_alloc(uhci->td_pool, GFP_ATOMIC, &dma_handle); 861da177e4SLinus Torvalds if (!td) 871da177e4SLinus Torvalds return NULL; 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds td->dma_handle = dma_handle; 901da177e4SLinus Torvalds td->frame = -1; 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds INIT_LIST_HEAD(&td->list); 931da177e4SLinus Torvalds INIT_LIST_HEAD(&td->fl_list); 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds return td; 961da177e4SLinus Torvalds } 971da177e4SLinus Torvalds 98dccf4a48SAlan Stern static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td) 99dccf4a48SAlan Stern { 100dccf4a48SAlan Stern if (!list_empty(&td->list)) 101dccf4a48SAlan Stern dev_warn(uhci_dev(uhci), "td %p still in list!\n", td); 102dccf4a48SAlan Stern if (!list_empty(&td->fl_list)) 103dccf4a48SAlan Stern dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td); 104dccf4a48SAlan Stern 105dccf4a48SAlan Stern dma_pool_free(uhci->td_pool, td, td->dma_handle); 106dccf4a48SAlan Stern } 107dccf4a48SAlan Stern 1081da177e4SLinus Torvalds static inline void uhci_fill_td(struct uhci_td *td, u32 status, 1091da177e4SLinus Torvalds u32 token, u32 buffer) 1101da177e4SLinus Torvalds { 1111da177e4SLinus Torvalds td->status = cpu_to_le32(status); 1121da177e4SLinus Torvalds td->token = cpu_to_le32(token); 1131da177e4SLinus Torvalds td->buffer = cpu_to_le32(buffer); 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds 11604538a25SAlan Stern static void uhci_add_td_to_urbp(struct uhci_td *td, struct urb_priv *urbp) 11704538a25SAlan Stern { 11804538a25SAlan Stern list_add_tail(&td->list, &urbp->td_list); 11904538a25SAlan Stern } 12004538a25SAlan Stern 12104538a25SAlan Stern static void uhci_remove_td_from_urbp(struct uhci_td *td) 12204538a25SAlan Stern { 12304538a25SAlan Stern list_del_init(&td->list); 12404538a25SAlan Stern } 12504538a25SAlan Stern 1261da177e4SLinus Torvalds /* 127687f5f34SAlan Stern * We insert Isochronous URBs directly into the frame list at the beginning 1281da177e4SLinus Torvalds */ 129dccf4a48SAlan Stern static inline void uhci_insert_td_in_frame_list(struct uhci_hcd *uhci, 130dccf4a48SAlan Stern struct uhci_td *td, unsigned framenum) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds framenum &= (UHCI_NUMFRAMES - 1); 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds td->frame = framenum; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds /* Is there a TD already mapped there? */ 137a1d59ce8SAlan Stern if (uhci->frame_cpu[framenum]) { 1381da177e4SLinus Torvalds struct uhci_td *ftd, *ltd; 1391da177e4SLinus Torvalds 140a1d59ce8SAlan Stern ftd = uhci->frame_cpu[framenum]; 1411da177e4SLinus Torvalds ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list); 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds list_add_tail(&td->fl_list, &ftd->fl_list); 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds td->link = ltd->link; 1461da177e4SLinus Torvalds wmb(); 1471da177e4SLinus Torvalds ltd->link = cpu_to_le32(td->dma_handle); 1481da177e4SLinus Torvalds } else { 149a1d59ce8SAlan Stern td->link = uhci->frame[framenum]; 1501da177e4SLinus Torvalds wmb(); 151a1d59ce8SAlan Stern uhci->frame[framenum] = cpu_to_le32(td->dma_handle); 152a1d59ce8SAlan Stern uhci->frame_cpu[framenum] = td; 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds 156dccf4a48SAlan Stern static inline void uhci_remove_td_from_frame_list(struct uhci_hcd *uhci, 157b81d3436SAlan Stern struct uhci_td *td) 1581da177e4SLinus Torvalds { 1591da177e4SLinus Torvalds /* If it's not inserted, don't remove it */ 160b81d3436SAlan Stern if (td->frame == -1) { 161b81d3436SAlan Stern WARN_ON(!list_empty(&td->fl_list)); 1621da177e4SLinus Torvalds return; 163b81d3436SAlan Stern } 1641da177e4SLinus Torvalds 165b81d3436SAlan Stern if (uhci->frame_cpu[td->frame] == td) { 1661da177e4SLinus Torvalds if (list_empty(&td->fl_list)) { 167a1d59ce8SAlan Stern uhci->frame[td->frame] = td->link; 168a1d59ce8SAlan Stern uhci->frame_cpu[td->frame] = NULL; 1691da177e4SLinus Torvalds } else { 1701da177e4SLinus Torvalds struct uhci_td *ntd; 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds ntd = list_entry(td->fl_list.next, struct uhci_td, fl_list); 173a1d59ce8SAlan Stern uhci->frame[td->frame] = cpu_to_le32(ntd->dma_handle); 174a1d59ce8SAlan Stern uhci->frame_cpu[td->frame] = ntd; 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds } else { 1771da177e4SLinus Torvalds struct uhci_td *ptd; 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds ptd = list_entry(td->fl_list.prev, struct uhci_td, fl_list); 1801da177e4SLinus Torvalds ptd->link = td->link; 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds list_del_init(&td->fl_list); 1841da177e4SLinus Torvalds td->frame = -1; 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 187dccf4a48SAlan Stern /* 188dccf4a48SAlan Stern * Remove all the TDs for an Isochronous URB from the frame list 189dccf4a48SAlan Stern */ 190dccf4a48SAlan Stern static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb) 191b81d3436SAlan Stern { 192b81d3436SAlan Stern struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv; 193b81d3436SAlan Stern struct uhci_td *td; 194b81d3436SAlan Stern 195b81d3436SAlan Stern list_for_each_entry(td, &urbp->td_list, list) 196dccf4a48SAlan Stern uhci_remove_td_from_frame_list(uhci, td); 197b81d3436SAlan Stern wmb(); 198b81d3436SAlan Stern } 199b81d3436SAlan Stern 200dccf4a48SAlan Stern static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci, 201dccf4a48SAlan Stern struct usb_device *udev, struct usb_host_endpoint *hep) 2021da177e4SLinus Torvalds { 2031da177e4SLinus Torvalds dma_addr_t dma_handle; 2041da177e4SLinus Torvalds struct uhci_qh *qh; 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds qh = dma_pool_alloc(uhci->qh_pool, GFP_ATOMIC, &dma_handle); 2071da177e4SLinus Torvalds if (!qh) 2081da177e4SLinus Torvalds return NULL; 2091da177e4SLinus Torvalds 21059e29ed9SAlan Stern memset(qh, 0, sizeof(*qh)); 2111da177e4SLinus Torvalds qh->dma_handle = dma_handle; 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds qh->element = UHCI_PTR_TERM; 2141da177e4SLinus Torvalds qh->link = UHCI_PTR_TERM; 2151da177e4SLinus Torvalds 216dccf4a48SAlan Stern INIT_LIST_HEAD(&qh->queue); 217dccf4a48SAlan Stern INIT_LIST_HEAD(&qh->node); 2181da177e4SLinus Torvalds 219dccf4a48SAlan Stern if (udev) { /* Normal QH */ 220af0bb599SAlan Stern qh->dummy_td = uhci_alloc_td(uhci); 221af0bb599SAlan Stern if (!qh->dummy_td) { 222af0bb599SAlan Stern dma_pool_free(uhci->qh_pool, qh, dma_handle); 223af0bb599SAlan Stern return NULL; 224af0bb599SAlan Stern } 225dccf4a48SAlan Stern qh->state = QH_STATE_IDLE; 226dccf4a48SAlan Stern qh->hep = hep; 227dccf4a48SAlan Stern qh->udev = udev; 228dccf4a48SAlan Stern hep->hcpriv = qh; 2294de7d2c2SAlan Stern qh->type = hep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; 2301da177e4SLinus Torvalds 231dccf4a48SAlan Stern } else { /* Skeleton QH */ 232dccf4a48SAlan Stern qh->state = QH_STATE_ACTIVE; 2334de7d2c2SAlan Stern qh->type = -1; 234dccf4a48SAlan Stern } 2351da177e4SLinus Torvalds return qh; 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh) 2391da177e4SLinus Torvalds { 240dccf4a48SAlan Stern WARN_ON(qh->state != QH_STATE_IDLE && qh->udev); 241dccf4a48SAlan Stern if (!list_empty(&qh->queue)) 2421da177e4SLinus Torvalds dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh); 2431da177e4SLinus Torvalds 244dccf4a48SAlan Stern list_del(&qh->node); 245dccf4a48SAlan Stern if (qh->udev) { 246dccf4a48SAlan Stern qh->hep->hcpriv = NULL; 247af0bb599SAlan Stern uhci_free_td(uhci, qh->dummy_td); 248dccf4a48SAlan Stern } 2491da177e4SLinus Torvalds dma_pool_free(uhci->qh_pool, qh, qh->dma_handle); 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds /* 253a0b458b6SAlan Stern * When a queue is stopped and a dequeued URB is given back, adjust 254a0b458b6SAlan Stern * the previous TD link (if the URB isn't first on the queue) or 255a0b458b6SAlan Stern * save its toggle value (if it is first and is currently executing). 2560ed8fee1SAlan Stern */ 257a0b458b6SAlan Stern static void uhci_cleanup_queue(struct uhci_qh *qh, 258a0b458b6SAlan Stern struct urb *urb) 2590ed8fee1SAlan Stern { 260a0b458b6SAlan Stern struct urb_priv *urbp = urb->hcpriv; 2610ed8fee1SAlan Stern struct uhci_td *td; 2620ed8fee1SAlan Stern 263a0b458b6SAlan Stern /* Isochronous pipes don't use toggles and their TD link pointers 264a0b458b6SAlan Stern * get adjusted during uhci_urb_dequeue(). */ 265a0b458b6SAlan Stern if (qh->type == USB_ENDPOINT_XFER_ISOC) 266a0b458b6SAlan Stern return; 267a0b458b6SAlan Stern 268a0b458b6SAlan Stern /* If the URB isn't first on its queue, adjust the link pointer 269a0b458b6SAlan Stern * of the last TD in the previous URB. The toggle doesn't need 270a0b458b6SAlan Stern * to be saved since this URB can't be executing yet. */ 271a0b458b6SAlan Stern if (qh->queue.next != &urbp->node) { 272a0b458b6SAlan Stern struct urb_priv *purbp; 273a0b458b6SAlan Stern struct uhci_td *ptd; 274a0b458b6SAlan Stern 275a0b458b6SAlan Stern purbp = list_entry(urbp->node.prev, struct urb_priv, node); 276a0b458b6SAlan Stern WARN_ON(list_empty(&purbp->td_list)); 277a0b458b6SAlan Stern ptd = list_entry(purbp->td_list.prev, struct uhci_td, 278a0b458b6SAlan Stern list); 279a0b458b6SAlan Stern td = list_entry(urbp->td_list.prev, struct uhci_td, 280a0b458b6SAlan Stern list); 281a0b458b6SAlan Stern ptd->link = td->link; 282a0b458b6SAlan Stern return; 283a0b458b6SAlan Stern } 284a0b458b6SAlan Stern 2850ed8fee1SAlan Stern /* If the QH element pointer is UHCI_PTR_TERM then then currently 2860ed8fee1SAlan Stern * executing URB has already been unlinked, so this one isn't it. */ 287a0b458b6SAlan Stern if (qh_element(qh) == UHCI_PTR_TERM) 2880ed8fee1SAlan Stern return; 2890ed8fee1SAlan Stern qh->element = UHCI_PTR_TERM; 2900ed8fee1SAlan Stern 291a0b458b6SAlan Stern /* Control pipes have to worry about toggles */ 292a0b458b6SAlan Stern if (qh->type == USB_ENDPOINT_XFER_CONTROL) 2930ed8fee1SAlan Stern return; 2940ed8fee1SAlan Stern 295a0b458b6SAlan Stern /* Save the next toggle value */ 29659e29ed9SAlan Stern WARN_ON(list_empty(&urbp->td_list)); 29759e29ed9SAlan Stern td = list_entry(urbp->td_list.next, struct uhci_td, list); 2980ed8fee1SAlan Stern qh->needs_fixup = 1; 2990ed8fee1SAlan Stern qh->initial_toggle = uhci_toggle(td_token(td)); 3000ed8fee1SAlan Stern } 3010ed8fee1SAlan Stern 3020ed8fee1SAlan Stern /* 3030ed8fee1SAlan Stern * Fix up the data toggles for URBs in a queue, when one of them 3040ed8fee1SAlan Stern * terminates early (short transfer, error, or dequeued). 3050ed8fee1SAlan Stern */ 3060ed8fee1SAlan Stern static void uhci_fixup_toggles(struct uhci_qh *qh, int skip_first) 3070ed8fee1SAlan Stern { 3080ed8fee1SAlan Stern struct urb_priv *urbp = NULL; 3090ed8fee1SAlan Stern struct uhci_td *td; 3100ed8fee1SAlan Stern unsigned int toggle = qh->initial_toggle; 3110ed8fee1SAlan Stern unsigned int pipe; 3120ed8fee1SAlan Stern 3130ed8fee1SAlan Stern /* Fixups for a short transfer start with the second URB in the 3140ed8fee1SAlan Stern * queue (the short URB is the first). */ 3150ed8fee1SAlan Stern if (skip_first) 3160ed8fee1SAlan Stern urbp = list_entry(qh->queue.next, struct urb_priv, node); 3170ed8fee1SAlan Stern 3180ed8fee1SAlan Stern /* When starting with the first URB, if the QH element pointer is 3190ed8fee1SAlan Stern * still valid then we know the URB's toggles are okay. */ 3200ed8fee1SAlan Stern else if (qh_element(qh) != UHCI_PTR_TERM) 3210ed8fee1SAlan Stern toggle = 2; 3220ed8fee1SAlan Stern 3230ed8fee1SAlan Stern /* Fix up the toggle for the URBs in the queue. Normally this 3240ed8fee1SAlan Stern * loop won't run more than once: When an error or short transfer 3250ed8fee1SAlan Stern * occurs, the queue usually gets emptied. */ 3261393adb2SAlan Stern urbp = list_prepare_entry(urbp, &qh->queue, node); 3270ed8fee1SAlan Stern list_for_each_entry_continue(urbp, &qh->queue, node) { 3280ed8fee1SAlan Stern 3290ed8fee1SAlan Stern /* If the first TD has the right toggle value, we don't 3300ed8fee1SAlan Stern * need to change any toggles in this URB */ 3310ed8fee1SAlan Stern td = list_entry(urbp->td_list.next, struct uhci_td, list); 3320ed8fee1SAlan Stern if (toggle > 1 || uhci_toggle(td_token(td)) == toggle) { 3330ed8fee1SAlan Stern td = list_entry(urbp->td_list.next, struct uhci_td, 3340ed8fee1SAlan Stern list); 3350ed8fee1SAlan Stern toggle = uhci_toggle(td_token(td)) ^ 1; 3360ed8fee1SAlan Stern 3370ed8fee1SAlan Stern /* Otherwise all the toggles in the URB have to be switched */ 3380ed8fee1SAlan Stern } else { 3390ed8fee1SAlan Stern list_for_each_entry(td, &urbp->td_list, list) { 3400ed8fee1SAlan Stern td->token ^= __constant_cpu_to_le32( 3410ed8fee1SAlan Stern TD_TOKEN_TOGGLE); 3420ed8fee1SAlan Stern toggle ^= 1; 3430ed8fee1SAlan Stern } 3440ed8fee1SAlan Stern } 3450ed8fee1SAlan Stern } 3460ed8fee1SAlan Stern 3470ed8fee1SAlan Stern wmb(); 3480ed8fee1SAlan Stern pipe = list_entry(qh->queue.next, struct urb_priv, node)->urb->pipe; 3490ed8fee1SAlan Stern usb_settoggle(qh->udev, usb_pipeendpoint(pipe), 3500ed8fee1SAlan Stern usb_pipeout(pipe), toggle); 3510ed8fee1SAlan Stern qh->needs_fixup = 0; 3520ed8fee1SAlan Stern } 3530ed8fee1SAlan Stern 3540ed8fee1SAlan Stern /* 355dccf4a48SAlan Stern * Put a QH on the schedule in both hardware and software 3561da177e4SLinus Torvalds */ 357dccf4a48SAlan Stern static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh) 3581da177e4SLinus Torvalds { 3591da177e4SLinus Torvalds struct uhci_qh *pqh; 3601da177e4SLinus Torvalds 361dccf4a48SAlan Stern WARN_ON(list_empty(&qh->queue)); 362dccf4a48SAlan Stern 363dccf4a48SAlan Stern /* Set the element pointer if it isn't set already. 364dccf4a48SAlan Stern * This isn't needed for Isochronous queues, but it doesn't hurt. */ 365dccf4a48SAlan Stern if (qh_element(qh) == UHCI_PTR_TERM) { 366dccf4a48SAlan Stern struct urb_priv *urbp = list_entry(qh->queue.next, 367dccf4a48SAlan Stern struct urb_priv, node); 368dccf4a48SAlan Stern struct uhci_td *td = list_entry(urbp->td_list.next, 369dccf4a48SAlan Stern struct uhci_td, list); 370dccf4a48SAlan Stern 371dccf4a48SAlan Stern qh->element = cpu_to_le32(td->dma_handle); 372dccf4a48SAlan Stern } 373dccf4a48SAlan Stern 37484afddd7SAlan Stern /* Treat the queue as if it has just advanced */ 37584afddd7SAlan Stern qh->wait_expired = 0; 37684afddd7SAlan Stern qh->advance_jiffies = jiffies; 37784afddd7SAlan Stern 378dccf4a48SAlan Stern if (qh->state == QH_STATE_ACTIVE) 3791da177e4SLinus Torvalds return; 380dccf4a48SAlan Stern qh->state = QH_STATE_ACTIVE; 381dccf4a48SAlan Stern 382dccf4a48SAlan Stern /* Move the QH from its old list to the end of the appropriate 383dccf4a48SAlan Stern * skeleton's list */ 3840ed8fee1SAlan Stern if (qh == uhci->next_qh) 3850ed8fee1SAlan Stern uhci->next_qh = list_entry(qh->node.next, struct uhci_qh, 3860ed8fee1SAlan Stern node); 387dccf4a48SAlan Stern list_move_tail(&qh->node, &qh->skel->node); 388dccf4a48SAlan Stern 389dccf4a48SAlan Stern /* Link it into the schedule */ 390dccf4a48SAlan Stern pqh = list_entry(qh->node.prev, struct uhci_qh, node); 391dccf4a48SAlan Stern qh->link = pqh->link; 392dccf4a48SAlan Stern wmb(); 393dccf4a48SAlan Stern pqh->link = UHCI_PTR_QH | cpu_to_le32(qh->dma_handle); 394dccf4a48SAlan Stern } 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds /* 397dccf4a48SAlan Stern * Take a QH off the hardware schedule 3981da177e4SLinus Torvalds */ 399dccf4a48SAlan Stern static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh) 400dccf4a48SAlan Stern { 401dccf4a48SAlan Stern struct uhci_qh *pqh; 4021da177e4SLinus Torvalds 403dccf4a48SAlan Stern if (qh->state == QH_STATE_UNLINKING) 404dccf4a48SAlan Stern return; 405dccf4a48SAlan Stern WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev); 406dccf4a48SAlan Stern qh->state = QH_STATE_UNLINKING; 4071da177e4SLinus Torvalds 408dccf4a48SAlan Stern /* Unlink the QH from the schedule and record when we did it */ 409dccf4a48SAlan Stern pqh = list_entry(qh->node.prev, struct uhci_qh, node); 410dccf4a48SAlan Stern pqh->link = qh->link; 411dccf4a48SAlan Stern mb(); 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds uhci_get_current_frame_number(uhci); 414dccf4a48SAlan Stern qh->unlink_frame = uhci->frame_number; 4151da177e4SLinus Torvalds 416dccf4a48SAlan Stern /* Force an interrupt so we know when the QH is fully unlinked */ 417dccf4a48SAlan Stern if (list_empty(&uhci->skel_unlink_qh->node)) 4181da177e4SLinus Torvalds uhci_set_next_interrupt(uhci); 4191da177e4SLinus Torvalds 420dccf4a48SAlan Stern /* Move the QH from its old list to the end of the unlinking list */ 4210ed8fee1SAlan Stern if (qh == uhci->next_qh) 4220ed8fee1SAlan Stern uhci->next_qh = list_entry(qh->node.next, struct uhci_qh, 4230ed8fee1SAlan Stern node); 424dccf4a48SAlan Stern list_move_tail(&qh->node, &uhci->skel_unlink_qh->node); 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds /* 428dccf4a48SAlan Stern * When we and the controller are through with a QH, it becomes IDLE. 429dccf4a48SAlan Stern * This happens when a QH has been off the schedule (on the unlinking 430dccf4a48SAlan Stern * list) for more than one frame, or when an error occurs while adding 431dccf4a48SAlan Stern * the first URB onto a new QH. 4321da177e4SLinus Torvalds */ 433dccf4a48SAlan Stern static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh) 434dccf4a48SAlan Stern { 435dccf4a48SAlan Stern WARN_ON(qh->state == QH_STATE_ACTIVE); 436dccf4a48SAlan Stern 4370ed8fee1SAlan Stern if (qh == uhci->next_qh) 4380ed8fee1SAlan Stern uhci->next_qh = list_entry(qh->node.next, struct uhci_qh, 4390ed8fee1SAlan Stern node); 440dccf4a48SAlan Stern list_move(&qh->node, &uhci->idle_qh_list); 441dccf4a48SAlan Stern qh->state = QH_STATE_IDLE; 442dccf4a48SAlan Stern 44359e29ed9SAlan Stern /* Now that the QH is idle, its post_td isn't being used */ 44459e29ed9SAlan Stern if (qh->post_td) { 44559e29ed9SAlan Stern uhci_free_td(uhci, qh->post_td); 44659e29ed9SAlan Stern qh->post_td = NULL; 44759e29ed9SAlan Stern } 44859e29ed9SAlan Stern 449dccf4a48SAlan Stern /* If anyone is waiting for a QH to become idle, wake them up */ 450dccf4a48SAlan Stern if (uhci->num_waiting) 451dccf4a48SAlan Stern wake_up_all(&uhci->waitqh); 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds 454dccf4a48SAlan Stern static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci, 455dccf4a48SAlan Stern struct urb *urb) 4561da177e4SLinus Torvalds { 4571da177e4SLinus Torvalds struct urb_priv *urbp; 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds urbp = kmem_cache_alloc(uhci_up_cachep, SLAB_ATOMIC); 4601da177e4SLinus Torvalds if (!urbp) 4611da177e4SLinus Torvalds return NULL; 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds memset((void *)urbp, 0, sizeof(*urbp)); 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds urbp->urb = urb; 4661da177e4SLinus Torvalds urb->hcpriv = urbp; 467dccf4a48SAlan Stern 468dccf4a48SAlan Stern INIT_LIST_HEAD(&urbp->node); 469dccf4a48SAlan Stern INIT_LIST_HEAD(&urbp->td_list); 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds return urbp; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 474dccf4a48SAlan Stern static void uhci_free_urb_priv(struct uhci_hcd *uhci, 475dccf4a48SAlan Stern struct urb_priv *urbp) 4761da177e4SLinus Torvalds { 4771da177e4SLinus Torvalds struct uhci_td *td, *tmp; 4781da177e4SLinus Torvalds 479dccf4a48SAlan Stern if (!list_empty(&urbp->node)) 480dccf4a48SAlan Stern dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n", 481dccf4a48SAlan Stern urbp->urb); 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds list_for_each_entry_safe(td, tmp, &urbp->td_list, list) { 48404538a25SAlan Stern uhci_remove_td_from_urbp(td); 48504538a25SAlan Stern uhci_free_td(uhci, td); 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds 488dccf4a48SAlan Stern urbp->urb->hcpriv = NULL; 4891da177e4SLinus Torvalds kmem_cache_free(uhci_up_cachep, urbp); 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds /* 4931da177e4SLinus Torvalds * Map status to standard result codes 4941da177e4SLinus Torvalds * 4951da177e4SLinus Torvalds * <status> is (td_status(td) & 0xF60000), a.k.a. 4961da177e4SLinus Torvalds * uhci_status_bits(td_status(td)). 4971da177e4SLinus Torvalds * Note: <status> does not include the TD_CTRL_NAK bit. 4981da177e4SLinus Torvalds * <dir_out> is True for output TDs and False for input TDs. 4991da177e4SLinus Torvalds */ 5001da177e4SLinus Torvalds static int uhci_map_status(int status, int dir_out) 5011da177e4SLinus Torvalds { 5021da177e4SLinus Torvalds if (!status) 5031da177e4SLinus Torvalds return 0; 5041da177e4SLinus Torvalds if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */ 5051da177e4SLinus Torvalds return -EPROTO; 5061da177e4SLinus Torvalds if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */ 5071da177e4SLinus Torvalds if (dir_out) 5081da177e4SLinus Torvalds return -EPROTO; 5091da177e4SLinus Torvalds else 5101da177e4SLinus Torvalds return -EILSEQ; 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds if (status & TD_CTRL_BABBLE) /* Babble */ 5131da177e4SLinus Torvalds return -EOVERFLOW; 5141da177e4SLinus Torvalds if (status & TD_CTRL_DBUFERR) /* Buffer error */ 5151da177e4SLinus Torvalds return -ENOSR; 5161da177e4SLinus Torvalds if (status & TD_CTRL_STALLED) /* Stalled */ 5171da177e4SLinus Torvalds return -EPIPE; 5181da177e4SLinus Torvalds WARN_ON(status & TD_CTRL_ACTIVE); /* Active */ 5191da177e4SLinus Torvalds return 0; 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds /* 5231da177e4SLinus Torvalds * Control transfers 5241da177e4SLinus Torvalds */ 525dccf4a48SAlan Stern static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, 526dccf4a48SAlan Stern struct uhci_qh *qh) 5271da177e4SLinus Torvalds { 5281da177e4SLinus Torvalds struct uhci_td *td; 5291da177e4SLinus Torvalds unsigned long destination, status; 530dccf4a48SAlan Stern int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize); 5311da177e4SLinus Torvalds int len = urb->transfer_buffer_length; 5321da177e4SLinus Torvalds dma_addr_t data = urb->transfer_dma; 533dccf4a48SAlan Stern __le32 *plink; 53404538a25SAlan Stern struct urb_priv *urbp = urb->hcpriv; 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds /* The "pipe" thing contains the destination in bits 8--18 */ 5371da177e4SLinus Torvalds destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP; 5381da177e4SLinus Torvalds 539af0bb599SAlan Stern /* 3 errors, dummy TD remains inactive */ 540af0bb599SAlan Stern status = uhci_maxerr(3); 5411da177e4SLinus Torvalds if (urb->dev->speed == USB_SPEED_LOW) 5421da177e4SLinus Torvalds status |= TD_CTRL_LS; 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds /* 5451da177e4SLinus Torvalds * Build the TD for the control request setup packet 5461da177e4SLinus Torvalds */ 547af0bb599SAlan Stern td = qh->dummy_td; 54804538a25SAlan Stern uhci_add_td_to_urbp(td, urbp); 549fa346568SAlan Stern uhci_fill_td(td, status, destination | uhci_explen(8), 5501da177e4SLinus Torvalds urb->setup_dma); 551dccf4a48SAlan Stern plink = &td->link; 552af0bb599SAlan Stern status |= TD_CTRL_ACTIVE; 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds /* 5551da177e4SLinus Torvalds * If direction is "send", change the packet ID from SETUP (0x2D) 5561da177e4SLinus Torvalds * to OUT (0xE1). Else change it from SETUP to IN (0x69) and 5571da177e4SLinus Torvalds * set Short Packet Detect (SPD) for all data packets. 5581da177e4SLinus Torvalds */ 5591da177e4SLinus Torvalds if (usb_pipeout(urb->pipe)) 5601da177e4SLinus Torvalds destination ^= (USB_PID_SETUP ^ USB_PID_OUT); 5611da177e4SLinus Torvalds else { 5621da177e4SLinus Torvalds destination ^= (USB_PID_SETUP ^ USB_PID_IN); 5631da177e4SLinus Torvalds status |= TD_CTRL_SPD; 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds /* 567687f5f34SAlan Stern * Build the DATA TDs 5681da177e4SLinus Torvalds */ 5691da177e4SLinus Torvalds while (len > 0) { 570dccf4a48SAlan Stern int pktsze = min(len, maxsze); 5711da177e4SLinus Torvalds 5722532178aSAlan Stern td = uhci_alloc_td(uhci); 5731da177e4SLinus Torvalds if (!td) 574af0bb599SAlan Stern goto nomem; 575dccf4a48SAlan Stern *plink = cpu_to_le32(td->dma_handle); 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds /* Alternate Data0/1 (start with Data1) */ 5781da177e4SLinus Torvalds destination ^= TD_TOKEN_TOGGLE; 5791da177e4SLinus Torvalds 58004538a25SAlan Stern uhci_add_td_to_urbp(td, urbp); 581fa346568SAlan Stern uhci_fill_td(td, status, destination | uhci_explen(pktsze), 5821da177e4SLinus Torvalds data); 583dccf4a48SAlan Stern plink = &td->link; 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds data += pktsze; 5861da177e4SLinus Torvalds len -= pktsze; 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds /* 5901da177e4SLinus Torvalds * Build the final TD for control status 5911da177e4SLinus Torvalds */ 5922532178aSAlan Stern td = uhci_alloc_td(uhci); 5931da177e4SLinus Torvalds if (!td) 594af0bb599SAlan Stern goto nomem; 595dccf4a48SAlan Stern *plink = cpu_to_le32(td->dma_handle); 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds /* 5981da177e4SLinus Torvalds * It's IN if the pipe is an output pipe or we're not expecting 5991da177e4SLinus Torvalds * data back. 6001da177e4SLinus Torvalds */ 6011da177e4SLinus Torvalds destination &= ~TD_TOKEN_PID_MASK; 6021da177e4SLinus Torvalds if (usb_pipeout(urb->pipe) || !urb->transfer_buffer_length) 6031da177e4SLinus Torvalds destination |= USB_PID_IN; 6041da177e4SLinus Torvalds else 6051da177e4SLinus Torvalds destination |= USB_PID_OUT; 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds destination |= TD_TOKEN_TOGGLE; /* End in Data1 */ 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds status &= ~TD_CTRL_SPD; 6101da177e4SLinus Torvalds 61104538a25SAlan Stern uhci_add_td_to_urbp(td, urbp); 6121da177e4SLinus Torvalds uhci_fill_td(td, status | TD_CTRL_IOC, 613fa346568SAlan Stern destination | uhci_explen(0), 0); 614af0bb599SAlan Stern plink = &td->link; 615af0bb599SAlan Stern 616af0bb599SAlan Stern /* 617af0bb599SAlan Stern * Build the new dummy TD and activate the old one 618af0bb599SAlan Stern */ 619af0bb599SAlan Stern td = uhci_alloc_td(uhci); 620af0bb599SAlan Stern if (!td) 621af0bb599SAlan Stern goto nomem; 622af0bb599SAlan Stern *plink = cpu_to_le32(td->dma_handle); 623af0bb599SAlan Stern 624af0bb599SAlan Stern uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0); 625af0bb599SAlan Stern wmb(); 626af0bb599SAlan Stern qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE); 627af0bb599SAlan Stern qh->dummy_td = td; 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds /* Low-speed transfers get a different queue, and won't hog the bus. 6301da177e4SLinus Torvalds * Also, some devices enumerate better without FSBR; the easiest way 6311da177e4SLinus Torvalds * to do that is to put URBs on the low-speed queue while the device 632630aa3cfSAlan Stern * isn't in the CONFIGURED state. */ 6331da177e4SLinus Torvalds if (urb->dev->speed == USB_SPEED_LOW || 634630aa3cfSAlan Stern urb->dev->state != USB_STATE_CONFIGURED) 635dccf4a48SAlan Stern qh->skel = uhci->skel_ls_control_qh; 6361da177e4SLinus Torvalds else { 637dccf4a48SAlan Stern qh->skel = uhci->skel_fs_control_qh; 63884afddd7SAlan Stern uhci_add_fsbr(uhci, urb); 6391da177e4SLinus Torvalds } 64059e29ed9SAlan Stern 64159e29ed9SAlan Stern urb->actual_length = -8; /* Account for the SETUP packet */ 642dccf4a48SAlan Stern return 0; 643af0bb599SAlan Stern 644af0bb599SAlan Stern nomem: 645af0bb599SAlan Stern /* Remove the dummy TD from the td_list so it doesn't get freed */ 64604538a25SAlan Stern uhci_remove_td_from_urbp(qh->dummy_td); 647af0bb599SAlan Stern return -ENOMEM; 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds /* 6511da177e4SLinus Torvalds * Common submit for bulk and interrupt 6521da177e4SLinus Torvalds */ 653dccf4a48SAlan Stern static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, 654dccf4a48SAlan Stern struct uhci_qh *qh) 6551da177e4SLinus Torvalds { 6561da177e4SLinus Torvalds struct uhci_td *td; 6571da177e4SLinus Torvalds unsigned long destination, status; 658dccf4a48SAlan Stern int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize); 6591da177e4SLinus Torvalds int len = urb->transfer_buffer_length; 6601da177e4SLinus Torvalds dma_addr_t data = urb->transfer_dma; 661af0bb599SAlan Stern __le32 *plink; 66204538a25SAlan Stern struct urb_priv *urbp = urb->hcpriv; 663af0bb599SAlan Stern unsigned int toggle; 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds if (len < 0) 6661da177e4SLinus Torvalds return -EINVAL; 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds /* The "pipe" thing contains the destination in bits 8--18 */ 6691da177e4SLinus Torvalds destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe); 670af0bb599SAlan Stern toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), 671af0bb599SAlan Stern usb_pipeout(urb->pipe)); 6721da177e4SLinus Torvalds 673af0bb599SAlan Stern /* 3 errors, dummy TD remains inactive */ 674af0bb599SAlan Stern status = uhci_maxerr(3); 6751da177e4SLinus Torvalds if (urb->dev->speed == USB_SPEED_LOW) 6761da177e4SLinus Torvalds status |= TD_CTRL_LS; 6771da177e4SLinus Torvalds if (usb_pipein(urb->pipe)) 6781da177e4SLinus Torvalds status |= TD_CTRL_SPD; 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds /* 681687f5f34SAlan Stern * Build the DATA TDs 6821da177e4SLinus Torvalds */ 683af0bb599SAlan Stern plink = NULL; 684af0bb599SAlan Stern td = qh->dummy_td; 6851da177e4SLinus Torvalds do { /* Allow zero length packets */ 6861da177e4SLinus Torvalds int pktsze = maxsze; 6871da177e4SLinus Torvalds 688dccf4a48SAlan Stern if (len <= pktsze) { /* The last packet */ 6891da177e4SLinus Torvalds pktsze = len; 6901da177e4SLinus Torvalds if (!(urb->transfer_flags & URB_SHORT_NOT_OK)) 6911da177e4SLinus Torvalds status &= ~TD_CTRL_SPD; 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds 694af0bb599SAlan Stern if (plink) { 6952532178aSAlan Stern td = uhci_alloc_td(uhci); 6961da177e4SLinus Torvalds if (!td) 697af0bb599SAlan Stern goto nomem; 698dccf4a48SAlan Stern *plink = cpu_to_le32(td->dma_handle); 699af0bb599SAlan Stern } 70004538a25SAlan Stern uhci_add_td_to_urbp(td, urbp); 701dccf4a48SAlan Stern uhci_fill_td(td, status, 702dccf4a48SAlan Stern destination | uhci_explen(pktsze) | 703af0bb599SAlan Stern (toggle << TD_TOKEN_TOGGLE_SHIFT), 7041da177e4SLinus Torvalds data); 705dccf4a48SAlan Stern plink = &td->link; 706af0bb599SAlan Stern status |= TD_CTRL_ACTIVE; 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds data += pktsze; 7091da177e4SLinus Torvalds len -= maxsze; 710af0bb599SAlan Stern toggle ^= 1; 7111da177e4SLinus Torvalds } while (len > 0); 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds /* 7141da177e4SLinus Torvalds * URB_ZERO_PACKET means adding a 0-length packet, if direction 7151da177e4SLinus Torvalds * is OUT and the transfer_length was an exact multiple of maxsze, 7161da177e4SLinus Torvalds * hence (len = transfer_length - N * maxsze) == 0 7171da177e4SLinus Torvalds * however, if transfer_length == 0, the zero packet was already 7181da177e4SLinus Torvalds * prepared above. 7191da177e4SLinus Torvalds */ 720dccf4a48SAlan Stern if ((urb->transfer_flags & URB_ZERO_PACKET) && 721dccf4a48SAlan Stern usb_pipeout(urb->pipe) && len == 0 && 722dccf4a48SAlan Stern urb->transfer_buffer_length > 0) { 7232532178aSAlan Stern td = uhci_alloc_td(uhci); 7241da177e4SLinus Torvalds if (!td) 725af0bb599SAlan Stern goto nomem; 726dccf4a48SAlan Stern *plink = cpu_to_le32(td->dma_handle); 7271da177e4SLinus Torvalds 72804538a25SAlan Stern uhci_add_td_to_urbp(td, urbp); 729af0bb599SAlan Stern uhci_fill_td(td, status, 730af0bb599SAlan Stern destination | uhci_explen(0) | 731af0bb599SAlan Stern (toggle << TD_TOKEN_TOGGLE_SHIFT), 7321da177e4SLinus Torvalds data); 733af0bb599SAlan Stern plink = &td->link; 7341da177e4SLinus Torvalds 735af0bb599SAlan Stern toggle ^= 1; 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds /* Set the interrupt-on-completion flag on the last packet. 7391da177e4SLinus Torvalds * A more-or-less typical 4 KB URB (= size of one memory page) 7401da177e4SLinus Torvalds * will require about 3 ms to transfer; that's a little on the 7411da177e4SLinus Torvalds * fast side but not enough to justify delaying an interrupt 7421da177e4SLinus Torvalds * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT 7431da177e4SLinus Torvalds * flag setting. */ 744dccf4a48SAlan Stern td->status |= __constant_cpu_to_le32(TD_CTRL_IOC); 7451da177e4SLinus Torvalds 746af0bb599SAlan Stern /* 747af0bb599SAlan Stern * Build the new dummy TD and activate the old one 748af0bb599SAlan Stern */ 749af0bb599SAlan Stern td = uhci_alloc_td(uhci); 750af0bb599SAlan Stern if (!td) 751af0bb599SAlan Stern goto nomem; 752af0bb599SAlan Stern *plink = cpu_to_le32(td->dma_handle); 753af0bb599SAlan Stern 754af0bb599SAlan Stern uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0); 755af0bb599SAlan Stern wmb(); 756af0bb599SAlan Stern qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE); 757af0bb599SAlan Stern qh->dummy_td = td; 758af0bb599SAlan Stern 759af0bb599SAlan Stern usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), 760af0bb599SAlan Stern usb_pipeout(urb->pipe), toggle); 761dccf4a48SAlan Stern return 0; 762af0bb599SAlan Stern 763af0bb599SAlan Stern nomem: 764af0bb599SAlan Stern /* Remove the dummy TD from the td_list so it doesn't get freed */ 76504538a25SAlan Stern uhci_remove_td_from_urbp(qh->dummy_td); 766af0bb599SAlan Stern return -ENOMEM; 7671da177e4SLinus Torvalds } 7681da177e4SLinus Torvalds 769dccf4a48SAlan Stern static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb, 770dccf4a48SAlan Stern struct uhci_qh *qh) 7711da177e4SLinus Torvalds { 7721da177e4SLinus Torvalds int ret; 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds /* Can't have low-speed bulk transfers */ 7751da177e4SLinus Torvalds if (urb->dev->speed == USB_SPEED_LOW) 7761da177e4SLinus Torvalds return -EINVAL; 7771da177e4SLinus Torvalds 778dccf4a48SAlan Stern qh->skel = uhci->skel_bulk_qh; 779dccf4a48SAlan Stern ret = uhci_submit_common(uhci, urb, qh); 780dccf4a48SAlan Stern if (ret == 0) 78184afddd7SAlan Stern uhci_add_fsbr(uhci, urb); 7821da177e4SLinus Torvalds return ret; 7831da177e4SLinus Torvalds } 7841da177e4SLinus Torvalds 785dccf4a48SAlan Stern static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb, 786dccf4a48SAlan Stern struct uhci_qh *qh) 7871da177e4SLinus Torvalds { 788dccf4a48SAlan Stern /* USB 1.1 interrupt transfers only involve one packet per interval. 789dccf4a48SAlan Stern * Drivers can submit URBs of any length, but longer ones will need 790dccf4a48SAlan Stern * multiple intervals to complete. 7911da177e4SLinus Torvalds */ 792dccf4a48SAlan Stern qh->skel = uhci->skelqh[__interval_to_skel(urb->interval)]; 793dccf4a48SAlan Stern return uhci_submit_common(uhci, urb, qh); 7941da177e4SLinus Torvalds } 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds /* 797b1869000SAlan Stern * Fix up the data structures following a short transfer 798b1869000SAlan Stern */ 799b1869000SAlan Stern static int uhci_fixup_short_transfer(struct uhci_hcd *uhci, 80059e29ed9SAlan Stern struct uhci_qh *qh, struct urb_priv *urbp) 801b1869000SAlan Stern { 802b1869000SAlan Stern struct uhci_td *td; 80359e29ed9SAlan Stern struct list_head *tmp; 80459e29ed9SAlan Stern int ret; 805b1869000SAlan Stern 806b1869000SAlan Stern td = list_entry(urbp->td_list.prev, struct uhci_td, list); 807b1869000SAlan Stern if (qh->type == USB_ENDPOINT_XFER_CONTROL) { 808b1869000SAlan Stern 809b1869000SAlan Stern /* When a control transfer is short, we have to restart 810b1869000SAlan Stern * the queue at the status stage transaction, which is 811b1869000SAlan Stern * the last TD. */ 81259e29ed9SAlan Stern WARN_ON(list_empty(&urbp->td_list)); 813b1869000SAlan Stern qh->element = cpu_to_le32(td->dma_handle); 81459e29ed9SAlan Stern tmp = td->list.prev; 815b1869000SAlan Stern ret = -EINPROGRESS; 816b1869000SAlan Stern 81759e29ed9SAlan Stern } else { 818b1869000SAlan Stern 819b1869000SAlan Stern /* When a bulk/interrupt transfer is short, we have to 820b1869000SAlan Stern * fix up the toggles of the following URBs on the queue 821b1869000SAlan Stern * before restarting the queue at the next URB. */ 82259e29ed9SAlan Stern qh->initial_toggle = uhci_toggle(td_token(qh->post_td)) ^ 1; 823b1869000SAlan Stern uhci_fixup_toggles(qh, 1); 824b1869000SAlan Stern 82559e29ed9SAlan Stern if (list_empty(&urbp->td_list)) 82659e29ed9SAlan Stern td = qh->post_td; 827b1869000SAlan Stern qh->element = td->link; 82859e29ed9SAlan Stern tmp = urbp->td_list.prev; 82959e29ed9SAlan Stern ret = 0; 830b1869000SAlan Stern } 831b1869000SAlan Stern 83259e29ed9SAlan Stern /* Remove all the TDs we skipped over, from tmp back to the start */ 83359e29ed9SAlan Stern while (tmp != &urbp->td_list) { 83459e29ed9SAlan Stern td = list_entry(tmp, struct uhci_td, list); 83559e29ed9SAlan Stern tmp = tmp->prev; 83659e29ed9SAlan Stern 83704538a25SAlan Stern uhci_remove_td_from_urbp(td); 83804538a25SAlan Stern uhci_free_td(uhci, td); 83959e29ed9SAlan Stern } 840b1869000SAlan Stern return ret; 841b1869000SAlan Stern } 842b1869000SAlan Stern 843b1869000SAlan Stern /* 844b1869000SAlan Stern * Common result for control, bulk, and interrupt 845b1869000SAlan Stern */ 846b1869000SAlan Stern static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb) 847b1869000SAlan Stern { 848b1869000SAlan Stern struct urb_priv *urbp = urb->hcpriv; 849b1869000SAlan Stern struct uhci_qh *qh = urbp->qh; 85059e29ed9SAlan Stern struct uhci_td *td, *tmp; 851b1869000SAlan Stern unsigned status; 852b1869000SAlan Stern int ret = 0; 853b1869000SAlan Stern 85459e29ed9SAlan Stern list_for_each_entry_safe(td, tmp, &urbp->td_list, list) { 855b1869000SAlan Stern unsigned int ctrlstat; 856b1869000SAlan Stern int len; 857b1869000SAlan Stern 858b1869000SAlan Stern ctrlstat = td_status(td); 859b1869000SAlan Stern status = uhci_status_bits(ctrlstat); 860b1869000SAlan Stern if (status & TD_CTRL_ACTIVE) 861b1869000SAlan Stern return -EINPROGRESS; 862b1869000SAlan Stern 863b1869000SAlan Stern len = uhci_actual_length(ctrlstat); 864b1869000SAlan Stern urb->actual_length += len; 865b1869000SAlan Stern 866b1869000SAlan Stern if (status) { 867b1869000SAlan Stern ret = uhci_map_status(status, 868b1869000SAlan Stern uhci_packetout(td_token(td))); 869b1869000SAlan Stern if ((debug == 1 && ret != -EPIPE) || debug > 1) { 870b1869000SAlan Stern /* Some debugging code */ 871b1869000SAlan Stern dev_dbg(uhci_dev(uhci), 872b1869000SAlan Stern "%s: failed with status %x\n", 873b1869000SAlan Stern __FUNCTION__, status); 874b1869000SAlan Stern 875b1869000SAlan Stern if (debug > 1 && errbuf) { 876b1869000SAlan Stern /* Print the chain for debugging */ 877b1869000SAlan Stern uhci_show_qh(urbp->qh, errbuf, 878b1869000SAlan Stern ERRBUF_LEN, 0); 879b1869000SAlan Stern lprintk(errbuf); 880b1869000SAlan Stern } 881b1869000SAlan Stern } 882b1869000SAlan Stern 883b1869000SAlan Stern } else if (len < uhci_expected_length(td_token(td))) { 884b1869000SAlan Stern 885b1869000SAlan Stern /* We received a short packet */ 886b1869000SAlan Stern if (urb->transfer_flags & URB_SHORT_NOT_OK) 887b1869000SAlan Stern ret = -EREMOTEIO; 888b1869000SAlan Stern else if (ctrlstat & TD_CTRL_SPD) 889b1869000SAlan Stern ret = 1; 890b1869000SAlan Stern } 891b1869000SAlan Stern 89204538a25SAlan Stern uhci_remove_td_from_urbp(td); 89359e29ed9SAlan Stern if (qh->post_td) 89404538a25SAlan Stern uhci_free_td(uhci, qh->post_td); 89559e29ed9SAlan Stern qh->post_td = td; 89659e29ed9SAlan Stern 897b1869000SAlan Stern if (ret != 0) 898b1869000SAlan Stern goto err; 899b1869000SAlan Stern } 900b1869000SAlan Stern return ret; 901b1869000SAlan Stern 902b1869000SAlan Stern err: 903b1869000SAlan Stern if (ret < 0) { 904b1869000SAlan Stern /* In case a control transfer gets an error 905b1869000SAlan Stern * during the setup stage */ 906b1869000SAlan Stern urb->actual_length = max(urb->actual_length, 0); 907b1869000SAlan Stern 908b1869000SAlan Stern /* Note that the queue has stopped and save 909b1869000SAlan Stern * the next toggle value */ 910b1869000SAlan Stern qh->element = UHCI_PTR_TERM; 911b1869000SAlan Stern qh->is_stopped = 1; 912b1869000SAlan Stern qh->needs_fixup = (qh->type != USB_ENDPOINT_XFER_CONTROL); 913b1869000SAlan Stern qh->initial_toggle = uhci_toggle(td_token(td)) ^ 914b1869000SAlan Stern (ret == -EREMOTEIO); 915b1869000SAlan Stern 916b1869000SAlan Stern } else /* Short packet received */ 91759e29ed9SAlan Stern ret = uhci_fixup_short_transfer(uhci, qh, urbp); 918b1869000SAlan Stern return ret; 919b1869000SAlan Stern } 920b1869000SAlan Stern 921b1869000SAlan Stern /* 9221da177e4SLinus Torvalds * Isochronous transfers 9231da177e4SLinus Torvalds */ 924dccf4a48SAlan Stern static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb, 925dccf4a48SAlan Stern struct uhci_qh *qh) 9261da177e4SLinus Torvalds { 927dccf4a48SAlan Stern struct uhci_td *td = NULL; /* Since urb->number_of_packets > 0 */ 9280ed8fee1SAlan Stern int i, frame; 929dccf4a48SAlan Stern unsigned long destination, status; 930b81d3436SAlan Stern struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv; 9311da177e4SLinus Torvalds 9320ed8fee1SAlan Stern if (urb->number_of_packets > 900) /* 900? Why? */ 9330ed8fee1SAlan Stern return -EFBIG; 9340ed8fee1SAlan Stern 9351da177e4SLinus Torvalds status = TD_CTRL_ACTIVE | TD_CTRL_IOS; 9361da177e4SLinus Torvalds destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe); 9371da177e4SLinus Torvalds 9380ed8fee1SAlan Stern /* Figure out the starting frame number */ 9390ed8fee1SAlan Stern if (urb->transfer_flags & URB_ISO_ASAP) { 9400ed8fee1SAlan Stern if (list_empty(&qh->queue)) { 9410ed8fee1SAlan Stern uhci_get_current_frame_number(uhci); 9420ed8fee1SAlan Stern urb->start_frame = (uhci->frame_number + 10); 9430ed8fee1SAlan Stern 9440ed8fee1SAlan Stern } else { /* Go right after the last one */ 9450ed8fee1SAlan Stern struct urb *last_urb; 9460ed8fee1SAlan Stern 9470ed8fee1SAlan Stern last_urb = list_entry(qh->queue.prev, 9480ed8fee1SAlan Stern struct urb_priv, node)->urb; 9490ed8fee1SAlan Stern urb->start_frame = (last_urb->start_frame + 9500ed8fee1SAlan Stern last_urb->number_of_packets * 9510ed8fee1SAlan Stern last_urb->interval); 9520ed8fee1SAlan Stern } 9530ed8fee1SAlan Stern } else { 9540ed8fee1SAlan Stern /* FIXME: Sanity check */ 9550ed8fee1SAlan Stern } 9560ed8fee1SAlan Stern urb->start_frame &= (UHCI_NUMFRAMES - 1); 9571da177e4SLinus Torvalds 958b81d3436SAlan Stern for (i = 0; i < urb->number_of_packets; i++) { 9592532178aSAlan Stern td = uhci_alloc_td(uhci); 9601da177e4SLinus Torvalds if (!td) 9611da177e4SLinus Torvalds return -ENOMEM; 9621da177e4SLinus Torvalds 96304538a25SAlan Stern uhci_add_td_to_urbp(td, urbp); 964dccf4a48SAlan Stern uhci_fill_td(td, status, destination | 965dccf4a48SAlan Stern uhci_explen(urb->iso_frame_desc[i].length), 966dccf4a48SAlan Stern urb->transfer_dma + 967dccf4a48SAlan Stern urb->iso_frame_desc[i].offset); 968b81d3436SAlan Stern } 9691da177e4SLinus Torvalds 970dccf4a48SAlan Stern /* Set the interrupt-on-completion flag on the last packet. */ 971dccf4a48SAlan Stern td->status |= __constant_cpu_to_le32(TD_CTRL_IOC); 972dccf4a48SAlan Stern 973dccf4a48SAlan Stern qh->skel = uhci->skel_iso_qh; 974dccf4a48SAlan Stern 975dccf4a48SAlan Stern /* Add the TDs to the frame list */ 976b81d3436SAlan Stern frame = urb->start_frame; 977b81d3436SAlan Stern list_for_each_entry(td, &urbp->td_list, list) { 978dccf4a48SAlan Stern uhci_insert_td_in_frame_list(uhci, td, frame); 979b81d3436SAlan Stern frame += urb->interval; 9801da177e4SLinus Torvalds } 9811da177e4SLinus Torvalds 982dccf4a48SAlan Stern return 0; 9831da177e4SLinus Torvalds } 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb) 9861da177e4SLinus Torvalds { 9871da177e4SLinus Torvalds struct uhci_td *td; 9881da177e4SLinus Torvalds struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 9891da177e4SLinus Torvalds int status; 9901da177e4SLinus Torvalds int i, ret = 0; 9911da177e4SLinus Torvalds 992b81d3436SAlan Stern urb->actual_length = urb->error_count = 0; 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds i = 0; 9951da177e4SLinus Torvalds list_for_each_entry(td, &urbp->td_list, list) { 9961da177e4SLinus Torvalds int actlength; 9971da177e4SLinus Torvalds unsigned int ctrlstat = td_status(td); 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds if (ctrlstat & TD_CTRL_ACTIVE) 10001da177e4SLinus Torvalds return -EINPROGRESS; 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds actlength = uhci_actual_length(ctrlstat); 10031da177e4SLinus Torvalds urb->iso_frame_desc[i].actual_length = actlength; 10041da177e4SLinus Torvalds urb->actual_length += actlength; 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds status = uhci_map_status(uhci_status_bits(ctrlstat), 10071da177e4SLinus Torvalds usb_pipeout(urb->pipe)); 10081da177e4SLinus Torvalds urb->iso_frame_desc[i].status = status; 10091da177e4SLinus Torvalds if (status) { 10101da177e4SLinus Torvalds urb->error_count++; 10111da177e4SLinus Torvalds ret = status; 10121da177e4SLinus Torvalds } 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds i++; 10151da177e4SLinus Torvalds } 10161da177e4SLinus Torvalds 10171da177e4SLinus Torvalds return ret; 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds static int uhci_urb_enqueue(struct usb_hcd *hcd, 1021dccf4a48SAlan Stern struct usb_host_endpoint *hep, 102255016f10SAl Viro struct urb *urb, gfp_t mem_flags) 10231da177e4SLinus Torvalds { 10241da177e4SLinus Torvalds int ret; 10251da177e4SLinus Torvalds struct uhci_hcd *uhci = hcd_to_uhci(hcd); 10261da177e4SLinus Torvalds unsigned long flags; 1027dccf4a48SAlan Stern struct urb_priv *urbp; 1028dccf4a48SAlan Stern struct uhci_qh *qh; 10291da177e4SLinus Torvalds int bustime; 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds spin_lock_irqsave(&uhci->lock, flags); 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds ret = urb->status; 10341da177e4SLinus Torvalds if (ret != -EINPROGRESS) /* URB already unlinked! */ 1035dccf4a48SAlan Stern goto done; 10361da177e4SLinus Torvalds 10371da177e4SLinus Torvalds ret = -ENOMEM; 1038dccf4a48SAlan Stern urbp = uhci_alloc_urb_priv(uhci, urb); 1039dccf4a48SAlan Stern if (!urbp) 1040dccf4a48SAlan Stern goto done; 1041dccf4a48SAlan Stern 1042dccf4a48SAlan Stern if (hep->hcpriv) 1043dccf4a48SAlan Stern qh = (struct uhci_qh *) hep->hcpriv; 1044dccf4a48SAlan Stern else { 1045dccf4a48SAlan Stern qh = uhci_alloc_qh(uhci, urb->dev, hep); 1046dccf4a48SAlan Stern if (!qh) 1047dccf4a48SAlan Stern goto err_no_qh; 10481da177e4SLinus Torvalds } 1049dccf4a48SAlan Stern urbp->qh = qh; 10501da177e4SLinus Torvalds 10514de7d2c2SAlan Stern switch (qh->type) { 10524de7d2c2SAlan Stern case USB_ENDPOINT_XFER_CONTROL: 1053dccf4a48SAlan Stern ret = uhci_submit_control(uhci, urb, qh); 1054dccf4a48SAlan Stern break; 10554de7d2c2SAlan Stern case USB_ENDPOINT_XFER_BULK: 1056dccf4a48SAlan Stern ret = uhci_submit_bulk(uhci, urb, qh); 10571da177e4SLinus Torvalds break; 10584de7d2c2SAlan Stern case USB_ENDPOINT_XFER_INT: 1059dccf4a48SAlan Stern if (list_empty(&qh->queue)) { 10601da177e4SLinus Torvalds bustime = usb_check_bandwidth(urb->dev, urb); 10611da177e4SLinus Torvalds if (bustime < 0) 10621da177e4SLinus Torvalds ret = bustime; 10631da177e4SLinus Torvalds else { 1064dccf4a48SAlan Stern ret = uhci_submit_interrupt(uhci, urb, qh); 1065dccf4a48SAlan Stern if (ret == 0) 10661da177e4SLinus Torvalds usb_claim_bandwidth(urb->dev, urb, bustime, 0); 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds } else { /* inherit from parent */ 1069dccf4a48SAlan Stern struct urb_priv *eurbp; 1070dccf4a48SAlan Stern 1071dccf4a48SAlan Stern eurbp = list_entry(qh->queue.prev, struct urb_priv, 1072dccf4a48SAlan Stern node); 1073dccf4a48SAlan Stern urb->bandwidth = eurbp->urb->bandwidth; 1074dccf4a48SAlan Stern ret = uhci_submit_interrupt(uhci, urb, qh); 10751da177e4SLinus Torvalds } 10761da177e4SLinus Torvalds break; 10774de7d2c2SAlan Stern case USB_ENDPOINT_XFER_ISOC: 10781da177e4SLinus Torvalds bustime = usb_check_bandwidth(urb->dev, urb); 10791da177e4SLinus Torvalds if (bustime < 0) { 10801da177e4SLinus Torvalds ret = bustime; 10811da177e4SLinus Torvalds break; 10821da177e4SLinus Torvalds } 10831da177e4SLinus Torvalds 1084dccf4a48SAlan Stern ret = uhci_submit_isochronous(uhci, urb, qh); 1085dccf4a48SAlan Stern if (ret == 0) 10861da177e4SLinus Torvalds usb_claim_bandwidth(urb->dev, urb, bustime, 1); 10871da177e4SLinus Torvalds break; 10881da177e4SLinus Torvalds } 1089dccf4a48SAlan Stern if (ret != 0) 1090dccf4a48SAlan Stern goto err_submit_failed; 10911da177e4SLinus Torvalds 1092dccf4a48SAlan Stern /* Add this URB to the QH */ 1093dccf4a48SAlan Stern urbp->qh = qh; 1094dccf4a48SAlan Stern list_add_tail(&urbp->node, &qh->queue); 10951da177e4SLinus Torvalds 1096dccf4a48SAlan Stern /* If the new URB is the first and only one on this QH then either 1097dccf4a48SAlan Stern * the QH is new and idle or else it's unlinked and waiting to 10982775562aSAlan Stern * become idle, so we can activate it right away. But only if the 10992775562aSAlan Stern * queue isn't stopped. */ 110084afddd7SAlan Stern if (qh->queue.next == &urbp->node && !qh->is_stopped) { 1101dccf4a48SAlan Stern uhci_activate_qh(uhci, qh); 110284afddd7SAlan Stern uhci_qh_wants_fsbr(uhci, qh); 110384afddd7SAlan Stern } 1104dccf4a48SAlan Stern goto done; 1105dccf4a48SAlan Stern 1106dccf4a48SAlan Stern err_submit_failed: 1107dccf4a48SAlan Stern if (qh->state == QH_STATE_IDLE) 1108dccf4a48SAlan Stern uhci_make_qh_idle(uhci, qh); /* Reclaim unused QH */ 1109dccf4a48SAlan Stern 1110dccf4a48SAlan Stern err_no_qh: 1111dccf4a48SAlan Stern uhci_free_urb_priv(uhci, urbp); 1112dccf4a48SAlan Stern 1113dccf4a48SAlan Stern done: 11141da177e4SLinus Torvalds spin_unlock_irqrestore(&uhci->lock, flags); 11151da177e4SLinus Torvalds return ret; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb) 11191da177e4SLinus Torvalds { 11201da177e4SLinus Torvalds struct uhci_hcd *uhci = hcd_to_uhci(hcd); 11211da177e4SLinus Torvalds unsigned long flags; 11221da177e4SLinus Torvalds struct urb_priv *urbp; 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds spin_lock_irqsave(&uhci->lock, flags); 11251da177e4SLinus Torvalds urbp = urb->hcpriv; 11261da177e4SLinus Torvalds if (!urbp) /* URB was never linked! */ 11271da177e4SLinus Torvalds goto done; 11281da177e4SLinus Torvalds 1129dccf4a48SAlan Stern /* Remove Isochronous TDs from the frame list ASAP */ 11304de7d2c2SAlan Stern if (urbp->qh->type == USB_ENDPOINT_XFER_ISOC) 1131dccf4a48SAlan Stern uhci_unlink_isochronous_tds(uhci, urb); 1132dccf4a48SAlan Stern uhci_unlink_qh(uhci, urbp->qh); 11331da177e4SLinus Torvalds 11341da177e4SLinus Torvalds done: 11351da177e4SLinus Torvalds spin_unlock_irqrestore(&uhci->lock, flags); 11361da177e4SLinus Torvalds return 0; 11371da177e4SLinus Torvalds } 11381da177e4SLinus Torvalds 11390ed8fee1SAlan Stern /* 11400ed8fee1SAlan Stern * Finish unlinking an URB and give it back 11410ed8fee1SAlan Stern */ 11420ed8fee1SAlan Stern static void uhci_giveback_urb(struct uhci_hcd *uhci, struct uhci_qh *qh, 11430ed8fee1SAlan Stern struct urb *urb, struct pt_regs *regs) 11440ed8fee1SAlan Stern __releases(uhci->lock) 11450ed8fee1SAlan Stern __acquires(uhci->lock) 11461da177e4SLinus Torvalds { 11471da177e4SLinus Torvalds struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv; 11481da177e4SLinus Torvalds 11490ed8fee1SAlan Stern /* Isochronous TDs get unlinked directly from the frame list */ 11504de7d2c2SAlan Stern if (qh->type == USB_ENDPOINT_XFER_ISOC) 11510ed8fee1SAlan Stern uhci_unlink_isochronous_tds(uhci, urb); 11521da177e4SLinus Torvalds 11530ed8fee1SAlan Stern /* Take the URB off the QH's queue. If the queue is now empty, 11540ed8fee1SAlan Stern * this is a perfect time for a toggle fixup. */ 11550ed8fee1SAlan Stern list_del_init(&urbp->node); 11560ed8fee1SAlan Stern if (list_empty(&qh->queue) && qh->needs_fixup) { 11570ed8fee1SAlan Stern usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), 11580ed8fee1SAlan Stern usb_pipeout(urb->pipe), qh->initial_toggle); 11590ed8fee1SAlan Stern qh->needs_fixup = 0; 11600ed8fee1SAlan Stern } 11610ed8fee1SAlan Stern 11620ed8fee1SAlan Stern uhci_free_urb_priv(uhci, urbp); 11630ed8fee1SAlan Stern 11644de7d2c2SAlan Stern switch (qh->type) { 11654de7d2c2SAlan Stern case USB_ENDPOINT_XFER_ISOC: 11660ed8fee1SAlan Stern /* Release bandwidth for Interrupt or Isoc. transfers */ 11670ed8fee1SAlan Stern if (urb->bandwidth) 11680ed8fee1SAlan Stern usb_release_bandwidth(urb->dev, urb, 1); 11690ed8fee1SAlan Stern break; 11704de7d2c2SAlan Stern case USB_ENDPOINT_XFER_INT: 11710ed8fee1SAlan Stern /* Release bandwidth for Interrupt or Isoc. transfers */ 11720ed8fee1SAlan Stern /* Make sure we don't release if we have a queued URB */ 11730ed8fee1SAlan Stern if (list_empty(&qh->queue) && urb->bandwidth) 11740ed8fee1SAlan Stern usb_release_bandwidth(urb->dev, urb, 0); 11750ed8fee1SAlan Stern else 11760ed8fee1SAlan Stern /* bandwidth was passed on to queued URB, */ 11770ed8fee1SAlan Stern /* so don't let usb_unlink_urb() release it */ 11780ed8fee1SAlan Stern urb->bandwidth = 0; 11790ed8fee1SAlan Stern break; 11800ed8fee1SAlan Stern } 11810ed8fee1SAlan Stern 11820ed8fee1SAlan Stern spin_unlock(&uhci->lock); 11830ed8fee1SAlan Stern usb_hcd_giveback_urb(uhci_to_hcd(uhci), urb, regs); 11840ed8fee1SAlan Stern spin_lock(&uhci->lock); 11850ed8fee1SAlan Stern 11860ed8fee1SAlan Stern /* If the queue is now empty, we can unlink the QH and give up its 11870ed8fee1SAlan Stern * reserved bandwidth. */ 11880ed8fee1SAlan Stern if (list_empty(&qh->queue)) { 11890ed8fee1SAlan Stern uhci_unlink_qh(uhci, qh); 11900ed8fee1SAlan Stern 11910ed8fee1SAlan Stern /* Bandwidth stuff not yet implemented */ 11920ed8fee1SAlan Stern } 11930ed8fee1SAlan Stern } 11940ed8fee1SAlan Stern 11950ed8fee1SAlan Stern /* 11960ed8fee1SAlan Stern * Scan the URBs in a QH's queue 11970ed8fee1SAlan Stern */ 11980ed8fee1SAlan Stern #define QH_FINISHED_UNLINKING(qh) \ 11990ed8fee1SAlan Stern (qh->state == QH_STATE_UNLINKING && \ 12000ed8fee1SAlan Stern uhci->frame_number + uhci->is_stopped != qh->unlink_frame) 12010ed8fee1SAlan Stern 12020ed8fee1SAlan Stern static void uhci_scan_qh(struct uhci_hcd *uhci, struct uhci_qh *qh, 12030ed8fee1SAlan Stern struct pt_regs *regs) 12040ed8fee1SAlan Stern { 12050ed8fee1SAlan Stern struct urb_priv *urbp; 12060ed8fee1SAlan Stern struct urb *urb; 12070ed8fee1SAlan Stern int status; 12080ed8fee1SAlan Stern 12090ed8fee1SAlan Stern while (!list_empty(&qh->queue)) { 12100ed8fee1SAlan Stern urbp = list_entry(qh->queue.next, struct urb_priv, node); 12110ed8fee1SAlan Stern urb = urbp->urb; 12120ed8fee1SAlan Stern 1213b1869000SAlan Stern if (qh->type == USB_ENDPOINT_XFER_ISOC) 12140ed8fee1SAlan Stern status = uhci_result_isochronous(uhci, urb); 1215b1869000SAlan Stern else 12160ed8fee1SAlan Stern status = uhci_result_common(uhci, urb); 12170ed8fee1SAlan Stern if (status == -EINPROGRESS) 12180ed8fee1SAlan Stern break; 12190ed8fee1SAlan Stern 12200ed8fee1SAlan Stern spin_lock(&urb->lock); 12210ed8fee1SAlan Stern if (urb->status == -EINPROGRESS) /* Not dequeued */ 12220ed8fee1SAlan Stern urb->status = status; 12230ed8fee1SAlan Stern else 12242775562aSAlan Stern status = ECONNRESET; /* Not -ECONNRESET */ 12250ed8fee1SAlan Stern spin_unlock(&urb->lock); 12260ed8fee1SAlan Stern 12270ed8fee1SAlan Stern /* Dequeued but completed URBs can't be given back unless 12280ed8fee1SAlan Stern * the QH is stopped or has finished unlinking. */ 12292775562aSAlan Stern if (status == ECONNRESET) { 12302775562aSAlan Stern if (QH_FINISHED_UNLINKING(qh)) 12312775562aSAlan Stern qh->is_stopped = 1; 12322775562aSAlan Stern else if (!qh->is_stopped) 12330ed8fee1SAlan Stern return; 12342775562aSAlan Stern } 12350ed8fee1SAlan Stern 12360ed8fee1SAlan Stern uhci_giveback_urb(uhci, qh, urb, regs); 12372775562aSAlan Stern if (status < 0) 12380ed8fee1SAlan Stern break; 12390ed8fee1SAlan Stern } 12400ed8fee1SAlan Stern 12410ed8fee1SAlan Stern /* If the QH is neither stopped nor finished unlinking (normal case), 12420ed8fee1SAlan Stern * our work here is done. */ 12432775562aSAlan Stern if (QH_FINISHED_UNLINKING(qh)) 12442775562aSAlan Stern qh->is_stopped = 1; 12452775562aSAlan Stern else if (!qh->is_stopped) 12460ed8fee1SAlan Stern return; 12470ed8fee1SAlan Stern 12480ed8fee1SAlan Stern /* Otherwise give back each of the dequeued URBs */ 12492775562aSAlan Stern restart: 12500ed8fee1SAlan Stern list_for_each_entry(urbp, &qh->queue, node) { 12510ed8fee1SAlan Stern urb = urbp->urb; 12520ed8fee1SAlan Stern if (urb->status != -EINPROGRESS) { 1253a0b458b6SAlan Stern uhci_cleanup_queue(qh, urb); 12540ed8fee1SAlan Stern uhci_giveback_urb(uhci, qh, urb, regs); 12550ed8fee1SAlan Stern goto restart; 12560ed8fee1SAlan Stern } 12570ed8fee1SAlan Stern } 12580ed8fee1SAlan Stern qh->is_stopped = 0; 12590ed8fee1SAlan Stern 12600ed8fee1SAlan Stern /* There are no more dequeued URBs. If there are still URBs on the 12610ed8fee1SAlan Stern * queue, the QH can now be re-activated. */ 12620ed8fee1SAlan Stern if (!list_empty(&qh->queue)) { 12630ed8fee1SAlan Stern if (qh->needs_fixup) 12640ed8fee1SAlan Stern uhci_fixup_toggles(qh, 0); 126584afddd7SAlan Stern 126684afddd7SAlan Stern /* If the first URB on the queue wants FSBR but its time 126784afddd7SAlan Stern * limit has expired, set the next TD to interrupt on 126884afddd7SAlan Stern * completion before reactivating the QH. */ 126984afddd7SAlan Stern urbp = list_entry(qh->queue.next, struct urb_priv, node); 127084afddd7SAlan Stern if (urbp->fsbr && qh->wait_expired) { 127184afddd7SAlan Stern struct uhci_td *td = list_entry(urbp->td_list.next, 127284afddd7SAlan Stern struct uhci_td, list); 127384afddd7SAlan Stern 127484afddd7SAlan Stern td->status |= __cpu_to_le32(TD_CTRL_IOC); 127584afddd7SAlan Stern } 127684afddd7SAlan Stern 12770ed8fee1SAlan Stern uhci_activate_qh(uhci, qh); 12780ed8fee1SAlan Stern } 12790ed8fee1SAlan Stern 12800ed8fee1SAlan Stern /* The queue is empty. The QH can become idle if it is fully 12810ed8fee1SAlan Stern * unlinked. */ 12820ed8fee1SAlan Stern else if (QH_FINISHED_UNLINKING(qh)) 12830ed8fee1SAlan Stern uhci_make_qh_idle(uhci, qh); 12841da177e4SLinus Torvalds } 12851da177e4SLinus Torvalds 12860ed8fee1SAlan Stern /* 128784afddd7SAlan Stern * Check for queues that have made some forward progress. 128884afddd7SAlan Stern * Returns 0 if the queue is not Isochronous, is ACTIVE, and 128984afddd7SAlan Stern * has not advanced since last examined; 1 otherwise. 1290*b761d9d8SAlan Stern * 1291*b761d9d8SAlan Stern * Early Intel controllers have a bug which causes qh->element sometimes 1292*b761d9d8SAlan Stern * not to advance when a TD completes successfully. The queue remains 1293*b761d9d8SAlan Stern * stuck on the inactive completed TD. We detect such cases and advance 1294*b761d9d8SAlan Stern * the element pointer by hand. 129584afddd7SAlan Stern */ 129684afddd7SAlan Stern static int uhci_advance_check(struct uhci_hcd *uhci, struct uhci_qh *qh) 129784afddd7SAlan Stern { 129884afddd7SAlan Stern struct urb_priv *urbp = NULL; 129984afddd7SAlan Stern struct uhci_td *td; 130084afddd7SAlan Stern int ret = 1; 130184afddd7SAlan Stern unsigned status; 130284afddd7SAlan Stern 130384afddd7SAlan Stern if (qh->type == USB_ENDPOINT_XFER_ISOC) 130484afddd7SAlan Stern return ret; 130584afddd7SAlan Stern 130684afddd7SAlan Stern /* Treat an UNLINKING queue as though it hasn't advanced. 130784afddd7SAlan Stern * This is okay because reactivation will treat it as though 130884afddd7SAlan Stern * it has advanced, and if it is going to become IDLE then 130984afddd7SAlan Stern * this doesn't matter anyway. Furthermore it's possible 131084afddd7SAlan Stern * for an UNLINKING queue not to have any URBs at all, or 131184afddd7SAlan Stern * for its first URB not to have any TDs (if it was dequeued 131284afddd7SAlan Stern * just as it completed). So it's not easy in any case to 131384afddd7SAlan Stern * test whether such queues have advanced. */ 131484afddd7SAlan Stern if (qh->state != QH_STATE_ACTIVE) { 131584afddd7SAlan Stern urbp = NULL; 131684afddd7SAlan Stern status = 0; 131784afddd7SAlan Stern 131884afddd7SAlan Stern } else { 131984afddd7SAlan Stern urbp = list_entry(qh->queue.next, struct urb_priv, node); 132084afddd7SAlan Stern td = list_entry(urbp->td_list.next, struct uhci_td, list); 132184afddd7SAlan Stern status = td_status(td); 132284afddd7SAlan Stern if (!(status & TD_CTRL_ACTIVE)) { 132384afddd7SAlan Stern 132484afddd7SAlan Stern /* We're okay, the queue has advanced */ 132584afddd7SAlan Stern qh->wait_expired = 0; 132684afddd7SAlan Stern qh->advance_jiffies = jiffies; 132784afddd7SAlan Stern return ret; 132884afddd7SAlan Stern } 132984afddd7SAlan Stern ret = 0; 133084afddd7SAlan Stern } 133184afddd7SAlan Stern 133284afddd7SAlan Stern /* The queue hasn't advanced; check for timeout */ 133384afddd7SAlan Stern if (!qh->wait_expired && time_after(jiffies, 133484afddd7SAlan Stern qh->advance_jiffies + QH_WAIT_TIMEOUT)) { 1335*b761d9d8SAlan Stern 1336*b761d9d8SAlan Stern /* Detect the Intel bug and work around it */ 1337*b761d9d8SAlan Stern if (qh->post_td && qh_element(qh) == 1338*b761d9d8SAlan Stern cpu_to_le32(qh->post_td->dma_handle)) { 1339*b761d9d8SAlan Stern qh->element = qh->post_td->link; 1340*b761d9d8SAlan Stern qh->advance_jiffies = jiffies; 1341*b761d9d8SAlan Stern return 1; 1342*b761d9d8SAlan Stern } 1343*b761d9d8SAlan Stern 134484afddd7SAlan Stern qh->wait_expired = 1; 134584afddd7SAlan Stern 134684afddd7SAlan Stern /* If the current URB wants FSBR, unlink it temporarily 134784afddd7SAlan Stern * so that we can safely set the next TD to interrupt on 134884afddd7SAlan Stern * completion. That way we'll know as soon as the queue 134984afddd7SAlan Stern * starts moving again. */ 135084afddd7SAlan Stern if (urbp && urbp->fsbr && !(status & TD_CTRL_IOC)) 135184afddd7SAlan Stern uhci_unlink_qh(uhci, qh); 135284afddd7SAlan Stern } 135384afddd7SAlan Stern return ret; 135484afddd7SAlan Stern } 135584afddd7SAlan Stern 135684afddd7SAlan Stern /* 13570ed8fee1SAlan Stern * Process events in the schedule, but only in one thread at a time 13580ed8fee1SAlan Stern */ 13591da177e4SLinus Torvalds static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs) 13601da177e4SLinus Torvalds { 13610ed8fee1SAlan Stern int i; 13620ed8fee1SAlan Stern struct uhci_qh *qh; 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds /* Don't allow re-entrant calls */ 13651da177e4SLinus Torvalds if (uhci->scan_in_progress) { 13661da177e4SLinus Torvalds uhci->need_rescan = 1; 13671da177e4SLinus Torvalds return; 13681da177e4SLinus Torvalds } 13691da177e4SLinus Torvalds uhci->scan_in_progress = 1; 13701da177e4SLinus Torvalds rescan: 13711da177e4SLinus Torvalds uhci->need_rescan = 0; 13721da177e4SLinus Torvalds 13736c1b445cSAlan Stern uhci_clear_next_interrupt(uhci); 13741da177e4SLinus Torvalds uhci_get_current_frame_number(uhci); 13751da177e4SLinus Torvalds 13760ed8fee1SAlan Stern /* Go through all the QH queues and process the URBs in each one */ 13770ed8fee1SAlan Stern for (i = 0; i < UHCI_NUM_SKELQH - 1; ++i) { 13780ed8fee1SAlan Stern uhci->next_qh = list_entry(uhci->skelqh[i]->node.next, 13790ed8fee1SAlan Stern struct uhci_qh, node); 13800ed8fee1SAlan Stern while ((qh = uhci->next_qh) != uhci->skelqh[i]) { 13810ed8fee1SAlan Stern uhci->next_qh = list_entry(qh->node.next, 13820ed8fee1SAlan Stern struct uhci_qh, node); 138384afddd7SAlan Stern 138484afddd7SAlan Stern if (uhci_advance_check(uhci, qh)) { 13850ed8fee1SAlan Stern uhci_scan_qh(uhci, qh, regs); 138684afddd7SAlan Stern if (qh->state == QH_STATE_ACTIVE) 138784afddd7SAlan Stern uhci_qh_wants_fsbr(uhci, qh); 138884afddd7SAlan Stern } 13891da177e4SLinus Torvalds } 13900ed8fee1SAlan Stern } 13911da177e4SLinus Torvalds 13921da177e4SLinus Torvalds if (uhci->need_rescan) 13931da177e4SLinus Torvalds goto rescan; 13941da177e4SLinus Torvalds uhci->scan_in_progress = 0; 13951da177e4SLinus Torvalds 139684afddd7SAlan Stern if (uhci->fsbr_is_on && time_after(jiffies, 139784afddd7SAlan Stern uhci->fsbr_jiffies + FSBR_OFF_DELAY)) 139884afddd7SAlan Stern uhci_fsbr_off(uhci); 139984afddd7SAlan Stern 140004538a25SAlan Stern if (list_empty(&uhci->skel_unlink_qh->node)) 14011da177e4SLinus Torvalds uhci_clear_next_interrupt(uhci); 14021da177e4SLinus Torvalds else 14031da177e4SLinus Torvalds uhci_set_next_interrupt(uhci); 14041da177e4SLinus Torvalds } 1405