1 // SPDX-License-Identifier: GPL-2.0+ 2 /*****************************************************************************/ 3 4 /* 5 * devio.c -- User space communication with USB devices. 6 * 7 * Copyright (C) 1999-2000 Thomas Sailer (sailer@ife.ee.ethz.ch) 8 * 9 * This file implements the usbfs/x/y files, where 10 * x is the bus number and y the device number. 11 * 12 * It allows user space programs/"drivers" to communicate directly 13 * with USB devices without intervening kernel driver. 14 * 15 * Revision history 16 * 22.12.1999 0.1 Initial release (split from proc_usb.c) 17 * 04.01.2000 0.2 Turned into its own filesystem 18 * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery 19 * (CAN-2005-3055) 20 */ 21 22 /*****************************************************************************/ 23 24 #include <linux/fs.h> 25 #include <linux/mm.h> 26 #include <linux/sched/signal.h> 27 #include <linux/slab.h> 28 #include <linux/signal.h> 29 #include <linux/poll.h> 30 #include <linux/module.h> 31 #include <linux/string.h> 32 #include <linux/usb.h> 33 #include <linux/usbdevice_fs.h> 34 #include <linux/usb/hcd.h> /* for usbcore internals */ 35 #include <linux/cdev.h> 36 #include <linux/notifier.h> 37 #include <linux/security.h> 38 #include <linux/user_namespace.h> 39 #include <linux/scatterlist.h> 40 #include <linux/uaccess.h> 41 #include <linux/dma-mapping.h> 42 #include <asm/byteorder.h> 43 #include <linux/moduleparam.h> 44 45 #include "usb.h" 46 47 #define USB_MAXBUS 64 48 #define USB_DEVICE_MAX (USB_MAXBUS * 128) 49 #define USB_SG_SIZE 16384 /* split-size for large txs */ 50 51 /* Mutual exclusion for removal, open, and release */ 52 DEFINE_MUTEX(usbfs_mutex); 53 54 struct usb_dev_state { 55 struct list_head list; /* state list */ 56 struct usb_device *dev; 57 struct file *file; 58 spinlock_t lock; /* protects the async urb lists */ 59 struct list_head async_pending; 60 struct list_head async_completed; 61 struct list_head memory_list; 62 wait_queue_head_t wait; /* wake up if a request completed */ 63 unsigned int discsignr; 64 struct pid *disc_pid; 65 const struct cred *cred; 66 sigval_t disccontext; 67 unsigned long ifclaimed; 68 u32 disabled_bulk_eps; 69 bool privileges_dropped; 70 unsigned long interface_allowed_mask; 71 }; 72 73 struct usb_memory { 74 struct list_head memlist; 75 int vma_use_count; 76 int urb_use_count; 77 u32 size; 78 void *mem; 79 dma_addr_t dma_handle; 80 unsigned long vm_start; 81 struct usb_dev_state *ps; 82 }; 83 84 struct async { 85 struct list_head asynclist; 86 struct usb_dev_state *ps; 87 struct pid *pid; 88 const struct cred *cred; 89 unsigned int signr; 90 unsigned int ifnum; 91 void __user *userbuffer; 92 void __user *userurb; 93 sigval_t userurb_sigval; 94 struct urb *urb; 95 struct usb_memory *usbm; 96 unsigned int mem_usage; 97 int status; 98 u8 bulk_addr; 99 u8 bulk_status; 100 }; 101 102 static bool usbfs_snoop; 103 module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR); 104 MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic"); 105 106 static unsigned usbfs_snoop_max = 65536; 107 module_param(usbfs_snoop_max, uint, S_IRUGO | S_IWUSR); 108 MODULE_PARM_DESC(usbfs_snoop_max, 109 "maximum number of bytes to print while snooping"); 110 111 #define snoop(dev, format, arg...) \ 112 do { \ 113 if (usbfs_snoop) \ 114 dev_info(dev, format, ## arg); \ 115 } while (0) 116 117 enum snoop_when { 118 SUBMIT, COMPLETE 119 }; 120 121 #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0) 122 123 /* Limit on the total amount of memory we can allocate for transfers */ 124 static u32 usbfs_memory_mb = 16; 125 module_param(usbfs_memory_mb, uint, 0644); 126 MODULE_PARM_DESC(usbfs_memory_mb, 127 "maximum MB allowed for usbfs buffers (0 = no limit)"); 128 129 /* Hard limit, necessary to avoid arithmetic overflow */ 130 #define USBFS_XFER_MAX (UINT_MAX / 2 - 1000000) 131 132 static atomic64_t usbfs_memory_usage; /* Total memory currently allocated */ 133 134 /* Check whether it's okay to allocate more memory for a transfer */ 135 static int usbfs_increase_memory_usage(u64 amount) 136 { 137 u64 lim; 138 139 lim = READ_ONCE(usbfs_memory_mb); 140 lim <<= 20; 141 142 atomic64_add(amount, &usbfs_memory_usage); 143 144 if (lim > 0 && atomic64_read(&usbfs_memory_usage) > lim) { 145 atomic64_sub(amount, &usbfs_memory_usage); 146 return -ENOMEM; 147 } 148 149 return 0; 150 } 151 152 /* Memory for a transfer is being deallocated */ 153 static void usbfs_decrease_memory_usage(u64 amount) 154 { 155 atomic64_sub(amount, &usbfs_memory_usage); 156 } 157 158 static int connected(struct usb_dev_state *ps) 159 { 160 return (!list_empty(&ps->list) && 161 ps->dev->state != USB_STATE_NOTATTACHED); 162 } 163 164 static void dec_usb_memory_use_count(struct usb_memory *usbm, int *count) 165 { 166 struct usb_dev_state *ps = usbm->ps; 167 unsigned long flags; 168 169 spin_lock_irqsave(&ps->lock, flags); 170 --*count; 171 if (usbm->urb_use_count == 0 && usbm->vma_use_count == 0) { 172 list_del(&usbm->memlist); 173 spin_unlock_irqrestore(&ps->lock, flags); 174 175 usb_free_coherent(ps->dev, usbm->size, usbm->mem, 176 usbm->dma_handle); 177 usbfs_decrease_memory_usage( 178 usbm->size + sizeof(struct usb_memory)); 179 kfree(usbm); 180 } else { 181 spin_unlock_irqrestore(&ps->lock, flags); 182 } 183 } 184 185 static void usbdev_vm_open(struct vm_area_struct *vma) 186 { 187 struct usb_memory *usbm = vma->vm_private_data; 188 unsigned long flags; 189 190 spin_lock_irqsave(&usbm->ps->lock, flags); 191 ++usbm->vma_use_count; 192 spin_unlock_irqrestore(&usbm->ps->lock, flags); 193 } 194 195 static void usbdev_vm_close(struct vm_area_struct *vma) 196 { 197 struct usb_memory *usbm = vma->vm_private_data; 198 199 dec_usb_memory_use_count(usbm, &usbm->vma_use_count); 200 } 201 202 static const struct vm_operations_struct usbdev_vm_ops = { 203 .open = usbdev_vm_open, 204 .close = usbdev_vm_close 205 }; 206 207 static int usbdev_mmap(struct file *file, struct vm_area_struct *vma) 208 { 209 struct usb_memory *usbm = NULL; 210 struct usb_dev_state *ps = file->private_data; 211 size_t size = vma->vm_end - vma->vm_start; 212 void *mem; 213 unsigned long flags; 214 dma_addr_t dma_handle; 215 int ret; 216 217 ret = usbfs_increase_memory_usage(size + sizeof(struct usb_memory)); 218 if (ret) 219 goto error; 220 221 usbm = kzalloc(sizeof(struct usb_memory), GFP_KERNEL); 222 if (!usbm) { 223 ret = -ENOMEM; 224 goto error_decrease_mem; 225 } 226 227 mem = usb_alloc_coherent(ps->dev, size, GFP_USER | __GFP_NOWARN, 228 &dma_handle); 229 if (!mem) { 230 ret = -ENOMEM; 231 goto error_free_usbm; 232 } 233 234 memset(mem, 0, size); 235 236 usbm->mem = mem; 237 usbm->dma_handle = dma_handle; 238 usbm->size = size; 239 usbm->ps = ps; 240 usbm->vm_start = vma->vm_start; 241 usbm->vma_use_count = 1; 242 INIT_LIST_HEAD(&usbm->memlist); 243 244 if (remap_pfn_range(vma, vma->vm_start, 245 virt_to_phys(usbm->mem) >> PAGE_SHIFT, 246 size, vma->vm_page_prot) < 0) { 247 dec_usb_memory_use_count(usbm, &usbm->vma_use_count); 248 return -EAGAIN; 249 } 250 251 vma->vm_flags |= VM_IO; 252 vma->vm_flags |= (VM_DONTEXPAND | VM_DONTDUMP); 253 vma->vm_ops = &usbdev_vm_ops; 254 vma->vm_private_data = usbm; 255 256 spin_lock_irqsave(&ps->lock, flags); 257 list_add_tail(&usbm->memlist, &ps->memory_list); 258 spin_unlock_irqrestore(&ps->lock, flags); 259 260 return 0; 261 262 error_free_usbm: 263 kfree(usbm); 264 error_decrease_mem: 265 usbfs_decrease_memory_usage(size + sizeof(struct usb_memory)); 266 error: 267 return ret; 268 } 269 270 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes, 271 loff_t *ppos) 272 { 273 struct usb_dev_state *ps = file->private_data; 274 struct usb_device *dev = ps->dev; 275 ssize_t ret = 0; 276 unsigned len; 277 loff_t pos; 278 int i; 279 280 pos = *ppos; 281 usb_lock_device(dev); 282 if (!connected(ps)) { 283 ret = -ENODEV; 284 goto err; 285 } else if (pos < 0) { 286 ret = -EINVAL; 287 goto err; 288 } 289 290 if (pos < sizeof(struct usb_device_descriptor)) { 291 /* 18 bytes - fits on the stack */ 292 struct usb_device_descriptor temp_desc; 293 294 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor)); 295 le16_to_cpus(&temp_desc.bcdUSB); 296 le16_to_cpus(&temp_desc.idVendor); 297 le16_to_cpus(&temp_desc.idProduct); 298 le16_to_cpus(&temp_desc.bcdDevice); 299 300 len = sizeof(struct usb_device_descriptor) - pos; 301 if (len > nbytes) 302 len = nbytes; 303 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) { 304 ret = -EFAULT; 305 goto err; 306 } 307 308 *ppos += len; 309 buf += len; 310 nbytes -= len; 311 ret += len; 312 } 313 314 pos = sizeof(struct usb_device_descriptor); 315 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) { 316 struct usb_config_descriptor *config = 317 (struct usb_config_descriptor *)dev->rawdescriptors[i]; 318 unsigned int length = le16_to_cpu(config->wTotalLength); 319 320 if (*ppos < pos + length) { 321 322 /* The descriptor may claim to be longer than it 323 * really is. Here is the actual allocated length. */ 324 unsigned alloclen = 325 le16_to_cpu(dev->config[i].desc.wTotalLength); 326 327 len = length - (*ppos - pos); 328 if (len > nbytes) 329 len = nbytes; 330 331 /* Simply don't write (skip over) unallocated parts */ 332 if (alloclen > (*ppos - pos)) { 333 alloclen -= (*ppos - pos); 334 if (copy_to_user(buf, 335 dev->rawdescriptors[i] + (*ppos - pos), 336 min(len, alloclen))) { 337 ret = -EFAULT; 338 goto err; 339 } 340 } 341 342 *ppos += len; 343 buf += len; 344 nbytes -= len; 345 ret += len; 346 } 347 348 pos += length; 349 } 350 351 err: 352 usb_unlock_device(dev); 353 return ret; 354 } 355 356 /* 357 * async list handling 358 */ 359 360 static struct async *alloc_async(unsigned int numisoframes) 361 { 362 struct async *as; 363 364 as = kzalloc(sizeof(struct async), GFP_KERNEL); 365 if (!as) 366 return NULL; 367 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL); 368 if (!as->urb) { 369 kfree(as); 370 return NULL; 371 } 372 return as; 373 } 374 375 static void free_async(struct async *as) 376 { 377 int i; 378 379 put_pid(as->pid); 380 if (as->cred) 381 put_cred(as->cred); 382 for (i = 0; i < as->urb->num_sgs; i++) { 383 if (sg_page(&as->urb->sg[i])) 384 kfree(sg_virt(&as->urb->sg[i])); 385 } 386 387 kfree(as->urb->sg); 388 if (as->usbm == NULL) 389 kfree(as->urb->transfer_buffer); 390 else 391 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count); 392 393 kfree(as->urb->setup_packet); 394 usb_free_urb(as->urb); 395 usbfs_decrease_memory_usage(as->mem_usage); 396 kfree(as); 397 } 398 399 static void async_newpending(struct async *as) 400 { 401 struct usb_dev_state *ps = as->ps; 402 unsigned long flags; 403 404 spin_lock_irqsave(&ps->lock, flags); 405 list_add_tail(&as->asynclist, &ps->async_pending); 406 spin_unlock_irqrestore(&ps->lock, flags); 407 } 408 409 static void async_removepending(struct async *as) 410 { 411 struct usb_dev_state *ps = as->ps; 412 unsigned long flags; 413 414 spin_lock_irqsave(&ps->lock, flags); 415 list_del_init(&as->asynclist); 416 spin_unlock_irqrestore(&ps->lock, flags); 417 } 418 419 static struct async *async_getcompleted(struct usb_dev_state *ps) 420 { 421 unsigned long flags; 422 struct async *as = NULL; 423 424 spin_lock_irqsave(&ps->lock, flags); 425 if (!list_empty(&ps->async_completed)) { 426 as = list_entry(ps->async_completed.next, struct async, 427 asynclist); 428 list_del_init(&as->asynclist); 429 } 430 spin_unlock_irqrestore(&ps->lock, flags); 431 return as; 432 } 433 434 static struct async *async_getpending(struct usb_dev_state *ps, 435 void __user *userurb) 436 { 437 struct async *as; 438 439 list_for_each_entry(as, &ps->async_pending, asynclist) 440 if (as->userurb == userurb) { 441 list_del_init(&as->asynclist); 442 return as; 443 } 444 445 return NULL; 446 } 447 448 static void snoop_urb(struct usb_device *udev, 449 void __user *userurb, int pipe, unsigned length, 450 int timeout_or_status, enum snoop_when when, 451 unsigned char *data, unsigned data_len) 452 { 453 static const char *types[] = {"isoc", "int", "ctrl", "bulk"}; 454 static const char *dirs[] = {"out", "in"}; 455 int ep; 456 const char *t, *d; 457 458 if (!usbfs_snoop) 459 return; 460 461 ep = usb_pipeendpoint(pipe); 462 t = types[usb_pipetype(pipe)]; 463 d = dirs[!!usb_pipein(pipe)]; 464 465 if (userurb) { /* Async */ 466 if (when == SUBMIT) 467 dev_info(&udev->dev, "userurb %pK, ep%d %s-%s, " 468 "length %u\n", 469 userurb, ep, t, d, length); 470 else 471 dev_info(&udev->dev, "userurb %pK, ep%d %s-%s, " 472 "actual_length %u status %d\n", 473 userurb, ep, t, d, length, 474 timeout_or_status); 475 } else { 476 if (when == SUBMIT) 477 dev_info(&udev->dev, "ep%d %s-%s, length %u, " 478 "timeout %d\n", 479 ep, t, d, length, timeout_or_status); 480 else 481 dev_info(&udev->dev, "ep%d %s-%s, actual_length %u, " 482 "status %d\n", 483 ep, t, d, length, timeout_or_status); 484 } 485 486 data_len = min(data_len, usbfs_snoop_max); 487 if (data && data_len > 0) { 488 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1, 489 data, data_len, 1); 490 } 491 } 492 493 static void snoop_urb_data(struct urb *urb, unsigned len) 494 { 495 int i, size; 496 497 len = min(len, usbfs_snoop_max); 498 if (!usbfs_snoop || len == 0) 499 return; 500 501 if (urb->num_sgs == 0) { 502 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1, 503 urb->transfer_buffer, len, 1); 504 return; 505 } 506 507 for (i = 0; i < urb->num_sgs && len; i++) { 508 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len; 509 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1, 510 sg_virt(&urb->sg[i]), size, 1); 511 len -= size; 512 } 513 } 514 515 static int copy_urb_data_to_user(u8 __user *userbuffer, struct urb *urb) 516 { 517 unsigned i, len, size; 518 519 if (urb->number_of_packets > 0) /* Isochronous */ 520 len = urb->transfer_buffer_length; 521 else /* Non-Isoc */ 522 len = urb->actual_length; 523 524 if (urb->num_sgs == 0) { 525 if (copy_to_user(userbuffer, urb->transfer_buffer, len)) 526 return -EFAULT; 527 return 0; 528 } 529 530 for (i = 0; i < urb->num_sgs && len; i++) { 531 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len; 532 if (copy_to_user(userbuffer, sg_virt(&urb->sg[i]), size)) 533 return -EFAULT; 534 userbuffer += size; 535 len -= size; 536 } 537 538 return 0; 539 } 540 541 #define AS_CONTINUATION 1 542 #define AS_UNLINK 2 543 544 static void cancel_bulk_urbs(struct usb_dev_state *ps, unsigned bulk_addr) 545 __releases(ps->lock) 546 __acquires(ps->lock) 547 { 548 struct urb *urb; 549 struct async *as; 550 551 /* Mark all the pending URBs that match bulk_addr, up to but not 552 * including the first one without AS_CONTINUATION. If such an 553 * URB is encountered then a new transfer has already started so 554 * the endpoint doesn't need to be disabled; otherwise it does. 555 */ 556 list_for_each_entry(as, &ps->async_pending, asynclist) { 557 if (as->bulk_addr == bulk_addr) { 558 if (as->bulk_status != AS_CONTINUATION) 559 goto rescan; 560 as->bulk_status = AS_UNLINK; 561 as->bulk_addr = 0; 562 } 563 } 564 ps->disabled_bulk_eps |= (1 << bulk_addr); 565 566 /* Now carefully unlink all the marked pending URBs */ 567 rescan: 568 list_for_each_entry(as, &ps->async_pending, asynclist) { 569 if (as->bulk_status == AS_UNLINK) { 570 as->bulk_status = 0; /* Only once */ 571 urb = as->urb; 572 usb_get_urb(urb); 573 spin_unlock(&ps->lock); /* Allow completions */ 574 usb_unlink_urb(urb); 575 usb_put_urb(urb); 576 spin_lock(&ps->lock); 577 goto rescan; 578 } 579 } 580 } 581 582 static void async_completed(struct urb *urb) 583 { 584 struct async *as = urb->context; 585 struct usb_dev_state *ps = as->ps; 586 struct pid *pid = NULL; 587 const struct cred *cred = NULL; 588 unsigned long flags; 589 sigval_t addr; 590 int signr, errno; 591 592 spin_lock_irqsave(&ps->lock, flags); 593 list_move_tail(&as->asynclist, &ps->async_completed); 594 as->status = urb->status; 595 signr = as->signr; 596 if (signr) { 597 errno = as->status; 598 addr = as->userurb_sigval; 599 pid = get_pid(as->pid); 600 cred = get_cred(as->cred); 601 } 602 snoop(&urb->dev->dev, "urb complete\n"); 603 snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length, 604 as->status, COMPLETE, NULL, 0); 605 if (usb_urb_dir_in(urb)) 606 snoop_urb_data(urb, urb->actual_length); 607 608 if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET && 609 as->status != -ENOENT) 610 cancel_bulk_urbs(ps, as->bulk_addr); 611 612 wake_up(&ps->wait); 613 spin_unlock_irqrestore(&ps->lock, flags); 614 615 if (signr) { 616 kill_pid_usb_asyncio(signr, errno, addr, pid, cred); 617 put_pid(pid); 618 put_cred(cred); 619 } 620 } 621 622 static void destroy_async(struct usb_dev_state *ps, struct list_head *list) 623 { 624 struct urb *urb; 625 struct async *as; 626 unsigned long flags; 627 628 spin_lock_irqsave(&ps->lock, flags); 629 while (!list_empty(list)) { 630 as = list_entry(list->next, struct async, asynclist); 631 list_del_init(&as->asynclist); 632 urb = as->urb; 633 usb_get_urb(urb); 634 635 /* drop the spinlock so the completion handler can run */ 636 spin_unlock_irqrestore(&ps->lock, flags); 637 usb_kill_urb(urb); 638 usb_put_urb(urb); 639 spin_lock_irqsave(&ps->lock, flags); 640 } 641 spin_unlock_irqrestore(&ps->lock, flags); 642 } 643 644 static void destroy_async_on_interface(struct usb_dev_state *ps, 645 unsigned int ifnum) 646 { 647 struct list_head *p, *q, hitlist; 648 unsigned long flags; 649 650 INIT_LIST_HEAD(&hitlist); 651 spin_lock_irqsave(&ps->lock, flags); 652 list_for_each_safe(p, q, &ps->async_pending) 653 if (ifnum == list_entry(p, struct async, asynclist)->ifnum) 654 list_move_tail(p, &hitlist); 655 spin_unlock_irqrestore(&ps->lock, flags); 656 destroy_async(ps, &hitlist); 657 } 658 659 static void destroy_all_async(struct usb_dev_state *ps) 660 { 661 destroy_async(ps, &ps->async_pending); 662 } 663 664 /* 665 * interface claims are made only at the request of user level code, 666 * which can also release them (explicitly or by closing files). 667 * they're also undone when devices disconnect. 668 */ 669 670 static int driver_probe(struct usb_interface *intf, 671 const struct usb_device_id *id) 672 { 673 return -ENODEV; 674 } 675 676 static void driver_disconnect(struct usb_interface *intf) 677 { 678 struct usb_dev_state *ps = usb_get_intfdata(intf); 679 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber; 680 681 if (!ps) 682 return; 683 684 /* NOTE: this relies on usbcore having canceled and completed 685 * all pending I/O requests; 2.6 does that. 686 */ 687 688 if (likely(ifnum < 8*sizeof(ps->ifclaimed))) 689 clear_bit(ifnum, &ps->ifclaimed); 690 else 691 dev_warn(&intf->dev, "interface number %u out of range\n", 692 ifnum); 693 694 usb_set_intfdata(intf, NULL); 695 696 /* force async requests to complete */ 697 destroy_async_on_interface(ps, ifnum); 698 } 699 700 /* The following routines are merely placeholders. There is no way 701 * to inform a user task about suspend or resumes. 702 */ 703 static int driver_suspend(struct usb_interface *intf, pm_message_t msg) 704 { 705 return 0; 706 } 707 708 static int driver_resume(struct usb_interface *intf) 709 { 710 return 0; 711 } 712 713 struct usb_driver usbfs_driver = { 714 .name = "usbfs", 715 .probe = driver_probe, 716 .disconnect = driver_disconnect, 717 .suspend = driver_suspend, 718 .resume = driver_resume, 719 }; 720 721 static int claimintf(struct usb_dev_state *ps, unsigned int ifnum) 722 { 723 struct usb_device *dev = ps->dev; 724 struct usb_interface *intf; 725 int err; 726 727 if (ifnum >= 8*sizeof(ps->ifclaimed)) 728 return -EINVAL; 729 /* already claimed */ 730 if (test_bit(ifnum, &ps->ifclaimed)) 731 return 0; 732 733 if (ps->privileges_dropped && 734 !test_bit(ifnum, &ps->interface_allowed_mask)) 735 return -EACCES; 736 737 intf = usb_ifnum_to_if(dev, ifnum); 738 if (!intf) 739 err = -ENOENT; 740 else 741 err = usb_driver_claim_interface(&usbfs_driver, intf, ps); 742 if (err == 0) 743 set_bit(ifnum, &ps->ifclaimed); 744 return err; 745 } 746 747 static int releaseintf(struct usb_dev_state *ps, unsigned int ifnum) 748 { 749 struct usb_device *dev; 750 struct usb_interface *intf; 751 int err; 752 753 err = -EINVAL; 754 if (ifnum >= 8*sizeof(ps->ifclaimed)) 755 return err; 756 dev = ps->dev; 757 intf = usb_ifnum_to_if(dev, ifnum); 758 if (!intf) 759 err = -ENOENT; 760 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) { 761 usb_driver_release_interface(&usbfs_driver, intf); 762 err = 0; 763 } 764 return err; 765 } 766 767 static int checkintf(struct usb_dev_state *ps, unsigned int ifnum) 768 { 769 if (ps->dev->state != USB_STATE_CONFIGURED) 770 return -EHOSTUNREACH; 771 if (ifnum >= 8*sizeof(ps->ifclaimed)) 772 return -EINVAL; 773 if (test_bit(ifnum, &ps->ifclaimed)) 774 return 0; 775 /* if not yet claimed, claim it for the driver */ 776 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim " 777 "interface %u before use\n", task_pid_nr(current), 778 current->comm, ifnum); 779 return claimintf(ps, ifnum); 780 } 781 782 static int findintfep(struct usb_device *dev, unsigned int ep) 783 { 784 unsigned int i, j, e; 785 struct usb_interface *intf; 786 struct usb_host_interface *alts; 787 struct usb_endpoint_descriptor *endpt; 788 789 if (ep & ~(USB_DIR_IN|0xf)) 790 return -EINVAL; 791 if (!dev->actconfig) 792 return -ESRCH; 793 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { 794 intf = dev->actconfig->interface[i]; 795 for (j = 0; j < intf->num_altsetting; j++) { 796 alts = &intf->altsetting[j]; 797 for (e = 0; e < alts->desc.bNumEndpoints; e++) { 798 endpt = &alts->endpoint[e].desc; 799 if (endpt->bEndpointAddress == ep) 800 return alts->desc.bInterfaceNumber; 801 } 802 } 803 } 804 return -ENOENT; 805 } 806 807 static int check_ctrlrecip(struct usb_dev_state *ps, unsigned int requesttype, 808 unsigned int request, unsigned int index) 809 { 810 int ret = 0; 811 struct usb_host_interface *alt_setting; 812 813 if (ps->dev->state != USB_STATE_UNAUTHENTICATED 814 && ps->dev->state != USB_STATE_ADDRESS 815 && ps->dev->state != USB_STATE_CONFIGURED) 816 return -EHOSTUNREACH; 817 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype)) 818 return 0; 819 820 /* 821 * check for the special corner case 'get_device_id' in the printer 822 * class specification, which we always want to allow as it is used 823 * to query things like ink level, etc. 824 */ 825 if (requesttype == 0xa1 && request == 0) { 826 alt_setting = usb_find_alt_setting(ps->dev->actconfig, 827 index >> 8, index & 0xff); 828 if (alt_setting 829 && alt_setting->desc.bInterfaceClass == USB_CLASS_PRINTER) 830 return 0; 831 } 832 833 index &= 0xff; 834 switch (requesttype & USB_RECIP_MASK) { 835 case USB_RECIP_ENDPOINT: 836 if ((index & ~USB_DIR_IN) == 0) 837 return 0; 838 ret = findintfep(ps->dev, index); 839 if (ret < 0) { 840 /* 841 * Some not fully compliant Win apps seem to get 842 * index wrong and have the endpoint number here 843 * rather than the endpoint address (with the 844 * correct direction). Win does let this through, 845 * so we'll not reject it here but leave it to 846 * the device to not break KVM. But we warn. 847 */ 848 ret = findintfep(ps->dev, index ^ 0x80); 849 if (ret >= 0) 850 dev_info(&ps->dev->dev, 851 "%s: process %i (%s) requesting ep %02x but needs %02x\n", 852 __func__, task_pid_nr(current), 853 current->comm, index, index ^ 0x80); 854 } 855 if (ret >= 0) 856 ret = checkintf(ps, ret); 857 break; 858 859 case USB_RECIP_INTERFACE: 860 ret = checkintf(ps, index); 861 break; 862 } 863 return ret; 864 } 865 866 static struct usb_host_endpoint *ep_to_host_endpoint(struct usb_device *dev, 867 unsigned char ep) 868 { 869 if (ep & USB_ENDPOINT_DIR_MASK) 870 return dev->ep_in[ep & USB_ENDPOINT_NUMBER_MASK]; 871 else 872 return dev->ep_out[ep & USB_ENDPOINT_NUMBER_MASK]; 873 } 874 875 static int parse_usbdevfs_streams(struct usb_dev_state *ps, 876 struct usbdevfs_streams __user *streams, 877 unsigned int *num_streams_ret, 878 unsigned int *num_eps_ret, 879 struct usb_host_endpoint ***eps_ret, 880 struct usb_interface **intf_ret) 881 { 882 unsigned int i, num_streams, num_eps; 883 struct usb_host_endpoint **eps; 884 struct usb_interface *intf = NULL; 885 unsigned char ep; 886 int ifnum, ret; 887 888 if (get_user(num_streams, &streams->num_streams) || 889 get_user(num_eps, &streams->num_eps)) 890 return -EFAULT; 891 892 if (num_eps < 1 || num_eps > USB_MAXENDPOINTS) 893 return -EINVAL; 894 895 /* The XHCI controller allows max 2 ^ 16 streams */ 896 if (num_streams_ret && (num_streams < 2 || num_streams > 65536)) 897 return -EINVAL; 898 899 eps = kmalloc_array(num_eps, sizeof(*eps), GFP_KERNEL); 900 if (!eps) 901 return -ENOMEM; 902 903 for (i = 0; i < num_eps; i++) { 904 if (get_user(ep, &streams->eps[i])) { 905 ret = -EFAULT; 906 goto error; 907 } 908 eps[i] = ep_to_host_endpoint(ps->dev, ep); 909 if (!eps[i]) { 910 ret = -EINVAL; 911 goto error; 912 } 913 914 /* usb_alloc/free_streams operate on an usb_interface */ 915 ifnum = findintfep(ps->dev, ep); 916 if (ifnum < 0) { 917 ret = ifnum; 918 goto error; 919 } 920 921 if (i == 0) { 922 ret = checkintf(ps, ifnum); 923 if (ret < 0) 924 goto error; 925 intf = usb_ifnum_to_if(ps->dev, ifnum); 926 } else { 927 /* Verify all eps belong to the same interface */ 928 if (ifnum != intf->altsetting->desc.bInterfaceNumber) { 929 ret = -EINVAL; 930 goto error; 931 } 932 } 933 } 934 935 if (num_streams_ret) 936 *num_streams_ret = num_streams; 937 *num_eps_ret = num_eps; 938 *eps_ret = eps; 939 *intf_ret = intf; 940 941 return 0; 942 943 error: 944 kfree(eps); 945 return ret; 946 } 947 948 static int match_devt(struct device *dev, void *data) 949 { 950 return dev->devt == (dev_t) (unsigned long) data; 951 } 952 953 static struct usb_device *usbdev_lookup_by_devt(dev_t devt) 954 { 955 struct device *dev; 956 957 dev = bus_find_device(&usb_bus_type, NULL, 958 (void *) (unsigned long) devt, match_devt); 959 if (!dev) 960 return NULL; 961 return to_usb_device(dev); 962 } 963 964 /* 965 * file operations 966 */ 967 static int usbdev_open(struct inode *inode, struct file *file) 968 { 969 struct usb_device *dev = NULL; 970 struct usb_dev_state *ps; 971 int ret; 972 973 ret = -ENOMEM; 974 ps = kzalloc(sizeof(struct usb_dev_state), GFP_KERNEL); 975 if (!ps) 976 goto out_free_ps; 977 978 ret = -ENODEV; 979 980 /* Protect against simultaneous removal or release */ 981 mutex_lock(&usbfs_mutex); 982 983 /* usbdev device-node */ 984 if (imajor(inode) == USB_DEVICE_MAJOR) 985 dev = usbdev_lookup_by_devt(inode->i_rdev); 986 987 mutex_unlock(&usbfs_mutex); 988 989 if (!dev) 990 goto out_free_ps; 991 992 usb_lock_device(dev); 993 if (dev->state == USB_STATE_NOTATTACHED) 994 goto out_unlock_device; 995 996 ret = usb_autoresume_device(dev); 997 if (ret) 998 goto out_unlock_device; 999 1000 ps->dev = dev; 1001 ps->file = file; 1002 ps->interface_allowed_mask = 0xFFFFFFFF; /* 32 bits */ 1003 spin_lock_init(&ps->lock); 1004 INIT_LIST_HEAD(&ps->list); 1005 INIT_LIST_HEAD(&ps->async_pending); 1006 INIT_LIST_HEAD(&ps->async_completed); 1007 INIT_LIST_HEAD(&ps->memory_list); 1008 init_waitqueue_head(&ps->wait); 1009 ps->disc_pid = get_pid(task_pid(current)); 1010 ps->cred = get_current_cred(); 1011 smp_wmb(); 1012 list_add_tail(&ps->list, &dev->filelist); 1013 file->private_data = ps; 1014 usb_unlock_device(dev); 1015 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current), 1016 current->comm); 1017 return ret; 1018 1019 out_unlock_device: 1020 usb_unlock_device(dev); 1021 usb_put_dev(dev); 1022 out_free_ps: 1023 kfree(ps); 1024 return ret; 1025 } 1026 1027 static int usbdev_release(struct inode *inode, struct file *file) 1028 { 1029 struct usb_dev_state *ps = file->private_data; 1030 struct usb_device *dev = ps->dev; 1031 unsigned int ifnum; 1032 struct async *as; 1033 1034 usb_lock_device(dev); 1035 usb_hub_release_all_ports(dev, ps); 1036 1037 list_del_init(&ps->list); 1038 1039 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed); 1040 ifnum++) { 1041 if (test_bit(ifnum, &ps->ifclaimed)) 1042 releaseintf(ps, ifnum); 1043 } 1044 destroy_all_async(ps); 1045 usb_autosuspend_device(dev); 1046 usb_unlock_device(dev); 1047 usb_put_dev(dev); 1048 put_pid(ps->disc_pid); 1049 put_cred(ps->cred); 1050 1051 as = async_getcompleted(ps); 1052 while (as) { 1053 free_async(as); 1054 as = async_getcompleted(ps); 1055 } 1056 1057 kfree(ps); 1058 return 0; 1059 } 1060 1061 static int proc_control(struct usb_dev_state *ps, void __user *arg) 1062 { 1063 struct usb_device *dev = ps->dev; 1064 struct usbdevfs_ctrltransfer ctrl; 1065 unsigned int tmo; 1066 unsigned char *tbuf; 1067 unsigned wLength; 1068 int i, pipe, ret; 1069 1070 if (copy_from_user(&ctrl, arg, sizeof(ctrl))) 1071 return -EFAULT; 1072 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.bRequest, 1073 ctrl.wIndex); 1074 if (ret) 1075 return ret; 1076 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */ 1077 if (wLength > PAGE_SIZE) 1078 return -EINVAL; 1079 ret = usbfs_increase_memory_usage(PAGE_SIZE + sizeof(struct urb) + 1080 sizeof(struct usb_ctrlrequest)); 1081 if (ret) 1082 return ret; 1083 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL); 1084 if (!tbuf) { 1085 ret = -ENOMEM; 1086 goto done; 1087 } 1088 tmo = ctrl.timeout; 1089 snoop(&dev->dev, "control urb: bRequestType=%02x " 1090 "bRequest=%02x wValue=%04x " 1091 "wIndex=%04x wLength=%04x\n", 1092 ctrl.bRequestType, ctrl.bRequest, ctrl.wValue, 1093 ctrl.wIndex, ctrl.wLength); 1094 if (ctrl.bRequestType & 0x80) { 1095 if (ctrl.wLength && !access_ok(ctrl.data, 1096 ctrl.wLength)) { 1097 ret = -EINVAL; 1098 goto done; 1099 } 1100 pipe = usb_rcvctrlpipe(dev, 0); 1101 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT, NULL, 0); 1102 1103 usb_unlock_device(dev); 1104 i = usb_control_msg(dev, pipe, ctrl.bRequest, 1105 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex, 1106 tbuf, ctrl.wLength, tmo); 1107 usb_lock_device(dev); 1108 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE, 1109 tbuf, max(i, 0)); 1110 if ((i > 0) && ctrl.wLength) { 1111 if (copy_to_user(ctrl.data, tbuf, i)) { 1112 ret = -EFAULT; 1113 goto done; 1114 } 1115 } 1116 } else { 1117 if (ctrl.wLength) { 1118 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) { 1119 ret = -EFAULT; 1120 goto done; 1121 } 1122 } 1123 pipe = usb_sndctrlpipe(dev, 0); 1124 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT, 1125 tbuf, ctrl.wLength); 1126 1127 usb_unlock_device(dev); 1128 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest, 1129 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex, 1130 tbuf, ctrl.wLength, tmo); 1131 usb_lock_device(dev); 1132 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE, NULL, 0); 1133 } 1134 if (i < 0 && i != -EPIPE) { 1135 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL " 1136 "failed cmd %s rqt %u rq %u len %u ret %d\n", 1137 current->comm, ctrl.bRequestType, ctrl.bRequest, 1138 ctrl.wLength, i); 1139 } 1140 ret = i; 1141 done: 1142 free_page((unsigned long) tbuf); 1143 usbfs_decrease_memory_usage(PAGE_SIZE + sizeof(struct urb) + 1144 sizeof(struct usb_ctrlrequest)); 1145 return ret; 1146 } 1147 1148 static int proc_bulk(struct usb_dev_state *ps, void __user *arg) 1149 { 1150 struct usb_device *dev = ps->dev; 1151 struct usbdevfs_bulktransfer bulk; 1152 unsigned int tmo, len1, pipe; 1153 int len2; 1154 unsigned char *tbuf; 1155 int i, ret; 1156 1157 if (copy_from_user(&bulk, arg, sizeof(bulk))) 1158 return -EFAULT; 1159 ret = findintfep(ps->dev, bulk.ep); 1160 if (ret < 0) 1161 return ret; 1162 ret = checkintf(ps, ret); 1163 if (ret) 1164 return ret; 1165 if (bulk.ep & USB_DIR_IN) 1166 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f); 1167 else 1168 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f); 1169 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN))) 1170 return -EINVAL; 1171 len1 = bulk.len; 1172 if (len1 >= (INT_MAX - sizeof(struct urb))) 1173 return -EINVAL; 1174 ret = usbfs_increase_memory_usage(len1 + sizeof(struct urb)); 1175 if (ret) 1176 return ret; 1177 tbuf = kmalloc(len1, GFP_KERNEL); 1178 if (!tbuf) { 1179 ret = -ENOMEM; 1180 goto done; 1181 } 1182 tmo = bulk.timeout; 1183 if (bulk.ep & 0x80) { 1184 if (len1 && !access_ok(bulk.data, len1)) { 1185 ret = -EINVAL; 1186 goto done; 1187 } 1188 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, NULL, 0); 1189 1190 usb_unlock_device(dev); 1191 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo); 1192 usb_lock_device(dev); 1193 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, tbuf, len2); 1194 1195 if (!i && len2) { 1196 if (copy_to_user(bulk.data, tbuf, len2)) { 1197 ret = -EFAULT; 1198 goto done; 1199 } 1200 } 1201 } else { 1202 if (len1) { 1203 if (copy_from_user(tbuf, bulk.data, len1)) { 1204 ret = -EFAULT; 1205 goto done; 1206 } 1207 } 1208 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, tbuf, len1); 1209 1210 usb_unlock_device(dev); 1211 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo); 1212 usb_lock_device(dev); 1213 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, NULL, 0); 1214 } 1215 ret = (i < 0 ? i : len2); 1216 done: 1217 kfree(tbuf); 1218 usbfs_decrease_memory_usage(len1 + sizeof(struct urb)); 1219 return ret; 1220 } 1221 1222 static void check_reset_of_active_ep(struct usb_device *udev, 1223 unsigned int epnum, char *ioctl_name) 1224 { 1225 struct usb_host_endpoint **eps; 1226 struct usb_host_endpoint *ep; 1227 1228 eps = (epnum & USB_DIR_IN) ? udev->ep_in : udev->ep_out; 1229 ep = eps[epnum & 0x0f]; 1230 if (ep && !list_empty(&ep->urb_list)) 1231 dev_warn(&udev->dev, "Process %d (%s) called USBDEVFS_%s for active endpoint 0x%02x\n", 1232 task_pid_nr(current), current->comm, 1233 ioctl_name, epnum); 1234 } 1235 1236 static int proc_resetep(struct usb_dev_state *ps, void __user *arg) 1237 { 1238 unsigned int ep; 1239 int ret; 1240 1241 if (get_user(ep, (unsigned int __user *)arg)) 1242 return -EFAULT; 1243 ret = findintfep(ps->dev, ep); 1244 if (ret < 0) 1245 return ret; 1246 ret = checkintf(ps, ret); 1247 if (ret) 1248 return ret; 1249 check_reset_of_active_ep(ps->dev, ep, "RESETEP"); 1250 usb_reset_endpoint(ps->dev, ep); 1251 return 0; 1252 } 1253 1254 static int proc_clearhalt(struct usb_dev_state *ps, void __user *arg) 1255 { 1256 unsigned int ep; 1257 int pipe; 1258 int ret; 1259 1260 if (get_user(ep, (unsigned int __user *)arg)) 1261 return -EFAULT; 1262 ret = findintfep(ps->dev, ep); 1263 if (ret < 0) 1264 return ret; 1265 ret = checkintf(ps, ret); 1266 if (ret) 1267 return ret; 1268 check_reset_of_active_ep(ps->dev, ep, "CLEAR_HALT"); 1269 if (ep & USB_DIR_IN) 1270 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f); 1271 else 1272 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f); 1273 1274 return usb_clear_halt(ps->dev, pipe); 1275 } 1276 1277 static int proc_getdriver(struct usb_dev_state *ps, void __user *arg) 1278 { 1279 struct usbdevfs_getdriver gd; 1280 struct usb_interface *intf; 1281 int ret; 1282 1283 if (copy_from_user(&gd, arg, sizeof(gd))) 1284 return -EFAULT; 1285 intf = usb_ifnum_to_if(ps->dev, gd.interface); 1286 if (!intf || !intf->dev.driver) 1287 ret = -ENODATA; 1288 else { 1289 strlcpy(gd.driver, intf->dev.driver->name, 1290 sizeof(gd.driver)); 1291 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0); 1292 } 1293 return ret; 1294 } 1295 1296 static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg) 1297 { 1298 struct usbdevfs_connectinfo ci; 1299 1300 memset(&ci, 0, sizeof(ci)); 1301 ci.devnum = ps->dev->devnum; 1302 ci.slow = ps->dev->speed == USB_SPEED_LOW; 1303 1304 if (copy_to_user(arg, &ci, sizeof(ci))) 1305 return -EFAULT; 1306 return 0; 1307 } 1308 1309 static int proc_resetdevice(struct usb_dev_state *ps) 1310 { 1311 struct usb_host_config *actconfig = ps->dev->actconfig; 1312 struct usb_interface *interface; 1313 int i, number; 1314 1315 /* Don't allow a device reset if the process has dropped the 1316 * privilege to do such things and any of the interfaces are 1317 * currently claimed. 1318 */ 1319 if (ps->privileges_dropped && actconfig) { 1320 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) { 1321 interface = actconfig->interface[i]; 1322 number = interface->cur_altsetting->desc.bInterfaceNumber; 1323 if (usb_interface_claimed(interface) && 1324 !test_bit(number, &ps->ifclaimed)) { 1325 dev_warn(&ps->dev->dev, 1326 "usbfs: interface %d claimed by %s while '%s' resets device\n", 1327 number, interface->dev.driver->name, current->comm); 1328 return -EACCES; 1329 } 1330 } 1331 } 1332 1333 return usb_reset_device(ps->dev); 1334 } 1335 1336 static int proc_setintf(struct usb_dev_state *ps, void __user *arg) 1337 { 1338 struct usbdevfs_setinterface setintf; 1339 int ret; 1340 1341 if (copy_from_user(&setintf, arg, sizeof(setintf))) 1342 return -EFAULT; 1343 ret = checkintf(ps, setintf.interface); 1344 if (ret) 1345 return ret; 1346 1347 destroy_async_on_interface(ps, setintf.interface); 1348 1349 return usb_set_interface(ps->dev, setintf.interface, 1350 setintf.altsetting); 1351 } 1352 1353 static int proc_setconfig(struct usb_dev_state *ps, void __user *arg) 1354 { 1355 int u; 1356 int status = 0; 1357 struct usb_host_config *actconfig; 1358 1359 if (get_user(u, (int __user *)arg)) 1360 return -EFAULT; 1361 1362 actconfig = ps->dev->actconfig; 1363 1364 /* Don't touch the device if any interfaces are claimed. 1365 * It could interfere with other drivers' operations, and if 1366 * an interface is claimed by usbfs it could easily deadlock. 1367 */ 1368 if (actconfig) { 1369 int i; 1370 1371 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) { 1372 if (usb_interface_claimed(actconfig->interface[i])) { 1373 dev_warn(&ps->dev->dev, 1374 "usbfs: interface %d claimed by %s " 1375 "while '%s' sets config #%d\n", 1376 actconfig->interface[i] 1377 ->cur_altsetting 1378 ->desc.bInterfaceNumber, 1379 actconfig->interface[i] 1380 ->dev.driver->name, 1381 current->comm, u); 1382 status = -EBUSY; 1383 break; 1384 } 1385 } 1386 } 1387 1388 /* SET_CONFIGURATION is often abused as a "cheap" driver reset, 1389 * so avoid usb_set_configuration()'s kick to sysfs 1390 */ 1391 if (status == 0) { 1392 if (actconfig && actconfig->desc.bConfigurationValue == u) 1393 status = usb_reset_configuration(ps->dev); 1394 else 1395 status = usb_set_configuration(ps->dev, u); 1396 } 1397 1398 return status; 1399 } 1400 1401 static struct usb_memory * 1402 find_memory_area(struct usb_dev_state *ps, const struct usbdevfs_urb *uurb) 1403 { 1404 struct usb_memory *usbm = NULL, *iter; 1405 unsigned long flags; 1406 unsigned long uurb_start = (unsigned long)uurb->buffer; 1407 1408 spin_lock_irqsave(&ps->lock, flags); 1409 list_for_each_entry(iter, &ps->memory_list, memlist) { 1410 if (uurb_start >= iter->vm_start && 1411 uurb_start < iter->vm_start + iter->size) { 1412 if (uurb->buffer_length > iter->vm_start + iter->size - 1413 uurb_start) { 1414 usbm = ERR_PTR(-EINVAL); 1415 } else { 1416 usbm = iter; 1417 usbm->urb_use_count++; 1418 } 1419 break; 1420 } 1421 } 1422 spin_unlock_irqrestore(&ps->lock, flags); 1423 return usbm; 1424 } 1425 1426 static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb, 1427 struct usbdevfs_iso_packet_desc __user *iso_frame_desc, 1428 void __user *arg, sigval_t userurb_sigval) 1429 { 1430 struct usbdevfs_iso_packet_desc *isopkt = NULL; 1431 struct usb_host_endpoint *ep; 1432 struct async *as = NULL; 1433 struct usb_ctrlrequest *dr = NULL; 1434 unsigned int u, totlen, isofrmlen; 1435 int i, ret, num_sgs = 0, ifnum = -1; 1436 int number_of_packets = 0; 1437 unsigned int stream_id = 0; 1438 void *buf; 1439 bool is_in; 1440 bool allow_short = false; 1441 bool allow_zero = false; 1442 unsigned long mask = USBDEVFS_URB_SHORT_NOT_OK | 1443 USBDEVFS_URB_BULK_CONTINUATION | 1444 USBDEVFS_URB_NO_FSBR | 1445 USBDEVFS_URB_ZERO_PACKET | 1446 USBDEVFS_URB_NO_INTERRUPT; 1447 /* USBDEVFS_URB_ISO_ASAP is a special case */ 1448 if (uurb->type == USBDEVFS_URB_TYPE_ISO) 1449 mask |= USBDEVFS_URB_ISO_ASAP; 1450 1451 if (uurb->flags & ~mask) 1452 return -EINVAL; 1453 1454 if ((unsigned int)uurb->buffer_length >= USBFS_XFER_MAX) 1455 return -EINVAL; 1456 if (uurb->buffer_length > 0 && !uurb->buffer) 1457 return -EINVAL; 1458 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL && 1459 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) { 1460 ifnum = findintfep(ps->dev, uurb->endpoint); 1461 if (ifnum < 0) 1462 return ifnum; 1463 ret = checkintf(ps, ifnum); 1464 if (ret) 1465 return ret; 1466 } 1467 ep = ep_to_host_endpoint(ps->dev, uurb->endpoint); 1468 if (!ep) 1469 return -ENOENT; 1470 is_in = (uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0; 1471 1472 u = 0; 1473 switch (uurb->type) { 1474 case USBDEVFS_URB_TYPE_CONTROL: 1475 if (!usb_endpoint_xfer_control(&ep->desc)) 1476 return -EINVAL; 1477 /* min 8 byte setup packet */ 1478 if (uurb->buffer_length < 8) 1479 return -EINVAL; 1480 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); 1481 if (!dr) 1482 return -ENOMEM; 1483 if (copy_from_user(dr, uurb->buffer, 8)) { 1484 ret = -EFAULT; 1485 goto error; 1486 } 1487 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) { 1488 ret = -EINVAL; 1489 goto error; 1490 } 1491 ret = check_ctrlrecip(ps, dr->bRequestType, dr->bRequest, 1492 le16_to_cpup(&dr->wIndex)); 1493 if (ret) 1494 goto error; 1495 uurb->buffer_length = le16_to_cpup(&dr->wLength); 1496 uurb->buffer += 8; 1497 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) { 1498 is_in = 1; 1499 uurb->endpoint |= USB_DIR_IN; 1500 } else { 1501 is_in = 0; 1502 uurb->endpoint &= ~USB_DIR_IN; 1503 } 1504 if (is_in) 1505 allow_short = true; 1506 snoop(&ps->dev->dev, "control urb: bRequestType=%02x " 1507 "bRequest=%02x wValue=%04x " 1508 "wIndex=%04x wLength=%04x\n", 1509 dr->bRequestType, dr->bRequest, 1510 __le16_to_cpup(&dr->wValue), 1511 __le16_to_cpup(&dr->wIndex), 1512 __le16_to_cpup(&dr->wLength)); 1513 u = sizeof(struct usb_ctrlrequest); 1514 break; 1515 1516 case USBDEVFS_URB_TYPE_BULK: 1517 if (!is_in) 1518 allow_zero = true; 1519 else 1520 allow_short = true; 1521 switch (usb_endpoint_type(&ep->desc)) { 1522 case USB_ENDPOINT_XFER_CONTROL: 1523 case USB_ENDPOINT_XFER_ISOC: 1524 return -EINVAL; 1525 case USB_ENDPOINT_XFER_INT: 1526 /* allow single-shot interrupt transfers */ 1527 uurb->type = USBDEVFS_URB_TYPE_INTERRUPT; 1528 goto interrupt_urb; 1529 } 1530 num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE); 1531 if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize) 1532 num_sgs = 0; 1533 if (ep->streams) 1534 stream_id = uurb->stream_id; 1535 break; 1536 1537 case USBDEVFS_URB_TYPE_INTERRUPT: 1538 if (!usb_endpoint_xfer_int(&ep->desc)) 1539 return -EINVAL; 1540 interrupt_urb: 1541 if (!is_in) 1542 allow_zero = true; 1543 else 1544 allow_short = true; 1545 break; 1546 1547 case USBDEVFS_URB_TYPE_ISO: 1548 /* arbitrary limit */ 1549 if (uurb->number_of_packets < 1 || 1550 uurb->number_of_packets > 128) 1551 return -EINVAL; 1552 if (!usb_endpoint_xfer_isoc(&ep->desc)) 1553 return -EINVAL; 1554 number_of_packets = uurb->number_of_packets; 1555 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) * 1556 number_of_packets; 1557 isopkt = memdup_user(iso_frame_desc, isofrmlen); 1558 if (IS_ERR(isopkt)) { 1559 ret = PTR_ERR(isopkt); 1560 isopkt = NULL; 1561 goto error; 1562 } 1563 for (totlen = u = 0; u < number_of_packets; u++) { 1564 /* 1565 * arbitrary limit need for USB 3.1 Gen2 1566 * sizemax: 96 DPs at SSP, 96 * 1024 = 98304 1567 */ 1568 if (isopkt[u].length > 98304) { 1569 ret = -EINVAL; 1570 goto error; 1571 } 1572 totlen += isopkt[u].length; 1573 } 1574 u *= sizeof(struct usb_iso_packet_descriptor); 1575 uurb->buffer_length = totlen; 1576 break; 1577 1578 default: 1579 return -EINVAL; 1580 } 1581 1582 if (uurb->buffer_length > 0 && 1583 !access_ok(uurb->buffer, uurb->buffer_length)) { 1584 ret = -EFAULT; 1585 goto error; 1586 } 1587 as = alloc_async(number_of_packets); 1588 if (!as) { 1589 ret = -ENOMEM; 1590 goto error; 1591 } 1592 1593 as->usbm = find_memory_area(ps, uurb); 1594 if (IS_ERR(as->usbm)) { 1595 ret = PTR_ERR(as->usbm); 1596 as->usbm = NULL; 1597 goto error; 1598 } 1599 1600 /* do not use SG buffers when memory mapped segments 1601 * are in use 1602 */ 1603 if (as->usbm) 1604 num_sgs = 0; 1605 1606 u += sizeof(struct async) + sizeof(struct urb) + uurb->buffer_length + 1607 num_sgs * sizeof(struct scatterlist); 1608 ret = usbfs_increase_memory_usage(u); 1609 if (ret) 1610 goto error; 1611 as->mem_usage = u; 1612 1613 if (num_sgs) { 1614 as->urb->sg = kmalloc_array(num_sgs, 1615 sizeof(struct scatterlist), 1616 GFP_KERNEL); 1617 if (!as->urb->sg) { 1618 ret = -ENOMEM; 1619 goto error; 1620 } 1621 as->urb->num_sgs = num_sgs; 1622 sg_init_table(as->urb->sg, as->urb->num_sgs); 1623 1624 totlen = uurb->buffer_length; 1625 for (i = 0; i < as->urb->num_sgs; i++) { 1626 u = (totlen > USB_SG_SIZE) ? USB_SG_SIZE : totlen; 1627 buf = kmalloc(u, GFP_KERNEL); 1628 if (!buf) { 1629 ret = -ENOMEM; 1630 goto error; 1631 } 1632 sg_set_buf(&as->urb->sg[i], buf, u); 1633 1634 if (!is_in) { 1635 if (copy_from_user(buf, uurb->buffer, u)) { 1636 ret = -EFAULT; 1637 goto error; 1638 } 1639 uurb->buffer += u; 1640 } 1641 totlen -= u; 1642 } 1643 } else if (uurb->buffer_length > 0) { 1644 if (as->usbm) { 1645 unsigned long uurb_start = (unsigned long)uurb->buffer; 1646 1647 as->urb->transfer_buffer = as->usbm->mem + 1648 (uurb_start - as->usbm->vm_start); 1649 } else { 1650 as->urb->transfer_buffer = kmalloc(uurb->buffer_length, 1651 GFP_KERNEL); 1652 if (!as->urb->transfer_buffer) { 1653 ret = -ENOMEM; 1654 goto error; 1655 } 1656 if (!is_in) { 1657 if (copy_from_user(as->urb->transfer_buffer, 1658 uurb->buffer, 1659 uurb->buffer_length)) { 1660 ret = -EFAULT; 1661 goto error; 1662 } 1663 } else if (uurb->type == USBDEVFS_URB_TYPE_ISO) { 1664 /* 1665 * Isochronous input data may end up being 1666 * discontiguous if some of the packets are 1667 * short. Clear the buffer so that the gaps 1668 * don't leak kernel data to userspace. 1669 */ 1670 memset(as->urb->transfer_buffer, 0, 1671 uurb->buffer_length); 1672 } 1673 } 1674 } 1675 as->urb->dev = ps->dev; 1676 as->urb->pipe = (uurb->type << 30) | 1677 __create_pipe(ps->dev, uurb->endpoint & 0xf) | 1678 (uurb->endpoint & USB_DIR_IN); 1679 1680 /* This tedious sequence is necessary because the URB_* flags 1681 * are internal to the kernel and subject to change, whereas 1682 * the USBDEVFS_URB_* flags are a user API and must not be changed. 1683 */ 1684 u = (is_in ? URB_DIR_IN : URB_DIR_OUT); 1685 if (uurb->flags & USBDEVFS_URB_ISO_ASAP) 1686 u |= URB_ISO_ASAP; 1687 if (allow_short && uurb->flags & USBDEVFS_URB_SHORT_NOT_OK) 1688 u |= URB_SHORT_NOT_OK; 1689 if (allow_zero && uurb->flags & USBDEVFS_URB_ZERO_PACKET) 1690 u |= URB_ZERO_PACKET; 1691 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT) 1692 u |= URB_NO_INTERRUPT; 1693 as->urb->transfer_flags = u; 1694 1695 if (!allow_short && uurb->flags & USBDEVFS_URB_SHORT_NOT_OK) 1696 dev_warn(&ps->dev->dev, "Requested nonsensical USBDEVFS_URB_SHORT_NOT_OK.\n"); 1697 if (!allow_zero && uurb->flags & USBDEVFS_URB_ZERO_PACKET) 1698 dev_warn(&ps->dev->dev, "Requested nonsensical USBDEVFS_URB_ZERO_PACKET.\n"); 1699 1700 as->urb->transfer_buffer_length = uurb->buffer_length; 1701 as->urb->setup_packet = (unsigned char *)dr; 1702 dr = NULL; 1703 as->urb->start_frame = uurb->start_frame; 1704 as->urb->number_of_packets = number_of_packets; 1705 as->urb->stream_id = stream_id; 1706 1707 if (ep->desc.bInterval) { 1708 if (uurb->type == USBDEVFS_URB_TYPE_ISO || 1709 ps->dev->speed == USB_SPEED_HIGH || 1710 ps->dev->speed >= USB_SPEED_SUPER) 1711 as->urb->interval = 1 << 1712 min(15, ep->desc.bInterval - 1); 1713 else 1714 as->urb->interval = ep->desc.bInterval; 1715 } 1716 1717 as->urb->context = as; 1718 as->urb->complete = async_completed; 1719 for (totlen = u = 0; u < number_of_packets; u++) { 1720 as->urb->iso_frame_desc[u].offset = totlen; 1721 as->urb->iso_frame_desc[u].length = isopkt[u].length; 1722 totlen += isopkt[u].length; 1723 } 1724 kfree(isopkt); 1725 isopkt = NULL; 1726 as->ps = ps; 1727 as->userurb = arg; 1728 as->userurb_sigval = userurb_sigval; 1729 if (as->usbm) { 1730 unsigned long uurb_start = (unsigned long)uurb->buffer; 1731 1732 as->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1733 as->urb->transfer_dma = as->usbm->dma_handle + 1734 (uurb_start - as->usbm->vm_start); 1735 } else if (is_in && uurb->buffer_length > 0) 1736 as->userbuffer = uurb->buffer; 1737 as->signr = uurb->signr; 1738 as->ifnum = ifnum; 1739 as->pid = get_pid(task_pid(current)); 1740 as->cred = get_current_cred(); 1741 snoop_urb(ps->dev, as->userurb, as->urb->pipe, 1742 as->urb->transfer_buffer_length, 0, SUBMIT, 1743 NULL, 0); 1744 if (!is_in) 1745 snoop_urb_data(as->urb, as->urb->transfer_buffer_length); 1746 1747 async_newpending(as); 1748 1749 if (usb_endpoint_xfer_bulk(&ep->desc)) { 1750 spin_lock_irq(&ps->lock); 1751 1752 /* Not exactly the endpoint address; the direction bit is 1753 * shifted to the 0x10 position so that the value will be 1754 * between 0 and 31. 1755 */ 1756 as->bulk_addr = usb_endpoint_num(&ep->desc) | 1757 ((ep->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK) 1758 >> 3); 1759 1760 /* If this bulk URB is the start of a new transfer, re-enable 1761 * the endpoint. Otherwise mark it as a continuation URB. 1762 */ 1763 if (uurb->flags & USBDEVFS_URB_BULK_CONTINUATION) 1764 as->bulk_status = AS_CONTINUATION; 1765 else 1766 ps->disabled_bulk_eps &= ~(1 << as->bulk_addr); 1767 1768 /* Don't accept continuation URBs if the endpoint is 1769 * disabled because of an earlier error. 1770 */ 1771 if (ps->disabled_bulk_eps & (1 << as->bulk_addr)) 1772 ret = -EREMOTEIO; 1773 else 1774 ret = usb_submit_urb(as->urb, GFP_ATOMIC); 1775 spin_unlock_irq(&ps->lock); 1776 } else { 1777 ret = usb_submit_urb(as->urb, GFP_KERNEL); 1778 } 1779 1780 if (ret) { 1781 dev_printk(KERN_DEBUG, &ps->dev->dev, 1782 "usbfs: usb_submit_urb returned %d\n", ret); 1783 snoop_urb(ps->dev, as->userurb, as->urb->pipe, 1784 0, ret, COMPLETE, NULL, 0); 1785 async_removepending(as); 1786 goto error; 1787 } 1788 return 0; 1789 1790 error: 1791 if (as && as->usbm) 1792 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count); 1793 kfree(isopkt); 1794 kfree(dr); 1795 if (as) 1796 free_async(as); 1797 return ret; 1798 } 1799 1800 static int proc_submiturb(struct usb_dev_state *ps, void __user *arg) 1801 { 1802 struct usbdevfs_urb uurb; 1803 sigval_t userurb_sigval; 1804 1805 if (copy_from_user(&uurb, arg, sizeof(uurb))) 1806 return -EFAULT; 1807 1808 memset(&userurb_sigval, 0, sizeof(userurb_sigval)); 1809 userurb_sigval.sival_ptr = arg; 1810 1811 return proc_do_submiturb(ps, &uurb, 1812 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc), 1813 arg, userurb_sigval); 1814 } 1815 1816 static int proc_unlinkurb(struct usb_dev_state *ps, void __user *arg) 1817 { 1818 struct urb *urb; 1819 struct async *as; 1820 unsigned long flags; 1821 1822 spin_lock_irqsave(&ps->lock, flags); 1823 as = async_getpending(ps, arg); 1824 if (!as) { 1825 spin_unlock_irqrestore(&ps->lock, flags); 1826 return -EINVAL; 1827 } 1828 1829 urb = as->urb; 1830 usb_get_urb(urb); 1831 spin_unlock_irqrestore(&ps->lock, flags); 1832 1833 usb_kill_urb(urb); 1834 usb_put_urb(urb); 1835 1836 return 0; 1837 } 1838 1839 static void compute_isochronous_actual_length(struct urb *urb) 1840 { 1841 unsigned int i; 1842 1843 if (urb->number_of_packets > 0) { 1844 urb->actual_length = 0; 1845 for (i = 0; i < urb->number_of_packets; i++) 1846 urb->actual_length += 1847 urb->iso_frame_desc[i].actual_length; 1848 } 1849 } 1850 1851 static int processcompl(struct async *as, void __user * __user *arg) 1852 { 1853 struct urb *urb = as->urb; 1854 struct usbdevfs_urb __user *userurb = as->userurb; 1855 void __user *addr = as->userurb; 1856 unsigned int i; 1857 1858 compute_isochronous_actual_length(urb); 1859 if (as->userbuffer && urb->actual_length) { 1860 if (copy_urb_data_to_user(as->userbuffer, urb)) 1861 goto err_out; 1862 } 1863 if (put_user(as->status, &userurb->status)) 1864 goto err_out; 1865 if (put_user(urb->actual_length, &userurb->actual_length)) 1866 goto err_out; 1867 if (put_user(urb->error_count, &userurb->error_count)) 1868 goto err_out; 1869 1870 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) { 1871 for (i = 0; i < urb->number_of_packets; i++) { 1872 if (put_user(urb->iso_frame_desc[i].actual_length, 1873 &userurb->iso_frame_desc[i].actual_length)) 1874 goto err_out; 1875 if (put_user(urb->iso_frame_desc[i].status, 1876 &userurb->iso_frame_desc[i].status)) 1877 goto err_out; 1878 } 1879 } 1880 1881 if (put_user(addr, (void __user * __user *)arg)) 1882 return -EFAULT; 1883 return 0; 1884 1885 err_out: 1886 return -EFAULT; 1887 } 1888 1889 static struct async *reap_as(struct usb_dev_state *ps) 1890 { 1891 DECLARE_WAITQUEUE(wait, current); 1892 struct async *as = NULL; 1893 struct usb_device *dev = ps->dev; 1894 1895 add_wait_queue(&ps->wait, &wait); 1896 for (;;) { 1897 __set_current_state(TASK_INTERRUPTIBLE); 1898 as = async_getcompleted(ps); 1899 if (as || !connected(ps)) 1900 break; 1901 if (signal_pending(current)) 1902 break; 1903 usb_unlock_device(dev); 1904 schedule(); 1905 usb_lock_device(dev); 1906 } 1907 remove_wait_queue(&ps->wait, &wait); 1908 set_current_state(TASK_RUNNING); 1909 return as; 1910 } 1911 1912 static int proc_reapurb(struct usb_dev_state *ps, void __user *arg) 1913 { 1914 struct async *as = reap_as(ps); 1915 1916 if (as) { 1917 int retval; 1918 1919 snoop(&ps->dev->dev, "reap %pK\n", as->userurb); 1920 retval = processcompl(as, (void __user * __user *)arg); 1921 free_async(as); 1922 return retval; 1923 } 1924 if (signal_pending(current)) 1925 return -EINTR; 1926 return -ENODEV; 1927 } 1928 1929 static int proc_reapurbnonblock(struct usb_dev_state *ps, void __user *arg) 1930 { 1931 int retval; 1932 struct async *as; 1933 1934 as = async_getcompleted(ps); 1935 if (as) { 1936 snoop(&ps->dev->dev, "reap %pK\n", as->userurb); 1937 retval = processcompl(as, (void __user * __user *)arg); 1938 free_async(as); 1939 } else { 1940 retval = (connected(ps) ? -EAGAIN : -ENODEV); 1941 } 1942 return retval; 1943 } 1944 1945 #ifdef CONFIG_COMPAT 1946 static int proc_control_compat(struct usb_dev_state *ps, 1947 struct usbdevfs_ctrltransfer32 __user *p32) 1948 { 1949 struct usbdevfs_ctrltransfer __user *p; 1950 __u32 udata; 1951 p = compat_alloc_user_space(sizeof(*p)); 1952 if (copy_in_user(p, p32, (sizeof(*p32) - sizeof(compat_caddr_t))) || 1953 get_user(udata, &p32->data) || 1954 put_user(compat_ptr(udata), &p->data)) 1955 return -EFAULT; 1956 return proc_control(ps, p); 1957 } 1958 1959 static int proc_bulk_compat(struct usb_dev_state *ps, 1960 struct usbdevfs_bulktransfer32 __user *p32) 1961 { 1962 struct usbdevfs_bulktransfer __user *p; 1963 compat_uint_t n; 1964 compat_caddr_t addr; 1965 1966 p = compat_alloc_user_space(sizeof(*p)); 1967 1968 if (get_user(n, &p32->ep) || put_user(n, &p->ep) || 1969 get_user(n, &p32->len) || put_user(n, &p->len) || 1970 get_user(n, &p32->timeout) || put_user(n, &p->timeout) || 1971 get_user(addr, &p32->data) || put_user(compat_ptr(addr), &p->data)) 1972 return -EFAULT; 1973 1974 return proc_bulk(ps, p); 1975 } 1976 static int proc_disconnectsignal_compat(struct usb_dev_state *ps, void __user *arg) 1977 { 1978 struct usbdevfs_disconnectsignal32 ds; 1979 1980 if (copy_from_user(&ds, arg, sizeof(ds))) 1981 return -EFAULT; 1982 ps->discsignr = ds.signr; 1983 ps->disccontext.sival_int = ds.context; 1984 return 0; 1985 } 1986 1987 static int get_urb32(struct usbdevfs_urb *kurb, 1988 struct usbdevfs_urb32 __user *uurb) 1989 { 1990 struct usbdevfs_urb32 urb32; 1991 if (copy_from_user(&urb32, uurb, sizeof(*uurb))) 1992 return -EFAULT; 1993 kurb->type = urb32.type; 1994 kurb->endpoint = urb32.endpoint; 1995 kurb->status = urb32.status; 1996 kurb->flags = urb32.flags; 1997 kurb->buffer = compat_ptr(urb32.buffer); 1998 kurb->buffer_length = urb32.buffer_length; 1999 kurb->actual_length = urb32.actual_length; 2000 kurb->start_frame = urb32.start_frame; 2001 kurb->number_of_packets = urb32.number_of_packets; 2002 kurb->error_count = urb32.error_count; 2003 kurb->signr = urb32.signr; 2004 kurb->usercontext = compat_ptr(urb32.usercontext); 2005 return 0; 2006 } 2007 2008 static int proc_submiturb_compat(struct usb_dev_state *ps, void __user *arg) 2009 { 2010 struct usbdevfs_urb uurb; 2011 sigval_t userurb_sigval; 2012 2013 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg)) 2014 return -EFAULT; 2015 2016 memset(&userurb_sigval, 0, sizeof(userurb_sigval)); 2017 userurb_sigval.sival_int = ptr_to_compat(arg); 2018 2019 return proc_do_submiturb(ps, &uurb, 2020 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc, 2021 arg, userurb_sigval); 2022 } 2023 2024 static int processcompl_compat(struct async *as, void __user * __user *arg) 2025 { 2026 struct urb *urb = as->urb; 2027 struct usbdevfs_urb32 __user *userurb = as->userurb; 2028 void __user *addr = as->userurb; 2029 unsigned int i; 2030 2031 compute_isochronous_actual_length(urb); 2032 if (as->userbuffer && urb->actual_length) { 2033 if (copy_urb_data_to_user(as->userbuffer, urb)) 2034 return -EFAULT; 2035 } 2036 if (put_user(as->status, &userurb->status)) 2037 return -EFAULT; 2038 if (put_user(urb->actual_length, &userurb->actual_length)) 2039 return -EFAULT; 2040 if (put_user(urb->error_count, &userurb->error_count)) 2041 return -EFAULT; 2042 2043 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) { 2044 for (i = 0; i < urb->number_of_packets; i++) { 2045 if (put_user(urb->iso_frame_desc[i].actual_length, 2046 &userurb->iso_frame_desc[i].actual_length)) 2047 return -EFAULT; 2048 if (put_user(urb->iso_frame_desc[i].status, 2049 &userurb->iso_frame_desc[i].status)) 2050 return -EFAULT; 2051 } 2052 } 2053 2054 if (put_user(ptr_to_compat(addr), (u32 __user *)arg)) 2055 return -EFAULT; 2056 return 0; 2057 } 2058 2059 static int proc_reapurb_compat(struct usb_dev_state *ps, void __user *arg) 2060 { 2061 struct async *as = reap_as(ps); 2062 2063 if (as) { 2064 int retval; 2065 2066 snoop(&ps->dev->dev, "reap %pK\n", as->userurb); 2067 retval = processcompl_compat(as, (void __user * __user *)arg); 2068 free_async(as); 2069 return retval; 2070 } 2071 if (signal_pending(current)) 2072 return -EINTR; 2073 return -ENODEV; 2074 } 2075 2076 static int proc_reapurbnonblock_compat(struct usb_dev_state *ps, void __user *arg) 2077 { 2078 int retval; 2079 struct async *as; 2080 2081 as = async_getcompleted(ps); 2082 if (as) { 2083 snoop(&ps->dev->dev, "reap %pK\n", as->userurb); 2084 retval = processcompl_compat(as, (void __user * __user *)arg); 2085 free_async(as); 2086 } else { 2087 retval = (connected(ps) ? -EAGAIN : -ENODEV); 2088 } 2089 return retval; 2090 } 2091 2092 2093 #endif 2094 2095 static int proc_disconnectsignal(struct usb_dev_state *ps, void __user *arg) 2096 { 2097 struct usbdevfs_disconnectsignal ds; 2098 2099 if (copy_from_user(&ds, arg, sizeof(ds))) 2100 return -EFAULT; 2101 ps->discsignr = ds.signr; 2102 ps->disccontext.sival_ptr = ds.context; 2103 return 0; 2104 } 2105 2106 static int proc_claiminterface(struct usb_dev_state *ps, void __user *arg) 2107 { 2108 unsigned int ifnum; 2109 2110 if (get_user(ifnum, (unsigned int __user *)arg)) 2111 return -EFAULT; 2112 return claimintf(ps, ifnum); 2113 } 2114 2115 static int proc_releaseinterface(struct usb_dev_state *ps, void __user *arg) 2116 { 2117 unsigned int ifnum; 2118 int ret; 2119 2120 if (get_user(ifnum, (unsigned int __user *)arg)) 2121 return -EFAULT; 2122 ret = releaseintf(ps, ifnum); 2123 if (ret < 0) 2124 return ret; 2125 destroy_async_on_interface(ps, ifnum); 2126 return 0; 2127 } 2128 2129 static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl) 2130 { 2131 int size; 2132 void *buf = NULL; 2133 int retval = 0; 2134 struct usb_interface *intf = NULL; 2135 struct usb_driver *driver = NULL; 2136 2137 if (ps->privileges_dropped) 2138 return -EACCES; 2139 2140 /* alloc buffer */ 2141 size = _IOC_SIZE(ctl->ioctl_code); 2142 if (size > 0) { 2143 buf = kmalloc(size, GFP_KERNEL); 2144 if (buf == NULL) 2145 return -ENOMEM; 2146 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) { 2147 if (copy_from_user(buf, ctl->data, size)) { 2148 kfree(buf); 2149 return -EFAULT; 2150 } 2151 } else { 2152 memset(buf, 0, size); 2153 } 2154 } 2155 2156 if (!connected(ps)) { 2157 kfree(buf); 2158 return -ENODEV; 2159 } 2160 2161 if (ps->dev->state != USB_STATE_CONFIGURED) 2162 retval = -EHOSTUNREACH; 2163 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno))) 2164 retval = -EINVAL; 2165 else switch (ctl->ioctl_code) { 2166 2167 /* disconnect kernel driver from interface */ 2168 case USBDEVFS_DISCONNECT: 2169 if (intf->dev.driver) { 2170 driver = to_usb_driver(intf->dev.driver); 2171 dev_dbg(&intf->dev, "disconnect by usbfs\n"); 2172 usb_driver_release_interface(driver, intf); 2173 } else 2174 retval = -ENODATA; 2175 break; 2176 2177 /* let kernel drivers try to (re)bind to the interface */ 2178 case USBDEVFS_CONNECT: 2179 if (!intf->dev.driver) 2180 retval = device_attach(&intf->dev); 2181 else 2182 retval = -EBUSY; 2183 break; 2184 2185 /* talk directly to the interface's driver */ 2186 default: 2187 if (intf->dev.driver) 2188 driver = to_usb_driver(intf->dev.driver); 2189 if (driver == NULL || driver->unlocked_ioctl == NULL) { 2190 retval = -ENOTTY; 2191 } else { 2192 retval = driver->unlocked_ioctl(intf, ctl->ioctl_code, buf); 2193 if (retval == -ENOIOCTLCMD) 2194 retval = -ENOTTY; 2195 } 2196 } 2197 2198 /* cleanup and return */ 2199 if (retval >= 0 2200 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0 2201 && size > 0 2202 && copy_to_user(ctl->data, buf, size) != 0) 2203 retval = -EFAULT; 2204 2205 kfree(buf); 2206 return retval; 2207 } 2208 2209 static int proc_ioctl_default(struct usb_dev_state *ps, void __user *arg) 2210 { 2211 struct usbdevfs_ioctl ctrl; 2212 2213 if (copy_from_user(&ctrl, arg, sizeof(ctrl))) 2214 return -EFAULT; 2215 return proc_ioctl(ps, &ctrl); 2216 } 2217 2218 #ifdef CONFIG_COMPAT 2219 static int proc_ioctl_compat(struct usb_dev_state *ps, compat_uptr_t arg) 2220 { 2221 struct usbdevfs_ioctl32 ioc32; 2222 struct usbdevfs_ioctl ctrl; 2223 2224 if (copy_from_user(&ioc32, compat_ptr(arg), sizeof(ioc32))) 2225 return -EFAULT; 2226 ctrl.ifno = ioc32.ifno; 2227 ctrl.ioctl_code = ioc32.ioctl_code; 2228 ctrl.data = compat_ptr(ioc32.data); 2229 return proc_ioctl(ps, &ctrl); 2230 } 2231 #endif 2232 2233 static int proc_claim_port(struct usb_dev_state *ps, void __user *arg) 2234 { 2235 unsigned portnum; 2236 int rc; 2237 2238 if (get_user(portnum, (unsigned __user *) arg)) 2239 return -EFAULT; 2240 rc = usb_hub_claim_port(ps->dev, portnum, ps); 2241 if (rc == 0) 2242 snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n", 2243 portnum, task_pid_nr(current), current->comm); 2244 return rc; 2245 } 2246 2247 static int proc_release_port(struct usb_dev_state *ps, void __user *arg) 2248 { 2249 unsigned portnum; 2250 2251 if (get_user(portnum, (unsigned __user *) arg)) 2252 return -EFAULT; 2253 return usb_hub_release_port(ps->dev, portnum, ps); 2254 } 2255 2256 static int proc_get_capabilities(struct usb_dev_state *ps, void __user *arg) 2257 { 2258 __u32 caps; 2259 2260 caps = USBDEVFS_CAP_ZERO_PACKET | USBDEVFS_CAP_NO_PACKET_SIZE_LIM | 2261 USBDEVFS_CAP_REAP_AFTER_DISCONNECT | USBDEVFS_CAP_MMAP | 2262 USBDEVFS_CAP_DROP_PRIVILEGES; 2263 if (!ps->dev->bus->no_stop_on_short) 2264 caps |= USBDEVFS_CAP_BULK_CONTINUATION; 2265 if (ps->dev->bus->sg_tablesize) 2266 caps |= USBDEVFS_CAP_BULK_SCATTER_GATHER; 2267 2268 if (put_user(caps, (__u32 __user *)arg)) 2269 return -EFAULT; 2270 2271 return 0; 2272 } 2273 2274 static int proc_disconnect_claim(struct usb_dev_state *ps, void __user *arg) 2275 { 2276 struct usbdevfs_disconnect_claim dc; 2277 struct usb_interface *intf; 2278 2279 if (copy_from_user(&dc, arg, sizeof(dc))) 2280 return -EFAULT; 2281 2282 intf = usb_ifnum_to_if(ps->dev, dc.interface); 2283 if (!intf) 2284 return -EINVAL; 2285 2286 if (intf->dev.driver) { 2287 struct usb_driver *driver = to_usb_driver(intf->dev.driver); 2288 2289 if (ps->privileges_dropped) 2290 return -EACCES; 2291 2292 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_IF_DRIVER) && 2293 strncmp(dc.driver, intf->dev.driver->name, 2294 sizeof(dc.driver)) != 0) 2295 return -EBUSY; 2296 2297 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_EXCEPT_DRIVER) && 2298 strncmp(dc.driver, intf->dev.driver->name, 2299 sizeof(dc.driver)) == 0) 2300 return -EBUSY; 2301 2302 dev_dbg(&intf->dev, "disconnect by usbfs\n"); 2303 usb_driver_release_interface(driver, intf); 2304 } 2305 2306 return claimintf(ps, dc.interface); 2307 } 2308 2309 static int proc_alloc_streams(struct usb_dev_state *ps, void __user *arg) 2310 { 2311 unsigned num_streams, num_eps; 2312 struct usb_host_endpoint **eps; 2313 struct usb_interface *intf; 2314 int r; 2315 2316 r = parse_usbdevfs_streams(ps, arg, &num_streams, &num_eps, 2317 &eps, &intf); 2318 if (r) 2319 return r; 2320 2321 destroy_async_on_interface(ps, 2322 intf->altsetting[0].desc.bInterfaceNumber); 2323 2324 r = usb_alloc_streams(intf, eps, num_eps, num_streams, GFP_KERNEL); 2325 kfree(eps); 2326 return r; 2327 } 2328 2329 static int proc_free_streams(struct usb_dev_state *ps, void __user *arg) 2330 { 2331 unsigned num_eps; 2332 struct usb_host_endpoint **eps; 2333 struct usb_interface *intf; 2334 int r; 2335 2336 r = parse_usbdevfs_streams(ps, arg, NULL, &num_eps, &eps, &intf); 2337 if (r) 2338 return r; 2339 2340 destroy_async_on_interface(ps, 2341 intf->altsetting[0].desc.bInterfaceNumber); 2342 2343 r = usb_free_streams(intf, eps, num_eps, GFP_KERNEL); 2344 kfree(eps); 2345 return r; 2346 } 2347 2348 static int proc_drop_privileges(struct usb_dev_state *ps, void __user *arg) 2349 { 2350 u32 data; 2351 2352 if (copy_from_user(&data, arg, sizeof(data))) 2353 return -EFAULT; 2354 2355 /* This is a one way operation. Once privileges are 2356 * dropped, you cannot regain them. You may however reissue 2357 * this ioctl to shrink the allowed interfaces mask. 2358 */ 2359 ps->interface_allowed_mask &= data; 2360 ps->privileges_dropped = true; 2361 2362 return 0; 2363 } 2364 2365 /* 2366 * NOTE: All requests here that have interface numbers as parameters 2367 * are assuming that somehow the configuration has been prevented from 2368 * changing. But there's no mechanism to ensure that... 2369 */ 2370 static long usbdev_do_ioctl(struct file *file, unsigned int cmd, 2371 void __user *p) 2372 { 2373 struct usb_dev_state *ps = file->private_data; 2374 struct inode *inode = file_inode(file); 2375 struct usb_device *dev = ps->dev; 2376 int ret = -ENOTTY; 2377 2378 if (!(file->f_mode & FMODE_WRITE)) 2379 return -EPERM; 2380 2381 usb_lock_device(dev); 2382 2383 /* Reap operations are allowed even after disconnection */ 2384 switch (cmd) { 2385 case USBDEVFS_REAPURB: 2386 snoop(&dev->dev, "%s: REAPURB\n", __func__); 2387 ret = proc_reapurb(ps, p); 2388 goto done; 2389 2390 case USBDEVFS_REAPURBNDELAY: 2391 snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__); 2392 ret = proc_reapurbnonblock(ps, p); 2393 goto done; 2394 2395 #ifdef CONFIG_COMPAT 2396 case USBDEVFS_REAPURB32: 2397 snoop(&dev->dev, "%s: REAPURB32\n", __func__); 2398 ret = proc_reapurb_compat(ps, p); 2399 goto done; 2400 2401 case USBDEVFS_REAPURBNDELAY32: 2402 snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__); 2403 ret = proc_reapurbnonblock_compat(ps, p); 2404 goto done; 2405 #endif 2406 } 2407 2408 if (!connected(ps)) { 2409 usb_unlock_device(dev); 2410 return -ENODEV; 2411 } 2412 2413 switch (cmd) { 2414 case USBDEVFS_CONTROL: 2415 snoop(&dev->dev, "%s: CONTROL\n", __func__); 2416 ret = proc_control(ps, p); 2417 if (ret >= 0) 2418 inode->i_mtime = current_time(inode); 2419 break; 2420 2421 case USBDEVFS_BULK: 2422 snoop(&dev->dev, "%s: BULK\n", __func__); 2423 ret = proc_bulk(ps, p); 2424 if (ret >= 0) 2425 inode->i_mtime = current_time(inode); 2426 break; 2427 2428 case USBDEVFS_RESETEP: 2429 snoop(&dev->dev, "%s: RESETEP\n", __func__); 2430 ret = proc_resetep(ps, p); 2431 if (ret >= 0) 2432 inode->i_mtime = current_time(inode); 2433 break; 2434 2435 case USBDEVFS_RESET: 2436 snoop(&dev->dev, "%s: RESET\n", __func__); 2437 ret = proc_resetdevice(ps); 2438 break; 2439 2440 case USBDEVFS_CLEAR_HALT: 2441 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__); 2442 ret = proc_clearhalt(ps, p); 2443 if (ret >= 0) 2444 inode->i_mtime = current_time(inode); 2445 break; 2446 2447 case USBDEVFS_GETDRIVER: 2448 snoop(&dev->dev, "%s: GETDRIVER\n", __func__); 2449 ret = proc_getdriver(ps, p); 2450 break; 2451 2452 case USBDEVFS_CONNECTINFO: 2453 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__); 2454 ret = proc_connectinfo(ps, p); 2455 break; 2456 2457 case USBDEVFS_SETINTERFACE: 2458 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__); 2459 ret = proc_setintf(ps, p); 2460 break; 2461 2462 case USBDEVFS_SETCONFIGURATION: 2463 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__); 2464 ret = proc_setconfig(ps, p); 2465 break; 2466 2467 case USBDEVFS_SUBMITURB: 2468 snoop(&dev->dev, "%s: SUBMITURB\n", __func__); 2469 ret = proc_submiturb(ps, p); 2470 if (ret >= 0) 2471 inode->i_mtime = current_time(inode); 2472 break; 2473 2474 #ifdef CONFIG_COMPAT 2475 case USBDEVFS_CONTROL32: 2476 snoop(&dev->dev, "%s: CONTROL32\n", __func__); 2477 ret = proc_control_compat(ps, p); 2478 if (ret >= 0) 2479 inode->i_mtime = current_time(inode); 2480 break; 2481 2482 case USBDEVFS_BULK32: 2483 snoop(&dev->dev, "%s: BULK32\n", __func__); 2484 ret = proc_bulk_compat(ps, p); 2485 if (ret >= 0) 2486 inode->i_mtime = current_time(inode); 2487 break; 2488 2489 case USBDEVFS_DISCSIGNAL32: 2490 snoop(&dev->dev, "%s: DISCSIGNAL32\n", __func__); 2491 ret = proc_disconnectsignal_compat(ps, p); 2492 break; 2493 2494 case USBDEVFS_SUBMITURB32: 2495 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__); 2496 ret = proc_submiturb_compat(ps, p); 2497 if (ret >= 0) 2498 inode->i_mtime = current_time(inode); 2499 break; 2500 2501 case USBDEVFS_IOCTL32: 2502 snoop(&dev->dev, "%s: IOCTL32\n", __func__); 2503 ret = proc_ioctl_compat(ps, ptr_to_compat(p)); 2504 break; 2505 #endif 2506 2507 case USBDEVFS_DISCARDURB: 2508 snoop(&dev->dev, "%s: DISCARDURB %pK\n", __func__, p); 2509 ret = proc_unlinkurb(ps, p); 2510 break; 2511 2512 case USBDEVFS_DISCSIGNAL: 2513 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__); 2514 ret = proc_disconnectsignal(ps, p); 2515 break; 2516 2517 case USBDEVFS_CLAIMINTERFACE: 2518 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__); 2519 ret = proc_claiminterface(ps, p); 2520 break; 2521 2522 case USBDEVFS_RELEASEINTERFACE: 2523 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__); 2524 ret = proc_releaseinterface(ps, p); 2525 break; 2526 2527 case USBDEVFS_IOCTL: 2528 snoop(&dev->dev, "%s: IOCTL\n", __func__); 2529 ret = proc_ioctl_default(ps, p); 2530 break; 2531 2532 case USBDEVFS_CLAIM_PORT: 2533 snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__); 2534 ret = proc_claim_port(ps, p); 2535 break; 2536 2537 case USBDEVFS_RELEASE_PORT: 2538 snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__); 2539 ret = proc_release_port(ps, p); 2540 break; 2541 case USBDEVFS_GET_CAPABILITIES: 2542 ret = proc_get_capabilities(ps, p); 2543 break; 2544 case USBDEVFS_DISCONNECT_CLAIM: 2545 ret = proc_disconnect_claim(ps, p); 2546 break; 2547 case USBDEVFS_ALLOC_STREAMS: 2548 ret = proc_alloc_streams(ps, p); 2549 break; 2550 case USBDEVFS_FREE_STREAMS: 2551 ret = proc_free_streams(ps, p); 2552 break; 2553 case USBDEVFS_DROP_PRIVILEGES: 2554 ret = proc_drop_privileges(ps, p); 2555 break; 2556 case USBDEVFS_GET_SPEED: 2557 ret = ps->dev->speed; 2558 break; 2559 } 2560 2561 done: 2562 usb_unlock_device(dev); 2563 if (ret >= 0) 2564 inode->i_atime = current_time(inode); 2565 return ret; 2566 } 2567 2568 static long usbdev_ioctl(struct file *file, unsigned int cmd, 2569 unsigned long arg) 2570 { 2571 int ret; 2572 2573 ret = usbdev_do_ioctl(file, cmd, (void __user *)arg); 2574 2575 return ret; 2576 } 2577 2578 #ifdef CONFIG_COMPAT 2579 static long usbdev_compat_ioctl(struct file *file, unsigned int cmd, 2580 unsigned long arg) 2581 { 2582 int ret; 2583 2584 ret = usbdev_do_ioctl(file, cmd, compat_ptr(arg)); 2585 2586 return ret; 2587 } 2588 #endif 2589 2590 /* No kernel lock - fine */ 2591 static __poll_t usbdev_poll(struct file *file, 2592 struct poll_table_struct *wait) 2593 { 2594 struct usb_dev_state *ps = file->private_data; 2595 __poll_t mask = 0; 2596 2597 poll_wait(file, &ps->wait, wait); 2598 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed)) 2599 mask |= EPOLLOUT | EPOLLWRNORM; 2600 if (!connected(ps)) 2601 mask |= EPOLLHUP; 2602 if (list_empty(&ps->list)) 2603 mask |= EPOLLERR; 2604 return mask; 2605 } 2606 2607 const struct file_operations usbdev_file_operations = { 2608 .owner = THIS_MODULE, 2609 .llseek = no_seek_end_llseek, 2610 .read = usbdev_read, 2611 .poll = usbdev_poll, 2612 .unlocked_ioctl = usbdev_ioctl, 2613 #ifdef CONFIG_COMPAT 2614 .compat_ioctl = usbdev_compat_ioctl, 2615 #endif 2616 .mmap = usbdev_mmap, 2617 .open = usbdev_open, 2618 .release = usbdev_release, 2619 }; 2620 2621 static void usbdev_remove(struct usb_device *udev) 2622 { 2623 struct usb_dev_state *ps; 2624 2625 while (!list_empty(&udev->filelist)) { 2626 ps = list_entry(udev->filelist.next, struct usb_dev_state, list); 2627 destroy_all_async(ps); 2628 wake_up_all(&ps->wait); 2629 list_del_init(&ps->list); 2630 if (ps->discsignr) 2631 kill_pid_usb_asyncio(ps->discsignr, EPIPE, ps->disccontext, 2632 ps->disc_pid, ps->cred); 2633 } 2634 } 2635 2636 static int usbdev_notify(struct notifier_block *self, 2637 unsigned long action, void *dev) 2638 { 2639 switch (action) { 2640 case USB_DEVICE_ADD: 2641 break; 2642 case USB_DEVICE_REMOVE: 2643 usbdev_remove(dev); 2644 break; 2645 } 2646 return NOTIFY_OK; 2647 } 2648 2649 static struct notifier_block usbdev_nb = { 2650 .notifier_call = usbdev_notify, 2651 }; 2652 2653 static struct cdev usb_device_cdev; 2654 2655 int __init usb_devio_init(void) 2656 { 2657 int retval; 2658 2659 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX, 2660 "usb_device"); 2661 if (retval) { 2662 printk(KERN_ERR "Unable to register minors for usb_device\n"); 2663 goto out; 2664 } 2665 cdev_init(&usb_device_cdev, &usbdev_file_operations); 2666 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX); 2667 if (retval) { 2668 printk(KERN_ERR "Unable to get usb_device major %d\n", 2669 USB_DEVICE_MAJOR); 2670 goto error_cdev; 2671 } 2672 usb_register_notify(&usbdev_nb); 2673 out: 2674 return retval; 2675 2676 error_cdev: 2677 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX); 2678 goto out; 2679 } 2680 2681 void usb_devio_cleanup(void) 2682 { 2683 usb_unregister_notify(&usbdev_nb); 2684 cdev_del(&usb_device_cdev); 2685 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX); 2686 } 2687