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