1 /*****************************************************************************/
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.
31 * 22.12.1999 0.1 Initial release (split from proc_usb.c)
32 * 04.01.2000 0.2 Turned into its own filesystem
35 /*****************************************************************************/
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 <asm/uaccess.h>
47 #include <asm/byteorder.h>
48 #include <linux/moduleparam.h>
50 #include "hcd.h" /* for usbcore internals */
54 struct list_head asynclist;
56 struct task_struct *task;
59 void __user *userbuffer;
64 static int usbfs_snoop = 0;
65 module_param (usbfs_snoop, bool, S_IRUGO | S_IWUSR);
66 MODULE_PARM_DESC (usbfs_snoop, "true to log all usbfs traffic");
68 #define snoop(dev, format, arg...) \
71 dev_info( dev , format , ## arg); \
75 #define MAX_USBFS_BUFFER_SIZE 16384
77 static inline int connected (struct usb_device *dev)
79 return dev->state != USB_STATE_NOTATTACHED;
82 static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
94 file->f_pos += offset;
106 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
108 struct dev_state *ps = (struct dev_state *)file->private_data;
109 struct usb_device *dev = ps->dev;
116 usb_lock_device(dev);
117 if (!connected(dev)) {
120 } else if (pos < 0) {
125 if (pos < sizeof(struct usb_device_descriptor)) {
126 struct usb_device_descriptor *desc = kmalloc(sizeof(*desc), GFP_KERNEL);
131 memcpy(desc, &dev->descriptor, sizeof(dev->descriptor));
132 le16_to_cpus(&desc->bcdUSB);
133 le16_to_cpus(&desc->idVendor);
134 le16_to_cpus(&desc->idProduct);
135 le16_to_cpus(&desc->bcdDevice);
137 len = sizeof(struct usb_device_descriptor) - pos;
140 if (copy_to_user(buf, ((char *)desc) + pos, len)) {
153 pos = sizeof(struct usb_device_descriptor);
154 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
155 struct usb_config_descriptor *config =
156 (struct usb_config_descriptor *)dev->rawdescriptors[i];
157 unsigned int length = le16_to_cpu(config->wTotalLength);
159 if (*ppos < pos + length) {
161 /* The descriptor may claim to be longer than it
162 * really is. Here is the actual allocated length. */
164 le16_to_cpu(dev->config[i].desc.wTotalLength);
166 len = length - (*ppos - pos);
170 /* Simply don't write (skip over) unallocated parts */
171 if (alloclen > (*ppos - pos)) {
172 alloclen -= (*ppos - pos);
173 if (copy_to_user(buf,
174 dev->rawdescriptors[i] + (*ppos - pos),
175 min(len, alloclen))) {
191 usb_unlock_device(dev);
196 * async list handling
199 static struct async *alloc_async(unsigned int numisoframes)
201 unsigned int assize = sizeof(struct async) + numisoframes * sizeof(struct usb_iso_packet_descriptor);
202 struct async *as = kmalloc(assize, GFP_KERNEL);
205 memset(as, 0, assize);
206 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
214 static void free_async(struct async *as)
216 kfree(as->urb->transfer_buffer);
217 kfree(as->urb->setup_packet);
218 usb_free_urb(as->urb);
222 static inline void async_newpending(struct async *as)
224 struct dev_state *ps = as->ps;
227 spin_lock_irqsave(&ps->lock, flags);
228 list_add_tail(&as->asynclist, &ps->async_pending);
229 spin_unlock_irqrestore(&ps->lock, flags);
232 static inline void async_removepending(struct async *as)
234 struct dev_state *ps = as->ps;
237 spin_lock_irqsave(&ps->lock, flags);
238 list_del_init(&as->asynclist);
239 spin_unlock_irqrestore(&ps->lock, flags);
242 static inline struct async *async_getcompleted(struct dev_state *ps)
245 struct async *as = NULL;
247 spin_lock_irqsave(&ps->lock, flags);
248 if (!list_empty(&ps->async_completed)) {
249 as = list_entry(ps->async_completed.next, struct async, asynclist);
250 list_del_init(&as->asynclist);
252 spin_unlock_irqrestore(&ps->lock, flags);
256 static inline struct async *async_getpending(struct dev_state *ps, void __user *userurb)
261 spin_lock_irqsave(&ps->lock, flags);
262 list_for_each_entry(as, &ps->async_pending, asynclist)
263 if (as->userurb == userurb) {
264 list_del_init(&as->asynclist);
265 spin_unlock_irqrestore(&ps->lock, flags);
268 spin_unlock_irqrestore(&ps->lock, flags);
272 static void async_completed(struct urb *urb, struct pt_regs *regs)
274 struct async *as = (struct async *)urb->context;
275 struct dev_state *ps = as->ps;
276 struct siginfo sinfo;
278 spin_lock(&ps->lock);
279 list_move_tail(&as->asynclist, &ps->async_completed);
280 spin_unlock(&ps->lock);
282 sinfo.si_signo = as->signr;
283 sinfo.si_errno = as->urb->status;
284 sinfo.si_code = SI_ASYNCIO;
285 sinfo.si_addr = as->userurb;
286 send_sig_info(as->signr, &sinfo, as->task);
291 static void destroy_async (struct dev_state *ps, struct list_head *list)
296 spin_lock_irqsave(&ps->lock, flags);
297 while (!list_empty(list)) {
298 as = list_entry(list->next, struct async, asynclist);
299 list_del_init(&as->asynclist);
301 /* drop the spinlock so the completion handler can run */
302 spin_unlock_irqrestore(&ps->lock, flags);
303 usb_kill_urb(as->urb);
304 spin_lock_irqsave(&ps->lock, flags);
306 spin_unlock_irqrestore(&ps->lock, flags);
307 as = async_getcompleted(ps);
310 as = async_getcompleted(ps);
314 static void destroy_async_on_interface (struct dev_state *ps, unsigned int ifnum)
316 struct list_head *p, *q, hitlist;
319 INIT_LIST_HEAD(&hitlist);
320 spin_lock_irqsave(&ps->lock, flags);
321 list_for_each_safe(p, q, &ps->async_pending)
322 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
323 list_move_tail(p, &hitlist);
324 spin_unlock_irqrestore(&ps->lock, flags);
325 destroy_async(ps, &hitlist);
328 static inline void destroy_all_async(struct dev_state *ps)
330 destroy_async(ps, &ps->async_pending);
334 * interface claims are made only at the request of user level code,
335 * which can also release them (explicitly or by closing files).
336 * they're also undone when devices disconnect.
339 static int driver_probe (struct usb_interface *intf,
340 const struct usb_device_id *id)
345 static void driver_disconnect(struct usb_interface *intf)
347 struct dev_state *ps = usb_get_intfdata (intf);
348 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
353 /* NOTE: this relies on usbcore having canceled and completed
354 * all pending I/O requests; 2.6 does that.
357 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
358 clear_bit(ifnum, &ps->ifclaimed);
360 warn("interface number %u out of range", ifnum);
362 usb_set_intfdata (intf, NULL);
364 /* force async requests to complete */
365 destroy_async_on_interface(ps, ifnum);
368 struct usb_driver usbfs_driver = {
369 .owner = THIS_MODULE,
371 .probe = driver_probe,
372 .disconnect = driver_disconnect,
375 static int claimintf(struct dev_state *ps, unsigned int ifnum)
377 struct usb_device *dev = ps->dev;
378 struct usb_interface *intf;
381 if (ifnum >= 8*sizeof(ps->ifclaimed))
383 /* already claimed */
384 if (test_bit(ifnum, &ps->ifclaimed))
387 /* lock against other changes to driver bindings */
388 down_write(&usb_bus_type.subsys.rwsem);
389 intf = usb_ifnum_to_if(dev, ifnum);
393 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
394 up_write(&usb_bus_type.subsys.rwsem);
396 set_bit(ifnum, &ps->ifclaimed);
400 static int releaseintf(struct dev_state *ps, unsigned int ifnum)
402 struct usb_device *dev;
403 struct usb_interface *intf;
407 if (ifnum >= 8*sizeof(ps->ifclaimed))
410 /* lock against other changes to driver bindings */
411 down_write(&usb_bus_type.subsys.rwsem);
412 intf = usb_ifnum_to_if(dev, ifnum);
415 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
416 usb_driver_release_interface(&usbfs_driver, intf);
419 up_write(&usb_bus_type.subsys.rwsem);
423 static int checkintf(struct dev_state *ps, unsigned int ifnum)
425 if (ps->dev->state != USB_STATE_CONFIGURED)
426 return -EHOSTUNREACH;
427 if (ifnum >= 8*sizeof(ps->ifclaimed))
429 if (test_bit(ifnum, &ps->ifclaimed))
431 /* if not yet claimed, claim it for the driver */
432 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim interface %u before use\n",
433 current->pid, current->comm, ifnum);
434 return claimintf(ps, ifnum);
437 static int findintfep(struct usb_device *dev, unsigned int ep)
439 unsigned int i, j, e;
440 struct usb_interface *intf;
441 struct usb_host_interface *alts;
442 struct usb_endpoint_descriptor *endpt;
444 if (ep & ~(USB_DIR_IN|0xf))
448 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
449 intf = dev->actconfig->interface[i];
450 for (j = 0; j < intf->num_altsetting; j++) {
451 alts = &intf->altsetting[j];
452 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
453 endpt = &alts->endpoint[e].desc;
454 if (endpt->bEndpointAddress == ep)
455 return alts->desc.bInterfaceNumber;
462 static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype, unsigned int index)
466 if (ps->dev->state != USB_STATE_CONFIGURED)
467 return -EHOSTUNREACH;
468 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
472 switch (requesttype & USB_RECIP_MASK) {
473 case USB_RECIP_ENDPOINT:
474 if ((ret = findintfep(ps->dev, index)) >= 0)
475 ret = checkintf(ps, ret);
478 case USB_RECIP_INTERFACE:
479 ret = checkintf(ps, index);
488 static int usbdev_open(struct inode *inode, struct file *file)
490 struct usb_device *dev;
491 struct dev_state *ps;
495 * no locking necessary here, as chrdev_open has the kernel lock
496 * (still acquire the kernel lock for safety)
499 if (!(ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL)))
504 dev = usb_get_dev(inode->u.generic_ip);
512 spin_lock_init(&ps->lock);
513 INIT_LIST_HEAD(&ps->async_pending);
514 INIT_LIST_HEAD(&ps->async_completed);
515 init_waitqueue_head(&ps->wait);
517 ps->disctask = current;
518 ps->disccontext = NULL;
521 list_add_tail(&ps->list, &dev->filelist);
522 file->private_data = ps;
529 static int usbdev_release(struct inode *inode, struct file *file)
531 struct dev_state *ps = (struct dev_state *)file->private_data;
532 struct usb_device *dev = ps->dev;
535 usb_lock_device(dev);
536 list_del_init(&ps->list);
537 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
539 if (test_bit(ifnum, &ps->ifclaimed))
540 releaseintf(ps, ifnum);
542 destroy_all_async(ps);
543 usb_unlock_device(dev);
550 static int proc_control(struct dev_state *ps, void __user *arg)
552 struct usb_device *dev = ps->dev;
553 struct usbdevfs_ctrltransfer ctrl;
558 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
560 if ((ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex)))
562 if (ctrl.wLength > PAGE_SIZE)
564 if (!(tbuf = (unsigned char *)__get_free_page(GFP_KERNEL)))
567 if (ctrl.bRequestType & 0x80) {
568 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data, ctrl.wLength)) {
569 free_page((unsigned long)tbuf);
572 snoop(&dev->dev, "control read: bRequest=%02x bRrequestType=%02x wValue=%04x wIndex=%04x\n",
573 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue, ctrl.wIndex);
575 usb_unlock_device(dev);
576 i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest, ctrl.bRequestType,
577 ctrl.wValue, ctrl.wIndex, tbuf, ctrl.wLength, tmo);
578 usb_lock_device(dev);
579 if ((i > 0) && ctrl.wLength) {
581 dev_info(&dev->dev, "control read: data ");
582 for (j = 0; j < ctrl.wLength; ++j)
583 printk ("%02x ", (unsigned char)(tbuf)[j]);
586 if (copy_to_user(ctrl.data, tbuf, ctrl.wLength)) {
587 free_page((unsigned long)tbuf);
593 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
594 free_page((unsigned long)tbuf);
598 snoop(&dev->dev, "control write: bRequest=%02x bRrequestType=%02x wValue=%04x wIndex=%04x\n",
599 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue, ctrl.wIndex);
601 dev_info(&dev->dev, "control write: data: ");
602 for (j = 0; j < ctrl.wLength; ++j)
603 printk ("%02x ", (unsigned char)(tbuf)[j]);
606 usb_unlock_device(dev);
607 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest, ctrl.bRequestType,
608 ctrl.wValue, ctrl.wIndex, tbuf, ctrl.wLength, tmo);
609 usb_lock_device(dev);
611 free_page((unsigned long)tbuf);
612 if (i<0 && i != -EPIPE) {
613 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
614 "failed cmd %s rqt %u rq %u len %u ret %d\n",
615 current->comm, ctrl.bRequestType, ctrl.bRequest,
621 static int proc_bulk(struct dev_state *ps, void __user *arg)
623 struct usb_device *dev = ps->dev;
624 struct usbdevfs_bulktransfer bulk;
625 unsigned int tmo, len1, pipe;
630 if (copy_from_user(&bulk, arg, sizeof(bulk)))
632 if ((ret = findintfep(ps->dev, bulk.ep)) < 0)
634 if ((ret = checkintf(ps, ret)))
636 if (bulk.ep & USB_DIR_IN)
637 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
639 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
640 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
643 if (len1 > MAX_USBFS_BUFFER_SIZE)
645 if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
648 if (bulk.ep & 0x80) {
649 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
653 usb_unlock_device(dev);
654 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
655 usb_lock_device(dev);
657 if (copy_to_user(bulk.data, tbuf, len2)) {
664 if (copy_from_user(tbuf, bulk.data, len1)) {
669 usb_unlock_device(dev);
670 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
671 usb_lock_device(dev);
679 static int proc_resetep(struct dev_state *ps, void __user *arg)
684 if (get_user(ep, (unsigned int __user *)arg))
686 if ((ret = findintfep(ps->dev, ep)) < 0)
688 if ((ret = checkintf(ps, ret)))
690 usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0);
694 static int proc_clearhalt(struct dev_state *ps, void __user *arg)
700 if (get_user(ep, (unsigned int __user *)arg))
702 if ((ret = findintfep(ps->dev, ep)) < 0)
704 if ((ret = checkintf(ps, ret)))
707 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
709 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
711 return usb_clear_halt(ps->dev, pipe);
715 static int proc_getdriver(struct dev_state *ps, void __user *arg)
717 struct usbdevfs_getdriver gd;
718 struct usb_interface *intf;
721 if (copy_from_user(&gd, arg, sizeof(gd)))
723 down_read(&usb_bus_type.subsys.rwsem);
724 intf = usb_ifnum_to_if(ps->dev, gd.interface);
725 if (!intf || !intf->dev.driver)
728 strncpy(gd.driver, intf->dev.driver->name,
730 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
732 up_read(&usb_bus_type.subsys.rwsem);
736 static int proc_connectinfo(struct dev_state *ps, void __user *arg)
738 struct usbdevfs_connectinfo ci;
740 ci.devnum = ps->dev->devnum;
741 ci.slow = ps->dev->speed == USB_SPEED_LOW;
742 if (copy_to_user(arg, &ci, sizeof(ci)))
747 static int proc_resetdevice(struct dev_state *ps)
749 return usb_reset_device(ps->dev);
753 static int proc_setintf(struct dev_state *ps, void __user *arg)
755 struct usbdevfs_setinterface setintf;
758 if (copy_from_user(&setintf, arg, sizeof(setintf)))
760 if ((ret = checkintf(ps, setintf.interface)))
762 return usb_set_interface(ps->dev, setintf.interface,
766 static int proc_setconfig(struct dev_state *ps, void __user *arg)
770 struct usb_host_config *actconfig;
772 if (get_user(u, (unsigned int __user *)arg))
775 actconfig = ps->dev->actconfig;
777 /* Don't touch the device if any interfaces are claimed.
778 * It could interfere with other drivers' operations, and if
779 * an interface is claimed by usbfs it could easily deadlock.
784 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
785 if (usb_interface_claimed(actconfig->interface[i])) {
786 dev_warn (&ps->dev->dev,
787 "usbfs: interface %d claimed by %s "
788 "while '%s' sets config #%d\n",
789 actconfig->interface[i]
791 ->desc.bInterfaceNumber,
792 actconfig->interface[i]
801 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
802 * so avoid usb_set_configuration()'s kick to sysfs
805 if (actconfig && actconfig->desc.bConfigurationValue == u)
806 status = usb_reset_configuration(ps->dev);
808 status = usb_set_configuration(ps->dev, u);
815 static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
816 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
819 struct usbdevfs_iso_packet_desc *isopkt = NULL;
820 struct usb_host_endpoint *ep;
822 struct usb_ctrlrequest *dr = NULL;
823 unsigned int u, totlen, isofrmlen;
824 int ret, interval = 0, ifnum = -1;
826 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP|USBDEVFS_URB_SHORT_NOT_OK|
827 URB_NO_FSBR|URB_ZERO_PACKET))
831 if (uurb->signr != 0 && (uurb->signr < SIGRTMIN || uurb->signr > SIGRTMAX))
833 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL && (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
834 if ((ifnum = findintfep(ps->dev, uurb->endpoint)) < 0)
836 if ((ret = checkintf(ps, ifnum)))
839 if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0)
840 ep = ps->dev->ep_in [uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
842 ep = ps->dev->ep_out [uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
846 case USBDEVFS_URB_TYPE_CONTROL:
847 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
848 != USB_ENDPOINT_XFER_CONTROL)
850 /* min 8 byte setup packet, max arbitrary */
851 if (uurb->buffer_length < 8 || uurb->buffer_length > PAGE_SIZE)
853 if (!(dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL)))
855 if (copy_from_user(dr, uurb->buffer, 8)) {
859 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
863 if ((ret = check_ctrlrecip(ps, dr->bRequestType, le16_to_cpup(&dr->wIndex)))) {
867 uurb->endpoint = (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) | (dr->bRequestType & USB_ENDPOINT_DIR_MASK);
868 uurb->number_of_packets = 0;
869 uurb->buffer_length = le16_to_cpup(&dr->wLength);
871 if (!access_ok((uurb->endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb->buffer, uurb->buffer_length)) {
877 case USBDEVFS_URB_TYPE_BULK:
878 switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
879 case USB_ENDPOINT_XFER_CONTROL:
880 case USB_ENDPOINT_XFER_ISOC:
882 /* allow single-shot interrupt transfers, at bogus rates */
884 uurb->number_of_packets = 0;
885 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
887 if (!access_ok((uurb->endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb->buffer, uurb->buffer_length))
891 case USBDEVFS_URB_TYPE_ISO:
892 /* arbitrary limit */
893 if (uurb->number_of_packets < 1 || uurb->number_of_packets > 128)
895 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
896 != USB_ENDPOINT_XFER_ISOC)
898 interval = 1 << min (15, ep->desc.bInterval - 1);
899 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) * uurb->number_of_packets;
900 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
902 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
906 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
907 if (isopkt[u].length > 1023) {
911 totlen += isopkt[u].length;
913 if (totlen > 32768) {
917 uurb->buffer_length = totlen;
920 case USBDEVFS_URB_TYPE_INTERRUPT:
921 uurb->number_of_packets = 0;
922 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
923 != USB_ENDPOINT_XFER_INT)
925 if (ps->dev->speed == USB_SPEED_HIGH)
926 interval = 1 << min (15, ep->desc.bInterval - 1);
928 interval = ep->desc.bInterval;
929 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
931 if (!access_ok((uurb->endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb->buffer, uurb->buffer_length))
938 if (!(as = alloc_async(uurb->number_of_packets))) {
943 if (!(as->urb->transfer_buffer = kmalloc(uurb->buffer_length, GFP_KERNEL))) {
949 as->urb->dev = ps->dev;
950 as->urb->pipe = (uurb->type << 30) | __create_pipe(ps->dev, uurb->endpoint & 0xf) | (uurb->endpoint & USB_DIR_IN);
951 as->urb->transfer_flags = uurb->flags;
952 as->urb->transfer_buffer_length = uurb->buffer_length;
953 as->urb->setup_packet = (unsigned char*)dr;
954 as->urb->start_frame = uurb->start_frame;
955 as->urb->number_of_packets = uurb->number_of_packets;
956 as->urb->interval = interval;
957 as->urb->context = as;
958 as->urb->complete = async_completed;
959 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
960 as->urb->iso_frame_desc[u].offset = totlen;
961 as->urb->iso_frame_desc[u].length = isopkt[u].length;
962 totlen += isopkt[u].length;
967 if (uurb->endpoint & USB_DIR_IN)
968 as->userbuffer = uurb->buffer;
970 as->userbuffer = NULL;
971 as->signr = uurb->signr;
974 if (!(uurb->endpoint & USB_DIR_IN)) {
975 if (copy_from_user(as->urb->transfer_buffer, uurb->buffer, as->urb->transfer_buffer_length)) {
980 async_newpending(as);
981 if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
982 dev_printk(KERN_DEBUG, &ps->dev->dev, "usbfs: usb_submit_urb returned %d\n", ret);
983 async_removepending(as);
990 static int proc_submiturb(struct dev_state *ps, void __user *arg)
992 struct usbdevfs_urb uurb;
994 if (copy_from_user(&uurb, arg, sizeof(uurb)))
997 return proc_do_submiturb(ps, &uurb, (((struct usbdevfs_urb __user *)arg)->iso_frame_desc), arg);
1000 static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
1004 as = async_getpending(ps, arg);
1007 usb_kill_urb(as->urb);
1011 static int processcompl(struct async *as, void __user * __user *arg)
1013 struct urb *urb = as->urb;
1014 struct usbdevfs_urb __user *userurb = as->userurb;
1015 void __user *addr = as->userurb;
1019 if (copy_to_user(as->userbuffer, urb->transfer_buffer, urb->transfer_buffer_length))
1021 if (put_user(urb->status, &userurb->status))
1023 if (put_user(urb->actual_length, &userurb->actual_length))
1025 if (put_user(urb->error_count, &userurb->error_count))
1028 if (usb_pipeisoc(urb->pipe)) {
1029 for (i = 0; i < urb->number_of_packets; i++) {
1030 if (put_user(urb->iso_frame_desc[i].actual_length,
1031 &userurb->iso_frame_desc[i].actual_length))
1033 if (put_user(urb->iso_frame_desc[i].status,
1034 &userurb->iso_frame_desc[i].status))
1041 if (put_user(addr, (void __user * __user *)arg))
1046 static struct async* reap_as(struct dev_state *ps)
1048 DECLARE_WAITQUEUE(wait, current);
1049 struct async *as = NULL;
1050 struct usb_device *dev = ps->dev;
1052 add_wait_queue(&ps->wait, &wait);
1054 __set_current_state(TASK_INTERRUPTIBLE);
1055 if ((as = async_getcompleted(ps)))
1057 if (signal_pending(current))
1059 usb_unlock_device(dev);
1061 usb_lock_device(dev);
1063 remove_wait_queue(&ps->wait, &wait);
1064 set_current_state(TASK_RUNNING);
1068 static int proc_reapurb(struct dev_state *ps, void __user *arg)
1070 struct async *as = reap_as(ps);
1072 return processcompl(as, (void __user * __user *)arg);
1073 if (signal_pending(current))
1078 static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
1082 if (!(as = async_getcompleted(ps)))
1084 return processcompl(as, (void __user * __user *)arg);
1087 #ifdef CONFIG_COMPAT
1089 static int get_urb32(struct usbdevfs_urb *kurb,
1090 struct usbdevfs_urb32 __user *uurb)
1093 if (get_user(kurb->type, &uurb->type) ||
1094 __get_user(kurb->endpoint, &uurb->endpoint) ||
1095 __get_user(kurb->status, &uurb->status) ||
1096 __get_user(kurb->flags, &uurb->flags) ||
1097 __get_user(kurb->buffer_length, &uurb->buffer_length) ||
1098 __get_user(kurb->actual_length, &uurb->actual_length) ||
1099 __get_user(kurb->start_frame, &uurb->start_frame) ||
1100 __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
1101 __get_user(kurb->error_count, &uurb->error_count) ||
1102 __get_user(kurb->signr, &uurb->signr))
1105 if (__get_user(uptr, &uurb->buffer))
1107 kurb->buffer = compat_ptr(uptr);
1108 if (__get_user(uptr, &uurb->buffer))
1110 kurb->usercontext = compat_ptr(uptr);
1115 static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
1117 struct usbdevfs_urb uurb;
1119 if (get_urb32(&uurb,(struct usbdevfs_urb32 *)arg))
1122 return proc_do_submiturb(ps, &uurb, ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc, arg);
1125 static int processcompl_compat(struct async *as, void __user * __user *arg)
1127 struct urb *urb = as->urb;
1128 struct usbdevfs_urb32 __user *userurb = as->userurb;
1129 void __user *addr = as->userurb;
1133 if (copy_to_user(as->userbuffer, urb->transfer_buffer, urb->transfer_buffer_length))
1135 if (put_user(urb->status, &userurb->status))
1137 if (put_user(urb->actual_length, &userurb->actual_length))
1139 if (put_user(urb->error_count, &userurb->error_count))
1142 if (usb_pipeisoc(urb->pipe)) {
1143 for (i = 0; i < urb->number_of_packets; i++) {
1144 if (put_user(urb->iso_frame_desc[i].actual_length,
1145 &userurb->iso_frame_desc[i].actual_length))
1147 if (put_user(urb->iso_frame_desc[i].status,
1148 &userurb->iso_frame_desc[i].status))
1154 if (put_user((u32)(u64)addr, (u32 __user *)arg))
1159 static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
1161 struct async *as = reap_as(ps);
1163 return processcompl_compat(as, (void __user * __user *)arg);
1164 if (signal_pending(current))
1169 static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
1173 if (!(as = async_getcompleted(ps)))
1175 return processcompl_compat(as, (void __user * __user *)arg);
1180 static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
1182 struct usbdevfs_disconnectsignal ds;
1184 if (copy_from_user(&ds, arg, sizeof(ds)))
1186 if (ds.signr != 0 && (ds.signr < SIGRTMIN || ds.signr > SIGRTMAX))
1188 ps->discsignr = ds.signr;
1189 ps->disccontext = ds.context;
1193 static int proc_claiminterface(struct dev_state *ps, void __user *arg)
1197 if (get_user(ifnum, (unsigned int __user *)arg))
1199 return claimintf(ps, ifnum);
1202 static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
1207 if (get_user(ifnum, (unsigned int __user *)arg))
1209 if ((ret = releaseintf(ps, ifnum)) < 0)
1211 destroy_async_on_interface (ps, ifnum);
1215 static int proc_ioctl (struct dev_state *ps, void __user *arg)
1217 struct usbdevfs_ioctl ctrl;
1221 struct usb_interface *intf = NULL;
1222 struct usb_driver *driver = NULL;
1225 /* get input parameters and alloc buffer */
1226 if (copy_from_user(&ctrl, arg, sizeof (ctrl)))
1228 if ((size = _IOC_SIZE (ctrl.ioctl_code)) > 0) {
1229 if ((buf = kmalloc (size, GFP_KERNEL)) == NULL)
1231 if ((_IOC_DIR(ctrl.ioctl_code) & _IOC_WRITE)) {
1232 if (copy_from_user (buf, ctrl.data, size)) {
1237 memset (buf, 0, size);
1241 if (!connected(ps->dev)) {
1246 if (ps->dev->state != USB_STATE_CONFIGURED)
1247 retval = -EHOSTUNREACH;
1248 else if (!(intf = usb_ifnum_to_if (ps->dev, ctrl.ifno)))
1250 else switch (ctrl.ioctl_code) {
1252 /* disconnect kernel driver from interface */
1253 case USBDEVFS_DISCONNECT:
1255 /* don't allow the user to unbind the hub driver from
1256 * a hub with children to manage */
1257 for (i = 0; i < ps->dev->maxchild; ++i) {
1258 if (ps->dev->children[i])
1264 down_write(&usb_bus_type.subsys.rwsem);
1265 if (intf->dev.driver) {
1266 driver = to_usb_driver(intf->dev.driver);
1267 dev_dbg (&intf->dev, "disconnect by usbfs\n");
1268 usb_driver_release_interface(driver, intf);
1271 up_write(&usb_bus_type.subsys.rwsem);
1274 /* let kernel drivers try to (re)bind to the interface */
1275 case USBDEVFS_CONNECT:
1276 usb_unlock_device(ps->dev);
1277 usb_lock_all_devices();
1278 bus_rescan_devices(intf->dev.bus);
1279 usb_unlock_all_devices();
1280 usb_lock_device(ps->dev);
1283 /* talk directly to the interface's driver */
1285 down_read(&usb_bus_type.subsys.rwsem);
1286 if (intf->dev.driver)
1287 driver = to_usb_driver(intf->dev.driver);
1288 if (driver == NULL || driver->ioctl == NULL) {
1291 retval = driver->ioctl (intf, ctrl.ioctl_code, buf);
1292 if (retval == -ENOIOCTLCMD)
1295 up_read(&usb_bus_type.subsys.rwsem);
1298 /* cleanup and return */
1300 && (_IOC_DIR (ctrl.ioctl_code) & _IOC_READ) != 0
1302 && copy_to_user (ctrl.data, buf, size) != 0)
1310 * NOTE: All requests here that have interface numbers as parameters
1311 * are assuming that somehow the configuration has been prevented from
1312 * changing. But there's no mechanism to ensure that...
1314 static int usbdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1316 struct dev_state *ps = (struct dev_state *)file->private_data;
1317 struct usb_device *dev = ps->dev;
1318 void __user *p = (void __user *)arg;
1321 if (!(file->f_mode & FMODE_WRITE))
1323 usb_lock_device(dev);
1324 if (!connected(dev)) {
1325 usb_unlock_device(dev);
1330 case USBDEVFS_CONTROL:
1331 snoop(&dev->dev, "%s: CONTROL\n", __FUNCTION__);
1332 ret = proc_control(ps, p);
1334 inode->i_mtime = CURRENT_TIME;
1338 snoop(&dev->dev, "%s: BULK\n", __FUNCTION__);
1339 ret = proc_bulk(ps, p);
1341 inode->i_mtime = CURRENT_TIME;
1344 case USBDEVFS_RESETEP:
1345 snoop(&dev->dev, "%s: RESETEP\n", __FUNCTION__);
1346 ret = proc_resetep(ps, p);
1348 inode->i_mtime = CURRENT_TIME;
1351 case USBDEVFS_RESET:
1352 snoop(&dev->dev, "%s: RESET\n", __FUNCTION__);
1353 ret = proc_resetdevice(ps);
1356 case USBDEVFS_CLEAR_HALT:
1357 snoop(&dev->dev, "%s: CLEAR_HALT\n", __FUNCTION__);
1358 ret = proc_clearhalt(ps, p);
1360 inode->i_mtime = CURRENT_TIME;
1363 case USBDEVFS_GETDRIVER:
1364 snoop(&dev->dev, "%s: GETDRIVER\n", __FUNCTION__);
1365 ret = proc_getdriver(ps, p);
1368 case USBDEVFS_CONNECTINFO:
1369 snoop(&dev->dev, "%s: CONNECTINFO\n", __FUNCTION__);
1370 ret = proc_connectinfo(ps, p);
1373 case USBDEVFS_SETINTERFACE:
1374 snoop(&dev->dev, "%s: SETINTERFACE\n", __FUNCTION__);
1375 ret = proc_setintf(ps, p);
1378 case USBDEVFS_SETCONFIGURATION:
1379 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __FUNCTION__);
1380 ret = proc_setconfig(ps, p);
1383 case USBDEVFS_SUBMITURB:
1384 snoop(&dev->dev, "%s: SUBMITURB\n", __FUNCTION__);
1385 ret = proc_submiturb(ps, p);
1387 inode->i_mtime = CURRENT_TIME;
1390 #ifdef CONFIG_COMPAT
1392 case USBDEVFS_SUBMITURB32:
1393 snoop(&dev->dev, "%s: SUBMITURB32\n", __FUNCTION__);
1394 ret = proc_submiturb_compat(ps, p);
1396 inode->i_mtime = CURRENT_TIME;
1399 case USBDEVFS_REAPURB32:
1400 snoop(&dev->dev, "%s: REAPURB32\n", __FUNCTION__);
1401 ret = proc_reapurb_compat(ps, p);
1404 case USBDEVFS_REAPURBNDELAY32:
1405 snoop(&dev->dev, "%s: REAPURBDELAY32\n", __FUNCTION__);
1406 ret = proc_reapurbnonblock_compat(ps, p);
1411 case USBDEVFS_DISCARDURB:
1412 snoop(&dev->dev, "%s: DISCARDURB\n", __FUNCTION__);
1413 ret = proc_unlinkurb(ps, p);
1416 case USBDEVFS_REAPURB:
1417 snoop(&dev->dev, "%s: REAPURB\n", __FUNCTION__);
1418 ret = proc_reapurb(ps, p);
1421 case USBDEVFS_REAPURBNDELAY:
1422 snoop(&dev->dev, "%s: REAPURBDELAY\n", __FUNCTION__);
1423 ret = proc_reapurbnonblock(ps, p);
1426 case USBDEVFS_DISCSIGNAL:
1427 snoop(&dev->dev, "%s: DISCSIGNAL\n", __FUNCTION__);
1428 ret = proc_disconnectsignal(ps, p);
1431 case USBDEVFS_CLAIMINTERFACE:
1432 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __FUNCTION__);
1433 ret = proc_claiminterface(ps, p);
1436 case USBDEVFS_RELEASEINTERFACE:
1437 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __FUNCTION__);
1438 ret = proc_releaseinterface(ps, p);
1441 case USBDEVFS_IOCTL:
1442 snoop(&dev->dev, "%s: IOCTL\n", __FUNCTION__);
1443 ret = proc_ioctl(ps, p);
1446 usb_unlock_device(dev);
1448 inode->i_atime = CURRENT_TIME;
1452 /* No kernel lock - fine */
1453 static unsigned int usbdev_poll(struct file *file, struct poll_table_struct *wait)
1455 struct dev_state *ps = (struct dev_state *)file->private_data;
1456 unsigned int mask = 0;
1458 poll_wait(file, &ps->wait, wait);
1459 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
1460 mask |= POLLOUT | POLLWRNORM;
1461 if (!connected(ps->dev))
1462 mask |= POLLERR | POLLHUP;
1466 struct file_operations usbfs_device_file_operations = {
1467 .llseek = usbdev_lseek,
1468 .read = usbdev_read,
1469 .poll = usbdev_poll,
1470 .ioctl = usbdev_ioctl,
1471 .open = usbdev_open,
1472 .release = usbdev_release,