forked from xtoolbox/TeenyUSB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb_desc.lua
332 lines (307 loc) · 10.9 KB
/
usb_desc.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
-- usb_desc.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.
--[[
USB descriptor base class, all other descriptors are generate by Descriptor
example:
Create a Device descriptor class
local DeviceDescriptorClass = Descriptor {
-- field default value, DUMMY means must filled by user
{ bLength = DUMMY },
{ bDescriptorType = DEVICE_DESCRIPTOR_TYPE },
{ bcdUSB = 0x200 },
{ bDeviceClass = 0 },
{ bDeviceSubClass = 0 },
{ bDeviceProtocol = 0 },
{ bMaxPacketSize = 8 },
{ idVendor = 0x0483 },
{ idProduct = 0x3748 },
{ bcdDevice = 0x100 },
{ iManufacture = 0 },
{ iProduct = 0 },
{ iSerial = 0 },
{ bNumConfigurations = 1 },
}
Create a real device descriptor
local deviceDescriptor = DeviceDescriptorClass {
bcdUSB = 0x110,
idVendor = 0x1234,
idProduct = 0x5678,
strManufacture = "Test Manufacture",
strProduct = "Test Product",
strSerial = "Test Serial",
}
print( tostring(deviceDescriptor) )
]]
SIMPLE = SIMPLE or false
VERBOSE = VERBOSE or false
DEVICE_DESCRIPTOR_TYPE = SIMPLE and 0x01 or "USB_DEVICE_DESCRIPTOR_TYPE"
CONFIGURATION_DESCRIPTOR_TYPE = SIMPLE and 0x02 or "USB_CONFIGURATION_DESCRIPTOR_TYPE"
STRING_DESCRIPTOR_TYPE = SIMPLE and 0x03 or "USB_STRING_DESCRIPTOR_TYPE"
INTERFACE_DESCRIPTOR_TYPE = SIMPLE and 0x04 or "USB_INTERFACE_DESCRIPTOR_TYPE"
ENDPOINT_DESCRIPTOR_TYPE = SIMPLE and 0x05 or "USB_ENDPOINT_DESCRIPTOR_TYPE"
IAD_DESCRIPTOR_TYPE = SIMPLE and 0x0B or "USB_IAD_DESCRIPTOR_TYPE"
DUMMY = {}
function info(...)
if not VERBOSE then return end
io.write("Info: ", ...)
io.write("\n")
end
function warning(...)
io.write("Warning: ", ...)
io.write("\n")
end
function splitU16(v)
return v&0xff, v>>8
end
function splitU32(v)
return v & 0xff, (v>>8)&0xff, (v>>16)&0xff, (v>>24)&0xff
end
local comment_col = 50
local fmt = string.format
function output8(v, desc, ident)
local r = fmt("0x%02x, ",v)
if SIMPLE then return r end
ident = ident or 0
r = r .. string.rep(" ", comment_col - #r - ident)
return r .. "/* " .. desc .. " */\n"
end
function output16(v, desc, ident)
local l,h = splitU16(v)
local r = fmt("0x%02x, 0x%02x, ", l, h)
if SIMPLE then return r end
ident = ident or 0
r = r .. string.rep(" ", comment_col - #r - ident)
return r .. "/* " .. desc .. " */\n"
end
function output32(v, desc, ident)
local l,h,hl,hh = splitU32(v)
local r = fmt("0x%02x, 0x%02x, 0x%02x, 0x%02x, ", l, h, hl, hh)
if SIMPLE then return r end
ident = ident or 0
r = r .. string.rep(" ", comment_col - #r - ident)
return r .. "/* " .. desc .. " */\n"
end
function outputChar(ch, desc, ident)
local r = ""
if ch>=0x20 and ch<=0x7f then
r = r .. fmt("'%s', ", string.char(ch))
else
r = r .. fmt("0x%02x, ", ch)
end
if SIMPLE then return r end
ident = ident or 0
r = r .. string.rep(" ", comment_col - #r - ident)
return r .. "/* " .. desc .. " */\n"
end
function outputWChar(v, desc, ident)
local l,h = splitU16(v)
local r = ""
if h == 0 and l>=0x20 and l<=0x7f then
r = r .. fmt("'%s', 0x%02x, ", string.char(l), h)
else
r = r .. fmt("0x%02x, 0x%02x, ", l, h)
end
if SIMPLE then return r end
ident = ident or 0
r = r .. string.rep(" ", comment_col - #r - ident)
return r .. "/* " .. desc .. " */\n"
end
local fieldSize
-- String means the value is a macro define
function outputString(v, desc, ident)
ident = ident or 0
if fieldSize(desc) == 2 then
v = "LOBYTE("..v.."), HIBYTE(" .. v .. ")"
end
if SIMPLE then return v .. ", " end
return v ..','.. string.rep(" ", comment_col - #v - 1 - ident) .. "/* " .. desc .. " */\n"
end
local field_prefix_table = {
b = output8,
bm = output8,
i = output8,
id = output16,
w = output16,
bcd = output16,
wc = outputWChar,
dw = output32,
}
local function fieldPrefix(field)
local pre = string.find(field, "[A-Z]")
local post = nil
if pre and pre>1 then
post = field:sub(pre)
pre = field:sub(1,pre-1)
end
--if not pre then error("unknown prefix of field \"" .. field .. '"') end
return pre, post
end
fieldSize = function(field)
local pre = fieldPrefix(field)
if field_prefix_table[pre] == output16 then return 2 end
if field_prefix_table[pre] == outputWChar then return 2 end
if field_prefix_table[pre] == output8 then return 1 end
if field_prefix_table[pre] == output32 then return 4 end
--error("unknown field size " .. tostring(filed))
return -1
end
-- every cnt output a newline
local function newLine(cnt)
cnt = cnt or 16
local init = 0
return function()
init = init + 1
if init == cnt then
init = 0
return "\n"
end
return ""
end
end
function outputField(desc, field)
local ident = desc.ident or " "
if type(field) == "table" then
-- parameter is layout table
local r = ""
local nl = newLine(8)
for i,v in ipairs(field) do
if SIMPLE then
r = r .. outputField(desc, v) .. nl()
else
r = r .. ident .. outputField(desc, v)
end
end
return r
end
if not field then
if not desc.layout then error("Descriptor layout not set for " .. tostring(desc.bDescriptorType) .. " descriptor") end
return outputField(desc, desc.layout)
end
local pre = fieldPrefix(field)
local output = field_prefix_table[pre]
if not output then error("Unknown prefix " .. tostring(pre) .. " of field " .. tostring(field)) end
-- output string field, output direct
if type(desc[field]) == "string" then return outputString(desc[field],field,#ident) end
-- output function field, evalute it then output
if type(desc[field]) == "function" then return output(desc[field](),field,#ident) end
-- output number field
return output(desc[field],field,#ident)
end
-- descriptor init function, bind to __call meta method
local function descInit(desc, param)
if param.varData then
for i,v in ipairs(param.varData) do
for name, value in pairs(v) do
desc.layout[#desc.layout+1] = name
desc[name] = value
desc.paramLength = desc.paramLength + fieldSize(name)
break
end
end
end
for k,v in pairs(desc.default) do
if param[k] then
desc.default[k] = param[k]
end
end
desc.bLength = desc.headerLength + desc.paramLength
for i,v in ipairs(param) do
desc[i] = v
end
-- copy strings in param to descriptor
for k,v in pairs(param) do
local pre,post = fieldPrefix(k)
local field = "i" .. tostring(post)
-- field prefix is strXXX, and iXXX exist in the layout
if pre == "str" and desc.default[field] then
desc[k] = v
end
end
return desc
end
-- descriptor to string, bind to __tostring meta method
local function descToString(desc)
local r = ""
if desc.outputHeader then
if type(desc.outputHeader) == "string" then r = desc.outputHeader end
if type(desc.outputHeader) == "function" then r = desc:outputHeader() end
end
r = r .. outputField(desc)
if desc.outputTail then
if type(desc.outputTail) == "string" then r = r .. desc.outputTail end
if type(desc.outputTail) == "function" then r = r .. desc:outputTail() end
end
return r
end
-- check the descriptor default value, this function is bind to __index meta method
local function check_default(desc, field)
-- field is number, means not k-v pair, ignore
if type(field) == "number" then return nil end
-- find the value in default table
local v = desc.default and desc.default[field] or nil
if v then
-- set to default value
info("Set ["..field.."] to default value " .. tostring(v) )
desc[field] = v
return v
end
-- otherwise rise an error
if desc.layoutMap[field] then
error('Fied "'..field..'" not set in ' .. tostring(desc.default.bDescriptorType) .. " descriptor")
end
end
function Descriptor(initTable)
local desc = {}
desc.layout = {}
desc.default = {}
desc.layoutMap = {}
desc.headerLength = 0
desc.paramLength = 0
for i,v in ipairs(initTable) do
for field, value in pairs(v) do
desc.default[field] = value
if value == DUMMY then desc.default[field] = false end
desc.layout[i] = field
desc.layoutMap[field] = true
desc.headerLength = desc.headerLength + fieldSize(field)
break
end
end
desc.ident = " "
setmetatable(desc, {
__call = descInit,
__tostring = descToString,
__index = check_default,
})
return desc
end