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