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 loff_t
usbdev_lseek(struct file
*file
, loff_t 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
;
193 down_read(&ps
->devsem
);
197 } else if (pos
< 0) {
202 if (pos
< sizeof(struct usb_device_descriptor
)) {
203 len
= sizeof(struct usb_device_descriptor
) - pos
;
206 if (copy_to_user(buf
, ((char *)&ps
->dev
->descriptor
) + pos
, len
)) {
217 pos
= sizeof(struct usb_device_descriptor
);
218 for (i
= 0; nbytes
&& i
< ps
->dev
->descriptor
.bNumConfigurations
; i
++) {
219 struct usb_config_descriptor
*config
=
220 (struct usb_config_descriptor
*)ps
->dev
->rawdescriptors
[i
];
221 unsigned int length
= le16_to_cpu(config
->wTotalLength
);
223 if (*ppos
< pos
+ length
) {
224 len
= length
- (*ppos
- pos
);
228 if (copy_to_user(buf
,
229 ps
->dev
->rawdescriptors
[i
] + (*ppos
- pos
), len
)) {
244 up_read(&ps
->devsem
);
248 extern inline unsigned int ld2(unsigned int x
)
274 * async list handling
277 static struct async
*alloc_async(unsigned int numisoframes
)
279 unsigned int assize
= sizeof(struct async
) + numisoframes
* sizeof(iso_packet_descriptor_t
);
280 struct async
*as
= kmalloc(assize
, GFP_KERNEL
);
283 memset(as
, 0, assize
);
284 as
->urb
.number_of_packets
= numisoframes
;
288 static void free_async(struct async
*as
)
290 if (as
->urb
.transfer_buffer
)
291 kfree(as
->urb
.transfer_buffer
);
295 extern __inline__
void async_newpending(struct async
*as
)
297 struct dev_state
*ps
= as
->ps
;
300 spin_lock_irqsave(&ps
->lock
, flags
);
301 list_add_tail(&as
->asynclist
, &ps
->async_pending
);
302 spin_unlock_irqrestore(&ps
->lock
, flags
);
305 extern __inline__
void async_removepending(struct async
*as
)
307 struct dev_state
*ps
= as
->ps
;
310 spin_lock_irqsave(&ps
->lock
, flags
);
311 list_del(&as
->asynclist
);
312 INIT_LIST_HEAD(&as
->asynclist
);
313 spin_unlock_irqrestore(&ps
->lock
, flags
);
316 extern __inline__
struct async
*async_getcompleted(struct dev_state
*ps
)
319 struct async
*as
= NULL
;
321 spin_lock_irqsave(&ps
->lock
, flags
);
322 if (!list_empty(&ps
->async_completed
)) {
323 as
= list_entry(ps
->async_completed
.next
, struct async
, asynclist
);
324 list_del(&as
->asynclist
);
325 INIT_LIST_HEAD(&as
->asynclist
);
327 spin_unlock_irqrestore(&ps
->lock
, flags
);
331 extern __inline__
struct async
*async_getpending(struct dev_state
*ps
, void *userurb
)
337 spin_lock_irqsave(&ps
->lock
, flags
);
338 for (p
= ps
->async_pending
.next
; p
!= &ps
->async_pending
; ) {
339 as
= list_entry(p
, struct async
, asynclist
);
341 if (as
->userurb
!= userurb
)
343 list_del(&as
->asynclist
);
344 INIT_LIST_HEAD(&as
->asynclist
);
345 spin_unlock_irqrestore(&ps
->lock
, flags
);
348 spin_unlock_irqrestore(&ps
->lock
, flags
);
352 static void async_completed(purb_t urb
)
354 struct async
*as
= (struct async
*)urb
->context
;
355 struct dev_state
*ps
= as
->ps
;
356 struct siginfo sinfo
;
359 printk(KERN_DEBUG
"usbdevfs: async_completed: status %d errcount %d actlen %d pipe 0x%x\n",
360 urb
->status
, urb
->error_count
, urb
->actual_length
, urb
->pipe
);
362 spin_lock(&ps
->lock
);
363 list_del(&as
->asynclist
);
364 list_add_tail(&as
->asynclist
, &ps
->async_completed
);
365 spin_unlock(&ps
->lock
);
368 sinfo
.si_signo
= as
->signr
;
369 sinfo
.si_errno
= as
->urb
.status
;
370 sinfo
.si_code
= SI_ASYNCIO
;
371 sinfo
.si_addr
= as
->userurb
;
372 send_sig_info(as
->signr
, &sinfo
, as
->task
);
376 static void destroy_all_async(struct dev_state
*ps
)
381 spin_lock_irqsave(&ps
->lock
, flags
);
382 while (!list_empty(&ps
->async_pending
)) {
383 as
= list_entry(ps
->async_pending
.next
, struct async
, asynclist
);
384 list_del(&as
->asynclist
);
385 INIT_LIST_HEAD(&as
->asynclist
);
386 spin_unlock_irqrestore(&ps
->lock
, flags
);
387 /* usb_unlink_urb calls the completion handler with status == USB_ST_URB_KILLED */
388 usb_unlink_urb(&as
->urb
);
389 spin_lock_irqsave(&ps
->lock
, flags
);
391 spin_unlock_irqrestore(&ps
->lock
, flags
);
392 while ((as
= async_getcompleted(ps
)))
400 static void *driver_probe(struct usb_device
*dev
, unsigned int intf
)
405 static void driver_disconnect(struct usb_device
*dev
, void *context
)
407 struct dev_state
*ps
= (struct dev_state
*)context
;
412 struct usb_driver usbdevfs_driver
= {
415 disconnect
: driver_disconnect
,
418 static int claimintf(struct dev_state
*ps
, unsigned int intf
)
420 struct usb_device
*dev
= ps
->dev
;
421 struct usb_interface
*iface
;
424 if (intf
>= 8*sizeof(ps
->ifclaimed
) || !dev
|| intf
>= dev
->actconfig
->bNumInterfaces
)
426 /* already claimed */
427 if (test_bit(intf
, &ps
->ifclaimed
))
429 iface
= &dev
->actconfig
->interface
[intf
];
432 if (!usb_interface_claimed(iface
)) {
433 usb_driver_claim_interface(&usbdevfs_driver
, iface
, ps
);
434 set_bit(intf
, &ps
->ifclaimed
);
441 static int releaseintf(struct dev_state
*ps
, unsigned int intf
)
443 struct usb_device
*dev
;
444 struct usb_interface
*iface
;
447 if (intf
>= 8*sizeof(ps
->ifclaimed
))
452 if (dev
&& test_and_clear_bit(intf
, &ps
->ifclaimed
)) {
453 iface
= &dev
->actconfig
->interface
[intf
];
454 usb_driver_release_interface(&usbdevfs_driver
, iface
);
461 static int checkintf(struct dev_state
*ps
, unsigned int intf
)
463 if (intf
>= 8*sizeof(ps
->ifclaimed
))
465 if (test_bit(intf
, &ps
->ifclaimed
))
467 /* if not yet claimed, claim it for the driver */
468 printk(KERN_WARNING
"usbdevfs: process %d (%s) did not claim interface %u before use\n",
469 current
->pid
, current
->comm
, intf
);
470 return claimintf(ps
, intf
);
473 static int findintfep(struct usb_device
*dev
, unsigned int ep
)
475 unsigned int i
, j
, e
;
476 struct usb_interface
*iface
;
477 struct usb_interface_descriptor
*alts
;
478 struct usb_endpoint_descriptor
*endpt
;
480 if (ep
& ~(USB_DIR_IN
|0xf))
482 for (i
= 0; i
< dev
->actconfig
->bNumInterfaces
; i
++) {
483 iface
= &dev
->actconfig
->interface
[i
];
484 for (j
= 0; j
< iface
->num_altsetting
; j
++) {
485 alts
= &iface
->altsetting
[j
];
486 for (e
= 0; e
< alts
->bNumEndpoints
; e
++) {
487 endpt
= &alts
->endpoint
[e
];
488 if (endpt
->bEndpointAddress
== ep
)
496 static int findintfif(struct usb_device
*dev
, unsigned int ifn
)
499 struct usb_interface
*iface
;
500 struct usb_interface_descriptor
*alts
;
504 for (i
= 0; i
< dev
->actconfig
->bNumInterfaces
; i
++) {
505 iface
= &dev
->actconfig
->interface
[i
];
506 for (j
= 0; j
< iface
->num_altsetting
; j
++) {
507 alts
= &iface
->altsetting
[j
];
508 if (alts
->bInterfaceNumber
== ifn
)
515 extern struct list_head usb_driver_list
;
517 static int finddriver(struct usb_driver
**driver
, char *name
)
519 struct list_head
*tmp
;
521 tmp
= usb_driver_list
.next
;
522 while (tmp
!= &usb_driver_list
) {
523 struct usb_driver
*d
= list_entry(tmp
, struct usb_driver
,
526 if (!strcmp(d
->name
, name
)) {
540 static int usbdev_open(struct inode
*inode
, struct file
*file
)
542 struct usb_device
*dev
;
543 struct dev_state
*ps
;
547 * no locking necessary here, as both sys_open (actually filp_open)
548 * and the hub thread have the kernel lock
549 * (still acquire the kernel lock for safety)
553 if (ITYPE(inode
->i_ino
) != IDEVICE
)
555 dev
= inode
->u
.usbdev_i
.p
.dev
;
559 if (!(ps
= kmalloc(sizeof(struct dev_state
), GFP_KERNEL
)))
564 spin_lock_init(&ps
->lock
);
565 INIT_LIST_HEAD(&ps
->async_pending
);
566 INIT_LIST_HEAD(&ps
->async_completed
);
567 init_waitqueue_head(&ps
->wait
);
568 init_rwsem(&ps
->devsem
);
570 ps
->disctask
= current
;
571 ps
->disccontext
= NULL
;
574 list_add_tail(&ps
->list
, &dev
->filelist
);
575 file
->private_data
= ps
;
581 static int usbdev_release(struct inode
*inode
, struct file
*file
)
583 struct dev_state
*ps
= (struct dev_state
*)file
->private_data
;
588 INIT_LIST_HEAD(&ps
->list
);
590 for (i
= 0; ps
->ifclaimed
&& i
< 8*sizeof(ps
->ifclaimed
); i
++)
591 if (test_bit(i
, &ps
->ifclaimed
))
595 destroy_all_async(ps
);
600 static int proc_control(struct dev_state
*ps
, void *arg
)
602 struct usb_device
*dev
= ps
->dev
;
603 struct usbdevfs_ctrltransfer ctrl
;
608 copy_from_user_ret(&ctrl
, (void *)arg
, sizeof(ctrl
), -EFAULT
);
609 switch (ctrl
.requesttype
& 0x1f) {
610 case USB_RECIP_ENDPOINT
:
611 if ((ret
= findintfep(ps
->dev
, ctrl
.index
& 0xff)) < 0)
613 if ((ret
= checkintf(ps
, ret
)))
617 case USB_RECIP_INTERFACE
:
618 if ((ret
= findintfif(ps
->dev
, ctrl
.index
& 0xff)) < 0)
620 if ((ret
= checkintf(ps
, ret
)))
624 if (ctrl
.length
> PAGE_SIZE
)
626 if (!(tbuf
= (unsigned char *)__get_free_page(GFP_KERNEL
)))
628 tmo
= (ctrl
.timeout
* HZ
+ 999) / 1000;
629 if (ctrl
.requesttype
& 0x80) {
630 if (ctrl
.length
&& !access_ok(VERIFY_WRITE
, ctrl
.data
, ctrl
.length
)) {
631 free_page((unsigned long)tbuf
);
634 i
= my_usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), ctrl
.request
, ctrl
.requesttype
,
635 ctrl
.value
, ctrl
.index
, tbuf
, ctrl
.length
, tmo
);
636 if ((i
> 0) && ctrl
.length
) {
637 copy_to_user_ret(ctrl
.data
, tbuf
, ctrl
.length
, -EFAULT
);
641 copy_from_user_ret(tbuf
, ctrl
.data
, ctrl
.length
, -EFAULT
);
643 i
= my_usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), ctrl
.request
, ctrl
.requesttype
,
644 ctrl
.value
, ctrl
.index
, tbuf
, ctrl
.length
, tmo
);
646 free_page((unsigned long)tbuf
);
648 printk(KERN_DEBUG
"usbdevfs: USBDEVFS_CONTROL failed dev %d rqt %u rq %u len %u ret %d\n",
649 dev
->devnum
, ctrl
.requesttype
, ctrl
.request
, ctrl
.length
, i
);
654 static int proc_bulk(struct dev_state
*ps
, void *arg
)
656 struct usb_device
*dev
= ps
->dev
;
657 struct usbdevfs_bulktransfer bulk
;
658 unsigned int tmo
, len1
, pipe
;
663 copy_from_user_ret(&bulk
, (void *)arg
, sizeof(bulk
), -EFAULT
);
664 if ((ret
= findintfep(ps
->dev
, bulk
.ep
)) < 0)
666 if ((ret
= checkintf(ps
, ret
)))
668 if (bulk
.ep
& USB_DIR_IN
)
669 pipe
= usb_rcvbulkpipe(dev
, bulk
.ep
& 0x7f);
671 pipe
= usb_sndbulkpipe(dev
, bulk
.ep
& 0x7f);
672 if (!usb_maxpacket(dev
, pipe
, !(bulk
.ep
& USB_DIR_IN
)))
675 if (len1
> PAGE_SIZE
)
677 if (!(tbuf
= (unsigned char *)__get_free_page(GFP_KERNEL
)))
679 tmo
= (bulk
.timeout
* HZ
+ 999) / 1000;
680 if (bulk
.ep
& 0x80) {
681 if (len1
&& !access_ok(VERIFY_WRITE
, bulk
.data
, len1
)) {
682 free_page((unsigned long)tbuf
);
685 i
= my_usb_bulk_msg(dev
, pipe
, tbuf
, len1
, &len2
, tmo
);
687 copy_to_user_ret(bulk
.data
, tbuf
, len2
, -EFAULT
);
691 copy_from_user_ret(tbuf
, bulk
.data
, len1
, -EFAULT
);
693 i
= my_usb_bulk_msg(dev
, pipe
, tbuf
, len1
, &len2
, tmo
);
695 free_page((unsigned long)tbuf
);
697 printk(KERN_WARNING
"usbdevfs: USBDEVFS_BULK failed dev %d ep 0x%x len %u ret %d\n",
698 dev
->devnum
, bulk
.ep
, bulk
.len
, i
);
704 static int proc_resetep(struct dev_state
*ps
, void *arg
)
709 get_user_ret(ep
, (unsigned int *)arg
, -EFAULT
);
710 if ((ret
= findintfep(ps
->dev
, ep
)) < 0)
712 if ((ret
= checkintf(ps
, ret
)))
714 usb_settoggle(ps
->dev
, ep
& 0xf, !(ep
& USB_DIR_IN
), 0);
718 static int proc_getdriver(struct dev_state
*ps
, void *arg
)
720 struct usbdevfs_getdriver gd
;
721 struct usb_interface
*interface
;
724 copy_from_user_ret(&gd
, arg
, sizeof(gd
), -EFAULT
);
725 if ((ret
= findintfif(ps
->dev
, gd
.interface
)) < 0)
727 interface
= usb_ifnum_to_if(ps
->dev
, gd
.interface
);
730 if (!interface
->driver
)
732 strcpy(gd
.driver
, interface
->driver
->name
);
733 copy_to_user_ret(arg
, &gd
, sizeof(gd
), -EFAULT
);
737 static int proc_connectinfo(struct dev_state
*ps
, void *arg
)
739 struct usbdevfs_connectinfo ci
;
741 ci
.devnum
= ps
->dev
->devnum
;
742 ci
.slow
= ps
->dev
->slow
;
743 copy_to_user_ret(arg
, &ci
, sizeof(ci
), -EFAULT
);
747 static int proc_resetdevice(struct dev_state
*ps
)
751 ret
= usb_reset_device(ps
->dev
);
755 for (i
= 0; i
< ps
->dev
->actconfig
->bNumInterfaces
; i
++) {
756 struct usb_interface
*intf
= &ps
->dev
->actconfig
->interface
[i
];
758 /* Don't simulate interfaces we've claimed */
759 if (test_bit(i
, &ps
->ifclaimed
))
763 down(&intf
->driver
->serialize
);
764 intf
->driver
->disconnect(ps
->dev
, intf
->private_data
);
765 intf
->driver
->probe(ps
->dev
, i
);
766 up(&intf
->driver
->serialize
);
773 static int proc_setintf(struct dev_state
*ps
, void *arg
)
775 struct usbdevfs_setinterface setintf
;
776 struct usb_interface
*interface
;
779 copy_from_user_ret(&setintf
, arg
, sizeof(setintf
), -EFAULT
);
780 if ((ret
= findintfif(ps
->dev
, setintf
.interface
)) < 0)
782 interface
= usb_ifnum_to_if(ps
->dev
, setintf
.interface
);
785 if (interface
->driver
) {
786 if ((ret
= checkintf(ps
, ret
)))
789 if (usb_set_interface(ps
->dev
, setintf
.interface
, setintf
.altsetting
))
794 static int proc_setconfig(struct dev_state
*ps
, void *arg
)
798 get_user_ret(u
, (unsigned int *)arg
, -EFAULT
);
799 if (usb_set_configuration(ps
->dev
, u
) < 0)
804 static int proc_submiturb(struct dev_state
*ps
, void *arg
)
806 struct usbdevfs_urb uurb
;
807 struct usbdevfs_iso_packet_desc
*isopkt
= NULL
;
809 unsigned int u
, totlen
, isofrmlen
;
812 copy_from_user_ret(&uurb
, arg
, sizeof(uurb
), -EFAULT
);
813 if (uurb
.flags
& ~(USBDEVFS_URB_ISO_ASAP
|USBDEVFS_URB_DISABLE_SPD
))
817 if (uurb
.signr
!= 0 && (uurb
.signr
< SIGRTMIN
|| uurb
.signr
> SIGRTMAX
))
819 if ((ret
= findintfep(ps
->dev
, uurb
.endpoint
)) < 0)
821 if ((ret
= checkintf(ps
, ret
)))
824 case USBDEVFS_URB_TYPE_BULK
:
825 uurb
.number_of_packets
= 0;
826 if (uurb
.buffer_length
> 16384)
828 if (!access_ok((uurb
.endpoint
& USB_DIR_IN
) ? VERIFY_WRITE
: VERIFY_READ
, uurb
.buffer
, uurb
.buffer_length
))
832 case USBDEVFS_URB_TYPE_ISO
:
833 /* arbitrary limit */
834 if (uurb
.number_of_packets
< 1 || uurb
.number_of_packets
> 128)
836 isofrmlen
= sizeof(struct usbdevfs_iso_packet_desc
) * uurb
.number_of_packets
;
837 if (!(isopkt
= kmalloc(isofrmlen
, GFP_KERNEL
)))
839 if (copy_from_user(isopkt
, &((struct usbdevfs_urb
*)arg
)->iso_frame_desc
, isofrmlen
)) {
843 for (totlen
= u
= 0; u
< uurb
.number_of_packets
; u
++) {
844 if (isopkt
[u
].length
> 1023) {
848 totlen
+= isopkt
[u
].length
;
850 if (totlen
> 32768) {
854 uurb
.buffer_length
= totlen
;
860 if (!(as
= alloc_async(uurb
.number_of_packets
))) {
865 if (!(as
->urb
.transfer_buffer
= kmalloc(uurb
.buffer_length
, GFP_KERNEL
))) {
872 as
->urb
.dev
= ps
->dev
;
873 as
->urb
.pipe
= (uurb
.type
<< 30) | __create_pipe(ps
->dev
, uurb
.endpoint
& 0xf) | (uurb
.endpoint
& USB_DIR_IN
);
874 as
->urb
.transfer_flags
= uurb
.flags
;
875 as
->urb
.transfer_buffer_length
= uurb
.buffer_length
;
876 as
->urb
.start_frame
= uurb
.start_frame
;
877 as
->urb
.number_of_packets
= uurb
.number_of_packets
;
878 as
->urb
.context
= as
;
879 as
->urb
.complete
= async_completed
;
880 for (totlen
= u
= 0; u
< uurb
.number_of_packets
; u
++) {
881 as
->urb
.iso_frame_desc
[u
].offset
= totlen
;
882 as
->urb
.iso_frame_desc
[u
].length
= isopkt
[u
].length
;
883 totlen
+= isopkt
[u
].length
;
889 if (uurb
.endpoint
& USB_DIR_IN
)
890 as
->userbuffer
= uurb
.buffer
;
892 as
->userbuffer
= NULL
;
893 as
->signr
= uurb
.signr
;
895 if (!(uurb
.endpoint
& USB_DIR_IN
)) {
896 if (copy_from_user(as
->urb
.transfer_buffer
, uurb
.buffer
, as
->urb
.transfer_buffer_length
)) {
901 async_newpending(as
);
902 if ((ret
= usb_submit_urb(&as
->urb
))) {
903 printk(KERN_DEBUG
"usbdevfs: usb_submit_urb returned %d\n", ret
);
904 async_removepending(as
);
911 static int proc_unlinkurb(struct dev_state
*ps
, void *arg
)
915 as
= async_getpending(ps
, arg
);
918 usb_unlink_urb(&as
->urb
);
922 static int processcompl(struct async
*as
)
927 if (copy_to_user(as
->userbuffer
, as
->urb
.transfer_buffer
, as
->urb
.transfer_buffer_length
))
929 put_user_ret(as
->urb
.status
, &((struct usbdevfs_urb
*)as
->userurb
)->status
, -EFAULT
);
930 put_user_ret(as
->urb
.actual_length
, &((struct usbdevfs_urb
*)as
->userurb
)->actual_length
, -EFAULT
);
931 put_user_ret(as
->urb
.error_count
, &((struct usbdevfs_urb
*)as
->userurb
)->error_count
, -EFAULT
);
932 if (!(usb_pipeisoc(as
->urb
.pipe
)))
934 for (i
= 0; i
< as
->urb
.number_of_packets
; i
++) {
935 put_user_ret(as
->urb
.iso_frame_desc
[i
].actual_length
,
936 &((struct usbdevfs_urb
*)as
->userurb
)->iso_frame_desc
[i
].actual_length
,
938 put_user_ret(as
->urb
.iso_frame_desc
[i
].status
,
939 &((struct usbdevfs_urb
*)as
->userurb
)->iso_frame_desc
[i
].status
,
945 static int proc_reapurb(struct dev_state
*ps
, void *arg
)
947 DECLARE_WAITQUEUE(wait
, current
);
948 struct async
*as
= NULL
;
952 add_wait_queue(&ps
->wait
, &wait
);
954 __set_current_state(TASK_INTERRUPTIBLE
);
955 if ((as
= async_getcompleted(ps
)))
957 if (signal_pending(current
))
959 up_read(&ps
->devsem
);
961 down_read(&ps
->devsem
);
963 remove_wait_queue(&ps
->wait
, &wait
);
964 set_current_state(TASK_RUNNING
);
966 ret
= processcompl(as
);
971 put_user_ret(addr
, (void **)arg
, -EFAULT
);
974 if (signal_pending(current
))
979 static int proc_reapurbnonblock(struct dev_state
*ps
, void *arg
)
985 if (!(as
= async_getcompleted(ps
)))
987 ret
= processcompl(as
);
992 put_user_ret(addr
, (void **)arg
, -EFAULT
);
996 static int proc_disconnectsignal(struct dev_state
*ps
, void *arg
)
998 struct usbdevfs_disconnectsignal ds
;
1000 copy_from_user_ret(&ds
, arg
, sizeof(ds
), -EFAULT
);
1001 if (ds
.signr
!= 0 && (ds
.signr
< SIGRTMIN
|| ds
.signr
> SIGRTMAX
))
1003 ps
->discsignr
= ds
.signr
;
1004 ps
->disccontext
= ds
.context
;
1008 static int proc_claiminterface(struct dev_state
*ps
, void *arg
)
1013 get_user_ret(intf
, (unsigned int *)arg
, -EFAULT
);
1014 if ((ret
= findintfif(ps
->dev
, intf
)) < 0)
1016 return claimintf(ps
, ret
);
1019 static int proc_releaseinterface(struct dev_state
*ps
, void *arg
)
1024 get_user_ret(intf
, (unsigned int *)arg
, -EFAULT
);
1025 if ((ret
= findintfif(ps
->dev
, intf
)) < 0)
1027 return releaseintf(ps
, intf
);
1030 static int proc_ioctl (struct dev_state
*ps
, void *arg
)
1032 struct usbdevfs_ioctl ctrl
;
1037 /* get input parameters and alloc buffer */
1038 copy_from_user_ret (&ctrl
, (void *) arg
, sizeof (ctrl
), -EFAULT
);
1039 if ((size
= _IOC_SIZE (ctrl
.ioctl_code
)) > 0) {
1040 if ((buf
= kmalloc (size
, GFP_KERNEL
)) == 0)
1042 if ((_IOC_DIR (ctrl
.ioctl_code
) & _IOC_WRITE
) != 0
1043 && copy_from_user (buf
, ctrl
.data
, size
) != 0) {
1047 memset (arg
, 0, size
);
1050 /* ioctl to device */
1051 if (ctrl
.ifno
< 0) {
1052 switch (ctrl
.ioctl_code
) {
1053 /* access/release token for issuing control messages
1054 * ask a particular driver to bind/unbind, ... etc
1059 /* ioctl to the driver which has claimed a given interface */
1061 struct usb_interface
*ifp
= 0;
1064 else if (ctrl
.ifno
>= ps
->dev
->actconfig
->bNumInterfaces
)
1067 if (!(ifp
= usb_ifnum_to_if (ps
->dev
, ctrl
.ifno
)))
1069 else if (ifp
->driver
== 0 || ifp
->driver
->ioctl
== 0)
1073 /* ifno might usefully be passed ... */
1074 retval
= ifp
->driver
->ioctl (ps
->dev
, ctrl
.ioctl_code
, buf
);
1075 /* size = min (size, retval)? */
1078 /* cleanup and return */
1080 && (_IOC_DIR (ctrl
.ioctl_code
) & _IOC_READ
) != 0
1082 && copy_to_user (ctrl
.data
, buf
, size
) != 0)
1089 static int usbdev_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1091 struct dev_state
*ps
= (struct dev_state
*)file
->private_data
;
1092 int ret
= -ENOIOCTLCMD
;
1094 if (!(file
->f_mode
& FMODE_WRITE
))
1096 down_read(&ps
->devsem
);
1098 up_read(&ps
->devsem
);
1102 case USBDEVFS_CONTROL
:
1103 ret
= proc_control(ps
, (void *)arg
);
1105 inode
->i_mtime
= CURRENT_TIME
;
1109 ret
= proc_bulk(ps
, (void *)arg
);
1111 inode
->i_mtime
= CURRENT_TIME
;
1114 case USBDEVFS_RESETEP
:
1115 ret
= proc_resetep(ps
, (void *)arg
);
1117 inode
->i_mtime
= CURRENT_TIME
;
1120 case USBDEVFS_RESET
:
1121 ret
= proc_resetdevice(ps
);
1124 case USBDEVFS_GETDRIVER
:
1125 ret
= proc_getdriver(ps
, (void *)arg
);
1128 case USBDEVFS_CONNECTINFO
:
1129 ret
= proc_connectinfo(ps
, (void *)arg
);
1132 case USBDEVFS_SETINTERFACE
:
1133 ret
= proc_setintf(ps
, (void *)arg
);
1136 case USBDEVFS_SETCONFIGURATION
:
1137 ret
= proc_setconfig(ps
, (void *)arg
);
1140 case USBDEVFS_SUBMITURB
:
1141 ret
= proc_submiturb(ps
, (void *)arg
);
1143 inode
->i_mtime
= CURRENT_TIME
;
1146 case USBDEVFS_DISCARDURB
:
1147 ret
= proc_unlinkurb(ps
, (void *)arg
);
1150 case USBDEVFS_REAPURB
:
1151 ret
= proc_reapurb(ps
, (void *)arg
);
1154 case USBDEVFS_REAPURBNDELAY
:
1155 ret
= proc_reapurbnonblock(ps
, (void *)arg
);
1158 case USBDEVFS_DISCSIGNAL
:
1159 ret
= proc_disconnectsignal(ps
, (void *)arg
);
1162 case USBDEVFS_CLAIMINTERFACE
:
1163 ret
= proc_claiminterface(ps
, (void *)arg
);
1166 case USBDEVFS_RELEASEINTERFACE
:
1167 ret
= proc_releaseinterface(ps
, (void *)arg
);
1170 case USBDEVFS_IOCTL
:
1171 ret
= proc_ioctl(ps
, (void *) arg
);
1174 up_read(&ps
->devsem
);
1176 inode
->i_atime
= CURRENT_TIME
;
1180 /* No kernel lock - fine */
1181 static unsigned int usbdev_poll(struct file
*file
, struct poll_table_struct
*wait
)
1183 struct dev_state
*ps
= (struct dev_state
*)file
->private_data
;
1184 unsigned int mask
= 0;
1186 poll_wait(file
, &ps
->wait
, wait
);
1187 if (file
->f_mode
& FMODE_WRITE
&& !list_empty(&ps
->async_completed
))
1188 mask
|= POLLOUT
| POLLWRNORM
;
1190 mask
|= POLLERR
| POLLHUP
;
1194 struct file_operations usbdevfs_device_file_operations
= {
1195 llseek
: usbdev_lseek
,
1198 ioctl
: usbdev_ioctl
,
1200 release
: usbdev_release
,