adutux.c 24 KB

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