1/*
2 * audio.c - audio interface for reference clock audio drivers
3 */
4#ifdef HAVE_CONFIG_H
5# include <config.h>
6#endif
7
8#if defined(HAVE_SYS_AUDIOIO_H) || defined(HAVE_SUN_AUDIOIO_H) || \
9    defined(HAVE_SYS_SOUNDCARD_H) || defined(HAVE_MACHINE_SOUNDCARD_H)
10
11#include "audio.h"
12#include "ntp_stdlib.h"
13#include "ntp_syslog.h"
14#ifdef HAVE_UNISTD_H
15# include <unistd.h>
16#endif
17#include <stdio.h>
18#include "ntp_string.h"
19
20#ifdef HAVE_SYS_AUDIOIO_H
21# include <sys/audioio.h>
22#endif /* HAVE_SYS_AUDIOIO_H */
23
24#ifdef HAVE_SUN_AUDIOIO_H
25# include <sys/ioccom.h>
26# include <sun/audioio.h>
27#endif /* HAVE_SUN_AUDIOIO_H */
28
29#ifdef HAVE_SYS_IOCTL_H
30# include <sys/ioctl.h>
31#endif /* HAVE_SYS_IOCTL_H */
32
33#include <fcntl.h>
34
35#ifdef HAVE_MACHINE_SOUNDCARD_H
36# include <machine/soundcard.h>
37# define PCM_STYLE_SOUND
38#else
39# ifdef HAVE_SYS_SOUNDCARD_H
40#  include <sys/soundcard.h>
41#  define PCM_STYLE_SOUND
42# endif
43#endif
44
45#ifdef PCM_STYLE_SOUND
46# include <ctype.h>
47#endif
48
49/*
50 * Global variables
51 */
52#ifdef HAVE_SYS_AUDIOIO_H
53static struct audio_device device; /* audio device ident */
54#endif /* HAVE_SYS_AUDIOIO_H */
55#ifdef PCM_STYLE_SOUND
56# define INIT_FILE "/etc/ntp.audio"
57int agc =	SOUND_MIXER_WRITE_RECLEV; /* or IGAIN or LINE */
58int monitor =	SOUND_MIXER_WRITE_VOLUME; /* or OGAIN */
59int devmask = 0;
60int recmask = 0;
61char cf_c_dev[100], cf_i_dev[100], cf_agc[100], cf_monitor[100];
62
63const char *m_names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
64#else /* not PCM_STYLE_SOUND */
65static struct audio_info info;	/* audio device info */
66#endif /* not PCM_STYLE_SOUND */
67static int ctl_fd;		/* audio control file descriptor */
68
69#ifdef PCM_STYLE_SOUND
70static void audio_config_read (int, char **, char **);
71static int  mixer_name (const char *, int);
72
73
74int
75mixer_name(
76	const char *m_name,
77	int m_mask
78	)
79{
80	int i;
81
82	for (i = 0; i < SOUND_MIXER_NRDEVICES; ++i)
83		if (((1 << i) & m_mask)
84		    && !strcmp(m_names[i], m_name))
85			break;
86
87	return (SOUND_MIXER_NRDEVICES == i)
88	    ? -1
89	    : i
90	    ;
91}
92
93
94/*
95 * Check:
96 *
97 * /etc/ntp.audio#	where # is the unit number
98 * /etc/ntp.audio.#	where # is the unit number
99 * /etc/ntp.audio
100 *
101 * for contents of the form:
102 *
103 * idev /dev/input_device
104 * cdev /dev/control_device
105 * agc pcm_input_device {igain,line,line1,...}
106 * monitor pcm_monitor_device {ogain,...}
107 *
108 * The device names for the "agc" and "monitor" keywords
109 * can be found by running either the "mixer" program or the
110 * util/audio-pcm program.
111 *
112 * Great hunks of this subroutine were swiped from refclock_oncore.c
113 */
114static void
115audio_config_read(
116	int unit,
117	char **c_dev,	/* Control device */
118	char **i_dev	/* input device */
119	)
120{
121	FILE *fd;
122	char device[20], line[100], ab[100];
123
124	sprintf(device, "%s%d", INIT_FILE, unit);
125	if ((fd = fopen(device, "r")) == NULL) {
126		printf("audio_config_read: <%s> NO\n", device);
127		sprintf(device, "%s.%d", INIT_FILE, unit);
128		if ((fd = fopen(device, "r")) == NULL) {
129			printf("audio_config_read: <%s> NO\n", device);
130			sprintf(device, "%s.%d", INIT_FILE, unit);
131			if ((fd = fopen(device, "r")) == NULL) {
132				printf("audio_config_read: <%s> NO\n", device);
133				return;
134			}
135		}
136	}
137	printf("audio_config_read: reading <%s>\n", device);
138	while (fgets(line, sizeof line, fd)) {
139		char *cp, *cc, *ca;
140		int i;
141
142		/* Remove comments */
143		if ((cp = strchr(line, '#')))
144			*cp = '\0';
145
146		/* Remove any trailing spaces */
147		for (i = strlen(line);
148		     i > 0 && isascii((int)line[i - 1]) && isspace((int)line[i - 1]);
149			)
150			line[--i] = '\0';
151
152		/* Remove leading space */
153		for (cc = line; *cc && isascii((int)*cc) && isspace((int)*cc); cc++)
154			continue;
155
156		/* Stop if nothing left */
157		if (!*cc)
158			continue;
159
160		/* Uppercase the command and find the arg */
161		for (ca = cc; *ca; ca++) {
162			if (isascii((int)*ca)) {
163				if (islower((int)*ca)) {
164					*ca = toupper(*ca);
165				} else if (isspace((int)*ca) || (*ca == '='))
166					break;
167			}
168		}
169
170		/* Remove space (and possible =) leading the arg */
171		for (; *ca && isascii((int)*ca) && (isspace((int)*ca) || (*ca == '=')); ca++)
172			continue;
173
174		if (!strncmp(cc, "IDEV", (size_t) 4)) {
175			sscanf(ca, "%s", ab);
176			strcpy(cf_i_dev, ab);
177			printf("idev <%s>\n", ab);
178		} else if (!strncmp(cc, "CDEV", (size_t) 4)) {
179			sscanf(ca, "%s", ab);
180			strcpy(cf_c_dev, ab);
181			printf("cdev <%s>\n", ab);
182		} else if (!strncmp(cc, "AGC", (size_t) 3)) {
183			sscanf(ca, "%s", ab);
184			strcpy(cf_agc, ab);
185			printf("agc <%s> %d\n", ab, i);
186		} else if (!strncmp(cc, "MONITOR", (size_t) 7)) {
187			sscanf(ca, "%s", ab);
188			strcpy(cf_monitor, ab);
189			printf("monitor <%s> %d\n", ab, mixer_name(ab, -1));
190		}
191	}
192	fclose(fd);
193	return;
194}
195#endif /* PCM_STYLE_SOUND */
196
197/*
198 * audio_init - open and initialize audio device
199 *
200 * This code works with SunOS 4.x, Solaris 2.x, and PCM; however, it is
201 * believed generic and applicable to other systems with a minor twid
202 * or two. All it does is open the device, set the buffer size (Solaris
203 * only), preset the gain and set the input port. It assumes that the
204 * codec sample rate (8000 Hz), precision (8 bits), number of channels
205 * (1) and encoding (ITU-T G.711 mu-law companded) have been set by
206 * default.
207 */
208int
209audio_init(
210	char	*dname,		/* device name */
211	int	bufsiz,		/* buffer size */
212	int	unit		/* device unit (0-3) */
213	)
214{
215#ifdef PCM_STYLE_SOUND
216# define ACTL_DEV	"/dev/mixer%d"
217	char actl_dev[30];
218# ifdef HAVE_STRUCT_SND_SIZE
219	struct snd_size s_size;
220# endif
221# ifdef AIOGFMT
222	snd_chan_param s_c_p;
223# endif
224#endif
225	int fd;
226	int rval;
227	char *actl =
228#ifdef PCM_STYLE_SOUND
229		actl_dev
230#else
231		"/dev/audioctl"
232#endif
233		;
234
235#ifdef PCM_STYLE_SOUND
236	(void)sprintf(actl_dev, ACTL_DEV, unit);
237
238	audio_config_read(unit, &actl, &dname);
239	/* If we have values for cf_c_dev or cf_i_dev, use them. */
240	if (*cf_c_dev)
241		actl = cf_c_dev;
242	if (*cf_i_dev)
243		dname = cf_i_dev;
244#endif
245
246	/*
247	 * Open audio device
248	 */
249	fd = open(dname, O_RDWR | O_NONBLOCK, 0777);
250	if (fd < 0) {
251		msyslog(LOG_ERR, "audio_init: %s %m\n", dname);
252		return (fd);
253	}
254
255	/*
256	 * Open audio control device.
257	 */
258	ctl_fd = open(actl, O_RDWR);
259	if (ctl_fd < 0) {
260		msyslog(LOG_ERR, "audio_init: invalid control device <%s>\n",
261		    actl);
262		close(fd);
263		return(ctl_fd);
264	}
265
266	/*
267	 * Set audio device parameters.
268	 */
269#ifdef PCM_STYLE_SOUND
270	printf("audio_init: <%s> bufsiz %d\n", dname, bufsiz);
271	rval = fd;
272
273# ifdef HAVE_STRUCT_SND_SIZE
274	if (ioctl(fd, AIOGSIZE, &s_size) == -1)
275	    printf("audio_init: AIOGSIZE: %s\n", strerror(errno));
276	else
277	    printf("audio_init: orig: play_size %d, rec_size %d\n",
278		s_size.play_size, s_size.rec_size);
279
280	s_size.play_size = s_size.rec_size = bufsiz;
281	printf("audio_init: want: play_size %d, rec_size %d\n",
282	       s_size.play_size, s_size.rec_size);
283
284	if (ioctl(fd, AIOSSIZE, &s_size) == -1)
285	    printf("audio_init: AIOSSIZE: %s\n", strerror(errno));
286	else
287	    printf("audio_init: set:  play_size %d, rec_size %d\n",
288		s_size.play_size, s_size.rec_size);
289# endif /* HAVE_STRUCT_SND_SIZE */
290
291# ifdef SNDCTL_DSP_SETFRAGMENT
292	{
293		int tmp = (16 << 16) + 6; /* 16 fragments, each 2^6 bytes */
294		if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &tmp) == -1)
295		    printf("audio_init: SNDCTL_DSP_SETFRAGMENT: %s\n",
296			   strerror(errno));
297	}
298# endif /* SNDCTL_DSP_SETFRAGMENT */
299
300# ifdef AIOGFMT
301	if (ioctl(fd, AIOGFMT, &s_c_p) == -1)
302	    printf("audio_init: AIOGFMT: %s\n", strerror(errno));
303	else
304	    printf("audio_init: play_rate %lu, rec_rate %lu, play_format %#lx, rec_format %#lx\n",
305		s_c_p.play_rate, s_c_p.rec_rate, s_c_p.play_format, s_c_p.rec_format);
306# endif
307
308	/* Grab the device and record masks */
309
310	if (ioctl(ctl_fd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
311	    printf("SOUND_MIXER_READ_DEVMASK: %s\n", strerror(errno));
312	if (ioctl(ctl_fd, SOUND_MIXER_READ_RECMASK, &recmask) == -1)
313	    printf("SOUND_MIXER_READ_RECMASK: %s\n", strerror(errno));
314
315	/* validate and set any specified config file stuff */
316	if (*cf_agc) {
317		int i;
318
319		i = mixer_name(cf_agc, devmask);
320		if (i >= 0)
321			agc = MIXER_WRITE(i);
322		else
323			printf("input %s not in recmask %#x\n",
324			       cf_agc, recmask);
325	}
326
327	if (*cf_monitor) {
328		int i;
329
330		/* devmask */
331		i = mixer_name(cf_monitor, devmask);
332		if (i >= 0)
333			monitor = MIXER_WRITE(i);
334		else
335			printf("monitor %s not in devmask %#x\n",
336			       cf_monitor, devmask);
337	}
338
339#else /* not PCM_STYLE_SOUND */
340	AUDIO_INITINFO(&info);
341	info.play.gain = AUDIO_MAX_GAIN;
342	info.play.port = AUDIO_SPEAKER;
343# ifdef HAVE_SYS_AUDIOIO_H
344	info.record.buffer_size = bufsiz;
345# endif /* HAVE_SYS_AUDIOIO_H */
346	rval = ioctl(ctl_fd, (int)AUDIO_SETINFO, (char *)&info);
347	if (rval < 0) {
348		msyslog(LOG_ERR, "audio: invalid control device parameters\n");
349		close(ctl_fd);
350		close(fd);
351		return(rval);
352	}
353	rval = fd;
354#endif /* not PCM_STYLE_SOUND */
355	return (rval);
356}
357
358
359/*
360 * audio_gain - adjust codec gains and port
361 */
362int
363audio_gain(
364	int gain,		/* volume level (gain) 0-255 */
365	int mongain,		/* input to output mix (monitor gain) 0-255 */
366	int port		/* selected I/O port: 1 mic/2 line in */
367	)
368{
369	int rval;
370	static int o_mongain = -1;
371	static int o_port = -1;
372
373#ifdef PCM_STYLE_SOUND
374	int l, r;
375
376	rval = 0;
377
378	r = l = 100 * gain / 255;	/* Normalize to 0-100 */
379# ifdef DEBUG
380	if (debug > 1)
381		printf("audio_gain: gain %d/%d\n", gain, l);
382# endif
383#if 0	/* not a good idea to do this; connector wiring dependency */
384	/* figure out what channel(s) to use. just nuke right for now. */
385	r = 0 ; /* setting to zero nicely mutes the channel */
386#endif
387	l |= r << 8;
388        if ( cf_agc )
389          rval = ioctl(ctl_fd, agc, &l);
390        else
391	  if (port == 2) {
392	    rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_LINE, &l);
393	  } else {
394	    rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_MIC, &l);
395	  }
396	if (rval == -1) {
397		printf("audio_gain: agc write: %s\n", strerror(errno));
398		return (rval);
399	}
400
401	if (o_mongain != mongain) {
402		r = l = 100 * mongain / 255;    /* Normalize to 0-100 */
403# ifdef DEBUG
404		if (debug > 1)
405			printf("audio_gain: mongain %d/%d\n", mongain, l);
406# endif
407		l |= r << 8;
408                if ( cf_monitor )
409                  rval = ioctl(ctl_fd, monitor, &l );
410                else
411		  rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_VOLUME, &l);
412		if (rval == -1) {
413			printf("audio_gain: mongain write: %s\n",
414			       strerror(errno));
415			return (rval);
416		}
417		o_mongain = mongain;
418	}
419
420	if (o_port != port) {
421# ifdef DEBUG
422		if (debug > 1)
423			printf("audio_gain: port %d\n", port);
424# endif
425		l = (1 << ((port == 2) ? SOUND_MIXER_LINE : SOUND_MIXER_MIC));
426		rval = ioctl(ctl_fd, SOUND_MIXER_WRITE_RECSRC, &l);
427		if (rval == -1) {
428			printf("SOUND_MIXER_WRITE_RECSRC: %s\n",
429			       strerror(errno));
430			return (rval);
431		}
432# ifdef DEBUG
433		if (debug > 1) {
434			if (ioctl(ctl_fd, SOUND_MIXER_READ_RECSRC, &l) == -1)
435				printf("SOUND_MIXER_WRITE_RECSRC: %s\n",
436				       strerror(errno));
437			else
438				printf("audio_gain: recsrc is %d\n", l);
439		}
440# endif
441		o_port = port;
442	}
443#else /* not PCM_STYLE_SOUND */
444	ioctl(ctl_fd, (int)AUDIO_GETINFO, (char *)&info);
445	info.record.encoding = AUDIO_ENCODING_ULAW;
446	info.record.error = 0;
447	info.record.gain = gain;
448	if (o_mongain != mongain)
449		o_mongain = info.monitor_gain = mongain;
450	if (o_port != port)
451		o_port = info.record.port = port;
452	rval = ioctl(ctl_fd, (int)AUDIO_SETINFO, (char *)&info);
453	if (rval < 0) {
454		msyslog(LOG_ERR, "audio_gain: %m");
455		return (rval);
456	}
457	rval = info.record.error;
458#endif /* not PCM_STYLE_SOUND */
459	return (rval);
460}
461
462
463/*
464 * audio_show - display audio parameters
465 *
466 * This code doesn't really do anything, except satisfy curiousity and
467 * verify the ioctl's work.
468 */
469void
470audio_show(void)
471{
472#ifdef PCM_STYLE_SOUND
473	int recsrc = 0;
474
475	printf("audio_show: ctl_fd %d\n", ctl_fd);
476	if (ioctl(ctl_fd, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
477	    printf("SOUND_MIXER_READ_RECSRC: %s\n", strerror(errno));
478
479#else /* not PCM_STYLE_SOUND */
480# ifdef HAVE_SYS_AUDIOIO_H
481	ioctl(ctl_fd, (int)AUDIO_GETDEV, &device);
482	printf("audio: name %s, version %s, config %s\n",
483	    device.name, device.version, device.config);
484# endif /* HAVE_SYS_AUDIOIO_H */
485	ioctl(ctl_fd, (int)AUDIO_GETINFO, (char *)&info);
486	printf(
487	    "audio: rate %d, chan %d, prec %d, code %d, gain %d, mon %d, port %d\n",
488	    info.record.sample_rate, info.record.channels,
489	    info.record.precision, info.record.encoding,
490	    info.record.gain, info.monitor_gain, info.record.port);
491	printf(
492	    "audio: samples %d, eof %d, pause %d, error %d, waiting %d, balance %d\n",
493	    info.record.samples, info.record.eof,
494	    info.record.pause, info.record.error,
495	    info.record.waiting, info.record.balance);
496#endif /* not PCM_STYLE_SOUND */
497}
498#else
499int audio_bs;
500#endif /* HAVE_{SYS_AUDIOIO,SUN_AUDIOIO,MACHINE_SOUNDCARD,SYS_SOUNDCARD}_H */
501