ldusb.c 23 KB

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