adutux.c 22 KB

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