vstusb.c 18 KB

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