hcm's fixes
[kernel-power] / usbhost / drivers / usb2 / 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  *  This file implements the usbfs/x/y files, where
23  *  x is the bus number and y the device number.
24  *
25  *  It allows user space programs/"drivers" to communicate directly
26  *  with USB devices without intervening kernel driver.
27  *
28  *  Revision history
29  *    22.12.1999   0.1   Initial release (split from proc_usb.c)
30  *    04.01.2000   0.2   Turned into its own filesystem
31  *    30.09.2005   0.3   Fix user-triggerable oops in async URB delivery
32  *                       (CAN-2005-3055)
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 <linux/cdev.h>
47 #include <linux/notifier.h>
48 #include <linux/security.h>
49 #include <asm/uaccess.h>
50 #include <asm/byteorder.h>
51 #include <linux/moduleparam.h>
52
53 #include "hcd.h"        /* for usbcore internals */
54 #include "usb.h"
55
56 #define USB_MAXBUS                      64
57 #define USB_DEVICE_MAX                  USB_MAXBUS * 128
58
59 /* Mutual exclusion for removal, open, and release */
60 DEFINE_MUTEX(usbfs_mutex);
61
62 struct dev_state {
63         struct list_head list;      /* state list */
64         struct usb_device *dev;
65         struct file *file;
66         spinlock_t lock;            /* protects the async urb lists */
67         struct list_head async_pending;
68         struct list_head async_completed;
69         wait_queue_head_t wait;     /* wake up if a request completed */
70         unsigned int discsignr;
71         struct pid *disc_pid;
72         uid_t disc_uid, disc_euid;
73         void __user *disccontext;
74         unsigned long ifclaimed;
75         u32 secid;
76 };
77
78 struct async {
79         struct list_head asynclist;
80         struct dev_state *ps;
81         struct pid *pid;
82         uid_t uid, euid;
83         unsigned int signr;
84         unsigned int ifnum;
85         void __user *userbuffer;
86         void __user *userurb;
87         struct urb *urb;
88         int status;
89         u32 secid;
90 };
91
92 static int usbfs_snoop;
93 module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
94 MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
95
96 #define snoop(dev, format, arg...)                              \
97         do {                                                    \
98                 if (usbfs_snoop)                                \
99                         dev_info(dev , format , ## arg);        \
100         } while (0)
101
102 #define USB_DEVICE_DEV          MKDEV(USB_DEVICE_MAJOR, 0)
103
104
105 #define MAX_USBFS_BUFFER_SIZE   16384
106
107 static inline int connected(struct dev_state *ps)
108 {
109         return (!list_empty(&ps->list) &&
110                         ps->dev->state != USB_STATE_NOTATTACHED);
111 }
112
113 static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
114 {
115         loff_t ret;
116
117         lock_kernel();
118
119         switch (orig) {
120         case 0:
121                 file->f_pos = offset;
122                 ret = file->f_pos;
123                 break;
124         case 1:
125                 file->f_pos += offset;
126                 ret = file->f_pos;
127                 break;
128         case 2:
129         default:
130                 ret = -EINVAL;
131         }
132
133         unlock_kernel();
134         return ret;
135 }
136
137 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
138                            loff_t *ppos)
139 {
140         struct dev_state *ps = file->private_data;
141         struct usb_device *dev = ps->dev;
142         ssize_t ret = 0;
143         unsigned len;
144         loff_t pos;
145         int i;
146
147         pos = *ppos;
148         usb_lock_device(dev);
149         if (!connected(ps)) {
150                 ret = -ENODEV;
151                 goto err;
152         } else if (pos < 0) {
153                 ret = -EINVAL;
154                 goto err;
155         }
156
157         if (pos < sizeof(struct usb_device_descriptor)) {
158                 /* 18 bytes - fits on the stack */
159                 struct usb_device_descriptor temp_desc;
160
161                 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
162                 le16_to_cpus(&temp_desc.bcdUSB);
163                 le16_to_cpus(&temp_desc.idVendor);
164                 le16_to_cpus(&temp_desc.idProduct);
165                 le16_to_cpus(&temp_desc.bcdDevice);
166
167                 len = sizeof(struct usb_device_descriptor) - pos;
168                 if (len > nbytes)
169                         len = nbytes;
170                 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
171                         ret = -EFAULT;
172                         goto err;
173                 }
174
175                 *ppos += len;
176                 buf += len;
177                 nbytes -= len;
178                 ret += len;
179         }
180
181         pos = sizeof(struct usb_device_descriptor);
182         for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
183                 struct usb_config_descriptor *config =
184                         (struct usb_config_descriptor *)dev->rawdescriptors[i];
185                 unsigned int length = le16_to_cpu(config->wTotalLength);
186
187                 if (*ppos < pos + length) {
188
189                         /* The descriptor may claim to be longer than it
190                          * really is.  Here is the actual allocated length. */
191                         unsigned alloclen =
192                                 le16_to_cpu(dev->config[i].desc.wTotalLength);
193
194                         len = length - (*ppos - pos);
195                         if (len > nbytes)
196                                 len = nbytes;
197
198                         /* Simply don't write (skip over) unallocated parts */
199                         if (alloclen > (*ppos - pos)) {
200                                 alloclen -= (*ppos - pos);
201                                 if (copy_to_user(buf,
202                                     dev->rawdescriptors[i] + (*ppos - pos),
203                                     min(len, alloclen))) {
204                                         ret = -EFAULT;
205                                         goto err;
206                                 }
207                         }
208
209                         *ppos += len;
210                         buf += len;
211                         nbytes -= len;
212                         ret += len;
213                 }
214
215                 pos += length;
216         }
217
218 err:
219         usb_unlock_device(dev);
220         return ret;
221 }
222
223 /*
224  * async list handling
225  */
226
227 static struct async *alloc_async(unsigned int numisoframes)
228 {
229         struct async *as;
230
231         as = kzalloc(sizeof(struct async), GFP_KERNEL);
232         if (!as)
233                 return NULL;
234         as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
235         if (!as->urb) {
236                 kfree(as);
237                 return NULL;
238         }
239         return as;
240 }
241
242 static void free_async(struct async *as)
243 {
244         put_pid(as->pid);
245         kfree(as->urb->transfer_buffer);
246         kfree(as->urb->setup_packet);
247         usb_free_urb(as->urb);
248         kfree(as);
249 }
250
251 static inline void async_newpending(struct async *as)
252 {
253         struct dev_state *ps = as->ps;
254         unsigned long flags;
255
256         spin_lock_irqsave(&ps->lock, flags);
257         list_add_tail(&as->asynclist, &ps->async_pending);
258         spin_unlock_irqrestore(&ps->lock, flags);
259 }
260
261 static inline void async_removepending(struct async *as)
262 {
263         struct dev_state *ps = as->ps;
264         unsigned long flags;
265
266         spin_lock_irqsave(&ps->lock, flags);
267         list_del_init(&as->asynclist);
268         spin_unlock_irqrestore(&ps->lock, flags);
269 }
270
271 static inline struct async *async_getcompleted(struct dev_state *ps)
272 {
273         unsigned long flags;
274         struct async *as = NULL;
275
276         spin_lock_irqsave(&ps->lock, flags);
277         if (!list_empty(&ps->async_completed)) {
278                 as = list_entry(ps->async_completed.next, struct async,
279                                 asynclist);
280                 list_del_init(&as->asynclist);
281         }
282         spin_unlock_irqrestore(&ps->lock, flags);
283         return as;
284 }
285
286 static inline struct async *async_getpending(struct dev_state *ps,
287                                              void __user *userurb)
288 {
289         unsigned long flags;
290         struct async *as;
291
292         spin_lock_irqsave(&ps->lock, flags);
293         list_for_each_entry(as, &ps->async_pending, asynclist)
294                 if (as->userurb == userurb) {
295                         list_del_init(&as->asynclist);
296                         spin_unlock_irqrestore(&ps->lock, flags);
297                         return as;
298                 }
299         spin_unlock_irqrestore(&ps->lock, flags);
300         return NULL;
301 }
302
303 static void snoop_urb(struct urb *urb, void __user *userurb)
304 {
305         int j;
306         unsigned char *data = urb->transfer_buffer;
307
308         if (!usbfs_snoop)
309                 return;
310
311         dev_info(&urb->dev->dev, "direction=%s\n",
312                         usb_urb_dir_in(urb) ? "IN" : "OUT");
313         dev_info(&urb->dev->dev, "userurb=%p\n", userurb);
314         dev_info(&urb->dev->dev, "transfer_buffer_length=%d\n",
315                  urb->transfer_buffer_length);
316         dev_info(&urb->dev->dev, "actual_length=%d\n", urb->actual_length);
317         dev_info(&urb->dev->dev, "data: ");
318         for (j = 0; j < urb->transfer_buffer_length; ++j)
319                 printk("%02x ", data[j]);
320         printk("\n");
321 }
322
323 static void async_completed(struct urb *urb)
324 {
325         struct async *as = urb->context;
326         struct dev_state *ps = as->ps;
327         struct siginfo sinfo;
328
329         spin_lock(&ps->lock);
330         list_move_tail(&as->asynclist, &ps->async_completed);
331         spin_unlock(&ps->lock);
332         as->status = urb->status;
333         if (as->signr) {
334                 sinfo.si_signo = as->signr;
335                 sinfo.si_errno = as->status;
336                 sinfo.si_code = SI_ASYNCIO;
337                 sinfo.si_addr = as->userurb;
338                 kill_pid_info_as_uid(as->signr, &sinfo, as->pid, as->uid,
339                                       as->euid, as->secid);
340         }
341         snoop(&urb->dev->dev, "urb complete\n");
342         snoop_urb(urb, as->userurb);
343         wake_up(&ps->wait);
344 }
345
346 static void destroy_async(struct dev_state *ps, struct list_head *list)
347 {
348         struct async *as;
349         unsigned long flags;
350
351         spin_lock_irqsave(&ps->lock, flags);
352         while (!list_empty(list)) {
353                 as = list_entry(list->next, struct async, asynclist);
354                 list_del_init(&as->asynclist);
355
356                 /* drop the spinlock so the completion handler can run */
357                 spin_unlock_irqrestore(&ps->lock, flags);
358                 usb_kill_urb(as->urb);
359                 spin_lock_irqsave(&ps->lock, flags);
360         }
361         spin_unlock_irqrestore(&ps->lock, flags);
362 }
363
364 static void destroy_async_on_interface(struct dev_state *ps,
365                                        unsigned int ifnum)
366 {
367         struct list_head *p, *q, hitlist;
368         unsigned long flags;
369
370         INIT_LIST_HEAD(&hitlist);
371         spin_lock_irqsave(&ps->lock, flags);
372         list_for_each_safe(p, q, &ps->async_pending)
373                 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
374                         list_move_tail(p, &hitlist);
375         spin_unlock_irqrestore(&ps->lock, flags);
376         destroy_async(ps, &hitlist);
377 }
378
379 static inline void destroy_all_async(struct dev_state *ps)
380 {
381         destroy_async(ps, &ps->async_pending);
382 }
383
384 /*
385  * interface claims are made only at the request of user level code,
386  * which can also release them (explicitly or by closing files).
387  * they're also undone when devices disconnect.
388  */
389
390 static int driver_probe(struct usb_interface *intf,
391                         const struct usb_device_id *id)
392 {
393         return -ENODEV;
394 }
395
396 static void driver_disconnect(struct usb_interface *intf)
397 {
398         struct dev_state *ps = usb_get_intfdata(intf);
399         unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
400
401         if (!ps)
402                 return;
403
404         /* NOTE:  this relies on usbcore having canceled and completed
405          * all pending I/O requests; 2.6 does that.
406          */
407
408         if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
409                 clear_bit(ifnum, &ps->ifclaimed);
410         else
411                 dev_warn(&intf->dev, "interface number %u out of range\n",
412                          ifnum);
413
414         usb_set_intfdata(intf, NULL);
415
416         /* force async requests to complete */
417         destroy_async_on_interface(ps, ifnum);
418 }
419
420 /* The following routines are merely placeholders.  There is no way
421  * to inform a user task about suspend or resumes.
422  */
423 static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
424 {
425         return 0;
426 }
427
428 static int driver_resume(struct usb_interface *intf)
429 {
430         return 0;
431 }
432
433 struct usb_driver usbfs_driver = {
434         .name =         "usbfs",
435         .probe =        driver_probe,
436         .disconnect =   driver_disconnect,
437         .suspend =      driver_suspend,
438         .resume =       driver_resume,
439 };
440
441 static int claimintf(struct dev_state *ps, unsigned int ifnum)
442 {
443         struct usb_device *dev = ps->dev;
444         struct usb_interface *intf;
445         int err;
446
447         if (ifnum >= 8*sizeof(ps->ifclaimed))
448                 return -EINVAL;
449         /* already claimed */
450         if (test_bit(ifnum, &ps->ifclaimed))
451                 return 0;
452
453         intf = usb_ifnum_to_if(dev, ifnum);
454         if (!intf)
455                 err = -ENOENT;
456         else
457                 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
458         if (err == 0)
459                 set_bit(ifnum, &ps->ifclaimed);
460         return err;
461 }
462
463 static int releaseintf(struct dev_state *ps, unsigned int ifnum)
464 {
465         struct usb_device *dev;
466         struct usb_interface *intf;
467         int err;
468
469         err = -EINVAL;
470         if (ifnum >= 8*sizeof(ps->ifclaimed))
471                 return err;
472         dev = ps->dev;
473         intf = usb_ifnum_to_if(dev, ifnum);
474         if (!intf)
475                 err = -ENOENT;
476         else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
477                 usb_driver_release_interface(&usbfs_driver, intf);
478                 err = 0;
479         }
480         return err;
481 }
482
483 static int checkintf(struct dev_state *ps, unsigned int ifnum)
484 {
485         if (ps->dev->state != USB_STATE_CONFIGURED)
486                 return -EHOSTUNREACH;
487         if (ifnum >= 8*sizeof(ps->ifclaimed))
488                 return -EINVAL;
489         if (test_bit(ifnum, &ps->ifclaimed))
490                 return 0;
491         /* if not yet claimed, claim it for the driver */
492         dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
493                  "interface %u before use\n", task_pid_nr(current),
494                  current->comm, ifnum);
495         return claimintf(ps, ifnum);
496 }
497
498 static int findintfep(struct usb_device *dev, unsigned int ep)
499 {
500         unsigned int i, j, e;
501         struct usb_interface *intf;
502         struct usb_host_interface *alts;
503         struct usb_endpoint_descriptor *endpt;
504
505         if (ep & ~(USB_DIR_IN|0xf))
506                 return -EINVAL;
507         if (!dev->actconfig)
508                 return -ESRCH;
509         for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
510                 intf = dev->actconfig->interface[i];
511                 for (j = 0; j < intf->num_altsetting; j++) {
512                         alts = &intf->altsetting[j];
513                         for (e = 0; e < alts->desc.bNumEndpoints; e++) {
514                                 endpt = &alts->endpoint[e].desc;
515                                 if (endpt->bEndpointAddress == ep)
516                                         return alts->desc.bInterfaceNumber;
517                         }
518                 }
519         }
520         return -ENOENT;
521 }
522
523 static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
524                            unsigned int index)
525 {
526         int ret = 0;
527
528         if (ps->dev->state != USB_STATE_ADDRESS
529          && ps->dev->state != USB_STATE_CONFIGURED)
530                 return -EHOSTUNREACH;
531         if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
532                 return 0;
533
534         index &= 0xff;
535         switch (requesttype & USB_RECIP_MASK) {
536         case USB_RECIP_ENDPOINT:
537                 ret = findintfep(ps->dev, index);
538                 if (ret >= 0)
539                         ret = checkintf(ps, ret);
540                 break;
541
542         case USB_RECIP_INTERFACE:
543                 ret = checkintf(ps, index);
544                 break;
545         }
546         return ret;
547 }
548
549 static int match_devt(struct device *dev, void *data)
550 {
551         return dev->devt == (dev_t) (unsigned long) data;
552 }
553
554 static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
555 {
556         struct device *dev;
557
558         dev = bus_find_device(&usb_bus_type, NULL,
559                               (void *) (unsigned long) devt, match_devt);
560         if (!dev)
561                 return NULL;
562         return container_of(dev, struct usb_device, dev);
563 }
564
565 /*
566  * file operations
567  */
568 static int usbdev_open(struct inode *inode, struct file *file)
569 {
570         struct usb_device *dev = NULL;
571         struct dev_state *ps;
572         int ret;
573
574         lock_kernel();
575         /* Protect against simultaneous removal or release */
576         mutex_lock(&usbfs_mutex);
577
578         ret = -ENOMEM;
579         ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
580         if (!ps)
581                 goto out;
582
583         ret = -ENOENT;
584
585         /* usbdev device-node */
586         if (imajor(inode) == USB_DEVICE_MAJOR)
587                 dev = usbdev_lookup_by_devt(inode->i_rdev);
588 #ifdef CONFIG_USB_DEVICEFS
589         /* procfs file */
590         if (!dev) {
591                 dev = inode->i_private;
592                 if (dev && dev->usbfs_dentry &&
593                                         dev->usbfs_dentry->d_inode == inode)
594                         usb_get_dev(dev);
595                 else
596                         dev = NULL;
597         }
598 #endif
599         if (!dev || dev->state == USB_STATE_NOTATTACHED)
600                 goto out;
601         ret = usb_autoresume_device(dev);
602         if (ret)
603                 goto out;
604
605         ret = 0;
606         ps->dev = dev;
607         ps->file = file;
608         spin_lock_init(&ps->lock);
609         INIT_LIST_HEAD(&ps->list);
610         INIT_LIST_HEAD(&ps->async_pending);
611         INIT_LIST_HEAD(&ps->async_completed);
612         init_waitqueue_head(&ps->wait);
613         ps->discsignr = 0;
614         ps->disc_pid = get_pid(task_pid(current));
615         ps->disc_uid = current->uid;
616         ps->disc_euid = current->euid;
617         ps->disccontext = NULL;
618         ps->ifclaimed = 0;
619         security_task_getsecid(current, &ps->secid);
620         smp_wmb();
621         list_add_tail(&ps->list, &dev->filelist);
622         file->private_data = ps;
623         snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
624                         current->comm);
625  out:
626         if (ret) {
627                 kfree(ps);
628                 usb_put_dev(dev);
629         }
630         mutex_unlock(&usbfs_mutex);
631         unlock_kernel();
632         return ret;
633 }
634
635 static int usbdev_release(struct inode *inode, struct file *file)
636 {
637         struct dev_state *ps = file->private_data;
638         struct usb_device *dev = ps->dev;
639         unsigned int ifnum;
640         struct async *as;
641
642         usb_lock_device(dev);
643
644         /* Protect against simultaneous open */
645         mutex_lock(&usbfs_mutex);
646         list_del_init(&ps->list);
647         mutex_unlock(&usbfs_mutex);
648
649         for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
650                         ifnum++) {
651                 if (test_bit(ifnum, &ps->ifclaimed))
652                         releaseintf(ps, ifnum);
653         }
654         destroy_all_async(ps);
655         usb_autosuspend_device(dev);
656         usb_unlock_device(dev);
657         usb_put_dev(dev);
658         put_pid(ps->disc_pid);
659
660         as = async_getcompleted(ps);
661         while (as) {
662                 free_async(as);
663                 as = async_getcompleted(ps);
664         }
665         kfree(ps);
666         return 0;
667 }
668
669 static int proc_control(struct dev_state *ps, void __user *arg)
670 {
671         struct usb_device *dev = ps->dev;
672         struct usbdevfs_ctrltransfer ctrl;
673         unsigned int tmo;
674         unsigned char *tbuf;
675         unsigned wLength;
676         int i, j, ret;
677
678         if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
679                 return -EFAULT;
680         ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex);
681         if (ret)
682                 return ret;
683         wLength = ctrl.wLength;         /* To suppress 64k PAGE_SIZE warning */
684         if (wLength > PAGE_SIZE)
685                 return -EINVAL;
686         tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
687         if (!tbuf)
688                 return -ENOMEM;
689         tmo = ctrl.timeout;
690         if (ctrl.bRequestType & 0x80) {
691                 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
692                                                ctrl.wLength)) {
693                         free_page((unsigned long)tbuf);
694                         return -EINVAL;
695                 }
696                 snoop(&dev->dev, "control read: bRequest=%02x "
697                                 "bRrequestType=%02x wValue=%04x "
698                                 "wIndex=%04x wLength=%04x\n",
699                         ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
700                                 ctrl.wIndex, ctrl.wLength);
701
702                 usb_unlock_device(dev);
703                 i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest,
704                                     ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
705                                     tbuf, ctrl.wLength, tmo);
706                 usb_lock_device(dev);
707                 if ((i > 0) && ctrl.wLength) {
708                         if (usbfs_snoop) {
709                                 dev_info(&dev->dev, "control read: data ");
710                                 for (j = 0; j < i; ++j)
711                                         printk("%02x ", (u8)(tbuf)[j]);
712                                 printk("\n");
713                         }
714                         if (copy_to_user(ctrl.data, tbuf, i)) {
715                                 free_page((unsigned long)tbuf);
716                                 return -EFAULT;
717                         }
718                 }
719         } else {
720                 if (ctrl.wLength) {
721                         if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
722                                 free_page((unsigned long)tbuf);
723                                 return -EFAULT;
724                         }
725                 }
726                 snoop(&dev->dev, "control write: bRequest=%02x "
727                                 "bRrequestType=%02x wValue=%04x "
728                                 "wIndex=%04x wLength=%04x\n",
729                         ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
730                                 ctrl.wIndex, ctrl.wLength);
731                 if (usbfs_snoop) {
732                         dev_info(&dev->dev, "control write: data: ");
733                         for (j = 0; j < ctrl.wLength; ++j)
734                                 printk("%02x ", (unsigned char)(tbuf)[j]);
735                         printk("\n");
736                 }
737                 usb_unlock_device(dev);
738                 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
739                                     ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
740                                     tbuf, ctrl.wLength, tmo);
741                 usb_lock_device(dev);
742         }
743         free_page((unsigned long)tbuf);
744         if (i < 0 && i != -EPIPE) {
745                 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
746                            "failed cmd %s rqt %u rq %u len %u ret %d\n",
747                            current->comm, ctrl.bRequestType, ctrl.bRequest,
748                            ctrl.wLength, i);
749         }
750         return i;
751 }
752
753 static int proc_bulk(struct dev_state *ps, void __user *arg)
754 {
755         struct usb_device *dev = ps->dev;
756         struct usbdevfs_bulktransfer bulk;
757         unsigned int tmo, len1, pipe;
758         int len2;
759         unsigned char *tbuf;
760         int i, j, ret;
761
762         if (copy_from_user(&bulk, arg, sizeof(bulk)))
763                 return -EFAULT;
764         ret = findintfep(ps->dev, bulk.ep);
765         if (ret < 0)
766                 return ret;
767         ret = checkintf(ps, ret);
768         if (ret)
769                 return ret;
770         if (bulk.ep & USB_DIR_IN)
771                 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
772         else
773                 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
774         if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
775                 return -EINVAL;
776         len1 = bulk.len;
777         if (len1 > MAX_USBFS_BUFFER_SIZE)
778                 return -EINVAL;
779         if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
780                 return -ENOMEM;
781         tmo = bulk.timeout;
782         if (bulk.ep & 0x80) {
783                 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
784                         kfree(tbuf);
785                         return -EINVAL;
786                 }
787                 snoop(&dev->dev, "bulk read: len=0x%02x timeout=%04d\n",
788                         bulk.len, bulk.timeout);
789                 usb_unlock_device(dev);
790                 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
791                 usb_lock_device(dev);
792                 if (!i && len2) {
793                         if (usbfs_snoop) {
794                                 dev_info(&dev->dev, "bulk read: data ");
795                                 for (j = 0; j < len2; ++j)
796                                         printk("%02x ", (u8)(tbuf)[j]);
797                                 printk("\n");
798                         }
799                         if (copy_to_user(bulk.data, tbuf, len2)) {
800                                 kfree(tbuf);
801                                 return -EFAULT;
802                         }
803                 }
804         } else {
805                 if (len1) {
806                         if (copy_from_user(tbuf, bulk.data, len1)) {
807                                 kfree(tbuf);
808                                 return -EFAULT;
809                         }
810                 }
811                 snoop(&dev->dev, "bulk write: len=0x%02x timeout=%04d\n",
812                         bulk.len, bulk.timeout);
813                 if (usbfs_snoop) {
814                         dev_info(&dev->dev, "bulk write: data: ");
815                         for (j = 0; j < len1; ++j)
816                                 printk("%02x ", (unsigned char)(tbuf)[j]);
817                         printk("\n");
818                 }
819                 usb_unlock_device(dev);
820                 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
821                 usb_lock_device(dev);
822         }
823         kfree(tbuf);
824         if (i < 0)
825                 return i;
826         return len2;
827 }
828
829 static int proc_resetep(struct dev_state *ps, void __user *arg)
830 {
831         unsigned int ep;
832         int ret;
833
834         if (get_user(ep, (unsigned int __user *)arg))
835                 return -EFAULT;
836         ret = findintfep(ps->dev, ep);
837         if (ret < 0)
838                 return ret;
839         ret = checkintf(ps, ret);
840         if (ret)
841                 return ret;
842         usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0);
843         return 0;
844 }
845
846 static int proc_clearhalt(struct dev_state *ps, void __user *arg)
847 {
848         unsigned int ep;
849         int pipe;
850         int ret;
851
852         if (get_user(ep, (unsigned int __user *)arg))
853                 return -EFAULT;
854         ret = findintfep(ps->dev, ep);
855         if (ret < 0)
856                 return ret;
857         ret = checkintf(ps, ret);
858         if (ret)
859                 return ret;
860         if (ep & USB_DIR_IN)
861                 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
862         else
863                 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
864
865         return usb_clear_halt(ps->dev, pipe);
866 }
867
868 static int proc_getdriver(struct dev_state *ps, void __user *arg)
869 {
870         struct usbdevfs_getdriver gd;
871         struct usb_interface *intf;
872         int ret;
873
874         if (copy_from_user(&gd, arg, sizeof(gd)))
875                 return -EFAULT;
876         intf = usb_ifnum_to_if(ps->dev, gd.interface);
877         if (!intf || !intf->dev.driver)
878                 ret = -ENODATA;
879         else {
880                 strncpy(gd.driver, intf->dev.driver->name,
881                                 sizeof(gd.driver));
882                 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
883         }
884         return ret;
885 }
886
887 static int proc_connectinfo(struct dev_state *ps, void __user *arg)
888 {
889         struct usbdevfs_connectinfo ci;
890
891         ci.devnum = ps->dev->devnum;
892         ci.slow = ps->dev->speed == USB_SPEED_LOW;
893         if (copy_to_user(arg, &ci, sizeof(ci)))
894                 return -EFAULT;
895         return 0;
896 }
897
898 static int proc_resetdevice(struct dev_state *ps)
899 {
900         return usb_reset_device(ps->dev);
901 }
902
903 static int proc_setintf(struct dev_state *ps, void __user *arg)
904 {
905         struct usbdevfs_setinterface setintf;
906         int ret;
907
908         if (copy_from_user(&setintf, arg, sizeof(setintf)))
909                 return -EFAULT;
910         if ((ret = checkintf(ps, setintf.interface)))
911                 return ret;
912         return usb_set_interface(ps->dev, setintf.interface,
913                         setintf.altsetting);
914 }
915
916 static int proc_setconfig(struct dev_state *ps, void __user *arg)
917 {
918         int u;
919         int status = 0;
920         struct usb_host_config *actconfig;
921
922         if (get_user(u, (int __user *)arg))
923                 return -EFAULT;
924
925         actconfig = ps->dev->actconfig;
926
927         /* Don't touch the device if any interfaces are claimed.
928          * It could interfere with other drivers' operations, and if
929          * an interface is claimed by usbfs it could easily deadlock.
930          */
931         if (actconfig) {
932                 int i;
933
934                 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
935                         if (usb_interface_claimed(actconfig->interface[i])) {
936                                 dev_warn(&ps->dev->dev,
937                                         "usbfs: interface %d claimed by %s "
938                                         "while '%s' sets config #%d\n",
939                                         actconfig->interface[i]
940                                                 ->cur_altsetting
941                                                 ->desc.bInterfaceNumber,
942                                         actconfig->interface[i]
943                                                 ->dev.driver->name,
944                                         current->comm, u);
945                                 status = -EBUSY;
946                                 break;
947                         }
948                 }
949         }
950
951         /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
952          * so avoid usb_set_configuration()'s kick to sysfs
953          */
954         if (status == 0) {
955                 if (actconfig && actconfig->desc.bConfigurationValue == u)
956                         status = usb_reset_configuration(ps->dev);
957                 else
958                         status = usb_set_configuration(ps->dev, u);
959         }
960
961         return status;
962 }
963
964 static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
965                         struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
966                         void __user *arg)
967 {
968         struct usbdevfs_iso_packet_desc *isopkt = NULL;
969         struct usb_host_endpoint *ep;
970         struct async *as;
971         struct usb_ctrlrequest *dr = NULL;
972         unsigned int u, totlen, isofrmlen;
973         int ret, ifnum = -1;
974         int is_in;
975
976         if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
977                                 USBDEVFS_URB_SHORT_NOT_OK |
978                                 USBDEVFS_URB_NO_FSBR |
979                                 USBDEVFS_URB_ZERO_PACKET |
980                                 USBDEVFS_URB_NO_INTERRUPT))
981                 return -EINVAL;
982         if (!uurb->buffer)
983                 return -EINVAL;
984         if (uurb->signr != 0 && (uurb->signr < SIGRTMIN ||
985                                  uurb->signr > SIGRTMAX))
986                 return -EINVAL;
987         if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
988             (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
989                 ifnum = findintfep(ps->dev, uurb->endpoint);
990                 if (ifnum < 0)
991                         return ifnum;
992                 ret = checkintf(ps, ifnum);
993                 if (ret)
994                         return ret;
995         }
996         if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
997                 is_in = 1;
998                 ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
999         } else {
1000                 is_in = 0;
1001                 ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
1002         }
1003         if (!ep)
1004                 return -ENOENT;
1005         switch(uurb->type) {
1006         case USBDEVFS_URB_TYPE_CONTROL:
1007                 if (!usb_endpoint_xfer_control(&ep->desc))
1008                         return -EINVAL;
1009                 /* min 8 byte setup packet,
1010                  * max 8 byte setup plus an arbitrary data stage */
1011                 if (uurb->buffer_length < 8 ||
1012                     uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE))
1013                         return -EINVAL;
1014                 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1015                 if (!dr)
1016                         return -ENOMEM;
1017                 if (copy_from_user(dr, uurb->buffer, 8)) {
1018                         kfree(dr);
1019                         return -EFAULT;
1020                 }
1021                 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
1022                         kfree(dr);
1023                         return -EINVAL;
1024                 }
1025                 ret = check_ctrlrecip(ps, dr->bRequestType,
1026                                       le16_to_cpup(&dr->wIndex));
1027                 if (ret) {
1028                         kfree(dr);
1029                         return ret;
1030                 }
1031                 uurb->number_of_packets = 0;
1032                 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1033                 uurb->buffer += 8;
1034                 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1035                         is_in = 1;
1036                         uurb->endpoint |= USB_DIR_IN;
1037                 } else {
1038                         is_in = 0;
1039                         uurb->endpoint &= ~USB_DIR_IN;
1040                 }
1041                 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1042                                 uurb->buffer, uurb->buffer_length)) {
1043                         kfree(dr);
1044                         return -EFAULT;
1045                 }
1046                 snoop(&ps->dev->dev, "control urb: bRequest=%02x "
1047                         "bRrequestType=%02x wValue=%04x "
1048                         "wIndex=%04x wLength=%04x\n",
1049                         dr->bRequest, dr->bRequestType,
1050                         __le16_to_cpup(&dr->wValue),
1051                         __le16_to_cpup(&dr->wIndex),
1052                         __le16_to_cpup(&dr->wLength));
1053                 break;
1054
1055         case USBDEVFS_URB_TYPE_BULK:
1056                 switch (usb_endpoint_type(&ep->desc)) {
1057                 case USB_ENDPOINT_XFER_CONTROL:
1058                 case USB_ENDPOINT_XFER_ISOC:
1059                         return -EINVAL;
1060                 /* allow single-shot interrupt transfers, at bogus rates */
1061                 }
1062                 uurb->number_of_packets = 0;
1063                 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1064                         return -EINVAL;
1065                 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1066                                 uurb->buffer, uurb->buffer_length))
1067                         return -EFAULT;
1068                 snoop(&ps->dev->dev, "bulk urb\n");
1069                 break;
1070
1071         case USBDEVFS_URB_TYPE_ISO:
1072                 /* arbitrary limit */
1073                 if (uurb->number_of_packets < 1 ||
1074                     uurb->number_of_packets > 128)
1075                         return -EINVAL;
1076                 if (!usb_endpoint_xfer_isoc(&ep->desc))
1077                         return -EINVAL;
1078                 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1079                                    uurb->number_of_packets;
1080                 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
1081                         return -ENOMEM;
1082                 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
1083                         kfree(isopkt);
1084                         return -EFAULT;
1085                 }
1086                 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1087                         /* arbitrary limit,
1088                          * sufficient for USB 2.0 high-bandwidth iso */
1089                         if (isopkt[u].length > 8192) {
1090                                 kfree(isopkt);
1091                                 return -EINVAL;
1092                         }
1093                         totlen += isopkt[u].length;
1094                 }
1095                 if (totlen > 32768) {
1096                         kfree(isopkt);
1097                         return -EINVAL;
1098                 }
1099                 uurb->buffer_length = totlen;
1100                 snoop(&ps->dev->dev, "iso urb\n");
1101                 break;
1102
1103         case USBDEVFS_URB_TYPE_INTERRUPT:
1104                 uurb->number_of_packets = 0;
1105                 if (!usb_endpoint_xfer_int(&ep->desc))
1106                         return -EINVAL;
1107                 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1108                         return -EINVAL;
1109                 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1110                                 uurb->buffer, uurb->buffer_length))
1111                         return -EFAULT;
1112                 snoop(&ps->dev->dev, "interrupt urb\n");
1113                 break;
1114
1115         default:
1116                 return -EINVAL;
1117         }
1118         as = alloc_async(uurb->number_of_packets);
1119         if (!as) {
1120                 kfree(isopkt);
1121                 kfree(dr);
1122                 return -ENOMEM;
1123         }
1124         as->urb->transfer_buffer = kmalloc(uurb->buffer_length, GFP_KERNEL);
1125         if (!as->urb->transfer_buffer) {
1126                 kfree(isopkt);
1127                 kfree(dr);
1128                 free_async(as);
1129                 return -ENOMEM;
1130         }
1131         as->urb->dev = ps->dev;
1132         as->urb->pipe = (uurb->type << 30) |
1133                         __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1134                         (uurb->endpoint & USB_DIR_IN);
1135
1136         /* This tedious sequence is necessary because the URB_* flags
1137          * are internal to the kernel and subject to change, whereas
1138          * the USBDEVFS_URB_* flags are a user API and must not be changed.
1139          */
1140         u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
1141         if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
1142                 u |= URB_ISO_ASAP;
1143         if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
1144                 u |= URB_SHORT_NOT_OK;
1145         if (uurb->flags & USBDEVFS_URB_NO_FSBR)
1146                 u |= URB_NO_FSBR;
1147         if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1148                 u |= URB_ZERO_PACKET;
1149         if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
1150                 u |= URB_NO_INTERRUPT;
1151         as->urb->transfer_flags = u;
1152
1153         as->urb->transfer_buffer_length = uurb->buffer_length;
1154         as->urb->setup_packet = (unsigned char *)dr;
1155         as->urb->start_frame = uurb->start_frame;
1156         as->urb->number_of_packets = uurb->number_of_packets;
1157         if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1158                         ps->dev->speed == USB_SPEED_HIGH)
1159                 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
1160         else
1161                 as->urb->interval = ep->desc.bInterval;
1162         as->urb->context = as;
1163         as->urb->complete = async_completed;
1164         for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1165                 as->urb->iso_frame_desc[u].offset = totlen;
1166                 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1167                 totlen += isopkt[u].length;
1168         }
1169         kfree(isopkt);
1170         as->ps = ps;
1171         as->userurb = arg;
1172         if (uurb->endpoint & USB_DIR_IN)
1173                 as->userbuffer = uurb->buffer;
1174         else
1175                 as->userbuffer = NULL;
1176         as->signr = uurb->signr;
1177         as->ifnum = ifnum;
1178         as->pid = get_pid(task_pid(current));
1179         as->uid = current->uid;
1180         as->euid = current->euid;
1181         security_task_getsecid(current, &as->secid);
1182         if (!is_in) {
1183                 if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
1184                                 as->urb->transfer_buffer_length)) {
1185                         free_async(as);
1186                         return -EFAULT;
1187                 }
1188         }
1189         snoop_urb(as->urb, as->userurb);
1190         async_newpending(as);
1191         if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
1192                 dev_printk(KERN_DEBUG, &ps->dev->dev,
1193                            "usbfs: usb_submit_urb returned %d\n", ret);
1194                 async_removepending(as);
1195                 free_async(as);
1196                 return ret;
1197         }
1198         return 0;
1199 }
1200
1201 static int proc_submiturb(struct dev_state *ps, void __user *arg)
1202 {
1203         struct usbdevfs_urb uurb;
1204
1205         if (copy_from_user(&uurb, arg, sizeof(uurb)))
1206                 return -EFAULT;
1207
1208         return proc_do_submiturb(ps, &uurb,
1209                         (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1210                         arg);
1211 }
1212
1213 static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
1214 {
1215         struct async *as;
1216
1217         as = async_getpending(ps, arg);
1218         if (!as)
1219                 return -EINVAL;
1220         usb_kill_urb(as->urb);
1221         return 0;
1222 }
1223
1224 static int processcompl(struct async *as, void __user * __user *arg)
1225 {
1226         struct urb *urb = as->urb;
1227         struct usbdevfs_urb __user *userurb = as->userurb;
1228         void __user *addr = as->userurb;
1229         unsigned int i;
1230
1231         if (as->userbuffer)
1232                 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1233                                  urb->transfer_buffer_length))
1234                         return -EFAULT;
1235         if (put_user(as->status, &userurb->status))
1236                 return -EFAULT;
1237         if (put_user(urb->actual_length, &userurb->actual_length))
1238                 return -EFAULT;
1239         if (put_user(urb->error_count, &userurb->error_count))
1240                 return -EFAULT;
1241
1242         if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1243                 for (i = 0; i < urb->number_of_packets; i++) {
1244                         if (put_user(urb->iso_frame_desc[i].actual_length,
1245                                      &userurb->iso_frame_desc[i].actual_length))
1246                                 return -EFAULT;
1247                         if (put_user(urb->iso_frame_desc[i].status,
1248                                      &userurb->iso_frame_desc[i].status))
1249                                 return -EFAULT;
1250                 }
1251         }
1252
1253         free_async(as);
1254
1255         if (put_user(addr, (void __user * __user *)arg))
1256                 return -EFAULT;
1257         return 0;
1258 }
1259
1260 static struct async *reap_as(struct dev_state *ps)
1261 {
1262         DECLARE_WAITQUEUE(wait, current);
1263         struct async *as = NULL;
1264         struct usb_device *dev = ps->dev;
1265
1266         add_wait_queue(&ps->wait, &wait);
1267         for (;;) {
1268                 __set_current_state(TASK_INTERRUPTIBLE);
1269                 as = async_getcompleted(ps);
1270                 if (as)
1271                         break;
1272                 if (signal_pending(current))
1273                         break;
1274                 usb_unlock_device(dev);
1275                 schedule();
1276                 usb_lock_device(dev);
1277         }
1278         remove_wait_queue(&ps->wait, &wait);
1279         set_current_state(TASK_RUNNING);
1280         return as;
1281 }
1282
1283 static int proc_reapurb(struct dev_state *ps, void __user *arg)
1284 {
1285         struct async *as = reap_as(ps);
1286         if (as)
1287                 return processcompl(as, (void __user * __user *)arg);
1288         if (signal_pending(current))
1289                 return -EINTR;
1290         return -EIO;
1291 }
1292
1293 static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
1294 {
1295         struct async *as;
1296
1297         if (!(as = async_getcompleted(ps)))
1298                 return -EAGAIN;
1299         return processcompl(as, (void __user * __user *)arg);
1300 }
1301
1302 #ifdef CONFIG_COMPAT
1303
1304 static int get_urb32(struct usbdevfs_urb *kurb,
1305                      struct usbdevfs_urb32 __user *uurb)
1306 {
1307         __u32  uptr;
1308         if (get_user(kurb->type, &uurb->type) ||
1309             __get_user(kurb->endpoint, &uurb->endpoint) ||
1310             __get_user(kurb->status, &uurb->status) ||
1311             __get_user(kurb->flags, &uurb->flags) ||
1312             __get_user(kurb->buffer_length, &uurb->buffer_length) ||
1313             __get_user(kurb->actual_length, &uurb->actual_length) ||
1314             __get_user(kurb->start_frame, &uurb->start_frame) ||
1315             __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
1316             __get_user(kurb->error_count, &uurb->error_count) ||
1317             __get_user(kurb->signr, &uurb->signr))
1318                 return -EFAULT;
1319
1320         if (__get_user(uptr, &uurb->buffer))
1321                 return -EFAULT;
1322         kurb->buffer = compat_ptr(uptr);
1323         if (__get_user(uptr, &uurb->buffer))
1324                 return -EFAULT;
1325         kurb->usercontext = compat_ptr(uptr);
1326
1327         return 0;
1328 }
1329
1330 static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
1331 {
1332         struct usbdevfs_urb uurb;
1333
1334         if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
1335                 return -EFAULT;
1336
1337         return proc_do_submiturb(ps, &uurb,
1338                         ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
1339                         arg);
1340 }
1341
1342 static int processcompl_compat(struct async *as, void __user * __user *arg)
1343 {
1344         struct urb *urb = as->urb;
1345         struct usbdevfs_urb32 __user *userurb = as->userurb;
1346         void __user *addr = as->userurb;
1347         unsigned int i;
1348
1349         if (as->userbuffer)
1350                 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1351                                  urb->transfer_buffer_length))
1352                         return -EFAULT;
1353         if (put_user(as->status, &userurb->status))
1354                 return -EFAULT;
1355         if (put_user(urb->actual_length, &userurb->actual_length))
1356                 return -EFAULT;
1357         if (put_user(urb->error_count, &userurb->error_count))
1358                 return -EFAULT;
1359
1360         if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1361                 for (i = 0; i < urb->number_of_packets; i++) {
1362                         if (put_user(urb->iso_frame_desc[i].actual_length,
1363                                      &userurb->iso_frame_desc[i].actual_length))
1364                                 return -EFAULT;
1365                         if (put_user(urb->iso_frame_desc[i].status,
1366                                      &userurb->iso_frame_desc[i].status))
1367                                 return -EFAULT;
1368                 }
1369         }
1370
1371         free_async(as);
1372         if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
1373                 return -EFAULT;
1374         return 0;
1375 }
1376
1377 static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
1378 {
1379         struct async *as = reap_as(ps);
1380         if (as)
1381                 return processcompl_compat(as, (void __user * __user *)arg);
1382         if (signal_pending(current))
1383                 return -EINTR;
1384         return -EIO;
1385 }
1386
1387 static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
1388 {
1389         struct async *as;
1390
1391         if (!(as = async_getcompleted(ps)))
1392                 return -EAGAIN;
1393         return processcompl_compat(as, (void __user * __user *)arg);
1394 }
1395
1396 #endif
1397
1398 static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
1399 {
1400         struct usbdevfs_disconnectsignal ds;
1401
1402         if (copy_from_user(&ds, arg, sizeof(ds)))
1403                 return -EFAULT;
1404         if (ds.signr != 0 && (ds.signr < SIGRTMIN || ds.signr > SIGRTMAX))
1405                 return -EINVAL;
1406         ps->discsignr = ds.signr;
1407         ps->disccontext = ds.context;
1408         return 0;
1409 }
1410
1411 static int proc_claiminterface(struct dev_state *ps, void __user *arg)
1412 {
1413         unsigned int ifnum;
1414
1415         if (get_user(ifnum, (unsigned int __user *)arg))
1416                 return -EFAULT;
1417         return claimintf(ps, ifnum);
1418 }
1419
1420 static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
1421 {
1422         unsigned int ifnum;
1423         int ret;
1424
1425         if (get_user(ifnum, (unsigned int __user *)arg))
1426                 return -EFAULT;
1427         if ((ret = releaseintf(ps, ifnum)) < 0)
1428                 return ret;
1429         destroy_async_on_interface (ps, ifnum);
1430         return 0;
1431 }
1432
1433 static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
1434 {
1435         int                     size;
1436         void                    *buf = NULL;
1437         int                     retval = 0;
1438         struct usb_interface    *intf = NULL;
1439         struct usb_driver       *driver = NULL;
1440
1441         /* alloc buffer */
1442         if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
1443                 if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
1444                         return -ENOMEM;
1445                 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
1446                         if (copy_from_user(buf, ctl->data, size)) {
1447                                 kfree(buf);
1448                                 return -EFAULT;
1449                         }
1450                 } else {
1451                         memset(buf, 0, size);
1452                 }
1453         }
1454
1455         if (!connected(ps)) {
1456                 kfree(buf);
1457                 return -ENODEV;
1458         }
1459
1460         if (ps->dev->state != USB_STATE_CONFIGURED)
1461                 retval = -EHOSTUNREACH;
1462         else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
1463                 retval = -EINVAL;
1464         else switch (ctl->ioctl_code) {
1465
1466         /* disconnect kernel driver from interface */
1467         case USBDEVFS_DISCONNECT:
1468                 if (intf->dev.driver) {
1469                         driver = to_usb_driver(intf->dev.driver);
1470                         dev_dbg(&intf->dev, "disconnect by usbfs\n");
1471                         usb_driver_release_interface(driver, intf);
1472                 } else
1473                         retval = -ENODATA;
1474                 break;
1475
1476         /* let kernel drivers try to (re)bind to the interface */
1477         case USBDEVFS_CONNECT:
1478                 if (!intf->dev.driver)
1479                         retval = device_attach(&intf->dev);
1480                 else
1481                         retval = -EBUSY;
1482                 break;
1483
1484         /* talk directly to the interface's driver */
1485         default:
1486                 if (intf->dev.driver)
1487                         driver = to_usb_driver(intf->dev.driver);
1488                 if (driver == NULL || driver->ioctl == NULL) {
1489                         retval = -ENOTTY;
1490                 } else {
1491                         retval = driver->ioctl(intf, ctl->ioctl_code, buf);
1492                         if (retval == -ENOIOCTLCMD)
1493                                 retval = -ENOTTY;
1494                 }
1495         }
1496
1497         /* cleanup and return */
1498         if (retval >= 0
1499                         && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
1500                         && size > 0
1501                         && copy_to_user(ctl->data, buf, size) != 0)
1502                 retval = -EFAULT;
1503
1504         kfree(buf);
1505         return retval;
1506 }
1507
1508 static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
1509 {
1510         struct usbdevfs_ioctl   ctrl;
1511
1512         if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1513                 return -EFAULT;
1514         return proc_ioctl(ps, &ctrl);
1515 }
1516
1517 #ifdef CONFIG_COMPAT
1518 static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
1519 {
1520         struct usbdevfs_ioctl32 __user *uioc;
1521         struct usbdevfs_ioctl ctrl;
1522         u32 udata;
1523
1524         uioc = compat_ptr((long)arg);
1525         if (get_user(ctrl.ifno, &uioc->ifno) ||
1526             get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
1527             __get_user(udata, &uioc->data))
1528                 return -EFAULT;
1529         ctrl.data = compat_ptr(udata);
1530
1531         return proc_ioctl(ps, &ctrl);
1532 }
1533 #endif
1534
1535 /*
1536  * NOTE:  All requests here that have interface numbers as parameters
1537  * are assuming that somehow the configuration has been prevented from
1538  * changing.  But there's no mechanism to ensure that...
1539  */
1540 static int usbdev_ioctl(struct inode *inode, struct file *file,
1541                         unsigned int cmd, unsigned long arg)
1542 {
1543         struct dev_state *ps = file->private_data;
1544         struct usb_device *dev = ps->dev;
1545         void __user *p = (void __user *)arg;
1546         int ret = -ENOTTY;
1547
1548         if (!(file->f_mode & FMODE_WRITE))
1549                 return -EPERM;
1550         usb_lock_device(dev);
1551         if (!connected(ps)) {
1552                 usb_unlock_device(dev);
1553                 return -ENODEV;
1554         }
1555
1556         switch (cmd) {
1557         case USBDEVFS_CONTROL:
1558                 snoop(&dev->dev, "%s: CONTROL\n", __func__);
1559                 ret = proc_control(ps, p);
1560                 if (ret >= 0)
1561                         inode->i_mtime = CURRENT_TIME;
1562                 break;
1563
1564         case USBDEVFS_BULK:
1565                 snoop(&dev->dev, "%s: BULK\n", __func__);
1566                 ret = proc_bulk(ps, p);
1567                 if (ret >= 0)
1568                         inode->i_mtime = CURRENT_TIME;
1569                 break;
1570
1571         case USBDEVFS_RESETEP:
1572                 snoop(&dev->dev, "%s: RESETEP\n", __func__);
1573                 ret = proc_resetep(ps, p);
1574                 if (ret >= 0)
1575                         inode->i_mtime = CURRENT_TIME;
1576                 break;
1577
1578         case USBDEVFS_RESET:
1579                 snoop(&dev->dev, "%s: RESET\n", __func__);
1580                 ret = proc_resetdevice(ps);
1581                 break;
1582
1583         case USBDEVFS_CLEAR_HALT:
1584                 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
1585                 ret = proc_clearhalt(ps, p);
1586                 if (ret >= 0)
1587                         inode->i_mtime = CURRENT_TIME;
1588                 break;
1589
1590         case USBDEVFS_GETDRIVER:
1591                 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
1592                 ret = proc_getdriver(ps, p);
1593                 break;
1594
1595         case USBDEVFS_CONNECTINFO:
1596                 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
1597                 ret = proc_connectinfo(ps, p);
1598                 break;
1599
1600         case USBDEVFS_SETINTERFACE:
1601                 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
1602                 ret = proc_setintf(ps, p);
1603                 break;
1604
1605         case USBDEVFS_SETCONFIGURATION:
1606                 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
1607                 ret = proc_setconfig(ps, p);
1608                 break;
1609
1610         case USBDEVFS_SUBMITURB:
1611                 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
1612                 ret = proc_submiturb(ps, p);
1613                 if (ret >= 0)
1614                         inode->i_mtime = CURRENT_TIME;
1615                 break;
1616
1617 #ifdef CONFIG_COMPAT
1618
1619         case USBDEVFS_SUBMITURB32:
1620                 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
1621                 ret = proc_submiturb_compat(ps, p);
1622                 if (ret >= 0)
1623                         inode->i_mtime = CURRENT_TIME;
1624                 break;
1625
1626         case USBDEVFS_REAPURB32:
1627                 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
1628                 ret = proc_reapurb_compat(ps, p);
1629                 break;
1630
1631         case USBDEVFS_REAPURBNDELAY32:
1632                 snoop(&dev->dev, "%s: REAPURBDELAY32\n", __func__);
1633                 ret = proc_reapurbnonblock_compat(ps, p);
1634                 break;
1635
1636         case USBDEVFS_IOCTL32:
1637                 snoop(&dev->dev, "%s: IOCTL\n", __func__);
1638                 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
1639                 break;
1640 #endif
1641
1642         case USBDEVFS_DISCARDURB:
1643                 snoop(&dev->dev, "%s: DISCARDURB\n", __func__);
1644                 ret = proc_unlinkurb(ps, p);
1645                 break;
1646
1647         case USBDEVFS_REAPURB:
1648                 snoop(&dev->dev, "%s: REAPURB\n", __func__);
1649                 ret = proc_reapurb(ps, p);
1650                 break;
1651
1652         case USBDEVFS_REAPURBNDELAY:
1653                 snoop(&dev->dev, "%s: REAPURBDELAY\n", __func__);
1654                 ret = proc_reapurbnonblock(ps, p);
1655                 break;
1656
1657         case USBDEVFS_DISCSIGNAL:
1658                 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
1659                 ret = proc_disconnectsignal(ps, p);
1660                 break;
1661
1662         case USBDEVFS_CLAIMINTERFACE:
1663                 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
1664                 ret = proc_claiminterface(ps, p);
1665                 break;
1666
1667         case USBDEVFS_RELEASEINTERFACE:
1668                 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
1669                 ret = proc_releaseinterface(ps, p);
1670                 break;
1671
1672         case USBDEVFS_IOCTL:
1673                 snoop(&dev->dev, "%s: IOCTL\n", __func__);
1674                 ret = proc_ioctl_default(ps, p);
1675                 break;
1676         }
1677         usb_unlock_device(dev);
1678         if (ret >= 0)
1679                 inode->i_atime = CURRENT_TIME;
1680         return ret;
1681 }
1682
1683 /* No kernel lock - fine */
1684 static unsigned int usbdev_poll(struct file *file,
1685                                 struct poll_table_struct *wait)
1686 {
1687         struct dev_state *ps = file->private_data;
1688         unsigned int mask = 0;
1689
1690         poll_wait(file, &ps->wait, wait);
1691         if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
1692                 mask |= POLLOUT | POLLWRNORM;
1693         if (!connected(ps))
1694                 mask |= POLLERR | POLLHUP;
1695         return mask;
1696 }
1697
1698 const struct file_operations usbdev_file_operations = {
1699         .owner =        THIS_MODULE,
1700         .llseek =       usbdev_lseek,
1701         .read =         usbdev_read,
1702         .poll =         usbdev_poll,
1703         .ioctl =        usbdev_ioctl,
1704         .open =         usbdev_open,
1705         .release =      usbdev_release,
1706 };
1707
1708 static void usbdev_remove(struct usb_device *udev)
1709 {
1710         struct dev_state *ps;
1711         struct siginfo sinfo;
1712
1713         while (!list_empty(&udev->filelist)) {
1714                 ps = list_entry(udev->filelist.next, struct dev_state, list);
1715                 destroy_all_async(ps);
1716                 wake_up_all(&ps->wait);
1717                 list_del_init(&ps->list);
1718                 if (ps->discsignr) {
1719                         sinfo.si_signo = ps->discsignr;
1720                         sinfo.si_errno = EPIPE;
1721                         sinfo.si_code = SI_ASYNCIO;
1722                         sinfo.si_addr = ps->disccontext;
1723                         kill_pid_info_as_uid(ps->discsignr, &sinfo,
1724                                         ps->disc_pid, ps->disc_uid,
1725                                         ps->disc_euid, ps->secid);
1726                 }
1727         }
1728 }
1729
1730 #ifdef CONFIG_USB_DEVICE_CLASS
1731 static struct class *usb_classdev_class;
1732
1733 static int usb_classdev_add(struct usb_device *dev)
1734 {
1735         struct device *cldev;
1736
1737         cldev = device_create(usb_classdev_class, &dev->dev, dev->dev.devt,
1738                               NULL, "usbdev%d.%d", dev->bus->busnum,
1739                               dev->devnum);
1740         if (IS_ERR(cldev))
1741                 return PTR_ERR(cldev);
1742         dev->usb_classdev = cldev;
1743         return 0;
1744 }
1745
1746 static void usb_classdev_remove(struct usb_device *dev)
1747 {
1748         if (dev->usb_classdev)
1749                 device_unregister(dev->usb_classdev);
1750 }
1751
1752 #else
1753 #define usb_classdev_add(dev)           0
1754 #define usb_classdev_remove(dev)        do {} while (0)
1755
1756 #endif
1757
1758 static int usbdev_notify(struct notifier_block *self,
1759                                unsigned long action, void *dev)
1760 {
1761         switch (action) {
1762         case USB_DEVICE_ADD:
1763                 if (usb_classdev_add(dev))
1764                         return NOTIFY_BAD;
1765                 break;
1766         case USB_DEVICE_REMOVE:
1767                 usb_classdev_remove(dev);
1768                 usbdev_remove(dev);
1769                 break;
1770         }
1771         return NOTIFY_OK;
1772 }
1773
1774 static struct notifier_block usbdev_nb = {
1775         .notifier_call =        usbdev_notify,
1776 };
1777
1778 static struct cdev usb_device_cdev;
1779
1780 int __init usb_devio_init(void)
1781 {
1782         int retval;
1783
1784         retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
1785                                         "usb_device");
1786         if (retval) {
1787                 printk(KERN_ERR "Unable to register minors for usb_device\n");
1788                 goto out;
1789         }
1790         cdev_init(&usb_device_cdev, &usbdev_file_operations);
1791         retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
1792         if (retval) {
1793                 printk(KERN_ERR "Unable to get usb_device major %d\n",
1794                        USB_DEVICE_MAJOR);
1795                 goto error_cdev;
1796         }
1797 #ifdef CONFIG_USB_DEVICE_CLASS
1798         usb_classdev_class = class_create(THIS_MODULE, "usb_device");
1799         if (IS_ERR(usb_classdev_class)) {
1800                 printk(KERN_ERR "Unable to register usb_device class\n");
1801                 retval = PTR_ERR(usb_classdev_class);
1802                 cdev_del(&usb_device_cdev);
1803                 usb_classdev_class = NULL;
1804                 goto out;
1805         }
1806         /* devices of this class shadow the major:minor of their parent
1807          * device, so clear ->dev_kobj to prevent adding duplicate entries
1808          * to /sys/dev
1809          */
1810         usb_classdev_class->dev_kobj = NULL;
1811 #endif
1812         usb_register_notify(&usbdev_nb);
1813 out:
1814         return retval;
1815
1816 error_cdev:
1817         unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
1818         goto out;
1819 }
1820
1821 void usb_devio_cleanup(void)
1822 {
1823         usb_unregister_notify(&usbdev_nb);
1824 #ifdef CONFIG_USB_DEVICE_CLASS
1825         class_destroy(usb_classdev_class);
1826 #endif
1827         cdev_del(&usb_device_cdev);
1828         unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
1829 }