1 /*****************************************************************************/ 2 3 /* 4 * devio.c -- User space communication with USB devices. 5 * 6 * Copyright (C) 1999-2000 Thomas Sailer (sailer@ife.ee.ethz.ch) 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 * 22 * This file implements the usbfs/x/y files, where 23 * x is the bus number and y the device number. 24 * 25 * It allows user space programs/"drivers" to communicate directly 26 * with USB devices without intervening kernel driver. 27 * 28 * Revision history 29 * 22.12.1999 0.1 Initial release (split from proc_usb.c) 30 * 04.01.2000 0.2 Turned into its own filesystem 31 * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery 32 * (CAN-2005-3055) 33 */ 34 35 /*****************************************************************************/ 36 37 #include <linux/fs.h> 38 #include <linux/mm.h> 39 #include <linux/slab.h> 40 #include <linux/smp_lock.h> 41 #include <linux/signal.h> 42 #include <linux/poll.h> 43 #include <linux/module.h> 44 #include <linux/usb.h> 45 #include <linux/usbdevice_fs.h> 46 #include <linux/cdev.h> 47 #include <linux/notifier.h> 48 #include <linux/security.h> 49 #include <asm/uaccess.h> 50 #include <asm/byteorder.h> 51 #include <linux/moduleparam.h> 52 53 #include "hcd.h" /* for usbcore internals */ 54 #include "usb.h" 55 56 #define USB_MAXBUS 64 57 #define USB_DEVICE_MAX USB_MAXBUS * 128 58 59 /* Mutual exclusion for removal, open, and release */ 60 DEFINE_MUTEX(usbfs_mutex); 61 62 struct dev_state { 63 struct list_head list; /* state list */ 64 struct usb_device *dev; 65 struct file *file; 66 spinlock_t lock; /* protects the async urb lists */ 67 struct list_head async_pending; 68 struct list_head async_completed; 69 wait_queue_head_t wait; /* wake up if a request completed */ 70 unsigned int discsignr; 71 struct pid *disc_pid; 72 uid_t disc_uid, disc_euid; 73 void __user *disccontext; 74 unsigned long ifclaimed; 75 u32 secid; 76 }; 77 78 struct async { 79 struct list_head asynclist; 80 struct dev_state *ps; 81 struct pid *pid; 82 uid_t uid, euid; 83 unsigned int signr; 84 unsigned int ifnum; 85 void __user *userbuffer; 86 void __user *userurb; 87 struct urb *urb; 88 int status; 89 u32 secid; 90 }; 91 92 static int usbfs_snoop; 93 module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR); 94 MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic"); 95 96 #define snoop(dev, format, arg...) \ 97 do { \ 98 if (usbfs_snoop) \ 99 dev_info(dev , format , ## arg); \ 100 } while (0) 101 102 #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0) 103 104 105 #define MAX_USBFS_BUFFER_SIZE 16384 106 107 static int connected(struct dev_state *ps) 108 { 109 return (!list_empty(&ps->list) && 110 ps->dev->state != USB_STATE_NOTATTACHED); 111 } 112 113 static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig) 114 { 115 loff_t ret; 116 117 lock_kernel(); 118 119 switch (orig) { 120 case 0: 121 file->f_pos = offset; 122 ret = file->f_pos; 123 break; 124 case 1: 125 file->f_pos += offset; 126 ret = file->f_pos; 127 break; 128 case 2: 129 default: 130 ret = -EINVAL; 131 } 132 133 unlock_kernel(); 134 return ret; 135 } 136 137 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes, 138 loff_t *ppos) 139 { 140 struct dev_state *ps = file->private_data; 141 struct usb_device *dev = ps->dev; 142 ssize_t ret = 0; 143 unsigned len; 144 loff_t pos; 145 int i; 146 147 pos = *ppos; 148 usb_lock_device(dev); 149 if (!connected(ps)) { 150 ret = -ENODEV; 151 goto err; 152 } else if (pos < 0) { 153 ret = -EINVAL; 154 goto err; 155 } 156 157 if (pos < sizeof(struct usb_device_descriptor)) { 158 /* 18 bytes - fits on the stack */ 159 struct usb_device_descriptor temp_desc; 160 161 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor)); 162 le16_to_cpus(&temp_desc.bcdUSB); 163 le16_to_cpus(&temp_desc.idVendor); 164 le16_to_cpus(&temp_desc.idProduct); 165 le16_to_cpus(&temp_desc.bcdDevice); 166 167 len = sizeof(struct usb_device_descriptor) - pos; 168 if (len > nbytes) 169 len = nbytes; 170 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) { 171 ret = -EFAULT; 172 goto err; 173 } 174 175 *ppos += len; 176 buf += len; 177 nbytes -= len; 178 ret += len; 179 } 180 181 pos = sizeof(struct usb_device_descriptor); 182 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) { 183 struct usb_config_descriptor *config = 184 (struct usb_config_descriptor *)dev->rawdescriptors[i]; 185 unsigned int length = le16_to_cpu(config->wTotalLength); 186 187 if (*ppos < pos + length) { 188 189 /* The descriptor may claim to be longer than it 190 * really is. Here is the actual allocated length. */ 191 unsigned alloclen = 192 le16_to_cpu(dev->config[i].desc.wTotalLength); 193 194 len = length - (*ppos - pos); 195 if (len > nbytes) 196 len = nbytes; 197 198 /* Simply don't write (skip over) unallocated parts */ 199 if (alloclen > (*ppos - pos)) { 200 alloclen -= (*ppos - pos); 201 if (copy_to_user(buf, 202 dev->rawdescriptors[i] + (*ppos - pos), 203 min(len, alloclen))) { 204 ret = -EFAULT; 205 goto err; 206 } 207 } 208 209 *ppos += len; 210 buf += len; 211 nbytes -= len; 212 ret += len; 213 } 214 215 pos += length; 216 } 217 218 err: 219 usb_unlock_device(dev); 220 return ret; 221 } 222 223 /* 224 * async list handling 225 */ 226 227 static struct async *alloc_async(unsigned int numisoframes) 228 { 229 struct async *as; 230 231 as = kzalloc(sizeof(struct async), GFP_KERNEL); 232 if (!as) 233 return NULL; 234 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL); 235 if (!as->urb) { 236 kfree(as); 237 return NULL; 238 } 239 return as; 240 } 241 242 static void free_async(struct async *as) 243 { 244 put_pid(as->pid); 245 kfree(as->urb->transfer_buffer); 246 kfree(as->urb->setup_packet); 247 usb_free_urb(as->urb); 248 kfree(as); 249 } 250 251 static void async_newpending(struct async *as) 252 { 253 struct dev_state *ps = as->ps; 254 unsigned long flags; 255 256 spin_lock_irqsave(&ps->lock, flags); 257 list_add_tail(&as->asynclist, &ps->async_pending); 258 spin_unlock_irqrestore(&ps->lock, flags); 259 } 260 261 static void async_removepending(struct async *as) 262 { 263 struct dev_state *ps = as->ps; 264 unsigned long flags; 265 266 spin_lock_irqsave(&ps->lock, flags); 267 list_del_init(&as->asynclist); 268 spin_unlock_irqrestore(&ps->lock, flags); 269 } 270 271 static struct async *async_getcompleted(struct dev_state *ps) 272 { 273 unsigned long flags; 274 struct async *as = NULL; 275 276 spin_lock_irqsave(&ps->lock, flags); 277 if (!list_empty(&ps->async_completed)) { 278 as = list_entry(ps->async_completed.next, struct async, 279 asynclist); 280 list_del_init(&as->asynclist); 281 } 282 spin_unlock_irqrestore(&ps->lock, flags); 283 return as; 284 } 285 286 static struct async *async_getpending(struct dev_state *ps, 287 void __user *userurb) 288 { 289 unsigned long flags; 290 struct async *as; 291 292 spin_lock_irqsave(&ps->lock, flags); 293 list_for_each_entry(as, &ps->async_pending, asynclist) 294 if (as->userurb == userurb) { 295 list_del_init(&as->asynclist); 296 spin_unlock_irqrestore(&ps->lock, flags); 297 return as; 298 } 299 spin_unlock_irqrestore(&ps->lock, flags); 300 return NULL; 301 } 302 303 static void snoop_urb(struct urb *urb, void __user *userurb) 304 { 305 unsigned j; 306 unsigned char *data = urb->transfer_buffer; 307 308 if (!usbfs_snoop) 309 return; 310 311 dev_info(&urb->dev->dev, "direction=%s\n", 312 usb_urb_dir_in(urb) ? "IN" : "OUT"); 313 dev_info(&urb->dev->dev, "userurb=%p\n", userurb); 314 dev_info(&urb->dev->dev, "transfer_buffer_length=%u\n", 315 urb->transfer_buffer_length); 316 dev_info(&urb->dev->dev, "actual_length=%u\n", urb->actual_length); 317 dev_info(&urb->dev->dev, "data: "); 318 for (j = 0; j < urb->transfer_buffer_length; ++j) 319 printk("%02x ", data[j]); 320 printk("\n"); 321 } 322 323 static void async_completed(struct urb *urb) 324 { 325 struct async *as = urb->context; 326 struct dev_state *ps = as->ps; 327 struct siginfo sinfo; 328 struct pid *pid = NULL; 329 uid_t uid = 0; 330 uid_t euid = 0; 331 u32 secid = 0; 332 int signr; 333 334 spin_lock(&ps->lock); 335 list_move_tail(&as->asynclist, &ps->async_completed); 336 as->status = urb->status; 337 signr = as->signr; 338 if (signr) { 339 sinfo.si_signo = as->signr; 340 sinfo.si_errno = as->status; 341 sinfo.si_code = SI_ASYNCIO; 342 sinfo.si_addr = as->userurb; 343 pid = as->pid; 344 uid = as->uid; 345 euid = as->euid; 346 secid = as->secid; 347 } 348 snoop(&urb->dev->dev, "urb complete\n"); 349 snoop_urb(urb, as->userurb); 350 spin_unlock(&ps->lock); 351 352 if (signr) 353 kill_pid_info_as_uid(sinfo.si_signo, &sinfo, pid, uid, 354 euid, secid); 355 356 wake_up(&ps->wait); 357 } 358 359 static void destroy_async(struct dev_state *ps, struct list_head *list) 360 { 361 struct async *as; 362 unsigned long flags; 363 364 spin_lock_irqsave(&ps->lock, flags); 365 while (!list_empty(list)) { 366 as = list_entry(list->next, struct async, asynclist); 367 list_del_init(&as->asynclist); 368 369 /* drop the spinlock so the completion handler can run */ 370 spin_unlock_irqrestore(&ps->lock, flags); 371 usb_kill_urb(as->urb); 372 spin_lock_irqsave(&ps->lock, flags); 373 } 374 spin_unlock_irqrestore(&ps->lock, flags); 375 } 376 377 static void destroy_async_on_interface(struct dev_state *ps, 378 unsigned int ifnum) 379 { 380 struct list_head *p, *q, hitlist; 381 unsigned long flags; 382 383 INIT_LIST_HEAD(&hitlist); 384 spin_lock_irqsave(&ps->lock, flags); 385 list_for_each_safe(p, q, &ps->async_pending) 386 if (ifnum == list_entry(p, struct async, asynclist)->ifnum) 387 list_move_tail(p, &hitlist); 388 spin_unlock_irqrestore(&ps->lock, flags); 389 destroy_async(ps, &hitlist); 390 } 391 392 static void destroy_all_async(struct dev_state *ps) 393 { 394 destroy_async(ps, &ps->async_pending); 395 } 396 397 /* 398 * interface claims are made only at the request of user level code, 399 * which can also release them (explicitly or by closing files). 400 * they're also undone when devices disconnect. 401 */ 402 403 static int driver_probe(struct usb_interface *intf, 404 const struct usb_device_id *id) 405 { 406 return -ENODEV; 407 } 408 409 static void driver_disconnect(struct usb_interface *intf) 410 { 411 struct dev_state *ps = usb_get_intfdata(intf); 412 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber; 413 414 if (!ps) 415 return; 416 417 /* NOTE: this relies on usbcore having canceled and completed 418 * all pending I/O requests; 2.6 does that. 419 */ 420 421 if (likely(ifnum < 8*sizeof(ps->ifclaimed))) 422 clear_bit(ifnum, &ps->ifclaimed); 423 else 424 dev_warn(&intf->dev, "interface number %u out of range\n", 425 ifnum); 426 427 usb_set_intfdata(intf, NULL); 428 429 /* force async requests to complete */ 430 destroy_async_on_interface(ps, ifnum); 431 } 432 433 /* The following routines are merely placeholders. There is no way 434 * to inform a user task about suspend or resumes. 435 */ 436 static int driver_suspend(struct usb_interface *intf, pm_message_t msg) 437 { 438 return 0; 439 } 440 441 static int driver_resume(struct usb_interface *intf) 442 { 443 return 0; 444 } 445 446 struct usb_driver usbfs_driver = { 447 .name = "usbfs", 448 .probe = driver_probe, 449 .disconnect = driver_disconnect, 450 .suspend = driver_suspend, 451 .resume = driver_resume, 452 }; 453 454 static int claimintf(struct dev_state *ps, unsigned int ifnum) 455 { 456 struct usb_device *dev = ps->dev; 457 struct usb_interface *intf; 458 int err; 459 460 if (ifnum >= 8*sizeof(ps->ifclaimed)) 461 return -EINVAL; 462 /* already claimed */ 463 if (test_bit(ifnum, &ps->ifclaimed)) 464 return 0; 465 466 intf = usb_ifnum_to_if(dev, ifnum); 467 if (!intf) 468 err = -ENOENT; 469 else 470 err = usb_driver_claim_interface(&usbfs_driver, intf, ps); 471 if (err == 0) 472 set_bit(ifnum, &ps->ifclaimed); 473 return err; 474 } 475 476 static int releaseintf(struct dev_state *ps, unsigned int ifnum) 477 { 478 struct usb_device *dev; 479 struct usb_interface *intf; 480 int err; 481 482 err = -EINVAL; 483 if (ifnum >= 8*sizeof(ps->ifclaimed)) 484 return err; 485 dev = ps->dev; 486 intf = usb_ifnum_to_if(dev, ifnum); 487 if (!intf) 488 err = -ENOENT; 489 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) { 490 usb_driver_release_interface(&usbfs_driver, intf); 491 err = 0; 492 } 493 return err; 494 } 495 496 static int checkintf(struct dev_state *ps, unsigned int ifnum) 497 { 498 if (ps->dev->state != USB_STATE_CONFIGURED) 499 return -EHOSTUNREACH; 500 if (ifnum >= 8*sizeof(ps->ifclaimed)) 501 return -EINVAL; 502 if (test_bit(ifnum, &ps->ifclaimed)) 503 return 0; 504 /* if not yet claimed, claim it for the driver */ 505 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim " 506 "interface %u before use\n", task_pid_nr(current), 507 current->comm, ifnum); 508 return claimintf(ps, ifnum); 509 } 510 511 static int findintfep(struct usb_device *dev, unsigned int ep) 512 { 513 unsigned int i, j, e; 514 struct usb_interface *intf; 515 struct usb_host_interface *alts; 516 struct usb_endpoint_descriptor *endpt; 517 518 if (ep & ~(USB_DIR_IN|0xf)) 519 return -EINVAL; 520 if (!dev->actconfig) 521 return -ESRCH; 522 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { 523 intf = dev->actconfig->interface[i]; 524 for (j = 0; j < intf->num_altsetting; j++) { 525 alts = &intf->altsetting[j]; 526 for (e = 0; e < alts->desc.bNumEndpoints; e++) { 527 endpt = &alts->endpoint[e].desc; 528 if (endpt->bEndpointAddress == ep) 529 return alts->desc.bInterfaceNumber; 530 } 531 } 532 } 533 return -ENOENT; 534 } 535 536 static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype, 537 unsigned int index) 538 { 539 int ret = 0; 540 541 if (ps->dev->state != USB_STATE_UNAUTHENTICATED 542 && ps->dev->state != USB_STATE_ADDRESS 543 && ps->dev->state != USB_STATE_CONFIGURED) 544 return -EHOSTUNREACH; 545 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype)) 546 return 0; 547 548 index &= 0xff; 549 switch (requesttype & USB_RECIP_MASK) { 550 case USB_RECIP_ENDPOINT: 551 ret = findintfep(ps->dev, index); 552 if (ret >= 0) 553 ret = checkintf(ps, ret); 554 break; 555 556 case USB_RECIP_INTERFACE: 557 ret = checkintf(ps, index); 558 break; 559 } 560 return ret; 561 } 562 563 static int match_devt(struct device *dev, void *data) 564 { 565 return dev->devt == (dev_t) (unsigned long) data; 566 } 567 568 static struct usb_device *usbdev_lookup_by_devt(dev_t devt) 569 { 570 struct device *dev; 571 572 dev = bus_find_device(&usb_bus_type, NULL, 573 (void *) (unsigned long) devt, match_devt); 574 if (!dev) 575 return NULL; 576 return container_of(dev, struct usb_device, dev); 577 } 578 579 /* 580 * file operations 581 */ 582 static int usbdev_open(struct inode *inode, struct file *file) 583 { 584 struct usb_device *dev = NULL; 585 struct dev_state *ps; 586 const struct cred *cred = current_cred(); 587 int ret; 588 589 lock_kernel(); 590 /* Protect against simultaneous removal or release */ 591 mutex_lock(&usbfs_mutex); 592 593 ret = -ENOMEM; 594 ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL); 595 if (!ps) 596 goto out; 597 598 ret = -ENODEV; 599 600 /* usbdev device-node */ 601 if (imajor(inode) == USB_DEVICE_MAJOR) 602 dev = usbdev_lookup_by_devt(inode->i_rdev); 603 #ifdef CONFIG_USB_DEVICEFS 604 /* procfs file */ 605 if (!dev) { 606 dev = inode->i_private; 607 if (dev && dev->usbfs_dentry && 608 dev->usbfs_dentry->d_inode == inode) 609 usb_get_dev(dev); 610 else 611 dev = NULL; 612 } 613 #endif 614 if (!dev || dev->state == USB_STATE_NOTATTACHED) 615 goto out; 616 ret = usb_autoresume_device(dev); 617 if (ret) 618 goto out; 619 620 ret = 0; 621 ps->dev = dev; 622 ps->file = file; 623 spin_lock_init(&ps->lock); 624 INIT_LIST_HEAD(&ps->list); 625 INIT_LIST_HEAD(&ps->async_pending); 626 INIT_LIST_HEAD(&ps->async_completed); 627 init_waitqueue_head(&ps->wait); 628 ps->discsignr = 0; 629 ps->disc_pid = get_pid(task_pid(current)); 630 ps->disc_uid = cred->uid; 631 ps->disc_euid = cred->euid; 632 ps->disccontext = NULL; 633 ps->ifclaimed = 0; 634 security_task_getsecid(current, &ps->secid); 635 smp_wmb(); 636 list_add_tail(&ps->list, &dev->filelist); 637 file->private_data = ps; 638 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current), 639 current->comm); 640 out: 641 if (ret) { 642 kfree(ps); 643 usb_put_dev(dev); 644 } 645 mutex_unlock(&usbfs_mutex); 646 unlock_kernel(); 647 return ret; 648 } 649 650 static int usbdev_release(struct inode *inode, struct file *file) 651 { 652 struct dev_state *ps = file->private_data; 653 struct usb_device *dev = ps->dev; 654 unsigned int ifnum; 655 struct async *as; 656 657 usb_lock_device(dev); 658 659 /* Protect against simultaneous open */ 660 mutex_lock(&usbfs_mutex); 661 list_del_init(&ps->list); 662 mutex_unlock(&usbfs_mutex); 663 664 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed); 665 ifnum++) { 666 if (test_bit(ifnum, &ps->ifclaimed)) 667 releaseintf(ps, ifnum); 668 } 669 destroy_all_async(ps); 670 usb_autosuspend_device(dev); 671 usb_unlock_device(dev); 672 usb_put_dev(dev); 673 put_pid(ps->disc_pid); 674 675 as = async_getcompleted(ps); 676 while (as) { 677 free_async(as); 678 as = async_getcompleted(ps); 679 } 680 kfree(ps); 681 return 0; 682 } 683 684 static int proc_control(struct dev_state *ps, void __user *arg) 685 { 686 struct usb_device *dev = ps->dev; 687 struct usbdevfs_ctrltransfer ctrl; 688 unsigned int tmo; 689 unsigned char *tbuf; 690 unsigned wLength; 691 int i, j, ret; 692 693 if (copy_from_user(&ctrl, arg, sizeof(ctrl))) 694 return -EFAULT; 695 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex); 696 if (ret) 697 return ret; 698 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */ 699 if (wLength > PAGE_SIZE) 700 return -EINVAL; 701 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL); 702 if (!tbuf) 703 return -ENOMEM; 704 tmo = ctrl.timeout; 705 if (ctrl.bRequestType & 0x80) { 706 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data, 707 ctrl.wLength)) { 708 free_page((unsigned long)tbuf); 709 return -EINVAL; 710 } 711 snoop(&dev->dev, "control read: bRequest=%02x " 712 "bRrequestType=%02x wValue=%04x " 713 "wIndex=%04x wLength=%04x\n", 714 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue, 715 ctrl.wIndex, ctrl.wLength); 716 717 usb_unlock_device(dev); 718 i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest, 719 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex, 720 tbuf, ctrl.wLength, tmo); 721 usb_lock_device(dev); 722 if ((i > 0) && ctrl.wLength) { 723 if (usbfs_snoop) { 724 dev_info(&dev->dev, "control read: data "); 725 for (j = 0; j < i; ++j) 726 printk("%02x ", (u8)(tbuf)[j]); 727 printk("\n"); 728 } 729 if (copy_to_user(ctrl.data, tbuf, i)) { 730 free_page((unsigned long)tbuf); 731 return -EFAULT; 732 } 733 } 734 } else { 735 if (ctrl.wLength) { 736 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) { 737 free_page((unsigned long)tbuf); 738 return -EFAULT; 739 } 740 } 741 snoop(&dev->dev, "control write: bRequest=%02x " 742 "bRrequestType=%02x wValue=%04x " 743 "wIndex=%04x wLength=%04x\n", 744 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue, 745 ctrl.wIndex, ctrl.wLength); 746 if (usbfs_snoop) { 747 dev_info(&dev->dev, "control write: data: "); 748 for (j = 0; j < ctrl.wLength; ++j) 749 printk("%02x ", (unsigned char)(tbuf)[j]); 750 printk("\n"); 751 } 752 usb_unlock_device(dev); 753 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest, 754 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex, 755 tbuf, ctrl.wLength, tmo); 756 usb_lock_device(dev); 757 } 758 free_page((unsigned long)tbuf); 759 if (i < 0 && i != -EPIPE) { 760 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL " 761 "failed cmd %s rqt %u rq %u len %u ret %d\n", 762 current->comm, ctrl.bRequestType, ctrl.bRequest, 763 ctrl.wLength, i); 764 } 765 return i; 766 } 767 768 static int proc_bulk(struct dev_state *ps, void __user *arg) 769 { 770 struct usb_device *dev = ps->dev; 771 struct usbdevfs_bulktransfer bulk; 772 unsigned int tmo, len1, pipe; 773 int len2; 774 unsigned char *tbuf; 775 int i, j, ret; 776 777 if (copy_from_user(&bulk, arg, sizeof(bulk))) 778 return -EFAULT; 779 ret = findintfep(ps->dev, bulk.ep); 780 if (ret < 0) 781 return ret; 782 ret = checkintf(ps, ret); 783 if (ret) 784 return ret; 785 if (bulk.ep & USB_DIR_IN) 786 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f); 787 else 788 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f); 789 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN))) 790 return -EINVAL; 791 len1 = bulk.len; 792 if (len1 > MAX_USBFS_BUFFER_SIZE) 793 return -EINVAL; 794 if (!(tbuf = kmalloc(len1, GFP_KERNEL))) 795 return -ENOMEM; 796 tmo = bulk.timeout; 797 if (bulk.ep & 0x80) { 798 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) { 799 kfree(tbuf); 800 return -EINVAL; 801 } 802 snoop(&dev->dev, "bulk read: len=0x%02x timeout=%04d\n", 803 bulk.len, bulk.timeout); 804 usb_unlock_device(dev); 805 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo); 806 usb_lock_device(dev); 807 if (!i && len2) { 808 if (usbfs_snoop) { 809 dev_info(&dev->dev, "bulk read: data "); 810 for (j = 0; j < len2; ++j) 811 printk("%02x ", (u8)(tbuf)[j]); 812 printk("\n"); 813 } 814 if (copy_to_user(bulk.data, tbuf, len2)) { 815 kfree(tbuf); 816 return -EFAULT; 817 } 818 } 819 } else { 820 if (len1) { 821 if (copy_from_user(tbuf, bulk.data, len1)) { 822 kfree(tbuf); 823 return -EFAULT; 824 } 825 } 826 snoop(&dev->dev, "bulk write: len=0x%02x timeout=%04d\n", 827 bulk.len, bulk.timeout); 828 if (usbfs_snoop) { 829 dev_info(&dev->dev, "bulk write: data: "); 830 for (j = 0; j < len1; ++j) 831 printk("%02x ", (unsigned char)(tbuf)[j]); 832 printk("\n"); 833 } 834 usb_unlock_device(dev); 835 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo); 836 usb_lock_device(dev); 837 } 838 kfree(tbuf); 839 if (i < 0) 840 return i; 841 return len2; 842 } 843 844 static int proc_resetep(struct dev_state *ps, void __user *arg) 845 { 846 unsigned int ep; 847 int ret; 848 849 if (get_user(ep, (unsigned int __user *)arg)) 850 return -EFAULT; 851 ret = findintfep(ps->dev, ep); 852 if (ret < 0) 853 return ret; 854 ret = checkintf(ps, ret); 855 if (ret) 856 return ret; 857 usb_reset_endpoint(ps->dev, ep); 858 return 0; 859 } 860 861 static int proc_clearhalt(struct dev_state *ps, void __user *arg) 862 { 863 unsigned int ep; 864 int pipe; 865 int ret; 866 867 if (get_user(ep, (unsigned int __user *)arg)) 868 return -EFAULT; 869 ret = findintfep(ps->dev, ep); 870 if (ret < 0) 871 return ret; 872 ret = checkintf(ps, ret); 873 if (ret) 874 return ret; 875 if (ep & USB_DIR_IN) 876 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f); 877 else 878 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f); 879 880 return usb_clear_halt(ps->dev, pipe); 881 } 882 883 static int proc_getdriver(struct dev_state *ps, void __user *arg) 884 { 885 struct usbdevfs_getdriver gd; 886 struct usb_interface *intf; 887 int ret; 888 889 if (copy_from_user(&gd, arg, sizeof(gd))) 890 return -EFAULT; 891 intf = usb_ifnum_to_if(ps->dev, gd.interface); 892 if (!intf || !intf->dev.driver) 893 ret = -ENODATA; 894 else { 895 strncpy(gd.driver, intf->dev.driver->name, 896 sizeof(gd.driver)); 897 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0); 898 } 899 return ret; 900 } 901 902 static int proc_connectinfo(struct dev_state *ps, void __user *arg) 903 { 904 struct usbdevfs_connectinfo ci; 905 906 ci.devnum = ps->dev->devnum; 907 ci.slow = ps->dev->speed == USB_SPEED_LOW; 908 if (copy_to_user(arg, &ci, sizeof(ci))) 909 return -EFAULT; 910 return 0; 911 } 912 913 static int proc_resetdevice(struct dev_state *ps) 914 { 915 return usb_reset_device(ps->dev); 916 } 917 918 static int proc_setintf(struct dev_state *ps, void __user *arg) 919 { 920 struct usbdevfs_setinterface setintf; 921 int ret; 922 923 if (copy_from_user(&setintf, arg, sizeof(setintf))) 924 return -EFAULT; 925 if ((ret = checkintf(ps, setintf.interface))) 926 return ret; 927 return usb_set_interface(ps->dev, setintf.interface, 928 setintf.altsetting); 929 } 930 931 static int proc_setconfig(struct dev_state *ps, void __user *arg) 932 { 933 int u; 934 int status = 0; 935 struct usb_host_config *actconfig; 936 937 if (get_user(u, (int __user *)arg)) 938 return -EFAULT; 939 940 actconfig = ps->dev->actconfig; 941 942 /* Don't touch the device if any interfaces are claimed. 943 * It could interfere with other drivers' operations, and if 944 * an interface is claimed by usbfs it could easily deadlock. 945 */ 946 if (actconfig) { 947 int i; 948 949 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) { 950 if (usb_interface_claimed(actconfig->interface[i])) { 951 dev_warn(&ps->dev->dev, 952 "usbfs: interface %d claimed by %s " 953 "while '%s' sets config #%d\n", 954 actconfig->interface[i] 955 ->cur_altsetting 956 ->desc.bInterfaceNumber, 957 actconfig->interface[i] 958 ->dev.driver->name, 959 current->comm, u); 960 status = -EBUSY; 961 break; 962 } 963 } 964 } 965 966 /* SET_CONFIGURATION is often abused as a "cheap" driver reset, 967 * so avoid usb_set_configuration()'s kick to sysfs 968 */ 969 if (status == 0) { 970 if (actconfig && actconfig->desc.bConfigurationValue == u) 971 status = usb_reset_configuration(ps->dev); 972 else 973 status = usb_set_configuration(ps->dev, u); 974 } 975 976 return status; 977 } 978 979 static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb, 980 struct usbdevfs_iso_packet_desc __user *iso_frame_desc, 981 void __user *arg) 982 { 983 struct usbdevfs_iso_packet_desc *isopkt = NULL; 984 struct usb_host_endpoint *ep; 985 struct async *as; 986 struct usb_ctrlrequest *dr = NULL; 987 const struct cred *cred = current_cred(); 988 unsigned int u, totlen, isofrmlen; 989 int ret, ifnum = -1; 990 int is_in; 991 992 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP | 993 USBDEVFS_URB_SHORT_NOT_OK | 994 USBDEVFS_URB_NO_FSBR | 995 USBDEVFS_URB_ZERO_PACKET | 996 USBDEVFS_URB_NO_INTERRUPT)) 997 return -EINVAL; 998 if (uurb->buffer_length > 0 && !uurb->buffer) 999 return -EINVAL; 1000 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL && 1001 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) { 1002 ifnum = findintfep(ps->dev, uurb->endpoint); 1003 if (ifnum < 0) 1004 return ifnum; 1005 ret = checkintf(ps, ifnum); 1006 if (ret) 1007 return ret; 1008 } 1009 if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) { 1010 is_in = 1; 1011 ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK]; 1012 } else { 1013 is_in = 0; 1014 ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK]; 1015 } 1016 if (!ep) 1017 return -ENOENT; 1018 switch(uurb->type) { 1019 case USBDEVFS_URB_TYPE_CONTROL: 1020 if (!usb_endpoint_xfer_control(&ep->desc)) 1021 return -EINVAL; 1022 /* min 8 byte setup packet, 1023 * max 8 byte setup plus an arbitrary data stage */ 1024 if (uurb->buffer_length < 8 || 1025 uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE)) 1026 return -EINVAL; 1027 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); 1028 if (!dr) 1029 return -ENOMEM; 1030 if (copy_from_user(dr, uurb->buffer, 8)) { 1031 kfree(dr); 1032 return -EFAULT; 1033 } 1034 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) { 1035 kfree(dr); 1036 return -EINVAL; 1037 } 1038 ret = check_ctrlrecip(ps, dr->bRequestType, 1039 le16_to_cpup(&dr->wIndex)); 1040 if (ret) { 1041 kfree(dr); 1042 return ret; 1043 } 1044 uurb->number_of_packets = 0; 1045 uurb->buffer_length = le16_to_cpup(&dr->wLength); 1046 uurb->buffer += 8; 1047 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) { 1048 is_in = 1; 1049 uurb->endpoint |= USB_DIR_IN; 1050 } else { 1051 is_in = 0; 1052 uurb->endpoint &= ~USB_DIR_IN; 1053 } 1054 snoop(&ps->dev->dev, "control urb: bRequest=%02x " 1055 "bRrequestType=%02x wValue=%04x " 1056 "wIndex=%04x wLength=%04x\n", 1057 dr->bRequest, dr->bRequestType, 1058 __le16_to_cpup(&dr->wValue), 1059 __le16_to_cpup(&dr->wIndex), 1060 __le16_to_cpup(&dr->wLength)); 1061 break; 1062 1063 case USBDEVFS_URB_TYPE_BULK: 1064 switch (usb_endpoint_type(&ep->desc)) { 1065 case USB_ENDPOINT_XFER_CONTROL: 1066 case USB_ENDPOINT_XFER_ISOC: 1067 return -EINVAL; 1068 /* allow single-shot interrupt transfers, at bogus rates */ 1069 } 1070 uurb->number_of_packets = 0; 1071 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE) 1072 return -EINVAL; 1073 snoop(&ps->dev->dev, "bulk urb\n"); 1074 break; 1075 1076 case USBDEVFS_URB_TYPE_ISO: 1077 /* arbitrary limit */ 1078 if (uurb->number_of_packets < 1 || 1079 uurb->number_of_packets > 128) 1080 return -EINVAL; 1081 if (!usb_endpoint_xfer_isoc(&ep->desc)) 1082 return -EINVAL; 1083 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) * 1084 uurb->number_of_packets; 1085 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL))) 1086 return -ENOMEM; 1087 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) { 1088 kfree(isopkt); 1089 return -EFAULT; 1090 } 1091 for (totlen = u = 0; u < uurb->number_of_packets; u++) { 1092 /* arbitrary limit, 1093 * sufficient for USB 2.0 high-bandwidth iso */ 1094 if (isopkt[u].length > 8192) { 1095 kfree(isopkt); 1096 return -EINVAL; 1097 } 1098 totlen += isopkt[u].length; 1099 } 1100 if (totlen > 32768) { 1101 kfree(isopkt); 1102 return -EINVAL; 1103 } 1104 uurb->buffer_length = totlen; 1105 snoop(&ps->dev->dev, "iso urb\n"); 1106 break; 1107 1108 case USBDEVFS_URB_TYPE_INTERRUPT: 1109 uurb->number_of_packets = 0; 1110 if (!usb_endpoint_xfer_int(&ep->desc)) 1111 return -EINVAL; 1112 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE) 1113 return -EINVAL; 1114 snoop(&ps->dev->dev, "interrupt urb\n"); 1115 break; 1116 1117 default: 1118 return -EINVAL; 1119 } 1120 if (uurb->buffer_length > 0 && 1121 !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ, 1122 uurb->buffer, uurb->buffer_length)) { 1123 kfree(isopkt); 1124 kfree(dr); 1125 return -EFAULT; 1126 } 1127 as = alloc_async(uurb->number_of_packets); 1128 if (!as) { 1129 kfree(isopkt); 1130 kfree(dr); 1131 return -ENOMEM; 1132 } 1133 if (uurb->buffer_length > 0) { 1134 as->urb->transfer_buffer = kmalloc(uurb->buffer_length, 1135 GFP_KERNEL); 1136 if (!as->urb->transfer_buffer) { 1137 kfree(isopkt); 1138 kfree(dr); 1139 free_async(as); 1140 return -ENOMEM; 1141 } 1142 } 1143 as->urb->dev = ps->dev; 1144 as->urb->pipe = (uurb->type << 30) | 1145 __create_pipe(ps->dev, uurb->endpoint & 0xf) | 1146 (uurb->endpoint & USB_DIR_IN); 1147 1148 /* This tedious sequence is necessary because the URB_* flags 1149 * are internal to the kernel and subject to change, whereas 1150 * the USBDEVFS_URB_* flags are a user API and must not be changed. 1151 */ 1152 u = (is_in ? URB_DIR_IN : URB_DIR_OUT); 1153 if (uurb->flags & USBDEVFS_URB_ISO_ASAP) 1154 u |= URB_ISO_ASAP; 1155 if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK) 1156 u |= URB_SHORT_NOT_OK; 1157 if (uurb->flags & USBDEVFS_URB_NO_FSBR) 1158 u |= URB_NO_FSBR; 1159 if (uurb->flags & USBDEVFS_URB_ZERO_PACKET) 1160 u |= URB_ZERO_PACKET; 1161 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT) 1162 u |= URB_NO_INTERRUPT; 1163 as->urb->transfer_flags = u; 1164 1165 as->urb->transfer_buffer_length = uurb->buffer_length; 1166 as->urb->setup_packet = (unsigned char *)dr; 1167 as->urb->start_frame = uurb->start_frame; 1168 as->urb->number_of_packets = uurb->number_of_packets; 1169 if (uurb->type == USBDEVFS_URB_TYPE_ISO || 1170 ps->dev->speed == USB_SPEED_HIGH) 1171 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1); 1172 else 1173 as->urb->interval = ep->desc.bInterval; 1174 as->urb->context = as; 1175 as->urb->complete = async_completed; 1176 for (totlen = u = 0; u < uurb->number_of_packets; u++) { 1177 as->urb->iso_frame_desc[u].offset = totlen; 1178 as->urb->iso_frame_desc[u].length = isopkt[u].length; 1179 totlen += isopkt[u].length; 1180 } 1181 kfree(isopkt); 1182 as->ps = ps; 1183 as->userurb = arg; 1184 if (is_in && uurb->buffer_length > 0) 1185 as->userbuffer = uurb->buffer; 1186 else 1187 as->userbuffer = NULL; 1188 as->signr = uurb->signr; 1189 as->ifnum = ifnum; 1190 as->pid = get_pid(task_pid(current)); 1191 as->uid = cred->uid; 1192 as->euid = cred->euid; 1193 security_task_getsecid(current, &as->secid); 1194 if (!is_in && uurb->buffer_length > 0) { 1195 if (copy_from_user(as->urb->transfer_buffer, uurb->buffer, 1196 uurb->buffer_length)) { 1197 free_async(as); 1198 return -EFAULT; 1199 } 1200 } 1201 snoop_urb(as->urb, as->userurb); 1202 async_newpending(as); 1203 if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) { 1204 dev_printk(KERN_DEBUG, &ps->dev->dev, 1205 "usbfs: usb_submit_urb returned %d\n", ret); 1206 async_removepending(as); 1207 free_async(as); 1208 return ret; 1209 } 1210 return 0; 1211 } 1212 1213 static int proc_submiturb(struct dev_state *ps, void __user *arg) 1214 { 1215 struct usbdevfs_urb uurb; 1216 1217 if (copy_from_user(&uurb, arg, sizeof(uurb))) 1218 return -EFAULT; 1219 1220 return proc_do_submiturb(ps, &uurb, 1221 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc), 1222 arg); 1223 } 1224 1225 static int proc_unlinkurb(struct dev_state *ps, void __user *arg) 1226 { 1227 struct async *as; 1228 1229 as = async_getpending(ps, arg); 1230 if (!as) 1231 return -EINVAL; 1232 usb_kill_urb(as->urb); 1233 return 0; 1234 } 1235 1236 static int processcompl(struct async *as, void __user * __user *arg) 1237 { 1238 struct urb *urb = as->urb; 1239 struct usbdevfs_urb __user *userurb = as->userurb; 1240 void __user *addr = as->userurb; 1241 unsigned int i; 1242 1243 if (as->userbuffer) 1244 if (copy_to_user(as->userbuffer, urb->transfer_buffer, 1245 urb->transfer_buffer_length)) 1246 goto err_out; 1247 if (put_user(as->status, &userurb->status)) 1248 goto err_out; 1249 if (put_user(urb->actual_length, &userurb->actual_length)) 1250 goto err_out; 1251 if (put_user(urb->error_count, &userurb->error_count)) 1252 goto err_out; 1253 1254 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) { 1255 for (i = 0; i < urb->number_of_packets; i++) { 1256 if (put_user(urb->iso_frame_desc[i].actual_length, 1257 &userurb->iso_frame_desc[i].actual_length)) 1258 goto err_out; 1259 if (put_user(urb->iso_frame_desc[i].status, 1260 &userurb->iso_frame_desc[i].status)) 1261 goto err_out; 1262 } 1263 } 1264 1265 free_async(as); 1266 1267 if (put_user(addr, (void __user * __user *)arg)) 1268 return -EFAULT; 1269 return 0; 1270 1271 err_out: 1272 free_async(as); 1273 return -EFAULT; 1274 } 1275 1276 static struct async *reap_as(struct dev_state *ps) 1277 { 1278 DECLARE_WAITQUEUE(wait, current); 1279 struct async *as = NULL; 1280 struct usb_device *dev = ps->dev; 1281 1282 add_wait_queue(&ps->wait, &wait); 1283 for (;;) { 1284 __set_current_state(TASK_INTERRUPTIBLE); 1285 as = async_getcompleted(ps); 1286 if (as) 1287 break; 1288 if (signal_pending(current)) 1289 break; 1290 usb_unlock_device(dev); 1291 schedule(); 1292 usb_lock_device(dev); 1293 } 1294 remove_wait_queue(&ps->wait, &wait); 1295 set_current_state(TASK_RUNNING); 1296 return as; 1297 } 1298 1299 static int proc_reapurb(struct dev_state *ps, void __user *arg) 1300 { 1301 struct async *as = reap_as(ps); 1302 if (as) 1303 return processcompl(as, (void __user * __user *)arg); 1304 if (signal_pending(current)) 1305 return -EINTR; 1306 return -EIO; 1307 } 1308 1309 static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg) 1310 { 1311 struct async *as; 1312 1313 if (!(as = async_getcompleted(ps))) 1314 return -EAGAIN; 1315 return processcompl(as, (void __user * __user *)arg); 1316 } 1317 1318 #ifdef CONFIG_COMPAT 1319 1320 static int get_urb32(struct usbdevfs_urb *kurb, 1321 struct usbdevfs_urb32 __user *uurb) 1322 { 1323 __u32 uptr; 1324 if (!access_ok(VERIFY_READ, uurb, sizeof(*uurb)) || 1325 __get_user(kurb->type, &uurb->type) || 1326 __get_user(kurb->endpoint, &uurb->endpoint) || 1327 __get_user(kurb->status, &uurb->status) || 1328 __get_user(kurb->flags, &uurb->flags) || 1329 __get_user(kurb->buffer_length, &uurb->buffer_length) || 1330 __get_user(kurb->actual_length, &uurb->actual_length) || 1331 __get_user(kurb->start_frame, &uurb->start_frame) || 1332 __get_user(kurb->number_of_packets, &uurb->number_of_packets) || 1333 __get_user(kurb->error_count, &uurb->error_count) || 1334 __get_user(kurb->signr, &uurb->signr)) 1335 return -EFAULT; 1336 1337 if (__get_user(uptr, &uurb->buffer)) 1338 return -EFAULT; 1339 kurb->buffer = compat_ptr(uptr); 1340 if (__get_user(uptr, &uurb->usercontext)) 1341 return -EFAULT; 1342 kurb->usercontext = compat_ptr(uptr); 1343 1344 return 0; 1345 } 1346 1347 static int proc_submiturb_compat(struct dev_state *ps, void __user *arg) 1348 { 1349 struct usbdevfs_urb uurb; 1350 1351 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg)) 1352 return -EFAULT; 1353 1354 return proc_do_submiturb(ps, &uurb, 1355 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc, 1356 arg); 1357 } 1358 1359 static int processcompl_compat(struct async *as, void __user * __user *arg) 1360 { 1361 struct urb *urb = as->urb; 1362 struct usbdevfs_urb32 __user *userurb = as->userurb; 1363 void __user *addr = as->userurb; 1364 unsigned int i; 1365 1366 if (as->userbuffer) 1367 if (copy_to_user(as->userbuffer, urb->transfer_buffer, 1368 urb->transfer_buffer_length)) 1369 return -EFAULT; 1370 if (put_user(as->status, &userurb->status)) 1371 return -EFAULT; 1372 if (put_user(urb->actual_length, &userurb->actual_length)) 1373 return -EFAULT; 1374 if (put_user(urb->error_count, &userurb->error_count)) 1375 return -EFAULT; 1376 1377 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) { 1378 for (i = 0; i < urb->number_of_packets; i++) { 1379 if (put_user(urb->iso_frame_desc[i].actual_length, 1380 &userurb->iso_frame_desc[i].actual_length)) 1381 return -EFAULT; 1382 if (put_user(urb->iso_frame_desc[i].status, 1383 &userurb->iso_frame_desc[i].status)) 1384 return -EFAULT; 1385 } 1386 } 1387 1388 free_async(as); 1389 if (put_user(ptr_to_compat(addr), (u32 __user *)arg)) 1390 return -EFAULT; 1391 return 0; 1392 } 1393 1394 static int proc_reapurb_compat(struct dev_state *ps, void __user *arg) 1395 { 1396 struct async *as = reap_as(ps); 1397 if (as) 1398 return processcompl_compat(as, (void __user * __user *)arg); 1399 if (signal_pending(current)) 1400 return -EINTR; 1401 return -EIO; 1402 } 1403 1404 static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg) 1405 { 1406 struct async *as; 1407 1408 if (!(as = async_getcompleted(ps))) 1409 return -EAGAIN; 1410 return processcompl_compat(as, (void __user * __user *)arg); 1411 } 1412 1413 #endif 1414 1415 static int proc_disconnectsignal(struct dev_state *ps, void __user *arg) 1416 { 1417 struct usbdevfs_disconnectsignal ds; 1418 1419 if (copy_from_user(&ds, arg, sizeof(ds))) 1420 return -EFAULT; 1421 ps->discsignr = ds.signr; 1422 ps->disccontext = ds.context; 1423 return 0; 1424 } 1425 1426 static int proc_claiminterface(struct dev_state *ps, void __user *arg) 1427 { 1428 unsigned int ifnum; 1429 1430 if (get_user(ifnum, (unsigned int __user *)arg)) 1431 return -EFAULT; 1432 return claimintf(ps, ifnum); 1433 } 1434 1435 static int proc_releaseinterface(struct dev_state *ps, void __user *arg) 1436 { 1437 unsigned int ifnum; 1438 int ret; 1439 1440 if (get_user(ifnum, (unsigned int __user *)arg)) 1441 return -EFAULT; 1442 if ((ret = releaseintf(ps, ifnum)) < 0) 1443 return ret; 1444 destroy_async_on_interface (ps, ifnum); 1445 return 0; 1446 } 1447 1448 static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl) 1449 { 1450 int size; 1451 void *buf = NULL; 1452 int retval = 0; 1453 struct usb_interface *intf = NULL; 1454 struct usb_driver *driver = NULL; 1455 1456 /* alloc buffer */ 1457 if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) { 1458 if ((buf = kmalloc(size, GFP_KERNEL)) == NULL) 1459 return -ENOMEM; 1460 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) { 1461 if (copy_from_user(buf, ctl->data, size)) { 1462 kfree(buf); 1463 return -EFAULT; 1464 } 1465 } else { 1466 memset(buf, 0, size); 1467 } 1468 } 1469 1470 if (!connected(ps)) { 1471 kfree(buf); 1472 return -ENODEV; 1473 } 1474 1475 if (ps->dev->state != USB_STATE_CONFIGURED) 1476 retval = -EHOSTUNREACH; 1477 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno))) 1478 retval = -EINVAL; 1479 else switch (ctl->ioctl_code) { 1480 1481 /* disconnect kernel driver from interface */ 1482 case USBDEVFS_DISCONNECT: 1483 if (intf->dev.driver) { 1484 driver = to_usb_driver(intf->dev.driver); 1485 dev_dbg(&intf->dev, "disconnect by usbfs\n"); 1486 usb_driver_release_interface(driver, intf); 1487 } else 1488 retval = -ENODATA; 1489 break; 1490 1491 /* let kernel drivers try to (re)bind to the interface */ 1492 case USBDEVFS_CONNECT: 1493 if (!intf->dev.driver) 1494 retval = device_attach(&intf->dev); 1495 else 1496 retval = -EBUSY; 1497 break; 1498 1499 /* talk directly to the interface's driver */ 1500 default: 1501 if (intf->dev.driver) 1502 driver = to_usb_driver(intf->dev.driver); 1503 if (driver == NULL || driver->ioctl == NULL) { 1504 retval = -ENOTTY; 1505 } else { 1506 retval = driver->ioctl(intf, ctl->ioctl_code, buf); 1507 if (retval == -ENOIOCTLCMD) 1508 retval = -ENOTTY; 1509 } 1510 } 1511 1512 /* cleanup and return */ 1513 if (retval >= 0 1514 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0 1515 && size > 0 1516 && copy_to_user(ctl->data, buf, size) != 0) 1517 retval = -EFAULT; 1518 1519 kfree(buf); 1520 return retval; 1521 } 1522 1523 static int proc_ioctl_default(struct dev_state *ps, void __user *arg) 1524 { 1525 struct usbdevfs_ioctl ctrl; 1526 1527 if (copy_from_user(&ctrl, arg, sizeof(ctrl))) 1528 return -EFAULT; 1529 return proc_ioctl(ps, &ctrl); 1530 } 1531 1532 #ifdef CONFIG_COMPAT 1533 static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg) 1534 { 1535 struct usbdevfs_ioctl32 __user *uioc; 1536 struct usbdevfs_ioctl ctrl; 1537 u32 udata; 1538 1539 uioc = compat_ptr((long)arg); 1540 if (!access_ok(VERIFY_READ, uioc, sizeof(*uioc)) || 1541 __get_user(ctrl.ifno, &uioc->ifno) || 1542 __get_user(ctrl.ioctl_code, &uioc->ioctl_code) || 1543 __get_user(udata, &uioc->data)) 1544 return -EFAULT; 1545 ctrl.data = compat_ptr(udata); 1546 1547 return proc_ioctl(ps, &ctrl); 1548 } 1549 #endif 1550 1551 /* 1552 * NOTE: All requests here that have interface numbers as parameters 1553 * are assuming that somehow the configuration has been prevented from 1554 * changing. But there's no mechanism to ensure that... 1555 */ 1556 static int usbdev_ioctl(struct inode *inode, struct file *file, 1557 unsigned int cmd, unsigned long arg) 1558 { 1559 struct dev_state *ps = file->private_data; 1560 struct usb_device *dev = ps->dev; 1561 void __user *p = (void __user *)arg; 1562 int ret = -ENOTTY; 1563 1564 if (!(file->f_mode & FMODE_WRITE)) 1565 return -EPERM; 1566 usb_lock_device(dev); 1567 if (!connected(ps)) { 1568 usb_unlock_device(dev); 1569 return -ENODEV; 1570 } 1571 1572 switch (cmd) { 1573 case USBDEVFS_CONTROL: 1574 snoop(&dev->dev, "%s: CONTROL\n", __func__); 1575 ret = proc_control(ps, p); 1576 if (ret >= 0) 1577 inode->i_mtime = CURRENT_TIME; 1578 break; 1579 1580 case USBDEVFS_BULK: 1581 snoop(&dev->dev, "%s: BULK\n", __func__); 1582 ret = proc_bulk(ps, p); 1583 if (ret >= 0) 1584 inode->i_mtime = CURRENT_TIME; 1585 break; 1586 1587 case USBDEVFS_RESETEP: 1588 snoop(&dev->dev, "%s: RESETEP\n", __func__); 1589 ret = proc_resetep(ps, p); 1590 if (ret >= 0) 1591 inode->i_mtime = CURRENT_TIME; 1592 break; 1593 1594 case USBDEVFS_RESET: 1595 snoop(&dev->dev, "%s: RESET\n", __func__); 1596 ret = proc_resetdevice(ps); 1597 break; 1598 1599 case USBDEVFS_CLEAR_HALT: 1600 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__); 1601 ret = proc_clearhalt(ps, p); 1602 if (ret >= 0) 1603 inode->i_mtime = CURRENT_TIME; 1604 break; 1605 1606 case USBDEVFS_GETDRIVER: 1607 snoop(&dev->dev, "%s: GETDRIVER\n", __func__); 1608 ret = proc_getdriver(ps, p); 1609 break; 1610 1611 case USBDEVFS_CONNECTINFO: 1612 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__); 1613 ret = proc_connectinfo(ps, p); 1614 break; 1615 1616 case USBDEVFS_SETINTERFACE: 1617 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__); 1618 ret = proc_setintf(ps, p); 1619 break; 1620 1621 case USBDEVFS_SETCONFIGURATION: 1622 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__); 1623 ret = proc_setconfig(ps, p); 1624 break; 1625 1626 case USBDEVFS_SUBMITURB: 1627 snoop(&dev->dev, "%s: SUBMITURB\n", __func__); 1628 ret = proc_submiturb(ps, p); 1629 if (ret >= 0) 1630 inode->i_mtime = CURRENT_TIME; 1631 break; 1632 1633 #ifdef CONFIG_COMPAT 1634 1635 case USBDEVFS_SUBMITURB32: 1636 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__); 1637 ret = proc_submiturb_compat(ps, p); 1638 if (ret >= 0) 1639 inode->i_mtime = CURRENT_TIME; 1640 break; 1641 1642 case USBDEVFS_REAPURB32: 1643 snoop(&dev->dev, "%s: REAPURB32\n", __func__); 1644 ret = proc_reapurb_compat(ps, p); 1645 break; 1646 1647 case USBDEVFS_REAPURBNDELAY32: 1648 snoop(&dev->dev, "%s: REAPURBDELAY32\n", __func__); 1649 ret = proc_reapurbnonblock_compat(ps, p); 1650 break; 1651 1652 case USBDEVFS_IOCTL32: 1653 snoop(&dev->dev, "%s: IOCTL\n", __func__); 1654 ret = proc_ioctl_compat(ps, ptr_to_compat(p)); 1655 break; 1656 #endif 1657 1658 case USBDEVFS_DISCARDURB: 1659 snoop(&dev->dev, "%s: DISCARDURB\n", __func__); 1660 ret = proc_unlinkurb(ps, p); 1661 break; 1662 1663 case USBDEVFS_REAPURB: 1664 snoop(&dev->dev, "%s: REAPURB\n", __func__); 1665 ret = proc_reapurb(ps, p); 1666 break; 1667 1668 case USBDEVFS_REAPURBNDELAY: 1669 snoop(&dev->dev, "%s: REAPURBDELAY\n", __func__); 1670 ret = proc_reapurbnonblock(ps, p); 1671 break; 1672 1673 case USBDEVFS_DISCSIGNAL: 1674 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__); 1675 ret = proc_disconnectsignal(ps, p); 1676 break; 1677 1678 case USBDEVFS_CLAIMINTERFACE: 1679 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__); 1680 ret = proc_claiminterface(ps, p); 1681 break; 1682 1683 case USBDEVFS_RELEASEINTERFACE: 1684 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__); 1685 ret = proc_releaseinterface(ps, p); 1686 break; 1687 1688 case USBDEVFS_IOCTL: 1689 snoop(&dev->dev, "%s: IOCTL\n", __func__); 1690 ret = proc_ioctl_default(ps, p); 1691 break; 1692 } 1693 usb_unlock_device(dev); 1694 if (ret >= 0) 1695 inode->i_atime = CURRENT_TIME; 1696 return ret; 1697 } 1698 1699 /* No kernel lock - fine */ 1700 static unsigned int usbdev_poll(struct file *file, 1701 struct poll_table_struct *wait) 1702 { 1703 struct dev_state *ps = file->private_data; 1704 unsigned int mask = 0; 1705 1706 poll_wait(file, &ps->wait, wait); 1707 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed)) 1708 mask |= POLLOUT | POLLWRNORM; 1709 if (!connected(ps)) 1710 mask |= POLLERR | POLLHUP; 1711 return mask; 1712 } 1713 1714 const struct file_operations usbdev_file_operations = { 1715 .owner = THIS_MODULE, 1716 .llseek = usbdev_lseek, 1717 .read = usbdev_read, 1718 .poll = usbdev_poll, 1719 .ioctl = usbdev_ioctl, 1720 .open = usbdev_open, 1721 .release = usbdev_release, 1722 }; 1723 1724 static void usbdev_remove(struct usb_device *udev) 1725 { 1726 struct dev_state *ps; 1727 struct siginfo sinfo; 1728 1729 while (!list_empty(&udev->filelist)) { 1730 ps = list_entry(udev->filelist.next, struct dev_state, list); 1731 destroy_all_async(ps); 1732 wake_up_all(&ps->wait); 1733 list_del_init(&ps->list); 1734 if (ps->discsignr) { 1735 sinfo.si_signo = ps->discsignr; 1736 sinfo.si_errno = EPIPE; 1737 sinfo.si_code = SI_ASYNCIO; 1738 sinfo.si_addr = ps->disccontext; 1739 kill_pid_info_as_uid(ps->discsignr, &sinfo, 1740 ps->disc_pid, ps->disc_uid, 1741 ps->disc_euid, ps->secid); 1742 } 1743 } 1744 } 1745 1746 #ifdef CONFIG_USB_DEVICE_CLASS 1747 static struct class *usb_classdev_class; 1748 1749 static int usb_classdev_add(struct usb_device *dev) 1750 { 1751 struct device *cldev; 1752 1753 cldev = device_create(usb_classdev_class, &dev->dev, dev->dev.devt, 1754 NULL, "usbdev%d.%d", dev->bus->busnum, 1755 dev->devnum); 1756 if (IS_ERR(cldev)) 1757 return PTR_ERR(cldev); 1758 dev->usb_classdev = cldev; 1759 return 0; 1760 } 1761 1762 static void usb_classdev_remove(struct usb_device *dev) 1763 { 1764 if (dev->usb_classdev) 1765 device_unregister(dev->usb_classdev); 1766 } 1767 1768 #else 1769 #define usb_classdev_add(dev) 0 1770 #define usb_classdev_remove(dev) do {} while (0) 1771 1772 #endif 1773 1774 static int usbdev_notify(struct notifier_block *self, 1775 unsigned long action, void *dev) 1776 { 1777 switch (action) { 1778 case USB_DEVICE_ADD: 1779 if (usb_classdev_add(dev)) 1780 return NOTIFY_BAD; 1781 break; 1782 case USB_DEVICE_REMOVE: 1783 usb_classdev_remove(dev); 1784 usbdev_remove(dev); 1785 break; 1786 } 1787 return NOTIFY_OK; 1788 } 1789 1790 static struct notifier_block usbdev_nb = { 1791 .notifier_call = usbdev_notify, 1792 }; 1793 1794 static struct cdev usb_device_cdev; 1795 1796 int __init usb_devio_init(void) 1797 { 1798 int retval; 1799 1800 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX, 1801 "usb_device"); 1802 if (retval) { 1803 printk(KERN_ERR "Unable to register minors for usb_device\n"); 1804 goto out; 1805 } 1806 cdev_init(&usb_device_cdev, &usbdev_file_operations); 1807 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX); 1808 if (retval) { 1809 printk(KERN_ERR "Unable to get usb_device major %d\n", 1810 USB_DEVICE_MAJOR); 1811 goto error_cdev; 1812 } 1813 #ifdef CONFIG_USB_DEVICE_CLASS 1814 usb_classdev_class = class_create(THIS_MODULE, "usb_device"); 1815 if (IS_ERR(usb_classdev_class)) { 1816 printk(KERN_ERR "Unable to register usb_device class\n"); 1817 retval = PTR_ERR(usb_classdev_class); 1818 cdev_del(&usb_device_cdev); 1819 usb_classdev_class = NULL; 1820 goto out; 1821 } 1822 /* devices of this class shadow the major:minor of their parent 1823 * device, so clear ->dev_kobj to prevent adding duplicate entries 1824 * to /sys/dev 1825 */ 1826 usb_classdev_class->dev_kobj = NULL; 1827 #endif 1828 usb_register_notify(&usbdev_nb); 1829 out: 1830 return retval; 1831 1832 error_cdev: 1833 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX); 1834 goto out; 1835 } 1836 1837 void usb_devio_cleanup(void) 1838 { 1839 usb_unregister_notify(&usbdev_nb); 1840 #ifdef CONFIG_USB_DEVICE_CLASS 1841 class_destroy(usb_classdev_class); 1842 #endif 1843 cdev_del(&usb_device_cdev); 1844 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX); 1845 } 1846