1 /*****************************************************************************/
4 * devio.c -- User space communication with USB devices.
6 * Copyright (C) 1999-2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * $Id: devio.c,v 1.7 2000/02/01 17:28:48 fliegl Exp $
24 * This file implements the usbfs/x/y files, where
25 * x is the bus number and y the device number.
27 * It allows user space programs/"drivers" to communicate directly
28 * with USB devices without intervening kernel driver.
31 * 22.12.1999 0.1 Initial release (split from proc_usb.c)
32 * 04.01.2000 0.2 Turned into its own filesystem
35 /*****************************************************************************/
39 #include <linux/slab.h>
40 #include <linux/smp_lock.h>
41 #include <linux/signal.h>
42 #include <linux/poll.h>
43 #include <linux/module.h>
44 #include <linux/usb.h>
45 #include <linux/usbdevice_fs.h>
46 #include <asm/uaccess.h>
47 #include <asm/byteorder.h>
48 #include <linux/moduleparam.h>
50 #include "hcd.h" /* for usbcore internals */
54 struct list_head asynclist
;
56 struct task_struct
*task
;
59 void __user
*userbuffer
;
64 static int usbfs_snoop
= 0;
65 module_param (usbfs_snoop
, bool, S_IRUGO
| S_IWUSR
);
66 MODULE_PARM_DESC (usbfs_snoop
, "true to log all usbfs traffic");
68 #define snoop(dev, format, arg...) \
71 dev_info( dev , format , ## arg); \
75 #define MAX_USBFS_BUFFER_SIZE 16384
77 static inline int connected (struct usb_device
*dev
)
79 return dev
->state
!= USB_STATE_NOTATTACHED
;
82 static loff_t
usbdev_lseek(struct file
*file
, loff_t offset
, int orig
)
94 file
->f_pos
+= offset
;
106 static ssize_t
usbdev_read(struct file
*file
, char __user
*buf
, size_t nbytes
, loff_t
*ppos
)
108 struct dev_state
*ps
= (struct dev_state
*)file
->private_data
;
109 struct usb_device
*dev
= ps
->dev
;
116 usb_lock_device(dev
);
117 if (!connected(dev
)) {
120 } else if (pos
< 0) {
125 if (pos
< sizeof(struct usb_device_descriptor
)) {
126 struct usb_device_descriptor
*desc
= kmalloc(sizeof(*desc
), GFP_KERNEL
);
131 memcpy(desc
, &dev
->descriptor
, sizeof(dev
->descriptor
));
132 le16_to_cpus(&desc
->bcdUSB
);
133 le16_to_cpus(&desc
->idVendor
);
134 le16_to_cpus(&desc
->idProduct
);
135 le16_to_cpus(&desc
->bcdDevice
);
137 len
= sizeof(struct usb_device_descriptor
) - pos
;
140 if (copy_to_user(buf
, ((char *)desc
) + pos
, len
)) {
153 pos
= sizeof(struct usb_device_descriptor
);
154 for (i
= 0; nbytes
&& i
< dev
->descriptor
.bNumConfigurations
; i
++) {
155 struct usb_config_descriptor
*config
=
156 (struct usb_config_descriptor
*)dev
->rawdescriptors
[i
];
157 unsigned int length
= le16_to_cpu(config
->wTotalLength
);
159 if (*ppos
< pos
+ length
) {
161 /* The descriptor may claim to be longer than it
162 * really is. Here is the actual allocated length. */
164 le16_to_cpu(dev
->config
[i
].desc
.wTotalLength
);
166 len
= length
- (*ppos
- pos
);
170 /* Simply don't write (skip over) unallocated parts */
171 if (alloclen
> (*ppos
- pos
)) {
172 alloclen
-= (*ppos
- pos
);
173 if (copy_to_user(buf
,
174 dev
->rawdescriptors
[i
] + (*ppos
- pos
),
175 min(len
, alloclen
))) {
191 usb_unlock_device(dev
);
196 * async list handling
199 static struct async
*alloc_async(unsigned int numisoframes
)
201 unsigned int assize
= sizeof(struct async
) + numisoframes
* sizeof(struct usb_iso_packet_descriptor
);
202 struct async
*as
= kmalloc(assize
, GFP_KERNEL
);
205 memset(as
, 0, assize
);
206 as
->urb
= usb_alloc_urb(numisoframes
, GFP_KERNEL
);
214 static void free_async(struct async
*as
)
216 kfree(as
->urb
->transfer_buffer
);
217 kfree(as
->urb
->setup_packet
);
218 usb_free_urb(as
->urb
);
222 static inline void async_newpending(struct async
*as
)
224 struct dev_state
*ps
= as
->ps
;
227 spin_lock_irqsave(&ps
->lock
, flags
);
228 list_add_tail(&as
->asynclist
, &ps
->async_pending
);
229 spin_unlock_irqrestore(&ps
->lock
, flags
);
232 static inline void async_removepending(struct async
*as
)
234 struct dev_state
*ps
= as
->ps
;
237 spin_lock_irqsave(&ps
->lock
, flags
);
238 list_del_init(&as
->asynclist
);
239 spin_unlock_irqrestore(&ps
->lock
, flags
);
242 static inline struct async
*async_getcompleted(struct dev_state
*ps
)
245 struct async
*as
= NULL
;
247 spin_lock_irqsave(&ps
->lock
, flags
);
248 if (!list_empty(&ps
->async_completed
)) {
249 as
= list_entry(ps
->async_completed
.next
, struct async
, asynclist
);
250 list_del_init(&as
->asynclist
);
252 spin_unlock_irqrestore(&ps
->lock
, flags
);
256 static inline struct async
*async_getpending(struct dev_state
*ps
, void __user
*userurb
)
261 spin_lock_irqsave(&ps
->lock
, flags
);
262 list_for_each_entry(as
, &ps
->async_pending
, asynclist
)
263 if (as
->userurb
== userurb
) {
264 list_del_init(&as
->asynclist
);
265 spin_unlock_irqrestore(&ps
->lock
, flags
);
268 spin_unlock_irqrestore(&ps
->lock
, flags
);
272 static void async_completed(struct urb
*urb
, struct pt_regs
*regs
)
274 struct async
*as
= (struct async
*)urb
->context
;
275 struct dev_state
*ps
= as
->ps
;
276 struct siginfo sinfo
;
278 spin_lock(&ps
->lock
);
279 list_move_tail(&as
->asynclist
, &ps
->async_completed
);
280 spin_unlock(&ps
->lock
);
282 sinfo
.si_signo
= as
->signr
;
283 sinfo
.si_errno
= as
->urb
->status
;
284 sinfo
.si_code
= SI_ASYNCIO
;
285 sinfo
.si_addr
= as
->userurb
;
286 send_sig_info(as
->signr
, &sinfo
, as
->task
);
291 static void destroy_async (struct dev_state
*ps
, struct list_head
*list
)
296 spin_lock_irqsave(&ps
->lock
, flags
);
297 while (!list_empty(list
)) {
298 as
= list_entry(list
->next
, struct async
, asynclist
);
299 list_del_init(&as
->asynclist
);
301 /* drop the spinlock so the completion handler can run */
302 spin_unlock_irqrestore(&ps
->lock
, flags
);
303 usb_kill_urb(as
->urb
);
304 spin_lock_irqsave(&ps
->lock
, flags
);
306 spin_unlock_irqrestore(&ps
->lock
, flags
);
307 as
= async_getcompleted(ps
);
310 as
= async_getcompleted(ps
);
314 static void destroy_async_on_interface (struct dev_state
*ps
, unsigned int ifnum
)
316 struct list_head
*p
, *q
, hitlist
;
319 INIT_LIST_HEAD(&hitlist
);
320 spin_lock_irqsave(&ps
->lock
, flags
);
321 list_for_each_safe(p
, q
, &ps
->async_pending
)
322 if (ifnum
== list_entry(p
, struct async
, asynclist
)->ifnum
)
323 list_move_tail(p
, &hitlist
);
324 spin_unlock_irqrestore(&ps
->lock
, flags
);
325 destroy_async(ps
, &hitlist
);
328 static inline void destroy_all_async(struct dev_state
*ps
)
330 destroy_async(ps
, &ps
->async_pending
);
334 * interface claims are made only at the request of user level code,
335 * which can also release them (explicitly or by closing files).
336 * they're also undone when devices disconnect.
339 static int driver_probe (struct usb_interface
*intf
,
340 const struct usb_device_id
*id
)
345 static void driver_disconnect(struct usb_interface
*intf
)
347 struct dev_state
*ps
= usb_get_intfdata (intf
);
348 unsigned int ifnum
= intf
->altsetting
->desc
.bInterfaceNumber
;
353 /* NOTE: this relies on usbcore having canceled and completed
354 * all pending I/O requests; 2.6 does that.
357 if (likely(ifnum
< 8*sizeof(ps
->ifclaimed
)))
358 clear_bit(ifnum
, &ps
->ifclaimed
);
360 warn("interface number %u out of range", ifnum
);
362 usb_set_intfdata (intf
, NULL
);
364 /* force async requests to complete */
365 destroy_async_on_interface(ps
, ifnum
);
368 struct usb_driver usbfs_driver
= {
369 .owner
= THIS_MODULE
,
371 .probe
= driver_probe
,
372 .disconnect
= driver_disconnect
,
375 static int claimintf(struct dev_state
*ps
, unsigned int ifnum
)
377 struct usb_device
*dev
= ps
->dev
;
378 struct usb_interface
*intf
;
381 if (ifnum
>= 8*sizeof(ps
->ifclaimed
))
383 /* already claimed */
384 if (test_bit(ifnum
, &ps
->ifclaimed
))
387 /* lock against other changes to driver bindings */
388 down_write(&usb_bus_type
.subsys
.rwsem
);
389 intf
= usb_ifnum_to_if(dev
, ifnum
);
393 err
= usb_driver_claim_interface(&usbfs_driver
, intf
, ps
);
394 up_write(&usb_bus_type
.subsys
.rwsem
);
396 set_bit(ifnum
, &ps
->ifclaimed
);
400 static int releaseintf(struct dev_state
*ps
, unsigned int ifnum
)
402 struct usb_device
*dev
;
403 struct usb_interface
*intf
;
407 if (ifnum
>= 8*sizeof(ps
->ifclaimed
))
410 /* lock against other changes to driver bindings */
411 down_write(&usb_bus_type
.subsys
.rwsem
);
412 intf
= usb_ifnum_to_if(dev
, ifnum
);
415 else if (test_and_clear_bit(ifnum
, &ps
->ifclaimed
)) {
416 usb_driver_release_interface(&usbfs_driver
, intf
);
419 up_write(&usb_bus_type
.subsys
.rwsem
);
423 static int checkintf(struct dev_state
*ps
, unsigned int ifnum
)
425 if (ps
->dev
->state
!= USB_STATE_CONFIGURED
)
426 return -EHOSTUNREACH
;
427 if (ifnum
>= 8*sizeof(ps
->ifclaimed
))
429 if (test_bit(ifnum
, &ps
->ifclaimed
))
431 /* if not yet claimed, claim it for the driver */
432 dev_warn(&ps
->dev
->dev
, "usbfs: process %d (%s) did not claim interface %u before use\n",
433 current
->pid
, current
->comm
, ifnum
);
434 return claimintf(ps
, ifnum
);
437 static int findintfep(struct usb_device
*dev
, unsigned int ep
)
439 unsigned int i
, j
, e
;
440 struct usb_interface
*intf
;
441 struct usb_host_interface
*alts
;
442 struct usb_endpoint_descriptor
*endpt
;
444 if (ep
& ~(USB_DIR_IN
|0xf))
448 for (i
= 0; i
< dev
->actconfig
->desc
.bNumInterfaces
; i
++) {
449 intf
= dev
->actconfig
->interface
[i
];
450 for (j
= 0; j
< intf
->num_altsetting
; j
++) {
451 alts
= &intf
->altsetting
[j
];
452 for (e
= 0; e
< alts
->desc
.bNumEndpoints
; e
++) {
453 endpt
= &alts
->endpoint
[e
].desc
;
454 if (endpt
->bEndpointAddress
== ep
)
455 return alts
->desc
.bInterfaceNumber
;
462 static int check_ctrlrecip(struct dev_state
*ps
, unsigned int requesttype
, unsigned int index
)
466 if (ps
->dev
->state
!= USB_STATE_CONFIGURED
)
467 return -EHOSTUNREACH
;
468 if (USB_TYPE_VENDOR
== (USB_TYPE_MASK
& requesttype
))
472 switch (requesttype
& USB_RECIP_MASK
) {
473 case USB_RECIP_ENDPOINT
:
474 if ((ret
= findintfep(ps
->dev
, index
)) >= 0)
475 ret
= checkintf(ps
, ret
);
478 case USB_RECIP_INTERFACE
:
479 ret
= checkintf(ps
, index
);
488 static int usbdev_open(struct inode
*inode
, struct file
*file
)
490 struct usb_device
*dev
;
491 struct dev_state
*ps
;
495 * no locking necessary here, as chrdev_open has the kernel lock
496 * (still acquire the kernel lock for safety)
499 if (!(ps
= kmalloc(sizeof(struct dev_state
), GFP_KERNEL
)))
504 dev
= usb_get_dev(inode
->u
.generic_ip
);
512 spin_lock_init(&ps
->lock
);
513 INIT_LIST_HEAD(&ps
->async_pending
);
514 INIT_LIST_HEAD(&ps
->async_completed
);
515 init_waitqueue_head(&ps
->wait
);
517 ps
->disctask
= current
;
518 ps
->disccontext
= NULL
;
521 list_add_tail(&ps
->list
, &dev
->filelist
);
522 file
->private_data
= ps
;
529 static int usbdev_release(struct inode
*inode
, struct file
*file
)
531 struct dev_state
*ps
= (struct dev_state
*)file
->private_data
;
532 struct usb_device
*dev
= ps
->dev
;
535 usb_lock_device(dev
);
536 list_del_init(&ps
->list
);
537 for (ifnum
= 0; ps
->ifclaimed
&& ifnum
< 8*sizeof(ps
->ifclaimed
);
539 if (test_bit(ifnum
, &ps
->ifclaimed
))
540 releaseintf(ps
, ifnum
);
542 destroy_all_async(ps
);
543 usb_unlock_device(dev
);
550 static int proc_control(struct dev_state
*ps
, void __user
*arg
)
552 struct usb_device
*dev
= ps
->dev
;
553 struct usbdevfs_ctrltransfer ctrl
;
558 if (copy_from_user(&ctrl
, arg
, sizeof(ctrl
)))
560 if ((ret
= check_ctrlrecip(ps
, ctrl
.bRequestType
, ctrl
.wIndex
)))
562 if (ctrl
.wLength
> PAGE_SIZE
)
564 if (!(tbuf
= (unsigned char *)__get_free_page(GFP_KERNEL
)))
567 if (ctrl
.bRequestType
& 0x80) {
568 if (ctrl
.wLength
&& !access_ok(VERIFY_WRITE
, ctrl
.data
, ctrl
.wLength
)) {
569 free_page((unsigned long)tbuf
);
572 snoop(&dev
->dev
, "control read: bRequest=%02x "
573 "bRrequestType=%02x wValue=%04x "
574 "wIndex=%04x wLength=%04x\n",
575 ctrl
.bRequest
, ctrl
.bRequestType
, ctrl
.wValue
,
576 ctrl
.wIndex
, ctrl
.wLength
);
578 usb_unlock_device(dev
);
579 i
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), ctrl
.bRequest
, ctrl
.bRequestType
,
580 ctrl
.wValue
, ctrl
.wIndex
, tbuf
, ctrl
.wLength
, tmo
);
581 usb_lock_device(dev
);
582 if ((i
> 0) && ctrl
.wLength
) {
584 dev_info(&dev
->dev
, "control read: data ");
585 for (j
= 0; j
< i
; ++j
)
586 printk ("%02x ", (unsigned char)(tbuf
)[j
]);
589 if (copy_to_user(ctrl
.data
, tbuf
, i
)) {
590 free_page((unsigned long)tbuf
);
596 if (copy_from_user(tbuf
, ctrl
.data
, ctrl
.wLength
)) {
597 free_page((unsigned long)tbuf
);
601 snoop(&dev
->dev
, "control write: bRequest=%02x "
602 "bRrequestType=%02x wValue=%04x "
603 "wIndex=%04x wLength=%04x\n",
604 ctrl
.bRequest
, ctrl
.bRequestType
, ctrl
.wValue
,
605 ctrl
.wIndex
, ctrl
.wLength
);
607 dev_info(&dev
->dev
, "control write: data: ");
608 for (j
= 0; j
< ctrl
.wLength
; ++j
)
609 printk ("%02x ", (unsigned char)(tbuf
)[j
]);
612 usb_unlock_device(dev
);
613 i
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), ctrl
.bRequest
, ctrl
.bRequestType
,
614 ctrl
.wValue
, ctrl
.wIndex
, tbuf
, ctrl
.wLength
, tmo
);
615 usb_lock_device(dev
);
617 free_page((unsigned long)tbuf
);
618 if (i
<0 && i
!= -EPIPE
) {
619 dev_printk(KERN_DEBUG
, &dev
->dev
, "usbfs: USBDEVFS_CONTROL "
620 "failed cmd %s rqt %u rq %u len %u ret %d\n",
621 current
->comm
, ctrl
.bRequestType
, ctrl
.bRequest
,
627 static int proc_bulk(struct dev_state
*ps
, void __user
*arg
)
629 struct usb_device
*dev
= ps
->dev
;
630 struct usbdevfs_bulktransfer bulk
;
631 unsigned int tmo
, len1
, pipe
;
636 if (copy_from_user(&bulk
, arg
, sizeof(bulk
)))
638 if ((ret
= findintfep(ps
->dev
, bulk
.ep
)) < 0)
640 if ((ret
= checkintf(ps
, ret
)))
642 if (bulk
.ep
& USB_DIR_IN
)
643 pipe
= usb_rcvbulkpipe(dev
, bulk
.ep
& 0x7f);
645 pipe
= usb_sndbulkpipe(dev
, bulk
.ep
& 0x7f);
646 if (!usb_maxpacket(dev
, pipe
, !(bulk
.ep
& USB_DIR_IN
)))
649 if (len1
> MAX_USBFS_BUFFER_SIZE
)
651 if (!(tbuf
= kmalloc(len1
, GFP_KERNEL
)))
654 if (bulk
.ep
& 0x80) {
655 if (len1
&& !access_ok(VERIFY_WRITE
, bulk
.data
, len1
)) {
659 usb_unlock_device(dev
);
660 i
= usb_bulk_msg(dev
, pipe
, tbuf
, len1
, &len2
, tmo
);
661 usb_lock_device(dev
);
663 if (copy_to_user(bulk
.data
, tbuf
, len2
)) {
670 if (copy_from_user(tbuf
, bulk
.data
, len1
)) {
675 usb_unlock_device(dev
);
676 i
= usb_bulk_msg(dev
, pipe
, tbuf
, len1
, &len2
, tmo
);
677 usb_lock_device(dev
);
685 static int proc_resetep(struct dev_state
*ps
, void __user
*arg
)
690 if (get_user(ep
, (unsigned int __user
*)arg
))
692 if ((ret
= findintfep(ps
->dev
, ep
)) < 0)
694 if ((ret
= checkintf(ps
, ret
)))
696 usb_settoggle(ps
->dev
, ep
& 0xf, !(ep
& USB_DIR_IN
), 0);
700 static int proc_clearhalt(struct dev_state
*ps
, void __user
*arg
)
706 if (get_user(ep
, (unsigned int __user
*)arg
))
708 if ((ret
= findintfep(ps
->dev
, ep
)) < 0)
710 if ((ret
= checkintf(ps
, ret
)))
713 pipe
= usb_rcvbulkpipe(ps
->dev
, ep
& 0x7f);
715 pipe
= usb_sndbulkpipe(ps
->dev
, ep
& 0x7f);
717 return usb_clear_halt(ps
->dev
, pipe
);
721 static int proc_getdriver(struct dev_state
*ps
, void __user
*arg
)
723 struct usbdevfs_getdriver gd
;
724 struct usb_interface
*intf
;
727 if (copy_from_user(&gd
, arg
, sizeof(gd
)))
729 down_read(&usb_bus_type
.subsys
.rwsem
);
730 intf
= usb_ifnum_to_if(ps
->dev
, gd
.interface
);
731 if (!intf
|| !intf
->dev
.driver
)
734 strncpy(gd
.driver
, intf
->dev
.driver
->name
,
736 ret
= (copy_to_user(arg
, &gd
, sizeof(gd
)) ? -EFAULT
: 0);
738 up_read(&usb_bus_type
.subsys
.rwsem
);
742 static int proc_connectinfo(struct dev_state
*ps
, void __user
*arg
)
744 struct usbdevfs_connectinfo ci
;
746 ci
.devnum
= ps
->dev
->devnum
;
747 ci
.slow
= ps
->dev
->speed
== USB_SPEED_LOW
;
748 if (copy_to_user(arg
, &ci
, sizeof(ci
)))
753 static int proc_resetdevice(struct dev_state
*ps
)
755 return usb_reset_device(ps
->dev
);
759 static int proc_setintf(struct dev_state
*ps
, void __user
*arg
)
761 struct usbdevfs_setinterface setintf
;
764 if (copy_from_user(&setintf
, arg
, sizeof(setintf
)))
766 if ((ret
= checkintf(ps
, setintf
.interface
)))
768 return usb_set_interface(ps
->dev
, setintf
.interface
,
772 static int proc_setconfig(struct dev_state
*ps
, void __user
*arg
)
776 struct usb_host_config
*actconfig
;
778 if (get_user(u
, (unsigned int __user
*)arg
))
781 actconfig
= ps
->dev
->actconfig
;
783 /* Don't touch the device if any interfaces are claimed.
784 * It could interfere with other drivers' operations, and if
785 * an interface is claimed by usbfs it could easily deadlock.
790 for (i
= 0; i
< actconfig
->desc
.bNumInterfaces
; ++i
) {
791 if (usb_interface_claimed(actconfig
->interface
[i
])) {
792 dev_warn (&ps
->dev
->dev
,
793 "usbfs: interface %d claimed by %s "
794 "while '%s' sets config #%d\n",
795 actconfig
->interface
[i
]
797 ->desc
.bInterfaceNumber
,
798 actconfig
->interface
[i
]
807 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
808 * so avoid usb_set_configuration()'s kick to sysfs
811 if (actconfig
&& actconfig
->desc
.bConfigurationValue
== u
)
812 status
= usb_reset_configuration(ps
->dev
);
814 status
= usb_set_configuration(ps
->dev
, u
);
821 static int proc_do_submiturb(struct dev_state
*ps
, struct usbdevfs_urb
*uurb
,
822 struct usbdevfs_iso_packet_desc __user
*iso_frame_desc
,
825 struct usbdevfs_iso_packet_desc
*isopkt
= NULL
;
826 struct usb_host_endpoint
*ep
;
828 struct usb_ctrlrequest
*dr
= NULL
;
829 unsigned int u
, totlen
, isofrmlen
;
830 int ret
, interval
= 0, ifnum
= -1;
832 if (uurb
->flags
& ~(USBDEVFS_URB_ISO_ASAP
|USBDEVFS_URB_SHORT_NOT_OK
|
833 URB_NO_FSBR
|URB_ZERO_PACKET
))
837 if (uurb
->signr
!= 0 && (uurb
->signr
< SIGRTMIN
|| uurb
->signr
> SIGRTMAX
))
839 if (!(uurb
->type
== USBDEVFS_URB_TYPE_CONTROL
&& (uurb
->endpoint
& ~USB_ENDPOINT_DIR_MASK
) == 0)) {
840 if ((ifnum
= findintfep(ps
->dev
, uurb
->endpoint
)) < 0)
842 if ((ret
= checkintf(ps
, ifnum
)))
845 if ((uurb
->endpoint
& USB_ENDPOINT_DIR_MASK
) != 0)
846 ep
= ps
->dev
->ep_in
[uurb
->endpoint
& USB_ENDPOINT_NUMBER_MASK
];
848 ep
= ps
->dev
->ep_out
[uurb
->endpoint
& USB_ENDPOINT_NUMBER_MASK
];
852 case USBDEVFS_URB_TYPE_CONTROL
:
853 if ((ep
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
854 != USB_ENDPOINT_XFER_CONTROL
)
856 /* min 8 byte setup packet, max arbitrary */
857 if (uurb
->buffer_length
< 8 || uurb
->buffer_length
> PAGE_SIZE
)
859 if (!(dr
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
)))
861 if (copy_from_user(dr
, uurb
->buffer
, 8)) {
865 if (uurb
->buffer_length
< (le16_to_cpup(&dr
->wLength
) + 8)) {
869 if ((ret
= check_ctrlrecip(ps
, dr
->bRequestType
, le16_to_cpup(&dr
->wIndex
)))) {
873 uurb
->endpoint
= (uurb
->endpoint
& ~USB_ENDPOINT_DIR_MASK
) | (dr
->bRequestType
& USB_ENDPOINT_DIR_MASK
);
874 uurb
->number_of_packets
= 0;
875 uurb
->buffer_length
= le16_to_cpup(&dr
->wLength
);
877 if (!access_ok((uurb
->endpoint
& USB_DIR_IN
) ? VERIFY_WRITE
: VERIFY_READ
, uurb
->buffer
, uurb
->buffer_length
)) {
883 case USBDEVFS_URB_TYPE_BULK
:
884 switch (ep
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
885 case USB_ENDPOINT_XFER_CONTROL
:
886 case USB_ENDPOINT_XFER_ISOC
:
888 /* allow single-shot interrupt transfers, at bogus rates */
890 uurb
->number_of_packets
= 0;
891 if (uurb
->buffer_length
> MAX_USBFS_BUFFER_SIZE
)
893 if (!access_ok((uurb
->endpoint
& USB_DIR_IN
) ? VERIFY_WRITE
: VERIFY_READ
, uurb
->buffer
, uurb
->buffer_length
))
897 case USBDEVFS_URB_TYPE_ISO
:
898 /* arbitrary limit */
899 if (uurb
->number_of_packets
< 1 || uurb
->number_of_packets
> 128)
901 if ((ep
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
902 != USB_ENDPOINT_XFER_ISOC
)
904 interval
= 1 << min (15, ep
->desc
.bInterval
- 1);
905 isofrmlen
= sizeof(struct usbdevfs_iso_packet_desc
) * uurb
->number_of_packets
;
906 if (!(isopkt
= kmalloc(isofrmlen
, GFP_KERNEL
)))
908 if (copy_from_user(isopkt
, iso_frame_desc
, isofrmlen
)) {
912 for (totlen
= u
= 0; u
< uurb
->number_of_packets
; u
++) {
913 if (isopkt
[u
].length
> 1023) {
917 totlen
+= isopkt
[u
].length
;
919 if (totlen
> 32768) {
923 uurb
->buffer_length
= totlen
;
926 case USBDEVFS_URB_TYPE_INTERRUPT
:
927 uurb
->number_of_packets
= 0;
928 if ((ep
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
929 != USB_ENDPOINT_XFER_INT
)
931 if (ps
->dev
->speed
== USB_SPEED_HIGH
)
932 interval
= 1 << min (15, ep
->desc
.bInterval
- 1);
934 interval
= ep
->desc
.bInterval
;
935 if (uurb
->buffer_length
> MAX_USBFS_BUFFER_SIZE
)
937 if (!access_ok((uurb
->endpoint
& USB_DIR_IN
) ? VERIFY_WRITE
: VERIFY_READ
, uurb
->buffer
, uurb
->buffer_length
))
944 if (!(as
= alloc_async(uurb
->number_of_packets
))) {
949 if (!(as
->urb
->transfer_buffer
= kmalloc(uurb
->buffer_length
, GFP_KERNEL
))) {
955 as
->urb
->dev
= ps
->dev
;
956 as
->urb
->pipe
= (uurb
->type
<< 30) | __create_pipe(ps
->dev
, uurb
->endpoint
& 0xf) | (uurb
->endpoint
& USB_DIR_IN
);
957 as
->urb
->transfer_flags
= uurb
->flags
;
958 as
->urb
->transfer_buffer_length
= uurb
->buffer_length
;
959 as
->urb
->setup_packet
= (unsigned char*)dr
;
960 as
->urb
->start_frame
= uurb
->start_frame
;
961 as
->urb
->number_of_packets
= uurb
->number_of_packets
;
962 as
->urb
->interval
= interval
;
963 as
->urb
->context
= as
;
964 as
->urb
->complete
= async_completed
;
965 for (totlen
= u
= 0; u
< uurb
->number_of_packets
; u
++) {
966 as
->urb
->iso_frame_desc
[u
].offset
= totlen
;
967 as
->urb
->iso_frame_desc
[u
].length
= isopkt
[u
].length
;
968 totlen
+= isopkt
[u
].length
;
973 if (uurb
->endpoint
& USB_DIR_IN
)
974 as
->userbuffer
= uurb
->buffer
;
976 as
->userbuffer
= NULL
;
977 as
->signr
= uurb
->signr
;
980 if (!(uurb
->endpoint
& USB_DIR_IN
)) {
981 if (copy_from_user(as
->urb
->transfer_buffer
, uurb
->buffer
, as
->urb
->transfer_buffer_length
)) {
986 async_newpending(as
);
987 if ((ret
= usb_submit_urb(as
->urb
, GFP_KERNEL
))) {
988 dev_printk(KERN_DEBUG
, &ps
->dev
->dev
, "usbfs: usb_submit_urb returned %d\n", ret
);
989 async_removepending(as
);
996 static int proc_submiturb(struct dev_state
*ps
, void __user
*arg
)
998 struct usbdevfs_urb uurb
;
1000 if (copy_from_user(&uurb
, arg
, sizeof(uurb
)))
1003 return proc_do_submiturb(ps
, &uurb
, (((struct usbdevfs_urb __user
*)arg
)->iso_frame_desc
), arg
);
1006 static int proc_unlinkurb(struct dev_state
*ps
, void __user
*arg
)
1010 as
= async_getpending(ps
, arg
);
1013 usb_kill_urb(as
->urb
);
1017 static int processcompl(struct async
*as
, void __user
* __user
*arg
)
1019 struct urb
*urb
= as
->urb
;
1020 struct usbdevfs_urb __user
*userurb
= as
->userurb
;
1021 void __user
*addr
= as
->userurb
;
1025 if (copy_to_user(as
->userbuffer
, urb
->transfer_buffer
, urb
->transfer_buffer_length
))
1027 if (put_user(urb
->status
, &userurb
->status
))
1029 if (put_user(urb
->actual_length
, &userurb
->actual_length
))
1031 if (put_user(urb
->error_count
, &userurb
->error_count
))
1034 if (usb_pipeisoc(urb
->pipe
)) {
1035 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1036 if (put_user(urb
->iso_frame_desc
[i
].actual_length
,
1037 &userurb
->iso_frame_desc
[i
].actual_length
))
1039 if (put_user(urb
->iso_frame_desc
[i
].status
,
1040 &userurb
->iso_frame_desc
[i
].status
))
1047 if (put_user(addr
, (void __user
* __user
*)arg
))
1052 static struct async
* reap_as(struct dev_state
*ps
)
1054 DECLARE_WAITQUEUE(wait
, current
);
1055 struct async
*as
= NULL
;
1056 struct usb_device
*dev
= ps
->dev
;
1058 add_wait_queue(&ps
->wait
, &wait
);
1060 __set_current_state(TASK_INTERRUPTIBLE
);
1061 if ((as
= async_getcompleted(ps
)))
1063 if (signal_pending(current
))
1065 usb_unlock_device(dev
);
1067 usb_lock_device(dev
);
1069 remove_wait_queue(&ps
->wait
, &wait
);
1070 set_current_state(TASK_RUNNING
);
1074 static int proc_reapurb(struct dev_state
*ps
, void __user
*arg
)
1076 struct async
*as
= reap_as(ps
);
1078 return processcompl(as
, (void __user
* __user
*)arg
);
1079 if (signal_pending(current
))
1084 static int proc_reapurbnonblock(struct dev_state
*ps
, void __user
*arg
)
1088 if (!(as
= async_getcompleted(ps
)))
1090 return processcompl(as
, (void __user
* __user
*)arg
);
1093 #ifdef CONFIG_COMPAT
1095 static int get_urb32(struct usbdevfs_urb
*kurb
,
1096 struct usbdevfs_urb32 __user
*uurb
)
1099 if (get_user(kurb
->type
, &uurb
->type
) ||
1100 __get_user(kurb
->endpoint
, &uurb
->endpoint
) ||
1101 __get_user(kurb
->status
, &uurb
->status
) ||
1102 __get_user(kurb
->flags
, &uurb
->flags
) ||
1103 __get_user(kurb
->buffer_length
, &uurb
->buffer_length
) ||
1104 __get_user(kurb
->actual_length
, &uurb
->actual_length
) ||
1105 __get_user(kurb
->start_frame
, &uurb
->start_frame
) ||
1106 __get_user(kurb
->number_of_packets
, &uurb
->number_of_packets
) ||
1107 __get_user(kurb
->error_count
, &uurb
->error_count
) ||
1108 __get_user(kurb
->signr
, &uurb
->signr
))
1111 if (__get_user(uptr
, &uurb
->buffer
))
1113 kurb
->buffer
= compat_ptr(uptr
);
1114 if (__get_user(uptr
, &uurb
->buffer
))
1116 kurb
->usercontext
= compat_ptr(uptr
);
1121 static int proc_submiturb_compat(struct dev_state
*ps
, void __user
*arg
)
1123 struct usbdevfs_urb uurb
;
1125 if (get_urb32(&uurb
,(struct usbdevfs_urb32
*)arg
))
1128 return proc_do_submiturb(ps
, &uurb
, ((struct usbdevfs_urb32 __user
*)arg
)->iso_frame_desc
, arg
);
1131 static int processcompl_compat(struct async
*as
, void __user
* __user
*arg
)
1133 struct urb
*urb
= as
->urb
;
1134 struct usbdevfs_urb32 __user
*userurb
= as
->userurb
;
1135 void __user
*addr
= as
->userurb
;
1139 if (copy_to_user(as
->userbuffer
, urb
->transfer_buffer
, urb
->transfer_buffer_length
))
1141 if (put_user(urb
->status
, &userurb
->status
))
1143 if (put_user(urb
->actual_length
, &userurb
->actual_length
))
1145 if (put_user(urb
->error_count
, &userurb
->error_count
))
1148 if (usb_pipeisoc(urb
->pipe
)) {
1149 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1150 if (put_user(urb
->iso_frame_desc
[i
].actual_length
,
1151 &userurb
->iso_frame_desc
[i
].actual_length
))
1153 if (put_user(urb
->iso_frame_desc
[i
].status
,
1154 &userurb
->iso_frame_desc
[i
].status
))
1160 if (put_user((u32
)(u64
)addr
, (u32 __user
*)arg
))
1165 static int proc_reapurb_compat(struct dev_state
*ps
, void __user
*arg
)
1167 struct async
*as
= reap_as(ps
);
1169 return processcompl_compat(as
, (void __user
* __user
*)arg
);
1170 if (signal_pending(current
))
1175 static int proc_reapurbnonblock_compat(struct dev_state
*ps
, void __user
*arg
)
1179 if (!(as
= async_getcompleted(ps
)))
1181 return processcompl_compat(as
, (void __user
* __user
*)arg
);
1186 static int proc_disconnectsignal(struct dev_state
*ps
, void __user
*arg
)
1188 struct usbdevfs_disconnectsignal ds
;
1190 if (copy_from_user(&ds
, arg
, sizeof(ds
)))
1192 if (ds
.signr
!= 0 && (ds
.signr
< SIGRTMIN
|| ds
.signr
> SIGRTMAX
))
1194 ps
->discsignr
= ds
.signr
;
1195 ps
->disccontext
= ds
.context
;
1199 static int proc_claiminterface(struct dev_state
*ps
, void __user
*arg
)
1203 if (get_user(ifnum
, (unsigned int __user
*)arg
))
1205 return claimintf(ps
, ifnum
);
1208 static int proc_releaseinterface(struct dev_state
*ps
, void __user
*arg
)
1213 if (get_user(ifnum
, (unsigned int __user
*)arg
))
1215 if ((ret
= releaseintf(ps
, ifnum
)) < 0)
1217 destroy_async_on_interface (ps
, ifnum
);
1221 static int proc_ioctl (struct dev_state
*ps
, void __user
*arg
)
1223 struct usbdevfs_ioctl ctrl
;
1227 struct usb_interface
*intf
= NULL
;
1228 struct usb_driver
*driver
= NULL
;
1231 /* get input parameters and alloc buffer */
1232 if (copy_from_user(&ctrl
, arg
, sizeof (ctrl
)))
1234 if ((size
= _IOC_SIZE (ctrl
.ioctl_code
)) > 0) {
1235 if ((buf
= kmalloc (size
, GFP_KERNEL
)) == NULL
)
1237 if ((_IOC_DIR(ctrl
.ioctl_code
) & _IOC_WRITE
)) {
1238 if (copy_from_user (buf
, ctrl
.data
, size
)) {
1243 memset (buf
, 0, size
);
1247 if (!connected(ps
->dev
)) {
1252 if (ps
->dev
->state
!= USB_STATE_CONFIGURED
)
1253 retval
= -EHOSTUNREACH
;
1254 else if (!(intf
= usb_ifnum_to_if (ps
->dev
, ctrl
.ifno
)))
1256 else switch (ctrl
.ioctl_code
) {
1258 /* disconnect kernel driver from interface */
1259 case USBDEVFS_DISCONNECT
:
1261 /* don't allow the user to unbind the hub driver from
1262 * a hub with children to manage */
1263 for (i
= 0; i
< ps
->dev
->maxchild
; ++i
) {
1264 if (ps
->dev
->children
[i
])
1270 down_write(&usb_bus_type
.subsys
.rwsem
);
1271 if (intf
->dev
.driver
) {
1272 driver
= to_usb_driver(intf
->dev
.driver
);
1273 dev_dbg (&intf
->dev
, "disconnect by usbfs\n");
1274 usb_driver_release_interface(driver
, intf
);
1277 up_write(&usb_bus_type
.subsys
.rwsem
);
1280 /* let kernel drivers try to (re)bind to the interface */
1281 case USBDEVFS_CONNECT
:
1282 usb_unlock_device(ps
->dev
);
1283 usb_lock_all_devices();
1284 bus_rescan_devices(intf
->dev
.bus
);
1285 usb_unlock_all_devices();
1286 usb_lock_device(ps
->dev
);
1289 /* talk directly to the interface's driver */
1291 down_read(&usb_bus_type
.subsys
.rwsem
);
1292 if (intf
->dev
.driver
)
1293 driver
= to_usb_driver(intf
->dev
.driver
);
1294 if (driver
== NULL
|| driver
->ioctl
== NULL
) {
1297 retval
= driver
->ioctl (intf
, ctrl
.ioctl_code
, buf
);
1298 if (retval
== -ENOIOCTLCMD
)
1301 up_read(&usb_bus_type
.subsys
.rwsem
);
1304 /* cleanup and return */
1306 && (_IOC_DIR (ctrl
.ioctl_code
) & _IOC_READ
) != 0
1308 && copy_to_user (ctrl
.data
, buf
, size
) != 0)
1316 * NOTE: All requests here that have interface numbers as parameters
1317 * are assuming that somehow the configuration has been prevented from
1318 * changing. But there's no mechanism to ensure that...
1320 static int usbdev_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1322 struct dev_state
*ps
= (struct dev_state
*)file
->private_data
;
1323 struct usb_device
*dev
= ps
->dev
;
1324 void __user
*p
= (void __user
*)arg
;
1327 if (!(file
->f_mode
& FMODE_WRITE
))
1329 usb_lock_device(dev
);
1330 if (!connected(dev
)) {
1331 usb_unlock_device(dev
);
1336 case USBDEVFS_CONTROL
:
1337 snoop(&dev
->dev
, "%s: CONTROL\n", __FUNCTION__
);
1338 ret
= proc_control(ps
, p
);
1340 inode
->i_mtime
= CURRENT_TIME
;
1344 snoop(&dev
->dev
, "%s: BULK\n", __FUNCTION__
);
1345 ret
= proc_bulk(ps
, p
);
1347 inode
->i_mtime
= CURRENT_TIME
;
1350 case USBDEVFS_RESETEP
:
1351 snoop(&dev
->dev
, "%s: RESETEP\n", __FUNCTION__
);
1352 ret
= proc_resetep(ps
, p
);
1354 inode
->i_mtime
= CURRENT_TIME
;
1357 case USBDEVFS_RESET
:
1358 snoop(&dev
->dev
, "%s: RESET\n", __FUNCTION__
);
1359 ret
= proc_resetdevice(ps
);
1362 case USBDEVFS_CLEAR_HALT
:
1363 snoop(&dev
->dev
, "%s: CLEAR_HALT\n", __FUNCTION__
);
1364 ret
= proc_clearhalt(ps
, p
);
1366 inode
->i_mtime
= CURRENT_TIME
;
1369 case USBDEVFS_GETDRIVER
:
1370 snoop(&dev
->dev
, "%s: GETDRIVER\n", __FUNCTION__
);
1371 ret
= proc_getdriver(ps
, p
);
1374 case USBDEVFS_CONNECTINFO
:
1375 snoop(&dev
->dev
, "%s: CONNECTINFO\n", __FUNCTION__
);
1376 ret
= proc_connectinfo(ps
, p
);
1379 case USBDEVFS_SETINTERFACE
:
1380 snoop(&dev
->dev
, "%s: SETINTERFACE\n", __FUNCTION__
);
1381 ret
= proc_setintf(ps
, p
);
1384 case USBDEVFS_SETCONFIGURATION
:
1385 snoop(&dev
->dev
, "%s: SETCONFIGURATION\n", __FUNCTION__
);
1386 ret
= proc_setconfig(ps
, p
);
1389 case USBDEVFS_SUBMITURB
:
1390 snoop(&dev
->dev
, "%s: SUBMITURB\n", __FUNCTION__
);
1391 ret
= proc_submiturb(ps
, p
);
1393 inode
->i_mtime
= CURRENT_TIME
;
1396 #ifdef CONFIG_COMPAT
1398 case USBDEVFS_SUBMITURB32
:
1399 snoop(&dev
->dev
, "%s: SUBMITURB32\n", __FUNCTION__
);
1400 ret
= proc_submiturb_compat(ps
, p
);
1402 inode
->i_mtime
= CURRENT_TIME
;
1405 case USBDEVFS_REAPURB32
:
1406 snoop(&dev
->dev
, "%s: REAPURB32\n", __FUNCTION__
);
1407 ret
= proc_reapurb_compat(ps
, p
);
1410 case USBDEVFS_REAPURBNDELAY32
:
1411 snoop(&dev
->dev
, "%s: REAPURBDELAY32\n", __FUNCTION__
);
1412 ret
= proc_reapurbnonblock_compat(ps
, p
);
1417 case USBDEVFS_DISCARDURB
:
1418 snoop(&dev
->dev
, "%s: DISCARDURB\n", __FUNCTION__
);
1419 ret
= proc_unlinkurb(ps
, p
);
1422 case USBDEVFS_REAPURB
:
1423 snoop(&dev
->dev
, "%s: REAPURB\n", __FUNCTION__
);
1424 ret
= proc_reapurb(ps
, p
);
1427 case USBDEVFS_REAPURBNDELAY
:
1428 snoop(&dev
->dev
, "%s: REAPURBDELAY\n", __FUNCTION__
);
1429 ret
= proc_reapurbnonblock(ps
, p
);
1432 case USBDEVFS_DISCSIGNAL
:
1433 snoop(&dev
->dev
, "%s: DISCSIGNAL\n", __FUNCTION__
);
1434 ret
= proc_disconnectsignal(ps
, p
);
1437 case USBDEVFS_CLAIMINTERFACE
:
1438 snoop(&dev
->dev
, "%s: CLAIMINTERFACE\n", __FUNCTION__
);
1439 ret
= proc_claiminterface(ps
, p
);
1442 case USBDEVFS_RELEASEINTERFACE
:
1443 snoop(&dev
->dev
, "%s: RELEASEINTERFACE\n", __FUNCTION__
);
1444 ret
= proc_releaseinterface(ps
, p
);
1447 case USBDEVFS_IOCTL
:
1448 snoop(&dev
->dev
, "%s: IOCTL\n", __FUNCTION__
);
1449 ret
= proc_ioctl(ps
, p
);
1452 usb_unlock_device(dev
);
1454 inode
->i_atime
= CURRENT_TIME
;
1458 /* No kernel lock - fine */
1459 static unsigned int usbdev_poll(struct file
*file
, struct poll_table_struct
*wait
)
1461 struct dev_state
*ps
= (struct dev_state
*)file
->private_data
;
1462 unsigned int mask
= 0;
1464 poll_wait(file
, &ps
->wait
, wait
);
1465 if (file
->f_mode
& FMODE_WRITE
&& !list_empty(&ps
->async_completed
))
1466 mask
|= POLLOUT
| POLLWRNORM
;
1467 if (!connected(ps
->dev
))
1468 mask
|= POLLERR
| POLLHUP
;
1472 struct file_operations usbfs_device_file_operations
= {
1473 .llseek
= usbdev_lseek
,
1474 .read
= usbdev_read
,
1475 .poll
= usbdev_poll
,
1476 .ioctl
= usbdev_ioctl
,
1477 .open
= usbdev_open
,
1478 .release
= usbdev_release
,