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/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 if (usb_excl_lock(dev, 1, 1) != 0) {
627 free_page((unsigned long)tbuf);
630 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
631 usb_excl_unlock(dev, 1);
633 if (copy_to_user(bulk.data, tbuf, len2)) {
634 free_page((unsigned long)tbuf);
640 if (copy_from_user(tbuf, bulk.data, len1)) {
641 free_page((unsigned long)tbuf);
645 if (usb_excl_lock(dev, 2, 1) != 0) {
646 free_page((unsigned long)tbuf);
649 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
650 usb_excl_unlock(dev, 2);
652 free_page((unsigned long)tbuf);
654 printk(KERN_WARNING "usbdevfs: USBDEVFS_BULK failed dev %d ep 0x%x len %u ret %d\n",
655 dev->devnum, bulk.ep, bulk.len, i);
661 static int proc_resetep(struct dev_state *ps, void *arg)
666 if (get_user(ep, (unsigned int *)arg))
668 if ((ret = findintfep(ps->dev, ep)) < 0)
670 if ((ret = checkintf(ps, ret)))
672 usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0);
676 static int proc_clearhalt(struct dev_state *ps, void *arg)
682 if (get_user(ep, (unsigned int *)arg))
684 if ((ret = findintfep(ps->dev, ep)) < 0)
686 if ((ret = checkintf(ps, ret)))
689 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
691 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
693 return usb_clear_halt(ps->dev, pipe);
697 static int proc_getdriver(struct dev_state *ps, void *arg)
699 struct usbdevfs_getdriver gd;
700 struct usb_interface *interface;
703 if (copy_from_user(&gd, arg, sizeof(gd)))
705 if ((ret = findintfif(ps->dev, gd.interface)) < 0)
707 interface = usb_ifnum_to_if(ps->dev, gd.interface);
710 if (!interface->driver)
712 strcpy(gd.driver, interface->driver->name);
713 if (copy_to_user(arg, &gd, sizeof(gd)))
718 static int proc_connectinfo(struct dev_state *ps, void *arg)
720 struct usbdevfs_connectinfo ci;
722 ci.devnum = ps->dev->devnum;
723 ci.slow = ps->dev->speed == USB_SPEED_LOW;
724 if (copy_to_user(arg, &ci, sizeof(ci)))
729 static int proc_resetdevice(struct dev_state *ps)
733 ret = usb_reset_device(ps->dev);
737 for (i = 0; i < ps->dev->actconfig->bNumInterfaces; i++) {
738 struct usb_interface *intf = &ps->dev->actconfig->interface[i];
740 /* Don't simulate interfaces we've claimed */
741 if (test_bit(i, &ps->ifclaimed))
745 const struct usb_device_id *id;
746 down(&intf->driver->serialize);
747 intf->driver->disconnect(ps->dev, intf->private_data);
748 id = usb_match_id(ps->dev,intf,intf->driver->id_table);
749 intf->driver->probe(ps->dev, i, id);
750 up(&intf->driver->serialize);
757 static int proc_setintf(struct dev_state *ps, void *arg)
759 struct usbdevfs_setinterface setintf;
760 struct usb_interface *interface;
763 if (copy_from_user(&setintf, arg, sizeof(setintf)))
765 if ((ret = findintfif(ps->dev, setintf.interface)) < 0)
767 interface = usb_ifnum_to_if(ps->dev, setintf.interface);
770 if (interface->driver) {
771 if ((ret = checkintf(ps, ret)))
774 if (usb_set_interface(ps->dev, setintf.interface, setintf.altsetting))
779 static int proc_setconfig(struct dev_state *ps, void *arg)
783 if (get_user(u, (unsigned int *)arg))
785 if (usb_set_configuration(ps->dev, u) < 0)
790 static int proc_submiturb(struct dev_state *ps, void *arg)
792 struct usbdevfs_urb uurb;
793 struct usbdevfs_iso_packet_desc *isopkt = NULL;
794 struct usb_endpoint_descriptor *ep_desc;
796 struct usb_ctrlrequest *dr = NULL;
797 unsigned int u, totlen, isofrmlen;
801 if (copy_from_user(&uurb, arg, sizeof(uurb)))
803 if (uurb.flags & ~(USBDEVFS_URB_ISO_ASAP|USBDEVFS_URB_DISABLE_SPD|USBDEVFS_URB_QUEUE_BULK|
804 USB_NO_FSBR|USB_ZERO_PACKET))
808 if (uurb.signr != 0 && (uurb.signr < SIGRTMIN || uurb.signr > SIGRTMAX))
810 if (!(uurb.type == USBDEVFS_URB_TYPE_CONTROL && (uurb.endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
811 if ((intf = findintfep(ps->dev, uurb.endpoint)) < 0)
813 if ((ret = checkintf(ps, intf)))
817 case USBDEVFS_URB_TYPE_CONTROL:
818 if ((uurb.endpoint & ~USB_ENDPOINT_DIR_MASK) != 0) {
819 if (!(ep_desc = usb_epnum_to_ep_desc(ps->dev, uurb.endpoint)))
821 if ((ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_CONTROL)
824 /* min 8 byte setup packet, max arbitrary */
825 if (uurb.buffer_length < 8 || uurb.buffer_length > PAGE_SIZE)
827 if (!(dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL)))
829 if (copy_from_user(dr, (unsigned char*)uurb.buffer, 8)) {
833 if (uurb.buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
837 if ((ret = check_ctrlrecip(ps, dr->bRequestType, le16_to_cpup(&dr->wIndex)))) {
841 uurb.endpoint = (uurb.endpoint & ~USB_ENDPOINT_DIR_MASK) | (dr->bRequestType & USB_ENDPOINT_DIR_MASK);
842 uurb.number_of_packets = 0;
843 uurb.buffer_length = le16_to_cpup(&dr->wLength);
845 if (!access_ok((uurb.endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb.buffer, uurb.buffer_length)) {
851 case USBDEVFS_URB_TYPE_BULK:
852 uurb.number_of_packets = 0;
853 if (uurb.buffer_length > 16384)
855 if (!access_ok((uurb.endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb.buffer, uurb.buffer_length))
859 case USBDEVFS_URB_TYPE_ISO:
860 /* arbitrary limit */
861 if (uurb.number_of_packets < 1 || uurb.number_of_packets > 128)
863 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) * uurb.number_of_packets;
864 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
866 if (copy_from_user(isopkt, &((struct usbdevfs_urb *)arg)->iso_frame_desc, isofrmlen)) {
870 for (totlen = u = 0; u < uurb.number_of_packets; u++) {
871 if (isopkt[u].length > 1023) {
875 totlen += isopkt[u].length;
877 if (totlen > 32768) {
881 uurb.buffer_length = totlen;
884 case USBDEVFS_URB_TYPE_INTERRUPT:
885 uurb.number_of_packets = 0;
886 if (uurb.buffer_length > 16384)
888 if (!access_ok((uurb.endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb.buffer, uurb.buffer_length))
895 if (!(as = alloc_async(uurb.number_of_packets))) {
902 if (!(as->urb.transfer_buffer = kmalloc(uurb.buffer_length, GFP_KERNEL))) {
911 as->urb.dev = ps->dev;
912 as->urb.pipe = (uurb.type << 30) | __create_pipe(ps->dev, uurb.endpoint & 0xf) | (uurb.endpoint & USB_DIR_IN);
913 as->urb.transfer_flags = uurb.flags;
914 as->urb.transfer_buffer_length = uurb.buffer_length;
915 as->urb.setup_packet = (unsigned char*)dr;
916 as->urb.start_frame = uurb.start_frame;
917 as->urb.number_of_packets = uurb.number_of_packets;
918 as->urb.context = as;
919 as->urb.complete = async_completed;
920 for (totlen = u = 0; u < uurb.number_of_packets; u++) {
921 as->urb.iso_frame_desc[u].offset = totlen;
922 as->urb.iso_frame_desc[u].length = isopkt[u].length;
923 totlen += isopkt[u].length;
929 if (uurb.endpoint & USB_DIR_IN)
930 as->userbuffer = uurb.buffer;
932 as->userbuffer = NULL;
933 as->signr = uurb.signr;
936 if (!(uurb.endpoint & USB_DIR_IN)) {
937 if (copy_from_user(as->urb.transfer_buffer, uurb.buffer, as->urb.transfer_buffer_length)) {
942 async_newpending(as);
943 if ((ret = usb_submit_urb(&as->urb))) {
944 printk(KERN_DEBUG "usbdevfs: usb_submit_urb returned %d\n", ret);
945 async_removepending(as);
952 static int proc_unlinkurb(struct dev_state *ps, void *arg)
956 as = async_getpending(ps, arg);
959 usb_unlink_urb(&as->urb);
963 static int processcompl(struct async *as)
968 if (copy_to_user(as->userbuffer, as->urb.transfer_buffer, as->urb.transfer_buffer_length))
970 if (put_user(as->urb.status,
971 &((struct usbdevfs_urb *)as->userurb)->status))
973 if (put_user(as->urb.actual_length,
974 &((struct usbdevfs_urb *)as->userurb)->actual_length))
976 if (put_user(as->urb.error_count,
977 &((struct usbdevfs_urb *)as->userurb)->error_count))
980 if (!(usb_pipeisoc(as->urb.pipe)))
982 for (i = 0; i < as->urb.number_of_packets; i++) {
983 if (put_user(as->urb.iso_frame_desc[i].actual_length,
984 &((struct usbdevfs_urb *)as->userurb)->iso_frame_desc[i].actual_length))
986 if (put_user(as->urb.iso_frame_desc[i].status,
987 &((struct usbdevfs_urb *)as->userurb)->iso_frame_desc[i].status))
993 static int proc_reapurb(struct dev_state *ps, void *arg)
995 DECLARE_WAITQUEUE(wait, current);
996 struct async *as = NULL;
1000 add_wait_queue(&ps->wait, &wait);
1002 __set_current_state(TASK_INTERRUPTIBLE);
1003 if ((as = async_getcompleted(ps)))
1005 if (signal_pending(current))
1007 up_read(&ps->devsem);
1009 down_read(&ps->devsem);
1011 remove_wait_queue(&ps->wait, &wait);
1012 set_current_state(TASK_RUNNING);
1014 ret = processcompl(as);
1019 if (put_user(addr, (void **)arg))
1023 if (signal_pending(current))
1028 static int proc_reapurbnonblock(struct dev_state *ps, void *arg)
1034 if (!(as = async_getcompleted(ps)))
1036 ret = processcompl(as);
1041 if (put_user(addr, (void **)arg))
1046 static int proc_disconnectsignal(struct dev_state *ps, void *arg)
1048 struct usbdevfs_disconnectsignal ds;
1050 if (copy_from_user(&ds, arg, sizeof(ds)))
1052 if (ds.signr != 0 && (ds.signr < SIGRTMIN || ds.signr > SIGRTMAX))
1054 ps->discsignr = ds.signr;
1055 ps->disccontext = ds.context;
1059 static int proc_claiminterface(struct dev_state *ps, void *arg)
1064 if (get_user(intf, (unsigned int *)arg))
1066 if ((ret = findintfif(ps->dev, intf)) < 0)
1068 return claimintf(ps, ret);
1071 static int proc_releaseinterface(struct dev_state *ps, void *arg)
1076 if (get_user(intf, (unsigned int *)arg))
1078 if ((ret = findintfif(ps->dev, intf)) < 0)
1080 if ((ret = releaseintf(ps, intf)) < 0)
1082 destroy_async_on_interface (ps, intf);
1086 static int proc_ioctl (struct dev_state *ps, void *arg)
1088 struct usbdevfs_ioctl ctrl;
1092 struct usb_interface *ifp = 0;
1093 struct usb_driver *driver = 0;
1095 /* get input parameters and alloc buffer */
1096 if (copy_from_user(&ctrl, (void *) arg, sizeof (ctrl)))
1098 if ((size = _IOC_SIZE (ctrl.ioctl_code)) > 0) {
1099 if ((buf = kmalloc (size, GFP_KERNEL)) == 0)
1101 if ((_IOC_DIR(ctrl.ioctl_code) & _IOC_WRITE)) {
1102 if (copy_from_user (buf, ctrl.data, size)) {
1107 memset (buf, 0, size);
1113 else if (!(ifp = usb_ifnum_to_if (ps->dev, ctrl.ifno)))
1115 else switch (ctrl.ioctl_code) {
1117 /* disconnect kernel driver from interface, leaving it unbound. */
1118 case USBDEVFS_DISCONNECT:
1119 driver = ifp->driver;
1121 down (&driver->serialize);
1122 dbg ("disconnect '%s' from dev %d interface %d",
1123 driver->name, ps->dev->devnum, ctrl.ifno);
1124 driver->disconnect (ps->dev, ifp->private_data);
1125 usb_driver_release_interface (driver, ifp);
1126 up (&driver->serialize);
1131 /* let kernel drivers try to (re)bind to the interface */
1132 case USBDEVFS_CONNECT:
1133 usb_find_interface_driver_for_ifnum (ps->dev, ctrl.ifno);
1136 /* talk directly to the interface's driver */
1138 driver = ifp->driver;
1139 if (driver == 0 || driver->ioctl == 0)
1142 /* ifno might usefully be passed ... */
1143 retval = driver->ioctl (ps->dev, ctrl.ioctl_code, buf);
1144 /* size = min_t(int, size, retval)? */
1145 if (retval == -ENOIOCTLCMD)
1150 /* cleanup and return */
1152 && (_IOC_DIR (ctrl.ioctl_code) & _IOC_READ) != 0
1154 && copy_to_user (ctrl.data, buf, size) != 0)
1161 static int usbdev_ioctl_exclusive(struct dev_state *ps, struct inode *inode,
1162 unsigned int cmd, unsigned long arg)
1167 case USBDEVFS_CONTROL:
1168 ret = proc_control(ps, (void *)arg);
1170 inode->i_mtime = CURRENT_TIME;
1173 case USBDEVFS_RESETEP:
1174 ret = proc_resetep(ps, (void *)arg);
1176 inode->i_mtime = CURRENT_TIME;
1179 case USBDEVFS_RESET:
1180 ret = proc_resetdevice(ps);
1183 case USBDEVFS_CLEAR_HALT:
1184 ret = proc_clearhalt(ps, (void *)arg);
1186 inode->i_mtime = CURRENT_TIME;
1189 case USBDEVFS_SETINTERFACE:
1190 ret = proc_setintf(ps, (void *)arg);
1193 case USBDEVFS_SETCONFIGURATION:
1194 ret = proc_setconfig(ps, (void *)arg);
1197 case USBDEVFS_SUBMITURB:
1198 ret = proc_submiturb(ps, (void *)arg);
1200 inode->i_mtime = CURRENT_TIME;
1203 case USBDEVFS_DISCARDURB:
1204 ret = proc_unlinkurb(ps, (void *)arg);
1207 case USBDEVFS_CLAIMINTERFACE:
1208 ret = proc_claiminterface(ps, (void *)arg);
1211 case USBDEVFS_RELEASEINTERFACE:
1212 ret = proc_releaseinterface(ps, (void *)arg);
1215 case USBDEVFS_IOCTL:
1216 ret = proc_ioctl(ps, (void *) arg);
1225 static int usbdev_ioctl(struct inode *inode, struct file *file,
1226 unsigned int cmd, unsigned long arg)
1228 struct dev_state *ps = file->private_data;
1231 if (!(file->f_mode & FMODE_WRITE))
1233 down_read(&ps->devsem);
1235 up_read(&ps->devsem);
1240 * Some ioctls don't touch the device and can be called without
1241 * grabbing its exclusive_access mutex; they are handled in this
1242 * switch. Other ioctls which need exclusive_access are handled in
1243 * usbdev_ioctl_exclusive().
1246 case USBDEVFS_GETDRIVER:
1247 ret = proc_getdriver(ps, (void *)arg);
1250 case USBDEVFS_CONNECTINFO:
1251 ret = proc_connectinfo(ps, (void *)arg);
1254 case USBDEVFS_REAPURB:
1255 ret = proc_reapurb(ps, (void *)arg);
1258 case USBDEVFS_REAPURBNDELAY:
1259 ret = proc_reapurbnonblock(ps, (void *)arg);
1262 case USBDEVFS_DISCSIGNAL:
1263 ret = proc_disconnectsignal(ps, (void *)arg);
1267 ret = proc_bulk(ps, (void *)arg);
1269 inode->i_mtime = CURRENT_TIME;
1272 case USBDEVFS_CONTROL:
1273 case USBDEVFS_RESETEP:
1274 case USBDEVFS_RESET:
1275 case USBDEVFS_CLEAR_HALT:
1276 case USBDEVFS_SETINTERFACE:
1277 case USBDEVFS_SETCONFIGURATION:
1278 case USBDEVFS_SUBMITURB:
1279 case USBDEVFS_DISCARDURB:
1280 case USBDEVFS_CLAIMINTERFACE:
1281 case USBDEVFS_RELEASEINTERFACE:
1282 case USBDEVFS_IOCTL:
1284 if (usb_excl_lock(ps->dev, 3, 1) == 0) {
1285 ret = usbdev_ioctl_exclusive(ps, inode, cmd, arg);
1286 usb_excl_unlock(ps->dev, 3);
1293 up_read(&ps->devsem);
1295 inode->i_atime = CURRENT_TIME;
1299 /* No kernel lock - fine */
1300 static unsigned int usbdev_poll(struct file *file, struct poll_table_struct *wait)
1302 struct dev_state *ps = (struct dev_state *)file->private_data;
1303 unsigned int mask = 0;
1305 poll_wait(file, &ps->wait, wait);
1306 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
1307 mask |= POLLOUT | POLLWRNORM;
1309 mask |= POLLERR | POLLHUP;
1313 struct file_operations usbdevfs_device_file_operations = {
1314 llseek: usbdev_lseek,
1317 ioctl: usbdev_ioctl,
1319 release: usbdev_release,