iowarrior.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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/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 offical 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. /* Use our own dbg macro */
  48. #undef dbg
  49. #define dbg( format, arg... ) do { if( debug ) printk( KERN_DEBUG __FILE__ ": " format "\n" , ## arg ); } while ( 0 )
  50. MODULE_AUTHOR(DRIVER_AUTHOR);
  51. MODULE_DESCRIPTION(DRIVER_DESC);
  52. MODULE_LICENSE("GPL");
  53. /* Module parameters */
  54. static int debug = 0;
  55. module_param(debug, bool, 0644);
  56. MODULE_PARM_DESC(debug, "debug=1 enables debugging messages");
  57. static struct usb_driver iowarrior_driver;
  58. static DEFINE_MUTEX(iowarrior_open_disc_lock);
  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 = urb->context;
  139. int intr_idx;
  140. int read_idx;
  141. int aux_idx;
  142. int offset;
  143. int status = urb->status;
  144. int retval;
  145. switch (status) {
  146. case 0:
  147. /* success */
  148. break;
  149. case -ECONNRESET:
  150. case -ENOENT:
  151. case -ESHUTDOWN:
  152. return;
  153. default:
  154. goto exit;
  155. }
  156. spin_lock(&dev->intr_idx_lock);
  157. intr_idx = atomic_read(&dev->intr_idx);
  158. /* aux_idx become previous intr_idx */
  159. aux_idx = (intr_idx == 0) ? (MAX_INTERRUPT_BUFFER - 1) : (intr_idx - 1);
  160. read_idx = atomic_read(&dev->read_idx);
  161. /* queue is not empty and it's interface 0 */
  162. if ((intr_idx != read_idx)
  163. && (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0)) {
  164. /* + 1 for serial number */
  165. offset = aux_idx * (dev->report_size + 1);
  166. if (!memcmp
  167. (dev->read_queue + offset, urb->transfer_buffer,
  168. dev->report_size)) {
  169. /* equal values on interface 0 will be ignored */
  170. spin_unlock(&dev->intr_idx_lock);
  171. goto exit;
  172. }
  173. }
  174. /* aux_idx become next intr_idx */
  175. aux_idx = (intr_idx == (MAX_INTERRUPT_BUFFER - 1)) ? 0 : (intr_idx + 1);
  176. if (read_idx == aux_idx) {
  177. /* queue full, dropping oldest input */
  178. read_idx = (++read_idx == MAX_INTERRUPT_BUFFER) ? 0 : read_idx;
  179. atomic_set(&dev->read_idx, read_idx);
  180. atomic_set(&dev->overflow_flag, 1);
  181. }
  182. /* +1 for serial number */
  183. offset = intr_idx * (dev->report_size + 1);
  184. memcpy(dev->read_queue + offset, urb->transfer_buffer,
  185. dev->report_size);
  186. *(dev->read_queue + offset + (dev->report_size)) = dev->serial_number++;
  187. atomic_set(&dev->intr_idx, aux_idx);
  188. spin_unlock(&dev->intr_idx_lock);
  189. /* tell the blocking read about the new data */
  190. wake_up_interruptible(&dev->read_wait);
  191. exit:
  192. retval = usb_submit_urb(urb, GFP_ATOMIC);
  193. if (retval)
  194. dev_err(&dev->interface->dev, "%s - usb_submit_urb failed with result %d\n",
  195. __func__, retval);
  196. }
  197. /*
  198. * USB Callback handler for write-ops
  199. */
  200. static void iowarrior_write_callback(struct urb *urb)
  201. {
  202. struct iowarrior *dev;
  203. int status = urb->status;
  204. dev = urb->context;
  205. /* sync/async unlink faults aren't errors */
  206. if (status &&
  207. !(status == -ENOENT ||
  208. status == -ECONNRESET || status == -ESHUTDOWN)) {
  209. dbg("%s - nonzero write bulk status received: %d",
  210. __func__, status);
  211. }
  212. /* free up our allocated buffer */
  213. usb_buffer_free(urb->dev, urb->transfer_buffer_length,
  214. urb->transfer_buffer, urb->transfer_dma);
  215. /* tell a waiting writer the interrupt-out-pipe is available again */
  216. atomic_dec(&dev->write_busy);
  217. wake_up_interruptible(&dev->write_wait);
  218. }
  219. /**
  220. * iowarrior_delete
  221. */
  222. static inline void iowarrior_delete(struct iowarrior *dev)
  223. {
  224. dbg("%s - minor %d", __func__, dev->minor);
  225. kfree(dev->int_in_buffer);
  226. usb_free_urb(dev->int_in_urb);
  227. kfree(dev->read_queue);
  228. kfree(dev);
  229. }
  230. /*---------------------*/
  231. /* fops implementation */
  232. /*---------------------*/
  233. static int read_index(struct iowarrior *dev)
  234. {
  235. int intr_idx, read_idx;
  236. read_idx = atomic_read(&dev->read_idx);
  237. intr_idx = atomic_read(&dev->intr_idx);
  238. return (read_idx == intr_idx ? -1 : read_idx);
  239. }
  240. /**
  241. * iowarrior_read
  242. */
  243. static ssize_t iowarrior_read(struct file *file, char __user *buffer,
  244. size_t count, loff_t *ppos)
  245. {
  246. struct iowarrior *dev;
  247. int read_idx;
  248. int offset;
  249. dev = (struct iowarrior *)file->private_data;
  250. /* verify that the device wasn't unplugged */
  251. if (dev == NULL || !dev->present)
  252. return -ENODEV;
  253. dbg("%s - minor %d, count = %zd", __func__, dev->minor, count);
  254. /* read count must be packet size (+ time stamp) */
  255. if ((count != dev->report_size)
  256. && (count != (dev->report_size + 1)))
  257. return -EINVAL;
  258. /* repeat until no buffer overrun in callback handler occur */
  259. do {
  260. atomic_set(&dev->overflow_flag, 0);
  261. if ((read_idx = read_index(dev)) == -1) {
  262. /* queue emty */
  263. if (file->f_flags & O_NONBLOCK)
  264. return -EAGAIN;
  265. else {
  266. //next line will return when there is either new data, or the device is unplugged
  267. int r = wait_event_interruptible(dev->read_wait,
  268. (!dev->present
  269. || (read_idx =
  270. read_index
  271. (dev)) !=
  272. -1));
  273. if (r) {
  274. //we were interrupted by a signal
  275. return -ERESTART;
  276. }
  277. if (!dev->present) {
  278. //The device was unplugged
  279. return -ENODEV;
  280. }
  281. if (read_idx == -1) {
  282. // Can this happen ???
  283. return 0;
  284. }
  285. }
  286. }
  287. offset = read_idx * (dev->report_size + 1);
  288. if (copy_to_user(buffer, dev->read_queue + offset, count)) {
  289. return -EFAULT;
  290. }
  291. } while (atomic_read(&dev->overflow_flag));
  292. read_idx = ++read_idx == MAX_INTERRUPT_BUFFER ? 0 : read_idx;
  293. atomic_set(&dev->read_idx, read_idx);
  294. return count;
  295. }
  296. /*
  297. * iowarrior_write
  298. */
  299. static ssize_t iowarrior_write(struct file *file,
  300. const char __user *user_buffer,
  301. size_t count, loff_t *ppos)
  302. {
  303. struct iowarrior *dev;
  304. int retval = 0;
  305. char *buf = NULL; /* for IOW24 and IOW56 we need a buffer */
  306. struct urb *int_out_urb = NULL;
  307. dev = (struct iowarrior *)file->private_data;
  308. mutex_lock(&dev->mutex);
  309. /* verify that the device wasn't unplugged */
  310. if (!dev->present) {
  311. retval = -ENODEV;
  312. goto exit;
  313. }
  314. dbg("%s - minor %d, count = %zd", __func__, dev->minor, count);
  315. /* if count is 0 we're already done */
  316. if (count == 0) {
  317. retval = 0;
  318. goto exit;
  319. }
  320. /* We only accept full reports */
  321. if (count != dev->report_size) {
  322. retval = -EINVAL;
  323. goto exit;
  324. }
  325. switch (dev->product_id) {
  326. case USB_DEVICE_ID_CODEMERCS_IOW24:
  327. case USB_DEVICE_ID_CODEMERCS_IOWPV1:
  328. case USB_DEVICE_ID_CODEMERCS_IOWPV2:
  329. case USB_DEVICE_ID_CODEMERCS_IOW40:
  330. /* IOW24 and IOW40 use a synchronous call */
  331. buf = kmalloc(8, GFP_KERNEL); /* 8 bytes are enough for both products */
  332. if (!buf) {
  333. retval = -ENOMEM;
  334. goto exit;
  335. }
  336. if (copy_from_user(buf, user_buffer, count)) {
  337. retval = -EFAULT;
  338. kfree(buf);
  339. goto exit;
  340. }
  341. retval = usb_set_report(dev->interface, 2, 0, buf, count);
  342. kfree(buf);
  343. goto exit;
  344. break;
  345. case USB_DEVICE_ID_CODEMERCS_IOW56:
  346. /* The IOW56 uses asynchronous IO and more urbs */
  347. if (atomic_read(&dev->write_busy) == MAX_WRITES_IN_FLIGHT) {
  348. /* Wait until we are below the limit for submitted urbs */
  349. if (file->f_flags & O_NONBLOCK) {
  350. retval = -EAGAIN;
  351. goto exit;
  352. } else {
  353. retval = wait_event_interruptible(dev->write_wait,
  354. (!dev->present || (atomic_read (&dev-> write_busy) < MAX_WRITES_IN_FLIGHT)));
  355. if (retval) {
  356. /* we were interrupted by a signal */
  357. retval = -ERESTART;
  358. goto exit;
  359. }
  360. if (!dev->present) {
  361. /* The device was unplugged */
  362. retval = -ENODEV;
  363. goto exit;
  364. }
  365. if (!dev->opened) {
  366. /* We were closed while waiting for an URB */
  367. retval = -ENODEV;
  368. goto exit;
  369. }
  370. }
  371. }
  372. atomic_inc(&dev->write_busy);
  373. int_out_urb = usb_alloc_urb(0, GFP_KERNEL);
  374. if (!int_out_urb) {
  375. retval = -ENOMEM;
  376. dbg("%s Unable to allocate urb ", __func__);
  377. goto error_no_urb;
  378. }
  379. buf = usb_buffer_alloc(dev->udev, dev->report_size,
  380. GFP_KERNEL, &int_out_urb->transfer_dma);
  381. if (!buf) {
  382. retval = -ENOMEM;
  383. dbg("%s Unable to allocate buffer ", __func__);
  384. goto error_no_buffer;
  385. }
  386. usb_fill_int_urb(int_out_urb, dev->udev,
  387. usb_sndintpipe(dev->udev,
  388. dev->int_out_endpoint->bEndpointAddress),
  389. buf, dev->report_size,
  390. iowarrior_write_callback, dev,
  391. dev->int_out_endpoint->bInterval);
  392. int_out_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  393. if (copy_from_user(buf, user_buffer, count)) {
  394. retval = -EFAULT;
  395. goto error;
  396. }
  397. retval = usb_submit_urb(int_out_urb, GFP_KERNEL);
  398. if (retval) {
  399. dbg("%s submit error %d for urb nr.%d", __func__,
  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_buffer_free(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 = (struct iowarrior *)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. lock_kernel();
  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. dbg("%s - minor %d, cmd 0x%.4x, arg %ld", __func__, dev->minor, cmd,
  455. 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 < 0)
  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. /* directly from the descriptor */
  503. info.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
  504. info.product = dev->product_id;
  505. info.revision = le16_to_cpu(dev->udev->descriptor.bcdDevice);
  506. /* 0==UNKNOWN, 1==LOW(usb1.1) ,2=FULL(usb1.1), 3=HIGH(usb2.0) */
  507. info.speed = le16_to_cpu(dev->udev->speed);
  508. info.if_num = dev->interface->cur_altsetting->desc.bInterfaceNumber;
  509. info.report_size = dev->report_size;
  510. /* serial number string has been read earlier 8 chars or empty string */
  511. memcpy(info.serial, dev->chip_serial,
  512. sizeof(dev->chip_serial));
  513. if (cfg_descriptor == NULL) {
  514. info.power = -1; /* no information available */
  515. } else {
  516. /* the MaxPower is stored in units of 2mA to make it fit into a byte-value */
  517. info.power = cfg_descriptor->bMaxPower * 2;
  518. }
  519. io_res = copy_to_user((struct iowarrior_info __user *)arg, &info,
  520. sizeof(struct iowarrior_info));
  521. if (io_res < 0)
  522. retval = -EFAULT;
  523. break;
  524. }
  525. default:
  526. /* return that we did not understand this ioctl call */
  527. retval = -ENOTTY;
  528. break;
  529. }
  530. error_out:
  531. /* unlock the device */
  532. mutex_unlock(&dev->mutex);
  533. unlock_kernel();
  534. kfree(buffer);
  535. return retval;
  536. }
  537. /**
  538. * iowarrior_open
  539. */
  540. static int iowarrior_open(struct inode *inode, struct file *file)
  541. {
  542. struct iowarrior *dev = NULL;
  543. struct usb_interface *interface;
  544. int subminor;
  545. int retval = 0;
  546. dbg("%s", __func__);
  547. subminor = iminor(inode);
  548. interface = usb_find_interface(&iowarrior_driver, subminor);
  549. if (!interface) {
  550. err("%s - error, can't find device for minor %d", __func__,
  551. subminor);
  552. return -ENODEV;
  553. }
  554. mutex_lock(&iowarrior_open_disc_lock);
  555. dev = usb_get_intfdata(interface);
  556. if (!dev) {
  557. mutex_unlock(&iowarrior_open_disc_lock);
  558. return -ENODEV;
  559. }
  560. mutex_lock(&dev->mutex);
  561. mutex_unlock(&iowarrior_open_disc_lock);
  562. /* Only one process can open each device, no sharing. */
  563. if (dev->opened) {
  564. retval = -EBUSY;
  565. goto out;
  566. }
  567. /* setup interrupt handler for receiving values */
  568. if ((retval = usb_submit_urb(dev->int_in_urb, GFP_KERNEL)) < 0) {
  569. dev_err(&interface->dev, "Error %d while submitting URB\n", retval);
  570. retval = -EFAULT;
  571. goto out;
  572. }
  573. /* increment our usage count for the driver */
  574. ++dev->opened;
  575. /* save our object in the file's private structure */
  576. file->private_data = dev;
  577. retval = 0;
  578. out:
  579. mutex_unlock(&dev->mutex);
  580. return retval;
  581. }
  582. /**
  583. * iowarrior_release
  584. */
  585. static int iowarrior_release(struct inode *inode, struct file *file)
  586. {
  587. struct iowarrior *dev;
  588. int retval = 0;
  589. dev = (struct iowarrior *)file->private_data;
  590. if (dev == NULL) {
  591. return -ENODEV;
  592. }
  593. dbg("%s - minor %d", __func__, dev->minor);
  594. /* lock our device */
  595. mutex_lock(&dev->mutex);
  596. if (dev->opened <= 0) {
  597. retval = -ENODEV; /* close called more than once */
  598. mutex_unlock(&dev->mutex);
  599. } else {
  600. dev->opened = 0; /* we're closeing now */
  601. retval = 0;
  602. if (dev->present) {
  603. /*
  604. The device is still connected so we only shutdown
  605. pending read-/write-ops.
  606. */
  607. usb_kill_urb(dev->int_in_urb);
  608. wake_up_interruptible(&dev->read_wait);
  609. wake_up_interruptible(&dev->write_wait);
  610. mutex_unlock(&dev->mutex);
  611. } else {
  612. /* The device was unplugged, cleanup resources */
  613. mutex_unlock(&dev->mutex);
  614. iowarrior_delete(dev);
  615. }
  616. }
  617. return retval;
  618. }
  619. static unsigned iowarrior_poll(struct file *file, poll_table * wait)
  620. {
  621. struct iowarrior *dev = file->private_data;
  622. unsigned int mask = 0;
  623. if (!dev->present)
  624. return POLLERR | POLLHUP;
  625. poll_wait(file, &dev->read_wait, wait);
  626. poll_wait(file, &dev->write_wait, wait);
  627. if (!dev->present)
  628. return POLLERR | POLLHUP;
  629. if (read_index(dev) != -1)
  630. mask |= POLLIN | POLLRDNORM;
  631. if (atomic_read(&dev->write_busy) < MAX_WRITES_IN_FLIGHT)
  632. mask |= POLLOUT | POLLWRNORM;
  633. return mask;
  634. }
  635. /*
  636. * File operations needed when we register this driver.
  637. * This assumes that this driver NEEDS file operations,
  638. * of course, which means that the driver is expected
  639. * to have a node in the /dev directory. If the USB
  640. * device were for a network interface then the driver
  641. * would use "struct net_driver" instead, and a serial
  642. * device would use "struct tty_driver".
  643. */
  644. static const struct file_operations iowarrior_fops = {
  645. .owner = THIS_MODULE,
  646. .write = iowarrior_write,
  647. .read = iowarrior_read,
  648. .unlocked_ioctl = iowarrior_ioctl,
  649. .open = iowarrior_open,
  650. .release = iowarrior_release,
  651. .poll = iowarrior_poll,
  652. };
  653. /*
  654. * usb class driver info in order to get a minor number from the usb core,
  655. * and to have the device registered with devfs and the driver core
  656. */
  657. static struct usb_class_driver iowarrior_class = {
  658. .name = "iowarrior%d",
  659. .fops = &iowarrior_fops,
  660. .minor_base = IOWARRIOR_MINOR_BASE,
  661. };
  662. /*---------------------------------*/
  663. /* probe and disconnect functions */
  664. /*---------------------------------*/
  665. /**
  666. * iowarrior_probe
  667. *
  668. * Called by the usb core when a new device is connected that it thinks
  669. * this driver might be interested in.
  670. */
  671. static int iowarrior_probe(struct usb_interface *interface,
  672. const struct usb_device_id *id)
  673. {
  674. struct usb_device *udev = interface_to_usbdev(interface);
  675. struct iowarrior *dev = NULL;
  676. struct usb_host_interface *iface_desc;
  677. struct usb_endpoint_descriptor *endpoint;
  678. int i;
  679. int retval = -ENOMEM;
  680. /* allocate memory for our device state and intialize it */
  681. dev = kzalloc(sizeof(struct iowarrior), GFP_KERNEL);
  682. if (dev == NULL) {
  683. dev_err(&interface->dev, "Out of memory\n");
  684. return retval;
  685. }
  686. mutex_init(&dev->mutex);
  687. atomic_set(&dev->intr_idx, 0);
  688. atomic_set(&dev->read_idx, 0);
  689. spin_lock_init(&dev->intr_idx_lock);
  690. atomic_set(&dev->overflow_flag, 0);
  691. init_waitqueue_head(&dev->read_wait);
  692. atomic_set(&dev->write_busy, 0);
  693. init_waitqueue_head(&dev->write_wait);
  694. dev->udev = udev;
  695. dev->interface = interface;
  696. iface_desc = interface->cur_altsetting;
  697. dev->product_id = le16_to_cpu(udev->descriptor.idProduct);
  698. /* set up the endpoint information */
  699. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  700. endpoint = &iface_desc->endpoint[i].desc;
  701. if (usb_endpoint_is_int_in(endpoint))
  702. dev->int_in_endpoint = endpoint;
  703. if (usb_endpoint_is_int_out(endpoint))
  704. /* this one will match for the IOWarrior56 only */
  705. dev->int_out_endpoint = endpoint;
  706. }
  707. /* we have to check the report_size often, so remember it in the endianess suitable for our machine */
  708. dev->report_size = le16_to_cpu(dev->int_in_endpoint->wMaxPacketSize);
  709. if ((dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) &&
  710. (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW56))
  711. /* IOWarrior56 has wMaxPacketSize different from report size */
  712. dev->report_size = 7;
  713. /* create the urb and buffer for reading */
  714. dev->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  715. if (!dev->int_in_urb) {
  716. dev_err(&interface->dev, "Couldn't allocate interrupt_in_urb\n");
  717. goto error;
  718. }
  719. dev->int_in_buffer = kmalloc(dev->report_size, GFP_KERNEL);
  720. if (!dev->int_in_buffer) {
  721. dev_err(&interface->dev, "Couldn't allocate int_in_buffer\n");
  722. goto error;
  723. }
  724. usb_fill_int_urb(dev->int_in_urb, dev->udev,
  725. usb_rcvintpipe(dev->udev,
  726. dev->int_in_endpoint->bEndpointAddress),
  727. dev->int_in_buffer, dev->report_size,
  728. iowarrior_callback, dev,
  729. dev->int_in_endpoint->bInterval);
  730. /* create an internal buffer for interrupt data from the device */
  731. dev->read_queue =
  732. kmalloc(((dev->report_size + 1) * MAX_INTERRUPT_BUFFER),
  733. GFP_KERNEL);
  734. if (!dev->read_queue) {
  735. dev_err(&interface->dev, "Couldn't allocate read_queue\n");
  736. goto error;
  737. }
  738. /* Get the serial-number of the chip */
  739. memset(dev->chip_serial, 0x00, sizeof(dev->chip_serial));
  740. usb_string(udev, udev->descriptor.iSerialNumber, dev->chip_serial,
  741. sizeof(dev->chip_serial));
  742. if (strlen(dev->chip_serial) != 8)
  743. memset(dev->chip_serial, 0x00, sizeof(dev->chip_serial));
  744. /* Set the idle timeout to 0, if this is interface 0 */
  745. if (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) {
  746. usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  747. 0x0A,
  748. USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
  749. 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
  750. }
  751. /* allow device read and ioctl */
  752. dev->present = 1;
  753. /* we can register the device now, as it is ready */
  754. usb_set_intfdata(interface, dev);
  755. retval = usb_register_dev(interface, &iowarrior_class);
  756. if (retval) {
  757. /* something prevented us from registering this driver */
  758. dev_err(&interface->dev, "Not able to get a minor for this device.\n");
  759. usb_set_intfdata(interface, NULL);
  760. goto error;
  761. }
  762. dev->minor = interface->minor;
  763. /* let the user know what node this device is now attached to */
  764. dev_info(&interface->dev, "IOWarrior product=0x%x, serial=%s interface=%d "
  765. "now attached to iowarrior%d\n", dev->product_id, dev->chip_serial,
  766. iface_desc->desc.bInterfaceNumber, dev->minor - IOWARRIOR_MINOR_BASE);
  767. return retval;
  768. error:
  769. iowarrior_delete(dev);
  770. return retval;
  771. }
  772. /**
  773. * iowarrior_disconnect
  774. *
  775. * Called by the usb core when the device is removed from the system.
  776. */
  777. static void iowarrior_disconnect(struct usb_interface *interface)
  778. {
  779. struct iowarrior *dev;
  780. int minor;
  781. dev = usb_get_intfdata(interface);
  782. mutex_lock(&iowarrior_open_disc_lock);
  783. usb_set_intfdata(interface, NULL);
  784. minor = dev->minor;
  785. /* give back our minor */
  786. usb_deregister_dev(interface, &iowarrior_class);
  787. mutex_lock(&dev->mutex);
  788. /* prevent device read, write and ioctl */
  789. dev->present = 0;
  790. mutex_unlock(&dev->mutex);
  791. mutex_unlock(&iowarrior_open_disc_lock);
  792. if (dev->opened) {
  793. /* There is a process that holds a filedescriptor to the device ,
  794. so we only shutdown read-/write-ops going on.
  795. Deleting the device is postponed until close() was called.
  796. */
  797. usb_kill_urb(dev->int_in_urb);
  798. wake_up_interruptible(&dev->read_wait);
  799. wake_up_interruptible(&dev->write_wait);
  800. } else {
  801. /* no process is using the device, cleanup now */
  802. iowarrior_delete(dev);
  803. }
  804. dev_info(&interface->dev, "I/O-Warror #%d now disconnected\n",
  805. minor - IOWARRIOR_MINOR_BASE);
  806. }
  807. /* usb specific object needed to register this driver with the usb subsystem */
  808. static struct usb_driver iowarrior_driver = {
  809. .name = "iowarrior",
  810. .probe = iowarrior_probe,
  811. .disconnect = iowarrior_disconnect,
  812. .id_table = iowarrior_ids,
  813. };
  814. static int __init iowarrior_init(void)
  815. {
  816. return usb_register(&iowarrior_driver);
  817. }
  818. static void __exit iowarrior_exit(void)
  819. {
  820. usb_deregister(&iowarrior_driver);
  821. }
  822. module_init(iowarrior_init);
  823. module_exit(iowarrior_exit);