ldusb.c 23 KB

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