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