blob: ae94176c64e41245efa23335d6a4787741ef9e7f [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>
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -070049#include <linux/notifier.h>
David Quigley7a019552006-06-30 01:55:48 -070050#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/uaccess.h>
52#include <asm/byteorder.h>
53#include <linux/moduleparam.h>
54
55#include "hcd.h" /* for usbcore internals */
56#include "usb.h"
57
Kay Sieversfbf82fd2005-07-31 01:05:53 +020058#define USB_MAXBUS 64
59#define USB_DEVICE_MAX USB_MAXBUS * 128
Kay Sieversfbf82fd2005-07-31 01:05:53 +020060
Alan Stern4a2a8a22006-07-01 22:05:01 -040061/* Mutual exclusion for removal, open, and release */
62DEFINE_MUTEX(usbfs_mutex);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064struct async {
65 struct list_head asynclist;
66 struct dev_state *ps;
Eric W. Biederman2425c082006-10-02 02:17:28 -070067 struct pid *pid;
Harald Welte46113832005-10-10 19:44:29 +020068 uid_t uid, euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 unsigned int signr;
70 unsigned int ifnum;
71 void __user *userbuffer;
72 void __user *userurb;
73 struct urb *urb;
Alan Sterne0152682007-08-24 15:42:52 -040074 int status;
David Quigley7a019552006-06-30 01:55:48 -070075 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -080078static int usbfs_snoop;
79module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
80MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82#define snoop(dev, format, arg...) \
83 do { \
84 if (usbfs_snoop) \
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -080085 dev_info(dev , format , ## arg); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 } while (0)
87
Alan Sternfad21bd2005-08-10 15:15:57 -040088#define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91#define MAX_USBFS_BUFFER_SIZE 16384
92
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -080093static inline int connected(struct dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Alan Stern349710c2006-07-01 22:05:56 -040095 return (!list_empty(&ps->list) &&
96 ps->dev->state != USB_STATE_NOTATTACHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
100{
101 loff_t ret;
102
103 lock_kernel();
104
105 switch (orig) {
106 case 0:
107 file->f_pos = offset;
108 ret = file->f_pos;
109 break;
110 case 1:
111 file->f_pos += offset;
112 ret = file->f_pos;
113 break;
114 case 2:
115 default:
116 ret = -EINVAL;
117 }
118
119 unlock_kernel();
120 return ret;
121}
122
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800123static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
124 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200126 struct dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 struct usb_device *dev = ps->dev;
128 ssize_t ret = 0;
129 unsigned len;
130 loff_t pos;
131 int i;
132
133 pos = *ppos;
134 usb_lock_device(dev);
Alan Stern349710c2006-07-01 22:05:56 -0400135 if (!connected(ps)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 ret = -ENODEV;
137 goto err;
138 } else if (pos < 0) {
139 ret = -EINVAL;
140 goto err;
141 }
142
143 if (pos < sizeof(struct usb_device_descriptor)) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800144 /* 18 bytes - fits on the stack */
145 struct usb_device_descriptor temp_desc;
Oliver Neukum8781ba02006-01-06 21:24:25 +0100146
147 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
Andrew Morton9fcd5c32006-01-18 23:55:07 -0800148 le16_to_cpus(&temp_desc.bcdUSB);
149 le16_to_cpus(&temp_desc.idVendor);
150 le16_to_cpus(&temp_desc.idProduct);
151 le16_to_cpus(&temp_desc.bcdDevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 len = sizeof(struct usb_device_descriptor) - pos;
154 if (len > nbytes)
155 len = nbytes;
Oliver Neukum8781ba02006-01-06 21:24:25 +0100156 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 ret = -EFAULT;
158 goto err;
159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 *ppos += len;
162 buf += len;
163 nbytes -= len;
164 ret += len;
165 }
166
167 pos = sizeof(struct usb_device_descriptor);
168 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
169 struct usb_config_descriptor *config =
170 (struct usb_config_descriptor *)dev->rawdescriptors[i];
171 unsigned int length = le16_to_cpu(config->wTotalLength);
172
173 if (*ppos < pos + length) {
174
175 /* The descriptor may claim to be longer than it
176 * really is. Here is the actual allocated length. */
177 unsigned alloclen =
178 le16_to_cpu(dev->config[i].desc.wTotalLength);
179
180 len = length - (*ppos - pos);
181 if (len > nbytes)
182 len = nbytes;
183
184 /* Simply don't write (skip over) unallocated parts */
185 if (alloclen > (*ppos - pos)) {
186 alloclen -= (*ppos - pos);
187 if (copy_to_user(buf,
188 dev->rawdescriptors[i] + (*ppos - pos),
189 min(len, alloclen))) {
190 ret = -EFAULT;
191 goto err;
192 }
193 }
194
195 *ppos += len;
196 buf += len;
197 nbytes -= len;
198 ret += len;
199 }
200
201 pos += length;
202 }
203
204err:
205 usb_unlock_device(dev);
206 return ret;
207}
208
209/*
210 * async list handling
211 */
212
213static struct async *alloc_async(unsigned int numisoframes)
214{
Pete Zaitcevdd95b812008-01-05 02:01:07 -0800215 struct async *as;
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400216
Pete Zaitcevdd95b812008-01-05 02:01:07 -0800217 as = kzalloc(sizeof(struct async), GFP_KERNEL);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800218 if (!as)
219 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
221 if (!as->urb) {
222 kfree(as);
223 return NULL;
224 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800225 return as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228static void free_async(struct async *as)
229{
Eric W. Biederman2425c082006-10-02 02:17:28 -0700230 put_pid(as->pid);
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -0700231 kfree(as->urb->transfer_buffer);
232 kfree(as->urb->setup_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 usb_free_urb(as->urb);
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -0700234 kfree(as);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237static inline void async_newpending(struct async *as)
238{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800239 struct dev_state *ps = as->ps;
240 unsigned long flags;
241
242 spin_lock_irqsave(&ps->lock, flags);
243 list_add_tail(&as->asynclist, &ps->async_pending);
244 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
247static inline void async_removepending(struct async *as)
248{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800249 struct dev_state *ps = as->ps;
250 unsigned long flags;
251
252 spin_lock_irqsave(&ps->lock, flags);
253 list_del_init(&as->asynclist);
254 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257static inline struct async *async_getcompleted(struct dev_state *ps)
258{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800259 unsigned long flags;
260 struct async *as = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800262 spin_lock_irqsave(&ps->lock, flags);
263 if (!list_empty(&ps->async_completed)) {
264 as = list_entry(ps->async_completed.next, struct async,
265 asynclist);
266 list_del_init(&as->asynclist);
267 }
268 spin_unlock_irqrestore(&ps->lock, flags);
269 return as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800272static inline struct async *async_getpending(struct dev_state *ps,
273 void __user *userurb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800275 unsigned long flags;
276 struct async *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800278 spin_lock_irqsave(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 list_for_each_entry(as, &ps->async_pending, asynclist)
280 if (as->userurb == userurb) {
281 list_del_init(&as->asynclist);
282 spin_unlock_irqrestore(&ps->lock, flags);
283 return as;
284 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800285 spin_unlock_irqrestore(&ps->lock, flags);
286 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700289static void snoop_urb(struct urb *urb, void __user *userurb)
290{
291 int j;
292 unsigned char *data = urb->transfer_buffer;
293
294 if (!usbfs_snoop)
295 return;
296
Alan Stern93cf9b92007-07-30 17:09:28 -0400297 dev_info(&urb->dev->dev, "direction=%s\n",
298 usb_urb_dir_in(urb) ? "IN" : "OUT");
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700299 dev_info(&urb->dev->dev, "userurb=%p\n", userurb);
300 dev_info(&urb->dev->dev, "transfer_buffer_length=%d\n",
301 urb->transfer_buffer_length);
302 dev_info(&urb->dev->dev, "actual_length=%d\n", urb->actual_length);
303 dev_info(&urb->dev->dev, "data: ");
304 for (j = 0; j < urb->transfer_buffer_length; ++j)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800305 printk("%02x ", data[j]);
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700306 printk("\n");
307}
308
David Howells7d12e782006-10-05 14:55:46 +0100309static void async_completed(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800311 struct async *as = urb->context;
312 struct dev_state *ps = as->ps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct siginfo sinfo;
314
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800315 spin_lock(&ps->lock);
316 list_move_tail(&as->asynclist, &ps->async_completed);
317 spin_unlock(&ps->lock);
Alan Sterne0152682007-08-24 15:42:52 -0400318 as->status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (as->signr) {
320 sinfo.si_signo = as->signr;
Alan Sterne0152682007-08-24 15:42:52 -0400321 sinfo.si_errno = as->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 sinfo.si_code = SI_ASYNCIO;
323 sinfo.si_addr = as->userurb;
Eric W. Biederman2425c082006-10-02 02:17:28 -0700324 kill_pid_info_as_uid(as->signr, &sinfo, as->pid, as->uid,
David Quigley7a019552006-06-30 01:55:48 -0700325 as->euid, as->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700327 snoop(&urb->dev->dev, "urb complete\n");
328 snoop_urb(urb, as->userurb);
329 wake_up(&ps->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800332static void destroy_async(struct dev_state *ps, struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 struct async *as;
335 unsigned long flags;
336
337 spin_lock_irqsave(&ps->lock, flags);
338 while (!list_empty(list)) {
339 as = list_entry(list->next, struct async, asynclist);
340 list_del_init(&as->asynclist);
341
342 /* drop the spinlock so the completion handler can run */
343 spin_unlock_irqrestore(&ps->lock, flags);
344 usb_kill_urb(as->urb);
345 spin_lock_irqsave(&ps->lock, flags);
346 }
347 spin_unlock_irqrestore(&ps->lock, flags);
348 as = async_getcompleted(ps);
349 while (as) {
350 free_async(as);
351 as = async_getcompleted(ps);
352 }
353}
354
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800355static void destroy_async_on_interface(struct dev_state *ps,
356 unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 struct list_head *p, *q, hitlist;
359 unsigned long flags;
360
361 INIT_LIST_HEAD(&hitlist);
362 spin_lock_irqsave(&ps->lock, flags);
363 list_for_each_safe(p, q, &ps->async_pending)
364 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
365 list_move_tail(p, &hitlist);
366 spin_unlock_irqrestore(&ps->lock, flags);
367 destroy_async(ps, &hitlist);
368}
369
370static inline void destroy_all_async(struct dev_state *ps)
371{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800372 destroy_async(ps, &ps->async_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375/*
376 * interface claims are made only at the request of user level code,
377 * which can also release them (explicitly or by closing files).
378 * they're also undone when devices disconnect.
379 */
380
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800381static int driver_probe(struct usb_interface *intf,
382 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 return -ENODEV;
385}
386
387static void driver_disconnect(struct usb_interface *intf)
388{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800389 struct dev_state *ps = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
391
392 if (!ps)
393 return;
394
395 /* NOTE: this relies on usbcore having canceled and completed
396 * all pending I/O requests; 2.6 does that.
397 */
398
399 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
400 clear_bit(ifnum, &ps->ifclaimed);
401 else
402 warn("interface number %u out of range", ifnum);
403
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800404 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 /* force async requests to complete */
407 destroy_async_on_interface(ps, ifnum);
408}
409
Alan Stern2e2eb832007-12-04 14:35:15 -0500410/* The following routines are merely placeholders. There is no way
411 * to inform a user task about suspend or resumes.
412 */
413static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
414{
415 return 0;
416}
417
418static int driver_resume(struct usb_interface *intf)
419{
420 return 0;
421}
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423struct usb_driver usbfs_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 .name = "usbfs",
425 .probe = driver_probe,
426 .disconnect = driver_disconnect,
Alan Stern2e2eb832007-12-04 14:35:15 -0500427 .suspend = driver_suspend,
428 .resume = driver_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429};
430
431static int claimintf(struct dev_state *ps, unsigned int ifnum)
432{
433 struct usb_device *dev = ps->dev;
434 struct usb_interface *intf;
435 int err;
436
437 if (ifnum >= 8*sizeof(ps->ifclaimed))
438 return -EINVAL;
439 /* already claimed */
440 if (test_bit(ifnum, &ps->ifclaimed))
441 return 0;
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 intf = usb_ifnum_to_if(dev, ifnum);
444 if (!intf)
445 err = -ENOENT;
446 else
447 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (err == 0)
449 set_bit(ifnum, &ps->ifclaimed);
450 return err;
451}
452
453static int releaseintf(struct dev_state *ps, unsigned int ifnum)
454{
455 struct usb_device *dev;
456 struct usb_interface *intf;
457 int err;
458
459 err = -EINVAL;
460 if (ifnum >= 8*sizeof(ps->ifclaimed))
461 return err;
462 dev = ps->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 intf = usb_ifnum_to_if(dev, ifnum);
464 if (!intf)
465 err = -ENOENT;
466 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
467 usb_driver_release_interface(&usbfs_driver, intf);
468 err = 0;
469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return err;
471}
472
473static int checkintf(struct dev_state *ps, unsigned int ifnum)
474{
475 if (ps->dev->state != USB_STATE_CONFIGURED)
476 return -EHOSTUNREACH;
477 if (ifnum >= 8*sizeof(ps->ifclaimed))
478 return -EINVAL;
479 if (test_bit(ifnum, &ps->ifclaimed))
480 return 0;
481 /* if not yet claimed, claim it for the driver */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800482 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
483 "interface %u before use\n", task_pid_nr(current),
484 current->comm, ifnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return claimintf(ps, ifnum);
486}
487
488static int findintfep(struct usb_device *dev, unsigned int ep)
489{
490 unsigned int i, j, e;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800491 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 struct usb_host_interface *alts;
493 struct usb_endpoint_descriptor *endpt;
494
495 if (ep & ~(USB_DIR_IN|0xf))
496 return -EINVAL;
497 if (!dev->actconfig)
498 return -ESRCH;
499 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
500 intf = dev->actconfig->interface[i];
501 for (j = 0; j < intf->num_altsetting; j++) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800502 alts = &intf->altsetting[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
504 endpt = &alts->endpoint[e].desc;
505 if (endpt->bEndpointAddress == ep)
506 return alts->desc.bInterfaceNumber;
507 }
508 }
509 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800510 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800513static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
514 unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 int ret = 0;
517
Horst Schirmeier24f8b112006-03-11 00:16:55 -0800518 if (ps->dev->state != USB_STATE_ADDRESS
519 && ps->dev->state != USB_STATE_CONFIGURED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return -EHOSTUNREACH;
521 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
522 return 0;
523
524 index &= 0xff;
525 switch (requesttype & USB_RECIP_MASK) {
526 case USB_RECIP_ENDPOINT:
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800527 ret = findintfep(ps->dev, index);
528 if (ret >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 ret = checkintf(ps, ret);
530 break;
531
532 case USB_RECIP_INTERFACE:
533 ret = checkintf(ps, index);
534 break;
535 }
536 return ret;
537}
538
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100539static int __match_minor(struct device *dev, void *data)
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700540{
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100541 int minor = *((int *)data);
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700542
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100543 if (dev->devt == MKDEV(USB_DEVICE_MAJOR, minor))
544 return 1;
545 return 0;
546}
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700547
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100548static struct usb_device *usbdev_lookup_by_minor(int minor)
549{
550 struct device *dev;
551
552 dev = bus_find_device(&usb_bus_type, NULL, &minor, __match_minor);
553 if (!dev)
554 return NULL;
555 put_device(dev);
556 return container_of(dev, struct usb_device, dev);
557}
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559/*
560 * file operations
561 */
562static int usbdev_open(struct inode *inode, struct file *file)
563{
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200564 struct usb_device *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct dev_state *ps;
566 int ret;
567
Alan Stern4a2a8a22006-07-01 22:05:01 -0400568 /* Protect against simultaneous removal or release */
569 mutex_lock(&usbfs_mutex);
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 ret = -ENOMEM;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800572 ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
573 if (!ps)
Alan Stern4a2a8a22006-07-01 22:05:01 -0400574 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 ret = -ENOENT;
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100577 /* usbdev device-node */
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200578 if (imajor(inode) == USB_DEVICE_MAJOR)
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100579 dev = usbdev_lookup_by_minor(iminor(inode));
580#ifdef CONFIG_USB_DEVICEFS
581 /* procfs file */
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200582 if (!dev)
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700583 dev = inode->i_private;
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100584#endif
Alan Stern01d883d2006-08-30 15:47:18 -0400585 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 goto out;
Alan Stern94fcda12006-11-20 11:38:46 -0500587 ret = usb_autoresume_device(dev);
Alan Stern01d883d2006-08-30 15:47:18 -0400588 if (ret)
589 goto out;
590
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200591 usb_get_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 ret = 0;
593 ps->dev = dev;
594 ps->file = file;
595 spin_lock_init(&ps->lock);
Dan Carpenter316547f2006-12-13 00:03:38 -0800596 INIT_LIST_HEAD(&ps->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 INIT_LIST_HEAD(&ps->async_pending);
598 INIT_LIST_HEAD(&ps->async_completed);
599 init_waitqueue_head(&ps->wait);
600 ps->discsignr = 0;
Eric W. Biederman2425c082006-10-02 02:17:28 -0700601 ps->disc_pid = get_pid(task_pid(current));
Linus Torvaldsd7dd8a72005-10-10 16:31:30 -0700602 ps->disc_uid = current->uid;
603 ps->disc_euid = current->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 ps->disccontext = NULL;
605 ps->ifclaimed = 0;
David Quigley7a019552006-06-30 01:55:48 -0700606 security_task_getsecid(current, &ps->secid);
Oliver Neukum527660a2007-04-20 20:50:48 +0200607 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 list_add_tail(&ps->list, &dev->filelist);
609 file->private_data = ps;
610 out:
Alan Stern01d883d2006-08-30 15:47:18 -0400611 if (ret)
612 kfree(ps);
Alan Stern4a2a8a22006-07-01 22:05:01 -0400613 mutex_unlock(&usbfs_mutex);
614 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617static int usbdev_release(struct inode *inode, struct file *file)
618{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200619 struct dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 struct usb_device *dev = ps->dev;
621 unsigned int ifnum;
622
623 usb_lock_device(dev);
Alan Stern4a2a8a22006-07-01 22:05:01 -0400624
625 /* Protect against simultaneous open */
626 mutex_lock(&usbfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 list_del_init(&ps->list);
Alan Stern4a2a8a22006-07-01 22:05:01 -0400628 mutex_unlock(&usbfs_mutex);
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
631 ifnum++) {
632 if (test_bit(ifnum, &ps->ifclaimed))
633 releaseintf(ps, ifnum);
634 }
635 destroy_all_async(ps);
Alan Stern94fcda12006-11-20 11:38:46 -0500636 usb_autosuspend_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 usb_unlock_device(dev);
638 usb_put_dev(dev);
Eric W. Biederman2425c082006-10-02 02:17:28 -0700639 put_pid(ps->disc_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 kfree(ps);
Alan Stern4a2a8a22006-07-01 22:05:01 -0400641 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644static int proc_control(struct dev_state *ps, void __user *arg)
645{
646 struct usb_device *dev = ps->dev;
647 struct usbdevfs_ctrltransfer ctrl;
648 unsigned int tmo;
649 unsigned char *tbuf;
650 int i, j, ret;
651
652 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
653 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800654 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex);
655 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return ret;
657 if (ctrl.wLength > PAGE_SIZE)
658 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800659 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
660 if (!tbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return -ENOMEM;
662 tmo = ctrl.timeout;
663 if (ctrl.bRequestType & 0x80) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800664 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
665 ctrl.wLength)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 free_page((unsigned long)tbuf);
667 return -EINVAL;
668 }
Alan Sternfe0410c2005-07-29 12:16:58 -0700669 snoop(&dev->dev, "control read: bRequest=%02x "
670 "bRrequestType=%02x wValue=%04x "
671 "wIndex=%04x wLength=%04x\n",
672 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
673 ctrl.wIndex, ctrl.wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 usb_unlock_device(dev);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800676 i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest,
677 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
678 tbuf, ctrl.wLength, tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 usb_lock_device(dev);
680 if ((i > 0) && ctrl.wLength) {
681 if (usbfs_snoop) {
682 dev_info(&dev->dev, "control read: data ");
Alan Sternfe0410c2005-07-29 12:16:58 -0700683 for (j = 0; j < i; ++j)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800684 printk("%02x ", (u8)(tbuf)[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 printk("\n");
686 }
Alan Sternfe0410c2005-07-29 12:16:58 -0700687 if (copy_to_user(ctrl.data, tbuf, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 free_page((unsigned long)tbuf);
689 return -EFAULT;
690 }
691 }
692 } else {
693 if (ctrl.wLength) {
694 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
695 free_page((unsigned long)tbuf);
696 return -EFAULT;
697 }
698 }
Alan Sternfe0410c2005-07-29 12:16:58 -0700699 snoop(&dev->dev, "control write: bRequest=%02x "
700 "bRrequestType=%02x wValue=%04x "
701 "wIndex=%04x wLength=%04x\n",
702 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
703 ctrl.wIndex, ctrl.wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (usbfs_snoop) {
705 dev_info(&dev->dev, "control write: data: ");
706 for (j = 0; j < ctrl.wLength; ++j)
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700707 printk("%02x ", (unsigned char)(tbuf)[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 printk("\n");
709 }
710 usb_unlock_device(dev);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800711 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
712 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
713 tbuf, ctrl.wLength, tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 usb_lock_device(dev);
715 }
716 free_page((unsigned long)tbuf);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800717 if (i < 0 && i != -EPIPE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
719 "failed cmd %s rqt %u rq %u len %u ret %d\n",
720 current->comm, ctrl.bRequestType, ctrl.bRequest,
721 ctrl.wLength, i);
722 }
723 return i;
724}
725
726static int proc_bulk(struct dev_state *ps, void __user *arg)
727{
728 struct usb_device *dev = ps->dev;
729 struct usbdevfs_bulktransfer bulk;
730 unsigned int tmo, len1, pipe;
731 int len2;
732 unsigned char *tbuf;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700733 int i, j, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 if (copy_from_user(&bulk, arg, sizeof(bulk)))
736 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800737 ret = findintfep(ps->dev, bulk.ep);
738 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800740 ret = checkintf(ps, ret);
741 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return ret;
743 if (bulk.ep & USB_DIR_IN)
744 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
745 else
746 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
747 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
748 return -EINVAL;
749 len1 = bulk.len;
750 if (len1 > MAX_USBFS_BUFFER_SIZE)
751 return -EINVAL;
752 if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
753 return -ENOMEM;
754 tmo = bulk.timeout;
755 if (bulk.ep & 0x80) {
756 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
757 kfree(tbuf);
758 return -EINVAL;
759 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700760 snoop(&dev->dev, "bulk read: len=0x%02x timeout=%04d\n",
761 bulk.len, bulk.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 usb_unlock_device(dev);
763 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
764 usb_lock_device(dev);
765 if (!i && len2) {
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700766 if (usbfs_snoop) {
767 dev_info(&dev->dev, "bulk read: data ");
768 for (j = 0; j < len2; ++j)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800769 printk("%02x ", (u8)(tbuf)[j]);
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700770 printk("\n");
771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (copy_to_user(bulk.data, tbuf, len2)) {
773 kfree(tbuf);
774 return -EFAULT;
775 }
776 }
777 } else {
778 if (len1) {
779 if (copy_from_user(tbuf, bulk.data, len1)) {
780 kfree(tbuf);
781 return -EFAULT;
782 }
783 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700784 snoop(&dev->dev, "bulk write: len=0x%02x timeout=%04d\n",
785 bulk.len, bulk.timeout);
786 if (usbfs_snoop) {
787 dev_info(&dev->dev, "bulk write: data: ");
788 for (j = 0; j < len1; ++j)
789 printk("%02x ", (unsigned char)(tbuf)[j]);
790 printk("\n");
791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 usb_unlock_device(dev);
793 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
794 usb_lock_device(dev);
795 }
796 kfree(tbuf);
797 if (i < 0)
798 return i;
799 return len2;
800}
801
802static int proc_resetep(struct dev_state *ps, void __user *arg)
803{
804 unsigned int ep;
805 int ret;
806
807 if (get_user(ep, (unsigned int __user *)arg))
808 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800809 ret = findintfep(ps->dev, ep);
810 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800812 ret = checkintf(ps, ret);
813 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return ret;
815 usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0);
816 return 0;
817}
818
819static int proc_clearhalt(struct dev_state *ps, void __user *arg)
820{
821 unsigned int ep;
822 int pipe;
823 int ret;
824
825 if (get_user(ep, (unsigned int __user *)arg))
826 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800827 ret = findintfep(ps->dev, ep);
828 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800830 ret = checkintf(ps, ret);
831 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return ret;
833 if (ep & USB_DIR_IN)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800834 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
835 else
836 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 return usb_clear_halt(ps->dev, pipe);
839}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841static int proc_getdriver(struct dev_state *ps, void __user *arg)
842{
843 struct usbdevfs_getdriver gd;
844 struct usb_interface *intf;
845 int ret;
846
847 if (copy_from_user(&gd, arg, sizeof(gd)))
848 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 intf = usb_ifnum_to_if(ps->dev, gd.interface);
850 if (!intf || !intf->dev.driver)
851 ret = -ENODATA;
852 else {
853 strncpy(gd.driver, intf->dev.driver->name,
854 sizeof(gd.driver));
855 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return ret;
858}
859
860static int proc_connectinfo(struct dev_state *ps, void __user *arg)
861{
862 struct usbdevfs_connectinfo ci;
863
864 ci.devnum = ps->dev->devnum;
865 ci.slow = ps->dev->speed == USB_SPEED_LOW;
866 if (copy_to_user(arg, &ci, sizeof(ci)))
867 return -EFAULT;
868 return 0;
869}
870
871static int proc_resetdevice(struct dev_state *ps)
872{
Alan Stern79efa092006-06-01 13:33:42 -0400873 return usb_reset_composite_device(ps->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
876static int proc_setintf(struct dev_state *ps, void __user *arg)
877{
878 struct usbdevfs_setinterface setintf;
879 int ret;
880
881 if (copy_from_user(&setintf, arg, sizeof(setintf)))
882 return -EFAULT;
883 if ((ret = checkintf(ps, setintf.interface)))
884 return ret;
885 return usb_set_interface(ps->dev, setintf.interface,
886 setintf.altsetting);
887}
888
889static int proc_setconfig(struct dev_state *ps, void __user *arg)
890{
Alan Stern3f141e22007-02-08 16:40:43 -0500891 int u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 int status = 0;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800893 struct usb_host_config *actconfig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Alan Stern3f141e22007-02-08 16:40:43 -0500895 if (get_user(u, (int __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return -EFAULT;
897
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800898 actconfig = ps->dev->actconfig;
899
900 /* Don't touch the device if any interfaces are claimed.
901 * It could interfere with other drivers' operations, and if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 * an interface is claimed by usbfs it could easily deadlock.
903 */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800904 if (actconfig) {
905 int i;
906
907 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
908 if (usb_interface_claimed(actconfig->interface[i])) {
909 dev_warn(&ps->dev->dev,
David Brownell72ebddb2005-04-11 18:34:17 -0700910 "usbfs: interface %d claimed by %s "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 "while '%s' sets config #%d\n",
912 actconfig->interface[i]
913 ->cur_altsetting
914 ->desc.bInterfaceNumber,
David Brownell72ebddb2005-04-11 18:34:17 -0700915 actconfig->interface[i]
916 ->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 current->comm, u);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800918 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800921 }
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
925 * so avoid usb_set_configuration()'s kick to sysfs
926 */
927 if (status == 0) {
928 if (actconfig && actconfig->desc.bConfigurationValue == u)
929 status = usb_reset_configuration(ps->dev);
930 else
931 status = usb_set_configuration(ps->dev, u);
932 }
933
934 return status;
935}
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800938 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
939 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 struct usbdevfs_iso_packet_desc *isopkt = NULL;
942 struct usb_host_endpoint *ep;
943 struct async *as;
944 struct usb_ctrlrequest *dr = NULL;
945 unsigned int u, totlen, isofrmlen;
Alan Stern97b9eb92007-02-26 14:56:14 -0500946 int ret, ifnum = -1;
Alan Stern93cf9b92007-07-30 17:09:28 -0400947 int is_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP|USBDEVFS_URB_SHORT_NOT_OK|
950 URB_NO_FSBR|URB_ZERO_PACKET))
951 return -EINVAL;
952 if (!uurb->buffer)
953 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800954 if (uurb->signr != 0 && (uurb->signr < SIGRTMIN ||
955 uurb->signr > SIGRTMAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800957 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
958 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
959 ifnum = findintfep(ps->dev, uurb->endpoint);
960 if (ifnum < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return ifnum;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800962 ret = checkintf(ps, ifnum);
963 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return ret;
965 }
Alan Stern93cf9b92007-07-30 17:09:28 -0400966 if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
967 is_in = 1;
968 ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
969 } else {
970 is_in = 0;
971 ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (!ep)
974 return -ENOENT;
975 switch(uurb->type) {
976 case USBDEVFS_URB_TYPE_CONTROL:
Alan Stern93cf9b92007-07-30 17:09:28 -0400977 if (!usb_endpoint_xfer_control(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800979 /* min 8 byte setup packet,
980 * max 8 byte setup plus an arbitrary data stage */
981 if (uurb->buffer_length < 8 ||
982 uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800984 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
985 if (!dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return -ENOMEM;
987 if (copy_from_user(dr, uurb->buffer, 8)) {
988 kfree(dr);
989 return -EFAULT;
990 }
991 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
992 kfree(dr);
993 return -EINVAL;
994 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800995 ret = check_ctrlrecip(ps, dr->bRequestType,
996 le16_to_cpup(&dr->wIndex));
997 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 kfree(dr);
999 return ret;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 uurb->number_of_packets = 0;
1002 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1003 uurb->buffer += 8;
Alan Stern93cf9b92007-07-30 17:09:28 -04001004 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1005 is_in = 1;
1006 uurb->endpoint |= USB_DIR_IN;
1007 } else {
1008 is_in = 0;
1009 uurb->endpoint &= ~USB_DIR_IN;
1010 }
1011 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1012 uurb->buffer, uurb->buffer_length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 kfree(dr);
1014 return -EFAULT;
1015 }
Chris Freydf251b82006-12-16 02:37:42 -05001016 snoop(&ps->dev->dev, "control urb: bRequest=%02x "
1017 "bRrequestType=%02x wValue=%04x "
1018 "wIndex=%04x wLength=%04x\n",
Alan Stern93cf9b92007-07-30 17:09:28 -04001019 dr->bRequest, dr->bRequestType,
1020 __le16_to_cpup(&dr->wValue),
1021 __le16_to_cpup(&dr->wIndex),
1022 __le16_to_cpup(&dr->wLength));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 break;
1024
1025 case USBDEVFS_URB_TYPE_BULK:
Alan Stern93cf9b92007-07-30 17:09:28 -04001026 switch (usb_endpoint_type(&ep->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 case USB_ENDPOINT_XFER_CONTROL:
1028 case USB_ENDPOINT_XFER_ISOC:
1029 return -EINVAL;
1030 /* allow single-shot interrupt transfers, at bogus rates */
1031 }
1032 uurb->number_of_packets = 0;
1033 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1034 return -EINVAL;
Alan Stern93cf9b92007-07-30 17:09:28 -04001035 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1036 uurb->buffer, uurb->buffer_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return -EFAULT;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -07001038 snoop(&ps->dev->dev, "bulk urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 break;
1040
1041 case USBDEVFS_URB_TYPE_ISO:
1042 /* arbitrary limit */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001043 if (uurb->number_of_packets < 1 ||
1044 uurb->number_of_packets > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return -EINVAL;
Alan Stern93cf9b92007-07-30 17:09:28 -04001046 if (!usb_endpoint_xfer_isoc(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001048 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1049 uurb->number_of_packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
1051 return -ENOMEM;
1052 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
1053 kfree(isopkt);
1054 return -EFAULT;
1055 }
1056 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001057 /* arbitrary limit,
1058 * sufficient for USB 2.0 high-bandwidth iso */
Micah Dowty36122422006-05-19 11:26:24 -07001059 if (isopkt[u].length > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 kfree(isopkt);
1061 return -EINVAL;
1062 }
1063 totlen += isopkt[u].length;
1064 }
1065 if (totlen > 32768) {
1066 kfree(isopkt);
1067 return -EINVAL;
1068 }
1069 uurb->buffer_length = totlen;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -07001070 snoop(&ps->dev->dev, "iso urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 break;
1072
1073 case USBDEVFS_URB_TYPE_INTERRUPT:
1074 uurb->number_of_packets = 0;
Alan Stern93cf9b92007-07-30 17:09:28 -04001075 if (!usb_endpoint_xfer_int(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1078 return -EINVAL;
Alan Stern93cf9b92007-07-30 17:09:28 -04001079 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1080 uurb->buffer, uurb->buffer_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return -EFAULT;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -07001082 snoop(&ps->dev->dev, "interrupt urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 break;
1084
1085 default:
1086 return -EINVAL;
1087 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001088 as = alloc_async(uurb->number_of_packets);
1089 if (!as) {
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -07001090 kfree(isopkt);
1091 kfree(dr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return -ENOMEM;
1093 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001094 as->urb->transfer_buffer = kmalloc(uurb->buffer_length, GFP_KERNEL);
1095 if (!as->urb->transfer_buffer) {
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -07001096 kfree(isopkt);
1097 kfree(dr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 free_async(as);
1099 return -ENOMEM;
1100 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001101 as->urb->dev = ps->dev;
1102 as->urb->pipe = (uurb->type << 30) |
Alan Stern93cf9b92007-07-30 17:09:28 -04001103 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1104 (uurb->endpoint & USB_DIR_IN);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001105 as->urb->transfer_flags = uurb->flags |
Alan Stern93cf9b92007-07-30 17:09:28 -04001106 (is_in ? URB_DIR_IN : URB_DIR_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 as->urb->transfer_buffer_length = uurb->buffer_length;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001108 as->urb->setup_packet = (unsigned char *)dr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 as->urb->start_frame = uurb->start_frame;
1110 as->urb->number_of_packets = uurb->number_of_packets;
Alan Stern97b9eb92007-02-26 14:56:14 -05001111 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1112 ps->dev->speed == USB_SPEED_HIGH)
1113 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
1114 else
1115 as->urb->interval = ep->desc.bInterval;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001116 as->urb->context = as;
1117 as->urb->complete = async_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1119 as->urb->iso_frame_desc[u].offset = totlen;
1120 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1121 totlen += isopkt[u].length;
1122 }
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -07001123 kfree(isopkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 as->ps = ps;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001125 as->userurb = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (uurb->endpoint & USB_DIR_IN)
1127 as->userbuffer = uurb->buffer;
1128 else
1129 as->userbuffer = NULL;
1130 as->signr = uurb->signr;
1131 as->ifnum = ifnum;
Eric W. Biederman2425c082006-10-02 02:17:28 -07001132 as->pid = get_pid(task_pid(current));
Harald Welte46113832005-10-10 19:44:29 +02001133 as->uid = current->uid;
1134 as->euid = current->euid;
David Quigley7a019552006-06-30 01:55:48 -07001135 security_task_getsecid(current, &as->secid);
Alan Stern93cf9b92007-07-30 17:09:28 -04001136 if (!is_in) {
1137 if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
1138 as->urb->transfer_buffer_length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 free_async(as);
1140 return -EFAULT;
1141 }
1142 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -07001143 snoop_urb(as->urb, as->userurb);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001144 async_newpending(as);
1145 if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
1146 dev_printk(KERN_DEBUG, &ps->dev->dev,
1147 "usbfs: usb_submit_urb returned %d\n", ret);
1148 async_removepending(as);
1149 free_async(as);
1150 return ret;
1151 }
1152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
1155static int proc_submiturb(struct dev_state *ps, void __user *arg)
1156{
1157 struct usbdevfs_urb uurb;
1158
1159 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1160 return -EFAULT;
1161
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001162 return proc_do_submiturb(ps, &uurb,
1163 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1164 arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
1167static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
1168{
1169 struct async *as;
1170
1171 as = async_getpending(ps, arg);
1172 if (!as)
1173 return -EINVAL;
1174 usb_kill_urb(as->urb);
1175 return 0;
1176}
1177
1178static int processcompl(struct async *as, void __user * __user *arg)
1179{
1180 struct urb *urb = as->urb;
1181 struct usbdevfs_urb __user *userurb = as->userurb;
1182 void __user *addr = as->userurb;
1183 unsigned int i;
1184
1185 if (as->userbuffer)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001186 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1187 urb->transfer_buffer_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return -EFAULT;
Alan Sterne0152682007-08-24 15:42:52 -04001189 if (put_user(as->status, &userurb->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return -EFAULT;
1191 if (put_user(urb->actual_length, &userurb->actual_length))
1192 return -EFAULT;
1193 if (put_user(urb->error_count, &userurb->error_count))
1194 return -EFAULT;
1195
Alan Stern93cf9b92007-07-30 17:09:28 -04001196 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
Christopher Li668a9542005-04-18 17:39:26 -07001197 for (i = 0; i < urb->number_of_packets; i++) {
1198 if (put_user(urb->iso_frame_desc[i].actual_length,
1199 &userurb->iso_frame_desc[i].actual_length))
1200 return -EFAULT;
1201 if (put_user(urb->iso_frame_desc[i].status,
1202 &userurb->iso_frame_desc[i].status))
1203 return -EFAULT;
1204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206
1207 free_async(as);
1208
1209 if (put_user(addr, (void __user * __user *)arg))
1210 return -EFAULT;
1211 return 0;
1212}
1213
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001214static struct async *reap_as(struct dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001216 DECLARE_WAITQUEUE(wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 struct async *as = NULL;
1218 struct usb_device *dev = ps->dev;
1219
1220 add_wait_queue(&ps->wait, &wait);
1221 for (;;) {
1222 __set_current_state(TASK_INTERRUPTIBLE);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001223 as = async_getcompleted(ps);
1224 if (as)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 break;
1226 if (signal_pending(current))
1227 break;
1228 usb_unlock_device(dev);
1229 schedule();
1230 usb_lock_device(dev);
1231 }
1232 remove_wait_queue(&ps->wait, &wait);
1233 set_current_state(TASK_RUNNING);
1234 return as;
1235}
1236
1237static int proc_reapurb(struct dev_state *ps, void __user *arg)
1238{
1239 struct async *as = reap_as(ps);
1240 if (as)
1241 return processcompl(as, (void __user * __user *)arg);
1242 if (signal_pending(current))
1243 return -EINTR;
1244 return -EIO;
1245}
1246
1247static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
1248{
1249 struct async *as;
1250
1251 if (!(as = async_getcompleted(ps)))
1252 return -EAGAIN;
1253 return processcompl(as, (void __user * __user *)arg);
1254}
1255
1256#ifdef CONFIG_COMPAT
1257
1258static int get_urb32(struct usbdevfs_urb *kurb,
1259 struct usbdevfs_urb32 __user *uurb)
1260{
1261 __u32 uptr;
1262 if (get_user(kurb->type, &uurb->type) ||
1263 __get_user(kurb->endpoint, &uurb->endpoint) ||
1264 __get_user(kurb->status, &uurb->status) ||
1265 __get_user(kurb->flags, &uurb->flags) ||
1266 __get_user(kurb->buffer_length, &uurb->buffer_length) ||
1267 __get_user(kurb->actual_length, &uurb->actual_length) ||
1268 __get_user(kurb->start_frame, &uurb->start_frame) ||
1269 __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
1270 __get_user(kurb->error_count, &uurb->error_count) ||
1271 __get_user(kurb->signr, &uurb->signr))
1272 return -EFAULT;
1273
1274 if (__get_user(uptr, &uurb->buffer))
1275 return -EFAULT;
1276 kurb->buffer = compat_ptr(uptr);
1277 if (__get_user(uptr, &uurb->buffer))
1278 return -EFAULT;
1279 kurb->usercontext = compat_ptr(uptr);
1280
1281 return 0;
1282}
1283
1284static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
1285{
1286 struct usbdevfs_urb uurb;
1287
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001288 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return -EFAULT;
1290
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001291 return proc_do_submiturb(ps, &uurb,
1292 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
1293 arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
1296static int processcompl_compat(struct async *as, void __user * __user *arg)
1297{
1298 struct urb *urb = as->urb;
1299 struct usbdevfs_urb32 __user *userurb = as->userurb;
1300 void __user *addr = as->userurb;
1301 unsigned int i;
1302
1303 if (as->userbuffer)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001304 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1305 urb->transfer_buffer_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return -EFAULT;
Alan Sterne0152682007-08-24 15:42:52 -04001307 if (put_user(as->status, &userurb->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return -EFAULT;
1309 if (put_user(urb->actual_length, &userurb->actual_length))
1310 return -EFAULT;
1311 if (put_user(urb->error_count, &userurb->error_count))
1312 return -EFAULT;
1313
Alan Stern93cf9b92007-07-30 17:09:28 -04001314 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
Christopher Li668a9542005-04-18 17:39:26 -07001315 for (i = 0; i < urb->number_of_packets; i++) {
1316 if (put_user(urb->iso_frame_desc[i].actual_length,
1317 &userurb->iso_frame_desc[i].actual_length))
1318 return -EFAULT;
1319 if (put_user(urb->iso_frame_desc[i].status,
1320 &userurb->iso_frame_desc[i].status))
1321 return -EFAULT;
1322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
1324
1325 free_async(as);
Al Viroc714de52006-10-10 22:45:37 +01001326 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return -EFAULT;
1328 return 0;
1329}
1330
1331static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
1332{
1333 struct async *as = reap_as(ps);
1334 if (as)
1335 return processcompl_compat(as, (void __user * __user *)arg);
1336 if (signal_pending(current))
1337 return -EINTR;
1338 return -EIO;
1339}
1340
1341static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
1342{
1343 struct async *as;
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (!(as = async_getcompleted(ps)))
1346 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return processcompl_compat(as, (void __user * __user *)arg);
1348}
1349
1350#endif
1351
1352static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
1353{
1354 struct usbdevfs_disconnectsignal ds;
1355
1356 if (copy_from_user(&ds, arg, sizeof(ds)))
1357 return -EFAULT;
1358 if (ds.signr != 0 && (ds.signr < SIGRTMIN || ds.signr > SIGRTMAX))
1359 return -EINVAL;
1360 ps->discsignr = ds.signr;
1361 ps->disccontext = ds.context;
1362 return 0;
1363}
1364
1365static int proc_claiminterface(struct dev_state *ps, void __user *arg)
1366{
1367 unsigned int ifnum;
1368
1369 if (get_user(ifnum, (unsigned int __user *)arg))
1370 return -EFAULT;
1371 return claimintf(ps, ifnum);
1372}
1373
1374static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
1375{
1376 unsigned int ifnum;
1377 int ret;
1378
1379 if (get_user(ifnum, (unsigned int __user *)arg))
1380 return -EFAULT;
1381 if ((ret = releaseintf(ps, ifnum)) < 0)
1382 return ret;
1383 destroy_async_on_interface (ps, ifnum);
1384 return 0;
1385}
1386
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001387static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 int size;
1390 void *buf = NULL;
1391 int retval = 0;
1392 struct usb_interface *intf = NULL;
1393 struct usb_driver *driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001395 /* alloc buffer */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001396 if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
1397 if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return -ENOMEM;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001399 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001400 if (copy_from_user(buf, ctl->data, size)) {
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -07001401 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return -EFAULT;
1403 }
1404 } else {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001405 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407 }
1408
Alan Stern349710c2006-07-01 22:05:56 -04001409 if (!connected(ps)) {
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -07001410 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return -ENODEV;
1412 }
1413
1414 if (ps->dev->state != USB_STATE_CONFIGURED)
1415 retval = -EHOSTUNREACH;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001416 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
1417 retval = -EINVAL;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001418 else switch (ctl->ioctl_code) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 /* disconnect kernel driver from interface */
1421 case USBDEVFS_DISCONNECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (intf->dev.driver) {
1423 driver = to_usb_driver(intf->dev.driver);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001424 dev_dbg(&intf->dev, "disconnect by usbfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 usb_driver_release_interface(driver, intf);
1426 } else
1427 retval = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 break;
1429
1430 /* let kernel drivers try to (re)bind to the interface */
1431 case USBDEVFS_CONNECT:
Alan Stern885e9742007-12-03 15:42:10 -05001432 if (!intf->dev.driver)
1433 retval = device_attach(&intf->dev);
1434 else
1435 retval = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 break;
1437
1438 /* talk directly to the interface's driver */
1439 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (intf->dev.driver)
1441 driver = to_usb_driver(intf->dev.driver);
1442 if (driver == NULL || driver->ioctl == NULL) {
1443 retval = -ENOTTY;
1444 } else {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001445 retval = driver->ioctl(intf, ctl->ioctl_code, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (retval == -ENOIOCTLCMD)
1447 retval = -ENOTTY;
1448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450
1451 /* cleanup and return */
1452 if (retval >= 0
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001453 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 && size > 0
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001455 && copy_to_user(ctl->data, buf, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 retval = -EFAULT;
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -07001457
1458 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return retval;
1460}
1461
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001462static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
1463{
1464 struct usbdevfs_ioctl ctrl;
1465
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001466 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001467 return -EFAULT;
1468 return proc_ioctl(ps, &ctrl);
1469}
1470
1471#ifdef CONFIG_COMPAT
Andrew Morton777da592005-11-17 09:47:02 -08001472static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001473{
1474 struct usbdevfs_ioctl32 __user *uioc;
1475 struct usbdevfs_ioctl ctrl;
1476 u32 udata;
1477
Andrew Morton058120d2005-11-17 09:47:49 -08001478 uioc = compat_ptr((long)arg);
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001479 if (get_user(ctrl.ifno, &uioc->ifno) ||
1480 get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
1481 __get_user(udata, &uioc->data))
1482 return -EFAULT;
1483 ctrl.data = compat_ptr(udata);
1484
1485 return proc_ioctl(ps, &ctrl);
1486}
1487#endif
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489/*
1490 * NOTE: All requests here that have interface numbers as parameters
1491 * are assuming that somehow the configuration has been prevented from
1492 * changing. But there's no mechanism to ensure that...
1493 */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001494static int usbdev_ioctl(struct inode *inode, struct file *file,
1495 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Tobias Klauserec17cf12006-09-13 21:38:41 +02001497 struct dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 struct usb_device *dev = ps->dev;
1499 void __user *p = (void __user *)arg;
1500 int ret = -ENOTTY;
1501
1502 if (!(file->f_mode & FMODE_WRITE))
1503 return -EPERM;
1504 usb_lock_device(dev);
Alan Stern349710c2006-07-01 22:05:56 -04001505 if (!connected(ps)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 usb_unlock_device(dev);
1507 return -ENODEV;
1508 }
1509
1510 switch (cmd) {
1511 case USBDEVFS_CONTROL:
1512 snoop(&dev->dev, "%s: CONTROL\n", __FUNCTION__);
1513 ret = proc_control(ps, p);
1514 if (ret >= 0)
1515 inode->i_mtime = CURRENT_TIME;
1516 break;
1517
1518 case USBDEVFS_BULK:
1519 snoop(&dev->dev, "%s: BULK\n", __FUNCTION__);
1520 ret = proc_bulk(ps, p);
1521 if (ret >= 0)
1522 inode->i_mtime = CURRENT_TIME;
1523 break;
1524
1525 case USBDEVFS_RESETEP:
1526 snoop(&dev->dev, "%s: RESETEP\n", __FUNCTION__);
1527 ret = proc_resetep(ps, p);
1528 if (ret >= 0)
1529 inode->i_mtime = CURRENT_TIME;
1530 break;
1531
1532 case USBDEVFS_RESET:
1533 snoop(&dev->dev, "%s: RESET\n", __FUNCTION__);
1534 ret = proc_resetdevice(ps);
1535 break;
1536
1537 case USBDEVFS_CLEAR_HALT:
1538 snoop(&dev->dev, "%s: CLEAR_HALT\n", __FUNCTION__);
1539 ret = proc_clearhalt(ps, p);
1540 if (ret >= 0)
1541 inode->i_mtime = CURRENT_TIME;
1542 break;
1543
1544 case USBDEVFS_GETDRIVER:
1545 snoop(&dev->dev, "%s: GETDRIVER\n", __FUNCTION__);
1546 ret = proc_getdriver(ps, p);
1547 break;
1548
1549 case USBDEVFS_CONNECTINFO:
1550 snoop(&dev->dev, "%s: CONNECTINFO\n", __FUNCTION__);
1551 ret = proc_connectinfo(ps, p);
1552 break;
1553
1554 case USBDEVFS_SETINTERFACE:
1555 snoop(&dev->dev, "%s: SETINTERFACE\n", __FUNCTION__);
1556 ret = proc_setintf(ps, p);
1557 break;
1558
1559 case USBDEVFS_SETCONFIGURATION:
1560 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __FUNCTION__);
1561 ret = proc_setconfig(ps, p);
1562 break;
1563
1564 case USBDEVFS_SUBMITURB:
1565 snoop(&dev->dev, "%s: SUBMITURB\n", __FUNCTION__);
1566 ret = proc_submiturb(ps, p);
1567 if (ret >= 0)
1568 inode->i_mtime = CURRENT_TIME;
1569 break;
1570
1571#ifdef CONFIG_COMPAT
1572
1573 case USBDEVFS_SUBMITURB32:
1574 snoop(&dev->dev, "%s: SUBMITURB32\n", __FUNCTION__);
1575 ret = proc_submiturb_compat(ps, p);
1576 if (ret >= 0)
1577 inode->i_mtime = CURRENT_TIME;
1578 break;
1579
1580 case USBDEVFS_REAPURB32:
1581 snoop(&dev->dev, "%s: REAPURB32\n", __FUNCTION__);
1582 ret = proc_reapurb_compat(ps, p);
1583 break;
1584
1585 case USBDEVFS_REAPURBNDELAY32:
1586 snoop(&dev->dev, "%s: REAPURBDELAY32\n", __FUNCTION__);
1587 ret = proc_reapurbnonblock_compat(ps, p);
1588 break;
1589
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001590 case USBDEVFS_IOCTL32:
1591 snoop(&dev->dev, "%s: IOCTL\n", __FUNCTION__);
Al Viroc714de52006-10-10 22:45:37 +01001592 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001593 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594#endif
1595
1596 case USBDEVFS_DISCARDURB:
1597 snoop(&dev->dev, "%s: DISCARDURB\n", __FUNCTION__);
1598 ret = proc_unlinkurb(ps, p);
1599 break;
1600
1601 case USBDEVFS_REAPURB:
1602 snoop(&dev->dev, "%s: REAPURB\n", __FUNCTION__);
1603 ret = proc_reapurb(ps, p);
1604 break;
1605
1606 case USBDEVFS_REAPURBNDELAY:
1607 snoop(&dev->dev, "%s: REAPURBDELAY\n", __FUNCTION__);
1608 ret = proc_reapurbnonblock(ps, p);
1609 break;
1610
1611 case USBDEVFS_DISCSIGNAL:
1612 snoop(&dev->dev, "%s: DISCSIGNAL\n", __FUNCTION__);
1613 ret = proc_disconnectsignal(ps, p);
1614 break;
1615
1616 case USBDEVFS_CLAIMINTERFACE:
1617 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __FUNCTION__);
1618 ret = proc_claiminterface(ps, p);
1619 break;
1620
1621 case USBDEVFS_RELEASEINTERFACE:
1622 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __FUNCTION__);
1623 ret = proc_releaseinterface(ps, p);
1624 break;
1625
1626 case USBDEVFS_IOCTL:
1627 snoop(&dev->dev, "%s: IOCTL\n", __FUNCTION__);
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001628 ret = proc_ioctl_default(ps, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 break;
1630 }
1631 usb_unlock_device(dev);
1632 if (ret >= 0)
1633 inode->i_atime = CURRENT_TIME;
1634 return ret;
1635}
1636
1637/* No kernel lock - fine */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001638static unsigned int usbdev_poll(struct file *file,
1639 struct poll_table_struct *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Tobias Klauserec17cf12006-09-13 21:38:41 +02001641 struct dev_state *ps = file->private_data;
1642 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 poll_wait(file, &ps->wait, wait);
1645 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
1646 mask |= POLLOUT | POLLWRNORM;
Alan Stern349710c2006-07-01 22:05:56 -04001647 if (!connected(ps))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 mask |= POLLERR | POLLHUP;
1649 return mask;
1650}
1651
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001652const struct file_operations usbdev_file_operations = {
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -07001653 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 .llseek = usbdev_lseek,
1655 .read = usbdev_read,
1656 .poll = usbdev_poll,
1657 .ioctl = usbdev_ioctl,
1658 .open = usbdev_open,
1659 .release = usbdev_release,
1660};
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001661
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001662#ifdef CONFIG_USB_DEVICE_CLASS
1663static struct class *usb_classdev_class;
1664
1665static int usb_classdev_add(struct usb_device *dev)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001666{
1667 int minor = ((dev->bus->busnum-1) * 128) + (dev->devnum-1);
1668
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001669 dev->usb_classdev = device_create(usb_classdev_class, &dev->dev,
Greg Kroah-Hartman7bc3d632006-06-19 23:59:31 -07001670 MKDEV(USB_DEVICE_MAJOR, minor),
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001671 "usbdev%d.%d", dev->bus->busnum, dev->devnum);
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001672 if (IS_ERR(dev->usb_classdev))
1673 return PTR_ERR(dev->usb_classdev);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001674
Akinobu Mita27d39e22006-10-09 18:09:33 +09001675 return 0;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001676}
1677
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001678static void usb_classdev_remove(struct usb_device *dev)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001679{
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001680 device_unregister(dev->usb_classdev);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001681}
1682
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001683static int usb_classdev_notify(struct notifier_block *self,
1684 unsigned long action, void *dev)
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001685{
1686 switch (action) {
1687 case USB_DEVICE_ADD:
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001688 if (usb_classdev_add(dev))
Akinobu Mita27d39e22006-10-09 18:09:33 +09001689 return NOTIFY_BAD;
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001690 break;
1691 case USB_DEVICE_REMOVE:
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001692 usb_classdev_remove(dev);
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001693 break;
1694 }
1695 return NOTIFY_OK;
1696}
1697
1698static struct notifier_block usbdev_nb = {
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001699 .notifier_call = usb_classdev_notify,
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001700};
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001701#endif
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001702
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -07001703static struct cdev usb_device_cdev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001704
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001705int __init usb_devio_init(void)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001706{
1707 int retval;
1708
Alan Sternfad21bd2005-08-10 15:15:57 -04001709 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001710 "usb_device");
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001711 if (retval) {
1712 err("unable to register minors for usb_device");
1713 goto out;
1714 }
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001715 cdev_init(&usb_device_cdev, &usbdev_file_operations);
Alan Sternfad21bd2005-08-10 15:15:57 -04001716 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001717 if (retval) {
1718 err("unable to get usb_device major %d", USB_DEVICE_MAJOR);
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001719 goto error_cdev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001720 }
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001721#ifdef CONFIG_USB_DEVICE_CLASS
1722 usb_classdev_class = class_create(THIS_MODULE, "usb_device");
1723 if (IS_ERR(usb_classdev_class)) {
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001724 err("unable to register usb_device class");
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001725 retval = PTR_ERR(usb_classdev_class);
1726 cdev_del(&usb_device_cdev);
1727 usb_classdev_class = NULL;
1728 goto out;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001729 }
1730
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001731 usb_register_notify(&usbdev_nb);
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001732#endif
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001733out:
1734 return retval;
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001735
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001736error_cdev:
1737 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
1738 goto out;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001739}
1740
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001741void usb_devio_cleanup(void)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001742{
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001743#ifdef CONFIG_USB_DEVICE_CLASS
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001744 usb_unregister_notify(&usbdev_nb);
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001745 class_destroy(usb_classdev_class);
1746#endif
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001747 cdev_del(&usb_device_cdev);
Alan Sternfad21bd2005-08-10 15:15:57 -04001748 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001749}