USB_HID_Serial
hid_serial_device_config.sh
#!/bin/sh
#
# Start hid....
#
UDC=`ls /sys/class/udc/`
# $UDC=13500000.otg
prg_name=$0
status=$1
device=$2
# 拼接数据(0x11 0x22 0x33 ===> \\x11\\x22\\x33)
echo_hex() {
    local data
    for i in $*; do
        i=${i#0x}
        i=${i#0X}
        data=$data"\\x"$i
    done
    echo -ne $data
}
# 配置hid设备
hid_serial_start() {
    echo "Creating the USB gadget..."
    mount -t configfs none /sys/kernel/config
    mkdir /sys/kernel/config/usb_gadget/hid_serial_demo
    cd /sys/kernel/config/usb_gadget/hid_serial_demo
    # 配置设备描述符
    echo "Setting Device Descriptor..."
    echo "0xef" > bDeviceClass
    echo "0x02" > bDeviceSubClass
    echo "0x01" > bDeviceProtocol
    echo "0x0110" > bcdUSB
    echo "0x0100" > bcdDevice
    # 此id根据用户产品进行设置,此id仅作为测试
    echo "0x0525" > idVendor
    echo "0xb4b1" > idProduct
    #配置字符串描述符
    echo "Setting English strings..."
    mkdir strings/0x409
    echo "INGENIC" > strings/0x409/manufacturer
    echo "Gadget Serial and Hid" > strings/0x409/product
    echo "ingenic" > strings/0x409/serialnumber
    #配置配置描述符
    echo "Creating Config..."
    mkdir configs/c.1
    echo "120" > configs/c.1/MaxPower
    echo "0x80" > configs/c.1/bmAttributes
    mkdir configs/c.1/strings/0x409
    echo "INGENIC" > configs/c.1/strings/0x409/configuration
    #配置acm功能描述符
    echo "Creating acm functions..."
    mkdir functions/acm.0
    # 配置报表描述符
    echo "Creating hid functions..."
    mkdir functions/hid.0
    if [ "$device" = "serial_keyboard" ]; then
        echo "0" > functions/hid.0/subclass
        echo "1" > functions/hid.0/protocol
        echo "8" > functions/hid.0/report_length
        # 键盘桌面设备定义
        keyboard_device="0x05 0x01 0x09 0x06 0xa1 0x01"
        # 键盘修饰键(Ctrl Alt Shift Windows Space..等)功能
        keyboard_modify="0x05 0x07 0x19 0xe0 0x29 0xe7 0x15 0x00 0x25 0x01 0x75 0x01 0x95 0x08 0x81 0x02 0x95 0x01 0x75 0x08 0x81 0x03"
        # 键盘LED灯功能
        keyboard_led="0x95 0x05 0x75 0x01 0x05 0x08 0x19 0x01 0x29 0x05 0x91 0x02 0x95 0x01 0x75 0x03 0x91 0x03"
        # 键盘主按键(a~z 0~9 F1~F12..等)功能
        keyboard_main="0x95 0x06 0x75 0x08 0x15 0x00 0x25 0x65 0x05 0x07 0x19 0x00 0x29 0x65 0x81 0x00"
        # 结束
        keyboard_end="0xc0"
        echo_hex "$keyboard_device $keyboard_modify $keyboard_led $keyboard_main $keyboard_end" > functions/hid.0/report_desc
    fi
    if [ "$device" = "serial_mouse" ]; then
        echo "0" > functions/hid.0/subclass
        echo "2" > functions/hid.0/protocol
        echo "4" > functions/hid.0/report_length
        # 鼠标桌面设备定义
        mouse_device="0x05 0x01 0x09 0x02 0xa1 0x01"
        # 鼠标按键(左键 右键 中键)功能
        mouse_key=" 0x09 0x01 0xa1 0x00 0x05 0x09 0x19 0x01 0x29 0x03 0x15 0x00 0x25 0x01 0x75 0x01 0x95 0x03 0x81 0x02 0x75 0x05 0x95 0x01 0x81 0x01"
        # 鼠标滑轮功能
        mouse_pulley="0x05 0x01 0x09 0x30 0x09 0x31 0x09 0x38 0x15 0x81 0x25 0x7f 0x75 0x08 0x95 0x03 0x81 0x06"
        # 结束
        mouse_end="0xc0 0xc0"
        echo_hex "$mouse_device $mouse_key $mouse_pulley $mouse_end" > functions/hid.0/report_desc
    fi
    ln -s functions/acm.0 configs/c.1
    ln -s functions/hid.0 configs/c.1
    echo $UDC > UDC
}
# 卸载serial设备
hid_serial_stop() {
    echo "stopping the USB gadget"
    cd /sys/kernel/config/usb_gadget/hid_serial_demo
    echo "Unbinding USB Device controller..."
    echo "" > UDC
    echo "Deleting HID SERIAL gadget functionality : hid.0 acm.0"
    rm configs/c.1/hid.0
    rmdir functions/hid.0
    rm configs/c.1/acm.0
    rmdir functions/acm.0
    echo "Cleaning up configuration..."
    rmdir configs/c.1/strings/0x409
    rmdir configs/c.1
    echo "cleaning English string..."
    rmdir strings/0x409
    echo "Removing gadget directory..."
    cd -
    rmdir /sys/kernel/config/usb_gadget/hid_serial_demo/
    umount /sys/kernel/config
}
case "$status" in
  start)
    if [ "$#" != "2" ]; then
        echo "Usage: $prg_name start {serial_keyboard|serial_mouse}"
        exit 1
    fi
    if [ "$device" != "serial_keyboard" ] && [ "$device" != "serial_mouse" ]; then
        echo "Usage: $prg_name start {serial_keyboard|serial_mouse}"
        exit 1
    fi
    if [ -d /sys/kernel/config/usb_gadget ]; then
        echo "Error: usb configfs already mounted"
        exit 1
    fi
    hid_serial_start
    ;;
  stop)
    if [ "$#" != "1" ]; then
        echo "Usage: $prg_name stop"
        exit 1
    fi
    if [ ! -d /sys/kernel/config/usb_gadget/hid_serial_demo ]; then
        echo "Error: usb configfs hid_serial_demo uninitialized"
        exit 1
    fi
    hid_serial_stop
    ;;
  *)
    echo "Usage1: $prg_name start {serial_keyboard|serial_mouse}"
    echo "Usage2: $prg_name stop"
    exit 1
esac
exit $?