blob: 7943901c641c7653a709dc9b64f2189d807f466f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * message.c - synchronous message handling
3 */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/pci.h> /* for scatterlist macros */
6#include <linux/usb.h>
7#include <linux/module.h>
8#include <linux/slab.h>
9#include <linux/init.h>
10#include <linux/mm.h>
11#include <linux/timer.h>
12#include <linux/ctype.h>
13#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/byteorder.h>
17
18#include "hcd.h" /* for usbcore internals */
19#include "usb.h"
20
Alan Stern67f5dde2007-07-24 18:23:23 -040021struct api_context {
22 struct completion done;
23 int status;
24};
25
David Howells7d12e782006-10-05 14:55:46 +010026static void usb_api_blocking_completion(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Alan Stern67f5dde2007-07-24 18:23:23 -040028 struct api_context *ctx = urb->context;
29
30 ctx->status = urb->status;
31 complete(&ctx->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032}
33
34
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020035/*
36 * Starts urb and waits for completion or timeout. Note that this call
37 * is NOT interruptible. Many device driver i/o requests should be
38 * interruptible and therefore these drivers should implement their
39 * own interruptible routines.
40 */
41static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -080042{
Alan Stern67f5dde2007-07-24 18:23:23 -040043 struct api_context ctx;
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020044 unsigned long expire;
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -070045 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Alan Stern67f5dde2007-07-24 18:23:23 -040047 init_completion(&ctx.done);
48 urb->context = &ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 urb->actual_length = 0;
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -070050 retval = usb_submit_urb(urb, GFP_NOIO);
51 if (unlikely(retval))
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020052 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020054 expire = timeout ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT;
Alan Stern67f5dde2007-07-24 18:23:23 -040055 if (!wait_for_completion_timeout(&ctx.done, expire)) {
56 usb_kill_urb(urb);
57 retval = (ctx.status == -ENOENT ? -ETIMEDOUT : ctx.status);
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020058
59 dev_dbg(&urb->dev->dev,
60 "%s timed out on ep%d%s len=%d/%d\n",
61 current->comm,
Alan Stern5e60a162007-07-30 17:07:21 -040062 usb_endpoint_num(&urb->ep->desc),
63 usb_urb_dir_in(urb) ? "in" : "out",
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020064 urb->actual_length,
65 urb->transfer_buffer_length);
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020066 } else
Alan Stern67f5dde2007-07-24 18:23:23 -040067 retval = ctx.status;
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020068out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (actual_length)
70 *actual_length = urb->actual_length;
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 usb_free_urb(urb);
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -070073 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76/*-------------------------------------------------------------------*/
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -080077/* returns status (negative) or length (positive) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int usb_internal_control_msg(struct usb_device *usb_dev,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -080079 unsigned int pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 struct usb_ctrlrequest *cmd,
81 void *data, int len, int timeout)
82{
83 struct urb *urb;
84 int retv;
85 int length;
86
87 urb = usb_alloc_urb(0, GFP_NOIO);
88 if (!urb)
89 return -ENOMEM;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -080090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char *)cmd, data,
92 len, usb_api_blocking_completion, NULL);
93
94 retv = usb_start_wait_urb(urb, timeout, &length);
95 if (retv < 0)
96 return retv;
97 else
98 return length;
99}
100
101/**
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800102 * usb_control_msg - Builds a control urb, sends it off and waits for completion
103 * @dev: pointer to the usb device to send the message to
104 * @pipe: endpoint "pipe" to send the message to
105 * @request: USB message request value
106 * @requesttype: USB message request type value
107 * @value: USB message value
108 * @index: USB message index value
109 * @data: pointer to the data to send
110 * @size: length in bytes of the data to send
111 * @timeout: time in msecs to wait for the message to complete before timing
112 * out (if 0 the wait is forever)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800114 * Context: !in_interrupt ()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800116 * This function sends a simple control message to a specified endpoint and
117 * waits for the message to complete, or timeout.
118 *
119 * If successful, it returns the number of bytes transferred, otherwise a
120 * negative error number.
121 *
122 * Don't use this function from within an interrupt context, like a bottom half
123 * handler. If you need an asynchronous message, or need to send a message
124 * from within interrupt context, use usb_submit_urb().
125 * If a thread in your driver uses this call, make sure your disconnect()
126 * method can wait for it to complete. Since you don't have a handle on the
127 * URB used, you can't cancel the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800129int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
130 __u8 requesttype, __u16 value, __u16 index, void *data,
131 __u16 size, int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800133 struct usb_ctrlrequest *dr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int ret;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800135
136 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (!dr)
138 return -ENOMEM;
139
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800140 dr->bRequestType = requesttype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 dr->bRequest = request;
Harvey Harrisonda2bbdc2008-10-29 14:25:51 -0700142 dr->wValue = cpu_to_le16(value);
143 dr->wIndex = cpu_to_le16(index);
144 dr->wLength = cpu_to_le16(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800146 /* dbg("usb_control_msg"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 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 *
172 * If successful, it returns 0, otherwise a negative error number. The number
173 * of actual bytes transferred will be stored in the actual_length paramater.
174 *
175 * Don't use this function from within an interrupt context, like a bottom half
176 * handler. If you need an asynchronous message, or need to send a message
177 * from within interrupt context, use usb_submit_urb() If a thread in your
178 * driver uses this call, make sure your disconnect() method can wait for it to
179 * complete. Since you don't have a handle on the URB used, you can't cancel
180 * the request.
181 */
182int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe,
183 void *data, int len, int *actual_length, int timeout)
184{
185 return usb_bulk_msg(usb_dev, pipe, data, len, actual_length, timeout);
186}
187EXPORT_SYMBOL_GPL(usb_interrupt_msg);
188
189/**
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800190 * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion
191 * @usb_dev: pointer to the usb device to send the message to
192 * @pipe: endpoint "pipe" to send the message to
193 * @data: pointer to the data to send
194 * @len: length in bytes of the data to send
195 * @actual_length: pointer to a location to put the actual length transferred
196 * in bytes
197 * @timeout: time in msecs to wait for the message to complete before
198 * timing out (if 0 the wait is forever)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800200 * Context: !in_interrupt ()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800202 * This function sends a simple bulk message to a specified endpoint
203 * and waits for the message to complete, or timeout.
Alan Sternd09d36a2005-09-26 16:22:45 -0400204 *
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800205 * If successful, it returns 0, otherwise a negative error number. The number
206 * of actual bytes transferred will be stored in the actual_length paramater.
207 *
208 * Don't use this function from within an interrupt context, like a bottom half
209 * handler. If you need an asynchronous message, or need to send a message
210 * from within interrupt context, use usb_submit_urb() If a thread in your
211 * driver uses this call, make sure your disconnect() method can wait for it to
212 * complete. Since you don't have a handle on the URB used, you can't cancel
213 * the request.
214 *
215 * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT ioctl,
216 * users are forced to abuse this routine by using it to submit URBs for
217 * interrupt endpoints. We will take the liberty of creating an interrupt URB
218 * (with the default interval) if the target is an interrupt endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800220int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
221 void *data, int len, int *actual_length, int timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 struct urb *urb;
Alan Sternd09d36a2005-09-26 16:22:45 -0400224 struct usb_host_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Alan Sternd09d36a2005-09-26 16:22:45 -0400226 ep = (usb_pipein(pipe) ? usb_dev->ep_in : usb_dev->ep_out)
227 [usb_pipeendpoint(pipe)];
228 if (!ep || len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return -EINVAL;
230
Alan Sternd09d36a2005-09-26 16:22:45 -0400231 urb = usb_alloc_urb(0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (!urb)
233 return -ENOMEM;
234
Alan Sternd09d36a2005-09-26 16:22:45 -0400235 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
236 USB_ENDPOINT_XFER_INT) {
237 pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
238 usb_fill_int_urb(urb, usb_dev, pipe, data, len,
Alan Stern8d062b92007-04-23 17:30:32 -0400239 usb_api_blocking_completion, NULL,
240 ep->desc.bInterval);
Alan Sternd09d36a2005-09-26 16:22:45 -0400241 } else
242 usb_fill_bulk_urb(urb, usb_dev, pipe, data, len,
243 usb_api_blocking_completion, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 return usb_start_wait_urb(urb, timeout, actual_length);
246}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600247EXPORT_SYMBOL_GPL(usb_bulk_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249/*-------------------------------------------------------------------*/
250
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800251static void sg_clean(struct usb_sg_request *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 if (io->urbs) {
254 while (io->entries--)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800255 usb_free_urb(io->urbs [io->entries]);
256 kfree(io->urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 io->urbs = NULL;
258 }
259 if (io->dev->dev.dma_mask != NULL)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800260 usb_buffer_unmap_sg(io->dev, usb_pipein(io->pipe),
261 io->sg, io->nents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 io->dev = NULL;
263}
264
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800265static void sg_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800267 struct usb_sg_request *io = urb->context;
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700268 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800270 spin_lock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 /* In 2.5 we require hcds' endpoint queues not to progress after fault
273 * reports, until the completion callback (this!) returns. That lets
274 * device driver code (like this routine) unlink queued urbs first,
275 * if it needs to, since the HC won't work on them at all. So it's
276 * not possible for page N+1 to overwrite page N, and so on.
277 *
278 * That's only for "hard" faults; "soft" faults (unlinks) sometimes
279 * complete before the HCD can get requests away from hardware,
280 * though never during cleanup after a hard fault.
281 */
282 if (io->status
283 && (io->status != -ECONNRESET
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700284 || status != -ECONNRESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 && urb->actual_length) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800286 dev_err(io->dev->bus->controller,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 "dev %s ep%d%s scatterlist error %d/%d\n",
288 io->dev->devpath,
Alan Stern5e60a162007-07-30 17:07:21 -0400289 usb_endpoint_num(&urb->ep->desc),
290 usb_urb_dir_in(urb) ? "in" : "out",
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700291 status, io->status);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800292 /* BUG (); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700295 if (io->status == 0 && status && status != -ECONNRESET) {
296 int i, found, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700298 io->status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /* the previous urbs, and this one, completed already.
301 * unlink pending urbs so they won't rx/tx bad data.
302 * careful: unlink can sometimes be synchronous...
303 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800304 spin_unlock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 for (i = 0, found = 0; i < io->entries; i++) {
306 if (!io->urbs [i] || !io->urbs [i]->dev)
307 continue;
308 if (found) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800309 retval = usb_unlink_urb(io->urbs [i]);
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700310 if (retval != -EINPROGRESS &&
311 retval != -ENODEV &&
312 retval != -EBUSY)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800313 dev_err(&io->dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 "%s, unlink --> %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800315 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 } else if (urb == io->urbs [i])
317 found = 1;
318 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800319 spin_lock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321 urb->dev = NULL;
322
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 *
347 * Returns zero for success, else a negative errno value. This initializes a
348 * scatter/gather request, allocating resources such as I/O mappings and urb
349 * memory (except maybe memory used by USB controller drivers).
350 *
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.
357 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800358int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
359 unsigned pipe, unsigned period, struct scatterlist *sg,
360 int nents, size_t length, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800362 int i;
363 int urb_flags;
364 int dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 if (!io || !dev || !sg
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800367 || usb_pipecontrol(pipe)
368 || usb_pipeisoc(pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 || nents <= 0)
370 return -EINVAL;
371
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800372 spin_lock_init(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 io->dev = dev;
374 io->pipe = pipe;
375 io->sg = sg;
376 io->nents = nents;
377
378 /* not all host controllers use DMA (like the mainstream pci ones);
379 * they can use PIO (sl811) or be software over another transport.
380 */
381 dma = (dev->dev.dma_mask != NULL);
382 if (dma)
Alan Stern5e60a162007-07-30 17:07:21 -0400383 io->entries = usb_buffer_map_sg(dev, usb_pipein(pipe),
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800384 sg, nents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 else
386 io->entries = nents;
387
388 /* initialize all the urbs we'll use */
389 if (io->entries <= 0)
390 return io->entries;
391
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800392 io->urbs = kmalloc(io->entries * sizeof *io->urbs, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (!io->urbs)
394 goto nomem;
395
Yoshihiro Shimodae2722522008-04-21 13:48:22 +0900396 urb_flags = URB_NO_INTERRUPT;
397 if (dma)
398 urb_flags |= URB_NO_TRANSFER_DMA_MAP;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800399 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 urb_flags |= URB_SHORT_NOT_OK;
401
Alan Stern7c3e28b2008-06-16 12:11:39 -0400402 for_each_sg(sg, sg, io->entries, i) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800403 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800405 io->urbs[i] = usb_alloc_urb(0, mem_flags);
406 if (!io->urbs[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 io->entries = i;
408 goto nomem;
409 }
410
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800411 io->urbs[i]->dev = NULL;
412 io->urbs[i]->pipe = pipe;
413 io->urbs[i]->interval = period;
414 io->urbs[i]->transfer_flags = urb_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800416 io->urbs[i]->complete = sg_complete;
417 io->urbs[i]->context = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Tony Lindgren35d07fd2007-03-31 18:15:43 -0700419 /*
420 * Some systems need to revert to PIO when DMA is temporarily
421 * unavailable. For their sakes, both transfer_buffer and
422 * transfer_dma are set when possible. However this can only
David Brownella12b8db2007-07-22 15:13:13 -0700423 * work on systems without:
424 *
425 * - HIGHMEM, since DMA buffers located in high memory are
426 * not directly addressable by the CPU for PIO;
427 *
428 * - IOMMU, since dma_map_sg() is allowed to use an IOMMU to
429 * make virtually discontiguous buffers be "dma-contiguous"
430 * so that PIO and DMA need diferent numbers of URBs.
431 *
432 * So when HIGHMEM or IOMMU are in use, transfer_buffer is NULL
Tony Lindgren35d07fd2007-03-31 18:15:43 -0700433 * to prevent stale pointers and to help spot bugs.
434 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (dma) {
Alan Stern7c3e28b2008-06-16 12:11:39 -0400436 io->urbs[i]->transfer_dma = sg_dma_address(sg);
437 len = sg_dma_len(sg);
Joerg Roedel966396d2007-10-24 12:49:48 +0200438#if defined(CONFIG_HIGHMEM) || defined(CONFIG_GART_IOMMU)
Tony Lindgren35d07fd2007-03-31 18:15:43 -0700439 io->urbs[i]->transfer_buffer = NULL;
440#else
Alan Stern7c3e28b2008-06-16 12:11:39 -0400441 io->urbs[i]->transfer_buffer = sg_virt(sg);
Tony Lindgren35d07fd2007-03-31 18:15:43 -0700442#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 } else {
444 /* hc may use _only_ transfer_buffer */
Alan Stern7c3e28b2008-06-16 12:11:39 -0400445 io->urbs[i]->transfer_buffer = sg_virt(sg);
446 len = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448
449 if (length) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800450 len = min_t(unsigned, len, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 length -= len;
452 if (length == 0)
453 io->entries = i + 1;
454 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800455 io->urbs[i]->transfer_buffer_length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800457 io->urbs[--i]->transfer_flags &= ~URB_NO_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 /* transaction state */
Alan Stern580da342008-08-05 13:05:17 -0400460 io->count = io->entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 io->status = 0;
462 io->bytes = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800463 init_completion(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return 0;
465
466nomem:
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800467 sg_clean(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return -ENOMEM;
469}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600470EXPORT_SYMBOL_GPL(usb_sg_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472/**
473 * usb_sg_wait - synchronously execute scatter/gather request
474 * @io: request block handle, as initialized with usb_sg_init().
475 * some fields become accessible when this call returns.
476 * Context: !in_interrupt ()
477 *
478 * This function blocks until the specified I/O operation completes. It
479 * leverages the grouping of the related I/O requests to get good transfer
480 * rates, by queueing the requests. At higher speeds, such queuing can
481 * significantly improve USB throughput.
482 *
483 * There are three kinds of completion for this function.
484 * (1) success, where io->status is zero. The number of io->bytes
485 * transferred is as requested.
486 * (2) error, where io->status is a negative errno value. The number
487 * of io->bytes transferred before the error is usually less
488 * than requested, and can be nonzero.
Steven Cole093cf722005-05-03 19:07:24 -0600489 * (3) cancellation, a type of error with status -ECONNRESET that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 * is initiated by usb_sg_cancel().
491 *
492 * When this function returns, all memory allocated through usb_sg_init() or
493 * this call will have been freed. The request block parameter may still be
494 * passed to usb_sg_cancel(), or it may be freed. It could also be
495 * reinitialized and then reused.
496 *
497 * Data Transfer Rates:
498 *
499 * Bulk transfers are valid for full or high speed endpoints.
500 * The best full speed data rate is 19 packets of 64 bytes each
501 * per frame, or 1216 bytes per millisecond.
502 * The best high speed data rate is 13 packets of 512 bytes each
503 * per microframe, or 52 KBytes per millisecond.
504 *
505 * The reason to use interrupt transfers through this API would most likely
506 * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
507 * could be transferred. That capability is less useful for low or full
508 * speed interrupt endpoints, which allow at most one packet per millisecond,
509 * of at most 8 or 64 bytes (respectively).
510 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800511void usb_sg_wait(struct usb_sg_request *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800513 int i;
514 int entries = io->entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /* queue the urbs. */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800517 spin_lock_irq(&io->lock);
Alan Stern8ccef0d2007-06-21 16:26:46 -0400518 i = 0;
519 while (i < entries && !io->status) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800520 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800522 io->urbs[i]->dev = io->dev;
523 retval = usb_submit_urb(io->urbs [i], GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 /* after we submit, let completions or cancelations fire;
526 * we handshake using io->status.
527 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800528 spin_unlock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 switch (retval) {
530 /* maybe we retrying will recover */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800531 case -ENXIO: /* hc didn't queue this one */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 case -EAGAIN:
533 case -ENOMEM:
534 io->urbs[i]->dev = NULL;
535 retval = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800536 yield();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 break;
538
539 /* no error? continue immediately.
540 *
541 * NOTE: to work better with UHCI (4K I/O buffer may
542 * need 3K of TDs) it may be good to limit how many
543 * URBs are queued at once; N milliseconds?
544 */
545 case 0:
Alan Stern8ccef0d2007-06-21 16:26:46 -0400546 ++i;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800547 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 break;
549
550 /* fail any uncompleted urbs */
551 default:
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800552 io->urbs[i]->dev = NULL;
553 io->urbs[i]->status = retval;
554 dev_dbg(&io->dev->dev, "%s, submit --> %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800555 __func__, retval);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800556 usb_sg_cancel(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800558 spin_lock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (retval && (io->status == 0 || io->status == -ECONNRESET))
560 io->status = retval;
561 }
562 io->count -= entries - i;
563 if (io->count == 0)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800564 complete(&io->complete);
565 spin_unlock_irq(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /* OK, yes, this could be packaged as non-blocking.
568 * So could the submit loop above ... but it's easier to
569 * solve neither problem than to solve both!
570 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800571 wait_for_completion(&io->complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800573 sg_clean(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600575EXPORT_SYMBOL_GPL(usb_sg_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577/**
578 * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
579 * @io: request block, initialized with usb_sg_init()
580 *
581 * This stops a request after it has been started by usb_sg_wait().
582 * It can also prevents one initialized by usb_sg_init() from starting,
583 * so that call just frees resources allocated to the request.
584 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800585void usb_sg_cancel(struct usb_sg_request *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800587 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800589 spin_lock_irqsave(&io->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 /* shut everything down, if it didn't already */
592 if (!io->status) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800593 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 io->status = -ECONNRESET;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800596 spin_unlock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 for (i = 0; i < io->entries; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800598 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 if (!io->urbs [i]->dev)
601 continue;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800602 retval = usb_unlink_urb(io->urbs [i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (retval != -EINPROGRESS && retval != -EBUSY)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800604 dev_warn(&io->dev->dev, "%s, unlink --> %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800605 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800607 spin_lock(&io->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800609 spin_unlock_irqrestore(&io->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600611EXPORT_SYMBOL_GPL(usb_sg_cancel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613/*-------------------------------------------------------------------*/
614
615/**
616 * usb_get_descriptor - issues a generic GET_DESCRIPTOR request
617 * @dev: the device whose descriptor is being retrieved
618 * @type: the descriptor type (USB_DT_*)
619 * @index: the number of the descriptor
620 * @buf: where to put the descriptor
621 * @size: how big is "buf"?
622 * Context: !in_interrupt ()
623 *
624 * Gets a USB descriptor. Convenience functions exist to simplify
625 * getting some types of descriptors. Use
626 * usb_get_string() or usb_string() for USB_DT_STRING.
627 * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG)
628 * are part of the device structure.
629 * In addition to a number of USB-standard descriptors, some
630 * devices also use class-specific or vendor-specific descriptors.
631 *
632 * This call is synchronous, and may not be used in an interrupt context.
633 *
634 * Returns the number of bytes received on success, or else the status code
635 * returned by the underlying usb_control_msg() call.
636 */
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800637int usb_get_descriptor(struct usb_device *dev, unsigned char type,
638 unsigned char index, void *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 int i;
641 int result;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800642
643 memset(buf, 0, size); /* Make sure we parse really received data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 for (i = 0; i < 3; ++i) {
Alan Sternc39772d2007-08-20 10:45:28 -0400646 /* retry on length 0 or error; some devices are flakey */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
648 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
649 (type << 8) + index, 0, buf, size,
650 USB_CTRL_GET_TIMEOUT);
Alan Sternc39772d2007-08-20 10:45:28 -0400651 if (result <= 0 && result != -ETIMEDOUT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 continue;
653 if (result > 1 && ((u8 *)buf)[1] != type) {
654 result = -EPROTO;
655 continue;
656 }
657 break;
658 }
659 return result;
660}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600661EXPORT_SYMBOL_GPL(usb_get_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663/**
664 * usb_get_string - gets a string descriptor
665 * @dev: the device whose string descriptor is being retrieved
666 * @langid: code for language chosen (from string descriptor zero)
667 * @index: the number of the descriptor
668 * @buf: where to put the string
669 * @size: how big is "buf"?
670 * Context: !in_interrupt ()
671 *
672 * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character,
673 * in little-endian byte order).
674 * The usb_string() function will often be a convenient way to turn
675 * these strings into kernel-printable form.
676 *
677 * Strings may be referenced in device, configuration, interface, or other
678 * descriptors, and could also be used in vendor-specific ways.
679 *
680 * This call is synchronous, and may not be used in an interrupt context.
681 *
682 * Returns the number of bytes received on success, or else the status code
683 * returned by the underlying usb_control_msg() call.
684 */
Adrian Bunke266a122005-11-08 21:05:43 +0100685static int usb_get_string(struct usb_device *dev, unsigned short langid,
686 unsigned char index, void *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 int i;
689 int result;
690
691 for (i = 0; i < 3; ++i) {
692 /* retry on length 0 or stall; some devices are flakey */
693 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
694 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
695 (USB_DT_STRING << 8) + index, langid, buf, size,
696 USB_CTRL_GET_TIMEOUT);
697 if (!(result == 0 || result == -EPIPE))
698 break;
699 }
700 return result;
701}
702
703static void usb_try_string_workarounds(unsigned char *buf, int *length)
704{
705 int newlength, oldlength = *length;
706
707 for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
708 if (!isprint(buf[newlength]) || buf[newlength + 1])
709 break;
710
711 if (newlength > 2) {
712 buf[0] = newlength;
713 *length = newlength;
714 }
715}
716
717static int usb_string_sub(struct usb_device *dev, unsigned int langid,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800718 unsigned int index, unsigned char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 int rc;
721
722 /* Try to read the string descriptor by asking for the maximum
723 * possible number of bytes */
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100724 if (dev->quirks & USB_QUIRK_STRING_FETCH_255)
725 rc = -EIO;
726 else
727 rc = usb_get_string(dev, langid, index, buf, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 /* If that failed try to read the descriptor length, then
730 * ask for just that many bytes */
731 if (rc < 2) {
732 rc = usb_get_string(dev, langid, index, buf, 2);
733 if (rc == 2)
734 rc = usb_get_string(dev, langid, index, buf, buf[0]);
735 }
736
737 if (rc >= 2) {
738 if (!buf[0] && !buf[1])
739 usb_try_string_workarounds(buf, &rc);
740
741 /* There might be extra junk at the end of the descriptor */
742 if (buf[0] < rc)
743 rc = buf[0];
744
745 rc = rc - (rc & 1); /* force a multiple of two */
746 }
747
748 if (rc < 2)
749 rc = (rc < 0 ? rc : -EINVAL);
750
751 return rc;
752}
753
754/**
755 * usb_string - returns ISO 8859-1 version of a string descriptor
756 * @dev: the device whose string descriptor is being retrieved
757 * @index: the number of the descriptor
758 * @buf: where to put the string
759 * @size: how big is "buf"?
760 * Context: !in_interrupt ()
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800761 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 * This converts the UTF-16LE encoded strings returned by devices, from
763 * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones
764 * that are more usable in most kernel contexts. Note that all characters
765 * in the chosen descriptor that can't be encoded using ISO-8859-1
766 * are converted to the question mark ("?") character, and this function
767 * chooses strings in the first language supported by the device.
768 *
769 * The ASCII (or, redundantly, "US-ASCII") character set is the seven-bit
770 * subset of ISO 8859-1. ISO-8859-1 is the eight-bit subset of Unicode,
771 * and is appropriate for use many uses of English and several other
772 * Western European languages. (But it doesn't include the "Euro" symbol.)
773 *
774 * This call is synchronous, and may not be used in an interrupt context.
775 *
776 * Returns length of the string (>= 0) or usb_control_msg status (< 0).
777 */
778int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
779{
780 unsigned char *tbuf;
781 int err;
782 unsigned int u, idx;
783
784 if (dev->state == USB_STATE_SUSPENDED)
785 return -EHOSTUNREACH;
786 if (size <= 0 || !buf || !index)
787 return -EINVAL;
788 buf[0] = 0;
Alan Sterneb764c42008-03-03 15:16:04 -0500789 tbuf = kmalloc(256, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (!tbuf)
791 return -ENOMEM;
792
793 /* get langid for strings if it's not yet known */
794 if (!dev->have_langid) {
795 err = usb_string_sub(dev, 0, 0, tbuf);
796 if (err < 0) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800797 dev_err(&dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 "string descriptor 0 read error: %d\n",
799 err);
800 goto errout;
801 } else if (err < 4) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800802 dev_err(&dev->dev, "string descriptor 0 too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 err = -EINVAL;
804 goto errout;
805 } else {
Alan Sternce361582006-11-20 11:12:22 -0500806 dev->have_langid = 1;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800807 dev->string_langid = tbuf[2] | (tbuf[3] << 8);
808 /* always use the first langid listed */
809 dev_dbg(&dev->dev, "default language 0x%04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 dev->string_langid);
811 }
812 }
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 err = usb_string_sub(dev, dev->string_langid, index, tbuf);
815 if (err < 0)
816 goto errout;
817
818 size--; /* leave room for trailing NULL char in output buffer */
819 for (idx = 0, u = 2; u < err; u += 2) {
820 if (idx >= size)
821 break;
822 if (tbuf[u+1]) /* high byte */
823 buf[idx++] = '?'; /* non ISO-8859-1 character */
824 else
825 buf[idx++] = tbuf[u];
826 }
827 buf[idx] = 0;
828 err = idx;
829
830 if (tbuf[1] != USB_DT_STRING)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800831 dev_dbg(&dev->dev,
832 "wrong descriptor type %02x for string %d (\"%s\")\n",
833 tbuf[1], index, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 errout:
836 kfree(tbuf);
837 return err;
838}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600839EXPORT_SYMBOL_GPL(usb_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Alan Stern4f62efe2005-10-24 16:24:14 -0400841/**
842 * usb_cache_string - read a string descriptor and cache it for later use
843 * @udev: the device whose string descriptor is being read
844 * @index: the descriptor index
845 *
846 * Returns a pointer to a kmalloc'ed buffer containing the descriptor string,
847 * or NULL if the index is 0 or the string could not be read.
848 */
849char *usb_cache_string(struct usb_device *udev, int index)
850{
851 char *buf;
852 char *smallbuf = NULL;
853 int len;
854
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800855 if (index <= 0)
856 return NULL;
857
858 buf = kmalloc(256, GFP_KERNEL);
859 if (buf) {
860 len = usb_string(udev, index, buf, 256);
861 if (len > 0) {
862 smallbuf = kmalloc(++len, GFP_KERNEL);
863 if (!smallbuf)
Alan Stern4f62efe2005-10-24 16:24:14 -0400864 return buf;
865 memcpy(smallbuf, buf, len);
866 }
867 kfree(buf);
868 }
869 return smallbuf;
870}
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872/*
873 * usb_get_device_descriptor - (re)reads the device descriptor (usbcore)
874 * @dev: the device whose device descriptor is being updated
875 * @size: how much of the descriptor to read
876 * Context: !in_interrupt ()
877 *
878 * Updates the copy of the device descriptor stored in the device structure,
Laurent Pinchart6ab16a92006-11-07 10:16:25 +0100879 * which dedicates space for this purpose.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 *
881 * Not exported, only for use by the core. If drivers really want to read
882 * the device descriptor directly, they can call usb_get_descriptor() with
883 * type = USB_DT_DEVICE and index = 0.
884 *
885 * This call is synchronous, and may not be used in an interrupt context.
886 *
887 * Returns the number of bytes received on success, or else the status code
888 * returned by the underlying usb_control_msg() call.
889 */
890int usb_get_device_descriptor(struct usb_device *dev, unsigned int size)
891{
892 struct usb_device_descriptor *desc;
893 int ret;
894
895 if (size > sizeof(*desc))
896 return -EINVAL;
897 desc = kmalloc(sizeof(*desc), GFP_NOIO);
898 if (!desc)
899 return -ENOMEM;
900
901 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800902 if (ret >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 memcpy(&dev->descriptor, desc, size);
904 kfree(desc);
905 return ret;
906}
907
908/**
909 * usb_get_status - issues a GET_STATUS call
910 * @dev: the device whose status is being checked
911 * @type: USB_RECIP_*; for device, interface, or endpoint
912 * @target: zero (for device), else interface or endpoint number
913 * @data: pointer to two bytes of bitmap data
914 * Context: !in_interrupt ()
915 *
916 * Returns device, interface, or endpoint status. Normally only of
917 * interest to see if the device is self powered, or has enabled the
918 * remote wakeup facility; or whether a bulk or interrupt endpoint
919 * is halted ("stalled").
920 *
921 * Bits in these status bitmaps are set using the SET_FEATURE request,
922 * and cleared using the CLEAR_FEATURE request. The usb_clear_halt()
923 * function should be used to clear halt ("stall") status.
924 *
925 * This call is synchronous, and may not be used in an interrupt context.
926 *
927 * Returns the number of bytes received on success, or else the status code
928 * returned by the underlying usb_control_msg() call.
929 */
930int usb_get_status(struct usb_device *dev, int type, int target, void *data)
931{
932 int ret;
933 u16 *status = kmalloc(sizeof(*status), GFP_KERNEL);
934
935 if (!status)
936 return -ENOMEM;
937
938 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
939 USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status,
940 sizeof(*status), USB_CTRL_GET_TIMEOUT);
941
942 *(u16 *)data = *status;
943 kfree(status);
944 return ret;
945}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600946EXPORT_SYMBOL_GPL(usb_get_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948/**
949 * usb_clear_halt - tells device to clear endpoint halt/stall condition
950 * @dev: device whose endpoint is halted
951 * @pipe: endpoint "pipe" being cleared
952 * Context: !in_interrupt ()
953 *
954 * This is used to clear halt conditions for bulk and interrupt endpoints,
955 * as reported by URB completion status. Endpoints that are halted are
956 * sometimes referred to as being "stalled". Such endpoints are unable
957 * to transmit or receive data until the halt status is cleared. Any URBs
958 * queued for such an endpoint should normally be unlinked by the driver
959 * before clearing the halt condition, as described in sections 5.7.5
960 * and 5.8.5 of the USB 2.0 spec.
961 *
962 * Note that control and isochronous endpoints don't halt, although control
963 * endpoints report "protocol stall" (for unsupported requests) using the
964 * same status code used to report a true stall.
965 *
966 * This call is synchronous, and may not be used in an interrupt context.
967 *
968 * Returns zero on success, or else the status code returned by the
969 * underlying usb_control_msg() call.
970 */
971int usb_clear_halt(struct usb_device *dev, int pipe)
972{
973 int result;
974 int endp = usb_pipeendpoint(pipe);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -0800975
976 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 endp |= USB_DIR_IN;
978
979 /* we don't care if it wasn't halted first. in fact some devices
980 * (like some ibmcam model 1 units) seem to expect hosts to make
981 * this request for iso endpoints, which can't halt!
982 */
983 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
984 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
985 USB_ENDPOINT_HALT, endp, NULL, 0,
986 USB_CTRL_SET_TIMEOUT);
987
988 /* don't un-halt or force to DATA0 except on success */
989 if (result < 0)
990 return result;
991
992 /* NOTE: seems like Microsoft and Apple don't bother verifying
993 * the clear "took", so some devices could lock up if you check...
994 * such as the Hagiwara FlashGate DUAL. So we won't bother.
995 *
996 * NOTE: make sure the logic here doesn't diverge much from
997 * the copy in usb-storage, for as long as we need two copies.
998 */
999
1000 /* toggle was reset by the clear */
1001 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
1002
1003 return 0;
1004}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001005EXPORT_SYMBOL_GPL(usb_clear_halt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Alan Stern3b23dd62008-12-05 14:10:34 -05001007static int create_intf_ep_devs(struct usb_interface *intf)
1008{
1009 struct usb_device *udev = interface_to_usbdev(intf);
1010 struct usb_host_interface *alt = intf->cur_altsetting;
1011 int i;
1012
1013 if (intf->ep_devs_created || intf->unregistering)
1014 return 0;
1015
1016 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1017 (void) usb_create_ep_devs(&intf->dev, &alt->endpoint[i], udev);
1018 intf->ep_devs_created = 1;
1019 return 0;
1020}
1021
1022static void remove_intf_ep_devs(struct usb_interface *intf)
1023{
1024 struct usb_host_interface *alt = intf->cur_altsetting;
1025 int i;
1026
1027 if (!intf->ep_devs_created)
1028 return;
1029
1030 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1031 usb_remove_ep_devs(&alt->endpoint[i]);
1032 intf->ep_devs_created = 0;
1033}
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035/**
1036 * usb_disable_endpoint -- Disable an endpoint by address
1037 * @dev: the device whose endpoint is being disabled
1038 * @epaddr: the endpoint's address. Endpoint number for output,
1039 * endpoint number + USB_DIR_IN for input
1040 *
1041 * Deallocates hcd/hardware state for this endpoint ... and nukes all
1042 * pending urbs.
1043 *
1044 * If the HCD hasn't registered a disable() function, this sets the
1045 * endpoint's maxpacket size to 0 to prevent further submissions.
1046 */
1047void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr)
1048{
1049 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1050 struct usb_host_endpoint *ep;
1051
1052 if (!dev)
1053 return;
1054
1055 if (usb_endpoint_out(epaddr)) {
1056 ep = dev->ep_out[epnum];
1057 dev->ep_out[epnum] = NULL;
1058 } else {
1059 ep = dev->ep_in[epnum];
1060 dev->ep_in[epnum] = NULL;
1061 }
Alan Sternbdd016b2007-07-30 17:05:22 -04001062 if (ep) {
1063 ep->enabled = 0;
Alan Stern95cf82f2007-09-10 11:33:05 -04001064 usb_hcd_flush_endpoint(dev, ep);
1065 usb_hcd_disable_endpoint(dev, ep);
Alan Sternbdd016b2007-07-30 17:05:22 -04001066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069/**
1070 * usb_disable_interface -- Disable all endpoints for an interface
1071 * @dev: the device whose interface is being disabled
1072 * @intf: pointer to the interface descriptor
1073 *
1074 * Disables all the endpoints for the interface's current altsetting.
1075 */
1076void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf)
1077{
1078 struct usb_host_interface *alt = intf->cur_altsetting;
1079 int i;
1080
1081 for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
1082 usb_disable_endpoint(dev,
1083 alt->endpoint[i].desc.bEndpointAddress);
1084 }
1085}
1086
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001087/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 * usb_disable_device - Disable all the endpoints for a USB device
1089 * @dev: the device whose endpoints are being disabled
1090 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
1091 *
1092 * Disables all the device's endpoints, potentially including endpoint 0.
1093 * Deallocates hcd/hardware state for the endpoints (nuking all or most
1094 * pending urbs) and usbcore state for the interfaces, so that usbcore
1095 * must usb_set_configuration() before any interfaces could be used.
1096 */
1097void usb_disable_device(struct usb_device *dev, int skip_ep0)
1098{
1099 int i;
1100
Harvey Harrison441b62c2008-03-03 16:08:34 -08001101 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __func__,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001102 skip_ep0 ? "non-ep0" : "all");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 for (i = skip_ep0; i < 16; ++i) {
1104 usb_disable_endpoint(dev, i);
1105 usb_disable_endpoint(dev, i + USB_DIR_IN);
1106 }
1107 dev->toggle[0] = dev->toggle[1] = 0;
1108
1109 /* getting rid of interfaces will disconnect
1110 * any drivers bound to them (a key side effect)
1111 */
1112 if (dev->actconfig) {
1113 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1114 struct usb_interface *interface;
1115
Alan Stern86d30742005-07-29 12:17:16 -07001116 /* remove this interface if it has been registered */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 interface = dev->actconfig->interface[i];
Daniel Ritzd305ef52005-09-22 00:47:24 -07001118 if (!device_is_registered(&interface->dev))
Alan Stern86d30742005-07-29 12:17:16 -07001119 continue;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001120 dev_dbg(&dev->dev, "unregistering interface %s\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001121 dev_name(&interface->dev));
Alan Stern352d0262008-10-29 15:16:58 -04001122 interface->unregistering = 1;
Alan Stern3b23dd62008-12-05 14:10:34 -05001123 remove_intf_ep_devs(interface);
Alan Stern1a211752008-07-30 11:31:50 -04001124 device_del(&interface->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126
1127 /* Now that the interfaces are unbound, nobody should
1128 * try to access them.
1129 */
1130 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001131 put_device(&dev->actconfig->interface[i]->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 dev->actconfig->interface[i] = NULL;
1133 }
1134 dev->actconfig = NULL;
1135 if (dev->state == USB_STATE_CONFIGURED)
1136 usb_set_device_state(dev, USB_STATE_ADDRESS);
1137 }
1138}
1139
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001140/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 * usb_enable_endpoint - Enable an endpoint for USB communications
1142 * @dev: the device whose interface is being enabled
1143 * @ep: the endpoint
1144 *
1145 * Resets the endpoint toggle, and sets dev->ep_{in,out} pointers.
1146 * For control endpoints, both the input and output sides are handled.
1147 */
Alan Sternbdd016b2007-07-30 17:05:22 -04001148void usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Alan Sternbdd016b2007-07-30 17:05:22 -04001150 int epnum = usb_endpoint_num(&ep->desc);
1151 int is_out = usb_endpoint_dir_out(&ep->desc);
1152 int is_control = usb_endpoint_xfer_control(&ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Alan Sternbdd016b2007-07-30 17:05:22 -04001154 if (is_out || is_control) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 usb_settoggle(dev, epnum, 1, 0);
1156 dev->ep_out[epnum] = ep;
1157 }
Alan Sternbdd016b2007-07-30 17:05:22 -04001158 if (!is_out || is_control) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 usb_settoggle(dev, epnum, 0, 0);
1160 dev->ep_in[epnum] = ep;
1161 }
Alan Sternbdd016b2007-07-30 17:05:22 -04001162 ep->enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001165/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 * usb_enable_interface - Enable all the endpoints for an interface
1167 * @dev: the device whose interface is being enabled
1168 * @intf: pointer to the interface descriptor
1169 *
1170 * Enables all the endpoints for the interface's current altsetting.
1171 */
1172static void usb_enable_interface(struct usb_device *dev,
1173 struct usb_interface *intf)
1174{
1175 struct usb_host_interface *alt = intf->cur_altsetting;
1176 int i;
1177
1178 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1179 usb_enable_endpoint(dev, &alt->endpoint[i]);
1180}
1181
1182/**
1183 * usb_set_interface - Makes a particular alternate setting be current
1184 * @dev: the device whose interface is being updated
1185 * @interface: the interface being updated
1186 * @alternate: the setting being chosen.
1187 * Context: !in_interrupt ()
1188 *
1189 * This is used to enable data transfers on interfaces that may not
1190 * be enabled by default. Not all devices support such configurability.
1191 * Only the driver bound to an interface may change its setting.
1192 *
1193 * Within any given configuration, each interface may have several
1194 * alternative settings. These are often used to control levels of
1195 * bandwidth consumption. For example, the default setting for a high
1196 * speed interrupt endpoint may not send more than 64 bytes per microframe,
1197 * while interrupt transfers of up to 3KBytes per microframe are legal.
1198 * Also, isochronous endpoints may never be part of an
1199 * interface's default setting. To access such bandwidth, alternate
1200 * interface settings must be made current.
1201 *
1202 * Note that in the Linux USB subsystem, bandwidth associated with
1203 * an endpoint in a given alternate setting is not reserved until an URB
1204 * is submitted that needs that bandwidth. Some other operating systems
1205 * allocate bandwidth early, when a configuration is chosen.
1206 *
1207 * This call is synchronous, and may not be used in an interrupt context.
1208 * Also, drivers must not change altsettings while urbs are scheduled for
1209 * endpoints in that interface; all such urbs must first be completed
1210 * (perhaps forced by unlinking).
1211 *
1212 * Returns zero on success, or else the status code returned by the
1213 * underlying usb_control_msg() call.
1214 */
1215int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1216{
1217 struct usb_interface *iface;
1218 struct usb_host_interface *alt;
1219 int ret;
1220 int manual = 0;
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001221 unsigned int epaddr;
1222 unsigned int pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 if (dev->state == USB_STATE_SUSPENDED)
1225 return -EHOSTUNREACH;
1226
1227 iface = usb_ifnum_to_if(dev, interface);
1228 if (!iface) {
1229 dev_dbg(&dev->dev, "selecting invalid interface %d\n",
1230 interface);
1231 return -EINVAL;
1232 }
1233
1234 alt = usb_altnum_to_altsetting(iface, alternate);
1235 if (!alt) {
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -07001236 dev_warn(&dev->dev, "selecting invalid altsetting %d",
1237 alternate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return -EINVAL;
1239 }
1240
Alan Stern392e1d92008-03-11 10:20:12 -04001241 if (dev->quirks & USB_QUIRK_NO_SET_INTF)
1242 ret = -EPIPE;
1243 else
1244 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1246 alternate, interface, NULL, 0, 5000);
1247
1248 /* 9.4.10 says devices don't need this and are free to STALL the
1249 * request if the interface only has one alternate setting.
1250 */
1251 if (ret == -EPIPE && iface->num_altsetting == 1) {
1252 dev_dbg(&dev->dev,
1253 "manual set_interface for iface %d, alt %d\n",
1254 interface, alternate);
1255 manual = 1;
1256 } else if (ret < 0)
1257 return ret;
1258
1259 /* FIXME drivers shouldn't need to replicate/bugfix the logic here
1260 * when they implement async or easily-killable versions of this or
1261 * other "should-be-internal" functions (like clear_halt).
1262 * should hcd+usbcore postprocess control requests?
1263 */
1264
1265 /* prevent submissions using previous endpoint settings */
Alan Stern3b23dd62008-12-05 14:10:34 -05001266 if (iface->cur_altsetting != alt) {
1267 remove_intf_ep_devs(iface);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001268 usb_remove_sysfs_intf_files(iface);
Alan Stern3b23dd62008-12-05 14:10:34 -05001269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 usb_disable_interface(dev, iface);
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 iface->cur_altsetting = alt;
1273
1274 /* If the interface only has one altsetting and the device didn't
David Brownella81e7ec2005-04-18 17:39:25 -07001275 * accept the request, we attempt to carry out the equivalent action
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 * by manually clearing the HALT feature for each endpoint in the
1277 * new altsetting.
1278 */
1279 if (manual) {
1280 int i;
1281
1282 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001283 epaddr = alt->endpoint[i].desc.bEndpointAddress;
1284 pipe = __create_pipe(dev,
1285 USB_ENDPOINT_NUMBER_MASK & epaddr) |
1286 (usb_endpoint_out(epaddr) ?
1287 USB_DIR_OUT : USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 usb_clear_halt(dev, pipe);
1290 }
1291 }
1292
1293 /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
1294 *
1295 * Note:
1296 * Despite EP0 is always present in all interfaces/AS, the list of
1297 * endpoints from the descriptor does not contain EP0. Due to its
1298 * omnipresence one might expect EP0 being considered "affected" by
1299 * any SetInterface request and hence assume toggles need to be reset.
1300 * However, EP0 toggles are re-synced for every individual transfer
1301 * during the SETUP stage - hence EP0 toggles are "don't care" here.
1302 * (Likewise, EP0 never "halts" on well designed devices.)
1303 */
1304 usb_enable_interface(dev, iface);
Alan Stern3b23dd62008-12-05 14:10:34 -05001305 if (device_is_registered(&iface->dev)) {
Alan Stern0e6c8e82005-10-24 15:33:03 -04001306 usb_create_sysfs_intf_files(iface);
Alan Stern3b23dd62008-12-05 14:10:34 -05001307 create_intf_ep_devs(iface);
1308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return 0;
1310}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001311EXPORT_SYMBOL_GPL(usb_set_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313/**
1314 * usb_reset_configuration - lightweight device reset
1315 * @dev: the device whose configuration is being reset
1316 *
1317 * This issues a standard SET_CONFIGURATION request to the device using
1318 * the current configuration. The effect is to reset most USB-related
1319 * state in the device, including interface altsettings (reset to zero),
1320 * endpoint halts (cleared), and data toggle (only for bulk and interrupt
1321 * endpoints). Other usbcore state is unchanged, including bindings of
1322 * usb device drivers to interfaces.
1323 *
1324 * Because this affects multiple interfaces, avoid using this with composite
1325 * (multi-interface) devices. Instead, the driver for each interface may
David Brownella81e7ec2005-04-18 17:39:25 -07001326 * use usb_set_interface() on the interfaces it claims. Be careful though;
1327 * some devices don't support the SET_INTERFACE request, and others won't
1328 * reset all the interface state (notably data toggles). Resetting the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 * configuration would affect other drivers' interfaces.
1330 *
1331 * The caller must own the device lock.
1332 *
1333 * Returns zero on success, else a negative error code.
1334 */
1335int usb_reset_configuration(struct usb_device *dev)
1336{
1337 int i, retval;
1338 struct usb_host_config *config;
1339
1340 if (dev->state == USB_STATE_SUSPENDED)
1341 return -EHOSTUNREACH;
1342
1343 /* caller must have locked the device and must own
1344 * the usb bus readlock (so driver bindings are stable);
1345 * calls during probe() are fine
1346 */
1347
1348 for (i = 1; i < 16; ++i) {
1349 usb_disable_endpoint(dev, i);
1350 usb_disable_endpoint(dev, i + USB_DIR_IN);
1351 }
1352
1353 config = dev->actconfig;
1354 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1355 USB_REQ_SET_CONFIGURATION, 0,
1356 config->desc.bConfigurationValue, 0,
1357 NULL, 0, USB_CTRL_SET_TIMEOUT);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001358 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 dev->toggle[0] = dev->toggle[1] = 0;
1362
1363 /* re-init hc/hcd interface/endpoint state */
1364 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1365 struct usb_interface *intf = config->interface[i];
1366 struct usb_host_interface *alt;
1367
1368 alt = usb_altnum_to_altsetting(intf, 0);
1369
1370 /* No altsetting 0? We'll assume the first altsetting.
1371 * We could use a GetInterface call, but if a device is
1372 * so non-compliant that it doesn't have altsetting 0
1373 * then I wouldn't trust its reply anyway.
1374 */
1375 if (!alt)
1376 alt = &intf->altsetting[0];
1377
Alan Stern3b23dd62008-12-05 14:10:34 -05001378 if (alt != intf->cur_altsetting) {
1379 remove_intf_ep_devs(intf);
1380 usb_remove_sysfs_intf_files(intf);
1381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 intf->cur_altsetting = alt;
1383 usb_enable_interface(dev, intf);
Alan Stern3b23dd62008-12-05 14:10:34 -05001384 if (device_is_registered(&intf->dev)) {
Alan Stern0e6c8e82005-10-24 15:33:03 -04001385 usb_create_sysfs_intf_files(intf);
Alan Stern3b23dd62008-12-05 14:10:34 -05001386 create_intf_ep_devs(intf);
1387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389 return 0;
1390}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001391EXPORT_SYMBOL_GPL(usb_reset_configuration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Greg Kroah-Hartmanb0e396e2007-08-02 22:44:27 -06001393static void usb_release_interface(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 struct usb_interface *intf = to_usb_interface(dev);
1396 struct usb_interface_cache *intfc =
1397 altsetting_to_usb_interface_cache(intf->altsetting);
1398
1399 kref_put(&intfc->ref, usb_release_interface_cache);
1400 kfree(intf);
1401}
1402
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001403#ifdef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +02001404static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001405{
1406 struct usb_device *usb_dev;
1407 struct usb_interface *intf;
1408 struct usb_host_interface *alt;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001409
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001410 intf = to_usb_interface(dev);
1411 usb_dev = interface_to_usbdev(intf);
1412 alt = intf->cur_altsetting;
1413
Kay Sievers7eff2e72007-08-14 15:15:12 +02001414 if (add_uevent_var(env, "INTERFACE=%d/%d/%d",
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001415 alt->desc.bInterfaceClass,
1416 alt->desc.bInterfaceSubClass,
1417 alt->desc.bInterfaceProtocol))
1418 return -ENOMEM;
1419
Kay Sievers7eff2e72007-08-14 15:15:12 +02001420 if (add_uevent_var(env,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001421 "MODALIAS=usb:"
1422 "v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001423 le16_to_cpu(usb_dev->descriptor.idVendor),
1424 le16_to_cpu(usb_dev->descriptor.idProduct),
1425 le16_to_cpu(usb_dev->descriptor.bcdDevice),
1426 usb_dev->descriptor.bDeviceClass,
1427 usb_dev->descriptor.bDeviceSubClass,
1428 usb_dev->descriptor.bDeviceProtocol,
1429 alt->desc.bInterfaceClass,
1430 alt->desc.bInterfaceSubClass,
1431 alt->desc.bInterfaceProtocol))
1432 return -ENOMEM;
1433
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001434 return 0;
1435}
1436
1437#else
1438
Kay Sievers7eff2e72007-08-14 15:15:12 +02001439static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001440{
1441 return -ENODEV;
1442}
1443#endif /* CONFIG_HOTPLUG */
1444
1445struct device_type usb_if_device_type = {
1446 .name = "usb_interface",
1447 .release = usb_release_interface,
1448 .uevent = usb_if_uevent,
1449};
1450
Craig W. Nadler165fe972007-06-15 23:14:35 -04001451static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001452 struct usb_host_config *config,
1453 u8 inum)
Craig W. Nadler165fe972007-06-15 23:14:35 -04001454{
1455 struct usb_interface_assoc_descriptor *retval = NULL;
1456 struct usb_interface_assoc_descriptor *intf_assoc;
1457 int first_intf;
1458 int last_intf;
1459 int i;
1460
1461 for (i = 0; (i < USB_MAXIADS && config->intf_assoc[i]); i++) {
1462 intf_assoc = config->intf_assoc[i];
1463 if (intf_assoc->bInterfaceCount == 0)
1464 continue;
1465
1466 first_intf = intf_assoc->bFirstInterface;
1467 last_intf = first_intf + (intf_assoc->bInterfaceCount - 1);
1468 if (inum >= first_intf && inum <= last_intf) {
1469 if (!retval)
1470 retval = intf_assoc;
1471 else
1472 dev_err(&dev->dev, "Interface #%d referenced"
1473 " by multiple IADs\n", inum);
1474 }
1475 }
1476
1477 return retval;
1478}
1479
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001480
1481/*
1482 * Internal function to queue a device reset
1483 *
1484 * This is initialized into the workstruct in 'struct
1485 * usb_device->reset_ws' that is launched by
1486 * message.c:usb_set_configuration() when initializing each 'struct
1487 * usb_interface'.
1488 *
1489 * It is safe to get the USB device without reference counts because
1490 * the life cycle of @iface is bound to the life cycle of @udev. Then,
1491 * this function will be ran only if @iface is alive (and before
1492 * freeing it any scheduled instances of it will have been cancelled).
1493 *
1494 * We need to set a flag (usb_dev->reset_running) because when we call
1495 * the reset, the interfaces might be unbound. The current interface
1496 * cannot try to remove the queued work as it would cause a deadlock
1497 * (you cannot remove your work from within your executing
1498 * workqueue). This flag lets it know, so that
1499 * usb_cancel_queued_reset() doesn't try to do it.
1500 *
1501 * See usb_queue_reset_device() for more details
1502 */
1503void __usb_queue_reset_device(struct work_struct *ws)
1504{
1505 int rc;
1506 struct usb_interface *iface =
1507 container_of(ws, struct usb_interface, reset_ws);
1508 struct usb_device *udev = interface_to_usbdev(iface);
1509
1510 rc = usb_lock_device_for_reset(udev, iface);
1511 if (rc >= 0) {
1512 iface->reset_running = 1;
1513 usb_reset_device(udev);
1514 iface->reset_running = 0;
1515 usb_unlock_device(udev);
1516 }
1517}
1518
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520/*
1521 * usb_set_configuration - Makes a particular device setting be current
1522 * @dev: the device whose configuration is being updated
1523 * @configuration: the configuration being chosen.
1524 * Context: !in_interrupt(), caller owns the device lock
1525 *
1526 * This is used to enable non-default device modes. Not all devices
1527 * use this kind of configurability; many devices only have one
1528 * configuration.
1529 *
Alan Stern3f141e22007-02-08 16:40:43 -05001530 * @configuration is the value of the configuration to be installed.
1531 * According to the USB spec (e.g. section 9.1.1.5), configuration values
1532 * must be non-zero; a value of zero indicates that the device in
1533 * unconfigured. However some devices erroneously use 0 as one of their
1534 * configuration values. To help manage such devices, this routine will
1535 * accept @configuration = -1 as indicating the device should be put in
1536 * an unconfigured state.
1537 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 * USB device configurations may affect Linux interoperability,
1539 * power consumption and the functionality available. For example,
1540 * the default configuration is limited to using 100mA of bus power,
1541 * so that when certain device functionality requires more power,
1542 * and the device is bus powered, that functionality should be in some
1543 * non-default device configuration. Other device modes may also be
1544 * reflected as configuration options, such as whether two ISDN
1545 * channels are available independently; and choosing between open
1546 * standard device protocols (like CDC) or proprietary ones.
1547 *
Inaky Perez-Gonzalez16bbab22007-07-31 20:34:01 -07001548 * Note that a non-authorized device (dev->authorized == 0) will only
1549 * be put in unconfigured mode.
1550 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 * Note that USB has an additional level of device configurability,
1552 * associated with interfaces. That configurability is accessed using
1553 * usb_set_interface().
1554 *
1555 * This call is synchronous. The calling context must be able to sleep,
1556 * must own the device lock, and must not hold the driver model's USB
Ming Lei6d243e52008-06-17 23:24:08 +08001557 * bus mutex; usb interface driver probe() methods cannot use this routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 *
1559 * Returns zero on success, or else the status code returned by the
Steven Cole093cf722005-05-03 19:07:24 -06001560 * underlying call that failed. On successful completion, each interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 * in the original device configuration has been destroyed, and each one
1562 * in the new configuration has been probed by all relevant usb device
1563 * drivers currently known to the kernel.
1564 */
1565int usb_set_configuration(struct usb_device *dev, int configuration)
1566{
1567 int i, ret;
1568 struct usb_host_config *cp = NULL;
1569 struct usb_interface **new_interfaces = NULL;
1570 int n, nintf;
1571
Inaky Perez-Gonzalez16bbab22007-07-31 20:34:01 -07001572 if (dev->authorized == 0 || configuration == -1)
Alan Stern3f141e22007-02-08 16:40:43 -05001573 configuration = 0;
1574 else {
1575 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1576 if (dev->config[i].desc.bConfigurationValue ==
1577 configuration) {
1578 cp = &dev->config[i];
1579 break;
1580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582 }
1583 if ((!cp && configuration != 0))
1584 return -EINVAL;
1585
1586 /* The USB spec says configuration 0 means unconfigured.
1587 * But if a device includes a configuration numbered 0,
1588 * we will accept it as a correctly configured state.
Alan Stern3f141e22007-02-08 16:40:43 -05001589 * Use -1 if you really want to unconfigure the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 */
1591 if (cp && configuration == 0)
1592 dev_warn(&dev->dev, "config 0 descriptor??\n");
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 /* Allocate memory for new interfaces before doing anything else,
1595 * so that if we run out then nothing will have changed. */
1596 n = nintf = 0;
1597 if (cp) {
1598 nintf = cp->desc.bNumInterfaces;
1599 new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
1600 GFP_KERNEL);
1601 if (!new_interfaces) {
Joe Perches898eb712007-10-18 03:06:30 -07001602 dev_err(&dev->dev, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return -ENOMEM;
1604 }
1605
1606 for (; n < nintf; ++n) {
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001607 new_interfaces[n] = kzalloc(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 sizeof(struct usb_interface),
1609 GFP_KERNEL);
1610 if (!new_interfaces[n]) {
Joe Perches898eb712007-10-18 03:06:30 -07001611 dev_err(&dev->dev, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 ret = -ENOMEM;
1613free_interfaces:
1614 while (--n >= 0)
1615 kfree(new_interfaces[n]);
1616 kfree(new_interfaces);
1617 return ret;
1618 }
1619 }
Alan Stern6ad07122006-06-01 13:59:16 -04001620
1621 i = dev->bus_mA - cp->desc.bMaxPower * 2;
1622 if (i < 0)
1623 dev_warn(&dev->dev, "new config #%d exceeds power "
1624 "limit by %dmA\n",
1625 configuration, -i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
1627
Alan Stern01d883d2006-08-30 15:47:18 -04001628 /* Wake up the device so we can send it the Set-Config request */
Alan Stern94fcda12006-11-20 11:38:46 -05001629 ret = usb_autoresume_device(dev);
Alan Stern01d883d2006-08-30 15:47:18 -04001630 if (ret)
1631 goto free_interfaces;
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 /* if it's already configured, clear out old state first.
1634 * getting rid of old interfaces means unbinding their drivers.
1635 */
1636 if (dev->state != USB_STATE_ADDRESS)
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001637 usb_disable_device(dev, 1); /* Skip ep0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001639 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1640 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
1641 NULL, 0, USB_CTRL_SET_TIMEOUT);
1642 if (ret < 0) {
Alan Stern6ad07122006-06-01 13:59:16 -04001643 /* All the old state is gone, so what else can we do?
1644 * The device is probably useless now anyway.
1645 */
1646 cp = NULL;
1647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 dev->actconfig = cp;
Alan Stern6ad07122006-06-01 13:59:16 -04001650 if (!cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 usb_set_device_state(dev, USB_STATE_ADDRESS);
Alan Stern94fcda12006-11-20 11:38:46 -05001652 usb_autosuspend_device(dev);
Alan Stern6ad07122006-06-01 13:59:16 -04001653 goto free_interfaces;
1654 }
1655 usb_set_device_state(dev, USB_STATE_CONFIGURED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Alan Stern6ad07122006-06-01 13:59:16 -04001657 /* Initialize the new interface structures and the
1658 * hc/hcd/usbcore interface/endpoint state.
1659 */
1660 for (i = 0; i < nintf; ++i) {
1661 struct usb_interface_cache *intfc;
1662 struct usb_interface *intf;
1663 struct usb_host_interface *alt;
1664
1665 cp->interface[i] = intf = new_interfaces[i];
1666 intfc = cp->intf_cache[i];
1667 intf->altsetting = intfc->altsetting;
1668 intf->num_altsetting = intfc->num_altsetting;
Craig W. Nadler165fe972007-06-15 23:14:35 -04001669 intf->intf_assoc = find_iad(dev, cp, i);
Alan Stern6ad07122006-06-01 13:59:16 -04001670 kref_get(&intfc->ref);
1671
1672 alt = usb_altnum_to_altsetting(intf, 0);
1673
1674 /* No altsetting 0? We'll assume the first altsetting.
1675 * We could use a GetInterface call, but if a device is
1676 * so non-compliant that it doesn't have altsetting 0
1677 * then I wouldn't trust its reply anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 */
Alan Stern6ad07122006-06-01 13:59:16 -04001679 if (!alt)
1680 alt = &intf->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Alan Stern6ad07122006-06-01 13:59:16 -04001682 intf->cur_altsetting = alt;
1683 usb_enable_interface(dev, intf);
1684 intf->dev.parent = &dev->dev;
1685 intf->dev.driver = NULL;
1686 intf->dev.bus = &usb_bus_type;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001687 intf->dev.type = &usb_if_device_type;
Alan Stern2e5f10e2008-04-30 15:37:19 -04001688 intf->dev.groups = usb_interface_groups;
Alan Stern6ad07122006-06-01 13:59:16 -04001689 intf->dev.dma_mask = dev->dev.dma_mask;
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08001690 INIT_WORK(&intf->reset_ws, __usb_queue_reset_device);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001691 device_initialize(&intf->dev);
Alan Stern6ad07122006-06-01 13:59:16 -04001692 mark_quiesced(intf);
Kay Sievers0031a062008-05-02 06:02:41 +02001693 dev_set_name(&intf->dev, "%d-%s:%d.%d",
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001694 dev->bus->busnum, dev->devpath,
1695 configuration, alt->desc.bInterfaceNumber);
Alan Stern6ad07122006-06-01 13:59:16 -04001696 }
1697 kfree(new_interfaces);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Alan Stern6ad07122006-06-01 13:59:16 -04001699 if (cp->string == NULL)
1700 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Alan Stern6ad07122006-06-01 13:59:16 -04001702 /* Now that all the interfaces are set up, register them
1703 * to trigger binding of drivers to interfaces. probe()
1704 * routines may install different altsettings and may
1705 * claim() any interfaces not yet bound. Many class drivers
1706 * need that: CDC, audio, video, etc.
1707 */
1708 for (i = 0; i < nintf; ++i) {
1709 struct usb_interface *intf = cp->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001711 dev_dbg(&dev->dev,
Alan Stern6ad07122006-06-01 13:59:16 -04001712 "adding %s (config #%d, interface %d)\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001713 dev_name(&intf->dev), configuration,
Alan Stern6ad07122006-06-01 13:59:16 -04001714 intf->cur_altsetting->desc.bInterfaceNumber);
Greg Kroah-Hartman3e35bf32008-01-30 15:21:33 -08001715 ret = device_add(&intf->dev);
Alan Stern6ad07122006-06-01 13:59:16 -04001716 if (ret != 0) {
1717 dev_err(&dev->dev, "device_add(%s) --> %d\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +02001718 dev_name(&intf->dev), ret);
Alan Stern6ad07122006-06-01 13:59:16 -04001719 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
Alan Stern3b23dd62008-12-05 14:10:34 -05001721 create_intf_ep_devs(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723
Alan Stern94fcda12006-11-20 11:38:46 -05001724 usb_autosuspend_device(dev);
Alan Stern86d30742005-07-29 12:17:16 -07001725 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
Alan Stern088dc272006-08-21 12:08:19 -04001728struct set_config_request {
1729 struct usb_device *udev;
1730 int config;
1731 struct work_struct work;
1732};
1733
1734/* Worker routine for usb_driver_set_configuration() */
David Howellsc4028952006-11-22 14:57:56 +00001735static void driver_set_config_work(struct work_struct *work)
Alan Stern088dc272006-08-21 12:08:19 -04001736{
David Howellsc4028952006-11-22 14:57:56 +00001737 struct set_config_request *req =
1738 container_of(work, struct set_config_request, work);
Alan Stern088dc272006-08-21 12:08:19 -04001739
1740 usb_lock_device(req->udev);
1741 usb_set_configuration(req->udev, req->config);
1742 usb_unlock_device(req->udev);
1743 usb_put_dev(req->udev);
1744 kfree(req);
1745}
1746
1747/**
1748 * usb_driver_set_configuration - Provide a way for drivers to change device configurations
1749 * @udev: the device whose configuration is being updated
1750 * @config: the configuration being chosen.
1751 * Context: In process context, must be able to sleep
1752 *
1753 * Device interface drivers are not allowed to change device configurations.
1754 * This is because changing configurations will destroy the interface the
1755 * driver is bound to and create new ones; it would be like a floppy-disk
1756 * driver telling the computer to replace the floppy-disk drive with a
1757 * tape drive!
1758 *
1759 * Still, in certain specialized circumstances the need may arise. This
1760 * routine gets around the normal restrictions by using a work thread to
1761 * submit the change-config request.
1762 *
1763 * Returns 0 if the request was succesfully queued, error code otherwise.
1764 * The caller has no way to know whether the queued request will eventually
1765 * succeed.
1766 */
1767int usb_driver_set_configuration(struct usb_device *udev, int config)
1768{
1769 struct set_config_request *req;
1770
1771 req = kmalloc(sizeof(*req), GFP_KERNEL);
1772 if (!req)
1773 return -ENOMEM;
1774 req->udev = udev;
1775 req->config = config;
David Howellsc4028952006-11-22 14:57:56 +00001776 INIT_WORK(&req->work, driver_set_config_work);
Alan Stern088dc272006-08-21 12:08:19 -04001777
1778 usb_get_dev(udev);
Alan Stern1737bf22006-12-15 16:04:52 -05001779 schedule_work(&req->work);
Alan Stern088dc272006-08-21 12:08:19 -04001780 return 0;
1781}
1782EXPORT_SYMBOL_GPL(usb_driver_set_configuration);