blob: 9bd26dec7599762bdc15f476da58112b9bedb2cb [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/byteorder.h>
18
19#include "hcd.h" /* for usbcore internals */
20#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
Alan Sternd09d36a2005-09-26 16:22:45 -0400229 ep = (usb_pipein(pipe) ? usb_dev->ep_in : usb_dev->ep_out)
230 [usb_pipeendpoint(pipe)];
231 if (!ep || len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return -EINVAL;
233
Alan Sternd09d36a2005-09-26 16:22:45 -0400234 urb = usb_alloc_urb(0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (!urb)
236 return -ENOMEM;
237
Alan Sternd09d36a2005-09-26 16:22:45 -0400238 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
239 USB_ENDPOINT_XFER_INT) {
240 pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
241 usb_fill_int_urb(urb, usb_dev, pipe, data, len,
Alan Stern8d062b92007-04-23 17:30:32 -0400242 usb_api_blocking_completion, NULL,
243 ep->desc.bInterval);
Alan Sternd09d36a2005-09-26 16:22:45 -0400244 } else
245 usb_fill_bulk_urb(urb, usb_dev, pipe, data, len,
246 usb_api_blocking_completion, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 return usb_start_wait_urb(urb, timeout, actual_length);
249}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600250EXPORT_SYMBOL_GPL(usb_bulk_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252/*-------------------------------------------------------------------*/
253
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800254static void sg_clean(struct usb_sg_request *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 if (io->urbs) {
257 while (io->entries--)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800258 usb_free_urb(io->urbs [io->entries]);
259 kfree(io->urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 io->urbs = NULL;
261 }
262 if (io->dev->dev.dma_mask != NULL)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800263 usb_buffer_unmap_sg(io->dev, usb_pipein(io->pipe),
264 io->sg, io->nents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 io->dev = NULL;
266}
267
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800268static void sg_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800270 struct usb_sg_request *io = urb->context;
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700271 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800273 spin_lock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /* In 2.5 we require hcds' endpoint queues not to progress after fault
276 * reports, until the completion callback (this!) returns. That lets
277 * device driver code (like this routine) unlink queued urbs first,
278 * if it needs to, since the HC won't work on them at all. So it's
279 * not possible for page N+1 to overwrite page N, and so on.
280 *
281 * That's only for "hard" faults; "soft" faults (unlinks) sometimes
282 * complete before the HCD can get requests away from hardware,
283 * though never during cleanup after a hard fault.
284 */
285 if (io->status
286 && (io->status != -ECONNRESET
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700287 || status != -ECONNRESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 && urb->actual_length) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800289 dev_err(io->dev->bus->controller,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 "dev %s ep%d%s scatterlist error %d/%d\n",
291 io->dev->devpath,
Alan Stern5e60a162007-07-30 17:07:21 -0400292 usb_endpoint_num(&urb->ep->desc),
293 usb_urb_dir_in(urb) ? "in" : "out",
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700294 status, io->status);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800295 /* BUG (); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700298 if (io->status == 0 && status && status != -ECONNRESET) {
299 int i, found, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700301 io->status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 /* the previous urbs, and this one, completed already.
304 * unlink pending urbs so they won't rx/tx bad data.
305 * careful: unlink can sometimes be synchronous...
306 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800307 spin_unlock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 for (i = 0, found = 0; i < io->entries; i++) {
309 if (!io->urbs [i] || !io->urbs [i]->dev)
310 continue;
311 if (found) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800312 retval = usb_unlink_urb(io->urbs [i]);
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700313 if (retval != -EINPROGRESS &&
314 retval != -ENODEV &&
315 retval != -EBUSY)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800316 dev_err(&io->dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 "%s, unlink --> %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800318 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 } else if (urb == io->urbs [i])
320 found = 1;
321 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800322 spin_lock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324 urb->dev = NULL;
325
326 /* on the last completion, signal usb_sg_wait() */
327 io->bytes += urb->actual_length;
328 io->count--;
329 if (!io->count)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800330 complete(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800332 spin_unlock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335
336/**
337 * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request
338 * @io: request block being initialized. until usb_sg_wait() returns,
339 * treat this as a pointer to an opaque block of memory,
340 * @dev: the usb device that will send or receive the data
341 * @pipe: endpoint "pipe" used to transfer the data
342 * @period: polling rate for interrupt endpoints, in frames or
343 * (for high speed endpoints) microframes; ignored for bulk
344 * @sg: scatterlist entries
345 * @nents: how many entries in the scatterlist
346 * @length: how many bytes to send from the scatterlist, or zero to
347 * send every byte identified in the list.
348 * @mem_flags: SLAB_* flags affecting memory allocations in this call
349 *
350 * Returns zero for success, else a negative errno value. This initializes a
351 * scatter/gather request, allocating resources such as I/O mappings and urb
352 * memory (except maybe memory used by USB controller drivers).
353 *
354 * The request must be issued using usb_sg_wait(), which waits for the I/O to
355 * complete (or to be canceled) and then cleans up all resources allocated by
356 * usb_sg_init().
357 *
358 * The request may be canceled with usb_sg_cancel(), either before or after
359 * usb_sg_wait() is called.
360 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800361int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
362 unsigned pipe, unsigned period, struct scatterlist *sg,
363 int nents, size_t length, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800365 int i;
366 int urb_flags;
367 int dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 if (!io || !dev || !sg
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800370 || usb_pipecontrol(pipe)
371 || usb_pipeisoc(pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 || nents <= 0)
373 return -EINVAL;
374
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800375 spin_lock_init(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 io->dev = dev;
377 io->pipe = pipe;
378 io->sg = sg;
379 io->nents = nents;
380
381 /* not all host controllers use DMA (like the mainstream pci ones);
382 * they can use PIO (sl811) or be software over another transport.
383 */
384 dma = (dev->dev.dma_mask != NULL);
385 if (dma)
Alan Stern5e60a162007-07-30 17:07:21 -0400386 io->entries = usb_buffer_map_sg(dev, usb_pipein(pipe),
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800387 sg, nents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 else
389 io->entries = nents;
390
391 /* initialize all the urbs we'll use */
392 if (io->entries <= 0)
393 return io->entries;
394
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800395 io->urbs = kmalloc(io->entries * sizeof *io->urbs, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (!io->urbs)
397 goto nomem;
398
Yoshihiro Shimodae2722522008-04-21 13:48:22 +0900399 urb_flags = URB_NO_INTERRUPT;
400 if (dma)
401 urb_flags |= URB_NO_TRANSFER_DMA_MAP;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800402 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 urb_flags |= URB_SHORT_NOT_OK;
404
Alan Stern7c3e28b2008-06-16 12:11:39 -0400405 for_each_sg(sg, sg, io->entries, i) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800406 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800408 io->urbs[i] = usb_alloc_urb(0, mem_flags);
409 if (!io->urbs[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 io->entries = i;
411 goto nomem;
412 }
413
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800414 io->urbs[i]->dev = NULL;
415 io->urbs[i]->pipe = pipe;
416 io->urbs[i]->interval = period;
417 io->urbs[i]->transfer_flags = urb_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800419 io->urbs[i]->complete = sg_complete;
420 io->urbs[i]->context = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Tony Lindgren35d07fd2007-03-31 18:15:43 -0700422 /*
423 * Some systems need to revert to PIO when DMA is temporarily
424 * unavailable. For their sakes, both transfer_buffer and
425 * transfer_dma are set when possible. However this can only
David Brownella12b8db2007-07-22 15:13:13 -0700426 * work on systems without:
427 *
428 * - HIGHMEM, since DMA buffers located in high memory are
429 * not directly addressable by the CPU for PIO;
430 *
431 * - IOMMU, since dma_map_sg() is allowed to use an IOMMU to
432 * make virtually discontiguous buffers be "dma-contiguous"
433 * so that PIO and DMA need diferent numbers of URBs.
434 *
435 * So when HIGHMEM or IOMMU are in use, transfer_buffer is NULL
Tony Lindgren35d07fd2007-03-31 18:15:43 -0700436 * to prevent stale pointers and to help spot bugs.
437 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (dma) {
Alan Stern7c3e28b2008-06-16 12:11:39 -0400439 io->urbs[i]->transfer_dma = sg_dma_address(sg);
440 len = sg_dma_len(sg);
Joerg Roedel966396d2007-10-24 12:49:48 +0200441#if defined(CONFIG_HIGHMEM) || defined(CONFIG_GART_IOMMU)
Tony Lindgren35d07fd2007-03-31 18:15:43 -0700442 io->urbs[i]->transfer_buffer = NULL;
443#else
Alan Stern7c3e28b2008-06-16 12:11:39 -0400444 io->urbs[i]->transfer_buffer = sg_virt(sg);
Tony Lindgren35d07fd2007-03-31 18:15:43 -0700445#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 } else {
447 /* hc may use _only_ transfer_buffer */
Alan Stern7c3e28b2008-06-16 12:11:39 -0400448 io->urbs[i]->transfer_buffer = sg_virt(sg);
449 len = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451
452 if (length) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800453 len = min_t(unsigned, len, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 length -= len;
455 if (length == 0)
456 io->entries = i + 1;
457 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800458 io->urbs[i]->transfer_buffer_length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800460 io->urbs[--i]->transfer_flags &= ~URB_NO_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /* transaction state */
Alan Stern580da342008-08-05 13:05:17 -0400463 io->count = io->entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 io->status = 0;
465 io->bytes = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800466 init_completion(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return 0;
468
469nomem:
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800470 sg_clean(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return -ENOMEM;
472}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600473EXPORT_SYMBOL_GPL(usb_sg_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475/**
476 * usb_sg_wait - synchronously execute scatter/gather request
477 * @io: request block handle, as initialized with usb_sg_init().
478 * some fields become accessible when this call returns.
479 * Context: !in_interrupt ()
480 *
481 * This function blocks until the specified I/O operation completes. It
482 * leverages the grouping of the related I/O requests to get good transfer
483 * rates, by queueing the requests. At higher speeds, such queuing can
484 * significantly improve USB throughput.
485 *
486 * There are three kinds of completion for this function.
487 * (1) success, where io->status is zero. The number of io->bytes
488 * transferred is as requested.
489 * (2) error, where io->status is a negative errno value. The number
490 * of io->bytes transferred before the error is usually less
491 * than requested, and can be nonzero.
Steven Cole093cf722005-05-03 19:07:24 -0600492 * (3) cancellation, a type of error with status -ECONNRESET that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * is initiated by usb_sg_cancel().
494 *
495 * When this function returns, all memory allocated through usb_sg_init() or
496 * this call will have been freed. The request block parameter may still be
497 * passed to usb_sg_cancel(), or it may be freed. It could also be
498 * reinitialized and then reused.
499 *
500 * Data Transfer Rates:
501 *
502 * Bulk transfers are valid for full or high speed endpoints.
503 * The best full speed data rate is 19 packets of 64 bytes each
504 * per frame, or 1216 bytes per millisecond.
505 * The best high speed data rate is 13 packets of 512 bytes each
506 * per microframe, or 52 KBytes per millisecond.
507 *
508 * The reason to use interrupt transfers through this API would most likely
509 * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
510 * could be transferred. That capability is less useful for low or full
511 * speed interrupt endpoints, which allow at most one packet per millisecond,
512 * of at most 8 or 64 bytes (respectively).
513 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800514void usb_sg_wait(struct usb_sg_request *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800516 int i;
517 int entries = io->entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 /* queue the urbs. */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800520 spin_lock_irq(&io->lock);
Alan Stern8ccef0d2007-06-21 16:26:46 -0400521 i = 0;
522 while (i < entries && !io->status) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800523 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800525 io->urbs[i]->dev = io->dev;
526 retval = usb_submit_urb(io->urbs [i], GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 /* after we submit, let completions or cancelations fire;
529 * we handshake using io->status.
530 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800531 spin_unlock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 switch (retval) {
533 /* maybe we retrying will recover */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800534 case -ENXIO: /* hc didn't queue this one */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 case -EAGAIN:
536 case -ENOMEM:
537 io->urbs[i]->dev = NULL;
538 retval = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800539 yield();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 break;
541
542 /* no error? continue immediately.
543 *
544 * NOTE: to work better with UHCI (4K I/O buffer may
545 * need 3K of TDs) it may be good to limit how many
546 * URBs are queued at once; N milliseconds?
547 */
548 case 0:
Alan Stern8ccef0d2007-06-21 16:26:46 -0400549 ++i;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800550 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 break;
552
553 /* fail any uncompleted urbs */
554 default:
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800555 io->urbs[i]->dev = NULL;
556 io->urbs[i]->status = retval;
557 dev_dbg(&io->dev->dev, "%s, submit --> %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800558 __func__, retval);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800559 usb_sg_cancel(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800561 spin_lock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (retval && (io->status == 0 || io->status == -ECONNRESET))
563 io->status = retval;
564 }
565 io->count -= entries - i;
566 if (io->count == 0)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800567 complete(&io->complete);
568 spin_unlock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /* OK, yes, this could be packaged as non-blocking.
571 * So could the submit loop above ... but it's easier to
572 * solve neither problem than to solve both!
573 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800574 wait_for_completion(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800576 sg_clean(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600578EXPORT_SYMBOL_GPL(usb_sg_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580/**
581 * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
582 * @io: request block, initialized with usb_sg_init()
583 *
584 * This stops a request after it has been started by usb_sg_wait().
585 * It can also prevents one initialized by usb_sg_init() from starting,
586 * so that call just frees resources allocated to the request.
587 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800588void usb_sg_cancel(struct usb_sg_request *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800590 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800592 spin_lock_irqsave(&io->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /* shut everything down, if it didn't already */
595 if (!io->status) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800596 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 io->status = -ECONNRESET;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800599 spin_unlock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 for (i = 0; i < io->entries; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800601 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if (!io->urbs [i]->dev)
604 continue;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800605 retval = usb_unlink_urb(io->urbs [i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (retval != -EINPROGRESS && retval != -EBUSY)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800607 dev_warn(&io->dev->dev, "%s, unlink --> %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800608 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800610 spin_lock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800612 spin_unlock_irqrestore(&io->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600614EXPORT_SYMBOL_GPL(usb_sg_cancel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616/*-------------------------------------------------------------------*/
617
618/**
619 * usb_get_descriptor - issues a generic GET_DESCRIPTOR request
620 * @dev: the device whose descriptor is being retrieved
621 * @type: the descriptor type (USB_DT_*)
622 * @index: the number of the descriptor
623 * @buf: where to put the descriptor
624 * @size: how big is "buf"?
625 * Context: !in_interrupt ()
626 *
627 * Gets a USB descriptor. Convenience functions exist to simplify
628 * getting some types of descriptors. Use
629 * usb_get_string() or usb_string() for USB_DT_STRING.
630 * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG)
631 * are part of the device structure.
632 * In addition to a number of USB-standard descriptors, some
633 * devices also use class-specific or vendor-specific descriptors.
634 *
635 * This call is synchronous, and may not be used in an interrupt context.
636 *
637 * Returns the number of bytes received on success, or else the status code
638 * returned by the underlying usb_control_msg() call.
639 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800640int usb_get_descriptor(struct usb_device *dev, unsigned char type,
641 unsigned char index, void *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 int i;
644 int result;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800645
646 memset(buf, 0, size); /* Make sure we parse really received data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 for (i = 0; i < 3; ++i) {
Alan Sternc39772d2007-08-20 10:45:28 -0400649 /* retry on length 0 or error; some devices are flakey */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
651 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
652 (type << 8) + index, 0, buf, size,
653 USB_CTRL_GET_TIMEOUT);
Alan Sternc39772d2007-08-20 10:45:28 -0400654 if (result <= 0 && result != -ETIMEDOUT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 continue;
656 if (result > 1 && ((u8 *)buf)[1] != type) {
Alan Stern67f5a4b2009-02-20 16:33:08 -0500657 result = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 continue;
659 }
660 break;
661 }
662 return result;
663}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600664EXPORT_SYMBOL_GPL(usb_get_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666/**
667 * usb_get_string - gets a string descriptor
668 * @dev: the device whose string descriptor is being retrieved
669 * @langid: code for language chosen (from string descriptor zero)
670 * @index: the number of the descriptor
671 * @buf: where to put the string
672 * @size: how big is "buf"?
673 * Context: !in_interrupt ()
674 *
675 * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character,
676 * in little-endian byte order).
677 * The usb_string() function will often be a convenient way to turn
678 * these strings into kernel-printable form.
679 *
680 * Strings may be referenced in device, configuration, interface, or other
681 * descriptors, and could also be used in vendor-specific ways.
682 *
683 * This call is synchronous, and may not be used in an interrupt context.
684 *
685 * Returns the number of bytes received on success, or else the status code
686 * returned by the underlying usb_control_msg() call.
687 */
Adrian Bunke266a122005-11-08 21:05:43 +0100688static int usb_get_string(struct usb_device *dev, unsigned short langid,
689 unsigned char index, void *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 int i;
692 int result;
693
694 for (i = 0; i < 3; ++i) {
695 /* retry on length 0 or stall; some devices are flakey */
696 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
697 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
698 (USB_DT_STRING << 8) + index, langid, buf, size,
699 USB_CTRL_GET_TIMEOUT);
Alan Stern67f5a4b2009-02-20 16:33:08 -0500700 if (result == 0 || result == -EPIPE)
701 continue;
702 if (result > 1 && ((u8 *) buf)[1] != USB_DT_STRING) {
703 result = -ENODATA;
704 continue;
705 }
706 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708 return result;
709}
710
711static void usb_try_string_workarounds(unsigned char *buf, int *length)
712{
713 int newlength, oldlength = *length;
714
715 for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
716 if (!isprint(buf[newlength]) || buf[newlength + 1])
717 break;
718
719 if (newlength > 2) {
720 buf[0] = newlength;
721 *length = newlength;
722 }
723}
724
725static int usb_string_sub(struct usb_device *dev, unsigned int langid,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800726 unsigned int index, unsigned char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 int rc;
729
730 /* Try to read the string descriptor by asking for the maximum
731 * possible number of bytes */
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100732 if (dev->quirks & USB_QUIRK_STRING_FETCH_255)
733 rc = -EIO;
734 else
735 rc = usb_get_string(dev, langid, index, buf, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 /* If that failed try to read the descriptor length, then
738 * ask for just that many bytes */
739 if (rc < 2) {
740 rc = usb_get_string(dev, langid, index, buf, 2);
741 if (rc == 2)
742 rc = usb_get_string(dev, langid, index, buf, buf[0]);
743 }
744
745 if (rc >= 2) {
746 if (!buf[0] && !buf[1])
747 usb_try_string_workarounds(buf, &rc);
748
749 /* There might be extra junk at the end of the descriptor */
750 if (buf[0] < rc)
751 rc = buf[0];
752
753 rc = rc - (rc & 1); /* force a multiple of two */
754 }
755
756 if (rc < 2)
757 rc = (rc < 0 ? rc : -EINVAL);
758
759 return rc;
760}
761
762/**
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200763 * usb_string - returns UTF-8 version of a string descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * @dev: the device whose string descriptor is being retrieved
765 * @index: the number of the descriptor
766 * @buf: where to put the string
767 * @size: how big is "buf"?
768 * Context: !in_interrupt ()
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800769 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 * This converts the UTF-16LE encoded strings returned by devices, from
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200771 * usb_get_string_descriptor(), to null-terminated UTF-8 encoded ones
772 * that are more usable in most kernel contexts. Note that this function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 * chooses strings in the first language supported by the device.
774 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * This call is synchronous, and may not be used in an interrupt context.
776 *
777 * Returns length of the string (>= 0) or usb_control_msg status (< 0).
778 */
779int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
780{
781 unsigned char *tbuf;
782 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 if (dev->state == USB_STATE_SUSPENDED)
785 return -EHOSTUNREACH;
786 if (size <= 0 || !buf || !index)
787 return -EINVAL;
788 buf[0] = 0;
Alan Stern74675a52009-04-30 10:08:18 -0400789 tbuf = kmalloc(256, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (!tbuf)
791 return -ENOMEM;
792
793 /* get langid for strings if it's not yet known */
794 if (!dev->have_langid) {
795 err = usb_string_sub(dev, 0, 0, tbuf);
796 if (err < 0) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800797 dev_err(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 "string descriptor 0 read error: %d\n",
799 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 } else if (err < 4) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800801 dev_err(&dev->dev, "string descriptor 0 too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 } else {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800803 dev->string_langid = tbuf[2] | (tbuf[3] << 8);
804 /* always use the first langid listed */
805 dev_dbg(&dev->dev, "default language 0x%04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 dev->string_langid);
807 }
Daniel Mackb7af0bb2009-03-20 19:58:57 +0100808
809 dev->have_langid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 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 */
Alan Stern74675a52009-04-30 10:08:18 -0400817 err = utf16s_to_utf8s((wchar_t *) &tbuf[2], (err - 2) / 2,
818 UTF16_LITTLE_ENDIAN, buf, size);
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200819 buf[err] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 if (tbuf[1] != USB_DT_STRING)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800822 dev_dbg(&dev->dev,
823 "wrong descriptor type %02x for string %d (\"%s\")\n",
824 tbuf[1], index, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 errout:
827 kfree(tbuf);
828 return err;
829}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600830EXPORT_SYMBOL_GPL(usb_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200832/* one UTF-8-encoded 16-bit character has at most three bytes */
833#define MAX_USB_STRING_SIZE (127 * 3 + 1)
834
Alan Stern4f62efe2005-10-24 16:24:14 -0400835/**
836 * usb_cache_string - read a string descriptor and cache it for later use
837 * @udev: the device whose string descriptor is being read
838 * @index: the descriptor index
839 *
840 * Returns a pointer to a kmalloc'ed buffer containing the descriptor string,
841 * or NULL if the index is 0 or the string could not be read.
842 */
843char *usb_cache_string(struct usb_device *udev, int index)
844{
845 char *buf;
846 char *smallbuf = NULL;
847 int len;
848
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800849 if (index <= 0)
850 return NULL;
851
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200852 buf = kmalloc(MAX_USB_STRING_SIZE, GFP_KERNEL);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800853 if (buf) {
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200854 len = usb_string(udev, index, buf, MAX_USB_STRING_SIZE);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800855 if (len > 0) {
856 smallbuf = kmalloc(++len, GFP_KERNEL);
857 if (!smallbuf)
Alan Stern4f62efe2005-10-24 16:24:14 -0400858 return buf;
859 memcpy(smallbuf, buf, len);
860 }
861 kfree(buf);
862 }
863 return smallbuf;
864}
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866/*
867 * usb_get_device_descriptor - (re)reads the device descriptor (usbcore)
868 * @dev: the device whose device descriptor is being updated
869 * @size: how much of the descriptor to read
870 * Context: !in_interrupt ()
871 *
872 * Updates the copy of the device descriptor stored in the device structure,
Laurent Pinchart6ab16a92006-11-07 10:16:25 +0100873 * which dedicates space for this purpose.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 *
875 * Not exported, only for use by the core. If drivers really want to read
876 * the device descriptor directly, they can call usb_get_descriptor() with
877 * type = USB_DT_DEVICE and index = 0.
878 *
879 * This call is synchronous, and may not be used in an interrupt context.
880 *
881 * Returns the number of bytes received on success, or else the status code
882 * returned by the underlying usb_control_msg() call.
883 */
884int usb_get_device_descriptor(struct usb_device *dev, unsigned int size)
885{
886 struct usb_device_descriptor *desc;
887 int ret;
888
889 if (size > sizeof(*desc))
890 return -EINVAL;
891 desc = kmalloc(sizeof(*desc), GFP_NOIO);
892 if (!desc)
893 return -ENOMEM;
894
895 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800896 if (ret >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 memcpy(&dev->descriptor, desc, size);
898 kfree(desc);
899 return ret;
900}
901
902/**
903 * usb_get_status - issues a GET_STATUS call
904 * @dev: the device whose status is being checked
905 * @type: USB_RECIP_*; for device, interface, or endpoint
906 * @target: zero (for device), else interface or endpoint number
907 * @data: pointer to two bytes of bitmap data
908 * Context: !in_interrupt ()
909 *
910 * Returns device, interface, or endpoint status. Normally only of
911 * interest to see if the device is self powered, or has enabled the
912 * remote wakeup facility; or whether a bulk or interrupt endpoint
913 * is halted ("stalled").
914 *
915 * Bits in these status bitmaps are set using the SET_FEATURE request,
916 * and cleared using the CLEAR_FEATURE request. The usb_clear_halt()
917 * function should be used to clear halt ("stall") status.
918 *
919 * This call is synchronous, and may not be used in an interrupt context.
920 *
921 * Returns the number of bytes received on success, or else the status code
922 * returned by the underlying usb_control_msg() call.
923 */
924int usb_get_status(struct usb_device *dev, int type, int target, void *data)
925{
926 int ret;
927 u16 *status = kmalloc(sizeof(*status), GFP_KERNEL);
928
929 if (!status)
930 return -ENOMEM;
931
932 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
933 USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status,
934 sizeof(*status), USB_CTRL_GET_TIMEOUT);
935
936 *(u16 *)data = *status;
937 kfree(status);
938 return ret;
939}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600940EXPORT_SYMBOL_GPL(usb_get_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942/**
943 * usb_clear_halt - tells device to clear endpoint halt/stall condition
944 * @dev: device whose endpoint is halted
945 * @pipe: endpoint "pipe" being cleared
946 * Context: !in_interrupt ()
947 *
948 * This is used to clear halt conditions for bulk and interrupt endpoints,
949 * as reported by URB completion status. Endpoints that are halted are
950 * sometimes referred to as being "stalled". Such endpoints are unable
951 * to transmit or receive data until the halt status is cleared. Any URBs
952 * queued for such an endpoint should normally be unlinked by the driver
953 * before clearing the halt condition, as described in sections 5.7.5
954 * and 5.8.5 of the USB 2.0 spec.
955 *
956 * Note that control and isochronous endpoints don't halt, although control
957 * endpoints report "protocol stall" (for unsupported requests) using the
958 * same status code used to report a true stall.
959 *
960 * This call is synchronous, and may not be used in an interrupt context.
961 *
962 * Returns zero on success, or else the status code returned by the
963 * underlying usb_control_msg() call.
964 */
965int usb_clear_halt(struct usb_device *dev, int pipe)
966{
967 int result;
968 int endp = usb_pipeendpoint(pipe);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800969
970 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 endp |= USB_DIR_IN;
972
973 /* we don't care if it wasn't halted first. in fact some devices
974 * (like some ibmcam model 1 units) seem to expect hosts to make
975 * this request for iso endpoints, which can't halt!
976 */
977 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
978 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
979 USB_ENDPOINT_HALT, endp, NULL, 0,
980 USB_CTRL_SET_TIMEOUT);
981
982 /* don't un-halt or force to DATA0 except on success */
983 if (result < 0)
984 return result;
985
986 /* NOTE: seems like Microsoft and Apple don't bother verifying
987 * the clear "took", so some devices could lock up if you check...
988 * such as the Hagiwara FlashGate DUAL. So we won't bother.
989 *
990 * NOTE: make sure the logic here doesn't diverge much from
991 * the copy in usb-storage, for as long as we need two copies.
992 */
993
David Vrabel3444b262009-04-08 17:36:28 +0000994 usb_reset_endpoint(dev, endp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 return 0;
997}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600998EXPORT_SYMBOL_GPL(usb_clear_halt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Alan Stern3b23dd62008-12-05 14:10:34 -05001000static int create_intf_ep_devs(struct usb_interface *intf)
1001{
1002 struct usb_device *udev = interface_to_usbdev(intf);
1003 struct usb_host_interface *alt = intf->cur_altsetting;
1004 int i;
1005
1006 if (intf->ep_devs_created || intf->unregistering)
1007 return 0;
1008
1009 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1010 (void) usb_create_ep_devs(&intf->dev, &alt->endpoint[i], udev);
1011 intf->ep_devs_created = 1;
1012 return 0;
1013}
1014
1015static void remove_intf_ep_devs(struct usb_interface *intf)
1016{
1017 struct usb_host_interface *alt = intf->cur_altsetting;
1018 int i;
1019
1020 if (!intf->ep_devs_created)
1021 return;
1022
1023 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1024 usb_remove_ep_devs(&alt->endpoint[i]);
1025 intf->ep_devs_created = 0;
1026}
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028/**
1029 * usb_disable_endpoint -- Disable an endpoint by address
1030 * @dev: the device whose endpoint is being disabled
1031 * @epaddr: the endpoint's address. Endpoint number for output,
1032 * endpoint number + USB_DIR_IN for input
Alan Sternddeac4e2009-01-15 17:03:33 -05001033 * @reset_hardware: flag to erase any endpoint state stored in the
1034 * controller hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 *
Alan Sternddeac4e2009-01-15 17:03:33 -05001036 * Disables the endpoint for URB submission and nukes all pending URBs.
1037 * If @reset_hardware is set then also deallocates hcd/hardware state
1038 * for the endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 */
Alan Sternddeac4e2009-01-15 17:03:33 -05001040void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr,
1041 bool reset_hardware)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1044 struct usb_host_endpoint *ep;
1045
1046 if (!dev)
1047 return;
1048
1049 if (usb_endpoint_out(epaddr)) {
1050 ep = dev->ep_out[epnum];
Alan Sternddeac4e2009-01-15 17:03:33 -05001051 if (reset_hardware)
1052 dev->ep_out[epnum] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 } else {
1054 ep = dev->ep_in[epnum];
Alan Sternddeac4e2009-01-15 17:03:33 -05001055 if (reset_hardware)
1056 dev->ep_in[epnum] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
Alan Sternbdd016b2007-07-30 17:05:22 -04001058 if (ep) {
1059 ep->enabled = 0;
Alan Stern95cf82f2007-09-10 11:33:05 -04001060 usb_hcd_flush_endpoint(dev, ep);
Alan Sternddeac4e2009-01-15 17:03:33 -05001061 if (reset_hardware)
1062 usb_hcd_disable_endpoint(dev, ep);
Alan Sternbdd016b2007-07-30 17:05:22 -04001063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066/**
David Vrabel3444b262009-04-08 17:36:28 +00001067 * usb_reset_endpoint - Reset an endpoint's state.
1068 * @dev: the device whose endpoint is to be reset
1069 * @epaddr: the endpoint's address. Endpoint number for output,
1070 * endpoint number + USB_DIR_IN for input
1071 *
1072 * Resets any host-side endpoint state such as the toggle bit,
1073 * sequence number or current window.
1074 */
1075void usb_reset_endpoint(struct usb_device *dev, unsigned int epaddr)
1076{
1077 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1078 struct usb_host_endpoint *ep;
1079
1080 if (usb_endpoint_out(epaddr))
1081 ep = dev->ep_out[epnum];
1082 else
1083 ep = dev->ep_in[epnum];
1084 if (ep)
1085 usb_hcd_reset_endpoint(dev, ep);
1086}
1087EXPORT_SYMBOL_GPL(usb_reset_endpoint);
1088
1089
1090/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 * usb_disable_interface -- Disable all endpoints for an interface
1092 * @dev: the device whose interface is being disabled
1093 * @intf: pointer to the interface descriptor
Alan Sternddeac4e2009-01-15 17:03:33 -05001094 * @reset_hardware: flag to erase any endpoint state stored in the
1095 * controller hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 *
1097 * Disables all the endpoints for the interface's current altsetting.
1098 */
Alan Sternddeac4e2009-01-15 17:03:33 -05001099void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf,
1100 bool reset_hardware)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 struct usb_host_interface *alt = intf->cur_altsetting;
1103 int i;
1104
1105 for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
1106 usb_disable_endpoint(dev,
Alan Sternddeac4e2009-01-15 17:03:33 -05001107 alt->endpoint[i].desc.bEndpointAddress,
1108 reset_hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110}
1111
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001112/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 * usb_disable_device - Disable all the endpoints for a USB device
1114 * @dev: the device whose endpoints are being disabled
1115 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
1116 *
1117 * Disables all the device's endpoints, potentially including endpoint 0.
1118 * Deallocates hcd/hardware state for the endpoints (nuking all or most
1119 * pending urbs) and usbcore state for the interfaces, so that usbcore
1120 * must usb_set_configuration() before any interfaces could be used.
1121 */
1122void usb_disable_device(struct usb_device *dev, int skip_ep0)
1123{
1124 int i;
1125
Harvey Harrison441b62c2008-03-03 16:08:34 -08001126 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __func__,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001127 skip_ep0 ? "non-ep0" : "all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 for (i = skip_ep0; i < 16; ++i) {
Alan Sternddeac4e2009-01-15 17:03:33 -05001129 usb_disable_endpoint(dev, i, true);
1130 usb_disable_endpoint(dev, i + USB_DIR_IN, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 /* getting rid of interfaces will disconnect
1134 * any drivers bound to them (a key side effect)
1135 */
1136 if (dev->actconfig) {
1137 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1138 struct usb_interface *interface;
1139
Alan Stern86d30742005-07-29 12:17:16 -07001140 /* remove this interface if it has been registered */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 interface = dev->actconfig->interface[i];
Daniel Ritzd305ef52005-09-22 00:47:24 -07001142 if (!device_is_registered(&interface->dev))
Alan Stern86d30742005-07-29 12:17:16 -07001143 continue;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001144 dev_dbg(&dev->dev, "unregistering interface %s\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001145 dev_name(&interface->dev));
Alan Stern352d0262008-10-29 15:16:58 -04001146 interface->unregistering = 1;
Alan Stern3b23dd62008-12-05 14:10:34 -05001147 remove_intf_ep_devs(interface);
Alan Stern1a211752008-07-30 11:31:50 -04001148 device_del(&interface->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150
1151 /* Now that the interfaces are unbound, nobody should
1152 * try to access them.
1153 */
1154 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001155 put_device(&dev->actconfig->interface[i]->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 dev->actconfig->interface[i] = NULL;
1157 }
1158 dev->actconfig = NULL;
1159 if (dev->state == USB_STATE_CONFIGURED)
1160 usb_set_device_state(dev, USB_STATE_ADDRESS);
1161 }
1162}
1163
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001164/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 * usb_enable_endpoint - Enable an endpoint for USB communications
1166 * @dev: the device whose interface is being enabled
1167 * @ep: the endpoint
David Vrabel3444b262009-04-08 17:36:28 +00001168 * @reset_ep: flag to reset the endpoint state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 *
David Vrabel3444b262009-04-08 17:36:28 +00001170 * Resets the endpoint state if asked, and sets dev->ep_{in,out} pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 * For control endpoints, both the input and output sides are handled.
1172 */
Alan Stern2caf7fc2008-12-31 11:31:33 -05001173void usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep,
David Vrabel3444b262009-04-08 17:36:28 +00001174 bool reset_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Alan Sternbdd016b2007-07-30 17:05:22 -04001176 int epnum = usb_endpoint_num(&ep->desc);
1177 int is_out = usb_endpoint_dir_out(&ep->desc);
1178 int is_control = usb_endpoint_xfer_control(&ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
David Vrabel3444b262009-04-08 17:36:28 +00001180 if (reset_ep)
1181 usb_hcd_reset_endpoint(dev, ep);
1182 if (is_out || is_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 dev->ep_out[epnum] = ep;
David Vrabel3444b262009-04-08 17:36:28 +00001184 if (!is_out || is_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 dev->ep_in[epnum] = ep;
Alan Sternbdd016b2007-07-30 17:05:22 -04001186 ep->enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001189/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 * usb_enable_interface - Enable all the endpoints for an interface
1191 * @dev: the device whose interface is being enabled
1192 * @intf: pointer to the interface descriptor
David Vrabel3444b262009-04-08 17:36:28 +00001193 * @reset_eps: flag to reset the endpoints' state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 *
1195 * Enables all the endpoints for the interface's current altsetting.
1196 */
Alan Stern2caf7fc2008-12-31 11:31:33 -05001197void usb_enable_interface(struct usb_device *dev,
David Vrabel3444b262009-04-08 17:36:28 +00001198 struct usb_interface *intf, bool reset_eps)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
1200 struct usb_host_interface *alt = intf->cur_altsetting;
1201 int i;
1202
1203 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
David Vrabel3444b262009-04-08 17:36:28 +00001204 usb_enable_endpoint(dev, &alt->endpoint[i], reset_eps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
1207/**
1208 * usb_set_interface - Makes a particular alternate setting be current
1209 * @dev: the device whose interface is being updated
1210 * @interface: the interface being updated
1211 * @alternate: the setting being chosen.
1212 * Context: !in_interrupt ()
1213 *
1214 * This is used to enable data transfers on interfaces that may not
1215 * be enabled by default. Not all devices support such configurability.
1216 * Only the driver bound to an interface may change its setting.
1217 *
1218 * Within any given configuration, each interface may have several
1219 * alternative settings. These are often used to control levels of
1220 * bandwidth consumption. For example, the default setting for a high
1221 * speed interrupt endpoint may not send more than 64 bytes per microframe,
1222 * while interrupt transfers of up to 3KBytes per microframe are legal.
1223 * Also, isochronous endpoints may never be part of an
1224 * interface's default setting. To access such bandwidth, alternate
1225 * interface settings must be made current.
1226 *
1227 * Note that in the Linux USB subsystem, bandwidth associated with
1228 * an endpoint in a given alternate setting is not reserved until an URB
1229 * is submitted that needs that bandwidth. Some other operating systems
1230 * allocate bandwidth early, when a configuration is chosen.
1231 *
1232 * This call is synchronous, and may not be used in an interrupt context.
1233 * Also, drivers must not change altsettings while urbs are scheduled for
1234 * endpoints in that interface; all such urbs must first be completed
1235 * (perhaps forced by unlinking).
1236 *
1237 * Returns zero on success, or else the status code returned by the
1238 * underlying usb_control_msg() call.
1239 */
1240int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1241{
1242 struct usb_interface *iface;
1243 struct usb_host_interface *alt;
1244 int ret;
1245 int manual = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001246 unsigned int epaddr;
1247 unsigned int pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 if (dev->state == USB_STATE_SUSPENDED)
1250 return -EHOSTUNREACH;
1251
1252 iface = usb_ifnum_to_if(dev, interface);
1253 if (!iface) {
1254 dev_dbg(&dev->dev, "selecting invalid interface %d\n",
1255 interface);
1256 return -EINVAL;
1257 }
1258
1259 alt = usb_altnum_to_altsetting(iface, alternate);
1260 if (!alt) {
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -07001261 dev_warn(&dev->dev, "selecting invalid altsetting %d",
1262 alternate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return -EINVAL;
1264 }
1265
Alan Stern392e1d92008-03-11 10:20:12 -04001266 if (dev->quirks & USB_QUIRK_NO_SET_INTF)
1267 ret = -EPIPE;
1268 else
1269 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1271 alternate, interface, NULL, 0, 5000);
1272
1273 /* 9.4.10 says devices don't need this and are free to STALL the
1274 * request if the interface only has one alternate setting.
1275 */
1276 if (ret == -EPIPE && iface->num_altsetting == 1) {
1277 dev_dbg(&dev->dev,
1278 "manual set_interface for iface %d, alt %d\n",
1279 interface, alternate);
1280 manual = 1;
1281 } else if (ret < 0)
1282 return ret;
1283
1284 /* FIXME drivers shouldn't need to replicate/bugfix the logic here
1285 * when they implement async or easily-killable versions of this or
1286 * other "should-be-internal" functions (like clear_halt).
1287 * should hcd+usbcore postprocess control requests?
1288 */
1289
1290 /* prevent submissions using previous endpoint settings */
Alan Stern3b23dd62008-12-05 14:10:34 -05001291 if (iface->cur_altsetting != alt) {
1292 remove_intf_ep_devs(iface);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001293 usb_remove_sysfs_intf_files(iface);
Alan Stern3b23dd62008-12-05 14:10:34 -05001294 }
Alan Sternddeac4e2009-01-15 17:03:33 -05001295 usb_disable_interface(dev, iface, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 iface->cur_altsetting = alt;
1298
1299 /* If the interface only has one altsetting and the device didn't
David Brownella81e7ec2005-04-18 17:39:25 -07001300 * accept the request, we attempt to carry out the equivalent action
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 * by manually clearing the HALT feature for each endpoint in the
1302 * new altsetting.
1303 */
1304 if (manual) {
1305 int i;
1306
1307 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001308 epaddr = alt->endpoint[i].desc.bEndpointAddress;
1309 pipe = __create_pipe(dev,
1310 USB_ENDPOINT_NUMBER_MASK & epaddr) |
1311 (usb_endpoint_out(epaddr) ?
1312 USB_DIR_OUT : USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 usb_clear_halt(dev, pipe);
1315 }
1316 }
1317
1318 /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
1319 *
1320 * Note:
1321 * Despite EP0 is always present in all interfaces/AS, the list of
1322 * endpoints from the descriptor does not contain EP0. Due to its
1323 * omnipresence one might expect EP0 being considered "affected" by
1324 * any SetInterface request and hence assume toggles need to be reset.
1325 * However, EP0 toggles are re-synced for every individual transfer
1326 * during the SETUP stage - hence EP0 toggles are "don't care" here.
1327 * (Likewise, EP0 never "halts" on well designed devices.)
1328 */
Alan Stern2caf7fc2008-12-31 11:31:33 -05001329 usb_enable_interface(dev, iface, true);
Alan Stern3b23dd62008-12-05 14:10:34 -05001330 if (device_is_registered(&iface->dev)) {
Alan Stern0e6c8e82005-10-24 15:33:03 -04001331 usb_create_sysfs_intf_files(iface);
Alan Stern3b23dd62008-12-05 14:10:34 -05001332 create_intf_ep_devs(iface);
1333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return 0;
1335}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001336EXPORT_SYMBOL_GPL(usb_set_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338/**
1339 * usb_reset_configuration - lightweight device reset
1340 * @dev: the device whose configuration is being reset
1341 *
1342 * This issues a standard SET_CONFIGURATION request to the device using
1343 * the current configuration. The effect is to reset most USB-related
1344 * state in the device, including interface altsettings (reset to zero),
David Vrabel3444b262009-04-08 17:36:28 +00001345 * endpoint halts (cleared), and endpoint state (only for bulk and interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 * endpoints). Other usbcore state is unchanged, including bindings of
1347 * usb device drivers to interfaces.
1348 *
1349 * Because this affects multiple interfaces, avoid using this with composite
1350 * (multi-interface) devices. Instead, the driver for each interface may
David Brownella81e7ec2005-04-18 17:39:25 -07001351 * use usb_set_interface() on the interfaces it claims. Be careful though;
1352 * some devices don't support the SET_INTERFACE request, and others won't
David Vrabel3444b262009-04-08 17:36:28 +00001353 * reset all the interface state (notably endpoint state). Resetting the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 * configuration would affect other drivers' interfaces.
1355 *
1356 * The caller must own the device lock.
1357 *
1358 * Returns zero on success, else a negative error code.
1359 */
1360int usb_reset_configuration(struct usb_device *dev)
1361{
1362 int i, retval;
1363 struct usb_host_config *config;
1364
1365 if (dev->state == USB_STATE_SUSPENDED)
1366 return -EHOSTUNREACH;
1367
1368 /* caller must have locked the device and must own
1369 * the usb bus readlock (so driver bindings are stable);
1370 * calls during probe() are fine
1371 */
1372
1373 for (i = 1; i < 16; ++i) {
Alan Sternddeac4e2009-01-15 17:03:33 -05001374 usb_disable_endpoint(dev, i, true);
1375 usb_disable_endpoint(dev, i + USB_DIR_IN, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377
1378 config = dev->actconfig;
1379 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1380 USB_REQ_SET_CONFIGURATION, 0,
1381 config->desc.bConfigurationValue, 0,
1382 NULL, 0, USB_CTRL_SET_TIMEOUT);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001383 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /* re-init hc/hcd interface/endpoint state */
1387 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1388 struct usb_interface *intf = config->interface[i];
1389 struct usb_host_interface *alt;
1390
1391 alt = usb_altnum_to_altsetting(intf, 0);
1392
1393 /* No altsetting 0? We'll assume the first altsetting.
1394 * We could use a GetInterface call, but if a device is
1395 * so non-compliant that it doesn't have altsetting 0
1396 * then I wouldn't trust its reply anyway.
1397 */
1398 if (!alt)
1399 alt = &intf->altsetting[0];
1400
Alan Stern3b23dd62008-12-05 14:10:34 -05001401 if (alt != intf->cur_altsetting) {
1402 remove_intf_ep_devs(intf);
1403 usb_remove_sysfs_intf_files(intf);
1404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 intf->cur_altsetting = alt;
Alan Stern2caf7fc2008-12-31 11:31:33 -05001406 usb_enable_interface(dev, intf, true);
Alan Stern3b23dd62008-12-05 14:10:34 -05001407 if (device_is_registered(&intf->dev)) {
Alan Stern0e6c8e82005-10-24 15:33:03 -04001408 usb_create_sysfs_intf_files(intf);
Alan Stern3b23dd62008-12-05 14:10:34 -05001409 create_intf_ep_devs(intf);
1410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412 return 0;
1413}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001414EXPORT_SYMBOL_GPL(usb_reset_configuration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Greg Kroah-Hartmanb0e396e2007-08-02 22:44:27 -06001416static void usb_release_interface(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
1418 struct usb_interface *intf = to_usb_interface(dev);
1419 struct usb_interface_cache *intfc =
1420 altsetting_to_usb_interface_cache(intf->altsetting);
1421
1422 kref_put(&intfc->ref, usb_release_interface_cache);
1423 kfree(intf);
1424}
1425
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001426#ifdef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +02001427static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001428{
1429 struct usb_device *usb_dev;
1430 struct usb_interface *intf;
1431 struct usb_host_interface *alt;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001432
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001433 intf = to_usb_interface(dev);
1434 usb_dev = interface_to_usbdev(intf);
1435 alt = intf->cur_altsetting;
1436
Kay Sievers7eff2e72007-08-14 15:15:12 +02001437 if (add_uevent_var(env, "INTERFACE=%d/%d/%d",
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001438 alt->desc.bInterfaceClass,
1439 alt->desc.bInterfaceSubClass,
1440 alt->desc.bInterfaceProtocol))
1441 return -ENOMEM;
1442
Kay Sievers7eff2e72007-08-14 15:15:12 +02001443 if (add_uevent_var(env,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001444 "MODALIAS=usb:"
1445 "v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001446 le16_to_cpu(usb_dev->descriptor.idVendor),
1447 le16_to_cpu(usb_dev->descriptor.idProduct),
1448 le16_to_cpu(usb_dev->descriptor.bcdDevice),
1449 usb_dev->descriptor.bDeviceClass,
1450 usb_dev->descriptor.bDeviceSubClass,
1451 usb_dev->descriptor.bDeviceProtocol,
1452 alt->desc.bInterfaceClass,
1453 alt->desc.bInterfaceSubClass,
1454 alt->desc.bInterfaceProtocol))
1455 return -ENOMEM;
1456
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001457 return 0;
1458}
1459
1460#else
1461
Kay Sievers7eff2e72007-08-14 15:15:12 +02001462static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001463{
1464 return -ENODEV;
1465}
1466#endif /* CONFIG_HOTPLUG */
1467
1468struct device_type usb_if_device_type = {
1469 .name = "usb_interface",
1470 .release = usb_release_interface,
1471 .uevent = usb_if_uevent,
1472};
1473
Craig W. Nadler165fe972007-06-15 23:14:35 -04001474static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001475 struct usb_host_config *config,
1476 u8 inum)
Craig W. Nadler165fe972007-06-15 23:14:35 -04001477{
1478 struct usb_interface_assoc_descriptor *retval = NULL;
1479 struct usb_interface_assoc_descriptor *intf_assoc;
1480 int first_intf;
1481 int last_intf;
1482 int i;
1483
1484 for (i = 0; (i < USB_MAXIADS && config->intf_assoc[i]); i++) {
1485 intf_assoc = config->intf_assoc[i];
1486 if (intf_assoc->bInterfaceCount == 0)
1487 continue;
1488
1489 first_intf = intf_assoc->bFirstInterface;
1490 last_intf = first_intf + (intf_assoc->bInterfaceCount - 1);
1491 if (inum >= first_intf && inum <= last_intf) {
1492 if (!retval)
1493 retval = intf_assoc;
1494 else
1495 dev_err(&dev->dev, "Interface #%d referenced"
1496 " by multiple IADs\n", inum);
1497 }
1498 }
1499
1500 return retval;
1501}
1502
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001503
1504/*
1505 * Internal function to queue a device reset
1506 *
1507 * This is initialized into the workstruct in 'struct
1508 * usb_device->reset_ws' that is launched by
1509 * message.c:usb_set_configuration() when initializing each 'struct
1510 * usb_interface'.
1511 *
1512 * It is safe to get the USB device without reference counts because
1513 * the life cycle of @iface is bound to the life cycle of @udev. Then,
1514 * this function will be ran only if @iface is alive (and before
1515 * freeing it any scheduled instances of it will have been cancelled).
1516 *
1517 * We need to set a flag (usb_dev->reset_running) because when we call
1518 * the reset, the interfaces might be unbound. The current interface
1519 * cannot try to remove the queued work as it would cause a deadlock
1520 * (you cannot remove your work from within your executing
1521 * workqueue). This flag lets it know, so that
1522 * usb_cancel_queued_reset() doesn't try to do it.
1523 *
1524 * See usb_queue_reset_device() for more details
1525 */
1526void __usb_queue_reset_device(struct work_struct *ws)
1527{
1528 int rc;
1529 struct usb_interface *iface =
1530 container_of(ws, struct usb_interface, reset_ws);
1531 struct usb_device *udev = interface_to_usbdev(iface);
1532
1533 rc = usb_lock_device_for_reset(udev, iface);
1534 if (rc >= 0) {
1535 iface->reset_running = 1;
1536 usb_reset_device(udev);
1537 iface->reset_running = 0;
1538 usb_unlock_device(udev);
1539 }
1540}
1541
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543/*
1544 * usb_set_configuration - Makes a particular device setting be current
1545 * @dev: the device whose configuration is being updated
1546 * @configuration: the configuration being chosen.
1547 * Context: !in_interrupt(), caller owns the device lock
1548 *
1549 * This is used to enable non-default device modes. Not all devices
1550 * use this kind of configurability; many devices only have one
1551 * configuration.
1552 *
Alan Stern3f141e22007-02-08 16:40:43 -05001553 * @configuration is the value of the configuration to be installed.
1554 * According to the USB spec (e.g. section 9.1.1.5), configuration values
1555 * must be non-zero; a value of zero indicates that the device in
1556 * unconfigured. However some devices erroneously use 0 as one of their
1557 * configuration values. To help manage such devices, this routine will
1558 * accept @configuration = -1 as indicating the device should be put in
1559 * an unconfigured state.
1560 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 * USB device configurations may affect Linux interoperability,
1562 * power consumption and the functionality available. For example,
1563 * the default configuration is limited to using 100mA of bus power,
1564 * so that when certain device functionality requires more power,
1565 * and the device is bus powered, that functionality should be in some
1566 * non-default device configuration. Other device modes may also be
1567 * reflected as configuration options, such as whether two ISDN
1568 * channels are available independently; and choosing between open
1569 * standard device protocols (like CDC) or proprietary ones.
1570 *
Inaky Perez-Gonzalez16bbab22007-07-31 20:34:01 -07001571 * Note that a non-authorized device (dev->authorized == 0) will only
1572 * be put in unconfigured mode.
1573 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 * Note that USB has an additional level of device configurability,
1575 * associated with interfaces. That configurability is accessed using
1576 * usb_set_interface().
1577 *
1578 * This call is synchronous. The calling context must be able to sleep,
1579 * must own the device lock, and must not hold the driver model's USB
Ming Lei6d243e52008-06-17 23:24:08 +08001580 * bus mutex; usb interface driver probe() methods cannot use this routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 *
1582 * Returns zero on success, or else the status code returned by the
Steven Cole093cf722005-05-03 19:07:24 -06001583 * underlying call that failed. On successful completion, each interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 * in the original device configuration has been destroyed, and each one
1585 * in the new configuration has been probed by all relevant usb device
1586 * drivers currently known to the kernel.
1587 */
1588int usb_set_configuration(struct usb_device *dev, int configuration)
1589{
1590 int i, ret;
1591 struct usb_host_config *cp = NULL;
1592 struct usb_interface **new_interfaces = NULL;
1593 int n, nintf;
1594
Inaky Perez-Gonzalez16bbab22007-07-31 20:34:01 -07001595 if (dev->authorized == 0 || configuration == -1)
Alan Stern3f141e22007-02-08 16:40:43 -05001596 configuration = 0;
1597 else {
1598 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1599 if (dev->config[i].desc.bConfigurationValue ==
1600 configuration) {
1601 cp = &dev->config[i];
1602 break;
1603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
1605 }
1606 if ((!cp && configuration != 0))
1607 return -EINVAL;
1608
1609 /* The USB spec says configuration 0 means unconfigured.
1610 * But if a device includes a configuration numbered 0,
1611 * we will accept it as a correctly configured state.
Alan Stern3f141e22007-02-08 16:40:43 -05001612 * Use -1 if you really want to unconfigure the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 */
1614 if (cp && configuration == 0)
1615 dev_warn(&dev->dev, "config 0 descriptor??\n");
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 /* Allocate memory for new interfaces before doing anything else,
1618 * so that if we run out then nothing will have changed. */
1619 n = nintf = 0;
1620 if (cp) {
1621 nintf = cp->desc.bNumInterfaces;
1622 new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
1623 GFP_KERNEL);
1624 if (!new_interfaces) {
Joe Perches898eb712007-10-18 03:06:30 -07001625 dev_err(&dev->dev, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return -ENOMEM;
1627 }
1628
1629 for (; n < nintf; ++n) {
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001630 new_interfaces[n] = kzalloc(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 sizeof(struct usb_interface),
1632 GFP_KERNEL);
1633 if (!new_interfaces[n]) {
Joe Perches898eb712007-10-18 03:06:30 -07001634 dev_err(&dev->dev, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 ret = -ENOMEM;
1636free_interfaces:
1637 while (--n >= 0)
1638 kfree(new_interfaces[n]);
1639 kfree(new_interfaces);
1640 return ret;
1641 }
1642 }
Alan Stern6ad07122006-06-01 13:59:16 -04001643
1644 i = dev->bus_mA - cp->desc.bMaxPower * 2;
1645 if (i < 0)
1646 dev_warn(&dev->dev, "new config #%d exceeds power "
1647 "limit by %dmA\n",
1648 configuration, -i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
1650
Alan Stern01d883d2006-08-30 15:47:18 -04001651 /* Wake up the device so we can send it the Set-Config request */
Alan Stern94fcda12006-11-20 11:38:46 -05001652 ret = usb_autoresume_device(dev);
Alan Stern01d883d2006-08-30 15:47:18 -04001653 if (ret)
1654 goto free_interfaces;
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /* if it's already configured, clear out old state first.
1657 * getting rid of old interfaces means unbinding their drivers.
1658 */
1659 if (dev->state != USB_STATE_ADDRESS)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001660 usb_disable_device(dev, 1); /* Skip ep0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Alan Sterndf718962008-12-19 10:27:56 -05001662 /* Get rid of pending async Set-Config requests for this device */
1663 cancel_async_set_config(dev);
1664
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001665 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1666 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
1667 NULL, 0, USB_CTRL_SET_TIMEOUT);
1668 if (ret < 0) {
Alan Stern6ad07122006-06-01 13:59:16 -04001669 /* All the old state is gone, so what else can we do?
1670 * The device is probably useless now anyway.
1671 */
1672 cp = NULL;
1673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 dev->actconfig = cp;
Alan Stern6ad07122006-06-01 13:59:16 -04001676 if (!cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 usb_set_device_state(dev, USB_STATE_ADDRESS);
Alan Stern94fcda12006-11-20 11:38:46 -05001678 usb_autosuspend_device(dev);
Alan Stern6ad07122006-06-01 13:59:16 -04001679 goto free_interfaces;
1680 }
1681 usb_set_device_state(dev, USB_STATE_CONFIGURED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Alan Stern6ad07122006-06-01 13:59:16 -04001683 /* Initialize the new interface structures and the
1684 * hc/hcd/usbcore interface/endpoint state.
1685 */
1686 for (i = 0; i < nintf; ++i) {
1687 struct usb_interface_cache *intfc;
1688 struct usb_interface *intf;
1689 struct usb_host_interface *alt;
1690
1691 cp->interface[i] = intf = new_interfaces[i];
1692 intfc = cp->intf_cache[i];
1693 intf->altsetting = intfc->altsetting;
1694 intf->num_altsetting = intfc->num_altsetting;
Craig W. Nadler165fe972007-06-15 23:14:35 -04001695 intf->intf_assoc = find_iad(dev, cp, i);
Alan Stern6ad07122006-06-01 13:59:16 -04001696 kref_get(&intfc->ref);
1697
1698 alt = usb_altnum_to_altsetting(intf, 0);
1699
1700 /* No altsetting 0? We'll assume the first altsetting.
1701 * We could use a GetInterface call, but if a device is
1702 * so non-compliant that it doesn't have altsetting 0
1703 * then I wouldn't trust its reply anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 */
Alan Stern6ad07122006-06-01 13:59:16 -04001705 if (!alt)
1706 alt = &intf->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Alan Stern6ad07122006-06-01 13:59:16 -04001708 intf->cur_altsetting = alt;
Alan Stern2caf7fc2008-12-31 11:31:33 -05001709 usb_enable_interface(dev, intf, true);
Alan Stern6ad07122006-06-01 13:59:16 -04001710 intf->dev.parent = &dev->dev;
1711 intf->dev.driver = NULL;
1712 intf->dev.bus = &usb_bus_type;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001713 intf->dev.type = &usb_if_device_type;
Alan Stern2e5f10e2008-04-30 15:37:19 -04001714 intf->dev.groups = usb_interface_groups;
Alan Stern6ad07122006-06-01 13:59:16 -04001715 intf->dev.dma_mask = dev->dev.dma_mask;
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001716 INIT_WORK(&intf->reset_ws, __usb_queue_reset_device);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001717 device_initialize(&intf->dev);
Alan Stern6ad07122006-06-01 13:59:16 -04001718 mark_quiesced(intf);
Kay Sievers0031a062008-05-02 06:02:41 +02001719 dev_set_name(&intf->dev, "%d-%s:%d.%d",
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001720 dev->bus->busnum, dev->devpath,
1721 configuration, alt->desc.bInterfaceNumber);
Alan Stern6ad07122006-06-01 13:59:16 -04001722 }
1723 kfree(new_interfaces);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Alan Stern1662e3a2009-03-18 14:28:53 -04001725 if (cp->string == NULL &&
1726 !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
Alan Stern6ad07122006-06-01 13:59:16 -04001727 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Alan Stern6ad07122006-06-01 13:59:16 -04001729 /* Now that all the interfaces are set up, register them
1730 * to trigger binding of drivers to interfaces. probe()
1731 * routines may install different altsettings and may
1732 * claim() any interfaces not yet bound. Many class drivers
1733 * need that: CDC, audio, video, etc.
1734 */
1735 for (i = 0; i < nintf; ++i) {
1736 struct usb_interface *intf = cp->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001738 dev_dbg(&dev->dev,
Alan Stern6ad07122006-06-01 13:59:16 -04001739 "adding %s (config #%d, interface %d)\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001740 dev_name(&intf->dev), configuration,
Alan Stern6ad07122006-06-01 13:59:16 -04001741 intf->cur_altsetting->desc.bInterfaceNumber);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001742 ret = device_add(&intf->dev);
Alan Stern6ad07122006-06-01 13:59:16 -04001743 if (ret != 0) {
1744 dev_err(&dev->dev, "device_add(%s) --> %d\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001745 dev_name(&intf->dev), ret);
Alan Stern6ad07122006-06-01 13:59:16 -04001746 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
Alan Stern3b23dd62008-12-05 14:10:34 -05001748 create_intf_ep_devs(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
1750
Alan Stern94fcda12006-11-20 11:38:46 -05001751 usb_autosuspend_device(dev);
Alan Stern86d30742005-07-29 12:17:16 -07001752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
Alan Sterndf718962008-12-19 10:27:56 -05001755static LIST_HEAD(set_config_list);
1756static DEFINE_SPINLOCK(set_config_lock);
1757
Alan Stern088dc272006-08-21 12:08:19 -04001758struct set_config_request {
1759 struct usb_device *udev;
1760 int config;
1761 struct work_struct work;
Alan Sterndf718962008-12-19 10:27:56 -05001762 struct list_head node;
Alan Stern088dc272006-08-21 12:08:19 -04001763};
1764
1765/* Worker routine for usb_driver_set_configuration() */
David Howellsc4028952006-11-22 14:57:56 +00001766static void driver_set_config_work(struct work_struct *work)
Alan Stern088dc272006-08-21 12:08:19 -04001767{
David Howellsc4028952006-11-22 14:57:56 +00001768 struct set_config_request *req =
1769 container_of(work, struct set_config_request, work);
Alan Sterndf718962008-12-19 10:27:56 -05001770 struct usb_device *udev = req->udev;
Alan Stern088dc272006-08-21 12:08:19 -04001771
Alan Sterndf718962008-12-19 10:27:56 -05001772 usb_lock_device(udev);
1773 spin_lock(&set_config_lock);
1774 list_del(&req->node);
1775 spin_unlock(&set_config_lock);
1776
1777 if (req->config >= -1) /* Is req still valid? */
1778 usb_set_configuration(udev, req->config);
1779 usb_unlock_device(udev);
1780 usb_put_dev(udev);
Alan Stern088dc272006-08-21 12:08:19 -04001781 kfree(req);
1782}
1783
Alan Sterndf718962008-12-19 10:27:56 -05001784/* Cancel pending Set-Config requests for a device whose configuration
1785 * was just changed
1786 */
1787static void cancel_async_set_config(struct usb_device *udev)
1788{
1789 struct set_config_request *req;
1790
1791 spin_lock(&set_config_lock);
1792 list_for_each_entry(req, &set_config_list, node) {
1793 if (req->udev == udev)
1794 req->config = -999; /* Mark as cancelled */
1795 }
1796 spin_unlock(&set_config_lock);
1797}
1798
Alan Stern088dc272006-08-21 12:08:19 -04001799/**
1800 * usb_driver_set_configuration - Provide a way for drivers to change device configurations
1801 * @udev: the device whose configuration is being updated
1802 * @config: the configuration being chosen.
1803 * Context: In process context, must be able to sleep
1804 *
1805 * Device interface drivers are not allowed to change device configurations.
1806 * This is because changing configurations will destroy the interface the
1807 * driver is bound to and create new ones; it would be like a floppy-disk
1808 * driver telling the computer to replace the floppy-disk drive with a
1809 * tape drive!
1810 *
1811 * Still, in certain specialized circumstances the need may arise. This
1812 * routine gets around the normal restrictions by using a work thread to
1813 * submit the change-config request.
1814 *
1815 * Returns 0 if the request was succesfully queued, error code otherwise.
1816 * The caller has no way to know whether the queued request will eventually
1817 * succeed.
1818 */
1819int usb_driver_set_configuration(struct usb_device *udev, int config)
1820{
1821 struct set_config_request *req;
1822
1823 req = kmalloc(sizeof(*req), GFP_KERNEL);
1824 if (!req)
1825 return -ENOMEM;
1826 req->udev = udev;
1827 req->config = config;
David Howellsc4028952006-11-22 14:57:56 +00001828 INIT_WORK(&req->work, driver_set_config_work);
Alan Stern088dc272006-08-21 12:08:19 -04001829
Alan Sterndf718962008-12-19 10:27:56 -05001830 spin_lock(&set_config_lock);
1831 list_add(&req->node, &set_config_list);
1832 spin_unlock(&set_config_lock);
1833
Alan Stern088dc272006-08-21 12:08:19 -04001834 usb_get_dev(udev);
Alan Stern1737bf2c2006-12-15 16:04:52 -05001835 schedule_work(&req->work);
Alan Stern088dc272006-08-21 12:08:19 -04001836 return 0;
1837}
1838EXPORT_SYMBOL_GPL(usb_driver_set_configuration);