-
Notifications
You must be signed in to change notification settings - Fork 44
/
tplgtool.py
executable file
·395 lines (372 loc) · 13.8 KB
/
tplgtool.py
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env python3
import argparse
import enum
from construct import this, ListContainer, Struct, Enum, FlagsEnum, Const, Switch, Array, Bytes, GreedyRange, FocusedSeq, Padded, Padding, PaddedString, Flag, Byte, Int16ul, Int32ul, Int64ul, Terminated
class TplgType(enum.IntEnum):
"File and Block header data types. `SND_SOC_TPLG_TYPE_`"
MIXER = 1
BYTES = 2
ENUM = 3
DAPM_GRAPH = 4
DAPM_WIDGET = 5
DAI_LINK = 6
PCM = 7
MANIFEST = 8
CODEC_LINK = 9
BACKEND_LINK = 10
PDATA = 11
DAI = 12
class DaiFormat(enum.IntEnum):
"DAI physical PCM data formats. `SND_SOC_DAI_FORMAT_`"
I2S = 1
RIGHT_J = 2
LEFT_J = 3
DSP_A = 4
DSP_B = 5
AC97 = 6
PDM = 7
class DaiLnkFlag(enum.IntFlag):
"DAI link flags. `SND_SOC_TPLG_LNK_FLGBIT_`"
SYMMETRIC_RATES = 0b0001
SYMMETRIC_CHANNELS = 0b0010
SYMMETRIC_SAMPLEBITS = 0b0100
VOICE_WAKEUP = 0b1000
class DaiMClk(enum.IntEnum):
"DAI mclk_direction. `SND_SOC_TPLG_MCLK_`"
CO = 0
CI = 1
class DaiBClk(enum.IntEnum):
"DAI topology BCLK parameter. `SND_SOC_TPLG_BCLK_`"
CP = 0
CC = 1
class DaiFSync(enum.IntEnum):
"DAI topology FSYNC parameter. `SND_SOC_TPLG_FSYNC_`"
CP = 0
CC = 1
class DaiClockGate(enum.IntEnum):
"DAI clock gating. `SND_SOC_TPLG_DAI_CLK_GATE_`"
UNDEFINED = 0
GATED = 1
CONT = 2
class CtlTlvt(enum.IntEnum):
"`SNDRV_CTL_TLVT_`"
CONTAINER = 0
DB_SCALE = 1
DB_LINEAR = 2
DB_RANGE = 3
DB_MINMAX = 4
DB_MINMAX_MUTE = 5
CHMAP_FIXED = 0x101
CHMAP_VAR = 0x102
CHMAP_PAIRED = 0x103
class DapmType(enum.IntEnum):
"`SND_SOC_TPLG_DAPM_`"
INPUT = 0
OUTPUT = 1
MUX = 2
MIXER = 3
PGA = 4
DRV = 5
ADC = 6
DAC = 7
SWITCH = 8
PRE = 9
POST = 10
AIF_IN = 11
AIF_OUT = 12
DAI_IN = 13
DAI_OUT = 14
DAI_LINK = 15
BUFFER = 16
HEDULER = 17
EFFECT = 18
SIGGEN = 19
SRC = 20
ASRC = 21
ENCODER = 22
DECODER = 23
class PcmFormatsFlag(enum.IntFlag):
S8 = 1 << 0
U8 = 1 << 1
S16_LE = 1 << 2
S16_BE = 1 << 3
U16_LE = 1 << 4
U16_BE = 1 << 5
S24_LE = 1 << 6
S24_BE = 1 << 7
U24_LE = 1 << 8
U24_BE = 1 << 9
S32_LE = 1 << 10
S32_BE = 1 << 11
U32_LE = 1 << 12
U32_BE = 1 << 13
FLOAT_LE = 1 << 14
FLOAT_BE = 1 << 15
class TplgBinaryFormat():
"Tplg format description"
_TPLG_MAGIC = b'CoSA'
_ABI_HEADER = Struct(
"magic" / Const(_TPLG_MAGIC),
"abi" / Int32ul,
)
@staticmethod
def get_file_abi_version(filename: str) -> int:
"Recognize the ABI version for TPLG file."
return TplgBinaryFormat._ABI_HEADER.parse_file(filename)["abi"]
def __init__(self):
self._abi = 5 # SND_SOC_TPLG_ABI_VERSION
self._max_channel = 8 # SND_SOC_TPLG_MAX_CHAN
self._max_formats = 16 # SND_SOC_TPLG_MAX_FORMATS
self._stream_config_max = 8 # SND_SOC_TPLG_STREAM_CONFIG_MAX
self._hw_config_max = 8 # SND_SOC_TPLG_HW_CONFIG_MAX
self._tlv_size = 32 # SND_SOC_TPLG_TLV_SIZE
self._id_name_maxlen = 44 # SNDRV_CTL_ELEM_ID_NAME_MAXLEN
self._num_texts = 16 # SND_SOC_TPLG_NUM_TEXTS
self._block_header = Struct( # snd_soc_tplg_hdr
"magic" / Const(self._TPLG_MAGIC),
"abi" / Const(self._abi, Int32ul),
"version" / Int32ul,
"type" / Enum(Int32ul, TplgType),
"size" / Const(4 * 9, Int32ul),
"vendor_type" / Int32ul,
"payload_size" / Int32ul,
"index" / Int32ul,
"count" / Int32ul,
)
self._private = Struct( # snd_soc_tplg_private
"size" / Int32ul,
"data" / Bytes(this.size), # TODO: parse private data
)
self._tlv_dbscale = Struct( # snd_soc_tplg_tlv_dbscale
"min" / Int32ul,
"step" / Int32ul,
"mute" / Int32ul,
)
self._ctl_tlv = Struct( # snd_soc_tplg_ctl_tlv
"size" / Int32ul,
"type" / Enum(Int32ul, CtlTlvt),
"scale" / Padded(4 * self._tlv_size, self._tlv_dbscale)
)
self._channel = Struct( # snd_soc_tplg_channel
"size" / Int32ul,
"reg" / Int32ul,
"shift" / Int32ul,
"id" / Int32ul,
)
self._io_ops = Struct( # snd_soc_tplg_io_ops
"get" / Int32ul,
"put" / Int32ul,
"info" / Int32ul,
)
self._kcontrol_hdr = Struct( # snd_soc_tplg_ctl_hdr
"size" / Int32ul,
"type" / Enum(Int32ul, TplgType),
"name" / PaddedString(self._id_name_maxlen, "ascii"),
"access" / Int32ul,
"ops" / self._io_ops,
"tlv" / self._ctl_tlv,
)
self._stream_caps = Struct( # snd_soc_tplg_stream_caps
"size" / Int32ul,
"name" / PaddedString(self._id_name_maxlen, "ascii"),
"formats" / FlagsEnum(Int64ul, PcmFormatsFlag),
"rates" / Int32ul, # SNDRV_PCM_RATE_ ?
"rate_min" / Int32ul,
"rate_max" / Int32ul,
"channels_min" / Int32ul,
"channels_max" / Int32ul,
"periods_min" / Int32ul,
"periods_max" / Int32ul,
"period_size_min" / Int32ul,
"period_size_max" / Int32ul,
"buffer_size_min" / Int32ul,
"buffer_size_max" / Int32ul,
"sig_bits" / Int32ul,
)
self._stream = Struct( # snd_soc_tplg_stream
"size" / Int32ul,
"name" / PaddedString(self._id_name_maxlen, "ascii"),
"format" / FlagsEnum(Int64ul, PcmFormatsFlag),
"rate" / Int32ul,
"period_bytes" / Int32ul,
"buffer_bytes" / Int32ul,
"channels" / Int32ul,
)
self._hw_config = Struct( # snd_soc_tplg_hw_config
"size" / Int32ul,
"id" / Int32ul,
"fmt" / Enum(Int32ul, DaiFormat),
"clock_gated" / Enum(Byte, DaiClockGate),
"invert_bclk" / Flag,
"invert_fsync" / Flag,
"bclk_provider" / Enum(Byte, DaiBClk),
"fsync_provider" / Enum(Byte, DaiFSync),
"mclk_direction" / Enum(Byte, DaiMClk),
Padding(2) * "reserved",
"mclk_rate" / Int32ul,
"bclk_rate" / Int32ul,
"fsync_rate" / Int32ul,
"tdm_slots" / Int32ul,
"tdm_slot_width" / Int32ul,
"tx_slots" / Int32ul,
"rx_slots" / Int32ul,
"tx_channels" / Int32ul,
"tx_chanmap" / Array(self._max_channel, Int32ul),
"rx_channels" / Int32ul,
"rx_chanmap" / Array(self._max_channel, Int32ul),
)
self._manifest = Struct( # snd_soc_tplg_manifest
"size" / Int32ul,
"control_elems" / Int32ul,
"widget_elems" / Int32ul,
"graph_elems" / Int32ul,
"pcm_elems" / Int32ul,
"dai_link_elems" / Int32ul,
"dai_elems" / Int32ul,
Padding(4 * 20) * "reserved",
"priv" / self._private,
)
self._mixer_control_body = Struct( # `snd_soc_tplg_mixer_control` without `hdr`
"size" / Int32ul,
"min" / Int32ul,
"max" / Int32ul,
"platform_max" / Int32ul,
"invert" / Int32ul,
"num_channels" / Int32ul,
"channel" / Array(self._max_channel, self._channel),
"priv" / self._private,
)
self._enum_control_body = Struct( # `snd_soc_tplg_enum_control` without `hdr`
"size" / Int32ul,
"num_channels" / Int32ul,
"channel" / Array(self._max_channel, self._channel),
"items" / Int32ul,
"mask" / Int32ul,
"count" / Int32ul,
"texts" / Array(self._num_texts, PaddedString(self._id_name_maxlen, "ascii")),
"values" / Array(self._num_texts * self._id_name_maxlen / 4, Int32ul),
"priv" / self._private,
)
self._bytes_control_body = Struct( # `snd_soc_tplg_bytes_control` without `hdr`
"size" / Int32ul,
"max" / Int32ul,
"mask" / Int32ul,
"base" / Int32ul,
"num_regs" / Int32ul,
"ext_ops" / self._io_ops,
"priv" / self._private,
)
self._kcontrol_cases = {
TplgType.MIXER.name: self._mixer_control_body,
TplgType.ENUM.name: self._enum_control_body,
TplgType.BYTES.name: self._bytes_control_body,
}
self._kcontrol_wrapper = Struct( # wrapper for kcontrol types, divide header and body
"hdr" / self._kcontrol_hdr,
"body" / Switch(
this.hdr.type,
self._kcontrol_cases,
),
)
self._dapm_graph_elem = Struct( # snd_soc_tplg_dapm_graph_elem
"sink" / PaddedString(self._id_name_maxlen, "ascii"),
"control" / PaddedString(self._id_name_maxlen, "ascii"),
"source" / PaddedString(self._id_name_maxlen, "ascii"),
)
self._dapm_widget = Struct( # snd_soc_tplg_dapm_widget
"size" / Int32ul,
"id" / Int32ul,
"name" / PaddedString(self._id_name_maxlen, "ascii"),
"sname" / PaddedString(self._id_name_maxlen, "ascii"),
"reg" / Int32ul,
"shift" / Int32ul,
"mask" / Int32ul,
"subseq" / Int32ul,
"invert" / Int32ul,
"ignore_suspend" / Int32ul,
"event_flags" / Int16ul,
"event_type" / Int16ul,
"num_kcontrols" / Int32ul,
"priv" / self._private,
)
self._dapm_widget_with_kcontrols = Struct(
"widget" / self._dapm_widget,
"kcontrols" / Array(this.widget.num_kcontrols, self._kcontrol_wrapper),
)
self._pcm = Struct( # snd_soc_tplg_pcm
"size" / Int32ul,
"pcm_name" / PaddedString(self._id_name_maxlen, "ascii"),
"dai_name" / PaddedString(self._id_name_maxlen, "ascii"),
"pcm_id" / Int32ul,
"dai_id" / Int32ul,
"playback" / Int32ul,
"capture" / Int32ul,
"compress" / Int32ul,
"stream" / Array(self._stream_config_max, self._stream),
"num_streams" / Int32ul,
"caps" / Array(2, self._stream_caps),
"flag_mask" / Int32ul,
"flags" / FlagsEnum(Int32ul, DaiLnkFlag),
"priv" / self._private,
)
self._link_config = Struct( # snd_soc_tplg_link_config
"size" / Int32ul,
"id" / Int32ul,
"name" / PaddedString(self._id_name_maxlen, "ascii"),
"stream_name" / PaddedString(self._id_name_maxlen, "ascii"),
"stream" / Array(self._stream_config_max, self._stream),
"num_streams" / Int32ul,
"hw_config" / Array(self._hw_config_max, self._hw_config),
"num_hw_configs" / Int32ul,
"default_hw_config_id" / Int32ul,
"flag_mask" / Int32ul,
"flags" / FlagsEnum(Int32ul, DaiLnkFlag),
"priv" / self._private,
)
self._section_blocks_cases = {
TplgType.MANIFEST.name: self._manifest,
TplgType.MIXER.name: self._kcontrol_wrapper,
TplgType.ENUM.name: self._kcontrol_wrapper,
TplgType.BYTES.name: self._kcontrol_wrapper,
TplgType.DAPM_GRAPH.name: self._dapm_graph_elem,
TplgType.DAPM_WIDGET.name: self._dapm_widget_with_kcontrols,
TplgType.PCM.name: self._pcm,
TplgType.CODEC_LINK.name: self._link_config,
TplgType.BACKEND_LINK.name: self._link_config,
}
_section_blocks_cases = dict((k, Array(this.header.count, v)) for (k, v) in self._section_blocks_cases.items())
self._section = Struct(
"header" / self._block_header,
"blocks" / Switch(
this.header.type,
_section_blocks_cases,
default = Padding(this.header.payload_size) # skip unknown blocks,
),
)
self.sections = FocusedSeq("sections",
"sections" / GreedyRange(self._section),
Terminated
)
def parse_file(self, filename: str) -> ListContainer:
"Parse the TPLG binary file."
return self.sections.parse_file(filename)
if __name__ == "__main__":
def parse_cmdline():
parser = argparse.ArgumentParser(add_help=True, formatter_class=argparse.RawTextHelpFormatter,
description='A Topology Reader totally Written in Python.')
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
parser.add_argument('-t', '--tplgroot', type=str, help="load tplg file from tplg_root folder")
parser.add_argument('-d', '--dump', type=str, help='dump specified topology information, '
'if multiple information types are wanted, use "," to separate them, eg, `-d pcm,graph`')
parser.add_argument('filename', type=str, help='topology file name(s), ' \
'if multiple topology file names are specified, please use "," to separate them')
# The below options are used to control generated graph
parser.add_argument('-D', '--directory', type=str, default=".", help="output directory for generated graph")
parser.add_argument('-F', '--format', type=str, default="png", help="output format for generated graph, check "
"https://graphviz.gitlab.io/_pages/doc/info/output.html for all supported formats")
parser.add_argument('-V', '--live_view', action="store_true", help="generate and view topology graph")
return vars(parser.parse_args())
cmd_args = parse_cmdline()
tplgFormat = TplgBinaryFormat()
tplgStruct = tplgFormat.parse_file(cmd_args['filename'])
print(tplgStruct)