]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/usb/core/devio.c
usbfs: Always allow ctrl requests with USB_RECIP_ENDPOINT on the ctrl ep
[karo-tx-linux.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  *  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/signal.h>
41 #include <linux/poll.h>
42 #include <linux/module.h>
43 #include <linux/usb.h>
44 #include <linux/usbdevice_fs.h>
45 #include <linux/usb/hcd.h>      /* for usbcore internals */
46 #include <linux/cdev.h>
47 #include <linux/notifier.h>
48 #include <linux/security.h>
49 #include <linux/user_namespace.h>
50 #include <linux/scatterlist.h>
51 #include <asm/uaccess.h>
52 #include <asm/byteorder.h>
53 #include <linux/moduleparam.h>
54
55 #include "usb.h"
56
57 #define USB_MAXBUS                      64
58 #define USB_DEVICE_MAX                  USB_MAXBUS * 128
59 #define USB_SG_SIZE                     16384 /* split-size for large txs */
60
61 /* Mutual exclusion for removal, open, and release */
62 DEFINE_MUTEX(usbfs_mutex);
63
64 struct dev_state {
65         struct list_head list;      /* state list */
66         struct usb_device *dev;
67         struct file *file;
68         spinlock_t lock;            /* protects the async urb lists */
69         struct list_head async_pending;
70         struct list_head async_completed;
71         wait_queue_head_t wait;     /* wake up if a request completed */
72         unsigned int discsignr;
73         struct pid *disc_pid;
74         const struct cred *cred;
75         void __user *disccontext;
76         unsigned long ifclaimed;
77         u32 secid;
78         u32 disabled_bulk_eps;
79 };
80
81 struct async {
82         struct list_head asynclist;
83         struct dev_state *ps;
84         struct pid *pid;
85         const struct cred *cred;
86         unsigned int signr;
87         unsigned int ifnum;
88         void __user *userbuffer;
89         void __user *userurb;
90         struct urb *urb;
91         unsigned int mem_usage;
92         int status;
93         u32 secid;
94         u8 bulk_addr;
95         u8 bulk_status;
96 };
97
98 static bool usbfs_snoop;
99 module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
100 MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
101
102 #define snoop(dev, format, arg...)                              \
103         do {                                                    \
104                 if (usbfs_snoop)                                \
105                         dev_info(dev , format , ## arg);        \
106         } while (0)
107
108 enum snoop_when {
109         SUBMIT, COMPLETE
110 };
111
112 #define USB_DEVICE_DEV          MKDEV(USB_DEVICE_MAJOR, 0)
113
114 /* Limit on the total amount of memory we can allocate for transfers */
115 static unsigned usbfs_memory_mb = 16;
116 module_param(usbfs_memory_mb, uint, 0644);
117 MODULE_PARM_DESC(usbfs_memory_mb,
118                 "maximum MB allowed for usbfs buffers (0 = no limit)");
119
120 /* Hard limit, necessary to avoid aithmetic overflow */
121 #define USBFS_XFER_MAX          (UINT_MAX / 2 - 1000000)
122
123 static atomic_t usbfs_memory_usage;     /* Total memory currently allocated */
124
125 /* Check whether it's okay to allocate more memory for a transfer */
126 static int usbfs_increase_memory_usage(unsigned amount)
127 {
128         unsigned lim;
129
130         /*
131          * Convert usbfs_memory_mb to bytes, avoiding overflows.
132          * 0 means use the hard limit (effectively unlimited).
133          */
134         lim = ACCESS_ONCE(usbfs_memory_mb);
135         if (lim == 0 || lim > (USBFS_XFER_MAX >> 20))
136                 lim = USBFS_XFER_MAX;
137         else
138                 lim <<= 20;
139
140         atomic_add(amount, &usbfs_memory_usage);
141         if (atomic_read(&usbfs_memory_usage) <= lim)
142                 return 0;
143         atomic_sub(amount, &usbfs_memory_usage);
144         return -ENOMEM;
145 }
146
147 /* Memory for a transfer is being deallocated */
148 static void usbfs_decrease_memory_usage(unsigned amount)
149 {
150         atomic_sub(amount, &usbfs_memory_usage);
151 }
152
153 static int connected(struct dev_state *ps)
154 {
155         return (!list_empty(&ps->list) &&
156                         ps->dev->state != USB_STATE_NOTATTACHED);
157 }
158
159 static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
160 {
161         loff_t ret;
162
163         mutex_lock(&file->f_dentry->d_inode->i_mutex);
164
165         switch (orig) {
166         case 0:
167                 file->f_pos = offset;
168                 ret = file->f_pos;
169                 break;
170         case 1:
171                 file->f_pos += offset;
172                 ret = file->f_pos;
173                 break;
174         case 2:
175         default:
176                 ret = -EINVAL;
177         }
178
179         mutex_unlock(&file->f_dentry->d_inode->i_mutex);
180         return ret;
181 }
182
183 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
184                            loff_t *ppos)
185 {
186         struct dev_state *ps = file->private_data;
187         struct usb_device *dev = ps->dev;
188         ssize_t ret = 0;
189         unsigned len;
190         loff_t pos;
191         int i;
192
193         pos = *ppos;
194         usb_lock_device(dev);
195         if (!connected(ps)) {
196                 ret = -ENODEV;
197                 goto err;
198         } else if (pos < 0) {
199                 ret = -EINVAL;
200                 goto err;
201         }
202
203         if (pos < sizeof(struct usb_device_descriptor)) {
204                 /* 18 bytes - fits on the stack */
205                 struct usb_device_descriptor temp_desc;
206
207                 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
208                 le16_to_cpus(&temp_desc.bcdUSB);
209                 le16_to_cpus(&temp_desc.idVendor);
210                 le16_to_cpus(&temp_desc.idProduct);
211                 le16_to_cpus(&temp_desc.bcdDevice);
212
213                 len = sizeof(struct usb_device_descriptor) - pos;
214                 if (len > nbytes)
215                         len = nbytes;
216                 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
217                         ret = -EFAULT;
218                         goto err;
219                 }
220
221                 *ppos += len;
222                 buf += len;
223                 nbytes -= len;
224                 ret += len;
225         }
226
227         pos = sizeof(struct usb_device_descriptor);
228         for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
229                 struct usb_config_descriptor *config =
230                         (struct usb_config_descriptor *)dev->rawdescriptors[i];
231                 unsigned int length = le16_to_cpu(config->wTotalLength);
232
233                 if (*ppos < pos + length) {
234
235                         /* The descriptor may claim to be longer than it
236                          * really is.  Here is the actual allocated length. */
237                         unsigned alloclen =
238                                 le16_to_cpu(dev->config[i].desc.wTotalLength);
239
240                         len = length - (*ppos - pos);
241                         if (len > nbytes)
242                                 len = nbytes;
243
244                         /* Simply don't write (skip over) unallocated parts */
245                         if (alloclen > (*ppos - pos)) {
246                                 alloclen -= (*ppos - pos);
247                                 if (copy_to_user(buf,
248                                     dev->rawdescriptors[i] + (*ppos - pos),
249                                     min(len, alloclen))) {
250                                         ret = -EFAULT;
251                                         goto err;
252                                 }
253                         }
254
255                         *ppos += len;
256                         buf += len;
257                         nbytes -= len;
258                         ret += len;
259                 }
260
261                 pos += length;
262         }
263
264 err:
265         usb_unlock_device(dev);
266         return ret;
267 }
268
269 /*
270  * async list handling
271  */
272
273 static struct async *alloc_async(unsigned int numisoframes)
274 {
275         struct async *as;
276
277         as = kzalloc(sizeof(struct async), GFP_KERNEL);
278         if (!as)
279                 return NULL;
280         as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
281         if (!as->urb) {
282                 kfree(as);
283                 return NULL;
284         }
285         return as;
286 }
287
288 static void free_async(struct async *as)
289 {
290         int i;
291
292         put_pid(as->pid);
293         if (as->cred)
294                 put_cred(as->cred);
295         for (i = 0; i < as->urb->num_sgs; i++) {
296                 if (sg_page(&as->urb->sg[i]))
297                         kfree(sg_virt(&as->urb->sg[i]));
298         }
299         kfree(as->urb->sg);
300         kfree(as->urb->transfer_buffer);
301         kfree(as->urb->setup_packet);
302         usb_free_urb(as->urb);
303         usbfs_decrease_memory_usage(as->mem_usage);
304         kfree(as);
305 }
306
307 static void async_newpending(struct async *as)
308 {
309         struct dev_state *ps = as->ps;
310         unsigned long flags;
311
312         spin_lock_irqsave(&ps->lock, flags);
313         list_add_tail(&as->asynclist, &ps->async_pending);
314         spin_unlock_irqrestore(&ps->lock, flags);
315 }
316
317 static void async_removepending(struct async *as)
318 {
319         struct dev_state *ps = as->ps;
320         unsigned long flags;
321
322         spin_lock_irqsave(&ps->lock, flags);
323         list_del_init(&as->asynclist);
324         spin_unlock_irqrestore(&ps->lock, flags);
325 }
326
327 static struct async *async_getcompleted(struct dev_state *ps)
328 {
329         unsigned long flags;
330         struct async *as = NULL;
331
332         spin_lock_irqsave(&ps->lock, flags);
333         if (!list_empty(&ps->async_completed)) {
334                 as = list_entry(ps->async_completed.next, struct async,
335                                 asynclist);
336                 list_del_init(&as->asynclist);
337         }
338         spin_unlock_irqrestore(&ps->lock, flags);
339         return as;
340 }
341
342 static struct async *async_getpending(struct dev_state *ps,
343                                              void __user *userurb)
344 {
345         struct async *as;
346
347         list_for_each_entry(as, &ps->async_pending, asynclist)
348                 if (as->userurb == userurb) {
349                         list_del_init(&as->asynclist);
350                         return as;
351                 }
352
353         return NULL;
354 }
355
356 static void snoop_urb(struct usb_device *udev,
357                 void __user *userurb, int pipe, unsigned length,
358                 int timeout_or_status, enum snoop_when when,
359                 unsigned char *data, unsigned data_len)
360 {
361         static const char *types[] = {"isoc", "int", "ctrl", "bulk"};
362         static const char *dirs[] = {"out", "in"};
363         int ep;
364         const char *t, *d;
365
366         if (!usbfs_snoop)
367                 return;
368
369         ep = usb_pipeendpoint(pipe);
370         t = types[usb_pipetype(pipe)];
371         d = dirs[!!usb_pipein(pipe)];
372
373         if (userurb) {          /* Async */
374                 if (when == SUBMIT)
375                         dev_info(&udev->dev, "userurb %p, ep%d %s-%s, "
376                                         "length %u\n",
377                                         userurb, ep, t, d, length);
378                 else
379                         dev_info(&udev->dev, "userurb %p, ep%d %s-%s, "
380                                         "actual_length %u status %d\n",
381                                         userurb, ep, t, d, length,
382                                         timeout_or_status);
383         } else {
384                 if (when == SUBMIT)
385                         dev_info(&udev->dev, "ep%d %s-%s, length %u, "
386                                         "timeout %d\n",
387                                         ep, t, d, length, timeout_or_status);
388                 else
389                         dev_info(&udev->dev, "ep%d %s-%s, actual_length %u, "
390                                         "status %d\n",
391                                         ep, t, d, length, timeout_or_status);
392         }
393
394         if (data && data_len > 0) {
395                 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
396                         data, data_len, 1);
397         }
398 }
399
400 static void snoop_urb_data(struct urb *urb, unsigned len)
401 {
402         int i, size;
403
404         if (!usbfs_snoop)
405                 return;
406
407         if (urb->num_sgs == 0) {
408                 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
409                         urb->transfer_buffer, len, 1);
410                 return;
411         }
412
413         for (i = 0; i < urb->num_sgs && len; i++) {
414                 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
415                 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
416                         sg_virt(&urb->sg[i]), size, 1);
417                 len -= size;
418         }
419 }
420
421 static int copy_urb_data_to_user(u8 __user *userbuffer, struct urb *urb)
422 {
423         unsigned i, len, size;
424
425         if (urb->number_of_packets > 0)         /* Isochronous */
426                 len = urb->transfer_buffer_length;
427         else                                    /* Non-Isoc */
428                 len = urb->actual_length;
429
430         if (urb->num_sgs == 0) {
431                 if (copy_to_user(userbuffer, urb->transfer_buffer, len))
432                         return -EFAULT;
433                 return 0;
434         }
435
436         for (i = 0; i < urb->num_sgs && len; i++) {
437                 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
438                 if (copy_to_user(userbuffer, sg_virt(&urb->sg[i]), size))
439                         return -EFAULT;
440                 userbuffer += size;
441                 len -= size;
442         }
443
444         return 0;
445 }
446
447 #define AS_CONTINUATION 1
448 #define AS_UNLINK       2
449
450 static void cancel_bulk_urbs(struct dev_state *ps, unsigned bulk_addr)
451 __releases(ps->lock)
452 __acquires(ps->lock)
453 {
454         struct urb *urb;
455         struct async *as;
456
457         /* Mark all the pending URBs that match bulk_addr, up to but not
458          * including the first one without AS_CONTINUATION.  If such an
459          * URB is encountered then a new transfer has already started so
460          * the endpoint doesn't need to be disabled; otherwise it does.
461          */
462         list_for_each_entry(as, &ps->async_pending, asynclist) {
463                 if (as->bulk_addr == bulk_addr) {
464                         if (as->bulk_status != AS_CONTINUATION)
465                                 goto rescan;
466                         as->bulk_status = AS_UNLINK;
467                         as->bulk_addr = 0;
468                 }
469         }
470         ps->disabled_bulk_eps |= (1 << bulk_addr);
471
472         /* Now carefully unlink all the marked pending URBs */
473  rescan:
474         list_for_each_entry(as, &ps->async_pending, asynclist) {
475                 if (as->bulk_status == AS_UNLINK) {
476                         as->bulk_status = 0;            /* Only once */
477                         urb = as->urb;
478                         usb_get_urb(urb);
479                         spin_unlock(&ps->lock);         /* Allow completions */
480                         usb_unlink_urb(urb);
481                         usb_put_urb(urb);
482                         spin_lock(&ps->lock);
483                         goto rescan;
484                 }
485         }
486 }
487
488 static void async_completed(struct urb *urb)
489 {
490         struct async *as = urb->context;
491         struct dev_state *ps = as->ps;
492         struct siginfo sinfo;
493         struct pid *pid = NULL;
494         u32 secid = 0;
495         const struct cred *cred = NULL;
496         int signr;
497
498         spin_lock(&ps->lock);
499         list_move_tail(&as->asynclist, &ps->async_completed);
500         as->status = urb->status;
501         signr = as->signr;
502         if (signr) {
503                 sinfo.si_signo = as->signr;
504                 sinfo.si_errno = as->status;
505                 sinfo.si_code = SI_ASYNCIO;
506                 sinfo.si_addr = as->userurb;
507                 pid = get_pid(as->pid);
508                 cred = get_cred(as->cred);
509                 secid = as->secid;
510         }
511         snoop(&urb->dev->dev, "urb complete\n");
512         snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length,
513                         as->status, COMPLETE, NULL, 0);
514         if ((urb->transfer_flags & URB_DIR_MASK) == USB_DIR_IN)
515                 snoop_urb_data(urb, urb->actual_length);
516
517         if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET &&
518                         as->status != -ENOENT)
519                 cancel_bulk_urbs(ps, as->bulk_addr);
520         spin_unlock(&ps->lock);
521
522         if (signr) {
523                 kill_pid_info_as_cred(sinfo.si_signo, &sinfo, pid, cred, secid);
524                 put_pid(pid);
525                 put_cred(cred);
526         }
527
528         wake_up(&ps->wait);
529 }
530
531 static void destroy_async(struct dev_state *ps, struct list_head *list)
532 {
533         struct urb *urb;
534         struct async *as;
535         unsigned long flags;
536
537         spin_lock_irqsave(&ps->lock, flags);
538         while (!list_empty(list)) {
539                 as = list_entry(list->next, struct async, asynclist);
540                 list_del_init(&as->asynclist);
541                 urb = as->urb;
542                 usb_get_urb(urb);
543
544                 /* drop the spinlock so the completion handler can run */
545                 spin_unlock_irqrestore(&ps->lock, flags);
546                 usb_kill_urb(urb);
547                 usb_put_urb(urb);
548                 spin_lock_irqsave(&ps->lock, flags);
549         }
550         spin_unlock_irqrestore(&ps->lock, flags);
551 }
552
553 static void destroy_async_on_interface(struct dev_state *ps,
554                                        unsigned int ifnum)
555 {
556         struct list_head *p, *q, hitlist;
557         unsigned long flags;
558
559         INIT_LIST_HEAD(&hitlist);
560         spin_lock_irqsave(&ps->lock, flags);
561         list_for_each_safe(p, q, &ps->async_pending)
562                 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
563                         list_move_tail(p, &hitlist);
564         spin_unlock_irqrestore(&ps->lock, flags);
565         destroy_async(ps, &hitlist);
566 }
567
568 static void destroy_all_async(struct dev_state *ps)
569 {
570         destroy_async(ps, &ps->async_pending);
571 }
572
573 /*
574  * interface claims are made only at the request of user level code,
575  * which can also release them (explicitly or by closing files).
576  * they're also undone when devices disconnect.
577  */
578
579 static int driver_probe(struct usb_interface *intf,
580                         const struct usb_device_id *id)
581 {
582         return -ENODEV;
583 }
584
585 static void driver_disconnect(struct usb_interface *intf)
586 {
587         struct dev_state *ps = usb_get_intfdata(intf);
588         unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
589
590         if (!ps)
591                 return;
592
593         /* NOTE:  this relies on usbcore having canceled and completed
594          * all pending I/O requests; 2.6 does that.
595          */
596
597         if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
598                 clear_bit(ifnum, &ps->ifclaimed);
599         else
600                 dev_warn(&intf->dev, "interface number %u out of range\n",
601                          ifnum);
602
603         usb_set_intfdata(intf, NULL);
604
605         /* force async requests to complete */
606         destroy_async_on_interface(ps, ifnum);
607 }
608
609 /* The following routines are merely placeholders.  There is no way
610  * to inform a user task about suspend or resumes.
611  */
612 static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
613 {
614         return 0;
615 }
616
617 static int driver_resume(struct usb_interface *intf)
618 {
619         return 0;
620 }
621
622 struct usb_driver usbfs_driver = {
623         .name =         "usbfs",
624         .probe =        driver_probe,
625         .disconnect =   driver_disconnect,
626         .suspend =      driver_suspend,
627         .resume =       driver_resume,
628 };
629
630 static int claimintf(struct dev_state *ps, unsigned int ifnum)
631 {
632         struct usb_device *dev = ps->dev;
633         struct usb_interface *intf;
634         int err;
635
636         if (ifnum >= 8*sizeof(ps->ifclaimed))
637                 return -EINVAL;
638         /* already claimed */
639         if (test_bit(ifnum, &ps->ifclaimed))
640                 return 0;
641
642         intf = usb_ifnum_to_if(dev, ifnum);
643         if (!intf)
644                 err = -ENOENT;
645         else
646                 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
647         if (err == 0)
648                 set_bit(ifnum, &ps->ifclaimed);
649         return err;
650 }
651
652 static int releaseintf(struct dev_state *ps, unsigned int ifnum)
653 {
654         struct usb_device *dev;
655         struct usb_interface *intf;
656         int err;
657
658         err = -EINVAL;
659         if (ifnum >= 8*sizeof(ps->ifclaimed))
660                 return err;
661         dev = ps->dev;
662         intf = usb_ifnum_to_if(dev, ifnum);
663         if (!intf)
664                 err = -ENOENT;
665         else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
666                 usb_driver_release_interface(&usbfs_driver, intf);
667                 err = 0;
668         }
669         return err;
670 }
671
672 static int checkintf(struct dev_state *ps, unsigned int ifnum)
673 {
674         if (ps->dev->state != USB_STATE_CONFIGURED)
675                 return -EHOSTUNREACH;
676         if (ifnum >= 8*sizeof(ps->ifclaimed))
677                 return -EINVAL;
678         if (test_bit(ifnum, &ps->ifclaimed))
679                 return 0;
680         /* if not yet claimed, claim it for the driver */
681         dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
682                  "interface %u before use\n", task_pid_nr(current),
683                  current->comm, ifnum);
684         return claimintf(ps, ifnum);
685 }
686
687 static int findintfep(struct usb_device *dev, unsigned int ep)
688 {
689         unsigned int i, j, e;
690         struct usb_interface *intf;
691         struct usb_host_interface *alts;
692         struct usb_endpoint_descriptor *endpt;
693
694         if (ep & ~(USB_DIR_IN|0xf))
695                 return -EINVAL;
696         if (!dev->actconfig)
697                 return -ESRCH;
698         for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
699                 intf = dev->actconfig->interface[i];
700                 for (j = 0; j < intf->num_altsetting; j++) {
701                         alts = &intf->altsetting[j];
702                         for (e = 0; e < alts->desc.bNumEndpoints; e++) {
703                                 endpt = &alts->endpoint[e].desc;
704                                 if (endpt->bEndpointAddress == ep)
705                                         return alts->desc.bInterfaceNumber;
706                         }
707                 }
708         }
709         return -ENOENT;
710 }
711
712 static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
713                            unsigned int request, unsigned int index)
714 {
715         int ret = 0;
716         struct usb_host_interface *alt_setting;
717
718         if (ps->dev->state != USB_STATE_UNAUTHENTICATED
719          && ps->dev->state != USB_STATE_ADDRESS
720          && ps->dev->state != USB_STATE_CONFIGURED)
721                 return -EHOSTUNREACH;
722         if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
723                 return 0;
724
725         /*
726          * check for the special corner case 'get_device_id' in the printer
727          * class specification, where wIndex is (interface << 8 | altsetting)
728          * instead of just interface
729          */
730         if (requesttype == 0xa1 && request == 0) {
731                 alt_setting = usb_find_alt_setting(ps->dev->actconfig,
732                                                    index >> 8, index & 0xff);
733                 if (alt_setting
734                  && alt_setting->desc.bInterfaceClass == USB_CLASS_PRINTER)
735                         index >>= 8;
736         }
737
738         index &= 0xff;
739         switch (requesttype & USB_RECIP_MASK) {
740         case USB_RECIP_ENDPOINT:
741                 if ((index & ~USB_DIR_IN) == 0)
742                         return 0;
743                 ret = findintfep(ps->dev, index);
744                 if (ret >= 0)
745                         ret = checkintf(ps, ret);
746                 break;
747
748         case USB_RECIP_INTERFACE:
749                 ret = checkintf(ps, index);
750                 break;
751         }
752         return ret;
753 }
754
755 static int match_devt(struct device *dev, void *data)
756 {
757         return dev->devt == (dev_t) (unsigned long) data;
758 }
759
760 static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
761 {
762         struct device *dev;
763
764         dev = bus_find_device(&usb_bus_type, NULL,
765                               (void *) (unsigned long) devt, match_devt);
766         if (!dev)
767                 return NULL;
768         return container_of(dev, struct usb_device, dev);
769 }
770
771 /*
772  * file operations
773  */
774 static int usbdev_open(struct inode *inode, struct file *file)
775 {
776         struct usb_device *dev = NULL;
777         struct dev_state *ps;
778         int ret;
779
780         ret = -ENOMEM;
781         ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
782         if (!ps)
783                 goto out_free_ps;
784
785         ret = -ENODEV;
786
787         /* Protect against simultaneous removal or release */
788         mutex_lock(&usbfs_mutex);
789
790         /* usbdev device-node */
791         if (imajor(inode) == USB_DEVICE_MAJOR)
792                 dev = usbdev_lookup_by_devt(inode->i_rdev);
793
794         mutex_unlock(&usbfs_mutex);
795
796         if (!dev)
797                 goto out_free_ps;
798
799         usb_lock_device(dev);
800         if (dev->state == USB_STATE_NOTATTACHED)
801                 goto out_unlock_device;
802
803         ret = usb_autoresume_device(dev);
804         if (ret)
805                 goto out_unlock_device;
806
807         ps->dev = dev;
808         ps->file = file;
809         spin_lock_init(&ps->lock);
810         INIT_LIST_HEAD(&ps->list);
811         INIT_LIST_HEAD(&ps->async_pending);
812         INIT_LIST_HEAD(&ps->async_completed);
813         init_waitqueue_head(&ps->wait);
814         ps->discsignr = 0;
815         ps->disc_pid = get_pid(task_pid(current));
816         ps->cred = get_current_cred();
817         ps->disccontext = NULL;
818         ps->ifclaimed = 0;
819         security_task_getsecid(current, &ps->secid);
820         smp_wmb();
821         list_add_tail(&ps->list, &dev->filelist);
822         file->private_data = ps;
823         usb_unlock_device(dev);
824         snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
825                         current->comm);
826         return ret;
827
828  out_unlock_device:
829         usb_unlock_device(dev);
830         usb_put_dev(dev);
831  out_free_ps:
832         kfree(ps);
833         return ret;
834 }
835
836 static int usbdev_release(struct inode *inode, struct file *file)
837 {
838         struct dev_state *ps = file->private_data;
839         struct usb_device *dev = ps->dev;
840         unsigned int ifnum;
841         struct async *as;
842
843         usb_lock_device(dev);
844         usb_hub_release_all_ports(dev, ps);
845
846         list_del_init(&ps->list);
847
848         for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
849                         ifnum++) {
850                 if (test_bit(ifnum, &ps->ifclaimed))
851                         releaseintf(ps, ifnum);
852         }
853         destroy_all_async(ps);
854         usb_autosuspend_device(dev);
855         usb_unlock_device(dev);
856         usb_put_dev(dev);
857         put_pid(ps->disc_pid);
858         put_cred(ps->cred);
859
860         as = async_getcompleted(ps);
861         while (as) {
862                 free_async(as);
863                 as = async_getcompleted(ps);
864         }
865         kfree(ps);
866         return 0;
867 }
868
869 static int proc_control(struct dev_state *ps, void __user *arg)
870 {
871         struct usb_device *dev = ps->dev;
872         struct usbdevfs_ctrltransfer ctrl;
873         unsigned int tmo;
874         unsigned char *tbuf;
875         unsigned wLength;
876         int i, pipe, ret;
877
878         if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
879                 return -EFAULT;
880         ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.bRequest,
881                               ctrl.wIndex);
882         if (ret)
883                 return ret;
884         wLength = ctrl.wLength;         /* To suppress 64k PAGE_SIZE warning */
885         if (wLength > PAGE_SIZE)
886                 return -EINVAL;
887         ret = usbfs_increase_memory_usage(PAGE_SIZE + sizeof(struct urb) +
888                         sizeof(struct usb_ctrlrequest));
889         if (ret)
890                 return ret;
891         tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
892         if (!tbuf) {
893                 ret = -ENOMEM;
894                 goto done;
895         }
896         tmo = ctrl.timeout;
897         snoop(&dev->dev, "control urb: bRequestType=%02x "
898                 "bRequest=%02x wValue=%04x "
899                 "wIndex=%04x wLength=%04x\n",
900                 ctrl.bRequestType, ctrl.bRequest,
901                 __le16_to_cpup(&ctrl.wValue),
902                 __le16_to_cpup(&ctrl.wIndex),
903                 __le16_to_cpup(&ctrl.wLength));
904         if (ctrl.bRequestType & 0x80) {
905                 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
906                                                ctrl.wLength)) {
907                         ret = -EINVAL;
908                         goto done;
909                 }
910                 pipe = usb_rcvctrlpipe(dev, 0);
911                 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT, NULL, 0);
912
913                 usb_unlock_device(dev);
914                 i = usb_control_msg(dev, pipe, ctrl.bRequest,
915                                     ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
916                                     tbuf, ctrl.wLength, tmo);
917                 usb_lock_device(dev);
918                 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE,
919                           tbuf, max(i, 0));
920                 if ((i > 0) && ctrl.wLength) {
921                         if (copy_to_user(ctrl.data, tbuf, i)) {
922                                 ret = -EFAULT;
923                                 goto done;
924                         }
925                 }
926         } else {
927                 if (ctrl.wLength) {
928                         if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
929                                 ret = -EFAULT;
930                                 goto done;
931                         }
932                 }
933                 pipe = usb_sndctrlpipe(dev, 0);
934                 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT,
935                         tbuf, ctrl.wLength);
936
937                 usb_unlock_device(dev);
938                 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
939                                     ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
940                                     tbuf, ctrl.wLength, tmo);
941                 usb_lock_device(dev);
942                 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE, NULL, 0);
943         }
944         if (i < 0 && i != -EPIPE) {
945                 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
946                            "failed cmd %s rqt %u rq %u len %u ret %d\n",
947                            current->comm, ctrl.bRequestType, ctrl.bRequest,
948                            ctrl.wLength, i);
949         }
950         ret = i;
951  done:
952         free_page((unsigned long) tbuf);
953         usbfs_decrease_memory_usage(PAGE_SIZE + sizeof(struct urb) +
954                         sizeof(struct usb_ctrlrequest));
955         return ret;
956 }
957
958 static int proc_bulk(struct dev_state *ps, void __user *arg)
959 {
960         struct usb_device *dev = ps->dev;
961         struct usbdevfs_bulktransfer bulk;
962         unsigned int tmo, len1, pipe;
963         int len2;
964         unsigned char *tbuf;
965         int i, ret;
966
967         if (copy_from_user(&bulk, arg, sizeof(bulk)))
968                 return -EFAULT;
969         ret = findintfep(ps->dev, bulk.ep);
970         if (ret < 0)
971                 return ret;
972         ret = checkintf(ps, ret);
973         if (ret)
974                 return ret;
975         if (bulk.ep & USB_DIR_IN)
976                 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
977         else
978                 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
979         if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
980                 return -EINVAL;
981         len1 = bulk.len;
982         if (len1 >= USBFS_XFER_MAX)
983                 return -EINVAL;
984         ret = usbfs_increase_memory_usage(len1 + sizeof(struct urb));
985         if (ret)
986                 return ret;
987         if (!(tbuf = kmalloc(len1, GFP_KERNEL))) {
988                 ret = -ENOMEM;
989                 goto done;
990         }
991         tmo = bulk.timeout;
992         if (bulk.ep & 0x80) {
993                 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
994                         ret = -EINVAL;
995                         goto done;
996                 }
997                 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, NULL, 0);
998
999                 usb_unlock_device(dev);
1000                 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
1001                 usb_lock_device(dev);
1002                 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, tbuf, len2);
1003
1004                 if (!i && len2) {
1005                         if (copy_to_user(bulk.data, tbuf, len2)) {
1006                                 ret = -EFAULT;
1007                                 goto done;
1008                         }
1009                 }
1010         } else {
1011                 if (len1) {
1012                         if (copy_from_user(tbuf, bulk.data, len1)) {
1013                                 ret = -EFAULT;
1014                                 goto done;
1015                         }
1016                 }
1017                 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, tbuf, len1);
1018
1019                 usb_unlock_device(dev);
1020                 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
1021                 usb_lock_device(dev);
1022                 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, NULL, 0);
1023         }
1024         ret = (i < 0 ? i : len2);
1025  done:
1026         kfree(tbuf);
1027         usbfs_decrease_memory_usage(len1 + sizeof(struct urb));
1028         return ret;
1029 }
1030
1031 static int proc_resetep(struct dev_state *ps, void __user *arg)
1032 {
1033         unsigned int ep;
1034         int ret;
1035
1036         if (get_user(ep, (unsigned int __user *)arg))
1037                 return -EFAULT;
1038         ret = findintfep(ps->dev, ep);
1039         if (ret < 0)
1040                 return ret;
1041         ret = checkintf(ps, ret);
1042         if (ret)
1043                 return ret;
1044         usb_reset_endpoint(ps->dev, ep);
1045         return 0;
1046 }
1047
1048 static int proc_clearhalt(struct dev_state *ps, void __user *arg)
1049 {
1050         unsigned int ep;
1051         int pipe;
1052         int ret;
1053
1054         if (get_user(ep, (unsigned int __user *)arg))
1055                 return -EFAULT;
1056         ret = findintfep(ps->dev, ep);
1057         if (ret < 0)
1058                 return ret;
1059         ret = checkintf(ps, ret);
1060         if (ret)
1061                 return ret;
1062         if (ep & USB_DIR_IN)
1063                 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
1064         else
1065                 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
1066
1067         return usb_clear_halt(ps->dev, pipe);
1068 }
1069
1070 static int proc_getdriver(struct dev_state *ps, void __user *arg)
1071 {
1072         struct usbdevfs_getdriver gd;
1073         struct usb_interface *intf;
1074         int ret;
1075
1076         if (copy_from_user(&gd, arg, sizeof(gd)))
1077                 return -EFAULT;
1078         intf = usb_ifnum_to_if(ps->dev, gd.interface);
1079         if (!intf || !intf->dev.driver)
1080                 ret = -ENODATA;
1081         else {
1082                 strncpy(gd.driver, intf->dev.driver->name,
1083                                 sizeof(gd.driver));
1084                 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
1085         }
1086         return ret;
1087 }
1088
1089 static int proc_connectinfo(struct dev_state *ps, void __user *arg)
1090 {
1091         struct usbdevfs_connectinfo ci = {
1092                 .devnum = ps->dev->devnum,
1093                 .slow = ps->dev->speed == USB_SPEED_LOW
1094         };
1095
1096         if (copy_to_user(arg, &ci, sizeof(ci)))
1097                 return -EFAULT;
1098         return 0;
1099 }
1100
1101 static int proc_resetdevice(struct dev_state *ps)
1102 {
1103         return usb_reset_device(ps->dev);
1104 }
1105
1106 static int proc_setintf(struct dev_state *ps, void __user *arg)
1107 {
1108         struct usbdevfs_setinterface setintf;
1109         int ret;
1110
1111         if (copy_from_user(&setintf, arg, sizeof(setintf)))
1112                 return -EFAULT;
1113         if ((ret = checkintf(ps, setintf.interface)))
1114                 return ret;
1115         return usb_set_interface(ps->dev, setintf.interface,
1116                         setintf.altsetting);
1117 }
1118
1119 static int proc_setconfig(struct dev_state *ps, void __user *arg)
1120 {
1121         int u;
1122         int status = 0;
1123         struct usb_host_config *actconfig;
1124
1125         if (get_user(u, (int __user *)arg))
1126                 return -EFAULT;
1127
1128         actconfig = ps->dev->actconfig;
1129
1130         /* Don't touch the device if any interfaces are claimed.
1131          * It could interfere with other drivers' operations, and if
1132          * an interface is claimed by usbfs it could easily deadlock.
1133          */
1134         if (actconfig) {
1135                 int i;
1136
1137                 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
1138                         if (usb_interface_claimed(actconfig->interface[i])) {
1139                                 dev_warn(&ps->dev->dev,
1140                                         "usbfs: interface %d claimed by %s "
1141                                         "while '%s' sets config #%d\n",
1142                                         actconfig->interface[i]
1143                                                 ->cur_altsetting
1144                                                 ->desc.bInterfaceNumber,
1145                                         actconfig->interface[i]
1146                                                 ->dev.driver->name,
1147                                         current->comm, u);
1148                                 status = -EBUSY;
1149                                 break;
1150                         }
1151                 }
1152         }
1153
1154         /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
1155          * so avoid usb_set_configuration()'s kick to sysfs
1156          */
1157         if (status == 0) {
1158                 if (actconfig && actconfig->desc.bConfigurationValue == u)
1159                         status = usb_reset_configuration(ps->dev);
1160                 else
1161                         status = usb_set_configuration(ps->dev, u);
1162         }
1163
1164         return status;
1165 }
1166
1167 static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1168                         struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
1169                         void __user *arg)
1170 {
1171         struct usbdevfs_iso_packet_desc *isopkt = NULL;
1172         struct usb_host_endpoint *ep;
1173         struct async *as = NULL;
1174         struct usb_ctrlrequest *dr = NULL;
1175         unsigned int u, totlen, isofrmlen;
1176         int i, ret, is_in, num_sgs = 0, ifnum = -1;
1177         void *buf;
1178
1179         if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
1180                                 USBDEVFS_URB_SHORT_NOT_OK |
1181                                 USBDEVFS_URB_BULK_CONTINUATION |
1182                                 USBDEVFS_URB_NO_FSBR |
1183                                 USBDEVFS_URB_ZERO_PACKET |
1184                                 USBDEVFS_URB_NO_INTERRUPT))
1185                 return -EINVAL;
1186         if (uurb->buffer_length > 0 && !uurb->buffer)
1187                 return -EINVAL;
1188         if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
1189             (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
1190                 ifnum = findintfep(ps->dev, uurb->endpoint);
1191                 if (ifnum < 0)
1192                         return ifnum;
1193                 ret = checkintf(ps, ifnum);
1194                 if (ret)
1195                         return ret;
1196         }
1197         if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
1198                 is_in = 1;
1199                 ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
1200         } else {
1201                 is_in = 0;
1202                 ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
1203         }
1204         if (!ep)
1205                 return -ENOENT;
1206
1207         u = 0;
1208         switch(uurb->type) {
1209         case USBDEVFS_URB_TYPE_CONTROL:
1210                 if (!usb_endpoint_xfer_control(&ep->desc))
1211                         return -EINVAL;
1212                 /* min 8 byte setup packet */
1213                 if (uurb->buffer_length < 8)
1214                         return -EINVAL;
1215                 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1216                 if (!dr)
1217                         return -ENOMEM;
1218                 if (copy_from_user(dr, uurb->buffer, 8)) {
1219                         ret = -EFAULT;
1220                         goto error;
1221                 }
1222                 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
1223                         ret = -EINVAL;
1224                         goto error;
1225                 }
1226                 ret = check_ctrlrecip(ps, dr->bRequestType, dr->bRequest,
1227                                       le16_to_cpup(&dr->wIndex));
1228                 if (ret)
1229                         goto error;
1230                 uurb->number_of_packets = 0;
1231                 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1232                 uurb->buffer += 8;
1233                 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1234                         is_in = 1;
1235                         uurb->endpoint |= USB_DIR_IN;
1236                 } else {
1237                         is_in = 0;
1238                         uurb->endpoint &= ~USB_DIR_IN;
1239                 }
1240                 snoop(&ps->dev->dev, "control urb: bRequestType=%02x "
1241                         "bRequest=%02x wValue=%04x "
1242                         "wIndex=%04x wLength=%04x\n",
1243                         dr->bRequestType, dr->bRequest,
1244                         __le16_to_cpup(&dr->wValue),
1245                         __le16_to_cpup(&dr->wIndex),
1246                         __le16_to_cpup(&dr->wLength));
1247                 u = sizeof(struct usb_ctrlrequest);
1248                 break;
1249
1250         case USBDEVFS_URB_TYPE_BULK:
1251                 switch (usb_endpoint_type(&ep->desc)) {
1252                 case USB_ENDPOINT_XFER_CONTROL:
1253                 case USB_ENDPOINT_XFER_ISOC:
1254                         return -EINVAL;
1255                 case USB_ENDPOINT_XFER_INT:
1256                         /* allow single-shot interrupt transfers */
1257                         uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
1258                         goto interrupt_urb;
1259                 }
1260                 uurb->number_of_packets = 0;
1261                 num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE);
1262                 if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize)
1263                         num_sgs = 0;
1264                 break;
1265
1266         case USBDEVFS_URB_TYPE_INTERRUPT:
1267                 if (!usb_endpoint_xfer_int(&ep->desc))
1268                         return -EINVAL;
1269  interrupt_urb:
1270                 uurb->number_of_packets = 0;
1271                 break;
1272
1273         case USBDEVFS_URB_TYPE_ISO:
1274                 /* arbitrary limit */
1275                 if (uurb->number_of_packets < 1 ||
1276                     uurb->number_of_packets > 128)
1277                         return -EINVAL;
1278                 if (!usb_endpoint_xfer_isoc(&ep->desc))
1279                         return -EINVAL;
1280                 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1281                                    uurb->number_of_packets;
1282                 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
1283                         return -ENOMEM;
1284                 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
1285                         ret = -EFAULT;
1286                         goto error;
1287                 }
1288                 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1289                         /* arbitrary limit,
1290                          * sufficient for USB 2.0 high-bandwidth iso */
1291                         if (isopkt[u].length > 8192) {
1292                                 ret = -EINVAL;
1293                                 goto error;
1294                         }
1295                         totlen += isopkt[u].length;
1296                 }
1297                 u *= sizeof(struct usb_iso_packet_descriptor);
1298                 uurb->buffer_length = totlen;
1299                 break;
1300
1301         default:
1302                 return -EINVAL;
1303         }
1304
1305         if (uurb->buffer_length >= USBFS_XFER_MAX) {
1306                 ret = -EINVAL;
1307                 goto error;
1308         }
1309         if (uurb->buffer_length > 0 &&
1310                         !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1311                                 uurb->buffer, uurb->buffer_length)) {
1312                 ret = -EFAULT;
1313                 goto error;
1314         }
1315         as = alloc_async(uurb->number_of_packets);
1316         if (!as) {
1317                 ret = -ENOMEM;
1318                 goto error;
1319         }
1320
1321         u += sizeof(struct async) + sizeof(struct urb) + uurb->buffer_length +
1322              num_sgs * sizeof(struct scatterlist);
1323         ret = usbfs_increase_memory_usage(u);
1324         if (ret)
1325                 goto error;
1326         as->mem_usage = u;
1327
1328         if (num_sgs) {
1329                 as->urb->sg = kmalloc(num_sgs * sizeof(struct scatterlist),
1330                                       GFP_KERNEL);
1331                 if (!as->urb->sg) {
1332                         ret = -ENOMEM;
1333                         goto error;
1334                 }
1335                 as->urb->num_sgs = num_sgs;
1336                 sg_init_table(as->urb->sg, as->urb->num_sgs);
1337
1338                 totlen = uurb->buffer_length;
1339                 for (i = 0; i < as->urb->num_sgs; i++) {
1340                         u = (totlen > USB_SG_SIZE) ? USB_SG_SIZE : totlen;
1341                         buf = kmalloc(u, GFP_KERNEL);
1342                         if (!buf) {
1343                                 ret = -ENOMEM;
1344                                 goto error;
1345                         }
1346                         sg_set_buf(&as->urb->sg[i], buf, u);
1347
1348                         if (!is_in) {
1349                                 if (copy_from_user(buf, uurb->buffer, u)) {
1350                                         ret = -EFAULT;
1351                                         goto error;
1352                                 }
1353                                 uurb->buffer += u;
1354                         }
1355                         totlen -= u;
1356                 }
1357         } else if (uurb->buffer_length > 0) {
1358                 as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
1359                                 GFP_KERNEL);
1360                 if (!as->urb->transfer_buffer) {
1361                         ret = -ENOMEM;
1362                         goto error;
1363                 }
1364
1365                 if (!is_in) {
1366                         if (copy_from_user(as->urb->transfer_buffer,
1367                                            uurb->buffer,
1368                                            uurb->buffer_length)) {
1369                                 ret = -EFAULT;
1370                                 goto error;
1371                         }
1372                 } else if (uurb->type == USBDEVFS_URB_TYPE_ISO) {
1373                         /*
1374                          * Isochronous input data may end up being
1375                          * discontiguous if some of the packets are short.
1376                          * Clear the buffer so that the gaps don't leak
1377                          * kernel data to userspace.
1378                          */
1379                         memset(as->urb->transfer_buffer, 0,
1380                                         uurb->buffer_length);
1381                 }
1382         }
1383         as->urb->dev = ps->dev;
1384         as->urb->pipe = (uurb->type << 30) |
1385                         __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1386                         (uurb->endpoint & USB_DIR_IN);
1387
1388         /* This tedious sequence is necessary because the URB_* flags
1389          * are internal to the kernel and subject to change, whereas
1390          * the USBDEVFS_URB_* flags are a user API and must not be changed.
1391          */
1392         u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
1393         if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
1394                 u |= URB_ISO_ASAP;
1395         if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
1396                 u |= URB_SHORT_NOT_OK;
1397         if (uurb->flags & USBDEVFS_URB_NO_FSBR)
1398                 u |= URB_NO_FSBR;
1399         if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1400                 u |= URB_ZERO_PACKET;
1401         if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
1402                 u |= URB_NO_INTERRUPT;
1403         as->urb->transfer_flags = u;
1404
1405         as->urb->transfer_buffer_length = uurb->buffer_length;
1406         as->urb->setup_packet = (unsigned char *)dr;
1407         dr = NULL;
1408         as->urb->start_frame = uurb->start_frame;
1409         as->urb->number_of_packets = uurb->number_of_packets;
1410         if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1411                         ps->dev->speed == USB_SPEED_HIGH)
1412                 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
1413         else
1414                 as->urb->interval = ep->desc.bInterval;
1415         as->urb->context = as;
1416         as->urb->complete = async_completed;
1417         for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1418                 as->urb->iso_frame_desc[u].offset = totlen;
1419                 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1420                 totlen += isopkt[u].length;
1421         }
1422         kfree(isopkt);
1423         isopkt = NULL;
1424         as->ps = ps;
1425         as->userurb = arg;
1426         if (is_in && uurb->buffer_length > 0)
1427                 as->userbuffer = uurb->buffer;
1428         else
1429                 as->userbuffer = NULL;
1430         as->signr = uurb->signr;
1431         as->ifnum = ifnum;
1432         as->pid = get_pid(task_pid(current));
1433         as->cred = get_current_cred();
1434         security_task_getsecid(current, &as->secid);
1435         snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1436                         as->urb->transfer_buffer_length, 0, SUBMIT,
1437                         NULL, 0);
1438         if (!is_in)
1439                 snoop_urb_data(as->urb, as->urb->transfer_buffer_length);
1440
1441         async_newpending(as);
1442
1443         if (usb_endpoint_xfer_bulk(&ep->desc)) {
1444                 spin_lock_irq(&ps->lock);
1445
1446                 /* Not exactly the endpoint address; the direction bit is
1447                  * shifted to the 0x10 position so that the value will be
1448                  * between 0 and 31.
1449                  */
1450                 as->bulk_addr = usb_endpoint_num(&ep->desc) |
1451                         ((ep->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1452                                 >> 3);
1453
1454                 /* If this bulk URB is the start of a new transfer, re-enable
1455                  * the endpoint.  Otherwise mark it as a continuation URB.
1456                  */
1457                 if (uurb->flags & USBDEVFS_URB_BULK_CONTINUATION)
1458                         as->bulk_status = AS_CONTINUATION;
1459                 else
1460                         ps->disabled_bulk_eps &= ~(1 << as->bulk_addr);
1461
1462                 /* Don't accept continuation URBs if the endpoint is
1463                  * disabled because of an earlier error.
1464                  */
1465                 if (ps->disabled_bulk_eps & (1 << as->bulk_addr))
1466                         ret = -EREMOTEIO;
1467                 else
1468                         ret = usb_submit_urb(as->urb, GFP_ATOMIC);
1469                 spin_unlock_irq(&ps->lock);
1470         } else {
1471                 ret = usb_submit_urb(as->urb, GFP_KERNEL);
1472         }
1473
1474         if (ret) {
1475                 dev_printk(KERN_DEBUG, &ps->dev->dev,
1476                            "usbfs: usb_submit_urb returned %d\n", ret);
1477                 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1478                                 0, ret, COMPLETE, NULL, 0);
1479                 async_removepending(as);
1480                 goto error;
1481         }
1482         return 0;
1483
1484  error:
1485         kfree(isopkt);
1486         kfree(dr);
1487         if (as)
1488                 free_async(as);
1489         return ret;
1490 }
1491
1492 static int proc_submiturb(struct dev_state *ps, void __user *arg)
1493 {
1494         struct usbdevfs_urb uurb;
1495
1496         if (copy_from_user(&uurb, arg, sizeof(uurb)))
1497                 return -EFAULT;
1498
1499         return proc_do_submiturb(ps, &uurb,
1500                         (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1501                         arg);
1502 }
1503
1504 static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
1505 {
1506         struct urb *urb;
1507         struct async *as;
1508         unsigned long flags;
1509
1510         spin_lock_irqsave(&ps->lock, flags);
1511         as = async_getpending(ps, arg);
1512         if (!as) {
1513                 spin_unlock_irqrestore(&ps->lock, flags);
1514                 return -EINVAL;
1515         }
1516
1517         urb = as->urb;
1518         usb_get_urb(urb);
1519         spin_unlock_irqrestore(&ps->lock, flags);
1520
1521         usb_kill_urb(urb);
1522         usb_put_urb(urb);
1523
1524         return 0;
1525 }
1526
1527 static int processcompl(struct async *as, void __user * __user *arg)
1528 {
1529         struct urb *urb = as->urb;
1530         struct usbdevfs_urb __user *userurb = as->userurb;
1531         void __user *addr = as->userurb;
1532         unsigned int i;
1533
1534         if (as->userbuffer && urb->actual_length) {
1535                 if (copy_urb_data_to_user(as->userbuffer, urb))
1536                         goto err_out;
1537         }
1538         if (put_user(as->status, &userurb->status))
1539                 goto err_out;
1540         if (put_user(urb->actual_length, &userurb->actual_length))
1541                 goto err_out;
1542         if (put_user(urb->error_count, &userurb->error_count))
1543                 goto err_out;
1544
1545         if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1546                 for (i = 0; i < urb->number_of_packets; i++) {
1547                         if (put_user(urb->iso_frame_desc[i].actual_length,
1548                                      &userurb->iso_frame_desc[i].actual_length))
1549                                 goto err_out;
1550                         if (put_user(urb->iso_frame_desc[i].status,
1551                                      &userurb->iso_frame_desc[i].status))
1552                                 goto err_out;
1553                 }
1554         }
1555
1556         if (put_user(addr, (void __user * __user *)arg))
1557                 return -EFAULT;
1558         return 0;
1559
1560 err_out:
1561         return -EFAULT;
1562 }
1563
1564 static struct async *reap_as(struct dev_state *ps)
1565 {
1566         DECLARE_WAITQUEUE(wait, current);
1567         struct async *as = NULL;
1568         struct usb_device *dev = ps->dev;
1569
1570         add_wait_queue(&ps->wait, &wait);
1571         for (;;) {
1572                 __set_current_state(TASK_INTERRUPTIBLE);
1573                 as = async_getcompleted(ps);
1574                 if (as)
1575                         break;
1576                 if (signal_pending(current))
1577                         break;
1578                 usb_unlock_device(dev);
1579                 schedule();
1580                 usb_lock_device(dev);
1581         }
1582         remove_wait_queue(&ps->wait, &wait);
1583         set_current_state(TASK_RUNNING);
1584         return as;
1585 }
1586
1587 static int proc_reapurb(struct dev_state *ps, void __user *arg)
1588 {
1589         struct async *as = reap_as(ps);
1590         if (as) {
1591                 int retval = processcompl(as, (void __user * __user *)arg);
1592                 free_async(as);
1593                 return retval;
1594         }
1595         if (signal_pending(current))
1596                 return -EINTR;
1597         return -EIO;
1598 }
1599
1600 static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
1601 {
1602         int retval;
1603         struct async *as;
1604
1605         as = async_getcompleted(ps);
1606         retval = -EAGAIN;
1607         if (as) {
1608                 retval = processcompl(as, (void __user * __user *)arg);
1609                 free_async(as);
1610         }
1611         return retval;
1612 }
1613
1614 #ifdef CONFIG_COMPAT
1615 static int proc_control_compat(struct dev_state *ps,
1616                                 struct usbdevfs_ctrltransfer32 __user *p32)
1617 {
1618         struct usbdevfs_ctrltransfer __user *p;
1619         __u32 udata;
1620         p = compat_alloc_user_space(sizeof(*p));
1621         if (copy_in_user(p, p32, (sizeof(*p32) - sizeof(compat_caddr_t))) ||
1622             get_user(udata, &p32->data) ||
1623             put_user(compat_ptr(udata), &p->data))
1624                 return -EFAULT;
1625         return proc_control(ps, p);
1626 }
1627
1628 static int proc_bulk_compat(struct dev_state *ps,
1629                         struct usbdevfs_bulktransfer32 __user *p32)
1630 {
1631         struct usbdevfs_bulktransfer __user *p;
1632         compat_uint_t n;
1633         compat_caddr_t addr;
1634
1635         p = compat_alloc_user_space(sizeof(*p));
1636
1637         if (get_user(n, &p32->ep) || put_user(n, &p->ep) ||
1638             get_user(n, &p32->len) || put_user(n, &p->len) ||
1639             get_user(n, &p32->timeout) || put_user(n, &p->timeout) ||
1640             get_user(addr, &p32->data) || put_user(compat_ptr(addr), &p->data))
1641                 return -EFAULT;
1642
1643         return proc_bulk(ps, p);
1644 }
1645 static int proc_disconnectsignal_compat(struct dev_state *ps, void __user *arg)
1646 {
1647         struct usbdevfs_disconnectsignal32 ds;
1648
1649         if (copy_from_user(&ds, arg, sizeof(ds)))
1650                 return -EFAULT;
1651         ps->discsignr = ds.signr;
1652         ps->disccontext = compat_ptr(ds.context);
1653         return 0;
1654 }
1655
1656 static int get_urb32(struct usbdevfs_urb *kurb,
1657                      struct usbdevfs_urb32 __user *uurb)
1658 {
1659         __u32  uptr;
1660         if (!access_ok(VERIFY_READ, uurb, sizeof(*uurb)) ||
1661             __get_user(kurb->type, &uurb->type) ||
1662             __get_user(kurb->endpoint, &uurb->endpoint) ||
1663             __get_user(kurb->status, &uurb->status) ||
1664             __get_user(kurb->flags, &uurb->flags) ||
1665             __get_user(kurb->buffer_length, &uurb->buffer_length) ||
1666             __get_user(kurb->actual_length, &uurb->actual_length) ||
1667             __get_user(kurb->start_frame, &uurb->start_frame) ||
1668             __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
1669             __get_user(kurb->error_count, &uurb->error_count) ||
1670             __get_user(kurb->signr, &uurb->signr))
1671                 return -EFAULT;
1672
1673         if (__get_user(uptr, &uurb->buffer))
1674                 return -EFAULT;
1675         kurb->buffer = compat_ptr(uptr);
1676         if (__get_user(uptr, &uurb->usercontext))
1677                 return -EFAULT;
1678         kurb->usercontext = compat_ptr(uptr);
1679
1680         return 0;
1681 }
1682
1683 static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
1684 {
1685         struct usbdevfs_urb uurb;
1686
1687         if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
1688                 return -EFAULT;
1689
1690         return proc_do_submiturb(ps, &uurb,
1691                         ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
1692                         arg);
1693 }
1694
1695 static int processcompl_compat(struct async *as, void __user * __user *arg)
1696 {
1697         struct urb *urb = as->urb;
1698         struct usbdevfs_urb32 __user *userurb = as->userurb;
1699         void __user *addr = as->userurb;
1700         unsigned int i;
1701
1702         if (as->userbuffer && urb->actual_length) {
1703                 if (copy_urb_data_to_user(as->userbuffer, urb))
1704                         return -EFAULT;
1705         }
1706         if (put_user(as->status, &userurb->status))
1707                 return -EFAULT;
1708         if (put_user(urb->actual_length, &userurb->actual_length))
1709                 return -EFAULT;
1710         if (put_user(urb->error_count, &userurb->error_count))
1711                 return -EFAULT;
1712
1713         if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1714                 for (i = 0; i < urb->number_of_packets; i++) {
1715                         if (put_user(urb->iso_frame_desc[i].actual_length,
1716                                      &userurb->iso_frame_desc[i].actual_length))
1717                                 return -EFAULT;
1718                         if (put_user(urb->iso_frame_desc[i].status,
1719                                      &userurb->iso_frame_desc[i].status))
1720                                 return -EFAULT;
1721                 }
1722         }
1723
1724         if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
1725                 return -EFAULT;
1726         return 0;
1727 }
1728
1729 static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
1730 {
1731         struct async *as = reap_as(ps);
1732         if (as) {
1733                 int retval = processcompl_compat(as, (void __user * __user *)arg);
1734                 free_async(as);
1735                 return retval;
1736         }
1737         if (signal_pending(current))
1738                 return -EINTR;
1739         return -EIO;
1740 }
1741
1742 static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
1743 {
1744         int retval;
1745         struct async *as;
1746
1747         retval = -EAGAIN;
1748         as = async_getcompleted(ps);
1749         if (as) {
1750                 retval = processcompl_compat(as, (void __user * __user *)arg);
1751                 free_async(as);
1752         }
1753         return retval;
1754 }
1755
1756
1757 #endif
1758
1759 static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
1760 {
1761         struct usbdevfs_disconnectsignal ds;
1762
1763         if (copy_from_user(&ds, arg, sizeof(ds)))
1764                 return -EFAULT;
1765         ps->discsignr = ds.signr;
1766         ps->disccontext = ds.context;
1767         return 0;
1768 }
1769
1770 static int proc_claiminterface(struct dev_state *ps, void __user *arg)
1771 {
1772         unsigned int ifnum;
1773
1774         if (get_user(ifnum, (unsigned int __user *)arg))
1775                 return -EFAULT;
1776         return claimintf(ps, ifnum);
1777 }
1778
1779 static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
1780 {
1781         unsigned int ifnum;
1782         int ret;
1783
1784         if (get_user(ifnum, (unsigned int __user *)arg))
1785                 return -EFAULT;
1786         if ((ret = releaseintf(ps, ifnum)) < 0)
1787                 return ret;
1788         destroy_async_on_interface (ps, ifnum);
1789         return 0;
1790 }
1791
1792 static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
1793 {
1794         int                     size;
1795         void                    *buf = NULL;
1796         int                     retval = 0;
1797         struct usb_interface    *intf = NULL;
1798         struct usb_driver       *driver = NULL;
1799
1800         /* alloc buffer */
1801         if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
1802                 if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
1803                         return -ENOMEM;
1804                 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
1805                         if (copy_from_user(buf, ctl->data, size)) {
1806                                 kfree(buf);
1807                                 return -EFAULT;
1808                         }
1809                 } else {
1810                         memset(buf, 0, size);
1811                 }
1812         }
1813
1814         if (!connected(ps)) {
1815                 kfree(buf);
1816                 return -ENODEV;
1817         }
1818
1819         if (ps->dev->state != USB_STATE_CONFIGURED)
1820                 retval = -EHOSTUNREACH;
1821         else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
1822                 retval = -EINVAL;
1823         else switch (ctl->ioctl_code) {
1824
1825         /* disconnect kernel driver from interface */
1826         case USBDEVFS_DISCONNECT:
1827                 if (intf->dev.driver) {
1828                         driver = to_usb_driver(intf->dev.driver);
1829                         dev_dbg(&intf->dev, "disconnect by usbfs\n");
1830                         usb_driver_release_interface(driver, intf);
1831                 } else
1832                         retval = -ENODATA;
1833                 break;
1834
1835         /* let kernel drivers try to (re)bind to the interface */
1836         case USBDEVFS_CONNECT:
1837                 if (!intf->dev.driver)
1838                         retval = device_attach(&intf->dev);
1839                 else
1840                         retval = -EBUSY;
1841                 break;
1842
1843         /* talk directly to the interface's driver */
1844         default:
1845                 if (intf->dev.driver)
1846                         driver = to_usb_driver(intf->dev.driver);
1847                 if (driver == NULL || driver->unlocked_ioctl == NULL) {
1848                         retval = -ENOTTY;
1849                 } else {
1850                         retval = driver->unlocked_ioctl(intf, ctl->ioctl_code, buf);
1851                         if (retval == -ENOIOCTLCMD)
1852                                 retval = -ENOTTY;
1853                 }
1854         }
1855
1856         /* cleanup and return */
1857         if (retval >= 0
1858                         && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
1859                         && size > 0
1860                         && copy_to_user(ctl->data, buf, size) != 0)
1861                 retval = -EFAULT;
1862
1863         kfree(buf);
1864         return retval;
1865 }
1866
1867 static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
1868 {
1869         struct usbdevfs_ioctl   ctrl;
1870
1871         if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1872                 return -EFAULT;
1873         return proc_ioctl(ps, &ctrl);
1874 }
1875
1876 #ifdef CONFIG_COMPAT
1877 static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
1878 {
1879         struct usbdevfs_ioctl32 __user *uioc;
1880         struct usbdevfs_ioctl ctrl;
1881         u32 udata;
1882
1883         uioc = compat_ptr((long)arg);
1884         if (!access_ok(VERIFY_READ, uioc, sizeof(*uioc)) ||
1885             __get_user(ctrl.ifno, &uioc->ifno) ||
1886             __get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
1887             __get_user(udata, &uioc->data))
1888                 return -EFAULT;
1889         ctrl.data = compat_ptr(udata);
1890
1891         return proc_ioctl(ps, &ctrl);
1892 }
1893 #endif
1894
1895 static int proc_claim_port(struct dev_state *ps, void __user *arg)
1896 {
1897         unsigned portnum;
1898         int rc;
1899
1900         if (get_user(portnum, (unsigned __user *) arg))
1901                 return -EFAULT;
1902         rc = usb_hub_claim_port(ps->dev, portnum, ps);
1903         if (rc == 0)
1904                 snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n",
1905                         portnum, task_pid_nr(current), current->comm);
1906         return rc;
1907 }
1908
1909 static int proc_release_port(struct dev_state *ps, void __user *arg)
1910 {
1911         unsigned portnum;
1912
1913         if (get_user(portnum, (unsigned __user *) arg))
1914                 return -EFAULT;
1915         return usb_hub_release_port(ps->dev, portnum, ps);
1916 }
1917
1918 static int proc_get_capabilities(struct dev_state *ps, void __user *arg)
1919 {
1920         __u32 caps;
1921
1922         caps = USBDEVFS_CAP_ZERO_PACKET | USBDEVFS_CAP_NO_PACKET_SIZE_LIM;
1923         if (!ps->dev->bus->no_stop_on_short)
1924                 caps |= USBDEVFS_CAP_BULK_CONTINUATION;
1925         if (ps->dev->bus->sg_tablesize)
1926                 caps |= USBDEVFS_CAP_BULK_SCATTER_GATHER;
1927
1928         if (put_user(caps, (__u32 __user *)arg))
1929                 return -EFAULT;
1930
1931         return 0;
1932 }
1933
1934 static int proc_disconnect_claim(struct dev_state *ps, void __user *arg)
1935 {
1936         struct usbdevfs_disconnect_claim dc;
1937         struct usb_interface *intf;
1938
1939         if (copy_from_user(&dc, arg, sizeof(dc)))
1940                 return -EFAULT;
1941
1942         intf = usb_ifnum_to_if(ps->dev, dc.interface);
1943         if (!intf)
1944                 return -EINVAL;
1945
1946         if (intf->dev.driver) {
1947                 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
1948
1949                 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_IF_DRIVER) &&
1950                                 strncmp(dc.driver, intf->dev.driver->name,
1951                                         sizeof(dc.driver)) != 0)
1952                         return -EBUSY;
1953
1954                 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_EXCEPT_DRIVER) &&
1955                                 strncmp(dc.driver, intf->dev.driver->name,
1956                                         sizeof(dc.driver)) == 0)
1957                         return -EBUSY;
1958
1959                 dev_dbg(&intf->dev, "disconnect by usbfs\n");
1960                 usb_driver_release_interface(driver, intf);
1961         }
1962
1963         return claimintf(ps, dc.interface);
1964 }
1965
1966 /*
1967  * NOTE:  All requests here that have interface numbers as parameters
1968  * are assuming that somehow the configuration has been prevented from
1969  * changing.  But there's no mechanism to ensure that...
1970  */
1971 static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
1972                                 void __user *p)
1973 {
1974         struct dev_state *ps = file->private_data;
1975         struct inode *inode = file->f_path.dentry->d_inode;
1976         struct usb_device *dev = ps->dev;
1977         int ret = -ENOTTY;
1978
1979         if (!(file->f_mode & FMODE_WRITE))
1980                 return -EPERM;
1981
1982         usb_lock_device(dev);
1983         if (!connected(ps)) {
1984                 usb_unlock_device(dev);
1985                 return -ENODEV;
1986         }
1987
1988         switch (cmd) {
1989         case USBDEVFS_CONTROL:
1990                 snoop(&dev->dev, "%s: CONTROL\n", __func__);
1991                 ret = proc_control(ps, p);
1992                 if (ret >= 0)
1993                         inode->i_mtime = CURRENT_TIME;
1994                 break;
1995
1996         case USBDEVFS_BULK:
1997                 snoop(&dev->dev, "%s: BULK\n", __func__);
1998                 ret = proc_bulk(ps, p);
1999                 if (ret >= 0)
2000                         inode->i_mtime = CURRENT_TIME;
2001                 break;
2002
2003         case USBDEVFS_RESETEP:
2004                 snoop(&dev->dev, "%s: RESETEP\n", __func__);
2005                 ret = proc_resetep(ps, p);
2006                 if (ret >= 0)
2007                         inode->i_mtime = CURRENT_TIME;
2008                 break;
2009
2010         case USBDEVFS_RESET:
2011                 snoop(&dev->dev, "%s: RESET\n", __func__);
2012                 ret = proc_resetdevice(ps);
2013                 break;
2014
2015         case USBDEVFS_CLEAR_HALT:
2016                 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
2017                 ret = proc_clearhalt(ps, p);
2018                 if (ret >= 0)
2019                         inode->i_mtime = CURRENT_TIME;
2020                 break;
2021
2022         case USBDEVFS_GETDRIVER:
2023                 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
2024                 ret = proc_getdriver(ps, p);
2025                 break;
2026
2027         case USBDEVFS_CONNECTINFO:
2028                 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
2029                 ret = proc_connectinfo(ps, p);
2030                 break;
2031
2032         case USBDEVFS_SETINTERFACE:
2033                 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
2034                 ret = proc_setintf(ps, p);
2035                 break;
2036
2037         case USBDEVFS_SETCONFIGURATION:
2038                 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
2039                 ret = proc_setconfig(ps, p);
2040                 break;
2041
2042         case USBDEVFS_SUBMITURB:
2043                 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
2044                 ret = proc_submiturb(ps, p);
2045                 if (ret >= 0)
2046                         inode->i_mtime = CURRENT_TIME;
2047                 break;
2048
2049 #ifdef CONFIG_COMPAT
2050         case USBDEVFS_CONTROL32:
2051                 snoop(&dev->dev, "%s: CONTROL32\n", __func__);
2052                 ret = proc_control_compat(ps, p);
2053                 if (ret >= 0)
2054                         inode->i_mtime = CURRENT_TIME;
2055                 break;
2056
2057         case USBDEVFS_BULK32:
2058                 snoop(&dev->dev, "%s: BULK32\n", __func__);
2059                 ret = proc_bulk_compat(ps, p);
2060                 if (ret >= 0)
2061                         inode->i_mtime = CURRENT_TIME;
2062                 break;
2063
2064         case USBDEVFS_DISCSIGNAL32:
2065                 snoop(&dev->dev, "%s: DISCSIGNAL32\n", __func__);
2066                 ret = proc_disconnectsignal_compat(ps, p);
2067                 break;
2068
2069         case USBDEVFS_SUBMITURB32:
2070                 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
2071                 ret = proc_submiturb_compat(ps, p);
2072                 if (ret >= 0)
2073                         inode->i_mtime = CURRENT_TIME;
2074                 break;
2075
2076         case USBDEVFS_REAPURB32:
2077                 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
2078                 ret = proc_reapurb_compat(ps, p);
2079                 break;
2080
2081         case USBDEVFS_REAPURBNDELAY32:
2082                 snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
2083                 ret = proc_reapurbnonblock_compat(ps, p);
2084                 break;
2085
2086         case USBDEVFS_IOCTL32:
2087                 snoop(&dev->dev, "%s: IOCTL32\n", __func__);
2088                 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
2089                 break;
2090 #endif
2091
2092         case USBDEVFS_DISCARDURB:
2093                 snoop(&dev->dev, "%s: DISCARDURB\n", __func__);
2094                 ret = proc_unlinkurb(ps, p);
2095                 break;
2096
2097         case USBDEVFS_REAPURB:
2098                 snoop(&dev->dev, "%s: REAPURB\n", __func__);
2099                 ret = proc_reapurb(ps, p);
2100                 break;
2101
2102         case USBDEVFS_REAPURBNDELAY:
2103                 snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
2104                 ret = proc_reapurbnonblock(ps, p);
2105                 break;
2106
2107         case USBDEVFS_DISCSIGNAL:
2108                 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
2109                 ret = proc_disconnectsignal(ps, p);
2110                 break;
2111
2112         case USBDEVFS_CLAIMINTERFACE:
2113                 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
2114                 ret = proc_claiminterface(ps, p);
2115                 break;
2116
2117         case USBDEVFS_RELEASEINTERFACE:
2118                 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
2119                 ret = proc_releaseinterface(ps, p);
2120                 break;
2121
2122         case USBDEVFS_IOCTL:
2123                 snoop(&dev->dev, "%s: IOCTL\n", __func__);
2124                 ret = proc_ioctl_default(ps, p);
2125                 break;
2126
2127         case USBDEVFS_CLAIM_PORT:
2128                 snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__);
2129                 ret = proc_claim_port(ps, p);
2130                 break;
2131
2132         case USBDEVFS_RELEASE_PORT:
2133                 snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__);
2134                 ret = proc_release_port(ps, p);
2135                 break;
2136         case USBDEVFS_GET_CAPABILITIES:
2137                 ret = proc_get_capabilities(ps, p);
2138                 break;
2139         case USBDEVFS_DISCONNECT_CLAIM:
2140                 ret = proc_disconnect_claim(ps, p);
2141                 break;
2142         }
2143         usb_unlock_device(dev);
2144         if (ret >= 0)
2145                 inode->i_atime = CURRENT_TIME;
2146         return ret;
2147 }
2148
2149 static long usbdev_ioctl(struct file *file, unsigned int cmd,
2150                         unsigned long arg)
2151 {
2152         int ret;
2153
2154         ret = usbdev_do_ioctl(file, cmd, (void __user *)arg);
2155
2156         return ret;
2157 }
2158
2159 #ifdef CONFIG_COMPAT
2160 static long usbdev_compat_ioctl(struct file *file, unsigned int cmd,
2161                         unsigned long arg)
2162 {
2163         int ret;
2164
2165         ret = usbdev_do_ioctl(file, cmd, compat_ptr(arg));
2166
2167         return ret;
2168 }
2169 #endif
2170
2171 /* No kernel lock - fine */
2172 static unsigned int usbdev_poll(struct file *file,
2173                                 struct poll_table_struct *wait)
2174 {
2175         struct dev_state *ps = file->private_data;
2176         unsigned int mask = 0;
2177
2178         poll_wait(file, &ps->wait, wait);
2179         if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
2180                 mask |= POLLOUT | POLLWRNORM;
2181         if (!connected(ps))
2182                 mask |= POLLERR | POLLHUP;
2183         return mask;
2184 }
2185
2186 const struct file_operations usbdev_file_operations = {
2187         .owner =          THIS_MODULE,
2188         .llseek =         usbdev_lseek,
2189         .read =           usbdev_read,
2190         .poll =           usbdev_poll,
2191         .unlocked_ioctl = usbdev_ioctl,
2192 #ifdef CONFIG_COMPAT
2193         .compat_ioctl =   usbdev_compat_ioctl,
2194 #endif
2195         .open =           usbdev_open,
2196         .release =        usbdev_release,
2197 };
2198
2199 static void usbdev_remove(struct usb_device *udev)
2200 {
2201         struct dev_state *ps;
2202         struct siginfo sinfo;
2203
2204         while (!list_empty(&udev->filelist)) {
2205                 ps = list_entry(udev->filelist.next, struct dev_state, list);
2206                 destroy_all_async(ps);
2207                 wake_up_all(&ps->wait);
2208                 list_del_init(&ps->list);
2209                 if (ps->discsignr) {
2210                         sinfo.si_signo = ps->discsignr;
2211                         sinfo.si_errno = EPIPE;
2212                         sinfo.si_code = SI_ASYNCIO;
2213                         sinfo.si_addr = ps->disccontext;
2214                         kill_pid_info_as_cred(ps->discsignr, &sinfo,
2215                                         ps->disc_pid, ps->cred, ps->secid);
2216                 }
2217         }
2218 }
2219
2220 static int usbdev_notify(struct notifier_block *self,
2221                                unsigned long action, void *dev)
2222 {
2223         switch (action) {
2224         case USB_DEVICE_ADD:
2225                 break;
2226         case USB_DEVICE_REMOVE:
2227                 usbdev_remove(dev);
2228                 break;
2229         }
2230         return NOTIFY_OK;
2231 }
2232
2233 static struct notifier_block usbdev_nb = {
2234         .notifier_call =        usbdev_notify,
2235 };
2236
2237 static struct cdev usb_device_cdev;
2238
2239 int __init usb_devio_init(void)
2240 {
2241         int retval;
2242
2243         retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
2244                                         "usb_device");
2245         if (retval) {
2246                 printk(KERN_ERR "Unable to register minors for usb_device\n");
2247                 goto out;
2248         }
2249         cdev_init(&usb_device_cdev, &usbdev_file_operations);
2250         retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
2251         if (retval) {
2252                 printk(KERN_ERR "Unable to get usb_device major %d\n",
2253                        USB_DEVICE_MAJOR);
2254                 goto error_cdev;
2255         }
2256         usb_register_notify(&usbdev_nb);
2257 out:
2258         return retval;
2259
2260 error_cdev:
2261         unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
2262         goto out;
2263 }
2264
2265 void usb_devio_cleanup(void)
2266 {
2267         usb_unregister_notify(&usbdev_nb);
2268         cdev_del(&usb_device_cdev);
2269         unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
2270 }