ldusb.c 24 KB

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