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