ldusb.c 23 KB

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