kernel: Remove some unneeded NULL checks after kmalloc() with M_WAITOK.
[dragonfly.git] / sys / dev / sound / pcm / buffer.c
... / ...
CommitLineData
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 */
28
29#include <dev/sound/pcm/sound.h>
30
31#include "feeder_if.h"
32
33SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/buffer.c,v 1.11 2008/01/06 16:55:51 swildner Exp $");
34
35/*
36 * XXX
37 *
38 * sndbuf_kqtask is a taskqueue callback routine, called from
39 * taskqueue_swi, which runs under the MP lock.
40 *
41 * The only purpose is to be able to KNOTE() from a sound
42 * interrupt, which is running without MP lock held and thus
43 * can't call KNOTE() directly.
44 */
45static void
46sndbuf_kqtask(void *context, int pending)
47{
48 struct snd_dbuf *b = context;
49 struct kqinfo *ki = sndbuf_getkq(b);
50
51 KNOTE(&ki->ki_note, 0);
52}
53
54struct snd_dbuf *
55sndbuf_create(device_t dev, char *drv, char *desc, struct pcm_channel *channel)
56{
57 struct snd_dbuf *b;
58
59 b = kmalloc(sizeof(*b), M_DEVBUF, M_WAITOK | M_ZERO);
60 ksnprintf(b->name, SNDBUF_NAMELEN, "%s:%s", drv, desc);
61 b->dev = dev;
62 b->channel = channel;
63 TASK_INIT(&b->kqtask, 0, sndbuf_kqtask, b);
64
65 return b;
66}
67
68void
69sndbuf_destroy(struct snd_dbuf *b)
70{
71 sndbuf_free(b);
72 kfree(b, M_DEVBUF);
73}
74
75bus_addr_t
76sndbuf_getbufaddr(struct snd_dbuf *buf)
77{
78 return (buf->buf_addr);
79}
80
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) {
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);
90 }
91 if (error == 0)
92 b->buf_addr = segs[0].ds_addr;
93 else
94 b->buf_addr = 0;
95}
96
97/*
98 * Allocate memory for DMA buffer. If the device does not use DMA transfers,
99 * the driver can call kmalloc(9) and sndbuf_setup() itself.
100 */
101
102int
103sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, unsigned int size)
104{
105 int ret;
106
107 b->dmatag = dmatag;
108 b->maxsize = size;
109 b->bufsize = b->maxsize;
110 b->buf_addr = 0;
111 b->flags |= SNDBUF_F_MANAGED;
112 if (bus_dmamem_alloc(b->dmatag, (void **)&b->buf, BUS_DMA_NOWAIT,
113 &b->dmamap)) {
114 sndbuf_free(b);
115 return (ENOMEM);
116 }
117 if (bus_dmamap_load(b->dmatag, b->dmamap, b->buf, b->maxsize,
118 sndbuf_setmap, b, 0) != 0 || b->buf_addr == 0) {
119 sndbuf_free(b);
120 return (ENOMEM);
121 }
122
123 ret = sndbuf_resize(b, 2, b->maxsize / 2);
124 if (ret != 0)
125 sndbuf_free(b);
126
127 return (ret);
128}
129
130int
131sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size)
132{
133 b->flags &= ~SNDBUF_F_MANAGED;
134 if (buf)
135 b->flags |= SNDBUF_F_MANAGED;
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)
146 kfree(b->tmpbuf, M_DEVBUF);
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 }
156
157 b->tmpbuf = NULL;
158 b->buf = NULL;
159 b->dmatag = NULL;
160 b->dmamap = NULL;
161}
162
163int
164sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
165{
166 u_int8_t *tmpbuf, *f2;
167
168 chn_lock(b->channel);
169 if (b->maxsize == 0)
170 goto out;
171 if (blkcnt == 0)
172 blkcnt = b->blkcnt;
173 if (blksz == 0)
174 blksz = b->blksz;
175 if (blkcnt < 2 || blksz < 16 || (blkcnt * blksz > b->maxsize)) {
176 chn_unlock(b->channel);
177 return EINVAL;
178 }
179 if (blkcnt == b->blkcnt && blksz == b->blksz)
180 goto out;
181
182 chn_unlock(b->channel);
183 tmpbuf = kmalloc(blkcnt * blksz, M_DEVBUF, M_WAITOK);
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;
195out:
196 chn_unlock(b->channel);
197 return 0;
198}
199
200int
201sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
202{
203 u_int8_t *buf, *tmpbuf, *f1, *f2;
204 unsigned int bufsize;
205 int ret;
206
207 if (blkcnt < 2 || blksz < 16)
208 return EINVAL;
209
210 bufsize = blksz * blkcnt;
211
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);
216
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;
225
226 sndbuf_reset(b);
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;
235 chn_lock(b->channel);
236 return ret;
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;
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;
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
408struct kqinfo *
409sndbuf_getkq(struct snd_dbuf *b)
410{
411 return &b->kq;
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
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;
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
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{
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");
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}