FreeBSD kernel sound device code
csamidi.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3
4 * Copyright (c) 2015-2018 Tai-hwa Liang <avatar@FreeBSD.org>
5 * All rights reserved
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/param.h>
30#include <sys/types.h>
31#include <sys/bus.h>
32#include <machine/bus.h>
33#include <sys/rman.h>
34#include <sys/systm.h>
35#include <sys/kobj.h>
36#include <sys/lock.h>
37#include <sys/mutex.h>
38
39#ifdef HAVE_KERNEL_OPTION_HEADERS
40#include "opt_snd.h"
41#endif
42
43#include <dev/pci/pcireg.h>
44#include <dev/pci/pcivar.h>
45
46#include <dev/sound/chip.h>
47#include <dev/sound/pcm/sound.h>
48
49#include <dev/sound/midi/midi.h>
51
54
55#include "mpufoi_if.h"
56
57SND_DECLARE_FILE("$FreeBSD$");
58
59/* pulled from mpu401.c */
60#define MPU_DATAPORT 0
61#define MPU_CMDPORT 1
62#define MPU_STATPORT 1
63#define MPU_RESET 0xff
64#define MPU_UART 0x3f
65#define MPU_ACK 0xfe
66#define MPU_STATMASK 0xc0
67#define MPU_OUTPUTBUSY 0x40
68#define MPU_INPUTBUSY 0x80
69
70/* device private data */
72 /* hardware resources */
73 int io_rid; /* io rid */
74 struct resource *io; /* io */
75
76 struct mtx mtx;
77 device_t dev;
78 struct mpu401 *mpu;
80 int mflags; /* MIDI flags */
81};
82
83static struct kobj_class csamidi_mpu_class;
84static devclass_t midicsa_devclass;
85
86static u_int32_t
87csamidi_readio(struct csa_midi_softc *scp, u_long offset)
88{
90 return bus_space_read_4(rman_get_bustag(scp->io), rman_get_bushandle(scp->io), offset) & 0xffffffff;
91 else
92 return (0);
93}
94
95static void
96csamidi_writeio(struct csa_midi_softc *scp, u_long offset, u_int32_t data)
97{
99 bus_space_write_4(rman_get_bustag(scp->io), rman_get_bushandle(scp->io), offset, data);
100}
101
102static void
104{
105 struct csa_midi_softc *scp = (struct csa_midi_softc *)arg;
106
107 if (scp->mpu_intr)
108 (scp->mpu_intr)(scp->mpu);
109}
110
111static unsigned char
112csamidi_mread(struct mpu401 *arg __unused, void *cookie, int reg)
113{
114 struct csa_midi_softc *scp = cookie;
115 unsigned int rc;
116 unsigned int uart_stat;
117
118 rc = 0;
119 /* hacks to convert hardware status to MPU compatible ones */
120 switch (reg) {
121 case MPU_STATPORT:
122 uart_stat = csamidi_readio(scp, BA0_MIDSR);
123 if (uart_stat & MIDSR_TBF)
124 rc |= MPU_OUTPUTBUSY; /* Tx buffer full */
125 if (uart_stat & MIDSR_RBE)
126 rc |= MPU_INPUTBUSY;
127 break;
128 case MPU_DATAPORT:
129 rc = csamidi_readio(scp, BA0_MIDRP);
130 break;
131 default:
132 printf("csamidi_mread: unknown register %d\n", reg);
133 break;
134 }
135 return (rc);
136}
137
138static void
139csamidi_mwrite(struct mpu401 *arg __unused, void *cookie, int reg, unsigned char b)
140{
141 struct csa_midi_softc *scp = cookie;
142 unsigned int val;
143
144 switch (reg) {
145 case MPU_CMDPORT:
146 switch (b)
147 {
148 case MPU_RESET:
149 /* preserve current operation mode */
151 /* reset the MIDI port */
154 csamidi_writeio(scp, BA0_MIDCR, 0x00);
155 /* restore previous operation mode */
157 break;
158 case MPU_UART:
159 /* switch to UART mode, no-op */
160 default:
161 break;
162 }
163 break;
164 case MPU_DATAPORT:
165 /* put the MIDI databyte in the write port */
167 break;
168 default:
169 printf("csamidi_mwrite: unknown register %d\n", reg);
170 break;
171 }
172}
173
174static int
175csamidi_muninit(struct mpu401 *arg __unused, void *cookie)
176{
177 struct csa_midi_softc *scp = cookie;
178
179 mtx_lock(&scp->mtx);
180 scp->mpu_intr = NULL;
181 mtx_unlock(&scp->mtx);
182
183 return (0);
184}
185
186static int
188{
189 struct sndcard_func *func;
190
191 /* The parent device has already been probed. */
192
193 func = device_get_ivars(dev);
194 if (func == NULL || func->func != SCF_MIDI)
195 return (ENXIO);
196
197 device_set_desc(dev, "CS461x MIDI");
198 return (0);
199}
200
201static int
203{
204 struct csa_midi_softc *scp;
205 struct sndcard_func *func;
206 int rc = ENXIO;
207
208 scp = device_get_softc(dev);
209 func = device_get_ivars(dev);
210
211 bzero(scp, sizeof(struct csa_midi_softc));
212 scp->dev = dev;
213
214 /* allocate the required resources */
215 scp->io_rid = PCIR_BAR(0);
216 scp->io = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
217 &scp->io_rid, RF_ACTIVE);
218 if (scp->io == NULL)
219 goto err0;
220
221 /* init the fake MPU401 interface. */
223 &scp->mpu_intr);
224 if (scp->mpu == NULL) {
225 rc = ENOMEM;
226 goto err1;
227 }
228
229 mtx_init(&scp->mtx, device_get_nameunit(dev), "csamidi softc",
230 MTX_DEF);
231
232 /* reset the MIDI port */
234 /* MIDI transmit enable, no interrupt */
237
238 return (0);
239err1:
240 bus_release_resource(dev, SYS_RES_MEMORY, scp->io_rid, scp->io);
241 scp->io = NULL;
242err0:
243 return (rc);
244}
245
246static int
248{
249 struct csa_midi_softc *scp;
250 int rc = 0;
251
252 scp = device_get_softc(dev);
253 rc = mpu401_uninit(scp->mpu);
254 if (rc)
255 return (rc);
256 if (scp->io != NULL) {
257 bus_release_resource(dev, SYS_RES_MEMORY, scp->io_rid,
258 scp->io);
259 scp->io = NULL;
260 }
261 mtx_destroy(&scp->mtx);
262 return (rc);
263}
264
265static kobj_method_t csamidi_mpu_methods[] = {
266 KOBJMETHOD(mpufoi_read, csamidi_mread),
267 KOBJMETHOD(mpufoi_write, csamidi_mwrite),
268 KOBJMETHOD(mpufoi_uninit, csamidi_muninit),
270};
271
272static DEFINE_CLASS(csamidi_mpu, csamidi_mpu_methods, 0);
273
274static device_method_t midicsa_methods[] = {
275 DEVMETHOD(device_probe, midicsa_probe),
276 DEVMETHOD(device_attach, midicsa_attach),
277 DEVMETHOD(device_detach, midicsa_detach),
278
279 DEVMETHOD_END
280};
281
282static driver_t midicsa_driver = {
283 "midi",
285 sizeof(struct csa_midi_softc),
286};
288MODULE_DEPEND(snd_csa_midi, snd_csa, 1, 1, 1);
290MODULE_VERSION(snd_csa_midi, 1);
u_int32_t data
Definition: ac97_if.m:60
struct snd_dbuf * b
Definition: channel_if.m:105
@ SCF_MIDI
Definition: chip.h:37
static kobj_method_t csamidi_mpu_methods[]
Definition: csamidi.c:265
static devclass_t midicsa_devclass
Definition: csamidi.c:84
static void csamidi_midi_intr(void *arg)
Definition: csamidi.c:103
MODULE_VERSION(snd_csa_midi, 1)
static void csamidi_mwrite(struct mpu401 *arg __unused, void *cookie, int reg, unsigned char b)
Definition: csamidi.c:139
static u_int32_t csamidi_readio(struct csa_midi_softc *scp, u_long offset)
Definition: csamidi.c:87
static int midicsa_detach(device_t dev)
Definition: csamidi.c:247
#define MPU_UART
Definition: csamidi.c:64
static device_method_t midicsa_methods[]
Definition: csamidi.c:274
#define MPU_OUTPUTBUSY
Definition: csamidi.c:67
static int midicsa_probe(device_t dev)
Definition: csamidi.c:187
#define MPU_INPUTBUSY
Definition: csamidi.c:68
SND_DECLARE_FILE("$FreeBSD$")
#define MPU_STATPORT
Definition: csamidi.c:62
#define MPU_DATAPORT
Definition: csamidi.c:60
static DEFINE_CLASS(csamidi_mpu, csamidi_mpu_methods, 0)
static int midicsa_attach(device_t dev)
Definition: csamidi.c:202
static int csamidi_muninit(struct mpu401 *arg __unused, void *cookie)
Definition: csamidi.c:175
DRIVER_MODULE(snd_csa_midi, csa, midicsa_driver, midicsa_devclass, 0, 0)
static struct kobj_class csamidi_mpu_class
Definition: csamidi.c:83
static driver_t midicsa_driver
Definition: csamidi.c:282
static void csamidi_writeio(struct csa_midi_softc *scp, u_long offset, u_int32_t data)
Definition: csamidi.c:96
#define MPU_RESET
Definition: csamidi.c:63
#define MPU_CMDPORT
Definition: csamidi.c:61
static unsigned char csamidi_mread(struct mpu401 *arg __unused, void *cookie, int reg)
Definition: csamidi.c:112
MODULE_DEPEND(snd_csa_midi, snd_csa, 1, 1, 1)
#define MIDSR_TBF
Definition: csareg.h:948
#define BA0_AC97_RESET
Definition: csareg.h:179
#define MIDSR_RBE
Definition: csareg.h:949
#define MIDCR_MRST
Definition: csareg.h:941
#define BA0_MIDCR
Definition: csareg.h:112
#define BA0_MIDRP
Definition: csareg.h:115
#define MIDCR_MLB
Definition: csareg.h:940
#define HICR_IEV
Definition: csareg.h:363
#define MIDCR_TXE
Definition: csareg.h:936
#define BA0_HICR
Definition: csareg.h:51
#define MIDCR_RXE
Definition: csareg.h:937
#define HICR_CHGM
Definition: csareg.h:364
#define BA0_MIDSR
Definition: csareg.h:113
#define BA0_MIDWP
Definition: csareg.h:114
uint8_t reg
Definition: hdac.c:211
#define KOBJMETHOD_END
Definition: midi.c:76
unsigned dev
Definition: mixer_if.m:59
int mpu401_uninit(struct mpu401 *m)
Definition: mpu401.c:208
struct mpu401 * mpu401_init(kobj_class_t cls, void *cookie, driver_intr_t softintr, mpu401_intr_t **cb)
Definition: mpu401.c:177
int mpu401_intr_t(struct mpu401 *_obj)
Definition: mpu401.h:36
u_int32_t val
#define PCIR_BAR(x)
#define SOUND_PREFVER
Definition: sound.h:103
#define SOUND_MAXVER
Definition: sound.h:104
#define SOUND_MINVER
Definition: sound.h:102
struct mtx mtx
Definition: csamidi.c:76
struct resource * io
Definition: csamidi.c:74
struct mpu401 * mpu
Definition: csamidi.c:78
device_t dev
Definition: csamidi.c:77
mpu401_intr_t * mpu_intr
Definition: csamidi.c:79
Definition: mpu401.c:76
int func
Definition: chip.h:47
uint16_t offset