midi.c revision 1.38 1 1.38 yamt /* $NetBSD: midi.c,v 1.38 2004/10/29 12:57:16 yamt 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.37 keihan * 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.38 yamt __KERNEL_RCSID(0, "$NetBSD: midi.c,v 1.38 2004/10/29 12:57:16 yamt 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.30 jdolecek dev_type_kqfilter(midikqfilter);
101 1.25 gehenna
102 1.25 gehenna const struct cdevsw midi_cdevsw = {
103 1.25 gehenna midiopen, midiclose, midiread, midiwrite, midiioctl,
104 1.30 jdolecek nostop, notty, midipoll, nommap, midikqfilter,
105 1.25 gehenna };
106 1.25 gehenna
107 1.28 thorpej CFATTACH_DECL(midi, sizeof(struct midi_softc),
108 1.29 thorpej midiprobe, midiattach, mididetach, midiactivate);
109 1.1 augustss
110 1.4 augustss #ifdef MIDI_SAVE
111 1.4 augustss #define MIDI_SAVE_SIZE 100000
112 1.4 augustss int midicnt;
113 1.4 augustss struct {
114 1.4 augustss int cnt;
115 1.4 augustss u_char buf[MIDI_SAVE_SIZE];
116 1.4 augustss } midisave;
117 1.4 augustss #define MIDI_GETSAVE _IOWR('m', 100, int)
118 1.4 augustss
119 1.4 augustss #endif
120 1.4 augustss
121 1.1 augustss extern struct cfdriver midi_cd;
122 1.1 augustss
123 1.1 augustss int
124 1.22 augustss midiprobe(struct device *parent, struct cfdata *match, void *aux)
125 1.1 augustss {
126 1.1 augustss struct audio_attach_args *sa = aux;
127 1.1 augustss
128 1.1 augustss DPRINTFN(6,("midiprobe: type=%d sa=%p hw=%p\n",
129 1.1 augustss sa->type, sa, sa->hwif));
130 1.16 augustss return (sa->type == AUDIODEV_TYPE_MIDI);
131 1.1 augustss }
132 1.1 augustss
133 1.1 augustss void
134 1.22 augustss midiattach(struct device *parent, struct device *self, void *aux)
135 1.1 augustss {
136 1.1 augustss struct midi_softc *sc = (void *)self;
137 1.1 augustss struct audio_attach_args *sa = aux;
138 1.38 yamt const struct midi_hw_if *hwp = sa->hwif;
139 1.1 augustss void *hdlp = sa->hdl;
140 1.1 augustss
141 1.1 augustss DPRINTFN(6, ("MIDI attach\n"));
142 1.1 augustss
143 1.1 augustss #ifdef DIAGNOSTIC
144 1.1 augustss if (hwp == 0 ||
145 1.1 augustss hwp->open == 0 ||
146 1.1 augustss hwp->close == 0 ||
147 1.1 augustss hwp->output == 0 ||
148 1.1 augustss hwp->getinfo == 0) {
149 1.1 augustss printf("midi: missing method\n");
150 1.1 augustss return;
151 1.1 augustss }
152 1.1 augustss #endif
153 1.15 thorpej
154 1.15 thorpej callout_init(&sc->sc_callout);
155 1.15 thorpej
156 1.1 augustss sc->hw_if = hwp;
157 1.1 augustss sc->hw_hdl = hdlp;
158 1.19 tshiozak sc->dying = 0;
159 1.2 augustss midi_attach(sc, parent);
160 1.2 augustss }
161 1.2 augustss
162 1.18 tshiozak int
163 1.22 augustss midiactivate(struct device *self, enum devact act)
164 1.18 tshiozak {
165 1.18 tshiozak struct midi_softc *sc = (struct midi_softc *)self;
166 1.18 tshiozak
167 1.18 tshiozak switch (act) {
168 1.18 tshiozak case DVACT_ACTIVATE:
169 1.18 tshiozak return (EOPNOTSUPP);
170 1.18 tshiozak
171 1.18 tshiozak case DVACT_DEACTIVATE:
172 1.18 tshiozak sc->dying = 1;
173 1.18 tshiozak break;
174 1.18 tshiozak }
175 1.18 tshiozak return (0);
176 1.18 tshiozak }
177 1.18 tshiozak
178 1.18 tshiozak int
179 1.22 augustss mididetach(struct device *self, int flags)
180 1.18 tshiozak {
181 1.18 tshiozak struct midi_softc *sc = (struct midi_softc *)self;
182 1.18 tshiozak int maj, mn;
183 1.18 tshiozak
184 1.18 tshiozak DPRINTF(("midi_detach: sc=%p flags=%d\n", sc, flags));
185 1.18 tshiozak
186 1.18 tshiozak sc->dying = 1;
187 1.18 tshiozak
188 1.18 tshiozak wakeup(&sc->wchan);
189 1.18 tshiozak wakeup(&sc->rchan);
190 1.18 tshiozak
191 1.18 tshiozak /* locate the major number */
192 1.25 gehenna maj = cdevsw_lookup_major(&midi_cdevsw);
193 1.18 tshiozak
194 1.18 tshiozak /* Nuke the vnodes for any open instances (calls close). */
195 1.18 tshiozak mn = self->dv_unit;
196 1.18 tshiozak vdevgone(maj, mn, mn, VCHR);
197 1.18 tshiozak
198 1.18 tshiozak return (0);
199 1.18 tshiozak }
200 1.18 tshiozak
201 1.2 augustss void
202 1.22 augustss midi_attach(struct midi_softc *sc, struct device *parent)
203 1.2 augustss {
204 1.2 augustss struct midi_info mi;
205 1.2 augustss
206 1.1 augustss sc->isopen = 0;
207 1.1 augustss
208 1.1 augustss midi_wait = MIDI_WAIT * hz / 1000000;
209 1.1 augustss if (midi_wait == 0)
210 1.1 augustss midi_wait = 1;
211 1.1 augustss
212 1.2 augustss sc->sc_dev = parent;
213 1.2 augustss sc->hw_if->getinfo(sc->hw_hdl, &mi);
214 1.1 augustss sc->props = mi.props;
215 1.13 soren printf(": %s\n", mi.name);
216 1.1 augustss }
217 1.1 augustss
218 1.1 augustss int
219 1.22 augustss midi_unit_count(void)
220 1.1 augustss {
221 1.1 augustss return midi_cd.cd_ndevs;
222 1.1 augustss }
223 1.1 augustss
224 1.1 augustss void
225 1.22 augustss midi_initbuf(struct midi_buffer *mb)
226 1.1 augustss {
227 1.1 augustss mb->used = 0;
228 1.1 augustss mb->usedhigh = MIDI_BUFSIZE;
229 1.1 augustss mb->end = mb->start + mb->usedhigh;
230 1.1 augustss mb->inp = mb->outp = mb->start;
231 1.1 augustss }
232 1.1 augustss
233 1.2 augustss int
234 1.22 augustss midi_sleep_timo(int *chan, char *label, int timo)
235 1.1 augustss {
236 1.1 augustss int st;
237 1.1 augustss
238 1.1 augustss if (!label)
239 1.1 augustss label = "midi";
240 1.1 augustss
241 1.1 augustss DPRINTFN(5, ("midi_sleep_timo: %p %s %d\n", chan, label, timo));
242 1.1 augustss *chan = 1;
243 1.1 augustss st = tsleep(chan, PWAIT | PCATCH, label, timo);
244 1.1 augustss *chan = 0;
245 1.1 augustss #ifdef MIDI_DEBUG
246 1.1 augustss if (st != 0)
247 1.1 augustss printf("midi_sleep: %d\n", st);
248 1.1 augustss #endif
249 1.1 augustss return st;
250 1.1 augustss }
251 1.1 augustss
252 1.2 augustss int
253 1.22 augustss midi_sleep(int *chan, char *label)
254 1.1 augustss {
255 1.1 augustss return midi_sleep_timo(chan, label, 0);
256 1.1 augustss }
257 1.1 augustss
258 1.2 augustss void
259 1.22 augustss midi_wakeup(int *chan)
260 1.1 augustss {
261 1.1 augustss if (*chan) {
262 1.1 augustss DPRINTFN(5, ("midi_wakeup: %p\n", chan));
263 1.1 augustss wakeup(chan);
264 1.1 augustss *chan = 0;
265 1.1 augustss }
266 1.1 augustss }
267 1.1 augustss
268 1.1 augustss static int midi_lengths[] = { 2,2,2,2,1,1,2,0 };
269 1.1 augustss /* Number of bytes in a MIDI command */
270 1.1 augustss #define MIDI_LENGTH(d) (midi_lengths[((d) >> 4) & 7])
271 1.1 augustss
272 1.1 augustss void
273 1.22 augustss midi_in(void *addr, int data)
274 1.1 augustss {
275 1.1 augustss struct midi_softc *sc = addr;
276 1.1 augustss struct midi_buffer *mb = &sc->inbuf;
277 1.1 augustss int i;
278 1.1 augustss
279 1.3 augustss if (!sc->isopen)
280 1.3 augustss return;
281 1.1 augustss if (data == MIDI_ACK)
282 1.1 augustss return;
283 1.14 augustss
284 1.14 augustss DPRINTFN(3, ("midi_in: sc=%p data=0x%02x state=%d pos=%d\n",
285 1.14 augustss sc, data, sc->in_state, sc->in_pos));
286 1.14 augustss
287 1.1 augustss if (!(sc->flags & FREAD))
288 1.1 augustss return; /* discard data if not reading */
289 1.1 augustss
290 1.1 augustss switch(sc->in_state) {
291 1.1 augustss case MIDI_IN_START:
292 1.1 augustss if (MIDI_IS_STATUS(data)) {
293 1.1 augustss switch(data) {
294 1.1 augustss case 0xf0: /* Sysex */
295 1.1 augustss sc->in_state = MIDI_IN_SYSEX;
296 1.36 gson sc->in_msg[0] = data;
297 1.36 gson sc->in_pos = 1;
298 1.36 gson sc->in_left = 0;
299 1.36 gson goto deliver_raw;
300 1.1 augustss case 0xf1: /* MTC quarter frame */
301 1.1 augustss case 0xf3: /* Song select */
302 1.1 augustss sc->in_state = MIDI_IN_DATA;
303 1.1 augustss sc->in_msg[0] = data;
304 1.1 augustss sc->in_pos = 1;
305 1.1 augustss sc->in_left = 1;
306 1.1 augustss break;
307 1.1 augustss case 0xf2: /* Song position pointer */
308 1.1 augustss sc->in_state = MIDI_IN_DATA;
309 1.1 augustss sc->in_msg[0] = data;
310 1.1 augustss sc->in_pos = 1;
311 1.1 augustss sc->in_left = 2;
312 1.1 augustss break;
313 1.1 augustss default:
314 1.1 augustss if (MIDI_IS_COMMON(data)) {
315 1.1 augustss sc->in_msg[0] = data;
316 1.1 augustss sc->in_pos = 1;
317 1.1 augustss goto deliver;
318 1.1 augustss } else {
319 1.1 augustss sc->in_state = MIDI_IN_DATA;
320 1.1 augustss sc->in_msg[0] = sc->in_status = data;
321 1.1 augustss sc->in_pos = 1;
322 1.14 augustss sc->in_left = MIDI_LENGTH(data);
323 1.1 augustss }
324 1.1 augustss break;
325 1.1 augustss }
326 1.1 augustss } else {
327 1.1 augustss if (MIDI_IS_STATUS(sc->in_status)) {
328 1.1 augustss sc->in_state = MIDI_IN_DATA;
329 1.1 augustss sc->in_msg[0] = sc->in_status;
330 1.1 augustss sc->in_msg[1] = data;
331 1.1 augustss sc->in_pos = 2;
332 1.1 augustss sc->in_left = MIDI_LENGTH(sc->in_status) - 1;
333 1.35 gson if (sc->in_left == 0)
334 1.35 gson goto deliver;
335 1.1 augustss }
336 1.1 augustss }
337 1.1 augustss return;
338 1.1 augustss case MIDI_IN_DATA:
339 1.35 gson KASSERT(sc->in_left >= 1);
340 1.35 gson KASSERT(sc->in_pos < 3);
341 1.1 augustss sc->in_msg[sc->in_pos++] = data;
342 1.35 gson if (--sc->in_left == 0)
343 1.1 augustss break; /* deliver data */
344 1.1 augustss return;
345 1.1 augustss case MIDI_IN_SYSEX:
346 1.36 gson sc->in_msg[0] = data;
347 1.36 gson sc->in_pos = 1;
348 1.36 gson sc->in_left = 0;
349 1.1 augustss if (data == MIDI_SYSEX_END)
350 1.1 augustss sc->in_state = MIDI_IN_START;
351 1.36 gson goto deliver_raw;
352 1.1 augustss }
353 1.1 augustss deliver:
354 1.1 augustss sc->in_state = MIDI_IN_START;
355 1.1 augustss #if NSEQUENCER > 0
356 1.1 augustss if (sc->seqopen) {
357 1.22 augustss extern void midiseq_in(struct midi_dev *,u_char *,int);
358 1.7 augustss midiseq_in(sc->seq_md, sc->in_msg, sc->in_pos);
359 1.1 augustss return;
360 1.1 augustss }
361 1.1 augustss #endif
362 1.36 gson deliver_raw:
363 1.1 augustss if (mb->used + sc->in_pos > mb->usedhigh) {
364 1.1 augustss DPRINTF(("midi_in: buffer full, discard data=0x%02x\n",
365 1.1 augustss sc->in_msg[0]));
366 1.1 augustss return;
367 1.1 augustss }
368 1.1 augustss for (i = 0; i < sc->in_pos; i++) {
369 1.1 augustss *mb->inp++ = sc->in_msg[i];
370 1.1 augustss if (mb->inp >= mb->end)
371 1.1 augustss mb->inp = mb->start;
372 1.1 augustss mb->used++;
373 1.1 augustss }
374 1.1 augustss midi_wakeup(&sc->rchan);
375 1.30 jdolecek selnotify(&sc->rsel, 0);
376 1.1 augustss if (sc->async)
377 1.1 augustss psignal(sc->async, SIGIO);
378 1.1 augustss }
379 1.1 augustss
380 1.1 augustss void
381 1.22 augustss midi_out(void *addr)
382 1.1 augustss {
383 1.1 augustss struct midi_softc *sc = addr;
384 1.3 augustss
385 1.3 augustss if (!sc->isopen)
386 1.3 augustss return;
387 1.1 augustss DPRINTFN(3, ("midi_out: %p\n", sc));
388 1.1 augustss midi_start_output(sc, 1);
389 1.1 augustss }
390 1.1 augustss
391 1.1 augustss int
392 1.34 fvdl midiopen(dev_t dev, int flags, int ifmt, struct proc *p)
393 1.1 augustss {
394 1.1 augustss struct midi_softc *sc;
395 1.38 yamt const struct midi_hw_if *hw;
396 1.1 augustss int error;
397 1.1 augustss
398 1.17 thorpej sc = device_lookup(&midi_cd, MIDIUNIT(dev));
399 1.17 thorpej if (sc == NULL)
400 1.17 thorpej return (ENXIO);
401 1.18 tshiozak if (sc->dying)
402 1.18 tshiozak return (EIO);
403 1.17 thorpej
404 1.1 augustss DPRINTF(("midiopen %p\n", sc));
405 1.1 augustss
406 1.1 augustss hw = sc->hw_if;
407 1.1 augustss if (!hw)
408 1.1 augustss return ENXIO;
409 1.1 augustss if (sc->isopen)
410 1.1 augustss return EBUSY;
411 1.1 augustss sc->in_state = MIDI_IN_START;
412 1.1 augustss sc->in_status = 0;
413 1.1 augustss error = hw->open(sc->hw_hdl, flags, midi_in, midi_out, sc);
414 1.1 augustss if (error)
415 1.1 augustss return error;
416 1.1 augustss sc->isopen++;
417 1.6 mycroft midi_initbuf(&sc->outbuf);
418 1.6 mycroft midi_initbuf(&sc->inbuf);
419 1.1 augustss sc->flags = flags;
420 1.1 augustss sc->rchan = 0;
421 1.1 augustss sc->wchan = 0;
422 1.1 augustss sc->pbus = 0;
423 1.1 augustss sc->async = 0;
424 1.1 augustss
425 1.4 augustss #ifdef MIDI_SAVE
426 1.4 augustss if (midicnt != 0) {
427 1.4 augustss midisave.cnt = midicnt;
428 1.4 augustss midicnt = 0;
429 1.4 augustss }
430 1.4 augustss #endif
431 1.4 augustss
432 1.1 augustss return 0;
433 1.1 augustss }
434 1.1 augustss
435 1.1 augustss int
436 1.34 fvdl midiclose(dev_t dev, int flags, int ifmt, struct proc *p)
437 1.1 augustss {
438 1.1 augustss int unit = MIDIUNIT(dev);
439 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
440 1.38 yamt const struct midi_hw_if *hw = sc->hw_if;
441 1.1 augustss int s, error;
442 1.1 augustss
443 1.1 augustss DPRINTF(("midiclose %p\n", sc));
444 1.1 augustss
445 1.1 augustss midi_start_output(sc, 0);
446 1.1 augustss error = 0;
447 1.1 augustss s = splaudio();
448 1.1 augustss while (sc->outbuf.used > 0 && !error) {
449 1.1 augustss DPRINTFN(2,("midiclose sleep used=%d\n", sc->outbuf.used));
450 1.3 augustss error = midi_sleep_timo(&sc->wchan, "mid_dr", 30*hz);
451 1.1 augustss }
452 1.6 mycroft splx(s);
453 1.3 augustss sc->isopen = 0;
454 1.1 augustss hw->close(sc->hw_hdl);
455 1.1 augustss #if NSEQUENCER > 0
456 1.1 augustss sc->seqopen = 0;
457 1.7 augustss sc->seq_md = 0;
458 1.1 augustss #endif
459 1.1 augustss return 0;
460 1.1 augustss }
461 1.1 augustss
462 1.1 augustss int
463 1.22 augustss midiread(dev_t dev, struct uio *uio, int ioflag)
464 1.1 augustss {
465 1.1 augustss int unit = MIDIUNIT(dev);
466 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
467 1.1 augustss struct midi_buffer *mb = &sc->inbuf;
468 1.1 augustss int error;
469 1.1 augustss u_char *outp;
470 1.1 augustss int used, cc, n, resid;
471 1.1 augustss int s;
472 1.1 augustss
473 1.11 nathanw DPRINTF(("midiread: %p, count=%lu\n", sc,
474 1.11 nathanw (unsigned long)uio->uio_resid));
475 1.1 augustss
476 1.18 tshiozak if (sc->dying)
477 1.18 tshiozak return EIO;
478 1.18 tshiozak
479 1.1 augustss error = 0;
480 1.1 augustss resid = uio->uio_resid;
481 1.1 augustss while (uio->uio_resid == resid && !error) {
482 1.1 augustss s = splaudio();
483 1.1 augustss while (mb->used <= 0) {
484 1.1 augustss if (ioflag & IO_NDELAY) {
485 1.1 augustss splx(s);
486 1.1 augustss return EWOULDBLOCK;
487 1.1 augustss }
488 1.1 augustss error = midi_sleep(&sc->rchan, "mid rd");
489 1.1 augustss if (error) {
490 1.1 augustss splx(s);
491 1.1 augustss return error;
492 1.1 augustss }
493 1.1 augustss }
494 1.1 augustss used = mb->used;
495 1.1 augustss outp = mb->outp;
496 1.1 augustss splx(s);
497 1.18 tshiozak if (sc->dying)
498 1.18 tshiozak return EIO;
499 1.1 augustss cc = used; /* maximum to read */
500 1.1 augustss n = mb->end - outp;
501 1.1 augustss if (n < cc)
502 1.1 augustss cc = n; /* don't read beyond end of buffer */
503 1.1 augustss if (uio->uio_resid < cc)
504 1.1 augustss cc = uio->uio_resid; /* and no more than we want */
505 1.1 augustss DPRINTFN(3, ("midiread: uiomove cc=%d\n", cc));
506 1.1 augustss error = uiomove(outp, cc, uio);
507 1.1 augustss if (error)
508 1.1 augustss break;
509 1.1 augustss used -= cc;
510 1.1 augustss outp += cc;
511 1.1 augustss if (outp >= mb->end)
512 1.1 augustss outp = mb->start;
513 1.1 augustss s = splaudio();
514 1.1 augustss mb->outp = outp;
515 1.1 augustss mb->used = used;
516 1.1 augustss splx(s);
517 1.1 augustss }
518 1.1 augustss return error;
519 1.1 augustss }
520 1.1 augustss
521 1.1 augustss void
522 1.22 augustss midi_timeout(void *arg)
523 1.1 augustss {
524 1.1 augustss struct midi_softc *sc = arg;
525 1.1 augustss
526 1.1 augustss DPRINTFN(3,("midi_timeout: %p\n", sc));
527 1.1 augustss midi_start_output(sc, 1);
528 1.1 augustss }
529 1.1 augustss
530 1.1 augustss int
531 1.22 augustss midi_start_output(struct midi_softc *sc, int intr)
532 1.1 augustss {
533 1.1 augustss struct midi_buffer *mb = &sc->outbuf;
534 1.21 tshiozak u_char out;
535 1.1 augustss int error;
536 1.1 augustss int s;
537 1.20 tshiozak int i;
538 1.1 augustss
539 1.1 augustss error = 0;
540 1.18 tshiozak
541 1.18 tshiozak if (sc->dying)
542 1.18 tshiozak return EIO;
543 1.18 tshiozak
544 1.1 augustss if (sc->pbus && !intr) {
545 1.1 augustss DPRINTFN(4, ("midi_start_output: busy\n"));
546 1.1 augustss return 0;
547 1.1 augustss }
548 1.20 tshiozak sc->pbus = (mb->used > 0)?1:0;
549 1.20 tshiozak for (i = 0; i < MIDI_MAX_WRITE && mb->used > 0 &&
550 1.20 tshiozak (!error || error==EINPROGRESS); i++) {
551 1.20 tshiozak s = splaudio();
552 1.21 tshiozak out = *mb->outp;
553 1.21 tshiozak mb->outp++;
554 1.21 tshiozak if (mb->outp >= mb->end)
555 1.21 tshiozak mb->outp = mb->start;
556 1.21 tshiozak mb->used--;
557 1.21 tshiozak splx(s);
558 1.4 augustss #ifdef MIDI_SAVE
559 1.21 tshiozak midisave.buf[midicnt] = out;
560 1.4 augustss midicnt = (midicnt + 1) % MIDI_SAVE_SIZE;
561 1.4 augustss #endif
562 1.21 tshiozak DPRINTFN(4, ("midi_start_output: %p i=%d, data=0x%02x\n",
563 1.21 tshiozak sc, i, out));
564 1.21 tshiozak error = sc->hw_if->output(sc->hw_hdl, out);
565 1.20 tshiozak if ((sc->props & MIDI_PROP_OUT_INTR) && error!=EINPROGRESS)
566 1.20 tshiozak /* If ointr is enabled, midi_start_output()
567 1.20 tshiozak * normally writes only one byte,
568 1.20 tshiozak * except hw_if->output() returns EINPROGRESS.
569 1.20 tshiozak */
570 1.20 tshiozak break;
571 1.1 augustss }
572 1.1 augustss midi_wakeup(&sc->wchan);
573 1.30 jdolecek selnotify(&sc->wsel, 0);
574 1.1 augustss if (sc->async)
575 1.1 augustss psignal(sc->async, SIGIO);
576 1.20 tshiozak if (!(sc->props & MIDI_PROP_OUT_INTR) || error==EINPROGRESS) {
577 1.20 tshiozak if (mb->used > 0)
578 1.15 thorpej callout_reset(&sc->sc_callout, midi_wait,
579 1.20 tshiozak midi_timeout, sc);
580 1.20 tshiozak else
581 1.20 tshiozak sc->pbus = 0;
582 1.20 tshiozak }
583 1.20 tshiozak if ((sc->props & MIDI_PROP_OUT_INTR) && error==EINPROGRESS)
584 1.20 tshiozak error = 0;
585 1.20 tshiozak
586 1.1 augustss return error;
587 1.1 augustss }
588 1.1 augustss
589 1.1 augustss int
590 1.22 augustss midiwrite(dev_t dev, struct uio *uio, int ioflag)
591 1.1 augustss {
592 1.1 augustss int unit = MIDIUNIT(dev);
593 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
594 1.1 augustss struct midi_buffer *mb = &sc->outbuf;
595 1.1 augustss int error;
596 1.1 augustss u_char *inp;
597 1.1 augustss int used, cc, n;
598 1.1 augustss int s;
599 1.1 augustss
600 1.11 nathanw DPRINTFN(2, ("midiwrite: %p, unit=%d, count=%lu\n", sc, unit,
601 1.11 nathanw (unsigned long)uio->uio_resid));
602 1.1 augustss
603 1.18 tshiozak if (sc->dying)
604 1.18 tshiozak return EIO;
605 1.18 tshiozak
606 1.1 augustss error = 0;
607 1.1 augustss while (uio->uio_resid > 0 && !error) {
608 1.1 augustss s = splaudio();
609 1.1 augustss if (mb->used >= mb->usedhigh) {
610 1.1 augustss DPRINTFN(3,("midi_write: sleep used=%d hiwat=%d\n",
611 1.1 augustss mb->used, mb->usedhigh));
612 1.1 augustss if (ioflag & IO_NDELAY) {
613 1.1 augustss splx(s);
614 1.1 augustss return EWOULDBLOCK;
615 1.1 augustss }
616 1.1 augustss error = midi_sleep(&sc->wchan, "mid wr");
617 1.1 augustss if (error) {
618 1.1 augustss splx(s);
619 1.1 augustss return error;
620 1.1 augustss }
621 1.1 augustss }
622 1.1 augustss used = mb->used;
623 1.1 augustss inp = mb->inp;
624 1.1 augustss splx(s);
625 1.18 tshiozak if (sc->dying)
626 1.18 tshiozak return EIO;
627 1.1 augustss cc = mb->usedhigh - used; /* maximum to write */
628 1.1 augustss n = mb->end - inp;
629 1.1 augustss if (n < cc)
630 1.9 augustss cc = n; /* don't write beyond end of buffer */
631 1.1 augustss if (uio->uio_resid < cc)
632 1.1 augustss cc = uio->uio_resid; /* and no more than we have */
633 1.1 augustss error = uiomove(inp, cc, uio);
634 1.1 augustss #ifdef MIDI_DEBUG
635 1.1 augustss if (error)
636 1.9 augustss printf("midi_write:(1) uiomove failed %d; "
637 1.9 augustss "cc=%d inp=%p\n",
638 1.1 augustss error, cc, inp);
639 1.1 augustss #endif
640 1.1 augustss if (error)
641 1.1 augustss break;
642 1.1 augustss inp = mb->inp + cc;
643 1.1 augustss if (inp >= mb->end)
644 1.1 augustss inp = mb->start;
645 1.1 augustss s = splaudio();
646 1.1 augustss mb->inp = inp;
647 1.1 augustss mb->used += cc;
648 1.1 augustss splx(s);
649 1.1 augustss error = midi_start_output(sc, 0);
650 1.1 augustss }
651 1.1 augustss return error;
652 1.5 augustss }
653 1.5 augustss
654 1.5 augustss /*
655 1.9 augustss * This write routine is only called from sequencer code and expects
656 1.5 augustss * a write that is smaller than the MIDI buffer.
657 1.5 augustss */
658 1.5 augustss int
659 1.22 augustss midi_writebytes(int unit, u_char *buf, int cc)
660 1.5 augustss {
661 1.5 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
662 1.5 augustss struct midi_buffer *mb = &sc->outbuf;
663 1.5 augustss int n, s;
664 1.5 augustss
665 1.5 augustss DPRINTFN(2, ("midi_writebytes: %p, unit=%d, cc=%d\n", sc, unit, cc));
666 1.9 augustss DPRINTFN(3, ("midi_writebytes: %x %x %x\n",buf[0],buf[1],buf[2]));
667 1.5 augustss
668 1.18 tshiozak if (sc->dying)
669 1.18 tshiozak return EIO;
670 1.18 tshiozak
671 1.5 augustss s = splaudio();
672 1.5 augustss if (mb->used + cc >= mb->usedhigh) {
673 1.5 augustss splx(s);
674 1.5 augustss return (EWOULDBLOCK);
675 1.5 augustss }
676 1.5 augustss n = mb->end - mb->inp;
677 1.5 augustss if (cc < n)
678 1.5 augustss n = cc;
679 1.9 augustss mb->used += cc;
680 1.9 augustss memcpy(mb->inp, buf, n);
681 1.5 augustss mb->inp += n;
682 1.5 augustss if (mb->inp >= mb->end) {
683 1.5 augustss mb->inp = mb->start;
684 1.5 augustss cc -= n;
685 1.5 augustss if (cc > 0) {
686 1.5 augustss memcpy(mb->inp, buf + n, cc);
687 1.5 augustss mb->inp += cc;
688 1.5 augustss }
689 1.5 augustss }
690 1.5 augustss splx(s);
691 1.5 augustss return (midi_start_output(sc, 0));
692 1.1 augustss }
693 1.1 augustss
694 1.1 augustss int
695 1.34 fvdl midiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
696 1.1 augustss {
697 1.1 augustss int unit = MIDIUNIT(dev);
698 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
699 1.38 yamt const struct midi_hw_if *hw = sc->hw_if;
700 1.1 augustss int error;
701 1.1 augustss
702 1.1 augustss DPRINTF(("midiioctl: %p cmd=0x%08lx\n", sc, cmd));
703 1.18 tshiozak
704 1.18 tshiozak if (sc->dying)
705 1.18 tshiozak return EIO;
706 1.18 tshiozak
707 1.1 augustss error = 0;
708 1.1 augustss switch (cmd) {
709 1.1 augustss case FIONBIO:
710 1.1 augustss /* All handled in the upper FS layer. */
711 1.1 augustss break;
712 1.1 augustss
713 1.1 augustss case FIOASYNC:
714 1.1 augustss if (*(int *)addr) {
715 1.1 augustss if (sc->async)
716 1.1 augustss return EBUSY;
717 1.34 fvdl sc->async = p;
718 1.34 fvdl DPRINTF(("midi_ioctl: FIOASYNC %p\n", p));
719 1.1 augustss } else
720 1.1 augustss sc->async = 0;
721 1.1 augustss break;
722 1.1 augustss
723 1.1 augustss #if 0
724 1.1 augustss case MIDI_PRETIME:
725 1.1 augustss /* XXX OSS
726 1.1 augustss * This should set up a read timeout, but that's
727 1.1 augustss * why we have poll(), so there's nothing yet. */
728 1.1 augustss error = EINVAL;
729 1.1 augustss break;
730 1.1 augustss #endif
731 1.1 augustss
732 1.4 augustss #ifdef MIDI_SAVE
733 1.4 augustss case MIDI_GETSAVE:
734 1.4 augustss error = copyout(&midisave, *(void **)addr, sizeof midisave);
735 1.4 augustss break;
736 1.4 augustss #endif
737 1.4 augustss
738 1.1 augustss default:
739 1.1 augustss if (hw->ioctl)
740 1.34 fvdl error = hw->ioctl(sc->hw_hdl, cmd, addr, flag, p);
741 1.1 augustss else
742 1.1 augustss error = EINVAL;
743 1.1 augustss break;
744 1.1 augustss }
745 1.1 augustss return error;
746 1.1 augustss }
747 1.1 augustss
748 1.1 augustss int
749 1.34 fvdl midipoll(dev_t dev, int events, struct proc *p)
750 1.1 augustss {
751 1.1 augustss int unit = MIDIUNIT(dev);
752 1.1 augustss struct midi_softc *sc = midi_cd.cd_devs[unit];
753 1.1 augustss int revents = 0;
754 1.24 gson int s;
755 1.1 augustss
756 1.1 augustss DPRINTF(("midipoll: %p events=0x%x\n", sc, events));
757 1.1 augustss
758 1.18 tshiozak if (sc->dying)
759 1.18 tshiozak return EIO;
760 1.24 gson
761 1.24 gson s = splaudio();
762 1.18 tshiozak
763 1.1 augustss if (events & (POLLIN | POLLRDNORM))
764 1.1 augustss if (sc->inbuf.used > 0)
765 1.1 augustss revents |= events & (POLLIN | POLLRDNORM);
766 1.1 augustss
767 1.1 augustss if (events & (POLLOUT | POLLWRNORM))
768 1.1 augustss if (sc->outbuf.used < sc->outbuf.usedhigh)
769 1.1 augustss revents |= events & (POLLOUT | POLLWRNORM);
770 1.1 augustss
771 1.1 augustss if (revents == 0) {
772 1.1 augustss if (events & (POLLIN | POLLRDNORM))
773 1.34 fvdl selrecord(p, &sc->rsel);
774 1.1 augustss
775 1.1 augustss if (events & (POLLOUT | POLLWRNORM))
776 1.34 fvdl selrecord(p, &sc->wsel);
777 1.1 augustss }
778 1.1 augustss
779 1.1 augustss splx(s);
780 1.1 augustss return revents;
781 1.30 jdolecek }
782 1.30 jdolecek
783 1.30 jdolecek static void
784 1.30 jdolecek filt_midirdetach(struct knote *kn)
785 1.30 jdolecek {
786 1.30 jdolecek struct midi_softc *sc = kn->kn_hook;
787 1.30 jdolecek int s;
788 1.30 jdolecek
789 1.30 jdolecek s = splaudio();
790 1.31 christos SLIST_REMOVE(&sc->rsel.sel_klist, kn, knote, kn_selnext);
791 1.30 jdolecek splx(s);
792 1.30 jdolecek }
793 1.30 jdolecek
794 1.30 jdolecek static int
795 1.30 jdolecek filt_midiread(struct knote *kn, long hint)
796 1.30 jdolecek {
797 1.30 jdolecek struct midi_softc *sc = kn->kn_hook;
798 1.30 jdolecek
799 1.30 jdolecek /* XXXLUKEM (thorpej): please make sure this is correct. */
800 1.30 jdolecek
801 1.30 jdolecek kn->kn_data = sc->inbuf.used;
802 1.30 jdolecek return (kn->kn_data > 0);
803 1.30 jdolecek }
804 1.30 jdolecek
805 1.30 jdolecek static const struct filterops midiread_filtops =
806 1.30 jdolecek { 1, NULL, filt_midirdetach, filt_midiread };
807 1.30 jdolecek
808 1.30 jdolecek static void
809 1.30 jdolecek filt_midiwdetach(struct knote *kn)
810 1.30 jdolecek {
811 1.30 jdolecek struct midi_softc *sc = kn->kn_hook;
812 1.30 jdolecek int s;
813 1.30 jdolecek
814 1.30 jdolecek s = splaudio();
815 1.31 christos SLIST_REMOVE(&sc->wsel.sel_klist, kn, knote, kn_selnext);
816 1.30 jdolecek splx(s);
817 1.30 jdolecek }
818 1.30 jdolecek
819 1.30 jdolecek static int
820 1.30 jdolecek filt_midiwrite(struct knote *kn, long hint)
821 1.30 jdolecek {
822 1.30 jdolecek struct midi_softc *sc = kn->kn_hook;
823 1.30 jdolecek
824 1.30 jdolecek /* XXXLUKEM (thorpej): please make sure this is correct. */
825 1.30 jdolecek
826 1.30 jdolecek kn->kn_data = sc->outbuf.usedhigh - sc->outbuf.used;
827 1.30 jdolecek return (kn->kn_data > 0);
828 1.30 jdolecek }
829 1.30 jdolecek
830 1.30 jdolecek static const struct filterops midiwrite_filtops =
831 1.30 jdolecek { 1, NULL, filt_midiwdetach, filt_midiwrite };
832 1.30 jdolecek
833 1.30 jdolecek int
834 1.30 jdolecek midikqfilter(dev_t dev, struct knote *kn)
835 1.30 jdolecek {
836 1.30 jdolecek int unit = MIDIUNIT(dev);
837 1.30 jdolecek struct midi_softc *sc = midi_cd.cd_devs[unit];
838 1.30 jdolecek struct klist *klist;
839 1.30 jdolecek int s;
840 1.30 jdolecek
841 1.30 jdolecek switch (kn->kn_filter) {
842 1.30 jdolecek case EVFILT_READ:
843 1.31 christos klist = &sc->rsel.sel_klist;
844 1.30 jdolecek kn->kn_fop = &midiread_filtops;
845 1.30 jdolecek break;
846 1.30 jdolecek
847 1.30 jdolecek case EVFILT_WRITE:
848 1.31 christos klist = &sc->wsel.sel_klist;
849 1.30 jdolecek kn->kn_fop = &midiwrite_filtops;
850 1.30 jdolecek break;
851 1.30 jdolecek
852 1.30 jdolecek default:
853 1.30 jdolecek return (1);
854 1.30 jdolecek }
855 1.30 jdolecek
856 1.30 jdolecek kn->kn_hook = sc;
857 1.30 jdolecek
858 1.30 jdolecek s = splaudio();
859 1.30 jdolecek SLIST_INSERT_HEAD(klist, kn, kn_selnext);
860 1.30 jdolecek splx(s);
861 1.30 jdolecek
862 1.30 jdolecek return (0);
863 1.1 augustss }
864 1.1 augustss
865 1.1 augustss void
866 1.22 augustss midi_getinfo(dev_t dev, struct midi_info *mi)
867 1.1 augustss {
868 1.1 augustss struct midi_softc *sc;
869 1.1 augustss
870 1.17 thorpej sc = device_lookup(&midi_cd, MIDIUNIT(dev));
871 1.17 thorpej if (sc == NULL)
872 1.18 tshiozak return;
873 1.18 tshiozak if (sc->dying)
874 1.1 augustss return;
875 1.17 thorpej
876 1.1 augustss sc->hw_if->getinfo(sc->hw_hdl, mi);
877 1.4 augustss }
878 1.4 augustss
879 1.10 drochner #endif /* NMIDI > 0 */
880 1.10 drochner
881 1.10 drochner #if NMIDI > 0 || NMIDIBUS > 0
882 1.10 drochner
883 1.22 augustss int audioprint(void *, const char *);
884 1.4 augustss
885 1.12 augustss struct device *
886 1.38 yamt midi_attach_mi(const struct midi_hw_if *mhwp, void *hdlp, struct device *dev)
887 1.4 augustss {
888 1.4 augustss struct audio_attach_args arg;
889 1.4 augustss
890 1.4 augustss #ifdef DIAGNOSTIC
891 1.4 augustss if (mhwp == NULL) {
892 1.32 thorpej aprint_error("midi_attach_mi: NULL\n");
893 1.12 augustss return (0);
894 1.4 augustss }
895 1.4 augustss #endif
896 1.4 augustss arg.type = AUDIODEV_TYPE_MIDI;
897 1.4 augustss arg.hwif = mhwp;
898 1.4 augustss arg.hdl = hdlp;
899 1.12 augustss return (config_found(dev, &arg, audioprint));
900 1.1 augustss }
901 1.1 augustss
902 1.10 drochner #endif /* NMIDI > 0 || NMIDIBUS > 0 */
903