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