adutux.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. * adutux - driver for ADU devices from Ontrak Control Systems
  3. * This is an experimental driver. Use at your own risk.
  4. * This driver is not supported by Ontrak Control Systems.
  5. *
  6. * Copyright (c) 2003 John Homppi (SCO, leave this notice here)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * derived from the Lego USB Tower driver 0.56:
  14. * Copyright (c) 2003 David Glance <davidgsf@sourceforge.net>
  15. * 2001 Juergen Stuber <stuber@loria.fr>
  16. * that was derived from USB Skeleton driver - 0.5
  17. * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com)
  18. *
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/module.h>
  25. #include <linux/usb.h>
  26. #include <linux/mutex.h>
  27. #include <linux/uaccess.h>
  28. #ifdef CONFIG_USB_DEBUG
  29. static int debug = 5;
  30. #else
  31. static int debug = 1;
  32. #endif
  33. /* Use our own dbg macro */
  34. #undef dbg
  35. #define dbg(lvl, format, arg...) \
  36. do { \
  37. if (debug >= lvl) \
  38. printk(KERN_DEBUG "%s: " format "\n", __FILE__, ##arg); \
  39. } while (0)
  40. /* Version Information */
  41. #define DRIVER_VERSION "v0.0.13"
  42. #define DRIVER_AUTHOR "John Homppi"
  43. #define DRIVER_DESC "adutux (see www.ontrak.net)"
  44. /* Module parameters */
  45. module_param(debug, int, S_IRUGO | S_IWUSR);
  46. MODULE_PARM_DESC(debug, "Debug enabled or not");
  47. /* Define these values to match your device */
  48. #define ADU_VENDOR_ID 0x0a07
  49. #define ADU_PRODUCT_ID 0x0064
  50. /* table of devices that work with this driver */
  51. static const struct usb_device_id device_table[] = {
  52. { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) }, /* ADU100 */
  53. { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) }, /* ADU120 */
  54. { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) }, /* ADU130 */
  55. { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) }, /* ADU200 */
  56. { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) }, /* ADU208 */
  57. { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) }, /* ADU218 */
  58. { } /* Terminating entry */
  59. };
  60. MODULE_DEVICE_TABLE(usb, device_table);
  61. #ifdef CONFIG_USB_DYNAMIC_MINORS
  62. #define ADU_MINOR_BASE 0
  63. #else
  64. #define ADU_MINOR_BASE 67
  65. #endif
  66. /* we can have up to this number of device plugged in at once */
  67. #define MAX_DEVICES 16
  68. #define COMMAND_TIMEOUT (2*HZ) /* 60 second timeout for a command */
  69. /*
  70. * The locking scheme is a vanilla 3-lock:
  71. * adu_device.buflock: A spinlock, covers what IRQs touch.
  72. * adutux_mutex: A Static lock to cover open_count. It would also cover
  73. * any globals, but we don't have them in 2.6.
  74. * adu_device.mtx: A mutex to hold across sleepers like copy_from_user.
  75. * It covers all of adu_device, except the open_count
  76. * and what .buflock covers.
  77. */
  78. /* Structure to hold all of our device specific stuff */
  79. struct adu_device {
  80. struct mutex mtx;
  81. struct usb_device *udev; /* save off the usb device pointer */
  82. struct usb_interface *interface;
  83. unsigned int minor; /* the starting minor number for this device */
  84. char serial_number[8];
  85. int open_count; /* number of times this port has been opened */
  86. char *read_buffer_primary;
  87. int read_buffer_length;
  88. char *read_buffer_secondary;
  89. int secondary_head;
  90. int secondary_tail;
  91. spinlock_t buflock;
  92. wait_queue_head_t read_wait;
  93. wait_queue_head_t write_wait;
  94. char *interrupt_in_buffer;
  95. struct usb_endpoint_descriptor *interrupt_in_endpoint;
  96. struct urb *interrupt_in_urb;
  97. int read_urb_finished;
  98. char *interrupt_out_buffer;
  99. struct usb_endpoint_descriptor *interrupt_out_endpoint;
  100. struct urb *interrupt_out_urb;
  101. int out_urb_finished;
  102. };
  103. static DEFINE_MUTEX(adutux_mutex);
  104. static struct usb_driver adu_driver;
  105. static void adu_debug_data(int level, const char *function, int size,
  106. const unsigned char *data)
  107. {
  108. int i;
  109. if (debug < level)
  110. return;
  111. printk(KERN_DEBUG "%s: %s - length = %d, data = ",
  112. __FILE__, function, size);
  113. for (i = 0; i < size; ++i)
  114. printk("%.2x ", data[i]);
  115. printk("\n");
  116. }
  117. /**
  118. * adu_abort_transfers
  119. * aborts transfers and frees associated data structures
  120. */
  121. static void adu_abort_transfers(struct adu_device *dev)
  122. {
  123. unsigned long flags;
  124. if (dev->udev == NULL)
  125. return;
  126. /* shutdown transfer */
  127. /* XXX Anchor these instead */
  128. spin_lock_irqsave(&dev->buflock, flags);
  129. if (!dev->read_urb_finished) {
  130. spin_unlock_irqrestore(&dev->buflock, flags);
  131. usb_kill_urb(dev->interrupt_in_urb);
  132. } else
  133. spin_unlock_irqrestore(&dev->buflock, flags);
  134. spin_lock_irqsave(&dev->buflock, flags);
  135. if (!dev->out_urb_finished) {
  136. spin_unlock_irqrestore(&dev->buflock, flags);
  137. usb_kill_urb(dev->interrupt_out_urb);
  138. } else
  139. spin_unlock_irqrestore(&dev->buflock, flags);
  140. }
  141. static void adu_delete(struct adu_device *dev)
  142. {
  143. /* free data structures */
  144. usb_free_urb(dev->interrupt_in_urb);
  145. usb_free_urb(dev->interrupt_out_urb);
  146. kfree(dev->read_buffer_primary);
  147. kfree(dev->read_buffer_secondary);
  148. kfree(dev->interrupt_in_buffer);
  149. kfree(dev->interrupt_out_buffer);
  150. kfree(dev);
  151. }
  152. static void adu_interrupt_in_callback(struct urb *urb)
  153. {
  154. struct adu_device *dev = urb->context;
  155. int status = urb->status;
  156. adu_debug_data(5, __func__, urb->actual_length,
  157. urb->transfer_buffer);
  158. spin_lock(&dev->buflock);
  159. if (status != 0) {
  160. if ((status != -ENOENT) && (status != -ECONNRESET) &&
  161. (status != -ESHUTDOWN)) {
  162. dev_dbg(&dev->udev->dev,
  163. "%s : nonzero status received: %d\n",
  164. __func__, status);
  165. }
  166. goto exit;
  167. }
  168. if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
  169. if (dev->read_buffer_length <
  170. (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) -
  171. (urb->actual_length)) {
  172. memcpy (dev->read_buffer_primary +
  173. dev->read_buffer_length,
  174. dev->interrupt_in_buffer, urb->actual_length);
  175. dev->read_buffer_length += urb->actual_length;
  176. dev_dbg(&dev->udev->dev,"%s reading %d\n", __func__,
  177. urb->actual_length);
  178. } else {
  179. dev_dbg(&dev->udev->dev,"%s : read_buffer overflow\n",
  180. __func__);
  181. }
  182. }
  183. exit:
  184. dev->read_urb_finished = 1;
  185. spin_unlock(&dev->buflock);
  186. /* always wake up so we recover from errors */
  187. wake_up_interruptible(&dev->read_wait);
  188. adu_debug_data(5, __func__, urb->actual_length,
  189. urb->transfer_buffer);
  190. }
  191. static void adu_interrupt_out_callback(struct urb *urb)
  192. {
  193. struct adu_device *dev = urb->context;
  194. int status = urb->status;
  195. adu_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
  196. if (status != 0) {
  197. if ((status != -ENOENT) &&
  198. (status != -ECONNRESET)) {
  199. dev_dbg(&dev->udev->dev,
  200. "%s :nonzero status received: %d\n", __func__,
  201. status);
  202. }
  203. goto exit;
  204. }
  205. spin_lock(&dev->buflock);
  206. dev->out_urb_finished = 1;
  207. wake_up(&dev->write_wait);
  208. spin_unlock(&dev->buflock);
  209. exit:
  210. adu_debug_data(5, __func__, urb->actual_length,
  211. urb->transfer_buffer);
  212. }
  213. static int adu_open(struct inode *inode, struct file *file)
  214. {
  215. struct adu_device *dev = NULL;
  216. struct usb_interface *interface;
  217. int subminor;
  218. int retval;
  219. subminor = iminor(inode);
  220. retval = mutex_lock_interruptible(&adutux_mutex);
  221. if (retval)
  222. goto exit_no_lock;
  223. interface = usb_find_interface(&adu_driver, subminor);
  224. if (!interface) {
  225. printk(KERN_ERR "adutux: %s - error, can't find device for "
  226. "minor %d\n", __func__, subminor);
  227. retval = -ENODEV;
  228. goto exit_no_device;
  229. }
  230. dev = usb_get_intfdata(interface);
  231. if (!dev || !dev->udev) {
  232. retval = -ENODEV;
  233. goto exit_no_device;
  234. }
  235. /* check that nobody else is using the device */
  236. if (dev->open_count) {
  237. retval = -EBUSY;
  238. goto exit_no_device;
  239. }
  240. ++dev->open_count;
  241. dev_dbg(&dev->udev->dev, "%s: open count %d\n", __func__,
  242. dev->open_count);
  243. /* save device in the file's private structure */
  244. file->private_data = dev;
  245. /* initialize in direction */
  246. dev->read_buffer_length = 0;
  247. /* fixup first read by having urb waiting for it */
  248. usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
  249. usb_rcvintpipe(dev->udev,
  250. dev->interrupt_in_endpoint->bEndpointAddress),
  251. dev->interrupt_in_buffer,
  252. usb_endpoint_maxp(dev->interrupt_in_endpoint),
  253. adu_interrupt_in_callback, dev,
  254. dev->interrupt_in_endpoint->bInterval);
  255. dev->read_urb_finished = 0;
  256. if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL))
  257. dev->read_urb_finished = 1;
  258. /* we ignore failure */
  259. /* end of fixup for first read */
  260. /* initialize out direction */
  261. dev->out_urb_finished = 1;
  262. retval = 0;
  263. exit_no_device:
  264. mutex_unlock(&adutux_mutex);
  265. exit_no_lock:
  266. return retval;
  267. }
  268. static void adu_release_internal(struct adu_device *dev)
  269. {
  270. /* decrement our usage count for the device */
  271. --dev->open_count;
  272. dev_dbg(&dev->udev->dev, "%s : open count %d\n", __func__,
  273. dev->open_count);
  274. if (dev->open_count <= 0) {
  275. adu_abort_transfers(dev);
  276. dev->open_count = 0;
  277. }
  278. }
  279. static int adu_release(struct inode *inode, struct file *file)
  280. {
  281. struct adu_device *dev;
  282. int retval = 0;
  283. if (file == NULL) {
  284. retval = -ENODEV;
  285. goto exit;
  286. }
  287. dev = file->private_data;
  288. if (dev == NULL) {
  289. retval = -ENODEV;
  290. goto exit;
  291. }
  292. mutex_lock(&adutux_mutex); /* not interruptible */
  293. if (dev->open_count <= 0) {
  294. dev_dbg(&dev->udev->dev, "%s : device not opened\n", __func__);
  295. retval = -ENODEV;
  296. goto unlock;
  297. }
  298. adu_release_internal(dev);
  299. if (dev->udev == NULL) {
  300. /* the device was unplugged before the file was released */
  301. if (!dev->open_count) /* ... and we're the last user */
  302. adu_delete(dev);
  303. }
  304. unlock:
  305. mutex_unlock(&adutux_mutex);
  306. exit:
  307. return retval;
  308. }
  309. static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
  310. loff_t *ppos)
  311. {
  312. struct adu_device *dev;
  313. size_t bytes_read = 0;
  314. size_t bytes_to_read = count;
  315. int i;
  316. int retval = 0;
  317. int timeout = 0;
  318. int should_submit = 0;
  319. unsigned long flags;
  320. DECLARE_WAITQUEUE(wait, current);
  321. dev = file->private_data;
  322. if (mutex_lock_interruptible(&dev->mtx))
  323. return -ERESTARTSYS;
  324. /* verify that the device wasn't unplugged */
  325. if (dev->udev == NULL) {
  326. retval = -ENODEV;
  327. printk(KERN_ERR "adutux: No device or device unplugged %d\n",
  328. retval);
  329. goto exit;
  330. }
  331. /* verify that some data was requested */
  332. if (count == 0) {
  333. dev_dbg(&dev->udev->dev, "%s : read request of 0 bytes\n",
  334. __func__);
  335. goto exit;
  336. }
  337. timeout = COMMAND_TIMEOUT;
  338. dev_dbg(&dev->udev->dev, "%s : about to start looping\n", __func__);
  339. while (bytes_to_read) {
  340. int data_in_secondary = dev->secondary_tail - dev->secondary_head;
  341. dev_dbg(&dev->udev->dev,
  342. "%s : while, data_in_secondary=%d, status=%d\n",
  343. __func__, data_in_secondary,
  344. dev->interrupt_in_urb->status);
  345. if (data_in_secondary) {
  346. /* drain secondary buffer */
  347. int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary;
  348. i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount);
  349. if (i) {
  350. retval = -EFAULT;
  351. goto exit;
  352. }
  353. dev->secondary_head += (amount - i);
  354. bytes_read += (amount - i);
  355. bytes_to_read -= (amount - i);
  356. if (i) {
  357. retval = bytes_read ? bytes_read : -EFAULT;
  358. goto exit;
  359. }
  360. } else {
  361. /* we check the primary buffer */
  362. spin_lock_irqsave (&dev->buflock, flags);
  363. if (dev->read_buffer_length) {
  364. /* we secure access to the primary */
  365. char *tmp;
  366. dev_dbg(&dev->udev->dev,
  367. "%s : swap, read_buffer_length = %d\n",
  368. __func__, dev->read_buffer_length);
  369. tmp = dev->read_buffer_secondary;
  370. dev->read_buffer_secondary = dev->read_buffer_primary;
  371. dev->read_buffer_primary = tmp;
  372. dev->secondary_head = 0;
  373. dev->secondary_tail = dev->read_buffer_length;
  374. dev->read_buffer_length = 0;
  375. spin_unlock_irqrestore(&dev->buflock, flags);
  376. /* we have a free buffer so use it */
  377. should_submit = 1;
  378. } else {
  379. /* even the primary was empty - we may need to do IO */
  380. if (!dev->read_urb_finished) {
  381. /* somebody is doing IO */
  382. spin_unlock_irqrestore(&dev->buflock, flags);
  383. dev_dbg(&dev->udev->dev,
  384. "%s : submitted already\n",
  385. __func__);
  386. } else {
  387. /* we must initiate input */
  388. dev_dbg(&dev->udev->dev,
  389. "%s : initiate input\n",
  390. __func__);
  391. dev->read_urb_finished = 0;
  392. spin_unlock_irqrestore(&dev->buflock, flags);
  393. usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
  394. usb_rcvintpipe(dev->udev,
  395. dev->interrupt_in_endpoint->bEndpointAddress),
  396. dev->interrupt_in_buffer,
  397. usb_endpoint_maxp(dev->interrupt_in_endpoint),
  398. adu_interrupt_in_callback,
  399. dev,
  400. dev->interrupt_in_endpoint->bInterval);
  401. retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
  402. if (retval) {
  403. dev->read_urb_finished = 1;
  404. if (retval == -ENOMEM) {
  405. retval = bytes_read ? bytes_read : -ENOMEM;
  406. }
  407. dev_dbg(&dev->udev->dev,
  408. "%s : submit failed\n",
  409. __func__);
  410. goto exit;
  411. }
  412. }
  413. /* we wait for I/O to complete */
  414. set_current_state(TASK_INTERRUPTIBLE);
  415. add_wait_queue(&dev->read_wait, &wait);
  416. spin_lock_irqsave(&dev->buflock, flags);
  417. if (!dev->read_urb_finished) {
  418. spin_unlock_irqrestore(&dev->buflock, flags);
  419. timeout = schedule_timeout(COMMAND_TIMEOUT);
  420. } else {
  421. spin_unlock_irqrestore(&dev->buflock, flags);
  422. set_current_state(TASK_RUNNING);
  423. }
  424. remove_wait_queue(&dev->read_wait, &wait);
  425. if (timeout <= 0) {
  426. dev_dbg(&dev->udev->dev,
  427. "%s : timeout\n", __func__);
  428. retval = bytes_read ? bytes_read : -ETIMEDOUT;
  429. goto exit;
  430. }
  431. if (signal_pending(current)) {
  432. dev_dbg(&dev->udev->dev,
  433. "%s : signal pending\n",
  434. __func__);
  435. retval = bytes_read ? bytes_read : -EINTR;
  436. goto exit;
  437. }
  438. }
  439. }
  440. }
  441. retval = bytes_read;
  442. /* if the primary buffer is empty then use it */
  443. spin_lock_irqsave(&dev->buflock, flags);
  444. if (should_submit && dev->read_urb_finished) {
  445. dev->read_urb_finished = 0;
  446. spin_unlock_irqrestore(&dev->buflock, flags);
  447. usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
  448. usb_rcvintpipe(dev->udev,
  449. dev->interrupt_in_endpoint->bEndpointAddress),
  450. dev->interrupt_in_buffer,
  451. usb_endpoint_maxp(dev->interrupt_in_endpoint),
  452. adu_interrupt_in_callback,
  453. dev,
  454. dev->interrupt_in_endpoint->bInterval);
  455. if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0)
  456. dev->read_urb_finished = 1;
  457. /* we ignore failure */
  458. } else {
  459. spin_unlock_irqrestore(&dev->buflock, flags);
  460. }
  461. exit:
  462. /* unlock the device */
  463. mutex_unlock(&dev->mtx);
  464. return retval;
  465. }
  466. static ssize_t adu_write(struct file *file, const __user char *buffer,
  467. size_t count, loff_t *ppos)
  468. {
  469. DECLARE_WAITQUEUE(waita, current);
  470. struct adu_device *dev;
  471. size_t bytes_written = 0;
  472. size_t bytes_to_write;
  473. size_t buffer_size;
  474. unsigned long flags;
  475. int retval;
  476. dev = file->private_data;
  477. retval = mutex_lock_interruptible(&dev->mtx);
  478. if (retval)
  479. goto exit_nolock;
  480. /* verify that the device wasn't unplugged */
  481. if (dev->udev == NULL) {
  482. retval = -ENODEV;
  483. printk(KERN_ERR "adutux: No device or device unplugged %d\n",
  484. retval);
  485. goto exit;
  486. }
  487. /* verify that we actually have some data to write */
  488. if (count == 0) {
  489. dev_dbg(&dev->udev->dev, "%s : write request of 0 bytes\n",
  490. __func__);
  491. goto exit;
  492. }
  493. while (count > 0) {
  494. add_wait_queue(&dev->write_wait, &waita);
  495. set_current_state(TASK_INTERRUPTIBLE);
  496. spin_lock_irqsave(&dev->buflock, flags);
  497. if (!dev->out_urb_finished) {
  498. spin_unlock_irqrestore(&dev->buflock, flags);
  499. mutex_unlock(&dev->mtx);
  500. if (signal_pending(current)) {
  501. dev_dbg(&dev->udev->dev, "%s : interrupted\n",
  502. __func__);
  503. set_current_state(TASK_RUNNING);
  504. retval = -EINTR;
  505. goto exit_onqueue;
  506. }
  507. if (schedule_timeout(COMMAND_TIMEOUT) == 0) {
  508. dev_dbg(&dev->udev->dev,
  509. "%s - command timed out.\n", __func__);
  510. retval = -ETIMEDOUT;
  511. goto exit_onqueue;
  512. }
  513. remove_wait_queue(&dev->write_wait, &waita);
  514. retval = mutex_lock_interruptible(&dev->mtx);
  515. if (retval) {
  516. retval = bytes_written ? bytes_written : retval;
  517. goto exit_nolock;
  518. }
  519. dev_dbg(&dev->udev->dev,
  520. "%s : in progress, count = %Zd\n",
  521. __func__, count);
  522. } else {
  523. spin_unlock_irqrestore(&dev->buflock, flags);
  524. set_current_state(TASK_RUNNING);
  525. remove_wait_queue(&dev->write_wait, &waita);
  526. dev_dbg(&dev->udev->dev, "%s : sending, count = %Zd\n",
  527. __func__, count);
  528. /* write the data into interrupt_out_buffer from userspace */
  529. buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
  530. bytes_to_write = count > buffer_size ? buffer_size : count;
  531. dev_dbg(&dev->udev->dev,
  532. "%s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd\n",
  533. __func__, buffer_size, count, bytes_to_write);
  534. if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
  535. retval = -EFAULT;
  536. goto exit;
  537. }
  538. /* send off the urb */
  539. usb_fill_int_urb(
  540. dev->interrupt_out_urb,
  541. dev->udev,
  542. usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
  543. dev->interrupt_out_buffer,
  544. bytes_to_write,
  545. adu_interrupt_out_callback,
  546. dev,
  547. dev->interrupt_out_endpoint->bInterval);
  548. dev->interrupt_out_urb->actual_length = bytes_to_write;
  549. dev->out_urb_finished = 0;
  550. retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
  551. if (retval < 0) {
  552. dev->out_urb_finished = 1;
  553. dev_err(&dev->udev->dev, "Couldn't submit "
  554. "interrupt_out_urb %d\n", retval);
  555. goto exit;
  556. }
  557. buffer += bytes_to_write;
  558. count -= bytes_to_write;
  559. bytes_written += bytes_to_write;
  560. }
  561. }
  562. mutex_unlock(&dev->mtx);
  563. return bytes_written;
  564. exit:
  565. mutex_unlock(&dev->mtx);
  566. exit_nolock:
  567. return retval;
  568. exit_onqueue:
  569. remove_wait_queue(&dev->write_wait, &waita);
  570. return retval;
  571. }
  572. /* file operations needed when we register this driver */
  573. static const struct file_operations adu_fops = {
  574. .owner = THIS_MODULE,
  575. .read = adu_read,
  576. .write = adu_write,
  577. .open = adu_open,
  578. .release = adu_release,
  579. .llseek = noop_llseek,
  580. };
  581. /*
  582. * usb class driver info in order to get a minor number from the usb core,
  583. * and to have the device registered with devfs and the driver core
  584. */
  585. static struct usb_class_driver adu_class = {
  586. .name = "usb/adutux%d",
  587. .fops = &adu_fops,
  588. .minor_base = ADU_MINOR_BASE,
  589. };
  590. /**
  591. * adu_probe
  592. *
  593. * Called by the usb core when a new device is connected that it thinks
  594. * this driver might be interested in.
  595. */
  596. static int adu_probe(struct usb_interface *interface,
  597. const struct usb_device_id *id)
  598. {
  599. struct usb_device *udev = interface_to_usbdev(interface);
  600. struct adu_device *dev = NULL;
  601. struct usb_host_interface *iface_desc;
  602. struct usb_endpoint_descriptor *endpoint;
  603. int retval = -ENODEV;
  604. int in_end_size;
  605. int out_end_size;
  606. int i;
  607. if (udev == NULL) {
  608. dev_err(&interface->dev, "udev is NULL.\n");
  609. goto exit;
  610. }
  611. /* allocate memory for our device state and initialize it */
  612. dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
  613. if (dev == NULL) {
  614. dev_err(&interface->dev, "Out of memory\n");
  615. retval = -ENOMEM;
  616. goto exit;
  617. }
  618. mutex_init(&dev->mtx);
  619. spin_lock_init(&dev->buflock);
  620. dev->udev = udev;
  621. init_waitqueue_head(&dev->read_wait);
  622. init_waitqueue_head(&dev->write_wait);
  623. iface_desc = &interface->altsetting[0];
  624. /* set up the endpoint information */
  625. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  626. endpoint = &iface_desc->endpoint[i].desc;
  627. if (usb_endpoint_is_int_in(endpoint))
  628. dev->interrupt_in_endpoint = endpoint;
  629. if (usb_endpoint_is_int_out(endpoint))
  630. dev->interrupt_out_endpoint = endpoint;
  631. }
  632. if (dev->interrupt_in_endpoint == NULL) {
  633. dev_err(&interface->dev, "interrupt in endpoint not found\n");
  634. goto error;
  635. }
  636. if (dev->interrupt_out_endpoint == NULL) {
  637. dev_err(&interface->dev, "interrupt out endpoint not found\n");
  638. goto error;
  639. }
  640. in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
  641. out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
  642. dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
  643. if (!dev->read_buffer_primary) {
  644. dev_err(&interface->dev, "Couldn't allocate read_buffer_primary\n");
  645. retval = -ENOMEM;
  646. goto error;
  647. }
  648. /* debug code prime the buffer */
  649. memset(dev->read_buffer_primary, 'a', in_end_size);
  650. memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
  651. memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
  652. memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
  653. dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
  654. if (!dev->read_buffer_secondary) {
  655. dev_err(&interface->dev, "Couldn't allocate read_buffer_secondary\n");
  656. retval = -ENOMEM;
  657. goto error;
  658. }
  659. /* debug code prime the buffer */
  660. memset(dev->read_buffer_secondary, 'e', in_end_size);
  661. memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
  662. memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
  663. memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);
  664. dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
  665. if (!dev->interrupt_in_buffer) {
  666. dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
  667. goto error;
  668. }
  669. /* debug code prime the buffer */
  670. memset(dev->interrupt_in_buffer, 'i', in_end_size);
  671. dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  672. if (!dev->interrupt_in_urb) {
  673. dev_err(&interface->dev, "Couldn't allocate interrupt_in_urb\n");
  674. goto error;
  675. }
  676. dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
  677. if (!dev->interrupt_out_buffer) {
  678. dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
  679. goto error;
  680. }
  681. dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
  682. if (!dev->interrupt_out_urb) {
  683. dev_err(&interface->dev, "Couldn't allocate interrupt_out_urb\n");
  684. goto error;
  685. }
  686. if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
  687. sizeof(dev->serial_number))) {
  688. dev_err(&interface->dev, "Could not retrieve serial number\n");
  689. goto error;
  690. }
  691. dev_dbg(&interface->dev,"serial_number=%s", dev->serial_number);
  692. /* we can register the device now, as it is ready */
  693. usb_set_intfdata(interface, dev);
  694. retval = usb_register_dev(interface, &adu_class);
  695. if (retval) {
  696. /* something prevented us from registering this driver */
  697. dev_err(&interface->dev, "Not able to get a minor for this device.\n");
  698. usb_set_intfdata(interface, NULL);
  699. goto error;
  700. }
  701. dev->minor = interface->minor;
  702. /* let the user know what node this device is now attached to */
  703. dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n",
  704. udev->descriptor.idProduct, dev->serial_number,
  705. (dev->minor - ADU_MINOR_BASE));
  706. exit:
  707. return retval;
  708. error:
  709. adu_delete(dev);
  710. return retval;
  711. }
  712. /**
  713. * adu_disconnect
  714. *
  715. * Called by the usb core when the device is removed from the system.
  716. */
  717. static void adu_disconnect(struct usb_interface *interface)
  718. {
  719. struct adu_device *dev;
  720. int minor;
  721. dev = usb_get_intfdata(interface);
  722. mutex_lock(&dev->mtx); /* not interruptible */
  723. dev->udev = NULL; /* poison */
  724. minor = dev->minor;
  725. usb_deregister_dev(interface, &adu_class);
  726. mutex_unlock(&dev->mtx);
  727. mutex_lock(&adutux_mutex);
  728. usb_set_intfdata(interface, NULL);
  729. /* if the device is not opened, then we clean up right now */
  730. dev_dbg(&dev->udev->dev, "%s : open count %d\n",
  731. __func__, dev->open_count);
  732. if (!dev->open_count)
  733. adu_delete(dev);
  734. mutex_unlock(&adutux_mutex);
  735. dev_info(&interface->dev, "ADU device adutux%d now disconnected\n",
  736. (minor - ADU_MINOR_BASE));
  737. }
  738. /* usb specific object needed to register this driver with the usb subsystem */
  739. static struct usb_driver adu_driver = {
  740. .name = "adutux",
  741. .probe = adu_probe,
  742. .disconnect = adu_disconnect,
  743. .id_table = device_table,
  744. };
  745. module_usb_driver(adu_driver);
  746. MODULE_AUTHOR(DRIVER_AUTHOR);
  747. MODULE_DESCRIPTION(DRIVER_DESC);
  748. MODULE_LICENSE("GPL");