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 * my own sync control and bulk methods. Here to experiment
60 * and because the kernel ones set the process to TASK_UNINTERRUPTIBLE.
64 wait_queue_head_t wait
;
67 static void sync_completed(purb_t urb
)
69 struct sync
*s
= (struct sync
*)urb
->context
;
74 static int do_sync(purb_t urb
, int timeout
)
76 DECLARE_WAITQUEUE(wait
, current
);
83 init_waitqueue_head(&s
.wait
);
84 add_wait_queue(&s
.wait
, &wait
);
86 urb
->complete
= sync_completed
;
87 set_current_state(TASK_INTERRUPTIBLE
);
88 if ((ret
= usb_submit_urb(urb
)))
90 while (urb
->status
== -EINPROGRESS
) {
91 tmdiff
= tm
- jiffies
;
96 if (signal_pending(current
)) {
100 schedule_timeout(tmdiff
);
104 set_current_state(TASK_RUNNING
);
106 remove_wait_queue(&s
.wait
, &wait
);
110 static int my_usb_control_msg(struct usb_device
*dev
, unsigned int pipe
, __u8 request
, __u8 requesttype
,
111 __u16 value
, __u16 index
, void *data
, __u16 size
, int timeout
)
116 if (!(urb
= usb_alloc_urb(0)))
118 if (!(urb
->setup_packet
= kmalloc(8, GFP_KERNEL
))) {
122 urb
->setup_packet
[0] = requesttype
;
123 urb
->setup_packet
[1] = request
;
124 urb
->setup_packet
[2] = value
;
125 urb
->setup_packet
[3] = value
>> 8;
126 urb
->setup_packet
[4] = index
;
127 urb
->setup_packet
[5] = index
>> 8;
128 urb
->setup_packet
[6] = size
;
129 urb
->setup_packet
[7] = size
>> 8;
132 urb
->transfer_buffer
= data
;
133 urb
->transfer_buffer_length
= size
;
134 ret
= do_sync(urb
, timeout
);
136 // ret = urb->status;
138 ret
= urb
->actual_length
;
139 kfree(urb
->setup_packet
);
144 static int my_usb_bulk_msg(struct usb_device
*dev
, unsigned int pipe
,
145 void *data
, int len
, int *actual_length
, int timeout
)
150 if (!(urb
= usb_alloc_urb(0)))
154 urb
->transfer_buffer
= data
;
155 urb
->transfer_buffer_length
= len
;
156 ret
= do_sync(urb
, timeout
);
158 // ret = urb->status;
159 if (ret
>= 0 && actual_length
!= NULL
)
160 *actual_length
= urb
->actual_length
;
165 static long long usbdev_lseek(struct file
*file
, long long offset
, int orig
)
169 file
->f_pos
= offset
;
173 file
->f_pos
+= offset
;
184 static ssize_t
usbdev_read(struct file
*file
, char * buf
, size_t nbytes
, loff_t
*ppos
)
186 struct dev_state
*ps
= (struct dev_state
*)file
->private_data
;
192 down_read(&ps
->devsem
);
197 else if (pos
< sizeof(struct usb_device_descriptor
)) {
198 len
= sizeof(struct usb_device_descriptor
) - pos
;
201 if (copy_to_user(buf
, ((char *)&ps
->dev
->descriptor
) + pos
, len
))
210 up_read(&ps
->devsem
);
214 extern inline unsigned int ld2(unsigned int x
)
240 * async list handling
243 static struct async
*alloc_async(unsigned int numisoframes
)
245 unsigned int assize
= sizeof(struct async
) + numisoframes
* sizeof(iso_packet_descriptor_t
);
246 struct async
*as
= kmalloc(assize
, GFP_KERNEL
);
249 memset(as
, 0, assize
);
250 as
->urb
.number_of_packets
= numisoframes
;
254 static void free_async(struct async
*as
)
256 if (as
->urb
.transfer_buffer
)
257 kfree(as
->urb
.transfer_buffer
);
261 extern __inline__
void async_newpending(struct async
*as
)
263 struct dev_state
*ps
= as
->ps
;
266 spin_lock_irqsave(&ps
->lock
, flags
);
267 list_add_tail(&as
->asynclist
, &ps
->async_pending
);
268 spin_unlock_irqrestore(&ps
->lock
, flags
);
271 extern __inline__
void async_removepending(struct async
*as
)
273 struct dev_state
*ps
= as
->ps
;
276 spin_lock_irqsave(&ps
->lock
, flags
);
277 list_del(&as
->asynclist
);
278 INIT_LIST_HEAD(&as
->asynclist
);
279 spin_unlock_irqrestore(&ps
->lock
, flags
);
282 extern __inline__
struct async
*async_getcompleted(struct dev_state
*ps
)
285 struct async
*as
= NULL
;
287 spin_lock_irqsave(&ps
->lock
, flags
);
288 if (!list_empty(&ps
->async_completed
)) {
289 as
= list_entry(ps
->async_completed
.next
, struct async
, asynclist
);
290 list_del(&as
->asynclist
);
291 INIT_LIST_HEAD(&as
->asynclist
);
293 spin_unlock_irqrestore(&ps
->lock
, flags
);
297 extern __inline__
struct async
*async_getpending(struct dev_state
*ps
, void *userurb
)
303 spin_lock_irqsave(&ps
->lock
, flags
);
304 for (p
= ps
->async_pending
.next
; p
!= &ps
->async_pending
; ) {
305 as
= list_entry(p
, struct async
, asynclist
);
307 if (as
->userurb
!= userurb
)
309 list_del(&as
->asynclist
);
310 INIT_LIST_HEAD(&as
->asynclist
);
311 spin_unlock_irqrestore(&ps
->lock
, flags
);
314 spin_unlock_irqrestore(&ps
->lock
, flags
);
318 static void async_completed(purb_t urb
)
320 struct async
*as
= (struct async
*)urb
->context
;
321 struct dev_state
*ps
= as
->ps
;
322 struct siginfo sinfo
;
325 printk(KERN_DEBUG
"usbdevfs: async_completed: status %d errcount %d actlen %d pipe 0x%x\n",
326 urb
->status
, urb
->error_count
, urb
->actual_length
, urb
->pipe
);
328 spin_lock(&ps
->lock
);
329 list_del(&as
->asynclist
);
330 list_add_tail(&as
->asynclist
, &ps
->async_completed
);
331 spin_unlock(&ps
->lock
);
334 sinfo
.si_signo
= as
->signr
;
335 sinfo
.si_errno
= as
->urb
.status
;
336 sinfo
.si_code
= SI_ASYNCIO
;
337 sinfo
.si_addr
= as
->userurb
;
338 send_sig_info(as
->signr
, &sinfo
, as
->task
);
342 static void destroy_all_async(struct dev_state
*ps
)
347 spin_lock_irqsave(&ps
->lock
, flags
);
348 while (!list_empty(&ps
->async_pending
)) {
349 as
= list_entry(ps
->async_pending
.next
, struct async
, asynclist
);
350 list_del(&as
->asynclist
);
351 INIT_LIST_HEAD(&as
->asynclist
);
352 spin_unlock_irqrestore(&ps
->lock
, flags
);
353 /* usb_unlink_urb calls the completion handler with status == USB_ST_URB_KILLED */
354 usb_unlink_urb(&as
->urb
);
355 spin_lock_irqsave(&ps
->lock
, flags
);
357 spin_unlock_irqrestore(&ps
->lock
, flags
);
358 while ((as
= async_getcompleted(ps
)))
366 static void *driver_probe(struct usb_device
*dev
, unsigned int intf
)
371 static void driver_disconnect(struct usb_device
*dev
, void *context
)
373 struct dev_state
*ps
= (struct dev_state
*)context
;
378 struct usb_driver usbdevfs_driver
= {
382 LIST_HEAD_INIT(usbdevfs_driver
.driver_list
),
387 static int claimintf(struct dev_state
*ps
, unsigned int intf
)
389 struct usb_device
*dev
= ps
->dev
;
390 struct usb_interface
*iface
;
393 if (intf
>= 8*sizeof(ps
->ifclaimed
) || !dev
|| intf
>= dev
->actconfig
->bNumInterfaces
)
395 /* already claimed */
396 if (test_bit(intf
, &ps
->ifclaimed
))
398 iface
= &dev
->actconfig
->interface
[intf
];
401 if (!usb_interface_claimed(iface
)) {
402 usb_driver_claim_interface(&usbdevfs_driver
, iface
, ps
);
403 set_bit(intf
, &ps
->ifclaimed
);
410 static int releaseintf(struct dev_state
*ps
, unsigned int intf
)
412 struct usb_device
*dev
;
413 struct usb_interface
*iface
;
416 if (intf
>= 8*sizeof(ps
->ifclaimed
))
421 if (dev
&& test_and_clear_bit(intf
, &ps
->ifclaimed
)) {
422 iface
= &dev
->actconfig
->interface
[intf
];
423 usb_driver_release_interface(&usbdevfs_driver
, iface
);
430 static int checkintf(struct dev_state
*ps
, unsigned int intf
)
432 if (intf
>= 8*sizeof(ps
->ifclaimed
))
434 if (test_bit(intf
, &ps
->ifclaimed
))
436 /* if not yet claimed, claim it for the driver */
437 printk(KERN_WARNING
"usbdevfs: process %d (%s) did not claim interface %u before use\n",
438 current
->pid
, current
->comm
, intf
);
439 return claimintf(ps
, intf
);
442 static int findintfep(struct usb_device
*dev
, unsigned int ep
)
444 unsigned int i
, j
, e
;
445 struct usb_interface
*iface
;
446 struct usb_interface_descriptor
*alts
;
447 struct usb_endpoint_descriptor
*endpt
;
449 if (ep
& ~(USB_DIR_IN
|0xf))
451 for (i
= 0; i
< dev
->actconfig
->bNumInterfaces
; i
++) {
452 iface
= &dev
->actconfig
->interface
[i
];
453 for (j
= 0; j
< iface
->num_altsetting
; j
++) {
454 alts
= &iface
->altsetting
[j
];
455 for (e
= 0; e
< alts
->bNumEndpoints
; e
++) {
456 endpt
= &alts
->endpoint
[e
];
457 if (endpt
->bEndpointAddress
== ep
)
465 static int findintfif(struct usb_device
*dev
, unsigned int ifn
)
468 struct usb_interface
*iface
;
469 struct usb_interface_descriptor
*alts
;
473 for (i
= 0; i
< dev
->actconfig
->bNumInterfaces
; i
++) {
474 iface
= &dev
->actconfig
->interface
[i
];
475 for (j
= 0; j
< iface
->num_altsetting
; j
++) {
476 alts
= &iface
->altsetting
[j
];
477 if (alts
->bInterfaceNumber
== ifn
)
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
;
535 INIT_LIST_HEAD(&ps
->list
);
537 for (i
= 0; ps
->ifclaimed
&& i
< 8*sizeof(ps
->ifclaimed
); i
++)
538 if (test_bit(i
, &ps
->ifclaimed
))
542 destroy_all_async(ps
);
547 static int proc_control(struct dev_state
*ps
, void *arg
)
549 struct usb_device
*dev
= ps
->dev
;
550 struct usbdevfs_ctrltransfer ctrl
;
555 copy_from_user_ret(&ctrl
, (void *)arg
, sizeof(ctrl
), -EFAULT
);
556 switch (ctrl
.requesttype
& 0x1f) {
557 case USB_RECIP_ENDPOINT
:
558 if ((ret
= findintfep(ps
->dev
, ctrl
.index
& 0xff)) < 0)
560 if ((ret
= checkintf(ps
, ret
)))
564 case USB_RECIP_INTERFACE
:
565 if ((ret
= findintfif(ps
->dev
, ctrl
.index
& 0xff)) < 0)
567 if ((ret
= checkintf(ps
, ret
)))
571 if (ctrl
.length
> PAGE_SIZE
)
573 if (!(tbuf
= (unsigned char *)__get_free_page(GFP_KERNEL
)))
575 tmo
= (ctrl
.timeout
* HZ
+ 999) / 1000;
576 if (ctrl
.requesttype
& 0x80) {
577 if (ctrl
.length
&& !access_ok(VERIFY_WRITE
, ctrl
.data
, ctrl
.length
)) {
578 free_page((unsigned long)tbuf
);
581 i
= my_usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), ctrl
.request
, ctrl
.requesttype
,
582 ctrl
.value
, ctrl
.index
, tbuf
, ctrl
.length
, tmo
);
583 if ((i
> 0) && ctrl
.length
) {
584 copy_to_user_ret(ctrl
.data
, tbuf
, ctrl
.length
, -EFAULT
);
588 copy_from_user_ret(tbuf
, ctrl
.data
, ctrl
.length
, -EFAULT
);
590 i
= my_usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), ctrl
.request
, ctrl
.requesttype
,
591 ctrl
.value
, ctrl
.index
, tbuf
, ctrl
.length
, tmo
);
593 free_page((unsigned long)tbuf
);
595 printk(KERN_DEBUG
"usbdevfs: USBDEVFS_CONTROL failed dev %d rqt %u rq %u len %u ret %d\n",
596 dev
->devnum
, ctrl
.requesttype
, ctrl
.request
, ctrl
.length
, i
);
601 static int proc_bulk(struct dev_state
*ps
, void *arg
)
603 struct usb_device
*dev
= ps
->dev
;
604 struct usbdevfs_bulktransfer bulk
;
605 unsigned int tmo
, len1
, pipe
;
610 copy_from_user_ret(&bulk
, (void *)arg
, sizeof(bulk
), -EFAULT
);
611 if ((ret
= findintfep(ps
->dev
, bulk
.ep
)) < 0)
613 if ((ret
= checkintf(ps
, ret
)))
615 if (bulk
.ep
& USB_DIR_IN
)
616 pipe
= usb_rcvbulkpipe(dev
, bulk
.ep
& 0x7f);
618 pipe
= usb_sndbulkpipe(dev
, bulk
.ep
& 0x7f);
619 if (!usb_maxpacket(dev
, pipe
, !(bulk
.ep
& USB_DIR_IN
)))
622 if (len1
> PAGE_SIZE
)
624 if (!(tbuf
= (unsigned char *)__get_free_page(GFP_KERNEL
)))
626 tmo
= (bulk
.timeout
* HZ
+ 999) / 1000;
627 if (bulk
.ep
& 0x80) {
628 if (len1
&& !access_ok(VERIFY_WRITE
, bulk
.data
, len1
)) {
629 free_page((unsigned long)tbuf
);
632 i
= my_usb_bulk_msg(dev
, pipe
, tbuf
, len1
, &len2
, tmo
);
634 copy_to_user_ret(bulk
.data
, tbuf
, len2
, -EFAULT
);
638 copy_from_user_ret(tbuf
, bulk
.data
, len1
, -EFAULT
);
640 i
= my_usb_bulk_msg(dev
, pipe
, tbuf
, len1
, &len2
, tmo
);
642 free_page((unsigned long)tbuf
);
644 printk(KERN_WARNING
"usbdevfs: USBDEVFS_BULK failed dev %d ep 0x%x len %u ret %d\n",
645 dev
->devnum
, bulk
.ep
, bulk
.len
, i
);
651 static int proc_resetep(struct dev_state
*ps
, void *arg
)
656 get_user_ret(ep
, (unsigned int *)arg
, -EFAULT
);
657 if ((ret
= findintfep(ps
->dev
, ep
)) < 0)
659 if ((ret
= checkintf(ps
, ret
)))
661 usb_settoggle(ps
->dev
, ep
& 0xf, !(ep
& USB_DIR_IN
), 0);
665 static int proc_setintf(struct dev_state
*ps
, void *arg
)
667 struct usbdevfs_setinterface setintf
;
670 copy_from_user_ret(&setintf
, arg
, sizeof(setintf
), -EFAULT
);
671 if ((ret
= findintfif(ps
->dev
, setintf
.interface
)) < 0)
673 if ((ret
= checkintf(ps
, ret
)))
675 if (usb_set_interface(ps
->dev
, setintf
.interface
, setintf
.altsetting
))
680 static int proc_setconfig(struct dev_state
*ps
, void *arg
)
684 get_user_ret(u
, (unsigned int *)arg
, -EFAULT
);
685 if (usb_set_configuration(ps
->dev
, u
) < 0)
690 static int proc_submiturb(struct dev_state
*ps
, void *arg
)
692 struct usbdevfs_urb uurb
;
693 struct usbdevfs_iso_packet_desc
*isopkt
= NULL
;
695 unsigned int u
, totlen
, isofrmlen
;
698 copy_from_user_ret(&uurb
, arg
, sizeof(uurb
), -EFAULT
);
699 if (uurb
.flags
& ~(USBDEVFS_URB_ISO_ASAP
|USBDEVFS_URB_DISABLE_SPD
))
703 if (uurb
.signr
!= 0 && (uurb
.signr
< SIGRTMIN
|| uurb
.signr
> SIGRTMAX
))
705 if ((ret
= findintfep(ps
->dev
, uurb
.endpoint
)) < 0)
707 if ((ret
= checkintf(ps
, ret
)))
710 case USBDEVFS_URB_TYPE_BULK
:
711 uurb
.number_of_packets
= 0;
712 if (uurb
.buffer_length
> 16384)
714 if (!access_ok((uurb
.endpoint
& USB_DIR_IN
) ? VERIFY_WRITE
: VERIFY_READ
, uurb
.buffer
, uurb
.buffer_length
))
718 case USBDEVFS_URB_TYPE_ISO
:
719 /* arbitrary limit */
720 if (uurb
.number_of_packets
< 1 || uurb
.number_of_packets
> 128)
722 isofrmlen
= sizeof(struct usbdevfs_iso_packet_desc
) * uurb
.number_of_packets
;
723 if (!(isopkt
= kmalloc(isofrmlen
, GFP_KERNEL
)))
725 if (copy_from_user(isopkt
, &((struct usbdevfs_urb
*)arg
)->iso_frame_desc
, isofrmlen
)) {
729 for (totlen
= u
= 0; u
< uurb
.number_of_packets
; u
++) {
730 if (isopkt
[u
].length
> 1023) {
734 totlen
+= isopkt
[u
].length
;
736 if (totlen
> 32768) {
740 uurb
.buffer_length
= totlen
;
746 if (!(as
= alloc_async(uurb
.number_of_packets
))) {
751 if (!(as
->urb
.transfer_buffer
= kmalloc(uurb
.buffer_length
, GFP_KERNEL
))) {
758 as
->urb
.dev
= ps
->dev
;
759 as
->urb
.pipe
= (uurb
.type
<< 30) | __create_pipe(ps
->dev
, uurb
.endpoint
& 0xf) | (uurb
.endpoint
& USB_DIR_IN
);
760 as
->urb
.transfer_flags
= uurb
.flags
;
761 as
->urb
.transfer_buffer_length
= uurb
.buffer_length
;
762 as
->urb
.start_frame
= uurb
.start_frame
;
763 as
->urb
.number_of_packets
= uurb
.number_of_packets
;
764 as
->urb
.context
= as
;
765 as
->urb
.complete
= async_completed
;
766 for (totlen
= u
= 0; u
< uurb
.number_of_packets
; u
++) {
767 as
->urb
.iso_frame_desc
[u
].offset
= totlen
;
768 as
->urb
.iso_frame_desc
[u
].length
= isopkt
[u
].length
;
769 totlen
+= isopkt
[u
].length
;
775 if (uurb
.endpoint
& USB_DIR_IN
)
776 as
->userbuffer
= uurb
.buffer
;
778 as
->userbuffer
= NULL
;
779 as
->signr
= uurb
.signr
;
781 if (!(uurb
.endpoint
& USB_DIR_IN
)) {
782 if (copy_from_user(as
->urb
.transfer_buffer
, uurb
.buffer
, as
->urb
.transfer_buffer_length
)) {
787 async_newpending(as
);
788 if ((ret
= usb_submit_urb(&as
->urb
))) {
789 printk(KERN_DEBUG
"usbdevfs: usb_submit_urb returned %d\n", ret
);
790 async_removepending(as
);
797 static int proc_unlinkurb(struct dev_state
*ps
, void *arg
)
801 as
= async_getpending(ps
, arg
);
804 usb_unlink_urb(&as
->urb
);
808 static int processcompl(struct async
*as
)
813 if (copy_to_user(as
->userbuffer
, as
->urb
.transfer_buffer
, as
->urb
.transfer_buffer_length
))
815 put_user_ret(as
->urb
.status
, &((struct usbdevfs_urb
*)as
->userurb
)->status
, -EFAULT
);
816 put_user_ret(as
->urb
.actual_length
, &((struct usbdevfs_urb
*)as
->userurb
)->actual_length
, -EFAULT
);
817 put_user_ret(as
->urb
.error_count
, &((struct usbdevfs_urb
*)as
->userurb
)->error_count
, -EFAULT
);
818 if (!(usb_pipeisoc(as
->urb
.pipe
)))
820 for (i
= 0; i
< as
->urb
.number_of_packets
; i
++) {
821 put_user_ret(as
->urb
.iso_frame_desc
[i
].actual_length
,
822 &((struct usbdevfs_urb
*)as
->userurb
)->iso_frame_desc
[i
].actual_length
,
824 put_user_ret(as
->urb
.iso_frame_desc
[i
].status
,
825 &((struct usbdevfs_urb
*)as
->userurb
)->iso_frame_desc
[i
].status
,
831 static int proc_reapurb(struct dev_state
*ps
, void *arg
)
833 DECLARE_WAITQUEUE(wait
, current
);
834 struct async
*as
= NULL
;
838 add_wait_queue(&ps
->wait
, &wait
);
840 __set_current_state(TASK_INTERRUPTIBLE
);
841 if ((as
= async_getcompleted(ps
)))
843 if (signal_pending(current
))
845 up_read(&ps
->devsem
);
847 down_read(&ps
->devsem
);
849 remove_wait_queue(&ps
->wait
, &wait
);
850 set_current_state(TASK_RUNNING
);
852 ret
= processcompl(as
);
857 put_user_ret(addr
, (void **)arg
, -EFAULT
);
860 if (signal_pending(current
))
865 static int proc_reapurbnonblock(struct dev_state
*ps
, void *arg
)
871 if (!(as
= async_getcompleted(ps
)))
873 ret
= processcompl(as
);
878 put_user_ret(addr
, (void **)arg
, -EFAULT
);
882 static int proc_disconnectsignal(struct dev_state
*ps
, void *arg
)
884 struct usbdevfs_disconnectsignal ds
;
886 copy_from_user_ret(&ds
, arg
, sizeof(ds
), -EFAULT
);
887 if (ds
.signr
!= 0 && (ds
.signr
< SIGRTMIN
|| ds
.signr
> SIGRTMAX
))
889 ps
->discsignr
= ds
.signr
;
890 ps
->disccontext
= ds
.context
;
894 static int proc_claiminterface(struct dev_state
*ps
, void *arg
)
899 get_user_ret(intf
, (unsigned int *)arg
, -EFAULT
);
900 if ((ret
= findintfif(ps
->dev
, intf
)) < 0)
902 return claimintf(ps
, ret
);
905 static int proc_releaseinterface(struct dev_state
*ps
, void *arg
)
910 get_user_ret(intf
, (unsigned int *)arg
, -EFAULT
);
911 if ((ret
= findintfif(ps
->dev
, intf
)) < 0)
913 return releaseintf(ps
, intf
);
916 static int usbdev_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
918 struct dev_state
*ps
= (struct dev_state
*)file
->private_data
;
919 int ret
= -ENOIOCTLCMD
;
921 if (!(file
->f_mode
& FMODE_WRITE
))
923 down_read(&ps
->devsem
);
925 up_read(&ps
->devsem
);
929 case USBDEVFS_CONTROL
:
930 ret
= proc_control(ps
, (void *)arg
);
932 inode
->i_mtime
= CURRENT_TIME
;
936 ret
= proc_bulk(ps
, (void *)arg
);
938 inode
->i_mtime
= CURRENT_TIME
;
941 case USBDEVFS_RESETEP
:
942 ret
= proc_resetep(ps
, (void *)arg
);
944 inode
->i_mtime
= CURRENT_TIME
;
947 case USBDEVFS_SETINTERFACE
:
948 ret
= proc_setintf(ps
, (void *)arg
);
951 case USBDEVFS_SETCONFIGURATION
:
952 ret
= proc_setconfig(ps
, (void *)arg
);
955 case USBDEVFS_SUBMITURB
:
956 ret
= proc_submiturb(ps
, (void *)arg
);
958 inode
->i_mtime
= CURRENT_TIME
;
961 case USBDEVFS_DISCARDURB
:
962 ret
= proc_unlinkurb(ps
, (void *)arg
);
965 case USBDEVFS_REAPURB
:
966 ret
= proc_reapurb(ps
, (void *)arg
);
969 case USBDEVFS_REAPURBNDELAY
:
970 ret
= proc_reapurbnonblock(ps
, (void *)arg
);
973 case USBDEVFS_DISCSIGNAL
:
974 ret
= proc_disconnectsignal(ps
, (void *)arg
);
977 case USBDEVFS_CLAIMINTERFACE
:
978 ret
= proc_claiminterface(ps
, (void *)arg
);
981 case USBDEVFS_RELEASEINTERFACE
:
982 ret
= proc_releaseinterface(ps
, (void *)arg
);
986 up_read(&ps
->devsem
);
988 inode
->i_atime
= CURRENT_TIME
;
992 static unsigned int usbdev_poll(struct file
*file
, struct poll_table_struct
*wait
)
994 struct dev_state
*ps
= (struct dev_state
*)file
->private_data
;
995 unsigned int mask
= 0;
997 poll_wait(file
, &ps
->wait
, wait
);
998 if (file
->f_mode
& FMODE_WRITE
&& !list_empty(&ps
->async_completed
))
999 mask
|= POLLOUT
| POLLWRNORM
;
1001 mask
|= POLLERR
| POLLHUP
;
1005 struct file_operations usbdevfs_device_file_operations
= {
1006 llseek
: usbdev_lseek
,
1009 ioctl
: usbdev_ioctl
,
1011 release
: usbdev_release
,