iowarrior.c 26 KB

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