📄 uhci-q.c
字号:
/* * Universal Host Controller Interface driver for USB. * * Maintainer: Alan Stern <stern@rowland.harvard.edu> * * (C) Copyright 1999 Linus Torvalds * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com * (C) Copyright 1999 Randy Dunlap * (C) Copyright 1999 Georg Acher, acher@in.tum.de * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com). * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c) * (C) Copyright 2004-2005 Alan Stern, stern@rowland.harvard.edu */static void uhci_free_pending_tds(struct uhci_hcd *uhci);/* * Technically, updating td->status here is a race, but it's not really a * problem. The worst that can happen is that we set the IOC bit again * generating a spurious interrupt. We could fix this by creating another * QH and leaving the IOC bit always set, but then we would have to play * games with the FSBR code to make sure we get the correct order in all * the cases. I don't think it's worth the effort */static void uhci_set_next_interrupt(struct uhci_hcd *uhci){ if (uhci->is_stopped) mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies); uhci->term_td->status |= cpu_to_le32(TD_CTRL_IOC); }static inline void uhci_clear_next_interrupt(struct uhci_hcd *uhci){ uhci->term_td->status &= ~cpu_to_le32(TD_CTRL_IOC);}static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci){ dma_addr_t dma_handle; struct uhci_td *td; td = dma_pool_alloc(uhci->td_pool, GFP_ATOMIC, &dma_handle); if (!td) return NULL; td->dma_handle = dma_handle; td->frame = -1; INIT_LIST_HEAD(&td->list); INIT_LIST_HEAD(&td->remove_list); INIT_LIST_HEAD(&td->fl_list); return td;}static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td){ if (!list_empty(&td->list)) dev_warn(uhci_dev(uhci), "td %p still in list!\n", td); if (!list_empty(&td->remove_list)) dev_warn(uhci_dev(uhci), "td %p still in remove_list!\n", td); if (!list_empty(&td->fl_list)) dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td); dma_pool_free(uhci->td_pool, td, td->dma_handle);}static inline void uhci_fill_td(struct uhci_td *td, u32 status, u32 token, u32 buffer){ td->status = cpu_to_le32(status); td->token = cpu_to_le32(token); td->buffer = cpu_to_le32(buffer);}/* * We insert Isochronous URBs directly into the frame list at the beginning */static inline void uhci_insert_td_in_frame_list(struct uhci_hcd *uhci, struct uhci_td *td, unsigned framenum){ framenum &= (UHCI_NUMFRAMES - 1); td->frame = framenum; /* Is there a TD already mapped there? */ if (uhci->frame_cpu[framenum]) { struct uhci_td *ftd, *ltd; ftd = uhci->frame_cpu[framenum]; ltd = list_entry(ftd->fl_list.prev, struct uhci_td, fl_list); list_add_tail(&td->fl_list, &ftd->fl_list); td->link = ltd->link; wmb(); ltd->link = cpu_to_le32(td->dma_handle); } else { td->link = uhci->frame[framenum]; wmb(); uhci->frame[framenum] = cpu_to_le32(td->dma_handle); uhci->frame_cpu[framenum] = td; }}static inline void uhci_remove_td_from_frame_list(struct uhci_hcd *uhci, struct uhci_td *td){ /* If it's not inserted, don't remove it */ if (td->frame == -1) { WARN_ON(!list_empty(&td->fl_list)); return; } if (uhci->frame_cpu[td->frame] == td) { if (list_empty(&td->fl_list)) { uhci->frame[td->frame] = td->link; uhci->frame_cpu[td->frame] = NULL; } else { struct uhci_td *ntd; ntd = list_entry(td->fl_list.next, struct uhci_td, fl_list); uhci->frame[td->frame] = cpu_to_le32(ntd->dma_handle); uhci->frame_cpu[td->frame] = ntd; } } else { struct uhci_td *ptd; ptd = list_entry(td->fl_list.prev, struct uhci_td, fl_list); ptd->link = td->link; } list_del_init(&td->fl_list); td->frame = -1;}/* * Remove all the TDs for an Isochronous URB from the frame list */static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb){ struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv; struct uhci_td *td; list_for_each_entry(td, &urbp->td_list, list) uhci_remove_td_from_frame_list(uhci, td); wmb();}static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci, struct usb_device *udev, struct usb_host_endpoint *hep){ dma_addr_t dma_handle; struct uhci_qh *qh; qh = dma_pool_alloc(uhci->qh_pool, GFP_ATOMIC, &dma_handle); if (!qh) return NULL; qh->dma_handle = dma_handle; qh->element = UHCI_PTR_TERM; qh->link = UHCI_PTR_TERM; INIT_LIST_HEAD(&qh->queue); INIT_LIST_HEAD(&qh->node); if (udev) { /* Normal QH */ qh->dummy_td = uhci_alloc_td(uhci); if (!qh->dummy_td) { dma_pool_free(uhci->qh_pool, qh, dma_handle); return NULL; } qh->state = QH_STATE_IDLE; qh->hep = hep; qh->udev = udev; hep->hcpriv = qh; } else { /* Skeleton QH */ qh->state = QH_STATE_ACTIVE; qh->udev = NULL; } return qh;}static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh){ WARN_ON(qh->state != QH_STATE_IDLE && qh->udev); if (!list_empty(&qh->queue)) dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh); list_del(&qh->node); if (qh->udev) { qh->hep->hcpriv = NULL; uhci_free_td(uhci, qh->dummy_td); } dma_pool_free(uhci->qh_pool, qh, qh->dma_handle);}/* * When the currently executing URB is dequeued, save its current toggle value */static void uhci_save_toggle(struct uhci_qh *qh, struct urb *urb){ struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv; struct uhci_td *td; /* If the QH element pointer is UHCI_PTR_TERM then then currently * executing URB has already been unlinked, so this one isn't it. */ if (qh_element(qh) == UHCI_PTR_TERM || qh->queue.next != &urbp->node) return; qh->element = UHCI_PTR_TERM; /* Only bulk and interrupt pipes have to worry about toggles */ if (!(usb_pipetype(urb->pipe) == PIPE_BULK || usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) return; /* Find the first active TD; that's the device's toggle state */ list_for_each_entry(td, &urbp->td_list, list) { if (td_status(td) & TD_CTRL_ACTIVE) { qh->needs_fixup = 1; qh->initial_toggle = uhci_toggle(td_token(td)); return; } } WARN_ON(1);}/* * Fix up the data toggles for URBs in a queue, when one of them * terminates early (short transfer, error, or dequeued). */static void uhci_fixup_toggles(struct uhci_qh *qh, int skip_first){ struct urb_priv *urbp = NULL; struct uhci_td *td; unsigned int toggle = qh->initial_toggle; unsigned int pipe; /* Fixups for a short transfer start with the second URB in the * queue (the short URB is the first). */ if (skip_first) urbp = list_entry(qh->queue.next, struct urb_priv, node); /* When starting with the first URB, if the QH element pointer is * still valid then we know the URB's toggles are okay. */ else if (qh_element(qh) != UHCI_PTR_TERM) toggle = 2; /* Fix up the toggle for the URBs in the queue. Normally this * loop won't run more than once: When an error or short transfer * occurs, the queue usually gets emptied. */ urbp = list_prepare_entry(urbp, &qh->queue, node); list_for_each_entry_continue(urbp, &qh->queue, node) { /* If the first TD has the right toggle value, we don't * need to change any toggles in this URB */ td = list_entry(urbp->td_list.next, struct uhci_td, list); if (toggle > 1 || uhci_toggle(td_token(td)) == toggle) { td = list_entry(urbp->td_list.prev, struct uhci_td, list); toggle = uhci_toggle(td_token(td)) ^ 1; /* Otherwise all the toggles in the URB have to be switched */ } else { list_for_each_entry(td, &urbp->td_list, list) { td->token ^= __constant_cpu_to_le32( TD_TOKEN_TOGGLE); toggle ^= 1; } } } wmb(); pipe = list_entry(qh->queue.next, struct urb_priv, node)->urb->pipe; usb_settoggle(qh->udev, usb_pipeendpoint(pipe), usb_pipeout(pipe), toggle); qh->needs_fixup = 0;}/* * Put a QH on the schedule in both hardware and software */static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh){ struct uhci_qh *pqh; WARN_ON(list_empty(&qh->queue)); /* Set the element pointer if it isn't set already. * This isn't needed for Isochronous queues, but it doesn't hurt. */ if (qh_element(qh) == UHCI_PTR_TERM) { struct urb_priv *urbp = list_entry(qh->queue.next, struct urb_priv, node); struct uhci_td *td = list_entry(urbp->td_list.next, struct uhci_td, list); qh->element = cpu_to_le32(td->dma_handle); } if (qh->state == QH_STATE_ACTIVE) return; qh->state = QH_STATE_ACTIVE; /* Move the QH from its old list to the end of the appropriate * skeleton's list */ if (qh == uhci->next_qh) uhci->next_qh = list_entry(qh->node.next, struct uhci_qh, node); list_move_tail(&qh->node, &qh->skel->node); /* Link it into the schedule */ pqh = list_entry(qh->node.prev, struct uhci_qh, node); qh->link = pqh->link; wmb(); pqh->link = UHCI_PTR_QH | cpu_to_le32(qh->dma_handle);}/* * Take a QH off the hardware schedule */static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh){ struct uhci_qh *pqh; if (qh->state == QH_STATE_UNLINKING) return; WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev); qh->state = QH_STATE_UNLINKING; /* Unlink the QH from the schedule and record when we did it */ pqh = list_entry(qh->node.prev, struct uhci_qh, node); pqh->link = qh->link; mb(); uhci_get_current_frame_number(uhci); qh->unlink_frame = uhci->frame_number; /* Force an interrupt so we know when the QH is fully unlinked */ if (list_empty(&uhci->skel_unlink_qh->node)) uhci_set_next_interrupt(uhci); /* Move the QH from its old list to the end of the unlinking list */ if (qh == uhci->next_qh) uhci->next_qh = list_entry(qh->node.next, struct uhci_qh, node); list_move_tail(&qh->node, &uhci->skel_unlink_qh->node);}/* * When we and the controller are through with a QH, it becomes IDLE. * This happens when a QH has been off the schedule (on the unlinking * list) for more than one frame, or when an error occurs while adding * the first URB onto a new QH. */static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh){ WARN_ON(qh->state == QH_STATE_ACTIVE); if (qh == uhci->next_qh) uhci->next_qh = list_entry(qh->node.next, struct uhci_qh, node); list_move(&qh->node, &uhci->idle_qh_list); qh->state = QH_STATE_IDLE; /* If anyone is waiting for a QH to become idle, wake them up */ if (uhci->num_waiting) wake_up_all(&uhci->waitqh);}static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci, struct urb *urb){ struct urb_priv *urbp; urbp = kmem_cache_alloc(uhci_up_cachep, SLAB_ATOMIC); if (!urbp) return NULL; memset((void *)urbp, 0, sizeof(*urbp)); urbp->urb = urb; urb->hcpriv = urbp; INIT_LIST_HEAD(&urbp->node); INIT_LIST_HEAD(&urbp->td_list); return urbp;}static void uhci_add_td_to_urb(struct urb *urb, struct uhci_td *td){ struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; list_add_tail(&td->list, &urbp->td_list);}static void uhci_remove_td_from_urb(struct uhci_td *td){ if (list_empty(&td->list)) return; list_del_init(&td->list);}static void uhci_free_urb_priv(struct uhci_hcd *uhci, struct urb_priv *urbp){ struct uhci_td *td, *tmp; if (!list_empty(&urbp->node)) dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n", urbp->urb); uhci_get_current_frame_number(uhci); if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age) { uhci_free_pending_tds(uhci); uhci->td_remove_age = uhci->frame_number; } /* Check to see if the remove list is empty. Set the IOC bit */ /* to force an interrupt so we can remove the TDs. */ if (list_empty(&uhci->td_remove_list)) uhci_set_next_interrupt(uhci); list_for_each_entry_safe(td, tmp, &urbp->td_list, list) { uhci_remove_td_from_urb(td); list_add(&td->remove_list, &uhci->td_remove_list); } urbp->urb->hcpriv = NULL; kmem_cache_free(uhci_up_cachep, urbp);}static void uhci_inc_fsbr(struct uhci_hcd *uhci, struct urb *urb){ struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; if ((!(urb->transfer_flags & URB_NO_FSBR)) && !urbp->fsbr) { urbp->fsbr = 1; if (!uhci->fsbr++ && !uhci->fsbrtimeout) uhci->skel_term_qh->link = cpu_to_le32(uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH; }}static void uhci_dec_fsbr(struct uhci_hcd *uhci, struct urb *urb){ struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; if ((!(urb->transfer_flags & URB_NO_FSBR)) && urbp->fsbr) { urbp->fsbr = 0; if (!--uhci->fsbr) uhci->fsbrtimeout = jiffies + FSBR_DELAY; }}/* * Map status to standard result codes * * <status> is (td_status(td) & 0xF60000), a.k.a. * uhci_status_bits(td_status(td)). * Note: <status> does not include the TD_CTRL_NAK bit. * <dir_out> is True for output TDs and False for input TDs. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -