title: read USB bulk message on Linux
body: <p>I am trying lessons on USB <a href="http://www.linuxforu.com/2011/12/data-transfers-to-from-usb-devices/" rel="nofollow">http://www.linuxforu.com/2011/12/data-transfers-to-from-usb-devices/</a> and stuck with the problem - while reading, usb_bulk_msg returns error 22 - Invalid argument. Write operation succeeds.</p>
  
  <pre><code>#include &lt;linux/module.h&gt;
  #include &lt;linux/kernel.h&gt;
  #include &lt;linux/usb.h&gt;
  
  #define MIN(a,b) (((a) &lt;= (b)) ? (a) : (b))
  #define BULK_EP_OUT 0x01
  #define BULK_EP_IN 0x82
  #define MAX_PKT_SIZE 512
  
  static struct usb_device *device;
  static struct usb_class_driver class;
  static unsigned char bulk_buf[MAX_PKT_SIZE];
  
  static int pen_open(struct inode *i, struct file *f)
  {
      return 0;
  }
  static int pen_close(struct inode *i, struct file *f)
  {
      return 0;
  }
  static ssize_t pen_read(struct file *f, char __user *buf, size_t cnt, loff_t *off)
  {
      int retval;
      int read_cnt;
  
      /* Read the data from the bulk endpoint */
      retval = usb_bulk_msg(device, usb_rcvbulkpipe(device, BULK_EP_IN),
              bulk_buf, MAX_PKT_SIZE, &amp;read_cnt, 5000);
      if (retval)
      {
          printk(KERN_ERR "Bulk message returned %d\n", retval);
          return retval;
      }
      if (copy_to_user(buf, bulk_buf, MIN(cnt, read_cnt)))
      {
          return -EFAULT;
      }
  
      return MIN(cnt, read_cnt);
  }
  static ssize_t pen_write(struct file *f, const char __user *buf, size_t cnt, loff_t *off)
  {
      int retval;
      int wrote_cnt = MIN(cnt, MAX_PKT_SIZE);
  
      if (copy_from_user(bulk_buf, buf, MIN(cnt, MAX_PKT_SIZE)))
      {
          return -EFAULT;
      }
  
      /* Write the data into the bulk endpoint */
      retval = usb_bulk_msg(device, usb_sndbulkpipe(device, BULK_EP_OUT),
              bulk_buf, MIN(cnt, MAX_PKT_SIZE), &amp;wrote_cnt, 5000);
      if (retval)
      {
          printk(KERN_ERR "Bulk message returned %d\n", retval);
          return retval;
      }
  
      return wrote_cnt;
  }
  
  static struct file_operations fops =
  {
      .open = pen_open,
      .release = pen_close,
      .read = pen_read,
      .write = pen_write,
  };
  
  static int pen_probe(struct usb_interface *interface, const struct usb_device_id *id)
  {
      int retval;
  
      device = interface_to_usbdev(interface);
  
      class.name = "usb/pen%d";
      class.fops = &amp;fops;
      if ((retval = usb_register_dev(interface, &amp;class)) &lt; 0)
      {
          /* Something prevented us from registering this driver */
          err("Not able to get a minor for this device.");
      }
      else
      {
          printk(KERN_INFO "Minor obtained: %d\n", interface-&gt;minor);
      }
  
      return retval;
  }
  
  static void pen_disconnect(struct usb_interface *interface)
  {
      usb_deregister_dev(interface, &amp;class);
  }
  
  /* Table of devices that work with this driver */
  static struct usb_device_id pen_table[] =
  {
      { USB_DEVICE(0x058F, 0x6387) },
      {} /* Terminating entry */
  };
  MODULE_DEVICE_TABLE (usb, pen_table);
  
  static struct usb_driver pen_driver =
  {
      .name = "pen_driver",
      .probe = pen_probe,
      .disconnect = pen_disconnect,
      .id_table = pen_table,
  };
  
  static int __init pen_init(void)
  {
      int result;
  
      /* Register this driver with the USB subsystem */
      if ((result = usb_register(&amp;pen_driver)))
      {
          err("usb_register failed. Error number %d", result);
      }
      return result;
  }
  
  static void __exit pen_exit(void)
  {
      /* Deregister this driver with the USB subsystem */
      usb_deregister(&amp;pen_driver);
  }
  
  module_init(pen_init);
  module_exit(pen_exit);
  
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("Anil Kumar Pugalia &lt;email_at_sarika-pugs_dot_com&gt;");
  MODULE_DESCRIPTION("USB Pen Device Driver");  
  </code></pre>