ldusb.c 25 KB

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