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
33 * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery
37 /*****************************************************************************/
41 #include <linux/slab.h>
42 #include <linux/smp_lock.h>
43 #include <linux/signal.h>
44 #include <linux/poll.h>
45 #include <linux/module.h>
46 #include <linux/usb.h>
47 #include <linux/usbdevice_fs.h>
48 #include <linux/cdev.h>
49 #include <linux/notifier.h>
50 #include <linux/security.h>
51 #include <asm/uaccess.h>
52 #include <asm/byteorder.h>
53 #include <linux/moduleparam.h>
55 #include "hcd.h" /* for usbcore internals */
59 #define USB_DEVICE_MAX USB_MAXBUS * 128
61 /* Mutual exclusion for removal, open, and release */
62 DEFINE_MUTEX(usbfs_mutex
);
65 struct list_head list
; /* state list */
66 struct usb_device
*dev
;
68 spinlock_t lock
; /* protects the async urb lists */
69 struct list_head async_pending
;
70 struct list_head async_completed
;
71 wait_queue_head_t wait
; /* wake up if a request completed */
72 unsigned int discsignr
;
74 uid_t disc_uid
, disc_euid
;
75 void __user
*disccontext
;
76 unsigned long ifclaimed
;
81 struct list_head asynclist
;
87 void __user
*userbuffer
;
94 static int usbfs_snoop
= 0;
95 module_param (usbfs_snoop
, bool, S_IRUGO
| S_IWUSR
);
96 MODULE_PARM_DESC (usbfs_snoop
, "true to log all usbfs traffic");
98 #define snoop(dev, format, arg...) \
101 dev_info( dev , format , ## arg); \
104 #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
107 #define MAX_USBFS_BUFFER_SIZE 16384
109 static int connected(struct dev_state
*ps
)
111 return (!list_empty(&ps
->list
) &&
112 ps
->dev
->state
!= USB_STATE_NOTATTACHED
);
115 static loff_t
usbdev_lseek(struct file
*file
, loff_t offset
, int orig
)
123 file
->f_pos
= offset
;
127 file
->f_pos
+= offset
;
139 static ssize_t
usbdev_read(struct file
*file
, char __user
*buf
, size_t nbytes
, loff_t
*ppos
)
141 struct dev_state
*ps
= file
->private_data
;
142 struct usb_device
*dev
= ps
->dev
;
149 usb_lock_device(dev
);
150 if (!connected(ps
)) {
153 } else if (pos
< 0) {
158 if (pos
< sizeof(struct usb_device_descriptor
)) {
159 struct usb_device_descriptor temp_desc
; /* 18 bytes - fits on the stack */
161 memcpy(&temp_desc
, &dev
->descriptor
, sizeof(dev
->descriptor
));
162 le16_to_cpus(&temp_desc
.bcdUSB
);
163 le16_to_cpus(&temp_desc
.idVendor
);
164 le16_to_cpus(&temp_desc
.idProduct
);
165 le16_to_cpus(&temp_desc
.bcdDevice
);
167 len
= sizeof(struct usb_device_descriptor
) - pos
;
170 if (copy_to_user(buf
, ((char *)&temp_desc
) + pos
, len
)) {
181 pos
= sizeof(struct usb_device_descriptor
);
182 for (i
= 0; nbytes
&& i
< dev
->descriptor
.bNumConfigurations
; i
++) {
183 struct usb_config_descriptor
*config
=
184 (struct usb_config_descriptor
*)dev
->rawdescriptors
[i
];
185 unsigned int length
= le16_to_cpu(config
->wTotalLength
);
187 if (*ppos
< pos
+ length
) {
189 /* The descriptor may claim to be longer than it
190 * really is. Here is the actual allocated length. */
192 le16_to_cpu(dev
->config
[i
].desc
.wTotalLength
);
194 len
= length
- (*ppos
- pos
);
198 /* Simply don't write (skip over) unallocated parts */
199 if (alloclen
> (*ppos
- pos
)) {
200 alloclen
-= (*ppos
- pos
);
201 if (copy_to_user(buf
,
202 dev
->rawdescriptors
[i
] + (*ppos
- pos
),
203 min(len
, alloclen
))) {
219 usb_unlock_device(dev
);
224 * async list handling
227 static struct async
*alloc_async(unsigned int numisoframes
)
229 unsigned int assize
= sizeof(struct async
) + numisoframes
* sizeof(struct usb_iso_packet_descriptor
);
230 struct async
*as
= kzalloc(assize
, GFP_KERNEL
);
234 as
->urb
= usb_alloc_urb(numisoframes
, GFP_KERNEL
);
242 static void free_async(struct async
*as
)
245 kfree(as
->urb
->transfer_buffer
);
246 kfree(as
->urb
->setup_packet
);
247 usb_free_urb(as
->urb
);
251 static void async_newpending(struct async
*as
)
253 struct dev_state
*ps
= as
->ps
;
256 spin_lock_irqsave(&ps
->lock
, flags
);
257 list_add_tail(&as
->asynclist
, &ps
->async_pending
);
258 spin_unlock_irqrestore(&ps
->lock
, flags
);
261 static void async_removepending(struct async
*as
)
263 struct dev_state
*ps
= as
->ps
;
266 spin_lock_irqsave(&ps
->lock
, flags
);
267 list_del_init(&as
->asynclist
);
268 spin_unlock_irqrestore(&ps
->lock
, flags
);
271 static struct async
*async_getcompleted(struct dev_state
*ps
)
274 struct async
*as
= NULL
;
276 spin_lock_irqsave(&ps
->lock
, flags
);
277 if (!list_empty(&ps
->async_completed
)) {
278 as
= list_entry(ps
->async_completed
.next
, struct async
, asynclist
);
279 list_del_init(&as
->asynclist
);
281 spin_unlock_irqrestore(&ps
->lock
, flags
);
285 static struct async
*async_getpending(struct dev_state
*ps
, void __user
*userurb
)
290 spin_lock_irqsave(&ps
->lock
, flags
);
291 list_for_each_entry(as
, &ps
->async_pending
, asynclist
)
292 if (as
->userurb
== userurb
) {
293 list_del_init(&as
->asynclist
);
294 spin_unlock_irqrestore(&ps
->lock
, flags
);
297 spin_unlock_irqrestore(&ps
->lock
, flags
);
301 static void snoop_urb(struct urb
*urb
, void __user
*userurb
)
304 unsigned char *data
= urb
->transfer_buffer
;
309 dev_info(&urb
->dev
->dev
, "direction=%s\n",
310 usb_urb_dir_in(urb
) ? "IN" : "OUT");
311 dev_info(&urb
->dev
->dev
, "userurb=%p\n", userurb
);
312 dev_info(&urb
->dev
->dev
, "transfer_buffer_length=%u\n",
313 urb
->transfer_buffer_length
);
314 dev_info(&urb
->dev
->dev
, "actual_length=%u\n", urb
->actual_length
);
315 dev_info(&urb
->dev
->dev
, "data: ");
316 for (j
= 0; j
< urb
->transfer_buffer_length
; ++j
)
317 printk ("%02x ", data
[j
]);
321 static void async_completed(struct urb
*urb
)
323 struct async
*as
= urb
->context
;
324 struct dev_state
*ps
= as
->ps
;
325 struct siginfo sinfo
;
326 struct pid
*pid
= NULL
;
332 spin_lock(&ps
->lock
);
333 list_move_tail(&as
->asynclist
, &ps
->async_completed
);
334 as
->status
= urb
->status
;
337 sinfo
.si_signo
= as
->signr
;
338 sinfo
.si_errno
= as
->status
;
339 sinfo
.si_code
= SI_ASYNCIO
;
340 sinfo
.si_addr
= as
->userurb
;
346 snoop(&urb
->dev
->dev
, "urb complete\n");
347 snoop_urb(urb
, as
->userurb
);
348 spin_unlock(&ps
->lock
);
351 kill_pid_info_as_uid(sinfo
.si_signo
, &sinfo
, pid
, uid
,
357 static void destroy_async (struct dev_state
*ps
, struct list_head
*list
)
362 spin_lock_irqsave(&ps
->lock
, flags
);
363 while (!list_empty(list
)) {
364 as
= list_entry(list
->next
, struct async
, asynclist
);
365 list_del_init(&as
->asynclist
);
367 /* drop the spinlock so the completion handler can run */
368 spin_unlock_irqrestore(&ps
->lock
, flags
);
369 usb_kill_urb(as
->urb
);
370 spin_lock_irqsave(&ps
->lock
, flags
);
372 spin_unlock_irqrestore(&ps
->lock
, flags
);
375 static void destroy_async_on_interface (struct dev_state
*ps
, unsigned int ifnum
)
377 struct list_head
*p
, *q
, hitlist
;
380 INIT_LIST_HEAD(&hitlist
);
381 spin_lock_irqsave(&ps
->lock
, flags
);
382 list_for_each_safe(p
, q
, &ps
->async_pending
)
383 if (ifnum
== list_entry(p
, struct async
, asynclist
)->ifnum
)
384 list_move_tail(p
, &hitlist
);
385 spin_unlock_irqrestore(&ps
->lock
, flags
);
386 destroy_async(ps
, &hitlist
);
389 static void destroy_all_async(struct dev_state
*ps
)
391 destroy_async(ps
, &ps
->async_pending
);
395 * interface claims are made only at the request of user level code,
396 * which can also release them (explicitly or by closing files).
397 * they're also undone when devices disconnect.
400 static int driver_probe (struct usb_interface
*intf
,
401 const struct usb_device_id
*id
)
406 static void driver_disconnect(struct usb_interface
*intf
)
408 struct dev_state
*ps
= usb_get_intfdata (intf
);
409 unsigned int ifnum
= intf
->altsetting
->desc
.bInterfaceNumber
;
414 /* NOTE: this relies on usbcore having canceled and completed
415 * all pending I/O requests; 2.6 does that.
418 if (likely(ifnum
< 8*sizeof(ps
->ifclaimed
)))
419 clear_bit(ifnum
, &ps
->ifclaimed
);
421 warn("interface number %u out of range", ifnum
);
423 usb_set_intfdata (intf
, NULL
);
425 /* force async requests to complete */
426 destroy_async_on_interface(ps
, ifnum
);
429 struct usb_driver usbfs_driver
= {
431 .probe
= driver_probe
,
432 .disconnect
= driver_disconnect
,
435 static int claimintf(struct dev_state
*ps
, unsigned int ifnum
)
437 struct usb_device
*dev
= ps
->dev
;
438 struct usb_interface
*intf
;
441 if (ifnum
>= 8*sizeof(ps
->ifclaimed
))
443 /* already claimed */
444 if (test_bit(ifnum
, &ps
->ifclaimed
))
447 intf
= usb_ifnum_to_if(dev
, ifnum
);
451 err
= usb_driver_claim_interface(&usbfs_driver
, intf
, ps
);
453 set_bit(ifnum
, &ps
->ifclaimed
);
454 else if (!strcmp(intf
->dev
.driver
->name
, "usblp")) // patch for U2EC
455 err
= 0; // patch for U2EC
459 static int releaseintf(struct dev_state
*ps
, unsigned int ifnum
)
461 struct usb_device
*dev
;
462 struct usb_interface
*intf
;
466 if (ifnum
>= 8*sizeof(ps
->ifclaimed
))
469 intf
= usb_ifnum_to_if(dev
, ifnum
);
472 else if (test_and_clear_bit(ifnum
, &ps
->ifclaimed
)) {
473 usb_driver_release_interface(&usbfs_driver
, intf
);
479 static int checkintf(struct dev_state
*ps
, unsigned int ifnum
)
481 if (ps
->dev
->state
!= USB_STATE_CONFIGURED
)
482 return -EHOSTUNREACH
;
483 if (ifnum
>= 8*sizeof(ps
->ifclaimed
))
485 if (test_bit(ifnum
, &ps
->ifclaimed
))
487 /* if not yet claimed, claim it for the driver */
489 /* dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim interface %u before use\n",
490 current->pid, current->comm, ifnum);//*/
491 return claimintf(ps
, ifnum
);
494 static int findintfep(struct usb_device
*dev
, unsigned int ep
)
496 unsigned int i
, j
, e
;
497 struct usb_interface
*intf
;
498 struct usb_host_interface
*alts
;
499 struct usb_endpoint_descriptor
*endpt
;
501 if (ep
& ~(USB_DIR_IN
|0xf))
505 for (i
= 0; i
< dev
->actconfig
->desc
.bNumInterfaces
; i
++) {
506 intf
= dev
->actconfig
->interface
[i
];
507 for (j
= 0; j
< intf
->num_altsetting
; j
++) {
508 alts
= &intf
->altsetting
[j
];
509 for (e
= 0; e
< alts
->desc
.bNumEndpoints
; e
++) {
510 endpt
= &alts
->endpoint
[e
].desc
;
511 if (endpt
->bEndpointAddress
== ep
)
512 return alts
->desc
.bInterfaceNumber
;
519 static int check_ctrlrecip(struct dev_state
*ps
, unsigned int requesttype
, unsigned int index
)
523 if (ps
->dev
->state
!= USB_STATE_ADDRESS
524 && ps
->dev
->state
!= USB_STATE_CONFIGURED
)
525 return -EHOSTUNREACH
;
526 if (USB_TYPE_VENDOR
== (USB_TYPE_MASK
& requesttype
))
530 switch (requesttype
& USB_RECIP_MASK
) {
531 case USB_RECIP_ENDPOINT
:
532 if ((ret
= findintfep(ps
->dev
, index
)) >= 0)
533 ret
= checkintf(ps
, ret
);
536 case USB_RECIP_INTERFACE
:
537 ret
= checkintf(ps
, index
);
543 static int match_devt(struct device
*dev
, void *data
)
545 return dev
->devt
== (dev_t
) (unsigned long) data
;
548 static struct usb_device
*usbdev_lookup_by_devt(dev_t devt
)
552 dev
= bus_find_device(&usb_bus_type
, NULL
,
553 (void *) (unsigned long) devt
, match_devt
);
556 return container_of(dev
, struct usb_device
, dev
);
562 static int usbdev_open(struct inode
*inode
, struct file
*file
)
564 struct usb_device
*dev
= NULL
;
565 struct dev_state
*ps
;
568 /* Protect against simultaneous removal or release */
569 mutex_lock(&usbfs_mutex
);
572 if (!(ps
= kmalloc(sizeof(struct dev_state
), GFP_KERNEL
)))
577 /* usbdev device-node */
578 if (imajor(inode
) == USB_DEVICE_MAJOR
)
579 dev
= usbdev_lookup_by_devt(inode
->i_rdev
);
580 #ifdef CONFIG_USB_DEVICEFS
583 dev
= inode
->i_private
;
584 if (dev
&& dev
->usbfs_dentry
&&
585 dev
->usbfs_dentry
->d_inode
== inode
)
591 if (!dev
|| dev
->state
== USB_STATE_NOTATTACHED
)
593 ret
= usb_autoresume_device(dev
);
600 spin_lock_init(&ps
->lock
);
601 INIT_LIST_HEAD(&ps
->list
);
602 INIT_LIST_HEAD(&ps
->async_pending
);
603 INIT_LIST_HEAD(&ps
->async_completed
);
604 init_waitqueue_head(&ps
->wait
);
606 ps
->disc_pid
= get_pid(task_pid(current
));
607 ps
->disc_uid
= current
->uid
;
608 ps
->disc_euid
= current
->euid
;
609 ps
->disccontext
= NULL
;
611 security_task_getsecid(current
, &ps
->secid
);
613 list_add_tail(&ps
->list
, &dev
->filelist
);
614 file
->private_data
= ps
;
620 mutex_unlock(&usbfs_mutex
);
624 static int usbdev_release(struct inode
*inode
, struct file
*file
)
626 struct dev_state
*ps
= file
->private_data
;
627 struct usb_device
*dev
= ps
->dev
;
631 usb_lock_device(dev
);
633 /* Protect against simultaneous open */
634 mutex_lock(&usbfs_mutex
);
635 list_del_init(&ps
->list
);
636 mutex_unlock(&usbfs_mutex
);
638 for (ifnum
= 0; ps
->ifclaimed
&& ifnum
< 8*sizeof(ps
->ifclaimed
);
640 if (test_bit(ifnum
, &ps
->ifclaimed
))
641 releaseintf(ps
, ifnum
);
643 destroy_all_async(ps
);
644 usb_autosuspend_device(dev
);
645 usb_unlock_device(dev
);
647 put_pid(ps
->disc_pid
);
649 as
= async_getcompleted(ps
);
652 as
= async_getcompleted(ps
);
658 static int proc_control(struct dev_state
*ps
, void __user
*arg
)
660 struct usb_device
*dev
= ps
->dev
;
661 struct usbdevfs_ctrltransfer ctrl
;
667 if (copy_from_user(&ctrl
, arg
, sizeof(ctrl
)))
669 if ((ret
= check_ctrlrecip(ps
, ctrl
.bRequestType
, ctrl
.wIndex
)))
671 wLength
= ctrl
.wLength
; /* To suppress 64k PAGE_SIZE warning */
672 if (wLength
> PAGE_SIZE
)
674 if (!(tbuf
= (unsigned char *)__get_free_page(GFP_KERNEL
)))
677 if (ctrl
.bRequestType
& 0x80) {
678 if (ctrl
.wLength
&& !access_ok(VERIFY_WRITE
, ctrl
.data
, ctrl
.wLength
)) {
679 free_page((unsigned long)tbuf
);
682 snoop(&dev
->dev
, "control read: bRequest=%02x "
683 "bRrequestType=%02x wValue=%04x "
684 "wIndex=%04x wLength=%04x\n",
685 ctrl
.bRequest
, ctrl
.bRequestType
, ctrl
.wValue
,
686 ctrl
.wIndex
, ctrl
.wLength
);
688 usb_unlock_device(dev
);
689 i
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), ctrl
.bRequest
, ctrl
.bRequestType
,
690 ctrl
.wValue
, ctrl
.wIndex
, tbuf
, ctrl
.wLength
, tmo
);
691 usb_lock_device(dev
);
692 if ((i
> 0) && ctrl
.wLength
) {
694 dev_info(&dev
->dev
, "control read: data ");
695 for (j
= 0; j
< i
; ++j
)
696 printk("%02x ", (unsigned char)(tbuf
)[j
]);
699 if (copy_to_user(ctrl
.data
, tbuf
, i
)) {
700 free_page((unsigned long)tbuf
);
706 if (copy_from_user(tbuf
, ctrl
.data
, ctrl
.wLength
)) {
707 free_page((unsigned long)tbuf
);
711 snoop(&dev
->dev
, "control write: bRequest=%02x "
712 "bRrequestType=%02x wValue=%04x "
713 "wIndex=%04x wLength=%04x\n",
714 ctrl
.bRequest
, ctrl
.bRequestType
, ctrl
.wValue
,
715 ctrl
.wIndex
, ctrl
.wLength
);
717 dev_info(&dev
->dev
, "control write: data: ");
718 for (j
= 0; j
< ctrl
.wLength
; ++j
)
719 printk("%02x ", (unsigned char)(tbuf
)[j
]);
722 usb_unlock_device(dev
);
723 i
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), ctrl
.bRequest
, ctrl
.bRequestType
,
724 ctrl
.wValue
, ctrl
.wIndex
, tbuf
, ctrl
.wLength
, tmo
);
725 usb_lock_device(dev
);
727 free_page((unsigned long)tbuf
);
728 if (i
<0 && i
!= -EPIPE
) {
729 dev_printk(KERN_DEBUG
, &dev
->dev
, "usbfs: USBDEVFS_CONTROL "
730 "failed cmd %s rqt %u rq %u len %u ret %d\n",
731 current
->comm
, ctrl
.bRequestType
, ctrl
.bRequest
,
737 static int proc_bulk(struct dev_state
*ps
, void __user
*arg
)
739 struct usb_device
*dev
= ps
->dev
;
740 struct usbdevfs_bulktransfer bulk
;
741 unsigned int tmo
, len1
, pipe
;
746 if (copy_from_user(&bulk
, arg
, sizeof(bulk
)))
748 if ((ret
= findintfep(ps
->dev
, bulk
.ep
)) < 0)
750 if ((ret
= checkintf(ps
, ret
)))
752 if (bulk
.ep
& USB_DIR_IN
)
753 pipe
= usb_rcvbulkpipe(dev
, bulk
.ep
& 0x7f);
755 pipe
= usb_sndbulkpipe(dev
, bulk
.ep
& 0x7f);
756 if (!usb_maxpacket(dev
, pipe
, !(bulk
.ep
& USB_DIR_IN
)))
759 if (len1
> MAX_USBFS_BUFFER_SIZE
)
761 if (!(tbuf
= kmalloc(len1
, GFP_KERNEL
)))
764 if (bulk
.ep
& 0x80) {
765 if (len1
&& !access_ok(VERIFY_WRITE
, bulk
.data
, len1
)) {
769 snoop(&dev
->dev
, "bulk read: len=0x%02x timeout=%04d\n",
770 bulk
.len
, bulk
.timeout
);
771 usb_unlock_device(dev
);
772 i
= usb_bulk_msg(dev
, pipe
, tbuf
, len1
, &len2
, tmo
);
773 usb_lock_device(dev
);
776 dev_info(&dev
->dev
, "bulk read: data ");
777 for (j
= 0; j
< len2
; ++j
)
778 printk("%02x ", (unsigned char)(tbuf
)[j
]);
781 if (copy_to_user(bulk
.data
, tbuf
, len2
)) {
788 if (copy_from_user(tbuf
, bulk
.data
, len1
)) {
793 snoop(&dev
->dev
, "bulk write: len=0x%02x timeout=%04d\n",
794 bulk
.len
, bulk
.timeout
);
796 dev_info(&dev
->dev
, "bulk write: data: ");
797 for (j
= 0; j
< len1
; ++j
)
798 printk("%02x ", (unsigned char)(tbuf
)[j
]);
801 usb_unlock_device(dev
);
802 i
= usb_bulk_msg(dev
, pipe
, tbuf
, len1
, &len2
, tmo
);
803 usb_lock_device(dev
);
811 static int proc_resetep(struct dev_state
*ps
, void __user
*arg
)
816 if (get_user(ep
, (unsigned int __user
*)arg
))
818 if ((ret
= findintfep(ps
->dev
, ep
)) < 0)
820 if ((ret
= checkintf(ps
, ret
)))
822 usb_settoggle(ps
->dev
, ep
& 0xf, !(ep
& USB_DIR_IN
), 0);
826 static int proc_clearhalt(struct dev_state
*ps
, void __user
*arg
)
832 if (get_user(ep
, (unsigned int __user
*)arg
))
834 if ((ret
= findintfep(ps
->dev
, ep
)) < 0)
836 if ((ret
= checkintf(ps
, ret
)))
839 pipe
= usb_rcvbulkpipe(ps
->dev
, ep
& 0x7f);
841 pipe
= usb_sndbulkpipe(ps
->dev
, ep
& 0x7f);
843 return usb_clear_halt(ps
->dev
, pipe
);
847 static int proc_getdriver(struct dev_state
*ps
, void __user
*arg
)
849 struct usbdevfs_getdriver gd
;
850 struct usb_interface
*intf
;
853 if (copy_from_user(&gd
, arg
, sizeof(gd
)))
855 intf
= usb_ifnum_to_if(ps
->dev
, gd
.interface
);
856 if (!intf
|| !intf
->dev
.driver
)
859 strncpy(gd
.driver
, intf
->dev
.driver
->name
,
861 ret
= (copy_to_user(arg
, &gd
, sizeof(gd
)) ? -EFAULT
: 0);
866 static int proc_connectinfo(struct dev_state
*ps
, void __user
*arg
)
868 struct usbdevfs_connectinfo ci
= {
869 .devnum
= ps
->dev
->devnum
,
870 .slow
= ps
->dev
->speed
== USB_SPEED_LOW
873 if (copy_to_user(arg
, &ci
, sizeof(ci
)))
878 static int proc_resetdevice(struct dev_state
*ps
)
880 return usb_reset_device(ps
->dev
);
883 static int proc_setintf(struct dev_state
*ps
, void __user
*arg
)
885 struct usbdevfs_setinterface setintf
;
888 if (copy_from_user(&setintf
, arg
, sizeof(setintf
)))
890 if ((ret
= checkintf(ps
, setintf
.interface
)))
892 return usb_set_interface(ps
->dev
, setintf
.interface
,
896 static int proc_setconfig(struct dev_state
*ps
, void __user
*arg
)
900 struct usb_host_config
*actconfig
;
902 if (get_user(u
, (int __user
*)arg
))
905 actconfig
= ps
->dev
->actconfig
;
907 /* Don't touch the device if any interfaces are claimed.
908 * It could interfere with other drivers' operations, and if
909 * an interface is claimed by usbfs it could easily deadlock.
914 for (i
= 0; i
< actconfig
->desc
.bNumInterfaces
; ++i
) {
915 if (usb_interface_claimed(actconfig
->interface
[i
])) {
916 dev_warn (&ps
->dev
->dev
,
917 "usbfs: interface %d claimed by %s "
918 "while '%s' sets config #%d\n",
919 actconfig
->interface
[i
]
921 ->desc
.bInterfaceNumber
,
922 actconfig
->interface
[i
]
931 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
932 * so avoid usb_set_configuration()'s kick to sysfs
935 if (actconfig
&& actconfig
->desc
.bConfigurationValue
== u
)
936 status
= usb_reset_configuration(ps
->dev
);
938 status
= usb_set_configuration(ps
->dev
, u
);
944 static int proc_do_submiturb(struct dev_state
*ps
, struct usbdevfs_urb
*uurb
,
945 struct usbdevfs_iso_packet_desc __user
*iso_frame_desc
,
948 struct usbdevfs_iso_packet_desc
*isopkt
= NULL
;
949 struct usb_host_endpoint
*ep
;
951 struct usb_ctrlrequest
*dr
= NULL
;
952 unsigned int u
, totlen
, isofrmlen
;
956 if (uurb
->flags
& ~(USBDEVFS_URB_ISO_ASAP
|USBDEVFS_URB_SHORT_NOT_OK
|
957 URB_NO_FSBR
|URB_ZERO_PACKET
))
959 if (uurb
->buffer_length
> 0 && !uurb
->buffer
)
961 if (!(uurb
->type
== USBDEVFS_URB_TYPE_CONTROL
&&
962 (uurb
->endpoint
& ~USB_ENDPOINT_DIR_MASK
) == 0)) {
963 if ((ifnum
= findintfep(ps
->dev
, uurb
->endpoint
)) < 0)
965 if ((ret
= checkintf(ps
, ifnum
)))
968 if ((uurb
->endpoint
& USB_ENDPOINT_DIR_MASK
) != 0) {
970 ep
= ps
->dev
->ep_in
[uurb
->endpoint
& USB_ENDPOINT_NUMBER_MASK
];
973 ep
= ps
->dev
->ep_out
[uurb
->endpoint
& USB_ENDPOINT_NUMBER_MASK
];
978 case USBDEVFS_URB_TYPE_CONTROL
:
979 if (!usb_endpoint_xfer_control(&ep
->desc
))
981 /* min 8 byte setup packet, max 8 byte setup plus an arbitrary data stage */
982 if (uurb
->buffer_length
< 8 || uurb
->buffer_length
> (8 + MAX_USBFS_BUFFER_SIZE
))
984 if (!(dr
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
)))
986 if (copy_from_user(dr
, uurb
->buffer
, 8)) {
990 if (uurb
->buffer_length
< (le16_to_cpup(&dr
->wLength
) + 8)) {
994 if ((ret
= check_ctrlrecip(ps
, dr
->bRequestType
, le16_to_cpup(&dr
->wIndex
)))) {
998 uurb
->number_of_packets
= 0;
999 uurb
->buffer_length
= le16_to_cpup(&dr
->wLength
);
1001 if ((dr
->bRequestType
& USB_DIR_IN
) && uurb
->buffer_length
) {
1003 uurb
->endpoint
|= USB_DIR_IN
;
1006 uurb
->endpoint
&= ~USB_DIR_IN
;
1008 snoop(&ps
->dev
->dev
, "control urb: bRequest=%02x "
1009 "bRrequestType=%02x wValue=%04x "
1010 "wIndex=%04x wLength=%04x\n",
1011 dr
->bRequest
, dr
->bRequestType
,
1012 __le16_to_cpup(&dr
->wValue
),
1013 __le16_to_cpup(&dr
->wIndex
),
1014 __le16_to_cpup(&dr
->wLength
));
1017 case USBDEVFS_URB_TYPE_BULK
:
1018 switch (usb_endpoint_type(&ep
->desc
)) {
1019 case USB_ENDPOINT_XFER_CONTROL
:
1020 case USB_ENDPOINT_XFER_ISOC
:
1022 /* allow single-shot interrupt transfers, at bogus rates */
1024 uurb
->number_of_packets
= 0;
1025 if (uurb
->buffer_length
> MAX_USBFS_BUFFER_SIZE
)
1027 snoop(&ps
->dev
->dev
, "bulk urb\n");
1030 case USBDEVFS_URB_TYPE_ISO
:
1031 /* arbitrary limit */
1032 if (uurb
->number_of_packets
< 1 || uurb
->number_of_packets
> 128)
1034 if (!usb_endpoint_xfer_isoc(&ep
->desc
))
1036 isofrmlen
= sizeof(struct usbdevfs_iso_packet_desc
) * uurb
->number_of_packets
;
1037 if (!(isopkt
= kmalloc(isofrmlen
, GFP_KERNEL
)))
1039 if (copy_from_user(isopkt
, iso_frame_desc
, isofrmlen
)) {
1043 for (totlen
= u
= 0; u
< uurb
->number_of_packets
; u
++) {
1044 /* arbitrary limit, sufficient for USB 2.0 high-bandwidth iso */
1045 if (isopkt
[u
].length
> 8192) {
1049 totlen
+= isopkt
[u
].length
;
1051 /* 3072 * 64 microframes */
1052 if (totlen
> 196608) {
1056 uurb
->buffer_length
= totlen
;
1057 snoop(&ps
->dev
->dev
, "iso urb\n");
1060 case USBDEVFS_URB_TYPE_INTERRUPT
:
1061 uurb
->number_of_packets
= 0;
1062 if (!usb_endpoint_xfer_int(&ep
->desc
))
1064 if (uurb
->buffer_length
> MAX_USBFS_BUFFER_SIZE
)
1066 snoop(&ps
->dev
->dev
, "interrupt urb\n");
1072 if (uurb
->buffer_length
> 0 &&
1073 !access_ok(is_in
? VERIFY_WRITE
: VERIFY_READ
,
1074 uurb
->buffer
, uurb
->buffer_length
)) {
1079 as
= alloc_async(uurb
->number_of_packets
);
1085 if (uurb
->buffer_length
> 0) {
1086 as
->urb
->transfer_buffer
= kmalloc(uurb
->buffer_length
,
1088 if (!as
->urb
->transfer_buffer
) {
1094 /* Isochronous input data may end up being discontiguous
1095 * if some of the packets are short. Clear the buffer so
1096 * that the gaps don't leak kernel data to userspace.
1098 if (is_in
&& uurb
->type
== USBDEVFS_URB_TYPE_ISO
)
1099 memset(as
->urb
->transfer_buffer
, 0,
1100 uurb
->buffer_length
);
1102 as
->urb
->dev
= ps
->dev
;
1103 as
->urb
->pipe
= (uurb
->type
<< 30) |
1104 __create_pipe(ps
->dev
, uurb
->endpoint
& 0xf) |
1105 (uurb
->endpoint
& USB_DIR_IN
);
1106 as
->urb
->transfer_flags
= uurb
->flags
|
1107 (is_in
? URB_DIR_IN
: URB_DIR_OUT
);
1108 as
->urb
->transfer_buffer_length
= uurb
->buffer_length
;
1109 as
->urb
->setup_packet
= (unsigned char*)dr
;
1110 as
->urb
->start_frame
= uurb
->start_frame
;
1111 as
->urb
->number_of_packets
= uurb
->number_of_packets
;
1112 if (uurb
->type
== USBDEVFS_URB_TYPE_ISO
||
1113 ps
->dev
->speed
== USB_SPEED_HIGH
)
1114 as
->urb
->interval
= 1 << min(15, ep
->desc
.bInterval
- 1);
1116 as
->urb
->interval
= ep
->desc
.bInterval
;
1117 as
->urb
->context
= as
;
1118 as
->urb
->complete
= async_completed
;
1119 for (totlen
= u
= 0; u
< uurb
->number_of_packets
; u
++) {
1120 as
->urb
->iso_frame_desc
[u
].offset
= totlen
;
1121 as
->urb
->iso_frame_desc
[u
].length
= isopkt
[u
].length
;
1122 totlen
+= isopkt
[u
].length
;
1127 if (is_in
&& uurb
->buffer_length
> 0)
1128 as
->userbuffer
= uurb
->buffer
;
1130 as
->userbuffer
= NULL
;
1131 as
->signr
= uurb
->signr
;
1133 as
->pid
= get_pid(task_pid(current
));
1134 as
->uid
= current
->uid
;
1135 as
->euid
= current
->euid
;
1136 security_task_getsecid(current
, &as
->secid
);
1137 if (!is_in
&& uurb
->buffer_length
> 0) {
1138 if (copy_from_user(as
->urb
->transfer_buffer
, uurb
->buffer
,
1139 uurb
->buffer_length
)) {
1144 snoop_urb(as
->urb
, as
->userurb
);
1145 async_newpending(as
);
1146 if ((ret
= usb_submit_urb(as
->urb
, GFP_KERNEL
))) {
1147 dev_printk(KERN_DEBUG
, &ps
->dev
->dev
, "usbfs: usb_submit_urb returned %d\n", ret
);
1148 async_removepending(as
);
1155 static int proc_submiturb(struct dev_state
*ps
, void __user
*arg
)
1157 struct usbdevfs_urb uurb
;
1159 if (copy_from_user(&uurb
, arg
, sizeof(uurb
)))
1162 return proc_do_submiturb(ps
, &uurb
, (((struct usbdevfs_urb __user
*)arg
)->iso_frame_desc
), arg
);
1165 static int proc_unlinkurb(struct dev_state
*ps
, void __user
*arg
)
1169 as
= async_getpending(ps
, arg
);
1172 usb_kill_urb(as
->urb
);
1176 static int processcompl(struct async
*as
, void __user
* __user
*arg
)
1178 struct urb
*urb
= as
->urb
;
1179 struct usbdevfs_urb __user
*userurb
= as
->userurb
;
1180 void __user
*addr
= as
->userurb
;
1183 if (as
->userbuffer
&& urb
->actual_length
) {
1184 if (urb
->number_of_packets
> 0) /* Isochronous */
1185 i
= urb
->transfer_buffer_length
;
1187 i
= urb
->actual_length
;
1188 if (copy_to_user(as
->userbuffer
, urb
->transfer_buffer
, i
))
1191 if (put_user(as
->status
, &userurb
->status
))
1193 if (put_user(urb
->actual_length
, &userurb
->actual_length
))
1195 if (put_user(urb
->error_count
, &userurb
->error_count
))
1198 if (usb_endpoint_xfer_isoc(&urb
->ep
->desc
)) {
1199 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1200 if (put_user(urb
->iso_frame_desc
[i
].actual_length
,
1201 &userurb
->iso_frame_desc
[i
].actual_length
))
1203 if (put_user(urb
->iso_frame_desc
[i
].status
,
1204 &userurb
->iso_frame_desc
[i
].status
))
1209 if (put_user(addr
, (void __user
* __user
*)arg
))
1217 static struct async
* reap_as(struct dev_state
*ps
)
1219 DECLARE_WAITQUEUE(wait
, current
);
1220 struct async
*as
= NULL
;
1221 struct usb_device
*dev
= ps
->dev
;
1223 add_wait_queue(&ps
->wait
, &wait
);
1225 __set_current_state(TASK_INTERRUPTIBLE
);
1226 if ((as
= async_getcompleted(ps
)))
1228 if (signal_pending(current
))
1230 usb_unlock_device(dev
);
1232 usb_lock_device(dev
);
1234 remove_wait_queue(&ps
->wait
, &wait
);
1235 set_current_state(TASK_RUNNING
);
1239 static int proc_reapurb(struct dev_state
*ps
, void __user
*arg
)
1241 struct async
*as
= reap_as(ps
);
1243 int retval
= processcompl(as
, (void __user
* __user
*)arg
);
1247 if (signal_pending(current
))
1252 static int proc_reapurbnonblock(struct dev_state
*ps
, void __user
*arg
)
1257 as
= async_getcompleted(ps
);
1260 retval
= processcompl(as
, (void __user
* __user
*)arg
);
1266 #ifdef CONFIG_COMPAT
1268 static int get_urb32(struct usbdevfs_urb
*kurb
,
1269 struct usbdevfs_urb32 __user
*uurb
)
1272 if (get_user(kurb
->type
, &uurb
->type
) ||
1273 __get_user(kurb
->endpoint
, &uurb
->endpoint
) ||
1274 __get_user(kurb
->status
, &uurb
->status
) ||
1275 __get_user(kurb
->flags
, &uurb
->flags
) ||
1276 __get_user(kurb
->buffer_length
, &uurb
->buffer_length
) ||
1277 __get_user(kurb
->actual_length
, &uurb
->actual_length
) ||
1278 __get_user(kurb
->start_frame
, &uurb
->start_frame
) ||
1279 __get_user(kurb
->number_of_packets
, &uurb
->number_of_packets
) ||
1280 __get_user(kurb
->error_count
, &uurb
->error_count
) ||
1281 __get_user(kurb
->signr
, &uurb
->signr
))
1284 if (__get_user(uptr
, &uurb
->buffer
))
1286 kurb
->buffer
= compat_ptr(uptr
);
1287 if (__get_user(uptr
, &uurb
->usercontext
))
1289 kurb
->usercontext
= compat_ptr(uptr
);
1294 static int proc_submiturb_compat(struct dev_state
*ps
, void __user
*arg
)
1296 struct usbdevfs_urb uurb
;
1298 if (get_urb32(&uurb
,(struct usbdevfs_urb32 __user
*)arg
))
1301 return proc_do_submiturb(ps
, &uurb
, ((struct usbdevfs_urb32 __user
*)arg
)->iso_frame_desc
, arg
);
1304 static int processcompl_compat(struct async
*as
, void __user
* __user
*arg
)
1306 struct urb
*urb
= as
->urb
;
1307 struct usbdevfs_urb32 __user
*userurb
= as
->userurb
;
1308 void __user
*addr
= as
->userurb
;
1311 if (as
->userbuffer
&& urb
->actual_length
)
1312 if (copy_to_user(as
->userbuffer
, urb
->transfer_buffer
,
1313 urb
->actual_length
))
1315 if (put_user(as
->status
, &userurb
->status
))
1317 if (put_user(urb
->actual_length
, &userurb
->actual_length
))
1319 if (put_user(urb
->error_count
, &userurb
->error_count
))
1322 if (usb_endpoint_xfer_isoc(&urb
->ep
->desc
)) {
1323 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1324 if (put_user(urb
->iso_frame_desc
[i
].actual_length
,
1325 &userurb
->iso_frame_desc
[i
].actual_length
))
1327 if (put_user(urb
->iso_frame_desc
[i
].status
,
1328 &userurb
->iso_frame_desc
[i
].status
))
1333 if (put_user(ptr_to_compat(addr
), (u32 __user
*)arg
))
1338 static int proc_reapurb_compat(struct dev_state
*ps
, void __user
*arg
)
1340 struct async
*as
= reap_as(ps
);
1342 int retval
= processcompl_compat(as
, (void __user
* __user
*)arg
);
1346 if (signal_pending(current
))
1351 static int proc_reapurbnonblock_compat(struct dev_state
*ps
, void __user
*arg
)
1357 as
= async_getcompleted(ps
);
1359 retval
= processcompl_compat(as
, (void __user
* __user
*)arg
);
1367 static int proc_disconnectsignal(struct dev_state
*ps
, void __user
*arg
)
1369 struct usbdevfs_disconnectsignal ds
;
1371 if (copy_from_user(&ds
, arg
, sizeof(ds
)))
1373 ps
->discsignr
= ds
.signr
;
1374 ps
->disccontext
= ds
.context
;
1378 static int proc_claiminterface(struct dev_state
*ps
, void __user
*arg
)
1382 if (get_user(ifnum
, (unsigned int __user
*)arg
))
1384 return claimintf(ps
, ifnum
);
1387 static int proc_releaseinterface(struct dev_state
*ps
, void __user
*arg
)
1392 if (get_user(ifnum
, (unsigned int __user
*)arg
))
1394 if ((ret
= releaseintf(ps
, ifnum
)) < 0)
1396 destroy_async_on_interface (ps
, ifnum
);
1400 static int proc_ioctl(struct dev_state
*ps
, struct usbdevfs_ioctl
*ctl
)
1405 struct usb_interface
*intf
= NULL
;
1406 struct usb_driver
*driver
= NULL
;
1409 if ((size
= _IOC_SIZE (ctl
->ioctl_code
)) > 0) {
1410 if ((buf
= kmalloc (size
, GFP_KERNEL
)) == NULL
)
1412 if ((_IOC_DIR(ctl
->ioctl_code
) & _IOC_WRITE
)) {
1413 if (copy_from_user (buf
, ctl
->data
, size
)) {
1418 memset (buf
, 0, size
);
1422 if (!connected(ps
)) {
1427 if (ps
->dev
->state
!= USB_STATE_CONFIGURED
)
1428 retval
= -EHOSTUNREACH
;
1429 else if (!(intf
= usb_ifnum_to_if (ps
->dev
, ctl
->ifno
)))
1431 else switch (ctl
->ioctl_code
) {
1433 /* disconnect kernel driver from interface */
1434 case USBDEVFS_DISCONNECT
:
1435 if (intf
->dev
.driver
) {
1436 driver
= to_usb_driver(intf
->dev
.driver
);
1437 dev_dbg (&intf
->dev
, "disconnect by usbfs\n");
1438 usb_driver_release_interface(driver
, intf
);
1443 /* let kernel drivers try to (re)bind to the interface */
1444 case USBDEVFS_CONNECT
:
1445 usb_unlock_device(ps
->dev
);
1446 retval
= bus_rescan_devices(intf
->dev
.bus
);
1447 usb_lock_device(ps
->dev
);
1450 /* talk directly to the interface's driver */
1452 if (intf
->dev
.driver
)
1453 driver
= to_usb_driver(intf
->dev
.driver
);
1454 if (driver
== NULL
|| driver
->ioctl
== NULL
) {
1457 retval
= driver
->ioctl (intf
, ctl
->ioctl_code
, buf
);
1458 if (retval
== -ENOIOCTLCMD
)
1463 /* cleanup and return */
1465 && (_IOC_DIR (ctl
->ioctl_code
) & _IOC_READ
) != 0
1467 && copy_to_user (ctl
->data
, buf
, size
) != 0)
1474 static int proc_ioctl_default(struct dev_state
*ps
, void __user
*arg
)
1476 struct usbdevfs_ioctl ctrl
;
1478 if (copy_from_user(&ctrl
, arg
, sizeof (ctrl
)))
1480 return proc_ioctl(ps
, &ctrl
);
1483 #ifdef CONFIG_COMPAT
1484 static int proc_ioctl_compat(struct dev_state
*ps
, compat_uptr_t arg
)
1486 struct usbdevfs_ioctl32 __user
*uioc
;
1487 struct usbdevfs_ioctl ctrl
;
1490 uioc
= compat_ptr((long)arg
);
1491 if (get_user(ctrl
.ifno
, &uioc
->ifno
) ||
1492 get_user(ctrl
.ioctl_code
, &uioc
->ioctl_code
) ||
1493 __get_user(udata
, &uioc
->data
))
1495 ctrl
.data
= compat_ptr(udata
);
1497 return proc_ioctl(ps
, &ctrl
);
1502 * NOTE: All requests here that have interface numbers as parameters
1503 * are assuming that somehow the configuration has been prevented from
1504 * changing. But there's no mechanism to ensure that...
1506 static int usbdev_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1508 struct dev_state
*ps
= file
->private_data
;
1509 struct usb_device
*dev
= ps
->dev
;
1510 void __user
*p
= (void __user
*)arg
;
1513 if (!(file
->f_mode
& FMODE_WRITE
))
1515 usb_lock_device(dev
);
1516 if (!connected(ps
)) {
1517 usb_unlock_device(dev
);
1522 case USBDEVFS_CONTROL
:
1523 snoop(&dev
->dev
, "%s: CONTROL\n", __FUNCTION__
);
1524 ret
= proc_control(ps
, p
);
1526 inode
->i_mtime
= CURRENT_TIME
;
1530 snoop(&dev
->dev
, "%s: BULK\n", __FUNCTION__
);
1531 ret
= proc_bulk(ps
, p
);
1533 inode
->i_mtime
= CURRENT_TIME
;
1536 case USBDEVFS_RESETEP
:
1537 snoop(&dev
->dev
, "%s: RESETEP\n", __FUNCTION__
);
1538 ret
= proc_resetep(ps
, p
);
1540 inode
->i_mtime
= CURRENT_TIME
;
1543 case USBDEVFS_RESET
:
1544 snoop(&dev
->dev
, "%s: RESET\n", __FUNCTION__
);
1545 ret
= proc_resetdevice(ps
);
1548 case USBDEVFS_CLEAR_HALT
:
1549 snoop(&dev
->dev
, "%s: CLEAR_HALT\n", __FUNCTION__
);
1550 ret
= proc_clearhalt(ps
, p
);
1552 inode
->i_mtime
= CURRENT_TIME
;
1555 case USBDEVFS_GETDRIVER
:
1556 snoop(&dev
->dev
, "%s: GETDRIVER\n", __FUNCTION__
);
1557 ret
= proc_getdriver(ps
, p
);
1560 case USBDEVFS_CONNECTINFO
:
1561 snoop(&dev
->dev
, "%s: CONNECTINFO\n", __FUNCTION__
);
1562 ret
= proc_connectinfo(ps
, p
);
1565 case USBDEVFS_SETINTERFACE
:
1566 snoop(&dev
->dev
, "%s: SETINTERFACE\n", __FUNCTION__
);
1567 ret
= proc_setintf(ps
, p
);
1570 case USBDEVFS_SETCONFIGURATION
:
1571 snoop(&dev
->dev
, "%s: SETCONFIGURATION\n", __FUNCTION__
);
1572 ret
= proc_setconfig(ps
, p
);
1575 case USBDEVFS_SUBMITURB
:
1576 snoop(&dev
->dev
, "%s: SUBMITURB\n", __FUNCTION__
);
1577 ret
= proc_submiturb(ps
, p
);
1579 inode
->i_mtime
= CURRENT_TIME
;
1582 #ifdef CONFIG_COMPAT
1584 case USBDEVFS_SUBMITURB32
:
1585 snoop(&dev
->dev
, "%s: SUBMITURB32\n", __FUNCTION__
);
1586 ret
= proc_submiturb_compat(ps
, p
);
1588 inode
->i_mtime
= CURRENT_TIME
;
1591 case USBDEVFS_REAPURB32
:
1592 snoop(&dev
->dev
, "%s: REAPURB32\n", __FUNCTION__
);
1593 ret
= proc_reapurb_compat(ps
, p
);
1596 case USBDEVFS_REAPURBNDELAY32
:
1597 snoop(&dev
->dev
, "%s: REAPURBDELAY32\n", __FUNCTION__
);
1598 ret
= proc_reapurbnonblock_compat(ps
, p
);
1601 case USBDEVFS_IOCTL32
:
1602 snoop(&dev
->dev
, "%s: IOCTL\n", __FUNCTION__
);
1603 ret
= proc_ioctl_compat(ps
, ptr_to_compat(p
));
1607 case USBDEVFS_DISCARDURB
:
1608 snoop(&dev
->dev
, "%s: DISCARDURB\n", __FUNCTION__
);
1609 ret
= proc_unlinkurb(ps
, p
);
1612 case USBDEVFS_REAPURB
:
1613 snoop(&dev
->dev
, "%s: REAPURB\n", __FUNCTION__
);
1614 ret
= proc_reapurb(ps
, p
);
1617 case USBDEVFS_REAPURBNDELAY
:
1618 snoop(&dev
->dev
, "%s: REAPURBDELAY\n", __FUNCTION__
);
1619 ret
= proc_reapurbnonblock(ps
, p
);
1622 case USBDEVFS_DISCSIGNAL
:
1623 snoop(&dev
->dev
, "%s: DISCSIGNAL\n", __FUNCTION__
);
1624 ret
= proc_disconnectsignal(ps
, p
);
1627 case USBDEVFS_CLAIMINTERFACE
:
1628 snoop(&dev
->dev
, "%s: CLAIMINTERFACE\n", __FUNCTION__
);
1629 ret
= proc_claiminterface(ps
, p
);
1632 case USBDEVFS_RELEASEINTERFACE
:
1633 snoop(&dev
->dev
, "%s: RELEASEINTERFACE\n", __FUNCTION__
);
1634 ret
= proc_releaseinterface(ps
, p
);
1637 case USBDEVFS_IOCTL
:
1638 snoop(&dev
->dev
, "%s: IOCTL\n", __FUNCTION__
);
1639 ret
= proc_ioctl_default(ps
, p
);
1642 usb_unlock_device(dev
);
1644 inode
->i_atime
= CURRENT_TIME
;
1648 /* No kernel lock - fine */
1649 static unsigned int usbdev_poll(struct file
*file
, struct poll_table_struct
*wait
)
1651 struct dev_state
*ps
= file
->private_data
;
1652 unsigned int mask
= 0;
1654 poll_wait(file
, &ps
->wait
, wait
);
1655 if (file
->f_mode
& FMODE_WRITE
&& !list_empty(&ps
->async_completed
))
1656 mask
|= POLLOUT
| POLLWRNORM
;
1658 mask
|= POLLERR
| POLLHUP
;
1662 const struct file_operations usbdev_file_operations
= {
1663 .llseek
= usbdev_lseek
,
1664 .read
= usbdev_read
,
1665 .poll
= usbdev_poll
,
1666 .ioctl
= usbdev_ioctl
,
1667 .open
= usbdev_open
,
1668 .release
= usbdev_release
,
1671 static void usbdev_remove(struct usb_device
*udev
)
1673 struct dev_state
*ps
;
1674 struct siginfo sinfo
;
1676 while (!list_empty(&udev
->filelist
)) {
1677 ps
= list_entry(udev
->filelist
.next
, struct dev_state
, list
);
1678 destroy_all_async(ps
);
1679 wake_up_all(&ps
->wait
);
1680 list_del_init(&ps
->list
);
1681 if (ps
->discsignr
) {
1682 sinfo
.si_signo
= ps
->discsignr
;
1683 sinfo
.si_errno
= EPIPE
;
1684 sinfo
.si_code
= SI_ASYNCIO
;
1685 sinfo
.si_addr
= ps
->disccontext
;
1686 kill_pid_info_as_uid(ps
->discsignr
, &sinfo
,
1687 ps
->disc_pid
, ps
->disc_uid
,
1688 ps
->disc_euid
, ps
->secid
);
1693 #ifdef CONFIG_USB_DEVICE_CLASS
1694 static struct class *usb_classdev_class
;
1696 static int usb_classdev_add(struct usb_device
*dev
)
1698 struct device
*cldev
;
1700 cldev
= device_create(usb_classdev_class
, &dev
->dev
, dev
->dev
.devt
,
1701 "usbdev%d.%d", dev
->bus
->busnum
,
1704 return PTR_ERR(cldev
);
1705 dev
->usb_classdev
= cldev
;
1709 static void usb_classdev_remove(struct usb_device
*dev
)
1711 if (dev
->usb_classdev
)
1712 device_unregister(dev
->usb_classdev
);
1716 #define usb_classdev_add(dev) 0
1717 #define usb_classdev_remove(dev) do {} while (0)
1721 static int usbdev_notify(struct notifier_block
*self
,
1722 unsigned long action
, void *dev
)
1725 case USB_DEVICE_ADD
:
1726 if (usb_classdev_add(dev
))
1729 case USB_DEVICE_REMOVE
:
1730 usb_classdev_remove(dev
);
1737 static struct notifier_block usbdev_nb
= {
1738 .notifier_call
= usbdev_notify
,
1741 static struct cdev usb_device_cdev
= {
1742 .kobj
= {.name
= "usb_device", },
1743 .owner
= THIS_MODULE
,
1746 int __init
usb_devio_init(void)
1750 retval
= register_chrdev_region(USB_DEVICE_DEV
, USB_DEVICE_MAX
,
1753 err("unable to register minors for usb_device");
1756 cdev_init(&usb_device_cdev
, &usbdev_file_operations
);
1757 retval
= cdev_add(&usb_device_cdev
, USB_DEVICE_DEV
, USB_DEVICE_MAX
);
1759 err("unable to get usb_device major %d", USB_DEVICE_MAJOR
);
1762 #ifdef CONFIG_USB_DEVICE_CLASS
1763 usb_classdev_class
= class_create(THIS_MODULE
, "usb_device");
1764 if (IS_ERR(usb_classdev_class
)) {
1765 err("unable to register usb_device class");
1766 retval
= PTR_ERR(usb_classdev_class
);
1767 cdev_del(&usb_device_cdev
);
1768 usb_classdev_class
= NULL
;
1772 usb_register_notify(&usbdev_nb
);
1777 unregister_chrdev_region(USB_DEVICE_DEV
, USB_DEVICE_MAX
);
1781 void usb_devio_cleanup(void)
1783 usb_unregister_notify(&usbdev_nb
);
1784 #ifdef CONFIG_USB_DEVICE_CLASS
1785 class_destroy(usb_classdev_class
);
1787 cdev_del(&usb_device_cdev
);
1788 unregister_chrdev_region(USB_DEVICE_DEV
, USB_DEVICE_MAX
);