ldusb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /**
  2. * Generic USB driver for report based interrupt in/out devices
  3. * like LD Didactic's USB devices. LD Didactic's USB devices are
  4. * HID devices which do not use HID report definitons (they use
  5. * raw interrupt in and our reports only for communication).
  6. *
  7. * This driver uses a ring buffer for time critical reading of
  8. * interrupt in reports and provides read and write methods for
  9. * raw interrupt reports (similar to the Windows HID driver).
  10. * Devices based on the book USB COMPLETE by Jan Axelson may need
  11. * such a compatibility to the Windows HID driver.
  12. *
  13. * Copyright (C) 2005 Michael Hund <mhund@ld-didactic.de>
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2 of
  18. * the License, or (at your option) any later version.
  19. *
  20. * Derived from Lego USB Tower driver
  21. * Copyright (C) 2003 David Glance <advidgsf@sourceforge.net>
  22. * 2001-2004 Juergen Stuber <starblue@users.sourceforge.net>
  23. *
  24. * V0.1 (mh) Initial version
  25. * V0.11 (mh) Added raw support for HID 1.0 devices (no interrupt out endpoint)
  26. * V0.12 (mh) Added kmalloc check for string buffer
  27. * V0.13 (mh) Added support for LD X-Ray and Machine Test System
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/errno.h>
  31. #include <linux/init.h>
  32. #include <linux/slab.h>
  33. #include <linux/module.h>
  34. #include <linux/mutex.h>
  35. #include <asm/uaccess.h>
  36. #include <linux/input.h>
  37. #include <linux/usb.h>
  38. #include <linux/poll.h>
  39. /* Define these values to match your devices */
  40. #define USB_VENDOR_ID_LD 0x0f11 /* USB Vendor ID of LD Didactic GmbH */
  41. #define USB_DEVICE_ID_LD_CASSY 0x1000 /* USB Product ID of CASSY-S */
  42. #define USB_DEVICE_ID_LD_POCKETCASSY 0x1010 /* USB Product ID of Pocket-CASSY */
  43. #define USB_DEVICE_ID_LD_MOBILECASSY 0x1020 /* USB Product ID of Mobile-CASSY */
  44. #define USB_DEVICE_ID_LD_JWM 0x1080 /* USB Product ID of Joule and Wattmeter */
  45. #define USB_DEVICE_ID_LD_DMMP 0x1081 /* USB Product ID of Digital Multimeter P (reserved) */
  46. #define USB_DEVICE_ID_LD_UMIP 0x1090 /* USB Product ID of UMI P */
  47. #define USB_DEVICE_ID_LD_XRAY1 0x1100 /* USB Product ID of X-Ray Apparatus */
  48. #define USB_DEVICE_ID_LD_XRAY2 0x1101 /* USB Product ID of X-Ray Apparatus */
  49. #define USB_DEVICE_ID_LD_VIDEOCOM 0x1200 /* USB Product ID of VideoCom */
  50. #define USB_DEVICE_ID_LD_COM3LAB 0x2000 /* USB Product ID of COM3LAB */
  51. #define USB_DEVICE_ID_LD_TELEPORT 0x2010 /* USB Product ID of Terminal Adapter */
  52. #define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020 /* USB Product ID of Network Analyser */
  53. #define USB_DEVICE_ID_LD_POWERCONTROL 0x2030 /* USB Product ID of Converter Control Unit */
  54. #define USB_DEVICE_ID_LD_MACHINETEST 0x2040 /* USB Product ID of Machine Test System */
  55. #define USB_VENDOR_ID_VERNIER 0x08f7
  56. #define USB_DEVICE_ID_VERNIER_LABPRO 0x0001
  57. #define USB_DEVICE_ID_VERNIER_GOTEMP 0x0002
  58. #define USB_DEVICE_ID_VERNIER_SKIP 0x0003
  59. #define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004
  60. #define USB_DEVICE_ID_VERNIER_LCSPEC 0x0006
  61. #define USB_VENDOR_ID_MICROCHIP 0x04d8
  62. #define USB_DEVICE_ID_PICDEM 0x000c
  63. #ifdef CONFIG_USB_DYNAMIC_MINORS
  64. #define USB_LD_MINOR_BASE 0
  65. #else
  66. #define USB_LD_MINOR_BASE 176
  67. #endif
  68. /* table of devices that work with this driver */
  69. static struct usb_device_id ld_usb_table [] = {
  70. { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
  71. { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
  72. { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
  73. { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
  74. { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
  75. { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
  76. { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) },
  77. { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
  78. { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
  79. { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
  80. { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
  81. { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
  82. { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
  83. { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
  84. { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) },
  85. { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) },
  86. { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) },
  87. { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) },
  88. { USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICDEM) },
  89. { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) },
  90. { } /* Terminating entry */
  91. };
  92. MODULE_DEVICE_TABLE(usb, ld_usb_table);
  93. MODULE_VERSION("V0.13");
  94. MODULE_AUTHOR("Michael Hund <mhund@ld-didactic.de>");
  95. MODULE_DESCRIPTION("LD USB Driver");
  96. MODULE_LICENSE("GPL");
  97. MODULE_SUPPORTED_DEVICE("LD USB Devices");
  98. #ifdef CONFIG_USB_DEBUG
  99. static int debug = 1;
  100. #else
  101. static int debug = 0;
  102. #endif
  103. /* Use our own dbg macro */
  104. #define dbg_info(dev, format, arg...) do { if (debug) dev_info(dev , format , ## arg); } while (0)
  105. /* Module parameters */
  106. module_param(debug, int, S_IRUGO | S_IWUSR);
  107. MODULE_PARM_DESC(debug, "Debug enabled or not");
  108. /* All interrupt in transfers are collected in a ring buffer to
  109. * avoid racing conditions and get better performance of the driver.
  110. */
  111. static int ring_buffer_size = 128;
  112. module_param(ring_buffer_size, int, 0);
  113. MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports");
  114. /* The write_buffer can contain more than one interrupt out transfer.
  115. */
  116. static int write_buffer_size = 10;
  117. module_param(write_buffer_size, int, 0);
  118. MODULE_PARM_DESC(write_buffer_size, "Write buffer size in reports");
  119. /* As of kernel version 2.6.4 ehci-hcd uses an
  120. * "only one interrupt transfer per frame" shortcut
  121. * to simplify the scheduling of periodic transfers.
  122. * This conflicts with our standard 1ms intervals for in and out URBs.
  123. * We use default intervals of 2ms for in and 2ms for out transfers,
  124. * which should be fast enough.
  125. * Increase the interval to allow more devices that do interrupt transfers,
  126. * or set to 1 to use the standard interval from the endpoint descriptors.
  127. */
  128. static int min_interrupt_in_interval = 2;
  129. module_param(min_interrupt_in_interval, int, 0);
  130. MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms");
  131. static int min_interrupt_out_interval = 2;
  132. module_param(min_interrupt_out_interval, int, 0);
  133. MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms");
  134. /* Structure to hold all of our device specific stuff */
  135. struct ld_usb {
  136. struct mutex mutex; /* locks this structure */
  137. struct usb_interface* intf; /* save off the usb interface pointer */
  138. int open_count; /* number of times this port has been opened */
  139. char* ring_buffer;
  140. unsigned int ring_head;
  141. unsigned int ring_tail;
  142. wait_queue_head_t read_wait;
  143. wait_queue_head_t write_wait;
  144. char* interrupt_in_buffer;
  145. struct usb_endpoint_descriptor* interrupt_in_endpoint;
  146. struct urb* interrupt_in_urb;
  147. int interrupt_in_interval;
  148. size_t interrupt_in_endpoint_size;
  149. int interrupt_in_running;
  150. int interrupt_in_done;
  151. int buffer_overflow;
  152. spinlock_t rbsl;
  153. char* interrupt_out_buffer;
  154. struct usb_endpoint_descriptor* interrupt_out_endpoint;
  155. struct urb* interrupt_out_urb;
  156. int interrupt_out_interval;
  157. size_t interrupt_out_endpoint_size;
  158. int interrupt_out_busy;
  159. };
  160. static struct usb_driver ld_usb_driver;
  161. /**
  162. * ld_usb_abort_transfers
  163. * aborts transfers and frees associated data structures
  164. */
  165. static void ld_usb_abort_transfers(struct ld_usb *dev)
  166. {
  167. /* shutdown transfer */
  168. if (dev->interrupt_in_running) {
  169. dev->interrupt_in_running = 0;
  170. if (dev->intf)
  171. usb_kill_urb(dev->interrupt_in_urb);
  172. }
  173. if (dev->interrupt_out_busy)
  174. if (dev->intf)
  175. usb_kill_urb(dev->interrupt_out_urb);
  176. }
  177. /**
  178. * ld_usb_delete
  179. */
  180. static void ld_usb_delete(struct ld_usb *dev)
  181. {
  182. ld_usb_abort_transfers(dev);
  183. /* free data structures */
  184. usb_free_urb(dev->interrupt_in_urb);
  185. usb_free_urb(dev->interrupt_out_urb);
  186. kfree(dev->ring_buffer);
  187. kfree(dev->interrupt_in_buffer);
  188. kfree(dev->interrupt_out_buffer);
  189. kfree(dev);
  190. }
  191. /**
  192. * ld_usb_interrupt_in_callback
  193. */
  194. static void ld_usb_interrupt_in_callback(struct urb *urb)
  195. {
  196. struct ld_usb *dev = urb->context;
  197. size_t *actual_buffer;
  198. unsigned int next_ring_head;
  199. int status = urb->status;
  200. int retval;
  201. if (status) {
  202. if (status == -ENOENT ||
  203. status == -ECONNRESET ||
  204. status == -ESHUTDOWN) {
  205. goto exit;
  206. } else {
  207. dbg_info(&dev->intf->dev, "%s: nonzero status received: %d\n",
  208. __func__, status);
  209. spin_lock(&dev->rbsl);
  210. goto resubmit; /* maybe we can recover */
  211. }
  212. }
  213. spin_lock(&dev->rbsl);
  214. if (urb->actual_length > 0) {
  215. next_ring_head = (dev->ring_head+1) % ring_buffer_size;
  216. if (next_ring_head != dev->ring_tail) {
  217. actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_head*(sizeof(size_t)+dev->interrupt_in_endpoint_size));
  218. /* actual_buffer gets urb->actual_length + interrupt_in_buffer */
  219. *actual_buffer = urb->actual_length;
  220. memcpy(actual_buffer+1, dev->interrupt_in_buffer, urb->actual_length);
  221. dev->ring_head = next_ring_head;
  222. dbg_info(&dev->intf->dev, "%s: received %d bytes\n",
  223. __func__, urb->actual_length);
  224. } else {
  225. dev_warn(&dev->intf->dev,
  226. "Ring buffer overflow, %d bytes dropped\n",
  227. urb->actual_length);
  228. dev->buffer_overflow = 1;
  229. }
  230. }
  231. resubmit:
  232. /* resubmit if we're still running */
  233. if (dev->interrupt_in_running && !dev->buffer_overflow && dev->intf) {
  234. retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
  235. if (retval) {
  236. dev_err(&dev->intf->dev,
  237. "usb_submit_urb failed (%d)\n", retval);
  238. dev->buffer_overflow = 1;
  239. }
  240. }
  241. spin_unlock(&dev->rbsl);
  242. exit:
  243. dev->interrupt_in_done = 1;
  244. wake_up_interruptible(&dev->read_wait);
  245. }
  246. /**
  247. * ld_usb_interrupt_out_callback
  248. */
  249. static void ld_usb_interrupt_out_callback(struct urb *urb)
  250. {
  251. struct ld_usb *dev = urb->context;
  252. int status = urb->status;
  253. /* sync/async unlink faults aren't errors */
  254. if (status && !(status == -ENOENT ||
  255. status == -ECONNRESET ||
  256. status == -ESHUTDOWN))
  257. dbg_info(&dev->intf->dev,
  258. "%s - nonzero write interrupt status received: %d\n",
  259. __func__, status);
  260. dev->interrupt_out_busy = 0;
  261. wake_up_interruptible(&dev->write_wait);
  262. }
  263. /**
  264. * ld_usb_open
  265. */
  266. static int ld_usb_open(struct inode *inode, struct file *file)
  267. {
  268. struct ld_usb *dev;
  269. int subminor;
  270. int retval;
  271. struct usb_interface *interface;
  272. nonseekable_open(inode, file);
  273. subminor = iminor(inode);
  274. interface = usb_find_interface(&ld_usb_driver, subminor);
  275. if (!interface) {
  276. err("%s - error, can't find device for minor %d\n",
  277. __func__, subminor);
  278. return -ENODEV;
  279. }
  280. dev = usb_get_intfdata(interface);
  281. if (!dev)
  282. return -ENODEV;
  283. /* lock this device */
  284. if (mutex_lock_interruptible(&dev->mutex))
  285. return -ERESTARTSYS;
  286. /* allow opening only once */
  287. if (dev->open_count) {
  288. retval = -EBUSY;
  289. goto unlock_exit;
  290. }
  291. dev->open_count = 1;
  292. /* initialize in direction */
  293. dev->ring_head = 0;
  294. dev->ring_tail = 0;
  295. dev->buffer_overflow = 0;
  296. usb_fill_int_urb(dev->interrupt_in_urb,
  297. interface_to_usbdev(interface),
  298. usb_rcvintpipe(interface_to_usbdev(interface),
  299. dev->interrupt_in_endpoint->bEndpointAddress),
  300. dev->interrupt_in_buffer,
  301. dev->interrupt_in_endpoint_size,
  302. ld_usb_interrupt_in_callback,
  303. dev,
  304. dev->interrupt_in_interval);
  305. dev->interrupt_in_running = 1;
  306. dev->interrupt_in_done = 0;
  307. retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
  308. if (retval) {
  309. dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval);
  310. dev->interrupt_in_running = 0;
  311. dev->open_count = 0;
  312. goto unlock_exit;
  313. }
  314. /* save device in the file's private structure */
  315. file->private_data = dev;
  316. unlock_exit:
  317. mutex_unlock(&dev->mutex);
  318. return retval;
  319. }
  320. /**
  321. * ld_usb_release
  322. */
  323. static int ld_usb_release(struct inode *inode, struct file *file)
  324. {
  325. struct ld_usb *dev;
  326. int retval = 0;
  327. dev = file->private_data;
  328. if (dev == NULL) {
  329. retval = -ENODEV;
  330. goto exit;
  331. }
  332. if (mutex_lock_interruptible(&dev->mutex)) {
  333. retval = -ERESTARTSYS;
  334. goto exit;
  335. }
  336. if (dev->open_count != 1) {
  337. retval = -ENODEV;
  338. goto unlock_exit;
  339. }
  340. if (dev->intf == NULL) {
  341. /* the device was unplugged before the file was released */
  342. mutex_unlock(&dev->mutex);
  343. /* unlock here as ld_usb_delete frees dev */
  344. ld_usb_delete(dev);
  345. goto exit;
  346. }
  347. /* wait until write transfer is finished */
  348. if (dev->interrupt_out_busy)
  349. wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
  350. ld_usb_abort_transfers(dev);
  351. dev->open_count = 0;
  352. unlock_exit:
  353. mutex_unlock(&dev->mutex);
  354. exit:
  355. return retval;
  356. }
  357. /**
  358. * ld_usb_poll
  359. */
  360. static unsigned int ld_usb_poll(struct file *file, poll_table *wait)
  361. {
  362. struct ld_usb *dev;
  363. unsigned int mask = 0;
  364. dev = file->private_data;
  365. poll_wait(file, &dev->read_wait, wait);
  366. poll_wait(file, &dev->write_wait, wait);
  367. if (dev->ring_head != dev->ring_tail)
  368. mask |= POLLIN | POLLRDNORM;
  369. if (!dev->interrupt_out_busy)
  370. mask |= POLLOUT | POLLWRNORM;
  371. return mask;
  372. }
  373. /**
  374. * ld_usb_read
  375. */
  376. static ssize_t ld_usb_read(struct file *file, char __user *buffer, size_t count,
  377. loff_t *ppos)
  378. {
  379. struct ld_usb *dev;
  380. size_t *actual_buffer;
  381. size_t bytes_to_read;
  382. int retval = 0;
  383. int rv;
  384. dev = file->private_data;
  385. /* verify that we actually have some data to read */
  386. if (count == 0)
  387. goto exit;
  388. /* lock this object */
  389. if (mutex_lock_interruptible(&dev->mutex)) {
  390. retval = -ERESTARTSYS;
  391. goto exit;
  392. }
  393. /* verify that the device wasn't unplugged */
  394. if (dev->intf == NULL) {
  395. retval = -ENODEV;
  396. err("No device or device unplugged %d\n", retval);
  397. goto unlock_exit;
  398. }
  399. /* wait for data */
  400. spin_lock_irq(&dev->rbsl);
  401. if (dev->ring_head == dev->ring_tail) {
  402. dev->interrupt_in_done = 0;
  403. spin_unlock_irq(&dev->rbsl);
  404. if (file->f_flags & O_NONBLOCK) {
  405. retval = -EAGAIN;
  406. goto unlock_exit;
  407. }
  408. retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done);
  409. if (retval < 0)
  410. goto unlock_exit;
  411. } else {
  412. spin_unlock_irq(&dev->rbsl);
  413. }
  414. /* actual_buffer contains actual_length + interrupt_in_buffer */
  415. actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_tail*(sizeof(size_t)+dev->interrupt_in_endpoint_size));
  416. bytes_to_read = min(count, *actual_buffer);
  417. if (bytes_to_read < *actual_buffer)
  418. dev_warn(&dev->intf->dev, "Read buffer overflow, %zd bytes dropped\n",
  419. *actual_buffer-bytes_to_read);
  420. /* copy one interrupt_in_buffer from ring_buffer into userspace */
  421. if (copy_to_user(buffer, actual_buffer+1, bytes_to_read)) {
  422. retval = -EFAULT;
  423. goto unlock_exit;
  424. }
  425. dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size;
  426. retval = bytes_to_read;
  427. spin_lock_irq(&dev->rbsl);
  428. if (dev->buffer_overflow) {
  429. dev->buffer_overflow = 0;
  430. spin_unlock_irq(&dev->rbsl);
  431. rv = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
  432. if (rv < 0)
  433. dev->buffer_overflow = 1;
  434. } else {
  435. spin_unlock_irq(&dev->rbsl);
  436. }
  437. unlock_exit:
  438. /* unlock the device */
  439. mutex_unlock(&dev->mutex);
  440. exit:
  441. return retval;
  442. }
  443. /**
  444. * ld_usb_write
  445. */
  446. static ssize_t ld_usb_write(struct file *file, const char __user *buffer,
  447. size_t count, loff_t *ppos)
  448. {
  449. struct ld_usb *dev;
  450. size_t bytes_to_write;
  451. int retval = 0;
  452. dev = file->private_data;
  453. /* verify that we actually have some data to write */
  454. if (count == 0)
  455. goto exit;
  456. /* lock this object */
  457. if (mutex_lock_interruptible(&dev->mutex)) {
  458. retval = -ERESTARTSYS;
  459. goto exit;
  460. }
  461. /* verify that the device wasn't unplugged */
  462. if (dev->intf == NULL) {
  463. retval = -ENODEV;
  464. err("No device or device unplugged %d\n", retval);
  465. goto unlock_exit;
  466. }
  467. /* wait until previous transfer is finished */
  468. if (dev->interrupt_out_busy) {
  469. if (file->f_flags & O_NONBLOCK) {
  470. retval = -EAGAIN;
  471. goto unlock_exit;
  472. }
  473. retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
  474. if (retval < 0) {
  475. goto unlock_exit;
  476. }
  477. }
  478. /* write the data into interrupt_out_buffer from userspace */
  479. bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size);
  480. if (bytes_to_write < count)
  481. dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write);
  482. dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", __func__, count, bytes_to_write);
  483. if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
  484. retval = -EFAULT;
  485. goto unlock_exit;
  486. }
  487. if (dev->interrupt_out_endpoint == NULL) {
  488. /* try HID_REQ_SET_REPORT=9 on control_endpoint instead of interrupt_out_endpoint */
  489. retval = usb_control_msg(interface_to_usbdev(dev->intf),
  490. usb_sndctrlpipe(interface_to_usbdev(dev->intf), 0),
  491. 9,
  492. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  493. 1 << 8, 0,
  494. dev->interrupt_out_buffer,
  495. bytes_to_write,
  496. USB_CTRL_SET_TIMEOUT * HZ);
  497. if (retval < 0)
  498. err("Couldn't submit HID_REQ_SET_REPORT %d\n", retval);
  499. goto unlock_exit;
  500. }
  501. /* send off the urb */
  502. usb_fill_int_urb(dev->interrupt_out_urb,
  503. interface_to_usbdev(dev->intf),
  504. usb_sndintpipe(interface_to_usbdev(dev->intf),
  505. dev->interrupt_out_endpoint->bEndpointAddress),
  506. dev->interrupt_out_buffer,
  507. bytes_to_write,
  508. ld_usb_interrupt_out_callback,
  509. dev,
  510. dev->interrupt_out_interval);
  511. dev->interrupt_out_busy = 1;
  512. wmb();
  513. retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
  514. if (retval) {
  515. dev->interrupt_out_busy = 0;
  516. err("Couldn't submit interrupt_out_urb %d\n", retval);
  517. goto unlock_exit;
  518. }
  519. retval = bytes_to_write;
  520. unlock_exit:
  521. /* unlock the device */
  522. mutex_unlock(&dev->mutex);
  523. exit:
  524. return retval;
  525. }
  526. /* file operations needed when we register this driver */
  527. static const struct file_operations ld_usb_fops = {
  528. .owner = THIS_MODULE,
  529. .read = ld_usb_read,
  530. .write = ld_usb_write,
  531. .open = ld_usb_open,
  532. .release = ld_usb_release,
  533. .poll = ld_usb_poll,
  534. };
  535. /*
  536. * usb class driver info in order to get a minor number from the usb core,
  537. * and to have the device registered with the driver core
  538. */
  539. static struct usb_class_driver ld_usb_class = {
  540. .name = "ldusb%d",
  541. .fops = &ld_usb_fops,
  542. .minor_base = USB_LD_MINOR_BASE,
  543. };
  544. /**
  545. * ld_usb_probe
  546. *
  547. * Called by the usb core when a new device is connected that it thinks
  548. * this driver might be interested in.
  549. */
  550. static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  551. {
  552. struct usb_device *udev = interface_to_usbdev(intf);
  553. struct ld_usb *dev = NULL;
  554. struct usb_host_interface *iface_desc;
  555. struct usb_endpoint_descriptor *endpoint;
  556. char *buffer;
  557. int i;
  558. int retval = -ENOMEM;
  559. /* allocate memory for our device state and intialize it */
  560. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  561. if (dev == NULL) {
  562. dev_err(&intf->dev, "Out of memory\n");
  563. goto exit;
  564. }
  565. mutex_init(&dev->mutex);
  566. spin_lock_init(&dev->rbsl);
  567. dev->intf = intf;
  568. init_waitqueue_head(&dev->read_wait);
  569. init_waitqueue_head(&dev->write_wait);
  570. /* workaround for early firmware versions on fast computers */
  571. if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) &&
  572. ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) ||
  573. (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) &&
  574. (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) {
  575. buffer = kmalloc(256, GFP_KERNEL);
  576. if (buffer == NULL) {
  577. dev_err(&intf->dev, "Couldn't allocate string buffer\n");
  578. goto error;
  579. }
  580. /* usb_string makes SETUP+STALL to leave always ControlReadLoop */
  581. usb_string(udev, 255, buffer, 256);
  582. kfree(buffer);
  583. }
  584. iface_desc = intf->cur_altsetting;
  585. /* set up the endpoint information */
  586. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  587. endpoint = &iface_desc->endpoint[i].desc;
  588. if (usb_endpoint_is_int_in(endpoint))
  589. dev->interrupt_in_endpoint = endpoint;
  590. if (usb_endpoint_is_int_out(endpoint))
  591. dev->interrupt_out_endpoint = endpoint;
  592. }
  593. if (dev->interrupt_in_endpoint == NULL) {
  594. dev_err(&intf->dev, "Interrupt in endpoint not found\n");
  595. goto error;
  596. }
  597. if (dev->interrupt_out_endpoint == NULL)
  598. dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
  599. dev->interrupt_in_endpoint_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
  600. dev->ring_buffer = kmalloc(ring_buffer_size*(sizeof(size_t)+dev->interrupt_in_endpoint_size), GFP_KERNEL);
  601. if (!dev->ring_buffer) {
  602. dev_err(&intf->dev, "Couldn't allocate ring_buffer\n");
  603. goto error;
  604. }
  605. dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
  606. if (!dev->interrupt_in_buffer) {
  607. dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
  608. goto error;
  609. }
  610. dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  611. if (!dev->interrupt_in_urb) {
  612. dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
  613. goto error;
  614. }
  615. dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
  616. udev->descriptor.bMaxPacketSize0;
  617. dev->interrupt_out_buffer = kmalloc(write_buffer_size*dev->interrupt_out_endpoint_size, GFP_KERNEL);
  618. if (!dev->interrupt_out_buffer) {
  619. dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
  620. goto error;
  621. }
  622. dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
  623. if (!dev->interrupt_out_urb) {
  624. dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
  625. goto error;
  626. }
  627. dev->interrupt_in_interval = min_interrupt_in_interval > dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
  628. if (dev->interrupt_out_endpoint)
  629. dev->interrupt_out_interval = min_interrupt_out_interval > dev->interrupt_out_endpoint->bInterval ? min_interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
  630. /* we can register the device now, as it is ready */
  631. usb_set_intfdata(intf, dev);
  632. retval = usb_register_dev(intf, &ld_usb_class);
  633. if (retval) {
  634. /* something prevented us from registering this driver */
  635. dev_err(&intf->dev, "Not able to get a minor for this device.\n");
  636. usb_set_intfdata(intf, NULL);
  637. goto error;
  638. }
  639. /* let the user know what node this device is now attached to */
  640. dev_info(&intf->dev, "LD USB Device #%d now attached to major %d minor %d\n",
  641. (intf->minor - USB_LD_MINOR_BASE), USB_MAJOR, intf->minor);
  642. exit:
  643. return retval;
  644. error:
  645. ld_usb_delete(dev);
  646. return retval;
  647. }
  648. /**
  649. * ld_usb_disconnect
  650. *
  651. * Called by the usb core when the device is removed from the system.
  652. */
  653. static void ld_usb_disconnect(struct usb_interface *intf)
  654. {
  655. struct ld_usb *dev;
  656. int minor;
  657. dev = usb_get_intfdata(intf);
  658. usb_set_intfdata(intf, NULL);
  659. minor = intf->minor;
  660. /* give back our minor */
  661. usb_deregister_dev(intf, &ld_usb_class);
  662. mutex_lock(&dev->mutex);
  663. /* if the device is not opened, then we clean up right now */
  664. if (!dev->open_count) {
  665. mutex_unlock(&dev->mutex);
  666. ld_usb_delete(dev);
  667. } else {
  668. dev->intf = NULL;
  669. mutex_unlock(&dev->mutex);
  670. }
  671. dev_info(&intf->dev, "LD USB Device #%d now disconnected\n",
  672. (minor - USB_LD_MINOR_BASE));
  673. }
  674. /* usb specific object needed to register this driver with the usb subsystem */
  675. static struct usb_driver ld_usb_driver = {
  676. .name = "ldusb",
  677. .probe = ld_usb_probe,
  678. .disconnect = ld_usb_disconnect,
  679. .id_table = ld_usb_table,
  680. };
  681. /**
  682. * ld_usb_init
  683. */
  684. static int __init ld_usb_init(void)
  685. {
  686. int retval;
  687. /* register this driver with the USB subsystem */
  688. retval = usb_register(&ld_usb_driver);
  689. if (retval)
  690. err("usb_register failed for the "__FILE__" driver. Error number %d\n", retval);
  691. return retval;
  692. }
  693. /**
  694. * ld_usb_exit
  695. */
  696. static void __exit ld_usb_exit(void)
  697. {
  698. /* deregister this driver with the USB subsystem */
  699. usb_deregister(&ld_usb_driver);
  700. }
  701. module_init(ld_usb_init);
  702. module_exit(ld_usb_exit);