blob: 8d874cad6581d6e3c90d2e5cf0be5a75bc6ff0f9 [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;
Sarah Sharpe04748e2009-04-27 19:59:01 -0700368 int use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 if (!io || !dev || !sg
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800371 || usb_pipecontrol(pipe)
372 || usb_pipeisoc(pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 || nents <= 0)
374 return -EINVAL;
375
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800376 spin_lock_init(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 io->dev = dev;
378 io->pipe = pipe;
379 io->sg = sg;
380 io->nents = nents;
381
382 /* not all host controllers use DMA (like the mainstream pci ones);
383 * they can use PIO (sl811) or be software over another transport.
384 */
385 dma = (dev->dev.dma_mask != NULL);
386 if (dma)
Alan Stern5e60a162007-07-30 17:07:21 -0400387 io->entries = usb_buffer_map_sg(dev, usb_pipein(pipe),
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800388 sg, nents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 else
390 io->entries = nents;
391
392 /* initialize all the urbs we'll use */
393 if (io->entries <= 0)
394 return io->entries;
395
David Vrabel4c1bd3d2009-08-24 14:44:30 +0100396 if (dev->bus->sg_tablesize > 0) {
Sarah Sharpe04748e2009-04-27 19:59:01 -0700397 io->urbs = kmalloc(sizeof *io->urbs, mem_flags);
398 use_sg = true;
399 } else {
400 io->urbs = kmalloc(io->entries * sizeof *io->urbs, mem_flags);
401 use_sg = false;
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (!io->urbs)
404 goto nomem;
405
Yoshihiro Shimodae2722522008-04-21 13:48:22 +0900406 urb_flags = URB_NO_INTERRUPT;
407 if (dma)
408 urb_flags |= URB_NO_TRANSFER_DMA_MAP;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800409 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 urb_flags |= URB_SHORT_NOT_OK;
411
Sarah Sharpe04748e2009-04-27 19:59:01 -0700412 if (use_sg) {
413 io->urbs[0] = usb_alloc_urb(0, mem_flags);
414 if (!io->urbs[0]) {
415 io->entries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 goto nomem;
417 }
418
Sarah Sharpe04748e2009-04-27 19:59:01 -0700419 io->urbs[0]->dev = NULL;
420 io->urbs[0]->pipe = pipe;
421 io->urbs[0]->interval = period;
422 io->urbs[0]->transfer_flags = urb_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Sarah Sharpe04748e2009-04-27 19:59:01 -0700424 io->urbs[0]->complete = sg_complete;
425 io->urbs[0]->context = io;
426 /* A length of zero means transfer the whole sg list */
427 io->urbs[0]->transfer_buffer_length = length;
428 if (length == 0) {
429 for_each_sg(sg, sg, io->entries, i) {
430 io->urbs[0]->transfer_buffer_length +=
431 sg_dma_len(sg);
432 }
433 }
434 io->urbs[0]->sg = io;
435 io->urbs[0]->num_sgs = io->entries;
436 io->entries = 1;
437 } else {
438 for_each_sg(sg, sg, io->entries, i) {
439 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Sarah Sharpe04748e2009-04-27 19:59:01 -0700441 io->urbs[i] = usb_alloc_urb(0, mem_flags);
442 if (!io->urbs[i]) {
443 io->entries = i;
444 goto nomem;
445 }
446
447 io->urbs[i]->dev = NULL;
448 io->urbs[i]->pipe = pipe;
449 io->urbs[i]->interval = period;
450 io->urbs[i]->transfer_flags = urb_flags;
451
452 io->urbs[i]->complete = sg_complete;
453 io->urbs[i]->context = io;
454
455 /*
Pete Zaitcev81bf46f2009-06-11 08:40:39 -0600456 * Some systems need to revert to PIO when DMA is temporarily
457 * unavailable. For their sakes, both transfer_buffer and
458 * transfer_dma are set when possible.
Sarah Sharpe04748e2009-04-27 19:59:01 -0700459 *
Pete Zaitcev81bf46f2009-06-11 08:40:39 -0600460 * Note that if IOMMU coalescing occurred, we cannot
461 * trust sg_page anymore, so check if S/G list shrunk.
Sarah Sharpe04748e2009-04-27 19:59:01 -0700462 */
Pete Zaitcev81bf46f2009-06-11 08:40:39 -0600463 if (io->nents == io->entries && !PageHighMem(sg_page(sg)))
464 io->urbs[i]->transfer_buffer = sg_virt(sg);
465 else
466 io->urbs[i]->transfer_buffer = NULL;
467
Sarah Sharpe04748e2009-04-27 19:59:01 -0700468 if (dma) {
469 io->urbs[i]->transfer_dma = sg_dma_address(sg);
470 len = sg_dma_len(sg);
Sarah Sharpe04748e2009-04-27 19:59:01 -0700471 } else {
472 /* hc may use _only_ transfer_buffer */
Sarah Sharpe04748e2009-04-27 19:59:01 -0700473 len = sg->length;
474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Sarah Sharpe04748e2009-04-27 19:59:01 -0700476 if (length) {
477 len = min_t(unsigned, len, length);
478 length -= len;
479 if (length == 0)
480 io->entries = i + 1;
481 }
482 io->urbs[i]->transfer_buffer_length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Sarah Sharpe04748e2009-04-27 19:59:01 -0700484 io->urbs[--i]->transfer_flags &= ~URB_NO_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /* transaction state */
Alan Stern580da342008-08-05 13:05:17 -0400488 io->count = io->entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 io->status = 0;
490 io->bytes = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800491 init_completion(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return 0;
493
494nomem:
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800495 sg_clean(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return -ENOMEM;
497}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600498EXPORT_SYMBOL_GPL(usb_sg_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500/**
501 * usb_sg_wait - synchronously execute scatter/gather request
502 * @io: request block handle, as initialized with usb_sg_init().
503 * some fields become accessible when this call returns.
504 * Context: !in_interrupt ()
505 *
506 * This function blocks until the specified I/O operation completes. It
507 * leverages the grouping of the related I/O requests to get good transfer
508 * rates, by queueing the requests. At higher speeds, such queuing can
509 * significantly improve USB throughput.
510 *
511 * There are three kinds of completion for this function.
512 * (1) success, where io->status is zero. The number of io->bytes
513 * transferred is as requested.
514 * (2) error, where io->status is a negative errno value. The number
515 * of io->bytes transferred before the error is usually less
516 * than requested, and can be nonzero.
Steven Cole093cf722005-05-03 19:07:24 -0600517 * (3) cancellation, a type of error with status -ECONNRESET that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 * is initiated by usb_sg_cancel().
519 *
520 * When this function returns, all memory allocated through usb_sg_init() or
521 * this call will have been freed. The request block parameter may still be
522 * passed to usb_sg_cancel(), or it may be freed. It could also be
523 * reinitialized and then reused.
524 *
525 * Data Transfer Rates:
526 *
527 * Bulk transfers are valid for full or high speed endpoints.
528 * The best full speed data rate is 19 packets of 64 bytes each
529 * per frame, or 1216 bytes per millisecond.
530 * The best high speed data rate is 13 packets of 512 bytes each
531 * per microframe, or 52 KBytes per millisecond.
532 *
533 * The reason to use interrupt transfers through this API would most likely
534 * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
535 * could be transferred. That capability is less useful for low or full
536 * speed interrupt endpoints, which allow at most one packet per millisecond,
537 * of at most 8 or 64 bytes (respectively).
Sarah Sharp79abb1a2009-04-27 19:58:26 -0700538 *
539 * It is not necessary to call this function to reserve bandwidth for devices
540 * under an xHCI host controller, as the bandwidth is reserved when the
541 * configuration or interface alt setting is selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800543void usb_sg_wait(struct usb_sg_request *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800545 int i;
546 int entries = io->entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /* queue the urbs. */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800549 spin_lock_irq(&io->lock);
Alan Stern8ccef0d2007-06-21 16:26:46 -0400550 i = 0;
551 while (i < entries && !io->status) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800552 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800554 io->urbs[i]->dev = io->dev;
555 retval = usb_submit_urb(io->urbs [i], GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 /* after we submit, let completions or cancelations fire;
558 * we handshake using io->status.
559 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800560 spin_unlock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 switch (retval) {
562 /* maybe we retrying will recover */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800563 case -ENXIO: /* hc didn't queue this one */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 case -EAGAIN:
565 case -ENOMEM:
566 io->urbs[i]->dev = NULL;
567 retval = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800568 yield();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 break;
570
571 /* no error? continue immediately.
572 *
573 * NOTE: to work better with UHCI (4K I/O buffer may
574 * need 3K of TDs) it may be good to limit how many
575 * URBs are queued at once; N milliseconds?
576 */
577 case 0:
Alan Stern8ccef0d2007-06-21 16:26:46 -0400578 ++i;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800579 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 break;
581
582 /* fail any uncompleted urbs */
583 default:
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800584 io->urbs[i]->dev = NULL;
585 io->urbs[i]->status = retval;
586 dev_dbg(&io->dev->dev, "%s, submit --> %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800587 __func__, retval);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800588 usb_sg_cancel(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800590 spin_lock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (retval && (io->status == 0 || io->status == -ECONNRESET))
592 io->status = retval;
593 }
594 io->count -= entries - i;
595 if (io->count == 0)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800596 complete(&io->complete);
597 spin_unlock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 /* OK, yes, this could be packaged as non-blocking.
600 * So could the submit loop above ... but it's easier to
601 * solve neither problem than to solve both!
602 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800603 wait_for_completion(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800605 sg_clean(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600607EXPORT_SYMBOL_GPL(usb_sg_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609/**
610 * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
611 * @io: request block, initialized with usb_sg_init()
612 *
613 * This stops a request after it has been started by usb_sg_wait().
614 * It can also prevents one initialized by usb_sg_init() from starting,
615 * so that call just frees resources allocated to the request.
616 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800617void usb_sg_cancel(struct usb_sg_request *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800619 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800621 spin_lock_irqsave(&io->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /* shut everything down, if it didn't already */
624 if (!io->status) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800625 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 io->status = -ECONNRESET;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800628 spin_unlock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 for (i = 0; i < io->entries; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800630 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 if (!io->urbs [i]->dev)
633 continue;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800634 retval = usb_unlink_urb(io->urbs [i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (retval != -EINPROGRESS && retval != -EBUSY)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800636 dev_warn(&io->dev->dev, "%s, unlink --> %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800637 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800639 spin_lock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800641 spin_unlock_irqrestore(&io->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600643EXPORT_SYMBOL_GPL(usb_sg_cancel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645/*-------------------------------------------------------------------*/
646
647/**
648 * usb_get_descriptor - issues a generic GET_DESCRIPTOR request
649 * @dev: the device whose descriptor is being retrieved
650 * @type: the descriptor type (USB_DT_*)
651 * @index: the number of the descriptor
652 * @buf: where to put the descriptor
653 * @size: how big is "buf"?
654 * Context: !in_interrupt ()
655 *
656 * Gets a USB descriptor. Convenience functions exist to simplify
657 * getting some types of descriptors. Use
658 * usb_get_string() or usb_string() for USB_DT_STRING.
659 * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG)
660 * are part of the device structure.
661 * In addition to a number of USB-standard descriptors, some
662 * devices also use class-specific or vendor-specific descriptors.
663 *
664 * This call is synchronous, and may not be used in an interrupt context.
665 *
666 * Returns the number of bytes received on success, or else the status code
667 * returned by the underlying usb_control_msg() call.
668 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800669int usb_get_descriptor(struct usb_device *dev, unsigned char type,
670 unsigned char index, void *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 int i;
673 int result;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800674
675 memset(buf, 0, size); /* Make sure we parse really received data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 for (i = 0; i < 3; ++i) {
Alan Sternc39772d2007-08-20 10:45:28 -0400678 /* retry on length 0 or error; some devices are flakey */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
680 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
681 (type << 8) + index, 0, buf, size,
682 USB_CTRL_GET_TIMEOUT);
Alan Sternc39772d2007-08-20 10:45:28 -0400683 if (result <= 0 && result != -ETIMEDOUT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 continue;
685 if (result > 1 && ((u8 *)buf)[1] != type) {
Alan Stern67f5a4b2009-02-20 16:33:08 -0500686 result = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 continue;
688 }
689 break;
690 }
691 return result;
692}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600693EXPORT_SYMBOL_GPL(usb_get_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695/**
696 * usb_get_string - gets a string descriptor
697 * @dev: the device whose string descriptor is being retrieved
698 * @langid: code for language chosen (from string descriptor zero)
699 * @index: the number of the descriptor
700 * @buf: where to put the string
701 * @size: how big is "buf"?
702 * Context: !in_interrupt ()
703 *
704 * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character,
705 * in little-endian byte order).
706 * The usb_string() function will often be a convenient way to turn
707 * these strings into kernel-printable form.
708 *
709 * Strings may be referenced in device, configuration, interface, or other
710 * descriptors, and could also be used in vendor-specific ways.
711 *
712 * This call is synchronous, and may not be used in an interrupt context.
713 *
714 * Returns the number of bytes received on success, or else the status code
715 * returned by the underlying usb_control_msg() call.
716 */
Adrian Bunke266a122005-11-08 21:05:43 +0100717static int usb_get_string(struct usb_device *dev, unsigned short langid,
718 unsigned char index, void *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 int i;
721 int result;
722
723 for (i = 0; i < 3; ++i) {
724 /* retry on length 0 or stall; some devices are flakey */
725 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
726 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
727 (USB_DT_STRING << 8) + index, langid, buf, size,
728 USB_CTRL_GET_TIMEOUT);
Alan Stern67f5a4b2009-02-20 16:33:08 -0500729 if (result == 0 || result == -EPIPE)
730 continue;
731 if (result > 1 && ((u8 *) buf)[1] != USB_DT_STRING) {
732 result = -ENODATA;
733 continue;
734 }
735 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737 return result;
738}
739
740static void usb_try_string_workarounds(unsigned char *buf, int *length)
741{
742 int newlength, oldlength = *length;
743
744 for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
745 if (!isprint(buf[newlength]) || buf[newlength + 1])
746 break;
747
748 if (newlength > 2) {
749 buf[0] = newlength;
750 *length = newlength;
751 }
752}
753
754static int usb_string_sub(struct usb_device *dev, unsigned int langid,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800755 unsigned int index, unsigned char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 int rc;
758
759 /* Try to read the string descriptor by asking for the maximum
760 * possible number of bytes */
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100761 if (dev->quirks & USB_QUIRK_STRING_FETCH_255)
762 rc = -EIO;
763 else
764 rc = usb_get_string(dev, langid, index, buf, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 /* If that failed try to read the descriptor length, then
767 * ask for just that many bytes */
768 if (rc < 2) {
769 rc = usb_get_string(dev, langid, index, buf, 2);
770 if (rc == 2)
771 rc = usb_get_string(dev, langid, index, buf, buf[0]);
772 }
773
774 if (rc >= 2) {
775 if (!buf[0] && !buf[1])
776 usb_try_string_workarounds(buf, &rc);
777
778 /* There might be extra junk at the end of the descriptor */
779 if (buf[0] < rc)
780 rc = buf[0];
781
782 rc = rc - (rc & 1); /* force a multiple of two */
783 }
784
785 if (rc < 2)
786 rc = (rc < 0 ? rc : -EINVAL);
787
788 return rc;
789}
790
Daniel Mack0cce2ed2009-07-10 11:04:58 +0200791static int usb_get_langid(struct usb_device *dev, unsigned char *tbuf)
792{
793 int err;
794
795 if (dev->have_langid)
796 return 0;
797
798 if (dev->string_langid < 0)
799 return -EPIPE;
800
801 err = usb_string_sub(dev, 0, 0, tbuf);
802
803 /* If the string was reported but is malformed, default to english
804 * (0x0409) */
805 if (err == -ENODATA || (err > 0 && err < 4)) {
806 dev->string_langid = 0x0409;
807 dev->have_langid = 1;
808 dev_err(&dev->dev,
809 "string descriptor 0 malformed (err = %d), "
810 "defaulting to 0x%04x\n",
811 err, dev->string_langid);
812 return 0;
813 }
814
815 /* In case of all other errors, we assume the device is not able to
816 * deal with strings at all. Set string_langid to -1 in order to
817 * prevent any string to be retrieved from the device */
818 if (err < 0) {
819 dev_err(&dev->dev, "string descriptor 0 read error: %d\n",
820 err);
821 dev->string_langid = -1;
822 return -EPIPE;
823 }
824
825 /* always use the first langid listed */
826 dev->string_langid = tbuf[2] | (tbuf[3] << 8);
827 dev->have_langid = 1;
828 dev_dbg(&dev->dev, "default language 0x%04x\n",
829 dev->string_langid);
830 return 0;
831}
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833/**
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200834 * usb_string - returns UTF-8 version of a string descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 * @dev: the device whose string descriptor is being retrieved
836 * @index: the number of the descriptor
837 * @buf: where to put the string
838 * @size: how big is "buf"?
839 * Context: !in_interrupt ()
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800840 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 * This converts the UTF-16LE encoded strings returned by devices, from
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200842 * usb_get_string_descriptor(), to null-terminated UTF-8 encoded ones
843 * that are more usable in most kernel contexts. Note that this function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 * chooses strings in the first language supported by the device.
845 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 * This call is synchronous, and may not be used in an interrupt context.
847 *
848 * Returns length of the string (>= 0) or usb_control_msg status (< 0).
849 */
850int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
851{
852 unsigned char *tbuf;
853 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 if (dev->state == USB_STATE_SUSPENDED)
856 return -EHOSTUNREACH;
857 if (size <= 0 || !buf || !index)
858 return -EINVAL;
859 buf[0] = 0;
Alan Stern74675a52009-04-30 10:08:18 -0400860 tbuf = kmalloc(256, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (!tbuf)
862 return -ENOMEM;
863
Daniel Mack0cce2ed2009-07-10 11:04:58 +0200864 err = usb_get_langid(dev, tbuf);
865 if (err < 0)
866 goto errout;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 err = usb_string_sub(dev, dev->string_langid, index, tbuf);
869 if (err < 0)
870 goto errout;
871
872 size--; /* leave room for trailing NULL char in output buffer */
Alan Stern74675a52009-04-30 10:08:18 -0400873 err = utf16s_to_utf8s((wchar_t *) &tbuf[2], (err - 2) / 2,
874 UTF16_LITTLE_ENDIAN, buf, size);
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200875 buf[err] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 if (tbuf[1] != USB_DT_STRING)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800878 dev_dbg(&dev->dev,
879 "wrong descriptor type %02x for string %d (\"%s\")\n",
880 tbuf[1], index, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 errout:
883 kfree(tbuf);
884 return err;
885}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600886EXPORT_SYMBOL_GPL(usb_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200888/* one UTF-8-encoded 16-bit character has at most three bytes */
889#define MAX_USB_STRING_SIZE (127 * 3 + 1)
890
Alan Stern4f62efe2005-10-24 16:24:14 -0400891/**
892 * usb_cache_string - read a string descriptor and cache it for later use
893 * @udev: the device whose string descriptor is being read
894 * @index: the descriptor index
895 *
896 * Returns a pointer to a kmalloc'ed buffer containing the descriptor string,
897 * or NULL if the index is 0 or the string could not be read.
898 */
899char *usb_cache_string(struct usb_device *udev, int index)
900{
901 char *buf;
902 char *smallbuf = NULL;
903 int len;
904
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800905 if (index <= 0)
906 return NULL;
907
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200908 buf = kmalloc(MAX_USB_STRING_SIZE, GFP_KERNEL);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800909 if (buf) {
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200910 len = usb_string(udev, index, buf, MAX_USB_STRING_SIZE);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800911 if (len > 0) {
912 smallbuf = kmalloc(++len, GFP_KERNEL);
913 if (!smallbuf)
Alan Stern4f62efe2005-10-24 16:24:14 -0400914 return buf;
915 memcpy(smallbuf, buf, len);
916 }
917 kfree(buf);
918 }
919 return smallbuf;
920}
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922/*
923 * usb_get_device_descriptor - (re)reads the device descriptor (usbcore)
924 * @dev: the device whose device descriptor is being updated
925 * @size: how much of the descriptor to read
926 * Context: !in_interrupt ()
927 *
928 * Updates the copy of the device descriptor stored in the device structure,
Laurent Pinchart6ab16a92006-11-07 10:16:25 +0100929 * which dedicates space for this purpose.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 *
931 * Not exported, only for use by the core. If drivers really want to read
932 * the device descriptor directly, they can call usb_get_descriptor() with
933 * type = USB_DT_DEVICE and index = 0.
934 *
935 * This call is synchronous, and may not be used in an interrupt context.
936 *
937 * Returns the number of bytes received on success, or else the status code
938 * returned by the underlying usb_control_msg() call.
939 */
940int usb_get_device_descriptor(struct usb_device *dev, unsigned int size)
941{
942 struct usb_device_descriptor *desc;
943 int ret;
944
945 if (size > sizeof(*desc))
946 return -EINVAL;
947 desc = kmalloc(sizeof(*desc), GFP_NOIO);
948 if (!desc)
949 return -ENOMEM;
950
951 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800952 if (ret >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 memcpy(&dev->descriptor, desc, size);
954 kfree(desc);
955 return ret;
956}
957
958/**
959 * usb_get_status - issues a GET_STATUS call
960 * @dev: the device whose status is being checked
961 * @type: USB_RECIP_*; for device, interface, or endpoint
962 * @target: zero (for device), else interface or endpoint number
963 * @data: pointer to two bytes of bitmap data
964 * Context: !in_interrupt ()
965 *
966 * Returns device, interface, or endpoint status. Normally only of
967 * interest to see if the device is self powered, or has enabled the
968 * remote wakeup facility; or whether a bulk or interrupt endpoint
969 * is halted ("stalled").
970 *
971 * Bits in these status bitmaps are set using the SET_FEATURE request,
972 * and cleared using the CLEAR_FEATURE request. The usb_clear_halt()
973 * function should be used to clear halt ("stall") status.
974 *
975 * This call is synchronous, and may not be used in an interrupt context.
976 *
977 * Returns the number of bytes received on success, or else the status code
978 * returned by the underlying usb_control_msg() call.
979 */
980int usb_get_status(struct usb_device *dev, int type, int target, void *data)
981{
982 int ret;
983 u16 *status = kmalloc(sizeof(*status), GFP_KERNEL);
984
985 if (!status)
986 return -ENOMEM;
987
988 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
989 USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status,
990 sizeof(*status), USB_CTRL_GET_TIMEOUT);
991
992 *(u16 *)data = *status;
993 kfree(status);
994 return ret;
995}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600996EXPORT_SYMBOL_GPL(usb_get_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998/**
999 * usb_clear_halt - tells device to clear endpoint halt/stall condition
1000 * @dev: device whose endpoint is halted
1001 * @pipe: endpoint "pipe" being cleared
1002 * Context: !in_interrupt ()
1003 *
1004 * This is used to clear halt conditions for bulk and interrupt endpoints,
1005 * as reported by URB completion status. Endpoints that are halted are
1006 * sometimes referred to as being "stalled". Such endpoints are unable
1007 * to transmit or receive data until the halt status is cleared. Any URBs
1008 * queued for such an endpoint should normally be unlinked by the driver
1009 * before clearing the halt condition, as described in sections 5.7.5
1010 * and 5.8.5 of the USB 2.0 spec.
1011 *
1012 * Note that control and isochronous endpoints don't halt, although control
1013 * endpoints report "protocol stall" (for unsupported requests) using the
1014 * same status code used to report a true stall.
1015 *
1016 * This call is synchronous, and may not be used in an interrupt context.
1017 *
1018 * Returns zero on success, or else the status code returned by the
1019 * underlying usb_control_msg() call.
1020 */
1021int usb_clear_halt(struct usb_device *dev, int pipe)
1022{
1023 int result;
1024 int endp = usb_pipeendpoint(pipe);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001025
1026 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 endp |= USB_DIR_IN;
1028
1029 /* we don't care if it wasn't halted first. in fact some devices
1030 * (like some ibmcam model 1 units) seem to expect hosts to make
1031 * this request for iso endpoints, which can't halt!
1032 */
1033 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1034 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
1035 USB_ENDPOINT_HALT, endp, NULL, 0,
1036 USB_CTRL_SET_TIMEOUT);
1037
1038 /* don't un-halt or force to DATA0 except on success */
1039 if (result < 0)
1040 return result;
1041
1042 /* NOTE: seems like Microsoft and Apple don't bother verifying
1043 * the clear "took", so some devices could lock up if you check...
1044 * such as the Hagiwara FlashGate DUAL. So we won't bother.
1045 *
1046 * NOTE: make sure the logic here doesn't diverge much from
1047 * the copy in usb-storage, for as long as we need two copies.
1048 */
1049
David Vrabel3444b262009-04-08 17:36:28 +00001050 usb_reset_endpoint(dev, endp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 return 0;
1053}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001054EXPORT_SYMBOL_GPL(usb_clear_halt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Alan Stern3b23dd62008-12-05 14:10:34 -05001056static int create_intf_ep_devs(struct usb_interface *intf)
1057{
1058 struct usb_device *udev = interface_to_usbdev(intf);
1059 struct usb_host_interface *alt = intf->cur_altsetting;
1060 int i;
1061
1062 if (intf->ep_devs_created || intf->unregistering)
1063 return 0;
1064
1065 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1066 (void) usb_create_ep_devs(&intf->dev, &alt->endpoint[i], udev);
1067 intf->ep_devs_created = 1;
1068 return 0;
1069}
1070
1071static void remove_intf_ep_devs(struct usb_interface *intf)
1072{
1073 struct usb_host_interface *alt = intf->cur_altsetting;
1074 int i;
1075
1076 if (!intf->ep_devs_created)
1077 return;
1078
1079 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1080 usb_remove_ep_devs(&alt->endpoint[i]);
1081 intf->ep_devs_created = 0;
1082}
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084/**
1085 * usb_disable_endpoint -- Disable an endpoint by address
1086 * @dev: the device whose endpoint is being disabled
1087 * @epaddr: the endpoint's address. Endpoint number for output,
1088 * endpoint number + USB_DIR_IN for input
Alan Sternddeac4e72009-01-15 17:03:33 -05001089 * @reset_hardware: flag to erase any endpoint state stored in the
1090 * controller hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 *
Alan Sternddeac4e72009-01-15 17:03:33 -05001092 * Disables the endpoint for URB submission and nukes all pending URBs.
1093 * If @reset_hardware is set then also deallocates hcd/hardware state
1094 * for the endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 */
Alan Sternddeac4e72009-01-15 17:03:33 -05001096void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr,
1097 bool reset_hardware)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
1099 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1100 struct usb_host_endpoint *ep;
1101
1102 if (!dev)
1103 return;
1104
1105 if (usb_endpoint_out(epaddr)) {
1106 ep = dev->ep_out[epnum];
Alan Sternddeac4e72009-01-15 17:03:33 -05001107 if (reset_hardware)
1108 dev->ep_out[epnum] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 } else {
1110 ep = dev->ep_in[epnum];
Alan Sternddeac4e72009-01-15 17:03:33 -05001111 if (reset_hardware)
1112 dev->ep_in[epnum] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
Alan Sternbdd016b2007-07-30 17:05:22 -04001114 if (ep) {
1115 ep->enabled = 0;
Alan Stern95cf82f2007-09-10 11:33:05 -04001116 usb_hcd_flush_endpoint(dev, ep);
Alan Sternddeac4e72009-01-15 17:03:33 -05001117 if (reset_hardware)
1118 usb_hcd_disable_endpoint(dev, ep);
Alan Sternbdd016b2007-07-30 17:05:22 -04001119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
1122/**
David Vrabel3444b262009-04-08 17:36:28 +00001123 * usb_reset_endpoint - Reset an endpoint's state.
1124 * @dev: the device whose endpoint is to be reset
1125 * @epaddr: the endpoint's address. Endpoint number for output,
1126 * endpoint number + USB_DIR_IN for input
1127 *
1128 * Resets any host-side endpoint state such as the toggle bit,
1129 * sequence number or current window.
1130 */
1131void usb_reset_endpoint(struct usb_device *dev, unsigned int epaddr)
1132{
1133 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1134 struct usb_host_endpoint *ep;
1135
1136 if (usb_endpoint_out(epaddr))
1137 ep = dev->ep_out[epnum];
1138 else
1139 ep = dev->ep_in[epnum];
1140 if (ep)
1141 usb_hcd_reset_endpoint(dev, ep);
1142}
1143EXPORT_SYMBOL_GPL(usb_reset_endpoint);
1144
1145
1146/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 * usb_disable_interface -- Disable all endpoints for an interface
1148 * @dev: the device whose interface is being disabled
1149 * @intf: pointer to the interface descriptor
Alan Sternddeac4e72009-01-15 17:03:33 -05001150 * @reset_hardware: flag to erase any endpoint state stored in the
1151 * controller hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 *
1153 * Disables all the endpoints for the interface's current altsetting.
1154 */
Alan Sternddeac4e72009-01-15 17:03:33 -05001155void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf,
1156 bool reset_hardware)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 struct usb_host_interface *alt = intf->cur_altsetting;
1159 int i;
1160
1161 for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
1162 usb_disable_endpoint(dev,
Alan Sternddeac4e72009-01-15 17:03:33 -05001163 alt->endpoint[i].desc.bEndpointAddress,
1164 reset_hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166}
1167
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001168/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 * usb_disable_device - Disable all the endpoints for a USB device
1170 * @dev: the device whose endpoints are being disabled
1171 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
1172 *
1173 * Disables all the device's endpoints, potentially including endpoint 0.
1174 * Deallocates hcd/hardware state for the endpoints (nuking all or most
1175 * pending urbs) and usbcore state for the interfaces, so that usbcore
1176 * must usb_set_configuration() before any interfaces could be used.
1177 */
1178void usb_disable_device(struct usb_device *dev, int skip_ep0)
1179{
1180 int i;
1181
Harvey Harrison441b62c2008-03-03 16:08:34 -08001182 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __func__,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001183 skip_ep0 ? "non-ep0" : "all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 for (i = skip_ep0; i < 16; ++i) {
Alan Sternddeac4e72009-01-15 17:03:33 -05001185 usb_disable_endpoint(dev, i, true);
1186 usb_disable_endpoint(dev, i + USB_DIR_IN, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 /* getting rid of interfaces will disconnect
1190 * any drivers bound to them (a key side effect)
1191 */
1192 if (dev->actconfig) {
1193 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1194 struct usb_interface *interface;
1195
Alan Stern86d30742005-07-29 12:17:16 -07001196 /* remove this interface if it has been registered */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 interface = dev->actconfig->interface[i];
Daniel Ritzd305ef52005-09-22 00:47:24 -07001198 if (!device_is_registered(&interface->dev))
Alan Stern86d30742005-07-29 12:17:16 -07001199 continue;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001200 dev_dbg(&dev->dev, "unregistering interface %s\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001201 dev_name(&interface->dev));
Alan Stern352d0262008-10-29 15:16:58 -04001202 interface->unregistering = 1;
Alan Stern3b23dd62008-12-05 14:10:34 -05001203 remove_intf_ep_devs(interface);
Alan Stern1a211752008-07-30 11:31:50 -04001204 device_del(&interface->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206
1207 /* Now that the interfaces are unbound, nobody should
1208 * try to access them.
1209 */
1210 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001211 put_device(&dev->actconfig->interface[i]->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 dev->actconfig->interface[i] = NULL;
1213 }
1214 dev->actconfig = NULL;
1215 if (dev->state == USB_STATE_CONFIGURED)
1216 usb_set_device_state(dev, USB_STATE_ADDRESS);
1217 }
1218}
1219
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001220/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 * usb_enable_endpoint - Enable an endpoint for USB communications
1222 * @dev: the device whose interface is being enabled
1223 * @ep: the endpoint
David Vrabel3444b262009-04-08 17:36:28 +00001224 * @reset_ep: flag to reset the endpoint state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 *
David Vrabel3444b262009-04-08 17:36:28 +00001226 * Resets the endpoint state if asked, and sets dev->ep_{in,out} pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 * For control endpoints, both the input and output sides are handled.
1228 */
Alan Stern2caf7fc2008-12-31 11:31:33 -05001229void usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep,
David Vrabel3444b262009-04-08 17:36:28 +00001230 bool reset_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Alan Sternbdd016b2007-07-30 17:05:22 -04001232 int epnum = usb_endpoint_num(&ep->desc);
1233 int is_out = usb_endpoint_dir_out(&ep->desc);
1234 int is_control = usb_endpoint_xfer_control(&ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
David Vrabel3444b262009-04-08 17:36:28 +00001236 if (reset_ep)
1237 usb_hcd_reset_endpoint(dev, ep);
1238 if (is_out || is_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 dev->ep_out[epnum] = ep;
David Vrabel3444b262009-04-08 17:36:28 +00001240 if (!is_out || is_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 dev->ep_in[epnum] = ep;
Alan Sternbdd016b2007-07-30 17:05:22 -04001242 ep->enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001245/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 * usb_enable_interface - Enable all the endpoints for an interface
1247 * @dev: the device whose interface is being enabled
1248 * @intf: pointer to the interface descriptor
David Vrabel3444b262009-04-08 17:36:28 +00001249 * @reset_eps: flag to reset the endpoints' state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 *
1251 * Enables all the endpoints for the interface's current altsetting.
1252 */
Alan Stern2caf7fc2008-12-31 11:31:33 -05001253void usb_enable_interface(struct usb_device *dev,
David Vrabel3444b262009-04-08 17:36:28 +00001254 struct usb_interface *intf, bool reset_eps)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 struct usb_host_interface *alt = intf->cur_altsetting;
1257 int i;
1258
1259 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
David Vrabel3444b262009-04-08 17:36:28 +00001260 usb_enable_endpoint(dev, &alt->endpoint[i], reset_eps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
1263/**
1264 * usb_set_interface - Makes a particular alternate setting be current
1265 * @dev: the device whose interface is being updated
1266 * @interface: the interface being updated
1267 * @alternate: the setting being chosen.
1268 * Context: !in_interrupt ()
1269 *
1270 * This is used to enable data transfers on interfaces that may not
1271 * be enabled by default. Not all devices support such configurability.
1272 * Only the driver bound to an interface may change its setting.
1273 *
1274 * Within any given configuration, each interface may have several
1275 * alternative settings. These are often used to control levels of
1276 * bandwidth consumption. For example, the default setting for a high
1277 * speed interrupt endpoint may not send more than 64 bytes per microframe,
1278 * while interrupt transfers of up to 3KBytes per microframe are legal.
1279 * Also, isochronous endpoints may never be part of an
1280 * interface's default setting. To access such bandwidth, alternate
1281 * interface settings must be made current.
1282 *
1283 * Note that in the Linux USB subsystem, bandwidth associated with
1284 * an endpoint in a given alternate setting is not reserved until an URB
1285 * is submitted that needs that bandwidth. Some other operating systems
1286 * allocate bandwidth early, when a configuration is chosen.
1287 *
1288 * This call is synchronous, and may not be used in an interrupt context.
1289 * Also, drivers must not change altsettings while urbs are scheduled for
1290 * endpoints in that interface; all such urbs must first be completed
1291 * (perhaps forced by unlinking).
1292 *
1293 * Returns zero on success, or else the status code returned by the
1294 * underlying usb_control_msg() call.
1295 */
1296int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1297{
1298 struct usb_interface *iface;
1299 struct usb_host_interface *alt;
1300 int ret;
1301 int manual = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001302 unsigned int epaddr;
1303 unsigned int pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 if (dev->state == USB_STATE_SUSPENDED)
1306 return -EHOSTUNREACH;
1307
1308 iface = usb_ifnum_to_if(dev, interface);
1309 if (!iface) {
1310 dev_dbg(&dev->dev, "selecting invalid interface %d\n",
1311 interface);
1312 return -EINVAL;
1313 }
1314
1315 alt = usb_altnum_to_altsetting(iface, alternate);
1316 if (!alt) {
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -07001317 dev_warn(&dev->dev, "selecting invalid altsetting %d",
1318 alternate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return -EINVAL;
1320 }
1321
Alan Stern392e1d92008-03-11 10:20:12 -04001322 if (dev->quirks & USB_QUIRK_NO_SET_INTF)
1323 ret = -EPIPE;
1324 else
1325 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1327 alternate, interface, NULL, 0, 5000);
1328
1329 /* 9.4.10 says devices don't need this and are free to STALL the
1330 * request if the interface only has one alternate setting.
1331 */
1332 if (ret == -EPIPE && iface->num_altsetting == 1) {
1333 dev_dbg(&dev->dev,
1334 "manual set_interface for iface %d, alt %d\n",
1335 interface, alternate);
1336 manual = 1;
1337 } else if (ret < 0)
1338 return ret;
1339
1340 /* FIXME drivers shouldn't need to replicate/bugfix the logic here
1341 * when they implement async or easily-killable versions of this or
1342 * other "should-be-internal" functions (like clear_halt).
1343 * should hcd+usbcore postprocess control requests?
1344 */
1345
1346 /* prevent submissions using previous endpoint settings */
Alan Stern3b23dd62008-12-05 14:10:34 -05001347 if (iface->cur_altsetting != alt) {
1348 remove_intf_ep_devs(iface);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001349 usb_remove_sysfs_intf_files(iface);
Alan Stern3b23dd62008-12-05 14:10:34 -05001350 }
Alan Sternddeac4e72009-01-15 17:03:33 -05001351 usb_disable_interface(dev, iface, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 iface->cur_altsetting = alt;
1354
1355 /* If the interface only has one altsetting and the device didn't
David Brownella81e7ec2005-04-18 17:39:25 -07001356 * accept the request, we attempt to carry out the equivalent action
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 * by manually clearing the HALT feature for each endpoint in the
1358 * new altsetting.
1359 */
1360 if (manual) {
1361 int i;
1362
1363 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001364 epaddr = alt->endpoint[i].desc.bEndpointAddress;
1365 pipe = __create_pipe(dev,
1366 USB_ENDPOINT_NUMBER_MASK & epaddr) |
1367 (usb_endpoint_out(epaddr) ?
1368 USB_DIR_OUT : USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 usb_clear_halt(dev, pipe);
1371 }
1372 }
1373
1374 /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
1375 *
1376 * Note:
1377 * Despite EP0 is always present in all interfaces/AS, the list of
1378 * endpoints from the descriptor does not contain EP0. Due to its
1379 * omnipresence one might expect EP0 being considered "affected" by
1380 * any SetInterface request and hence assume toggles need to be reset.
1381 * However, EP0 toggles are re-synced for every individual transfer
1382 * during the SETUP stage - hence EP0 toggles are "don't care" here.
1383 * (Likewise, EP0 never "halts" on well designed devices.)
1384 */
Alan Stern2caf7fc2008-12-31 11:31:33 -05001385 usb_enable_interface(dev, iface, true);
Alan Stern3b23dd62008-12-05 14:10:34 -05001386 if (device_is_registered(&iface->dev)) {
Alan Stern0e6c8e82005-10-24 15:33:03 -04001387 usb_create_sysfs_intf_files(iface);
Alan Stern3b23dd62008-12-05 14:10:34 -05001388 create_intf_ep_devs(iface);
1389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return 0;
1391}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001392EXPORT_SYMBOL_GPL(usb_set_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394/**
1395 * usb_reset_configuration - lightweight device reset
1396 * @dev: the device whose configuration is being reset
1397 *
1398 * This issues a standard SET_CONFIGURATION request to the device using
1399 * the current configuration. The effect is to reset most USB-related
1400 * state in the device, including interface altsettings (reset to zero),
David Vrabel3444b262009-04-08 17:36:28 +00001401 * endpoint halts (cleared), and endpoint state (only for bulk and interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 * endpoints). Other usbcore state is unchanged, including bindings of
1403 * usb device drivers to interfaces.
1404 *
1405 * Because this affects multiple interfaces, avoid using this with composite
1406 * (multi-interface) devices. Instead, the driver for each interface may
David Brownella81e7ec2005-04-18 17:39:25 -07001407 * use usb_set_interface() on the interfaces it claims. Be careful though;
1408 * some devices don't support the SET_INTERFACE request, and others won't
David Vrabel3444b262009-04-08 17:36:28 +00001409 * reset all the interface state (notably endpoint state). Resetting the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 * configuration would affect other drivers' interfaces.
1411 *
1412 * The caller must own the device lock.
1413 *
1414 * Returns zero on success, else a negative error code.
1415 */
1416int usb_reset_configuration(struct usb_device *dev)
1417{
1418 int i, retval;
1419 struct usb_host_config *config;
1420
1421 if (dev->state == USB_STATE_SUSPENDED)
1422 return -EHOSTUNREACH;
1423
1424 /* caller must have locked the device and must own
1425 * the usb bus readlock (so driver bindings are stable);
1426 * calls during probe() are fine
1427 */
1428
1429 for (i = 1; i < 16; ++i) {
Alan Sternddeac4e72009-01-15 17:03:33 -05001430 usb_disable_endpoint(dev, i, true);
1431 usb_disable_endpoint(dev, i + USB_DIR_IN, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433
1434 config = dev->actconfig;
1435 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1436 USB_REQ_SET_CONFIGURATION, 0,
1437 config->desc.bConfigurationValue, 0,
1438 NULL, 0, USB_CTRL_SET_TIMEOUT);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001439 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /* re-init hc/hcd interface/endpoint state */
1443 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1444 struct usb_interface *intf = config->interface[i];
1445 struct usb_host_interface *alt;
1446
1447 alt = usb_altnum_to_altsetting(intf, 0);
1448
1449 /* No altsetting 0? We'll assume the first altsetting.
1450 * We could use a GetInterface call, but if a device is
1451 * so non-compliant that it doesn't have altsetting 0
1452 * then I wouldn't trust its reply anyway.
1453 */
1454 if (!alt)
1455 alt = &intf->altsetting[0];
1456
Alan Stern3b23dd62008-12-05 14:10:34 -05001457 if (alt != intf->cur_altsetting) {
1458 remove_intf_ep_devs(intf);
1459 usb_remove_sysfs_intf_files(intf);
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 intf->cur_altsetting = alt;
Alan Stern2caf7fc2008-12-31 11:31:33 -05001462 usb_enable_interface(dev, intf, true);
Alan Stern3b23dd62008-12-05 14:10:34 -05001463 if (device_is_registered(&intf->dev)) {
Alan Stern0e6c8e82005-10-24 15:33:03 -04001464 usb_create_sysfs_intf_files(intf);
Alan Stern3b23dd62008-12-05 14:10:34 -05001465 create_intf_ep_devs(intf);
1466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
1468 return 0;
1469}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001470EXPORT_SYMBOL_GPL(usb_reset_configuration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Greg Kroah-Hartmanb0e396e2007-08-02 22:44:27 -06001472static void usb_release_interface(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
1474 struct usb_interface *intf = to_usb_interface(dev);
1475 struct usb_interface_cache *intfc =
1476 altsetting_to_usb_interface_cache(intf->altsetting);
1477
1478 kref_put(&intfc->ref, usb_release_interface_cache);
1479 kfree(intf);
1480}
1481
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001482#ifdef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +02001483static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001484{
1485 struct usb_device *usb_dev;
1486 struct usb_interface *intf;
1487 struct usb_host_interface *alt;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001488
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001489 intf = to_usb_interface(dev);
1490 usb_dev = interface_to_usbdev(intf);
1491 alt = intf->cur_altsetting;
1492
Kay Sievers7eff2e72007-08-14 15:15:12 +02001493 if (add_uevent_var(env, "INTERFACE=%d/%d/%d",
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001494 alt->desc.bInterfaceClass,
1495 alt->desc.bInterfaceSubClass,
1496 alt->desc.bInterfaceProtocol))
1497 return -ENOMEM;
1498
Kay Sievers7eff2e72007-08-14 15:15:12 +02001499 if (add_uevent_var(env,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001500 "MODALIAS=usb:"
1501 "v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001502 le16_to_cpu(usb_dev->descriptor.idVendor),
1503 le16_to_cpu(usb_dev->descriptor.idProduct),
1504 le16_to_cpu(usb_dev->descriptor.bcdDevice),
1505 usb_dev->descriptor.bDeviceClass,
1506 usb_dev->descriptor.bDeviceSubClass,
1507 usb_dev->descriptor.bDeviceProtocol,
1508 alt->desc.bInterfaceClass,
1509 alt->desc.bInterfaceSubClass,
1510 alt->desc.bInterfaceProtocol))
1511 return -ENOMEM;
1512
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001513 return 0;
1514}
1515
1516#else
1517
Kay Sievers7eff2e72007-08-14 15:15:12 +02001518static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001519{
1520 return -ENODEV;
1521}
1522#endif /* CONFIG_HOTPLUG */
1523
1524struct device_type usb_if_device_type = {
1525 .name = "usb_interface",
1526 .release = usb_release_interface,
1527 .uevent = usb_if_uevent,
1528};
1529
Craig W. Nadler165fe972007-06-15 23:14:35 -04001530static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001531 struct usb_host_config *config,
1532 u8 inum)
Craig W. Nadler165fe972007-06-15 23:14:35 -04001533{
1534 struct usb_interface_assoc_descriptor *retval = NULL;
1535 struct usb_interface_assoc_descriptor *intf_assoc;
1536 int first_intf;
1537 int last_intf;
1538 int i;
1539
1540 for (i = 0; (i < USB_MAXIADS && config->intf_assoc[i]); i++) {
1541 intf_assoc = config->intf_assoc[i];
1542 if (intf_assoc->bInterfaceCount == 0)
1543 continue;
1544
1545 first_intf = intf_assoc->bFirstInterface;
1546 last_intf = first_intf + (intf_assoc->bInterfaceCount - 1);
1547 if (inum >= first_intf && inum <= last_intf) {
1548 if (!retval)
1549 retval = intf_assoc;
1550 else
1551 dev_err(&dev->dev, "Interface #%d referenced"
1552 " by multiple IADs\n", inum);
1553 }
1554 }
1555
1556 return retval;
1557}
1558
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001559
1560/*
1561 * Internal function to queue a device reset
1562 *
1563 * This is initialized into the workstruct in 'struct
1564 * usb_device->reset_ws' that is launched by
1565 * message.c:usb_set_configuration() when initializing each 'struct
1566 * usb_interface'.
1567 *
1568 * It is safe to get the USB device without reference counts because
1569 * the life cycle of @iface is bound to the life cycle of @udev. Then,
1570 * this function will be ran only if @iface is alive (and before
1571 * freeing it any scheduled instances of it will have been cancelled).
1572 *
1573 * We need to set a flag (usb_dev->reset_running) because when we call
1574 * the reset, the interfaces might be unbound. The current interface
1575 * cannot try to remove the queued work as it would cause a deadlock
1576 * (you cannot remove your work from within your executing
1577 * workqueue). This flag lets it know, so that
1578 * usb_cancel_queued_reset() doesn't try to do it.
1579 *
1580 * See usb_queue_reset_device() for more details
1581 */
1582void __usb_queue_reset_device(struct work_struct *ws)
1583{
1584 int rc;
1585 struct usb_interface *iface =
1586 container_of(ws, struct usb_interface, reset_ws);
1587 struct usb_device *udev = interface_to_usbdev(iface);
1588
1589 rc = usb_lock_device_for_reset(udev, iface);
1590 if (rc >= 0) {
1591 iface->reset_running = 1;
1592 usb_reset_device(udev);
1593 iface->reset_running = 0;
1594 usb_unlock_device(udev);
1595 }
1596}
1597
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599/*
1600 * usb_set_configuration - Makes a particular device setting be current
1601 * @dev: the device whose configuration is being updated
1602 * @configuration: the configuration being chosen.
1603 * Context: !in_interrupt(), caller owns the device lock
1604 *
1605 * This is used to enable non-default device modes. Not all devices
1606 * use this kind of configurability; many devices only have one
1607 * configuration.
1608 *
Alan Stern3f141e22007-02-08 16:40:43 -05001609 * @configuration is the value of the configuration to be installed.
1610 * According to the USB spec (e.g. section 9.1.1.5), configuration values
1611 * must be non-zero; a value of zero indicates that the device in
1612 * unconfigured. However some devices erroneously use 0 as one of their
1613 * configuration values. To help manage such devices, this routine will
1614 * accept @configuration = -1 as indicating the device should be put in
1615 * an unconfigured state.
1616 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 * USB device configurations may affect Linux interoperability,
1618 * power consumption and the functionality available. For example,
1619 * the default configuration is limited to using 100mA of bus power,
1620 * so that when certain device functionality requires more power,
1621 * and the device is bus powered, that functionality should be in some
1622 * non-default device configuration. Other device modes may also be
1623 * reflected as configuration options, such as whether two ISDN
1624 * channels are available independently; and choosing between open
1625 * standard device protocols (like CDC) or proprietary ones.
1626 *
Inaky Perez-Gonzalez16bbab22007-07-31 20:34:01 -07001627 * Note that a non-authorized device (dev->authorized == 0) will only
1628 * be put in unconfigured mode.
1629 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 * Note that USB has an additional level of device configurability,
1631 * associated with interfaces. That configurability is accessed using
1632 * usb_set_interface().
1633 *
1634 * This call is synchronous. The calling context must be able to sleep,
1635 * must own the device lock, and must not hold the driver model's USB
Ming Lei6d243e52008-06-17 23:24:08 +08001636 * bus mutex; usb interface driver probe() methods cannot use this routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 *
1638 * Returns zero on success, or else the status code returned by the
Steven Cole093cf722005-05-03 19:07:24 -06001639 * underlying call that failed. On successful completion, each interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 * in the original device configuration has been destroyed, and each one
1641 * in the new configuration has been probed by all relevant usb device
1642 * drivers currently known to the kernel.
1643 */
1644int usb_set_configuration(struct usb_device *dev, int configuration)
1645{
1646 int i, ret;
1647 struct usb_host_config *cp = NULL;
1648 struct usb_interface **new_interfaces = NULL;
1649 int n, nintf;
1650
Inaky Perez-Gonzalez16bbab22007-07-31 20:34:01 -07001651 if (dev->authorized == 0 || configuration == -1)
Alan Stern3f141e22007-02-08 16:40:43 -05001652 configuration = 0;
1653 else {
1654 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1655 if (dev->config[i].desc.bConfigurationValue ==
1656 configuration) {
1657 cp = &dev->config[i];
1658 break;
1659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
1661 }
1662 if ((!cp && configuration != 0))
1663 return -EINVAL;
1664
1665 /* The USB spec says configuration 0 means unconfigured.
1666 * But if a device includes a configuration numbered 0,
1667 * we will accept it as a correctly configured state.
Alan Stern3f141e22007-02-08 16:40:43 -05001668 * Use -1 if you really want to unconfigure the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 */
1670 if (cp && configuration == 0)
1671 dev_warn(&dev->dev, "config 0 descriptor??\n");
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 /* Allocate memory for new interfaces before doing anything else,
1674 * so that if we run out then nothing will have changed. */
1675 n = nintf = 0;
1676 if (cp) {
1677 nintf = cp->desc.bNumInterfaces;
1678 new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
1679 GFP_KERNEL);
1680 if (!new_interfaces) {
Joe Perches898eb712007-10-18 03:06:30 -07001681 dev_err(&dev->dev, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return -ENOMEM;
1683 }
1684
1685 for (; n < nintf; ++n) {
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001686 new_interfaces[n] = kzalloc(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 sizeof(struct usb_interface),
1688 GFP_KERNEL);
1689 if (!new_interfaces[n]) {
Joe Perches898eb712007-10-18 03:06:30 -07001690 dev_err(&dev->dev, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 ret = -ENOMEM;
1692free_interfaces:
1693 while (--n >= 0)
1694 kfree(new_interfaces[n]);
1695 kfree(new_interfaces);
1696 return ret;
1697 }
1698 }
Alan Stern6ad07122006-06-01 13:59:16 -04001699
1700 i = dev->bus_mA - cp->desc.bMaxPower * 2;
1701 if (i < 0)
1702 dev_warn(&dev->dev, "new config #%d exceeds power "
1703 "limit by %dmA\n",
1704 configuration, -i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
1706
Alan Stern01d883d2006-08-30 15:47:18 -04001707 /* Wake up the device so we can send it the Set-Config request */
Alan Stern94fcda12006-11-20 11:38:46 -05001708 ret = usb_autoresume_device(dev);
Alan Stern01d883d2006-08-30 15:47:18 -04001709 if (ret)
1710 goto free_interfaces;
1711
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001712 /* Make sure we have bandwidth (and available HCD resources) for this
1713 * configuration. Remove endpoints from the schedule if we're dropping
1714 * this configuration to set configuration 0. After this point, the
1715 * host controller will not allow submissions to dropped endpoints. If
1716 * this call fails, the device state is unchanged.
1717 */
1718 if (cp)
1719 ret = usb_hcd_check_bandwidth(dev, cp, NULL);
1720 else
1721 ret = usb_hcd_check_bandwidth(dev, NULL, NULL);
1722 if (ret < 0) {
1723 usb_autosuspend_device(dev);
1724 goto free_interfaces;
1725 }
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 /* if it's already configured, clear out old state first.
1728 * getting rid of old interfaces means unbinding their drivers.
1729 */
1730 if (dev->state != USB_STATE_ADDRESS)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001731 usb_disable_device(dev, 1); /* Skip ep0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Alan Sterndf718962008-12-19 10:27:56 -05001733 /* Get rid of pending async Set-Config requests for this device */
1734 cancel_async_set_config(dev);
1735
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001736 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1737 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
1738 NULL, 0, USB_CTRL_SET_TIMEOUT);
1739 if (ret < 0) {
Alan Stern6ad07122006-06-01 13:59:16 -04001740 /* All the old state is gone, so what else can we do?
1741 * The device is probably useless now anyway.
1742 */
1743 cp = NULL;
1744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 dev->actconfig = cp;
Alan Stern6ad07122006-06-01 13:59:16 -04001747 if (!cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 usb_set_device_state(dev, USB_STATE_ADDRESS);
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001749 usb_hcd_check_bandwidth(dev, NULL, NULL);
Alan Stern94fcda12006-11-20 11:38:46 -05001750 usb_autosuspend_device(dev);
Alan Stern6ad07122006-06-01 13:59:16 -04001751 goto free_interfaces;
1752 }
1753 usb_set_device_state(dev, USB_STATE_CONFIGURED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Alan Stern6ad07122006-06-01 13:59:16 -04001755 /* Initialize the new interface structures and the
1756 * hc/hcd/usbcore interface/endpoint state.
1757 */
1758 for (i = 0; i < nintf; ++i) {
1759 struct usb_interface_cache *intfc;
1760 struct usb_interface *intf;
1761 struct usb_host_interface *alt;
1762
1763 cp->interface[i] = intf = new_interfaces[i];
1764 intfc = cp->intf_cache[i];
1765 intf->altsetting = intfc->altsetting;
1766 intf->num_altsetting = intfc->num_altsetting;
Craig W. Nadler165fe972007-06-15 23:14:35 -04001767 intf->intf_assoc = find_iad(dev, cp, i);
Alan Stern6ad07122006-06-01 13:59:16 -04001768 kref_get(&intfc->ref);
1769
1770 alt = usb_altnum_to_altsetting(intf, 0);
1771
1772 /* No altsetting 0? We'll assume the first altsetting.
1773 * We could use a GetInterface call, but if a device is
1774 * so non-compliant that it doesn't have altsetting 0
1775 * then I wouldn't trust its reply anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 */
Alan Stern6ad07122006-06-01 13:59:16 -04001777 if (!alt)
1778 alt = &intf->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Alan Stern6ad07122006-06-01 13:59:16 -04001780 intf->cur_altsetting = alt;
Alan Stern2caf7fc2008-12-31 11:31:33 -05001781 usb_enable_interface(dev, intf, true);
Alan Stern6ad07122006-06-01 13:59:16 -04001782 intf->dev.parent = &dev->dev;
1783 intf->dev.driver = NULL;
1784 intf->dev.bus = &usb_bus_type;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001785 intf->dev.type = &usb_if_device_type;
Alan Stern2e5f10e2008-04-30 15:37:19 -04001786 intf->dev.groups = usb_interface_groups;
Alan Stern6ad07122006-06-01 13:59:16 -04001787 intf->dev.dma_mask = dev->dev.dma_mask;
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001788 INIT_WORK(&intf->reset_ws, __usb_queue_reset_device);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001789 device_initialize(&intf->dev);
Alan Stern6ad07122006-06-01 13:59:16 -04001790 mark_quiesced(intf);
Kay Sievers0031a062008-05-02 06:02:41 +02001791 dev_set_name(&intf->dev, "%d-%s:%d.%d",
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001792 dev->bus->busnum, dev->devpath,
1793 configuration, alt->desc.bInterfaceNumber);
Alan Stern6ad07122006-06-01 13:59:16 -04001794 }
1795 kfree(new_interfaces);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Alan Stern1662e3a2009-03-18 14:28:53 -04001797 if (cp->string == NULL &&
1798 !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
Alan Stern6ad07122006-06-01 13:59:16 -04001799 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Alan Stern6ad07122006-06-01 13:59:16 -04001801 /* Now that all the interfaces are set up, register them
1802 * to trigger binding of drivers to interfaces. probe()
1803 * routines may install different altsettings and may
1804 * claim() any interfaces not yet bound. Many class drivers
1805 * need that: CDC, audio, video, etc.
1806 */
1807 for (i = 0; i < nintf; ++i) {
1808 struct usb_interface *intf = cp->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001810 dev_dbg(&dev->dev,
Alan Stern6ad07122006-06-01 13:59:16 -04001811 "adding %s (config #%d, interface %d)\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001812 dev_name(&intf->dev), configuration,
Alan Stern6ad07122006-06-01 13:59:16 -04001813 intf->cur_altsetting->desc.bInterfaceNumber);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001814 ret = device_add(&intf->dev);
Alan Stern6ad07122006-06-01 13:59:16 -04001815 if (ret != 0) {
1816 dev_err(&dev->dev, "device_add(%s) --> %d\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001817 dev_name(&intf->dev), ret);
Alan Stern6ad07122006-06-01 13:59:16 -04001818 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
Alan Stern3b23dd62008-12-05 14:10:34 -05001820 create_intf_ep_devs(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
1822
Alan Stern94fcda12006-11-20 11:38:46 -05001823 usb_autosuspend_device(dev);
Alan Stern86d30742005-07-29 12:17:16 -07001824 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Alan Sterndf718962008-12-19 10:27:56 -05001827static LIST_HEAD(set_config_list);
1828static DEFINE_SPINLOCK(set_config_lock);
1829
Alan Stern088dc272006-08-21 12:08:19 -04001830struct set_config_request {
1831 struct usb_device *udev;
1832 int config;
1833 struct work_struct work;
Alan Sterndf718962008-12-19 10:27:56 -05001834 struct list_head node;
Alan Stern088dc272006-08-21 12:08:19 -04001835};
1836
1837/* Worker routine for usb_driver_set_configuration() */
David Howellsc4028952006-11-22 14:57:56 +00001838static void driver_set_config_work(struct work_struct *work)
Alan Stern088dc272006-08-21 12:08:19 -04001839{
David Howellsc4028952006-11-22 14:57:56 +00001840 struct set_config_request *req =
1841 container_of(work, struct set_config_request, work);
Alan Sterndf718962008-12-19 10:27:56 -05001842 struct usb_device *udev = req->udev;
Alan Stern088dc272006-08-21 12:08:19 -04001843
Alan Sterndf718962008-12-19 10:27:56 -05001844 usb_lock_device(udev);
1845 spin_lock(&set_config_lock);
1846 list_del(&req->node);
1847 spin_unlock(&set_config_lock);
1848
1849 if (req->config >= -1) /* Is req still valid? */
1850 usb_set_configuration(udev, req->config);
1851 usb_unlock_device(udev);
1852 usb_put_dev(udev);
Alan Stern088dc272006-08-21 12:08:19 -04001853 kfree(req);
1854}
1855
Alan Sterndf718962008-12-19 10:27:56 -05001856/* Cancel pending Set-Config requests for a device whose configuration
1857 * was just changed
1858 */
1859static void cancel_async_set_config(struct usb_device *udev)
1860{
1861 struct set_config_request *req;
1862
1863 spin_lock(&set_config_lock);
1864 list_for_each_entry(req, &set_config_list, node) {
1865 if (req->udev == udev)
1866 req->config = -999; /* Mark as cancelled */
1867 }
1868 spin_unlock(&set_config_lock);
1869}
1870
Alan Stern088dc272006-08-21 12:08:19 -04001871/**
1872 * usb_driver_set_configuration - Provide a way for drivers to change device configurations
1873 * @udev: the device whose configuration is being updated
1874 * @config: the configuration being chosen.
1875 * Context: In process context, must be able to sleep
1876 *
1877 * Device interface drivers are not allowed to change device configurations.
1878 * This is because changing configurations will destroy the interface the
1879 * driver is bound to and create new ones; it would be like a floppy-disk
1880 * driver telling the computer to replace the floppy-disk drive with a
1881 * tape drive!
1882 *
1883 * Still, in certain specialized circumstances the need may arise. This
1884 * routine gets around the normal restrictions by using a work thread to
1885 * submit the change-config request.
1886 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001887 * Returns 0 if the request was successfully queued, error code otherwise.
Alan Stern088dc272006-08-21 12:08:19 -04001888 * The caller has no way to know whether the queued request will eventually
1889 * succeed.
1890 */
1891int usb_driver_set_configuration(struct usb_device *udev, int config)
1892{
1893 struct set_config_request *req;
1894
1895 req = kmalloc(sizeof(*req), GFP_KERNEL);
1896 if (!req)
1897 return -ENOMEM;
1898 req->udev = udev;
1899 req->config = config;
David Howellsc4028952006-11-22 14:57:56 +00001900 INIT_WORK(&req->work, driver_set_config_work);
Alan Stern088dc272006-08-21 12:08:19 -04001901
Alan Sterndf718962008-12-19 10:27:56 -05001902 spin_lock(&set_config_lock);
1903 list_add(&req->node, &set_config_list);
1904 spin_unlock(&set_config_lock);
1905
Alan Stern088dc272006-08-21 12:08:19 -04001906 usb_get_dev(udev);
Alan Stern1737bf22006-12-15 16:04:52 -05001907 schedule_work(&req->work);
Alan Stern088dc272006-08-21 12:08:19 -04001908 return 0;
1909}
1910EXPORT_SYMBOL_GPL(usb_driver_set_configuration);