xref: /netbsd-src/usr.sbin/hdaudioctl/graph.c (revision 31510453944a10501ce606a0b8c7e5d36d433e6e)
1*31510453Schristos /* $NetBSD: graph.c,v 1.6 2021/06/21 03:09:52 christos Exp $ */
2f549e36eSsborrill 
3f549e36eSsborrill /*
4f549e36eSsborrill  * Copyright (c) 2009 Precedence Technologies Ltd <support@precedence.co.uk>
5f549e36eSsborrill  * Copyright (c) 2009 Jared D. McNeill <jmcneill@invisible.ca>
6f549e36eSsborrill  * All rights reserved.
7f549e36eSsborrill  *
8f549e36eSsborrill  * This code is derived from software contributed to The NetBSD Foundation
9f549e36eSsborrill  * by Precedence Technologies Ltd
10f549e36eSsborrill  *
11f549e36eSsborrill  * Redistribution and use in source and binary forms, with or without
12f549e36eSsborrill  * modification, are permitted provided that the following conditions
13f549e36eSsborrill  * are met:
14f549e36eSsborrill  * 1. Redistributions of source code must retain the above copyright
15f549e36eSsborrill  *    notice, this list of conditions and the following disclaimer.
16f549e36eSsborrill  * 2. The name of the author may not be used to endorse or promote products
17f549e36eSsborrill  *    derived from this software without specific prior written permission.
18f549e36eSsborrill  *
19f549e36eSsborrill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20f549e36eSsborrill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21f549e36eSsborrill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22f549e36eSsborrill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23f549e36eSsborrill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24f549e36eSsborrill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25f549e36eSsborrill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26f549e36eSsborrill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27f549e36eSsborrill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28f549e36eSsborrill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29f549e36eSsborrill  * SUCH DAMAGE.
30f549e36eSsborrill  */
31f549e36eSsborrill 
32f549e36eSsborrill #include <sys/types.h>
33f549e36eSsborrill #include <sys/ioctl.h>
34f549e36eSsborrill 
35f549e36eSsborrill #include <prop/proplib.h>
36f549e36eSsborrill 
37f549e36eSsborrill #include <errno.h>
38f549e36eSsborrill #include <fcntl.h>
39f549e36eSsborrill #include <stdio.h>
40f549e36eSsborrill #include <stdlib.h>
41f549e36eSsborrill #include <unistd.h>
42f549e36eSsborrill 
438a9ff04bSjmcneill #include <dev/hdaudio/hdaudioio.h>
448a9ff04bSjmcneill #include <dev/hdaudio/hdaudioreg.h>
45f549e36eSsborrill 
46f549e36eSsborrill #include "hdaudioctl.h"
47f549e36eSsborrill 
48f549e36eSsborrill int
hdaudioctl_graph(int fd,int argc,char * argv[])49f549e36eSsborrill hdaudioctl_graph(int fd, int argc, char *argv[])
50f549e36eSsborrill {
51f549e36eSsborrill 	prop_dictionary_t request, response;
52f549e36eSsborrill 	prop_object_iterator_t iter;
53f549e36eSsborrill 	prop_number_t nnid;
54f549e36eSsborrill 	prop_array_t connlist;
55f549e36eSsborrill 	const char *name;
56f549e36eSsborrill 	int error, index;
57f549e36eSsborrill 	uint32_t cap, config;
58f549e36eSsborrill 	uint16_t reqnid, reqcodecid;
59f549e36eSsborrill 	uint16_t vendor, product;
60f549e36eSsborrill 	uint8_t type, nid;
61f549e36eSsborrill 	char buf[10] = "??h";
62f549e36eSsborrill 
63f549e36eSsborrill 	if (argc != 2)
64f549e36eSsborrill 		usage();
65f549e36eSsborrill 
66f549e36eSsborrill 	reqcodecid = strtol(argv[0], NULL, 0);
67f549e36eSsborrill 	reqnid = strtol(argv[1], NULL, 0);
68f549e36eSsborrill 
69f549e36eSsborrill 	request = prop_dictionary_create();
70f549e36eSsborrill 	if (request == NULL) {
71f549e36eSsborrill 		fprintf(stderr, "out of memory\n");
72f549e36eSsborrill 		return ENOMEM;
73f549e36eSsborrill 	}
74f549e36eSsborrill 
75f549e36eSsborrill 	prop_dictionary_set_uint16(request, "codecid", reqcodecid);
76f549e36eSsborrill 	prop_dictionary_set_uint16(request, "nid", reqnid);
77f549e36eSsborrill 
78f549e36eSsborrill 	error = prop_dictionary_sendrecv_ioctl(request, fd,
79f549e36eSsborrill 	    HDAUDIO_FGRP_CODEC_INFO, &response);
80f549e36eSsborrill 	if (error != 0) {
81f549e36eSsborrill 		perror("HDAUDIO_FGRP_CODEC_INFO failed");
82f549e36eSsborrill 		prop_object_release(request);
83f549e36eSsborrill 		return error;
84f549e36eSsborrill 	}
85f549e36eSsborrill 
86f549e36eSsborrill 	prop_dictionary_get_uint16(response, "vendor-id", &vendor);
87f549e36eSsborrill 	prop_dictionary_get_uint16(response, "product-id", &product);
88f549e36eSsborrill 
89f549e36eSsborrill 	printf("digraph \"HD Audio %04X:%04X\" {\n",
90f549e36eSsborrill 	    vendor, product);
91f549e36eSsborrill 
92f549e36eSsborrill 	for (index = 0;; index++) {
93f549e36eSsborrill 		prop_dictionary_set_uint16(request, "index", index);
94f549e36eSsborrill 		error = prop_dictionary_sendrecv_ioctl(request, fd,
95f549e36eSsborrill 		    HDAUDIO_FGRP_WIDGET_INFO, &response);
96f549e36eSsborrill 		if (error != 0)
97f549e36eSsborrill 			break;
98*31510453Schristos 		prop_dictionary_get_string(response, "name", &name);
99f549e36eSsborrill 		prop_dictionary_get_uint32(response, "cap", &cap);
100f549e36eSsborrill 		prop_dictionary_get_uint32(response, "config", &config);
101f549e36eSsborrill 		prop_dictionary_get_uint8(response, "type", &type);
102f549e36eSsborrill 		prop_dictionary_get_uint8(response, "nid", &nid);
103f549e36eSsborrill 
104f549e36eSsborrill 		sprintf(buf, "widget%02Xh", nid);
105f549e36eSsborrill 
106f549e36eSsborrill 		switch (type) {
107f549e36eSsborrill 		case COP_AWCAP_TYPE_AUDIO_OUTPUT:
10804588613Sjoerg 			printf(" %s [label=\"%s\\naudio output\",shape=box,style=filled,fillcolor=\""
10904588613Sjoerg 			    "#88ff88\"];\n", buf, buf);
110f549e36eSsborrill 			break;
111f549e36eSsborrill 		case COP_AWCAP_TYPE_AUDIO_INPUT:
11204588613Sjoerg 			printf(" %s [label=\"%s\\naudio input\",shape=box,style=filled,fillcolor=\""
11304588613Sjoerg 			    "#ff8888\"];\n", buf, buf);
114f549e36eSsborrill 			break;
115f549e36eSsborrill 		case COP_AWCAP_TYPE_AUDIO_MIXER:
11604588613Sjoerg 			printf(" %s [label=\"%s\\naudio mixer\","
11704588613Sjoerg 			    "shape=invhouse];\n", buf, buf);
118f549e36eSsborrill 			break;
119f549e36eSsborrill 		case COP_AWCAP_TYPE_AUDIO_SELECTOR:
12004588613Sjoerg 			printf(" %s [label=\"%s\\naudio selector\","
12104588613Sjoerg 			    "shape=invtrapezium];\n", buf, buf);
122f549e36eSsborrill 			break;
123f549e36eSsborrill 		case COP_AWCAP_TYPE_PIN_COMPLEX:
124f549e36eSsborrill 			printf(" %s [label=\"%s\\ndevice=%s\",style=filled",
125f549e36eSsborrill 			    buf, buf,
126f549e36eSsborrill 			    pin_devices[COP_CFG_DEFAULT_DEVICE(config)]);
127f549e36eSsborrill 			if (cap & COP_PINCAP_OUTPUT_CAPABLE &&
128f549e36eSsborrill 			    cap & COP_PINCAP_INPUT_CAPABLE)
12904588613Sjoerg 				puts(",shape=doublecircle,fillcolor=\""
13004588613Sjoerg 				    "#ffff88\"];");
131f549e36eSsborrill 			else if (cap & COP_PINCAP_OUTPUT_CAPABLE)
13204588613Sjoerg 				puts(",shape=circle,fillcolor=\"#88ff88\"];");
133f549e36eSsborrill 			else if (cap & COP_PINCAP_INPUT_CAPABLE)
13404588613Sjoerg 				puts(",shape=circle,fillcolor=\"#ff8888\"];");
135f549e36eSsborrill 			else
13604588613Sjoerg 				puts(",shape=circle,fillcolor=\"#888888\"];");
13704588613Sjoerg 			break;
13804588613Sjoerg 		case COP_AWCAP_TYPE_POWER_WIDGET:
13904588613Sjoerg 			printf(" %s [label=\"%s\\npower widget\","
14004588613Sjoerg 			    "shape=box];\n", buf, buf);
14104588613Sjoerg 			break;
14204588613Sjoerg 		case COP_AWCAP_TYPE_VOLUME_KNOB:
14304588613Sjoerg 			printf(" %s [label=\"%s\\nvolume knob\","
14404588613Sjoerg 			    "shape=box];\n", buf, buf);
14504588613Sjoerg 			break;
14604588613Sjoerg 		case COP_AWCAP_TYPE_BEEP_GENERATOR:
14704588613Sjoerg 			printf(" %s [label=\"%s\\nbeep generator\","
14804588613Sjoerg 			    "shape=box];\n", buf, buf);
14904588613Sjoerg 			break;
15004588613Sjoerg 		case COP_AWCAP_TYPE_VENDOR_DEFINED:
15104588613Sjoerg 			printf(" %s [label=\"%s\\nvendor defined\","
15204588613Sjoerg 			    "shape=box];\n", buf, buf);
153f549e36eSsborrill 			break;
154f549e36eSsborrill 		}
155f549e36eSsborrill 		connlist = prop_dictionary_get(response, "connlist");
156f549e36eSsborrill 		if (connlist == NULL)
157f549e36eSsborrill 			goto next;
158f549e36eSsborrill 		iter = prop_array_iterator(connlist);
159f549e36eSsborrill 		prop_object_iterator_reset(iter);
160f549e36eSsborrill 		while ((nnid = prop_object_iterator_next(iter)) != NULL) {
161bba3c683Sthorpej 			nid = prop_number_unsigned_value(nnid);
162f549e36eSsborrill 			printf(" widget%02Xh -> %s [sametail=widget%02Xh];\n",
163f549e36eSsborrill 			    nid, buf, nid);
164f549e36eSsborrill 		}
165f549e36eSsborrill 		prop_object_iterator_release(iter);
166f549e36eSsborrill next:
167f549e36eSsborrill 		prop_object_release(response);
168f549e36eSsborrill 	}
169f549e36eSsborrill 
170f549e36eSsborrill 	printf(" {rank=min;");
171f549e36eSsborrill 	for (index = 0;; index++) {
172f549e36eSsborrill 		prop_dictionary_set_uint16(request, "index", index);
173f549e36eSsborrill 		error = prop_dictionary_sendrecv_ioctl(request, fd,
174f549e36eSsborrill 		    HDAUDIO_AFG_WIDGET_INFO, &response);
175f549e36eSsborrill 		if (error != 0)
176f549e36eSsborrill 			break;
177*31510453Schristos 		prop_dictionary_get_string(response, "name", &name);
178f549e36eSsborrill 		prop_dictionary_get_uint8(response, "type", &type);
179f549e36eSsborrill 		prop_dictionary_get_uint8(response, "nid", &nid);
180f549e36eSsborrill 
181f549e36eSsborrill 		sprintf(buf, "widget%02Xh", nid);
182f549e36eSsborrill 
183f549e36eSsborrill 		switch (type) {
184f549e36eSsborrill 		case COP_AWCAP_TYPE_AUDIO_OUTPUT:
185f549e36eSsborrill 		case COP_AWCAP_TYPE_AUDIO_INPUT:
186f549e36eSsborrill 			printf(" %s;", buf);
187f549e36eSsborrill 			break;
188f549e36eSsborrill 		}
189f549e36eSsborrill 		prop_object_release(response);
190f549e36eSsborrill 	}
191f549e36eSsborrill 	printf("}\n");
192f549e36eSsborrill 
193f549e36eSsborrill 	printf(" {rank=max;");
194f549e36eSsborrill 	for (index = 0;; index++) {
195f549e36eSsborrill 		prop_dictionary_set_uint16(request, "index", index);
196f549e36eSsborrill 		error = prop_dictionary_sendrecv_ioctl(request, fd,
197f549e36eSsborrill 		    HDAUDIO_AFG_WIDGET_INFO, &response);
198f549e36eSsborrill 		if (error != 0)
199f549e36eSsborrill 			break;
200*31510453Schristos 		prop_dictionary_get_string(response, "name", &name);
201f549e36eSsborrill 		prop_dictionary_get_uint8(response, "type", &type);
202f549e36eSsborrill 		prop_dictionary_get_uint8(response, "nid", &nid);
203f549e36eSsborrill 
204f549e36eSsborrill 		sprintf(buf, "widget%02Xh", nid);
205f549e36eSsborrill 
206f549e36eSsborrill 		switch (type) {
207f549e36eSsborrill 		case COP_AWCAP_TYPE_PIN_COMPLEX:
208f549e36eSsborrill 			printf(" %s;", buf);
209f549e36eSsborrill 			break;
210f549e36eSsborrill 		}
211f549e36eSsborrill 		prop_object_release(response);
212f549e36eSsborrill 	}
213f549e36eSsborrill 	printf("}\n");
214f549e36eSsborrill 
215f549e36eSsborrill 	printf("}\n");
216f549e36eSsborrill 
217f549e36eSsborrill 	prop_object_release(request);
218f549e36eSsborrill 
219f549e36eSsborrill 	return 0;
220f549e36eSsborrill }
221