ldusb.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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. #ifdef CONFIG_USB_DEBUG
  126. static int debug = 1;
  127. #else
  128. static int debug = 0;
  129. #endif
  130. /* Use our own dbg macro */
  131. #define dbg_info(dev, format, arg...) do { if (debug) dev_info(dev , format , ## arg); } while (0)
  132. /* Module parameters */
  133. module_param(debug, int, S_IRUGO | S_IWUSR);
  134. MODULE_PARM_DESC(debug, "Debug enabled or not");
  135. /* All interrupt in transfers are collected in a ring buffer to
  136. * avoid racing conditions and get better performance of the driver.
  137. */
  138. static int ring_buffer_size = 128;
  139. module_param(ring_buffer_size, int, 0);
  140. MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports");
  141. /* The write_buffer can contain more than one interrupt out transfer.
  142. */
  143. static int write_buffer_size = 10;
  144. module_param(write_buffer_size, int, 0);
  145. MODULE_PARM_DESC(write_buffer_size, "Write buffer size in reports");
  146. /* As of kernel version 2.6.4 ehci-hcd uses an
  147. * "only one interrupt transfer per frame" shortcut
  148. * to simplify the scheduling of periodic transfers.
  149. * This conflicts with our standard 1ms intervals for in and out URBs.
  150. * We use default intervals of 2ms for in and 2ms for out transfers,
  151. * which should be fast enough.
  152. * Increase the interval to allow more devices that do interrupt transfers,
  153. * or set to 1 to use the standard interval from the endpoint descriptors.
  154. */
  155. static int min_interrupt_in_interval = 2;
  156. module_param(min_interrupt_in_interval, int, 0);
  157. MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms");
  158. static int min_interrupt_out_interval = 2;
  159. module_param(min_interrupt_out_interval, int, 0);
  160. MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms");
  161. /* Structure to hold all of our device specific stuff */
  162. struct ld_usb {
  163. struct mutex mutex; /* locks this structure */
  164. struct usb_interface* intf; /* save off the usb interface pointer */
  165. int open_count; /* number of times this port has been opened */
  166. char* ring_buffer;
  167. unsigned int ring_head;
  168. unsigned int ring_tail;
  169. wait_queue_head_t read_wait;
  170. wait_queue_head_t write_wait;
  171. char* interrupt_in_buffer;
  172. struct usb_endpoint_descriptor* interrupt_in_endpoint;
  173. struct urb* interrupt_in_urb;
  174. int interrupt_in_interval;
  175. size_t interrupt_in_endpoint_size;
  176. int interrupt_in_running;
  177. int interrupt_in_done;
  178. int buffer_overflow;
  179. spinlock_t rbsl;
  180. char* interrupt_out_buffer;
  181. struct usb_endpoint_descriptor* interrupt_out_endpoint;
  182. struct urb* interrupt_out_urb;
  183. int interrupt_out_interval;
  184. size_t interrupt_out_endpoint_size;
  185. int interrupt_out_busy;
  186. };
  187. static struct usb_driver ld_usb_driver;
  188. /**
  189. * ld_usb_abort_transfers
  190. * aborts transfers and frees associated data structures
  191. */
  192. static void ld_usb_abort_transfers(struct ld_usb *dev)
  193. {
  194. /* shutdown transfer */
  195. if (dev->interrupt_in_running) {
  196. dev->interrupt_in_running = 0;
  197. if (dev->intf)
  198. usb_kill_urb(dev->interrupt_in_urb);
  199. }
  200. if (dev->interrupt_out_busy)
  201. if (dev->intf)
  202. usb_kill_urb(dev->interrupt_out_urb);
  203. }
  204. /**
  205. * ld_usb_delete
  206. */
  207. static void ld_usb_delete(struct ld_usb *dev)
  208. {
  209. ld_usb_abort_transfers(dev);
  210. /* free data structures */
  211. usb_free_urb(dev->interrupt_in_urb);
  212. usb_free_urb(dev->interrupt_out_urb);
  213. kfree(dev->ring_buffer);
  214. kfree(dev->interrupt_in_buffer);
  215. kfree(dev->interrupt_out_buffer);
  216. kfree(dev);
  217. }
  218. /**
  219. * ld_usb_interrupt_in_callback
  220. */
  221. static void ld_usb_interrupt_in_callback(struct urb *urb)
  222. {
  223. struct ld_usb *dev = urb->context;
  224. size_t *actual_buffer;
  225. unsigned int next_ring_head;
  226. int status = urb->status;
  227. int retval;
  228. if (status) {
  229. if (status == -ENOENT ||
  230. status == -ECONNRESET ||
  231. status == -ESHUTDOWN) {
  232. goto exit;
  233. } else {
  234. dbg_info(&dev->intf->dev, "%s: nonzero status received: %d\n",
  235. __func__, status);
  236. spin_lock(&dev->rbsl);
  237. goto resubmit; /* maybe we can recover */
  238. }
  239. }
  240. spin_lock(&dev->rbsl);
  241. if (urb->actual_length > 0) {
  242. next_ring_head = (dev->ring_head+1) % ring_buffer_size;
  243. if (next_ring_head != dev->ring_tail) {
  244. actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_head*(sizeof(size_t)+dev->interrupt_in_endpoint_size));
  245. /* actual_buffer gets urb->actual_length + interrupt_in_buffer */
  246. *actual_buffer = urb->actual_length;
  247. memcpy(actual_buffer+1, dev->interrupt_in_buffer, urb->actual_length);
  248. dev->ring_head = next_ring_head;
  249. dbg_info(&dev->intf->dev, "%s: received %d bytes\n",
  250. __func__, urb->actual_length);
  251. } else {
  252. dev_warn(&dev->intf->dev,
  253. "Ring buffer overflow, %d bytes dropped\n",
  254. urb->actual_length);
  255. dev->buffer_overflow = 1;
  256. }
  257. }
  258. resubmit:
  259. /* resubmit if we're still running */
  260. if (dev->interrupt_in_running && !dev->buffer_overflow && dev->intf) {
  261. retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
  262. if (retval) {
  263. dev_err(&dev->intf->dev,
  264. "usb_submit_urb failed (%d)\n", retval);
  265. dev->buffer_overflow = 1;
  266. }
  267. }
  268. spin_unlock(&dev->rbsl);
  269. exit:
  270. dev->interrupt_in_done = 1;
  271. wake_up_interruptible(&dev->read_wait);
  272. }
  273. /**
  274. * ld_usb_interrupt_out_callback
  275. */
  276. static void ld_usb_interrupt_out_callback(struct urb *urb)
  277. {
  278. struct ld_usb *dev = urb->context;
  279. int status = urb->status;
  280. /* sync/async unlink faults aren't errors */
  281. if (status && !(status == -ENOENT ||
  282. status == -ECONNRESET ||
  283. status == -ESHUTDOWN))
  284. dbg_info(&dev->intf->dev,
  285. "%s - nonzero write interrupt status received: %d\n",
  286. __func__, status);
  287. dev->interrupt_out_busy = 0;
  288. wake_up_interruptible(&dev->write_wait);
  289. }
  290. /**
  291. * ld_usb_open
  292. */
  293. static int ld_usb_open(struct inode *inode, struct file *file)
  294. {
  295. struct ld_usb *dev;
  296. int subminor;
  297. int retval;
  298. struct usb_interface *interface;
  299. nonseekable_open(inode, file);
  300. subminor = iminor(inode);
  301. interface = usb_find_interface(&ld_usb_driver, subminor);
  302. if (!interface) {
  303. err("%s - error, can't find device for minor %d\n",
  304. __func__, subminor);
  305. return -ENODEV;
  306. }
  307. dev = usb_get_intfdata(interface);
  308. if (!dev)
  309. return -ENODEV;
  310. /* lock this device */
  311. if (mutex_lock_interruptible(&dev->mutex))
  312. return -ERESTARTSYS;
  313. /* allow opening only once */
  314. if (dev->open_count) {
  315. retval = -EBUSY;
  316. goto unlock_exit;
  317. }
  318. dev->open_count = 1;
  319. /* initialize in direction */
  320. dev->ring_head = 0;
  321. dev->ring_tail = 0;
  322. dev->buffer_overflow = 0;
  323. usb_fill_int_urb(dev->interrupt_in_urb,
  324. interface_to_usbdev(interface),
  325. usb_rcvintpipe(interface_to_usbdev(interface),
  326. dev->interrupt_in_endpoint->bEndpointAddress),
  327. dev->interrupt_in_buffer,
  328. dev->interrupt_in_endpoint_size,
  329. ld_usb_interrupt_in_callback,
  330. dev,
  331. dev->interrupt_in_interval);
  332. dev->interrupt_in_running = 1;
  333. dev->interrupt_in_done = 0;
  334. retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
  335. if (retval) {
  336. dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval);
  337. dev->interrupt_in_running = 0;
  338. dev->open_count = 0;
  339. goto unlock_exit;
  340. }
  341. /* save device in the file's private structure */
  342. file->private_data = dev;
  343. unlock_exit:
  344. mutex_unlock(&dev->mutex);
  345. return retval;
  346. }
  347. /**
  348. * ld_usb_release
  349. */
  350. static int ld_usb_release(struct inode *inode, struct file *file)
  351. {
  352. struct ld_usb *dev;
  353. int retval = 0;
  354. dev = file->private_data;
  355. if (dev == NULL) {
  356. retval = -ENODEV;
  357. goto exit;
  358. }
  359. if (mutex_lock_interruptible(&dev->mutex)) {
  360. retval = -ERESTARTSYS;
  361. goto exit;
  362. }
  363. if (dev->open_count != 1) {
  364. retval = -ENODEV;
  365. goto unlock_exit;
  366. }
  367. if (dev->intf == NULL) {
  368. /* the device was unplugged before the file was released */
  369. mutex_unlock(&dev->mutex);
  370. /* unlock here as ld_usb_delete frees dev */
  371. ld_usb_delete(dev);
  372. goto exit;
  373. }
  374. /* wait until write transfer is finished */
  375. if (dev->interrupt_out_busy)
  376. wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
  377. ld_usb_abort_transfers(dev);
  378. dev->open_count = 0;
  379. unlock_exit:
  380. mutex_unlock(&dev->mutex);
  381. exit:
  382. return retval;
  383. }
  384. /**
  385. * ld_usb_poll
  386. */
  387. static unsigned int ld_usb_poll(struct file *file, poll_table *wait)
  388. {
  389. struct ld_usb *dev;
  390. unsigned int mask = 0;
  391. dev = file->private_data;
  392. if (!dev->intf)
  393. return POLLERR | POLLHUP;
  394. poll_wait(file, &dev->read_wait, wait);
  395. poll_wait(file, &dev->write_wait, wait);
  396. if (dev->ring_head != dev->ring_tail)
  397. mask |= POLLIN | POLLRDNORM;
  398. if (!dev->interrupt_out_busy)
  399. mask |= POLLOUT | POLLWRNORM;
  400. return mask;
  401. }
  402. /**
  403. * ld_usb_read
  404. */
  405. static ssize_t ld_usb_read(struct file *file, char __user *buffer, size_t count,
  406. loff_t *ppos)
  407. {
  408. struct ld_usb *dev;
  409. size_t *actual_buffer;
  410. size_t bytes_to_read;
  411. int retval = 0;
  412. int rv;
  413. dev = file->private_data;
  414. /* verify that we actually have some data to read */
  415. if (count == 0)
  416. goto exit;
  417. /* lock this object */
  418. if (mutex_lock_interruptible(&dev->mutex)) {
  419. retval = -ERESTARTSYS;
  420. goto exit;
  421. }
  422. /* verify that the device wasn't unplugged */
  423. if (dev->intf == NULL) {
  424. retval = -ENODEV;
  425. err("No device or device unplugged %d\n", retval);
  426. goto unlock_exit;
  427. }
  428. /* wait for data */
  429. spin_lock_irq(&dev->rbsl);
  430. if (dev->ring_head == dev->ring_tail) {
  431. dev->interrupt_in_done = 0;
  432. spin_unlock_irq(&dev->rbsl);
  433. if (file->f_flags & O_NONBLOCK) {
  434. retval = -EAGAIN;
  435. goto unlock_exit;
  436. }
  437. retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done);
  438. if (retval < 0)
  439. goto unlock_exit;
  440. } else {
  441. spin_unlock_irq(&dev->rbsl);
  442. }
  443. /* actual_buffer contains actual_length + interrupt_in_buffer */
  444. actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_tail*(sizeof(size_t)+dev->interrupt_in_endpoint_size));
  445. bytes_to_read = min(count, *actual_buffer);
  446. if (bytes_to_read < *actual_buffer)
  447. dev_warn(&dev->intf->dev, "Read buffer overflow, %zd bytes dropped\n",
  448. *actual_buffer-bytes_to_read);
  449. /* copy one interrupt_in_buffer from ring_buffer into userspace */
  450. if (copy_to_user(buffer, actual_buffer+1, bytes_to_read)) {
  451. retval = -EFAULT;
  452. goto unlock_exit;
  453. }
  454. dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size;
  455. retval = bytes_to_read;
  456. spin_lock_irq(&dev->rbsl);
  457. if (dev->buffer_overflow) {
  458. dev->buffer_overflow = 0;
  459. spin_unlock_irq(&dev->rbsl);
  460. rv = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
  461. if (rv < 0)
  462. dev->buffer_overflow = 1;
  463. } else {
  464. spin_unlock_irq(&dev->rbsl);
  465. }
  466. unlock_exit:
  467. /* unlock the device */
  468. mutex_unlock(&dev->mutex);
  469. exit:
  470. return retval;
  471. }
  472. /**
  473. * ld_usb_write
  474. */
  475. static ssize_t ld_usb_write(struct file *file, const char __user *buffer,
  476. size_t count, loff_t *ppos)
  477. {
  478. struct ld_usb *dev;
  479. size_t bytes_to_write;
  480. int retval = 0;
  481. dev = file->private_data;
  482. /* verify that we actually have some data to write */
  483. if (count == 0)
  484. goto exit;
  485. /* lock this object */
  486. if (mutex_lock_interruptible(&dev->mutex)) {
  487. retval = -ERESTARTSYS;
  488. goto exit;
  489. }
  490. /* verify that the device wasn't unplugged */
  491. if (dev->intf == NULL) {
  492. retval = -ENODEV;
  493. err("No device or device unplugged %d\n", retval);
  494. goto unlock_exit;
  495. }
  496. /* wait until previous transfer is finished */
  497. if (dev->interrupt_out_busy) {
  498. if (file->f_flags & O_NONBLOCK) {
  499. retval = -EAGAIN;
  500. goto unlock_exit;
  501. }
  502. retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
  503. if (retval < 0) {
  504. goto unlock_exit;
  505. }
  506. }
  507. /* write the data into interrupt_out_buffer from userspace */
  508. bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size);
  509. if (bytes_to_write < count)
  510. dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write);
  511. dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", __func__, count, bytes_to_write);
  512. if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
  513. retval = -EFAULT;
  514. goto unlock_exit;
  515. }
  516. if (dev->interrupt_out_endpoint == NULL) {
  517. /* try HID_REQ_SET_REPORT=9 on control_endpoint instead of interrupt_out_endpoint */
  518. retval = usb_control_msg(interface_to_usbdev(dev->intf),
  519. usb_sndctrlpipe(interface_to_usbdev(dev->intf), 0),
  520. 9,
  521. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  522. 1 << 8, 0,
  523. dev->interrupt_out_buffer,
  524. bytes_to_write,
  525. USB_CTRL_SET_TIMEOUT * HZ);
  526. if (retval < 0)
  527. err("Couldn't submit HID_REQ_SET_REPORT %d\n", retval);
  528. goto unlock_exit;
  529. }
  530. /* send off the urb */
  531. usb_fill_int_urb(dev->interrupt_out_urb,
  532. interface_to_usbdev(dev->intf),
  533. usb_sndintpipe(interface_to_usbdev(dev->intf),
  534. dev->interrupt_out_endpoint->bEndpointAddress),
  535. dev->interrupt_out_buffer,
  536. bytes_to_write,
  537. ld_usb_interrupt_out_callback,
  538. dev,
  539. dev->interrupt_out_interval);
  540. dev->interrupt_out_busy = 1;
  541. wmb();
  542. retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
  543. if (retval) {
  544. dev->interrupt_out_busy = 0;
  545. err("Couldn't submit interrupt_out_urb %d\n", retval);
  546. goto unlock_exit;
  547. }
  548. retval = bytes_to_write;
  549. unlock_exit:
  550. /* unlock the device */
  551. mutex_unlock(&dev->mutex);
  552. exit:
  553. return retval;
  554. }
  555. /* file operations needed when we register this driver */
  556. static const struct file_operations ld_usb_fops = {
  557. .owner = THIS_MODULE,
  558. .read = ld_usb_read,
  559. .write = ld_usb_write,
  560. .open = ld_usb_open,
  561. .release = ld_usb_release,
  562. .poll = ld_usb_poll,
  563. .llseek = no_llseek,
  564. };
  565. /*
  566. * usb class driver info in order to get a minor number from the usb core,
  567. * and to have the device registered with the driver core
  568. */
  569. static struct usb_class_driver ld_usb_class = {
  570. .name = "ldusb%d",
  571. .fops = &ld_usb_fops,
  572. .minor_base = USB_LD_MINOR_BASE,
  573. };
  574. /**
  575. * ld_usb_probe
  576. *
  577. * Called by the usb core when a new device is connected that it thinks
  578. * this driver might be interested in.
  579. */
  580. static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  581. {
  582. struct usb_device *udev = interface_to_usbdev(intf);
  583. struct ld_usb *dev = NULL;
  584. struct usb_host_interface *iface_desc;
  585. struct usb_endpoint_descriptor *endpoint;
  586. char *buffer;
  587. int i;
  588. int retval = -ENOMEM;
  589. /* allocate memory for our device state and initialize it */
  590. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  591. if (dev == NULL) {
  592. dev_err(&intf->dev, "Out of memory\n");
  593. goto exit;
  594. }
  595. mutex_init(&dev->mutex);
  596. spin_lock_init(&dev->rbsl);
  597. dev->intf = intf;
  598. init_waitqueue_head(&dev->read_wait);
  599. init_waitqueue_head(&dev->write_wait);
  600. /* workaround for early firmware versions on fast computers */
  601. if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) &&
  602. ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) ||
  603. (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) &&
  604. (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) {
  605. buffer = kmalloc(256, GFP_KERNEL);
  606. if (buffer == NULL) {
  607. dev_err(&intf->dev, "Couldn't allocate string buffer\n");
  608. goto error;
  609. }
  610. /* usb_string makes SETUP+STALL to leave always ControlReadLoop */
  611. usb_string(udev, 255, buffer, 256);
  612. kfree(buffer);
  613. }
  614. iface_desc = intf->cur_altsetting;
  615. /* set up the endpoint information */
  616. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  617. endpoint = &iface_desc->endpoint[i].desc;
  618. if (usb_endpoint_is_int_in(endpoint))
  619. dev->interrupt_in_endpoint = endpoint;
  620. if (usb_endpoint_is_int_out(endpoint))
  621. dev->interrupt_out_endpoint = endpoint;
  622. }
  623. if (dev->interrupt_in_endpoint == NULL) {
  624. dev_err(&intf->dev, "Interrupt in endpoint not found\n");
  625. goto error;
  626. }
  627. if (dev->interrupt_out_endpoint == NULL)
  628. dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
  629. dev->interrupt_in_endpoint_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
  630. dev->ring_buffer = kmalloc(ring_buffer_size*(sizeof(size_t)+dev->interrupt_in_endpoint_size), GFP_KERNEL);
  631. if (!dev->ring_buffer) {
  632. dev_err(&intf->dev, "Couldn't allocate ring_buffer\n");
  633. goto error;
  634. }
  635. dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
  636. if (!dev->interrupt_in_buffer) {
  637. dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
  638. goto error;
  639. }
  640. dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  641. if (!dev->interrupt_in_urb) {
  642. dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
  643. goto error;
  644. }
  645. dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
  646. udev->descriptor.bMaxPacketSize0;
  647. dev->interrupt_out_buffer = kmalloc(write_buffer_size*dev->interrupt_out_endpoint_size, GFP_KERNEL);
  648. if (!dev->interrupt_out_buffer) {
  649. dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
  650. goto error;
  651. }
  652. dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
  653. if (!dev->interrupt_out_urb) {
  654. dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
  655. goto error;
  656. }
  657. dev->interrupt_in_interval = min_interrupt_in_interval > dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
  658. if (dev->interrupt_out_endpoint)
  659. dev->interrupt_out_interval = min_interrupt_out_interval > dev->interrupt_out_endpoint->bInterval ? min_interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
  660. /* we can register the device now, as it is ready */
  661. usb_set_intfdata(intf, dev);
  662. retval = usb_register_dev(intf, &ld_usb_class);
  663. if (retval) {
  664. /* something prevented us from registering this driver */
  665. dev_err(&intf->dev, "Not able to get a minor for this device.\n");
  666. usb_set_intfdata(intf, NULL);
  667. goto error;
  668. }
  669. /* let the user know what node this device is now attached to */
  670. dev_info(&intf->dev, "LD USB Device #%d now attached to major %d minor %d\n",
  671. (intf->minor - USB_LD_MINOR_BASE), USB_MAJOR, intf->minor);
  672. exit:
  673. return retval;
  674. error:
  675. ld_usb_delete(dev);
  676. return retval;
  677. }
  678. /**
  679. * ld_usb_disconnect
  680. *
  681. * Called by the usb core when the device is removed from the system.
  682. */
  683. static void ld_usb_disconnect(struct usb_interface *intf)
  684. {
  685. struct ld_usb *dev;
  686. int minor;
  687. dev = usb_get_intfdata(intf);
  688. usb_set_intfdata(intf, NULL);
  689. minor = intf->minor;
  690. /* give back our minor */
  691. usb_deregister_dev(intf, &ld_usb_class);
  692. mutex_lock(&dev->mutex);
  693. /* if the device is not opened, then we clean up right now */
  694. if (!dev->open_count) {
  695. mutex_unlock(&dev->mutex);
  696. ld_usb_delete(dev);
  697. } else {
  698. dev->intf = NULL;
  699. /* wake up pollers */
  700. wake_up_interruptible_all(&dev->read_wait);
  701. wake_up_interruptible_all(&dev->write_wait);
  702. mutex_unlock(&dev->mutex);
  703. }
  704. dev_info(&intf->dev, "LD USB Device #%d now disconnected\n",
  705. (minor - USB_LD_MINOR_BASE));
  706. }
  707. /* usb specific object needed to register this driver with the usb subsystem */
  708. static struct usb_driver ld_usb_driver = {
  709. .name = "ldusb",
  710. .probe = ld_usb_probe,
  711. .disconnect = ld_usb_disconnect,
  712. .id_table = ld_usb_table,
  713. };
  714. /**
  715. * ld_usb_init
  716. */
  717. static int __init ld_usb_init(void)
  718. {
  719. int retval;
  720. /* register this driver with the USB subsystem */
  721. retval = usb_register(&ld_usb_driver);
  722. if (retval)
  723. err("usb_register failed for the %s driver. Error number %d\n", __FILE__, retval);
  724. return retval;
  725. }
  726. /**
  727. * ld_usb_exit
  728. */
  729. static void __exit ld_usb_exit(void)
  730. {
  731. /* deregister this driver with the USB subsystem */
  732. usb_deregister(&ld_usb_driver);
  733. }
  734. module_init(ld_usb_init);
  735. module_exit(ld_usb_exit);