blob: ea681f157368f245772ab145073594bf4f6fb273 [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 Neukum7ceec1f2007-01-26 14:26:21 +010015#include <linux/usb/quirks.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020016#include <linux/usb/hcd.h> /* for usbcore internals */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/byteorder.h>
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "usb.h"
20
Alan Sterndf718962008-12-19 10:27:56 -050021static void cancel_async_set_config(struct usb_device *udev);
22
Alan Stern67f5dde2007-07-24 18:23:23 -040023struct api_context {
24 struct completion done;
25 int status;
26};
27
David Howells7d12e782006-10-05 14:55:46 +010028static void usb_api_blocking_completion(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Alan Stern67f5dde2007-07-24 18:23:23 -040030 struct api_context *ctx = urb->context;
31
32 ctx->status = urb->status;
33 complete(&ctx->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034}
35
36
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020037/*
38 * Starts urb and waits for completion or timeout. Note that this call
39 * is NOT interruptible. Many device driver i/o requests should be
40 * interruptible and therefore these drivers should implement their
41 * own interruptible routines.
42 */
43static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -080044{
Alan Stern67f5dde2007-07-24 18:23:23 -040045 struct api_context ctx;
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020046 unsigned long expire;
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -070047 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Alan Stern67f5dde2007-07-24 18:23:23 -040049 init_completion(&ctx.done);
50 urb->context = &ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 urb->actual_length = 0;
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -070052 retval = usb_submit_urb(urb, GFP_NOIO);
53 if (unlikely(retval))
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020054 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020056 expire = timeout ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT;
Alan Stern67f5dde2007-07-24 18:23:23 -040057 if (!wait_for_completion_timeout(&ctx.done, expire)) {
58 usb_kill_urb(urb);
59 retval = (ctx.status == -ENOENT ? -ETIMEDOUT : ctx.status);
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020060
61 dev_dbg(&urb->dev->dev,
Roel Kluin71d27182009-03-13 12:19:18 +010062 "%s timed out on ep%d%s len=%u/%u\n",
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020063 current->comm,
Alan Stern5e60a162007-07-30 17:07:21 -040064 usb_endpoint_num(&urb->ep->desc),
65 usb_urb_dir_in(urb) ? "in" : "out",
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020066 urb->actual_length,
67 urb->transfer_buffer_length);
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020068 } else
Alan Stern67f5dde2007-07-24 18:23:23 -040069 retval = ctx.status;
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020070out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (actual_length)
72 *actual_length = urb->actual_length;
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 usb_free_urb(urb);
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -070075 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
78/*-------------------------------------------------------------------*/
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -080079/* returns status (negative) or length (positive) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int usb_internal_control_msg(struct usb_device *usb_dev,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -080081 unsigned int pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 struct usb_ctrlrequest *cmd,
83 void *data, int len, int timeout)
84{
85 struct urb *urb;
86 int retv;
87 int length;
88
89 urb = usb_alloc_urb(0, GFP_NOIO);
90 if (!urb)
91 return -ENOMEM;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -080092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char *)cmd, data,
94 len, usb_api_blocking_completion, NULL);
95
96 retv = usb_start_wait_urb(urb, timeout, &length);
97 if (retv < 0)
98 return retv;
99 else
100 return length;
101}
102
103/**
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800104 * usb_control_msg - Builds a control urb, sends it off and waits for completion
105 * @dev: pointer to the usb device to send the message to
106 * @pipe: endpoint "pipe" to send the message to
107 * @request: USB message request value
108 * @requesttype: USB message request type value
109 * @value: USB message value
110 * @index: USB message index value
111 * @data: pointer to the data to send
112 * @size: length in bytes of the data to send
113 * @timeout: time in msecs to wait for the message to complete before timing
114 * out (if 0 the wait is forever)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800116 * Context: !in_interrupt ()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800118 * This function sends a simple control message to a specified endpoint and
119 * waits for the message to complete, or timeout.
120 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800121 * Don't use this function from within an interrupt context, like a bottom half
122 * handler. If you need an asynchronous message, or need to send a message
123 * from within interrupt context, use usb_submit_urb().
124 * If a thread in your driver uses this call, make sure your disconnect()
125 * method can wait for it to complete. Since you don't have a handle on the
126 * URB used, you can't cancel the request.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200127 *
128 * Return: If successful, the number of bytes transferred. Otherwise, a negative
129 * error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800131int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
132 __u8 requesttype, __u16 value, __u16 index, void *data,
133 __u16 size, int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800135 struct usb_ctrlrequest *dr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int ret;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800137
138 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (!dr)
140 return -ENOMEM;
141
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800142 dr->bRequestType = requesttype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 dr->bRequest = request;
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700144 dr->wValue = cpu_to_le16(value);
145 dr->wIndex = cpu_to_le16(index);
146 dr->wLength = cpu_to_le16(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);
149
150 kfree(dr);
151
152 return ret;
153}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600154EXPORT_SYMBOL_GPL(usb_control_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156/**
Greg Kroah-Hartman782a7a62006-05-19 13:20:20 -0700157 * usb_interrupt_msg - Builds an interrupt urb, sends it off and waits for completion
158 * @usb_dev: pointer to the usb device to send the message to
159 * @pipe: endpoint "pipe" to send the message to
160 * @data: pointer to the data to send
161 * @len: length in bytes of the data to send
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800162 * @actual_length: pointer to a location to put the actual length transferred
163 * in bytes
Greg Kroah-Hartman782a7a62006-05-19 13:20:20 -0700164 * @timeout: time in msecs to wait for the message to complete before
165 * timing out (if 0 the wait is forever)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800166 *
Greg Kroah-Hartman782a7a62006-05-19 13:20:20 -0700167 * Context: !in_interrupt ()
168 *
169 * This function sends a simple interrupt message to a specified endpoint and
170 * waits for the message to complete, or timeout.
171 *
Greg Kroah-Hartman782a7a62006-05-19 13:20:20 -0700172 * Don't use this function from within an interrupt context, like a bottom half
173 * handler. If you need an asynchronous message, or need to send a message
174 * from within interrupt context, use usb_submit_urb() If a thread in your
175 * driver uses this call, make sure your disconnect() method can wait for it to
176 * complete. Since you don't have a handle on the URB used, you can't cancel
177 * the request.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200178 *
179 * Return:
180 * If successful, 0. Otherwise a negative error number. The number of actual
Masanari Iidae2278672014-02-18 22:54:36 +0900181 * bytes transferred will be stored in the @actual_length parameter.
Greg Kroah-Hartman782a7a62006-05-19 13:20:20 -0700182 */
183int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe,
184 void *data, int len, int *actual_length, int timeout)
185{
186 return usb_bulk_msg(usb_dev, pipe, data, len, actual_length, timeout);
187}
188EXPORT_SYMBOL_GPL(usb_interrupt_msg);
189
190/**
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800191 * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion
192 * @usb_dev: pointer to the usb device to send the message to
193 * @pipe: endpoint "pipe" to send the message to
194 * @data: pointer to the data to send
195 * @len: length in bytes of the data to send
196 * @actual_length: pointer to a location to put the actual length transferred
197 * in bytes
198 * @timeout: time in msecs to wait for the message to complete before
199 * timing out (if 0 the wait is forever)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800201 * Context: !in_interrupt ()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800203 * This function sends a simple bulk message to a specified endpoint
204 * and waits for the message to complete, or timeout.
Alan Sternd09d36a2005-09-26 16:22:45 -0400205 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800206 * Don't use this function from within an interrupt context, like a bottom half
207 * handler. If you need an asynchronous message, or need to send a message
208 * from within interrupt context, use usb_submit_urb() If a thread in your
209 * driver uses this call, make sure your disconnect() method can wait for it to
210 * complete. Since you don't have a handle on the URB used, you can't cancel
211 * the request.
212 *
213 * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT ioctl,
214 * users are forced to abuse this routine by using it to submit URBs for
215 * interrupt endpoints. We will take the liberty of creating an interrupt URB
216 * (with the default interval) if the target is an interrupt endpoint.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200217 *
218 * Return:
219 * If successful, 0. Otherwise a negative error number. The number of actual
Rahul Bedarkar025d4432014-01-04 11:24:41 +0530220 * bytes transferred will be stored in the @actual_length parameter.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200221 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800223int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
224 void *data, int len, int *actual_length, int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct urb *urb;
Alan Sternd09d36a2005-09-26 16:22:45 -0400227 struct usb_host_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Matthew Wilcoxfe54b052010-04-30 13:11:29 -0600229 ep = usb_pipe_endpoint(usb_dev, pipe);
Alan Sternd09d36a2005-09-26 16:22:45 -0400230 if (!ep || len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return -EINVAL;
232
Alan Sternd09d36a2005-09-26 16:22:45 -0400233 urb = usb_alloc_urb(0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (!urb)
235 return -ENOMEM;
236
Alan Sternd09d36a2005-09-26 16:22:45 -0400237 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
238 USB_ENDPOINT_XFER_INT) {
239 pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
240 usb_fill_int_urb(urb, usb_dev, pipe, data, len,
Alan Stern8d062b92007-04-23 17:30:32 -0400241 usb_api_blocking_completion, NULL,
242 ep->desc.bInterval);
Alan Sternd09d36a2005-09-26 16:22:45 -0400243 } else
244 usb_fill_bulk_urb(urb, usb_dev, pipe, data, len,
245 usb_api_blocking_completion, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 return usb_start_wait_urb(urb, timeout, actual_length);
248}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600249EXPORT_SYMBOL_GPL(usb_bulk_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251/*-------------------------------------------------------------------*/
252
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800253static void sg_clean(struct usb_sg_request *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 if (io->urbs) {
256 while (io->entries--)
Tülin İzer085528e2013-05-17 10:32:35 +0300257 usb_free_urb(io->urbs[io->entries]);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800258 kfree(io->urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 io->urbs = NULL;
260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 io->dev = NULL;
262}
263
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800264static void sg_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800266 struct usb_sg_request *io = urb->context;
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700267 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800269 spin_lock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 /* In 2.5 we require hcds' endpoint queues not to progress after fault
272 * reports, until the completion callback (this!) returns. That lets
273 * device driver code (like this routine) unlink queued urbs first,
274 * if it needs to, since the HC won't work on them at all. So it's
275 * not possible for page N+1 to overwrite page N, and so on.
276 *
277 * That's only for "hard" faults; "soft" faults (unlinks) sometimes
278 * complete before the HCD can get requests away from hardware,
279 * though never during cleanup after a hard fault.
280 */
281 if (io->status
282 && (io->status != -ECONNRESET
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700283 || status != -ECONNRESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 && urb->actual_length) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800285 dev_err(io->dev->bus->controller,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 "dev %s ep%d%s scatterlist error %d/%d\n",
287 io->dev->devpath,
Alan Stern5e60a162007-07-30 17:07:21 -0400288 usb_endpoint_num(&urb->ep->desc),
289 usb_urb_dir_in(urb) ? "in" : "out",
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700290 status, io->status);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800291 /* BUG (); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700294 if (io->status == 0 && status && status != -ECONNRESET) {
295 int i, found, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700297 io->status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 /* the previous urbs, and this one, completed already.
300 * unlink pending urbs so they won't rx/tx bad data.
301 * careful: unlink can sometimes be synchronous...
302 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800303 spin_unlock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 for (i = 0, found = 0; i < io->entries; i++) {
David Mosberger98b74b02016-03-08 14:42:48 -0700305 if (!io->urbs[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 continue;
307 if (found) {
David Mosberger98b74b02016-03-08 14:42:48 -0700308 usb_block_urb(io->urbs[i]);
Tülin İzer085528e2013-05-17 10:32:35 +0300309 retval = usb_unlink_urb(io->urbs[i]);
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700310 if (retval != -EINPROGRESS &&
311 retval != -ENODEV &&
Alan Sternbcf39852012-03-22 11:00:21 -0400312 retval != -EBUSY &&
313 retval != -EIDRM)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800314 dev_err(&io->dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 "%s, unlink --> %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800316 __func__, retval);
Tülin İzer085528e2013-05-17 10:32:35 +0300317 } else if (urb == io->urbs[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 found = 1;
319 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800320 spin_lock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /* on the last completion, signal usb_sg_wait() */
324 io->bytes += urb->actual_length;
325 io->count--;
326 if (!io->count)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800327 complete(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800329 spin_unlock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332
333/**
334 * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request
335 * @io: request block being initialized. until usb_sg_wait() returns,
336 * treat this as a pointer to an opaque block of memory,
337 * @dev: the usb device that will send or receive the data
338 * @pipe: endpoint "pipe" used to transfer the data
339 * @period: polling rate for interrupt endpoints, in frames or
340 * (for high speed endpoints) microframes; ignored for bulk
341 * @sg: scatterlist entries
342 * @nents: how many entries in the scatterlist
343 * @length: how many bytes to send from the scatterlist, or zero to
344 * send every byte identified in the list.
345 * @mem_flags: SLAB_* flags affecting memory allocations in this call
346 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200347 * This initializes a scatter/gather request, allocating resources such as
348 * I/O mappings and urb memory (except maybe memory used by USB controller
349 * drivers).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 *
351 * The request must be issued using usb_sg_wait(), which waits for the I/O to
352 * complete (or to be canceled) and then cleans up all resources allocated by
353 * usb_sg_init().
354 *
355 * The request may be canceled with usb_sg_cancel(), either before or after
356 * usb_sg_wait() is called.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200357 *
358 * Return: Zero for success, else a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800360int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
361 unsigned pipe, unsigned period, struct scatterlist *sg,
362 int nents, size_t length, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800364 int i;
365 int urb_flags;
Sarah Sharpe04748e2009-04-27 19:59:01 -0700366 int use_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 if (!io || !dev || !sg
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800369 || usb_pipecontrol(pipe)
370 || usb_pipeisoc(pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 || nents <= 0)
372 return -EINVAL;
373
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800374 spin_lock_init(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 io->dev = dev;
376 io->pipe = pipe;
Alan Stern0ba169af2010-05-05 15:26:17 -0400377
378 if (dev->bus->sg_tablesize > 0) {
379 use_sg = true;
380 io->entries = 1;
381 } else {
382 use_sg = false;
383 io->entries = nents;
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 /* initialize all the urbs we'll use */
Tülin İzera1fefaa2013-05-17 10:33:05 +0300387 io->urbs = kmalloc(io->entries * sizeof(*io->urbs), mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (!io->urbs)
389 goto nomem;
390
Alan Stern0ba169af2010-05-05 15:26:17 -0400391 urb_flags = URB_NO_INTERRUPT;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800392 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 urb_flags |= URB_SHORT_NOT_OK;
394
Alan Stern0ba169af2010-05-05 15:26:17 -0400395 for_each_sg(sg, sg, io->entries, i) {
396 struct urb *urb;
397 unsigned len;
398
399 urb = usb_alloc_urb(0, mem_flags);
400 if (!urb) {
401 io->entries = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 goto nomem;
403 }
Alan Stern0ba169af2010-05-05 15:26:17 -0400404 io->urbs[i] = urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Alan Stern0ba169af2010-05-05 15:26:17 -0400406 urb->dev = NULL;
407 urb->pipe = pipe;
408 urb->interval = period;
409 urb->transfer_flags = urb_flags;
410 urb->complete = sg_complete;
411 urb->context = io;
412 urb->sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Alan Stern0ba169af2010-05-05 15:26:17 -0400414 if (use_sg) {
415 /* There is no single transfer buffer */
416 urb->transfer_buffer = NULL;
417 urb->num_sgs = nents;
Alan Sternff9c8952010-04-02 13:27:28 -0400418
Alan Stern0ba169af2010-05-05 15:26:17 -0400419 /* A length of zero means transfer the whole sg list */
420 len = length;
421 if (len == 0) {
Alan Stern64d65872010-06-18 10:16:33 -0400422 struct scatterlist *sg2;
423 int j;
424
425 for_each_sg(sg, sg2, nents, j)
426 len += sg2->length;
Sarah Sharpe04748e2009-04-27 19:59:01 -0700427 }
Alan Stern0ba169af2010-05-05 15:26:17 -0400428 } else {
Sarah Sharpe04748e2009-04-27 19:59:01 -0700429 /*
Alan Sternff9c8952010-04-02 13:27:28 -0400430 * Some systems can't use DMA; they use PIO instead.
431 * For their sakes, transfer_buffer is set whenever
432 * possible.
Sarah Sharpe04748e2009-04-27 19:59:01 -0700433 */
Alan Sternff9c8952010-04-02 13:27:28 -0400434 if (!PageHighMem(sg_page(sg)))
Alan Stern0ba169af2010-05-05 15:26:17 -0400435 urb->transfer_buffer = sg_virt(sg);
Pete Zaitcev81bf46f2009-06-11 08:40:39 -0600436 else
Alan Stern0ba169af2010-05-05 15:26:17 -0400437 urb->transfer_buffer = NULL;
Pete Zaitcev81bf46f2009-06-11 08:40:39 -0600438
Alan Sternff9c8952010-04-02 13:27:28 -0400439 len = sg->length;
Sarah Sharpe04748e2009-04-27 19:59:01 -0700440 if (length) {
Dan Carpenteredb2b252011-09-27 09:25:19 +0300441 len = min_t(size_t, len, length);
Sarah Sharpe04748e2009-04-27 19:59:01 -0700442 length -= len;
443 if (length == 0)
444 io->entries = i + 1;
445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Alan Stern0ba169af2010-05-05 15:26:17 -0400447 urb->transfer_buffer_length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
Alan Stern0ba169af2010-05-05 15:26:17 -0400449 io->urbs[--i]->transfer_flags &= ~URB_NO_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 /* transaction state */
Alan Stern580da342008-08-05 13:05:17 -0400452 io->count = io->entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 io->status = 0;
454 io->bytes = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800455 init_completion(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return 0;
457
458nomem:
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800459 sg_clean(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return -ENOMEM;
461}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600462EXPORT_SYMBOL_GPL(usb_sg_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464/**
465 * usb_sg_wait - synchronously execute scatter/gather request
466 * @io: request block handle, as initialized with usb_sg_init().
467 * some fields become accessible when this call returns.
468 * Context: !in_interrupt ()
469 *
470 * This function blocks until the specified I/O operation completes. It
471 * leverages the grouping of the related I/O requests to get good transfer
472 * rates, by queueing the requests. At higher speeds, such queuing can
473 * significantly improve USB throughput.
474 *
475 * There are three kinds of completion for this function.
476 * (1) success, where io->status is zero. The number of io->bytes
477 * transferred is as requested.
478 * (2) error, where io->status is a negative errno value. The number
479 * of io->bytes transferred before the error is usually less
480 * than requested, and can be nonzero.
Steven Cole093cf722005-05-03 19:07:24 -0600481 * (3) cancellation, a type of error with status -ECONNRESET that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 * is initiated by usb_sg_cancel().
483 *
484 * When this function returns, all memory allocated through usb_sg_init() or
485 * this call will have been freed. The request block parameter may still be
486 * passed to usb_sg_cancel(), or it may be freed. It could also be
487 * reinitialized and then reused.
488 *
489 * Data Transfer Rates:
490 *
491 * Bulk transfers are valid for full or high speed endpoints.
492 * The best full speed data rate is 19 packets of 64 bytes each
493 * per frame, or 1216 bytes per millisecond.
494 * The best high speed data rate is 13 packets of 512 bytes each
495 * per microframe, or 52 KBytes per millisecond.
496 *
497 * The reason to use interrupt transfers through this API would most likely
498 * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
499 * could be transferred. That capability is less useful for low or full
500 * speed interrupt endpoints, which allow at most one packet per millisecond,
501 * of at most 8 or 64 bytes (respectively).
Sarah Sharp79abb1a2009-04-27 19:58:26 -0700502 *
503 * It is not necessary to call this function to reserve bandwidth for devices
504 * under an xHCI host controller, as the bandwidth is reserved when the
505 * configuration or interface alt setting is selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800507void usb_sg_wait(struct usb_sg_request *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800509 int i;
510 int entries = io->entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 /* queue the urbs. */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800513 spin_lock_irq(&io->lock);
Alan Stern8ccef0d2007-06-21 16:26:46 -0400514 i = 0;
515 while (i < entries && !io->status) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800516 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800518 io->urbs[i]->dev = io->dev;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800519 spin_unlock_irq(&io->lock);
David Mosberger98b74b02016-03-08 14:42:48 -0700520
521 retval = usb_submit_urb(io->urbs[i], GFP_NOIO);
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 switch (retval) {
524 /* maybe we retrying will recover */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800525 case -ENXIO: /* hc didn't queue this one */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 case -EAGAIN:
527 case -ENOMEM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 retval = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800529 yield();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 break;
531
532 /* no error? continue immediately.
533 *
534 * NOTE: to work better with UHCI (4K I/O buffer may
535 * need 3K of TDs) it may be good to limit how many
536 * URBs are queued at once; N milliseconds?
537 */
538 case 0:
Alan Stern8ccef0d2007-06-21 16:26:46 -0400539 ++i;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800540 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 break;
542
543 /* fail any uncompleted urbs */
544 default:
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800545 io->urbs[i]->status = retval;
546 dev_dbg(&io->dev->dev, "%s, submit --> %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800547 __func__, retval);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800548 usb_sg_cancel(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800550 spin_lock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (retval && (io->status == 0 || io->status == -ECONNRESET))
552 io->status = retval;
553 }
554 io->count -= entries - i;
555 if (io->count == 0)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800556 complete(&io->complete);
557 spin_unlock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /* OK, yes, this could be packaged as non-blocking.
560 * So could the submit loop above ... but it's easier to
561 * solve neither problem than to solve both!
562 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800563 wait_for_completion(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800565 sg_clean(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600567EXPORT_SYMBOL_GPL(usb_sg_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569/**
570 * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
571 * @io: request block, initialized with usb_sg_init()
572 *
573 * This stops a request after it has been started by usb_sg_wait().
574 * It can also prevents one initialized by usb_sg_init() from starting,
575 * so that call just frees resources allocated to the request.
576 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800577void usb_sg_cancel(struct usb_sg_request *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800579 unsigned long flags;
David Mosberger5f2e5fb2016-03-08 14:42:49 -0700580 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800582 spin_lock_irqsave(&io->lock, flags);
David Mosberger5f2e5fb2016-03-08 14:42:49 -0700583 if (io->status) {
584 spin_unlock_irqrestore(&io->lock, flags);
585 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
David Mosberger5f2e5fb2016-03-08 14:42:49 -0700587 /* shut everything down */
588 io->status = -ECONNRESET;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800589 spin_unlock_irqrestore(&io->lock, flags);
David Mosberger5f2e5fb2016-03-08 14:42:49 -0700590
591 for (i = io->entries - 1; i >= 0; --i) {
592 usb_block_urb(io->urbs[i]);
593
594 retval = usb_unlink_urb(io->urbs[i]);
595 if (retval != -EINPROGRESS
596 && retval != -ENODEV
597 && retval != -EBUSY
598 && retval != -EIDRM)
599 dev_warn(&io->dev->dev, "%s, unlink --> %d\n",
600 __func__, retval);
601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600603EXPORT_SYMBOL_GPL(usb_sg_cancel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605/*-------------------------------------------------------------------*/
606
607/**
608 * usb_get_descriptor - issues a generic GET_DESCRIPTOR request
609 * @dev: the device whose descriptor is being retrieved
610 * @type: the descriptor type (USB_DT_*)
611 * @index: the number of the descriptor
612 * @buf: where to put the descriptor
613 * @size: how big is "buf"?
614 * Context: !in_interrupt ()
615 *
616 * Gets a USB descriptor. Convenience functions exist to simplify
617 * getting some types of descriptors. Use
618 * usb_get_string() or usb_string() for USB_DT_STRING.
619 * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG)
620 * are part of the device structure.
621 * In addition to a number of USB-standard descriptors, some
622 * devices also use class-specific or vendor-specific descriptors.
623 *
624 * This call is synchronous, and may not be used in an interrupt context.
625 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200626 * Return: The number of bytes received on success, or else the status code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 * returned by the underlying usb_control_msg() call.
628 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800629int usb_get_descriptor(struct usb_device *dev, unsigned char type,
630 unsigned char index, void *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 int i;
633 int result;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800634
635 memset(buf, 0, size); /* Make sure we parse really received data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 for (i = 0; i < 3; ++i) {
Alan Sternc39772d2007-08-20 10:45:28 -0400638 /* retry on length 0 or error; some devices are flakey */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
640 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
641 (type << 8) + index, 0, buf, size,
642 USB_CTRL_GET_TIMEOUT);
Alan Sternc39772d2007-08-20 10:45:28 -0400643 if (result <= 0 && result != -ETIMEDOUT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 continue;
645 if (result > 1 && ((u8 *)buf)[1] != type) {
Alan Stern67f5a4b2009-02-20 16:33:08 -0500646 result = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 continue;
648 }
649 break;
650 }
651 return result;
652}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600653EXPORT_SYMBOL_GPL(usb_get_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655/**
656 * usb_get_string - gets a string descriptor
657 * @dev: the device whose string descriptor is being retrieved
658 * @langid: code for language chosen (from string descriptor zero)
659 * @index: the number of the descriptor
660 * @buf: where to put the string
661 * @size: how big is "buf"?
662 * Context: !in_interrupt ()
663 *
664 * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character,
665 * in little-endian byte order).
666 * The usb_string() function will often be a convenient way to turn
667 * these strings into kernel-printable form.
668 *
669 * Strings may be referenced in device, configuration, interface, or other
670 * descriptors, and could also be used in vendor-specific ways.
671 *
672 * This call is synchronous, and may not be used in an interrupt context.
673 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200674 * Return: The number of bytes received on success, or else the status code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 * returned by the underlying usb_control_msg() call.
676 */
Adrian Bunke266a122005-11-08 21:05:43 +0100677static int usb_get_string(struct usb_device *dev, unsigned short langid,
678 unsigned char index, void *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 int i;
681 int result;
682
683 for (i = 0; i < 3; ++i) {
684 /* retry on length 0 or stall; some devices are flakey */
685 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
686 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
687 (USB_DT_STRING << 8) + index, langid, buf, size,
688 USB_CTRL_GET_TIMEOUT);
Alan Stern67f5a4b2009-02-20 16:33:08 -0500689 if (result == 0 || result == -EPIPE)
690 continue;
691 if (result > 1 && ((u8 *) buf)[1] != USB_DT_STRING) {
692 result = -ENODATA;
693 continue;
694 }
695 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697 return result;
698}
699
700static void usb_try_string_workarounds(unsigned char *buf, int *length)
701{
702 int newlength, oldlength = *length;
703
704 for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
705 if (!isprint(buf[newlength]) || buf[newlength + 1])
706 break;
707
708 if (newlength > 2) {
709 buf[0] = newlength;
710 *length = newlength;
711 }
712}
713
714static int usb_string_sub(struct usb_device *dev, unsigned int langid,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800715 unsigned int index, unsigned char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 int rc;
718
719 /* Try to read the string descriptor by asking for the maximum
720 * possible number of bytes */
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100721 if (dev->quirks & USB_QUIRK_STRING_FETCH_255)
722 rc = -EIO;
723 else
724 rc = usb_get_string(dev, langid, index, buf, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 /* If that failed try to read the descriptor length, then
727 * ask for just that many bytes */
728 if (rc < 2) {
729 rc = usb_get_string(dev, langid, index, buf, 2);
730 if (rc == 2)
731 rc = usb_get_string(dev, langid, index, buf, buf[0]);
732 }
733
734 if (rc >= 2) {
735 if (!buf[0] && !buf[1])
736 usb_try_string_workarounds(buf, &rc);
737
738 /* There might be extra junk at the end of the descriptor */
739 if (buf[0] < rc)
740 rc = buf[0];
741
742 rc = rc - (rc & 1); /* force a multiple of two */
743 }
744
745 if (rc < 2)
746 rc = (rc < 0 ? rc : -EINVAL);
747
748 return rc;
749}
750
Daniel Mack0cce2ed2009-07-10 11:04:58 +0200751static int usb_get_langid(struct usb_device *dev, unsigned char *tbuf)
752{
753 int err;
754
755 if (dev->have_langid)
756 return 0;
757
758 if (dev->string_langid < 0)
759 return -EPIPE;
760
761 err = usb_string_sub(dev, 0, 0, tbuf);
762
763 /* If the string was reported but is malformed, default to english
764 * (0x0409) */
765 if (err == -ENODATA || (err > 0 && err < 4)) {
766 dev->string_langid = 0x0409;
767 dev->have_langid = 1;
768 dev_err(&dev->dev,
Scot Doyle586af072014-09-25 15:16:48 +0000769 "language id specifier not provided by device, defaulting to English\n");
Daniel Mack0cce2ed2009-07-10 11:04:58 +0200770 return 0;
771 }
772
773 /* In case of all other errors, we assume the device is not able to
774 * deal with strings at all. Set string_langid to -1 in order to
775 * prevent any string to be retrieved from the device */
776 if (err < 0) {
777 dev_err(&dev->dev, "string descriptor 0 read error: %d\n",
778 err);
779 dev->string_langid = -1;
780 return -EPIPE;
781 }
782
783 /* always use the first langid listed */
784 dev->string_langid = tbuf[2] | (tbuf[3] << 8);
785 dev->have_langid = 1;
786 dev_dbg(&dev->dev, "default language 0x%04x\n",
787 dev->string_langid);
788 return 0;
789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/**
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200792 * usb_string - returns UTF-8 version of a string descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 * @dev: the device whose string descriptor is being retrieved
794 * @index: the number of the descriptor
795 * @buf: where to put the string
796 * @size: how big is "buf"?
797 * Context: !in_interrupt ()
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800798 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 * This converts the UTF-16LE encoded strings returned by devices, from
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200800 * usb_get_string_descriptor(), to null-terminated UTF-8 encoded ones
801 * that are more usable in most kernel contexts. Note that this function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 * chooses strings in the first language supported by the device.
803 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * This call is synchronous, and may not be used in an interrupt context.
805 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200806 * Return: length of the string (>= 0) or usb_control_msg status (< 0).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 */
808int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
809{
810 unsigned char *tbuf;
811 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 if (dev->state == USB_STATE_SUSPENDED)
814 return -EHOSTUNREACH;
815 if (size <= 0 || !buf || !index)
816 return -EINVAL;
817 buf[0] = 0;
Alan Stern74675a52009-04-30 10:08:18 -0400818 tbuf = kmalloc(256, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (!tbuf)
820 return -ENOMEM;
821
Daniel Mack0cce2ed2009-07-10 11:04:58 +0200822 err = usb_get_langid(dev, tbuf);
823 if (err < 0)
824 goto errout;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 err = usb_string_sub(dev, dev->string_langid, index, tbuf);
827 if (err < 0)
828 goto errout;
829
830 size--; /* leave room for trailing NULL char in output buffer */
Alan Stern74675a52009-04-30 10:08:18 -0400831 err = utf16s_to_utf8s((wchar_t *) &tbuf[2], (err - 2) / 2,
832 UTF16_LITTLE_ENDIAN, buf, size);
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200833 buf[err] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 if (tbuf[1] != USB_DT_STRING)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800836 dev_dbg(&dev->dev,
837 "wrong descriptor type %02x for string %d (\"%s\")\n",
838 tbuf[1], index, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 errout:
841 kfree(tbuf);
842 return err;
843}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600844EXPORT_SYMBOL_GPL(usb_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200846/* one UTF-8-encoded 16-bit character has at most three bytes */
847#define MAX_USB_STRING_SIZE (127 * 3 + 1)
848
Alan Stern4f62efe2005-10-24 16:24:14 -0400849/**
850 * usb_cache_string - read a string descriptor and cache it for later use
851 * @udev: the device whose string descriptor is being read
852 * @index: the descriptor index
853 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200854 * Return: A pointer to a kmalloc'ed buffer containing the descriptor string,
855 * or %NULL if the index is 0 or the string could not be read.
Alan Stern4f62efe2005-10-24 16:24:14 -0400856 */
857char *usb_cache_string(struct usb_device *udev, int index)
858{
859 char *buf;
860 char *smallbuf = NULL;
861 int len;
862
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800863 if (index <= 0)
864 return NULL;
865
Oliver Neukumacbe2fe2010-01-12 12:32:50 +0100866 buf = kmalloc(MAX_USB_STRING_SIZE, GFP_NOIO);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800867 if (buf) {
Clemens Ladischa853a3d2009-04-24 10:12:18 +0200868 len = usb_string(udev, index, buf, MAX_USB_STRING_SIZE);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800869 if (len > 0) {
Oliver Neukumacbe2fe2010-01-12 12:32:50 +0100870 smallbuf = kmalloc(++len, GFP_NOIO);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800871 if (!smallbuf)
Alan Stern4f62efe2005-10-24 16:24:14 -0400872 return buf;
873 memcpy(smallbuf, buf, len);
874 }
875 kfree(buf);
876 }
877 return smallbuf;
878}
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880/*
881 * usb_get_device_descriptor - (re)reads the device descriptor (usbcore)
882 * @dev: the device whose device descriptor is being updated
883 * @size: how much of the descriptor to read
884 * Context: !in_interrupt ()
885 *
886 * Updates the copy of the device descriptor stored in the device structure,
Laurent Pinchart6ab16a92006-11-07 10:16:25 +0100887 * which dedicates space for this purpose.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 *
889 * Not exported, only for use by the core. If drivers really want to read
890 * the device descriptor directly, they can call usb_get_descriptor() with
891 * type = USB_DT_DEVICE and index = 0.
892 *
893 * This call is synchronous, and may not be used in an interrupt context.
894 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200895 * Return: The number of bytes received on success, or else the status code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 * returned by the underlying usb_control_msg() call.
897 */
898int usb_get_device_descriptor(struct usb_device *dev, unsigned int size)
899{
900 struct usb_device_descriptor *desc;
901 int ret;
902
903 if (size > sizeof(*desc))
904 return -EINVAL;
905 desc = kmalloc(sizeof(*desc), GFP_NOIO);
906 if (!desc)
907 return -ENOMEM;
908
909 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800910 if (ret >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 memcpy(&dev->descriptor, desc, size);
912 kfree(desc);
913 return ret;
914}
915
916/**
917 * usb_get_status - issues a GET_STATUS call
918 * @dev: the device whose status is being checked
919 * @type: USB_RECIP_*; for device, interface, or endpoint
920 * @target: zero (for device), else interface or endpoint number
921 * @data: pointer to two bytes of bitmap data
922 * Context: !in_interrupt ()
923 *
924 * Returns device, interface, or endpoint status. Normally only of
925 * interest to see if the device is self powered, or has enabled the
926 * remote wakeup facility; or whether a bulk or interrupt endpoint
927 * is halted ("stalled").
928 *
929 * Bits in these status bitmaps are set using the SET_FEATURE request,
930 * and cleared using the CLEAR_FEATURE request. The usb_clear_halt()
931 * function should be used to clear halt ("stall") status.
932 *
933 * This call is synchronous, and may not be used in an interrupt context.
934 *
Alan Stern15b73362013-07-30 15:35:40 -0400935 * Returns 0 and the status value in *@data (in host byte order) on success,
936 * or else the status code from the underlying usb_control_msg() call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
938int usb_get_status(struct usb_device *dev, int type, int target, void *data)
939{
940 int ret;
Alan Stern15b73362013-07-30 15:35:40 -0400941 __le16 *status = kmalloc(sizeof(*status), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 if (!status)
944 return -ENOMEM;
945
946 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
947 USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status,
948 sizeof(*status), USB_CTRL_GET_TIMEOUT);
949
Alan Stern15b73362013-07-30 15:35:40 -0400950 if (ret == 2) {
951 *(u16 *) data = le16_to_cpu(*status);
952 ret = 0;
953 } else if (ret >= 0) {
954 ret = -EIO;
955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 kfree(status);
957 return ret;
958}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600959EXPORT_SYMBOL_GPL(usb_get_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961/**
962 * usb_clear_halt - tells device to clear endpoint halt/stall condition
963 * @dev: device whose endpoint is halted
964 * @pipe: endpoint "pipe" being cleared
965 * Context: !in_interrupt ()
966 *
967 * This is used to clear halt conditions for bulk and interrupt endpoints,
968 * as reported by URB completion status. Endpoints that are halted are
969 * sometimes referred to as being "stalled". Such endpoints are unable
970 * to transmit or receive data until the halt status is cleared. Any URBs
971 * queued for such an endpoint should normally be unlinked by the driver
972 * before clearing the halt condition, as described in sections 5.7.5
973 * and 5.8.5 of the USB 2.0 spec.
974 *
975 * Note that control and isochronous endpoints don't halt, although control
976 * endpoints report "protocol stall" (for unsupported requests) using the
977 * same status code used to report a true stall.
978 *
979 * This call is synchronous, and may not be used in an interrupt context.
980 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200981 * Return: Zero on success, or else the status code returned by the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 * underlying usb_control_msg() call.
983 */
984int usb_clear_halt(struct usb_device *dev, int pipe)
985{
986 int result;
987 int endp = usb_pipeendpoint(pipe);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800988
989 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 endp |= USB_DIR_IN;
991
992 /* we don't care if it wasn't halted first. in fact some devices
993 * (like some ibmcam model 1 units) seem to expect hosts to make
994 * this request for iso endpoints, which can't halt!
995 */
996 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
997 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
998 USB_ENDPOINT_HALT, endp, NULL, 0,
999 USB_CTRL_SET_TIMEOUT);
1000
1001 /* don't un-halt or force to DATA0 except on success */
1002 if (result < 0)
1003 return result;
1004
1005 /* NOTE: seems like Microsoft and Apple don't bother verifying
1006 * the clear "took", so some devices could lock up if you check...
1007 * such as the Hagiwara FlashGate DUAL. So we won't bother.
1008 *
1009 * NOTE: make sure the logic here doesn't diverge much from
1010 * the copy in usb-storage, for as long as we need two copies.
1011 */
1012
David Vrabel3444b262009-04-08 17:36:28 +00001013 usb_reset_endpoint(dev, endp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 return 0;
1016}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001017EXPORT_SYMBOL_GPL(usb_clear_halt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Alan Stern3b23dd62008-12-05 14:10:34 -05001019static int create_intf_ep_devs(struct usb_interface *intf)
1020{
1021 struct usb_device *udev = interface_to_usbdev(intf);
1022 struct usb_host_interface *alt = intf->cur_altsetting;
1023 int i;
1024
1025 if (intf->ep_devs_created || intf->unregistering)
1026 return 0;
1027
1028 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1029 (void) usb_create_ep_devs(&intf->dev, &alt->endpoint[i], udev);
1030 intf->ep_devs_created = 1;
1031 return 0;
1032}
1033
1034static void remove_intf_ep_devs(struct usb_interface *intf)
1035{
1036 struct usb_host_interface *alt = intf->cur_altsetting;
1037 int i;
1038
1039 if (!intf->ep_devs_created)
1040 return;
1041
1042 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1043 usb_remove_ep_devs(&alt->endpoint[i]);
1044 intf->ep_devs_created = 0;
1045}
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047/**
1048 * usb_disable_endpoint -- Disable an endpoint by address
1049 * @dev: the device whose endpoint is being disabled
1050 * @epaddr: the endpoint's address. Endpoint number for output,
1051 * endpoint number + USB_DIR_IN for input
Alan Sternddeac4e2009-01-15 17:03:33 -05001052 * @reset_hardware: flag to erase any endpoint state stored in the
1053 * controller hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 *
Alan Sternddeac4e2009-01-15 17:03:33 -05001055 * Disables the endpoint for URB submission and nukes all pending URBs.
1056 * If @reset_hardware is set then also deallocates hcd/hardware state
1057 * for the endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 */
Alan Sternddeac4e2009-01-15 17:03:33 -05001059void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr,
1060 bool reset_hardware)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1063 struct usb_host_endpoint *ep;
1064
1065 if (!dev)
1066 return;
1067
1068 if (usb_endpoint_out(epaddr)) {
1069 ep = dev->ep_out[epnum];
Alan Sternddeac4e2009-01-15 17:03:33 -05001070 if (reset_hardware)
1071 dev->ep_out[epnum] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 } else {
1073 ep = dev->ep_in[epnum];
Alan Sternddeac4e2009-01-15 17:03:33 -05001074 if (reset_hardware)
1075 dev->ep_in[epnum] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
Alan Sternbdd016b2007-07-30 17:05:22 -04001077 if (ep) {
1078 ep->enabled = 0;
Alan Stern95cf82f2007-09-10 11:33:05 -04001079 usb_hcd_flush_endpoint(dev, ep);
Alan Sternddeac4e2009-01-15 17:03:33 -05001080 if (reset_hardware)
1081 usb_hcd_disable_endpoint(dev, ep);
Alan Sternbdd016b2007-07-30 17:05:22 -04001082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
1085/**
David Vrabel3444b262009-04-08 17:36:28 +00001086 * usb_reset_endpoint - Reset an endpoint's state.
1087 * @dev: the device whose endpoint is to be reset
1088 * @epaddr: the endpoint's address. Endpoint number for output,
1089 * endpoint number + USB_DIR_IN for input
1090 *
1091 * Resets any host-side endpoint state such as the toggle bit,
1092 * sequence number or current window.
1093 */
1094void usb_reset_endpoint(struct usb_device *dev, unsigned int epaddr)
1095{
1096 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1097 struct usb_host_endpoint *ep;
1098
1099 if (usb_endpoint_out(epaddr))
1100 ep = dev->ep_out[epnum];
1101 else
1102 ep = dev->ep_in[epnum];
1103 if (ep)
1104 usb_hcd_reset_endpoint(dev, ep);
1105}
1106EXPORT_SYMBOL_GPL(usb_reset_endpoint);
1107
1108
1109/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 * usb_disable_interface -- Disable all endpoints for an interface
1111 * @dev: the device whose interface is being disabled
1112 * @intf: pointer to the interface descriptor
Alan Sternddeac4e2009-01-15 17:03:33 -05001113 * @reset_hardware: flag to erase any endpoint state stored in the
1114 * controller hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 *
1116 * Disables all the endpoints for the interface's current altsetting.
1117 */
Alan Sternddeac4e2009-01-15 17:03:33 -05001118void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf,
1119 bool reset_hardware)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 struct usb_host_interface *alt = intf->cur_altsetting;
1122 int i;
1123
1124 for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
1125 usb_disable_endpoint(dev,
Alan Sternddeac4e2009-01-15 17:03:33 -05001126 alt->endpoint[i].desc.bEndpointAddress,
1127 reset_hardware);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129}
1130
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001131/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 * usb_disable_device - Disable all the endpoints for a USB device
1133 * @dev: the device whose endpoints are being disabled
1134 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
1135 *
1136 * Disables all the device's endpoints, potentially including endpoint 0.
1137 * Deallocates hcd/hardware state for the endpoints (nuking all or most
1138 * pending urbs) and usbcore state for the interfaces, so that usbcore
1139 * must usb_set_configuration() before any interfaces could be used.
1140 */
1141void usb_disable_device(struct usb_device *dev, int skip_ep0)
1142{
1143 int i;
Sarah Sharpfccf4e82011-06-05 23:22:22 -07001144 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 /* getting rid of interfaces will disconnect
1147 * any drivers bound to them (a key side effect)
1148 */
1149 if (dev->actconfig) {
Alan Sternca5c4852011-07-06 17:03:45 -04001150 /*
1151 * FIXME: In order to avoid self-deadlock involving the
1152 * bandwidth_mutex, we have to mark all the interfaces
1153 * before unregistering any of them.
1154 */
1155 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++)
1156 dev->actconfig->interface[i]->unregistering = 1;
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1159 struct usb_interface *interface;
1160
Alan Stern86d30742005-07-29 12:17:16 -07001161 /* remove this interface if it has been registered */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 interface = dev->actconfig->interface[i];
Daniel Ritzd305ef52005-09-22 00:47:24 -07001163 if (!device_is_registered(&interface->dev))
Alan Stern86d30742005-07-29 12:17:16 -07001164 continue;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001165 dev_dbg(&dev->dev, "unregistering interface %s\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001166 dev_name(&interface->dev));
Alan Stern3b23dd62008-12-05 14:10:34 -05001167 remove_intf_ep_devs(interface);
Alan Stern1a211752008-07-30 11:31:50 -04001168 device_del(&interface->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
1170
1171 /* Now that the interfaces are unbound, nobody should
1172 * try to access them.
1173 */
1174 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001175 put_device(&dev->actconfig->interface[i]->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 dev->actconfig->interface[i] = NULL;
1177 }
Sarah Sharpf468f7b2013-10-08 08:28:43 -07001178
1179 if (dev->usb2_hw_lpm_enabled == 1)
1180 usb_set_usb2_hardware_lpm(dev, 0);
Sarah Sharp24971912012-07-05 14:09:30 -07001181 usb_unlocked_disable_lpm(dev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07001182 usb_disable_ltm(dev);
Sarah Sharpf468f7b2013-10-08 08:28:43 -07001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 dev->actconfig = NULL;
1185 if (dev->state == USB_STATE_CONFIGURED)
1186 usb_set_device_state(dev, USB_STATE_ADDRESS);
1187 }
Alan Stern80f0cf32010-09-30 15:16:23 -04001188
1189 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __func__,
1190 skip_ep0 ? "non-ep0" : "all");
Sarah Sharpfccf4e82011-06-05 23:22:22 -07001191 if (hcd->driver->check_bandwidth) {
1192 /* First pass: Cancel URBs, leave endpoint pointers intact. */
1193 for (i = skip_ep0; i < 16; ++i) {
1194 usb_disable_endpoint(dev, i, false);
1195 usb_disable_endpoint(dev, i + USB_DIR_IN, false);
1196 }
1197 /* Remove endpoints from the host controller internal state */
Alan Stern8963c482012-04-17 15:22:39 -04001198 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharpfccf4e82011-06-05 23:22:22 -07001199 usb_hcd_alloc_bandwidth(dev, NULL, NULL, NULL);
Alan Stern8963c482012-04-17 15:22:39 -04001200 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharpfccf4e82011-06-05 23:22:22 -07001201 /* Second pass: remove endpoint pointers */
1202 }
Alan Stern80f0cf32010-09-30 15:16:23 -04001203 for (i = skip_ep0; i < 16; ++i) {
1204 usb_disable_endpoint(dev, i, true);
1205 usb_disable_endpoint(dev, i + USB_DIR_IN, true);
1206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001209/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 * usb_enable_endpoint - Enable an endpoint for USB communications
1211 * @dev: the device whose interface is being enabled
1212 * @ep: the endpoint
David Vrabel3444b262009-04-08 17:36:28 +00001213 * @reset_ep: flag to reset the endpoint state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 *
David Vrabel3444b262009-04-08 17:36:28 +00001215 * Resets the endpoint state if asked, and sets dev->ep_{in,out} pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 * For control endpoints, both the input and output sides are handled.
1217 */
Alan Stern2caf7fc2008-12-31 11:31:33 -05001218void usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep,
David Vrabel3444b262009-04-08 17:36:28 +00001219 bool reset_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Alan Sternbdd016b2007-07-30 17:05:22 -04001221 int epnum = usb_endpoint_num(&ep->desc);
1222 int is_out = usb_endpoint_dir_out(&ep->desc);
1223 int is_control = usb_endpoint_xfer_control(&ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
David Vrabel3444b262009-04-08 17:36:28 +00001225 if (reset_ep)
1226 usb_hcd_reset_endpoint(dev, ep);
1227 if (is_out || is_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 dev->ep_out[epnum] = ep;
David Vrabel3444b262009-04-08 17:36:28 +00001229 if (!is_out || is_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 dev->ep_in[epnum] = ep;
Alan Sternbdd016b2007-07-30 17:05:22 -04001231 ep->enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001234/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 * usb_enable_interface - Enable all the endpoints for an interface
1236 * @dev: the device whose interface is being enabled
1237 * @intf: pointer to the interface descriptor
David Vrabel3444b262009-04-08 17:36:28 +00001238 * @reset_eps: flag to reset the endpoints' state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 *
1240 * Enables all the endpoints for the interface's current altsetting.
1241 */
Alan Stern2caf7fc2008-12-31 11:31:33 -05001242void usb_enable_interface(struct usb_device *dev,
David Vrabel3444b262009-04-08 17:36:28 +00001243 struct usb_interface *intf, bool reset_eps)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 struct usb_host_interface *alt = intf->cur_altsetting;
1246 int i;
1247
1248 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
David Vrabel3444b262009-04-08 17:36:28 +00001249 usb_enable_endpoint(dev, &alt->endpoint[i], reset_eps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
1252/**
1253 * usb_set_interface - Makes a particular alternate setting be current
1254 * @dev: the device whose interface is being updated
1255 * @interface: the interface being updated
1256 * @alternate: the setting being chosen.
1257 * Context: !in_interrupt ()
1258 *
1259 * This is used to enable data transfers on interfaces that may not
1260 * be enabled by default. Not all devices support such configurability.
1261 * Only the driver bound to an interface may change its setting.
1262 *
1263 * Within any given configuration, each interface may have several
1264 * alternative settings. These are often used to control levels of
1265 * bandwidth consumption. For example, the default setting for a high
1266 * speed interrupt endpoint may not send more than 64 bytes per microframe,
1267 * while interrupt transfers of up to 3KBytes per microframe are legal.
1268 * Also, isochronous endpoints may never be part of an
1269 * interface's default setting. To access such bandwidth, alternate
1270 * interface settings must be made current.
1271 *
1272 * Note that in the Linux USB subsystem, bandwidth associated with
1273 * an endpoint in a given alternate setting is not reserved until an URB
1274 * is submitted that needs that bandwidth. Some other operating systems
1275 * allocate bandwidth early, when a configuration is chosen.
1276 *
1277 * This call is synchronous, and may not be used in an interrupt context.
1278 * Also, drivers must not change altsettings while urbs are scheduled for
1279 * endpoints in that interface; all such urbs must first be completed
1280 * (perhaps forced by unlinking).
1281 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02001282 * Return: Zero on success, or else the status code returned by the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 * underlying usb_control_msg() call.
1284 */
1285int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1286{
1287 struct usb_interface *iface;
1288 struct usb_host_interface *alt;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001289 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
Hans de Goede7a7b5622013-11-08 16:37:26 +01001290 int i, ret, manual = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001291 unsigned int epaddr;
1292 unsigned int pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 if (dev->state == USB_STATE_SUSPENDED)
1295 return -EHOSTUNREACH;
1296
1297 iface = usb_ifnum_to_if(dev, interface);
1298 if (!iface) {
1299 dev_dbg(&dev->dev, "selecting invalid interface %d\n",
1300 interface);
1301 return -EINVAL;
1302 }
Alan Sterne534c5b2011-07-01 16:43:02 -04001303 if (iface->unregistering)
1304 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 alt = usb_altnum_to_altsetting(iface, alternate);
1307 if (!alt) {
Thadeu Lima de Souza Cascardo385f6902010-01-17 19:24:03 -02001308 dev_warn(&dev->dev, "selecting invalid altsetting %d\n",
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -07001309 alternate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return -EINVAL;
1311 }
1312
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001313 /* Make sure we have enough bandwidth for this alternate interface.
1314 * Remove the current alt setting and add the new alt setting.
1315 */
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001316 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp83060952012-05-02 14:25:52 -07001317 /* Disable LPM, and re-enable it once the new alt setting is installed,
1318 * so that the xHCI driver can recalculate the U1/U2 timeouts.
1319 */
1320 if (usb_disable_lpm(dev)) {
1321 dev_err(&iface->dev, "%s Failed to disable LPM\n.", __func__);
1322 mutex_unlock(hcd->bandwidth_mutex);
1323 return -ENOMEM;
1324 }
Hans de Goede7a7b5622013-11-08 16:37:26 +01001325 /* Changing alt-setting also frees any allocated streams */
1326 for (i = 0; i < iface->cur_altsetting->desc.bNumEndpoints; i++)
1327 iface->cur_altsetting->endpoint[i].streams = 0;
1328
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001329 ret = usb_hcd_alloc_bandwidth(dev, NULL, iface->cur_altsetting, alt);
1330 if (ret < 0) {
1331 dev_info(&dev->dev, "Not enough bandwidth for altsetting %d\n",
1332 alternate);
Sarah Sharp83060952012-05-02 14:25:52 -07001333 usb_enable_lpm(dev);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001334 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001335 return ret;
1336 }
1337
Alan Stern392e1d92008-03-11 10:20:12 -04001338 if (dev->quirks & USB_QUIRK_NO_SET_INTF)
1339 ret = -EPIPE;
1340 else
1341 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1343 alternate, interface, NULL, 0, 5000);
1344
1345 /* 9.4.10 says devices don't need this and are free to STALL the
1346 * request if the interface only has one alternate setting.
1347 */
1348 if (ret == -EPIPE && iface->num_altsetting == 1) {
1349 dev_dbg(&dev->dev,
1350 "manual set_interface for iface %d, alt %d\n",
1351 interface, alternate);
1352 manual = 1;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001353 } else if (ret < 0) {
1354 /* Re-instate the old alt setting */
1355 usb_hcd_alloc_bandwidth(dev, NULL, alt, iface->cur_altsetting);
Sarah Sharp83060952012-05-02 14:25:52 -07001356 usb_enable_lpm(dev);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001357 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return ret;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001359 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001360 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 /* FIXME drivers shouldn't need to replicate/bugfix the logic here
1363 * when they implement async or easily-killable versions of this or
1364 * other "should-be-internal" functions (like clear_halt).
1365 * should hcd+usbcore postprocess control requests?
1366 */
1367
1368 /* prevent submissions using previous endpoint settings */
Alan Stern3b23dd62008-12-05 14:10:34 -05001369 if (iface->cur_altsetting != alt) {
1370 remove_intf_ep_devs(iface);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001371 usb_remove_sysfs_intf_files(iface);
Alan Stern3b23dd62008-12-05 14:10:34 -05001372 }
Alan Sternddeac4e2009-01-15 17:03:33 -05001373 usb_disable_interface(dev, iface, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 iface->cur_altsetting = alt;
1376
Sarah Sharp83060952012-05-02 14:25:52 -07001377 /* Now that the interface is installed, re-enable LPM. */
1378 usb_unlocked_enable_lpm(dev);
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 /* If the interface only has one altsetting and the device didn't
David Brownella81e7ec2005-04-18 17:39:25 -07001381 * accept the request, we attempt to carry out the equivalent action
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 * by manually clearing the HALT feature for each endpoint in the
1383 * new altsetting.
1384 */
1385 if (manual) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001387 epaddr = alt->endpoint[i].desc.bEndpointAddress;
1388 pipe = __create_pipe(dev,
1389 USB_ENDPOINT_NUMBER_MASK & epaddr) |
1390 (usb_endpoint_out(epaddr) ?
1391 USB_DIR_OUT : USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 usb_clear_halt(dev, pipe);
1394 }
1395 }
1396
1397 /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
1398 *
1399 * Note:
1400 * Despite EP0 is always present in all interfaces/AS, the list of
1401 * endpoints from the descriptor does not contain EP0. Due to its
1402 * omnipresence one might expect EP0 being considered "affected" by
1403 * any SetInterface request and hence assume toggles need to be reset.
1404 * However, EP0 toggles are re-synced for every individual transfer
1405 * during the SETUP stage - hence EP0 toggles are "don't care" here.
1406 * (Likewise, EP0 never "halts" on well designed devices.)
1407 */
Alan Stern2caf7fc2008-12-31 11:31:33 -05001408 usb_enable_interface(dev, iface, true);
Alan Stern3b23dd62008-12-05 14:10:34 -05001409 if (device_is_registered(&iface->dev)) {
Alan Stern0e6c8e82005-10-24 15:33:03 -04001410 usb_create_sysfs_intf_files(iface);
Alan Stern3b23dd62008-12-05 14:10:34 -05001411 create_intf_ep_devs(iface);
1412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return 0;
1414}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001415EXPORT_SYMBOL_GPL(usb_set_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417/**
1418 * usb_reset_configuration - lightweight device reset
1419 * @dev: the device whose configuration is being reset
1420 *
1421 * This issues a standard SET_CONFIGURATION request to the device using
1422 * the current configuration. The effect is to reset most USB-related
1423 * state in the device, including interface altsettings (reset to zero),
David Vrabel3444b262009-04-08 17:36:28 +00001424 * endpoint halts (cleared), and endpoint state (only for bulk and interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 * endpoints). Other usbcore state is unchanged, including bindings of
1426 * usb device drivers to interfaces.
1427 *
1428 * Because this affects multiple interfaces, avoid using this with composite
1429 * (multi-interface) devices. Instead, the driver for each interface may
David Brownella81e7ec2005-04-18 17:39:25 -07001430 * use usb_set_interface() on the interfaces it claims. Be careful though;
1431 * some devices don't support the SET_INTERFACE request, and others won't
David Vrabel3444b262009-04-08 17:36:28 +00001432 * reset all the interface state (notably endpoint state). Resetting the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 * configuration would affect other drivers' interfaces.
1434 *
1435 * The caller must own the device lock.
1436 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02001437 * Return: Zero on success, else a negative error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 */
1439int usb_reset_configuration(struct usb_device *dev)
1440{
1441 int i, retval;
1442 struct usb_host_config *config;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001443 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 if (dev->state == USB_STATE_SUSPENDED)
1446 return -EHOSTUNREACH;
1447
1448 /* caller must have locked the device and must own
1449 * the usb bus readlock (so driver bindings are stable);
1450 * calls during probe() are fine
1451 */
1452
1453 for (i = 1; i < 16; ++i) {
Alan Sternddeac4e2009-01-15 17:03:33 -05001454 usb_disable_endpoint(dev, i, true);
1455 usb_disable_endpoint(dev, i + USB_DIR_IN, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457
1458 config = dev->actconfig;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001459 retval = 0;
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001460 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp83060952012-05-02 14:25:52 -07001461 /* Disable LPM, and re-enable it once the configuration is reset, so
1462 * that the xHCI driver can recalculate the U1/U2 timeouts.
1463 */
1464 if (usb_disable_lpm(dev)) {
1465 dev_err(&dev->dev, "%s Failed to disable LPM\n.", __func__);
1466 mutex_unlock(hcd->bandwidth_mutex);
1467 return -ENOMEM;
1468 }
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001469 /* Make sure we have enough bandwidth for each alternate setting 0 */
1470 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1471 struct usb_interface *intf = config->interface[i];
1472 struct usb_host_interface *alt;
1473
1474 alt = usb_altnum_to_altsetting(intf, 0);
1475 if (!alt)
1476 alt = &intf->altsetting[0];
1477 if (alt != intf->cur_altsetting)
1478 retval = usb_hcd_alloc_bandwidth(dev, NULL,
1479 intf->cur_altsetting, alt);
1480 if (retval < 0)
1481 break;
1482 }
1483 /* If not, reinstate the old alternate settings */
1484 if (retval < 0) {
1485reset_old_alts:
Roel Kluine4a3d942010-02-18 02:36:23 +01001486 for (i--; i >= 0; i--) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001487 struct usb_interface *intf = config->interface[i];
1488 struct usb_host_interface *alt;
1489
1490 alt = usb_altnum_to_altsetting(intf, 0);
1491 if (!alt)
1492 alt = &intf->altsetting[0];
1493 if (alt != intf->cur_altsetting)
1494 usb_hcd_alloc_bandwidth(dev, NULL,
1495 alt, intf->cur_altsetting);
1496 }
Sarah Sharp83060952012-05-02 14:25:52 -07001497 usb_enable_lpm(dev);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001498 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001499 return retval;
1500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1502 USB_REQ_SET_CONFIGURATION, 0,
1503 config->desc.bConfigurationValue, 0,
1504 NULL, 0, USB_CTRL_SET_TIMEOUT);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001505 if (retval < 0)
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001506 goto reset_old_alts;
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001507 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 /* re-init hc/hcd interface/endpoint state */
1510 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1511 struct usb_interface *intf = config->interface[i];
1512 struct usb_host_interface *alt;
1513
1514 alt = usb_altnum_to_altsetting(intf, 0);
1515
1516 /* No altsetting 0? We'll assume the first altsetting.
1517 * We could use a GetInterface call, but if a device is
1518 * so non-compliant that it doesn't have altsetting 0
1519 * then I wouldn't trust its reply anyway.
1520 */
1521 if (!alt)
1522 alt = &intf->altsetting[0];
1523
Alan Stern3b23dd62008-12-05 14:10:34 -05001524 if (alt != intf->cur_altsetting) {
1525 remove_intf_ep_devs(intf);
1526 usb_remove_sysfs_intf_files(intf);
1527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 intf->cur_altsetting = alt;
Alan Stern2caf7fc2008-12-31 11:31:33 -05001529 usb_enable_interface(dev, intf, true);
Alan Stern3b23dd62008-12-05 14:10:34 -05001530 if (device_is_registered(&intf->dev)) {
Alan Stern0e6c8e82005-10-24 15:33:03 -04001531 usb_create_sysfs_intf_files(intf);
Alan Stern3b23dd62008-12-05 14:10:34 -05001532 create_intf_ep_devs(intf);
1533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
Sarah Sharp83060952012-05-02 14:25:52 -07001535 /* Now that the interfaces are installed, re-enable LPM. */
1536 usb_unlocked_enable_lpm(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return 0;
1538}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001539EXPORT_SYMBOL_GPL(usb_reset_configuration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Greg Kroah-Hartmanb0e396e2007-08-02 22:44:27 -06001541static void usb_release_interface(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 struct usb_interface *intf = to_usb_interface(dev);
1544 struct usb_interface_cache *intfc =
1545 altsetting_to_usb_interface_cache(intf->altsetting);
1546
1547 kref_put(&intfc->ref, usb_release_interface_cache);
Alan Stern524134d2015-01-21 14:02:43 -05001548 usb_put_dev(interface_to_usbdev(intf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 kfree(intf);
1550}
1551
Stefan Kochb3910ce2015-08-25 21:10:08 +02001552/*
1553 * usb_deauthorize_interface - deauthorize an USB interface
1554 *
1555 * @intf: USB interface structure
1556 */
1557void usb_deauthorize_interface(struct usb_interface *intf)
1558{
1559 struct device *dev = &intf->dev;
1560
1561 device_lock(dev->parent);
1562
1563 if (intf->authorized) {
1564 device_lock(dev);
1565 intf->authorized = 0;
1566 device_unlock(dev);
1567
1568 usb_forced_unbind_intf(intf);
1569 }
1570
1571 device_unlock(dev->parent);
1572}
1573
1574/*
1575 * usb_authorize_interface - authorize an USB interface
1576 *
1577 * @intf: USB interface structure
1578 */
1579void usb_authorize_interface(struct usb_interface *intf)
1580{
1581 struct device *dev = &intf->dev;
1582
1583 if (!intf->authorized) {
1584 device_lock(dev);
1585 intf->authorized = 1; /* authorize interface */
1586 device_unlock(dev);
1587 }
1588}
1589
Kay Sievers7eff2e72007-08-14 15:15:12 +02001590static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001591{
1592 struct usb_device *usb_dev;
1593 struct usb_interface *intf;
1594 struct usb_host_interface *alt;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001595
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001596 intf = to_usb_interface(dev);
1597 usb_dev = interface_to_usbdev(intf);
1598 alt = intf->cur_altsetting;
1599
Kay Sievers7eff2e72007-08-14 15:15:12 +02001600 if (add_uevent_var(env, "INTERFACE=%d/%d/%d",
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001601 alt->desc.bInterfaceClass,
1602 alt->desc.bInterfaceSubClass,
1603 alt->desc.bInterfaceProtocol))
1604 return -ENOMEM;
1605
Kay Sievers7eff2e72007-08-14 15:15:12 +02001606 if (add_uevent_var(env,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001607 "MODALIAS=usb:"
Bjørn Mork81df2d52012-05-18 21:27:43 +02001608 "v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02Xin%02X",
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001609 le16_to_cpu(usb_dev->descriptor.idVendor),
1610 le16_to_cpu(usb_dev->descriptor.idProduct),
1611 le16_to_cpu(usb_dev->descriptor.bcdDevice),
1612 usb_dev->descriptor.bDeviceClass,
1613 usb_dev->descriptor.bDeviceSubClass,
1614 usb_dev->descriptor.bDeviceProtocol,
1615 alt->desc.bInterfaceClass,
1616 alt->desc.bInterfaceSubClass,
Bjørn Mork81df2d52012-05-18 21:27:43 +02001617 alt->desc.bInterfaceProtocol,
1618 alt->desc.bInterfaceNumber))
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001619 return -ENOMEM;
1620
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001621 return 0;
1622}
1623
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001624struct device_type usb_if_device_type = {
1625 .name = "usb_interface",
1626 .release = usb_release_interface,
1627 .uevent = usb_if_uevent,
1628};
1629
Craig W. Nadler165fe972007-06-15 23:14:35 -04001630static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001631 struct usb_host_config *config,
1632 u8 inum)
Craig W. Nadler165fe972007-06-15 23:14:35 -04001633{
1634 struct usb_interface_assoc_descriptor *retval = NULL;
1635 struct usb_interface_assoc_descriptor *intf_assoc;
1636 int first_intf;
1637 int last_intf;
1638 int i;
1639
1640 for (i = 0; (i < USB_MAXIADS && config->intf_assoc[i]); i++) {
1641 intf_assoc = config->intf_assoc[i];
1642 if (intf_assoc->bInterfaceCount == 0)
1643 continue;
1644
1645 first_intf = intf_assoc->bFirstInterface;
1646 last_intf = first_intf + (intf_assoc->bInterfaceCount - 1);
1647 if (inum >= first_intf && inum <= last_intf) {
1648 if (!retval)
1649 retval = intf_assoc;
1650 else
1651 dev_err(&dev->dev, "Interface #%d referenced"
1652 " by multiple IADs\n", inum);
1653 }
1654 }
1655
1656 return retval;
1657}
1658
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001659
1660/*
1661 * Internal function to queue a device reset
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001662 * See usb_queue_reset_device() for more details
1663 */
Felipe Balbi09e81f32009-12-04 15:47:44 +02001664static void __usb_queue_reset_device(struct work_struct *ws)
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001665{
1666 int rc;
1667 struct usb_interface *iface =
1668 container_of(ws, struct usb_interface, reset_ws);
1669 struct usb_device *udev = interface_to_usbdev(iface);
1670
1671 rc = usb_lock_device_for_reset(udev, iface);
1672 if (rc >= 0) {
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001673 usb_reset_device(udev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001674 usb_unlock_device(udev);
1675 }
Alan Stern524134d2015-01-21 14:02:43 -05001676 usb_put_intf(iface); /* Undo _get_ in usb_queue_reset_device() */
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001677}
1678
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680/*
1681 * usb_set_configuration - Makes a particular device setting be current
1682 * @dev: the device whose configuration is being updated
1683 * @configuration: the configuration being chosen.
1684 * Context: !in_interrupt(), caller owns the device lock
1685 *
1686 * This is used to enable non-default device modes. Not all devices
1687 * use this kind of configurability; many devices only have one
1688 * configuration.
1689 *
Alan Stern3f141e22007-02-08 16:40:43 -05001690 * @configuration is the value of the configuration to be installed.
1691 * According to the USB spec (e.g. section 9.1.1.5), configuration values
1692 * must be non-zero; a value of zero indicates that the device in
1693 * unconfigured. However some devices erroneously use 0 as one of their
1694 * configuration values. To help manage such devices, this routine will
1695 * accept @configuration = -1 as indicating the device should be put in
1696 * an unconfigured state.
1697 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 * USB device configurations may affect Linux interoperability,
1699 * power consumption and the functionality available. For example,
1700 * the default configuration is limited to using 100mA of bus power,
1701 * so that when certain device functionality requires more power,
1702 * and the device is bus powered, that functionality should be in some
1703 * non-default device configuration. Other device modes may also be
1704 * reflected as configuration options, such as whether two ISDN
1705 * channels are available independently; and choosing between open
1706 * standard device protocols (like CDC) or proprietary ones.
1707 *
Inaky Perez-Gonzalez16bbab22007-07-31 20:34:01 -07001708 * Note that a non-authorized device (dev->authorized == 0) will only
1709 * be put in unconfigured mode.
1710 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 * Note that USB has an additional level of device configurability,
1712 * associated with interfaces. That configurability is accessed using
1713 * usb_set_interface().
1714 *
1715 * This call is synchronous. The calling context must be able to sleep,
1716 * must own the device lock, and must not hold the driver model's USB
Ming Lei6d243e52008-06-17 23:24:08 +08001717 * bus mutex; usb interface driver probe() methods cannot use this routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 *
1719 * Returns zero on success, or else the status code returned by the
Steven Cole093cf722005-05-03 19:07:24 -06001720 * underlying call that failed. On successful completion, each interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 * in the original device configuration has been destroyed, and each one
1722 * in the new configuration has been probed by all relevant usb device
1723 * drivers currently known to the kernel.
1724 */
1725int usb_set_configuration(struct usb_device *dev, int configuration)
1726{
1727 int i, ret;
1728 struct usb_host_config *cp = NULL;
1729 struct usb_interface **new_interfaces = NULL;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001730 struct usb_hcd *hcd = bus_to_hcd(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 int n, nintf;
1732
Inaky Perez-Gonzalez16bbab22007-07-31 20:34:01 -07001733 if (dev->authorized == 0 || configuration == -1)
Alan Stern3f141e22007-02-08 16:40:43 -05001734 configuration = 0;
1735 else {
1736 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1737 if (dev->config[i].desc.bConfigurationValue ==
1738 configuration) {
1739 cp = &dev->config[i];
1740 break;
1741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743 }
1744 if ((!cp && configuration != 0))
1745 return -EINVAL;
1746
1747 /* The USB spec says configuration 0 means unconfigured.
1748 * But if a device includes a configuration numbered 0,
1749 * we will accept it as a correctly configured state.
Alan Stern3f141e22007-02-08 16:40:43 -05001750 * Use -1 if you really want to unconfigure the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 */
1752 if (cp && configuration == 0)
1753 dev_warn(&dev->dev, "config 0 descriptor??\n");
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 /* Allocate memory for new interfaces before doing anything else,
1756 * so that if we run out then nothing will have changed. */
1757 n = nintf = 0;
1758 if (cp) {
1759 nintf = cp->desc.bNumInterfaces;
1760 new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
Oliver Neukumacbe2fe2010-01-12 12:32:50 +01001761 GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if (!new_interfaces) {
Joe Perches898eb712007-10-18 03:06:30 -07001763 dev_err(&dev->dev, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return -ENOMEM;
1765 }
1766
1767 for (; n < nintf; ++n) {
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001768 new_interfaces[n] = kzalloc(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 sizeof(struct usb_interface),
Oliver Neukumacbe2fe2010-01-12 12:32:50 +01001770 GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (!new_interfaces[n]) {
Joe Perches898eb712007-10-18 03:06:30 -07001772 dev_err(&dev->dev, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 ret = -ENOMEM;
1774free_interfaces:
1775 while (--n >= 0)
1776 kfree(new_interfaces[n]);
1777 kfree(new_interfaces);
1778 return ret;
1779 }
1780 }
Alan Stern6ad07122006-06-01 13:59:16 -04001781
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +01001782 i = dev->bus_mA - usb_get_max_power(dev, cp);
Alan Stern6ad07122006-06-01 13:59:16 -04001783 if (i < 0)
1784 dev_warn(&dev->dev, "new config #%d exceeds power "
1785 "limit by %dmA\n",
1786 configuration, -i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788
Alan Stern01d883d2006-08-30 15:47:18 -04001789 /* Wake up the device so we can send it the Set-Config request */
Alan Stern94fcda12006-11-20 11:38:46 -05001790 ret = usb_autoresume_device(dev);
Alan Stern01d883d2006-08-30 15:47:18 -04001791 if (ret)
1792 goto free_interfaces;
1793
Thadeu Lima de Souza Cascardo07919712010-08-28 03:06:29 -03001794 /* if it's already configured, clear out old state first.
1795 * getting rid of old interfaces means unbinding their drivers.
1796 */
1797 if (dev->state != USB_STATE_ADDRESS)
1798 usb_disable_device(dev, 1); /* Skip ep0 */
1799
1800 /* Get rid of pending async Set-Config requests for this device */
1801 cancel_async_set_config(dev);
1802
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001803 /* Make sure we have bandwidth (and available HCD resources) for this
1804 * configuration. Remove endpoints from the schedule if we're dropping
1805 * this configuration to set configuration 0. After this point, the
1806 * host controller will not allow submissions to dropped endpoints. If
1807 * this call fails, the device state is unchanged.
1808 */
Alan Stern8963c482012-04-17 15:22:39 -04001809 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp83060952012-05-02 14:25:52 -07001810 /* Disable LPM, and re-enable it once the new configuration is
1811 * installed, so that the xHCI driver can recalculate the U1/U2
1812 * timeouts.
1813 */
Sarah Sharp9cf65992012-07-03 23:22:38 -07001814 if (dev->actconfig && usb_disable_lpm(dev)) {
Sarah Sharp83060952012-05-02 14:25:52 -07001815 dev_err(&dev->dev, "%s Failed to disable LPM\n.", __func__);
1816 mutex_unlock(hcd->bandwidth_mutex);
Sachin Kamatc058f7a2012-11-21 11:01:19 +05301817 ret = -ENOMEM;
1818 goto free_interfaces;
Sarah Sharp83060952012-05-02 14:25:52 -07001819 }
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001820 ret = usb_hcd_alloc_bandwidth(dev, cp, NULL, NULL);
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001821 if (ret < 0) {
Sarah Sharp9cf65992012-07-03 23:22:38 -07001822 if (dev->actconfig)
1823 usb_enable_lpm(dev);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07001824 mutex_unlock(hcd->bandwidth_mutex);
Thadeu Lima de Souza Cascardo07919712010-08-28 03:06:29 -03001825 usb_autosuspend_device(dev);
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001826 goto free_interfaces;
1827 }
1828
Alan Stern36caff52012-11-07 10:31:30 -05001829 /*
1830 * Initialize the new interface structures and the
Alan Stern6ad07122006-06-01 13:59:16 -04001831 * hc/hcd/usbcore interface/endpoint state.
1832 */
1833 for (i = 0; i < nintf; ++i) {
1834 struct usb_interface_cache *intfc;
1835 struct usb_interface *intf;
1836 struct usb_host_interface *alt;
1837
1838 cp->interface[i] = intf = new_interfaces[i];
1839 intfc = cp->intf_cache[i];
1840 intf->altsetting = intfc->altsetting;
1841 intf->num_altsetting = intfc->num_altsetting;
Stefan Koch6b2bd3c2015-08-25 21:10:06 +02001842 intf->authorized = !!HCD_INTF_AUTHORIZED(hcd);
Alan Stern6ad07122006-06-01 13:59:16 -04001843 kref_get(&intfc->ref);
1844
1845 alt = usb_altnum_to_altsetting(intf, 0);
1846
1847 /* No altsetting 0? We'll assume the first altsetting.
1848 * We could use a GetInterface call, but if a device is
1849 * so non-compliant that it doesn't have altsetting 0
1850 * then I wouldn't trust its reply anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 */
Alan Stern6ad07122006-06-01 13:59:16 -04001852 if (!alt)
1853 alt = &intf->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Daniel Mackb3a3dd02012-06-12 20:23:52 +02001855 intf->intf_assoc =
1856 find_iad(dev, cp, alt->desc.bInterfaceNumber);
Alan Stern6ad07122006-06-01 13:59:16 -04001857 intf->cur_altsetting = alt;
Alan Stern2caf7fc2008-12-31 11:31:33 -05001858 usb_enable_interface(dev, intf, true);
Alan Stern6ad07122006-06-01 13:59:16 -04001859 intf->dev.parent = &dev->dev;
1860 intf->dev.driver = NULL;
1861 intf->dev.bus = &usb_bus_type;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001862 intf->dev.type = &usb_if_device_type;
Alan Stern2e5f10e2008-04-30 15:37:19 -04001863 intf->dev.groups = usb_interface_groups;
Alan Stern6ad07122006-06-01 13:59:16 -04001864 intf->dev.dma_mask = dev->dev.dma_mask;
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001865 INIT_WORK(&intf->reset_ws, __usb_queue_reset_device);
Alan Stern0026e002010-09-21 15:01:53 -04001866 intf->minor = -1;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001867 device_initialize(&intf->dev);
Ming Lei63defa72010-11-15 15:56:54 -05001868 pm_runtime_no_callbacks(&intf->dev);
Kay Sievers0031a062008-05-02 06:02:41 +02001869 dev_set_name(&intf->dev, "%d-%s:%d.%d",
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001870 dev->bus->busnum, dev->devpath,
1871 configuration, alt->desc.bInterfaceNumber);
Alan Stern524134d2015-01-21 14:02:43 -05001872 usb_get_dev(dev);
Alan Stern6ad07122006-06-01 13:59:16 -04001873 }
1874 kfree(new_interfaces);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Alan Stern36caff52012-11-07 10:31:30 -05001876 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1877 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
1878 NULL, 0, USB_CTRL_SET_TIMEOUT);
1879 if (ret < 0 && cp) {
1880 /*
1881 * All the old state is gone, so what else can we do?
1882 * The device is probably useless now anyway.
1883 */
1884 usb_hcd_alloc_bandwidth(dev, NULL, NULL, NULL);
1885 for (i = 0; i < nintf; ++i) {
1886 usb_disable_interface(dev, cp->interface[i], true);
1887 put_device(&cp->interface[i]->dev);
1888 cp->interface[i] = NULL;
1889 }
1890 cp = NULL;
1891 }
1892
1893 dev->actconfig = cp;
1894 mutex_unlock(hcd->bandwidth_mutex);
1895
1896 if (!cp) {
1897 usb_set_device_state(dev, USB_STATE_ADDRESS);
1898
1899 /* Leave LPM disabled while the device is unconfigured. */
1900 usb_autosuspend_device(dev);
1901 return ret;
1902 }
1903 usb_set_device_state(dev, USB_STATE_CONFIGURED);
1904
Alan Stern1662e3a2009-03-18 14:28:53 -04001905 if (cp->string == NULL &&
1906 !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
Alan Stern6ad07122006-06-01 13:59:16 -04001907 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Sarah Sharp83060952012-05-02 14:25:52 -07001909 /* Now that the interfaces are installed, re-enable LPM. */
1910 usb_unlocked_enable_lpm(dev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07001911 /* Enable LTM if it was turned off by usb_disable_device. */
1912 usb_enable_ltm(dev);
Sarah Sharp83060952012-05-02 14:25:52 -07001913
Alan Stern6ad07122006-06-01 13:59:16 -04001914 /* Now that all the interfaces are set up, register them
1915 * to trigger binding of drivers to interfaces. probe()
1916 * routines may install different altsettings and may
1917 * claim() any interfaces not yet bound. Many class drivers
1918 * need that: CDC, audio, video, etc.
1919 */
1920 for (i = 0; i < nintf; ++i) {
1921 struct usb_interface *intf = cp->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001923 dev_dbg(&dev->dev,
Alan Stern6ad07122006-06-01 13:59:16 -04001924 "adding %s (config #%d, interface %d)\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001925 dev_name(&intf->dev), configuration,
Alan Stern6ad07122006-06-01 13:59:16 -04001926 intf->cur_altsetting->desc.bInterfaceNumber);
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01001927 device_enable_async_suspend(&intf->dev);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001928 ret = device_add(&intf->dev);
Alan Stern6ad07122006-06-01 13:59:16 -04001929 if (ret != 0) {
1930 dev_err(&dev->dev, "device_add(%s) --> %d\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001931 dev_name(&intf->dev), ret);
Alan Stern6ad07122006-06-01 13:59:16 -04001932 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
Alan Stern3b23dd62008-12-05 14:10:34 -05001934 create_intf_ep_devs(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 }
1936
Alan Stern94fcda12006-11-20 11:38:46 -05001937 usb_autosuspend_device(dev);
Alan Stern86d30742005-07-29 12:17:16 -07001938 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
Valentina Maneab7945b72014-01-23 23:12:29 +02001940EXPORT_SYMBOL_GPL(usb_set_configuration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Alan Sterndf718962008-12-19 10:27:56 -05001942static LIST_HEAD(set_config_list);
1943static DEFINE_SPINLOCK(set_config_lock);
1944
Alan Stern088dc272006-08-21 12:08:19 -04001945struct set_config_request {
1946 struct usb_device *udev;
1947 int config;
1948 struct work_struct work;
Alan Sterndf718962008-12-19 10:27:56 -05001949 struct list_head node;
Alan Stern088dc272006-08-21 12:08:19 -04001950};
1951
1952/* Worker routine for usb_driver_set_configuration() */
David Howellsc4028952006-11-22 14:57:56 +00001953static void driver_set_config_work(struct work_struct *work)
Alan Stern088dc272006-08-21 12:08:19 -04001954{
David Howellsc4028952006-11-22 14:57:56 +00001955 struct set_config_request *req =
1956 container_of(work, struct set_config_request, work);
Alan Sterndf718962008-12-19 10:27:56 -05001957 struct usb_device *udev = req->udev;
Alan Stern088dc272006-08-21 12:08:19 -04001958
Alan Sterndf718962008-12-19 10:27:56 -05001959 usb_lock_device(udev);
1960 spin_lock(&set_config_lock);
1961 list_del(&req->node);
1962 spin_unlock(&set_config_lock);
1963
1964 if (req->config >= -1) /* Is req still valid? */
1965 usb_set_configuration(udev, req->config);
1966 usb_unlock_device(udev);
1967 usb_put_dev(udev);
Alan Stern088dc272006-08-21 12:08:19 -04001968 kfree(req);
1969}
1970
Alan Sterndf718962008-12-19 10:27:56 -05001971/* Cancel pending Set-Config requests for a device whose configuration
1972 * was just changed
1973 */
1974static void cancel_async_set_config(struct usb_device *udev)
1975{
1976 struct set_config_request *req;
1977
1978 spin_lock(&set_config_lock);
1979 list_for_each_entry(req, &set_config_list, node) {
1980 if (req->udev == udev)
1981 req->config = -999; /* Mark as cancelled */
1982 }
1983 spin_unlock(&set_config_lock);
1984}
1985
Alan Stern088dc272006-08-21 12:08:19 -04001986/**
1987 * usb_driver_set_configuration - Provide a way for drivers to change device configurations
1988 * @udev: the device whose configuration is being updated
1989 * @config: the configuration being chosen.
1990 * Context: In process context, must be able to sleep
1991 *
1992 * Device interface drivers are not allowed to change device configurations.
1993 * This is because changing configurations will destroy the interface the
1994 * driver is bound to and create new ones; it would be like a floppy-disk
1995 * driver telling the computer to replace the floppy-disk drive with a
1996 * tape drive!
1997 *
1998 * Still, in certain specialized circumstances the need may arise. This
1999 * routine gets around the normal restrictions by using a work thread to
2000 * submit the change-config request.
2001 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02002002 * Return: 0 if the request was successfully queued, error code otherwise.
Alan Stern088dc272006-08-21 12:08:19 -04002003 * The caller has no way to know whether the queued request will eventually
2004 * succeed.
2005 */
2006int usb_driver_set_configuration(struct usb_device *udev, int config)
2007{
2008 struct set_config_request *req;
2009
2010 req = kmalloc(sizeof(*req), GFP_KERNEL);
2011 if (!req)
2012 return -ENOMEM;
2013 req->udev = udev;
2014 req->config = config;
David Howellsc4028952006-11-22 14:57:56 +00002015 INIT_WORK(&req->work, driver_set_config_work);
Alan Stern088dc272006-08-21 12:08:19 -04002016
Alan Sterndf718962008-12-19 10:27:56 -05002017 spin_lock(&set_config_lock);
2018 list_add(&req->node, &set_config_list);
2019 spin_unlock(&set_config_lock);
2020
Alan Stern088dc272006-08-21 12:08:19 -04002021 usb_get_dev(udev);
Alan Stern1737bf2c2006-12-15 16:04:52 -05002022 schedule_work(&req->work);
Alan Stern088dc272006-08-21 12:08:19 -04002023 return 0;
2024}
2025EXPORT_SYMBOL_GPL(usb_driver_set_configuration);