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