iowarrior.c 25 KB

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