usbfs: don't store bad pointers in registration
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / core / devio.c
blob5580c6e59baefff99fc8553476e5b17f27f6b399
1 /*****************************************************************************/
3 /*
4 * devio.c -- User space communication with USB devices.
6 * Copyright (C) 1999-2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
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.
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.
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.
22 * This file implements the usbfs/x/y files, where
23 * x is the bus number and y the device number.
25 * It allows user space programs/"drivers" to communicate directly
26 * with USB devices without intervening kernel driver.
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)
35 /*****************************************************************************/
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>
53 #include "hcd.h" /* for usbcore internals */
54 #include "usb.h"
56 #define USB_MAXBUS 64
57 #define USB_DEVICE_MAX USB_MAXBUS * 128
59 /* Mutual exclusion for removal, open, and release */
60 DEFINE_MUTEX(usbfs_mutex);
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;
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;
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");
96 #define snoop(dev, format, arg...) \
97 do { \
98 if (usbfs_snoop) \
99 dev_info(dev , format , ## arg); \
100 } while (0)
102 #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
105 #define MAX_USBFS_BUFFER_SIZE 16384
107 static inline int connected(struct dev_state *ps)
109 return (!list_empty(&ps->list) &&
110 ps->dev->state != USB_STATE_NOTATTACHED);
113 static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
115 loff_t ret;
117 lock_kernel();
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;
133 unlock_kernel();
134 return ret;
137 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
138 loff_t *ppos)
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;
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;
157 if (pos < sizeof(struct usb_device_descriptor)) {
158 /* 18 bytes - fits on the stack */
159 struct usb_device_descriptor temp_desc;
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);
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;
175 *ppos += len;
176 buf += len;
177 nbytes -= len;
178 ret += len;
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);
187 if (*ppos < pos + length) {
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);
194 len = length - (*ppos - pos);
195 if (len > nbytes)
196 len = nbytes;
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;
209 *ppos += len;
210 buf += len;
211 nbytes -= len;
212 ret += len;
215 pos += length;
218 err:
219 usb_unlock_device(dev);
220 return ret;
224 * async list handling
227 static struct async *alloc_async(unsigned int numisoframes)
229 struct async *as;
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;
239 return as;
242 static void free_async(struct async *as)
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);
251 static inline void async_newpending(struct async *as)
253 struct dev_state *ps = as->ps;
254 unsigned long flags;
256 spin_lock_irqsave(&ps->lock, flags);
257 list_add_tail(&as->asynclist, &ps->async_pending);
258 spin_unlock_irqrestore(&ps->lock, flags);
261 static inline void async_removepending(struct async *as)
263 struct dev_state *ps = as->ps;
264 unsigned long flags;
266 spin_lock_irqsave(&ps->lock, flags);
267 list_del_init(&as->asynclist);
268 spin_unlock_irqrestore(&ps->lock, flags);
271 static inline struct async *async_getcompleted(struct dev_state *ps)
273 unsigned long flags;
274 struct async *as = NULL;
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);
282 spin_unlock_irqrestore(&ps->lock, flags);
283 return as;
286 static inline struct async *async_getpending(struct dev_state *ps,
287 void __user *userurb)
289 unsigned long flags;
290 struct async *as;
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;
299 spin_unlock_irqrestore(&ps->lock, flags);
300 return NULL;
303 static void snoop_urb(struct urb *urb, void __user *userurb)
305 int j;
306 unsigned char *data = urb->transfer_buffer;
308 if (!usbfs_snoop)
309 return;
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");
323 static void async_completed(struct urb *urb)
325 struct async *as = urb->context;
326 struct dev_state *ps = as->ps;
327 struct siginfo sinfo;
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);
341 snoop(&urb->dev->dev, "urb complete\n");
342 snoop_urb(urb, as->userurb);
343 wake_up(&ps->wait);
346 static void destroy_async(struct dev_state *ps, struct list_head *list)
348 struct async *as;
349 unsigned long flags;
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);
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);
361 spin_unlock_irqrestore(&ps->lock, flags);
362 as = async_getcompleted(ps);
363 while (as) {
364 free_async(as);
365 as = async_getcompleted(ps);
369 static void destroy_async_on_interface(struct dev_state *ps,
370 unsigned int ifnum)
372 struct list_head *p, *q, hitlist;
373 unsigned long flags;
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);
384 static inline void destroy_all_async(struct dev_state *ps)
386 destroy_async(ps, &ps->async_pending);
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.
395 static int driver_probe(struct usb_interface *intf,
396 const struct usb_device_id *id)
398 return -ENODEV;
401 static void driver_disconnect(struct usb_interface *intf)
403 struct dev_state *ps = usb_get_intfdata(intf);
404 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
406 if (!ps)
407 return;
409 /* NOTE: this relies on usbcore having canceled and completed
410 * all pending I/O requests; 2.6 does that.
413 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
414 clear_bit(ifnum, &ps->ifclaimed);
415 else
416 warn("interface number %u out of range", ifnum);
418 usb_set_intfdata(intf, NULL);
420 /* force async requests to complete */
421 destroy_async_on_interface(ps, ifnum);
424 /* The following routines are merely placeholders. There is no way
425 * to inform a user task about suspend or resumes.
427 static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
429 return 0;
432 static int driver_resume(struct usb_interface *intf)
434 return 0;
437 struct usb_driver usbfs_driver = {
438 .name = "usbfs",
439 .probe = driver_probe,
440 .disconnect = driver_disconnect,
441 .suspend = driver_suspend,
442 .resume = driver_resume,
445 static int claimintf(struct dev_state *ps, unsigned int ifnum)
447 struct usb_device *dev = ps->dev;
448 struct usb_interface *intf;
449 int err;
451 if (ifnum >= 8*sizeof(ps->ifclaimed))
452 return -EINVAL;
453 /* already claimed */
454 if (test_bit(ifnum, &ps->ifclaimed))
455 return 0;
457 intf = usb_ifnum_to_if(dev, ifnum);
458 if (!intf)
459 err = -ENOENT;
460 else
461 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
462 if (err == 0)
463 set_bit(ifnum, &ps->ifclaimed);
464 return err;
467 static int releaseintf(struct dev_state *ps, unsigned int ifnum)
469 struct usb_device *dev;
470 struct usb_interface *intf;
471 int err;
473 err = -EINVAL;
474 if (ifnum >= 8*sizeof(ps->ifclaimed))
475 return err;
476 dev = ps->dev;
477 intf = usb_ifnum_to_if(dev, ifnum);
478 if (!intf)
479 err = -ENOENT;
480 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
481 usb_driver_release_interface(&usbfs_driver, intf);
482 err = 0;
484 return err;
487 static int checkintf(struct dev_state *ps, unsigned int ifnum)
489 if (ps->dev->state != USB_STATE_CONFIGURED)
490 return -EHOSTUNREACH;
491 if (ifnum >= 8*sizeof(ps->ifclaimed))
492 return -EINVAL;
493 if (test_bit(ifnum, &ps->ifclaimed))
494 return 0;
495 /* if not yet claimed, claim it for the driver */
496 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
497 "interface %u before use\n", task_pid_nr(current),
498 current->comm, ifnum);
499 return claimintf(ps, ifnum);
502 static int findintfep(struct usb_device *dev, unsigned int ep)
504 unsigned int i, j, e;
505 struct usb_interface *intf;
506 struct usb_host_interface *alts;
507 struct usb_endpoint_descriptor *endpt;
509 if (ep & ~(USB_DIR_IN|0xf))
510 return -EINVAL;
511 if (!dev->actconfig)
512 return -ESRCH;
513 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
514 intf = dev->actconfig->interface[i];
515 for (j = 0; j < intf->num_altsetting; j++) {
516 alts = &intf->altsetting[j];
517 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
518 endpt = &alts->endpoint[e].desc;
519 if (endpt->bEndpointAddress == ep)
520 return alts->desc.bInterfaceNumber;
524 return -ENOENT;
527 static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
528 unsigned int index)
530 int ret = 0;
532 if (ps->dev->state != USB_STATE_ADDRESS
533 && ps->dev->state != USB_STATE_CONFIGURED)
534 return -EHOSTUNREACH;
535 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
536 return 0;
538 index &= 0xff;
539 switch (requesttype & USB_RECIP_MASK) {
540 case USB_RECIP_ENDPOINT:
541 ret = findintfep(ps->dev, index);
542 if (ret >= 0)
543 ret = checkintf(ps, ret);
544 break;
546 case USB_RECIP_INTERFACE:
547 ret = checkintf(ps, index);
548 break;
550 return ret;
553 static int match_devt(struct device *dev, void *data)
555 return (dev->devt == (dev_t) data);
558 static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
560 struct device *dev;
562 dev = bus_find_device(&usb_bus_type, NULL, (void *) devt, match_devt);
563 if (!dev)
564 return NULL;
565 return container_of(dev, struct usb_device, dev);
569 * file operations
571 static int usbdev_open(struct inode *inode, struct file *file)
573 struct usb_device *dev = NULL;
574 struct dev_state *ps;
575 int ret;
577 lock_kernel();
578 /* Protect against simultaneous removal or release */
579 mutex_lock(&usbfs_mutex);
581 ret = -ENOMEM;
582 ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
583 if (!ps)
584 goto out;
586 ret = -ENOENT;
588 /* usbdev device-node */
589 if (imajor(inode) == USB_DEVICE_MAJOR)
590 dev = usbdev_lookup_by_devt(inode->i_rdev);
591 #ifdef CONFIG_USB_DEVICEFS
592 /* procfs file */
593 if (!dev) {
594 dev = inode->i_private;
595 if (dev && dev->usbfs_dentry &&
596 dev->usbfs_dentry->d_inode == inode)
597 usb_get_dev(dev);
598 else
599 dev = NULL;
601 #endif
602 if (!dev || dev->state == USB_STATE_NOTATTACHED)
603 goto out;
604 ret = usb_autoresume_device(dev);
605 if (ret)
606 goto out;
608 ret = 0;
609 ps->dev = dev;
610 ps->file = file;
611 spin_lock_init(&ps->lock);
612 INIT_LIST_HEAD(&ps->list);
613 INIT_LIST_HEAD(&ps->async_pending);
614 INIT_LIST_HEAD(&ps->async_completed);
615 init_waitqueue_head(&ps->wait);
616 ps->discsignr = 0;
617 ps->disc_pid = get_pid(task_pid(current));
618 ps->disc_uid = current->uid;
619 ps->disc_euid = current->euid;
620 ps->disccontext = NULL;
621 ps->ifclaimed = 0;
622 security_task_getsecid(current, &ps->secid);
623 smp_wmb();
624 list_add_tail(&ps->list, &dev->filelist);
625 file->private_data = ps;
626 out:
627 if (ret) {
628 kfree(ps);
629 usb_put_dev(dev);
631 mutex_unlock(&usbfs_mutex);
632 unlock_kernel();
633 return ret;
636 static int usbdev_release(struct inode *inode, struct file *file)
638 struct dev_state *ps = file->private_data;
639 struct usb_device *dev = ps->dev;
640 unsigned int ifnum;
642 usb_lock_device(dev);
644 /* Protect against simultaneous open */
645 mutex_lock(&usbfs_mutex);
646 list_del_init(&ps->list);
647 mutex_unlock(&usbfs_mutex);
649 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
650 ifnum++) {
651 if (test_bit(ifnum, &ps->ifclaimed))
652 releaseintf(ps, ifnum);
654 destroy_all_async(ps);
655 usb_autosuspend_device(dev);
656 usb_unlock_device(dev);
657 usb_put_dev(dev);
658 put_pid(ps->disc_pid);
659 kfree(ps);
660 return 0;
663 static int proc_control(struct dev_state *ps, void __user *arg)
665 struct usb_device *dev = ps->dev;
666 struct usbdevfs_ctrltransfer ctrl;
667 unsigned int tmo;
668 unsigned char *tbuf;
669 unsigned wLength;
670 int i, j, ret;
672 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
673 return -EFAULT;
674 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex);
675 if (ret)
676 return ret;
677 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
678 if (wLength > PAGE_SIZE)
679 return -EINVAL;
680 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
681 if (!tbuf)
682 return -ENOMEM;
683 tmo = ctrl.timeout;
684 if (ctrl.bRequestType & 0x80) {
685 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
686 ctrl.wLength)) {
687 free_page((unsigned long)tbuf);
688 return -EINVAL;
690 snoop(&dev->dev, "control read: bRequest=%02x "
691 "bRrequestType=%02x wValue=%04x "
692 "wIndex=%04x wLength=%04x\n",
693 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
694 ctrl.wIndex, ctrl.wLength);
696 usb_unlock_device(dev);
697 i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest,
698 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
699 tbuf, ctrl.wLength, tmo);
700 usb_lock_device(dev);
701 if ((i > 0) && ctrl.wLength) {
702 if (usbfs_snoop) {
703 dev_info(&dev->dev, "control read: data ");
704 for (j = 0; j < i; ++j)
705 printk("%02x ", (u8)(tbuf)[j]);
706 printk("\n");
708 if (copy_to_user(ctrl.data, tbuf, i)) {
709 free_page((unsigned long)tbuf);
710 return -EFAULT;
713 } else {
714 if (ctrl.wLength) {
715 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
716 free_page((unsigned long)tbuf);
717 return -EFAULT;
720 snoop(&dev->dev, "control write: bRequest=%02x "
721 "bRrequestType=%02x wValue=%04x "
722 "wIndex=%04x wLength=%04x\n",
723 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
724 ctrl.wIndex, ctrl.wLength);
725 if (usbfs_snoop) {
726 dev_info(&dev->dev, "control write: data: ");
727 for (j = 0; j < ctrl.wLength; ++j)
728 printk("%02x ", (unsigned char)(tbuf)[j]);
729 printk("\n");
731 usb_unlock_device(dev);
732 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
733 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
734 tbuf, ctrl.wLength, tmo);
735 usb_lock_device(dev);
737 free_page((unsigned long)tbuf);
738 if (i < 0 && i != -EPIPE) {
739 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
740 "failed cmd %s rqt %u rq %u len %u ret %d\n",
741 current->comm, ctrl.bRequestType, ctrl.bRequest,
742 ctrl.wLength, i);
744 return i;
747 static int proc_bulk(struct dev_state *ps, void __user *arg)
749 struct usb_device *dev = ps->dev;
750 struct usbdevfs_bulktransfer bulk;
751 unsigned int tmo, len1, pipe;
752 int len2;
753 unsigned char *tbuf;
754 int i, j, ret;
756 if (copy_from_user(&bulk, arg, sizeof(bulk)))
757 return -EFAULT;
758 ret = findintfep(ps->dev, bulk.ep);
759 if (ret < 0)
760 return ret;
761 ret = checkintf(ps, ret);
762 if (ret)
763 return ret;
764 if (bulk.ep & USB_DIR_IN)
765 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
766 else
767 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
768 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
769 return -EINVAL;
770 len1 = bulk.len;
771 if (len1 > MAX_USBFS_BUFFER_SIZE)
772 return -EINVAL;
773 if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
774 return -ENOMEM;
775 tmo = bulk.timeout;
776 if (bulk.ep & 0x80) {
777 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
778 kfree(tbuf);
779 return -EINVAL;
781 snoop(&dev->dev, "bulk read: len=0x%02x timeout=%04d\n",
782 bulk.len, bulk.timeout);
783 usb_unlock_device(dev);
784 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
785 usb_lock_device(dev);
786 if (!i && len2) {
787 if (usbfs_snoop) {
788 dev_info(&dev->dev, "bulk read: data ");
789 for (j = 0; j < len2; ++j)
790 printk("%02x ", (u8)(tbuf)[j]);
791 printk("\n");
793 if (copy_to_user(bulk.data, tbuf, len2)) {
794 kfree(tbuf);
795 return -EFAULT;
798 } else {
799 if (len1) {
800 if (copy_from_user(tbuf, bulk.data, len1)) {
801 kfree(tbuf);
802 return -EFAULT;
805 snoop(&dev->dev, "bulk write: len=0x%02x timeout=%04d\n",
806 bulk.len, bulk.timeout);
807 if (usbfs_snoop) {
808 dev_info(&dev->dev, "bulk write: data: ");
809 for (j = 0; j < len1; ++j)
810 printk("%02x ", (unsigned char)(tbuf)[j]);
811 printk("\n");
813 usb_unlock_device(dev);
814 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
815 usb_lock_device(dev);
817 kfree(tbuf);
818 if (i < 0)
819 return i;
820 return len2;
823 static int proc_resetep(struct dev_state *ps, void __user *arg)
825 unsigned int ep;
826 int ret;
828 if (get_user(ep, (unsigned int __user *)arg))
829 return -EFAULT;
830 ret = findintfep(ps->dev, ep);
831 if (ret < 0)
832 return ret;
833 ret = checkintf(ps, ret);
834 if (ret)
835 return ret;
836 usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0);
837 return 0;
840 static int proc_clearhalt(struct dev_state *ps, void __user *arg)
842 unsigned int ep;
843 int pipe;
844 int ret;
846 if (get_user(ep, (unsigned int __user *)arg))
847 return -EFAULT;
848 ret = findintfep(ps->dev, ep);
849 if (ret < 0)
850 return ret;
851 ret = checkintf(ps, ret);
852 if (ret)
853 return ret;
854 if (ep & USB_DIR_IN)
855 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
856 else
857 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
859 return usb_clear_halt(ps->dev, pipe);
862 static int proc_getdriver(struct dev_state *ps, void __user *arg)
864 struct usbdevfs_getdriver gd;
865 struct usb_interface *intf;
866 int ret;
868 if (copy_from_user(&gd, arg, sizeof(gd)))
869 return -EFAULT;
870 intf = usb_ifnum_to_if(ps->dev, gd.interface);
871 if (!intf || !intf->dev.driver)
872 ret = -ENODATA;
873 else {
874 strncpy(gd.driver, intf->dev.driver->name,
875 sizeof(gd.driver));
876 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
878 return ret;
881 static int proc_connectinfo(struct dev_state *ps, void __user *arg)
883 struct usbdevfs_connectinfo ci;
885 ci.devnum = ps->dev->devnum;
886 ci.slow = ps->dev->speed == USB_SPEED_LOW;
887 if (copy_to_user(arg, &ci, sizeof(ci)))
888 return -EFAULT;
889 return 0;
892 static int proc_resetdevice(struct dev_state *ps)
894 return usb_reset_device(ps->dev);
897 static int proc_setintf(struct dev_state *ps, void __user *arg)
899 struct usbdevfs_setinterface setintf;
900 int ret;
902 if (copy_from_user(&setintf, arg, sizeof(setintf)))
903 return -EFAULT;
904 if ((ret = checkintf(ps, setintf.interface)))
905 return ret;
906 return usb_set_interface(ps->dev, setintf.interface,
907 setintf.altsetting);
910 static int proc_setconfig(struct dev_state *ps, void __user *arg)
912 int u;
913 int status = 0;
914 struct usb_host_config *actconfig;
916 if (get_user(u, (int __user *)arg))
917 return -EFAULT;
919 actconfig = ps->dev->actconfig;
921 /* Don't touch the device if any interfaces are claimed.
922 * It could interfere with other drivers' operations, and if
923 * an interface is claimed by usbfs it could easily deadlock.
925 if (actconfig) {
926 int i;
928 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
929 if (usb_interface_claimed(actconfig->interface[i])) {
930 dev_warn(&ps->dev->dev,
931 "usbfs: interface %d claimed by %s "
932 "while '%s' sets config #%d\n",
933 actconfig->interface[i]
934 ->cur_altsetting
935 ->desc.bInterfaceNumber,
936 actconfig->interface[i]
937 ->dev.driver->name,
938 current->comm, u);
939 status = -EBUSY;
940 break;
945 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
946 * so avoid usb_set_configuration()'s kick to sysfs
948 if (status == 0) {
949 if (actconfig && actconfig->desc.bConfigurationValue == u)
950 status = usb_reset_configuration(ps->dev);
951 else
952 status = usb_set_configuration(ps->dev, u);
955 return status;
958 static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
959 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
960 void __user *arg)
962 struct usbdevfs_iso_packet_desc *isopkt = NULL;
963 struct usb_host_endpoint *ep;
964 struct async *as;
965 struct usb_ctrlrequest *dr = NULL;
966 unsigned int u, totlen, isofrmlen;
967 int ret, ifnum = -1;
968 int is_in;
970 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
971 USBDEVFS_URB_SHORT_NOT_OK |
972 USBDEVFS_URB_NO_FSBR |
973 USBDEVFS_URB_ZERO_PACKET |
974 USBDEVFS_URB_NO_INTERRUPT))
975 return -EINVAL;
976 if (!uurb->buffer)
977 return -EINVAL;
978 if (uurb->signr != 0 && (uurb->signr < SIGRTMIN ||
979 uurb->signr > SIGRTMAX))
980 return -EINVAL;
981 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
982 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
983 ifnum = findintfep(ps->dev, uurb->endpoint);
984 if (ifnum < 0)
985 return ifnum;
986 ret = checkintf(ps, ifnum);
987 if (ret)
988 return ret;
990 if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
991 is_in = 1;
992 ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
993 } else {
994 is_in = 0;
995 ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
997 if (!ep)
998 return -ENOENT;
999 switch(uurb->type) {
1000 case USBDEVFS_URB_TYPE_CONTROL:
1001 if (!usb_endpoint_xfer_control(&ep->desc))
1002 return -EINVAL;
1003 /* min 8 byte setup packet,
1004 * max 8 byte setup plus an arbitrary data stage */
1005 if (uurb->buffer_length < 8 ||
1006 uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE))
1007 return -EINVAL;
1008 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1009 if (!dr)
1010 return -ENOMEM;
1011 if (copy_from_user(dr, uurb->buffer, 8)) {
1012 kfree(dr);
1013 return -EFAULT;
1015 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
1016 kfree(dr);
1017 return -EINVAL;
1019 ret = check_ctrlrecip(ps, dr->bRequestType,
1020 le16_to_cpup(&dr->wIndex));
1021 if (ret) {
1022 kfree(dr);
1023 return ret;
1025 uurb->number_of_packets = 0;
1026 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1027 uurb->buffer += 8;
1028 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1029 is_in = 1;
1030 uurb->endpoint |= USB_DIR_IN;
1031 } else {
1032 is_in = 0;
1033 uurb->endpoint &= ~USB_DIR_IN;
1035 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1036 uurb->buffer, uurb->buffer_length)) {
1037 kfree(dr);
1038 return -EFAULT;
1040 snoop(&ps->dev->dev, "control urb: bRequest=%02x "
1041 "bRrequestType=%02x wValue=%04x "
1042 "wIndex=%04x wLength=%04x\n",
1043 dr->bRequest, dr->bRequestType,
1044 __le16_to_cpup(&dr->wValue),
1045 __le16_to_cpup(&dr->wIndex),
1046 __le16_to_cpup(&dr->wLength));
1047 break;
1049 case USBDEVFS_URB_TYPE_BULK:
1050 switch (usb_endpoint_type(&ep->desc)) {
1051 case USB_ENDPOINT_XFER_CONTROL:
1052 case USB_ENDPOINT_XFER_ISOC:
1053 return -EINVAL;
1054 /* allow single-shot interrupt transfers, at bogus rates */
1056 uurb->number_of_packets = 0;
1057 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1058 return -EINVAL;
1059 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1060 uurb->buffer, uurb->buffer_length))
1061 return -EFAULT;
1062 snoop(&ps->dev->dev, "bulk urb\n");
1063 break;
1065 case USBDEVFS_URB_TYPE_ISO:
1066 /* arbitrary limit */
1067 if (uurb->number_of_packets < 1 ||
1068 uurb->number_of_packets > 128)
1069 return -EINVAL;
1070 if (!usb_endpoint_xfer_isoc(&ep->desc))
1071 return -EINVAL;
1072 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1073 uurb->number_of_packets;
1074 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
1075 return -ENOMEM;
1076 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
1077 kfree(isopkt);
1078 return -EFAULT;
1080 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1081 /* arbitrary limit,
1082 * sufficient for USB 2.0 high-bandwidth iso */
1083 if (isopkt[u].length > 8192) {
1084 kfree(isopkt);
1085 return -EINVAL;
1087 totlen += isopkt[u].length;
1089 if (totlen > 32768) {
1090 kfree(isopkt);
1091 return -EINVAL;
1093 uurb->buffer_length = totlen;
1094 snoop(&ps->dev->dev, "iso urb\n");
1095 break;
1097 case USBDEVFS_URB_TYPE_INTERRUPT:
1098 uurb->number_of_packets = 0;
1099 if (!usb_endpoint_xfer_int(&ep->desc))
1100 return -EINVAL;
1101 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1102 return -EINVAL;
1103 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1104 uurb->buffer, uurb->buffer_length))
1105 return -EFAULT;
1106 snoop(&ps->dev->dev, "interrupt urb\n");
1107 break;
1109 default:
1110 return -EINVAL;
1112 as = alloc_async(uurb->number_of_packets);
1113 if (!as) {
1114 kfree(isopkt);
1115 kfree(dr);
1116 return -ENOMEM;
1118 as->urb->transfer_buffer = kmalloc(uurb->buffer_length, GFP_KERNEL);
1119 if (!as->urb->transfer_buffer) {
1120 kfree(isopkt);
1121 kfree(dr);
1122 free_async(as);
1123 return -ENOMEM;
1125 as->urb->dev = ps->dev;
1126 as->urb->pipe = (uurb->type << 30) |
1127 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1128 (uurb->endpoint & USB_DIR_IN);
1130 /* This tedious sequence is necessary because the URB_* flags
1131 * are internal to the kernel and subject to change, whereas
1132 * the USBDEVFS_URB_* flags are a user API and must not be changed.
1134 u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
1135 if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
1136 u |= URB_ISO_ASAP;
1137 if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
1138 u |= URB_SHORT_NOT_OK;
1139 if (uurb->flags & USBDEVFS_URB_NO_FSBR)
1140 u |= URB_NO_FSBR;
1141 if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1142 u |= URB_ZERO_PACKET;
1143 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
1144 u |= URB_NO_INTERRUPT;
1145 as->urb->transfer_flags = u;
1147 as->urb->transfer_buffer_length = uurb->buffer_length;
1148 as->urb->setup_packet = (unsigned char *)dr;
1149 as->urb->start_frame = uurb->start_frame;
1150 as->urb->number_of_packets = uurb->number_of_packets;
1151 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1152 ps->dev->speed == USB_SPEED_HIGH)
1153 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
1154 else
1155 as->urb->interval = ep->desc.bInterval;
1156 as->urb->context = as;
1157 as->urb->complete = async_completed;
1158 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1159 as->urb->iso_frame_desc[u].offset = totlen;
1160 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1161 totlen += isopkt[u].length;
1163 kfree(isopkt);
1164 as->ps = ps;
1165 as->userurb = arg;
1166 if (uurb->endpoint & USB_DIR_IN)
1167 as->userbuffer = uurb->buffer;
1168 else
1169 as->userbuffer = NULL;
1170 as->signr = uurb->signr;
1171 as->ifnum = ifnum;
1172 as->pid = get_pid(task_pid(current));
1173 as->uid = current->uid;
1174 as->euid = current->euid;
1175 security_task_getsecid(current, &as->secid);
1176 if (!is_in) {
1177 if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
1178 as->urb->transfer_buffer_length)) {
1179 free_async(as);
1180 return -EFAULT;
1183 snoop_urb(as->urb, as->userurb);
1184 async_newpending(as);
1185 if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
1186 dev_printk(KERN_DEBUG, &ps->dev->dev,
1187 "usbfs: usb_submit_urb returned %d\n", ret);
1188 async_removepending(as);
1189 free_async(as);
1190 return ret;
1192 return 0;
1195 static int proc_submiturb(struct dev_state *ps, void __user *arg)
1197 struct usbdevfs_urb uurb;
1199 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1200 return -EFAULT;
1202 return proc_do_submiturb(ps, &uurb,
1203 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1204 arg);
1207 static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
1209 struct async *as;
1211 as = async_getpending(ps, arg);
1212 if (!as)
1213 return -EINVAL;
1214 usb_kill_urb(as->urb);
1215 return 0;
1218 static int processcompl(struct async *as, void __user * __user *arg)
1220 struct urb *urb = as->urb;
1221 struct usbdevfs_urb __user *userurb = as->userurb;
1222 void __user *addr = as->userurb;
1223 unsigned int i;
1225 if (as->userbuffer)
1226 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1227 urb->transfer_buffer_length))
1228 return -EFAULT;
1229 if (put_user(as->status, &userurb->status))
1230 return -EFAULT;
1231 if (put_user(urb->actual_length, &userurb->actual_length))
1232 return -EFAULT;
1233 if (put_user(urb->error_count, &userurb->error_count))
1234 return -EFAULT;
1236 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1237 for (i = 0; i < urb->number_of_packets; i++) {
1238 if (put_user(urb->iso_frame_desc[i].actual_length,
1239 &userurb->iso_frame_desc[i].actual_length))
1240 return -EFAULT;
1241 if (put_user(urb->iso_frame_desc[i].status,
1242 &userurb->iso_frame_desc[i].status))
1243 return -EFAULT;
1247 free_async(as);
1249 if (put_user(addr, (void __user * __user *)arg))
1250 return -EFAULT;
1251 return 0;
1254 static struct async *reap_as(struct dev_state *ps)
1256 DECLARE_WAITQUEUE(wait, current);
1257 struct async *as = NULL;
1258 struct usb_device *dev = ps->dev;
1260 add_wait_queue(&ps->wait, &wait);
1261 for (;;) {
1262 __set_current_state(TASK_INTERRUPTIBLE);
1263 as = async_getcompleted(ps);
1264 if (as)
1265 break;
1266 if (signal_pending(current))
1267 break;
1268 usb_unlock_device(dev);
1269 schedule();
1270 usb_lock_device(dev);
1272 remove_wait_queue(&ps->wait, &wait);
1273 set_current_state(TASK_RUNNING);
1274 return as;
1277 static int proc_reapurb(struct dev_state *ps, void __user *arg)
1279 struct async *as = reap_as(ps);
1280 if (as)
1281 return processcompl(as, (void __user * __user *)arg);
1282 if (signal_pending(current))
1283 return -EINTR;
1284 return -EIO;
1287 static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
1289 struct async *as;
1291 if (!(as = async_getcompleted(ps)))
1292 return -EAGAIN;
1293 return processcompl(as, (void __user * __user *)arg);
1296 #ifdef CONFIG_COMPAT
1298 static int get_urb32(struct usbdevfs_urb *kurb,
1299 struct usbdevfs_urb32 __user *uurb)
1301 __u32 uptr;
1302 if (get_user(kurb->type, &uurb->type) ||
1303 __get_user(kurb->endpoint, &uurb->endpoint) ||
1304 __get_user(kurb->status, &uurb->status) ||
1305 __get_user(kurb->flags, &uurb->flags) ||
1306 __get_user(kurb->buffer_length, &uurb->buffer_length) ||
1307 __get_user(kurb->actual_length, &uurb->actual_length) ||
1308 __get_user(kurb->start_frame, &uurb->start_frame) ||
1309 __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
1310 __get_user(kurb->error_count, &uurb->error_count) ||
1311 __get_user(kurb->signr, &uurb->signr))
1312 return -EFAULT;
1314 if (__get_user(uptr, &uurb->buffer))
1315 return -EFAULT;
1316 kurb->buffer = compat_ptr(uptr);
1317 if (__get_user(uptr, &uurb->buffer))
1318 return -EFAULT;
1319 kurb->usercontext = compat_ptr(uptr);
1321 return 0;
1324 static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
1326 struct usbdevfs_urb uurb;
1328 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
1329 return -EFAULT;
1331 return proc_do_submiturb(ps, &uurb,
1332 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
1333 arg);
1336 static int processcompl_compat(struct async *as, void __user * __user *arg)
1338 struct urb *urb = as->urb;
1339 struct usbdevfs_urb32 __user *userurb = as->userurb;
1340 void __user *addr = as->userurb;
1341 unsigned int i;
1343 if (as->userbuffer)
1344 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1345 urb->transfer_buffer_length))
1346 return -EFAULT;
1347 if (put_user(as->status, &userurb->status))
1348 return -EFAULT;
1349 if (put_user(urb->actual_length, &userurb->actual_length))
1350 return -EFAULT;
1351 if (put_user(urb->error_count, &userurb->error_count))
1352 return -EFAULT;
1354 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1355 for (i = 0; i < urb->number_of_packets; i++) {
1356 if (put_user(urb->iso_frame_desc[i].actual_length,
1357 &userurb->iso_frame_desc[i].actual_length))
1358 return -EFAULT;
1359 if (put_user(urb->iso_frame_desc[i].status,
1360 &userurb->iso_frame_desc[i].status))
1361 return -EFAULT;
1365 free_async(as);
1366 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
1367 return -EFAULT;
1368 return 0;
1371 static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
1373 struct async *as = reap_as(ps);
1374 if (as)
1375 return processcompl_compat(as, (void __user * __user *)arg);
1376 if (signal_pending(current))
1377 return -EINTR;
1378 return -EIO;
1381 static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
1383 struct async *as;
1385 if (!(as = async_getcompleted(ps)))
1386 return -EAGAIN;
1387 return processcompl_compat(as, (void __user * __user *)arg);
1390 #endif
1392 static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
1394 struct usbdevfs_disconnectsignal ds;
1396 if (copy_from_user(&ds, arg, sizeof(ds)))
1397 return -EFAULT;
1398 if (ds.signr != 0 && (ds.signr < SIGRTMIN || ds.signr > SIGRTMAX))
1399 return -EINVAL;
1400 ps->discsignr = ds.signr;
1401 ps->disccontext = ds.context;
1402 return 0;
1405 static int proc_claiminterface(struct dev_state *ps, void __user *arg)
1407 unsigned int ifnum;
1409 if (get_user(ifnum, (unsigned int __user *)arg))
1410 return -EFAULT;
1411 return claimintf(ps, ifnum);
1414 static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
1416 unsigned int ifnum;
1417 int ret;
1419 if (get_user(ifnum, (unsigned int __user *)arg))
1420 return -EFAULT;
1421 if ((ret = releaseintf(ps, ifnum)) < 0)
1422 return ret;
1423 destroy_async_on_interface (ps, ifnum);
1424 return 0;
1427 static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
1429 int size;
1430 void *buf = NULL;
1431 int retval = 0;
1432 struct usb_interface *intf = NULL;
1433 struct usb_driver *driver = NULL;
1435 /* alloc buffer */
1436 if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
1437 if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
1438 return -ENOMEM;
1439 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
1440 if (copy_from_user(buf, ctl->data, size)) {
1441 kfree(buf);
1442 return -EFAULT;
1444 } else {
1445 memset(buf, 0, size);
1449 if (!connected(ps)) {
1450 kfree(buf);
1451 return -ENODEV;
1454 if (ps->dev->state != USB_STATE_CONFIGURED)
1455 retval = -EHOSTUNREACH;
1456 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
1457 retval = -EINVAL;
1458 else switch (ctl->ioctl_code) {
1460 /* disconnect kernel driver from interface */
1461 case USBDEVFS_DISCONNECT:
1462 if (intf->dev.driver) {
1463 driver = to_usb_driver(intf->dev.driver);
1464 dev_dbg(&intf->dev, "disconnect by usbfs\n");
1465 usb_driver_release_interface(driver, intf);
1466 } else
1467 retval = -ENODATA;
1468 break;
1470 /* let kernel drivers try to (re)bind to the interface */
1471 case USBDEVFS_CONNECT:
1472 if (!intf->dev.driver)
1473 retval = device_attach(&intf->dev);
1474 else
1475 retval = -EBUSY;
1476 break;
1478 /* talk directly to the interface's driver */
1479 default:
1480 if (intf->dev.driver)
1481 driver = to_usb_driver(intf->dev.driver);
1482 if (driver == NULL || driver->ioctl == NULL) {
1483 retval = -ENOTTY;
1484 } else {
1485 retval = driver->ioctl(intf, ctl->ioctl_code, buf);
1486 if (retval == -ENOIOCTLCMD)
1487 retval = -ENOTTY;
1491 /* cleanup and return */
1492 if (retval >= 0
1493 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
1494 && size > 0
1495 && copy_to_user(ctl->data, buf, size) != 0)
1496 retval = -EFAULT;
1498 kfree(buf);
1499 return retval;
1502 static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
1504 struct usbdevfs_ioctl ctrl;
1506 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1507 return -EFAULT;
1508 return proc_ioctl(ps, &ctrl);
1511 #ifdef CONFIG_COMPAT
1512 static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
1514 struct usbdevfs_ioctl32 __user *uioc;
1515 struct usbdevfs_ioctl ctrl;
1516 u32 udata;
1518 uioc = compat_ptr((long)arg);
1519 if (get_user(ctrl.ifno, &uioc->ifno) ||
1520 get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
1521 __get_user(udata, &uioc->data))
1522 return -EFAULT;
1523 ctrl.data = compat_ptr(udata);
1525 return proc_ioctl(ps, &ctrl);
1527 #endif
1530 * NOTE: All requests here that have interface numbers as parameters
1531 * are assuming that somehow the configuration has been prevented from
1532 * changing. But there's no mechanism to ensure that...
1534 static int usbdev_ioctl(struct inode *inode, struct file *file,
1535 unsigned int cmd, unsigned long arg)
1537 struct dev_state *ps = file->private_data;
1538 struct usb_device *dev = ps->dev;
1539 void __user *p = (void __user *)arg;
1540 int ret = -ENOTTY;
1542 if (!(file->f_mode & FMODE_WRITE))
1543 return -EPERM;
1544 usb_lock_device(dev);
1545 if (!connected(ps)) {
1546 usb_unlock_device(dev);
1547 return -ENODEV;
1550 switch (cmd) {
1551 case USBDEVFS_CONTROL:
1552 snoop(&dev->dev, "%s: CONTROL\n", __func__);
1553 ret = proc_control(ps, p);
1554 if (ret >= 0)
1555 inode->i_mtime = CURRENT_TIME;
1556 break;
1558 case USBDEVFS_BULK:
1559 snoop(&dev->dev, "%s: BULK\n", __func__);
1560 ret = proc_bulk(ps, p);
1561 if (ret >= 0)
1562 inode->i_mtime = CURRENT_TIME;
1563 break;
1565 case USBDEVFS_RESETEP:
1566 snoop(&dev->dev, "%s: RESETEP\n", __func__);
1567 ret = proc_resetep(ps, p);
1568 if (ret >= 0)
1569 inode->i_mtime = CURRENT_TIME;
1570 break;
1572 case USBDEVFS_RESET:
1573 snoop(&dev->dev, "%s: RESET\n", __func__);
1574 ret = proc_resetdevice(ps);
1575 break;
1577 case USBDEVFS_CLEAR_HALT:
1578 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
1579 ret = proc_clearhalt(ps, p);
1580 if (ret >= 0)
1581 inode->i_mtime = CURRENT_TIME;
1582 break;
1584 case USBDEVFS_GETDRIVER:
1585 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
1586 ret = proc_getdriver(ps, p);
1587 break;
1589 case USBDEVFS_CONNECTINFO:
1590 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
1591 ret = proc_connectinfo(ps, p);
1592 break;
1594 case USBDEVFS_SETINTERFACE:
1595 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
1596 ret = proc_setintf(ps, p);
1597 break;
1599 case USBDEVFS_SETCONFIGURATION:
1600 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
1601 ret = proc_setconfig(ps, p);
1602 break;
1604 case USBDEVFS_SUBMITURB:
1605 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
1606 ret = proc_submiturb(ps, p);
1607 if (ret >= 0)
1608 inode->i_mtime = CURRENT_TIME;
1609 break;
1611 #ifdef CONFIG_COMPAT
1613 case USBDEVFS_SUBMITURB32:
1614 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
1615 ret = proc_submiturb_compat(ps, p);
1616 if (ret >= 0)
1617 inode->i_mtime = CURRENT_TIME;
1618 break;
1620 case USBDEVFS_REAPURB32:
1621 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
1622 ret = proc_reapurb_compat(ps, p);
1623 break;
1625 case USBDEVFS_REAPURBNDELAY32:
1626 snoop(&dev->dev, "%s: REAPURBDELAY32\n", __func__);
1627 ret = proc_reapurbnonblock_compat(ps, p);
1628 break;
1630 case USBDEVFS_IOCTL32:
1631 snoop(&dev->dev, "%s: IOCTL\n", __func__);
1632 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
1633 break;
1634 #endif
1636 case USBDEVFS_DISCARDURB:
1637 snoop(&dev->dev, "%s: DISCARDURB\n", __func__);
1638 ret = proc_unlinkurb(ps, p);
1639 break;
1641 case USBDEVFS_REAPURB:
1642 snoop(&dev->dev, "%s: REAPURB\n", __func__);
1643 ret = proc_reapurb(ps, p);
1644 break;
1646 case USBDEVFS_REAPURBNDELAY:
1647 snoop(&dev->dev, "%s: REAPURBDELAY\n", __func__);
1648 ret = proc_reapurbnonblock(ps, p);
1649 break;
1651 case USBDEVFS_DISCSIGNAL:
1652 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
1653 ret = proc_disconnectsignal(ps, p);
1654 break;
1656 case USBDEVFS_CLAIMINTERFACE:
1657 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
1658 ret = proc_claiminterface(ps, p);
1659 break;
1661 case USBDEVFS_RELEASEINTERFACE:
1662 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
1663 ret = proc_releaseinterface(ps, p);
1664 break;
1666 case USBDEVFS_IOCTL:
1667 snoop(&dev->dev, "%s: IOCTL\n", __func__);
1668 ret = proc_ioctl_default(ps, p);
1669 break;
1671 usb_unlock_device(dev);
1672 if (ret >= 0)
1673 inode->i_atime = CURRENT_TIME;
1674 return ret;
1677 /* No kernel lock - fine */
1678 static unsigned int usbdev_poll(struct file *file,
1679 struct poll_table_struct *wait)
1681 struct dev_state *ps = file->private_data;
1682 unsigned int mask = 0;
1684 poll_wait(file, &ps->wait, wait);
1685 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
1686 mask |= POLLOUT | POLLWRNORM;
1687 if (!connected(ps))
1688 mask |= POLLERR | POLLHUP;
1689 return mask;
1692 const struct file_operations usbdev_file_operations = {
1693 .owner = THIS_MODULE,
1694 .llseek = usbdev_lseek,
1695 .read = usbdev_read,
1696 .poll = usbdev_poll,
1697 .ioctl = usbdev_ioctl,
1698 .open = usbdev_open,
1699 .release = usbdev_release,
1702 void usb_fs_classdev_common_remove(struct usb_device *udev)
1704 struct dev_state *ps;
1705 struct siginfo sinfo;
1707 while (!list_empty(&udev->filelist)) {
1708 ps = list_entry(udev->filelist.next, struct dev_state, list);
1709 destroy_all_async(ps);
1710 wake_up_all(&ps->wait);
1711 list_del_init(&ps->list);
1712 if (ps->discsignr) {
1713 sinfo.si_signo = ps->discsignr;
1714 sinfo.si_errno = EPIPE;
1715 sinfo.si_code = SI_ASYNCIO;
1716 sinfo.si_addr = ps->disccontext;
1717 kill_pid_info_as_uid(ps->discsignr, &sinfo,
1718 ps->disc_pid, ps->disc_uid,
1719 ps->disc_euid, ps->secid);
1724 #ifdef CONFIG_USB_DEVICE_CLASS
1725 static struct class *usb_classdev_class;
1727 static int usb_classdev_add(struct usb_device *dev)
1729 struct device *cldev;
1731 cldev = device_create(usb_classdev_class, &dev->dev, dev->dev.devt,
1732 "usbdev%d.%d", dev->bus->busnum,
1733 dev->devnum);
1734 if (IS_ERR(cldev))
1735 return PTR_ERR(cldev);
1736 dev->usb_classdev = cldev;
1737 return 0;
1740 static void usb_classdev_remove(struct usb_device *dev)
1742 if (dev->usb_classdev)
1743 device_unregister(dev->usb_classdev);
1744 usb_fs_classdev_common_remove(dev);
1747 static int usb_classdev_notify(struct notifier_block *self,
1748 unsigned long action, void *dev)
1750 switch (action) {
1751 case USB_DEVICE_ADD:
1752 if (usb_classdev_add(dev))
1753 return NOTIFY_BAD;
1754 break;
1755 case USB_DEVICE_REMOVE:
1756 usb_classdev_remove(dev);
1757 break;
1759 return NOTIFY_OK;
1762 static struct notifier_block usbdev_nb = {
1763 .notifier_call = usb_classdev_notify,
1765 #endif
1767 static struct cdev usb_device_cdev;
1769 int __init usb_devio_init(void)
1771 int retval;
1773 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
1774 "usb_device");
1775 if (retval) {
1776 err("unable to register minors for usb_device");
1777 goto out;
1779 cdev_init(&usb_device_cdev, &usbdev_file_operations);
1780 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
1781 if (retval) {
1782 err("unable to get usb_device major %d", USB_DEVICE_MAJOR);
1783 goto error_cdev;
1785 #ifdef CONFIG_USB_DEVICE_CLASS
1786 usb_classdev_class = class_create(THIS_MODULE, "usb_device");
1787 if (IS_ERR(usb_classdev_class)) {
1788 err("unable to register usb_device class");
1789 retval = PTR_ERR(usb_classdev_class);
1790 cdev_del(&usb_device_cdev);
1791 usb_classdev_class = NULL;
1792 goto out;
1795 usb_register_notify(&usbdev_nb);
1796 #endif
1797 out:
1798 return retval;
1800 error_cdev:
1801 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
1802 goto out;
1805 void usb_devio_cleanup(void)
1807 #ifdef CONFIG_USB_DEVICE_CLASS
1808 usb_unregister_notify(&usbdev_nb);
1809 class_destroy(usb_classdev_class);
1810 #endif
1811 cdev_del(&usb_device_cdev);
1812 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);