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