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