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