RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / usb / core / devio.c
blobfb46f98879cc16b93e10b3f72a261b180c7b948d
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 * $Id: devio.c,v 1.7 2000/02/01 17:28:48 fliegl Exp $
24 * This file implements the usbfs/x/y files, where
25 * x is the bus number and y the device number.
27 * It allows user space programs/"drivers" to communicate directly
28 * with USB devices without intervening kernel driver.
30 * Revision history
31 * 22.12.1999 0.1 Initial release (split from proc_usb.c)
32 * 04.01.2000 0.2 Turned into its own filesystem
33 * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery
34 * (CAN-2005-3055)
37 /*****************************************************************************/
39 #include <linux/fs.h>
40 #include <linux/mm.h>
41 #include <linux/slab.h>
42 #include <linux/smp_lock.h>
43 #include <linux/signal.h>
44 #include <linux/poll.h>
45 #include <linux/module.h>
46 #include <linux/usb.h>
47 #include <linux/usbdevice_fs.h>
48 #include <linux/cdev.h>
49 #include <linux/notifier.h>
50 #include <linux/security.h>
51 #include <asm/uaccess.h>
52 #include <asm/byteorder.h>
53 #include <linux/moduleparam.h>
55 #include "hcd.h" /* for usbcore internals */
56 #include "usb.h"
58 #define USB_MAXBUS 64
59 #define USB_DEVICE_MAX USB_MAXBUS * 128
61 /* Mutual exclusion for removal, open, and release */
62 DEFINE_MUTEX(usbfs_mutex);
64 struct dev_state {
65 struct list_head list; /* state list */
66 struct usb_device *dev;
67 struct file *file;
68 spinlock_t lock; /* protects the async urb lists */
69 struct list_head async_pending;
70 struct list_head async_completed;
71 wait_queue_head_t wait; /* wake up if a request completed */
72 unsigned int discsignr;
73 struct pid *disc_pid;
74 uid_t disc_uid, disc_euid;
75 void __user *disccontext;
76 unsigned long ifclaimed;
77 u32 secid;
80 struct async {
81 struct list_head asynclist;
82 struct dev_state *ps;
83 struct pid *pid;
84 uid_t uid, euid;
85 unsigned int signr;
86 unsigned int ifnum;
87 void __user *userbuffer;
88 void __user *userurb;
89 struct urb *urb;
90 int status;
91 u32 secid;
94 static int usbfs_snoop = 0;
95 module_param (usbfs_snoop, bool, S_IRUGO | S_IWUSR);
96 MODULE_PARM_DESC (usbfs_snoop, "true to log all usbfs traffic");
98 #define snoop(dev, format, arg...) \
99 do { \
100 if (usbfs_snoop) \
101 dev_info( dev , format , ## arg); \
102 } while (0)
104 #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
107 #define MAX_USBFS_BUFFER_SIZE 16384
109 static int connected(struct dev_state *ps)
111 return (!list_empty(&ps->list) &&
112 ps->dev->state != USB_STATE_NOTATTACHED);
115 static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
117 loff_t ret;
119 lock_kernel();
121 switch (orig) {
122 case 0:
123 file->f_pos = offset;
124 ret = file->f_pos;
125 break;
126 case 1:
127 file->f_pos += offset;
128 ret = file->f_pos;
129 break;
130 case 2:
131 default:
132 ret = -EINVAL;
135 unlock_kernel();
136 return ret;
139 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
141 struct dev_state *ps = file->private_data;
142 struct usb_device *dev = ps->dev;
143 ssize_t ret = 0;
144 unsigned len;
145 loff_t pos;
146 int i;
148 pos = *ppos;
149 usb_lock_device(dev);
150 if (!connected(ps)) {
151 ret = -ENODEV;
152 goto err;
153 } else if (pos < 0) {
154 ret = -EINVAL;
155 goto err;
158 if (pos < sizeof(struct usb_device_descriptor)) {
159 struct usb_device_descriptor temp_desc ; /* 18 bytes - fits on the stack */
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 unsigned int assize = sizeof(struct async) + numisoframes * sizeof(struct usb_iso_packet_descriptor);
230 struct async *as = kzalloc(assize, 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 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 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 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, asynclist);
279 list_del_init(&as->asynclist);
281 spin_unlock_irqrestore(&ps->lock, flags);
282 return as;
285 static struct async *async_getpending(struct dev_state *ps, void __user *userurb)
287 unsigned long flags;
288 struct async *as;
290 spin_lock_irqsave(&ps->lock, flags);
291 list_for_each_entry(as, &ps->async_pending, asynclist)
292 if (as->userurb == userurb) {
293 list_del_init(&as->asynclist);
294 spin_unlock_irqrestore(&ps->lock, flags);
295 return as;
297 spin_unlock_irqrestore(&ps->lock, flags);
298 return NULL;
301 static void snoop_urb(struct urb *urb, void __user *userurb)
303 unsigned j;
304 unsigned char *data = urb->transfer_buffer;
306 if (!usbfs_snoop)
307 return;
309 dev_info(&urb->dev->dev, "direction=%s\n",
310 usb_urb_dir_in(urb) ? "IN" : "OUT");
311 dev_info(&urb->dev->dev, "userurb=%p\n", userurb);
312 dev_info(&urb->dev->dev, "transfer_buffer_length=%u\n",
313 urb->transfer_buffer_length);
314 dev_info(&urb->dev->dev, "actual_length=%u\n", urb->actual_length);
315 dev_info(&urb->dev->dev, "data: ");
316 for (j = 0; j < urb->transfer_buffer_length; ++j)
317 printk ("%02x ", data[j]);
318 printk("\n");
321 static void async_completed(struct urb *urb)
323 struct async *as = urb->context;
324 struct dev_state *ps = as->ps;
325 struct siginfo sinfo;
326 struct pid *pid = NULL;
327 uid_t uid = 0;
328 uid_t euid = 0;
329 u32 secid = 0;
330 int signr;
332 spin_lock(&ps->lock);
333 list_move_tail(&as->asynclist, &ps->async_completed);
334 as->status = urb->status;
335 signr = as->signr;
336 if (signr) {
337 sinfo.si_signo = as->signr;
338 sinfo.si_errno = as->status;
339 sinfo.si_code = SI_ASYNCIO;
340 sinfo.si_addr = as->userurb;
341 pid = as->pid;
342 uid = as->uid;
343 euid = as->euid;
344 secid = as->secid;
346 snoop(&urb->dev->dev, "urb complete\n");
347 snoop_urb(urb, as->userurb);
348 spin_unlock(&ps->lock);
350 if (signr)
351 kill_pid_info_as_uid(sinfo.si_signo, &sinfo, pid, uid,
352 euid, secid);
354 wake_up(&ps->wait);
357 static void destroy_async (struct dev_state *ps, struct list_head *list)
359 struct async *as;
360 unsigned long flags;
362 spin_lock_irqsave(&ps->lock, flags);
363 while (!list_empty(list)) {
364 as = list_entry(list->next, struct async, asynclist);
365 list_del_init(&as->asynclist);
367 /* drop the spinlock so the completion handler can run */
368 spin_unlock_irqrestore(&ps->lock, flags);
369 usb_kill_urb(as->urb);
370 spin_lock_irqsave(&ps->lock, flags);
372 spin_unlock_irqrestore(&ps->lock, flags);
375 static void destroy_async_on_interface (struct dev_state *ps, unsigned int ifnum)
377 struct list_head *p, *q, hitlist;
378 unsigned long flags;
380 INIT_LIST_HEAD(&hitlist);
381 spin_lock_irqsave(&ps->lock, flags);
382 list_for_each_safe(p, q, &ps->async_pending)
383 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
384 list_move_tail(p, &hitlist);
385 spin_unlock_irqrestore(&ps->lock, flags);
386 destroy_async(ps, &hitlist);
389 static void destroy_all_async(struct dev_state *ps)
391 destroy_async(ps, &ps->async_pending);
395 * interface claims are made only at the request of user level code,
396 * which can also release them (explicitly or by closing files).
397 * they're also undone when devices disconnect.
400 static int driver_probe (struct usb_interface *intf,
401 const struct usb_device_id *id)
403 return -ENODEV;
406 static void driver_disconnect(struct usb_interface *intf)
408 struct dev_state *ps = usb_get_intfdata (intf);
409 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
411 if (!ps)
412 return;
414 /* NOTE: this relies on usbcore having canceled and completed
415 * all pending I/O requests; 2.6 does that.
418 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
419 clear_bit(ifnum, &ps->ifclaimed);
420 else
421 warn("interface number %u out of range", ifnum);
423 usb_set_intfdata (intf, NULL);
425 /* force async requests to complete */
426 destroy_async_on_interface(ps, ifnum);
429 struct usb_driver usbfs_driver = {
430 .name = "usbfs",
431 .probe = driver_probe,
432 .disconnect = driver_disconnect,
435 static int claimintf(struct dev_state *ps, unsigned int ifnum)
437 struct usb_device *dev = ps->dev;
438 struct usb_interface *intf;
439 int err;
441 if (ifnum >= 8*sizeof(ps->ifclaimed))
442 return -EINVAL;
443 /* already claimed */
444 if (test_bit(ifnum, &ps->ifclaimed))
445 return 0;
447 intf = usb_ifnum_to_if(dev, ifnum);
448 if (!intf)
449 err = -ENOENT;
450 else
451 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
452 if (err == 0)
453 set_bit(ifnum, &ps->ifclaimed);
454 else if (!strcmp(intf->dev.driver->name, "usblp")) // patch for U2EC
455 err = 0; // patch for U2EC
456 return err;
459 static int releaseintf(struct dev_state *ps, unsigned int ifnum)
461 struct usb_device *dev;
462 struct usb_interface *intf;
463 int err;
465 err = -EINVAL;
466 if (ifnum >= 8*sizeof(ps->ifclaimed))
467 return err;
468 dev = ps->dev;
469 intf = usb_ifnum_to_if(dev, ifnum);
470 if (!intf)
471 err = -ENOENT;
472 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
473 usb_driver_release_interface(&usbfs_driver, intf);
474 err = 0;
476 return err;
479 static int checkintf(struct dev_state *ps, unsigned int ifnum)
481 if (ps->dev->state != USB_STATE_CONFIGURED)
482 return -EHOSTUNREACH;
483 if (ifnum >= 8*sizeof(ps->ifclaimed))
484 return -EINVAL;
485 if (test_bit(ifnum, &ps->ifclaimed))
486 return 0;
487 /* if not yet claimed, claim it for the driver */
488 // U2EC mark this.
489 /* dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim interface %u before use\n",
490 current->pid, current->comm, ifnum);//*/
491 return claimintf(ps, ifnum);
494 static int findintfep(struct usb_device *dev, unsigned int ep)
496 unsigned int i, j, e;
497 struct usb_interface *intf;
498 struct usb_host_interface *alts;
499 struct usb_endpoint_descriptor *endpt;
501 if (ep & ~(USB_DIR_IN|0xf))
502 return -EINVAL;
503 if (!dev->actconfig)
504 return -ESRCH;
505 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
506 intf = dev->actconfig->interface[i];
507 for (j = 0; j < intf->num_altsetting; j++) {
508 alts = &intf->altsetting[j];
509 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
510 endpt = &alts->endpoint[e].desc;
511 if (endpt->bEndpointAddress == ep)
512 return alts->desc.bInterfaceNumber;
516 return -ENOENT;
519 static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype, unsigned int index)
521 int ret = 0;
523 if (ps->dev->state != USB_STATE_ADDRESS
524 && ps->dev->state != USB_STATE_CONFIGURED)
525 return -EHOSTUNREACH;
526 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
527 return 0;
529 index &= 0xff;
530 switch (requesttype & USB_RECIP_MASK) {
531 case USB_RECIP_ENDPOINT:
532 if ((ret = findintfep(ps->dev, index)) >= 0)
533 ret = checkintf(ps, ret);
534 break;
536 case USB_RECIP_INTERFACE:
537 ret = checkintf(ps, index);
538 break;
540 return ret;
543 static int match_devt(struct device *dev, void *data)
545 return dev->devt == (dev_t) (unsigned long) data;
548 static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
550 struct device *dev;
552 dev = bus_find_device(&usb_bus_type, NULL,
553 (void *) (unsigned long) devt, match_devt);
554 if (!dev)
555 return NULL;
556 return container_of(dev, struct usb_device, dev);
560 * file operations
562 static int usbdev_open(struct inode *inode, struct file *file)
564 struct usb_device *dev = NULL;
565 struct dev_state *ps;
566 int ret;
568 /* Protect against simultaneous removal or release */
569 mutex_lock(&usbfs_mutex);
571 ret = -ENOMEM;
572 if (!(ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL)))
573 goto out;
575 ret = -ENODEV;
577 /* usbdev device-node */
578 if (imajor(inode) == USB_DEVICE_MAJOR)
579 dev = usbdev_lookup_by_devt(inode->i_rdev);
580 #ifdef CONFIG_USB_DEVICEFS
581 /* procfs file */
582 if (!dev) {
583 dev = inode->i_private;
584 if (dev && dev->usbfs_dentry &&
585 dev->usbfs_dentry->d_inode == inode)
586 usb_get_dev(dev);
587 else
588 dev = NULL;
590 #endif
591 if (!dev || dev->state == USB_STATE_NOTATTACHED)
592 goto out;
593 ret = usb_autoresume_device(dev);
594 if (ret)
595 goto out;
597 ret = 0;
598 ps->dev = dev;
599 ps->file = file;
600 spin_lock_init(&ps->lock);
601 INIT_LIST_HEAD(&ps->list);
602 INIT_LIST_HEAD(&ps->async_pending);
603 INIT_LIST_HEAD(&ps->async_completed);
604 init_waitqueue_head(&ps->wait);
605 ps->discsignr = 0;
606 ps->disc_pid = get_pid(task_pid(current));
607 ps->disc_uid = current->uid;
608 ps->disc_euid = current->euid;
609 ps->disccontext = NULL;
610 ps->ifclaimed = 0;
611 security_task_getsecid(current, &ps->secid);
612 smp_wmb();
613 list_add_tail(&ps->list, &dev->filelist);
614 file->private_data = ps;
615 out:
616 if (ret) {
617 kfree(ps);
618 usb_put_dev(dev);
620 mutex_unlock(&usbfs_mutex);
621 return ret;
624 static int usbdev_release(struct inode *inode, struct file *file)
626 struct dev_state *ps = file->private_data;
627 struct usb_device *dev = ps->dev;
628 unsigned int ifnum;
629 struct async *as;
631 usb_lock_device(dev);
633 /* Protect against simultaneous open */
634 mutex_lock(&usbfs_mutex);
635 list_del_init(&ps->list);
636 mutex_unlock(&usbfs_mutex);
638 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
639 ifnum++) {
640 if (test_bit(ifnum, &ps->ifclaimed))
641 releaseintf(ps, ifnum);
643 destroy_all_async(ps);
644 usb_autosuspend_device(dev);
645 usb_unlock_device(dev);
646 usb_put_dev(dev);
647 put_pid(ps->disc_pid);
649 as = async_getcompleted(ps);
650 while (as) {
651 free_async(as);
652 as = async_getcompleted(ps);
654 kfree(ps);
655 return 0;
658 static int proc_control(struct dev_state *ps, void __user *arg)
660 struct usb_device *dev = ps->dev;
661 struct usbdevfs_ctrltransfer ctrl;
662 unsigned int tmo;
663 unsigned char *tbuf;
664 unsigned wLength;
665 int i, j, ret;
667 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
668 return -EFAULT;
669 if ((ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex)))
670 return ret;
671 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
672 if (wLength > PAGE_SIZE)
673 return -EINVAL;
674 if (!(tbuf = (unsigned char *)__get_free_page(GFP_KERNEL)))
675 return -ENOMEM;
676 tmo = ctrl.timeout;
677 if (ctrl.bRequestType & 0x80) {
678 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data, ctrl.wLength)) {
679 free_page((unsigned long)tbuf);
680 return -EINVAL;
682 snoop(&dev->dev, "control read: bRequest=%02x "
683 "bRrequestType=%02x wValue=%04x "
684 "wIndex=%04x wLength=%04x\n",
685 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
686 ctrl.wIndex, ctrl.wLength);
688 usb_unlock_device(dev);
689 i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest, ctrl.bRequestType,
690 ctrl.wValue, ctrl.wIndex, tbuf, ctrl.wLength, tmo);
691 usb_lock_device(dev);
692 if ((i > 0) && ctrl.wLength) {
693 if (usbfs_snoop) {
694 dev_info(&dev->dev, "control read: data ");
695 for (j = 0; j < i; ++j)
696 printk("%02x ", (unsigned char)(tbuf)[j]);
697 printk("\n");
699 if (copy_to_user(ctrl.data, tbuf, i)) {
700 free_page((unsigned long)tbuf);
701 return -EFAULT;
704 } else {
705 if (ctrl.wLength) {
706 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
707 free_page((unsigned long)tbuf);
708 return -EFAULT;
711 snoop(&dev->dev, "control write: bRequest=%02x "
712 "bRrequestType=%02x wValue=%04x "
713 "wIndex=%04x wLength=%04x\n",
714 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
715 ctrl.wIndex, ctrl.wLength);
716 if (usbfs_snoop) {
717 dev_info(&dev->dev, "control write: data: ");
718 for (j = 0; j < ctrl.wLength; ++j)
719 printk("%02x ", (unsigned char)(tbuf)[j]);
720 printk("\n");
722 usb_unlock_device(dev);
723 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest, ctrl.bRequestType,
724 ctrl.wValue, ctrl.wIndex, tbuf, ctrl.wLength, tmo);
725 usb_lock_device(dev);
727 free_page((unsigned long)tbuf);
728 if (i<0 && i != -EPIPE) {
729 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
730 "failed cmd %s rqt %u rq %u len %u ret %d\n",
731 current->comm, ctrl.bRequestType, ctrl.bRequest,
732 ctrl.wLength, i);
734 return i;
737 static int proc_bulk(struct dev_state *ps, void __user *arg)
739 struct usb_device *dev = ps->dev;
740 struct usbdevfs_bulktransfer bulk;
741 unsigned int tmo, len1, pipe;
742 int len2;
743 unsigned char *tbuf;
744 int i, j, ret;
746 if (copy_from_user(&bulk, arg, sizeof(bulk)))
747 return -EFAULT;
748 if ((ret = findintfep(ps->dev, bulk.ep)) < 0)
749 return ret;
750 if ((ret = checkintf(ps, ret)))
751 return ret;
752 if (bulk.ep & USB_DIR_IN)
753 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
754 else
755 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
756 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
757 return -EINVAL;
758 len1 = bulk.len;
759 if (len1 > MAX_USBFS_BUFFER_SIZE)
760 return -EINVAL;
761 if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
762 return -ENOMEM;
763 tmo = bulk.timeout;
764 if (bulk.ep & 0x80) {
765 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
766 kfree(tbuf);
767 return -EINVAL;
769 snoop(&dev->dev, "bulk read: len=0x%02x timeout=%04d\n",
770 bulk.len, bulk.timeout);
771 usb_unlock_device(dev);
772 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
773 usb_lock_device(dev);
774 if (!i && len2) {
775 if (usbfs_snoop) {
776 dev_info(&dev->dev, "bulk read: data ");
777 for (j = 0; j < len2; ++j)
778 printk("%02x ", (unsigned char)(tbuf)[j]);
779 printk("\n");
781 if (copy_to_user(bulk.data, tbuf, len2)) {
782 kfree(tbuf);
783 return -EFAULT;
786 } else {
787 if (len1) {
788 if (copy_from_user(tbuf, bulk.data, len1)) {
789 kfree(tbuf);
790 return -EFAULT;
793 snoop(&dev->dev, "bulk write: len=0x%02x timeout=%04d\n",
794 bulk.len, bulk.timeout);
795 if (usbfs_snoop) {
796 dev_info(&dev->dev, "bulk write: data: ");
797 for (j = 0; j < len1; ++j)
798 printk("%02x ", (unsigned char)(tbuf)[j]);
799 printk("\n");
801 usb_unlock_device(dev);
802 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
803 usb_lock_device(dev);
805 kfree(tbuf);
806 if (i < 0)
807 return i;
808 return len2;
811 static int proc_resetep(struct dev_state *ps, void __user *arg)
813 unsigned int ep;
814 int ret;
816 if (get_user(ep, (unsigned int __user *)arg))
817 return -EFAULT;
818 if ((ret = findintfep(ps->dev, ep)) < 0)
819 return ret;
820 if ((ret = checkintf(ps, ret)))
821 return ret;
822 usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0);
823 return 0;
826 static int proc_clearhalt(struct dev_state *ps, void __user *arg)
828 unsigned int ep;
829 int pipe;
830 int ret;
832 if (get_user(ep, (unsigned int __user *)arg))
833 return -EFAULT;
834 if ((ret = findintfep(ps->dev, ep)) < 0)
835 return ret;
836 if ((ret = checkintf(ps, ret)))
837 return ret;
838 if (ep & USB_DIR_IN)
839 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
840 else
841 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
843 return usb_clear_halt(ps->dev, pipe);
847 static int proc_getdriver(struct dev_state *ps, void __user *arg)
849 struct usbdevfs_getdriver gd;
850 struct usb_interface *intf;
851 int ret;
853 if (copy_from_user(&gd, arg, sizeof(gd)))
854 return -EFAULT;
855 intf = usb_ifnum_to_if(ps->dev, gd.interface);
856 if (!intf || !intf->dev.driver)
857 ret = -ENODATA;
858 else {
859 strncpy(gd.driver, intf->dev.driver->name,
860 sizeof(gd.driver));
861 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
863 return ret;
866 static int proc_connectinfo(struct dev_state *ps, void __user *arg)
868 struct usbdevfs_connectinfo ci = {
869 .devnum = ps->dev->devnum,
870 .slow = ps->dev->speed == USB_SPEED_LOW
873 if (copy_to_user(arg, &ci, sizeof(ci)))
874 return -EFAULT;
875 return 0;
878 static int proc_resetdevice(struct dev_state *ps)
880 return usb_reset_device(ps->dev);
883 static int proc_setintf(struct dev_state *ps, void __user *arg)
885 struct usbdevfs_setinterface setintf;
886 int ret;
888 if (copy_from_user(&setintf, arg, sizeof(setintf)))
889 return -EFAULT;
890 if ((ret = checkintf(ps, setintf.interface)))
891 return ret;
892 return usb_set_interface(ps->dev, setintf.interface,
893 setintf.altsetting);
896 static int proc_setconfig(struct dev_state *ps, void __user *arg)
898 int u;
899 int status = 0;
900 struct usb_host_config *actconfig;
902 if (get_user(u, (int __user *)arg))
903 return -EFAULT;
905 actconfig = ps->dev->actconfig;
907 /* Don't touch the device if any interfaces are claimed.
908 * It could interfere with other drivers' operations, and if
909 * an interface is claimed by usbfs it could easily deadlock.
911 if (actconfig) {
912 int i;
914 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
915 if (usb_interface_claimed(actconfig->interface[i])) {
916 dev_warn (&ps->dev->dev,
917 "usbfs: interface %d claimed by %s "
918 "while '%s' sets config #%d\n",
919 actconfig->interface[i]
920 ->cur_altsetting
921 ->desc.bInterfaceNumber,
922 actconfig->interface[i]
923 ->dev.driver->name,
924 current->comm, u);
925 status = -EBUSY;
926 break;
931 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
932 * so avoid usb_set_configuration()'s kick to sysfs
934 if (status == 0) {
935 if (actconfig && actconfig->desc.bConfigurationValue == u)
936 status = usb_reset_configuration(ps->dev);
937 else
938 status = usb_set_configuration(ps->dev, u);
941 return status;
944 static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
945 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
946 void __user *arg)
948 struct usbdevfs_iso_packet_desc *isopkt = NULL;
949 struct usb_host_endpoint *ep;
950 struct async *as;
951 struct usb_ctrlrequest *dr = NULL;
952 unsigned int u, totlen, isofrmlen;
953 int ret, ifnum = -1;
954 int is_in;
956 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP|USBDEVFS_URB_SHORT_NOT_OK|
957 URB_NO_FSBR|URB_ZERO_PACKET))
958 return -EINVAL;
959 if (uurb->buffer_length > 0 && !uurb->buffer)
960 return -EINVAL;
961 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
962 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
963 if ((ifnum = findintfep(ps->dev, uurb->endpoint)) < 0)
964 return ifnum;
965 if ((ret = checkintf(ps, ifnum)))
966 return ret;
968 if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
969 is_in = 1;
970 ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
971 } else {
972 is_in = 0;
973 ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
975 if (!ep)
976 return -ENOENT;
977 switch(uurb->type) {
978 case USBDEVFS_URB_TYPE_CONTROL:
979 if (!usb_endpoint_xfer_control(&ep->desc))
980 return -EINVAL;
981 /* min 8 byte setup packet, max 8 byte setup plus an arbitrary data stage */
982 if (uurb->buffer_length < 8 || uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE))
983 return -EINVAL;
984 if (!(dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL)))
985 return -ENOMEM;
986 if (copy_from_user(dr, uurb->buffer, 8)) {
987 kfree(dr);
988 return -EFAULT;
990 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
991 kfree(dr);
992 return -EINVAL;
994 if ((ret = check_ctrlrecip(ps, dr->bRequestType, le16_to_cpup(&dr->wIndex)))) {
995 kfree(dr);
996 return ret;
998 uurb->number_of_packets = 0;
999 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1000 uurb->buffer += 8;
1001 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1002 is_in = 1;
1003 uurb->endpoint |= USB_DIR_IN;
1004 } else {
1005 is_in = 0;
1006 uurb->endpoint &= ~USB_DIR_IN;
1008 snoop(&ps->dev->dev, "control urb: bRequest=%02x "
1009 "bRrequestType=%02x wValue=%04x "
1010 "wIndex=%04x wLength=%04x\n",
1011 dr->bRequest, dr->bRequestType,
1012 __le16_to_cpup(&dr->wValue),
1013 __le16_to_cpup(&dr->wIndex),
1014 __le16_to_cpup(&dr->wLength));
1015 break;
1017 case USBDEVFS_URB_TYPE_BULK:
1018 switch (usb_endpoint_type(&ep->desc)) {
1019 case USB_ENDPOINT_XFER_CONTROL:
1020 case USB_ENDPOINT_XFER_ISOC:
1021 return -EINVAL;
1022 /* allow single-shot interrupt transfers, at bogus rates */
1024 uurb->number_of_packets = 0;
1025 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1026 return -EINVAL;
1027 snoop(&ps->dev->dev, "bulk urb\n");
1028 break;
1030 case USBDEVFS_URB_TYPE_ISO:
1031 /* arbitrary limit */
1032 if (uurb->number_of_packets < 1 || uurb->number_of_packets > 128)
1033 return -EINVAL;
1034 if (!usb_endpoint_xfer_isoc(&ep->desc))
1035 return -EINVAL;
1036 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) * uurb->number_of_packets;
1037 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
1038 return -ENOMEM;
1039 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
1040 kfree(isopkt);
1041 return -EFAULT;
1043 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1044 /* arbitrary limit, sufficient for USB 2.0 high-bandwidth iso */
1045 if (isopkt[u].length > 8192) {
1046 kfree(isopkt);
1047 return -EINVAL;
1049 totlen += isopkt[u].length;
1051 /* 3072 * 64 microframes */
1052 if (totlen > 196608) {
1053 kfree(isopkt);
1054 return -EINVAL;
1056 uurb->buffer_length = totlen;
1057 snoop(&ps->dev->dev, "iso urb\n");
1058 break;
1060 case USBDEVFS_URB_TYPE_INTERRUPT:
1061 uurb->number_of_packets = 0;
1062 if (!usb_endpoint_xfer_int(&ep->desc))
1063 return -EINVAL;
1064 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1065 return -EINVAL;
1066 snoop(&ps->dev->dev, "interrupt urb\n");
1067 break;
1069 default:
1070 return -EINVAL;
1072 if (uurb->buffer_length > 0 &&
1073 !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1074 uurb->buffer, uurb->buffer_length)) {
1075 kfree(isopkt);
1076 kfree(dr);
1077 return -EFAULT;
1079 as = alloc_async(uurb->number_of_packets);
1080 if (!as) {
1081 kfree(isopkt);
1082 kfree(dr);
1083 return -ENOMEM;
1085 if (uurb->buffer_length > 0) {
1086 as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
1087 GFP_KERNEL);
1088 if (!as->urb->transfer_buffer) {
1089 kfree(isopkt);
1090 kfree(dr);
1091 free_async(as);
1092 return -ENOMEM;
1094 /* Isochronous input data may end up being discontiguous
1095 * if some of the packets are short. Clear the buffer so
1096 * that the gaps don't leak kernel data to userspace.
1098 if (is_in && uurb->type == USBDEVFS_URB_TYPE_ISO)
1099 memset(as->urb->transfer_buffer, 0,
1100 uurb->buffer_length);
1102 as->urb->dev = ps->dev;
1103 as->urb->pipe = (uurb->type << 30) |
1104 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1105 (uurb->endpoint & USB_DIR_IN);
1106 as->urb->transfer_flags = uurb->flags |
1107 (is_in ? URB_DIR_IN : URB_DIR_OUT);
1108 as->urb->transfer_buffer_length = uurb->buffer_length;
1109 as->urb->setup_packet = (unsigned char*)dr;
1110 as->urb->start_frame = uurb->start_frame;
1111 as->urb->number_of_packets = uurb->number_of_packets;
1112 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1113 ps->dev->speed == USB_SPEED_HIGH)
1114 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
1115 else
1116 as->urb->interval = ep->desc.bInterval;
1117 as->urb->context = as;
1118 as->urb->complete = async_completed;
1119 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1120 as->urb->iso_frame_desc[u].offset = totlen;
1121 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1122 totlen += isopkt[u].length;
1124 kfree(isopkt);
1125 as->ps = ps;
1126 as->userurb = arg;
1127 if (is_in && uurb->buffer_length > 0)
1128 as->userbuffer = uurb->buffer;
1129 else
1130 as->userbuffer = NULL;
1131 as->signr = uurb->signr;
1132 as->ifnum = ifnum;
1133 as->pid = get_pid(task_pid(current));
1134 as->uid = current->uid;
1135 as->euid = current->euid;
1136 security_task_getsecid(current, &as->secid);
1137 if (!is_in && uurb->buffer_length > 0) {
1138 if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
1139 uurb->buffer_length)) {
1140 free_async(as);
1141 return -EFAULT;
1144 snoop_urb(as->urb, as->userurb);
1145 async_newpending(as);
1146 if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
1147 dev_printk(KERN_DEBUG, &ps->dev->dev, "usbfs: usb_submit_urb returned %d\n", ret);
1148 async_removepending(as);
1149 free_async(as);
1150 return ret;
1152 return 0;
1155 static int proc_submiturb(struct dev_state *ps, void __user *arg)
1157 struct usbdevfs_urb uurb;
1159 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1160 return -EFAULT;
1162 return proc_do_submiturb(ps, &uurb, (((struct usbdevfs_urb __user *)arg)->iso_frame_desc), arg);
1165 static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
1167 struct async *as;
1169 as = async_getpending(ps, arg);
1170 if (!as)
1171 return -EINVAL;
1172 usb_kill_urb(as->urb);
1173 return 0;
1176 static int processcompl(struct async *as, void __user * __user *arg)
1178 struct urb *urb = as->urb;
1179 struct usbdevfs_urb __user *userurb = as->userurb;
1180 void __user *addr = as->userurb;
1181 unsigned int i;
1183 if (as->userbuffer && urb->actual_length) {
1184 if (urb->number_of_packets > 0) /* Isochronous */
1185 i = urb->transfer_buffer_length;
1186 else /* Non-Isoc */
1187 i = urb->actual_length;
1188 if (copy_to_user(as->userbuffer, urb->transfer_buffer, i))
1189 goto err_out;
1191 if (put_user(as->status, &userurb->status))
1192 goto err_out;
1193 if (put_user(urb->actual_length, &userurb->actual_length))
1194 goto err_out;
1195 if (put_user(urb->error_count, &userurb->error_count))
1196 goto err_out;
1198 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1199 for (i = 0; i < urb->number_of_packets; i++) {
1200 if (put_user(urb->iso_frame_desc[i].actual_length,
1201 &userurb->iso_frame_desc[i].actual_length))
1202 goto err_out;
1203 if (put_user(urb->iso_frame_desc[i].status,
1204 &userurb->iso_frame_desc[i].status))
1205 goto err_out;
1209 if (put_user(addr, (void __user * __user *)arg))
1210 return -EFAULT;
1211 return 0;
1213 err_out:
1214 return -EFAULT;
1217 static struct async* reap_as(struct dev_state *ps)
1219 DECLARE_WAITQUEUE(wait, current);
1220 struct async *as = NULL;
1221 struct usb_device *dev = ps->dev;
1223 add_wait_queue(&ps->wait, &wait);
1224 for (;;) {
1225 __set_current_state(TASK_INTERRUPTIBLE);
1226 if ((as = async_getcompleted(ps)))
1227 break;
1228 if (signal_pending(current))
1229 break;
1230 usb_unlock_device(dev);
1231 schedule();
1232 usb_lock_device(dev);
1234 remove_wait_queue(&ps->wait, &wait);
1235 set_current_state(TASK_RUNNING);
1236 return as;
1239 static int proc_reapurb(struct dev_state *ps, void __user *arg)
1241 struct async *as = reap_as(ps);
1242 if (as) {
1243 int retval = processcompl(as, (void __user * __user *)arg);
1244 free_async(as);
1245 return retval;
1247 if (signal_pending(current))
1248 return -EINTR;
1249 return -EIO;
1252 static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
1254 int retval;
1255 struct async *as;
1257 as = async_getcompleted(ps);
1258 retval = -EAGAIN;
1259 if (as) {
1260 retval = processcompl(as, (void __user * __user *)arg);
1261 free_async(as);
1263 return retval;
1266 #ifdef CONFIG_COMPAT
1268 static int get_urb32(struct usbdevfs_urb *kurb,
1269 struct usbdevfs_urb32 __user *uurb)
1271 __u32 uptr;
1272 if (get_user(kurb->type, &uurb->type) ||
1273 __get_user(kurb->endpoint, &uurb->endpoint) ||
1274 __get_user(kurb->status, &uurb->status) ||
1275 __get_user(kurb->flags, &uurb->flags) ||
1276 __get_user(kurb->buffer_length, &uurb->buffer_length) ||
1277 __get_user(kurb->actual_length, &uurb->actual_length) ||
1278 __get_user(kurb->start_frame, &uurb->start_frame) ||
1279 __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
1280 __get_user(kurb->error_count, &uurb->error_count) ||
1281 __get_user(kurb->signr, &uurb->signr))
1282 return -EFAULT;
1284 if (__get_user(uptr, &uurb->buffer))
1285 return -EFAULT;
1286 kurb->buffer = compat_ptr(uptr);
1287 if (__get_user(uptr, &uurb->usercontext))
1288 return -EFAULT;
1289 kurb->usercontext = compat_ptr(uptr);
1291 return 0;
1294 static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
1296 struct usbdevfs_urb uurb;
1298 if (get_urb32(&uurb,(struct usbdevfs_urb32 __user *)arg))
1299 return -EFAULT;
1301 return proc_do_submiturb(ps, &uurb, ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc, arg);
1304 static int processcompl_compat(struct async *as, void __user * __user *arg)
1306 struct urb *urb = as->urb;
1307 struct usbdevfs_urb32 __user *userurb = as->userurb;
1308 void __user *addr = as->userurb;
1309 unsigned int i;
1311 if (as->userbuffer && urb->actual_length)
1312 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1313 urb->actual_length))
1314 return -EFAULT;
1315 if (put_user(as->status, &userurb->status))
1316 return -EFAULT;
1317 if (put_user(urb->actual_length, &userurb->actual_length))
1318 return -EFAULT;
1319 if (put_user(urb->error_count, &userurb->error_count))
1320 return -EFAULT;
1322 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1323 for (i = 0; i < urb->number_of_packets; i++) {
1324 if (put_user(urb->iso_frame_desc[i].actual_length,
1325 &userurb->iso_frame_desc[i].actual_length))
1326 return -EFAULT;
1327 if (put_user(urb->iso_frame_desc[i].status,
1328 &userurb->iso_frame_desc[i].status))
1329 return -EFAULT;
1333 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
1334 return -EFAULT;
1335 return 0;
1338 static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
1340 struct async *as = reap_as(ps);
1341 if (as) {
1342 int retval = processcompl_compat(as, (void __user * __user *)arg);
1343 free_async(as);
1344 return retval;
1346 if (signal_pending(current))
1347 return -EINTR;
1348 return -EIO;
1351 static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
1353 int retval;
1354 struct async *as;
1356 retval = -EAGAIN;
1357 as = async_getcompleted(ps);
1358 if (as) {
1359 retval = processcompl_compat(as, (void __user * __user *)arg);
1360 free_async(as);
1362 return retval;
1365 #endif
1367 static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
1369 struct usbdevfs_disconnectsignal ds;
1371 if (copy_from_user(&ds, arg, sizeof(ds)))
1372 return -EFAULT;
1373 ps->discsignr = ds.signr;
1374 ps->disccontext = ds.context;
1375 return 0;
1378 static int proc_claiminterface(struct dev_state *ps, void __user *arg)
1380 unsigned int ifnum;
1382 if (get_user(ifnum, (unsigned int __user *)arg))
1383 return -EFAULT;
1384 return claimintf(ps, ifnum);
1387 static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
1389 unsigned int ifnum;
1390 int ret;
1392 if (get_user(ifnum, (unsigned int __user *)arg))
1393 return -EFAULT;
1394 if ((ret = releaseintf(ps, ifnum)) < 0)
1395 return ret;
1396 destroy_async_on_interface (ps, ifnum);
1397 return 0;
1400 static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
1402 int size;
1403 void *buf = NULL;
1404 int retval = 0;
1405 struct usb_interface *intf = NULL;
1406 struct usb_driver *driver = NULL;
1408 /* alloc buffer */
1409 if ((size = _IOC_SIZE (ctl->ioctl_code)) > 0) {
1410 if ((buf = kmalloc (size, GFP_KERNEL)) == NULL)
1411 return -ENOMEM;
1412 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
1413 if (copy_from_user (buf, ctl->data, size)) {
1414 kfree(buf);
1415 return -EFAULT;
1417 } else {
1418 memset (buf, 0, size);
1422 if (!connected(ps)) {
1423 kfree(buf);
1424 return -ENODEV;
1427 if (ps->dev->state != USB_STATE_CONFIGURED)
1428 retval = -EHOSTUNREACH;
1429 else if (!(intf = usb_ifnum_to_if (ps->dev, ctl->ifno)))
1430 retval = -EINVAL;
1431 else switch (ctl->ioctl_code) {
1433 /* disconnect kernel driver from interface */
1434 case USBDEVFS_DISCONNECT:
1435 if (intf->dev.driver) {
1436 driver = to_usb_driver(intf->dev.driver);
1437 dev_dbg (&intf->dev, "disconnect by usbfs\n");
1438 usb_driver_release_interface(driver, intf);
1439 } else
1440 retval = -ENODATA;
1441 break;
1443 /* let kernel drivers try to (re)bind to the interface */
1444 case USBDEVFS_CONNECT:
1445 usb_unlock_device(ps->dev);
1446 retval = bus_rescan_devices(intf->dev.bus);
1447 usb_lock_device(ps->dev);
1448 break;
1450 /* talk directly to the interface's driver */
1451 default:
1452 if (intf->dev.driver)
1453 driver = to_usb_driver(intf->dev.driver);
1454 if (driver == NULL || driver->ioctl == NULL) {
1455 retval = -ENOTTY;
1456 } else {
1457 retval = driver->ioctl (intf, ctl->ioctl_code, buf);
1458 if (retval == -ENOIOCTLCMD)
1459 retval = -ENOTTY;
1463 /* cleanup and return */
1464 if (retval >= 0
1465 && (_IOC_DIR (ctl->ioctl_code) & _IOC_READ) != 0
1466 && size > 0
1467 && copy_to_user (ctl->data, buf, size) != 0)
1468 retval = -EFAULT;
1470 kfree(buf);
1471 return retval;
1474 static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
1476 struct usbdevfs_ioctl ctrl;
1478 if (copy_from_user(&ctrl, arg, sizeof (ctrl)))
1479 return -EFAULT;
1480 return proc_ioctl(ps, &ctrl);
1483 #ifdef CONFIG_COMPAT
1484 static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
1486 struct usbdevfs_ioctl32 __user *uioc;
1487 struct usbdevfs_ioctl ctrl;
1488 u32 udata;
1490 uioc = compat_ptr((long)arg);
1491 if (get_user(ctrl.ifno, &uioc->ifno) ||
1492 get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
1493 __get_user(udata, &uioc->data))
1494 return -EFAULT;
1495 ctrl.data = compat_ptr(udata);
1497 return proc_ioctl(ps, &ctrl);
1499 #endif
1502 * NOTE: All requests here that have interface numbers as parameters
1503 * are assuming that somehow the configuration has been prevented from
1504 * changing. But there's no mechanism to ensure that...
1506 static int usbdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1508 struct dev_state *ps = file->private_data;
1509 struct usb_device *dev = ps->dev;
1510 void __user *p = (void __user *)arg;
1511 int ret = -ENOTTY;
1513 if (!(file->f_mode & FMODE_WRITE))
1514 return -EPERM;
1515 usb_lock_device(dev);
1516 if (!connected(ps)) {
1517 usb_unlock_device(dev);
1518 return -ENODEV;
1521 switch (cmd) {
1522 case USBDEVFS_CONTROL:
1523 snoop(&dev->dev, "%s: CONTROL\n", __FUNCTION__);
1524 ret = proc_control(ps, p);
1525 if (ret >= 0)
1526 inode->i_mtime = CURRENT_TIME;
1527 break;
1529 case USBDEVFS_BULK:
1530 snoop(&dev->dev, "%s: BULK\n", __FUNCTION__);
1531 ret = proc_bulk(ps, p);
1532 if (ret >= 0)
1533 inode->i_mtime = CURRENT_TIME;
1534 break;
1536 case USBDEVFS_RESETEP:
1537 snoop(&dev->dev, "%s: RESETEP\n", __FUNCTION__);
1538 ret = proc_resetep(ps, p);
1539 if (ret >= 0)
1540 inode->i_mtime = CURRENT_TIME;
1541 break;
1543 case USBDEVFS_RESET:
1544 snoop(&dev->dev, "%s: RESET\n", __FUNCTION__);
1545 ret = proc_resetdevice(ps);
1546 break;
1548 case USBDEVFS_CLEAR_HALT:
1549 snoop(&dev->dev, "%s: CLEAR_HALT\n", __FUNCTION__);
1550 ret = proc_clearhalt(ps, p);
1551 if (ret >= 0)
1552 inode->i_mtime = CURRENT_TIME;
1553 break;
1555 case USBDEVFS_GETDRIVER:
1556 snoop(&dev->dev, "%s: GETDRIVER\n", __FUNCTION__);
1557 ret = proc_getdriver(ps, p);
1558 break;
1560 case USBDEVFS_CONNECTINFO:
1561 snoop(&dev->dev, "%s: CONNECTINFO\n", __FUNCTION__);
1562 ret = proc_connectinfo(ps, p);
1563 break;
1565 case USBDEVFS_SETINTERFACE:
1566 snoop(&dev->dev, "%s: SETINTERFACE\n", __FUNCTION__);
1567 ret = proc_setintf(ps, p);
1568 break;
1570 case USBDEVFS_SETCONFIGURATION:
1571 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __FUNCTION__);
1572 ret = proc_setconfig(ps, p);
1573 break;
1575 case USBDEVFS_SUBMITURB:
1576 snoop(&dev->dev, "%s: SUBMITURB\n", __FUNCTION__);
1577 ret = proc_submiturb(ps, p);
1578 if (ret >= 0)
1579 inode->i_mtime = CURRENT_TIME;
1580 break;
1582 #ifdef CONFIG_COMPAT
1584 case USBDEVFS_SUBMITURB32:
1585 snoop(&dev->dev, "%s: SUBMITURB32\n", __FUNCTION__);
1586 ret = proc_submiturb_compat(ps, p);
1587 if (ret >= 0)
1588 inode->i_mtime = CURRENT_TIME;
1589 break;
1591 case USBDEVFS_REAPURB32:
1592 snoop(&dev->dev, "%s: REAPURB32\n", __FUNCTION__);
1593 ret = proc_reapurb_compat(ps, p);
1594 break;
1596 case USBDEVFS_REAPURBNDELAY32:
1597 snoop(&dev->dev, "%s: REAPURBDELAY32\n", __FUNCTION__);
1598 ret = proc_reapurbnonblock_compat(ps, p);
1599 break;
1601 case USBDEVFS_IOCTL32:
1602 snoop(&dev->dev, "%s: IOCTL\n", __FUNCTION__);
1603 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
1604 break;
1605 #endif
1607 case USBDEVFS_DISCARDURB:
1608 snoop(&dev->dev, "%s: DISCARDURB\n", __FUNCTION__);
1609 ret = proc_unlinkurb(ps, p);
1610 break;
1612 case USBDEVFS_REAPURB:
1613 snoop(&dev->dev, "%s: REAPURB\n", __FUNCTION__);
1614 ret = proc_reapurb(ps, p);
1615 break;
1617 case USBDEVFS_REAPURBNDELAY:
1618 snoop(&dev->dev, "%s: REAPURBDELAY\n", __FUNCTION__);
1619 ret = proc_reapurbnonblock(ps, p);
1620 break;
1622 case USBDEVFS_DISCSIGNAL:
1623 snoop(&dev->dev, "%s: DISCSIGNAL\n", __FUNCTION__);
1624 ret = proc_disconnectsignal(ps, p);
1625 break;
1627 case USBDEVFS_CLAIMINTERFACE:
1628 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __FUNCTION__);
1629 ret = proc_claiminterface(ps, p);
1630 break;
1632 case USBDEVFS_RELEASEINTERFACE:
1633 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __FUNCTION__);
1634 ret = proc_releaseinterface(ps, p);
1635 break;
1637 case USBDEVFS_IOCTL:
1638 snoop(&dev->dev, "%s: IOCTL\n", __FUNCTION__);
1639 ret = proc_ioctl_default(ps, p);
1640 break;
1642 usb_unlock_device(dev);
1643 if (ret >= 0)
1644 inode->i_atime = CURRENT_TIME;
1645 return ret;
1648 /* No kernel lock - fine */
1649 static unsigned int usbdev_poll(struct file *file, struct poll_table_struct *wait)
1651 struct dev_state *ps = file->private_data;
1652 unsigned int mask = 0;
1654 poll_wait(file, &ps->wait, wait);
1655 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
1656 mask |= POLLOUT | POLLWRNORM;
1657 if (!connected(ps))
1658 mask |= POLLERR | POLLHUP;
1659 return mask;
1662 const struct file_operations usbdev_file_operations = {
1663 .llseek = usbdev_lseek,
1664 .read = usbdev_read,
1665 .poll = usbdev_poll,
1666 .ioctl = usbdev_ioctl,
1667 .open = usbdev_open,
1668 .release = usbdev_release,
1671 static void usbdev_remove(struct usb_device *udev)
1673 struct dev_state *ps;
1674 struct siginfo sinfo;
1676 while (!list_empty(&udev->filelist)) {
1677 ps = list_entry(udev->filelist.next, struct dev_state, list);
1678 destroy_all_async(ps);
1679 wake_up_all(&ps->wait);
1680 list_del_init(&ps->list);
1681 if (ps->discsignr) {
1682 sinfo.si_signo = ps->discsignr;
1683 sinfo.si_errno = EPIPE;
1684 sinfo.si_code = SI_ASYNCIO;
1685 sinfo.si_addr = ps->disccontext;
1686 kill_pid_info_as_uid(ps->discsignr, &sinfo,
1687 ps->disc_pid, ps->disc_uid,
1688 ps->disc_euid, ps->secid);
1693 #ifdef CONFIG_USB_DEVICE_CLASS
1694 static struct class *usb_classdev_class;
1696 static int usb_classdev_add(struct usb_device *dev)
1698 struct device *cldev;
1700 cldev = device_create(usb_classdev_class, &dev->dev, dev->dev.devt,
1701 "usbdev%d.%d", dev->bus->busnum,
1702 dev->devnum);
1703 if (IS_ERR(cldev))
1704 return PTR_ERR(cldev);
1705 dev->usb_classdev = cldev;
1706 return 0;
1709 static void usb_classdev_remove(struct usb_device *dev)
1711 if (dev->usb_classdev)
1712 device_unregister(dev->usb_classdev);
1715 #else
1716 #define usb_classdev_add(dev) 0
1717 #define usb_classdev_remove(dev) do {} while (0)
1719 #endif
1721 static int usbdev_notify(struct notifier_block *self,
1722 unsigned long action, void *dev)
1724 switch (action) {
1725 case USB_DEVICE_ADD:
1726 if (usb_classdev_add(dev))
1727 return NOTIFY_BAD;
1728 break;
1729 case USB_DEVICE_REMOVE:
1730 usb_classdev_remove(dev);
1731 usbdev_remove(dev);
1732 break;
1734 return NOTIFY_OK;
1737 static struct notifier_block usbdev_nb = {
1738 .notifier_call = usbdev_notify,
1741 static struct cdev usb_device_cdev = {
1742 .kobj = {.name = "usb_device", },
1743 .owner = THIS_MODULE,
1746 int __init usb_devio_init(void)
1748 int retval;
1750 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
1751 "usb_device");
1752 if (retval) {
1753 err("unable to register minors for usb_device");
1754 goto out;
1756 cdev_init(&usb_device_cdev, &usbdev_file_operations);
1757 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
1758 if (retval) {
1759 err("unable to get usb_device major %d", USB_DEVICE_MAJOR);
1760 goto error_cdev;
1762 #ifdef CONFIG_USB_DEVICE_CLASS
1763 usb_classdev_class = class_create(THIS_MODULE, "usb_device");
1764 if (IS_ERR(usb_classdev_class)) {
1765 err("unable to register usb_device class");
1766 retval = PTR_ERR(usb_classdev_class);
1767 cdev_del(&usb_device_cdev);
1768 usb_classdev_class = NULL;
1769 goto out;
1771 #endif
1772 usb_register_notify(&usbdev_nb);
1773 out:
1774 return retval;
1776 error_cdev:
1777 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
1778 goto out;
1781 void usb_devio_cleanup(void)
1783 usb_unregister_notify(&usbdev_nb);
1784 #ifdef CONFIG_USB_DEVICE_CLASS
1785 class_destroy(usb_classdev_class);
1786 #endif
1787 cdev_del(&usb_device_cdev);
1788 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);