This is a cache of https://discuss.96boards.org/t/usb-gadget-configfs/5230. It is a snapshot of the page at 2024-10-31T08:54:28.921+0000.
USB Gadget ConfigFS - DragonBoard 820c - 96Boards Forum

USB Gadget ConfigFS

I’m trying to work through a usb gadget configfs example (keyboard hid) on the 820C dragonboard.

I am hoping to use the OTG USB as the device USB port. I am unable to make the functions/hid.usb0 directory (see sequence below).

Anyone had success using usb gadget configfs on the 410c or 820c for any sort of USB device?

This initial part of the configuration works as expected -

$ su
// password entry
$ modprobe libcomposite
// configfs mounted on /sys/kernel/config
// /sys/kernel/usbgadget/usb_gadget exists
$ mkdir /sys/kernel/config/usb_gadget/keyboardgadget
// directory created
$ cd /sys/kernel/config/usb_gadget/keyboardgadget
$ mkdir configs/c.1
// directory created

The next directory creation step fails -

$ mkdir functions/hid.usb0
mkdir: cannot create directory ‘functions.hid.usb0’: no such file or directory

// rfs
http://snapshots.linaro.org/96boards/dragonboard820c/linaro/debian/222/
Kernel package version: 4.14.15-00320-g4579f43d0d88-36
// kernel
$ git clone working/qualcomm/kernel.git - Qualcomm Landing Team kernel
git checkout 4579f43d0d88

Thanks

I am not sure if this is the issue but, as you have the kernel source, look at your .config

# CONFIG_USB_CONFIGFS_F_HID is not set

From make menuconfig:

Symbol: USB_CONFIGFS_F_HID [=n]                                         │  
  │ Type  : boolean                                                         │  
  │ Prompt: HID function                                                    │  
  │   Location:                                                             │  
  │     -> Device Drivers                                                   │  
  │       -> USB support (USB_SUPPORT [=y])                                 │  
  │         -> USB Gadget Support (USB_GADGET [=y])                         │  
  │ (1)       -> USB Gadget functions configurable through configfs (USB_CO │  
  │   Defined at drivers/usb/gadget/Kconfig:435                             │  
  │   Depends on: USB_SUPPORT [=y] && USB_GADGET [=y] && USB_CONFIGFS [=m]  │  
  │   Selects: USB_F_HID [=n]                                               │  
  │                                                                         │

Thanks kldixon,

Gadget functions are enabled as a loadable module and FunctionFS is also enabled.

That is the default config setting and is the same on my machine. The point is that the HID function is not enabled, which was exactly where the script failed.

Thanks kldixon,

I added HID function under “USB Gadget functions configurable through configfs” and added HID Gadget (as module) as well. I had to configure “USB Gadget functions configurable through configfs” as a built-in in order to be able to write to functions without errors.

I was able to login as su and issue the following configuration commands without any errors (following the example).

Do you know what I should be expecting on the Host side of the USB connection. I don’t see any enumeration reported in lsusb on my host PC.

MANUFACTURER=“Some Company” # manufacturer attribute
SERIAL=“Frosted Flakes” # device serial number
IDPRODUCT=“0xa4ac” # hex product ID, issued by USB Group
IDVENDOR=“0x0525” # hex vendor ID, assigned by USB Group
PRODUCT=“Emulated HID Keyboard” # cleartext product description
CONFIG_NAME=“Configuration 1” # name of this configuration
MAX_POWER_MA=120 # max power this configuration can consume in mA
PROTOCOL=1 # 1 for keyboard. see usb spec
SUBCLASS=1 # it seems either 1 or 0 works, dunno why
REPORT_LENGTH=8 # number of bytes per report
DESCRIPTOR=/config/usb_gadget/keyboardgadget/kybd-descriptor.bin # binary blob of report descriptor, see HID class spec
UDC=ci_hdrc.0 # name of the UDC driver to use (found in /sys/class/udc/)

// not required if built-in - modprobe libcomposite # load configfs
// configfs already mounted on /sys/kernel/config - mount none /sys/kernel/config -t configfs
mkdir /sys/kernel/config/usb_gadget/keyboardgadget
cd /config/usb_gadget/keyboardgadget # cd to gadget dir
mkdir configs/c.1 # make the skeleton for a config for this gadget
mkdir functions/hid.usb0 # add hid function (will fail if kernel < 3.19, which hid was added in)
// cannot write when configfs build as module
// can write when configfs build in
echo $PROTOCOL > functions/hid.usb0/protocol # set the HID protocol
echo $SUBCLASS > functions/hid.usb0/subclass # set the device subclass
echo $REPORT_LENGTH > functions/hid.usb0/report_length # set the byte length of HID reports
cat $DESCRIPTOR > functions/hid.usb0/report_desc # write the binary blob of the report descriptor to report_desc; see HID class spec
mkdir strings/0x409 # setup standard device attribute strings
mkdir configs/c.1/strings/0x409
echo $IDPRODUCT > idProduct
echo $IDVENDOR > idVendor
echo $SERIAL > strings/0x409/serialnumber
echo $MANUFACTURER > strings/0x409/manufacturer
echo $PRODUCT > strings/0x409/product
echo $CONFIG_NAME > configs/c.1/strings/0x409/configuration
echo $MAX_POWER_MA > configs/c.1/MaxPower
ln -s functions/hid.usb0 configs/c.1 # put the function into the configuration by creating a symlink

CONNECT USB CABLE TO HOST

// binding
// echo $UDC > UDC
ls /sys/class/udc > UDC

Do you have any USB related kernel message (dmesg) ?

Hi Loic,

kernel message on the host PC during boot related to USB are (with USB mouse and keyboard attached)

[ 0.233518] usbcore: registered new interface driver usbfs
[ 0.233552] usbcore: registered new interface driver hub
[ 0.233615] usbcore: registered new device driver usb

[ 3.858538] usbcore: registered new interface driver rtl8150
[ 3.861383] usbcore: registered new interface driver r8152
[ 3.867154] usbcore: registered new interface driver asix
[ 3.872443] usbcore: registered new interface driver ax88179_178a

[ 3.896114] ehci_hcd: USB 2.0 ‘Enhanced’ Host Controller (EHCI) Driver

[ 3.924016] ohci_hcd: USB 1.1 ‘Open’ Host Controller (OHCI) Driver

[ 3.946297] usbcore: registered new interface driver usb-storage
[ 3.949363] usbcore: registered new interface driver usbserial
[ 3.955603] usbcore: registered new interface driver usbserial_generic
[ 3.961216] usbserial: USB Serial support registered for generic

[ 4.127317] usbcore: registered new interface driver usbhid
[ 4.131626] usbhid: USB HID core driver
[ 4.140021] usbcore: registered new interface driver snd-usb-audio

[ 4.180225] qcom-qusb2-phy 7411000.phy: Registered Qcom-QUSB2 phy
[ 4.180657] qcom-qusb2-phy 7412000.phy: Registered Qcom-QUSB2 phy

[ 4.537035] xhci-hcd xhci-hcd.0.auto: new USB bus registered, assigned bus number 1

[ 4.577032] hub 1-0:1.0: USB hub found
[ 4.580370] hub 1-0:1.0: 1 port detected
[ 4.584381] xhci-hcd xhci-hcd.0.auto: xHCI Host Controller
[ 4.588237] xhci-hcd xhci-hcd.0.auto: new USB bus registered, assigned bus number 2
[ 4.593610] usb usb2: We don’t know the algorithms for LPM for this host, disabling LPM.
[ 4.601601] hub 2-0:1.0: USB hub found

[ 4.918089] usb 1-1: new high-speed USB device number 2 using xhci-hcd
[ 5.073970] hub 1-1:1.0: USB hub found
[ 5.074698] hub 1-1:1.0: 4 ports detected
[ 5.194407] usb 2-1: new SuperSpeed USB device number 2 using xhci-hcd
[ 5.234226] hub 2-1:1.0: USB hub found
[ 5.234586] hub 2-1:1.0: 4 ports detected

[ 5.386154] usb 1-1.1: new high-speed USB device number 3 using xhci-hcd
[ 5.522007] hub 1-1.1:1.0: USB hub found
[ 5.522328] hub 1-1.1:1.0: 4 ports detected
[ 5.814116] usb 1-1.1.1: new low-speed USB device number 4 using xhci-hcd
[ 5.942868] input: CHICONY USB Keyboard as /devices/platform/soc/soc:usb@6a00000/6a00000.dwc3/xhci-hcd.0.auto/usb1/1-1/1-1.1/1-1.1.1/1-1.1.1:1.0/0003:04F2:0833.0001/input/input1
[ 6.002594] hid-generic 0003:04F2:0833.0001: input: USB HID v1.11 Keyboard [CHICONY USB Keyboard] on usb-xhci-hcd.0.auto-1.1.1/input0
[ 6.011175] input: CHICONY USB Keyboard as /devices/platform/soc/soc:usb@6a00000/6a00000.dwc3/xhci-hcd.0.auto/usb1/1-1/1-1.1/1-1.1.1/1-1.1.1:1.1/0003:04F2:0833.0002/input/input2
[ 6.070212] hid-generic 0003:04F2:0833.0002: input: USB HID v1.11 Device [CHICONY USB Keyboard] on usb-xhci-hcd.0.auto-1.1.1/input1
[ 6.150140] usb 1-1.1.2: new low-speed USB device number 5 using xhci-hcd
[ 6.262225] input: USB Optical Mouse as /devices/platform/soc/soc:usb@6a00000/6a00000.dwc3/xhci-hcd.0.auto/usb1/1-1/1-1.1/1-1.1.2/1-1.1.2:1.0/0003:045E:0797.0003/input/input3
[ 6.262752] hid-generic 0003:045E:0797.0003: input: USB HID v1.11 Mouse [USB Optical Mouse] on usb-xhci-hcd.0.auto-1.1.2/input0

I do not see any kernel logs when running the configuration commands. I see the following kernel logs when removing and inserting the usb otg

remove usb otg

[ 1084.424972] xhci-hcd xhci-hcd.2.auto: xHCI Host Controller
[ 1084.425053] xhci-hcd xhci-hcd.2.auto: new USB bus registered, assigned bus number 3
[ 1084.442548] xhci-hcd xhci-hcd.2.auto: hcc params 0x0220f665 hci version 0x100 quirks 0x02010010
[ 1084.442594] xhci-hcd xhci-hcd.2.auto: irq 178, io mem 0x07600000
[ 1084.450520] hub 3-0:1.0: USB hub found
[ 1084.456376] hub 3-0:1.0: 1 port detected
[ 1084.460116] xhci-hcd xhci-hcd.2.auto: xHCI Host Controller
[ 1084.463962] xhci-hcd xhci-hcd.2.auto: new USB bus registered, assigned bus number 4
[ 1084.469289] usb usb4: We don’t know the algorithms for LPM for this host, disabling LPM.
[ 1084.477241] hub 4-0:1.0: USB hub found
[ 1084.485169] hub 4-0:1.0: config failed, hub doesn’t have any ports! (err -19)

insert usb otg

[ 1128.357731] xhci-hcd xhci-hcd.2.auto: remove, state 4
[ 1128.357810] usb usb4: USB disconnect, device number 1
[ 1128.364597] xhci-hcd xhci-hcd.2.auto: USB bus 4 deregistered
[ 1128.367253] xhci-hcd xhci-hcd.2.auto: remove, state 4
[ 1128.372839] usb usb3: USB disconnect, device number 1
[ 1128.377998] xhci-hcd xhci-hcd.2.auto: USB bus 3 deregistered

I have also modified arch/arm64/boot/dts/qcom/apq8096-db820c.dtsi

	usb@7600000 {
		status = "okay";

		dwc3@7600000 {
			extcon = <&usb2_id>;
			dr_mode = "peripheral"; // was dr_mode = "otg";
			maximum-speed = "high-speed";
		};
	};

The last step in the configuration process is to enable the USD HID device (ls /sys/class/udc > UDC). Before I run this I have to connect the usb otg to the host to get the usb controller registered in /sys/class/uvc. If everything is configured correctly, do you know if I should see a host enumeration after I issue the “$ ls /sys/class/udc > UDC” command?