iowarrior.c 27 KB

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