ldusb.c 22 KB

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