blob: 025ac0bd35340be8178bccb59f08105768475783 [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>
Oliver Neukum7ceec1f2007-01-26 14:26:21 +010014#include <linux/usb/quirks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/byteorder.h>
Alexey Dobriyan5d68dfc2006-01-19 00:06:07 +030016#include <asm/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
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/*-------------------------------------------------------------------*/
77// returns status (negative) or length (positive)
78static int usb_internal_control_msg(struct usb_device *usb_dev,
79 unsigned int pipe,
80 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;
90
91 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/**
102 * 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
112 * timing out (if 0 the wait is forever)
113 * Context: !in_interrupt ()
114 *
115 * This function sends a simple control message to a specified endpoint
116 * and waits for the message to complete, or timeout.
117 *
118 * If successful, it returns the number of bytes transferred, otherwise a negative error number.
119 *
120 * Don't use this function from within an interrupt context, like a
121 * bottom half handler. If you need an asynchronous message, or need to send
122 * a message from within interrupt context, use usb_submit_urb()
123 * If a thread in your driver uses this call, make sure your disconnect()
124 * method can wait for it to complete. Since you don't have a handle on
125 * the URB used, you can't cancel the request.
126 */
127int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request, __u8 requesttype,
128 __u16 value, __u16 index, void *data, __u16 size, int timeout)
129{
130 struct usb_ctrlrequest *dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
131 int ret;
132
133 if (!dr)
134 return -ENOMEM;
135
136 dr->bRequestType= requesttype;
137 dr->bRequest = request;
138 dr->wValue = cpu_to_le16p(&value);
139 dr->wIndex = cpu_to_le16p(&index);
140 dr->wLength = cpu_to_le16p(&size);
141
142 //dbg("usb_control_msg");
143
144 ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);
145
146 kfree(dr);
147
148 return ret;
149}
150
151
152/**
Greg Kroah-Hartman782a7a62006-05-19 13:20:20 -0700153 * usb_interrupt_msg - Builds an interrupt urb, sends it off and waits for completion
154 * @usb_dev: pointer to the usb device to send the message to
155 * @pipe: endpoint "pipe" to send the message to
156 * @data: pointer to the data to send
157 * @len: length in bytes of the data to send
158 * @actual_length: pointer to a location to put the actual length transferred in bytes
159 * @timeout: time in msecs to wait for the message to complete before
160 * timing out (if 0 the wait is forever)
161 * Context: !in_interrupt ()
162 *
163 * This function sends a simple interrupt message to a specified endpoint and
164 * waits for the message to complete, or timeout.
165 *
166 * If successful, it returns 0, otherwise a negative error number. The number
167 * of actual bytes transferred will be stored in the actual_length paramater.
168 *
169 * Don't use this function from within an interrupt context, like a bottom half
170 * handler. If you need an asynchronous message, or need to send a message
171 * from within interrupt context, use usb_submit_urb() If a thread in your
172 * driver uses this call, make sure your disconnect() method can wait for it to
173 * complete. Since you don't have a handle on the URB used, you can't cancel
174 * the request.
175 */
176int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe,
177 void *data, int len, int *actual_length, int timeout)
178{
179 return usb_bulk_msg(usb_dev, pipe, data, len, actual_length, timeout);
180}
181EXPORT_SYMBOL_GPL(usb_interrupt_msg);
182
183/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion
185 * @usb_dev: pointer to the usb device to send the message to
186 * @pipe: endpoint "pipe" to send the message to
187 * @data: pointer to the data to send
188 * @len: length in bytes of the data to send
189 * @actual_length: pointer to a location to put the actual length transferred in bytes
190 * @timeout: time in msecs to wait for the message to complete before
191 * timing out (if 0 the wait is forever)
192 * Context: !in_interrupt ()
193 *
194 * This function sends a simple bulk message to a specified endpoint
195 * and waits for the message to complete, or timeout.
196 *
197 * If successful, it returns 0, otherwise a negative error number.
198 * The number of actual bytes transferred will be stored in the
199 * actual_length paramater.
200 *
201 * Don't use this function from within an interrupt context, like a
202 * bottom half handler. If you need an asynchronous message, or need to
203 * send a message from within interrupt context, use usb_submit_urb()
204 * If a thread in your driver uses this call, make sure your disconnect()
205 * method can wait for it to complete. Since you don't have a handle on
206 * the URB used, you can't cancel the request.
Alan Sternd09d36a2005-09-26 16:22:45 -0400207 *
208 * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT
209 * ioctl, users are forced to abuse this routine by using it to submit
210 * URBs for interrupt endpoints. We will take the liberty of creating
211 * an interrupt URB (with the default interval) if the target is an
212 * interrupt endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 */
214int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
215 void *data, int len, int *actual_length, int timeout)
216{
217 struct urb *urb;
Alan Sternd09d36a2005-09-26 16:22:45 -0400218 struct usb_host_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Alan Sternd09d36a2005-09-26 16:22:45 -0400220 ep = (usb_pipein(pipe) ? usb_dev->ep_in : usb_dev->ep_out)
221 [usb_pipeendpoint(pipe)];
222 if (!ep || len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return -EINVAL;
224
Alan Sternd09d36a2005-09-26 16:22:45 -0400225 urb = usb_alloc_urb(0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (!urb)
227 return -ENOMEM;
228
Alan Sternd09d36a2005-09-26 16:22:45 -0400229 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
230 USB_ENDPOINT_XFER_INT) {
231 pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
232 usb_fill_int_urb(urb, usb_dev, pipe, data, len,
Alan Stern8d062b92007-04-23 17:30:32 -0400233 usb_api_blocking_completion, NULL,
234 ep->desc.bInterval);
Alan Sternd09d36a2005-09-26 16:22:45 -0400235 } else
236 usb_fill_bulk_urb(urb, usb_dev, pipe, data, len,
237 usb_api_blocking_completion, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 return usb_start_wait_urb(urb, timeout, actual_length);
240}
241
242/*-------------------------------------------------------------------*/
243
244static void sg_clean (struct usb_sg_request *io)
245{
246 if (io->urbs) {
247 while (io->entries--)
248 usb_free_urb (io->urbs [io->entries]);
249 kfree (io->urbs);
250 io->urbs = NULL;
251 }
252 if (io->dev->dev.dma_mask != NULL)
Alan Stern5e60a162007-07-30 17:07:21 -0400253 usb_buffer_unmap_sg (io->dev, usb_pipein(io->pipe),
254 io->sg, io->nents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 io->dev = NULL;
256}
257
David Howells7d12e782006-10-05 14:55:46 +0100258static void sg_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200260 struct usb_sg_request *io = urb->context;
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700261 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 spin_lock (&io->lock);
264
265 /* In 2.5 we require hcds' endpoint queues not to progress after fault
266 * reports, until the completion callback (this!) returns. That lets
267 * device driver code (like this routine) unlink queued urbs first,
268 * if it needs to, since the HC won't work on them at all. So it's
269 * not possible for page N+1 to overwrite page N, and so on.
270 *
271 * That's only for "hard" faults; "soft" faults (unlinks) sometimes
272 * complete before the HCD can get requests away from hardware,
273 * though never during cleanup after a hard fault.
274 */
275 if (io->status
276 && (io->status != -ECONNRESET
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700277 || status != -ECONNRESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 && urb->actual_length) {
279 dev_err (io->dev->bus->controller,
280 "dev %s ep%d%s scatterlist error %d/%d\n",
281 io->dev->devpath,
Alan Stern5e60a162007-07-30 17:07:21 -0400282 usb_endpoint_num(&urb->ep->desc),
283 usb_urb_dir_in(urb) ? "in" : "out",
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700284 status, io->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 // BUG ();
286 }
287
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700288 if (io->status == 0 && status && status != -ECONNRESET) {
289 int i, found, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700291 io->status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 /* the previous urbs, and this one, completed already.
294 * unlink pending urbs so they won't rx/tx bad data.
295 * careful: unlink can sometimes be synchronous...
296 */
297 spin_unlock (&io->lock);
298 for (i = 0, found = 0; i < io->entries; i++) {
299 if (!io->urbs [i] || !io->urbs [i]->dev)
300 continue;
301 if (found) {
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700302 retval = usb_unlink_urb (io->urbs [i]);
303 if (retval != -EINPROGRESS &&
304 retval != -ENODEV &&
305 retval != -EBUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 dev_err (&io->dev->dev,
307 "%s, unlink --> %d\n",
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700308 __FUNCTION__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 } else if (urb == io->urbs [i])
310 found = 1;
311 }
312 spin_lock (&io->lock);
313 }
314 urb->dev = NULL;
315
316 /* on the last completion, signal usb_sg_wait() */
317 io->bytes += urb->actual_length;
318 io->count--;
319 if (!io->count)
320 complete (&io->complete);
321
322 spin_unlock (&io->lock);
323}
324
325
326/**
327 * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request
328 * @io: request block being initialized. until usb_sg_wait() returns,
329 * treat this as a pointer to an opaque block of memory,
330 * @dev: the usb device that will send or receive the data
331 * @pipe: endpoint "pipe" used to transfer the data
332 * @period: polling rate for interrupt endpoints, in frames or
333 * (for high speed endpoints) microframes; ignored for bulk
334 * @sg: scatterlist entries
335 * @nents: how many entries in the scatterlist
336 * @length: how many bytes to send from the scatterlist, or zero to
337 * send every byte identified in the list.
338 * @mem_flags: SLAB_* flags affecting memory allocations in this call
339 *
340 * Returns zero for success, else a negative errno value. This initializes a
341 * scatter/gather request, allocating resources such as I/O mappings and urb
342 * memory (except maybe memory used by USB controller drivers).
343 *
344 * The request must be issued using usb_sg_wait(), which waits for the I/O to
345 * complete (or to be canceled) and then cleans up all resources allocated by
346 * usb_sg_init().
347 *
348 * The request may be canceled with usb_sg_cancel(), either before or after
349 * usb_sg_wait() is called.
350 */
351int usb_sg_init (
352 struct usb_sg_request *io,
353 struct usb_device *dev,
354 unsigned pipe,
355 unsigned period,
356 struct scatterlist *sg,
357 int nents,
358 size_t length,
Al Viro55016f12005-10-21 03:21:58 -0400359 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360)
361{
362 int i;
363 int urb_flags;
364 int dma;
365
366 if (!io || !dev || !sg
367 || usb_pipecontrol (pipe)
368 || usb_pipeisoc (pipe)
369 || nents <= 0)
370 return -EINVAL;
371
372 spin_lock_init (&io->lock);
373 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),
384 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
392 io->count = io->entries;
393 io->urbs = kmalloc (io->entries * sizeof *io->urbs, mem_flags);
394 if (!io->urbs)
395 goto nomem;
396
Alan Sternb375a042005-07-29 16:11:07 -0400397 urb_flags = URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (usb_pipein (pipe))
399 urb_flags |= URB_SHORT_NOT_OK;
400
401 for (i = 0; i < io->entries; i++) {
402 unsigned len;
403
404 io->urbs [i] = usb_alloc_urb (0, mem_flags);
405 if (!io->urbs [i]) {
406 io->entries = i;
407 goto nomem;
408 }
409
410 io->urbs [i]->dev = NULL;
411 io->urbs [i]->pipe = pipe;
412 io->urbs [i]->interval = period;
413 io->urbs [i]->transfer_flags = urb_flags;
414
415 io->urbs [i]->complete = sg_complete;
416 io->urbs [i]->context = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Tony Lindgren35d07fd2007-03-31 18:15:43 -0700418 /*
419 * Some systems need to revert to PIO when DMA is temporarily
420 * unavailable. For their sakes, both transfer_buffer and
421 * transfer_dma are set when possible. However this can only
David Brownella12b8db2007-07-22 15:13:13 -0700422 * work on systems without:
423 *
424 * - HIGHMEM, since DMA buffers located in high memory are
425 * not directly addressable by the CPU for PIO;
426 *
427 * - IOMMU, since dma_map_sg() is allowed to use an IOMMU to
428 * make virtually discontiguous buffers be "dma-contiguous"
429 * so that PIO and DMA need diferent numbers of URBs.
430 *
431 * So when HIGHMEM or IOMMU are in use, transfer_buffer is NULL
Tony Lindgren35d07fd2007-03-31 18:15:43 -0700432 * to prevent stale pointers and to help spot bugs.
433 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 io->urbs [i]->transfer_dma = sg_dma_address (sg + i);
436 len = sg_dma_len (sg + i);
David Brownella12b8db2007-07-22 15:13:13 -0700437#if defined(CONFIG_HIGHMEM) || defined(CONFIG_IOMMU)
Tony Lindgren35d07fd2007-03-31 18:15:43 -0700438 io->urbs[i]->transfer_buffer = NULL;
439#else
440 io->urbs[i]->transfer_buffer =
441 page_address(sg[i].page) + sg[i].offset;
442#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 } else {
444 /* hc may use _only_ transfer_buffer */
445 io->urbs [i]->transfer_buffer =
446 page_address (sg [i].page) + sg [i].offset;
447 len = sg [i].length;
448 }
449
450 if (length) {
451 len = min_t (unsigned, len, length);
452 length -= len;
453 if (length == 0)
454 io->entries = i + 1;
455 }
456 io->urbs [i]->transfer_buffer_length = len;
457 }
458 io->urbs [--i]->transfer_flags &= ~URB_NO_INTERRUPT;
459
460 /* transaction state */
461 io->status = 0;
462 io->bytes = 0;
463 init_completion (&io->complete);
464 return 0;
465
466nomem:
467 sg_clean (io);
468 return -ENOMEM;
469}
470
471
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 */
511void usb_sg_wait (struct usb_sg_request *io)
512{
513 int i, entries = io->entries;
514
515 /* queue the urbs. */
516 spin_lock_irq (&io->lock);
Alan Stern8ccef0d2007-06-21 16:26:46 -0400517 i = 0;
518 while (i < entries && !io->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 int retval;
520
521 io->urbs [i]->dev = io->dev;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800522 retval = usb_submit_urb (io->urbs [i], GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 /* after we submit, let completions or cancelations fire;
525 * we handshake using io->status.
526 */
527 spin_unlock_irq (&io->lock);
528 switch (retval) {
529 /* maybe we retrying will recover */
530 case -ENXIO: // hc didn't queue this one
531 case -EAGAIN:
532 case -ENOMEM:
533 io->urbs[i]->dev = NULL;
534 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 yield ();
536 break;
537
538 /* no error? continue immediately.
539 *
540 * NOTE: to work better with UHCI (4K I/O buffer may
541 * need 3K of TDs) it may be good to limit how many
542 * URBs are queued at once; N milliseconds?
543 */
544 case 0:
Alan Stern8ccef0d2007-06-21 16:26:46 -0400545 ++i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 cpu_relax ();
547 break;
548
549 /* fail any uncompleted urbs */
550 default:
551 io->urbs [i]->dev = NULL;
552 io->urbs [i]->status = retval;
553 dev_dbg (&io->dev->dev, "%s, submit --> %d\n",
554 __FUNCTION__, retval);
555 usb_sg_cancel (io);
556 }
557 spin_lock_irq (&io->lock);
558 if (retval && (io->status == 0 || io->status == -ECONNRESET))
559 io->status = retval;
560 }
561 io->count -= entries - i;
562 if (io->count == 0)
563 complete (&io->complete);
564 spin_unlock_irq (&io->lock);
565
566 /* OK, yes, this could be packaged as non-blocking.
567 * So could the submit loop above ... but it's easier to
568 * solve neither problem than to solve both!
569 */
570 wait_for_completion (&io->complete);
571
572 sg_clean (io);
573}
574
575/**
576 * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
577 * @io: request block, initialized with usb_sg_init()
578 *
579 * This stops a request after it has been started by usb_sg_wait().
580 * It can also prevents one initialized by usb_sg_init() from starting,
581 * so that call just frees resources allocated to the request.
582 */
583void usb_sg_cancel (struct usb_sg_request *io)
584{
585 unsigned long flags;
586
587 spin_lock_irqsave (&io->lock, flags);
588
589 /* shut everything down, if it didn't already */
590 if (!io->status) {
591 int i;
592
593 io->status = -ECONNRESET;
594 spin_unlock (&io->lock);
595 for (i = 0; i < io->entries; i++) {
596 int retval;
597
598 if (!io->urbs [i]->dev)
599 continue;
600 retval = usb_unlink_urb (io->urbs [i]);
601 if (retval != -EINPROGRESS && retval != -EBUSY)
602 dev_warn (&io->dev->dev, "%s, unlink --> %d\n",
603 __FUNCTION__, retval);
604 }
605 spin_lock (&io->lock);
606 }
607 spin_unlock_irqrestore (&io->lock, flags);
608}
609
610/*-------------------------------------------------------------------*/
611
612/**
613 * usb_get_descriptor - issues a generic GET_DESCRIPTOR request
614 * @dev: the device whose descriptor is being retrieved
615 * @type: the descriptor type (USB_DT_*)
616 * @index: the number of the descriptor
617 * @buf: where to put the descriptor
618 * @size: how big is "buf"?
619 * Context: !in_interrupt ()
620 *
621 * Gets a USB descriptor. Convenience functions exist to simplify
622 * getting some types of descriptors. Use
623 * usb_get_string() or usb_string() for USB_DT_STRING.
624 * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG)
625 * are part of the device structure.
626 * In addition to a number of USB-standard descriptors, some
627 * devices also use class-specific or vendor-specific descriptors.
628 *
629 * This call is synchronous, and may not be used in an interrupt context.
630 *
631 * Returns the number of bytes received on success, or else the status code
632 * returned by the underlying usb_control_msg() call.
633 */
634int usb_get_descriptor(struct usb_device *dev, unsigned char type, unsigned char index, void *buf, int size)
635{
636 int i;
637 int result;
638
639 memset(buf,0,size); // Make sure we parse really received data
640
641 for (i = 0; i < 3; ++i) {
Alan Sternc39772d2007-08-20 10:45:28 -0400642 /* retry on length 0 or error; some devices are flakey */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
644 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
645 (type << 8) + index, 0, buf, size,
646 USB_CTRL_GET_TIMEOUT);
Alan Sternc39772d2007-08-20 10:45:28 -0400647 if (result <= 0 && result != -ETIMEDOUT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 continue;
649 if (result > 1 && ((u8 *)buf)[1] != type) {
650 result = -EPROTO;
651 continue;
652 }
653 break;
654 }
655 return result;
656}
657
658/**
659 * usb_get_string - gets a string descriptor
660 * @dev: the device whose string descriptor is being retrieved
661 * @langid: code for language chosen (from string descriptor zero)
662 * @index: the number of the descriptor
663 * @buf: where to put the string
664 * @size: how big is "buf"?
665 * Context: !in_interrupt ()
666 *
667 * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character,
668 * in little-endian byte order).
669 * The usb_string() function will often be a convenient way to turn
670 * these strings into kernel-printable form.
671 *
672 * Strings may be referenced in device, configuration, interface, or other
673 * descriptors, and could also be used in vendor-specific ways.
674 *
675 * This call is synchronous, and may not be used in an interrupt context.
676 *
677 * Returns the number of bytes received on success, or else the status code
678 * returned by the underlying usb_control_msg() call.
679 */
Adrian Bunke266a122005-11-08 21:05:43 +0100680static int usb_get_string(struct usb_device *dev, unsigned short langid,
681 unsigned char index, void *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 int i;
684 int result;
685
686 for (i = 0; i < 3; ++i) {
687 /* retry on length 0 or stall; some devices are flakey */
688 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
689 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
690 (USB_DT_STRING << 8) + index, langid, buf, size,
691 USB_CTRL_GET_TIMEOUT);
692 if (!(result == 0 || result == -EPIPE))
693 break;
694 }
695 return result;
696}
697
698static void usb_try_string_workarounds(unsigned char *buf, int *length)
699{
700 int newlength, oldlength = *length;
701
702 for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
703 if (!isprint(buf[newlength]) || buf[newlength + 1])
704 break;
705
706 if (newlength > 2) {
707 buf[0] = newlength;
708 *length = newlength;
709 }
710}
711
712static int usb_string_sub(struct usb_device *dev, unsigned int langid,
713 unsigned int index, unsigned char *buf)
714{
715 int rc;
716
717 /* Try to read the string descriptor by asking for the maximum
718 * possible number of bytes */
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100719 if (dev->quirks & USB_QUIRK_STRING_FETCH_255)
720 rc = -EIO;
721 else
722 rc = usb_get_string(dev, langid, index, buf, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 /* If that failed try to read the descriptor length, then
725 * ask for just that many bytes */
726 if (rc < 2) {
727 rc = usb_get_string(dev, langid, index, buf, 2);
728 if (rc == 2)
729 rc = usb_get_string(dev, langid, index, buf, buf[0]);
730 }
731
732 if (rc >= 2) {
733 if (!buf[0] && !buf[1])
734 usb_try_string_workarounds(buf, &rc);
735
736 /* There might be extra junk at the end of the descriptor */
737 if (buf[0] < rc)
738 rc = buf[0];
739
740 rc = rc - (rc & 1); /* force a multiple of two */
741 }
742
743 if (rc < 2)
744 rc = (rc < 0 ? rc : -EINVAL);
745
746 return rc;
747}
748
749/**
750 * usb_string - returns ISO 8859-1 version of a string descriptor
751 * @dev: the device whose string descriptor is being retrieved
752 * @index: the number of the descriptor
753 * @buf: where to put the string
754 * @size: how big is "buf"?
755 * Context: !in_interrupt ()
756 *
757 * This converts the UTF-16LE encoded strings returned by devices, from
758 * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones
759 * that are more usable in most kernel contexts. Note that all characters
760 * in the chosen descriptor that can't be encoded using ISO-8859-1
761 * are converted to the question mark ("?") character, and this function
762 * chooses strings in the first language supported by the device.
763 *
764 * The ASCII (or, redundantly, "US-ASCII") character set is the seven-bit
765 * subset of ISO 8859-1. ISO-8859-1 is the eight-bit subset of Unicode,
766 * and is appropriate for use many uses of English and several other
767 * Western European languages. (But it doesn't include the "Euro" symbol.)
768 *
769 * This call is synchronous, and may not be used in an interrupt context.
770 *
771 * Returns length of the string (>= 0) or usb_control_msg status (< 0).
772 */
773int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
774{
775 unsigned char *tbuf;
776 int err;
777 unsigned int u, idx;
778
779 if (dev->state == USB_STATE_SUSPENDED)
780 return -EHOSTUNREACH;
781 if (size <= 0 || !buf || !index)
782 return -EINVAL;
783 buf[0] = 0;
784 tbuf = kmalloc(256, GFP_KERNEL);
785 if (!tbuf)
786 return -ENOMEM;
787
788 /* get langid for strings if it's not yet known */
789 if (!dev->have_langid) {
790 err = usb_string_sub(dev, 0, 0, tbuf);
791 if (err < 0) {
792 dev_err (&dev->dev,
793 "string descriptor 0 read error: %d\n",
794 err);
795 goto errout;
796 } else if (err < 4) {
797 dev_err (&dev->dev, "string descriptor 0 too short\n");
798 err = -EINVAL;
799 goto errout;
800 } else {
Alan Sternce361582006-11-20 11:12:22 -0500801 dev->have_langid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 dev->string_langid = tbuf[2] | (tbuf[3]<< 8);
803 /* always use the first langid listed */
804 dev_dbg (&dev->dev, "default language 0x%04x\n",
805 dev->string_langid);
806 }
807 }
808
809 err = usb_string_sub(dev, dev->string_langid, index, tbuf);
810 if (err < 0)
811 goto errout;
812
813 size--; /* leave room for trailing NULL char in output buffer */
814 for (idx = 0, u = 2; u < err; u += 2) {
815 if (idx >= size)
816 break;
817 if (tbuf[u+1]) /* high byte */
818 buf[idx++] = '?'; /* non ISO-8859-1 character */
819 else
820 buf[idx++] = tbuf[u];
821 }
822 buf[idx] = 0;
823 err = idx;
824
825 if (tbuf[1] != USB_DT_STRING)
826 dev_dbg(&dev->dev, "wrong descriptor type %02x for string %d (\"%s\")\n", tbuf[1], index, buf);
827
828 errout:
829 kfree(tbuf);
830 return err;
831}
832
Alan Stern4f62efe2005-10-24 16:24:14 -0400833/**
834 * usb_cache_string - read a string descriptor and cache it for later use
835 * @udev: the device whose string descriptor is being read
836 * @index: the descriptor index
837 *
838 * Returns a pointer to a kmalloc'ed buffer containing the descriptor string,
839 * or NULL if the index is 0 or the string could not be read.
840 */
841char *usb_cache_string(struct usb_device *udev, int index)
842{
843 char *buf;
844 char *smallbuf = NULL;
845 int len;
846
847 if (index > 0 && (buf = kmalloc(256, GFP_KERNEL)) != NULL) {
848 if ((len = usb_string(udev, index, buf, 256)) > 0) {
849 if ((smallbuf = kmalloc(++len, GFP_KERNEL)) == NULL)
850 return buf;
851 memcpy(smallbuf, buf, len);
852 }
853 kfree(buf);
854 }
855 return smallbuf;
856}
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858/*
859 * usb_get_device_descriptor - (re)reads the device descriptor (usbcore)
860 * @dev: the device whose device descriptor is being updated
861 * @size: how much of the descriptor to read
862 * Context: !in_interrupt ()
863 *
864 * Updates the copy of the device descriptor stored in the device structure,
Laurent Pinchart6ab16a92006-11-07 10:16:25 +0100865 * which dedicates space for this purpose.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 *
867 * Not exported, only for use by the core. If drivers really want to read
868 * the device descriptor directly, they can call usb_get_descriptor() with
869 * type = USB_DT_DEVICE and index = 0.
870 *
871 * This call is synchronous, and may not be used in an interrupt context.
872 *
873 * Returns the number of bytes received on success, or else the status code
874 * returned by the underlying usb_control_msg() call.
875 */
876int usb_get_device_descriptor(struct usb_device *dev, unsigned int size)
877{
878 struct usb_device_descriptor *desc;
879 int ret;
880
881 if (size > sizeof(*desc))
882 return -EINVAL;
883 desc = kmalloc(sizeof(*desc), GFP_NOIO);
884 if (!desc)
885 return -ENOMEM;
886
887 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);
888 if (ret >= 0)
889 memcpy(&dev->descriptor, desc, size);
890 kfree(desc);
891 return ret;
892}
893
894/**
895 * usb_get_status - issues a GET_STATUS call
896 * @dev: the device whose status is being checked
897 * @type: USB_RECIP_*; for device, interface, or endpoint
898 * @target: zero (for device), else interface or endpoint number
899 * @data: pointer to two bytes of bitmap data
900 * Context: !in_interrupt ()
901 *
902 * Returns device, interface, or endpoint status. Normally only of
903 * interest to see if the device is self powered, or has enabled the
904 * remote wakeup facility; or whether a bulk or interrupt endpoint
905 * is halted ("stalled").
906 *
907 * Bits in these status bitmaps are set using the SET_FEATURE request,
908 * and cleared using the CLEAR_FEATURE request. The usb_clear_halt()
909 * function should be used to clear halt ("stall") status.
910 *
911 * This call is synchronous, and may not be used in an interrupt context.
912 *
913 * Returns the number of bytes received on success, or else the status code
914 * returned by the underlying usb_control_msg() call.
915 */
916int usb_get_status(struct usb_device *dev, int type, int target, void *data)
917{
918 int ret;
919 u16 *status = kmalloc(sizeof(*status), GFP_KERNEL);
920
921 if (!status)
922 return -ENOMEM;
923
924 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
925 USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status,
926 sizeof(*status), USB_CTRL_GET_TIMEOUT);
927
928 *(u16 *)data = *status;
929 kfree(status);
930 return ret;
931}
932
933/**
934 * usb_clear_halt - tells device to clear endpoint halt/stall condition
935 * @dev: device whose endpoint is halted
936 * @pipe: endpoint "pipe" being cleared
937 * Context: !in_interrupt ()
938 *
939 * This is used to clear halt conditions for bulk and interrupt endpoints,
940 * as reported by URB completion status. Endpoints that are halted are
941 * sometimes referred to as being "stalled". Such endpoints are unable
942 * to transmit or receive data until the halt status is cleared. Any URBs
943 * queued for such an endpoint should normally be unlinked by the driver
944 * before clearing the halt condition, as described in sections 5.7.5
945 * and 5.8.5 of the USB 2.0 spec.
946 *
947 * Note that control and isochronous endpoints don't halt, although control
948 * endpoints report "protocol stall" (for unsupported requests) using the
949 * same status code used to report a true stall.
950 *
951 * This call is synchronous, and may not be used in an interrupt context.
952 *
953 * Returns zero on success, or else the status code returned by the
954 * underlying usb_control_msg() call.
955 */
956int usb_clear_halt(struct usb_device *dev, int pipe)
957{
958 int result;
959 int endp = usb_pipeendpoint(pipe);
960
961 if (usb_pipein (pipe))
962 endp |= USB_DIR_IN;
963
964 /* we don't care if it wasn't halted first. in fact some devices
965 * (like some ibmcam model 1 units) seem to expect hosts to make
966 * this request for iso endpoints, which can't halt!
967 */
968 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
969 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
970 USB_ENDPOINT_HALT, endp, NULL, 0,
971 USB_CTRL_SET_TIMEOUT);
972
973 /* don't un-halt or force to DATA0 except on success */
974 if (result < 0)
975 return result;
976
977 /* NOTE: seems like Microsoft and Apple don't bother verifying
978 * the clear "took", so some devices could lock up if you check...
979 * such as the Hagiwara FlashGate DUAL. So we won't bother.
980 *
981 * NOTE: make sure the logic here doesn't diverge much from
982 * the copy in usb-storage, for as long as we need two copies.
983 */
984
985 /* toggle was reset by the clear */
986 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
987
988 return 0;
989}
990
991/**
992 * usb_disable_endpoint -- Disable an endpoint by address
993 * @dev: the device whose endpoint is being disabled
994 * @epaddr: the endpoint's address. Endpoint number for output,
995 * endpoint number + USB_DIR_IN for input
996 *
997 * Deallocates hcd/hardware state for this endpoint ... and nukes all
998 * pending urbs.
999 *
1000 * If the HCD hasn't registered a disable() function, this sets the
1001 * endpoint's maxpacket size to 0 to prevent further submissions.
1002 */
1003void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr)
1004{
1005 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1006 struct usb_host_endpoint *ep;
1007
1008 if (!dev)
1009 return;
1010
1011 if (usb_endpoint_out(epaddr)) {
1012 ep = dev->ep_out[epnum];
1013 dev->ep_out[epnum] = NULL;
1014 } else {
1015 ep = dev->ep_in[epnum];
1016 dev->ep_in[epnum] = NULL;
1017 }
Alan Sternbdd016b2007-07-30 17:05:22 -04001018 if (ep) {
1019 ep->enabled = 0;
Alan Sterna6d2bb92006-08-30 11:27:36 -04001020 usb_hcd_endpoint_disable(dev, ep);
Alan Sternbdd016b2007-07-30 17:05:22 -04001021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
1024/**
1025 * usb_disable_interface -- Disable all endpoints for an interface
1026 * @dev: the device whose interface is being disabled
1027 * @intf: pointer to the interface descriptor
1028 *
1029 * Disables all the endpoints for the interface's current altsetting.
1030 */
1031void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf)
1032{
1033 struct usb_host_interface *alt = intf->cur_altsetting;
1034 int i;
1035
1036 for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
1037 usb_disable_endpoint(dev,
1038 alt->endpoint[i].desc.bEndpointAddress);
1039 }
1040}
1041
1042/*
1043 * usb_disable_device - Disable all the endpoints for a USB device
1044 * @dev: the device whose endpoints are being disabled
1045 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
1046 *
1047 * Disables all the device's endpoints, potentially including endpoint 0.
1048 * Deallocates hcd/hardware state for the endpoints (nuking all or most
1049 * pending urbs) and usbcore state for the interfaces, so that usbcore
1050 * must usb_set_configuration() before any interfaces could be used.
1051 */
1052void usb_disable_device(struct usb_device *dev, int skip_ep0)
1053{
1054 int i;
1055
1056 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __FUNCTION__,
1057 skip_ep0 ? "non-ep0" : "all");
1058 for (i = skip_ep0; i < 16; ++i) {
1059 usb_disable_endpoint(dev, i);
1060 usb_disable_endpoint(dev, i + USB_DIR_IN);
1061 }
1062 dev->toggle[0] = dev->toggle[1] = 0;
1063
1064 /* getting rid of interfaces will disconnect
1065 * any drivers bound to them (a key side effect)
1066 */
1067 if (dev->actconfig) {
1068 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1069 struct usb_interface *interface;
1070
Alan Stern86d30742005-07-29 12:17:16 -07001071 /* remove this interface if it has been registered */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 interface = dev->actconfig->interface[i];
Daniel Ritzd305ef52005-09-22 00:47:24 -07001073 if (!device_is_registered(&interface->dev))
Alan Stern86d30742005-07-29 12:17:16 -07001074 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 dev_dbg (&dev->dev, "unregistering interface %s\n",
1076 interface->dev.bus_id);
1077 usb_remove_sysfs_intf_files(interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 device_del (&interface->dev);
1079 }
1080
1081 /* Now that the interfaces are unbound, nobody should
1082 * try to access them.
1083 */
1084 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1085 put_device (&dev->actconfig->interface[i]->dev);
1086 dev->actconfig->interface[i] = NULL;
1087 }
1088 dev->actconfig = NULL;
1089 if (dev->state == USB_STATE_CONFIGURED)
1090 usb_set_device_state(dev, USB_STATE_ADDRESS);
1091 }
1092}
1093
1094
1095/*
1096 * usb_enable_endpoint - Enable an endpoint for USB communications
1097 * @dev: the device whose interface is being enabled
1098 * @ep: the endpoint
1099 *
1100 * Resets the endpoint toggle, and sets dev->ep_{in,out} pointers.
1101 * For control endpoints, both the input and output sides are handled.
1102 */
Alan Sternbdd016b2007-07-30 17:05:22 -04001103void usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Alan Sternbdd016b2007-07-30 17:05:22 -04001105 int epnum = usb_endpoint_num(&ep->desc);
1106 int is_out = usb_endpoint_dir_out(&ep->desc);
1107 int is_control = usb_endpoint_xfer_control(&ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Alan Sternbdd016b2007-07-30 17:05:22 -04001109 if (is_out || is_control) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 usb_settoggle(dev, epnum, 1, 0);
1111 dev->ep_out[epnum] = ep;
1112 }
Alan Sternbdd016b2007-07-30 17:05:22 -04001113 if (!is_out || is_control) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 usb_settoggle(dev, epnum, 0, 0);
1115 dev->ep_in[epnum] = ep;
1116 }
Alan Sternbdd016b2007-07-30 17:05:22 -04001117 ep->enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
1120/*
1121 * usb_enable_interface - Enable all the endpoints for an interface
1122 * @dev: the device whose interface is being enabled
1123 * @intf: pointer to the interface descriptor
1124 *
1125 * Enables all the endpoints for the interface's current altsetting.
1126 */
1127static void usb_enable_interface(struct usb_device *dev,
1128 struct usb_interface *intf)
1129{
1130 struct usb_host_interface *alt = intf->cur_altsetting;
1131 int i;
1132
1133 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1134 usb_enable_endpoint(dev, &alt->endpoint[i]);
1135}
1136
1137/**
1138 * usb_set_interface - Makes a particular alternate setting be current
1139 * @dev: the device whose interface is being updated
1140 * @interface: the interface being updated
1141 * @alternate: the setting being chosen.
1142 * Context: !in_interrupt ()
1143 *
1144 * This is used to enable data transfers on interfaces that may not
1145 * be enabled by default. Not all devices support such configurability.
1146 * Only the driver bound to an interface may change its setting.
1147 *
1148 * Within any given configuration, each interface may have several
1149 * alternative settings. These are often used to control levels of
1150 * bandwidth consumption. For example, the default setting for a high
1151 * speed interrupt endpoint may not send more than 64 bytes per microframe,
1152 * while interrupt transfers of up to 3KBytes per microframe are legal.
1153 * Also, isochronous endpoints may never be part of an
1154 * interface's default setting. To access such bandwidth, alternate
1155 * interface settings must be made current.
1156 *
1157 * Note that in the Linux USB subsystem, bandwidth associated with
1158 * an endpoint in a given alternate setting is not reserved until an URB
1159 * is submitted that needs that bandwidth. Some other operating systems
1160 * allocate bandwidth early, when a configuration is chosen.
1161 *
1162 * This call is synchronous, and may not be used in an interrupt context.
1163 * Also, drivers must not change altsettings while urbs are scheduled for
1164 * endpoints in that interface; all such urbs must first be completed
1165 * (perhaps forced by unlinking).
1166 *
1167 * Returns zero on success, or else the status code returned by the
1168 * underlying usb_control_msg() call.
1169 */
1170int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1171{
1172 struct usb_interface *iface;
1173 struct usb_host_interface *alt;
1174 int ret;
1175 int manual = 0;
1176
1177 if (dev->state == USB_STATE_SUSPENDED)
1178 return -EHOSTUNREACH;
1179
1180 iface = usb_ifnum_to_if(dev, interface);
1181 if (!iface) {
1182 dev_dbg(&dev->dev, "selecting invalid interface %d\n",
1183 interface);
1184 return -EINVAL;
1185 }
1186
1187 alt = usb_altnum_to_altsetting(iface, alternate);
1188 if (!alt) {
1189 warn("selecting invalid altsetting %d", alternate);
1190 return -EINVAL;
1191 }
1192
1193 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1194 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1195 alternate, interface, NULL, 0, 5000);
1196
1197 /* 9.4.10 says devices don't need this and are free to STALL the
1198 * request if the interface only has one alternate setting.
1199 */
1200 if (ret == -EPIPE && iface->num_altsetting == 1) {
1201 dev_dbg(&dev->dev,
1202 "manual set_interface for iface %d, alt %d\n",
1203 interface, alternate);
1204 manual = 1;
1205 } else if (ret < 0)
1206 return ret;
1207
1208 /* FIXME drivers shouldn't need to replicate/bugfix the logic here
1209 * when they implement async or easily-killable versions of this or
1210 * other "should-be-internal" functions (like clear_halt).
1211 * should hcd+usbcore postprocess control requests?
1212 */
1213
1214 /* prevent submissions using previous endpoint settings */
Alan Stern0e6c8e82005-10-24 15:33:03 -04001215 if (device_is_registered(&iface->dev))
1216 usb_remove_sysfs_intf_files(iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 usb_disable_interface(dev, iface);
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 iface->cur_altsetting = alt;
1220
1221 /* If the interface only has one altsetting and the device didn't
David Brownella81e7ec2005-04-18 17:39:25 -07001222 * accept the request, we attempt to carry out the equivalent action
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 * by manually clearing the HALT feature for each endpoint in the
1224 * new altsetting.
1225 */
1226 if (manual) {
1227 int i;
1228
1229 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
1230 unsigned int epaddr =
1231 alt->endpoint[i].desc.bEndpointAddress;
1232 unsigned int pipe =
1233 __create_pipe(dev, USB_ENDPOINT_NUMBER_MASK & epaddr)
1234 | (usb_endpoint_out(epaddr) ? USB_DIR_OUT : USB_DIR_IN);
1235
1236 usb_clear_halt(dev, pipe);
1237 }
1238 }
1239
1240 /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
1241 *
1242 * Note:
1243 * Despite EP0 is always present in all interfaces/AS, the list of
1244 * endpoints from the descriptor does not contain EP0. Due to its
1245 * omnipresence one might expect EP0 being considered "affected" by
1246 * any SetInterface request and hence assume toggles need to be reset.
1247 * However, EP0 toggles are re-synced for every individual transfer
1248 * during the SETUP stage - hence EP0 toggles are "don't care" here.
1249 * (Likewise, EP0 never "halts" on well designed devices.)
1250 */
1251 usb_enable_interface(dev, iface);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001252 if (device_is_registered(&iface->dev))
1253 usb_create_sysfs_intf_files(iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 return 0;
1256}
1257
1258/**
1259 * usb_reset_configuration - lightweight device reset
1260 * @dev: the device whose configuration is being reset
1261 *
1262 * This issues a standard SET_CONFIGURATION request to the device using
1263 * the current configuration. The effect is to reset most USB-related
1264 * state in the device, including interface altsettings (reset to zero),
1265 * endpoint halts (cleared), and data toggle (only for bulk and interrupt
1266 * endpoints). Other usbcore state is unchanged, including bindings of
1267 * usb device drivers to interfaces.
1268 *
1269 * Because this affects multiple interfaces, avoid using this with composite
1270 * (multi-interface) devices. Instead, the driver for each interface may
David Brownella81e7ec2005-04-18 17:39:25 -07001271 * use usb_set_interface() on the interfaces it claims. Be careful though;
1272 * some devices don't support the SET_INTERFACE request, and others won't
1273 * reset all the interface state (notably data toggles). Resetting the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 * configuration would affect other drivers' interfaces.
1275 *
1276 * The caller must own the device lock.
1277 *
1278 * Returns zero on success, else a negative error code.
1279 */
1280int usb_reset_configuration(struct usb_device *dev)
1281{
1282 int i, retval;
1283 struct usb_host_config *config;
1284
1285 if (dev->state == USB_STATE_SUSPENDED)
1286 return -EHOSTUNREACH;
1287
1288 /* caller must have locked the device and must own
1289 * the usb bus readlock (so driver bindings are stable);
1290 * calls during probe() are fine
1291 */
1292
1293 for (i = 1; i < 16; ++i) {
1294 usb_disable_endpoint(dev, i);
1295 usb_disable_endpoint(dev, i + USB_DIR_IN);
1296 }
1297
1298 config = dev->actconfig;
1299 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1300 USB_REQ_SET_CONFIGURATION, 0,
1301 config->desc.bConfigurationValue, 0,
1302 NULL, 0, USB_CTRL_SET_TIMEOUT);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001303 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 dev->toggle[0] = dev->toggle[1] = 0;
1307
1308 /* re-init hc/hcd interface/endpoint state */
1309 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1310 struct usb_interface *intf = config->interface[i];
1311 struct usb_host_interface *alt;
1312
Alan Stern0e6c8e82005-10-24 15:33:03 -04001313 if (device_is_registered(&intf->dev))
1314 usb_remove_sysfs_intf_files(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 alt = usb_altnum_to_altsetting(intf, 0);
1316
1317 /* No altsetting 0? We'll assume the first altsetting.
1318 * We could use a GetInterface call, but if a device is
1319 * so non-compliant that it doesn't have altsetting 0
1320 * then I wouldn't trust its reply anyway.
1321 */
1322 if (!alt)
1323 alt = &intf->altsetting[0];
1324
1325 intf->cur_altsetting = alt;
1326 usb_enable_interface(dev, intf);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001327 if (device_is_registered(&intf->dev))
1328 usb_create_sysfs_intf_files(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
1330 return 0;
1331}
1332
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001333void usb_release_interface(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 struct usb_interface *intf = to_usb_interface(dev);
1336 struct usb_interface_cache *intfc =
1337 altsetting_to_usb_interface_cache(intf->altsetting);
1338
1339 kref_put(&intfc->ref, usb_release_interface_cache);
1340 kfree(intf);
1341}
1342
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001343#ifdef CONFIG_HOTPLUG
1344static int usb_if_uevent(struct device *dev, char **envp, int num_envp,
1345 char *buffer, int buffer_size)
1346{
1347 struct usb_device *usb_dev;
1348 struct usb_interface *intf;
1349 struct usb_host_interface *alt;
1350 int i = 0;
1351 int length = 0;
1352
1353 if (!dev)
1354 return -ENODEV;
1355
1356 /* driver is often null here; dev_dbg() would oops */
1357 pr_debug ("usb %s: uevent\n", dev->bus_id);
1358
1359 intf = to_usb_interface(dev);
1360 usb_dev = interface_to_usbdev(intf);
1361 alt = intf->cur_altsetting;
1362
Kay Sieversd65cc1b2007-08-21 13:41:08 +02001363#ifdef CONFIG_USB_DEVICEFS
1364 if (add_uevent_var(envp, num_envp, &i,
1365 buffer, buffer_size, &length,
1366 "DEVICE=/proc/bus/usb/%03d/%03d",
1367 usb_dev->bus->busnum, usb_dev->devnum))
1368 return -ENOMEM;
1369#endif
1370
1371 if (add_uevent_var(envp, num_envp, &i,
1372 buffer, buffer_size, &length,
1373 "PRODUCT=%x/%x/%x",
1374 le16_to_cpu(usb_dev->descriptor.idVendor),
1375 le16_to_cpu(usb_dev->descriptor.idProduct),
1376 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
1377 return -ENOMEM;
1378
1379 if (add_uevent_var(envp, num_envp, &i,
1380 buffer, buffer_size, &length,
1381 "TYPE=%d/%d/%d",
1382 usb_dev->descriptor.bDeviceClass,
1383 usb_dev->descriptor.bDeviceSubClass,
1384 usb_dev->descriptor.bDeviceProtocol))
1385 return -ENOMEM;
1386
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001387 if (add_uevent_var(envp, num_envp, &i,
1388 buffer, buffer_size, &length,
1389 "INTERFACE=%d/%d/%d",
1390 alt->desc.bInterfaceClass,
1391 alt->desc.bInterfaceSubClass,
1392 alt->desc.bInterfaceProtocol))
1393 return -ENOMEM;
1394
1395 if (add_uevent_var(envp, num_envp, &i,
1396 buffer, buffer_size, &length,
1397 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
1398 le16_to_cpu(usb_dev->descriptor.idVendor),
1399 le16_to_cpu(usb_dev->descriptor.idProduct),
1400 le16_to_cpu(usb_dev->descriptor.bcdDevice),
1401 usb_dev->descriptor.bDeviceClass,
1402 usb_dev->descriptor.bDeviceSubClass,
1403 usb_dev->descriptor.bDeviceProtocol,
1404 alt->desc.bInterfaceClass,
1405 alt->desc.bInterfaceSubClass,
1406 alt->desc.bInterfaceProtocol))
1407 return -ENOMEM;
1408
1409 envp[i] = NULL;
1410 return 0;
1411}
1412
1413#else
1414
1415static int usb_if_uevent(struct device *dev, char **envp,
1416 int num_envp, char *buffer, int buffer_size)
1417{
1418 return -ENODEV;
1419}
1420#endif /* CONFIG_HOTPLUG */
1421
1422struct device_type usb_if_device_type = {
1423 .name = "usb_interface",
1424 .release = usb_release_interface,
1425 .uevent = usb_if_uevent,
1426};
1427
Craig W. Nadler165fe972007-06-15 23:14:35 -04001428static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,
1429 struct usb_host_config *config,
1430 u8 inum)
1431{
1432 struct usb_interface_assoc_descriptor *retval = NULL;
1433 struct usb_interface_assoc_descriptor *intf_assoc;
1434 int first_intf;
1435 int last_intf;
1436 int i;
1437
1438 for (i = 0; (i < USB_MAXIADS && config->intf_assoc[i]); i++) {
1439 intf_assoc = config->intf_assoc[i];
1440 if (intf_assoc->bInterfaceCount == 0)
1441 continue;
1442
1443 first_intf = intf_assoc->bFirstInterface;
1444 last_intf = first_intf + (intf_assoc->bInterfaceCount - 1);
1445 if (inum >= first_intf && inum <= last_intf) {
1446 if (!retval)
1447 retval = intf_assoc;
1448 else
1449 dev_err(&dev->dev, "Interface #%d referenced"
1450 " by multiple IADs\n", inum);
1451 }
1452 }
1453
1454 return retval;
1455}
1456
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458/*
1459 * usb_set_configuration - Makes a particular device setting be current
1460 * @dev: the device whose configuration is being updated
1461 * @configuration: the configuration being chosen.
1462 * Context: !in_interrupt(), caller owns the device lock
1463 *
1464 * This is used to enable non-default device modes. Not all devices
1465 * use this kind of configurability; many devices only have one
1466 * configuration.
1467 *
Alan Stern3f141e22007-02-08 16:40:43 -05001468 * @configuration is the value of the configuration to be installed.
1469 * According to the USB spec (e.g. section 9.1.1.5), configuration values
1470 * must be non-zero; a value of zero indicates that the device in
1471 * unconfigured. However some devices erroneously use 0 as one of their
1472 * configuration values. To help manage such devices, this routine will
1473 * accept @configuration = -1 as indicating the device should be put in
1474 * an unconfigured state.
1475 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 * USB device configurations may affect Linux interoperability,
1477 * power consumption and the functionality available. For example,
1478 * the default configuration is limited to using 100mA of bus power,
1479 * so that when certain device functionality requires more power,
1480 * and the device is bus powered, that functionality should be in some
1481 * non-default device configuration. Other device modes may also be
1482 * reflected as configuration options, such as whether two ISDN
1483 * channels are available independently; and choosing between open
1484 * standard device protocols (like CDC) or proprietary ones.
1485 *
Inaky Perez-Gonzalez16bbab22007-07-31 20:34:01 -07001486 * Note that a non-authorized device (dev->authorized == 0) will only
1487 * be put in unconfigured mode.
1488 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 * Note that USB has an additional level of device configurability,
1490 * associated with interfaces. That configurability is accessed using
1491 * usb_set_interface().
1492 *
1493 * This call is synchronous. The calling context must be able to sleep,
1494 * must own the device lock, and must not hold the driver model's USB
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -04001495 * bus mutex; usb device driver probe() methods cannot use this routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 *
1497 * Returns zero on success, or else the status code returned by the
Steven Cole093cf722005-05-03 19:07:24 -06001498 * underlying call that failed. On successful completion, each interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 * in the original device configuration has been destroyed, and each one
1500 * in the new configuration has been probed by all relevant usb device
1501 * drivers currently known to the kernel.
1502 */
1503int usb_set_configuration(struct usb_device *dev, int configuration)
1504{
1505 int i, ret;
1506 struct usb_host_config *cp = NULL;
1507 struct usb_interface **new_interfaces = NULL;
1508 int n, nintf;
1509
Inaky Perez-Gonzalez16bbab22007-07-31 20:34:01 -07001510 if (dev->authorized == 0 || configuration == -1)
Alan Stern3f141e22007-02-08 16:40:43 -05001511 configuration = 0;
1512 else {
1513 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1514 if (dev->config[i].desc.bConfigurationValue ==
1515 configuration) {
1516 cp = &dev->config[i];
1517 break;
1518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 }
1520 }
1521 if ((!cp && configuration != 0))
1522 return -EINVAL;
1523
1524 /* The USB spec says configuration 0 means unconfigured.
1525 * But if a device includes a configuration numbered 0,
1526 * we will accept it as a correctly configured state.
Alan Stern3f141e22007-02-08 16:40:43 -05001527 * Use -1 if you really want to unconfigure the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 */
1529 if (cp && configuration == 0)
1530 dev_warn(&dev->dev, "config 0 descriptor??\n");
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 /* Allocate memory for new interfaces before doing anything else,
1533 * so that if we run out then nothing will have changed. */
1534 n = nintf = 0;
1535 if (cp) {
1536 nintf = cp->desc.bNumInterfaces;
1537 new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
1538 GFP_KERNEL);
1539 if (!new_interfaces) {
1540 dev_err(&dev->dev, "Out of memory");
1541 return -ENOMEM;
1542 }
1543
1544 for (; n < nintf; ++n) {
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001545 new_interfaces[n] = kzalloc(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 sizeof(struct usb_interface),
1547 GFP_KERNEL);
1548 if (!new_interfaces[n]) {
1549 dev_err(&dev->dev, "Out of memory");
1550 ret = -ENOMEM;
1551free_interfaces:
1552 while (--n >= 0)
1553 kfree(new_interfaces[n]);
1554 kfree(new_interfaces);
1555 return ret;
1556 }
1557 }
Alan Stern6ad07122006-06-01 13:59:16 -04001558
1559 i = dev->bus_mA - cp->desc.bMaxPower * 2;
1560 if (i < 0)
1561 dev_warn(&dev->dev, "new config #%d exceeds power "
1562 "limit by %dmA\n",
1563 configuration, -i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
1565
Alan Stern01d883d2006-08-30 15:47:18 -04001566 /* Wake up the device so we can send it the Set-Config request */
Alan Stern94fcda12006-11-20 11:38:46 -05001567 ret = usb_autoresume_device(dev);
Alan Stern01d883d2006-08-30 15:47:18 -04001568 if (ret)
1569 goto free_interfaces;
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 /* if it's already configured, clear out old state first.
1572 * getting rid of old interfaces means unbinding their drivers.
1573 */
1574 if (dev->state != USB_STATE_ADDRESS)
1575 usb_disable_device (dev, 1); // Skip ep0
1576
1577 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1578 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
Alan Stern6ad07122006-06-01 13:59:16 -04001579 NULL, 0, USB_CTRL_SET_TIMEOUT)) < 0) {
1580
1581 /* All the old state is gone, so what else can we do?
1582 * The device is probably useless now anyway.
1583 */
1584 cp = NULL;
1585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 dev->actconfig = cp;
Alan Stern6ad07122006-06-01 13:59:16 -04001588 if (!cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 usb_set_device_state(dev, USB_STATE_ADDRESS);
Alan Stern94fcda12006-11-20 11:38:46 -05001590 usb_autosuspend_device(dev);
Alan Stern6ad07122006-06-01 13:59:16 -04001591 goto free_interfaces;
1592 }
1593 usb_set_device_state(dev, USB_STATE_CONFIGURED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Alan Stern6ad07122006-06-01 13:59:16 -04001595 /* Initialize the new interface structures and the
1596 * hc/hcd/usbcore interface/endpoint state.
1597 */
1598 for (i = 0; i < nintf; ++i) {
1599 struct usb_interface_cache *intfc;
1600 struct usb_interface *intf;
1601 struct usb_host_interface *alt;
1602
1603 cp->interface[i] = intf = new_interfaces[i];
1604 intfc = cp->intf_cache[i];
1605 intf->altsetting = intfc->altsetting;
1606 intf->num_altsetting = intfc->num_altsetting;
Craig W. Nadler165fe972007-06-15 23:14:35 -04001607 intf->intf_assoc = find_iad(dev, cp, i);
Alan Stern6ad07122006-06-01 13:59:16 -04001608 kref_get(&intfc->ref);
1609
1610 alt = usb_altnum_to_altsetting(intf, 0);
1611
1612 /* No altsetting 0? We'll assume the first altsetting.
1613 * We could use a GetInterface call, but if a device is
1614 * so non-compliant that it doesn't have altsetting 0
1615 * then I wouldn't trust its reply anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 */
Alan Stern6ad07122006-06-01 13:59:16 -04001617 if (!alt)
1618 alt = &intf->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Alan Stern6ad07122006-06-01 13:59:16 -04001620 intf->cur_altsetting = alt;
1621 usb_enable_interface(dev, intf);
1622 intf->dev.parent = &dev->dev;
1623 intf->dev.driver = NULL;
1624 intf->dev.bus = &usb_bus_type;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001625 intf->dev.type = &usb_if_device_type;
Alan Stern6ad07122006-06-01 13:59:16 -04001626 intf->dev.dma_mask = dev->dev.dma_mask;
Alan Stern6ad07122006-06-01 13:59:16 -04001627 device_initialize (&intf->dev);
1628 mark_quiesced(intf);
1629 sprintf (&intf->dev.bus_id[0], "%d-%s:%d.%d",
1630 dev->bus->busnum, dev->devpath,
1631 configuration, alt->desc.bInterfaceNumber);
1632 }
1633 kfree(new_interfaces);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Alan Stern6ad07122006-06-01 13:59:16 -04001635 if (cp->string == NULL)
1636 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Alan Stern6ad07122006-06-01 13:59:16 -04001638 /* Now that all the interfaces are set up, register them
1639 * to trigger binding of drivers to interfaces. probe()
1640 * routines may install different altsettings and may
1641 * claim() any interfaces not yet bound. Many class drivers
1642 * need that: CDC, audio, video, etc.
1643 */
1644 for (i = 0; i < nintf; ++i) {
1645 struct usb_interface *intf = cp->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Alan Stern6ad07122006-06-01 13:59:16 -04001647 dev_dbg (&dev->dev,
1648 "adding %s (config #%d, interface %d)\n",
1649 intf->dev.bus_id, configuration,
1650 intf->cur_altsetting->desc.bInterfaceNumber);
1651 ret = device_add (&intf->dev);
1652 if (ret != 0) {
1653 dev_err(&dev->dev, "device_add(%s) --> %d\n",
1654 intf->dev.bus_id, ret);
1655 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
Alan Stern6ad07122006-06-01 13:59:16 -04001657 usb_create_sysfs_intf_files (intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
1659
Alan Stern94fcda12006-11-20 11:38:46 -05001660 usb_autosuspend_device(dev);
Alan Stern86d30742005-07-29 12:17:16 -07001661 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662}
1663
Alan Stern088dc272006-08-21 12:08:19 -04001664struct set_config_request {
1665 struct usb_device *udev;
1666 int config;
1667 struct work_struct work;
1668};
1669
1670/* Worker routine for usb_driver_set_configuration() */
David Howellsc4028952006-11-22 14:57:56 +00001671static void driver_set_config_work(struct work_struct *work)
Alan Stern088dc272006-08-21 12:08:19 -04001672{
David Howellsc4028952006-11-22 14:57:56 +00001673 struct set_config_request *req =
1674 container_of(work, struct set_config_request, work);
Alan Stern088dc272006-08-21 12:08:19 -04001675
1676 usb_lock_device(req->udev);
1677 usb_set_configuration(req->udev, req->config);
1678 usb_unlock_device(req->udev);
1679 usb_put_dev(req->udev);
1680 kfree(req);
1681}
1682
1683/**
1684 * usb_driver_set_configuration - Provide a way for drivers to change device configurations
1685 * @udev: the device whose configuration is being updated
1686 * @config: the configuration being chosen.
1687 * Context: In process context, must be able to sleep
1688 *
1689 * Device interface drivers are not allowed to change device configurations.
1690 * This is because changing configurations will destroy the interface the
1691 * driver is bound to and create new ones; it would be like a floppy-disk
1692 * driver telling the computer to replace the floppy-disk drive with a
1693 * tape drive!
1694 *
1695 * Still, in certain specialized circumstances the need may arise. This
1696 * routine gets around the normal restrictions by using a work thread to
1697 * submit the change-config request.
1698 *
1699 * Returns 0 if the request was succesfully queued, error code otherwise.
1700 * The caller has no way to know whether the queued request will eventually
1701 * succeed.
1702 */
1703int usb_driver_set_configuration(struct usb_device *udev, int config)
1704{
1705 struct set_config_request *req;
1706
1707 req = kmalloc(sizeof(*req), GFP_KERNEL);
1708 if (!req)
1709 return -ENOMEM;
1710 req->udev = udev;
1711 req->config = config;
David Howellsc4028952006-11-22 14:57:56 +00001712 INIT_WORK(&req->work, driver_set_config_work);
Alan Stern088dc272006-08-21 12:08:19 -04001713
1714 usb_get_dev(udev);
Alan Stern1737bf22006-12-15 16:04:52 -05001715 schedule_work(&req->work);
Alan Stern088dc272006-08-21 12:08:19 -04001716 return 0;
1717}
1718EXPORT_SYMBOL_GPL(usb_driver_set_configuration);
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720// synchronous request completion model
1721EXPORT_SYMBOL(usb_control_msg);
1722EXPORT_SYMBOL(usb_bulk_msg);
1723
1724EXPORT_SYMBOL(usb_sg_init);
1725EXPORT_SYMBOL(usb_sg_cancel);
1726EXPORT_SYMBOL(usb_sg_wait);
1727
1728// synchronous control message convenience routines
1729EXPORT_SYMBOL(usb_get_descriptor);
1730EXPORT_SYMBOL(usb_get_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731EXPORT_SYMBOL(usb_string);
1732
1733// synchronous calls that also maintain usbcore state
1734EXPORT_SYMBOL(usb_clear_halt);
1735EXPORT_SYMBOL(usb_reset_configuration);
1736EXPORT_SYMBOL(usb_set_interface);
1737