xref: /netbsd/sys/dev/isa/midi_pcppi.c (revision bf9ec67e)
1 /*	$NetBSD: midi_pcppi.c,v 1.5 2001/11/13 08:01:25 lukem 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@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.5 2001/11/13 08:01:25 lukem 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 <dev/isa/pcppivar.h>
55 
56 #include <dev/audio_if.h>
57 #include <dev/midi_if.h>
58 #include <dev/midivar.h>
59 #include <dev/midisynvar.h>
60 
61 #define MAX_DURATION 30		/* turn off sound automagically after 30 s */
62 
63 struct midi_pcppi_softc {
64 	struct midi_softc sc_mididev;
65 	midisyn sc_midisyn;
66 };
67 
68 int	midi_pcppi_match __P((struct device *, struct cfdata *, void *));
69 void	midi_pcppi_attach __P((struct device *, struct device *, void *));
70 
71 void	midi_pcppi_on    __P((midisyn *, u_int32_t, u_int32_t, u_int32_t));
72 void	midi_pcppi_off   __P((midisyn *, u_int32_t, u_int32_t, u_int32_t));
73 void	midi_pcppi_close __P((midisyn *));
74 
75 struct cfattach midi_pcppi_ca = {
76 	sizeof(struct midi_pcppi_softc), midi_pcppi_match, midi_pcppi_attach
77 };
78 
79 struct midisyn_methods midi_pcppi_hw = {
80 	0,			/* open */
81 	midi_pcppi_close,
82 	0,			/* ioctl */
83 	0,			/* allocv */
84 	midi_pcppi_on,
85 	midi_pcppi_off,
86 	0,
87 	0,
88 	0,
89 	0,
90 	0,
91 	0,
92 };
93 
94 int midi_pcppi_attached = 0;	/* Not very nice */
95 
96 int
97 midi_pcppi_match(parent, match, aux)
98 	struct device *parent;
99 	struct cfdata *match;
100 	void *aux;
101 {
102 	return (!midi_pcppi_attached);
103 }
104 
105 void
106 midi_pcppi_attach(parent, self, aux)
107 	struct device *parent;
108 	struct device *self;
109 	void *aux;
110 {
111 	struct midi_pcppi_softc *sc = (struct midi_pcppi_softc *)self;
112 	struct pcppi_attach_args *pa = (struct pcppi_attach_args *)aux;
113 	midisyn *ms;
114 
115 	ms = &sc->sc_midisyn;
116 	ms->mets = &midi_pcppi_hw;
117 	strcpy(ms->name, "PC speaker");
118 	ms->nvoice = 1;
119 	ms->flags = MS_DOALLOC | MS_FREQXLATE;
120 	ms->data = pa->pa_cookie;
121 
122 	midi_pcppi_attached++;
123 
124 	midisyn_attach(&sc->sc_mididev, ms);
125 	midi_attach(&sc->sc_mididev, parent);
126 }
127 
128 void
129 midi_pcppi_on(ms, chan, note, vel)
130 	midisyn *ms;
131 	u_int32_t chan, note, vel;
132 {
133 	pcppi_tag_t t = ms->data;
134 
135 	/*printf("ON  %p %d\n", t, MIDISYN_FREQ_TO_HZ(note));*/
136 	pcppi_bell(t, MIDISYN_FREQ_TO_HZ(note), MAX_DURATION * hz, 0);
137 }
138 
139 void
140 midi_pcppi_off(ms, chan, note, vel)
141 	midisyn *ms;
142 	u_int32_t chan, note, vel;
143 {
144 	pcppi_tag_t t = ms->data;
145 
146 	/*printf("OFF %p %d\n", t, note >> 16);*/
147 	pcppi_bell(t, 0, 0, 0);
148 }
149 
150 void
151 midi_pcppi_close(ms)
152 	midisyn *ms;
153 {
154 	pcppi_tag_t t = ms->data;
155 
156 	/* Make sure we are quiet. */
157 	pcppi_bell(t, 0, 0, 0);
158 }
159