13

I'm asking this because I haven't found anything clear about this subject after searching for days. So, I'm sorry if the question already exists.

I'm working in a project, just for fun, where I want to control the PC mouse from the Raspberry Pi. The user input would be done by any kind of hardware connected to the RP, my main doubt is about how to control to send the movement instructions to the PC from the RP through USB.

I know that I have to enable the OTG mode in order to make the PC recognize it as a usb device, but I have no idea about how to send the mouse movements to the PC through the USB and make Windows / Linux understand it. I found that "Linux-USB Gadget API Framework" exists, but there is no example about using it, and I don't actually know if it is the best approach.

Could you please give me some clues or resources to help me?

Thank you very much.

4
  • Here's a place to start, I think: kernel.org/doc/Documentation/usb/gadget_hid.txt There's a hid_gadget_test program mentioned there, I do not have a pi handy but you might check apt-file search hid_gadget_test (and apt install apt-file first if necessary).
    – goldilocks
    Commented Oct 10, 2016 at 14:21
  • @goldilocks No, your link looks pretty much spot-on, it's definitely the right side of the fence. On the other side live regular HID drivers like usbkbd.ko Commented Oct 10, 2016 at 15:29
  • @goldilocks nah, in Linux, "gadget" always means running as slave, not host.
    – hobbs
    Commented Oct 10, 2016 at 17:47
  • @hobbs You're right -- I second guessed there because I couldn't remember which end g_ether is used from when tethering the zero as an ethernet gadget (but it is the slave side, the host side driver is the more generic usbnet).
    – goldilocks
    Commented Oct 10, 2016 at 22:31

1 Answer 1

7

Try using g_hid module mentioned by @goldilocks:

  1. Add a line dtoverlay=dwc2 to your config.txt and reboot
  2. Insert the driver driver with modprobe g_hid
  3. Run hid_gadget_test /dev/hidg0 keyboard. You may need to build the program first, using the sample code provided here.

Another, more flexible way to achieve that you want (without writing custom drivers) is to use gadgetfs. I know at least one project which turned a small ARM computer into USB/Bluetooth keyboard/mouse/gamepad using this approach.

Unfortunately, the status of gadgetfs on Rapsberry Pi is uncertain. This thread suggests it doesn't work, but it's unclear what the author tried to do exactly. I'd suggest to try it nevertheless:

  1. Add a line dtoverlay=dwc2 to your config.txt and reboot
  2. Insert the gadget FS driver with modprobe gadgetfs
  3. Mount the userspace filesystem with mkdir /dev/gadget; mount -t gadgetfs none /dev/gadget/

If you get no errors at this point, you'll be able to use gadgetfs libraries like this one to program your custom USB devices.

4
  • 1
    Thank you very much for your answer! I'll try both ways and let you know as soon as I can.
    – user54774
    Commented Oct 10, 2016 at 15:53
  • 1
    @Desproposito Did you ever get this working? I'd love an update
    – Allison
    Commented Mar 8, 2017 at 4:19
  • @Sirens, I'm sorry, buy I couldn't get it working :( But I'll try another approach as soon as I have time to try another ways.
    – user54774
    Commented Mar 23, 2017 at 6:59
  • @Desproposito Could you tell what exactly you couldn't get working? Were you able to modprobe the drivers? Could you mount gadgetfs? Could you build and run hid_gadget_test? Commented Mar 23, 2017 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.