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