blob: 71514be8b71531a1596269bd6156809bd4133559 [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"
Alan Stern7cbe5dc2009-06-29 10:56:54 -040055#include "hub.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Kay Sieversfbf82fd2005-07-31 01:05:53 +020057#define USB_MAXBUS 64
58#define USB_DEVICE_MAX USB_MAXBUS * 128
Kay Sieversfbf82fd2005-07-31 01:05:53 +020059
Alan Stern4a2a8a22006-07-01 22:05:01 -040060/* Mutual exclusion for removal, open, and release */
61DEFINE_MUTEX(usbfs_mutex);
62
Alan Sterncd9f0372008-06-24 14:47:04 -040063struct dev_state {
64 struct list_head list; /* state list */
65 struct usb_device *dev;
66 struct file *file;
67 spinlock_t lock; /* protects the async urb lists */
68 struct list_head async_pending;
69 struct list_head async_completed;
70 wait_queue_head_t wait; /* wake up if a request completed */
71 unsigned int discsignr;
72 struct pid *disc_pid;
73 uid_t disc_uid, disc_euid;
74 void __user *disccontext;
75 unsigned long ifclaimed;
76 u32 secid;
77};
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079struct async {
80 struct list_head asynclist;
81 struct dev_state *ps;
Eric W. Biederman2425c082006-10-02 02:17:28 -070082 struct pid *pid;
Harald Welte46113832005-10-10 19:44:29 +020083 uid_t uid, euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 unsigned int signr;
85 unsigned int ifnum;
86 void __user *userbuffer;
87 void __user *userurb;
88 struct urb *urb;
Alan Sterne0152682007-08-24 15:42:52 -040089 int status;
David Quigley7a019552006-06-30 01:55:48 -070090 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091};
92
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -080093static int usbfs_snoop;
94module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
95MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#define snoop(dev, format, arg...) \
98 do { \
99 if (usbfs_snoop) \
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800100 dev_info(dev , format , ## arg); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 } while (0)
102
Alan Stern4c6e8972009-06-29 11:02:04 -0400103enum snoop_when {
104 SUBMIT, COMPLETE
105};
106
Alan Sternfad21bd2005-08-10 15:15:57 -0400107#define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define MAX_USBFS_BUFFER_SIZE 16384
110
Alan Stern4c6e8972009-06-29 11:02:04 -0400111
Alan Sternd34d9722009-03-09 13:44:48 -0400112static int connected(struct dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Alan Stern349710c2006-07-01 22:05:56 -0400114 return (!list_empty(&ps->list) &&
115 ps->dev->state != USB_STATE_NOTATTACHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
119{
120 loff_t ret;
121
122 lock_kernel();
123
124 switch (orig) {
125 case 0:
126 file->f_pos = offset;
127 ret = file->f_pos;
128 break;
129 case 1:
130 file->f_pos += offset;
131 ret = file->f_pos;
132 break;
133 case 2:
134 default:
135 ret = -EINVAL;
136 }
137
138 unlock_kernel();
139 return ret;
140}
141
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800142static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
143 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200145 struct dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct usb_device *dev = ps->dev;
147 ssize_t ret = 0;
148 unsigned len;
149 loff_t pos;
150 int i;
151
152 pos = *ppos;
153 usb_lock_device(dev);
Alan Stern349710c2006-07-01 22:05:56 -0400154 if (!connected(ps)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 ret = -ENODEV;
156 goto err;
157 } else if (pos < 0) {
158 ret = -EINVAL;
159 goto err;
160 }
161
162 if (pos < sizeof(struct usb_device_descriptor)) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800163 /* 18 bytes - fits on the stack */
164 struct usb_device_descriptor temp_desc;
Oliver Neukum8781ba02006-01-06 21:24:25 +0100165
166 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
Andrew Morton9fcd5c32006-01-18 23:55:07 -0800167 le16_to_cpus(&temp_desc.bcdUSB);
168 le16_to_cpus(&temp_desc.idVendor);
169 le16_to_cpus(&temp_desc.idProduct);
170 le16_to_cpus(&temp_desc.bcdDevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 len = sizeof(struct usb_device_descriptor) - pos;
173 if (len > nbytes)
174 len = nbytes;
Oliver Neukum8781ba02006-01-06 21:24:25 +0100175 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 ret = -EFAULT;
177 goto err;
178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 *ppos += len;
181 buf += len;
182 nbytes -= len;
183 ret += len;
184 }
185
186 pos = sizeof(struct usb_device_descriptor);
187 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
188 struct usb_config_descriptor *config =
189 (struct usb_config_descriptor *)dev->rawdescriptors[i];
190 unsigned int length = le16_to_cpu(config->wTotalLength);
191
192 if (*ppos < pos + length) {
193
194 /* The descriptor may claim to be longer than it
195 * really is. Here is the actual allocated length. */
196 unsigned alloclen =
197 le16_to_cpu(dev->config[i].desc.wTotalLength);
198
199 len = length - (*ppos - pos);
200 if (len > nbytes)
201 len = nbytes;
202
203 /* Simply don't write (skip over) unallocated parts */
204 if (alloclen > (*ppos - pos)) {
205 alloclen -= (*ppos - pos);
206 if (copy_to_user(buf,
207 dev->rawdescriptors[i] + (*ppos - pos),
208 min(len, alloclen))) {
209 ret = -EFAULT;
210 goto err;
211 }
212 }
213
214 *ppos += len;
215 buf += len;
216 nbytes -= len;
217 ret += len;
218 }
219
220 pos += length;
221 }
222
223err:
224 usb_unlock_device(dev);
225 return ret;
226}
227
228/*
229 * async list handling
230 */
231
232static struct async *alloc_async(unsigned int numisoframes)
233{
Pete Zaitcevdd95b812008-01-05 02:01:07 -0800234 struct async *as;
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400235
Pete Zaitcevdd95b812008-01-05 02:01:07 -0800236 as = kzalloc(sizeof(struct async), GFP_KERNEL);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800237 if (!as)
238 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
240 if (!as->urb) {
241 kfree(as);
242 return NULL;
243 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800244 return as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
247static void free_async(struct async *as)
248{
Eric W. Biederman2425c082006-10-02 02:17:28 -0700249 put_pid(as->pid);
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -0700250 kfree(as->urb->transfer_buffer);
251 kfree(as->urb->setup_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 usb_free_urb(as->urb);
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -0700253 kfree(as);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Alan Sternd34d9722009-03-09 13:44:48 -0400256static void async_newpending(struct async *as)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800258 struct dev_state *ps = as->ps;
259 unsigned long flags;
260
261 spin_lock_irqsave(&ps->lock, flags);
262 list_add_tail(&as->asynclist, &ps->async_pending);
263 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Alan Sternd34d9722009-03-09 13:44:48 -0400266static void async_removepending(struct async *as)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800268 struct dev_state *ps = as->ps;
269 unsigned long flags;
270
271 spin_lock_irqsave(&ps->lock, flags);
272 list_del_init(&as->asynclist);
273 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Alan Sternd34d9722009-03-09 13:44:48 -0400276static struct async *async_getcompleted(struct dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800278 unsigned long flags;
279 struct async *as = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800281 spin_lock_irqsave(&ps->lock, flags);
282 if (!list_empty(&ps->async_completed)) {
283 as = list_entry(ps->async_completed.next, struct async,
284 asynclist);
285 list_del_init(&as->asynclist);
286 }
287 spin_unlock_irqrestore(&ps->lock, flags);
288 return as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
Alan Sternd34d9722009-03-09 13:44:48 -0400291static struct async *async_getpending(struct dev_state *ps,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800292 void __user *userurb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800294 unsigned long flags;
295 struct async *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800297 spin_lock_irqsave(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 list_for_each_entry(as, &ps->async_pending, asynclist)
299 if (as->userurb == userurb) {
300 list_del_init(&as->asynclist);
301 spin_unlock_irqrestore(&ps->lock, flags);
302 return as;
303 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800304 spin_unlock_irqrestore(&ps->lock, flags);
305 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Alan Stern4c6e8972009-06-29 11:02:04 -0400308static void snoop_urb(struct usb_device *udev,
309 void __user *userurb, int pipe, unsigned length,
310 int timeout_or_status, enum snoop_when when)
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700311{
Alan Stern4c6e8972009-06-29 11:02:04 -0400312 static const char *types[] = {"isoc", "int", "ctrl", "bulk"};
313 static const char *dirs[] = {"out", "in"};
314 int ep;
315 const char *t, *d;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700316
317 if (!usbfs_snoop)
318 return;
319
Alan Stern4c6e8972009-06-29 11:02:04 -0400320 ep = usb_pipeendpoint(pipe);
321 t = types[usb_pipetype(pipe)];
322 d = dirs[!!usb_pipein(pipe)];
323
324 if (userurb) { /* Async */
325 if (when == SUBMIT)
326 dev_info(&udev->dev, "userurb %p, ep%d %s-%s, "
327 "length %u\n",
328 userurb, ep, t, d, length);
329 else
330 dev_info(&udev->dev, "userurb %p, ep%d %s-%s, "
331 "actual_length %u status %d\n",
332 userurb, ep, t, d, length,
333 timeout_or_status);
334 } else {
335 if (when == SUBMIT)
336 dev_info(&udev->dev, "ep%d %s-%s, length %u, "
337 "timeout %d\n",
338 ep, t, d, length, timeout_or_status);
339 else
340 dev_info(&udev->dev, "ep%d %s-%s, actual_length %u, "
341 "status %d\n",
342 ep, t, d, length, timeout_or_status);
343 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700344}
345
David Howells7d12e782006-10-05 14:55:46 +0100346static void async_completed(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800348 struct async *as = urb->context;
349 struct dev_state *ps = as->ps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct siginfo sinfo;
Oliver Neukum516a1a02009-07-08 19:09:23 +0200351 struct pid *pid = NULL;
352 uid_t uid = 0;
353 uid_t euid = 0;
354 u32 secid = 0;
355 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800357 spin_lock(&ps->lock);
358 list_move_tail(&as->asynclist, &ps->async_completed);
Alan Sterne0152682007-08-24 15:42:52 -0400359 as->status = urb->status;
Oliver Neukum516a1a02009-07-08 19:09:23 +0200360 signr = as->signr;
361 if (signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 sinfo.si_signo = as->signr;
Alan Sterne0152682007-08-24 15:42:52 -0400363 sinfo.si_errno = as->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 sinfo.si_code = SI_ASYNCIO;
365 sinfo.si_addr = as->userurb;
Oliver Neukum516a1a02009-07-08 19:09:23 +0200366 pid = as->pid;
367 uid = as->uid;
368 euid = as->euid;
369 secid = as->secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700371 snoop(&urb->dev->dev, "urb complete\n");
Alan Stern4c6e8972009-06-29 11:02:04 -0400372 snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length,
373 as->status, COMPLETE);
Oliver Neukum516a1a02009-07-08 19:09:23 +0200374 spin_unlock(&ps->lock);
375
376 if (signr)
377 kill_pid_info_as_uid(sinfo.si_signo, &sinfo, pid, uid,
378 euid, secid);
379
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700380 wake_up(&ps->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800383static void destroy_async(struct dev_state *ps, struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 struct async *as;
386 unsigned long flags;
387
388 spin_lock_irqsave(&ps->lock, flags);
389 while (!list_empty(list)) {
390 as = list_entry(list->next, struct async, asynclist);
391 list_del_init(&as->asynclist);
392
393 /* drop the spinlock so the completion handler can run */
394 spin_unlock_irqrestore(&ps->lock, flags);
395 usb_kill_urb(as->urb);
396 spin_lock_irqsave(&ps->lock, flags);
397 }
398 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800401static void destroy_async_on_interface(struct dev_state *ps,
402 unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 struct list_head *p, *q, hitlist;
405 unsigned long flags;
406
407 INIT_LIST_HEAD(&hitlist);
408 spin_lock_irqsave(&ps->lock, flags);
409 list_for_each_safe(p, q, &ps->async_pending)
410 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
411 list_move_tail(p, &hitlist);
412 spin_unlock_irqrestore(&ps->lock, flags);
413 destroy_async(ps, &hitlist);
414}
415
Alan Sternd34d9722009-03-09 13:44:48 -0400416static void destroy_all_async(struct dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800418 destroy_async(ps, &ps->async_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421/*
422 * interface claims are made only at the request of user level code,
423 * which can also release them (explicitly or by closing files).
424 * they're also undone when devices disconnect.
425 */
426
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800427static int driver_probe(struct usb_interface *intf,
428 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 return -ENODEV;
431}
432
433static void driver_disconnect(struct usb_interface *intf)
434{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800435 struct dev_state *ps = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
437
438 if (!ps)
439 return;
440
441 /* NOTE: this relies on usbcore having canceled and completed
442 * all pending I/O requests; 2.6 does that.
443 */
444
445 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
446 clear_bit(ifnum, &ps->ifclaimed);
447 else
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -0700448 dev_warn(&intf->dev, "interface number %u out of range\n",
449 ifnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800451 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 /* force async requests to complete */
454 destroy_async_on_interface(ps, ifnum);
455}
456
Alan Stern2e2eb832007-12-04 14:35:15 -0500457/* The following routines are merely placeholders. There is no way
458 * to inform a user task about suspend or resumes.
459 */
460static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
461{
462 return 0;
463}
464
465static int driver_resume(struct usb_interface *intf)
466{
467 return 0;
468}
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470struct usb_driver usbfs_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 .name = "usbfs",
472 .probe = driver_probe,
473 .disconnect = driver_disconnect,
Alan Stern2e2eb832007-12-04 14:35:15 -0500474 .suspend = driver_suspend,
475 .resume = driver_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476};
477
478static int claimintf(struct dev_state *ps, unsigned int ifnum)
479{
480 struct usb_device *dev = ps->dev;
481 struct usb_interface *intf;
482 int err;
483
484 if (ifnum >= 8*sizeof(ps->ifclaimed))
485 return -EINVAL;
486 /* already claimed */
487 if (test_bit(ifnum, &ps->ifclaimed))
488 return 0;
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 intf = usb_ifnum_to_if(dev, ifnum);
491 if (!intf)
492 err = -ENOENT;
493 else
494 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (err == 0)
496 set_bit(ifnum, &ps->ifclaimed);
497 return err;
498}
499
500static int releaseintf(struct dev_state *ps, unsigned int ifnum)
501{
502 struct usb_device *dev;
503 struct usb_interface *intf;
504 int err;
505
506 err = -EINVAL;
507 if (ifnum >= 8*sizeof(ps->ifclaimed))
508 return err;
509 dev = ps->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 intf = usb_ifnum_to_if(dev, ifnum);
511 if (!intf)
512 err = -ENOENT;
513 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
514 usb_driver_release_interface(&usbfs_driver, intf);
515 err = 0;
516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return err;
518}
519
520static int checkintf(struct dev_state *ps, unsigned int ifnum)
521{
522 if (ps->dev->state != USB_STATE_CONFIGURED)
523 return -EHOSTUNREACH;
524 if (ifnum >= 8*sizeof(ps->ifclaimed))
525 return -EINVAL;
526 if (test_bit(ifnum, &ps->ifclaimed))
527 return 0;
528 /* if not yet claimed, claim it for the driver */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800529 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
530 "interface %u before use\n", task_pid_nr(current),
531 current->comm, ifnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return claimintf(ps, ifnum);
533}
534
535static int findintfep(struct usb_device *dev, unsigned int ep)
536{
537 unsigned int i, j, e;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800538 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 struct usb_host_interface *alts;
540 struct usb_endpoint_descriptor *endpt;
541
542 if (ep & ~(USB_DIR_IN|0xf))
543 return -EINVAL;
544 if (!dev->actconfig)
545 return -ESRCH;
546 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
547 intf = dev->actconfig->interface[i];
548 for (j = 0; j < intf->num_altsetting; j++) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800549 alts = &intf->altsetting[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
551 endpt = &alts->endpoint[e].desc;
552 if (endpt->bEndpointAddress == ep)
553 return alts->desc.bInterfaceNumber;
554 }
555 }
556 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800557 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800560static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
561 unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 int ret = 0;
564
David Vrabel6da9c992009-02-18 14:43:47 +0000565 if (ps->dev->state != USB_STATE_UNAUTHENTICATED
566 && ps->dev->state != USB_STATE_ADDRESS
Horst Schirmeier24f8b112006-03-11 00:16:55 -0800567 && ps->dev->state != USB_STATE_CONFIGURED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return -EHOSTUNREACH;
569 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
570 return 0;
571
572 index &= 0xff;
573 switch (requesttype & USB_RECIP_MASK) {
574 case USB_RECIP_ENDPOINT:
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800575 ret = findintfep(ps->dev, index);
576 if (ret >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 ret = checkintf(ps, ret);
578 break;
579
580 case USB_RECIP_INTERFACE:
581 ret = checkintf(ps, index);
582 break;
583 }
584 return ret;
585}
586
Alan Stern61ad04a2008-06-24 14:47:12 -0400587static int match_devt(struct device *dev, void *data)
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700588{
David Howellsa80d5ff2008-07-02 12:28:55 +0100589 return dev->devt == (dev_t) (unsigned long) data;
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100590}
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700591
Alan Stern61ad04a2008-06-24 14:47:12 -0400592static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100593{
594 struct device *dev;
595
David Howellsa80d5ff2008-07-02 12:28:55 +0100596 dev = bus_find_device(&usb_bus_type, NULL,
597 (void *) (unsigned long) devt, match_devt);
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100598 if (!dev)
599 return NULL;
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100600 return container_of(dev, struct usb_device, dev);
601}
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603/*
604 * file operations
605 */
606static int usbdev_open(struct inode *inode, struct file *file)
607{
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200608 struct usb_device *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 struct dev_state *ps;
David Howells86a264a2008-11-14 10:39:18 +1100610 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 int ret;
612
Jonathan Corbetb5b4aa62008-05-16 14:25:20 -0600613 lock_kernel();
Alan Stern4a2a8a22006-07-01 22:05:01 -0400614 /* Protect against simultaneous removal or release */
615 mutex_lock(&usbfs_mutex);
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 ret = -ENOMEM;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800618 ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
619 if (!ps)
Alan Stern4a2a8a22006-07-01 22:05:01 -0400620 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Alan Stern01105a22009-07-30 15:28:14 -0400622 ret = -ENODEV;
Alan Stern61ad04a2008-06-24 14:47:12 -0400623
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100624 /* usbdev device-node */
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200625 if (imajor(inode) == USB_DEVICE_MAJOR)
Alan Stern61ad04a2008-06-24 14:47:12 -0400626 dev = usbdev_lookup_by_devt(inode->i_rdev);
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100627#ifdef CONFIG_USB_DEVICEFS
628 /* procfs file */
Alan Sternd64aac32008-06-24 14:47:19 -0400629 if (!dev) {
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700630 dev = inode->i_private;
Alan Sternd64aac32008-06-24 14:47:19 -0400631 if (dev && dev->usbfs_dentry &&
632 dev->usbfs_dentry->d_inode == inode)
633 usb_get_dev(dev);
634 else
635 dev = NULL;
636 }
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100637#endif
Alan Sternd64aac32008-06-24 14:47:19 -0400638 if (!dev || dev->state == USB_STATE_NOTATTACHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 goto out;
Alan Stern94fcda12006-11-20 11:38:46 -0500640 ret = usb_autoresume_device(dev);
Alan Stern01d883d2006-08-30 15:47:18 -0400641 if (ret)
642 goto out;
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 ret = 0;
645 ps->dev = dev;
646 ps->file = file;
647 spin_lock_init(&ps->lock);
Dan Carpenter316547f2006-12-13 00:03:38 -0800648 INIT_LIST_HEAD(&ps->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 INIT_LIST_HEAD(&ps->async_pending);
650 INIT_LIST_HEAD(&ps->async_completed);
651 init_waitqueue_head(&ps->wait);
652 ps->discsignr = 0;
Eric W. Biederman2425c082006-10-02 02:17:28 -0700653 ps->disc_pid = get_pid(task_pid(current));
David Howells86a264a2008-11-14 10:39:18 +1100654 ps->disc_uid = cred->uid;
655 ps->disc_euid = cred->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 ps->disccontext = NULL;
657 ps->ifclaimed = 0;
David Quigley7a019552006-06-30 01:55:48 -0700658 security_task_getsecid(current, &ps->secid);
Oliver Neukum527660a2007-04-20 20:50:48 +0200659 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 list_add_tail(&ps->list, &dev->filelist);
661 file->private_data = ps;
Alan Stern2da41d52008-10-06 11:24:26 -0400662 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
663 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 out:
Alan Sternd64aac32008-06-24 14:47:19 -0400665 if (ret) {
Alan Stern01d883d2006-08-30 15:47:18 -0400666 kfree(ps);
Alan Sternd64aac32008-06-24 14:47:19 -0400667 usb_put_dev(dev);
668 }
Alan Stern4a2a8a22006-07-01 22:05:01 -0400669 mutex_unlock(&usbfs_mutex);
Jonathan Corbetb5b4aa62008-05-16 14:25:20 -0600670 unlock_kernel();
Alan Stern4a2a8a22006-07-01 22:05:01 -0400671 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674static int usbdev_release(struct inode *inode, struct file *file)
675{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200676 struct dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 struct usb_device *dev = ps->dev;
678 unsigned int ifnum;
Alan Stern6ff10462009-03-09 13:44:02 -0400679 struct async *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 usb_lock_device(dev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400682 usb_hub_release_all_ports(dev, ps);
Alan Stern4a2a8a22006-07-01 22:05:01 -0400683
684 /* Protect against simultaneous open */
685 mutex_lock(&usbfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 list_del_init(&ps->list);
Alan Stern4a2a8a22006-07-01 22:05:01 -0400687 mutex_unlock(&usbfs_mutex);
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
690 ifnum++) {
691 if (test_bit(ifnum, &ps->ifclaimed))
692 releaseintf(ps, ifnum);
693 }
694 destroy_all_async(ps);
Alan Stern94fcda12006-11-20 11:38:46 -0500695 usb_autosuspend_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 usb_unlock_device(dev);
697 usb_put_dev(dev);
Eric W. Biederman2425c082006-10-02 02:17:28 -0700698 put_pid(ps->disc_pid);
Alan Stern6ff10462009-03-09 13:44:02 -0400699
700 as = async_getcompleted(ps);
701 while (as) {
702 free_async(as);
703 as = async_getcompleted(ps);
704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 kfree(ps);
Alan Stern4a2a8a22006-07-01 22:05:01 -0400706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
709static int proc_control(struct dev_state *ps, void __user *arg)
710{
711 struct usb_device *dev = ps->dev;
712 struct usbdevfs_ctrltransfer ctrl;
713 unsigned int tmo;
714 unsigned char *tbuf;
Andrew Mortonff66e3c2008-03-12 13:32:24 -0700715 unsigned wLength;
Alan Stern4c6e8972009-06-29 11:02:04 -0400716 int i, pipe, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
719 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800720 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex);
721 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return ret;
Andrew Mortonff66e3c2008-03-12 13:32:24 -0700723 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
724 if (wLength > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800726 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
727 if (!tbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return -ENOMEM;
729 tmo = ctrl.timeout;
730 if (ctrl.bRequestType & 0x80) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800731 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
732 ctrl.wLength)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 free_page((unsigned long)tbuf);
734 return -EINVAL;
735 }
Alan Stern4c6e8972009-06-29 11:02:04 -0400736 pipe = usb_rcvctrlpipe(dev, 0);
737 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 usb_unlock_device(dev);
Alan Stern4c6e8972009-06-29 11:02:04 -0400740 i = usb_control_msg(dev, pipe, ctrl.bRequest,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800741 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
742 tbuf, ctrl.wLength, tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 usb_lock_device(dev);
Alan Stern4c6e8972009-06-29 11:02:04 -0400744 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE);
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if ((i > 0) && ctrl.wLength) {
Alan Sternfe0410c2005-07-29 12:16:58 -0700747 if (copy_to_user(ctrl.data, tbuf, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 free_page((unsigned long)tbuf);
749 return -EFAULT;
750 }
751 }
752 } else {
753 if (ctrl.wLength) {
754 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
755 free_page((unsigned long)tbuf);
756 return -EFAULT;
757 }
758 }
Alan Stern4c6e8972009-06-29 11:02:04 -0400759 pipe = usb_sndctrlpipe(dev, 0);
760 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT);
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 usb_unlock_device(dev);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800763 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
764 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
765 tbuf, ctrl.wLength, tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 usb_lock_device(dev);
Alan Stern4c6e8972009-06-29 11:02:04 -0400767 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 free_page((unsigned long)tbuf);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800770 if (i < 0 && i != -EPIPE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
772 "failed cmd %s rqt %u rq %u len %u ret %d\n",
773 current->comm, ctrl.bRequestType, ctrl.bRequest,
774 ctrl.wLength, i);
775 }
776 return i;
777}
778
779static int proc_bulk(struct dev_state *ps, void __user *arg)
780{
781 struct usb_device *dev = ps->dev;
782 struct usbdevfs_bulktransfer bulk;
783 unsigned int tmo, len1, pipe;
784 int len2;
785 unsigned char *tbuf;
Alan Stern4c6e8972009-06-29 11:02:04 -0400786 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 if (copy_from_user(&bulk, arg, sizeof(bulk)))
789 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800790 ret = findintfep(ps->dev, bulk.ep);
791 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800793 ret = checkintf(ps, ret);
794 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return ret;
796 if (bulk.ep & USB_DIR_IN)
797 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
798 else
799 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
800 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
801 return -EINVAL;
802 len1 = bulk.len;
803 if (len1 > MAX_USBFS_BUFFER_SIZE)
804 return -EINVAL;
805 if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
806 return -ENOMEM;
807 tmo = bulk.timeout;
808 if (bulk.ep & 0x80) {
809 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
810 kfree(tbuf);
811 return -EINVAL;
812 }
Alan Stern4c6e8972009-06-29 11:02:04 -0400813 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT);
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 usb_unlock_device(dev);
816 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
817 usb_lock_device(dev);
Alan Stern4c6e8972009-06-29 11:02:04 -0400818 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE);
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (!i && len2) {
821 if (copy_to_user(bulk.data, tbuf, len2)) {
822 kfree(tbuf);
823 return -EFAULT;
824 }
825 }
826 } else {
827 if (len1) {
828 if (copy_from_user(tbuf, bulk.data, len1)) {
829 kfree(tbuf);
830 return -EFAULT;
831 }
832 }
Alan Stern4c6e8972009-06-29 11:02:04 -0400833 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT);
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 usb_unlock_device(dev);
836 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
837 usb_lock_device(dev);
Alan Stern4c6e8972009-06-29 11:02:04 -0400838 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840 kfree(tbuf);
841 if (i < 0)
842 return i;
843 return len2;
844}
845
846static int proc_resetep(struct dev_state *ps, void __user *arg)
847{
848 unsigned int ep;
849 int ret;
850
851 if (get_user(ep, (unsigned int __user *)arg))
852 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800853 ret = findintfep(ps->dev, ep);
854 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800856 ret = checkintf(ps, ret);
857 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return ret;
David Vrabel3444b262009-04-08 17:36:28 +0000859 usb_reset_endpoint(ps->dev, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return 0;
861}
862
863static int proc_clearhalt(struct dev_state *ps, void __user *arg)
864{
865 unsigned int ep;
866 int pipe;
867 int ret;
868
869 if (get_user(ep, (unsigned int __user *)arg))
870 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800871 ret = findintfep(ps->dev, ep);
872 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800874 ret = checkintf(ps, ret);
875 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return ret;
877 if (ep & USB_DIR_IN)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800878 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
879 else
880 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 return usb_clear_halt(ps->dev, pipe);
883}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885static int proc_getdriver(struct dev_state *ps, void __user *arg)
886{
887 struct usbdevfs_getdriver gd;
888 struct usb_interface *intf;
889 int ret;
890
891 if (copy_from_user(&gd, arg, sizeof(gd)))
892 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 intf = usb_ifnum_to_if(ps->dev, gd.interface);
894 if (!intf || !intf->dev.driver)
895 ret = -ENODATA;
896 else {
897 strncpy(gd.driver, intf->dev.driver->name,
898 sizeof(gd.driver));
899 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return ret;
902}
903
904static int proc_connectinfo(struct dev_state *ps, void __user *arg)
905{
906 struct usbdevfs_connectinfo ci;
907
908 ci.devnum = ps->dev->devnum;
909 ci.slow = ps->dev->speed == USB_SPEED_LOW;
910 if (copy_to_user(arg, &ci, sizeof(ci)))
911 return -EFAULT;
912 return 0;
913}
914
915static int proc_resetdevice(struct dev_state *ps)
916{
Ming Lei742120c2008-06-18 22:00:29 +0800917 return usb_reset_device(ps->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
920static int proc_setintf(struct dev_state *ps, void __user *arg)
921{
922 struct usbdevfs_setinterface setintf;
923 int ret;
924
925 if (copy_from_user(&setintf, arg, sizeof(setintf)))
926 return -EFAULT;
927 if ((ret = checkintf(ps, setintf.interface)))
928 return ret;
929 return usb_set_interface(ps->dev, setintf.interface,
930 setintf.altsetting);
931}
932
933static int proc_setconfig(struct dev_state *ps, void __user *arg)
934{
Alan Stern3f141e22007-02-08 16:40:43 -0500935 int u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 int status = 0;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800937 struct usb_host_config *actconfig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Alan Stern3f141e22007-02-08 16:40:43 -0500939 if (get_user(u, (int __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return -EFAULT;
941
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800942 actconfig = ps->dev->actconfig;
943
944 /* Don't touch the device if any interfaces are claimed.
945 * It could interfere with other drivers' operations, and if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 * an interface is claimed by usbfs it could easily deadlock.
947 */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800948 if (actconfig) {
949 int i;
950
951 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
952 if (usb_interface_claimed(actconfig->interface[i])) {
953 dev_warn(&ps->dev->dev,
David Brownell72ebddb2005-04-11 18:34:17 -0700954 "usbfs: interface %d claimed by %s "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 "while '%s' sets config #%d\n",
956 actconfig->interface[i]
957 ->cur_altsetting
958 ->desc.bInterfaceNumber,
David Brownell72ebddb2005-04-11 18:34:17 -0700959 actconfig->interface[i]
960 ->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 current->comm, u);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800962 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800965 }
966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
969 * so avoid usb_set_configuration()'s kick to sysfs
970 */
971 if (status == 0) {
972 if (actconfig && actconfig->desc.bConfigurationValue == u)
973 status = usb_reset_configuration(ps->dev);
974 else
975 status = usb_set_configuration(ps->dev, u);
976 }
977
978 return status;
979}
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800982 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
983 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 struct usbdevfs_iso_packet_desc *isopkt = NULL;
986 struct usb_host_endpoint *ep;
987 struct async *as;
988 struct usb_ctrlrequest *dr = NULL;
David Howells86a264a2008-11-14 10:39:18 +1100989 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 unsigned int u, totlen, isofrmlen;
Alan Stern97b9eb92007-02-26 14:56:14 -0500991 int ret, ifnum = -1;
Alan Stern93cf9b92007-07-30 17:09:28 -0400992 int is_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Alan Stern14722ef2008-04-17 10:18:11 -0400994 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
995 USBDEVFS_URB_SHORT_NOT_OK |
996 USBDEVFS_URB_NO_FSBR |
997 USBDEVFS_URB_ZERO_PACKET |
998 USBDEVFS_URB_NO_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return -EINVAL;
Alan Stern91801352009-06-29 11:04:54 -04001000 if (uurb->buffer_length > 0 && !uurb->buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001002 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
1003 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
1004 ifnum = findintfep(ps->dev, uurb->endpoint);
1005 if (ifnum < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return ifnum;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001007 ret = checkintf(ps, ifnum);
1008 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return ret;
1010 }
Alan Stern93cf9b92007-07-30 17:09:28 -04001011 if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
1012 is_in = 1;
1013 ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
1014 } else {
1015 is_in = 0;
1016 ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
1017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (!ep)
1019 return -ENOENT;
1020 switch(uurb->type) {
1021 case USBDEVFS_URB_TYPE_CONTROL:
Alan Stern93cf9b92007-07-30 17:09:28 -04001022 if (!usb_endpoint_xfer_control(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001024 /* min 8 byte setup packet,
1025 * max 8 byte setup plus an arbitrary data stage */
1026 if (uurb->buffer_length < 8 ||
1027 uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001029 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1030 if (!dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return -ENOMEM;
1032 if (copy_from_user(dr, uurb->buffer, 8)) {
1033 kfree(dr);
1034 return -EFAULT;
1035 }
1036 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
1037 kfree(dr);
1038 return -EINVAL;
1039 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001040 ret = check_ctrlrecip(ps, dr->bRequestType,
1041 le16_to_cpup(&dr->wIndex));
1042 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 kfree(dr);
1044 return ret;
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 uurb->number_of_packets = 0;
1047 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1048 uurb->buffer += 8;
Alan Stern93cf9b92007-07-30 17:09:28 -04001049 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1050 is_in = 1;
1051 uurb->endpoint |= USB_DIR_IN;
1052 } else {
1053 is_in = 0;
1054 uurb->endpoint &= ~USB_DIR_IN;
1055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 break;
1057
1058 case USBDEVFS_URB_TYPE_BULK:
Alan Stern93cf9b92007-07-30 17:09:28 -04001059 switch (usb_endpoint_type(&ep->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 case USB_ENDPOINT_XFER_CONTROL:
1061 case USB_ENDPOINT_XFER_ISOC:
1062 return -EINVAL;
1063 /* allow single-shot interrupt transfers, at bogus rates */
1064 }
1065 uurb->number_of_packets = 0;
1066 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1067 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 break;
1069
1070 case USBDEVFS_URB_TYPE_ISO:
1071 /* arbitrary limit */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001072 if (uurb->number_of_packets < 1 ||
1073 uurb->number_of_packets > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return -EINVAL;
Alan Stern93cf9b92007-07-30 17:09:28 -04001075 if (!usb_endpoint_xfer_isoc(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001077 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1078 uurb->number_of_packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
1080 return -ENOMEM;
1081 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
1082 kfree(isopkt);
1083 return -EFAULT;
1084 }
1085 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001086 /* arbitrary limit,
1087 * sufficient for USB 2.0 high-bandwidth iso */
Micah Dowty36122422006-05-19 11:26:24 -07001088 if (isopkt[u].length > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 kfree(isopkt);
1090 return -EINVAL;
1091 }
1092 totlen += isopkt[u].length;
1093 }
Markus Rechberger59718972009-08-09 21:23:34 +02001094 /* 3072 * 64 microframes */
1095 if (totlen > 196608) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 kfree(isopkt);
1097 return -EINVAL;
1098 }
1099 uurb->buffer_length = totlen;
1100 break;
1101
1102 case USBDEVFS_URB_TYPE_INTERRUPT:
1103 uurb->number_of_packets = 0;
Alan Stern93cf9b92007-07-30 17:09:28 -04001104 if (!usb_endpoint_xfer_int(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1107 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 break;
1109
1110 default:
1111 return -EINVAL;
1112 }
Alan Stern91801352009-06-29 11:04:54 -04001113 if (uurb->buffer_length > 0 &&
1114 !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1115 uurb->buffer, uurb->buffer_length)) {
1116 kfree(isopkt);
1117 kfree(dr);
1118 return -EFAULT;
1119 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001120 as = alloc_async(uurb->number_of_packets);
1121 if (!as) {
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -07001122 kfree(isopkt);
1123 kfree(dr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return -ENOMEM;
1125 }
Alan Stern91801352009-06-29 11:04:54 -04001126 if (uurb->buffer_length > 0) {
1127 as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
1128 GFP_KERNEL);
1129 if (!as->urb->transfer_buffer) {
1130 kfree(isopkt);
1131 kfree(dr);
1132 free_async(as);
1133 return -ENOMEM;
1134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001136 as->urb->dev = ps->dev;
1137 as->urb->pipe = (uurb->type << 30) |
Alan Stern93cf9b92007-07-30 17:09:28 -04001138 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1139 (uurb->endpoint & USB_DIR_IN);
Alan Stern14722ef2008-04-17 10:18:11 -04001140
1141 /* This tedious sequence is necessary because the URB_* flags
1142 * are internal to the kernel and subject to change, whereas
1143 * the USBDEVFS_URB_* flags are a user API and must not be changed.
1144 */
1145 u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
1146 if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
1147 u |= URB_ISO_ASAP;
1148 if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
1149 u |= URB_SHORT_NOT_OK;
1150 if (uurb->flags & USBDEVFS_URB_NO_FSBR)
1151 u |= URB_NO_FSBR;
1152 if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1153 u |= URB_ZERO_PACKET;
1154 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
1155 u |= URB_NO_INTERRUPT;
1156 as->urb->transfer_flags = u;
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 as->urb->transfer_buffer_length = uurb->buffer_length;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001159 as->urb->setup_packet = (unsigned char *)dr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 as->urb->start_frame = uurb->start_frame;
1161 as->urb->number_of_packets = uurb->number_of_packets;
Alan Stern97b9eb92007-02-26 14:56:14 -05001162 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1163 ps->dev->speed == USB_SPEED_HIGH)
1164 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
1165 else
1166 as->urb->interval = ep->desc.bInterval;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001167 as->urb->context = as;
1168 as->urb->complete = async_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1170 as->urb->iso_frame_desc[u].offset = totlen;
1171 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1172 totlen += isopkt[u].length;
1173 }
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -07001174 kfree(isopkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 as->ps = ps;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001176 as->userurb = arg;
Alan Stern91801352009-06-29 11:04:54 -04001177 if (is_in && uurb->buffer_length > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 as->userbuffer = uurb->buffer;
1179 else
1180 as->userbuffer = NULL;
1181 as->signr = uurb->signr;
1182 as->ifnum = ifnum;
Eric W. Biederman2425c082006-10-02 02:17:28 -07001183 as->pid = get_pid(task_pid(current));
David Howells86a264a2008-11-14 10:39:18 +11001184 as->uid = cred->uid;
1185 as->euid = cred->euid;
David Quigley7a019552006-06-30 01:55:48 -07001186 security_task_getsecid(current, &as->secid);
Alan Stern91801352009-06-29 11:04:54 -04001187 if (!is_in && uurb->buffer_length > 0) {
Alan Stern93cf9b92007-07-30 17:09:28 -04001188 if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
Alan Stern91801352009-06-29 11:04:54 -04001189 uurb->buffer_length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 free_async(as);
1191 return -EFAULT;
1192 }
1193 }
Alan Stern4c6e8972009-06-29 11:02:04 -04001194 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1195 as->urb->transfer_buffer_length, 0, SUBMIT);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001196 async_newpending(as);
1197 if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
1198 dev_printk(KERN_DEBUG, &ps->dev->dev,
1199 "usbfs: usb_submit_urb returned %d\n", ret);
Alan Stern4c6e8972009-06-29 11:02:04 -04001200 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1201 0, ret, COMPLETE);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001202 async_removepending(as);
1203 free_async(as);
1204 return ret;
1205 }
1206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
1209static int proc_submiturb(struct dev_state *ps, void __user *arg)
1210{
1211 struct usbdevfs_urb uurb;
1212
1213 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1214 return -EFAULT;
1215
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001216 return proc_do_submiturb(ps, &uurb,
1217 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1218 arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
1221static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
1222{
1223 struct async *as;
1224
1225 as = async_getpending(ps, arg);
1226 if (!as)
1227 return -EINVAL;
1228 usb_kill_urb(as->urb);
1229 return 0;
1230}
1231
1232static int processcompl(struct async *as, void __user * __user *arg)
1233{
1234 struct urb *urb = as->urb;
1235 struct usbdevfs_urb __user *userurb = as->userurb;
1236 void __user *addr = as->userurb;
1237 unsigned int i;
1238
1239 if (as->userbuffer)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001240 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1241 urb->transfer_buffer_length))
Oliver Neukumd794a022009-06-28 23:34:14 +02001242 goto err_out;
Alan Sterne0152682007-08-24 15:42:52 -04001243 if (put_user(as->status, &userurb->status))
Oliver Neukumd794a022009-06-28 23:34:14 +02001244 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (put_user(urb->actual_length, &userurb->actual_length))
Oliver Neukumd794a022009-06-28 23:34:14 +02001246 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (put_user(urb->error_count, &userurb->error_count))
Oliver Neukumd794a022009-06-28 23:34:14 +02001248 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Alan Stern93cf9b92007-07-30 17:09:28 -04001250 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
Christopher Li668a9542005-04-18 17:39:26 -07001251 for (i = 0; i < urb->number_of_packets; i++) {
1252 if (put_user(urb->iso_frame_desc[i].actual_length,
1253 &userurb->iso_frame_desc[i].actual_length))
Oliver Neukumd794a022009-06-28 23:34:14 +02001254 goto err_out;
Christopher Li668a9542005-04-18 17:39:26 -07001255 if (put_user(urb->iso_frame_desc[i].status,
1256 &userurb->iso_frame_desc[i].status))
Oliver Neukumd794a022009-06-28 23:34:14 +02001257 goto err_out;
Christopher Li668a9542005-04-18 17:39:26 -07001258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260
1261 free_async(as);
1262
1263 if (put_user(addr, (void __user * __user *)arg))
1264 return -EFAULT;
1265 return 0;
Oliver Neukumd794a022009-06-28 23:34:14 +02001266
1267err_out:
1268 free_async(as);
1269 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001272static struct async *reap_as(struct dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001274 DECLARE_WAITQUEUE(wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 struct async *as = NULL;
1276 struct usb_device *dev = ps->dev;
1277
1278 add_wait_queue(&ps->wait, &wait);
1279 for (;;) {
1280 __set_current_state(TASK_INTERRUPTIBLE);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001281 as = async_getcompleted(ps);
1282 if (as)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 break;
1284 if (signal_pending(current))
1285 break;
1286 usb_unlock_device(dev);
1287 schedule();
1288 usb_lock_device(dev);
1289 }
1290 remove_wait_queue(&ps->wait, &wait);
1291 set_current_state(TASK_RUNNING);
1292 return as;
1293}
1294
1295static int proc_reapurb(struct dev_state *ps, void __user *arg)
1296{
1297 struct async *as = reap_as(ps);
1298 if (as)
1299 return processcompl(as, (void __user * __user *)arg);
1300 if (signal_pending(current))
1301 return -EINTR;
1302 return -EIO;
1303}
1304
1305static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
1306{
1307 struct async *as;
1308
1309 if (!(as = async_getcompleted(ps)))
1310 return -EAGAIN;
1311 return processcompl(as, (void __user * __user *)arg);
1312}
1313
1314#ifdef CONFIG_COMPAT
1315
1316static int get_urb32(struct usbdevfs_urb *kurb,
1317 struct usbdevfs_urb32 __user *uurb)
1318{
1319 __u32 uptr;
Michael Buesch18753eb2009-07-29 11:39:03 +02001320 if (!access_ok(VERIFY_READ, uurb, sizeof(*uurb)) ||
1321 __get_user(kurb->type, &uurb->type) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 __get_user(kurb->endpoint, &uurb->endpoint) ||
1323 __get_user(kurb->status, &uurb->status) ||
1324 __get_user(kurb->flags, &uurb->flags) ||
1325 __get_user(kurb->buffer_length, &uurb->buffer_length) ||
1326 __get_user(kurb->actual_length, &uurb->actual_length) ||
1327 __get_user(kurb->start_frame, &uurb->start_frame) ||
1328 __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
1329 __get_user(kurb->error_count, &uurb->error_count) ||
1330 __get_user(kurb->signr, &uurb->signr))
1331 return -EFAULT;
1332
1333 if (__get_user(uptr, &uurb->buffer))
1334 return -EFAULT;
1335 kurb->buffer = compat_ptr(uptr);
Mark Lorded0c7722009-01-02 02:48:24 -05001336 if (__get_user(uptr, &uurb->usercontext))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return -EFAULT;
1338 kurb->usercontext = compat_ptr(uptr);
1339
1340 return 0;
1341}
1342
1343static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
1344{
1345 struct usbdevfs_urb uurb;
1346
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001347 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return -EFAULT;
1349
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001350 return proc_do_submiturb(ps, &uurb,
1351 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
1352 arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
1355static int processcompl_compat(struct async *as, void __user * __user *arg)
1356{
1357 struct urb *urb = as->urb;
1358 struct usbdevfs_urb32 __user *userurb = as->userurb;
1359 void __user *addr = as->userurb;
1360 unsigned int i;
1361
1362 if (as->userbuffer)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001363 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1364 urb->transfer_buffer_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return -EFAULT;
Alan Sterne0152682007-08-24 15:42:52 -04001366 if (put_user(as->status, &userurb->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return -EFAULT;
1368 if (put_user(urb->actual_length, &userurb->actual_length))
1369 return -EFAULT;
1370 if (put_user(urb->error_count, &userurb->error_count))
1371 return -EFAULT;
1372
Alan Stern93cf9b92007-07-30 17:09:28 -04001373 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
Christopher Li668a9542005-04-18 17:39:26 -07001374 for (i = 0; i < urb->number_of_packets; i++) {
1375 if (put_user(urb->iso_frame_desc[i].actual_length,
1376 &userurb->iso_frame_desc[i].actual_length))
1377 return -EFAULT;
1378 if (put_user(urb->iso_frame_desc[i].status,
1379 &userurb->iso_frame_desc[i].status))
1380 return -EFAULT;
1381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383
1384 free_async(as);
Al Viroc714de52006-10-10 22:45:37 +01001385 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return -EFAULT;
1387 return 0;
1388}
1389
1390static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
1391{
1392 struct async *as = reap_as(ps);
1393 if (as)
1394 return processcompl_compat(as, (void __user * __user *)arg);
1395 if (signal_pending(current))
1396 return -EINTR;
1397 return -EIO;
1398}
1399
1400static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
1401{
1402 struct async *as;
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (!(as = async_getcompleted(ps)))
1405 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return processcompl_compat(as, (void __user * __user *)arg);
1407}
1408
1409#endif
1410
1411static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
1412{
1413 struct usbdevfs_disconnectsignal ds;
1414
1415 if (copy_from_user(&ds, arg, sizeof(ds)))
1416 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 ps->discsignr = ds.signr;
1418 ps->disccontext = ds.context;
1419 return 0;
1420}
1421
1422static int proc_claiminterface(struct dev_state *ps, void __user *arg)
1423{
1424 unsigned int ifnum;
1425
1426 if (get_user(ifnum, (unsigned int __user *)arg))
1427 return -EFAULT;
1428 return claimintf(ps, ifnum);
1429}
1430
1431static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
1432{
1433 unsigned int ifnum;
1434 int ret;
1435
1436 if (get_user(ifnum, (unsigned int __user *)arg))
1437 return -EFAULT;
1438 if ((ret = releaseintf(ps, ifnum)) < 0)
1439 return ret;
1440 destroy_async_on_interface (ps, ifnum);
1441 return 0;
1442}
1443
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001444static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 int size;
1447 void *buf = NULL;
1448 int retval = 0;
1449 struct usb_interface *intf = NULL;
1450 struct usb_driver *driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001452 /* alloc buffer */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001453 if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
1454 if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return -ENOMEM;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001456 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001457 if (copy_from_user(buf, ctl->data, size)) {
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -07001458 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return -EFAULT;
1460 }
1461 } else {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001462 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464 }
1465
Alan Stern349710c2006-07-01 22:05:56 -04001466 if (!connected(ps)) {
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -07001467 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return -ENODEV;
1469 }
1470
1471 if (ps->dev->state != USB_STATE_CONFIGURED)
1472 retval = -EHOSTUNREACH;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001473 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
1474 retval = -EINVAL;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001475 else switch (ctl->ioctl_code) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 /* disconnect kernel driver from interface */
1478 case USBDEVFS_DISCONNECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (intf->dev.driver) {
1480 driver = to_usb_driver(intf->dev.driver);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001481 dev_dbg(&intf->dev, "disconnect by usbfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 usb_driver_release_interface(driver, intf);
1483 } else
1484 retval = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 break;
1486
1487 /* let kernel drivers try to (re)bind to the interface */
1488 case USBDEVFS_CONNECT:
Alan Stern885e9742007-12-03 15:42:10 -05001489 if (!intf->dev.driver)
1490 retval = device_attach(&intf->dev);
1491 else
1492 retval = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 break;
1494
1495 /* talk directly to the interface's driver */
1496 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (intf->dev.driver)
1498 driver = to_usb_driver(intf->dev.driver);
1499 if (driver == NULL || driver->ioctl == NULL) {
1500 retval = -ENOTTY;
1501 } else {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001502 retval = driver->ioctl(intf, ctl->ioctl_code, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (retval == -ENOIOCTLCMD)
1504 retval = -ENOTTY;
1505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
1507
1508 /* cleanup and return */
1509 if (retval >= 0
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001510 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 && size > 0
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001512 && copy_to_user(ctl->data, buf, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 retval = -EFAULT;
Jesper Juhl6fd19f4b2005-04-18 17:39:33 -07001514
1515 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return retval;
1517}
1518
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001519static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
1520{
1521 struct usbdevfs_ioctl ctrl;
1522
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001523 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001524 return -EFAULT;
1525 return proc_ioctl(ps, &ctrl);
1526}
1527
1528#ifdef CONFIG_COMPAT
Andrew Morton777da592005-11-17 09:47:02 -08001529static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001530{
1531 struct usbdevfs_ioctl32 __user *uioc;
1532 struct usbdevfs_ioctl ctrl;
1533 u32 udata;
1534
Andrew Morton058120d2005-11-17 09:47:49 -08001535 uioc = compat_ptr((long)arg);
Michael Buesch18753eb2009-07-29 11:39:03 +02001536 if (!access_ok(VERIFY_READ, uioc, sizeof(*uioc)) ||
1537 __get_user(ctrl.ifno, &uioc->ifno) ||
1538 __get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001539 __get_user(udata, &uioc->data))
1540 return -EFAULT;
1541 ctrl.data = compat_ptr(udata);
1542
1543 return proc_ioctl(ps, &ctrl);
1544}
1545#endif
1546
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001547static int proc_claim_port(struct dev_state *ps, void __user *arg)
1548{
1549 unsigned portnum;
1550 int rc;
1551
1552 if (get_user(portnum, (unsigned __user *) arg))
1553 return -EFAULT;
1554 rc = usb_hub_claim_port(ps->dev, portnum, ps);
1555 if (rc == 0)
1556 snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n",
1557 portnum, task_pid_nr(current), current->comm);
1558 return rc;
1559}
1560
1561static int proc_release_port(struct dev_state *ps, void __user *arg)
1562{
1563 unsigned portnum;
1564
1565 if (get_user(portnum, (unsigned __user *) arg))
1566 return -EFAULT;
1567 return usb_hub_release_port(ps->dev, portnum, ps);
1568}
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570/*
1571 * NOTE: All requests here that have interface numbers as parameters
1572 * are assuming that somehow the configuration has been prevented from
1573 * changing. But there's no mechanism to ensure that...
1574 */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001575static int usbdev_ioctl(struct inode *inode, struct file *file,
1576 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Tobias Klauserec17cf12006-09-13 21:38:41 +02001578 struct dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 struct usb_device *dev = ps->dev;
1580 void __user *p = (void __user *)arg;
1581 int ret = -ENOTTY;
1582
1583 if (!(file->f_mode & FMODE_WRITE))
1584 return -EPERM;
1585 usb_lock_device(dev);
Alan Stern349710c2006-07-01 22:05:56 -04001586 if (!connected(ps)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 usb_unlock_device(dev);
1588 return -ENODEV;
1589 }
1590
1591 switch (cmd) {
1592 case USBDEVFS_CONTROL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001593 snoop(&dev->dev, "%s: CONTROL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 ret = proc_control(ps, p);
1595 if (ret >= 0)
1596 inode->i_mtime = CURRENT_TIME;
1597 break;
1598
1599 case USBDEVFS_BULK:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001600 snoop(&dev->dev, "%s: BULK\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 ret = proc_bulk(ps, p);
1602 if (ret >= 0)
1603 inode->i_mtime = CURRENT_TIME;
1604 break;
1605
1606 case USBDEVFS_RESETEP:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001607 snoop(&dev->dev, "%s: RESETEP\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 ret = proc_resetep(ps, p);
1609 if (ret >= 0)
1610 inode->i_mtime = CURRENT_TIME;
1611 break;
1612
1613 case USBDEVFS_RESET:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001614 snoop(&dev->dev, "%s: RESET\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 ret = proc_resetdevice(ps);
1616 break;
1617
1618 case USBDEVFS_CLEAR_HALT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001619 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 ret = proc_clearhalt(ps, p);
1621 if (ret >= 0)
1622 inode->i_mtime = CURRENT_TIME;
1623 break;
1624
1625 case USBDEVFS_GETDRIVER:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001626 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 ret = proc_getdriver(ps, p);
1628 break;
1629
1630 case USBDEVFS_CONNECTINFO:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001631 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 ret = proc_connectinfo(ps, p);
1633 break;
1634
1635 case USBDEVFS_SETINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001636 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 ret = proc_setintf(ps, p);
1638 break;
1639
1640 case USBDEVFS_SETCONFIGURATION:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001641 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 ret = proc_setconfig(ps, p);
1643 break;
1644
1645 case USBDEVFS_SUBMITURB:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001646 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 ret = proc_submiturb(ps, p);
1648 if (ret >= 0)
1649 inode->i_mtime = CURRENT_TIME;
1650 break;
1651
1652#ifdef CONFIG_COMPAT
1653
1654 case USBDEVFS_SUBMITURB32:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001655 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 ret = proc_submiturb_compat(ps, p);
1657 if (ret >= 0)
1658 inode->i_mtime = CURRENT_TIME;
1659 break;
1660
1661 case USBDEVFS_REAPURB32:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001662 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 ret = proc_reapurb_compat(ps, p);
1664 break;
1665
1666 case USBDEVFS_REAPURBNDELAY32:
Alan Stern4c6e8972009-06-29 11:02:04 -04001667 snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 ret = proc_reapurbnonblock_compat(ps, p);
1669 break;
1670
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001671 case USBDEVFS_IOCTL32:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001672 snoop(&dev->dev, "%s: IOCTL\n", __func__);
Al Viroc714de52006-10-10 22:45:37 +01001673 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001674 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675#endif
1676
1677 case USBDEVFS_DISCARDURB:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001678 snoop(&dev->dev, "%s: DISCARDURB\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 ret = proc_unlinkurb(ps, p);
1680 break;
1681
1682 case USBDEVFS_REAPURB:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001683 snoop(&dev->dev, "%s: REAPURB\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 ret = proc_reapurb(ps, p);
1685 break;
1686
1687 case USBDEVFS_REAPURBNDELAY:
Alan Stern4c6e8972009-06-29 11:02:04 -04001688 snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 ret = proc_reapurbnonblock(ps, p);
1690 break;
1691
1692 case USBDEVFS_DISCSIGNAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001693 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 ret = proc_disconnectsignal(ps, p);
1695 break;
1696
1697 case USBDEVFS_CLAIMINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001698 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 ret = proc_claiminterface(ps, p);
1700 break;
1701
1702 case USBDEVFS_RELEASEINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001703 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 ret = proc_releaseinterface(ps, p);
1705 break;
1706
1707 case USBDEVFS_IOCTL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001708 snoop(&dev->dev, "%s: IOCTL\n", __func__);
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001709 ret = proc_ioctl_default(ps, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 break;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001711
1712 case USBDEVFS_CLAIM_PORT:
1713 snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__);
1714 ret = proc_claim_port(ps, p);
1715 break;
1716
1717 case USBDEVFS_RELEASE_PORT:
1718 snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__);
1719 ret = proc_release_port(ps, p);
1720 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722 usb_unlock_device(dev);
1723 if (ret >= 0)
1724 inode->i_atime = CURRENT_TIME;
1725 return ret;
1726}
1727
1728/* No kernel lock - fine */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001729static unsigned int usbdev_poll(struct file *file,
1730 struct poll_table_struct *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Tobias Klauserec17cf12006-09-13 21:38:41 +02001732 struct dev_state *ps = file->private_data;
1733 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 poll_wait(file, &ps->wait, wait);
1736 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
1737 mask |= POLLOUT | POLLWRNORM;
Alan Stern349710c2006-07-01 22:05:56 -04001738 if (!connected(ps))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 mask |= POLLERR | POLLHUP;
1740 return mask;
1741}
1742
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001743const struct file_operations usbdev_file_operations = {
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -07001744 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 .llseek = usbdev_lseek,
1746 .read = usbdev_read,
1747 .poll = usbdev_poll,
1748 .ioctl = usbdev_ioctl,
1749 .open = usbdev_open,
1750 .release = usbdev_release,
1751};
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001752
Alan Stern501950d2009-01-13 11:33:42 -05001753static void usbdev_remove(struct usb_device *udev)
Alan Sterncd9f0372008-06-24 14:47:04 -04001754{
1755 struct dev_state *ps;
1756 struct siginfo sinfo;
1757
1758 while (!list_empty(&udev->filelist)) {
1759 ps = list_entry(udev->filelist.next, struct dev_state, list);
1760 destroy_all_async(ps);
1761 wake_up_all(&ps->wait);
1762 list_del_init(&ps->list);
1763 if (ps->discsignr) {
1764 sinfo.si_signo = ps->discsignr;
1765 sinfo.si_errno = EPIPE;
1766 sinfo.si_code = SI_ASYNCIO;
1767 sinfo.si_addr = ps->disccontext;
1768 kill_pid_info_as_uid(ps->discsignr, &sinfo,
1769 ps->disc_pid, ps->disc_uid,
1770 ps->disc_euid, ps->secid);
1771 }
1772 }
1773}
1774
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001775#ifdef CONFIG_USB_DEVICE_CLASS
1776static struct class *usb_classdev_class;
1777
1778static int usb_classdev_add(struct usb_device *dev)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001779{
Alan Sterne04199b2008-06-24 14:47:29 -04001780 struct device *cldev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001781
Greg Kroah-Hartmanb0b090e2008-07-21 20:03:34 -07001782 cldev = device_create(usb_classdev_class, &dev->dev, dev->dev.devt,
1783 NULL, "usbdev%d.%d", dev->bus->busnum,
1784 dev->devnum);
Alan Sterne04199b2008-06-24 14:47:29 -04001785 if (IS_ERR(cldev))
1786 return PTR_ERR(cldev);
1787 dev->usb_classdev = cldev;
Akinobu Mita27d39e22006-10-09 18:09:33 +09001788 return 0;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001789}
1790
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001791static void usb_classdev_remove(struct usb_device *dev)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001792{
Alan Sterne04199b2008-06-24 14:47:29 -04001793 if (dev->usb_classdev)
1794 device_unregister(dev->usb_classdev);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001795}
1796
Alan Stern501950d2009-01-13 11:33:42 -05001797#else
1798#define usb_classdev_add(dev) 0
1799#define usb_classdev_remove(dev) do {} while (0)
1800
1801#endif
1802
1803static int usbdev_notify(struct notifier_block *self,
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001804 unsigned long action, void *dev)
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001805{
1806 switch (action) {
1807 case USB_DEVICE_ADD:
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001808 if (usb_classdev_add(dev))
Akinobu Mita27d39e22006-10-09 18:09:33 +09001809 return NOTIFY_BAD;
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001810 break;
1811 case USB_DEVICE_REMOVE:
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001812 usb_classdev_remove(dev);
Alan Stern501950d2009-01-13 11:33:42 -05001813 usbdev_remove(dev);
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001814 break;
1815 }
1816 return NOTIFY_OK;
1817}
1818
1819static struct notifier_block usbdev_nb = {
Alan Stern501950d2009-01-13 11:33:42 -05001820 .notifier_call = usbdev_notify,
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001821};
1822
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -07001823static struct cdev usb_device_cdev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001824
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001825int __init usb_devio_init(void)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001826{
1827 int retval;
1828
Alan Sternfad21bd2005-08-10 15:15:57 -04001829 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001830 "usb_device");
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001831 if (retval) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -07001832 printk(KERN_ERR "Unable to register minors for usb_device\n");
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001833 goto out;
1834 }
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001835 cdev_init(&usb_device_cdev, &usbdev_file_operations);
Alan Sternfad21bd2005-08-10 15:15:57 -04001836 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001837 if (retval) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -07001838 printk(KERN_ERR "Unable to get usb_device major %d\n",
1839 USB_DEVICE_MAJOR);
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001840 goto error_cdev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001841 }
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001842#ifdef CONFIG_USB_DEVICE_CLASS
1843 usb_classdev_class = class_create(THIS_MODULE, "usb_device");
1844 if (IS_ERR(usb_classdev_class)) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -07001845 printk(KERN_ERR "Unable to register usb_device class\n");
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001846 retval = PTR_ERR(usb_classdev_class);
1847 cdev_del(&usb_device_cdev);
1848 usb_classdev_class = NULL;
1849 goto out;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001850 }
Dan Williamse105b8b2008-04-21 10:51:07 -07001851 /* devices of this class shadow the major:minor of their parent
1852 * device, so clear ->dev_kobj to prevent adding duplicate entries
1853 * to /sys/dev
1854 */
1855 usb_classdev_class->dev_kobj = NULL;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001856#endif
Alan Stern501950d2009-01-13 11:33:42 -05001857 usb_register_notify(&usbdev_nb);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001858out:
1859 return retval;
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001860
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001861error_cdev:
1862 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
1863 goto out;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001864}
1865
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001866void usb_devio_cleanup(void)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001867{
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001868 usb_unregister_notify(&usbdev_nb);
Alan Stern501950d2009-01-13 11:33:42 -05001869#ifdef CONFIG_USB_DEVICE_CLASS
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001870 class_destroy(usb_classdev_class);
1871#endif
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001872 cdev_del(&usb_device_cdev);
Alan Sternfad21bd2005-08-10 15:15:57 -04001873 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001874}