Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * message.c - synchronous message handling |
| 3 | */ |
| 4 | |
| 5 | #include <linux/config.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/pci.h> /* for scatterlist macros */ |
| 7 | #include <linux/usb.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/mm.h> |
| 12 | #include <linux/timer.h> |
| 13 | #include <linux/ctype.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <asm/byteorder.h> |
Alexey Dobriyan | 5d68dfc | 2006-01-19 00:06:07 +0300 | [diff] [blame] | 16 | #include <asm/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | #include "hcd.h" /* for usbcore internals */ |
| 19 | #include "usb.h" |
| 20 | |
| 21 | static void usb_api_blocking_completion(struct urb *urb, struct pt_regs *regs) |
| 22 | { |
| 23 | complete((struct completion *)urb->context); |
| 24 | } |
| 25 | |
| 26 | |
| 27 | static void timeout_kill(unsigned long data) |
| 28 | { |
| 29 | struct urb *urb = (struct urb *) data; |
| 30 | |
| 31 | usb_unlink_urb(urb); |
| 32 | } |
| 33 | |
| 34 | // Starts urb and waits for completion or timeout |
| 35 | // note that this call is NOT interruptible, while |
| 36 | // many device driver i/o requests should be interruptible |
| 37 | static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length) |
| 38 | { |
| 39 | struct completion done; |
| 40 | struct timer_list timer; |
| 41 | int status; |
| 42 | |
| 43 | init_completion(&done); |
| 44 | urb->context = &done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | urb->actual_length = 0; |
| 46 | status = usb_submit_urb(urb, GFP_NOIO); |
| 47 | |
| 48 | if (status == 0) { |
| 49 | if (timeout > 0) { |
| 50 | init_timer(&timer); |
| 51 | timer.expires = jiffies + msecs_to_jiffies(timeout); |
| 52 | timer.data = (unsigned long)urb; |
| 53 | timer.function = timeout_kill; |
| 54 | /* grr. timeout _should_ include submit delays. */ |
| 55 | add_timer(&timer); |
| 56 | } |
| 57 | wait_for_completion(&done); |
| 58 | status = urb->status; |
| 59 | /* note: HCDs return ETIMEDOUT for other reasons too */ |
| 60 | if (status == -ECONNRESET) { |
| 61 | dev_dbg(&urb->dev->dev, |
| 62 | "%s timed out on ep%d%s len=%d/%d\n", |
| 63 | current->comm, |
| 64 | usb_pipeendpoint(urb->pipe), |
| 65 | usb_pipein(urb->pipe) ? "in" : "out", |
| 66 | urb->actual_length, |
| 67 | urb->transfer_buffer_length |
| 68 | ); |
| 69 | if (urb->actual_length > 0) |
| 70 | status = 0; |
| 71 | else |
| 72 | status = -ETIMEDOUT; |
| 73 | } |
| 74 | if (timeout > 0) |
| 75 | del_timer_sync(&timer); |
| 76 | } |
| 77 | |
| 78 | if (actual_length) |
| 79 | *actual_length = urb->actual_length; |
| 80 | usb_free_urb(urb); |
| 81 | return status; |
| 82 | } |
| 83 | |
| 84 | /*-------------------------------------------------------------------*/ |
| 85 | // returns status (negative) or length (positive) |
| 86 | static int usb_internal_control_msg(struct usb_device *usb_dev, |
| 87 | unsigned int pipe, |
| 88 | struct usb_ctrlrequest *cmd, |
| 89 | void *data, int len, int timeout) |
| 90 | { |
| 91 | struct urb *urb; |
| 92 | int retv; |
| 93 | int length; |
| 94 | |
| 95 | urb = usb_alloc_urb(0, GFP_NOIO); |
| 96 | if (!urb) |
| 97 | return -ENOMEM; |
| 98 | |
| 99 | usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char *)cmd, data, |
| 100 | len, usb_api_blocking_completion, NULL); |
| 101 | |
| 102 | retv = usb_start_wait_urb(urb, timeout, &length); |
| 103 | if (retv < 0) |
| 104 | return retv; |
| 105 | else |
| 106 | return length; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * usb_control_msg - Builds a control urb, sends it off and waits for completion |
| 111 | * @dev: pointer to the usb device to send the message to |
| 112 | * @pipe: endpoint "pipe" to send the message to |
| 113 | * @request: USB message request value |
| 114 | * @requesttype: USB message request type value |
| 115 | * @value: USB message value |
| 116 | * @index: USB message index value |
| 117 | * @data: pointer to the data to send |
| 118 | * @size: length in bytes of the data to send |
| 119 | * @timeout: time in msecs to wait for the message to complete before |
| 120 | * timing out (if 0 the wait is forever) |
| 121 | * Context: !in_interrupt () |
| 122 | * |
| 123 | * This function sends a simple control message to a specified endpoint |
| 124 | * and waits for the message to complete, or timeout. |
| 125 | * |
| 126 | * If successful, it returns the number of bytes transferred, otherwise a negative error number. |
| 127 | * |
| 128 | * Don't use this function from within an interrupt context, like a |
| 129 | * bottom half handler. If you need an asynchronous message, or need to send |
| 130 | * a message from within interrupt context, use usb_submit_urb() |
| 131 | * If a thread in your driver uses this call, make sure your disconnect() |
| 132 | * method can wait for it to complete. Since you don't have a handle on |
| 133 | * the URB used, you can't cancel the request. |
| 134 | */ |
| 135 | int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request, __u8 requesttype, |
| 136 | __u16 value, __u16 index, void *data, __u16 size, int timeout) |
| 137 | { |
| 138 | struct usb_ctrlrequest *dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO); |
| 139 | int ret; |
| 140 | |
| 141 | if (!dr) |
| 142 | return -ENOMEM; |
| 143 | |
| 144 | dr->bRequestType= requesttype; |
| 145 | dr->bRequest = request; |
| 146 | dr->wValue = cpu_to_le16p(&value); |
| 147 | dr->wIndex = cpu_to_le16p(&index); |
| 148 | dr->wLength = cpu_to_le16p(&size); |
| 149 | |
| 150 | //dbg("usb_control_msg"); |
| 151 | |
| 152 | ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout); |
| 153 | |
| 154 | kfree(dr); |
| 155 | |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /** |
Greg Kroah-Hartman | 782a7a6 | 2006-05-19 13:20:20 -0700 | [diff] [blame^] | 161 | * usb_interrupt_msg - Builds an interrupt urb, sends it off and waits for completion |
| 162 | * @usb_dev: pointer to the usb device to send the message to |
| 163 | * @pipe: endpoint "pipe" to send the message to |
| 164 | * @data: pointer to the data to send |
| 165 | * @len: length in bytes of the data to send |
| 166 | * @actual_length: pointer to a location to put the actual length transferred in bytes |
| 167 | * @timeout: time in msecs to wait for the message to complete before |
| 168 | * timing out (if 0 the wait is forever) |
| 169 | * Context: !in_interrupt () |
| 170 | * |
| 171 | * This function sends a simple interrupt message to a specified endpoint and |
| 172 | * waits for the message to complete, or timeout. |
| 173 | * |
| 174 | * If successful, it returns 0, otherwise a negative error number. The number |
| 175 | * of actual bytes transferred will be stored in the actual_length paramater. |
| 176 | * |
| 177 | * Don't use this function from within an interrupt context, like a bottom half |
| 178 | * handler. If you need an asynchronous message, or need to send a message |
| 179 | * from within interrupt context, use usb_submit_urb() If a thread in your |
| 180 | * driver uses this call, make sure your disconnect() method can wait for it to |
| 181 | * complete. Since you don't have a handle on the URB used, you can't cancel |
| 182 | * the request. |
| 183 | */ |
| 184 | int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe, |
| 185 | void *data, int len, int *actual_length, int timeout) |
| 186 | { |
| 187 | return usb_bulk_msg(usb_dev, pipe, data, len, actual_length, timeout); |
| 188 | } |
| 189 | EXPORT_SYMBOL_GPL(usb_interrupt_msg); |
| 190 | |
| 191 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion |
| 193 | * @usb_dev: pointer to the usb device to send the message to |
| 194 | * @pipe: endpoint "pipe" to send the message to |
| 195 | * @data: pointer to the data to send |
| 196 | * @len: length in bytes of the data to send |
| 197 | * @actual_length: pointer to a location to put the actual length transferred in bytes |
| 198 | * @timeout: time in msecs to wait for the message to complete before |
| 199 | * timing out (if 0 the wait is forever) |
| 200 | * Context: !in_interrupt () |
| 201 | * |
| 202 | * This function sends a simple bulk message to a specified endpoint |
| 203 | * and waits for the message to complete, or timeout. |
| 204 | * |
| 205 | * If successful, it returns 0, otherwise a negative error number. |
| 206 | * The number of actual bytes transferred will be stored in the |
| 207 | * actual_length paramater. |
| 208 | * |
| 209 | * Don't use this function from within an interrupt context, like a |
| 210 | * bottom half handler. If you need an asynchronous message, or need to |
| 211 | * send a message from within interrupt context, use usb_submit_urb() |
| 212 | * If a thread in your driver uses this call, make sure your disconnect() |
| 213 | * method can wait for it to complete. Since you don't have a handle on |
| 214 | * the URB used, you can't cancel the request. |
Alan Stern | d09d36a | 2005-09-26 16:22:45 -0400 | [diff] [blame] | 215 | * |
| 216 | * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT |
| 217 | * ioctl, users are forced to abuse this routine by using it to submit |
| 218 | * URBs for interrupt endpoints. We will take the liberty of creating |
| 219 | * an interrupt URB (with the default interval) if the target is an |
| 220 | * interrupt endpoint. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | */ |
| 222 | int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, |
| 223 | void *data, int len, int *actual_length, int timeout) |
| 224 | { |
| 225 | struct urb *urb; |
Alan Stern | d09d36a | 2005-09-26 16:22:45 -0400 | [diff] [blame] | 226 | struct usb_host_endpoint *ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
Alan Stern | d09d36a | 2005-09-26 16:22:45 -0400 | [diff] [blame] | 228 | ep = (usb_pipein(pipe) ? usb_dev->ep_in : usb_dev->ep_out) |
| 229 | [usb_pipeendpoint(pipe)]; |
| 230 | if (!ep || len < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | return -EINVAL; |
| 232 | |
Alan Stern | d09d36a | 2005-09-26 16:22:45 -0400 | [diff] [blame] | 233 | urb = usb_alloc_urb(0, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | if (!urb) |
| 235 | return -ENOMEM; |
| 236 | |
Alan Stern | d09d36a | 2005-09-26 16:22:45 -0400 | [diff] [blame] | 237 | if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == |
| 238 | USB_ENDPOINT_XFER_INT) { |
| 239 | pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30); |
| 240 | usb_fill_int_urb(urb, usb_dev, pipe, data, len, |
| 241 | usb_api_blocking_completion, NULL, |
| 242 | ep->desc.bInterval); |
| 243 | } else |
| 244 | usb_fill_bulk_urb(urb, usb_dev, pipe, data, len, |
| 245 | usb_api_blocking_completion, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
| 247 | return usb_start_wait_urb(urb, timeout, actual_length); |
| 248 | } |
| 249 | |
| 250 | /*-------------------------------------------------------------------*/ |
| 251 | |
| 252 | static void sg_clean (struct usb_sg_request *io) |
| 253 | { |
| 254 | if (io->urbs) { |
| 255 | while (io->entries--) |
| 256 | usb_free_urb (io->urbs [io->entries]); |
| 257 | kfree (io->urbs); |
| 258 | io->urbs = NULL; |
| 259 | } |
| 260 | if (io->dev->dev.dma_mask != NULL) |
| 261 | usb_buffer_unmap_sg (io->dev, io->pipe, io->sg, io->nents); |
| 262 | io->dev = NULL; |
| 263 | } |
| 264 | |
| 265 | static void sg_complete (struct urb *urb, struct pt_regs *regs) |
| 266 | { |
| 267 | struct usb_sg_request *io = (struct usb_sg_request *) urb->context; |
| 268 | |
| 269 | spin_lock (&io->lock); |
| 270 | |
| 271 | /* In 2.5 we require hcds' endpoint queues not to progress after fault |
| 272 | * reports, until the completion callback (this!) returns. That lets |
| 273 | * device driver code (like this routine) unlink queued urbs first, |
| 274 | * if it needs to, since the HC won't work on them at all. So it's |
| 275 | * not possible for page N+1 to overwrite page N, and so on. |
| 276 | * |
| 277 | * That's only for "hard" faults; "soft" faults (unlinks) sometimes |
| 278 | * complete before the HCD can get requests away from hardware, |
| 279 | * though never during cleanup after a hard fault. |
| 280 | */ |
| 281 | if (io->status |
| 282 | && (io->status != -ECONNRESET |
| 283 | || urb->status != -ECONNRESET) |
| 284 | && urb->actual_length) { |
| 285 | dev_err (io->dev->bus->controller, |
| 286 | "dev %s ep%d%s scatterlist error %d/%d\n", |
| 287 | io->dev->devpath, |
| 288 | usb_pipeendpoint (urb->pipe), |
| 289 | usb_pipein (urb->pipe) ? "in" : "out", |
| 290 | urb->status, io->status); |
| 291 | // BUG (); |
| 292 | } |
| 293 | |
| 294 | if (io->status == 0 && urb->status && urb->status != -ECONNRESET) { |
| 295 | int i, found, status; |
| 296 | |
| 297 | io->status = urb->status; |
| 298 | |
| 299 | /* the previous urbs, and this one, completed already. |
| 300 | * unlink pending urbs so they won't rx/tx bad data. |
| 301 | * careful: unlink can sometimes be synchronous... |
| 302 | */ |
| 303 | spin_unlock (&io->lock); |
| 304 | for (i = 0, found = 0; i < io->entries; i++) { |
| 305 | if (!io->urbs [i] || !io->urbs [i]->dev) |
| 306 | continue; |
| 307 | if (found) { |
| 308 | status = usb_unlink_urb (io->urbs [i]); |
david-b@pacbell.net | 8f34c28 | 2005-08-11 19:36:36 -0700 | [diff] [blame] | 309 | if (status != -EINPROGRESS |
| 310 | && status != -ENODEV |
| 311 | && status != -EBUSY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | dev_err (&io->dev->dev, |
| 313 | "%s, unlink --> %d\n", |
| 314 | __FUNCTION__, status); |
| 315 | } else if (urb == io->urbs [i]) |
| 316 | found = 1; |
| 317 | } |
| 318 | spin_lock (&io->lock); |
| 319 | } |
| 320 | urb->dev = NULL; |
| 321 | |
| 322 | /* on the last completion, signal usb_sg_wait() */ |
| 323 | io->bytes += urb->actual_length; |
| 324 | io->count--; |
| 325 | if (!io->count) |
| 326 | complete (&io->complete); |
| 327 | |
| 328 | spin_unlock (&io->lock); |
| 329 | } |
| 330 | |
| 331 | |
| 332 | /** |
| 333 | * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request |
| 334 | * @io: request block being initialized. until usb_sg_wait() returns, |
| 335 | * treat this as a pointer to an opaque block of memory, |
| 336 | * @dev: the usb device that will send or receive the data |
| 337 | * @pipe: endpoint "pipe" used to transfer the data |
| 338 | * @period: polling rate for interrupt endpoints, in frames or |
| 339 | * (for high speed endpoints) microframes; ignored for bulk |
| 340 | * @sg: scatterlist entries |
| 341 | * @nents: how many entries in the scatterlist |
| 342 | * @length: how many bytes to send from the scatterlist, or zero to |
| 343 | * send every byte identified in the list. |
| 344 | * @mem_flags: SLAB_* flags affecting memory allocations in this call |
| 345 | * |
| 346 | * Returns zero for success, else a negative errno value. This initializes a |
| 347 | * scatter/gather request, allocating resources such as I/O mappings and urb |
| 348 | * memory (except maybe memory used by USB controller drivers). |
| 349 | * |
| 350 | * The request must be issued using usb_sg_wait(), which waits for the I/O to |
| 351 | * complete (or to be canceled) and then cleans up all resources allocated by |
| 352 | * usb_sg_init(). |
| 353 | * |
| 354 | * The request may be canceled with usb_sg_cancel(), either before or after |
| 355 | * usb_sg_wait() is called. |
| 356 | */ |
| 357 | int usb_sg_init ( |
| 358 | struct usb_sg_request *io, |
| 359 | struct usb_device *dev, |
| 360 | unsigned pipe, |
| 361 | unsigned period, |
| 362 | struct scatterlist *sg, |
| 363 | int nents, |
| 364 | size_t length, |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 365 | gfp_t mem_flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | ) |
| 367 | { |
| 368 | int i; |
| 369 | int urb_flags; |
| 370 | int dma; |
| 371 | |
| 372 | if (!io || !dev || !sg |
| 373 | || usb_pipecontrol (pipe) |
| 374 | || usb_pipeisoc (pipe) |
| 375 | || nents <= 0) |
| 376 | return -EINVAL; |
| 377 | |
| 378 | spin_lock_init (&io->lock); |
| 379 | io->dev = dev; |
| 380 | io->pipe = pipe; |
| 381 | io->sg = sg; |
| 382 | io->nents = nents; |
| 383 | |
| 384 | /* not all host controllers use DMA (like the mainstream pci ones); |
| 385 | * they can use PIO (sl811) or be software over another transport. |
| 386 | */ |
| 387 | dma = (dev->dev.dma_mask != NULL); |
| 388 | if (dma) |
| 389 | io->entries = usb_buffer_map_sg (dev, pipe, sg, nents); |
| 390 | else |
| 391 | io->entries = nents; |
| 392 | |
| 393 | /* initialize all the urbs we'll use */ |
| 394 | if (io->entries <= 0) |
| 395 | return io->entries; |
| 396 | |
| 397 | io->count = io->entries; |
| 398 | io->urbs = kmalloc (io->entries * sizeof *io->urbs, mem_flags); |
| 399 | if (!io->urbs) |
| 400 | goto nomem; |
| 401 | |
Alan Stern | b375a04 | 2005-07-29 16:11:07 -0400 | [diff] [blame] | 402 | urb_flags = URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | if (usb_pipein (pipe)) |
| 404 | urb_flags |= URB_SHORT_NOT_OK; |
| 405 | |
| 406 | for (i = 0; i < io->entries; i++) { |
| 407 | unsigned len; |
| 408 | |
| 409 | io->urbs [i] = usb_alloc_urb (0, mem_flags); |
| 410 | if (!io->urbs [i]) { |
| 411 | io->entries = i; |
| 412 | goto nomem; |
| 413 | } |
| 414 | |
| 415 | io->urbs [i]->dev = NULL; |
| 416 | io->urbs [i]->pipe = pipe; |
| 417 | io->urbs [i]->interval = period; |
| 418 | io->urbs [i]->transfer_flags = urb_flags; |
| 419 | |
| 420 | io->urbs [i]->complete = sg_complete; |
| 421 | io->urbs [i]->context = io; |
| 422 | io->urbs [i]->status = -EINPROGRESS; |
| 423 | io->urbs [i]->actual_length = 0; |
| 424 | |
| 425 | if (dma) { |
| 426 | /* hc may use _only_ transfer_dma */ |
| 427 | io->urbs [i]->transfer_dma = sg_dma_address (sg + i); |
| 428 | len = sg_dma_len (sg + i); |
| 429 | } else { |
| 430 | /* hc may use _only_ transfer_buffer */ |
| 431 | io->urbs [i]->transfer_buffer = |
| 432 | page_address (sg [i].page) + sg [i].offset; |
| 433 | len = sg [i].length; |
| 434 | } |
| 435 | |
| 436 | if (length) { |
| 437 | len = min_t (unsigned, len, length); |
| 438 | length -= len; |
| 439 | if (length == 0) |
| 440 | io->entries = i + 1; |
| 441 | } |
| 442 | io->urbs [i]->transfer_buffer_length = len; |
| 443 | } |
| 444 | io->urbs [--i]->transfer_flags &= ~URB_NO_INTERRUPT; |
| 445 | |
| 446 | /* transaction state */ |
| 447 | io->status = 0; |
| 448 | io->bytes = 0; |
| 449 | init_completion (&io->complete); |
| 450 | return 0; |
| 451 | |
| 452 | nomem: |
| 453 | sg_clean (io); |
| 454 | return -ENOMEM; |
| 455 | } |
| 456 | |
| 457 | |
| 458 | /** |
| 459 | * usb_sg_wait - synchronously execute scatter/gather request |
| 460 | * @io: request block handle, as initialized with usb_sg_init(). |
| 461 | * some fields become accessible when this call returns. |
| 462 | * Context: !in_interrupt () |
| 463 | * |
| 464 | * This function blocks until the specified I/O operation completes. It |
| 465 | * leverages the grouping of the related I/O requests to get good transfer |
| 466 | * rates, by queueing the requests. At higher speeds, such queuing can |
| 467 | * significantly improve USB throughput. |
| 468 | * |
| 469 | * There are three kinds of completion for this function. |
| 470 | * (1) success, where io->status is zero. The number of io->bytes |
| 471 | * transferred is as requested. |
| 472 | * (2) error, where io->status is a negative errno value. The number |
| 473 | * of io->bytes transferred before the error is usually less |
| 474 | * than requested, and can be nonzero. |
Steven Cole | 093cf72 | 2005-05-03 19:07:24 -0600 | [diff] [blame] | 475 | * (3) cancellation, a type of error with status -ECONNRESET that |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | * is initiated by usb_sg_cancel(). |
| 477 | * |
| 478 | * When this function returns, all memory allocated through usb_sg_init() or |
| 479 | * this call will have been freed. The request block parameter may still be |
| 480 | * passed to usb_sg_cancel(), or it may be freed. It could also be |
| 481 | * reinitialized and then reused. |
| 482 | * |
| 483 | * Data Transfer Rates: |
| 484 | * |
| 485 | * Bulk transfers are valid for full or high speed endpoints. |
| 486 | * The best full speed data rate is 19 packets of 64 bytes each |
| 487 | * per frame, or 1216 bytes per millisecond. |
| 488 | * The best high speed data rate is 13 packets of 512 bytes each |
| 489 | * per microframe, or 52 KBytes per millisecond. |
| 490 | * |
| 491 | * The reason to use interrupt transfers through this API would most likely |
| 492 | * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond |
| 493 | * could be transferred. That capability is less useful for low or full |
| 494 | * speed interrupt endpoints, which allow at most one packet per millisecond, |
| 495 | * of at most 8 or 64 bytes (respectively). |
| 496 | */ |
| 497 | void usb_sg_wait (struct usb_sg_request *io) |
| 498 | { |
| 499 | int i, entries = io->entries; |
| 500 | |
| 501 | /* queue the urbs. */ |
| 502 | spin_lock_irq (&io->lock); |
| 503 | for (i = 0; i < entries && !io->status; i++) { |
| 504 | int retval; |
| 505 | |
| 506 | io->urbs [i]->dev = io->dev; |
| 507 | retval = usb_submit_urb (io->urbs [i], SLAB_ATOMIC); |
| 508 | |
| 509 | /* after we submit, let completions or cancelations fire; |
| 510 | * we handshake using io->status. |
| 511 | */ |
| 512 | spin_unlock_irq (&io->lock); |
| 513 | switch (retval) { |
| 514 | /* maybe we retrying will recover */ |
| 515 | case -ENXIO: // hc didn't queue this one |
| 516 | case -EAGAIN: |
| 517 | case -ENOMEM: |
| 518 | io->urbs[i]->dev = NULL; |
| 519 | retval = 0; |
| 520 | i--; |
| 521 | yield (); |
| 522 | break; |
| 523 | |
| 524 | /* no error? continue immediately. |
| 525 | * |
| 526 | * NOTE: to work better with UHCI (4K I/O buffer may |
| 527 | * need 3K of TDs) it may be good to limit how many |
| 528 | * URBs are queued at once; N milliseconds? |
| 529 | */ |
| 530 | case 0: |
| 531 | cpu_relax (); |
| 532 | break; |
| 533 | |
| 534 | /* fail any uncompleted urbs */ |
| 535 | default: |
| 536 | io->urbs [i]->dev = NULL; |
| 537 | io->urbs [i]->status = retval; |
| 538 | dev_dbg (&io->dev->dev, "%s, submit --> %d\n", |
| 539 | __FUNCTION__, retval); |
| 540 | usb_sg_cancel (io); |
| 541 | } |
| 542 | spin_lock_irq (&io->lock); |
| 543 | if (retval && (io->status == 0 || io->status == -ECONNRESET)) |
| 544 | io->status = retval; |
| 545 | } |
| 546 | io->count -= entries - i; |
| 547 | if (io->count == 0) |
| 548 | complete (&io->complete); |
| 549 | spin_unlock_irq (&io->lock); |
| 550 | |
| 551 | /* OK, yes, this could be packaged as non-blocking. |
| 552 | * So could the submit loop above ... but it's easier to |
| 553 | * solve neither problem than to solve both! |
| 554 | */ |
| 555 | wait_for_completion (&io->complete); |
| 556 | |
| 557 | sg_clean (io); |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait() |
| 562 | * @io: request block, initialized with usb_sg_init() |
| 563 | * |
| 564 | * This stops a request after it has been started by usb_sg_wait(). |
| 565 | * It can also prevents one initialized by usb_sg_init() from starting, |
| 566 | * so that call just frees resources allocated to the request. |
| 567 | */ |
| 568 | void usb_sg_cancel (struct usb_sg_request *io) |
| 569 | { |
| 570 | unsigned long flags; |
| 571 | |
| 572 | spin_lock_irqsave (&io->lock, flags); |
| 573 | |
| 574 | /* shut everything down, if it didn't already */ |
| 575 | if (!io->status) { |
| 576 | int i; |
| 577 | |
| 578 | io->status = -ECONNRESET; |
| 579 | spin_unlock (&io->lock); |
| 580 | for (i = 0; i < io->entries; i++) { |
| 581 | int retval; |
| 582 | |
| 583 | if (!io->urbs [i]->dev) |
| 584 | continue; |
| 585 | retval = usb_unlink_urb (io->urbs [i]); |
| 586 | if (retval != -EINPROGRESS && retval != -EBUSY) |
| 587 | dev_warn (&io->dev->dev, "%s, unlink --> %d\n", |
| 588 | __FUNCTION__, retval); |
| 589 | } |
| 590 | spin_lock (&io->lock); |
| 591 | } |
| 592 | spin_unlock_irqrestore (&io->lock, flags); |
| 593 | } |
| 594 | |
| 595 | /*-------------------------------------------------------------------*/ |
| 596 | |
| 597 | /** |
| 598 | * usb_get_descriptor - issues a generic GET_DESCRIPTOR request |
| 599 | * @dev: the device whose descriptor is being retrieved |
| 600 | * @type: the descriptor type (USB_DT_*) |
| 601 | * @index: the number of the descriptor |
| 602 | * @buf: where to put the descriptor |
| 603 | * @size: how big is "buf"? |
| 604 | * Context: !in_interrupt () |
| 605 | * |
| 606 | * Gets a USB descriptor. Convenience functions exist to simplify |
| 607 | * getting some types of descriptors. Use |
| 608 | * usb_get_string() or usb_string() for USB_DT_STRING. |
| 609 | * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG) |
| 610 | * are part of the device structure. |
| 611 | * In addition to a number of USB-standard descriptors, some |
| 612 | * devices also use class-specific or vendor-specific descriptors. |
| 613 | * |
| 614 | * This call is synchronous, and may not be used in an interrupt context. |
| 615 | * |
| 616 | * Returns the number of bytes received on success, or else the status code |
| 617 | * returned by the underlying usb_control_msg() call. |
| 618 | */ |
| 619 | int usb_get_descriptor(struct usb_device *dev, unsigned char type, unsigned char index, void *buf, int size) |
| 620 | { |
| 621 | int i; |
| 622 | int result; |
| 623 | |
| 624 | memset(buf,0,size); // Make sure we parse really received data |
| 625 | |
| 626 | for (i = 0; i < 3; ++i) { |
| 627 | /* retry on length 0 or stall; some devices are flakey */ |
| 628 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 629 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, |
| 630 | (type << 8) + index, 0, buf, size, |
| 631 | USB_CTRL_GET_TIMEOUT); |
| 632 | if (result == 0 || result == -EPIPE) |
| 633 | continue; |
| 634 | if (result > 1 && ((u8 *)buf)[1] != type) { |
| 635 | result = -EPROTO; |
| 636 | continue; |
| 637 | } |
| 638 | break; |
| 639 | } |
| 640 | return result; |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * usb_get_string - gets a string descriptor |
| 645 | * @dev: the device whose string descriptor is being retrieved |
| 646 | * @langid: code for language chosen (from string descriptor zero) |
| 647 | * @index: the number of the descriptor |
| 648 | * @buf: where to put the string |
| 649 | * @size: how big is "buf"? |
| 650 | * Context: !in_interrupt () |
| 651 | * |
| 652 | * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character, |
| 653 | * in little-endian byte order). |
| 654 | * The usb_string() function will often be a convenient way to turn |
| 655 | * these strings into kernel-printable form. |
| 656 | * |
| 657 | * Strings may be referenced in device, configuration, interface, or other |
| 658 | * descriptors, and could also be used in vendor-specific ways. |
| 659 | * |
| 660 | * This call is synchronous, and may not be used in an interrupt context. |
| 661 | * |
| 662 | * Returns the number of bytes received on success, or else the status code |
| 663 | * returned by the underlying usb_control_msg() call. |
| 664 | */ |
Adrian Bunk | e266a12 | 2005-11-08 21:05:43 +0100 | [diff] [blame] | 665 | static int usb_get_string(struct usb_device *dev, unsigned short langid, |
| 666 | unsigned char index, void *buf, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | { |
| 668 | int i; |
| 669 | int result; |
| 670 | |
| 671 | for (i = 0; i < 3; ++i) { |
| 672 | /* retry on length 0 or stall; some devices are flakey */ |
| 673 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 674 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, |
| 675 | (USB_DT_STRING << 8) + index, langid, buf, size, |
| 676 | USB_CTRL_GET_TIMEOUT); |
| 677 | if (!(result == 0 || result == -EPIPE)) |
| 678 | break; |
| 679 | } |
| 680 | return result; |
| 681 | } |
| 682 | |
| 683 | static void usb_try_string_workarounds(unsigned char *buf, int *length) |
| 684 | { |
| 685 | int newlength, oldlength = *length; |
| 686 | |
| 687 | for (newlength = 2; newlength + 1 < oldlength; newlength += 2) |
| 688 | if (!isprint(buf[newlength]) || buf[newlength + 1]) |
| 689 | break; |
| 690 | |
| 691 | if (newlength > 2) { |
| 692 | buf[0] = newlength; |
| 693 | *length = newlength; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | static int usb_string_sub(struct usb_device *dev, unsigned int langid, |
| 698 | unsigned int index, unsigned char *buf) |
| 699 | { |
| 700 | int rc; |
| 701 | |
| 702 | /* Try to read the string descriptor by asking for the maximum |
| 703 | * possible number of bytes */ |
| 704 | rc = usb_get_string(dev, langid, index, buf, 255); |
| 705 | |
| 706 | /* If that failed try to read the descriptor length, then |
| 707 | * ask for just that many bytes */ |
| 708 | if (rc < 2) { |
| 709 | rc = usb_get_string(dev, langid, index, buf, 2); |
| 710 | if (rc == 2) |
| 711 | rc = usb_get_string(dev, langid, index, buf, buf[0]); |
| 712 | } |
| 713 | |
| 714 | if (rc >= 2) { |
| 715 | if (!buf[0] && !buf[1]) |
| 716 | usb_try_string_workarounds(buf, &rc); |
| 717 | |
| 718 | /* There might be extra junk at the end of the descriptor */ |
| 719 | if (buf[0] < rc) |
| 720 | rc = buf[0]; |
| 721 | |
| 722 | rc = rc - (rc & 1); /* force a multiple of two */ |
| 723 | } |
| 724 | |
| 725 | if (rc < 2) |
| 726 | rc = (rc < 0 ? rc : -EINVAL); |
| 727 | |
| 728 | return rc; |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * usb_string - returns ISO 8859-1 version of a string descriptor |
| 733 | * @dev: the device whose string descriptor is being retrieved |
| 734 | * @index: the number of the descriptor |
| 735 | * @buf: where to put the string |
| 736 | * @size: how big is "buf"? |
| 737 | * Context: !in_interrupt () |
| 738 | * |
| 739 | * This converts the UTF-16LE encoded strings returned by devices, from |
| 740 | * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones |
| 741 | * that are more usable in most kernel contexts. Note that all characters |
| 742 | * in the chosen descriptor that can't be encoded using ISO-8859-1 |
| 743 | * are converted to the question mark ("?") character, and this function |
| 744 | * chooses strings in the first language supported by the device. |
| 745 | * |
| 746 | * The ASCII (or, redundantly, "US-ASCII") character set is the seven-bit |
| 747 | * subset of ISO 8859-1. ISO-8859-1 is the eight-bit subset of Unicode, |
| 748 | * and is appropriate for use many uses of English and several other |
| 749 | * Western European languages. (But it doesn't include the "Euro" symbol.) |
| 750 | * |
| 751 | * This call is synchronous, and may not be used in an interrupt context. |
| 752 | * |
| 753 | * Returns length of the string (>= 0) or usb_control_msg status (< 0). |
| 754 | */ |
| 755 | int usb_string(struct usb_device *dev, int index, char *buf, size_t size) |
| 756 | { |
| 757 | unsigned char *tbuf; |
| 758 | int err; |
| 759 | unsigned int u, idx; |
| 760 | |
| 761 | if (dev->state == USB_STATE_SUSPENDED) |
| 762 | return -EHOSTUNREACH; |
| 763 | if (size <= 0 || !buf || !index) |
| 764 | return -EINVAL; |
| 765 | buf[0] = 0; |
| 766 | tbuf = kmalloc(256, GFP_KERNEL); |
| 767 | if (!tbuf) |
| 768 | return -ENOMEM; |
| 769 | |
| 770 | /* get langid for strings if it's not yet known */ |
| 771 | if (!dev->have_langid) { |
| 772 | err = usb_string_sub(dev, 0, 0, tbuf); |
| 773 | if (err < 0) { |
| 774 | dev_err (&dev->dev, |
| 775 | "string descriptor 0 read error: %d\n", |
| 776 | err); |
| 777 | goto errout; |
| 778 | } else if (err < 4) { |
| 779 | dev_err (&dev->dev, "string descriptor 0 too short\n"); |
| 780 | err = -EINVAL; |
| 781 | goto errout; |
| 782 | } else { |
| 783 | dev->have_langid = -1; |
| 784 | dev->string_langid = tbuf[2] | (tbuf[3]<< 8); |
| 785 | /* always use the first langid listed */ |
| 786 | dev_dbg (&dev->dev, "default language 0x%04x\n", |
| 787 | dev->string_langid); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | err = usb_string_sub(dev, dev->string_langid, index, tbuf); |
| 792 | if (err < 0) |
| 793 | goto errout; |
| 794 | |
| 795 | size--; /* leave room for trailing NULL char in output buffer */ |
| 796 | for (idx = 0, u = 2; u < err; u += 2) { |
| 797 | if (idx >= size) |
| 798 | break; |
| 799 | if (tbuf[u+1]) /* high byte */ |
| 800 | buf[idx++] = '?'; /* non ISO-8859-1 character */ |
| 801 | else |
| 802 | buf[idx++] = tbuf[u]; |
| 803 | } |
| 804 | buf[idx] = 0; |
| 805 | err = idx; |
| 806 | |
| 807 | if (tbuf[1] != USB_DT_STRING) |
| 808 | dev_dbg(&dev->dev, "wrong descriptor type %02x for string %d (\"%s\")\n", tbuf[1], index, buf); |
| 809 | |
| 810 | errout: |
| 811 | kfree(tbuf); |
| 812 | return err; |
| 813 | } |
| 814 | |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 815 | /** |
| 816 | * usb_cache_string - read a string descriptor and cache it for later use |
| 817 | * @udev: the device whose string descriptor is being read |
| 818 | * @index: the descriptor index |
| 819 | * |
| 820 | * Returns a pointer to a kmalloc'ed buffer containing the descriptor string, |
| 821 | * or NULL if the index is 0 or the string could not be read. |
| 822 | */ |
| 823 | char *usb_cache_string(struct usb_device *udev, int index) |
| 824 | { |
| 825 | char *buf; |
| 826 | char *smallbuf = NULL; |
| 827 | int len; |
| 828 | |
| 829 | if (index > 0 && (buf = kmalloc(256, GFP_KERNEL)) != NULL) { |
| 830 | if ((len = usb_string(udev, index, buf, 256)) > 0) { |
| 831 | if ((smallbuf = kmalloc(++len, GFP_KERNEL)) == NULL) |
| 832 | return buf; |
| 833 | memcpy(smallbuf, buf, len); |
| 834 | } |
| 835 | kfree(buf); |
| 836 | } |
| 837 | return smallbuf; |
| 838 | } |
| 839 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | /* |
| 841 | * usb_get_device_descriptor - (re)reads the device descriptor (usbcore) |
| 842 | * @dev: the device whose device descriptor is being updated |
| 843 | * @size: how much of the descriptor to read |
| 844 | * Context: !in_interrupt () |
| 845 | * |
| 846 | * Updates the copy of the device descriptor stored in the device structure, |
| 847 | * which dedicates space for this purpose. Note that several fields are |
| 848 | * converted to the host CPU's byte order: the USB version (bcdUSB), and |
| 849 | * vendors product and version fields (idVendor, idProduct, and bcdDevice). |
| 850 | * That lets device drivers compare against non-byteswapped constants. |
| 851 | * |
| 852 | * Not exported, only for use by the core. If drivers really want to read |
| 853 | * the device descriptor directly, they can call usb_get_descriptor() with |
| 854 | * type = USB_DT_DEVICE and index = 0. |
| 855 | * |
| 856 | * This call is synchronous, and may not be used in an interrupt context. |
| 857 | * |
| 858 | * Returns the number of bytes received on success, or else the status code |
| 859 | * returned by the underlying usb_control_msg() call. |
| 860 | */ |
| 861 | int usb_get_device_descriptor(struct usb_device *dev, unsigned int size) |
| 862 | { |
| 863 | struct usb_device_descriptor *desc; |
| 864 | int ret; |
| 865 | |
| 866 | if (size > sizeof(*desc)) |
| 867 | return -EINVAL; |
| 868 | desc = kmalloc(sizeof(*desc), GFP_NOIO); |
| 869 | if (!desc) |
| 870 | return -ENOMEM; |
| 871 | |
| 872 | ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size); |
| 873 | if (ret >= 0) |
| 874 | memcpy(&dev->descriptor, desc, size); |
| 875 | kfree(desc); |
| 876 | return ret; |
| 877 | } |
| 878 | |
| 879 | /** |
| 880 | * usb_get_status - issues a GET_STATUS call |
| 881 | * @dev: the device whose status is being checked |
| 882 | * @type: USB_RECIP_*; for device, interface, or endpoint |
| 883 | * @target: zero (for device), else interface or endpoint number |
| 884 | * @data: pointer to two bytes of bitmap data |
| 885 | * Context: !in_interrupt () |
| 886 | * |
| 887 | * Returns device, interface, or endpoint status. Normally only of |
| 888 | * interest to see if the device is self powered, or has enabled the |
| 889 | * remote wakeup facility; or whether a bulk or interrupt endpoint |
| 890 | * is halted ("stalled"). |
| 891 | * |
| 892 | * Bits in these status bitmaps are set using the SET_FEATURE request, |
| 893 | * and cleared using the CLEAR_FEATURE request. The usb_clear_halt() |
| 894 | * function should be used to clear halt ("stall") status. |
| 895 | * |
| 896 | * This call is synchronous, and may not be used in an interrupt context. |
| 897 | * |
| 898 | * Returns the number of bytes received on success, or else the status code |
| 899 | * returned by the underlying usb_control_msg() call. |
| 900 | */ |
| 901 | int usb_get_status(struct usb_device *dev, int type, int target, void *data) |
| 902 | { |
| 903 | int ret; |
| 904 | u16 *status = kmalloc(sizeof(*status), GFP_KERNEL); |
| 905 | |
| 906 | if (!status) |
| 907 | return -ENOMEM; |
| 908 | |
| 909 | ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 910 | USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status, |
| 911 | sizeof(*status), USB_CTRL_GET_TIMEOUT); |
| 912 | |
| 913 | *(u16 *)data = *status; |
| 914 | kfree(status); |
| 915 | return ret; |
| 916 | } |
| 917 | |
| 918 | /** |
| 919 | * usb_clear_halt - tells device to clear endpoint halt/stall condition |
| 920 | * @dev: device whose endpoint is halted |
| 921 | * @pipe: endpoint "pipe" being cleared |
| 922 | * Context: !in_interrupt () |
| 923 | * |
| 924 | * This is used to clear halt conditions for bulk and interrupt endpoints, |
| 925 | * as reported by URB completion status. Endpoints that are halted are |
| 926 | * sometimes referred to as being "stalled". Such endpoints are unable |
| 927 | * to transmit or receive data until the halt status is cleared. Any URBs |
| 928 | * queued for such an endpoint should normally be unlinked by the driver |
| 929 | * before clearing the halt condition, as described in sections 5.7.5 |
| 930 | * and 5.8.5 of the USB 2.0 spec. |
| 931 | * |
| 932 | * Note that control and isochronous endpoints don't halt, although control |
| 933 | * endpoints report "protocol stall" (for unsupported requests) using the |
| 934 | * same status code used to report a true stall. |
| 935 | * |
| 936 | * This call is synchronous, and may not be used in an interrupt context. |
| 937 | * |
| 938 | * Returns zero on success, or else the status code returned by the |
| 939 | * underlying usb_control_msg() call. |
| 940 | */ |
| 941 | int usb_clear_halt(struct usb_device *dev, int pipe) |
| 942 | { |
| 943 | int result; |
| 944 | int endp = usb_pipeendpoint(pipe); |
| 945 | |
| 946 | if (usb_pipein (pipe)) |
| 947 | endp |= USB_DIR_IN; |
| 948 | |
| 949 | /* we don't care if it wasn't halted first. in fact some devices |
| 950 | * (like some ibmcam model 1 units) seem to expect hosts to make |
| 951 | * this request for iso endpoints, which can't halt! |
| 952 | */ |
| 953 | result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 954 | USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, |
| 955 | USB_ENDPOINT_HALT, endp, NULL, 0, |
| 956 | USB_CTRL_SET_TIMEOUT); |
| 957 | |
| 958 | /* don't un-halt or force to DATA0 except on success */ |
| 959 | if (result < 0) |
| 960 | return result; |
| 961 | |
| 962 | /* NOTE: seems like Microsoft and Apple don't bother verifying |
| 963 | * the clear "took", so some devices could lock up if you check... |
| 964 | * such as the Hagiwara FlashGate DUAL. So we won't bother. |
| 965 | * |
| 966 | * NOTE: make sure the logic here doesn't diverge much from |
| 967 | * the copy in usb-storage, for as long as we need two copies. |
| 968 | */ |
| 969 | |
| 970 | /* toggle was reset by the clear */ |
| 971 | usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0); |
| 972 | |
| 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | /** |
| 977 | * usb_disable_endpoint -- Disable an endpoint by address |
| 978 | * @dev: the device whose endpoint is being disabled |
| 979 | * @epaddr: the endpoint's address. Endpoint number for output, |
| 980 | * endpoint number + USB_DIR_IN for input |
| 981 | * |
| 982 | * Deallocates hcd/hardware state for this endpoint ... and nukes all |
| 983 | * pending urbs. |
| 984 | * |
| 985 | * If the HCD hasn't registered a disable() function, this sets the |
| 986 | * endpoint's maxpacket size to 0 to prevent further submissions. |
| 987 | */ |
| 988 | void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr) |
| 989 | { |
| 990 | unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK; |
| 991 | struct usb_host_endpoint *ep; |
| 992 | |
| 993 | if (!dev) |
| 994 | return; |
| 995 | |
| 996 | if (usb_endpoint_out(epaddr)) { |
| 997 | ep = dev->ep_out[epnum]; |
| 998 | dev->ep_out[epnum] = NULL; |
| 999 | } else { |
| 1000 | ep = dev->ep_in[epnum]; |
| 1001 | dev->ep_in[epnum] = NULL; |
| 1002 | } |
| 1003 | if (ep && dev->bus && dev->bus->op && dev->bus->op->disable) |
| 1004 | dev->bus->op->disable(dev, ep); |
| 1005 | } |
| 1006 | |
| 1007 | /** |
| 1008 | * usb_disable_interface -- Disable all endpoints for an interface |
| 1009 | * @dev: the device whose interface is being disabled |
| 1010 | * @intf: pointer to the interface descriptor |
| 1011 | * |
| 1012 | * Disables all the endpoints for the interface's current altsetting. |
| 1013 | */ |
| 1014 | void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf) |
| 1015 | { |
| 1016 | struct usb_host_interface *alt = intf->cur_altsetting; |
| 1017 | int i; |
| 1018 | |
| 1019 | for (i = 0; i < alt->desc.bNumEndpoints; ++i) { |
| 1020 | usb_disable_endpoint(dev, |
| 1021 | alt->endpoint[i].desc.bEndpointAddress); |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | /* |
| 1026 | * usb_disable_device - Disable all the endpoints for a USB device |
| 1027 | * @dev: the device whose endpoints are being disabled |
| 1028 | * @skip_ep0: 0 to disable endpoint 0, 1 to skip it. |
| 1029 | * |
| 1030 | * Disables all the device's endpoints, potentially including endpoint 0. |
| 1031 | * Deallocates hcd/hardware state for the endpoints (nuking all or most |
| 1032 | * pending urbs) and usbcore state for the interfaces, so that usbcore |
| 1033 | * must usb_set_configuration() before any interfaces could be used. |
| 1034 | */ |
| 1035 | void usb_disable_device(struct usb_device *dev, int skip_ep0) |
| 1036 | { |
| 1037 | int i; |
| 1038 | |
| 1039 | dev_dbg(&dev->dev, "%s nuking %s URBs\n", __FUNCTION__, |
| 1040 | skip_ep0 ? "non-ep0" : "all"); |
| 1041 | for (i = skip_ep0; i < 16; ++i) { |
| 1042 | usb_disable_endpoint(dev, i); |
| 1043 | usb_disable_endpoint(dev, i + USB_DIR_IN); |
| 1044 | } |
| 1045 | dev->toggle[0] = dev->toggle[1] = 0; |
| 1046 | |
| 1047 | /* getting rid of interfaces will disconnect |
| 1048 | * any drivers bound to them (a key side effect) |
| 1049 | */ |
| 1050 | if (dev->actconfig) { |
| 1051 | for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { |
| 1052 | struct usb_interface *interface; |
| 1053 | |
Alan Stern | 86d3074 | 2005-07-29 12:17:16 -0700 | [diff] [blame] | 1054 | /* remove this interface if it has been registered */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | interface = dev->actconfig->interface[i]; |
Daniel Ritz | d305ef5 | 2005-09-22 00:47:24 -0700 | [diff] [blame] | 1056 | if (!device_is_registered(&interface->dev)) |
Alan Stern | 86d3074 | 2005-07-29 12:17:16 -0700 | [diff] [blame] | 1057 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | dev_dbg (&dev->dev, "unregistering interface %s\n", |
| 1059 | interface->dev.bus_id); |
| 1060 | usb_remove_sysfs_intf_files(interface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | device_del (&interface->dev); |
| 1062 | } |
| 1063 | |
| 1064 | /* Now that the interfaces are unbound, nobody should |
| 1065 | * try to access them. |
| 1066 | */ |
| 1067 | for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { |
| 1068 | put_device (&dev->actconfig->interface[i]->dev); |
| 1069 | dev->actconfig->interface[i] = NULL; |
| 1070 | } |
| 1071 | dev->actconfig = NULL; |
| 1072 | if (dev->state == USB_STATE_CONFIGURED) |
| 1073 | usb_set_device_state(dev, USB_STATE_ADDRESS); |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | |
| 1078 | /* |
| 1079 | * usb_enable_endpoint - Enable an endpoint for USB communications |
| 1080 | * @dev: the device whose interface is being enabled |
| 1081 | * @ep: the endpoint |
| 1082 | * |
| 1083 | * Resets the endpoint toggle, and sets dev->ep_{in,out} pointers. |
| 1084 | * For control endpoints, both the input and output sides are handled. |
| 1085 | */ |
| 1086 | static void |
| 1087 | usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep) |
| 1088 | { |
| 1089 | unsigned int epaddr = ep->desc.bEndpointAddress; |
| 1090 | unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK; |
| 1091 | int is_control; |
| 1092 | |
| 1093 | is_control = ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) |
| 1094 | == USB_ENDPOINT_XFER_CONTROL); |
| 1095 | if (usb_endpoint_out(epaddr) || is_control) { |
| 1096 | usb_settoggle(dev, epnum, 1, 0); |
| 1097 | dev->ep_out[epnum] = ep; |
| 1098 | } |
| 1099 | if (!usb_endpoint_out(epaddr) || is_control) { |
| 1100 | usb_settoggle(dev, epnum, 0, 0); |
| 1101 | dev->ep_in[epnum] = ep; |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | /* |
| 1106 | * usb_enable_interface - Enable all the endpoints for an interface |
| 1107 | * @dev: the device whose interface is being enabled |
| 1108 | * @intf: pointer to the interface descriptor |
| 1109 | * |
| 1110 | * Enables all the endpoints for the interface's current altsetting. |
| 1111 | */ |
| 1112 | static void usb_enable_interface(struct usb_device *dev, |
| 1113 | struct usb_interface *intf) |
| 1114 | { |
| 1115 | struct usb_host_interface *alt = intf->cur_altsetting; |
| 1116 | int i; |
| 1117 | |
| 1118 | for (i = 0; i < alt->desc.bNumEndpoints; ++i) |
| 1119 | usb_enable_endpoint(dev, &alt->endpoint[i]); |
| 1120 | } |
| 1121 | |
| 1122 | /** |
| 1123 | * usb_set_interface - Makes a particular alternate setting be current |
| 1124 | * @dev: the device whose interface is being updated |
| 1125 | * @interface: the interface being updated |
| 1126 | * @alternate: the setting being chosen. |
| 1127 | * Context: !in_interrupt () |
| 1128 | * |
| 1129 | * This is used to enable data transfers on interfaces that may not |
| 1130 | * be enabled by default. Not all devices support such configurability. |
| 1131 | * Only the driver bound to an interface may change its setting. |
| 1132 | * |
| 1133 | * Within any given configuration, each interface may have several |
| 1134 | * alternative settings. These are often used to control levels of |
| 1135 | * bandwidth consumption. For example, the default setting for a high |
| 1136 | * speed interrupt endpoint may not send more than 64 bytes per microframe, |
| 1137 | * while interrupt transfers of up to 3KBytes per microframe are legal. |
| 1138 | * Also, isochronous endpoints may never be part of an |
| 1139 | * interface's default setting. To access such bandwidth, alternate |
| 1140 | * interface settings must be made current. |
| 1141 | * |
| 1142 | * Note that in the Linux USB subsystem, bandwidth associated with |
| 1143 | * an endpoint in a given alternate setting is not reserved until an URB |
| 1144 | * is submitted that needs that bandwidth. Some other operating systems |
| 1145 | * allocate bandwidth early, when a configuration is chosen. |
| 1146 | * |
| 1147 | * This call is synchronous, and may not be used in an interrupt context. |
| 1148 | * Also, drivers must not change altsettings while urbs are scheduled for |
| 1149 | * endpoints in that interface; all such urbs must first be completed |
| 1150 | * (perhaps forced by unlinking). |
| 1151 | * |
| 1152 | * Returns zero on success, or else the status code returned by the |
| 1153 | * underlying usb_control_msg() call. |
| 1154 | */ |
| 1155 | int usb_set_interface(struct usb_device *dev, int interface, int alternate) |
| 1156 | { |
| 1157 | struct usb_interface *iface; |
| 1158 | struct usb_host_interface *alt; |
| 1159 | int ret; |
| 1160 | int manual = 0; |
| 1161 | |
| 1162 | if (dev->state == USB_STATE_SUSPENDED) |
| 1163 | return -EHOSTUNREACH; |
| 1164 | |
| 1165 | iface = usb_ifnum_to_if(dev, interface); |
| 1166 | if (!iface) { |
| 1167 | dev_dbg(&dev->dev, "selecting invalid interface %d\n", |
| 1168 | interface); |
| 1169 | return -EINVAL; |
| 1170 | } |
| 1171 | |
| 1172 | alt = usb_altnum_to_altsetting(iface, alternate); |
| 1173 | if (!alt) { |
| 1174 | warn("selecting invalid altsetting %d", alternate); |
| 1175 | return -EINVAL; |
| 1176 | } |
| 1177 | |
| 1178 | ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 1179 | USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE, |
| 1180 | alternate, interface, NULL, 0, 5000); |
| 1181 | |
| 1182 | /* 9.4.10 says devices don't need this and are free to STALL the |
| 1183 | * request if the interface only has one alternate setting. |
| 1184 | */ |
| 1185 | if (ret == -EPIPE && iface->num_altsetting == 1) { |
| 1186 | dev_dbg(&dev->dev, |
| 1187 | "manual set_interface for iface %d, alt %d\n", |
| 1188 | interface, alternate); |
| 1189 | manual = 1; |
| 1190 | } else if (ret < 0) |
| 1191 | return ret; |
| 1192 | |
| 1193 | /* FIXME drivers shouldn't need to replicate/bugfix the logic here |
| 1194 | * when they implement async or easily-killable versions of this or |
| 1195 | * other "should-be-internal" functions (like clear_halt). |
| 1196 | * should hcd+usbcore postprocess control requests? |
| 1197 | */ |
| 1198 | |
| 1199 | /* prevent submissions using previous endpoint settings */ |
Alan Stern | 0e6c8e8 | 2005-10-24 15:33:03 -0400 | [diff] [blame] | 1200 | if (device_is_registered(&iface->dev)) |
| 1201 | usb_remove_sysfs_intf_files(iface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | usb_disable_interface(dev, iface); |
| 1203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | iface->cur_altsetting = alt; |
| 1205 | |
| 1206 | /* If the interface only has one altsetting and the device didn't |
David Brownell | a81e7ec | 2005-04-18 17:39:25 -0700 | [diff] [blame] | 1207 | * accept the request, we attempt to carry out the equivalent action |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | * by manually clearing the HALT feature for each endpoint in the |
| 1209 | * new altsetting. |
| 1210 | */ |
| 1211 | if (manual) { |
| 1212 | int i; |
| 1213 | |
| 1214 | for (i = 0; i < alt->desc.bNumEndpoints; i++) { |
| 1215 | unsigned int epaddr = |
| 1216 | alt->endpoint[i].desc.bEndpointAddress; |
| 1217 | unsigned int pipe = |
| 1218 | __create_pipe(dev, USB_ENDPOINT_NUMBER_MASK & epaddr) |
| 1219 | | (usb_endpoint_out(epaddr) ? USB_DIR_OUT : USB_DIR_IN); |
| 1220 | |
| 1221 | usb_clear_halt(dev, pipe); |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | /* 9.1.1.5: reset toggles for all endpoints in the new altsetting |
| 1226 | * |
| 1227 | * Note: |
| 1228 | * Despite EP0 is always present in all interfaces/AS, the list of |
| 1229 | * endpoints from the descriptor does not contain EP0. Due to its |
| 1230 | * omnipresence one might expect EP0 being considered "affected" by |
| 1231 | * any SetInterface request and hence assume toggles need to be reset. |
| 1232 | * However, EP0 toggles are re-synced for every individual transfer |
| 1233 | * during the SETUP stage - hence EP0 toggles are "don't care" here. |
| 1234 | * (Likewise, EP0 never "halts" on well designed devices.) |
| 1235 | */ |
| 1236 | usb_enable_interface(dev, iface); |
Alan Stern | 0e6c8e8 | 2005-10-24 15:33:03 -0400 | [diff] [blame] | 1237 | if (device_is_registered(&iface->dev)) |
| 1238 | usb_create_sysfs_intf_files(iface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | /** |
| 1244 | * usb_reset_configuration - lightweight device reset |
| 1245 | * @dev: the device whose configuration is being reset |
| 1246 | * |
| 1247 | * This issues a standard SET_CONFIGURATION request to the device using |
| 1248 | * the current configuration. The effect is to reset most USB-related |
| 1249 | * state in the device, including interface altsettings (reset to zero), |
| 1250 | * endpoint halts (cleared), and data toggle (only for bulk and interrupt |
| 1251 | * endpoints). Other usbcore state is unchanged, including bindings of |
| 1252 | * usb device drivers to interfaces. |
| 1253 | * |
| 1254 | * Because this affects multiple interfaces, avoid using this with composite |
| 1255 | * (multi-interface) devices. Instead, the driver for each interface may |
David Brownell | a81e7ec | 2005-04-18 17:39:25 -0700 | [diff] [blame] | 1256 | * use usb_set_interface() on the interfaces it claims. Be careful though; |
| 1257 | * some devices don't support the SET_INTERFACE request, and others won't |
| 1258 | * reset all the interface state (notably data toggles). Resetting the whole |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | * configuration would affect other drivers' interfaces. |
| 1260 | * |
| 1261 | * The caller must own the device lock. |
| 1262 | * |
| 1263 | * Returns zero on success, else a negative error code. |
| 1264 | */ |
| 1265 | int usb_reset_configuration(struct usb_device *dev) |
| 1266 | { |
| 1267 | int i, retval; |
| 1268 | struct usb_host_config *config; |
| 1269 | |
| 1270 | if (dev->state == USB_STATE_SUSPENDED) |
| 1271 | return -EHOSTUNREACH; |
| 1272 | |
| 1273 | /* caller must have locked the device and must own |
| 1274 | * the usb bus readlock (so driver bindings are stable); |
| 1275 | * calls during probe() are fine |
| 1276 | */ |
| 1277 | |
| 1278 | for (i = 1; i < 16; ++i) { |
| 1279 | usb_disable_endpoint(dev, i); |
| 1280 | usb_disable_endpoint(dev, i + USB_DIR_IN); |
| 1281 | } |
| 1282 | |
| 1283 | config = dev->actconfig; |
| 1284 | retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 1285 | USB_REQ_SET_CONFIGURATION, 0, |
| 1286 | config->desc.bConfigurationValue, 0, |
| 1287 | NULL, 0, USB_CTRL_SET_TIMEOUT); |
Alan Stern | 0e6c8e8 | 2005-10-24 15:33:03 -0400 | [diff] [blame] | 1288 | if (retval < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | |
| 1291 | dev->toggle[0] = dev->toggle[1] = 0; |
| 1292 | |
| 1293 | /* re-init hc/hcd interface/endpoint state */ |
| 1294 | for (i = 0; i < config->desc.bNumInterfaces; i++) { |
| 1295 | struct usb_interface *intf = config->interface[i]; |
| 1296 | struct usb_host_interface *alt; |
| 1297 | |
Alan Stern | 0e6c8e8 | 2005-10-24 15:33:03 -0400 | [diff] [blame] | 1298 | if (device_is_registered(&intf->dev)) |
| 1299 | usb_remove_sysfs_intf_files(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | alt = usb_altnum_to_altsetting(intf, 0); |
| 1301 | |
| 1302 | /* No altsetting 0? We'll assume the first altsetting. |
| 1303 | * We could use a GetInterface call, but if a device is |
| 1304 | * so non-compliant that it doesn't have altsetting 0 |
| 1305 | * then I wouldn't trust its reply anyway. |
| 1306 | */ |
| 1307 | if (!alt) |
| 1308 | alt = &intf->altsetting[0]; |
| 1309 | |
| 1310 | intf->cur_altsetting = alt; |
| 1311 | usb_enable_interface(dev, intf); |
Alan Stern | 0e6c8e8 | 2005-10-24 15:33:03 -0400 | [diff] [blame] | 1312 | if (device_is_registered(&intf->dev)) |
| 1313 | usb_create_sysfs_intf_files(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | } |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
| 1318 | static void release_interface(struct device *dev) |
| 1319 | { |
| 1320 | struct usb_interface *intf = to_usb_interface(dev); |
| 1321 | struct usb_interface_cache *intfc = |
| 1322 | altsetting_to_usb_interface_cache(intf->altsetting); |
| 1323 | |
| 1324 | kref_put(&intfc->ref, usb_release_interface_cache); |
| 1325 | kfree(intf); |
| 1326 | } |
| 1327 | |
| 1328 | /* |
| 1329 | * usb_set_configuration - Makes a particular device setting be current |
| 1330 | * @dev: the device whose configuration is being updated |
| 1331 | * @configuration: the configuration being chosen. |
| 1332 | * Context: !in_interrupt(), caller owns the device lock |
| 1333 | * |
| 1334 | * This is used to enable non-default device modes. Not all devices |
| 1335 | * use this kind of configurability; many devices only have one |
| 1336 | * configuration. |
| 1337 | * |
| 1338 | * USB device configurations may affect Linux interoperability, |
| 1339 | * power consumption and the functionality available. For example, |
| 1340 | * the default configuration is limited to using 100mA of bus power, |
| 1341 | * so that when certain device functionality requires more power, |
| 1342 | * and the device is bus powered, that functionality should be in some |
| 1343 | * non-default device configuration. Other device modes may also be |
| 1344 | * reflected as configuration options, such as whether two ISDN |
| 1345 | * channels are available independently; and choosing between open |
| 1346 | * standard device protocols (like CDC) or proprietary ones. |
| 1347 | * |
| 1348 | * Note that USB has an additional level of device configurability, |
| 1349 | * associated with interfaces. That configurability is accessed using |
| 1350 | * usb_set_interface(). |
| 1351 | * |
| 1352 | * This call is synchronous. The calling context must be able to sleep, |
| 1353 | * must own the device lock, and must not hold the driver model's USB |
| 1354 | * bus rwsem; usb device driver probe() methods cannot use this routine. |
| 1355 | * |
| 1356 | * Returns zero on success, or else the status code returned by the |
Steven Cole | 093cf72 | 2005-05-03 19:07:24 -0600 | [diff] [blame] | 1357 | * underlying call that failed. On successful completion, each interface |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | * in the original device configuration has been destroyed, and each one |
| 1359 | * in the new configuration has been probed by all relevant usb device |
| 1360 | * drivers currently known to the kernel. |
| 1361 | */ |
| 1362 | int usb_set_configuration(struct usb_device *dev, int configuration) |
| 1363 | { |
| 1364 | int i, ret; |
| 1365 | struct usb_host_config *cp = NULL; |
| 1366 | struct usb_interface **new_interfaces = NULL; |
| 1367 | int n, nintf; |
| 1368 | |
| 1369 | for (i = 0; i < dev->descriptor.bNumConfigurations; i++) { |
| 1370 | if (dev->config[i].desc.bConfigurationValue == configuration) { |
| 1371 | cp = &dev->config[i]; |
| 1372 | break; |
| 1373 | } |
| 1374 | } |
| 1375 | if ((!cp && configuration != 0)) |
| 1376 | return -EINVAL; |
| 1377 | |
| 1378 | /* The USB spec says configuration 0 means unconfigured. |
| 1379 | * But if a device includes a configuration numbered 0, |
| 1380 | * we will accept it as a correctly configured state. |
| 1381 | */ |
| 1382 | if (cp && configuration == 0) |
| 1383 | dev_warn(&dev->dev, "config 0 descriptor??\n"); |
| 1384 | |
| 1385 | if (dev->state == USB_STATE_SUSPENDED) |
| 1386 | return -EHOSTUNREACH; |
| 1387 | |
| 1388 | /* Allocate memory for new interfaces before doing anything else, |
| 1389 | * so that if we run out then nothing will have changed. */ |
| 1390 | n = nintf = 0; |
| 1391 | if (cp) { |
| 1392 | nintf = cp->desc.bNumInterfaces; |
| 1393 | new_interfaces = kmalloc(nintf * sizeof(*new_interfaces), |
| 1394 | GFP_KERNEL); |
| 1395 | if (!new_interfaces) { |
| 1396 | dev_err(&dev->dev, "Out of memory"); |
| 1397 | return -ENOMEM; |
| 1398 | } |
| 1399 | |
| 1400 | for (; n < nintf; ++n) { |
Alan Stern | 0a1ef3b | 2005-10-24 15:38:24 -0400 | [diff] [blame] | 1401 | new_interfaces[n] = kzalloc( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | sizeof(struct usb_interface), |
| 1403 | GFP_KERNEL); |
| 1404 | if (!new_interfaces[n]) { |
| 1405 | dev_err(&dev->dev, "Out of memory"); |
| 1406 | ret = -ENOMEM; |
| 1407 | free_interfaces: |
| 1408 | while (--n >= 0) |
| 1409 | kfree(new_interfaces[n]); |
| 1410 | kfree(new_interfaces); |
| 1411 | return ret; |
| 1412 | } |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | /* if it's already configured, clear out old state first. |
| 1417 | * getting rid of old interfaces means unbinding their drivers. |
| 1418 | */ |
| 1419 | if (dev->state != USB_STATE_ADDRESS) |
| 1420 | usb_disable_device (dev, 1); // Skip ep0 |
| 1421 | |
Horst Schirmeier | f48219d | 2006-03-09 14:10:49 +0100 | [diff] [blame] | 1422 | if (cp) { |
| 1423 | i = dev->bus_mA - cp->desc.bMaxPower * 2; |
| 1424 | if (i < 0) |
| 1425 | dev_warn(&dev->dev, "new config #%d exceeds power " |
| 1426 | "limit by %dmA\n", |
| 1427 | configuration, -i); |
| 1428 | } |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 1429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 1431 | USB_REQ_SET_CONFIGURATION, 0, configuration, 0, |
| 1432 | NULL, 0, USB_CTRL_SET_TIMEOUT)) < 0) |
| 1433 | goto free_interfaces; |
| 1434 | |
| 1435 | dev->actconfig = cp; |
| 1436 | if (!cp) |
| 1437 | usb_set_device_state(dev, USB_STATE_ADDRESS); |
| 1438 | else { |
| 1439 | usb_set_device_state(dev, USB_STATE_CONFIGURED); |
| 1440 | |
| 1441 | /* Initialize the new interface structures and the |
| 1442 | * hc/hcd/usbcore interface/endpoint state. |
| 1443 | */ |
| 1444 | for (i = 0; i < nintf; ++i) { |
| 1445 | struct usb_interface_cache *intfc; |
| 1446 | struct usb_interface *intf; |
| 1447 | struct usb_host_interface *alt; |
| 1448 | |
| 1449 | cp->interface[i] = intf = new_interfaces[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | intfc = cp->intf_cache[i]; |
| 1451 | intf->altsetting = intfc->altsetting; |
| 1452 | intf->num_altsetting = intfc->num_altsetting; |
| 1453 | kref_get(&intfc->ref); |
| 1454 | |
| 1455 | alt = usb_altnum_to_altsetting(intf, 0); |
| 1456 | |
| 1457 | /* No altsetting 0? We'll assume the first altsetting. |
| 1458 | * We could use a GetInterface call, but if a device is |
| 1459 | * so non-compliant that it doesn't have altsetting 0 |
| 1460 | * then I wouldn't trust its reply anyway. |
| 1461 | */ |
| 1462 | if (!alt) |
| 1463 | alt = &intf->altsetting[0]; |
| 1464 | |
| 1465 | intf->cur_altsetting = alt; |
| 1466 | usb_enable_interface(dev, intf); |
| 1467 | intf->dev.parent = &dev->dev; |
| 1468 | intf->dev.driver = NULL; |
| 1469 | intf->dev.bus = &usb_bus_type; |
| 1470 | intf->dev.dma_mask = dev->dev.dma_mask; |
| 1471 | intf->dev.release = release_interface; |
| 1472 | device_initialize (&intf->dev); |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 1473 | mark_quiesced(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | sprintf (&intf->dev.bus_id[0], "%d-%s:%d.%d", |
| 1475 | dev->bus->busnum, dev->devpath, |
| 1476 | configuration, |
| 1477 | alt->desc.bInterfaceNumber); |
| 1478 | } |
| 1479 | kfree(new_interfaces); |
| 1480 | |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 1481 | if (cp->string == NULL) |
| 1482 | cp->string = usb_cache_string(dev, |
| 1483 | cp->desc.iConfiguration); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | |
| 1485 | /* Now that all the interfaces are set up, register them |
| 1486 | * to trigger binding of drivers to interfaces. probe() |
| 1487 | * routines may install different altsettings and may |
| 1488 | * claim() any interfaces not yet bound. Many class drivers |
| 1489 | * need that: CDC, audio, video, etc. |
| 1490 | */ |
| 1491 | for (i = 0; i < nintf; ++i) { |
| 1492 | struct usb_interface *intf = cp->interface[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | dev_dbg (&dev->dev, |
| 1495 | "adding %s (config #%d, interface %d)\n", |
| 1496 | intf->dev.bus_id, configuration, |
Dmitry Torokhov | d4ece29 | 2005-11-17 09:46:44 -0800 | [diff] [blame] | 1497 | intf->cur_altsetting->desc.bInterfaceNumber); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | ret = device_add (&intf->dev); |
| 1499 | if (ret != 0) { |
| 1500 | dev_err(&dev->dev, |
| 1501 | "device_add(%s) --> %d\n", |
| 1502 | intf->dev.bus_id, |
| 1503 | ret); |
| 1504 | continue; |
| 1505 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | usb_create_sysfs_intf_files (intf); |
| 1507 | } |
| 1508 | } |
| 1509 | |
Alan Stern | 86d3074 | 2005-07-29 12:17:16 -0700 | [diff] [blame] | 1510 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | // synchronous request completion model |
| 1514 | EXPORT_SYMBOL(usb_control_msg); |
| 1515 | EXPORT_SYMBOL(usb_bulk_msg); |
| 1516 | |
| 1517 | EXPORT_SYMBOL(usb_sg_init); |
| 1518 | EXPORT_SYMBOL(usb_sg_cancel); |
| 1519 | EXPORT_SYMBOL(usb_sg_wait); |
| 1520 | |
| 1521 | // synchronous control message convenience routines |
| 1522 | EXPORT_SYMBOL(usb_get_descriptor); |
| 1523 | EXPORT_SYMBOL(usb_get_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | EXPORT_SYMBOL(usb_string); |
| 1525 | |
| 1526 | // synchronous calls that also maintain usbcore state |
| 1527 | EXPORT_SYMBOL(usb_clear_halt); |
| 1528 | EXPORT_SYMBOL(usb_reset_configuration); |
| 1529 | EXPORT_SYMBOL(usb_set_interface); |
| 1530 | |