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