⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dmabuf.c

📁 freebsd v4.4内核源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * snd/dmabuf.c *  * This file implements the new DMA routines for the sound driver. * AUTO DMA MODE (ISA DMA SIDE). * * Copyright by Luigi Rizzo - 1997 *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer *    in the documentation and/or other materials provided with the *    distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * */#include <i386/isa/snd/sound.h>#include <i386/isa/sound/ulaw.h>#define MIN_CHUNK_SIZE 256	/* for uiomove etc. */#define	DMA_ALIGN_THRESHOLD	4#define	DMA_ALIGN_MASK		(~ (DMA_ALIGN_THRESHOLD - 1))static void dsp_wr_dmadone(snddev_info *d);static void dsp_rd_dmadone(snddev_info *d);/* * SOUND OUTPUTWe use a circular buffer to store samples directed to the DAC.The buffer is split into two variable-size regions, each identifiedby an offset in the buffer (rp,fp) and a length (rl,fl):      0          rp,rl        fp,fl    bufsize      |__________>____________>________|	  FREE   d   READY    w FREE      READY: data written from the process and ready to be sent to the DAC;  FREE: free part of the buffer.Both regions can wrap around the end of the buffer.  At initialization,READY is empty, FREE takes all the available space, and dma isidle.  dl contains the length of the current DMA transfer, dl=0means that the dma is idle.The two boundaries (rp,fp) in the buffers are advanced by DMA [d]and write() [w] operations. The first portion of the READY regionis used for DMA transfers. The transfer is started at rp and withchunks of length dl. During DMA operations, dsp_wr_dmaupdate()updates rp, rl and fl tracking the ISA DMA engine as the transfermakes progress.When a new block is written, fp advances and rl,fl are updatedaccordingly.The code works as follows: the user write routine dsp_write_body()fills up the READY region with new data (reclaiming space from theFREE region) and starts the write DMA engine if inactive.  When aDMA transfer is complete, an interrupt causes dsp_wrintr() to becalled which extends the FREE region and possibly starts the nexttransfer.In some cases, the code tries to track the current status of DMAoperations by calling dsp_wr_dmaupdate() which changes rp, rl and fl.The sistem tries to make all DMA transfers use the same size,play_blocksize or rec_blocksize. The size is either selected bythe user, or computed by the system to correspond to about .25s ofaudio. The blocksize must be within a range which is currently:	min(5ms, 40 bytes) ... 1/2 buffer size.When there aren't enough data (write) or space (read), a transferis started with a reduced size.To reduce problems in case of overruns, the routine which fills upthe buffer should initialize (e.g. by repeating the last value) areasonably long area after the last block so that no noise isproduced on overruns.  *  *//* * dsp_wr_dmadone() updates pointers and wakes up any process sleeping * or waiting on a select(). * Must be called at spltty(). */static voiddsp_wr_dmadone(snddev_info *d){    snd_dbuf *b = & (d->dbuf_out) ;    dsp_wr_dmaupdate(b);    /*     * XXX here it would be more efficient to record if there     * actually is a sleeping process, but this should still work.     */    wakeup(b); 	/* wakeup possible sleepers */    if (b->sel.si_pid &&	    ( !(d->flags & SND_F_HAS_SIZE) || b->fl >= d->play_blocksize ) )	selwakeup( & b->sel );}/* * dsp_wr_dmaupdate() tracks the status of a (write) dma transfer, * updating pointers. It must be called at spltty() and the ISA DMA must * have been started. * * NOTE: when we are using auto dma in the device, rl might become *  negative. */voiddsp_wr_dmaupdate(snd_dbuf *b){    int tmp, delta;    tmp = b->bufsize - isa_dmastatus1(b->chan) ;    tmp &= DMA_ALIGN_MASK; /* align... */    delta = tmp - b->rp;    if (delta < 0) /* wrapped */	delta += b->bufsize ;    b->rp = tmp;    b->rl -= delta ;    b->fl += delta ;    b->total += delta ;}/* * Write interrupt routine. Can be called from other places (e.g. * to start a paused transfer), but with interrupts disabled. */voiddsp_wrintr(snddev_info *d){    snd_dbuf *b = & (d->dbuf_out) ;    if (b->dl) {		/* dma was active */	b->int_count++;	dsp_wr_dmadone(d);    }    DEB(if (b->rl < 0)	printf("dsp_wrintr: dl %d, rp:rl %d:%d, fp:fl %d:%d\n",	    b->dl, b->rp, b->rl, b->fp, b->fl));    /*     * start another dma operation only if have ready data in the buffer,     * there is no pending abort, have a full-duplex device, or have a     * half duplex device and there is no pending op on the other side.     *     * Force transfers to be aligned to a boundary of 4, which is     * needed when doing stereo and 16-bit. We could make this     * adaptive, but why bother for now...     */    if (  b->rl >= DMA_ALIGN_THRESHOLD  &&	  ! (d->flags & SND_F_ABORTING) &&	  ( FULL_DUPLEX(d) || ! (d->flags & SND_F_READING)  ) ) {	int l = min(b->rl, d->play_blocksize );	/* avoid too large transfer */	l &= DMA_ALIGN_MASK ; /* realign things */        DEB(printf("dsp_wrintr: dl %d -> %d\n", b->dl, l);)	/*	 * check if we need to reprogram the DMA on the sound card.	 * This happens if the size has changed _and_ the new size	 * is smaller, or it matches the blocksize.	 */	if (l != b->dl && (b->dl == 0 || l < b->dl || l == d->play_blocksize) ) {	    /* for any reason, size has changed. Stop and restart */	    DEB(printf("wrintr: bsz change from %d to %d, rp %d rl %d\n",		b->dl, l, b->rp, b->rl));	    if (b->dl != 0)		d->callback(d, SND_CB_WR | SND_CB_STOP );	    /*	     * at high speed, it might well be that the count	     * changes in the meantime. So we try to update b->rl	     */	    dsp_wr_dmaupdate(b) ;	    l = min(b->rl, d->play_blocksize );	    l &= DMA_ALIGN_MASK ; /* realign things */	    b->dl = l; /* record previous transfer size */	    d->callback(d, SND_CB_WR | SND_CB_START );	}    } else {	/* cannot start a new dma transfer */	DEB(printf("cannot start wr-dma flags 0x%08x rp %d rl %d\n",		d->flags, b->rp, b->rl));	if (b->dl > 0) { /* was active */	    b->dl = 0;	    d->callback(d, SND_CB_WR | SND_CB_STOP );	/* stop dma */	    if (d->flags & SND_F_WRITING)		DEB(printf("Race! got wrint while reloading...\n"));	    else if (b->rl <= 0) /* XXX added 980110 lr */		reset_dbuf(b, SND_CHAN_WR);	}	/*	 * if switching to read, should start the read dma...	 */	if ( !FULL_DUPLEX(d) && (d->flags & SND_F_READING) )	    dsp_rdintr(d);    }}/* * user write routine * * advance the boundary between READY and FREE, fill the space with * uiomove(), and possibly start DMA. Do the above until the transfer * is complete. *  * To minimize latency in case a pending DMA transfer is about to end, * we do the transfer in pieces of increasing sizes, extending the * READY area at every checkpoint. In the (necessary) assumption that * memory bandwidth is larger than the rate at which the dma consumes * data, we reduce the latency to something proportional to the length * of the first piece, while keeping the overhead low and being able * to feed the DMA with large blocks. * * assume d->flags |= SND_F_WRITING ; has been done before */intdsp_write_body(snddev_info *d, struct uio *buf){    int n, l, bsz, ret = 0 ;    long s;    snd_dbuf *b = & (d->dbuf_out) ;    /*     * bsz is the max size for the next transfer. If the dma was idle     * (dl == 0), we want it as large as possible. Otherwise, start with     * a small block to avoid underruns if we are close to the end of     * the previous operation.     */    bsz =  b->dl ? MIN_CHUNK_SIZE : b->bufsize ;    while ( (n = buf->uio_resid) ) {        l = min (n, bsz);       /* at most n bytes ... */        s = spltty();  /* no interrupts here ... */	dsp_wr_dmaupdate(b);        l = min( l, b->fl );    /* no more than avail. space */	DEB(printf("dsp_write_body: prepare %d bytes out of %d\n", l,n));	/*	 * at this point, we assume that if l==0 the dma engine	 * must be running.	 */        if (l == 0) { /* no space, must sleep */	    int timeout;	    if (d->flags & SND_F_NBIO) {		/* unless of course we are doing non-blocking i/o */		splx(s);		break;	    }	    DEB(printf("dsp_write_body: l=0, (fl %d) sleeping\n", b->fl));	    if ( b->fl < n )		timeout = hz;	    else		timeout = 1 ;            ret = tsleep( (caddr_t)b, PRIBIO|PCATCH, "dspwr", timeout);	    if (ret == EINTR)		d->flags |= SND_F_ABORTING ;	    splx(s);	    if (ret == EINTR || ret == ERESTART)		break ;            continue;        }        splx(s);	/*	 * copy data to the buffer, and possibly do format	 * conversions (here, from ULAW to U8).	 * NOTE: I can use fp here since it is not modified by the	 * interrupt routines.	 */	if (b->fp + l > b->bufsize) {	    int l1 = b->bufsize - b->fp ;	    uiomove(b->buf + b->fp, l1, buf) ;	    uiomove(b->buf, l - l1, buf) ;	    if (d->flags & SND_F_XLAT8) {		translate_bytes(ulaw_dsp, b->buf + b->fp, l1);		translate_bytes(ulaw_dsp, b->buf , l - l1);	    }	} else {	    uiomove(b->buf + b->fp, l, buf) ;	    if (d->flags & SND_F_XLAT8)		translate_bytes(ulaw_dsp, b->buf + b->fp, l);	}        s = spltty();  /* no interrupts here ... */        b->rl += l ;    /* this more ready bytes */        b->fl -= l ;    /* this less free bytes */        b->fp += l ;        if (b->fp >= b->bufsize)        /* handle wraps */            b->fp -= b->bufsize ;        if ( b->dl == 0 ) /* dma was idle, restart it */            dsp_wrintr(d) ;        splx(s) ;	if (buf->uio_resid == 0 && (b->fp & (b->sample_size - 1)) == 0) {	    /*	     * If data is correctly aligned, pad the region with	     * replicas of the last sample. l0 goes from current to	     * the buffer end, l1 is the portion which wraps around.	     */	    int l0, l1, i;	    l1 = min(/* b->dl */ d->play_blocksize, b->fl);	    l0 = min (l1, b->bufsize - b->fp);	    l1 = l1 - l0 ;	    i = b->fp - b->sample_size;	    if (i < 0 ) i += b->bufsize ;	    if (b->sample_size == 1) {		u_char *p= (u_char *)(b->buf + i), sample = *p;				for ( ; l0 ; l0--)		    *p++ = sample ;		for (p= (u_char *)(b->buf) ; l1 ; l1--)		    *p++ = sample ;	    } else if (b->sample_size == 2) {		u_short *p= (u_short *)(b->buf + i), sample = *p;		l1 /= 2 ;		l0 /= 2 ;		for ( ; l0 ; l0--)		    *p++ = sample ;		for (p= (u_short *)(b->buf) ; l1 ; l1--)		    *p++ = sample ;	    } else { /* must be 4 ... */		u_long *p= (u_long *)(b->buf + i), sample = *p;		l1 /= 4 ;		l0 /= 4 ;		for ( ; l0 ; l0--)		    *p++ = sample ;		for (p= (u_long *)(b->buf) ; l1 ; l1--)		    *p++ = sample ;	    }	}        bsz = min(b->bufsize, bsz*2);    }    s = spltty();  /* no interrupts here ... */    d->flags &= ~SND_F_WRITING ;    if (d->flags & SND_F_ABORTING) {        d->flags &= ~SND_F_ABORTING;	splx(s);	dsp_wrabort(d, 1 /* restart */);	/* XXX return EINTR ? */    }    splx(s) ;    return ret ;}/* * SOUND INPUT *The input part is similar to the output one, with a circular buffersplit in two regions, and boundaries advancing because of read() calls[r] or dma operation [d].  At initialization, as for the writeroutine, READY is empty, and FREE takes all the space.      0          rp,rl        fp,fl    bufsize      |__________>____________>________|	  FREE   r   READY    d  FREE    Operation is as follows: upon user read (dsp_read_body()) a DMA readis started if not already active (marked by b->dl > 0),then as soon as data are available in the READY region they aretransferred to the user buffer, thus advancing the boundary between FREEand READY. Upon interrupts, caused by a completion of a DMA transfer,the READY region is extended and possibly a new transfer is started.When necessary, dsp_rd_dmaupdate() is called to advance fp (and updaterl,fl accordingly). Upon user reads, rp is advanced and rl,fl areupdated accordingly.The rules to choose the size of the new DMA area are similar to

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -