adutux.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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 <asm/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 __FILE__ " : " format " \n", ## 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 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. /* Structure to hold all of our device specific stuff */
  70. struct adu_device {
  71. struct mutex mtx; /* locks this structure */
  72. struct usb_device* udev; /* save off the usb device pointer */
  73. struct usb_interface* interface;
  74. unsigned char minor; /* the starting minor number for this device */
  75. char serial_number[8];
  76. int open_count; /* number of times this port has been opened */
  77. char* read_buffer_primary;
  78. int read_buffer_length;
  79. char* read_buffer_secondary;
  80. int secondary_head;
  81. int secondary_tail;
  82. spinlock_t buflock;
  83. wait_queue_head_t read_wait;
  84. wait_queue_head_t write_wait;
  85. char* interrupt_in_buffer;
  86. struct usb_endpoint_descriptor* interrupt_in_endpoint;
  87. struct urb* interrupt_in_urb;
  88. int read_urb_finished;
  89. char* interrupt_out_buffer;
  90. struct usb_endpoint_descriptor* interrupt_out_endpoint;
  91. struct urb* interrupt_out_urb;
  92. };
  93. static struct usb_driver adu_driver;
  94. static void adu_debug_data(int level, const char *function, int size,
  95. const unsigned char *data)
  96. {
  97. int i;
  98. if (debug < level)
  99. return;
  100. printk(KERN_DEBUG __FILE__": %s - length = %d, data = ",
  101. function, size);
  102. for (i = 0; i < size; ++i)
  103. printk("%.2x ", data[i]);
  104. printk("\n");
  105. }
  106. /**
  107. * adu_abort_transfers
  108. * aborts transfers and frees associated data structures
  109. */
  110. static void adu_abort_transfers(struct adu_device *dev)
  111. {
  112. dbg(2," %s : enter", __FUNCTION__);
  113. if (dev == NULL) {
  114. dbg(1," %s : dev is null", __FUNCTION__);
  115. goto exit;
  116. }
  117. if (dev->udev == NULL) {
  118. dbg(1," %s : udev is null", __FUNCTION__);
  119. goto exit;
  120. }
  121. dbg(2," %s : udev state %d", __FUNCTION__, dev->udev->state);
  122. if (dev->udev->state == USB_STATE_NOTATTACHED) {
  123. dbg(1," %s : udev is not attached", __FUNCTION__);
  124. goto exit;
  125. }
  126. /* shutdown transfer */
  127. usb_unlink_urb(dev->interrupt_in_urb);
  128. usb_unlink_urb(dev->interrupt_out_urb);
  129. exit:
  130. dbg(2," %s : leave", __FUNCTION__);
  131. }
  132. static void adu_delete(struct adu_device *dev)
  133. {
  134. dbg(2, "%s enter", __FUNCTION__);
  135. adu_abort_transfers(dev);
  136. /* free data structures */
  137. usb_free_urb(dev->interrupt_in_urb);
  138. usb_free_urb(dev->interrupt_out_urb);
  139. kfree(dev->read_buffer_primary);
  140. kfree(dev->read_buffer_secondary);
  141. kfree(dev->interrupt_in_buffer);
  142. kfree(dev->interrupt_out_buffer);
  143. kfree(dev);
  144. dbg(2, "%s : leave", __FUNCTION__);
  145. }
  146. static void adu_interrupt_in_callback(struct urb *urb)
  147. {
  148. struct adu_device *dev = urb->context;
  149. int status = urb->status;
  150. dbg(4," %s : enter, status %d", __FUNCTION__, status);
  151. adu_debug_data(5, __FUNCTION__, urb->actual_length,
  152. urb->transfer_buffer);
  153. spin_lock(&dev->buflock);
  154. if (status != 0) {
  155. if ((status != -ENOENT) && (status != -ECONNRESET) &&
  156. (status != -ESHUTDOWN)) {
  157. dbg(1," %s : nonzero status received: %d",
  158. __FUNCTION__, status);
  159. }
  160. goto exit;
  161. }
  162. if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
  163. if (dev->read_buffer_length <
  164. (4 * le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize)) -
  165. (urb->actual_length)) {
  166. memcpy (dev->read_buffer_primary +
  167. dev->read_buffer_length,
  168. dev->interrupt_in_buffer, urb->actual_length);
  169. dev->read_buffer_length += urb->actual_length;
  170. dbg(2," %s reading %d ", __FUNCTION__,
  171. urb->actual_length);
  172. } else {
  173. dbg(1," %s : read_buffer overflow", __FUNCTION__);
  174. }
  175. }
  176. exit:
  177. dev->read_urb_finished = 1;
  178. spin_unlock(&dev->buflock);
  179. /* always wake up so we recover from errors */
  180. wake_up_interruptible(&dev->read_wait);
  181. adu_debug_data(5, __FUNCTION__, urb->actual_length,
  182. urb->transfer_buffer);
  183. dbg(4," %s : leave, status %d", __FUNCTION__, status);
  184. }
  185. static void adu_interrupt_out_callback(struct urb *urb)
  186. {
  187. struct adu_device *dev = urb->context;
  188. int status = urb->status;
  189. dbg(4," %s : enter, status %d", __FUNCTION__, status);
  190. adu_debug_data(5,__FUNCTION__, urb->actual_length, urb->transfer_buffer);
  191. if (status != 0) {
  192. if ((status != -ENOENT) &&
  193. (status != -ECONNRESET)) {
  194. dbg(1, " %s :nonzero status received: %d",
  195. __FUNCTION__, status);
  196. }
  197. goto exit;
  198. }
  199. wake_up_interruptible(&dev->write_wait);
  200. exit:
  201. adu_debug_data(5, __FUNCTION__, urb->actual_length,
  202. urb->transfer_buffer);
  203. dbg(4," %s : leave, status %d", __FUNCTION__, status);
  204. }
  205. static int adu_open(struct inode *inode, struct file *file)
  206. {
  207. struct adu_device *dev = NULL;
  208. struct usb_interface *interface;
  209. int subminor;
  210. int retval = 0;
  211. dbg(2,"%s : enter", __FUNCTION__);
  212. subminor = iminor(inode);
  213. interface = usb_find_interface(&adu_driver, subminor);
  214. if (!interface) {
  215. err("%s - error, can't find device for minor %d",
  216. __FUNCTION__, subminor);
  217. retval = -ENODEV;
  218. goto exit_no_device;
  219. }
  220. dev = usb_get_intfdata(interface);
  221. if (!dev) {
  222. retval = -ENODEV;
  223. goto exit_no_device;
  224. }
  225. /* lock this device */
  226. if ((retval = mutex_lock_interruptible(&dev->mtx))) {
  227. dbg(2, "%s : mutex lock failed", __FUNCTION__);
  228. goto exit_no_device;
  229. }
  230. /* increment our usage count for the device */
  231. ++dev->open_count;
  232. dbg(2,"%s : open count %d", __FUNCTION__, dev->open_count);
  233. /* save device in the file's private structure */
  234. file->private_data = dev;
  235. if (dev->open_count == 1) {
  236. /* initialize in direction */
  237. dev->read_buffer_length = 0;
  238. /* fixup first read by having urb waiting for it */
  239. usb_fill_int_urb(dev->interrupt_in_urb,dev->udev,
  240. usb_rcvintpipe(dev->udev,
  241. dev->interrupt_in_endpoint->bEndpointAddress),
  242. dev->interrupt_in_buffer,
  243. le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
  244. adu_interrupt_in_callback, dev,
  245. dev->interrupt_in_endpoint->bInterval);
  246. /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */
  247. dev->read_urb_finished = 0;
  248. retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
  249. if (retval)
  250. --dev->open_count;
  251. }
  252. mutex_unlock(&dev->mtx);
  253. exit_no_device:
  254. dbg(2,"%s : leave, return value %d ", __FUNCTION__, retval);
  255. return retval;
  256. }
  257. static int adu_release_internal(struct adu_device *dev)
  258. {
  259. int retval = 0;
  260. dbg(2," %s : enter", __FUNCTION__);
  261. /* decrement our usage count for the device */
  262. --dev->open_count;
  263. dbg(2," %s : open count %d", __FUNCTION__, dev->open_count);
  264. if (dev->open_count <= 0) {
  265. adu_abort_transfers(dev);
  266. dev->open_count = 0;
  267. }
  268. dbg(2," %s : leave", __FUNCTION__);
  269. return retval;
  270. }
  271. static int adu_release(struct inode *inode, struct file *file)
  272. {
  273. struct adu_device *dev = NULL;
  274. int retval = 0;
  275. dbg(2," %s : enter", __FUNCTION__);
  276. if (file == NULL) {
  277. dbg(1," %s : file is NULL", __FUNCTION__);
  278. retval = -ENODEV;
  279. goto exit;
  280. }
  281. dev = file->private_data;
  282. if (dev == NULL) {
  283. dbg(1," %s : object is NULL", __FUNCTION__);
  284. retval = -ENODEV;
  285. goto exit;
  286. }
  287. /* lock our device */
  288. mutex_lock(&dev->mtx); /* not interruptible */
  289. if (dev->open_count <= 0) {
  290. dbg(1," %s : device not opened", __FUNCTION__);
  291. retval = -ENODEV;
  292. goto exit;
  293. }
  294. if (dev->udev == NULL) {
  295. /* the device was unplugged before the file was released */
  296. mutex_unlock(&dev->mtx);
  297. adu_delete(dev);
  298. dev = NULL;
  299. } else {
  300. /* do the work */
  301. retval = adu_release_internal(dev);
  302. }
  303. exit:
  304. if (dev)
  305. mutex_unlock(&dev->mtx);
  306. dbg(2," %s : leave, return value %d", __FUNCTION__, retval);
  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. dbg(2," %s : enter, count = %Zd, file=%p", __FUNCTION__, count, file);
  322. dev = file->private_data;
  323. dbg(2," %s : dev=%p", __FUNCTION__, dev);
  324. /* lock this object */
  325. if (mutex_lock_interruptible(&dev->mtx))
  326. return -ERESTARTSYS;
  327. /* verify that the device wasn't unplugged */
  328. if (dev->udev == NULL || dev->minor == 0) {
  329. retval = -ENODEV;
  330. err("No device or device unplugged %d", retval);
  331. goto exit;
  332. }
  333. /* verify that some data was requested */
  334. if (count == 0) {
  335. dbg(1," %s : read request of 0 bytes", __FUNCTION__);
  336. goto exit;
  337. }
  338. timeout = COMMAND_TIMEOUT;
  339. dbg(2," %s : about to start looping", __FUNCTION__);
  340. while (bytes_to_read) {
  341. int data_in_secondary = dev->secondary_tail - dev->secondary_head;
  342. dbg(2," %s : while, data_in_secondary=%d, status=%d",
  343. __FUNCTION__, 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 < 0) {
  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. dbg(2," %s : swap, read_buffer_length = %d",
  367. __FUNCTION__, dev->read_buffer_length);
  368. tmp = dev->read_buffer_secondary;
  369. dev->read_buffer_secondary = dev->read_buffer_primary;
  370. dev->read_buffer_primary = tmp;
  371. dev->secondary_head = 0;
  372. dev->secondary_tail = dev->read_buffer_length;
  373. dev->read_buffer_length = 0;
  374. spin_unlock_irqrestore(&dev->buflock, flags);
  375. /* we have a free buffer so use it */
  376. should_submit = 1;
  377. } else {
  378. /* even the primary was empty - we may need to do IO */
  379. if (dev->interrupt_in_urb->status == -EINPROGRESS) {
  380. /* somebody is doing IO */
  381. spin_unlock_irqrestore(&dev->buflock, flags);
  382. dbg(2," %s : submitted already", __FUNCTION__);
  383. } else {
  384. /* we must initiate input */
  385. dbg(2," %s : initiate input", __FUNCTION__);
  386. dev->read_urb_finished = 0;
  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. le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
  392. adu_interrupt_in_callback,
  393. dev,
  394. dev->interrupt_in_endpoint->bInterval);
  395. retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
  396. if (!retval) {
  397. spin_unlock_irqrestore(&dev->buflock, flags);
  398. dbg(2," %s : submitted OK", __FUNCTION__);
  399. } else {
  400. if (retval == -ENOMEM) {
  401. retval = bytes_read ? bytes_read : -ENOMEM;
  402. }
  403. spin_unlock_irqrestore(&dev->buflock, flags);
  404. dbg(2," %s : submit failed", __FUNCTION__);
  405. goto exit;
  406. }
  407. }
  408. /* we wait for I/O to complete */
  409. set_current_state(TASK_INTERRUPTIBLE);
  410. add_wait_queue(&dev->read_wait, &wait);
  411. if (!dev->read_urb_finished)
  412. timeout = schedule_timeout(COMMAND_TIMEOUT);
  413. else
  414. set_current_state(TASK_RUNNING);
  415. remove_wait_queue(&dev->read_wait, &wait);
  416. if (timeout <= 0) {
  417. dbg(2," %s : timeout", __FUNCTION__);
  418. retval = bytes_read ? bytes_read : -ETIMEDOUT;
  419. goto exit;
  420. }
  421. if (signal_pending(current)) {
  422. dbg(2," %s : signal pending", __FUNCTION__);
  423. retval = bytes_read ? bytes_read : -EINTR;
  424. goto exit;
  425. }
  426. }
  427. }
  428. }
  429. retval = bytes_read;
  430. /* if the primary buffer is empty then use it */
  431. if (should_submit && !dev->interrupt_in_urb->status==-EINPROGRESS) {
  432. usb_fill_int_urb(dev->interrupt_in_urb,dev->udev,
  433. usb_rcvintpipe(dev->udev,
  434. dev->interrupt_in_endpoint->bEndpointAddress),
  435. dev->interrupt_in_buffer,
  436. le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
  437. adu_interrupt_in_callback,
  438. dev,
  439. dev->interrupt_in_endpoint->bInterval);
  440. /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */
  441. dev->read_urb_finished = 0;
  442. usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
  443. /* we ignore failure */
  444. }
  445. exit:
  446. /* unlock the device */
  447. mutex_unlock(&dev->mtx);
  448. dbg(2," %s : leave, return value %d", __FUNCTION__, retval);
  449. return retval;
  450. }
  451. static ssize_t adu_write(struct file *file, const __user char *buffer,
  452. size_t count, loff_t *ppos)
  453. {
  454. struct adu_device *dev;
  455. size_t bytes_written = 0;
  456. size_t bytes_to_write;
  457. size_t buffer_size;
  458. int retval;
  459. int timeout = 0;
  460. dbg(2," %s : enter, count = %Zd", __FUNCTION__, count);
  461. dev = file->private_data;
  462. /* lock this object */
  463. retval = mutex_lock_interruptible(&dev->mtx);
  464. if (retval)
  465. goto exit_nolock;
  466. /* verify that the device wasn't unplugged */
  467. if (dev->udev == NULL || dev->minor == 0) {
  468. retval = -ENODEV;
  469. err("No device or device unplugged %d", retval);
  470. goto exit;
  471. }
  472. /* verify that we actually have some data to write */
  473. if (count == 0) {
  474. dbg(1," %s : write request of 0 bytes", __FUNCTION__);
  475. goto exit;
  476. }
  477. while (count > 0) {
  478. if (dev->interrupt_out_urb->status == -EINPROGRESS) {
  479. timeout = COMMAND_TIMEOUT;
  480. while (timeout > 0) {
  481. if (signal_pending(current)) {
  482. dbg(1," %s : interrupted", __FUNCTION__);
  483. retval = -EINTR;
  484. goto exit;
  485. }
  486. mutex_unlock(&dev->mtx);
  487. timeout = interruptible_sleep_on_timeout(&dev->write_wait, timeout);
  488. retval = mutex_lock_interruptible(&dev->mtx);
  489. if (retval) {
  490. retval = bytes_written ? bytes_written : retval;
  491. goto exit_nolock;
  492. }
  493. if (timeout > 0) {
  494. break;
  495. }
  496. dbg(1," %s : interrupted timeout: %d", __FUNCTION__, timeout);
  497. }
  498. dbg(1," %s : final timeout: %d", __FUNCTION__, timeout);
  499. if (timeout == 0) {
  500. dbg(1, "%s - command timed out.", __FUNCTION__);
  501. retval = -ETIMEDOUT;
  502. goto exit;
  503. }
  504. dbg(4," %s : in progress, count = %Zd", __FUNCTION__, count);
  505. } else {
  506. dbg(4," %s : sending, count = %Zd", __FUNCTION__, count);
  507. /* write the data into interrupt_out_buffer from userspace */
  508. buffer_size = le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize);
  509. bytes_to_write = count > buffer_size ? buffer_size : count;
  510. dbg(4," %s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd",
  511. __FUNCTION__, buffer_size, count, bytes_to_write);
  512. if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
  513. retval = -EFAULT;
  514. goto exit;
  515. }
  516. /* send off the urb */
  517. usb_fill_int_urb(
  518. dev->interrupt_out_urb,
  519. dev->udev,
  520. usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
  521. dev->interrupt_out_buffer,
  522. bytes_to_write,
  523. adu_interrupt_out_callback,
  524. dev,
  525. dev->interrupt_in_endpoint->bInterval);
  526. /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */
  527. dev->interrupt_out_urb->actual_length = bytes_to_write;
  528. retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
  529. if (retval < 0) {
  530. err("Couldn't submit interrupt_out_urb %d", retval);
  531. goto exit;
  532. }
  533. buffer += bytes_to_write;
  534. count -= bytes_to_write;
  535. bytes_written += bytes_to_write;
  536. }
  537. }
  538. retval = bytes_written;
  539. exit:
  540. /* unlock the device */
  541. mutex_unlock(&dev->mtx);
  542. exit_nolock:
  543. dbg(2," %s : leave, return value %d", __FUNCTION__, retval);
  544. return retval;
  545. }
  546. /* file operations needed when we register this driver */
  547. static const struct file_operations adu_fops = {
  548. .owner = THIS_MODULE,
  549. .read = adu_read,
  550. .write = adu_write,
  551. .open = adu_open,
  552. .release = adu_release,
  553. };
  554. /*
  555. * usb class driver info in order to get a minor number from the usb core,
  556. * and to have the device registered with devfs and the driver core
  557. */
  558. static struct usb_class_driver adu_class = {
  559. .name = "usb/adutux%d",
  560. .fops = &adu_fops,
  561. .minor_base = ADU_MINOR_BASE,
  562. };
  563. /**
  564. * adu_probe
  565. *
  566. * Called by the usb core when a new device is connected that it thinks
  567. * this driver might be interested in.
  568. */
  569. static int adu_probe(struct usb_interface *interface,
  570. const struct usb_device_id *id)
  571. {
  572. struct usb_device *udev = interface_to_usbdev(interface);
  573. struct adu_device *dev = NULL;
  574. struct usb_host_interface *iface_desc;
  575. struct usb_endpoint_descriptor *endpoint;
  576. int retval = -ENODEV;
  577. int in_end_size;
  578. int out_end_size;
  579. int i;
  580. dbg(2," %s : enter", __FUNCTION__);
  581. if (udev == NULL) {
  582. dev_err(&interface->dev, "udev is NULL.\n");
  583. goto exit;
  584. }
  585. /* allocate memory for our device state and intialize it */
  586. dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
  587. if (dev == NULL) {
  588. dev_err(&interface->dev, "Out of memory\n");
  589. retval = -ENOMEM;
  590. goto exit;
  591. }
  592. mutex_init(&dev->mtx);
  593. spin_lock_init(&dev->buflock);
  594. dev->udev = udev;
  595. init_waitqueue_head(&dev->read_wait);
  596. init_waitqueue_head(&dev->write_wait);
  597. iface_desc = &interface->altsetting[0];
  598. /* set up the endpoint information */
  599. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  600. endpoint = &iface_desc->endpoint[i].desc;
  601. if (usb_endpoint_is_int_in(endpoint))
  602. dev->interrupt_in_endpoint = endpoint;
  603. if (usb_endpoint_is_int_out(endpoint))
  604. dev->interrupt_out_endpoint = endpoint;
  605. }
  606. if (dev->interrupt_in_endpoint == NULL) {
  607. dev_err(&interface->dev, "interrupt in endpoint not found\n");
  608. goto error;
  609. }
  610. if (dev->interrupt_out_endpoint == NULL) {
  611. dev_err(&interface->dev, "interrupt out endpoint not found\n");
  612. goto error;
  613. }
  614. in_end_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
  615. out_end_size = le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize);
  616. dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
  617. if (!dev->read_buffer_primary) {
  618. dev_err(&interface->dev, "Couldn't allocate read_buffer_primary\n");
  619. retval = -ENOMEM;
  620. goto error;
  621. }
  622. /* debug code prime the buffer */
  623. memset(dev->read_buffer_primary, 'a', in_end_size);
  624. memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
  625. memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
  626. memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
  627. dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
  628. if (!dev->read_buffer_secondary) {
  629. dev_err(&interface->dev, "Couldn't allocate read_buffer_secondary\n");
  630. retval = -ENOMEM;
  631. goto error;
  632. }
  633. /* debug code prime the buffer */
  634. memset(dev->read_buffer_secondary, 'e', in_end_size);
  635. memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
  636. memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
  637. memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);
  638. dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
  639. if (!dev->interrupt_in_buffer) {
  640. dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
  641. goto error;
  642. }
  643. /* debug code prime the buffer */
  644. memset(dev->interrupt_in_buffer, 'i', in_end_size);
  645. dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  646. if (!dev->interrupt_in_urb) {
  647. dev_err(&interface->dev, "Couldn't allocate interrupt_in_urb\n");
  648. goto error;
  649. }
  650. dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
  651. if (!dev->interrupt_out_buffer) {
  652. dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
  653. goto error;
  654. }
  655. dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
  656. if (!dev->interrupt_out_urb) {
  657. dev_err(&interface->dev, "Couldn't allocate interrupt_out_urb\n");
  658. goto error;
  659. }
  660. if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
  661. sizeof(dev->serial_number))) {
  662. dev_err(&interface->dev, "Could not retrieve serial number\n");
  663. goto error;
  664. }
  665. dbg(2," %s : serial_number=%s", __FUNCTION__, dev->serial_number);
  666. /* we can register the device now, as it is ready */
  667. usb_set_intfdata(interface, dev);
  668. retval = usb_register_dev(interface, &adu_class);
  669. if (retval) {
  670. /* something prevented us from registering this driver */
  671. dev_err(&interface->dev, "Not able to get a minor for this device.\n");
  672. usb_set_intfdata(interface, NULL);
  673. goto error;
  674. }
  675. dev->minor = interface->minor;
  676. /* let the user know what node this device is now attached to */
  677. dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d",
  678. udev->descriptor.idProduct, dev->serial_number,
  679. (dev->minor - ADU_MINOR_BASE));
  680. exit:
  681. dbg(2," %s : leave, return value %p (dev)", __FUNCTION__, dev);
  682. return retval;
  683. error:
  684. adu_delete(dev);
  685. return retval;
  686. }
  687. /**
  688. * adu_disconnect
  689. *
  690. * Called by the usb core when the device is removed from the system.
  691. */
  692. static void adu_disconnect(struct usb_interface *interface)
  693. {
  694. struct adu_device *dev;
  695. int minor;
  696. dbg(2," %s : enter", __FUNCTION__);
  697. dev = usb_get_intfdata(interface);
  698. usb_set_intfdata(interface, NULL);
  699. minor = dev->minor;
  700. /* give back our minor */
  701. usb_deregister_dev(interface, &adu_class);
  702. dev->minor = 0;
  703. mutex_lock(&dev->mtx); /* not interruptible */
  704. /* if the device is not opened, then we clean up right now */
  705. dbg(2," %s : open count %d", __FUNCTION__, dev->open_count);
  706. if (!dev->open_count) {
  707. mutex_unlock(&dev->mtx);
  708. adu_delete(dev);
  709. } else {
  710. dev->udev = NULL;
  711. mutex_unlock(&dev->mtx);
  712. }
  713. dev_info(&interface->dev, "ADU device adutux%d now disconnected",
  714. (minor - ADU_MINOR_BASE));
  715. dbg(2," %s : leave", __FUNCTION__);
  716. }
  717. /* usb specific object needed to register this driver with the usb subsystem */
  718. static struct usb_driver adu_driver = {
  719. .name = "adutux",
  720. .probe = adu_probe,
  721. .disconnect = adu_disconnect,
  722. .id_table = device_table,
  723. };
  724. static int __init adu_init(void)
  725. {
  726. int result;
  727. dbg(2," %s : enter", __FUNCTION__);
  728. /* register this driver with the USB subsystem */
  729. result = usb_register(&adu_driver);
  730. if (result < 0) {
  731. err("usb_register failed for the "__FILE__" driver. "
  732. "Error number %d", result);
  733. goto exit;
  734. }
  735. info("adutux " DRIVER_DESC " " DRIVER_VERSION);
  736. info("adutux is an experimental driver. Use at your own risk");
  737. exit:
  738. dbg(2," %s : leave, return value %d", __FUNCTION__, result);
  739. return result;
  740. }
  741. static void __exit adu_exit(void)
  742. {
  743. dbg(2," %s : enter", __FUNCTION__);
  744. /* deregister this driver with the USB subsystem */
  745. usb_deregister(&adu_driver);
  746. dbg(2," %s : leave", __FUNCTION__);
  747. }
  748. module_init(adu_init);
  749. module_exit(adu_exit);
  750. MODULE_AUTHOR(DRIVER_AUTHOR);
  751. MODULE_DESCRIPTION(DRIVER_DESC);
  752. MODULE_LICENSE("GPL");