summaryrefslogtreecommitdiffstats
path: root/sys/dev/sound/pcm/dsp.c
blob: 59c14e9a00749b88d44f1d4ecdc0b5d927a51526 (plain)
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*
 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/param.h>
#include <sys/queue.h>

#include <dev/sound/pcm/sound.h>

#define OLDPCM_IOCTL

static int getchns(snddev_info *d, int chan, pcm_channel **rdch, pcm_channel **wrch);

static pcm_channel *
allocchn(snddev_info *d, int direction)
{
	pcm_channel *chns = (direction == PCMDIR_PLAY)? d->play : d->rec;
	int i, cnt = (direction == PCMDIR_PLAY)? d->playcount : d->reccount;
	for (i = 0; i < cnt; i++) {
		if (!(chns[i].flags & (CHN_F_BUSY | CHN_F_DEAD))) {
			chns[i].flags |= CHN_F_BUSY;
			return &chns[i];
		}
	}
	return NULL;
}

static int
getchns(snddev_info *d, int chan, pcm_channel **rdch, pcm_channel **wrch)
{
	KASSERT((d->flags & SD_F_PRIO_SET) != SD_F_PRIO_SET, \
		("getchns: read and write both prioritised"));

	if ((d->flags & SD_F_SIMPLEX) && (d->flags & SD_F_PRIO_SET)) {
		*rdch = (d->flags & SD_F_PRIO_RD)? d->arec[chan] : &d->fakechan;
		*wrch = (d->flags & SD_F_PRIO_WR)? d->aplay[chan] : &d->fakechan;
		d->fakechan.flags |= CHN_F_BUSY;
	} else {
		*rdch = d->arec[chan];
		*wrch = d->aplay[chan];
	}
	return 0;
}

static void
setchns(snddev_info *d, int chan)
{
	KASSERT((d->flags & SD_F_PRIO_SET) != SD_F_PRIO_SET, \
		("getchns: read and write both prioritised"));
	d->flags |= SD_F_DIR_SET;
}

int
dsp_open(snddev_info *d, int chan, int oflags, int devtype)
{
	pcm_channel *rdch, *wrch;
	u_int32_t fmt;

	if (chan >= d->chancount) return ENODEV;
	if ((d->flags & SD_F_SIMPLEX) && (d->ref[chan] > 0)) return EBUSY;
	rdch = d->arec[chan];
	wrch = d->aplay[chan];
	if (oflags & FREAD) {
		if (rdch == NULL) {
			rdch = allocchn(d, PCMDIR_REC);
			if (!rdch) return EBUSY;
		} else return EBUSY;
	}
	if (oflags & FWRITE) {
		if (wrch == NULL) {
			wrch = allocchn(d, PCMDIR_PLAY);
			if (!wrch) {
				if (rdch && (oflags & FREAD))
					rdch->flags &= ~CHN_F_BUSY;
				return EBUSY;
			}
		} else return EBUSY;
	}
	d->aplay[chan] = wrch;
	d->arec[chan] = rdch;
	d->ref[chan]++;
	switch (devtype) {
	case SND_DEV_DSP16:
		fmt = AFMT_S16_LE;
		break;

	case SND_DEV_DSP:
		fmt = AFMT_U8;
		break;

	case SND_DEV_AUDIO:
		fmt = AFMT_MU_LAW;
		break;

	case SND_DEV_NORESET:
		fmt = 0;
		break;

	default:
		return ENXIO;
	}

	if (rdch && (oflags & FREAD)) {
	        chn_reset(rdch, fmt);
		if (oflags & O_NONBLOCK) rdch->flags |= CHN_F_NBIO;
	}
	if (wrch && (oflags & FWRITE)) {
	        chn_reset(wrch, fmt);
		if (oflags & O_NONBLOCK) wrch->flags |= CHN_F_NBIO;
	}
	return 0;
}

int
dsp_close(snddev_info *d, int chan, int devtype)
{
	pcm_channel *rdch, *wrch;

	d->ref[chan] = 0;
#if 0
	/* enable this if/when every close() is propagated here */
	if (d->ref[chan]) return 0;
#endif
	d->flags &= ~SD_F_TRANSIENT;
	rdch = d->arec[chan];
	wrch = d->aplay[chan];

	if (rdch) {
		chn_abort(rdch);
		rdch->flags &= ~(CHN_F_BUSY | CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
		chn_reset(rdch, 0);
	}
	if (wrch) {
		chn_flush(wrch);
		wrch->flags &= ~(CHN_F_BUSY | CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
		chn_reset(wrch, 0);
	}
	d->aplay[chan] = NULL;
	d->arec[chan] = NULL;
	return 0;
}

int
dsp_read(snddev_info *d, int chan, struct uio *buf, int flag)
{
	pcm_channel *rdch, *wrch;

	if (!(d->flags & SD_F_PRIO_SET)) d->flags |= SD_F_PRIO_RD;
	if (!(d->flags & SD_F_DIR_SET)) setchns(d, chan);
	getchns(d, chan, &rdch, &wrch);
	KASSERT(rdch, ("dsp_read: nonexistant channel"));
	KASSERT(rdch->flags & CHN_F_BUSY, ("dsp_read: nonbusy channel"));
	if (rdch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) return EINVAL;
	if (!(rdch->flags & CHN_F_RUNNING))
		rdch->flags |= CHN_F_RUNNING;
	return chn_read(rdch, buf);
}

int
dsp_write(snddev_info *d, int chan, struct uio *buf, int flag)
{
	pcm_channel *rdch, *wrch;

	if (!(d->flags & SD_F_PRIO_SET)) d->flags |= SD_F_PRIO_WR;
	if (!(d->flags & SD_F_DIR_SET)) setchns(d, chan);
	getchns(d, chan, &rdch, &wrch);
	KASSERT(wrch, ("dsp_write: nonexistant channel"));
	KASSERT(wrch->flags & CHN_F_BUSY, ("dsp_write: nonbusy channel"));
	if (wrch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) return EINVAL;
	if (!(wrch->flags & CHN_F_RUNNING))
		wrch->flags |= CHN_F_RUNNING;
	return chn_write(wrch, buf);
}

int
dsp_ioctl(snddev_info *d, int chan, u_long cmd, caddr_t arg)
{
    	int ret = 0, *arg_i = (int *)arg;
    	u_long s;
    	pcm_channel *wrch = NULL, *rdch = NULL;

	rdch = d->arec[chan];
	wrch = d->aplay[chan];

	if (rdch && (rdch->flags & CHN_F_DEAD))
		rdch = NULL;
	if (wrch && (wrch->flags & CHN_F_DEAD))
		wrch = NULL;
	if (!(rdch || wrch))
		return EINVAL;
    	/*
     	 * all routines are called with int. blocked. Make sure that
     	 * ints are re-enabled when calling slow or blocking functions!
     	 */
    	s = spltty();
    	switch(cmd) {
#ifdef OLDPCM_IOCTL
    	/*
     	 * we start with the new ioctl interface.
     	 */
    	case AIONWRITE:	/* how many bytes can write ? */
		if (wrch && wrch->buffer.dl)
			while (chn_wrfeed(wrch) > 0);
		*arg_i = wrch? wrch->buffer2nd.fl : 0;
		break;

    	case AIOSSIZE:     /* set the current blocksize */
		{
	    		struct snd_size *p = (struct snd_size *)arg;
	    		if (wrch)
				chn_setblocksize(wrch, 2, p->play_size);
	    		if (rdch)
				chn_setblocksize(rdch, 2, p->rec_size);
		}
		/* FALLTHROUGH */
    	case AIOGSIZE:	/* get the current blocksize */
		{
	    		struct snd_size *p = (struct snd_size *)arg;
	    		if (wrch) p->play_size = wrch->buffer2nd.blksz;
	    		if (rdch) p->rec_size = rdch->buffer2nd.blksz;
		}
		break;

    	case AIOSFMT:
		{
	    		snd_chan_param *p = (snd_chan_param *)arg;
	    		if (wrch) {
				chn_setformat(wrch, p->play_format);
				chn_setspeed(wrch, p->play_rate);
	    		}
	    		if (rdch) {
				chn_setformat(rdch, p->rec_format);
				chn_setspeed(rdch, p->rec_rate);
	    		}
		}
		/* FALLTHROUGH */

    	case AIOGFMT:
		{
	    		snd_chan_param *p = (snd_chan_param *)arg;
	    		p->play_rate = wrch? wrch->speed : 0;
	    		p->rec_rate = rdch? rdch->speed : 0;
	    		p->play_format = wrch? wrch->format : 0;
	    		p->rec_format = rdch? rdch->format : 0;
		}
		break;

    	case AIOGCAP:     /* get capabilities */
		{
	    		snd_capabilities *p = (snd_capabilities *)arg;
			pcmchan_caps *pcaps = NULL, *rcaps = NULL;
			if (rdch) rcaps = chn_getcaps(rdch);
			if (wrch) pcaps = chn_getcaps(wrch);
	    		p->rate_min = max(rcaps? rcaps->minspeed : 0,
	                      		  pcaps? pcaps->minspeed : 0);
	    		p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
	                      		  pcaps? pcaps->maxspeed : 1000000);
	    		p->bufsize = min(rdch? rdch->buffer2nd.bufsize : 1000000,
	                     		 wrch? wrch->buffer2nd.bufsize : 1000000);
			/* XXX bad on sb16 */
	    		p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
			 	     (wrch? chn_getformats(wrch) : 0xffffffff);
			if (rdch && wrch)
				p->formats |= (d->flags & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
	    		p->mixers = 1; /* default: one mixer */
	    		p->inputs = d->mixer->devs;
	    		p->left = p->right = 100;
		}
		break;

    	case AIOSTOP:
		if (*arg_i == AIOSYNC_PLAY && wrch) *arg_i = chn_abort(wrch);
		else if (*arg_i == AIOSYNC_CAPTURE && rdch) *arg_i = chn_abort(rdch);
		else {
	   	 	printf("AIOSTOP: bad channel 0x%x\n", *arg_i);
	    		*arg_i = 0;
		}
		break;

    	case AIOSYNC:
		printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
	    		((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
		break;
#endif
	/*
	 * here follow the standard ioctls (filio.h etc.)
	 */
    	case FIONREAD: /* get # bytes to read */
		if (rdch && rdch->buffer.dl)
			while (chn_rdfeed(rdch) > 0);
		*arg_i = rdch? rdch->buffer2nd.rl : 0;
		break;

    	case FIOASYNC: /*set/clear async i/o */
		DEB( printf("FIOASYNC\n") ; )
		break;

    	case SNDCTL_DSP_NONBLOCK:
    	case FIONBIO: /* set/clear non-blocking i/o */
		if (rdch) rdch->flags &= ~CHN_F_NBIO;
		if (wrch) wrch->flags &= ~CHN_F_NBIO;
		if (*arg_i) {
		    	if (rdch) rdch->flags |= CHN_F_NBIO;
		    	if (wrch) wrch->flags |= CHN_F_NBIO;
		}
		break;

    	/*
	 * Finally, here is the linux-compatible ioctl interface
	 */
#define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
    	case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
    	case SNDCTL_DSP_GETBLKSIZE:
		if (wrch)
			*arg_i = wrch->buffer2nd.blksz;
		else if (rdch)
			*arg_i = rdch->buffer2nd.blksz;
		else
			*arg_i = 0;
		break ;

    	case SNDCTL_DSP_SETBLKSIZE:
		RANGE(*arg_i, 16, 65536);
		if (wrch) chn_setblocksize(wrch, 2, *arg_i);
		if (rdch) chn_setblocksize(rdch, 2, *arg_i);
		break;

    	case SNDCTL_DSP_RESET:
		DEB(printf("dsp reset\n"));
		splx(s);
		if (wrch) chn_abort(wrch);
		if (rdch) chn_abort(rdch);
		break;

    	case SNDCTL_DSP_SYNC:
		DEB(printf("dsp sync\n"));
		splx(s);
		if (wrch) chn_sync(wrch, wrch->buffer2nd.bufsize - 4);
		break;

    	case SNDCTL_DSP_SPEED:
		splx(s);
		if (wrch)
			ret = chn_setspeed(wrch, *arg_i);
		if (rdch && ret == 0)
			ret = chn_setspeed(rdch, *arg_i);
		/* fallthru */

    	case SOUND_PCM_READ_RATE:
		*arg_i = wrch? wrch->speed : rdch->speed;
		break;

    	case SNDCTL_DSP_STEREO:
		splx(s);
		if (wrch)
			ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) |
					((*arg_i)? AFMT_STEREO : 0));
		if (rdch && ret == 0)
			ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) |
				        ((*arg_i)? AFMT_STEREO : 0));
		*arg_i = ((wrch? wrch->format : rdch->format) & AFMT_STEREO)? 1 : 0;
		break;

    	case SOUND_PCM_WRITE_CHANNELS:
/*	case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
		splx(s);
		if (*arg_i == 1 || *arg_i == 2) {
	  		if (wrch)
				ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) |
					((*arg_i == 2)? AFMT_STEREO : 0));
			if (rdch && ret == 0)
				ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) |
				        ((*arg_i == 2)? AFMT_STEREO : 0));
			*arg_i = ((wrch? wrch->format : rdch->format) & AFMT_STEREO)? 2 : 1;
		} else
			*arg_i = 0;
		break;

    	case SOUND_PCM_READ_CHANNELS:
		*arg_i = ((wrch? wrch->format : rdch->format) & AFMT_STEREO)? 2 : 1;
		break;

    	case SNDCTL_DSP_GETFMTS:	/* returns a mask of supported fmts */
		*arg_i = wrch? chn_getformats(wrch) : chn_getformats(rdch);
		break ;

    	case SNDCTL_DSP_SETFMT:	/* sets _one_ format */
		splx(s);
		if ((*arg_i != AFMT_QUERY)) {
			if (wrch)
				ret = chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO));
			if (rdch && ret == 0)
				ret = chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO));
		}
		*arg_i = (wrch? wrch->format : rdch->format) & ~AFMT_STEREO;
		break;

    	case SNDCTL_DSP_SUBDIVIDE:
		/* XXX watch out, this is RW! */
		printf("SNDCTL_DSP_SUBDIVIDE unimplemented\n");
		break;

    	case SNDCTL_DSP_SETFRAGMENT:
		DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
		{
			pcm_channel *c = wrch? wrch : rdch;
			u_int32_t fragln = (*arg_i) & 0x0000ffff;
			u_int32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
			u_int32_t fragsz;

			RANGE(fragln, 4, 16);
			fragsz = 1 << fragln;

			if (maxfrags == 0)
				maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
			if (maxfrags < 2) {
				ret = EINVAL;
				break;
			}
			if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
				maxfrags = CHN_2NDBUFMAXSIZE / fragsz;

			DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
		    	if (rdch)
				ret = chn_setblocksize(rdch, maxfrags, fragsz);
		    	if (wrch && ret == 0)
				ret = chn_setblocksize(wrch, maxfrags, fragsz);

			fragsz = c->buffer2nd.blksz;
			fragln = 0;
			while (fragsz > 1) {
				fragln++;
				fragsz >>= 1;
			}
	    		*arg_i = (c->buffer2nd.blkcnt << 16) | fragln;
		}
		break;

    	case SNDCTL_DSP_GETISPACE: /* XXX Space for reading? Makes no sense... */
		/* return the size of data available in the input queue */
		{
	    		audio_buf_info *a = (audio_buf_info *)arg;
	    		if (rdch) {
	        		snd_dbuf *b = &rdch->buffer;
	        		snd_dbuf *bs = &rdch->buffer2nd;
				if (b->dl && !(rdch->flags & CHN_F_MAPPED))
					/*
					 * Suck up the secondary and DMA buffer.
					 * chn_rdfeed*() takes care of the alignment.
					 */
					while (chn_rdfeed(rdch) > 0);
				a->bytes = bs->rl;
	        		a->fragments = a->bytes / rdch->buffer2nd.blksz;
	        		a->fragstotal = rdch->buffer2nd.blkcnt;
	        		a->fragsize = rdch->buffer2nd.blksz;
	    		}
		}
		break;

    	case SNDCTL_DSP_GETOSPACE:
		/* return space available in the output queue */
		{
	    		audio_buf_info *a = (audio_buf_info *)arg;
	    		if (wrch) {
	        		snd_dbuf *b = &wrch->buffer;
	        		snd_dbuf *bs = &wrch->buffer2nd;
				if (b->dl && !(wrch->flags & CHN_F_MAPPED)) {
					/*
					 * Fill up the secondary and DMA buffer.
					 * chn_wrfeed*() takes care of the alignment.
					 * Check for underflow before writing into the buffers.
					 */
					chn_checkunderflow(wrch);
					while (chn_wrfeed(wrch) > 0);
				}
				a->bytes = bs->fl;
	        		a->fragments = a->bytes / wrch->buffer2nd.blksz;
	        		a->fragstotal = wrch->buffer2nd.blkcnt;
	        		a->fragsize = wrch->buffer2nd.blksz;
	    		}
		}
		break;

    	case SNDCTL_DSP_GETIPTR:
		{
	    		count_info *a = (count_info *)arg;
	    		if (rdch) {
	        		snd_dbuf *b = &rdch->buffer;
	        		snd_dbuf *bs = &rdch->buffer2nd;
	        		if (b->dl && !(rdch->flags & CHN_F_MAPPED))
					/*
					 * Suck up the secondary and DMA buffer.
					 * chn_rdfeed*() takes care of the alignment.
					 */
					while (chn_rdfeed(rdch) > 0);
	        		a->bytes = bs->total;
	        		a->blocks = rdch->blocks;
	        		a->ptr = bs->rp;
				rdch->blocks = 0;
	    		} else ret = EINVAL;
		}
		break;

    	case SNDCTL_DSP_GETOPTR:
		{
	    		count_info *a = (count_info *)arg;
	    		if (wrch) {
    	        		snd_dbuf *b = &wrch->buffer;
	        		snd_dbuf *bs = &wrch->buffer2nd;
				if (b->dl && !(wrch->flags & CHN_F_MAPPED)) {
					/*
					 * Fill up the secondary and DMA buffer.
					 * chn_wrfeed*() takes care of the alignment.
					 * Check for underflow before writing into the buffers.
					 */
					chn_checkunderflow(wrch);
					while (chn_wrfeed(wrch) > 0);
				}
	        		a->bytes = bs->total;
	        		a->blocks = wrch->blocks;
	        		a->ptr = bs->rp;
				wrch->blocks = 0;
	    		} else ret = EINVAL;
		}
		break;

    	case SNDCTL_DSP_GETCAPS:
		*arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
		if (rdch && wrch && !(d->flags & SD_F_SIMPLEX))
			*arg_i |= DSP_CAP_DUPLEX;
		break;

    	case SOUND_PCM_READ_BITS:
        	*arg_i = ((wrch? wrch->format : rdch->format) & AFMT_16BIT)? 16 : 8;
		break;

    	case SNDCTL_DSP_SETTRIGGER:
		if (rdch) {
			rdch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
		    	if (*arg_i & PCM_ENABLE_INPUT)
				rdch->flags |= CHN_F_TRIGGERED;
			else
				rdch->flags |= CHN_F_NOTRIGGER;
			chn_intr(rdch);
		}
		if (wrch) {
			wrch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
		    	if (*arg_i & PCM_ENABLE_OUTPUT)
				wrch->flags |= CHN_F_TRIGGERED;
			else
				wrch->flags |= CHN_F_NOTRIGGER;
			chn_intr(wrch);
		}
		break;

    	case SNDCTL_DSP_GETTRIGGER:
		*arg_i = 0;
		if (wrch && wrch->flags & CHN_F_TRIGGERED)
			*arg_i |= PCM_ENABLE_OUTPUT;
		if (rdch && rdch->flags & CHN_F_TRIGGERED)
			*arg_i |= PCM_ENABLE_INPUT;
		break;

	case SNDCTL_DSP_GETODELAY:
		if (wrch) {
			snd_dbuf *b = &wrch->buffer;
	        	snd_dbuf *bs = &wrch->buffer2nd;
			if (b->dl) {
				chn_checkunderflow(wrch);
				if (!(wrch->flags & CHN_F_MAPPED))
					while (chn_wrfeed(wrch) > 0);
			}
			*arg_i = b->rl + bs->rl;
		} else
			ret = EINVAL;
		break;

    	case SNDCTL_DSP_POST:
		if (wrch) {
			wrch->flags &= ~CHN_F_NOTRIGGER;
			chn_start(wrch, 1);
		}
		break;

    	case SNDCTL_DSP_MAPINBUF:
    	case SNDCTL_DSP_MAPOUTBUF:
    	case SNDCTL_DSP_SETSYNCRO:
		/* undocumented */

    	case SOUND_PCM_WRITE_FILTER:
    	case SOUND_PCM_READ_FILTER:
		/* dunno what these do, don't sound important */
    	default:
		DEB(printf("default ioctl chan%d fn 0x%08lx fail\n", chan, cmd));
		ret = EINVAL;
		break;
    	}
    	splx(s);
    	return ret;
}

int
dsp_poll(snddev_info *d, int chan, int events, struct proc *p)
{
	int ret = 0, e;
	pcm_channel *wrch = NULL, *rdch = NULL;

	getchns(d, chan, &rdch, &wrch);
	e = events & (POLLOUT | POLLWRNORM);
	if (wrch && e) ret |= chn_poll(wrch, e, p);
	e = events & (POLLIN | POLLRDNORM);
	if (rdch && e) ret |= chn_poll(rdch, e, p);
	return ret;
}

int
dsp_mmap(snddev_info *d, int chan, vm_offset_t offset, int nprot)
{
	pcm_channel *wrch = NULL, *rdch = NULL, *c = NULL;

	getchns(d, chan, &rdch, &wrch);
	/* XXX this is broken by line 204 of vm/device_pager.c, so force write buffer */
	if (1 || (wrch && (nprot & PROT_WRITE)))
		c = wrch;
	else if (rdch && (nprot & PROT_READ))
		c = rdch;
	if (c && (c->format == c->buffer.fmt)) {
		c->flags |= CHN_F_MAPPED;
		return atop(vtophys(c->buffer2nd.buf + offset));
	} else
		return -1;
}

OpenPOWER on IntegriCloud