blob: 58e403460065c77ad2c64e4a274a9760d48f197b [file] [log] [blame]
Eddie Jamesb9c253d2018-12-11 13:25:50 -06001#!/bin/sh
2
Jae Hyun Yooc11257d2020-07-22 23:39:18 -07003hid_conf_directory="/sys/kernel/config/usb_gadget/obmc_hid"
4dev_name="1e6a0000.usb-vhub"
Eddie Jamesb9c253d2018-12-11 13:25:50 -06005
Jae Hyun Yooc11257d2020-07-22 23:39:18 -07006create_hid() {
7 # create gadget
8 mkdir "${hid_conf_directory}"
Patrick Williams67a9bd42022-12-04 15:03:19 -06009 cd "${hid_conf_directory}" || exit 1
Jae Hyun Yooc11257d2020-07-22 23:39:18 -070010
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 Ma6c8f5d2023-07-26 18:50:28 +054521 echo "Virtual Keyboard and Mouse" > strings/0x409/product
Jae Hyun Yooc11257d2020-07-22 23:39:18 -070022 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 Yooc85668d2022-05-02 07:21:58 -070064 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 Yooc11257d2020-07-22 23:39:18 -070065
66 # Create HID mouse function
67 mkdir functions/hid.1
68
69 echo 2 > functions/hid.1/protocol # 2: mouse
Tejas Patil3fa0bfb2021-12-23 15:44:47 +053070 echo 6 > functions/hid.1/report_length
Jae Hyun Yooc11257d2020-07-22 23:39:18 -070071 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 Patil3fa0bfb2021-12-23 15:44:47 +0530102 # 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 Yooc11257d2020-07-22 23:39:18 -0700110 # 0xc0, // END_COLLECTION
111 # 0xc0 // END_COLLECTION
Jae Hyun Yooc85668d2022-05-02 07:21:58 -0700112 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 Yooc11257d2020-07-22 23:39:18 -0700113
114 # Create configuration
115 mkdir configs/c.1
116 mkdir configs/c.1/strings/0x409
117
Neal Liu3b201f62022-01-11 15:10:05 +0800118 echo 0xe0 > configs/c.1/bmAttributes
Jae Hyun Yooc11257d2020-07-22 23:39:18 -0700119 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
127connect_hid() {
Jae Hyun Yooc85668d2022-05-02 07:21:58 -0700128 if ! grep -q "${dev_name}:p" UDC; then
Jae Hyun Yooc11257d2020-07-22 23:39:18 -0700129 i=0
130 num_ports=5
131 base_usb_dir="/sys/bus/platform/devices/${dev_name}/${dev_name}:p"
Jae Hyun Yooc85668d2022-05-02 07:21:58 -0700132 while [ "${i}" -lt "${num_ports}" ]; do
133 port=$(("${i}" + 1))
134 i="${port}"
Jae Hyun Yooc11257d2020-07-22 23:39:18 -0700135 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
143disconnect_hid() {
Jae Hyun Yooc85668d2022-05-02 07:21:58 -0700144 if grep -q "${dev_name}:p" UDC; then
Jae Hyun Yooc11257d2020-07-22 23:39:18 -0700145 echo "" > UDC
146 fi
147}
148
Jae Hyun Yooc11257d2020-07-22 23:39:18 -0700149if [ ! -e "${hid_conf_directory}" ]; then
150 create_hid
151else
Patrick Williams67a9bd42022-12-04 15:03:19 -0600152 cd "${hid_conf_directory}" || exit 1
Eddie Jamesb9c253d2018-12-11 13:25:50 -0600153fi
154
Jae Hyun Yooc11257d2020-07-22 23:39:18 -0700155if [ "$1" = "connect" ]; then
156 connect_hid
157elif [ "$1" = "disconnect" ]; then
158 disconnect_hid
159else
Jae Hyun Yooc85668d2022-05-02 07:21:58 -0700160 echo >&2 "Invalid option: $1. Use 'connect' or 'disconnect'."
161 exit 1
Jae Hyun Yooc11257d2020-07-22 23:39:18 -0700162fi