1 #include <linux/module.h>
2 #include <linux/string.h>
3 #include <linux/bitops.h>
4 #include <linux/slab.h>
8 #include <linux/wait.h>
11 #define to_urb(d) container_of(d, struct urb, kref)
14 static void urb_destroy(
struct kref *
kref)
18 if (urb->transfer_flags & URB_FREE_BUFFER)
19 kfree(urb->transfer_buffer);
41 memset(urb, 0,
sizeof(*urb));
42 kref_init(&urb->kref);
43 INIT_LIST_HEAD(&urb->anchor_list);
68 urb =
kmalloc(
sizeof(
struct urb) +
69 iso_packets *
sizeof(
struct usb_iso_packet_descriptor),
93 kref_put(&urb->kref, urb_destroy);
110 kref_get(&urb->kref);
136 spin_unlock_irqrestore(&anchor->lock, flags);
141 static void __usb_unanchor_urb(
struct urb *
urb,
struct usb_anchor *
anchor)
146 if (list_empty(&anchor->urb_list))
159 struct usb_anchor *
anchor;
164 anchor = urb->anchor;
174 if (
likely(anchor == urb->anchor))
175 __usb_unanchor_urb(urb, anchor);
176 spin_unlock_irqrestore(&anchor->lock, flags);
304 struct usb_device *
dev;
305 struct usb_host_endpoint *ep;
308 if (!urb || urb->hcpriv || !urb->complete)
318 ep = usb_pipe_endpoint(dev, urb->pipe);
324 urb->actual_length = 0;
329 xfertype = usb_endpoint_type(&ep->desc);
339 is_out = usb_endpoint_dir_out(&ep->desc);
343 urb->transfer_flags &= ~(URB_DIR_MASK | URB_DMA_MAP_SINGLE |
344 URB_DMA_MAP_PAGE | URB_DMA_MAP_SG | URB_MAP_LOCAL |
345 URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL |
346 URB_DMA_SG_COMBINED);
347 urb->transfer_flags |= (is_out ? URB_DIR_OUT : URB_DIR_IN);
353 max = usb_endpoint_maxp(&ep->desc);
356 "bogus endpoint ep%d%s in %s (bad maxpacket %d)\n",
357 usb_endpoint_num(&ep->desc), is_out ?
"out" :
"in",
373 int burst = 1 + ep->ss_ep_comp.bMaxBurst;
381 int mult = 1 + ((max >> 11) & 0x03);
386 if (urb->number_of_packets <= 0)
388 for (n = 0; n < urb->number_of_packets; n++) {
389 len = urb->iso_frame_desc[
n].length;
390 if (len < 0 || len > max)
392 urb->iso_frame_desc[
n].status = -
EXDEV;
393 urb->iso_frame_desc[
n].actual_length = 0;
398 if (urb->transfer_buffer_length >
INT_MAX)
406 unsigned int allowed;
407 static int pipetypes[4] = {
408 PIPE_CONTROL, PIPE_ISOCHRONOUS, PIPE_BULK, PIPE_INTERRUPT
412 if (usb_pipetype(urb->pipe) != pipetypes[xfertype])
413 dev_WARN(&dev->dev,
"BOGUS urb xfer, pipe %x != type %x\n",
414 usb_pipetype(urb->pipe), pipetypes[xfertype]);
417 allowed = (URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT | URB_DIR_MASK |
422 allowed |= URB_ZERO_PACKET;
425 allowed |= URB_NO_FSBR;
429 allowed |= URB_SHORT_NOT_OK;
432 allowed |= URB_ISO_ASAP;
435 allowed &= urb->transfer_flags;
438 if (allowed != urb->transfer_flags)
439 dev_WARN(&dev->dev,
"BOGUS urb flags, %x --> %x\n",
440 urb->transfer_flags, allowed);
455 switch (dev->speed) {
457 if (urb->interval < 6)
461 if (urb->interval <= 0)
466 switch (dev->speed) {
469 if (urb->interval > (1 << 15))
474 if (urb->interval > 16)
479 if (urb->interval > (1024 * 8))
480 urb->interval = 1024 * 8;
486 if (urb->interval > 255)
491 if (urb->interval > 1024)
492 urb->interval = 1024;
502 urb->interval =
min(max, 1 <<
ilog2(urb->interval));
624 if (!(urb && urb->dev && urb->ep))
665 if (!(urb && urb->dev && urb->ep))
718 spin_lock_irq(&anchor->lock);
719 while (!list_empty(&anchor->urb_list)) {
720 victim =
list_entry(anchor->urb_list.prev,
struct urb,
724 spin_unlock_irq(&anchor->lock);
728 spin_lock_irq(&anchor->lock);
730 spin_unlock_irq(&anchor->lock);
750 spin_lock_irq(&anchor->lock);
751 anchor->poisoned = 1;
752 while (!list_empty(&anchor->urb_list)) {
753 victim =
list_entry(anchor->urb_list.prev,
struct urb,
757 spin_unlock_irq(&anchor->lock);
761 spin_lock_irq(&anchor->lock);
763 spin_unlock_irq(&anchor->lock);
783 anchor->poisoned = 0;
784 spin_unlock_irqrestore(&anchor->lock, flags);
819 unsigned int timeout)
839 if (!list_empty(&anchor->urb_list)) {
840 victim =
list_entry(anchor->urb_list.next,
struct urb,
843 __usb_unanchor_urb(victim, anchor);
847 spin_unlock_irqrestore(&anchor->lock, flags);
866 while (!list_empty(&anchor->urb_list)) {
867 victim =
list_entry(anchor->urb_list.prev,
struct urb,
869 __usb_unanchor_urb(victim, anchor);
871 spin_unlock_irqrestore(&anchor->lock, flags);
884 return list_empty(&anchor->urb_list);