audiodev.c revision 1.14 1 1.14 isaki /* $NetBSD: audiodev.c,v 1.14 2019/08/24 06:32:25 isaki Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.1 mrg * Copyright (c) 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.1 mrg * Redistribution and use in source and binary forms, with or without
8 1.1 mrg * modification, are permitted provided that the following conditions
9 1.1 mrg * are met:
10 1.1 mrg * 1. Redistributions of source code must retain the above copyright
11 1.1 mrg * notice, this list of conditions and the following disclaimer.
12 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mrg * notice, this list of conditions and the following disclaimer in the
14 1.1 mrg * documentation and/or other materials provided with the distribution.
15 1.1 mrg *
16 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 mrg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 mrg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 mrg * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 mrg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 mrg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 mrg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 mrg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 mrg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 mrg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 mrg * POSSIBILITY OF SUCH DAMAGE.
27 1.1 mrg */
28 1.1 mrg
29 1.1 mrg #include <sys/queue.h>
30 1.1 mrg #include <sys/ioctl.h>
31 1.1 mrg #include <sys/stat.h>
32 1.1 mrg #include <sys/drvctlio.h>
33 1.1 mrg
34 1.12 isaki #include <err.h>
35 1.7 isaki #include <errno.h>
36 1.1 mrg #include <fcntl.h>
37 1.1 mrg #include <paths.h>
38 1.1 mrg #include <stdio.h>
39 1.1 mrg #include <stdlib.h>
40 1.1 mrg #include <string.h>
41 1.1 mrg #include <unistd.h>
42 1.1 mrg
43 1.1 mrg #include "audiodev.h"
44 1.1 mrg #include "drvctl.h"
45 1.2 jmcneill #include "dtmf.h"
46 1.1 mrg
47 1.9 isaki static int audiodev_test_chmask(struct audiodev *, unsigned int,
48 1.9 isaki audio_info_t *);
49 1.9 isaki
50 1.1 mrg static TAILQ_HEAD(audiodevhead, audiodev) audiodevlist =
51 1.1 mrg TAILQ_HEAD_INITIALIZER(audiodevlist);
52 1.10 isaki static unsigned int maxunit;
53 1.1 mrg
54 1.1 mrg static int
55 1.1 mrg audiodev_getinfo(struct audiodev *adev)
56 1.1 mrg {
57 1.1 mrg struct stat st;
58 1.7 isaki struct audiofmt *f;
59 1.7 isaki audio_format_query_t query;
60 1.7 isaki int i;
61 1.1 mrg
62 1.7 isaki if (stat(adev->ctlpath, &st) == -1)
63 1.1 mrg return -1;
64 1.1 mrg adev->dev = st.st_rdev;
65 1.1 mrg
66 1.7 isaki if (stat(_PATH_AUDIOCTL, &st) != -1 && st.st_rdev == adev->dev)
67 1.1 mrg adev->defaultdev = true;
68 1.1 mrg
69 1.8 isaki adev->ctlfd = open(adev->ctlpath, O_RDONLY);
70 1.8 isaki if (adev->ctlfd == -1) {
71 1.6 mrg return -1;
72 1.6 mrg }
73 1.8 isaki if (ioctl(adev->ctlfd, AUDIO_GETDEV, &adev->audio_device) == -1) {
74 1.8 isaki close(adev->ctlfd);
75 1.1 mrg return -1;
76 1.1 mrg }
77 1.1 mrg
78 1.7 isaki for (i = 0; ;i++) {
79 1.7 isaki memset(&query, 0, sizeof(query));
80 1.7 isaki query.index = i;
81 1.8 isaki if (ioctl(adev->ctlfd, AUDIO_QUERYFORMAT, &query) == -1) {
82 1.7 isaki if (errno == ENODEV) {
83 1.7 isaki /* QUERYFORMAT not supported. */
84 1.7 isaki break;
85 1.7 isaki }
86 1.7 isaki if (errno == EINVAL)
87 1.7 isaki break;
88 1.8 isaki close(adev->ctlfd);
89 1.7 isaki return -1;
90 1.7 isaki }
91 1.7 isaki
92 1.7 isaki f = calloc(1, sizeof(*f));
93 1.7 isaki f->fmt = query.fmt;
94 1.7 isaki TAILQ_INSERT_TAIL(&adev->formats, f, next);
95 1.7 isaki }
96 1.7 isaki
97 1.8 isaki if (ioctl(adev->ctlfd, AUDIO_GETFORMAT, &adev->hwinfo) == -1) {
98 1.8 isaki close(adev->ctlfd);
99 1.7 isaki return -1;
100 1.7 isaki }
101 1.2 jmcneill
102 1.1 mrg return 0;
103 1.1 mrg }
104 1.1 mrg
105 1.1 mrg static int
106 1.3 jmcneill audiodev_add(const char *pdev, const char *dev, unsigned int unit)
107 1.1 mrg {
108 1.1 mrg struct audiodev *adev;
109 1.1 mrg
110 1.1 mrg adev = calloc(1, sizeof(*adev));
111 1.1 mrg if (adev == NULL)
112 1.1 mrg return -1;
113 1.1 mrg
114 1.3 jmcneill strlcpy(adev->pxname, pdev, sizeof(adev->pxname));
115 1.1 mrg strlcpy(adev->xname, dev, sizeof(adev->xname));
116 1.7 isaki snprintf(adev->path, sizeof(adev->path), "/dev/%s", dev);
117 1.7 isaki snprintf(adev->ctlpath, sizeof(adev->ctlpath), "/dev/audioctl%d", unit);
118 1.1 mrg adev->unit = unit;
119 1.7 isaki TAILQ_INIT(&adev->formats);
120 1.1 mrg
121 1.1 mrg if (audiodev_getinfo(adev) == -1) {
122 1.1 mrg free(adev);
123 1.1 mrg return -1;
124 1.1 mrg }
125 1.1 mrg
126 1.1 mrg #ifdef DEBUG
127 1.7 isaki printf("DEBUG: [%c] %s(%s): %s\n", adev->defaultdev ? '*' : ' ',
128 1.7 isaki adev->path, adev->ctlpath, adev->audio_device.name);
129 1.7 isaki struct audiofmt *f;
130 1.7 isaki TAILQ_FOREACH(f, &adev->formats, next) {
131 1.7 isaki printf("DEBUG: enc%d, %d/%d, %dch\n",
132 1.7 isaki f->fmt.encoding,
133 1.7 isaki f->fmt.validbits,
134 1.7 isaki f->fmt.precision,
135 1.7 isaki f->fmt.channels);
136 1.7 isaki }
137 1.1 mrg #endif
138 1.1 mrg
139 1.1 mrg TAILQ_INSERT_TAIL(&audiodevlist, adev, next);
140 1.1 mrg
141 1.10 isaki if (unit > maxunit)
142 1.10 isaki maxunit = unit;
143 1.10 isaki
144 1.1 mrg return 0;
145 1.1 mrg }
146 1.1 mrg
147 1.1 mrg static void
148 1.3 jmcneill audiodev_cb(void *args, const char *pdev, const char *dev, unsigned int unit)
149 1.1 mrg {
150 1.3 jmcneill audiodev_add(pdev, dev, unit);
151 1.1 mrg }
152 1.1 mrg
153 1.1 mrg int
154 1.1 mrg audiodev_refresh(void)
155 1.1 mrg {
156 1.1 mrg struct audiodev *adev;
157 1.1 mrg int fd, error;
158 1.1 mrg
159 1.2 jmcneill fd = open(DRVCTLDEV, O_RDONLY);
160 1.1 mrg if (fd == -1) {
161 1.12 isaki warn("open %s", DRVCTLDEV);
162 1.1 mrg return -1;
163 1.1 mrg }
164 1.1 mrg
165 1.1 mrg while (!TAILQ_EMPTY(&audiodevlist)) {
166 1.1 mrg adev = TAILQ_FIRST(&audiodevlist);
167 1.8 isaki if (adev->ctlfd != -1)
168 1.8 isaki close(adev->ctlfd);
169 1.1 mrg TAILQ_REMOVE(&audiodevlist, adev, next);
170 1.1 mrg free(adev);
171 1.1 mrg }
172 1.1 mrg
173 1.1 mrg error = drvctl_foreach(fd, "audio", audiodev_cb, NULL);
174 1.1 mrg if (error == -1) {
175 1.12 isaki warnx("drvctl failed");
176 1.1 mrg return -1;
177 1.1 mrg }
178 1.1 mrg
179 1.1 mrg close(fd);
180 1.1 mrg
181 1.1 mrg return 0;
182 1.1 mrg }
183 1.1 mrg
184 1.1 mrg unsigned int
185 1.10 isaki audiodev_maxunit(void)
186 1.1 mrg {
187 1.10 isaki return maxunit;
188 1.1 mrg }
189 1.1 mrg
190 1.10 isaki /*
191 1.10 isaki * Get audiodev corresponding to audio<i> device.
192 1.10 isaki */
193 1.1 mrg struct audiodev *
194 1.1 mrg audiodev_get(unsigned int i)
195 1.1 mrg {
196 1.1 mrg struct audiodev *adev;
197 1.1 mrg
198 1.1 mrg TAILQ_FOREACH(adev, &audiodevlist, next) {
199 1.10 isaki if (i == adev->unit)
200 1.1 mrg return adev;
201 1.1 mrg }
202 1.1 mrg
203 1.1 mrg return NULL;
204 1.1 mrg }
205 1.1 mrg
206 1.1 mrg int
207 1.1 mrg audiodev_set_default(struct audiodev *adev)
208 1.1 mrg {
209 1.1 mrg char audiopath[PATH_MAX+1];
210 1.1 mrg char soundpath[PATH_MAX+1];
211 1.1 mrg char audioctlpath[PATH_MAX+1];
212 1.1 mrg char mixerpath[PATH_MAX+1];
213 1.1 mrg
214 1.7 isaki snprintf(audiopath, sizeof(audiopath),
215 1.1 mrg _PATH_AUDIO "%u", adev->unit);
216 1.7 isaki snprintf(soundpath, sizeof(soundpath),
217 1.1 mrg _PATH_SOUND "%u", adev->unit);
218 1.7 isaki snprintf(audioctlpath, sizeof(audioctlpath),
219 1.1 mrg _PATH_AUDIOCTL "%u", adev->unit);
220 1.7 isaki snprintf(mixerpath, sizeof(mixerpath),
221 1.1 mrg _PATH_MIXER "%u", adev->unit);
222 1.1 mrg
223 1.1 mrg unlink(_PATH_AUDIO);
224 1.1 mrg unlink(_PATH_SOUND);
225 1.1 mrg unlink(_PATH_AUDIOCTL);
226 1.1 mrg unlink(_PATH_MIXER);
227 1.1 mrg
228 1.1 mrg if (symlink(audiopath, _PATH_AUDIO) == -1) {
229 1.12 isaki warn("symlink %s", _PATH_AUDIO);
230 1.1 mrg return -1;
231 1.1 mrg }
232 1.1 mrg if (symlink(soundpath, _PATH_SOUND) == -1) {
233 1.12 isaki warn("symlink %s", _PATH_SOUND);
234 1.1 mrg return -1;
235 1.1 mrg }
236 1.1 mrg if (symlink(audioctlpath, _PATH_AUDIOCTL) == -1) {
237 1.12 isaki warn("symlink %s", _PATH_AUDIOCTL);
238 1.1 mrg return -1;
239 1.1 mrg }
240 1.1 mrg if (symlink(mixerpath, _PATH_MIXER) == -1) {
241 1.12 isaki warn("symlink %s", _PATH_MIXER);
242 1.1 mrg return -1;
243 1.1 mrg }
244 1.1 mrg
245 1.1 mrg return 0;
246 1.1 mrg }
247 1.2 jmcneill
248 1.2 jmcneill int
249 1.7 isaki audiodev_set_param(struct audiodev *adev, int mode,
250 1.7 isaki const char *encname, unsigned int prec, unsigned int ch, unsigned int freq)
251 1.7 isaki {
252 1.8 isaki audio_info_t ai;
253 1.7 isaki int setmode;
254 1.7 isaki u_int enc;
255 1.7 isaki
256 1.7 isaki setmode = 0;
257 1.8 isaki ai = adev->hwinfo;
258 1.7 isaki
259 1.7 isaki for (enc = 0; enc < encoding_max; enc++) {
260 1.7 isaki if (strcmp(encname, encoding_names[enc]) == 0)
261 1.7 isaki break;
262 1.7 isaki }
263 1.7 isaki if (enc >= encoding_max) {
264 1.12 isaki warnx("unknown encoding name: %s", encname);
265 1.7 isaki return -1;
266 1.7 isaki }
267 1.7 isaki
268 1.7 isaki if ((ai.mode & mode & AUMODE_PLAY)) {
269 1.7 isaki setmode |= AUMODE_PLAY;
270 1.7 isaki ai.play.encoding = enc;
271 1.7 isaki ai.play.precision = prec;
272 1.7 isaki ai.play.channels = ch;
273 1.7 isaki ai.play.sample_rate = freq;
274 1.7 isaki }
275 1.7 isaki if ((ai.mode & mode & AUMODE_RECORD)) {
276 1.7 isaki setmode |= AUMODE_RECORD;
277 1.7 isaki ai.record.encoding = enc;
278 1.7 isaki ai.record.precision = prec;
279 1.7 isaki ai.record.channels = ch;
280 1.7 isaki ai.record.sample_rate = freq;
281 1.7 isaki }
282 1.7 isaki
283 1.7 isaki ai.mode = setmode;
284 1.7 isaki printf("setting %s to %s:%u, %uch, %uHz\n",
285 1.7 isaki adev->xname, encname, prec, ch, freq);
286 1.8 isaki if (ioctl(adev->ctlfd, AUDIO_SETFORMAT, &ai) == -1) {
287 1.12 isaki warn("ioctl AUDIO_SETFORMAT");
288 1.7 isaki return -1;
289 1.7 isaki }
290 1.7 isaki return 0;
291 1.7 isaki }
292 1.7 isaki
293 1.7 isaki int
294 1.9 isaki audiodev_test(struct audiodev *adev)
295 1.2 jmcneill {
296 1.2 jmcneill audio_info_t info;
297 1.9 isaki unsigned int i;
298 1.9 isaki int rv;
299 1.9 isaki
300 1.9 isaki rv = -1;
301 1.7 isaki
302 1.9 isaki adev->fd = open(adev->path, O_WRONLY);
303 1.9 isaki if (adev->fd == -1) {
304 1.12 isaki warn("open %s", adev->path);
305 1.7 isaki return -1;
306 1.7 isaki }
307 1.2 jmcneill
308 1.2 jmcneill AUDIO_INITINFO(&info);
309 1.14 isaki info.play.sample_rate = adev->hwinfo.play.sample_rate;
310 1.8 isaki info.play.channels = adev->hwinfo.play.channels;
311 1.2 jmcneill info.play.precision = 16;
312 1.2 jmcneill info.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
313 1.2 jmcneill info.mode = AUMODE_PLAY;
314 1.9 isaki if (ioctl(adev->fd, AUDIO_SETINFO, &info) == -1) {
315 1.12 isaki warn("ioctl AUDIO_SETINFO");
316 1.9 isaki goto done;
317 1.2 jmcneill }
318 1.13 isaki if (ioctl(adev->fd, AUDIO_GETBUFINFO, &info) == -1) {
319 1.13 isaki warn("ioctl AUDIO_GETBUFINFO");
320 1.9 isaki goto done;
321 1.9 isaki }
322 1.9 isaki
323 1.9 isaki for (i = 0; i < adev->hwinfo.play.channels; i++) {
324 1.9 isaki printf(" testing channel %u...", i);
325 1.9 isaki fflush(stdout);
326 1.9 isaki if (audiodev_test_chmask(adev, 1 << i, &info) == -1)
327 1.9 isaki goto done;
328 1.9 isaki printf(" done\n");
329 1.2 jmcneill }
330 1.2 jmcneill
331 1.9 isaki rv = 0;
332 1.9 isaki done:
333 1.9 isaki close(adev->fd);
334 1.9 isaki return rv;
335 1.9 isaki }
336 1.9 isaki
337 1.9 isaki static int
338 1.9 isaki audiodev_test_chmask(struct audiodev *adev, unsigned int chanmask,
339 1.9 isaki audio_info_t *info)
340 1.9 isaki {
341 1.9 isaki int16_t *buf;
342 1.9 isaki size_t buflen;
343 1.9 isaki off_t off;
344 1.9 isaki int rv;
345 1.9 isaki
346 1.9 isaki rv = -1;
347 1.9 isaki
348 1.14 isaki dtmf_new(&buf, &buflen, adev->hwinfo.play.sample_rate, 2,
349 1.8 isaki adev->hwinfo.play.channels, chanmask, 350.0, 440.0);
350 1.7 isaki if (buf == NULL) {
351 1.9 isaki return -1;
352 1.7 isaki }
353 1.2 jmcneill
354 1.2 jmcneill off = 0;
355 1.2 jmcneill while (buflen > 0) {
356 1.5 dholland size_t wlen;
357 1.5 dholland ssize_t ret;
358 1.5 dholland
359 1.9 isaki wlen = info->play.buffer_size;
360 1.2 jmcneill if (wlen > buflen)
361 1.2 jmcneill wlen = buflen;
362 1.9 isaki ret = write(adev->fd, (char *)buf + off, wlen);
363 1.5 dholland if (ret == -1) {
364 1.12 isaki warn("write");
365 1.4 jmcneill goto done;
366 1.4 jmcneill }
367 1.5 dholland wlen = ret;
368 1.2 jmcneill off += wlen;
369 1.2 jmcneill buflen -= wlen;
370 1.2 jmcneill }
371 1.2 jmcneill
372 1.9 isaki if (ioctl(adev->fd, AUDIO_DRAIN) == -1) {
373 1.12 isaki warn("ioctl AUDIO_DRAIN");
374 1.7 isaki goto done;
375 1.7 isaki }
376 1.2 jmcneill
377 1.7 isaki rv = 0;
378 1.4 jmcneill done:
379 1.2 jmcneill free(buf);
380 1.4 jmcneill return rv;
381 1.2 jmcneill }
382