channel.c revision 61753
1/*
2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
3 * Portions Copyright by Luigi Rizzo - 1997-99
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/dev/sound/pcm/channel.c 61753 2000-06-17 19:29:40Z cg $
28 */
29
30#include <dev/sound/pcm/sound.h>
31
32#define MIN_CHUNK_SIZE 		256	/* for uiomove etc. */
33#define	DMA_ALIGN_THRESHOLD	4
34#define	DMA_ALIGN_MASK		(~(DMA_ALIGN_THRESHOLD - 1))
35
36#define ISA_DMA(b) (((b)->chan >= 0 && (b)->chan != 4 && (b)->chan < 8))
37#define CANCHANGE(c) (!(c)->buffer.dl)
38#define ROUND(x) ((x) & DMA_ALIGN_MASK)
39/*
40#define DEB(x) x
41*/
42static void buf_clear(snd_dbuf *b, u_int32_t fmt, int length);
43static void chn_dmaupdate(pcm_channel *c);
44static void chn_wrintr(pcm_channel *c);
45static void chn_rdintr(pcm_channel *c);
46static u_int32_t chn_start(pcm_channel *c);
47/*
48 * SOUND OUTPUT
49
50We use a circular buffer to store samples directed to the DAC.
51The buffer is split into two variable-size regions, each identified
52by an offset in the buffer (rp,fp) and a length (rl,fl):
53
54      0          rp,rl        fp,fl    bufsize
55      |__________>____________>________|
56	  FREE   d   READY    w FREE
57
58  READY: data written from the process and ready to be sent to the DAC;
59  FREE: free part of the buffer.
60
61Both regions can wrap around the end of the buffer.  At initialization,
62READY is empty, FREE takes all the available space, and dma is
63idle.  dl contains the length of the current DMA transfer, dl=0
64means that the dma is idle.
65
66The two boundaries (rp,fp) in the buffers are advanced by DMA [d]
67and write() [w] operations. The first portion of the READY region
68is used for DMA transfers. The transfer is started at rp and with
69chunks of length dl. During DMA operations, dsp_wr_dmaupdate()
70updates rp, rl and fl tracking the ISA DMA engine as the transfer
71makes progress.
72When a new block is written, fp advances and rl,fl are updated
73accordingly.
74
75The code works as follows: the user write routine dsp_write_body()
76fills up the READY region with new data (reclaiming space from the
77FREE region) and starts the write DMA engine if inactive.  When a
78DMA transfer is complete, an interrupt causes dsp_wrintr() to be
79called which extends the FREE region and possibly starts the next
80transfer.
81
82In some cases, the code tries to track the current status of DMA
83operations by calling dsp_wr_dmaupdate() which changes rp, rl and fl.
84
85The system tries to make all DMA transfers use the same size,
86play_blocksize or rec_blocksize. The size is either selected by
87the user, or computed by the system to correspond to about .25s of
88audio. The blocksize must be within a range which is currently:
89
90	min(5ms, 40 bytes) ... 1/2 buffer size.
91
92When there aren't enough data (write) or space (read), a transfer
93is started with a reduced size.
94
95To reduce problems in case of overruns, the routine which fills up
96the buffer should initialize (e.g. by repeating the last value) a
97reasonably long area after the last block so that no noise is
98produced on overruns.
99
100  *
101  */
102
103
104/* XXX  this is broken: in the event a bounce buffer is used, data never
105 * gets copied in or out of the real buffer.  fix requires mods to isa_dma.c
106 * and possibly fixes to other autodma mode clients
107 */
108static void
109chn_isadmabounce(pcm_channel *c)
110{
111	if (ISA_DMA(&c->buffer)) {
112		/* tell isa_dma to bounce data in/out */
113    	} else KASSERT(1, ("chn_isadmabounce called on invalid channel"));
114}
115
116static int
117chn_polltrigger(pcm_channel *c)
118{
119	snd_dbuf *bs = &c->buffer2nd;
120	unsigned lim = (c->flags & CHN_F_HAS_SIZE)? bs->blksz : 0;
121	int trig = 0;
122
123	if (c->flags & CHN_F_MAPPED)
124		trig = ((bs->int_count > bs->prev_int_count) || bs->prev_int_count == 0);
125	else
126		trig = (((c->direction == PCMDIR_PLAY)? bs->fl : bs->rl) > lim);
127	return trig;
128}
129
130static int
131chn_pollreset(pcm_channel *c)
132{
133	snd_dbuf *bs = &c->buffer2nd;
134
135	if (c->flags & CHN_F_MAPPED)
136		bs->prev_int_count = bs->int_count;
137	return 1;
138}
139
140/*
141 * chn_dmadone() updates pointers and wakes up any process waiting
142 * on a select(). Must be called at spltty().
143 */
144static void
145chn_dmadone(pcm_channel *c)
146{
147	snd_dbuf *b = &c->buffer;
148
149	if (c->direction == PCMDIR_PLAY)
150		chn_checkunderflow(c);
151	else
152		chn_dmaupdate(c);
153	if (ISA_DMA(b))
154		chn_isadmabounce(c); /* sync bounce buffer */
155	b->int_count++;
156}
157
158/*
159 * chn_dmawakeup() wakes up any process sleeping. Separated from
160 * chn_dmadone() so that wakeup occurs only when feed from a
161 * secondary buffer to a DMA buffer takes place. Must be called
162 * at spltty().
163 */
164static void
165chn_dmawakeup(pcm_channel *c)
166{
167	snd_dbuf *b = &c->buffer;
168
169	wakeup(b);
170}
171
172/*
173 * chn_dmaupdate() tracks the status of a dma transfer,
174 * updating pointers. It must be called at spltty().
175 *
176 * NOTE: when we are using auto dma in the device, rl might become
177 * negative.
178 */
179DEB (static int chn_updatecount=0);
180
181static void
182chn_dmaupdate(pcm_channel *c)
183{
184	snd_dbuf *b = &c->buffer;
185	int delta, hwptr;
186	DEB (int b_rl=b->rl; int b_fl=b->fl; int b_rp=b->rp; int b_fp=b->fp);
187
188	hwptr = chn_getptr(c);
189	if (c->direction == PCMDIR_PLAY) {
190		delta = (b->bufsize + hwptr - b->rp) % b->bufsize;
191		b->rp = hwptr;
192		b->rl -= delta;
193		b->fl += delta;
194
195		if (b->rl < 0) {
196			DEB(printf("OUCH!(%d) rl %d(%d) delta %d bufsize %d hwptr %d rp %d(%d)\n", chn_updatecount++, b->rl, b_rl, delta, b->bufsize, hwptr, b->rp, b_rp));
197		}
198	} else {
199		delta = (b->bufsize + hwptr - b->fp) % b->bufsize;
200		b->fp = hwptr;
201		b->rl += delta;
202		b->fl -= delta;
203		if (b->fl < 0) {
204			DEB(printf("OUCH!(%d) fl %d(%d) delta %d bufsize %d hwptr %d fp %d(%d)\n", chn_updatecount++, b->fl, b_fl, delta, b->bufsize, hwptr, b->fp, b_fp));
205		}
206	}
207	b->total += delta;
208}
209
210/*
211 * Check channel for underflow occured. Reset DMA buffer in case of
212 * underflow, so that new data can go into the buffer. It must be
213 * called at spltty().
214 */
215void
216chn_checkunderflow(pcm_channel *c)
217{
218    	snd_dbuf *b = &c->buffer;
219
220	if (b->underflow) {
221		DEB(printf("Clear underflow condition\n"));
222		/*
223		 * The DMA keeps running even after underflow occurs.
224		 * Hence the value returned by chn_getptr() here soon
225		 * gets a lag when we get back to chn_write(). Although
226		 * there are no easy and precise methods to figure out
227		 * the lag, a quarter of b->bufsize would be a fair
228		 * choice, provided that a DMA interrupt generates upon
229		 * each transfer of a half b->bufsize.
230		 */
231		b->rp = chn_getptr(c);
232		b->fp = (b->rp + b->bufsize / 4) % b->bufsize;
233		b->rl = b->bufsize / 4;
234		b->fl = b->bufsize - b->rl;
235	  	b->underflow = 0;
236	} else {
237		/* chn_dmaupdate(c); */
238	}
239}
240
241/*
242 * Feeds new data to the write dma buffer. Can be called in the bottom half.
243 * Hence must be called at spltty.
244 */
245int
246chn_wrfeed(pcm_channel *c)
247{
248    	snd_dbuf *b = &c->buffer;
249    	snd_dbuf *bs = &c->buffer2nd;
250	int a, l, lacc;
251
252	/* ensure we always have a whole number of samples */
253	a = (1 << c->align) - 1;
254	lacc = 0;
255    	if (c->flags & CHN_F_MAPPED) {
256		bs->rl = min(b->blksz, b->fl);
257		bs->fl = 0;
258		a = 0;
259	}
260	/*
261	printf("b: [rl: %d, rp %d, fl %d, fp %d]; bs: [rl: %d, rp %d, fl %d, fp %d]\n",
262		b->rl, b->rp, b->fl, b->fp, bs->rl, bs->rp, bs->fl, bs->fp);
263	*/
264	/* Don't allow write unaligned data */
265	while (bs->rl > a && b->fl > a) {
266		/* ensure we always have a whole number of samples */
267		l = min(min(bs->rl, bs->bufsize - bs->rp), min(b->fl, b->bufsize - b->fp)) & ~a;
268		if (l == 0)
269			return lacc;
270		/* Move the samples, update the markers and pointers. */
271		bcopy(bs->buf + bs->rp, b->buf + b->fp, l);
272		bs->fl += l;
273		bs->rl -= l;
274		bs->rp = (bs->rp + l) % bs->bufsize;
275		b->rl += l;
276		b->fl -= l;
277		b->fp = (b->fp + l) % b->bufsize;
278		/* Clear the new space in the secondary buffer. */
279		buf_clear(bs, bs->fmt, l);
280		/* Accumulate the total bytes of the moved samples. */
281		lacc += l;
282		/* A feed to the DMA buffer is equivalent to an interrupt. */
283		bs->total += l;
284    		if (c->flags & CHN_F_MAPPED) {
285			if (bs->total - bs->prev_total >= bs->blksz) {
286				bs->prev_total = bs->total;
287				bs->int_count++;
288				c->blocks++;
289			}
290		} else
291			bs->int_count++;
292		if (bs->sel.si_pid && chn_polltrigger(c))
293			selwakeup(&bs->sel);
294	}
295
296	return lacc;
297}
298
299/* Feeds new data to the secondary write buffer. */
300static int
301chn_wrfeed2nd(pcm_channel *c, struct uio *buf)
302{
303    	snd_dbuf *bs = &c->buffer2nd;
304	int l, w, wacc;
305
306	/* The DMA buffer may have some space. */
307	while (chn_wrfeed(c) > 0);
308
309	/* ensure we always have a whole number of samples */
310	wacc = 0;
311	while (buf->uio_resid > 0 && bs->fl > 0) {
312		/*
313		 * The size of the data to move here does not have to be
314		 * aligned. We take care of it upon moving the data to a
315		 * DMA buffer.
316		 */
317		l = min(bs->fl, bs->bufsize - bs->fp);
318		/* Move the samples, update the markers and pointers. */
319		w = c->feeder->feed(c->feeder, c, bs->buf + bs->fp, l, buf);
320		if (w == 0)
321			panic("no feed");
322		bs->rl += w;
323		bs->fl -= w;
324		bs->fp = (bs->fp + w) % bs->bufsize;
325		/* Accumulate the total bytes of the moved samples. */
326		wacc += w;
327
328		/* If any pcm data gets moved, push it to the DMA buffer. */
329		if (w > 0)
330			while (chn_wrfeed(c) > 0);
331	}
332
333	return wacc;
334}
335
336/*
337 * Write interrupt routine. Can be called from other places (e.g.
338 * to start a paused transfer), but with interrupts disabled.
339 */
340static void
341chn_wrintr(pcm_channel *c)
342{
343    	snd_dbuf *b = &c->buffer;
344
345    	if (b->underflow && !(c->flags & CHN_F_MAPPED)) {
346/*		printf("underflow return\n");
347*/		return; /* nothing new happened */
348	}
349	if (b->dl)
350		chn_dmadone(c);
351
352    	/*
353	 * start another dma operation only if have ready data in the buffer,
354	 * there is no pending abort, have a full-duplex device, or have a
355	 * half duplex device and there is no pending op on the other side.
356	 *
357	 * Force transfers to be aligned to a boundary of 4, which is
358	 * needed when doing stereo and 16-bit.
359	 */
360
361	/* Check underflow and update the pointers. */
362	chn_checkunderflow(c);
363
364	/*
365	 * Fill up the DMA buffer, followed by waking up the top half.
366	 * If some of the pcm data in uio are still left, the top half
367	 * goes to sleep by itself.
368	 */
369	if (c->flags & CHN_F_MAPPED)
370		chn_wrfeed(c);
371	else {
372		while (chn_wrfeed(c) > 0);
373		buf_clear(b, b->fmt, b->fl);
374	}
375	chn_dmawakeup(c);
376    	if (c->flags & CHN_F_TRIGGERED) {
377		chn_dmaupdate(c);
378		/*
379	 	 * check if we need to reprogram the DMA on the sound card.
380	 	 * This happens if the size has changed from zero
381	 	 */
382		if (b->dl == 0) {
383			/* Start DMA operation */
384	    		b->dl = b->blksz; /* record new transfer size */
385	    		chn_trigger(c, PCMTRIG_START);
386		}
387 		/*
388 		 * Emulate writing by DMA, i.e. transfer the pcm data from
389 		 * the emulated-DMA buffer to the device itself.
390 		 */
391 		chn_trigger(c, PCMTRIG_EMLDMAWR);
392		if (b->rl < b->dl) {
393			DEB(printf("near underflow (%d < %d), %d\n", b->rl, b->dl, b->fl));
394			/*
395			 * we are near to underflow condition, so to prevent
396			 * audio 'clicks' clear next b->fl bytes
397			 */
398			buf_clear(b, b->fmt, b->fl);
399			if (b->rl < DMA_ALIGN_THRESHOLD)
400				b->underflow = 1;
401		}
402    	} else {
403		/* cannot start a new dma transfer */
404		DEB(printf("underflow, flags 0x%08x rp %d rl %d\n", c->flags, b->rp, b->rl));
405		if (b->dl) { /* DMA was active */
406			b->underflow = 1; /* set underflow flag */
407			buf_clear(b, b->fmt, b->bufsize);
408		}
409    	}
410}
411
412/*
413 * user write routine
414 *
415 * advance the boundary between READY and FREE, fill the space with
416 * uiomove(), and possibly start DMA. Do the above until the transfer
417 * is complete.
418 *
419 * To minimize latency in case a pending DMA transfer is about to end,
420 * we do the transfer in pieces of increasing sizes, extending the
421 * READY area at every checkpoint. In the (necessary) assumption that
422 * memory bandwidth is larger than the rate at which the dma consumes
423 * data, we reduce the latency to something proportional to the length
424 * of the first piece, while keeping the overhead low and being able
425 * to feed the DMA with large blocks.
426 */
427
428int
429chn_write(pcm_channel *c, struct uio *buf)
430{
431	int 		ret = 0, timeout, res, newsize;
432	long		s;
433	snd_dbuf       *b = &c->buffer;
434	snd_dbuf       *bs = &c->buffer2nd;
435
436	if (c->flags & CHN_F_WRITING) {
437		/* This shouldn't happen and is actually silly
438		 * - will never wake up, just timeout; why not sleep on b?
439		 */
440	       	tsleep(&s, PZERO, "pcmwrW", hz);
441		return EBUSY;
442	}
443	c->flags |= CHN_F_WRITING;
444	c->flags &= ~CHN_F_ABORTING;
445	s = spltty();
446
447	/*
448	 * XXX Certain applications attempt to write larger size
449	 * of pcm data than c->blocksize2nd without blocking,
450	 * resulting partial write. Expand the block size so that
451	 * the write operation avoids blocking.
452	 */
453	if ((c->flags & CHN_F_NBIO) && buf->uio_resid > bs->blksz) {
454		DEB(printf("pcm warning: broken app, nbio and tried to write %d bytes with fragsz %d\n",
455			buf->uio_resid, bs->blksz));
456		newsize = 16;
457		while (newsize < min(buf->uio_resid, CHN_2NDBUFMAXSIZE / 2))
458			newsize <<= 1;
459		chn_setblocksize(c, bs->blkcnt, newsize);
460		DEB(printf("pcm warning: frags reset to %d x %d\n", bs->blkcnt, bs->blksz));
461	}
462
463	/* Store the initial size in the uio. */
464	res = buf->uio_resid;
465
466	/*
467	 * Fill up the secondary and DMA buffer.
468	 * chn_wrfeed*() takes care of the alignment.
469	 */
470
471	/* Check for underflow before writing into the buffers. */
472	chn_checkunderflow(c);
473  	while (chn_wrfeed2nd(c, buf) > 0);
474   	if ((c->flags & CHN_F_NBIO) && (buf->uio_resid > 0))
475		ret = EAGAIN;
476
477	/* Start playing if not yet. */
478	if (!b->dl)
479		chn_start(c);
480
481	if (ret == 0) {
482		/* Wait until all samples are played in blocking mode. */
483   		while (buf->uio_resid > 0) {
484			/* Check for underflow before writing into the buffers. */
485			chn_checkunderflow(c);
486			/* Fill up the buffers with new pcm data. */
487  			while (chn_wrfeed2nd(c, buf) > 0);
488
489			/* Have we finished to feed the secondary buffer? */
490			if (buf->uio_resid == 0)
491				break;
492
493			/* Wait for new free space to write new pcm samples. */
494			/* splx(s); */
495			timeout = 1; /*(buf->uio_resid >= b->dl)? hz / 20 : 1; */
496   			ret = tsleep(b, PRIBIO | PCATCH, "pcmwr", timeout);
497   			/* s = spltty(); */
498 			/* if (ret == EINTR) chn_abort(c); */
499 			if (ret == EINTR || ret == ERESTART)
500				break;
501 		}
502	} else
503		ret = 0;
504	c->flags &= ~CHN_F_WRITING;
505   	splx(s);
506	return ret;
507}
508
509/*
510 * SOUND INPUT
511 *
512
513The input part is similar to the output one, with a circular buffer
514split in two regions, and boundaries advancing because of read() calls
515[r] or dma operation [d].  At initialization, as for the write
516routine, READY is empty, and FREE takes all the space.
517
518      0          rp,rl        fp,fl    bufsize
519      |__________>____________>________|
520	  FREE   r   READY    d  FREE
521
522Operation is as follows: upon user read (dsp_read_body()) a DMA read
523is started if not already active (marked by b->dl > 0),
524then as soon as data are available in the READY region they are
525transferred to the user buffer, thus advancing the boundary between FREE
526and READY. Upon interrupts, caused by a completion of a DMA transfer,
527the READY region is extended and possibly a new transfer is started.
528
529When necessary, dsp_rd_dmaupdate() is called to advance fp (and update
530rl,fl accordingly). Upon user reads, rp is advanced and rl,fl are
531updated accordingly.
532
533The rules to choose the size of the new DMA area are similar to
534the other case, with a preferred constant transfer size equal to
535rec_blocksize, and fallback to smaller sizes if no space is available.
536
537 */
538
539static int
540chn_rddump(pcm_channel *c, int cnt)
541{
542    	snd_dbuf *b = &c->buffer;
543
544	printf("overrun, dumping %d bytes\n", cnt);
545	b->rl -= cnt;
546	b->fl += cnt;
547	b->rp = (b->rp + cnt) % b->bufsize;
548	return cnt;
549}
550
551/*
552 * Feed new data from the read buffer. Can be called in the bottom half.
553 * Hence must be called at spltty.
554 */
555int
556chn_rdfeed(pcm_channel *c)
557{
558    	snd_dbuf *b = &c->buffer;
559    	snd_dbuf *bs = &c->buffer2nd;
560	int l, lacc;
561
562	/*
563	printf("b: [rl: %d, rp %d, fl %d, fp %d]; bs: [rl: %d, rp %d, fl %d, fp %d]\n",
564		b->rl, b->rp, b->fl, b->fp, bs->rl, bs->rp, bs->fl, bs->fp);
565	 */
566	/* ensure we always have a whole number of samples */
567	lacc = 0;
568	while (bs->fl >= DMA_ALIGN_THRESHOLD && b->rl >= DMA_ALIGN_THRESHOLD) {
569		l = min(min(bs->fl, bs->bufsize - bs->fp), min(b->rl, b->bufsize - b->rp)) & DMA_ALIGN_MASK;
570		/* Move the samples, update the markers and pointers. */
571		bcopy(b->buf + b->rp, bs->buf + bs->fp, l);
572		bs->fl -= l;
573		bs->rl += l;
574		bs->fp = (bs->fp + l) % bs->bufsize;
575		b->rl -= l;
576		b->fl += l;
577		b->rp = (b->rp + l) % b->bufsize;
578		/* Accumulate the total bytes of the moved samples. */
579		lacc += l;
580		/* A feed from the DMA buffer is equivalent to an interrupt. */
581		bs->int_count++;
582		if (bs->sel.si_pid && chn_polltrigger(c))
583			selwakeup(&bs->sel);
584	}
585
586	return lacc;
587}
588
589/* Feeds new data from the secondary read buffer. */
590static int
591chn_rdfeed2nd(pcm_channel *c, struct uio *buf)
592{
593    	snd_dbuf *bs = &c->buffer2nd;
594	int l, w, wacc;
595
596	/* ensure we always have a whole number of samples */
597	wacc = 0;
598	while ((buf->uio_resid > 0) && (bs->rl > 0)) {
599		/* The DMA buffer may have pcm data. */
600		/* while (chn_rdfeed(c) > 0); */
601		/*
602		 * The size of the data to move here does not have to be
603		 * aligned. We take care of it upon moving the data to a
604		 * DMA buffer.
605		 */
606		l = min(bs->rl, bs->bufsize - bs->rp);
607		/* Move the samples, update the markers and pointers. */
608		w = c->feeder->feed(c->feeder, c, bs->buf + bs->rp, l, buf);
609		if (w == 0)
610			panic("no feed");
611		bs->fl += w;
612		bs->rl -= w;
613		bs->rp = (bs->rp + w) % bs->bufsize;
614		/* Clear the new space in the secondary buffer. */
615		buf_clear(bs, bs->fmt, l);
616		/* Accumulate the total bytes of the moved samples. */
617		bs->total += w;
618		wacc += w;
619	}
620
621	return wacc;
622}
623
624/* read interrupt routine. Must be called with interrupts blocked. */
625static void
626chn_rdintr(pcm_channel *c)
627{
628    	snd_dbuf *b = &c->buffer;
629
630    	if (b->dl) chn_dmadone(c);
631
632    	DEB(printf("rdintr: start dl %d, rp:rl %d:%d, fp:fl %d:%d\n",
633		b->dl, b->rp, b->rl, b->fp, b->fl));
634
635	/* Update the pointers. */
636	chn_dmaupdate(c);
637
638	/*
639	 * Suck up the DMA buffer, followed by waking up the top half.
640	 * If some of the pcm data in the secondary buffer are still left,
641	 * the top half goes to sleep by itself.
642	 */
643	while(chn_rdfeed(c) > 0);
644	chn_dmawakeup(c);
645
646	if (b->fl < b->dl) {
647		DEB(printf("near overflow (%d < %d), %d\n", b->fl, b->dl, b->rl));
648		chn_rddump(c, b->blksz - b->fl);
649	}
650
651	if (c->flags & CHN_F_TRIGGERED) {
652		/*
653	 	 * check if we need to reprogram the DMA on the sound card.
654	 	 * This happens if the size has changed from zero
655	 	 */
656		if (b->dl == 0) {
657			/* Start DMA operation */
658	    		b->dl = b->blksz; /* record new transfer size */
659	    		chn_trigger(c, PCMTRIG_START);
660		}
661 		/*
662 		 * Emulate writing by DMA, i.e. transfer the pcm data from
663 		 * the emulated-DMA buffer to the device itself.
664 		 */
665 		chn_trigger(c, PCMTRIG_EMLDMARD);
666    	} else {
667		if (b->dl) { /* was active */
668	    		b->dl = 0;
669	    		chn_trigger(c, PCMTRIG_STOP);
670	    		chn_dmaupdate(c);
671		}
672    	}
673}
674
675/*
676 * body of user-read routine
677 *
678 * Start DMA if not active; wait for READY not empty.
679 * Transfer data from READY region using uiomove(), advance boundary
680 * between FREE and READY. Repeat until transfer is complete.
681 *
682 * To avoid excessive latency in freeing up space for the DMA
683 * engine, transfers are done in blocks of increasing size, so that
684 * the latency is proportional to the size of the smallest block, but
685 * we have a low overhead and are able to feed the dma engine with
686 * large blocks.
687 *
688 * NOTE: in the current version, read will not return more than
689 * blocksize bytes at once (unless more are already available), to
690 * avoid that requests using very large buffers block for too long.
691 */
692
693int
694chn_read(pcm_channel *c, struct uio *buf)
695{
696	int		ret = 0, timeout, limit, res;
697	long		s;
698	snd_dbuf       *b = &c->buffer;
699	snd_dbuf       *bs = &c->buffer2nd;
700
701	if (c->flags & CHN_F_READING) {
702		/* This shouldn't happen and is actually silly */
703		tsleep(&s, PZERO, "pcmrdR", hz);
704		return (EBUSY);
705	}
706
707  	s = spltty();
708
709	/* Store the initial size in the uio. */
710	res = buf->uio_resid;
711
712	c->flags |= CHN_F_READING;
713	c->flags &= ~CHN_F_ABORTING;
714
715	/* suck up the DMA and secondary buffers. */
716 	while (chn_rdfeed2nd(c, buf) > 0);
717
718	if (buf->uio_resid == 0)
719		goto skip;
720
721	limit = res - b->blksz;
722	if (limit < 0)
723		limit = 0;
724
725	/* Start capturing if not yet. */
726  	if ((!bs->rl || !b->rl) && !b->dl)
727		chn_start(c);
728
729  	if (!(c->flags & CHN_F_NBIO)) {
730  		/* Wait until all samples are captured. */
731  		while (buf->uio_resid > 0) {
732			/* Suck up the DMA and secondary buffers. */
733			chn_dmaupdate(c);
734			while (chn_rdfeed(c) > 0);
735 			while (chn_rdfeed2nd(c, buf) > 0);
736
737			/* Have we finished to feed the uio? */
738			if (buf->uio_resid == 0)
739				break;
740
741			/* Wait for new pcm samples. */
742			/* splx(s); */
743			timeout = (buf->uio_resid - limit >= b->dl)? hz / 20 : 1;
744  			ret = tsleep(b, PRIBIO | PCATCH, "pcmrd", 1);
745  			/* s = spltty(); */
746			/* if (ret == EINTR) chn_abort(c); */
747			if (ret == EINTR || ret == ERESTART)
748				break;
749		}
750	} else {
751		/* If no pcm data was read on nonblocking, return EAGAIN. */
752		if (buf->uio_resid == res)
753			ret = EAGAIN;
754	}
755
756skip:
757	c->flags &= ~CHN_F_READING;
758  	splx(s);
759	return ret;
760}
761
762void
763chn_intr(pcm_channel *c)
764{
765	if (c->flags & CHN_F_INIT)
766		chn_reinit(c);
767	if (c->direction == PCMDIR_PLAY)
768		chn_wrintr(c);
769	else
770		chn_rdintr(c);
771}
772
773u_int32_t
774chn_start(pcm_channel *c)
775{
776	u_int32_t r, s;
777	snd_dbuf *b = &c->buffer;
778
779	r = 0;
780	s = spltty();
781    	if (b->dl == 0 && !(c->flags & (CHN_F_MAPPED | CHN_F_NOTRIGGER))) {
782		if (c->direction == PCMDIR_PLAY) {
783			/* Fill up the DMA buffer. */
784			while (chn_wrfeed(c) > 0);
785			if (b->rl >= b->blksz)
786				r = CHN_F_TRIGGERED;
787		} else {
788			/* Suck up the DMA buffer. */
789			while (chn_rdfeed(c) > 0);
790			if (b->fl >= b->blksz)
791				r = CHN_F_TRIGGERED;
792		}
793		c->flags |= r;
794		chn_intr(c);
795	}
796	splx(s);
797	return r;
798}
799
800static void
801chn_dma_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
802{
803	snd_dbuf *b = (snd_dbuf *)arg;
804
805	if (bootverbose) {
806		printf("pcm: setmap %lx, %lx; ", (unsigned long)segs->ds_addr,
807		       (unsigned long)segs->ds_len);
808		printf("%p -> %lx\n", b->buf, (unsigned long)vtophys(b->buf));
809	}
810}
811
812/*
813 * Allocate memory for DMA buffer. If the device do not perform DMA transfer,
814 * the drvier can call malloc(9) by its own.
815 */
816int
817chn_allocbuf(snd_dbuf *b, bus_dma_tag_t parent_dmat)
818{
819	if (bus_dmamem_alloc(parent_dmat, (void **)&b->buf,
820			     BUS_DMA_NOWAIT, &b->dmamap)) return -1;
821	if (bus_dmamap_load(parent_dmat, b->dmamap, b->buf,
822			    b->bufsize, chn_dma_setmap, b, 0)) return -1;
823	return 0;
824}
825
826static void
827buf_clear(snd_dbuf *b, u_int32_t fmt, int length)
828{
829	int i;
830	u_int16_t data, *p;
831
832	if (length == 0)
833		return;
834
835	if (fmt & AFMT_SIGNED)
836		data = 0x00;
837	else
838		data = 0x80;
839
840	if (fmt & AFMT_16BIT)
841		data <<= 8;
842	else
843		data |= data << 8;
844
845	if (fmt & AFMT_BIGENDIAN)
846		data = ((data >> 8) & 0x00ff) | ((data << 8) & 0xff00);
847
848	i = b->fp;
849	p = (u_int16_t *)(b->buf + b->fp);
850	while (length > 1) {
851		*p++ = data;
852		length -= 2;
853		i += 2;
854		if (i >= b->bufsize) {
855			p = (u_int16_t *)b->buf;
856			i = 0;
857		}
858	}
859	if (length == 1)
860		*(b->buf + i) = data & 0xff;
861}
862
863void
864chn_resetbuf(pcm_channel *c)
865{
866	snd_dbuf *b = &c->buffer;
867	snd_dbuf *bs = &c->buffer2nd;
868
869	c->blocks = 0;
870	b->rp = b->fp = 0;
871	b->dl = b->rl = 0;
872	b->fl = b->bufsize;
873	b->prev_total = b->total = 0;
874	b->prev_int_count = b->int_count = 0;
875	b->underflow = 0;
876	if (b->buf && b->bufsize > 0)
877		buf_clear(b, b->fmt, b->bufsize);
878
879	bs->rp = bs->fp = 0;
880	bs->dl = bs->rl = 0;
881	bs->fl = bs->bufsize;
882	bs->prev_total = bs->total = 0;
883	bs->prev_int_count = bs->int_count = 0;
884	bs->underflow = 0;
885	if (bs->buf && bs->bufsize > 0)
886		buf_clear(bs, bs->fmt, bs->bufsize);
887}
888
889void
890buf_isadma(snd_dbuf *b, int go)
891{
892	if (ISA_DMA(b)) {
893		switch (go) {
894		case PCMTRIG_START:
895			DEB(printf("buf 0x%p ISA DMA started\n", b));
896			isa_dmastart(b->dir | ISADMA_RAW, b->buf,
897					b->bufsize, b->chan);
898			break;
899		case PCMTRIG_STOP:
900		case PCMTRIG_ABORT:
901			DEB(printf("buf 0x%p ISA DMA stopped\n", b));
902			isa_dmastop(b->chan);
903			isa_dmadone(b->dir | ISADMA_RAW, b->buf, b->bufsize,
904				    b->chan);
905			break;
906		}
907    	} else KASSERT(1, ("buf_isadma called on invalid channel"));
908}
909
910int
911buf_isadmaptr(snd_dbuf *b)
912{
913	if (ISA_DMA(b)) {
914		int i = b->dl? isa_dmastatus(b->chan) : b->bufsize;
915		if (i < 0)
916			i = 0;
917		return b->bufsize - i;
918    	} else KASSERT(1, ("buf_isadmaptr called on invalid channel"));
919	return -1;
920}
921
922/*
923 * chn_sync waits until the space in the given channel goes above
924 * a threshold. The threshold is checked against fl or rl respectively.
925 * Assume that the condition can become true, do not check here...
926 */
927int
928chn_sync(pcm_channel *c, int threshold)
929{
930    	u_long s, rdy;
931    	int ret;
932    	snd_dbuf *b = &c->buffer;
933    	snd_dbuf *bs = &c->buffer2nd;
934
935    	for (;;) {
936		s = spltty();
937		chn_checkunderflow(c);
938		while (chn_wrfeed(c) > 0);
939		rdy = (c->direction == PCMDIR_PLAY)? bs->fl : bs->rl;
940		if (rdy <= threshold) {
941	    		ret = tsleep((caddr_t)b, PRIBIO | PCATCH, "pcmsyn", 1);
942	    		splx(s);
943	    		if (ret == ERESTART || ret == EINTR) {
944				DEB(printf("chn_sync: tsleep returns %d\n", ret));
945				return -1;
946	    		}
947		} else break;
948    	}
949    	splx(s);
950    	return 0;
951}
952
953int
954chn_poll(pcm_channel *c, int ev, struct proc *p)
955{
956	snd_dbuf *b = &c->buffer;
957	snd_dbuf *bs = &c->buffer2nd;
958	u_long s;
959	int ret;
960
961	s = spltty();
962    	if (!(c->flags & CHN_F_MAPPED)) {
963		if (c->direction == PCMDIR_PLAY) {
964			/* Fill up the DMA buffer. */
965			chn_checkunderflow(c);
966			while (chn_wrfeed(c) > 0);
967		} else {
968			/* Suck up the DMA buffer. */
969			chn_dmaupdate(c);
970			while (chn_rdfeed(c) > 0);
971		}
972		if (!b->dl)
973			chn_start(c);
974	}
975	ret = 0;
976	if (chn_polltrigger(c) && chn_pollreset(c))
977		ret = ev;
978	else
979		selrecord(p, &bs->sel);
980	splx(s);
981	return ret;
982}
983
984/*
985 * chn_abort is a non-blocking function which aborts a pending
986 * DMA transfer and flushes the buffers.
987 * It returns the number of bytes that have not been transferred.
988 */
989int
990chn_abort(pcm_channel *c)
991{
992    	int missing = 0, cnt = 0;
993    	snd_dbuf *b = &c->buffer;
994    	snd_dbuf *bs = &c->buffer2nd;
995
996	if (!b->dl)
997		return 0;
998	c->flags |= CHN_F_ABORTING;
999	c->flags &= ~CHN_F_TRIGGERED;
1000	cnt = 10;
1001	while (!b->underflow && (cnt-- > 0))
1002		tsleep((caddr_t)b, PRIBIO | PCATCH, "pcmabr", hz / 50);
1003	chn_trigger(c, PCMTRIG_ABORT);
1004	b->dl = 0;
1005	chn_dmaupdate(c);
1006    	missing = bs->rl + b->rl;
1007    	return missing;
1008}
1009
1010/*
1011 * this routine tries to flush the dma transfer. It is called
1012 * on a close. We immediately abort any read DMA
1013 * operation, and then wait for the play buffer to drain.
1014 */
1015
1016int
1017chn_flush(pcm_channel *c)
1018{
1019    	int ret, count, s, resid, resid_p;
1020    	snd_dbuf *b = &c->buffer;
1021    	snd_dbuf *bs = &c->buffer2nd;
1022
1023    	DEB(printf("chn_flush c->flags 0x%08x\n", c->flags));
1024    	c->flags |= CHN_F_CLOSING;
1025    	if (c->direction == PCMDIR_REC)
1026		chn_abort(c);
1027    	else if (b->dl) {
1028		resid_p = resid = b->rl + bs->rl;
1029		count = 10;
1030		while ((count > 0) && (resid > 0) && !b->underflow) {
1031			/* still pending output data. */
1032			ret = tsleep((caddr_t)b, PRIBIO | PCATCH, "pcmflu", hz / 10);
1033			if (ret == EINTR || ret == ERESTART) {
1034	    			DEB(printf("chn_flush: tsleep returns %d\n", ret));
1035	    			return ret;
1036			}
1037 			s = spltty();
1038			chn_dmaupdate(c);
1039			splx(s);
1040			DEB(printf("chn_flush: now rl = %d, fl = %d\n", b->rl, b->fl));
1041			resid = b->rl + bs->rl;
1042			if (resid >= resid_p)
1043				count--;
1044			resid_p = resid;
1045   		}
1046		if (count == 0)
1047			DEB(printf("chn_flush: timeout flushing dbuf_out, cnt 0x%x flags 0x%x\n", b->rl, c->flags));
1048    		if (c->direction == PCMDIR_PLAY && b->dl)
1049			chn_abort(c);
1050	}
1051    	c->flags &= ~CHN_F_CLOSING;
1052    	return 0;
1053}
1054
1055int
1056chn_reset(pcm_channel *c, u_int32_t fmt)
1057{
1058	int r = 0;
1059
1060	chn_abort(c);
1061	c->flags &= CHN_F_RESET;
1062	r = chn_setblocksize(c, CHN_2NDBUFBLKNUM, CHN_2NDBUFBLKSIZE);
1063	if (r)
1064		return r;
1065	if (fmt) {
1066		r = chn_setformat(c, fmt);
1067		if (r == 0)
1068			r = chn_setspeed(c, DSP_DEFAULT_SPEED);
1069		if (r == 0)
1070			r = chn_setvolume(c, 100, 100);
1071	}
1072	chn_resetbuf(c);
1073	/* c->flags |= CHN_F_INIT; */
1074	return 0;
1075}
1076
1077int
1078chn_reinit(pcm_channel *c)
1079{
1080	if ((c->flags & CHN_F_INIT) && CANCHANGE(c)) {
1081		chn_setformat(c, c->format);
1082		chn_setspeed(c, c->speed);
1083		chn_setvolume(c, (c->volume >> 8) & 0xff, c->volume & 0xff);
1084		c->flags &= ~CHN_F_INIT;
1085		return 1;
1086	}
1087	return 0;
1088}
1089
1090int
1091chn_init(pcm_channel *c, void *devinfo, int dir)
1092{
1093	snd_dbuf       *bs = &c->buffer2nd;
1094
1095	/* Initialize the hardware and DMA buffer first. */
1096	c->flags = 0;
1097	c->feeder = &feeder_root;
1098	c->buffer.chan = -1;
1099	c->devinfo = c->init(devinfo, &c->buffer, c, dir);
1100	if (c->devinfo == NULL || c->buffer.bufsize == 0)
1101		return 1;
1102	chn_setdir(c, dir);
1103
1104	/* And the secondary buffer. */
1105	bs->buf = NULL;
1106	bs->bufsize = 0;
1107	return 0;
1108}
1109
1110int
1111chn_setdir(pcm_channel *c, int dir)
1112{
1113	int r;
1114
1115	c->direction = dir;
1116	r = c->setdir(c->devinfo, c->direction);
1117	if (!r && ISA_DMA(&c->buffer))
1118		c->buffer.dir = (dir == PCMDIR_PLAY)? ISADMA_WRITE : ISADMA_READ;
1119	return r;
1120}
1121
1122int
1123chn_setvolume(pcm_channel *c, int left, int right)
1124{
1125	/* could add a feeder for volume changing if channel returns -1 */
1126	if (CANCHANGE(c)) {
1127		c->volume = (left << 8) | right;
1128		return 0;
1129	}
1130	c->volume = (left << 8) | right;
1131	c->flags |= CHN_F_INIT;
1132	return 0;
1133}
1134
1135int
1136chn_setspeed(pcm_channel *c, int speed)
1137{
1138	if (speed <= 0)
1139		return EINVAL;
1140	/* could add a feeder for rate conversion */
1141	if (CANCHANGE(c)) {
1142		c->speed = c->setspeed(c->devinfo, speed);
1143		return 0;
1144	}
1145	c->speed = speed;
1146	c->flags |= CHN_F_INIT;
1147	return 0;
1148}
1149
1150int
1151chn_setformat(pcm_channel *c, u_int32_t fmt)
1152{
1153	snd_dbuf *b = &c->buffer;
1154	snd_dbuf *bs = &c->buffer2nd;
1155
1156	u_int32_t hwfmt;
1157	if (CANCHANGE(c)) {
1158		c->format = fmt;
1159		hwfmt = chn_feedchain(c);
1160		if ((c->flags & CHN_F_MAPPED) && c->format != hwfmt)
1161			return EINVAL;
1162		b->fmt = hwfmt;
1163		bs->fmt = hwfmt;
1164		chn_resetbuf(c);
1165		c->setformat(c->devinfo, hwfmt);
1166		return 0;
1167	}
1168	c->format = fmt;
1169	c->flags |= CHN_F_INIT;
1170	return 0;
1171}
1172
1173int
1174chn_setblocksize(pcm_channel *c, int blkcnt, int blksz)
1175{
1176	snd_dbuf *b = &c->buffer;
1177	snd_dbuf *bs = &c->buffer2nd;
1178	int s, ss, bufsz;
1179
1180	if (bs->blkcnt == blkcnt && bs->blksz == blksz)
1181		return 0;
1182    	if (c->flags & CHN_F_MAPPED) {
1183		DEB(printf("chn_setblocksize: can't work on mapped channel"));
1184		return EINVAL;
1185	}
1186	c->flags &= ~CHN_F_HAS_SIZE;
1187
1188	ss = 1;
1189	ss <<= (bs->fmt & AFMT_STEREO)? 1 : 0;
1190	ss <<= (bs->fmt & AFMT_16BIT)? 1 : 0;
1191
1192	if (blksz >= 2)
1193		c->flags |= CHN_F_HAS_SIZE;
1194	/* let us specify blksz without setting CHN_F_HAS_SIZE */
1195	if (blksz < 0)
1196		blksz = -blksz;
1197	/* default to blksz = ~0.25s */
1198	if (blksz < 16)
1199		blksz = (ss * c->speed) >> 2;
1200	if (blksz > CHN_2NDBUFMAXSIZE / 2)
1201		blksz = CHN_2NDBUFMAXSIZE / 2;
1202	if (blkcnt < 2)
1203		blkcnt = 2;
1204
1205	if (blkcnt * blksz > CHN_2NDBUFMAXSIZE)
1206		blkcnt = CHN_2NDBUFMAXSIZE / blksz;
1207	bufsz = blkcnt * blksz;
1208
1209	s = spltty();
1210	if (bs->buf != NULL)
1211		free(bs->buf, M_DEVBUF);
1212	bs->buf = malloc(bufsz, M_DEVBUF, M_WAITOK);
1213	if (bs->buf == NULL) {
1214      		splx(s);
1215		DEB(printf("chn_setblocksize: out of memory."));
1216		return ENOSPC;
1217	}
1218	bs->bufsize = bufsz;
1219	bs->rl = bs->rp = bs->fp = 0;
1220	bs->fl = bs->bufsize;
1221	buf_clear(bs, bs->fmt, bs->bufsize);
1222	bs->blkcnt = blkcnt;
1223	bs->blksz = blksz;
1224	RANGE(blksz, 16, b->bufsize / 2);
1225	b->blksz = c->setblocksize(c->devinfo, blksz);
1226	splx(s);
1227
1228	return 0;
1229}
1230
1231int
1232chn_trigger(pcm_channel *c, int go)
1233{
1234	return c->trigger(c->devinfo, go);
1235}
1236
1237int
1238chn_getptr(pcm_channel *c)
1239{
1240	int hwptr;
1241	int a = (1 << c->align) - 1;
1242	snd_dbuf *b = &c->buffer;
1243
1244	hwptr = b->dl? c->getptr(c->devinfo) : 0;
1245	/* don't allow unaligned values in the hwa ptr */
1246	hwptr &= ~a ; /* Apply channel align mask */
1247	hwptr &= DMA_ALIGN_MASK; /* Apply DMA align mask */
1248	return hwptr;
1249}
1250
1251pcmchan_caps *
1252chn_getcaps(pcm_channel *c)
1253{
1254	return c->getcaps(c->devinfo);
1255}
1256