1 // SPDX-License-Identifier: GPL-2.0-only
3 * fireworks_midi.c - a part of driver for Fireworks based devices
5 * Copyright (c) 2009-2010 Clemens Ladisch
6 * Copyright (c) 2013-2014 Takashi Sakamoto
10 static int midi_capture_open(struct snd_rawmidi_substream *substream)
12 struct snd_efw *efw = substream->rmidi->private_data;
15 err = snd_efw_stream_lock_try(efw);
19 mutex_lock(&efw->mutex);
20 efw->capture_substreams++;
21 err = snd_efw_stream_start_duplex(efw, 0);
22 mutex_unlock(&efw->mutex);
24 snd_efw_stream_lock_release(efw);
30 static int midi_playback_open(struct snd_rawmidi_substream *substream)
32 struct snd_efw *efw = substream->rmidi->private_data;
35 err = snd_efw_stream_lock_try(efw);
39 mutex_lock(&efw->mutex);
40 efw->playback_substreams++;
41 err = snd_efw_stream_start_duplex(efw, 0);
42 mutex_unlock(&efw->mutex);
44 snd_efw_stream_lock_release(efw);
49 static int midi_capture_close(struct snd_rawmidi_substream *substream)
51 struct snd_efw *efw = substream->rmidi->private_data;
53 mutex_lock(&efw->mutex);
54 efw->capture_substreams--;
55 snd_efw_stream_stop_duplex(efw);
56 mutex_unlock(&efw->mutex);
58 snd_efw_stream_lock_release(efw);
62 static int midi_playback_close(struct snd_rawmidi_substream *substream)
64 struct snd_efw *efw = substream->rmidi->private_data;
66 mutex_lock(&efw->mutex);
67 efw->playback_substreams--;
68 snd_efw_stream_stop_duplex(efw);
69 mutex_unlock(&efw->mutex);
71 snd_efw_stream_lock_release(efw);
75 static void midi_capture_trigger(struct snd_rawmidi_substream *substrm, int up)
77 struct snd_efw *efw = substrm->rmidi->private_data;
80 spin_lock_irqsave(&efw->lock, flags);
83 amdtp_am824_midi_trigger(&efw->tx_stream,
84 substrm->number, substrm);
86 amdtp_am824_midi_trigger(&efw->tx_stream,
87 substrm->number, NULL);
89 spin_unlock_irqrestore(&efw->lock, flags);
92 static void midi_playback_trigger(struct snd_rawmidi_substream *substrm, int up)
94 struct snd_efw *efw = substrm->rmidi->private_data;
97 spin_lock_irqsave(&efw->lock, flags);
100 amdtp_am824_midi_trigger(&efw->rx_stream,
101 substrm->number, substrm);
103 amdtp_am824_midi_trigger(&efw->rx_stream,
104 substrm->number, NULL);
106 spin_unlock_irqrestore(&efw->lock, flags);
109 static void set_midi_substream_names(struct snd_efw *efw,
110 struct snd_rawmidi_str *str)
112 struct snd_rawmidi_substream *subs;
114 list_for_each_entry(subs, &str->substreams, list) {
115 snprintf(subs->name, sizeof(subs->name),
116 "%s MIDI %d", efw->card->shortname, subs->number + 1);
120 int snd_efw_create_midi_devices(struct snd_efw *efw)
122 static const struct snd_rawmidi_ops capture_ops = {
123 .open = midi_capture_open,
124 .close = midi_capture_close,
125 .trigger = midi_capture_trigger,
127 static const struct snd_rawmidi_ops playback_ops = {
128 .open = midi_playback_open,
129 .close = midi_playback_close,
130 .trigger = midi_playback_trigger,
132 struct snd_rawmidi *rmidi;
133 struct snd_rawmidi_str *str;
136 /* create midi ports */
137 err = snd_rawmidi_new(efw->card, efw->card->driver, 0,
138 efw->midi_out_ports, efw->midi_in_ports,
143 snprintf(rmidi->name, sizeof(rmidi->name),
144 "%s MIDI", efw->card->shortname);
145 rmidi->private_data = efw;
147 if (efw->midi_in_ports > 0) {
148 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
150 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
153 str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT];
155 set_midi_substream_names(efw, str);
158 if (efw->midi_out_ports > 0) {
159 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
161 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
164 str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT];
166 set_midi_substream_names(efw, str);
169 if ((efw->midi_out_ports > 0) && (efw->midi_in_ports > 0))
170 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX;