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