1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
|
/* $OpenBSD: midi.c,v 1.28 2012/04/17 07:58:47 ratchov Exp $ */
/*
* Copyright (c) 2003, 2004 Alexandre Ratchov
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* TODO
* - put the sequencer stuff in sequencer.c and sequencervar.h
* there is no reason to have it here. The sequencer
* driver need only to open the midi hw_if thus it does not
* need this driver
*/
#include "sequencer.h"
#include <sys/param.h>
#include <sys/fcntl.h>
#include <sys/systm.h>
#include <sys/ioctl.h>
#include <sys/exec.h>
#include <sys/conf.h>
#include <sys/lkm.h>
#include <sys/proc.h>
#include <sys/poll.h>
#include <sys/kernel.h>
#include <sys/timeout.h>
#include <sys/vnode.h>
#include <sys/signalvar.h>
#include <sys/malloc.h>
#include <sys/device.h>
#include <dev/midi_if.h>
#include <dev/audio_if.h>
#include <dev/midivar.h>
int midiopen(dev_t, int, int, struct proc *);
int midiclose(dev_t, int, int, struct proc *);
int midiread(dev_t, struct uio *, int);
int midiwrite(dev_t, struct uio *, int);
int midipoll(dev_t, int, struct proc *);
int midikqfilter(dev_t, struct knote *);
int midiioctl(dev_t, u_long, caddr_t, int, struct proc *);
int midiprobe(struct device *, void *, void *);
void midiattach(struct device *, struct device *, void *);
int mididetach(struct device *, int);
int midiprint(void *, const char *);
void midi_iintr(void *, int);
void midi_ointr(void *);
void midi_out_start(struct midi_softc *);
void midi_out_stop(struct midi_softc *);
void midi_out_do(struct midi_softc *);
void midi_attach(struct midi_softc *, struct device *);
#if NSEQUENCER > 0
int midi_unit_count(void);
void midi_toevent(struct midi_softc *, int);
int midi_writebytes(int, u_char *, int);
void midiseq_in(struct midi_dev *, u_char *, int);
#endif
struct cfattach midi_ca = {
sizeof(struct midi_softc), midiprobe, midiattach, mididetach
};
struct cfdriver midi_cd = {
NULL, "midi", DV_DULL
};
void filt_midiwdetach(struct knote *);
int filt_midiwrite(struct knote *, long);
struct filterops midiwrite_filtops = {
1, NULL, filt_midiwdetach, filt_midiwrite
};
void filt_midirdetach(struct knote *);
int filt_midiread(struct knote *, long);
struct filterops midiread_filtops = {
1, NULL, filt_midirdetach, filt_midiread
};
void
midi_iintr(void *addr, int data)
{
struct midi_softc *sc = (struct midi_softc *)addr;
struct midi_buffer *mb = &sc->inbuf;
if (sc->isdying || !sc->isopen || !(sc->flags & FREAD))
return;
#if NSEQUENCER > 0
if (sc->seqopen) {
midi_toevent(sc, data);
return;
}
#endif
if (MIDIBUF_ISFULL(mb))
return; /* discard data */
MIDIBUF_WRITE(mb, data);
if (mb->used == 1) {
if (sc->rchan) {
sc->rchan = 0;
wakeup(&sc->rchan);
}
selwakeup(&sc->rsel);
if (sc->async)
psignal(sc->async, SIGIO);
}
}
int
midiread(dev_t dev, struct uio *uio, int ioflag)
{
struct midi_softc *sc = MIDI_DEV2SC(dev);
struct midi_buffer *mb = &sc->inbuf;
unsigned count;
int s, error;
if (!(sc->flags & FREAD))
return ENXIO;
/* if there is no data then sleep (unless IO_NDELAY flag is set) */
s = splaudio();
while (MIDIBUF_ISEMPTY(mb)) {
if (sc->isdying) {
splx(s);
return EIO;
}
if (ioflag & IO_NDELAY) {
splx(s);
return EWOULDBLOCK;
}
sc->rchan = 1;
error = tsleep(&sc->rchan, PWAIT|PCATCH, "mid_rd", 0);
if (error) {
splx(s);
return error;
}
}
/* at this stage, there is at least 1 byte */
while (uio->uio_resid > 0 && mb->used > 0) {
count = MIDIBUF_SIZE - mb->start;
if (count > mb->used)
count = mb->used;
if (count > uio->uio_resid)
count = uio->uio_resid;
error = uiomove(mb->data + mb->start, count, uio);
if (error) {
splx(s);
return error;
}
MIDIBUF_REMOVE(mb, count);
}
splx(s);
return 0;
}
void
midi_ointr(void *addr)
{
struct midi_softc *sc = (struct midi_softc *)addr;
struct midi_buffer *mb;
int s;
if (sc->isopen && !sc->isdying) {
mb = &sc->outbuf;
s = splaudio();
if (mb->used > 0) {
#ifdef MIDI_DEBUG
if (!sc->isbusy) {
printf("midi_ointr: output must be busy\n");
}
#endif
midi_out_do(sc);
} else if (sc->isbusy)
midi_out_stop(sc);
splx(s);
}
}
void
midi_out_start(struct midi_softc *sc)
{
if (!sc->isbusy) {
sc->isbusy = 1;
midi_out_do(sc);
}
}
void
midi_out_stop(struct midi_softc *sc)
{
sc->isbusy = 0;
if (sc->wchan) {
sc->wchan = 0;
wakeup(&sc->wchan);
}
selwakeup(&sc->wsel);
if (sc->async)
psignal(sc->async, SIGIO);
}
void
midi_out_do(struct midi_softc *sc)
{
struct midi_buffer *mb = &sc->outbuf;
while (mb->used > 0) {
if (!sc->hw_if->output(sc->hw_hdl, mb->data[mb->start]))
break;
MIDIBUF_REMOVE(mb, 1);
if (MIDIBUF_ISEMPTY(mb)) {
if (sc->hw_if->flush != NULL)
sc->hw_if->flush(sc->hw_hdl);
midi_out_stop(sc);
return;
}
}
if (!(sc->props & MIDI_PROP_OUT_INTR)) {
if (MIDIBUF_ISEMPTY(mb))
midi_out_stop(sc);
else
timeout_add(&sc->timeo, 1);
}
}
int
midiwrite(dev_t dev, struct uio *uio, int ioflag)
{
struct midi_softc *sc = MIDI_DEV2SC(dev);
struct midi_buffer *mb = &sc->outbuf;
unsigned count;
int s, error;
if (!(sc->flags & FWRITE))
return ENXIO;
if (sc->isdying)
return EIO;
/*
* If IO_NDELAY flag is set then check if there is enough room
* in the buffer to store at least one byte. If not then dont
* start the write process.
*/
if ((ioflag & IO_NDELAY) && MIDIBUF_ISFULL(mb) && (uio->uio_resid > 0))
return EWOULDBLOCK;
while (uio->uio_resid > 0) {
s = splaudio();
while (MIDIBUF_ISFULL(mb)) {
if (ioflag & IO_NDELAY) {
/*
* At this stage at least one byte is already
* moved so we do not return EWOULDBLOCK
*/
splx(s);
return 0;
}
sc->wchan = 1;
error = tsleep(&sc->wchan, PWAIT|PCATCH, "mid_wr", 0);
if (error) {
splx(s);
return error;
}
if (sc->isdying) {
splx(s);
return EIO;
}
}
count = MIDIBUF_SIZE - MIDIBUF_END(mb);
if (count > MIDIBUF_AVAIL(mb))
count = MIDIBUF_AVAIL(mb);
if (count > uio->uio_resid)
count = uio->uio_resid;
error = uiomove(mb->data + MIDIBUF_END(mb), count, uio);
if (error) {
splx(s);
return error;
}
mb->used += count;
midi_out_start(sc);
splx(s);
}
return 0;
}
int
midipoll(dev_t dev, int events, struct proc *p)
{
struct midi_softc *sc = MIDI_DEV2SC(dev);
int s, revents;
if (sc->isdying)
return POLLERR;
revents = 0;
s = splaudio();
if (events & (POLLIN | POLLRDNORM)) {
if (!MIDIBUF_ISEMPTY(&sc->inbuf))
revents |= events & (POLLIN | POLLRDNORM);
}
if (events & (POLLOUT | POLLWRNORM)) {
if (!MIDIBUF_ISFULL(&sc->outbuf))
revents |= events & (POLLOUT | POLLWRNORM);
}
if (revents == 0) {
if (events & (POLLIN | POLLRDNORM))
selrecord(p, &sc->rsel);
if (events & (POLLOUT | POLLWRNORM))
selrecord(p, &sc->wsel);
}
splx(s);
return (revents);
}
int
midikqfilter(dev_t dev, struct knote *kn)
{
struct midi_softc *sc = MIDI_DEV2SC(dev);
struct klist *klist;
int s;
switch (kn->kn_filter) {
case EVFILT_READ:
klist = &sc->rsel.si_note;
kn->kn_fop = &midiread_filtops;
break;
case EVFILT_WRITE:
klist = &sc->wsel.si_note;
kn->kn_fop = &midiwrite_filtops;
break;
default:
return (EINVAL);
}
kn->kn_hook = (void *)sc;
s = splaudio();
SLIST_INSERT_HEAD(klist, kn, kn_selnext);
splx(s);
return (0);
}
void
filt_midirdetach(struct knote *kn)
{
struct midi_softc *sc = (struct midi_softc *)kn->kn_hook;
int s;
s = splaudio();
SLIST_REMOVE(&sc->rsel.si_note, kn, knote, kn_selnext);
splx(s);
}
int
filt_midiread(struct knote *kn, long hint)
{
struct midi_softc *sc = (struct midi_softc *)kn->kn_hook;
int s, retval;
s = splaudio();
retval = !MIDIBUF_ISEMPTY(&sc->inbuf);
splx(s);
return (retval);
}
void
filt_midiwdetach(struct knote *kn)
{
struct midi_softc *sc = (struct midi_softc *)kn->kn_hook;
int s;
s = splaudio();
SLIST_REMOVE(&sc->wsel.si_note, kn, knote, kn_selnext);
splx(s);
}
int
filt_midiwrite(struct knote *kn, long hint)
{
struct midi_softc *sc = (struct midi_softc *)kn->kn_hook;
int s, retval;
s = splaudio();
retval = !MIDIBUF_ISFULL(&sc->outbuf);
splx(s);
return (retval);
}
int
midiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
{
struct midi_softc *sc = MIDI_DEV2SC(dev);
if (sc->isdying)
return EIO;
switch(cmd) {
case FIONBIO:
/* All handled in the upper FS layer */
break;
case FIOASYNC:
if (*(int *)addr) {
if (sc->async)
return EBUSY;
sc->async = p;
} else
sc->async = 0;
break;
default:
return ENOTTY;
}
return 0;
}
int
midiopen(dev_t dev, int flags, int mode, struct proc *p)
{
struct midi_softc *sc;
int err;
if (MIDI_UNIT(dev) >= midi_cd.cd_ndevs)
return ENXIO;
sc = MIDI_DEV2SC(dev);
if (sc == NULL) /* there may be more units than devices */
return ENXIO;
if (sc->isdying)
return EIO;
if (sc->isopen)
return EBUSY;
MIDIBUF_INIT(&sc->inbuf);
MIDIBUF_INIT(&sc->outbuf);
sc->isbusy = 0;
sc->rchan = sc->wchan = 0;
sc->async = 0;
sc->flags = flags;
err = sc->hw_if->open(sc->hw_hdl, flags, midi_iintr, midi_ointr, sc);
if (err)
return err;
sc->isopen = 1;
#if NSEQUENCER > 0
sc->seq_md = 0;
sc->seqopen = 0;
sc->evstatus = 0xff;
#endif
return 0;
}
int
midiclose(dev_t dev, int fflag, int devtype, struct proc *p)
{
struct midi_softc *sc = MIDI_DEV2SC(dev);
struct midi_buffer *mb;
int error;
int s;
mb = &sc->outbuf;
if (!sc->isdying) {
/* start draining output buffer */
s = splaudio();
if (!MIDIBUF_ISEMPTY(mb))
midi_out_start(sc);
while (sc->isbusy) {
sc->wchan = 1;
error = tsleep(&sc->wchan, PWAIT, "mid_dr", 5 * hz);
if (error || sc->isdying)
break;
}
splx(s);
}
/*
* some hw_if->close() reset immediately the midi uart
* which flushes the internal buffer of the uart device,
* so we may lose some (important) data. To avoid this,
* sleep 20ms (around 64 bytes) to give the time to the
* uart to drain its internal buffers.
*/
tsleep(&sc->wchan, PWAIT, "mid_cl", hz * MIDI_MAXWRITE / MIDI_RATE);
sc->hw_if->close(sc->hw_hdl);
sc->isopen = 0;
return 0;
}
int
midiprobe(struct device *parent, void *match, void *aux)
{
struct audio_attach_args *sa = aux;
return (sa != NULL && (sa->type == AUDIODEV_TYPE_MIDI) ? 1 : 0);
}
void
midi_attach(struct midi_softc *sc, struct device *parent)
{
struct midi_info mi;
sc->isdying = 0;
sc->hw_if->getinfo(sc->hw_hdl, &mi);
sc->props = mi.props;
sc->isopen = 0;
timeout_set(&sc->timeo, midi_ointr, sc);
printf(": <%s>\n", mi.name);
}
void
midiattach(struct device *parent, struct device *self, void *aux)
{
struct midi_softc *sc = (struct midi_softc *)self;
struct audio_attach_args *sa = (struct audio_attach_args *)aux;
struct midi_hw_if *hwif = sa->hwif;
void *hdl = sa->hdl;
#ifdef DIAGNOSTIC
if (hwif == 0 ||
hwif->open == 0 ||
hwif->close == 0 ||
hwif->output == 0 ||
hwif->getinfo == 0) {
printf("midi: missing method\n");
return;
}
#endif
sc->hw_if = hwif;
sc->hw_hdl = hdl;
midi_attach(sc, parent);
}
int
mididetach(struct device *self, int flags)
{
struct midi_softc *sc = (struct midi_softc *)self;
int maj, mn;
sc->isdying = 1;
if (sc->wchan) {
sc->wchan = 0;
wakeup(&sc->wchan);
}
if (sc->rchan) {
sc->rchan = 0;
wakeup(&sc->rchan);
}
/* locate the major number */
for (maj = 0; maj < nchrdev; maj++) {
if (cdevsw[maj].d_open == midiopen) {
/* Nuke the vnodes for any open instances (calls close). */
mn = self->dv_unit;
vdevgone(maj, mn, mn, VCHR);
}
}
return 0;
}
int
midiprint(void *aux, const char *pnp)
{
if (pnp)
printf("midi at %s", pnp);
return (UNCONF);
}
void
midi_getinfo(dev_t dev, struct midi_info *mi)
{
struct midi_softc *sc = MIDI_DEV2SC(dev);
if (MIDI_UNIT(dev) >= midi_cd.cd_ndevs || sc == NULL || sc->isdying) {
mi->name = "unconfigured";
mi->props = 0;
return;
}
sc->hw_if->getinfo(sc->hw_hdl, mi);
}
struct device *
midi_attach_mi(struct midi_hw_if *hwif, void *hdl, struct device *dev)
{
struct audio_attach_args arg;
arg.type = AUDIODEV_TYPE_MIDI;
arg.hwif = hwif;
arg.hdl = hdl;
return config_found(dev, &arg, midiprint);
}
int
midi_unit_count(void)
{
return midi_cd.cd_ndevs;
}
#if NSEQUENCER > 0
#define MIDI_EVLEN(status) (midi_evlen[((status) >> 4) & 7])
unsigned midi_evlen[] = { 2, 2, 2, 2, 1, 1, 2 };
void
midi_toevent(struct midi_softc *sc, int data)
{
unsigned char mesg[3];
if (data >= 0xf8) { /* is it a realtime message ? */
switch(data) {
case 0xf8: /* midi timer tic */
case 0xfa: /* midi timer start */
case 0xfb: /* midi timer continue (after stop) */
case 0xfc: /* midi timer stop */
mesg[0] = data;
midiseq_in(sc->seq_md, mesg, 1);
break;
default:
break;
}
} else if (data >= 0x80) { /* is it a common or voice message ? */
sc->evstatus = data;
sc->evindex = 0;
} else { /* else it is a data byte */
/* strip common messages and bogus data */
if (sc->evstatus >= 0xf0 || sc->evstatus < 0x80)
return;
sc->evdata[sc->evindex++] = data;
if (sc->evindex == MIDI_EVLEN(sc->evstatus)) {
sc->evindex = 0;
mesg[0] = sc->evstatus;
mesg[1] = sc->evdata[0];
mesg[2] = sc->evdata[1];
midiseq_in(sc->seq_md, mesg, 1 + MIDI_EVLEN(sc->evstatus));
}
}
}
int
midi_writebytes(int unit, unsigned char *mesg, int mesglen)
{
struct midi_softc *sc = midi_cd.cd_devs[unit];
struct midi_buffer *mb = &sc->outbuf;
unsigned count;
int s;
s = splaudio();
if (mesglen > MIDIBUF_AVAIL(mb)) {
splx(s);
return EWOULDBLOCK;
}
while (mesglen > 0) {
count = MIDIBUF_SIZE - MIDIBUF_END(mb);
if (count > MIDIBUF_AVAIL(mb)) count = MIDIBUF_AVAIL(mb);
if (count > mesglen) count = mesglen;
bcopy(mesg, mb->data + MIDIBUF_END(mb), count);
mb->used += count;
mesg += count;
mesglen -= count;
midi_out_start(sc);
}
splx(s);
return 0;
}
#endif /* NSEQUENCER > 0 */
|