blob: 4f8221e3a0ffcef56333dff9b65e3e14d14100de [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/mm.h>
10#include <linux/timer.h>
11#include <linux/ctype.h>
Clemens Ladischa853a3d2009-04-24 10:12:18 +020012#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/device.h>
Ralf Baechle11763602007-10-23 20:42:11 +020014#include <linux/scatterlist.h>
Oliver Neukume4c6fb72016-07-14 15:41:30 +020015#include <linux/usb/cdc.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
Masanari Iidae2278672014-02-18 22:54:36 +0900182 * bytes transferred will be stored in the @actual_length parameter.
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
Rahul Bedarkar025d4432014-01-04 11:24:41 +0530221 * bytes transferred will be stored in the @actual_length parameter.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200222 *
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++) {
David Mosberger98b74b02016-03-08 14:42:48 -0700306 if (!io->urbs[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 continue;
308 if (found) {
David Mosberger98b74b02016-03-08 14:42:48 -0700309 usb_block_urb(io->urbs[i]);
Tülin İzer085528e2013-05-17 10:32:35 +0300310 retval = usb_unlink_urb(io->urbs[i]);
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700311 if (retval != -EINPROGRESS &&
312 retval != -ENODEV &&
Alan Sternbcf39852012-03-22 11:00:21 -0400313 retval != -EBUSY &&
314 retval != -EIDRM)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800315 dev_err(&io->dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 "%s, unlink --> %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800317 __func__, retval);
Tülin İzer085528e2013-05-17 10:32:35 +0300318 } else if (urb == io->urbs[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 found = 1;
320 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800321 spin_lock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* on the last completion, signal usb_sg_wait() */
325 io->bytes += urb->actual_length;
326 io->count--;
327 if (!io->count)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800328 complete(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800330 spin_unlock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
333
334/**
335 * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request
336 * @io: request block being initialized. until usb_sg_wait() returns,
337 * treat this as a pointer to an opaque block of memory,
338 * @dev: the usb device that will send or receive the data
339 * @pipe: endpoint "pipe" used to transfer the data
340 * @period: polling rate for interrupt endpoints, in frames or
341 * (for high speed endpoints) microframes; ignored for bulk
342 * @sg: scatterlist entries
343 * @nents: how many entries in the scatterlist
344 * @length: how many bytes to send from the scatterlist, or zero to
345 * send every byte identified in the list.
346 * @mem_flags: SLAB_* flags affecting memory allocations in this call
347 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200348 * This initializes a scatter/gather request, allocating resources such as
349 * I/O mappings and urb memory (except maybe memory used by USB controller
350 * drivers).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *
352 * The request must be issued using usb_sg_wait(), which waits for the I/O to
353 * complete (or to be canceled) and then cleans up all resources allocated by
354 * usb_sg_init().
355 *
356 * The request may be canceled with usb_sg_cancel(), either before or after
357 * usb_sg_wait() is called.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200358 *
359 * Return: Zero for success, else a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800361int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
362 unsigned pipe, unsigned period, struct scatterlist *sg,
363 int nents, size_t length, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800365 int i;
366 int urb_flags;
Sarah Sharpe04748e2009-04-27 19:59:01 -0700367 int use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 if (!io || !dev || !sg
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800370 || usb_pipecontrol(pipe)
371 || usb_pipeisoc(pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 || nents <= 0)
373 return -EINVAL;
374
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800375 spin_lock_init(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 io->dev = dev;
377 io->pipe = pipe;
Alan Stern0ba169af2010-05-05 15:26:17 -0400378
379 if (dev->bus->sg_tablesize > 0) {
380 use_sg = true;
381 io->entries = 1;
382 } else {
383 use_sg = false;
384 io->entries = nents;
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /* initialize all the urbs we'll use */
Tülin İzera1fefaa2013-05-17 10:33:05 +0300388 io->urbs = kmalloc(io->entries * sizeof(*io->urbs), mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (!io->urbs)
390 goto nomem;
391
Alan Stern0ba169af2010-05-05 15:26:17 -0400392 urb_flags = URB_NO_INTERRUPT;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800393 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 urb_flags |= URB_SHORT_NOT_OK;
395
Alan Stern0ba169af2010-05-05 15:26:17 -0400396 for_each_sg(sg, sg, io->entries, i) {
397 struct urb *urb;
398 unsigned len;
399
400 urb = usb_alloc_urb(0, mem_flags);
401 if (!urb) {
402 io->entries = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 goto nomem;
404 }
Alan Stern0ba169af2010-05-05 15:26:17 -0400405 io->urbs[i] = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Alan Stern0ba169af2010-05-05 15:26:17 -0400407 urb->dev = NULL;
408 urb->pipe = pipe;
409 urb->interval = period;
410 urb->transfer_flags = urb_flags;
411 urb->complete = sg_complete;
412 urb->context = io;
413 urb->sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Alan Stern0ba169af2010-05-05 15:26:17 -0400415 if (use_sg) {
416 /* There is no single transfer buffer */
417 urb->transfer_buffer = NULL;
418 urb->num_sgs = nents;
Alan Sternff9c8952010-04-02 13:27:28 -0400419
Alan Stern0ba169af2010-05-05 15:26:17 -0400420 /* A length of zero means transfer the whole sg list */
421 len = length;
422 if (len == 0) {
Alan Stern64d65872010-06-18 10:16:33 -0400423 struct scatterlist *sg2;
424 int j;
425
426 for_each_sg(sg, sg2, nents, j)
427 len += sg2->length;
Sarah Sharpe04748e2009-04-27 19:59:01 -0700428 }
Alan Stern0ba169af2010-05-05 15:26:17 -0400429 } else {
Sarah Sharpe04748e2009-04-27 19:59:01 -0700430 /*
Alan Sternff9c8952010-04-02 13:27:28 -0400431 * Some systems can't use DMA; they use PIO instead.
432 * For their sakes, transfer_buffer is set whenever
433 * possible.
Sarah Sharpe04748e2009-04-27 19:59:01 -0700434 */
Alan Sternff9c8952010-04-02 13:27:28 -0400435 if (!PageHighMem(sg_page(sg)))
Alan Stern0ba169af2010-05-05 15:26:17 -0400436 urb->transfer_buffer = sg_virt(sg);
Pete Zaitcev81bf46f2009-06-11 08:40:39 -0600437 else
Alan Stern0ba169af2010-05-05 15:26:17 -0400438 urb->transfer_buffer = NULL;
Pete Zaitcev81bf46f2009-06-11 08:40:39 -0600439
Alan Sternff9c8952010-04-02 13:27:28 -0400440 len = sg->length;
Sarah Sharpe04748e2009-04-27 19:59:01 -0700441 if (length) {
Dan Carpenteredb2b252011-09-27 09:25:19 +0300442 len = min_t(size_t, len, length);
Sarah Sharpe04748e2009-04-27 19:59:01 -0700443 length -= len;
444 if (length == 0)
445 io->entries = i + 1;
446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Alan Stern0ba169af2010-05-05 15:26:17 -0400448 urb->transfer_buffer_length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
Alan Stern0ba169af2010-05-05 15:26:17 -0400450 io->urbs[--i]->transfer_flags &= ~URB_NO_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /* transaction state */
Alan Stern580da342008-08-05 13:05:17 -0400453 io->count = io->entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 io->status = 0;
455 io->bytes = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800456 init_completion(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
458
459nomem:
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800460 sg_clean(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return -ENOMEM;
462}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600463EXPORT_SYMBOL_GPL(usb_sg_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465/**
466 * usb_sg_wait - synchronously execute scatter/gather request
467 * @io: request block handle, as initialized with usb_sg_init().
468 * some fields become accessible when this call returns.
469 * Context: !in_interrupt ()
470 *
471 * This function blocks until the specified I/O operation completes. It
472 * leverages the grouping of the related I/O requests to get good transfer
473 * rates, by queueing the requests. At higher speeds, such queuing can
474 * significantly improve USB throughput.
475 *
476 * There are three kinds of completion for this function.
477 * (1) success, where io->status is zero. The number of io->bytes
478 * transferred is as requested.
479 * (2) error, where io->status is a negative errno value. The number
480 * of io->bytes transferred before the error is usually less
481 * than requested, and can be nonzero.
Steven Cole093cf722005-05-03 19:07:24 -0600482 * (3) cancellation, a type of error with status -ECONNRESET that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 * is initiated by usb_sg_cancel().
484 *
485 * When this function returns, all memory allocated through usb_sg_init() or
486 * this call will have been freed. The request block parameter may still be
487 * passed to usb_sg_cancel(), or it may be freed. It could also be
488 * reinitialized and then reused.
489 *
490 * Data Transfer Rates:
491 *
492 * Bulk transfers are valid for full or high speed endpoints.
493 * The best full speed data rate is 19 packets of 64 bytes each
494 * per frame, or 1216 bytes per millisecond.
495 * The best high speed data rate is 13 packets of 512 bytes each
496 * per microframe, or 52 KBytes per millisecond.
497 *
498 * The reason to use interrupt transfers through this API would most likely
499 * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
500 * could be transferred. That capability is less useful for low or full
501 * speed interrupt endpoints, which allow at most one packet per millisecond,
502 * of at most 8 or 64 bytes (respectively).
Sarah Sharp79abb1a2009-04-27 19:58:26 -0700503 *
504 * It is not necessary to call this function to reserve bandwidth for devices
505 * under an xHCI host controller, as the bandwidth is reserved when the
506 * configuration or interface alt setting is selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800508void usb_sg_wait(struct usb_sg_request *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800510 int i;
511 int entries = io->entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /* queue the urbs. */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800514 spin_lock_irq(&io->lock);
Alan Stern8ccef0d2007-06-21 16:26:46 -0400515 i = 0;
516 while (i < entries && !io->status) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800517 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800519 io->urbs[i]->dev = io->dev;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800520 spin_unlock_irq(&io->lock);
David Mosberger98b74b02016-03-08 14:42:48 -0700521
522 retval = usb_submit_urb(io->urbs[i], GFP_NOIO);
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 switch (retval) {
525 /* maybe we retrying will recover */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800526 case -ENXIO: /* hc didn't queue this one */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 case -EAGAIN:
528 case -ENOMEM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 retval = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800530 yield();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 break;
532
533 /* no error? continue immediately.
534 *
535 * NOTE: to work better with UHCI (4K I/O buffer may
536 * need 3K of TDs) it may be good to limit how many
537 * URBs are queued at once; N milliseconds?
538 */
539 case 0:
Alan Stern8ccef0d2007-06-21 16:26:46 -0400540 ++i;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800541 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 break;
543
544 /* fail any uncompleted urbs */
545 default:
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800546 io->urbs[i]->status = retval;
547 dev_dbg(&io->dev->dev, "%s, submit --> %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800548 __func__, retval);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800549 usb_sg_cancel(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800551 spin_lock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (retval && (io->status == 0 || io->status == -ECONNRESET))
553 io->status = retval;
554 }
555 io->count -= entries - i;
556 if (io->count == 0)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800557 complete(&io->complete);
558 spin_unlock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /* OK, yes, this could be packaged as non-blocking.
561 * So could the submit loop above ... but it's easier to
562 * solve neither problem than to solve both!
563 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800564 wait_for_completion(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800566 sg_clean(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600568EXPORT_SYMBOL_GPL(usb_sg_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570/**
571 * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
572 * @io: request block, initialized with usb_sg_init()
573 *
574 * This stops a request after it has been started by usb_sg_wait().
575 * It can also prevents one initialized by usb_sg_init() from starting,
576 * so that call just frees resources allocated to the request.
577 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800578void usb_sg_cancel(struct usb_sg_request *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800580 unsigned long flags;
David Mosberger5f2e5fb2016-03-08 14:42:49 -0700581 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800583 spin_lock_irqsave(&io->lock, flags);
David Mosberger5f2e5fb2016-03-08 14:42:49 -0700584 if (io->status) {
585 spin_unlock_irqrestore(&io->lock, flags);
586 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
David Mosberger5f2e5fb2016-03-08 14:42:49 -0700588 /* shut everything down */
589 io->status = -ECONNRESET;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800590 spin_unlock_irqrestore(&io->lock, flags);
David Mosberger5f2e5fb2016-03-08 14:42:49 -0700591
592 for (i = io->entries - 1; i >= 0; --i) {
593 usb_block_urb(io->urbs[i]);
594
595 retval = usb_unlink_urb(io->urbs[i]);
596 if (retval != -EINPROGRESS
597 && retval != -ENODEV
598 && retval != -EBUSY
599 && retval != -EIDRM)
600 dev_warn(&io->dev->dev, "%s, unlink --> %d\n",
601 __func__, retval);
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600604EXPORT_SYMBOL_GPL(usb_sg_cancel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606/*-------------------------------------------------------------------*/
607
608/**
609 * usb_get_descriptor - issues a generic GET_DESCRIPTOR request
610 * @dev: the device whose descriptor is being retrieved
611 * @type: the descriptor type (USB_DT_*)
612 * @index: the number of the descriptor
613 * @buf: where to put the descriptor
614 * @size: how big is "buf"?
615 * Context: !in_interrupt ()
616 *
617 * Gets a USB descriptor. Convenience functions exist to simplify
618 * getting some types of descriptors. Use
619 * usb_get_string() or usb_string() for USB_DT_STRING.
620 * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG)
621 * are part of the device structure.
622 * In addition to a number of USB-standard descriptors, some
623 * devices also use class-specific or vendor-specific descriptors.
624 *
625 * This call is synchronous, and may not be used in an interrupt context.
626 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200627 * Return: The number of bytes received on success, or else the status code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 * returned by the underlying usb_control_msg() call.
629 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800630int usb_get_descriptor(struct usb_device *dev, unsigned char type,
631 unsigned char index, void *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 int i;
634 int result;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800635
636 memset(buf, 0, size); /* Make sure we parse really received data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 for (i = 0; i < 3; ++i) {
Alan Sternc39772d2007-08-20 10:45:28 -0400639 /* retry on length 0 or error; some devices are flakey */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
641 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
642 (type << 8) + index, 0, buf, size,
643 USB_CTRL_GET_TIMEOUT);
Alan Sternc39772d2007-08-20 10:45:28 -0400644 if (result <= 0 && result != -ETIMEDOUT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 continue;
646 if (result > 1 && ((u8 *)buf)[1] != type) {
Alan Stern67f5a4b2009-02-20 16:33:08 -0500647 result = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 continue;
649 }
650 break;
651 }
652 return result;
653}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600654EXPORT_SYMBOL_GPL(usb_get_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656/**
657 * usb_get_string - gets a string descriptor
658 * @dev: the device whose string descriptor is being retrieved
659 * @langid: code for language chosen (from string descriptor zero)
660 * @index: the number of the descriptor
661 * @buf: where to put the string
662 * @size: how big is "buf"?
663 * Context: !in_interrupt ()
664 *
665 * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character,
666 * in little-endian byte order).
667 * The usb_string() function will often be a convenient way to turn
668 * these strings into kernel-printable form.
669 *
670 * Strings may be referenced in device, configuration, interface, or other
671 * descriptors, and could also be used in vendor-specific ways.
672 *
673 * This call is synchronous, and may not be used in an interrupt context.
674 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200675 * Return: The number of bytes received on success, or else the status code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 * returned by the underlying usb_control_msg() call.
677 */
Adrian Bunke266a122005-11-08 21:05:43 +0100678static int usb_get_string(struct usb_device *dev, unsigned short langid,
679 unsigned char index, void *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 int i;
682 int result;
683
684 for (i = 0; i < 3; ++i) {
685 /* retry on length 0 or stall; some devices are flakey */
686 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
687 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
688 (USB_DT_STRING << 8) + index, langid, buf, size,
689 USB_CTRL_GET_TIMEOUT);
Alan Stern67f5a4b2009-02-20 16:33:08 -0500690 if (result == 0 || result == -EPIPE)
691 continue;
692 if (result > 1 && ((u8 *) buf)[1] != USB_DT_STRING) {
693 result = -ENODATA;
694 continue;
695 }
696 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698 return result;
699}
700
701static void usb_try_string_workarounds(unsigned char *buf, int *length)
702{
703 int newlength, oldlength = *length;
704
705 for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
706 if (!isprint(buf[newlength]) || buf[newlength + 1])
707 break;
708
709 if (newlength > 2) {
710 buf[0] = newlength;
711 *length = newlength;
712 }
713}
714
715static int usb_string_sub(struct usb_device *dev, unsigned int langid,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800716 unsigned int index, unsigned char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 int rc;
719
720 /* Try to read the string descriptor by asking for the maximum
721 * possible number of bytes */
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100722 if (dev->quirks & USB_QUIRK_STRING_FETCH_255)
723 rc = -EIO;
724 else
725 rc = usb_get_string(dev, langid, index, buf, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 /* If that failed try to read the descriptor length, then
728 * ask for just that many bytes */
729 if (rc < 2) {
730 rc = usb_get_string(dev, langid, index, buf, 2);
731 if (rc == 2)
732 rc = usb_get_string(dev, langid, index, buf, buf[0]);
733 }
734
735 if (rc >= 2) {
736 if (!buf[0] && !buf[1])
737 usb_try_string_workarounds(buf, &rc);
738
739 /* There might be extra junk at the end of the descriptor */
740 if (buf[0] < rc)
741 rc = buf[0];
742
743 rc = rc - (rc & 1); /* force a multiple of two */
744 }
745
746 if (rc < 2)
747 rc = (rc < 0 ? rc : -EINVAL);
748
749 return rc;
750}
751
Daniel Mack0cce2ed2009-07-10 11:04:58 +0200752static int usb_get_langid(struct usb_device *dev, unsigned char *tbuf)
753{
754 int err;
755
756 if (dev->have_langid)
757 return 0;
758
759 if (dev->string_langid < 0)
760 return -EPIPE;
761
762 err = usb_string_sub(dev, 0, 0, tbuf);
763
764 /* If the string was reported but is malformed, default to english
765 * (0x0409) */
766 if (err == -ENODATA || (err > 0 && err < 4)) {
767 dev->string_langid = 0x0409;
768 dev->have_langid = 1;
769 dev_err(&dev->dev,
Scot Doyle586af072014-09-25 15:16:48 +0000770 "language id specifier not provided by device, defaulting to English\n");
Daniel Mack0cce2ed2009-07-10 11:04:58 +0200771 return 0;
772 }
773
774 /* In case of all other errors, we assume the device is not able to
775 * deal with strings at all. Set string_langid to -1 in order to
776 * prevent any string to be retrieved from the device */
777 if (err < 0) {
778 dev_err(&dev->dev, "string descriptor 0 read error: %d\n",
779 err);
780 dev->string_langid = -1;
781 return -EPIPE;
782 }
783
784 /* always use the first langid listed */
785 dev->string_langid = tbuf[2] | (tbuf[3] << 8);
786 dev->have_langid = 1;
787 dev_dbg(&dev->dev, "default language 0x%04x\n",
788 dev->string_langid);
789 return 0;
790}
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792/**
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200793 * usb_string - returns UTF-8 version of a string descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 * @dev: the device whose string descriptor is being retrieved
795 * @index: the number of the descriptor
796 * @buf: where to put the string
797 * @size: how big is "buf"?
798 * Context: !in_interrupt ()
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800799 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 * This converts the UTF-16LE encoded strings returned by devices, from
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200801 * usb_get_string_descriptor(), to null-terminated UTF-8 encoded ones
802 * that are more usable in most kernel contexts. Note that this function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 * chooses strings in the first language supported by the device.
804 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 * This call is synchronous, and may not be used in an interrupt context.
806 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200807 * Return: length of the string (>= 0) or usb_control_msg status (< 0).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 */
809int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
810{
811 unsigned char *tbuf;
812 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (dev->state == USB_STATE_SUSPENDED)
815 return -EHOSTUNREACH;
816 if (size <= 0 || !buf || !index)
817 return -EINVAL;
818 buf[0] = 0;
Alan Stern74675a52009-04-30 10:08:18 -0400819 tbuf = kmalloc(256, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (!tbuf)
821 return -ENOMEM;
822
Daniel Mack0cce2ed2009-07-10 11:04:58 +0200823 err = usb_get_langid(dev, tbuf);
824 if (err < 0)
825 goto errout;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 err = usb_string_sub(dev, dev->string_langid, index, tbuf);
828 if (err < 0)
829 goto errout;
830
831 size--; /* leave room for trailing NULL char in output buffer */
Alan Stern74675a52009-04-30 10:08:18 -0400832 err = utf16s_to_utf8s((wchar_t *) &tbuf[2], (err - 2) / 2,
833 UTF16_LITTLE_ENDIAN, buf, size);
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200834 buf[err] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 if (tbuf[1] != USB_DT_STRING)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800837 dev_dbg(&dev->dev,
838 "wrong descriptor type %02x for string %d (\"%s\")\n",
839 tbuf[1], index, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 errout:
842 kfree(tbuf);
843 return err;
844}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600845EXPORT_SYMBOL_GPL(usb_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200847/* one UTF-8-encoded 16-bit character has at most three bytes */
848#define MAX_USB_STRING_SIZE (127 * 3 + 1)
849
Alan Stern4f62efe2005-10-24 16:24:14 -0400850/**
851 * usb_cache_string - read a string descriptor and cache it for later use
852 * @udev: the device whose string descriptor is being read
853 * @index: the descriptor index
854 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200855 * Return: A pointer to a kmalloc'ed buffer containing the descriptor string,
856 * or %NULL if the index is 0 or the string could not be read.
Alan Stern4f62efe2005-10-24 16:24:14 -0400857 */
858char *usb_cache_string(struct usb_device *udev, int index)
859{
860 char *buf;
861 char *smallbuf = NULL;
862 int len;
863
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800864 if (index <= 0)
865 return NULL;
866
Oliver Neukumacbe2fe2010-01-12 12:32:50 +0100867 buf = kmalloc(MAX_USB_STRING_SIZE, GFP_NOIO);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800868 if (buf) {
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200869 len = usb_string(udev, index, buf, MAX_USB_STRING_SIZE);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800870 if (len > 0) {
Oliver Neukumacbe2fe2010-01-12 12:32:50 +0100871 smallbuf = kmalloc(++len, GFP_NOIO);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800872 if (!smallbuf)
Alan Stern4f62efe2005-10-24 16:24:14 -0400873 return buf;
874 memcpy(smallbuf, buf, len);
875 }
876 kfree(buf);
877 }
878 return smallbuf;
879}
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881/*
882 * usb_get_device_descriptor - (re)reads the device descriptor (usbcore)
883 * @dev: the device whose device descriptor is being updated
884 * @size: how much of the descriptor to read
885 * Context: !in_interrupt ()
886 *
887 * Updates the copy of the device descriptor stored in the device structure,
Laurent Pinchart6ab16a92006-11-07 10:16:25 +0100888 * which dedicates space for this purpose.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 *
890 * Not exported, only for use by the core. If drivers really want to read
891 * the device descriptor directly, they can call usb_get_descriptor() with
892 * type = USB_DT_DEVICE and index = 0.
893 *
894 * This call is synchronous, and may not be used in an interrupt context.
895 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200896 * Return: The number of bytes received on success, or else the status code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 * returned by the underlying usb_control_msg() call.
898 */
899int usb_get_device_descriptor(struct usb_device *dev, unsigned int size)
900{
901 struct usb_device_descriptor *desc;
902 int ret;
903
904 if (size > sizeof(*desc))
905 return -EINVAL;
906 desc = kmalloc(sizeof(*desc), GFP_NOIO);
907 if (!desc)
908 return -ENOMEM;
909
910 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800911 if (ret >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 memcpy(&dev->descriptor, desc, size);
913 kfree(desc);
914 return ret;
915}
916
917/**
918 * usb_get_status - issues a GET_STATUS call
919 * @dev: the device whose status is being checked
920 * @type: USB_RECIP_*; for device, interface, or endpoint
921 * @target: zero (for device), else interface or endpoint number
922 * @data: pointer to two bytes of bitmap data
923 * Context: !in_interrupt ()
924 *
925 * Returns device, interface, or endpoint status. Normally only of
926 * interest to see if the device is self powered, or has enabled the
927 * remote wakeup facility; or whether a bulk or interrupt endpoint
928 * is halted ("stalled").
929 *
930 * Bits in these status bitmaps are set using the SET_FEATURE request,
931 * and cleared using the CLEAR_FEATURE request. The usb_clear_halt()
932 * function should be used to clear halt ("stall") status.
933 *
934 * This call is synchronous, and may not be used in an interrupt context.
935 *
Alan Stern15b73362013-07-30 15:35:40 -0400936 * Returns 0 and the status value in *@data (in host byte order) on success,
937 * or else the status code from the underlying usb_control_msg() call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 */
939int usb_get_status(struct usb_device *dev, int type, int target, void *data)
940{
941 int ret;
Alan Stern15b73362013-07-30 15:35:40 -0400942 __le16 *status = kmalloc(sizeof(*status), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 if (!status)
945 return -ENOMEM;
946
947 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
948 USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status,
949 sizeof(*status), USB_CTRL_GET_TIMEOUT);
950
Alan Stern15b73362013-07-30 15:35:40 -0400951 if (ret == 2) {
952 *(u16 *) data = le16_to_cpu(*status);
953 ret = 0;
954 } else if (ret >= 0) {
955 ret = -EIO;
956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 kfree(status);
958 return ret;
959}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600960EXPORT_SYMBOL_GPL(usb_get_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962/**
963 * usb_clear_halt - tells device to clear endpoint halt/stall condition
964 * @dev: device whose endpoint is halted
965 * @pipe: endpoint "pipe" being cleared
966 * Context: !in_interrupt ()
967 *
968 * This is used to clear halt conditions for bulk and interrupt endpoints,
969 * as reported by URB completion status. Endpoints that are halted are
970 * sometimes referred to as being "stalled". Such endpoints are unable
971 * to transmit or receive data until the halt status is cleared. Any URBs
972 * queued for such an endpoint should normally be unlinked by the driver
973 * before clearing the halt condition, as described in sections 5.7.5
974 * and 5.8.5 of the USB 2.0 spec.
975 *
976 * Note that control and isochronous endpoints don't halt, although control
977 * endpoints report "protocol stall" (for unsupported requests) using the
978 * same status code used to report a true stall.
979 *
980 * This call is synchronous, and may not be used in an interrupt context.
981 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200982 * Return: Zero on success, or else the status code returned by the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 * underlying usb_control_msg() call.
984 */
985int usb_clear_halt(struct usb_device *dev, int pipe)
986{
987 int result;
988 int endp = usb_pipeendpoint(pipe);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800989
990 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 endp |= USB_DIR_IN;
992
993 /* we don't care if it wasn't halted first. in fact some devices
994 * (like some ibmcam model 1 units) seem to expect hosts to make
995 * this request for iso endpoints, which can't halt!
996 */
997 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
998 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
999 USB_ENDPOINT_HALT, endp, NULL, 0,
1000 USB_CTRL_SET_TIMEOUT);
1001
1002 /* don't un-halt or force to DATA0 except on success */
1003 if (result < 0)
1004 return result;
1005
1006 /* NOTE: seems like Microsoft and Apple don't bother verifying
1007 * the clear "took", so some devices could lock up if you check...
1008 * such as the Hagiwara FlashGate DUAL. So we won't bother.
1009 *
1010 * NOTE: make sure the logic here doesn't diverge much from
1011 * the copy in usb-storage, for as long as we need two copies.
1012 */
1013
David Vrabel3444b262009-04-08 17:36:28 +00001014 usb_reset_endpoint(dev, endp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 return 0;
1017}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001018EXPORT_SYMBOL_GPL(usb_clear_halt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Alan Stern3b23dd62008-12-05 14:10:34 -05001020static int create_intf_ep_devs(struct usb_interface *intf)
1021{
1022 struct usb_device *udev = interface_to_usbdev(intf);
1023 struct usb_host_interface *alt = intf->cur_altsetting;
1024 int i;
1025
1026 if (intf->ep_devs_created || intf->unregistering)
1027 return 0;
1028
1029 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1030 (void) usb_create_ep_devs(&intf->dev, &alt->endpoint[i], udev);
1031 intf->ep_devs_created = 1;
1032 return 0;
1033}
1034
1035static void remove_intf_ep_devs(struct usb_interface *intf)
1036{
1037 struct usb_host_interface *alt = intf->cur_altsetting;
1038 int i;
1039
1040 if (!intf->ep_devs_created)
1041 return;
1042
1043 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1044 usb_remove_ep_devs(&alt->endpoint[i]);
1045 intf->ep_devs_created = 0;
1046}
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048/**
1049 * usb_disable_endpoint -- Disable an endpoint by address
1050 * @dev: the device whose endpoint is being disabled
1051 * @epaddr: the endpoint's address. Endpoint number for output,
1052 * endpoint number + USB_DIR_IN for input
Alan Sternddeac4e72009-01-15 17:03:33 -05001053 * @reset_hardware: flag to erase any endpoint state stored in the
1054 * controller hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 *
Alan Sternddeac4e72009-01-15 17:03:33 -05001056 * Disables the endpoint for URB submission and nukes all pending URBs.
1057 * If @reset_hardware is set then also deallocates hcd/hardware state
1058 * for the endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 */
Alan Sternddeac4e72009-01-15 17:03:33 -05001060void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr,
1061 bool reset_hardware)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1064 struct usb_host_endpoint *ep;
1065
1066 if (!dev)
1067 return;
1068
1069 if (usb_endpoint_out(epaddr)) {
1070 ep = dev->ep_out[epnum];
Alan Sternddeac4e72009-01-15 17:03:33 -05001071 if (reset_hardware)
1072 dev->ep_out[epnum] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 } else {
1074 ep = dev->ep_in[epnum];
Alan Sternddeac4e72009-01-15 17:03:33 -05001075 if (reset_hardware)
1076 dev->ep_in[epnum] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
Alan Sternbdd016b2007-07-30 17:05:22 -04001078 if (ep) {
1079 ep->enabled = 0;
Alan Stern95cf82f2007-09-10 11:33:05 -04001080 usb_hcd_flush_endpoint(dev, ep);
Alan Sternddeac4e72009-01-15 17:03:33 -05001081 if (reset_hardware)
1082 usb_hcd_disable_endpoint(dev, ep);
Alan Sternbdd016b2007-07-30 17:05:22 -04001083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
1086/**
David Vrabel3444b262009-04-08 17:36:28 +00001087 * usb_reset_endpoint - Reset an endpoint's state.
1088 * @dev: the device whose endpoint is to be reset
1089 * @epaddr: the endpoint's address. Endpoint number for output,
1090 * endpoint number + USB_DIR_IN for input
1091 *
1092 * Resets any host-side endpoint state such as the toggle bit,
1093 * sequence number or current window.
1094 */
1095void usb_reset_endpoint(struct usb_device *dev, unsigned int epaddr)
1096{
1097 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1098 struct usb_host_endpoint *ep;
1099
1100 if (usb_endpoint_out(epaddr))
1101 ep = dev->ep_out[epnum];
1102 else
1103 ep = dev->ep_in[epnum];
1104 if (ep)
1105 usb_hcd_reset_endpoint(dev, ep);
1106}
1107EXPORT_SYMBOL_GPL(usb_reset_endpoint);
1108
1109
1110/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 * usb_disable_interface -- Disable all endpoints for an interface
1112 * @dev: the device whose interface is being disabled
1113 * @intf: pointer to the interface descriptor
Alan Sternddeac4e72009-01-15 17:03:33 -05001114 * @reset_hardware: flag to erase any endpoint state stored in the
1115 * controller hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 *
1117 * Disables all the endpoints for the interface's current altsetting.
1118 */
Alan Sternddeac4e72009-01-15 17:03:33 -05001119void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf,
1120 bool reset_hardware)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 struct usb_host_interface *alt = intf->cur_altsetting;
1123 int i;
1124
1125 for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
1126 usb_disable_endpoint(dev,
Alan Sternddeac4e72009-01-15 17:03:33 -05001127 alt->endpoint[i].desc.bEndpointAddress,
1128 reset_hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130}
1131
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001132/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 * usb_disable_device - Disable all the endpoints for a USB device
1134 * @dev: the device whose endpoints are being disabled
1135 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
1136 *
1137 * Disables all the device's endpoints, potentially including endpoint 0.
1138 * Deallocates hcd/hardware state for the endpoints (nuking all or most
1139 * pending urbs) and usbcore state for the interfaces, so that usbcore
1140 * must usb_set_configuration() before any interfaces could be used.
1141 */
1142void usb_disable_device(struct usb_device *dev, int skip_ep0)
1143{
1144 int i;
Sarah Sharpfccf4e82011-06-05 23:22:22 -07001145 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /* getting rid of interfaces will disconnect
1148 * any drivers bound to them (a key side effect)
1149 */
1150 if (dev->actconfig) {
Alan Sternca5c4852011-07-06 17:03:45 -04001151 /*
1152 * FIXME: In order to avoid self-deadlock involving the
1153 * bandwidth_mutex, we have to mark all the interfaces
1154 * before unregistering any of them.
1155 */
1156 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++)
1157 dev->actconfig->interface[i]->unregistering = 1;
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1160 struct usb_interface *interface;
1161
Alan Stern86d30742005-07-29 12:17:16 -07001162 /* remove this interface if it has been registered */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 interface = dev->actconfig->interface[i];
Daniel Ritzd305ef52005-09-22 00:47:24 -07001164 if (!device_is_registered(&interface->dev))
Alan Stern86d30742005-07-29 12:17:16 -07001165 continue;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001166 dev_dbg(&dev->dev, "unregistering interface %s\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001167 dev_name(&interface->dev));
Alan Stern3b23dd62008-12-05 14:10:34 -05001168 remove_intf_ep_devs(interface);
Alan Stern1a211752008-07-30 11:31:50 -04001169 device_del(&interface->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
1171
1172 /* Now that the interfaces are unbound, nobody should
1173 * try to access them.
1174 */
1175 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001176 put_device(&dev->actconfig->interface[i]->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 dev->actconfig->interface[i] = NULL;
1178 }
Sarah Sharpf468f7b2013-10-08 08:28:43 -07001179
1180 if (dev->usb2_hw_lpm_enabled == 1)
1181 usb_set_usb2_hardware_lpm(dev, 0);
Sarah Sharp24971912012-07-05 14:09:30 -07001182 usb_unlocked_disable_lpm(dev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07001183 usb_disable_ltm(dev);
Sarah Sharpf468f7b2013-10-08 08:28:43 -07001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 dev->actconfig = NULL;
1186 if (dev->state == USB_STATE_CONFIGURED)
1187 usb_set_device_state(dev, USB_STATE_ADDRESS);
1188 }
Alan Stern80f0cf32010-09-30 15:16:23 -04001189
1190 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __func__,
1191 skip_ep0 ? "non-ep0" : "all");
Sarah Sharpfccf4e82011-06-05 23:22:22 -07001192 if (hcd->driver->check_bandwidth) {
1193 /* First pass: Cancel URBs, leave endpoint pointers intact. */
1194 for (i = skip_ep0; i < 16; ++i) {
1195 usb_disable_endpoint(dev, i, false);
1196 usb_disable_endpoint(dev, i + USB_DIR_IN, false);
1197 }
1198 /* Remove endpoints from the host controller internal state */
Alan Stern8963c482012-04-17 15:22:39 -04001199 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharpfccf4e82011-06-05 23:22:22 -07001200 usb_hcd_alloc_bandwidth(dev, NULL, NULL, NULL);
Alan Stern8963c482012-04-17 15:22:39 -04001201 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharpfccf4e82011-06-05 23:22:22 -07001202 /* Second pass: remove endpoint pointers */
1203 }
Alan Stern80f0cf32010-09-30 15:16:23 -04001204 for (i = skip_ep0; i < 16; ++i) {
1205 usb_disable_endpoint(dev, i, true);
1206 usb_disable_endpoint(dev, i + USB_DIR_IN, true);
1207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001210/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 * usb_enable_endpoint - Enable an endpoint for USB communications
1212 * @dev: the device whose interface is being enabled
1213 * @ep: the endpoint
David Vrabel3444b262009-04-08 17:36:28 +00001214 * @reset_ep: flag to reset the endpoint state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 *
David Vrabel3444b262009-04-08 17:36:28 +00001216 * Resets the endpoint state if asked, and sets dev->ep_{in,out} pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 * For control endpoints, both the input and output sides are handled.
1218 */
Alan Stern2caf7fc2008-12-31 11:31:33 -05001219void usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep,
David Vrabel3444b262009-04-08 17:36:28 +00001220 bool reset_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Alan Sternbdd016b2007-07-30 17:05:22 -04001222 int epnum = usb_endpoint_num(&ep->desc);
1223 int is_out = usb_endpoint_dir_out(&ep->desc);
1224 int is_control = usb_endpoint_xfer_control(&ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
David Vrabel3444b262009-04-08 17:36:28 +00001226 if (reset_ep)
1227 usb_hcd_reset_endpoint(dev, ep);
1228 if (is_out || is_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 dev->ep_out[epnum] = ep;
David Vrabel3444b262009-04-08 17:36:28 +00001230 if (!is_out || is_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 dev->ep_in[epnum] = ep;
Alan Sternbdd016b2007-07-30 17:05:22 -04001232 ep->enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001235/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 * usb_enable_interface - Enable all the endpoints for an interface
1237 * @dev: the device whose interface is being enabled
1238 * @intf: pointer to the interface descriptor
David Vrabel3444b262009-04-08 17:36:28 +00001239 * @reset_eps: flag to reset the endpoints' state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 *
1241 * Enables all the endpoints for the interface's current altsetting.
1242 */
Alan Stern2caf7fc2008-12-31 11:31:33 -05001243void usb_enable_interface(struct usb_device *dev,
David Vrabel3444b262009-04-08 17:36:28 +00001244 struct usb_interface *intf, bool reset_eps)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 struct usb_host_interface *alt = intf->cur_altsetting;
1247 int i;
1248
1249 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
David Vrabel3444b262009-04-08 17:36:28 +00001250 usb_enable_endpoint(dev, &alt->endpoint[i], reset_eps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
1253/**
1254 * usb_set_interface - Makes a particular alternate setting be current
1255 * @dev: the device whose interface is being updated
1256 * @interface: the interface being updated
1257 * @alternate: the setting being chosen.
1258 * Context: !in_interrupt ()
1259 *
1260 * This is used to enable data transfers on interfaces that may not
1261 * be enabled by default. Not all devices support such configurability.
1262 * Only the driver bound to an interface may change its setting.
1263 *
1264 * Within any given configuration, each interface may have several
1265 * alternative settings. These are often used to control levels of
1266 * bandwidth consumption. For example, the default setting for a high
1267 * speed interrupt endpoint may not send more than 64 bytes per microframe,
1268 * while interrupt transfers of up to 3KBytes per microframe are legal.
1269 * Also, isochronous endpoints may never be part of an
1270 * interface's default setting. To access such bandwidth, alternate
1271 * interface settings must be made current.
1272 *
1273 * Note that in the Linux USB subsystem, bandwidth associated with
1274 * an endpoint in a given alternate setting is not reserved until an URB
1275 * is submitted that needs that bandwidth. Some other operating systems
1276 * allocate bandwidth early, when a configuration is chosen.
1277 *
1278 * This call is synchronous, and may not be used in an interrupt context.
1279 * Also, drivers must not change altsettings while urbs are scheduled for
1280 * endpoints in that interface; all such urbs must first be completed
1281 * (perhaps forced by unlinking).
1282 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02001283 * Return: Zero on success, or else the status code returned by the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 * underlying usb_control_msg() call.
1285 */
1286int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1287{
1288 struct usb_interface *iface;
1289 struct usb_host_interface *alt;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001290 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
Hans de Goede7a7b5622013-11-08 16:37:26 +01001291 int i, ret, manual = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001292 unsigned int epaddr;
1293 unsigned int pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 if (dev->state == USB_STATE_SUSPENDED)
1296 return -EHOSTUNREACH;
1297
1298 iface = usb_ifnum_to_if(dev, interface);
1299 if (!iface) {
1300 dev_dbg(&dev->dev, "selecting invalid interface %d\n",
1301 interface);
1302 return -EINVAL;
1303 }
Alan Sterne534c5b2011-07-01 16:43:02 -04001304 if (iface->unregistering)
1305 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 alt = usb_altnum_to_altsetting(iface, alternate);
1308 if (!alt) {
Thadeu Lima de Souza Cascardo385f6902010-01-17 19:24:03 -02001309 dev_warn(&dev->dev, "selecting invalid altsetting %d\n",
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -07001310 alternate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return -EINVAL;
1312 }
1313
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001314 /* Make sure we have enough bandwidth for this alternate interface.
1315 * Remove the current alt setting and add the new alt setting.
1316 */
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001317 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp83060952012-05-02 14:25:52 -07001318 /* Disable LPM, and re-enable it once the new alt setting is installed,
1319 * so that the xHCI driver can recalculate the U1/U2 timeouts.
1320 */
1321 if (usb_disable_lpm(dev)) {
1322 dev_err(&iface->dev, "%s Failed to disable LPM\n.", __func__);
1323 mutex_unlock(hcd->bandwidth_mutex);
1324 return -ENOMEM;
1325 }
Hans de Goede7a7b5622013-11-08 16:37:26 +01001326 /* Changing alt-setting also frees any allocated streams */
1327 for (i = 0; i < iface->cur_altsetting->desc.bNumEndpoints; i++)
1328 iface->cur_altsetting->endpoint[i].streams = 0;
1329
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001330 ret = usb_hcd_alloc_bandwidth(dev, NULL, iface->cur_altsetting, alt);
1331 if (ret < 0) {
1332 dev_info(&dev->dev, "Not enough bandwidth for altsetting %d\n",
1333 alternate);
Sarah Sharp83060952012-05-02 14:25:52 -07001334 usb_enable_lpm(dev);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001335 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001336 return ret;
1337 }
1338
Alan Stern392e1d92008-03-11 10:20:12 -04001339 if (dev->quirks & USB_QUIRK_NO_SET_INTF)
1340 ret = -EPIPE;
1341 else
1342 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1344 alternate, interface, NULL, 0, 5000);
1345
1346 /* 9.4.10 says devices don't need this and are free to STALL the
1347 * request if the interface only has one alternate setting.
1348 */
1349 if (ret == -EPIPE && iface->num_altsetting == 1) {
1350 dev_dbg(&dev->dev,
1351 "manual set_interface for iface %d, alt %d\n",
1352 interface, alternate);
1353 manual = 1;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001354 } else if (ret < 0) {
1355 /* Re-instate the old alt setting */
1356 usb_hcd_alloc_bandwidth(dev, NULL, alt, iface->cur_altsetting);
Sarah Sharp83060952012-05-02 14:25:52 -07001357 usb_enable_lpm(dev);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001358 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return ret;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001360 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001361 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 /* FIXME drivers shouldn't need to replicate/bugfix the logic here
1364 * when they implement async or easily-killable versions of this or
1365 * other "should-be-internal" functions (like clear_halt).
1366 * should hcd+usbcore postprocess control requests?
1367 */
1368
1369 /* prevent submissions using previous endpoint settings */
Alan Stern3b23dd62008-12-05 14:10:34 -05001370 if (iface->cur_altsetting != alt) {
1371 remove_intf_ep_devs(iface);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001372 usb_remove_sysfs_intf_files(iface);
Alan Stern3b23dd62008-12-05 14:10:34 -05001373 }
Alan Sternddeac4e72009-01-15 17:03:33 -05001374 usb_disable_interface(dev, iface, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 iface->cur_altsetting = alt;
1377
Sarah Sharp83060952012-05-02 14:25:52 -07001378 /* Now that the interface is installed, re-enable LPM. */
1379 usb_unlocked_enable_lpm(dev);
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 /* If the interface only has one altsetting and the device didn't
David Brownella81e7ec2005-04-18 17:39:25 -07001382 * accept the request, we attempt to carry out the equivalent action
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 * by manually clearing the HALT feature for each endpoint in the
1384 * new altsetting.
1385 */
1386 if (manual) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001388 epaddr = alt->endpoint[i].desc.bEndpointAddress;
1389 pipe = __create_pipe(dev,
1390 USB_ENDPOINT_NUMBER_MASK & epaddr) |
1391 (usb_endpoint_out(epaddr) ?
1392 USB_DIR_OUT : USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 usb_clear_halt(dev, pipe);
1395 }
1396 }
1397
1398 /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
1399 *
1400 * Note:
1401 * Despite EP0 is always present in all interfaces/AS, the list of
1402 * endpoints from the descriptor does not contain EP0. Due to its
1403 * omnipresence one might expect EP0 being considered "affected" by
1404 * any SetInterface request and hence assume toggles need to be reset.
1405 * However, EP0 toggles are re-synced for every individual transfer
1406 * during the SETUP stage - hence EP0 toggles are "don't care" here.
1407 * (Likewise, EP0 never "halts" on well designed devices.)
1408 */
Alan Stern2caf7fc2008-12-31 11:31:33 -05001409 usb_enable_interface(dev, iface, true);
Alan Stern3b23dd62008-12-05 14:10:34 -05001410 if (device_is_registered(&iface->dev)) {
Alan Stern0e6c8e82005-10-24 15:33:03 -04001411 usb_create_sysfs_intf_files(iface);
Alan Stern3b23dd62008-12-05 14:10:34 -05001412 create_intf_ep_devs(iface);
1413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return 0;
1415}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001416EXPORT_SYMBOL_GPL(usb_set_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418/**
Hemant Kumarad400622017-08-18 17:54:58 -07001419 * usb_set_interface_timeout - Makes a particular alternate setting be current
1420 * and allows to set a timout value for this control transfer.
1421 * @dev: the device whose interface is being updated
1422 * @interface: the interface being updated
1423 * @alternate: the setting being chosen.
1424 * @timeout: timeout value in ms (non-zero), before which this transfer should
1425 * complete.
1426 * Context: !in_interrupt ()
1427 *
1428 * This is used to enable data transfers on interfaces that may not
1429 * be enabled by default. Not all devices support such configurability.
1430 * Only the driver bound to an interface may change its setting.
1431 *
1432 * Within any given configuration, each interface may have several
1433 * alternative settings. These are often used to control levels of
1434 * bandwidth consumption. For example, the default setting for a high
1435 * speed interrupt endpoint may not send more than 64 bytes per microframe,
1436 * while interrupt transfers of up to 3KBytes per microframe are legal.
1437 * Also, isochronous endpoints may never be part of an
1438 * interface's default setting. To access such bandwidth, alternate
1439 * interface settings must be made current.
1440 *
1441 * Note that in the Linux USB subsystem, bandwidth associated with
1442 * an endpoint in a given alternate setting is not reserved until an URB
1443 * is submitted that needs that bandwidth. Some other operating systems
1444 * allocate bandwidth early, when a configuration is chosen.
1445 *
1446 * This call is synchronous, and may not be used in an interrupt context.
1447 * Also, drivers must not change altsettings while urbs are scheduled for
1448 * endpoints in that interface; all such urbs must first be completed
1449 * (perhaps forced by unlinking).
1450 *
1451 * Return: Zero on success, or else the status code returned by the
1452 * underlying usb_control_msg() call.
1453 */
1454int usb_set_interface_timeout(struct usb_device *dev, int interface,
1455 int alternate, unsigned long timeout_ms)
1456{
1457 struct usb_interface *iface;
1458 struct usb_host_interface *alt;
1459 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
1460 int i, ret, manual = 0;
1461 unsigned int epaddr;
1462 unsigned int pipe;
1463
1464 if (dev->state == USB_STATE_SUSPENDED)
1465 return -EHOSTUNREACH;
1466
1467 iface = usb_ifnum_to_if(dev, interface);
1468 if (!iface) {
1469 dev_dbg(&dev->dev, "selecting invalid interface %d\n",
1470 interface);
1471 return -EINVAL;
1472 }
1473 if (iface->unregistering)
1474 return -ENODEV;
1475
1476 alt = usb_altnum_to_altsetting(iface, alternate);
1477 if (!alt) {
1478 dev_warn(&dev->dev, "selecting invalid altsetting %d\n",
1479 alternate);
1480 return -EINVAL;
1481 }
1482
1483 /* Make sure we have enough bandwidth for this alternate interface.
1484 * Remove the current alt setting and add the new alt setting.
1485 */
1486 mutex_lock(hcd->bandwidth_mutex);
1487 /* Disable LPM, and re-enable it once the new alt setting is installed,
1488 * so that the xHCI driver can recalculate the U1/U2 timeouts.
1489 */
1490 if (usb_disable_lpm(dev)) {
1491 dev_err(&iface->dev, "%s Failed to disable LPM\n.", __func__);
1492 mutex_unlock(hcd->bandwidth_mutex);
1493 return -ENOMEM;
1494 }
1495 /* Changing alt-setting also frees any allocated streams */
1496 for (i = 0; i < iface->cur_altsetting->desc.bNumEndpoints; i++)
1497 iface->cur_altsetting->endpoint[i].streams = 0;
1498
1499 ret = usb_hcd_alloc_bandwidth(dev, NULL, iface->cur_altsetting, alt);
1500 if (ret < 0) {
1501 dev_info(&dev->dev, "Not enough bandwidth for altsetting %d\n",
1502 alternate);
1503 usb_enable_lpm(dev);
1504 mutex_unlock(hcd->bandwidth_mutex);
1505 return ret;
1506 }
1507
1508 if (dev->quirks & USB_QUIRK_NO_SET_INTF)
1509 ret = -EPIPE;
1510 else
1511 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1512 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1513 alternate, interface, NULL, 0, timeout_ms);
1514
1515 /* 9.4.10 says devices don't need this and are free to STALL the
1516 * request if the interface only has one alternate setting.
1517 */
1518 if (ret == -EPIPE && iface->num_altsetting == 1) {
1519 dev_dbg(&dev->dev,
1520 "manual set_interface for iface %d, alt %d\n",
1521 interface, alternate);
1522 manual = 1;
1523 } else if (ret < 0) {
1524 /* Re-instate the old alt setting */
1525 usb_hcd_alloc_bandwidth(dev, NULL, alt, iface->cur_altsetting);
1526 usb_enable_lpm(dev);
1527 mutex_unlock(hcd->bandwidth_mutex);
1528 return ret;
1529 }
1530 mutex_unlock(hcd->bandwidth_mutex);
1531
1532 /* FIXME drivers shouldn't need to replicate/bugfix the logic here
1533 * when they implement async or easily-killable versions of this or
1534 * other "should-be-internal" functions (like clear_halt).
1535 * should hcd+usbcore postprocess control requests?
1536 */
1537
1538 /* prevent submissions using previous endpoint settings */
1539 if (iface->cur_altsetting != alt) {
1540 remove_intf_ep_devs(iface);
1541 usb_remove_sysfs_intf_files(iface);
1542 }
1543 usb_disable_interface(dev, iface, true);
1544
1545 iface->cur_altsetting = alt;
1546
1547 /* Now that the interface is installed, re-enable LPM. */
1548 usb_unlocked_enable_lpm(dev);
1549
1550 /* If the interface only has one altsetting and the device didn't
1551 * accept the request, we attempt to carry out the equivalent action
1552 * by manually clearing the HALT feature for each endpoint in the
1553 * new altsetting.
1554 */
1555 if (manual) {
1556 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
1557 epaddr = alt->endpoint[i].desc.bEndpointAddress;
1558 pipe = __create_pipe(dev,
1559 USB_ENDPOINT_NUMBER_MASK & epaddr) |
1560 (usb_endpoint_out(epaddr) ?
1561 USB_DIR_OUT : USB_DIR_IN);
1562
1563 usb_clear_halt(dev, pipe);
1564 }
1565 }
1566
1567 /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
1568 *
1569 * Note:
1570 * Despite EP0 is always present in all interfaces/AS, the list of
1571 * endpoints from the descriptor does not contain EP0. Due to its
1572 * omnipresence one might expect EP0 being considered "affected" by
1573 * any SetInterface request and hence assume toggles need to be reset.
1574 * However, EP0 toggles are re-synced for every individual transfer
1575 * during the SETUP stage - hence EP0 toggles are "don't care" here.
1576 * (Likewise, EP0 never "halts" on well designed devices.)
1577 */
1578 usb_enable_interface(dev, iface, true);
1579 if (device_is_registered(&iface->dev)) {
1580 usb_create_sysfs_intf_files(iface);
1581 create_intf_ep_devs(iface);
1582 }
1583 return 0;
1584}
1585EXPORT_SYMBOL(usb_set_interface_timeout);
1586
1587/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 * usb_reset_configuration - lightweight device reset
1589 * @dev: the device whose configuration is being reset
1590 *
1591 * This issues a standard SET_CONFIGURATION request to the device using
1592 * the current configuration. The effect is to reset most USB-related
1593 * state in the device, including interface altsettings (reset to zero),
David Vrabel3444b262009-04-08 17:36:28 +00001594 * endpoint halts (cleared), and endpoint state (only for bulk and interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 * endpoints). Other usbcore state is unchanged, including bindings of
1596 * usb device drivers to interfaces.
1597 *
1598 * Because this affects multiple interfaces, avoid using this with composite
1599 * (multi-interface) devices. Instead, the driver for each interface may
David Brownella81e7ec2005-04-18 17:39:25 -07001600 * use usb_set_interface() on the interfaces it claims. Be careful though;
1601 * some devices don't support the SET_INTERFACE request, and others won't
David Vrabel3444b262009-04-08 17:36:28 +00001602 * reset all the interface state (notably endpoint state). Resetting the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 * configuration would affect other drivers' interfaces.
1604 *
1605 * The caller must own the device lock.
1606 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02001607 * Return: Zero on success, else a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 */
1609int usb_reset_configuration(struct usb_device *dev)
1610{
1611 int i, retval;
1612 struct usb_host_config *config;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001613 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 if (dev->state == USB_STATE_SUSPENDED)
1616 return -EHOSTUNREACH;
1617
1618 /* caller must have locked the device and must own
1619 * the usb bus readlock (so driver bindings are stable);
1620 * calls during probe() are fine
1621 */
1622
1623 for (i = 1; i < 16; ++i) {
Alan Sternddeac4e72009-01-15 17:03:33 -05001624 usb_disable_endpoint(dev, i, true);
1625 usb_disable_endpoint(dev, i + USB_DIR_IN, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
1627
1628 config = dev->actconfig;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001629 retval = 0;
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001630 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp83060952012-05-02 14:25:52 -07001631 /* Disable LPM, and re-enable it once the configuration is reset, so
1632 * that the xHCI driver can recalculate the U1/U2 timeouts.
1633 */
1634 if (usb_disable_lpm(dev)) {
1635 dev_err(&dev->dev, "%s Failed to disable LPM\n.", __func__);
1636 mutex_unlock(hcd->bandwidth_mutex);
1637 return -ENOMEM;
1638 }
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001639 /* Make sure we have enough bandwidth for each alternate setting 0 */
1640 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1641 struct usb_interface *intf = config->interface[i];
1642 struct usb_host_interface *alt;
1643
1644 alt = usb_altnum_to_altsetting(intf, 0);
1645 if (!alt)
1646 alt = &intf->altsetting[0];
1647 if (alt != intf->cur_altsetting)
1648 retval = usb_hcd_alloc_bandwidth(dev, NULL,
1649 intf->cur_altsetting, alt);
1650 if (retval < 0)
1651 break;
1652 }
1653 /* If not, reinstate the old alternate settings */
1654 if (retval < 0) {
1655reset_old_alts:
Roel Kluine4a3d942010-02-18 02:36:23 +01001656 for (i--; i >= 0; i--) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001657 struct usb_interface *intf = config->interface[i];
1658 struct usb_host_interface *alt;
1659
1660 alt = usb_altnum_to_altsetting(intf, 0);
1661 if (!alt)
1662 alt = &intf->altsetting[0];
1663 if (alt != intf->cur_altsetting)
1664 usb_hcd_alloc_bandwidth(dev, NULL,
1665 alt, intf->cur_altsetting);
1666 }
Sarah Sharp83060952012-05-02 14:25:52 -07001667 usb_enable_lpm(dev);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001668 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001669 return retval;
1670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1672 USB_REQ_SET_CONFIGURATION, 0,
1673 config->desc.bConfigurationValue, 0,
1674 NULL, 0, USB_CTRL_SET_TIMEOUT);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001675 if (retval < 0)
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001676 goto reset_old_alts;
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001677 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 /* re-init hc/hcd interface/endpoint state */
1680 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1681 struct usb_interface *intf = config->interface[i];
1682 struct usb_host_interface *alt;
1683
1684 alt = usb_altnum_to_altsetting(intf, 0);
1685
1686 /* No altsetting 0? We'll assume the first altsetting.
1687 * We could use a GetInterface call, but if a device is
1688 * so non-compliant that it doesn't have altsetting 0
1689 * then I wouldn't trust its reply anyway.
1690 */
1691 if (!alt)
1692 alt = &intf->altsetting[0];
1693
Alan Stern3b23dd62008-12-05 14:10:34 -05001694 if (alt != intf->cur_altsetting) {
1695 remove_intf_ep_devs(intf);
1696 usb_remove_sysfs_intf_files(intf);
1697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 intf->cur_altsetting = alt;
Alan Stern2caf7fc2008-12-31 11:31:33 -05001699 usb_enable_interface(dev, intf, true);
Alan Stern3b23dd62008-12-05 14:10:34 -05001700 if (device_is_registered(&intf->dev)) {
Alan Stern0e6c8e82005-10-24 15:33:03 -04001701 usb_create_sysfs_intf_files(intf);
Alan Stern3b23dd62008-12-05 14:10:34 -05001702 create_intf_ep_devs(intf);
1703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
Sarah Sharp83060952012-05-02 14:25:52 -07001705 /* Now that the interfaces are installed, re-enable LPM. */
1706 usb_unlocked_enable_lpm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 return 0;
1708}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001709EXPORT_SYMBOL_GPL(usb_reset_configuration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Greg Kroah-Hartmanb0e396e2007-08-02 22:44:27 -06001711static void usb_release_interface(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 struct usb_interface *intf = to_usb_interface(dev);
1714 struct usb_interface_cache *intfc =
1715 altsetting_to_usb_interface_cache(intf->altsetting);
1716
1717 kref_put(&intfc->ref, usb_release_interface_cache);
Alan Stern524134d2015-01-21 14:02:43 -05001718 usb_put_dev(interface_to_usbdev(intf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 kfree(intf);
1720}
1721
Stefan Kochb3910ce2015-08-25 21:10:08 +02001722/*
1723 * usb_deauthorize_interface - deauthorize an USB interface
1724 *
1725 * @intf: USB interface structure
1726 */
1727void usb_deauthorize_interface(struct usb_interface *intf)
1728{
1729 struct device *dev = &intf->dev;
1730
1731 device_lock(dev->parent);
1732
1733 if (intf->authorized) {
1734 device_lock(dev);
1735 intf->authorized = 0;
1736 device_unlock(dev);
1737
1738 usb_forced_unbind_intf(intf);
1739 }
1740
1741 device_unlock(dev->parent);
1742}
1743
1744/*
1745 * usb_authorize_interface - authorize an USB interface
1746 *
1747 * @intf: USB interface structure
1748 */
1749void usb_authorize_interface(struct usb_interface *intf)
1750{
1751 struct device *dev = &intf->dev;
1752
1753 if (!intf->authorized) {
1754 device_lock(dev);
1755 intf->authorized = 1; /* authorize interface */
1756 device_unlock(dev);
1757 }
1758}
1759
Kay Sievers7eff2e72007-08-14 15:15:12 +02001760static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001761{
1762 struct usb_device *usb_dev;
1763 struct usb_interface *intf;
1764 struct usb_host_interface *alt;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001765
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001766 intf = to_usb_interface(dev);
1767 usb_dev = interface_to_usbdev(intf);
1768 alt = intf->cur_altsetting;
1769
Kay Sievers7eff2e72007-08-14 15:15:12 +02001770 if (add_uevent_var(env, "INTERFACE=%d/%d/%d",
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001771 alt->desc.bInterfaceClass,
1772 alt->desc.bInterfaceSubClass,
1773 alt->desc.bInterfaceProtocol))
1774 return -ENOMEM;
1775
Kay Sievers7eff2e72007-08-14 15:15:12 +02001776 if (add_uevent_var(env,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001777 "MODALIAS=usb:"
Bjørn Mork81df2d52012-05-18 21:27:43 +02001778 "v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02Xin%02X",
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001779 le16_to_cpu(usb_dev->descriptor.idVendor),
1780 le16_to_cpu(usb_dev->descriptor.idProduct),
1781 le16_to_cpu(usb_dev->descriptor.bcdDevice),
1782 usb_dev->descriptor.bDeviceClass,
1783 usb_dev->descriptor.bDeviceSubClass,
1784 usb_dev->descriptor.bDeviceProtocol,
1785 alt->desc.bInterfaceClass,
1786 alt->desc.bInterfaceSubClass,
Bjørn Mork81df2d52012-05-18 21:27:43 +02001787 alt->desc.bInterfaceProtocol,
1788 alt->desc.bInterfaceNumber))
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001789 return -ENOMEM;
1790
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001791 return 0;
1792}
1793
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001794struct device_type usb_if_device_type = {
1795 .name = "usb_interface",
1796 .release = usb_release_interface,
1797 .uevent = usb_if_uevent,
1798};
1799
Craig W. Nadler165fe972007-06-15 23:14:35 -04001800static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001801 struct usb_host_config *config,
1802 u8 inum)
Craig W. Nadler165fe972007-06-15 23:14:35 -04001803{
1804 struct usb_interface_assoc_descriptor *retval = NULL;
1805 struct usb_interface_assoc_descriptor *intf_assoc;
1806 int first_intf;
1807 int last_intf;
1808 int i;
1809
1810 for (i = 0; (i < USB_MAXIADS && config->intf_assoc[i]); i++) {
1811 intf_assoc = config->intf_assoc[i];
1812 if (intf_assoc->bInterfaceCount == 0)
1813 continue;
1814
1815 first_intf = intf_assoc->bFirstInterface;
1816 last_intf = first_intf + (intf_assoc->bInterfaceCount - 1);
1817 if (inum >= first_intf && inum <= last_intf) {
1818 if (!retval)
1819 retval = intf_assoc;
1820 else
1821 dev_err(&dev->dev, "Interface #%d referenced"
1822 " by multiple IADs\n", inum);
1823 }
1824 }
1825
1826 return retval;
1827}
1828
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001829
1830/*
1831 * Internal function to queue a device reset
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001832 * See usb_queue_reset_device() for more details
1833 */
Felipe Balbi09e81f32009-12-04 15:47:44 +02001834static void __usb_queue_reset_device(struct work_struct *ws)
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001835{
1836 int rc;
1837 struct usb_interface *iface =
1838 container_of(ws, struct usb_interface, reset_ws);
1839 struct usb_device *udev = interface_to_usbdev(iface);
1840
1841 rc = usb_lock_device_for_reset(udev, iface);
1842 if (rc >= 0) {
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001843 usb_reset_device(udev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001844 usb_unlock_device(udev);
1845 }
Alan Stern524134d2015-01-21 14:02:43 -05001846 usb_put_intf(iface); /* Undo _get_ in usb_queue_reset_device() */
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001847}
1848
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850/*
1851 * usb_set_configuration - Makes a particular device setting be current
1852 * @dev: the device whose configuration is being updated
1853 * @configuration: the configuration being chosen.
1854 * Context: !in_interrupt(), caller owns the device lock
1855 *
1856 * This is used to enable non-default device modes. Not all devices
1857 * use this kind of configurability; many devices only have one
1858 * configuration.
1859 *
Alan Stern3f141e22007-02-08 16:40:43 -05001860 * @configuration is the value of the configuration to be installed.
1861 * According to the USB spec (e.g. section 9.1.1.5), configuration values
1862 * must be non-zero; a value of zero indicates that the device in
1863 * unconfigured. However some devices erroneously use 0 as one of their
1864 * configuration values. To help manage such devices, this routine will
1865 * accept @configuration = -1 as indicating the device should be put in
1866 * an unconfigured state.
1867 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 * USB device configurations may affect Linux interoperability,
1869 * power consumption and the functionality available. For example,
1870 * the default configuration is limited to using 100mA of bus power,
1871 * so that when certain device functionality requires more power,
1872 * and the device is bus powered, that functionality should be in some
1873 * non-default device configuration. Other device modes may also be
1874 * reflected as configuration options, such as whether two ISDN
1875 * channels are available independently; and choosing between open
1876 * standard device protocols (like CDC) or proprietary ones.
1877 *
Inaky Perez-Gonzalez16bbab22007-07-31 20:34:01 -07001878 * Note that a non-authorized device (dev->authorized == 0) will only
1879 * be put in unconfigured mode.
1880 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 * Note that USB has an additional level of device configurability,
1882 * associated with interfaces. That configurability is accessed using
1883 * usb_set_interface().
1884 *
1885 * This call is synchronous. The calling context must be able to sleep,
1886 * must own the device lock, and must not hold the driver model's USB
Ming Lei6d243e52008-06-17 23:24:08 +08001887 * bus mutex; usb interface driver probe() methods cannot use this routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 *
1889 * Returns zero on success, or else the status code returned by the
Steven Cole093cf722005-05-03 19:07:24 -06001890 * underlying call that failed. On successful completion, each interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 * in the original device configuration has been destroyed, and each one
1892 * in the new configuration has been probed by all relevant usb device
1893 * drivers currently known to the kernel.
1894 */
1895int usb_set_configuration(struct usb_device *dev, int configuration)
1896{
1897 int i, ret;
1898 struct usb_host_config *cp = NULL;
1899 struct usb_interface **new_interfaces = NULL;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001900 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 int n, nintf;
1902
Inaky Perez-Gonzalez16bbab22007-07-31 20:34:01 -07001903 if (dev->authorized == 0 || configuration == -1)
Alan Stern3f141e22007-02-08 16:40:43 -05001904 configuration = 0;
1905 else {
1906 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1907 if (dev->config[i].desc.bConfigurationValue ==
1908 configuration) {
1909 cp = &dev->config[i];
1910 break;
1911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913 }
1914 if ((!cp && configuration != 0))
1915 return -EINVAL;
1916
1917 /* The USB spec says configuration 0 means unconfigured.
1918 * But if a device includes a configuration numbered 0,
1919 * we will accept it as a correctly configured state.
Alan Stern3f141e22007-02-08 16:40:43 -05001920 * Use -1 if you really want to unconfigure the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 */
1922 if (cp && configuration == 0)
1923 dev_warn(&dev->dev, "config 0 descriptor??\n");
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 /* Allocate memory for new interfaces before doing anything else,
1926 * so that if we run out then nothing will have changed. */
1927 n = nintf = 0;
1928 if (cp) {
1929 nintf = cp->desc.bNumInterfaces;
1930 new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
Oliver Neukumacbe2fe2010-01-12 12:32:50 +01001931 GFP_NOIO);
Wolfram Sang93fab792016-08-25 19:39:00 +02001932 if (!new_interfaces)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 for (; n < nintf; ++n) {
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001936 new_interfaces[n] = kzalloc(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 sizeof(struct usb_interface),
Oliver Neukumacbe2fe2010-01-12 12:32:50 +01001938 GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (!new_interfaces[n]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 ret = -ENOMEM;
1941free_interfaces:
1942 while (--n >= 0)
1943 kfree(new_interfaces[n]);
1944 kfree(new_interfaces);
1945 return ret;
1946 }
1947 }
Alan Stern6ad07122006-06-01 13:59:16 -04001948
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +01001949 i = dev->bus_mA - usb_get_max_power(dev, cp);
Alan Stern6ad07122006-06-01 13:59:16 -04001950 if (i < 0)
1951 dev_warn(&dev->dev, "new config #%d exceeds power "
1952 "limit by %dmA\n",
1953 configuration, -i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
1955
Alan Stern01d883d2006-08-30 15:47:18 -04001956 /* Wake up the device so we can send it the Set-Config request */
Alan Stern94fcda12006-11-20 11:38:46 -05001957 ret = usb_autoresume_device(dev);
Alan Stern01d883d2006-08-30 15:47:18 -04001958 if (ret)
1959 goto free_interfaces;
1960
Thadeu Lima de Souza Cascardo07919712010-08-28 03:06:29 -03001961 /* if it's already configured, clear out old state first.
1962 * getting rid of old interfaces means unbinding their drivers.
1963 */
1964 if (dev->state != USB_STATE_ADDRESS)
1965 usb_disable_device(dev, 1); /* Skip ep0 */
1966
1967 /* Get rid of pending async Set-Config requests for this device */
1968 cancel_async_set_config(dev);
1969
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001970 /* Make sure we have bandwidth (and available HCD resources) for this
1971 * configuration. Remove endpoints from the schedule if we're dropping
1972 * this configuration to set configuration 0. After this point, the
1973 * host controller will not allow submissions to dropped endpoints. If
1974 * this call fails, the device state is unchanged.
1975 */
Alan Stern8963c482012-04-17 15:22:39 -04001976 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp83060952012-05-02 14:25:52 -07001977 /* Disable LPM, and re-enable it once the new configuration is
1978 * installed, so that the xHCI driver can recalculate the U1/U2
1979 * timeouts.
1980 */
Sarah Sharp9cf65992012-07-03 23:22:38 -07001981 if (dev->actconfig && usb_disable_lpm(dev)) {
Sarah Sharp83060952012-05-02 14:25:52 -07001982 dev_err(&dev->dev, "%s Failed to disable LPM\n.", __func__);
1983 mutex_unlock(hcd->bandwidth_mutex);
Sachin Kamatc058f7a2012-11-21 11:01:19 +05301984 ret = -ENOMEM;
1985 goto free_interfaces;
Sarah Sharp83060952012-05-02 14:25:52 -07001986 }
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001987 ret = usb_hcd_alloc_bandwidth(dev, cp, NULL, NULL);
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001988 if (ret < 0) {
Sarah Sharp9cf65992012-07-03 23:22:38 -07001989 if (dev->actconfig)
1990 usb_enable_lpm(dev);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001991 mutex_unlock(hcd->bandwidth_mutex);
Thadeu Lima de Souza Cascardo07919712010-08-28 03:06:29 -03001992 usb_autosuspend_device(dev);
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001993 goto free_interfaces;
1994 }
1995
Alan Stern36caff52012-11-07 10:31:30 -05001996 /*
1997 * Initialize the new interface structures and the
Alan Stern6ad07122006-06-01 13:59:16 -04001998 * hc/hcd/usbcore interface/endpoint state.
1999 */
2000 for (i = 0; i < nintf; ++i) {
2001 struct usb_interface_cache *intfc;
2002 struct usb_interface *intf;
2003 struct usb_host_interface *alt;
2004
2005 cp->interface[i] = intf = new_interfaces[i];
2006 intfc = cp->intf_cache[i];
2007 intf->altsetting = intfc->altsetting;
2008 intf->num_altsetting = intfc->num_altsetting;
Stefan Koch6b2bd3c2015-08-25 21:10:06 +02002009 intf->authorized = !!HCD_INTF_AUTHORIZED(hcd);
Alan Stern6ad07122006-06-01 13:59:16 -04002010 kref_get(&intfc->ref);
2011
2012 alt = usb_altnum_to_altsetting(intf, 0);
2013
2014 /* No altsetting 0? We'll assume the first altsetting.
2015 * We could use a GetInterface call, but if a device is
2016 * so non-compliant that it doesn't have altsetting 0
2017 * then I wouldn't trust its reply anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 */
Alan Stern6ad07122006-06-01 13:59:16 -04002019 if (!alt)
2020 alt = &intf->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Daniel Mackb3a3dd02012-06-12 20:23:52 +02002022 intf->intf_assoc =
2023 find_iad(dev, cp, alt->desc.bInterfaceNumber);
Alan Stern6ad07122006-06-01 13:59:16 -04002024 intf->cur_altsetting = alt;
Alan Stern2caf7fc2008-12-31 11:31:33 -05002025 usb_enable_interface(dev, intf, true);
Alan Stern6ad07122006-06-01 13:59:16 -04002026 intf->dev.parent = &dev->dev;
2027 intf->dev.driver = NULL;
2028 intf->dev.bus = &usb_bus_type;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002029 intf->dev.type = &usb_if_device_type;
Alan Stern2e5f10e2008-04-30 15:37:19 -04002030 intf->dev.groups = usb_interface_groups;
Roger Quadrosb44bbc42016-09-13 11:16:03 +03002031 /*
2032 * Please refer to usb_alloc_dev() to see why we set
2033 * dma_mask and dma_pfn_offset.
2034 */
Alan Stern6ad07122006-06-01 13:59:16 -04002035 intf->dev.dma_mask = dev->dev.dma_mask;
Roger Quadrosb44bbc42016-09-13 11:16:03 +03002036 intf->dev.dma_pfn_offset = dev->dev.dma_pfn_offset;
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08002037 INIT_WORK(&intf->reset_ws, __usb_queue_reset_device);
Alan Stern0026e002010-09-21 15:01:53 -04002038 intf->minor = -1;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08002039 device_initialize(&intf->dev);
Ming Lei63defa72010-11-15 15:56:54 -05002040 pm_runtime_no_callbacks(&intf->dev);
Kay Sievers0031a062008-05-02 06:02:41 +02002041 dev_set_name(&intf->dev, "%d-%s:%d.%d",
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08002042 dev->bus->busnum, dev->devpath,
2043 configuration, alt->desc.bInterfaceNumber);
Alan Stern524134d2015-01-21 14:02:43 -05002044 usb_get_dev(dev);
Alan Stern6ad07122006-06-01 13:59:16 -04002045 }
2046 kfree(new_interfaces);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Alan Stern36caff52012-11-07 10:31:30 -05002048 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
2049 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
2050 NULL, 0, USB_CTRL_SET_TIMEOUT);
2051 if (ret < 0 && cp) {
2052 /*
2053 * All the old state is gone, so what else can we do?
2054 * The device is probably useless now anyway.
2055 */
2056 usb_hcd_alloc_bandwidth(dev, NULL, NULL, NULL);
2057 for (i = 0; i < nintf; ++i) {
2058 usb_disable_interface(dev, cp->interface[i], true);
2059 put_device(&cp->interface[i]->dev);
2060 cp->interface[i] = NULL;
2061 }
2062 cp = NULL;
2063 }
2064
2065 dev->actconfig = cp;
2066 mutex_unlock(hcd->bandwidth_mutex);
2067
2068 if (!cp) {
2069 usb_set_device_state(dev, USB_STATE_ADDRESS);
2070
2071 /* Leave LPM disabled while the device is unconfigured. */
2072 usb_autosuspend_device(dev);
2073 return ret;
2074 }
2075 usb_set_device_state(dev, USB_STATE_CONFIGURED);
2076
Alan Stern1662e3a2009-03-18 14:28:53 -04002077 if (cp->string == NULL &&
2078 !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
Alan Stern6ad07122006-06-01 13:59:16 -04002079 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Sarah Sharp83060952012-05-02 14:25:52 -07002081 /* Now that the interfaces are installed, re-enable LPM. */
2082 usb_unlocked_enable_lpm(dev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07002083 /* Enable LTM if it was turned off by usb_disable_device. */
2084 usb_enable_ltm(dev);
Sarah Sharp83060952012-05-02 14:25:52 -07002085
Alan Stern6ad07122006-06-01 13:59:16 -04002086 /* Now that all the interfaces are set up, register them
2087 * to trigger binding of drivers to interfaces. probe()
2088 * routines may install different altsettings and may
2089 * claim() any interfaces not yet bound. Many class drivers
2090 * need that: CDC, audio, video, etc.
2091 */
2092 for (i = 0; i < nintf; ++i) {
2093 struct usb_interface *intf = cp->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08002095 dev_dbg(&dev->dev,
Alan Stern6ad07122006-06-01 13:59:16 -04002096 "adding %s (config #%d, interface %d)\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02002097 dev_name(&intf->dev), configuration,
Alan Stern6ad07122006-06-01 13:59:16 -04002098 intf->cur_altsetting->desc.bInterfaceNumber);
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002099 device_enable_async_suspend(&intf->dev);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08002100 ret = device_add(&intf->dev);
Alan Stern6ad07122006-06-01 13:59:16 -04002101 if (ret != 0) {
2102 dev_err(&dev->dev, "device_add(%s) --> %d\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02002103 dev_name(&intf->dev), ret);
Alan Stern6ad07122006-06-01 13:59:16 -04002104 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
Alan Stern3b23dd62008-12-05 14:10:34 -05002106 create_intf_ep_devs(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
2108
Alan Stern94fcda12006-11-20 11:38:46 -05002109 usb_autosuspend_device(dev);
Alan Stern86d30742005-07-29 12:17:16 -07002110 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
Valentina Maneab7945b72014-01-23 23:12:29 +02002112EXPORT_SYMBOL_GPL(usb_set_configuration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Alan Sterndf718962008-12-19 10:27:56 -05002114static LIST_HEAD(set_config_list);
2115static DEFINE_SPINLOCK(set_config_lock);
2116
Alan Stern088dc272006-08-21 12:08:19 -04002117struct set_config_request {
2118 struct usb_device *udev;
2119 int config;
2120 struct work_struct work;
Alan Sterndf718962008-12-19 10:27:56 -05002121 struct list_head node;
Alan Stern088dc272006-08-21 12:08:19 -04002122};
2123
2124/* Worker routine for usb_driver_set_configuration() */
David Howellsc4028952006-11-22 14:57:56 +00002125static void driver_set_config_work(struct work_struct *work)
Alan Stern088dc272006-08-21 12:08:19 -04002126{
David Howellsc4028952006-11-22 14:57:56 +00002127 struct set_config_request *req =
2128 container_of(work, struct set_config_request, work);
Alan Sterndf718962008-12-19 10:27:56 -05002129 struct usb_device *udev = req->udev;
Alan Stern088dc272006-08-21 12:08:19 -04002130
Alan Sterndf718962008-12-19 10:27:56 -05002131 usb_lock_device(udev);
2132 spin_lock(&set_config_lock);
2133 list_del(&req->node);
2134 spin_unlock(&set_config_lock);
2135
2136 if (req->config >= -1) /* Is req still valid? */
2137 usb_set_configuration(udev, req->config);
2138 usb_unlock_device(udev);
2139 usb_put_dev(udev);
Alan Stern088dc272006-08-21 12:08:19 -04002140 kfree(req);
2141}
2142
Alan Sterndf718962008-12-19 10:27:56 -05002143/* Cancel pending Set-Config requests for a device whose configuration
2144 * was just changed
2145 */
2146static void cancel_async_set_config(struct usb_device *udev)
2147{
2148 struct set_config_request *req;
2149
2150 spin_lock(&set_config_lock);
2151 list_for_each_entry(req, &set_config_list, node) {
2152 if (req->udev == udev)
2153 req->config = -999; /* Mark as cancelled */
2154 }
2155 spin_unlock(&set_config_lock);
2156}
2157
Alan Stern088dc272006-08-21 12:08:19 -04002158/**
2159 * usb_driver_set_configuration - Provide a way for drivers to change device configurations
2160 * @udev: the device whose configuration is being updated
2161 * @config: the configuration being chosen.
2162 * Context: In process context, must be able to sleep
2163 *
2164 * Device interface drivers are not allowed to change device configurations.
2165 * This is because changing configurations will destroy the interface the
2166 * driver is bound to and create new ones; it would be like a floppy-disk
2167 * driver telling the computer to replace the floppy-disk drive with a
2168 * tape drive!
2169 *
2170 * Still, in certain specialized circumstances the need may arise. This
2171 * routine gets around the normal restrictions by using a work thread to
2172 * submit the change-config request.
2173 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02002174 * Return: 0 if the request was successfully queued, error code otherwise.
Alan Stern088dc272006-08-21 12:08:19 -04002175 * The caller has no way to know whether the queued request will eventually
2176 * succeed.
2177 */
2178int usb_driver_set_configuration(struct usb_device *udev, int config)
2179{
2180 struct set_config_request *req;
2181
2182 req = kmalloc(sizeof(*req), GFP_KERNEL);
2183 if (!req)
2184 return -ENOMEM;
2185 req->udev = udev;
2186 req->config = config;
David Howellsc4028952006-11-22 14:57:56 +00002187 INIT_WORK(&req->work, driver_set_config_work);
Alan Stern088dc272006-08-21 12:08:19 -04002188
Alan Sterndf718962008-12-19 10:27:56 -05002189 spin_lock(&set_config_lock);
2190 list_add(&req->node, &set_config_list);
2191 spin_unlock(&set_config_lock);
2192
Alan Stern088dc272006-08-21 12:08:19 -04002193 usb_get_dev(udev);
Alan Stern1737bf22006-12-15 16:04:52 -05002194 schedule_work(&req->work);
Alan Stern088dc272006-08-21 12:08:19 -04002195 return 0;
2196}
2197EXPORT_SYMBOL_GPL(usb_driver_set_configuration);
Oliver Neukume4c6fb72016-07-14 15:41:30 +02002198
2199/**
2200 * cdc_parse_cdc_header - parse the extra headers present in CDC devices
2201 * @hdr: the place to put the results of the parsing
2202 * @intf: the interface for which parsing is requested
2203 * @buffer: pointer to the extra headers to be parsed
2204 * @buflen: length of the extra headers
2205 *
2206 * This evaluates the extra headers present in CDC devices which
2207 * bind the interfaces for data and control and provide details
2208 * about the capabilities of the device.
2209 *
2210 * Return: number of descriptors parsed or -EINVAL
2211 * if the header is contradictory beyond salvage
2212 */
2213
2214int cdc_parse_cdc_header(struct usb_cdc_parsed_header *hdr,
2215 struct usb_interface *intf,
2216 u8 *buffer,
2217 int buflen)
2218{
2219 /* duplicates are ignored */
2220 struct usb_cdc_union_desc *union_header = NULL;
2221
2222 /* duplicates are not tolerated */
2223 struct usb_cdc_header_desc *header = NULL;
2224 struct usb_cdc_ether_desc *ether = NULL;
2225 struct usb_cdc_mdlm_detail_desc *detail = NULL;
2226 struct usb_cdc_mdlm_desc *desc = NULL;
2227
2228 unsigned int elength;
2229 int cnt = 0;
2230
2231 memset(hdr, 0x00, sizeof(struct usb_cdc_parsed_header));
2232 hdr->phonet_magic_present = false;
2233 while (buflen > 0) {
2234 elength = buffer[0];
2235 if (!elength) {
2236 dev_err(&intf->dev, "skipping garbage byte\n");
2237 elength = 1;
2238 goto next_desc;
2239 }
Greg Kroah-Hartman767f7a22017-09-21 16:58:48 +02002240 if ((buflen < elength) || (elength < 3)) {
2241 dev_err(&intf->dev, "invalid descriptor buffer length\n");
2242 break;
2243 }
Oliver Neukume4c6fb72016-07-14 15:41:30 +02002244 if (buffer[1] != USB_DT_CS_INTERFACE) {
2245 dev_err(&intf->dev, "skipping garbage\n");
2246 goto next_desc;
2247 }
2248
2249 switch (buffer[2]) {
2250 case USB_CDC_UNION_TYPE: /* we've found it */
2251 if (elength < sizeof(struct usb_cdc_union_desc))
2252 goto next_desc;
2253 if (union_header) {
2254 dev_err(&intf->dev, "More than one union descriptor, skipping ...\n");
2255 goto next_desc;
2256 }
2257 union_header = (struct usb_cdc_union_desc *)buffer;
2258 break;
2259 case USB_CDC_COUNTRY_TYPE:
2260 if (elength < sizeof(struct usb_cdc_country_functional_desc))
2261 goto next_desc;
2262 hdr->usb_cdc_country_functional_desc =
2263 (struct usb_cdc_country_functional_desc *)buffer;
2264 break;
2265 case USB_CDC_HEADER_TYPE:
2266 if (elength != sizeof(struct usb_cdc_header_desc))
2267 goto next_desc;
2268 if (header)
2269 return -EINVAL;
2270 header = (struct usb_cdc_header_desc *)buffer;
2271 break;
2272 case USB_CDC_ACM_TYPE:
2273 if (elength < sizeof(struct usb_cdc_acm_descriptor))
2274 goto next_desc;
2275 hdr->usb_cdc_acm_descriptor =
2276 (struct usb_cdc_acm_descriptor *)buffer;
2277 break;
2278 case USB_CDC_ETHERNET_TYPE:
2279 if (elength != sizeof(struct usb_cdc_ether_desc))
2280 goto next_desc;
2281 if (ether)
2282 return -EINVAL;
2283 ether = (struct usb_cdc_ether_desc *)buffer;
2284 break;
2285 case USB_CDC_CALL_MANAGEMENT_TYPE:
2286 if (elength < sizeof(struct usb_cdc_call_mgmt_descriptor))
2287 goto next_desc;
2288 hdr->usb_cdc_call_mgmt_descriptor =
2289 (struct usb_cdc_call_mgmt_descriptor *)buffer;
2290 break;
2291 case USB_CDC_DMM_TYPE:
2292 if (elength < sizeof(struct usb_cdc_dmm_desc))
2293 goto next_desc;
2294 hdr->usb_cdc_dmm_desc =
2295 (struct usb_cdc_dmm_desc *)buffer;
2296 break;
2297 case USB_CDC_MDLM_TYPE:
2298 if (elength < sizeof(struct usb_cdc_mdlm_desc *))
2299 goto next_desc;
2300 if (desc)
2301 return -EINVAL;
2302 desc = (struct usb_cdc_mdlm_desc *)buffer;
2303 break;
2304 case USB_CDC_MDLM_DETAIL_TYPE:
2305 if (elength < sizeof(struct usb_cdc_mdlm_detail_desc *))
2306 goto next_desc;
2307 if (detail)
2308 return -EINVAL;
2309 detail = (struct usb_cdc_mdlm_detail_desc *)buffer;
2310 break;
2311 case USB_CDC_NCM_TYPE:
2312 if (elength < sizeof(struct usb_cdc_ncm_desc))
2313 goto next_desc;
2314 hdr->usb_cdc_ncm_desc = (struct usb_cdc_ncm_desc *)buffer;
2315 break;
2316 case USB_CDC_MBIM_TYPE:
2317 if (elength < sizeof(struct usb_cdc_mbim_desc))
2318 goto next_desc;
2319
2320 hdr->usb_cdc_mbim_desc = (struct usb_cdc_mbim_desc *)buffer;
2321 break;
2322 case USB_CDC_MBIM_EXTENDED_TYPE:
2323 if (elength < sizeof(struct usb_cdc_mbim_extended_desc))
2324 break;
2325 hdr->usb_cdc_mbim_extended_desc =
2326 (struct usb_cdc_mbim_extended_desc *)buffer;
2327 break;
2328 case CDC_PHONET_MAGIC_NUMBER:
2329 hdr->phonet_magic_present = true;
2330 break;
2331 default:
2332 /*
2333 * there are LOTS more CDC descriptors that
2334 * could legitimately be found here.
2335 */
2336 dev_dbg(&intf->dev, "Ignoring descriptor: type %02x, length %ud\n",
2337 buffer[2], elength);
2338 goto next_desc;
2339 }
2340 cnt++;
2341next_desc:
2342 buflen -= elength;
2343 buffer += elength;
2344 }
2345 hdr->usb_cdc_union_desc = union_header;
2346 hdr->usb_cdc_header_desc = header;
2347 hdr->usb_cdc_mdlm_detail_desc = detail;
2348 hdr->usb_cdc_mdlm_desc = desc;
2349 hdr->usb_cdc_ether_desc = ether;
2350 return cnt;
2351}
2352
2353EXPORT_SYMBOL(cdc_parse_cdc_header);