iowarrior.c 26 KB

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