ldusb.c 23 KB

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