ldusb.c 23 KB

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