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