1#!/bin/sh 2 3hid_conf_directory="/sys/kernel/config/usb_gadget/obmc_hid" 4dev_name="1e6a0000.usb-vhub" 5 6create_hid() { 7 # create gadget 8 mkdir "${hid_conf_directory}" 9 cd "${hid_conf_directory}" 10 11 # add basic information 12 echo 0x0100 > bcdDevice 13 echo 0x0200 > bcdUSB 14 echo 0x0104 > idProduct # Multifunction Composite Gadget 15 echo 0x1d6b > idVendor # Linux Foundation 16 17 # create English locale 18 mkdir strings/0x409 19 20 echo "OpenBMC" > strings/0x409/manufacturer 21 echo "virtual_input" > strings/0x409/product 22 echo "OBMC0001" > strings/0x409/serialnumber 23 24 # Create HID keyboard function 25 mkdir functions/hid.0 26 27 echo 1 > functions/hid.0/protocol # 1: keyboard 28 echo 8 > functions/hid.0/report_length 29 echo 1 > functions/hid.0/subclass 30 31 # Binary HID keyboard descriptor 32 # 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 33 # 0x09, 0x06, // USAGE (Keyboard) 34 # 0xa1, 0x01, // COLLECTION (Application) 35 # 0x05, 0x07, // USAGE_PAGE (Keyboard) 36 # 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl) 37 # 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI) 38 # 0x15, 0x00, // LOGICAL_MINIMUM (0) 39 # 0x25, 0x01, // LOGICAL_MAXIMUM (1) 40 # 0x75, 0x01, // REPORT_SIZE (1) 41 # 0x95, 0x08, // REPORT_COUNT (8) 42 # 0x81, 0x02, // INPUT (Data,Var,Abs) 43 # 0x95, 0x01, // REPORT_COUNT (1) 44 # 0x75, 0x08, // REPORT_SIZE (8) 45 # 0x81, 0x03, // INPUT (Data,Var,Abs) 46 # 0x95, 0x05, // REPORT_COUNT (5) 47 # 0x75, 0x01, // REPORT_SIZE (1) 48 # 0x05, 0x08, // USAGE_PAGE (LEDs) 49 # 0x19, 0x01, // USAGE_MINIMUM (Num Lock) 50 # 0x29, 0x05, // USAGE_MAXIMUM (Kana) 51 # 0x91, 0x02, // OUTPUT (Data,Var,Abs) 52 # 0x95, 0x01, // REPORT_COUNT (1) 53 # 0x75, 0x03, // REPORT_SIZE (3) 54 # 0x91, 0x03, // OUTPUT (Cnst,Var,Abs) 55 # 0x95, 0x06, // REPORT_COUNT (6) 56 # 0x75, 0x08, // REPORT_SIZE (8) 57 # 0x15, 0x00, // LOGICAL_MINIMUM (0) 58 # 0x25, 0x65, // LOGICAL_MAXIMUM (101) 59 # 0x05, 0x07, // USAGE_PAGE (Keyboard) 60 # 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated)) 61 # 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application) 62 # 0x81, 0x00, // INPUT (Data,Ary,Abs) 63 # 0xc0 // END_COLLECTION 64 echo -ne '\x05\x01\x09\x06\xa1\x01\x05\x07\x19\xe0\x29\xe7\x15\x00\x25\x01\x75\x01\x95\x08\x81\x02\x95\x01\x75\x08\x81\x03\x95\x05\x75\x01\x05\x08\x19\x01\x29\x05\x91\x02\x95\x01\x75\x03\x91\x03\x95\x06\x75\x08\x15\x00\x25\x65\x05\x07\x19\x00\x29\x65\x81\x00\xc0' > functions/hid.0/report_desc 65 66 # Create HID mouse function 67 mkdir functions/hid.1 68 69 echo 2 > functions/hid.1/protocol # 2: mouse 70 echo 5 > functions/hid.1/report_length 71 echo 1 > functions/hid.1/subclass 72 73 # Binary HID mouse descriptor (absolute coordinate) 74 # 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 75 # 0x09, 0x02, // USAGE (Mouse) 76 # 0xa1, 0x01, // COLLECTION (Application) 77 # 0x09, 0x01, // USAGE (Pointer) 78 # 0xa1, 0x00, // COLLECTION (Physical) 79 # 0x05, 0x09, // USAGE_PAGE (Button) 80 # 0x19, 0x01, // USAGE_MINIMUM (Button 1) 81 # 0x29, 0x03, // USAGE_MAXIMUM (Button 3) 82 # 0x15, 0x00, // LOGICAL_MINIMUM (0) 83 # 0x25, 0x01, // LOGICAL_MAXIMUM (1) 84 # 0x95, 0x03, // REPORT_COUNT (3) 85 # 0x75, 0x01, // REPORT_SIZE (1) 86 # 0x81, 0x02, // INPUT (Data,Var,Abs) 87 # 0x95, 0x01, // REPORT_COUNT (1) 88 # 0x75, 0x05, // REPORT_SIZE (5) 89 # 0x81, 0x03, // INPUT (Cnst,Var,Abs) 90 # 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 91 # 0x09, 0x30, // USAGE (X) 92 # 0x09, 0x31, // USAGE (Y) 93 # 0x35, 0x00, // PHYSICAL_MINIMUM (0) 94 # 0x46, 0xff, 0x7f, // PHYSICAL_MAXIMUM (32767) 95 # 0x15, 0x00, // LOGICAL_MINIMUM (0) 96 # 0x26, 0xff, 0x7f, // LOGICAL_MAXIMUM (32767) 97 # 0x65, 0x11, // UNIT (SI Lin:Distance) 98 # 0x55, 0x00, // UNIT_EXPONENT (0) 99 # 0x75, 0x10, // REPORT_SIZE (16) 100 # 0x95, 0x02, // REPORT_COUNT (2) 101 # 0x81, 0x02, // INPUT (Data,Var,Abs) 102 # 0xc0, // END_COLLECTION 103 # 0xc0 // END_COLLECTION 104 echo -ne '\x05\x01\x09\x02\xa1\x01\x09\x01\xa1\x00\x05\x09\x19\x01\x29\x03\x15\x00\x25\x01\x95\x03\x75\x01\x81\x02\x95\x01\x75\x05\x81\x03\x05\x01\x09\x30\x09\x31\x35\x00\x46\xff\x7f\x15\x00\x26\xff\x7f\x65\x11\x55\x00\x75\x10\x95\x02\x81\x02\xc0\xc0' > functions/hid.1/report_desc 105 106 # Create configuration 107 mkdir configs/c.1 108 mkdir configs/c.1/strings/0x409 109 110 echo 0x80 > configs/c.1/bmAttributes 111 echo 200 > configs/c.1/MaxPower 112 echo "" > configs/c.1/strings/0x409/configuration 113 114 # Link HID functions to configuration 115 ln -s functions/hid.0 configs/c.1 116 ln -s functions/hid.1 configs/c.1 117} 118 119connect_hid() { 120 if ! [[ `cat UDC` =~ "${dev_name}:p" ]]; then 121 i=0 122 num_ports=5 123 base_usb_dir="/sys/bus/platform/devices/${dev_name}/${dev_name}:p" 124 while [ $i -lt $num_ports ]; do 125 port=$(($i + 1)) 126 i=$port 127 if [ ! -e "${base_usb_dir}${port}/gadget/suspended" ]; then 128 break 129 fi 130 done 131 echo "${dev_name}:p${port}" > UDC 132 fi 133} 134 135disconnect_hid() { 136 if [[ `cat UDC` =~ "${dev_name}:p" ]]; then 137 echo "" > UDC 138 fi 139} 140 141original_directory="$(pwd)" 142 143if [ ! -e "${hid_conf_directory}" ]; then 144 create_hid 145else 146 cd "${hid_conf_directory}" 147fi 148 149if [ "$1" = "connect" ]; then 150 connect_hid 151elif [ "$1" = "disconnect" ]; then 152 disconnect_hid 153else 154 echo "Invalid option: $1. Use 'connect' or 'disconnect'." 155fi 156 157cd "${original_directory}" 158