cms.c revision 1.18 1 1.18 martin /* $NetBSD: cms.c,v 1.18 2008/04/28 20:23:52 martin Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.1 augustss * Redistribution and use in source and binary forms, with or without
8 1.1 augustss * modification, are permitted provided that the following conditions
9 1.1 augustss * are met:
10 1.1 augustss * 1. Redistributions of source code must retain the above copyright
11 1.1 augustss * notice, this list of conditions and the following disclaimer.
12 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 augustss * notice, this list of conditions and the following disclaimer in the
14 1.1 augustss * documentation and/or other materials provided with the distribution.
15 1.1 augustss *
16 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
27 1.1 augustss */
28 1.3 lukem
29 1.3 lukem #include <sys/cdefs.h>
30 1.18 martin __KERNEL_RCSID(0, "$NetBSD: cms.c,v 1.18 2008/04/28 20:23:52 martin Exp $");
31 1.1 augustss
32 1.1 augustss #include <sys/param.h>
33 1.1 augustss #include <sys/systm.h>
34 1.1 augustss #include <sys/kernel.h>
35 1.1 augustss #include <sys/device.h>
36 1.1 augustss #include <sys/select.h>
37 1.1 augustss
38 1.15 ad #include <sys/bus.h>
39 1.1 augustss
40 1.1 augustss #include <sys/audioio.h>
41 1.1 augustss #include <dev/audio_if.h>
42 1.1 augustss #include <dev/audiovar.h>
43 1.1 augustss
44 1.1 augustss #include <sys/midiio.h>
45 1.1 augustss #include <dev/midi_if.h>
46 1.1 augustss #include <dev/midivar.h>
47 1.1 augustss #include <dev/midisynvar.h>
48 1.1 augustss
49 1.1 augustss #include <dev/isa/isareg.h>
50 1.1 augustss #include <dev/isa/isavar.h>
51 1.1 augustss #include <dev/isa/cmsreg.h>
52 1.1 augustss
53 1.1 augustss #ifdef AUDIO_DEBUG
54 1.1 augustss #define DPRINTF(x) if (cmsdebug) printf x
55 1.1 augustss int cmsdebug = 0;
56 1.1 augustss #else
57 1.1 augustss #define DPRINTF(x)
58 1.1 augustss #endif
59 1.1 augustss
60 1.1 augustss struct cms_softc {
61 1.1 augustss struct midi_softc sc_mididev;
62 1.1 augustss
63 1.1 augustss bus_space_tag_t sc_iot;
64 1.1 augustss bus_space_handle_t sc_ioh;
65 1.1 augustss
66 1.1 augustss /* shadow registers for each chip */
67 1.1 augustss u_int8_t sc_shadowregs[32*2];
68 1.1 augustss midisyn sc_midisyn;
69 1.1 augustss };
70 1.1 augustss
71 1.16 cube int cms_probe(device_t, cfdata_t, void *);
72 1.16 cube void cms_attach(device_t, device_t, void *);
73 1.1 augustss
74 1.16 cube CFATTACH_DECL_NEW(cms, sizeof(struct cms_softc),
75 1.7 thorpej cms_probe, cms_attach, NULL, NULL);
76 1.1 augustss
77 1.9 perry int cms_open(midisyn *, int);
78 1.9 perry void cms_close(midisyn *);
79 1.12 chap void cms_on(midisyn *, uint_fast16_t, midipitch_t, int16_t);
80 1.12 chap void cms_off(midisyn *, uint_fast16_t, uint_fast8_t);
81 1.1 augustss
82 1.1 augustss struct midisyn_methods midi_cms_hw = {
83 1.12 chap .open = cms_open,
84 1.12 chap .close = cms_close,
85 1.12 chap .attackv = cms_on,
86 1.12 chap .releasev = cms_off,
87 1.1 augustss };
88 1.1 augustss
89 1.9 perry static void cms_reset(struct cms_softc *);
90 1.1 augustss
91 1.1 augustss static char cms_note_table[] = {
92 1.1 augustss /* A */ 3,
93 1.1 augustss /* A# */ 31,
94 1.1 augustss /* B */ 58,
95 1.1 augustss /* C */ 83,
96 1.1 augustss /* C# */ 107,
97 1.1 augustss /* D */ 130,
98 1.1 augustss /* D# */ 151,
99 1.1 augustss /* E */ 172,
100 1.1 augustss /* F */ 191,
101 1.1 augustss /* F# */ 209,
102 1.1 augustss /* G */ 226,
103 1.1 augustss /* G# */ 242,
104 1.1 augustss };
105 1.1 augustss
106 1.1 augustss #define NOTE_TO_OCTAVE(note) (((note)-CMS_FIRST_NOTE)/12)
107 1.1 augustss #define NOTE_TO_COUNT(note) cms_note_table[(((note)-CMS_FIRST_NOTE)%12)]
108 1.1 augustss
109 1.1 augustss int
110 1.16 cube cms_probe(device_t parent, cfdata_t match, void *aux)
111 1.1 augustss {
112 1.1 augustss struct isa_attach_args *ia = aux;
113 1.1 augustss bus_space_tag_t iot;
114 1.1 augustss bus_space_handle_t ioh;
115 1.1 augustss int found = 0;
116 1.1 augustss int i;
117 1.1 augustss
118 1.1 augustss DPRINTF(("cms_probe():\n"));
119 1.1 augustss
120 1.1 augustss iot = ia->ia_iot;
121 1.1 augustss
122 1.4 thorpej if (ia->ia_nio < 1)
123 1.1 augustss return 0;
124 1.1 augustss
125 1.4 thorpej if (ISA_DIRECT_CONFIG(ia))
126 1.4 thorpej return 0;
127 1.4 thorpej
128 1.8 drochner if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
129 1.4 thorpej return 0;
130 1.4 thorpej
131 1.4 thorpej if (bus_space_map(iot, ia->ia_io[0].ir_addr, CMS_IOSIZE, 0, &ioh))
132 1.1 augustss return 0;
133 1.1 augustss
134 1.1 augustss bus_space_write_1(iot, ioh, CMS_WREG, 0xaa);
135 1.1 augustss if (bus_space_read_1(iot, ioh, CMS_RREG) != 0xaa)
136 1.1 augustss goto out;
137 1.1 augustss
138 1.1 augustss for (i = 0; i < 8; i++) {
139 1.1 augustss if (bus_space_read_1(iot, ioh, CMS_MREG) != 0x7f)
140 1.1 augustss goto out;
141 1.1 augustss }
142 1.1 augustss found = 1;
143 1.1 augustss
144 1.4 thorpej ia->ia_nio = 1;
145 1.4 thorpej ia->ia_io[0].ir_size = CMS_IOSIZE;
146 1.4 thorpej
147 1.4 thorpej ia->ia_niomem = 0;
148 1.4 thorpej ia->ia_nirq = 0;
149 1.4 thorpej ia->ia_ndrq = 0;
150 1.1 augustss
151 1.1 augustss out:
152 1.1 augustss bus_space_unmap(iot, ioh, CMS_IOSIZE);
153 1.1 augustss
154 1.1 augustss return found;
155 1.1 augustss }
156 1.1 augustss
157 1.1 augustss
158 1.1 augustss void
159 1.16 cube cms_attach(device_t parent, device_t self, void *aux)
160 1.1 augustss {
161 1.16 cube struct cms_softc *sc = device_private(self);
162 1.1 augustss struct isa_attach_args *ia = aux;
163 1.1 augustss bus_space_tag_t iot;
164 1.1 augustss bus_space_handle_t ioh;
165 1.1 augustss midisyn *ms;
166 1.1 augustss struct audio_attach_args arg;
167 1.1 augustss
168 1.17 cube sc->sc_mididev.dev = self;
169 1.16 cube
170 1.16 cube aprint_normal("\n");
171 1.1 augustss
172 1.1 augustss DPRINTF(("cms_attach():\n"));
173 1.1 augustss
174 1.1 augustss iot = ia->ia_iot;
175 1.1 augustss
176 1.4 thorpej if (bus_space_map(iot, ia->ia_io[0].ir_addr, CMS_IOSIZE, 0, &ioh)) {
177 1.16 cube aprint_error_dev(self, "can't map i/o space\n");
178 1.1 augustss return;
179 1.1 augustss }
180 1.1 augustss
181 1.1 augustss sc->sc_iot = iot;
182 1.1 augustss sc->sc_ioh = ioh;
183 1.1 augustss
184 1.1 augustss /* now let's reset the chips */
185 1.1 augustss cms_reset(sc);
186 1.1 augustss
187 1.1 augustss ms = &sc->sc_midisyn;
188 1.1 augustss ms->mets = &midi_cms_hw;
189 1.1 augustss strcpy(ms->name, "Creative Music System");
190 1.1 augustss ms->nvoice = CMS_NVOICES;
191 1.1 augustss ms->data = sc;
192 1.1 augustss
193 1.1 augustss /* use the synthesiser */
194 1.1 augustss midisyn_attach(&sc->sc_mididev, ms);
195 1.1 augustss
196 1.1 augustss /* now attach the midi device to the synthesiser */
197 1.1 augustss arg.type = AUDIODEV_TYPE_MIDI;
198 1.1 augustss arg.hwif = sc->sc_mididev.hw_if;
199 1.1 augustss arg.hdl = sc->sc_mididev.hw_hdl;
200 1.16 cube config_found(self, &arg, 0);
201 1.1 augustss }
202 1.1 augustss
203 1.1 augustss
204 1.1 augustss int
205 1.14 christos cms_open(midisyn *ms, int flag)
206 1.1 augustss {
207 1.1 augustss struct cms_softc *sc = (struct cms_softc *)ms->data;
208 1.1 augustss
209 1.1 augustss cms_reset(sc);
210 1.1 augustss
211 1.1 augustss return 0;
212 1.1 augustss }
213 1.1 augustss
214 1.1 augustss void
215 1.16 cube cms_close(midisyn *ms)
216 1.1 augustss {
217 1.1 augustss struct cms_softc *sc = (struct cms_softc *)ms->data;
218 1.1 augustss
219 1.1 augustss cms_reset(sc);
220 1.1 augustss }
221 1.1 augustss
222 1.1 augustss void
223 1.12 chap cms_on(midisyn *ms, uint_fast16_t vidx, midipitch_t mp, int16_t level_cB)
224 1.1 augustss {
225 1.1 augustss struct cms_softc *sc = (struct cms_softc *)ms->data;
226 1.12 chap int chip = CHAN_TO_CHIP(vidx);
227 1.12 chap int voice = CHAN_TO_VOICE(vidx);
228 1.12 chap uint32_t note;
229 1.1 augustss u_int8_t octave;
230 1.1 augustss u_int8_t count;
231 1.1 augustss u_int8_t reg;
232 1.1 augustss u_int8_t vol;
233 1.12 chap
234 1.12 chap /*
235 1.12 chap * The next line is a regrettable hack, because it drops all pitch
236 1.12 chap * adjustment midisyn has supplied in miditune, so this synth will
237 1.12 chap * not respond to tuning, pitchbend, etc. It seems it ought to be
238 1.12 chap * possible to DTRT if the formula that generated the cms_note_table
239 1.12 chap * can be found, but I've had no luck, and a plot of the numbers in
240 1.12 chap * the table clearly curves the wrong way to be derived any obvious
241 1.12 chap * way from the equal tempered scale. (Or maybe the table's wrong?
242 1.12 chap * Has this device been playing flat up the scale? I don't have
243 1.12 chap * access to one to try.)
244 1.12 chap */
245 1.12 chap note = MIDIPITCH_TO_KEY(mp);
246 1.1 augustss
247 1.1 augustss if (note < CMS_FIRST_NOTE)
248 1.1 augustss return;
249 1.1 augustss
250 1.1 augustss octave = NOTE_TO_OCTAVE(note);
251 1.1 augustss count = NOTE_TO_COUNT(note);
252 1.1 augustss
253 1.1 augustss DPRINTF(("chip=%d voice=%d octave=%d count=%d offset=%d shift=%d\n",
254 1.1 augustss chip, voice, octave, count, OCTAVE_OFFSET(voice),
255 1.1 augustss OCTAVE_SHIFT(voice)));
256 1.1 augustss
257 1.1 augustss /* write the count */
258 1.1 augustss CMS_WRITE(sc, chip, CMS_IREG_FREQ0 + voice, count);
259 1.1 augustss
260 1.1 augustss /* select the octave */
261 1.1 augustss reg = CMS_READ(sc, chip, CMS_IREG_OCTAVE_1_0 + OCTAVE_OFFSET(voice));
262 1.1 augustss reg &= ~(0x0f<<OCTAVE_SHIFT(voice));
263 1.1 augustss reg |= ((octave&0x7)<<OCTAVE_SHIFT(voice));
264 1.1 augustss CMS_WRITE(sc, chip, CMS_IREG_OCTAVE_1_0 + OCTAVE_OFFSET(voice), reg);
265 1.1 augustss
266 1.1 augustss /* set the volume */
267 1.12 chap /* this may be the wrong curve but will do something. no docs! */
268 1.12 chap vol = 15 + (level_cB > -75) ? level_cB/5 : -15;
269 1.1 augustss CMS_WRITE(sc, chip, CMS_IREG_VOL0 + voice, ((vol<<4)|vol));
270 1.1 augustss
271 1.1 augustss /* enable the voice */
272 1.1 augustss reg = CMS_READ(sc, chip, CMS_IREG_FREQ_CTL);
273 1.1 augustss reg |= (1<<voice);
274 1.10 perry CMS_WRITE(sc, chip, CMS_IREG_FREQ_CTL, reg);
275 1.1 augustss }
276 1.1 augustss
277 1.1 augustss void
278 1.14 christos cms_off(midisyn *ms, uint_fast16_t vidx, uint_fast8_t vel)
279 1.1 augustss {
280 1.1 augustss struct cms_softc *sc = (struct cms_softc *)ms->data;
281 1.12 chap int chip = CHAN_TO_CHIP(vidx);
282 1.12 chap int voice = CHAN_TO_VOICE(vidx);
283 1.1 augustss u_int8_t reg;
284 1.1 augustss
285 1.1 augustss /* disable the channel */
286 1.1 augustss reg = CMS_READ(sc, chip, CMS_IREG_FREQ_CTL);
287 1.1 augustss reg &= ~(1<<voice);
288 1.10 perry CMS_WRITE(sc, chip, CMS_IREG_FREQ_CTL, reg);
289 1.1 augustss }
290 1.1 augustss
291 1.1 augustss static void
292 1.16 cube cms_reset(struct cms_softc *sc)
293 1.1 augustss {
294 1.1 augustss int i;
295 1.1 augustss
296 1.1 augustss DPRINTF(("cms_reset():\n"));
297 1.1 augustss
298 1.1 augustss for (i = 0; i < 6; i++) {
299 1.1 augustss CMS_WRITE(sc, 0, CMS_IREG_VOL0+i, 0x00);
300 1.1 augustss CMS_WRITE(sc, 1, CMS_IREG_VOL0+i, 0x00);
301 1.1 augustss
302 1.1 augustss CMS_WRITE(sc, 0, CMS_IREG_FREQ0+i, 0x00);
303 1.1 augustss CMS_WRITE(sc, 1, CMS_IREG_FREQ0+i, 0x00);
304 1.1 augustss }
305 1.1 augustss
306 1.1 augustss for (i = 0; i < 3; i++) {
307 1.10 perry CMS_WRITE(sc, 0, CMS_IREG_OCTAVE_1_0+i, 0x00);
308 1.10 perry CMS_WRITE(sc, 1, CMS_IREG_OCTAVE_1_0+i, 0x00);
309 1.1 augustss }
310 1.1 augustss
311 1.1 augustss CMS_WRITE(sc, 0, CMS_IREG_FREQ_CTL, 0x00);
312 1.1 augustss CMS_WRITE(sc, 1, CMS_IREG_FREQ_CTL, 0x00);
313 1.1 augustss
314 1.1 augustss CMS_WRITE(sc, 0, CMS_IREG_NOISE_CTL, 0x00);
315 1.1 augustss CMS_WRITE(sc, 1, CMS_IREG_NOISE_CTL, 0x00);
316 1.1 augustss
317 1.1 augustss CMS_WRITE(sc, 0, CMS_IREG_NOISE_BW, 0x00);
318 1.1 augustss CMS_WRITE(sc, 1, CMS_IREG_NOISE_BW, 0x00);
319 1.1 augustss
320 1.1 augustss /*
321 1.1 augustss * These registers don't appear to be useful, but must be
322 1.1 augustss * cleared otherwise certain voices don't work properly
323 1.1 augustss */
324 1.1 augustss CMS_WRITE(sc, 0, 0x18, 0x00);
325 1.1 augustss CMS_WRITE(sc, 1, 0x18, 0x00);
326 1.1 augustss CMS_WRITE(sc, 0, 0x19, 0x00);
327 1.1 augustss CMS_WRITE(sc, 1, 0x19, 0x00);
328 1.1 augustss
329 1.1 augustss CMS_WRITE(sc, 0, CMS_IREG_SYS_CTL, CMS_IREG_SYS_RESET);
330 1.1 augustss CMS_WRITE(sc, 1, CMS_IREG_SYS_CTL, CMS_IREG_SYS_RESET);
331 1.1 augustss
332 1.1 augustss CMS_WRITE(sc, 0, CMS_IREG_SYS_CTL, CMS_IREG_SYS_ENBL);
333 1.1 augustss CMS_WRITE(sc, 1, CMS_IREG_SYS_CTL, CMS_IREG_SYS_ENBL);
334 1.1 augustss }
335