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