Home | History | Annotate | Line # | Download | only in audiocfg
audiodev.c revision 1.6.16.1
      1  1.6.16.1  christos /* $NetBSD: audiodev.c,v 1.6.16.1 2019/06/10 22:10:17 christos 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.6.16.1  christos #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.1       mrg static TAILQ_HEAD(audiodevhead, audiodev) audiodevlist =
     47       1.1       mrg     TAILQ_HEAD_INITIALIZER(audiodevlist);
     48       1.1       mrg 
     49       1.2  jmcneill #define AUDIODEV_SAMPLE_RATE	44100
     50       1.2  jmcneill 
     51       1.1       mrg static int
     52       1.1       mrg audiodev_getinfo(struct audiodev *adev)
     53       1.1       mrg {
     54       1.1       mrg 	struct stat st;
     55  1.6.16.1  christos 	struct audiofmt *f;
     56  1.6.16.1  christos 	audio_format_query_t query;
     57  1.6.16.1  christos 	int i;
     58       1.1       mrg 
     59  1.6.16.1  christos 	if (stat(adev->ctlpath, &st) == -1)
     60       1.1       mrg 		return -1;
     61       1.1       mrg 	adev->dev = st.st_rdev;
     62       1.1       mrg 
     63  1.6.16.1  christos 	if (stat(_PATH_AUDIOCTL, &st) != -1 && st.st_rdev == adev->dev)
     64       1.1       mrg 		adev->defaultdev = true;
     65       1.1       mrg 
     66  1.6.16.1  christos 	adev->fd = open(adev->ctlpath, O_RDONLY);
     67       1.6       mrg 	if (adev->fd == -1) {
     68       1.6       mrg 			return -1;
     69       1.6       mrg 	}
     70       1.1       mrg 	if (ioctl(adev->fd, AUDIO_GETDEV, &adev->audio_device) == -1) {
     71       1.1       mrg 		close(adev->fd);
     72       1.1       mrg 		return -1;
     73       1.1       mrg 	}
     74       1.1       mrg 
     75  1.6.16.1  christos 	for (i = 0; ;i++) {
     76  1.6.16.1  christos 		memset(&query, 0, sizeof(query));
     77  1.6.16.1  christos 		query.index = i;
     78  1.6.16.1  christos 		if (ioctl(adev->fd, AUDIO_QUERYFORMAT, &query) == -1) {
     79  1.6.16.1  christos 			if (errno == ENODEV) {
     80  1.6.16.1  christos 				/* QUERYFORMAT not supported. */
     81  1.6.16.1  christos 				break;
     82  1.6.16.1  christos 			}
     83  1.6.16.1  christos 			if (errno == EINVAL)
     84  1.6.16.1  christos 				break;
     85  1.6.16.1  christos 			close(adev->fd);
     86  1.6.16.1  christos 			return -1;
     87  1.6.16.1  christos 		}
     88  1.6.16.1  christos 
     89  1.6.16.1  christos 		f = calloc(1, sizeof(*f));
     90  1.6.16.1  christos 		f->fmt = query.fmt;
     91  1.6.16.1  christos 		TAILQ_INSERT_TAIL(&adev->formats, f, next);
     92  1.6.16.1  christos 	}
     93  1.6.16.1  christos 
     94  1.6.16.1  christos 	if (ioctl(adev->fd, AUDIO_GETFORMAT, &adev->info) == -1) {
     95  1.6.16.1  christos 		close(adev->fd);
     96  1.6.16.1  christos 		return -1;
     97  1.6.16.1  christos 	}
     98       1.2  jmcneill 
     99       1.1       mrg 	return 0;
    100       1.1       mrg }
    101       1.1       mrg 
    102       1.1       mrg static int
    103       1.3  jmcneill audiodev_add(const char *pdev, const char *dev, unsigned int unit)
    104       1.1       mrg {
    105       1.1       mrg 	struct audiodev *adev;
    106       1.1       mrg 
    107       1.1       mrg 	adev = calloc(1, sizeof(*adev));
    108       1.1       mrg 	if (adev == NULL)
    109       1.1       mrg 		return -1;
    110       1.1       mrg 
    111       1.3  jmcneill 	strlcpy(adev->pxname, pdev, sizeof(adev->pxname));
    112       1.1       mrg 	strlcpy(adev->xname, dev, sizeof(adev->xname));
    113  1.6.16.1  christos 	snprintf(adev->path, sizeof(adev->path), "/dev/%s", dev);
    114  1.6.16.1  christos 	snprintf(adev->ctlpath, sizeof(adev->ctlpath), "/dev/audioctl%d", unit);
    115       1.1       mrg 	adev->unit = unit;
    116  1.6.16.1  christos 	TAILQ_INIT(&adev->formats);
    117       1.1       mrg 
    118       1.1       mrg 	if (audiodev_getinfo(adev) == -1) {
    119       1.1       mrg 		free(adev);
    120       1.1       mrg 		return -1;
    121       1.1       mrg 	}
    122       1.1       mrg 
    123       1.1       mrg #ifdef DEBUG
    124  1.6.16.1  christos 	printf("DEBUG: [%c] %s(%s): %s\n", adev->defaultdev ? '*' : ' ',
    125  1.6.16.1  christos 	    adev->path, adev->ctlpath, adev->audio_device.name);
    126  1.6.16.1  christos 	struct audiofmt *f;
    127  1.6.16.1  christos 	TAILQ_FOREACH(f, &adev->formats, next) {
    128  1.6.16.1  christos 		printf("DEBUG: enc%d, %d/%d, %dch\n",
    129  1.6.16.1  christos 		    f->fmt.encoding,
    130  1.6.16.1  christos 		    f->fmt.validbits,
    131  1.6.16.1  christos 		    f->fmt.precision,
    132  1.6.16.1  christos 		    f->fmt.channels);
    133  1.6.16.1  christos 	}
    134       1.1       mrg #endif
    135       1.1       mrg 
    136       1.1       mrg 	TAILQ_INSERT_TAIL(&audiodevlist, adev, next);
    137       1.1       mrg 
    138       1.1       mrg 	return 0;
    139       1.1       mrg }
    140       1.1       mrg 
    141       1.1       mrg static void
    142       1.3  jmcneill audiodev_cb(void *args, const char *pdev, const char *dev, unsigned int unit)
    143       1.1       mrg {
    144       1.3  jmcneill 	audiodev_add(pdev, dev, unit);
    145       1.1       mrg }
    146       1.1       mrg 
    147       1.1       mrg int
    148       1.1       mrg audiodev_refresh(void)
    149       1.1       mrg {
    150       1.1       mrg 	struct audiodev *adev;
    151       1.1       mrg 	int fd, error;
    152       1.1       mrg 
    153       1.2  jmcneill 	fd = open(DRVCTLDEV, O_RDONLY);
    154       1.1       mrg 	if (fd == -1) {
    155       1.1       mrg 		perror("open " DRVCTLDEV);
    156       1.1       mrg 		return -1;
    157       1.1       mrg 	}
    158       1.1       mrg 
    159       1.1       mrg 	while (!TAILQ_EMPTY(&audiodevlist)) {
    160       1.1       mrg 		adev = TAILQ_FIRST(&audiodevlist);
    161       1.1       mrg 		if (adev->fd != -1)
    162       1.1       mrg 			close(adev->fd);
    163       1.1       mrg 		TAILQ_REMOVE(&audiodevlist, adev, next);
    164       1.1       mrg 		free(adev);
    165       1.1       mrg 	}
    166       1.1       mrg 
    167       1.1       mrg 	error = drvctl_foreach(fd, "audio", audiodev_cb, NULL);
    168       1.1       mrg 	if (error == -1) {
    169       1.1       mrg 		perror("drvctl");
    170       1.1       mrg 		return -1;
    171       1.1       mrg 	}
    172       1.1       mrg 
    173       1.1       mrg 	close(fd);
    174       1.1       mrg 
    175       1.1       mrg 	return 0;
    176       1.1       mrg }
    177       1.1       mrg 
    178       1.1       mrg unsigned int
    179       1.1       mrg audiodev_count(void)
    180       1.1       mrg {
    181       1.1       mrg 	struct audiodev *adev;
    182       1.1       mrg 	unsigned int n;
    183       1.1       mrg 
    184       1.1       mrg 	n = 0;
    185       1.1       mrg 	TAILQ_FOREACH(adev, &audiodevlist, next)
    186       1.1       mrg 		++n;
    187       1.1       mrg 
    188       1.1       mrg 	return n;
    189       1.1       mrg }
    190       1.1       mrg 
    191       1.1       mrg struct audiodev *
    192       1.1       mrg audiodev_get(unsigned int i)
    193       1.1       mrg {
    194       1.1       mrg 	struct audiodev *adev;
    195       1.1       mrg 	unsigned int n;
    196       1.1       mrg 
    197       1.1       mrg 	n = 0;
    198       1.1       mrg 	TAILQ_FOREACH(adev, &audiodevlist, next) {
    199       1.1       mrg 		if (n == i)
    200       1.1       mrg 			return adev;
    201       1.1       mrg 		++n;
    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.6.16.1  christos 	snprintf(audiopath, sizeof(audiopath),
    216       1.1       mrg 	    _PATH_AUDIO "%u", adev->unit);
    217  1.6.16.1  christos 	snprintf(soundpath, sizeof(soundpath),
    218       1.1       mrg 	    _PATH_SOUND "%u", adev->unit);
    219  1.6.16.1  christos 	snprintf(audioctlpath, sizeof(audioctlpath),
    220       1.1       mrg 	    _PATH_AUDIOCTL "%u", adev->unit);
    221  1.6.16.1  christos 	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.6.16.1  christos audiodev_set_param(struct audiodev *adev, int mode,
    251  1.6.16.1  christos 	const char *encname, unsigned int prec, unsigned int ch, unsigned int freq)
    252  1.6.16.1  christos {
    253  1.6.16.1  christos 	struct audio_info ai;
    254  1.6.16.1  christos 	int setmode;
    255  1.6.16.1  christos 	u_int enc;
    256  1.6.16.1  christos 
    257  1.6.16.1  christos 	setmode = 0;
    258  1.6.16.1  christos 	ai = adev->info;
    259  1.6.16.1  christos 
    260  1.6.16.1  christos 	for (enc = 0; enc < encoding_max; enc++) {
    261  1.6.16.1  christos 		if (strcmp(encname, encoding_names[enc]) == 0)
    262  1.6.16.1  christos 			break;
    263  1.6.16.1  christos 	}
    264  1.6.16.1  christos 	if (enc >= encoding_max) {
    265  1.6.16.1  christos 		fprintf(stderr, "unknown encoding name: %s\n", encname);
    266  1.6.16.1  christos 		errno = EINVAL;
    267  1.6.16.1  christos 		return -1;
    268  1.6.16.1  christos 	}
    269  1.6.16.1  christos 
    270  1.6.16.1  christos 	if ((ai.mode & mode & AUMODE_PLAY)) {
    271  1.6.16.1  christos 		setmode |= AUMODE_PLAY;
    272  1.6.16.1  christos 		ai.play.encoding = enc;
    273  1.6.16.1  christos 		ai.play.precision = prec;
    274  1.6.16.1  christos 		ai.play.channels = ch;
    275  1.6.16.1  christos 		ai.play.sample_rate = freq;
    276  1.6.16.1  christos 	}
    277  1.6.16.1  christos 	if ((ai.mode & mode & AUMODE_RECORD)) {
    278  1.6.16.1  christos 		setmode |= AUMODE_RECORD;
    279  1.6.16.1  christos 		ai.record.encoding = enc;
    280  1.6.16.1  christos 		ai.record.precision = prec;
    281  1.6.16.1  christos 		ai.record.channels = ch;
    282  1.6.16.1  christos 		ai.record.sample_rate = freq;
    283  1.6.16.1  christos 	}
    284  1.6.16.1  christos 
    285  1.6.16.1  christos 	if (setmode == 0) {
    286  1.6.16.1  christos 		errno = EINVAL;
    287  1.6.16.1  christos 		return -1;
    288  1.6.16.1  christos 	}
    289  1.6.16.1  christos 
    290  1.6.16.1  christos 	ai.mode = setmode;
    291  1.6.16.1  christos 	printf("setting %s to %s:%u, %uch, %uHz\n",
    292  1.6.16.1  christos 	    adev->xname, encname, prec, ch, freq);
    293  1.6.16.1  christos 	if (ioctl(adev->fd, AUDIO_SETFORMAT, &ai) == -1) {
    294  1.6.16.1  christos 		perror("ioctl AUDIO_SETFORMAT");
    295  1.6.16.1  christos 		return -1;
    296  1.6.16.1  christos 	}
    297  1.6.16.1  christos 	return 0;
    298  1.6.16.1  christos }
    299  1.6.16.1  christos 
    300  1.6.16.1  christos int
    301       1.2  jmcneill audiodev_test(struct audiodev *adev, unsigned int chanmask)
    302       1.2  jmcneill {
    303       1.2  jmcneill 	audio_info_t info;
    304       1.2  jmcneill 	int16_t *buf;
    305       1.2  jmcneill 	size_t buflen;
    306       1.2  jmcneill 	off_t off;
    307  1.6.16.1  christos 	int fd;
    308  1.6.16.1  christos 	int rv = -1;
    309  1.6.16.1  christos 
    310  1.6.16.1  christos 	fd = open(adev->path, O_WRONLY);
    311  1.6.16.1  christos 	if (fd == -1) {
    312  1.6.16.1  christos 		perror("open");
    313  1.6.16.1  christos 		return -1;
    314  1.6.16.1  christos 	}
    315       1.2  jmcneill 
    316       1.2  jmcneill 	AUDIO_INITINFO(&info);
    317       1.2  jmcneill 	info.play.sample_rate = AUDIODEV_SAMPLE_RATE;
    318  1.6.16.1  christos 	info.play.channels = adev->info.play.channels;
    319       1.2  jmcneill 	info.play.precision = 16;
    320       1.2  jmcneill 	info.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
    321       1.2  jmcneill 	info.mode = AUMODE_PLAY;
    322  1.6.16.1  christos 	if (ioctl(fd, AUDIO_SETINFO, &info) == -1) {
    323       1.2  jmcneill 		perror("ioctl AUDIO_SETINFO");
    324  1.6.16.1  christos 		goto abort;
    325       1.2  jmcneill 	}
    326  1.6.16.1  christos 	if (ioctl(fd, AUDIO_GETINFO, &info) == -1) {
    327       1.2  jmcneill 		perror("ioctl AUDIO_GETINFO");
    328  1.6.16.1  christos 		goto abort;
    329       1.2  jmcneill 	}
    330       1.2  jmcneill 
    331       1.2  jmcneill 	dtmf_new(&buf, &buflen, info.play.sample_rate, 2,
    332  1.6.16.1  christos 	    adev->info.play.channels, chanmask, 350.0, 440.0);
    333  1.6.16.1  christos 	if (buf == NULL) {
    334  1.6.16.1  christos 		goto abort;
    335  1.6.16.1  christos 	}
    336       1.2  jmcneill 
    337       1.2  jmcneill 	off = 0;
    338       1.2  jmcneill 	while (buflen > 0) {
    339       1.5  dholland 		size_t wlen;
    340       1.5  dholland 		ssize_t ret;
    341       1.5  dholland 
    342       1.5  dholland 		wlen = info.play.buffer_size;
    343       1.2  jmcneill 		if (wlen > buflen)
    344       1.2  jmcneill 			wlen = buflen;
    345  1.6.16.1  christos 		ret = write(fd, (char *)buf + off, wlen);
    346       1.5  dholland 		if (ret == -1) {
    347       1.4  jmcneill 			perror("write");
    348       1.4  jmcneill 			goto done;
    349       1.4  jmcneill 		}
    350       1.5  dholland 		wlen = ret;
    351       1.2  jmcneill 		off += wlen;
    352       1.2  jmcneill 		buflen -= wlen;
    353       1.2  jmcneill 	}
    354       1.2  jmcneill 
    355  1.6.16.1  christos 	if (ioctl(fd, AUDIO_DRAIN) == -1) {
    356       1.2  jmcneill 		perror("ioctl AUDIO_DRAIN");
    357  1.6.16.1  christos 		goto done;
    358  1.6.16.1  christos 	}
    359       1.2  jmcneill 
    360  1.6.16.1  christos 	rv = 0;
    361       1.4  jmcneill done:
    362       1.2  jmcneill 	free(buf);
    363  1.6.16.1  christos abort:
    364  1.6.16.1  christos 	close(fd);
    365       1.2  jmcneill 
    366       1.4  jmcneill 	return rv;
    367       1.2  jmcneill }
    368