ldusb.c 24 KB

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