Lines Matching +full:0 +full:m

125 	struct snd_midi *m;  member
151 DEFINE_CLASS(midisynth, midisynth_methods, 0);
160 * 0 == no error
182 static int midistat_isopen = 0;
237 SYSCTL_NODE(_hw, OID_AUTO, midi, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
239 static SYSCTL_NODE(_hw_midi, OID_AUTO, stat, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
244 SYSCTL_INT(_hw_midi, OID_AUTO, debug, CTLFLAG_RW, &midi_debug, 0, "");
247 SYSCTL_INT(_hw_midi, OID_AUTO, dumpraw, CTLFLAG_RW, &midi_dumpraw, 0, "");
250 SYSCTL_INT(_hw_midi, OID_AUTO, instroff, CTLFLAG_RW, &midi_instroff, 0, "");
254 &midistat_verbose, 0, "");
280 * Register a new rmidi device. cls midi_if interface unit == 0 means
281 * auto-assign new unit number unit != 0 already assigned a unit number, eg.
284 * unit=0, channel=1..(number of channels) and cookie=soft_c and won't care
295 struct snd_midi *m; in midi_init() local
307 TAILQ_FOREACH(m, &midi_devs, link) { in midi_init()
308 mtx_lock(&m->lock); in midi_init()
309 if (unit != 0) { in midi_init()
310 if (m->unit == unit && m->channel == channel) { in midi_init()
311 mtx_unlock(&m->lock); in midi_init()
318 if (m->unit > i) in midi_init()
319 i = m->unit; in midi_init()
321 mtx_unlock(&m->lock); in midi_init()
324 if (unit == 0) in midi_init()
328 m = malloc(sizeof(*m), M_MIDI, M_WAITOK | M_ZERO); in midi_init()
329 m->synth = malloc(sizeof(*m->synth), M_MIDI, M_WAITOK | M_ZERO); in midi_init()
330 kobj_init((kobj_t)m->synth, &midisynth_class); in midi_init()
331 m->synth->m = m; in midi_init()
332 kobj_init((kobj_t)m, cls); in midi_init()
333 inqsize = MPU_INQSIZE(m, cookie); in midi_init()
334 outqsize = MPU_OUTQSIZE(m, cookie); in midi_init()
340 mtx_init(&m->lock, "raw midi", NULL, 0); in midi_init()
341 mtx_init(&m->qlock, "q raw midi", NULL, 0); in midi_init()
343 mtx_lock(&m->lock); in midi_init()
344 mtx_lock(&m->qlock); in midi_init()
351 MIDIQ_INIT(m->inq, buf, inqsize); in midi_init()
357 m->hiwat = outqsize / 2; in midi_init()
359 MIDIQ_INIT(m->outq, buf, outqsize); in midi_init()
361 if ((inqsize && !MIDIQ_BUF(m->inq)) || in midi_init()
362 (outqsize && !MIDIQ_BUF(m->outq))) in midi_init()
365 m->busy = 0; in midi_init()
366 m->flags = 0; in midi_init()
367 m->unit = unit; in midi_init()
368 m->channel = channel; in midi_init()
369 m->cookie = cookie; in midi_init()
371 if (MPU_INIT(m, cookie)) in midi_init()
374 mtx_unlock(&m->lock); in midi_init()
375 mtx_unlock(&m->qlock); in midi_init()
377 TAILQ_INSERT_TAIL(&midi_devs, m, link); in midi_init()
381 m->dev = make_dev(&midi_cdevsw, unit, UID_ROOT, GID_WHEEL, 0666, in midi_init()
383 m->dev->si_drv1 = m; in midi_init()
385 return m; in midi_init()
388 mtx_destroy(&m->qlock); in midi_init()
389 mtx_destroy(&m->lock); in midi_init()
391 if (MIDIQ_BUF(m->inq)) in midi_init()
392 free(MIDIQ_BUF(m->inq), M_MIDI); in midi_init()
393 if (MIDIQ_BUF(m->outq)) in midi_init()
394 free(MIDIQ_BUF(m->outq), M_MIDI); in midi_init()
396 free(m->synth, M_MIDI); in midi_init()
397 free(m, M_MIDI); in midi_init()
412 midi_uninit(struct snd_midi *m) in midi_uninit() argument
418 mtx_lock(&m->lock); in midi_uninit()
419 if (m->busy) { in midi_uninit()
420 if (!(m->rchan || m->wchan)) in midi_uninit()
423 if (m->rchan) { in midi_uninit()
424 wakeup(&m->rchan); in midi_uninit()
425 m->rchan = 0; in midi_uninit()
427 if (m->wchan) { in midi_uninit()
428 wakeup(&m->wchan); in midi_uninit()
429 m->wchan = 0; in midi_uninit()
432 err = midi_destroy(m, 0); in midi_uninit()
437 mtx_unlock(&m->lock); in midi_uninit()
451 static int midi_lengths[] = {2, 2, 2, 2, 1, 1, 2, 0};
456 #define MIDI_ACK 0xfe
457 #define MIDI_IS_STATUS(d) ((d) >= 0x80)
458 #define MIDI_IS_COMMON(d) ((d) >= 0xf0)
460 #define MIDI_SYSEX_START 0xF0
461 #define MIDI_SYSEX_END 0xF7
464 midi_in(struct snd_midi *m, uint8_t *buf, int size) in midi_in() argument
470 MIDI_DEBUG(5, printf("midi_in: m=%p size=%d\n", m, size)); in midi_in()
475 if (!(m->flags & M_RX)) in midi_in()
478 used = 0; in midi_in()
480 mtx_lock(&m->qlock); in midi_in()
481 #if 0 in midi_in()
487 if (!(m->flags & M_RX)) in midi_in()
490 for (i = sig = 0; i < size; i++) { in midi_in()
492 enq = 0; in midi_in()
496 switch (m->inq_state) { in midi_in()
500 case 0xf0: /* Sysex */ in midi_in()
501 m->inq_state = MIDI_IN_SYSEX; in midi_in()
503 case 0xf1: /* MTC quarter frame */ in midi_in()
504 case 0xf3: /* Song select */ in midi_in()
505 m->inq_state = MIDI_IN_DATA; in midi_in()
507 m->inq_left = 1; in midi_in()
509 case 0xf2: /* Song position pointer */ in midi_in()
510 m->inq_state = MIDI_IN_DATA; in midi_in()
512 m->inq_left = 2; in midi_in()
519 m->inq_state = MIDI_IN_DATA; in midi_in()
521 m->inq_status = data; in midi_in()
522 m->inq_left = MIDI_LENGTH(data); in midi_in()
526 } else if (MIDI_IS_STATUS(m->inq_status)) { in midi_in()
527 m->inq_state = MIDI_IN_DATA; in midi_in()
528 if (!MIDIQ_FULL(m->inq)) { in midi_in()
530 MIDIQ_ENQ(m->inq, &m->inq_status, 1); in midi_in()
533 m->inq_left = MIDI_LENGTH(m->inq_status) - 1; in midi_in()
542 if (--m->inq_left <= 0) in midi_in()
547 m->inq_state = MIDI_IN_START; in midi_in()
552 if (!MIDIQ_FULL(m->inq)) { in midi_in()
553 MIDIQ_ENQ(m->inq, &data, 1); in midi_in()
563 (intmax_t)MIDIQ_LEN(m->inq), in midi_in()
564 (intmax_t)MIDIQ_AVAIL(m->inq))); in midi_in()
565 if (MIDIQ_AVAIL(m->inq) > size) { in midi_in()
567 MIDIQ_ENQ(m->inq, buf, size); in midi_in()
570 mtx_unlock(&m->qlock); in midi_in()
571 return 0; in midi_in()
573 if (m->rchan) { in midi_in()
574 wakeup(&m->rchan); in midi_in()
575 m->rchan = 0; in midi_in()
577 selwakeup(&m->rsel); in midi_in()
578 if (m->async) { in midi_in()
579 PROC_LOCK(m->async); in midi_in()
580 kern_psignal(m->async, SIGIO); in midi_in()
581 PROC_UNLOCK(m->async); in midi_in()
583 #if 0 in midi_in()
586 mtx_unlock(&m->qlock); in midi_in()
594 midi_out(struct snd_midi *m, uint8_t *buf, int size) in midi_out() argument
601 if (!(m->flags & M_TXEN)) in midi_out()
602 return 0; in midi_out()
604 MIDI_DEBUG(2, printf("midi_out: %p\n", m)); in midi_out()
605 mtx_lock(&m->qlock); in midi_out()
606 used = MIN(size, MIDIQ_LEN(m->outq)); in midi_out()
609 MIDIQ_DEQ(m->outq, buf, used); in midi_out()
610 if (MIDIQ_EMPTY(m->outq)) { in midi_out()
611 m->flags &= ~M_TXEN; in midi_out()
612 MPU_CALLBACKP(m, m->cookie, m->flags); in midi_out()
614 if (used && MIDIQ_AVAIL(m->outq) > m->hiwat) { in midi_out()
615 if (m->wchan) { in midi_out()
616 wakeup(&m->wchan); in midi_out()
617 m->wchan = 0; in midi_out()
619 selwakeup(&m->wsel); in midi_out()
620 if (m->async) { in midi_out()
621 PROC_LOCK(m->async); in midi_out()
622 kern_psignal(m->async, SIGIO); in midi_out()
623 PROC_UNLOCK(m->async); in midi_out()
626 mtx_unlock(&m->qlock); in midi_out()
636 struct snd_midi *m = i_dev->si_drv1; in midi_open() local
641 if (m == NULL) in midi_open()
644 mtx_lock(&m->lock); in midi_open()
645 mtx_lock(&m->qlock); in midi_open()
647 retval = 0; in midi_open()
650 if (MIDIQ_SIZE(m->inq) == 0) in midi_open()
652 else if (m->flags & M_RX) in midi_open()
658 if (MIDIQ_SIZE(m->outq) == 0) in midi_open()
660 else if (m->flags & M_TX) in midi_open()
665 m->busy++; in midi_open()
667 m->rchan = 0; in midi_open()
668 m->wchan = 0; in midi_open()
669 m->async = 0; in midi_open()
672 m->flags |= M_RX | M_RXEN; in midi_open()
677 MIDIQ_CLEAR(m->inq); in midi_open()
681 m->flags |= M_TX; in midi_open()
683 MPU_CALLBACK(m, m->cookie, m->flags); in midi_open()
687 err: mtx_unlock(&m->qlock); in midi_open()
688 mtx_unlock(&m->lock); in midi_open()
695 struct snd_midi *m = i_dev->si_drv1; in midi_close() local
702 if (m == NULL) in midi_close()
705 mtx_lock(&m->lock); in midi_close()
706 mtx_lock(&m->qlock); in midi_close()
708 if ((flags & FREAD && !(m->flags & M_RX)) || in midi_close()
709 (flags & FWRITE && !(m->flags & M_TX))) { in midi_close()
713 m->busy--; in midi_close()
715 oldflags = m->flags; in midi_close()
718 m->flags &= ~(M_RX | M_RXEN); in midi_close()
720 m->flags &= ~M_TX; in midi_close()
722 if ((m->flags & (M_TXEN | M_RXEN)) != (oldflags & (M_RXEN | M_TXEN))) in midi_close()
723 MPU_CALLBACK(m, m->cookie, m->flags); in midi_close()
725 MIDI_DEBUG(1, printf("midi_close: closed, busy = %d.\n", m->busy)); in midi_close()
727 mtx_unlock(&m->qlock); in midi_close()
728 mtx_unlock(&m->lock); in midi_close()
729 retval = 0; in midi_close()
741 struct snd_midi *m = i_dev->si_drv1; in midi_read() local
751 if (m == NULL) in midi_read()
754 mtx_lock(&m->lock); in midi_read()
755 mtx_lock(&m->qlock); in midi_read()
757 if (!(m->flags & M_RX)) in midi_read()
760 while (uio->uio_resid > 0) { in midi_read()
761 while (MIDIQ_EMPTY(m->inq)) { in midi_read()
765 mtx_unlock(&m->lock); in midi_read()
766 m->rchan = 1; in midi_read()
767 retval = msleep(&m->rchan, &m->qlock, in midi_read()
768 PCATCH | PDROP, "midi RX", 0); in midi_read()
775 if (m != i_dev->si_drv1) in midi_read()
780 mtx_lock(&m->lock); in midi_read()
781 mtx_lock(&m->qlock); in midi_read()
782 m->rchan = 0; in midi_read()
783 if (!m->busy) in midi_read()
788 * At this point, it is certain that m->inq has data in midi_read()
791 used = MIN(MIDIQ_LEN(m->inq), uio->uio_resid); in midi_read()
795 MIDIQ_DEQ(m->inq, buf, used); in midi_read()
804 retval = 0; in midi_read()
805 err1: mtx_unlock(&m->qlock); in midi_read()
806 mtx_unlock(&m->lock); in midi_read()
819 struct snd_midi *m = i_dev->si_drv1; in midi_write() local
825 retval = 0; in midi_write()
826 if (m == NULL) in midi_write()
829 mtx_lock(&m->lock); in midi_write()
830 mtx_lock(&m->qlock); in midi_write()
832 if (!(m->flags & M_TX)) in midi_write()
835 while (uio->uio_resid > 0) { in midi_write()
836 while (MIDIQ_AVAIL(m->outq) == 0) { in midi_write()
840 mtx_unlock(&m->lock); in midi_write()
841 m->wchan = 1; in midi_write()
843 retval = msleep(&m->wchan, &m->qlock, in midi_write()
844 PCATCH | PDROP, "midi TX", 0); in midi_write()
851 if (m != i_dev->si_drv1) in midi_write()
855 mtx_lock(&m->lock); in midi_write()
856 mtx_lock(&m->qlock); in midi_write()
857 m->wchan = 0; in midi_write()
858 if (!m->busy) in midi_write()
866 used = MIN(MIDIQ_AVAIL(m->outq), uio->uio_resid); in midi_write()
869 uio->uio_resid, (intmax_t)MIDIQ_LEN(m->outq), in midi_write()
870 (intmax_t)MIDIQ_AVAIL(m->outq))); in midi_write()
876 MIDIQ_ENQ(m->outq, buf, used); in midi_write()
880 if (!(m->flags & M_TXEN)) { in midi_write()
881 m->flags |= M_TXEN; in midi_write()
882 MPU_CALLBACK(m, m->cookie, m->flags); in midi_write()
888 retval = 0; in midi_write()
889 err1: mtx_unlock(&m->qlock); in midi_write()
890 mtx_unlock(&m->lock); in midi_write()
904 struct snd_midi *m = i_dev->si_drv1; in midi_poll() local
907 if (m == NULL) in midi_poll()
908 return 0; in midi_poll()
910 revents = 0; in midi_poll()
912 mtx_lock(&m->lock); in midi_poll()
913 mtx_lock(&m->qlock); in midi_poll()
916 if (!MIDIQ_EMPTY(m->inq)) in midi_poll()
920 if (MIDIQ_AVAIL(m->outq) < m->hiwat) in midi_poll()
923 if (revents == 0) { in midi_poll()
925 selrecord(td, &m->rsel); in midi_poll()
928 selrecord(td, &m->wsel); in midi_poll()
930 mtx_unlock(&m->lock); in midi_poll()
931 mtx_unlock(&m->qlock); in midi_poll()
954 error = (midistat_prepare(&midistat_sbuf) > 0) ? 0 : ENOMEM; in midistat_open()
956 midistat_isopen = 0; in midistat_open()
971 midistat_isopen = 0; in midistat_close()
973 return 0; in midistat_close()
988 if (uio->uio_offset < 0 || uio->uio_offset > sbuf_len(&midistat_sbuf)) { in midistat_read()
992 err = 0; in midistat_read()
994 if (l > 0) { in midistat_read()
1009 struct snd_midi *m; in midistat_prepare() local
1021 TAILQ_FOREACH(m, &midi_devs, link) { in midistat_prepare()
1022 mtx_lock(&m->lock); in midistat_prepare()
1023 sbuf_printf(s, "%s [%d/%d:%s]", m->name, m->unit, m->channel, in midistat_prepare()
1024 MPU_PROVIDER(m, m->cookie)); in midistat_prepare()
1025 sbuf_printf(s, "%s", MPU_DESCR(m, m->cookie, midistat_verbose)); in midistat_prepare()
1027 mtx_unlock(&m->lock); in midistat_prepare()
1079 struct snd_midi *m = ((struct synth_midi *)n)->m; in midisynth_open() local
1085 if (m == NULL) in midisynth_open()
1088 mtx_lock(&m->lock); in midisynth_open()
1089 mtx_lock(&m->qlock); in midisynth_open()
1091 retval = 0; in midisynth_open()
1094 if (MIDIQ_SIZE(m->inq) == 0) in midisynth_open()
1096 else if (m->flags & M_RX) in midisynth_open()
1102 if (MIDIQ_SIZE(m->outq) == 0) in midisynth_open()
1104 else if (m->flags & M_TX) in midisynth_open()
1109 m->busy++; in midisynth_open()
1112 * TODO: Consider m->async = 0; in midisynth_open()
1116 m->flags |= M_RX | M_RXEN; in midisynth_open()
1121 MIDIQ_CLEAR(m->inq); in midisynth_open()
1122 m->rchan = 0; in midisynth_open()
1126 m->flags |= M_TX; in midisynth_open()
1127 m->wchan = 0; in midisynth_open()
1129 m->synth_flags = flags & (FREAD | FWRITE); in midisynth_open()
1131 MPU_CALLBACK(m, m->cookie, m->flags); in midisynth_open()
1133 err: mtx_unlock(&m->qlock); in midisynth_open()
1134 mtx_unlock(&m->lock); in midisynth_open()
1142 struct snd_midi *m = ((struct synth_midi *)n)->m; in midisynth_close() local
1147 m->synth_flags & FREAD ? "M_RX" : "", in midisynth_close()
1148 m->synth_flags & FWRITE ? "M_TX" : "")); in midisynth_close()
1150 if (m == NULL) in midisynth_close()
1153 mtx_lock(&m->lock); in midisynth_close()
1154 mtx_lock(&m->qlock); in midisynth_close()
1156 if ((m->synth_flags & FREAD && !(m->flags & M_RX)) || in midisynth_close()
1157 (m->synth_flags & FWRITE && !(m->flags & M_TX))) { in midisynth_close()
1161 m->busy--; in midisynth_close()
1163 oldflags = m->flags; in midisynth_close()
1165 if (m->synth_flags & FREAD) in midisynth_close()
1166 m->flags &= ~(M_RX | M_RXEN); in midisynth_close()
1167 if (m->synth_flags & FWRITE) in midisynth_close()
1168 m->flags &= ~M_TX; in midisynth_close()
1170 if ((m->flags & (M_TXEN | M_RXEN)) != (oldflags & (M_RXEN | M_TXEN))) in midisynth_close()
1171 MPU_CALLBACK(m, m->cookie, m->flags); in midisynth_close()
1173 MIDI_DEBUG(1, printf("midi_close: closed, busy = %d.\n", m->busy)); in midisynth_close()
1175 mtx_unlock(&m->qlock); in midisynth_close()
1176 mtx_unlock(&m->lock); in midisynth_close()
1177 retval = 0; in midisynth_close()
1188 struct snd_midi *m = ((struct synth_midi *)n)->m; in midisynth_writeraw() local
1195 retval = 0; in midisynth_writeraw()
1197 if (m == NULL) in midisynth_writeraw()
1200 mtx_lock(&m->lock); in midisynth_writeraw()
1201 mtx_lock(&m->qlock); in midisynth_writeraw()
1203 if (!(m->flags & M_TX)) in midisynth_writeraw()
1209 while (len > 0) { in midisynth_writeraw()
1210 while (MIDIQ_AVAIL(m->outq) == 0) { in midisynth_writeraw()
1211 if (!(m->flags & M_TXEN)) { in midisynth_writeraw()
1212 m->flags |= M_TXEN; in midisynth_writeraw()
1213 MPU_CALLBACK(m, m->cookie, m->flags); in midisynth_writeraw()
1215 mtx_unlock(&m->lock); in midisynth_writeraw()
1216 m->wchan = 1; in midisynth_writeraw()
1218 retval = msleep(&m->wchan, &m->qlock, in midisynth_writeraw()
1219 PCATCH | PDROP, "midi TX", 0); in midisynth_writeraw()
1229 mtx_lock(&m->lock); in midisynth_writeraw()
1230 mtx_lock(&m->qlock); in midisynth_writeraw()
1231 m->wchan = 0; in midisynth_writeraw()
1232 if (!m->busy) in midisynth_writeraw()
1240 used = MIN(MIDIQ_AVAIL(m->outq), len); in midisynth_writeraw()
1244 len, (intmax_t)MIDIQ_LEN(m->outq), in midisynth_writeraw()
1245 (intmax_t)MIDIQ_AVAIL(m->outq))); in midisynth_writeraw()
1248 for (i = 0; i < used; i++) in midisynth_writeraw()
1251 MIDIQ_ENQ(m->outq, buf, used); in midisynth_writeraw()
1257 if (!(m->flags & M_TXEN)) { in midisynth_writeraw()
1258 m->flags |= M_TXEN; in midisynth_writeraw()
1259 MPU_CALLBACK(m, m->cookie, m->flags); in midisynth_writeraw()
1268 retval = 0; in midisynth_writeraw()
1269 err1: mtx_unlock(&m->qlock); in midisynth_writeraw()
1270 mtx_unlock(&m->lock); in midisynth_writeraw()
1286 c[0] = 0x90 | (chn & 0x0f); /* Note on. */ in midisynth_killnote()
1288 c[2] = 0; in midisynth_killnote()
1290 c[0] = 0x80 | (chn & 0x0f); /* Note off. */ in midisynth_killnote()
1306 c[0] = 0xc0 | (chn & 0x0f); /* Progamme change. */ in midisynth_setinstr()
1323 c[0] = 0x90 | (chn & 0x0f); /* Note on. */ in midisynth_startnote()
1343 c[0] = 0xb0 | (chn & 0x0f); /* Control Message. */ in midisynth_controller()
1357 c[0] = 0xe0 | (chn & 0x0f); /* Pitch bend. */ in midisynth_bender()
1358 c[1] = (u_char)val & 0x7f; in midisynth_bender()
1359 c[2] = (u_char)(val >> 7) & 0x7f; in midisynth_bender()
1368 midi_destroy(struct snd_midi *m, int midiuninit) in midi_destroy() argument
1371 mtx_assert(&m->lock, MA_OWNED); in midi_destroy()
1374 m->dev->si_drv1 = NULL; in midi_destroy()
1375 mtx_unlock(&m->lock); /* XXX */ in midi_destroy()
1376 destroy_dev(m->dev); in midi_destroy()
1377 TAILQ_REMOVE(&midi_devs, m, link); in midi_destroy()
1379 MPU_UNINIT(m, m->cookie); in midi_destroy()
1380 free(MIDIQ_BUF(m->inq), M_MIDI); in midi_destroy()
1381 free(MIDIQ_BUF(m->outq), M_MIDI); in midi_destroy()
1382 mtx_destroy(&m->qlock); in midi_destroy()
1383 mtx_destroy(&m->lock); in midi_destroy()
1384 free(m->synth, M_MIDI); in midi_destroy()
1385 free(m, M_MIDI); in midi_destroy()
1386 return 0; in midi_destroy()
1402 return 0; in midi_load()
1408 struct snd_midi *m, *tmp; in midi_unload() local
1417 TAILQ_FOREACH_SAFE(m, &midi_devs, link, tmp) { in midi_unload()
1418 mtx_lock(&m->lock); in midi_unload()
1419 if (m->busy) in midi_unload()
1422 retval = midi_destroy(m, 1); in midi_unload()
1433 return 0; in midi_unload()
1436 mtx_unlock(&m->lock); in midi_unload()
1451 retval = 0; in midi_modevent()
1456 if (retval == 0) in midi_modevent()
1462 if (retval == 0) in midi_modevent()
1484 int retval = 0; in midimapper_open_locked()
1485 struct snd_midi *m; in midimapper_open_locked() local
1488 TAILQ_FOREACH(m, &midi_devs, link) { in midimapper_open_locked()
1510 return 0; in midimapper_close()
1516 struct snd_midi *m; in midimapper_fetch_synth_locked() local
1517 int retval = 0; in midimapper_fetch_synth_locked()
1520 TAILQ_FOREACH(m, &midi_devs, link) { in midimapper_fetch_synth_locked()
1522 return (kobj_t)m->synth; in midimapper_fetch_synth_locked()