blob: 528befdcc7813b64e42aa345eb32dcddf4c9108e [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * This file implements the usbfs/x/y files, where
23 * x is the bus number and y the device number.
24 *
25 * It allows user space programs/"drivers" to communicate directly
26 * with USB devices without intervening kernel driver.
27 *
28 * Revision history
29 * 22.12.1999 0.1 Initial release (split from proc_usb.c)
30 * 04.01.2000 0.2 Turned into its own filesystem
Harald Welte46113832005-10-10 19:44:29 +020031 * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery
32 * (CAN-2005-3055)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
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>
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -070047#include <linux/notifier.h>
David Quigley7a019552006-06-30 01:55:48 -070048#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/uaccess.h>
50#include <asm/byteorder.h>
51#include <linux/moduleparam.h>
52
53#include "hcd.h" /* for usbcore internals */
54#include "usb.h"
55
Kay Sieversfbf82fd2005-07-31 01:05:53 +020056#define USB_MAXBUS 64
57#define USB_DEVICE_MAX USB_MAXBUS * 128
Kay Sieversfbf82fd2005-07-31 01:05:53 +020058
Alan Stern4a2a8a22006-07-01 22:05:01 -040059/* Mutual exclusion for removal, open, and release */
60DEFINE_MUTEX(usbfs_mutex);
61
Alan Sterncd9f0372008-06-24 14:47:04 -040062struct dev_state {
63 struct list_head list; /* state list */
64 struct usb_device *dev;
65 struct file *file;
66 spinlock_t lock; /* protects the async urb lists */
67 struct list_head async_pending;
68 struct list_head async_completed;
69 wait_queue_head_t wait; /* wake up if a request completed */
70 unsigned int discsignr;
71 struct pid *disc_pid;
72 uid_t disc_uid, disc_euid;
73 void __user *disccontext;
74 unsigned long ifclaimed;
75 u32 secid;
76};
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078struct async {
79 struct list_head asynclist;
80 struct dev_state *ps;
Eric W. Biederman2425c082006-10-02 02:17:28 -070081 struct pid *pid;
Harald Welte46113832005-10-10 19:44:29 +020082 uid_t uid, euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 unsigned int signr;
84 unsigned int ifnum;
85 void __user *userbuffer;
86 void __user *userurb;
87 struct urb *urb;
Alan Sterne0152682007-08-24 15:42:52 -040088 int status;
David Quigley7a019552006-06-30 01:55:48 -070089 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -080092static int usbfs_snoop;
93module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
94MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96#define snoop(dev, format, arg...) \
97 do { \
98 if (usbfs_snoop) \
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -080099 dev_info(dev , format , ## arg); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 } while (0)
101
Alan Sternfad21bd2005-08-10 15:15:57 -0400102#define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105#define MAX_USBFS_BUFFER_SIZE 16384
106
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800107static inline int connected(struct dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Alan Stern349710c2006-07-01 22:05:56 -0400109 return (!list_empty(&ps->list) &&
110 ps->dev->state != USB_STATE_NOTATTACHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
114{
115 loff_t ret;
116
117 lock_kernel();
118
119 switch (orig) {
120 case 0:
121 file->f_pos = offset;
122 ret = file->f_pos;
123 break;
124 case 1:
125 file->f_pos += offset;
126 ret = file->f_pos;
127 break;
128 case 2:
129 default:
130 ret = -EINVAL;
131 }
132
133 unlock_kernel();
134 return ret;
135}
136
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800137static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
138 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200140 struct dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct usb_device *dev = ps->dev;
142 ssize_t ret = 0;
143 unsigned len;
144 loff_t pos;
145 int i;
146
147 pos = *ppos;
148 usb_lock_device(dev);
Alan Stern349710c2006-07-01 22:05:56 -0400149 if (!connected(ps)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 ret = -ENODEV;
151 goto err;
152 } else if (pos < 0) {
153 ret = -EINVAL;
154 goto err;
155 }
156
157 if (pos < sizeof(struct usb_device_descriptor)) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800158 /* 18 bytes - fits on the stack */
159 struct usb_device_descriptor temp_desc;
Oliver Neukum8781ba02006-01-06 21:24:25 +0100160
161 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
Andrew Morton9fcd5c32006-01-18 23:55:07 -0800162 le16_to_cpus(&temp_desc.bcdUSB);
163 le16_to_cpus(&temp_desc.idVendor);
164 le16_to_cpus(&temp_desc.idProduct);
165 le16_to_cpus(&temp_desc.bcdDevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 len = sizeof(struct usb_device_descriptor) - pos;
168 if (len > nbytes)
169 len = nbytes;
Oliver Neukum8781ba02006-01-06 21:24:25 +0100170 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 ret = -EFAULT;
172 goto err;
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 *ppos += len;
176 buf += len;
177 nbytes -= len;
178 ret += len;
179 }
180
181 pos = sizeof(struct usb_device_descriptor);
182 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
183 struct usb_config_descriptor *config =
184 (struct usb_config_descriptor *)dev->rawdescriptors[i];
185 unsigned int length = le16_to_cpu(config->wTotalLength);
186
187 if (*ppos < pos + length) {
188
189 /* The descriptor may claim to be longer than it
190 * really is. Here is the actual allocated length. */
191 unsigned alloclen =
192 le16_to_cpu(dev->config[i].desc.wTotalLength);
193
194 len = length - (*ppos - pos);
195 if (len > nbytes)
196 len = nbytes;
197
198 /* Simply don't write (skip over) unallocated parts */
199 if (alloclen > (*ppos - pos)) {
200 alloclen -= (*ppos - pos);
201 if (copy_to_user(buf,
202 dev->rawdescriptors[i] + (*ppos - pos),
203 min(len, alloclen))) {
204 ret = -EFAULT;
205 goto err;
206 }
207 }
208
209 *ppos += len;
210 buf += len;
211 nbytes -= len;
212 ret += len;
213 }
214
215 pos += length;
216 }
217
218err:
219 usb_unlock_device(dev);
220 return ret;
221}
222
223/*
224 * async list handling
225 */
226
227static struct async *alloc_async(unsigned int numisoframes)
228{
Pete Zaitcevdd95b812008-01-05 02:01:07 -0800229 struct async *as;
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400230
Pete Zaitcevdd95b812008-01-05 02:01:07 -0800231 as = kzalloc(sizeof(struct async), GFP_KERNEL);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800232 if (!as)
233 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
235 if (!as->urb) {
236 kfree(as);
237 return NULL;
238 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800239 return as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242static void free_async(struct async *as)
243{
Eric W. Biederman2425c082006-10-02 02:17:28 -0700244 put_pid(as->pid);
Jesper Juhl6fd19f42005-04-18 17:39:33 -0700245 kfree(as->urb->transfer_buffer);
246 kfree(as->urb->setup_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 usb_free_urb(as->urb);
Jesper Juhl6fd19f42005-04-18 17:39:33 -0700248 kfree(as);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251static inline void async_newpending(struct async *as)
252{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800253 struct dev_state *ps = as->ps;
254 unsigned long flags;
255
256 spin_lock_irqsave(&ps->lock, flags);
257 list_add_tail(&as->asynclist, &ps->async_pending);
258 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261static inline void async_removepending(struct async *as)
262{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800263 struct dev_state *ps = as->ps;
264 unsigned long flags;
265
266 spin_lock_irqsave(&ps->lock, flags);
267 list_del_init(&as->asynclist);
268 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271static inline struct async *async_getcompleted(struct dev_state *ps)
272{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800273 unsigned long flags;
274 struct async *as = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800276 spin_lock_irqsave(&ps->lock, flags);
277 if (!list_empty(&ps->async_completed)) {
278 as = list_entry(ps->async_completed.next, struct async,
279 asynclist);
280 list_del_init(&as->asynclist);
281 }
282 spin_unlock_irqrestore(&ps->lock, flags);
283 return as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800286static inline struct async *async_getpending(struct dev_state *ps,
287 void __user *userurb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800289 unsigned long flags;
290 struct async *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800292 spin_lock_irqsave(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 list_for_each_entry(as, &ps->async_pending, asynclist)
294 if (as->userurb == userurb) {
295 list_del_init(&as->asynclist);
296 spin_unlock_irqrestore(&ps->lock, flags);
297 return as;
298 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800299 spin_unlock_irqrestore(&ps->lock, flags);
300 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700303static void snoop_urb(struct urb *urb, void __user *userurb)
304{
305 int j;
306 unsigned char *data = urb->transfer_buffer;
307
308 if (!usbfs_snoop)
309 return;
310
Alan Stern93cf9b92007-07-30 17:09:28 -0400311 dev_info(&urb->dev->dev, "direction=%s\n",
312 usb_urb_dir_in(urb) ? "IN" : "OUT");
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700313 dev_info(&urb->dev->dev, "userurb=%p\n", userurb);
314 dev_info(&urb->dev->dev, "transfer_buffer_length=%d\n",
315 urb->transfer_buffer_length);
316 dev_info(&urb->dev->dev, "actual_length=%d\n", urb->actual_length);
317 dev_info(&urb->dev->dev, "data: ");
318 for (j = 0; j < urb->transfer_buffer_length; ++j)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800319 printk("%02x ", data[j]);
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700320 printk("\n");
321}
322
David Howells7d12e782006-10-05 14:55:46 +0100323static void async_completed(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800325 struct async *as = urb->context;
326 struct dev_state *ps = as->ps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct siginfo sinfo;
328
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800329 spin_lock(&ps->lock);
330 list_move_tail(&as->asynclist, &ps->async_completed);
331 spin_unlock(&ps->lock);
Alan Sterne0152682007-08-24 15:42:52 -0400332 as->status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (as->signr) {
334 sinfo.si_signo = as->signr;
Alan Sterne0152682007-08-24 15:42:52 -0400335 sinfo.si_errno = as->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 sinfo.si_code = SI_ASYNCIO;
337 sinfo.si_addr = as->userurb;
Eric W. Biederman2425c082006-10-02 02:17:28 -0700338 kill_pid_info_as_uid(as->signr, &sinfo, as->pid, as->uid,
David Quigley7a019552006-06-30 01:55:48 -0700339 as->euid, as->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700341 snoop(&urb->dev->dev, "urb complete\n");
342 snoop_urb(urb, as->userurb);
343 wake_up(&ps->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800346static void destroy_async(struct dev_state *ps, struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 struct async *as;
349 unsigned long flags;
350
351 spin_lock_irqsave(&ps->lock, flags);
352 while (!list_empty(list)) {
353 as = list_entry(list->next, struct async, asynclist);
354 list_del_init(&as->asynclist);
355
356 /* drop the spinlock so the completion handler can run */
357 spin_unlock_irqrestore(&ps->lock, flags);
358 usb_kill_urb(as->urb);
359 spin_lock_irqsave(&ps->lock, flags);
360 }
361 spin_unlock_irqrestore(&ps->lock, flags);
362 as = async_getcompleted(ps);
363 while (as) {
364 free_async(as);
365 as = async_getcompleted(ps);
366 }
367}
368
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800369static void destroy_async_on_interface(struct dev_state *ps,
370 unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct list_head *p, *q, hitlist;
373 unsigned long flags;
374
375 INIT_LIST_HEAD(&hitlist);
376 spin_lock_irqsave(&ps->lock, flags);
377 list_for_each_safe(p, q, &ps->async_pending)
378 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
379 list_move_tail(p, &hitlist);
380 spin_unlock_irqrestore(&ps->lock, flags);
381 destroy_async(ps, &hitlist);
382}
383
384static inline void destroy_all_async(struct dev_state *ps)
385{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800386 destroy_async(ps, &ps->async_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389/*
390 * interface claims are made only at the request of user level code,
391 * which can also release them (explicitly or by closing files).
392 * they're also undone when devices disconnect.
393 */
394
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800395static int driver_probe(struct usb_interface *intf,
396 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 return -ENODEV;
399}
400
401static void driver_disconnect(struct usb_interface *intf)
402{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800403 struct dev_state *ps = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
405
406 if (!ps)
407 return;
408
409 /* NOTE: this relies on usbcore having canceled and completed
410 * all pending I/O requests; 2.6 does that.
411 */
412
413 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
414 clear_bit(ifnum, &ps->ifclaimed);
415 else
416 warn("interface number %u out of range", ifnum);
417
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800418 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 /* force async requests to complete */
421 destroy_async_on_interface(ps, ifnum);
422}
423
Alan Stern2e2eb832007-12-04 14:35:15 -0500424/* The following routines are merely placeholders. There is no way
425 * to inform a user task about suspend or resumes.
426 */
427static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
428{
429 return 0;
430}
431
432static int driver_resume(struct usb_interface *intf)
433{
434 return 0;
435}
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437struct usb_driver usbfs_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 .name = "usbfs",
439 .probe = driver_probe,
440 .disconnect = driver_disconnect,
Alan Stern2e2eb832007-12-04 14:35:15 -0500441 .suspend = driver_suspend,
442 .resume = driver_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443};
444
445static int claimintf(struct dev_state *ps, unsigned int ifnum)
446{
447 struct usb_device *dev = ps->dev;
448 struct usb_interface *intf;
449 int err;
450
451 if (ifnum >= 8*sizeof(ps->ifclaimed))
452 return -EINVAL;
453 /* already claimed */
454 if (test_bit(ifnum, &ps->ifclaimed))
455 return 0;
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 intf = usb_ifnum_to_if(dev, ifnum);
458 if (!intf)
459 err = -ENOENT;
460 else
461 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (err == 0)
463 set_bit(ifnum, &ps->ifclaimed);
464 return err;
465}
466
467static int releaseintf(struct dev_state *ps, unsigned int ifnum)
468{
469 struct usb_device *dev;
470 struct usb_interface *intf;
471 int err;
472
473 err = -EINVAL;
474 if (ifnum >= 8*sizeof(ps->ifclaimed))
475 return err;
476 dev = ps->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 intf = usb_ifnum_to_if(dev, ifnum);
478 if (!intf)
479 err = -ENOENT;
480 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
481 usb_driver_release_interface(&usbfs_driver, intf);
482 err = 0;
483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return err;
485}
486
487static int checkintf(struct dev_state *ps, unsigned int ifnum)
488{
489 if (ps->dev->state != USB_STATE_CONFIGURED)
490 return -EHOSTUNREACH;
491 if (ifnum >= 8*sizeof(ps->ifclaimed))
492 return -EINVAL;
493 if (test_bit(ifnum, &ps->ifclaimed))
494 return 0;
495 /* if not yet claimed, claim it for the driver */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800496 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
497 "interface %u before use\n", task_pid_nr(current),
498 current->comm, ifnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return claimintf(ps, ifnum);
500}
501
502static int findintfep(struct usb_device *dev, unsigned int ep)
503{
504 unsigned int i, j, e;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800505 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct usb_host_interface *alts;
507 struct usb_endpoint_descriptor *endpt;
508
509 if (ep & ~(USB_DIR_IN|0xf))
510 return -EINVAL;
511 if (!dev->actconfig)
512 return -ESRCH;
513 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
514 intf = dev->actconfig->interface[i];
515 for (j = 0; j < intf->num_altsetting; j++) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800516 alts = &intf->altsetting[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
518 endpt = &alts->endpoint[e].desc;
519 if (endpt->bEndpointAddress == ep)
520 return alts->desc.bInterfaceNumber;
521 }
522 }
523 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800524 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800527static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
528 unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 int ret = 0;
531
Horst Schirmeier24f8b112006-03-11 00:16:55 -0800532 if (ps->dev->state != USB_STATE_ADDRESS
533 && ps->dev->state != USB_STATE_CONFIGURED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return -EHOSTUNREACH;
535 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
536 return 0;
537
538 index &= 0xff;
539 switch (requesttype & USB_RECIP_MASK) {
540 case USB_RECIP_ENDPOINT:
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800541 ret = findintfep(ps->dev, index);
542 if (ret >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 ret = checkintf(ps, ret);
544 break;
545
546 case USB_RECIP_INTERFACE:
547 ret = checkintf(ps, index);
548 break;
549 }
550 return ret;
551}
552
Alan Stern61ad04a2008-06-24 14:47:12 -0400553static int match_devt(struct device *dev, void *data)
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700554{
David Howellsa80d5ff2008-07-02 12:28:55 +0100555 return dev->devt == (dev_t) (unsigned long) data;
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100556}
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700557
Alan Stern61ad04a2008-06-24 14:47:12 -0400558static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100559{
560 struct device *dev;
561
David Howellsa80d5ff2008-07-02 12:28:55 +0100562 dev = bus_find_device(&usb_bus_type, NULL,
563 (void *) (unsigned long) devt, match_devt);
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100564 if (!dev)
565 return NULL;
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100566 return container_of(dev, struct usb_device, dev);
567}
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569/*
570 * file operations
571 */
572static int usbdev_open(struct inode *inode, struct file *file)
573{
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200574 struct usb_device *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct dev_state *ps;
576 int ret;
577
Jonathan Corbetb5b4aa62008-05-16 14:25:20 -0600578 lock_kernel();
Alan Stern4a2a8a22006-07-01 22:05:01 -0400579 /* Protect against simultaneous removal or release */
580 mutex_lock(&usbfs_mutex);
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 ret = -ENOMEM;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800583 ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
584 if (!ps)
Alan Stern4a2a8a22006-07-01 22:05:01 -0400585 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 ret = -ENOENT;
Alan Stern61ad04a2008-06-24 14:47:12 -0400588
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100589 /* usbdev device-node */
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200590 if (imajor(inode) == USB_DEVICE_MAJOR)
Alan Stern61ad04a2008-06-24 14:47:12 -0400591 dev = usbdev_lookup_by_devt(inode->i_rdev);
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100592#ifdef CONFIG_USB_DEVICEFS
593 /* procfs file */
Alan Sternd64aac32008-06-24 14:47:19 -0400594 if (!dev) {
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700595 dev = inode->i_private;
Alan Sternd64aac32008-06-24 14:47:19 -0400596 if (dev && dev->usbfs_dentry &&
597 dev->usbfs_dentry->d_inode == inode)
598 usb_get_dev(dev);
599 else
600 dev = NULL;
601 }
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100602#endif
Alan Sternd64aac32008-06-24 14:47:19 -0400603 if (!dev || dev->state == USB_STATE_NOTATTACHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 goto out;
Alan Stern94fcda12006-11-20 11:38:46 -0500605 ret = usb_autoresume_device(dev);
Alan Stern01d883d2006-08-30 15:47:18 -0400606 if (ret)
607 goto out;
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 ret = 0;
610 ps->dev = dev;
611 ps->file = file;
612 spin_lock_init(&ps->lock);
Dan Carpenter316547f2006-12-13 00:03:38 -0800613 INIT_LIST_HEAD(&ps->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 INIT_LIST_HEAD(&ps->async_pending);
615 INIT_LIST_HEAD(&ps->async_completed);
616 init_waitqueue_head(&ps->wait);
617 ps->discsignr = 0;
Eric W. Biederman2425c082006-10-02 02:17:28 -0700618 ps->disc_pid = get_pid(task_pid(current));
Linus Torvaldsd7dd8a72005-10-10 16:31:30 -0700619 ps->disc_uid = current->uid;
620 ps->disc_euid = current->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 ps->disccontext = NULL;
622 ps->ifclaimed = 0;
David Quigley7a019552006-06-30 01:55:48 -0700623 security_task_getsecid(current, &ps->secid);
Oliver Neukum527660a2007-04-20 20:50:48 +0200624 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 list_add_tail(&ps->list, &dev->filelist);
626 file->private_data = ps;
Alan Stern2da41d52008-10-06 11:24:26 -0400627 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
628 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 out:
Alan Sternd64aac32008-06-24 14:47:19 -0400630 if (ret) {
Alan Stern01d883d2006-08-30 15:47:18 -0400631 kfree(ps);
Alan Sternd64aac32008-06-24 14:47:19 -0400632 usb_put_dev(dev);
633 }
Alan Stern4a2a8a22006-07-01 22:05:01 -0400634 mutex_unlock(&usbfs_mutex);
Jonathan Corbetb5b4aa62008-05-16 14:25:20 -0600635 unlock_kernel();
Alan Stern4a2a8a22006-07-01 22:05:01 -0400636 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639static int usbdev_release(struct inode *inode, struct file *file)
640{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200641 struct dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 struct usb_device *dev = ps->dev;
643 unsigned int ifnum;
644
645 usb_lock_device(dev);
Alan Stern4a2a8a22006-07-01 22:05:01 -0400646
647 /* Protect against simultaneous open */
648 mutex_lock(&usbfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 list_del_init(&ps->list);
Alan Stern4a2a8a22006-07-01 22:05:01 -0400650 mutex_unlock(&usbfs_mutex);
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
653 ifnum++) {
654 if (test_bit(ifnum, &ps->ifclaimed))
655 releaseintf(ps, ifnum);
656 }
657 destroy_all_async(ps);
Alan Stern94fcda12006-11-20 11:38:46 -0500658 usb_autosuspend_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 usb_unlock_device(dev);
660 usb_put_dev(dev);
Eric W. Biederman2425c082006-10-02 02:17:28 -0700661 put_pid(ps->disc_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 kfree(ps);
Alan Stern4a2a8a22006-07-01 22:05:01 -0400663 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
666static int proc_control(struct dev_state *ps, void __user *arg)
667{
668 struct usb_device *dev = ps->dev;
669 struct usbdevfs_ctrltransfer ctrl;
670 unsigned int tmo;
671 unsigned char *tbuf;
Andrew Mortonff66e3c2008-03-12 13:32:24 -0700672 unsigned wLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 int i, j, ret;
674
675 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
676 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800677 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex);
678 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return ret;
Andrew Mortonff66e3c2008-03-12 13:32:24 -0700680 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
681 if (wLength > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800683 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
684 if (!tbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return -ENOMEM;
686 tmo = ctrl.timeout;
687 if (ctrl.bRequestType & 0x80) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800688 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
689 ctrl.wLength)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 free_page((unsigned long)tbuf);
691 return -EINVAL;
692 }
Alan Sternfe0410c2005-07-29 12:16:58 -0700693 snoop(&dev->dev, "control read: bRequest=%02x "
694 "bRrequestType=%02x wValue=%04x "
695 "wIndex=%04x wLength=%04x\n",
696 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
697 ctrl.wIndex, ctrl.wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 usb_unlock_device(dev);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800700 i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest,
701 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
702 tbuf, ctrl.wLength, tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 usb_lock_device(dev);
704 if ((i > 0) && ctrl.wLength) {
705 if (usbfs_snoop) {
706 dev_info(&dev->dev, "control read: data ");
Alan Sternfe0410c2005-07-29 12:16:58 -0700707 for (j = 0; j < i; ++j)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800708 printk("%02x ", (u8)(tbuf)[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 printk("\n");
710 }
Alan Sternfe0410c2005-07-29 12:16:58 -0700711 if (copy_to_user(ctrl.data, tbuf, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 free_page((unsigned long)tbuf);
713 return -EFAULT;
714 }
715 }
716 } else {
717 if (ctrl.wLength) {
718 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
719 free_page((unsigned long)tbuf);
720 return -EFAULT;
721 }
722 }
Alan Sternfe0410c2005-07-29 12:16:58 -0700723 snoop(&dev->dev, "control write: bRequest=%02x "
724 "bRrequestType=%02x wValue=%04x "
725 "wIndex=%04x wLength=%04x\n",
726 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
727 ctrl.wIndex, ctrl.wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (usbfs_snoop) {
729 dev_info(&dev->dev, "control write: data: ");
730 for (j = 0; j < ctrl.wLength; ++j)
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700731 printk("%02x ", (unsigned char)(tbuf)[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 printk("\n");
733 }
734 usb_unlock_device(dev);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800735 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
736 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
737 tbuf, ctrl.wLength, tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 usb_lock_device(dev);
739 }
740 free_page((unsigned long)tbuf);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800741 if (i < 0 && i != -EPIPE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
743 "failed cmd %s rqt %u rq %u len %u ret %d\n",
744 current->comm, ctrl.bRequestType, ctrl.bRequest,
745 ctrl.wLength, i);
746 }
747 return i;
748}
749
750static int proc_bulk(struct dev_state *ps, void __user *arg)
751{
752 struct usb_device *dev = ps->dev;
753 struct usbdevfs_bulktransfer bulk;
754 unsigned int tmo, len1, pipe;
755 int len2;
756 unsigned char *tbuf;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700757 int i, j, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 if (copy_from_user(&bulk, arg, sizeof(bulk)))
760 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800761 ret = findintfep(ps->dev, bulk.ep);
762 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800764 ret = checkintf(ps, ret);
765 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return ret;
767 if (bulk.ep & USB_DIR_IN)
768 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
769 else
770 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
771 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
772 return -EINVAL;
773 len1 = bulk.len;
774 if (len1 > MAX_USBFS_BUFFER_SIZE)
775 return -EINVAL;
776 if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
777 return -ENOMEM;
778 tmo = bulk.timeout;
779 if (bulk.ep & 0x80) {
780 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
781 kfree(tbuf);
782 return -EINVAL;
783 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700784 snoop(&dev->dev, "bulk read: len=0x%02x timeout=%04d\n",
785 bulk.len, bulk.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 usb_unlock_device(dev);
787 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
788 usb_lock_device(dev);
789 if (!i && len2) {
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700790 if (usbfs_snoop) {
791 dev_info(&dev->dev, "bulk read: data ");
792 for (j = 0; j < len2; ++j)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800793 printk("%02x ", (u8)(tbuf)[j]);
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700794 printk("\n");
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (copy_to_user(bulk.data, tbuf, len2)) {
797 kfree(tbuf);
798 return -EFAULT;
799 }
800 }
801 } else {
802 if (len1) {
803 if (copy_from_user(tbuf, bulk.data, len1)) {
804 kfree(tbuf);
805 return -EFAULT;
806 }
807 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700808 snoop(&dev->dev, "bulk write: len=0x%02x timeout=%04d\n",
809 bulk.len, bulk.timeout);
810 if (usbfs_snoop) {
811 dev_info(&dev->dev, "bulk write: data: ");
812 for (j = 0; j < len1; ++j)
813 printk("%02x ", (unsigned char)(tbuf)[j]);
814 printk("\n");
815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 usb_unlock_device(dev);
817 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
818 usb_lock_device(dev);
819 }
820 kfree(tbuf);
821 if (i < 0)
822 return i;
823 return len2;
824}
825
826static int proc_resetep(struct dev_state *ps, void __user *arg)
827{
828 unsigned int ep;
829 int ret;
830
831 if (get_user(ep, (unsigned int __user *)arg))
832 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800833 ret = findintfep(ps->dev, ep);
834 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800836 ret = checkintf(ps, ret);
837 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return ret;
839 usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0);
840 return 0;
841}
842
843static int proc_clearhalt(struct dev_state *ps, void __user *arg)
844{
845 unsigned int ep;
846 int pipe;
847 int ret;
848
849 if (get_user(ep, (unsigned int __user *)arg))
850 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800851 ret = findintfep(ps->dev, ep);
852 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800854 ret = checkintf(ps, ret);
855 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return ret;
857 if (ep & USB_DIR_IN)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800858 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
859 else
860 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 return usb_clear_halt(ps->dev, pipe);
863}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865static int proc_getdriver(struct dev_state *ps, void __user *arg)
866{
867 struct usbdevfs_getdriver gd;
868 struct usb_interface *intf;
869 int ret;
870
871 if (copy_from_user(&gd, arg, sizeof(gd)))
872 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 intf = usb_ifnum_to_if(ps->dev, gd.interface);
874 if (!intf || !intf->dev.driver)
875 ret = -ENODATA;
876 else {
877 strncpy(gd.driver, intf->dev.driver->name,
878 sizeof(gd.driver));
879 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return ret;
882}
883
884static int proc_connectinfo(struct dev_state *ps, void __user *arg)
885{
886 struct usbdevfs_connectinfo ci;
887
888 ci.devnum = ps->dev->devnum;
889 ci.slow = ps->dev->speed == USB_SPEED_LOW;
890 if (copy_to_user(arg, &ci, sizeof(ci)))
891 return -EFAULT;
892 return 0;
893}
894
895static int proc_resetdevice(struct dev_state *ps)
896{
Ming Lei742120c2008-06-18 22:00:29 +0800897 return usb_reset_device(ps->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900static int proc_setintf(struct dev_state *ps, void __user *arg)
901{
902 struct usbdevfs_setinterface setintf;
903 int ret;
904
905 if (copy_from_user(&setintf, arg, sizeof(setintf)))
906 return -EFAULT;
907 if ((ret = checkintf(ps, setintf.interface)))
908 return ret;
909 return usb_set_interface(ps->dev, setintf.interface,
910 setintf.altsetting);
911}
912
913static int proc_setconfig(struct dev_state *ps, void __user *arg)
914{
Alan Stern3f141e22007-02-08 16:40:43 -0500915 int u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 int status = 0;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800917 struct usb_host_config *actconfig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Alan Stern3f141e22007-02-08 16:40:43 -0500919 if (get_user(u, (int __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return -EFAULT;
921
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800922 actconfig = ps->dev->actconfig;
923
924 /* Don't touch the device if any interfaces are claimed.
925 * It could interfere with other drivers' operations, and if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 * an interface is claimed by usbfs it could easily deadlock.
927 */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800928 if (actconfig) {
929 int i;
930
931 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
932 if (usb_interface_claimed(actconfig->interface[i])) {
933 dev_warn(&ps->dev->dev,
David Brownell72ebddb2005-04-11 18:34:17 -0700934 "usbfs: interface %d claimed by %s "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 "while '%s' sets config #%d\n",
936 actconfig->interface[i]
937 ->cur_altsetting
938 ->desc.bInterfaceNumber,
David Brownell72ebddb2005-04-11 18:34:17 -0700939 actconfig->interface[i]
940 ->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 current->comm, u);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800942 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800945 }
946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
949 * so avoid usb_set_configuration()'s kick to sysfs
950 */
951 if (status == 0) {
952 if (actconfig && actconfig->desc.bConfigurationValue == u)
953 status = usb_reset_configuration(ps->dev);
954 else
955 status = usb_set_configuration(ps->dev, u);
956 }
957
958 return status;
959}
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800962 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
963 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 struct usbdevfs_iso_packet_desc *isopkt = NULL;
966 struct usb_host_endpoint *ep;
967 struct async *as;
968 struct usb_ctrlrequest *dr = NULL;
969 unsigned int u, totlen, isofrmlen;
Alan Stern97b9eb92007-02-26 14:56:14 -0500970 int ret, ifnum = -1;
Alan Stern93cf9b92007-07-30 17:09:28 -0400971 int is_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Alan Stern14722ef2008-04-17 10:18:11 -0400973 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
974 USBDEVFS_URB_SHORT_NOT_OK |
975 USBDEVFS_URB_NO_FSBR |
976 USBDEVFS_URB_ZERO_PACKET |
977 USBDEVFS_URB_NO_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return -EINVAL;
979 if (!uurb->buffer)
980 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800981 if (uurb->signr != 0 && (uurb->signr < SIGRTMIN ||
982 uurb->signr > SIGRTMAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800984 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
985 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
986 ifnum = findintfep(ps->dev, uurb->endpoint);
987 if (ifnum < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return ifnum;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800989 ret = checkintf(ps, ifnum);
990 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return ret;
992 }
Alan Stern93cf9b92007-07-30 17:09:28 -0400993 if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
994 is_in = 1;
995 ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
996 } else {
997 is_in = 0;
998 ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (!ep)
1001 return -ENOENT;
1002 switch(uurb->type) {
1003 case USBDEVFS_URB_TYPE_CONTROL:
Alan Stern93cf9b92007-07-30 17:09:28 -04001004 if (!usb_endpoint_xfer_control(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001006 /* min 8 byte setup packet,
1007 * max 8 byte setup plus an arbitrary data stage */
1008 if (uurb->buffer_length < 8 ||
1009 uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001011 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1012 if (!dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return -ENOMEM;
1014 if (copy_from_user(dr, uurb->buffer, 8)) {
1015 kfree(dr);
1016 return -EFAULT;
1017 }
1018 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
1019 kfree(dr);
1020 return -EINVAL;
1021 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001022 ret = check_ctrlrecip(ps, dr->bRequestType,
1023 le16_to_cpup(&dr->wIndex));
1024 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 kfree(dr);
1026 return ret;
1027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 uurb->number_of_packets = 0;
1029 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1030 uurb->buffer += 8;
Alan Stern93cf9b92007-07-30 17:09:28 -04001031 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1032 is_in = 1;
1033 uurb->endpoint |= USB_DIR_IN;
1034 } else {
1035 is_in = 0;
1036 uurb->endpoint &= ~USB_DIR_IN;
1037 }
1038 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1039 uurb->buffer, uurb->buffer_length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 kfree(dr);
1041 return -EFAULT;
1042 }
Chris Freydf251b82006-12-16 02:37:42 -05001043 snoop(&ps->dev->dev, "control urb: bRequest=%02x "
1044 "bRrequestType=%02x wValue=%04x "
1045 "wIndex=%04x wLength=%04x\n",
Alan Stern93cf9b92007-07-30 17:09:28 -04001046 dr->bRequest, dr->bRequestType,
1047 __le16_to_cpup(&dr->wValue),
1048 __le16_to_cpup(&dr->wIndex),
1049 __le16_to_cpup(&dr->wLength));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 break;
1051
1052 case USBDEVFS_URB_TYPE_BULK:
Alan Stern93cf9b92007-07-30 17:09:28 -04001053 switch (usb_endpoint_type(&ep->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 case USB_ENDPOINT_XFER_CONTROL:
1055 case USB_ENDPOINT_XFER_ISOC:
1056 return -EINVAL;
1057 /* allow single-shot interrupt transfers, at bogus rates */
1058 }
1059 uurb->number_of_packets = 0;
1060 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1061 return -EINVAL;
Alan Stern93cf9b92007-07-30 17:09:28 -04001062 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1063 uurb->buffer, uurb->buffer_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return -EFAULT;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -07001065 snoop(&ps->dev->dev, "bulk urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 break;
1067
1068 case USBDEVFS_URB_TYPE_ISO:
1069 /* arbitrary limit */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001070 if (uurb->number_of_packets < 1 ||
1071 uurb->number_of_packets > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return -EINVAL;
Alan Stern93cf9b92007-07-30 17:09:28 -04001073 if (!usb_endpoint_xfer_isoc(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001075 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1076 uurb->number_of_packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
1078 return -ENOMEM;
1079 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
1080 kfree(isopkt);
1081 return -EFAULT;
1082 }
1083 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001084 /* arbitrary limit,
1085 * sufficient for USB 2.0 high-bandwidth iso */
Micah Dowty36122422006-05-19 11:26:24 -07001086 if (isopkt[u].length > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 kfree(isopkt);
1088 return -EINVAL;
1089 }
1090 totlen += isopkt[u].length;
1091 }
1092 if (totlen > 32768) {
1093 kfree(isopkt);
1094 return -EINVAL;
1095 }
1096 uurb->buffer_length = totlen;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -07001097 snoop(&ps->dev->dev, "iso urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 break;
1099
1100 case USBDEVFS_URB_TYPE_INTERRUPT:
1101 uurb->number_of_packets = 0;
Alan Stern93cf9b92007-07-30 17:09:28 -04001102 if (!usb_endpoint_xfer_int(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1105 return -EINVAL;
Alan Stern93cf9b92007-07-30 17:09:28 -04001106 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1107 uurb->buffer, uurb->buffer_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return -EFAULT;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -07001109 snoop(&ps->dev->dev, "interrupt urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 break;
1111
1112 default:
1113 return -EINVAL;
1114 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001115 as = alloc_async(uurb->number_of_packets);
1116 if (!as) {
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001117 kfree(isopkt);
1118 kfree(dr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return -ENOMEM;
1120 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001121 as->urb->transfer_buffer = kmalloc(uurb->buffer_length, GFP_KERNEL);
1122 if (!as->urb->transfer_buffer) {
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001123 kfree(isopkt);
1124 kfree(dr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 free_async(as);
1126 return -ENOMEM;
1127 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001128 as->urb->dev = ps->dev;
1129 as->urb->pipe = (uurb->type << 30) |
Alan Stern93cf9b92007-07-30 17:09:28 -04001130 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1131 (uurb->endpoint & USB_DIR_IN);
Alan Stern14722ef2008-04-17 10:18:11 -04001132
1133 /* This tedious sequence is necessary because the URB_* flags
1134 * are internal to the kernel and subject to change, whereas
1135 * the USBDEVFS_URB_* flags are a user API and must not be changed.
1136 */
1137 u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
1138 if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
1139 u |= URB_ISO_ASAP;
1140 if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
1141 u |= URB_SHORT_NOT_OK;
1142 if (uurb->flags & USBDEVFS_URB_NO_FSBR)
1143 u |= URB_NO_FSBR;
1144 if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1145 u |= URB_ZERO_PACKET;
1146 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
1147 u |= URB_NO_INTERRUPT;
1148 as->urb->transfer_flags = u;
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 as->urb->transfer_buffer_length = uurb->buffer_length;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001151 as->urb->setup_packet = (unsigned char *)dr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 as->urb->start_frame = uurb->start_frame;
1153 as->urb->number_of_packets = uurb->number_of_packets;
Alan Stern97b9eb92007-02-26 14:56:14 -05001154 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1155 ps->dev->speed == USB_SPEED_HIGH)
1156 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
1157 else
1158 as->urb->interval = ep->desc.bInterval;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001159 as->urb->context = as;
1160 as->urb->complete = async_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1162 as->urb->iso_frame_desc[u].offset = totlen;
1163 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1164 totlen += isopkt[u].length;
1165 }
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001166 kfree(isopkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 as->ps = ps;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001168 as->userurb = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (uurb->endpoint & USB_DIR_IN)
1170 as->userbuffer = uurb->buffer;
1171 else
1172 as->userbuffer = NULL;
1173 as->signr = uurb->signr;
1174 as->ifnum = ifnum;
Eric W. Biederman2425c082006-10-02 02:17:28 -07001175 as->pid = get_pid(task_pid(current));
Harald Welte46113832005-10-10 19:44:29 +02001176 as->uid = current->uid;
1177 as->euid = current->euid;
David Quigley7a019552006-06-30 01:55:48 -07001178 security_task_getsecid(current, &as->secid);
Alan Stern93cf9b92007-07-30 17:09:28 -04001179 if (!is_in) {
1180 if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
1181 as->urb->transfer_buffer_length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 free_async(as);
1183 return -EFAULT;
1184 }
1185 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -07001186 snoop_urb(as->urb, as->userurb);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001187 async_newpending(as);
1188 if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
1189 dev_printk(KERN_DEBUG, &ps->dev->dev,
1190 "usbfs: usb_submit_urb returned %d\n", ret);
1191 async_removepending(as);
1192 free_async(as);
1193 return ret;
1194 }
1195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
1198static int proc_submiturb(struct dev_state *ps, void __user *arg)
1199{
1200 struct usbdevfs_urb uurb;
1201
1202 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1203 return -EFAULT;
1204
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001205 return proc_do_submiturb(ps, &uurb,
1206 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1207 arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
1211{
1212 struct async *as;
1213
1214 as = async_getpending(ps, arg);
1215 if (!as)
1216 return -EINVAL;
1217 usb_kill_urb(as->urb);
1218 return 0;
1219}
1220
1221static int processcompl(struct async *as, void __user * __user *arg)
1222{
1223 struct urb *urb = as->urb;
1224 struct usbdevfs_urb __user *userurb = as->userurb;
1225 void __user *addr = as->userurb;
1226 unsigned int i;
1227
1228 if (as->userbuffer)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001229 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1230 urb->transfer_buffer_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return -EFAULT;
Alan Sterne0152682007-08-24 15:42:52 -04001232 if (put_user(as->status, &userurb->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return -EFAULT;
1234 if (put_user(urb->actual_length, &userurb->actual_length))
1235 return -EFAULT;
1236 if (put_user(urb->error_count, &userurb->error_count))
1237 return -EFAULT;
1238
Alan Stern93cf9b92007-07-30 17:09:28 -04001239 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
Christopher Li668a9542005-04-18 17:39:26 -07001240 for (i = 0; i < urb->number_of_packets; i++) {
1241 if (put_user(urb->iso_frame_desc[i].actual_length,
1242 &userurb->iso_frame_desc[i].actual_length))
1243 return -EFAULT;
1244 if (put_user(urb->iso_frame_desc[i].status,
1245 &userurb->iso_frame_desc[i].status))
1246 return -EFAULT;
1247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249
1250 free_async(as);
1251
1252 if (put_user(addr, (void __user * __user *)arg))
1253 return -EFAULT;
1254 return 0;
1255}
1256
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001257static struct async *reap_as(struct dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001259 DECLARE_WAITQUEUE(wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 struct async *as = NULL;
1261 struct usb_device *dev = ps->dev;
1262
1263 add_wait_queue(&ps->wait, &wait);
1264 for (;;) {
1265 __set_current_state(TASK_INTERRUPTIBLE);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001266 as = async_getcompleted(ps);
1267 if (as)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 break;
1269 if (signal_pending(current))
1270 break;
1271 usb_unlock_device(dev);
1272 schedule();
1273 usb_lock_device(dev);
1274 }
1275 remove_wait_queue(&ps->wait, &wait);
1276 set_current_state(TASK_RUNNING);
1277 return as;
1278}
1279
1280static int proc_reapurb(struct dev_state *ps, void __user *arg)
1281{
1282 struct async *as = reap_as(ps);
1283 if (as)
1284 return processcompl(as, (void __user * __user *)arg);
1285 if (signal_pending(current))
1286 return -EINTR;
1287 return -EIO;
1288}
1289
1290static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
1291{
1292 struct async *as;
1293
1294 if (!(as = async_getcompleted(ps)))
1295 return -EAGAIN;
1296 return processcompl(as, (void __user * __user *)arg);
1297}
1298
1299#ifdef CONFIG_COMPAT
1300
1301static int get_urb32(struct usbdevfs_urb *kurb,
1302 struct usbdevfs_urb32 __user *uurb)
1303{
1304 __u32 uptr;
1305 if (get_user(kurb->type, &uurb->type) ||
1306 __get_user(kurb->endpoint, &uurb->endpoint) ||
1307 __get_user(kurb->status, &uurb->status) ||
1308 __get_user(kurb->flags, &uurb->flags) ||
1309 __get_user(kurb->buffer_length, &uurb->buffer_length) ||
1310 __get_user(kurb->actual_length, &uurb->actual_length) ||
1311 __get_user(kurb->start_frame, &uurb->start_frame) ||
1312 __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
1313 __get_user(kurb->error_count, &uurb->error_count) ||
1314 __get_user(kurb->signr, &uurb->signr))
1315 return -EFAULT;
1316
1317 if (__get_user(uptr, &uurb->buffer))
1318 return -EFAULT;
1319 kurb->buffer = compat_ptr(uptr);
1320 if (__get_user(uptr, &uurb->buffer))
1321 return -EFAULT;
1322 kurb->usercontext = compat_ptr(uptr);
1323
1324 return 0;
1325}
1326
1327static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
1328{
1329 struct usbdevfs_urb uurb;
1330
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001331 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return -EFAULT;
1333
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001334 return proc_do_submiturb(ps, &uurb,
1335 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
1336 arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
1339static int processcompl_compat(struct async *as, void __user * __user *arg)
1340{
1341 struct urb *urb = as->urb;
1342 struct usbdevfs_urb32 __user *userurb = as->userurb;
1343 void __user *addr = as->userurb;
1344 unsigned int i;
1345
1346 if (as->userbuffer)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001347 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1348 urb->transfer_buffer_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return -EFAULT;
Alan Sterne0152682007-08-24 15:42:52 -04001350 if (put_user(as->status, &userurb->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return -EFAULT;
1352 if (put_user(urb->actual_length, &userurb->actual_length))
1353 return -EFAULT;
1354 if (put_user(urb->error_count, &userurb->error_count))
1355 return -EFAULT;
1356
Alan Stern93cf9b92007-07-30 17:09:28 -04001357 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
Christopher Li668a9542005-04-18 17:39:26 -07001358 for (i = 0; i < urb->number_of_packets; i++) {
1359 if (put_user(urb->iso_frame_desc[i].actual_length,
1360 &userurb->iso_frame_desc[i].actual_length))
1361 return -EFAULT;
1362 if (put_user(urb->iso_frame_desc[i].status,
1363 &userurb->iso_frame_desc[i].status))
1364 return -EFAULT;
1365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367
1368 free_async(as);
Al Viroc714de52006-10-10 22:45:37 +01001369 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return -EFAULT;
1371 return 0;
1372}
1373
1374static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
1375{
1376 struct async *as = reap_as(ps);
1377 if (as)
1378 return processcompl_compat(as, (void __user * __user *)arg);
1379 if (signal_pending(current))
1380 return -EINTR;
1381 return -EIO;
1382}
1383
1384static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
1385{
1386 struct async *as;
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (!(as = async_getcompleted(ps)))
1389 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return processcompl_compat(as, (void __user * __user *)arg);
1391}
1392
1393#endif
1394
1395static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
1396{
1397 struct usbdevfs_disconnectsignal ds;
1398
1399 if (copy_from_user(&ds, arg, sizeof(ds)))
1400 return -EFAULT;
1401 if (ds.signr != 0 && (ds.signr < SIGRTMIN || ds.signr > SIGRTMAX))
1402 return -EINVAL;
1403 ps->discsignr = ds.signr;
1404 ps->disccontext = ds.context;
1405 return 0;
1406}
1407
1408static int proc_claiminterface(struct dev_state *ps, void __user *arg)
1409{
1410 unsigned int ifnum;
1411
1412 if (get_user(ifnum, (unsigned int __user *)arg))
1413 return -EFAULT;
1414 return claimintf(ps, ifnum);
1415}
1416
1417static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
1418{
1419 unsigned int ifnum;
1420 int ret;
1421
1422 if (get_user(ifnum, (unsigned int __user *)arg))
1423 return -EFAULT;
1424 if ((ret = releaseintf(ps, ifnum)) < 0)
1425 return ret;
1426 destroy_async_on_interface (ps, ifnum);
1427 return 0;
1428}
1429
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001430static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 int size;
1433 void *buf = NULL;
1434 int retval = 0;
1435 struct usb_interface *intf = NULL;
1436 struct usb_driver *driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001438 /* alloc buffer */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001439 if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
1440 if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return -ENOMEM;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001442 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001443 if (copy_from_user(buf, ctl->data, size)) {
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001444 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return -EFAULT;
1446 }
1447 } else {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001448 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450 }
1451
Alan Stern349710c2006-07-01 22:05:56 -04001452 if (!connected(ps)) {
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001453 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return -ENODEV;
1455 }
1456
1457 if (ps->dev->state != USB_STATE_CONFIGURED)
1458 retval = -EHOSTUNREACH;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001459 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
1460 retval = -EINVAL;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001461 else switch (ctl->ioctl_code) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 /* disconnect kernel driver from interface */
1464 case USBDEVFS_DISCONNECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (intf->dev.driver) {
1466 driver = to_usb_driver(intf->dev.driver);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001467 dev_dbg(&intf->dev, "disconnect by usbfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 usb_driver_release_interface(driver, intf);
1469 } else
1470 retval = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 break;
1472
1473 /* let kernel drivers try to (re)bind to the interface */
1474 case USBDEVFS_CONNECT:
Alan Stern885e9742007-12-03 15:42:10 -05001475 if (!intf->dev.driver)
1476 retval = device_attach(&intf->dev);
1477 else
1478 retval = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 break;
1480
1481 /* talk directly to the interface's driver */
1482 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (intf->dev.driver)
1484 driver = to_usb_driver(intf->dev.driver);
1485 if (driver == NULL || driver->ioctl == NULL) {
1486 retval = -ENOTTY;
1487 } else {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001488 retval = driver->ioctl(intf, ctl->ioctl_code, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (retval == -ENOIOCTLCMD)
1490 retval = -ENOTTY;
1491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
1493
1494 /* cleanup and return */
1495 if (retval >= 0
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001496 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 && size > 0
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001498 && copy_to_user(ctl->data, buf, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 retval = -EFAULT;
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001500
1501 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return retval;
1503}
1504
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001505static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
1506{
1507 struct usbdevfs_ioctl ctrl;
1508
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001509 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001510 return -EFAULT;
1511 return proc_ioctl(ps, &ctrl);
1512}
1513
1514#ifdef CONFIG_COMPAT
Andrew Morton777da592005-11-17 09:47:02 -08001515static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001516{
1517 struct usbdevfs_ioctl32 __user *uioc;
1518 struct usbdevfs_ioctl ctrl;
1519 u32 udata;
1520
Andrew Morton058120d2005-11-17 09:47:49 -08001521 uioc = compat_ptr((long)arg);
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001522 if (get_user(ctrl.ifno, &uioc->ifno) ||
1523 get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
1524 __get_user(udata, &uioc->data))
1525 return -EFAULT;
1526 ctrl.data = compat_ptr(udata);
1527
1528 return proc_ioctl(ps, &ctrl);
1529}
1530#endif
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532/*
1533 * NOTE: All requests here that have interface numbers as parameters
1534 * are assuming that somehow the configuration has been prevented from
1535 * changing. But there's no mechanism to ensure that...
1536 */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001537static int usbdev_ioctl(struct inode *inode, struct file *file,
1538 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Tobias Klauserec17cf12006-09-13 21:38:41 +02001540 struct dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 struct usb_device *dev = ps->dev;
1542 void __user *p = (void __user *)arg;
1543 int ret = -ENOTTY;
1544
1545 if (!(file->f_mode & FMODE_WRITE))
1546 return -EPERM;
1547 usb_lock_device(dev);
Alan Stern349710c2006-07-01 22:05:56 -04001548 if (!connected(ps)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 usb_unlock_device(dev);
1550 return -ENODEV;
1551 }
1552
1553 switch (cmd) {
1554 case USBDEVFS_CONTROL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001555 snoop(&dev->dev, "%s: CONTROL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 ret = proc_control(ps, p);
1557 if (ret >= 0)
1558 inode->i_mtime = CURRENT_TIME;
1559 break;
1560
1561 case USBDEVFS_BULK:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001562 snoop(&dev->dev, "%s: BULK\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 ret = proc_bulk(ps, p);
1564 if (ret >= 0)
1565 inode->i_mtime = CURRENT_TIME;
1566 break;
1567
1568 case USBDEVFS_RESETEP:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001569 snoop(&dev->dev, "%s: RESETEP\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 ret = proc_resetep(ps, p);
1571 if (ret >= 0)
1572 inode->i_mtime = CURRENT_TIME;
1573 break;
1574
1575 case USBDEVFS_RESET:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001576 snoop(&dev->dev, "%s: RESET\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 ret = proc_resetdevice(ps);
1578 break;
1579
1580 case USBDEVFS_CLEAR_HALT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001581 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 ret = proc_clearhalt(ps, p);
1583 if (ret >= 0)
1584 inode->i_mtime = CURRENT_TIME;
1585 break;
1586
1587 case USBDEVFS_GETDRIVER:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001588 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 ret = proc_getdriver(ps, p);
1590 break;
1591
1592 case USBDEVFS_CONNECTINFO:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001593 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 ret = proc_connectinfo(ps, p);
1595 break;
1596
1597 case USBDEVFS_SETINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001598 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 ret = proc_setintf(ps, p);
1600 break;
1601
1602 case USBDEVFS_SETCONFIGURATION:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001603 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 ret = proc_setconfig(ps, p);
1605 break;
1606
1607 case USBDEVFS_SUBMITURB:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001608 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 ret = proc_submiturb(ps, p);
1610 if (ret >= 0)
1611 inode->i_mtime = CURRENT_TIME;
1612 break;
1613
1614#ifdef CONFIG_COMPAT
1615
1616 case USBDEVFS_SUBMITURB32:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001617 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 ret = proc_submiturb_compat(ps, p);
1619 if (ret >= 0)
1620 inode->i_mtime = CURRENT_TIME;
1621 break;
1622
1623 case USBDEVFS_REAPURB32:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001624 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 ret = proc_reapurb_compat(ps, p);
1626 break;
1627
1628 case USBDEVFS_REAPURBNDELAY32:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001629 snoop(&dev->dev, "%s: REAPURBDELAY32\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 ret = proc_reapurbnonblock_compat(ps, p);
1631 break;
1632
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001633 case USBDEVFS_IOCTL32:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001634 snoop(&dev->dev, "%s: IOCTL\n", __func__);
Al Viroc714de52006-10-10 22:45:37 +01001635 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001636 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637#endif
1638
1639 case USBDEVFS_DISCARDURB:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001640 snoop(&dev->dev, "%s: DISCARDURB\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 ret = proc_unlinkurb(ps, p);
1642 break;
1643
1644 case USBDEVFS_REAPURB:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001645 snoop(&dev->dev, "%s: REAPURB\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 ret = proc_reapurb(ps, p);
1647 break;
1648
1649 case USBDEVFS_REAPURBNDELAY:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001650 snoop(&dev->dev, "%s: REAPURBDELAY\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 ret = proc_reapurbnonblock(ps, p);
1652 break;
1653
1654 case USBDEVFS_DISCSIGNAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001655 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 ret = proc_disconnectsignal(ps, p);
1657 break;
1658
1659 case USBDEVFS_CLAIMINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001660 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 ret = proc_claiminterface(ps, p);
1662 break;
1663
1664 case USBDEVFS_RELEASEINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001665 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 ret = proc_releaseinterface(ps, p);
1667 break;
1668
1669 case USBDEVFS_IOCTL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001670 snoop(&dev->dev, "%s: IOCTL\n", __func__);
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001671 ret = proc_ioctl_default(ps, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 break;
1673 }
1674 usb_unlock_device(dev);
1675 if (ret >= 0)
1676 inode->i_atime = CURRENT_TIME;
1677 return ret;
1678}
1679
1680/* No kernel lock - fine */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001681static unsigned int usbdev_poll(struct file *file,
1682 struct poll_table_struct *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Tobias Klauserec17cf12006-09-13 21:38:41 +02001684 struct dev_state *ps = file->private_data;
1685 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 poll_wait(file, &ps->wait, wait);
1688 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
1689 mask |= POLLOUT | POLLWRNORM;
Alan Stern349710c2006-07-01 22:05:56 -04001690 if (!connected(ps))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 mask |= POLLERR | POLLHUP;
1692 return mask;
1693}
1694
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001695const struct file_operations usbdev_file_operations = {
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -07001696 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 .llseek = usbdev_lseek,
1698 .read = usbdev_read,
1699 .poll = usbdev_poll,
1700 .ioctl = usbdev_ioctl,
1701 .open = usbdev_open,
1702 .release = usbdev_release,
1703};
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001704
Alan Sterncd9f0372008-06-24 14:47:04 -04001705void usb_fs_classdev_common_remove(struct usb_device *udev)
1706{
1707 struct dev_state *ps;
1708 struct siginfo sinfo;
1709
1710 while (!list_empty(&udev->filelist)) {
1711 ps = list_entry(udev->filelist.next, struct dev_state, list);
1712 destroy_all_async(ps);
1713 wake_up_all(&ps->wait);
1714 list_del_init(&ps->list);
1715 if (ps->discsignr) {
1716 sinfo.si_signo = ps->discsignr;
1717 sinfo.si_errno = EPIPE;
1718 sinfo.si_code = SI_ASYNCIO;
1719 sinfo.si_addr = ps->disccontext;
1720 kill_pid_info_as_uid(ps->discsignr, &sinfo,
1721 ps->disc_pid, ps->disc_uid,
1722 ps->disc_euid, ps->secid);
1723 }
1724 }
1725}
1726
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001727#ifdef CONFIG_USB_DEVICE_CLASS
1728static struct class *usb_classdev_class;
1729
1730static int usb_classdev_add(struct usb_device *dev)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001731{
Alan Sterne04199b2008-06-24 14:47:29 -04001732 struct device *cldev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001733
Greg Kroah-Hartmanb0b090e2008-07-21 20:03:34 -07001734 cldev = device_create(usb_classdev_class, &dev->dev, dev->dev.devt,
1735 NULL, "usbdev%d.%d", dev->bus->busnum,
1736 dev->devnum);
Alan Sterne04199b2008-06-24 14:47:29 -04001737 if (IS_ERR(cldev))
1738 return PTR_ERR(cldev);
1739 dev->usb_classdev = cldev;
Akinobu Mita27d39e22006-10-09 18:09:33 +09001740 return 0;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001741}
1742
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001743static void usb_classdev_remove(struct usb_device *dev)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001744{
Alan Sterne04199b2008-06-24 14:47:29 -04001745 if (dev->usb_classdev)
1746 device_unregister(dev->usb_classdev);
Alan Sterncd9f0372008-06-24 14:47:04 -04001747 usb_fs_classdev_common_remove(dev);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001748}
1749
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001750static int usb_classdev_notify(struct notifier_block *self,
1751 unsigned long action, void *dev)
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001752{
1753 switch (action) {
1754 case USB_DEVICE_ADD:
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001755 if (usb_classdev_add(dev))
Akinobu Mita27d39e22006-10-09 18:09:33 +09001756 return NOTIFY_BAD;
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001757 break;
1758 case USB_DEVICE_REMOVE:
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001759 usb_classdev_remove(dev);
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001760 break;
1761 }
1762 return NOTIFY_OK;
1763}
1764
1765static struct notifier_block usbdev_nb = {
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001766 .notifier_call = usb_classdev_notify,
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001767};
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001768#endif
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001769
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -07001770static struct cdev usb_device_cdev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001771
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001772int __init usb_devio_init(void)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001773{
1774 int retval;
1775
Alan Sternfad21bd2005-08-10 15:15:57 -04001776 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001777 "usb_device");
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001778 if (retval) {
1779 err("unable to register minors for usb_device");
1780 goto out;
1781 }
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001782 cdev_init(&usb_device_cdev, &usbdev_file_operations);
Alan Sternfad21bd2005-08-10 15:15:57 -04001783 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001784 if (retval) {
1785 err("unable to get usb_device major %d", USB_DEVICE_MAJOR);
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001786 goto error_cdev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001787 }
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001788#ifdef CONFIG_USB_DEVICE_CLASS
1789 usb_classdev_class = class_create(THIS_MODULE, "usb_device");
1790 if (IS_ERR(usb_classdev_class)) {
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001791 err("unable to register usb_device class");
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001792 retval = PTR_ERR(usb_classdev_class);
1793 cdev_del(&usb_device_cdev);
1794 usb_classdev_class = NULL;
1795 goto out;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001796 }
Dan Williamse105b8b2008-04-21 10:51:07 -07001797 /* devices of this class shadow the major:minor of their parent
1798 * device, so clear ->dev_kobj to prevent adding duplicate entries
1799 * to /sys/dev
1800 */
1801 usb_classdev_class->dev_kobj = NULL;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001802
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001803 usb_register_notify(&usbdev_nb);
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001804#endif
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001805out:
1806 return retval;
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001807
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001808error_cdev:
1809 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
1810 goto out;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001811}
1812
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001813void usb_devio_cleanup(void)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001814{
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001815#ifdef CONFIG_USB_DEVICE_CLASS
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001816 usb_unregister_notify(&usbdev_nb);
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001817 class_destroy(usb_classdev_class);
1818#endif
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001819 cdev_del(&usb_device_cdev);
Alan Sternfad21bd2005-08-10 15:15:57 -04001820 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001821}