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 len = sizeof(struct usb_device_descriptor) - pos;
129 if (copy_to_user(buf, ((char *)&dev->descriptor) + pos, len)) {
140 pos = sizeof(struct usb_device_descriptor);
141 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
142 struct usb_config_descriptor *config =
143 (struct usb_config_descriptor *)dev->rawdescriptors[i];
144 unsigned int length = le16_to_cpu(config->wTotalLength);
146 if (*ppos < pos + length) {
148 /* The descriptor may claim to be longer than it
149 * really is. Here is the actual allocated length. */
151 dev->config[i].desc.wTotalLength;
153 len = length - (*ppos - pos);
157 /* Simply don't write (skip over) unallocated parts */
158 if (alloclen > (*ppos - pos)) {
159 alloclen -= (*ppos - pos);
160 if (copy_to_user(buf,
161 dev->rawdescriptors[i] + (*ppos - pos),
162 min(len, alloclen))) {
178 usb_unlock_device(dev);
183 * async list handling
186 static struct async *alloc_async(unsigned int numisoframes)
188 unsigned int assize = sizeof(struct async) + numisoframes * sizeof(struct usb_iso_packet_descriptor);
189 struct async *as = kmalloc(assize, GFP_KERNEL);
192 memset(as, 0, assize);
193 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
201 static void free_async(struct async *as)
203 if (as->urb->transfer_buffer)
204 kfree(as->urb->transfer_buffer);
205 if (as->urb->setup_packet)
206 kfree(as->urb->setup_packet);
207 usb_free_urb(as->urb);
211 static inline void async_newpending(struct async *as)
213 struct dev_state *ps = as->ps;
216 spin_lock_irqsave(&ps->lock, flags);
217 list_add_tail(&as->asynclist, &ps->async_pending);
218 spin_unlock_irqrestore(&ps->lock, flags);
221 static inline void async_removepending(struct async *as)
223 struct dev_state *ps = as->ps;
226 spin_lock_irqsave(&ps->lock, flags);
227 list_del_init(&as->asynclist);
228 spin_unlock_irqrestore(&ps->lock, flags);
231 static inline struct async *async_getcompleted(struct dev_state *ps)
234 struct async *as = NULL;
236 spin_lock_irqsave(&ps->lock, flags);
237 if (!list_empty(&ps->async_completed)) {
238 as = list_entry(ps->async_completed.next, struct async, asynclist);
239 list_del_init(&as->asynclist);
241 spin_unlock_irqrestore(&ps->lock, flags);
245 static inline struct async *async_getpending(struct dev_state *ps, void __user *userurb)
250 spin_lock_irqsave(&ps->lock, flags);
251 list_for_each_entry(as, &ps->async_pending, asynclist)
252 if (as->userurb == userurb) {
253 list_del_init(&as->asynclist);
254 spin_unlock_irqrestore(&ps->lock, flags);
257 spin_unlock_irqrestore(&ps->lock, flags);
261 static void async_completed(struct urb *urb, struct pt_regs *regs)
263 struct async *as = (struct async *)urb->context;
264 struct dev_state *ps = as->ps;
265 struct siginfo sinfo;
267 spin_lock(&ps->lock);
268 list_move_tail(&as->asynclist, &ps->async_completed);
269 spin_unlock(&ps->lock);
271 sinfo.si_signo = as->signr;
272 sinfo.si_errno = as->urb->status;
273 sinfo.si_code = SI_ASYNCIO;
274 sinfo.si_addr = as->userurb;
275 send_sig_info(as->signr, &sinfo, as->task);
280 static void destroy_async (struct dev_state *ps, struct list_head *list)
285 spin_lock_irqsave(&ps->lock, flags);
286 while (!list_empty(list)) {
287 as = list_entry(list->next, struct async, asynclist);
288 list_del_init(&as->asynclist);
290 /* drop the spinlock so the completion handler can run */
291 spin_unlock_irqrestore(&ps->lock, flags);
292 usb_kill_urb(as->urb);
293 spin_lock_irqsave(&ps->lock, flags);
295 spin_unlock_irqrestore(&ps->lock, flags);
296 as = async_getcompleted(ps);
299 as = async_getcompleted(ps);
303 static void destroy_async_on_interface (struct dev_state *ps, unsigned int ifnum)
305 struct list_head *p, *q, hitlist;
308 INIT_LIST_HEAD(&hitlist);
309 spin_lock_irqsave(&ps->lock, flags);
310 list_for_each_safe(p, q, &ps->async_pending)
311 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
312 list_move_tail(p, &hitlist);
313 spin_unlock_irqrestore(&ps->lock, flags);
314 destroy_async(ps, &hitlist);
317 static inline void destroy_all_async(struct dev_state *ps)
319 destroy_async(ps, &ps->async_pending);
323 * interface claims are made only at the request of user level code,
324 * which can also release them (explicitly or by closing files).
325 * they're also undone when devices disconnect.
328 static int driver_probe (struct usb_interface *intf,
329 const struct usb_device_id *id)
334 static void driver_disconnect(struct usb_interface *intf)
336 struct dev_state *ps = usb_get_intfdata (intf);
337 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
342 /* NOTE: this relies on usbcore having canceled and completed
343 * all pending I/O requests; 2.6 does that.
346 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
347 clear_bit(ifnum, &ps->ifclaimed);
349 warn("interface number %u out of range", ifnum);
351 usb_set_intfdata (intf, NULL);
353 /* force async requests to complete */
354 destroy_async_on_interface(ps, ifnum);
357 struct usb_driver usbfs_driver = {
358 .owner = THIS_MODULE,
360 .probe = driver_probe,
361 .disconnect = driver_disconnect,
364 static int claimintf(struct dev_state *ps, unsigned int ifnum)
366 struct usb_device *dev = ps->dev;
367 struct usb_interface *intf;
370 if (ifnum >= 8*sizeof(ps->ifclaimed))
372 /* already claimed */
373 if (test_bit(ifnum, &ps->ifclaimed))
376 /* lock against other changes to driver bindings */
377 down_write(&usb_bus_type.subsys.rwsem);
378 intf = usb_ifnum_to_if(dev, ifnum);
382 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
383 up_write(&usb_bus_type.subsys.rwsem);
385 set_bit(ifnum, &ps->ifclaimed);
389 static int releaseintf(struct dev_state *ps, unsigned int ifnum)
391 struct usb_device *dev;
392 struct usb_interface *intf;
396 if (ifnum >= 8*sizeof(ps->ifclaimed))
399 /* lock against other changes to driver bindings */
400 down_write(&usb_bus_type.subsys.rwsem);
401 intf = usb_ifnum_to_if(dev, ifnum);
404 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
405 usb_driver_release_interface(&usbfs_driver, intf);
408 up_write(&usb_bus_type.subsys.rwsem);
412 static int checkintf(struct dev_state *ps, unsigned int ifnum)
414 if (ps->dev->state != USB_STATE_CONFIGURED)
415 return -EHOSTUNREACH;
416 if (ifnum >= 8*sizeof(ps->ifclaimed))
418 if (test_bit(ifnum, &ps->ifclaimed))
420 /* if not yet claimed, claim it for the driver */
421 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim interface %u before use\n",
422 current->pid, current->comm, ifnum);
423 return claimintf(ps, ifnum);
426 static int findintfep(struct usb_device *dev, unsigned int ep)
428 unsigned int i, j, e;
429 struct usb_interface *intf;
430 struct usb_host_interface *alts;
431 struct usb_endpoint_descriptor *endpt;
433 if (ep & ~(USB_DIR_IN|0xf))
437 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
438 intf = dev->actconfig->interface[i];
439 for (j = 0; j < intf->num_altsetting; j++) {
440 alts = &intf->altsetting[j];
441 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
442 endpt = &alts->endpoint[e].desc;
443 if (endpt->bEndpointAddress == ep)
444 return alts->desc.bInterfaceNumber;
451 static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype, unsigned int index)
455 if (ps->dev->state != USB_STATE_CONFIGURED)
456 return -EHOSTUNREACH;
457 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
461 switch (requesttype & USB_RECIP_MASK) {
462 case USB_RECIP_ENDPOINT:
463 if ((ret = findintfep(ps->dev, index)) >= 0)
464 ret = checkintf(ps, ret);
467 case USB_RECIP_INTERFACE:
468 ret = checkintf(ps, index);
477 static int usbdev_open(struct inode *inode, struct file *file)
479 struct usb_device *dev;
480 struct dev_state *ps;
484 * no locking necessary here, as chrdev_open has the kernel lock
485 * (still acquire the kernel lock for safety)
488 if (!(ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL)))
493 dev = usb_get_dev(inode->u.generic_ip);
501 spin_lock_init(&ps->lock);
502 INIT_LIST_HEAD(&ps->async_pending);
503 INIT_LIST_HEAD(&ps->async_completed);
504 init_waitqueue_head(&ps->wait);
506 ps->disctask = current;
507 ps->disccontext = NULL;
510 list_add_tail(&ps->list, &dev->filelist);
511 file->private_data = ps;
518 static int usbdev_release(struct inode *inode, struct file *file)
520 struct dev_state *ps = (struct dev_state *)file->private_data;
521 struct usb_device *dev = ps->dev;
524 usb_lock_device(dev);
525 list_del_init(&ps->list);
526 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
528 if (test_bit(ifnum, &ps->ifclaimed))
529 releaseintf(ps, ifnum);
531 destroy_all_async(ps);
532 usb_unlock_device(dev);
539 static int proc_control(struct dev_state *ps, void __user *arg)
541 struct usb_device *dev = ps->dev;
542 struct usbdevfs_ctrltransfer ctrl;
547 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
549 if ((ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex)))
551 if (ctrl.wLength > PAGE_SIZE)
553 if (!(tbuf = (unsigned char *)__get_free_page(GFP_KERNEL)))
555 tmo = (ctrl.timeout * HZ + 999) / 1000;
556 if (ctrl.bRequestType & 0x80) {
557 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data, ctrl.wLength)) {
558 free_page((unsigned long)tbuf);
561 snoop(&dev->dev, "control read: bRequest=%02x bRrequestType=%02x wValue=%04x wIndex=%04x\n",
562 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue, ctrl.wIndex);
564 usb_unlock_device(dev);
565 i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest, ctrl.bRequestType,
566 ctrl.wValue, ctrl.wIndex, tbuf, ctrl.wLength, tmo);
567 usb_lock_device(dev);
568 if ((i > 0) && ctrl.wLength) {
570 dev_info(&dev->dev, "control read: data ");
571 for (j = 0; j < ctrl.wLength; ++j)
572 printk ("%02x ", (unsigned char)(tbuf)[j]);
575 if (copy_to_user(ctrl.data, tbuf, ctrl.wLength)) {
576 free_page((unsigned long)tbuf);
582 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
583 free_page((unsigned long)tbuf);
587 snoop(&dev->dev, "control write: bRequest=%02x bRrequestType=%02x wValue=%04x wIndex=%04x\n",
588 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue, ctrl.wIndex);
590 dev_info(&dev->dev, "control write: data: ");
591 for (j = 0; j < ctrl.wLength; ++j)
592 printk ("%02x ", (unsigned char)(tbuf)[j]);
595 usb_unlock_device(dev);
596 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest, ctrl.bRequestType,
597 ctrl.wValue, ctrl.wIndex, tbuf, ctrl.wLength, tmo);
598 usb_lock_device(dev);
600 free_page((unsigned long)tbuf);
601 if (i<0 && i != -EPIPE) {
602 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
603 "failed cmd %s rqt %u rq %u len %u ret %d\n",
604 current->comm, ctrl.bRequestType, ctrl.bRequest,
610 static int proc_bulk(struct dev_state *ps, void __user *arg)
612 struct usb_device *dev = ps->dev;
613 struct usbdevfs_bulktransfer bulk;
614 unsigned int tmo, len1, pipe;
619 if (copy_from_user(&bulk, arg, sizeof(bulk)))
621 if ((ret = findintfep(ps->dev, bulk.ep)) < 0)
623 if ((ret = checkintf(ps, ret)))
625 if (bulk.ep & USB_DIR_IN)
626 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
628 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
629 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
632 if (len1 > MAX_USBFS_BUFFER_SIZE)
634 if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
636 tmo = (bulk.timeout * HZ + 999) / 1000;
637 if (bulk.ep & 0x80) {
638 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
642 usb_unlock_device(dev);
643 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
644 usb_lock_device(dev);
646 if (copy_to_user(bulk.data, tbuf, len2)) {
653 if (copy_from_user(tbuf, bulk.data, len1)) {
658 usb_unlock_device(dev);
659 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
660 usb_lock_device(dev);
664 dev_warn(&dev->dev, "usbfs: USBDEVFS_BULK failed "
665 "ep 0x%x len %u ret %d\n", bulk.ep, bulk.len, i);
671 static int proc_resetep(struct dev_state *ps, void __user *arg)
676 if (get_user(ep, (unsigned int __user *)arg))
678 if ((ret = findintfep(ps->dev, ep)) < 0)
680 if ((ret = checkintf(ps, ret)))
682 usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0);
686 static int proc_clearhalt(struct dev_state *ps, void __user *arg)
692 if (get_user(ep, (unsigned int __user *)arg))
694 if ((ret = findintfep(ps->dev, ep)) < 0)
696 if ((ret = checkintf(ps, ret)))
699 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
701 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
703 return usb_clear_halt(ps->dev, pipe);
707 static int proc_getdriver(struct dev_state *ps, void __user *arg)
709 struct usbdevfs_getdriver gd;
710 struct usb_interface *intf;
713 if (copy_from_user(&gd, arg, sizeof(gd)))
715 down_read(&usb_bus_type.subsys.rwsem);
716 intf = usb_ifnum_to_if(ps->dev, gd.interface);
717 if (!intf || !intf->dev.driver)
720 strncpy(gd.driver, intf->dev.driver->name,
722 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
724 up_read(&usb_bus_type.subsys.rwsem);
728 static int proc_connectinfo(struct dev_state *ps, void __user *arg)
730 struct usbdevfs_connectinfo ci;
732 ci.devnum = ps->dev->devnum;
733 ci.slow = ps->dev->speed == USB_SPEED_LOW;
734 if (copy_to_user(arg, &ci, sizeof(ci)))
739 static int proc_resetdevice(struct dev_state *ps)
741 return usb_reset_device(ps->dev);
745 static int proc_setintf(struct dev_state *ps, void __user *arg)
747 struct usbdevfs_setinterface setintf;
750 if (copy_from_user(&setintf, arg, sizeof(setintf)))
752 if ((ret = checkintf(ps, setintf.interface)))
754 return usb_set_interface(ps->dev, setintf.interface,
758 static int proc_setconfig(struct dev_state *ps, void __user *arg)
762 struct usb_host_config *actconfig;
764 if (get_user(u, (unsigned int __user *)arg))
767 actconfig = ps->dev->actconfig;
769 /* Don't touch the device if any interfaces are claimed.
770 * It could interfere with other drivers' operations, and if
771 * an interface is claimed by usbfs it could easily deadlock.
776 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
777 if (usb_interface_claimed(actconfig->interface[i])) {
778 dev_warn (&ps->dev->dev,
779 "usbfs: interface %d claimed "
780 "while '%s' sets config #%d\n",
781 actconfig->interface[i]
783 ->desc.bInterfaceNumber,
785 #if 0 /* FIXME: enable in 2.6.10 or so */
793 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
794 * so avoid usb_set_configuration()'s kick to sysfs
797 if (actconfig && actconfig->desc.bConfigurationValue == u)
798 status = usb_reset_configuration(ps->dev);
800 status = usb_set_configuration(ps->dev, u);
806 static int proc_submiturb(struct dev_state *ps, void __user *arg)
808 struct usbdevfs_urb uurb;
809 struct usbdevfs_iso_packet_desc *isopkt = NULL;
810 struct usb_endpoint_descriptor *ep_desc;
812 struct usb_ctrlrequest *dr = NULL;
813 unsigned int u, totlen, isofrmlen;
814 int ret, interval = 0, ifnum = -1;
816 if (copy_from_user(&uurb, arg, sizeof(uurb)))
818 if (uurb.flags & ~(USBDEVFS_URB_ISO_ASAP|USBDEVFS_URB_SHORT_NOT_OK|
819 URB_NO_FSBR|URB_ZERO_PACKET))
823 if (uurb.signr != 0 && (uurb.signr < SIGRTMIN || uurb.signr > SIGRTMAX))
825 if (!(uurb.type == USBDEVFS_URB_TYPE_CONTROL && (uurb.endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
826 if ((ifnum = findintfep(ps->dev, uurb.endpoint)) < 0)
828 if ((ret = checkintf(ps, ifnum)))
832 case USBDEVFS_URB_TYPE_CONTROL:
833 if ((uurb.endpoint & ~USB_ENDPOINT_DIR_MASK) != 0) {
834 if (!(ep_desc = usb_epnum_to_ep_desc(ps->dev, uurb.endpoint)))
836 if ((ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_CONTROL)
839 /* min 8 byte setup packet, max arbitrary */
840 if (uurb.buffer_length < 8 || uurb.buffer_length > PAGE_SIZE)
842 if (!(dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL)))
844 if (copy_from_user(dr, uurb.buffer, 8)) {
848 if (uurb.buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
852 if ((ret = check_ctrlrecip(ps, dr->bRequestType, le16_to_cpup(&dr->wIndex)))) {
856 uurb.endpoint = (uurb.endpoint & ~USB_ENDPOINT_DIR_MASK) | (dr->bRequestType & USB_ENDPOINT_DIR_MASK);
857 uurb.number_of_packets = 0;
858 uurb.buffer_length = le16_to_cpup(&dr->wLength);
860 if (!access_ok((uurb.endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb.buffer, uurb.buffer_length)) {
866 case USBDEVFS_URB_TYPE_BULK:
867 uurb.number_of_packets = 0;
868 if (uurb.buffer_length > MAX_USBFS_BUFFER_SIZE)
870 if (!access_ok((uurb.endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb.buffer, uurb.buffer_length))
874 case USBDEVFS_URB_TYPE_ISO:
875 /* arbitrary limit */
876 if (uurb.number_of_packets < 1 || uurb.number_of_packets > 128)
878 if (!(ep_desc = usb_epnum_to_ep_desc(ps->dev, uurb.endpoint)))
880 interval = 1 << min (15, ep_desc->bInterval - 1);
881 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) * uurb.number_of_packets;
882 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
884 if (copy_from_user(isopkt, &((struct usbdevfs_urb __user *)arg)->iso_frame_desc, isofrmlen)) {
888 for (totlen = u = 0; u < uurb.number_of_packets; u++) {
889 if (isopkt[u].length > 1023) {
893 totlen += isopkt[u].length;
895 if (totlen > 32768) {
899 uurb.buffer_length = totlen;
902 case USBDEVFS_URB_TYPE_INTERRUPT:
903 uurb.number_of_packets = 0;
904 if (!(ep_desc = usb_epnum_to_ep_desc(ps->dev, uurb.endpoint)))
906 if (ps->dev->speed == USB_SPEED_HIGH)
907 interval = 1 << min (15, ep_desc->bInterval - 1);
909 interval = ep_desc->bInterval;
910 if (uurb.buffer_length > MAX_USBFS_BUFFER_SIZE)
912 if (!access_ok((uurb.endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb.buffer, uurb.buffer_length))
919 if (!(as = alloc_async(uurb.number_of_packets))) {
926 if (!(as->urb->transfer_buffer = kmalloc(uurb.buffer_length, GFP_KERNEL))) {
934 as->urb->dev = ps->dev;
935 as->urb->pipe = (uurb.type << 30) | __create_pipe(ps->dev, uurb.endpoint & 0xf) | (uurb.endpoint & USB_DIR_IN);
936 as->urb->transfer_flags = uurb.flags;
937 as->urb->transfer_buffer_length = uurb.buffer_length;
938 as->urb->setup_packet = (unsigned char*)dr;
939 as->urb->start_frame = uurb.start_frame;
940 as->urb->number_of_packets = uurb.number_of_packets;
941 as->urb->interval = interval;
942 as->urb->context = as;
943 as->urb->complete = async_completed;
944 for (totlen = u = 0; u < uurb.number_of_packets; u++) {
945 as->urb->iso_frame_desc[u].offset = totlen;
946 as->urb->iso_frame_desc[u].length = isopkt[u].length;
947 totlen += isopkt[u].length;
953 if (uurb.endpoint & USB_DIR_IN)
954 as->userbuffer = uurb.buffer;
956 as->userbuffer = NULL;
957 as->signr = uurb.signr;
960 if (!(uurb.endpoint & USB_DIR_IN)) {
961 if (copy_from_user(as->urb->transfer_buffer, uurb.buffer, as->urb->transfer_buffer_length)) {
966 async_newpending(as);
967 if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
968 dev_printk(KERN_DEBUG, &ps->dev->dev, "usbfs: usb_submit_urb returned %d\n", ret);
969 async_removepending(as);
976 static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
980 as = async_getpending(ps, arg);
983 usb_kill_urb(as->urb);
987 static int processcompl(struct async *as)
989 struct urb *urb = as->urb;
990 struct usbdevfs_urb __user *userurb = as->userurb;
994 if (copy_to_user(as->userbuffer, urb->transfer_buffer, urb->transfer_buffer_length))
996 if (put_user(urb->status, &userurb->status))
998 if (put_user(urb->actual_length, &userurb->actual_length))
1000 if (put_user(urb->error_count, &userurb->error_count))
1003 if (!(usb_pipeisoc(urb->pipe)))
1005 for (i = 0; i < urb->number_of_packets; i++) {
1006 if (put_user(urb->iso_frame_desc[i].actual_length,
1007 &userurb->iso_frame_desc[i].actual_length))
1009 if (put_user(urb->iso_frame_desc[i].status,
1010 &userurb->iso_frame_desc[i].status))
1016 static int proc_reapurb(struct dev_state *ps, void __user *arg)
1018 DECLARE_WAITQUEUE(wait, current);
1019 struct async *as = NULL;
1021 struct usb_device *dev = ps->dev;
1024 add_wait_queue(&ps->wait, &wait);
1026 __set_current_state(TASK_INTERRUPTIBLE);
1027 if ((as = async_getcompleted(ps)))
1029 if (signal_pending(current))
1031 usb_unlock_device(dev);
1033 usb_lock_device(dev);
1035 remove_wait_queue(&ps->wait, &wait);
1036 set_current_state(TASK_RUNNING);
1038 ret = processcompl(as);
1043 if (put_user(addr, (void __user * __user *)arg))
1047 if (signal_pending(current))
1052 static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
1058 if (!(as = async_getcompleted(ps)))
1060 ret = processcompl(as);
1065 if (put_user(addr, (void __user * __user *)arg))
1070 static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
1072 struct usbdevfs_disconnectsignal ds;
1074 if (copy_from_user(&ds, arg, sizeof(ds)))
1076 if (ds.signr != 0 && (ds.signr < SIGRTMIN || ds.signr > SIGRTMAX))
1078 ps->discsignr = ds.signr;
1079 ps->disccontext = ds.context;
1083 static int proc_claiminterface(struct dev_state *ps, void __user *arg)
1087 if (get_user(ifnum, (unsigned int __user *)arg))
1089 return claimintf(ps, ifnum);
1092 static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
1097 if (get_user(ifnum, (unsigned int __user *)arg))
1099 if ((ret = releaseintf(ps, ifnum)) < 0)
1101 destroy_async_on_interface (ps, ifnum);
1105 static int proc_ioctl (struct dev_state *ps, void __user *arg)
1107 struct usbdevfs_ioctl ctrl;
1111 struct usb_interface *intf = NULL;
1112 struct usb_driver *driver = NULL;
1114 /* get input parameters and alloc buffer */
1115 if (copy_from_user(&ctrl, arg, sizeof (ctrl)))
1117 if ((size = _IOC_SIZE (ctrl.ioctl_code)) > 0) {
1118 if ((buf = kmalloc (size, GFP_KERNEL)) == 0)
1120 if ((_IOC_DIR(ctrl.ioctl_code) & _IOC_WRITE)) {
1121 if (copy_from_user (buf, ctrl.data, size)) {
1126 memset (buf, 0, size);
1130 if (!connected(ps->dev)) {
1136 if (ps->dev->state != USB_STATE_CONFIGURED)
1137 retval = -EHOSTUNREACH;
1138 else if (!(intf = usb_ifnum_to_if (ps->dev, ctrl.ifno)))
1140 else switch (ctrl.ioctl_code) {
1142 /* disconnect kernel driver from interface */
1143 case USBDEVFS_DISCONNECT:
1144 down_write(&usb_bus_type.subsys.rwsem);
1145 if (intf->dev.driver) {
1146 driver = to_usb_driver(intf->dev.driver);
1147 dev_dbg (&intf->dev, "disconnect by usbfs\n");
1148 usb_driver_release_interface(driver, intf);
1151 up_write(&usb_bus_type.subsys.rwsem);
1154 /* let kernel drivers try to (re)bind to the interface */
1155 case USBDEVFS_CONNECT:
1156 usb_unlock_device(ps->dev);
1157 usb_lock_all_devices();
1158 bus_rescan_devices(intf->dev.bus);
1159 usb_unlock_all_devices();
1160 usb_lock_device(ps->dev);
1163 /* talk directly to the interface's driver */
1165 down_read(&usb_bus_type.subsys.rwsem);
1166 if (intf->dev.driver)
1167 driver = to_usb_driver(intf->dev.driver);
1168 if (driver == 0 || driver->ioctl == 0) {
1171 retval = driver->ioctl (intf, ctrl.ioctl_code, buf);
1172 if (retval == -ENOIOCTLCMD)
1175 up_read(&usb_bus_type.subsys.rwsem);
1178 /* cleanup and return */
1180 && (_IOC_DIR (ctrl.ioctl_code) & _IOC_READ) != 0
1182 && copy_to_user (ctrl.data, buf, size) != 0)
1190 * NOTE: All requests here that have interface numbers as parameters
1191 * are assuming that somehow the configuration has been prevented from
1192 * changing. But there's no mechanism to ensure that...
1194 static int usbdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1196 struct dev_state *ps = (struct dev_state *)file->private_data;
1197 struct usb_device *dev = ps->dev;
1198 void __user *p = (void __user *)arg;
1201 if (!(file->f_mode & FMODE_WRITE))
1203 usb_lock_device(dev);
1204 if (!connected(dev)) {
1205 usb_unlock_device(dev);
1210 case USBDEVFS_CONTROL:
1211 snoop(&dev->dev, "%s: CONTROL\n", __FUNCTION__);
1212 ret = proc_control(ps, p);
1214 inode->i_mtime = CURRENT_TIME;
1218 snoop(&dev->dev, "%s: BULK\n", __FUNCTION__);
1219 ret = proc_bulk(ps, p);
1221 inode->i_mtime = CURRENT_TIME;
1224 case USBDEVFS_RESETEP:
1225 snoop(&dev->dev, "%s: RESETEP\n", __FUNCTION__);
1226 ret = proc_resetep(ps, p);
1228 inode->i_mtime = CURRENT_TIME;
1231 case USBDEVFS_RESET:
1232 snoop(&dev->dev, "%s: RESET\n", __FUNCTION__);
1233 ret = proc_resetdevice(ps);
1236 case USBDEVFS_CLEAR_HALT:
1237 snoop(&dev->dev, "%s: CLEAR_HALT\n", __FUNCTION__);
1238 ret = proc_clearhalt(ps, p);
1240 inode->i_mtime = CURRENT_TIME;
1243 case USBDEVFS_GETDRIVER:
1244 snoop(&dev->dev, "%s: GETDRIVER\n", __FUNCTION__);
1245 ret = proc_getdriver(ps, p);
1248 case USBDEVFS_CONNECTINFO:
1249 snoop(&dev->dev, "%s: CONNECTINFO\n", __FUNCTION__);
1250 ret = proc_connectinfo(ps, p);
1253 case USBDEVFS_SETINTERFACE:
1254 snoop(&dev->dev, "%s: SETINTERFACE\n", __FUNCTION__);
1255 ret = proc_setintf(ps, p);
1258 case USBDEVFS_SETCONFIGURATION:
1259 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __FUNCTION__);
1260 ret = proc_setconfig(ps, p);
1263 case USBDEVFS_SUBMITURB:
1264 snoop(&dev->dev, "%s: SUBMITURB\n", __FUNCTION__);
1265 ret = proc_submiturb(ps, p);
1267 inode->i_mtime = CURRENT_TIME;
1270 case USBDEVFS_DISCARDURB:
1271 snoop(&dev->dev, "%s: DISCARDURB\n", __FUNCTION__);
1272 ret = proc_unlinkurb(ps, p);
1275 case USBDEVFS_REAPURB:
1276 snoop(&dev->dev, "%s: REAPURB\n", __FUNCTION__);
1277 ret = proc_reapurb(ps, p);
1280 case USBDEVFS_REAPURBNDELAY:
1281 snoop(&dev->dev, "%s: REAPURBDELAY\n", __FUNCTION__);
1282 ret = proc_reapurbnonblock(ps, p);
1285 case USBDEVFS_DISCSIGNAL:
1286 snoop(&dev->dev, "%s: DISCSIGNAL\n", __FUNCTION__);
1287 ret = proc_disconnectsignal(ps, p);
1290 case USBDEVFS_CLAIMINTERFACE:
1291 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __FUNCTION__);
1292 ret = proc_claiminterface(ps, p);
1295 case USBDEVFS_RELEASEINTERFACE:
1296 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __FUNCTION__);
1297 ret = proc_releaseinterface(ps, p);
1300 case USBDEVFS_IOCTL:
1301 snoop(&dev->dev, "%s: IOCTL\n", __FUNCTION__);
1302 ret = proc_ioctl(ps, p);
1305 usb_unlock_device(dev);
1307 inode->i_atime = CURRENT_TIME;
1311 /* No kernel lock - fine */
1312 static unsigned int usbdev_poll(struct file *file, struct poll_table_struct *wait)
1314 struct dev_state *ps = (struct dev_state *)file->private_data;
1315 unsigned int mask = 0;
1317 poll_wait(file, &ps->wait, wait);
1318 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
1319 mask |= POLLOUT | POLLWRNORM;
1320 if (!connected(ps->dev))
1321 mask |= POLLERR | POLLHUP;
1325 struct file_operations usbfs_device_file_operations = {
1326 .llseek = usbdev_lseek,
1327 .read = usbdev_read,
1328 .poll = usbdev_poll,
1329 .ioctl = usbdev_ioctl,
1330 .open = usbdev_open,
1331 .release = usbdev_release,