iowarrior.c 26 KB

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