1 /* $OpenBSD: audio.c,v 1.46 2005/06/02 19:04:18 joris Exp $ */
2 /* $NetBSD: audio.c,v 1.119 1999/11/09 16:50:47 augustss Exp $ */
3
4 /*
5 * Copyright (c) 1991-1993 Regents of the University of California.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the Computer Systems
19 * Engineering Group at Lawrence Berkeley Laboratory.
20 * 4. Neither the name of the University nor of the Laboratory may be used
21 * to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 /*
38 * This is a (partially) SunOS-compatible /dev/audio driver for NetBSD.
39 *
40 * This code tries to do something half-way sensible with
41 * half-duplex hardware, such as with the SoundBlaster hardware. With
42 * half-duplex hardware allowing O_RDWR access doesn't really make
43 * sense. However, closing and opening the device to "turn around the
44 * line" is relatively expensive and costs a card reset (which can
45 * take some time, at least for the SoundBlaster hardware). Instead
46 * we allow O_RDWR access, and provide an ioctl to set the "mode",
47 * i.e. playing or recording.
48 *
49 * If you write to a half-duplex device in record mode, the data is
50 * tossed. If you read from the device in play mode, you get silence
51 * filled buffers at the rate at which samples are naturally
52 * generated.
53 *
54 * If you try to set both play and record mode on a half-duplex
55 * device, playing takes precedence.
56 */
57
58 /*
59 * Todo:
60 * - Add softaudio() isr processing for wakeup, poll, signals,
61 * and silence fill.
62 */
63
64 #include "audio.h"
65 #if NAUDIO > 0
66
67 #include <sys/param.h>
68 #include <sys/ioctl.h>
69 #include <sys/fcntl.h>
70 #include <sys/vnode.h>
71 #include <sys/select.h>
72 #include <sys/poll.h>
73 #include <sys/malloc.h>
74 #include <sys/proc.h>
75 #include <sys/systm.h>
76 #include <sys/syslog.h>
77 #include <sys/kernel.h>
78 #include <sys/signalvar.h>
79 #include <sys/conf.h>
80 #include <sys/audioio.h>
81 #include <sys/device.h>
82
83 #include <dev/audio_if.h>
84 #include <dev/audiovar.h>
85
86 #include <dev/rndvar.h>
87
88 #include <machine/endian.h>
89
90 #ifdef AUDIO_DEBUG
91 #define DPRINTF(x) if (audiodebug) printf x
92 #define DPRINTFN(n,x) if (audiodebug>(n)) printf x
93 int audiodebug = 0;
94 #else
95 #define DPRINTF(x)
96 #define DPRINTFN(n,x)
97 #endif
98
99 #define ROUNDSIZE(x) x &= -16 /* round to nice boundary */
100
101 int audio_blk_ms = AUDIO_BLK_MS;
102
103 int audiosetinfo(struct audio_softc *, struct audio_info *);
104 int audiogetinfo(struct audio_softc *, struct audio_info *);
105
106 int audio_open(dev_t, struct audio_softc *, int, int, struct proc *);
107 int audio_close(dev_t, int, int, struct proc *);
108 int audio_read(dev_t, struct uio *, int);
109 int audio_write(dev_t, struct uio *, int);
110 int audio_ioctl(dev_t, u_long, caddr_t, int, struct proc *);
111 int audio_poll(dev_t, int, struct proc *);
112 paddr_t audio_mmap(dev_t, off_t, int);
113
114 int mixer_open(dev_t, struct audio_softc *, int, int, struct proc *);
115 int mixer_close(dev_t, int, int, struct proc *);
116 int mixer_ioctl(dev_t, u_long, caddr_t, int, struct proc *);
117 static void mixer_remove(struct audio_softc *, struct proc *p);
118 static void mixer_signal(struct audio_softc *);
119
120 void audio_init_record(struct audio_softc *);
121 void audio_init_play(struct audio_softc *);
122 int audiostartr(struct audio_softc *);
123 int audiostartp(struct audio_softc *);
124 void audio_rint(void *);
125 void audio_pint(void *);
126 int audio_check_params(struct audio_params *);
127
128 void audio_calc_blksize(struct audio_softc *, int);
129 void audio_fill_silence(struct audio_params *, u_char *, int);
130 int audio_silence_copyout(struct audio_softc *, int, struct uio *);
131
132 void audio_init_ringbuffer(struct audio_ringbuffer *);
133 int audio_initbufs(struct audio_softc *);
134 void audio_calcwater(struct audio_softc *);
135 static __inline int audio_sleep_timo(int *, char *, int);
136 static __inline int audio_sleep(int *, char *);
137 static __inline void audio_wakeup(int *);
138 void audio_selwakeup(struct audio_softc *sc, int play);
139 int audio_drain(struct audio_softc *);
140 void audio_clear(struct audio_softc *);
141 static __inline void audio_pint_silence(struct audio_softc *, struct audio_ringbuffer *, u_char *, int);
142
143 int audio_alloc_ring(struct audio_softc *, struct audio_ringbuffer *, int, int);
144 void audio_free_ring(struct audio_softc *, struct audio_ringbuffer *);
145
146 int audioprint(void *, const char *);
147
148 #define __BROKEN_INDIRECT_CONFIG /* XXX */
149 #ifdef __BROKEN_INDIRECT_CONFIG
150 int audioprobe(struct device *, void *, void *);
151 #else
152 int audioprobe(struct device *, struct cfdata *, void *);
153 #endif
154 void audioattach(struct device *, struct device *, void *);
155 int audiodetach(struct device *, int);
156 int audioactivate(struct device *, enum devact);
157
158 struct portname {
159 char *name;
160 int mask;
161 };
162 static struct portname itable[] = {
163 { AudioNmicrophone, AUDIO_MICROPHONE },
164 { AudioNline, AUDIO_LINE_IN },
165 { AudioNcd, AUDIO_CD },
166 { 0 }
167 };
168 static struct portname otable[] = {
169 { AudioNspeaker, AUDIO_SPEAKER },
170 { AudioNheadphone, AUDIO_HEADPHONE },
171 { AudioNline, AUDIO_LINE_OUT },
172 { 0 }
173 };
174 void au_check_ports(struct audio_softc *, struct au_mixer_ports *,
175 mixer_devinfo_t *, int, char *, char *,
176 struct portname *);
177 int au_set_gain(struct audio_softc *, struct au_mixer_ports *,
178 int, int);
179 void au_get_gain(struct audio_softc *, struct au_mixer_ports *,
180 u_int *, u_char *);
181 int au_set_port(struct audio_softc *, struct au_mixer_ports *,
182 u_int);
183 int au_get_port(struct audio_softc *, struct au_mixer_ports *);
184 int au_get_lr_value(struct audio_softc *, mixer_ctrl_t *,
185 int *, int *r);
186 int au_set_lr_value(struct audio_softc *, mixer_ctrl_t *,
187 int, int);
188 int au_portof(struct audio_softc *, char *);
189
190
191 /* The default audio mode: 8 kHz mono ulaw */
192 struct audio_params audio_default =
193 { 8000, AUDIO_ENCODING_ULAW, 8, 1, 0, 1 };
194
195 struct cfattach audio_ca = {
196 sizeof(struct audio_softc), audioprobe, audioattach,
197 audiodetach, audioactivate
198 };
199
200 struct cfdriver audio_cd = {
201 NULL, "audio", DV_DULL
202 };
203
204 void filt_audiowdetach(struct knote *kn);
205 int filt_audiowrite(struct knote *kn, long hint);
206
207 struct filterops audiowrite_filtops =
208 { 1, NULL, filt_audiowdetach, filt_audiowrite};
209
210 void filt_audiordetach(struct knote *kn);
211 int filt_audioread(struct knote *kn, long hint);
212
213 struct filterops audioread_filtops =
214 { 1, NULL, filt_audiordetach, filt_audioread};
215
216 int
audioprobe(parent,match,aux)217 audioprobe(parent, match, aux)
218 struct device *parent;
219 #ifdef __BROKEN_INDIRECT_CONFIG
220 void *match;
221 #else
222 struct cfdata *match;
223 #endif
224 void *aux;
225 {
226 struct audio_attach_args *sa = aux;
227
228 DPRINTF(("audioprobe: type=%d sa=%p hw=%p\n",
229 sa->type, sa, sa->hwif));
230 return (sa->type == AUDIODEV_TYPE_AUDIO) ? 1 : 0;
231 }
232
233 void
audioattach(parent,self,aux)234 audioattach(parent, self, aux)
235 struct device *parent, *self;
236 void *aux;
237 {
238 struct audio_softc *sc = (void *)self;
239 struct audio_attach_args *sa = aux;
240 struct audio_hw_if *hwp = sa->hwif;
241 void *hdlp = sa->hdl;
242 int error;
243 mixer_devinfo_t mi;
244 int iclass, oclass;
245
246 printf("\n");
247
248 #ifdef DIAGNOSTIC
249 if (hwp == 0 ||
250 hwp->open == 0 ||
251 hwp->close == 0 ||
252 hwp->query_encoding == 0 ||
253 hwp->set_params == 0 ||
254 (hwp->start_output == 0 && hwp->trigger_output == 0) ||
255 (hwp->start_input == 0 && hwp->trigger_input == 0) ||
256 hwp->halt_output == 0 ||
257 hwp->halt_input == 0 ||
258 hwp->getdev == 0 ||
259 hwp->set_port == 0 ||
260 hwp->get_port == 0 ||
261 hwp->query_devinfo == 0 ||
262 hwp->get_props == 0) {
263 printf("audio: missing method\n");
264 sc->hw_if = 0;
265 return;
266 }
267 #endif
268
269 sc->hw_if = hwp;
270 sc->hw_hdl = hdlp;
271 sc->sc_dev = parent;
272
273 error = audio_alloc_ring(sc, &sc->sc_pr, AUMODE_PLAY, AU_RING_SIZE);
274 if (error) {
275 sc->hw_if = 0;
276 printf("audio: could not allocate play buffer\n");
277 return;
278 }
279 error = audio_alloc_ring(sc, &sc->sc_rr, AUMODE_RECORD, AU_RING_SIZE);
280 if (error) {
281 audio_free_ring(sc, &sc->sc_pr);
282 sc->hw_if = 0;
283 printf("audio: could not allocate record buffer\n");
284 return;
285 }
286
287 /*
288 * Set default softc params
289 */
290 sc->sc_pparams = audio_default;
291 sc->sc_rparams = audio_default;
292
293 /* Set up some default values */
294 sc->sc_blkset = 0;
295 audio_calc_blksize(sc, AUMODE_RECORD);
296 audio_calc_blksize(sc, AUMODE_PLAY);
297 audio_init_ringbuffer(&sc->sc_rr);
298 audio_init_ringbuffer(&sc->sc_pr);
299 audio_calcwater(sc);
300
301 iclass = oclass = -1;
302 sc->sc_inports.index = -1;
303 sc->sc_inports.nports = 0;
304 sc->sc_inports.isenum = 0;
305 sc->sc_inports.allports = 0;
306 sc->sc_outports.index = -1;
307 sc->sc_outports.nports = 0;
308 sc->sc_outports.isenum = 0;
309 sc->sc_outports.allports = 0;
310 sc->sc_monitor_port = -1;
311 for(mi.index = 0; ; mi.index++) {
312 if (hwp->query_devinfo(hdlp, &mi) != 0)
313 break;
314 if (mi.type == AUDIO_MIXER_CLASS &&
315 strcmp(mi.label.name, AudioCrecord) == 0)
316 iclass = mi.index;
317 if (mi.type == AUDIO_MIXER_CLASS &&
318 strcmp(mi.label.name, AudioCmonitor) == 0)
319 oclass = mi.index;
320 }
321 for(mi.index = 0; ; mi.index++) {
322 if (hwp->query_devinfo(hdlp, &mi) != 0)
323 break;
324 if (mi.type == AUDIO_MIXER_CLASS)
325 continue;
326 au_check_ports(sc, &sc->sc_inports, &mi, iclass,
327 AudioNsource, AudioNrecord, itable);
328 au_check_ports(sc, &sc->sc_outports, &mi, oclass,
329 AudioNoutput, AudioNmaster, otable);
330 if (mi.mixer_class == oclass &&
331 (strcmp(mi.label.name, AudioNmonitor) == 0))
332 sc->sc_monitor_port = mi.index;
333 }
334 DPRINTF(("audio_attach: inputs ports=0x%x, output ports=0x%x\n",
335 sc->sc_inports.allports, sc->sc_outports.allports));
336 }
337
338 int
audioactivate(self,act)339 audioactivate(self, act)
340 struct device *self;
341 enum devact act;
342 {
343 struct audio_softc *sc = (struct audio_softc *)self;
344
345 switch (act) {
346 case DVACT_ACTIVATE:
347 return (EOPNOTSUPP);
348 break;
349
350 case DVACT_DEACTIVATE:
351 sc->sc_dying = 1;
352 break;
353 }
354 return (0);
355 }
356
357 int
audiodetach(self,flags)358 audiodetach(self, flags)
359 struct device *self;
360 int flags;
361 {
362 struct audio_softc *sc = (struct audio_softc *)self;
363 int maj, mn;
364 int s;
365
366 DPRINTF(("audio_detach: sc=%p flags=%d\n", sc, flags));
367
368 sc->sc_dying = 1;
369
370 wakeup(&sc->sc_wchan);
371 wakeup(&sc->sc_rchan);
372 s = splaudio();
373 if (--sc->sc_refcnt >= 0) {
374 if (tsleep(&sc->sc_refcnt, PZERO, "auddet", hz * 120))
375 printf("audiodetach: %s didn't detach\n",
376 sc->dev.dv_xname);
377 }
378 splx(s);
379
380 /* free resources */
381 audio_free_ring(sc, &sc->sc_pr);
382 audio_free_ring(sc, &sc->sc_rr);
383
384 /* locate the major number */
385 for (maj = 0; maj < nchrdev; maj++)
386 if (cdevsw[maj].d_open == audioopen)
387 break;
388
389 /* Nuke the vnodes for any open instances (calls close). */
390 mn = self->dv_unit;
391 vdevgone(maj, mn | SOUND_DEVICE, mn | SOUND_DEVICE, VCHR);
392 vdevgone(maj, mn | AUDIO_DEVICE, mn | AUDIO_DEVICE, VCHR);
393 vdevgone(maj, mn | AUDIOCTL_DEVICE, mn | AUDIOCTL_DEVICE, VCHR);
394 vdevgone(maj, mn | MIXER_DEVICE, mn | MIXER_DEVICE, VCHR);
395
396 return (0);
397 }
398
399 int
au_portof(sc,name)400 au_portof(sc, name)
401 struct audio_softc *sc;
402 char *name;
403 {
404 mixer_devinfo_t mi;
405
406 for(mi.index = 0;
407 sc->hw_if->query_devinfo(sc->hw_hdl, &mi) == 0;
408 mi.index++)
409 if (strcmp(mi.label.name, name) == 0)
410 return mi.index;
411 return -1;
412 }
413
414 void
au_check_ports(sc,ports,mi,cls,name,mname,tbl)415 au_check_ports(sc, ports, mi, cls, name, mname, tbl)
416 struct audio_softc *sc;
417 struct au_mixer_ports *ports;
418 mixer_devinfo_t *mi;
419 int cls;
420 char *name;
421 char *mname;
422 struct portname *tbl;
423 {
424 int i, j;
425
426 if (mi->mixer_class != cls)
427 return;
428 if (strcmp(mi->label.name, mname) == 0) {
429 ports->master = mi->index;
430 return;
431 }
432 if (strcmp(mi->label.name, name) != 0)
433 return;
434 if (mi->type == AUDIO_MIXER_ENUM) {
435 ports->index = mi->index;
436 for(i = 0; tbl[i].name; i++) {
437 for(j = 0; j < mi->un.e.num_mem; j++) {
438 if (strcmp(mi->un.e.member[j].label.name,
439 tbl[i].name) == 0) {
440 ports->aumask[ports->nports] = tbl[i].mask;
441 ports->misel [ports->nports] = mi->un.e.member[j].ord;
442 ports->miport[ports->nports++] =
443 au_portof(sc, mi->un.e.member[j].label.name);
444 ports->allports |= tbl[i].mask;
445 }
446 }
447 }
448 ports->isenum = 1;
449 } else if (mi->type == AUDIO_MIXER_SET) {
450 ports->index = mi->index;
451 for(i = 0; tbl[i].name; i++) {
452 for(j = 0; j < mi->un.s.num_mem; j++) {
453 if (strcmp(mi->un.s.member[j].label.name,
454 tbl[i].name) == 0) {
455 ports->aumask[ports->nports] = tbl[i].mask;
456 ports->misel [ports->nports] = mi->un.s.member[j].mask;
457 ports->miport[ports->nports++] =
458 au_portof(sc, mi->un.s.member[j].label.name);
459 ports->allports |= tbl[i].mask;
460 }
461 }
462 }
463 }
464 }
465
466 /*
467 * Called from hardware driver. This is where the MI audio driver gets
468 * probed/attached to the hardware driver.
469 */
470 struct device *
audio_attach_mi(ahwp,hdlp,dev)471 audio_attach_mi(ahwp, hdlp, dev)
472 struct audio_hw_if *ahwp;
473 void *hdlp;
474 struct device *dev;
475 {
476 struct audio_attach_args arg;
477
478 #ifdef DIAGNOSTIC
479 if (ahwp == NULL) {
480 printf ("audio_attach_mi: NULL\n");
481 return 0;
482 }
483 #endif
484
485 arg.type = AUDIODEV_TYPE_AUDIO;
486 arg.hwif = ahwp;
487 arg.hdl = hdlp;
488 return config_found(dev, &arg, audioprint);
489 }
490
491 #if NAUDIO > 0
492 int
audioprint(aux,pnp)493 audioprint(aux, pnp)
494 void *aux;
495 const char *pnp;
496 {
497 struct audio_attach_args *arg = aux;
498 const char *type;
499
500 if (pnp != NULL) {
501 switch (arg->type) {
502 case AUDIODEV_TYPE_AUDIO:
503 type = "audio";
504 break;
505 case AUDIODEV_TYPE_OPL:
506 type = "opl";
507 break;
508 case AUDIODEV_TYPE_MPU:
509 type = "mpu";
510 break;
511 default:
512 panic("audioprint: unknown type %d", arg->type);
513 }
514 printf("%s at %s", type, pnp);
515 }
516 return (UNCONF);
517 }
518
519 #endif /* NAUDIO > 0 */
520
521 #ifdef AUDIO_DEBUG
522 void audio_printsc(struct audio_softc *);
523 void audio_print_params(char *, struct audio_params *);
524
525 void
audio_printsc(sc)526 audio_printsc(sc)
527 struct audio_softc *sc;
528 {
529 printf("hwhandle %p hw_if %p ", sc->hw_hdl, sc->hw_if);
530 printf("open 0x%x mode 0x%x\n", sc->sc_open, sc->sc_mode);
531 printf("rchan 0x%x wchan 0x%x ", sc->sc_rchan, sc->sc_wchan);
532 printf("rring used 0x%x pring used=%d\n", sc->sc_rr.used, sc->sc_pr.used);
533 printf("rbus 0x%x pbus 0x%x ", sc->sc_rbus, sc->sc_pbus);
534 printf("blksize %d", sc->sc_pr.blksize);
535 printf("hiwat %d lowat %d\n", sc->sc_pr.usedhigh, sc->sc_pr.usedlow);
536 }
537
538 void
audio_print_params(s,p)539 audio_print_params(s, p)
540 char *s;
541 struct audio_params *p;
542 {
543 printf("audio: %s sr=%ld, enc=%d, chan=%d, prec=%d\n", s,
544 p->sample_rate, p->encoding, p->channels, p->precision);
545 }
546 #endif
547
548 int
audio_alloc_ring(sc,r,direction,bufsize)549 audio_alloc_ring(sc, r, direction, bufsize)
550 struct audio_softc *sc;
551 struct audio_ringbuffer *r;
552 int direction;
553 int bufsize;
554 {
555 struct audio_hw_if *hw = sc->hw_if;
556 void *hdl = sc->hw_hdl;
557 /*
558 * Alloc DMA play and record buffers
559 */
560 if (bufsize < AUMINBUF)
561 bufsize = AUMINBUF;
562 ROUNDSIZE(bufsize);
563 if (hw->round_buffersize)
564 bufsize = hw->round_buffersize(hdl, direction, bufsize);
565 r->bufsize = bufsize;
566 if (hw->allocm)
567 r->start = hw->allocm(hdl, direction, r->bufsize, M_DEVBUF,
568 M_WAITOK);
569 else
570 r->start = malloc(bufsize, M_DEVBUF, M_WAITOK);
571 if (r->start == 0)
572 return ENOMEM;
573 return 0;
574 }
575
576 void
audio_free_ring(sc,r)577 audio_free_ring(sc, r)
578 struct audio_softc *sc;
579 struct audio_ringbuffer *r;
580 {
581 if (sc->hw_if->freem) {
582 sc->hw_if->freem(sc->hw_hdl, r->start, M_DEVBUF);
583 } else {
584 free(r->start, M_DEVBUF);
585 }
586 }
587
588 int
audioopen(dev,flags,ifmt,p)589 audioopen(dev, flags, ifmt, p)
590 dev_t dev;
591 int flags, ifmt;
592 struct proc *p;
593 {
594 int unit = AUDIOUNIT(dev);
595 struct audio_softc *sc;
596 int error;
597
598 if (unit >= audio_cd.cd_ndevs ||
599 (sc = audio_cd.cd_devs[unit]) == NULL)
600 return ENXIO;
601
602 if (sc->sc_dying)
603 return (EIO);
604
605 if (!sc->hw_if)
606 return (ENXIO);
607
608 sc->sc_refcnt ++;
609 switch (AUDIODEV(dev)) {
610 case SOUND_DEVICE:
611 case AUDIO_DEVICE:
612 case AUDIOCTL_DEVICE:
613 error = audio_open(dev, sc, flags, ifmt, p);
614 break;
615 case MIXER_DEVICE:
616 error = mixer_open(dev, sc, flags, ifmt, p);
617 break;
618 default:
619 error = ENXIO;
620 break;
621 }
622
623 if (--sc->sc_refcnt < 0)
624 wakeup(&sc->sc_refcnt);
625
626 return (error);
627 }
628
629 int
audioclose(dev,flags,ifmt,p)630 audioclose(dev, flags, ifmt, p)
631 dev_t dev;
632 int flags, ifmt;
633 struct proc *p;
634 {
635
636 switch (AUDIODEV(dev)) {
637 case SOUND_DEVICE:
638 case AUDIO_DEVICE:
639 return (audio_close(dev, flags, ifmt, p));
640 case MIXER_DEVICE:
641 return (mixer_close(dev, flags, ifmt, p));
642 case AUDIOCTL_DEVICE:
643 return 0;
644 default:
645 return (ENXIO);
646 }
647 }
648
649 int
audioread(dev,uio,ioflag)650 audioread(dev, uio, ioflag)
651 dev_t dev;
652 struct uio *uio;
653 int ioflag;
654 {
655 int unit = AUDIOUNIT(dev);
656 struct audio_softc *sc;
657 int error;
658
659 if (unit >= audio_cd.cd_ndevs ||
660 (sc = audio_cd.cd_devs[unit]) == NULL)
661 return ENXIO;
662
663 if (sc->sc_dying)
664 return (EIO);
665
666 sc->sc_refcnt ++;
667 switch (AUDIODEV(dev)) {
668 case SOUND_DEVICE:
669 case AUDIO_DEVICE:
670 error = audio_read(dev, uio, ioflag);
671 break;
672 case AUDIOCTL_DEVICE:
673 case MIXER_DEVICE:
674 error = ENODEV;
675 break;
676 default:
677 error = ENXIO;
678 break;
679 }
680
681 if (--sc->sc_refcnt < 0)
682 wakeup(&sc->sc_refcnt);
683 return (error);
684 }
685
686 int
audiowrite(dev,uio,ioflag)687 audiowrite(dev, uio, ioflag)
688 dev_t dev;
689 struct uio *uio;
690 int ioflag;
691 {
692 int unit = AUDIOUNIT(dev);
693 struct audio_softc *sc;
694 int error;
695
696 if (unit >= audio_cd.cd_ndevs ||
697 (sc = audio_cd.cd_devs[unit]) == NULL)
698 return ENXIO;
699
700 if (sc->sc_dying)
701 return (EIO);
702
703 sc->sc_refcnt ++;
704 switch (AUDIODEV(dev)) {
705 case SOUND_DEVICE:
706 case AUDIO_DEVICE:
707 error = audio_write(dev, uio, ioflag);
708 break;
709 case AUDIOCTL_DEVICE:
710 case MIXER_DEVICE:
711 error = ENODEV;
712 break;
713 default:
714 error = ENXIO;
715 break;
716 }
717
718 if (--sc->sc_refcnt < 0)
719 wakeup(&sc->sc_refcnt);
720 return (error);
721 }
722
723 int
audioioctl(dev,cmd,addr,flag,p)724 audioioctl(dev, cmd, addr, flag, p)
725 dev_t dev;
726 u_long cmd;
727 caddr_t addr;
728 int flag;
729 struct proc *p;
730 {
731 int unit = AUDIOUNIT(dev);
732 struct audio_softc *sc;
733 int error;
734
735 if (unit >= audio_cd.cd_ndevs ||
736 (sc = audio_cd.cd_devs[unit]) == NULL)
737 return ENXIO;
738
739 if (sc->sc_dying)
740 return (EIO);
741
742 sc->sc_refcnt ++;
743 switch (AUDIODEV(dev)) {
744 case SOUND_DEVICE:
745 case AUDIO_DEVICE:
746 case AUDIOCTL_DEVICE:
747 error = audio_ioctl(dev, cmd, addr, flag, p);
748 break;
749 case MIXER_DEVICE:
750 error = mixer_ioctl(dev, cmd, addr, flag, p);
751 break;
752 default:
753 error = ENXIO;
754 break;
755 }
756
757 if (--sc->sc_refcnt < 0)
758 wakeup(&sc->sc_refcnt);
759 return (error);
760 }
761
762 int
audiopoll(dev,events,p)763 audiopoll(dev, events, p)
764 dev_t dev;
765 int events;
766 struct proc *p;
767 {
768 int unit = AUDIOUNIT(dev);
769 struct audio_softc *sc;
770 int error;
771
772 if (unit >= audio_cd.cd_ndevs ||
773 (sc = audio_cd.cd_devs[unit]) == NULL)
774 return ENXIO;
775
776 if (sc->sc_dying)
777 return (EIO);
778
779 sc->sc_refcnt ++;
780 switch (AUDIODEV(dev)) {
781 case SOUND_DEVICE:
782 case AUDIO_DEVICE:
783 error = audio_poll(dev, events, p);
784 break;
785 case AUDIOCTL_DEVICE:
786 case MIXER_DEVICE:
787 error = 0;
788 break;
789 default:
790 error = 0;
791 break;
792 }
793
794 if (--sc->sc_refcnt < 0)
795 wakeup(&sc->sc_refcnt);
796 return (error);
797 }
798
799 paddr_t
audiommap(dev,off,prot)800 audiommap(dev, off, prot)
801 dev_t dev;
802 off_t off;
803 int prot;
804 {
805 int unit = AUDIOUNIT(dev);
806 struct audio_softc *sc;
807 int ret;
808
809 if (unit >= audio_cd.cd_ndevs ||
810 (sc = audio_cd.cd_devs[unit]) == NULL)
811 return (-1);
812
813 if (sc->sc_dying)
814 return (-1);
815
816 sc->sc_refcnt ++;
817 switch (AUDIODEV(dev)) {
818 case SOUND_DEVICE:
819 case AUDIO_DEVICE:
820 ret = audio_mmap(dev, off, prot);
821 break;
822 case AUDIOCTL_DEVICE:
823 case MIXER_DEVICE:
824 ret = -1;
825 break;
826 default:
827 ret = -1;
828 break;
829 }
830
831 if (--sc->sc_refcnt < 0)
832 wakeup(&sc->sc_refcnt);
833 return (ret);
834 }
835
836 /*
837 * Audio driver
838 */
839 void
audio_init_ringbuffer(rp)840 audio_init_ringbuffer(rp)
841 struct audio_ringbuffer *rp;
842 {
843 int nblks;
844 int blksize = rp->blksize;
845
846 if (blksize < AUMINBLK)
847 blksize = AUMINBLK;
848 nblks = rp->bufsize / blksize;
849 if (nblks < AUMINNOBLK) {
850 nblks = AUMINNOBLK;
851 blksize = rp->bufsize / nblks;
852 ROUNDSIZE(blksize);
853 }
854 DPRINTF(("audio_init_ringbuffer: blksize=%d\n", blksize));
855 rp->blksize = blksize;
856 rp->maxblks = nblks;
857 rp->used = 0;
858 rp->end = rp->start + nblks * blksize;
859 rp->inp = rp->outp = rp->start;
860 rp->stamp = 0;
861 rp->drops = 0;
862 rp->pause = 0;
863 rp->copying = 0;
864 rp->needfill = 0;
865 rp->mmapped = 0;
866 }
867
868 int
audio_initbufs(sc)869 audio_initbufs(sc)
870 struct audio_softc *sc;
871 {
872 struct audio_hw_if *hw = sc->hw_if;
873 int error;
874
875 DPRINTF(("audio_initbufs: mode=0x%x\n", sc->sc_mode));
876 audio_init_ringbuffer(&sc->sc_rr);
877 if (hw->init_input && (sc->sc_mode & AUMODE_RECORD)) {
878 error = hw->init_input(sc->hw_hdl, sc->sc_rr.start,
879 sc->sc_rr.end - sc->sc_rr.start);
880 if (error)
881 return error;
882 }
883
884 audio_init_ringbuffer(&sc->sc_pr);
885 sc->sc_sil_count = 0;
886 if (hw->init_output && (sc->sc_mode & AUMODE_PLAY)) {
887 error = hw->init_output(sc->hw_hdl, sc->sc_pr.start,
888 sc->sc_pr.end - sc->sc_pr.start);
889 if (error)
890 return error;
891 }
892
893 #ifdef AUDIO_INTR_TIME
894 sc->sc_pnintr = 0;
895 sc->sc_pblktime = (u_long)(
896 (u_long)sc->sc_pr.blksize * 100000 /
897 (u_long)(sc->sc_pparams.precision / NBBY *
898 sc->sc_pparams.channels *
899 sc->sc_pparams.sample_rate)) * 10;
900 DPRINTF(("audio: play blktime = %lu for %d\n",
901 sc->sc_pblktime, sc->sc_pr.blksize));
902 sc->sc_rnintr = 0;
903 sc->sc_rblktime = (u_long)(
904 (u_long)sc->sc_rr.blksize * 100000 /
905 (u_long)(sc->sc_rparams.precision / NBBY *
906 sc->sc_rparams.channels *
907 sc->sc_rparams.sample_rate)) * 10;
908 DPRINTF(("audio: record blktime = %lu for %d\n",
909 sc->sc_rblktime, sc->sc_rr.blksize));
910 #endif
911
912 return 0;
913 }
914
915 void
audio_calcwater(sc)916 audio_calcwater(sc)
917 struct audio_softc *sc;
918 {
919 sc->sc_pr.usedhigh = sc->sc_pr.end - sc->sc_pr.start;
920 sc->sc_pr.usedlow = sc->sc_pr.usedhigh * 3 / 4; /* set lowater at 75% */
921 if (sc->sc_pr.usedlow == sc->sc_pr.usedhigh)
922 sc->sc_pr.usedlow -= sc->sc_pr.blksize;
923 sc->sc_rr.usedhigh = sc->sc_rr.end - sc->sc_rr.start - sc->sc_rr.blksize;
924 sc->sc_rr.usedlow = 0;
925 }
926
927 static __inline int
audio_sleep_timo(chan,label,timo)928 audio_sleep_timo(chan, label, timo)
929 int *chan;
930 char *label;
931 int timo;
932 {
933 int st;
934
935 if (!label)
936 label = "audio";
937
938 DPRINTFN(3, ("audio_sleep_timo: chan=%p, label=%s, timo=%d\n",
939 chan, label, timo));
940 *chan = 1;
941 st = tsleep(chan, PWAIT | PCATCH, label, timo);
942 *chan = 0;
943 #ifdef AUDIO_DEBUG
944 if (st != 0)
945 printf("audio_sleep: woke up st=%d\n", st);
946 #endif
947 return (st);
948 }
949
950 static __inline int
audio_sleep(chan,label)951 audio_sleep(chan, label)
952 int *chan;
953 char *label;
954 {
955 return audio_sleep_timo(chan, label, 0);
956 }
957
958 /* call at splaudio() */
959 static __inline void
audio_wakeup(chan)960 audio_wakeup(chan)
961 int *chan;
962 {
963 DPRINTFN(3, ("audio_wakeup: chan=%p, *chan=%d\n", chan, *chan));
964 if (*chan) {
965 wakeup(chan);
966 *chan = 0;
967 }
968 }
969
970 int
audio_open(dev,sc,flags,ifmt,p)971 audio_open(dev, sc, flags, ifmt, p)
972 dev_t dev;
973 struct audio_softc *sc;
974 int flags, ifmt;
975 struct proc *p;
976 {
977 int error;
978 int mode;
979 struct audio_info ai;
980
981 DPRINTF(("audio_open: dev=0x%x flags=0x%x sc=%p hdl=%p\n", dev, flags, sc, sc->hw_hdl));
982
983 if (ISDEVAUDIOCTL(dev))
984 return 0;
985
986 if ((sc->sc_open & (AUOPEN_READ|AUOPEN_WRITE)) != 0)
987 return (EBUSY);
988
989 error = sc->hw_if->open(sc->hw_hdl, flags);
990 if (error)
991 return (error);
992
993 sc->sc_async_audio = 0;
994 sc->sc_rchan = 0;
995 sc->sc_wchan = 0;
996 sc->sc_blkset = 0; /* Block sizes not set yet */
997 sc->sc_sil_count = 0;
998 sc->sc_rbus = 0;
999 sc->sc_pbus = 0;
1000 sc->sc_eof = 0;
1001 sc->sc_playdrop = 0;
1002
1003 sc->sc_full_duplex = 0;
1004 /* doesn't always work right on SB.
1005 (flags & (FWRITE|FREAD)) == (FWRITE|FREAD) &&
1006 (sc->hw_if->get_props(sc->hw_hdl) & AUDIO_PROP_FULLDUPLEX);
1007 */
1008
1009 mode = 0;
1010 if (flags & FREAD) {
1011 sc->sc_open |= AUOPEN_READ;
1012 mode |= AUMODE_RECORD;
1013 }
1014 if (flags & FWRITE) {
1015 sc->sc_open |= AUOPEN_WRITE;
1016 mode |= AUMODE_PLAY | AUMODE_PLAY_ALL;
1017 }
1018
1019 /*
1020 * Multiplex device: /dev/audio (MU-Law) and /dev/sound (linear)
1021 * The /dev/audio is always (re)set to 8-bit MU-Law mono
1022 * For the other devices, you get what they were last set to.
1023 */
1024 if (ISDEVAUDIO(dev)) {
1025 /* /dev/audio */
1026 sc->sc_rparams = audio_default;
1027 sc->sc_pparams = audio_default;
1028 }
1029 #ifdef DIAGNOSTIC
1030 /*
1031 * Sample rate and precision are supposed to be set to proper
1032 * default values by the hardware driver, so that it may give
1033 * us these values.
1034 */
1035 if (sc->sc_rparams.precision == 0 || sc->sc_pparams.precision == 0) {
1036 printf("audio_open: 0 precision\n");
1037 error = EINVAL;
1038 goto bad;
1039 }
1040 #endif
1041
1042 AUDIO_INITINFO(&ai);
1043 ai.record.sample_rate = sc->sc_rparams.sample_rate;
1044 ai.record.encoding = sc->sc_rparams.encoding;
1045 ai.record.channels = sc->sc_rparams.channels;
1046 ai.record.precision = sc->sc_rparams.precision;
1047 ai.play.sample_rate = sc->sc_pparams.sample_rate;
1048 ai.play.encoding = sc->sc_pparams.encoding;
1049 ai.play.channels = sc->sc_pparams.channels;
1050 ai.play.precision = sc->sc_pparams.precision;
1051 ai.mode = mode;
1052 sc->sc_pr.blksize = sc->sc_rr.blksize = 0; /* force recalculation */
1053 error = audiosetinfo(sc, &ai);
1054 if (error)
1055 goto bad;
1056
1057 DPRINTF(("audio_open: done sc_mode = 0x%x\n", sc->sc_mode));
1058
1059 return 0;
1060
1061 bad:
1062 sc->hw_if->close(sc->hw_hdl);
1063 sc->sc_open = 0;
1064 sc->sc_mode = 0;
1065 sc->sc_full_duplex = 0;
1066 return error;
1067 }
1068
1069 /*
1070 * Must be called from task context.
1071 */
1072 void
audio_init_record(sc)1073 audio_init_record(sc)
1074 struct audio_softc *sc;
1075 {
1076 int s = splaudio();
1077
1078 if (sc->hw_if->speaker_ctl &&
1079 (!sc->sc_full_duplex || (sc->sc_mode & AUMODE_PLAY) == 0))
1080 sc->hw_if->speaker_ctl(sc->hw_hdl, SPKR_OFF);
1081 splx(s);
1082 }
1083
1084 /*
1085 * Must be called from task context.
1086 */
1087 void
audio_init_play(sc)1088 audio_init_play(sc)
1089 struct audio_softc *sc;
1090 {
1091 int s = splaudio();
1092
1093 sc->sc_wstamp = sc->sc_pr.stamp;
1094 if (sc->hw_if->speaker_ctl)
1095 sc->hw_if->speaker_ctl(sc->hw_hdl, SPKR_ON);
1096 splx(s);
1097 }
1098
1099 int
audio_drain(sc)1100 audio_drain(sc)
1101 struct audio_softc *sc;
1102 {
1103 int error, drops;
1104 struct audio_ringbuffer *cb = &sc->sc_pr;
1105 int s;
1106
1107 DPRINTF(("audio_drain: enter busy=%d used=%d\n",
1108 sc->sc_pbus, sc->sc_pr.used));
1109 if (sc->sc_pr.mmapped || sc->sc_pr.used <= 0)
1110 return 0;
1111 if (!sc->sc_pbus) {
1112 /* We've never started playing, probably because the
1113 * block was too short. Pad it and start now.
1114 */
1115 int cc;
1116 u_char *inp = cb->inp;
1117
1118 cc = cb->blksize - (inp - cb->start) % cb->blksize;
1119 audio_fill_silence(&sc->sc_pparams, inp, cc);
1120 inp += cc;
1121 if (inp >= cb->end)
1122 inp = cb->start;
1123 s = splaudio();
1124 cb->used += cc;
1125 cb->inp = inp;
1126 error = audiostartp(sc);
1127 splx(s);
1128 if (error)
1129 return error;
1130 }
1131 /*
1132 * Play until a silence block has been played, then we
1133 * know all has been drained.
1134 * XXX This should be done some other way to avoid
1135 * playing silence.
1136 */
1137 #ifdef DIAGNOSTIC
1138 if (cb->copying) {
1139 printf("audio_drain: copying in progress!?!\n");
1140 cb->copying = 0;
1141 }
1142 #endif
1143 drops = cb->drops;
1144 error = 0;
1145 s = splaudio();
1146 while (cb->drops == drops && !error) {
1147 DPRINTF(("audio_drain: used=%d, drops=%ld\n", sc->sc_pr.used, cb->drops));
1148 /*
1149 * When the process is exiting, it ignores all signals and
1150 * we can't interrupt this sleep, so we set a timeout just in case.
1151 */
1152 error = audio_sleep_timo(&sc->sc_wchan, "aud_dr", 30*hz);
1153 if (sc->sc_dying)
1154 error = EIO;
1155 }
1156 splx(s);
1157 return error;
1158 }
1159
1160 /*
1161 * Close an audio chip.
1162 */
1163 /* ARGSUSED */
1164 int
audio_close(dev,flags,ifmt,p)1165 audio_close(dev, flags, ifmt, p)
1166 dev_t dev;
1167 int flags, ifmt;
1168 struct proc *p;
1169 {
1170 int unit = AUDIOUNIT(dev);
1171 struct audio_softc *sc = audio_cd.cd_devs[unit];
1172 struct audio_hw_if *hw = sc->hw_if;
1173 int s;
1174
1175 DPRINTF(("audio_close: unit=%d flags=0x%x\n", unit, flags));
1176
1177 s = splaudio();
1178 /* Stop recording. */
1179 if ((flags & FREAD) && sc->sc_rbus) {
1180 /*
1181 * XXX Some drivers (e.g. SB) use the same routine
1182 * to halt input and output so don't halt input if
1183 * in full duplex mode. These drivers should be fixed.
1184 */
1185 if (!sc->sc_full_duplex || sc->hw_if->halt_input != sc->hw_if->halt_output)
1186 sc->hw_if->halt_input(sc->hw_hdl);
1187 sc->sc_rbus = 0;
1188 }
1189 /*
1190 * Block until output drains, but allow ^C interrupt.
1191 */
1192 sc->sc_pr.usedlow = sc->sc_pr.blksize; /* avoid excessive wakeups */
1193 /*
1194 * If there is pending output, let it drain (unless
1195 * the output is paused).
1196 */
1197 if ((flags & FWRITE) && sc->sc_pbus) {
1198 if (!sc->sc_pr.pause && !audio_drain(sc) && hw->drain)
1199 (void)hw->drain(sc->hw_hdl);
1200 sc->hw_if->halt_output(sc->hw_hdl);
1201 sc->sc_pbus = 0;
1202 }
1203
1204 hw->close(sc->hw_hdl);
1205
1206 /*
1207 * If flags has neither read nor write then reset both
1208 * directions. Encountered when someone runs revoke(2).
1209 */
1210
1211 if ((flags & FREAD) || ((flags & (FREAD|FWRITE)) == 0)) {
1212 sc->sc_open &= ~AUOPEN_READ;
1213 sc->sc_mode &= ~AUMODE_RECORD;
1214 }
1215 if ((flags & FWRITE) || ((flags & (FREAD|FWRITE)) == 0)) {
1216 sc->sc_open &= ~AUOPEN_WRITE;
1217 sc->sc_mode &= ~(AUMODE_PLAY|AUMODE_PLAY_ALL);
1218 }
1219
1220 sc->sc_async_audio = 0;
1221 sc->sc_full_duplex = 0;
1222 splx(s);
1223 DPRINTF(("audio_close: done\n"));
1224
1225 return (0);
1226 }
1227
1228 int
audio_read(dev,uio,ioflag)1229 audio_read(dev, uio, ioflag)
1230 dev_t dev;
1231 struct uio *uio;
1232 int ioflag;
1233 {
1234 int unit = AUDIOUNIT(dev);
1235 struct audio_softc *sc = audio_cd.cd_devs[unit];
1236 struct audio_ringbuffer *cb = &sc->sc_rr;
1237 u_char *outp;
1238 int error, s, used, cc, n;
1239
1240 if (cb->mmapped)
1241 return EINVAL;
1242
1243 DPRINTFN(1,("audio_read: cc=%d mode=%d\n",
1244 uio->uio_resid, sc->sc_mode));
1245
1246 error = 0;
1247 /*
1248 * If hardware is half-duplex and currently playing, return
1249 * silence blocks based on the number of blocks we have output.
1250 */
1251 if (!sc->sc_full_duplex &&
1252 (sc->sc_mode & AUMODE_PLAY)) {
1253 while (uio->uio_resid > 0 && !error) {
1254 s = splaudio();
1255 for(;;) {
1256 cc = sc->sc_pr.stamp - sc->sc_wstamp;
1257 if (cc > 0)
1258 break;
1259 DPRINTF(("audio_read: stamp=%lu, wstamp=%lu\n",
1260 sc->sc_pr.stamp, sc->sc_wstamp));
1261 if (ioflag & IO_NDELAY) {
1262 splx(s);
1263 return EWOULDBLOCK;
1264 }
1265 error = audio_sleep(&sc->sc_rchan, "aud_hr");
1266 if (sc->sc_dying)
1267 error = EIO;
1268 if (error) {
1269 splx(s);
1270 return error;
1271 }
1272 }
1273 splx(s);
1274
1275 if (uio->uio_resid < cc)
1276 cc = uio->uio_resid;
1277 DPRINTFN(1, ("audio_read: reading in write mode, cc=%d\n", cc));
1278 error = audio_silence_copyout(sc, cc, uio);
1279 sc->sc_wstamp += cc;
1280 }
1281 return (error);
1282 }
1283 while (uio->uio_resid > 0 && !error) {
1284 s = splaudio();
1285 while (cb->used <= 0) {
1286 if (!sc->sc_rbus) {
1287 error = audiostartr(sc);
1288 if (error) {
1289 splx(s);
1290 return error;
1291 }
1292 }
1293 if (ioflag & IO_NDELAY) {
1294 splx(s);
1295 return (EWOULDBLOCK);
1296 }
1297 DPRINTFN(2, ("audio_read: sleep used=%d\n", cb->used));
1298 error = audio_sleep(&sc->sc_rchan, "aud_rd");
1299 if (sc->sc_dying)
1300 error = EIO;
1301 if (error) {
1302 splx(s);
1303 return error;
1304 }
1305 }
1306 used = cb->used;
1307 outp = cb->outp;
1308 cb->copying = 1;
1309 splx(s);
1310 cc = used - cb->usedlow; /* maximum to read */
1311 n = cb->end - outp;
1312 if (n < cc)
1313 cc = n; /* don't read beyond end of buffer */
1314
1315 if (uio->uio_resid < cc)
1316 cc = uio->uio_resid; /* and no more than we want */
1317
1318 if (sc->sc_rparams.sw_code)
1319 sc->sc_rparams.sw_code(sc->hw_hdl, outp, cc);
1320 DPRINTFN(1,("audio_read: outp=%p, cc=%d\n", outp, cc));
1321 error = uiomove(outp, cc, uio);
1322 used -= cc;
1323 outp += cc;
1324 if (outp >= cb->end)
1325 outp = cb->start;
1326 s = splaudio();
1327 cb->outp = outp;
1328 cb->used = used;
1329 cb->copying = 0;
1330 splx(s);
1331 }
1332 return (error);
1333 }
1334
1335 void
audio_clear(sc)1336 audio_clear(sc)
1337 struct audio_softc *sc;
1338 {
1339 int s = splaudio();
1340
1341 if (sc->sc_rbus) {
1342 audio_wakeup(&sc->sc_rchan);
1343 sc->hw_if->halt_input(sc->hw_hdl);
1344 sc->sc_rbus = 0;
1345 }
1346 if (sc->sc_pbus) {
1347 audio_wakeup(&sc->sc_wchan);
1348 sc->hw_if->halt_output(sc->hw_hdl);
1349 sc->sc_pbus = 0;
1350 }
1351 splx(s);
1352 }
1353
1354 void
audio_calc_blksize(sc,mode)1355 audio_calc_blksize(sc, mode)
1356 struct audio_softc *sc;
1357 int mode;
1358 {
1359 struct audio_hw_if *hw = sc->hw_if;
1360 struct audio_params *parm;
1361 struct audio_ringbuffer *rb;
1362 int bs;
1363
1364 if (sc->sc_blkset)
1365 return;
1366
1367 if (mode == AUMODE_PLAY) {
1368 parm = &sc->sc_pparams;
1369 rb = &sc->sc_pr;
1370 } else {
1371 parm = &sc->sc_rparams;
1372 rb = &sc->sc_rr;
1373 }
1374
1375 bs = parm->sample_rate * audio_blk_ms / 1000 *
1376 parm->channels * parm->precision / NBBY *
1377 parm->factor;
1378 ROUNDSIZE(bs);
1379 if (hw->round_blocksize)
1380 bs = hw->round_blocksize(sc->hw_hdl, bs);
1381 rb->blksize = bs;
1382
1383 DPRINTF(("audio_calc_blksize: %s blksize=%d\n",
1384 mode == AUMODE_PLAY ? "play" : "record", bs));
1385 }
1386
1387 void
audio_fill_silence(params,p,n)1388 audio_fill_silence(params, p, n)
1389 struct audio_params *params;
1390 u_char *p;
1391 int n;
1392 {
1393 u_char auzero0, auzero1 = 0; /* initialize to please gcc */
1394 int nfill = 1;
1395
1396 switch (params->encoding) {
1397 case AUDIO_ENCODING_ULAW:
1398 auzero0 = 0x7f;
1399 break;
1400 case AUDIO_ENCODING_ALAW:
1401 auzero0 = 0x55;
1402 break;
1403 case AUDIO_ENCODING_MPEG_L1_STREAM:
1404 case AUDIO_ENCODING_MPEG_L1_PACKETS:
1405 case AUDIO_ENCODING_MPEG_L1_SYSTEM:
1406 case AUDIO_ENCODING_MPEG_L2_STREAM:
1407 case AUDIO_ENCODING_MPEG_L2_PACKETS:
1408 case AUDIO_ENCODING_MPEG_L2_SYSTEM:
1409 case AUDIO_ENCODING_ADPCM: /* is this right XXX */
1410 case AUDIO_ENCODING_SLINEAR_LE:
1411 case AUDIO_ENCODING_SLINEAR_BE:
1412 auzero0 = 0; /* fortunately this works for both 8 and 16 bits */
1413 break;
1414 case AUDIO_ENCODING_ULINEAR_LE:
1415 case AUDIO_ENCODING_ULINEAR_BE:
1416 if (params->precision == 16) {
1417 nfill = 2;
1418 if (params->encoding == AUDIO_ENCODING_ULINEAR_LE) {
1419 auzero0 = 0;
1420 auzero1 = 0x80;
1421 } else {
1422 auzero0 = 0x80;
1423 auzero1 = 0;
1424 }
1425 } else
1426 auzero0 = 0x80;
1427 break;
1428 default:
1429 DPRINTF(("audio: bad encoding %d\n", params->encoding));
1430 auzero0 = 0;
1431 break;
1432 }
1433 if (nfill == 1) {
1434 while (--n >= 0)
1435 *p++ = auzero0; /* XXX memset */
1436 } else /* nfill must be 2 */ {
1437 while (n > 1) {
1438 *p++ = auzero0;
1439 *p++ = auzero1;
1440 n -= 2;
1441 }
1442 }
1443 }
1444
1445 int
audio_silence_copyout(sc,n,uio)1446 audio_silence_copyout(sc, n, uio)
1447 struct audio_softc *sc;
1448 int n;
1449 struct uio *uio;
1450 {
1451 int error;
1452 int k;
1453 u_char zerobuf[128];
1454
1455 audio_fill_silence(&sc->sc_rparams, zerobuf, sizeof zerobuf);
1456
1457 error = 0;
1458 while (n > 0 && uio->uio_resid > 0 && !error) {
1459 k = min(n, min(uio->uio_resid, sizeof zerobuf));
1460 error = uiomove(zerobuf, k, uio);
1461 n -= k;
1462 }
1463 return (error);
1464 }
1465
1466 int
audio_write(dev,uio,ioflag)1467 audio_write(dev, uio, ioflag)
1468 dev_t dev;
1469 struct uio *uio;
1470 int ioflag;
1471 {
1472 int unit = AUDIOUNIT(dev);
1473 struct audio_softc *sc = audio_cd.cd_devs[unit];
1474 struct audio_ringbuffer *cb = &sc->sc_pr;
1475 u_char *inp, *einp;
1476 int saveerror, error, s, n, cc, used;
1477
1478 DPRINTFN(2, ("audio_write: sc=%p(unit=%d) count=%d used=%d(hi=%d)\n", sc, unit,
1479 uio->uio_resid, sc->sc_pr.used, sc->sc_pr.usedhigh));
1480
1481 if (cb->mmapped)
1482 return EINVAL;
1483
1484 if (uio->uio_resid == 0) {
1485 sc->sc_eof++;
1486 return 0;
1487 }
1488
1489 /*
1490 * If half-duplex and currently recording, throw away data.
1491 */
1492 if (!sc->sc_full_duplex &&
1493 (sc->sc_mode & AUMODE_RECORD)) {
1494 uio->uio_offset += uio->uio_resid;
1495 uio->uio_resid = 0;
1496 DPRINTF(("audio_write: half-dpx read busy\n"));
1497 return (0);
1498 }
1499
1500 if (!(sc->sc_mode & AUMODE_PLAY_ALL) && sc->sc_playdrop > 0) {
1501 n = min(sc->sc_playdrop, uio->uio_resid);
1502 DPRINTF(("audio_write: playdrop %d\n", n));
1503 uio->uio_offset += n;
1504 uio->uio_resid -= n;
1505 sc->sc_playdrop -= n;
1506 if (uio->uio_resid == 0)
1507 return 0;
1508 }
1509
1510 DPRINTFN(1, ("audio_write: sr=%ld, enc=%d, prec=%d, chan=%d, sw=%p, fact=%d\n",
1511 sc->sc_pparams.sample_rate, sc->sc_pparams.encoding,
1512 sc->sc_pparams.precision, sc->sc_pparams.channels,
1513 sc->sc_pparams.sw_code, sc->sc_pparams.factor));
1514
1515 error = 0;
1516 while (uio->uio_resid > 0 && !error) {
1517 s = splaudio();
1518 while (cb->used >= cb->usedhigh) {
1519 DPRINTFN(2, ("audio_write: sleep used=%d lowat=%d hiwat=%d\n",
1520 cb->used, cb->usedlow, cb->usedhigh));
1521 if (ioflag & IO_NDELAY) {
1522 splx(s);
1523 return (EWOULDBLOCK);
1524 }
1525 error = audio_sleep(&sc->sc_wchan, "aud_wr");
1526 if (sc->sc_dying)
1527 error = EIO;
1528 if (error) {
1529 splx(s);
1530 return error;
1531 }
1532 }
1533 used = cb->used;
1534 inp = cb->inp;
1535 cb->copying = 1;
1536 splx(s);
1537 cc = cb->usedhigh - used; /* maximum to write */
1538 n = cb->end - inp;
1539 if (sc->sc_pparams.factor != 1) {
1540 /* Compensate for software coding expansion factor. */
1541 n /= sc->sc_pparams.factor;
1542 cc /= sc->sc_pparams.factor;
1543 }
1544 if (n < cc)
1545 cc = n; /* don't write beyond end of buffer */
1546 if (uio->uio_resid < cc)
1547 cc = uio->uio_resid; /* and no more than we have */
1548
1549 #ifdef DIAGNOSTIC
1550 /*
1551 * This should never happen since the block size and and
1552 * block pointers are always nicely aligned.
1553 */
1554 if (cc == 0) {
1555 printf("audio_write: cc == 0, swcode=%p, factor=%d\n",
1556 sc->sc_pparams.sw_code, sc->sc_pparams.factor);
1557 cb->copying = 0;
1558 return EINVAL;
1559 }
1560 #endif
1561 DPRINTFN(1, ("audio_write: uiomove cc=%d inp=%p, left=%d\n",
1562 cc, inp, uio->uio_resid));
1563 n = uio->uio_resid;
1564 error = uiomove(inp, cc, uio);
1565 cc = n - uio->uio_resid; /* number of bytes actually moved */
1566 #ifdef AUDIO_DEBUG
1567 if (error)
1568 printf("audio_write:(1) uiomove failed %d; cc=%d inp=%p\n",
1569 error, cc, inp);
1570 #endif
1571 /*
1572 * Continue even if uiomove() failed because we may have
1573 * gotten a partial block.
1574 */
1575
1576 if (sc->sc_pparams.sw_code) {
1577 sc->sc_pparams.sw_code(sc->hw_hdl, inp, cc);
1578 /* Adjust count after the expansion. */
1579 cc *= sc->sc_pparams.factor;
1580 DPRINTFN(1, ("audio_write: expanded cc=%d\n", cc));
1581 }
1582
1583 einp = cb->inp + cc;
1584 if (einp >= cb->end)
1585 einp = cb->start;
1586
1587 s = splaudio();
1588 /*
1589 * This is a very suboptimal way of keeping track of
1590 * silence in the buffer, but it is simple.
1591 */
1592 sc->sc_sil_count = 0;
1593
1594 cb->inp = einp;
1595 cb->used += cc;
1596 /* If the interrupt routine wants the last block filled AND
1597 * the copy did not fill the last block completely it needs to
1598 * be padded.
1599 */
1600 if (cb->needfill &&
1601 (inp - cb->start) / cb->blksize ==
1602 (einp - cb->start) / cb->blksize) {
1603 /* Figure out how many bytes there is to a block boundary. */
1604 cc = cb->blksize - (einp - cb->start) % cb->blksize;
1605 DPRINTF(("audio_write: partial fill %d\n", cc));
1606 } else
1607 cc = 0;
1608 cb->needfill = 0;
1609 cb->copying = 0;
1610 if (!sc->sc_pbus && !cb->pause) {
1611 saveerror = error;
1612 error = audiostartp(sc);
1613 if (saveerror != 0) {
1614 /* Report the first error that occurred. */
1615 error = saveerror;
1616 }
1617 }
1618 splx(s);
1619 if (cc) {
1620 DPRINTFN(1, ("audio_write: fill %d\n", cc));
1621 audio_fill_silence(&sc->sc_pparams, einp, cc);
1622 }
1623 }
1624 return (error);
1625 }
1626
1627 int
audio_ioctl(dev,cmd,addr,flag,p)1628 audio_ioctl(dev, cmd, addr, flag, p)
1629 dev_t dev;
1630 u_long cmd;
1631 caddr_t addr;
1632 int flag;
1633 struct proc *p;
1634 {
1635 int unit = AUDIOUNIT(dev);
1636 struct audio_softc *sc = audio_cd.cd_devs[unit];
1637 struct audio_hw_if *hw = sc->hw_if;
1638 struct audio_offset *ao;
1639 int error = 0, s, offs, fd;
1640 int rbus, pbus;
1641
1642 DPRINTF(("audio_ioctl(%d,'%c',%d)\n",
1643 IOCPARM_LEN(cmd), IOCGROUP(cmd), cmd&0xff));
1644 switch (cmd) {
1645 case FIONBIO:
1646 /* All handled in the upper FS layer. */
1647 break;
1648
1649 case FIOASYNC:
1650 if (*(int *)addr) {
1651 if (sc->sc_async_audio)
1652 return (EBUSY);
1653 sc->sc_async_audio = p;
1654 DPRINTF(("audio_ioctl: FIOASYNC %p\n", p));
1655 } else
1656 sc->sc_async_audio = 0;
1657 break;
1658
1659 case AUDIO_FLUSH:
1660 DPRINTF(("AUDIO_FLUSH\n"));
1661 rbus = sc->sc_rbus;
1662 pbus = sc->sc_pbus;
1663 audio_clear(sc);
1664 s = splaudio();
1665 error = audio_initbufs(sc);
1666 if (error) {
1667 splx(s);
1668 return error;
1669 }
1670 if ((sc->sc_mode & AUMODE_PLAY) && !sc->sc_pbus && pbus)
1671 error = audiostartp(sc);
1672 if (!error &&
1673 (sc->sc_mode & AUMODE_RECORD) && !sc->sc_rbus && rbus)
1674 error = audiostartr(sc);
1675 splx(s);
1676 break;
1677
1678 /*
1679 * Number of read (write) samples dropped. We don't know where or
1680 * when they were dropped.
1681 */
1682 case AUDIO_RERROR:
1683 *(int *)addr = sc->sc_rr.drops;
1684 break;
1685
1686 case AUDIO_PERROR:
1687 *(int *)addr = sc->sc_pr.drops;
1688 break;
1689
1690 /*
1691 * Offsets into buffer.
1692 */
1693 case AUDIO_GETIOFFS:
1694 s = splaudio();
1695 /* figure out where next DMA will start */
1696 ao = (struct audio_offset *)addr;
1697 ao->samples = sc->sc_rr.stamp;
1698 ao->deltablks = (sc->sc_rr.stamp - sc->sc_rr.stamp_last) / sc->sc_rr.blksize;
1699 sc->sc_rr.stamp_last = sc->sc_rr.stamp;
1700 ao->offset = sc->sc_rr.inp - sc->sc_rr.start;
1701 splx(s);
1702 break;
1703
1704 case AUDIO_GETOOFFS:
1705 s = splaudio();
1706 /* figure out where next DMA will start */
1707 ao = (struct audio_offset *)addr;
1708 offs = sc->sc_pr.outp - sc->sc_pr.start + sc->sc_pr.blksize;
1709 if (sc->sc_pr.start + offs >= sc->sc_pr.end)
1710 offs = 0;
1711 ao->samples = sc->sc_pr.stamp;
1712 ao->deltablks = (sc->sc_pr.stamp - sc->sc_pr.stamp_last) / sc->sc_pr.blksize;
1713 sc->sc_pr.stamp_last = sc->sc_pr.stamp;
1714 ao->offset = offs;
1715 splx(s);
1716 break;
1717
1718 /*
1719 * How many bytes will elapse until mike hears the first
1720 * sample of what we write next?
1721 */
1722 case AUDIO_WSEEK:
1723 *(u_long *)addr = sc->sc_rr.used;
1724 break;
1725
1726 case AUDIO_SETINFO:
1727 DPRINTF(("AUDIO_SETINFO mode=0x%x\n", sc->sc_mode));
1728 error = audiosetinfo(sc, (struct audio_info *)addr);
1729 break;
1730
1731 case AUDIO_GETINFO:
1732 DPRINTF(("AUDIO_GETINFO\n"));
1733 error = audiogetinfo(sc, (struct audio_info *)addr);
1734 break;
1735
1736 case AUDIO_DRAIN:
1737 DPRINTF(("AUDIO_DRAIN\n"));
1738 error = audio_drain(sc);
1739 if (!error && hw->drain)
1740 error = hw->drain(sc->hw_hdl);
1741 break;
1742
1743 case AUDIO_GETDEV:
1744 DPRINTF(("AUDIO_GETDEV\n"));
1745 error = hw->getdev(sc->hw_hdl, (audio_device_t *)addr);
1746 break;
1747
1748 case AUDIO_GETENC:
1749 DPRINTF(("AUDIO_GETENC\n"));
1750 /* Pass read/write info down to query_encoding */
1751 ((struct audio_encoding *)addr)->flags = sc->sc_open;
1752 error = hw->query_encoding(sc->hw_hdl, (struct audio_encoding *)addr);
1753 break;
1754
1755 case AUDIO_GETFD:
1756 DPRINTF(("AUDIO_GETFD\n"));
1757 *(int *)addr = sc->sc_full_duplex;
1758 break;
1759
1760 case AUDIO_SETFD:
1761 DPRINTF(("AUDIO_SETFD\n"));
1762 fd = *(int *)addr;
1763 if (hw->get_props(sc->hw_hdl) & AUDIO_PROP_FULLDUPLEX) {
1764 if (hw->setfd)
1765 error = hw->setfd(sc->hw_hdl, fd);
1766 else
1767 error = 0;
1768 if (!error)
1769 sc->sc_full_duplex = fd;
1770 } else {
1771 if (fd)
1772 error = ENOTTY;
1773 else
1774 error = 0;
1775 }
1776 break;
1777
1778 case AUDIO_GETPROPS:
1779 DPRINTF(("AUDIO_GETPROPS\n"));
1780 *(int *)addr = hw->get_props(sc->hw_hdl);
1781 break;
1782
1783 default:
1784 DPRINTF(("audio_ioctl: unknown ioctl\n"));
1785 error = ENOTTY;
1786 break;
1787 }
1788 DPRINTF(("audio_ioctl(%d,'%c',%d) result %d\n",
1789 IOCPARM_LEN(cmd), IOCGROUP(cmd), cmd&0xff, error));
1790 return (error);
1791 }
1792
1793 void
audio_selwakeup(struct audio_softc * sc,int play)1794 audio_selwakeup(struct audio_softc *sc, int play)
1795 {
1796 struct selinfo *si;
1797
1798 si = play? &sc->sc_wsel : &sc->sc_rsel;
1799
1800 audio_wakeup(play? &sc->sc_wchan : &sc->sc_rchan);
1801 selwakeup(si);
1802 if (sc->sc_async_audio)
1803 psignal(sc->sc_async_audio, SIGIO);
1804 KNOTE(&si->si_note, 0);
1805 }
1806
1807 #define AUDIO_FILTREAD(sc) ((sc->sc_mode & AUMODE_PLAY) ? \
1808 sc->sc_pr.stamp > sc->sc_wstamp : sc->sc_rr.used > sc->sc_rr.usedlow)
1809 #define AUDIO_FILTWRITE(sc) \
1810 (sc->sc_mode & AUMODE_RECORD || sc->sc_pr.used <= sc->sc_pr.usedlow)
1811
1812 int
audio_poll(dev,events,p)1813 audio_poll(dev, events, p)
1814 dev_t dev;
1815 int events;
1816 struct proc *p;
1817 {
1818 int unit = AUDIOUNIT(dev);
1819 struct audio_softc *sc = audio_cd.cd_devs[unit];
1820 int revents = 0, s = splaudio();
1821
1822 DPRINTF(("audio_poll: events=0x%x mode=%d\n", events, sc->sc_mode));
1823
1824 if (events & (POLLIN | POLLRDNORM)) {
1825 if (AUDIO_FILTREAD(sc))
1826 revents |= events & (POLLIN | POLLRDNORM);
1827 }
1828 if (events & (POLLOUT | POLLWRNORM)) {
1829 if (AUDIO_FILTWRITE(sc))
1830 revents |= events & (POLLOUT | POLLWRNORM);
1831 }
1832 if (revents == 0) {
1833 if (events & (POLLIN | POLLRDNORM))
1834 selrecord(p, &sc->sc_rsel);
1835 if (events & (POLLOUT | POLLWRNORM))
1836 selrecord(p, &sc->sc_wsel);
1837 }
1838 splx(s);
1839 return (revents);
1840 }
1841
1842 paddr_t
audio_mmap(dev,off,prot)1843 audio_mmap(dev, off, prot)
1844 dev_t dev;
1845 off_t off;
1846 int prot;
1847 {
1848 int s;
1849 int unit = AUDIOUNIT(dev);
1850 struct audio_softc *sc = audio_cd.cd_devs[unit];
1851 struct audio_hw_if *hw = sc->hw_if;
1852 struct audio_ringbuffer *cb;
1853
1854 DPRINTF(("audio_mmap: off=%d, prot=%d\n", off, prot));
1855
1856 if (!(hw->get_props(sc->hw_hdl) & AUDIO_PROP_MMAP) || !hw->mappage)
1857 return -1;
1858 #if 0
1859 /* XXX
1860 * The idea here was to use the protection to determine if
1861 * we are mapping the read or write buffer, but it fails.
1862 * The VM system is broken in (at least) two ways.
1863 * 1) If you map memory VM_PROT_WRITE you SIGSEGV
1864 * when writing to it, so VM_PROT_READ|VM_PROT_WRITE
1865 * has to be used for mmapping the play buffer.
1866 * 2) Even if calling mmap() with VM_PROT_READ|VM_PROT_WRITE
1867 * audio_mmap will get called at some point with VM_PROT_READ
1868 * only.
1869 * So, alas, we always map the play buffer for now.
1870 */
1871 if (prot == (VM_PROT_READ|VM_PROT_WRITE) ||
1872 prot == VM_PROT_WRITE)
1873 cb = &sc->sc_pr;
1874 else if (prot == VM_PROT_READ)
1875 cb = &sc->sc_rr;
1876 else
1877 return -1;
1878 #else
1879 cb = &sc->sc_pr;
1880 #endif
1881
1882 if ((u_int)off >= cb->bufsize)
1883 return -1;
1884 if (!cb->mmapped) {
1885 cb->mmapped = 1;
1886 if (cb == &sc->sc_pr) {
1887 audio_fill_silence(&sc->sc_pparams, cb->start, cb->bufsize);
1888 s = splaudio();
1889 if (!sc->sc_pbus)
1890 (void)audiostartp(sc);
1891 splx(s);
1892 } else {
1893 s = splaudio();
1894 if (!sc->sc_rbus)
1895 (void)audiostartr(sc);
1896 splx(s);
1897 }
1898 }
1899
1900 return hw->mappage(sc->hw_hdl, cb->start, off, prot);
1901 }
1902
1903 int
audiostartr(sc)1904 audiostartr(sc)
1905 struct audio_softc *sc;
1906 {
1907 int error;
1908
1909 DPRINTF(("audiostartr: start=%p used=%d(hi=%d) mmapped=%d\n",
1910 sc->sc_rr.start, sc->sc_rr.used, sc->sc_rr.usedhigh,
1911 sc->sc_rr.mmapped));
1912
1913 if (sc->hw_if->trigger_input)
1914 error = sc->hw_if->trigger_input(sc->hw_hdl, sc->sc_rr.start,
1915 sc->sc_rr.end, sc->sc_rr.blksize,
1916 audio_rint, (void *)sc, &sc->sc_rparams);
1917 else
1918 error = sc->hw_if->start_input(sc->hw_hdl, sc->sc_rr.start,
1919 sc->sc_rr.blksize, audio_rint, (void *)sc);
1920 if (error) {
1921 DPRINTF(("audiostartr failed: %d\n", error));
1922 return error;
1923 }
1924 sc->sc_rbus = 1;
1925 return 0;
1926 }
1927
1928 int
audiostartp(sc)1929 audiostartp(sc)
1930 struct audio_softc *sc;
1931 {
1932 int error;
1933
1934 DPRINTF(("audiostartp: start=%p used=%d(hi=%d) mmapped=%d\n",
1935 sc->sc_pr.start, sc->sc_pr.used, sc->sc_pr.usedhigh,
1936 sc->sc_pr.mmapped));
1937
1938 if (!sc->sc_pr.mmapped && sc->sc_pr.used < sc->sc_pr.blksize)
1939 return 0;
1940
1941 if (sc->hw_if->trigger_output)
1942 error = sc->hw_if->trigger_output(sc->hw_hdl, sc->sc_pr.start,
1943 sc->sc_pr.end, sc->sc_pr.blksize,
1944 audio_pint, (void *)sc, &sc->sc_pparams);
1945 else
1946 error = sc->hw_if->start_output(sc->hw_hdl, sc->sc_pr.outp,
1947 sc->sc_pr.blksize, audio_pint, (void *)sc);
1948 if (error) {
1949 DPRINTF(("audiostartp failed: %d\n", error));
1950 return error;
1951 }
1952 sc->sc_pbus = 1;
1953 return 0;
1954 }
1955
1956 /*
1957 * When the play interrupt routine finds that the write isn't keeping
1958 * the buffer filled it will insert silence in the buffer to make up
1959 * for this. The part of the buffer that is filled with silence
1960 * is kept track of in a very approximate way: it starts at sc_sil_start
1961 * and extends sc_sil_count bytes. If there is already silence in
1962 * the requested area nothing is done; so when the whole buffer is
1963 * silent nothing happens. When the writer starts again sc_sil_count
1964 * is set to 0.
1965 */
1966 /* XXX
1967 * Putting silence into the output buffer should not really be done
1968 * at splaudio, but there is no softaudio level to do it at yet.
1969 */
1970 static __inline void
audio_pint_silence(sc,cb,inp,cc)1971 audio_pint_silence(sc, cb, inp, cc)
1972 struct audio_softc *sc;
1973 struct audio_ringbuffer *cb;
1974 u_char *inp;
1975 int cc;
1976 {
1977 u_char *s, *e, *p, *q;
1978
1979 if (sc->sc_sil_count > 0) {
1980 s = sc->sc_sil_start; /* start of silence */
1981 e = s + sc->sc_sil_count; /* end of silence, may be beyond end */
1982 p = inp; /* adjusted pointer to area to fill */
1983 if (p < s)
1984 p += cb->end - cb->start;
1985 q = p+cc;
1986 /* Check if there is already silence. */
1987 if (!(s <= p && p < e &&
1988 s <= q && q <= e)) {
1989 if (s <= p)
1990 sc->sc_sil_count = max(sc->sc_sil_count, q-s);
1991 DPRINTFN(5, ("audio_pint_silence: fill cc=%d inp=%p, count=%d size=%d\n",
1992 cc, inp, sc->sc_sil_count, (int)(cb->end - cb->start)));
1993 audio_fill_silence(&sc->sc_pparams, inp, cc);
1994 } else {
1995 DPRINTFN(5, ("audio_pint_silence: already silent cc=%d inp=%p\n", cc, inp));
1996
1997 }
1998 } else {
1999 sc->sc_sil_start = inp;
2000 sc->sc_sil_count = cc;
2001 DPRINTFN(5, ("audio_pint_silence: start fill %p %d\n",
2002 inp, cc));
2003 audio_fill_silence(&sc->sc_pparams, inp, cc);
2004 }
2005 }
2006
2007 /*
2008 * Called from HW driver module on completion of dma output.
2009 * Start output of new block, wrap in ring buffer if needed.
2010 * If no more buffers to play, output zero instead.
2011 * Do a wakeup if necessary.
2012 */
2013 void
audio_pint(v)2014 audio_pint(v)
2015 void *v;
2016 {
2017 struct audio_softc *sc = v;
2018 struct audio_hw_if *hw = sc->hw_if;
2019 struct audio_ringbuffer *cb = &sc->sc_pr;
2020 u_char *inp;
2021 int cc, ccr;
2022 int blksize;
2023 int error;
2024
2025 if (!sc->sc_open)
2026 return; /* ignore interrupt if not open */
2027
2028 blksize = cb->blksize;
2029
2030 add_auvis_randomness((long)cb);
2031
2032 cb->outp += blksize;
2033 if (cb->outp >= cb->end)
2034 cb->outp = cb->start;
2035 cb->stamp += blksize / sc->sc_pparams.factor;
2036 if (cb->mmapped) {
2037 DPRINTFN(5, ("audio_pint: mmapped outp=%p cc=%d inp=%p\n",
2038 cb->outp, blksize, cb->inp));
2039 if (!hw->trigger_output)
2040 (void)hw->start_output(sc->hw_hdl, cb->outp,
2041 blksize, audio_pint, (void *)sc);
2042 return;
2043 }
2044
2045 #ifdef AUDIO_INTR_TIME
2046 {
2047 struct timeval tv;
2048 u_long t;
2049 microtime(&tv);
2050 t = tv.tv_usec + 1000000 * tv.tv_sec;
2051 if (sc->sc_pnintr) {
2052 long lastdelta, totdelta;
2053 lastdelta = t - sc->sc_plastintr - sc->sc_pblktime;
2054 if (lastdelta > sc->sc_pblktime / 3) {
2055 printf("audio: play interrupt(%d) off relative by %ld us (%lu)\n",
2056 sc->sc_pnintr, lastdelta, sc->sc_pblktime);
2057 }
2058 totdelta = t - sc->sc_pfirstintr - sc->sc_pblktime * sc->sc_pnintr;
2059 if (totdelta > sc->sc_pblktime) {
2060 printf("audio: play interrupt(%d) off absolute by %ld us (%lu) (LOST)\n",
2061 sc->sc_pnintr, totdelta, sc->sc_pblktime);
2062 sc->sc_pnintr++; /* avoid repeated messages */
2063 }
2064 } else
2065 sc->sc_pfirstintr = t;
2066 sc->sc_plastintr = t;
2067 sc->sc_pnintr++;
2068 }
2069 #endif
2070
2071 cb->used -= blksize;
2072 if (cb->used < blksize) {
2073 /* we don't have a full block to use */
2074 if (cb->copying) {
2075 /* writer is in progress, don't disturb */
2076 cb->needfill = 1;
2077 DPRINTFN(1, ("audio_pint: copying in progress\n"));
2078 } else {
2079 inp = cb->inp;
2080 cc = blksize - (inp - cb->start) % blksize;
2081 ccr = cc / sc->sc_pparams.factor;
2082 if (cb->pause)
2083 cb->pdrops += ccr;
2084 else {
2085 cb->drops += ccr;
2086 sc->sc_playdrop += ccr;
2087 }
2088 audio_pint_silence(sc, cb, inp, cc);
2089 inp += cc;
2090 if (inp >= cb->end)
2091 inp = cb->start;
2092 cb->inp = inp;
2093 cb->used += cc;
2094
2095 /* Clear next block so we keep ahead of the DMA. */
2096 if (cb->used + cc < cb->usedhigh)
2097 audio_pint_silence(sc, cb, inp, blksize);
2098 }
2099 }
2100
2101 DPRINTFN(5, ("audio_pint: outp=%p cc=%d\n", cb->outp, blksize));
2102 if (!hw->trigger_output) {
2103 error = hw->start_output(sc->hw_hdl, cb->outp, blksize,
2104 audio_pint, (void *)sc);
2105 if (error) {
2106 /* XXX does this really help? */
2107 DPRINTF(("audio_pint restart failed: %d\n", error));
2108 audio_clear(sc);
2109 }
2110 }
2111
2112 DPRINTFN(2, ("audio_pint: mode=%d pause=%d used=%d lowat=%d\n",
2113 sc->sc_mode, cb->pause, cb->used, cb->usedlow));
2114 if ((sc->sc_mode & AUMODE_PLAY) && !cb->pause &&
2115 cb->used <= cb->usedlow)
2116 audio_selwakeup(sc, 1);
2117
2118 /* Possible to return one or more "phantom blocks" now. */
2119 if (!sc->sc_full_duplex && sc->sc_rchan)
2120 audio_selwakeup(sc, 0);
2121 }
2122
2123 /*
2124 * Called from HW driver module on completion of dma input.
2125 * Mark it as input in the ring buffer (fiddle pointers).
2126 * Do a wakeup if necessary.
2127 */
2128 void
audio_rint(v)2129 audio_rint(v)
2130 void *v;
2131 {
2132 struct audio_softc *sc = v;
2133 struct audio_hw_if *hw = sc->hw_if;
2134 struct audio_ringbuffer *cb = &sc->sc_rr;
2135 int blksize;
2136 int error;
2137
2138 if (!sc->sc_open)
2139 return; /* ignore interrupt if not open */
2140
2141 add_auvis_randomness((long)cb);
2142
2143 blksize = cb->blksize;
2144
2145 cb->inp += blksize;
2146 if (cb->inp >= cb->end)
2147 cb->inp = cb->start;
2148 cb->stamp += blksize;
2149 if (cb->mmapped) {
2150 DPRINTFN(2, ("audio_rint: mmapped inp=%p cc=%d\n",
2151 cb->inp, blksize));
2152 if (!hw->trigger_input)
2153 (void)hw->start_input(sc->hw_hdl, cb->inp, blksize,
2154 audio_rint, (void *)sc);
2155 return;
2156 }
2157
2158 #ifdef AUDIO_INTR_TIME
2159 {
2160 struct timeval tv;
2161 u_long t;
2162 microtime(&tv);
2163 t = tv.tv_usec + 1000000 * tv.tv_sec;
2164 if (sc->sc_rnintr) {
2165 long lastdelta, totdelta;
2166 lastdelta = t - sc->sc_rlastintr - sc->sc_rblktime;
2167 if (lastdelta > sc->sc_rblktime / 5) {
2168 printf("audio: record interrupt(%d) off relative by %ld us (%lu)\n",
2169 sc->sc_rnintr, lastdelta, sc->sc_rblktime);
2170 }
2171 totdelta = t - sc->sc_rfirstintr - sc->sc_rblktime * sc->sc_rnintr;
2172 if (totdelta > sc->sc_rblktime / 2) {
2173 sc->sc_rnintr++;
2174 printf("audio: record interrupt(%d) off absolute by %ld us (%lu)\n",
2175 sc->sc_rnintr, totdelta, sc->sc_rblktime);
2176 sc->sc_rnintr++; /* avoid repeated messages */
2177 }
2178 } else
2179 sc->sc_rfirstintr = t;
2180 sc->sc_rlastintr = t;
2181 sc->sc_rnintr++;
2182 }
2183 #endif
2184
2185 cb->used += blksize;
2186 if (cb->pause) {
2187 DPRINTFN(1, ("audio_rint: pdrops %lu\n", cb->pdrops));
2188 cb->pdrops += blksize;
2189 cb->outp += blksize;
2190 cb->used -= blksize;
2191 } else if (cb->used + blksize >= cb->usedhigh && !cb->copying) {
2192 DPRINTFN(1, ("audio_rint: drops %lu\n", cb->drops));
2193 cb->drops += blksize;
2194 cb->outp += blksize;
2195 cb->used -= blksize;
2196 }
2197
2198 DPRINTFN(2, ("audio_rint: inp=%p cc=%d used=%d\n",
2199 cb->inp, blksize, cb->used));
2200 if (!hw->trigger_input) {
2201 error = hw->start_input(sc->hw_hdl, cb->inp, blksize,
2202 audio_rint, (void *)sc);
2203 if (error) {
2204 /* XXX does this really help? */
2205 DPRINTF(("audio_rint: restart failed: %d\n", error));
2206 audio_clear(sc);
2207 }
2208 }
2209
2210 audio_selwakeup(sc, 0);
2211 }
2212
2213 int
audio_check_params(p)2214 audio_check_params(p)
2215 struct audio_params *p;
2216 {
2217 if (p->encoding == AUDIO_ENCODING_PCM16) {
2218 if (p->precision == 8)
2219 p->encoding = AUDIO_ENCODING_ULINEAR;
2220 else
2221 p->encoding = AUDIO_ENCODING_SLINEAR;
2222 } else if (p->encoding == AUDIO_ENCODING_PCM8) {
2223 if (p->precision == 8)
2224 p->encoding = AUDIO_ENCODING_ULINEAR;
2225 else
2226 return EINVAL;
2227 }
2228
2229 if (p->encoding == AUDIO_ENCODING_SLINEAR)
2230 #if BYTE_ORDER == LITTLE_ENDIAN
2231 p->encoding = AUDIO_ENCODING_SLINEAR_LE;
2232 #else
2233 p->encoding = AUDIO_ENCODING_SLINEAR_BE;
2234 #endif
2235 if (p->encoding == AUDIO_ENCODING_ULINEAR)
2236 #if BYTE_ORDER == LITTLE_ENDIAN
2237 p->encoding = AUDIO_ENCODING_ULINEAR_LE;
2238 #else
2239 p->encoding = AUDIO_ENCODING_ULINEAR_BE;
2240 #endif
2241
2242 switch (p->encoding) {
2243 case AUDIO_ENCODING_ULAW:
2244 case AUDIO_ENCODING_ALAW:
2245 case AUDIO_ENCODING_ADPCM:
2246 if (p->precision != 8)
2247 return (EINVAL);
2248 break;
2249 case AUDIO_ENCODING_SLINEAR_LE:
2250 case AUDIO_ENCODING_SLINEAR_BE:
2251 case AUDIO_ENCODING_ULINEAR_LE:
2252 case AUDIO_ENCODING_ULINEAR_BE:
2253 if (p->precision != 8 && p->precision != 16)
2254 return (EINVAL);
2255 break;
2256 case AUDIO_ENCODING_MPEG_L1_STREAM:
2257 case AUDIO_ENCODING_MPEG_L1_PACKETS:
2258 case AUDIO_ENCODING_MPEG_L1_SYSTEM:
2259 case AUDIO_ENCODING_MPEG_L2_STREAM:
2260 case AUDIO_ENCODING_MPEG_L2_PACKETS:
2261 case AUDIO_ENCODING_MPEG_L2_SYSTEM:
2262 break;
2263 default:
2264 return (EINVAL);
2265 }
2266
2267 if (p->channels < 1 || p->channels > 8) /* sanity check # of channels */
2268 return (EINVAL);
2269
2270 return (0);
2271 }
2272
2273 int
au_set_lr_value(sc,ct,l,r)2274 au_set_lr_value(sc, ct, l, r)
2275 struct audio_softc *sc;
2276 mixer_ctrl_t *ct;
2277 int l, r;
2278 {
2279 ct->type = AUDIO_MIXER_VALUE;
2280 ct->un.value.num_channels = 2;
2281 ct->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
2282 ct->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
2283 if (sc->hw_if->set_port(sc->hw_hdl, ct) == 0)
2284 return 0;
2285 ct->un.value.num_channels = 1;
2286 ct->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r)/2;
2287 return sc->hw_if->set_port(sc->hw_hdl, ct);
2288 }
2289
2290 int
au_set_gain(sc,ports,gain,balance)2291 au_set_gain(sc, ports, gain, balance)
2292 struct audio_softc *sc;
2293 struct au_mixer_ports *ports;
2294 int gain;
2295 int balance;
2296 {
2297 mixer_ctrl_t ct;
2298 int i, error;
2299 int l, r;
2300 u_int mask;
2301 int nset;
2302
2303 if (balance == AUDIO_MID_BALANCE) {
2304 l = r = gain;
2305 } else if (balance < AUDIO_MID_BALANCE) {
2306 r = gain;
2307 l = (balance * gain) / AUDIO_MID_BALANCE;
2308 } else {
2309 l = gain;
2310 r = ((AUDIO_RIGHT_BALANCE - balance) * gain)
2311 / AUDIO_MID_BALANCE;
2312 }
2313 DPRINTF(("au_set_gain: gain=%d balance=%d, l=%d r=%d\n",
2314 gain, balance, l, r));
2315
2316 if (ports->index == -1) {
2317 usemaster:
2318 if (ports->master == -1)
2319 return 0; /* just ignore it silently */
2320 ct.dev = ports->master;
2321 error = au_set_lr_value(sc, &ct, l, r);
2322 } else {
2323 ct.dev = ports->index;
2324 if (ports->isenum) {
2325 ct.type = AUDIO_MIXER_ENUM;
2326 error = sc->hw_if->get_port(sc->hw_hdl, &ct);
2327 if (error)
2328 return error;
2329 for(i = 0; i < ports->nports; i++) {
2330 if (ports->misel[i] == ct.un.ord) {
2331 ct.dev = ports->miport[i];
2332 if (ct.dev == -1 ||
2333 au_set_lr_value(sc, &ct, l, r))
2334 goto usemaster;
2335 else
2336 break;
2337 }
2338 }
2339 } else {
2340 ct.type = AUDIO_MIXER_SET;
2341 error = sc->hw_if->get_port(sc->hw_hdl, &ct);
2342 if (error)
2343 return error;
2344 mask = ct.un.mask;
2345 nset = 0;
2346 for(i = 0; i < ports->nports; i++) {
2347 if (ports->misel[i] & mask) {
2348 ct.dev = ports->miport[i];
2349 if (ct.dev != -1 &&
2350 au_set_lr_value(sc, &ct, l, r) == 0)
2351 nset++;
2352 }
2353 }
2354 if (nset == 0)
2355 goto usemaster;
2356 }
2357 }
2358 if (!error)
2359 mixer_signal(sc);
2360 return error;
2361 }
2362
2363 int
au_get_lr_value(sc,ct,l,r)2364 au_get_lr_value(sc, ct, l, r)
2365 struct audio_softc *sc;
2366 mixer_ctrl_t *ct;
2367 int *l, *r;
2368 {
2369 int error;
2370
2371 ct->un.value.num_channels = 2;
2372 if (sc->hw_if->get_port(sc->hw_hdl, ct) == 0) {
2373 *l = ct->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
2374 *r = ct->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
2375 } else {
2376 ct->un.value.num_channels = 1;
2377 error = sc->hw_if->get_port(sc->hw_hdl, ct);
2378 if (error)
2379 return error;
2380 *r = *l = ct->un.value.level[AUDIO_MIXER_LEVEL_MONO];
2381 }
2382 return 0;
2383 }
2384
2385 void
au_get_gain(sc,ports,pgain,pbalance)2386 au_get_gain(sc, ports, pgain, pbalance)
2387 struct audio_softc *sc;
2388 struct au_mixer_ports *ports;
2389 u_int *pgain;
2390 u_char *pbalance;
2391 {
2392 mixer_ctrl_t ct;
2393 int i, l, r, n;
2394 int lgain = AUDIO_MAX_GAIN/2, rgain = AUDIO_MAX_GAIN/2;
2395
2396 if (ports->index == -1) {
2397 usemaster:
2398 if (ports->master == -1)
2399 goto bad;
2400 ct.dev = ports->master;
2401 ct.type = AUDIO_MIXER_VALUE;
2402 if (au_get_lr_value(sc, &ct, &lgain, &rgain))
2403 goto bad;
2404 } else {
2405 ct.dev = ports->index;
2406 if (ports->isenum) {
2407 ct.type = AUDIO_MIXER_ENUM;
2408 if (sc->hw_if->get_port(sc->hw_hdl, &ct))
2409 goto bad;
2410 ct.type = AUDIO_MIXER_VALUE;
2411 for(i = 0; i < ports->nports; i++) {
2412 if (ports->misel[i] == ct.un.ord) {
2413 ct.dev = ports->miport[i];
2414 if (ct.dev == -1 ||
2415 au_get_lr_value(sc, &ct,
2416 &lgain, &rgain))
2417 goto usemaster;
2418 else
2419 break;
2420 }
2421 }
2422 } else {
2423 ct.type = AUDIO_MIXER_SET;
2424 if (sc->hw_if->get_port(sc->hw_hdl, &ct))
2425 goto bad;
2426 ct.type = AUDIO_MIXER_VALUE;
2427 lgain = rgain = n = 0;
2428 for(i = 0; i < ports->nports; i++) {
2429 if (ports->misel[i] & ct.un.mask) {
2430 ct.dev = ports->miport[i];
2431 if (ct.dev == -1 ||
2432 au_get_lr_value(sc, &ct, &l, &r))
2433 goto usemaster;
2434 else {
2435 lgain += l;
2436 rgain += r;
2437 n++;
2438 }
2439 }
2440 }
2441 if (n != 0) {
2442 lgain /= n;
2443 rgain /= n;
2444 }
2445 }
2446 }
2447 bad:
2448 if (lgain == rgain) { /* handles lgain==rgain==0 */
2449 *pgain = lgain;
2450 *pbalance = AUDIO_MID_BALANCE;
2451 } else if (lgain < rgain) {
2452 *pgain = rgain;
2453 *pbalance = (AUDIO_MID_BALANCE * lgain) / rgain;
2454 } else /* lgain > rgain */ {
2455 *pgain = lgain;
2456 *pbalance = AUDIO_RIGHT_BALANCE -
2457 (AUDIO_MID_BALANCE * rgain) / lgain;
2458 }
2459 }
2460
2461 int
au_set_port(sc,ports,port)2462 au_set_port(sc, ports, port)
2463 struct audio_softc *sc;
2464 struct au_mixer_ports *ports;
2465 u_int port;
2466 {
2467 mixer_ctrl_t ct;
2468 int i, error;
2469
2470 if (port == 0 && ports->allports == 0)
2471 return 0; /* allow this special case */
2472
2473 if (ports->index == -1)
2474 return EINVAL;
2475 ct.dev = ports->index;
2476 if (ports->isenum) {
2477 if (port & (port-1))
2478 return EINVAL; /* Only one port allowed */
2479 ct.type = AUDIO_MIXER_ENUM;
2480 error = EINVAL;
2481 for(i = 0; i < ports->nports; i++)
2482 if (ports->aumask[i] == port) {
2483 ct.un.ord = ports->misel[i];
2484 error = sc->hw_if->set_port(sc->hw_hdl, &ct);
2485 break;
2486 }
2487 } else {
2488 ct.type = AUDIO_MIXER_SET;
2489 ct.un.mask = 0;
2490 for(i = 0; i < ports->nports; i++)
2491 if (ports->aumask[i] & port)
2492 ct.un.mask |= ports->misel[i];
2493 if (port != 0 && ct.un.mask == 0)
2494 error = EINVAL;
2495 else
2496 error = sc->hw_if->set_port(sc->hw_hdl, &ct);
2497 }
2498 if (!error)
2499 mixer_signal(sc);
2500 return error;
2501 }
2502
2503 int
au_get_port(sc,ports)2504 au_get_port(sc, ports)
2505 struct audio_softc *sc;
2506 struct au_mixer_ports *ports;
2507 {
2508 mixer_ctrl_t ct;
2509 int i, aumask;
2510
2511 if (ports->index == -1)
2512 return 0;
2513 ct.dev = ports->index;
2514 ct.type = ports->isenum ? AUDIO_MIXER_ENUM : AUDIO_MIXER_SET;
2515 if (sc->hw_if->get_port(sc->hw_hdl, &ct))
2516 return 0;
2517 aumask = 0;
2518 if (ports->isenum) {
2519 for(i = 0; i < ports->nports; i++)
2520 if (ct.un.ord == ports->misel[i])
2521 aumask = ports->aumask[i];
2522 } else {
2523 for(i = 0; i < ports->nports; i++)
2524 if (ct.un.mask & ports->misel[i])
2525 aumask |= ports->aumask[i];
2526 }
2527 return aumask;
2528 }
2529
2530 int
audiosetinfo(sc,ai)2531 audiosetinfo(sc, ai)
2532 struct audio_softc *sc;
2533 struct audio_info *ai;
2534 {
2535 struct audio_prinfo *r = &ai->record, *p = &ai->play;
2536 int cleared;
2537 int s, setmode, modechange = 0;
2538 int error;
2539 struct audio_hw_if *hw = sc->hw_if;
2540 struct audio_params pp, rp;
2541 int np, nr;
2542 unsigned int blks;
2543 int oldpblksize, oldrblksize;
2544 int rbus, pbus;
2545 u_int gain;
2546 u_char balance;
2547
2548 if (hw == 0) /* HW has not attached */
2549 return(ENXIO);
2550
2551 rbus = sc->sc_rbus;
2552 pbus = sc->sc_pbus;
2553 error = 0;
2554 cleared = 0;
2555
2556 pp = sc->sc_pparams; /* Temporary encoding storage in */
2557 rp = sc->sc_rparams; /* case setting the modes fails. */
2558 nr = np = 0;
2559
2560 if (p->sample_rate != ~0) {
2561 pp.sample_rate = p->sample_rate;
2562 np++;
2563 }
2564 if (r->sample_rate != ~0) {
2565 rp.sample_rate = r->sample_rate;
2566 nr++;
2567 }
2568 if (p->encoding != ~0) {
2569 pp.encoding = p->encoding;
2570 np++;
2571 }
2572 if (r->encoding != ~0) {
2573 rp.encoding = r->encoding;
2574 nr++;
2575 }
2576 if (p->precision != ~0) {
2577 pp.precision = p->precision;
2578 np++;
2579 }
2580 if (r->precision != ~0) {
2581 rp.precision = r->precision;
2582 nr++;
2583 }
2584 if (p->channels != ~0) {
2585 pp.channels = p->channels;
2586 np++;
2587 }
2588 if (r->channels != ~0) {
2589 rp.channels = r->channels;
2590 nr++;
2591 }
2592 #ifdef AUDIO_DEBUG
2593 if (audiodebug && nr)
2594 audio_print_params("Setting record params", &rp);
2595 if (audiodebug && np)
2596 audio_print_params("Setting play params", &pp);
2597 #endif
2598 if (nr && (error = audio_check_params(&rp)))
2599 return error;
2600 if (np && (error = audio_check_params(&pp)))
2601 return error;
2602 setmode = 0;
2603 if (nr) {
2604 if (!cleared)
2605 audio_clear(sc);
2606 modechange = cleared = 1;
2607 rp.sw_code = 0;
2608 rp.factor = 1;
2609 setmode |= AUMODE_RECORD;
2610 }
2611 if (np) {
2612 if (!cleared)
2613 audio_clear(sc);
2614 modechange = cleared = 1;
2615 pp.sw_code = 0;
2616 pp.factor = 1;
2617 setmode |= AUMODE_PLAY;
2618 }
2619
2620 if (ai->mode != ~0) {
2621 if (!cleared)
2622 audio_clear(sc);
2623 modechange = cleared = 1;
2624 sc->sc_mode = ai->mode;
2625 if (sc->sc_mode & AUMODE_PLAY_ALL)
2626 sc->sc_mode |= AUMODE_PLAY;
2627 if ((sc->sc_mode & AUMODE_PLAY) && !sc->sc_full_duplex)
2628 /* Play takes precedence */
2629 sc->sc_mode &= ~AUMODE_RECORD;
2630 }
2631
2632 if (modechange) {
2633 int indep = hw->get_props(sc->hw_hdl) & AUDIO_PROP_INDEPENDENT;
2634 if (!indep) {
2635 if (setmode == AUMODE_RECORD)
2636 pp = rp;
2637 else if (setmode == AUMODE_PLAY)
2638 rp = pp;
2639 }
2640 error = hw->set_params(sc->hw_hdl, setmode,
2641 sc->sc_mode & (AUMODE_PLAY | AUMODE_RECORD), &pp, &rp);
2642 if (error)
2643 return (error);
2644 if (!indep) {
2645 if (setmode == AUMODE_RECORD) {
2646 pp.sample_rate = rp.sample_rate;
2647 pp.encoding = rp.encoding;
2648 pp.channels = rp.channels;
2649 pp.precision = rp.precision;
2650 } else if (setmode == AUMODE_PLAY) {
2651 rp.sample_rate = pp.sample_rate;
2652 rp.encoding = pp.encoding;
2653 rp.channels = pp.channels;
2654 rp.precision = pp.precision;
2655 }
2656 }
2657 sc->sc_rparams = rp;
2658 sc->sc_pparams = pp;
2659 }
2660
2661 oldpblksize = sc->sc_pr.blksize;
2662 oldrblksize = sc->sc_rr.blksize;
2663 /* Play params can affect the record params, so recalculate blksize. */
2664 if (nr || np) {
2665 audio_calc_blksize(sc, AUMODE_RECORD);
2666 audio_calc_blksize(sc, AUMODE_PLAY);
2667 }
2668 #ifdef AUDIO_DEBUG
2669 if (audiodebug > 1 && nr)
2670 audio_print_params("After setting record params", &sc->sc_rparams);
2671 if (audiodebug > 1 && np)
2672 audio_print_params("After setting play params", &sc->sc_pparams);
2673 #endif
2674
2675 if (p->port != ~0) {
2676 if (!cleared)
2677 audio_clear(sc);
2678 cleared = 1;
2679
2680 error = au_set_port(sc, &sc->sc_outports, p->port);
2681 if (error)
2682 return(error);
2683 }
2684 if (r->port != ~0) {
2685 if (!cleared)
2686 audio_clear(sc);
2687 cleared = 1;
2688
2689 error = au_set_port(sc, &sc->sc_inports, r->port);
2690 if (error)
2691 return(error);
2692 }
2693 if (p->gain != ~0) {
2694 au_get_gain(sc, &sc->sc_outports, &gain, &balance);
2695 error = au_set_gain(sc, &sc->sc_outports, p->gain, balance);
2696 if (error)
2697 return(error);
2698 }
2699 if (r->gain != ~0) {
2700 au_get_gain(sc, &sc->sc_inports, &gain, &balance);
2701 error = au_set_gain(sc, &sc->sc_inports, r->gain, balance);
2702 if (error)
2703 return(error);
2704 }
2705
2706 if (p->balance != (u_char)~0) {
2707 au_get_gain(sc, &sc->sc_outports, &gain, &balance);
2708 error = au_set_gain(sc, &sc->sc_outports, gain, p->balance);
2709 if (error)
2710 return(error);
2711 }
2712 if (r->balance != (u_char)~0) {
2713 au_get_gain(sc, &sc->sc_inports, &gain, &balance);
2714 error = au_set_gain(sc, &sc->sc_inports, gain, r->balance);
2715 if (error)
2716 return(error);
2717 }
2718
2719 if (ai->monitor_gain != ~0 &&
2720 sc->sc_monitor_port != -1) {
2721 mixer_ctrl_t ct;
2722
2723 ct.dev = sc->sc_monitor_port;
2724 ct.type = AUDIO_MIXER_VALUE;
2725 ct.un.value.num_channels = 1;
2726 ct.un.value.level[AUDIO_MIXER_LEVEL_MONO] = ai->monitor_gain;
2727 error = sc->hw_if->set_port(sc->hw_hdl, &ct);
2728 if (error)
2729 return(error);
2730 }
2731
2732 if (p->pause != (u_char)~0) {
2733 sc->sc_pr.pause = p->pause;
2734 if (!p->pause && !sc->sc_pbus && (sc->sc_mode & AUMODE_PLAY)) {
2735 s = splaudio();
2736 error = audiostartp(sc);
2737 splx(s);
2738 if (error)
2739 return error;
2740 }
2741 }
2742 if (r->pause != (u_char)~0) {
2743 sc->sc_rr.pause = r->pause;
2744 if (!r->pause && !sc->sc_rbus && (sc->sc_mode & AUMODE_RECORD)) {
2745 s = splaudio();
2746 error = audiostartr(sc);
2747 splx(s);
2748 if (error)
2749 return error;
2750 }
2751 }
2752
2753 if (ai->blocksize != ~0) {
2754 /* Block size specified explicitly. */
2755 if (!cleared)
2756 audio_clear(sc);
2757 cleared = 1;
2758
2759 if (ai->blocksize == 0) {
2760 audio_calc_blksize(sc, AUMODE_RECORD);
2761 audio_calc_blksize(sc, AUMODE_PLAY);
2762 sc->sc_blkset = 0;
2763 } else {
2764 int bs = ai->blocksize;
2765 if (hw->round_blocksize)
2766 bs = hw->round_blocksize(sc->hw_hdl, bs);
2767 sc->sc_pr.blksize = sc->sc_rr.blksize = bs;
2768 sc->sc_blkset = 1;
2769 }
2770 }
2771
2772 if (ai->mode != ~0) {
2773 if (sc->sc_mode & AUMODE_PLAY)
2774 audio_init_play(sc);
2775 if (sc->sc_mode & AUMODE_RECORD)
2776 audio_init_record(sc);
2777 }
2778
2779 if (hw->commit_settings) {
2780 error = hw->commit_settings(sc->hw_hdl);
2781 if (error)
2782 return (error);
2783 }
2784
2785 if (cleared) {
2786 s = splaudio();
2787 error = audio_initbufs(sc);
2788 if (error) goto err;
2789 if (sc->sc_pr.blksize != oldpblksize ||
2790 sc->sc_rr.blksize != oldrblksize)
2791 audio_calcwater(sc);
2792 if ((sc->sc_mode & AUMODE_PLAY) &&
2793 pbus && !sc->sc_pbus)
2794 error = audiostartp(sc);
2795 if (!error &&
2796 (sc->sc_mode & AUMODE_RECORD) &&
2797 rbus && !sc->sc_rbus)
2798 error = audiostartr(sc);
2799 err:
2800 splx(s);
2801 if (error)
2802 return error;
2803 }
2804
2805 /* Change water marks after initializing the buffers. */
2806 if (ai->hiwat != ~0) {
2807 blks = ai->hiwat;
2808 if (blks > sc->sc_pr.maxblks)
2809 blks = sc->sc_pr.maxblks;
2810 if (blks < 2)
2811 blks = 2;
2812 sc->sc_pr.usedhigh = blks * sc->sc_pr.blksize;
2813 }
2814 if (ai->lowat != ~0) {
2815 blks = ai->lowat;
2816 if (blks > sc->sc_pr.maxblks - 1)
2817 blks = sc->sc_pr.maxblks - 1;
2818 sc->sc_pr.usedlow = blks * sc->sc_pr.blksize;
2819 }
2820 if (ai->hiwat != ~0 || ai->lowat != ~0) {
2821 if (sc->sc_pr.usedlow > sc->sc_pr.usedhigh - sc->sc_pr.blksize)
2822 sc->sc_pr.usedlow = sc->sc_pr.usedhigh - sc->sc_pr.blksize;
2823 }
2824
2825 return (0);
2826 }
2827
2828 int
audiogetinfo(sc,ai)2829 audiogetinfo(sc, ai)
2830 struct audio_softc *sc;
2831 struct audio_info *ai;
2832 {
2833 struct audio_prinfo *r = &ai->record, *p = &ai->play;
2834 struct audio_hw_if *hw = sc->hw_if;
2835
2836 if (hw == 0) /* HW has not attached */
2837 return(ENXIO);
2838
2839 p->sample_rate = sc->sc_pparams.sample_rate;
2840 r->sample_rate = sc->sc_rparams.sample_rate;
2841 p->channels = sc->sc_pparams.channels;
2842 r->channels = sc->sc_rparams.channels;
2843 p->precision = sc->sc_pparams.precision;
2844 r->precision = sc->sc_rparams.precision;
2845 p->encoding = sc->sc_pparams.encoding;
2846 r->encoding = sc->sc_rparams.encoding;
2847
2848 r->port = au_get_port(sc, &sc->sc_inports);
2849 p->port = au_get_port(sc, &sc->sc_outports);
2850
2851 r->avail_ports = sc->sc_inports.allports;
2852 p->avail_ports = sc->sc_outports.allports;
2853
2854 au_get_gain(sc, &sc->sc_inports, &r->gain, &r->balance);
2855 au_get_gain(sc, &sc->sc_outports, &p->gain, &p->balance);
2856
2857 if (sc->sc_monitor_port != -1) {
2858 mixer_ctrl_t ct;
2859
2860 ct.dev = sc->sc_monitor_port;
2861 ct.type = AUDIO_MIXER_VALUE;
2862 ct.un.value.num_channels = 1;
2863 if (sc->hw_if->get_port(sc->hw_hdl, &ct))
2864 ai->monitor_gain = 0;
2865 else
2866 ai->monitor_gain =
2867 ct.un.value.level[AUDIO_MIXER_LEVEL_MONO];
2868 } else
2869 ai->monitor_gain = 0;
2870
2871 p->seek = sc->sc_pr.used;
2872 r->seek = sc->sc_rr.used;
2873
2874 p->samples = sc->sc_pr.stamp - sc->sc_pr.drops;
2875 r->samples = sc->sc_rr.stamp - sc->sc_rr.drops;
2876
2877 p->eof = sc->sc_eof;
2878 r->eof = 0;
2879
2880 p->pause = sc->sc_pr.pause;
2881 r->pause = sc->sc_rr.pause;
2882
2883 p->error = sc->sc_pr.drops != 0;
2884 r->error = sc->sc_rr.drops != 0;
2885
2886 p->waiting = r->waiting = 0; /* open never hangs */
2887
2888 p->open = (sc->sc_open & AUOPEN_WRITE) != 0;
2889 r->open = (sc->sc_open & AUOPEN_READ) != 0;
2890
2891 p->active = sc->sc_pbus;
2892 r->active = sc->sc_rbus;
2893
2894 p->buffer_size = sc->sc_pr.bufsize;
2895 r->buffer_size = sc->sc_rr.bufsize;
2896
2897 ai->blocksize = sc->sc_pr.blksize;
2898 ai->hiwat = sc->sc_pr.usedhigh / sc->sc_pr.blksize;
2899 ai->lowat = sc->sc_pr.usedlow / sc->sc_pr.blksize;
2900 ai->mode = sc->sc_mode;
2901
2902 return (0);
2903 }
2904
2905 /*
2906 * Mixer driver
2907 */
2908 int
mixer_open(dev,sc,flags,ifmt,p)2909 mixer_open(dev, sc, flags, ifmt, p)
2910 dev_t dev;
2911 struct audio_softc *sc;
2912 int flags, ifmt;
2913 struct proc *p;
2914 {
2915 DPRINTF(("mixer_open: dev=0x%x flags=0x%x sc=%p\n", dev, flags, sc));
2916
2917 return (0);
2918 }
2919
2920 /*
2921 * Remove a process from those to be signalled on mixer activity.
2922 */
2923 static void
mixer_remove(sc,p)2924 mixer_remove(sc, p)
2925 struct audio_softc *sc;
2926 struct proc *p;
2927 {
2928 struct mixer_asyncs **pm, *m;
2929
2930 for(pm = &sc->sc_async_mixer; *pm; pm = &(*pm)->next) {
2931 if ((*pm)->proc == p) {
2932 m = *pm;
2933 *pm = m->next;
2934 free(m, M_DEVBUF);
2935 return;
2936 }
2937 }
2938 }
2939
2940 /*
2941 * Signal all processes waitinf for the mixer.
2942 */
2943 static void
mixer_signal(sc)2944 mixer_signal(sc)
2945 struct audio_softc *sc;
2946 {
2947 struct mixer_asyncs *m;
2948
2949 for(m = sc->sc_async_mixer; m; m = m->next)
2950 psignal(m->proc, SIGIO);
2951 }
2952
2953 /*
2954 * Close a mixer device
2955 */
2956 /* ARGSUSED */
2957 int
mixer_close(dev,flags,ifmt,p)2958 mixer_close(dev, flags, ifmt, p)
2959 dev_t dev;
2960 int flags, ifmt;
2961 struct proc *p;
2962 {
2963 int unit = AUDIOUNIT(dev);
2964 struct audio_softc *sc = audio_cd.cd_devs[unit];
2965
2966 DPRINTF(("mixer_close: unit %d\n", AUDIOUNIT(dev)));
2967
2968 mixer_remove(sc, p);
2969
2970 return (0);
2971 }
2972
2973 int
mixer_ioctl(dev,cmd,addr,flag,p)2974 mixer_ioctl(dev, cmd, addr, flag, p)
2975 dev_t dev;
2976 u_long cmd;
2977 caddr_t addr;
2978 int flag;
2979 struct proc *p;
2980 {
2981 int unit = AUDIOUNIT(dev);
2982 struct audio_softc *sc = audio_cd.cd_devs[unit];
2983 struct audio_hw_if *hw = sc->hw_if;
2984 int error = EINVAL;
2985
2986 DPRINTF(("mixer_ioctl(%d,'%c',%d)\n",
2987 IOCPARM_LEN(cmd), IOCGROUP(cmd), cmd&0xff));
2988
2989 switch (cmd) {
2990 case FIOASYNC:
2991 mixer_remove(sc, p); /* remove old entry */
2992 if (*(int *)addr) {
2993 struct mixer_asyncs *ma;
2994 ma = malloc(sizeof (struct mixer_asyncs),
2995 M_DEVBUF, M_WAITOK);
2996 ma->next = sc->sc_async_mixer;
2997 ma->proc = p;
2998 sc->sc_async_mixer = ma;
2999 }
3000 error = 0;
3001 break;
3002
3003 case AUDIO_GETDEV:
3004 DPRINTF(("AUDIO_GETDEV\n"));
3005 error = hw->getdev(sc->hw_hdl, (audio_device_t *)addr);
3006 break;
3007
3008 case AUDIO_MIXER_DEVINFO:
3009 DPRINTF(("AUDIO_MIXER_DEVINFO\n"));
3010 ((mixer_devinfo_t *)addr)->un.v.delta = 0; /* default */
3011 error = hw->query_devinfo(sc->hw_hdl, (mixer_devinfo_t *)addr);
3012 break;
3013
3014 case AUDIO_MIXER_READ:
3015 DPRINTF(("AUDIO_MIXER_READ\n"));
3016 error = hw->get_port(sc->hw_hdl, (mixer_ctrl_t *)addr);
3017 break;
3018
3019 case AUDIO_MIXER_WRITE:
3020 if (!(flag & FWRITE))
3021 return (EACCES);
3022 DPRINTF(("AUDIO_MIXER_WRITE\n"));
3023 error = hw->set_port(sc->hw_hdl, (mixer_ctrl_t *)addr);
3024 if (!error && hw->commit_settings)
3025 error = hw->commit_settings(sc->hw_hdl);
3026 if (!error)
3027 mixer_signal(sc);
3028 break;
3029
3030 default:
3031 error = ENOTTY;
3032 break;
3033 }
3034 DPRINTF(("mixer_ioctl(%d,'%c',%d) result %d\n",
3035 IOCPARM_LEN(cmd), IOCGROUP(cmd), cmd&0xff, error));
3036 return (error);
3037 }
3038 #endif
3039
3040 int
audiokqfilter(dev,kn)3041 audiokqfilter(dev, kn)
3042 dev_t dev;
3043 struct knote *kn;
3044 {
3045 int unit = AUDIOUNIT(dev);
3046 struct audio_softc *sc = audio_cd.cd_devs[unit];
3047 struct klist *klist;
3048 int s;
3049
3050 switch (kn->kn_filter) {
3051 case EVFILT_READ:
3052 klist = &sc->sc_rsel.si_note;
3053 kn->kn_fop = &audioread_filtops;
3054 break;
3055 case EVFILT_WRITE:
3056 klist = &sc->sc_wsel.si_note;
3057 kn->kn_fop = &audiowrite_filtops;
3058 break;
3059 default:
3060 return (1);
3061 }
3062 kn->kn_hook = (void *)sc;
3063
3064 s = splaudio();
3065 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
3066 splx(s);
3067
3068 return (0);
3069 }
3070
3071 void
filt_audiordetach(struct knote * kn)3072 filt_audiordetach(struct knote *kn)
3073 {
3074 struct audio_softc *sc = (struct audio_softc *)kn->kn_hook;
3075 int s = splaudio();
3076
3077 SLIST_REMOVE(&sc->sc_rsel.si_note, kn, knote, kn_selnext);
3078 splx(s);
3079 }
3080
3081 int
filt_audioread(struct knote * kn,long hint)3082 filt_audioread(struct knote *kn, long hint)
3083 {
3084 struct audio_softc *sc = (struct audio_softc *)kn->kn_hook;
3085
3086 return AUDIO_FILTREAD(sc);
3087 }
3088
3089 void
filt_audiowdetach(struct knote * kn)3090 filt_audiowdetach(struct knote *kn)
3091 {
3092 struct audio_softc *sc = (struct audio_softc *)kn->kn_hook;
3093 int s = splaudio();
3094
3095 SLIST_REMOVE(&sc->sc_wsel.si_note, kn, knote, kn_selnext);
3096 splx(s);
3097 }
3098
3099 int
filt_audiowrite(struct knote * kn,long hint)3100 filt_audiowrite(struct knote *kn, long hint)
3101 {
3102 struct audio_softc *sc = (struct audio_softc *)kn->kn_hook;
3103
3104 return AUDIO_FILTWRITE(sc);
3105 }
3106