ldusb.c 24 KB

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