midi_pcppi.c revision 1.13 1 /* $NetBSD: midi_pcppi.c,v 1.13 2006/06/30 13:56:25 chap Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss (at) NetBSD.org).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: midi_pcppi.c,v 1.13 2006/06/30 13:56:25 chap Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/errno.h>
46 #include <sys/device.h>
47 #include <sys/malloc.h>
48 #include <sys/proc.h>
49 #include <sys/conf.h>
50 #include <sys/select.h>
51 #include <sys/audioio.h>
52 #include <sys/midiio.h>
53
54 #include <machine/bus.h>
55
56 #include <dev/isa/pcppivar.h>
57
58 #include <dev/audio_if.h>
59 #include <dev/midi_if.h>
60 #include <dev/midivar.h>
61 #include <dev/midisynvar.h>
62
63 #define MAX_DURATION 30 /* turn off sound automagically after 30 s */
64
65 struct midi_pcppi_softc {
66 struct midi_softc sc_mididev;
67 midisyn sc_midisyn;
68 };
69
70 int midi_pcppi_match(struct device *, struct cfdata *, void *);
71 void midi_pcppi_attach(struct device *, struct device *, void *);
72
73 void midi_pcppi_on (midisyn *, uint_fast16_t, midipitch_t, int16_t);
74 void midi_pcppi_off (midisyn *, uint_fast16_t, uint_fast8_t);
75 void midi_pcppi_close(midisyn *);
76 static void midi_pcppi_repitchv(midisyn *, uint_fast16_t, midipitch_t);
77
78 CFATTACH_DECL(midi_pcppi, sizeof(struct midi_pcppi_softc),
79 midi_pcppi_match, midi_pcppi_attach, NULL, NULL);
80
81 struct midisyn_methods midi_pcppi_hw = {
82 .close = midi_pcppi_close,
83 .attackv = midi_pcppi_on,
84 .releasev = midi_pcppi_off,
85 .repitchv = midi_pcppi_repitchv,
86 };
87
88 int midi_pcppi_attached = 0; /* Not very nice */
89
90 int
91 midi_pcppi_match(parent, match, aux)
92 struct device *parent;
93 struct cfdata *match;
94 void *aux;
95 {
96 return (!midi_pcppi_attached);
97 }
98
99 void
100 midi_pcppi_attach(parent, self, aux)
101 struct device *parent;
102 struct device *self;
103 void *aux;
104 {
105 struct midi_pcppi_softc *sc = (struct midi_pcppi_softc *)self;
106 struct pcppi_attach_args *pa = (struct pcppi_attach_args *)aux;
107 midisyn *ms;
108
109 ms = &sc->sc_midisyn;
110 ms->mets = &midi_pcppi_hw;
111 strcpy(ms->name, "PC speaker");
112 ms->nvoice = 1;
113 ms->data = pa->pa_cookie;
114
115 midi_pcppi_attached++;
116
117 midisyn_attach(&sc->sc_mididev, ms);
118 midi_attach(&sc->sc_mididev, parent);
119 }
120
121 void
122 midi_pcppi_on(midisyn *ms,
123 uint_fast16_t voice, midipitch_t mp, int16_t level)
124 {
125 pcppi_tag_t t = ms->data;
126
127 pcppi_bell(t,
128 MIDIHZ18_TO_HZ(MIDIPITCH_TO_HZ18(mp)),
129 MAX_DURATION * hz, 0);
130 }
131
132 void
133 midi_pcppi_off(midisyn *ms, uint_fast16_t voice, uint_fast8_t vel)
134 {
135 pcppi_tag_t t = ms->data;
136
137 /*printf("OFF %p %d\n", t, note >> 16);*/
138 pcppi_bell(t, 0, 0, 0);
139 }
140
141 void
142 midi_pcppi_close(ms)
143 midisyn *ms;
144 {
145 pcppi_tag_t t = ms->data;
146
147 /* Make sure we are quiet. */
148 pcppi_bell(t, 0, 0, 0);
149 }
150
151 static void
152 midi_pcppi_repitchv(midisyn *ms, uint_fast16_t voice, midipitch_t newpitch)
153 {
154 midi_pcppi_on(ms, voice, newpitch, 64);
155 }
156