Eddie James | b9c253d | 2018-12-11 13:25:50 -0600 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 3 | hid_conf_directory="/sys/kernel/config/usb_gadget/obmc_hid" |
| 4 | dev_name="1e6a0000.usb-vhub" |
Eddie James | b9c253d | 2018-12-11 13:25:50 -0600 | [diff] [blame] | 5 | |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 6 | create_hid() { |
| 7 | # create gadget |
| 8 | mkdir "${hid_conf_directory}" |
Patrick Williams | 67a9bd4 | 2022-12-04 15:03:19 -0600 | [diff] [blame] | 9 | cd "${hid_conf_directory}" || exit 1 |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 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 |
Mohammed Javith Akthar M | a6c8f5d | 2023-07-26 18:50:28 +0545 | [diff] [blame] | 21 | echo "Virtual Keyboard and Mouse" > strings/0x409/product |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 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 |
Jae Hyun Yoo | c85668d | 2022-05-02 07:21:58 -0700 | [diff] [blame] | 64 | printf '\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 |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 65 | |
| 66 | # Create HID mouse function |
| 67 | mkdir functions/hid.1 |
| 68 | |
| 69 | echo 2 > functions/hid.1/protocol # 2: mouse |
Tejas Patil | 3fa0bfb | 2021-12-23 15:44:47 +0530 | [diff] [blame] | 70 | echo 6 > functions/hid.1/report_length |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 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) |
Tejas Patil | 3fa0bfb | 2021-12-23 15:44:47 +0530 | [diff] [blame] | 102 | # 0x09, 0x38, // Usage (Wheel) |
| 103 | # 0x15, 0xff, // LOGICAL_MINIMUM (-1) |
| 104 | # 0x25, 0x01, // LOGICAL_MAXIMUM (1) |
| 105 | # 0x35, 0x00, // PHYSICAL_MINIMUM (-127) |
| 106 | # 0x45, 0x00, // PHYSICAL_MAXIMUM (127) |
| 107 | # 0x75, 0x08, // REPORT_SIZE (8) |
| 108 | # 0x95, 0x01, // REPORT_COUNT (1) |
| 109 | # 0x81, 0x06, // INPUT (Data,Var,Rel) |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 110 | # 0xc0, // END_COLLECTION |
| 111 | # 0xc0 // END_COLLECTION |
Jae Hyun Yoo | c85668d | 2022-05-02 07:21:58 -0700 | [diff] [blame] | 112 | printf '\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\x09\x38\x15\xff\x25\x01\x35\x00\x45\x00\x75\x08\x95\x01\x81\x06\xc0\xc0' > functions/hid.1/report_desc |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 113 | |
| 114 | # Create configuration |
| 115 | mkdir configs/c.1 |
| 116 | mkdir configs/c.1/strings/0x409 |
| 117 | |
Neal Liu | 3b201f6 | 2022-01-11 15:10:05 +0800 | [diff] [blame] | 118 | echo 0xe0 > configs/c.1/bmAttributes |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 119 | echo 200 > configs/c.1/MaxPower |
| 120 | echo "" > configs/c.1/strings/0x409/configuration |
| 121 | |
| 122 | # Link HID functions to configuration |
| 123 | ln -s functions/hid.0 configs/c.1 |
| 124 | ln -s functions/hid.1 configs/c.1 |
| 125 | } |
| 126 | |
| 127 | connect_hid() { |
Jae Hyun Yoo | c85668d | 2022-05-02 07:21:58 -0700 | [diff] [blame] | 128 | if ! grep -q "${dev_name}:p" UDC; then |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 129 | i=0 |
| 130 | num_ports=5 |
| 131 | base_usb_dir="/sys/bus/platform/devices/${dev_name}/${dev_name}:p" |
Jae Hyun Yoo | c85668d | 2022-05-02 07:21:58 -0700 | [diff] [blame] | 132 | while [ "${i}" -lt "${num_ports}" ]; do |
| 133 | port=$(("${i}" + 1)) |
| 134 | i="${port}" |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 135 | if [ ! -e "${base_usb_dir}${port}/gadget/suspended" ]; then |
| 136 | break |
| 137 | fi |
| 138 | done |
| 139 | echo "${dev_name}:p${port}" > UDC |
| 140 | fi |
| 141 | } |
| 142 | |
| 143 | disconnect_hid() { |
Jae Hyun Yoo | c85668d | 2022-05-02 07:21:58 -0700 | [diff] [blame] | 144 | if grep -q "${dev_name}:p" UDC; then |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 145 | echo "" > UDC |
| 146 | fi |
| 147 | } |
| 148 | |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 149 | if [ ! -e "${hid_conf_directory}" ]; then |
| 150 | create_hid |
| 151 | else |
Patrick Williams | 67a9bd4 | 2022-12-04 15:03:19 -0600 | [diff] [blame] | 152 | cd "${hid_conf_directory}" || exit 1 |
Eddie James | b9c253d | 2018-12-11 13:25:50 -0600 | [diff] [blame] | 153 | fi |
| 154 | |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 155 | if [ "$1" = "connect" ]; then |
| 156 | connect_hid |
| 157 | elif [ "$1" = "disconnect" ]; then |
| 158 | disconnect_hid |
| 159 | else |
Jae Hyun Yoo | c85668d | 2022-05-02 07:21:58 -0700 | [diff] [blame] | 160 | echo >&2 "Invalid option: $1. Use 'connect' or 'disconnect'." |
| 161 | exit 1 |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 162 | fi |