Click here to Skip to main content
15,791,440 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All

I'm currently working on a touchscreen keyboard project where the BBB displays images on a screen, receives the touch events form the touch panel and has to send them to the main PC as key events. So the BBB has to present it self as a HID (keyboard) device to the PC.

What I have to do is, write a program that runs on the BeagleBone that has to do the following things:
- Takes the images from its FLASH memory and arranges them on the screen;
- Captures the output of the touch panel and translates them into proper key events.
- And lastly, it has to connect to the USB Device controller and send those key events to the PC.

The hardware is present. The BBB has USB On-The-Go, which can be used as USB device controller. It's configured as USB Ethernet device and mass storage device by default and those functions do work.

So far I've written a script that configures the USB OTG device as HID, but now what?!
I really don't know how to continue. I'm not even sure that my script configures everything as it should. The examples (of such scripts) I looked from, were somewhat outdated.

The script fills in data like VID and PID into the files created by the configfs and creates some new files and directories as per the examples I've seen. Only the last command fails: ls: write error: Device or resource busy This is where the desired kernel module has to be mentioned so that configfs loads it. But the module (usb_f_hid) does get loaded! So I thought that error is not a big deal?!

I was expecting that a device file would appear in /dev after the script is ran, so I can open it and start communicating with the USB device controller. But I was not able to find such a device file. it has to be named "usb0" but no device filed with "usb" prefix are present before or after the script is ran.

The BeagleBone runs Debian Linux beaglebone 4.4.14-bone11 #1 Thu Jul 14 08:51:47 EEST 2016 armv7l GNU/Linux

Can you point me to some resources that explain the process of using BBB as HID or something of the sort.

Also how can I remove the USB network functionality properly?

Thanks a lot in advance.

What I have tried:

This is the entire script. It's executed as root.
The configfs is already mounted and the libcomposite module is loaded at startup so I don't have to do that in the script.
mkdir /sys/kernel/config/usb_gadget/usb_keyboard

mkdir /sys/kernel/config/usb_gadget/usb_keyboard/configs/c.1
mkdir /sys/kernel/config/usb_gadget/usb_keyboard/functions/hid.usb0

echo "1" > /sys/kernel/config/usb_gadget/usb_keyboard/functions/hid.usb0/protocol                 # set the HID protocol
echo "1" > /sys/kernel/config/usb_gadget/usb_keyboard/functions/hid.usb0/subclass                 # set the device subclass
echo "8" > /sys/kernel/config/usb_gadget/usb_keyboard/functions/hid.usb0/report_length            # REPORT_LENGTH = 8  set the byte length of HID reports
cat /home/debian/report_desc.bin > /sys/kernel/config/usb_gadget/usb_keyboard/functions/hid.usb0/report_desc

mkdir /sys/kernel/config/usb_gadget/usb_keyboard/strings/0x409
mkdir /sys/kernel/config/usb_gadget/usb_keyboard/configs/c.1/strings/0x409

echo "0x37f2" > /sys/kernel/config/usb_gadget/usb_keyboard/idVendor
echo "0x0001" > /sys/kernel/config/usb_gadget/usb_keyboard/idProduct
echo "0x03"   > /sys/kernel/config/usb_gadget/usb_keyboard/bDeviceClass

echo "71cb32dc0e49d782e9d1"       > /sys/kernel/config/usb_gadget/usb_keyboard/strings/0x409/serialnumber
echo "**** ***** Technology Ltd." > /sys/kernel/config/usb_gadget/usb_keyboard/strings/0x409/manufacturer
echo "*** Keyboard"               > /sys/kernel/config/usb_gadget/usb_keyboard/strings/0x409/product
echo "Custom USB Keyboard"        > /sys/kernel/config/usb_gadget/usb_keyboard/configs/c.1/strings/0x409/configuration
echo "120"                        > /sys/kernel/config/usb_gadget/usb_keyboard/configs/c.1/MaxPower

ln -s /sys/kernel/config/usb_gadget/usb_keyboard/functions/hid.usb0 /sys/kernel/config/usb_gadget/usb_keyboard/configs/c.1

ls /sys/class/udc/ > /sys/kernel/config/usb_gadget/usb_keyboard/UDC #This fails with "ls: write error: Device or resource busy"

The contetnts of /sys/class/udc/ are not what they're supposed to be as per the instructions for configfs HID setup. There sould be a list of modules to be loaded. Instead, there is this "" simlink.

Also I googled my a** off trying to find some more info on the matter but nothing comes out.
Updated 28-Sep-17 3:29am

I don't know if you still need this, but I found the solution to your problem.

I'm just starting a project where I need to do the same.

The BBB sets up the USB clients in /opt/scripts/boot/
And it seems that after this, you can no longer disable the UDC in order to install your gadget.

So I disabled the Mass Storage Device and the USB Network in /etc/default/bb-boot and also commented 2 lines in /opt/scripts/boot/ to disable the ACM (USB Serial).
Make sure you connect via ethernet and know the IP before you reboot :-)
After that, you should be able to disable the UDC with
cd /sys/kernel/config/usb_gadget/g_multi
echo "" > UDC
and run your script.

Hope this helps.
Share this answer
Ivan Ivanov 83 3-Feb-19 6:43am    
Yes editing is absolutely the way to configure BBBs gadgets. It gives you control over almost all peripheries.
I have not done such so far. So this might not help.

As far as I understand, /sys/class/udc/ is created / filled by the preceding script commands (does not exist before or is empty). So I would wait some time (sleep) before accessing that directory (not busy anymore).

You can also do the last step manually. That is execute
ls /sys/class/udc/
pick a device name, and write that to the UDC file using echo.

If the directory contains multiple files that should be all used I would also expect a 'ls -1 dir' command to get a newline separated list.
Share this answer
Ivan Ivanov 83 28-Sep-17 8:56am    
Thank you for your reply :)

"As far as I understand, /sys/class/udc/ is created / filled by the preceding script commands (does not exist before or is empty)"
- That could very well be the case. However the content of that directory is the same before and after the script is ran. Ether the commands in the script don't affect the content or they're not setting everything properly.

I did try to echo the contents of /sys/class/udc/ (namely into the UDC file but I get the same error every time. (That's after the script is ran)

So, are you familiar with programming BeagleBone, if you don't mind me asking? :)
Jochen Arndt 28-Sep-17 9:13am    
Not with the BB but with the Pi. But both use Debian so that there are many analogies.

Unfortunately I have not used USB gadgets so far. So I actually don't know what is going on in detail. But you are writing to /sys/kernel/config/usb_gadget/usb_keyboard/ before. When you know the device name and it does not change, you can try to set it a little bit earlier (or try to access UDC earlier) to know when that becomes busy.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900