USB HID Gadget

15 6666
Edited by Talion12 at Fri Sep 18, 2015 07:50

Hi Community,

I have a project with some other guys, where we want to create a mobile HID-touch device with the Banana PI.

At the moment we are struggeling with the USB-HID configuration, because the final solution should work as a HID-Keyboard, so if you plug the Banana into a PC with an USB cable, the PC should recognize it as an HID-Keyboard.

Touch, Display and Power Pack is working fine. We also have a solution for the HID-configuration. At the moment we are using an Arduino which is working as an GPIO-HID converter, and its working fine, but this solution needs a lot of space in our mobile case.

After some research, I stumbled over the USB HID Gadget:

After that i was looking through the bananian OS and i found the g_hid.ko which, if i`m correct, is able to act as an ubs-hid keyboard if you use the usb otg port.

I found a lot of guides, especially for android smartphones, where people were able to use their devices as a keyboard.
But for now, i'm not able to adapt their settings for the banana pi and i'm wondering if anyone of the banana pi community have tested this and could guide me through the settings?

TLDR: I would like to use the Banana as a HID-keyboard via USB OTG and need help to setup the USB HID gadget and looking for someone who could guide me through this.


Edit: I have a Banana PI and Banana PI pro.

The more i read about usb hid gadget, the more i get confused.

Maybe someone could answer some of these questions which would help me to understand a little bit better:

1. I have enabled the USB OTG in the script.bin, but i`m not sure how to test this connection. I´ve tried to plug the banana via USB OTG into my Windows client to see if anything happens, but nothing. Any suggestions?

2. When i do a modprobe on the g_hid.ko it asks for some parameters of an USB device. I´m not sure which device this g_hid driver is asking for? I´ve tried the parameters from the USB root hub, but i got the message "No such device". Maybe someone could enlighten me.


I did some more research and found out, that i have to create a platform device which is called hidg. If this device exists, the g_hid.ko will be loadable.

So how do i create a platform device?

I have downloaded the bananian kernel source and i was looking through the file device.c.

This file configures the platform_devices and i added the code from

Compiling the kernel was successfull (without any errors) and i put the uImage to the SD card, but there was no new hidg-device after rebooting.

Some internet sites also mentioned, that this code has to be put into the hid.c, which is the config-file for the g_hid.ko. ... 6fd133577293984ca45

I only added the green part into the existing bananian hid.c.

I was able to compile this module with these changes without any errors, but still inserting this module was not possible (No such device)

So i have a question:
Could somebody guide me through the creation of a platform_device within the bananian enviroment?

I am trying to do something similar but with a Banana Pi M2. I haven't figured it out yet either. But here's what I already know (which could be wrong or incomplete):
  • g_hid is the driver. The driver talks to the kernel to drive a device. What and where is this device?
  • The kernel learns about this device through a structure called "platform_devices"
  • Go to [1], and copy the two structures
    1. static struct hidg_func_descriptor my_hid_data
    Copy the Code
    1. static struct platform_device my_hid
    Copy the Code
    into hid.c and also this [2]
    1. int status;
    2. status = platform_device_register(&my_hid);
    3. if (status < 0)
    4. {
    5.         platform_driver_unregister(&my_hid);
    6.         return status;
    7. }
    Copy the Code

Now compile and load the module. You can send keystrokes to it using /dev/hidg0 by using the test program hid_gadget_test

What I haven't figured out:
  • Format of data to be sent to /dev/hidg0 when I want to use my pwn program and not hid_gadget_test
  • How to send data to /dev/hidg0 based on GPIO status
  • What exactly the parameters in hidg_func_descriptor my_hid_data   mean


so have you been successful in loading the module?

Edited by Talion12 at Dec 20, 2015 13:26

Thanks for your reply.

I would also like to know if you were able to load the module deadmetaphor?

I will try your settings in the next couple of days, but i´m sure that i had already did this without any success before.

I have one problem, BPi M2's Android refuses to boot when I choose the gadget function as 'hid'. I tried digging around and it seems that all the files required by "android composite gadget" (things like mass storage etc.) have certain BPi M2 specific code and f_hid.c doesn't. So, for the time being I am assuming, that the kernel sources haven't been edited to support *all* gadget functions, just the ones required by Android's default function.

In the mean time, I found this android keyboard and will be trying to out.
I also bought an Arduino Leonardo, to see if I can interface a BPi or RPi to it over SPI and make it act like a USB peripheral.

Hello Talion12,

I got /dev/hidg0 working using "libcomposite" and "configfs" and I was able to send keystrokes to the host PC using the example test application included in the kernel tree.

Which kernel version are you using? If it is >3.19 (I assume) you are in luck

deadmetaphor replied at Jan 05, 2016 23:13
Hello Talion12,

I got /dev/hidg0 working using "libcomposite" and "configfs" and I was able to send ...

Hi deadmetaphor,

Wow great!

The problem is, that we are using the bananian linux for our project and starting with a new kernel will be a lot of work i think. But i will look through it.

I have also read about the libcomposite and configfs and it seems to be pretty easy, but it would be great if you could write a short summary about what you did with the configfs.

Did you also read about the gadgetfs? Isn't similar to configfs?

I have also done some progress with compiling the g_hid module. I was able to create the platform device /hidg, but still i get an error when loading the module (No such device). I'm still on it. At the moment i'm trying this guide

deadmetaphor replied at Jan 05, 2016 23:13
Hello Talion12,

I got /dev/hidg0 working using "libcomposite" and "configfs" and I was able to send ...

Hi deadmetaphor,
I'm trying to do something similar. I would like to enable configfs/functionfs. Could you please write some more specifics..Like what options you enabled in menuconfig and which modules you loaded? Is it possible to do it with the mainline kernel?

You have to log in before you can reply Login | Sign Up

Points Rules