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 usbdevfs/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>
49 #include "hcd.h" /* for usbcore internals */
53 struct list_head asynclist;
55 struct task_struct *task;
58 void __user *userbuffer;
63 static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
75 file->f_pos += offset;
87 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
89 struct dev_state *ps = (struct dev_state *)file->private_data;
96 down_read(&ps->devsem);
100 } else if (pos < 0) {
105 if (pos < sizeof(struct usb_device_descriptor)) {
106 len = sizeof(struct usb_device_descriptor) - pos;
109 if (copy_to_user(buf, ((char *)&ps->dev->descriptor) + pos, len)) {
120 pos = sizeof(struct usb_device_descriptor);
121 for (i = 0; nbytes && i < ps->dev->descriptor.bNumConfigurations; i++) {
122 struct usb_config_descriptor *config =
123 (struct usb_config_descriptor *)ps->dev->rawdescriptors[i];
124 unsigned int length = le16_to_cpu(config->wTotalLength);
126 if (*ppos < pos + length) {
128 /* The descriptor may claim to be longer than it
129 * really is. Here is the actual allocated length. */
131 ps->dev->config[i].desc.wTotalLength;
133 len = length - (*ppos - pos);
137 /* Simply don't write (skip over) unallocated parts */
138 if (alloclen > (*ppos - pos)) {
139 alloclen -= (*ppos - pos);
140 if (copy_to_user(buf,
141 ps->dev->rawdescriptors[i] + (*ppos - pos),
142 min(len, alloclen))) {
158 up_read(&ps->devsem);
162 extern inline unsigned int ld2(unsigned int x)
188 * async list handling
191 static struct async *alloc_async(unsigned int numisoframes)
193 unsigned int assize = sizeof(struct async) + numisoframes * sizeof(struct usb_iso_packet_descriptor);
194 struct async *as = kmalloc(assize, GFP_KERNEL);
197 memset(as, 0, assize);
198 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
206 static void free_async(struct async *as)
208 if (as->urb->transfer_buffer)
209 kfree(as->urb->transfer_buffer);
210 if (as->urb->setup_packet)
211 kfree(as->urb->setup_packet);
212 usb_free_urb(as->urb);
216 extern __inline__ void async_newpending(struct async *as)
218 struct dev_state *ps = as->ps;
221 spin_lock_irqsave(&ps->lock, flags);
222 list_add_tail(&as->asynclist, &ps->async_pending);
223 spin_unlock_irqrestore(&ps->lock, flags);
226 extern __inline__ void async_removepending(struct async *as)
228 struct dev_state *ps = as->ps;
231 spin_lock_irqsave(&ps->lock, flags);
232 list_del_init(&as->asynclist);
233 spin_unlock_irqrestore(&ps->lock, flags);
236 extern __inline__ struct async *async_getcompleted(struct dev_state *ps)
239 struct async *as = NULL;
241 spin_lock_irqsave(&ps->lock, flags);
242 if (!list_empty(&ps->async_completed)) {
243 as = list_entry(ps->async_completed.next, struct async, asynclist);
244 list_del_init(&as->asynclist);
246 spin_unlock_irqrestore(&ps->lock, flags);
250 extern __inline__ struct async *async_getpending(struct dev_state *ps, void __user *userurb)
255 spin_lock_irqsave(&ps->lock, flags);
256 list_for_each_entry(as, &ps->async_pending, asynclist)
257 if (as->userurb == userurb) {
258 list_del_init(&as->asynclist);
259 spin_unlock_irqrestore(&ps->lock, flags);
262 spin_unlock_irqrestore(&ps->lock, flags);
266 static void async_completed(struct urb *urb, struct pt_regs *regs)
268 struct async *as = (struct async *)urb->context;
269 struct dev_state *ps = as->ps;
270 struct siginfo sinfo;
272 spin_lock(&ps->lock);
273 list_move_tail(&as->asynclist, &ps->async_completed);
274 spin_unlock(&ps->lock);
276 sinfo.si_signo = as->signr;
277 sinfo.si_errno = as->urb->status;
278 sinfo.si_code = SI_ASYNCIO;
279 sinfo.si_addr = (void *)as->userurb;
280 send_sig_info(as->signr, &sinfo, as->task);
285 static void destroy_async (struct dev_state *ps, struct list_head *list)
290 spin_lock_irqsave(&ps->lock, flags);
291 while (!list_empty(list)) {
292 as = list_entry(list->next, struct async, asynclist);
293 list_del_init(&as->asynclist);
294 spin_unlock_irqrestore(&ps->lock, flags);
295 /* usb_unlink_urb calls the completion handler with status == -ENOENT */
296 usb_unlink_urb(as->urb);
297 spin_lock_irqsave(&ps->lock, flags);
299 spin_unlock_irqrestore(&ps->lock, flags);
300 while ((as = async_getcompleted(ps)))
304 static void destroy_async_on_interface (struct dev_state *ps, unsigned int intf)
306 struct list_head *p, *q, hitlist;
309 INIT_LIST_HEAD(&hitlist);
310 spin_lock_irqsave(&ps->lock, flags);
311 list_for_each_safe(p, q, &ps->async_pending)
312 if (intf == list_entry(p, struct async, asynclist)->intf)
313 list_move_tail(p, &hitlist);
314 spin_unlock_irqrestore(&ps->lock, flags);
315 destroy_async(ps, &hitlist);
318 extern __inline__ void destroy_all_async(struct dev_state *ps)
320 destroy_async(ps, &ps->async_pending);
324 * interface claims are made only at the request of user level code,
325 * which can also release them (explicitly or by closing files).
326 * they're also undone when devices disconnect.
329 static int driver_probe (struct usb_interface *intf,
330 const struct usb_device_id *id)
335 static void driver_disconnect(struct usb_interface *intf)
337 struct dev_state *ps = usb_get_intfdata (intf);
342 /* NOTE: this relies on usbcore having canceled and completed
343 * all pending I/O requests; 2.6 does that.
346 /* prevent new I/O requests */
348 clear_bit(intf->cur_altsetting->desc.bInterfaceNumber, &ps->ifclaimed);
349 usb_set_intfdata (intf, NULL);
351 /* force async requests to complete */
352 destroy_all_async (ps);
355 struct usb_driver usbdevfs_driver = {
356 .owner = THIS_MODULE,
358 .probe = driver_probe,
359 .disconnect = driver_disconnect,
362 static int claimintf(struct dev_state *ps, unsigned int intf)
364 struct usb_device *dev = ps->dev;
365 struct usb_interface *iface;
368 if (intf >= 8*sizeof(ps->ifclaimed) || !dev
369 || intf >= dev->actconfig->desc.bNumInterfaces)
371 /* already claimed */
372 if (test_bit(intf, &ps->ifclaimed))
374 iface = dev->actconfig->interface[intf];
377 /* lock against other changes to driver bindings */
378 down_write(&usb_bus_type.subsys.rwsem);
379 if (!usb_interface_claimed(iface)) {
380 usb_driver_claim_interface(&usbdevfs_driver, iface, ps);
381 set_bit(intf, &ps->ifclaimed);
384 up_write(&usb_bus_type.subsys.rwsem);
388 static int releaseintf(struct dev_state *ps, unsigned int intf)
390 struct usb_device *dev;
391 struct usb_interface *iface;
394 if (intf >= 8*sizeof(ps->ifclaimed))
398 down(&dev->serialize);
399 /* lock against other changes to driver bindings */
400 down_write(&usb_bus_type.subsys.rwsem);
401 if (test_and_clear_bit(intf, &ps->ifclaimed)) {
402 iface = dev->actconfig->interface[intf];
403 usb_driver_release_interface(&usbdevfs_driver, iface);
406 up_write(&usb_bus_type.subsys.rwsem);
411 static int checkintf(struct dev_state *ps, unsigned int intf)
413 if (intf >= 8*sizeof(ps->ifclaimed))
415 if (test_bit(intf, &ps->ifclaimed))
417 /* if not yet claimed, claim it for the driver */
418 printk(KERN_WARNING "usbfs: process %d (%s) did not claim interface %u before use\n",
419 current->pid, current->comm, intf);
420 return claimintf(ps, intf);
423 static int findintfep(struct usb_device *dev, unsigned int ep)
425 unsigned int i, j, e;
426 struct usb_interface *iface;
427 struct usb_host_interface *alts;
428 struct usb_endpoint_descriptor *endpt;
430 if (ep & ~(USB_DIR_IN|0xf))
434 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
435 iface = dev->actconfig->interface[i];
436 for (j = 0; j < iface->num_altsetting; j++) {
437 alts = &iface->altsetting[j];
438 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
439 endpt = &alts->endpoint[e].desc;
440 if (endpt->bEndpointAddress == ep)
448 static int findintfif(struct usb_device *dev, unsigned int ifn)
456 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
457 if (dev->actconfig->interface[i]->
458 altsetting[0].desc.bInterfaceNumber == ifn)
464 static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype, unsigned int index)
468 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
471 switch (requesttype & USB_RECIP_MASK) {
472 case USB_RECIP_ENDPOINT:
473 if ((ret = findintfep(ps->dev, index & 0xff)) < 0)
475 if ((ret = checkintf(ps, ret)))
479 case USB_RECIP_INTERFACE:
480 if ((ret = findintfif(ps->dev, index & 0xff)) < 0)
482 if ((ret = checkintf(ps, ret)))
492 static int usbdev_open(struct inode *inode, struct file *file)
494 struct usb_device *dev;
495 struct dev_state *ps;
499 * no locking necessary here, as both sys_open (actually filp_open)
500 * and the hub thread have the kernel lock
501 * (still acquire the kernel lock for safety)
504 if (!(ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL)))
509 dev = inode->u.generic_ip;
517 spin_lock_init(&ps->lock);
518 INIT_LIST_HEAD(&ps->async_pending);
519 INIT_LIST_HEAD(&ps->async_completed);
520 init_waitqueue_head(&ps->wait);
521 init_rwsem(&ps->devsem);
523 ps->disctask = current;
524 ps->disccontext = NULL;
527 list_add_tail(&ps->list, &dev->filelist);
528 file->private_data = ps;
535 static int usbdev_release(struct inode *inode, struct file *file)
537 struct dev_state *ps = (struct dev_state *)file->private_data;
541 list_del_init(&ps->list);
544 for (i = 0; ps->ifclaimed && i < 8*sizeof(ps->ifclaimed); i++)
545 if (test_bit(i, &ps->ifclaimed))
549 destroy_all_async(ps);
554 static int proc_control(struct dev_state *ps, void __user *arg)
556 struct usb_device *dev = ps->dev;
557 struct usbdevfs_ctrltransfer ctrl;
562 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
564 if ((ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex)))
566 if (ctrl.wLength > PAGE_SIZE)
568 if (!(tbuf = (unsigned char *)__get_free_page(GFP_KERNEL)))
570 tmo = (ctrl.timeout * HZ + 999) / 1000;
571 if (ctrl.bRequestType & 0x80) {
572 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data, ctrl.wLength)) {
573 free_page((unsigned long)tbuf);
576 i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest, ctrl.bRequestType,
577 ctrl.wValue, ctrl.wIndex, tbuf, ctrl.wLength, tmo);
578 if ((i > 0) && ctrl.wLength) {
579 if (copy_to_user(ctrl.data, tbuf, ctrl.wLength)) {
580 free_page((unsigned long)tbuf);
586 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
587 free_page((unsigned long)tbuf);
591 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest, ctrl.bRequestType,
592 ctrl.wValue, ctrl.wIndex, tbuf, ctrl.wLength, tmo);
594 free_page((unsigned long)tbuf);
596 printk(KERN_DEBUG "usbfs: USBDEVFS_CONTROL failed "
597 "cmd %s dev %d rqt %u rq %u len %u ret %d\n",
599 dev->devnum, ctrl.bRequestType, ctrl.bRequest, ctrl.wLength, i);
604 static int proc_bulk(struct dev_state *ps, void __user *arg)
606 struct usb_device *dev = ps->dev;
607 struct usbdevfs_bulktransfer bulk;
608 unsigned int tmo, len1, pipe;
613 if (copy_from_user(&bulk, arg, sizeof(bulk)))
615 if ((ret = findintfep(ps->dev, bulk.ep)) < 0)
617 if ((ret = checkintf(ps, ret)))
619 if (bulk.ep & USB_DIR_IN)
620 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
622 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
623 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
626 if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
628 tmo = (bulk.timeout * HZ + 999) / 1000;
629 if (bulk.ep & 0x80) {
630 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
634 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
636 if (copy_to_user(bulk.data, tbuf, len2)) {
643 if (copy_from_user(tbuf, bulk.data, len1)) {
648 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
652 printk(KERN_WARNING "usbfs: USBDEVFS_BULK failed dev %d ep 0x%x len %u ret %d\n",
653 dev->devnum, bulk.ep, bulk.len, i);
659 static int proc_resetep(struct dev_state *ps, void __user *arg)
664 if (get_user(ep, (unsigned int __user *)arg))
666 if ((ret = findintfep(ps->dev, ep)) < 0)
668 if ((ret = checkintf(ps, ret)))
670 usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0);
674 static int proc_clearhalt(struct dev_state *ps, void __user *arg)
680 if (get_user(ep, (unsigned int __user *)arg))
682 if ((ret = findintfep(ps->dev, ep)) < 0)
684 if ((ret = checkintf(ps, ret)))
687 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
689 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
691 return usb_clear_halt(ps->dev, pipe);
695 static int proc_getdriver(struct dev_state *ps, void __user *arg)
697 struct usbdevfs_getdriver gd;
698 struct usb_interface *interface;
701 if (copy_from_user(&gd, arg, sizeof(gd)))
703 if ((ret = findintfif(ps->dev, gd.interface)) < 0)
705 interface = ps->dev->actconfig->interface[ret];
706 if (!interface->dev.driver)
708 strncpy(gd.driver, interface->dev.driver->name, sizeof(gd.driver));
709 if (copy_to_user(arg, &gd, sizeof(gd)))
714 static int proc_connectinfo(struct dev_state *ps, void __user *arg)
716 struct usbdevfs_connectinfo ci;
718 ci.devnum = ps->dev->devnum;
719 ci.slow = ps->dev->speed == USB_SPEED_LOW;
720 if (copy_to_user(arg, &ci, sizeof(ci)))
725 static int proc_resetdevice(struct dev_state *ps)
727 /* FIXME when usb_reset_device() is fixed we'll need to grab
728 * ps->dev->serialize before calling it.
730 return usb_reset_device(ps->dev);
734 static int proc_setintf(struct dev_state *ps, void __user *arg)
736 struct usbdevfs_setinterface setintf;
737 struct usb_interface *interface;
740 if (copy_from_user(&setintf, arg, sizeof(setintf)))
742 if ((ret = findintfif(ps->dev, setintf.interface)) < 0)
744 interface = ps->dev->actconfig->interface[ret];
745 if (interface->dev.driver) {
746 if ((ret = checkintf(ps, ret)))
749 if (usb_set_interface(ps->dev, setintf.interface, setintf.altsetting))
754 static int proc_setconfig(struct dev_state *ps, void __user *arg)
758 struct usb_host_config *actconfig;
760 if (get_user(u, (unsigned int __user *)arg))
763 down(&ps->dev->serialize);
764 actconfig = ps->dev->actconfig;
766 /* Don't touch the device if any interfaces are claimed.
767 * It could interfere with other drivers' operations, and if
768 * an interface is claimed by usbfs it could easily deadlock.
773 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
774 if (usb_interface_claimed(actconfig->interface[i])) {
775 dev_warn (&ps->dev->dev,
776 "usbfs: interface %d claimed "
777 "while '%s' sets config #%d\n",
778 actconfig->interface[i]
780 ->desc.bInterfaceNumber,
782 #if 0 /* FIXME: enable in 2.6.10 or so */
790 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
791 * so avoid usb_set_configuration()'s kick to sysfs
794 if (actconfig && actconfig->desc.bConfigurationValue == u)
795 status = usb_reset_configuration(ps->dev);
797 status = usb_set_configuration(ps->dev, u);
799 up(&ps->dev->serialize);
804 static int proc_submiturb(struct dev_state *ps, void __user *arg)
806 struct usbdevfs_urb uurb;
807 struct usbdevfs_iso_packet_desc *isopkt = NULL;
808 struct usb_endpoint_descriptor *ep_desc;
810 struct usb_ctrlrequest *dr = NULL;
811 unsigned int u, totlen, isofrmlen;
812 int ret, interval = 0, intf = -1;
814 if (copy_from_user(&uurb, arg, sizeof(uurb)))
816 if (uurb.flags & ~(USBDEVFS_URB_ISO_ASAP|USBDEVFS_URB_SHORT_NOT_OK|
817 URB_NO_FSBR|URB_ZERO_PACKET))
821 if (uurb.signr != 0 && (uurb.signr < SIGRTMIN || uurb.signr > SIGRTMAX))
823 if (!(uurb.type == USBDEVFS_URB_TYPE_CONTROL && (uurb.endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
824 if ((intf = findintfep(ps->dev, uurb.endpoint)) < 0)
826 if ((ret = checkintf(ps, intf)))
830 case USBDEVFS_URB_TYPE_CONTROL:
831 if ((uurb.endpoint & ~USB_ENDPOINT_DIR_MASK) != 0) {
832 if (!(ep_desc = usb_epnum_to_ep_desc(ps->dev, uurb.endpoint)))
834 if ((ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_CONTROL)
837 /* min 8 byte setup packet, max arbitrary */
838 if (uurb.buffer_length < 8 || uurb.buffer_length > PAGE_SIZE)
840 if (!(dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL)))
842 if (copy_from_user(dr, uurb.buffer, 8)) {
846 if (uurb.buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
850 if ((ret = check_ctrlrecip(ps, dr->bRequestType, le16_to_cpup(&dr->wIndex)))) {
854 uurb.endpoint = (uurb.endpoint & ~USB_ENDPOINT_DIR_MASK) | (dr->bRequestType & USB_ENDPOINT_DIR_MASK);
855 uurb.number_of_packets = 0;
856 uurb.buffer_length = le16_to_cpup(&dr->wLength);
858 if (!access_ok((uurb.endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb.buffer, uurb.buffer_length)) {
864 case USBDEVFS_URB_TYPE_BULK:
865 uurb.number_of_packets = 0;
866 if (uurb.buffer_length > 16384)
868 if (!access_ok((uurb.endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb.buffer, uurb.buffer_length))
872 case USBDEVFS_URB_TYPE_ISO:
873 /* arbitrary limit */
874 if (uurb.number_of_packets < 1 || uurb.number_of_packets > 128)
876 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) * uurb.number_of_packets;
877 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
879 if (copy_from_user(isopkt, &((struct usbdevfs_urb *)arg)->iso_frame_desc, isofrmlen)) {
883 for (totlen = u = 0; u < uurb.number_of_packets; u++) {
884 if (isopkt[u].length > 1023) {
888 totlen += isopkt[u].length;
890 if (totlen > 32768) {
894 uurb.buffer_length = totlen;
897 case USBDEVFS_URB_TYPE_INTERRUPT:
898 uurb.number_of_packets = 0;
899 if (!(ep_desc = usb_epnum_to_ep_desc(ps->dev, uurb.endpoint)))
901 interval = ep_desc->bInterval;
902 if (uurb.buffer_length > 16384)
904 if (!access_ok((uurb.endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb.buffer, uurb.buffer_length))
911 if (!(as = alloc_async(uurb.number_of_packets))) {
918 if (!(as->urb->transfer_buffer = kmalloc(uurb.buffer_length, GFP_KERNEL))) {
926 as->urb->dev = ps->dev;
927 as->urb->pipe = (uurb.type << 30) | __create_pipe(ps->dev, uurb.endpoint & 0xf) | (uurb.endpoint & USB_DIR_IN);
928 as->urb->transfer_flags = uurb.flags;
929 as->urb->transfer_buffer_length = uurb.buffer_length;
930 as->urb->setup_packet = (unsigned char*)dr;
931 as->urb->start_frame = uurb.start_frame;
932 as->urb->number_of_packets = uurb.number_of_packets;
933 as->urb->interval = interval;
934 as->urb->context = as;
935 as->urb->complete = async_completed;
936 for (totlen = u = 0; u < uurb.number_of_packets; u++) {
937 as->urb->iso_frame_desc[u].offset = totlen;
938 as->urb->iso_frame_desc[u].length = isopkt[u].length;
939 totlen += isopkt[u].length;
945 if (uurb.endpoint & USB_DIR_IN)
946 as->userbuffer = uurb.buffer;
948 as->userbuffer = NULL;
949 as->signr = uurb.signr;
952 if (!(uurb.endpoint & USB_DIR_IN)) {
953 if (copy_from_user(as->urb->transfer_buffer, uurb.buffer, as->urb->transfer_buffer_length)) {
958 async_newpending(as);
959 if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
960 printk(KERN_DEBUG "usbfs: usb_submit_urb returned %d\n", ret);
961 async_removepending(as);
968 static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
972 as = async_getpending(ps, arg);
975 usb_unlink_urb(as->urb);
979 static int processcompl(struct async *as)
981 struct urb *urb = as->urb;
985 if (copy_to_user(as->userbuffer, urb->transfer_buffer, urb->transfer_buffer_length))
987 if (put_user(urb->status,
988 &((struct usbdevfs_urb *)as->userurb)->status))
990 if (put_user(urb->actual_length,
991 &((struct usbdevfs_urb *)as->userurb)->actual_length))
993 if (put_user(urb->error_count,
994 &((struct usbdevfs_urb *)as->userurb)->error_count))
997 if (!(usb_pipeisoc(urb->pipe)))
999 for (i = 0; i < urb->number_of_packets; i++) {
1000 if (put_user(urb->iso_frame_desc[i].actual_length,
1001 &((struct usbdevfs_urb *)as->userurb)->iso_frame_desc[i].actual_length))
1003 if (put_user(urb->iso_frame_desc[i].status,
1004 &((struct usbdevfs_urb *)as->userurb)->iso_frame_desc[i].status))
1010 static int proc_reapurb(struct dev_state *ps, void __user *arg)
1012 DECLARE_WAITQUEUE(wait, current);
1013 struct async *as = NULL;
1017 add_wait_queue(&ps->wait, &wait);
1019 __set_current_state(TASK_INTERRUPTIBLE);
1020 if ((as = async_getcompleted(ps)))
1022 if (signal_pending(current))
1024 up_read(&ps->devsem);
1026 down_read(&ps->devsem);
1028 remove_wait_queue(&ps->wait, &wait);
1029 set_current_state(TASK_RUNNING);
1031 ret = processcompl(as);
1036 if (put_user(addr, (void **)arg))
1040 if (signal_pending(current))
1045 static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
1051 if (!(as = async_getcompleted(ps)))
1053 ret = processcompl(as);
1058 if (put_user(addr, (void **)arg))
1063 static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
1065 struct usbdevfs_disconnectsignal ds;
1067 if (copy_from_user(&ds, arg, sizeof(ds)))
1069 if (ds.signr != 0 && (ds.signr < SIGRTMIN || ds.signr > SIGRTMAX))
1071 ps->discsignr = ds.signr;
1072 ps->disccontext = ds.context;
1076 static int proc_claiminterface(struct dev_state *ps, void __user *arg)
1081 if (get_user(intf, (unsigned int __user *)arg))
1083 if ((ret = findintfif(ps->dev, intf)) < 0)
1085 return claimintf(ps, ret);
1088 static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
1093 if (get_user(intf, (unsigned int __user *)arg))
1095 if ((ret = findintfif(ps->dev, intf)) < 0)
1097 if ((ret = releaseintf(ps, intf)) < 0)
1099 destroy_async_on_interface (ps, intf);
1103 static int proc_ioctl (struct dev_state *ps, void __user *arg)
1105 struct usbdevfs_ioctl ctrl;
1109 struct usb_interface *ifp = 0;
1110 struct usb_driver *driver = 0;
1112 /* get input parameters and alloc buffer */
1113 if (copy_from_user(&ctrl, arg, sizeof (ctrl)))
1115 if ((size = _IOC_SIZE (ctrl.ioctl_code)) > 0) {
1116 if ((buf = kmalloc (size, GFP_KERNEL)) == 0)
1118 if ((_IOC_DIR(ctrl.ioctl_code) & _IOC_WRITE)) {
1119 if (copy_from_user (buf, ctrl.data, size)) {
1124 memset (buf, 0, size);
1134 down(&ps->dev->serialize);
1135 if (ps->dev->state != USB_STATE_CONFIGURED)
1137 else if (!(ifp = usb_ifnum_to_if (ps->dev, ctrl.ifno)))
1139 else switch (ctrl.ioctl_code) {
1141 /* disconnect kernel driver from interface */
1142 case USBDEVFS_DISCONNECT:
1143 down_write(&usb_bus_type.subsys.rwsem);
1144 if (ifp->dev.driver) {
1145 driver = to_usb_driver(ifp->dev.driver);
1146 dev_dbg (&ifp->dev, "disconnect by usbfs\n");
1147 usb_driver_release_interface(driver, ifp);
1150 up_write(&usb_bus_type.subsys.rwsem);
1153 /* let kernel drivers try to (re)bind to the interface */
1154 case USBDEVFS_CONNECT:
1155 bus_rescan_devices(ifp->dev.bus);
1158 /* talk directly to the interface's driver */
1160 down_read(&usb_bus_type.subsys.rwsem);
1161 if (ifp->dev.driver)
1162 driver = to_usb_driver(ifp->dev.driver);
1163 if (driver == 0 || driver->ioctl == 0) {
1166 retval = driver->ioctl (ifp, ctrl.ioctl_code, buf);
1167 if (retval == -ENOIOCTLCMD)
1170 up_read(&usb_bus_type.subsys.rwsem);
1172 up(&ps->dev->serialize);
1174 /* cleanup and return */
1176 && (_IOC_DIR (ctrl.ioctl_code) & _IOC_READ) != 0
1178 && copy_to_user (ctrl.data, buf, size) != 0)
1186 * NOTE: All requests here that have interface numbers as parameters
1187 * are assuming that somehow the configuration has been prevented from
1188 * changing. But there's no mechanism to ensure that...
1190 static int usbdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1192 struct dev_state *ps = (struct dev_state *)file->private_data;
1195 if (!(file->f_mode & FMODE_WRITE))
1197 down_read(&ps->devsem);
1199 up_read(&ps->devsem);
1203 case USBDEVFS_CONTROL:
1204 ret = proc_control(ps, (void __user *)arg);
1206 inode->i_mtime = CURRENT_TIME;
1210 ret = proc_bulk(ps, (void __user *)arg);
1212 inode->i_mtime = CURRENT_TIME;
1215 case USBDEVFS_RESETEP:
1216 ret = proc_resetep(ps, (void __user *)arg);
1218 inode->i_mtime = CURRENT_TIME;
1221 case USBDEVFS_RESET:
1222 ret = proc_resetdevice(ps);
1225 case USBDEVFS_CLEAR_HALT:
1226 ret = proc_clearhalt(ps, (void __user *)arg);
1228 inode->i_mtime = CURRENT_TIME;
1231 case USBDEVFS_GETDRIVER:
1232 ret = proc_getdriver(ps, (void __user *)arg);
1235 case USBDEVFS_CONNECTINFO:
1236 ret = proc_connectinfo(ps, (void __user *)arg);
1239 case USBDEVFS_SETINTERFACE:
1240 ret = proc_setintf(ps, (void __user *)arg);
1243 case USBDEVFS_SETCONFIGURATION:
1244 ret = proc_setconfig(ps, (void __user *)arg);
1247 case USBDEVFS_SUBMITURB:
1248 ret = proc_submiturb(ps, (void __user *)arg);
1250 inode->i_mtime = CURRENT_TIME;
1253 case USBDEVFS_DISCARDURB:
1254 ret = proc_unlinkurb(ps, (void __user *)arg);
1257 case USBDEVFS_REAPURB:
1258 ret = proc_reapurb(ps, (void __user *)arg);
1261 case USBDEVFS_REAPURBNDELAY:
1262 ret = proc_reapurbnonblock(ps, (void __user *)arg);
1265 case USBDEVFS_DISCSIGNAL:
1266 ret = proc_disconnectsignal(ps, (void __user *)arg);
1269 case USBDEVFS_CLAIMINTERFACE:
1270 ret = proc_claiminterface(ps, (void __user *)arg);
1273 case USBDEVFS_RELEASEINTERFACE:
1274 ret = proc_releaseinterface(ps, (void __user *)arg);
1277 case USBDEVFS_IOCTL:
1278 ret = proc_ioctl(ps, (void __user *) arg);
1281 up_read(&ps->devsem);
1283 inode->i_atime = CURRENT_TIME;
1287 /* No kernel lock - fine */
1288 static unsigned int usbdev_poll(struct file *file, struct poll_table_struct *wait)
1290 struct dev_state *ps = (struct dev_state *)file->private_data;
1291 unsigned int mask = 0;
1293 poll_wait(file, &ps->wait, wait);
1294 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
1295 mask |= POLLOUT | POLLWRNORM;
1297 mask |= POLLERR | POLLHUP;
1301 struct file_operations usbdevfs_device_file_operations = {
1302 .llseek = usbdev_lseek,
1303 .read = usbdev_read,
1304 .poll = usbdev_poll,
1305 .ioctl = usbdev_ioctl,
1306 .open = usbdev_open,
1307 .release = usbdev_release,