Remove bogus checks after kmalloc(M_WAITOK) which never returns NULL.
[dragonfly.git] / sys / dev / sound / pcm / buffer.c
blob1581806c97ba20d9abbc493520b7251714cf4264
1 /*-
2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
3 * All rights reserved.
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.
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.
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.11 2008/01/06 16:55:51 swildner Exp $
30 #include <dev/sound/pcm/sound.h>
32 #include "feeder_if.h"
34 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/buffer.c,v 1.11 2008/01/06 16:55:51 swildner Exp $");
37 * sndbuf_seltask is a taskqueue callback routine, called from
38 * taskqueue_swi, which runs under the MP lock.
40 * The only purpose is to be able to selwakeup() from a sound
41 * interrupt, which is running without MP lock held and thus
42 * can't call selwakeup() directly.
44 static void
45 sndbuf_seltask(void *context, int pending)
47 struct snd_dbuf *b = context;
49 selwakeup(sndbuf_getsel(b));
52 struct snd_dbuf *
53 sndbuf_create(device_t dev, char *drv, char *desc, struct pcm_channel *channel)
55 struct snd_dbuf *b;
57 b = kmalloc(sizeof(*b), M_DEVBUF, M_WAITOK | M_ZERO);
58 ksnprintf(b->name, SNDBUF_NAMELEN, "%s:%s", drv, desc);
59 b->dev = dev;
60 b->channel = channel;
61 TASK_INIT(&b->seltask, 0, sndbuf_seltask, b);
63 return b;
66 void
67 sndbuf_destroy(struct snd_dbuf *b)
69 sndbuf_free(b);
70 kfree(b, M_DEVBUF);
73 bus_addr_t
74 sndbuf_getbufaddr(struct snd_dbuf *buf)
76 return (buf->buf_addr);
79 static void
80 sndbuf_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
82 struct snd_dbuf *b = (struct snd_dbuf *)arg;
84 if (bootverbose) {
85 device_printf(b->dev, "sndbuf_setmap %lx, %lx; ",
86 (u_long)segs[0].ds_addr, (u_long)segs[0].ds_len);
87 kprintf("%p -> %lx\n", b->buf, (u_long)segs[0].ds_addr);
89 if (error == 0)
90 b->buf_addr = segs[0].ds_addr;
91 else
92 b->buf_addr = 0;
96 * Allocate memory for DMA buffer. If the device does not use DMA transfers,
97 * the driver can call kmalloc(9) and sndbuf_setup() itself.
101 sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, unsigned int size)
103 int ret;
105 b->dmatag = dmatag;
106 b->maxsize = size;
107 b->bufsize = b->maxsize;
108 b->buf_addr = 0;
109 b->flags |= SNDBUF_F_MANAGED;
110 if (bus_dmamem_alloc(b->dmatag, (void **)&b->buf, BUS_DMA_NOWAIT,
111 &b->dmamap)) {
112 sndbuf_free(b);
113 return (ENOMEM);
115 if (bus_dmamap_load(b->dmatag, b->dmamap, b->buf, b->maxsize,
116 sndbuf_setmap, b, 0) != 0 || b->buf_addr == 0) {
117 sndbuf_free(b);
118 return (ENOMEM);
121 ret = sndbuf_resize(b, 2, b->maxsize / 2);
122 if (ret != 0)
123 sndbuf_free(b);
125 return (ret);
129 sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size)
131 b->flags &= ~SNDBUF_F_MANAGED;
132 if (buf)
133 b->flags |= SNDBUF_F_MANAGED;
134 b->buf = buf;
135 b->maxsize = size;
136 b->bufsize = b->maxsize;
137 return sndbuf_resize(b, 2, b->maxsize / 2);
140 void
141 sndbuf_free(struct snd_dbuf *b)
143 if (b->tmpbuf)
144 kfree(b->tmpbuf, M_DEVBUF);
145 if (b->buf) {
146 if (b->flags & SNDBUF_F_MANAGED) {
147 if (b->dmamap)
148 bus_dmamap_unload(b->dmatag, b->dmamap);
149 if (b->dmatag)
150 bus_dmamem_free(b->dmatag, b->buf, b->dmamap);
151 } else
152 kfree(b->buf, M_DEVBUF);
155 b->tmpbuf = NULL;
156 b->buf = NULL;
157 b->dmatag = NULL;
158 b->dmamap = NULL;
162 sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
164 u_int8_t *tmpbuf, *f2;
166 chn_lock(b->channel);
167 if (b->maxsize == 0)
168 goto out;
169 if (blkcnt == 0)
170 blkcnt = b->blkcnt;
171 if (blksz == 0)
172 blksz = b->blksz;
173 if (blkcnt < 2 || blksz < 16 || (blkcnt * blksz > b->maxsize)) {
174 chn_unlock(b->channel);
175 return EINVAL;
177 if (blkcnt == b->blkcnt && blksz == b->blksz)
178 goto out;
180 chn_unlock(b->channel);
181 tmpbuf = kmalloc(blkcnt * blksz, M_DEVBUF, M_NOWAIT);
182 if (tmpbuf == NULL)
183 return ENOMEM;
184 chn_lock(b->channel);
185 b->blkcnt = blkcnt;
186 b->blksz = blksz;
187 b->bufsize = blkcnt * blksz;
188 f2 = b->tmpbuf;
189 b->tmpbuf = tmpbuf;
190 sndbuf_reset(b);
191 chn_unlock(b->channel);
192 if (f2 != NULL)
193 kfree(f2, M_DEVBUF);
194 return 0;
195 out:
196 chn_unlock(b->channel);
197 return 0;
201 sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
203 u_int8_t *buf, *tmpbuf, *f1, *f2;
204 unsigned int bufsize;
205 int ret;
207 if (blkcnt < 2 || blksz < 16)
208 return EINVAL;
210 bufsize = blksz * blkcnt;
212 chn_unlock(b->channel);
213 buf = kmalloc(bufsize, M_DEVBUF, M_WAITOK);
214 tmpbuf = kmalloc(bufsize, M_DEVBUF, M_WAITOK);
215 chn_lock(b->channel);
217 b->blkcnt = blkcnt;
218 b->blksz = blksz;
219 b->bufsize = bufsize;
220 b->maxsize = bufsize;
221 f1 = b->buf;
222 f2 = b->tmpbuf;
223 b->buf = buf;
224 b->tmpbuf = tmpbuf;
226 sndbuf_reset(b);
228 chn_unlock(b->channel);
229 if (f1)
230 kfree(f1, M_DEVBUF);
231 if (f2)
232 kfree(f2, M_DEVBUF);
234 ret = 0;
235 chn_lock(b->channel);
236 return ret;
239 void
240 sndbuf_clear(struct snd_dbuf *b, unsigned int length)
242 int i;
243 u_char data, *p;
245 if (length == 0)
246 return;
247 if (length > b->bufsize)
248 length = b->bufsize;
250 if (b->fmt & AFMT_SIGNED)
251 data = 0x00;
252 else
253 data = 0x80;
255 i = sndbuf_getfreeptr(b);
256 p = sndbuf_getbuf(b);
257 while (length > 0) {
258 p[i] = data;
259 length--;
260 i++;
261 if (i >= b->bufsize)
262 i = 0;
266 void
267 sndbuf_fillsilence(struct snd_dbuf *b)
269 int i;
270 u_char data, *p;
272 if (b->fmt & AFMT_SIGNED)
273 data = 0x00;
274 else
275 data = 0x80;
277 i = 0;
278 p = sndbuf_getbuf(b);
279 while (i < b->bufsize)
280 p[i++] = data;
281 b->rp = 0;
282 b->rl = b->bufsize;
285 void
286 sndbuf_reset(struct snd_dbuf *b)
288 b->hp = 0;
289 b->rp = 0;
290 b->rl = 0;
291 b->dl = 0;
292 b->prev_total = 0;
293 b->total = 0;
294 b->xrun = 0;
295 if (b->buf && b->bufsize > 0)
296 sndbuf_clear(b, b->bufsize);
299 u_int32_t
300 sndbuf_getfmt(struct snd_dbuf *b)
302 return b->fmt;
306 sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt)
308 b->fmt = fmt;
309 b->bps = 1;
310 b->bps <<= (b->fmt & AFMT_STEREO)? 1 : 0;
311 if (b->fmt & AFMT_16BIT)
312 b->bps <<= 1;
313 else if (b->fmt & AFMT_24BIT)
314 b->bps *= 3;
315 else if (b->fmt & AFMT_32BIT)
316 b->bps <<= 2;
317 return 0;
320 unsigned int
321 sndbuf_getspd(struct snd_dbuf *b)
323 return b->spd;
326 void
327 sndbuf_setspd(struct snd_dbuf *b, unsigned int spd)
329 b->spd = spd;
332 unsigned int
333 sndbuf_getalign(struct snd_dbuf *b)
335 static int align[] = {0, 1, 1, 2, 2, 2, 2, 3};
337 return align[b->bps - 1];
340 unsigned int
341 sndbuf_getblkcnt(struct snd_dbuf *b)
343 return b->blkcnt;
346 void
347 sndbuf_setblkcnt(struct snd_dbuf *b, unsigned int blkcnt)
349 b->blkcnt = blkcnt;
352 unsigned int
353 sndbuf_getblksz(struct snd_dbuf *b)
355 return b->blksz;
358 void
359 sndbuf_setblksz(struct snd_dbuf *b, unsigned int blksz)
361 b->blksz = blksz;
364 unsigned int
365 sndbuf_getbps(struct snd_dbuf *b)
367 return b->bps;
370 void *
371 sndbuf_getbuf(struct snd_dbuf *b)
373 return b->buf;
376 void *
377 sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs)
379 KASSERT(ofs < b->bufsize, ("%s: ofs invalid %d", __func__, ofs));
381 return b->buf + ofs;
384 unsigned int
385 sndbuf_getsize(struct snd_dbuf *b)
387 return b->bufsize;
390 unsigned int
391 sndbuf_getmaxsize(struct snd_dbuf *b)
393 return b->maxsize;
396 unsigned int
397 sndbuf_runsz(struct snd_dbuf *b)
399 return b->dl;
402 void
403 sndbuf_setrun(struct snd_dbuf *b, int go)
405 b->dl = go? b->blksz : 0;
408 struct selinfo *
409 sndbuf_getsel(struct snd_dbuf *b)
411 return &b->sel;
414 /************************************************************/
415 unsigned int
416 sndbuf_getxrun(struct snd_dbuf *b)
418 SNDBUF_LOCKASSERT(b);
420 return b->xrun;
423 void
424 sndbuf_setxrun(struct snd_dbuf *b, unsigned int cnt)
426 SNDBUF_LOCKASSERT(b);
428 b->xrun = cnt;
431 unsigned int
432 sndbuf_gethwptr(struct snd_dbuf *b)
434 SNDBUF_LOCKASSERT(b);
436 return b->hp;
439 void
440 sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr)
442 SNDBUF_LOCKASSERT(b);
444 b->hp = ptr;
447 unsigned int
448 sndbuf_getready(struct snd_dbuf *b)
450 SNDBUF_LOCKASSERT(b);
451 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
453 return b->rl;
456 unsigned int
457 sndbuf_getreadyptr(struct snd_dbuf *b)
459 SNDBUF_LOCKASSERT(b);
460 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
462 return b->rp;
465 unsigned int
466 sndbuf_getfree(struct snd_dbuf *b)
468 SNDBUF_LOCKASSERT(b);
469 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
471 return b->bufsize - b->rl;
474 unsigned int
475 sndbuf_getfreeptr(struct snd_dbuf *b)
477 SNDBUF_LOCKASSERT(b);
478 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
479 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
481 return (b->rp + b->rl) % b->bufsize;
484 unsigned int
485 sndbuf_getblocks(struct snd_dbuf *b)
487 SNDBUF_LOCKASSERT(b);
489 return b->total / b->blksz;
492 unsigned int
493 sndbuf_getprevblocks(struct snd_dbuf *b)
495 SNDBUF_LOCKASSERT(b);
497 return b->prev_total / b->blksz;
500 unsigned int
501 sndbuf_gettotal(struct snd_dbuf *b)
503 SNDBUF_LOCKASSERT(b);
505 return b->total;
508 void
509 sndbuf_updateprevtotal(struct snd_dbuf *b)
511 SNDBUF_LOCKASSERT(b);
513 b->prev_total = b->total;
516 /************************************************************/
519 sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count)
521 int l;
523 KASSERT(count <= sndbuf_getfree(b), ("%s: count %d > kfree %d", __func__, count, sndbuf_getfree(b)));
524 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
525 b->total += count;
526 if (from != NULL) {
527 while (count > 0) {
528 l = MIN(count, sndbuf_getsize(b) - sndbuf_getfreeptr(b));
529 bcopy(from, sndbuf_getbufofs(b, sndbuf_getfreeptr(b)), l);
530 from += l;
531 b->rl += l;
532 count -= l;
534 } else
535 b->rl += count;
536 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
538 return 0;
542 sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count)
544 int l;
546 KASSERT(count <= sndbuf_getready(b), ("%s: count %d > ready %d", __func__, count, sndbuf_getready(b)));
547 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
548 if (to != NULL) {
549 while (count > 0) {
550 l = MIN(count, sndbuf_getsize(b) - sndbuf_getreadyptr(b));
551 bcopy(sndbuf_getbufofs(b, sndbuf_getreadyptr(b)), to, l);
552 to += l;
553 b->rl -= l;
554 b->rp = (b->rp + l) % b->bufsize;
555 count -= l;
557 } else {
558 b->rl -= count;
559 b->rp = (b->rp + count) % b->bufsize;
561 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
563 return 0;
566 /* count is number of bytes we want added to destination buffer */
568 sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count)
570 KASSERT(count > 0, ("can't feed 0 bytes"));
572 if (sndbuf_getfree(to) < count)
573 return EINVAL;
575 count = FEEDER_FEED(feeder, channel, to->tmpbuf, count, from);
576 if (count)
577 sndbuf_acquire(to, to->tmpbuf, count);
578 /* the root feeder has called sndbuf_dispose(from, , bytes fetched) */
580 return 0;
583 /************************************************************/
585 void
586 sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what)
588 kprintf("%s: [", s);
589 if (what & 0x01)
590 kprintf(" bufsize: %d, maxsize: %d", b->bufsize, b->maxsize);
591 if (what & 0x02)
592 kprintf(" dl: %d, rp: %d, rl: %d, hp: %d", b->dl, b->rp, b->rl, b->hp);
593 if (what & 0x04)
594 kprintf(" total: %d, prev_total: %d, xrun: %d", b->total, b->prev_total, b->xrun);
595 if (what & 0x08)
596 kprintf(" fmt: 0x%x, spd: %d", b->fmt, b->spd);
597 if (what & 0x10)
598 kprintf(" blksz: %d, blkcnt: %d, flags: 0x%x", b->blksz, b->blkcnt, b->flags);
599 kprintf(" ]\n");
602 /************************************************************/
603 u_int32_t
604 sndbuf_getflags(struct snd_dbuf *b)
606 return b->flags;
609 void
610 sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on)
612 b->flags &= ~flags;
613 if (on)
614 b->flags |= flags;