Remove some duplicate FreeBSD CVS IDs, move some IDs to better places.
[dragonfly.git] / sys / dev / sound / pcm / buffer.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
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
MD
25 *
26 * $FreeBSD: src/sys/dev/sound/pcm/buffer.c,v 1.1.2.4 2002/04/22 15:49:35 cg Exp $
741cff4d 27 * $DragonFly: src/sys/dev/sound/pcm/buffer.c,v 1.3 2003/07/30 00:20:39 dillon Exp $
984263bc
MD
28 */
29
30#include <dev/sound/pcm/sound.h>
31
32#include "feeder_if.h"
33
741cff4d 34SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/buffer.c,v 1.3 2003/07/30 00:20:39 dillon Exp $");
984263bc
MD
35
36#define SNDBUF_NAMELEN 48
37struct snd_dbuf {
38 device_t dev;
39 u_int8_t *buf, *tmpbuf;
40 unsigned int bufsize, maxsize;
41 volatile int dl; /* transfer size */
42 volatile int rp; /* pointers to the ready area */
43 volatile int rl; /* length of ready area */
44 volatile int hp;
45 volatile u_int32_t total, prev_total;
46 int isadmachan, dir; /* dma channel */
47 u_int32_t fmt, spd, bps;
48 unsigned int blksz, blkcnt;
49 int xrun;
50 u_int32_t flags;
51 bus_dmamap_t dmamap;
52 bus_dma_tag_t dmatag;
53 struct selinfo sel;
54 char name[SNDBUF_NAMELEN];
55};
56
57struct snd_dbuf *
58sndbuf_create(device_t dev, char *drv, char *desc)
59{
60 struct snd_dbuf *b;
61
62 b = malloc(sizeof(*b), M_DEVBUF, M_WAITOK | M_ZERO);
63 snprintf(b->name, SNDBUF_NAMELEN, "%s:%s", drv, desc);
64 b->dev = dev;
65
66 return b;
67}
68
69void
70sndbuf_destroy(struct snd_dbuf *b)
71{
72 free(b, M_DEVBUF);
73}
74
75static void
76sndbuf_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
77{
78 struct snd_dbuf *b = (struct snd_dbuf *)arg;
79
80 if (bootverbose) {
81 device_printf(b->dev, "sndbuf_setmap %lx, %lx; ", (unsigned long)segs->ds_addr,
82 (unsigned long)segs->ds_len);
83 printf("%p -> %lx\n", b->buf, (unsigned long)vtophys(b->buf));
84 }
85}
86
87/*
88 * Allocate memory for DMA buffer. If the device does not use DMA transfers,
89 * the driver can call malloc(9) and sndbuf_setup() itself.
90 */
91int
92sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, unsigned int size)
93{
94 b->dmatag = dmatag;
95 b->maxsize = size;
96 b->bufsize = b->maxsize;
97 if (bus_dmamem_alloc(b->dmatag, (void **)&b->buf, BUS_DMA_NOWAIT, &b->dmamap))
98 return ENOSPC;
99 if (bus_dmamap_load(b->dmatag, b->dmamap, b->buf, b->maxsize, sndbuf_setmap, b, 0))
100 return ENOSPC;
101 return sndbuf_resize(b, 2, b->maxsize / 2);
102}
103
104int
105sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size)
106{
107 b->buf = buf;
108 b->maxsize = size;
109 b->bufsize = b->maxsize;
110 return sndbuf_resize(b, 2, b->maxsize / 2);
111}
112
113void
114sndbuf_free(struct snd_dbuf *b)
115{
116 if (b->tmpbuf)
117 free(b->tmpbuf, M_DEVBUF);
118 b->tmpbuf = NULL;
119
120 if (b->dmamap)
121 bus_dmamap_unload(b->dmatag, b->dmamap);
122
123 if (b->dmamap && b->buf)
124 bus_dmamem_free(b->dmatag, b->buf, b->dmamap);
125 b->dmamap = NULL;
126 b->buf = NULL;
127}
128
129int
130sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
131{
132 if (b->maxsize == 0)
133 return 0;
134 if (blkcnt == 0)
135 blkcnt = b->blkcnt;
136 if (blksz == 0)
137 blksz = b->blksz;
138 if (blkcnt < 2 || blksz < 16 || (blkcnt * blksz > b->maxsize))
139 return EINVAL;
140 if (blkcnt == b->blkcnt && blksz == b->blksz)
141 return 0;
142 b->blkcnt = blkcnt;
143 b->blksz = blksz;
144 b->bufsize = blkcnt * blksz;
145 if (b->tmpbuf)
146 free(b->tmpbuf, M_DEVBUF);
147 b->tmpbuf = malloc(b->bufsize, M_DEVBUF, M_WAITOK);
148 sndbuf_reset(b);
149 return 0;
150}
151
152int
153sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
154{
155 if (blkcnt < 2 || blksz < 16)
156 return EINVAL;
157
158 b->blkcnt = blkcnt;
159 b->blksz = blksz;
160
161 b->maxsize = blkcnt * blksz;
162 b->bufsize = b->maxsize;
163
164 if (b->buf)
165 free(b->buf, M_DEVBUF);
166 b->buf = malloc(b->bufsize, M_DEVBUF, M_WAITOK);
167 if (b->buf == NULL)
168 return ENOMEM;
169
170 if (b->tmpbuf)
171 free(b->tmpbuf, M_DEVBUF);
172 b->tmpbuf = malloc(b->bufsize, M_DEVBUF, M_WAITOK);
173 if (b->tmpbuf == NULL)
174 return ENOMEM;
175
176 sndbuf_reset(b);
177 return 0;
178}
179
180void
181sndbuf_clear(struct snd_dbuf *b, unsigned int length)
182{
183 int i;
184 u_char data, *p;
185
186 if (length == 0)
187 return;
188 if (length > b->bufsize)
189 length = b->bufsize;
190
191 if (b->fmt & AFMT_SIGNED)
192 data = 0x00;
193 else
194 data = 0x80;
195
196 i = sndbuf_getfreeptr(b);
197 p = sndbuf_getbuf(b);
198 while (length > 0) {
199 p[i] = data;
200 length--;
201 i++;
202 if (i >= b->bufsize)
203 i = 0;
204 }
205}
206
207void
208sndbuf_fillsilence(struct snd_dbuf *b)
209{
210 int i;
211 u_char data, *p;
212
213 if (b->fmt & AFMT_SIGNED)
214 data = 0x00;
215 else
216 data = 0x80;
217
218 i = 0;
219 p = sndbuf_getbuf(b);
220 while (i < b->bufsize)
221 p[i++] = data;
222 b->rp = 0;
223 b->rl = b->bufsize;
224}
225
226void
227sndbuf_reset(struct snd_dbuf *b)
228{
229 b->hp = 0;
230 b->rp = 0;
231 b->rl = 0;
232 b->dl = 0;
233 b->prev_total = 0;
234 b->total = 0;
235 b->xrun = 0;
236 if (b->buf && b->bufsize > 0)
237 sndbuf_clear(b, b->bufsize);
238}
239
240u_int32_t
241sndbuf_getfmt(struct snd_dbuf *b)
242{
243 return b->fmt;
244}
245
246int
247sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt)
248{
249 b->fmt = fmt;
250 b->bps = 1;
251 b->bps <<= (b->fmt & AFMT_STEREO)? 1 : 0;
252 b->bps <<= (b->fmt & AFMT_16BIT)? 1 : 0;
253 b->bps <<= (b->fmt & AFMT_32BIT)? 2 : 0;
254 return 0;
255}
256
257unsigned int
258sndbuf_getspd(struct snd_dbuf *b)
259{
260 return b->spd;
261}
262
263void
264sndbuf_setspd(struct snd_dbuf *b, unsigned int spd)
265{
266 b->spd = spd;
267}
268
269unsigned int
270sndbuf_getalign(struct snd_dbuf *b)
271{
272 static int align[] = {0, 1, 1, 2, 2, 2, 2, 3};
273
274 return align[b->bps - 1];
275}
276
277unsigned int
278sndbuf_getblkcnt(struct snd_dbuf *b)
279{
280 return b->blkcnt;
281}
282
283void
284sndbuf_setblkcnt(struct snd_dbuf *b, unsigned int blkcnt)
285{
286 b->blkcnt = blkcnt;
287}
288
289unsigned int
290sndbuf_getblksz(struct snd_dbuf *b)
291{
292 return b->blksz;
293}
294
295void
296sndbuf_setblksz(struct snd_dbuf *b, unsigned int blksz)
297{
298 b->blksz = blksz;
299}
300
301unsigned int
302sndbuf_getbps(struct snd_dbuf *b)
303{
304 return b->bps;
305}
306
307void *
308sndbuf_getbuf(struct snd_dbuf *b)
309{
310 return b->buf;
311}
312
313void *
314sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs)
315{
316 KASSERT(ofs < b->bufsize, ("%s: ofs invalid %d", __func__, ofs));
317
318 return b->buf + ofs;
319}
320
321unsigned int
322sndbuf_getsize(struct snd_dbuf *b)
323{
324 return b->bufsize;
325}
326
327unsigned int
328sndbuf_getmaxsize(struct snd_dbuf *b)
329{
330 return b->maxsize;
331}
332
333unsigned int
334sndbuf_runsz(struct snd_dbuf *b)
335{
336 return b->dl;
337}
338
339void
340sndbuf_setrun(struct snd_dbuf *b, int go)
341{
342 b->dl = go? b->blksz : 0;
343}
344
345struct selinfo *
346sndbuf_getsel(struct snd_dbuf *b)
347{
348 return &b->sel;
349}
350
351/************************************************************/
352unsigned int
353sndbuf_getxrun(struct snd_dbuf *b)
354{
355 SNDBUF_LOCKASSERT(b);
356
357 return b->xrun;
358}
359
360void
361sndbuf_setxrun(struct snd_dbuf *b, unsigned int cnt)
362{
363 SNDBUF_LOCKASSERT(b);
364
365 b->xrun = cnt;
366}
367
368unsigned int
369sndbuf_gethwptr(struct snd_dbuf *b)
370{
371 SNDBUF_LOCKASSERT(b);
372
373 return b->hp;
374}
375
376void
377sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr)
378{
379 SNDBUF_LOCKASSERT(b);
380
381 b->hp = ptr;
382}
383
384unsigned int
385sndbuf_getready(struct snd_dbuf *b)
386{
387 SNDBUF_LOCKASSERT(b);
388 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
389
390 return b->rl;
391}
392
393unsigned int
394sndbuf_getreadyptr(struct snd_dbuf *b)
395{
396 SNDBUF_LOCKASSERT(b);
397 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
398
399 return b->rp;
400}
401
402unsigned int
403sndbuf_getfree(struct snd_dbuf *b)
404{
405 SNDBUF_LOCKASSERT(b);
406 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
407
408 return b->bufsize - b->rl;
409}
410
411unsigned int
412sndbuf_getfreeptr(struct snd_dbuf *b)
413{
414 SNDBUF_LOCKASSERT(b);
415 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
416 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
417
418 return (b->rp + b->rl) % b->bufsize;
419}
420
421unsigned int
422sndbuf_getblocks(struct snd_dbuf *b)
423{
424 SNDBUF_LOCKASSERT(b);
425
426 return b->total / b->blksz;
427}
428
429unsigned int
430sndbuf_getprevblocks(struct snd_dbuf *b)
431{
432 SNDBUF_LOCKASSERT(b);
433
434 return b->prev_total / b->blksz;
435}
436
437unsigned int
438sndbuf_gettotal(struct snd_dbuf *b)
439{
440 SNDBUF_LOCKASSERT(b);
441
442 return b->total;
443}
444
445void
446sndbuf_updateprevtotal(struct snd_dbuf *b)
447{
448 SNDBUF_LOCKASSERT(b);
449
450 b->prev_total = b->total;
451}
452
453/************************************************************/
454
455int
456sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count)
457{
458 int l;
459
460 KASSERT(count <= sndbuf_getfree(b), ("%s: count %d > free %d", __func__, count, sndbuf_getfree(b)));
461 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
462 b->total += count;
463 if (from != NULL) {
464 while (count > 0) {
465 l = MIN(count, sndbuf_getsize(b) - sndbuf_getfreeptr(b));
466 bcopy(from, sndbuf_getbufofs(b, sndbuf_getfreeptr(b)), l);
467 from += l;
468 b->rl += l;
469 count -= l;
470 }
471 } else
472 b->rl += count;
473 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
474
475 return 0;
476}
477
478int
479sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count)
480{
481 int l;
482
483 KASSERT(count <= sndbuf_getready(b), ("%s: count %d > ready %d", __func__, count, sndbuf_getready(b)));
484 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
485 if (to != NULL) {
486 while (count > 0) {
487 l = MIN(count, sndbuf_getsize(b) - sndbuf_getreadyptr(b));
488 bcopy(sndbuf_getbufofs(b, sndbuf_getreadyptr(b)), to, l);
489 to += l;
490 b->rl -= l;
491 b->rp = (b->rp + l) % b->bufsize;
492 count -= l;
493 }
494 } else {
495 b->rl -= count;
496 b->rp = (b->rp + count) % b->bufsize;
497 }
498 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
499
500 return 0;
501}
502
503int
504sndbuf_uiomove(struct snd_dbuf *b, struct uio *uio, unsigned int count)
505{
506 int x, c, p, rd, err;
507
508 err = 0;
509 rd = (uio->uio_rw == UIO_READ)? 1 : 0;
510 if (count > uio->uio_resid)
511 return EINVAL;
512
513 if (count > (rd? sndbuf_getready(b) : sndbuf_getfree(b))) {
514 return EINVAL;
515 }
516
517 while (err == 0 && count > 0) {
518 p = rd? sndbuf_getreadyptr(b) : sndbuf_getfreeptr(b);
519 c = MIN(count, sndbuf_getsize(b) - p);
520 x = uio->uio_resid;
521 err = uiomove(sndbuf_getbufofs(b, p), c, uio);
522 x -= uio->uio_resid;
523 count -= x;
524 x = rd? sndbuf_dispose(b, NULL, x) : sndbuf_acquire(b, NULL, x);
525 }
526
527 return 0;
528}
529
530/* count is number of bytes we want added to destination buffer */
531int
532sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count)
533{
534 KASSERT(count > 0, ("can't feed 0 bytes"));
535
536 if (sndbuf_getfree(to) < count)
537 return EINVAL;
538
539 count = FEEDER_FEED(feeder, channel, to->tmpbuf, count, from);
540 if (count)
541 sndbuf_acquire(to, to->tmpbuf, count);
542 /* the root feeder has called sndbuf_dispose(from, , bytes fetched) */
543
544 return 0;
545}
546
547/************************************************************/
548
549void
550sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what)
551{
552 printf("%s: [", s);
553 if (what & 0x01)
554 printf(" bufsize: %d, maxsize: %d", b->bufsize, b->maxsize);
555 if (what & 0x02)
556 printf(" dl: %d, rp: %d, rl: %d, hp: %d", b->dl, b->rp, b->rl, b->hp);
557 if (what & 0x04)
558 printf(" total: %d, prev_total: %d, xrun: %d", b->total, b->prev_total, b->xrun);
559 if (what & 0x08)
560 printf(" fmt: 0x%x, spd: %d", b->fmt, b->spd);
561 if (what & 0x10)
562 printf(" blksz: %d, blkcnt: %d, flags: 0x%x", b->blksz, b->blkcnt, b->flags);
563 printf(" ]\n");
564}
565
566/************************************************************/
567u_int32_t
568sndbuf_getflags(struct snd_dbuf *b)
569{
570 return b->flags;
571}
572
573void
574sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on)
575{
576 b->flags &= ~flags;
577 if (on)
578 b->flags |= flags;
579}
580
581/************************************************************/
582
583int
584sndbuf_isadmasetup(struct snd_dbuf *b, struct resource *drq)
585{
586 /* should do isa_dma_acquire/isa_dma_release here */
587 if (drq == NULL) {
588 b->isadmachan = -1;
589 } else {
590 sndbuf_setflags(b, SNDBUF_F_ISADMA, 1);
591 b->isadmachan = rman_get_start(drq);
592 }
593 return 0;
594}
595
596int
597sndbuf_isadmasetdir(struct snd_dbuf *b, int dir)
598{
599 KASSERT(b, ("sndbuf_isadmasetdir called with b == NULL"));
600 KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadmasetdir called on non-ISA buffer"));
601
602 b->dir = (dir == PCMDIR_PLAY)? ISADMA_WRITE : ISADMA_READ;
603 return 0;
604}
605
606void
607sndbuf_isadma(struct snd_dbuf *b, int go)
608{
609 KASSERT(b, ("sndbuf_isadma called with b == NULL"));
610 KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadma called on non-ISA buffer"));
611
612 switch (go) {
613 case PCMTRIG_START:
614 /* isa_dmainit(b->chan, size); */
615 isa_dmastart(b->dir | ISADMA_RAW, b->buf, b->bufsize, b->isadmachan);
616 break;
617
618 case PCMTRIG_STOP:
619 case PCMTRIG_ABORT:
620 isa_dmastop(b->isadmachan);
621 isa_dmadone(b->dir | ISADMA_RAW, b->buf, b->bufsize, b->isadmachan);
622 break;
623 }
624
625 DEB(printf("buf 0x%p ISA DMA %s, channel %d\n",
626 b,
627 (go == PCMTRIG_START)? "started" : "stopped",
628 b->isadmachan));
629}
630
631int
632sndbuf_isadmaptr(struct snd_dbuf *b)
633{
634 int i;
635
636 KASSERT(b, ("sndbuf_isadmaptr called with b == NULL"));
637 KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadmaptr called on non-ISA buffer"));
638
639 if (!sndbuf_runsz(b))
640 return 0;
641 i = isa_dmastatus(b->isadmachan);
642 KASSERT(i >= 0, ("isa_dmastatus returned %d", i));
643 return b->bufsize - i;
644}
645
646void
647sndbuf_isadmabounce(struct snd_dbuf *b)
648{
649 KASSERT(b, ("sndbuf_isadmabounce called with b == NULL"));
650 KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadmabounce called on non-ISA buffer"));
651
652 /* tell isa_dma to bounce data in/out */
653}
654