adutux.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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. dbg(1, " %s : udev is null", __func__);
  126. return;
  127. }
  128. /* shutdown transfer */
  129. /* XXX Anchor these instead */
  130. spin_lock_irqsave(&dev->buflock, flags);
  131. if (!dev->read_urb_finished) {
  132. spin_unlock_irqrestore(&dev->buflock, flags);
  133. usb_kill_urb(dev->interrupt_in_urb);
  134. } else
  135. spin_unlock_irqrestore(&dev->buflock, flags);
  136. spin_lock_irqsave(&dev->buflock, flags);
  137. if (!dev->out_urb_finished) {
  138. spin_unlock_irqrestore(&dev->buflock, flags);
  139. usb_kill_urb(dev->interrupt_out_urb);
  140. } else
  141. spin_unlock_irqrestore(&dev->buflock, flags);
  142. }
  143. static void adu_delete(struct adu_device *dev)
  144. {
  145. /* free data structures */
  146. usb_free_urb(dev->interrupt_in_urb);
  147. usb_free_urb(dev->interrupt_out_urb);
  148. kfree(dev->read_buffer_primary);
  149. kfree(dev->read_buffer_secondary);
  150. kfree(dev->interrupt_in_buffer);
  151. kfree(dev->interrupt_out_buffer);
  152. kfree(dev);
  153. }
  154. static void adu_interrupt_in_callback(struct urb *urb)
  155. {
  156. struct adu_device *dev = urb->context;
  157. int status = urb->status;
  158. adu_debug_data(5, __func__, urb->actual_length,
  159. urb->transfer_buffer);
  160. spin_lock(&dev->buflock);
  161. if (status != 0) {
  162. if ((status != -ENOENT) && (status != -ECONNRESET) &&
  163. (status != -ESHUTDOWN)) {
  164. dbg(1, " %s : nonzero status received: %d",
  165. __func__, status);
  166. }
  167. goto exit;
  168. }
  169. if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
  170. if (dev->read_buffer_length <
  171. (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) -
  172. (urb->actual_length)) {
  173. memcpy (dev->read_buffer_primary +
  174. dev->read_buffer_length,
  175. dev->interrupt_in_buffer, urb->actual_length);
  176. dev->read_buffer_length += urb->actual_length;
  177. dbg(2, " %s reading %d ", __func__,
  178. urb->actual_length);
  179. } else {
  180. dbg(1, " %s : read_buffer overflow", __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. dbg(1, " %s :nonzero status received: %d",
  200. __func__, status);
  201. }
  202. goto exit;
  203. }
  204. spin_lock(&dev->buflock);
  205. dev->out_urb_finished = 1;
  206. wake_up(&dev->write_wait);
  207. spin_unlock(&dev->buflock);
  208. exit:
  209. adu_debug_data(5, __func__, urb->actual_length,
  210. urb->transfer_buffer);
  211. }
  212. static int adu_open(struct inode *inode, struct file *file)
  213. {
  214. struct adu_device *dev = NULL;
  215. struct usb_interface *interface;
  216. int subminor;
  217. int retval;
  218. subminor = iminor(inode);
  219. retval = mutex_lock_interruptible(&adutux_mutex);
  220. if (retval) {
  221. dbg(2, "%s : mutex lock failed", __func__);
  222. goto exit_no_lock;
  223. }
  224. interface = usb_find_interface(&adu_driver, subminor);
  225. if (!interface) {
  226. printk(KERN_ERR "adutux: %s - error, can't find device for "
  227. "minor %d\n", __func__, subminor);
  228. retval = -ENODEV;
  229. goto exit_no_device;
  230. }
  231. dev = usb_get_intfdata(interface);
  232. if (!dev || !dev->udev) {
  233. retval = -ENODEV;
  234. goto exit_no_device;
  235. }
  236. /* check that nobody else is using the device */
  237. if (dev->open_count) {
  238. retval = -EBUSY;
  239. goto exit_no_device;
  240. }
  241. ++dev->open_count;
  242. dbg(2, "%s : open count %d", __func__, 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. dbg(2, " %s : open count %d", __func__, dev->open_count);
  273. if (dev->open_count <= 0) {
  274. adu_abort_transfers(dev);
  275. dev->open_count = 0;
  276. }
  277. }
  278. static int adu_release(struct inode *inode, struct file *file)
  279. {
  280. struct adu_device *dev;
  281. int retval = 0;
  282. if (file == NULL) {
  283. dbg(1, " %s : file is NULL", __func__);
  284. retval = -ENODEV;
  285. goto exit;
  286. }
  287. dev = file->private_data;
  288. if (dev == NULL) {
  289. dbg(1, " %s : object is NULL", __func__);
  290. retval = -ENODEV;
  291. goto exit;
  292. }
  293. mutex_lock(&adutux_mutex); /* not interruptible */
  294. if (dev->open_count <= 0) {
  295. dbg(1, " %s : device not opened", __func__);
  296. retval = -ENODEV;
  297. goto unlock;
  298. }
  299. adu_release_internal(dev);
  300. if (dev->udev == NULL) {
  301. /* the device was unplugged before the file was released */
  302. if (!dev->open_count) /* ... and we're the last user */
  303. adu_delete(dev);
  304. }
  305. unlock:
  306. mutex_unlock(&adutux_mutex);
  307. exit:
  308. return retval;
  309. }
  310. static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
  311. loff_t *ppos)
  312. {
  313. struct adu_device *dev;
  314. size_t bytes_read = 0;
  315. size_t bytes_to_read = count;
  316. int i;
  317. int retval = 0;
  318. int timeout = 0;
  319. int should_submit = 0;
  320. unsigned long flags;
  321. DECLARE_WAITQUEUE(wait, current);
  322. dev = file->private_data;
  323. if (mutex_lock_interruptible(&dev->mtx))
  324. return -ERESTARTSYS;
  325. /* verify that the device wasn't unplugged */
  326. if (dev->udev == NULL) {
  327. retval = -ENODEV;
  328. printk(KERN_ERR "adutux: No device or device unplugged %d\n",
  329. retval);
  330. goto exit;
  331. }
  332. /* verify that some data was requested */
  333. if (count == 0) {
  334. dbg(1, " %s : read request of 0 bytes", __func__);
  335. goto exit;
  336. }
  337. timeout = COMMAND_TIMEOUT;
  338. dbg(2, " %s : about to start looping", __func__);
  339. while (bytes_to_read) {
  340. int data_in_secondary = dev->secondary_tail - dev->secondary_head;
  341. dbg(2, " %s : while, data_in_secondary=%d, status=%d",
  342. __func__, data_in_secondary,
  343. dev->interrupt_in_urb->status);
  344. if (data_in_secondary) {
  345. /* drain secondary buffer */
  346. int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary;
  347. i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount);
  348. if (i) {
  349. retval = -EFAULT;
  350. goto exit;
  351. }
  352. dev->secondary_head += (amount - i);
  353. bytes_read += (amount - i);
  354. bytes_to_read -= (amount - i);
  355. if (i) {
  356. retval = bytes_read ? bytes_read : -EFAULT;
  357. goto exit;
  358. }
  359. } else {
  360. /* we check the primary buffer */
  361. spin_lock_irqsave (&dev->buflock, flags);
  362. if (dev->read_buffer_length) {
  363. /* we secure access to the primary */
  364. char *tmp;
  365. dbg(2, " %s : swap, read_buffer_length = %d",
  366. __func__, dev->read_buffer_length);
  367. tmp = dev->read_buffer_secondary;
  368. dev->read_buffer_secondary = dev->read_buffer_primary;
  369. dev->read_buffer_primary = tmp;
  370. dev->secondary_head = 0;
  371. dev->secondary_tail = dev->read_buffer_length;
  372. dev->read_buffer_length = 0;
  373. spin_unlock_irqrestore(&dev->buflock, flags);
  374. /* we have a free buffer so use it */
  375. should_submit = 1;
  376. } else {
  377. /* even the primary was empty - we may need to do IO */
  378. if (!dev->read_urb_finished) {
  379. /* somebody is doing IO */
  380. spin_unlock_irqrestore(&dev->buflock, flags);
  381. dbg(2, " %s : submitted already", __func__);
  382. } else {
  383. /* we must initiate input */
  384. dbg(2, " %s : initiate input", __func__);
  385. dev->read_urb_finished = 0;
  386. spin_unlock_irqrestore(&dev->buflock, flags);
  387. usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
  388. usb_rcvintpipe(dev->udev,
  389. dev->interrupt_in_endpoint->bEndpointAddress),
  390. dev->interrupt_in_buffer,
  391. usb_endpoint_maxp(dev->interrupt_in_endpoint),
  392. adu_interrupt_in_callback,
  393. dev,
  394. dev->interrupt_in_endpoint->bInterval);
  395. retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
  396. if (retval) {
  397. dev->read_urb_finished = 1;
  398. if (retval == -ENOMEM) {
  399. retval = bytes_read ? bytes_read : -ENOMEM;
  400. }
  401. dbg(2, " %s : submit failed", __func__);
  402. goto exit;
  403. }
  404. }
  405. /* we wait for I/O to complete */
  406. set_current_state(TASK_INTERRUPTIBLE);
  407. add_wait_queue(&dev->read_wait, &wait);
  408. spin_lock_irqsave(&dev->buflock, flags);
  409. if (!dev->read_urb_finished) {
  410. spin_unlock_irqrestore(&dev->buflock, flags);
  411. timeout = schedule_timeout(COMMAND_TIMEOUT);
  412. } else {
  413. spin_unlock_irqrestore(&dev->buflock, flags);
  414. set_current_state(TASK_RUNNING);
  415. }
  416. remove_wait_queue(&dev->read_wait, &wait);
  417. if (timeout <= 0) {
  418. dbg(2, " %s : timeout", __func__);
  419. retval = bytes_read ? bytes_read : -ETIMEDOUT;
  420. goto exit;
  421. }
  422. if (signal_pending(current)) {
  423. dbg(2, " %s : signal pending", __func__);
  424. retval = bytes_read ? bytes_read : -EINTR;
  425. goto exit;
  426. }
  427. }
  428. }
  429. }
  430. retval = bytes_read;
  431. /* if the primary buffer is empty then use it */
  432. spin_lock_irqsave(&dev->buflock, flags);
  433. if (should_submit && dev->read_urb_finished) {
  434. dev->read_urb_finished = 0;
  435. spin_unlock_irqrestore(&dev->buflock, flags);
  436. usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
  437. usb_rcvintpipe(dev->udev,
  438. dev->interrupt_in_endpoint->bEndpointAddress),
  439. dev->interrupt_in_buffer,
  440. usb_endpoint_maxp(dev->interrupt_in_endpoint),
  441. adu_interrupt_in_callback,
  442. dev,
  443. dev->interrupt_in_endpoint->bInterval);
  444. if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0)
  445. dev->read_urb_finished = 1;
  446. /* we ignore failure */
  447. } else {
  448. spin_unlock_irqrestore(&dev->buflock, flags);
  449. }
  450. exit:
  451. /* unlock the device */
  452. mutex_unlock(&dev->mtx);
  453. return retval;
  454. }
  455. static ssize_t adu_write(struct file *file, const __user char *buffer,
  456. size_t count, loff_t *ppos)
  457. {
  458. DECLARE_WAITQUEUE(waita, current);
  459. struct adu_device *dev;
  460. size_t bytes_written = 0;
  461. size_t bytes_to_write;
  462. size_t buffer_size;
  463. unsigned long flags;
  464. int retval;
  465. dev = file->private_data;
  466. retval = mutex_lock_interruptible(&dev->mtx);
  467. if (retval)
  468. goto exit_nolock;
  469. /* verify that the device wasn't unplugged */
  470. if (dev->udev == NULL) {
  471. retval = -ENODEV;
  472. printk(KERN_ERR "adutux: No device or device unplugged %d\n",
  473. retval);
  474. goto exit;
  475. }
  476. /* verify that we actually have some data to write */
  477. if (count == 0) {
  478. dbg(1, " %s : write request of 0 bytes", __func__);
  479. goto exit;
  480. }
  481. while (count > 0) {
  482. add_wait_queue(&dev->write_wait, &waita);
  483. set_current_state(TASK_INTERRUPTIBLE);
  484. spin_lock_irqsave(&dev->buflock, flags);
  485. if (!dev->out_urb_finished) {
  486. spin_unlock_irqrestore(&dev->buflock, flags);
  487. mutex_unlock(&dev->mtx);
  488. if (signal_pending(current)) {
  489. dbg(1, " %s : interrupted", __func__);
  490. set_current_state(TASK_RUNNING);
  491. retval = -EINTR;
  492. goto exit_onqueue;
  493. }
  494. if (schedule_timeout(COMMAND_TIMEOUT) == 0) {
  495. dbg(1, "%s - command timed out.", __func__);
  496. retval = -ETIMEDOUT;
  497. goto exit_onqueue;
  498. }
  499. remove_wait_queue(&dev->write_wait, &waita);
  500. retval = mutex_lock_interruptible(&dev->mtx);
  501. if (retval) {
  502. retval = bytes_written ? bytes_written : retval;
  503. goto exit_nolock;
  504. }
  505. dbg(4, " %s : in progress, count = %Zd", __func__, count);
  506. } else {
  507. spin_unlock_irqrestore(&dev->buflock, flags);
  508. set_current_state(TASK_RUNNING);
  509. remove_wait_queue(&dev->write_wait, &waita);
  510. dbg(4, " %s : sending, count = %Zd", __func__, count);
  511. /* write the data into interrupt_out_buffer from userspace */
  512. buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
  513. bytes_to_write = count > buffer_size ? buffer_size : count;
  514. dbg(4, " %s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd",
  515. __func__, buffer_size, count, bytes_to_write);
  516. if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
  517. retval = -EFAULT;
  518. goto exit;
  519. }
  520. /* send off the urb */
  521. usb_fill_int_urb(
  522. dev->interrupt_out_urb,
  523. dev->udev,
  524. usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
  525. dev->interrupt_out_buffer,
  526. bytes_to_write,
  527. adu_interrupt_out_callback,
  528. dev,
  529. dev->interrupt_out_endpoint->bInterval);
  530. dev->interrupt_out_urb->actual_length = bytes_to_write;
  531. dev->out_urb_finished = 0;
  532. retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
  533. if (retval < 0) {
  534. dev->out_urb_finished = 1;
  535. dev_err(&dev->udev->dev, "Couldn't submit "
  536. "interrupt_out_urb %d\n", retval);
  537. goto exit;
  538. }
  539. buffer += bytes_to_write;
  540. count -= bytes_to_write;
  541. bytes_written += bytes_to_write;
  542. }
  543. }
  544. mutex_unlock(&dev->mtx);
  545. return bytes_written;
  546. exit:
  547. mutex_unlock(&dev->mtx);
  548. exit_nolock:
  549. return retval;
  550. exit_onqueue:
  551. remove_wait_queue(&dev->write_wait, &waita);
  552. return retval;
  553. }
  554. /* file operations needed when we register this driver */
  555. static const struct file_operations adu_fops = {
  556. .owner = THIS_MODULE,
  557. .read = adu_read,
  558. .write = adu_write,
  559. .open = adu_open,
  560. .release = adu_release,
  561. .llseek = noop_llseek,
  562. };
  563. /*
  564. * usb class driver info in order to get a minor number from the usb core,
  565. * and to have the device registered with devfs and the driver core
  566. */
  567. static struct usb_class_driver adu_class = {
  568. .name = "usb/adutux%d",
  569. .fops = &adu_fops,
  570. .minor_base = ADU_MINOR_BASE,
  571. };
  572. /**
  573. * adu_probe
  574. *
  575. * Called by the usb core when a new device is connected that it thinks
  576. * this driver might be interested in.
  577. */
  578. static int adu_probe(struct usb_interface *interface,
  579. const struct usb_device_id *id)
  580. {
  581. struct usb_device *udev = interface_to_usbdev(interface);
  582. struct adu_device *dev = NULL;
  583. struct usb_host_interface *iface_desc;
  584. struct usb_endpoint_descriptor *endpoint;
  585. int retval = -ENODEV;
  586. int in_end_size;
  587. int out_end_size;
  588. int i;
  589. if (udev == NULL) {
  590. dev_err(&interface->dev, "udev is NULL.\n");
  591. goto exit;
  592. }
  593. /* allocate memory for our device state and initialize it */
  594. dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
  595. if (dev == NULL) {
  596. dev_err(&interface->dev, "Out of memory\n");
  597. retval = -ENOMEM;
  598. goto exit;
  599. }
  600. mutex_init(&dev->mtx);
  601. spin_lock_init(&dev->buflock);
  602. dev->udev = udev;
  603. init_waitqueue_head(&dev->read_wait);
  604. init_waitqueue_head(&dev->write_wait);
  605. iface_desc = &interface->altsetting[0];
  606. /* set up the endpoint information */
  607. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  608. endpoint = &iface_desc->endpoint[i].desc;
  609. if (usb_endpoint_is_int_in(endpoint))
  610. dev->interrupt_in_endpoint = endpoint;
  611. if (usb_endpoint_is_int_out(endpoint))
  612. dev->interrupt_out_endpoint = endpoint;
  613. }
  614. if (dev->interrupt_in_endpoint == NULL) {
  615. dev_err(&interface->dev, "interrupt in endpoint not found\n");
  616. goto error;
  617. }
  618. if (dev->interrupt_out_endpoint == NULL) {
  619. dev_err(&interface->dev, "interrupt out endpoint not found\n");
  620. goto error;
  621. }
  622. in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
  623. out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
  624. dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
  625. if (!dev->read_buffer_primary) {
  626. dev_err(&interface->dev, "Couldn't allocate read_buffer_primary\n");
  627. retval = -ENOMEM;
  628. goto error;
  629. }
  630. /* debug code prime the buffer */
  631. memset(dev->read_buffer_primary, 'a', in_end_size);
  632. memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
  633. memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
  634. memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
  635. dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
  636. if (!dev->read_buffer_secondary) {
  637. dev_err(&interface->dev, "Couldn't allocate read_buffer_secondary\n");
  638. retval = -ENOMEM;
  639. goto error;
  640. }
  641. /* debug code prime the buffer */
  642. memset(dev->read_buffer_secondary, 'e', in_end_size);
  643. memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
  644. memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
  645. memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);
  646. dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
  647. if (!dev->interrupt_in_buffer) {
  648. dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
  649. goto error;
  650. }
  651. /* debug code prime the buffer */
  652. memset(dev->interrupt_in_buffer, 'i', in_end_size);
  653. dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  654. if (!dev->interrupt_in_urb) {
  655. dev_err(&interface->dev, "Couldn't allocate interrupt_in_urb\n");
  656. goto error;
  657. }
  658. dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
  659. if (!dev->interrupt_out_buffer) {
  660. dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
  661. goto error;
  662. }
  663. dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
  664. if (!dev->interrupt_out_urb) {
  665. dev_err(&interface->dev, "Couldn't allocate interrupt_out_urb\n");
  666. goto error;
  667. }
  668. if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
  669. sizeof(dev->serial_number))) {
  670. dev_err(&interface->dev, "Could not retrieve serial number\n");
  671. goto error;
  672. }
  673. dbg(2, " %s : serial_number=%s", __func__, dev->serial_number);
  674. /* we can register the device now, as it is ready */
  675. usb_set_intfdata(interface, dev);
  676. retval = usb_register_dev(interface, &adu_class);
  677. if (retval) {
  678. /* something prevented us from registering this driver */
  679. dev_err(&interface->dev, "Not able to get a minor for this device.\n");
  680. usb_set_intfdata(interface, NULL);
  681. goto error;
  682. }
  683. dev->minor = interface->minor;
  684. /* let the user know what node this device is now attached to */
  685. dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n",
  686. udev->descriptor.idProduct, dev->serial_number,
  687. (dev->minor - ADU_MINOR_BASE));
  688. exit:
  689. return retval;
  690. error:
  691. adu_delete(dev);
  692. return retval;
  693. }
  694. /**
  695. * adu_disconnect
  696. *
  697. * Called by the usb core when the device is removed from the system.
  698. */
  699. static void adu_disconnect(struct usb_interface *interface)
  700. {
  701. struct adu_device *dev;
  702. int minor;
  703. dev = usb_get_intfdata(interface);
  704. mutex_lock(&dev->mtx); /* not interruptible */
  705. dev->udev = NULL; /* poison */
  706. minor = dev->minor;
  707. usb_deregister_dev(interface, &adu_class);
  708. mutex_unlock(&dev->mtx);
  709. mutex_lock(&adutux_mutex);
  710. usb_set_intfdata(interface, NULL);
  711. /* if the device is not opened, then we clean up right now */
  712. dbg(2, " %s : open count %d", __func__, dev->open_count);
  713. if (!dev->open_count)
  714. adu_delete(dev);
  715. mutex_unlock(&adutux_mutex);
  716. dev_info(&interface->dev, "ADU device adutux%d now disconnected\n",
  717. (minor - ADU_MINOR_BASE));
  718. }
  719. /* usb specific object needed to register this driver with the usb subsystem */
  720. static struct usb_driver adu_driver = {
  721. .name = "adutux",
  722. .probe = adu_probe,
  723. .disconnect = adu_disconnect,
  724. .id_table = device_table,
  725. };
  726. module_usb_driver(adu_driver);
  727. MODULE_AUTHOR(DRIVER_AUTHOR);
  728. MODULE_DESCRIPTION(DRIVER_DESC);
  729. MODULE_LICENSE("GPL");