midi.c revision 1.29 1 1.29 thorpej /* $NetBSD: midi.c,v 1.29 2002/10/02 16:33:30 thorpej Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.8 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.8 augustss * by Lennart Augustsson (augustss (at) netbsd.org).
9 1.1 augustss *
10 1.1 augustss * Redistribution and use in source and binary forms, with or without
11 1.1 augustss * modification, are permitted provided that the following conditions
12 1.1 augustss * are met:
13 1.1 augustss * 1. Redistributions of source code must retain the above copyright
14 1.1 augustss * notice, this list of conditions and the following disclaimer.
15 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 augustss * notice, this list of conditions and the following disclaimer in the
17 1.1 augustss * documentation and/or other materials provided with the distribution.
18 1.1 augustss * 3. All advertising materials mentioning features or use of this software
19 1.1 augustss * must display the following acknowledgement:
20 1.1 augustss * This product includes software developed by the NetBSD
21 1.1 augustss * Foundation, Inc. and its contributors.
22 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 augustss * contributors may be used to endorse or promote products derived
24 1.1 augustss * from this software without specific prior written permission.
25 1.1 augustss *
26 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
37 1.1 augustss */
38 1.23 lukem
39 1.23 lukem #include <sys/cdefs.h>
40 1.29 thorpej __KERNEL_RCSID(0, "$NetBSD: midi.c,v 1.29 2002/10/02 16:33:30 thorpej Exp $");
41 1.1 augustss
42 1.1 augustss #include "midi.h"
43 1.1 augustss #include "sequencer.h"
44 1.1 augustss
45 1.1 augustss #include <sys/param.h>
46 1.1 augustss #include <sys/ioctl.h>
47 1.1 augustss #include <sys/fcntl.h>
48 1.1 augustss #include <sys/vnode.h>
49 1.1 augustss #include <sys/select.h>
50 1.1 augustss #include <sys/poll.h>
51 1.1 augustss #include <sys/malloc.h>
52 1.1 augustss #include <sys/proc.h>
53 1.1 augustss #include <sys/systm.h>
54 1.15 thorpej #include <sys/callout.h>
55 1.1 augustss #include <sys/syslog.h>
56 1.1 augustss #include <sys/kernel.h>
57 1.1 augustss #include <sys/signalvar.h>
58 1.1 augustss #include <sys/conf.h>
59 1.1 augustss #include <sys/audioio.h>
60 1.1 augustss #include <sys/midiio.h>
61 1.1 augustss #include <sys/device.h>
62 1.1 augustss
63 1.1 augustss #include <dev/audio_if.h>
64 1.4 augustss #include <dev/midi_if.h>
65 1.1 augustss #include <dev/midivar.h>
66 1.1 augustss
67 1.10 drochner #if NMIDI > 0
68 1.10 drochner
69 1.1 augustss #ifdef AUDIO_DEBUG
70 1.1 augustss #define DPRINTF(x) if (mididebug) printf x
71 1.1 augustss #define DPRINTFN(n,x) if (mididebug >= (n)) printf x
72 1.1 augustss int mididebug = 0;
73 1.1 augustss #else
74 1.1 augustss #define DPRINTF(x)
75 1.1 augustss #define DPRINTFN(n,x)
76 1.1 augustss #endif
77 1.1 augustss
78 1.1 augustss int midi_wait;
79 1.1 augustss
80 1.22 augustss void midi_in(void *, int);
81 1.22 augustss void midi_out(void *);
82 1.22 augustss int midi_start_output(struct midi_softc *, int);
83 1.22 augustss int midi_sleep_timo(int *, char *, int);
84 1.22 augustss int midi_sleep(int *, char *);
85 1.22 augustss void midi_wakeup(int *);
86 1.22 augustss void midi_initbuf(struct midi_buffer *);
87 1.22 augustss void midi_timeout(void *);
88 1.22 augustss
89 1.22 augustss int midiprobe(struct device *, struct cfdata *, void *);
90 1.22 augustss void midiattach(struct device *, struct device *, void *);
91 1.22 augustss int mididetach(struct device *, int);
92 1.22 augustss int midiactivate(struct device *, enum devact);
93 1.1 augustss
94 1.25 gehenna dev_type_open(midiopen);
95 1.25 gehenna dev_type_close(midiclose);
96 1.25 gehenna dev_type_read(midiread);
97 1.25 gehenna dev_type_write(midiwrite);
98 1.25 gehenna dev_type_ioctl(midiioctl);
99 1.25 gehenna dev_type_poll(midipoll);
100 1.25 gehenna
101 1.25 gehenna const struct cdevsw midi_cdevsw = {
102 1.25 gehenna midiopen, midiclose, midiread, midiwrite, midiioctl,
103 1.25 gehenna nostop, notty, midipoll, nommap,
104 1.25 gehenna };
105 1.25 gehenna
106 1.28 thorpej CFATTACH_DECL(midi, sizeof(struct midi_softc),
107 1.29 thorpej midiprobe, midiattach, mididetach, midiactivate);
108 1.1 augustss
109 1.4 augustss #ifdef MIDI_SAVE
110 1.4 augustss #define MIDI_SAVE_SIZE 100000
111 1.4 augustss int midicnt;
112 1.4 augustss struct {
113 1.4 augustss int cnt;
114 1.4 augustss u_char buf[MIDI_SAVE_SIZE];
115 1.4 augustss } midisave;
116 1.4 augustss #define MIDI_GETSAVE _IOWR('m', 100, int)
117 1.4 augustss
118 1.4 augustss #endif
119 1.4 augustss
120 1.1 augustss extern struct cfdriver midi_cd;
121 1.1 augustss
122 1.1 augustss int
123 1.22 augustss midiprobe(struct device *parent, struct cfdata *match, void *aux)
124 1.1 augustss {
125 1.1 augustss struct audio_attach_args *sa = aux;
126 1.1 augustss
127 1.1 augustss DPRINTFN(6,("midiprobe: type=%d sa=%p hw=%p\n",
128 1.1 augustss sa->type, sa, sa->hwif));
129 1.16 augustss return (sa->type == AUDIODEV_TYPE_MIDI);
130 1.1 augustss }
131 1.1 augustss
132 1.1 augustss void
133 1.22 augustss midiattach(struct device *parent, struct device *self, void *aux)
134 1.1 augustss {
135 1.1 augustss struct midi_softc *sc = (void *)self;
136 1.1 augustss struct audio_attach_args *sa = aux;
137 1.1 augustss struct midi_hw_if *hwp = sa->hwif;
138 1.1 augustss void *hdlp = sa->hdl;
139 1.1 augustss
140 1.1 augustss DPRINTFN(6, ("MIDI attach\n"));
141 1.1 augustss
142 1.1 augustss #ifdef DIAGNOSTIC
143 1.1 augustss if (hwp == 0 ||
144 1.1 augustss hwp->open == 0 ||
145 1.1 augustss hwp->close == 0 ||
146 1.1 augustss hwp->output == 0 ||
147 1.1 augustss hwp->getinfo == 0) {
148 1.1 augustss printf("midi: missing method\n");
149 1.1 augustss return;
150 1.1 augustss }
151 1.1 augustss #endif
152 1.15 thorpej
153 1.15 thorpej callout_init(&sc->sc_callout);
154 1.15 thorpej
155 1.1 augustss sc->hw_if = hwp;
156 1.1 augustss sc->hw_hdl = hdlp;
157 1.19 tshiozak sc->dying = 0;
158 1.2 augustss midi_attach(sc, parent);
159 1.2 augustss }
160 1.2 augustss
161 1.18 tshiozak int
162 1.22 augustss midiactivate(struct device *self, enum devact act)
163 1.18 tshiozak {
164 1.18 tshiozak struct midi_softc *sc = (struct midi_softc *)self;
165 1.18 tshiozak
166 1.18 tshiozak switch (act) {
167 1.18 tshiozak case DVACT_ACTIVATE:
168 1.18 tshiozak return (EOPNOTSUPP);
169 1.18 tshiozak
170 1.18 tshiozak case DVACT_DEACTIVATE:
171 1.18 tshiozak sc->dying = 1;
172 1.18 tshiozak break;
173 1.18 tshiozak }
174 1.18 tshiozak return (0);
175 1.18 tshiozak }
176 1.18 tshiozak
177 1.18 tshiozak int
178 1.22 augustss mididetach(struct device *self, int flags)
179 1.18 tshiozak {
180 1.18 tshiozak struct midi_softc *sc = (struct midi_softc *)self;
181 1.18 tshiozak int maj, mn;
182 1.18 tshiozak
183 1.18 tshiozak DPRINTF(("midi_detach: sc=%p flags=%d\n", sc, flags));
184 1.18 tshiozak
185 1.18 tshiozak sc->dying = 1;
186 1.18 tshiozak
187 1.18 tshiozak wakeup(&sc->wchan);
188 1.18 tshiozak wakeup(&sc->rchan);
189 1.18 tshiozak
190 1.18 tshiozak /* locate the major number */
191 1.25 gehenna maj = cdevsw_lookup_major(&midi_cdevsw);
192 1.18 tshiozak
193 1.18 tshiozak /* Nuke the vnodes for any open instances (calls close). */
194 1.18 tshiozak mn = self->dv_unit;
195 1.18 tshiozak vdevgone(maj, mn, mn, VCHR);
196 1.18 tshiozak
197 1.18 tshiozak return (0);
198 1.18 tshiozak }
199 1.18 tshiozak
200 1.2 augustss void
201 1.22 augustss midi_attach(struct midi_softc *sc, struct device *parent)
202 1.2 augustss {
203 1.2 augustss struct midi_info mi;
204 1.2 augustss
205 1.1 augustss sc->isopen = 0;
206 1.1 augustss
207 1.1 augustss midi_wait = MIDI_WAIT * hz / 1000000;
208 1.1 augustss if (midi_wait == 0)
209 1.1 augustss midi_wait = 1;
210 1.1 augustss
211 1.2 augustss sc->sc_dev = parent;
212 1.2 augustss sc->hw_if->getinfo(sc->hw_hdl, &mi);
213 1.1 augustss sc->props = mi.props;
214 1.13 soren printf(": %s\n", mi.name);
215 1.1 augustss }
216 1.1 augustss
217 1.1 augustss int
218 1.22 augustss midi_unit_count(void)
219 1.1 augustss {
220 1.1 augustss return midi_cd.cd_ndevs;
221 1.1 augustss }
222 1.1 augustss
223 1.1 augustss void
224 1.22 augustss midi_initbuf(struct midi_buffer *mb)
225 1.1 augustss {
226 1.1 augustss mb->used = 0;
227 1.1 augustss mb->usedhigh = MIDI_BUFSIZE;
228 1.1 augustss mb->end = mb->start + mb->usedhigh;
229 1.1 augustss mb->inp = mb->outp = mb->start;
230 1.1 augustss }
231 1.1 augustss
232 1.2 augustss int
233 1.22 augustss midi_sleep_timo(int *chan, char *label, int timo)
234 1.1 augustss {
235 1.1 augustss int st;
236 1.1 augustss
237 1.1 augustss if (!label)
238 1.1 augustss label = "midi";
239 1.1 augustss
240 1.1 augustss DPRINTFN(5, ("midi_sleep_timo: %p %s %d\n", chan, label, timo));
241 1.1 augustss *chan = 1;
242 1.1 augustss st = tsleep(chan, PWAIT | PCATCH, label, timo);
243 1.1 augustss *chan = 0;
244 1.1 augustss #ifdef MIDI_DEBUG
245 1.1 augustss if (st != 0)
246 1.1 augustss printf("midi_sleep: %d\n", st);
247 1.1 augustss #endif
248 1.1 augustss return st;
249 1.1 augustss }
250 1.1 augustss
251 1.2 augustss int
252 1.22 augustss midi_sleep(int *chan, char *label)
253 1.1 augustss {
254 1.1 augustss return midi_sleep_timo(chan, label, 0);
255 1.1 augustss }
256 1.1 augustss
257 1.2 augustss void
258 1.22 augustss midi_wakeup(int *chan)
259 1.1 augustss {
260 1.1 augustss if (*chan) {
261 1.1 augustss DPRINTFN(5, ("midi_wakeup: %p\n", chan));
262 1.1 augustss wakeup(chan);
263 1.1 augustss *chan = 0;
264 1.1 augustss }
265 1.1 augustss }
266 1.1 augustss
267 1.1 augustss static int midi_lengths[] = { 2,2,2,2,1,1,2,0 };
268 1.1 augustss /* Number of bytes in a MIDI command */
269 1.1 augustss #define MIDI_LENGTH(d) (midi_lengths[((d) >> 4) & 7])
270 1.1 augustss
271 1.1 augustss void
272 1.22 augustss midi_in(void *addr, int data)
273 1.1 augustss {
274 1.1 augustss struct midi_softc *sc = addr;
275 1.1 augustss struct midi_buffer *mb = &sc->inbuf;
276 1.1 augustss int i;
277 1.1 augustss
278 1.3 augustss if (!sc->isopen)
279 1.3 augustss return;
280 1.1 augustss if (data == MIDI_ACK)
281 1.1 augustss return;
282 1.14 augustss
283 1.14 augustss DPRINTFN(3, ("midi_in: sc=%p data=0x%02x state=%d pos=%d\n",
284 1.14 augustss sc, data, sc->in_state, sc->in_pos));
285 1.14 augustss
286 1.1 augustss if (!(sc->flags & FREAD))
287 1.1 augustss return; /* discard data if not reading */
288 1.1 augustss
289 1.1 augustss switch(sc->in_state) {
290 1.1 augustss case MIDI_IN_START:
291 1.1 augustss if (MIDI_IS_STATUS(data)) {
292 1.1 augustss switch(data) {
293 1.1 augustss case 0xf0: /* Sysex */
294 1.1 augustss sc->in_state = MIDI_IN_SYSEX;
295 1.1 augustss break;
296 1.1 augustss case 0xf1: /* MTC quarter frame */
297 1.1 augustss case 0xf3: /* Song select */
298 1.1 augustss sc->in_state = MIDI_IN_DATA;
299 1.1 augustss sc->in_msg[0] = data;
300 1.1 augustss sc->in_pos = 1;
301 1.1 augustss sc->in_left = 1;
302 1.1 augustss break;
303 1.1 augustss case 0xf2: /* Song position pointer */
304 1.1 augustss sc->in_state = MIDI_IN_DATA;
305 1.1 augustss sc->in_msg[0] = data;
306 1.1 augustss sc->in_pos = 1;
307 1.1 augustss sc->in_left = 2;
308 1.1 augustss break;
309 1.1 augustss default:
310 1.1 augustss if (MIDI_IS_COMMON(data)) {
311 1.1 augustss sc->in_msg[0] = data;
312 1.1 augustss sc->in_pos = 1;
313 1.1 augustss goto deliver;
314 1.1 augustss } else {
315 1.1 augustss sc->in_state = MIDI_IN_DATA;
316 1.1 augustss sc->in_msg[0] = sc->in_status = data;
317 1.1 augustss sc->in_pos = 1;
318 1.14 augustss sc->in_left = MIDI_LENGTH(data);
319 1.1 augustss }
320 1.1 augustss break;
321 1.1 augustss }
322 1.1 augustss } else {
323 1.1 augustss if (MIDI_IS_STATUS(sc->in_status)) {
324 1.1 augustss sc->in_state = MIDI_IN_DATA;
325 1.1 augustss sc->in_msg[0] = sc->in_status;
326 1.1 augustss sc->in_msg[1] = data;
327 1.1 augustss sc->in_pos = 2;
328 1.1 augustss sc->in_left = MIDI_LENGTH(sc->in_status) - 1;
329 1.1 augustss }
330 1.1 augustss }
331 1.1 augustss return;
332 1.1 augustss case MIDI_IN_DATA:
333 1.1 augustss sc->in_msg[sc->in_pos++] = data;
334 1.1 augustss if (--sc->in_left <= 0)
335 1.1 augustss break; /* deliver data */
336 1.1 augustss return;
337 1.1 augustss case MIDI_IN_SYSEX:
338 1.1 augustss if (data == MIDI_SYSEX_END)
339 1.1 augustss sc->in_state = MIDI_IN_START;
340 1.1 augustss return;
341 1.1 augustss }
342 1.1 augustss deliver:
343 1.1 augustss sc->in_state = MIDI_IN_START;
344 1.1 augustss #if NSEQUENCER > 0
345 1.1 augustss if (sc->seqopen) {
346 1.22 augustss extern void midiseq_in(struct midi_dev *,u_char *,int);
347 1.7 augustss midiseq_in(sc->seq_md, sc->in_msg, sc->in_pos);
348 1.1 augustss return;
349 1.1 augustss }
350 1.1 augustss #endif
351 1.1 augustss
352 1.1 augustss if (mb->used + sc->in_pos > mb->usedhigh) {
353 1.1 augustss DPRINTF(("midi_in: buffer full, discard data=0x%02x\n",
354 1.1 augustss sc->in_msg[0]));
355 1.1 augustss return;
356 1.1 augustss }
357 1.1 augustss for (i = 0; i < sc->in_pos; i++) {
358 1.1 augustss *mb->inp++ = sc->in_msg[i];
359 1.1 augustss if (mb->inp >= mb->end)
360 1.1 augustss mb->inp = mb->start;
361 1.1 augustss mb->used++;
362 1.1 augustss }
363 1.1 augustss midi_wakeup(&sc->rchan);
364 1.1 augustss selwakeup(&sc->rsel);
365 1.1 augustss if (sc->async)
366 1.1 augustss psignal(sc->async, SIGIO);
367 1.1 augustss }
368 1.1 augustss
369 1.1 augustss void
370 1.22 augustss midi_out(void *addr)
371 1.1 augustss {
372 1.1 augustss struct midi_softc *sc = addr;
373 1.3 augustss
374 1.3 augustss if (!sc->isopen)
375 1.3 augustss return;
376 1.1 augustss DPRINTFN(3, ("midi_out: %p\n", sc));
377 1.1 augustss midi_start_output(sc, 1);
378 1.1 augustss }
379 1.1 augustss
380 1.1 augustss int
381 1.22 augustss midiopen(dev_t dev, int flags, int ifmt, struct proc *p)
382 1.1 augustss {
383 1.1 augustss struct midi_softc *sc;
384 1.1 augustss struct midi_hw_if *hw;
385 1.1 augustss int error;
386 1.1 augustss
387 1.17 thorpej sc = device_lookup(&midi_cd, MIDIUNIT(dev));
388 1.17 thorpej if (sc == NULL)
389 1.17 thorpej return (ENXIO);
390 1.18 tshiozak if (sc->dying)
391 1.18 tshiozak return (EIO);
392 1.17 thorpej
393 1.1 augustss DPRINTF(("midiopen %p\n", sc));
394 1.1 augustss
395 1.1 augustss hw = sc->hw_if;
396 1.1 augustss if (!hw)
397 1.1 augustss return ENXIO;
398 1.1 augustss if (sc->isopen)
399 1.1 augustss return EBUSY;
400 1.1 augustss sc->in_state = MIDI_IN_START;
401 1.1 augustss sc->in_status = 0;
402 1.1 augustss error = hw->open(sc->hw_hdl, flags, midi_in, midi_out, sc);
403 1.1 augustss if (error)
404 1.1 augustss return error;
405 1.1 augustss sc->isopen++;
406 1.6 mycroft midi_initbuf(&sc->outbuf);
407 1.6 mycroft midi_initbuf(&sc->inbuf);
408 1.1 augustss sc->flags = flags;
409 1.1 augustss sc->rchan = 0;
410 1.1 augustss sc->wchan = 0;
411 1.1 augustss sc->pbus = 0;
412 1.1 augustss sc->async = 0;
413 1.1 augustss
414 1.4 augustss #ifdef MIDI_SAVE
415 1.4 augustss if (midicnt != 0) {
416 1.4 augustss midisave.cnt = midicnt;
417 1.4 augustss midicnt = 0;
418 1.4 augustss }
419 1.4 augustss #endif
420 1.4 augustss
421 1.1 augustss return 0;
422 1.1 augustss }
423 1.1 augustss
424 1.1 augustss int
425 1.22 augustss midiclose(dev_t dev, int flags, int ifmt, struct proc *p)
426 1.1 augustss {
427 1.1 augustss int unit = MIDIUNIT(dev);
428 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
429 1.1 augustss struct midi_hw_if *hw = sc->hw_if;
430 1.1 augustss int s, error;
431 1.1 augustss
432 1.1 augustss DPRINTF(("midiclose %p\n", sc));
433 1.1 augustss
434 1.1 augustss midi_start_output(sc, 0);
435 1.1 augustss error = 0;
436 1.1 augustss s = splaudio();
437 1.1 augustss while (sc->outbuf.used > 0 && !error) {
438 1.1 augustss DPRINTFN(2,("midiclose sleep used=%d\n", sc->outbuf.used));
439 1.3 augustss error = midi_sleep_timo(&sc->wchan, "mid_dr", 30*hz);
440 1.1 augustss }
441 1.6 mycroft splx(s);
442 1.3 augustss sc->isopen = 0;
443 1.1 augustss hw->close(sc->hw_hdl);
444 1.1 augustss #if NSEQUENCER > 0
445 1.1 augustss sc->seqopen = 0;
446 1.7 augustss sc->seq_md = 0;
447 1.1 augustss #endif
448 1.1 augustss return 0;
449 1.1 augustss }
450 1.1 augustss
451 1.1 augustss int
452 1.22 augustss midiread(dev_t dev, struct uio *uio, int ioflag)
453 1.1 augustss {
454 1.1 augustss int unit = MIDIUNIT(dev);
455 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
456 1.1 augustss struct midi_buffer *mb = &sc->inbuf;
457 1.1 augustss int error;
458 1.1 augustss u_char *outp;
459 1.1 augustss int used, cc, n, resid;
460 1.1 augustss int s;
461 1.1 augustss
462 1.11 nathanw DPRINTF(("midiread: %p, count=%lu\n", sc,
463 1.11 nathanw (unsigned long)uio->uio_resid));
464 1.1 augustss
465 1.18 tshiozak if (sc->dying)
466 1.18 tshiozak return EIO;
467 1.18 tshiozak
468 1.1 augustss error = 0;
469 1.1 augustss resid = uio->uio_resid;
470 1.1 augustss while (uio->uio_resid == resid && !error) {
471 1.1 augustss s = splaudio();
472 1.1 augustss while (mb->used <= 0) {
473 1.1 augustss if (ioflag & IO_NDELAY) {
474 1.1 augustss splx(s);
475 1.1 augustss return EWOULDBLOCK;
476 1.1 augustss }
477 1.1 augustss error = midi_sleep(&sc->rchan, "mid rd");
478 1.1 augustss if (error) {
479 1.1 augustss splx(s);
480 1.1 augustss return error;
481 1.1 augustss }
482 1.1 augustss }
483 1.1 augustss used = mb->used;
484 1.1 augustss outp = mb->outp;
485 1.1 augustss splx(s);
486 1.18 tshiozak if (sc->dying)
487 1.18 tshiozak return EIO;
488 1.1 augustss cc = used; /* maximum to read */
489 1.1 augustss n = mb->end - outp;
490 1.1 augustss if (n < cc)
491 1.1 augustss cc = n; /* don't read beyond end of buffer */
492 1.1 augustss if (uio->uio_resid < cc)
493 1.1 augustss cc = uio->uio_resid; /* and no more than we want */
494 1.1 augustss DPRINTFN(3, ("midiread: uiomove cc=%d\n", cc));
495 1.1 augustss error = uiomove(outp, cc, uio);
496 1.1 augustss if (error)
497 1.1 augustss break;
498 1.1 augustss used -= cc;
499 1.1 augustss outp += cc;
500 1.1 augustss if (outp >= mb->end)
501 1.1 augustss outp = mb->start;
502 1.1 augustss s = splaudio();
503 1.1 augustss mb->outp = outp;
504 1.1 augustss mb->used = used;
505 1.1 augustss splx(s);
506 1.1 augustss }
507 1.1 augustss return error;
508 1.1 augustss }
509 1.1 augustss
510 1.1 augustss void
511 1.22 augustss midi_timeout(void *arg)
512 1.1 augustss {
513 1.1 augustss struct midi_softc *sc = arg;
514 1.1 augustss
515 1.1 augustss DPRINTFN(3,("midi_timeout: %p\n", sc));
516 1.1 augustss midi_start_output(sc, 1);
517 1.1 augustss }
518 1.1 augustss
519 1.1 augustss int
520 1.22 augustss midi_start_output(struct midi_softc *sc, int intr)
521 1.1 augustss {
522 1.1 augustss struct midi_buffer *mb = &sc->outbuf;
523 1.21 tshiozak u_char out;
524 1.1 augustss int error;
525 1.1 augustss int s;
526 1.20 tshiozak int i;
527 1.1 augustss
528 1.1 augustss error = 0;
529 1.18 tshiozak
530 1.18 tshiozak if (sc->dying)
531 1.18 tshiozak return EIO;
532 1.18 tshiozak
533 1.1 augustss if (sc->pbus && !intr) {
534 1.1 augustss DPRINTFN(4, ("midi_start_output: busy\n"));
535 1.1 augustss return 0;
536 1.1 augustss }
537 1.20 tshiozak sc->pbus = (mb->used > 0)?1:0;
538 1.20 tshiozak for (i = 0; i < MIDI_MAX_WRITE && mb->used > 0 &&
539 1.20 tshiozak (!error || error==EINPROGRESS); i++) {
540 1.20 tshiozak s = splaudio();
541 1.21 tshiozak out = *mb->outp;
542 1.21 tshiozak mb->outp++;
543 1.21 tshiozak if (mb->outp >= mb->end)
544 1.21 tshiozak mb->outp = mb->start;
545 1.21 tshiozak mb->used--;
546 1.21 tshiozak splx(s);
547 1.4 augustss #ifdef MIDI_SAVE
548 1.21 tshiozak midisave.buf[midicnt] = out;
549 1.4 augustss midicnt = (midicnt + 1) % MIDI_SAVE_SIZE;
550 1.4 augustss #endif
551 1.21 tshiozak DPRINTFN(4, ("midi_start_output: %p i=%d, data=0x%02x\n",
552 1.21 tshiozak sc, i, out));
553 1.21 tshiozak error = sc->hw_if->output(sc->hw_hdl, out);
554 1.20 tshiozak if ((sc->props & MIDI_PROP_OUT_INTR) && error!=EINPROGRESS)
555 1.20 tshiozak /* If ointr is enabled, midi_start_output()
556 1.20 tshiozak * normally writes only one byte,
557 1.20 tshiozak * except hw_if->output() returns EINPROGRESS.
558 1.20 tshiozak */
559 1.20 tshiozak break;
560 1.1 augustss }
561 1.1 augustss midi_wakeup(&sc->wchan);
562 1.1 augustss selwakeup(&sc->wsel);
563 1.1 augustss if (sc->async)
564 1.1 augustss psignal(sc->async, SIGIO);
565 1.20 tshiozak if (!(sc->props & MIDI_PROP_OUT_INTR) || error==EINPROGRESS) {
566 1.20 tshiozak if (mb->used > 0)
567 1.15 thorpej callout_reset(&sc->sc_callout, midi_wait,
568 1.20 tshiozak midi_timeout, sc);
569 1.20 tshiozak else
570 1.20 tshiozak sc->pbus = 0;
571 1.20 tshiozak }
572 1.20 tshiozak if ((sc->props & MIDI_PROP_OUT_INTR) && error==EINPROGRESS)
573 1.20 tshiozak error = 0;
574 1.20 tshiozak
575 1.1 augustss return error;
576 1.1 augustss }
577 1.1 augustss
578 1.1 augustss int
579 1.22 augustss midiwrite(dev_t dev, struct uio *uio, int ioflag)
580 1.1 augustss {
581 1.1 augustss int unit = MIDIUNIT(dev);
582 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
583 1.1 augustss struct midi_buffer *mb = &sc->outbuf;
584 1.1 augustss int error;
585 1.1 augustss u_char *inp;
586 1.1 augustss int used, cc, n;
587 1.1 augustss int s;
588 1.1 augustss
589 1.11 nathanw DPRINTFN(2, ("midiwrite: %p, unit=%d, count=%lu\n", sc, unit,
590 1.11 nathanw (unsigned long)uio->uio_resid));
591 1.1 augustss
592 1.18 tshiozak if (sc->dying)
593 1.18 tshiozak return EIO;
594 1.18 tshiozak
595 1.1 augustss error = 0;
596 1.1 augustss while (uio->uio_resid > 0 && !error) {
597 1.1 augustss s = splaudio();
598 1.1 augustss if (mb->used >= mb->usedhigh) {
599 1.1 augustss DPRINTFN(3,("midi_write: sleep used=%d hiwat=%d\n",
600 1.1 augustss mb->used, mb->usedhigh));
601 1.1 augustss if (ioflag & IO_NDELAY) {
602 1.1 augustss splx(s);
603 1.1 augustss return EWOULDBLOCK;
604 1.1 augustss }
605 1.1 augustss error = midi_sleep(&sc->wchan, "mid wr");
606 1.1 augustss if (error) {
607 1.1 augustss splx(s);
608 1.1 augustss return error;
609 1.1 augustss }
610 1.1 augustss }
611 1.1 augustss used = mb->used;
612 1.1 augustss inp = mb->inp;
613 1.1 augustss splx(s);
614 1.18 tshiozak if (sc->dying)
615 1.18 tshiozak return EIO;
616 1.1 augustss cc = mb->usedhigh - used; /* maximum to write */
617 1.1 augustss n = mb->end - inp;
618 1.1 augustss if (n < cc)
619 1.9 augustss cc = n; /* don't write beyond end of buffer */
620 1.1 augustss if (uio->uio_resid < cc)
621 1.1 augustss cc = uio->uio_resid; /* and no more than we have */
622 1.1 augustss error = uiomove(inp, cc, uio);
623 1.1 augustss #ifdef MIDI_DEBUG
624 1.1 augustss if (error)
625 1.9 augustss printf("midi_write:(1) uiomove failed %d; "
626 1.9 augustss "cc=%d inp=%p\n",
627 1.1 augustss error, cc, inp);
628 1.1 augustss #endif
629 1.1 augustss if (error)
630 1.1 augustss break;
631 1.1 augustss inp = mb->inp + cc;
632 1.1 augustss if (inp >= mb->end)
633 1.1 augustss inp = mb->start;
634 1.1 augustss s = splaudio();
635 1.1 augustss mb->inp = inp;
636 1.1 augustss mb->used += cc;
637 1.1 augustss splx(s);
638 1.1 augustss error = midi_start_output(sc, 0);
639 1.1 augustss }
640 1.1 augustss return error;
641 1.5 augustss }
642 1.5 augustss
643 1.5 augustss /*
644 1.9 augustss * This write routine is only called from sequencer code and expects
645 1.5 augustss * a write that is smaller than the MIDI buffer.
646 1.5 augustss */
647 1.5 augustss int
648 1.22 augustss midi_writebytes(int unit, u_char *buf, int cc)
649 1.5 augustss {
650 1.5 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
651 1.5 augustss struct midi_buffer *mb = &sc->outbuf;
652 1.5 augustss int n, s;
653 1.5 augustss
654 1.5 augustss DPRINTFN(2, ("midi_writebytes: %p, unit=%d, cc=%d\n", sc, unit, cc));
655 1.9 augustss DPRINTFN(3, ("midi_writebytes: %x %x %x\n",buf[0],buf[1],buf[2]));
656 1.5 augustss
657 1.18 tshiozak if (sc->dying)
658 1.18 tshiozak return EIO;
659 1.18 tshiozak
660 1.5 augustss s = splaudio();
661 1.5 augustss if (mb->used + cc >= mb->usedhigh) {
662 1.5 augustss splx(s);
663 1.5 augustss return (EWOULDBLOCK);
664 1.5 augustss }
665 1.5 augustss n = mb->end - mb->inp;
666 1.5 augustss if (cc < n)
667 1.5 augustss n = cc;
668 1.9 augustss mb->used += cc;
669 1.9 augustss memcpy(mb->inp, buf, n);
670 1.5 augustss mb->inp += n;
671 1.5 augustss if (mb->inp >= mb->end) {
672 1.5 augustss mb->inp = mb->start;
673 1.5 augustss cc -= n;
674 1.5 augustss if (cc > 0) {
675 1.5 augustss memcpy(mb->inp, buf + n, cc);
676 1.5 augustss mb->inp += cc;
677 1.5 augustss }
678 1.5 augustss }
679 1.5 augustss splx(s);
680 1.5 augustss return (midi_start_output(sc, 0));
681 1.1 augustss }
682 1.1 augustss
683 1.1 augustss int
684 1.22 augustss midiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
685 1.1 augustss {
686 1.1 augustss int unit = MIDIUNIT(dev);
687 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
688 1.1 augustss struct midi_hw_if *hw = sc->hw_if;
689 1.1 augustss int error;
690 1.1 augustss
691 1.1 augustss DPRINTF(("midiioctl: %p cmd=0x%08lx\n", sc, cmd));
692 1.18 tshiozak
693 1.18 tshiozak if (sc->dying)
694 1.18 tshiozak return EIO;
695 1.18 tshiozak
696 1.1 augustss error = 0;
697 1.1 augustss switch (cmd) {
698 1.1 augustss case FIONBIO:
699 1.1 augustss /* All handled in the upper FS layer. */
700 1.1 augustss break;
701 1.1 augustss
702 1.1 augustss case FIOASYNC:
703 1.1 augustss if (*(int *)addr) {
704 1.1 augustss if (sc->async)
705 1.1 augustss return EBUSY;
706 1.1 augustss sc->async = p;
707 1.1 augustss DPRINTF(("midi_ioctl: FIOASYNC %p\n", p));
708 1.1 augustss } else
709 1.1 augustss sc->async = 0;
710 1.1 augustss break;
711 1.1 augustss
712 1.1 augustss #if 0
713 1.1 augustss case MIDI_PRETIME:
714 1.1 augustss /* XXX OSS
715 1.1 augustss * This should set up a read timeout, but that's
716 1.1 augustss * why we have poll(), so there's nothing yet. */
717 1.1 augustss error = EINVAL;
718 1.1 augustss break;
719 1.1 augustss #endif
720 1.1 augustss
721 1.4 augustss #ifdef MIDI_SAVE
722 1.4 augustss case MIDI_GETSAVE:
723 1.4 augustss error = copyout(&midisave, *(void **)addr, sizeof midisave);
724 1.4 augustss break;
725 1.4 augustss #endif
726 1.4 augustss
727 1.1 augustss default:
728 1.1 augustss if (hw->ioctl)
729 1.2 augustss error = hw->ioctl(sc->hw_hdl, cmd, addr, flag, p);
730 1.1 augustss else
731 1.1 augustss error = EINVAL;
732 1.1 augustss break;
733 1.1 augustss }
734 1.1 augustss return error;
735 1.1 augustss }
736 1.1 augustss
737 1.1 augustss int
738 1.22 augustss midipoll(dev_t dev, int events, struct proc *p)
739 1.1 augustss {
740 1.1 augustss int unit = MIDIUNIT(dev);
741 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
742 1.1 augustss int revents = 0;
743 1.24 gson int s;
744 1.1 augustss
745 1.1 augustss DPRINTF(("midipoll: %p events=0x%x\n", sc, events));
746 1.1 augustss
747 1.18 tshiozak if (sc->dying)
748 1.18 tshiozak return EIO;
749 1.24 gson
750 1.24 gson s = splaudio();
751 1.18 tshiozak
752 1.1 augustss if (events & (POLLIN | POLLRDNORM))
753 1.1 augustss if (sc->inbuf.used > 0)
754 1.1 augustss revents |= events & (POLLIN | POLLRDNORM);
755 1.1 augustss
756 1.1 augustss if (events & (POLLOUT | POLLWRNORM))
757 1.1 augustss if (sc->outbuf.used < sc->outbuf.usedhigh)
758 1.1 augustss revents |= events & (POLLOUT | POLLWRNORM);
759 1.1 augustss
760 1.1 augustss if (revents == 0) {
761 1.1 augustss if (events & (POLLIN | POLLRDNORM))
762 1.1 augustss selrecord(p, &sc->rsel);
763 1.1 augustss
764 1.1 augustss if (events & (POLLOUT | POLLWRNORM))
765 1.1 augustss selrecord(p, &sc->wsel);
766 1.1 augustss }
767 1.1 augustss
768 1.1 augustss splx(s);
769 1.1 augustss return revents;
770 1.1 augustss }
771 1.1 augustss
772 1.1 augustss void
773 1.22 augustss midi_getinfo(dev_t dev, struct midi_info *mi)
774 1.1 augustss {
775 1.1 augustss struct midi_softc *sc;
776 1.1 augustss
777 1.17 thorpej sc = device_lookup(&midi_cd, MIDIUNIT(dev));
778 1.17 thorpej if (sc == NULL)
779 1.18 tshiozak return;
780 1.18 tshiozak if (sc->dying)
781 1.1 augustss return;
782 1.17 thorpej
783 1.1 augustss sc->hw_if->getinfo(sc->hw_hdl, mi);
784 1.4 augustss }
785 1.4 augustss
786 1.10 drochner #endif /* NMIDI > 0 */
787 1.10 drochner
788 1.10 drochner #if NMIDI > 0 || NMIDIBUS > 0
789 1.10 drochner
790 1.22 augustss int audioprint(void *, const char *);
791 1.4 augustss
792 1.12 augustss struct device *
793 1.22 augustss midi_attach_mi(struct midi_hw_if *mhwp, void *hdlp, struct device *dev)
794 1.4 augustss {
795 1.4 augustss struct audio_attach_args arg;
796 1.4 augustss
797 1.4 augustss #ifdef DIAGNOSTIC
798 1.4 augustss if (mhwp == NULL) {
799 1.4 augustss printf("midi_attach_mi: NULL\n");
800 1.12 augustss return (0);
801 1.4 augustss }
802 1.4 augustss #endif
803 1.4 augustss arg.type = AUDIODEV_TYPE_MIDI;
804 1.4 augustss arg.hwif = mhwp;
805 1.4 augustss arg.hdl = hdlp;
806 1.12 augustss return (config_found(dev, &arg, audioprint));
807 1.1 augustss }
808 1.1 augustss
809 1.10 drochner #endif /* NMIDI > 0 || NMIDIBUS > 0 */
810