aboutsummaryrefslogtreecommitdiff
path: root/sys/i386/isa/sound/vat_audio.c
blob: 74f0bdd2526ddd12161994871afcb9170e5dc397 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * Minimally compliant SunOS compatible audio driver front-end
 * for use with VAT.
 *
 * This is a front end for the voxware based drivers that form the standard
 * audio driver system for FreeBSD.  It will not operate without the voxware
 * package.
 *
 * This is not a full implementation of the SunOS audio driver, don't
 * expect anything other than vat to operate with it.
 *
 * ---WARNING
 * ---WARNING this work is not complete, it still doesn't work
 * ---WARNING
 *
 * Copyright (C) 1993-1994 Jim Lowe
 * Additional development by Amancio Hasty
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation.
 *
 * This software is provided `as-is'.  The author disclaims all
 * warranties with regard to this software, including without
 * limitation all implied warranties of merchantability, fitness for
 * a particular purpose, or noninfringement.  In no event shall the
 * author be liable for any damages whatsoever, including
 * special, incidental or consequential damages, including loss of
 * use, data, or profits, even if advised of the possibility thereof,
 * and regardless of whether in an action in contract, tort or
 * negligence, arising out of or in connection with the use or
 * performance of this software.
 *
 */

#include "vat_audio.h"
#include "snd.h"                 /* Generic Sound Driver (voxware) */

#if (NVAT_AUDIO > 0) && (NSND > 0)

#include "sound_config.h"
#include "os.h"
#include "vat_audioio.h"

#define	splaudio	splclock

extern int sndopen  (dev_t dev, int flags);
extern int sndclose (dev_t dev, int flags);
extern int sndioctl (dev_t dev, int cmd, void *arg, int mode);
extern int sndread  (int dev, struct uio *uio);
extern int sndwrite (int dev, struct uio *uio);

struct va_softc	{
	dev_t	rdev;		/* record device */
	dev_t	pdev;		/* playback device */
	dev_t	mixer;		/* mixer device */
	struct	selinfo wsel;	/* write select info */
	struct	selinfo	rsel;	/* read select info */
	int	rlevel;
	int	plevel;
	int	open;
} va_softc;

#define	DEF_SAMPLE_RATE	8007

#ifndef AUDIOBLOCKSIZE
#define	AUDIOBLOCKSIZE	160	/* 20ms at 8khz */
#endif

static int iblocksize = AUDIOBLOCKSIZE;
static int oblocksize = 1024;

static u_int record_devices = (SOUND_MASK_MIC|SOUND_MASK_LINE|SOUND_MASK_CD);

static int default_level[SOUND_MIXER_NRDEVICES] = { /* max = 0x64 */
	0x3232,		/* Master Volume */
	0x3232,		/* Bass */
	0x3232,		/* Treble */
	0x0000,		/* FM */
	0x6464,		/* PCM */
	0x0000,		/* PC Speaker */
	0x6464,		/* Ext Line */
	0x6464,		/* Mic */
	0x4b4b,		/* CD */
	0x0000,		/* Recording monitor (input mixer)  -- avoid feedback */
	0x4b4b,		/* SB PCM */
	0x6464,		/* Record Level -- to ADC */
};


static void
setpgain(int level)
{
	register struct va_softc *va = (struct va_softc *)&va_softc;
	int	arg;

	level = (level * 100 / 255) & 0x7f;
	arg   = (level << 8) | level;

	sndioctl(va->mixer, MIXER_WRITE(SOUND_MIXER_PCM), &arg, 0);
}

static void
setrgain(int level)
{
	register struct va_softc *va = (struct va_softc *)&va_softc;
	int	arg, arg1;

	level = (level * 100 / 255) & 0x7f;
	arg   = (level << 8) | level;

	sndioctl(va->mixer, SOUND_MIXER_WRITE_LINE, &arg, 0);
	sndioctl(va->mixer, SOUND_MIXER_WRITE_MIC,  &arg, 0);
	sndioctl(va->mixer, SOUND_MIXER_WRITE_CD,   &arg, 0);
}

static int
setprate(int rate)
{
	register struct va_softc *va = (struct va_softc *)&va_softc;
	register int dev;

	dev = va->pdev >> 4;
	return (audio_devs[dev]->ioctl(dev, SOUND_PCM_WRITE_RATE, rate, 1));
}

static int
setrrate(int rate)
{
	register struct va_softc *va = (struct va_softc *)&va_softc;
	register int dev;

	dev = va->rdev >> 4;
	return (audio_devs[dev]->ioctl(dev, SOUND_PCM_WRITE_RATE, rate, 1));
}

int
vaopen(dev_t dev, int flags)
{
	register struct va_softc *va = (struct va_softc *)&va_softc;
	int	s;

	if (va->open)
		return(EBUSY);
	else
		va->open = 1;

#ifdef	SND_BIDIR
	va->rdev  = SND_DEV_AUDIO | (1<<4);	/* first and second device */
	va->pdev  = SND_DEV_AUDIO | (0<<4);
	va->mixer = SND_DEV_CTL;

	s = sndopen(va->rdev, FREAD);
	if (s) {
		va->open = 0;
		return(s);
	}

	s = sndopen(va->pdev, FWRITE);
	if (s) {
		va->open = 0;
		sndclose(va->rdev, FREAD);
		return(s);
	}
#else
	va->rdev  = SND_DEV_AUDIO | (0<<4);	/* first attached device */
	va->pdev  = SND_DEV_AUDIO | (0<<4);
	va->mixer = SND_DEV_CTL;

	s = sndopen(va->rdev, flags);
	if (s) {
		va->open = 0;
		return(s);
	}
#endif

	/* set sample rates */
	setprate(DEF_SAMPLE_RATE);
	setrrate(DEF_SAMPLE_RATE);

	/* set block size for I/O samples */
	sndioctl(va->rdev, SNDCTL_DSP_GETBLKSIZE, &iblocksize, 0);
	sndioctl(va->pdev, SNDCTL_DSP_GETBLKSIZE, &oblocksize, 0);

	/* initialize mixer controls the way we want them */
	sndioctl(va->mixer, SOUND_MIXER_WRITE_RECSRC, &record_devices, 0);

	for (s = 0; s < SOUND_MIXER_NRDEVICES; s++)
		sndioctl(va->mixer, MIXER_WRITE(s), &default_level[s], 0);

	va->rlevel = (default_level[SOUND_MASK_MIC] & 0x7f) * 255 / 100;
	va->plevel = (default_level[SOUND_MASK_PCM] & 0x7f) * 255 / 100;

	if (flags & FREAD)		/* start the read process */
		DMAbuf_start_input(va->rdev>>4);

	return(0);
}

int
vaclose(dev_t dev, int flags)
{
	register struct va_softc *va = (struct va_softc *)&va_softc;

	va->open = 0;

	sndioctl(va->mixer, SNDCTL_DSP_RESET, NULL, 0);

#ifdef	SND_BIDIR
	sndclose(va->pdev, FWRITE);
	sndclose(va->rdev, FREAD);
#else
	sndclose(va->rdev, flags);
#endif
	return (0);
}

int
varead(dev_t dev, struct uio *buf)
{
	register struct va_softc *va = (struct va_softc *)&va_softc;

	return sndread(va->rdev, buf);
}

int
vawrite(dev_t dev, struct uio *buf)
{
	register struct va_softc *va = (struct va_softc *)&va_softc;

	return sndwrite(va->pdev, buf);
}

void
audio_get_info(struct va_softc *va, struct audio_info *ai)
{
	struct	audio_prinfo	*r, *p;
	int	rdev = va->rdev >> 4;
	int	pdev = va->pdev >> 4;

	r = &ai->record;
	p = &ai->play;

	p->sample_rate =
		audio_devs[pdev]->ioctl(pdev, SOUND_PCM_READ_RATE, 0, 1);
	r->sample_rate =
		audio_devs[rdev]->ioctl(rdev, SOUND_PCM_READ_RATE, 0, 1);

	p->channels =
		audio_devs[pdev]->ioctl(pdev, SOUND_PCM_READ_CHANNELS, 0, 1);
	r->channels =
		audio_devs[rdev]->ioctl(rdev, SOUND_PCM_READ_CHANNELS, 0, 1);

	p->precision = audio_devs[pdev]->ioctl(pdev, SOUND_PCM_READ_BITS, 0, 1);
	r->precision = audio_devs[rdev]->ioctl(rdev, SOUND_PCM_READ_BITS, 0, 1);

	p->encoding = r->encoding = AUDIO_ENCODING_ULAW;

	ai->monitor_gain = 0;

	r->gain = va->rlevel;
	p->gain = va->plevel;

	r->port = 1;
	p->port = AUDIO_SPEAKER;

	p->open = r->open = va->open;
}

void
audio_set_info(struct va_softc *va, struct audio_info *ai)
{
	struct	audio_prinfo	*r, *p;
	int	rdev = va->rdev >> 4;
	int	pdev = va->pdev >> 4;

	r = &ai->record;
	p = &ai->play;

	/* Only set gains if mode == -1, I think this is a bug in vat. */

	if (ai->mode == ~0) {
		if (p->gain != ~0) {
			va->plevel = p->gain;
			setpgain(va->plevel);
		}
		if (r->gain != ~0) {
			va->rlevel = r->gain;
			setrgain(va->rlevel);
		}
	}

	if (p->sample_rate != ~0)
		p->sample_rate = setprate(p->sample_rate);

	if (r->sample_rate != ~0)
		r->sample_rate = setrrate(r->sample_rate);

	DMAbuf_start_input(rdev);
}

int
vaioctl(dev_t dev, int cmd, caddr_t arg, int mode)
{
	register struct va_softc *va = (struct va_softc *)&va_softc;
	int	s;

	switch(cmd) {
	case FIONBIO:
		break;	/* handled above in file i/o routines */
	case AUDIO_GETINFO:
		audio_get_info(va, (struct audio_info *)arg);
		break;
	case AUDIO_SETINFO:
		audio_set_info(va, (struct audio_info *)arg);
		break;
	default:
		printf("vaioctl: cmd=0x%x, '%c', num = %d, len=%d, %s\n",
			cmd, IOCGROUP(cmd), cmd & 0xff, IOCPARM_LEN(cmd),
			cmd&IOC_IN ? "in" : "out");

		s = sndioctl(va->rdev, cmd, arg, mode);

		if (s == 0)
			s = sndioctl(va->pdev, cmd, arg, mode);
		break;
	}
	return(0);
}

int
vaselect(dev_t dev, int rw, struct proc *p)
{
	register struct va_softc *va = (struct va_softc *)&va_softc;
	int	s, r;

	r = 0;
	s = splaudio();

	switch (rw) {
	case FREAD:
		if (DMAbuf_input_ready(va->rdev>>4))
			r = 1;
		else
			selrecord(p, &va->rsel);
		break;
	case FWRITE:
		if (DMAbuf_output_ready(va->pdev>>4))
			r = 1;
		else
			selrecord(p, &va->wsel);
		break;
	}

	splx(s);
	return(r);
}

void
audio_rint(void)
{
	register struct va_softc *va = (struct va_softc *)&va_softc;

	if (!va->open)
		return;

	selwakeup(&va->rsel);
}

void
audio_pint(void)
{
	register struct va_softc *va = (struct va_softc *)&va_softc;

	if (!va->open)
		return;

	selwakeup(&va->wsel);
}
#else

void audio_rint(void) {}
void audio_pint(void) {}

#endif /* NVAT_AUDIO && NSND */