audiodev.c revision 1.9 1 1.9 isaki /* $NetBSD: audiodev.c,v 1.9 2019/08/24 04:04:10 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.7 isaki #include <errno.h>
35 1.1 mrg #include <fcntl.h>
36 1.1 mrg #include <paths.h>
37 1.1 mrg #include <stdio.h>
38 1.1 mrg #include <stdlib.h>
39 1.1 mrg #include <string.h>
40 1.1 mrg #include <unistd.h>
41 1.1 mrg
42 1.1 mrg #include "audiodev.h"
43 1.1 mrg #include "drvctl.h"
44 1.2 jmcneill #include "dtmf.h"
45 1.1 mrg
46 1.9 isaki static int audiodev_test_chmask(struct audiodev *, unsigned int,
47 1.9 isaki audio_info_t *);
48 1.9 isaki
49 1.1 mrg static TAILQ_HEAD(audiodevhead, audiodev) audiodevlist =
50 1.1 mrg TAILQ_HEAD_INITIALIZER(audiodevlist);
51 1.1 mrg
52 1.2 jmcneill #define AUDIODEV_SAMPLE_RATE 44100
53 1.2 jmcneill
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.1 mrg return 0;
142 1.1 mrg }
143 1.1 mrg
144 1.1 mrg static void
145 1.3 jmcneill audiodev_cb(void *args, const char *pdev, const char *dev, unsigned int unit)
146 1.1 mrg {
147 1.3 jmcneill audiodev_add(pdev, dev, unit);
148 1.1 mrg }
149 1.1 mrg
150 1.1 mrg int
151 1.1 mrg audiodev_refresh(void)
152 1.1 mrg {
153 1.1 mrg struct audiodev *adev;
154 1.1 mrg int fd, error;
155 1.1 mrg
156 1.2 jmcneill fd = open(DRVCTLDEV, O_RDONLY);
157 1.1 mrg if (fd == -1) {
158 1.1 mrg perror("open " DRVCTLDEV);
159 1.1 mrg return -1;
160 1.1 mrg }
161 1.1 mrg
162 1.1 mrg while (!TAILQ_EMPTY(&audiodevlist)) {
163 1.1 mrg adev = TAILQ_FIRST(&audiodevlist);
164 1.8 isaki if (adev->ctlfd != -1)
165 1.8 isaki close(adev->ctlfd);
166 1.1 mrg TAILQ_REMOVE(&audiodevlist, adev, next);
167 1.1 mrg free(adev);
168 1.1 mrg }
169 1.1 mrg
170 1.1 mrg error = drvctl_foreach(fd, "audio", audiodev_cb, NULL);
171 1.1 mrg if (error == -1) {
172 1.1 mrg perror("drvctl");
173 1.1 mrg return -1;
174 1.1 mrg }
175 1.1 mrg
176 1.1 mrg close(fd);
177 1.1 mrg
178 1.1 mrg return 0;
179 1.1 mrg }
180 1.1 mrg
181 1.1 mrg unsigned int
182 1.1 mrg audiodev_count(void)
183 1.1 mrg {
184 1.1 mrg struct audiodev *adev;
185 1.1 mrg unsigned int n;
186 1.1 mrg
187 1.1 mrg n = 0;
188 1.1 mrg TAILQ_FOREACH(adev, &audiodevlist, next)
189 1.1 mrg ++n;
190 1.1 mrg
191 1.1 mrg return n;
192 1.1 mrg }
193 1.1 mrg
194 1.1 mrg struct audiodev *
195 1.1 mrg audiodev_get(unsigned int i)
196 1.1 mrg {
197 1.1 mrg struct audiodev *adev;
198 1.1 mrg unsigned int n;
199 1.1 mrg
200 1.1 mrg n = 0;
201 1.1 mrg TAILQ_FOREACH(adev, &audiodevlist, next) {
202 1.1 mrg if (n == i)
203 1.1 mrg return adev;
204 1.1 mrg ++n;
205 1.1 mrg }
206 1.1 mrg
207 1.1 mrg return NULL;
208 1.1 mrg }
209 1.1 mrg
210 1.1 mrg int
211 1.1 mrg audiodev_set_default(struct audiodev *adev)
212 1.1 mrg {
213 1.1 mrg char audiopath[PATH_MAX+1];
214 1.1 mrg char soundpath[PATH_MAX+1];
215 1.1 mrg char audioctlpath[PATH_MAX+1];
216 1.1 mrg char mixerpath[PATH_MAX+1];
217 1.1 mrg
218 1.7 isaki snprintf(audiopath, sizeof(audiopath),
219 1.1 mrg _PATH_AUDIO "%u", adev->unit);
220 1.7 isaki snprintf(soundpath, sizeof(soundpath),
221 1.1 mrg _PATH_SOUND "%u", adev->unit);
222 1.7 isaki snprintf(audioctlpath, sizeof(audioctlpath),
223 1.1 mrg _PATH_AUDIOCTL "%u", adev->unit);
224 1.7 isaki snprintf(mixerpath, sizeof(mixerpath),
225 1.1 mrg _PATH_MIXER "%u", adev->unit);
226 1.1 mrg
227 1.1 mrg unlink(_PATH_AUDIO);
228 1.1 mrg unlink(_PATH_SOUND);
229 1.1 mrg unlink(_PATH_AUDIOCTL);
230 1.1 mrg unlink(_PATH_MIXER);
231 1.1 mrg
232 1.1 mrg if (symlink(audiopath, _PATH_AUDIO) == -1) {
233 1.1 mrg perror("symlink " _PATH_AUDIO);
234 1.1 mrg return -1;
235 1.1 mrg }
236 1.1 mrg if (symlink(soundpath, _PATH_SOUND) == -1) {
237 1.1 mrg perror("symlink " _PATH_SOUND);
238 1.1 mrg return -1;
239 1.1 mrg }
240 1.1 mrg if (symlink(audioctlpath, _PATH_AUDIOCTL) == -1) {
241 1.1 mrg perror("symlink " _PATH_AUDIOCTL);
242 1.1 mrg return -1;
243 1.1 mrg }
244 1.1 mrg if (symlink(mixerpath, _PATH_MIXER) == -1) {
245 1.1 mrg perror("symlink " _PATH_MIXER);
246 1.1 mrg return -1;
247 1.1 mrg }
248 1.1 mrg
249 1.1 mrg return 0;
250 1.1 mrg }
251 1.2 jmcneill
252 1.2 jmcneill int
253 1.7 isaki audiodev_set_param(struct audiodev *adev, int mode,
254 1.7 isaki const char *encname, unsigned int prec, unsigned int ch, unsigned int freq)
255 1.7 isaki {
256 1.8 isaki audio_info_t ai;
257 1.7 isaki int setmode;
258 1.7 isaki u_int enc;
259 1.7 isaki
260 1.7 isaki setmode = 0;
261 1.8 isaki ai = adev->hwinfo;
262 1.7 isaki
263 1.7 isaki for (enc = 0; enc < encoding_max; enc++) {
264 1.7 isaki if (strcmp(encname, encoding_names[enc]) == 0)
265 1.7 isaki break;
266 1.7 isaki }
267 1.7 isaki if (enc >= encoding_max) {
268 1.7 isaki fprintf(stderr, "unknown encoding name: %s\n", encname);
269 1.7 isaki errno = EINVAL;
270 1.7 isaki return -1;
271 1.7 isaki }
272 1.7 isaki
273 1.7 isaki if ((ai.mode & mode & AUMODE_PLAY)) {
274 1.7 isaki setmode |= AUMODE_PLAY;
275 1.7 isaki ai.play.encoding = enc;
276 1.7 isaki ai.play.precision = prec;
277 1.7 isaki ai.play.channels = ch;
278 1.7 isaki ai.play.sample_rate = freq;
279 1.7 isaki }
280 1.7 isaki if ((ai.mode & mode & AUMODE_RECORD)) {
281 1.7 isaki setmode |= AUMODE_RECORD;
282 1.7 isaki ai.record.encoding = enc;
283 1.7 isaki ai.record.precision = prec;
284 1.7 isaki ai.record.channels = ch;
285 1.7 isaki ai.record.sample_rate = freq;
286 1.7 isaki }
287 1.7 isaki
288 1.7 isaki if (setmode == 0) {
289 1.7 isaki errno = EINVAL;
290 1.7 isaki return -1;
291 1.7 isaki }
292 1.7 isaki
293 1.7 isaki ai.mode = setmode;
294 1.7 isaki printf("setting %s to %s:%u, %uch, %uHz\n",
295 1.7 isaki adev->xname, encname, prec, ch, freq);
296 1.8 isaki if (ioctl(adev->ctlfd, AUDIO_SETFORMAT, &ai) == -1) {
297 1.7 isaki perror("ioctl AUDIO_SETFORMAT");
298 1.7 isaki return -1;
299 1.7 isaki }
300 1.7 isaki return 0;
301 1.7 isaki }
302 1.7 isaki
303 1.7 isaki int
304 1.9 isaki audiodev_test(struct audiodev *adev)
305 1.2 jmcneill {
306 1.2 jmcneill audio_info_t info;
307 1.9 isaki unsigned int i;
308 1.9 isaki int rv;
309 1.9 isaki
310 1.9 isaki rv = -1;
311 1.7 isaki
312 1.9 isaki adev->fd = open(adev->path, O_WRONLY);
313 1.9 isaki if (adev->fd == -1) {
314 1.7 isaki perror("open");
315 1.7 isaki return -1;
316 1.7 isaki }
317 1.2 jmcneill
318 1.2 jmcneill AUDIO_INITINFO(&info);
319 1.2 jmcneill info.play.sample_rate = AUDIODEV_SAMPLE_RATE;
320 1.8 isaki info.play.channels = adev->hwinfo.play.channels;
321 1.2 jmcneill info.play.precision = 16;
322 1.2 jmcneill info.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
323 1.2 jmcneill info.mode = AUMODE_PLAY;
324 1.9 isaki if (ioctl(adev->fd, AUDIO_SETINFO, &info) == -1) {
325 1.2 jmcneill perror("ioctl AUDIO_SETINFO");
326 1.9 isaki goto done;
327 1.2 jmcneill }
328 1.9 isaki if (ioctl(adev->fd, AUDIO_GETINFO, &info) == -1) {
329 1.2 jmcneill perror("ioctl AUDIO_GETINFO");
330 1.9 isaki goto done;
331 1.9 isaki }
332 1.9 isaki
333 1.9 isaki for (i = 0; i < adev->hwinfo.play.channels; i++) {
334 1.9 isaki printf(" testing channel %u...", i);
335 1.9 isaki fflush(stdout);
336 1.9 isaki if (audiodev_test_chmask(adev, 1 << i, &info) == -1)
337 1.9 isaki goto done;
338 1.9 isaki printf(" done\n");
339 1.2 jmcneill }
340 1.2 jmcneill
341 1.9 isaki rv = 0;
342 1.9 isaki done:
343 1.9 isaki close(adev->fd);
344 1.9 isaki return rv;
345 1.9 isaki }
346 1.9 isaki
347 1.9 isaki static int
348 1.9 isaki audiodev_test_chmask(struct audiodev *adev, unsigned int chanmask,
349 1.9 isaki audio_info_t *info)
350 1.9 isaki {
351 1.9 isaki int16_t *buf;
352 1.9 isaki size_t buflen;
353 1.9 isaki off_t off;
354 1.9 isaki int rv;
355 1.9 isaki
356 1.9 isaki rv = -1;
357 1.9 isaki
358 1.9 isaki dtmf_new(&buf, &buflen, info->play.sample_rate, 2,
359 1.8 isaki adev->hwinfo.play.channels, chanmask, 350.0, 440.0);
360 1.7 isaki if (buf == NULL) {
361 1.9 isaki return -1;
362 1.7 isaki }
363 1.2 jmcneill
364 1.2 jmcneill off = 0;
365 1.2 jmcneill while (buflen > 0) {
366 1.5 dholland size_t wlen;
367 1.5 dholland ssize_t ret;
368 1.5 dholland
369 1.9 isaki wlen = info->play.buffer_size;
370 1.2 jmcneill if (wlen > buflen)
371 1.2 jmcneill wlen = buflen;
372 1.9 isaki ret = write(adev->fd, (char *)buf + off, wlen);
373 1.5 dholland if (ret == -1) {
374 1.4 jmcneill perror("write");
375 1.4 jmcneill goto done;
376 1.4 jmcneill }
377 1.5 dholland wlen = ret;
378 1.2 jmcneill off += wlen;
379 1.2 jmcneill buflen -= wlen;
380 1.2 jmcneill }
381 1.2 jmcneill
382 1.9 isaki if (ioctl(adev->fd, AUDIO_DRAIN) == -1) {
383 1.2 jmcneill perror("ioctl AUDIO_DRAIN");
384 1.7 isaki goto done;
385 1.7 isaki }
386 1.2 jmcneill
387 1.7 isaki rv = 0;
388 1.4 jmcneill done:
389 1.2 jmcneill free(buf);
390 1.4 jmcneill return rv;
391 1.2 jmcneill }
392