adutux.c 24 KB

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