Takashi Sakamoto | 9e796e7 | 2017-03-22 21:30:23 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * motu-midi.h - a part of driver for MOTU FireWire series |
| 3 | * |
| 4 | * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp> |
| 5 | * |
| 6 | * Licensed under the terms of the GNU General Public License, version 2. |
| 7 | */ |
| 8 | #include "motu.h" |
| 9 | |
| 10 | static int midi_capture_open(struct snd_rawmidi_substream *substream) |
| 11 | { |
| 12 | struct snd_motu *motu = substream->rmidi->private_data; |
| 13 | int err; |
| 14 | |
| 15 | mutex_lock(&motu->mutex); |
| 16 | |
| 17 | motu->capture_substreams++; |
| 18 | err = snd_motu_stream_start_duplex(motu, 0); |
| 19 | |
| 20 | mutex_unlock(&motu->mutex); |
| 21 | |
| 22 | return err; |
| 23 | } |
| 24 | |
| 25 | static int midi_playback_open(struct snd_rawmidi_substream *substream) |
| 26 | { |
| 27 | struct snd_motu *motu = substream->rmidi->private_data; |
| 28 | int err; |
| 29 | |
| 30 | mutex_lock(&motu->mutex); |
| 31 | |
| 32 | motu->playback_substreams++; |
| 33 | err = snd_motu_stream_start_duplex(motu, 0); |
| 34 | |
| 35 | mutex_unlock(&motu->mutex); |
| 36 | |
| 37 | return err; |
| 38 | } |
| 39 | |
| 40 | static int midi_capture_close(struct snd_rawmidi_substream *substream) |
| 41 | { |
| 42 | struct snd_motu *motu = substream->rmidi->private_data; |
| 43 | |
| 44 | mutex_lock(&motu->mutex); |
| 45 | |
| 46 | motu->capture_substreams--; |
| 47 | snd_motu_stream_stop_duplex(motu); |
| 48 | |
| 49 | mutex_unlock(&motu->mutex); |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int midi_playback_close(struct snd_rawmidi_substream *substream) |
| 55 | { |
| 56 | struct snd_motu *motu = substream->rmidi->private_data; |
| 57 | |
| 58 | mutex_lock(&motu->mutex); |
| 59 | |
| 60 | motu->playback_substreams--; |
| 61 | snd_motu_stream_stop_duplex(motu); |
| 62 | |
| 63 | mutex_unlock(&motu->mutex); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static void midi_capture_trigger(struct snd_rawmidi_substream *substrm, int up) |
| 69 | { |
| 70 | struct snd_motu *motu = substrm->rmidi->private_data; |
| 71 | unsigned long flags; |
| 72 | |
| 73 | spin_lock_irqsave(&motu->lock, flags); |
| 74 | |
| 75 | if (up) |
| 76 | amdtp_motu_midi_trigger(&motu->tx_stream, substrm->number, |
| 77 | substrm); |
| 78 | else |
| 79 | amdtp_motu_midi_trigger(&motu->tx_stream, substrm->number, |
| 80 | NULL); |
| 81 | |
| 82 | spin_unlock_irqrestore(&motu->lock, flags); |
| 83 | } |
| 84 | |
| 85 | static void midi_playback_trigger(struct snd_rawmidi_substream *substrm, int up) |
| 86 | { |
| 87 | struct snd_motu *motu = substrm->rmidi->private_data; |
| 88 | unsigned long flags; |
| 89 | |
| 90 | spin_lock_irqsave(&motu->lock, flags); |
| 91 | |
| 92 | if (up) |
| 93 | amdtp_motu_midi_trigger(&motu->rx_stream, substrm->number, |
| 94 | substrm); |
| 95 | else |
| 96 | amdtp_motu_midi_trigger(&motu->rx_stream, substrm->number, |
| 97 | NULL); |
| 98 | |
| 99 | spin_unlock_irqrestore(&motu->lock, flags); |
| 100 | } |
| 101 | |
| 102 | static void set_midi_substream_names(struct snd_motu *motu, |
| 103 | struct snd_rawmidi_str *str) |
| 104 | { |
| 105 | struct snd_rawmidi_substream *subs; |
| 106 | |
| 107 | list_for_each_entry(subs, &str->substreams, list) { |
| 108 | snprintf(subs->name, sizeof(subs->name), |
| 109 | "%s MIDI %d", motu->card->shortname, subs->number + 1); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | int snd_motu_create_midi_devices(struct snd_motu *motu) |
| 114 | { |
| 115 | static struct snd_rawmidi_ops capture_ops = { |
| 116 | .open = midi_capture_open, |
| 117 | .close = midi_capture_close, |
| 118 | .trigger = midi_capture_trigger, |
| 119 | }; |
| 120 | static struct snd_rawmidi_ops playback_ops = { |
| 121 | .open = midi_playback_open, |
| 122 | .close = midi_playback_close, |
| 123 | .trigger = midi_playback_trigger, |
| 124 | }; |
| 125 | struct snd_rawmidi *rmidi; |
| 126 | struct snd_rawmidi_str *str; |
| 127 | int err; |
| 128 | |
| 129 | /* create midi ports */ |
| 130 | err = snd_rawmidi_new(motu->card, motu->card->driver, 0, 1, 1, &rmidi); |
| 131 | if (err < 0) |
| 132 | return err; |
| 133 | |
| 134 | snprintf(rmidi->name, sizeof(rmidi->name), |
| 135 | "%s MIDI", motu->card->shortname); |
| 136 | rmidi->private_data = motu; |
| 137 | |
| 138 | rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT | |
| 139 | SNDRV_RAWMIDI_INFO_OUTPUT | |
| 140 | SNDRV_RAWMIDI_INFO_DUPLEX; |
| 141 | |
| 142 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, |
| 143 | &capture_ops); |
| 144 | str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]; |
| 145 | set_midi_substream_names(motu, str); |
| 146 | |
| 147 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, |
| 148 | &playback_ops); |
| 149 | str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]; |
| 150 | set_midi_substream_names(motu, str); |
| 151 | |
| 152 | return 0; |
| 153 | } |