xref: /netbsd-src/sys/dev/isa/midi_pcppi.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: midi_pcppi.c,v 1.18 2008/03/05 10:48:48 cube 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.18 2008/03/05 10:48:48 cube 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 <sys/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(device_t, cfdata_t , void *);
71 void	midi_pcppi_attach(device_t, device_t, 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_NEW(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(device_t parent, cfdata_t match, void *aux)
92 {
93 	return (!midi_pcppi_attached);
94 }
95 
96 void
97 midi_pcppi_attach(device_t parent, device_t self, void *aux)
98 {
99 	struct midi_pcppi_softc *sc = device_private(self);
100 	struct pcppi_attach_args *pa = (struct pcppi_attach_args *)aux;
101 	midisyn *ms;
102 
103 	sc->sc_mididev.dev = self;
104 	ms = &sc->sc_midisyn;
105 	ms->mets = &midi_pcppi_hw;
106 	strcpy(ms->name, "PC speaker");
107 	ms->nvoice = 1;
108 	ms->data = pa->pa_cookie;
109 
110 	midi_pcppi_attached++;
111 
112 	midisyn_attach(&sc->sc_mididev, ms);
113 	midi_attach(&sc->sc_mididev, parent);
114         if (!device_pmf_is_registered(self))
115 		if (!pmf_device_register(self, NULL, NULL))
116 			aprint_error_dev(self,
117 			    "couldn't establish power handler\n");
118 }
119 
120 void
121 midi_pcppi_on(midisyn *ms,
122     uint_fast16_t voice, midipitch_t mp, int16_t level)
123 {
124 	pcppi_tag_t t = ms->data;
125 
126 	pcppi_bell(t,
127 	           MIDIHZ18_TO_HZ(MIDIPITCH_TO_HZ18(mp)),
128 	           MAX_DURATION * hz, 0);
129 }
130 
131 void
132 midi_pcppi_off(midisyn *ms, uint_fast16_t voice, uint_fast8_t vel)
133 {
134 	pcppi_tag_t t = ms->data;
135 
136 	/*printf("OFF %p %d\n", t, note >> 16);*/
137 	pcppi_bell(t, 0, 0, 0);
138 }
139 
140 void
141 midi_pcppi_close(ms)
142 	midisyn *ms;
143 {
144 	pcppi_tag_t t = ms->data;
145 
146 	/* Make sure we are quiet. */
147 	pcppi_bell(t, 0, 0, 0);
148 }
149 
150 static void
151 midi_pcppi_repitchv(midisyn *ms, uint_fast16_t voice, midipitch_t newpitch)
152 {
153 	midi_pcppi_on(ms, voice, newpitch, 64);
154 }
155