1 // SPDX-License-Identifier: GPL-2.0-only
12 "review.coreboot.org/coreboot.git/util/hda-decoder/decoder"
17 var indentLevel
int = 0
19 func indentedPrintf(format
string, args
...interface{}) (n
int, err error
) {
20 s
:= fmt
.Sprintf("%s%s", strings
.Repeat("\t", indentLevel
), format
)
21 return fmt
.Printf(s
, args
...)
24 func stringToUint32(s
string) uint32 {
25 s
= strings
.Replace(s
, "0x", "", -1)
26 v
, err
:= strconv
.ParseUint(s
, 16, 32)
33 func decodeConfig(config
uint32) {
34 out
:= decoder
.ToHumanReadable(decoder
.Decode(config
))
36 indentedPrintf("%s,\n", out
.PortConnectivity
)
37 indentedPrintf("%s,\n", out
.Location
)
38 indentedPrintf("%s,\n", out
.DefaultDevice
)
39 indentedPrintf("%s,\n", out
.ConnectionType
)
40 indentedPrintf("%s,\n", out
.Color
)
41 indentedPrintf("%s,\n", out
.Misc
)
42 indentedPrintf("%s, %s\n", out
.DefaultAssociation
, out
.Sequence
)
45 func printDisconnectedPort(config
uint32) {
46 // The value 0x411111f0 is not defined in the specification, but is a
47 // common value vendors use to indicate "not connected".
48 const nc
uint32 = 0x411111f0
50 // Setting some values (e.g. 0x40000000) as `AZALIA_PIN_CFG_NC(0)` is
51 // probably harmless. However, we will stay on the safe side for now.
52 if (config
& 0xfffffff0) != nc
{
53 // Do not decode these values, as they would likely describe a
54 // bogus device which could be slighly confusing.
55 fmt
.Printf("0x%08x), // does not describe a jack or internal device\n", config
)
57 fmt
.Printf("AZALIA_PIN_CFG_NC(%d)),\n", (config
& 0x0000000f))
61 func decodeFile(path
string, codec
uint32) {
62 file
, err
:= os
.Open(path
)
68 scanner
:= bufio
.NewScanner(file
)
71 fields
:= strings
.Fields(scanner
.Text())
73 fmt
.Print("// Something went wrong\n")
77 pin
:= stringToUint32(fields
[0])
78 config
:= stringToUint32(fields
[1])
80 indentedPrintf("AZALIA_PIN_CFG(%d, 0x%02x, ", codec
, pin
)
81 if decoder
.PortIsConnected(config
) {
82 fmt
.Printf("AZALIA_PIN_DESC(\n")
86 indentedPrintf(")),\n")
88 printDisconnectedPort(config
)
93 func getFileContents(path
string) string {
94 contents
, err
:= os
.ReadFile(path
)
98 return strings
.TrimSpace(string(contents
))
101 func getLineCount(path
string) int {
102 return len(strings
.Split(getFileContents(path
), "\n"))
105 func decodeDeviceCodec(path
string, codec
uint32, isLastCodec
bool, generate
bool) {
107 vendorId
:= getFileContents(path
+ "/vendor_id")
108 vendorName
:= getFileContents(path
+ "/vendor_name")
109 chipName
:= getFileContents(path
+ "/chip_name")
110 subsystemId
:= getFileContents(path
+ "/subsystem_id")
111 lineCount
:= getLineCount(path
+ "/init_pin_configs")
113 indentedPrintf("%s, // Vendor/Device ID: %s %s\n", vendorId
, vendorName
, chipName
)
114 indentedPrintf("%s, // Subsystem ID\n", subsystemId
)
115 indentedPrintf("%d,\n", lineCount
+1)
116 indentedPrintf("AZALIA_SUBVENDOR(%d, %s),\n\n", codec
, subsystemId
)
119 decodeFile(path
+"/init_pin_configs", codec
)
125 func decodeDeviceCodecs(generate
bool) {
126 matches
, err
:= filepath
.Glob("/sys/class/sound/hwC0D*")
130 re
:= regexp
.MustCompile(`D([0-9]+)$`)
132 for i
, match
:= range matches
{
133 codec
:= stringToUint32(re
.FindStringSubmatch(match
)[1])
134 isLastCodec
:= (i
+ 1) == len(matches
)
136 decodeDeviceCodec(match
, codec
, isLastCodec
, generate
)
140 func isFlagPassed(name
string) bool {
143 flag
.Visit(func(f
*flag
.Flag
) {
152 codec
:= flag
.Uint64("codec", 0, "Set the codec number when decoding a file\n"+
153 "This flag is only meaningful in combination with the 'file' flag")
154 config
:= flag
.Uint64("config", 0, "Decode a single configuration")
155 file
:= flag
.String("file", "", "Decode configurations in a file\n"+
156 "The decoder assumes each line in the file has the format: <pin> <config>")
157 generate
:= flag
.Bool("generate", false, "Automatically generate hda_verb.c for the host device")
160 if isFlagPassed("config") {
161 decodeConfig(uint32(*config
))
162 } else if isFlagPassed("file") {
163 decodeFile(*file
, uint32(*codec
))
166 fmt
.Printf("/* SPDX-License-Identifier: GPL-2.0-only */\n\n")
167 fmt
.Printf("#include <device/azalia_device.h>\n\n")
168 fmt
.Printf("const u32 cim_verb_data[] = {\n")
171 decodeDeviceCodecs(*generate
)
175 fmt
.Printf("const u32 pc_beep_verbs[] = {};\n")
176 fmt
.Printf("AZALIA_ARRAY_SIZES;\n")