iowarrior.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Native support for the I/O-Warrior USB devices
  4. *
  5. * Copyright (c) 2003-2005 Code Mercenaries GmbH
  6. * written by Christian Lucht <lucht@codemercs.com>
  7. *
  8. * based on
  9. * usb-skeleton.c by Greg Kroah-Hartman <greg@kroah.com>
  10. * brlvger.c by Stephane Dalton <sdalton@videotron.ca>
  11. * and St�hane Doyon <s.doyon@videotron.ca>
  12. *
  13. * Released under the GPLv2.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/usb.h>
  17. #include <linux/slab.h>
  18. #include <linux/sched.h>
  19. #include <linux/mutex.h>
  20. #include <linux/poll.h>
  21. #include <linux/usb/iowarrior.h>
  22. #define DRIVER_AUTHOR "Christian Lucht <lucht@codemercs.com>"
  23. #define DRIVER_DESC "USB IO-Warrior driver"
  24. #define USB_VENDOR_ID_CODEMERCS 1984
  25. /* low speed iowarrior */
  26. #define USB_DEVICE_ID_CODEMERCS_IOW40 0x1500
  27. #define USB_DEVICE_ID_CODEMERCS_IOW24 0x1501
  28. #define USB_DEVICE_ID_CODEMERCS_IOWPV1 0x1511
  29. #define USB_DEVICE_ID_CODEMERCS_IOWPV2 0x1512
  30. /* full speed iowarrior */
  31. #define USB_DEVICE_ID_CODEMERCS_IOW56 0x1503
  32. /* Get a minor range for your devices from the usb maintainer */
  33. #ifdef CONFIG_USB_DYNAMIC_MINORS
  34. #define IOWARRIOR_MINOR_BASE 0
  35. #else
  36. #define IOWARRIOR_MINOR_BASE 208 // SKELETON_MINOR_BASE 192 + 16, not official yet
  37. #endif
  38. /* interrupt input queue size */
  39. #define MAX_INTERRUPT_BUFFER 16
  40. /*
  41. maximum number of urbs that are submitted for writes at the same time,
  42. this applies to the IOWarrior56 only!
  43. IOWarrior24 and IOWarrior40 use synchronous usb_control_msg calls.
  44. */
  45. #define MAX_WRITES_IN_FLIGHT 4
  46. MODULE_AUTHOR(DRIVER_AUTHOR);
  47. MODULE_DESCRIPTION(DRIVER_DESC);
  48. MODULE_LICENSE("GPL");
  49. /* Module parameters */
  50. static DEFINE_MUTEX(iowarrior_mutex);
  51. static struct usb_driver iowarrior_driver;
  52. static DEFINE_MUTEX(iowarrior_open_disc_lock);
  53. /*--------------*/
  54. /* data */
  55. /*--------------*/
  56. /* Structure to hold all of our device specific stuff */
  57. struct iowarrior {
  58. struct mutex mutex; /* locks this structure */
  59. struct usb_device *udev; /* save off the usb device pointer */
  60. struct usb_interface *interface; /* the interface for this device */
  61. unsigned char minor; /* the starting minor number for this device */
  62. struct usb_endpoint_descriptor *int_out_endpoint; /* endpoint for reading (needed for IOW56 only) */
  63. struct usb_endpoint_descriptor *int_in_endpoint; /* endpoint for reading */
  64. struct urb *int_in_urb; /* the urb for reading data */
  65. unsigned char *int_in_buffer; /* buffer for data to be read */
  66. unsigned char serial_number; /* to detect lost packages */
  67. unsigned char *read_queue; /* size is MAX_INTERRUPT_BUFFER * packet size */
  68. wait_queue_head_t read_wait;
  69. wait_queue_head_t write_wait; /* wait-queue for writing to the device */
  70. atomic_t write_busy; /* number of write-urbs submitted */
  71. atomic_t read_idx;
  72. atomic_t intr_idx;
  73. spinlock_t intr_idx_lock; /* protects intr_idx */
  74. atomic_t overflow_flag; /* signals an index 'rollover' */
  75. int present; /* this is 1 as long as the device is connected */
  76. int opened; /* this is 1 if the device is currently open */
  77. char chip_serial[9]; /* the serial number string of the chip connected */
  78. int report_size; /* number of bytes in a report */
  79. u16 product_id;
  80. };
  81. /*--------------*/
  82. /* globals */
  83. /*--------------*/
  84. /*
  85. * USB spec identifies 5 second timeouts.
  86. */
  87. #define GET_TIMEOUT 5
  88. #define USB_REQ_GET_REPORT 0x01
  89. //#if 0
  90. static int usb_get_report(struct usb_device *dev,
  91. struct usb_host_interface *inter, unsigned char type,
  92. unsigned char id, void *buf, int size)
  93. {
  94. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  95. USB_REQ_GET_REPORT,
  96. USB_DIR_IN | USB_TYPE_CLASS |
  97. USB_RECIP_INTERFACE, (type << 8) + id,
  98. inter->desc.bInterfaceNumber, buf, size,
  99. GET_TIMEOUT*HZ);
  100. }
  101. //#endif
  102. #define USB_REQ_SET_REPORT 0x09
  103. static int usb_set_report(struct usb_interface *intf, unsigned char type,
  104. unsigned char id, void *buf, int size)
  105. {
  106. return usb_control_msg(interface_to_usbdev(intf),
  107. usb_sndctrlpipe(interface_to_usbdev(intf), 0),
  108. USB_REQ_SET_REPORT,
  109. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  110. (type << 8) + id,
  111. intf->cur_altsetting->desc.bInterfaceNumber, buf,
  112. size, HZ);
  113. }
  114. /*---------------------*/
  115. /* driver registration */
  116. /*---------------------*/
  117. /* table of devices that work with this driver */
  118. static const struct usb_device_id iowarrior_ids[] = {
  119. {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW40)},
  120. {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24)},
  121. {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOWPV1)},
  122. {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOWPV2)},
  123. {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW56)},
  124. {} /* Terminating entry */
  125. };
  126. MODULE_DEVICE_TABLE(usb, iowarrior_ids);
  127. /*
  128. * USB callback handler for reading data
  129. */
  130. static void iowarrior_callback(struct urb *urb)
  131. {
  132. struct iowarrior *dev = urb->context;
  133. int intr_idx;
  134. int read_idx;
  135. int aux_idx;
  136. int offset;
  137. int status = urb->status;
  138. int retval;
  139. switch (status) {
  140. case 0:
  141. /* success */
  142. break;
  143. case -ECONNRESET:
  144. case -ENOENT:
  145. case -ESHUTDOWN:
  146. return;
  147. default:
  148. goto exit;
  149. }
  150. spin_lock(&dev->intr_idx_lock);
  151. intr_idx = atomic_read(&dev->intr_idx);
  152. /* aux_idx become previous intr_idx */
  153. aux_idx = (intr_idx == 0) ? (MAX_INTERRUPT_BUFFER - 1) : (intr_idx - 1);
  154. read_idx = atomic_read(&dev->read_idx);
  155. /* queue is not empty and it's interface 0 */
  156. if ((intr_idx != read_idx)
  157. && (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0)) {
  158. /* + 1 for serial number */
  159. offset = aux_idx * (dev->report_size + 1);
  160. if (!memcmp
  161. (dev->read_queue + offset, urb->transfer_buffer,
  162. dev->report_size)) {
  163. /* equal values on interface 0 will be ignored */
  164. spin_unlock(&dev->intr_idx_lock);
  165. goto exit;
  166. }
  167. }
  168. /* aux_idx become next intr_idx */
  169. aux_idx = (intr_idx == (MAX_INTERRUPT_BUFFER - 1)) ? 0 : (intr_idx + 1);
  170. if (read_idx == aux_idx) {
  171. /* queue full, dropping oldest input */
  172. read_idx = (++read_idx == MAX_INTERRUPT_BUFFER) ? 0 : read_idx;
  173. atomic_set(&dev->read_idx, read_idx);
  174. atomic_set(&dev->overflow_flag, 1);
  175. }
  176. /* +1 for serial number */
  177. offset = intr_idx * (dev->report_size + 1);
  178. memcpy(dev->read_queue + offset, urb->transfer_buffer,
  179. dev->report_size);
  180. *(dev->read_queue + offset + (dev->report_size)) = dev->serial_number++;
  181. atomic_set(&dev->intr_idx, aux_idx);
  182. spin_unlock(&dev->intr_idx_lock);
  183. /* tell the blocking read about the new data */
  184. wake_up_interruptible(&dev->read_wait);
  185. exit:
  186. retval = usb_submit_urb(urb, GFP_ATOMIC);
  187. if (retval)
  188. dev_err(&dev->interface->dev, "%s - usb_submit_urb failed with result %d\n",
  189. __func__, retval);
  190. }
  191. /*
  192. * USB Callback handler for write-ops
  193. */
  194. static void iowarrior_write_callback(struct urb *urb)
  195. {
  196. struct iowarrior *dev;
  197. int status = urb->status;
  198. dev = urb->context;
  199. /* sync/async unlink faults aren't errors */
  200. if (status &&
  201. !(status == -ENOENT ||
  202. status == -ECONNRESET || status == -ESHUTDOWN)) {
  203. dev_dbg(&dev->interface->dev,
  204. "nonzero write bulk status received: %d\n", status);
  205. }
  206. /* free up our allocated buffer */
  207. usb_free_coherent(urb->dev, urb->transfer_buffer_length,
  208. urb->transfer_buffer, urb->transfer_dma);
  209. /* tell a waiting writer the interrupt-out-pipe is available again */
  210. atomic_dec(&dev->write_busy);
  211. wake_up_interruptible(&dev->write_wait);
  212. }
  213. /**
  214. * iowarrior_delete
  215. */
  216. static inline void iowarrior_delete(struct iowarrior *dev)
  217. {
  218. dev_dbg(&dev->interface->dev, "minor %d\n", dev->minor);
  219. kfree(dev->int_in_buffer);
  220. usb_free_urb(dev->int_in_urb);
  221. kfree(dev->read_queue);
  222. kfree(dev);
  223. }
  224. /*---------------------*/
  225. /* fops implementation */
  226. /*---------------------*/
  227. static int read_index(struct iowarrior *dev)
  228. {
  229. int intr_idx, read_idx;
  230. read_idx = atomic_read(&dev->read_idx);
  231. intr_idx = atomic_read(&dev->intr_idx);
  232. return (read_idx == intr_idx ? -1 : read_idx);
  233. }
  234. /**
  235. * iowarrior_read
  236. */
  237. static ssize_t iowarrior_read(struct file *file, char __user *buffer,
  238. size_t count, loff_t *ppos)
  239. {
  240. struct iowarrior *dev;
  241. int read_idx;
  242. int offset;
  243. dev = file->private_data;
  244. /* verify that the device wasn't unplugged */
  245. if (!dev || !dev->present)
  246. return -ENODEV;
  247. dev_dbg(&dev->interface->dev, "minor %d, count = %zd\n",
  248. dev->minor, count);
  249. /* read count must be packet size (+ time stamp) */
  250. if ((count != dev->report_size)
  251. && (count != (dev->report_size + 1)))
  252. return -EINVAL;
  253. /* repeat until no buffer overrun in callback handler occur */
  254. do {
  255. atomic_set(&dev->overflow_flag, 0);
  256. if ((read_idx = read_index(dev)) == -1) {
  257. /* queue empty */
  258. if (file->f_flags & O_NONBLOCK)
  259. return -EAGAIN;
  260. else {
  261. //next line will return when there is either new data, or the device is unplugged
  262. int r = wait_event_interruptible(dev->read_wait,
  263. (!dev->present
  264. || (read_idx =
  265. read_index
  266. (dev)) !=
  267. -1));
  268. if (r) {
  269. //we were interrupted by a signal
  270. return -ERESTART;
  271. }
  272. if (!dev->present) {
  273. //The device was unplugged
  274. return -ENODEV;
  275. }
  276. if (read_idx == -1) {
  277. // Can this happen ???
  278. return 0;
  279. }
  280. }
  281. }
  282. offset = read_idx * (dev->report_size + 1);
  283. if (copy_to_user(buffer, dev->read_queue + offset, count)) {
  284. return -EFAULT;
  285. }
  286. } while (atomic_read(&dev->overflow_flag));
  287. read_idx = ++read_idx == MAX_INTERRUPT_BUFFER ? 0 : read_idx;
  288. atomic_set(&dev->read_idx, read_idx);
  289. return count;
  290. }
  291. /*
  292. * iowarrior_write
  293. */
  294. static ssize_t iowarrior_write(struct file *file,
  295. const char __user *user_buffer,
  296. size_t count, loff_t *ppos)
  297. {
  298. struct iowarrior *dev;
  299. int retval = 0;
  300. char *buf = NULL; /* for IOW24 and IOW56 we need a buffer */
  301. struct urb *int_out_urb = NULL;
  302. dev = file->private_data;
  303. mutex_lock(&dev->mutex);
  304. /* verify that the device wasn't unplugged */
  305. if (!dev->present) {
  306. retval = -ENODEV;
  307. goto exit;
  308. }
  309. dev_dbg(&dev->interface->dev, "minor %d, count = %zd\n",
  310. dev->minor, count);
  311. /* if count is 0 we're already done */
  312. if (count == 0) {
  313. retval = 0;
  314. goto exit;
  315. }
  316. /* We only accept full reports */
  317. if (count != dev->report_size) {
  318. retval = -EINVAL;
  319. goto exit;
  320. }
  321. switch (dev->product_id) {
  322. case USB_DEVICE_ID_CODEMERCS_IOW24:
  323. case USB_DEVICE_ID_CODEMERCS_IOWPV1:
  324. case USB_DEVICE_ID_CODEMERCS_IOWPV2:
  325. case USB_DEVICE_ID_CODEMERCS_IOW40:
  326. /* IOW24 and IOW40 use a synchronous call */
  327. buf = memdup_user(user_buffer, count);
  328. if (IS_ERR(buf)) {
  329. retval = PTR_ERR(buf);
  330. goto exit;
  331. }
  332. retval = usb_set_report(dev->interface, 2, 0, buf, count);
  333. kfree(buf);
  334. goto exit;
  335. break;
  336. case USB_DEVICE_ID_CODEMERCS_IOW56:
  337. /* The IOW56 uses asynchronous IO and more urbs */
  338. if (atomic_read(&dev->write_busy) == MAX_WRITES_IN_FLIGHT) {
  339. /* Wait until we are below the limit for submitted urbs */
  340. if (file->f_flags & O_NONBLOCK) {
  341. retval = -EAGAIN;
  342. goto exit;
  343. } else {
  344. retval = wait_event_interruptible(dev->write_wait,
  345. (!dev->present || (atomic_read (&dev-> write_busy) < MAX_WRITES_IN_FLIGHT)));
  346. if (retval) {
  347. /* we were interrupted by a signal */
  348. retval = -ERESTART;
  349. goto exit;
  350. }
  351. if (!dev->present) {
  352. /* The device was unplugged */
  353. retval = -ENODEV;
  354. goto exit;
  355. }
  356. if (!dev->opened) {
  357. /* We were closed while waiting for an URB */
  358. retval = -ENODEV;
  359. goto exit;
  360. }
  361. }
  362. }
  363. atomic_inc(&dev->write_busy);
  364. int_out_urb = usb_alloc_urb(0, GFP_KERNEL);
  365. if (!int_out_urb) {
  366. retval = -ENOMEM;
  367. goto error_no_urb;
  368. }
  369. buf = usb_alloc_coherent(dev->udev, dev->report_size,
  370. GFP_KERNEL, &int_out_urb->transfer_dma);
  371. if (!buf) {
  372. retval = -ENOMEM;
  373. dev_dbg(&dev->interface->dev,
  374. "Unable to allocate buffer\n");
  375. goto error_no_buffer;
  376. }
  377. usb_fill_int_urb(int_out_urb, dev->udev,
  378. usb_sndintpipe(dev->udev,
  379. dev->int_out_endpoint->bEndpointAddress),
  380. buf, dev->report_size,
  381. iowarrior_write_callback, dev,
  382. dev->int_out_endpoint->bInterval);
  383. int_out_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  384. if (copy_from_user(buf, user_buffer, count)) {
  385. retval = -EFAULT;
  386. goto error;
  387. }
  388. retval = usb_submit_urb(int_out_urb, GFP_KERNEL);
  389. if (retval) {
  390. dev_dbg(&dev->interface->dev,
  391. "submit error %d for urb nr.%d\n",
  392. retval, atomic_read(&dev->write_busy));
  393. goto error;
  394. }
  395. /* submit was ok */
  396. retval = count;
  397. usb_free_urb(int_out_urb);
  398. goto exit;
  399. break;
  400. default:
  401. /* what do we have here ? An unsupported Product-ID ? */
  402. dev_err(&dev->interface->dev, "%s - not supported for product=0x%x\n",
  403. __func__, dev->product_id);
  404. retval = -EFAULT;
  405. goto exit;
  406. break;
  407. }
  408. error:
  409. usb_free_coherent(dev->udev, dev->report_size, buf,
  410. int_out_urb->transfer_dma);
  411. error_no_buffer:
  412. usb_free_urb(int_out_urb);
  413. error_no_urb:
  414. atomic_dec(&dev->write_busy);
  415. wake_up_interruptible(&dev->write_wait);
  416. exit:
  417. mutex_unlock(&dev->mutex);
  418. return retval;
  419. }
  420. /**
  421. * iowarrior_ioctl
  422. */
  423. static long iowarrior_ioctl(struct file *file, unsigned int cmd,
  424. unsigned long arg)
  425. {
  426. struct iowarrior *dev = NULL;
  427. __u8 *buffer;
  428. __u8 __user *user_buffer;
  429. int retval;
  430. int io_res; /* checks for bytes read/written and copy_to/from_user results */
  431. dev = file->private_data;
  432. if (!dev)
  433. return -ENODEV;
  434. buffer = kzalloc(dev->report_size, GFP_KERNEL);
  435. if (!buffer)
  436. return -ENOMEM;
  437. /* lock this object */
  438. mutex_lock(&iowarrior_mutex);
  439. mutex_lock(&dev->mutex);
  440. /* verify that the device wasn't unplugged */
  441. if (!dev->present) {
  442. retval = -ENODEV;
  443. goto error_out;
  444. }
  445. dev_dbg(&dev->interface->dev, "minor %d, cmd 0x%.4x, arg %ld\n",
  446. dev->minor, cmd, arg);
  447. retval = 0;
  448. io_res = 0;
  449. switch (cmd) {
  450. case IOW_WRITE:
  451. if (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW24 ||
  452. dev->product_id == USB_DEVICE_ID_CODEMERCS_IOWPV1 ||
  453. dev->product_id == USB_DEVICE_ID_CODEMERCS_IOWPV2 ||
  454. dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW40) {
  455. user_buffer = (__u8 __user *)arg;
  456. io_res = copy_from_user(buffer, user_buffer,
  457. dev->report_size);
  458. if (io_res) {
  459. retval = -EFAULT;
  460. } else {
  461. io_res = usb_set_report(dev->interface, 2, 0,
  462. buffer,
  463. dev->report_size);
  464. if (io_res < 0)
  465. retval = io_res;
  466. }
  467. } else {
  468. retval = -EINVAL;
  469. dev_err(&dev->interface->dev,
  470. "ioctl 'IOW_WRITE' is not supported for product=0x%x.\n",
  471. dev->product_id);
  472. }
  473. break;
  474. case IOW_READ:
  475. user_buffer = (__u8 __user *)arg;
  476. io_res = usb_get_report(dev->udev,
  477. dev->interface->cur_altsetting, 1, 0,
  478. buffer, dev->report_size);
  479. if (io_res < 0)
  480. retval = io_res;
  481. else {
  482. io_res = copy_to_user(user_buffer, buffer, dev->report_size);
  483. if (io_res)
  484. retval = -EFAULT;
  485. }
  486. break;
  487. case IOW_GETINFO:
  488. {
  489. /* Report available information for the device */
  490. struct iowarrior_info info;
  491. /* needed for power consumption */
  492. struct usb_config_descriptor *cfg_descriptor = &dev->udev->actconfig->desc;
  493. memset(&info, 0, sizeof(info));
  494. /* directly from the descriptor */
  495. info.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
  496. info.product = dev->product_id;
  497. info.revision = le16_to_cpu(dev->udev->descriptor.bcdDevice);
  498. /* 0==UNKNOWN, 1==LOW(usb1.1) ,2=FULL(usb1.1), 3=HIGH(usb2.0) */
  499. info.speed = dev->udev->speed;
  500. info.if_num = dev->interface->cur_altsetting->desc.bInterfaceNumber;
  501. info.report_size = dev->report_size;
  502. /* serial number string has been read earlier 8 chars or empty string */
  503. memcpy(info.serial, dev->chip_serial,
  504. sizeof(dev->chip_serial));
  505. if (cfg_descriptor == NULL) {
  506. info.power = -1; /* no information available */
  507. } else {
  508. /* the MaxPower is stored in units of 2mA to make it fit into a byte-value */
  509. info.power = cfg_descriptor->bMaxPower * 2;
  510. }
  511. io_res = copy_to_user((struct iowarrior_info __user *)arg, &info,
  512. sizeof(struct iowarrior_info));
  513. if (io_res)
  514. retval = -EFAULT;
  515. break;
  516. }
  517. default:
  518. /* return that we did not understand this ioctl call */
  519. retval = -ENOTTY;
  520. break;
  521. }
  522. error_out:
  523. /* unlock the device */
  524. mutex_unlock(&dev->mutex);
  525. mutex_unlock(&iowarrior_mutex);
  526. kfree(buffer);
  527. return retval;
  528. }
  529. /**
  530. * iowarrior_open
  531. */
  532. static int iowarrior_open(struct inode *inode, struct file *file)
  533. {
  534. struct iowarrior *dev = NULL;
  535. struct usb_interface *interface;
  536. int subminor;
  537. int retval = 0;
  538. mutex_lock(&iowarrior_mutex);
  539. subminor = iminor(inode);
  540. interface = usb_find_interface(&iowarrior_driver, subminor);
  541. if (!interface) {
  542. mutex_unlock(&iowarrior_mutex);
  543. printk(KERN_ERR "%s - error, can't find device for minor %d\n",
  544. __func__, subminor);
  545. return -ENODEV;
  546. }
  547. mutex_lock(&iowarrior_open_disc_lock);
  548. dev = usb_get_intfdata(interface);
  549. if (!dev) {
  550. mutex_unlock(&iowarrior_open_disc_lock);
  551. mutex_unlock(&iowarrior_mutex);
  552. return -ENODEV;
  553. }
  554. mutex_lock(&dev->mutex);
  555. mutex_unlock(&iowarrior_open_disc_lock);
  556. /* Only one process can open each device, no sharing. */
  557. if (dev->opened) {
  558. retval = -EBUSY;
  559. goto out;
  560. }
  561. /* setup interrupt handler for receiving values */
  562. if ((retval = usb_submit_urb(dev->int_in_urb, GFP_KERNEL)) < 0) {
  563. dev_err(&interface->dev, "Error %d while submitting URB\n", retval);
  564. retval = -EFAULT;
  565. goto out;
  566. }
  567. /* increment our usage count for the driver */
  568. ++dev->opened;
  569. /* save our object in the file's private structure */
  570. file->private_data = dev;
  571. retval = 0;
  572. out:
  573. mutex_unlock(&dev->mutex);
  574. mutex_unlock(&iowarrior_mutex);
  575. return retval;
  576. }
  577. /**
  578. * iowarrior_release
  579. */
  580. static int iowarrior_release(struct inode *inode, struct file *file)
  581. {
  582. struct iowarrior *dev;
  583. int retval = 0;
  584. dev = file->private_data;
  585. if (!dev)
  586. return -ENODEV;
  587. dev_dbg(&dev->interface->dev, "minor %d\n", dev->minor);
  588. /* lock our device */
  589. mutex_lock(&dev->mutex);
  590. if (dev->opened <= 0) {
  591. retval = -ENODEV; /* close called more than once */
  592. mutex_unlock(&dev->mutex);
  593. } else {
  594. dev->opened = 0; /* we're closing now */
  595. retval = 0;
  596. if (dev->present) {
  597. /*
  598. The device is still connected so we only shutdown
  599. pending read-/write-ops.
  600. */
  601. usb_kill_urb(dev->int_in_urb);
  602. wake_up_interruptible(&dev->read_wait);
  603. wake_up_interruptible(&dev->write_wait);
  604. mutex_unlock(&dev->mutex);
  605. } else {
  606. /* The device was unplugged, cleanup resources */
  607. mutex_unlock(&dev->mutex);
  608. iowarrior_delete(dev);
  609. }
  610. }
  611. return retval;
  612. }
  613. static __poll_t iowarrior_poll(struct file *file, poll_table * wait)
  614. {
  615. struct iowarrior *dev = file->private_data;
  616. __poll_t mask = 0;
  617. if (!dev->present)
  618. return EPOLLERR | EPOLLHUP;
  619. poll_wait(file, &dev->read_wait, wait);
  620. poll_wait(file, &dev->write_wait, wait);
  621. if (!dev->present)
  622. return EPOLLERR | EPOLLHUP;
  623. if (read_index(dev) != -1)
  624. mask |= EPOLLIN | EPOLLRDNORM;
  625. if (atomic_read(&dev->write_busy) < MAX_WRITES_IN_FLIGHT)
  626. mask |= EPOLLOUT | EPOLLWRNORM;
  627. return mask;
  628. }
  629. /*
  630. * File operations needed when we register this driver.
  631. * This assumes that this driver NEEDS file operations,
  632. * of course, which means that the driver is expected
  633. * to have a node in the /dev directory. If the USB
  634. * device were for a network interface then the driver
  635. * would use "struct net_driver" instead, and a serial
  636. * device would use "struct tty_driver".
  637. */
  638. static const struct file_operations iowarrior_fops = {
  639. .owner = THIS_MODULE,
  640. .write = iowarrior_write,
  641. .read = iowarrior_read,
  642. .unlocked_ioctl = iowarrior_ioctl,
  643. .open = iowarrior_open,
  644. .release = iowarrior_release,
  645. .poll = iowarrior_poll,
  646. .llseek = noop_llseek,
  647. };
  648. static char *iowarrior_devnode(struct device *dev, umode_t *mode)
  649. {
  650. return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
  651. }
  652. /*
  653. * usb class driver info in order to get a minor number from the usb core,
  654. * and to have the device registered with devfs and the driver core
  655. */
  656. static struct usb_class_driver iowarrior_class = {
  657. .name = "iowarrior%d",
  658. .devnode = iowarrior_devnode,
  659. .fops = &iowarrior_fops,
  660. .minor_base = IOWARRIOR_MINOR_BASE,
  661. };
  662. /*---------------------------------*/
  663. /* probe and disconnect functions */
  664. /*---------------------------------*/
  665. /**
  666. * iowarrior_probe
  667. *
  668. * Called by the usb core when a new device is connected that it thinks
  669. * this driver might be interested in.
  670. */
  671. static int iowarrior_probe(struct usb_interface *interface,
  672. const struct usb_device_id *id)
  673. {
  674. struct usb_device *udev = interface_to_usbdev(interface);
  675. struct iowarrior *dev = NULL;
  676. struct usb_host_interface *iface_desc;
  677. int retval = -ENOMEM;
  678. int res;
  679. /* allocate memory for our device state and initialize it */
  680. dev = kzalloc(sizeof(struct iowarrior), GFP_KERNEL);
  681. if (!dev)
  682. return retval;
  683. mutex_init(&dev->mutex);
  684. atomic_set(&dev->intr_idx, 0);
  685. atomic_set(&dev->read_idx, 0);
  686. spin_lock_init(&dev->intr_idx_lock);
  687. atomic_set(&dev->overflow_flag, 0);
  688. init_waitqueue_head(&dev->read_wait);
  689. atomic_set(&dev->write_busy, 0);
  690. init_waitqueue_head(&dev->write_wait);
  691. dev->udev = udev;
  692. dev->interface = interface;
  693. iface_desc = interface->cur_altsetting;
  694. dev->product_id = le16_to_cpu(udev->descriptor.idProduct);
  695. res = usb_find_last_int_in_endpoint(iface_desc, &dev->int_in_endpoint);
  696. if (res) {
  697. dev_err(&interface->dev, "no interrupt-in endpoint found\n");
  698. retval = res;
  699. goto error;
  700. }
  701. if (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW56) {
  702. res = usb_find_last_int_out_endpoint(iface_desc,
  703. &dev->int_out_endpoint);
  704. if (res) {
  705. dev_err(&interface->dev, "no interrupt-out endpoint found\n");
  706. retval = res;
  707. goto error;
  708. }
  709. }
  710. /* we have to check the report_size often, so remember it in the endianness suitable for our machine */
  711. dev->report_size = usb_endpoint_maxp(dev->int_in_endpoint);
  712. if ((dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) &&
  713. (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW56))
  714. /* IOWarrior56 has wMaxPacketSize different from report size */
  715. dev->report_size = 7;
  716. /* create the urb and buffer for reading */
  717. dev->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  718. if (!dev->int_in_urb)
  719. goto error;
  720. dev->int_in_buffer = kmalloc(dev->report_size, GFP_KERNEL);
  721. if (!dev->int_in_buffer)
  722. goto error;
  723. usb_fill_int_urb(dev->int_in_urb, dev->udev,
  724. usb_rcvintpipe(dev->udev,
  725. dev->int_in_endpoint->bEndpointAddress),
  726. dev->int_in_buffer, dev->report_size,
  727. iowarrior_callback, dev,
  728. dev->int_in_endpoint->bInterval);
  729. /* create an internal buffer for interrupt data from the device */
  730. dev->read_queue =
  731. kmalloc(((dev->report_size + 1) * MAX_INTERRUPT_BUFFER),
  732. GFP_KERNEL);
  733. if (!dev->read_queue)
  734. goto error;
  735. /* Get the serial-number of the chip */
  736. memset(dev->chip_serial, 0x00, sizeof(dev->chip_serial));
  737. usb_string(udev, udev->descriptor.iSerialNumber, dev->chip_serial,
  738. sizeof(dev->chip_serial));
  739. if (strlen(dev->chip_serial) != 8)
  740. memset(dev->chip_serial, 0x00, sizeof(dev->chip_serial));
  741. /* Set the idle timeout to 0, if this is interface 0 */
  742. if (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) {
  743. usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  744. 0x0A,
  745. USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
  746. 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
  747. }
  748. /* allow device read and ioctl */
  749. dev->present = 1;
  750. /* we can register the device now, as it is ready */
  751. usb_set_intfdata(interface, dev);
  752. retval = usb_register_dev(interface, &iowarrior_class);
  753. if (retval) {
  754. /* something prevented us from registering this driver */
  755. dev_err(&interface->dev, "Not able to get a minor for this device.\n");
  756. usb_set_intfdata(interface, NULL);
  757. goto error;
  758. }
  759. dev->minor = interface->minor;
  760. /* let the user know what node this device is now attached to */
  761. dev_info(&interface->dev, "IOWarrior product=0x%x, serial=%s interface=%d "
  762. "now attached to iowarrior%d\n", dev->product_id, dev->chip_serial,
  763. iface_desc->desc.bInterfaceNumber, dev->minor - IOWARRIOR_MINOR_BASE);
  764. return retval;
  765. error:
  766. iowarrior_delete(dev);
  767. return retval;
  768. }
  769. /**
  770. * iowarrior_disconnect
  771. *
  772. * Called by the usb core when the device is removed from the system.
  773. */
  774. static void iowarrior_disconnect(struct usb_interface *interface)
  775. {
  776. struct iowarrior *dev;
  777. int minor;
  778. dev = usb_get_intfdata(interface);
  779. mutex_lock(&iowarrior_open_disc_lock);
  780. usb_set_intfdata(interface, NULL);
  781. minor = dev->minor;
  782. /* give back our minor */
  783. usb_deregister_dev(interface, &iowarrior_class);
  784. mutex_lock(&dev->mutex);
  785. /* prevent device read, write and ioctl */
  786. dev->present = 0;
  787. mutex_unlock(&dev->mutex);
  788. mutex_unlock(&iowarrior_open_disc_lock);
  789. if (dev->opened) {
  790. /* There is a process that holds a filedescriptor to the device ,
  791. so we only shutdown read-/write-ops going on.
  792. Deleting the device is postponed until close() was called.
  793. */
  794. usb_kill_urb(dev->int_in_urb);
  795. wake_up_interruptible(&dev->read_wait);
  796. wake_up_interruptible(&dev->write_wait);
  797. } else {
  798. /* no process is using the device, cleanup now */
  799. iowarrior_delete(dev);
  800. }
  801. dev_info(&interface->dev, "I/O-Warror #%d now disconnected\n",
  802. minor - IOWARRIOR_MINOR_BASE);
  803. }
  804. /* usb specific object needed to register this driver with the usb subsystem */
  805. static struct usb_driver iowarrior_driver = {
  806. .name = "iowarrior",
  807. .probe = iowarrior_probe,
  808. .disconnect = iowarrior_disconnect,
  809. .id_table = iowarrior_ids,
  810. };
  811. module_usb_driver(iowarrior_driver);