xref: /freebsd/sys/dev/sound/pcm/buffer.c (revision 1a2cdef4962b47be5057809ce730a733b7f3c27c)
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$
27  */
28 
29 #include <dev/sound/pcm/sound.h>
30 
31 #include "feeder_if.h"
32 
33 #define	MIN(x, y) (((x) < (y))? (x) : (y))
34 
35 #define SNDBUF_NAMELEN	48
36 struct snd_dbuf {
37         u_int8_t *buf, *tmpbuf;
38         unsigned int bufsize, maxsize;
39         volatile int dl; /* transfer size */
40         volatile int rp; /* pointers to the ready area */
41 	volatile int rl; /* length of ready area */
42 	volatile int hp;
43 	volatile u_int32_t total, prev_total;
44 	int isadmachan, dir;       /* dma channel */
45 	u_int32_t fmt, spd, bps;
46 	unsigned int blksz, blkcnt;
47 	int xrun;
48 	u_int32_t flags;
49 	bus_dmamap_t dmamap;
50 	bus_dma_tag_t dmatag;
51 	struct selinfo sel;
52 	char name[SNDBUF_NAMELEN];
53 };
54 
55 struct snd_dbuf *
56 sndbuf_create(char *drv, char *desc)
57 {
58 	struct snd_dbuf *b;
59 
60 	b = malloc(sizeof(*b), M_DEVBUF, M_WAITOK | M_ZERO);
61 	snprintf(b->name, SNDBUF_NAMELEN, "%s:%s", drv, desc);
62 	return b;
63 }
64 
65 void
66 sndbuf_destroy(struct snd_dbuf *b)
67 {
68 	free(b, M_DEVBUF);
69 }
70 
71 static void
72 sndbuf_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
73 {
74 	struct snd_dbuf *b = (struct snd_dbuf *)arg;
75 
76 	if (bootverbose) {
77 		printf("pcm: setmap %lx, %lx; ", (unsigned long)segs->ds_addr,
78 		       (unsigned long)segs->ds_len);
79 		printf("%p -> %lx\n", b->buf, (unsigned long)vtophys(b->buf));
80 	}
81 }
82 
83 /*
84  * Allocate memory for DMA buffer. If the device does not use DMA transfers,
85  * the driver can call malloc(9) and sndbuf_setup() itself.
86  */
87 int
88 sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, unsigned int size)
89 {
90 	b->dmatag = dmatag;
91 	b->maxsize = size;
92 	b->bufsize = b->maxsize;
93 	if (bus_dmamem_alloc(b->dmatag, (void **)&b->buf, BUS_DMA_NOWAIT, &b->dmamap))
94 		return ENOSPC;
95 	if (bus_dmamap_load(b->dmatag, b->dmamap, b->buf, b->maxsize, sndbuf_setmap, b, 0))
96 		return ENOSPC;
97 	return sndbuf_resize(b, 2, b->maxsize / 2);
98 }
99 
100 int
101 sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size)
102 {
103 	bzero(b, sizeof(*b));
104 	b->buf = buf;
105 	b->maxsize = size;
106 	b->bufsize = b->maxsize;
107 	return sndbuf_resize(b, 2, b->maxsize / 2);
108 }
109 
110 void
111 sndbuf_free(struct snd_dbuf *b)
112 {
113 	if (b->tmpbuf)
114 		free(b->tmpbuf, M_DEVBUF);
115 	b->tmpbuf = NULL;
116 
117 	if (b->dmamap)
118 		bus_dmamap_unload(b->dmatag, b->dmamap);
119 
120 	if (b->dmamap && b->buf)
121 		bus_dmamem_free(b->dmatag, b->buf, b->dmamap);
122 }
123 
124 int
125 sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
126 {
127 	if (b->maxsize == 0)
128 		return 0;
129 	if (blkcnt == 0)
130 		blkcnt = b->blkcnt;
131 	if (blksz == 0)
132 		blksz = b->blksz;
133 	if (blkcnt < 2 || blksz < 16 || (blkcnt * blksz > b->maxsize))
134 		return EINVAL;
135 	b->blkcnt = blkcnt;
136 	b->blksz = blksz;
137 	b->bufsize = blkcnt * blksz;
138 	if (b->tmpbuf)
139 		free(b->tmpbuf, M_DEVBUF);
140 	b->tmpbuf = malloc(b->bufsize, M_DEVBUF, M_WAITOK);
141 	sndbuf_reset(b);
142 	return 0;
143 }
144 
145 int
146 sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
147 {
148 	if (blkcnt < 2 || blksz < 16)
149 		return EINVAL;
150 
151 	b->blkcnt = blkcnt;
152 	b->blksz = blksz;
153 
154 	b->maxsize = blkcnt * blksz;
155 	b->bufsize = b->maxsize;
156 
157 	if (b->buf)
158 		free(b->buf, M_DEVBUF);
159 	b->buf = malloc(b->bufsize, M_DEVBUF, M_WAITOK);
160 	if (b->buf == NULL)
161 		return ENOMEM;
162 
163 	if (b->tmpbuf)
164 		free(b->tmpbuf, M_DEVBUF);
165 	b->tmpbuf = malloc(b->bufsize, M_DEVBUF, M_WAITOK);
166 	if (b->tmpbuf == NULL)
167 		return ENOMEM;
168 
169 	sndbuf_reset(b);
170 	return 0;
171 }
172 
173 void
174 sndbuf_clear(struct snd_dbuf *b, unsigned int length)
175 {
176 	int i;
177 	u_char data, *p;
178 
179 	if (length == 0)
180 		return;
181 	if (length > b->bufsize)
182 		length = b->bufsize;
183 
184 	if (b->fmt & AFMT_SIGNED)
185 		data = 0x00;
186 	else
187 		data = 0x80;
188 
189 	if (b->fmt & AFMT_16BIT)
190 		data <<= 8;
191 	else
192 		data |= data << 8;
193 
194 	i = sndbuf_getfreeptr(b);
195 	p = sndbuf_getbuf(b);
196 	while (length > 0) {
197 		p[i] = data;
198 		length--;
199 		i++;
200 		if (i >= b->bufsize)
201 			i = 0;
202 	}
203 }
204 
205 void
206 sndbuf_reset(struct snd_dbuf *b)
207 {
208 	b->hp = 0;
209 	b->rp = 0;
210 	b->rl = 0;
211 	b->dl = 0;
212 	b->prev_total = 0;
213 	b->total = 0;
214 	b->xrun = 0;
215 	if (b->buf && b->bufsize > 0)
216 		sndbuf_clear(b, b->bufsize);
217 }
218 
219 u_int32_t
220 sndbuf_getfmt(struct snd_dbuf *b)
221 {
222 	return b->fmt;
223 }
224 
225 int
226 sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt)
227 {
228 	b->fmt = fmt;
229 	b->bps = 1;
230 	b->bps <<= (b->fmt & AFMT_STEREO)? 1 : 0;
231 	b->bps <<= (b->fmt & AFMT_16BIT)? 1 : 0;
232 	b->bps <<= (b->fmt & AFMT_32BIT)? 2 : 0;
233 	return 0;
234 }
235 
236 unsigned int
237 sndbuf_getspd(struct snd_dbuf *b)
238 {
239 	return b->spd;
240 }
241 
242 void
243 sndbuf_setspd(struct snd_dbuf *b, unsigned int spd)
244 {
245 	b->spd = spd;
246 }
247 
248 unsigned int
249 sndbuf_getalign(struct snd_dbuf *b)
250 {
251 	static int align[] = {0, 1, 1, 2, 2, 2, 2, 3};
252 
253 	return align[b->bps - 1];
254 }
255 
256 unsigned int
257 sndbuf_getblkcnt(struct snd_dbuf *b)
258 {
259 	return b->blkcnt;
260 }
261 
262 void
263 sndbuf_setblkcnt(struct snd_dbuf *b, unsigned int blkcnt)
264 {
265 	b->blkcnt = blkcnt;
266 }
267 
268 unsigned int
269 sndbuf_getblksz(struct snd_dbuf *b)
270 {
271 	return b->blksz;
272 }
273 
274 void
275 sndbuf_setblksz(struct snd_dbuf *b, unsigned int blksz)
276 {
277 	b->blksz = blksz;
278 }
279 
280 unsigned int
281 sndbuf_getbps(struct snd_dbuf *b)
282 {
283 	return b->bps;
284 }
285 
286 void *
287 sndbuf_getbuf(struct snd_dbuf *b)
288 {
289 	return b->buf;
290 }
291 
292 void *
293 sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs)
294 {
295 	KASSERT((ofs >= 0) && (ofs <= b->bufsize), ("%s: ofs invalid %d", __FUNCTION__, ofs));
296 
297 	return b->buf + ofs;
298 }
299 
300 unsigned int
301 sndbuf_getsize(struct snd_dbuf *b)
302 {
303 	return b->bufsize;
304 }
305 
306 unsigned int
307 sndbuf_getmaxsize(struct snd_dbuf *b)
308 {
309 	return b->maxsize;
310 }
311 
312 unsigned int
313 sndbuf_runsz(struct snd_dbuf *b)
314 {
315 	return b->dl;
316 }
317 
318 void
319 sndbuf_setrun(struct snd_dbuf *b, int go)
320 {
321 	b->dl = go? b->blksz : 0;
322 }
323 
324 struct selinfo *
325 sndbuf_getsel(struct snd_dbuf *b)
326 {
327 	return &b->sel;
328 }
329 
330 /************************************************************/
331 unsigned int
332 sndbuf_getxrun(struct snd_dbuf *b)
333 {
334 	SNDBUF_LOCKASSERT(b);
335 
336 	return b->xrun;
337 }
338 
339 void
340 sndbuf_setxrun(struct snd_dbuf *b, unsigned int cnt)
341 {
342 	SNDBUF_LOCKASSERT(b);
343 
344 	b->xrun = cnt;
345 }
346 
347 unsigned int
348 sndbuf_gethwptr(struct snd_dbuf *b)
349 {
350 	SNDBUF_LOCKASSERT(b);
351 
352 	return b->hp;
353 }
354 
355 void
356 sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr)
357 {
358 	SNDBUF_LOCKASSERT(b);
359 
360 	b->hp = ptr;
361 }
362 
363 unsigned int
364 sndbuf_getready(struct snd_dbuf *b)
365 {
366 	SNDBUF_LOCKASSERT(b);
367 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __FUNCTION__, b->rl));
368 
369 	return b->rl;
370 }
371 
372 unsigned int
373 sndbuf_getreadyptr(struct snd_dbuf *b)
374 {
375 	SNDBUF_LOCKASSERT(b);
376 	KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __FUNCTION__, b->rp));
377 
378 	return b->rp;
379 }
380 
381 unsigned int
382 sndbuf_getfree(struct snd_dbuf *b)
383 {
384 	SNDBUF_LOCKASSERT(b);
385 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __FUNCTION__, b->rl));
386 
387 	return b->bufsize - b->rl;
388 }
389 
390 unsigned int
391 sndbuf_getfreeptr(struct snd_dbuf *b)
392 {
393 	SNDBUF_LOCKASSERT(b);
394 	KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __FUNCTION__, b->rp));
395 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __FUNCTION__, b->rl));
396 
397 	return (b->rp + b->rl) % b->bufsize;
398 }
399 
400 unsigned int
401 sndbuf_getblocks(struct snd_dbuf *b)
402 {
403 	SNDBUF_LOCKASSERT(b);
404 
405 	return b->total / b->blksz;
406 }
407 
408 unsigned int
409 sndbuf_getprevblocks(struct snd_dbuf *b)
410 {
411 	SNDBUF_LOCKASSERT(b);
412 
413 	return b->prev_total / b->blksz;
414 }
415 
416 unsigned int
417 sndbuf_gettotal(struct snd_dbuf *b)
418 {
419 	SNDBUF_LOCKASSERT(b);
420 
421 	return b->total;
422 }
423 
424 void
425 sndbuf_updateprevtotal(struct snd_dbuf *b)
426 {
427 	SNDBUF_LOCKASSERT(b);
428 
429 	b->prev_total = b->total;
430 }
431 
432 /************************************************************/
433 
434 int
435 sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count)
436 {
437 	int l;
438 
439 	KASSERT(count <= sndbuf_getfree(b), ("%s: count %d > free %d", __FUNCTION__, count, sndbuf_getfree(b)));
440 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __FUNCTION__, b->rl));
441 	b->total += count;
442 	if (from != NULL) {
443 		while (count > 0) {
444 			l = MIN(count, sndbuf_getsize(b) - sndbuf_getfreeptr(b));
445 			bcopy(from, sndbuf_getbufofs(b, sndbuf_getfreeptr(b)), l);
446 			from += l;
447 			b->rl += l;
448 			count -= l;
449 		}
450 	} else
451 		b->rl += count;
452 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __FUNCTION__, b->rl, count));
453 
454 	return 0;
455 }
456 
457 int
458 sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count)
459 {
460 	int l;
461 
462 	KASSERT(count <= sndbuf_getready(b), ("%s: count %d > ready %d", __FUNCTION__, count, sndbuf_getready(b)));
463 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __FUNCTION__, b->rl));
464 	if (to != NULL) {
465 		while (count > 0) {
466 			l = MIN(count, sndbuf_getsize(b) - sndbuf_getreadyptr(b));
467 			bcopy(sndbuf_getbufofs(b, sndbuf_getreadyptr(b)), to, l);
468 			to += l;
469 			b->rl -= l;
470 			b->rp = (b->rp + l) % b->bufsize;
471 			count -= l;
472 		}
473 	} else {
474 		b->rl -= count;
475 		b->rp = (b->rp + count) % b->bufsize;
476 	}
477 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __FUNCTION__, b->rl, count));
478 
479 	return 0;
480 }
481 
482 int
483 sndbuf_uiomove(struct snd_dbuf *b, struct uio *uio, unsigned int count)
484 {
485 	int x, c, p, rd, err;
486 
487 	err = 0;
488 	rd = (uio->uio_rw == UIO_READ)? 1 : 0;
489 	if (count > uio->uio_resid)
490 		return EINVAL;
491 
492 	if (count > (rd? sndbuf_getready(b) : sndbuf_getfree(b))) {
493 		return EINVAL;
494 	}
495 
496 	while (err == 0 && count > 0) {
497 		p = rd? sndbuf_getreadyptr(b) : sndbuf_getfreeptr(b);
498 		c = MIN(count, sndbuf_getsize(b) - p);
499 		x = uio->uio_resid;
500 		err = uiomove(sndbuf_getbufofs(b, p), c, uio);
501 		x -= uio->uio_resid;
502 		count -= x;
503 		x = rd? sndbuf_dispose(b, NULL, x) : sndbuf_acquire(b, NULL, x);
504 	}
505 
506 	return 0;
507 }
508 
509 /* count is number of bytes we want added to destination buffer */
510 int
511 sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count)
512 {
513 	if (sndbuf_getfree(to) < count)
514 		return EINVAL;
515 
516 	count = FEEDER_FEED(feeder, channel, to->tmpbuf, count, from);
517 	if (count)
518 		sndbuf_acquire(to, to->tmpbuf, count);
519 	/* the root feeder has called sndbuf_dispose(from, , bytes fetched) */
520 
521 	return 0;
522 }
523 
524 /************************************************************/
525 
526 void
527 sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what)
528 {
529 	printf("%s: [", s);
530 	if (what & 0x01)
531 		printf(" bufsize: %d, maxsize: %d", b->bufsize, b->maxsize);
532 	if (what & 0x02)
533 		printf(" dl: %d, rp: %d, rl: %d, hp: %d", b->dl, b->rp, b->rl, b->hp);
534 	if (what & 0x04)
535 		printf(" total: %d, prev_total: %d, xrun: %d", b->total, b->prev_total, b->xrun);
536    	if (what & 0x08)
537 		printf(" fmt: 0x%x, spd: %d", b->fmt, b->spd);
538 	if (what & 0x10)
539 		printf(" blksz: %d, blkcnt: %d, flags: 0x%x", b->blksz, b->blkcnt, b->flags);
540 	printf(" ]\n");
541 }
542 
543 /************************************************************/
544 u_int32_t
545 sndbuf_getflags(struct snd_dbuf *b)
546 {
547 	return b->flags;
548 }
549 
550 void
551 sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on)
552 {
553 	b->flags &= ~flags;
554 	if (on)
555 		b->flags |= flags;
556 }
557 
558 /************************************************************/
559 
560 int
561 sndbuf_isadmasetup(struct snd_dbuf *b, struct resource *drq)
562 {
563 	/* should do isa_dma_acquire/isa_dma_release here */
564 	if (drq == NULL) {
565 		b->isadmachan = -1;
566 	} else {
567 		sndbuf_setflags(b, SNDBUF_F_ISADMA, 1);
568 		b->isadmachan = rman_get_start(drq);
569 	}
570 	return 0;
571 }
572 
573 int
574 sndbuf_isadmasetdir(struct snd_dbuf *b, int dir)
575 {
576 	KASSERT(b, ("sndbuf_isadmasetdir called with b == NULL"));
577 	KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadmasetdir called on non-ISA buffer"));
578 
579 	b->dir = (dir == PCMDIR_PLAY)? ISADMA_WRITE : ISADMA_READ;
580 	return 0;
581 }
582 
583 void
584 sndbuf_isadma(struct snd_dbuf *b, int go)
585 {
586 	KASSERT(b, ("sndbuf_isadma called with b == NULL"));
587 	KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadma called on non-ISA buffer"));
588 
589 	switch (go) {
590 	case PCMTRIG_START:
591 		/* isa_dmainit(b->chan, size); */
592 		isa_dmastart(b->dir | ISADMA_RAW, b->buf, b->bufsize, b->isadmachan);
593 		break;
594 
595 	case PCMTRIG_STOP:
596 	case PCMTRIG_ABORT:
597 		isa_dmastop(b->isadmachan);
598 		isa_dmadone(b->dir | ISADMA_RAW, b->buf, b->bufsize, b->isadmachan);
599 		break;
600 	}
601 
602 	DEB(printf("buf 0x%p ISA DMA %s, channel %d\n",
603 		b,
604 		(go == PCMTRIG_START)? "started" : "stopped",
605 		b->chan));
606 }
607 
608 int
609 sndbuf_isadmaptr(struct snd_dbuf *b)
610 {
611 	int i;
612 
613 	KASSERT(b, ("sndbuf_isadmaptr called with b == NULL"));
614 	KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadmaptr called on non-ISA buffer"));
615 
616 	if (!sndbuf_runsz(b))
617 		return 0;
618 	i = isa_dmastatus(b->isadmachan);
619 	KASSERT(i >= 0, ("isa_dmastatus returned %d", i));
620 	return b->bufsize - i;
621 }
622 
623 void
624 sndbuf_isadmabounce(struct snd_dbuf *b)
625 {
626 	KASSERT(b, ("sndbuf_isadmabounce called with b == NULL"));
627 	KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadmabounce called on non-ISA buffer"));
628 
629 	/* tell isa_dma to bounce data in/out */
630 }
631 
632