summaryrefslogtreecommitdiff
path: root/usr.sbin/hdaudioctl/hdaudioctl.c
blob: 04429651b50e5e15372833abaf27312c817ecf56 (plain)
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
/* $NetBSD: hdaudioctl.c,v 1.2.18.1 2020/06/20 16:08:57 martin Exp $ */

/*
 * Copyright (c) 2009 Precedence Technologies Ltd <support@precedence.co.uk>
 * Copyright (c) 2009 Jared D. McNeill <jmcneill@invisible.ca>
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Precedence Technologies Ltd
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/ioctl.h>

#include <prop/proplib.h>

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>

#include <dev/hdaudio/hdaudioio.h>
#include <dev/hdaudio/hdaudioreg.h>

#include "hdaudioctl.h"

#define DEVPATH_HDAUDIO	"/dev/hdaudio0"

const char *pin_devices[16] = {
	"Line out", "Speaker", "Headphones", "CD",
	"SPDIF Out", "Digital Out", "Modem Line", "Modem Handset",
	"Line In", "AUX", "Mic In", "Telephony",
	"SPDIF In", "Digital In", "Reserved", "Other"
};
static const char *pin_jacks[16] = {
	"Unknown", "1/8\"", "1/4\"", "ATAPI",
	"RCA", "Optic", "Digital", "Analog",
	"DIN", "XLR", "RJ-11", "Combo",
	"0xC", "0xD", "0xE", "Other"
};
static const char *pin_connections[4] = {
	"Jack", "None", "Fixed", "Both"
};
static const char *pin_colors[16] = {
	"Unknown", "Black", "Grey", "Blue",
	"Green", "Red", "Orange", "Yellow",
	"Purple", "Pink", "Res. A", "Res. B",
	"Res. C", "Res. D", "White", "Other"
};
static const char *pin_locations[64] = {
	"0x00", "Rear", "Front", "Left",
	"Right", "Top", "Bottom", "Rear-panel",
	"Drive-bay", "0x09", "0x0a", "0x0b",
	"0x0c", "0x0d", "0x0e", "0x0f",
	"Internal", "0x11", "0x12", "0x13",
	"0x14", "0x15", "0x16", "Riser",
	"0x18", "Onboard", "0x1a", "0x1b",
	"0x1c", "0x1d", "0x1e", "0x1f",
	"External", "Ext-Rear", "Ext-Front", "Ext-Left",
	"Ext-Right", "Ext-Top", "Ext-Bottom", "0x07",
	"0x28", "0x29", "0x2a", "0x2b",
	"0x2c", "0x2d", "0x2e", "0x2f",
	"Other", "0x31", "0x32", "0x33",
	"0x34", "0x35", "Other-Bott", "Lid-In",
	"Lid-Out", "0x39", "0x3a", "0x3b",
	"0x3c", "0x3d", "0x3e", "0x3f"
};

void
usage(void)
{
	const char *prog;
	prog = getprogname();

	fprintf(stderr, "usage: %s [-f dev] list\n", prog);
	fprintf(stderr, "       %s [-f dev] show <codecid> <nid>\n", prog);
	fprintf(stderr, "       %s [-f dev] get <codecid> <nid>\n", prog);
	fprintf(stderr, "       %s [-f dev] set <codecid> <nid> [plist]\n",
	    prog);
	fprintf(stderr, "       %s [-f dev] graph <codecid> <nid>\n", prog);
	exit(EXIT_FAILURE);
}

static int
hdaudioctl_list(int fd)
{
	prop_dictionary_t request, response;
	prop_dictionary_t dict;
	prop_object_iterator_t iter;
	prop_object_t obj;
	prop_array_t array;
	uint16_t nid, codecid;
	uint16_t vendor, product;
	uint32_t subsystem;
	const char *device = NULL;
	int error;

	request = prop_dictionary_create();
	if (request == NULL) {
		fprintf(stderr, "out of memory\n");
		return ENOMEM;
	}

	error = prop_dictionary_sendrecv_ioctl(request, fd,
	    HDAUDIO_FGRP_INFO, &response);
	if (error != 0) {
		perror("HDAUDIO_FGRP_INFO failed");
		return error;
	}

	array = prop_dictionary_get(response, "function-group-info");
	iter = prop_array_iterator(array);
	prop_object_iterator_reset(iter);
	while ((obj = prop_object_iterator_next(iter)) != NULL) {
		dict = (prop_dictionary_t)obj;
		prop_dictionary_get_uint16(dict, "codecid", &codecid);
		prop_dictionary_get_uint16(dict, "nid", &nid);
		prop_dictionary_get_uint16(dict, "vendor-id", &vendor);
		prop_dictionary_get_uint16(dict, "product-id", &product);
		prop_dictionary_get_uint32(dict, "subsystem-id", &subsystem);
		prop_dictionary_get_cstring_nocopy(dict, "device", &device);

		printf("codecid 0x%02X nid 0x%02X vendor 0x%04X "
		    "product 0x%04X subsystem 0x%08X device %s\n",
		    codecid, nid, vendor, product, subsystem,
		    device ? device : "<none>");
	}

	prop_object_release(array);
	prop_object_release(response);
	prop_object_release(request);

	return 0;
}

static int
hdaudioctl_get(int fd, int argc, char *argv[])
{
	prop_dictionary_t request, response;
	prop_array_t config;
	uint16_t nid, codecid;
	const char *xml;
	int error;

	if (argc != 2)
		usage();

	codecid = strtol(argv[0], NULL, 0);
	nid = strtol(argv[1], NULL, 0);

	request = prop_dictionary_create();
	if (request == NULL) {
		fprintf(stderr, "out of memory\n");
		return ENOMEM;
	}

	prop_dictionary_set_uint16(request, "codecid", codecid);
	prop_dictionary_set_uint16(request, "nid", nid);

	error = prop_dictionary_sendrecv_ioctl(request, fd,
	    HDAUDIO_FGRP_GETCONFIG, &response);
	if (error != 0) {
		perror("HDAUDIO_FGRP_GETCONFIG failed");
		return error;
	}

	config = prop_dictionary_get(response, "pin-config");
	xml = prop_array_externalize(config);

	printf("%s\n", xml);

	prop_object_release(response);
	prop_object_release(request);

	return 0;
}

static int
hdaudioctl_set(int fd, int argc, char *argv[])
{
	prop_dictionary_t request, response;
	prop_array_t config = NULL;
	uint16_t nid, codecid;
	int error;

	if (argc < 2 || argc > 3)
		usage();

	codecid = strtol(argv[0], NULL, 0);
	nid = strtol(argv[1], NULL, 0);
	if (argc == 3) {
		config = prop_array_internalize_from_file(argv[2]);
		if (config == NULL) {
			fprintf(stderr,
			    "couldn't load configuration from %s\n", argv[2]);
			return EIO;
		}
	}

	request = prop_dictionary_create();
	if (request == NULL) {
		fprintf(stderr, "out of memory\n");
		return ENOMEM;
	}

	prop_dictionary_set_uint16(request, "codecid", codecid);
	prop_dictionary_set_uint16(request, "nid", nid);
	if (config)
		prop_dictionary_set(request, "pin-config", config);

	error = prop_dictionary_sendrecv_ioctl(request, fd,
	    HDAUDIO_FGRP_SETCONFIG, &response);
	if (error != 0) {
		perror("HDAUDIO_FGRP_SETCONFIG failed");
		return error;
	}

	prop_object_release(response);
	prop_object_release(request);

	return 0;
}

/* Based on page 178 onwards:
 * https://www.intel.com/content/dam/www/public/us/en/documents/product-specifications/high-definition-audio-specification.pdf
 * 31:30 = Port connectivity
 * 29:24 = Location
 * 23:20 = Default device
 * 19:16 = Connection type
 * 15:12 = Color
 *  11:8 = Misc
 *   7:4 = Default association
 *   3:0 = Sequence
 */

static int
hdaudioctl_show(int fd, int argc, char *argv[])
{
	prop_dictionary_t request, response, dict;
	prop_array_t array;
	prop_object_iterator_t iter;
	prop_object_t obj;
	uint16_t nid, codecid;
	uint32_t config;
	int error;
	const char *device, *conn, *jack, *loc, *color;
	if (argc != 2)
		usage();

	codecid = strtol(argv[0], NULL, 0);
	nid = strtol(argv[1], NULL, 0);

	request = prop_dictionary_create();
	if (request == NULL) {
		fprintf(stderr, "out of memory\n");
		return ENOMEM;
	}

	prop_dictionary_set_uint16(request, "codecid", codecid);
	prop_dictionary_set_uint16(request, "nid", nid);

	error = prop_dictionary_sendrecv_ioctl(request, fd,
	    HDAUDIO_FGRP_GETCONFIG, &response);
	if (error != 0) {
		perror("HDAUDIO_FGRP_GETCONFIG failed");
		return error;
	}

	array = prop_dictionary_get(response, "pin-config");
	iter = prop_array_iterator(array);
	prop_object_iterator_reset(iter);
	printf("nid Data     As Seq Device         Conn  Jack    "
	    "Location   Color   Misc\n");
	printf("================================================="
	    "=======================\n");
	while ((obj = prop_object_iterator_next(iter)) != NULL) {
		dict = (prop_dictionary_t)obj;
		prop_dictionary_get_uint32(dict, "config", &config);
		prop_dictionary_get_uint16(dict, "nid", &nid);
		device = pin_devices[(config >> 20U) & 0xf];
		conn = pin_connections[(config >> 30U) & 0x3];
		jack = pin_jacks[(config >> 16) & 0xf];
		loc = pin_locations[(config >> 24) & 0x3f];
		color = pin_colors[(config >> 12) & 0xf];
		printf("%3d %08X %2d %3d %-14s %-5s %-7s %-10s %-7s %4X\n",
		    nid, config, ((config >> 4U) & 0xf), (config & 0xf),
		    device, conn, jack, loc, color, ((config >> 8U) & 0xf));
	}
	prop_object_release(array);
	prop_object_release(response);
	prop_object_release(request);

	return 0;
}


int
main(int argc, char *argv[])
{
	int fd, error;
	int ch;
	const char *devpath = DEVPATH_HDAUDIO;

	while ((ch = getopt(argc, argv, "f:h")) != -1) {
		switch (ch) {
		case 'f':
			devpath = strdup(optarg);
			break;
		case 'h':
		default:
			usage();
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	if (argc < 1)
		usage();

	fd = open(devpath, O_RDWR);
	if (fd < 0) {
		fprintf(stderr, "Error opening %s: %s\n", devpath,
		    strerror(errno));
		return EXIT_FAILURE;
	}

	error = 0;
	if (strcmp(argv[0], "list") == 0)
		error = hdaudioctl_list(fd);
	else if (strcmp(argv[0], "get") == 0)
		error = hdaudioctl_get(fd, argc - 1, argv + 1);
	else if (strcmp(argv[0], "set") == 0)
		error = hdaudioctl_set(fd, argc - 1, argv + 1);
	else if (strcmp(argv[0], "graph") == 0)
		error = hdaudioctl_graph(fd, argc - 1, argv + 1);
	else if (strcmp(argv[0], "show") == 0)
		error = hdaudioctl_show(fd, argc - 1, argv + 1);
	else
		usage();

	close(fd);

	if (error)
		return EXIT_FAILURE;
	return EXIT_SUCCESS;
}