iowarrior.c 26 KB

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