| 1 | /* $NetBSD: midi_pcppi.c,v 1.26 2012/04/09 10:18:17 plunky Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1998, 2008 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), and by Andrew Doran. |
| 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 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | * POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #include <sys/cdefs.h> |
| 33 | __KERNEL_RCSID(0, "$NetBSD: midi_pcppi.c,v 1.26 2012/04/09 10:18:17 plunky Exp $" ); |
| 34 | |
| 35 | #include <sys/param.h> |
| 36 | #include <sys/systm.h> |
| 37 | #include <sys/kernel.h> |
| 38 | #include <sys/errno.h> |
| 39 | #include <sys/device.h> |
| 40 | #include <sys/proc.h> |
| 41 | #include <sys/conf.h> |
| 42 | #include <sys/select.h> |
| 43 | #include <sys/audioio.h> |
| 44 | #include <sys/midiio.h> |
| 45 | #include <sys/tty.h> |
| 46 | #include <sys/bus.h> |
| 47 | |
| 48 | #include <dev/isa/pcppivar.h> |
| 49 | |
| 50 | #include <dev/audio_if.h> |
| 51 | #include <dev/midi_if.h> |
| 52 | #include <dev/midivar.h> |
| 53 | #include <dev/midisynvar.h> |
| 54 | |
| 55 | #define MAX_DURATION 30 /* turn off sound automagically after 30 s */ |
| 56 | |
| 57 | struct midi_pcppi_softc { |
| 58 | struct midi_softc sc_mididev; |
| 59 | midisyn sc_midisyn; |
| 60 | }; |
| 61 | |
| 62 | static int midi_pcppi_match(device_t, cfdata_t , void *); |
| 63 | static void midi_pcppi_attach(device_t, device_t, void *); |
| 64 | static int midi_pcppi_detach(device_t, int); |
| 65 | |
| 66 | static void midi_pcppi_on (midisyn *, uint_fast16_t, midipitch_t, int16_t); |
| 67 | static void midi_pcppi_off (midisyn *, uint_fast16_t, uint_fast8_t); |
| 68 | static void midi_pcppi_close(midisyn *); |
| 69 | static void midi_pcppi_repitchv(midisyn *, uint_fast16_t, midipitch_t); |
| 70 | |
| 71 | CFATTACH_DECL3_NEW(midi_pcppi, sizeof(struct midi_pcppi_softc), |
| 72 | midi_pcppi_match, midi_pcppi_attach, midi_pcppi_detach, NULL, NULL, NULL, |
| 73 | DVF_DETACH_SHUTDOWN); |
| 74 | |
| 75 | static struct midisyn_methods midi_pcppi_hw = { |
| 76 | .close = midi_pcppi_close, |
| 77 | .attackv = midi_pcppi_on, |
| 78 | .releasev = midi_pcppi_off, |
| 79 | .repitchv = midi_pcppi_repitchv, |
| 80 | }; |
| 81 | |
| 82 | int midi_pcppi_attached = 0; /* Not very nice */ |
| 83 | |
| 84 | static int |
| 85 | midi_pcppi_match(device_t parent, cfdata_t match, void *aux) |
| 86 | { |
| 87 | return (!midi_pcppi_attached); |
| 88 | } |
| 89 | |
| 90 | static void |
| 91 | midi_pcppi_attach(device_t parent, device_t self, void *aux) |
| 92 | { |
| 93 | struct midi_pcppi_softc *sc = device_private(self); |
| 94 | struct pcppi_attach_args *pa = (struct pcppi_attach_args *)aux; |
| 95 | midisyn *ms; |
| 96 | |
| 97 | midi_pcppi_attached++; |
| 98 | |
| 99 | ms = &sc->sc_midisyn; |
| 100 | ms->mets = &midi_pcppi_hw; |
| 101 | strcpy(ms->name, "PC speaker" ); |
| 102 | ms->nvoice = 1; |
| 103 | ms->data = pa->pa_cookie; |
| 104 | ms->lock = &tty_lock; |
| 105 | midisyn_init(ms); |
| 106 | |
| 107 | sc->sc_mididev.dev = self; |
| 108 | sc->sc_mididev.hw_if = &midisyn_hw_if; |
| 109 | sc->sc_mididev.hw_hdl = ms; |
| 110 | midi_attach(&sc->sc_mididev); |
| 111 | } |
| 112 | |
| 113 | static int |
| 114 | midi_pcppi_detach(device_t self, int flags) |
| 115 | { |
| 116 | KASSERT(midi_pcppi_attached > 0); |
| 117 | |
| 118 | midi_pcppi_attached--; |
| 119 | return mididetach(self, flags); |
| 120 | } |
| 121 | |
| 122 | static void |
| 123 | midi_pcppi_on(midisyn *ms, |
| 124 | uint_fast16_t voice, midipitch_t mp, int16_t level) |
| 125 | { |
| 126 | pcppi_tag_t t = ms->data; |
| 127 | |
| 128 | KASSERT(mutex_owned(&tty_lock)); |
| 129 | |
| 130 | pcppi_bell_locked(t, MIDIHZ18_TO_HZ(MIDIPITCH_TO_HZ18(mp)), |
| 131 | MAX_DURATION * hz, 0); |
| 132 | } |
| 133 | |
| 134 | static void |
| 135 | midi_pcppi_off(midisyn *ms, uint_fast16_t voice, uint_fast8_t vel) |
| 136 | { |
| 137 | pcppi_tag_t t = ms->data; |
| 138 | |
| 139 | KASSERT(mutex_owned(&tty_lock)); |
| 140 | |
| 141 | /*printf("OFF %p %d\n", t, note >> 16);*/ |
| 142 | pcppi_bell_locked(t, 0, 0, 0); |
| 143 | } |
| 144 | |
| 145 | static void |
| 146 | midi_pcppi_close(midisyn *ms) |
| 147 | { |
| 148 | pcppi_tag_t t = ms->data; |
| 149 | |
| 150 | KASSERT(mutex_owned(&tty_lock)); |
| 151 | |
| 152 | /* Make sure we are quiet. */ |
| 153 | pcppi_bell_locked(t, 0, 0, 0); |
| 154 | } |
| 155 | |
| 156 | static void |
| 157 | midi_pcppi_repitchv(midisyn *ms, uint_fast16_t voice, midipitch_t newpitch) |
| 158 | { |
| 159 | KASSERT(mutex_owned(&tty_lock)); |
| 160 | |
| 161 | midi_pcppi_on(ms, voice, newpitch, 64); |
| 162 | } |
| 163 | |