Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /*****************************************************************************/ |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * 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 Welte | 4611383 | 2005-10-10 19:44:29 +0200 | [diff] [blame] | 31 | * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery |
| 32 | * (CAN-2005-3055) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | */ |
| 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 Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 46 | #include <linux/cdev.h> |
Greg Kroah-Hartman | a7b986b | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 47 | #include <linux/notifier.h> |
David Quigley | 7a01955 | 2006-06-30 01:55:48 -0700 | [diff] [blame] | 48 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #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 Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame^] | 55 | #include "hub.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 57 | #define USB_MAXBUS 64 |
| 58 | #define USB_DEVICE_MAX USB_MAXBUS * 128 |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 59 | |
Alan Stern | 4a2a8a2 | 2006-07-01 22:05:01 -0400 | [diff] [blame] | 60 | /* Mutual exclusion for removal, open, and release */ |
| 61 | DEFINE_MUTEX(usbfs_mutex); |
| 62 | |
Alan Stern | cd9f037 | 2008-06-24 14:47:04 -0400 | [diff] [blame] | 63 | struct 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | struct async { |
| 80 | struct list_head asynclist; |
| 81 | struct dev_state *ps; |
Eric W. Biederman | 2425c08 | 2006-10-02 02:17:28 -0700 | [diff] [blame] | 82 | struct pid *pid; |
Harald Welte | 4611383 | 2005-10-10 19:44:29 +0200 | [diff] [blame] | 83 | uid_t uid, euid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | unsigned int signr; |
| 85 | unsigned int ifnum; |
| 86 | void __user *userbuffer; |
| 87 | void __user *userurb; |
| 88 | struct urb *urb; |
Alan Stern | e015268 | 2007-08-24 15:42:52 -0400 | [diff] [blame] | 89 | int status; |
David Quigley | 7a01955 | 2006-06-30 01:55:48 -0700 | [diff] [blame] | 90 | u32 secid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 93 | static int usbfs_snoop; |
| 94 | module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR); |
| 95 | MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | #define snoop(dev, format, arg...) \ |
| 98 | do { \ |
| 99 | if (usbfs_snoop) \ |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 100 | dev_info(dev , format , ## arg); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } while (0) |
| 102 | |
Alan Stern | fad21bd | 2005-08-10 15:15:57 -0400 | [diff] [blame] | 103 | #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0) |
| 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | #define MAX_USBFS_BUFFER_SIZE 16384 |
| 107 | |
Alan Stern | d34d972 | 2009-03-09 13:44:48 -0400 | [diff] [blame] | 108 | static int connected(struct dev_state *ps) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
Alan Stern | 349710c | 2006-07-01 22:05:56 -0400 | [diff] [blame] | 110 | return (!list_empty(&ps->list) && |
| 111 | ps->dev->state != USB_STATE_NOTATTACHED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig) |
| 115 | { |
| 116 | loff_t ret; |
| 117 | |
| 118 | lock_kernel(); |
| 119 | |
| 120 | switch (orig) { |
| 121 | case 0: |
| 122 | file->f_pos = offset; |
| 123 | ret = file->f_pos; |
| 124 | break; |
| 125 | case 1: |
| 126 | file->f_pos += offset; |
| 127 | ret = file->f_pos; |
| 128 | break; |
| 129 | case 2: |
| 130 | default: |
| 131 | ret = -EINVAL; |
| 132 | } |
| 133 | |
| 134 | unlock_kernel(); |
| 135 | return ret; |
| 136 | } |
| 137 | |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 138 | static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes, |
| 139 | loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
Tobias Klauser | ec17cf1 | 2006-09-13 21:38:41 +0200 | [diff] [blame] | 141 | struct dev_state *ps = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | struct usb_device *dev = ps->dev; |
| 143 | ssize_t ret = 0; |
| 144 | unsigned len; |
| 145 | loff_t pos; |
| 146 | int i; |
| 147 | |
| 148 | pos = *ppos; |
| 149 | usb_lock_device(dev); |
Alan Stern | 349710c | 2006-07-01 22:05:56 -0400 | [diff] [blame] | 150 | if (!connected(ps)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | ret = -ENODEV; |
| 152 | goto err; |
| 153 | } else if (pos < 0) { |
| 154 | ret = -EINVAL; |
| 155 | goto err; |
| 156 | } |
| 157 | |
| 158 | if (pos < sizeof(struct usb_device_descriptor)) { |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 159 | /* 18 bytes - fits on the stack */ |
| 160 | struct usb_device_descriptor temp_desc; |
Oliver Neukum | 8781ba0 | 2006-01-06 21:24:25 +0100 | [diff] [blame] | 161 | |
| 162 | memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor)); |
Andrew Morton | 9fcd5c3 | 2006-01-18 23:55:07 -0800 | [diff] [blame] | 163 | le16_to_cpus(&temp_desc.bcdUSB); |
| 164 | le16_to_cpus(&temp_desc.idVendor); |
| 165 | le16_to_cpus(&temp_desc.idProduct); |
| 166 | le16_to_cpus(&temp_desc.bcdDevice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | len = sizeof(struct usb_device_descriptor) - pos; |
| 169 | if (len > nbytes) |
| 170 | len = nbytes; |
Oliver Neukum | 8781ba0 | 2006-01-06 21:24:25 +0100 | [diff] [blame] | 171 | if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | ret = -EFAULT; |
| 173 | goto err; |
| 174 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | *ppos += len; |
| 177 | buf += len; |
| 178 | nbytes -= len; |
| 179 | ret += len; |
| 180 | } |
| 181 | |
| 182 | pos = sizeof(struct usb_device_descriptor); |
| 183 | for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) { |
| 184 | struct usb_config_descriptor *config = |
| 185 | (struct usb_config_descriptor *)dev->rawdescriptors[i]; |
| 186 | unsigned int length = le16_to_cpu(config->wTotalLength); |
| 187 | |
| 188 | if (*ppos < pos + length) { |
| 189 | |
| 190 | /* The descriptor may claim to be longer than it |
| 191 | * really is. Here is the actual allocated length. */ |
| 192 | unsigned alloclen = |
| 193 | le16_to_cpu(dev->config[i].desc.wTotalLength); |
| 194 | |
| 195 | len = length - (*ppos - pos); |
| 196 | if (len > nbytes) |
| 197 | len = nbytes; |
| 198 | |
| 199 | /* Simply don't write (skip over) unallocated parts */ |
| 200 | if (alloclen > (*ppos - pos)) { |
| 201 | alloclen -= (*ppos - pos); |
| 202 | if (copy_to_user(buf, |
| 203 | dev->rawdescriptors[i] + (*ppos - pos), |
| 204 | min(len, alloclen))) { |
| 205 | ret = -EFAULT; |
| 206 | goto err; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | *ppos += len; |
| 211 | buf += len; |
| 212 | nbytes -= len; |
| 213 | ret += len; |
| 214 | } |
| 215 | |
| 216 | pos += length; |
| 217 | } |
| 218 | |
| 219 | err: |
| 220 | usb_unlock_device(dev); |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * async list handling |
| 226 | */ |
| 227 | |
| 228 | static struct async *alloc_async(unsigned int numisoframes) |
| 229 | { |
Pete Zaitcev | dd95b81 | 2008-01-05 02:01:07 -0800 | [diff] [blame] | 230 | struct async *as; |
Alan Stern | 0a1ef3b | 2005-10-24 15:38:24 -0400 | [diff] [blame] | 231 | |
Pete Zaitcev | dd95b81 | 2008-01-05 02:01:07 -0800 | [diff] [blame] | 232 | as = kzalloc(sizeof(struct async), GFP_KERNEL); |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 233 | if (!as) |
| 234 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL); |
| 236 | if (!as->urb) { |
| 237 | kfree(as); |
| 238 | return NULL; |
| 239 | } |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 240 | return as; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static void free_async(struct async *as) |
| 244 | { |
Eric W. Biederman | 2425c08 | 2006-10-02 02:17:28 -0700 | [diff] [blame] | 245 | put_pid(as->pid); |
Jesper Juhl | 6fd19f4 | 2005-04-18 17:39:33 -0700 | [diff] [blame] | 246 | kfree(as->urb->transfer_buffer); |
| 247 | kfree(as->urb->setup_packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | usb_free_urb(as->urb); |
Jesper Juhl | 6fd19f4 | 2005-04-18 17:39:33 -0700 | [diff] [blame] | 249 | kfree(as); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Alan Stern | d34d972 | 2009-03-09 13:44:48 -0400 | [diff] [blame] | 252 | static void async_newpending(struct async *as) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 254 | struct dev_state *ps = as->ps; |
| 255 | unsigned long flags; |
| 256 | |
| 257 | spin_lock_irqsave(&ps->lock, flags); |
| 258 | list_add_tail(&as->asynclist, &ps->async_pending); |
| 259 | spin_unlock_irqrestore(&ps->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Alan Stern | d34d972 | 2009-03-09 13:44:48 -0400 | [diff] [blame] | 262 | static void async_removepending(struct async *as) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 264 | struct dev_state *ps = as->ps; |
| 265 | unsigned long flags; |
| 266 | |
| 267 | spin_lock_irqsave(&ps->lock, flags); |
| 268 | list_del_init(&as->asynclist); |
| 269 | spin_unlock_irqrestore(&ps->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Alan Stern | d34d972 | 2009-03-09 13:44:48 -0400 | [diff] [blame] | 272 | static struct async *async_getcompleted(struct dev_state *ps) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 274 | unsigned long flags; |
| 275 | struct async *as = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 277 | spin_lock_irqsave(&ps->lock, flags); |
| 278 | if (!list_empty(&ps->async_completed)) { |
| 279 | as = list_entry(ps->async_completed.next, struct async, |
| 280 | asynclist); |
| 281 | list_del_init(&as->asynclist); |
| 282 | } |
| 283 | spin_unlock_irqrestore(&ps->lock, flags); |
| 284 | return as; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Alan Stern | d34d972 | 2009-03-09 13:44:48 -0400 | [diff] [blame] | 287 | static struct async *async_getpending(struct dev_state *ps, |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 288 | void __user *userurb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 290 | unsigned long flags; |
| 291 | struct async *as; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 293 | spin_lock_irqsave(&ps->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | list_for_each_entry(as, &ps->async_pending, asynclist) |
| 295 | if (as->userurb == userurb) { |
| 296 | list_del_init(&as->asynclist); |
| 297 | spin_unlock_irqrestore(&ps->lock, flags); |
| 298 | return as; |
| 299 | } |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 300 | spin_unlock_irqrestore(&ps->lock, flags); |
| 301 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 304 | static void snoop_urb(struct urb *urb, void __user *userurb) |
| 305 | { |
Roel Kluin | 71d2718 | 2009-03-13 12:19:18 +0100 | [diff] [blame] | 306 | unsigned j; |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 307 | unsigned char *data = urb->transfer_buffer; |
| 308 | |
| 309 | if (!usbfs_snoop) |
| 310 | return; |
| 311 | |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 312 | dev_info(&urb->dev->dev, "direction=%s\n", |
| 313 | usb_urb_dir_in(urb) ? "IN" : "OUT"); |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 314 | dev_info(&urb->dev->dev, "userurb=%p\n", userurb); |
Roel Kluin | 71d2718 | 2009-03-13 12:19:18 +0100 | [diff] [blame] | 315 | dev_info(&urb->dev->dev, "transfer_buffer_length=%u\n", |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 316 | urb->transfer_buffer_length); |
Roel Kluin | 71d2718 | 2009-03-13 12:19:18 +0100 | [diff] [blame] | 317 | dev_info(&urb->dev->dev, "actual_length=%u\n", urb->actual_length); |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 318 | dev_info(&urb->dev->dev, "data: "); |
| 319 | for (j = 0; j < urb->transfer_buffer_length; ++j) |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 320 | printk("%02x ", data[j]); |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 321 | printk("\n"); |
| 322 | } |
| 323 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 324 | static void async_completed(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | { |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 326 | struct async *as = urb->context; |
| 327 | struct dev_state *ps = as->ps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | struct siginfo sinfo; |
Oliver Neukum | 516a1a0 | 2009-07-08 19:09:23 +0200 | [diff] [blame] | 329 | struct pid *pid = NULL; |
| 330 | uid_t uid = 0; |
| 331 | uid_t euid = 0; |
| 332 | u32 secid = 0; |
| 333 | int signr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 335 | spin_lock(&ps->lock); |
| 336 | list_move_tail(&as->asynclist, &ps->async_completed); |
Alan Stern | e015268 | 2007-08-24 15:42:52 -0400 | [diff] [blame] | 337 | as->status = urb->status; |
Oliver Neukum | 516a1a0 | 2009-07-08 19:09:23 +0200 | [diff] [blame] | 338 | signr = as->signr; |
| 339 | if (signr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | sinfo.si_signo = as->signr; |
Alan Stern | e015268 | 2007-08-24 15:42:52 -0400 | [diff] [blame] | 341 | sinfo.si_errno = as->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | sinfo.si_code = SI_ASYNCIO; |
| 343 | sinfo.si_addr = as->userurb; |
Oliver Neukum | 516a1a0 | 2009-07-08 19:09:23 +0200 | [diff] [blame] | 344 | pid = as->pid; |
| 345 | uid = as->uid; |
| 346 | euid = as->euid; |
| 347 | secid = as->secid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 349 | snoop(&urb->dev->dev, "urb complete\n"); |
| 350 | snoop_urb(urb, as->userurb); |
Oliver Neukum | 516a1a0 | 2009-07-08 19:09:23 +0200 | [diff] [blame] | 351 | spin_unlock(&ps->lock); |
| 352 | |
| 353 | if (signr) |
| 354 | kill_pid_info_as_uid(sinfo.si_signo, &sinfo, pid, uid, |
| 355 | euid, secid); |
| 356 | |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 357 | wake_up(&ps->wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 360 | static void destroy_async(struct dev_state *ps, struct list_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
| 362 | struct async *as; |
| 363 | unsigned long flags; |
| 364 | |
| 365 | spin_lock_irqsave(&ps->lock, flags); |
| 366 | while (!list_empty(list)) { |
| 367 | as = list_entry(list->next, struct async, asynclist); |
| 368 | list_del_init(&as->asynclist); |
| 369 | |
| 370 | /* drop the spinlock so the completion handler can run */ |
| 371 | spin_unlock_irqrestore(&ps->lock, flags); |
| 372 | usb_kill_urb(as->urb); |
| 373 | spin_lock_irqsave(&ps->lock, flags); |
| 374 | } |
| 375 | spin_unlock_irqrestore(&ps->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 378 | static void destroy_async_on_interface(struct dev_state *ps, |
| 379 | unsigned int ifnum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | { |
| 381 | struct list_head *p, *q, hitlist; |
| 382 | unsigned long flags; |
| 383 | |
| 384 | INIT_LIST_HEAD(&hitlist); |
| 385 | spin_lock_irqsave(&ps->lock, flags); |
| 386 | list_for_each_safe(p, q, &ps->async_pending) |
| 387 | if (ifnum == list_entry(p, struct async, asynclist)->ifnum) |
| 388 | list_move_tail(p, &hitlist); |
| 389 | spin_unlock_irqrestore(&ps->lock, flags); |
| 390 | destroy_async(ps, &hitlist); |
| 391 | } |
| 392 | |
Alan Stern | d34d972 | 2009-03-09 13:44:48 -0400 | [diff] [blame] | 393 | static void destroy_all_async(struct dev_state *ps) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 395 | destroy_async(ps, &ps->async_pending); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /* |
| 399 | * interface claims are made only at the request of user level code, |
| 400 | * which can also release them (explicitly or by closing files). |
| 401 | * they're also undone when devices disconnect. |
| 402 | */ |
| 403 | |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 404 | static int driver_probe(struct usb_interface *intf, |
| 405 | const struct usb_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | { |
| 407 | return -ENODEV; |
| 408 | } |
| 409 | |
| 410 | static void driver_disconnect(struct usb_interface *intf) |
| 411 | { |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 412 | struct dev_state *ps = usb_get_intfdata(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber; |
| 414 | |
| 415 | if (!ps) |
| 416 | return; |
| 417 | |
| 418 | /* NOTE: this relies on usbcore having canceled and completed |
| 419 | * all pending I/O requests; 2.6 does that. |
| 420 | */ |
| 421 | |
| 422 | if (likely(ifnum < 8*sizeof(ps->ifclaimed))) |
| 423 | clear_bit(ifnum, &ps->ifclaimed); |
| 424 | else |
Greg Kroah-Hartman | 3b6004f | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 425 | dev_warn(&intf->dev, "interface number %u out of range\n", |
| 426 | ifnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 428 | usb_set_intfdata(intf, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | /* force async requests to complete */ |
| 431 | destroy_async_on_interface(ps, ifnum); |
| 432 | } |
| 433 | |
Alan Stern | 2e2eb83 | 2007-12-04 14:35:15 -0500 | [diff] [blame] | 434 | /* The following routines are merely placeholders. There is no way |
| 435 | * to inform a user task about suspend or resumes. |
| 436 | */ |
| 437 | static int driver_suspend(struct usb_interface *intf, pm_message_t msg) |
| 438 | { |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | static int driver_resume(struct usb_interface *intf) |
| 443 | { |
| 444 | return 0; |
| 445 | } |
| 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | struct usb_driver usbfs_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | .name = "usbfs", |
| 449 | .probe = driver_probe, |
| 450 | .disconnect = driver_disconnect, |
Alan Stern | 2e2eb83 | 2007-12-04 14:35:15 -0500 | [diff] [blame] | 451 | .suspend = driver_suspend, |
| 452 | .resume = driver_resume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | }; |
| 454 | |
| 455 | static int claimintf(struct dev_state *ps, unsigned int ifnum) |
| 456 | { |
| 457 | struct usb_device *dev = ps->dev; |
| 458 | struct usb_interface *intf; |
| 459 | int err; |
| 460 | |
| 461 | if (ifnum >= 8*sizeof(ps->ifclaimed)) |
| 462 | return -EINVAL; |
| 463 | /* already claimed */ |
| 464 | if (test_bit(ifnum, &ps->ifclaimed)) |
| 465 | return 0; |
| 466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | intf = usb_ifnum_to_if(dev, ifnum); |
| 468 | if (!intf) |
| 469 | err = -ENOENT; |
| 470 | else |
| 471 | err = usb_driver_claim_interface(&usbfs_driver, intf, ps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | if (err == 0) |
| 473 | set_bit(ifnum, &ps->ifclaimed); |
| 474 | return err; |
| 475 | } |
| 476 | |
| 477 | static int releaseintf(struct dev_state *ps, unsigned int ifnum) |
| 478 | { |
| 479 | struct usb_device *dev; |
| 480 | struct usb_interface *intf; |
| 481 | int err; |
| 482 | |
| 483 | err = -EINVAL; |
| 484 | if (ifnum >= 8*sizeof(ps->ifclaimed)) |
| 485 | return err; |
| 486 | dev = ps->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | intf = usb_ifnum_to_if(dev, ifnum); |
| 488 | if (!intf) |
| 489 | err = -ENOENT; |
| 490 | else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) { |
| 491 | usb_driver_release_interface(&usbfs_driver, intf); |
| 492 | err = 0; |
| 493 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | return err; |
| 495 | } |
| 496 | |
| 497 | static int checkintf(struct dev_state *ps, unsigned int ifnum) |
| 498 | { |
| 499 | if (ps->dev->state != USB_STATE_CONFIGURED) |
| 500 | return -EHOSTUNREACH; |
| 501 | if (ifnum >= 8*sizeof(ps->ifclaimed)) |
| 502 | return -EINVAL; |
| 503 | if (test_bit(ifnum, &ps->ifclaimed)) |
| 504 | return 0; |
| 505 | /* if not yet claimed, claim it for the driver */ |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 506 | dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim " |
| 507 | "interface %u before use\n", task_pid_nr(current), |
| 508 | current->comm, ifnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | return claimintf(ps, ifnum); |
| 510 | } |
| 511 | |
| 512 | static int findintfep(struct usb_device *dev, unsigned int ep) |
| 513 | { |
| 514 | unsigned int i, j, e; |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 515 | struct usb_interface *intf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | struct usb_host_interface *alts; |
| 517 | struct usb_endpoint_descriptor *endpt; |
| 518 | |
| 519 | if (ep & ~(USB_DIR_IN|0xf)) |
| 520 | return -EINVAL; |
| 521 | if (!dev->actconfig) |
| 522 | return -ESRCH; |
| 523 | for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { |
| 524 | intf = dev->actconfig->interface[i]; |
| 525 | for (j = 0; j < intf->num_altsetting; j++) { |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 526 | alts = &intf->altsetting[j]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | for (e = 0; e < alts->desc.bNumEndpoints; e++) { |
| 528 | endpt = &alts->endpoint[e].desc; |
| 529 | if (endpt->bEndpointAddress == ep) |
| 530 | return alts->desc.bInterfaceNumber; |
| 531 | } |
| 532 | } |
| 533 | } |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 534 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 537 | static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype, |
| 538 | unsigned int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | { |
| 540 | int ret = 0; |
| 541 | |
David Vrabel | 6da9c99 | 2009-02-18 14:43:47 +0000 | [diff] [blame] | 542 | if (ps->dev->state != USB_STATE_UNAUTHENTICATED |
| 543 | && ps->dev->state != USB_STATE_ADDRESS |
Horst Schirmeier | 24f8b11 | 2006-03-11 00:16:55 -0800 | [diff] [blame] | 544 | && ps->dev->state != USB_STATE_CONFIGURED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | return -EHOSTUNREACH; |
| 546 | if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype)) |
| 547 | return 0; |
| 548 | |
| 549 | index &= 0xff; |
| 550 | switch (requesttype & USB_RECIP_MASK) { |
| 551 | case USB_RECIP_ENDPOINT: |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 552 | ret = findintfep(ps->dev, index); |
| 553 | if (ret >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | ret = checkintf(ps, ret); |
| 555 | break; |
| 556 | |
| 557 | case USB_RECIP_INTERFACE: |
| 558 | ret = checkintf(ps, index); |
| 559 | break; |
| 560 | } |
| 561 | return ret; |
| 562 | } |
| 563 | |
Alan Stern | 61ad04a | 2008-06-24 14:47:12 -0400 | [diff] [blame] | 564 | static int match_devt(struct device *dev, void *data) |
Greg Kroah-Hartman | 4592bf5 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 565 | { |
David Howells | a80d5ff | 2008-07-02 12:28:55 +0100 | [diff] [blame] | 566 | return dev->devt == (dev_t) (unsigned long) data; |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 567 | } |
Greg Kroah-Hartman | 4592bf5 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 568 | |
Alan Stern | 61ad04a | 2008-06-24 14:47:12 -0400 | [diff] [blame] | 569 | static struct usb_device *usbdev_lookup_by_devt(dev_t devt) |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 570 | { |
| 571 | struct device *dev; |
| 572 | |
David Howells | a80d5ff | 2008-07-02 12:28:55 +0100 | [diff] [blame] | 573 | dev = bus_find_device(&usb_bus_type, NULL, |
| 574 | (void *) (unsigned long) devt, match_devt); |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 575 | if (!dev) |
| 576 | return NULL; |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 577 | return container_of(dev, struct usb_device, dev); |
| 578 | } |
Greg Kroah-Hartman | 4592bf5 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | /* |
| 581 | * file operations |
| 582 | */ |
| 583 | static int usbdev_open(struct inode *inode, struct file *file) |
| 584 | { |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 585 | struct usb_device *dev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | struct dev_state *ps; |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 587 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | int ret; |
| 589 | |
Jonathan Corbet | b5b4aa6 | 2008-05-16 14:25:20 -0600 | [diff] [blame] | 590 | lock_kernel(); |
Alan Stern | 4a2a8a2 | 2006-07-01 22:05:01 -0400 | [diff] [blame] | 591 | /* Protect against simultaneous removal or release */ |
| 592 | mutex_lock(&usbfs_mutex); |
| 593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | ret = -ENOMEM; |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 595 | ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL); |
| 596 | if (!ps) |
Alan Stern | 4a2a8a2 | 2006-07-01 22:05:01 -0400 | [diff] [blame] | 597 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
Alan Stern | 01105a2 | 2009-07-30 15:28:14 -0400 | [diff] [blame] | 599 | ret = -ENODEV; |
Alan Stern | 61ad04a | 2008-06-24 14:47:12 -0400 | [diff] [blame] | 600 | |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 601 | /* usbdev device-node */ |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 602 | if (imajor(inode) == USB_DEVICE_MAJOR) |
Alan Stern | 61ad04a | 2008-06-24 14:47:12 -0400 | [diff] [blame] | 603 | dev = usbdev_lookup_by_devt(inode->i_rdev); |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 604 | #ifdef CONFIG_USB_DEVICEFS |
| 605 | /* procfs file */ |
Alan Stern | d64aac3 | 2008-06-24 14:47:19 -0400 | [diff] [blame] | 606 | if (!dev) { |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 607 | dev = inode->i_private; |
Alan Stern | d64aac3 | 2008-06-24 14:47:19 -0400 | [diff] [blame] | 608 | if (dev && dev->usbfs_dentry && |
| 609 | dev->usbfs_dentry->d_inode == inode) |
| 610 | usb_get_dev(dev); |
| 611 | else |
| 612 | dev = NULL; |
| 613 | } |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 614 | #endif |
Alan Stern | d64aac3 | 2008-06-24 14:47:19 -0400 | [diff] [blame] | 615 | if (!dev || dev->state == USB_STATE_NOTATTACHED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | goto out; |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 617 | ret = usb_autoresume_device(dev); |
Alan Stern | 01d883d | 2006-08-30 15:47:18 -0400 | [diff] [blame] | 618 | if (ret) |
| 619 | goto out; |
| 620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | ret = 0; |
| 622 | ps->dev = dev; |
| 623 | ps->file = file; |
| 624 | spin_lock_init(&ps->lock); |
Dan Carpenter | 316547f | 2006-12-13 00:03:38 -0800 | [diff] [blame] | 625 | INIT_LIST_HEAD(&ps->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | INIT_LIST_HEAD(&ps->async_pending); |
| 627 | INIT_LIST_HEAD(&ps->async_completed); |
| 628 | init_waitqueue_head(&ps->wait); |
| 629 | ps->discsignr = 0; |
Eric W. Biederman | 2425c08 | 2006-10-02 02:17:28 -0700 | [diff] [blame] | 630 | ps->disc_pid = get_pid(task_pid(current)); |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 631 | ps->disc_uid = cred->uid; |
| 632 | ps->disc_euid = cred->euid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | ps->disccontext = NULL; |
| 634 | ps->ifclaimed = 0; |
David Quigley | 7a01955 | 2006-06-30 01:55:48 -0700 | [diff] [blame] | 635 | security_task_getsecid(current, &ps->secid); |
Oliver Neukum | 527660a | 2007-04-20 20:50:48 +0200 | [diff] [blame] | 636 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | list_add_tail(&ps->list, &dev->filelist); |
| 638 | file->private_data = ps; |
Alan Stern | 2da41d5f | 2008-10-06 11:24:26 -0400 | [diff] [blame] | 639 | snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current), |
| 640 | current->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | out: |
Alan Stern | d64aac3 | 2008-06-24 14:47:19 -0400 | [diff] [blame] | 642 | if (ret) { |
Alan Stern | 01d883d | 2006-08-30 15:47:18 -0400 | [diff] [blame] | 643 | kfree(ps); |
Alan Stern | d64aac3 | 2008-06-24 14:47:19 -0400 | [diff] [blame] | 644 | usb_put_dev(dev); |
| 645 | } |
Alan Stern | 4a2a8a2 | 2006-07-01 22:05:01 -0400 | [diff] [blame] | 646 | mutex_unlock(&usbfs_mutex); |
Jonathan Corbet | b5b4aa6 | 2008-05-16 14:25:20 -0600 | [diff] [blame] | 647 | unlock_kernel(); |
Alan Stern | 4a2a8a2 | 2006-07-01 22:05:01 -0400 | [diff] [blame] | 648 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | static int usbdev_release(struct inode *inode, struct file *file) |
| 652 | { |
Tobias Klauser | ec17cf1 | 2006-09-13 21:38:41 +0200 | [diff] [blame] | 653 | struct dev_state *ps = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | struct usb_device *dev = ps->dev; |
| 655 | unsigned int ifnum; |
Alan Stern | 6ff1046 | 2009-03-09 13:44:02 -0400 | [diff] [blame] | 656 | struct async *as; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
| 658 | usb_lock_device(dev); |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame^] | 659 | usb_hub_release_all_ports(dev, ps); |
Alan Stern | 4a2a8a2 | 2006-07-01 22:05:01 -0400 | [diff] [blame] | 660 | |
| 661 | /* Protect against simultaneous open */ |
| 662 | mutex_lock(&usbfs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | list_del_init(&ps->list); |
Alan Stern | 4a2a8a2 | 2006-07-01 22:05:01 -0400 | [diff] [blame] | 664 | mutex_unlock(&usbfs_mutex); |
| 665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed); |
| 667 | ifnum++) { |
| 668 | if (test_bit(ifnum, &ps->ifclaimed)) |
| 669 | releaseintf(ps, ifnum); |
| 670 | } |
| 671 | destroy_all_async(ps); |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 672 | usb_autosuspend_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | usb_unlock_device(dev); |
| 674 | usb_put_dev(dev); |
Eric W. Biederman | 2425c08 | 2006-10-02 02:17:28 -0700 | [diff] [blame] | 675 | put_pid(ps->disc_pid); |
Alan Stern | 6ff1046 | 2009-03-09 13:44:02 -0400 | [diff] [blame] | 676 | |
| 677 | as = async_getcompleted(ps); |
| 678 | while (as) { |
| 679 | free_async(as); |
| 680 | as = async_getcompleted(ps); |
| 681 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | kfree(ps); |
Alan Stern | 4a2a8a2 | 2006-07-01 22:05:01 -0400 | [diff] [blame] | 683 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | static int proc_control(struct dev_state *ps, void __user *arg) |
| 687 | { |
| 688 | struct usb_device *dev = ps->dev; |
| 689 | struct usbdevfs_ctrltransfer ctrl; |
| 690 | unsigned int tmo; |
| 691 | unsigned char *tbuf; |
Andrew Morton | ff66e3c | 2008-03-12 13:32:24 -0700 | [diff] [blame] | 692 | unsigned wLength; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | int i, j, ret; |
| 694 | |
| 695 | if (copy_from_user(&ctrl, arg, sizeof(ctrl))) |
| 696 | return -EFAULT; |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 697 | ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.wIndex); |
| 698 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | return ret; |
Andrew Morton | ff66e3c | 2008-03-12 13:32:24 -0700 | [diff] [blame] | 700 | wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */ |
| 701 | if (wLength > PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | return -EINVAL; |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 703 | tbuf = (unsigned char *)__get_free_page(GFP_KERNEL); |
| 704 | if (!tbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | return -ENOMEM; |
| 706 | tmo = ctrl.timeout; |
| 707 | if (ctrl.bRequestType & 0x80) { |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 708 | if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data, |
| 709 | ctrl.wLength)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | free_page((unsigned long)tbuf); |
| 711 | return -EINVAL; |
| 712 | } |
Alan Stern | fe0410c | 2005-07-29 12:16:58 -0700 | [diff] [blame] | 713 | snoop(&dev->dev, "control read: bRequest=%02x " |
| 714 | "bRrequestType=%02x wValue=%04x " |
| 715 | "wIndex=%04x wLength=%04x\n", |
| 716 | ctrl.bRequest, ctrl.bRequestType, ctrl.wValue, |
| 717 | ctrl.wIndex, ctrl.wLength); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | |
| 719 | usb_unlock_device(dev); |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 720 | i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest, |
| 721 | ctrl.bRequestType, ctrl.wValue, ctrl.wIndex, |
| 722 | tbuf, ctrl.wLength, tmo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | usb_lock_device(dev); |
| 724 | if ((i > 0) && ctrl.wLength) { |
| 725 | if (usbfs_snoop) { |
| 726 | dev_info(&dev->dev, "control read: data "); |
Alan Stern | fe0410c | 2005-07-29 12:16:58 -0700 | [diff] [blame] | 727 | for (j = 0; j < i; ++j) |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 728 | printk("%02x ", (u8)(tbuf)[j]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | printk("\n"); |
| 730 | } |
Alan Stern | fe0410c | 2005-07-29 12:16:58 -0700 | [diff] [blame] | 731 | if (copy_to_user(ctrl.data, tbuf, i)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | free_page((unsigned long)tbuf); |
| 733 | return -EFAULT; |
| 734 | } |
| 735 | } |
| 736 | } else { |
| 737 | if (ctrl.wLength) { |
| 738 | if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) { |
| 739 | free_page((unsigned long)tbuf); |
| 740 | return -EFAULT; |
| 741 | } |
| 742 | } |
Alan Stern | fe0410c | 2005-07-29 12:16:58 -0700 | [diff] [blame] | 743 | snoop(&dev->dev, "control write: bRequest=%02x " |
| 744 | "bRrequestType=%02x wValue=%04x " |
| 745 | "wIndex=%04x wLength=%04x\n", |
| 746 | ctrl.bRequest, ctrl.bRequestType, ctrl.wValue, |
| 747 | ctrl.wIndex, ctrl.wLength); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | if (usbfs_snoop) { |
| 749 | dev_info(&dev->dev, "control write: data: "); |
| 750 | for (j = 0; j < ctrl.wLength; ++j) |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 751 | printk("%02x ", (unsigned char)(tbuf)[j]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | printk("\n"); |
| 753 | } |
| 754 | usb_unlock_device(dev); |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 755 | i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest, |
| 756 | ctrl.bRequestType, ctrl.wValue, ctrl.wIndex, |
| 757 | tbuf, ctrl.wLength, tmo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | usb_lock_device(dev); |
| 759 | } |
| 760 | free_page((unsigned long)tbuf); |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 761 | if (i < 0 && i != -EPIPE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL " |
| 763 | "failed cmd %s rqt %u rq %u len %u ret %d\n", |
| 764 | current->comm, ctrl.bRequestType, ctrl.bRequest, |
| 765 | ctrl.wLength, i); |
| 766 | } |
| 767 | return i; |
| 768 | } |
| 769 | |
| 770 | static int proc_bulk(struct dev_state *ps, void __user *arg) |
| 771 | { |
| 772 | struct usb_device *dev = ps->dev; |
| 773 | struct usbdevfs_bulktransfer bulk; |
| 774 | unsigned int tmo, len1, pipe; |
| 775 | int len2; |
| 776 | unsigned char *tbuf; |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 777 | int i, j, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
| 779 | if (copy_from_user(&bulk, arg, sizeof(bulk))) |
| 780 | return -EFAULT; |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 781 | ret = findintfep(ps->dev, bulk.ep); |
| 782 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | return ret; |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 784 | ret = checkintf(ps, ret); |
| 785 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | return ret; |
| 787 | if (bulk.ep & USB_DIR_IN) |
| 788 | pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f); |
| 789 | else |
| 790 | pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f); |
| 791 | if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN))) |
| 792 | return -EINVAL; |
| 793 | len1 = bulk.len; |
| 794 | if (len1 > MAX_USBFS_BUFFER_SIZE) |
| 795 | return -EINVAL; |
| 796 | if (!(tbuf = kmalloc(len1, GFP_KERNEL))) |
| 797 | return -ENOMEM; |
| 798 | tmo = bulk.timeout; |
| 799 | if (bulk.ep & 0x80) { |
| 800 | if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) { |
| 801 | kfree(tbuf); |
| 802 | return -EINVAL; |
| 803 | } |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 804 | snoop(&dev->dev, "bulk read: len=0x%02x timeout=%04d\n", |
| 805 | bulk.len, bulk.timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | usb_unlock_device(dev); |
| 807 | i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo); |
| 808 | usb_lock_device(dev); |
| 809 | if (!i && len2) { |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 810 | if (usbfs_snoop) { |
| 811 | dev_info(&dev->dev, "bulk read: data "); |
| 812 | for (j = 0; j < len2; ++j) |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 813 | printk("%02x ", (u8)(tbuf)[j]); |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 814 | printk("\n"); |
| 815 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | if (copy_to_user(bulk.data, tbuf, len2)) { |
| 817 | kfree(tbuf); |
| 818 | return -EFAULT; |
| 819 | } |
| 820 | } |
| 821 | } else { |
| 822 | if (len1) { |
| 823 | if (copy_from_user(tbuf, bulk.data, len1)) { |
| 824 | kfree(tbuf); |
| 825 | return -EFAULT; |
| 826 | } |
| 827 | } |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 828 | snoop(&dev->dev, "bulk write: len=0x%02x timeout=%04d\n", |
| 829 | bulk.len, bulk.timeout); |
| 830 | if (usbfs_snoop) { |
| 831 | dev_info(&dev->dev, "bulk write: data: "); |
| 832 | for (j = 0; j < len1; ++j) |
| 833 | printk("%02x ", (unsigned char)(tbuf)[j]); |
| 834 | printk("\n"); |
| 835 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | usb_unlock_device(dev); |
| 837 | i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo); |
| 838 | usb_lock_device(dev); |
| 839 | } |
| 840 | kfree(tbuf); |
| 841 | if (i < 0) |
| 842 | return i; |
| 843 | return len2; |
| 844 | } |
| 845 | |
| 846 | static 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-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 853 | ret = findintfep(ps->dev, ep); |
| 854 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | return ret; |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 856 | ret = checkintf(ps, ret); |
| 857 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | return ret; |
David Vrabel | 3444b26 | 2009-04-08 17:36:28 +0000 | [diff] [blame] | 859 | usb_reset_endpoint(ps->dev, ep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | static 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-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 871 | ret = findintfep(ps->dev, ep); |
| 872 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | return ret; |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 874 | ret = checkintf(ps, ret); |
| 875 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | return ret; |
| 877 | if (ep & USB_DIR_IN) |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 878 | pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f); |
| 879 | else |
| 880 | pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | |
| 882 | return usb_clear_halt(ps->dev, pipe); |
| 883 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
| 885 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | return ret; |
| 902 | } |
| 903 | |
| 904 | static 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 | |
| 915 | static int proc_resetdevice(struct dev_state *ps) |
| 916 | { |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 917 | return usb_reset_device(ps->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | static 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 | |
| 933 | static int proc_setconfig(struct dev_state *ps, void __user *arg) |
| 934 | { |
Alan Stern | 3f141e2 | 2007-02-08 16:40:43 -0500 | [diff] [blame] | 935 | int u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | int status = 0; |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 937 | struct usb_host_config *actconfig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
Alan Stern | 3f141e2 | 2007-02-08 16:40:43 -0500 | [diff] [blame] | 939 | if (get_user(u, (int __user *)arg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | return -EFAULT; |
| 941 | |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 942 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | * an interface is claimed by usbfs it could easily deadlock. |
| 947 | */ |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 948 | 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 Brownell | 72ebddb | 2005-04-11 18:34:17 -0700 | [diff] [blame] | 954 | "usbfs: interface %d claimed by %s " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | "while '%s' sets config #%d\n", |
| 956 | actconfig->interface[i] |
| 957 | ->cur_altsetting |
| 958 | ->desc.bInterfaceNumber, |
David Brownell | 72ebddb | 2005-04-11 18:34:17 -0700 | [diff] [blame] | 959 | actconfig->interface[i] |
| 960 | ->dev.driver->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | current->comm, u); |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 962 | status = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | } |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 965 | } |
| 966 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb, |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 982 | struct usbdevfs_iso_packet_desc __user *iso_frame_desc, |
| 983 | void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | { |
| 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 Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 989 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | unsigned int u, totlen, isofrmlen; |
Alan Stern | 97b9eb9 | 2007-02-26 14:56:14 -0500 | [diff] [blame] | 991 | int ret, ifnum = -1; |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 992 | int is_in; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | |
Alan Stern | 14722ef | 2008-04-17 10:18:11 -0400 | [diff] [blame] | 994 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | return -EINVAL; |
Alan Stern | 9180135 | 2009-06-29 11:04:54 -0400 | [diff] [blame] | 1000 | if (uurb->buffer_length > 0 && !uurb->buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | return -EINVAL; |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1002 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | return ifnum; |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1007 | ret = checkintf(ps, ifnum); |
| 1008 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | return ret; |
| 1010 | } |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1011 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | if (!ep) |
| 1019 | return -ENOENT; |
| 1020 | switch(uurb->type) { |
| 1021 | case USBDEVFS_URB_TYPE_CONTROL: |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1022 | if (!usb_endpoint_xfer_control(&ep->desc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | return -EINVAL; |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1024 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | return -EINVAL; |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1029 | dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); |
| 1030 | if (!dr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | 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-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1040 | ret = check_ctrlrecip(ps, dr->bRequestType, |
| 1041 | le16_to_cpup(&dr->wIndex)); |
| 1042 | if (ret) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | kfree(dr); |
| 1044 | return ret; |
| 1045 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | uurb->number_of_packets = 0; |
| 1047 | uurb->buffer_length = le16_to_cpup(&dr->wLength); |
| 1048 | uurb->buffer += 8; |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1049 | 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 | } |
Chris Frey | df251b8 | 2006-12-16 02:37:42 -0500 | [diff] [blame] | 1056 | snoop(&ps->dev->dev, "control urb: bRequest=%02x " |
| 1057 | "bRrequestType=%02x wValue=%04x " |
| 1058 | "wIndex=%04x wLength=%04x\n", |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1059 | dr->bRequest, dr->bRequestType, |
| 1060 | __le16_to_cpup(&dr->wValue), |
| 1061 | __le16_to_cpup(&dr->wIndex), |
| 1062 | __le16_to_cpup(&dr->wLength)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | break; |
| 1064 | |
| 1065 | case USBDEVFS_URB_TYPE_BULK: |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1066 | switch (usb_endpoint_type(&ep->desc)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | case USB_ENDPOINT_XFER_CONTROL: |
| 1068 | case USB_ENDPOINT_XFER_ISOC: |
| 1069 | return -EINVAL; |
| 1070 | /* allow single-shot interrupt transfers, at bogus rates */ |
| 1071 | } |
| 1072 | uurb->number_of_packets = 0; |
| 1073 | if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE) |
| 1074 | return -EINVAL; |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1075 | snoop(&ps->dev->dev, "bulk urb\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | break; |
| 1077 | |
| 1078 | case USBDEVFS_URB_TYPE_ISO: |
| 1079 | /* arbitrary limit */ |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1080 | if (uurb->number_of_packets < 1 || |
| 1081 | uurb->number_of_packets > 128) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | return -EINVAL; |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1083 | if (!usb_endpoint_xfer_isoc(&ep->desc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | return -EINVAL; |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1085 | isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) * |
| 1086 | uurb->number_of_packets; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL))) |
| 1088 | return -ENOMEM; |
| 1089 | if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) { |
| 1090 | kfree(isopkt); |
| 1091 | return -EFAULT; |
| 1092 | } |
| 1093 | for (totlen = u = 0; u < uurb->number_of_packets; u++) { |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1094 | /* arbitrary limit, |
| 1095 | * sufficient for USB 2.0 high-bandwidth iso */ |
Micah Dowty | 3612242 | 2006-05-19 11:26:24 -0700 | [diff] [blame] | 1096 | if (isopkt[u].length > 8192) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | kfree(isopkt); |
| 1098 | return -EINVAL; |
| 1099 | } |
| 1100 | totlen += isopkt[u].length; |
| 1101 | } |
| 1102 | if (totlen > 32768) { |
| 1103 | kfree(isopkt); |
| 1104 | return -EINVAL; |
| 1105 | } |
| 1106 | uurb->buffer_length = totlen; |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1107 | snoop(&ps->dev->dev, "iso urb\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | break; |
| 1109 | |
| 1110 | case USBDEVFS_URB_TYPE_INTERRUPT: |
| 1111 | uurb->number_of_packets = 0; |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1112 | if (!usb_endpoint_xfer_int(&ep->desc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE) |
| 1115 | return -EINVAL; |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1116 | snoop(&ps->dev->dev, "interrupt urb\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | break; |
| 1118 | |
| 1119 | default: |
| 1120 | return -EINVAL; |
| 1121 | } |
Alan Stern | 9180135 | 2009-06-29 11:04:54 -0400 | [diff] [blame] | 1122 | if (uurb->buffer_length > 0 && |
| 1123 | !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ, |
| 1124 | uurb->buffer, uurb->buffer_length)) { |
| 1125 | kfree(isopkt); |
| 1126 | kfree(dr); |
| 1127 | return -EFAULT; |
| 1128 | } |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1129 | as = alloc_async(uurb->number_of_packets); |
| 1130 | if (!as) { |
Jesper Juhl | 6fd19f4 | 2005-04-18 17:39:33 -0700 | [diff] [blame] | 1131 | kfree(isopkt); |
| 1132 | kfree(dr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | return -ENOMEM; |
| 1134 | } |
Alan Stern | 9180135 | 2009-06-29 11:04:54 -0400 | [diff] [blame] | 1135 | if (uurb->buffer_length > 0) { |
| 1136 | as->urb->transfer_buffer = kmalloc(uurb->buffer_length, |
| 1137 | GFP_KERNEL); |
| 1138 | if (!as->urb->transfer_buffer) { |
| 1139 | kfree(isopkt); |
| 1140 | kfree(dr); |
| 1141 | free_async(as); |
| 1142 | return -ENOMEM; |
| 1143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | } |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1145 | as->urb->dev = ps->dev; |
| 1146 | as->urb->pipe = (uurb->type << 30) | |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1147 | __create_pipe(ps->dev, uurb->endpoint & 0xf) | |
| 1148 | (uurb->endpoint & USB_DIR_IN); |
Alan Stern | 14722ef | 2008-04-17 10:18:11 -0400 | [diff] [blame] | 1149 | |
| 1150 | /* This tedious sequence is necessary because the URB_* flags |
| 1151 | * are internal to the kernel and subject to change, whereas |
| 1152 | * the USBDEVFS_URB_* flags are a user API and must not be changed. |
| 1153 | */ |
| 1154 | u = (is_in ? URB_DIR_IN : URB_DIR_OUT); |
| 1155 | if (uurb->flags & USBDEVFS_URB_ISO_ASAP) |
| 1156 | u |= URB_ISO_ASAP; |
| 1157 | if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK) |
| 1158 | u |= URB_SHORT_NOT_OK; |
| 1159 | if (uurb->flags & USBDEVFS_URB_NO_FSBR) |
| 1160 | u |= URB_NO_FSBR; |
| 1161 | if (uurb->flags & USBDEVFS_URB_ZERO_PACKET) |
| 1162 | u |= URB_ZERO_PACKET; |
| 1163 | if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT) |
| 1164 | u |= URB_NO_INTERRUPT; |
| 1165 | as->urb->transfer_flags = u; |
| 1166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | as->urb->transfer_buffer_length = uurb->buffer_length; |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1168 | as->urb->setup_packet = (unsigned char *)dr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | as->urb->start_frame = uurb->start_frame; |
| 1170 | as->urb->number_of_packets = uurb->number_of_packets; |
Alan Stern | 97b9eb9 | 2007-02-26 14:56:14 -0500 | [diff] [blame] | 1171 | if (uurb->type == USBDEVFS_URB_TYPE_ISO || |
| 1172 | ps->dev->speed == USB_SPEED_HIGH) |
| 1173 | as->urb->interval = 1 << min(15, ep->desc.bInterval - 1); |
| 1174 | else |
| 1175 | as->urb->interval = ep->desc.bInterval; |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1176 | as->urb->context = as; |
| 1177 | as->urb->complete = async_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | for (totlen = u = 0; u < uurb->number_of_packets; u++) { |
| 1179 | as->urb->iso_frame_desc[u].offset = totlen; |
| 1180 | as->urb->iso_frame_desc[u].length = isopkt[u].length; |
| 1181 | totlen += isopkt[u].length; |
| 1182 | } |
Jesper Juhl | 6fd19f4 | 2005-04-18 17:39:33 -0700 | [diff] [blame] | 1183 | kfree(isopkt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | as->ps = ps; |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1185 | as->userurb = arg; |
Alan Stern | 9180135 | 2009-06-29 11:04:54 -0400 | [diff] [blame] | 1186 | if (is_in && uurb->buffer_length > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | as->userbuffer = uurb->buffer; |
| 1188 | else |
| 1189 | as->userbuffer = NULL; |
| 1190 | as->signr = uurb->signr; |
| 1191 | as->ifnum = ifnum; |
Eric W. Biederman | 2425c08 | 2006-10-02 02:17:28 -0700 | [diff] [blame] | 1192 | as->pid = get_pid(task_pid(current)); |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 1193 | as->uid = cred->uid; |
| 1194 | as->euid = cred->euid; |
David Quigley | 7a01955 | 2006-06-30 01:55:48 -0700 | [diff] [blame] | 1195 | security_task_getsecid(current, &as->secid); |
Alan Stern | 9180135 | 2009-06-29 11:04:54 -0400 | [diff] [blame] | 1196 | if (!is_in && uurb->buffer_length > 0) { |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1197 | if (copy_from_user(as->urb->transfer_buffer, uurb->buffer, |
Alan Stern | 9180135 | 2009-06-29 11:04:54 -0400 | [diff] [blame] | 1198 | uurb->buffer_length)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | free_async(as); |
| 1200 | return -EFAULT; |
| 1201 | } |
| 1202 | } |
Greg Kroah-Hartman | e639dd3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1203 | snoop_urb(as->urb, as->userurb); |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1204 | async_newpending(as); |
| 1205 | if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) { |
| 1206 | dev_printk(KERN_DEBUG, &ps->dev->dev, |
| 1207 | "usbfs: usb_submit_urb returned %d\n", ret); |
| 1208 | async_removepending(as); |
| 1209 | free_async(as); |
| 1210 | return ret; |
| 1211 | } |
| 1212 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | static int proc_submiturb(struct dev_state *ps, void __user *arg) |
| 1216 | { |
| 1217 | struct usbdevfs_urb uurb; |
| 1218 | |
| 1219 | if (copy_from_user(&uurb, arg, sizeof(uurb))) |
| 1220 | return -EFAULT; |
| 1221 | |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1222 | return proc_do_submiturb(ps, &uurb, |
| 1223 | (((struct usbdevfs_urb __user *)arg)->iso_frame_desc), |
| 1224 | arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | static int proc_unlinkurb(struct dev_state *ps, void __user *arg) |
| 1228 | { |
| 1229 | struct async *as; |
| 1230 | |
| 1231 | as = async_getpending(ps, arg); |
| 1232 | if (!as) |
| 1233 | return -EINVAL; |
| 1234 | usb_kill_urb(as->urb); |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
| 1238 | static int processcompl(struct async *as, void __user * __user *arg) |
| 1239 | { |
| 1240 | struct urb *urb = as->urb; |
| 1241 | struct usbdevfs_urb __user *userurb = as->userurb; |
| 1242 | void __user *addr = as->userurb; |
| 1243 | unsigned int i; |
| 1244 | |
| 1245 | if (as->userbuffer) |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1246 | if (copy_to_user(as->userbuffer, urb->transfer_buffer, |
| 1247 | urb->transfer_buffer_length)) |
Oliver Neukum | d794a02 | 2009-06-28 23:34:14 +0200 | [diff] [blame] | 1248 | goto err_out; |
Alan Stern | e015268 | 2007-08-24 15:42:52 -0400 | [diff] [blame] | 1249 | if (put_user(as->status, &userurb->status)) |
Oliver Neukum | d794a02 | 2009-06-28 23:34:14 +0200 | [diff] [blame] | 1250 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | if (put_user(urb->actual_length, &userurb->actual_length)) |
Oliver Neukum | d794a02 | 2009-06-28 23:34:14 +0200 | [diff] [blame] | 1252 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | if (put_user(urb->error_count, &userurb->error_count)) |
Oliver Neukum | d794a02 | 2009-06-28 23:34:14 +0200 | [diff] [blame] | 1254 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1256 | if (usb_endpoint_xfer_isoc(&urb->ep->desc)) { |
Christopher Li | 668a954 | 2005-04-18 17:39:26 -0700 | [diff] [blame] | 1257 | for (i = 0; i < urb->number_of_packets; i++) { |
| 1258 | if (put_user(urb->iso_frame_desc[i].actual_length, |
| 1259 | &userurb->iso_frame_desc[i].actual_length)) |
Oliver Neukum | d794a02 | 2009-06-28 23:34:14 +0200 | [diff] [blame] | 1260 | goto err_out; |
Christopher Li | 668a954 | 2005-04-18 17:39:26 -0700 | [diff] [blame] | 1261 | if (put_user(urb->iso_frame_desc[i].status, |
| 1262 | &userurb->iso_frame_desc[i].status)) |
Oliver Neukum | d794a02 | 2009-06-28 23:34:14 +0200 | [diff] [blame] | 1263 | goto err_out; |
Christopher Li | 668a954 | 2005-04-18 17:39:26 -0700 | [diff] [blame] | 1264 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | free_async(as); |
| 1268 | |
| 1269 | if (put_user(addr, (void __user * __user *)arg)) |
| 1270 | return -EFAULT; |
| 1271 | return 0; |
Oliver Neukum | d794a02 | 2009-06-28 23:34:14 +0200 | [diff] [blame] | 1272 | |
| 1273 | err_out: |
| 1274 | free_async(as); |
| 1275 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | } |
| 1277 | |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1278 | static struct async *reap_as(struct dev_state *ps) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | { |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1280 | DECLARE_WAITQUEUE(wait, current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | struct async *as = NULL; |
| 1282 | struct usb_device *dev = ps->dev; |
| 1283 | |
| 1284 | add_wait_queue(&ps->wait, &wait); |
| 1285 | for (;;) { |
| 1286 | __set_current_state(TASK_INTERRUPTIBLE); |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1287 | as = async_getcompleted(ps); |
| 1288 | if (as) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | break; |
| 1290 | if (signal_pending(current)) |
| 1291 | break; |
| 1292 | usb_unlock_device(dev); |
| 1293 | schedule(); |
| 1294 | usb_lock_device(dev); |
| 1295 | } |
| 1296 | remove_wait_queue(&ps->wait, &wait); |
| 1297 | set_current_state(TASK_RUNNING); |
| 1298 | return as; |
| 1299 | } |
| 1300 | |
| 1301 | static int proc_reapurb(struct dev_state *ps, void __user *arg) |
| 1302 | { |
| 1303 | struct async *as = reap_as(ps); |
| 1304 | if (as) |
| 1305 | return processcompl(as, (void __user * __user *)arg); |
| 1306 | if (signal_pending(current)) |
| 1307 | return -EINTR; |
| 1308 | return -EIO; |
| 1309 | } |
| 1310 | |
| 1311 | static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg) |
| 1312 | { |
| 1313 | struct async *as; |
| 1314 | |
| 1315 | if (!(as = async_getcompleted(ps))) |
| 1316 | return -EAGAIN; |
| 1317 | return processcompl(as, (void __user * __user *)arg); |
| 1318 | } |
| 1319 | |
| 1320 | #ifdef CONFIG_COMPAT |
| 1321 | |
| 1322 | static int get_urb32(struct usbdevfs_urb *kurb, |
| 1323 | struct usbdevfs_urb32 __user *uurb) |
| 1324 | { |
| 1325 | __u32 uptr; |
Michael Buesch | 18753eb | 2009-07-29 11:39:03 +0200 | [diff] [blame] | 1326 | if (!access_ok(VERIFY_READ, uurb, sizeof(*uurb)) || |
| 1327 | __get_user(kurb->type, &uurb->type) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | __get_user(kurb->endpoint, &uurb->endpoint) || |
| 1329 | __get_user(kurb->status, &uurb->status) || |
| 1330 | __get_user(kurb->flags, &uurb->flags) || |
| 1331 | __get_user(kurb->buffer_length, &uurb->buffer_length) || |
| 1332 | __get_user(kurb->actual_length, &uurb->actual_length) || |
| 1333 | __get_user(kurb->start_frame, &uurb->start_frame) || |
| 1334 | __get_user(kurb->number_of_packets, &uurb->number_of_packets) || |
| 1335 | __get_user(kurb->error_count, &uurb->error_count) || |
| 1336 | __get_user(kurb->signr, &uurb->signr)) |
| 1337 | return -EFAULT; |
| 1338 | |
| 1339 | if (__get_user(uptr, &uurb->buffer)) |
| 1340 | return -EFAULT; |
| 1341 | kurb->buffer = compat_ptr(uptr); |
Mark Lord | ed0c772 | 2009-01-02 02:48:24 -0500 | [diff] [blame] | 1342 | if (__get_user(uptr, &uurb->usercontext)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | return -EFAULT; |
| 1344 | kurb->usercontext = compat_ptr(uptr); |
| 1345 | |
| 1346 | return 0; |
| 1347 | } |
| 1348 | |
| 1349 | static int proc_submiturb_compat(struct dev_state *ps, void __user *arg) |
| 1350 | { |
| 1351 | struct usbdevfs_urb uurb; |
| 1352 | |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1353 | if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | return -EFAULT; |
| 1355 | |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1356 | return proc_do_submiturb(ps, &uurb, |
| 1357 | ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc, |
| 1358 | arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | static int processcompl_compat(struct async *as, void __user * __user *arg) |
| 1362 | { |
| 1363 | struct urb *urb = as->urb; |
| 1364 | struct usbdevfs_urb32 __user *userurb = as->userurb; |
| 1365 | void __user *addr = as->userurb; |
| 1366 | unsigned int i; |
| 1367 | |
| 1368 | if (as->userbuffer) |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1369 | if (copy_to_user(as->userbuffer, urb->transfer_buffer, |
| 1370 | urb->transfer_buffer_length)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | return -EFAULT; |
Alan Stern | e015268 | 2007-08-24 15:42:52 -0400 | [diff] [blame] | 1372 | if (put_user(as->status, &userurb->status)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | return -EFAULT; |
| 1374 | if (put_user(urb->actual_length, &userurb->actual_length)) |
| 1375 | return -EFAULT; |
| 1376 | if (put_user(urb->error_count, &userurb->error_count)) |
| 1377 | return -EFAULT; |
| 1378 | |
Alan Stern | 93cf9b9 | 2007-07-30 17:09:28 -0400 | [diff] [blame] | 1379 | if (usb_endpoint_xfer_isoc(&urb->ep->desc)) { |
Christopher Li | 668a954 | 2005-04-18 17:39:26 -0700 | [diff] [blame] | 1380 | for (i = 0; i < urb->number_of_packets; i++) { |
| 1381 | if (put_user(urb->iso_frame_desc[i].actual_length, |
| 1382 | &userurb->iso_frame_desc[i].actual_length)) |
| 1383 | return -EFAULT; |
| 1384 | if (put_user(urb->iso_frame_desc[i].status, |
| 1385 | &userurb->iso_frame_desc[i].status)) |
| 1386 | return -EFAULT; |
| 1387 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | free_async(as); |
Al Viro | c714de5 | 2006-10-10 22:45:37 +0100 | [diff] [blame] | 1391 | if (put_user(ptr_to_compat(addr), (u32 __user *)arg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | return -EFAULT; |
| 1393 | return 0; |
| 1394 | } |
| 1395 | |
| 1396 | static int proc_reapurb_compat(struct dev_state *ps, void __user *arg) |
| 1397 | { |
| 1398 | struct async *as = reap_as(ps); |
| 1399 | if (as) |
| 1400 | return processcompl_compat(as, (void __user * __user *)arg); |
| 1401 | if (signal_pending(current)) |
| 1402 | return -EINTR; |
| 1403 | return -EIO; |
| 1404 | } |
| 1405 | |
| 1406 | static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg) |
| 1407 | { |
| 1408 | struct async *as; |
| 1409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | if (!(as = async_getcompleted(ps))) |
| 1411 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | return processcompl_compat(as, (void __user * __user *)arg); |
| 1413 | } |
| 1414 | |
| 1415 | #endif |
| 1416 | |
| 1417 | static int proc_disconnectsignal(struct dev_state *ps, void __user *arg) |
| 1418 | { |
| 1419 | struct usbdevfs_disconnectsignal ds; |
| 1420 | |
| 1421 | if (copy_from_user(&ds, arg, sizeof(ds))) |
| 1422 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | ps->discsignr = ds.signr; |
| 1424 | ps->disccontext = ds.context; |
| 1425 | return 0; |
| 1426 | } |
| 1427 | |
| 1428 | static int proc_claiminterface(struct dev_state *ps, void __user *arg) |
| 1429 | { |
| 1430 | unsigned int ifnum; |
| 1431 | |
| 1432 | if (get_user(ifnum, (unsigned int __user *)arg)) |
| 1433 | return -EFAULT; |
| 1434 | return claimintf(ps, ifnum); |
| 1435 | } |
| 1436 | |
| 1437 | static int proc_releaseinterface(struct dev_state *ps, void __user *arg) |
| 1438 | { |
| 1439 | unsigned int ifnum; |
| 1440 | int ret; |
| 1441 | |
| 1442 | if (get_user(ifnum, (unsigned int __user *)arg)) |
| 1443 | return -EFAULT; |
| 1444 | if ((ret = releaseintf(ps, ifnum)) < 0) |
| 1445 | return ret; |
| 1446 | destroy_async_on_interface (ps, ifnum); |
| 1447 | return 0; |
| 1448 | } |
| 1449 | |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1450 | static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | int size; |
| 1453 | void *buf = NULL; |
| 1454 | int retval = 0; |
| 1455 | struct usb_interface *intf = NULL; |
| 1456 | struct usb_driver *driver = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1458 | /* alloc buffer */ |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1459 | if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) { |
| 1460 | if ((buf = kmalloc(size, GFP_KERNEL)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | return -ENOMEM; |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1462 | if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) { |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1463 | if (copy_from_user(buf, ctl->data, size)) { |
Jesper Juhl | 6fd19f4 | 2005-04-18 17:39:33 -0700 | [diff] [blame] | 1464 | kfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | return -EFAULT; |
| 1466 | } |
| 1467 | } else { |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1468 | memset(buf, 0, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | } |
| 1470 | } |
| 1471 | |
Alan Stern | 349710c | 2006-07-01 22:05:56 -0400 | [diff] [blame] | 1472 | if (!connected(ps)) { |
Jesper Juhl | 6fd19f4 | 2005-04-18 17:39:33 -0700 | [diff] [blame] | 1473 | kfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | return -ENODEV; |
| 1475 | } |
| 1476 | |
| 1477 | if (ps->dev->state != USB_STATE_CONFIGURED) |
| 1478 | retval = -EHOSTUNREACH; |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1479 | else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno))) |
| 1480 | retval = -EINVAL; |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1481 | else switch (ctl->ioctl_code) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
| 1483 | /* disconnect kernel driver from interface */ |
| 1484 | case USBDEVFS_DISCONNECT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | if (intf->dev.driver) { |
| 1486 | driver = to_usb_driver(intf->dev.driver); |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1487 | dev_dbg(&intf->dev, "disconnect by usbfs\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | usb_driver_release_interface(driver, intf); |
| 1489 | } else |
| 1490 | retval = -ENODATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | break; |
| 1492 | |
| 1493 | /* let kernel drivers try to (re)bind to the interface */ |
| 1494 | case USBDEVFS_CONNECT: |
Alan Stern | 885e974 | 2007-12-03 15:42:10 -0500 | [diff] [blame] | 1495 | if (!intf->dev.driver) |
| 1496 | retval = device_attach(&intf->dev); |
| 1497 | else |
| 1498 | retval = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | break; |
| 1500 | |
| 1501 | /* talk directly to the interface's driver */ |
| 1502 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | if (intf->dev.driver) |
| 1504 | driver = to_usb_driver(intf->dev.driver); |
| 1505 | if (driver == NULL || driver->ioctl == NULL) { |
| 1506 | retval = -ENOTTY; |
| 1507 | } else { |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1508 | retval = driver->ioctl(intf, ctl->ioctl_code, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | if (retval == -ENOIOCTLCMD) |
| 1510 | retval = -ENOTTY; |
| 1511 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | /* cleanup and return */ |
| 1515 | if (retval >= 0 |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1516 | && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | && size > 0 |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1518 | && copy_to_user(ctl->data, buf, size) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | retval = -EFAULT; |
Jesper Juhl | 6fd19f4 | 2005-04-18 17:39:33 -0700 | [diff] [blame] | 1520 | |
| 1521 | kfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | return retval; |
| 1523 | } |
| 1524 | |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1525 | static int proc_ioctl_default(struct dev_state *ps, void __user *arg) |
| 1526 | { |
| 1527 | struct usbdevfs_ioctl ctrl; |
| 1528 | |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1529 | if (copy_from_user(&ctrl, arg, sizeof(ctrl))) |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1530 | return -EFAULT; |
| 1531 | return proc_ioctl(ps, &ctrl); |
| 1532 | } |
| 1533 | |
| 1534 | #ifdef CONFIG_COMPAT |
Andrew Morton | 777da59 | 2005-11-17 09:47:02 -0800 | [diff] [blame] | 1535 | static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg) |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1536 | { |
| 1537 | struct usbdevfs_ioctl32 __user *uioc; |
| 1538 | struct usbdevfs_ioctl ctrl; |
| 1539 | u32 udata; |
| 1540 | |
Andrew Morton | 058120d | 2005-11-17 09:47:49 -0800 | [diff] [blame] | 1541 | uioc = compat_ptr((long)arg); |
Michael Buesch | 18753eb | 2009-07-29 11:39:03 +0200 | [diff] [blame] | 1542 | if (!access_ok(VERIFY_READ, uioc, sizeof(*uioc)) || |
| 1543 | __get_user(ctrl.ifno, &uioc->ifno) || |
| 1544 | __get_user(ctrl.ioctl_code, &uioc->ioctl_code) || |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1545 | __get_user(udata, &uioc->data)) |
| 1546 | return -EFAULT; |
| 1547 | ctrl.data = compat_ptr(udata); |
| 1548 | |
| 1549 | return proc_ioctl(ps, &ctrl); |
| 1550 | } |
| 1551 | #endif |
| 1552 | |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame^] | 1553 | static int proc_claim_port(struct dev_state *ps, void __user *arg) |
| 1554 | { |
| 1555 | unsigned portnum; |
| 1556 | int rc; |
| 1557 | |
| 1558 | if (get_user(portnum, (unsigned __user *) arg)) |
| 1559 | return -EFAULT; |
| 1560 | rc = usb_hub_claim_port(ps->dev, portnum, ps); |
| 1561 | if (rc == 0) |
| 1562 | snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n", |
| 1563 | portnum, task_pid_nr(current), current->comm); |
| 1564 | return rc; |
| 1565 | } |
| 1566 | |
| 1567 | static int proc_release_port(struct dev_state *ps, void __user *arg) |
| 1568 | { |
| 1569 | unsigned portnum; |
| 1570 | |
| 1571 | if (get_user(portnum, (unsigned __user *) arg)) |
| 1572 | return -EFAULT; |
| 1573 | return usb_hub_release_port(ps->dev, portnum, ps); |
| 1574 | } |
| 1575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | /* |
| 1577 | * NOTE: All requests here that have interface numbers as parameters |
| 1578 | * are assuming that somehow the configuration has been prevented from |
| 1579 | * changing. But there's no mechanism to ensure that... |
| 1580 | */ |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1581 | static int usbdev_ioctl(struct inode *inode, struct file *file, |
| 1582 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | { |
Tobias Klauser | ec17cf1 | 2006-09-13 21:38:41 +0200 | [diff] [blame] | 1584 | struct dev_state *ps = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | struct usb_device *dev = ps->dev; |
| 1586 | void __user *p = (void __user *)arg; |
| 1587 | int ret = -ENOTTY; |
| 1588 | |
| 1589 | if (!(file->f_mode & FMODE_WRITE)) |
| 1590 | return -EPERM; |
| 1591 | usb_lock_device(dev); |
Alan Stern | 349710c | 2006-07-01 22:05:56 -0400 | [diff] [blame] | 1592 | if (!connected(ps)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | usb_unlock_device(dev); |
| 1594 | return -ENODEV; |
| 1595 | } |
| 1596 | |
| 1597 | switch (cmd) { |
| 1598 | case USBDEVFS_CONTROL: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1599 | snoop(&dev->dev, "%s: CONTROL\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | ret = proc_control(ps, p); |
| 1601 | if (ret >= 0) |
| 1602 | inode->i_mtime = CURRENT_TIME; |
| 1603 | break; |
| 1604 | |
| 1605 | case USBDEVFS_BULK: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1606 | snoop(&dev->dev, "%s: BULK\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | ret = proc_bulk(ps, p); |
| 1608 | if (ret >= 0) |
| 1609 | inode->i_mtime = CURRENT_TIME; |
| 1610 | break; |
| 1611 | |
| 1612 | case USBDEVFS_RESETEP: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1613 | snoop(&dev->dev, "%s: RESETEP\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | ret = proc_resetep(ps, p); |
| 1615 | if (ret >= 0) |
| 1616 | inode->i_mtime = CURRENT_TIME; |
| 1617 | break; |
| 1618 | |
| 1619 | case USBDEVFS_RESET: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1620 | snoop(&dev->dev, "%s: RESET\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | ret = proc_resetdevice(ps); |
| 1622 | break; |
| 1623 | |
| 1624 | case USBDEVFS_CLEAR_HALT: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1625 | snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | ret = proc_clearhalt(ps, p); |
| 1627 | if (ret >= 0) |
| 1628 | inode->i_mtime = CURRENT_TIME; |
| 1629 | break; |
| 1630 | |
| 1631 | case USBDEVFS_GETDRIVER: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1632 | snoop(&dev->dev, "%s: GETDRIVER\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | ret = proc_getdriver(ps, p); |
| 1634 | break; |
| 1635 | |
| 1636 | case USBDEVFS_CONNECTINFO: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1637 | snoop(&dev->dev, "%s: CONNECTINFO\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | ret = proc_connectinfo(ps, p); |
| 1639 | break; |
| 1640 | |
| 1641 | case USBDEVFS_SETINTERFACE: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1642 | snoop(&dev->dev, "%s: SETINTERFACE\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | ret = proc_setintf(ps, p); |
| 1644 | break; |
| 1645 | |
| 1646 | case USBDEVFS_SETCONFIGURATION: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1647 | snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | ret = proc_setconfig(ps, p); |
| 1649 | break; |
| 1650 | |
| 1651 | case USBDEVFS_SUBMITURB: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1652 | snoop(&dev->dev, "%s: SUBMITURB\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | ret = proc_submiturb(ps, p); |
| 1654 | if (ret >= 0) |
| 1655 | inode->i_mtime = CURRENT_TIME; |
| 1656 | break; |
| 1657 | |
| 1658 | #ifdef CONFIG_COMPAT |
| 1659 | |
| 1660 | case USBDEVFS_SUBMITURB32: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1661 | snoop(&dev->dev, "%s: SUBMITURB32\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | ret = proc_submiturb_compat(ps, p); |
| 1663 | if (ret >= 0) |
| 1664 | inode->i_mtime = CURRENT_TIME; |
| 1665 | break; |
| 1666 | |
| 1667 | case USBDEVFS_REAPURB32: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1668 | snoop(&dev->dev, "%s: REAPURB32\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | ret = proc_reapurb_compat(ps, p); |
| 1670 | break; |
| 1671 | |
| 1672 | case USBDEVFS_REAPURBNDELAY32: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1673 | snoop(&dev->dev, "%s: REAPURBDELAY32\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | ret = proc_reapurbnonblock_compat(ps, p); |
| 1675 | break; |
| 1676 | |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1677 | case USBDEVFS_IOCTL32: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1678 | snoop(&dev->dev, "%s: IOCTL\n", __func__); |
Al Viro | c714de5 | 2006-10-10 22:45:37 +0100 | [diff] [blame] | 1679 | ret = proc_ioctl_compat(ps, ptr_to_compat(p)); |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1680 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | #endif |
| 1682 | |
| 1683 | case USBDEVFS_DISCARDURB: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1684 | snoop(&dev->dev, "%s: DISCARDURB\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | ret = proc_unlinkurb(ps, p); |
| 1686 | break; |
| 1687 | |
| 1688 | case USBDEVFS_REAPURB: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1689 | snoop(&dev->dev, "%s: REAPURB\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | ret = proc_reapurb(ps, p); |
| 1691 | break; |
| 1692 | |
| 1693 | case USBDEVFS_REAPURBNDELAY: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1694 | snoop(&dev->dev, "%s: REAPURBDELAY\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | ret = proc_reapurbnonblock(ps, p); |
| 1696 | break; |
| 1697 | |
| 1698 | case USBDEVFS_DISCSIGNAL: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1699 | snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | ret = proc_disconnectsignal(ps, p); |
| 1701 | break; |
| 1702 | |
| 1703 | case USBDEVFS_CLAIMINTERFACE: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1704 | snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | ret = proc_claiminterface(ps, p); |
| 1706 | break; |
| 1707 | |
| 1708 | case USBDEVFS_RELEASEINTERFACE: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1709 | snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | ret = proc_releaseinterface(ps, p); |
| 1711 | break; |
| 1712 | |
| 1713 | case USBDEVFS_IOCTL: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1714 | snoop(&dev->dev, "%s: IOCTL\n", __func__); |
Pete Zaitcev | c36fc88 | 2005-10-17 18:15:54 -0700 | [diff] [blame] | 1715 | ret = proc_ioctl_default(ps, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | break; |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame^] | 1717 | |
| 1718 | case USBDEVFS_CLAIM_PORT: |
| 1719 | snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__); |
| 1720 | ret = proc_claim_port(ps, p); |
| 1721 | break; |
| 1722 | |
| 1723 | case USBDEVFS_RELEASE_PORT: |
| 1724 | snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__); |
| 1725 | ret = proc_release_port(ps, p); |
| 1726 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | } |
| 1728 | usb_unlock_device(dev); |
| 1729 | if (ret >= 0) |
| 1730 | inode->i_atime = CURRENT_TIME; |
| 1731 | return ret; |
| 1732 | } |
| 1733 | |
| 1734 | /* No kernel lock - fine */ |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1735 | static unsigned int usbdev_poll(struct file *file, |
| 1736 | struct poll_table_struct *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | { |
Tobias Klauser | ec17cf1 | 2006-09-13 21:38:41 +0200 | [diff] [blame] | 1738 | struct dev_state *ps = file->private_data; |
| 1739 | unsigned int mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | |
| 1741 | poll_wait(file, &ps->wait, wait); |
| 1742 | if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed)) |
| 1743 | mask |= POLLOUT | POLLWRNORM; |
Alan Stern | 349710c | 2006-07-01 22:05:56 -0400 | [diff] [blame] | 1744 | if (!connected(ps)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | mask |= POLLERR | POLLHUP; |
| 1746 | return mask; |
| 1747 | } |
| 1748 | |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1749 | const struct file_operations usbdev_file_operations = { |
Greg Kroah-Hartman | 7e7654a | 2007-09-12 15:06:57 -0700 | [diff] [blame] | 1750 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | .llseek = usbdev_lseek, |
| 1752 | .read = usbdev_read, |
| 1753 | .poll = usbdev_poll, |
| 1754 | .ioctl = usbdev_ioctl, |
| 1755 | .open = usbdev_open, |
| 1756 | .release = usbdev_release, |
| 1757 | }; |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1758 | |
Alan Stern | 501950d | 2009-01-13 11:33:42 -0500 | [diff] [blame] | 1759 | static void usbdev_remove(struct usb_device *udev) |
Alan Stern | cd9f037 | 2008-06-24 14:47:04 -0400 | [diff] [blame] | 1760 | { |
| 1761 | struct dev_state *ps; |
| 1762 | struct siginfo sinfo; |
| 1763 | |
| 1764 | while (!list_empty(&udev->filelist)) { |
| 1765 | ps = list_entry(udev->filelist.next, struct dev_state, list); |
| 1766 | destroy_all_async(ps); |
| 1767 | wake_up_all(&ps->wait); |
| 1768 | list_del_init(&ps->list); |
| 1769 | if (ps->discsignr) { |
| 1770 | sinfo.si_signo = ps->discsignr; |
| 1771 | sinfo.si_errno = EPIPE; |
| 1772 | sinfo.si_code = SI_ASYNCIO; |
| 1773 | sinfo.si_addr = ps->disccontext; |
| 1774 | kill_pid_info_as_uid(ps->discsignr, &sinfo, |
| 1775 | ps->disc_pid, ps->disc_uid, |
| 1776 | ps->disc_euid, ps->secid); |
| 1777 | } |
| 1778 | } |
| 1779 | } |
| 1780 | |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1781 | #ifdef CONFIG_USB_DEVICE_CLASS |
| 1782 | static struct class *usb_classdev_class; |
| 1783 | |
| 1784 | static int usb_classdev_add(struct usb_device *dev) |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1785 | { |
Alan Stern | e04199b | 2008-06-24 14:47:29 -0400 | [diff] [blame] | 1786 | struct device *cldev; |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1787 | |
Greg Kroah-Hartman | b0b090e | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 1788 | cldev = device_create(usb_classdev_class, &dev->dev, dev->dev.devt, |
| 1789 | NULL, "usbdev%d.%d", dev->bus->busnum, |
| 1790 | dev->devnum); |
Alan Stern | e04199b | 2008-06-24 14:47:29 -0400 | [diff] [blame] | 1791 | if (IS_ERR(cldev)) |
| 1792 | return PTR_ERR(cldev); |
| 1793 | dev->usb_classdev = cldev; |
Akinobu Mita | 27d39e2 | 2006-10-09 18:09:33 +0900 | [diff] [blame] | 1794 | return 0; |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1795 | } |
| 1796 | |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1797 | static void usb_classdev_remove(struct usb_device *dev) |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1798 | { |
Alan Stern | e04199b | 2008-06-24 14:47:29 -0400 | [diff] [blame] | 1799 | if (dev->usb_classdev) |
| 1800 | device_unregister(dev->usb_classdev); |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1801 | } |
| 1802 | |
Alan Stern | 501950d | 2009-01-13 11:33:42 -0500 | [diff] [blame] | 1803 | #else |
| 1804 | #define usb_classdev_add(dev) 0 |
| 1805 | #define usb_classdev_remove(dev) do {} while (0) |
| 1806 | |
| 1807 | #endif |
| 1808 | |
| 1809 | static int usbdev_notify(struct notifier_block *self, |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1810 | unsigned long action, void *dev) |
Greg Kroah-Hartman | a7b986b | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1811 | { |
| 1812 | switch (action) { |
| 1813 | case USB_DEVICE_ADD: |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1814 | if (usb_classdev_add(dev)) |
Akinobu Mita | 27d39e2 | 2006-10-09 18:09:33 +0900 | [diff] [blame] | 1815 | return NOTIFY_BAD; |
Greg Kroah-Hartman | a7b986b | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1816 | break; |
| 1817 | case USB_DEVICE_REMOVE: |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1818 | usb_classdev_remove(dev); |
Alan Stern | 501950d | 2009-01-13 11:33:42 -0500 | [diff] [blame] | 1819 | usbdev_remove(dev); |
Greg Kroah-Hartman | a7b986b | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1820 | break; |
| 1821 | } |
| 1822 | return NOTIFY_OK; |
| 1823 | } |
| 1824 | |
| 1825 | static struct notifier_block usbdev_nb = { |
Alan Stern | 501950d | 2009-01-13 11:33:42 -0500 | [diff] [blame] | 1826 | .notifier_call = usbdev_notify, |
Greg Kroah-Hartman | a7b986b | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1827 | }; |
| 1828 | |
Greg Kroah-Hartman | 7e7654a | 2007-09-12 15:06:57 -0700 | [diff] [blame] | 1829 | static struct cdev usb_device_cdev; |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1830 | |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1831 | int __init usb_devio_init(void) |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1832 | { |
| 1833 | int retval; |
| 1834 | |
Alan Stern | fad21bd | 2005-08-10 15:15:57 -0400 | [diff] [blame] | 1835 | retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX, |
Greg Kroah-Hartman | 04e482f | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 1836 | "usb_device"); |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1837 | if (retval) { |
Greg Kroah-Hartman | 69a8594 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 1838 | printk(KERN_ERR "Unable to register minors for usb_device\n"); |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1839 | goto out; |
| 1840 | } |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1841 | cdev_init(&usb_device_cdev, &usbdev_file_operations); |
Alan Stern | fad21bd | 2005-08-10 15:15:57 -0400 | [diff] [blame] | 1842 | retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX); |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1843 | if (retval) { |
Greg Kroah-Hartman | 69a8594 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 1844 | printk(KERN_ERR "Unable to get usb_device major %d\n", |
| 1845 | USB_DEVICE_MAJOR); |
Greg Kroah-Hartman | a7b986b | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1846 | goto error_cdev; |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1847 | } |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1848 | #ifdef CONFIG_USB_DEVICE_CLASS |
| 1849 | usb_classdev_class = class_create(THIS_MODULE, "usb_device"); |
| 1850 | if (IS_ERR(usb_classdev_class)) { |
Greg Kroah-Hartman | 69a8594 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 1851 | printk(KERN_ERR "Unable to register usb_device class\n"); |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1852 | retval = PTR_ERR(usb_classdev_class); |
| 1853 | cdev_del(&usb_device_cdev); |
| 1854 | usb_classdev_class = NULL; |
| 1855 | goto out; |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1856 | } |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 1857 | /* devices of this class shadow the major:minor of their parent |
| 1858 | * device, so clear ->dev_kobj to prevent adding duplicate entries |
| 1859 | * to /sys/dev |
| 1860 | */ |
| 1861 | usb_classdev_class->dev_kobj = NULL; |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1862 | #endif |
Alan Stern | 501950d | 2009-01-13 11:33:42 -0500 | [diff] [blame] | 1863 | usb_register_notify(&usbdev_nb); |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1864 | out: |
| 1865 | return retval; |
Greg Kroah-Hartman | a7b986b | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1866 | |
Greg Kroah-Hartman | a7b986b | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1867 | error_cdev: |
| 1868 | unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX); |
| 1869 | goto out; |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1870 | } |
| 1871 | |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1872 | void usb_devio_cleanup(void) |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1873 | { |
Greg Kroah-Hartman | a7b986b | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1874 | usb_unregister_notify(&usbdev_nb); |
Alan Stern | 501950d | 2009-01-13 11:33:42 -0500 | [diff] [blame] | 1875 | #ifdef CONFIG_USB_DEVICE_CLASS |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 1876 | class_destroy(usb_classdev_class); |
| 1877 | #endif |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1878 | cdev_del(&usb_device_cdev); |
Alan Stern | fad21bd | 2005-08-10 15:15:57 -0400 | [diff] [blame] | 1879 | unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX); |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1880 | } |