adutux.c 21 KB

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