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