iowarrior.c 26 KB

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