ldusb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. 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. /* prevent races between open() and disconnect() */
  157. static DEFINE_MUTEX(disconnect_mutex);
  158. static struct usb_driver ld_usb_driver;
  159. /**
  160. * ld_usb_abort_transfers
  161. * aborts transfers and frees associated data structures
  162. */
  163. static void ld_usb_abort_transfers(struct ld_usb *dev)
  164. {
  165. /* shutdown transfer */
  166. if (dev->interrupt_in_running) {
  167. dev->interrupt_in_running = 0;
  168. if (dev->intf)
  169. usb_kill_urb(dev->interrupt_in_urb);
  170. }
  171. if (dev->interrupt_out_busy)
  172. if (dev->intf)
  173. usb_kill_urb(dev->interrupt_out_urb);
  174. }
  175. /**
  176. * ld_usb_delete
  177. */
  178. static void ld_usb_delete(struct ld_usb *dev)
  179. {
  180. ld_usb_abort_transfers(dev);
  181. /* free data structures */
  182. usb_free_urb(dev->interrupt_in_urb);
  183. usb_free_urb(dev->interrupt_out_urb);
  184. kfree(dev->ring_buffer);
  185. kfree(dev->interrupt_in_buffer);
  186. kfree(dev->interrupt_out_buffer);
  187. kfree(dev);
  188. }
  189. /**
  190. * ld_usb_interrupt_in_callback
  191. */
  192. static void ld_usb_interrupt_in_callback(struct urb *urb)
  193. {
  194. struct ld_usb *dev = urb->context;
  195. size_t *actual_buffer;
  196. unsigned int next_ring_head;
  197. int retval;
  198. if (urb->status) {
  199. if (urb->status == -ENOENT ||
  200. urb->status == -ECONNRESET ||
  201. urb->status == -ESHUTDOWN) {
  202. goto exit;
  203. } else {
  204. dbg_info(&dev->intf->dev, "%s: nonzero status received: %d\n",
  205. __FUNCTION__, urb->status);
  206. goto resubmit; /* maybe we can recover */
  207. }
  208. }
  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. __FUNCTION__, urb->actual_length);
  219. } else
  220. dev_warn(&dev->intf->dev,
  221. "Ring buffer overflow, %d bytes dropped\n",
  222. urb->actual_length);
  223. }
  224. resubmit:
  225. /* resubmit if we're still running */
  226. if (dev->interrupt_in_running && dev->intf) {
  227. retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
  228. if (retval)
  229. dev_err(&dev->intf->dev,
  230. "usb_submit_urb failed (%d)\n", retval);
  231. }
  232. exit:
  233. dev->interrupt_in_done = 1;
  234. wake_up_interruptible(&dev->read_wait);
  235. }
  236. /**
  237. * ld_usb_interrupt_out_callback
  238. */
  239. static void ld_usb_interrupt_out_callback(struct urb *urb)
  240. {
  241. struct ld_usb *dev = urb->context;
  242. /* sync/async unlink faults aren't errors */
  243. if (urb->status && !(urb->status == -ENOENT ||
  244. urb->status == -ECONNRESET ||
  245. urb->status == -ESHUTDOWN))
  246. dbg_info(&dev->intf->dev,
  247. "%s - nonzero write interrupt status received: %d\n",
  248. __FUNCTION__, urb->status);
  249. dev->interrupt_out_busy = 0;
  250. wake_up_interruptible(&dev->write_wait);
  251. }
  252. /**
  253. * ld_usb_open
  254. */
  255. static int ld_usb_open(struct inode *inode, struct file *file)
  256. {
  257. struct ld_usb *dev;
  258. int subminor;
  259. int retval = 0;
  260. struct usb_interface *interface;
  261. nonseekable_open(inode, file);
  262. subminor = iminor(inode);
  263. mutex_lock(&disconnect_mutex);
  264. interface = usb_find_interface(&ld_usb_driver, subminor);
  265. if (!interface) {
  266. err("%s - error, can't find device for minor %d\n",
  267. __FUNCTION__, subminor);
  268. retval = -ENODEV;
  269. goto unlock_disconnect_exit;
  270. }
  271. dev = usb_get_intfdata(interface);
  272. if (!dev) {
  273. retval = -ENODEV;
  274. goto unlock_disconnect_exit;
  275. }
  276. /* lock this device */
  277. if (down_interruptible(&dev->sem)) {
  278. retval = -ERESTARTSYS;
  279. goto unlock_disconnect_exit;
  280. }
  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. usb_fill_int_urb(dev->interrupt_in_urb,
  291. interface_to_usbdev(interface),
  292. usb_rcvintpipe(interface_to_usbdev(interface),
  293. dev->interrupt_in_endpoint->bEndpointAddress),
  294. dev->interrupt_in_buffer,
  295. dev->interrupt_in_endpoint_size,
  296. ld_usb_interrupt_in_callback,
  297. dev,
  298. dev->interrupt_in_interval);
  299. dev->interrupt_in_running = 1;
  300. dev->interrupt_in_done = 0;
  301. retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
  302. if (retval) {
  303. dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval);
  304. dev->interrupt_in_running = 0;
  305. dev->open_count = 0;
  306. goto unlock_exit;
  307. }
  308. /* save device in the file's private structure */
  309. file->private_data = dev;
  310. unlock_exit:
  311. up(&dev->sem);
  312. unlock_disconnect_exit:
  313. mutex_unlock(&disconnect_mutex);
  314. return retval;
  315. }
  316. /**
  317. * ld_usb_release
  318. */
  319. static int ld_usb_release(struct inode *inode, struct file *file)
  320. {
  321. struct ld_usb *dev;
  322. int retval = 0;
  323. dev = file->private_data;
  324. if (dev == NULL) {
  325. retval = -ENODEV;
  326. goto exit;
  327. }
  328. if (down_interruptible(&dev->sem)) {
  329. retval = -ERESTARTSYS;
  330. goto exit;
  331. }
  332. if (dev->open_count != 1) {
  333. retval = -ENODEV;
  334. goto unlock_exit;
  335. }
  336. if (dev->intf == NULL) {
  337. /* the device was unplugged before the file was released */
  338. up(&dev->sem);
  339. /* unlock here as ld_usb_delete frees dev */
  340. ld_usb_delete(dev);
  341. goto exit;
  342. }
  343. /* wait until write transfer is finished */
  344. if (dev->interrupt_out_busy)
  345. wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
  346. ld_usb_abort_transfers(dev);
  347. dev->open_count = 0;
  348. unlock_exit:
  349. up(&dev->sem);
  350. exit:
  351. return retval;
  352. }
  353. /**
  354. * ld_usb_poll
  355. */
  356. static unsigned int ld_usb_poll(struct file *file, poll_table *wait)
  357. {
  358. struct ld_usb *dev;
  359. unsigned int mask = 0;
  360. dev = file->private_data;
  361. poll_wait(file, &dev->read_wait, wait);
  362. poll_wait(file, &dev->write_wait, wait);
  363. if (dev->ring_head != dev->ring_tail)
  364. mask |= POLLIN | POLLRDNORM;
  365. if (!dev->interrupt_out_busy)
  366. mask |= POLLOUT | POLLWRNORM;
  367. return mask;
  368. }
  369. /**
  370. * ld_usb_read
  371. */
  372. static ssize_t ld_usb_read(struct file *file, char __user *buffer, size_t count,
  373. loff_t *ppos)
  374. {
  375. struct ld_usb *dev;
  376. size_t *actual_buffer;
  377. size_t bytes_to_read;
  378. int retval = 0;
  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 (down_interruptible(&dev->sem)) {
  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. if (dev->ring_head == dev->ring_tail) {
  396. if (file->f_flags & O_NONBLOCK) {
  397. retval = -EAGAIN;
  398. goto unlock_exit;
  399. }
  400. retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done);
  401. if (retval < 0)
  402. goto unlock_exit;
  403. }
  404. /* actual_buffer contains actual_length + interrupt_in_buffer */
  405. actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_tail*(sizeof(size_t)+dev->interrupt_in_endpoint_size));
  406. bytes_to_read = min(count, *actual_buffer);
  407. if (bytes_to_read < *actual_buffer)
  408. dev_warn(&dev->intf->dev, "Read buffer overflow, %zd bytes dropped\n",
  409. *actual_buffer-bytes_to_read);
  410. /* copy one interrupt_in_buffer from ring_buffer into userspace */
  411. if (copy_to_user(buffer, actual_buffer+1, bytes_to_read)) {
  412. retval = -EFAULT;
  413. goto unlock_exit;
  414. }
  415. dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size;
  416. retval = bytes_to_read;
  417. unlock_exit:
  418. /* unlock the device */
  419. up(&dev->sem);
  420. exit:
  421. return retval;
  422. }
  423. /**
  424. * ld_usb_write
  425. */
  426. static ssize_t ld_usb_write(struct file *file, const char __user *buffer,
  427. size_t count, loff_t *ppos)
  428. {
  429. struct ld_usb *dev;
  430. size_t bytes_to_write;
  431. int retval = 0;
  432. dev = file->private_data;
  433. /* verify that we actually have some data to write */
  434. if (count == 0)
  435. goto exit;
  436. /* lock this object */
  437. if (down_interruptible(&dev->sem)) {
  438. retval = -ERESTARTSYS;
  439. goto exit;
  440. }
  441. /* verify that the device wasn't unplugged */
  442. if (dev->intf == NULL) {
  443. retval = -ENODEV;
  444. err("No device or device unplugged %d\n", retval);
  445. goto unlock_exit;
  446. }
  447. /* wait until previous transfer is finished */
  448. if (dev->interrupt_out_busy) {
  449. if (file->f_flags & O_NONBLOCK) {
  450. retval = -EAGAIN;
  451. goto unlock_exit;
  452. }
  453. retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
  454. if (retval < 0) {
  455. goto unlock_exit;
  456. }
  457. }
  458. /* write the data into interrupt_out_buffer from userspace */
  459. bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size);
  460. if (bytes_to_write < count)
  461. dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write);
  462. dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", __FUNCTION__, count, bytes_to_write);
  463. if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
  464. retval = -EFAULT;
  465. goto unlock_exit;
  466. }
  467. if (dev->interrupt_out_endpoint == NULL) {
  468. /* try HID_REQ_SET_REPORT=9 on control_endpoint instead of interrupt_out_endpoint */
  469. retval = usb_control_msg(interface_to_usbdev(dev->intf),
  470. usb_sndctrlpipe(interface_to_usbdev(dev->intf), 0),
  471. 9,
  472. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  473. 1 << 8, 0,
  474. dev->interrupt_out_buffer,
  475. bytes_to_write,
  476. USB_CTRL_SET_TIMEOUT * HZ);
  477. if (retval < 0)
  478. err("Couldn't submit HID_REQ_SET_REPORT %d\n", retval);
  479. goto unlock_exit;
  480. }
  481. /* send off the urb */
  482. usb_fill_int_urb(dev->interrupt_out_urb,
  483. interface_to_usbdev(dev->intf),
  484. usb_sndintpipe(interface_to_usbdev(dev->intf),
  485. dev->interrupt_out_endpoint->bEndpointAddress),
  486. dev->interrupt_out_buffer,
  487. bytes_to_write,
  488. ld_usb_interrupt_out_callback,
  489. dev,
  490. dev->interrupt_out_interval);
  491. dev->interrupt_out_busy = 1;
  492. wmb();
  493. retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
  494. if (retval) {
  495. dev->interrupt_out_busy = 0;
  496. err("Couldn't submit interrupt_out_urb %d\n", retval);
  497. goto unlock_exit;
  498. }
  499. retval = bytes_to_write;
  500. unlock_exit:
  501. /* unlock the device */
  502. up(&dev->sem);
  503. exit:
  504. return retval;
  505. }
  506. /* file operations needed when we register this driver */
  507. static const struct file_operations ld_usb_fops = {
  508. .owner = THIS_MODULE,
  509. .read = ld_usb_read,
  510. .write = ld_usb_write,
  511. .open = ld_usb_open,
  512. .release = ld_usb_release,
  513. .poll = ld_usb_poll,
  514. };
  515. /*
  516. * usb class driver info in order to get a minor number from the usb core,
  517. * and to have the device registered with the driver core
  518. */
  519. static struct usb_class_driver ld_usb_class = {
  520. .name = "ldusb%d",
  521. .fops = &ld_usb_fops,
  522. .minor_base = USB_LD_MINOR_BASE,
  523. };
  524. /**
  525. * ld_usb_probe
  526. *
  527. * Called by the usb core when a new device is connected that it thinks
  528. * this driver might be interested in.
  529. */
  530. static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  531. {
  532. struct usb_device *udev = interface_to_usbdev(intf);
  533. struct ld_usb *dev = NULL;
  534. struct usb_host_interface *iface_desc;
  535. struct usb_endpoint_descriptor *endpoint;
  536. char *buffer;
  537. int i;
  538. int retval = -ENOMEM;
  539. /* allocate memory for our device state and intialize it */
  540. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  541. if (dev == NULL) {
  542. dev_err(&intf->dev, "Out of memory\n");
  543. goto exit;
  544. }
  545. init_MUTEX(&dev->sem);
  546. dev->intf = intf;
  547. init_waitqueue_head(&dev->read_wait);
  548. init_waitqueue_head(&dev->write_wait);
  549. /* workaround for early firmware versions on fast computers */
  550. if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) &&
  551. ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) ||
  552. (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) &&
  553. (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) {
  554. buffer = kmalloc(256, GFP_KERNEL);
  555. if (buffer == NULL) {
  556. dev_err(&intf->dev, "Couldn't allocate string buffer\n");
  557. goto error;
  558. }
  559. /* usb_string makes SETUP+STALL to leave always ControlReadLoop */
  560. usb_string(udev, 255, buffer, 256);
  561. kfree(buffer);
  562. }
  563. iface_desc = intf->cur_altsetting;
  564. /* set up the endpoint information */
  565. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  566. endpoint = &iface_desc->endpoint[i].desc;
  567. if (usb_endpoint_is_int_in(endpoint))
  568. dev->interrupt_in_endpoint = endpoint;
  569. if (usb_endpoint_is_int_out(endpoint))
  570. dev->interrupt_out_endpoint = endpoint;
  571. }
  572. if (dev->interrupt_in_endpoint == NULL) {
  573. dev_err(&intf->dev, "Interrupt in endpoint not found\n");
  574. goto error;
  575. }
  576. if (dev->interrupt_out_endpoint == NULL)
  577. dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
  578. dev->interrupt_in_endpoint_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
  579. dev->ring_buffer = kmalloc(ring_buffer_size*(sizeof(size_t)+dev->interrupt_in_endpoint_size), GFP_KERNEL);
  580. if (!dev->ring_buffer) {
  581. dev_err(&intf->dev, "Couldn't allocate ring_buffer\n");
  582. goto error;
  583. }
  584. dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
  585. if (!dev->interrupt_in_buffer) {
  586. dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
  587. goto error;
  588. }
  589. dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  590. if (!dev->interrupt_in_urb) {
  591. dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
  592. goto error;
  593. }
  594. dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) :
  595. udev->descriptor.bMaxPacketSize0;
  596. dev->interrupt_out_buffer = kmalloc(write_buffer_size*dev->interrupt_out_endpoint_size, GFP_KERNEL);
  597. if (!dev->interrupt_out_buffer) {
  598. dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
  599. goto error;
  600. }
  601. dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
  602. if (!dev->interrupt_out_urb) {
  603. dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
  604. goto error;
  605. }
  606. dev->interrupt_in_interval = min_interrupt_in_interval > dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
  607. if (dev->interrupt_out_endpoint)
  608. dev->interrupt_out_interval = min_interrupt_out_interval > dev->interrupt_out_endpoint->bInterval ? min_interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
  609. /* we can register the device now, as it is ready */
  610. usb_set_intfdata(intf, dev);
  611. retval = usb_register_dev(intf, &ld_usb_class);
  612. if (retval) {
  613. /* something prevented us from registering this driver */
  614. dev_err(&intf->dev, "Not able to get a minor for this device.\n");
  615. usb_set_intfdata(intf, NULL);
  616. goto error;
  617. }
  618. /* let the user know what node this device is now attached to */
  619. dev_info(&intf->dev, "LD USB Device #%d now attached to major %d minor %d\n",
  620. (intf->minor - USB_LD_MINOR_BASE), USB_MAJOR, intf->minor);
  621. exit:
  622. return retval;
  623. error:
  624. ld_usb_delete(dev);
  625. return retval;
  626. }
  627. /**
  628. * ld_usb_disconnect
  629. *
  630. * Called by the usb core when the device is removed from the system.
  631. */
  632. static void ld_usb_disconnect(struct usb_interface *intf)
  633. {
  634. struct ld_usb *dev;
  635. int minor;
  636. mutex_lock(&disconnect_mutex);
  637. dev = usb_get_intfdata(intf);
  638. usb_set_intfdata(intf, NULL);
  639. down(&dev->sem);
  640. minor = intf->minor;
  641. /* give back our minor */
  642. usb_deregister_dev(intf, &ld_usb_class);
  643. /* if the device is not opened, then we clean up right now */
  644. if (!dev->open_count) {
  645. up(&dev->sem);
  646. ld_usb_delete(dev);
  647. } else {
  648. dev->intf = NULL;
  649. up(&dev->sem);
  650. }
  651. mutex_unlock(&disconnect_mutex);
  652. dev_info(&intf->dev, "LD USB Device #%d now disconnected\n",
  653. (minor - USB_LD_MINOR_BASE));
  654. }
  655. /* usb specific object needed to register this driver with the usb subsystem */
  656. static struct usb_driver ld_usb_driver = {
  657. .name = "ldusb",
  658. .probe = ld_usb_probe,
  659. .disconnect = ld_usb_disconnect,
  660. .id_table = ld_usb_table,
  661. };
  662. /**
  663. * ld_usb_init
  664. */
  665. static int __init ld_usb_init(void)
  666. {
  667. int retval;
  668. /* register this driver with the USB subsystem */
  669. retval = usb_register(&ld_usb_driver);
  670. if (retval)
  671. err("usb_register failed for the "__FILE__" driver. Error number %d\n", retval);
  672. return retval;
  673. }
  674. /**
  675. * ld_usb_exit
  676. */
  677. static void __exit ld_usb_exit(void)
  678. {
  679. /* deregister this driver with the USB subsystem */
  680. usb_deregister(&ld_usb_driver);
  681. }
  682. module_init(ld_usb_init);
  683. module_exit(ld_usb_exit);