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