blob: a73e08fdab36336fee6f14bacef1c7dd1ec58764 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * message.c - synchronous message handling
3 */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#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>
Clemens Ladischa853a3d2009-04-24 10:12:18 +020013#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/device.h>
Ralf Baechle11763602007-10-23 20:42:11 +020015#include <linux/scatterlist.h>
Oliver Neukum7ceec1f2007-01-26 14:26:21 +010016#include <linux/usb/quirks.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020017#include <linux/usb/hcd.h> /* for usbcore internals */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/byteorder.h>
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "usb.h"
21
Alan Sterndf718962008-12-19 10:27:56 -050022static void cancel_async_set_config(struct usb_device *udev);
23
Alan Stern67f5dde2007-07-24 18:23:23 -040024struct api_context {
25 struct completion done;
26 int status;
27};
28
David Howells7d12e782006-10-05 14:55:46 +010029static void usb_api_blocking_completion(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Alan Stern67f5dde2007-07-24 18:23:23 -040031 struct api_context *ctx = urb->context;
32
33 ctx->status = urb->status;
34 complete(&ctx->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
37
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020038/*
39 * Starts urb and waits for completion or timeout. Note that this call
40 * is NOT interruptible. Many device driver i/o requests should be
41 * interruptible and therefore these drivers should implement their
42 * own interruptible routines.
43 */
44static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -080045{
Alan Stern67f5dde2007-07-24 18:23:23 -040046 struct api_context ctx;
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020047 unsigned long expire;
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -070048 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Alan Stern67f5dde2007-07-24 18:23:23 -040050 init_completion(&ctx.done);
51 urb->context = &ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 urb->actual_length = 0;
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -070053 retval = usb_submit_urb(urb, GFP_NOIO);
54 if (unlikely(retval))
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020055 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020057 expire = timeout ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT;
Alan Stern67f5dde2007-07-24 18:23:23 -040058 if (!wait_for_completion_timeout(&ctx.done, expire)) {
59 usb_kill_urb(urb);
60 retval = (ctx.status == -ENOENT ? -ETIMEDOUT : ctx.status);
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020061
62 dev_dbg(&urb->dev->dev,
Roel Kluin71d27182009-03-13 12:19:18 +010063 "%s timed out on ep%d%s len=%u/%u\n",
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020064 current->comm,
Alan Stern5e60a162007-07-30 17:07:21 -040065 usb_endpoint_num(&urb->ep->desc),
66 usb_urb_dir_in(urb) ? "in" : "out",
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020067 urb->actual_length,
68 urb->transfer_buffer_length);
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020069 } else
Alan Stern67f5dde2007-07-24 18:23:23 -040070 retval = ctx.status;
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020071out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (actual_length)
73 *actual_length = urb->actual_length;
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 usb_free_urb(urb);
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -070076 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79/*-------------------------------------------------------------------*/
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -080080/* returns status (negative) or length (positive) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static int usb_internal_control_msg(struct usb_device *usb_dev,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -080082 unsigned int pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 struct usb_ctrlrequest *cmd,
84 void *data, int len, int timeout)
85{
86 struct urb *urb;
87 int retv;
88 int length;
89
90 urb = usb_alloc_urb(0, GFP_NOIO);
91 if (!urb)
92 return -ENOMEM;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -080093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char *)cmd, data,
95 len, usb_api_blocking_completion, NULL);
96
97 retv = usb_start_wait_urb(urb, timeout, &length);
98 if (retv < 0)
99 return retv;
100 else
101 return length;
102}
103
104/**
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800105 * usb_control_msg - Builds a control urb, sends it off and waits for completion
106 * @dev: pointer to the usb device to send the message to
107 * @pipe: endpoint "pipe" to send the message to
108 * @request: USB message request value
109 * @requesttype: USB message request type value
110 * @value: USB message value
111 * @index: USB message index value
112 * @data: pointer to the data to send
113 * @size: length in bytes of the data to send
114 * @timeout: time in msecs to wait for the message to complete before timing
115 * out (if 0 the wait is forever)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800117 * Context: !in_interrupt ()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800119 * This function sends a simple control message to a specified endpoint and
120 * waits for the message to complete, or timeout.
121 *
122 * If successful, it returns the number of bytes transferred, otherwise a
123 * negative error number.
124 *
125 * Don't use this function from within an interrupt context, like a bottom half
126 * handler. If you need an asynchronous message, or need to send a message
127 * from within interrupt context, use usb_submit_urb().
128 * If a thread in your driver uses this call, make sure your disconnect()
129 * method can wait for it to complete. Since you don't have a handle on the
130 * URB used, you can't cancel the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800132int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
133 __u8 requesttype, __u16 value, __u16 index, void *data,
134 __u16 size, int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800136 struct usb_ctrlrequest *dr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int ret;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800138
139 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (!dr)
141 return -ENOMEM;
142
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800143 dr->bRequestType = requesttype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 dr->bRequest = request;
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700145 dr->wValue = cpu_to_le16(value);
146 dr->wIndex = cpu_to_le16(index);
147 dr->wLength = cpu_to_le16(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800149 /* dbg("usb_control_msg"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);
152
153 kfree(dr);
154
155 return ret;
156}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600157EXPORT_SYMBOL_GPL(usb_control_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159/**
Greg Kroah-Hartman782a7a62006-05-19 13:20:20 -0700160 * usb_interrupt_msg - Builds an interrupt urb, sends it off and waits for completion
161 * @usb_dev: pointer to the usb device to send the message to
162 * @pipe: endpoint "pipe" to send the message to
163 * @data: pointer to the data to send
164 * @len: length in bytes of the data to send
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800165 * @actual_length: pointer to a location to put the actual length transferred
166 * in bytes
Greg Kroah-Hartman782a7a62006-05-19 13:20:20 -0700167 * @timeout: time in msecs to wait for the message to complete before
168 * timing out (if 0 the wait is forever)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800169 *
Greg Kroah-Hartman782a7a62006-05-19 13:20:20 -0700170 * Context: !in_interrupt ()
171 *
172 * This function sends a simple interrupt message to a specified endpoint and
173 * waits for the message to complete, or timeout.
174 *
175 * If successful, it returns 0, otherwise a negative error number. The number
176 * of actual bytes transferred will be stored in the actual_length paramater.
177 *
178 * Don't use this function from within an interrupt context, like a bottom half
179 * handler. If you need an asynchronous message, or need to send a message
180 * from within interrupt context, use usb_submit_urb() If a thread in your
181 * driver uses this call, make sure your disconnect() method can wait for it to
182 * complete. Since you don't have a handle on the URB used, you can't cancel
183 * the request.
184 */
185int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe,
186 void *data, int len, int *actual_length, int timeout)
187{
188 return usb_bulk_msg(usb_dev, pipe, data, len, actual_length, timeout);
189}
190EXPORT_SYMBOL_GPL(usb_interrupt_msg);
191
192/**
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800193 * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion
194 * @usb_dev: pointer to the usb device to send the message to
195 * @pipe: endpoint "pipe" to send the message to
196 * @data: pointer to the data to send
197 * @len: length in bytes of the data to send
198 * @actual_length: pointer to a location to put the actual length transferred
199 * in bytes
200 * @timeout: time in msecs to wait for the message to complete before
201 * timing out (if 0 the wait is forever)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800203 * Context: !in_interrupt ()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800205 * This function sends a simple bulk message to a specified endpoint
206 * and waits for the message to complete, or timeout.
Alan Sternd09d36a2005-09-26 16:22:45 -0400207 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800208 * If successful, it returns 0, otherwise a negative error number. The number
209 * of actual bytes transferred will be stored in the actual_length paramater.
210 *
211 * Don't use this function from within an interrupt context, like a bottom half
212 * handler. If you need an asynchronous message, or need to send a message
213 * from within interrupt context, use usb_submit_urb() If a thread in your
214 * driver uses this call, make sure your disconnect() method can wait for it to
215 * complete. Since you don't have a handle on the URB used, you can't cancel
216 * the request.
217 *
218 * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT ioctl,
219 * users are forced to abuse this routine by using it to submit URBs for
220 * interrupt endpoints. We will take the liberty of creating an interrupt URB
221 * (with the default interval) if the target is an interrupt endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800223int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
224 void *data, int len, int *actual_length, int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct urb *urb;
Alan Sternd09d36a2005-09-26 16:22:45 -0400227 struct usb_host_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Matthew Wilcoxfe54b052010-04-30 13:11:29 -0600229 ep = usb_pipe_endpoint(usb_dev, pipe);
Alan Sternd09d36a2005-09-26 16:22:45 -0400230 if (!ep || len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return -EINVAL;
232
Alan Sternd09d36a2005-09-26 16:22:45 -0400233 urb = usb_alloc_urb(0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (!urb)
235 return -ENOMEM;
236
Alan Sternd09d36a2005-09-26 16:22:45 -0400237 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
238 USB_ENDPOINT_XFER_INT) {
239 pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
240 usb_fill_int_urb(urb, usb_dev, pipe, data, len,
Alan Stern8d062b92007-04-23 17:30:32 -0400241 usb_api_blocking_completion, NULL,
242 ep->desc.bInterval);
Alan Sternd09d36a2005-09-26 16:22:45 -0400243 } else
244 usb_fill_bulk_urb(urb, usb_dev, pipe, data, len,
245 usb_api_blocking_completion, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 return usb_start_wait_urb(urb, timeout, actual_length);
248}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600249EXPORT_SYMBOL_GPL(usb_bulk_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251/*-------------------------------------------------------------------*/
252
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800253static void sg_clean(struct usb_sg_request *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 if (io->urbs) {
256 while (io->entries--)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800257 usb_free_urb(io->urbs [io->entries]);
258 kfree(io->urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 io->urbs = NULL;
260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 io->dev = NULL;
262}
263
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800264static void sg_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800266 struct usb_sg_request *io = urb->context;
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700267 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800269 spin_lock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 /* In 2.5 we require hcds' endpoint queues not to progress after fault
272 * reports, until the completion callback (this!) returns. That lets
273 * device driver code (like this routine) unlink queued urbs first,
274 * if it needs to, since the HC won't work on them at all. So it's
275 * not possible for page N+1 to overwrite page N, and so on.
276 *
277 * That's only for "hard" faults; "soft" faults (unlinks) sometimes
278 * complete before the HCD can get requests away from hardware,
279 * though never during cleanup after a hard fault.
280 */
281 if (io->status
282 && (io->status != -ECONNRESET
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700283 || status != -ECONNRESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 && urb->actual_length) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800285 dev_err(io->dev->bus->controller,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 "dev %s ep%d%s scatterlist error %d/%d\n",
287 io->dev->devpath,
Alan Stern5e60a162007-07-30 17:07:21 -0400288 usb_endpoint_num(&urb->ep->desc),
289 usb_urb_dir_in(urb) ? "in" : "out",
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700290 status, io->status);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800291 /* BUG (); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700294 if (io->status == 0 && status && status != -ECONNRESET) {
295 int i, found, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700297 io->status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 /* the previous urbs, and this one, completed already.
300 * unlink pending urbs so they won't rx/tx bad data.
301 * careful: unlink can sometimes be synchronous...
302 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800303 spin_unlock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 for (i = 0, found = 0; i < io->entries; i++) {
305 if (!io->urbs [i] || !io->urbs [i]->dev)
306 continue;
307 if (found) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800308 retval = usb_unlink_urb(io->urbs [i]);
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700309 if (retval != -EINPROGRESS &&
310 retval != -ENODEV &&
311 retval != -EBUSY)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800312 dev_err(&io->dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 "%s, unlink --> %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800314 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 } else if (urb == io->urbs [i])
316 found = 1;
317 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800318 spin_lock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320 urb->dev = NULL;
321
322 /* on the last completion, signal usb_sg_wait() */
323 io->bytes += urb->actual_length;
324 io->count--;
325 if (!io->count)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800326 complete(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800328 spin_unlock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331
332/**
333 * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request
334 * @io: request block being initialized. until usb_sg_wait() returns,
335 * treat this as a pointer to an opaque block of memory,
336 * @dev: the usb device that will send or receive the data
337 * @pipe: endpoint "pipe" used to transfer the data
338 * @period: polling rate for interrupt endpoints, in frames or
339 * (for high speed endpoints) microframes; ignored for bulk
340 * @sg: scatterlist entries
341 * @nents: how many entries in the scatterlist
342 * @length: how many bytes to send from the scatterlist, or zero to
343 * send every byte identified in the list.
344 * @mem_flags: SLAB_* flags affecting memory allocations in this call
345 *
346 * Returns zero for success, else a negative errno value. This initializes a
347 * scatter/gather request, allocating resources such as I/O mappings and urb
348 * memory (except maybe memory used by USB controller drivers).
349 *
350 * The request must be issued using usb_sg_wait(), which waits for the I/O to
351 * complete (or to be canceled) and then cleans up all resources allocated by
352 * usb_sg_init().
353 *
354 * The request may be canceled with usb_sg_cancel(), either before or after
355 * usb_sg_wait() is called.
356 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800357int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
358 unsigned pipe, unsigned period, struct scatterlist *sg,
359 int nents, size_t length, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800361 int i;
362 int urb_flags;
Sarah Sharpe04748e2009-04-27 19:59:01 -0700363 int use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 if (!io || !dev || !sg
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800366 || usb_pipecontrol(pipe)
367 || usb_pipeisoc(pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 || nents <= 0)
369 return -EINVAL;
370
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800371 spin_lock_init(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 io->dev = dev;
373 io->pipe = pipe;
Alan Stern0ba169af2010-05-05 15:26:17 -0400374
375 if (dev->bus->sg_tablesize > 0) {
376 use_sg = true;
377 io->entries = 1;
378 } else {
379 use_sg = false;
380 io->entries = nents;
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /* initialize all the urbs we'll use */
Alan Stern0ba169af2010-05-05 15:26:17 -0400384 io->urbs = kmalloc(io->entries * sizeof *io->urbs, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (!io->urbs)
386 goto nomem;
387
Alan Stern0ba169af2010-05-05 15:26:17 -0400388 urb_flags = URB_NO_INTERRUPT;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800389 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 urb_flags |= URB_SHORT_NOT_OK;
391
Alan Stern0ba169af2010-05-05 15:26:17 -0400392 for_each_sg(sg, sg, io->entries, i) {
393 struct urb *urb;
394 unsigned len;
395
396 urb = usb_alloc_urb(0, mem_flags);
397 if (!urb) {
398 io->entries = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 goto nomem;
400 }
Alan Stern0ba169af2010-05-05 15:26:17 -0400401 io->urbs[i] = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Alan Stern0ba169af2010-05-05 15:26:17 -0400403 urb->dev = NULL;
404 urb->pipe = pipe;
405 urb->interval = period;
406 urb->transfer_flags = urb_flags;
407 urb->complete = sg_complete;
408 urb->context = io;
409 urb->sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Alan Stern0ba169af2010-05-05 15:26:17 -0400411 if (use_sg) {
412 /* There is no single transfer buffer */
413 urb->transfer_buffer = NULL;
414 urb->num_sgs = nents;
Alan Sternff9c8952010-04-02 13:27:28 -0400415
Alan Stern0ba169af2010-05-05 15:26:17 -0400416 /* A length of zero means transfer the whole sg list */
417 len = length;
418 if (len == 0) {
419 for_each_sg(sg, sg, nents, i)
420 len += sg->length;
Sarah Sharpe04748e2009-04-27 19:59:01 -0700421 }
Alan Stern0ba169af2010-05-05 15:26:17 -0400422 } else {
Sarah Sharpe04748e2009-04-27 19:59:01 -0700423 /*
Alan Sternff9c8952010-04-02 13:27:28 -0400424 * Some systems can't use DMA; they use PIO instead.
425 * For their sakes, transfer_buffer is set whenever
426 * possible.
Sarah Sharpe04748e2009-04-27 19:59:01 -0700427 */
Alan Sternff9c8952010-04-02 13:27:28 -0400428 if (!PageHighMem(sg_page(sg)))
Alan Stern0ba169af2010-05-05 15:26:17 -0400429 urb->transfer_buffer = sg_virt(sg);
Pete Zaitcev81bf46f2009-06-11 08:40:39 -0600430 else
Alan Stern0ba169af2010-05-05 15:26:17 -0400431 urb->transfer_buffer = NULL;
Pete Zaitcev81bf46f2009-06-11 08:40:39 -0600432
Alan Sternff9c8952010-04-02 13:27:28 -0400433 len = sg->length;
Sarah Sharpe04748e2009-04-27 19:59:01 -0700434 if (length) {
435 len = min_t(unsigned, len, length);
436 length -= len;
437 if (length == 0)
438 io->entries = i + 1;
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
Alan Stern0ba169af2010-05-05 15:26:17 -0400441 urb->transfer_buffer_length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
Alan Stern0ba169af2010-05-05 15:26:17 -0400443 io->urbs[--i]->transfer_flags &= ~URB_NO_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /* transaction state */
Alan Stern580da342008-08-05 13:05:17 -0400446 io->count = io->entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 io->status = 0;
448 io->bytes = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800449 init_completion(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return 0;
451
452nomem:
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800453 sg_clean(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return -ENOMEM;
455}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600456EXPORT_SYMBOL_GPL(usb_sg_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458/**
459 * usb_sg_wait - synchronously execute scatter/gather request
460 * @io: request block handle, as initialized with usb_sg_init().
461 * some fields become accessible when this call returns.
462 * Context: !in_interrupt ()
463 *
464 * This function blocks until the specified I/O operation completes. It
465 * leverages the grouping of the related I/O requests to get good transfer
466 * rates, by queueing the requests. At higher speeds, such queuing can
467 * significantly improve USB throughput.
468 *
469 * There are three kinds of completion for this function.
470 * (1) success, where io->status is zero. The number of io->bytes
471 * transferred is as requested.
472 * (2) error, where io->status is a negative errno value. The number
473 * of io->bytes transferred before the error is usually less
474 * than requested, and can be nonzero.
Steven Cole093cf722005-05-03 19:07:24 -0600475 * (3) cancellation, a type of error with status -ECONNRESET that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 * is initiated by usb_sg_cancel().
477 *
478 * When this function returns, all memory allocated through usb_sg_init() or
479 * this call will have been freed. The request block parameter may still be
480 * passed to usb_sg_cancel(), or it may be freed. It could also be
481 * reinitialized and then reused.
482 *
483 * Data Transfer Rates:
484 *
485 * Bulk transfers are valid for full or high speed endpoints.
486 * The best full speed data rate is 19 packets of 64 bytes each
487 * per frame, or 1216 bytes per millisecond.
488 * The best high speed data rate is 13 packets of 512 bytes each
489 * per microframe, or 52 KBytes per millisecond.
490 *
491 * The reason to use interrupt transfers through this API would most likely
492 * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
493 * could be transferred. That capability is less useful for low or full
494 * speed interrupt endpoints, which allow at most one packet per millisecond,
495 * of at most 8 or 64 bytes (respectively).
Sarah Sharp79abb1a2009-04-27 19:58:26 -0700496 *
497 * It is not necessary to call this function to reserve bandwidth for devices
498 * under an xHCI host controller, as the bandwidth is reserved when the
499 * configuration or interface alt setting is selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800501void usb_sg_wait(struct usb_sg_request *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800503 int i;
504 int entries = io->entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /* queue the urbs. */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800507 spin_lock_irq(&io->lock);
Alan Stern8ccef0d2007-06-21 16:26:46 -0400508 i = 0;
509 while (i < entries && !io->status) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800510 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800512 io->urbs[i]->dev = io->dev;
513 retval = usb_submit_urb(io->urbs [i], GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 /* after we submit, let completions or cancelations fire;
516 * we handshake using io->status.
517 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800518 spin_unlock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 switch (retval) {
520 /* maybe we retrying will recover */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800521 case -ENXIO: /* hc didn't queue this one */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 case -EAGAIN:
523 case -ENOMEM:
524 io->urbs[i]->dev = NULL;
525 retval = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800526 yield();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 break;
528
529 /* no error? continue immediately.
530 *
531 * NOTE: to work better with UHCI (4K I/O buffer may
532 * need 3K of TDs) it may be good to limit how many
533 * URBs are queued at once; N milliseconds?
534 */
535 case 0:
Alan Stern8ccef0d2007-06-21 16:26:46 -0400536 ++i;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800537 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 break;
539
540 /* fail any uncompleted urbs */
541 default:
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800542 io->urbs[i]->dev = NULL;
543 io->urbs[i]->status = retval;
544 dev_dbg(&io->dev->dev, "%s, submit --> %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800545 __func__, retval);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800546 usb_sg_cancel(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800548 spin_lock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (retval && (io->status == 0 || io->status == -ECONNRESET))
550 io->status = retval;
551 }
552 io->count -= entries - i;
553 if (io->count == 0)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800554 complete(&io->complete);
555 spin_unlock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 /* OK, yes, this could be packaged as non-blocking.
558 * So could the submit loop above ... but it's easier to
559 * solve neither problem than to solve both!
560 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800561 wait_for_completion(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800563 sg_clean(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600565EXPORT_SYMBOL_GPL(usb_sg_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567/**
568 * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
569 * @io: request block, initialized with usb_sg_init()
570 *
571 * This stops a request after it has been started by usb_sg_wait().
572 * It can also prevents one initialized by usb_sg_init() from starting,
573 * so that call just frees resources allocated to the request.
574 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800575void usb_sg_cancel(struct usb_sg_request *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800577 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800579 spin_lock_irqsave(&io->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 /* shut everything down, if it didn't already */
582 if (!io->status) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800583 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 io->status = -ECONNRESET;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800586 spin_unlock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 for (i = 0; i < io->entries; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800588 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 if (!io->urbs [i]->dev)
591 continue;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800592 retval = usb_unlink_urb(io->urbs [i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (retval != -EINPROGRESS && retval != -EBUSY)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800594 dev_warn(&io->dev->dev, "%s, unlink --> %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800595 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800597 spin_lock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800599 spin_unlock_irqrestore(&io->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600601EXPORT_SYMBOL_GPL(usb_sg_cancel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603/*-------------------------------------------------------------------*/
604
605/**
606 * usb_get_descriptor - issues a generic GET_DESCRIPTOR request
607 * @dev: the device whose descriptor is being retrieved
608 * @type: the descriptor type (USB_DT_*)
609 * @index: the number of the descriptor
610 * @buf: where to put the descriptor
611 * @size: how big is "buf"?
612 * Context: !in_interrupt ()
613 *
614 * Gets a USB descriptor. Convenience functions exist to simplify
615 * getting some types of descriptors. Use
616 * usb_get_string() or usb_string() for USB_DT_STRING.
617 * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG)
618 * are part of the device structure.
619 * In addition to a number of USB-standard descriptors, some
620 * devices also use class-specific or vendor-specific descriptors.
621 *
622 * This call is synchronous, and may not be used in an interrupt context.
623 *
624 * Returns the number of bytes received on success, or else the status code
625 * returned by the underlying usb_control_msg() call.
626 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800627int usb_get_descriptor(struct usb_device *dev, unsigned char type,
628 unsigned char index, void *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 int i;
631 int result;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800632
633 memset(buf, 0, size); /* Make sure we parse really received data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 for (i = 0; i < 3; ++i) {
Alan Sternc39772d2007-08-20 10:45:28 -0400636 /* retry on length 0 or error; some devices are flakey */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
638 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
639 (type << 8) + index, 0, buf, size,
640 USB_CTRL_GET_TIMEOUT);
Alan Sternc39772d2007-08-20 10:45:28 -0400641 if (result <= 0 && result != -ETIMEDOUT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 continue;
643 if (result > 1 && ((u8 *)buf)[1] != type) {
Alan Stern67f5a4b2009-02-20 16:33:08 -0500644 result = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 continue;
646 }
647 break;
648 }
649 return result;
650}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600651EXPORT_SYMBOL_GPL(usb_get_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653/**
654 * usb_get_string - gets a string descriptor
655 * @dev: the device whose string descriptor is being retrieved
656 * @langid: code for language chosen (from string descriptor zero)
657 * @index: the number of the descriptor
658 * @buf: where to put the string
659 * @size: how big is "buf"?
660 * Context: !in_interrupt ()
661 *
662 * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character,
663 * in little-endian byte order).
664 * The usb_string() function will often be a convenient way to turn
665 * these strings into kernel-printable form.
666 *
667 * Strings may be referenced in device, configuration, interface, or other
668 * descriptors, and could also be used in vendor-specific ways.
669 *
670 * This call is synchronous, and may not be used in an interrupt context.
671 *
672 * Returns the number of bytes received on success, or else the status code
673 * returned by the underlying usb_control_msg() call.
674 */
Adrian Bunke266a122005-11-08 21:05:43 +0100675static int usb_get_string(struct usb_device *dev, unsigned short langid,
676 unsigned char index, void *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 int i;
679 int result;
680
681 for (i = 0; i < 3; ++i) {
682 /* retry on length 0 or stall; some devices are flakey */
683 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
684 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
685 (USB_DT_STRING << 8) + index, langid, buf, size,
686 USB_CTRL_GET_TIMEOUT);
Alan Stern67f5a4b2009-02-20 16:33:08 -0500687 if (result == 0 || result == -EPIPE)
688 continue;
689 if (result > 1 && ((u8 *) buf)[1] != USB_DT_STRING) {
690 result = -ENODATA;
691 continue;
692 }
693 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695 return result;
696}
697
698static void usb_try_string_workarounds(unsigned char *buf, int *length)
699{
700 int newlength, oldlength = *length;
701
702 for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
703 if (!isprint(buf[newlength]) || buf[newlength + 1])
704 break;
705
706 if (newlength > 2) {
707 buf[0] = newlength;
708 *length = newlength;
709 }
710}
711
712static int usb_string_sub(struct usb_device *dev, unsigned int langid,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800713 unsigned int index, unsigned char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 int rc;
716
717 /* Try to read the string descriptor by asking for the maximum
718 * possible number of bytes */
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100719 if (dev->quirks & USB_QUIRK_STRING_FETCH_255)
720 rc = -EIO;
721 else
722 rc = usb_get_string(dev, langid, index, buf, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 /* If that failed try to read the descriptor length, then
725 * ask for just that many bytes */
726 if (rc < 2) {
727 rc = usb_get_string(dev, langid, index, buf, 2);
728 if (rc == 2)
729 rc = usb_get_string(dev, langid, index, buf, buf[0]);
730 }
731
732 if (rc >= 2) {
733 if (!buf[0] && !buf[1])
734 usb_try_string_workarounds(buf, &rc);
735
736 /* There might be extra junk at the end of the descriptor */
737 if (buf[0] < rc)
738 rc = buf[0];
739
740 rc = rc - (rc & 1); /* force a multiple of two */
741 }
742
743 if (rc < 2)
744 rc = (rc < 0 ? rc : -EINVAL);
745
746 return rc;
747}
748
Daniel Mack0cce2ed2009-07-10 11:04:58 +0200749static int usb_get_langid(struct usb_device *dev, unsigned char *tbuf)
750{
751 int err;
752
753 if (dev->have_langid)
754 return 0;
755
756 if (dev->string_langid < 0)
757 return -EPIPE;
758
759 err = usb_string_sub(dev, 0, 0, tbuf);
760
761 /* If the string was reported but is malformed, default to english
762 * (0x0409) */
763 if (err == -ENODATA || (err > 0 && err < 4)) {
764 dev->string_langid = 0x0409;
765 dev->have_langid = 1;
766 dev_err(&dev->dev,
767 "string descriptor 0 malformed (err = %d), "
768 "defaulting to 0x%04x\n",
769 err, dev->string_langid);
770 return 0;
771 }
772
773 /* In case of all other errors, we assume the device is not able to
774 * deal with strings at all. Set string_langid to -1 in order to
775 * prevent any string to be retrieved from the device */
776 if (err < 0) {
777 dev_err(&dev->dev, "string descriptor 0 read error: %d\n",
778 err);
779 dev->string_langid = -1;
780 return -EPIPE;
781 }
782
783 /* always use the first langid listed */
784 dev->string_langid = tbuf[2] | (tbuf[3] << 8);
785 dev->have_langid = 1;
786 dev_dbg(&dev->dev, "default language 0x%04x\n",
787 dev->string_langid);
788 return 0;
789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/**
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200792 * usb_string - returns UTF-8 version of a string descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 * @dev: the device whose string descriptor is being retrieved
794 * @index: the number of the descriptor
795 * @buf: where to put the string
796 * @size: how big is "buf"?
797 * Context: !in_interrupt ()
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800798 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 * This converts the UTF-16LE encoded strings returned by devices, from
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200800 * usb_get_string_descriptor(), to null-terminated UTF-8 encoded ones
801 * that are more usable in most kernel contexts. Note that this function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 * chooses strings in the first language supported by the device.
803 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * This call is synchronous, and may not be used in an interrupt context.
805 *
806 * Returns length of the string (>= 0) or usb_control_msg status (< 0).
807 */
808int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
809{
810 unsigned char *tbuf;
811 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 if (dev->state == USB_STATE_SUSPENDED)
814 return -EHOSTUNREACH;
815 if (size <= 0 || !buf || !index)
816 return -EINVAL;
817 buf[0] = 0;
Alan Stern74675a52009-04-30 10:08:18 -0400818 tbuf = kmalloc(256, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (!tbuf)
820 return -ENOMEM;
821
Daniel Mack0cce2ed2009-07-10 11:04:58 +0200822 err = usb_get_langid(dev, tbuf);
823 if (err < 0)
824 goto errout;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 err = usb_string_sub(dev, dev->string_langid, index, tbuf);
827 if (err < 0)
828 goto errout;
829
830 size--; /* leave room for trailing NULL char in output buffer */
Alan Stern74675a52009-04-30 10:08:18 -0400831 err = utf16s_to_utf8s((wchar_t *) &tbuf[2], (err - 2) / 2,
832 UTF16_LITTLE_ENDIAN, buf, size);
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200833 buf[err] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 if (tbuf[1] != USB_DT_STRING)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800836 dev_dbg(&dev->dev,
837 "wrong descriptor type %02x for string %d (\"%s\")\n",
838 tbuf[1], index, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 errout:
841 kfree(tbuf);
842 return err;
843}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600844EXPORT_SYMBOL_GPL(usb_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200846/* one UTF-8-encoded 16-bit character has at most three bytes */
847#define MAX_USB_STRING_SIZE (127 * 3 + 1)
848
Alan Stern4f62efe2005-10-24 16:24:14 -0400849/**
850 * usb_cache_string - read a string descriptor and cache it for later use
851 * @udev: the device whose string descriptor is being read
852 * @index: the descriptor index
853 *
854 * Returns a pointer to a kmalloc'ed buffer containing the descriptor string,
855 * or NULL if the index is 0 or the string could not be read.
856 */
857char *usb_cache_string(struct usb_device *udev, int index)
858{
859 char *buf;
860 char *smallbuf = NULL;
861 int len;
862
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800863 if (index <= 0)
864 return NULL;
865
Oliver Neukumacbe2fe2010-01-12 12:32:50 +0100866 buf = kmalloc(MAX_USB_STRING_SIZE, GFP_NOIO);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800867 if (buf) {
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200868 len = usb_string(udev, index, buf, MAX_USB_STRING_SIZE);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800869 if (len > 0) {
Oliver Neukumacbe2fe2010-01-12 12:32:50 +0100870 smallbuf = kmalloc(++len, GFP_NOIO);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800871 if (!smallbuf)
Alan Stern4f62efe2005-10-24 16:24:14 -0400872 return buf;
873 memcpy(smallbuf, buf, len);
874 }
875 kfree(buf);
876 }
877 return smallbuf;
878}
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880/*
881 * usb_get_device_descriptor - (re)reads the device descriptor (usbcore)
882 * @dev: the device whose device descriptor is being updated
883 * @size: how much of the descriptor to read
884 * Context: !in_interrupt ()
885 *
886 * Updates the copy of the device descriptor stored in the device structure,
Laurent Pinchart6ab16a92006-11-07 10:16:25 +0100887 * which dedicates space for this purpose.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 *
889 * Not exported, only for use by the core. If drivers really want to read
890 * the device descriptor directly, they can call usb_get_descriptor() with
891 * type = USB_DT_DEVICE and index = 0.
892 *
893 * This call is synchronous, and may not be used in an interrupt context.
894 *
895 * Returns the number of bytes received on success, or else the status code
896 * returned by the underlying usb_control_msg() call.
897 */
898int usb_get_device_descriptor(struct usb_device *dev, unsigned int size)
899{
900 struct usb_device_descriptor *desc;
901 int ret;
902
903 if (size > sizeof(*desc))
904 return -EINVAL;
905 desc = kmalloc(sizeof(*desc), GFP_NOIO);
906 if (!desc)
907 return -ENOMEM;
908
909 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800910 if (ret >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 memcpy(&dev->descriptor, desc, size);
912 kfree(desc);
913 return ret;
914}
915
916/**
917 * usb_get_status - issues a GET_STATUS call
918 * @dev: the device whose status is being checked
919 * @type: USB_RECIP_*; for device, interface, or endpoint
920 * @target: zero (for device), else interface or endpoint number
921 * @data: pointer to two bytes of bitmap data
922 * Context: !in_interrupt ()
923 *
924 * Returns device, interface, or endpoint status. Normally only of
925 * interest to see if the device is self powered, or has enabled the
926 * remote wakeup facility; or whether a bulk or interrupt endpoint
927 * is halted ("stalled").
928 *
929 * Bits in these status bitmaps are set using the SET_FEATURE request,
930 * and cleared using the CLEAR_FEATURE request. The usb_clear_halt()
931 * function should be used to clear halt ("stall") status.
932 *
933 * This call is synchronous, and may not be used in an interrupt context.
934 *
935 * Returns the number of bytes received on success, or else the status code
936 * returned by the underlying usb_control_msg() call.
937 */
938int usb_get_status(struct usb_device *dev, int type, int target, void *data)
939{
940 int ret;
941 u16 *status = kmalloc(sizeof(*status), GFP_KERNEL);
942
943 if (!status)
944 return -ENOMEM;
945
946 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
947 USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status,
948 sizeof(*status), USB_CTRL_GET_TIMEOUT);
949
950 *(u16 *)data = *status;
951 kfree(status);
952 return ret;
953}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600954EXPORT_SYMBOL_GPL(usb_get_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956/**
957 * usb_clear_halt - tells device to clear endpoint halt/stall condition
958 * @dev: device whose endpoint is halted
959 * @pipe: endpoint "pipe" being cleared
960 * Context: !in_interrupt ()
961 *
962 * This is used to clear halt conditions for bulk and interrupt endpoints,
963 * as reported by URB completion status. Endpoints that are halted are
964 * sometimes referred to as being "stalled". Such endpoints are unable
965 * to transmit or receive data until the halt status is cleared. Any URBs
966 * queued for such an endpoint should normally be unlinked by the driver
967 * before clearing the halt condition, as described in sections 5.7.5
968 * and 5.8.5 of the USB 2.0 spec.
969 *
970 * Note that control and isochronous endpoints don't halt, although control
971 * endpoints report "protocol stall" (for unsupported requests) using the
972 * same status code used to report a true stall.
973 *
974 * This call is synchronous, and may not be used in an interrupt context.
975 *
976 * Returns zero on success, or else the status code returned by the
977 * underlying usb_control_msg() call.
978 */
979int usb_clear_halt(struct usb_device *dev, int pipe)
980{
981 int result;
982 int endp = usb_pipeendpoint(pipe);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800983
984 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 endp |= USB_DIR_IN;
986
987 /* we don't care if it wasn't halted first. in fact some devices
988 * (like some ibmcam model 1 units) seem to expect hosts to make
989 * this request for iso endpoints, which can't halt!
990 */
991 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
992 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
993 USB_ENDPOINT_HALT, endp, NULL, 0,
994 USB_CTRL_SET_TIMEOUT);
995
996 /* don't un-halt or force to DATA0 except on success */
997 if (result < 0)
998 return result;
999
1000 /* NOTE: seems like Microsoft and Apple don't bother verifying
1001 * the clear "took", so some devices could lock up if you check...
1002 * such as the Hagiwara FlashGate DUAL. So we won't bother.
1003 *
1004 * NOTE: make sure the logic here doesn't diverge much from
1005 * the copy in usb-storage, for as long as we need two copies.
1006 */
1007
David Vrabel3444b262009-04-08 17:36:28 +00001008 usb_reset_endpoint(dev, endp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 return 0;
1011}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001012EXPORT_SYMBOL_GPL(usb_clear_halt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Alan Stern3b23dd62008-12-05 14:10:34 -05001014static int create_intf_ep_devs(struct usb_interface *intf)
1015{
1016 struct usb_device *udev = interface_to_usbdev(intf);
1017 struct usb_host_interface *alt = intf->cur_altsetting;
1018 int i;
1019
1020 if (intf->ep_devs_created || intf->unregistering)
1021 return 0;
1022
1023 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1024 (void) usb_create_ep_devs(&intf->dev, &alt->endpoint[i], udev);
1025 intf->ep_devs_created = 1;
1026 return 0;
1027}
1028
1029static void remove_intf_ep_devs(struct usb_interface *intf)
1030{
1031 struct usb_host_interface *alt = intf->cur_altsetting;
1032 int i;
1033
1034 if (!intf->ep_devs_created)
1035 return;
1036
1037 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1038 usb_remove_ep_devs(&alt->endpoint[i]);
1039 intf->ep_devs_created = 0;
1040}
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042/**
1043 * usb_disable_endpoint -- Disable an endpoint by address
1044 * @dev: the device whose endpoint is being disabled
1045 * @epaddr: the endpoint's address. Endpoint number for output,
1046 * endpoint number + USB_DIR_IN for input
Alan Sternddeac4e2009-01-15 17:03:33 -05001047 * @reset_hardware: flag to erase any endpoint state stored in the
1048 * controller hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 *
Alan Sternddeac4e2009-01-15 17:03:33 -05001050 * Disables the endpoint for URB submission and nukes all pending URBs.
1051 * If @reset_hardware is set then also deallocates hcd/hardware state
1052 * for the endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 */
Alan Sternddeac4e2009-01-15 17:03:33 -05001054void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr,
1055 bool reset_hardware)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1058 struct usb_host_endpoint *ep;
1059
1060 if (!dev)
1061 return;
1062
1063 if (usb_endpoint_out(epaddr)) {
1064 ep = dev->ep_out[epnum];
Alan Sternddeac4e2009-01-15 17:03:33 -05001065 if (reset_hardware)
1066 dev->ep_out[epnum] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 } else {
1068 ep = dev->ep_in[epnum];
Alan Sternddeac4e2009-01-15 17:03:33 -05001069 if (reset_hardware)
1070 dev->ep_in[epnum] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
Alan Sternbdd016b2007-07-30 17:05:22 -04001072 if (ep) {
1073 ep->enabled = 0;
Alan Stern95cf82f2007-09-10 11:33:05 -04001074 usb_hcd_flush_endpoint(dev, ep);
Alan Sternddeac4e2009-01-15 17:03:33 -05001075 if (reset_hardware)
1076 usb_hcd_disable_endpoint(dev, ep);
Alan Sternbdd016b2007-07-30 17:05:22 -04001077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
1080/**
David Vrabel3444b262009-04-08 17:36:28 +00001081 * usb_reset_endpoint - Reset an endpoint's state.
1082 * @dev: the device whose endpoint is to be reset
1083 * @epaddr: the endpoint's address. Endpoint number for output,
1084 * endpoint number + USB_DIR_IN for input
1085 *
1086 * Resets any host-side endpoint state such as the toggle bit,
1087 * sequence number or current window.
1088 */
1089void usb_reset_endpoint(struct usb_device *dev, unsigned int epaddr)
1090{
1091 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1092 struct usb_host_endpoint *ep;
1093
1094 if (usb_endpoint_out(epaddr))
1095 ep = dev->ep_out[epnum];
1096 else
1097 ep = dev->ep_in[epnum];
1098 if (ep)
1099 usb_hcd_reset_endpoint(dev, ep);
1100}
1101EXPORT_SYMBOL_GPL(usb_reset_endpoint);
1102
1103
1104/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 * usb_disable_interface -- Disable all endpoints for an interface
1106 * @dev: the device whose interface is being disabled
1107 * @intf: pointer to the interface descriptor
Alan Sternddeac4e2009-01-15 17:03:33 -05001108 * @reset_hardware: flag to erase any endpoint state stored in the
1109 * controller hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 *
1111 * Disables all the endpoints for the interface's current altsetting.
1112 */
Alan Sternddeac4e2009-01-15 17:03:33 -05001113void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf,
1114 bool reset_hardware)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 struct usb_host_interface *alt = intf->cur_altsetting;
1117 int i;
1118
1119 for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
1120 usb_disable_endpoint(dev,
Alan Sternddeac4e2009-01-15 17:03:33 -05001121 alt->endpoint[i].desc.bEndpointAddress,
1122 reset_hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124}
1125
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001126/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 * usb_disable_device - Disable all the endpoints for a USB device
1128 * @dev: the device whose endpoints are being disabled
1129 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
1130 *
1131 * Disables all the device's endpoints, potentially including endpoint 0.
1132 * Deallocates hcd/hardware state for the endpoints (nuking all or most
1133 * pending urbs) and usbcore state for the interfaces, so that usbcore
1134 * must usb_set_configuration() before any interfaces could be used.
1135 */
1136void usb_disable_device(struct usb_device *dev, int skip_ep0)
1137{
1138 int i;
1139
Harvey Harrison441b62c2008-03-03 16:08:34 -08001140 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __func__,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001141 skip_ep0 ? "non-ep0" : "all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 for (i = skip_ep0; i < 16; ++i) {
Alan Sternddeac4e2009-01-15 17:03:33 -05001143 usb_disable_endpoint(dev, i, true);
1144 usb_disable_endpoint(dev, i + USB_DIR_IN, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 /* getting rid of interfaces will disconnect
1148 * any drivers bound to them (a key side effect)
1149 */
1150 if (dev->actconfig) {
1151 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1152 struct usb_interface *interface;
1153
Alan Stern86d30742005-07-29 12:17:16 -07001154 /* remove this interface if it has been registered */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 interface = dev->actconfig->interface[i];
Daniel Ritzd305ef52005-09-22 00:47:24 -07001156 if (!device_is_registered(&interface->dev))
Alan Stern86d30742005-07-29 12:17:16 -07001157 continue;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001158 dev_dbg(&dev->dev, "unregistering interface %s\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001159 dev_name(&interface->dev));
Alan Stern352d0262008-10-29 15:16:58 -04001160 interface->unregistering = 1;
Alan Stern3b23dd62008-12-05 14:10:34 -05001161 remove_intf_ep_devs(interface);
Alan Stern1a211752008-07-30 11:31:50 -04001162 device_del(&interface->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
1164
1165 /* Now that the interfaces are unbound, nobody should
1166 * try to access them.
1167 */
1168 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001169 put_device(&dev->actconfig->interface[i]->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 dev->actconfig->interface[i] = NULL;
1171 }
1172 dev->actconfig = NULL;
1173 if (dev->state == USB_STATE_CONFIGURED)
1174 usb_set_device_state(dev, USB_STATE_ADDRESS);
1175 }
1176}
1177
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001178/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 * usb_enable_endpoint - Enable an endpoint for USB communications
1180 * @dev: the device whose interface is being enabled
1181 * @ep: the endpoint
David Vrabel3444b262009-04-08 17:36:28 +00001182 * @reset_ep: flag to reset the endpoint state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 *
David Vrabel3444b262009-04-08 17:36:28 +00001184 * Resets the endpoint state if asked, and sets dev->ep_{in,out} pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 * For control endpoints, both the input and output sides are handled.
1186 */
Alan Stern2caf7fc2008-12-31 11:31:33 -05001187void usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep,
David Vrabel3444b262009-04-08 17:36:28 +00001188 bool reset_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
Alan Sternbdd016b2007-07-30 17:05:22 -04001190 int epnum = usb_endpoint_num(&ep->desc);
1191 int is_out = usb_endpoint_dir_out(&ep->desc);
1192 int is_control = usb_endpoint_xfer_control(&ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
David Vrabel3444b262009-04-08 17:36:28 +00001194 if (reset_ep)
1195 usb_hcd_reset_endpoint(dev, ep);
1196 if (is_out || is_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 dev->ep_out[epnum] = ep;
David Vrabel3444b262009-04-08 17:36:28 +00001198 if (!is_out || is_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 dev->ep_in[epnum] = ep;
Alan Sternbdd016b2007-07-30 17:05:22 -04001200 ep->enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001203/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 * usb_enable_interface - Enable all the endpoints for an interface
1205 * @dev: the device whose interface is being enabled
1206 * @intf: pointer to the interface descriptor
David Vrabel3444b262009-04-08 17:36:28 +00001207 * @reset_eps: flag to reset the endpoints' state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 *
1209 * Enables all the endpoints for the interface's current altsetting.
1210 */
Alan Stern2caf7fc2008-12-31 11:31:33 -05001211void usb_enable_interface(struct usb_device *dev,
David Vrabel3444b262009-04-08 17:36:28 +00001212 struct usb_interface *intf, bool reset_eps)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 struct usb_host_interface *alt = intf->cur_altsetting;
1215 int i;
1216
1217 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
David Vrabel3444b262009-04-08 17:36:28 +00001218 usb_enable_endpoint(dev, &alt->endpoint[i], reset_eps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
1221/**
1222 * usb_set_interface - Makes a particular alternate setting be current
1223 * @dev: the device whose interface is being updated
1224 * @interface: the interface being updated
1225 * @alternate: the setting being chosen.
1226 * Context: !in_interrupt ()
1227 *
1228 * This is used to enable data transfers on interfaces that may not
1229 * be enabled by default. Not all devices support such configurability.
1230 * Only the driver bound to an interface may change its setting.
1231 *
1232 * Within any given configuration, each interface may have several
1233 * alternative settings. These are often used to control levels of
1234 * bandwidth consumption. For example, the default setting for a high
1235 * speed interrupt endpoint may not send more than 64 bytes per microframe,
1236 * while interrupt transfers of up to 3KBytes per microframe are legal.
1237 * Also, isochronous endpoints may never be part of an
1238 * interface's default setting. To access such bandwidth, alternate
1239 * interface settings must be made current.
1240 *
1241 * Note that in the Linux USB subsystem, bandwidth associated with
1242 * an endpoint in a given alternate setting is not reserved until an URB
1243 * is submitted that needs that bandwidth. Some other operating systems
1244 * allocate bandwidth early, when a configuration is chosen.
1245 *
1246 * This call is synchronous, and may not be used in an interrupt context.
1247 * Also, drivers must not change altsettings while urbs are scheduled for
1248 * endpoints in that interface; all such urbs must first be completed
1249 * (perhaps forced by unlinking).
1250 *
1251 * Returns zero on success, or else the status code returned by the
1252 * underlying usb_control_msg() call.
1253 */
1254int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1255{
1256 struct usb_interface *iface;
1257 struct usb_host_interface *alt;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001258 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 int ret;
1260 int manual = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001261 unsigned int epaddr;
1262 unsigned int pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 if (dev->state == USB_STATE_SUSPENDED)
1265 return -EHOSTUNREACH;
1266
1267 iface = usb_ifnum_to_if(dev, interface);
1268 if (!iface) {
1269 dev_dbg(&dev->dev, "selecting invalid interface %d\n",
1270 interface);
1271 return -EINVAL;
1272 }
1273
1274 alt = usb_altnum_to_altsetting(iface, alternate);
1275 if (!alt) {
Thadeu Lima de Souza Cascardo385f6902010-01-17 19:24:03 -02001276 dev_warn(&dev->dev, "selecting invalid altsetting %d\n",
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -07001277 alternate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return -EINVAL;
1279 }
1280
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001281 /* Make sure we have enough bandwidth for this alternate interface.
1282 * Remove the current alt setting and add the new alt setting.
1283 */
1284 mutex_lock(&hcd->bandwidth_mutex);
1285 ret = usb_hcd_alloc_bandwidth(dev, NULL, iface->cur_altsetting, alt);
1286 if (ret < 0) {
1287 dev_info(&dev->dev, "Not enough bandwidth for altsetting %d\n",
1288 alternate);
1289 mutex_unlock(&hcd->bandwidth_mutex);
1290 return ret;
1291 }
1292
Alan Stern392e1d92008-03-11 10:20:12 -04001293 if (dev->quirks & USB_QUIRK_NO_SET_INTF)
1294 ret = -EPIPE;
1295 else
1296 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1298 alternate, interface, NULL, 0, 5000);
1299
1300 /* 9.4.10 says devices don't need this and are free to STALL the
1301 * request if the interface only has one alternate setting.
1302 */
1303 if (ret == -EPIPE && iface->num_altsetting == 1) {
1304 dev_dbg(&dev->dev,
1305 "manual set_interface for iface %d, alt %d\n",
1306 interface, alternate);
1307 manual = 1;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001308 } else if (ret < 0) {
1309 /* Re-instate the old alt setting */
1310 usb_hcd_alloc_bandwidth(dev, NULL, alt, iface->cur_altsetting);
1311 mutex_unlock(&hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return ret;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001313 }
1314 mutex_unlock(&hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 /* FIXME drivers shouldn't need to replicate/bugfix the logic here
1317 * when they implement async or easily-killable versions of this or
1318 * other "should-be-internal" functions (like clear_halt).
1319 * should hcd+usbcore postprocess control requests?
1320 */
1321
1322 /* prevent submissions using previous endpoint settings */
Alan Stern3b23dd62008-12-05 14:10:34 -05001323 if (iface->cur_altsetting != alt) {
1324 remove_intf_ep_devs(iface);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001325 usb_remove_sysfs_intf_files(iface);
Alan Stern3b23dd62008-12-05 14:10:34 -05001326 }
Alan Sternddeac4e2009-01-15 17:03:33 -05001327 usb_disable_interface(dev, iface, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 iface->cur_altsetting = alt;
1330
1331 /* If the interface only has one altsetting and the device didn't
David Brownella81e7ec2005-04-18 17:39:25 -07001332 * accept the request, we attempt to carry out the equivalent action
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 * by manually clearing the HALT feature for each endpoint in the
1334 * new altsetting.
1335 */
1336 if (manual) {
1337 int i;
1338
1339 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001340 epaddr = alt->endpoint[i].desc.bEndpointAddress;
1341 pipe = __create_pipe(dev,
1342 USB_ENDPOINT_NUMBER_MASK & epaddr) |
1343 (usb_endpoint_out(epaddr) ?
1344 USB_DIR_OUT : USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 usb_clear_halt(dev, pipe);
1347 }
1348 }
1349
1350 /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
1351 *
1352 * Note:
1353 * Despite EP0 is always present in all interfaces/AS, the list of
1354 * endpoints from the descriptor does not contain EP0. Due to its
1355 * omnipresence one might expect EP0 being considered "affected" by
1356 * any SetInterface request and hence assume toggles need to be reset.
1357 * However, EP0 toggles are re-synced for every individual transfer
1358 * during the SETUP stage - hence EP0 toggles are "don't care" here.
1359 * (Likewise, EP0 never "halts" on well designed devices.)
1360 */
Alan Stern2caf7fc2008-12-31 11:31:33 -05001361 usb_enable_interface(dev, iface, true);
Alan Stern3b23dd62008-12-05 14:10:34 -05001362 if (device_is_registered(&iface->dev)) {
Alan Stern0e6c8e82005-10-24 15:33:03 -04001363 usb_create_sysfs_intf_files(iface);
Alan Stern3b23dd62008-12-05 14:10:34 -05001364 create_intf_ep_devs(iface);
1365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return 0;
1367}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001368EXPORT_SYMBOL_GPL(usb_set_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370/**
1371 * usb_reset_configuration - lightweight device reset
1372 * @dev: the device whose configuration is being reset
1373 *
1374 * This issues a standard SET_CONFIGURATION request to the device using
1375 * the current configuration. The effect is to reset most USB-related
1376 * state in the device, including interface altsettings (reset to zero),
David Vrabel3444b262009-04-08 17:36:28 +00001377 * endpoint halts (cleared), and endpoint state (only for bulk and interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 * endpoints). Other usbcore state is unchanged, including bindings of
1379 * usb device drivers to interfaces.
1380 *
1381 * Because this affects multiple interfaces, avoid using this with composite
1382 * (multi-interface) devices. Instead, the driver for each interface may
David Brownella81e7ec2005-04-18 17:39:25 -07001383 * use usb_set_interface() on the interfaces it claims. Be careful though;
1384 * some devices don't support the SET_INTERFACE request, and others won't
David Vrabel3444b262009-04-08 17:36:28 +00001385 * reset all the interface state (notably endpoint state). Resetting the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * configuration would affect other drivers' interfaces.
1387 *
1388 * The caller must own the device lock.
1389 *
1390 * Returns zero on success, else a negative error code.
1391 */
1392int usb_reset_configuration(struct usb_device *dev)
1393{
1394 int i, retval;
1395 struct usb_host_config *config;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001396 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 if (dev->state == USB_STATE_SUSPENDED)
1399 return -EHOSTUNREACH;
1400
1401 /* caller must have locked the device and must own
1402 * the usb bus readlock (so driver bindings are stable);
1403 * calls during probe() are fine
1404 */
1405
1406 for (i = 1; i < 16; ++i) {
Alan Sternddeac4e2009-01-15 17:03:33 -05001407 usb_disable_endpoint(dev, i, true);
1408 usb_disable_endpoint(dev, i + USB_DIR_IN, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410
1411 config = dev->actconfig;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001412 retval = 0;
1413 mutex_lock(&hcd->bandwidth_mutex);
1414 /* Make sure we have enough bandwidth for each alternate setting 0 */
1415 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1416 struct usb_interface *intf = config->interface[i];
1417 struct usb_host_interface *alt;
1418
1419 alt = usb_altnum_to_altsetting(intf, 0);
1420 if (!alt)
1421 alt = &intf->altsetting[0];
1422 if (alt != intf->cur_altsetting)
1423 retval = usb_hcd_alloc_bandwidth(dev, NULL,
1424 intf->cur_altsetting, alt);
1425 if (retval < 0)
1426 break;
1427 }
1428 /* If not, reinstate the old alternate settings */
1429 if (retval < 0) {
1430reset_old_alts:
Roel Kluine4a3d942010-02-18 02:36:23 +01001431 for (i--; i >= 0; i--) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001432 struct usb_interface *intf = config->interface[i];
1433 struct usb_host_interface *alt;
1434
1435 alt = usb_altnum_to_altsetting(intf, 0);
1436 if (!alt)
1437 alt = &intf->altsetting[0];
1438 if (alt != intf->cur_altsetting)
1439 usb_hcd_alloc_bandwidth(dev, NULL,
1440 alt, intf->cur_altsetting);
1441 }
1442 mutex_unlock(&hcd->bandwidth_mutex);
1443 return retval;
1444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1446 USB_REQ_SET_CONFIGURATION, 0,
1447 config->desc.bConfigurationValue, 0,
1448 NULL, 0, USB_CTRL_SET_TIMEOUT);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001449 if (retval < 0)
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001450 goto reset_old_alts;
1451 mutex_unlock(&hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 /* re-init hc/hcd interface/endpoint state */
1454 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1455 struct usb_interface *intf = config->interface[i];
1456 struct usb_host_interface *alt;
1457
1458 alt = usb_altnum_to_altsetting(intf, 0);
1459
1460 /* No altsetting 0? We'll assume the first altsetting.
1461 * We could use a GetInterface call, but if a device is
1462 * so non-compliant that it doesn't have altsetting 0
1463 * then I wouldn't trust its reply anyway.
1464 */
1465 if (!alt)
1466 alt = &intf->altsetting[0];
1467
Alan Stern3b23dd62008-12-05 14:10:34 -05001468 if (alt != intf->cur_altsetting) {
1469 remove_intf_ep_devs(intf);
1470 usb_remove_sysfs_intf_files(intf);
1471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 intf->cur_altsetting = alt;
Alan Stern2caf7fc2008-12-31 11:31:33 -05001473 usb_enable_interface(dev, intf, true);
Alan Stern3b23dd62008-12-05 14:10:34 -05001474 if (device_is_registered(&intf->dev)) {
Alan Stern0e6c8e82005-10-24 15:33:03 -04001475 usb_create_sysfs_intf_files(intf);
Alan Stern3b23dd62008-12-05 14:10:34 -05001476 create_intf_ep_devs(intf);
1477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
1479 return 0;
1480}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001481EXPORT_SYMBOL_GPL(usb_reset_configuration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Greg Kroah-Hartmanb0e396e2007-08-02 22:44:27 -06001483static void usb_release_interface(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 struct usb_interface *intf = to_usb_interface(dev);
1486 struct usb_interface_cache *intfc =
1487 altsetting_to_usb_interface_cache(intf->altsetting);
1488
1489 kref_put(&intfc->ref, usb_release_interface_cache);
1490 kfree(intf);
1491}
1492
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001493#ifdef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +02001494static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001495{
1496 struct usb_device *usb_dev;
1497 struct usb_interface *intf;
1498 struct usb_host_interface *alt;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001499
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001500 intf = to_usb_interface(dev);
1501 usb_dev = interface_to_usbdev(intf);
1502 alt = intf->cur_altsetting;
1503
Kay Sievers7eff2e72007-08-14 15:15:12 +02001504 if (add_uevent_var(env, "INTERFACE=%d/%d/%d",
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001505 alt->desc.bInterfaceClass,
1506 alt->desc.bInterfaceSubClass,
1507 alt->desc.bInterfaceProtocol))
1508 return -ENOMEM;
1509
Kay Sievers7eff2e72007-08-14 15:15:12 +02001510 if (add_uevent_var(env,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001511 "MODALIAS=usb:"
1512 "v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001513 le16_to_cpu(usb_dev->descriptor.idVendor),
1514 le16_to_cpu(usb_dev->descriptor.idProduct),
1515 le16_to_cpu(usb_dev->descriptor.bcdDevice),
1516 usb_dev->descriptor.bDeviceClass,
1517 usb_dev->descriptor.bDeviceSubClass,
1518 usb_dev->descriptor.bDeviceProtocol,
1519 alt->desc.bInterfaceClass,
1520 alt->desc.bInterfaceSubClass,
1521 alt->desc.bInterfaceProtocol))
1522 return -ENOMEM;
1523
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001524 return 0;
1525}
1526
1527#else
1528
Kay Sievers7eff2e72007-08-14 15:15:12 +02001529static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001530{
1531 return -ENODEV;
1532}
1533#endif /* CONFIG_HOTPLUG */
1534
1535struct device_type usb_if_device_type = {
1536 .name = "usb_interface",
1537 .release = usb_release_interface,
1538 .uevent = usb_if_uevent,
1539};
1540
Craig W. Nadler165fe972007-06-15 23:14:35 -04001541static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001542 struct usb_host_config *config,
1543 u8 inum)
Craig W. Nadler165fe972007-06-15 23:14:35 -04001544{
1545 struct usb_interface_assoc_descriptor *retval = NULL;
1546 struct usb_interface_assoc_descriptor *intf_assoc;
1547 int first_intf;
1548 int last_intf;
1549 int i;
1550
1551 for (i = 0; (i < USB_MAXIADS && config->intf_assoc[i]); i++) {
1552 intf_assoc = config->intf_assoc[i];
1553 if (intf_assoc->bInterfaceCount == 0)
1554 continue;
1555
1556 first_intf = intf_assoc->bFirstInterface;
1557 last_intf = first_intf + (intf_assoc->bInterfaceCount - 1);
1558 if (inum >= first_intf && inum <= last_intf) {
1559 if (!retval)
1560 retval = intf_assoc;
1561 else
1562 dev_err(&dev->dev, "Interface #%d referenced"
1563 " by multiple IADs\n", inum);
1564 }
1565 }
1566
1567 return retval;
1568}
1569
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001570
1571/*
1572 * Internal function to queue a device reset
1573 *
1574 * This is initialized into the workstruct in 'struct
1575 * usb_device->reset_ws' that is launched by
1576 * message.c:usb_set_configuration() when initializing each 'struct
1577 * usb_interface'.
1578 *
1579 * It is safe to get the USB device without reference counts because
1580 * the life cycle of @iface is bound to the life cycle of @udev. Then,
1581 * this function will be ran only if @iface is alive (and before
1582 * freeing it any scheduled instances of it will have been cancelled).
1583 *
1584 * We need to set a flag (usb_dev->reset_running) because when we call
1585 * the reset, the interfaces might be unbound. The current interface
1586 * cannot try to remove the queued work as it would cause a deadlock
1587 * (you cannot remove your work from within your executing
1588 * workqueue). This flag lets it know, so that
1589 * usb_cancel_queued_reset() doesn't try to do it.
1590 *
1591 * See usb_queue_reset_device() for more details
1592 */
Felipe Balbi09e81f32009-12-04 15:47:44 +02001593static void __usb_queue_reset_device(struct work_struct *ws)
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001594{
1595 int rc;
1596 struct usb_interface *iface =
1597 container_of(ws, struct usb_interface, reset_ws);
1598 struct usb_device *udev = interface_to_usbdev(iface);
1599
1600 rc = usb_lock_device_for_reset(udev, iface);
1601 if (rc >= 0) {
1602 iface->reset_running = 1;
1603 usb_reset_device(udev);
1604 iface->reset_running = 0;
1605 usb_unlock_device(udev);
1606 }
1607}
1608
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610/*
1611 * usb_set_configuration - Makes a particular device setting be current
1612 * @dev: the device whose configuration is being updated
1613 * @configuration: the configuration being chosen.
1614 * Context: !in_interrupt(), caller owns the device lock
1615 *
1616 * This is used to enable non-default device modes. Not all devices
1617 * use this kind of configurability; many devices only have one
1618 * configuration.
1619 *
Alan Stern3f141e22007-02-08 16:40:43 -05001620 * @configuration is the value of the configuration to be installed.
1621 * According to the USB spec (e.g. section 9.1.1.5), configuration values
1622 * must be non-zero; a value of zero indicates that the device in
1623 * unconfigured. However some devices erroneously use 0 as one of their
1624 * configuration values. To help manage such devices, this routine will
1625 * accept @configuration = -1 as indicating the device should be put in
1626 * an unconfigured state.
1627 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 * USB device configurations may affect Linux interoperability,
1629 * power consumption and the functionality available. For example,
1630 * the default configuration is limited to using 100mA of bus power,
1631 * so that when certain device functionality requires more power,
1632 * and the device is bus powered, that functionality should be in some
1633 * non-default device configuration. Other device modes may also be
1634 * reflected as configuration options, such as whether two ISDN
1635 * channels are available independently; and choosing between open
1636 * standard device protocols (like CDC) or proprietary ones.
1637 *
Inaky Perez-Gonzalez16bbab22007-07-31 20:34:01 -07001638 * Note that a non-authorized device (dev->authorized == 0) will only
1639 * be put in unconfigured mode.
1640 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 * Note that USB has an additional level of device configurability,
1642 * associated with interfaces. That configurability is accessed using
1643 * usb_set_interface().
1644 *
1645 * This call is synchronous. The calling context must be able to sleep,
1646 * must own the device lock, and must not hold the driver model's USB
Ming Lei6d243e52008-06-17 23:24:08 +08001647 * bus mutex; usb interface driver probe() methods cannot use this routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 *
1649 * Returns zero on success, or else the status code returned by the
Steven Cole093cf722005-05-03 19:07:24 -06001650 * underlying call that failed. On successful completion, each interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 * in the original device configuration has been destroyed, and each one
1652 * in the new configuration has been probed by all relevant usb device
1653 * drivers currently known to the kernel.
1654 */
1655int usb_set_configuration(struct usb_device *dev, int configuration)
1656{
1657 int i, ret;
1658 struct usb_host_config *cp = NULL;
1659 struct usb_interface **new_interfaces = NULL;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001660 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 int n, nintf;
1662
Inaky Perez-Gonzalez16bbab22007-07-31 20:34:01 -07001663 if (dev->authorized == 0 || configuration == -1)
Alan Stern3f141e22007-02-08 16:40:43 -05001664 configuration = 0;
1665 else {
1666 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1667 if (dev->config[i].desc.bConfigurationValue ==
1668 configuration) {
1669 cp = &dev->config[i];
1670 break;
1671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673 }
1674 if ((!cp && configuration != 0))
1675 return -EINVAL;
1676
1677 /* The USB spec says configuration 0 means unconfigured.
1678 * But if a device includes a configuration numbered 0,
1679 * we will accept it as a correctly configured state.
Alan Stern3f141e22007-02-08 16:40:43 -05001680 * Use -1 if you really want to unconfigure the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 */
1682 if (cp && configuration == 0)
1683 dev_warn(&dev->dev, "config 0 descriptor??\n");
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 /* Allocate memory for new interfaces before doing anything else,
1686 * so that if we run out then nothing will have changed. */
1687 n = nintf = 0;
1688 if (cp) {
1689 nintf = cp->desc.bNumInterfaces;
1690 new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
Oliver Neukumacbe2fe2010-01-12 12:32:50 +01001691 GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (!new_interfaces) {
Joe Perches898eb712007-10-18 03:06:30 -07001693 dev_err(&dev->dev, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return -ENOMEM;
1695 }
1696
1697 for (; n < nintf; ++n) {
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001698 new_interfaces[n] = kzalloc(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 sizeof(struct usb_interface),
Oliver Neukumacbe2fe2010-01-12 12:32:50 +01001700 GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (!new_interfaces[n]) {
Joe Perches898eb712007-10-18 03:06:30 -07001702 dev_err(&dev->dev, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 ret = -ENOMEM;
1704free_interfaces:
1705 while (--n >= 0)
1706 kfree(new_interfaces[n]);
1707 kfree(new_interfaces);
1708 return ret;
1709 }
1710 }
Alan Stern6ad07122006-06-01 13:59:16 -04001711
1712 i = dev->bus_mA - cp->desc.bMaxPower * 2;
1713 if (i < 0)
1714 dev_warn(&dev->dev, "new config #%d exceeds power "
1715 "limit by %dmA\n",
1716 configuration, -i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
1718
Alan Stern01d883d2006-08-30 15:47:18 -04001719 /* Wake up the device so we can send it the Set-Config request */
Alan Stern94fcda12006-11-20 11:38:46 -05001720 ret = usb_autoresume_device(dev);
Alan Stern01d883d2006-08-30 15:47:18 -04001721 if (ret)
1722 goto free_interfaces;
1723
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001724 /* Make sure we have bandwidth (and available HCD resources) for this
1725 * configuration. Remove endpoints from the schedule if we're dropping
1726 * this configuration to set configuration 0. After this point, the
1727 * host controller will not allow submissions to dropped endpoints. If
1728 * this call fails, the device state is unchanged.
1729 */
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001730 mutex_lock(&hcd->bandwidth_mutex);
1731 ret = usb_hcd_alloc_bandwidth(dev, cp, NULL, NULL);
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001732 if (ret < 0) {
1733 usb_autosuspend_device(dev);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001734 mutex_unlock(&hcd->bandwidth_mutex);
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001735 goto free_interfaces;
1736 }
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 /* if it's already configured, clear out old state first.
1739 * getting rid of old interfaces means unbinding their drivers.
1740 */
1741 if (dev->state != USB_STATE_ADDRESS)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001742 usb_disable_device(dev, 1); /* Skip ep0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Alan Sterndf718962008-12-19 10:27:56 -05001744 /* Get rid of pending async Set-Config requests for this device */
1745 cancel_async_set_config(dev);
1746
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001747 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1748 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
1749 NULL, 0, USB_CTRL_SET_TIMEOUT);
1750 if (ret < 0) {
Alan Stern6ad07122006-06-01 13:59:16 -04001751 /* All the old state is gone, so what else can we do?
1752 * The device is probably useless now anyway.
1753 */
1754 cp = NULL;
1755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 dev->actconfig = cp;
Alan Stern6ad07122006-06-01 13:59:16 -04001758 if (!cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 usb_set_device_state(dev, USB_STATE_ADDRESS);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001760 usb_hcd_alloc_bandwidth(dev, NULL, NULL, NULL);
Alan Stern94fcda12006-11-20 11:38:46 -05001761 usb_autosuspend_device(dev);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001762 mutex_unlock(&hcd->bandwidth_mutex);
Alan Stern6ad07122006-06-01 13:59:16 -04001763 goto free_interfaces;
1764 }
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001765 mutex_unlock(&hcd->bandwidth_mutex);
Alan Stern6ad07122006-06-01 13:59:16 -04001766 usb_set_device_state(dev, USB_STATE_CONFIGURED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Alan Stern6ad07122006-06-01 13:59:16 -04001768 /* Initialize the new interface structures and the
1769 * hc/hcd/usbcore interface/endpoint state.
1770 */
1771 for (i = 0; i < nintf; ++i) {
1772 struct usb_interface_cache *intfc;
1773 struct usb_interface *intf;
1774 struct usb_host_interface *alt;
1775
1776 cp->interface[i] = intf = new_interfaces[i];
1777 intfc = cp->intf_cache[i];
1778 intf->altsetting = intfc->altsetting;
1779 intf->num_altsetting = intfc->num_altsetting;
Craig W. Nadler165fe972007-06-15 23:14:35 -04001780 intf->intf_assoc = find_iad(dev, cp, i);
Alan Stern6ad07122006-06-01 13:59:16 -04001781 kref_get(&intfc->ref);
1782
1783 alt = usb_altnum_to_altsetting(intf, 0);
1784
1785 /* No altsetting 0? We'll assume the first altsetting.
1786 * We could use a GetInterface call, but if a device is
1787 * so non-compliant that it doesn't have altsetting 0
1788 * then I wouldn't trust its reply anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 */
Alan Stern6ad07122006-06-01 13:59:16 -04001790 if (!alt)
1791 alt = &intf->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Alan Stern6ad07122006-06-01 13:59:16 -04001793 intf->cur_altsetting = alt;
Alan Stern2caf7fc2008-12-31 11:31:33 -05001794 usb_enable_interface(dev, intf, true);
Alan Stern6ad07122006-06-01 13:59:16 -04001795 intf->dev.parent = &dev->dev;
1796 intf->dev.driver = NULL;
1797 intf->dev.bus = &usb_bus_type;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001798 intf->dev.type = &usb_if_device_type;
Alan Stern2e5f10e2008-04-30 15:37:19 -04001799 intf->dev.groups = usb_interface_groups;
Alan Stern6ad07122006-06-01 13:59:16 -04001800 intf->dev.dma_mask = dev->dev.dma_mask;
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001801 INIT_WORK(&intf->reset_ws, __usb_queue_reset_device);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001802 device_initialize(&intf->dev);
Kay Sievers0031a062008-05-02 06:02:41 +02001803 dev_set_name(&intf->dev, "%d-%s:%d.%d",
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001804 dev->bus->busnum, dev->devpath,
1805 configuration, alt->desc.bInterfaceNumber);
Alan Stern6ad07122006-06-01 13:59:16 -04001806 }
1807 kfree(new_interfaces);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Alan Stern1662e3a2009-03-18 14:28:53 -04001809 if (cp->string == NULL &&
1810 !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
Alan Stern6ad07122006-06-01 13:59:16 -04001811 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Alan Stern6ad07122006-06-01 13:59:16 -04001813 /* Now that all the interfaces are set up, register them
1814 * to trigger binding of drivers to interfaces. probe()
1815 * routines may install different altsettings and may
1816 * claim() any interfaces not yet bound. Many class drivers
1817 * need that: CDC, audio, video, etc.
1818 */
1819 for (i = 0; i < nintf; ++i) {
1820 struct usb_interface *intf = cp->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001822 dev_dbg(&dev->dev,
Alan Stern6ad07122006-06-01 13:59:16 -04001823 "adding %s (config #%d, interface %d)\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001824 dev_name(&intf->dev), configuration,
Alan Stern6ad07122006-06-01 13:59:16 -04001825 intf->cur_altsetting->desc.bInterfaceNumber);
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01001826 device_enable_async_suspend(&intf->dev);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001827 ret = device_add(&intf->dev);
Alan Stern6ad07122006-06-01 13:59:16 -04001828 if (ret != 0) {
1829 dev_err(&dev->dev, "device_add(%s) --> %d\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001830 dev_name(&intf->dev), ret);
Alan Stern6ad07122006-06-01 13:59:16 -04001831 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
Alan Stern3b23dd62008-12-05 14:10:34 -05001833 create_intf_ep_devs(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
1835
Alan Stern94fcda12006-11-20 11:38:46 -05001836 usb_autosuspend_device(dev);
Alan Stern86d30742005-07-29 12:17:16 -07001837 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Alan Sterndf718962008-12-19 10:27:56 -05001840static LIST_HEAD(set_config_list);
1841static DEFINE_SPINLOCK(set_config_lock);
1842
Alan Stern088dc272006-08-21 12:08:19 -04001843struct set_config_request {
1844 struct usb_device *udev;
1845 int config;
1846 struct work_struct work;
Alan Sterndf718962008-12-19 10:27:56 -05001847 struct list_head node;
Alan Stern088dc272006-08-21 12:08:19 -04001848};
1849
1850/* Worker routine for usb_driver_set_configuration() */
David Howellsc4028952006-11-22 14:57:56 +00001851static void driver_set_config_work(struct work_struct *work)
Alan Stern088dc272006-08-21 12:08:19 -04001852{
David Howellsc4028952006-11-22 14:57:56 +00001853 struct set_config_request *req =
1854 container_of(work, struct set_config_request, work);
Alan Sterndf718962008-12-19 10:27:56 -05001855 struct usb_device *udev = req->udev;
Alan Stern088dc272006-08-21 12:08:19 -04001856
Alan Sterndf718962008-12-19 10:27:56 -05001857 usb_lock_device(udev);
1858 spin_lock(&set_config_lock);
1859 list_del(&req->node);
1860 spin_unlock(&set_config_lock);
1861
1862 if (req->config >= -1) /* Is req still valid? */
1863 usb_set_configuration(udev, req->config);
1864 usb_unlock_device(udev);
1865 usb_put_dev(udev);
Alan Stern088dc272006-08-21 12:08:19 -04001866 kfree(req);
1867}
1868
Alan Sterndf718962008-12-19 10:27:56 -05001869/* Cancel pending Set-Config requests for a device whose configuration
1870 * was just changed
1871 */
1872static void cancel_async_set_config(struct usb_device *udev)
1873{
1874 struct set_config_request *req;
1875
1876 spin_lock(&set_config_lock);
1877 list_for_each_entry(req, &set_config_list, node) {
1878 if (req->udev == udev)
1879 req->config = -999; /* Mark as cancelled */
1880 }
1881 spin_unlock(&set_config_lock);
1882}
1883
Alan Stern088dc272006-08-21 12:08:19 -04001884/**
1885 * usb_driver_set_configuration - Provide a way for drivers to change device configurations
1886 * @udev: the device whose configuration is being updated
1887 * @config: the configuration being chosen.
1888 * Context: In process context, must be able to sleep
1889 *
1890 * Device interface drivers are not allowed to change device configurations.
1891 * This is because changing configurations will destroy the interface the
1892 * driver is bound to and create new ones; it would be like a floppy-disk
1893 * driver telling the computer to replace the floppy-disk drive with a
1894 * tape drive!
1895 *
1896 * Still, in certain specialized circumstances the need may arise. This
1897 * routine gets around the normal restrictions by using a work thread to
1898 * submit the change-config request.
1899 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001900 * Returns 0 if the request was successfully queued, error code otherwise.
Alan Stern088dc272006-08-21 12:08:19 -04001901 * The caller has no way to know whether the queued request will eventually
1902 * succeed.
1903 */
1904int usb_driver_set_configuration(struct usb_device *udev, int config)
1905{
1906 struct set_config_request *req;
1907
1908 req = kmalloc(sizeof(*req), GFP_KERNEL);
1909 if (!req)
1910 return -ENOMEM;
1911 req->udev = udev;
1912 req->config = config;
David Howellsc4028952006-11-22 14:57:56 +00001913 INIT_WORK(&req->work, driver_set_config_work);
Alan Stern088dc272006-08-21 12:08:19 -04001914
Alan Sterndf718962008-12-19 10:27:56 -05001915 spin_lock(&set_config_lock);
1916 list_add(&req->node, &set_config_list);
1917 spin_unlock(&set_config_lock);
1918
Alan Stern088dc272006-08-21 12:08:19 -04001919 usb_get_dev(udev);
Alan Stern1737bf2c2006-12-15 16:04:52 -05001920 schedule_work(&req->work);
Alan Stern088dc272006-08-21 12:08:19 -04001921 return 0;
1922}
1923EXPORT_SYMBOL_GPL(usb_driver_set_configuration);