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