📄 hc_simple.c
字号:
/*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------* * simple generic USB HCD frontend Version 0.9.5 (10/28/2001) * for embedded HCs (eg. isp1161, MPC850, ...) * * USB URB handling, hci_ hcs_ * URB queueing, qu_ * Transfer scheduling, sh_ * * Roman Weissgaerber weissg@vienna.at (C) 2001 * *-------------------------------------------------------------------------* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * *-------------------------------------------------------------------------*//* * V0.9.5 (10/28/2001) start_int()/stop_int() enable interrupt processing on demand * ISOC processing * V0.9.2 (10/10/2001) sh_done_list: on error just reject last packet * V0.9.1 (10/8/2001) unlink_urb check also for USB_ST_URB_PENDING * V0.9 (9/23/2001) * * * * * * * *//* main lock for urb access */static spinlock_t usb_urb_lock = SPIN_LOCK_UNLOCKED; /*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/ /* URB HCD API function layer * * * *//*-------------------------------------------------------------------------*/ /* set actual length to 0 and set status * queue URB * */ static inline int hcs_urb_queue (hci_t * hci, urb_t * urb){ int i; if (usb_pipeisoc (urb->pipe)) { for (i = 0; i < urb->number_of_packets; i++) { urb->iso_frame_desc[i].actual_length = 0; urb->iso_frame_desc[i].status = -EXDEV; } /* urb->next hack : 1 .. resub, 0 .. single shot */ urb->interval = urb->next ? 1 : 0; } urb->status = USB_ST_URB_PENDING; urb->actual_length = 0; urb->error_count = 0; if (usb_pipecontrol (urb->pipe)) hc_flush_data_cache (hci, urb->setup_packet, 8); if (usb_pipeout (urb->pipe)) hc_flush_data_cache (hci, urb->transfer_buffer, urb->transfer_buffer_length); qu_queue_urb (hci, urb); return 0;}/*-------------------------------------------------------------------------*//* return path (complete - callback) of URB API * also handles resubmition of intr URBs * */static int hcs_return_urb (hci_t * hci, urb_t * urb, int resub_ok){ struct usb_device * dev = urb->dev; int resubmit = 0; if (urb_debug) urb_print (urb, "RET", usb_pipeout (urb->pipe)); resubmit = urb->interval && resub_ok; urb->dev = urb->hcpriv = NULL; if (urb->complete) urb->complete (urb); /* call complete */ if (resubmit) { /* requeue the URB */ urb->dev = dev; hcs_urb_queue (hci, urb); } return 0;}/*-------------------------------------------------------------------------*//* got a transfer request * */ static int hci_submit_urb (urb_t * urb){ hci_t * hci; unsigned int pipe = urb->pipe; unsigned long flags; int ret; if (!urb->dev || !urb->dev->bus || urb->hcpriv) return -EINVAL; if (usb_endpoint_halted (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe))) return -EPIPE; hci = (hci_t *) urb->dev->bus->hcpriv; /* a request to the virtual root hub */ if (usb_pipedevice (pipe) == hci->rh.devnum) { if (urb_debug > 1) urb_print (urb, "SUB-RH", usb_pipein (pipe)); //printk("EEpig submit urb to virtual root devnum %d\n",hci->rh.devnum); return rh_submit_urb (urb); } if (urb_debug) urb_print (urb, "SUB", usb_pipein (pipe)); /* queue the URB to its endpoint-queue */ spin_lock_irqsave (&usb_urb_lock, flags); ret = hcs_urb_queue (hci, urb); spin_unlock_irqrestore (&usb_urb_lock, flags); return ret;}/*-------------------------------------------------------------------------*//* unlink URB: mark the URB to unlink * */ static int hci_unlink_urb (urb_t * urb){ unsigned long flags; hci_t * hci; DECLARE_WAITQUEUE (wait, current); void * comp = NULL; if (!urb) /* just to be sure */ return -EINVAL; if (!urb->dev || !urb->dev->bus) return -ENODEV; hci = (hci_t *) urb->dev->bus->hcpriv; /* a request to the virtual root hub */ if (usb_pipedevice (urb->pipe) == hci->rh.devnum) { return rh_unlink_urb (urb); } if (urb_debug) urb_print (urb, "UNLINK", 1); spin_lock_irqsave (&usb_urb_lock, flags); if (!list_empty (&urb->urb_list) && urb->status == USB_ST_URB_PENDING) { /* URB active? */ if (urb->transfer_flags & (USB_ASYNC_UNLINK | USB_TIMEOUT_KILLED)) { /* asynchron with callback */ list_del (&urb->urb_list); /* relink the urb to the del list */ list_add (&urb->urb_list, &hci->del_list); spin_unlock_irqrestore (&usb_urb_lock, flags); } else { /* synchron without callback */ add_wait_queue (&hci->waitq, &wait); set_current_state (TASK_UNINTERRUPTIBLE); comp = urb->complete; urb->complete = NULL; list_del (&urb->urb_list); /* relink the urb to the del list */ list_add (&urb->urb_list, &hci->del_list); spin_unlock_irqrestore (&usb_urb_lock, flags); schedule_timeout(HZ/50); if (!list_empty (&urb->urb_list)) list_del (&urb->urb_list); urb->complete = comp; urb->hcpriv = NULL; remove_wait_queue (&hci->waitq, &wait); } } else { /* hcd does not own URB but we keep the driver happy anyway */ spin_unlock_irqrestore (&usb_urb_lock, flags); if (urb->complete && (urb->transfer_flags & USB_ASYNC_UNLINK)) { urb->status = -ENOENT; urb->actual_length = 0; urb->complete (urb); urb->status = 0; } else { urb->status = -ENOENT; } } return 0;}/*-------------------------------------------------------------------------*//* allocate private data space for a usb device * */static int hci_alloc_dev (struct usb_device * usb_dev){ struct hci_device * dev; int i; dev = kmalloc (sizeof (*dev), GFP_KERNEL); if (!dev) return -ENOMEM; memset (dev, 0, sizeof (*dev)); for (i = 0; i < 32; i++) { INIT_LIST_HEAD (&(dev->ed [i].urb_queue)); dev->ed [i].pipe_head = NULL; } usb_dev->hcpriv = dev; if (hc_verbose) printk ("USB HC dev alloc %d bytes\n", sizeof (*dev)); return 0;}/*-------------------------------------------------------------------------*//* free private data space of usb device * */ static int hci_free_dev (struct usb_device * usb_dev){ if (hc_verbose) printk ("USB HC dev free\n"); if (usb_dev->hcpriv) kfree (usb_dev->hcpriv); usb_dev->hcpriv = NULL; return 0;}/*-------------------------------------------------------------------------*//* tell us the current USB frame number * */static int hci_get_current_frame_number (struct usb_device *usb_dev) { hci_t * hci = usb_dev->bus->hcpriv; return GET_FRAME_NUMBER (hci);}/*-------------------------------------------------------------------------*//* make a list of all io-functions * */ static struct usb_operations hci_device_operations = { hci_alloc_dev, hci_free_dev, hci_get_current_frame_number, hci_submit_urb, hci_unlink_urb};/*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*//* URB queueing: * * For each type of transfer (INTR, BULK, ISO, CTRL) there is a list of * active URBs. * (hci->intr_list, hci->bulk_list, hci->iso_list, hci->ctrl_list) * For every endpoint the head URB of the queued URBs is linked to one of * those lists. * * The rest of the queued URBs of an endpoint are linked into a * private URB list for each endpoint. (hci_dev->ed [endpoint_io].urb_queue) * hci_dev->ed [endpoint_io].pipe_head .. points to the head URB which is * in one of the active URB lists. * * The index of an endpoint consists of its number and its direction. * * The state of an intr and iso URB is 0. * For ctrl URBs the states are US_CTRL_SETUP, US_CTRL_DATA, US_CTRL_ACK * Bulk URBs states are US_BULK and US_BULK0 (with 0-len packet) * * * * */#ifdef HC_URB_TIMEOUTstatic void qu_urb_timeout (unsigned long lurb) { urb_t * urb = (urb_t *) lurb; urb->transfer_flags |= USB_TIMEOUT_KILLED; hci_unlink_urb (urb);}#endif /*-------------------------------------------------------------------------*/static inline int qu_pipeindex (__u32 pipe) { return (usb_pipeendpoint (pipe) << 1) | (usb_pipecontrol (pipe) ? 0 : usb_pipeout (pipe));}static inline void qu_seturbstate (urb_t * urb, int state) { urb->pipe &= ~0x1f; urb->pipe |= state & 0x1f;}static inline int qu_urbstate (urb_t * urb) { return urb->pipe & 0x1f;}/*-------------------------------------------------------------------------*/ static inline void qu_queue_active_urb (hci_t * hci, urb_t * urb, epd_t * ed) { int urb_state = 0; switch (usb_pipetype (urb->pipe)) { case PIPE_CONTROL: list_add (&urb->urb_list, &hci->ctrl_list); urb_state = US_CTRL_SETUP; break; case PIPE_BULK: list_add (&urb->urb_list, &hci->bulk_list); if ((urb->transfer_flags & USB_ZERO_PACKET) && urb->transfer_buffer_length > 0 && ((urb->transfer_buffer_length % usb_maxpacket (urb->dev, urb->pipe, usb_pipeout (urb->pipe))) == 0)) { urb_state = US_BULK0; } break; case PIPE_INTERRUPT: urb->start_frame = hci->frame_number; list_add (&urb->urb_list, &hci->intr_list); break; case PIPE_ISOCHRONOUS: list_add (&urb->urb_list, &hci->iso_list); break; }#ifdef HC_URB_TIMEOUT if (urb->timeout) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -