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