iowarrior.c 26 KB

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