xref: /freebsd/sys/dev/sound/pcm/buffer.c (revision 3642298923e528d795e3a30ec165d2b469e28b40)
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 	if (b->fmt & AFMT_16BIT)
290 		b->bps <<= 1;
291 	else if (b->fmt & AFMT_24BIT)
292 		b->bps *= 3;
293 	else if (b->fmt & AFMT_32BIT)
294 		b->bps <<= 2;
295 	return 0;
296 }
297 
298 unsigned int
299 sndbuf_getspd(struct snd_dbuf *b)
300 {
301 	return b->spd;
302 }
303 
304 void
305 sndbuf_setspd(struct snd_dbuf *b, unsigned int spd)
306 {
307 	b->spd = spd;
308 }
309 
310 unsigned int
311 sndbuf_getalign(struct snd_dbuf *b)
312 {
313 	static int align[] = {0, 1, 1, 2, 2, 2, 2, 3};
314 
315 	return align[b->bps - 1];
316 }
317 
318 unsigned int
319 sndbuf_getblkcnt(struct snd_dbuf *b)
320 {
321 	return b->blkcnt;
322 }
323 
324 void
325 sndbuf_setblkcnt(struct snd_dbuf *b, unsigned int blkcnt)
326 {
327 	b->blkcnt = blkcnt;
328 }
329 
330 unsigned int
331 sndbuf_getblksz(struct snd_dbuf *b)
332 {
333 	return b->blksz;
334 }
335 
336 void
337 sndbuf_setblksz(struct snd_dbuf *b, unsigned int blksz)
338 {
339 	b->blksz = blksz;
340 }
341 
342 unsigned int
343 sndbuf_getbps(struct snd_dbuf *b)
344 {
345 	return b->bps;
346 }
347 
348 void *
349 sndbuf_getbuf(struct snd_dbuf *b)
350 {
351 	return b->buf;
352 }
353 
354 void *
355 sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs)
356 {
357 	KASSERT(ofs < b->bufsize, ("%s: ofs invalid %d", __func__, ofs));
358 
359 	return b->buf + ofs;
360 }
361 
362 unsigned int
363 sndbuf_getsize(struct snd_dbuf *b)
364 {
365 	return b->bufsize;
366 }
367 
368 unsigned int
369 sndbuf_getmaxsize(struct snd_dbuf *b)
370 {
371 	return b->maxsize;
372 }
373 
374 unsigned int
375 sndbuf_runsz(struct snd_dbuf *b)
376 {
377 	return b->dl;
378 }
379 
380 void
381 sndbuf_setrun(struct snd_dbuf *b, int go)
382 {
383 	b->dl = go? b->blksz : 0;
384 }
385 
386 struct selinfo *
387 sndbuf_getsel(struct snd_dbuf *b)
388 {
389 	return &b->sel;
390 }
391 
392 /************************************************************/
393 unsigned int
394 sndbuf_getxrun(struct snd_dbuf *b)
395 {
396 	SNDBUF_LOCKASSERT(b);
397 
398 	return b->xrun;
399 }
400 
401 void
402 sndbuf_setxrun(struct snd_dbuf *b, unsigned int cnt)
403 {
404 	SNDBUF_LOCKASSERT(b);
405 
406 	b->xrun = cnt;
407 }
408 
409 unsigned int
410 sndbuf_gethwptr(struct snd_dbuf *b)
411 {
412 	SNDBUF_LOCKASSERT(b);
413 
414 	return b->hp;
415 }
416 
417 void
418 sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr)
419 {
420 	SNDBUF_LOCKASSERT(b);
421 
422 	b->hp = ptr;
423 }
424 
425 unsigned int
426 sndbuf_getready(struct snd_dbuf *b)
427 {
428 	SNDBUF_LOCKASSERT(b);
429 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
430 
431 	return b->rl;
432 }
433 
434 unsigned int
435 sndbuf_getreadyptr(struct snd_dbuf *b)
436 {
437 	SNDBUF_LOCKASSERT(b);
438 	KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
439 
440 	return b->rp;
441 }
442 
443 unsigned int
444 sndbuf_getfree(struct snd_dbuf *b)
445 {
446 	SNDBUF_LOCKASSERT(b);
447 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
448 
449 	return b->bufsize - b->rl;
450 }
451 
452 unsigned int
453 sndbuf_getfreeptr(struct snd_dbuf *b)
454 {
455 	SNDBUF_LOCKASSERT(b);
456 	KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
457 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
458 
459 	return (b->rp + b->rl) % b->bufsize;
460 }
461 
462 unsigned int
463 sndbuf_getblocks(struct snd_dbuf *b)
464 {
465 	SNDBUF_LOCKASSERT(b);
466 
467 	return b->total / b->blksz;
468 }
469 
470 unsigned int
471 sndbuf_getprevblocks(struct snd_dbuf *b)
472 {
473 	SNDBUF_LOCKASSERT(b);
474 
475 	return b->prev_total / b->blksz;
476 }
477 
478 unsigned int
479 sndbuf_gettotal(struct snd_dbuf *b)
480 {
481 	SNDBUF_LOCKASSERT(b);
482 
483 	return b->total;
484 }
485 
486 void
487 sndbuf_updateprevtotal(struct snd_dbuf *b)
488 {
489 	SNDBUF_LOCKASSERT(b);
490 
491 	b->prev_total = b->total;
492 }
493 
494 /************************************************************/
495 
496 int
497 sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count)
498 {
499 	int l;
500 
501 	KASSERT(count <= sndbuf_getfree(b), ("%s: count %d > free %d", __func__, count, sndbuf_getfree(b)));
502 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
503 	b->total += count;
504 	if (from != NULL) {
505 		while (count > 0) {
506 			l = MIN(count, sndbuf_getsize(b) - sndbuf_getfreeptr(b));
507 			bcopy(from, sndbuf_getbufofs(b, sndbuf_getfreeptr(b)), l);
508 			from += l;
509 			b->rl += l;
510 			count -= l;
511 		}
512 	} else
513 		b->rl += count;
514 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
515 
516 	return 0;
517 }
518 
519 int
520 sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count)
521 {
522 	int l;
523 
524 	KASSERT(count <= sndbuf_getready(b), ("%s: count %d > ready %d", __func__, count, sndbuf_getready(b)));
525 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
526 	if (to != NULL) {
527 		while (count > 0) {
528 			l = MIN(count, sndbuf_getsize(b) - sndbuf_getreadyptr(b));
529 			bcopy(sndbuf_getbufofs(b, sndbuf_getreadyptr(b)), to, l);
530 			to += l;
531 			b->rl -= l;
532 			b->rp = (b->rp + l) % b->bufsize;
533 			count -= l;
534 		}
535 	} else {
536 		b->rl -= count;
537 		b->rp = (b->rp + count) % b->bufsize;
538 	}
539 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
540 
541 	return 0;
542 }
543 
544 /* count is number of bytes we want added to destination buffer */
545 int
546 sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count)
547 {
548 	KASSERT(count > 0, ("can't feed 0 bytes"));
549 
550 	if (sndbuf_getfree(to) < count)
551 		return EINVAL;
552 
553 	count = FEEDER_FEED(feeder, channel, to->tmpbuf, count, from);
554 	if (count)
555 		sndbuf_acquire(to, to->tmpbuf, count);
556 	/* the root feeder has called sndbuf_dispose(from, , bytes fetched) */
557 
558 	return 0;
559 }
560 
561 /************************************************************/
562 
563 void
564 sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what)
565 {
566 	printf("%s: [", s);
567 	if (what & 0x01)
568 		printf(" bufsize: %d, maxsize: %d", b->bufsize, b->maxsize);
569 	if (what & 0x02)
570 		printf(" dl: %d, rp: %d, rl: %d, hp: %d", b->dl, b->rp, b->rl, b->hp);
571 	if (what & 0x04)
572 		printf(" total: %d, prev_total: %d, xrun: %d", b->total, b->prev_total, b->xrun);
573    	if (what & 0x08)
574 		printf(" fmt: 0x%x, spd: %d", b->fmt, b->spd);
575 	if (what & 0x10)
576 		printf(" blksz: %d, blkcnt: %d, flags: 0x%x", b->blksz, b->blkcnt, b->flags);
577 	printf(" ]\n");
578 }
579 
580 /************************************************************/
581 u_int32_t
582 sndbuf_getflags(struct snd_dbuf *b)
583 {
584 	return b->flags;
585 }
586 
587 void
588 sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on)
589 {
590 	b->flags &= ~flags;
591 	if (on)
592 		b->flags |= flags;
593 }
594 
595