blob: 308609039c73e1bbf1252adee9f2e276cc73c852 [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;
328
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800329 spin_lock(&ps->lock);
330 list_move_tail(&as->asynclist, &ps->async_completed);
331 spin_unlock(&ps->lock);
Alan Sterne0152682007-08-24 15:42:52 -0400332 as->status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (as->signr) {
334 sinfo.si_signo = as->signr;
Alan Sterne0152682007-08-24 15:42:52 -0400335 sinfo.si_errno = as->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 sinfo.si_code = SI_ASYNCIO;
337 sinfo.si_addr = as->userurb;
Eric W. Biederman2425c082006-10-02 02:17:28 -0700338 kill_pid_info_as_uid(as->signr, &sinfo, as->pid, as->uid,
David Quigley7a019552006-06-30 01:55:48 -0700339 as->euid, as->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700341 snoop(&urb->dev->dev, "urb complete\n");
342 snoop_urb(urb, as->userurb);
343 wake_up(&ps->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800346static void destroy_async(struct dev_state *ps, struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 struct async *as;
349 unsigned long flags;
350
351 spin_lock_irqsave(&ps->lock, flags);
352 while (!list_empty(list)) {
353 as = list_entry(list->next, struct async, asynclist);
354 list_del_init(&as->asynclist);
355
356 /* drop the spinlock so the completion handler can run */
357 spin_unlock_irqrestore(&ps->lock, flags);
358 usb_kill_urb(as->urb);
359 spin_lock_irqsave(&ps->lock, flags);
360 }
361 spin_unlock_irqrestore(&ps->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800364static void destroy_async_on_interface(struct dev_state *ps,
365 unsigned int ifnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 struct list_head *p, *q, hitlist;
368 unsigned long flags;
369
370 INIT_LIST_HEAD(&hitlist);
371 spin_lock_irqsave(&ps->lock, flags);
372 list_for_each_safe(p, q, &ps->async_pending)
373 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
374 list_move_tail(p, &hitlist);
375 spin_unlock_irqrestore(&ps->lock, flags);
376 destroy_async(ps, &hitlist);
377}
378
Alan Sternd34d9722009-03-09 13:44:48 -0400379static void destroy_all_async(struct dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800381 destroy_async(ps, &ps->async_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384/*
385 * interface claims are made only at the request of user level code,
386 * which can also release them (explicitly or by closing files).
387 * they're also undone when devices disconnect.
388 */
389
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800390static int driver_probe(struct usb_interface *intf,
391 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 return -ENODEV;
394}
395
396static void driver_disconnect(struct usb_interface *intf)
397{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800398 struct dev_state *ps = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
400
401 if (!ps)
402 return;
403
404 /* NOTE: this relies on usbcore having canceled and completed
405 * all pending I/O requests; 2.6 does that.
406 */
407
408 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
409 clear_bit(ifnum, &ps->ifclaimed);
410 else
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -0700411 dev_warn(&intf->dev, "interface number %u out of range\n",
412 ifnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800414 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* force async requests to complete */
417 destroy_async_on_interface(ps, ifnum);
418}
419
Alan Stern2e2eb832007-12-04 14:35:15 -0500420/* The following routines are merely placeholders. There is no way
421 * to inform a user task about suspend or resumes.
422 */
423static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
424{
425 return 0;
426}
427
428static int driver_resume(struct usb_interface *intf)
429{
430 return 0;
431}
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433struct usb_driver usbfs_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 .name = "usbfs",
435 .probe = driver_probe,
436 .disconnect = driver_disconnect,
Alan Stern2e2eb832007-12-04 14:35:15 -0500437 .suspend = driver_suspend,
438 .resume = driver_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439};
440
441static int claimintf(struct dev_state *ps, unsigned int ifnum)
442{
443 struct usb_device *dev = ps->dev;
444 struct usb_interface *intf;
445 int err;
446
447 if (ifnum >= 8*sizeof(ps->ifclaimed))
448 return -EINVAL;
449 /* already claimed */
450 if (test_bit(ifnum, &ps->ifclaimed))
451 return 0;
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 intf = usb_ifnum_to_if(dev, ifnum);
454 if (!intf)
455 err = -ENOENT;
456 else
457 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (err == 0)
459 set_bit(ifnum, &ps->ifclaimed);
460 return err;
461}
462
463static int releaseintf(struct dev_state *ps, unsigned int ifnum)
464{
465 struct usb_device *dev;
466 struct usb_interface *intf;
467 int err;
468
469 err = -EINVAL;
470 if (ifnum >= 8*sizeof(ps->ifclaimed))
471 return err;
472 dev = ps->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 intf = usb_ifnum_to_if(dev, ifnum);
474 if (!intf)
475 err = -ENOENT;
476 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
477 usb_driver_release_interface(&usbfs_driver, intf);
478 err = 0;
479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return err;
481}
482
483static int checkintf(struct dev_state *ps, unsigned int ifnum)
484{
485 if (ps->dev->state != USB_STATE_CONFIGURED)
486 return -EHOSTUNREACH;
487 if (ifnum >= 8*sizeof(ps->ifclaimed))
488 return -EINVAL;
489 if (test_bit(ifnum, &ps->ifclaimed))
490 return 0;
491 /* if not yet claimed, claim it for the driver */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800492 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
493 "interface %u before use\n", task_pid_nr(current),
494 current->comm, ifnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return claimintf(ps, ifnum);
496}
497
498static int findintfep(struct usb_device *dev, unsigned int ep)
499{
500 unsigned int i, j, e;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800501 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct usb_host_interface *alts;
503 struct usb_endpoint_descriptor *endpt;
504
505 if (ep & ~(USB_DIR_IN|0xf))
506 return -EINVAL;
507 if (!dev->actconfig)
508 return -ESRCH;
509 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
510 intf = dev->actconfig->interface[i];
511 for (j = 0; j < intf->num_altsetting; j++) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800512 alts = &intf->altsetting[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
514 endpt = &alts->endpoint[e].desc;
515 if (endpt->bEndpointAddress == ep)
516 return alts->desc.bInterfaceNumber;
517 }
518 }
519 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800520 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800523static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
524 unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 int ret = 0;
527
David Vrabel6da9c992009-02-18 14:43:47 +0000528 if (ps->dev->state != USB_STATE_UNAUTHENTICATED
529 && ps->dev->state != USB_STATE_ADDRESS
Horst Schirmeier24f8b112006-03-11 00:16:55 -0800530 && ps->dev->state != USB_STATE_CONFIGURED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return -EHOSTUNREACH;
532 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
533 return 0;
534
535 index &= 0xff;
536 switch (requesttype & USB_RECIP_MASK) {
537 case USB_RECIP_ENDPOINT:
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800538 ret = findintfep(ps->dev, index);
539 if (ret >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 ret = checkintf(ps, ret);
541 break;
542
543 case USB_RECIP_INTERFACE:
544 ret = checkintf(ps, index);
545 break;
546 }
547 return ret;
548}
549
Alan Stern61ad04a2008-06-24 14:47:12 -0400550static int match_devt(struct device *dev, void *data)
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700551{
David Howellsa80d5ff2008-07-02 12:28:55 +0100552 return dev->devt == (dev_t) (unsigned long) data;
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100553}
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700554
Alan Stern61ad04a2008-06-24 14:47:12 -0400555static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100556{
557 struct device *dev;
558
David Howellsa80d5ff2008-07-02 12:28:55 +0100559 dev = bus_find_device(&usb_bus_type, NULL,
560 (void *) (unsigned long) devt, match_devt);
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100561 if (!dev)
562 return NULL;
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100563 return container_of(dev, struct usb_device, dev);
564}
Greg Kroah-Hartman4592bf52005-06-20 21:15:16 -0700565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566/*
567 * file operations
568 */
569static int usbdev_open(struct inode *inode, struct file *file)
570{
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200571 struct usb_device *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 struct dev_state *ps;
David Howells86a264a2008-11-14 10:39:18 +1100573 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 int ret;
575
Jonathan Corbetb5b4aa62008-05-16 14:25:20 -0600576 lock_kernel();
Alan Stern4a2a8a22006-07-01 22:05:01 -0400577 /* Protect against simultaneous removal or release */
578 mutex_lock(&usbfs_mutex);
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 ret = -ENOMEM;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800581 ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
582 if (!ps)
Alan Stern4a2a8a22006-07-01 22:05:01 -0400583 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 ret = -ENOENT;
Alan Stern61ad04a2008-06-24 14:47:12 -0400586
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100587 /* usbdev device-node */
Kay Sieversfbf82fd2005-07-31 01:05:53 +0200588 if (imajor(inode) == USB_DEVICE_MAJOR)
Alan Stern61ad04a2008-06-24 14:47:12 -0400589 dev = usbdev_lookup_by_devt(inode->i_rdev);
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100590#ifdef CONFIG_USB_DEVICEFS
591 /* procfs file */
Alan Sternd64aac32008-06-24 14:47:19 -0400592 if (!dev) {
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700593 dev = inode->i_private;
Alan Sternd64aac32008-06-24 14:47:19 -0400594 if (dev && dev->usbfs_dentry &&
595 dev->usbfs_dentry->d_inode == inode)
596 usb_get_dev(dev);
597 else
598 dev = NULL;
599 }
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100600#endif
Alan Sternd64aac32008-06-24 14:47:19 -0400601 if (!dev || dev->state == USB_STATE_NOTATTACHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 goto out;
Alan Stern94fcda12006-11-20 11:38:46 -0500603 ret = usb_autoresume_device(dev);
Alan Stern01d883d2006-08-30 15:47:18 -0400604 if (ret)
605 goto out;
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 ret = 0;
608 ps->dev = dev;
609 ps->file = file;
610 spin_lock_init(&ps->lock);
Dan Carpenter316547f2006-12-13 00:03:38 -0800611 INIT_LIST_HEAD(&ps->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 INIT_LIST_HEAD(&ps->async_pending);
613 INIT_LIST_HEAD(&ps->async_completed);
614 init_waitqueue_head(&ps->wait);
615 ps->discsignr = 0;
Eric W. Biederman2425c082006-10-02 02:17:28 -0700616 ps->disc_pid = get_pid(task_pid(current));
David Howells86a264a2008-11-14 10:39:18 +1100617 ps->disc_uid = cred->uid;
618 ps->disc_euid = cred->euid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 ps->disccontext = NULL;
620 ps->ifclaimed = 0;
David Quigley7a019552006-06-30 01:55:48 -0700621 security_task_getsecid(current, &ps->secid);
Oliver Neukum527660a2007-04-20 20:50:48 +0200622 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 list_add_tail(&ps->list, &dev->filelist);
624 file->private_data = ps;
Alan Stern2da41d52008-10-06 11:24:26 -0400625 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
626 current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 out:
Alan Sternd64aac32008-06-24 14:47:19 -0400628 if (ret) {
Alan Stern01d883d2006-08-30 15:47:18 -0400629 kfree(ps);
Alan Sternd64aac32008-06-24 14:47:19 -0400630 usb_put_dev(dev);
631 }
Alan Stern4a2a8a22006-07-01 22:05:01 -0400632 mutex_unlock(&usbfs_mutex);
Jonathan Corbetb5b4aa62008-05-16 14:25:20 -0600633 unlock_kernel();
Alan Stern4a2a8a22006-07-01 22:05:01 -0400634 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
637static int usbdev_release(struct inode *inode, struct file *file)
638{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200639 struct dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 struct usb_device *dev = ps->dev;
641 unsigned int ifnum;
Alan Stern6ff10462009-03-09 13:44:02 -0400642 struct async *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 usb_lock_device(dev);
Alan Stern4a2a8a22006-07-01 22:05:01 -0400645
646 /* Protect against simultaneous open */
647 mutex_lock(&usbfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 list_del_init(&ps->list);
Alan Stern4a2a8a22006-07-01 22:05:01 -0400649 mutex_unlock(&usbfs_mutex);
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
652 ifnum++) {
653 if (test_bit(ifnum, &ps->ifclaimed))
654 releaseintf(ps, ifnum);
655 }
656 destroy_all_async(ps);
Alan Stern94fcda12006-11-20 11:38:46 -0500657 usb_autosuspend_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 usb_unlock_device(dev);
659 usb_put_dev(dev);
Eric W. Biederman2425c082006-10-02 02:17:28 -0700660 put_pid(ps->disc_pid);
Alan Stern6ff10462009-03-09 13:44:02 -0400661
662 as = async_getcompleted(ps);
663 while (as) {
664 free_async(as);
665 as = async_getcompleted(ps);
666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 kfree(ps);
Alan Stern4a2a8a22006-07-01 22:05:01 -0400668 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671static int proc_control(struct dev_state *ps, void __user *arg)
672{
673 struct usb_device *dev = ps->dev;
674 struct usbdevfs_ctrltransfer ctrl;
675 unsigned int tmo;
676 unsigned char *tbuf;
Andrew Mortonff66e3c2008-03-12 13:32:24 -0700677 unsigned wLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 int i, j, ret;
679
680 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
681 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800682 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex);
683 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return ret;
Andrew Mortonff66e3c2008-03-12 13:32:24 -0700685 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
686 if (wLength > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800688 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
689 if (!tbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return -ENOMEM;
691 tmo = ctrl.timeout;
692 if (ctrl.bRequestType & 0x80) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800693 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
694 ctrl.wLength)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 free_page((unsigned long)tbuf);
696 return -EINVAL;
697 }
Alan Sternfe0410c2005-07-29 12:16:58 -0700698 snoop(&dev->dev, "control read: bRequest=%02x "
699 "bRrequestType=%02x wValue=%04x "
700 "wIndex=%04x wLength=%04x\n",
701 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
702 ctrl.wIndex, ctrl.wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 usb_unlock_device(dev);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800705 i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest,
706 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
707 tbuf, ctrl.wLength, tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 usb_lock_device(dev);
709 if ((i > 0) && ctrl.wLength) {
710 if (usbfs_snoop) {
711 dev_info(&dev->dev, "control read: data ");
Alan Sternfe0410c2005-07-29 12:16:58 -0700712 for (j = 0; j < i; ++j)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800713 printk("%02x ", (u8)(tbuf)[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 printk("\n");
715 }
Alan Sternfe0410c2005-07-29 12:16:58 -0700716 if (copy_to_user(ctrl.data, tbuf, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 free_page((unsigned long)tbuf);
718 return -EFAULT;
719 }
720 }
721 } else {
722 if (ctrl.wLength) {
723 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
724 free_page((unsigned long)tbuf);
725 return -EFAULT;
726 }
727 }
Alan Sternfe0410c2005-07-29 12:16:58 -0700728 snoop(&dev->dev, "control write: bRequest=%02x "
729 "bRrequestType=%02x wValue=%04x "
730 "wIndex=%04x wLength=%04x\n",
731 ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
732 ctrl.wIndex, ctrl.wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (usbfs_snoop) {
734 dev_info(&dev->dev, "control write: data: ");
735 for (j = 0; j < ctrl.wLength; ++j)
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700736 printk("%02x ", (unsigned char)(tbuf)[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 printk("\n");
738 }
739 usb_unlock_device(dev);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800740 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
741 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
742 tbuf, ctrl.wLength, tmo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 usb_lock_device(dev);
744 }
745 free_page((unsigned long)tbuf);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800746 if (i < 0 && i != -EPIPE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
748 "failed cmd %s rqt %u rq %u len %u ret %d\n",
749 current->comm, ctrl.bRequestType, ctrl.bRequest,
750 ctrl.wLength, i);
751 }
752 return i;
753}
754
755static int proc_bulk(struct dev_state *ps, void __user *arg)
756{
757 struct usb_device *dev = ps->dev;
758 struct usbdevfs_bulktransfer bulk;
759 unsigned int tmo, len1, pipe;
760 int len2;
761 unsigned char *tbuf;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700762 int i, j, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 if (copy_from_user(&bulk, arg, sizeof(bulk)))
765 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800766 ret = findintfep(ps->dev, bulk.ep);
767 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800769 ret = checkintf(ps, ret);
770 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return ret;
772 if (bulk.ep & USB_DIR_IN)
773 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
774 else
775 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
776 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
777 return -EINVAL;
778 len1 = bulk.len;
779 if (len1 > MAX_USBFS_BUFFER_SIZE)
780 return -EINVAL;
781 if (!(tbuf = kmalloc(len1, GFP_KERNEL)))
782 return -ENOMEM;
783 tmo = bulk.timeout;
784 if (bulk.ep & 0x80) {
785 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
786 kfree(tbuf);
787 return -EINVAL;
788 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700789 snoop(&dev->dev, "bulk read: len=0x%02x timeout=%04d\n",
790 bulk.len, bulk.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 usb_unlock_device(dev);
792 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
793 usb_lock_device(dev);
794 if (!i && len2) {
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700795 if (usbfs_snoop) {
796 dev_info(&dev->dev, "bulk read: data ");
797 for (j = 0; j < len2; ++j)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800798 printk("%02x ", (u8)(tbuf)[j]);
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700799 printk("\n");
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (copy_to_user(bulk.data, tbuf, len2)) {
802 kfree(tbuf);
803 return -EFAULT;
804 }
805 }
806 } else {
807 if (len1) {
808 if (copy_from_user(tbuf, bulk.data, len1)) {
809 kfree(tbuf);
810 return -EFAULT;
811 }
812 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -0700813 snoop(&dev->dev, "bulk write: len=0x%02x timeout=%04d\n",
814 bulk.len, bulk.timeout);
815 if (usbfs_snoop) {
816 dev_info(&dev->dev, "bulk write: data: ");
817 for (j = 0; j < len1; ++j)
818 printk("%02x ", (unsigned char)(tbuf)[j]);
819 printk("\n");
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 usb_unlock_device(dev);
822 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
823 usb_lock_device(dev);
824 }
825 kfree(tbuf);
826 if (i < 0)
827 return i;
828 return len2;
829}
830
831static int proc_resetep(struct dev_state *ps, void __user *arg)
832{
833 unsigned int ep;
834 int ret;
835
836 if (get_user(ep, (unsigned int __user *)arg))
837 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800838 ret = findintfep(ps->dev, ep);
839 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800841 ret = checkintf(ps, ret);
842 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return ret;
David Vrabel3444b262009-04-08 17:36:28 +0000844 usb_reset_endpoint(ps->dev, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return 0;
846}
847
848static int proc_clearhalt(struct dev_state *ps, void __user *arg)
849{
850 unsigned int ep;
851 int pipe;
852 int ret;
853
854 if (get_user(ep, (unsigned int __user *)arg))
855 return -EFAULT;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800856 ret = findintfep(ps->dev, ep);
857 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return ret;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800859 ret = checkintf(ps, ret);
860 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return ret;
862 if (ep & USB_DIR_IN)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800863 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
864 else
865 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 return usb_clear_halt(ps->dev, pipe);
868}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870static int proc_getdriver(struct dev_state *ps, void __user *arg)
871{
872 struct usbdevfs_getdriver gd;
873 struct usb_interface *intf;
874 int ret;
875
876 if (copy_from_user(&gd, arg, sizeof(gd)))
877 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 intf = usb_ifnum_to_if(ps->dev, gd.interface);
879 if (!intf || !intf->dev.driver)
880 ret = -ENODATA;
881 else {
882 strncpy(gd.driver, intf->dev.driver->name,
883 sizeof(gd.driver));
884 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return ret;
887}
888
889static int proc_connectinfo(struct dev_state *ps, void __user *arg)
890{
891 struct usbdevfs_connectinfo ci;
892
893 ci.devnum = ps->dev->devnum;
894 ci.slow = ps->dev->speed == USB_SPEED_LOW;
895 if (copy_to_user(arg, &ci, sizeof(ci)))
896 return -EFAULT;
897 return 0;
898}
899
900static int proc_resetdevice(struct dev_state *ps)
901{
Ming Lei742120c2008-06-18 22:00:29 +0800902 return usb_reset_device(ps->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
905static int proc_setintf(struct dev_state *ps, void __user *arg)
906{
907 struct usbdevfs_setinterface setintf;
908 int ret;
909
910 if (copy_from_user(&setintf, arg, sizeof(setintf)))
911 return -EFAULT;
912 if ((ret = checkintf(ps, setintf.interface)))
913 return ret;
914 return usb_set_interface(ps->dev, setintf.interface,
915 setintf.altsetting);
916}
917
918static int proc_setconfig(struct dev_state *ps, void __user *arg)
919{
Alan Stern3f141e22007-02-08 16:40:43 -0500920 int u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int status = 0;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800922 struct usb_host_config *actconfig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Alan Stern3f141e22007-02-08 16:40:43 -0500924 if (get_user(u, (int __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return -EFAULT;
926
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800927 actconfig = ps->dev->actconfig;
928
929 /* Don't touch the device if any interfaces are claimed.
930 * It could interfere with other drivers' operations, and if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 * an interface is claimed by usbfs it could easily deadlock.
932 */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800933 if (actconfig) {
934 int i;
935
936 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
937 if (usb_interface_claimed(actconfig->interface[i])) {
938 dev_warn(&ps->dev->dev,
David Brownell72ebddb2005-04-11 18:34:17 -0700939 "usbfs: interface %d claimed by %s "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 "while '%s' sets config #%d\n",
941 actconfig->interface[i]
942 ->cur_altsetting
943 ->desc.bInterfaceNumber,
David Brownell72ebddb2005-04-11 18:34:17 -0700944 actconfig->interface[i]
945 ->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 current->comm, u);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800947 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800950 }
951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
954 * so avoid usb_set_configuration()'s kick to sysfs
955 */
956 if (status == 0) {
957 if (actconfig && actconfig->desc.bConfigurationValue == u)
958 status = usb_reset_configuration(ps->dev);
959 else
960 status = usb_set_configuration(ps->dev, u);
961 }
962
963 return status;
964}
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800967 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
968 void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 struct usbdevfs_iso_packet_desc *isopkt = NULL;
971 struct usb_host_endpoint *ep;
972 struct async *as;
973 struct usb_ctrlrequest *dr = NULL;
David Howells86a264a2008-11-14 10:39:18 +1100974 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 unsigned int u, totlen, isofrmlen;
Alan Stern97b9eb92007-02-26 14:56:14 -0500976 int ret, ifnum = -1;
Alan Stern93cf9b92007-07-30 17:09:28 -0400977 int is_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Alan Stern14722ef2008-04-17 10:18:11 -0400979 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
980 USBDEVFS_URB_SHORT_NOT_OK |
981 USBDEVFS_URB_NO_FSBR |
982 USBDEVFS_URB_ZERO_PACKET |
983 USBDEVFS_URB_NO_INTERRUPT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return -EINVAL;
985 if (!uurb->buffer)
986 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800987 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
988 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
989 ifnum = findintfep(ps->dev, uurb->endpoint);
990 if (ifnum < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return ifnum;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -0800992 ret = checkintf(ps, ifnum);
993 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return ret;
995 }
Alan Stern93cf9b92007-07-30 17:09:28 -0400996 if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
997 is_in = 1;
998 ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
999 } else {
1000 is_in = 0;
1001 ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
1002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (!ep)
1004 return -ENOENT;
1005 switch(uurb->type) {
1006 case USBDEVFS_URB_TYPE_CONTROL:
Alan Stern93cf9b92007-07-30 17:09:28 -04001007 if (!usb_endpoint_xfer_control(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001009 /* min 8 byte setup packet,
1010 * max 8 byte setup plus an arbitrary data stage */
1011 if (uurb->buffer_length < 8 ||
1012 uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001014 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1015 if (!dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return -ENOMEM;
1017 if (copy_from_user(dr, uurb->buffer, 8)) {
1018 kfree(dr);
1019 return -EFAULT;
1020 }
1021 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
1022 kfree(dr);
1023 return -EINVAL;
1024 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001025 ret = check_ctrlrecip(ps, dr->bRequestType,
1026 le16_to_cpup(&dr->wIndex));
1027 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 kfree(dr);
1029 return ret;
1030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 uurb->number_of_packets = 0;
1032 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1033 uurb->buffer += 8;
Alan Stern93cf9b92007-07-30 17:09:28 -04001034 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1035 is_in = 1;
1036 uurb->endpoint |= USB_DIR_IN;
1037 } else {
1038 is_in = 0;
1039 uurb->endpoint &= ~USB_DIR_IN;
1040 }
1041 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1042 uurb->buffer, uurb->buffer_length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 kfree(dr);
1044 return -EFAULT;
1045 }
Chris Freydf251b82006-12-16 02:37:42 -05001046 snoop(&ps->dev->dev, "control urb: bRequest=%02x "
1047 "bRrequestType=%02x wValue=%04x "
1048 "wIndex=%04x wLength=%04x\n",
Alan Stern93cf9b92007-07-30 17:09:28 -04001049 dr->bRequest, dr->bRequestType,
1050 __le16_to_cpup(&dr->wValue),
1051 __le16_to_cpup(&dr->wIndex),
1052 __le16_to_cpup(&dr->wLength));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 break;
1054
1055 case USBDEVFS_URB_TYPE_BULK:
Alan Stern93cf9b92007-07-30 17:09:28 -04001056 switch (usb_endpoint_type(&ep->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 case USB_ENDPOINT_XFER_CONTROL:
1058 case USB_ENDPOINT_XFER_ISOC:
1059 return -EINVAL;
1060 /* allow single-shot interrupt transfers, at bogus rates */
1061 }
1062 uurb->number_of_packets = 0;
1063 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1064 return -EINVAL;
Alan Stern93cf9b92007-07-30 17:09:28 -04001065 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1066 uurb->buffer, uurb->buffer_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return -EFAULT;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -07001068 snoop(&ps->dev->dev, "bulk urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 break;
1070
1071 case USBDEVFS_URB_TYPE_ISO:
1072 /* arbitrary limit */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001073 if (uurb->number_of_packets < 1 ||
1074 uurb->number_of_packets > 128)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return -EINVAL;
Alan Stern93cf9b92007-07-30 17:09:28 -04001076 if (!usb_endpoint_xfer_isoc(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return -EINVAL;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001078 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1079 uurb->number_of_packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
1081 return -ENOMEM;
1082 if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
1083 kfree(isopkt);
1084 return -EFAULT;
1085 }
1086 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001087 /* arbitrary limit,
1088 * sufficient for USB 2.0 high-bandwidth iso */
Micah Dowty36122422006-05-19 11:26:24 -07001089 if (isopkt[u].length > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 kfree(isopkt);
1091 return -EINVAL;
1092 }
1093 totlen += isopkt[u].length;
1094 }
1095 if (totlen > 32768) {
1096 kfree(isopkt);
1097 return -EINVAL;
1098 }
1099 uurb->buffer_length = totlen;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -07001100 snoop(&ps->dev->dev, "iso urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 break;
1102
1103 case USBDEVFS_URB_TYPE_INTERRUPT:
1104 uurb->number_of_packets = 0;
Alan Stern93cf9b92007-07-30 17:09:28 -04001105 if (!usb_endpoint_xfer_int(&ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1108 return -EINVAL;
Alan Stern93cf9b92007-07-30 17:09:28 -04001109 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1110 uurb->buffer, uurb->buffer_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return -EFAULT;
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -07001112 snoop(&ps->dev->dev, "interrupt urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 break;
1114
1115 default:
1116 return -EINVAL;
1117 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001118 as = alloc_async(uurb->number_of_packets);
1119 if (!as) {
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001120 kfree(isopkt);
1121 kfree(dr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return -ENOMEM;
1123 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001124 as->urb->transfer_buffer = kmalloc(uurb->buffer_length, GFP_KERNEL);
1125 if (!as->urb->transfer_buffer) {
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001126 kfree(isopkt);
1127 kfree(dr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 free_async(as);
1129 return -ENOMEM;
1130 }
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001131 as->urb->dev = ps->dev;
1132 as->urb->pipe = (uurb->type << 30) |
Alan Stern93cf9b92007-07-30 17:09:28 -04001133 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1134 (uurb->endpoint & USB_DIR_IN);
Alan Stern14722ef2008-04-17 10:18:11 -04001135
1136 /* This tedious sequence is necessary because the URB_* flags
1137 * are internal to the kernel and subject to change, whereas
1138 * the USBDEVFS_URB_* flags are a user API and must not be changed.
1139 */
1140 u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
1141 if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
1142 u |= URB_ISO_ASAP;
1143 if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
1144 u |= URB_SHORT_NOT_OK;
1145 if (uurb->flags & USBDEVFS_URB_NO_FSBR)
1146 u |= URB_NO_FSBR;
1147 if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1148 u |= URB_ZERO_PACKET;
1149 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
1150 u |= URB_NO_INTERRUPT;
1151 as->urb->transfer_flags = u;
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 as->urb->transfer_buffer_length = uurb->buffer_length;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001154 as->urb->setup_packet = (unsigned char *)dr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 as->urb->start_frame = uurb->start_frame;
1156 as->urb->number_of_packets = uurb->number_of_packets;
Alan Stern97b9eb92007-02-26 14:56:14 -05001157 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1158 ps->dev->speed == USB_SPEED_HIGH)
1159 as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
1160 else
1161 as->urb->interval = ep->desc.bInterval;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001162 as->urb->context = as;
1163 as->urb->complete = async_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 for (totlen = u = 0; u < uurb->number_of_packets; u++) {
1165 as->urb->iso_frame_desc[u].offset = totlen;
1166 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1167 totlen += isopkt[u].length;
1168 }
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001169 kfree(isopkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 as->ps = ps;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001171 as->userurb = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (uurb->endpoint & USB_DIR_IN)
1173 as->userbuffer = uurb->buffer;
1174 else
1175 as->userbuffer = NULL;
1176 as->signr = uurb->signr;
1177 as->ifnum = ifnum;
Eric W. Biederman2425c082006-10-02 02:17:28 -07001178 as->pid = get_pid(task_pid(current));
David Howells86a264a2008-11-14 10:39:18 +11001179 as->uid = cred->uid;
1180 as->euid = cred->euid;
David Quigley7a019552006-06-30 01:55:48 -07001181 security_task_getsecid(current, &as->secid);
Alan Stern93cf9b92007-07-30 17:09:28 -04001182 if (!is_in) {
1183 if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
1184 as->urb->transfer_buffer_length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 free_async(as);
1186 return -EFAULT;
1187 }
1188 }
Greg Kroah-Hartmane639dd32005-06-20 21:15:16 -07001189 snoop_urb(as->urb, as->userurb);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001190 async_newpending(as);
1191 if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
1192 dev_printk(KERN_DEBUG, &ps->dev->dev,
1193 "usbfs: usb_submit_urb returned %d\n", ret);
1194 async_removepending(as);
1195 free_async(as);
1196 return ret;
1197 }
1198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
1201static int proc_submiturb(struct dev_state *ps, void __user *arg)
1202{
1203 struct usbdevfs_urb uurb;
1204
1205 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1206 return -EFAULT;
1207
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001208 return proc_do_submiturb(ps, &uurb,
1209 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1210 arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
1213static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
1214{
1215 struct async *as;
1216
1217 as = async_getpending(ps, arg);
1218 if (!as)
1219 return -EINVAL;
1220 usb_kill_urb(as->urb);
1221 return 0;
1222}
1223
1224static int processcompl(struct async *as, void __user * __user *arg)
1225{
1226 struct urb *urb = as->urb;
1227 struct usbdevfs_urb __user *userurb = as->userurb;
1228 void __user *addr = as->userurb;
1229 unsigned int i;
1230
1231 if (as->userbuffer)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001232 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1233 urb->transfer_buffer_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return -EFAULT;
Alan Sterne0152682007-08-24 15:42:52 -04001235 if (put_user(as->status, &userurb->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return -EFAULT;
1237 if (put_user(urb->actual_length, &userurb->actual_length))
1238 return -EFAULT;
1239 if (put_user(urb->error_count, &userurb->error_count))
1240 return -EFAULT;
1241
Alan Stern93cf9b92007-07-30 17:09:28 -04001242 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
Christopher Li668a9542005-04-18 17:39:26 -07001243 for (i = 0; i < urb->number_of_packets; i++) {
1244 if (put_user(urb->iso_frame_desc[i].actual_length,
1245 &userurb->iso_frame_desc[i].actual_length))
1246 return -EFAULT;
1247 if (put_user(urb->iso_frame_desc[i].status,
1248 &userurb->iso_frame_desc[i].status))
1249 return -EFAULT;
1250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252
1253 free_async(as);
1254
1255 if (put_user(addr, (void __user * __user *)arg))
1256 return -EFAULT;
1257 return 0;
1258}
1259
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001260static struct async *reap_as(struct dev_state *ps)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001262 DECLARE_WAITQUEUE(wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 struct async *as = NULL;
1264 struct usb_device *dev = ps->dev;
1265
1266 add_wait_queue(&ps->wait, &wait);
1267 for (;;) {
1268 __set_current_state(TASK_INTERRUPTIBLE);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001269 as = async_getcompleted(ps);
1270 if (as)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 break;
1272 if (signal_pending(current))
1273 break;
1274 usb_unlock_device(dev);
1275 schedule();
1276 usb_lock_device(dev);
1277 }
1278 remove_wait_queue(&ps->wait, &wait);
1279 set_current_state(TASK_RUNNING);
1280 return as;
1281}
1282
1283static int proc_reapurb(struct dev_state *ps, void __user *arg)
1284{
1285 struct async *as = reap_as(ps);
1286 if (as)
1287 return processcompl(as, (void __user * __user *)arg);
1288 if (signal_pending(current))
1289 return -EINTR;
1290 return -EIO;
1291}
1292
1293static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
1294{
1295 struct async *as;
1296
1297 if (!(as = async_getcompleted(ps)))
1298 return -EAGAIN;
1299 return processcompl(as, (void __user * __user *)arg);
1300}
1301
1302#ifdef CONFIG_COMPAT
1303
1304static int get_urb32(struct usbdevfs_urb *kurb,
1305 struct usbdevfs_urb32 __user *uurb)
1306{
1307 __u32 uptr;
1308 if (get_user(kurb->type, &uurb->type) ||
1309 __get_user(kurb->endpoint, &uurb->endpoint) ||
1310 __get_user(kurb->status, &uurb->status) ||
1311 __get_user(kurb->flags, &uurb->flags) ||
1312 __get_user(kurb->buffer_length, &uurb->buffer_length) ||
1313 __get_user(kurb->actual_length, &uurb->actual_length) ||
1314 __get_user(kurb->start_frame, &uurb->start_frame) ||
1315 __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
1316 __get_user(kurb->error_count, &uurb->error_count) ||
1317 __get_user(kurb->signr, &uurb->signr))
1318 return -EFAULT;
1319
1320 if (__get_user(uptr, &uurb->buffer))
1321 return -EFAULT;
1322 kurb->buffer = compat_ptr(uptr);
Mark Lorded0c7722009-01-02 02:48:24 -05001323 if (__get_user(uptr, &uurb->usercontext))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return -EFAULT;
1325 kurb->usercontext = compat_ptr(uptr);
1326
1327 return 0;
1328}
1329
1330static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
1331{
1332 struct usbdevfs_urb uurb;
1333
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001334 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return -EFAULT;
1336
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001337 return proc_do_submiturb(ps, &uurb,
1338 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
1339 arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
1342static int processcompl_compat(struct async *as, void __user * __user *arg)
1343{
1344 struct urb *urb = as->urb;
1345 struct usbdevfs_urb32 __user *userurb = as->userurb;
1346 void __user *addr = as->userurb;
1347 unsigned int i;
1348
1349 if (as->userbuffer)
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001350 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1351 urb->transfer_buffer_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return -EFAULT;
Alan Sterne0152682007-08-24 15:42:52 -04001353 if (put_user(as->status, &userurb->status))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return -EFAULT;
1355 if (put_user(urb->actual_length, &userurb->actual_length))
1356 return -EFAULT;
1357 if (put_user(urb->error_count, &userurb->error_count))
1358 return -EFAULT;
1359
Alan Stern93cf9b92007-07-30 17:09:28 -04001360 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
Christopher Li668a9542005-04-18 17:39:26 -07001361 for (i = 0; i < urb->number_of_packets; i++) {
1362 if (put_user(urb->iso_frame_desc[i].actual_length,
1363 &userurb->iso_frame_desc[i].actual_length))
1364 return -EFAULT;
1365 if (put_user(urb->iso_frame_desc[i].status,
1366 &userurb->iso_frame_desc[i].status))
1367 return -EFAULT;
1368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370
1371 free_async(as);
Al Viroc714de52006-10-10 22:45:37 +01001372 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return -EFAULT;
1374 return 0;
1375}
1376
1377static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
1378{
1379 struct async *as = reap_as(ps);
1380 if (as)
1381 return processcompl_compat(as, (void __user * __user *)arg);
1382 if (signal_pending(current))
1383 return -EINTR;
1384 return -EIO;
1385}
1386
1387static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
1388{
1389 struct async *as;
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (!(as = async_getcompleted(ps)))
1392 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return processcompl_compat(as, (void __user * __user *)arg);
1394}
1395
1396#endif
1397
1398static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
1399{
1400 struct usbdevfs_disconnectsignal ds;
1401
1402 if (copy_from_user(&ds, arg, sizeof(ds)))
1403 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 ps->discsignr = ds.signr;
1405 ps->disccontext = ds.context;
1406 return 0;
1407}
1408
1409static int proc_claiminterface(struct dev_state *ps, void __user *arg)
1410{
1411 unsigned int ifnum;
1412
1413 if (get_user(ifnum, (unsigned int __user *)arg))
1414 return -EFAULT;
1415 return claimintf(ps, ifnum);
1416}
1417
1418static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
1419{
1420 unsigned int ifnum;
1421 int ret;
1422
1423 if (get_user(ifnum, (unsigned int __user *)arg))
1424 return -EFAULT;
1425 if ((ret = releaseintf(ps, ifnum)) < 0)
1426 return ret;
1427 destroy_async_on_interface (ps, ifnum);
1428 return 0;
1429}
1430
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001431static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 int size;
1434 void *buf = NULL;
1435 int retval = 0;
1436 struct usb_interface *intf = NULL;
1437 struct usb_driver *driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001439 /* alloc buffer */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001440 if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
1441 if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return -ENOMEM;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001443 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001444 if (copy_from_user(buf, ctl->data, size)) {
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001445 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return -EFAULT;
1447 }
1448 } else {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001449 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451 }
1452
Alan Stern349710c2006-07-01 22:05:56 -04001453 if (!connected(ps)) {
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001454 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return -ENODEV;
1456 }
1457
1458 if (ps->dev->state != USB_STATE_CONFIGURED)
1459 retval = -EHOSTUNREACH;
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001460 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
1461 retval = -EINVAL;
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001462 else switch (ctl->ioctl_code) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 /* disconnect kernel driver from interface */
1465 case USBDEVFS_DISCONNECT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (intf->dev.driver) {
1467 driver = to_usb_driver(intf->dev.driver);
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001468 dev_dbg(&intf->dev, "disconnect by usbfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 usb_driver_release_interface(driver, intf);
1470 } else
1471 retval = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 break;
1473
1474 /* let kernel drivers try to (re)bind to the interface */
1475 case USBDEVFS_CONNECT:
Alan Stern885e9742007-12-03 15:42:10 -05001476 if (!intf->dev.driver)
1477 retval = device_attach(&intf->dev);
1478 else
1479 retval = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 break;
1481
1482 /* talk directly to the interface's driver */
1483 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (intf->dev.driver)
1485 driver = to_usb_driver(intf->dev.driver);
1486 if (driver == NULL || driver->ioctl == NULL) {
1487 retval = -ENOTTY;
1488 } else {
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001489 retval = driver->ioctl(intf, ctl->ioctl_code, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 if (retval == -ENOIOCTLCMD)
1491 retval = -ENOTTY;
1492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494
1495 /* cleanup and return */
1496 if (retval >= 0
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001497 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 && size > 0
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001499 && copy_to_user(ctl->data, buf, size) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 retval = -EFAULT;
Jesper Juhl6fd19f42005-04-18 17:39:33 -07001501
1502 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return retval;
1504}
1505
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001506static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
1507{
1508 struct usbdevfs_ioctl ctrl;
1509
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001510 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001511 return -EFAULT;
1512 return proc_ioctl(ps, &ctrl);
1513}
1514
1515#ifdef CONFIG_COMPAT
Andrew Morton777da592005-11-17 09:47:02 -08001516static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001517{
1518 struct usbdevfs_ioctl32 __user *uioc;
1519 struct usbdevfs_ioctl ctrl;
1520 u32 udata;
1521
Andrew Morton058120d2005-11-17 09:47:49 -08001522 uioc = compat_ptr((long)arg);
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001523 if (get_user(ctrl.ifno, &uioc->ifno) ||
1524 get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
1525 __get_user(udata, &uioc->data))
1526 return -EFAULT;
1527 ctrl.data = compat_ptr(udata);
1528
1529 return proc_ioctl(ps, &ctrl);
1530}
1531#endif
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533/*
1534 * NOTE: All requests here that have interface numbers as parameters
1535 * are assuming that somehow the configuration has been prevented from
1536 * changing. But there's no mechanism to ensure that...
1537 */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001538static int usbdev_ioctl(struct inode *inode, struct file *file,
1539 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Tobias Klauserec17cf12006-09-13 21:38:41 +02001541 struct dev_state *ps = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 struct usb_device *dev = ps->dev;
1543 void __user *p = (void __user *)arg;
1544 int ret = -ENOTTY;
1545
1546 if (!(file->f_mode & FMODE_WRITE))
1547 return -EPERM;
1548 usb_lock_device(dev);
Alan Stern349710c2006-07-01 22:05:56 -04001549 if (!connected(ps)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 usb_unlock_device(dev);
1551 return -ENODEV;
1552 }
1553
1554 switch (cmd) {
1555 case USBDEVFS_CONTROL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001556 snoop(&dev->dev, "%s: CONTROL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 ret = proc_control(ps, p);
1558 if (ret >= 0)
1559 inode->i_mtime = CURRENT_TIME;
1560 break;
1561
1562 case USBDEVFS_BULK:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001563 snoop(&dev->dev, "%s: BULK\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 ret = proc_bulk(ps, p);
1565 if (ret >= 0)
1566 inode->i_mtime = CURRENT_TIME;
1567 break;
1568
1569 case USBDEVFS_RESETEP:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001570 snoop(&dev->dev, "%s: RESETEP\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 ret = proc_resetep(ps, p);
1572 if (ret >= 0)
1573 inode->i_mtime = CURRENT_TIME;
1574 break;
1575
1576 case USBDEVFS_RESET:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001577 snoop(&dev->dev, "%s: RESET\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 ret = proc_resetdevice(ps);
1579 break;
1580
1581 case USBDEVFS_CLEAR_HALT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001582 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 ret = proc_clearhalt(ps, p);
1584 if (ret >= 0)
1585 inode->i_mtime = CURRENT_TIME;
1586 break;
1587
1588 case USBDEVFS_GETDRIVER:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001589 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 ret = proc_getdriver(ps, p);
1591 break;
1592
1593 case USBDEVFS_CONNECTINFO:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001594 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 ret = proc_connectinfo(ps, p);
1596 break;
1597
1598 case USBDEVFS_SETINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001599 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 ret = proc_setintf(ps, p);
1601 break;
1602
1603 case USBDEVFS_SETCONFIGURATION:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001604 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 ret = proc_setconfig(ps, p);
1606 break;
1607
1608 case USBDEVFS_SUBMITURB:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001609 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 ret = proc_submiturb(ps, p);
1611 if (ret >= 0)
1612 inode->i_mtime = CURRENT_TIME;
1613 break;
1614
1615#ifdef CONFIG_COMPAT
1616
1617 case USBDEVFS_SUBMITURB32:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001618 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 ret = proc_submiturb_compat(ps, p);
1620 if (ret >= 0)
1621 inode->i_mtime = CURRENT_TIME;
1622 break;
1623
1624 case USBDEVFS_REAPURB32:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001625 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 ret = proc_reapurb_compat(ps, p);
1627 break;
1628
1629 case USBDEVFS_REAPURBNDELAY32:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001630 snoop(&dev->dev, "%s: REAPURBDELAY32\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 ret = proc_reapurbnonblock_compat(ps, p);
1632 break;
1633
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001634 case USBDEVFS_IOCTL32:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001635 snoop(&dev->dev, "%s: IOCTL\n", __func__);
Al Viroc714de52006-10-10 22:45:37 +01001636 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001637 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638#endif
1639
1640 case USBDEVFS_DISCARDURB:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001641 snoop(&dev->dev, "%s: DISCARDURB\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 ret = proc_unlinkurb(ps, p);
1643 break;
1644
1645 case USBDEVFS_REAPURB:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001646 snoop(&dev->dev, "%s: REAPURB\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 ret = proc_reapurb(ps, p);
1648 break;
1649
1650 case USBDEVFS_REAPURBNDELAY:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001651 snoop(&dev->dev, "%s: REAPURBDELAY\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 ret = proc_reapurbnonblock(ps, p);
1653 break;
1654
1655 case USBDEVFS_DISCSIGNAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001656 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 ret = proc_disconnectsignal(ps, p);
1658 break;
1659
1660 case USBDEVFS_CLAIMINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001661 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 ret = proc_claiminterface(ps, p);
1663 break;
1664
1665 case USBDEVFS_RELEASEINTERFACE:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001666 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 ret = proc_releaseinterface(ps, p);
1668 break;
1669
1670 case USBDEVFS_IOCTL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001671 snoop(&dev->dev, "%s: IOCTL\n", __func__);
Pete Zaitcevc36fc882005-10-17 18:15:54 -07001672 ret = proc_ioctl_default(ps, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 break;
1674 }
1675 usb_unlock_device(dev);
1676 if (ret >= 0)
1677 inode->i_atime = CURRENT_TIME;
1678 return ret;
1679}
1680
1681/* No kernel lock - fine */
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001682static unsigned int usbdev_poll(struct file *file,
1683 struct poll_table_struct *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Tobias Klauserec17cf12006-09-13 21:38:41 +02001685 struct dev_state *ps = file->private_data;
1686 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 poll_wait(file, &ps->wait, wait);
1689 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
1690 mask |= POLLOUT | POLLWRNORM;
Alan Stern349710c2006-07-01 22:05:56 -04001691 if (!connected(ps))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 mask |= POLLERR | POLLHUP;
1693 return mask;
1694}
1695
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001696const struct file_operations usbdev_file_operations = {
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -07001697 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 .llseek = usbdev_lseek,
1699 .read = usbdev_read,
1700 .poll = usbdev_poll,
1701 .ioctl = usbdev_ioctl,
1702 .open = usbdev_open,
1703 .release = usbdev_release,
1704};
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001705
Alan Stern501950d2009-01-13 11:33:42 -05001706static void usbdev_remove(struct usb_device *udev)
Alan Sterncd9f0372008-06-24 14:47:04 -04001707{
1708 struct dev_state *ps;
1709 struct siginfo sinfo;
1710
1711 while (!list_empty(&udev->filelist)) {
1712 ps = list_entry(udev->filelist.next, struct dev_state, list);
1713 destroy_all_async(ps);
1714 wake_up_all(&ps->wait);
1715 list_del_init(&ps->list);
1716 if (ps->discsignr) {
1717 sinfo.si_signo = ps->discsignr;
1718 sinfo.si_errno = EPIPE;
1719 sinfo.si_code = SI_ASYNCIO;
1720 sinfo.si_addr = ps->disccontext;
1721 kill_pid_info_as_uid(ps->discsignr, &sinfo,
1722 ps->disc_pid, ps->disc_uid,
1723 ps->disc_euid, ps->secid);
1724 }
1725 }
1726}
1727
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001728#ifdef CONFIG_USB_DEVICE_CLASS
1729static struct class *usb_classdev_class;
1730
1731static int usb_classdev_add(struct usb_device *dev)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001732{
Alan Sterne04199b2008-06-24 14:47:29 -04001733 struct device *cldev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001734
Greg Kroah-Hartmanb0b090e2008-07-21 20:03:34 -07001735 cldev = device_create(usb_classdev_class, &dev->dev, dev->dev.devt,
1736 NULL, "usbdev%d.%d", dev->bus->busnum,
1737 dev->devnum);
Alan Sterne04199b2008-06-24 14:47:29 -04001738 if (IS_ERR(cldev))
1739 return PTR_ERR(cldev);
1740 dev->usb_classdev = cldev;
Akinobu Mita27d39e22006-10-09 18:09:33 +09001741 return 0;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001742}
1743
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001744static void usb_classdev_remove(struct usb_device *dev)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001745{
Alan Sterne04199b2008-06-24 14:47:29 -04001746 if (dev->usb_classdev)
1747 device_unregister(dev->usb_classdev);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001748}
1749
Alan Stern501950d2009-01-13 11:33:42 -05001750#else
1751#define usb_classdev_add(dev) 0
1752#define usb_classdev_remove(dev) do {} while (0)
1753
1754#endif
1755
1756static int usbdev_notify(struct notifier_block *self,
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001757 unsigned long action, void *dev)
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001758{
1759 switch (action) {
1760 case USB_DEVICE_ADD:
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001761 if (usb_classdev_add(dev))
Akinobu Mita27d39e22006-10-09 18:09:33 +09001762 return NOTIFY_BAD;
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001763 break;
1764 case USB_DEVICE_REMOVE:
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001765 usb_classdev_remove(dev);
Alan Stern501950d2009-01-13 11:33:42 -05001766 usbdev_remove(dev);
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001767 break;
1768 }
1769 return NOTIFY_OK;
1770}
1771
1772static struct notifier_block usbdev_nb = {
Alan Stern501950d2009-01-13 11:33:42 -05001773 .notifier_call = usbdev_notify,
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001774};
1775
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -07001776static struct cdev usb_device_cdev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001777
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001778int __init usb_devio_init(void)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001779{
1780 int retval;
1781
Alan Sternfad21bd2005-08-10 15:15:57 -04001782 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
Greg Kroah-Hartman04e482ff2008-01-30 15:21:33 -08001783 "usb_device");
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001784 if (retval) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -07001785 printk(KERN_ERR "Unable to register minors for usb_device\n");
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001786 goto out;
1787 }
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001788 cdev_init(&usb_device_cdev, &usbdev_file_operations);
Alan Sternfad21bd2005-08-10 15:15:57 -04001789 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001790 if (retval) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -07001791 printk(KERN_ERR "Unable to get usb_device major %d\n",
1792 USB_DEVICE_MAJOR);
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001793 goto error_cdev;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001794 }
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001795#ifdef CONFIG_USB_DEVICE_CLASS
1796 usb_classdev_class = class_create(THIS_MODULE, "usb_device");
1797 if (IS_ERR(usb_classdev_class)) {
Greg Kroah-Hartman69a85942008-08-14 09:37:34 -07001798 printk(KERN_ERR "Unable to register usb_device class\n");
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001799 retval = PTR_ERR(usb_classdev_class);
1800 cdev_del(&usb_device_cdev);
1801 usb_classdev_class = NULL;
1802 goto out;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001803 }
Dan Williamse105b8b2008-04-21 10:51:07 -07001804 /* devices of this class shadow the major:minor of their parent
1805 * device, so clear ->dev_kobj to prevent adding duplicate entries
1806 * to /sys/dev
1807 */
1808 usb_classdev_class->dev_kobj = NULL;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001809#endif
Alan Stern501950d2009-01-13 11:33:42 -05001810 usb_register_notify(&usbdev_nb);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001811out:
1812 return retval;
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001813
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001814error_cdev:
1815 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
1816 goto out;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001817}
1818
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001819void usb_devio_cleanup(void)
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001820{
Greg Kroah-Hartmana7b986b2005-06-20 21:15:16 -07001821 usb_unregister_notify(&usbdev_nb);
Alan Stern501950d2009-01-13 11:33:42 -05001822#ifdef CONFIG_USB_DEVICE_CLASS
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001823 class_destroy(usb_classdev_class);
1824#endif
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001825 cdev_del(&usb_device_cdev);
Alan Sternfad21bd2005-08-10 15:15:57 -04001826 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001827}