iowarrior.c 25 KB

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