2 * bebob_midi.c - a part of driver for BeBoB based devices
4 * Copyright (c) 2013-2014 Takashi Sakamoto
6 * Licensed under the terms of the GNU General Public License, version 2.
11 static int midi_capture_open(struct snd_rawmidi_substream
*substream
)
13 struct snd_bebob
*bebob
= substream
->rmidi
->private_data
;
16 err
= snd_bebob_stream_lock_try(bebob
);
20 mutex_lock(&bebob
->mutex
);
21 bebob
->substreams_counter
++;
22 err
= snd_bebob_stream_start_duplex(bebob
, 0);
23 mutex_unlock(&bebob
->mutex
);
25 snd_bebob_stream_lock_release(bebob
);
30 static int midi_playback_open(struct snd_rawmidi_substream
*substream
)
32 struct snd_bebob
*bebob
= substream
->rmidi
->private_data
;
35 err
= snd_bebob_stream_lock_try(bebob
);
39 mutex_lock(&bebob
->mutex
);
40 bebob
->substreams_counter
++;
41 err
= snd_bebob_stream_start_duplex(bebob
, 0);
42 mutex_unlock(&bebob
->mutex
);
44 snd_bebob_stream_lock_release(bebob
);
49 static int midi_capture_close(struct snd_rawmidi_substream
*substream
)
51 struct snd_bebob
*bebob
= substream
->rmidi
->private_data
;
53 mutex_lock(&bebob
->mutex
);
54 bebob
->substreams_counter
--;
55 snd_bebob_stream_stop_duplex(bebob
);
56 mutex_unlock(&bebob
->mutex
);
58 snd_bebob_stream_lock_release(bebob
);
62 static int midi_playback_close(struct snd_rawmidi_substream
*substream
)
64 struct snd_bebob
*bebob
= substream
->rmidi
->private_data
;
66 mutex_lock(&bebob
->mutex
);
67 bebob
->substreams_counter
--;
68 snd_bebob_stream_stop_duplex(bebob
);
69 mutex_unlock(&bebob
->mutex
);
71 snd_bebob_stream_lock_release(bebob
);
75 static void midi_capture_trigger(struct snd_rawmidi_substream
*substrm
, int up
)
77 struct snd_bebob
*bebob
= substrm
->rmidi
->private_data
;
80 spin_lock_irqsave(&bebob
->lock
, flags
);
83 amdtp_am824_midi_trigger(&bebob
->tx_stream
,
84 substrm
->number
, substrm
);
86 amdtp_am824_midi_trigger(&bebob
->tx_stream
,
87 substrm
->number
, NULL
);
89 spin_unlock_irqrestore(&bebob
->lock
, flags
);
92 static void midi_playback_trigger(struct snd_rawmidi_substream
*substrm
, int up
)
94 struct snd_bebob
*bebob
= substrm
->rmidi
->private_data
;
97 spin_lock_irqsave(&bebob
->lock
, flags
);
100 amdtp_am824_midi_trigger(&bebob
->rx_stream
,
101 substrm
->number
, substrm
);
103 amdtp_am824_midi_trigger(&bebob
->rx_stream
,
104 substrm
->number
, NULL
);
106 spin_unlock_irqrestore(&bebob
->lock
, flags
);
109 static struct snd_rawmidi_ops midi_capture_ops
= {
110 .open
= midi_capture_open
,
111 .close
= midi_capture_close
,
112 .trigger
= midi_capture_trigger
,
115 static struct snd_rawmidi_ops midi_playback_ops
= {
116 .open
= midi_playback_open
,
117 .close
= midi_playback_close
,
118 .trigger
= midi_playback_trigger
,
121 static void set_midi_substream_names(struct snd_bebob
*bebob
,
122 struct snd_rawmidi_str
*str
)
124 struct snd_rawmidi_substream
*subs
;
126 list_for_each_entry(subs
, &str
->substreams
, list
) {
127 snprintf(subs
->name
, sizeof(subs
->name
),
129 bebob
->card
->shortname
, subs
->number
+ 1);
133 int snd_bebob_create_midi_devices(struct snd_bebob
*bebob
)
135 struct snd_rawmidi
*rmidi
;
136 struct snd_rawmidi_str
*str
;
139 /* create midi ports */
140 err
= snd_rawmidi_new(bebob
->card
, bebob
->card
->driver
, 0,
141 bebob
->midi_output_ports
, bebob
->midi_input_ports
,
146 snprintf(rmidi
->name
, sizeof(rmidi
->name
),
147 "%s MIDI", bebob
->card
->shortname
);
148 rmidi
->private_data
= bebob
;
150 if (bebob
->midi_input_ports
> 0) {
151 rmidi
->info_flags
|= SNDRV_RAWMIDI_INFO_INPUT
;
153 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
,
156 str
= &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_INPUT
];
158 set_midi_substream_names(bebob
, str
);
161 if (bebob
->midi_output_ports
> 0) {
162 rmidi
->info_flags
|= SNDRV_RAWMIDI_INFO_OUTPUT
;
164 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
,
167 str
= &rmidi
->streams
[SNDRV_RAWMIDI_STREAM_OUTPUT
];
169 set_midi_substream_names(bebob
, str
);
172 if ((bebob
->midi_output_ports
> 0) && (bebob
->midi_input_ports
> 0))
173 rmidi
->info_flags
|= SNDRV_RAWMIDI_INFO_DUPLEX
;