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