HID 的 configfs,空 /sys/class/udc

HID 的 configfs,空 /sys/class/udc

目前正在为鼠标设备实现一个 usb hid。它完全按照预期在 raspberry pi 上工作,以在另一台电脑上模拟鼠标移动。实现类似于和这个详细实现:

#!/usr/bin/env bash

# Configures USB gadgets per: https://www.kernel.org/doc/Documentation/usb/gadget_configfs.txt

# Exit on first error.
set -e

# Echo commands to stdout.
set -x

# Treat undefined environment variables as errors.
set -u

modprobe libcomposite

# Adapted from https://github.com/girst/hardpass-sendHID/blob/master/README.md

cd /sys/kernel/config/usb_gadget/
mkdir -p g1
cd g1

echo 0x1d6b > idVendor  # Linux Foundation
echo 0x0104 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB    # USB2

STRINGS_DIR="strings/0x409"
mkdir -p "$STRINGS_DIR"
echo "6b65796d696d6570690" > "${STRINGS_DIR}/serialnumber"
echo "tinypilot" > "${STRINGS_DIR}/manufacturer"
echo "Multifunction USB Device" > "${STRINGS_DIR}/product"

# Keyboard
KEYBOARD_FUNCTIONS_DIR="functions/hid.keyboard"
mkdir -p "$KEYBOARD_FUNCTIONS_DIR"
echo 1 > "${KEYBOARD_FUNCTIONS_DIR}/protocol" # Keyboard
echo 1 > "${KEYBOARD_FUNCTIONS_DIR}/subclass" # Boot interface subclass
echo 8 > "${KEYBOARD_FUNCTIONS_DIR}/report_length"
# Write the report descriptor
# Source: https://www.kernel.org/doc/html/latest/usb/gadget_hid.html
D=$(mktemp)
echo -ne \\x05\\x01 >> "$D" # USAGE_PAGE (Generic Desktop)
echo -ne \\x09\\x06 >> "$D" # USAGE (Keyboard)
echo -ne \\xa1\\x01 >> "$D" # COLLECTION (Application)
echo -ne \\x05\\x07 >> "$D" #   USAGE_PAGE (Keyboard)
echo -ne \\x19\\xe0 >> "$D" #   USAGE_MINIMUM (Keyboard LeftControl)
echo -ne \\x29\\xe7 >> "$D" #   USAGE_MAXIMUM (Keyboard Right GUI)
echo -ne \\x15\\x00 >> "$D" #   LOGICAL_MINIMUM (0)
echo -ne \\x25\\x01 >> "$D" #   LOGICAL_MAXIMUM (1)
echo -ne \\x75\\x01 >> "$D" #   REPORT_SIZE (1)
echo -ne \\x95\\x08 >> "$D" #   REPORT_COUNT (8)
echo -ne \\x81\\x02 >> "$D" #   INPUT (Data,Var,Abs)
echo -ne \\x95\\x01 >> "$D" #   REPORT_COUNT (1)
echo -ne \\x75\\x08 >> "$D" #   REPORT_SIZE (8)
echo -ne \\x81\\x03 >> "$D" #   INPUT (Cnst,Var,Abs)
echo -ne \\x95\\x05 >> "$D" #   REPORT_COUNT (5)
echo -ne \\x75\\x01 >> "$D" #   REPORT_SIZE (1)
echo -ne \\x05\\x08 >> "$D" #   USAGE_PAGE (LEDs)
echo -ne \\x19\\x01 >> "$D" #   USAGE_MINIMUM (Num Lock)
echo -ne \\x29\\x05 >> "$D" #   USAGE_MAXIMUM (Kana)
echo -ne \\x91\\x02 >> "$D" #   OUTPUT (Data,Var,Abs)
echo -ne \\x95\\x01 >> "$D" #   REPORT_COUNT (1)
echo -ne \\x75\\x03 >> "$D" #   REPORT_SIZE (3)
echo -ne \\x91\\x03 >> "$D" #   OUTPUT (Cnst,Var,Abs)
echo -ne \\x95\\x06 >> "$D" #   REPORT_COUNT (6)
echo -ne \\x75\\x08 >> "$D" #   REPORT_SIZE (8)
echo -ne \\x15\\x00 >> "$D" #   LOGICAL_MINIMUM (0)
echo -ne \\x25\\x65 >> "$D" #   LOGICAL_MAXIMUM (101)
echo -ne \\x05\\x07 >> "$D" #   USAGE_PAGE (Keyboard)
echo -ne \\x19\\x00 >> "$D" #   USAGE_MINIMUM (Reserved)
echo -ne \\x29\\x65 >> "$D" #   USAGE_MAXIMUM (Keyboard Application)
echo -ne \\x81\\x00 >> "$D" #   INPUT (Data,Ary,Abs)
echo -ne \\xc0 >> "$D"      # END_COLLECTION
cp "$D" "${KEYBOARD_FUNCTIONS_DIR}/report_desc"

# Mouse
MOUSE_FUNCTIONS_DIR="functions/hid.mouse"
mkdir -p "$MOUSE_FUNCTIONS_DIR"
echo 0 > "${MOUSE_FUNCTIONS_DIR}/protocol"
echo 0 > "${MOUSE_FUNCTIONS_DIR}/subclass"
echo 5 > "${MOUSE_FUNCTIONS_DIR}/report_length"
# Write the report descriptor
D=$(mktemp)
echo -ne \\x05\\x01 >> "$D"      # USAGE_PAGE (Generic Desktop)
echo -ne \\x09\\x02 >> "$D"      # USAGE (Mouse)
echo -ne \\xA1\\x01 >> "$D"      # COLLECTION (Application)
                                 #   8-buttons
echo -ne \\x05\\x09 >> "$D"      #   USAGE_PAGE (Button)
echo -ne \\x19\\x01 >> "$D"      #   USAGE_MINIMUM (Button 1)
echo -ne \\x29\\x08 >> "$D"      #   USAGE_MAXIMUM (Button 8)
echo -ne \\x15\\x00 >> "$D"      #   LOGICAL_MINIMUM (0)
echo -ne \\x25\\x01 >> "$D"      #   LOGICAL_MAXIMUM (1)
echo -ne \\x95\\x08 >> "$D"      #   REPORT_COUNT (8)
echo -ne \\x75\\x01 >> "$D"      #   REPORT_SIZE (1)
echo -ne \\x81\\x02 >> "$D"      #   INPUT (Data,Var,Abs)
                                 #   x,y absolute coordinates
echo -ne \\x05\\x01 >> "$D"      #   USAGE_PAGE (Generic Desktop)
echo -ne \\x09\\x30 >> "$D"      #   USAGE (X)
echo -ne \\x09\\x31 >> "$D"      #   USAGE (Y)
echo -ne \\x16\\x00\\x00 >> "$D" #   LOGICAL_MINIMUM (0)
echo -ne \\x26\\xFF\\x7F >> "$D" #   LOGICAL_MAXIMUM (32767)
echo -ne \\x75\\x10 >> "$D"      #   REPORT_SIZE (16)
echo -ne \\x95\\x02 >> "$D"      #   REPORT_COUNT (2)
echo -ne \\x81\\x02 >> "$D"      #   INPUT (Data,Var,Abs)
                                 #   vertical wheel
echo -ne \\x09\\x38 >> "$D"      #   USAGE (wheel)
echo -ne \\x15\\x81 >> "$D"      #   LOGICAL_MINIMUM (-127)
echo -ne \\x25\\x7F >> "$D"      #   LOGICAL_MAXIMUM (127)
echo -ne \\x75\\x08 >> "$D"      #   REPORT_SIZE (8)
echo -ne \\x95\\x01 >> "$D"      #   REPORT_COUNT (1)
echo -ne \\x81\\x06 >> "$D"      #   INPUT (Data,Var,Rel)
                                 #   horizontal wheel
echo -ne \\x05\\x0C >> "$D"      #   USAGE_PAGE (Consumer Devices)
echo -ne \\x0A\\x38\\x02 >> "$D" #   USAGE (AC Pan)
echo -ne \\x15\\x81 >> "$D"      #   LOGICAL_MINIMUM (-127)
echo -ne \\x25\\x7F >> "$D"      #   LOGICAL_MAXIMUM (127)
echo -ne \\x75\\x08 >> "$D"      #   REPORT_SIZE (8)
echo -ne \\x95\\x01 >> "$D"      #   REPORT_COUNT (1)
echo -ne \\x81\\x06 >> "$D"      #   INPUT (Data,Var,Rel)
echo -ne \\xC0 >> "$D"           # END_COLLECTION
cp "$D" "${MOUSE_FUNCTIONS_DIR}/report_desc"

CONFIG_INDEX=1
CONFIGS_DIR="configs/c.${CONFIG_INDEX}"
mkdir -p "$CONFIGS_DIR"
echo 250 > "${CONFIGS_DIR}/MaxPower"

CONFIGS_STRINGS_DIR="${CONFIGS_DIR}/strings/0x409"
mkdir -p "$CONFIGS_STRINGS_DIR"
echo "Config ${CONFIG_INDEX}: ECM network" > "${CONFIGS_STRINGS_DIR}/configuration"

ln -s "$KEYBOARD_FUNCTIONS_DIR" "${CONFIGS_DIR}/"
ln -s "$MOUSE_FUNCTIONS_DIR" "${CONFIGS_DIR}/"
ls /sys/class/udc > UDC

chmod 777 /dev/hidg0
chmod 777 /dev/hidg1

然而,当在运行 Linux 的工业 PC 上实现类似的设置时,相同的设置不起作用。主要问题是 /sys/class/udc 是空的。对于 raspi,有一个 udc fe980000.usb。对于工业 PC,则不存在。来自其他各种论坛。树莓派有这个是因为dtoverlay = dwc2的配置与驱动有关。工控机上好像没有这样的驱动。

已探索/观察到以下内容:

  1. 确认 configfs 的内核配置设置没问题: CONFIG_USB_CONFIGFS_F_HID=y

  2. modprobe configfs 和 libcomposite 正在工作

  3. ls /dev 不显示 hidg0 或 hidg(number)

  4. 安装到 /sys/kernel/config/usb_gadget 可以按照创建文件夹的预期进行

唯一的问题/症状是 /dev/hidg0 没有出现,因此无法写入。与此同时,udc 为空,导致 bash 脚本在 ls /sys/class/udc > UDC 处失败

对于工业电脑,它运行在分销商 ID:Ubuntu 描述:Ubuntu 18.04.5 LTS 版本:18.04 代号:bionic 5.4.0-81-generic

提前致歉,因为我对 linux/ubuntu 还很陌生。任何帮助是极大的赞赏。

硬件编辑: 体系结构:x86_64 CPU 操作模式:32 位、64 位 字节顺序:Little Endian CPU:16 在线 CPU 列表:每个 0-15 个线程核心:2 每个插槽核心:8 插槽:1 NUMA 节点:1 供应商 ID:GenuineIntel CPU 系列:6 型号:158 型号名称:Intel(R) Core(TM) i9-9900T CPU @ 2.10GHz 步进:13 CPU MHz:899.790 CPU 最大 MHz:4400,0000 CPU 最小 MHz:800,0000 BogoMIPS:4199.88 虚拟化:VT-x L1d 缓存:32K L1i 缓存:32K L2 缓存:256K L3 缓存:16384K NUMA 节点0 CPU:0-15

lsusb 总线 002 设备 002:ID 1058:25ee Western Digital Technologies, Inc. 总线 002 设备 001:ID 1d6b:0003 Linux Foundation 3.0 根集线器 总线 001 设备 002:ID 0424:2514 Standard Microsystems Corp. USB 2.0 集线器总线 001 设备 001 : ID 1d6b:0002 Linux Foundation 2.0 根集线器

与 raspi 相比,有更多的 com 端口和 USB 端口

相关内容