4 * Copyright (c) 2009 Precedence Technologies Ltd <support@precedence.co.uk>
5 * Copyright (c) 2009 Jared D. McNeill <jmcneill@invisible.ca>
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Precedence Technologies Ltd
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
35 #include <prop/proplib.h>
45 #include <dev/pci/hdaudio/hdaudioio.h>
46 #include <dev/pci/hdaudio/hdaudioreg.h>
48 #include "hdaudioctl.h"
50 #define DEVPATH_HDAUDIO "/dev/hdaudio0"
58 fprintf(stderr
, "usage: %s [-f dev] list\n", prog
);
59 fprintf(stderr
, " %s [-f dev] get <codecid> <nid>\n", prog
);
60 fprintf(stderr
, " %s [-f dev] set <codecid> <nid> [plist]\n",
62 fprintf(stderr
, " %s [-f dev] graph <codecid> <nid>\n", prog
);
67 hdaudioctl_list(int fd
)
69 prop_dictionary_t request
, response
;
70 prop_dictionary_t dict
;
71 prop_object_iterator_t iter
;
74 uint16_t nid
, codecid
;
75 uint16_t vendor
, product
;
77 const char *device
= NULL
;
80 request
= prop_dictionary_create();
81 if (request
== NULL
) {
82 fprintf(stderr
, "out of memory\n");
86 error
= prop_dictionary_sendrecv_ioctl(request
, fd
,
87 HDAUDIO_FGRP_INFO
, &response
);
89 perror("HDAUDIO_FGRP_INFO failed");
93 array
= prop_dictionary_get(response
, "function-group-info");
94 iter
= prop_array_iterator(array
);
95 prop_object_iterator_reset(iter
);
96 while ((obj
= prop_object_iterator_next(iter
)) != NULL
) {
97 dict
= (prop_dictionary_t
)obj
;
98 prop_dictionary_get_uint16(dict
, "codecid", &codecid
);
99 prop_dictionary_get_uint16(dict
, "nid", &nid
);
100 prop_dictionary_get_uint16(dict
, "vendor-id", &vendor
);
101 prop_dictionary_get_uint16(dict
, "product-id", &product
);
102 prop_dictionary_get_uint32(dict
, "subsystem-id", &subsystem
);
103 prop_dictionary_get_cstring_nocopy(dict
, "device", &device
);
105 printf("codecid 0x%02X nid 0x%02X vendor 0x%04X "
106 "product 0x%04X subsystem 0x%08X device %s\n",
107 codecid
, nid
, vendor
, product
, subsystem
,
108 device
? device
: "<none>");
111 prop_object_release(array
);
112 prop_object_release(response
);
113 prop_object_release(request
);
119 hdaudioctl_get(int fd
, int argc
, char *argv
[])
121 prop_dictionary_t request
, response
;
123 uint16_t nid
, codecid
;
130 codecid
= strtol(argv
[0], NULL
, 0);
131 nid
= strtol(argv
[1], NULL
, 0);
133 request
= prop_dictionary_create();
134 if (request
== NULL
) {
135 fprintf(stderr
, "out of memory\n");
139 prop_dictionary_set_uint16(request
, "codecid", codecid
);
140 prop_dictionary_set_uint16(request
, "nid", nid
);
142 error
= prop_dictionary_sendrecv_ioctl(request
, fd
,
143 HDAUDIO_FGRP_GETCONFIG
, &response
);
145 perror("HDAUDIO_FGRP_GETCONFIG failed");
149 config
= prop_dictionary_get(response
, "pin-config");
150 xml
= prop_array_externalize(config
);
154 prop_object_release(response
);
155 prop_object_release(request
);
161 hdaudioctl_set(int fd
, int argc
, char *argv
[])
163 prop_dictionary_t request
, response
;
164 prop_array_t config
= NULL
;
165 uint16_t nid
, codecid
;
168 if (argc
< 2 || argc
> 3)
171 codecid
= strtol(argv
[0], NULL
, 0);
172 nid
= strtol(argv
[1], NULL
, 0);
174 config
= prop_array_internalize_from_file(argv
[2]);
175 if (config
== NULL
) {
177 "couldn't load configuration from %s\n", argv
[2]);
182 request
= prop_dictionary_create();
183 if (request
== NULL
) {
184 fprintf(stderr
, "out of memory\n");
188 prop_dictionary_set_uint16(request
, "codecid", codecid
);
189 prop_dictionary_set_uint16(request
, "nid", nid
);
191 prop_dictionary_set(request
, "pin-config", config
);
193 error
= prop_dictionary_sendrecv_ioctl(request
, fd
,
194 HDAUDIO_FGRP_SETCONFIG
, &response
);
196 perror("HDAUDIO_FGRP_SETCONFIG failed");
200 prop_object_release(response
);
201 prop_object_release(request
);
208 main(int argc
, char *argv
[])
212 const char *devpath
= DEVPATH_HDAUDIO
;
214 while ((ch
= getopt(argc
, argv
, "f:h")) != -1) {
217 devpath
= strdup(optarg
);
231 fd
= open(devpath
, O_RDWR
);
233 fprintf(stderr
, "Error opening %s: %s\n", devpath
,
239 if (strcmp(argv
[0], "list") == 0)
240 error
= hdaudioctl_list(fd
);
241 else if (strcmp(argv
[0], "get") == 0)
242 error
= hdaudioctl_get(fd
, argc
- 1, argv
+ 1);
243 else if (strcmp(argv
[0], "set") == 0)
244 error
= hdaudioctl_set(fd
, argc
- 1, argv
+ 1);
245 else if (strcmp(argv
[0], "graph") == 0)
246 error
= hdaudioctl_graph(fd
, argc
- 1, argv
+ 1);