blob: 82927e1ed27d078f513a69727ebf7e55ef885ad8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * message.c - synchronous message handling
3 */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/pci.h> /* for scatterlist macros */
6#include <linux/usb.h>
7#include <linux/module.h>
8#include <linux/slab.h>
9#include <linux/init.h>
10#include <linux/mm.h>
11#include <linux/timer.h>
12#include <linux/ctype.h>
Clemens Ladischa853a3d2009-04-24 10:12:18 +020013#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/device.h>
Ralf Baechle11763602007-10-23 20:42:11 +020015#include <linux/scatterlist.h>
Oliver Neukum7ceec1f2007-01-26 14:26:21 +010016#include <linux/usb/quirks.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020017#include <linux/usb/hcd.h> /* for usbcore internals */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/byteorder.h>
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "usb.h"
21
Alan Sterndf718962008-12-19 10:27:56 -050022static void cancel_async_set_config(struct usb_device *udev);
23
Alan Stern67f5dde2007-07-24 18:23:23 -040024struct api_context {
25 struct completion done;
26 int status;
27};
28
David Howells7d12e782006-10-05 14:55:46 +010029static void usb_api_blocking_completion(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Alan Stern67f5dde2007-07-24 18:23:23 -040031 struct api_context *ctx = urb->context;
32
33 ctx->status = urb->status;
34 complete(&ctx->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
37
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020038/*
39 * Starts urb and waits for completion or timeout. Note that this call
40 * is NOT interruptible. Many device driver i/o requests should be
41 * interruptible and therefore these drivers should implement their
42 * own interruptible routines.
43 */
44static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -080045{
Alan Stern67f5dde2007-07-24 18:23:23 -040046 struct api_context ctx;
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020047 unsigned long expire;
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -070048 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Alan Stern67f5dde2007-07-24 18:23:23 -040050 init_completion(&ctx.done);
51 urb->context = &ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 urb->actual_length = 0;
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -070053 retval = usb_submit_urb(urb, GFP_NOIO);
54 if (unlikely(retval))
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020055 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020057 expire = timeout ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT;
Alan Stern67f5dde2007-07-24 18:23:23 -040058 if (!wait_for_completion_timeout(&ctx.done, expire)) {
59 usb_kill_urb(urb);
60 retval = (ctx.status == -ENOENT ? -ETIMEDOUT : ctx.status);
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020061
62 dev_dbg(&urb->dev->dev,
Roel Kluin71d27182009-03-13 12:19:18 +010063 "%s timed out on ep%d%s len=%u/%u\n",
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020064 current->comm,
Alan Stern5e60a162007-07-30 17:07:21 -040065 usb_endpoint_num(&urb->ep->desc),
66 usb_urb_dir_in(urb) ? "in" : "out",
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020067 urb->actual_length,
68 urb->transfer_buffer_length);
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020069 } else
Alan Stern67f5dde2007-07-24 18:23:23 -040070 retval = ctx.status;
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020071out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (actual_length)
73 *actual_length = urb->actual_length;
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 usb_free_urb(urb);
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -070076 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79/*-------------------------------------------------------------------*/
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -080080/* returns status (negative) or length (positive) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static int usb_internal_control_msg(struct usb_device *usb_dev,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -080082 unsigned int pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 struct usb_ctrlrequest *cmd,
84 void *data, int len, int timeout)
85{
86 struct urb *urb;
87 int retv;
88 int length;
89
90 urb = usb_alloc_urb(0, GFP_NOIO);
91 if (!urb)
92 return -ENOMEM;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -080093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char *)cmd, data,
95 len, usb_api_blocking_completion, NULL);
96
97 retv = usb_start_wait_urb(urb, timeout, &length);
98 if (retv < 0)
99 return retv;
100 else
101 return length;
102}
103
104/**
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800105 * usb_control_msg - Builds a control urb, sends it off and waits for completion
106 * @dev: pointer to the usb device to send the message to
107 * @pipe: endpoint "pipe" to send the message to
108 * @request: USB message request value
109 * @requesttype: USB message request type value
110 * @value: USB message value
111 * @index: USB message index value
112 * @data: pointer to the data to send
113 * @size: length in bytes of the data to send
114 * @timeout: time in msecs to wait for the message to complete before timing
115 * out (if 0 the wait is forever)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800117 * Context: !in_interrupt ()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800119 * This function sends a simple control message to a specified endpoint and
120 * waits for the message to complete, or timeout.
121 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800122 * Don't use this function from within an interrupt context, like a bottom half
123 * handler. If you need an asynchronous message, or need to send a message
124 * from within interrupt context, use usb_submit_urb().
125 * If a thread in your driver uses this call, make sure your disconnect()
126 * method can wait for it to complete. Since you don't have a handle on the
127 * URB used, you can't cancel the request.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200128 *
129 * Return: If successful, the number of bytes transferred. Otherwise, a negative
130 * error number.
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);
150
151 kfree(dr);
152
153 return ret;
154}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600155EXPORT_SYMBOL_GPL(usb_control_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157/**
Greg Kroah-Hartman782a7a62006-05-19 13:20:20 -0700158 * usb_interrupt_msg - Builds an interrupt urb, sends it off and waits for completion
159 * @usb_dev: pointer to the usb device to send the message to
160 * @pipe: endpoint "pipe" to send the message to
161 * @data: pointer to the data to send
162 * @len: length in bytes of the data to send
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800163 * @actual_length: pointer to a location to put the actual length transferred
164 * in bytes
Greg Kroah-Hartman782a7a62006-05-19 13:20:20 -0700165 * @timeout: time in msecs to wait for the message to complete before
166 * timing out (if 0 the wait is forever)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800167 *
Greg Kroah-Hartman782a7a62006-05-19 13:20:20 -0700168 * Context: !in_interrupt ()
169 *
170 * This function sends a simple interrupt message to a specified endpoint and
171 * waits for the message to complete, or timeout.
172 *
Greg Kroah-Hartman782a7a62006-05-19 13:20:20 -0700173 * Don't use this function from within an interrupt context, like a bottom half
174 * handler. If you need an asynchronous message, or need to send a message
175 * from within interrupt context, use usb_submit_urb() If a thread in your
176 * driver uses this call, make sure your disconnect() method can wait for it to
177 * complete. Since you don't have a handle on the URB used, you can't cancel
178 * the request.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200179 *
180 * Return:
181 * If successful, 0. Otherwise a negative error number. The number of actual
182 * bytes transferred will be stored in the @actual_length paramater.
Greg Kroah-Hartman782a7a62006-05-19 13:20:20 -0700183 */
184int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe,
185 void *data, int len, int *actual_length, int timeout)
186{
187 return usb_bulk_msg(usb_dev, pipe, data, len, actual_length, timeout);
188}
189EXPORT_SYMBOL_GPL(usb_interrupt_msg);
190
191/**
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800192 * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion
193 * @usb_dev: pointer to the usb device to send the message to
194 * @pipe: endpoint "pipe" to send the message to
195 * @data: pointer to the data to send
196 * @len: length in bytes of the data to send
197 * @actual_length: pointer to a location to put the actual length transferred
198 * in bytes
199 * @timeout: time in msecs to wait for the message to complete before
200 * timing out (if 0 the wait is forever)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800202 * Context: !in_interrupt ()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800204 * This function sends a simple bulk message to a specified endpoint
205 * and waits for the message to complete, or timeout.
Alan Sternd09d36a2005-09-26 16:22:45 -0400206 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800207 * Don't use this function from within an interrupt context, like a bottom half
208 * handler. If you need an asynchronous message, or need to send a message
209 * from within interrupt context, use usb_submit_urb() If a thread in your
210 * driver uses this call, make sure your disconnect() method can wait for it to
211 * complete. Since you don't have a handle on the URB used, you can't cancel
212 * the request.
213 *
214 * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT ioctl,
215 * users are forced to abuse this routine by using it to submit URBs for
216 * interrupt endpoints. We will take the liberty of creating an interrupt URB
217 * (with the default interval) if the target is an interrupt endpoint.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200218 *
219 * Return:
220 * If successful, 0. Otherwise a negative error number. The number of actual
221 * bytes transferred will be stored in the @actual_length paramater.
222 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800224int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
225 void *data, int len, int *actual_length, int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 struct urb *urb;
Alan Sternd09d36a2005-09-26 16:22:45 -0400228 struct usb_host_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Matthew Wilcoxfe54b052010-04-30 13:11:29 -0600230 ep = usb_pipe_endpoint(usb_dev, pipe);
Alan Sternd09d36a2005-09-26 16:22:45 -0400231 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--)
Tülin İzer085528e2013-05-17 10:32:35 +0300258 usb_free_urb(io->urbs[io->entries]);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800259 kfree(io->urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 io->urbs = NULL;
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 io->dev = NULL;
263}
264
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800265static void sg_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800267 struct usb_sg_request *io = urb->context;
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700268 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800270 spin_lock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 /* In 2.5 we require hcds' endpoint queues not to progress after fault
273 * reports, until the completion callback (this!) returns. That lets
274 * device driver code (like this routine) unlink queued urbs first,
275 * if it needs to, since the HC won't work on them at all. So it's
276 * not possible for page N+1 to overwrite page N, and so on.
277 *
278 * That's only for "hard" faults; "soft" faults (unlinks) sometimes
279 * complete before the HCD can get requests away from hardware,
280 * though never during cleanup after a hard fault.
281 */
282 if (io->status
283 && (io->status != -ECONNRESET
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700284 || status != -ECONNRESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 && urb->actual_length) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800286 dev_err(io->dev->bus->controller,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 "dev %s ep%d%s scatterlist error %d/%d\n",
288 io->dev->devpath,
Alan Stern5e60a162007-07-30 17:07:21 -0400289 usb_endpoint_num(&urb->ep->desc),
290 usb_urb_dir_in(urb) ? "in" : "out",
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700291 status, io->status);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800292 /* BUG (); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700295 if (io->status == 0 && status && status != -ECONNRESET) {
296 int i, found, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700298 io->status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /* the previous urbs, and this one, completed already.
301 * unlink pending urbs so they won't rx/tx bad data.
302 * careful: unlink can sometimes be synchronous...
303 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800304 spin_unlock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 for (i = 0, found = 0; i < io->entries; i++) {
Tülin İzer085528e2013-05-17 10:32:35 +0300306 if (!io->urbs[i] || !io->urbs[i]->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 continue;
308 if (found) {
Tülin İzer085528e2013-05-17 10:32:35 +0300309 retval = usb_unlink_urb(io->urbs[i]);
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700310 if (retval != -EINPROGRESS &&
311 retval != -ENODEV &&
Alan Sternbcf39852012-03-22 11:00:21 -0400312 retval != -EBUSY &&
313 retval != -EIDRM)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800314 dev_err(&io->dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 "%s, unlink --> %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800316 __func__, retval);
Tülin İzer085528e2013-05-17 10:32:35 +0300317 } else if (urb == io->urbs[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 found = 1;
319 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800320 spin_lock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /* on the last completion, signal usb_sg_wait() */
324 io->bytes += urb->actual_length;
325 io->count--;
326 if (!io->count)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800327 complete(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800329 spin_unlock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332
333/**
334 * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request
335 * @io: request block being initialized. until usb_sg_wait() returns,
336 * treat this as a pointer to an opaque block of memory,
337 * @dev: the usb device that will send or receive the data
338 * @pipe: endpoint "pipe" used to transfer the data
339 * @period: polling rate for interrupt endpoints, in frames or
340 * (for high speed endpoints) microframes; ignored for bulk
341 * @sg: scatterlist entries
342 * @nents: how many entries in the scatterlist
343 * @length: how many bytes to send from the scatterlist, or zero to
344 * send every byte identified in the list.
345 * @mem_flags: SLAB_* flags affecting memory allocations in this call
346 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200347 * This initializes a scatter/gather request, allocating resources such as
348 * I/O mappings and urb memory (except maybe memory used by USB controller
349 * drivers).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 *
351 * The request must be issued using usb_sg_wait(), which waits for the I/O to
352 * complete (or to be canceled) and then cleans up all resources allocated by
353 * usb_sg_init().
354 *
355 * The request may be canceled with usb_sg_cancel(), either before or after
356 * usb_sg_wait() is called.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200357 *
358 * Return: Zero for success, else a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800360int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
361 unsigned pipe, unsigned period, struct scatterlist *sg,
362 int nents, size_t length, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800364 int i;
365 int urb_flags;
Sarah Sharpe04748e2009-04-27 19:59:01 -0700366 int use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 if (!io || !dev || !sg
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800369 || usb_pipecontrol(pipe)
370 || usb_pipeisoc(pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 || nents <= 0)
372 return -EINVAL;
373
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800374 spin_lock_init(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 io->dev = dev;
376 io->pipe = pipe;
Alan Stern0ba169af2010-05-05 15:26:17 -0400377
378 if (dev->bus->sg_tablesize > 0) {
379 use_sg = true;
380 io->entries = 1;
381 } else {
382 use_sg = false;
383 io->entries = nents;
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 /* initialize all the urbs we'll use */
Tülin İzera1fefaa2013-05-17 10:33:05 +0300387 io->urbs = kmalloc(io->entries * sizeof(*io->urbs), mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (!io->urbs)
389 goto nomem;
390
Alan Stern0ba169af2010-05-05 15:26:17 -0400391 urb_flags = URB_NO_INTERRUPT;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800392 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 urb_flags |= URB_SHORT_NOT_OK;
394
Alan Stern0ba169af2010-05-05 15:26:17 -0400395 for_each_sg(sg, sg, io->entries, i) {
396 struct urb *urb;
397 unsigned len;
398
399 urb = usb_alloc_urb(0, mem_flags);
400 if (!urb) {
401 io->entries = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 goto nomem;
403 }
Alan Stern0ba169af2010-05-05 15:26:17 -0400404 io->urbs[i] = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Alan Stern0ba169af2010-05-05 15:26:17 -0400406 urb->dev = NULL;
407 urb->pipe = pipe;
408 urb->interval = period;
409 urb->transfer_flags = urb_flags;
410 urb->complete = sg_complete;
411 urb->context = io;
412 urb->sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Alan Stern0ba169af2010-05-05 15:26:17 -0400414 if (use_sg) {
415 /* There is no single transfer buffer */
416 urb->transfer_buffer = NULL;
417 urb->num_sgs = nents;
Alan Sternff9c8952010-04-02 13:27:28 -0400418
Alan Stern0ba169af2010-05-05 15:26:17 -0400419 /* A length of zero means transfer the whole sg list */
420 len = length;
421 if (len == 0) {
Alan Stern64d65872010-06-18 10:16:33 -0400422 struct scatterlist *sg2;
423 int j;
424
425 for_each_sg(sg, sg2, nents, j)
426 len += sg2->length;
Sarah Sharpe04748e2009-04-27 19:59:01 -0700427 }
Alan Stern0ba169af2010-05-05 15:26:17 -0400428 } else {
Sarah Sharpe04748e2009-04-27 19:59:01 -0700429 /*
Alan Sternff9c8952010-04-02 13:27:28 -0400430 * Some systems can't use DMA; they use PIO instead.
431 * For their sakes, transfer_buffer is set whenever
432 * possible.
Sarah Sharpe04748e2009-04-27 19:59:01 -0700433 */
Alan Sternff9c8952010-04-02 13:27:28 -0400434 if (!PageHighMem(sg_page(sg)))
Alan Stern0ba169af2010-05-05 15:26:17 -0400435 urb->transfer_buffer = sg_virt(sg);
Pete Zaitcev81bf46f2009-06-11 08:40:39 -0600436 else
Alan Stern0ba169af2010-05-05 15:26:17 -0400437 urb->transfer_buffer = NULL;
Pete Zaitcev81bf46f2009-06-11 08:40:39 -0600438
Alan Sternff9c8952010-04-02 13:27:28 -0400439 len = sg->length;
Sarah Sharpe04748e2009-04-27 19:59:01 -0700440 if (length) {
Dan Carpenteredb2b252011-09-27 09:25:19 +0300441 len = min_t(size_t, len, length);
Sarah Sharpe04748e2009-04-27 19:59:01 -0700442 length -= len;
443 if (length == 0)
444 io->entries = i + 1;
445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Alan Stern0ba169af2010-05-05 15:26:17 -0400447 urb->transfer_buffer_length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
Alan Stern0ba169af2010-05-05 15:26:17 -0400449 io->urbs[--i]->transfer_flags &= ~URB_NO_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 /* transaction state */
Alan Stern580da342008-08-05 13:05:17 -0400452 io->count = io->entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 io->status = 0;
454 io->bytes = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800455 init_completion(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return 0;
457
458nomem:
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800459 sg_clean(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return -ENOMEM;
461}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600462EXPORT_SYMBOL_GPL(usb_sg_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464/**
465 * usb_sg_wait - synchronously execute scatter/gather request
466 * @io: request block handle, as initialized with usb_sg_init().
467 * some fields become accessible when this call returns.
468 * Context: !in_interrupt ()
469 *
470 * This function blocks until the specified I/O operation completes. It
471 * leverages the grouping of the related I/O requests to get good transfer
472 * rates, by queueing the requests. At higher speeds, such queuing can
473 * significantly improve USB throughput.
474 *
475 * There are three kinds of completion for this function.
476 * (1) success, where io->status is zero. The number of io->bytes
477 * transferred is as requested.
478 * (2) error, where io->status is a negative errno value. The number
479 * of io->bytes transferred before the error is usually less
480 * than requested, and can be nonzero.
Steven Cole093cf722005-05-03 19:07:24 -0600481 * (3) cancellation, a type of error with status -ECONNRESET that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 * is initiated by usb_sg_cancel().
483 *
484 * When this function returns, all memory allocated through usb_sg_init() or
485 * this call will have been freed. The request block parameter may still be
486 * passed to usb_sg_cancel(), or it may be freed. It could also be
487 * reinitialized and then reused.
488 *
489 * Data Transfer Rates:
490 *
491 * Bulk transfers are valid for full or high speed endpoints.
492 * The best full speed data rate is 19 packets of 64 bytes each
493 * per frame, or 1216 bytes per millisecond.
494 * The best high speed data rate is 13 packets of 512 bytes each
495 * per microframe, or 52 KBytes per millisecond.
496 *
497 * The reason to use interrupt transfers through this API would most likely
498 * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
499 * could be transferred. That capability is less useful for low or full
500 * speed interrupt endpoints, which allow at most one packet per millisecond,
501 * of at most 8 or 64 bytes (respectively).
Sarah Sharp79abb1a2009-04-27 19:58:26 -0700502 *
503 * It is not necessary to call this function to reserve bandwidth for devices
504 * under an xHCI host controller, as the bandwidth is reserved when the
505 * configuration or interface alt setting is selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800507void usb_sg_wait(struct usb_sg_request *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800509 int i;
510 int entries = io->entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 /* queue the urbs. */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800513 spin_lock_irq(&io->lock);
Alan Stern8ccef0d2007-06-21 16:26:46 -0400514 i = 0;
515 while (i < entries && !io->status) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800516 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800518 io->urbs[i]->dev = io->dev;
Tülin İzer085528e2013-05-17 10:32:35 +0300519 retval = usb_submit_urb(io->urbs[i], GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 /* after we submit, let completions or cancelations fire;
522 * we handshake using io->status.
523 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800524 spin_unlock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 switch (retval) {
526 /* maybe we retrying will recover */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800527 case -ENXIO: /* hc didn't queue this one */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 case -EAGAIN:
529 case -ENOMEM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 retval = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800531 yield();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 break;
533
534 /* no error? continue immediately.
535 *
536 * NOTE: to work better with UHCI (4K I/O buffer may
537 * need 3K of TDs) it may be good to limit how many
538 * URBs are queued at once; N milliseconds?
539 */
540 case 0:
Alan Stern8ccef0d2007-06-21 16:26:46 -0400541 ++i;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800542 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 break;
544
545 /* fail any uncompleted urbs */
546 default:
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800547 io->urbs[i]->status = retval;
548 dev_dbg(&io->dev->dev, "%s, submit --> %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800549 __func__, retval);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800550 usb_sg_cancel(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800552 spin_lock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (retval && (io->status == 0 || io->status == -ECONNRESET))
554 io->status = retval;
555 }
556 io->count -= entries - i;
557 if (io->count == 0)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800558 complete(&io->complete);
559 spin_unlock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 /* OK, yes, this could be packaged as non-blocking.
562 * So could the submit loop above ... but it's easier to
563 * solve neither problem than to solve both!
564 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800565 wait_for_completion(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800567 sg_clean(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600569EXPORT_SYMBOL_GPL(usb_sg_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571/**
572 * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
573 * @io: request block, initialized with usb_sg_init()
574 *
575 * This stops a request after it has been started by usb_sg_wait().
576 * It can also prevents one initialized by usb_sg_init() from starting,
577 * so that call just frees resources allocated to the request.
578 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800579void usb_sg_cancel(struct usb_sg_request *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800581 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800583 spin_lock_irqsave(&io->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /* shut everything down, if it didn't already */
586 if (!io->status) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800587 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 io->status = -ECONNRESET;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800590 spin_unlock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 for (i = 0; i < io->entries; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800592 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Tülin İzer085528e2013-05-17 10:32:35 +0300594 if (!io->urbs[i]->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 continue;
Tülin İzer085528e2013-05-17 10:32:35 +0300596 retval = usb_unlink_urb(io->urbs[i]);
Alan Sternbcf39852012-03-22 11:00:21 -0400597 if (retval != -EINPROGRESS
598 && retval != -ENODEV
599 && retval != -EBUSY
600 && retval != -EIDRM)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800601 dev_warn(&io->dev->dev, "%s, unlink --> %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800602 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800604 spin_lock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800606 spin_unlock_irqrestore(&io->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600608EXPORT_SYMBOL_GPL(usb_sg_cancel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610/*-------------------------------------------------------------------*/
611
612/**
613 * usb_get_descriptor - issues a generic GET_DESCRIPTOR request
614 * @dev: the device whose descriptor is being retrieved
615 * @type: the descriptor type (USB_DT_*)
616 * @index: the number of the descriptor
617 * @buf: where to put the descriptor
618 * @size: how big is "buf"?
619 * Context: !in_interrupt ()
620 *
621 * Gets a USB descriptor. Convenience functions exist to simplify
622 * getting some types of descriptors. Use
623 * usb_get_string() or usb_string() for USB_DT_STRING.
624 * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG)
625 * are part of the device structure.
626 * In addition to a number of USB-standard descriptors, some
627 * devices also use class-specific or vendor-specific descriptors.
628 *
629 * This call is synchronous, and may not be used in an interrupt context.
630 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200631 * Return: The number of bytes received on success, or else the status code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 * returned by the underlying usb_control_msg() call.
633 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800634int usb_get_descriptor(struct usb_device *dev, unsigned char type,
635 unsigned char index, void *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 int i;
638 int result;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800639
640 memset(buf, 0, size); /* Make sure we parse really received data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 for (i = 0; i < 3; ++i) {
Alan Sternc39772d2007-08-20 10:45:28 -0400643 /* retry on length 0 or error; some devices are flakey */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
645 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
646 (type << 8) + index, 0, buf, size,
647 USB_CTRL_GET_TIMEOUT);
Alan Sternc39772d2007-08-20 10:45:28 -0400648 if (result <= 0 && result != -ETIMEDOUT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 continue;
650 if (result > 1 && ((u8 *)buf)[1] != type) {
Alan Stern67f5a4b2009-02-20 16:33:08 -0500651 result = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 continue;
653 }
654 break;
655 }
656 return result;
657}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600658EXPORT_SYMBOL_GPL(usb_get_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660/**
661 * usb_get_string - gets a string descriptor
662 * @dev: the device whose string descriptor is being retrieved
663 * @langid: code for language chosen (from string descriptor zero)
664 * @index: the number of the descriptor
665 * @buf: where to put the string
666 * @size: how big is "buf"?
667 * Context: !in_interrupt ()
668 *
669 * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character,
670 * in little-endian byte order).
671 * The usb_string() function will often be a convenient way to turn
672 * these strings into kernel-printable form.
673 *
674 * Strings may be referenced in device, configuration, interface, or other
675 * descriptors, and could also be used in vendor-specific ways.
676 *
677 * This call is synchronous, and may not be used in an interrupt context.
678 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200679 * Return: The number of bytes received on success, or else the status code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * returned by the underlying usb_control_msg() call.
681 */
Adrian Bunke266a122005-11-08 21:05:43 +0100682static int usb_get_string(struct usb_device *dev, unsigned short langid,
683 unsigned char index, void *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 int i;
686 int result;
687
688 for (i = 0; i < 3; ++i) {
689 /* retry on length 0 or stall; some devices are flakey */
690 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
691 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
692 (USB_DT_STRING << 8) + index, langid, buf, size,
693 USB_CTRL_GET_TIMEOUT);
Alan Stern67f5a4b2009-02-20 16:33:08 -0500694 if (result == 0 || result == -EPIPE)
695 continue;
696 if (result > 1 && ((u8 *) buf)[1] != USB_DT_STRING) {
697 result = -ENODATA;
698 continue;
699 }
700 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702 return result;
703}
704
705static void usb_try_string_workarounds(unsigned char *buf, int *length)
706{
707 int newlength, oldlength = *length;
708
709 for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
710 if (!isprint(buf[newlength]) || buf[newlength + 1])
711 break;
712
713 if (newlength > 2) {
714 buf[0] = newlength;
715 *length = newlength;
716 }
717}
718
719static int usb_string_sub(struct usb_device *dev, unsigned int langid,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800720 unsigned int index, unsigned char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 int rc;
723
724 /* Try to read the string descriptor by asking for the maximum
725 * possible number of bytes */
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100726 if (dev->quirks & USB_QUIRK_STRING_FETCH_255)
727 rc = -EIO;
728 else
729 rc = usb_get_string(dev, langid, index, buf, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 /* If that failed try to read the descriptor length, then
732 * ask for just that many bytes */
733 if (rc < 2) {
734 rc = usb_get_string(dev, langid, index, buf, 2);
735 if (rc == 2)
736 rc = usb_get_string(dev, langid, index, buf, buf[0]);
737 }
738
739 if (rc >= 2) {
740 if (!buf[0] && !buf[1])
741 usb_try_string_workarounds(buf, &rc);
742
743 /* There might be extra junk at the end of the descriptor */
744 if (buf[0] < rc)
745 rc = buf[0];
746
747 rc = rc - (rc & 1); /* force a multiple of two */
748 }
749
750 if (rc < 2)
751 rc = (rc < 0 ? rc : -EINVAL);
752
753 return rc;
754}
755
Daniel Mack0cce2ed2009-07-10 11:04:58 +0200756static int usb_get_langid(struct usb_device *dev, unsigned char *tbuf)
757{
758 int err;
759
760 if (dev->have_langid)
761 return 0;
762
763 if (dev->string_langid < 0)
764 return -EPIPE;
765
766 err = usb_string_sub(dev, 0, 0, tbuf);
767
768 /* If the string was reported but is malformed, default to english
769 * (0x0409) */
770 if (err == -ENODATA || (err > 0 && err < 4)) {
771 dev->string_langid = 0x0409;
772 dev->have_langid = 1;
773 dev_err(&dev->dev,
774 "string descriptor 0 malformed (err = %d), "
775 "defaulting to 0x%04x\n",
776 err, dev->string_langid);
777 return 0;
778 }
779
780 /* In case of all other errors, we assume the device is not able to
781 * deal with strings at all. Set string_langid to -1 in order to
782 * prevent any string to be retrieved from the device */
783 if (err < 0) {
784 dev_err(&dev->dev, "string descriptor 0 read error: %d\n",
785 err);
786 dev->string_langid = -1;
787 return -EPIPE;
788 }
789
790 /* always use the first langid listed */
791 dev->string_langid = tbuf[2] | (tbuf[3] << 8);
792 dev->have_langid = 1;
793 dev_dbg(&dev->dev, "default language 0x%04x\n",
794 dev->string_langid);
795 return 0;
796}
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798/**
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200799 * usb_string - returns UTF-8 version of a string descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 * @dev: the device whose string descriptor is being retrieved
801 * @index: the number of the descriptor
802 * @buf: where to put the string
803 * @size: how big is "buf"?
804 * Context: !in_interrupt ()
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800805 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 * This converts the UTF-16LE encoded strings returned by devices, from
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200807 * usb_get_string_descriptor(), to null-terminated UTF-8 encoded ones
808 * that are more usable in most kernel contexts. Note that this function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 * chooses strings in the first language supported by the device.
810 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 * This call is synchronous, and may not be used in an interrupt context.
812 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200813 * Return: length of the string (>= 0) or usb_control_msg status (< 0).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 */
815int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
816{
817 unsigned char *tbuf;
818 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (dev->state == USB_STATE_SUSPENDED)
821 return -EHOSTUNREACH;
822 if (size <= 0 || !buf || !index)
823 return -EINVAL;
824 buf[0] = 0;
Alan Stern74675a52009-04-30 10:08:18 -0400825 tbuf = kmalloc(256, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (!tbuf)
827 return -ENOMEM;
828
Daniel Mack0cce2ed2009-07-10 11:04:58 +0200829 err = usb_get_langid(dev, tbuf);
830 if (err < 0)
831 goto errout;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 err = usb_string_sub(dev, dev->string_langid, index, tbuf);
834 if (err < 0)
835 goto errout;
836
837 size--; /* leave room for trailing NULL char in output buffer */
Alan Stern74675a52009-04-30 10:08:18 -0400838 err = utf16s_to_utf8s((wchar_t *) &tbuf[2], (err - 2) / 2,
839 UTF16_LITTLE_ENDIAN, buf, size);
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200840 buf[err] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 if (tbuf[1] != USB_DT_STRING)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800843 dev_dbg(&dev->dev,
844 "wrong descriptor type %02x for string %d (\"%s\")\n",
845 tbuf[1], index, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 errout:
848 kfree(tbuf);
849 return err;
850}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600851EXPORT_SYMBOL_GPL(usb_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200853/* one UTF-8-encoded 16-bit character has at most three bytes */
854#define MAX_USB_STRING_SIZE (127 * 3 + 1)
855
Alan Stern4f62efe2005-10-24 16:24:14 -0400856/**
857 * usb_cache_string - read a string descriptor and cache it for later use
858 * @udev: the device whose string descriptor is being read
859 * @index: the descriptor index
860 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200861 * Return: A pointer to a kmalloc'ed buffer containing the descriptor string,
862 * or %NULL if the index is 0 or the string could not be read.
Alan Stern4f62efe2005-10-24 16:24:14 -0400863 */
864char *usb_cache_string(struct usb_device *udev, int index)
865{
866 char *buf;
867 char *smallbuf = NULL;
868 int len;
869
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800870 if (index <= 0)
871 return NULL;
872
Oliver Neukumacbe2fe2010-01-12 12:32:50 +0100873 buf = kmalloc(MAX_USB_STRING_SIZE, GFP_NOIO);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800874 if (buf) {
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200875 len = usb_string(udev, index, buf, MAX_USB_STRING_SIZE);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800876 if (len > 0) {
Oliver Neukumacbe2fe2010-01-12 12:32:50 +0100877 smallbuf = kmalloc(++len, GFP_NOIO);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800878 if (!smallbuf)
Alan Stern4f62efe2005-10-24 16:24:14 -0400879 return buf;
880 memcpy(smallbuf, buf, len);
881 }
882 kfree(buf);
883 }
884 return smallbuf;
885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887/*
888 * usb_get_device_descriptor - (re)reads the device descriptor (usbcore)
889 * @dev: the device whose device descriptor is being updated
890 * @size: how much of the descriptor to read
891 * Context: !in_interrupt ()
892 *
893 * Updates the copy of the device descriptor stored in the device structure,
Laurent Pinchart6ab16a92006-11-07 10:16:25 +0100894 * which dedicates space for this purpose.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 *
896 * Not exported, only for use by the core. If drivers really want to read
897 * the device descriptor directly, they can call usb_get_descriptor() with
898 * type = USB_DT_DEVICE and index = 0.
899 *
900 * This call is synchronous, and may not be used in an interrupt context.
901 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200902 * Return: The number of bytes received on success, or else the status code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 * returned by the underlying usb_control_msg() call.
904 */
905int usb_get_device_descriptor(struct usb_device *dev, unsigned int size)
906{
907 struct usb_device_descriptor *desc;
908 int ret;
909
910 if (size > sizeof(*desc))
911 return -EINVAL;
912 desc = kmalloc(sizeof(*desc), GFP_NOIO);
913 if (!desc)
914 return -ENOMEM;
915
916 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800917 if (ret >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 memcpy(&dev->descriptor, desc, size);
919 kfree(desc);
920 return ret;
921}
922
923/**
924 * usb_get_status - issues a GET_STATUS call
925 * @dev: the device whose status is being checked
926 * @type: USB_RECIP_*; for device, interface, or endpoint
927 * @target: zero (for device), else interface or endpoint number
928 * @data: pointer to two bytes of bitmap data
929 * Context: !in_interrupt ()
930 *
931 * Returns device, interface, or endpoint status. Normally only of
932 * interest to see if the device is self powered, or has enabled the
933 * remote wakeup facility; or whether a bulk or interrupt endpoint
934 * is halted ("stalled").
935 *
936 * Bits in these status bitmaps are set using the SET_FEATURE request,
937 * and cleared using the CLEAR_FEATURE request. The usb_clear_halt()
938 * function should be used to clear halt ("stall") status.
939 *
940 * This call is synchronous, and may not be used in an interrupt context.
941 *
Alan Stern15b73362013-07-30 15:35:40 -0400942 * Returns 0 and the status value in *@data (in host byte order) on success,
943 * or else the status code from the underlying usb_control_msg() call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 */
945int usb_get_status(struct usb_device *dev, int type, int target, void *data)
946{
947 int ret;
Alan Stern15b73362013-07-30 15:35:40 -0400948 __le16 *status = kmalloc(sizeof(*status), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 if (!status)
951 return -ENOMEM;
952
953 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
954 USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status,
955 sizeof(*status), USB_CTRL_GET_TIMEOUT);
956
Alan Stern15b73362013-07-30 15:35:40 -0400957 if (ret == 2) {
958 *(u16 *) data = le16_to_cpu(*status);
959 ret = 0;
960 } else if (ret >= 0) {
961 ret = -EIO;
962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 kfree(status);
964 return ret;
965}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600966EXPORT_SYMBOL_GPL(usb_get_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968/**
969 * usb_clear_halt - tells device to clear endpoint halt/stall condition
970 * @dev: device whose endpoint is halted
971 * @pipe: endpoint "pipe" being cleared
972 * Context: !in_interrupt ()
973 *
974 * This is used to clear halt conditions for bulk and interrupt endpoints,
975 * as reported by URB completion status. Endpoints that are halted are
976 * sometimes referred to as being "stalled". Such endpoints are unable
977 * to transmit or receive data until the halt status is cleared. Any URBs
978 * queued for such an endpoint should normally be unlinked by the driver
979 * before clearing the halt condition, as described in sections 5.7.5
980 * and 5.8.5 of the USB 2.0 spec.
981 *
982 * Note that control and isochronous endpoints don't halt, although control
983 * endpoints report "protocol stall" (for unsupported requests) using the
984 * same status code used to report a true stall.
985 *
986 * This call is synchronous, and may not be used in an interrupt context.
987 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200988 * Return: Zero on success, or else the status code returned by the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 * underlying usb_control_msg() call.
990 */
991int usb_clear_halt(struct usb_device *dev, int pipe)
992{
993 int result;
994 int endp = usb_pipeendpoint(pipe);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800995
996 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 endp |= USB_DIR_IN;
998
999 /* we don't care if it wasn't halted first. in fact some devices
1000 * (like some ibmcam model 1 units) seem to expect hosts to make
1001 * this request for iso endpoints, which can't halt!
1002 */
1003 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1004 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
1005 USB_ENDPOINT_HALT, endp, NULL, 0,
1006 USB_CTRL_SET_TIMEOUT);
1007
1008 /* don't un-halt or force to DATA0 except on success */
1009 if (result < 0)
1010 return result;
1011
1012 /* NOTE: seems like Microsoft and Apple don't bother verifying
1013 * the clear "took", so some devices could lock up if you check...
1014 * such as the Hagiwara FlashGate DUAL. So we won't bother.
1015 *
1016 * NOTE: make sure the logic here doesn't diverge much from
1017 * the copy in usb-storage, for as long as we need two copies.
1018 */
1019
David Vrabel3444b262009-04-08 17:36:28 +00001020 usb_reset_endpoint(dev, endp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 return 0;
1023}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001024EXPORT_SYMBOL_GPL(usb_clear_halt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Alan Stern3b23dd62008-12-05 14:10:34 -05001026static int create_intf_ep_devs(struct usb_interface *intf)
1027{
1028 struct usb_device *udev = interface_to_usbdev(intf);
1029 struct usb_host_interface *alt = intf->cur_altsetting;
1030 int i;
1031
1032 if (intf->ep_devs_created || intf->unregistering)
1033 return 0;
1034
1035 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1036 (void) usb_create_ep_devs(&intf->dev, &alt->endpoint[i], udev);
1037 intf->ep_devs_created = 1;
1038 return 0;
1039}
1040
1041static void remove_intf_ep_devs(struct usb_interface *intf)
1042{
1043 struct usb_host_interface *alt = intf->cur_altsetting;
1044 int i;
1045
1046 if (!intf->ep_devs_created)
1047 return;
1048
1049 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1050 usb_remove_ep_devs(&alt->endpoint[i]);
1051 intf->ep_devs_created = 0;
1052}
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054/**
1055 * usb_disable_endpoint -- Disable an endpoint by address
1056 * @dev: the device whose endpoint is being disabled
1057 * @epaddr: the endpoint's address. Endpoint number for output,
1058 * endpoint number + USB_DIR_IN for input
Alan Sternddeac4e72009-01-15 17:03:33 -05001059 * @reset_hardware: flag to erase any endpoint state stored in the
1060 * controller hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 *
Alan Sternddeac4e72009-01-15 17:03:33 -05001062 * Disables the endpoint for URB submission and nukes all pending URBs.
1063 * If @reset_hardware is set then also deallocates hcd/hardware state
1064 * for the endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 */
Alan Sternddeac4e72009-01-15 17:03:33 -05001066void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr,
1067 bool reset_hardware)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1070 struct usb_host_endpoint *ep;
1071
1072 if (!dev)
1073 return;
1074
1075 if (usb_endpoint_out(epaddr)) {
1076 ep = dev->ep_out[epnum];
Alan Sternddeac4e72009-01-15 17:03:33 -05001077 if (reset_hardware)
1078 dev->ep_out[epnum] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 } else {
1080 ep = dev->ep_in[epnum];
Alan Sternddeac4e72009-01-15 17:03:33 -05001081 if (reset_hardware)
1082 dev->ep_in[epnum] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
Alan Sternbdd016b2007-07-30 17:05:22 -04001084 if (ep) {
1085 ep->enabled = 0;
Alan Stern95cf82f2007-09-10 11:33:05 -04001086 usb_hcd_flush_endpoint(dev, ep);
Alan Sternddeac4e72009-01-15 17:03:33 -05001087 if (reset_hardware)
1088 usb_hcd_disable_endpoint(dev, ep);
Alan Sternbdd016b2007-07-30 17:05:22 -04001089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092/**
David Vrabel3444b262009-04-08 17:36:28 +00001093 * usb_reset_endpoint - Reset an endpoint's state.
1094 * @dev: the device whose endpoint is to be reset
1095 * @epaddr: the endpoint's address. Endpoint number for output,
1096 * endpoint number + USB_DIR_IN for input
1097 *
1098 * Resets any host-side endpoint state such as the toggle bit,
1099 * sequence number or current window.
1100 */
1101void usb_reset_endpoint(struct usb_device *dev, unsigned int epaddr)
1102{
1103 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1104 struct usb_host_endpoint *ep;
1105
1106 if (usb_endpoint_out(epaddr))
1107 ep = dev->ep_out[epnum];
1108 else
1109 ep = dev->ep_in[epnum];
1110 if (ep)
1111 usb_hcd_reset_endpoint(dev, ep);
1112}
1113EXPORT_SYMBOL_GPL(usb_reset_endpoint);
1114
1115
1116/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 * usb_disable_interface -- Disable all endpoints for an interface
1118 * @dev: the device whose interface is being disabled
1119 * @intf: pointer to the interface descriptor
Alan Sternddeac4e72009-01-15 17:03:33 -05001120 * @reset_hardware: flag to erase any endpoint state stored in the
1121 * controller hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 *
1123 * Disables all the endpoints for the interface's current altsetting.
1124 */
Alan Sternddeac4e72009-01-15 17:03:33 -05001125void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf,
1126 bool reset_hardware)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 struct usb_host_interface *alt = intf->cur_altsetting;
1129 int i;
1130
1131 for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
1132 usb_disable_endpoint(dev,
Alan Sternddeac4e72009-01-15 17:03:33 -05001133 alt->endpoint[i].desc.bEndpointAddress,
1134 reset_hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136}
1137
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001138/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 * usb_disable_device - Disable all the endpoints for a USB device
1140 * @dev: the device whose endpoints are being disabled
1141 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
1142 *
1143 * Disables all the device's endpoints, potentially including endpoint 0.
1144 * Deallocates hcd/hardware state for the endpoints (nuking all or most
1145 * pending urbs) and usbcore state for the interfaces, so that usbcore
1146 * must usb_set_configuration() before any interfaces could be used.
1147 */
1148void usb_disable_device(struct usb_device *dev, int skip_ep0)
1149{
1150 int i;
Sarah Sharpfccf4e82011-06-05 23:22:22 -07001151 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /* getting rid of interfaces will disconnect
1154 * any drivers bound to them (a key side effect)
1155 */
1156 if (dev->actconfig) {
Alan Sternca5c4852011-07-06 17:03:45 -04001157 /*
1158 * FIXME: In order to avoid self-deadlock involving the
1159 * bandwidth_mutex, we have to mark all the interfaces
1160 * before unregistering any of them.
1161 */
1162 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++)
1163 dev->actconfig->interface[i]->unregistering = 1;
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1166 struct usb_interface *interface;
1167
Alan Stern86d30742005-07-29 12:17:16 -07001168 /* remove this interface if it has been registered */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 interface = dev->actconfig->interface[i];
Daniel Ritzd305ef52005-09-22 00:47:24 -07001170 if (!device_is_registered(&interface->dev))
Alan Stern86d30742005-07-29 12:17:16 -07001171 continue;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001172 dev_dbg(&dev->dev, "unregistering interface %s\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001173 dev_name(&interface->dev));
Alan Stern3b23dd62008-12-05 14:10:34 -05001174 remove_intf_ep_devs(interface);
Alan Stern1a211752008-07-30 11:31:50 -04001175 device_del(&interface->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177
1178 /* Now that the interfaces are unbound, nobody should
1179 * try to access them.
1180 */
1181 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001182 put_device(&dev->actconfig->interface[i]->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 dev->actconfig->interface[i] = NULL;
1184 }
Sarah Sharp24971912012-07-05 14:09:30 -07001185 usb_unlocked_disable_lpm(dev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07001186 usb_disable_ltm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 dev->actconfig = NULL;
1188 if (dev->state == USB_STATE_CONFIGURED)
1189 usb_set_device_state(dev, USB_STATE_ADDRESS);
1190 }
Alan Stern80f0cf32010-09-30 15:16:23 -04001191
1192 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __func__,
1193 skip_ep0 ? "non-ep0" : "all");
Sarah Sharpfccf4e82011-06-05 23:22:22 -07001194 if (hcd->driver->check_bandwidth) {
1195 /* First pass: Cancel URBs, leave endpoint pointers intact. */
1196 for (i = skip_ep0; i < 16; ++i) {
1197 usb_disable_endpoint(dev, i, false);
1198 usb_disable_endpoint(dev, i + USB_DIR_IN, false);
1199 }
1200 /* Remove endpoints from the host controller internal state */
Alan Stern8963c482012-04-17 15:22:39 -04001201 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharpfccf4e82011-06-05 23:22:22 -07001202 usb_hcd_alloc_bandwidth(dev, NULL, NULL, NULL);
Alan Stern8963c482012-04-17 15:22:39 -04001203 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharpfccf4e82011-06-05 23:22:22 -07001204 /* Second pass: remove endpoint pointers */
1205 }
Alan Stern80f0cf32010-09-30 15:16:23 -04001206 for (i = skip_ep0; i < 16; ++i) {
1207 usb_disable_endpoint(dev, i, true);
1208 usb_disable_endpoint(dev, i + USB_DIR_IN, true);
1209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001212/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 * usb_enable_endpoint - Enable an endpoint for USB communications
1214 * @dev: the device whose interface is being enabled
1215 * @ep: the endpoint
David Vrabel3444b262009-04-08 17:36:28 +00001216 * @reset_ep: flag to reset the endpoint state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 *
David Vrabel3444b262009-04-08 17:36:28 +00001218 * Resets the endpoint state if asked, and sets dev->ep_{in,out} pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 * For control endpoints, both the input and output sides are handled.
1220 */
Alan Stern2caf7fc2008-12-31 11:31:33 -05001221void usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep,
David Vrabel3444b262009-04-08 17:36:28 +00001222 bool reset_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Alan Sternbdd016b2007-07-30 17:05:22 -04001224 int epnum = usb_endpoint_num(&ep->desc);
1225 int is_out = usb_endpoint_dir_out(&ep->desc);
1226 int is_control = usb_endpoint_xfer_control(&ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
David Vrabel3444b262009-04-08 17:36:28 +00001228 if (reset_ep)
1229 usb_hcd_reset_endpoint(dev, ep);
1230 if (is_out || is_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 dev->ep_out[epnum] = ep;
David Vrabel3444b262009-04-08 17:36:28 +00001232 if (!is_out || is_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 dev->ep_in[epnum] = ep;
Alan Sternbdd016b2007-07-30 17:05:22 -04001234 ep->enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001237/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 * usb_enable_interface - Enable all the endpoints for an interface
1239 * @dev: the device whose interface is being enabled
1240 * @intf: pointer to the interface descriptor
David Vrabel3444b262009-04-08 17:36:28 +00001241 * @reset_eps: flag to reset the endpoints' state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 *
1243 * Enables all the endpoints for the interface's current altsetting.
1244 */
Alan Stern2caf7fc2008-12-31 11:31:33 -05001245void usb_enable_interface(struct usb_device *dev,
David Vrabel3444b262009-04-08 17:36:28 +00001246 struct usb_interface *intf, bool reset_eps)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
1248 struct usb_host_interface *alt = intf->cur_altsetting;
1249 int i;
1250
1251 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
David Vrabel3444b262009-04-08 17:36:28 +00001252 usb_enable_endpoint(dev, &alt->endpoint[i], reset_eps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
1255/**
1256 * usb_set_interface - Makes a particular alternate setting be current
1257 * @dev: the device whose interface is being updated
1258 * @interface: the interface being updated
1259 * @alternate: the setting being chosen.
1260 * Context: !in_interrupt ()
1261 *
1262 * This is used to enable data transfers on interfaces that may not
1263 * be enabled by default. Not all devices support such configurability.
1264 * Only the driver bound to an interface may change its setting.
1265 *
1266 * Within any given configuration, each interface may have several
1267 * alternative settings. These are often used to control levels of
1268 * bandwidth consumption. For example, the default setting for a high
1269 * speed interrupt endpoint may not send more than 64 bytes per microframe,
1270 * while interrupt transfers of up to 3KBytes per microframe are legal.
1271 * Also, isochronous endpoints may never be part of an
1272 * interface's default setting. To access such bandwidth, alternate
1273 * interface settings must be made current.
1274 *
1275 * Note that in the Linux USB subsystem, bandwidth associated with
1276 * an endpoint in a given alternate setting is not reserved until an URB
1277 * is submitted that needs that bandwidth. Some other operating systems
1278 * allocate bandwidth early, when a configuration is chosen.
1279 *
1280 * This call is synchronous, and may not be used in an interrupt context.
1281 * Also, drivers must not change altsettings while urbs are scheduled for
1282 * endpoints in that interface; all such urbs must first be completed
1283 * (perhaps forced by unlinking).
1284 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02001285 * Return: Zero on success, or else the status code returned by the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 * underlying usb_control_msg() call.
1287 */
1288int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1289{
1290 struct usb_interface *iface;
1291 struct usb_host_interface *alt;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001292 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 int ret;
1294 int manual = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001295 unsigned int epaddr;
1296 unsigned int pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 if (dev->state == USB_STATE_SUSPENDED)
1299 return -EHOSTUNREACH;
1300
1301 iface = usb_ifnum_to_if(dev, interface);
1302 if (!iface) {
1303 dev_dbg(&dev->dev, "selecting invalid interface %d\n",
1304 interface);
1305 return -EINVAL;
1306 }
Alan Sterne534c5b2011-07-01 16:43:02 -04001307 if (iface->unregistering)
1308 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 alt = usb_altnum_to_altsetting(iface, alternate);
1311 if (!alt) {
Thadeu Lima de Souza Cascardo385f6902010-01-17 19:24:03 -02001312 dev_warn(&dev->dev, "selecting invalid altsetting %d\n",
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -07001313 alternate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return -EINVAL;
1315 }
1316
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001317 /* Make sure we have enough bandwidth for this alternate interface.
1318 * Remove the current alt setting and add the new alt setting.
1319 */
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001320 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp83060952012-05-02 14:25:52 -07001321 /* Disable LPM, and re-enable it once the new alt setting is installed,
1322 * so that the xHCI driver can recalculate the U1/U2 timeouts.
1323 */
1324 if (usb_disable_lpm(dev)) {
1325 dev_err(&iface->dev, "%s Failed to disable LPM\n.", __func__);
1326 mutex_unlock(hcd->bandwidth_mutex);
1327 return -ENOMEM;
1328 }
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001329 ret = usb_hcd_alloc_bandwidth(dev, NULL, iface->cur_altsetting, alt);
1330 if (ret < 0) {
1331 dev_info(&dev->dev, "Not enough bandwidth for altsetting %d\n",
1332 alternate);
Sarah Sharp83060952012-05-02 14:25:52 -07001333 usb_enable_lpm(dev);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001334 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001335 return ret;
1336 }
1337
Alan Stern392e1d92008-03-11 10:20:12 -04001338 if (dev->quirks & USB_QUIRK_NO_SET_INTF)
1339 ret = -EPIPE;
1340 else
1341 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1343 alternate, interface, NULL, 0, 5000);
1344
1345 /* 9.4.10 says devices don't need this and are free to STALL the
1346 * request if the interface only has one alternate setting.
1347 */
1348 if (ret == -EPIPE && iface->num_altsetting == 1) {
1349 dev_dbg(&dev->dev,
1350 "manual set_interface for iface %d, alt %d\n",
1351 interface, alternate);
1352 manual = 1;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001353 } else if (ret < 0) {
1354 /* Re-instate the old alt setting */
1355 usb_hcd_alloc_bandwidth(dev, NULL, alt, iface->cur_altsetting);
Sarah Sharp83060952012-05-02 14:25:52 -07001356 usb_enable_lpm(dev);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001357 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return ret;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001359 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001360 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 /* FIXME drivers shouldn't need to replicate/bugfix the logic here
1363 * when they implement async or easily-killable versions of this or
1364 * other "should-be-internal" functions (like clear_halt).
1365 * should hcd+usbcore postprocess control requests?
1366 */
1367
1368 /* prevent submissions using previous endpoint settings */
Alan Stern3b23dd62008-12-05 14:10:34 -05001369 if (iface->cur_altsetting != alt) {
1370 remove_intf_ep_devs(iface);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001371 usb_remove_sysfs_intf_files(iface);
Alan Stern3b23dd62008-12-05 14:10:34 -05001372 }
Alan Sternddeac4e72009-01-15 17:03:33 -05001373 usb_disable_interface(dev, iface, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 iface->cur_altsetting = alt;
1376
Sarah Sharp83060952012-05-02 14:25:52 -07001377 /* Now that the interface is installed, re-enable LPM. */
1378 usb_unlocked_enable_lpm(dev);
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 /* If the interface only has one altsetting and the device didn't
David Brownella81e7ec2005-04-18 17:39:25 -07001381 * accept the request, we attempt to carry out the equivalent action
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 * by manually clearing the HALT feature for each endpoint in the
1383 * new altsetting.
1384 */
1385 if (manual) {
1386 int i;
1387
1388 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001389 epaddr = alt->endpoint[i].desc.bEndpointAddress;
1390 pipe = __create_pipe(dev,
1391 USB_ENDPOINT_NUMBER_MASK & epaddr) |
1392 (usb_endpoint_out(epaddr) ?
1393 USB_DIR_OUT : USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 usb_clear_halt(dev, pipe);
1396 }
1397 }
1398
1399 /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
1400 *
1401 * Note:
1402 * Despite EP0 is always present in all interfaces/AS, the list of
1403 * endpoints from the descriptor does not contain EP0. Due to its
1404 * omnipresence one might expect EP0 being considered "affected" by
1405 * any SetInterface request and hence assume toggles need to be reset.
1406 * However, EP0 toggles are re-synced for every individual transfer
1407 * during the SETUP stage - hence EP0 toggles are "don't care" here.
1408 * (Likewise, EP0 never "halts" on well designed devices.)
1409 */
Alan Stern2caf7fc2008-12-31 11:31:33 -05001410 usb_enable_interface(dev, iface, true);
Alan Stern3b23dd62008-12-05 14:10:34 -05001411 if (device_is_registered(&iface->dev)) {
Alan Stern0e6c8e82005-10-24 15:33:03 -04001412 usb_create_sysfs_intf_files(iface);
Alan Stern3b23dd62008-12-05 14:10:34 -05001413 create_intf_ep_devs(iface);
1414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return 0;
1416}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001417EXPORT_SYMBOL_GPL(usb_set_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419/**
1420 * usb_reset_configuration - lightweight device reset
1421 * @dev: the device whose configuration is being reset
1422 *
1423 * This issues a standard SET_CONFIGURATION request to the device using
1424 * the current configuration. The effect is to reset most USB-related
1425 * state in the device, including interface altsettings (reset to zero),
David Vrabel3444b262009-04-08 17:36:28 +00001426 * endpoint halts (cleared), and endpoint state (only for bulk and interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 * endpoints). Other usbcore state is unchanged, including bindings of
1428 * usb device drivers to interfaces.
1429 *
1430 * Because this affects multiple interfaces, avoid using this with composite
1431 * (multi-interface) devices. Instead, the driver for each interface may
David Brownella81e7ec2005-04-18 17:39:25 -07001432 * use usb_set_interface() on the interfaces it claims. Be careful though;
1433 * some devices don't support the SET_INTERFACE request, and others won't
David Vrabel3444b262009-04-08 17:36:28 +00001434 * reset all the interface state (notably endpoint state). Resetting the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 * configuration would affect other drivers' interfaces.
1436 *
1437 * The caller must own the device lock.
1438 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02001439 * Return: Zero on success, else a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 */
1441int usb_reset_configuration(struct usb_device *dev)
1442{
1443 int i, retval;
1444 struct usb_host_config *config;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001445 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 if (dev->state == USB_STATE_SUSPENDED)
1448 return -EHOSTUNREACH;
1449
1450 /* caller must have locked the device and must own
1451 * the usb bus readlock (so driver bindings are stable);
1452 * calls during probe() are fine
1453 */
1454
1455 for (i = 1; i < 16; ++i) {
Alan Sternddeac4e72009-01-15 17:03:33 -05001456 usb_disable_endpoint(dev, i, true);
1457 usb_disable_endpoint(dev, i + USB_DIR_IN, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459
1460 config = dev->actconfig;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001461 retval = 0;
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001462 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp83060952012-05-02 14:25:52 -07001463 /* Disable LPM, and re-enable it once the configuration is reset, so
1464 * that the xHCI driver can recalculate the U1/U2 timeouts.
1465 */
1466 if (usb_disable_lpm(dev)) {
1467 dev_err(&dev->dev, "%s Failed to disable LPM\n.", __func__);
1468 mutex_unlock(hcd->bandwidth_mutex);
1469 return -ENOMEM;
1470 }
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001471 /* Make sure we have enough bandwidth for each alternate setting 0 */
1472 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1473 struct usb_interface *intf = config->interface[i];
1474 struct usb_host_interface *alt;
1475
1476 alt = usb_altnum_to_altsetting(intf, 0);
1477 if (!alt)
1478 alt = &intf->altsetting[0];
1479 if (alt != intf->cur_altsetting)
1480 retval = usb_hcd_alloc_bandwidth(dev, NULL,
1481 intf->cur_altsetting, alt);
1482 if (retval < 0)
1483 break;
1484 }
1485 /* If not, reinstate the old alternate settings */
1486 if (retval < 0) {
1487reset_old_alts:
Roel Kluine4a3d942010-02-18 02:36:23 +01001488 for (i--; i >= 0; i--) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001489 struct usb_interface *intf = config->interface[i];
1490 struct usb_host_interface *alt;
1491
1492 alt = usb_altnum_to_altsetting(intf, 0);
1493 if (!alt)
1494 alt = &intf->altsetting[0];
1495 if (alt != intf->cur_altsetting)
1496 usb_hcd_alloc_bandwidth(dev, NULL,
1497 alt, intf->cur_altsetting);
1498 }
Sarah Sharp83060952012-05-02 14:25:52 -07001499 usb_enable_lpm(dev);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001500 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001501 return retval;
1502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1504 USB_REQ_SET_CONFIGURATION, 0,
1505 config->desc.bConfigurationValue, 0,
1506 NULL, 0, USB_CTRL_SET_TIMEOUT);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001507 if (retval < 0)
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001508 goto reset_old_alts;
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001509 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 /* re-init hc/hcd interface/endpoint state */
1512 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1513 struct usb_interface *intf = config->interface[i];
1514 struct usb_host_interface *alt;
1515
1516 alt = usb_altnum_to_altsetting(intf, 0);
1517
1518 /* No altsetting 0? We'll assume the first altsetting.
1519 * We could use a GetInterface call, but if a device is
1520 * so non-compliant that it doesn't have altsetting 0
1521 * then I wouldn't trust its reply anyway.
1522 */
1523 if (!alt)
1524 alt = &intf->altsetting[0];
1525
Alan Stern3b23dd62008-12-05 14:10:34 -05001526 if (alt != intf->cur_altsetting) {
1527 remove_intf_ep_devs(intf);
1528 usb_remove_sysfs_intf_files(intf);
1529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 intf->cur_altsetting = alt;
Alan Stern2caf7fc2008-12-31 11:31:33 -05001531 usb_enable_interface(dev, intf, true);
Alan Stern3b23dd62008-12-05 14:10:34 -05001532 if (device_is_registered(&intf->dev)) {
Alan Stern0e6c8e82005-10-24 15:33:03 -04001533 usb_create_sysfs_intf_files(intf);
Alan Stern3b23dd62008-12-05 14:10:34 -05001534 create_intf_ep_devs(intf);
1535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
Sarah Sharp83060952012-05-02 14:25:52 -07001537 /* Now that the interfaces are installed, re-enable LPM. */
1538 usb_unlocked_enable_lpm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return 0;
1540}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001541EXPORT_SYMBOL_GPL(usb_reset_configuration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Greg Kroah-Hartmanb0e396e2007-08-02 22:44:27 -06001543static void usb_release_interface(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
1545 struct usb_interface *intf = to_usb_interface(dev);
1546 struct usb_interface_cache *intfc =
1547 altsetting_to_usb_interface_cache(intf->altsetting);
1548
1549 kref_put(&intfc->ref, usb_release_interface_cache);
1550 kfree(intf);
1551}
1552
Kay Sievers7eff2e72007-08-14 15:15:12 +02001553static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001554{
1555 struct usb_device *usb_dev;
1556 struct usb_interface *intf;
1557 struct usb_host_interface *alt;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001558
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001559 intf = to_usb_interface(dev);
1560 usb_dev = interface_to_usbdev(intf);
1561 alt = intf->cur_altsetting;
1562
Kay Sievers7eff2e72007-08-14 15:15:12 +02001563 if (add_uevent_var(env, "INTERFACE=%d/%d/%d",
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001564 alt->desc.bInterfaceClass,
1565 alt->desc.bInterfaceSubClass,
1566 alt->desc.bInterfaceProtocol))
1567 return -ENOMEM;
1568
Kay Sievers7eff2e72007-08-14 15:15:12 +02001569 if (add_uevent_var(env,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001570 "MODALIAS=usb:"
Bjørn Mork81df2d52012-05-18 21:27:43 +02001571 "v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02Xin%02X",
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001572 le16_to_cpu(usb_dev->descriptor.idVendor),
1573 le16_to_cpu(usb_dev->descriptor.idProduct),
1574 le16_to_cpu(usb_dev->descriptor.bcdDevice),
1575 usb_dev->descriptor.bDeviceClass,
1576 usb_dev->descriptor.bDeviceSubClass,
1577 usb_dev->descriptor.bDeviceProtocol,
1578 alt->desc.bInterfaceClass,
1579 alt->desc.bInterfaceSubClass,
Bjørn Mork81df2d52012-05-18 21:27:43 +02001580 alt->desc.bInterfaceProtocol,
1581 alt->desc.bInterfaceNumber))
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001582 return -ENOMEM;
1583
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001584 return 0;
1585}
1586
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001587struct device_type usb_if_device_type = {
1588 .name = "usb_interface",
1589 .release = usb_release_interface,
1590 .uevent = usb_if_uevent,
1591};
1592
Craig W. Nadler165fe972007-06-15 23:14:35 -04001593static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001594 struct usb_host_config *config,
1595 u8 inum)
Craig W. Nadler165fe972007-06-15 23:14:35 -04001596{
1597 struct usb_interface_assoc_descriptor *retval = NULL;
1598 struct usb_interface_assoc_descriptor *intf_assoc;
1599 int first_intf;
1600 int last_intf;
1601 int i;
1602
1603 for (i = 0; (i < USB_MAXIADS && config->intf_assoc[i]); i++) {
1604 intf_assoc = config->intf_assoc[i];
1605 if (intf_assoc->bInterfaceCount == 0)
1606 continue;
1607
1608 first_intf = intf_assoc->bFirstInterface;
1609 last_intf = first_intf + (intf_assoc->bInterfaceCount - 1);
1610 if (inum >= first_intf && inum <= last_intf) {
1611 if (!retval)
1612 retval = intf_assoc;
1613 else
1614 dev_err(&dev->dev, "Interface #%d referenced"
1615 " by multiple IADs\n", inum);
1616 }
1617 }
1618
1619 return retval;
1620}
1621
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001622
1623/*
1624 * Internal function to queue a device reset
1625 *
1626 * This is initialized into the workstruct in 'struct
1627 * usb_device->reset_ws' that is launched by
1628 * message.c:usb_set_configuration() when initializing each 'struct
1629 * usb_interface'.
1630 *
1631 * It is safe to get the USB device without reference counts because
1632 * the life cycle of @iface is bound to the life cycle of @udev. Then,
1633 * this function will be ran only if @iface is alive (and before
1634 * freeing it any scheduled instances of it will have been cancelled).
1635 *
1636 * We need to set a flag (usb_dev->reset_running) because when we call
1637 * the reset, the interfaces might be unbound. The current interface
1638 * cannot try to remove the queued work as it would cause a deadlock
1639 * (you cannot remove your work from within your executing
1640 * workqueue). This flag lets it know, so that
1641 * usb_cancel_queued_reset() doesn't try to do it.
1642 *
1643 * See usb_queue_reset_device() for more details
1644 */
Felipe Balbi09e81f32009-12-04 15:47:44 +02001645static void __usb_queue_reset_device(struct work_struct *ws)
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001646{
1647 int rc;
1648 struct usb_interface *iface =
1649 container_of(ws, struct usb_interface, reset_ws);
1650 struct usb_device *udev = interface_to_usbdev(iface);
1651
1652 rc = usb_lock_device_for_reset(udev, iface);
1653 if (rc >= 0) {
1654 iface->reset_running = 1;
1655 usb_reset_device(udev);
1656 iface->reset_running = 0;
1657 usb_unlock_device(udev);
1658 }
1659}
1660
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662/*
1663 * usb_set_configuration - Makes a particular device setting be current
1664 * @dev: the device whose configuration is being updated
1665 * @configuration: the configuration being chosen.
1666 * Context: !in_interrupt(), caller owns the device lock
1667 *
1668 * This is used to enable non-default device modes. Not all devices
1669 * use this kind of configurability; many devices only have one
1670 * configuration.
1671 *
Alan Stern3f141e22007-02-08 16:40:43 -05001672 * @configuration is the value of the configuration to be installed.
1673 * According to the USB spec (e.g. section 9.1.1.5), configuration values
1674 * must be non-zero; a value of zero indicates that the device in
1675 * unconfigured. However some devices erroneously use 0 as one of their
1676 * configuration values. To help manage such devices, this routine will
1677 * accept @configuration = -1 as indicating the device should be put in
1678 * an unconfigured state.
1679 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 * USB device configurations may affect Linux interoperability,
1681 * power consumption and the functionality available. For example,
1682 * the default configuration is limited to using 100mA of bus power,
1683 * so that when certain device functionality requires more power,
1684 * and the device is bus powered, that functionality should be in some
1685 * non-default device configuration. Other device modes may also be
1686 * reflected as configuration options, such as whether two ISDN
1687 * channels are available independently; and choosing between open
1688 * standard device protocols (like CDC) or proprietary ones.
1689 *
Inaky Perez-Gonzalez16bbab22007-07-31 20:34:01 -07001690 * Note that a non-authorized device (dev->authorized == 0) will only
1691 * be put in unconfigured mode.
1692 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 * Note that USB has an additional level of device configurability,
1694 * associated with interfaces. That configurability is accessed using
1695 * usb_set_interface().
1696 *
1697 * This call is synchronous. The calling context must be able to sleep,
1698 * must own the device lock, and must not hold the driver model's USB
Ming Lei6d243e52008-06-17 23:24:08 +08001699 * bus mutex; usb interface driver probe() methods cannot use this routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 *
1701 * Returns zero on success, or else the status code returned by the
Steven Cole093cf722005-05-03 19:07:24 -06001702 * underlying call that failed. On successful completion, each interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 * in the original device configuration has been destroyed, and each one
1704 * in the new configuration has been probed by all relevant usb device
1705 * drivers currently known to the kernel.
1706 */
1707int usb_set_configuration(struct usb_device *dev, int configuration)
1708{
1709 int i, ret;
1710 struct usb_host_config *cp = NULL;
1711 struct usb_interface **new_interfaces = NULL;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001712 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 int n, nintf;
1714
Inaky Perez-Gonzalez16bbab22007-07-31 20:34:01 -07001715 if (dev->authorized == 0 || configuration == -1)
Alan Stern3f141e22007-02-08 16:40:43 -05001716 configuration = 0;
1717 else {
1718 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1719 if (dev->config[i].desc.bConfigurationValue ==
1720 configuration) {
1721 cp = &dev->config[i];
1722 break;
1723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
1725 }
1726 if ((!cp && configuration != 0))
1727 return -EINVAL;
1728
1729 /* The USB spec says configuration 0 means unconfigured.
1730 * But if a device includes a configuration numbered 0,
1731 * we will accept it as a correctly configured state.
Alan Stern3f141e22007-02-08 16:40:43 -05001732 * Use -1 if you really want to unconfigure the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 */
1734 if (cp && configuration == 0)
1735 dev_warn(&dev->dev, "config 0 descriptor??\n");
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 /* Allocate memory for new interfaces before doing anything else,
1738 * so that if we run out then nothing will have changed. */
1739 n = nintf = 0;
1740 if (cp) {
1741 nintf = cp->desc.bNumInterfaces;
1742 new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
Oliver Neukumacbe2fe2010-01-12 12:32:50 +01001743 GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (!new_interfaces) {
Joe Perches898eb712007-10-18 03:06:30 -07001745 dev_err(&dev->dev, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 return -ENOMEM;
1747 }
1748
1749 for (; n < nintf; ++n) {
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001750 new_interfaces[n] = kzalloc(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 sizeof(struct usb_interface),
Oliver Neukumacbe2fe2010-01-12 12:32:50 +01001752 GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if (!new_interfaces[n]) {
Joe Perches898eb712007-10-18 03:06:30 -07001754 dev_err(&dev->dev, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 ret = -ENOMEM;
1756free_interfaces:
1757 while (--n >= 0)
1758 kfree(new_interfaces[n]);
1759 kfree(new_interfaces);
1760 return ret;
1761 }
1762 }
Alan Stern6ad07122006-06-01 13:59:16 -04001763
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +01001764 i = dev->bus_mA - usb_get_max_power(dev, cp);
Alan Stern6ad07122006-06-01 13:59:16 -04001765 if (i < 0)
1766 dev_warn(&dev->dev, "new config #%d exceeds power "
1767 "limit by %dmA\n",
1768 configuration, -i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
1770
Alan Stern01d883d2006-08-30 15:47:18 -04001771 /* Wake up the device so we can send it the Set-Config request */
Alan Stern94fcda12006-11-20 11:38:46 -05001772 ret = usb_autoresume_device(dev);
Alan Stern01d883d2006-08-30 15:47:18 -04001773 if (ret)
1774 goto free_interfaces;
1775
Thadeu Lima de Souza Cascardo07919712010-08-28 03:06:29 -03001776 /* if it's already configured, clear out old state first.
1777 * getting rid of old interfaces means unbinding their drivers.
1778 */
1779 if (dev->state != USB_STATE_ADDRESS)
1780 usb_disable_device(dev, 1); /* Skip ep0 */
1781
1782 /* Get rid of pending async Set-Config requests for this device */
1783 cancel_async_set_config(dev);
1784
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001785 /* Make sure we have bandwidth (and available HCD resources) for this
1786 * configuration. Remove endpoints from the schedule if we're dropping
1787 * this configuration to set configuration 0. After this point, the
1788 * host controller will not allow submissions to dropped endpoints. If
1789 * this call fails, the device state is unchanged.
1790 */
Alan Stern8963c482012-04-17 15:22:39 -04001791 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp83060952012-05-02 14:25:52 -07001792 /* Disable LPM, and re-enable it once the new configuration is
1793 * installed, so that the xHCI driver can recalculate the U1/U2
1794 * timeouts.
1795 */
Sarah Sharp9cf65992012-07-03 23:22:38 -07001796 if (dev->actconfig && usb_disable_lpm(dev)) {
Sarah Sharp83060952012-05-02 14:25:52 -07001797 dev_err(&dev->dev, "%s Failed to disable LPM\n.", __func__);
1798 mutex_unlock(hcd->bandwidth_mutex);
Sachin Kamatc058f7a2012-11-21 11:01:19 +05301799 ret = -ENOMEM;
1800 goto free_interfaces;
Sarah Sharp83060952012-05-02 14:25:52 -07001801 }
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001802 ret = usb_hcd_alloc_bandwidth(dev, cp, NULL, NULL);
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001803 if (ret < 0) {
Sarah Sharp9cf65992012-07-03 23:22:38 -07001804 if (dev->actconfig)
1805 usb_enable_lpm(dev);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001806 mutex_unlock(hcd->bandwidth_mutex);
Thadeu Lima de Souza Cascardo07919712010-08-28 03:06:29 -03001807 usb_autosuspend_device(dev);
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001808 goto free_interfaces;
1809 }
1810
Alan Stern36caff52012-11-07 10:31:30 -05001811 /*
1812 * Initialize the new interface structures and the
Alan Stern6ad07122006-06-01 13:59:16 -04001813 * hc/hcd/usbcore interface/endpoint state.
1814 */
1815 for (i = 0; i < nintf; ++i) {
1816 struct usb_interface_cache *intfc;
1817 struct usb_interface *intf;
1818 struct usb_host_interface *alt;
1819
1820 cp->interface[i] = intf = new_interfaces[i];
1821 intfc = cp->intf_cache[i];
1822 intf->altsetting = intfc->altsetting;
1823 intf->num_altsetting = intfc->num_altsetting;
1824 kref_get(&intfc->ref);
1825
1826 alt = usb_altnum_to_altsetting(intf, 0);
1827
1828 /* No altsetting 0? We'll assume the first altsetting.
1829 * We could use a GetInterface call, but if a device is
1830 * so non-compliant that it doesn't have altsetting 0
1831 * then I wouldn't trust its reply anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 */
Alan Stern6ad07122006-06-01 13:59:16 -04001833 if (!alt)
1834 alt = &intf->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Daniel Mackb3a3dd02012-06-12 20:23:52 +02001836 intf->intf_assoc =
1837 find_iad(dev, cp, alt->desc.bInterfaceNumber);
Alan Stern6ad07122006-06-01 13:59:16 -04001838 intf->cur_altsetting = alt;
Alan Stern2caf7fc2008-12-31 11:31:33 -05001839 usb_enable_interface(dev, intf, true);
Alan Stern6ad07122006-06-01 13:59:16 -04001840 intf->dev.parent = &dev->dev;
1841 intf->dev.driver = NULL;
1842 intf->dev.bus = &usb_bus_type;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001843 intf->dev.type = &usb_if_device_type;
Alan Stern2e5f10e2008-04-30 15:37:19 -04001844 intf->dev.groups = usb_interface_groups;
Alan Stern6ad07122006-06-01 13:59:16 -04001845 intf->dev.dma_mask = dev->dev.dma_mask;
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001846 INIT_WORK(&intf->reset_ws, __usb_queue_reset_device);
Alan Stern0026e002010-09-21 15:01:53 -04001847 intf->minor = -1;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001848 device_initialize(&intf->dev);
Ming Lei63defa72010-11-15 15:56:54 -05001849 pm_runtime_no_callbacks(&intf->dev);
Kay Sievers0031a062008-05-02 06:02:41 +02001850 dev_set_name(&intf->dev, "%d-%s:%d.%d",
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001851 dev->bus->busnum, dev->devpath,
1852 configuration, alt->desc.bInterfaceNumber);
Alan Stern6ad07122006-06-01 13:59:16 -04001853 }
1854 kfree(new_interfaces);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Alan Stern36caff52012-11-07 10:31:30 -05001856 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1857 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
1858 NULL, 0, USB_CTRL_SET_TIMEOUT);
1859 if (ret < 0 && cp) {
1860 /*
1861 * All the old state is gone, so what else can we do?
1862 * The device is probably useless now anyway.
1863 */
1864 usb_hcd_alloc_bandwidth(dev, NULL, NULL, NULL);
1865 for (i = 0; i < nintf; ++i) {
1866 usb_disable_interface(dev, cp->interface[i], true);
1867 put_device(&cp->interface[i]->dev);
1868 cp->interface[i] = NULL;
1869 }
1870 cp = NULL;
1871 }
1872
1873 dev->actconfig = cp;
1874 mutex_unlock(hcd->bandwidth_mutex);
1875
1876 if (!cp) {
1877 usb_set_device_state(dev, USB_STATE_ADDRESS);
1878
1879 /* Leave LPM disabled while the device is unconfigured. */
1880 usb_autosuspend_device(dev);
1881 return ret;
1882 }
1883 usb_set_device_state(dev, USB_STATE_CONFIGURED);
1884
Alan Stern1662e3a2009-03-18 14:28:53 -04001885 if (cp->string == NULL &&
1886 !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
Alan Stern6ad07122006-06-01 13:59:16 -04001887 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Sarah Sharp83060952012-05-02 14:25:52 -07001889 /* Now that the interfaces are installed, re-enable LPM. */
1890 usb_unlocked_enable_lpm(dev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07001891 /* Enable LTM if it was turned off by usb_disable_device. */
1892 usb_enable_ltm(dev);
Sarah Sharp83060952012-05-02 14:25:52 -07001893
Alan Stern6ad07122006-06-01 13:59:16 -04001894 /* Now that all the interfaces are set up, register them
1895 * to trigger binding of drivers to interfaces. probe()
1896 * routines may install different altsettings and may
1897 * claim() any interfaces not yet bound. Many class drivers
1898 * need that: CDC, audio, video, etc.
1899 */
1900 for (i = 0; i < nintf; ++i) {
1901 struct usb_interface *intf = cp->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001903 dev_dbg(&dev->dev,
Alan Stern6ad07122006-06-01 13:59:16 -04001904 "adding %s (config #%d, interface %d)\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001905 dev_name(&intf->dev), configuration,
Alan Stern6ad07122006-06-01 13:59:16 -04001906 intf->cur_altsetting->desc.bInterfaceNumber);
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01001907 device_enable_async_suspend(&intf->dev);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001908 ret = device_add(&intf->dev);
Alan Stern6ad07122006-06-01 13:59:16 -04001909 if (ret != 0) {
1910 dev_err(&dev->dev, "device_add(%s) --> %d\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001911 dev_name(&intf->dev), ret);
Alan Stern6ad07122006-06-01 13:59:16 -04001912 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
Alan Stern3b23dd62008-12-05 14:10:34 -05001914 create_intf_ep_devs(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
1916
Alan Stern94fcda12006-11-20 11:38:46 -05001917 usb_autosuspend_device(dev);
Alan Stern86d30742005-07-29 12:17:16 -07001918 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
1920
Alan Sterndf718962008-12-19 10:27:56 -05001921static LIST_HEAD(set_config_list);
1922static DEFINE_SPINLOCK(set_config_lock);
1923
Alan Stern088dc272006-08-21 12:08:19 -04001924struct set_config_request {
1925 struct usb_device *udev;
1926 int config;
1927 struct work_struct work;
Alan Sterndf718962008-12-19 10:27:56 -05001928 struct list_head node;
Alan Stern088dc272006-08-21 12:08:19 -04001929};
1930
1931/* Worker routine for usb_driver_set_configuration() */
David Howellsc4028952006-11-22 14:57:56 +00001932static void driver_set_config_work(struct work_struct *work)
Alan Stern088dc272006-08-21 12:08:19 -04001933{
David Howellsc4028952006-11-22 14:57:56 +00001934 struct set_config_request *req =
1935 container_of(work, struct set_config_request, work);
Alan Sterndf718962008-12-19 10:27:56 -05001936 struct usb_device *udev = req->udev;
Alan Stern088dc272006-08-21 12:08:19 -04001937
Alan Sterndf718962008-12-19 10:27:56 -05001938 usb_lock_device(udev);
1939 spin_lock(&set_config_lock);
1940 list_del(&req->node);
1941 spin_unlock(&set_config_lock);
1942
1943 if (req->config >= -1) /* Is req still valid? */
1944 usb_set_configuration(udev, req->config);
1945 usb_unlock_device(udev);
1946 usb_put_dev(udev);
Alan Stern088dc272006-08-21 12:08:19 -04001947 kfree(req);
1948}
1949
Alan Sterndf718962008-12-19 10:27:56 -05001950/* Cancel pending Set-Config requests for a device whose configuration
1951 * was just changed
1952 */
1953static void cancel_async_set_config(struct usb_device *udev)
1954{
1955 struct set_config_request *req;
1956
1957 spin_lock(&set_config_lock);
1958 list_for_each_entry(req, &set_config_list, node) {
1959 if (req->udev == udev)
1960 req->config = -999; /* Mark as cancelled */
1961 }
1962 spin_unlock(&set_config_lock);
1963}
1964
Alan Stern088dc272006-08-21 12:08:19 -04001965/**
1966 * usb_driver_set_configuration - Provide a way for drivers to change device configurations
1967 * @udev: the device whose configuration is being updated
1968 * @config: the configuration being chosen.
1969 * Context: In process context, must be able to sleep
1970 *
1971 * Device interface drivers are not allowed to change device configurations.
1972 * This is because changing configurations will destroy the interface the
1973 * driver is bound to and create new ones; it would be like a floppy-disk
1974 * driver telling the computer to replace the floppy-disk drive with a
1975 * tape drive!
1976 *
1977 * Still, in certain specialized circumstances the need may arise. This
1978 * routine gets around the normal restrictions by using a work thread to
1979 * submit the change-config request.
1980 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02001981 * Return: 0 if the request was successfully queued, error code otherwise.
Alan Stern088dc272006-08-21 12:08:19 -04001982 * The caller has no way to know whether the queued request will eventually
1983 * succeed.
1984 */
1985int usb_driver_set_configuration(struct usb_device *udev, int config)
1986{
1987 struct set_config_request *req;
1988
1989 req = kmalloc(sizeof(*req), GFP_KERNEL);
1990 if (!req)
1991 return -ENOMEM;
1992 req->udev = udev;
1993 req->config = config;
David Howellsc4028952006-11-22 14:57:56 +00001994 INIT_WORK(&req->work, driver_set_config_work);
Alan Stern088dc272006-08-21 12:08:19 -04001995
Alan Sterndf718962008-12-19 10:27:56 -05001996 spin_lock(&set_config_lock);
1997 list_add(&req->node, &set_config_list);
1998 spin_unlock(&set_config_lock);
1999
Alan Stern088dc272006-08-21 12:08:19 -04002000 usb_get_dev(udev);
Alan Stern1737bf22006-12-15 16:04:52 -05002001 schedule_work(&req->work);
Alan Stern088dc272006-08-21 12:08:19 -04002002 return 0;
2003}
2004EXPORT_SYMBOL_GPL(usb_driver_set_configuration);