iowarrior.c 25 KB

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