Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 1 | /***************************************************************************** |
| 2 | * File: drivers/usb/misc/vstusb.c |
| 3 | * |
| 4 | * Purpose: Support for the bulk USB Vernier Spectrophotometers |
| 5 | * |
| 6 | * Author: Johnnie Peters |
| 7 | * Axian Consulting |
| 8 | * Beaverton, OR, USA 97005 |
| 9 | * |
| 10 | * Modified by: EQware Engineering, Inc. |
| 11 | * Oregon City, OR, USA 97045 |
| 12 | * |
| 13 | * Copyright: 2007, 2008 |
| 14 | * Vernier Software & Technology |
| 15 | * Beaverton, OR, USA 97005 |
| 16 | * |
| 17 | * Web: www.vernier.com |
| 18 | * |
| 19 | * This program is free software; you can redistribute it and/or modify |
| 20 | * it under the terms of the GNU General Public License version 2 as |
| 21 | * published by the Free Software Foundation. |
| 22 | * |
| 23 | *****************************************************************************/ |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/mutex.h> |
Oliver Neukum | 8626645 | 2010-01-13 15:33:15 +0100 | [diff] [blame^] | 30 | #include <linux/smp_lock.h> |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 31 | #include <linux/uaccess.h> |
| 32 | #include <linux/usb.h> |
| 33 | |
| 34 | #include <linux/usb/vstusb.h> |
| 35 | |
| 36 | #define DRIVER_VERSION "VST USB Driver Version 1.5" |
| 37 | #define DRIVER_DESC "Vernier Software Technology Bulk USB Driver" |
| 38 | |
| 39 | #ifdef CONFIG_USB_DYNAMIC_MINORS |
| 40 | #define VSTUSB_MINOR_BASE 0 |
| 41 | #else |
| 42 | #define VSTUSB_MINOR_BASE 199 |
| 43 | #endif |
| 44 | |
| 45 | #define USB_VENDOR_OCEANOPTICS 0x2457 |
| 46 | #define USB_VENDOR_VERNIER 0x08F7 /* Vernier Software & Technology */ |
| 47 | |
| 48 | #define USB_PRODUCT_USB2000 0x1002 |
| 49 | #define USB_PRODUCT_ADC1000_FW 0x1003 /* firmware download (renumerates) */ |
| 50 | #define USB_PRODUCT_ADC1000 0x1004 |
| 51 | #define USB_PRODUCT_HR2000_FW 0x1009 /* firmware download (renumerates) */ |
| 52 | #define USB_PRODUCT_HR2000 0x100A |
| 53 | #define USB_PRODUCT_HR4000_FW 0x1011 /* firmware download (renumerates) */ |
| 54 | #define USB_PRODUCT_HR4000 0x1012 |
| 55 | #define USB_PRODUCT_USB650 0x1014 /* "Red Tide" */ |
| 56 | #define USB_PRODUCT_QE65000 0x1018 |
| 57 | #define USB_PRODUCT_USB4000 0x1022 |
| 58 | #define USB_PRODUCT_USB325 0x1024 /* "Vernier Spectrometer" */ |
| 59 | |
| 60 | #define USB_PRODUCT_LABPRO 0x0001 |
| 61 | #define USB_PRODUCT_LABQUEST 0x0005 |
| 62 | |
Stephen Ware | 84dcd59 | 2008-10-08 10:53:56 -0700 | [diff] [blame] | 63 | #define VST_MAXBUFFER (64*1024) |
| 64 | |
Németh Márton | 33b9e16 | 2010-01-10 15:34:45 +0100 | [diff] [blame] | 65 | static const struct usb_device_id id_table[] = { |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 66 | { USB_DEVICE(USB_VENDOR_OCEANOPTICS, USB_PRODUCT_USB2000)}, |
| 67 | { USB_DEVICE(USB_VENDOR_OCEANOPTICS, USB_PRODUCT_HR4000)}, |
| 68 | { USB_DEVICE(USB_VENDOR_OCEANOPTICS, USB_PRODUCT_USB650)}, |
| 69 | { USB_DEVICE(USB_VENDOR_OCEANOPTICS, USB_PRODUCT_USB4000)}, |
| 70 | { USB_DEVICE(USB_VENDOR_OCEANOPTICS, USB_PRODUCT_USB325)}, |
| 71 | { USB_DEVICE(USB_VENDOR_VERNIER, USB_PRODUCT_LABQUEST)}, |
| 72 | { USB_DEVICE(USB_VENDOR_VERNIER, USB_PRODUCT_LABPRO)}, |
| 73 | {}, |
| 74 | }; |
| 75 | |
| 76 | MODULE_DEVICE_TABLE(usb, id_table); |
| 77 | |
| 78 | struct vstusb_device { |
Stephen Ware | 84dcd59 | 2008-10-08 10:53:56 -0700 | [diff] [blame] | 79 | struct kref kref; |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 80 | struct mutex lock; |
| 81 | struct usb_device *usb_dev; |
| 82 | char present; |
| 83 | char isopen; |
| 84 | struct usb_anchor submitted; |
| 85 | int rd_pipe; |
| 86 | int rd_timeout_ms; |
| 87 | int wr_pipe; |
| 88 | int wr_timeout_ms; |
| 89 | }; |
Stephen Ware | 84dcd59 | 2008-10-08 10:53:56 -0700 | [diff] [blame] | 90 | #define to_vst_dev(d) container_of(d, struct vstusb_device, kref) |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 91 | |
| 92 | static struct usb_driver vstusb_driver; |
| 93 | |
Stephen Ware | 84dcd59 | 2008-10-08 10:53:56 -0700 | [diff] [blame] | 94 | static void vstusb_delete(struct kref *kref) |
| 95 | { |
| 96 | struct vstusb_device *vstdev = to_vst_dev(kref); |
| 97 | |
| 98 | usb_put_dev(vstdev->usb_dev); |
| 99 | kfree(vstdev); |
| 100 | } |
| 101 | |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 102 | static int vstusb_open(struct inode *inode, struct file *file) |
| 103 | { |
| 104 | struct vstusb_device *vstdev; |
| 105 | struct usb_interface *interface; |
| 106 | |
Oliver Neukum | 8626645 | 2010-01-13 15:33:15 +0100 | [diff] [blame^] | 107 | lock_kernel(); |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 108 | interface = usb_find_interface(&vstusb_driver, iminor(inode)); |
| 109 | |
| 110 | if (!interface) { |
| 111 | printk(KERN_ERR KBUILD_MODNAME |
| 112 | ": %s - error, can't find device for minor %d\n", |
| 113 | __func__, iminor(inode)); |
Oliver Neukum | 8626645 | 2010-01-13 15:33:15 +0100 | [diff] [blame^] | 114 | unlock_kernel(); |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 115 | return -ENODEV; |
| 116 | } |
| 117 | |
| 118 | vstdev = usb_get_intfdata(interface); |
| 119 | |
Oliver Neukum | 8626645 | 2010-01-13 15:33:15 +0100 | [diff] [blame^] | 120 | if (!vstdev) { |
| 121 | unlock_kernel(); |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 122 | return -ENODEV; |
Oliver Neukum | 8626645 | 2010-01-13 15:33:15 +0100 | [diff] [blame^] | 123 | } |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 124 | |
| 125 | /* lock this device */ |
| 126 | mutex_lock(&vstdev->lock); |
| 127 | |
| 128 | /* can only open one time */ |
| 129 | if ((!vstdev->present) || (vstdev->isopen)) { |
| 130 | mutex_unlock(&vstdev->lock); |
Oliver Neukum | 8626645 | 2010-01-13 15:33:15 +0100 | [diff] [blame^] | 131 | unlock_kernel(); |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 132 | return -EBUSY; |
| 133 | } |
| 134 | |
Stephen Ware | 84dcd59 | 2008-10-08 10:53:56 -0700 | [diff] [blame] | 135 | /* increment our usage count */ |
| 136 | kref_get(&vstdev->kref); |
| 137 | |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 138 | vstdev->isopen = 1; |
| 139 | |
| 140 | /* save device in the file's private structure */ |
| 141 | file->private_data = vstdev; |
| 142 | |
| 143 | dev_dbg(&vstdev->usb_dev->dev, "%s: opened\n", __func__); |
| 144 | |
| 145 | mutex_unlock(&vstdev->lock); |
Oliver Neukum | 8626645 | 2010-01-13 15:33:15 +0100 | [diff] [blame^] | 146 | unlock_kernel(); |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
Stephen Ware | 84dcd59 | 2008-10-08 10:53:56 -0700 | [diff] [blame] | 151 | static int vstusb_release(struct inode *inode, struct file *file) |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 152 | { |
| 153 | struct vstusb_device *vstdev; |
| 154 | |
| 155 | vstdev = file->private_data; |
| 156 | |
| 157 | if (vstdev == NULL) |
| 158 | return -ENODEV; |
| 159 | |
| 160 | mutex_lock(&vstdev->lock); |
| 161 | |
| 162 | vstdev->isopen = 0; |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 163 | |
Stephen Ware | 84dcd59 | 2008-10-08 10:53:56 -0700 | [diff] [blame] | 164 | dev_dbg(&vstdev->usb_dev->dev, "%s: released\n", __func__); |
| 165 | |
| 166 | mutex_unlock(&vstdev->lock); |
| 167 | |
| 168 | kref_put(&vstdev->kref, vstusb_delete); |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static void usb_api_blocking_completion(struct urb *urb) |
| 174 | { |
| 175 | struct completion *completeit = urb->context; |
| 176 | |
| 177 | complete(completeit); |
| 178 | } |
| 179 | |
| 180 | static int vstusb_fill_and_send_urb(struct urb *urb, |
| 181 | struct usb_device *usb_dev, |
| 182 | unsigned int pipe, void *data, |
| 183 | unsigned int len, struct completion *done) |
| 184 | { |
| 185 | struct usb_host_endpoint *ep; |
| 186 | struct usb_host_endpoint **hostep; |
| 187 | unsigned int pipend; |
| 188 | |
| 189 | int status; |
| 190 | |
| 191 | hostep = usb_pipein(pipe) ? usb_dev->ep_in : usb_dev->ep_out; |
| 192 | pipend = usb_pipeendpoint(pipe); |
| 193 | ep = hostep[pipend]; |
| 194 | |
| 195 | if (!ep || (len == 0)) |
| 196 | return -EINVAL; |
| 197 | |
| 198 | if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) |
| 199 | == USB_ENDPOINT_XFER_INT) { |
| 200 | pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30); |
| 201 | usb_fill_int_urb(urb, usb_dev, pipe, data, len, |
| 202 | (usb_complete_t)usb_api_blocking_completion, |
| 203 | NULL, ep->desc.bInterval); |
| 204 | } else |
| 205 | usb_fill_bulk_urb(urb, usb_dev, pipe, data, len, |
| 206 | (usb_complete_t)usb_api_blocking_completion, |
| 207 | NULL); |
| 208 | |
| 209 | init_completion(done); |
| 210 | urb->context = done; |
| 211 | urb->actual_length = 0; |
| 212 | status = usb_submit_urb(urb, GFP_KERNEL); |
| 213 | |
| 214 | return status; |
| 215 | } |
| 216 | |
| 217 | static int vstusb_complete_urb(struct urb *urb, struct completion *done, |
| 218 | int timeout, int *actual_length) |
| 219 | { |
| 220 | unsigned long expire; |
| 221 | int status; |
| 222 | |
| 223 | expire = timeout ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT; |
| 224 | if (!wait_for_completion_interruptible_timeout(done, expire)) { |
| 225 | usb_kill_urb(urb); |
| 226 | status = urb->status == -ENOENT ? -ETIMEDOUT : urb->status; |
| 227 | |
| 228 | dev_dbg(&urb->dev->dev, |
| 229 | "%s timed out on ep%d%s len=%d/%d, urb status = %d\n", |
| 230 | current->comm, |
| 231 | usb_pipeendpoint(urb->pipe), |
| 232 | usb_pipein(urb->pipe) ? "in" : "out", |
| 233 | urb->actual_length, |
| 234 | urb->transfer_buffer_length, |
| 235 | urb->status); |
| 236 | |
| 237 | } else { |
| 238 | if (signal_pending(current)) { |
| 239 | /* if really an error */ |
| 240 | if (urb->status && !((urb->status == -ENOENT) || |
| 241 | (urb->status == -ECONNRESET) || |
| 242 | (urb->status == -ESHUTDOWN))) { |
| 243 | status = -EINTR; |
| 244 | usb_kill_urb(urb); |
| 245 | } else { |
| 246 | status = 0; |
| 247 | } |
| 248 | |
| 249 | dev_dbg(&urb->dev->dev, |
| 250 | "%s: signal pending on ep%d%s len=%d/%d," |
| 251 | "urb status = %d\n", |
| 252 | current->comm, |
| 253 | usb_pipeendpoint(urb->pipe), |
| 254 | usb_pipein(urb->pipe) ? "in" : "out", |
| 255 | urb->actual_length, |
| 256 | urb->transfer_buffer_length, |
| 257 | urb->status); |
| 258 | |
| 259 | } else { |
| 260 | status = urb->status; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | if (actual_length) |
| 265 | *actual_length = urb->actual_length; |
| 266 | |
| 267 | return status; |
| 268 | } |
| 269 | |
| 270 | static ssize_t vstusb_read(struct file *file, char __user *buffer, |
| 271 | size_t count, loff_t *ppos) |
| 272 | { |
| 273 | struct vstusb_device *vstdev; |
| 274 | int cnt = -1; |
| 275 | void *buf; |
| 276 | int retval = 0; |
| 277 | |
| 278 | struct urb *urb; |
| 279 | struct usb_device *dev; |
| 280 | unsigned int pipe; |
| 281 | int timeout; |
| 282 | |
| 283 | DECLARE_COMPLETION_ONSTACK(done); |
| 284 | |
| 285 | vstdev = file->private_data; |
| 286 | |
| 287 | if (vstdev == NULL) |
| 288 | return -ENODEV; |
| 289 | |
| 290 | /* verify that we actually want to read some data */ |
Stephen Ware | 84dcd59 | 2008-10-08 10:53:56 -0700 | [diff] [blame] | 291 | if ((count == 0) || (count > VST_MAXBUFFER)) |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 292 | return -EINVAL; |
| 293 | |
| 294 | /* lock this object */ |
| 295 | if (mutex_lock_interruptible(&vstdev->lock)) |
| 296 | return -ERESTARTSYS; |
| 297 | |
| 298 | /* anyone home */ |
| 299 | if (!vstdev->present) { |
| 300 | mutex_unlock(&vstdev->lock); |
| 301 | printk(KERN_ERR KBUILD_MODNAME |
| 302 | ": %s: device not present\n", __func__); |
| 303 | return -ENODEV; |
| 304 | } |
| 305 | |
| 306 | /* pull out the necessary data */ |
| 307 | dev = vstdev->usb_dev; |
| 308 | pipe = usb_rcvbulkpipe(dev, vstdev->rd_pipe); |
| 309 | timeout = vstdev->rd_timeout_ms; |
| 310 | |
| 311 | buf = kmalloc(count, GFP_KERNEL); |
| 312 | if (buf == NULL) { |
| 313 | mutex_unlock(&vstdev->lock); |
| 314 | return -ENOMEM; |
| 315 | } |
| 316 | |
| 317 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 318 | if (!urb) { |
| 319 | kfree(buf); |
| 320 | mutex_unlock(&vstdev->lock); |
| 321 | return -ENOMEM; |
| 322 | } |
| 323 | |
| 324 | usb_anchor_urb(urb, &vstdev->submitted); |
| 325 | retval = vstusb_fill_and_send_urb(urb, dev, pipe, buf, count, &done); |
| 326 | mutex_unlock(&vstdev->lock); |
| 327 | if (retval) { |
| 328 | usb_unanchor_urb(urb); |
| 329 | dev_err(&dev->dev, "%s: error %d filling and sending urb %d\n", |
| 330 | __func__, retval, pipe); |
| 331 | goto exit; |
| 332 | } |
| 333 | |
| 334 | retval = vstusb_complete_urb(urb, &done, timeout, &cnt); |
| 335 | if (retval) { |
| 336 | dev_err(&dev->dev, "%s: error %d completing urb %d\n", |
| 337 | __func__, retval, pipe); |
| 338 | goto exit; |
| 339 | } |
| 340 | |
| 341 | if (copy_to_user(buffer, buf, cnt)) { |
| 342 | dev_err(&dev->dev, "%s: can't copy_to_user\n", __func__); |
| 343 | retval = -EFAULT; |
| 344 | } else { |
| 345 | retval = cnt; |
| 346 | dev_dbg(&dev->dev, "%s: read %d bytes from pipe %d\n", |
| 347 | __func__, cnt, pipe); |
| 348 | } |
| 349 | |
| 350 | exit: |
| 351 | usb_free_urb(urb); |
| 352 | kfree(buf); |
| 353 | return retval; |
| 354 | } |
| 355 | |
| 356 | static ssize_t vstusb_write(struct file *file, const char __user *buffer, |
| 357 | size_t count, loff_t *ppos) |
| 358 | { |
| 359 | struct vstusb_device *vstdev; |
| 360 | int cnt = -1; |
| 361 | void *buf; |
| 362 | int retval = 0; |
| 363 | |
| 364 | struct urb *urb; |
| 365 | struct usb_device *dev; |
| 366 | unsigned int pipe; |
| 367 | int timeout; |
| 368 | |
| 369 | DECLARE_COMPLETION_ONSTACK(done); |
| 370 | |
| 371 | vstdev = file->private_data; |
| 372 | |
| 373 | if (vstdev == NULL) |
| 374 | return -ENODEV; |
| 375 | |
| 376 | /* verify that we actually have some data to write */ |
Stephen Ware | 84dcd59 | 2008-10-08 10:53:56 -0700 | [diff] [blame] | 377 | if ((count == 0) || (count > VST_MAXBUFFER)) |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 378 | return retval; |
| 379 | |
| 380 | /* lock this object */ |
| 381 | if (mutex_lock_interruptible(&vstdev->lock)) |
| 382 | return -ERESTARTSYS; |
| 383 | |
| 384 | /* anyone home */ |
| 385 | if (!vstdev->present) { |
| 386 | mutex_unlock(&vstdev->lock); |
| 387 | printk(KERN_ERR KBUILD_MODNAME |
| 388 | ": %s: device not present\n", __func__); |
| 389 | return -ENODEV; |
| 390 | } |
| 391 | |
| 392 | /* pull out the necessary data */ |
| 393 | dev = vstdev->usb_dev; |
| 394 | pipe = usb_sndbulkpipe(dev, vstdev->wr_pipe); |
| 395 | timeout = vstdev->wr_timeout_ms; |
| 396 | |
| 397 | buf = kmalloc(count, GFP_KERNEL); |
| 398 | if (buf == NULL) { |
| 399 | mutex_unlock(&vstdev->lock); |
| 400 | return -ENOMEM; |
| 401 | } |
| 402 | |
| 403 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 404 | if (!urb) { |
| 405 | kfree(buf); |
| 406 | mutex_unlock(&vstdev->lock); |
| 407 | return -ENOMEM; |
| 408 | } |
| 409 | |
| 410 | if (copy_from_user(buf, buffer, count)) { |
Jiri Slaby | a08b43a | 2009-03-11 21:47:39 +0100 | [diff] [blame] | 411 | mutex_unlock(&vstdev->lock); |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 412 | dev_err(&dev->dev, "%s: can't copy_from_user\n", __func__); |
| 413 | retval = -EFAULT; |
| 414 | goto exit; |
| 415 | } |
| 416 | |
| 417 | usb_anchor_urb(urb, &vstdev->submitted); |
| 418 | retval = vstusb_fill_and_send_urb(urb, dev, pipe, buf, count, &done); |
| 419 | mutex_unlock(&vstdev->lock); |
| 420 | if (retval) { |
| 421 | usb_unanchor_urb(urb); |
| 422 | dev_err(&dev->dev, "%s: error %d filling and sending urb %d\n", |
| 423 | __func__, retval, pipe); |
| 424 | goto exit; |
| 425 | } |
| 426 | |
| 427 | retval = vstusb_complete_urb(urb, &done, timeout, &cnt); |
| 428 | if (retval) { |
| 429 | dev_err(&dev->dev, "%s: error %d completing urb %d\n", |
| 430 | __func__, retval, pipe); |
| 431 | goto exit; |
| 432 | } else { |
| 433 | retval = cnt; |
| 434 | dev_dbg(&dev->dev, "%s: sent %d bytes to pipe %d\n", |
| 435 | __func__, cnt, pipe); |
| 436 | } |
| 437 | |
| 438 | exit: |
| 439 | usb_free_urb(urb); |
| 440 | kfree(buf); |
| 441 | return retval; |
| 442 | } |
| 443 | |
| 444 | static long vstusb_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 445 | { |
| 446 | int retval = 0; |
| 447 | int cnt = -1; |
| 448 | void __user *data = (void __user *)arg; |
| 449 | struct vstusb_args usb_data; |
| 450 | |
| 451 | struct vstusb_device *vstdev; |
| 452 | void *buffer = NULL; /* must be initialized. buffer is |
| 453 | * referenced on exit but not all |
| 454 | * ioctls allocate it */ |
| 455 | |
| 456 | struct urb *urb = NULL; /* must be initialized. urb is |
| 457 | * referenced on exit but not all |
| 458 | * ioctls allocate it */ |
| 459 | struct usb_device *dev; |
| 460 | unsigned int pipe; |
| 461 | int timeout; |
| 462 | |
| 463 | DECLARE_COMPLETION_ONSTACK(done); |
| 464 | |
| 465 | vstdev = file->private_data; |
| 466 | |
| 467 | if (_IOC_TYPE(cmd) != VST_IOC_MAGIC) { |
| 468 | dev_warn(&vstdev->usb_dev->dev, |
| 469 | "%s: ioctl command %x, bad ioctl magic %x, " |
| 470 | "expected %x\n", __func__, cmd, |
| 471 | _IOC_TYPE(cmd), VST_IOC_MAGIC); |
| 472 | return -EINVAL; |
| 473 | } |
| 474 | |
| 475 | if (vstdev == NULL) |
| 476 | return -ENODEV; |
| 477 | |
| 478 | if (copy_from_user(&usb_data, data, sizeof(struct vstusb_args))) { |
| 479 | dev_err(&vstdev->usb_dev->dev, "%s: can't copy_from_user\n", |
| 480 | __func__); |
| 481 | return -EFAULT; |
| 482 | } |
| 483 | |
| 484 | /* lock this object */ |
| 485 | if (mutex_lock_interruptible(&vstdev->lock)) { |
| 486 | retval = -ERESTARTSYS; |
| 487 | goto exit; |
| 488 | } |
| 489 | |
| 490 | /* anyone home */ |
| 491 | if (!vstdev->present) { |
| 492 | mutex_unlock(&vstdev->lock); |
| 493 | dev_err(&vstdev->usb_dev->dev, "%s: device not present\n", |
| 494 | __func__); |
| 495 | retval = -ENODEV; |
| 496 | goto exit; |
| 497 | } |
| 498 | |
| 499 | /* pull out the necessary data */ |
| 500 | dev = vstdev->usb_dev; |
| 501 | |
| 502 | switch (cmd) { |
| 503 | |
| 504 | case IOCTL_VSTUSB_CONFIG_RW: |
| 505 | |
| 506 | vstdev->rd_pipe = usb_data.rd_pipe; |
| 507 | vstdev->rd_timeout_ms = usb_data.rd_timeout_ms; |
| 508 | vstdev->wr_pipe = usb_data.wr_pipe; |
| 509 | vstdev->wr_timeout_ms = usb_data.wr_timeout_ms; |
| 510 | |
| 511 | mutex_unlock(&vstdev->lock); |
| 512 | |
| 513 | dev_dbg(&dev->dev, "%s: setting pipes/timeouts, " |
| 514 | "rdpipe = %d, rdtimeout = %d, " |
| 515 | "wrpipe = %d, wrtimeout = %d\n", __func__, |
| 516 | vstdev->rd_pipe, vstdev->rd_timeout_ms, |
| 517 | vstdev->wr_pipe, vstdev->wr_timeout_ms); |
| 518 | break; |
| 519 | |
| 520 | case IOCTL_VSTUSB_SEND_PIPE: |
| 521 | |
Stephen Ware | 84dcd59 | 2008-10-08 10:53:56 -0700 | [diff] [blame] | 522 | if ((usb_data.count == 0) || (usb_data.count > VST_MAXBUFFER)) { |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 523 | mutex_unlock(&vstdev->lock); |
| 524 | retval = -EINVAL; |
| 525 | goto exit; |
| 526 | } |
| 527 | |
| 528 | buffer = kmalloc(usb_data.count, GFP_KERNEL); |
| 529 | if (buffer == NULL) { |
| 530 | mutex_unlock(&vstdev->lock); |
| 531 | retval = -ENOMEM; |
| 532 | goto exit; |
| 533 | } |
| 534 | |
| 535 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 536 | if (!urb) { |
| 537 | mutex_unlock(&vstdev->lock); |
| 538 | retval = -ENOMEM; |
| 539 | goto exit; |
| 540 | } |
| 541 | |
| 542 | timeout = usb_data.timeout_ms; |
| 543 | |
| 544 | pipe = usb_sndbulkpipe(dev, usb_data.pipe); |
| 545 | |
| 546 | if (copy_from_user(buffer, usb_data.buffer, usb_data.count)) { |
| 547 | dev_err(&dev->dev, "%s: can't copy_from_user\n", |
| 548 | __func__); |
| 549 | mutex_unlock(&vstdev->lock); |
| 550 | retval = -EFAULT; |
| 551 | goto exit; |
| 552 | } |
| 553 | |
| 554 | usb_anchor_urb(urb, &vstdev->submitted); |
| 555 | retval = vstusb_fill_and_send_urb(urb, dev, pipe, buffer, |
| 556 | usb_data.count, &done); |
| 557 | mutex_unlock(&vstdev->lock); |
| 558 | if (retval) { |
| 559 | usb_unanchor_urb(urb); |
| 560 | dev_err(&dev->dev, |
| 561 | "%s: error %d filling and sending urb %d\n", |
| 562 | __func__, retval, pipe); |
| 563 | goto exit; |
| 564 | } |
| 565 | |
| 566 | retval = vstusb_complete_urb(urb, &done, timeout, &cnt); |
| 567 | if (retval) { |
| 568 | dev_err(&dev->dev, "%s: error %d completing urb %d\n", |
| 569 | __func__, retval, pipe); |
| 570 | } |
| 571 | |
| 572 | break; |
| 573 | case IOCTL_VSTUSB_RECV_PIPE: |
| 574 | |
Stephen Ware | 84dcd59 | 2008-10-08 10:53:56 -0700 | [diff] [blame] | 575 | if ((usb_data.count == 0) || (usb_data.count > VST_MAXBUFFER)) { |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 576 | mutex_unlock(&vstdev->lock); |
| 577 | retval = -EINVAL; |
| 578 | goto exit; |
| 579 | } |
| 580 | |
| 581 | buffer = kmalloc(usb_data.count, GFP_KERNEL); |
| 582 | if (buffer == NULL) { |
| 583 | mutex_unlock(&vstdev->lock); |
| 584 | retval = -ENOMEM; |
| 585 | goto exit; |
| 586 | } |
| 587 | |
| 588 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 589 | if (!urb) { |
| 590 | mutex_unlock(&vstdev->lock); |
| 591 | retval = -ENOMEM; |
| 592 | goto exit; |
| 593 | } |
| 594 | |
| 595 | timeout = usb_data.timeout_ms; |
| 596 | |
| 597 | pipe = usb_rcvbulkpipe(dev, usb_data.pipe); |
| 598 | |
| 599 | usb_anchor_urb(urb, &vstdev->submitted); |
| 600 | retval = vstusb_fill_and_send_urb(urb, dev, pipe, buffer, |
| 601 | usb_data.count, &done); |
| 602 | mutex_unlock(&vstdev->lock); |
| 603 | if (retval) { |
| 604 | usb_unanchor_urb(urb); |
| 605 | dev_err(&dev->dev, |
| 606 | "%s: error %d filling and sending urb %d\n", |
| 607 | __func__, retval, pipe); |
| 608 | goto exit; |
| 609 | } |
| 610 | |
| 611 | retval = vstusb_complete_urb(urb, &done, timeout, &cnt); |
| 612 | if (retval) { |
| 613 | dev_err(&dev->dev, "%s: error %d completing urb %d\n", |
| 614 | __func__, retval, pipe); |
| 615 | goto exit; |
| 616 | } |
| 617 | |
| 618 | if (copy_to_user(usb_data.buffer, buffer, cnt)) { |
| 619 | dev_err(&dev->dev, "%s: can't copy_to_user\n", |
| 620 | __func__); |
| 621 | retval = -EFAULT; |
| 622 | goto exit; |
| 623 | } |
| 624 | |
| 625 | usb_data.count = cnt; |
| 626 | if (copy_to_user(data, &usb_data, sizeof(struct vstusb_args))) { |
| 627 | dev_err(&dev->dev, "%s: can't copy_to_user\n", |
| 628 | __func__); |
| 629 | retval = -EFAULT; |
| 630 | } else { |
Greg Kroah-Hartman | 6c5ab37 | 2008-10-31 10:09:57 -0700 | [diff] [blame] | 631 | dev_dbg(&dev->dev, "%s: recv %zd bytes from pipe %d\n", |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 632 | __func__, usb_data.count, usb_data.pipe); |
| 633 | } |
| 634 | |
| 635 | break; |
| 636 | |
| 637 | default: |
| 638 | mutex_unlock(&vstdev->lock); |
| 639 | dev_warn(&dev->dev, "ioctl_vstusb: invalid ioctl cmd %x\n", |
| 640 | cmd); |
| 641 | return -EINVAL; |
| 642 | break; |
| 643 | } |
| 644 | exit: |
| 645 | usb_free_urb(urb); |
| 646 | kfree(buffer); |
| 647 | return retval; |
| 648 | } |
| 649 | |
| 650 | static const struct file_operations vstusb_fops = { |
| 651 | .owner = THIS_MODULE, |
| 652 | .read = vstusb_read, |
| 653 | .write = vstusb_write, |
| 654 | .unlocked_ioctl = vstusb_ioctl, |
| 655 | .compat_ioctl = vstusb_ioctl, |
| 656 | .open = vstusb_open, |
Stephen Ware | 84dcd59 | 2008-10-08 10:53:56 -0700 | [diff] [blame] | 657 | .release = vstusb_release, |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 658 | }; |
| 659 | |
| 660 | static struct usb_class_driver usb_vstusb_class = { |
| 661 | .name = "usb/vstusb%d", |
| 662 | .fops = &vstusb_fops, |
| 663 | .minor_base = VSTUSB_MINOR_BASE, |
| 664 | }; |
| 665 | |
| 666 | static int vstusb_probe(struct usb_interface *intf, |
| 667 | const struct usb_device_id *id) |
| 668 | { |
| 669 | struct usb_device *dev = interface_to_usbdev(intf); |
| 670 | struct vstusb_device *vstdev; |
| 671 | int i; |
| 672 | int retval = 0; |
| 673 | |
| 674 | /* allocate memory for our device state and intialize it */ |
| 675 | |
| 676 | vstdev = kzalloc(sizeof(*vstdev), GFP_KERNEL); |
| 677 | if (vstdev == NULL) |
| 678 | return -ENOMEM; |
| 679 | |
Stephen Ware | 84dcd59 | 2008-10-08 10:53:56 -0700 | [diff] [blame] | 680 | /* must do usb_get_dev() prior to kref_init() since the kref_put() |
| 681 | * release function will do a usb_put_dev() */ |
| 682 | usb_get_dev(dev); |
| 683 | kref_init(&vstdev->kref); |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 684 | mutex_init(&vstdev->lock); |
| 685 | |
| 686 | i = dev->descriptor.bcdDevice; |
| 687 | |
| 688 | dev_dbg(&intf->dev, "Version %1d%1d.%1d%1d found at address %d\n", |
| 689 | (i & 0xF000) >> 12, (i & 0xF00) >> 8, |
| 690 | (i & 0xF0) >> 4, (i & 0xF), dev->devnum); |
| 691 | |
| 692 | vstdev->present = 1; |
| 693 | vstdev->isopen = 0; |
| 694 | vstdev->usb_dev = dev; |
| 695 | init_usb_anchor(&vstdev->submitted); |
| 696 | |
| 697 | usb_set_intfdata(intf, vstdev); |
| 698 | retval = usb_register_dev(intf, &usb_vstusb_class); |
| 699 | if (retval) { |
| 700 | dev_err(&intf->dev, |
| 701 | "%s: Not able to get a minor for this device.\n", |
| 702 | __func__); |
| 703 | usb_set_intfdata(intf, NULL); |
Stephen Ware | 84dcd59 | 2008-10-08 10:53:56 -0700 | [diff] [blame] | 704 | kref_put(&vstdev->kref, vstusb_delete); |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 705 | return retval; |
| 706 | } |
| 707 | |
| 708 | /* let the user know what node this device is now attached to */ |
| 709 | dev_info(&intf->dev, |
| 710 | "VST USB Device #%d now attached to major %d minor %d\n", |
| 711 | (intf->minor - VSTUSB_MINOR_BASE), USB_MAJOR, intf->minor); |
| 712 | |
| 713 | dev_info(&intf->dev, "%s, %s\n", DRIVER_DESC, DRIVER_VERSION); |
| 714 | |
| 715 | return retval; |
| 716 | } |
| 717 | |
| 718 | static void vstusb_disconnect(struct usb_interface *intf) |
| 719 | { |
| 720 | struct vstusb_device *vstdev = usb_get_intfdata(intf); |
| 721 | |
| 722 | usb_deregister_dev(intf, &usb_vstusb_class); |
| 723 | usb_set_intfdata(intf, NULL); |
| 724 | |
| 725 | if (vstdev) { |
| 726 | |
| 727 | mutex_lock(&vstdev->lock); |
| 728 | vstdev->present = 0; |
| 729 | |
| 730 | usb_kill_anchored_urbs(&vstdev->submitted); |
| 731 | |
Stephen Ware | 84dcd59 | 2008-10-08 10:53:56 -0700 | [diff] [blame] | 732 | mutex_unlock(&vstdev->lock); |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 733 | |
Stephen Ware | 84dcd59 | 2008-10-08 10:53:56 -0700 | [diff] [blame] | 734 | kref_put(&vstdev->kref, vstusb_delete); |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 735 | } |
Stephen Ware | 84dcd59 | 2008-10-08 10:53:56 -0700 | [diff] [blame] | 736 | |
Stephen Ware | cbc3011 | 2008-09-30 11:39:38 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | static int vstusb_suspend(struct usb_interface *intf, pm_message_t message) |
| 740 | { |
| 741 | struct vstusb_device *vstdev = usb_get_intfdata(intf); |
| 742 | int time; |
| 743 | if (!vstdev) |
| 744 | return 0; |
| 745 | |
| 746 | mutex_lock(&vstdev->lock); |
| 747 | time = usb_wait_anchor_empty_timeout(&vstdev->submitted, 1000); |
| 748 | if (!time) |
| 749 | usb_kill_anchored_urbs(&vstdev->submitted); |
| 750 | mutex_unlock(&vstdev->lock); |
| 751 | |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | static int vstusb_resume(struct usb_interface *intf) |
| 756 | { |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | static struct usb_driver vstusb_driver = { |
| 761 | .name = "vstusb", |
| 762 | .probe = vstusb_probe, |
| 763 | .disconnect = vstusb_disconnect, |
| 764 | .suspend = vstusb_suspend, |
| 765 | .resume = vstusb_resume, |
| 766 | .id_table = id_table, |
| 767 | }; |
| 768 | |
| 769 | static int __init vstusb_init(void) |
| 770 | { |
| 771 | int rc; |
| 772 | |
| 773 | rc = usb_register(&vstusb_driver); |
| 774 | if (rc) |
| 775 | printk(KERN_ERR "%s: failed to register (%d)", __func__, rc); |
| 776 | |
| 777 | return rc; |
| 778 | } |
| 779 | |
| 780 | static void __exit vstusb_exit(void) |
| 781 | { |
| 782 | usb_deregister(&vstusb_driver); |
| 783 | } |
| 784 | |
| 785 | module_init(vstusb_init); |
| 786 | module_exit(vstusb_exit); |
| 787 | |
| 788 | MODULE_AUTHOR("Dennis O'Brien/Stephen Ware"); |
| 789 | MODULE_DESCRIPTION(DRIVER_VERSION); |
| 790 | MODULE_LICENSE("GPL"); |