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