ldusb.c 24 KB

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