Merge with Linux 2.3.99-pre4.
[linux-2.6/linux-mips.git] / drivers / usb / devio.c
blob933895934ae7e8ac13b8f7e6f0d3d5f85537f3bf
1 /*****************************************************************************/
3 /*
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.
30 * Revision history
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 /*****************************************************************************/
37 #include <linux/fs.h>
38 #include <linux/mm.h>
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>
48 struct async {
49 struct list_head asynclist;
50 struct dev_state *ps;
51 struct task_struct *task;
52 unsigned int signr;
53 void *userbuffer;
54 void *userurb;
55 urb_t urb;
59 * my own sync control and bulk methods. Here to experiment
60 * and because the kernel ones set the process to TASK_UNINTERRUPTIBLE.
63 struct sync {
64 wait_queue_head_t wait;
67 static void sync_completed(purb_t urb)
69 struct sync *s = (struct sync *)urb->context;
71 wake_up(&s->wait);
74 static int do_sync(purb_t urb, int timeout)
76 DECLARE_WAITQUEUE(wait, current);
77 unsigned long tm;
78 signed long tmdiff;
79 struct sync s;
80 int ret;
82 tm = jiffies+timeout;
83 init_waitqueue_head(&s.wait);
84 add_wait_queue(&s.wait, &wait);
85 urb->context = &s;
86 urb->complete = sync_completed;
87 set_current_state(TASK_INTERRUPTIBLE);
88 if ((ret = usb_submit_urb(urb)))
89 goto out;
90 while (urb->status == -EINPROGRESS) {
91 tmdiff = tm - jiffies;
92 if (tmdiff <= 0) {
93 ret = -ETIMEDOUT;
94 goto out;
96 if (signal_pending(current)) {
97 ret = -EINTR;
98 goto out;
100 schedule_timeout(tmdiff);
102 ret = urb->status;
103 out:
104 set_current_state(TASK_RUNNING);
105 usb_unlink_urb(urb);
106 remove_wait_queue(&s.wait, &wait);
107 return ret;
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)
113 urb_t *urb;
114 int ret;
116 if (!(urb = usb_alloc_urb(0)))
117 return -ENOMEM;
118 if (!(urb->setup_packet = kmalloc(8, GFP_KERNEL))) {
119 usb_free_urb(urb);
120 return -ENOMEM;
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;
130 urb->dev = dev;
131 urb->pipe = pipe;
132 urb->transfer_buffer = data;
133 urb->transfer_buffer_length = size;
134 ret = do_sync(urb, timeout);
135 //if (ret >= 0)
136 // ret = urb->status;
137 if (ret >= 0)
138 ret = urb->actual_length;
139 kfree(urb->setup_packet);
140 usb_free_urb(urb);
141 return ret;
144 static int my_usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
145 void *data, int len, int *actual_length, int timeout)
147 urb_t *urb;
148 int ret;
150 if (!(urb = usb_alloc_urb(0)))
151 return -ENOMEM;
152 urb->dev = dev;
153 urb->pipe = pipe;
154 urb->transfer_buffer = data;
155 urb->transfer_buffer_length = len;
156 ret = do_sync(urb, timeout);
157 //if (ret >= 0)
158 // ret = urb->status;
159 if (ret >= 0 && actual_length != NULL)
160 *actual_length = urb->actual_length;
161 usb_free_urb(urb);
162 return ret;
165 static long long usbdev_lseek(struct file *file, long long offset, int orig)
167 switch (orig) {
168 case 0:
169 file->f_pos = offset;
170 return file->f_pos;
172 case 1:
173 file->f_pos += offset;
174 return file->f_pos;
176 case 2:
177 return -EINVAL;
179 default:
180 return -EINVAL;
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;
187 ssize_t ret = 0;
188 unsigned len;
189 loff_t pos;
191 pos = *ppos;
192 down_read(&ps->devsem);
193 if (!ps->dev)
194 ret = -ENODEV;
195 else if (pos < 0)
196 ret = -EINVAL;
197 else if (pos < sizeof(struct usb_device_descriptor)) {
198 len = sizeof(struct usb_device_descriptor) - pos;
199 if (len > nbytes)
200 len = nbytes;
201 if (copy_to_user(buf, ((char *)&ps->dev->descriptor) + pos, len))
202 ret = -EFAULT;
203 else {
204 *ppos += len;
205 buf += len;
206 nbytes -= len;
207 ret += len;
210 up_read(&ps->devsem);
211 return ret;
214 extern inline unsigned int ld2(unsigned int x)
216 unsigned int r = 0;
218 if (x >= 0x10000) {
219 x >>= 16;
220 r += 16;
222 if (x >= 0x100) {
223 x >>= 8;
224 r += 8;
226 if (x >= 0x10) {
227 x >>= 4;
228 r += 4;
230 if (x >= 4) {
231 x >>= 2;
232 r += 2;
234 if (x >= 2)
235 r++;
236 return r;
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);
247 if (!as)
248 return NULL;
249 memset(as, 0, assize);
250 as->urb.number_of_packets = numisoframes;
251 return as;
254 static void free_async(struct async *as)
256 if (as->urb.transfer_buffer)
257 kfree(as->urb.transfer_buffer);
258 kfree(as);
261 extern __inline__ void async_newpending(struct async *as)
263 struct dev_state *ps = as->ps;
264 unsigned long flags;
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;
274 unsigned long flags;
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)
284 unsigned long flags;
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);
294 return as;
297 extern __inline__ struct async *async_getpending(struct dev_state *ps, void *userurb)
299 unsigned long flags;
300 struct async *as;
301 struct list_head *p;
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);
306 p = p->next;
307 if (as->userurb != userurb)
308 continue;
309 list_del(&as->asynclist);
310 INIT_LIST_HEAD(&as->asynclist);
311 spin_unlock_irqrestore(&ps->lock, flags);
312 return as;
314 spin_unlock_irqrestore(&ps->lock, flags);
315 return NULL;
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;
324 #if 1
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);
327 #endif
328 spin_lock(&ps->lock);
329 list_del(&as->asynclist);
330 list_add_tail(&as->asynclist, &ps->async_completed);
331 spin_unlock(&ps->lock);
332 wake_up(&ps->wait);
333 if (as->signr) {
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)
344 struct async *as;
345 unsigned long flags;
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)))
359 free_async(as);
363 * interface claiming
366 static void *driver_probe(struct usb_device *dev, unsigned int intf)
368 return NULL;
371 static void driver_disconnect(struct usb_device *dev, void *context)
373 struct dev_state *ps = (struct dev_state *)context;
375 ps->ifclaimed = 0;
378 struct usb_driver usbdevfs_driver = {
379 "usbdevfs",
380 driver_probe,
381 driver_disconnect,
382 LIST_HEAD_INIT(usbdevfs_driver.driver_list),
383 NULL,
387 static int claimintf(struct dev_state *ps, unsigned int intf)
389 struct usb_device *dev = ps->dev;
390 struct usb_interface *iface;
391 int err;
393 if (intf >= 8*sizeof(ps->ifclaimed) || !dev || intf >= dev->actconfig->bNumInterfaces)
394 return -EINVAL;
395 /* already claimed */
396 if (test_bit(intf, &ps->ifclaimed))
397 return 0;
398 iface = &dev->actconfig->interface[intf];
399 err = -EBUSY;
400 lock_kernel();
401 if (!usb_interface_claimed(iface)) {
402 usb_driver_claim_interface(&usbdevfs_driver, iface, ps);
403 set_bit(intf, &ps->ifclaimed);
404 err = 0;
406 unlock_kernel();
407 return err;
410 static int releaseintf(struct dev_state *ps, unsigned int intf)
412 struct usb_device *dev;
413 struct usb_interface *iface;
414 int err;
416 if (intf >= 8*sizeof(ps->ifclaimed))
417 return -EINVAL;
418 err = -EINVAL;
419 lock_kernel();
420 dev = ps->dev;
421 if (dev && test_and_clear_bit(intf, &ps->ifclaimed)) {
422 iface = &dev->actconfig->interface[intf];
423 usb_driver_release_interface(&usbdevfs_driver, iface);
424 err = 0;
426 unlock_kernel();
427 return err;
430 static int checkintf(struct dev_state *ps, unsigned int intf)
432 if (intf >= 8*sizeof(ps->ifclaimed))
433 return -EINVAL;
434 if (test_bit(intf, &ps->ifclaimed))
435 return 0;
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))
450 return -EINVAL;
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)
458 return i;
462 return -ENOENT;
465 static int findintfif(struct usb_device *dev, unsigned int ifn)
467 unsigned int i, j;
468 struct usb_interface *iface;
469 struct usb_interface_descriptor *alts;
471 if (ifn & ~0xff)
472 return -EINVAL;
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)
478 return i;
481 return -ENOENT;
485 * file operations
487 static int usbdev_open(struct inode *inode, struct file *file)
489 struct usb_device *dev;
490 struct dev_state *ps;
491 int ret;
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)
498 lock_kernel();
499 ret = -ENOENT;
500 if (ITYPE(inode->i_ino) != IDEVICE)
501 goto out;
502 dev = inode->u.usbdev_i.p.dev;
503 if (!dev)
504 goto out;
505 ret = -ENOMEM;
506 if (!(ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL)))
507 goto out;
508 ret = 0;
509 ps->dev = dev;
510 ps->file = file;
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);
516 ps->discsignr = 0;
517 ps->disctask = current;
518 ps->disccontext = NULL;
519 ps->ifclaimed = 0;
520 wmb();
521 list_add_tail(&ps->list, &dev->filelist);
522 file->private_data = ps;
523 out:
524 unlock_kernel();
525 return ret;
528 static int usbdev_release(struct inode *inode, struct file *file)
530 struct dev_state *ps = (struct dev_state *)file->private_data;
531 unsigned int i;
533 lock_kernel();
534 list_del(&ps->list);
535 INIT_LIST_HEAD(&ps->list);
536 if (ps->dev) {
537 for (i = 0; ps->ifclaimed && i < 8*sizeof(ps->ifclaimed); i++)
538 if (test_bit(i, &ps->ifclaimed))
539 releaseintf(ps, i);
541 unlock_kernel();
542 destroy_all_async(ps);
543 kfree(ps);
544 return 0;
547 static int proc_control(struct dev_state *ps, void *arg)
549 struct usb_device *dev = ps->dev;
550 struct usbdevfs_ctrltransfer ctrl;
551 unsigned int tmo;
552 unsigned char *tbuf;
553 int i, ret;
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)
559 return ret;
560 if ((ret = checkintf(ps, ret)))
561 return ret;
562 break;
564 case USB_RECIP_INTERFACE:
565 if ((ret = findintfif(ps->dev, ctrl.index & 0xff)) < 0)
566 return ret;
567 if ((ret = checkintf(ps, ret)))
568 return ret;
569 break;
571 if (ctrl.length > PAGE_SIZE)
572 return -EINVAL;
573 if (!(tbuf = (unsigned char *)__get_free_page(GFP_KERNEL)))
574 return -ENOMEM;
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);
579 return -EINVAL;
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);
586 } else {
587 if (ctrl.length) {
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);
594 if (i<0) {
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);
598 return 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;
606 int len2;
607 unsigned char *tbuf;
608 int i, ret;
610 copy_from_user_ret(&bulk, (void *)arg, sizeof(bulk), -EFAULT);
611 if ((ret = findintfep(ps->dev, bulk.ep)) < 0)
612 return ret;
613 if ((ret = checkintf(ps, ret)))
614 return ret;
615 if (bulk.ep & USB_DIR_IN)
616 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
617 else
618 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
619 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
620 return -EINVAL;
621 len1 = bulk.len;
622 if (len1 > PAGE_SIZE)
623 len1 = PAGE_SIZE;
624 if (!(tbuf = (unsigned char *)__get_free_page(GFP_KERNEL)))
625 return -ENOMEM;
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);
630 return -EINVAL;
632 i = my_usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
633 if (!i && len2) {
634 copy_to_user_ret(bulk.data, tbuf, len2, -EFAULT);
636 } else {
637 if (len1) {
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);
643 if (i < 0) {
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);
646 return i;
648 return len2;
651 static int proc_resetep(struct dev_state *ps, void *arg)
653 unsigned int ep;
654 int ret;
656 get_user_ret(ep, (unsigned int *)arg, -EFAULT);
657 if ((ret = findintfep(ps->dev, ep)) < 0)
658 return ret;
659 if ((ret = checkintf(ps, ret)))
660 return ret;
661 usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0);
662 return 0;
665 static int proc_setintf(struct dev_state *ps, void *arg)
667 struct usbdevfs_setinterface setintf;
668 int ret;
670 copy_from_user_ret(&setintf, arg, sizeof(setintf), -EFAULT);
671 if ((ret = findintfif(ps->dev, setintf.interface)) < 0)
672 return ret;
673 if ((ret = checkintf(ps, ret)))
674 return ret;
675 if (usb_set_interface(ps->dev, setintf.interface, setintf.altsetting))
676 return -EINVAL;
677 return 0;
680 static int proc_setconfig(struct dev_state *ps, void *arg)
682 unsigned int u;
684 get_user_ret(u, (unsigned int *)arg, -EFAULT);
685 if (usb_set_configuration(ps->dev, u) < 0)
686 return -EINVAL;
687 return 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;
694 struct async *as;
695 unsigned int u, totlen, isofrmlen;
696 int ret;
698 copy_from_user_ret(&uurb, arg, sizeof(uurb), -EFAULT);
699 if (uurb.flags & ~(USBDEVFS_URB_ISO_ASAP|USBDEVFS_URB_DISABLE_SPD))
700 return -EINVAL;
701 if (!uurb.buffer)
702 return -EINVAL;
703 if (uurb.signr != 0 && (uurb.signr < SIGRTMIN || uurb.signr > SIGRTMAX))
704 return -EINVAL;
705 if ((ret = findintfep(ps->dev, uurb.endpoint)) < 0)
706 return ret;
707 if ((ret = checkintf(ps, ret)))
708 return ret;
709 switch(uurb.type) {
710 case USBDEVFS_URB_TYPE_BULK:
711 uurb.number_of_packets = 0;
712 if (uurb.buffer_length > 16384)
713 return -EINVAL;
714 if (!access_ok((uurb.endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb.buffer, uurb.buffer_length))
715 return -EFAULT;
716 break;
718 case USBDEVFS_URB_TYPE_ISO:
719 /* arbitrary limit */
720 if (uurb.number_of_packets < 1 || uurb.number_of_packets > 128)
721 return -EINVAL;
722 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) * uurb.number_of_packets;
723 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
724 return -ENOMEM;
725 if (copy_from_user(isopkt, &((struct usbdevfs_urb *)arg)->iso_frame_desc, isofrmlen)) {
726 kfree(isopkt);
727 return -EFAULT;
729 for (totlen = u = 0; u < uurb.number_of_packets; u++) {
730 if (isopkt[u].length > 1023) {
731 kfree(isopkt);
732 return -EINVAL;
734 totlen += isopkt[u].length;
736 if (totlen > 32768) {
737 kfree(isopkt);
738 return -ENOMEM;
740 uurb.buffer_length = totlen;
741 break;
743 default:
744 return -EINVAL;
746 if (!(as = alloc_async(uurb.number_of_packets))) {
747 if (isopkt)
748 kfree(isopkt);
749 return -ENOMEM;
751 if (!(as->urb.transfer_buffer = kmalloc(uurb.buffer_length, GFP_KERNEL))) {
752 if (isopkt)
753 kfree(isopkt);
754 free_async(as);
755 return -ENOMEM;
757 as->urb.next = NULL;
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;
771 if (isopkt)
772 kfree(isopkt);
773 as->ps = ps;
774 as->userurb = arg;
775 if (uurb.endpoint & USB_DIR_IN)
776 as->userbuffer = uurb.buffer;
777 else
778 as->userbuffer = NULL;
779 as->signr = uurb.signr;
780 as->task = current;
781 if (!(uurb.endpoint & USB_DIR_IN)) {
782 if (copy_from_user(as->urb.transfer_buffer, uurb.buffer, as->urb.transfer_buffer_length)) {
783 free_async(as);
784 return -EFAULT;
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);
791 free_async(as);
792 return ret;
794 return 0;
797 static int proc_unlinkurb(struct dev_state *ps, void *arg)
799 struct async *as;
801 as = async_getpending(ps, arg);
802 if (!as)
803 return -EINVAL;
804 usb_unlink_urb(&as->urb);
805 return 0;
808 static int processcompl(struct async *as)
810 unsigned int i;
812 if (as->userbuffer)
813 if (copy_to_user(as->userbuffer, as->urb.transfer_buffer, as->urb.transfer_buffer_length))
814 return -EFAULT;
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)))
819 return 0;
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,
823 -EFAULT);
824 put_user_ret(as->urb.iso_frame_desc[i].status,
825 &((struct usbdevfs_urb *)as->userurb)->iso_frame_desc[i].status,
826 -EFAULT);
828 return 0;
831 static int proc_reapurb(struct dev_state *ps, void *arg)
833 DECLARE_WAITQUEUE(wait, current);
834 struct async *as = NULL;
835 void *addr;
836 int ret;
838 add_wait_queue(&ps->wait, &wait);
839 while (ps->dev) {
840 __set_current_state(TASK_INTERRUPTIBLE);
841 if ((as = async_getcompleted(ps)))
842 break;
843 if (signal_pending(current))
844 break;
845 up_read(&ps->devsem);
846 schedule();
847 down_read(&ps->devsem);
849 remove_wait_queue(&ps->wait, &wait);
850 set_current_state(TASK_RUNNING);
851 if (as) {
852 ret = processcompl(as);
853 addr = as->userurb;
854 free_async(as);
855 if (ret)
856 return ret;
857 put_user_ret(addr, (void **)arg, -EFAULT);
858 return 0;
860 if (signal_pending(current))
861 return -EINTR;
862 return -EIO;
865 static int proc_reapurbnonblock(struct dev_state *ps, void *arg)
867 struct async *as;
868 void *addr;
869 int ret;
871 if (!(as = async_getcompleted(ps)))
872 return -EAGAIN;
873 ret = processcompl(as);
874 addr = as->userurb;
875 free_async(as);
876 if (ret)
877 return ret;
878 put_user_ret(addr, (void **)arg, -EFAULT);
879 return 0;
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))
888 return -EINVAL;
889 ps->discsignr = ds.signr;
890 ps->disccontext = ds.context;
891 return 0;
894 static int proc_claiminterface(struct dev_state *ps, void *arg)
896 unsigned int intf;
897 int ret;
899 get_user_ret(intf, (unsigned int *)arg, -EFAULT);
900 if ((ret = findintfif(ps->dev, intf)) < 0)
901 return ret;
902 return claimintf(ps, ret);
905 static int proc_releaseinterface(struct dev_state *ps, void *arg)
907 unsigned int intf;
908 int ret;
910 get_user_ret(intf, (unsigned int *)arg, -EFAULT);
911 if ((ret = findintfif(ps->dev, intf)) < 0)
912 return ret;
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))
922 return -EPERM;
923 down_read(&ps->devsem);
924 if (!ps->dev) {
925 up_read(&ps->devsem);
926 return -ENODEV;
928 switch (cmd) {
929 case USBDEVFS_CONTROL:
930 ret = proc_control(ps, (void *)arg);
931 if (ret >= 0)
932 inode->i_mtime = CURRENT_TIME;
933 break;
935 case USBDEVFS_BULK:
936 ret = proc_bulk(ps, (void *)arg);
937 if (ret >= 0)
938 inode->i_mtime = CURRENT_TIME;
939 break;
941 case USBDEVFS_RESETEP:
942 ret = proc_resetep(ps, (void *)arg);
943 if (ret >= 0)
944 inode->i_mtime = CURRENT_TIME;
945 break;
947 case USBDEVFS_SETINTERFACE:
948 ret = proc_setintf(ps, (void *)arg);
949 break;
951 case USBDEVFS_SETCONFIGURATION:
952 ret = proc_setconfig(ps, (void *)arg);
953 break;
955 case USBDEVFS_SUBMITURB:
956 ret = proc_submiturb(ps, (void *)arg);
957 if (ret >= 0)
958 inode->i_mtime = CURRENT_TIME;
959 break;
961 case USBDEVFS_DISCARDURB:
962 ret = proc_unlinkurb(ps, (void *)arg);
963 break;
965 case USBDEVFS_REAPURB:
966 ret = proc_reapurb(ps, (void *)arg);
967 break;
969 case USBDEVFS_REAPURBNDELAY:
970 ret = proc_reapurbnonblock(ps, (void *)arg);
971 break;
973 case USBDEVFS_DISCSIGNAL:
974 ret = proc_disconnectsignal(ps, (void *)arg);
975 break;
977 case USBDEVFS_CLAIMINTERFACE:
978 ret = proc_claiminterface(ps, (void *)arg);
979 break;
981 case USBDEVFS_RELEASEINTERFACE:
982 ret = proc_releaseinterface(ps, (void *)arg);
983 break;
986 up_read(&ps->devsem);
987 if (ret >= 0)
988 inode->i_atime = CURRENT_TIME;
989 return ret;
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;
1000 if (!ps->dev)
1001 mask |= POLLERR | POLLHUP;
1002 return mask;
1005 struct file_operations usbdevfs_device_file_operations = {
1006 llseek: usbdev_lseek,
1007 read: usbdev_read,
1008 poll: usbdev_poll,
1009 ioctl: usbdev_ioctl,
1010 open: usbdev_open,
1011 release: usbdev_release,