blob: ad4b956380d25900c19d19d3c89768318426bfe9 [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
David Howells7d12e782006-10-05 14:55:46 +010021static void usb_api_blocking_completion(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
23 complete((struct completion *)urb->context);
24}
25
26
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020027/*
28 * Starts urb and waits for completion or timeout. Note that this call
29 * is NOT interruptible. Many device driver i/o requests should be
30 * interruptible and therefore these drivers should implement their
31 * own interruptible routines.
32 */
33static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020035 struct completion done;
36 unsigned long expire;
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -070037 int retval;
38 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40 init_completion(&done);
41 urb->context = &done;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 urb->actual_length = 0;
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -070043 retval = usb_submit_urb(urb, GFP_NOIO);
44 if (unlikely(retval))
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020045 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020047 expire = timeout ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT;
48 if (!wait_for_completion_timeout(&done, expire)) {
49
50 dev_dbg(&urb->dev->dev,
51 "%s timed out on ep%d%s len=%d/%d\n",
52 current->comm,
53 usb_pipeendpoint(urb->pipe),
54 usb_pipein(urb->pipe) ? "in" : "out",
55 urb->actual_length,
56 urb->transfer_buffer_length);
57
58 usb_kill_urb(urb);
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -070059 retval = status == -ENOENT ? -ETIMEDOUT : status;
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020060 } else
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -070061 retval = status;
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020062out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (actual_length)
64 *actual_length = urb->actual_length;
Franck Bui-Huuecdc0a52006-07-12 10:09:41 +020065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 usb_free_urb(urb);
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -070067 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
70/*-------------------------------------------------------------------*/
71// returns status (negative) or length (positive)
72static int usb_internal_control_msg(struct usb_device *usb_dev,
73 unsigned int pipe,
74 struct usb_ctrlrequest *cmd,
75 void *data, int len, int timeout)
76{
77 struct urb *urb;
78 int retv;
79 int length;
80
81 urb = usb_alloc_urb(0, GFP_NOIO);
82 if (!urb)
83 return -ENOMEM;
84
85 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char *)cmd, data,
86 len, usb_api_blocking_completion, NULL);
87
88 retv = usb_start_wait_urb(urb, timeout, &length);
89 if (retv < 0)
90 return retv;
91 else
92 return length;
93}
94
95/**
96 * usb_control_msg - Builds a control urb, sends it off and waits for completion
97 * @dev: pointer to the usb device to send the message to
98 * @pipe: endpoint "pipe" to send the message to
99 * @request: USB message request value
100 * @requesttype: USB message request type value
101 * @value: USB message value
102 * @index: USB message index value
103 * @data: pointer to the data to send
104 * @size: length in bytes of the data to send
105 * @timeout: time in msecs to wait for the message to complete before
106 * timing out (if 0 the wait is forever)
107 * Context: !in_interrupt ()
108 *
109 * This function sends a simple control message to a specified endpoint
110 * and waits for the message to complete, or timeout.
111 *
112 * If successful, it returns the number of bytes transferred, otherwise a negative error number.
113 *
114 * Don't use this function from within an interrupt context, like a
115 * bottom half handler. If you need an asynchronous message, or need to send
116 * a message from within interrupt context, use usb_submit_urb()
117 * If a thread in your driver uses this call, make sure your disconnect()
118 * method can wait for it to complete. Since you don't have a handle on
119 * the URB used, you can't cancel the request.
120 */
121int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request, __u8 requesttype,
122 __u16 value, __u16 index, void *data, __u16 size, int timeout)
123{
124 struct usb_ctrlrequest *dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
125 int ret;
126
127 if (!dr)
128 return -ENOMEM;
129
130 dr->bRequestType= requesttype;
131 dr->bRequest = request;
132 dr->wValue = cpu_to_le16p(&value);
133 dr->wIndex = cpu_to_le16p(&index);
134 dr->wLength = cpu_to_le16p(&size);
135
136 //dbg("usb_control_msg");
137
138 ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);
139
140 kfree(dr);
141
142 return ret;
143}
144
145
146/**
Greg Kroah-Hartman782a7a62006-05-19 13:20:20 -0700147 * usb_interrupt_msg - Builds an interrupt urb, sends it off and waits for completion
148 * @usb_dev: pointer to the usb device to send the message to
149 * @pipe: endpoint "pipe" to send the message to
150 * @data: pointer to the data to send
151 * @len: length in bytes of the data to send
152 * @actual_length: pointer to a location to put the actual length transferred in bytes
153 * @timeout: time in msecs to wait for the message to complete before
154 * timing out (if 0 the wait is forever)
155 * Context: !in_interrupt ()
156 *
157 * This function sends a simple interrupt message to a specified endpoint and
158 * waits for the message to complete, or timeout.
159 *
160 * If successful, it returns 0, otherwise a negative error number. The number
161 * of actual bytes transferred will be stored in the actual_length paramater.
162 *
163 * Don't use this function from within an interrupt context, like a bottom half
164 * handler. If you need an asynchronous message, or need to send a message
165 * from within interrupt context, use usb_submit_urb() If a thread in your
166 * driver uses this call, make sure your disconnect() method can wait for it to
167 * complete. Since you don't have a handle on the URB used, you can't cancel
168 * the request.
169 */
170int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe,
171 void *data, int len, int *actual_length, int timeout)
172{
173 return usb_bulk_msg(usb_dev, pipe, data, len, actual_length, timeout);
174}
175EXPORT_SYMBOL_GPL(usb_interrupt_msg);
176
177/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion
179 * @usb_dev: pointer to the usb device to send the message to
180 * @pipe: endpoint "pipe" to send the message to
181 * @data: pointer to the data to send
182 * @len: length in bytes of the data to send
183 * @actual_length: pointer to a location to put the actual length transferred in bytes
184 * @timeout: time in msecs to wait for the message to complete before
185 * timing out (if 0 the wait is forever)
186 * Context: !in_interrupt ()
187 *
188 * This function sends a simple bulk message to a specified endpoint
189 * and waits for the message to complete, or timeout.
190 *
191 * If successful, it returns 0, otherwise a negative error number.
192 * The number of actual bytes transferred will be stored in the
193 * actual_length paramater.
194 *
195 * Don't use this function from within an interrupt context, like a
196 * bottom half handler. If you need an asynchronous message, or need to
197 * send a message from within interrupt context, use usb_submit_urb()
198 * If a thread in your driver uses this call, make sure your disconnect()
199 * method can wait for it to complete. Since you don't have a handle on
200 * the URB used, you can't cancel the request.
Alan Sternd09d36a2005-09-26 16:22:45 -0400201 *
202 * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT
203 * ioctl, users are forced to abuse this routine by using it to submit
204 * URBs for interrupt endpoints. We will take the liberty of creating
205 * an interrupt URB (with the default interval) if the target is an
206 * interrupt endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 */
208int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
209 void *data, int len, int *actual_length, int timeout)
210{
211 struct urb *urb;
Alan Sternd09d36a2005-09-26 16:22:45 -0400212 struct usb_host_endpoint *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Alan Sternd09d36a2005-09-26 16:22:45 -0400214 ep = (usb_pipein(pipe) ? usb_dev->ep_in : usb_dev->ep_out)
215 [usb_pipeendpoint(pipe)];
216 if (!ep || len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return -EINVAL;
218
Alan Sternd09d36a2005-09-26 16:22:45 -0400219 urb = usb_alloc_urb(0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (!urb)
221 return -ENOMEM;
222
Alan Sternd09d36a2005-09-26 16:22:45 -0400223 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
224 USB_ENDPOINT_XFER_INT) {
225 pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
226 usb_fill_int_urb(urb, usb_dev, pipe, data, len,
Alan Stern8d062b92007-04-23 17:30:32 -0400227 usb_api_blocking_completion, NULL,
228 ep->desc.bInterval);
Alan Sternd09d36a2005-09-26 16:22:45 -0400229 } else
230 usb_fill_bulk_urb(urb, usb_dev, pipe, data, len,
231 usb_api_blocking_completion, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 return usb_start_wait_urb(urb, timeout, actual_length);
234}
235
236/*-------------------------------------------------------------------*/
237
238static void sg_clean (struct usb_sg_request *io)
239{
240 if (io->urbs) {
241 while (io->entries--)
242 usb_free_urb (io->urbs [io->entries]);
243 kfree (io->urbs);
244 io->urbs = NULL;
245 }
246 if (io->dev->dev.dma_mask != NULL)
247 usb_buffer_unmap_sg (io->dev, io->pipe, io->sg, io->nents);
248 io->dev = NULL;
249}
250
David Howells7d12e782006-10-05 14:55:46 +0100251static void sg_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200253 struct usb_sg_request *io = urb->context;
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700254 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 spin_lock (&io->lock);
257
258 /* In 2.5 we require hcds' endpoint queues not to progress after fault
259 * reports, until the completion callback (this!) returns. That lets
260 * device driver code (like this routine) unlink queued urbs first,
261 * if it needs to, since the HC won't work on them at all. So it's
262 * not possible for page N+1 to overwrite page N, and so on.
263 *
264 * That's only for "hard" faults; "soft" faults (unlinks) sometimes
265 * complete before the HCD can get requests away from hardware,
266 * though never during cleanup after a hard fault.
267 */
268 if (io->status
269 && (io->status != -ECONNRESET
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700270 || status != -ECONNRESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 && urb->actual_length) {
272 dev_err (io->dev->bus->controller,
273 "dev %s ep%d%s scatterlist error %d/%d\n",
274 io->dev->devpath,
275 usb_pipeendpoint (urb->pipe),
276 usb_pipein (urb->pipe) ? "in" : "out",
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700277 status, io->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 // BUG ();
279 }
280
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700281 if (io->status == 0 && status && status != -ECONNRESET) {
282 int i, found, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700284 io->status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /* the previous urbs, and this one, completed already.
287 * unlink pending urbs so they won't rx/tx bad data.
288 * careful: unlink can sometimes be synchronous...
289 */
290 spin_unlock (&io->lock);
291 for (i = 0, found = 0; i < io->entries; i++) {
292 if (!io->urbs [i] || !io->urbs [i]->dev)
293 continue;
294 if (found) {
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700295 retval = usb_unlink_urb (io->urbs [i]);
296 if (retval != -EINPROGRESS &&
297 retval != -ENODEV &&
298 retval != -EBUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 dev_err (&io->dev->dev,
300 "%s, unlink --> %d\n",
Greg Kroah-Hartman3fc3e822007-07-18 10:58:02 -0700301 __FUNCTION__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 } else if (urb == io->urbs [i])
303 found = 1;
304 }
305 spin_lock (&io->lock);
306 }
307 urb->dev = NULL;
308
309 /* on the last completion, signal usb_sg_wait() */
310 io->bytes += urb->actual_length;
311 io->count--;
312 if (!io->count)
313 complete (&io->complete);
314
315 spin_unlock (&io->lock);
316}
317
318
319/**
320 * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request
321 * @io: request block being initialized. until usb_sg_wait() returns,
322 * treat this as a pointer to an opaque block of memory,
323 * @dev: the usb device that will send or receive the data
324 * @pipe: endpoint "pipe" used to transfer the data
325 * @period: polling rate for interrupt endpoints, in frames or
326 * (for high speed endpoints) microframes; ignored for bulk
327 * @sg: scatterlist entries
328 * @nents: how many entries in the scatterlist
329 * @length: how many bytes to send from the scatterlist, or zero to
330 * send every byte identified in the list.
331 * @mem_flags: SLAB_* flags affecting memory allocations in this call
332 *
333 * Returns zero for success, else a negative errno value. This initializes a
334 * scatter/gather request, allocating resources such as I/O mappings and urb
335 * memory (except maybe memory used by USB controller drivers).
336 *
337 * The request must be issued using usb_sg_wait(), which waits for the I/O to
338 * complete (or to be canceled) and then cleans up all resources allocated by
339 * usb_sg_init().
340 *
341 * The request may be canceled with usb_sg_cancel(), either before or after
342 * usb_sg_wait() is called.
343 */
344int usb_sg_init (
345 struct usb_sg_request *io,
346 struct usb_device *dev,
347 unsigned pipe,
348 unsigned period,
349 struct scatterlist *sg,
350 int nents,
351 size_t length,
Al Viro55016f12005-10-21 03:21:58 -0400352 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353)
354{
355 int i;
356 int urb_flags;
357 int dma;
358
359 if (!io || !dev || !sg
360 || usb_pipecontrol (pipe)
361 || usb_pipeisoc (pipe)
362 || nents <= 0)
363 return -EINVAL;
364
365 spin_lock_init (&io->lock);
366 io->dev = dev;
367 io->pipe = pipe;
368 io->sg = sg;
369 io->nents = nents;
370
371 /* not all host controllers use DMA (like the mainstream pci ones);
372 * they can use PIO (sl811) or be software over another transport.
373 */
374 dma = (dev->dev.dma_mask != NULL);
375 if (dma)
376 io->entries = usb_buffer_map_sg (dev, pipe, sg, nents);
377 else
378 io->entries = nents;
379
380 /* initialize all the urbs we'll use */
381 if (io->entries <= 0)
382 return io->entries;
383
384 io->count = io->entries;
385 io->urbs = kmalloc (io->entries * sizeof *io->urbs, mem_flags);
386 if (!io->urbs)
387 goto nomem;
388
Alan Sternb375a042005-07-29 16:11:07 -0400389 urb_flags = URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (usb_pipein (pipe))
391 urb_flags |= URB_SHORT_NOT_OK;
392
393 for (i = 0; i < io->entries; i++) {
394 unsigned len;
395
396 io->urbs [i] = usb_alloc_urb (0, mem_flags);
397 if (!io->urbs [i]) {
398 io->entries = i;
399 goto nomem;
400 }
401
402 io->urbs [i]->dev = NULL;
403 io->urbs [i]->pipe = pipe;
404 io->urbs [i]->interval = period;
405 io->urbs [i]->transfer_flags = urb_flags;
406
407 io->urbs [i]->complete = sg_complete;
408 io->urbs [i]->context = io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Tony Lindgren35d07fd2007-03-31 18:15:43 -0700410 /*
411 * Some systems need to revert to PIO when DMA is temporarily
412 * unavailable. For their sakes, both transfer_buffer and
413 * transfer_dma are set when possible. However this can only
David Brownella12b8db2007-07-22 15:13:13 -0700414 * work on systems without:
415 *
416 * - HIGHMEM, since DMA buffers located in high memory are
417 * not directly addressable by the CPU for PIO;
418 *
419 * - IOMMU, since dma_map_sg() is allowed to use an IOMMU to
420 * make virtually discontiguous buffers be "dma-contiguous"
421 * so that PIO and DMA need diferent numbers of URBs.
422 *
423 * So when HIGHMEM or IOMMU are in use, transfer_buffer is NULL
Tony Lindgren35d07fd2007-03-31 18:15:43 -0700424 * to prevent stale pointers and to help spot bugs.
425 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 io->urbs [i]->transfer_dma = sg_dma_address (sg + i);
428 len = sg_dma_len (sg + i);
David Brownella12b8db2007-07-22 15:13:13 -0700429#if defined(CONFIG_HIGHMEM) || defined(CONFIG_IOMMU)
Tony Lindgren35d07fd2007-03-31 18:15:43 -0700430 io->urbs[i]->transfer_buffer = NULL;
431#else
432 io->urbs[i]->transfer_buffer =
433 page_address(sg[i].page) + sg[i].offset;
434#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 } else {
436 /* hc may use _only_ transfer_buffer */
437 io->urbs [i]->transfer_buffer =
438 page_address (sg [i].page) + sg [i].offset;
439 len = sg [i].length;
440 }
441
442 if (length) {
443 len = min_t (unsigned, len, length);
444 length -= len;
445 if (length == 0)
446 io->entries = i + 1;
447 }
448 io->urbs [i]->transfer_buffer_length = len;
449 }
450 io->urbs [--i]->transfer_flags &= ~URB_NO_INTERRUPT;
451
452 /* transaction state */
453 io->status = 0;
454 io->bytes = 0;
455 init_completion (&io->complete);
456 return 0;
457
458nomem:
459 sg_clean (io);
460 return -ENOMEM;
461}
462
463
464/**
465 * usb_sg_wait - synchronously execute scatter/gather request
466 * @io: request block handle, as initialized with usb_sg_init().
467 * some fields become accessible when this call returns.
468 * Context: !in_interrupt ()
469 *
470 * This function blocks until the specified I/O operation completes. It
471 * leverages the grouping of the related I/O requests to get good transfer
472 * rates, by queueing the requests. At higher speeds, such queuing can
473 * significantly improve USB throughput.
474 *
475 * There are three kinds of completion for this function.
476 * (1) success, where io->status is zero. The number of io->bytes
477 * transferred is as requested.
478 * (2) error, where io->status is a negative errno value. The number
479 * of io->bytes transferred before the error is usually less
480 * than requested, and can be nonzero.
Steven Cole093cf722005-05-03 19:07:24 -0600481 * (3) cancellation, a type of error with status -ECONNRESET that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 * is initiated by usb_sg_cancel().
483 *
484 * When this function returns, all memory allocated through usb_sg_init() or
485 * this call will have been freed. The request block parameter may still be
486 * passed to usb_sg_cancel(), or it may be freed. It could also be
487 * reinitialized and then reused.
488 *
489 * Data Transfer Rates:
490 *
491 * Bulk transfers are valid for full or high speed endpoints.
492 * The best full speed data rate is 19 packets of 64 bytes each
493 * per frame, or 1216 bytes per millisecond.
494 * The best high speed data rate is 13 packets of 512 bytes each
495 * per microframe, or 52 KBytes per millisecond.
496 *
497 * The reason to use interrupt transfers through this API would most likely
498 * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
499 * could be transferred. That capability is less useful for low or full
500 * speed interrupt endpoints, which allow at most one packet per millisecond,
501 * of at most 8 or 64 bytes (respectively).
502 */
503void usb_sg_wait (struct usb_sg_request *io)
504{
505 int i, entries = io->entries;
506
507 /* queue the urbs. */
508 spin_lock_irq (&io->lock);
Alan Stern8ccef0d2007-06-21 16:26:46 -0400509 i = 0;
510 while (i < entries && !io->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 int retval;
512
513 io->urbs [i]->dev = io->dev;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800514 retval = usb_submit_urb (io->urbs [i], GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /* after we submit, let completions or cancelations fire;
517 * we handshake using io->status.
518 */
519 spin_unlock_irq (&io->lock);
520 switch (retval) {
521 /* maybe we retrying will recover */
522 case -ENXIO: // hc didn't queue this one
523 case -EAGAIN:
524 case -ENOMEM:
525 io->urbs[i]->dev = NULL;
526 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 yield ();
528 break;
529
530 /* no error? continue immediately.
531 *
532 * NOTE: to work better with UHCI (4K I/O buffer may
533 * need 3K of TDs) it may be good to limit how many
534 * URBs are queued at once; N milliseconds?
535 */
536 case 0:
Alan Stern8ccef0d2007-06-21 16:26:46 -0400537 ++i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 cpu_relax ();
539 break;
540
541 /* fail any uncompleted urbs */
542 default:
543 io->urbs [i]->dev = NULL;
544 io->urbs [i]->status = retval;
545 dev_dbg (&io->dev->dev, "%s, submit --> %d\n",
546 __FUNCTION__, retval);
547 usb_sg_cancel (io);
548 }
549 spin_lock_irq (&io->lock);
550 if (retval && (io->status == 0 || io->status == -ECONNRESET))
551 io->status = retval;
552 }
553 io->count -= entries - i;
554 if (io->count == 0)
555 complete (&io->complete);
556 spin_unlock_irq (&io->lock);
557
558 /* OK, yes, this could be packaged as non-blocking.
559 * So could the submit loop above ... but it's easier to
560 * solve neither problem than to solve both!
561 */
562 wait_for_completion (&io->complete);
563
564 sg_clean (io);
565}
566
567/**
568 * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
569 * @io: request block, initialized with usb_sg_init()
570 *
571 * This stops a request after it has been started by usb_sg_wait().
572 * It can also prevents one initialized by usb_sg_init() from starting,
573 * so that call just frees resources allocated to the request.
574 */
575void usb_sg_cancel (struct usb_sg_request *io)
576{
577 unsigned long flags;
578
579 spin_lock_irqsave (&io->lock, flags);
580
581 /* shut everything down, if it didn't already */
582 if (!io->status) {
583 int i;
584
585 io->status = -ECONNRESET;
586 spin_unlock (&io->lock);
587 for (i = 0; i < io->entries; i++) {
588 int retval;
589
590 if (!io->urbs [i]->dev)
591 continue;
592 retval = usb_unlink_urb (io->urbs [i]);
593 if (retval != -EINPROGRESS && retval != -EBUSY)
594 dev_warn (&io->dev->dev, "%s, unlink --> %d\n",
595 __FUNCTION__, retval);
596 }
597 spin_lock (&io->lock);
598 }
599 spin_unlock_irqrestore (&io->lock, flags);
600}
601
602/*-------------------------------------------------------------------*/
603
604/**
605 * usb_get_descriptor - issues a generic GET_DESCRIPTOR request
606 * @dev: the device whose descriptor is being retrieved
607 * @type: the descriptor type (USB_DT_*)
608 * @index: the number of the descriptor
609 * @buf: where to put the descriptor
610 * @size: how big is "buf"?
611 * Context: !in_interrupt ()
612 *
613 * Gets a USB descriptor. Convenience functions exist to simplify
614 * getting some types of descriptors. Use
615 * usb_get_string() or usb_string() for USB_DT_STRING.
616 * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG)
617 * are part of the device structure.
618 * In addition to a number of USB-standard descriptors, some
619 * devices also use class-specific or vendor-specific descriptors.
620 *
621 * This call is synchronous, and may not be used in an interrupt context.
622 *
623 * Returns the number of bytes received on success, or else the status code
624 * returned by the underlying usb_control_msg() call.
625 */
626int usb_get_descriptor(struct usb_device *dev, unsigned char type, unsigned char index, void *buf, int size)
627{
628 int i;
629 int result;
630
631 memset(buf,0,size); // Make sure we parse really received data
632
633 for (i = 0; i < 3; ++i) {
634 /* retry on length 0 or stall; some devices are flakey */
635 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
636 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
637 (type << 8) + index, 0, buf, size,
638 USB_CTRL_GET_TIMEOUT);
639 if (result == 0 || result == -EPIPE)
640 continue;
641 if (result > 1 && ((u8 *)buf)[1] != type) {
642 result = -EPROTO;
643 continue;
644 }
645 break;
646 }
647 return result;
648}
649
650/**
651 * usb_get_string - gets a string descriptor
652 * @dev: the device whose string descriptor is being retrieved
653 * @langid: code for language chosen (from string descriptor zero)
654 * @index: the number of the descriptor
655 * @buf: where to put the string
656 * @size: how big is "buf"?
657 * Context: !in_interrupt ()
658 *
659 * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character,
660 * in little-endian byte order).
661 * The usb_string() function will often be a convenient way to turn
662 * these strings into kernel-printable form.
663 *
664 * Strings may be referenced in device, configuration, interface, or other
665 * descriptors, and could also be used in vendor-specific ways.
666 *
667 * This call is synchronous, and may not be used in an interrupt context.
668 *
669 * Returns the number of bytes received on success, or else the status code
670 * returned by the underlying usb_control_msg() call.
671 */
Adrian Bunke266a122005-11-08 21:05:43 +0100672static int usb_get_string(struct usb_device *dev, unsigned short langid,
673 unsigned char index, void *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 int i;
676 int result;
677
678 for (i = 0; i < 3; ++i) {
679 /* retry on length 0 or stall; some devices are flakey */
680 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
681 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
682 (USB_DT_STRING << 8) + index, langid, buf, size,
683 USB_CTRL_GET_TIMEOUT);
684 if (!(result == 0 || result == -EPIPE))
685 break;
686 }
687 return result;
688}
689
690static void usb_try_string_workarounds(unsigned char *buf, int *length)
691{
692 int newlength, oldlength = *length;
693
694 for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
695 if (!isprint(buf[newlength]) || buf[newlength + 1])
696 break;
697
698 if (newlength > 2) {
699 buf[0] = newlength;
700 *length = newlength;
701 }
702}
703
704static int usb_string_sub(struct usb_device *dev, unsigned int langid,
705 unsigned int index, unsigned char *buf)
706{
707 int rc;
708
709 /* Try to read the string descriptor by asking for the maximum
710 * possible number of bytes */
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100711 if (dev->quirks & USB_QUIRK_STRING_FETCH_255)
712 rc = -EIO;
713 else
714 rc = usb_get_string(dev, langid, index, buf, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 /* If that failed try to read the descriptor length, then
717 * ask for just that many bytes */
718 if (rc < 2) {
719 rc = usb_get_string(dev, langid, index, buf, 2);
720 if (rc == 2)
721 rc = usb_get_string(dev, langid, index, buf, buf[0]);
722 }
723
724 if (rc >= 2) {
725 if (!buf[0] && !buf[1])
726 usb_try_string_workarounds(buf, &rc);
727
728 /* There might be extra junk at the end of the descriptor */
729 if (buf[0] < rc)
730 rc = buf[0];
731
732 rc = rc - (rc & 1); /* force a multiple of two */
733 }
734
735 if (rc < 2)
736 rc = (rc < 0 ? rc : -EINVAL);
737
738 return rc;
739}
740
741/**
742 * usb_string - returns ISO 8859-1 version of a string descriptor
743 * @dev: the device whose string descriptor is being retrieved
744 * @index: the number of the descriptor
745 * @buf: where to put the string
746 * @size: how big is "buf"?
747 * Context: !in_interrupt ()
748 *
749 * This converts the UTF-16LE encoded strings returned by devices, from
750 * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones
751 * that are more usable in most kernel contexts. Note that all characters
752 * in the chosen descriptor that can't be encoded using ISO-8859-1
753 * are converted to the question mark ("?") character, and this function
754 * chooses strings in the first language supported by the device.
755 *
756 * The ASCII (or, redundantly, "US-ASCII") character set is the seven-bit
757 * subset of ISO 8859-1. ISO-8859-1 is the eight-bit subset of Unicode,
758 * and is appropriate for use many uses of English and several other
759 * Western European languages. (But it doesn't include the "Euro" symbol.)
760 *
761 * This call is synchronous, and may not be used in an interrupt context.
762 *
763 * Returns length of the string (>= 0) or usb_control_msg status (< 0).
764 */
765int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
766{
767 unsigned char *tbuf;
768 int err;
769 unsigned int u, idx;
770
771 if (dev->state == USB_STATE_SUSPENDED)
772 return -EHOSTUNREACH;
773 if (size <= 0 || !buf || !index)
774 return -EINVAL;
775 buf[0] = 0;
776 tbuf = kmalloc(256, GFP_KERNEL);
777 if (!tbuf)
778 return -ENOMEM;
779
780 /* get langid for strings if it's not yet known */
781 if (!dev->have_langid) {
782 err = usb_string_sub(dev, 0, 0, tbuf);
783 if (err < 0) {
784 dev_err (&dev->dev,
785 "string descriptor 0 read error: %d\n",
786 err);
787 goto errout;
788 } else if (err < 4) {
789 dev_err (&dev->dev, "string descriptor 0 too short\n");
790 err = -EINVAL;
791 goto errout;
792 } else {
Alan Sternce361582006-11-20 11:12:22 -0500793 dev->have_langid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 dev->string_langid = tbuf[2] | (tbuf[3]<< 8);
795 /* always use the first langid listed */
796 dev_dbg (&dev->dev, "default language 0x%04x\n",
797 dev->string_langid);
798 }
799 }
800
801 err = usb_string_sub(dev, dev->string_langid, index, tbuf);
802 if (err < 0)
803 goto errout;
804
805 size--; /* leave room for trailing NULL char in output buffer */
806 for (idx = 0, u = 2; u < err; u += 2) {
807 if (idx >= size)
808 break;
809 if (tbuf[u+1]) /* high byte */
810 buf[idx++] = '?'; /* non ISO-8859-1 character */
811 else
812 buf[idx++] = tbuf[u];
813 }
814 buf[idx] = 0;
815 err = idx;
816
817 if (tbuf[1] != USB_DT_STRING)
818 dev_dbg(&dev->dev, "wrong descriptor type %02x for string %d (\"%s\")\n", tbuf[1], index, buf);
819
820 errout:
821 kfree(tbuf);
822 return err;
823}
824
Alan Stern4f62efe2005-10-24 16:24:14 -0400825/**
826 * usb_cache_string - read a string descriptor and cache it for later use
827 * @udev: the device whose string descriptor is being read
828 * @index: the descriptor index
829 *
830 * Returns a pointer to a kmalloc'ed buffer containing the descriptor string,
831 * or NULL if the index is 0 or the string could not be read.
832 */
833char *usb_cache_string(struct usb_device *udev, int index)
834{
835 char *buf;
836 char *smallbuf = NULL;
837 int len;
838
839 if (index > 0 && (buf = kmalloc(256, GFP_KERNEL)) != NULL) {
840 if ((len = usb_string(udev, index, buf, 256)) > 0) {
841 if ((smallbuf = kmalloc(++len, GFP_KERNEL)) == NULL)
842 return buf;
843 memcpy(smallbuf, buf, len);
844 }
845 kfree(buf);
846 }
847 return smallbuf;
848}
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850/*
851 * usb_get_device_descriptor - (re)reads the device descriptor (usbcore)
852 * @dev: the device whose device descriptor is being updated
853 * @size: how much of the descriptor to read
854 * Context: !in_interrupt ()
855 *
856 * Updates the copy of the device descriptor stored in the device structure,
Laurent Pinchart6ab16a92006-11-07 10:16:25 +0100857 * which dedicates space for this purpose.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 *
859 * Not exported, only for use by the core. If drivers really want to read
860 * the device descriptor directly, they can call usb_get_descriptor() with
861 * type = USB_DT_DEVICE and index = 0.
862 *
863 * This call is synchronous, and may not be used in an interrupt context.
864 *
865 * Returns the number of bytes received on success, or else the status code
866 * returned by the underlying usb_control_msg() call.
867 */
868int usb_get_device_descriptor(struct usb_device *dev, unsigned int size)
869{
870 struct usb_device_descriptor *desc;
871 int ret;
872
873 if (size > sizeof(*desc))
874 return -EINVAL;
875 desc = kmalloc(sizeof(*desc), GFP_NOIO);
876 if (!desc)
877 return -ENOMEM;
878
879 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);
880 if (ret >= 0)
881 memcpy(&dev->descriptor, desc, size);
882 kfree(desc);
883 return ret;
884}
885
886/**
887 * usb_get_status - issues a GET_STATUS call
888 * @dev: the device whose status is being checked
889 * @type: USB_RECIP_*; for device, interface, or endpoint
890 * @target: zero (for device), else interface or endpoint number
891 * @data: pointer to two bytes of bitmap data
892 * Context: !in_interrupt ()
893 *
894 * Returns device, interface, or endpoint status. Normally only of
895 * interest to see if the device is self powered, or has enabled the
896 * remote wakeup facility; or whether a bulk or interrupt endpoint
897 * is halted ("stalled").
898 *
899 * Bits in these status bitmaps are set using the SET_FEATURE request,
900 * and cleared using the CLEAR_FEATURE request. The usb_clear_halt()
901 * function should be used to clear halt ("stall") status.
902 *
903 * This call is synchronous, and may not be used in an interrupt context.
904 *
905 * Returns the number of bytes received on success, or else the status code
906 * returned by the underlying usb_control_msg() call.
907 */
908int usb_get_status(struct usb_device *dev, int type, int target, void *data)
909{
910 int ret;
911 u16 *status = kmalloc(sizeof(*status), GFP_KERNEL);
912
913 if (!status)
914 return -ENOMEM;
915
916 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
917 USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status,
918 sizeof(*status), USB_CTRL_GET_TIMEOUT);
919
920 *(u16 *)data = *status;
921 kfree(status);
922 return ret;
923}
924
925/**
926 * usb_clear_halt - tells device to clear endpoint halt/stall condition
927 * @dev: device whose endpoint is halted
928 * @pipe: endpoint "pipe" being cleared
929 * Context: !in_interrupt ()
930 *
931 * This is used to clear halt conditions for bulk and interrupt endpoints,
932 * as reported by URB completion status. Endpoints that are halted are
933 * sometimes referred to as being "stalled". Such endpoints are unable
934 * to transmit or receive data until the halt status is cleared. Any URBs
935 * queued for such an endpoint should normally be unlinked by the driver
936 * before clearing the halt condition, as described in sections 5.7.5
937 * and 5.8.5 of the USB 2.0 spec.
938 *
939 * Note that control and isochronous endpoints don't halt, although control
940 * endpoints report "protocol stall" (for unsupported requests) using the
941 * same status code used to report a true stall.
942 *
943 * This call is synchronous, and may not be used in an interrupt context.
944 *
945 * Returns zero on success, or else the status code returned by the
946 * underlying usb_control_msg() call.
947 */
948int usb_clear_halt(struct usb_device *dev, int pipe)
949{
950 int result;
951 int endp = usb_pipeendpoint(pipe);
952
953 if (usb_pipein (pipe))
954 endp |= USB_DIR_IN;
955
956 /* we don't care if it wasn't halted first. in fact some devices
957 * (like some ibmcam model 1 units) seem to expect hosts to make
958 * this request for iso endpoints, which can't halt!
959 */
960 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
961 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
962 USB_ENDPOINT_HALT, endp, NULL, 0,
963 USB_CTRL_SET_TIMEOUT);
964
965 /* don't un-halt or force to DATA0 except on success */
966 if (result < 0)
967 return result;
968
969 /* NOTE: seems like Microsoft and Apple don't bother verifying
970 * the clear "took", so some devices could lock up if you check...
971 * such as the Hagiwara FlashGate DUAL. So we won't bother.
972 *
973 * NOTE: make sure the logic here doesn't diverge much from
974 * the copy in usb-storage, for as long as we need two copies.
975 */
976
977 /* toggle was reset by the clear */
978 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
979
980 return 0;
981}
982
983/**
984 * usb_disable_endpoint -- Disable an endpoint by address
985 * @dev: the device whose endpoint is being disabled
986 * @epaddr: the endpoint's address. Endpoint number for output,
987 * endpoint number + USB_DIR_IN for input
988 *
989 * Deallocates hcd/hardware state for this endpoint ... and nukes all
990 * pending urbs.
991 *
992 * If the HCD hasn't registered a disable() function, this sets the
993 * endpoint's maxpacket size to 0 to prevent further submissions.
994 */
995void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr)
996{
997 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
998 struct usb_host_endpoint *ep;
999
1000 if (!dev)
1001 return;
1002
1003 if (usb_endpoint_out(epaddr)) {
1004 ep = dev->ep_out[epnum];
1005 dev->ep_out[epnum] = NULL;
1006 } else {
1007 ep = dev->ep_in[epnum];
1008 dev->ep_in[epnum] = NULL;
1009 }
Alan Sterna6d2bb92006-08-30 11:27:36 -04001010 if (ep && dev->bus)
1011 usb_hcd_endpoint_disable(dev, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014/**
1015 * usb_disable_interface -- Disable all endpoints for an interface
1016 * @dev: the device whose interface is being disabled
1017 * @intf: pointer to the interface descriptor
1018 *
1019 * Disables all the endpoints for the interface's current altsetting.
1020 */
1021void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf)
1022{
1023 struct usb_host_interface *alt = intf->cur_altsetting;
1024 int i;
1025
1026 for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
1027 usb_disable_endpoint(dev,
1028 alt->endpoint[i].desc.bEndpointAddress);
1029 }
1030}
1031
1032/*
1033 * usb_disable_device - Disable all the endpoints for a USB device
1034 * @dev: the device whose endpoints are being disabled
1035 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
1036 *
1037 * Disables all the device's endpoints, potentially including endpoint 0.
1038 * Deallocates hcd/hardware state for the endpoints (nuking all or most
1039 * pending urbs) and usbcore state for the interfaces, so that usbcore
1040 * must usb_set_configuration() before any interfaces could be used.
1041 */
1042void usb_disable_device(struct usb_device *dev, int skip_ep0)
1043{
1044 int i;
1045
1046 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __FUNCTION__,
1047 skip_ep0 ? "non-ep0" : "all");
1048 for (i = skip_ep0; i < 16; ++i) {
1049 usb_disable_endpoint(dev, i);
1050 usb_disable_endpoint(dev, i + USB_DIR_IN);
1051 }
1052 dev->toggle[0] = dev->toggle[1] = 0;
1053
1054 /* getting rid of interfaces will disconnect
1055 * any drivers bound to them (a key side effect)
1056 */
1057 if (dev->actconfig) {
1058 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1059 struct usb_interface *interface;
1060
Alan Stern86d30742005-07-29 12:17:16 -07001061 /* remove this interface if it has been registered */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 interface = dev->actconfig->interface[i];
Daniel Ritzd305ef52005-09-22 00:47:24 -07001063 if (!device_is_registered(&interface->dev))
Alan Stern86d30742005-07-29 12:17:16 -07001064 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 dev_dbg (&dev->dev, "unregistering interface %s\n",
1066 interface->dev.bus_id);
1067 usb_remove_sysfs_intf_files(interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 device_del (&interface->dev);
1069 }
1070
1071 /* Now that the interfaces are unbound, nobody should
1072 * try to access them.
1073 */
1074 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1075 put_device (&dev->actconfig->interface[i]->dev);
1076 dev->actconfig->interface[i] = NULL;
1077 }
1078 dev->actconfig = NULL;
1079 if (dev->state == USB_STATE_CONFIGURED)
1080 usb_set_device_state(dev, USB_STATE_ADDRESS);
1081 }
1082}
1083
1084
1085/*
1086 * usb_enable_endpoint - Enable an endpoint for USB communications
1087 * @dev: the device whose interface is being enabled
1088 * @ep: the endpoint
1089 *
1090 * Resets the endpoint toggle, and sets dev->ep_{in,out} pointers.
1091 * For control endpoints, both the input and output sides are handled.
1092 */
1093static void
1094usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep)
1095{
1096 unsigned int epaddr = ep->desc.bEndpointAddress;
1097 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1098 int is_control;
1099
1100 is_control = ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1101 == USB_ENDPOINT_XFER_CONTROL);
1102 if (usb_endpoint_out(epaddr) || is_control) {
1103 usb_settoggle(dev, epnum, 1, 0);
1104 dev->ep_out[epnum] = ep;
1105 }
1106 if (!usb_endpoint_out(epaddr) || is_control) {
1107 usb_settoggle(dev, epnum, 0, 0);
1108 dev->ep_in[epnum] = ep;
1109 }
1110}
1111
1112/*
1113 * usb_enable_interface - Enable all the endpoints for an interface
1114 * @dev: the device whose interface is being enabled
1115 * @intf: pointer to the interface descriptor
1116 *
1117 * Enables all the endpoints for the interface's current altsetting.
1118 */
1119static void usb_enable_interface(struct usb_device *dev,
1120 struct usb_interface *intf)
1121{
1122 struct usb_host_interface *alt = intf->cur_altsetting;
1123 int i;
1124
1125 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1126 usb_enable_endpoint(dev, &alt->endpoint[i]);
1127}
1128
1129/**
1130 * usb_set_interface - Makes a particular alternate setting be current
1131 * @dev: the device whose interface is being updated
1132 * @interface: the interface being updated
1133 * @alternate: the setting being chosen.
1134 * Context: !in_interrupt ()
1135 *
1136 * This is used to enable data transfers on interfaces that may not
1137 * be enabled by default. Not all devices support such configurability.
1138 * Only the driver bound to an interface may change its setting.
1139 *
1140 * Within any given configuration, each interface may have several
1141 * alternative settings. These are often used to control levels of
1142 * bandwidth consumption. For example, the default setting for a high
1143 * speed interrupt endpoint may not send more than 64 bytes per microframe,
1144 * while interrupt transfers of up to 3KBytes per microframe are legal.
1145 * Also, isochronous endpoints may never be part of an
1146 * interface's default setting. To access such bandwidth, alternate
1147 * interface settings must be made current.
1148 *
1149 * Note that in the Linux USB subsystem, bandwidth associated with
1150 * an endpoint in a given alternate setting is not reserved until an URB
1151 * is submitted that needs that bandwidth. Some other operating systems
1152 * allocate bandwidth early, when a configuration is chosen.
1153 *
1154 * This call is synchronous, and may not be used in an interrupt context.
1155 * Also, drivers must not change altsettings while urbs are scheduled for
1156 * endpoints in that interface; all such urbs must first be completed
1157 * (perhaps forced by unlinking).
1158 *
1159 * Returns zero on success, or else the status code returned by the
1160 * underlying usb_control_msg() call.
1161 */
1162int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1163{
1164 struct usb_interface *iface;
1165 struct usb_host_interface *alt;
1166 int ret;
1167 int manual = 0;
1168
1169 if (dev->state == USB_STATE_SUSPENDED)
1170 return -EHOSTUNREACH;
1171
1172 iface = usb_ifnum_to_if(dev, interface);
1173 if (!iface) {
1174 dev_dbg(&dev->dev, "selecting invalid interface %d\n",
1175 interface);
1176 return -EINVAL;
1177 }
1178
1179 alt = usb_altnum_to_altsetting(iface, alternate);
1180 if (!alt) {
1181 warn("selecting invalid altsetting %d", alternate);
1182 return -EINVAL;
1183 }
1184
1185 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1186 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1187 alternate, interface, NULL, 0, 5000);
1188
1189 /* 9.4.10 says devices don't need this and are free to STALL the
1190 * request if the interface only has one alternate setting.
1191 */
1192 if (ret == -EPIPE && iface->num_altsetting == 1) {
1193 dev_dbg(&dev->dev,
1194 "manual set_interface for iface %d, alt %d\n",
1195 interface, alternate);
1196 manual = 1;
1197 } else if (ret < 0)
1198 return ret;
1199
1200 /* FIXME drivers shouldn't need to replicate/bugfix the logic here
1201 * when they implement async or easily-killable versions of this or
1202 * other "should-be-internal" functions (like clear_halt).
1203 * should hcd+usbcore postprocess control requests?
1204 */
1205
1206 /* prevent submissions using previous endpoint settings */
Alan Stern0e6c8e82005-10-24 15:33:03 -04001207 if (device_is_registered(&iface->dev))
1208 usb_remove_sysfs_intf_files(iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 usb_disable_interface(dev, iface);
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 iface->cur_altsetting = alt;
1212
1213 /* If the interface only has one altsetting and the device didn't
David Brownella81e7ec2005-04-18 17:39:25 -07001214 * accept the request, we attempt to carry out the equivalent action
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 * by manually clearing the HALT feature for each endpoint in the
1216 * new altsetting.
1217 */
1218 if (manual) {
1219 int i;
1220
1221 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
1222 unsigned int epaddr =
1223 alt->endpoint[i].desc.bEndpointAddress;
1224 unsigned int pipe =
1225 __create_pipe(dev, USB_ENDPOINT_NUMBER_MASK & epaddr)
1226 | (usb_endpoint_out(epaddr) ? USB_DIR_OUT : USB_DIR_IN);
1227
1228 usb_clear_halt(dev, pipe);
1229 }
1230 }
1231
1232 /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
1233 *
1234 * Note:
1235 * Despite EP0 is always present in all interfaces/AS, the list of
1236 * endpoints from the descriptor does not contain EP0. Due to its
1237 * omnipresence one might expect EP0 being considered "affected" by
1238 * any SetInterface request and hence assume toggles need to be reset.
1239 * However, EP0 toggles are re-synced for every individual transfer
1240 * during the SETUP stage - hence EP0 toggles are "don't care" here.
1241 * (Likewise, EP0 never "halts" on well designed devices.)
1242 */
1243 usb_enable_interface(dev, iface);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001244 if (device_is_registered(&iface->dev))
1245 usb_create_sysfs_intf_files(iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 return 0;
1248}
1249
1250/**
1251 * usb_reset_configuration - lightweight device reset
1252 * @dev: the device whose configuration is being reset
1253 *
1254 * This issues a standard SET_CONFIGURATION request to the device using
1255 * the current configuration. The effect is to reset most USB-related
1256 * state in the device, including interface altsettings (reset to zero),
1257 * endpoint halts (cleared), and data toggle (only for bulk and interrupt
1258 * endpoints). Other usbcore state is unchanged, including bindings of
1259 * usb device drivers to interfaces.
1260 *
1261 * Because this affects multiple interfaces, avoid using this with composite
1262 * (multi-interface) devices. Instead, the driver for each interface may
David Brownella81e7ec2005-04-18 17:39:25 -07001263 * use usb_set_interface() on the interfaces it claims. Be careful though;
1264 * some devices don't support the SET_INTERFACE request, and others won't
1265 * reset all the interface state (notably data toggles). Resetting the whole
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 * configuration would affect other drivers' interfaces.
1267 *
1268 * The caller must own the device lock.
1269 *
1270 * Returns zero on success, else a negative error code.
1271 */
1272int usb_reset_configuration(struct usb_device *dev)
1273{
1274 int i, retval;
1275 struct usb_host_config *config;
1276
1277 if (dev->state == USB_STATE_SUSPENDED)
1278 return -EHOSTUNREACH;
1279
1280 /* caller must have locked the device and must own
1281 * the usb bus readlock (so driver bindings are stable);
1282 * calls during probe() are fine
1283 */
1284
1285 for (i = 1; i < 16; ++i) {
1286 usb_disable_endpoint(dev, i);
1287 usb_disable_endpoint(dev, i + USB_DIR_IN);
1288 }
1289
1290 config = dev->actconfig;
1291 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1292 USB_REQ_SET_CONFIGURATION, 0,
1293 config->desc.bConfigurationValue, 0,
1294 NULL, 0, USB_CTRL_SET_TIMEOUT);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001295 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 dev->toggle[0] = dev->toggle[1] = 0;
1299
1300 /* re-init hc/hcd interface/endpoint state */
1301 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1302 struct usb_interface *intf = config->interface[i];
1303 struct usb_host_interface *alt;
1304
Alan Stern0e6c8e82005-10-24 15:33:03 -04001305 if (device_is_registered(&intf->dev))
1306 usb_remove_sysfs_intf_files(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 alt = usb_altnum_to_altsetting(intf, 0);
1308
1309 /* No altsetting 0? We'll assume the first altsetting.
1310 * We could use a GetInterface call, but if a device is
1311 * so non-compliant that it doesn't have altsetting 0
1312 * then I wouldn't trust its reply anyway.
1313 */
1314 if (!alt)
1315 alt = &intf->altsetting[0];
1316
1317 intf->cur_altsetting = alt;
1318 usb_enable_interface(dev, intf);
Alan Stern0e6c8e82005-10-24 15:33:03 -04001319 if (device_is_registered(&intf->dev))
1320 usb_create_sysfs_intf_files(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322 return 0;
1323}
1324
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001325void usb_release_interface(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 struct usb_interface *intf = to_usb_interface(dev);
1328 struct usb_interface_cache *intfc =
1329 altsetting_to_usb_interface_cache(intf->altsetting);
1330
1331 kref_put(&intfc->ref, usb_release_interface_cache);
1332 kfree(intf);
1333}
1334
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001335#ifdef CONFIG_HOTPLUG
1336static int usb_if_uevent(struct device *dev, char **envp, int num_envp,
1337 char *buffer, int buffer_size)
1338{
1339 struct usb_device *usb_dev;
1340 struct usb_interface *intf;
1341 struct usb_host_interface *alt;
1342 int i = 0;
1343 int length = 0;
1344
1345 if (!dev)
1346 return -ENODEV;
1347
1348 /* driver is often null here; dev_dbg() would oops */
1349 pr_debug ("usb %s: uevent\n", dev->bus_id);
1350
1351 intf = to_usb_interface(dev);
1352 usb_dev = interface_to_usbdev(intf);
1353 alt = intf->cur_altsetting;
1354
1355 if (add_uevent_var(envp, num_envp, &i,
1356 buffer, buffer_size, &length,
1357 "INTERFACE=%d/%d/%d",
1358 alt->desc.bInterfaceClass,
1359 alt->desc.bInterfaceSubClass,
1360 alt->desc.bInterfaceProtocol))
1361 return -ENOMEM;
1362
1363 if (add_uevent_var(envp, num_envp, &i,
1364 buffer, buffer_size, &length,
1365 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
1366 le16_to_cpu(usb_dev->descriptor.idVendor),
1367 le16_to_cpu(usb_dev->descriptor.idProduct),
1368 le16_to_cpu(usb_dev->descriptor.bcdDevice),
1369 usb_dev->descriptor.bDeviceClass,
1370 usb_dev->descriptor.bDeviceSubClass,
1371 usb_dev->descriptor.bDeviceProtocol,
1372 alt->desc.bInterfaceClass,
1373 alt->desc.bInterfaceSubClass,
1374 alt->desc.bInterfaceProtocol))
1375 return -ENOMEM;
1376
1377 envp[i] = NULL;
1378 return 0;
1379}
1380
1381#else
1382
1383static int usb_if_uevent(struct device *dev, char **envp,
1384 int num_envp, char *buffer, int buffer_size)
1385{
1386 return -ENODEV;
1387}
1388#endif /* CONFIG_HOTPLUG */
1389
1390struct device_type usb_if_device_type = {
1391 .name = "usb_interface",
1392 .release = usb_release_interface,
1393 .uevent = usb_if_uevent,
1394};
1395
Craig W. Nadler165fe972007-06-15 23:14:35 -04001396static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,
1397 struct usb_host_config *config,
1398 u8 inum)
1399{
1400 struct usb_interface_assoc_descriptor *retval = NULL;
1401 struct usb_interface_assoc_descriptor *intf_assoc;
1402 int first_intf;
1403 int last_intf;
1404 int i;
1405
1406 for (i = 0; (i < USB_MAXIADS && config->intf_assoc[i]); i++) {
1407 intf_assoc = config->intf_assoc[i];
1408 if (intf_assoc->bInterfaceCount == 0)
1409 continue;
1410
1411 first_intf = intf_assoc->bFirstInterface;
1412 last_intf = first_intf + (intf_assoc->bInterfaceCount - 1);
1413 if (inum >= first_intf && inum <= last_intf) {
1414 if (!retval)
1415 retval = intf_assoc;
1416 else
1417 dev_err(&dev->dev, "Interface #%d referenced"
1418 " by multiple IADs\n", inum);
1419 }
1420 }
1421
1422 return retval;
1423}
1424
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426/*
1427 * usb_set_configuration - Makes a particular device setting be current
1428 * @dev: the device whose configuration is being updated
1429 * @configuration: the configuration being chosen.
1430 * Context: !in_interrupt(), caller owns the device lock
1431 *
1432 * This is used to enable non-default device modes. Not all devices
1433 * use this kind of configurability; many devices only have one
1434 * configuration.
1435 *
Alan Stern3f141e22007-02-08 16:40:43 -05001436 * @configuration is the value of the configuration to be installed.
1437 * According to the USB spec (e.g. section 9.1.1.5), configuration values
1438 * must be non-zero; a value of zero indicates that the device in
1439 * unconfigured. However some devices erroneously use 0 as one of their
1440 * configuration values. To help manage such devices, this routine will
1441 * accept @configuration = -1 as indicating the device should be put in
1442 * an unconfigured state.
1443 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 * USB device configurations may affect Linux interoperability,
1445 * power consumption and the functionality available. For example,
1446 * the default configuration is limited to using 100mA of bus power,
1447 * so that when certain device functionality requires more power,
1448 * and the device is bus powered, that functionality should be in some
1449 * non-default device configuration. Other device modes may also be
1450 * reflected as configuration options, such as whether two ISDN
1451 * channels are available independently; and choosing between open
1452 * standard device protocols (like CDC) or proprietary ones.
1453 *
1454 * Note that USB has an additional level of device configurability,
1455 * associated with interfaces. That configurability is accessed using
1456 * usb_set_interface().
1457 *
1458 * This call is synchronous. The calling context must be able to sleep,
1459 * must own the device lock, and must not hold the driver model's USB
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -04001460 * bus mutex; usb device driver probe() methods cannot use this routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 *
1462 * Returns zero on success, or else the status code returned by the
Steven Cole093cf722005-05-03 19:07:24 -06001463 * underlying call that failed. On successful completion, each interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 * in the original device configuration has been destroyed, and each one
1465 * in the new configuration has been probed by all relevant usb device
1466 * drivers currently known to the kernel.
1467 */
1468int usb_set_configuration(struct usb_device *dev, int configuration)
1469{
1470 int i, ret;
1471 struct usb_host_config *cp = NULL;
1472 struct usb_interface **new_interfaces = NULL;
1473 int n, nintf;
1474
Alan Stern3f141e22007-02-08 16:40:43 -05001475 if (configuration == -1)
1476 configuration = 0;
1477 else {
1478 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1479 if (dev->config[i].desc.bConfigurationValue ==
1480 configuration) {
1481 cp = &dev->config[i];
1482 break;
1483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
1485 }
1486 if ((!cp && configuration != 0))
1487 return -EINVAL;
1488
1489 /* The USB spec says configuration 0 means unconfigured.
1490 * But if a device includes a configuration numbered 0,
1491 * we will accept it as a correctly configured state.
Alan Stern3f141e22007-02-08 16:40:43 -05001492 * Use -1 if you really want to unconfigure the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 */
1494 if (cp && configuration == 0)
1495 dev_warn(&dev->dev, "config 0 descriptor??\n");
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 /* Allocate memory for new interfaces before doing anything else,
1498 * so that if we run out then nothing will have changed. */
1499 n = nintf = 0;
1500 if (cp) {
1501 nintf = cp->desc.bNumInterfaces;
1502 new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
1503 GFP_KERNEL);
1504 if (!new_interfaces) {
1505 dev_err(&dev->dev, "Out of memory");
1506 return -ENOMEM;
1507 }
1508
1509 for (; n < nintf; ++n) {
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001510 new_interfaces[n] = kzalloc(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 sizeof(struct usb_interface),
1512 GFP_KERNEL);
1513 if (!new_interfaces[n]) {
1514 dev_err(&dev->dev, "Out of memory");
1515 ret = -ENOMEM;
1516free_interfaces:
1517 while (--n >= 0)
1518 kfree(new_interfaces[n]);
1519 kfree(new_interfaces);
1520 return ret;
1521 }
1522 }
Alan Stern6ad07122006-06-01 13:59:16 -04001523
1524 i = dev->bus_mA - cp->desc.bMaxPower * 2;
1525 if (i < 0)
1526 dev_warn(&dev->dev, "new config #%d exceeds power "
1527 "limit by %dmA\n",
1528 configuration, -i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
1530
Alan Stern01d883d2006-08-30 15:47:18 -04001531 /* Wake up the device so we can send it the Set-Config request */
Alan Stern94fcda12006-11-20 11:38:46 -05001532 ret = usb_autoresume_device(dev);
Alan Stern01d883d2006-08-30 15:47:18 -04001533 if (ret)
1534 goto free_interfaces;
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 /* if it's already configured, clear out old state first.
1537 * getting rid of old interfaces means unbinding their drivers.
1538 */
1539 if (dev->state != USB_STATE_ADDRESS)
1540 usb_disable_device (dev, 1); // Skip ep0
1541
1542 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1543 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
Alan Stern6ad07122006-06-01 13:59:16 -04001544 NULL, 0, USB_CTRL_SET_TIMEOUT)) < 0) {
1545
1546 /* All the old state is gone, so what else can we do?
1547 * The device is probably useless now anyway.
1548 */
1549 cp = NULL;
1550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 dev->actconfig = cp;
Alan Stern6ad07122006-06-01 13:59:16 -04001553 if (!cp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 usb_set_device_state(dev, USB_STATE_ADDRESS);
Alan Stern94fcda12006-11-20 11:38:46 -05001555 usb_autosuspend_device(dev);
Alan Stern6ad07122006-06-01 13:59:16 -04001556 goto free_interfaces;
1557 }
1558 usb_set_device_state(dev, USB_STATE_CONFIGURED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Alan Stern6ad07122006-06-01 13:59:16 -04001560 /* Initialize the new interface structures and the
1561 * hc/hcd/usbcore interface/endpoint state.
1562 */
1563 for (i = 0; i < nintf; ++i) {
1564 struct usb_interface_cache *intfc;
1565 struct usb_interface *intf;
1566 struct usb_host_interface *alt;
1567
1568 cp->interface[i] = intf = new_interfaces[i];
1569 intfc = cp->intf_cache[i];
1570 intf->altsetting = intfc->altsetting;
1571 intf->num_altsetting = intfc->num_altsetting;
Craig W. Nadler165fe972007-06-15 23:14:35 -04001572 intf->intf_assoc = find_iad(dev, cp, i);
Alan Stern6ad07122006-06-01 13:59:16 -04001573 kref_get(&intfc->ref);
1574
1575 alt = usb_altnum_to_altsetting(intf, 0);
1576
1577 /* No altsetting 0? We'll assume the first altsetting.
1578 * We could use a GetInterface call, but if a device is
1579 * so non-compliant that it doesn't have altsetting 0
1580 * then I wouldn't trust its reply anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 */
Alan Stern6ad07122006-06-01 13:59:16 -04001582 if (!alt)
1583 alt = &intf->altsetting[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Alan Stern6ad07122006-06-01 13:59:16 -04001585 intf->cur_altsetting = alt;
1586 usb_enable_interface(dev, intf);
1587 intf->dev.parent = &dev->dev;
1588 intf->dev.driver = NULL;
1589 intf->dev.bus = &usb_bus_type;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001590 intf->dev.type = &usb_if_device_type;
Alan Stern6ad07122006-06-01 13:59:16 -04001591 intf->dev.dma_mask = dev->dev.dma_mask;
Alan Stern6ad07122006-06-01 13:59:16 -04001592 device_initialize (&intf->dev);
1593 mark_quiesced(intf);
1594 sprintf (&intf->dev.bus_id[0], "%d-%s:%d.%d",
1595 dev->bus->busnum, dev->devpath,
1596 configuration, alt->desc.bInterfaceNumber);
1597 }
1598 kfree(new_interfaces);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Alan Stern6ad07122006-06-01 13:59:16 -04001600 if (cp->string == NULL)
1601 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Alan Stern6ad07122006-06-01 13:59:16 -04001603 /* Now that all the interfaces are set up, register them
1604 * to trigger binding of drivers to interfaces. probe()
1605 * routines may install different altsettings and may
1606 * claim() any interfaces not yet bound. Many class drivers
1607 * need that: CDC, audio, video, etc.
1608 */
1609 for (i = 0; i < nintf; ++i) {
1610 struct usb_interface *intf = cp->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Alan Stern6ad07122006-06-01 13:59:16 -04001612 dev_dbg (&dev->dev,
1613 "adding %s (config #%d, interface %d)\n",
1614 intf->dev.bus_id, configuration,
1615 intf->cur_altsetting->desc.bInterfaceNumber);
1616 ret = device_add (&intf->dev);
1617 if (ret != 0) {
1618 dev_err(&dev->dev, "device_add(%s) --> %d\n",
1619 intf->dev.bus_id, ret);
1620 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
Alan Stern6ad07122006-06-01 13:59:16 -04001622 usb_create_sysfs_intf_files (intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624
Alan Stern94fcda12006-11-20 11:38:46 -05001625 usb_autosuspend_device(dev);
Alan Stern86d30742005-07-29 12:17:16 -07001626 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628
Alan Stern088dc272006-08-21 12:08:19 -04001629struct set_config_request {
1630 struct usb_device *udev;
1631 int config;
1632 struct work_struct work;
1633};
1634
1635/* Worker routine for usb_driver_set_configuration() */
David Howellsc4028952006-11-22 14:57:56 +00001636static void driver_set_config_work(struct work_struct *work)
Alan Stern088dc272006-08-21 12:08:19 -04001637{
David Howellsc4028952006-11-22 14:57:56 +00001638 struct set_config_request *req =
1639 container_of(work, struct set_config_request, work);
Alan Stern088dc272006-08-21 12:08:19 -04001640
1641 usb_lock_device(req->udev);
1642 usb_set_configuration(req->udev, req->config);
1643 usb_unlock_device(req->udev);
1644 usb_put_dev(req->udev);
1645 kfree(req);
1646}
1647
1648/**
1649 * usb_driver_set_configuration - Provide a way for drivers to change device configurations
1650 * @udev: the device whose configuration is being updated
1651 * @config: the configuration being chosen.
1652 * Context: In process context, must be able to sleep
1653 *
1654 * Device interface drivers are not allowed to change device configurations.
1655 * This is because changing configurations will destroy the interface the
1656 * driver is bound to and create new ones; it would be like a floppy-disk
1657 * driver telling the computer to replace the floppy-disk drive with a
1658 * tape drive!
1659 *
1660 * Still, in certain specialized circumstances the need may arise. This
1661 * routine gets around the normal restrictions by using a work thread to
1662 * submit the change-config request.
1663 *
1664 * Returns 0 if the request was succesfully queued, error code otherwise.
1665 * The caller has no way to know whether the queued request will eventually
1666 * succeed.
1667 */
1668int usb_driver_set_configuration(struct usb_device *udev, int config)
1669{
1670 struct set_config_request *req;
1671
1672 req = kmalloc(sizeof(*req), GFP_KERNEL);
1673 if (!req)
1674 return -ENOMEM;
1675 req->udev = udev;
1676 req->config = config;
David Howellsc4028952006-11-22 14:57:56 +00001677 INIT_WORK(&req->work, driver_set_config_work);
Alan Stern088dc272006-08-21 12:08:19 -04001678
1679 usb_get_dev(udev);
Alan Stern1737bf22006-12-15 16:04:52 -05001680 schedule_work(&req->work);
Alan Stern088dc272006-08-21 12:08:19 -04001681 return 0;
1682}
1683EXPORT_SYMBOL_GPL(usb_driver_set_configuration);
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685// synchronous request completion model
1686EXPORT_SYMBOL(usb_control_msg);
1687EXPORT_SYMBOL(usb_bulk_msg);
1688
1689EXPORT_SYMBOL(usb_sg_init);
1690EXPORT_SYMBOL(usb_sg_cancel);
1691EXPORT_SYMBOL(usb_sg_wait);
1692
1693// synchronous control message convenience routines
1694EXPORT_SYMBOL(usb_get_descriptor);
1695EXPORT_SYMBOL(usb_get_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696EXPORT_SYMBOL(usb_string);
1697
1698// synchronous calls that also maintain usbcore state
1699EXPORT_SYMBOL(usb_clear_halt);
1700EXPORT_SYMBOL(usb_reset_configuration);
1701EXPORT_SYMBOL(usb_set_interface);
1702