blob: 8ff6da3c51f72c1a3b6d0b0a671eabe43018e5d3 [file] [log] [blame]
Takashi Sakamotoa113ff82014-12-09 00:10:39 +09001/*
2 * dice_midi.c - a part of driver for Dice based devices
3 *
4 * Copyright (c) 2014 Takashi Sakamoto
5 *
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
8#include "dice.h"
9
10static int midi_open(struct snd_rawmidi_substream *substream)
11{
12 struct snd_dice *dice = substream->rmidi->private_data;
13 int err;
14
15 err = snd_dice_stream_lock_try(dice);
16 if (err < 0)
17 return err;
18
19 mutex_lock(&dice->mutex);
20
21 dice->substreams_counter++;
22 err = snd_dice_stream_start_duplex(dice, 0);
23
24 mutex_unlock(&dice->mutex);
25
26 if (err < 0)
27 snd_dice_stream_lock_release(dice);
28
29 return err;
30}
31
32static int midi_close(struct snd_rawmidi_substream *substream)
33{
34 struct snd_dice *dice = substream->rmidi->private_data;
35
36 mutex_lock(&dice->mutex);
37
38 dice->substreams_counter--;
39 snd_dice_stream_stop_duplex(dice);
40
41 mutex_unlock(&dice->mutex);
42
43 snd_dice_stream_lock_release(dice);
44 return 0;
45}
46
47static void midi_capture_trigger(struct snd_rawmidi_substream *substrm, int up)
48{
49 struct snd_dice *dice = substrm->rmidi->private_data;
50 unsigned long flags;
51
52 spin_lock_irqsave(&dice->lock, flags);
53
54 if (up)
Takashi Sakamoto8ae25b762016-03-07 22:35:42 +090055 amdtp_am824_midi_trigger(&dice->tx_stream[0],
Takashi Sakamotoa113ff82014-12-09 00:10:39 +090056 substrm->number, substrm);
57 else
Takashi Sakamoto8ae25b762016-03-07 22:35:42 +090058 amdtp_am824_midi_trigger(&dice->tx_stream[0],
Takashi Sakamotoa113ff82014-12-09 00:10:39 +090059 substrm->number, NULL);
60
61 spin_unlock_irqrestore(&dice->lock, flags);
62}
63
64static void midi_playback_trigger(struct snd_rawmidi_substream *substrm, int up)
65{
66 struct snd_dice *dice = substrm->rmidi->private_data;
67 unsigned long flags;
68
69 spin_lock_irqsave(&dice->lock, flags);
70
71 if (up)
Takashi Sakamoto8ae25b762016-03-07 22:35:42 +090072 amdtp_am824_midi_trigger(&dice->rx_stream[0],
Takashi Sakamoto03e2a672015-09-19 11:21:59 +090073 substrm->number, substrm);
Takashi Sakamotoa113ff82014-12-09 00:10:39 +090074 else
Takashi Sakamoto8ae25b762016-03-07 22:35:42 +090075 amdtp_am824_midi_trigger(&dice->rx_stream[0],
Takashi Sakamoto03e2a672015-09-19 11:21:59 +090076 substrm->number, NULL);
Takashi Sakamotoa113ff82014-12-09 00:10:39 +090077
78 spin_unlock_irqrestore(&dice->lock, flags);
79}
80
Takashi Sakamotoa113ff82014-12-09 00:10:39 +090081static void set_midi_substream_names(struct snd_dice *dice,
82 struct snd_rawmidi_str *str)
83{
84 struct snd_rawmidi_substream *subs;
85
86 list_for_each_entry(subs, &str->substreams, list) {
87 snprintf(subs->name, sizeof(subs->name),
88 "%s MIDI %d", dice->card->shortname, subs->number + 1);
89 }
90}
91
92int snd_dice_create_midi(struct snd_dice *dice)
93{
Takashi Iwai57eb6792017-01-05 17:29:54 +010094 static const struct snd_rawmidi_ops capture_ops = {
Takashi Sakamotofcbe08d42017-01-05 21:48:10 +090095 .open = midi_open,
96 .close = midi_close,
97 .trigger = midi_capture_trigger,
98 };
Takashi Iwai57eb6792017-01-05 17:29:54 +010099 static const struct snd_rawmidi_ops playback_ops = {
Takashi Sakamotofcbe08d42017-01-05 21:48:10 +0900100 .open = midi_open,
101 .close = midi_close,
102 .trigger = midi_playback_trigger,
103 };
Takashi Sakamotob9022f42016-02-08 22:54:17 +0900104 __be32 reg;
Takashi Sakamotoa113ff82014-12-09 00:10:39 +0900105 struct snd_rawmidi *rmidi;
106 struct snd_rawmidi_str *str;
Takashi Sakamotob9022f42016-02-08 22:54:17 +0900107 unsigned int midi_in_ports, midi_out_ports;
Takashi Sakamotoa113ff82014-12-09 00:10:39 +0900108 int err;
109
Takashi Sakamotob9022f42016-02-08 22:54:17 +0900110 /*
111 * Use the number of MIDI conformant data channel at current sampling
112 * transfer frequency.
113 */
114 err = snd_dice_transaction_read_tx(dice, TX_NUMBER_MIDI,
115 &reg, sizeof(reg));
116 if (err < 0)
117 return err;
118 midi_in_ports = be32_to_cpu(reg);
119
120 err = snd_dice_transaction_read_rx(dice, RX_NUMBER_MIDI,
121 &reg, sizeof(reg));
122 if (err < 0)
123 return err;
124 midi_out_ports = be32_to_cpu(reg);
Takashi Sakamotoa113ff82014-12-09 00:10:39 +0900125
126 if (midi_in_ports + midi_out_ports == 0)
127 return 0;
128
129 /* create midi ports */
130 err = snd_rawmidi_new(dice->card, dice->card->driver, 0,
131 midi_out_ports, midi_in_ports,
132 &rmidi);
133 if (err < 0)
134 return err;
135
136 snprintf(rmidi->name, sizeof(rmidi->name),
137 "%s MIDI", dice->card->shortname);
138 rmidi->private_data = dice;
139
140 if (midi_in_ports > 0) {
141 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
142
143 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
144 &capture_ops);
145
146 str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT];
147
148 set_midi_substream_names(dice, str);
149 }
150
151 if (midi_out_ports > 0) {
152 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
153
154 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
155 &playback_ops);
156
157 str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT];
158
159 set_midi_substream_names(dice, str);
160 }
161
162 if ((midi_out_ports > 0) && (midi_in_ports > 0))
163 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX;
164
165 return 0;
166}