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.1.1.1 2005/04/11 02:50:40 jack 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/usb.h>
44 #include <linux/usbdevice_fs.h>
45 #include <asm/uaccess.h>
49 struct list_head asynclist;
51 struct task_struct *task;
59 static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
67 file->f_pos += offset;
78 static ssize_t usbdev_read(struct file *file, char * buf, size_t nbytes, loff_t *ppos)
80 struct dev_state *ps = (struct dev_state *)file->private_data;
87 down_read(&ps->devsem);
96 if (pos < sizeof(struct usb_device_descriptor)) {
97 len = sizeof(struct usb_device_descriptor) - pos;
100 if (copy_to_user(buf, ((char *)&ps->dev->descriptor) + pos, len)) {
111 last = sizeof(struct usb_device_descriptor);
112 for (i = 0; nbytes && i < ps->dev->descriptor.bNumConfigurations; i++) {
113 struct usb_config_descriptor *config =
114 (struct usb_config_descriptor *)ps->dev->rawdescriptors[i];
115 unsigned int length = le16_to_cpu(config->wTotalLength);
117 if (pos < last + length) {
118 len = length - (pos - last);
122 if (copy_to_user(buf,
123 ps->dev->rawdescriptors[i] + (pos - last), len)) {
139 up_read(&ps->devsem);
143 static inline unsigned int ld2(unsigned int x)
169 * async list handling
172 static struct async *alloc_async(unsigned int numisoframes)
174 unsigned int assize = sizeof(struct async) + numisoframes * sizeof(struct iso_packet_descriptor);
175 struct async *as = kmalloc(assize, GFP_KERNEL);
178 memset(as, 0, assize);
179 as->urb.number_of_packets = numisoframes;
180 spin_lock_init(&as->urb.lock);
184 static void free_async(struct async *as)
186 if (as->urb.transfer_buffer)
187 kfree(as->urb.transfer_buffer);
188 if (as->urb.setup_packet)
189 kfree(as->urb.setup_packet);
193 static inline void async_newpending(struct async *as)
195 struct dev_state *ps = as->ps;
198 spin_lock_irqsave(&ps->lock, flags);
199 list_add_tail(&as->asynclist, &ps->async_pending);
200 spin_unlock_irqrestore(&ps->lock, flags);
203 static inline void async_removepending(struct async *as)
205 struct dev_state *ps = as->ps;
208 spin_lock_irqsave(&ps->lock, flags);
209 list_del_init(&as->asynclist);
210 spin_unlock_irqrestore(&ps->lock, flags);
213 static inline struct async *async_getcompleted(struct dev_state *ps)
216 struct async *as = NULL;
218 spin_lock_irqsave(&ps->lock, flags);
219 if (!list_empty(&ps->async_completed)) {
220 as = list_entry(ps->async_completed.next, struct async, asynclist);
221 list_del_init(&as->asynclist);
223 spin_unlock_irqrestore(&ps->lock, flags);
227 static inline struct async *async_getpending(struct dev_state *ps, void *userurb)
232 spin_lock_irqsave(&ps->lock, flags);
233 list_for_each_entry(as, &ps->async_pending, asynclist)
234 if (as->userurb == userurb) {
235 list_del_init(&as->asynclist);
236 spin_unlock_irqrestore(&ps->lock, flags);
239 spin_unlock_irqrestore(&ps->lock, flags);
243 static void async_completed(struct urb *urb)
245 struct async *as = (struct async *)urb->context;
246 struct dev_state *ps = as->ps;
247 struct siginfo sinfo;
249 spin_lock(&ps->lock);
250 list_move_tail(&as->asynclist, &ps->async_completed);
251 spin_unlock(&ps->lock);
254 sinfo.si_signo = as->signr;
255 sinfo.si_errno = as->urb.status;
256 sinfo.si_code = SI_ASYNCIO;
257 sinfo.si_addr = as->userurb;
258 send_sig_info(as->signr, &sinfo, as->task);
262 static void destroy_async (struct dev_state *ps, struct list_head *list)
267 spin_lock_irqsave(&ps->lock, flags);
268 while (!list_empty(list)) {
269 as = list_entry(list->next, struct async, asynclist);
270 list_del_init(&as->asynclist);
271 spin_unlock_irqrestore(&ps->lock, flags);
272 /* usb_unlink_urb calls the completion handler with status == USB_ST_URB_KILLED */
273 usb_unlink_urb(&as->urb);
274 spin_lock_irqsave(&ps->lock, flags);
276 spin_unlock_irqrestore(&ps->lock, flags);
277 while ((as = async_getcompleted(ps)))
281 static void destroy_async_on_interface (struct dev_state *ps, unsigned int intf)
283 struct list_head *p, *q, hitlist;
286 INIT_LIST_HEAD(&hitlist);
287 spin_lock_irqsave(&ps->lock, flags);
288 list_for_each_safe(p, q, &ps->async_pending)
289 if (intf == list_entry(p, struct async, asynclist)->intf)
290 list_move_tail(p, &hitlist);
291 spin_unlock_irqrestore(&ps->lock, flags);
292 destroy_async(ps, &hitlist);
295 static inline void destroy_all_async(struct dev_state *ps)
297 destroy_async(ps, &ps->async_pending);
301 * interface claims are made only at the request of user level code,
302 * which can also release them (explicitly or by closing files).
303 * they're also undone when devices disconnect.
306 static void *driver_probe(struct usb_device *dev, unsigned int intf,
307 const struct usb_device_id *id)
312 static void driver_disconnect(struct usb_device *dev, void *context)
314 struct dev_state *ps = (struct dev_state *)context;
319 /* this waits till synchronous requests complete */
320 down_write (&ps->devsem);
322 /* prevent new I/O requests */
326 /* force async requests to complete */
327 destroy_all_async (ps);
329 up_write (&ps->devsem);
332 struct usb_driver usbdevfs_driver = {
335 disconnect: driver_disconnect,
338 static int claimintf(struct dev_state *ps, unsigned int intf)
340 struct usb_device *dev = ps->dev;
341 struct usb_interface *iface;
344 if (intf >= 8*sizeof(ps->ifclaimed) || !dev || intf >= dev->actconfig->bNumInterfaces)
346 /* already claimed */
347 if (test_bit(intf, &ps->ifclaimed))
349 iface = &dev->actconfig->interface[intf];
352 if (!usb_interface_claimed(iface)) {
353 usb_driver_claim_interface(&usbdevfs_driver, iface, ps);
354 set_bit(intf, &ps->ifclaimed);
361 static int releaseintf(struct dev_state *ps, unsigned int intf)
363 struct usb_device *dev;
364 struct usb_interface *iface;
367 if (intf >= 8*sizeof(ps->ifclaimed))
372 if (dev && test_and_clear_bit(intf, &ps->ifclaimed)) {
373 iface = &dev->actconfig->interface[intf];
374 usb_driver_release_interface(&usbdevfs_driver, iface);
381 static int checkintf(struct dev_state *ps, unsigned int intf)
383 if (intf >= 8*sizeof(ps->ifclaimed))
385 if (test_bit(intf, &ps->ifclaimed))
387 /* if not yet claimed, claim it for the driver */
388 printk(KERN_WARNING "usbdevfs: process %d (%s) did not claim interface %u before use\n",
389 current->pid, current->comm, intf);
390 return claimintf(ps, intf);
393 static int findintfep(struct usb_device *dev, unsigned int ep)
395 unsigned int i, j, e;
396 struct usb_interface *iface;
397 struct usb_interface_descriptor *alts;
398 struct usb_endpoint_descriptor *endpt;
400 if (ep & ~(USB_DIR_IN|0xf))
402 for (i = 0; i < dev->actconfig->bNumInterfaces; i++) {
403 iface = &dev->actconfig->interface[i];
404 for (j = 0; j < iface->num_altsetting; j++) {
405 alts = &iface->altsetting[j];
406 for (e = 0; e < alts->bNumEndpoints; e++) {
407 endpt = &alts->endpoint[e];
408 if (endpt->bEndpointAddress == ep)
416 static int findintfif(struct usb_device *dev, unsigned int ifn)
419 struct usb_interface *iface;
420 struct usb_interface_descriptor *alts;
424 for (i = 0; i < dev->actconfig->bNumInterfaces; i++) {
425 iface = &dev->actconfig->interface[i];
426 for (j = 0; j < iface->num_altsetting; j++) {
427 alts = &iface->altsetting[j];
428 if (alts->bInterfaceNumber == ifn)
435 extern struct list_head usb_driver_list;
438 static int finddriver(struct usb_driver **driver, char *name)
440 struct list_head *tmp;
442 tmp = usb_driver_list.next;
443 while (tmp != &usb_driver_list) {
444 struct usb_driver *d = list_entry(tmp, struct usb_driver,
447 if (!strcmp(d->name, name)) {
459 static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype, unsigned int index)
463 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
466 switch (requesttype & USB_RECIP_MASK) {
467 case USB_RECIP_ENDPOINT:
468 if ((ret = findintfep(ps->dev, index & 0xff)) < 0)
470 if ((ret = checkintf(ps, ret)))
474 case USB_RECIP_INTERFACE:
475 if ((ret = findintfif(ps->dev, index & 0xff)) < 0)
477 if ((ret = checkintf(ps, ret)))
487 static int usbdev_open(struct inode *inode, struct file *file)
489 struct usb_device *dev;
490 struct dev_state *ps;
494 * no locking necessary here, as both sys_open (actually filp_open)
495 * and the hub thread have the kernel lock
496 * (still acquire the kernel lock for safety)
500 if (ITYPE(inode->i_ino) != IDEVICE)
502 dev = inode->u.usbdev_i.p.dev;
506 if (!(ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL)))
511 spin_lock_init(&ps->lock);
512 INIT_LIST_HEAD(&ps->async_pending);
513 INIT_LIST_HEAD(&ps->async_completed);
514 init_waitqueue_head(&ps->wait);
515 init_rwsem(&ps->devsem);
517 ps->disctask = current;
518 ps->disccontext = NULL;
521 list_add_tail(&ps->list, &dev->filelist);
522 file->private_data = ps;
528 static int usbdev_release(struct inode *inode, struct file *file)
530 struct dev_state *ps = (struct dev_state *)file->private_data;
534 list_del_init(&ps->list);
536 for (i = 0; ps->ifclaimed && i < 8*sizeof(ps->ifclaimed); i++)
537 if (test_bit(i, &ps->ifclaimed))
541 destroy_all_async(ps);
546 static int proc_control(struct dev_state *ps, void *arg)
548 struct usb_device *dev = ps->dev;
549 struct usbdevfs_ctrltransfer ctrl;
554 if (copy_from_user(&ctrl, (void *)arg, sizeof(ctrl)))
556 if ((ret = check_ctrlrecip(ps, ctrl.requesttype, ctrl.index)))
558 if (ctrl.length > PAGE_SIZE)
560 if (!(tbuf = (unsigned char *)__get_free_page(GFP_KERNEL)))
562 tmo = (ctrl.timeout * HZ + 999) / 1000;
563 if (ctrl.requesttype & 0x80) {
564 if (ctrl.length && !access_ok(VERIFY_WRITE, ctrl.data, ctrl.length)) {
565 free_page((unsigned long)tbuf);
568 i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.request, ctrl.requesttype,
569 ctrl.value, ctrl.index, tbuf, ctrl.length, tmo);
570 if ((i > 0) && ctrl.length) {
571 if (copy_to_user(ctrl.data, tbuf, ctrl.length)) {
572 free_page((unsigned long)tbuf);
578 if (copy_from_user(tbuf, ctrl.data, ctrl.length)) {
579 free_page((unsigned long)tbuf);
583 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.request, ctrl.requesttype,
584 ctrl.value, ctrl.index, tbuf, ctrl.length, tmo);
586 free_page((unsigned long)tbuf);
588 printk(KERN_DEBUG "usbdevfs: USBDEVFS_CONTROL failed dev %d rqt %u rq %u len %u ret %d\n",
589 dev->devnum, ctrl.requesttype, ctrl.request, ctrl.length, i);
594 static int proc_bulk(struct dev_state *ps, void *arg)
596 struct usb_device *dev = ps->dev;
597 struct usbdevfs_bulktransfer bulk;
598 unsigned int tmo, len1, pipe;
603 if (copy_from_user(&bulk, (void *)arg, sizeof(bulk)))
605 if ((ret = findintfep(ps->dev, bulk.ep)) < 0)
607 if ((ret = checkintf(ps, ret)))
609 if (bulk.ep & USB_DIR_IN)
610 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
612 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
613 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
616 if (len1 > PAGE_SIZE)
618 if (!(tbuf = (unsigned char *)__get_free_page(GFP_KERNEL)))
620 tmo = (bulk.timeout * HZ + 999) / 1000;
621 if (bulk.ep & 0x80) {
622 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
623 free_page((unsigned long)tbuf);
626 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
628 if (copy_to_user(bulk.data, tbuf, len2)) {
629 free_page((unsigned long)tbuf);
635 if (copy_from_user(tbuf, bulk.data, len1)) {
636 free_page((unsigned long)tbuf);
640 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
642 free_page((unsigned long)tbuf);
644 printk(KERN_WARNING "usbdevfs: USBDEVFS_BULK failed dev %d ep 0x%x len %u ret %d\n",
645 dev->devnum, bulk.ep, bulk.len, i);
651 static int proc_resetep(struct dev_state *ps, void *arg)
656 if (get_user(ep, (unsigned int *)arg))
658 if ((ret = findintfep(ps->dev, ep)) < 0)
660 if ((ret = checkintf(ps, ret)))
662 usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0);
666 static int proc_clearhalt(struct dev_state *ps, void *arg)
672 if (get_user(ep, (unsigned int *)arg))
674 if ((ret = findintfep(ps->dev, ep)) < 0)
676 if ((ret = checkintf(ps, ret)))
679 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
681 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
683 return usb_clear_halt(ps->dev, pipe);
687 static int proc_getdriver(struct dev_state *ps, void *arg)
689 struct usbdevfs_getdriver gd;
690 struct usb_interface *interface;
693 if (copy_from_user(&gd, arg, sizeof(gd)))
695 if ((ret = findintfif(ps->dev, gd.interface)) < 0)
697 interface = usb_ifnum_to_if(ps->dev, gd.interface);
700 if (!interface->driver)
702 strcpy(gd.driver, interface->driver->name);
703 if (copy_to_user(arg, &gd, sizeof(gd)))
708 static int proc_connectinfo(struct dev_state *ps, void *arg)
710 struct usbdevfs_connectinfo ci;
712 ci.devnum = ps->dev->devnum;
713 ci.slow = ps->dev->speed == USB_SPEED_LOW;
714 if (copy_to_user(arg, &ci, sizeof(ci)))
719 static int proc_resetdevice(struct dev_state *ps)
723 ret = usb_reset_device(ps->dev);
727 for (i = 0; i < ps->dev->actconfig->bNumInterfaces; i++) {
728 struct usb_interface *intf = &ps->dev->actconfig->interface[i];
730 /* Don't simulate interfaces we've claimed */
731 if (test_bit(i, &ps->ifclaimed))
735 const struct usb_device_id *id;
736 down(&intf->driver->serialize);
737 intf->driver->disconnect(ps->dev, intf->private_data);
738 id = usb_match_id(ps->dev,intf,intf->driver->id_table);
739 intf->driver->probe(ps->dev, i, id);
740 up(&intf->driver->serialize);
747 static int proc_setintf(struct dev_state *ps, void *arg)
749 struct usbdevfs_setinterface setintf;
750 struct usb_interface *interface;
753 if (copy_from_user(&setintf, arg, sizeof(setintf)))
755 if ((ret = findintfif(ps->dev, setintf.interface)) < 0)
757 interface = usb_ifnum_to_if(ps->dev, setintf.interface);
760 if (interface->driver) {
761 if ((ret = checkintf(ps, ret)))
764 if (usb_set_interface(ps->dev, setintf.interface, setintf.altsetting))
769 static int proc_setconfig(struct dev_state *ps, void *arg)
773 if (get_user(u, (unsigned int *)arg))
775 if (usb_set_configuration(ps->dev, u) < 0)
780 static int proc_submiturb(struct dev_state *ps, void *arg)
782 struct usbdevfs_urb uurb;
783 struct usbdevfs_iso_packet_desc *isopkt = NULL;
784 struct usb_endpoint_descriptor *ep_desc;
786 struct usb_ctrlrequest *dr = NULL;
787 unsigned int u, totlen, isofrmlen;
791 if (copy_from_user(&uurb, arg, sizeof(uurb)))
793 if (uurb.flags & ~(USBDEVFS_URB_ISO_ASAP|USBDEVFS_URB_DISABLE_SPD|USBDEVFS_URB_QUEUE_BULK|
794 USB_NO_FSBR|USB_ZERO_PACKET))
798 if (uurb.signr != 0 && (uurb.signr < SIGRTMIN || uurb.signr > SIGRTMAX))
800 if (!(uurb.type == USBDEVFS_URB_TYPE_CONTROL && (uurb.endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
801 if ((intf = findintfep(ps->dev, uurb.endpoint)) < 0)
803 if ((ret = checkintf(ps, intf)))
807 case USBDEVFS_URB_TYPE_CONTROL:
808 if ((uurb.endpoint & ~USB_ENDPOINT_DIR_MASK) != 0) {
809 if (!(ep_desc = usb_epnum_to_ep_desc(ps->dev, uurb.endpoint)))
811 if ((ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_CONTROL)
814 /* min 8 byte setup packet, max arbitrary */
815 if (uurb.buffer_length < 8 || uurb.buffer_length > PAGE_SIZE)
817 if (!(dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL)))
819 if (copy_from_user(dr, (unsigned char*)uurb.buffer, 8)) {
823 if (uurb.buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
827 if ((ret = check_ctrlrecip(ps, dr->bRequestType, le16_to_cpup(&dr->wIndex)))) {
831 uurb.endpoint = (uurb.endpoint & ~USB_ENDPOINT_DIR_MASK) | (dr->bRequestType & USB_ENDPOINT_DIR_MASK);
832 uurb.number_of_packets = 0;
833 uurb.buffer_length = le16_to_cpup(&dr->wLength);
835 if (!access_ok((uurb.endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb.buffer, uurb.buffer_length)) {
841 case USBDEVFS_URB_TYPE_BULK:
842 uurb.number_of_packets = 0;
843 if (uurb.buffer_length > 16384)
845 if (!access_ok((uurb.endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb.buffer, uurb.buffer_length))
849 case USBDEVFS_URB_TYPE_ISO:
850 /* arbitrary limit */
851 if (uurb.number_of_packets < 1 || uurb.number_of_packets > 128)
853 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) * uurb.number_of_packets;
854 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
856 if (copy_from_user(isopkt, &((struct usbdevfs_urb *)arg)->iso_frame_desc, isofrmlen)) {
860 for (totlen = u = 0; u < uurb.number_of_packets; u++) {
861 if (isopkt[u].length > 1023) {
865 totlen += isopkt[u].length;
867 if (totlen > 32768) {
871 uurb.buffer_length = totlen;
874 case USBDEVFS_URB_TYPE_INTERRUPT:
875 uurb.number_of_packets = 0;
876 if (uurb.buffer_length > 16384)
878 if (!access_ok((uurb.endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb.buffer, uurb.buffer_length))
885 if (!(as = alloc_async(uurb.number_of_packets))) {
892 if (!(as->urb.transfer_buffer = kmalloc(uurb.buffer_length, GFP_KERNEL))) {
901 as->urb.dev = ps->dev;
902 as->urb.pipe = (uurb.type << 30) | __create_pipe(ps->dev, uurb.endpoint & 0xf) | (uurb.endpoint & USB_DIR_IN);
903 as->urb.transfer_flags = uurb.flags;
904 as->urb.transfer_buffer_length = uurb.buffer_length;
905 as->urb.setup_packet = (unsigned char*)dr;
906 as->urb.start_frame = uurb.start_frame;
907 as->urb.number_of_packets = uurb.number_of_packets;
908 as->urb.context = as;
909 as->urb.complete = async_completed;
910 for (totlen = u = 0; u < uurb.number_of_packets; u++) {
911 as->urb.iso_frame_desc[u].offset = totlen;
912 as->urb.iso_frame_desc[u].length = isopkt[u].length;
913 totlen += isopkt[u].length;
919 if (uurb.endpoint & USB_DIR_IN)
920 as->userbuffer = uurb.buffer;
922 as->userbuffer = NULL;
923 as->signr = uurb.signr;
926 if (!(uurb.endpoint & USB_DIR_IN)) {
927 if (copy_from_user(as->urb.transfer_buffer, uurb.buffer, as->urb.transfer_buffer_length)) {
932 async_newpending(as);
933 if ((ret = usb_submit_urb(&as->urb))) {
934 printk(KERN_DEBUG "usbdevfs: usb_submit_urb returned %d\n", ret);
935 async_removepending(as);
942 static int proc_unlinkurb(struct dev_state *ps, void *arg)
946 as = async_getpending(ps, arg);
949 usb_unlink_urb(&as->urb);
953 static int processcompl(struct async *as)
958 if (copy_to_user(as->userbuffer, as->urb.transfer_buffer, as->urb.transfer_buffer_length))
960 if (put_user(as->urb.status,
961 &((struct usbdevfs_urb *)as->userurb)->status))
963 if (put_user(as->urb.actual_length,
964 &((struct usbdevfs_urb *)as->userurb)->actual_length))
966 if (put_user(as->urb.error_count,
967 &((struct usbdevfs_urb *)as->userurb)->error_count))
970 if (!(usb_pipeisoc(as->urb.pipe)))
972 for (i = 0; i < as->urb.number_of_packets; i++) {
973 if (put_user(as->urb.iso_frame_desc[i].actual_length,
974 &((struct usbdevfs_urb *)as->userurb)->iso_frame_desc[i].actual_length))
976 if (put_user(as->urb.iso_frame_desc[i].status,
977 &((struct usbdevfs_urb *)as->userurb)->iso_frame_desc[i].status))
983 static int proc_reapurb(struct dev_state *ps, void *arg)
985 DECLARE_WAITQUEUE(wait, current);
986 struct async *as = NULL;
990 add_wait_queue(&ps->wait, &wait);
992 __set_current_state(TASK_INTERRUPTIBLE);
993 if ((as = async_getcompleted(ps)))
995 if (signal_pending(current))
997 up_read(&ps->devsem);
999 down_read(&ps->devsem);
1001 remove_wait_queue(&ps->wait, &wait);
1002 set_current_state(TASK_RUNNING);
1004 ret = processcompl(as);
1009 if (put_user(addr, (void **)arg))
1013 if (signal_pending(current))
1018 static int proc_reapurbnonblock(struct dev_state *ps, void *arg)
1024 if (!(as = async_getcompleted(ps)))
1026 ret = processcompl(as);
1031 if (put_user(addr, (void **)arg))
1036 static int proc_disconnectsignal(struct dev_state *ps, void *arg)
1038 struct usbdevfs_disconnectsignal ds;
1040 if (copy_from_user(&ds, arg, sizeof(ds)))
1042 if (ds.signr != 0 && (ds.signr < SIGRTMIN || ds.signr > SIGRTMAX))
1044 ps->discsignr = ds.signr;
1045 ps->disccontext = ds.context;
1049 static int proc_claiminterface(struct dev_state *ps, void *arg)
1054 if (get_user(intf, (unsigned int *)arg))
1056 if ((ret = findintfif(ps->dev, intf)) < 0)
1058 return claimintf(ps, ret);
1061 static int proc_releaseinterface(struct dev_state *ps, void *arg)
1066 if (get_user(intf, (unsigned int *)arg))
1068 if ((ret = findintfif(ps->dev, intf)) < 0)
1070 if ((ret = releaseintf(ps, intf)) < 0)
1072 destroy_async_on_interface (ps, intf);
1076 static int proc_ioctl (struct dev_state *ps, void *arg)
1078 struct usbdevfs_ioctl ctrl;
1082 struct usb_interface *ifp = 0;
1083 struct usb_driver *driver = 0;
1085 /* get input parameters and alloc buffer */
1086 if (copy_from_user(&ctrl, (void *) arg, sizeof (ctrl)))
1088 if ((size = _IOC_SIZE (ctrl.ioctl_code)) > 0) {
1089 if ((buf = kmalloc (size, GFP_KERNEL)) == 0)
1091 if ((_IOC_DIR(ctrl.ioctl_code) & _IOC_WRITE)) {
1092 if (copy_from_user (buf, ctrl.data, size)) {
1097 memset (buf, 0, size);
1103 else if (!(ifp = usb_ifnum_to_if (ps->dev, ctrl.ifno)))
1105 else switch (ctrl.ioctl_code) {
1107 /* disconnect kernel driver from interface, leaving it unbound. */
1108 case USBDEVFS_DISCONNECT:
1109 driver = ifp->driver;
1111 down (&driver->serialize);
1112 dbg ("disconnect '%s' from dev %d interface %d",
1113 driver->name, ps->dev->devnum, ctrl.ifno);
1114 driver->disconnect (ps->dev, ifp->private_data);
1115 usb_driver_release_interface (driver, ifp);
1116 up (&driver->serialize);
1121 /* let kernel drivers try to (re)bind to the interface */
1122 case USBDEVFS_CONNECT:
1123 usb_find_interface_driver_for_ifnum (ps->dev, ctrl.ifno);
1126 /* talk directly to the interface's driver */
1128 driver = ifp->driver;
1129 if (driver == 0 || driver->ioctl == 0)
1132 /* ifno might usefully be passed ... */
1133 retval = driver->ioctl (ps->dev, ctrl.ioctl_code, buf);
1134 /* size = min_t(int, size, retval)? */
1138 /* cleanup and return */
1140 && (_IOC_DIR (ctrl.ioctl_code) & _IOC_READ) != 0
1142 && copy_to_user (ctrl.data, buf, size) != 0)
1149 static int usbdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1151 struct dev_state *ps = (struct dev_state *)file->private_data;
1152 int ret = -ENOIOCTLCMD;
1154 if (!(file->f_mode & FMODE_WRITE))
1156 down_read(&ps->devsem);
1158 up_read(&ps->devsem);
1162 case USBDEVFS_CONTROL:
1163 ret = proc_control(ps, (void *)arg);
1165 inode->i_mtime = CURRENT_TIME;
1169 ret = proc_bulk(ps, (void *)arg);
1171 inode->i_mtime = CURRENT_TIME;
1174 case USBDEVFS_RESETEP:
1175 ret = proc_resetep(ps, (void *)arg);
1177 inode->i_mtime = CURRENT_TIME;
1180 case USBDEVFS_RESET:
1181 ret = proc_resetdevice(ps);
1184 case USBDEVFS_CLEAR_HALT:
1185 ret = proc_clearhalt(ps, (void *)arg);
1187 inode->i_mtime = CURRENT_TIME;
1190 case USBDEVFS_GETDRIVER:
1191 ret = proc_getdriver(ps, (void *)arg);
1194 case USBDEVFS_CONNECTINFO:
1195 ret = proc_connectinfo(ps, (void *)arg);
1198 case USBDEVFS_SETINTERFACE:
1199 ret = proc_setintf(ps, (void *)arg);
1202 case USBDEVFS_SETCONFIGURATION:
1203 ret = proc_setconfig(ps, (void *)arg);
1206 case USBDEVFS_SUBMITURB:
1207 ret = proc_submiturb(ps, (void *)arg);
1209 inode->i_mtime = CURRENT_TIME;
1212 case USBDEVFS_DISCARDURB:
1213 ret = proc_unlinkurb(ps, (void *)arg);
1216 case USBDEVFS_REAPURB:
1217 ret = proc_reapurb(ps, (void *)arg);
1220 case USBDEVFS_REAPURBNDELAY:
1221 ret = proc_reapurbnonblock(ps, (void *)arg);
1224 case USBDEVFS_DISCSIGNAL:
1225 ret = proc_disconnectsignal(ps, (void *)arg);
1228 case USBDEVFS_CLAIMINTERFACE:
1229 ret = proc_claiminterface(ps, (void *)arg);
1232 case USBDEVFS_RELEASEINTERFACE:
1233 ret = proc_releaseinterface(ps, (void *)arg);
1236 case USBDEVFS_IOCTL:
1237 ret = proc_ioctl(ps, (void *) arg);
1240 up_read(&ps->devsem);
1242 inode->i_atime = CURRENT_TIME;
1246 /* No kernel lock - fine */
1247 static unsigned int usbdev_poll(struct file *file, struct poll_table_struct *wait)
1249 struct dev_state *ps = (struct dev_state *)file->private_data;
1250 unsigned int mask = 0;
1252 poll_wait(file, &ps->wait, wait);
1253 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
1254 mask |= POLLOUT | POLLWRNORM;
1256 mask |= POLLERR | POLLHUP;
1260 struct file_operations usbdevfs_device_file_operations = {
1261 llseek: usbdev_lseek,
1264 ioctl: usbdev_ioctl,
1266 release: usbdev_release,