ldusb.c 23 KB

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