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 program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 * 23 * This file implements the usbfs/x/y files, where 24 * x is the bus number and y the device number. 25 * 26 * It allows user space programs/"drivers" to communicate directly 27 * with USB devices without intervening kernel driver. 28 * 29 * Revision history 30 * 22.12.1999 0.1 Initial release (split from proc_usb.c) 31 * 04.01.2000 0.2 Turned into its own filesystem 32 * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery 33 * (CAN-2005-3055) 34 */ 35 36 /*****************************************************************************/ 37 38 #include <linux/fs.h> 39 #include <linux/mm.h> 40 #include <linux/sched/signal.h> 41 #include <linux/slab.h> 42 #include <linux/signal.h> 43 #include <linux/poll.h> 44 #include <linux/module.h> 45 #include <linux/string.h> 46 #include <linux/usb.h> 47 #include <linux/usbdevice_fs.h> 48 #include <linux/usb/hcd.h> /* for usbcore internals */ 49 #include <linux/cdev.h> 50 #include <linux/notifier.h> 51 #include <linux/security.h> 52 #include <linux/user_namespace.h> 53 #include <linux/scatterlist.h> 54 #include <linux/uaccess.h> 55 #include <linux/dma-mapping.h> 56 #include <asm/byteorder.h> 57 #include <linux/moduleparam.h> 58 59 #include "usb.h" 60 61 #define USB_MAXBUS 64 62 #define USB_DEVICE_MAX (USB_MAXBUS * 128) 63 #define USB_SG_SIZE 16384 /* split-size for large txs */ 64 65 /* Mutual exclusion for removal, open, and release */ 66 DEFINE_MUTEX(usbfs_mutex); 67 68 struct usb_dev_state { 69 struct list_head list; /* state list */ 70 struct usb_device *dev; 71 struct file *file; 72 spinlock_t lock; /* protects the async urb lists */ 73 struct list_head async_pending; 74 struct list_head async_completed; 75 struct list_head memory_list; 76 wait_queue_head_t wait; /* wake up if a request completed */ 77 unsigned int discsignr; 78 struct pid *disc_pid; 79 const struct cred *cred; 80 void __user *disccontext; 81 unsigned long ifclaimed; 82 u32 secid; 83 u32 disabled_bulk_eps; 84 bool privileges_dropped; 85 unsigned long interface_allowed_mask; 86 }; 87 88 struct usb_memory { 89 struct list_head memlist; 90 int vma_use_count; 91 int urb_use_count; 92 u32 size; 93 void *mem; 94 dma_addr_t dma_handle; 95 unsigned long vm_start; 96 struct usb_dev_state *ps; 97 }; 98 99 struct async { 100 struct list_head asynclist; 101 struct usb_dev_state *ps; 102 struct pid *pid; 103 const struct cred *cred; 104 unsigned int signr; 105 unsigned int ifnum; 106 void __user *userbuffer; 107 void __user *userurb; 108 struct urb *urb; 109 struct usb_memory *usbm; 110 unsigned int mem_usage; 111 int status; 112 u32 secid; 113 u8 bulk_addr; 114 u8 bulk_status; 115 }; 116 117 static bool usbfs_snoop; 118 module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR); 119 MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic"); 120 121 static unsigned usbfs_snoop_max = 65536; 122 module_param(usbfs_snoop_max, uint, S_IRUGO | S_IWUSR); 123 MODULE_PARM_DESC(usbfs_snoop_max, 124 "maximum number of bytes to print while snooping"); 125 126 #define snoop(dev, format, arg...) \ 127 do { \ 128 if (usbfs_snoop) \ 129 dev_info(dev, format, ## arg); \ 130 } while (0) 131 132 enum snoop_when { 133 SUBMIT, COMPLETE 134 }; 135 136 #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0) 137 138 /* Limit on the total amount of memory we can allocate for transfers */ 139 static u32 usbfs_memory_mb = 16; 140 module_param(usbfs_memory_mb, uint, 0644); 141 MODULE_PARM_DESC(usbfs_memory_mb, 142 "maximum MB allowed for usbfs buffers (0 = no limit)"); 143 144 /* Hard limit, necessary to avoid arithmetic overflow */ 145 #define USBFS_XFER_MAX (UINT_MAX / 2 - 1000000) 146 147 static atomic64_t usbfs_memory_usage; /* Total memory currently allocated */ 148 149 /* Check whether it's okay to allocate more memory for a transfer */ 150 static int usbfs_increase_memory_usage(u64 amount) 151 { 152 u64 lim; 153 154 lim = ACCESS_ONCE(usbfs_memory_mb); 155 lim <<= 20; 156 157 atomic64_add(amount, &usbfs_memory_usage); 158 159 if (lim > 0 && atomic64_read(&usbfs_memory_usage) > lim) { 160 atomic64_sub(amount, &usbfs_memory_usage); 161 return -ENOMEM; 162 } 163 164 return 0; 165 } 166 167 /* Memory for a transfer is being deallocated */ 168 static void usbfs_decrease_memory_usage(u64 amount) 169 { 170 atomic64_sub(amount, &usbfs_memory_usage); 171 } 172 173 static int connected(struct usb_dev_state *ps) 174 { 175 return (!list_empty(&ps->list) && 176 ps->dev->state != USB_STATE_NOTATTACHED); 177 } 178 179 static void dec_usb_memory_use_count(struct usb_memory *usbm, int *count) 180 { 181 struct usb_dev_state *ps = usbm->ps; 182 unsigned long flags; 183 184 spin_lock_irqsave(&ps->lock, flags); 185 --*count; 186 if (usbm->urb_use_count == 0 && usbm->vma_use_count == 0) { 187 list_del(&usbm->memlist); 188 spin_unlock_irqrestore(&ps->lock, flags); 189 190 usb_free_coherent(ps->dev, usbm->size, usbm->mem, 191 usbm->dma_handle); 192 usbfs_decrease_memory_usage( 193 usbm->size + sizeof(struct usb_memory)); 194 kfree(usbm); 195 } else { 196 spin_unlock_irqrestore(&ps->lock, flags); 197 } 198 } 199 200 static void usbdev_vm_open(struct vm_area_struct *vma) 201 { 202 struct usb_memory *usbm = vma->vm_private_data; 203 unsigned long flags; 204 205 spin_lock_irqsave(&usbm->ps->lock, flags); 206 ++usbm->vma_use_count; 207 spin_unlock_irqrestore(&usbm->ps->lock, flags); 208 } 209 210 static void usbdev_vm_close(struct vm_area_struct *vma) 211 { 212 struct usb_memory *usbm = vma->vm_private_data; 213 214 dec_usb_memory_use_count(usbm, &usbm->vma_use_count); 215 } 216 217 static const struct vm_operations_struct usbdev_vm_ops = { 218 .open = usbdev_vm_open, 219 .close = usbdev_vm_close 220 }; 221 222 static int usbdev_mmap(struct file *file, struct vm_area_struct *vma) 223 { 224 struct usb_memory *usbm = NULL; 225 struct usb_dev_state *ps = file->private_data; 226 size_t size = vma->vm_end - vma->vm_start; 227 void *mem; 228 unsigned long flags; 229 dma_addr_t dma_handle; 230 int ret; 231 232 ret = usbfs_increase_memory_usage(size + sizeof(struct usb_memory)); 233 if (ret) 234 goto error; 235 236 usbm = kzalloc(sizeof(struct usb_memory), GFP_KERNEL); 237 if (!usbm) { 238 ret = -ENOMEM; 239 goto error_decrease_mem; 240 } 241 242 mem = usb_alloc_coherent(ps->dev, size, GFP_USER | __GFP_NOWARN, 243 &dma_handle); 244 if (!mem) { 245 ret = -ENOMEM; 246 goto error_free_usbm; 247 } 248 249 memset(mem, 0, size); 250 251 usbm->mem = mem; 252 usbm->dma_handle = dma_handle; 253 usbm->size = size; 254 usbm->ps = ps; 255 usbm->vm_start = vma->vm_start; 256 usbm->vma_use_count = 1; 257 INIT_LIST_HEAD(&usbm->memlist); 258 259 if (remap_pfn_range(vma, vma->vm_start, 260 virt_to_phys(usbm->mem) >> PAGE_SHIFT, 261 size, vma->vm_page_prot) < 0) { 262 dec_usb_memory_use_count(usbm, &usbm->vma_use_count); 263 return -EAGAIN; 264 } 265 266 vma->vm_flags |= VM_IO; 267 vma->vm_flags |= (VM_DONTEXPAND | VM_DONTDUMP); 268 vma->vm_ops = &usbdev_vm_ops; 269 vma->vm_private_data = usbm; 270 271 spin_lock_irqsave(&ps->lock, flags); 272 list_add_tail(&usbm->memlist, &ps->memory_list); 273 spin_unlock_irqrestore(&ps->lock, flags); 274 275 return 0; 276 277 error_free_usbm: 278 kfree(usbm); 279 error_decrease_mem: 280 usbfs_decrease_memory_usage(size + sizeof(struct usb_memory)); 281 error: 282 return ret; 283 } 284 285 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes, 286 loff_t *ppos) 287 { 288 struct usb_dev_state *ps = file->private_data; 289 struct usb_device *dev = ps->dev; 290 ssize_t ret = 0; 291 unsigned len; 292 loff_t pos; 293 int i; 294 295 pos = *ppos; 296 usb_lock_device(dev); 297 if (!connected(ps)) { 298 ret = -ENODEV; 299 goto err; 300 } else if (pos < 0) { 301 ret = -EINVAL; 302 goto err; 303 } 304 305 if (pos < sizeof(struct usb_device_descriptor)) { 306 /* 18 bytes - fits on the stack */ 307 struct usb_device_descriptor temp_desc; 308 309 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor)); 310 le16_to_cpus(&temp_desc.bcdUSB); 311 le16_to_cpus(&temp_desc.idVendor); 312 le16_to_cpus(&temp_desc.idProduct); 313 le16_to_cpus(&temp_desc.bcdDevice); 314 315 len = sizeof(struct usb_device_descriptor) - pos; 316 if (len > nbytes) 317 len = nbytes; 318 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) { 319 ret = -EFAULT; 320 goto err; 321 } 322 323 *ppos += len; 324 buf += len; 325 nbytes -= len; 326 ret += len; 327 } 328 329 pos = sizeof(struct usb_device_descriptor); 330 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) { 331 struct usb_config_descriptor *config = 332 (struct usb_config_descriptor *)dev->rawdescriptors[i]; 333 unsigned int length = le16_to_cpu(config->wTotalLength); 334 335 if (*ppos < pos + length) { 336 337 /* The descriptor may claim to be longer than it 338 * really is. Here is the actual allocated length. */ 339 unsigned alloclen = 340 le16_to_cpu(dev->config[i].desc.wTotalLength); 341 342 len = length - (*ppos - pos); 343 if (len > nbytes) 344 len = nbytes; 345 346 /* Simply don't write (skip over) unallocated parts */ 347 if (alloclen > (*ppos - pos)) { 348 alloclen -= (*ppos - pos); 349 if (copy_to_user(buf, 350 dev->rawdescriptors[i] + (*ppos - pos), 351 min(len, alloclen))) { 352 ret = -EFAULT; 353 goto err; 354 } 355 } 356 357 *ppos += len; 358 buf += len; 359 nbytes -= len; 360 ret += len; 361 } 362 363 pos += length; 364 } 365 366 err: 367 usb_unlock_device(dev); 368 return ret; 369 } 370 371 /* 372 * async list handling 373 */ 374 375 static struct async *alloc_async(unsigned int numisoframes) 376 { 377 struct async *as; 378 379 as = kzalloc(sizeof(struct async), GFP_KERNEL); 380 if (!as) 381 return NULL; 382 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL); 383 if (!as->urb) { 384 kfree(as); 385 return NULL; 386 } 387 return as; 388 } 389 390 static void free_async(struct async *as) 391 { 392 int i; 393 394 put_pid(as->pid); 395 if (as->cred) 396 put_cred(as->cred); 397 for (i = 0; i < as->urb->num_sgs; i++) { 398 if (sg_page(&as->urb->sg[i])) 399 kfree(sg_virt(&as->urb->sg[i])); 400 } 401 402 kfree(as->urb->sg); 403 if (as->usbm == NULL) 404 kfree(as->urb->transfer_buffer); 405 else 406 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count); 407 408 kfree(as->urb->setup_packet); 409 usb_free_urb(as->urb); 410 usbfs_decrease_memory_usage(as->mem_usage); 411 kfree(as); 412 } 413 414 static void async_newpending(struct async *as) 415 { 416 struct usb_dev_state *ps = as->ps; 417 unsigned long flags; 418 419 spin_lock_irqsave(&ps->lock, flags); 420 list_add_tail(&as->asynclist, &ps->async_pending); 421 spin_unlock_irqrestore(&ps->lock, flags); 422 } 423 424 static void async_removepending(struct async *as) 425 { 426 struct usb_dev_state *ps = as->ps; 427 unsigned long flags; 428 429 spin_lock_irqsave(&ps->lock, flags); 430 list_del_init(&as->asynclist); 431 spin_unlock_irqrestore(&ps->lock, flags); 432 } 433 434 static struct async *async_getcompleted(struct usb_dev_state *ps) 435 { 436 unsigned long flags; 437 struct async *as = NULL; 438 439 spin_lock_irqsave(&ps->lock, flags); 440 if (!list_empty(&ps->async_completed)) { 441 as = list_entry(ps->async_completed.next, struct async, 442 asynclist); 443 list_del_init(&as->asynclist); 444 } 445 spin_unlock_irqrestore(&ps->lock, flags); 446 return as; 447 } 448 449 static struct async *async_getpending(struct usb_dev_state *ps, 450 void __user *userurb) 451 { 452 struct async *as; 453 454 list_for_each_entry(as, &ps->async_pending, asynclist) 455 if (as->userurb == userurb) { 456 list_del_init(&as->asynclist); 457 return as; 458 } 459 460 return NULL; 461 } 462 463 static void snoop_urb(struct usb_device *udev, 464 void __user *userurb, int pipe, unsigned length, 465 int timeout_or_status, enum snoop_when when, 466 unsigned char *data, unsigned data_len) 467 { 468 static const char *types[] = {"isoc", "int", "ctrl", "bulk"}; 469 static const char *dirs[] = {"out", "in"}; 470 int ep; 471 const char *t, *d; 472 473 if (!usbfs_snoop) 474 return; 475 476 ep = usb_pipeendpoint(pipe); 477 t = types[usb_pipetype(pipe)]; 478 d = dirs[!!usb_pipein(pipe)]; 479 480 if (userurb) { /* Async */ 481 if (when == SUBMIT) 482 dev_info(&udev->dev, "userurb %pK, ep%d %s-%s, " 483 "length %u\n", 484 userurb, ep, t, d, length); 485 else 486 dev_info(&udev->dev, "userurb %pK, ep%d %s-%s, " 487 "actual_length %u status %d\n", 488 userurb, ep, t, d, length, 489 timeout_or_status); 490 } else { 491 if (when == SUBMIT) 492 dev_info(&udev->dev, "ep%d %s-%s, length %u, " 493 "timeout %d\n", 494 ep, t, d, length, timeout_or_status); 495 else 496 dev_info(&udev->dev, "ep%d %s-%s, actual_length %u, " 497 "status %d\n", 498 ep, t, d, length, timeout_or_status); 499 } 500 501 data_len = min(data_len, usbfs_snoop_max); 502 if (data && data_len > 0) { 503 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1, 504 data, data_len, 1); 505 } 506 } 507 508 static void snoop_urb_data(struct urb *urb, unsigned len) 509 { 510 int i, size; 511 512 len = min(len, usbfs_snoop_max); 513 if (!usbfs_snoop || len == 0) 514 return; 515 516 if (urb->num_sgs == 0) { 517 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1, 518 urb->transfer_buffer, len, 1); 519 return; 520 } 521 522 for (i = 0; i < urb->num_sgs && len; i++) { 523 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len; 524 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1, 525 sg_virt(&urb->sg[i]), size, 1); 526 len -= size; 527 } 528 } 529 530 static int copy_urb_data_to_user(u8 __user *userbuffer, struct urb *urb) 531 { 532 unsigned i, len, size; 533 534 if (urb->number_of_packets > 0) /* Isochronous */ 535 len = urb->transfer_buffer_length; 536 else /* Non-Isoc */ 537 len = urb->actual_length; 538 539 if (urb->num_sgs == 0) { 540 if (copy_to_user(userbuffer, urb->transfer_buffer, len)) 541 return -EFAULT; 542 return 0; 543 } 544 545 for (i = 0; i < urb->num_sgs && len; i++) { 546 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len; 547 if (copy_to_user(userbuffer, sg_virt(&urb->sg[i]), size)) 548 return -EFAULT; 549 userbuffer += size; 550 len -= size; 551 } 552 553 return 0; 554 } 555 556 #define AS_CONTINUATION 1 557 #define AS_UNLINK 2 558 559 static void cancel_bulk_urbs(struct usb_dev_state *ps, unsigned bulk_addr) 560 __releases(ps->lock) 561 __acquires(ps->lock) 562 { 563 struct urb *urb; 564 struct async *as; 565 566 /* Mark all the pending URBs that match bulk_addr, up to but not 567 * including the first one without AS_CONTINUATION. If such an 568 * URB is encountered then a new transfer has already started so 569 * the endpoint doesn't need to be disabled; otherwise it does. 570 */ 571 list_for_each_entry(as, &ps->async_pending, asynclist) { 572 if (as->bulk_addr == bulk_addr) { 573 if (as->bulk_status != AS_CONTINUATION) 574 goto rescan; 575 as->bulk_status = AS_UNLINK; 576 as->bulk_addr = 0; 577 } 578 } 579 ps->disabled_bulk_eps |= (1 << bulk_addr); 580 581 /* Now carefully unlink all the marked pending URBs */ 582 rescan: 583 list_for_each_entry(as, &ps->async_pending, asynclist) { 584 if (as->bulk_status == AS_UNLINK) { 585 as->bulk_status = 0; /* Only once */ 586 urb = as->urb; 587 usb_get_urb(urb); 588 spin_unlock(&ps->lock); /* Allow completions */ 589 usb_unlink_urb(urb); 590 usb_put_urb(urb); 591 spin_lock(&ps->lock); 592 goto rescan; 593 } 594 } 595 } 596 597 static void async_completed(struct urb *urb) 598 { 599 struct async *as = urb->context; 600 struct usb_dev_state *ps = as->ps; 601 struct siginfo sinfo; 602 struct pid *pid = NULL; 603 u32 secid = 0; 604 const struct cred *cred = NULL; 605 int signr; 606 607 spin_lock(&ps->lock); 608 list_move_tail(&as->asynclist, &ps->async_completed); 609 as->status = urb->status; 610 signr = as->signr; 611 if (signr) { 612 memset(&sinfo, 0, sizeof(sinfo)); 613 sinfo.si_signo = as->signr; 614 sinfo.si_errno = as->status; 615 sinfo.si_code = SI_ASYNCIO; 616 sinfo.si_addr = as->userurb; 617 pid = get_pid(as->pid); 618 cred = get_cred(as->cred); 619 secid = as->secid; 620 } 621 snoop(&urb->dev->dev, "urb complete\n"); 622 snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length, 623 as->status, COMPLETE, NULL, 0); 624 if ((urb->transfer_flags & URB_DIR_MASK) == URB_DIR_IN) 625 snoop_urb_data(urb, urb->actual_length); 626 627 if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET && 628 as->status != -ENOENT) 629 cancel_bulk_urbs(ps, as->bulk_addr); 630 631 wake_up(&ps->wait); 632 spin_unlock(&ps->lock); 633 634 if (signr) { 635 kill_pid_info_as_cred(sinfo.si_signo, &sinfo, pid, cred, secid); 636 put_pid(pid); 637 put_cred(cred); 638 } 639 } 640 641 static void destroy_async(struct usb_dev_state *ps, struct list_head *list) 642 { 643 struct urb *urb; 644 struct async *as; 645 unsigned long flags; 646 647 spin_lock_irqsave(&ps->lock, flags); 648 while (!list_empty(list)) { 649 as = list_entry(list->next, struct async, asynclist); 650 list_del_init(&as->asynclist); 651 urb = as->urb; 652 usb_get_urb(urb); 653 654 /* drop the spinlock so the completion handler can run */ 655 spin_unlock_irqrestore(&ps->lock, flags); 656 usb_kill_urb(urb); 657 usb_put_urb(urb); 658 spin_lock_irqsave(&ps->lock, flags); 659 } 660 spin_unlock_irqrestore(&ps->lock, flags); 661 } 662 663 static void destroy_async_on_interface(struct usb_dev_state *ps, 664 unsigned int ifnum) 665 { 666 struct list_head *p, *q, hitlist; 667 unsigned long flags; 668 669 INIT_LIST_HEAD(&hitlist); 670 spin_lock_irqsave(&ps->lock, flags); 671 list_for_each_safe(p, q, &ps->async_pending) 672 if (ifnum == list_entry(p, struct async, asynclist)->ifnum) 673 list_move_tail(p, &hitlist); 674 spin_unlock_irqrestore(&ps->lock, flags); 675 destroy_async(ps, &hitlist); 676 } 677 678 static void destroy_all_async(struct usb_dev_state *ps) 679 { 680 destroy_async(ps, &ps->async_pending); 681 } 682 683 /* 684 * interface claims are made only at the request of user level code, 685 * which can also release them (explicitly or by closing files). 686 * they're also undone when devices disconnect. 687 */ 688 689 static int driver_probe(struct usb_interface *intf, 690 const struct usb_device_id *id) 691 { 692 return -ENODEV; 693 } 694 695 static void driver_disconnect(struct usb_interface *intf) 696 { 697 struct usb_dev_state *ps = usb_get_intfdata(intf); 698 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber; 699 700 if (!ps) 701 return; 702 703 /* NOTE: this relies on usbcore having canceled and completed 704 * all pending I/O requests; 2.6 does that. 705 */ 706 707 if (likely(ifnum < 8*sizeof(ps->ifclaimed))) 708 clear_bit(ifnum, &ps->ifclaimed); 709 else 710 dev_warn(&intf->dev, "interface number %u out of range\n", 711 ifnum); 712 713 usb_set_intfdata(intf, NULL); 714 715 /* force async requests to complete */ 716 destroy_async_on_interface(ps, ifnum); 717 } 718 719 /* The following routines are merely placeholders. There is no way 720 * to inform a user task about suspend or resumes. 721 */ 722 static int driver_suspend(struct usb_interface *intf, pm_message_t msg) 723 { 724 return 0; 725 } 726 727 static int driver_resume(struct usb_interface *intf) 728 { 729 return 0; 730 } 731 732 struct usb_driver usbfs_driver = { 733 .name = "usbfs", 734 .probe = driver_probe, 735 .disconnect = driver_disconnect, 736 .suspend = driver_suspend, 737 .resume = driver_resume, 738 }; 739 740 static int claimintf(struct usb_dev_state *ps, unsigned int ifnum) 741 { 742 struct usb_device *dev = ps->dev; 743 struct usb_interface *intf; 744 int err; 745 746 if (ifnum >= 8*sizeof(ps->ifclaimed)) 747 return -EINVAL; 748 /* already claimed */ 749 if (test_bit(ifnum, &ps->ifclaimed)) 750 return 0; 751 752 if (ps->privileges_dropped && 753 !test_bit(ifnum, &ps->interface_allowed_mask)) 754 return -EACCES; 755 756 intf = usb_ifnum_to_if(dev, ifnum); 757 if (!intf) 758 err = -ENOENT; 759 else 760 err = usb_driver_claim_interface(&usbfs_driver, intf, ps); 761 if (err == 0) 762 set_bit(ifnum, &ps->ifclaimed); 763 return err; 764 } 765 766 static int releaseintf(struct usb_dev_state *ps, unsigned int ifnum) 767 { 768 struct usb_device *dev; 769 struct usb_interface *intf; 770 int err; 771 772 err = -EINVAL; 773 if (ifnum >= 8*sizeof(ps->ifclaimed)) 774 return err; 775 dev = ps->dev; 776 intf = usb_ifnum_to_if(dev, ifnum); 777 if (!intf) 778 err = -ENOENT; 779 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) { 780 usb_driver_release_interface(&usbfs_driver, intf); 781 err = 0; 782 } 783 return err; 784 } 785 786 static int checkintf(struct usb_dev_state *ps, unsigned int ifnum) 787 { 788 if (ps->dev->state != USB_STATE_CONFIGURED) 789 return -EHOSTUNREACH; 790 if (ifnum >= 8*sizeof(ps->ifclaimed)) 791 return -EINVAL; 792 if (test_bit(ifnum, &ps->ifclaimed)) 793 return 0; 794 /* if not yet claimed, claim it for the driver */ 795 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim " 796 "interface %u before use\n", task_pid_nr(current), 797 current->comm, ifnum); 798 return claimintf(ps, ifnum); 799 } 800 801 static int findintfep(struct usb_device *dev, unsigned int ep) 802 { 803 unsigned int i, j, e; 804 struct usb_interface *intf; 805 struct usb_host_interface *alts; 806 struct usb_endpoint_descriptor *endpt; 807 808 if (ep & ~(USB_DIR_IN|0xf)) 809 return -EINVAL; 810 if (!dev->actconfig) 811 return -ESRCH; 812 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { 813 intf = dev->actconfig->interface[i]; 814 for (j = 0; j < intf->num_altsetting; j++) { 815 alts = &intf->altsetting[j]; 816 for (e = 0; e < alts->desc.bNumEndpoints; e++) { 817 endpt = &alts->endpoint[e].desc; 818 if (endpt->bEndpointAddress == ep) 819 return alts->desc.bInterfaceNumber; 820 } 821 } 822 } 823 return -ENOENT; 824 } 825 826 static int check_ctrlrecip(struct usb_dev_state *ps, unsigned int requesttype, 827 unsigned int request, unsigned int index) 828 { 829 int ret = 0; 830 struct usb_host_interface *alt_setting; 831 832 if (ps->dev->state != USB_STATE_UNAUTHENTICATED 833 && ps->dev->state != USB_STATE_ADDRESS 834 && ps->dev->state != USB_STATE_CONFIGURED) 835 return -EHOSTUNREACH; 836 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype)) 837 return 0; 838 839 /* 840 * check for the special corner case 'get_device_id' in the printer 841 * class specification, which we always want to allow as it is used 842 * to query things like ink level, etc. 843 */ 844 if (requesttype == 0xa1 && request == 0) { 845 alt_setting = usb_find_alt_setting(ps->dev->actconfig, 846 index >> 8, index & 0xff); 847 if (alt_setting 848 && alt_setting->desc.bInterfaceClass == USB_CLASS_PRINTER) 849 return 0; 850 } 851 852 index &= 0xff; 853 switch (requesttype & USB_RECIP_MASK) { 854 case USB_RECIP_ENDPOINT: 855 if ((index & ~USB_DIR_IN) == 0) 856 return 0; 857 ret = findintfep(ps->dev, index); 858 if (ret < 0) { 859 /* 860 * Some not fully compliant Win apps seem to get 861 * index wrong and have the endpoint number here 862 * rather than the endpoint address (with the 863 * correct direction). Win does let this through, 864 * so we'll not reject it here but leave it to 865 * the device to not break KVM. But we warn. 866 */ 867 ret = findintfep(ps->dev, index ^ 0x80); 868 if (ret >= 0) 869 dev_info(&ps->dev->dev, 870 "%s: process %i (%s) requesting ep %02x but needs %02x\n", 871 __func__, task_pid_nr(current), 872 current->comm, index, index ^ 0x80); 873 } 874 if (ret >= 0) 875 ret = checkintf(ps, ret); 876 break; 877 878 case USB_RECIP_INTERFACE: 879 ret = checkintf(ps, index); 880 break; 881 } 882 return ret; 883 } 884 885 static struct usb_host_endpoint *ep_to_host_endpoint(struct usb_device *dev, 886 unsigned char ep) 887 { 888 if (ep & USB_ENDPOINT_DIR_MASK) 889 return dev->ep_in[ep & USB_ENDPOINT_NUMBER_MASK]; 890 else 891 return dev->ep_out[ep & USB_ENDPOINT_NUMBER_MASK]; 892 } 893 894 static int parse_usbdevfs_streams(struct usb_dev_state *ps, 895 struct usbdevfs_streams __user *streams, 896 unsigned int *num_streams_ret, 897 unsigned int *num_eps_ret, 898 struct usb_host_endpoint ***eps_ret, 899 struct usb_interface **intf_ret) 900 { 901 unsigned int i, num_streams, num_eps; 902 struct usb_host_endpoint **eps; 903 struct usb_interface *intf = NULL; 904 unsigned char ep; 905 int ifnum, ret; 906 907 if (get_user(num_streams, &streams->num_streams) || 908 get_user(num_eps, &streams->num_eps)) 909 return -EFAULT; 910 911 if (num_eps < 1 || num_eps > USB_MAXENDPOINTS) 912 return -EINVAL; 913 914 /* The XHCI controller allows max 2 ^ 16 streams */ 915 if (num_streams_ret && (num_streams < 2 || num_streams > 65536)) 916 return -EINVAL; 917 918 eps = kmalloc(num_eps * sizeof(*eps), GFP_KERNEL); 919 if (!eps) 920 return -ENOMEM; 921 922 for (i = 0; i < num_eps; i++) { 923 if (get_user(ep, &streams->eps[i])) { 924 ret = -EFAULT; 925 goto error; 926 } 927 eps[i] = ep_to_host_endpoint(ps->dev, ep); 928 if (!eps[i]) { 929 ret = -EINVAL; 930 goto error; 931 } 932 933 /* usb_alloc/free_streams operate on an usb_interface */ 934 ifnum = findintfep(ps->dev, ep); 935 if (ifnum < 0) { 936 ret = ifnum; 937 goto error; 938 } 939 940 if (i == 0) { 941 ret = checkintf(ps, ifnum); 942 if (ret < 0) 943 goto error; 944 intf = usb_ifnum_to_if(ps->dev, ifnum); 945 } else { 946 /* Verify all eps belong to the same interface */ 947 if (ifnum != intf->altsetting->desc.bInterfaceNumber) { 948 ret = -EINVAL; 949 goto error; 950 } 951 } 952 } 953 954 if (num_streams_ret) 955 *num_streams_ret = num_streams; 956 *num_eps_ret = num_eps; 957 *eps_ret = eps; 958 *intf_ret = intf; 959 960 return 0; 961 962 error: 963 kfree(eps); 964 return ret; 965 } 966 967 static int match_devt(struct device *dev, void *data) 968 { 969 return dev->devt == (dev_t) (unsigned long) data; 970 } 971 972 static struct usb_device *usbdev_lookup_by_devt(dev_t devt) 973 { 974 struct device *dev; 975 976 dev = bus_find_device(&usb_bus_type, NULL, 977 (void *) (unsigned long) devt, match_devt); 978 if (!dev) 979 return NULL; 980 return to_usb_device(dev); 981 } 982 983 /* 984 * file operations 985 */ 986 static int usbdev_open(struct inode *inode, struct file *file) 987 { 988 struct usb_device *dev = NULL; 989 struct usb_dev_state *ps; 990 int ret; 991 992 ret = -ENOMEM; 993 ps = kzalloc(sizeof(struct usb_dev_state), GFP_KERNEL); 994 if (!ps) 995 goto out_free_ps; 996 997 ret = -ENODEV; 998 999 /* Protect against simultaneous removal or release */ 1000 mutex_lock(&usbfs_mutex); 1001 1002 /* usbdev device-node */ 1003 if (imajor(inode) == USB_DEVICE_MAJOR) 1004 dev = usbdev_lookup_by_devt(inode->i_rdev); 1005 1006 mutex_unlock(&usbfs_mutex); 1007 1008 if (!dev) 1009 goto out_free_ps; 1010 1011 usb_lock_device(dev); 1012 if (dev->state == USB_STATE_NOTATTACHED) 1013 goto out_unlock_device; 1014 1015 ret = usb_autoresume_device(dev); 1016 if (ret) 1017 goto out_unlock_device; 1018 1019 ps->dev = dev; 1020 ps->file = file; 1021 ps->interface_allowed_mask = 0xFFFFFFFF; /* 32 bits */ 1022 spin_lock_init(&ps->lock); 1023 INIT_LIST_HEAD(&ps->list); 1024 INIT_LIST_HEAD(&ps->async_pending); 1025 INIT_LIST_HEAD(&ps->async_completed); 1026 INIT_LIST_HEAD(&ps->memory_list); 1027 init_waitqueue_head(&ps->wait); 1028 ps->disc_pid = get_pid(task_pid(current)); 1029 ps->cred = get_current_cred(); 1030 security_task_getsecid(current, &ps->secid); 1031 smp_wmb(); 1032 list_add_tail(&ps->list, &dev->filelist); 1033 file->private_data = ps; 1034 usb_unlock_device(dev); 1035 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current), 1036 current->comm); 1037 return ret; 1038 1039 out_unlock_device: 1040 usb_unlock_device(dev); 1041 usb_put_dev(dev); 1042 out_free_ps: 1043 kfree(ps); 1044 return ret; 1045 } 1046 1047 static int usbdev_release(struct inode *inode, struct file *file) 1048 { 1049 struct usb_dev_state *ps = file->private_data; 1050 struct usb_device *dev = ps->dev; 1051 unsigned int ifnum; 1052 struct async *as; 1053 1054 usb_lock_device(dev); 1055 usb_hub_release_all_ports(dev, ps); 1056 1057 list_del_init(&ps->list); 1058 1059 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed); 1060 ifnum++) { 1061 if (test_bit(ifnum, &ps->ifclaimed)) 1062 releaseintf(ps, ifnum); 1063 } 1064 destroy_all_async(ps); 1065 usb_autosuspend_device(dev); 1066 usb_unlock_device(dev); 1067 usb_put_dev(dev); 1068 put_pid(ps->disc_pid); 1069 put_cred(ps->cred); 1070 1071 as = async_getcompleted(ps); 1072 while (as) { 1073 free_async(as); 1074 as = async_getcompleted(ps); 1075 } 1076 1077 kfree(ps); 1078 return 0; 1079 } 1080 1081 static int proc_control(struct usb_dev_state *ps, void __user *arg) 1082 { 1083 struct usb_device *dev = ps->dev; 1084 struct usbdevfs_ctrltransfer ctrl; 1085 unsigned int tmo; 1086 unsigned char *tbuf; 1087 unsigned wLength; 1088 int i, pipe, ret; 1089 1090 if (copy_from_user(&ctrl, arg, sizeof(ctrl))) 1091 return -EFAULT; 1092 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.bRequest, 1093 ctrl.wIndex); 1094 if (ret) 1095 return ret; 1096 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */ 1097 if (wLength > PAGE_SIZE) 1098 return -EINVAL; 1099 ret = usbfs_increase_memory_usage(PAGE_SIZE + sizeof(struct urb) + 1100 sizeof(struct usb_ctrlrequest)); 1101 if (ret) 1102 return ret; 1103 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL); 1104 if (!tbuf) { 1105 ret = -ENOMEM; 1106 goto done; 1107 } 1108 tmo = ctrl.timeout; 1109 snoop(&dev->dev, "control urb: bRequestType=%02x " 1110 "bRequest=%02x wValue=%04x " 1111 "wIndex=%04x wLength=%04x\n", 1112 ctrl.bRequestType, ctrl.bRequest, ctrl.wValue, 1113 ctrl.wIndex, ctrl.wLength); 1114 if (ctrl.bRequestType & 0x80) { 1115 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data, 1116 ctrl.wLength)) { 1117 ret = -EINVAL; 1118 goto done; 1119 } 1120 pipe = usb_rcvctrlpipe(dev, 0); 1121 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT, NULL, 0); 1122 1123 usb_unlock_device(dev); 1124 i = usb_control_msg(dev, pipe, ctrl.bRequest, 1125 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex, 1126 tbuf, ctrl.wLength, tmo); 1127 usb_lock_device(dev); 1128 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE, 1129 tbuf, max(i, 0)); 1130 if ((i > 0) && ctrl.wLength) { 1131 if (copy_to_user(ctrl.data, tbuf, i)) { 1132 ret = -EFAULT; 1133 goto done; 1134 } 1135 } 1136 } else { 1137 if (ctrl.wLength) { 1138 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) { 1139 ret = -EFAULT; 1140 goto done; 1141 } 1142 } 1143 pipe = usb_sndctrlpipe(dev, 0); 1144 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT, 1145 tbuf, ctrl.wLength); 1146 1147 usb_unlock_device(dev); 1148 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest, 1149 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex, 1150 tbuf, ctrl.wLength, tmo); 1151 usb_lock_device(dev); 1152 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE, NULL, 0); 1153 } 1154 if (i < 0 && i != -EPIPE) { 1155 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL " 1156 "failed cmd %s rqt %u rq %u len %u ret %d\n", 1157 current->comm, ctrl.bRequestType, ctrl.bRequest, 1158 ctrl.wLength, i); 1159 } 1160 ret = i; 1161 done: 1162 free_page((unsigned long) tbuf); 1163 usbfs_decrease_memory_usage(PAGE_SIZE + sizeof(struct urb) + 1164 sizeof(struct usb_ctrlrequest)); 1165 return ret; 1166 } 1167 1168 static int proc_bulk(struct usb_dev_state *ps, void __user *arg) 1169 { 1170 struct usb_device *dev = ps->dev; 1171 struct usbdevfs_bulktransfer bulk; 1172 unsigned int tmo, len1, pipe; 1173 int len2; 1174 unsigned char *tbuf; 1175 int i, ret; 1176 1177 if (copy_from_user(&bulk, arg, sizeof(bulk))) 1178 return -EFAULT; 1179 ret = findintfep(ps->dev, bulk.ep); 1180 if (ret < 0) 1181 return ret; 1182 ret = checkintf(ps, ret); 1183 if (ret) 1184 return ret; 1185 if (bulk.ep & USB_DIR_IN) 1186 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f); 1187 else 1188 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f); 1189 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN))) 1190 return -EINVAL; 1191 len1 = bulk.len; 1192 if (len1 >= (INT_MAX - sizeof(struct urb))) 1193 return -EINVAL; 1194 ret = usbfs_increase_memory_usage(len1 + sizeof(struct urb)); 1195 if (ret) 1196 return ret; 1197 tbuf = kmalloc(len1, GFP_KERNEL); 1198 if (!tbuf) { 1199 ret = -ENOMEM; 1200 goto done; 1201 } 1202 tmo = bulk.timeout; 1203 if (bulk.ep & 0x80) { 1204 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) { 1205 ret = -EINVAL; 1206 goto done; 1207 } 1208 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, NULL, 0); 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, tbuf, len2); 1214 1215 if (!i && len2) { 1216 if (copy_to_user(bulk.data, tbuf, len2)) { 1217 ret = -EFAULT; 1218 goto done; 1219 } 1220 } 1221 } else { 1222 if (len1) { 1223 if (copy_from_user(tbuf, bulk.data, len1)) { 1224 ret = -EFAULT; 1225 goto done; 1226 } 1227 } 1228 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, tbuf, len1); 1229 1230 usb_unlock_device(dev); 1231 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo); 1232 usb_lock_device(dev); 1233 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, NULL, 0); 1234 } 1235 ret = (i < 0 ? i : len2); 1236 done: 1237 kfree(tbuf); 1238 usbfs_decrease_memory_usage(len1 + sizeof(struct urb)); 1239 return ret; 1240 } 1241 1242 static void check_reset_of_active_ep(struct usb_device *udev, 1243 unsigned int epnum, char *ioctl_name) 1244 { 1245 struct usb_host_endpoint **eps; 1246 struct usb_host_endpoint *ep; 1247 1248 eps = (epnum & USB_DIR_IN) ? udev->ep_in : udev->ep_out; 1249 ep = eps[epnum & 0x0f]; 1250 if (ep && !list_empty(&ep->urb_list)) 1251 dev_warn(&udev->dev, "Process %d (%s) called USBDEVFS_%s for active endpoint 0x%02x\n", 1252 task_pid_nr(current), current->comm, 1253 ioctl_name, epnum); 1254 } 1255 1256 static int proc_resetep(struct usb_dev_state *ps, void __user *arg) 1257 { 1258 unsigned int ep; 1259 int ret; 1260 1261 if (get_user(ep, (unsigned int __user *)arg)) 1262 return -EFAULT; 1263 ret = findintfep(ps->dev, ep); 1264 if (ret < 0) 1265 return ret; 1266 ret = checkintf(ps, ret); 1267 if (ret) 1268 return ret; 1269 check_reset_of_active_ep(ps->dev, ep, "RESETEP"); 1270 usb_reset_endpoint(ps->dev, ep); 1271 return 0; 1272 } 1273 1274 static int proc_clearhalt(struct usb_dev_state *ps, void __user *arg) 1275 { 1276 unsigned int ep; 1277 int pipe; 1278 int ret; 1279 1280 if (get_user(ep, (unsigned int __user *)arg)) 1281 return -EFAULT; 1282 ret = findintfep(ps->dev, ep); 1283 if (ret < 0) 1284 return ret; 1285 ret = checkintf(ps, ret); 1286 if (ret) 1287 return ret; 1288 check_reset_of_active_ep(ps->dev, ep, "CLEAR_HALT"); 1289 if (ep & USB_DIR_IN) 1290 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f); 1291 else 1292 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f); 1293 1294 return usb_clear_halt(ps->dev, pipe); 1295 } 1296 1297 static int proc_getdriver(struct usb_dev_state *ps, void __user *arg) 1298 { 1299 struct usbdevfs_getdriver gd; 1300 struct usb_interface *intf; 1301 int ret; 1302 1303 if (copy_from_user(&gd, arg, sizeof(gd))) 1304 return -EFAULT; 1305 intf = usb_ifnum_to_if(ps->dev, gd.interface); 1306 if (!intf || !intf->dev.driver) 1307 ret = -ENODATA; 1308 else { 1309 strlcpy(gd.driver, intf->dev.driver->name, 1310 sizeof(gd.driver)); 1311 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0); 1312 } 1313 return ret; 1314 } 1315 1316 static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg) 1317 { 1318 struct usbdevfs_connectinfo ci; 1319 1320 memset(&ci, 0, sizeof(ci)); 1321 ci.devnum = ps->dev->devnum; 1322 ci.slow = ps->dev->speed == USB_SPEED_LOW; 1323 1324 if (copy_to_user(arg, &ci, sizeof(ci))) 1325 return -EFAULT; 1326 return 0; 1327 } 1328 1329 static int proc_resetdevice(struct usb_dev_state *ps) 1330 { 1331 struct usb_host_config *actconfig = ps->dev->actconfig; 1332 struct usb_interface *interface; 1333 int i, number; 1334 1335 /* Don't allow a device reset if the process has dropped the 1336 * privilege to do such things and any of the interfaces are 1337 * currently claimed. 1338 */ 1339 if (ps->privileges_dropped && actconfig) { 1340 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) { 1341 interface = actconfig->interface[i]; 1342 number = interface->cur_altsetting->desc.bInterfaceNumber; 1343 if (usb_interface_claimed(interface) && 1344 !test_bit(number, &ps->ifclaimed)) { 1345 dev_warn(&ps->dev->dev, 1346 "usbfs: interface %d claimed by %s while '%s' resets device\n", 1347 number, interface->dev.driver->name, current->comm); 1348 return -EACCES; 1349 } 1350 } 1351 } 1352 1353 return usb_reset_device(ps->dev); 1354 } 1355 1356 static int proc_setintf(struct usb_dev_state *ps, void __user *arg) 1357 { 1358 struct usbdevfs_setinterface setintf; 1359 int ret; 1360 1361 if (copy_from_user(&setintf, arg, sizeof(setintf))) 1362 return -EFAULT; 1363 ret = checkintf(ps, setintf.interface); 1364 if (ret) 1365 return ret; 1366 1367 destroy_async_on_interface(ps, setintf.interface); 1368 1369 return usb_set_interface(ps->dev, setintf.interface, 1370 setintf.altsetting); 1371 } 1372 1373 static int proc_setconfig(struct usb_dev_state *ps, void __user *arg) 1374 { 1375 int u; 1376 int status = 0; 1377 struct usb_host_config *actconfig; 1378 1379 if (get_user(u, (int __user *)arg)) 1380 return -EFAULT; 1381 1382 actconfig = ps->dev->actconfig; 1383 1384 /* Don't touch the device if any interfaces are claimed. 1385 * It could interfere with other drivers' operations, and if 1386 * an interface is claimed by usbfs it could easily deadlock. 1387 */ 1388 if (actconfig) { 1389 int i; 1390 1391 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) { 1392 if (usb_interface_claimed(actconfig->interface[i])) { 1393 dev_warn(&ps->dev->dev, 1394 "usbfs: interface %d claimed by %s " 1395 "while '%s' sets config #%d\n", 1396 actconfig->interface[i] 1397 ->cur_altsetting 1398 ->desc.bInterfaceNumber, 1399 actconfig->interface[i] 1400 ->dev.driver->name, 1401 current->comm, u); 1402 status = -EBUSY; 1403 break; 1404 } 1405 } 1406 } 1407 1408 /* SET_CONFIGURATION is often abused as a "cheap" driver reset, 1409 * so avoid usb_set_configuration()'s kick to sysfs 1410 */ 1411 if (status == 0) { 1412 if (actconfig && actconfig->desc.bConfigurationValue == u) 1413 status = usb_reset_configuration(ps->dev); 1414 else 1415 status = usb_set_configuration(ps->dev, u); 1416 } 1417 1418 return status; 1419 } 1420 1421 static struct usb_memory * 1422 find_memory_area(struct usb_dev_state *ps, const struct usbdevfs_urb *uurb) 1423 { 1424 struct usb_memory *usbm = NULL, *iter; 1425 unsigned long flags; 1426 unsigned long uurb_start = (unsigned long)uurb->buffer; 1427 1428 spin_lock_irqsave(&ps->lock, flags); 1429 list_for_each_entry(iter, &ps->memory_list, memlist) { 1430 if (uurb_start >= iter->vm_start && 1431 uurb_start < iter->vm_start + iter->size) { 1432 if (uurb->buffer_length > iter->vm_start + iter->size - 1433 uurb_start) { 1434 usbm = ERR_PTR(-EINVAL); 1435 } else { 1436 usbm = iter; 1437 usbm->urb_use_count++; 1438 } 1439 break; 1440 } 1441 } 1442 spin_unlock_irqrestore(&ps->lock, flags); 1443 return usbm; 1444 } 1445 1446 static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb, 1447 struct usbdevfs_iso_packet_desc __user *iso_frame_desc, 1448 void __user *arg) 1449 { 1450 struct usbdevfs_iso_packet_desc *isopkt = NULL; 1451 struct usb_host_endpoint *ep; 1452 struct async *as = NULL; 1453 struct usb_ctrlrequest *dr = NULL; 1454 unsigned int u, totlen, isofrmlen; 1455 int i, ret, is_in, num_sgs = 0, ifnum = -1; 1456 int number_of_packets = 0; 1457 unsigned int stream_id = 0; 1458 void *buf; 1459 1460 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP | 1461 USBDEVFS_URB_SHORT_NOT_OK | 1462 USBDEVFS_URB_BULK_CONTINUATION | 1463 USBDEVFS_URB_NO_FSBR | 1464 USBDEVFS_URB_ZERO_PACKET | 1465 USBDEVFS_URB_NO_INTERRUPT)) 1466 return -EINVAL; 1467 if ((unsigned int)uurb->buffer_length >= USBFS_XFER_MAX) 1468 return -EINVAL; 1469 if (uurb->buffer_length > 0 && !uurb->buffer) 1470 return -EINVAL; 1471 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL && 1472 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) { 1473 ifnum = findintfep(ps->dev, uurb->endpoint); 1474 if (ifnum < 0) 1475 return ifnum; 1476 ret = checkintf(ps, ifnum); 1477 if (ret) 1478 return ret; 1479 } 1480 ep = ep_to_host_endpoint(ps->dev, uurb->endpoint); 1481 if (!ep) 1482 return -ENOENT; 1483 is_in = (uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0; 1484 1485 u = 0; 1486 switch (uurb->type) { 1487 case USBDEVFS_URB_TYPE_CONTROL: 1488 if (!usb_endpoint_xfer_control(&ep->desc)) 1489 return -EINVAL; 1490 /* min 8 byte setup packet */ 1491 if (uurb->buffer_length < 8) 1492 return -EINVAL; 1493 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); 1494 if (!dr) 1495 return -ENOMEM; 1496 if (copy_from_user(dr, uurb->buffer, 8)) { 1497 ret = -EFAULT; 1498 goto error; 1499 } 1500 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) { 1501 ret = -EINVAL; 1502 goto error; 1503 } 1504 ret = check_ctrlrecip(ps, dr->bRequestType, dr->bRequest, 1505 le16_to_cpup(&dr->wIndex)); 1506 if (ret) 1507 goto error; 1508 uurb->buffer_length = le16_to_cpup(&dr->wLength); 1509 uurb->buffer += 8; 1510 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) { 1511 is_in = 1; 1512 uurb->endpoint |= USB_DIR_IN; 1513 } else { 1514 is_in = 0; 1515 uurb->endpoint &= ~USB_DIR_IN; 1516 } 1517 snoop(&ps->dev->dev, "control urb: bRequestType=%02x " 1518 "bRequest=%02x wValue=%04x " 1519 "wIndex=%04x wLength=%04x\n", 1520 dr->bRequestType, dr->bRequest, 1521 __le16_to_cpup(&dr->wValue), 1522 __le16_to_cpup(&dr->wIndex), 1523 __le16_to_cpup(&dr->wLength)); 1524 u = sizeof(struct usb_ctrlrequest); 1525 break; 1526 1527 case USBDEVFS_URB_TYPE_BULK: 1528 switch (usb_endpoint_type(&ep->desc)) { 1529 case USB_ENDPOINT_XFER_CONTROL: 1530 case USB_ENDPOINT_XFER_ISOC: 1531 return -EINVAL; 1532 case USB_ENDPOINT_XFER_INT: 1533 /* allow single-shot interrupt transfers */ 1534 uurb->type = USBDEVFS_URB_TYPE_INTERRUPT; 1535 goto interrupt_urb; 1536 } 1537 num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE); 1538 if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize) 1539 num_sgs = 0; 1540 if (ep->streams) 1541 stream_id = uurb->stream_id; 1542 break; 1543 1544 case USBDEVFS_URB_TYPE_INTERRUPT: 1545 if (!usb_endpoint_xfer_int(&ep->desc)) 1546 return -EINVAL; 1547 interrupt_urb: 1548 break; 1549 1550 case USBDEVFS_URB_TYPE_ISO: 1551 /* arbitrary limit */ 1552 if (uurb->number_of_packets < 1 || 1553 uurb->number_of_packets > 128) 1554 return -EINVAL; 1555 if (!usb_endpoint_xfer_isoc(&ep->desc)) 1556 return -EINVAL; 1557 number_of_packets = uurb->number_of_packets; 1558 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) * 1559 number_of_packets; 1560 isopkt = memdup_user(iso_frame_desc, isofrmlen); 1561 if (IS_ERR(isopkt)) { 1562 ret = PTR_ERR(isopkt); 1563 isopkt = NULL; 1564 goto error; 1565 } 1566 for (totlen = u = 0; u < number_of_packets; u++) { 1567 /* 1568 * arbitrary limit need for USB 3.0 1569 * bMaxBurst (0~15 allowed, 1~16 packets) 1570 * bmAttributes (bit 1:0, mult 0~2, 1~3 packets) 1571 * sizemax: 1024 * 16 * 3 = 49152 1572 */ 1573 if (isopkt[u].length > 49152) { 1574 ret = -EINVAL; 1575 goto error; 1576 } 1577 totlen += isopkt[u].length; 1578 } 1579 u *= sizeof(struct usb_iso_packet_descriptor); 1580 uurb->buffer_length = totlen; 1581 break; 1582 1583 default: 1584 return -EINVAL; 1585 } 1586 1587 if (uurb->buffer_length > 0 && 1588 !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ, 1589 uurb->buffer, uurb->buffer_length)) { 1590 ret = -EFAULT; 1591 goto error; 1592 } 1593 as = alloc_async(number_of_packets); 1594 if (!as) { 1595 ret = -ENOMEM; 1596 goto error; 1597 } 1598 1599 as->usbm = find_memory_area(ps, uurb); 1600 if (IS_ERR(as->usbm)) { 1601 ret = PTR_ERR(as->usbm); 1602 as->usbm = NULL; 1603 goto error; 1604 } 1605 1606 /* do not use SG buffers when memory mapped segments 1607 * are in use 1608 */ 1609 if (as->usbm) 1610 num_sgs = 0; 1611 1612 u += sizeof(struct async) + sizeof(struct urb) + uurb->buffer_length + 1613 num_sgs * sizeof(struct scatterlist); 1614 ret = usbfs_increase_memory_usage(u); 1615 if (ret) 1616 goto error; 1617 as->mem_usage = u; 1618 1619 if (num_sgs) { 1620 as->urb->sg = kmalloc(num_sgs * sizeof(struct scatterlist), 1621 GFP_KERNEL); 1622 if (!as->urb->sg) { 1623 ret = -ENOMEM; 1624 goto error; 1625 } 1626 as->urb->num_sgs = num_sgs; 1627 sg_init_table(as->urb->sg, as->urb->num_sgs); 1628 1629 totlen = uurb->buffer_length; 1630 for (i = 0; i < as->urb->num_sgs; i++) { 1631 u = (totlen > USB_SG_SIZE) ? USB_SG_SIZE : totlen; 1632 buf = kmalloc(u, GFP_KERNEL); 1633 if (!buf) { 1634 ret = -ENOMEM; 1635 goto error; 1636 } 1637 sg_set_buf(&as->urb->sg[i], buf, u); 1638 1639 if (!is_in) { 1640 if (copy_from_user(buf, uurb->buffer, u)) { 1641 ret = -EFAULT; 1642 goto error; 1643 } 1644 uurb->buffer += u; 1645 } 1646 totlen -= u; 1647 } 1648 } else if (uurb->buffer_length > 0) { 1649 if (as->usbm) { 1650 unsigned long uurb_start = (unsigned long)uurb->buffer; 1651 1652 as->urb->transfer_buffer = as->usbm->mem + 1653 (uurb_start - as->usbm->vm_start); 1654 } else { 1655 as->urb->transfer_buffer = kmalloc(uurb->buffer_length, 1656 GFP_KERNEL); 1657 if (!as->urb->transfer_buffer) { 1658 ret = -ENOMEM; 1659 goto error; 1660 } 1661 if (!is_in) { 1662 if (copy_from_user(as->urb->transfer_buffer, 1663 uurb->buffer, 1664 uurb->buffer_length)) { 1665 ret = -EFAULT; 1666 goto error; 1667 } 1668 } else if (uurb->type == USBDEVFS_URB_TYPE_ISO) { 1669 /* 1670 * Isochronous input data may end up being 1671 * discontiguous if some of the packets are 1672 * short. Clear the buffer so that the gaps 1673 * don't leak kernel data to userspace. 1674 */ 1675 memset(as->urb->transfer_buffer, 0, 1676 uurb->buffer_length); 1677 } 1678 } 1679 } 1680 as->urb->dev = ps->dev; 1681 as->urb->pipe = (uurb->type << 30) | 1682 __create_pipe(ps->dev, uurb->endpoint & 0xf) | 1683 (uurb->endpoint & USB_DIR_IN); 1684 1685 /* This tedious sequence is necessary because the URB_* flags 1686 * are internal to the kernel and subject to change, whereas 1687 * the USBDEVFS_URB_* flags are a user API and must not be changed. 1688 */ 1689 u = (is_in ? URB_DIR_IN : URB_DIR_OUT); 1690 if (uurb->flags & USBDEVFS_URB_ISO_ASAP) 1691 u |= URB_ISO_ASAP; 1692 if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK && is_in) 1693 u |= URB_SHORT_NOT_OK; 1694 if (uurb->flags & USBDEVFS_URB_NO_FSBR) 1695 u |= URB_NO_FSBR; 1696 if (uurb->flags & USBDEVFS_URB_ZERO_PACKET) 1697 u |= URB_ZERO_PACKET; 1698 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT) 1699 u |= URB_NO_INTERRUPT; 1700 as->urb->transfer_flags = u; 1701 1702 as->urb->transfer_buffer_length = uurb->buffer_length; 1703 as->urb->setup_packet = (unsigned char *)dr; 1704 dr = NULL; 1705 as->urb->start_frame = uurb->start_frame; 1706 as->urb->number_of_packets = number_of_packets; 1707 as->urb->stream_id = stream_id; 1708 1709 if (ep->desc.bInterval) { 1710 if (uurb->type == USBDEVFS_URB_TYPE_ISO || 1711 ps->dev->speed == USB_SPEED_HIGH || 1712 ps->dev->speed >= USB_SPEED_SUPER) 1713 as->urb->interval = 1 << 1714 min(15, ep->desc.bInterval - 1); 1715 else 1716 as->urb->interval = ep->desc.bInterval; 1717 } 1718 1719 as->urb->context = as; 1720 as->urb->complete = async_completed; 1721 for (totlen = u = 0; u < number_of_packets; u++) { 1722 as->urb->iso_frame_desc[u].offset = totlen; 1723 as->urb->iso_frame_desc[u].length = isopkt[u].length; 1724 totlen += isopkt[u].length; 1725 } 1726 kfree(isopkt); 1727 isopkt = NULL; 1728 as->ps = ps; 1729 as->userurb = arg; 1730 if (as->usbm) { 1731 unsigned long uurb_start = (unsigned long)uurb->buffer; 1732 1733 as->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1734 as->urb->transfer_dma = as->usbm->dma_handle + 1735 (uurb_start - as->usbm->vm_start); 1736 } else if (is_in && uurb->buffer_length > 0) 1737 as->userbuffer = uurb->buffer; 1738 as->signr = uurb->signr; 1739 as->ifnum = ifnum; 1740 as->pid = get_pid(task_pid(current)); 1741 as->cred = get_current_cred(); 1742 security_task_getsecid(current, &as->secid); 1743 snoop_urb(ps->dev, as->userurb, as->urb->pipe, 1744 as->urb->transfer_buffer_length, 0, SUBMIT, 1745 NULL, 0); 1746 if (!is_in) 1747 snoop_urb_data(as->urb, as->urb->transfer_buffer_length); 1748 1749 async_newpending(as); 1750 1751 if (usb_endpoint_xfer_bulk(&ep->desc)) { 1752 spin_lock_irq(&ps->lock); 1753 1754 /* Not exactly the endpoint address; the direction bit is 1755 * shifted to the 0x10 position so that the value will be 1756 * between 0 and 31. 1757 */ 1758 as->bulk_addr = usb_endpoint_num(&ep->desc) | 1759 ((ep->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK) 1760 >> 3); 1761 1762 /* If this bulk URB is the start of a new transfer, re-enable 1763 * the endpoint. Otherwise mark it as a continuation URB. 1764 */ 1765 if (uurb->flags & USBDEVFS_URB_BULK_CONTINUATION) 1766 as->bulk_status = AS_CONTINUATION; 1767 else 1768 ps->disabled_bulk_eps &= ~(1 << as->bulk_addr); 1769 1770 /* Don't accept continuation URBs if the endpoint is 1771 * disabled because of an earlier error. 1772 */ 1773 if (ps->disabled_bulk_eps & (1 << as->bulk_addr)) 1774 ret = -EREMOTEIO; 1775 else 1776 ret = usb_submit_urb(as->urb, GFP_ATOMIC); 1777 spin_unlock_irq(&ps->lock); 1778 } else { 1779 ret = usb_submit_urb(as->urb, GFP_KERNEL); 1780 } 1781 1782 if (ret) { 1783 dev_printk(KERN_DEBUG, &ps->dev->dev, 1784 "usbfs: usb_submit_urb returned %d\n", ret); 1785 snoop_urb(ps->dev, as->userurb, as->urb->pipe, 1786 0, ret, COMPLETE, NULL, 0); 1787 async_removepending(as); 1788 goto error; 1789 } 1790 return 0; 1791 1792 error: 1793 if (as && as->usbm) 1794 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count); 1795 kfree(isopkt); 1796 kfree(dr); 1797 if (as) 1798 free_async(as); 1799 return ret; 1800 } 1801 1802 static int proc_submiturb(struct usb_dev_state *ps, void __user *arg) 1803 { 1804 struct usbdevfs_urb uurb; 1805 1806 if (copy_from_user(&uurb, arg, sizeof(uurb))) 1807 return -EFAULT; 1808 1809 return proc_do_submiturb(ps, &uurb, 1810 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc), 1811 arg); 1812 } 1813 1814 static int proc_unlinkurb(struct usb_dev_state *ps, void __user *arg) 1815 { 1816 struct urb *urb; 1817 struct async *as; 1818 unsigned long flags; 1819 1820 spin_lock_irqsave(&ps->lock, flags); 1821 as = async_getpending(ps, arg); 1822 if (!as) { 1823 spin_unlock_irqrestore(&ps->lock, flags); 1824 return -EINVAL; 1825 } 1826 1827 urb = as->urb; 1828 usb_get_urb(urb); 1829 spin_unlock_irqrestore(&ps->lock, flags); 1830 1831 usb_kill_urb(urb); 1832 usb_put_urb(urb); 1833 1834 return 0; 1835 } 1836 1837 static int processcompl(struct async *as, void __user * __user *arg) 1838 { 1839 struct urb *urb = as->urb; 1840 struct usbdevfs_urb __user *userurb = as->userurb; 1841 void __user *addr = as->userurb; 1842 unsigned int i; 1843 1844 if (as->userbuffer && urb->actual_length) { 1845 if (copy_urb_data_to_user(as->userbuffer, urb)) 1846 goto err_out; 1847 } 1848 if (put_user(as->status, &userurb->status)) 1849 goto err_out; 1850 if (put_user(urb->actual_length, &userurb->actual_length)) 1851 goto err_out; 1852 if (put_user(urb->error_count, &userurb->error_count)) 1853 goto err_out; 1854 1855 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) { 1856 for (i = 0; i < urb->number_of_packets; i++) { 1857 if (put_user(urb->iso_frame_desc[i].actual_length, 1858 &userurb->iso_frame_desc[i].actual_length)) 1859 goto err_out; 1860 if (put_user(urb->iso_frame_desc[i].status, 1861 &userurb->iso_frame_desc[i].status)) 1862 goto err_out; 1863 } 1864 } 1865 1866 if (put_user(addr, (void __user * __user *)arg)) 1867 return -EFAULT; 1868 return 0; 1869 1870 err_out: 1871 return -EFAULT; 1872 } 1873 1874 static struct async *reap_as(struct usb_dev_state *ps) 1875 { 1876 DECLARE_WAITQUEUE(wait, current); 1877 struct async *as = NULL; 1878 struct usb_device *dev = ps->dev; 1879 1880 add_wait_queue(&ps->wait, &wait); 1881 for (;;) { 1882 __set_current_state(TASK_INTERRUPTIBLE); 1883 as = async_getcompleted(ps); 1884 if (as || !connected(ps)) 1885 break; 1886 if (signal_pending(current)) 1887 break; 1888 usb_unlock_device(dev); 1889 schedule(); 1890 usb_lock_device(dev); 1891 } 1892 remove_wait_queue(&ps->wait, &wait); 1893 set_current_state(TASK_RUNNING); 1894 return as; 1895 } 1896 1897 static int proc_reapurb(struct usb_dev_state *ps, void __user *arg) 1898 { 1899 struct async *as = reap_as(ps); 1900 1901 if (as) { 1902 int retval; 1903 1904 snoop(&ps->dev->dev, "reap %pK\n", as->userurb); 1905 retval = processcompl(as, (void __user * __user *)arg); 1906 free_async(as); 1907 return retval; 1908 } 1909 if (signal_pending(current)) 1910 return -EINTR; 1911 return -ENODEV; 1912 } 1913 1914 static int proc_reapurbnonblock(struct usb_dev_state *ps, void __user *arg) 1915 { 1916 int retval; 1917 struct async *as; 1918 1919 as = async_getcompleted(ps); 1920 if (as) { 1921 snoop(&ps->dev->dev, "reap %pK\n", as->userurb); 1922 retval = processcompl(as, (void __user * __user *)arg); 1923 free_async(as); 1924 } else { 1925 retval = (connected(ps) ? -EAGAIN : -ENODEV); 1926 } 1927 return retval; 1928 } 1929 1930 #ifdef CONFIG_COMPAT 1931 static int proc_control_compat(struct usb_dev_state *ps, 1932 struct usbdevfs_ctrltransfer32 __user *p32) 1933 { 1934 struct usbdevfs_ctrltransfer __user *p; 1935 __u32 udata; 1936 p = compat_alloc_user_space(sizeof(*p)); 1937 if (copy_in_user(p, p32, (sizeof(*p32) - sizeof(compat_caddr_t))) || 1938 get_user(udata, &p32->data) || 1939 put_user(compat_ptr(udata), &p->data)) 1940 return -EFAULT; 1941 return proc_control(ps, p); 1942 } 1943 1944 static int proc_bulk_compat(struct usb_dev_state *ps, 1945 struct usbdevfs_bulktransfer32 __user *p32) 1946 { 1947 struct usbdevfs_bulktransfer __user *p; 1948 compat_uint_t n; 1949 compat_caddr_t addr; 1950 1951 p = compat_alloc_user_space(sizeof(*p)); 1952 1953 if (get_user(n, &p32->ep) || put_user(n, &p->ep) || 1954 get_user(n, &p32->len) || put_user(n, &p->len) || 1955 get_user(n, &p32->timeout) || put_user(n, &p->timeout) || 1956 get_user(addr, &p32->data) || put_user(compat_ptr(addr), &p->data)) 1957 return -EFAULT; 1958 1959 return proc_bulk(ps, p); 1960 } 1961 static int proc_disconnectsignal_compat(struct usb_dev_state *ps, void __user *arg) 1962 { 1963 struct usbdevfs_disconnectsignal32 ds; 1964 1965 if (copy_from_user(&ds, arg, sizeof(ds))) 1966 return -EFAULT; 1967 ps->discsignr = ds.signr; 1968 ps->disccontext = compat_ptr(ds.context); 1969 return 0; 1970 } 1971 1972 static int get_urb32(struct usbdevfs_urb *kurb, 1973 struct usbdevfs_urb32 __user *uurb) 1974 { 1975 struct usbdevfs_urb32 urb32; 1976 if (copy_from_user(&urb32, uurb, sizeof(*uurb))) 1977 return -EFAULT; 1978 kurb->type = urb32.type; 1979 kurb->endpoint = urb32.endpoint; 1980 kurb->status = urb32.status; 1981 kurb->flags = urb32.flags; 1982 kurb->buffer = compat_ptr(urb32.buffer); 1983 kurb->buffer_length = urb32.buffer_length; 1984 kurb->actual_length = urb32.actual_length; 1985 kurb->start_frame = urb32.start_frame; 1986 kurb->number_of_packets = urb32.number_of_packets; 1987 kurb->error_count = urb32.error_count; 1988 kurb->signr = urb32.signr; 1989 kurb->usercontext = compat_ptr(urb32.usercontext); 1990 return 0; 1991 } 1992 1993 static int proc_submiturb_compat(struct usb_dev_state *ps, void __user *arg) 1994 { 1995 struct usbdevfs_urb uurb; 1996 1997 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg)) 1998 return -EFAULT; 1999 2000 return proc_do_submiturb(ps, &uurb, 2001 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc, 2002 arg); 2003 } 2004 2005 static int processcompl_compat(struct async *as, void __user * __user *arg) 2006 { 2007 struct urb *urb = as->urb; 2008 struct usbdevfs_urb32 __user *userurb = as->userurb; 2009 void __user *addr = as->userurb; 2010 unsigned int i; 2011 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