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