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