blob: f5da2cd4ce4219ab7a78519600cdc6dca168f0ff [file] [log] [blame]
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +09001/*
2 * fireworks_midi.c - a part of driver for Fireworks based devices
3 *
4 * Copyright (c) 2009-2010 Clemens Ladisch
5 * Copyright (c) 2013-2014 Takashi Sakamoto
6 *
7 * Licensed under the terms of the GNU General Public License, version 2.
8 */
9#include "fireworks.h"
10
11static int midi_capture_open(struct snd_rawmidi_substream *substream)
12{
13 struct snd_efw *efw = substream->rmidi->private_data;
Takashi Sakamoto594ddce2014-04-25 22:45:12 +090014 int err;
15
16 err = snd_efw_stream_lock_try(efw);
17 if (err < 0)
18 goto end;
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +090019
Takashi Sakamotoea54a372015-11-14 16:54:50 +090020 mutex_lock(&efw->mutex);
Takashi Sakamoto4d2c50a2015-11-14 16:54:51 +090021 efw->capture_substreams++;
Takashi Sakamoto594ddce2014-04-25 22:45:12 +090022 err = snd_efw_stream_start_duplex(efw, 0);
Takashi Sakamotoea54a372015-11-14 16:54:50 +090023 mutex_unlock(&efw->mutex);
Takashi Sakamoto594ddce2014-04-25 22:45:12 +090024 if (err < 0)
25 snd_efw_stream_lock_release(efw);
26
27end:
28 return err;
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +090029}
30
31static int midi_playback_open(struct snd_rawmidi_substream *substream)
32{
33 struct snd_efw *efw = substream->rmidi->private_data;
Takashi Sakamoto594ddce2014-04-25 22:45:12 +090034 int err;
35
36 err = snd_efw_stream_lock_try(efw);
37 if (err < 0)
38 goto end;
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +090039
Takashi Sakamotoea54a372015-11-14 16:54:50 +090040 mutex_lock(&efw->mutex);
Takashi Sakamoto4d2c50a2015-11-14 16:54:51 +090041 efw->playback_substreams++;
Takashi Sakamoto594ddce2014-04-25 22:45:12 +090042 err = snd_efw_stream_start_duplex(efw, 0);
Takashi Sakamotoea54a372015-11-14 16:54:50 +090043 mutex_unlock(&efw->mutex);
Takashi Sakamoto594ddce2014-04-25 22:45:12 +090044 if (err < 0)
45 snd_efw_stream_lock_release(efw);
46end:
47 return err;
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +090048}
49
50static int midi_capture_close(struct snd_rawmidi_substream *substream)
51{
52 struct snd_efw *efw = substream->rmidi->private_data;
53
Takashi Sakamotoea54a372015-11-14 16:54:50 +090054 mutex_lock(&efw->mutex);
Takashi Sakamoto4d2c50a2015-11-14 16:54:51 +090055 efw->capture_substreams--;
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +090056 snd_efw_stream_stop_duplex(efw);
Takashi Sakamotoea54a372015-11-14 16:54:50 +090057 mutex_unlock(&efw->mutex);
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +090058
Takashi Sakamoto594ddce2014-04-25 22:45:12 +090059 snd_efw_stream_lock_release(efw);
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +090060 return 0;
61}
62
63static int midi_playback_close(struct snd_rawmidi_substream *substream)
64{
65 struct snd_efw *efw = substream->rmidi->private_data;
66
Takashi Sakamotoea54a372015-11-14 16:54:50 +090067 mutex_lock(&efw->mutex);
Takashi Sakamoto4d2c50a2015-11-14 16:54:51 +090068 efw->playback_substreams--;
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +090069 snd_efw_stream_stop_duplex(efw);
Takashi Sakamotoea54a372015-11-14 16:54:50 +090070 mutex_unlock(&efw->mutex);
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +090071
Takashi Sakamoto594ddce2014-04-25 22:45:12 +090072 snd_efw_stream_lock_release(efw);
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +090073 return 0;
74}
75
76static void midi_capture_trigger(struct snd_rawmidi_substream *substrm, int up)
77{
78 struct snd_efw *efw = substrm->rmidi->private_data;
79 unsigned long flags;
80
81 spin_lock_irqsave(&efw->lock, flags);
82
83 if (up)
Takashi Sakamoto03e2a672015-09-19 11:21:59 +090084 amdtp_am824_midi_trigger(&efw->tx_stream,
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +090085 substrm->number, substrm);
86 else
Takashi Sakamoto03e2a672015-09-19 11:21:59 +090087 amdtp_am824_midi_trigger(&efw->tx_stream,
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +090088 substrm->number, NULL);
89
90 spin_unlock_irqrestore(&efw->lock, flags);
91}
92
93static void midi_playback_trigger(struct snd_rawmidi_substream *substrm, int up)
94{
95 struct snd_efw *efw = substrm->rmidi->private_data;
96 unsigned long flags;
97
98 spin_lock_irqsave(&efw->lock, flags);
99
100 if (up)
Takashi Sakamoto03e2a672015-09-19 11:21:59 +0900101 amdtp_am824_midi_trigger(&efw->rx_stream,
102 substrm->number, substrm);
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +0900103 else
Takashi Sakamoto03e2a672015-09-19 11:21:59 +0900104 amdtp_am824_midi_trigger(&efw->rx_stream,
105 substrm->number, NULL);
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +0900106
107 spin_unlock_irqrestore(&efw->lock, flags);
108}
109
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +0900110static void set_midi_substream_names(struct snd_efw *efw,
111 struct snd_rawmidi_str *str)
112{
113 struct snd_rawmidi_substream *subs;
114
115 list_for_each_entry(subs, &str->substreams, list) {
116 snprintf(subs->name, sizeof(subs->name),
117 "%s MIDI %d", efw->card->shortname, subs->number + 1);
118 }
119}
120
121int snd_efw_create_midi_devices(struct snd_efw *efw)
122{
Takashi Iwai57eb6792017-01-05 17:29:54 +0100123 static const struct snd_rawmidi_ops capture_ops = {
Takashi Sakamoto7cdc8872017-01-05 21:48:08 +0900124 .open = midi_capture_open,
125 .close = midi_capture_close,
126 .trigger = midi_capture_trigger,
127 };
Takashi Iwai57eb6792017-01-05 17:29:54 +0100128 static const struct snd_rawmidi_ops playback_ops = {
Takashi Sakamoto7cdc8872017-01-05 21:48:08 +0900129 .open = midi_playback_open,
130 .close = midi_playback_close,
131 .trigger = midi_playback_trigger,
132 };
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +0900133 struct snd_rawmidi *rmidi;
134 struct snd_rawmidi_str *str;
135 int err;
136
137 /* create midi ports */
138 err = snd_rawmidi_new(efw->card, efw->card->driver, 0,
139 efw->midi_out_ports, efw->midi_in_ports,
140 &rmidi);
141 if (err < 0)
142 return err;
143
144 snprintf(rmidi->name, sizeof(rmidi->name),
145 "%s MIDI", efw->card->shortname);
146 rmidi->private_data = efw;
147
148 if (efw->midi_in_ports > 0) {
149 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
150
151 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
Takashi Sakamoto7cdc8872017-01-05 21:48:08 +0900152 &capture_ops);
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +0900153
154 str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT];
155
156 set_midi_substream_names(efw, str);
157 }
158
159 if (efw->midi_out_ports > 0) {
160 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
161
162 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
Takashi Sakamoto7cdc8872017-01-05 21:48:08 +0900163 &playback_ops);
Takashi Sakamotoa63d3ff2014-04-25 22:45:09 +0900164
165 str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT];
166
167 set_midi_substream_names(efw, str);
168 }
169
170 if ((efw->midi_out_ports > 0) && (efw->midi_in_ports > 0))
171 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX;
172
173 return 0;
174}