blob: 46ca2af5ef1c0509ae4419a8b56d32f2e4f9e604 [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
Alan Sternd34d9722009-03-09 13:44:48 -0400107static 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
Alan Sternd34d9722009-03-09 13:44:48 -0400251static void async_newpending(struct async *as)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
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
Alan Sternd34d9722009-03-09 13:44:48 -0400261static void async_removepending(struct async *as)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
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
Alan Sternd34d9722009-03-09 13:44:48 -0400271static struct async *async_getcompleted(struct dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
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
Alan Sternd34d9722009-03-09 13:44:48 -0400286static struct async *async_getpending(struct dev_state *ps,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800287 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{
Roel Kluin71d27182009-03-13 12:19:18 +0100305 unsigned j;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700306 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);
Roel Kluin71d27182009-03-13 12:19:18 +0100314 dev_info(&urb->dev->dev, "transfer_buffer_length=%u\n",
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700315 urb->transfer_buffer_length);
Roel Kluin71d27182009-03-13 12:19:18 +0100316 dev_info(&urb->dev->dev, "actual_length=%u\n", urb->actual_length);
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700317 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;
Oliver Neukum516a1a02009-07-08 19:09:23 +0200328 struct pid *pid = NULL;
329 uid_t uid = 0;
330 uid_t euid = 0;
331 u32 secid = 0;
332 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800334 spin_lock(&ps->lock);
335 list_move_tail(&as->asynclist, &ps->async_completed);
Alan Sterne0152682007-08-24 15:42:52 -0400336 as->status = urb->status;
Oliver Neukum516a1a02009-07-08 19:09:23 +0200337 signr = as->signr;
338 if (signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 sinfo.si_signo = as->signr;
Alan Sterne0152682007-08-24 15:42:52 -0400340 sinfo.si_errno = as->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 sinfo.si_code = SI_ASYNCIO;
342 sinfo.si_addr = as->userurb;
Oliver Neukum516a1a02009-07-08 19:09:23 +0200343 pid = as->pid;
344 uid = as->uid;
345 euid = as->euid;
346 secid = as->secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700348 snoop(&urb->dev->dev, "urb complete\n");
349 snoop_urb(urb, as->userurb);
Oliver Neukum516a1a02009-07-08 19:09:23 +0200350 spin_unlock(&ps->lock);
351
352 if (signr)
353 kill_pid_info_as_uid(sinfo.si_signo, &sinfo, pid, uid,
354 euid, secid);
355
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700356 wake_up(&ps->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800359static void destroy_async(struct dev_state *ps, struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 struct async *as;
362 unsigned long flags;
363
364 spin_lock_irqsave(&ps->lock, flags);
365 while (!list_empty(list)) {
366 as = list_entry(list->next, struct async, asynclist);
367 list_del_init(&as->asynclist);
368
369 /* drop the spinlock so the completion handler can run */
370 spin_unlock_irqrestore(&ps->lock, flags);
371 usb_kill_urb(as->urb);
372 spin_lock_irqsave(&ps->lock, flags);
373 }
374 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800377static void destroy_async_on_interface(struct dev_state *ps,
378 unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 struct list_head *p, *q, hitlist;
381 unsigned long flags;
382
383 INIT_LIST_HEAD(&hitlist);
384 spin_lock_irqsave(&ps->lock, flags);
385 list_for_each_safe(p, q, &ps->async_pending)
386 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
387 list_move_tail(p, &hitlist);
388 spin_unlock_irqrestore(&ps->lock, flags);
389 destroy_async(ps, &hitlist);
390}
391
Alan Sternd34d9722009-03-09 13:44:48 -0400392static void destroy_all_async(struct dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800394 destroy_async(ps, &ps->async_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397/*
398 * interface claims are made only at the request of user level code,
399 * which can also release them (explicitly or by closing files).
400 * they're also undone when devices disconnect.
401 */
402
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800403static int driver_probe(struct usb_interface *intf,
404 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 return -ENODEV;
407}
408
409static void driver_disconnect(struct usb_interface *intf)
410{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800411 struct dev_state *ps = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
413
414 if (!ps)
415 return;
416
417 /* NOTE: this relies on usbcore having canceled and completed
418 * all pending I/O requests; 2.6 does that.
419 */
420
421 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
422 clear_bit(ifnum, &ps->ifclaimed);
423 else
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -0700424 dev_warn(&intf->dev, "interface number %u out of range\n",
425 ifnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800427 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /* force async requests to complete */
430 destroy_async_on_interface(ps, ifnum);
431}
432
Alan Stern2e2eb832007-12-04 14:35:15 -0500433/* The following routines are merely placeholders. There is no way
434 * to inform a user task about suspend or resumes.
435 */
436static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
437{
438 return 0;
439}
440
441static int driver_resume(struct usb_interface *intf)
442{
443 return 0;
444}
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446struct usb_driver usbfs_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 .name = "usbfs",
448 .probe = driver_probe,
449 .disconnect = driver_disconnect,
Alan Stern2e2eb832007-12-04 14:35:15 -0500450 .suspend = driver_suspend,
451 .resume = driver_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452};
453
454static int claimintf(struct dev_state *ps, unsigned int ifnum)
455{
456 struct usb_device *dev = ps->dev;
457 struct usb_interface *intf;
458 int err;
459
460 if (ifnum >= 8*sizeof(ps->ifclaimed))
461 return -EINVAL;
462 /* already claimed */
463 if (test_bit(ifnum, &ps->ifclaimed))
464 return 0;
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 intf = usb_ifnum_to_if(dev, ifnum);
467 if (!intf)
468 err = -ENOENT;
469 else
470 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (err == 0)
472 set_bit(ifnum, &ps->ifclaimed);
473 return err;
474}
475
476static int releaseintf(struct dev_state *ps, unsigned int ifnum)
477{
478 struct usb_device *dev;
479 struct usb_interface *intf;
480 int err;
481
482 err = -EINVAL;
483 if (ifnum >= 8*sizeof(ps->ifclaimed))
484 return err;
485 dev = ps->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 intf = usb_ifnum_to_if(dev, ifnum);
487 if (!intf)
488 err = -ENOENT;
489 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
490 usb_driver_release_interface(&usbfs_driver, intf);
491 err = 0;
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return err;
494}
495
496static int checkintf(struct dev_state *ps, unsigned int ifnum)
497{
498 if (ps->dev->state != USB_STATE_CONFIGURED)
499 return -EHOSTUNREACH;
500 if (ifnum >= 8*sizeof(ps->ifclaimed))
501 return -EINVAL;
502 if (test_bit(ifnum, &ps->ifclaimed))
503 return 0;
504 /* if not yet claimed, claim it for the driver */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800505 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
506 "interface %u before use\n", task_pid_nr(current),
507 current->comm, ifnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return claimintf(ps, ifnum);
509}
510
511static int findintfep(struct usb_device *dev, unsigned int ep)
512{
513 unsigned int i, j, e;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800514 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct usb_host_interface *alts;
516 struct usb_endpoint_descriptor *endpt;
517
518 if (ep & ~(USB_DIR_IN|0xf))
519 return -EINVAL;
520 if (!dev->actconfig)
521 return -ESRCH;
522 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
523 intf = dev->actconfig->interface[i];
524 for (j = 0; j < intf->num_altsetting; j++) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800525 alts = &intf->altsetting[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
527 endpt = &alts->endpoint[e].desc;
528 if (endpt->bEndpointAddress == ep)
529 return alts->desc.bInterfaceNumber;
530 }
531 }
532 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800533 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800536static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
537 unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 int ret = 0;
540
David Vrabel6da9c992009-02-18 14:43:47 +0000541 if (ps->dev->state != USB_STATE_UNAUTHENTICATED
542 && ps->dev->state != USB_STATE_ADDRESS
Horst Schirmeier24f8b112006-03-11 00:16:55 -0800543 && ps->dev->state != USB_STATE_CONFIGURED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return -EHOSTUNREACH;
545 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
546 return 0;
547
548 index &= 0xff;
549 switch (requesttype & USB_RECIP_MASK) {
550 case USB_RECIP_ENDPOINT:
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800551 ret = findintfep(ps->dev, index);
552 if (ret >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 ret = checkintf(ps, ret);
554 break;
555
556 case USB_RECIP_INTERFACE:
557 ret = checkintf(ps, index);
558 break;
559 }
560 return ret;
561}
562
Alan Stern61ad04a2008-06-24 14:47:12 -0400563static int match_devt(struct device *dev, void *data)
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700564{
David Howellsa80d5ff2008-07-02 12:28:55 +0100565 return dev->devt == (dev_t) (unsigned long) data;
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100566}
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700567
Alan Stern61ad04a2008-06-24 14:47:12 -0400568static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100569{
570 struct device *dev;
571
David Howellsa80d5ff2008-07-02 12:28:55 +0100572 dev = bus_find_device(&usb_bus_type, NULL,
573 (void *) (unsigned long) devt, match_devt);
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100574 if (!dev)
575 return NULL;
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100576 return container_of(dev, struct usb_device, dev);
577}
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579/*
580 * file operations
581 */
582static int usbdev_open(struct inode *inode, struct file *file)
583{
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200584 struct usb_device *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct dev_state *ps;
David Howells86a264a2008-11-14 10:39:18 +1100586 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 int ret;
588
Jonathan Corbetb5b4aa62008-05-16 14:25:20 -0600589 lock_kernel();
Alan Stern4a2a8a22006-07-01 22:05:01 -0400590 /* Protect against simultaneous removal or release */
591 mutex_lock(&usbfs_mutex);
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 ret = -ENOMEM;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800594 ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
595 if (!ps)
Alan Stern4a2a8a22006-07-01 22:05:01 -0400596 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 ret = -ENOENT;
Alan Stern61ad04a2008-06-24 14:47:12 -0400599
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100600 /* usbdev device-node */
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200601 if (imajor(inode) == USB_DEVICE_MAJOR)
Alan Stern61ad04a2008-06-24 14:47:12 -0400602 dev = usbdev_lookup_by_devt(inode->i_rdev);
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100603#ifdef CONFIG_USB_DEVICEFS
604 /* procfs file */
Alan Sternd64aac32008-06-24 14:47:19 -0400605 if (!dev) {
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700606 dev = inode->i_private;
Alan Sternd64aac32008-06-24 14:47:19 -0400607 if (dev && dev->usbfs_dentry &&
608 dev->usbfs_dentry->d_inode == inode)
609 usb_get_dev(dev);
610 else
611 dev = NULL;
612 }
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100613#endif
Alan Sternd64aac32008-06-24 14:47:19 -0400614 if (!dev || dev->state == USB_STATE_NOTATTACHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 goto out;
Alan Stern94fcda12006-11-20 11:38:46 -0500616 ret = usb_autoresume_device(dev);
Alan Stern01d883d2006-08-30 15:47:18 -0400617 if (ret)
618 goto out;
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 ret = 0;
621 ps->dev = dev;
622 ps->file = file;
623 spin_lock_init(&ps->lock);
Dan Carpenter316547f2006-12-13 00:03:38 -0800624 INIT_LIST_HEAD(&ps->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 INIT_LIST_HEAD(&ps->async_pending);
626 INIT_LIST_HEAD(&ps->async_completed);
627 init_waitqueue_head(&ps->wait);
628 ps->discsignr = 0;
Eric W. Biederman2425c082006-10-02 02:17:28 -0700629 ps->disc_pid = get_pid(task_pid(current));
David Howells86a264a2008-11-14 10:39:18 +1100630 ps->disc_uid = cred->uid;
631 ps->disc_euid = cred->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 ps->disccontext = NULL;
633 ps->ifclaimed = 0;
David Quigley7a019552006-06-30 01:55:48 -0700634 security_task_getsecid(current, &ps->secid);
Oliver Neukum527660a2007-04-20 20:50:48 +0200635 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 list_add_tail(&ps->list, &dev->filelist);
637 file->private_data = ps;
Alan Stern2da41d52008-10-06 11:24:26 -0400638 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
639 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 out:
Alan Sternd64aac32008-06-24 14:47:19 -0400641 if (ret) {
Alan Stern01d883d2006-08-30 15:47:18 -0400642 kfree(ps);
Alan Sternd64aac32008-06-24 14:47:19 -0400643 usb_put_dev(dev);
644 }
Alan Stern4a2a8a22006-07-01 22:05:01 -0400645 mutex_unlock(&usbfs_mutex);
Jonathan Corbetb5b4aa62008-05-16 14:25:20 -0600646 unlock_kernel();
Alan Stern4a2a8a22006-07-01 22:05:01 -0400647 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650static int usbdev_release(struct inode *inode, struct file *file)
651{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200652 struct dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct usb_device *dev = ps->dev;
654 unsigned int ifnum;
Alan Stern6ff10462009-03-09 13:44:02 -0400655 struct async *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 usb_lock_device(dev);
Alan Stern4a2a8a22006-07-01 22:05:01 -0400658
659 /* Protect against simultaneous open */
660 mutex_lock(&usbfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 list_del_init(&ps->list);
Alan Stern4a2a8a22006-07-01 22:05:01 -0400662 mutex_unlock(&usbfs_mutex);
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
665 ifnum++) {
666 if (test_bit(ifnum, &ps->ifclaimed))
667 releaseintf(ps, ifnum);
668 }
669 destroy_all_async(ps);
Alan Stern94fcda12006-11-20 11:38:46 -0500670 usb_autosuspend_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 usb_unlock_device(dev);
672 usb_put_dev(dev);
Eric W. Biederman2425c082006-10-02 02:17:28 -0700673 put_pid(ps->disc_pid);
Alan Stern6ff10462009-03-09 13:44:02 -0400674
675 as = async_getcompleted(ps);
676 while (as) {
677 free_async(as);
678 as = async_getcompleted(ps);
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 kfree(ps);
Alan Stern4a2a8a22006-07-01 22:05:01 -0400681 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684static int proc_control(struct dev_state *ps, void __user *arg)
685{
686 struct usb_device *dev = ps->dev;
687 struct usbdevfs_ctrltransfer ctrl;
688 unsigned int tmo;
689 unsigned char *tbuf;
Andrew Mortonff66e3c2008-03-12 13:32:24 -0700690 unsigned wLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 int i, j, ret;
692
693 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
694 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800695 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex);
696 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return ret;
Andrew Mortonff66e3c2008-03-12 13:32:24 -0700698 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
699 if (wLength > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800701 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
702 if (!tbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return -ENOMEM;
704 tmo = ctrl.timeout;
705 if (ctrl.bRequestType & 0x80) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800706 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
707 ctrl.wLength)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 free_page((unsigned long)tbuf);
709 return -EINVAL;
710 }
Alan Sternfe0410c2005-07-29 12:16:58 -0700711 snoop(&dev->dev, "control read: bRequest=%02x "
712 "bRrequestType=%02x wValue=%04x "
713 "wIndex=%04x wLength=%04x\n",
714 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
715 ctrl.wIndex, ctrl.wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 usb_unlock_device(dev);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800718 i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest,
719 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
720 tbuf, ctrl.wLength, tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 usb_lock_device(dev);
722 if ((i > 0) && ctrl.wLength) {
723 if (usbfs_snoop) {
724 dev_info(&dev->dev, "control read: data ");
Alan Sternfe0410c2005-07-29 12:16:58 -0700725 for (j = 0; j < i; ++j)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800726 printk("%02x ", (u8)(tbuf)[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 printk("\n");
728 }
Alan Sternfe0410c2005-07-29 12:16:58 -0700729 if (copy_to_user(ctrl.data, tbuf, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 free_page((unsigned long)tbuf);
731 return -EFAULT;
732 }
733 }
734 } else {
735 if (ctrl.wLength) {
736 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
737 free_page((unsigned long)tbuf);
738 return -EFAULT;
739 }
740 }
Alan Sternfe0410c2005-07-29 12:16:58 -0700741 snoop(&dev->dev, "control write: bRequest=%02x "
742 "bRrequestType=%02x wValue=%04x "
743 "wIndex=%04x wLength=%04x\n",
744 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
745 ctrl.wIndex, ctrl.wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (usbfs_snoop) {
747 dev_info(&dev->dev, "control write: data: ");
748 for (j = 0; j < ctrl.wLength; ++j)
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700749 printk("%02x ", (unsigned char)(tbuf)[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 printk("\n");
751 }
752 usb_unlock_device(dev);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800753 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
754 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
755 tbuf, ctrl.wLength, tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 usb_lock_device(dev);
757 }
758 free_page((unsigned long)tbuf);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800759 if (i < 0 && i != -EPIPE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
761 "failed cmd %s rqt %u rq %u len %u ret %d\n",
762 current->comm, ctrl.bRequestType, ctrl.bRequest,
763 ctrl.wLength, i);
764 }
765 return i;
766}
767
768static int proc_bulk(struct dev_state *ps, void __user *arg)
769{
770 struct usb_device *dev = ps->dev;
771 struct usbdevfs_bulktransfer bulk;
772 unsigned int tmo, len1, pipe;
773 int len2;
774 unsigned char *tbuf;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700775 int i, j, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 if (copy_from_user(&bulk, arg, sizeof(bulk)))
778 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800779 ret = findintfep(ps->dev, bulk.ep);
780 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800782 ret = checkintf(ps, ret);
783 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return ret;
785 if (bulk.ep & USB_DIR_IN)
786 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
787 else
788 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
789 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
790 return -EINVAL;
791 len1 = bulk.len;
792 if (len1 > MAX_USBFS_BUFFER_SIZE)
793 return -EINVAL;
794 if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
795 return -ENOMEM;
796 tmo = bulk.timeout;
797 if (bulk.ep & 0x80) {
798 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
799 kfree(tbuf);
800 return -EINVAL;
801 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700802 snoop(&dev->dev, "bulk read: len=0x%02x timeout=%04d\n",
803 bulk.len, bulk.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 usb_unlock_device(dev);
805 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
806 usb_lock_device(dev);
807 if (!i && len2) {
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700808 if (usbfs_snoop) {
809 dev_info(&dev->dev, "bulk read: data ");
810 for (j = 0; j < len2; ++j)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800811 printk("%02x ", (u8)(tbuf)[j]);
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700812 printk("\n");
813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (copy_to_user(bulk.data, tbuf, len2)) {
815 kfree(tbuf);
816 return -EFAULT;
817 }
818 }
819 } else {
820 if (len1) {
821 if (copy_from_user(tbuf, bulk.data, len1)) {
822 kfree(tbuf);
823 return -EFAULT;
824 }
825 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700826 snoop(&dev->dev, "bulk write: len=0x%02x timeout=%04d\n",
827 bulk.len, bulk.timeout);
828 if (usbfs_snoop) {
829 dev_info(&dev->dev, "bulk write: data: ");
830 for (j = 0; j < len1; ++j)
831 printk("%02x ", (unsigned char)(tbuf)[j]);
832 printk("\n");
833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 usb_unlock_device(dev);
835 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
836 usb_lock_device(dev);
837 }
838 kfree(tbuf);
839 if (i < 0)
840 return i;
841 return len2;
842}
843
844static int proc_resetep(struct dev_state *ps, void __user *arg)
845{
846 unsigned int ep;
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;
David Vrabel3444b262009-04-08 17:36:28 +0000857 usb_reset_endpoint(ps->dev, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return 0;
859}
860
861static int proc_clearhalt(struct dev_state *ps, void __user *arg)
862{
863 unsigned int ep;
864 int pipe;
865 int ret;
866
867 if (get_user(ep, (unsigned int __user *)arg))
868 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800869 ret = findintfep(ps->dev, ep);
870 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800872 ret = checkintf(ps, ret);
873 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return ret;
875 if (ep & USB_DIR_IN)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800876 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
877 else
878 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 return usb_clear_halt(ps->dev, pipe);
881}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883static int proc_getdriver(struct dev_state *ps, void __user *arg)
884{
885 struct usbdevfs_getdriver gd;
886 struct usb_interface *intf;
887 int ret;
888
889 if (copy_from_user(&gd, arg, sizeof(gd)))
890 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 intf = usb_ifnum_to_if(ps->dev, gd.interface);
892 if (!intf || !intf->dev.driver)
893 ret = -ENODATA;
894 else {
895 strncpy(gd.driver, intf->dev.driver->name,
896 sizeof(gd.driver));
897 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return ret;
900}
901
902static int proc_connectinfo(struct dev_state *ps, void __user *arg)
903{
904 struct usbdevfs_connectinfo ci;
905
906 ci.devnum = ps->dev->devnum;
907 ci.slow = ps->dev->speed == USB_SPEED_LOW;
908 if (copy_to_user(arg, &ci, sizeof(ci)))
909 return -EFAULT;
910 return 0;
911}
912
913static int proc_resetdevice(struct dev_state *ps)
914{
Ming Lei742120c2008-06-18 22:00:29 +0800915 return usb_reset_device(ps->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918static int proc_setintf(struct dev_state *ps, void __user *arg)
919{
920 struct usbdevfs_setinterface setintf;
921 int ret;
922
923 if (copy_from_user(&setintf, arg, sizeof(setintf)))
924 return -EFAULT;
925 if ((ret = checkintf(ps, setintf.interface)))
926 return ret;
927 return usb_set_interface(ps->dev, setintf.interface,
928 setintf.altsetting);
929}
930
931static int proc_setconfig(struct dev_state *ps, void __user *arg)
932{
Alan Stern3f141e22007-02-08 16:40:43 -0500933 int u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 int status = 0;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800935 struct usb_host_config *actconfig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Alan Stern3f141e22007-02-08 16:40:43 -0500937 if (get_user(u, (int __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return -EFAULT;
939
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800940 actconfig = ps->dev->actconfig;
941
942 /* Don't touch the device if any interfaces are claimed.
943 * It could interfere with other drivers' operations, and if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 * an interface is claimed by usbfs it could easily deadlock.
945 */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800946 if (actconfig) {
947 int i;
948
949 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
950 if (usb_interface_claimed(actconfig->interface[i])) {
951 dev_warn(&ps->dev->dev,
David Brownell72ebddb2005-04-11 18:34:17 -0700952 "usbfs: interface %d claimed by %s "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 "while '%s' sets config #%d\n",
954 actconfig->interface[i]
955 ->cur_altsetting
956 ->desc.bInterfaceNumber,
David Brownell72ebddb2005-04-11 18:34:17 -0700957 actconfig->interface[i]
958 ->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 current->comm, u);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800960 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800963 }
964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
967 * so avoid usb_set_configuration()'s kick to sysfs
968 */
969 if (status == 0) {
970 if (actconfig && actconfig->desc.bConfigurationValue == u)
971 status = usb_reset_configuration(ps->dev);
972 else
973 status = usb_set_configuration(ps->dev, u);
974 }
975
976 return status;
977}
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800980 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
981 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
983 struct usbdevfs_iso_packet_desc *isopkt = NULL;
984 struct usb_host_endpoint *ep;
985 struct async *as;
986 struct usb_ctrlrequest *dr = NULL;
David Howells86a264a2008-11-14 10:39:18 +1100987 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 unsigned int u, totlen, isofrmlen;
Alan Stern97b9eb92007-02-26 14:56:14 -0500989 int ret, ifnum = -1;
Alan Stern93cf9b92007-07-30 17:09:28 -0400990 int is_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Alan Stern14722ef2008-04-17 10:18:11 -0400992 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
993 USBDEVFS_URB_SHORT_NOT_OK |
994 USBDEVFS_URB_NO_FSBR |
995 USBDEVFS_URB_ZERO_PACKET |
996 USBDEVFS_URB_NO_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return -EINVAL;
998 if (!uurb->buffer)
999 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001000 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
1001 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
1002 ifnum = findintfep(ps->dev, uurb->endpoint);
1003 if (ifnum < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return ifnum;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001005 ret = checkintf(ps, ifnum);
1006 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return ret;
1008 }
Alan Stern93cf9b92007-07-30 17:09:28 -04001009 if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
1010 is_in = 1;
1011 ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
1012 } else {
1013 is_in = 0;
1014 ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
1015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (!ep)
1017 return -ENOENT;
1018 switch(uurb->type) {
1019 case USBDEVFS_URB_TYPE_CONTROL:
Alan Stern93cf9b92007-07-30 17:09:28 -04001020 if (!usb_endpoint_xfer_control(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001022 /* min 8 byte setup packet,
1023 * max 8 byte setup plus an arbitrary data stage */
1024 if (uurb->buffer_length < 8 ||
1025 uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001027 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1028 if (!dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return -ENOMEM;
1030 if (copy_from_user(dr, uurb->buffer, 8)) {
1031 kfree(dr);
1032 return -EFAULT;
1033 }
1034 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
1035 kfree(dr);
1036 return -EINVAL;
1037 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001038 ret = check_ctrlrecip(ps, dr->bRequestType,
1039 le16_to_cpup(&dr->wIndex));
1040 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 kfree(dr);
1042 return ret;
1043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 uurb->number_of_packets = 0;
1045 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1046 uurb->buffer += 8;
Alan Stern93cf9b92007-07-30 17:09:28 -04001047 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1048 is_in = 1;
1049 uurb->endpoint |= USB_DIR_IN;
1050 } else {
1051 is_in = 0;
1052 uurb->endpoint &= ~USB_DIR_IN;
1053 }
1054 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1055 uurb->buffer, uurb->buffer_length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 kfree(dr);
1057 return -EFAULT;
1058 }
Chris Freydf251b82006-12-16 02:37:42 -05001059 snoop(&ps->dev->dev, "control urb: bRequest=%02x "
1060 "bRrequestType=%02x wValue=%04x "
1061 "wIndex=%04x wLength=%04x\n",
Alan Stern93cf9b92007-07-30 17:09:28 -04001062 dr->bRequest, dr->bRequestType,
1063 __le16_to_cpup(&dr->wValue),
1064 __le16_to_cpup(&dr->wIndex),
1065 __le16_to_cpup(&dr->wLength));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 break;
1067
1068 case USBDEVFS_URB_TYPE_BULK:
Alan Stern93cf9b92007-07-30 17:09:28 -04001069 switch (usb_endpoint_type(&ep->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 case USB_ENDPOINT_XFER_CONTROL:
1071 case USB_ENDPOINT_XFER_ISOC:
1072 return -EINVAL;
1073 /* allow single-shot interrupt transfers, at bogus rates */
1074 }
1075 uurb->number_of_packets = 0;
1076 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1077 return -EINVAL;
Alan Stern93cf9b92007-07-30 17:09:28 -04001078 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1079 uurb->buffer, uurb->buffer_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return -EFAULT;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -07001081 snoop(&ps->dev->dev, "bulk urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 break;
1083
1084 case USBDEVFS_URB_TYPE_ISO:
1085 /* arbitrary limit */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001086 if (uurb->number_of_packets < 1 ||
1087 uurb->number_of_packets > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return -EINVAL;
Alan Stern93cf9b92007-07-30 17:09:28 -04001089 if (!usb_endpoint_xfer_isoc(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001091 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1092 uurb->number_of_packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
1094 return -ENOMEM;
1095 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
1096 kfree(isopkt);
1097 return -EFAULT;
1098 }
1099 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001100 /* arbitrary limit,
1101 * sufficient for USB 2.0 high-bandwidth iso */
Micah Dowty36122422006-05-19 11:26:24 -07001102 if (isopkt[u].length > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 kfree(isopkt);
1104 return -EINVAL;
1105 }
1106 totlen += isopkt[u].length;
1107 }
1108 if (totlen > 32768) {
1109 kfree(isopkt);
1110 return -EINVAL;
1111 }
1112 uurb->buffer_length = totlen;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -07001113 snoop(&ps->dev->dev, "iso urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 break;
1115
1116 case USBDEVFS_URB_TYPE_INTERRUPT:
1117 uurb->number_of_packets = 0;
Alan Stern93cf9b92007-07-30 17:09:28 -04001118 if (!usb_endpoint_xfer_int(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1121 return -EINVAL;
Alan Stern93cf9b92007-07-30 17:09:28 -04001122 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1123 uurb->buffer, uurb->buffer_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return -EFAULT;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -07001125 snoop(&ps->dev->dev, "interrupt urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 break;
1127
1128 default:
1129 return -EINVAL;
1130 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001131 as = alloc_async(uurb->number_of_packets);
1132 if (!as) {
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001133 kfree(isopkt);
1134 kfree(dr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return -ENOMEM;
1136 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001137 as->urb->transfer_buffer = kmalloc(uurb->buffer_length, GFP_KERNEL);
1138 if (!as->urb->transfer_buffer) {
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001139 kfree(isopkt);
1140 kfree(dr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 free_async(as);
1142 return -ENOMEM;
1143 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001144 as->urb->dev = ps->dev;
1145 as->urb->pipe = (uurb->type << 30) |
Alan Stern93cf9b92007-07-30 17:09:28 -04001146 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1147 (uurb->endpoint & USB_DIR_IN);
Alan Stern14722ef2008-04-17 10:18:11 -04001148
1149 /* This tedious sequence is necessary because the URB_* flags
1150 * are internal to the kernel and subject to change, whereas
1151 * the USBDEVFS_URB_* flags are a user API and must not be changed.
1152 */
1153 u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
1154 if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
1155 u |= URB_ISO_ASAP;
1156 if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
1157 u |= URB_SHORT_NOT_OK;
1158 if (uurb->flags & USBDEVFS_URB_NO_FSBR)
1159 u |= URB_NO_FSBR;
1160 if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1161 u |= URB_ZERO_PACKET;
1162 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
1163 u |= URB_NO_INTERRUPT;
1164 as->urb->transfer_flags = u;
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 as->urb->transfer_buffer_length = uurb->buffer_length;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001167 as->urb->setup_packet = (unsigned char *)dr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 as->urb->start_frame = uurb->start_frame;
1169 as->urb->number_of_packets = uurb->number_of_packets;
Alan Stern97b9eb92007-02-26 14:56:14 -05001170 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1171 ps->dev->speed == USB_SPEED_HIGH)
1172 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
1173 else
1174 as->urb->interval = ep->desc.bInterval;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001175 as->urb->context = as;
1176 as->urb->complete = async_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1178 as->urb->iso_frame_desc[u].offset = totlen;
1179 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1180 totlen += isopkt[u].length;
1181 }
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001182 kfree(isopkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 as->ps = ps;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001184 as->userurb = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (uurb->endpoint & USB_DIR_IN)
1186 as->userbuffer = uurb->buffer;
1187 else
1188 as->userbuffer = NULL;
1189 as->signr = uurb->signr;
1190 as->ifnum = ifnum;
Eric W. Biederman2425c082006-10-02 02:17:28 -07001191 as->pid = get_pid(task_pid(current));
David Howells86a264a2008-11-14 10:39:18 +11001192 as->uid = cred->uid;
1193 as->euid = cred->euid;
David Quigley7a019552006-06-30 01:55:48 -07001194 security_task_getsecid(current, &as->secid);
Alan Stern93cf9b92007-07-30 17:09:28 -04001195 if (!is_in) {
1196 if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
1197 as->urb->transfer_buffer_length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 free_async(as);
1199 return -EFAULT;
1200 }
1201 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -07001202 snoop_urb(as->urb, as->userurb);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001203 async_newpending(as);
1204 if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
1205 dev_printk(KERN_DEBUG, &ps->dev->dev,
1206 "usbfs: usb_submit_urb returned %d\n", ret);
1207 async_removepending(as);
1208 free_async(as);
1209 return ret;
1210 }
1211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
1214static int proc_submiturb(struct dev_state *ps, void __user *arg)
1215{
1216 struct usbdevfs_urb uurb;
1217
1218 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1219 return -EFAULT;
1220
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001221 return proc_do_submiturb(ps, &uurb,
1222 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1223 arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
1227{
1228 struct async *as;
1229
1230 as = async_getpending(ps, arg);
1231 if (!as)
1232 return -EINVAL;
1233 usb_kill_urb(as->urb);
1234 return 0;
1235}
1236
1237static int processcompl(struct async *as, void __user * __user *arg)
1238{
1239 struct urb *urb = as->urb;
1240 struct usbdevfs_urb __user *userurb = as->userurb;
1241 void __user *addr = as->userurb;
1242 unsigned int i;
1243
1244 if (as->userbuffer)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001245 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1246 urb->transfer_buffer_length))
Oliver Neukumd794a022009-06-28 23:34:14 +02001247 goto err_out;
Alan Sterne0152682007-08-24 15:42:52 -04001248 if (put_user(as->status, &userurb->status))
Oliver Neukumd794a022009-06-28 23:34:14 +02001249 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (put_user(urb->actual_length, &userurb->actual_length))
Oliver Neukumd794a022009-06-28 23:34:14 +02001251 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (put_user(urb->error_count, &userurb->error_count))
Oliver Neukumd794a022009-06-28 23:34:14 +02001253 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Alan Stern93cf9b92007-07-30 17:09:28 -04001255 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
Christopher Li668a9542005-04-18 17:39:26 -07001256 for (i = 0; i < urb->number_of_packets; i++) {
1257 if (put_user(urb->iso_frame_desc[i].actual_length,
1258 &userurb->iso_frame_desc[i].actual_length))
Oliver Neukumd794a022009-06-28 23:34:14 +02001259 goto err_out;
Christopher Li668a9542005-04-18 17:39:26 -07001260 if (put_user(urb->iso_frame_desc[i].status,
1261 &userurb->iso_frame_desc[i].status))
Oliver Neukumd794a022009-06-28 23:34:14 +02001262 goto err_out;
Christopher Li668a9542005-04-18 17:39:26 -07001263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265
1266 free_async(as);
1267
1268 if (put_user(addr, (void __user * __user *)arg))
1269 return -EFAULT;
1270 return 0;
Oliver Neukumd794a022009-06-28 23:34:14 +02001271
1272err_out:
1273 free_async(as);
1274 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001277static struct async *reap_as(struct dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001279 DECLARE_WAITQUEUE(wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 struct async *as = NULL;
1281 struct usb_device *dev = ps->dev;
1282
1283 add_wait_queue(&ps->wait, &wait);
1284 for (;;) {
1285 __set_current_state(TASK_INTERRUPTIBLE);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001286 as = async_getcompleted(ps);
1287 if (as)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 break;
1289 if (signal_pending(current))
1290 break;
1291 usb_unlock_device(dev);
1292 schedule();
1293 usb_lock_device(dev);
1294 }
1295 remove_wait_queue(&ps->wait, &wait);
1296 set_current_state(TASK_RUNNING);
1297 return as;
1298}
1299
1300static int proc_reapurb(struct dev_state *ps, void __user *arg)
1301{
1302 struct async *as = reap_as(ps);
1303 if (as)
1304 return processcompl(as, (void __user * __user *)arg);
1305 if (signal_pending(current))
1306 return -EINTR;
1307 return -EIO;
1308}
1309
1310static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
1311{
1312 struct async *as;
1313
1314 if (!(as = async_getcompleted(ps)))
1315 return -EAGAIN;
1316 return processcompl(as, (void __user * __user *)arg);
1317}
1318
1319#ifdef CONFIG_COMPAT
1320
1321static int get_urb32(struct usbdevfs_urb *kurb,
1322 struct usbdevfs_urb32 __user *uurb)
1323{
1324 __u32 uptr;
1325 if (get_user(kurb->type, &uurb->type) ||
1326 __get_user(kurb->endpoint, &uurb->endpoint) ||
1327 __get_user(kurb->status, &uurb->status) ||
1328 __get_user(kurb->flags, &uurb->flags) ||
1329 __get_user(kurb->buffer_length, &uurb->buffer_length) ||
1330 __get_user(kurb->actual_length, &uurb->actual_length) ||
1331 __get_user(kurb->start_frame, &uurb->start_frame) ||
1332 __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
1333 __get_user(kurb->error_count, &uurb->error_count) ||
1334 __get_user(kurb->signr, &uurb->signr))
1335 return -EFAULT;
1336
1337 if (__get_user(uptr, &uurb->buffer))
1338 return -EFAULT;
1339 kurb->buffer = compat_ptr(uptr);
Mark Lorded0c7722009-01-02 02:48:24 -05001340 if (__get_user(uptr, &uurb->usercontext))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return -EFAULT;
1342 kurb->usercontext = compat_ptr(uptr);
1343
1344 return 0;
1345}
1346
1347static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
1348{
1349 struct usbdevfs_urb uurb;
1350
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001351 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return -EFAULT;
1353
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001354 return proc_do_submiturb(ps, &uurb,
1355 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
1356 arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
1359static int processcompl_compat(struct async *as, void __user * __user *arg)
1360{
1361 struct urb *urb = as->urb;
1362 struct usbdevfs_urb32 __user *userurb = as->userurb;
1363 void __user *addr = as->userurb;
1364 unsigned int i;
1365
1366 if (as->userbuffer)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001367 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1368 urb->transfer_buffer_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return -EFAULT;
Alan Sterne0152682007-08-24 15:42:52 -04001370 if (put_user(as->status, &userurb->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return -EFAULT;
1372 if (put_user(urb->actual_length, &userurb->actual_length))
1373 return -EFAULT;
1374 if (put_user(urb->error_count, &userurb->error_count))
1375 return -EFAULT;
1376
Alan Stern93cf9b92007-07-30 17:09:28 -04001377 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
Christopher Li668a9542005-04-18 17:39:26 -07001378 for (i = 0; i < urb->number_of_packets; i++) {
1379 if (put_user(urb->iso_frame_desc[i].actual_length,
1380 &userurb->iso_frame_desc[i].actual_length))
1381 return -EFAULT;
1382 if (put_user(urb->iso_frame_desc[i].status,
1383 &userurb->iso_frame_desc[i].status))
1384 return -EFAULT;
1385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387
1388 free_async(as);
Al Viroc714de52006-10-10 22:45:37 +01001389 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return -EFAULT;
1391 return 0;
1392}
1393
1394static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
1395{
1396 struct async *as = reap_as(ps);
1397 if (as)
1398 return processcompl_compat(as, (void __user * __user *)arg);
1399 if (signal_pending(current))
1400 return -EINTR;
1401 return -EIO;
1402}
1403
1404static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
1405{
1406 struct async *as;
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (!(as = async_getcompleted(ps)))
1409 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return processcompl_compat(as, (void __user * __user *)arg);
1411}
1412
1413#endif
1414
1415static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
1416{
1417 struct usbdevfs_disconnectsignal ds;
1418
1419 if (copy_from_user(&ds, arg, sizeof(ds)))
1420 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 ps->discsignr = ds.signr;
1422 ps->disccontext = ds.context;
1423 return 0;
1424}
1425
1426static int proc_claiminterface(struct dev_state *ps, void __user *arg)
1427{
1428 unsigned int ifnum;
1429
1430 if (get_user(ifnum, (unsigned int __user *)arg))
1431 return -EFAULT;
1432 return claimintf(ps, ifnum);
1433}
1434
1435static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
1436{
1437 unsigned int ifnum;
1438 int ret;
1439
1440 if (get_user(ifnum, (unsigned int __user *)arg))
1441 return -EFAULT;
1442 if ((ret = releaseintf(ps, ifnum)) < 0)
1443 return ret;
1444 destroy_async_on_interface (ps, ifnum);
1445 return 0;
1446}
1447
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001448static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 int size;
1451 void *buf = NULL;
1452 int retval = 0;
1453 struct usb_interface *intf = NULL;
1454 struct usb_driver *driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001456 /* alloc buffer */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001457 if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
1458 if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return -ENOMEM;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001460 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001461 if (copy_from_user(buf, ctl->data, size)) {
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001462 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return -EFAULT;
1464 }
1465 } else {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001466 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
1468 }
1469
Alan Stern349710c2006-07-01 22:05:56 -04001470 if (!connected(ps)) {
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001471 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return -ENODEV;
1473 }
1474
1475 if (ps->dev->state != USB_STATE_CONFIGURED)
1476 retval = -EHOSTUNREACH;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001477 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
1478 retval = -EINVAL;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001479 else switch (ctl->ioctl_code) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 /* disconnect kernel driver from interface */
1482 case USBDEVFS_DISCONNECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (intf->dev.driver) {
1484 driver = to_usb_driver(intf->dev.driver);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001485 dev_dbg(&intf->dev, "disconnect by usbfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 usb_driver_release_interface(driver, intf);
1487 } else
1488 retval = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 break;
1490
1491 /* let kernel drivers try to (re)bind to the interface */
1492 case USBDEVFS_CONNECT:
Alan Stern885e9742007-12-03 15:42:10 -05001493 if (!intf->dev.driver)
1494 retval = device_attach(&intf->dev);
1495 else
1496 retval = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 break;
1498
1499 /* talk directly to the interface's driver */
1500 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (intf->dev.driver)
1502 driver = to_usb_driver(intf->dev.driver);
1503 if (driver == NULL || driver->ioctl == NULL) {
1504 retval = -ENOTTY;
1505 } else {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001506 retval = driver->ioctl(intf, ctl->ioctl_code, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (retval == -ENOIOCTLCMD)
1508 retval = -ENOTTY;
1509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511
1512 /* cleanup and return */
1513 if (retval >= 0
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001514 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 && size > 0
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001516 && copy_to_user(ctl->data, buf, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 retval = -EFAULT;
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001518
1519 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return retval;
1521}
1522
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001523static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
1524{
1525 struct usbdevfs_ioctl ctrl;
1526
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001527 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001528 return -EFAULT;
1529 return proc_ioctl(ps, &ctrl);
1530}
1531
1532#ifdef CONFIG_COMPAT
Andrew Morton777da592005-11-17 09:47:02 -08001533static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001534{
1535 struct usbdevfs_ioctl32 __user *uioc;
1536 struct usbdevfs_ioctl ctrl;
1537 u32 udata;
1538
Andrew Morton058120d2005-11-17 09:47:49 -08001539 uioc = compat_ptr((long)arg);
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001540 if (get_user(ctrl.ifno, &uioc->ifno) ||
1541 get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
1542 __get_user(udata, &uioc->data))
1543 return -EFAULT;
1544 ctrl.data = compat_ptr(udata);
1545
1546 return proc_ioctl(ps, &ctrl);
1547}
1548#endif
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550/*
1551 * NOTE: All requests here that have interface numbers as parameters
1552 * are assuming that somehow the configuration has been prevented from
1553 * changing. But there's no mechanism to ensure that...
1554 */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001555static int usbdev_ioctl(struct inode *inode, struct file *file,
1556 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Tobias Klauserec17cf12006-09-13 21:38:41 +02001558 struct dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 struct usb_device *dev = ps->dev;
1560 void __user *p = (void __user *)arg;
1561 int ret = -ENOTTY;
1562
1563 if (!(file->f_mode & FMODE_WRITE))
1564 return -EPERM;
1565 usb_lock_device(dev);
Alan Stern349710c2006-07-01 22:05:56 -04001566 if (!connected(ps)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 usb_unlock_device(dev);
1568 return -ENODEV;
1569 }
1570
1571 switch (cmd) {
1572 case USBDEVFS_CONTROL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001573 snoop(&dev->dev, "%s: CONTROL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 ret = proc_control(ps, p);
1575 if (ret >= 0)
1576 inode->i_mtime = CURRENT_TIME;
1577 break;
1578
1579 case USBDEVFS_BULK:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001580 snoop(&dev->dev, "%s: BULK\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 ret = proc_bulk(ps, p);
1582 if (ret >= 0)
1583 inode->i_mtime = CURRENT_TIME;
1584 break;
1585
1586 case USBDEVFS_RESETEP:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001587 snoop(&dev->dev, "%s: RESETEP\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 ret = proc_resetep(ps, p);
1589 if (ret >= 0)
1590 inode->i_mtime = CURRENT_TIME;
1591 break;
1592
1593 case USBDEVFS_RESET:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001594 snoop(&dev->dev, "%s: RESET\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 ret = proc_resetdevice(ps);
1596 break;
1597
1598 case USBDEVFS_CLEAR_HALT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001599 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 ret = proc_clearhalt(ps, p);
1601 if (ret >= 0)
1602 inode->i_mtime = CURRENT_TIME;
1603 break;
1604
1605 case USBDEVFS_GETDRIVER:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001606 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 ret = proc_getdriver(ps, p);
1608 break;
1609
1610 case USBDEVFS_CONNECTINFO:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001611 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 ret = proc_connectinfo(ps, p);
1613 break;
1614
1615 case USBDEVFS_SETINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001616 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 ret = proc_setintf(ps, p);
1618 break;
1619
1620 case USBDEVFS_SETCONFIGURATION:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001621 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 ret = proc_setconfig(ps, p);
1623 break;
1624
1625 case USBDEVFS_SUBMITURB:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001626 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 ret = proc_submiturb(ps, p);
1628 if (ret >= 0)
1629 inode->i_mtime = CURRENT_TIME;
1630 break;
1631
1632#ifdef CONFIG_COMPAT
1633
1634 case USBDEVFS_SUBMITURB32:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001635 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 ret = proc_submiturb_compat(ps, p);
1637 if (ret >= 0)
1638 inode->i_mtime = CURRENT_TIME;
1639 break;
1640
1641 case USBDEVFS_REAPURB32:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001642 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 ret = proc_reapurb_compat(ps, p);
1644 break;
1645
1646 case USBDEVFS_REAPURBNDELAY32:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001647 snoop(&dev->dev, "%s: REAPURBDELAY32\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 ret = proc_reapurbnonblock_compat(ps, p);
1649 break;
1650
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001651 case USBDEVFS_IOCTL32:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001652 snoop(&dev->dev, "%s: IOCTL\n", __func__);
Al Viroc714de52006-10-10 22:45:37 +01001653 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001654 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655#endif
1656
1657 case USBDEVFS_DISCARDURB:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001658 snoop(&dev->dev, "%s: DISCARDURB\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 ret = proc_unlinkurb(ps, p);
1660 break;
1661
1662 case USBDEVFS_REAPURB:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001663 snoop(&dev->dev, "%s: REAPURB\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 ret = proc_reapurb(ps, p);
1665 break;
1666
1667 case USBDEVFS_REAPURBNDELAY:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001668 snoop(&dev->dev, "%s: REAPURBDELAY\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 ret = proc_reapurbnonblock(ps, p);
1670 break;
1671
1672 case USBDEVFS_DISCSIGNAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001673 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 ret = proc_disconnectsignal(ps, p);
1675 break;
1676
1677 case USBDEVFS_CLAIMINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001678 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 ret = proc_claiminterface(ps, p);
1680 break;
1681
1682 case USBDEVFS_RELEASEINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001683 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 ret = proc_releaseinterface(ps, p);
1685 break;
1686
1687 case USBDEVFS_IOCTL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001688 snoop(&dev->dev, "%s: IOCTL\n", __func__);
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001689 ret = proc_ioctl_default(ps, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 break;
1691 }
1692 usb_unlock_device(dev);
1693 if (ret >= 0)
1694 inode->i_atime = CURRENT_TIME;
1695 return ret;
1696}
1697
1698/* No kernel lock - fine */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001699static unsigned int usbdev_poll(struct file *file,
1700 struct poll_table_struct *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Tobias Klauserec17cf12006-09-13 21:38:41 +02001702 struct dev_state *ps = file->private_data;
1703 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 poll_wait(file, &ps->wait, wait);
1706 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
1707 mask |= POLLOUT | POLLWRNORM;
Alan Stern349710c2006-07-01 22:05:56 -04001708 if (!connected(ps))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 mask |= POLLERR | POLLHUP;
1710 return mask;
1711}
1712
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001713const struct file_operations usbdev_file_operations = {
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -07001714 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 .llseek = usbdev_lseek,
1716 .read = usbdev_read,
1717 .poll = usbdev_poll,
1718 .ioctl = usbdev_ioctl,
1719 .open = usbdev_open,
1720 .release = usbdev_release,
1721};
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001722
Alan Stern501950d2009-01-13 11:33:42 -05001723static void usbdev_remove(struct usb_device *udev)
Alan Sterncd9f0372008-06-24 14:47:04 -04001724{
1725 struct dev_state *ps;
1726 struct siginfo sinfo;
1727
1728 while (!list_empty(&udev->filelist)) {
1729 ps = list_entry(udev->filelist.next, struct dev_state, list);
1730 destroy_all_async(ps);
1731 wake_up_all(&ps->wait);
1732 list_del_init(&ps->list);
1733 if (ps->discsignr) {
1734 sinfo.si_signo = ps->discsignr;
1735 sinfo.si_errno = EPIPE;
1736 sinfo.si_code = SI_ASYNCIO;
1737 sinfo.si_addr = ps->disccontext;
1738 kill_pid_info_as_uid(ps->discsignr, &sinfo,
1739 ps->disc_pid, ps->disc_uid,
1740 ps->disc_euid, ps->secid);
1741 }
1742 }
1743}
1744
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001745#ifdef CONFIG_USB_DEVICE_CLASS
1746static struct class *usb_classdev_class;
1747
1748static int usb_classdev_add(struct usb_device *dev)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001749{
Alan Sterne04199b2008-06-24 14:47:29 -04001750 struct device *cldev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001751
Greg Kroah-Hartmanb0b090e2008-07-21 20:03:34 -07001752 cldev = device_create(usb_classdev_class, &dev->dev, dev->dev.devt,
1753 NULL, "usbdev%d.%d", dev->bus->busnum,
1754 dev->devnum);
Alan Sterne04199b2008-06-24 14:47:29 -04001755 if (IS_ERR(cldev))
1756 return PTR_ERR(cldev);
1757 dev->usb_classdev = cldev;
Akinobu Mita27d39e22006-10-09 18:09:33 +09001758 return 0;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001759}
1760
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001761static void usb_classdev_remove(struct usb_device *dev)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001762{
Alan Sterne04199b2008-06-24 14:47:29 -04001763 if (dev->usb_classdev)
1764 device_unregister(dev->usb_classdev);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001765}
1766
Alan Stern501950d2009-01-13 11:33:42 -05001767#else
1768#define usb_classdev_add(dev) 0
1769#define usb_classdev_remove(dev) do {} while (0)
1770
1771#endif
1772
1773static int usbdev_notify(struct notifier_block *self,
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001774 unsigned long action, void *dev)
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001775{
1776 switch (action) {
1777 case USB_DEVICE_ADD:
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001778 if (usb_classdev_add(dev))
Akinobu Mita27d39e22006-10-09 18:09:33 +09001779 return NOTIFY_BAD;
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001780 break;
1781 case USB_DEVICE_REMOVE:
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001782 usb_classdev_remove(dev);
Alan Stern501950d2009-01-13 11:33:42 -05001783 usbdev_remove(dev);
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001784 break;
1785 }
1786 return NOTIFY_OK;
1787}
1788
1789static struct notifier_block usbdev_nb = {
Alan Stern501950d2009-01-13 11:33:42 -05001790 .notifier_call = usbdev_notify,
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001791};
1792
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -07001793static struct cdev usb_device_cdev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001794
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001795int __init usb_devio_init(void)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001796{
1797 int retval;
1798
Alan Sternfad21bd2005-08-10 15:15:57 -04001799 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001800 "usb_device");
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001801 if (retval) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -07001802 printk(KERN_ERR "Unable to register minors for usb_device\n");
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001803 goto out;
1804 }
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001805 cdev_init(&usb_device_cdev, &usbdev_file_operations);
Alan Sternfad21bd2005-08-10 15:15:57 -04001806 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001807 if (retval) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -07001808 printk(KERN_ERR "Unable to get usb_device major %d\n",
1809 USB_DEVICE_MAJOR);
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001810 goto error_cdev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001811 }
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001812#ifdef CONFIG_USB_DEVICE_CLASS
1813 usb_classdev_class = class_create(THIS_MODULE, "usb_device");
1814 if (IS_ERR(usb_classdev_class)) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -07001815 printk(KERN_ERR "Unable to register usb_device class\n");
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001816 retval = PTR_ERR(usb_classdev_class);
1817 cdev_del(&usb_device_cdev);
1818 usb_classdev_class = NULL;
1819 goto out;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001820 }
Dan Williamse105b8b2008-04-21 10:51:07 -07001821 /* devices of this class shadow the major:minor of their parent
1822 * device, so clear ->dev_kobj to prevent adding duplicate entries
1823 * to /sys/dev
1824 */
1825 usb_classdev_class->dev_kobj = NULL;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001826#endif
Alan Stern501950d2009-01-13 11:33:42 -05001827 usb_register_notify(&usbdev_nb);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001828out:
1829 return retval;
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001830
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001831error_cdev:
1832 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
1833 goto out;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001834}
1835
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001836void usb_devio_cleanup(void)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001837{
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001838 usb_unregister_notify(&usbdev_nb);
Alan Stern501950d2009-01-13 11:33:42 -05001839#ifdef CONFIG_USB_DEVICE_CLASS
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001840 class_destroy(usb_classdev_class);
1841#endif
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001842 cdev_del(&usb_device_cdev);
Alan Sternfad21bd2005-08-10 15:15:57 -04001843 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001844}