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