ldusb.c 22 KB

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