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