ldusb.c 25 KB

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