Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * message.c - synchronous message handling |
| 3 | */ |
| 4 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/pci.h> /* for scatterlist macros */ |
| 6 | #include <linux/usb.h> |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/mm.h> |
| 11 | #include <linux/timer.h> |
| 12 | #include <linux/ctype.h> |
| 13 | #include <linux/device.h> |
Ralf Baechle | 1176360 | 2007-10-23 20:42:11 +0200 | [diff] [blame] | 14 | #include <linux/scatterlist.h> |
Oliver Neukum | 7ceec1f | 2007-01-26 14:26:21 +0100 | [diff] [blame] | 15 | #include <linux/usb/quirks.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/byteorder.h> |
| 17 | |
| 18 | #include "hcd.h" /* for usbcore internals */ |
| 19 | #include "usb.h" |
| 20 | |
Alan Stern | 67f5dde | 2007-07-24 18:23:23 -0400 | [diff] [blame] | 21 | struct api_context { |
| 22 | struct completion done; |
| 23 | int status; |
| 24 | }; |
| 25 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 26 | static void usb_api_blocking_completion(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
Alan Stern | 67f5dde | 2007-07-24 18:23:23 -0400 | [diff] [blame] | 28 | struct api_context *ctx = urb->context; |
| 29 | |
| 30 | ctx->status = urb->status; |
| 31 | complete(&ctx->done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | |
Franck Bui-Huu | ecdc0a5 | 2006-07-12 10:09:41 +0200 | [diff] [blame] | 35 | /* |
| 36 | * Starts urb and waits for completion or timeout. Note that this call |
| 37 | * is NOT interruptible. Many device driver i/o requests should be |
| 38 | * interruptible and therefore these drivers should implement their |
| 39 | * own interruptible routines. |
| 40 | */ |
| 41 | static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length) |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 42 | { |
Alan Stern | 67f5dde | 2007-07-24 18:23:23 -0400 | [diff] [blame] | 43 | struct api_context ctx; |
Franck Bui-Huu | ecdc0a5 | 2006-07-12 10:09:41 +0200 | [diff] [blame] | 44 | unsigned long expire; |
Greg Kroah-Hartman | 3fc3e82 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 45 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Alan Stern | 67f5dde | 2007-07-24 18:23:23 -0400 | [diff] [blame] | 47 | init_completion(&ctx.done); |
| 48 | urb->context = &ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | urb->actual_length = 0; |
Greg Kroah-Hartman | 3fc3e82 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 50 | retval = usb_submit_urb(urb, GFP_NOIO); |
| 51 | if (unlikely(retval)) |
Franck Bui-Huu | ecdc0a5 | 2006-07-12 10:09:41 +0200 | [diff] [blame] | 52 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Franck Bui-Huu | ecdc0a5 | 2006-07-12 10:09:41 +0200 | [diff] [blame] | 54 | expire = timeout ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT; |
Alan Stern | 67f5dde | 2007-07-24 18:23:23 -0400 | [diff] [blame] | 55 | if (!wait_for_completion_timeout(&ctx.done, expire)) { |
| 56 | usb_kill_urb(urb); |
| 57 | retval = (ctx.status == -ENOENT ? -ETIMEDOUT : ctx.status); |
Franck Bui-Huu | ecdc0a5 | 2006-07-12 10:09:41 +0200 | [diff] [blame] | 58 | |
| 59 | dev_dbg(&urb->dev->dev, |
| 60 | "%s timed out on ep%d%s len=%d/%d\n", |
| 61 | current->comm, |
Alan Stern | 5e60a16 | 2007-07-30 17:07:21 -0400 | [diff] [blame] | 62 | usb_endpoint_num(&urb->ep->desc), |
| 63 | usb_urb_dir_in(urb) ? "in" : "out", |
Franck Bui-Huu | ecdc0a5 | 2006-07-12 10:09:41 +0200 | [diff] [blame] | 64 | urb->actual_length, |
| 65 | urb->transfer_buffer_length); |
Franck Bui-Huu | ecdc0a5 | 2006-07-12 10:09:41 +0200 | [diff] [blame] | 66 | } else |
Alan Stern | 67f5dde | 2007-07-24 18:23:23 -0400 | [diff] [blame] | 67 | retval = ctx.status; |
Franck Bui-Huu | ecdc0a5 | 2006-07-12 10:09:41 +0200 | [diff] [blame] | 68 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | if (actual_length) |
| 70 | *actual_length = urb->actual_length; |
Franck Bui-Huu | ecdc0a5 | 2006-07-12 10:09:41 +0200 | [diff] [blame] | 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | usb_free_urb(urb); |
Greg Kroah-Hartman | 3fc3e82 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 73 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /*-------------------------------------------------------------------*/ |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 77 | /* returns status (negative) or length (positive) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | static int usb_internal_control_msg(struct usb_device *usb_dev, |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 79 | unsigned int pipe, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | struct usb_ctrlrequest *cmd, |
| 81 | void *data, int len, int timeout) |
| 82 | { |
| 83 | struct urb *urb; |
| 84 | int retv; |
| 85 | int length; |
| 86 | |
| 87 | urb = usb_alloc_urb(0, GFP_NOIO); |
| 88 | if (!urb) |
| 89 | return -ENOMEM; |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char *)cmd, data, |
| 92 | len, usb_api_blocking_completion, NULL); |
| 93 | |
| 94 | retv = usb_start_wait_urb(urb, timeout, &length); |
| 95 | if (retv < 0) |
| 96 | return retv; |
| 97 | else |
| 98 | return length; |
| 99 | } |
| 100 | |
| 101 | /** |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 102 | * usb_control_msg - Builds a control urb, sends it off and waits for completion |
| 103 | * @dev: pointer to the usb device to send the message to |
| 104 | * @pipe: endpoint "pipe" to send the message to |
| 105 | * @request: USB message request value |
| 106 | * @requesttype: USB message request type value |
| 107 | * @value: USB message value |
| 108 | * @index: USB message index value |
| 109 | * @data: pointer to the data to send |
| 110 | * @size: length in bytes of the data to send |
| 111 | * @timeout: time in msecs to wait for the message to complete before timing |
| 112 | * out (if 0 the wait is forever) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | * |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 114 | * Context: !in_interrupt () |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | * |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 116 | * This function sends a simple control message to a specified endpoint and |
| 117 | * waits for the message to complete, or timeout. |
| 118 | * |
| 119 | * If successful, it returns the number of bytes transferred, otherwise a |
| 120 | * negative error number. |
| 121 | * |
| 122 | * Don't use this function from within an interrupt context, like a bottom half |
| 123 | * handler. If you need an asynchronous message, or need to send a message |
| 124 | * from within interrupt context, use usb_submit_urb(). |
| 125 | * If a thread in your driver uses this call, make sure your disconnect() |
| 126 | * method can wait for it to complete. Since you don't have a handle on the |
| 127 | * URB used, you can't cancel the request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | */ |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 129 | int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request, |
| 130 | __u8 requesttype, __u16 value, __u16 index, void *data, |
| 131 | __u16 size, int timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 133 | struct usb_ctrlrequest *dr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | int ret; |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 135 | |
| 136 | dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | if (!dr) |
| 138 | return -ENOMEM; |
| 139 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 140 | dr->bRequestType = requesttype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | dr->bRequest = request; |
| 142 | dr->wValue = cpu_to_le16p(&value); |
| 143 | dr->wIndex = cpu_to_le16p(&index); |
| 144 | dr->wLength = cpu_to_le16p(&size); |
| 145 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 146 | /* dbg("usb_control_msg"); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout); |
| 149 | |
| 150 | kfree(dr); |
| 151 | |
| 152 | return ret; |
| 153 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 154 | EXPORT_SYMBOL_GPL(usb_control_msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | /** |
Greg Kroah-Hartman | 782a7a6 | 2006-05-19 13:20:20 -0700 | [diff] [blame] | 157 | * usb_interrupt_msg - Builds an interrupt urb, sends it off and waits for completion |
| 158 | * @usb_dev: pointer to the usb device to send the message to |
| 159 | * @pipe: endpoint "pipe" to send the message to |
| 160 | * @data: pointer to the data to send |
| 161 | * @len: length in bytes of the data to send |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 162 | * @actual_length: pointer to a location to put the actual length transferred |
| 163 | * in bytes |
Greg Kroah-Hartman | 782a7a6 | 2006-05-19 13:20:20 -0700 | [diff] [blame] | 164 | * @timeout: time in msecs to wait for the message to complete before |
| 165 | * timing out (if 0 the wait is forever) |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 166 | * |
Greg Kroah-Hartman | 782a7a6 | 2006-05-19 13:20:20 -0700 | [diff] [blame] | 167 | * Context: !in_interrupt () |
| 168 | * |
| 169 | * This function sends a simple interrupt message to a specified endpoint and |
| 170 | * waits for the message to complete, or timeout. |
| 171 | * |
| 172 | * If successful, it returns 0, otherwise a negative error number. The number |
| 173 | * of actual bytes transferred will be stored in the actual_length paramater. |
| 174 | * |
| 175 | * Don't use this function from within an interrupt context, like a bottom half |
| 176 | * handler. If you need an asynchronous message, or need to send a message |
| 177 | * from within interrupt context, use usb_submit_urb() If a thread in your |
| 178 | * driver uses this call, make sure your disconnect() method can wait for it to |
| 179 | * complete. Since you don't have a handle on the URB used, you can't cancel |
| 180 | * the request. |
| 181 | */ |
| 182 | int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe, |
| 183 | void *data, int len, int *actual_length, int timeout) |
| 184 | { |
| 185 | return usb_bulk_msg(usb_dev, pipe, data, len, actual_length, timeout); |
| 186 | } |
| 187 | EXPORT_SYMBOL_GPL(usb_interrupt_msg); |
| 188 | |
| 189 | /** |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 190 | * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion |
| 191 | * @usb_dev: pointer to the usb device to send the message to |
| 192 | * @pipe: endpoint "pipe" to send the message to |
| 193 | * @data: pointer to the data to send |
| 194 | * @len: length in bytes of the data to send |
| 195 | * @actual_length: pointer to a location to put the actual length transferred |
| 196 | * in bytes |
| 197 | * @timeout: time in msecs to wait for the message to complete before |
| 198 | * timing out (if 0 the wait is forever) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | * |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 200 | * Context: !in_interrupt () |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | * |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 202 | * This function sends a simple bulk message to a specified endpoint |
| 203 | * and waits for the message to complete, or timeout. |
Alan Stern | d09d36a | 2005-09-26 16:22:45 -0400 | [diff] [blame] | 204 | * |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 205 | * If successful, it returns 0, otherwise a negative error number. The number |
| 206 | * of actual bytes transferred will be stored in the actual_length paramater. |
| 207 | * |
| 208 | * Don't use this function from within an interrupt context, like a bottom half |
| 209 | * handler. If you need an asynchronous message, or need to send a message |
| 210 | * from within interrupt context, use usb_submit_urb() If a thread in your |
| 211 | * driver uses this call, make sure your disconnect() method can wait for it to |
| 212 | * complete. Since you don't have a handle on the URB used, you can't cancel |
| 213 | * the request. |
| 214 | * |
| 215 | * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT ioctl, |
| 216 | * users are forced to abuse this routine by using it to submit URBs for |
| 217 | * interrupt endpoints. We will take the liberty of creating an interrupt URB |
| 218 | * (with the default interval) if the target is an interrupt endpoint. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | */ |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 220 | int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, |
| 221 | void *data, int len, int *actual_length, int timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
| 223 | struct urb *urb; |
Alan Stern | d09d36a | 2005-09-26 16:22:45 -0400 | [diff] [blame] | 224 | struct usb_host_endpoint *ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Alan Stern | d09d36a | 2005-09-26 16:22:45 -0400 | [diff] [blame] | 226 | ep = (usb_pipein(pipe) ? usb_dev->ep_in : usb_dev->ep_out) |
| 227 | [usb_pipeendpoint(pipe)]; |
| 228 | if (!ep || len < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | return -EINVAL; |
| 230 | |
Alan Stern | d09d36a | 2005-09-26 16:22:45 -0400 | [diff] [blame] | 231 | urb = usb_alloc_urb(0, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | if (!urb) |
| 233 | return -ENOMEM; |
| 234 | |
Alan Stern | d09d36a | 2005-09-26 16:22:45 -0400 | [diff] [blame] | 235 | if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == |
| 236 | USB_ENDPOINT_XFER_INT) { |
| 237 | pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30); |
| 238 | usb_fill_int_urb(urb, usb_dev, pipe, data, len, |
Alan Stern | 8d062b9 | 2007-04-23 17:30:32 -0400 | [diff] [blame] | 239 | usb_api_blocking_completion, NULL, |
| 240 | ep->desc.bInterval); |
Alan Stern | d09d36a | 2005-09-26 16:22:45 -0400 | [diff] [blame] | 241 | } else |
| 242 | usb_fill_bulk_urb(urb, usb_dev, pipe, data, len, |
| 243 | usb_api_blocking_completion, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | return usb_start_wait_urb(urb, timeout, actual_length); |
| 246 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 247 | EXPORT_SYMBOL_GPL(usb_bulk_msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | /*-------------------------------------------------------------------*/ |
| 250 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 251 | static void sg_clean(struct usb_sg_request *io) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { |
| 253 | if (io->urbs) { |
| 254 | while (io->entries--) |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 255 | usb_free_urb(io->urbs [io->entries]); |
| 256 | kfree(io->urbs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | io->urbs = NULL; |
| 258 | } |
| 259 | if (io->dev->dev.dma_mask != NULL) |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 260 | usb_buffer_unmap_sg(io->dev, usb_pipein(io->pipe), |
| 261 | io->sg, io->nents); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | io->dev = NULL; |
| 263 | } |
| 264 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 265 | static void sg_complete(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 267 | struct usb_sg_request *io = urb->context; |
Greg Kroah-Hartman | 3fc3e82 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 268 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 270 | spin_lock(&io->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | /* In 2.5 we require hcds' endpoint queues not to progress after fault |
| 273 | * reports, until the completion callback (this!) returns. That lets |
| 274 | * device driver code (like this routine) unlink queued urbs first, |
| 275 | * if it needs to, since the HC won't work on them at all. So it's |
| 276 | * not possible for page N+1 to overwrite page N, and so on. |
| 277 | * |
| 278 | * That's only for "hard" faults; "soft" faults (unlinks) sometimes |
| 279 | * complete before the HCD can get requests away from hardware, |
| 280 | * though never during cleanup after a hard fault. |
| 281 | */ |
| 282 | if (io->status |
| 283 | && (io->status != -ECONNRESET |
Greg Kroah-Hartman | 3fc3e82 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 284 | || status != -ECONNRESET) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | && urb->actual_length) { |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 286 | dev_err(io->dev->bus->controller, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | "dev %s ep%d%s scatterlist error %d/%d\n", |
| 288 | io->dev->devpath, |
Alan Stern | 5e60a16 | 2007-07-30 17:07:21 -0400 | [diff] [blame] | 289 | usb_endpoint_num(&urb->ep->desc), |
| 290 | usb_urb_dir_in(urb) ? "in" : "out", |
Greg Kroah-Hartman | 3fc3e82 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 291 | status, io->status); |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 292 | /* BUG (); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Greg Kroah-Hartman | 3fc3e82 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 295 | if (io->status == 0 && status && status != -ECONNRESET) { |
| 296 | int i, found, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Greg Kroah-Hartman | 3fc3e82 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 298 | io->status = status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
| 300 | /* the previous urbs, and this one, completed already. |
| 301 | * unlink pending urbs so they won't rx/tx bad data. |
| 302 | * careful: unlink can sometimes be synchronous... |
| 303 | */ |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 304 | spin_unlock(&io->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | for (i = 0, found = 0; i < io->entries; i++) { |
| 306 | if (!io->urbs [i] || !io->urbs [i]->dev) |
| 307 | continue; |
| 308 | if (found) { |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 309 | retval = usb_unlink_urb(io->urbs [i]); |
Greg Kroah-Hartman | 3fc3e82 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 310 | if (retval != -EINPROGRESS && |
| 311 | retval != -ENODEV && |
| 312 | retval != -EBUSY) |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 313 | dev_err(&io->dev->dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | "%s, unlink --> %d\n", |
Greg Kroah-Hartman | 3fc3e82 | 2007-07-18 10:58:02 -0700 | [diff] [blame] | 315 | __FUNCTION__, retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | } else if (urb == io->urbs [i]) |
| 317 | found = 1; |
| 318 | } |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 319 | spin_lock(&io->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
| 321 | urb->dev = NULL; |
| 322 | |
| 323 | /* on the last completion, signal usb_sg_wait() */ |
| 324 | io->bytes += urb->actual_length; |
| 325 | io->count--; |
| 326 | if (!io->count) |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 327 | complete(&io->complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 329 | spin_unlock(&io->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | |
| 333 | /** |
| 334 | * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request |
| 335 | * @io: request block being initialized. until usb_sg_wait() returns, |
| 336 | * treat this as a pointer to an opaque block of memory, |
| 337 | * @dev: the usb device that will send or receive the data |
| 338 | * @pipe: endpoint "pipe" used to transfer the data |
| 339 | * @period: polling rate for interrupt endpoints, in frames or |
| 340 | * (for high speed endpoints) microframes; ignored for bulk |
| 341 | * @sg: scatterlist entries |
| 342 | * @nents: how many entries in the scatterlist |
| 343 | * @length: how many bytes to send from the scatterlist, or zero to |
| 344 | * send every byte identified in the list. |
| 345 | * @mem_flags: SLAB_* flags affecting memory allocations in this call |
| 346 | * |
| 347 | * Returns zero for success, else a negative errno value. This initializes a |
| 348 | * scatter/gather request, allocating resources such as I/O mappings and urb |
| 349 | * memory (except maybe memory used by USB controller drivers). |
| 350 | * |
| 351 | * The request must be issued using usb_sg_wait(), which waits for the I/O to |
| 352 | * complete (or to be canceled) and then cleans up all resources allocated by |
| 353 | * usb_sg_init(). |
| 354 | * |
| 355 | * The request may be canceled with usb_sg_cancel(), either before or after |
| 356 | * usb_sg_wait() is called. |
| 357 | */ |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 358 | int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev, |
| 359 | unsigned pipe, unsigned period, struct scatterlist *sg, |
| 360 | int nents, size_t length, gfp_t mem_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 362 | int i; |
| 363 | int urb_flags; |
| 364 | int dma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
| 366 | if (!io || !dev || !sg |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 367 | || usb_pipecontrol(pipe) |
| 368 | || usb_pipeisoc(pipe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | || nents <= 0) |
| 370 | return -EINVAL; |
| 371 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 372 | spin_lock_init(&io->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | io->dev = dev; |
| 374 | io->pipe = pipe; |
| 375 | io->sg = sg; |
| 376 | io->nents = nents; |
| 377 | |
| 378 | /* not all host controllers use DMA (like the mainstream pci ones); |
| 379 | * they can use PIO (sl811) or be software over another transport. |
| 380 | */ |
| 381 | dma = (dev->dev.dma_mask != NULL); |
| 382 | if (dma) |
Alan Stern | 5e60a16 | 2007-07-30 17:07:21 -0400 | [diff] [blame] | 383 | io->entries = usb_buffer_map_sg(dev, usb_pipein(pipe), |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 384 | sg, nents); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | else |
| 386 | io->entries = nents; |
| 387 | |
| 388 | /* initialize all the urbs we'll use */ |
| 389 | if (io->entries <= 0) |
| 390 | return io->entries; |
| 391 | |
| 392 | io->count = io->entries; |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 393 | io->urbs = kmalloc(io->entries * sizeof *io->urbs, mem_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | if (!io->urbs) |
| 395 | goto nomem; |
| 396 | |
Alan Stern | b375a04 | 2005-07-29 16:11:07 -0400 | [diff] [blame] | 397 | urb_flags = URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT; |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 398 | if (usb_pipein(pipe)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | urb_flags |= URB_SHORT_NOT_OK; |
| 400 | |
| 401 | for (i = 0; i < io->entries; i++) { |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 402 | unsigned len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 404 | io->urbs[i] = usb_alloc_urb(0, mem_flags); |
| 405 | if (!io->urbs[i]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | io->entries = i; |
| 407 | goto nomem; |
| 408 | } |
| 409 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 410 | io->urbs[i]->dev = NULL; |
| 411 | io->urbs[i]->pipe = pipe; |
| 412 | io->urbs[i]->interval = period; |
| 413 | io->urbs[i]->transfer_flags = urb_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 415 | io->urbs[i]->complete = sg_complete; |
| 416 | io->urbs[i]->context = io; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
Tony Lindgren | 35d07fd | 2007-03-31 18:15:43 -0700 | [diff] [blame] | 418 | /* |
| 419 | * Some systems need to revert to PIO when DMA is temporarily |
| 420 | * unavailable. For their sakes, both transfer_buffer and |
| 421 | * transfer_dma are set when possible. However this can only |
David Brownell | a12b8db | 2007-07-22 15:13:13 -0700 | [diff] [blame] | 422 | * work on systems without: |
| 423 | * |
| 424 | * - HIGHMEM, since DMA buffers located in high memory are |
| 425 | * not directly addressable by the CPU for PIO; |
| 426 | * |
| 427 | * - IOMMU, since dma_map_sg() is allowed to use an IOMMU to |
| 428 | * make virtually discontiguous buffers be "dma-contiguous" |
| 429 | * so that PIO and DMA need diferent numbers of URBs. |
| 430 | * |
| 431 | * So when HIGHMEM or IOMMU are in use, transfer_buffer is NULL |
Tony Lindgren | 35d07fd | 2007-03-31 18:15:43 -0700 | [diff] [blame] | 432 | * to prevent stale pointers and to help spot bugs. |
| 433 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | if (dma) { |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 435 | io->urbs[i]->transfer_dma = sg_dma_address(sg + i); |
| 436 | len = sg_dma_len(sg + i); |
Joerg Roedel | 966396d | 2007-10-24 12:49:48 +0200 | [diff] [blame] | 437 | #if defined(CONFIG_HIGHMEM) || defined(CONFIG_GART_IOMMU) |
Tony Lindgren | 35d07fd | 2007-03-31 18:15:43 -0700 | [diff] [blame] | 438 | io->urbs[i]->transfer_buffer = NULL; |
| 439 | #else |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 440 | io->urbs[i]->transfer_buffer = sg_virt(&sg[i]); |
Tony Lindgren | 35d07fd | 2007-03-31 18:15:43 -0700 | [diff] [blame] | 441 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | } else { |
| 443 | /* hc may use _only_ transfer_buffer */ |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 444 | io->urbs[i]->transfer_buffer = sg_virt(&sg[i]); |
| 445 | len = sg[i].length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | if (length) { |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 449 | len = min_t(unsigned, len, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | length -= len; |
| 451 | if (length == 0) |
| 452 | io->entries = i + 1; |
| 453 | } |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 454 | io->urbs[i]->transfer_buffer_length = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 456 | io->urbs[--i]->transfer_flags &= ~URB_NO_INTERRUPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
| 458 | /* transaction state */ |
| 459 | io->status = 0; |
| 460 | io->bytes = 0; |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 461 | init_completion(&io->complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | return 0; |
| 463 | |
| 464 | nomem: |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 465 | sg_clean(io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | return -ENOMEM; |
| 467 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 468 | EXPORT_SYMBOL_GPL(usb_sg_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
| 470 | /** |
| 471 | * usb_sg_wait - synchronously execute scatter/gather request |
| 472 | * @io: request block handle, as initialized with usb_sg_init(). |
| 473 | * some fields become accessible when this call returns. |
| 474 | * Context: !in_interrupt () |
| 475 | * |
| 476 | * This function blocks until the specified I/O operation completes. It |
| 477 | * leverages the grouping of the related I/O requests to get good transfer |
| 478 | * rates, by queueing the requests. At higher speeds, such queuing can |
| 479 | * significantly improve USB throughput. |
| 480 | * |
| 481 | * There are three kinds of completion for this function. |
| 482 | * (1) success, where io->status is zero. The number of io->bytes |
| 483 | * transferred is as requested. |
| 484 | * (2) error, where io->status is a negative errno value. The number |
| 485 | * of io->bytes transferred before the error is usually less |
| 486 | * than requested, and can be nonzero. |
Steven Cole | 093cf72 | 2005-05-03 19:07:24 -0600 | [diff] [blame] | 487 | * (3) cancellation, a type of error with status -ECONNRESET that |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | * is initiated by usb_sg_cancel(). |
| 489 | * |
| 490 | * When this function returns, all memory allocated through usb_sg_init() or |
| 491 | * this call will have been freed. The request block parameter may still be |
| 492 | * passed to usb_sg_cancel(), or it may be freed. It could also be |
| 493 | * reinitialized and then reused. |
| 494 | * |
| 495 | * Data Transfer Rates: |
| 496 | * |
| 497 | * Bulk transfers are valid for full or high speed endpoints. |
| 498 | * The best full speed data rate is 19 packets of 64 bytes each |
| 499 | * per frame, or 1216 bytes per millisecond. |
| 500 | * The best high speed data rate is 13 packets of 512 bytes each |
| 501 | * per microframe, or 52 KBytes per millisecond. |
| 502 | * |
| 503 | * The reason to use interrupt transfers through this API would most likely |
| 504 | * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond |
| 505 | * could be transferred. That capability is less useful for low or full |
| 506 | * speed interrupt endpoints, which allow at most one packet per millisecond, |
| 507 | * of at most 8 or 64 bytes (respectively). |
| 508 | */ |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 509 | void usb_sg_wait(struct usb_sg_request *io) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | { |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 511 | int i; |
| 512 | int entries = io->entries; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
| 514 | /* queue the urbs. */ |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 515 | spin_lock_irq(&io->lock); |
Alan Stern | 8ccef0d | 2007-06-21 16:26:46 -0400 | [diff] [blame] | 516 | i = 0; |
| 517 | while (i < entries && !io->status) { |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 518 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 520 | io->urbs[i]->dev = io->dev; |
| 521 | retval = usb_submit_urb(io->urbs [i], GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
| 523 | /* after we submit, let completions or cancelations fire; |
| 524 | * we handshake using io->status. |
| 525 | */ |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 526 | spin_unlock_irq(&io->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | switch (retval) { |
| 528 | /* maybe we retrying will recover */ |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 529 | case -ENXIO: /* hc didn't queue this one */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | case -EAGAIN: |
| 531 | case -ENOMEM: |
| 532 | io->urbs[i]->dev = NULL; |
| 533 | retval = 0; |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 534 | yield(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | break; |
| 536 | |
| 537 | /* no error? continue immediately. |
| 538 | * |
| 539 | * NOTE: to work better with UHCI (4K I/O buffer may |
| 540 | * need 3K of TDs) it may be good to limit how many |
| 541 | * URBs are queued at once; N milliseconds? |
| 542 | */ |
| 543 | case 0: |
Alan Stern | 8ccef0d | 2007-06-21 16:26:46 -0400 | [diff] [blame] | 544 | ++i; |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 545 | cpu_relax(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | break; |
| 547 | |
| 548 | /* fail any uncompleted urbs */ |
| 549 | default: |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 550 | io->urbs[i]->dev = NULL; |
| 551 | io->urbs[i]->status = retval; |
| 552 | dev_dbg(&io->dev->dev, "%s, submit --> %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | __FUNCTION__, retval); |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 554 | usb_sg_cancel(io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | } |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 556 | spin_lock_irq(&io->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | if (retval && (io->status == 0 || io->status == -ECONNRESET)) |
| 558 | io->status = retval; |
| 559 | } |
| 560 | io->count -= entries - i; |
| 561 | if (io->count == 0) |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 562 | complete(&io->complete); |
| 563 | spin_unlock_irq(&io->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | |
| 565 | /* OK, yes, this could be packaged as non-blocking. |
| 566 | * So could the submit loop above ... but it's easier to |
| 567 | * solve neither problem than to solve both! |
| 568 | */ |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 569 | wait_for_completion(&io->complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 571 | sg_clean(io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 573 | EXPORT_SYMBOL_GPL(usb_sg_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | |
| 575 | /** |
| 576 | * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait() |
| 577 | * @io: request block, initialized with usb_sg_init() |
| 578 | * |
| 579 | * This stops a request after it has been started by usb_sg_wait(). |
| 580 | * It can also prevents one initialized by usb_sg_init() from starting, |
| 581 | * so that call just frees resources allocated to the request. |
| 582 | */ |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 583 | void usb_sg_cancel(struct usb_sg_request *io) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | { |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 585 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 587 | spin_lock_irqsave(&io->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
| 589 | /* shut everything down, if it didn't already */ |
| 590 | if (!io->status) { |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 591 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
| 593 | io->status = -ECONNRESET; |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 594 | spin_unlock(&io->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | for (i = 0; i < io->entries; i++) { |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 596 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
| 598 | if (!io->urbs [i]->dev) |
| 599 | continue; |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 600 | retval = usb_unlink_urb(io->urbs [i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | if (retval != -EINPROGRESS && retval != -EBUSY) |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 602 | dev_warn(&io->dev->dev, "%s, unlink --> %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | __FUNCTION__, retval); |
| 604 | } |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 605 | spin_lock(&io->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | } |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 607 | spin_unlock_irqrestore(&io->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 609 | EXPORT_SYMBOL_GPL(usb_sg_cancel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
| 611 | /*-------------------------------------------------------------------*/ |
| 612 | |
| 613 | /** |
| 614 | * usb_get_descriptor - issues a generic GET_DESCRIPTOR request |
| 615 | * @dev: the device whose descriptor is being retrieved |
| 616 | * @type: the descriptor type (USB_DT_*) |
| 617 | * @index: the number of the descriptor |
| 618 | * @buf: where to put the descriptor |
| 619 | * @size: how big is "buf"? |
| 620 | * Context: !in_interrupt () |
| 621 | * |
| 622 | * Gets a USB descriptor. Convenience functions exist to simplify |
| 623 | * getting some types of descriptors. Use |
| 624 | * usb_get_string() or usb_string() for USB_DT_STRING. |
| 625 | * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG) |
| 626 | * are part of the device structure. |
| 627 | * In addition to a number of USB-standard descriptors, some |
| 628 | * devices also use class-specific or vendor-specific descriptors. |
| 629 | * |
| 630 | * This call is synchronous, and may not be used in an interrupt context. |
| 631 | * |
| 632 | * Returns the number of bytes received on success, or else the status code |
| 633 | * returned by the underlying usb_control_msg() call. |
| 634 | */ |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 635 | int usb_get_descriptor(struct usb_device *dev, unsigned char type, |
| 636 | unsigned char index, void *buf, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | { |
| 638 | int i; |
| 639 | int result; |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 640 | |
| 641 | memset(buf, 0, size); /* Make sure we parse really received data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
| 643 | for (i = 0; i < 3; ++i) { |
Alan Stern | c39772d | 2007-08-20 10:45:28 -0400 | [diff] [blame] | 644 | /* retry on length 0 or error; some devices are flakey */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 646 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, |
| 647 | (type << 8) + index, 0, buf, size, |
| 648 | USB_CTRL_GET_TIMEOUT); |
Alan Stern | c39772d | 2007-08-20 10:45:28 -0400 | [diff] [blame] | 649 | if (result <= 0 && result != -ETIMEDOUT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | continue; |
| 651 | if (result > 1 && ((u8 *)buf)[1] != type) { |
| 652 | result = -EPROTO; |
| 653 | continue; |
| 654 | } |
| 655 | break; |
| 656 | } |
| 657 | return result; |
| 658 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 659 | EXPORT_SYMBOL_GPL(usb_get_descriptor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
| 661 | /** |
| 662 | * usb_get_string - gets a string descriptor |
| 663 | * @dev: the device whose string descriptor is being retrieved |
| 664 | * @langid: code for language chosen (from string descriptor zero) |
| 665 | * @index: the number of the descriptor |
| 666 | * @buf: where to put the string |
| 667 | * @size: how big is "buf"? |
| 668 | * Context: !in_interrupt () |
| 669 | * |
| 670 | * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character, |
| 671 | * in little-endian byte order). |
| 672 | * The usb_string() function will often be a convenient way to turn |
| 673 | * these strings into kernel-printable form. |
| 674 | * |
| 675 | * Strings may be referenced in device, configuration, interface, or other |
| 676 | * descriptors, and could also be used in vendor-specific ways. |
| 677 | * |
| 678 | * This call is synchronous, and may not be used in an interrupt context. |
| 679 | * |
| 680 | * Returns the number of bytes received on success, or else the status code |
| 681 | * returned by the underlying usb_control_msg() call. |
| 682 | */ |
Adrian Bunk | e266a12 | 2005-11-08 21:05:43 +0100 | [diff] [blame] | 683 | static int usb_get_string(struct usb_device *dev, unsigned short langid, |
| 684 | unsigned char index, void *buf, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | { |
| 686 | int i; |
| 687 | int result; |
| 688 | |
| 689 | for (i = 0; i < 3; ++i) { |
| 690 | /* retry on length 0 or stall; some devices are flakey */ |
| 691 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 692 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, |
| 693 | (USB_DT_STRING << 8) + index, langid, buf, size, |
| 694 | USB_CTRL_GET_TIMEOUT); |
| 695 | if (!(result == 0 || result == -EPIPE)) |
| 696 | break; |
| 697 | } |
| 698 | return result; |
| 699 | } |
| 700 | |
| 701 | static void usb_try_string_workarounds(unsigned char *buf, int *length) |
| 702 | { |
| 703 | int newlength, oldlength = *length; |
| 704 | |
| 705 | for (newlength = 2; newlength + 1 < oldlength; newlength += 2) |
| 706 | if (!isprint(buf[newlength]) || buf[newlength + 1]) |
| 707 | break; |
| 708 | |
| 709 | if (newlength > 2) { |
| 710 | buf[0] = newlength; |
| 711 | *length = newlength; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | static int usb_string_sub(struct usb_device *dev, unsigned int langid, |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 716 | unsigned int index, unsigned char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | { |
| 718 | int rc; |
| 719 | |
| 720 | /* Try to read the string descriptor by asking for the maximum |
| 721 | * possible number of bytes */ |
Oliver Neukum | 7ceec1f | 2007-01-26 14:26:21 +0100 | [diff] [blame] | 722 | if (dev->quirks & USB_QUIRK_STRING_FETCH_255) |
| 723 | rc = -EIO; |
| 724 | else |
| 725 | rc = usb_get_string(dev, langid, index, buf, 255); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
| 727 | /* If that failed try to read the descriptor length, then |
| 728 | * ask for just that many bytes */ |
| 729 | if (rc < 2) { |
| 730 | rc = usb_get_string(dev, langid, index, buf, 2); |
| 731 | if (rc == 2) |
| 732 | rc = usb_get_string(dev, langid, index, buf, buf[0]); |
| 733 | } |
| 734 | |
| 735 | if (rc >= 2) { |
| 736 | if (!buf[0] && !buf[1]) |
| 737 | usb_try_string_workarounds(buf, &rc); |
| 738 | |
| 739 | /* There might be extra junk at the end of the descriptor */ |
| 740 | if (buf[0] < rc) |
| 741 | rc = buf[0]; |
| 742 | |
| 743 | rc = rc - (rc & 1); /* force a multiple of two */ |
| 744 | } |
| 745 | |
| 746 | if (rc < 2) |
| 747 | rc = (rc < 0 ? rc : -EINVAL); |
| 748 | |
| 749 | return rc; |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * usb_string - returns ISO 8859-1 version of a string descriptor |
| 754 | * @dev: the device whose string descriptor is being retrieved |
| 755 | * @index: the number of the descriptor |
| 756 | * @buf: where to put the string |
| 757 | * @size: how big is "buf"? |
| 758 | * Context: !in_interrupt () |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 759 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | * This converts the UTF-16LE encoded strings returned by devices, from |
| 761 | * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones |
| 762 | * that are more usable in most kernel contexts. Note that all characters |
| 763 | * in the chosen descriptor that can't be encoded using ISO-8859-1 |
| 764 | * are converted to the question mark ("?") character, and this function |
| 765 | * chooses strings in the first language supported by the device. |
| 766 | * |
| 767 | * The ASCII (or, redundantly, "US-ASCII") character set is the seven-bit |
| 768 | * subset of ISO 8859-1. ISO-8859-1 is the eight-bit subset of Unicode, |
| 769 | * and is appropriate for use many uses of English and several other |
| 770 | * Western European languages. (But it doesn't include the "Euro" symbol.) |
| 771 | * |
| 772 | * This call is synchronous, and may not be used in an interrupt context. |
| 773 | * |
| 774 | * Returns length of the string (>= 0) or usb_control_msg status (< 0). |
| 775 | */ |
| 776 | int usb_string(struct usb_device *dev, int index, char *buf, size_t size) |
| 777 | { |
| 778 | unsigned char *tbuf; |
| 779 | int err; |
| 780 | unsigned int u, idx; |
| 781 | |
| 782 | if (dev->state == USB_STATE_SUSPENDED) |
| 783 | return -EHOSTUNREACH; |
| 784 | if (size <= 0 || !buf || !index) |
| 785 | return -EINVAL; |
| 786 | buf[0] = 0; |
| 787 | tbuf = kmalloc(256, GFP_KERNEL); |
| 788 | if (!tbuf) |
| 789 | return -ENOMEM; |
| 790 | |
| 791 | /* get langid for strings if it's not yet known */ |
| 792 | if (!dev->have_langid) { |
| 793 | err = usb_string_sub(dev, 0, 0, tbuf); |
| 794 | if (err < 0) { |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 795 | dev_err(&dev->dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | "string descriptor 0 read error: %d\n", |
| 797 | err); |
| 798 | goto errout; |
| 799 | } else if (err < 4) { |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 800 | dev_err(&dev->dev, "string descriptor 0 too short\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | err = -EINVAL; |
| 802 | goto errout; |
| 803 | } else { |
Alan Stern | ce36158 | 2006-11-20 11:12:22 -0500 | [diff] [blame] | 804 | dev->have_langid = 1; |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 805 | dev->string_langid = tbuf[2] | (tbuf[3] << 8); |
| 806 | /* always use the first langid listed */ |
| 807 | dev_dbg(&dev->dev, "default language 0x%04x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | dev->string_langid); |
| 809 | } |
| 810 | } |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | err = usb_string_sub(dev, dev->string_langid, index, tbuf); |
| 813 | if (err < 0) |
| 814 | goto errout; |
| 815 | |
| 816 | size--; /* leave room for trailing NULL char in output buffer */ |
| 817 | for (idx = 0, u = 2; u < err; u += 2) { |
| 818 | if (idx >= size) |
| 819 | break; |
| 820 | if (tbuf[u+1]) /* high byte */ |
| 821 | buf[idx++] = '?'; /* non ISO-8859-1 character */ |
| 822 | else |
| 823 | buf[idx++] = tbuf[u]; |
| 824 | } |
| 825 | buf[idx] = 0; |
| 826 | err = idx; |
| 827 | |
| 828 | if (tbuf[1] != USB_DT_STRING) |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 829 | dev_dbg(&dev->dev, |
| 830 | "wrong descriptor type %02x for string %d (\"%s\")\n", |
| 831 | tbuf[1], index, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | |
| 833 | errout: |
| 834 | kfree(tbuf); |
| 835 | return err; |
| 836 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 837 | EXPORT_SYMBOL_GPL(usb_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 839 | /** |
| 840 | * usb_cache_string - read a string descriptor and cache it for later use |
| 841 | * @udev: the device whose string descriptor is being read |
| 842 | * @index: the descriptor index |
| 843 | * |
| 844 | * Returns a pointer to a kmalloc'ed buffer containing the descriptor string, |
| 845 | * or NULL if the index is 0 or the string could not be read. |
| 846 | */ |
| 847 | char *usb_cache_string(struct usb_device *udev, int index) |
| 848 | { |
| 849 | char *buf; |
| 850 | char *smallbuf = NULL; |
| 851 | int len; |
| 852 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 853 | if (index <= 0) |
| 854 | return NULL; |
| 855 | |
| 856 | buf = kmalloc(256, GFP_KERNEL); |
| 857 | if (buf) { |
| 858 | len = usb_string(udev, index, buf, 256); |
| 859 | if (len > 0) { |
| 860 | smallbuf = kmalloc(++len, GFP_KERNEL); |
| 861 | if (!smallbuf) |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 862 | return buf; |
| 863 | memcpy(smallbuf, buf, len); |
| 864 | } |
| 865 | kfree(buf); |
| 866 | } |
| 867 | return smallbuf; |
| 868 | } |
| 869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | /* |
| 871 | * usb_get_device_descriptor - (re)reads the device descriptor (usbcore) |
| 872 | * @dev: the device whose device descriptor is being updated |
| 873 | * @size: how much of the descriptor to read |
| 874 | * Context: !in_interrupt () |
| 875 | * |
| 876 | * Updates the copy of the device descriptor stored in the device structure, |
Laurent Pinchart | 6ab16a9 | 2006-11-07 10:16:25 +0100 | [diff] [blame] | 877 | * which dedicates space for this purpose. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | * |
| 879 | * Not exported, only for use by the core. If drivers really want to read |
| 880 | * the device descriptor directly, they can call usb_get_descriptor() with |
| 881 | * type = USB_DT_DEVICE and index = 0. |
| 882 | * |
| 883 | * This call is synchronous, and may not be used in an interrupt context. |
| 884 | * |
| 885 | * Returns the number of bytes received on success, or else the status code |
| 886 | * returned by the underlying usb_control_msg() call. |
| 887 | */ |
| 888 | int usb_get_device_descriptor(struct usb_device *dev, unsigned int size) |
| 889 | { |
| 890 | struct usb_device_descriptor *desc; |
| 891 | int ret; |
| 892 | |
| 893 | if (size > sizeof(*desc)) |
| 894 | return -EINVAL; |
| 895 | desc = kmalloc(sizeof(*desc), GFP_NOIO); |
| 896 | if (!desc) |
| 897 | return -ENOMEM; |
| 898 | |
| 899 | ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size); |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 900 | if (ret >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | memcpy(&dev->descriptor, desc, size); |
| 902 | kfree(desc); |
| 903 | return ret; |
| 904 | } |
| 905 | |
| 906 | /** |
| 907 | * usb_get_status - issues a GET_STATUS call |
| 908 | * @dev: the device whose status is being checked |
| 909 | * @type: USB_RECIP_*; for device, interface, or endpoint |
| 910 | * @target: zero (for device), else interface or endpoint number |
| 911 | * @data: pointer to two bytes of bitmap data |
| 912 | * Context: !in_interrupt () |
| 913 | * |
| 914 | * Returns device, interface, or endpoint status. Normally only of |
| 915 | * interest to see if the device is self powered, or has enabled the |
| 916 | * remote wakeup facility; or whether a bulk or interrupt endpoint |
| 917 | * is halted ("stalled"). |
| 918 | * |
| 919 | * Bits in these status bitmaps are set using the SET_FEATURE request, |
| 920 | * and cleared using the CLEAR_FEATURE request. The usb_clear_halt() |
| 921 | * function should be used to clear halt ("stall") status. |
| 922 | * |
| 923 | * This call is synchronous, and may not be used in an interrupt context. |
| 924 | * |
| 925 | * Returns the number of bytes received on success, or else the status code |
| 926 | * returned by the underlying usb_control_msg() call. |
| 927 | */ |
| 928 | int usb_get_status(struct usb_device *dev, int type, int target, void *data) |
| 929 | { |
| 930 | int ret; |
| 931 | u16 *status = kmalloc(sizeof(*status), GFP_KERNEL); |
| 932 | |
| 933 | if (!status) |
| 934 | return -ENOMEM; |
| 935 | |
| 936 | ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 937 | USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status, |
| 938 | sizeof(*status), USB_CTRL_GET_TIMEOUT); |
| 939 | |
| 940 | *(u16 *)data = *status; |
| 941 | kfree(status); |
| 942 | return ret; |
| 943 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 944 | EXPORT_SYMBOL_GPL(usb_get_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
| 946 | /** |
| 947 | * usb_clear_halt - tells device to clear endpoint halt/stall condition |
| 948 | * @dev: device whose endpoint is halted |
| 949 | * @pipe: endpoint "pipe" being cleared |
| 950 | * Context: !in_interrupt () |
| 951 | * |
| 952 | * This is used to clear halt conditions for bulk and interrupt endpoints, |
| 953 | * as reported by URB completion status. Endpoints that are halted are |
| 954 | * sometimes referred to as being "stalled". Such endpoints are unable |
| 955 | * to transmit or receive data until the halt status is cleared. Any URBs |
| 956 | * queued for such an endpoint should normally be unlinked by the driver |
| 957 | * before clearing the halt condition, as described in sections 5.7.5 |
| 958 | * and 5.8.5 of the USB 2.0 spec. |
| 959 | * |
| 960 | * Note that control and isochronous endpoints don't halt, although control |
| 961 | * endpoints report "protocol stall" (for unsupported requests) using the |
| 962 | * same status code used to report a true stall. |
| 963 | * |
| 964 | * This call is synchronous, and may not be used in an interrupt context. |
| 965 | * |
| 966 | * Returns zero on success, or else the status code returned by the |
| 967 | * underlying usb_control_msg() call. |
| 968 | */ |
| 969 | int usb_clear_halt(struct usb_device *dev, int pipe) |
| 970 | { |
| 971 | int result; |
| 972 | int endp = usb_pipeendpoint(pipe); |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 973 | |
| 974 | if (usb_pipein(pipe)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | endp |= USB_DIR_IN; |
| 976 | |
| 977 | /* we don't care if it wasn't halted first. in fact some devices |
| 978 | * (like some ibmcam model 1 units) seem to expect hosts to make |
| 979 | * this request for iso endpoints, which can't halt! |
| 980 | */ |
| 981 | result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 982 | USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, |
| 983 | USB_ENDPOINT_HALT, endp, NULL, 0, |
| 984 | USB_CTRL_SET_TIMEOUT); |
| 985 | |
| 986 | /* don't un-halt or force to DATA0 except on success */ |
| 987 | if (result < 0) |
| 988 | return result; |
| 989 | |
| 990 | /* NOTE: seems like Microsoft and Apple don't bother verifying |
| 991 | * the clear "took", so some devices could lock up if you check... |
| 992 | * such as the Hagiwara FlashGate DUAL. So we won't bother. |
| 993 | * |
| 994 | * NOTE: make sure the logic here doesn't diverge much from |
| 995 | * the copy in usb-storage, for as long as we need two copies. |
| 996 | */ |
| 997 | |
| 998 | /* toggle was reset by the clear */ |
| 999 | usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0); |
| 1000 | |
| 1001 | return 0; |
| 1002 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 1003 | EXPORT_SYMBOL_GPL(usb_clear_halt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
| 1005 | /** |
| 1006 | * usb_disable_endpoint -- Disable an endpoint by address |
| 1007 | * @dev: the device whose endpoint is being disabled |
| 1008 | * @epaddr: the endpoint's address. Endpoint number for output, |
| 1009 | * endpoint number + USB_DIR_IN for input |
| 1010 | * |
| 1011 | * Deallocates hcd/hardware state for this endpoint ... and nukes all |
| 1012 | * pending urbs. |
| 1013 | * |
| 1014 | * If the HCD hasn't registered a disable() function, this sets the |
| 1015 | * endpoint's maxpacket size to 0 to prevent further submissions. |
| 1016 | */ |
| 1017 | void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr) |
| 1018 | { |
| 1019 | unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK; |
| 1020 | struct usb_host_endpoint *ep; |
| 1021 | |
| 1022 | if (!dev) |
| 1023 | return; |
| 1024 | |
| 1025 | if (usb_endpoint_out(epaddr)) { |
| 1026 | ep = dev->ep_out[epnum]; |
| 1027 | dev->ep_out[epnum] = NULL; |
| 1028 | } else { |
| 1029 | ep = dev->ep_in[epnum]; |
| 1030 | dev->ep_in[epnum] = NULL; |
| 1031 | } |
Alan Stern | bdd016b | 2007-07-30 17:05:22 -0400 | [diff] [blame] | 1032 | if (ep) { |
| 1033 | ep->enabled = 0; |
Alan Stern | 95cf82f | 2007-09-10 11:33:05 -0400 | [diff] [blame] | 1034 | usb_hcd_flush_endpoint(dev, ep); |
| 1035 | usb_hcd_disable_endpoint(dev, ep); |
Alan Stern | bdd016b | 2007-07-30 17:05:22 -0400 | [diff] [blame] | 1036 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | /** |
| 1040 | * usb_disable_interface -- Disable all endpoints for an interface |
| 1041 | * @dev: the device whose interface is being disabled |
| 1042 | * @intf: pointer to the interface descriptor |
| 1043 | * |
| 1044 | * Disables all the endpoints for the interface's current altsetting. |
| 1045 | */ |
| 1046 | void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf) |
| 1047 | { |
| 1048 | struct usb_host_interface *alt = intf->cur_altsetting; |
| 1049 | int i; |
| 1050 | |
| 1051 | for (i = 0; i < alt->desc.bNumEndpoints; ++i) { |
| 1052 | usb_disable_endpoint(dev, |
| 1053 | alt->endpoint[i].desc.bEndpointAddress); |
| 1054 | } |
| 1055 | } |
| 1056 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1057 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | * usb_disable_device - Disable all the endpoints for a USB device |
| 1059 | * @dev: the device whose endpoints are being disabled |
| 1060 | * @skip_ep0: 0 to disable endpoint 0, 1 to skip it. |
| 1061 | * |
| 1062 | * Disables all the device's endpoints, potentially including endpoint 0. |
| 1063 | * Deallocates hcd/hardware state for the endpoints (nuking all or most |
| 1064 | * pending urbs) and usbcore state for the interfaces, so that usbcore |
| 1065 | * must usb_set_configuration() before any interfaces could be used. |
| 1066 | */ |
| 1067 | void usb_disable_device(struct usb_device *dev, int skip_ep0) |
| 1068 | { |
| 1069 | int i; |
| 1070 | |
| 1071 | dev_dbg(&dev->dev, "%s nuking %s URBs\n", __FUNCTION__, |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1072 | skip_ep0 ? "non-ep0" : "all"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | for (i = skip_ep0; i < 16; ++i) { |
| 1074 | usb_disable_endpoint(dev, i); |
| 1075 | usb_disable_endpoint(dev, i + USB_DIR_IN); |
| 1076 | } |
| 1077 | dev->toggle[0] = dev->toggle[1] = 0; |
| 1078 | |
| 1079 | /* getting rid of interfaces will disconnect |
| 1080 | * any drivers bound to them (a key side effect) |
| 1081 | */ |
| 1082 | if (dev->actconfig) { |
| 1083 | for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { |
| 1084 | struct usb_interface *interface; |
| 1085 | |
Alan Stern | 86d3074 | 2005-07-29 12:17:16 -0700 | [diff] [blame] | 1086 | /* remove this interface if it has been registered */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | interface = dev->actconfig->interface[i]; |
Daniel Ritz | d305ef5 | 2005-09-22 00:47:24 -0700 | [diff] [blame] | 1088 | if (!device_is_registered(&interface->dev)) |
Alan Stern | 86d3074 | 2005-07-29 12:17:16 -0700 | [diff] [blame] | 1089 | continue; |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1090 | dev_dbg(&dev->dev, "unregistering interface %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | interface->dev.bus_id); |
| 1092 | usb_remove_sysfs_intf_files(interface); |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1093 | device_del(&interface->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | /* Now that the interfaces are unbound, nobody should |
| 1097 | * try to access them. |
| 1098 | */ |
| 1099 | for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1100 | put_device(&dev->actconfig->interface[i]->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | dev->actconfig->interface[i] = NULL; |
| 1102 | } |
| 1103 | dev->actconfig = NULL; |
| 1104 | if (dev->state == USB_STATE_CONFIGURED) |
| 1105 | usb_set_device_state(dev, USB_STATE_ADDRESS); |
| 1106 | } |
| 1107 | } |
| 1108 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1109 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | * usb_enable_endpoint - Enable an endpoint for USB communications |
| 1111 | * @dev: the device whose interface is being enabled |
| 1112 | * @ep: the endpoint |
| 1113 | * |
| 1114 | * Resets the endpoint toggle, and sets dev->ep_{in,out} pointers. |
| 1115 | * For control endpoints, both the input and output sides are handled. |
| 1116 | */ |
Alan Stern | bdd016b | 2007-07-30 17:05:22 -0400 | [diff] [blame] | 1117 | void usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | { |
Alan Stern | bdd016b | 2007-07-30 17:05:22 -0400 | [diff] [blame] | 1119 | int epnum = usb_endpoint_num(&ep->desc); |
| 1120 | int is_out = usb_endpoint_dir_out(&ep->desc); |
| 1121 | int is_control = usb_endpoint_xfer_control(&ep->desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | |
Alan Stern | bdd016b | 2007-07-30 17:05:22 -0400 | [diff] [blame] | 1123 | if (is_out || is_control) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | usb_settoggle(dev, epnum, 1, 0); |
| 1125 | dev->ep_out[epnum] = ep; |
| 1126 | } |
Alan Stern | bdd016b | 2007-07-30 17:05:22 -0400 | [diff] [blame] | 1127 | if (!is_out || is_control) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | usb_settoggle(dev, epnum, 0, 0); |
| 1129 | dev->ep_in[epnum] = ep; |
| 1130 | } |
Alan Stern | bdd016b | 2007-07-30 17:05:22 -0400 | [diff] [blame] | 1131 | ep->enabled = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1134 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | * usb_enable_interface - Enable all the endpoints for an interface |
| 1136 | * @dev: the device whose interface is being enabled |
| 1137 | * @intf: pointer to the interface descriptor |
| 1138 | * |
| 1139 | * Enables all the endpoints for the interface's current altsetting. |
| 1140 | */ |
| 1141 | static void usb_enable_interface(struct usb_device *dev, |
| 1142 | struct usb_interface *intf) |
| 1143 | { |
| 1144 | struct usb_host_interface *alt = intf->cur_altsetting; |
| 1145 | int i; |
| 1146 | |
| 1147 | for (i = 0; i < alt->desc.bNumEndpoints; ++i) |
| 1148 | usb_enable_endpoint(dev, &alt->endpoint[i]); |
| 1149 | } |
| 1150 | |
| 1151 | /** |
| 1152 | * usb_set_interface - Makes a particular alternate setting be current |
| 1153 | * @dev: the device whose interface is being updated |
| 1154 | * @interface: the interface being updated |
| 1155 | * @alternate: the setting being chosen. |
| 1156 | * Context: !in_interrupt () |
| 1157 | * |
| 1158 | * This is used to enable data transfers on interfaces that may not |
| 1159 | * be enabled by default. Not all devices support such configurability. |
| 1160 | * Only the driver bound to an interface may change its setting. |
| 1161 | * |
| 1162 | * Within any given configuration, each interface may have several |
| 1163 | * alternative settings. These are often used to control levels of |
| 1164 | * bandwidth consumption. For example, the default setting for a high |
| 1165 | * speed interrupt endpoint may not send more than 64 bytes per microframe, |
| 1166 | * while interrupt transfers of up to 3KBytes per microframe are legal. |
| 1167 | * Also, isochronous endpoints may never be part of an |
| 1168 | * interface's default setting. To access such bandwidth, alternate |
| 1169 | * interface settings must be made current. |
| 1170 | * |
| 1171 | * Note that in the Linux USB subsystem, bandwidth associated with |
| 1172 | * an endpoint in a given alternate setting is not reserved until an URB |
| 1173 | * is submitted that needs that bandwidth. Some other operating systems |
| 1174 | * allocate bandwidth early, when a configuration is chosen. |
| 1175 | * |
| 1176 | * This call is synchronous, and may not be used in an interrupt context. |
| 1177 | * Also, drivers must not change altsettings while urbs are scheduled for |
| 1178 | * endpoints in that interface; all such urbs must first be completed |
| 1179 | * (perhaps forced by unlinking). |
| 1180 | * |
| 1181 | * Returns zero on success, or else the status code returned by the |
| 1182 | * underlying usb_control_msg() call. |
| 1183 | */ |
| 1184 | int usb_set_interface(struct usb_device *dev, int interface, int alternate) |
| 1185 | { |
| 1186 | struct usb_interface *iface; |
| 1187 | struct usb_host_interface *alt; |
| 1188 | int ret; |
| 1189 | int manual = 0; |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1190 | unsigned int epaddr; |
| 1191 | unsigned int pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | |
| 1193 | if (dev->state == USB_STATE_SUSPENDED) |
| 1194 | return -EHOSTUNREACH; |
| 1195 | |
| 1196 | iface = usb_ifnum_to_if(dev, interface); |
| 1197 | if (!iface) { |
| 1198 | dev_dbg(&dev->dev, "selecting invalid interface %d\n", |
| 1199 | interface); |
| 1200 | return -EINVAL; |
| 1201 | } |
| 1202 | |
| 1203 | alt = usb_altnum_to_altsetting(iface, alternate); |
| 1204 | if (!alt) { |
| 1205 | warn("selecting invalid altsetting %d", alternate); |
| 1206 | return -EINVAL; |
| 1207 | } |
| 1208 | |
| 1209 | ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 1210 | USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE, |
| 1211 | alternate, interface, NULL, 0, 5000); |
| 1212 | |
| 1213 | /* 9.4.10 says devices don't need this and are free to STALL the |
| 1214 | * request if the interface only has one alternate setting. |
| 1215 | */ |
| 1216 | if (ret == -EPIPE && iface->num_altsetting == 1) { |
| 1217 | dev_dbg(&dev->dev, |
| 1218 | "manual set_interface for iface %d, alt %d\n", |
| 1219 | interface, alternate); |
| 1220 | manual = 1; |
| 1221 | } else if (ret < 0) |
| 1222 | return ret; |
| 1223 | |
| 1224 | /* FIXME drivers shouldn't need to replicate/bugfix the logic here |
| 1225 | * when they implement async or easily-killable versions of this or |
| 1226 | * other "should-be-internal" functions (like clear_halt). |
| 1227 | * should hcd+usbcore postprocess control requests? |
| 1228 | */ |
| 1229 | |
| 1230 | /* prevent submissions using previous endpoint settings */ |
Alan Stern | 7e61559 | 2007-11-06 11:43:42 -0500 | [diff] [blame] | 1231 | if (iface->cur_altsetting != alt && device_is_registered(&iface->dev)) |
Alan Stern | 0e6c8e8 | 2005-10-24 15:33:03 -0400 | [diff] [blame] | 1232 | usb_remove_sysfs_intf_files(iface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | usb_disable_interface(dev, iface); |
| 1234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | iface->cur_altsetting = alt; |
| 1236 | |
| 1237 | /* If the interface only has one altsetting and the device didn't |
David Brownell | a81e7ec | 2005-04-18 17:39:25 -0700 | [diff] [blame] | 1238 | * accept the request, we attempt to carry out the equivalent action |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | * by manually clearing the HALT feature for each endpoint in the |
| 1240 | * new altsetting. |
| 1241 | */ |
| 1242 | if (manual) { |
| 1243 | int i; |
| 1244 | |
| 1245 | for (i = 0; i < alt->desc.bNumEndpoints; i++) { |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1246 | epaddr = alt->endpoint[i].desc.bEndpointAddress; |
| 1247 | pipe = __create_pipe(dev, |
| 1248 | USB_ENDPOINT_NUMBER_MASK & epaddr) | |
| 1249 | (usb_endpoint_out(epaddr) ? |
| 1250 | USB_DIR_OUT : USB_DIR_IN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | |
| 1252 | usb_clear_halt(dev, pipe); |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | /* 9.1.1.5: reset toggles for all endpoints in the new altsetting |
| 1257 | * |
| 1258 | * Note: |
| 1259 | * Despite EP0 is always present in all interfaces/AS, the list of |
| 1260 | * endpoints from the descriptor does not contain EP0. Due to its |
| 1261 | * omnipresence one might expect EP0 being considered "affected" by |
| 1262 | * any SetInterface request and hence assume toggles need to be reset. |
| 1263 | * However, EP0 toggles are re-synced for every individual transfer |
| 1264 | * during the SETUP stage - hence EP0 toggles are "don't care" here. |
| 1265 | * (Likewise, EP0 never "halts" on well designed devices.) |
| 1266 | */ |
| 1267 | usb_enable_interface(dev, iface); |
Alan Stern | 7e61559 | 2007-11-06 11:43:42 -0500 | [diff] [blame] | 1268 | if (device_is_registered(&iface->dev)) |
Alan Stern | 0e6c8e8 | 2005-10-24 15:33:03 -0400 | [diff] [blame] | 1269 | usb_create_sysfs_intf_files(iface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | |
| 1271 | return 0; |
| 1272 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 1273 | EXPORT_SYMBOL_GPL(usb_set_interface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | |
| 1275 | /** |
| 1276 | * usb_reset_configuration - lightweight device reset |
| 1277 | * @dev: the device whose configuration is being reset |
| 1278 | * |
| 1279 | * This issues a standard SET_CONFIGURATION request to the device using |
| 1280 | * the current configuration. The effect is to reset most USB-related |
| 1281 | * state in the device, including interface altsettings (reset to zero), |
| 1282 | * endpoint halts (cleared), and data toggle (only for bulk and interrupt |
| 1283 | * endpoints). Other usbcore state is unchanged, including bindings of |
| 1284 | * usb device drivers to interfaces. |
| 1285 | * |
| 1286 | * Because this affects multiple interfaces, avoid using this with composite |
| 1287 | * (multi-interface) devices. Instead, the driver for each interface may |
David Brownell | a81e7ec | 2005-04-18 17:39:25 -0700 | [diff] [blame] | 1288 | * use usb_set_interface() on the interfaces it claims. Be careful though; |
| 1289 | * some devices don't support the SET_INTERFACE request, and others won't |
| 1290 | * reset all the interface state (notably data toggles). Resetting the whole |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | * configuration would affect other drivers' interfaces. |
| 1292 | * |
| 1293 | * The caller must own the device lock. |
| 1294 | * |
| 1295 | * Returns zero on success, else a negative error code. |
| 1296 | */ |
| 1297 | int usb_reset_configuration(struct usb_device *dev) |
| 1298 | { |
| 1299 | int i, retval; |
| 1300 | struct usb_host_config *config; |
| 1301 | |
| 1302 | if (dev->state == USB_STATE_SUSPENDED) |
| 1303 | return -EHOSTUNREACH; |
| 1304 | |
| 1305 | /* caller must have locked the device and must own |
| 1306 | * the usb bus readlock (so driver bindings are stable); |
| 1307 | * calls during probe() are fine |
| 1308 | */ |
| 1309 | |
| 1310 | for (i = 1; i < 16; ++i) { |
| 1311 | usb_disable_endpoint(dev, i); |
| 1312 | usb_disable_endpoint(dev, i + USB_DIR_IN); |
| 1313 | } |
| 1314 | |
| 1315 | config = dev->actconfig; |
| 1316 | retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 1317 | USB_REQ_SET_CONFIGURATION, 0, |
| 1318 | config->desc.bConfigurationValue, 0, |
| 1319 | NULL, 0, USB_CTRL_SET_TIMEOUT); |
Alan Stern | 0e6c8e8 | 2005-10-24 15:33:03 -0400 | [diff] [blame] | 1320 | if (retval < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | |
| 1323 | dev->toggle[0] = dev->toggle[1] = 0; |
| 1324 | |
| 1325 | /* re-init hc/hcd interface/endpoint state */ |
| 1326 | for (i = 0; i < config->desc.bNumInterfaces; i++) { |
| 1327 | struct usb_interface *intf = config->interface[i]; |
| 1328 | struct usb_host_interface *alt; |
| 1329 | |
Alan Stern | 0e6c8e8 | 2005-10-24 15:33:03 -0400 | [diff] [blame] | 1330 | if (device_is_registered(&intf->dev)) |
| 1331 | usb_remove_sysfs_intf_files(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | alt = usb_altnum_to_altsetting(intf, 0); |
| 1333 | |
| 1334 | /* No altsetting 0? We'll assume the first altsetting. |
| 1335 | * We could use a GetInterface call, but if a device is |
| 1336 | * so non-compliant that it doesn't have altsetting 0 |
| 1337 | * then I wouldn't trust its reply anyway. |
| 1338 | */ |
| 1339 | if (!alt) |
| 1340 | alt = &intf->altsetting[0]; |
| 1341 | |
| 1342 | intf->cur_altsetting = alt; |
| 1343 | usb_enable_interface(dev, intf); |
Alan Stern | 0e6c8e8 | 2005-10-24 15:33:03 -0400 | [diff] [blame] | 1344 | if (device_is_registered(&intf->dev)) |
| 1345 | usb_create_sysfs_intf_files(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | } |
| 1347 | return 0; |
| 1348 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 1349 | EXPORT_SYMBOL_GPL(usb_reset_configuration); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | |
Greg Kroah-Hartman | b0e396e | 2007-08-02 22:44:27 -0600 | [diff] [blame] | 1351 | static void usb_release_interface(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | { |
| 1353 | struct usb_interface *intf = to_usb_interface(dev); |
| 1354 | struct usb_interface_cache *intfc = |
| 1355 | altsetting_to_usb_interface_cache(intf->altsetting); |
| 1356 | |
| 1357 | kref_put(&intfc->ref, usb_release_interface_cache); |
| 1358 | kfree(intf); |
| 1359 | } |
| 1360 | |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1361 | #ifdef CONFIG_HOTPLUG |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1362 | static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env) |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1363 | { |
| 1364 | struct usb_device *usb_dev; |
| 1365 | struct usb_interface *intf; |
| 1366 | struct usb_host_interface *alt; |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1367 | |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1368 | intf = to_usb_interface(dev); |
| 1369 | usb_dev = interface_to_usbdev(intf); |
| 1370 | alt = intf->cur_altsetting; |
| 1371 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1372 | if (add_uevent_var(env, "INTERFACE=%d/%d/%d", |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1373 | alt->desc.bInterfaceClass, |
| 1374 | alt->desc.bInterfaceSubClass, |
| 1375 | alt->desc.bInterfaceProtocol)) |
| 1376 | return -ENOMEM; |
| 1377 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1378 | if (add_uevent_var(env, |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1379 | "MODALIAS=usb:" |
| 1380 | "v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X", |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1381 | le16_to_cpu(usb_dev->descriptor.idVendor), |
| 1382 | le16_to_cpu(usb_dev->descriptor.idProduct), |
| 1383 | le16_to_cpu(usb_dev->descriptor.bcdDevice), |
| 1384 | usb_dev->descriptor.bDeviceClass, |
| 1385 | usb_dev->descriptor.bDeviceSubClass, |
| 1386 | usb_dev->descriptor.bDeviceProtocol, |
| 1387 | alt->desc.bInterfaceClass, |
| 1388 | alt->desc.bInterfaceSubClass, |
| 1389 | alt->desc.bInterfaceProtocol)) |
| 1390 | return -ENOMEM; |
| 1391 | |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1392 | return 0; |
| 1393 | } |
| 1394 | |
| 1395 | #else |
| 1396 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1397 | static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env) |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1398 | { |
| 1399 | return -ENODEV; |
| 1400 | } |
| 1401 | #endif /* CONFIG_HOTPLUG */ |
| 1402 | |
| 1403 | struct device_type usb_if_device_type = { |
| 1404 | .name = "usb_interface", |
| 1405 | .release = usb_release_interface, |
| 1406 | .uevent = usb_if_uevent, |
| 1407 | }; |
| 1408 | |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 1409 | static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev, |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1410 | struct usb_host_config *config, |
| 1411 | u8 inum) |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 1412 | { |
| 1413 | struct usb_interface_assoc_descriptor *retval = NULL; |
| 1414 | struct usb_interface_assoc_descriptor *intf_assoc; |
| 1415 | int first_intf; |
| 1416 | int last_intf; |
| 1417 | int i; |
| 1418 | |
| 1419 | for (i = 0; (i < USB_MAXIADS && config->intf_assoc[i]); i++) { |
| 1420 | intf_assoc = config->intf_assoc[i]; |
| 1421 | if (intf_assoc->bInterfaceCount == 0) |
| 1422 | continue; |
| 1423 | |
| 1424 | first_intf = intf_assoc->bFirstInterface; |
| 1425 | last_intf = first_intf + (intf_assoc->bInterfaceCount - 1); |
| 1426 | if (inum >= first_intf && inum <= last_intf) { |
| 1427 | if (!retval) |
| 1428 | retval = intf_assoc; |
| 1429 | else |
| 1430 | dev_err(&dev->dev, "Interface #%d referenced" |
| 1431 | " by multiple IADs\n", inum); |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | return retval; |
| 1436 | } |
| 1437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | /* |
| 1439 | * usb_set_configuration - Makes a particular device setting be current |
| 1440 | * @dev: the device whose configuration is being updated |
| 1441 | * @configuration: the configuration being chosen. |
| 1442 | * Context: !in_interrupt(), caller owns the device lock |
| 1443 | * |
| 1444 | * This is used to enable non-default device modes. Not all devices |
| 1445 | * use this kind of configurability; many devices only have one |
| 1446 | * configuration. |
| 1447 | * |
Alan Stern | 3f141e2 | 2007-02-08 16:40:43 -0500 | [diff] [blame] | 1448 | * @configuration is the value of the configuration to be installed. |
| 1449 | * According to the USB spec (e.g. section 9.1.1.5), configuration values |
| 1450 | * must be non-zero; a value of zero indicates that the device in |
| 1451 | * unconfigured. However some devices erroneously use 0 as one of their |
| 1452 | * configuration values. To help manage such devices, this routine will |
| 1453 | * accept @configuration = -1 as indicating the device should be put in |
| 1454 | * an unconfigured state. |
| 1455 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | * USB device configurations may affect Linux interoperability, |
| 1457 | * power consumption and the functionality available. For example, |
| 1458 | * the default configuration is limited to using 100mA of bus power, |
| 1459 | * so that when certain device functionality requires more power, |
| 1460 | * and the device is bus powered, that functionality should be in some |
| 1461 | * non-default device configuration. Other device modes may also be |
| 1462 | * reflected as configuration options, such as whether two ISDN |
| 1463 | * channels are available independently; and choosing between open |
| 1464 | * standard device protocols (like CDC) or proprietary ones. |
| 1465 | * |
Inaky Perez-Gonzalez | 16bbab2 | 2007-07-31 20:34:01 -0700 | [diff] [blame] | 1466 | * Note that a non-authorized device (dev->authorized == 0) will only |
| 1467 | * be put in unconfigured mode. |
| 1468 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | * Note that USB has an additional level of device configurability, |
| 1470 | * associated with interfaces. That configurability is accessed using |
| 1471 | * usb_set_interface(). |
| 1472 | * |
| 1473 | * This call is synchronous. The calling context must be able to sleep, |
| 1474 | * must own the device lock, and must not hold the driver model's USB |
Greg Kroah-Hartman | 341487a8 | 2007-04-09 11:52:31 -0400 | [diff] [blame] | 1475 | * bus mutex; usb device driver probe() methods cannot use this routine. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | * |
| 1477 | * Returns zero on success, or else the status code returned by the |
Steven Cole | 093cf72 | 2005-05-03 19:07:24 -0600 | [diff] [blame] | 1478 | * underlying call that failed. On successful completion, each interface |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | * in the original device configuration has been destroyed, and each one |
| 1480 | * in the new configuration has been probed by all relevant usb device |
| 1481 | * drivers currently known to the kernel. |
| 1482 | */ |
| 1483 | int usb_set_configuration(struct usb_device *dev, int configuration) |
| 1484 | { |
| 1485 | int i, ret; |
| 1486 | struct usb_host_config *cp = NULL; |
| 1487 | struct usb_interface **new_interfaces = NULL; |
| 1488 | int n, nintf; |
| 1489 | |
Inaky Perez-Gonzalez | 16bbab2 | 2007-07-31 20:34:01 -0700 | [diff] [blame] | 1490 | if (dev->authorized == 0 || configuration == -1) |
Alan Stern | 3f141e2 | 2007-02-08 16:40:43 -0500 | [diff] [blame] | 1491 | configuration = 0; |
| 1492 | else { |
| 1493 | for (i = 0; i < dev->descriptor.bNumConfigurations; i++) { |
| 1494 | if (dev->config[i].desc.bConfigurationValue == |
| 1495 | configuration) { |
| 1496 | cp = &dev->config[i]; |
| 1497 | break; |
| 1498 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | } |
| 1500 | } |
| 1501 | if ((!cp && configuration != 0)) |
| 1502 | return -EINVAL; |
| 1503 | |
| 1504 | /* The USB spec says configuration 0 means unconfigured. |
| 1505 | * But if a device includes a configuration numbered 0, |
| 1506 | * we will accept it as a correctly configured state. |
Alan Stern | 3f141e2 | 2007-02-08 16:40:43 -0500 | [diff] [blame] | 1507 | * Use -1 if you really want to unconfigure the device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | */ |
| 1509 | if (cp && configuration == 0) |
| 1510 | dev_warn(&dev->dev, "config 0 descriptor??\n"); |
| 1511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | /* Allocate memory for new interfaces before doing anything else, |
| 1513 | * so that if we run out then nothing will have changed. */ |
| 1514 | n = nintf = 0; |
| 1515 | if (cp) { |
| 1516 | nintf = cp->desc.bNumInterfaces; |
| 1517 | new_interfaces = kmalloc(nintf * sizeof(*new_interfaces), |
| 1518 | GFP_KERNEL); |
| 1519 | if (!new_interfaces) { |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 1520 | dev_err(&dev->dev, "Out of memory\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | return -ENOMEM; |
| 1522 | } |
| 1523 | |
| 1524 | for (; n < nintf; ++n) { |
Alan Stern | 0a1ef3b | 2005-10-24 15:38:24 -0400 | [diff] [blame] | 1525 | new_interfaces[n] = kzalloc( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | sizeof(struct usb_interface), |
| 1527 | GFP_KERNEL); |
| 1528 | if (!new_interfaces[n]) { |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 1529 | dev_err(&dev->dev, "Out of memory\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | ret = -ENOMEM; |
| 1531 | free_interfaces: |
| 1532 | while (--n >= 0) |
| 1533 | kfree(new_interfaces[n]); |
| 1534 | kfree(new_interfaces); |
| 1535 | return ret; |
| 1536 | } |
| 1537 | } |
Alan Stern | 6ad0712 | 2006-06-01 13:59:16 -0400 | [diff] [blame] | 1538 | |
| 1539 | i = dev->bus_mA - cp->desc.bMaxPower * 2; |
| 1540 | if (i < 0) |
| 1541 | dev_warn(&dev->dev, "new config #%d exceeds power " |
| 1542 | "limit by %dmA\n", |
| 1543 | configuration, -i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
Alan Stern | 01d883d | 2006-08-30 15:47:18 -0400 | [diff] [blame] | 1546 | /* Wake up the device so we can send it the Set-Config request */ |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1547 | ret = usb_autoresume_device(dev); |
Alan Stern | 01d883d | 2006-08-30 15:47:18 -0400 | [diff] [blame] | 1548 | if (ret) |
| 1549 | goto free_interfaces; |
| 1550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | /* if it's already configured, clear out old state first. |
| 1552 | * getting rid of old interfaces means unbinding their drivers. |
| 1553 | */ |
| 1554 | if (dev->state != USB_STATE_ADDRESS) |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1555 | usb_disable_device(dev, 1); /* Skip ep0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1557 | ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 1558 | USB_REQ_SET_CONFIGURATION, 0, configuration, 0, |
| 1559 | NULL, 0, USB_CTRL_SET_TIMEOUT); |
| 1560 | if (ret < 0) { |
Alan Stern | 6ad0712 | 2006-06-01 13:59:16 -0400 | [diff] [blame] | 1561 | /* All the old state is gone, so what else can we do? |
| 1562 | * The device is probably useless now anyway. |
| 1563 | */ |
| 1564 | cp = NULL; |
| 1565 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | |
| 1567 | dev->actconfig = cp; |
Alan Stern | 6ad0712 | 2006-06-01 13:59:16 -0400 | [diff] [blame] | 1568 | if (!cp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | usb_set_device_state(dev, USB_STATE_ADDRESS); |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1570 | usb_autosuspend_device(dev); |
Alan Stern | 6ad0712 | 2006-06-01 13:59:16 -0400 | [diff] [blame] | 1571 | goto free_interfaces; |
| 1572 | } |
| 1573 | usb_set_device_state(dev, USB_STATE_CONFIGURED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | |
Alan Stern | 6ad0712 | 2006-06-01 13:59:16 -0400 | [diff] [blame] | 1575 | /* Initialize the new interface structures and the |
| 1576 | * hc/hcd/usbcore interface/endpoint state. |
| 1577 | */ |
| 1578 | for (i = 0; i < nintf; ++i) { |
| 1579 | struct usb_interface_cache *intfc; |
| 1580 | struct usb_interface *intf; |
| 1581 | struct usb_host_interface *alt; |
| 1582 | |
| 1583 | cp->interface[i] = intf = new_interfaces[i]; |
| 1584 | intfc = cp->intf_cache[i]; |
| 1585 | intf->altsetting = intfc->altsetting; |
| 1586 | intf->num_altsetting = intfc->num_altsetting; |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 1587 | intf->intf_assoc = find_iad(dev, cp, i); |
Alan Stern | 6ad0712 | 2006-06-01 13:59:16 -0400 | [diff] [blame] | 1588 | kref_get(&intfc->ref); |
| 1589 | |
| 1590 | alt = usb_altnum_to_altsetting(intf, 0); |
| 1591 | |
| 1592 | /* No altsetting 0? We'll assume the first altsetting. |
| 1593 | * We could use a GetInterface call, but if a device is |
| 1594 | * so non-compliant that it doesn't have altsetting 0 |
| 1595 | * then I wouldn't trust its reply anyway. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | */ |
Alan Stern | 6ad0712 | 2006-06-01 13:59:16 -0400 | [diff] [blame] | 1597 | if (!alt) |
| 1598 | alt = &intf->altsetting[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | |
Alan Stern | 6ad0712 | 2006-06-01 13:59:16 -0400 | [diff] [blame] | 1600 | intf->cur_altsetting = alt; |
| 1601 | usb_enable_interface(dev, intf); |
| 1602 | intf->dev.parent = &dev->dev; |
| 1603 | intf->dev.driver = NULL; |
| 1604 | intf->dev.bus = &usb_bus_type; |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1605 | intf->dev.type = &usb_if_device_type; |
Alan Stern | 6ad0712 | 2006-06-01 13:59:16 -0400 | [diff] [blame] | 1606 | intf->dev.dma_mask = dev->dev.dma_mask; |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1607 | device_initialize(&intf->dev); |
Alan Stern | 6ad0712 | 2006-06-01 13:59:16 -0400 | [diff] [blame] | 1608 | mark_quiesced(intf); |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1609 | sprintf(&intf->dev.bus_id[0], "%d-%s:%d.%d", |
| 1610 | dev->bus->busnum, dev->devpath, |
| 1611 | configuration, alt->desc.bInterfaceNumber); |
Alan Stern | 6ad0712 | 2006-06-01 13:59:16 -0400 | [diff] [blame] | 1612 | } |
| 1613 | kfree(new_interfaces); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | |
Alan Stern | 6ad0712 | 2006-06-01 13:59:16 -0400 | [diff] [blame] | 1615 | if (cp->string == NULL) |
| 1616 | cp->string = usb_cache_string(dev, cp->desc.iConfiguration); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | |
Alan Stern | 6ad0712 | 2006-06-01 13:59:16 -0400 | [diff] [blame] | 1618 | /* Now that all the interfaces are set up, register them |
| 1619 | * to trigger binding of drivers to interfaces. probe() |
| 1620 | * routines may install different altsettings and may |
| 1621 | * claim() any interfaces not yet bound. Many class drivers |
| 1622 | * need that: CDC, audio, video, etc. |
| 1623 | */ |
| 1624 | for (i = 0; i < nintf; ++i) { |
| 1625 | struct usb_interface *intf = cp->interface[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1627 | dev_dbg(&dev->dev, |
Alan Stern | 6ad0712 | 2006-06-01 13:59:16 -0400 | [diff] [blame] | 1628 | "adding %s (config #%d, interface %d)\n", |
| 1629 | intf->dev.bus_id, configuration, |
| 1630 | intf->cur_altsetting->desc.bInterfaceNumber); |
Greg Kroah-Hartman | 3e35bf3 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1631 | ret = device_add(&intf->dev); |
Alan Stern | 6ad0712 | 2006-06-01 13:59:16 -0400 | [diff] [blame] | 1632 | if (ret != 0) { |
| 1633 | dev_err(&dev->dev, "device_add(%s) --> %d\n", |
| 1634 | intf->dev.bus_id, ret); |
| 1635 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | } |
Alan Stern | 439a903 | 2007-10-19 09:51:58 -0400 | [diff] [blame] | 1637 | usb_create_sysfs_intf_files(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | } |
| 1639 | |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1640 | usb_autosuspend_device(dev); |
Alan Stern | 86d3074 | 2005-07-29 12:17:16 -0700 | [diff] [blame] | 1641 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
Alan Stern | 088dc27 | 2006-08-21 12:08:19 -0400 | [diff] [blame] | 1644 | struct set_config_request { |
| 1645 | struct usb_device *udev; |
| 1646 | int config; |
| 1647 | struct work_struct work; |
| 1648 | }; |
| 1649 | |
| 1650 | /* Worker routine for usb_driver_set_configuration() */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1651 | static void driver_set_config_work(struct work_struct *work) |
Alan Stern | 088dc27 | 2006-08-21 12:08:19 -0400 | [diff] [blame] | 1652 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1653 | struct set_config_request *req = |
| 1654 | container_of(work, struct set_config_request, work); |
Alan Stern | 088dc27 | 2006-08-21 12:08:19 -0400 | [diff] [blame] | 1655 | |
| 1656 | usb_lock_device(req->udev); |
| 1657 | usb_set_configuration(req->udev, req->config); |
| 1658 | usb_unlock_device(req->udev); |
| 1659 | usb_put_dev(req->udev); |
| 1660 | kfree(req); |
| 1661 | } |
| 1662 | |
| 1663 | /** |
| 1664 | * usb_driver_set_configuration - Provide a way for drivers to change device configurations |
| 1665 | * @udev: the device whose configuration is being updated |
| 1666 | * @config: the configuration being chosen. |
| 1667 | * Context: In process context, must be able to sleep |
| 1668 | * |
| 1669 | * Device interface drivers are not allowed to change device configurations. |
| 1670 | * This is because changing configurations will destroy the interface the |
| 1671 | * driver is bound to and create new ones; it would be like a floppy-disk |
| 1672 | * driver telling the computer to replace the floppy-disk drive with a |
| 1673 | * tape drive! |
| 1674 | * |
| 1675 | * Still, in certain specialized circumstances the need may arise. This |
| 1676 | * routine gets around the normal restrictions by using a work thread to |
| 1677 | * submit the change-config request. |
| 1678 | * |
| 1679 | * Returns 0 if the request was succesfully queued, error code otherwise. |
| 1680 | * The caller has no way to know whether the queued request will eventually |
| 1681 | * succeed. |
| 1682 | */ |
| 1683 | int usb_driver_set_configuration(struct usb_device *udev, int config) |
| 1684 | { |
| 1685 | struct set_config_request *req; |
| 1686 | |
| 1687 | req = kmalloc(sizeof(*req), GFP_KERNEL); |
| 1688 | if (!req) |
| 1689 | return -ENOMEM; |
| 1690 | req->udev = udev; |
| 1691 | req->config = config; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1692 | INIT_WORK(&req->work, driver_set_config_work); |
Alan Stern | 088dc27 | 2006-08-21 12:08:19 -0400 | [diff] [blame] | 1693 | |
| 1694 | usb_get_dev(udev); |
Alan Stern | 1737bf2 | 2006-12-15 16:04:52 -0500 | [diff] [blame] | 1695 | schedule_work(&req->work); |
Alan Stern | 088dc27 | 2006-08-21 12:08:19 -0400 | [diff] [blame] | 1696 | return 0; |
| 1697 | } |
| 1698 | EXPORT_SYMBOL_GPL(usb_driver_set_configuration); |