ldusb.c 23 KB

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