| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * tascam-midi.c - a part of driver for TASCAM FireWire series |
| 4 | * |
| 5 | * Copyright (c) 2015 Takashi Sakamoto |
| 6 | */ |
| 7 | |
| 8 | #include "tascam.h" |
| 9 | |
| 10 | static int midi_capture_open(struct snd_rawmidi_substream *substream) |
| 11 | { |
| 12 | /* Do nothing. */ |
| 13 | return 0; |
| 14 | } |
| 15 | |
| 16 | static int midi_playback_open(struct snd_rawmidi_substream *substream) |
| 17 | { |
| 18 | struct snd_tscm *tscm = substream->rmidi->private_data; |
| 19 | |
| 20 | snd_fw_async_midi_port_init(port: &tscm->out_ports[substream->number]); |
| 21 | |
| 22 | return 0; |
| 23 | } |
| 24 | |
| 25 | static int midi_capture_close(struct snd_rawmidi_substream *substream) |
| 26 | { |
| 27 | /* Do nothing. */ |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | static int midi_playback_close(struct snd_rawmidi_substream *substream) |
| 32 | { |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static void midi_playback_drain(struct snd_rawmidi_substream *substream) |
| 37 | { |
| 38 | struct snd_tscm *tscm = substream->rmidi->private_data; |
| 39 | |
| 40 | snd_fw_async_midi_port_finish(port: &tscm->out_ports[substream->number]); |
| 41 | } |
| 42 | |
| 43 | static void midi_capture_trigger(struct snd_rawmidi_substream *substrm, int up) |
| 44 | { |
| 45 | struct snd_tscm *tscm = substrm->rmidi->private_data; |
| 46 | |
| 47 | guard(spinlock_irqsave)(l: &tscm->lock); |
| 48 | |
| 49 | if (up) |
| 50 | tscm->tx_midi_substreams[substrm->number] = substrm; |
| 51 | else |
| 52 | tscm->tx_midi_substreams[substrm->number] = NULL; |
| 53 | } |
| 54 | |
| 55 | static void midi_playback_trigger(struct snd_rawmidi_substream *substrm, int up) |
| 56 | { |
| 57 | struct snd_tscm *tscm = substrm->rmidi->private_data; |
| 58 | |
| 59 | guard(spinlock_irqsave)(l: &tscm->lock); |
| 60 | |
| 61 | if (up) |
| 62 | snd_fw_async_midi_port_run(port: &tscm->out_ports[substrm->number], |
| 63 | substream: substrm); |
| 64 | } |
| 65 | |
| 66 | int snd_tscm_create_midi_devices(struct snd_tscm *tscm) |
| 67 | { |
| 68 | static const struct snd_rawmidi_ops capture_ops = { |
| 69 | .open = midi_capture_open, |
| 70 | .close = midi_capture_close, |
| 71 | .trigger = midi_capture_trigger, |
| 72 | }; |
| 73 | static const struct snd_rawmidi_ops playback_ops = { |
| 74 | .open = midi_playback_open, |
| 75 | .close = midi_playback_close, |
| 76 | .drain = midi_playback_drain, |
| 77 | .trigger = midi_playback_trigger, |
| 78 | }; |
| 79 | struct snd_rawmidi *rmidi; |
| 80 | struct snd_rawmidi_str *stream; |
| 81 | struct snd_rawmidi_substream *subs; |
| 82 | int err; |
| 83 | |
| 84 | err = snd_rawmidi_new(card: tscm->card, id: tscm->card->driver, device: 0, |
| 85 | output_count: tscm->spec->midi_playback_ports, |
| 86 | input_count: tscm->spec->midi_capture_ports, |
| 87 | rmidi: &rmidi); |
| 88 | if (err < 0) |
| 89 | return err; |
| 90 | |
| 91 | snprintf(buf: rmidi->name, size: sizeof(rmidi->name), |
| 92 | fmt: "%s MIDI" , tscm->card->shortname); |
| 93 | rmidi->private_data = tscm; |
| 94 | |
| 95 | rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT; |
| 96 | snd_rawmidi_set_ops(rmidi, stream: SNDRV_RAWMIDI_STREAM_INPUT, |
| 97 | ops: &capture_ops); |
| 98 | stream = &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]; |
| 99 | |
| 100 | /* Set port names for MIDI input. */ |
| 101 | list_for_each_entry(subs, &stream->substreams, list) { |
| 102 | /* TODO: support virtual MIDI ports. */ |
| 103 | if (subs->number < tscm->spec->midi_capture_ports) { |
| 104 | /* Hardware MIDI ports. */ |
| 105 | scnprintf(buf: subs->name, size: sizeof(subs->name), |
| 106 | fmt: "%s MIDI %d" , |
| 107 | tscm->card->shortname, subs->number + 1); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT; |
| 112 | snd_rawmidi_set_ops(rmidi, stream: SNDRV_RAWMIDI_STREAM_OUTPUT, |
| 113 | ops: &playback_ops); |
| 114 | stream = &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]; |
| 115 | |
| 116 | /* Set port names for MIDI ourput. */ |
| 117 | list_for_each_entry(subs, &stream->substreams, list) { |
| 118 | if (subs->number < tscm->spec->midi_playback_ports) { |
| 119 | /* Hardware MIDI ports only. */ |
| 120 | scnprintf(buf: subs->name, size: sizeof(subs->name), |
| 121 | fmt: "%s MIDI %d" , |
| 122 | tscm->card->shortname, subs->number + 1); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | rmidi->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX; |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |