mb/starlabs/{lite_adl,byte_adl}: Don't select MAINBOARD_HAS_TPM2
[coreboot2.git] / util / hda-decoder / main.go
blobd686f199675f03d84a8b702edb8401fc669edc45
1 // SPDX-License-Identifier: GPL-2.0-only
2 package main
4 import (
5 "bufio"
6 "flag"
7 "fmt"
8 "log"
9 "os"
10 "path/filepath"
11 "regexp"
12 "review.coreboot.org/coreboot.git/util/hda-decoder/decoder"
13 "strconv"
14 "strings"
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)
27 if err != nil {
28 log.Fatal(err)
30 return uint32(v)
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)
56 } else {
57 fmt.Printf("AZALIA_PIN_CFG_NC(%d)),\n", (config & 0x0000000f))
61 func decodeFile(path string, codec uint32) {
62 file, err := os.Open(path)
63 if err != nil {
64 log.Fatal(err)
66 defer file.Close()
68 scanner := bufio.NewScanner(file)
70 for scanner.Scan() {
71 fields := strings.Fields(scanner.Text())
72 if len(fields) != 2 {
73 fmt.Print("// Something went wrong\n")
74 continue
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")
83 indentLevel += 1
84 decodeConfig(config)
85 indentLevel -= 1
86 indentedPrintf(")),\n")
87 } else {
88 printDisconnectedPort(config)
93 func getFileContents(path string) string {
94 contents, err := os.ReadFile(path)
95 if err != nil {
96 log.Fatal(err)
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) {
106 if generate {
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)
120 if !isLastCodec {
121 fmt.Printf("\n")
125 func decodeDeviceCodecs(generate bool) {
126 matches, err := filepath.Glob("/sys/class/sound/hwC0D*")
127 if err != nil {
128 log.Fatal(err)
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 {
141 found := false
143 flag.Visit(func(f *flag.Flag) {
144 if f.Name == name {
145 found = true
148 return found
151 func main() {
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")
158 flag.Parse()
160 if isFlagPassed("config") {
161 decodeConfig(uint32(*config))
162 } else if isFlagPassed("file") {
163 decodeFile(*file, uint32(*codec))
164 } else {
165 if *generate {
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")
169 indentLevel += 1
171 decodeDeviceCodecs(*generate)
172 if *generate {
173 indentLevel -= 1
174 fmt.Printf("};\n\n")
175 fmt.Printf("const u32 pc_beep_verbs[] = {};\n")
176 fmt.Printf("AZALIA_ARRAY_SIZES;\n")