/home/lenb/src/to-linus branch 'acpi-2.6.12'
[linux-2.6] / drivers / usb / core / devio.c
1 /*****************************************************************************/
2
3 /*
4  *      devio.c  --  User space communication with USB devices.
5  *
6  *      Copyright (C) 1999-2000  Thomas Sailer (sailer@ife.ee.ethz.ch)
7  *
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.
12  *
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.
17  *
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.
21  *
22  *  $Id: devio.c,v 1.7 2000/02/01 17:28:48 fliegl Exp $
23  *
24  *  This file implements the usbfs/x/y files, where
25  *  x is the bus number and y the device number.
26  *
27  *  It allows user space programs/"drivers" to communicate directly
28  *  with USB devices without intervening kernel driver.
29  *
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
33  */
34
35 /*****************************************************************************/
36
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/module.h>
44 #include <linux/usb.h>
45 #include <linux/usbdevice_fs.h>
46 #include <asm/uaccess.h>
47 #include <asm/byteorder.h>
48 #include <linux/moduleparam.h>
49
50 #include "hcd.h"        /* for usbcore internals */
51 #include "usb.h"
52
53 struct async {
54         struct list_head asynclist;
55         struct dev_state *ps;
56         struct task_struct *task;
57         unsigned int signr;
58         unsigned int ifnum;
59         void __user *userbuffer;
60         void __user *userurb;
61         struct urb *urb;
62 };
63
64 static int usbfs_snoop = 0;
65 module_param (usbfs_snoop, bool, S_IRUGO | S_IWUSR);
66 MODULE_PARM_DESC (usbfs_snoop, "true to log all usbfs traffic");
67
68 #define snoop(dev, format, arg...)                              \
69         do {                                                    \
70                 if (usbfs_snoop)                                \
71                         dev_info( dev , format , ## arg);       \
72         } while (0)
73
74
75 #define MAX_USBFS_BUFFER_SIZE   16384
76
77 static inline int connected (struct usb_device *dev)
78 {
79         return dev->state != USB_STATE_NOTATTACHED;
80 }
81
82 static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
83 {
84         loff_t ret;
85
86         lock_kernel();
87
88         switch (orig) {
89         case 0:
90                 file->f_pos = offset;
91                 ret = file->f_pos;
92                 break;
93         case 1:
94                 file->f_pos += offset;
95                 ret = file->f_pos;
96                 break;
97         case 2:
98         default:
99                 ret = -EINVAL;
100         }
101
102         unlock_kernel();
103         return ret;
104 }
105
106 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
107 {
108         struct dev_state *ps = (struct dev_state *)file->private_data;
109         struct usb_device *dev = ps->dev;
110         ssize_t ret = 0;
111         unsigned len;
112         loff_t pos;
113         int i;
114
115         pos = *ppos;
116         usb_lock_device(dev);
117         if (!connected(dev)) {
118                 ret = -ENODEV;
119                 goto err;
120         } else if (pos < 0) {
121                 ret = -EINVAL;
122                 goto err;
123         }
124
125         if (pos < sizeof(struct usb_device_descriptor)) {
126                 struct usb_device_descriptor *desc = kmalloc(sizeof(*desc), GFP_KERNEL);
127                 if (!desc) {
128                         ret = -ENOMEM;
129                         goto err;
130                 }
131                 memcpy(desc, &dev->descriptor, sizeof(dev->descriptor));
132                 le16_to_cpus(&desc->bcdUSB);
133                 le16_to_cpus(&desc->idVendor);
134                 le16_to_cpus(&desc->idProduct);
135                 le16_to_cpus(&desc->bcdDevice);
136
137                 len = sizeof(struct usb_device_descriptor) - pos;
138                 if (len > nbytes)
139                         len = nbytes;
140                 if (copy_to_user(buf, ((char *)desc) + pos, len)) {
141                         kfree(desc);
142                         ret = -EFAULT;
143                         goto err;
144                 }
145                 kfree(desc);
146
147                 *ppos += len;
148                 buf += len;
149                 nbytes -= len;
150                 ret += len;
151         }
152
153         pos = sizeof(struct usb_device_descriptor);
154         for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
155                 struct usb_config_descriptor *config =
156                         (struct usb_config_descriptor *)dev->rawdescriptors[i];
157                 unsigned int length = le16_to_cpu(config->wTotalLength);
158
159                 if (*ppos < pos + length) {
160
161                         /* The descriptor may claim to be longer than it
162                          * really is.  Here is the actual allocated length. */
163                         unsigned alloclen =
164                                 le16_to_cpu(dev->config[i].desc.wTotalLength);
165
166                         len = length - (*ppos - pos);
167                         if (len > nbytes)
168                                 len = nbytes;
169
170                         /* Simply don't write (skip over) unallocated parts */
171                         if (alloclen > (*ppos - pos)) {
172                                 alloclen -= (*ppos - pos);
173                                 if (copy_to_user(buf,
174                                     dev->rawdescriptors[i] + (*ppos - pos),
175                                     min(len, alloclen))) {
176                                         ret = -EFAULT;
177                                         goto err;
178                                 }
179                         }
180
181                         *ppos += len;
182                         buf += len;
183                         nbytes -= len;
184                         ret += len;
185                 }
186
187                 pos += length;
188         }
189
190 err:
191         usb_unlock_device(dev);
192         return ret;
193 }
194
195 /*
196  * async list handling
197  */
198
199 static struct async *alloc_async(unsigned int numisoframes)
200 {
201         unsigned int assize = sizeof(struct async) + numisoframes * sizeof(struct usb_iso_packet_descriptor);
202         struct async *as = kmalloc(assize, GFP_KERNEL);
203         if (!as)
204                 return NULL;
205         memset(as, 0, assize);
206         as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
207         if (!as->urb) {
208                 kfree(as);
209                 return NULL;
210         }
211         return as;
212 }
213
214 static void free_async(struct async *as)
215 {
216         kfree(as->urb->transfer_buffer);
217         kfree(as->urb->setup_packet);
218         usb_free_urb(as->urb);
219         kfree(as);
220 }
221
222 static inline void async_newpending(struct async *as)
223 {
224         struct dev_state *ps = as->ps;
225         unsigned long flags;
226         
227         spin_lock_irqsave(&ps->lock, flags);
228         list_add_tail(&as->asynclist, &ps->async_pending);
229         spin_unlock_irqrestore(&ps->lock, flags);
230 }
231
232 static inline void async_removepending(struct async *as)
233 {
234         struct dev_state *ps = as->ps;
235         unsigned long flags;
236         
237         spin_lock_irqsave(&ps->lock, flags);
238         list_del_init(&as->asynclist);
239         spin_unlock_irqrestore(&ps->lock, flags);
240 }
241
242 static inline struct async *async_getcompleted(struct dev_state *ps)
243 {
244         unsigned long flags;
245         struct async *as = NULL;
246
247         spin_lock_irqsave(&ps->lock, flags);
248         if (!list_empty(&ps->async_completed)) {
249                 as = list_entry(ps->async_completed.next, struct async, asynclist);
250                 list_del_init(&as->asynclist);
251         }
252         spin_unlock_irqrestore(&ps->lock, flags);
253         return as;
254 }
255
256 static inline struct async *async_getpending(struct dev_state *ps, void __user *userurb)
257 {
258         unsigned long flags;
259         struct async *as;
260
261         spin_lock_irqsave(&ps->lock, flags);
262         list_for_each_entry(as, &ps->async_pending, asynclist)
263                 if (as->userurb == userurb) {
264                         list_del_init(&as->asynclist);
265                         spin_unlock_irqrestore(&ps->lock, flags);
266                         return as;
267                 }
268         spin_unlock_irqrestore(&ps->lock, flags);
269         return NULL;
270 }
271
272 static void async_completed(struct urb *urb, struct pt_regs *regs)
273 {
274         struct async *as = (struct async *)urb->context;
275         struct dev_state *ps = as->ps;
276         struct siginfo sinfo;
277
278         spin_lock(&ps->lock);
279         list_move_tail(&as->asynclist, &ps->async_completed);
280         spin_unlock(&ps->lock);
281         if (as->signr) {
282                 sinfo.si_signo = as->signr;
283                 sinfo.si_errno = as->urb->status;
284                 sinfo.si_code = SI_ASYNCIO;
285                 sinfo.si_addr = as->userurb;
286                 send_sig_info(as->signr, &sinfo, as->task);
287         }
288         wake_up(&ps->wait);
289 }
290
291 static void destroy_async (struct dev_state *ps, struct list_head *list)
292 {
293         struct async *as;
294         unsigned long flags;
295
296         spin_lock_irqsave(&ps->lock, flags);
297         while (!list_empty(list)) {
298                 as = list_entry(list->next, struct async, asynclist);
299                 list_del_init(&as->asynclist);
300
301                 /* drop the spinlock so the completion handler can run */
302                 spin_unlock_irqrestore(&ps->lock, flags);
303                 usb_kill_urb(as->urb);
304                 spin_lock_irqsave(&ps->lock, flags);
305         }
306         spin_unlock_irqrestore(&ps->lock, flags);
307         as = async_getcompleted(ps);
308         while (as) {
309                 free_async(as);
310                 as = async_getcompleted(ps);
311         }
312 }
313
314 static void destroy_async_on_interface (struct dev_state *ps, unsigned int ifnum)
315 {
316         struct list_head *p, *q, hitlist;
317         unsigned long flags;
318
319         INIT_LIST_HEAD(&hitlist);
320         spin_lock_irqsave(&ps->lock, flags);
321         list_for_each_safe(p, q, &ps->async_pending)
322                 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
323                         list_move_tail(p, &hitlist);
324         spin_unlock_irqrestore(&ps->lock, flags);
325         destroy_async(ps, &hitlist);
326 }
327
328 static inline void destroy_all_async(struct dev_state *ps)
329 {
330                 destroy_async(ps, &ps->async_pending);
331 }
332
333 /*
334  * interface claims are made only at the request of user level code,
335  * which can also release them (explicitly or by closing files).
336  * they're also undone when devices disconnect.
337  */
338
339 static int driver_probe (struct usb_interface *intf,
340                          const struct usb_device_id *id)
341 {
342         return -ENODEV;
343 }
344
345 static void driver_disconnect(struct usb_interface *intf)
346 {
347         struct dev_state *ps = usb_get_intfdata (intf);
348         unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
349
350         if (!ps)
351                 return;
352
353         /* NOTE:  this relies on usbcore having canceled and completed
354          * all pending I/O requests; 2.6 does that.
355          */
356
357         if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
358                 clear_bit(ifnum, &ps->ifclaimed);
359         else
360                 warn("interface number %u out of range", ifnum);
361
362         usb_set_intfdata (intf, NULL);
363
364         /* force async requests to complete */
365         destroy_async_on_interface(ps, ifnum);
366 }
367
368 struct usb_driver usbfs_driver = {
369         .owner =        THIS_MODULE,
370         .name =         "usbfs",
371         .probe =        driver_probe,
372         .disconnect =   driver_disconnect,
373 };
374
375 static int claimintf(struct dev_state *ps, unsigned int ifnum)
376 {
377         struct usb_device *dev = ps->dev;
378         struct usb_interface *intf;
379         int err;
380
381         if (ifnum >= 8*sizeof(ps->ifclaimed))
382                 return -EINVAL;
383         /* already claimed */
384         if (test_bit(ifnum, &ps->ifclaimed))
385                 return 0;
386
387         /* lock against other changes to driver bindings */
388         down_write(&usb_bus_type.subsys.rwsem);
389         intf = usb_ifnum_to_if(dev, ifnum);
390         if (!intf)
391                 err = -ENOENT;
392         else
393                 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
394         up_write(&usb_bus_type.subsys.rwsem);
395         if (err == 0)
396                 set_bit(ifnum, &ps->ifclaimed);
397         return err;
398 }
399
400 static int releaseintf(struct dev_state *ps, unsigned int ifnum)
401 {
402         struct usb_device *dev;
403         struct usb_interface *intf;
404         int err;
405
406         err = -EINVAL;
407         if (ifnum >= 8*sizeof(ps->ifclaimed))
408                 return err;
409         dev = ps->dev;
410         /* lock against other changes to driver bindings */
411         down_write(&usb_bus_type.subsys.rwsem);
412         intf = usb_ifnum_to_if(dev, ifnum);
413         if (!intf)
414                 err = -ENOENT;
415         else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
416                 usb_driver_release_interface(&usbfs_driver, intf);
417                 err = 0;
418         }
419         up_write(&usb_bus_type.subsys.rwsem);
420         return err;
421 }
422
423 static int checkintf(struct dev_state *ps, unsigned int ifnum)
424 {
425         if (ps->dev->state != USB_STATE_CONFIGURED)
426                 return -EHOSTUNREACH;
427         if (ifnum >= 8*sizeof(ps->ifclaimed))
428                 return -EINVAL;
429         if (test_bit(ifnum, &ps->ifclaimed))
430                 return 0;
431         /* if not yet claimed, claim it for the driver */
432         dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim interface %u before use\n",
433                current->pid, current->comm, ifnum);
434         return claimintf(ps, ifnum);
435 }
436
437 static int findintfep(struct usb_device *dev, unsigned int ep)
438 {
439         unsigned int i, j, e;
440         struct usb_interface *intf;
441         struct usb_host_interface *alts;
442         struct usb_endpoint_descriptor *endpt;
443
444         if (ep & ~(USB_DIR_IN|0xf))
445                 return -EINVAL;
446         if (!dev->actconfig)
447                 return -ESRCH;
448         for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
449                 intf = dev->actconfig->interface[i];
450                 for (j = 0; j < intf->num_altsetting; j++) {
451                         alts = &intf->altsetting[j];
452                         for (e = 0; e < alts->desc.bNumEndpoints; e++) {
453                                 endpt = &alts->endpoint[e].desc;
454                                 if (endpt->bEndpointAddress == ep)
455                                         return alts->desc.bInterfaceNumber;
456                         }
457                 }
458         }
459         return -ENOENT; 
460 }
461
462 static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype, unsigned int index)
463 {
464         int ret = 0;
465
466         if (ps->dev->state != USB_STATE_CONFIGURED)
467                 return -EHOSTUNREACH;
468         if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
469                 return 0;
470
471         index &= 0xff;
472         switch (requesttype & USB_RECIP_MASK) {
473         case USB_RECIP_ENDPOINT:
474                 if ((ret = findintfep(ps->dev, index)) >= 0)
475                         ret = checkintf(ps, ret);
476                 break;
477
478         case USB_RECIP_INTERFACE:
479                 ret = checkintf(ps, index);
480                 break;
481         }
482         return ret;
483 }
484
485 /*
486  * file operations
487  */
488 static int usbdev_open(struct inode *inode, struct file *file)
489 {
490         struct usb_device *dev;
491         struct dev_state *ps;
492         int ret;
493
494         /* 
495          * no locking necessary here, as chrdev_open has the kernel lock
496          * (still acquire the kernel lock for safety)
497          */
498         ret = -ENOMEM;
499         if (!(ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL)))
500                 goto out_nolock;
501
502         lock_kernel();
503         ret = -ENOENT;
504         dev = usb_get_dev(inode->u.generic_ip);
505         if (!dev) {
506                 kfree(ps);
507                 goto out;
508         }
509         ret = 0;
510         ps->dev = dev;
511         ps->file = file;
512         spin_lock_init(&ps->lock);
513         INIT_LIST_HEAD(&ps->async_pending);
514         INIT_LIST_HEAD(&ps->async_completed);
515         init_waitqueue_head(&ps->wait);
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  out_nolock:
526         return ret;
527 }
528
529 static int usbdev_release(struct inode *inode, struct file *file)
530 {
531         struct dev_state *ps = (struct dev_state *)file->private_data;
532         struct usb_device *dev = ps->dev;
533         unsigned int ifnum;
534
535         usb_lock_device(dev);
536         list_del_init(&ps->list);
537         for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
538                         ifnum++) {
539                 if (test_bit(ifnum, &ps->ifclaimed))
540                         releaseintf(ps, ifnum);
541         }
542         destroy_all_async(ps);
543         usb_unlock_device(dev);
544         usb_put_dev(dev);
545         ps->dev = NULL;
546         kfree(ps);
547         return 0;
548 }
549
550 static int proc_control(struct dev_state *ps, void __user *arg)
551 {
552         struct usb_device *dev = ps->dev;
553         struct usbdevfs_ctrltransfer ctrl;
554         unsigned int tmo;
555         unsigned char *tbuf;
556         int i, j, ret;
557
558         if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
559                 return -EFAULT;
560         if ((ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex)))
561                 return ret;
562         if (ctrl.wLength > PAGE_SIZE)
563                 return -EINVAL;
564         if (!(tbuf = (unsigned char *)__get_free_page(GFP_KERNEL)))
565                 return -ENOMEM;
566         tmo = ctrl.timeout;
567         if (ctrl.bRequestType & 0x80) {
568                 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data, ctrl.wLength)) {
569                         free_page((unsigned long)tbuf);
570                         return -EINVAL;
571                 }
572                 snoop(&dev->dev, "control read: bRequest=%02x "
573                                 "bRrequestType=%02x wValue=%04x "
574                                 "wIndex=%04x wLength=%04x\n",
575                         ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
576                                 ctrl.wIndex, ctrl.wLength);
577
578                 usb_unlock_device(dev);
579                 i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest, ctrl.bRequestType,
580                                        ctrl.wValue, ctrl.wIndex, tbuf, ctrl.wLength, tmo);
581                 usb_lock_device(dev);
582                 if ((i > 0) && ctrl.wLength) {
583                         if (usbfs_snoop) {
584                                 dev_info(&dev->dev, "control read: data ");
585                                 for (j = 0; j < i; ++j)
586                                         printk ("%02x ", (unsigned char)(tbuf)[j]);
587                                 printk("\n");
588                         }
589                         if (copy_to_user(ctrl.data, tbuf, i)) {
590                                 free_page((unsigned long)tbuf);
591                                 return -EFAULT;
592                         }
593                 }
594         } else {
595                 if (ctrl.wLength) {
596                         if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
597                                 free_page((unsigned long)tbuf);
598                                 return -EFAULT;
599                         }
600                 }
601                 snoop(&dev->dev, "control write: bRequest=%02x "
602                                 "bRrequestType=%02x wValue=%04x "
603                                 "wIndex=%04x wLength=%04x\n",
604                         ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
605                                 ctrl.wIndex, ctrl.wLength);
606                 if (usbfs_snoop) {
607                         dev_info(&dev->dev, "control write: data: ");
608                         for (j = 0; j < ctrl.wLength; ++j)
609                                 printk ("%02x ", (unsigned char)(tbuf)[j]);
610                         printk("\n");
611                 }
612                 usb_unlock_device(dev);
613                 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest, ctrl.bRequestType,
614                                        ctrl.wValue, ctrl.wIndex, tbuf, ctrl.wLength, tmo);
615                 usb_lock_device(dev);
616         }
617         free_page((unsigned long)tbuf);
618         if (i<0 && i != -EPIPE) {
619                 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
620                            "failed cmd %s rqt %u rq %u len %u ret %d\n",
621                            current->comm, ctrl.bRequestType, ctrl.bRequest,
622                            ctrl.wLength, i);
623         }
624         return i;
625 }
626
627 static int proc_bulk(struct dev_state *ps, void __user *arg)
628 {
629         struct usb_device *dev = ps->dev;
630         struct usbdevfs_bulktransfer bulk;
631         unsigned int tmo, len1, pipe;
632         int len2;
633         unsigned char *tbuf;
634         int i, ret;
635
636         if (copy_from_user(&bulk, arg, sizeof(bulk)))
637                 return -EFAULT;
638         if ((ret = findintfep(ps->dev, bulk.ep)) < 0)
639                 return ret;
640         if ((ret = checkintf(ps, ret)))
641                 return ret;
642         if (bulk.ep & USB_DIR_IN)
643                 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
644         else
645                 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
646         if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
647                 return -EINVAL;
648         len1 = bulk.len;
649         if (len1 > MAX_USBFS_BUFFER_SIZE)
650                 return -EINVAL;
651         if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
652                 return -ENOMEM;
653         tmo = bulk.timeout;
654         if (bulk.ep & 0x80) {
655                 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
656                         kfree(tbuf);
657                         return -EINVAL;
658                 }
659                 usb_unlock_device(dev);
660                 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
661                 usb_lock_device(dev);
662                 if (!i && len2) {
663                         if (copy_to_user(bulk.data, tbuf, len2)) {
664                                 kfree(tbuf);
665                                 return -EFAULT;
666                         }
667                 }
668         } else {
669                 if (len1) {
670                         if (copy_from_user(tbuf, bulk.data, len1)) {
671                                 kfree(tbuf);
672                                 return -EFAULT;
673                         }
674                 }
675                 usb_unlock_device(dev);
676                 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
677                 usb_lock_device(dev);
678         }
679         kfree(tbuf);
680         if (i < 0)
681                 return i;
682         return len2;
683 }
684
685 static int proc_resetep(struct dev_state *ps, void __user *arg)
686 {
687         unsigned int ep;
688         int ret;
689
690         if (get_user(ep, (unsigned int __user *)arg))
691                 return -EFAULT;
692         if ((ret = findintfep(ps->dev, ep)) < 0)
693                 return ret;
694         if ((ret = checkintf(ps, ret)))
695                 return ret;
696         usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0);
697         return 0;
698 }
699
700 static int proc_clearhalt(struct dev_state *ps, void __user *arg)
701 {
702         unsigned int ep;
703         int pipe;
704         int ret;
705
706         if (get_user(ep, (unsigned int __user *)arg))
707                 return -EFAULT;
708         if ((ret = findintfep(ps->dev, ep)) < 0)
709                 return ret;
710         if ((ret = checkintf(ps, ret)))
711                 return ret;
712         if (ep & USB_DIR_IN)
713                 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
714         else
715                 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
716
717         return usb_clear_halt(ps->dev, pipe);
718 }
719                 
720
721 static int proc_getdriver(struct dev_state *ps, void __user *arg)
722 {
723         struct usbdevfs_getdriver gd;
724         struct usb_interface *intf;
725         int ret;
726
727         if (copy_from_user(&gd, arg, sizeof(gd)))
728                 return -EFAULT;
729         down_read(&usb_bus_type.subsys.rwsem);
730         intf = usb_ifnum_to_if(ps->dev, gd.interface);
731         if (!intf || !intf->dev.driver)
732                 ret = -ENODATA;
733         else {
734                 strncpy(gd.driver, intf->dev.driver->name,
735                                 sizeof(gd.driver));
736                 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
737         }
738         up_read(&usb_bus_type.subsys.rwsem);
739         return ret;
740 }
741
742 static int proc_connectinfo(struct dev_state *ps, void __user *arg)
743 {
744         struct usbdevfs_connectinfo ci;
745
746         ci.devnum = ps->dev->devnum;
747         ci.slow = ps->dev->speed == USB_SPEED_LOW;
748         if (copy_to_user(arg, &ci, sizeof(ci)))
749                 return -EFAULT;
750         return 0;
751 }
752
753 static int proc_resetdevice(struct dev_state *ps)
754 {
755         return usb_reset_device(ps->dev);
756
757 }
758
759 static int proc_setintf(struct dev_state *ps, void __user *arg)
760 {
761         struct usbdevfs_setinterface setintf;
762         int ret;
763
764         if (copy_from_user(&setintf, arg, sizeof(setintf)))
765                 return -EFAULT;
766         if ((ret = checkintf(ps, setintf.interface)))
767                 return ret;
768         return usb_set_interface(ps->dev, setintf.interface,
769                         setintf.altsetting);
770 }
771
772 static int proc_setconfig(struct dev_state *ps, void __user *arg)
773 {
774         unsigned int u;
775         int status = 0;
776         struct usb_host_config *actconfig;
777
778         if (get_user(u, (unsigned int __user *)arg))
779                 return -EFAULT;
780
781         actconfig = ps->dev->actconfig;
782  
783         /* Don't touch the device if any interfaces are claimed.
784          * It could interfere with other drivers' operations, and if
785          * an interface is claimed by usbfs it could easily deadlock.
786          */
787         if (actconfig) {
788                 int i;
789  
790                 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
791                         if (usb_interface_claimed(actconfig->interface[i])) {
792                                 dev_warn (&ps->dev->dev,
793                                         "usbfs: interface %d claimed by %s "
794                                         "while '%s' sets config #%d\n",
795                                         actconfig->interface[i]
796                                                 ->cur_altsetting
797                                                 ->desc.bInterfaceNumber,
798                                         actconfig->interface[i]
799                                                 ->dev.driver->name,
800                                         current->comm, u);
801                                 status = -EBUSY;
802                                 break;
803                         }
804                 }
805         }
806
807         /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
808          * so avoid usb_set_configuration()'s kick to sysfs
809          */
810         if (status == 0) {
811                 if (actconfig && actconfig->desc.bConfigurationValue == u)
812                         status = usb_reset_configuration(ps->dev);
813                 else
814                         status = usb_set_configuration(ps->dev, u);
815         }
816
817         return status;
818 }
819
820
821 static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
822                              struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
823                              void __user *arg)
824 {
825         struct usbdevfs_iso_packet_desc *isopkt = NULL;
826         struct usb_host_endpoint *ep;
827         struct async *as;
828         struct usb_ctrlrequest *dr = NULL;
829         unsigned int u, totlen, isofrmlen;
830         int ret, interval = 0, ifnum = -1;
831
832         if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP|USBDEVFS_URB_SHORT_NOT_OK|
833                            URB_NO_FSBR|URB_ZERO_PACKET))
834                 return -EINVAL;
835         if (!uurb->buffer)
836                 return -EINVAL;
837         if (uurb->signr != 0 && (uurb->signr < SIGRTMIN || uurb->signr > SIGRTMAX))
838                 return -EINVAL;
839         if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL && (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
840                 if ((ifnum = findintfep(ps->dev, uurb->endpoint)) < 0)
841                         return ifnum;
842                 if ((ret = checkintf(ps, ifnum)))
843                         return ret;
844         }
845         if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0)
846                 ep = ps->dev->ep_in [uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
847         else
848                 ep = ps->dev->ep_out [uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
849         if (!ep)
850                 return -ENOENT;
851         switch(uurb->type) {
852         case USBDEVFS_URB_TYPE_CONTROL:
853                 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
854                                 != USB_ENDPOINT_XFER_CONTROL)
855                         return -EINVAL;
856                 /* min 8 byte setup packet, max arbitrary */
857                 if (uurb->buffer_length < 8 || uurb->buffer_length > PAGE_SIZE)
858                         return -EINVAL;
859                 if (!(dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL)))
860                         return -ENOMEM;
861                 if (copy_from_user(dr, uurb->buffer, 8)) {
862                         kfree(dr);
863                         return -EFAULT;
864                 }
865                 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
866                         kfree(dr);
867                         return -EINVAL;
868                 }
869                 if ((ret = check_ctrlrecip(ps, dr->bRequestType, le16_to_cpup(&dr->wIndex)))) {
870                         kfree(dr);
871                         return ret;
872                 }
873                 uurb->endpoint = (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) | (dr->bRequestType & USB_ENDPOINT_DIR_MASK);
874                 uurb->number_of_packets = 0;
875                 uurb->buffer_length = le16_to_cpup(&dr->wLength);
876                 uurb->buffer += 8;
877                 if (!access_ok((uurb->endpoint & USB_DIR_IN) ?  VERIFY_WRITE : VERIFY_READ, uurb->buffer, uurb->buffer_length)) {
878                         kfree(dr);
879                         return -EFAULT;
880                 }
881                 break;
882
883         case USBDEVFS_URB_TYPE_BULK:
884                 switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
885                 case USB_ENDPOINT_XFER_CONTROL:
886                 case USB_ENDPOINT_XFER_ISOC:
887                         return -EINVAL;
888                 /* allow single-shot interrupt transfers, at bogus rates */
889                 }
890                 uurb->number_of_packets = 0;
891                 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
892                         return -EINVAL;
893                 if (!access_ok((uurb->endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb->buffer, uurb->buffer_length))
894                         return -EFAULT;
895                 break;
896
897         case USBDEVFS_URB_TYPE_ISO:
898                 /* arbitrary limit */
899                 if (uurb->number_of_packets < 1 || uurb->number_of_packets > 128)
900                         return -EINVAL;
901                 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
902                                 != USB_ENDPOINT_XFER_ISOC)
903                         return -EINVAL;
904                 interval = 1 << min (15, ep->desc.bInterval - 1);
905                 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) * uurb->number_of_packets;
906                 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
907                         return -ENOMEM;
908                 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
909                         kfree(isopkt);
910                         return -EFAULT;
911                 }
912                 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
913                         if (isopkt[u].length > 1023) {
914                                 kfree(isopkt);
915                                 return -EINVAL;
916                         }
917                         totlen += isopkt[u].length;
918                 }
919                 if (totlen > 32768) {
920                         kfree(isopkt);
921                         return -EINVAL;
922                 }
923                 uurb->buffer_length = totlen;
924                 break;
925
926         case USBDEVFS_URB_TYPE_INTERRUPT:
927                 uurb->number_of_packets = 0;
928                 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
929                                 != USB_ENDPOINT_XFER_INT)
930                         return -EINVAL;
931                 if (ps->dev->speed == USB_SPEED_HIGH)
932                         interval = 1 << min (15, ep->desc.bInterval - 1);
933                 else
934                         interval = ep->desc.bInterval;
935                 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
936                         return -EINVAL;
937                 if (!access_ok((uurb->endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb->buffer, uurb->buffer_length))
938                         return -EFAULT;
939                 break;
940
941         default:
942                 return -EINVAL;
943         }
944         if (!(as = alloc_async(uurb->number_of_packets))) {
945                 kfree(isopkt);
946                 kfree(dr);
947                 return -ENOMEM;
948         }
949         if (!(as->urb->transfer_buffer = kmalloc(uurb->buffer_length, GFP_KERNEL))) {
950                 kfree(isopkt);
951                 kfree(dr);
952                 free_async(as);
953                 return -ENOMEM;
954         }
955         as->urb->dev = ps->dev;
956         as->urb->pipe = (uurb->type << 30) | __create_pipe(ps->dev, uurb->endpoint & 0xf) | (uurb->endpoint & USB_DIR_IN);
957         as->urb->transfer_flags = uurb->flags;
958         as->urb->transfer_buffer_length = uurb->buffer_length;
959         as->urb->setup_packet = (unsigned char*)dr;
960         as->urb->start_frame = uurb->start_frame;
961         as->urb->number_of_packets = uurb->number_of_packets;
962         as->urb->interval = interval;
963         as->urb->context = as;
964         as->urb->complete = async_completed;
965         for (totlen = u = 0; u < uurb->number_of_packets; u++) {
966                 as->urb->iso_frame_desc[u].offset = totlen;
967                 as->urb->iso_frame_desc[u].length = isopkt[u].length;
968                 totlen += isopkt[u].length;
969         }
970         kfree(isopkt);
971         as->ps = ps;
972         as->userurb = arg;
973         if (uurb->endpoint & USB_DIR_IN)
974                 as->userbuffer = uurb->buffer;
975         else
976                 as->userbuffer = NULL;
977         as->signr = uurb->signr;
978         as->ifnum = ifnum;
979         as->task = current;
980         if (!(uurb->endpoint & USB_DIR_IN)) {
981                 if (copy_from_user(as->urb->transfer_buffer, uurb->buffer, as->urb->transfer_buffer_length)) {
982                         free_async(as);
983                         return -EFAULT;
984                 }
985         }
986         async_newpending(as);
987         if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
988                 dev_printk(KERN_DEBUG, &ps->dev->dev, "usbfs: usb_submit_urb returned %d\n", ret);
989                 async_removepending(as);
990                 free_async(as);
991                 return ret;
992         }
993         return 0;
994 }
995
996 static int proc_submiturb(struct dev_state *ps, void __user *arg)
997 {
998         struct usbdevfs_urb uurb;
999
1000         if (copy_from_user(&uurb, arg, sizeof(uurb)))
1001                 return -EFAULT;
1002
1003         return proc_do_submiturb(ps, &uurb, (((struct usbdevfs_urb __user *)arg)->iso_frame_desc), arg);
1004 }
1005
1006 static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
1007 {
1008         struct async *as;
1009
1010         as = async_getpending(ps, arg);
1011         if (!as)
1012                 return -EINVAL;
1013         usb_kill_urb(as->urb);
1014         return 0;
1015 }
1016
1017 static int processcompl(struct async *as, void __user * __user *arg)
1018 {
1019         struct urb *urb = as->urb;
1020         struct usbdevfs_urb __user *userurb = as->userurb;
1021         void __user *addr = as->userurb;
1022         unsigned int i;
1023
1024         if (as->userbuffer)
1025                 if (copy_to_user(as->userbuffer, urb->transfer_buffer, urb->transfer_buffer_length))
1026                         return -EFAULT;
1027         if (put_user(urb->status, &userurb->status))
1028                 return -EFAULT;
1029         if (put_user(urb->actual_length, &userurb->actual_length))
1030                 return -EFAULT;
1031         if (put_user(urb->error_count, &userurb->error_count))
1032                 return -EFAULT;
1033
1034         if (usb_pipeisoc(urb->pipe)) {
1035                 for (i = 0; i < urb->number_of_packets; i++) {
1036                         if (put_user(urb->iso_frame_desc[i].actual_length,
1037                                      &userurb->iso_frame_desc[i].actual_length))
1038                                 return -EFAULT;
1039                         if (put_user(urb->iso_frame_desc[i].status,
1040                                      &userurb->iso_frame_desc[i].status))
1041                                 return -EFAULT;
1042                 }
1043         }
1044
1045         free_async(as);
1046
1047         if (put_user(addr, (void __user * __user *)arg))
1048                 return -EFAULT;
1049         return 0;
1050 }
1051
1052 static struct async* reap_as(struct dev_state *ps)
1053 {
1054         DECLARE_WAITQUEUE(wait, current);
1055         struct async *as = NULL;
1056         struct usb_device *dev = ps->dev;
1057
1058         add_wait_queue(&ps->wait, &wait);
1059         for (;;) {
1060                 __set_current_state(TASK_INTERRUPTIBLE);
1061                 if ((as = async_getcompleted(ps)))
1062                         break;
1063                 if (signal_pending(current))
1064                         break;
1065                 usb_unlock_device(dev);
1066                 schedule();
1067                 usb_lock_device(dev);
1068         }
1069         remove_wait_queue(&ps->wait, &wait);
1070         set_current_state(TASK_RUNNING);
1071         return as;
1072 }
1073
1074 static int proc_reapurb(struct dev_state *ps, void __user *arg)
1075 {
1076         struct async *as = reap_as(ps);
1077         if (as)
1078                 return processcompl(as, (void __user * __user *)arg);
1079         if (signal_pending(current))
1080                 return -EINTR;
1081         return -EIO;
1082 }
1083
1084 static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
1085 {
1086         struct async *as;
1087
1088         if (!(as = async_getcompleted(ps)))
1089                 return -EAGAIN;
1090         return processcompl(as, (void __user * __user *)arg);
1091 }
1092
1093 #ifdef CONFIG_COMPAT
1094
1095 static int get_urb32(struct usbdevfs_urb *kurb,
1096                      struct usbdevfs_urb32 __user *uurb)
1097 {
1098         __u32  uptr;
1099         if (get_user(kurb->type, &uurb->type) ||
1100             __get_user(kurb->endpoint, &uurb->endpoint) ||
1101             __get_user(kurb->status, &uurb->status) ||
1102             __get_user(kurb->flags, &uurb->flags) ||
1103             __get_user(kurb->buffer_length, &uurb->buffer_length) ||
1104             __get_user(kurb->actual_length, &uurb->actual_length) ||
1105             __get_user(kurb->start_frame, &uurb->start_frame) ||
1106             __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
1107             __get_user(kurb->error_count, &uurb->error_count) ||
1108             __get_user(kurb->signr, &uurb->signr))
1109                 return -EFAULT;
1110
1111         if (__get_user(uptr, &uurb->buffer))
1112                 return -EFAULT;
1113         kurb->buffer = compat_ptr(uptr);
1114         if (__get_user(uptr, &uurb->buffer))
1115                 return -EFAULT;
1116         kurb->usercontext = compat_ptr(uptr);
1117
1118         return 0;
1119 }
1120
1121 static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
1122 {
1123         struct usbdevfs_urb uurb;
1124
1125         if (get_urb32(&uurb,(struct usbdevfs_urb32 *)arg))
1126                 return -EFAULT;
1127
1128         return proc_do_submiturb(ps, &uurb, ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc, arg);
1129 }
1130
1131 static int processcompl_compat(struct async *as, void __user * __user *arg)
1132 {
1133         struct urb *urb = as->urb;
1134         struct usbdevfs_urb32 __user *userurb = as->userurb;
1135         void __user *addr = as->userurb;
1136         unsigned int i;
1137
1138         if (as->userbuffer)
1139                 if (copy_to_user(as->userbuffer, urb->transfer_buffer, urb->transfer_buffer_length))
1140                         return -EFAULT;
1141         if (put_user(urb->status, &userurb->status))
1142                 return -EFAULT;
1143         if (put_user(urb->actual_length, &userurb->actual_length))
1144                 return -EFAULT;
1145         if (put_user(urb->error_count, &userurb->error_count))
1146                 return -EFAULT;
1147
1148         if (usb_pipeisoc(urb->pipe)) {
1149                 for (i = 0; i < urb->number_of_packets; i++) {
1150                         if (put_user(urb->iso_frame_desc[i].actual_length,
1151                                      &userurb->iso_frame_desc[i].actual_length))
1152                                 return -EFAULT;
1153                         if (put_user(urb->iso_frame_desc[i].status,
1154                                      &userurb->iso_frame_desc[i].status))
1155                                 return -EFAULT;
1156                 }
1157         }
1158
1159         free_async(as);
1160         if (put_user((u32)(u64)addr, (u32 __user *)arg))
1161                 return -EFAULT;
1162         return 0;
1163 }
1164
1165 static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
1166 {
1167         struct async *as = reap_as(ps);
1168         if (as)
1169                 return processcompl_compat(as, (void __user * __user *)arg);
1170         if (signal_pending(current))
1171                 return -EINTR;
1172         return -EIO;
1173 }
1174
1175 static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
1176 {
1177         struct async *as;
1178
1179         if (!(as = async_getcompleted(ps)))
1180                 return -EAGAIN;
1181         return processcompl_compat(as, (void __user * __user *)arg);
1182 }
1183
1184 #endif
1185
1186 static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
1187 {
1188         struct usbdevfs_disconnectsignal ds;
1189
1190         if (copy_from_user(&ds, arg, sizeof(ds)))
1191                 return -EFAULT;
1192         if (ds.signr != 0 && (ds.signr < SIGRTMIN || ds.signr > SIGRTMAX))
1193                 return -EINVAL;
1194         ps->discsignr = ds.signr;
1195         ps->disccontext = ds.context;
1196         return 0;
1197 }
1198
1199 static int proc_claiminterface(struct dev_state *ps, void __user *arg)
1200 {
1201         unsigned int ifnum;
1202
1203         if (get_user(ifnum, (unsigned int __user *)arg))
1204                 return -EFAULT;
1205         return claimintf(ps, ifnum);
1206 }
1207
1208 static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
1209 {
1210         unsigned int ifnum;
1211         int ret;
1212
1213         if (get_user(ifnum, (unsigned int __user *)arg))
1214                 return -EFAULT;
1215         if ((ret = releaseintf(ps, ifnum)) < 0)
1216                 return ret;
1217         destroy_async_on_interface (ps, ifnum);
1218         return 0;
1219 }
1220
1221 static int proc_ioctl (struct dev_state *ps, void __user *arg)
1222 {
1223         struct usbdevfs_ioctl   ctrl;
1224         int                     size;
1225         void                    *buf = NULL;
1226         int                     retval = 0;
1227         struct usb_interface    *intf = NULL;
1228         struct usb_driver       *driver = NULL;
1229         int                     i;
1230
1231         /* get input parameters and alloc buffer */
1232         if (copy_from_user(&ctrl, arg, sizeof (ctrl)))
1233                 return -EFAULT;
1234         if ((size = _IOC_SIZE (ctrl.ioctl_code)) > 0) {
1235                 if ((buf = kmalloc (size, GFP_KERNEL)) == NULL)
1236                         return -ENOMEM;
1237                 if ((_IOC_DIR(ctrl.ioctl_code) & _IOC_WRITE)) {
1238                         if (copy_from_user (buf, ctrl.data, size)) {
1239                                 kfree(buf);
1240                                 return -EFAULT;
1241                         }
1242                 } else {
1243                         memset (buf, 0, size);
1244                 }
1245         }
1246
1247         if (!connected(ps->dev)) {
1248                 kfree(buf);
1249                 return -ENODEV;
1250         }
1251
1252         if (ps->dev->state != USB_STATE_CONFIGURED)
1253                 retval = -EHOSTUNREACH;
1254         else if (!(intf = usb_ifnum_to_if (ps->dev, ctrl.ifno)))
1255                retval = -EINVAL;
1256         else switch (ctrl.ioctl_code) {
1257
1258         /* disconnect kernel driver from interface */
1259         case USBDEVFS_DISCONNECT:
1260
1261                 /* don't allow the user to unbind the hub driver from
1262                  * a hub with children to manage */
1263                 for (i = 0; i < ps->dev->maxchild; ++i) {
1264                         if (ps->dev->children[i])
1265                                 retval = -EBUSY;
1266                 }
1267                 if (retval)
1268                         break;
1269
1270                 down_write(&usb_bus_type.subsys.rwsem);
1271                 if (intf->dev.driver) {
1272                         driver = to_usb_driver(intf->dev.driver);
1273                         dev_dbg (&intf->dev, "disconnect by usbfs\n");
1274                         usb_driver_release_interface(driver, intf);
1275                 } else
1276                         retval = -ENODATA;
1277                 up_write(&usb_bus_type.subsys.rwsem);
1278                 break;
1279
1280         /* let kernel drivers try to (re)bind to the interface */
1281         case USBDEVFS_CONNECT:
1282                 usb_unlock_device(ps->dev);
1283                 usb_lock_all_devices();
1284                 bus_rescan_devices(intf->dev.bus);
1285                 usb_unlock_all_devices();
1286                 usb_lock_device(ps->dev);
1287                 break;
1288
1289         /* talk directly to the interface's driver */
1290         default:
1291                 down_read(&usb_bus_type.subsys.rwsem);
1292                 if (intf->dev.driver)
1293                         driver = to_usb_driver(intf->dev.driver);
1294                 if (driver == NULL || driver->ioctl == NULL) {
1295                         retval = -ENOTTY;
1296                 } else {
1297                         retval = driver->ioctl (intf, ctrl.ioctl_code, buf);
1298                         if (retval == -ENOIOCTLCMD)
1299                                 retval = -ENOTTY;
1300                 }
1301                 up_read(&usb_bus_type.subsys.rwsem);
1302         }
1303
1304         /* cleanup and return */
1305         if (retval >= 0
1306                         && (_IOC_DIR (ctrl.ioctl_code) & _IOC_READ) != 0
1307                         && size > 0
1308                         && copy_to_user (ctrl.data, buf, size) != 0)
1309                 retval = -EFAULT;
1310
1311         kfree(buf);
1312         return retval;
1313 }
1314
1315 /*
1316  * NOTE:  All requests here that have interface numbers as parameters
1317  * are assuming that somehow the configuration has been prevented from
1318  * changing.  But there's no mechanism to ensure that...
1319  */
1320 static int usbdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1321 {
1322         struct dev_state *ps = (struct dev_state *)file->private_data;
1323         struct usb_device *dev = ps->dev;
1324         void __user *p = (void __user *)arg;
1325         int ret = -ENOTTY;
1326
1327         if (!(file->f_mode & FMODE_WRITE))
1328                 return -EPERM;
1329         usb_lock_device(dev);
1330         if (!connected(dev)) {
1331                 usb_unlock_device(dev);
1332                 return -ENODEV;
1333         }
1334
1335         switch (cmd) {
1336         case USBDEVFS_CONTROL:
1337                 snoop(&dev->dev, "%s: CONTROL\n", __FUNCTION__);
1338                 ret = proc_control(ps, p);
1339                 if (ret >= 0)
1340                         inode->i_mtime = CURRENT_TIME;
1341                 break;
1342
1343         case USBDEVFS_BULK:
1344                 snoop(&dev->dev, "%s: BULK\n", __FUNCTION__);
1345                 ret = proc_bulk(ps, p);
1346                 if (ret >= 0)
1347                         inode->i_mtime = CURRENT_TIME;
1348                 break;
1349
1350         case USBDEVFS_RESETEP:
1351                 snoop(&dev->dev, "%s: RESETEP\n", __FUNCTION__);
1352                 ret = proc_resetep(ps, p);
1353                 if (ret >= 0)
1354                         inode->i_mtime = CURRENT_TIME;
1355                 break;
1356
1357         case USBDEVFS_RESET:
1358                 snoop(&dev->dev, "%s: RESET\n", __FUNCTION__);
1359                 ret = proc_resetdevice(ps);
1360                 break;
1361
1362         case USBDEVFS_CLEAR_HALT:
1363                 snoop(&dev->dev, "%s: CLEAR_HALT\n", __FUNCTION__);
1364                 ret = proc_clearhalt(ps, p);
1365                 if (ret >= 0)
1366                         inode->i_mtime = CURRENT_TIME;
1367                 break;
1368
1369         case USBDEVFS_GETDRIVER:
1370                 snoop(&dev->dev, "%s: GETDRIVER\n", __FUNCTION__);
1371                 ret = proc_getdriver(ps, p);
1372                 break;
1373
1374         case USBDEVFS_CONNECTINFO:
1375                 snoop(&dev->dev, "%s: CONNECTINFO\n", __FUNCTION__);
1376                 ret = proc_connectinfo(ps, p);
1377                 break;
1378
1379         case USBDEVFS_SETINTERFACE:
1380                 snoop(&dev->dev, "%s: SETINTERFACE\n", __FUNCTION__);
1381                 ret = proc_setintf(ps, p);
1382                 break;
1383
1384         case USBDEVFS_SETCONFIGURATION:
1385                 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __FUNCTION__);
1386                 ret = proc_setconfig(ps, p);
1387                 break;
1388
1389         case USBDEVFS_SUBMITURB:
1390                 snoop(&dev->dev, "%s: SUBMITURB\n", __FUNCTION__);
1391                 ret = proc_submiturb(ps, p);
1392                 if (ret >= 0)
1393                         inode->i_mtime = CURRENT_TIME;
1394                 break;
1395
1396 #ifdef CONFIG_COMPAT
1397
1398         case USBDEVFS_SUBMITURB32:
1399                 snoop(&dev->dev, "%s: SUBMITURB32\n", __FUNCTION__);
1400                 ret = proc_submiturb_compat(ps, p);
1401                 if (ret >= 0)
1402                         inode->i_mtime = CURRENT_TIME;
1403                 break;
1404
1405         case USBDEVFS_REAPURB32:
1406                 snoop(&dev->dev, "%s: REAPURB32\n", __FUNCTION__);
1407                 ret = proc_reapurb_compat(ps, p);
1408                 break;
1409
1410         case USBDEVFS_REAPURBNDELAY32:
1411                 snoop(&dev->dev, "%s: REAPURBDELAY32\n", __FUNCTION__);
1412                 ret = proc_reapurbnonblock_compat(ps, p);
1413                 break;
1414
1415 #endif
1416
1417         case USBDEVFS_DISCARDURB:
1418                 snoop(&dev->dev, "%s: DISCARDURB\n", __FUNCTION__);
1419                 ret = proc_unlinkurb(ps, p);
1420                 break;
1421
1422         case USBDEVFS_REAPURB:
1423                 snoop(&dev->dev, "%s: REAPURB\n", __FUNCTION__);
1424                 ret = proc_reapurb(ps, p);
1425                 break;
1426
1427         case USBDEVFS_REAPURBNDELAY:
1428                 snoop(&dev->dev, "%s: REAPURBDELAY\n", __FUNCTION__);
1429                 ret = proc_reapurbnonblock(ps, p);
1430                 break;
1431
1432         case USBDEVFS_DISCSIGNAL:
1433                 snoop(&dev->dev, "%s: DISCSIGNAL\n", __FUNCTION__);
1434                 ret = proc_disconnectsignal(ps, p);
1435                 break;
1436
1437         case USBDEVFS_CLAIMINTERFACE:
1438                 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __FUNCTION__);
1439                 ret = proc_claiminterface(ps, p);
1440                 break;
1441
1442         case USBDEVFS_RELEASEINTERFACE:
1443                 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __FUNCTION__);
1444                 ret = proc_releaseinterface(ps, p);
1445                 break;
1446
1447         case USBDEVFS_IOCTL:
1448                 snoop(&dev->dev, "%s: IOCTL\n", __FUNCTION__);
1449                 ret = proc_ioctl(ps, p);
1450                 break;
1451         }
1452         usb_unlock_device(dev);
1453         if (ret >= 0)
1454                 inode->i_atime = CURRENT_TIME;
1455         return ret;
1456 }
1457
1458 /* No kernel lock - fine */
1459 static unsigned int usbdev_poll(struct file *file, struct poll_table_struct *wait)
1460 {
1461         struct dev_state *ps = (struct dev_state *)file->private_data;
1462         unsigned int mask = 0;
1463
1464         poll_wait(file, &ps->wait, wait);
1465         if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
1466                 mask |= POLLOUT | POLLWRNORM;
1467         if (!connected(ps->dev))
1468                 mask |= POLLERR | POLLHUP;
1469         return mask;
1470 }
1471
1472 struct file_operations usbfs_device_file_operations = {
1473         .llseek =       usbdev_lseek,
1474         .read =         usbdev_read,
1475         .poll =         usbdev_poll,
1476         .ioctl =        usbdev_ioctl,
1477         .open =         usbdev_open,
1478         .release =      usbdev_release,
1479 };