dsp.c revision 59246
1/*
2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/dev/sound/pcm/dsp.c 59246 2000-04-15 05:04:12Z cg $
27 */
28
29#include <sys/param.h>
30#include <sys/queue.h>
31#include <sys/kernel.h>
32
33#include <dev/sound/pcm/sound.h>
34
35static int getchns(snddev_info *d, int chan, pcm_channel **rdch, pcm_channel **wrch);
36
37static pcm_channel *
38allocchn(snddev_info *d, int direction)
39{
40	pcm_channel *chns = (direction == PCMDIR_PLAY)? d->play : d->rec;
41	int i, cnt = (direction == PCMDIR_PLAY)? d->playcount : d->reccount;
42	for (i = 0; i < cnt; i++) {
43		if (!(chns[i].flags & CHN_F_BUSY)) {
44			chns[i].flags |= CHN_F_BUSY;
45			return &chns[i];
46		}
47	}
48	return NULL;
49}
50
51static int
52getchns(snddev_info *d, int chan, pcm_channel **rdch, pcm_channel **wrch)
53{
54	KASSERT((d->flags & SD_F_PRIO_SET) != SD_F_PRIO_SET, \
55		("getchns: read and write both prioritised"));
56
57	if ((d->flags & SD_F_SIMPLEX) && (d->flags & SD_F_PRIO_SET)) {
58		*rdch = (d->flags & SD_F_PRIO_RD)? d->arec[chan] : &d->fakechan;
59		*wrch = (d->flags & SD_F_PRIO_WR)? d->aplay[chan] : &d->fakechan;
60		d->fakechan.flags |= CHN_F_BUSY;
61	} else {
62		*rdch = d->arec[chan];
63		*wrch = d->aplay[chan];
64	}
65	return 0;
66}
67
68static void
69setchns(snddev_info *d, int chan)
70{
71	KASSERT((d->flags & SD_F_PRIO_SET) != SD_F_PRIO_SET, \
72		("getchns: read and write both prioritised"));
73	d->flags |= SD_F_DIR_SET;
74	if (d->swap) d->swap(d->devinfo, (d->flags & SD_F_PRIO_WR)? PCMDIR_PLAY : PCMDIR_REC);
75}
76
77int
78dsp_open(snddev_info *d, int chan, int oflags, int devtype)
79{
80	pcm_channel *rdch, *wrch;
81	u_int32_t fmt;
82
83	if (chan >= d->chancount) return ENODEV;
84	if ((d->flags & SD_F_SIMPLEX) && (d->ref[chan] > 0)) return EBUSY;
85	rdch = d->arec[chan];
86	wrch = d->aplay[chan];
87	if (oflags & FREAD) {
88		if (rdch == NULL) {
89			rdch = allocchn(d, PCMDIR_REC);
90			if (!rdch) return EBUSY;
91		} else return EBUSY;
92	}
93	if (oflags & FWRITE) {
94		if (wrch == NULL) {
95			wrch = allocchn(d, PCMDIR_PLAY);
96			if (!wrch && (oflags & FREAD)) {
97				rdch->flags &= ~CHN_F_BUSY;
98				return EBUSY;
99			}
100		} else return EBUSY;
101	}
102	d->aplay[chan] = wrch;
103	d->arec[chan] = rdch;
104	d->ref[chan]++;
105	switch (devtype) {
106	case SND_DEV_DSP16:
107		fmt = AFMT_S16_LE;
108		break;
109
110	case SND_DEV_DSP:
111		fmt = AFMT_U8;
112		break;
113
114	case SND_DEV_AUDIO:
115		fmt = AFMT_MU_LAW;
116		break;
117
118	case SND_DEV_NORESET:
119		fmt = 0;
120		break;
121
122	default:
123		return ENXIO;
124	}
125
126	if (rdch && (oflags & FREAD)) {
127	        chn_reset(rdch);
128		if (oflags & O_NONBLOCK) rdch->flags |= CHN_F_NBIO;
129		if (fmt) {
130			rdch->volume = (100 << 8) | 100;
131			rdch->format = fmt;
132			rdch->speed = DSP_DEFAULT_SPEED;
133		}
134	}
135	if (wrch && (oflags & FWRITE)) {
136	        chn_reset(wrch);
137		if (oflags & O_NONBLOCK) wrch->flags |= CHN_F_NBIO;
138		if (fmt) {
139			wrch->volume = (100 << 8) | 100;
140			wrch->format = fmt;
141			wrch->speed = DSP_DEFAULT_SPEED;
142		}
143	}
144	return 0;
145}
146
147int
148dsp_close(snddev_info *d, int chan, int devtype)
149{
150	pcm_channel *rdch, *wrch;
151
152	d->ref[chan]--;
153	if (d->ref[chan]) return 0;
154	d->flags &= ~SD_F_TRANSIENT;
155	rdch = d->arec[chan];
156	wrch = d->aplay[chan];
157
158	if (rdch) {
159		chn_abort(rdch);
160		rdch->flags &= ~(CHN_F_BUSY | CHN_F_RUNNING | CHN_F_MAPPED);
161		chn_reset(rdch);
162	}
163	if (wrch) {
164		chn_flush(wrch);
165		wrch->flags &= ~(CHN_F_BUSY | CHN_F_RUNNING | CHN_F_MAPPED);
166		chn_reset(wrch);
167	}
168	d->aplay[chan] = NULL;
169	d->arec[chan] = NULL;
170	return 0;
171}
172
173int
174dsp_read(snddev_info *d, int chan, struct uio *buf, int flag)
175{
176	pcm_channel *rdch, *wrch;
177
178	if (!(d->flags & SD_F_PRIO_SET)) d->flags |= SD_F_PRIO_RD;
179	if (!(d->flags & SD_F_DIR_SET)) setchns(d, chan);
180	getchns(d, chan, &rdch, &wrch);
181	KASSERT(rdch, ("dsp_read: nonexistant channel"));
182	KASSERT(rdch->flags & CHN_F_BUSY, ("dsp_read: nonbusy channel"));
183	if (rdch->flags & CHN_F_MAPPED) return EINVAL;
184	if (!(rdch->flags & CHN_F_RUNNING)) {
185		rdch->flags |= CHN_F_RUNNING;
186		chn_reinit(rdch);
187	}
188	return chn_read(rdch, buf);
189}
190
191int
192dsp_write(snddev_info *d, int chan, struct uio *buf, int flag)
193{
194	pcm_channel *rdch, *wrch;
195
196	if (!(d->flags & SD_F_PRIO_SET)) d->flags |= SD_F_PRIO_WR;
197	if (!(d->flags & SD_F_DIR_SET)) setchns(d, chan);
198	getchns(d, chan, &rdch, &wrch);
199	KASSERT(wrch, ("dsp_write: nonexistant channel"));
200	KASSERT(wrch->flags & CHN_F_BUSY, ("dsp_write: nonbusy channel"));
201	if (wrch->flags & CHN_F_MAPPED) return EINVAL;
202	if (!(wrch->flags & CHN_F_RUNNING)) {
203		wrch->flags |= CHN_F_RUNNING;
204		chn_reinit(wrch);
205	}
206	return chn_write(wrch, buf);
207}
208
209int
210dsp_ioctl(snddev_info *d, int chan, u_long cmd, caddr_t arg)
211{
212    	int ret = 0, *arg_i = (int *)arg;
213    	u_long s;
214    	pcm_channel *wrch = NULL, *rdch = NULL;
215
216	rdch = d->arec[chan];
217	wrch = d->aplay[chan];
218
219    	/*
220     	 * all routines are called with int. blocked. Make sure that
221     	 * ints are re-enabled when calling slow or blocking functions!
222     	 */
223    	s = spltty();
224    	switch(cmd) {
225#ifdef OLDPCM_IOCTL
226    	/*
227     	 * we start with the new ioctl interface.
228     	 */
229    	case AIONWRITE:	/* how many bytes can write ? */
230		if (wrch && wrch->buffer.dl)
231			while (chn_wrfeed(wrch) > 0);
232		*arg_i = wrch? wrch->buffer2nd.fl : 0;
233		break;
234
235    	case AIOSSIZE:     /* set the current blocksize */
236		{
237	    		struct snd_size *p = (struct snd_size *)arg;
238	    		if (wrch) chn_setblocksize(wrch, p->play_size);
239	    		if (rdch) chn_setblocksize(rdch, p->rec_size);
240		}
241		/* FALLTHROUGH */
242    	case AIOGSIZE:	/* get the current blocksize */
243		{
244	    		struct snd_size *p = (struct snd_size *)arg;
245	    		if (wrch) p->play_size = wrch->blocksize2nd;
246	    		if (rdch) p->rec_size = rdch->blocksize2nd;
247		}
248		break;
249
250    	case AIOSFMT:
251		{
252	    		snd_chan_param *p = (snd_chan_param *)arg;
253	    		if (wrch) {
254				chn_setformat(wrch, p->play_format);
255				chn_setspeed(wrch, p->play_rate);
256	    		}
257	    		if (rdch) {
258				chn_setformat(rdch, p->rec_format);
259				chn_setspeed(rdch, p->rec_rate);
260	    		}
261		}
262		/* FALLTHROUGH */
263
264    	case AIOGFMT:
265		{
266	    		snd_chan_param *p = (snd_chan_param *)arg;
267	    		p->play_rate = wrch? wrch->speed : 0;
268	    		p->rec_rate = rdch? rdch->speed : 0;
269	    		p->play_format = wrch? wrch->format : 0;
270	    		p->rec_format = rdch? rdch->format : 0;
271		}
272		break;
273
274    	case AIOGCAP:     /* get capabilities */
275		{
276	    		snd_capabilities *p = (snd_capabilities *)arg;
277			pcmchan_caps *pcaps = NULL, *rcaps = NULL;
278			if (rdch) rcaps = chn_getcaps(rdch);
279			if (wrch) pcaps = chn_getcaps(wrch);
280	    		p->rate_min = max(rcaps? rcaps->minspeed : 0,
281	                      		  pcaps? pcaps->minspeed : 0);
282	    		p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
283	                      		  pcaps? pcaps->maxspeed : 1000000);
284	    		p->bufsize = min(rdch? rdch->buffer2nd.bufsize : 1000000,
285	                     		 wrch? wrch->buffer2nd.bufsize : 1000000);
286			/* XXX bad on sb16 */
287	    		p->formats = (rcaps? rcaps->formats : 0xffffffff) &
288			 	     (pcaps? pcaps->formats : 0xffffffff);
289	    		p->mixers = 1; /* default: one mixer */
290	    		p->inputs = d->mixer.devs;
291	    		p->left = p->right = 100;
292		}
293		break;
294
295    	case AIOSTOP:
296		if (*arg_i == AIOSYNC_PLAY && wrch) *arg_i = chn_abort(wrch);
297		else if (*arg_i == AIOSYNC_CAPTURE && rdch) *arg_i = chn_abort(rdch);
298		else {
299	   	 	printf("AIOSTOP: bad channel 0x%x\n", *arg_i);
300	    		*arg_i = 0;
301		}
302		break;
303
304    	case AIOSYNC:
305		printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
306	    		((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
307		break;
308	/*
309	 * here follow the standard ioctls (filio.h etc.)
310	 */
311    	case FIONREAD: /* get # bytes to read */
312		if (rdch && rdch->buffer.dl)
313			while (chn_rdfeed(rdch) > 0);
314		*arg_i = rdch? rdch->buffer2nd.rl : 0;
315		break;
316
317    	case FIOASYNC: /*set/clear async i/o */
318		DEB( printf("FIOASYNC\n") ; )
319		break;
320#endif
321    	case SNDCTL_DSP_NONBLOCK:
322#ifdef OLDPCM_IOCTL
323    	case FIONBIO: /* set/clear non-blocking i/o */
324#endif
325		if (rdch) rdch->flags &= ~CHN_F_NBIO;
326		if (wrch) wrch->flags &= ~CHN_F_NBIO;
327		if (*arg_i) {
328		    	if (rdch) rdch->flags |= CHN_F_NBIO;
329		    	if (wrch) wrch->flags |= CHN_F_NBIO;
330		}
331		break;
332
333    	/*
334	 * Finally, here is the linux-compatible ioctl interface
335	 */
336#define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
337    	case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
338    	case SNDCTL_DSP_GETBLKSIZE:
339		if (wrch)
340			*arg_i = wrch->blocksize2nd;
341		else if (rdch)
342			*arg_i = rdch->blocksize2nd;
343		else
344			*arg_i = 0;
345		break ;
346
347    	case SNDCTL_DSP_SETBLKSIZE:
348		RANGE(*arg_i, 16, 65536);
349		if (wrch) chn_setblocksize(wrch, 2, *arg_i);
350		if (rdch) chn_setblocksize(rdch, 2, *arg_i);
351		break;
352
353    	case SNDCTL_DSP_RESET:
354		DEB(printf("dsp reset\n"));
355		splx(s);
356		if (wrch) chn_abort(wrch);
357		if (rdch) chn_abort(rdch);
358		break;
359
360    	case SNDCTL_DSP_SYNC:
361		DEB(printf("dsp sync\n"));
362		splx(s);
363		if (wrch) chn_sync(wrch, wrch->buffer2nd.bufsize - 4);
364		break;
365
366    	case SNDCTL_DSP_SPEED:
367		splx(s);
368		if (wrch) chn_setspeed(wrch, *arg_i);
369		if (rdch) chn_setspeed(rdch, *arg_i);
370		/* fallthru */
371
372    	case SOUND_PCM_READ_RATE:
373		*arg_i = wrch? wrch->speed : rdch->speed;
374		break;
375
376    	case SNDCTL_DSP_STEREO:
377		splx(s);
378		if (wrch) chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) |
379					((*arg_i)? AFMT_STEREO : 0));
380		if (rdch) chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) |
381				        ((*arg_i)? AFMT_STEREO : 0));
382		*arg_i = ((wrch? wrch->format : rdch->format) & AFMT_STEREO)? 1 : 0;
383		break;
384
385    	case SOUND_PCM_WRITE_CHANNELS:
386		splx(s);
387		if (wrch) chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) |
388					((*arg_i == 2)? AFMT_STEREO : 0));
389		if (rdch) chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) |
390					((*arg_i == 2)? AFMT_STEREO : 0));
391		/* fallthru */
392
393    	case SOUND_PCM_READ_CHANNELS:
394		*arg_i = ((wrch? wrch->format : rdch->format) & AFMT_STEREO)? 2 : 1;
395		break;
396
397    	case SNDCTL_DSP_GETFMTS:	/* returns a mask of supported fmts */
398		*arg_i = wrch? chn_getcaps(wrch)->formats : chn_getcaps(rdch)->formats;
399		break ;
400
401    	case SNDCTL_DSP_SETFMT:	/* sets _one_ format */
402		splx(s);
403		if (wrch) chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO));
404		if (rdch) chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO));
405		*arg_i = (wrch? wrch->format: rdch->format) & ~AFMT_STEREO;
406		break;
407
408    	case SNDCTL_DSP_SUBDIVIDE:
409		/* XXX watch out, this is RW! */
410		DEB(printf("SNDCTL_DSP_SUBDIVIDE unimplemented\n");)
411		break;
412
413    	case SNDCTL_DSP_SETFRAGMENT:
414		/* XXX watch out, this is RW! */
415		DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
416		{
417			pcm_channel *c = wrch? wrch : rdch;
418			u_int32_t fragln = (*arg_i) & 0x0000ffff;
419			u_int32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
420			u_int32_t fragsz;
421
422			RANGE(fragln, 4, 16);
423			fragsz = 1 << fragln;
424
425			if (maxfrags == 0)
426				maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
427			if (maxfrags < 2) {
428				ret = EINVAL;
429				break;
430			}
431			if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
432				maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
433
434		    	if (rdch)
435				ret = chn_setblocksize(rdch, maxfrags, fragsz);
436		    	if (wrch && ret == 0)
437				ret = chn_setblocksize(wrch, maxfrags, fragsz);
438
439	    		*arg_i = (c->fragments << 16) | fragsz;
440		}
441		break;
442
443    	case SNDCTL_DSP_GETISPACE: /* XXX Space for reading? Makes no sense... */
444		/* return the size of data available in the input queue */
445		{
446	    		audio_buf_info *a = (audio_buf_info *)arg;
447	    		if (rdch) {
448	        		snd_dbuf *b = &rdch->buffer;
449	        		snd_dbuf *bs = &rdch->buffer2nd;
450				if (b->dl && !(rdch->flags & CHN_F_MAPPED))
451					/*
452					 * Suck up the secondary and DMA buffer.
453					 * chn_rdfeed*() takes care of the alignment.
454					 */
455					while (chn_rdfeed(rdch) > 0);
456				a->bytes = bs->rl;
457	        		a->fragments = a->bytes / rdch->blocksize2nd;
458	        		a->fragstotal = rdch->fragments;
459	        		a->fragsize = rdch->blocksize2nd;
460	    		}
461		}
462		break;
463
464    	case SNDCTL_DSP_GETOSPACE:
465		/* return space available in the output queue */
466		{
467	    		audio_buf_info *a = (audio_buf_info *)arg;
468	    		if (wrch) {
469	        		snd_dbuf *b = &wrch->buffer;
470	        		snd_dbuf *bs = &wrch->buffer2nd;
471				if (b->dl && !(wrch->flags & CHN_F_MAPPED)) {
472					/*
473					 * Fill up the secondary and DMA buffer.
474					 * chn_wrfeed*() takes care of the alignment.
475					 * Check for underflow before writing into the buffers.
476					 */
477					chn_checkunderflow(wrch);
478					while (chn_wrfeed(wrch) > 0);
479				}
480				a->bytes = bs->fl;
481	        		a->fragments = a->bytes / wrch->blocksize2nd;
482	        		a->fragstotal = wrch->fragments;
483	        		a->fragsize = wrch->blocksize2nd;
484	    		}
485		}
486		break;
487
488    	case SNDCTL_DSP_GETIPTR:
489		{
490	    		count_info *a = (count_info *)arg;
491	    		if (rdch) {
492	        		snd_dbuf *b = &rdch->buffer;
493	        		snd_dbuf *bs = &rdch->buffer2nd;
494	        		if (b->dl && !(rdch->flags & CHN_F_MAPPED))
495					/*
496					 * Suck up the secondary and DMA buffer.
497					 * chn_rdfeed*() takes care of the alignment.
498					 */
499					while (chn_rdfeed(rdch) > 0);
500	        		a->bytes = bs->total;
501	        		a->blocks = bs->rl / rdch->blocksize2nd;
502	        		a->ptr = bs->rl % rdch->blocksize2nd;
503	    		} else ret = EINVAL;
504		}
505		break;
506
507    	case SNDCTL_DSP_GETOPTR:
508		{
509	    		count_info *a = (count_info *)arg;
510	    		if (wrch) {
511    	        		snd_dbuf *b = &wrch->buffer;
512	        		snd_dbuf *bs = &wrch->buffer2nd;
513				if (b->dl && !(wrch->flags & CHN_F_MAPPED)) {
514					/*
515					 * Fill up the secondary and DMA buffer.
516					 * chn_wrfeed*() takes care of the alignment.
517					 * Check for underflow before writing into the buffers.
518					 */
519					chn_checkunderflow(wrch);
520					while (chn_wrfeed(wrch) > 0);
521				}
522	        		a->bytes = bs->total;
523	        		a->blocks = wrch->blocks;
524	        		a->ptr = bs->rp;
525				wrch->blocks = 0;
526	    		} else ret = EINVAL;
527		}
528		break;
529
530    	case SNDCTL_DSP_GETCAPS:
531		*arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
532		if (rdch && wrch && !(d->flags & SD_F_SIMPLEX))
533			*arg_i |= DSP_CAP_DUPLEX;
534		break;
535
536    	case SOUND_PCM_READ_BITS:
537        	*arg_i = ((wrch? wrch->format : rdch->format) & AFMT_16BIT)? 16 : 8;
538		break;
539
540    	case SNDCTL_DSP_SETTRIGGER:
541		if (rdch) {
542			rdch->flags &= ~CHN_F_TRIGGERED;
543		    	if (*arg_i & PCM_ENABLE_INPUT)
544				rdch->flags |= CHN_F_TRIGGERED;
545			chn_intr(rdch);
546		}
547		if (wrch) {
548			wrch->flags &= ~CHN_F_TRIGGERED;
549		    	if (*arg_i & PCM_ENABLE_OUTPUT)
550				wrch->flags |= CHN_F_TRIGGERED;
551			chn_intr(wrch);
552		}
553		break;
554
555    	case SNDCTL_DSP_GETTRIGGER:
556		*arg_i = 0;
557		if (wrch && wrch->flags & CHN_F_TRIGGERED)
558			*arg_i |= PCM_ENABLE_OUTPUT;
559		if (rdch && rdch->flags & CHN_F_TRIGGERED)
560			*arg_i |= PCM_ENABLE_INPUT;
561		break;
562
563	case SNDCTL_DSP_GETODELAY:
564		if (wrch) {
565			snd_dbuf *b = &wrch->buffer;
566			if (b->dl) {
567				chn_checkunderflow(wrch);
568				if (!(wrch->flags & CHN_F_MAPPED))
569					while (chn_wrfeed(wrch) > 0);
570			}
571			*arg = b->total;
572		} else
573			ret = EINVAL;
574		break;
575
576    	case SNDCTL_DSP_MAPINBUF:
577    	case SNDCTL_DSP_MAPOUTBUF:
578    	case SNDCTL_DSP_SETSYNCRO:
579		/* undocumented */
580
581    	case SNDCTL_DSP_POST:
582    	case SOUND_PCM_WRITE_FILTER:
583    	case SOUND_PCM_READ_FILTER:
584		/* dunno what these do, don't sound important */
585    	default:
586		DEB(printf("default ioctl chan%d fn 0x%08lx fail\n", chan, cmd));
587		ret = EINVAL;
588		break;
589    	}
590    	splx(s);
591    	return ret;
592}
593
594int
595dsp_poll(snddev_info *d, int chan, int events, struct proc *p)
596{
597	int ret = 0, e;
598	pcm_channel *wrch = NULL, *rdch = NULL;
599
600	getchns(d, chan, &rdch, &wrch);
601	e = events & (POLLOUT | POLLWRNORM);
602	if (wrch && e) ret |= chn_poll(wrch, e, p);
603	e = events & (POLLIN | POLLRDNORM);
604	if (rdch && e) ret |= chn_poll(rdch, e, p);
605	return ret;
606}
607
608int
609dsp_mmap(snddev_info *d, int chan, vm_offset_t offset, int nprot)
610{
611	pcm_channel *wrch = NULL, *rdch = NULL, *c = NULL;
612
613	getchns(d, chan, &rdch, &wrch);
614	/* XXX this is broken by line 204 of vm/device_pager.c, so force write buffer */
615	if (1 || (wrch && (nprot & PROT_WRITE))) c = wrch;
616	else if (rdch && (nprot & PROT_READ)) c = rdch;
617	if (c) {
618		c->flags |= CHN_F_MAPPED;
619		return atop(vtophys(c->buffer2nd.buf + offset));
620	}
621	return -1;
622}
623
624