forked from xtoolbox/TeenyUSB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb_hid.lua
229 lines (216 loc) · 8.98 KB
/
usb_hid.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
-- usb_hid.lua
-- _______ _____ _______
-- |__ __| | __ \__ __|
-- | | ___ ___ _ __ _ _| | | | | |
-- | |/ _ \/ _ \ '_ \| | | | | | | | |
-- | | __/ __/ | | | |_| | |__| | | |
-- |_|\___|\___|_| |_|\__, |_____/ |_|
-- __/ |
-- |___/
--
-- TeenyDT - GUI and command line tool to generate USB descriptors and drivers
--
-- Copyright (c) 2019 XToolBox - admin@xtoolbox.org
-- www.tusb.org
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
require("usb_common_desc")
function HIDDescriptor(param)
local varData = {}
param.bNumDescriptors = #param
for i,v in ipairs(param) do
local t = {}
t["bDescriptorType"..i] = 0x22
varData[#varData+1] = t
t = {}
t["wDescriptorLength"..i] = #v
varData[#varData+1] = t
end
param.varData = varData
local desc = Descriptor{
{bLength = DUMMY },
{bDescriptorType = 0x21 },
{bcdHID = 0x111 },
{bCountryCode = 0 },
{bNumDescriptors = DUMMY },
}(param)
desc.outputHeader = desc.ident .. "/* HID descriptor " .. (param.alias or "") .. " */\n"
desc.outputTail = ""
return desc
end
local TAG = "HID: "
local function outputReportDescriptor(hid)
local r = ""
hid.declare = ""
if hid.report then
hid.declare = string.gsub([[
#define $prefix$REPORT_DESCRIPTOR_SIZE_IF$ifn$ $size$
extern const uint8_t $prefix$ReportDescriptor_if$ifn$[$prefix$REPORT_DESCRIPTOR_SIZE_IF$ifn$];
]], "%$(%w+)%$", {prefix = hid.prefix, ifn = hid.bInterfaceNumber, size = #hid.report })
r = r .. string.gsub([[
#define $prefix$REPORT_DESCRIPTOR_SIZE_IF$ifn$ $size$
WEAK __ALIGN_BEGIN const uint8_t $prefix$ReportDescriptor_if$ifn$[$prefix$REPORT_DESCRIPTOR_SIZE_IF$ifn$] __ALIGN_END = {
]], "%$(%w+)%$", {prefix = hid.prefix, ifn = hid.bInterfaceNumber, size = #hid.report })
if hid.report.text then
r = r .. "\n" .. hid.report.text .. "\n"
else
for i,v in ipairs(hid.report) do
r = r .. string.format("0x%02x, ", v)
if (i & 0xf) == 0 then r = r .."\n" end
end
end
r = r .. "};\n\n"
end
return r
end
function HID_BuildReport(str)
str = string.gsub(str, "//", "--")
local r = load( "return { ".. str .. "}")() or {0}
r.text = string.gsub(str, "%-%-", "//")
return r
end
function HID_InOut(size)
local str = string.format("0x95, 0x%02x,", size)
local r = [[
-- report descriptor for general input/output
0x06, 0x00, 0xFF, -- Usage Page (Vendor Defined 0xFF00)
0x09, 0x01, -- Usage (0x01)
0xA1, 0x01, -- Collection (Application)
0x09, 0x02, -- Usage (0x02)
0x15, 0x00, -- Logical Minimum (0)
0x25, 0xFF, -- Logical Maximum (255)
0x75, 0x08, -- Report Size (8)
]]..str..
[[ -- Report Count (]]..tostring(size)..[[)
0x81, 0x02, -- Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x09, 0x03, -- Usage (0x03)
0x91, 0x02, -- Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
0xC0 -- End Collection
]]
return HID_BuildReport(r)
end
function HID_BootKeyBoard()
local r = [[
-- Boot keyboad report descriptor
0x05, 0x01, -- USAGE_PAGE (Generic Desktop)
0x09, 0x06, -- USAGE (Keyboard)
0xa1, 0x01, -- COLLECTION (Application)
0x05, 0x07, -- USAGE_PAGE (Keyboard)
0x19, 0xe0, -- USAGE_MINIMUM (Keyboard LeftControl)
0x29, 0xe7, -- USAGE_MAXIMUM (Keyboard Right GUI)
0x15, 0x00, -- LOGICAL_MINIMUM (0)
0x25, 0x01, -- LOGICAL_MAXIMUM (1)
0x75, 0x01, -- REPORT_SIZE (1)
0x95, 0x08, -- REPORT_COUNT (8)
0x81, 0x02, -- INPUT (Data,Var,Abs)
0x95, 0x01, -- REPORT_COUNT (1)
0x75, 0x08, -- REPORT_SIZE (8)
0x81, 0x03, -- INPUT (Cnst,Var,Abs)
0x95, 0x05, -- REPORT_COUNT (5)
0x75, 0x01, -- REPORT_SIZE (1)
0x05, 0x08, -- USAGE_PAGE (LEDs)
0x19, 0x01, -- USAGE_MINIMUM (Num Lock)
0x29, 0x05, -- USAGE_MAXIMUM (Kana)
0x91, 0x02, -- OUTPUT (Data,Var,Abs)
0x95, 0x01, -- REPORT_COUNT (1)
0x75, 0x03, -- REPORT_SIZE (3)
0x91, 0x03, -- OUTPUT (Cnst,Var,Abs)
0x95, 0x06, -- REPORT_COUNT (6)
0x75, 0x08, -- REPORT_SIZE (8)
0x15, 0x00, -- LOGICAL_MINIMUM (0)
0x25, 0x65, -- LOGICAL_MAXIMUM (101)
0x05, 0x07, -- USAGE_PAGE (Keyboard)
0x19, 0x00, -- USAGE_MINIMUM (Reserved (no event indicated))
0x29, 0x65, -- USAGE_MAXIMUM (Keyboard Application)
0x81, 0x00, -- INPUT (Data,Ary,Abs)
0xc0 -- END_COLLECTION
]]
return HID_BuildReport(r)
end
function HID_BootMouse()
local r = [[
-- Boot Mouse report descriptor
0x05, 0x01, -- USAGE_PAGE (Generic Desktop)
0x09, 0x02, -- USAGE (Mouse)
0xa1, 0x01, -- COLLECTION (Application)
0x09, 0x01, -- USAGE (Pointer)
0xa1, 0x00, -- COLLECTION (Physical)
0x05, 0x09, -- USAGE_PAGE (Button)
0x19, 0x01, -- USAGE_MINIMUM (Button 1)
0x29, 0x03, -- USAGE_MAXIMUM (Button 3)
0x15, 0x00, -- LOGICAL_MINIMUM (0)
0x25, 0x01, -- LOGICAL_MAXIMUM (1)
0x95, 0x03, -- REPORT_COUNT (3)
0x75, 0x01, -- REPORT_SIZE (1)
0x81, 0x02, -- INPUT (Data,Var,Abs)
0x95, 0x01, -- REPORT_COUNT (1)
0x75, 0x05, -- REPORT_SIZE (5)
0x81, 0x03, -- INPUT (Cnst,Var,Abs)
0x05, 0x01, -- USAGE_PAGE (Generic Desktop)
0x09, 0x30, -- USAGE (X)
0x09, 0x31, -- USAGE (Y)
0x15, 0x81, -- LOGICAL_MINIMUM (-127)
0x25, 0x7f, -- LOGICAL_MAXIMUM (127)
0x75, 0x08, -- REPORT_SIZE (8)
0x95, 0x02, -- REPORT_COUNT (2)
0x81, 0x06, -- INPUT (Data,Var,Rel)
0xc0, -- END_COLLECTION
0xc0 -- END_COLLECTION
]]
return HID_BuildReport(r)
end
function HID_ReportTemp()
local r = [[
-- Temaple report
0x06, 0x00, 0xFF, -- Usage Page (Vendor Defined 0xFF00)
0x09, 0x01, -- Usage (0x01)
0xA1, 0x01, -- Collection (Application)
0x09, 0x02, -- Usage (0x02)
0x15, 0x00, -- Logical Minimum (0)
0x25, 0xFF, -- Logical Maximum (255)
0x75, 0x08, -- Report Size (8)
0x95, 0x40, -- Report Count (64)
0x81, 0x02, -- Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x09, 0x03, -- Usage (0x03)
0x91, 0x02, -- Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
0xC0 -- End Collection
]]
return HID_BuildReport(r)
end
function USB_HID(param)
local Hid
local ReadEp = param.ReadEp or param[1]
local WriteEp = param.WriteEp or param[2]
Hid = Interface{
bInterfaceClass = 3,
bInterfaceSubClass = param.isBoot and 1 or 0,
bInterfaceProtocol = param.isKey and 1 or (param.isMouse and 2 or 0),
HIDDescriptor{
param.report,
param.physical,
},
ReadEp,
WriteEp,
}
Hid.report = param.report
Hid.physical = param.physical
Hid.extraDesc = outputReportDescriptor
Hid.hasReport = true
return Hid
end