audiodev.c revision 1.13 1 1.13 isaki /* $NetBSD: audiodev.c,v 1.13 2019/08/24 06:16:27 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.2 jmcneill #define AUDIODEV_SAMPLE_RATE 44100
55 1.2 jmcneill
56 1.1 mrg static int
57 1.1 mrg audiodev_getinfo(struct audiodev *adev)
58 1.1 mrg {
59 1.1 mrg struct stat st;
60 1.7 isaki struct audiofmt *f;
61 1.7 isaki audio_format_query_t query;
62 1.7 isaki int i;
63 1.1 mrg
64 1.7 isaki if (stat(adev->ctlpath, &st) == -1)
65 1.1 mrg return -1;
66 1.1 mrg adev->dev = st.st_rdev;
67 1.1 mrg
68 1.7 isaki if (stat(_PATH_AUDIOCTL, &st) != -1 && st.st_rdev == adev->dev)
69 1.1 mrg adev->defaultdev = true;
70 1.1 mrg
71 1.8 isaki adev->ctlfd = open(adev->ctlpath, O_RDONLY);
72 1.8 isaki if (adev->ctlfd == -1) {
73 1.6 mrg return -1;
74 1.6 mrg }
75 1.8 isaki if (ioctl(adev->ctlfd, AUDIO_GETDEV, &adev->audio_device) == -1) {
76 1.8 isaki close(adev->ctlfd);
77 1.1 mrg return -1;
78 1.1 mrg }
79 1.1 mrg
80 1.7 isaki for (i = 0; ;i++) {
81 1.7 isaki memset(&query, 0, sizeof(query));
82 1.7 isaki query.index = i;
83 1.8 isaki if (ioctl(adev->ctlfd, AUDIO_QUERYFORMAT, &query) == -1) {
84 1.7 isaki if (errno == ENODEV) {
85 1.7 isaki /* QUERYFORMAT not supported. */
86 1.7 isaki break;
87 1.7 isaki }
88 1.7 isaki if (errno == EINVAL)
89 1.7 isaki break;
90 1.8 isaki close(adev->ctlfd);
91 1.7 isaki return -1;
92 1.7 isaki }
93 1.7 isaki
94 1.7 isaki f = calloc(1, sizeof(*f));
95 1.7 isaki f->fmt = query.fmt;
96 1.7 isaki TAILQ_INSERT_TAIL(&adev->formats, f, next);
97 1.7 isaki }
98 1.7 isaki
99 1.8 isaki if (ioctl(adev->ctlfd, AUDIO_GETFORMAT, &adev->hwinfo) == -1) {
100 1.8 isaki close(adev->ctlfd);
101 1.7 isaki return -1;
102 1.7 isaki }
103 1.2 jmcneill
104 1.1 mrg return 0;
105 1.1 mrg }
106 1.1 mrg
107 1.1 mrg static int
108 1.3 jmcneill audiodev_add(const char *pdev, const char *dev, unsigned int unit)
109 1.1 mrg {
110 1.1 mrg struct audiodev *adev;
111 1.1 mrg
112 1.1 mrg adev = calloc(1, sizeof(*adev));
113 1.1 mrg if (adev == NULL)
114 1.1 mrg return -1;
115 1.1 mrg
116 1.3 jmcneill strlcpy(adev->pxname, pdev, sizeof(adev->pxname));
117 1.1 mrg strlcpy(adev->xname, dev, sizeof(adev->xname));
118 1.7 isaki snprintf(adev->path, sizeof(adev->path), "/dev/%s", dev);
119 1.7 isaki snprintf(adev->ctlpath, sizeof(adev->ctlpath), "/dev/audioctl%d", unit);
120 1.1 mrg adev->unit = unit;
121 1.7 isaki TAILQ_INIT(&adev->formats);
122 1.1 mrg
123 1.1 mrg if (audiodev_getinfo(adev) == -1) {
124 1.1 mrg free(adev);
125 1.1 mrg return -1;
126 1.1 mrg }
127 1.1 mrg
128 1.1 mrg #ifdef DEBUG
129 1.7 isaki printf("DEBUG: [%c] %s(%s): %s\n", adev->defaultdev ? '*' : ' ',
130 1.7 isaki adev->path, adev->ctlpath, adev->audio_device.name);
131 1.7 isaki struct audiofmt *f;
132 1.7 isaki TAILQ_FOREACH(f, &adev->formats, next) {
133 1.7 isaki printf("DEBUG: enc%d, %d/%d, %dch\n",
134 1.7 isaki f->fmt.encoding,
135 1.7 isaki f->fmt.validbits,
136 1.7 isaki f->fmt.precision,
137 1.7 isaki f->fmt.channels);
138 1.7 isaki }
139 1.1 mrg #endif
140 1.1 mrg
141 1.1 mrg TAILQ_INSERT_TAIL(&audiodevlist, adev, next);
142 1.1 mrg
143 1.10 isaki if (unit > maxunit)
144 1.10 isaki maxunit = unit;
145 1.10 isaki
146 1.1 mrg return 0;
147 1.1 mrg }
148 1.1 mrg
149 1.1 mrg static void
150 1.3 jmcneill audiodev_cb(void *args, const char *pdev, const char *dev, unsigned int unit)
151 1.1 mrg {
152 1.3 jmcneill audiodev_add(pdev, dev, unit);
153 1.1 mrg }
154 1.1 mrg
155 1.1 mrg int
156 1.1 mrg audiodev_refresh(void)
157 1.1 mrg {
158 1.1 mrg struct audiodev *adev;
159 1.1 mrg int fd, error;
160 1.1 mrg
161 1.2 jmcneill fd = open(DRVCTLDEV, O_RDONLY);
162 1.1 mrg if (fd == -1) {
163 1.12 isaki warn("open %s", DRVCTLDEV);
164 1.1 mrg return -1;
165 1.1 mrg }
166 1.1 mrg
167 1.1 mrg while (!TAILQ_EMPTY(&audiodevlist)) {
168 1.1 mrg adev = TAILQ_FIRST(&audiodevlist);
169 1.8 isaki if (adev->ctlfd != -1)
170 1.8 isaki close(adev->ctlfd);
171 1.1 mrg TAILQ_REMOVE(&audiodevlist, adev, next);
172 1.1 mrg free(adev);
173 1.1 mrg }
174 1.1 mrg
175 1.1 mrg error = drvctl_foreach(fd, "audio", audiodev_cb, NULL);
176 1.1 mrg if (error == -1) {
177 1.12 isaki warnx("drvctl failed");
178 1.1 mrg return -1;
179 1.1 mrg }
180 1.1 mrg
181 1.1 mrg close(fd);
182 1.1 mrg
183 1.1 mrg return 0;
184 1.1 mrg }
185 1.1 mrg
186 1.1 mrg unsigned int
187 1.10 isaki audiodev_maxunit(void)
188 1.1 mrg {
189 1.10 isaki return maxunit;
190 1.1 mrg }
191 1.1 mrg
192 1.10 isaki /*
193 1.10 isaki * Get audiodev corresponding to audio<i> device.
194 1.10 isaki */
195 1.1 mrg struct audiodev *
196 1.1 mrg audiodev_get(unsigned int i)
197 1.1 mrg {
198 1.1 mrg struct audiodev *adev;
199 1.1 mrg
200 1.1 mrg TAILQ_FOREACH(adev, &audiodevlist, next) {
201 1.10 isaki if (i == adev->unit)
202 1.1 mrg return adev;
203 1.1 mrg }
204 1.1 mrg
205 1.1 mrg return NULL;
206 1.1 mrg }
207 1.1 mrg
208 1.1 mrg int
209 1.1 mrg audiodev_set_default(struct audiodev *adev)
210 1.1 mrg {
211 1.1 mrg char audiopath[PATH_MAX+1];
212 1.1 mrg char soundpath[PATH_MAX+1];
213 1.1 mrg char audioctlpath[PATH_MAX+1];
214 1.1 mrg char mixerpath[PATH_MAX+1];
215 1.1 mrg
216 1.7 isaki snprintf(audiopath, sizeof(audiopath),
217 1.1 mrg _PATH_AUDIO "%u", adev->unit);
218 1.7 isaki snprintf(soundpath, sizeof(soundpath),
219 1.1 mrg _PATH_SOUND "%u", adev->unit);
220 1.7 isaki snprintf(audioctlpath, sizeof(audioctlpath),
221 1.1 mrg _PATH_AUDIOCTL "%u", adev->unit);
222 1.7 isaki snprintf(mixerpath, sizeof(mixerpath),
223 1.1 mrg _PATH_MIXER "%u", adev->unit);
224 1.1 mrg
225 1.1 mrg unlink(_PATH_AUDIO);
226 1.1 mrg unlink(_PATH_SOUND);
227 1.1 mrg unlink(_PATH_AUDIOCTL);
228 1.1 mrg unlink(_PATH_MIXER);
229 1.1 mrg
230 1.1 mrg if (symlink(audiopath, _PATH_AUDIO) == -1) {
231 1.12 isaki warn("symlink %s", _PATH_AUDIO);
232 1.1 mrg return -1;
233 1.1 mrg }
234 1.1 mrg if (symlink(soundpath, _PATH_SOUND) == -1) {
235 1.12 isaki warn("symlink %s", _PATH_SOUND);
236 1.1 mrg return -1;
237 1.1 mrg }
238 1.1 mrg if (symlink(audioctlpath, _PATH_AUDIOCTL) == -1) {
239 1.12 isaki warn("symlink %s", _PATH_AUDIOCTL);
240 1.1 mrg return -1;
241 1.1 mrg }
242 1.1 mrg if (symlink(mixerpath, _PATH_MIXER) == -1) {
243 1.12 isaki warn("symlink %s", _PATH_MIXER);
244 1.1 mrg return -1;
245 1.1 mrg }
246 1.1 mrg
247 1.1 mrg return 0;
248 1.1 mrg }
249 1.2 jmcneill
250 1.2 jmcneill int
251 1.7 isaki audiodev_set_param(struct audiodev *adev, int mode,
252 1.7 isaki const char *encname, unsigned int prec, unsigned int ch, unsigned int freq)
253 1.7 isaki {
254 1.8 isaki audio_info_t ai;
255 1.7 isaki int setmode;
256 1.7 isaki u_int enc;
257 1.7 isaki
258 1.7 isaki setmode = 0;
259 1.8 isaki ai = adev->hwinfo;
260 1.7 isaki
261 1.7 isaki for (enc = 0; enc < encoding_max; enc++) {
262 1.7 isaki if (strcmp(encname, encoding_names[enc]) == 0)
263 1.7 isaki break;
264 1.7 isaki }
265 1.7 isaki if (enc >= encoding_max) {
266 1.12 isaki warnx("unknown encoding name: %s", encname);
267 1.7 isaki return -1;
268 1.7 isaki }
269 1.7 isaki
270 1.7 isaki if ((ai.mode & mode & AUMODE_PLAY)) {
271 1.7 isaki setmode |= AUMODE_PLAY;
272 1.7 isaki ai.play.encoding = enc;
273 1.7 isaki ai.play.precision = prec;
274 1.7 isaki ai.play.channels = ch;
275 1.7 isaki ai.play.sample_rate = freq;
276 1.7 isaki }
277 1.7 isaki if ((ai.mode & mode & AUMODE_RECORD)) {
278 1.7 isaki setmode |= AUMODE_RECORD;
279 1.7 isaki ai.record.encoding = enc;
280 1.7 isaki ai.record.precision = prec;
281 1.7 isaki ai.record.channels = ch;
282 1.7 isaki ai.record.sample_rate = freq;
283 1.7 isaki }
284 1.7 isaki
285 1.7 isaki ai.mode = setmode;
286 1.7 isaki printf("setting %s to %s:%u, %uch, %uHz\n",
287 1.7 isaki adev->xname, encname, prec, ch, freq);
288 1.8 isaki if (ioctl(adev->ctlfd, AUDIO_SETFORMAT, &ai) == -1) {
289 1.12 isaki warn("ioctl AUDIO_SETFORMAT");
290 1.7 isaki return -1;
291 1.7 isaki }
292 1.7 isaki return 0;
293 1.7 isaki }
294 1.7 isaki
295 1.7 isaki int
296 1.9 isaki audiodev_test(struct audiodev *adev)
297 1.2 jmcneill {
298 1.2 jmcneill audio_info_t info;
299 1.9 isaki unsigned int i;
300 1.9 isaki int rv;
301 1.9 isaki
302 1.9 isaki rv = -1;
303 1.7 isaki
304 1.9 isaki adev->fd = open(adev->path, O_WRONLY);
305 1.9 isaki if (adev->fd == -1) {
306 1.12 isaki warn("open %s", adev->path);
307 1.7 isaki return -1;
308 1.7 isaki }
309 1.2 jmcneill
310 1.2 jmcneill AUDIO_INITINFO(&info);
311 1.2 jmcneill info.play.sample_rate = AUDIODEV_SAMPLE_RATE;
312 1.8 isaki info.play.channels = adev->hwinfo.play.channels;
313 1.2 jmcneill info.play.precision = 16;
314 1.2 jmcneill info.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
315 1.2 jmcneill info.mode = AUMODE_PLAY;
316 1.9 isaki if (ioctl(adev->fd, AUDIO_SETINFO, &info) == -1) {
317 1.12 isaki warn("ioctl AUDIO_SETINFO");
318 1.9 isaki goto done;
319 1.2 jmcneill }
320 1.13 isaki if (ioctl(adev->fd, AUDIO_GETBUFINFO, &info) == -1) {
321 1.13 isaki warn("ioctl AUDIO_GETBUFINFO");
322 1.9 isaki goto done;
323 1.9 isaki }
324 1.9 isaki
325 1.9 isaki for (i = 0; i < adev->hwinfo.play.channels; i++) {
326 1.9 isaki printf(" testing channel %u...", i);
327 1.9 isaki fflush(stdout);
328 1.9 isaki if (audiodev_test_chmask(adev, 1 << i, &info) == -1)
329 1.9 isaki goto done;
330 1.9 isaki printf(" done\n");
331 1.2 jmcneill }
332 1.2 jmcneill
333 1.9 isaki rv = 0;
334 1.9 isaki done:
335 1.9 isaki close(adev->fd);
336 1.9 isaki return rv;
337 1.9 isaki }
338 1.9 isaki
339 1.9 isaki static int
340 1.9 isaki audiodev_test_chmask(struct audiodev *adev, unsigned int chanmask,
341 1.9 isaki audio_info_t *info)
342 1.9 isaki {
343 1.9 isaki int16_t *buf;
344 1.9 isaki size_t buflen;
345 1.9 isaki off_t off;
346 1.9 isaki int rv;
347 1.9 isaki
348 1.9 isaki rv = -1;
349 1.9 isaki
350 1.9 isaki dtmf_new(&buf, &buflen, info->play.sample_rate, 2,
351 1.8 isaki adev->hwinfo.play.channels, chanmask, 350.0, 440.0);
352 1.7 isaki if (buf == NULL) {
353 1.9 isaki return -1;
354 1.7 isaki }
355 1.2 jmcneill
356 1.2 jmcneill off = 0;
357 1.2 jmcneill while (buflen > 0) {
358 1.5 dholland size_t wlen;
359 1.5 dholland ssize_t ret;
360 1.5 dholland
361 1.9 isaki wlen = info->play.buffer_size;
362 1.2 jmcneill if (wlen > buflen)
363 1.2 jmcneill wlen = buflen;
364 1.9 isaki ret = write(adev->fd, (char *)buf + off, wlen);
365 1.5 dholland if (ret == -1) {
366 1.12 isaki warn("write");
367 1.4 jmcneill goto done;
368 1.4 jmcneill }
369 1.5 dholland wlen = ret;
370 1.2 jmcneill off += wlen;
371 1.2 jmcneill buflen -= wlen;
372 1.2 jmcneill }
373 1.2 jmcneill
374 1.9 isaki if (ioctl(adev->fd, AUDIO_DRAIN) == -1) {
375 1.12 isaki warn("ioctl AUDIO_DRAIN");
376 1.7 isaki goto done;
377 1.7 isaki }
378 1.2 jmcneill
379 1.7 isaki rv = 0;
380 1.4 jmcneill done:
381 1.2 jmcneill free(buf);
382 1.4 jmcneill return rv;
383 1.2 jmcneill }
384