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