• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
midi_open(struct snd_rawmidi_substream * substream)10 static 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 
midi_close(struct snd_rawmidi_substream * substream)32 static 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 
midi_capture_trigger(struct snd_rawmidi_substream * substrm,int up)47 static 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)
55 		amdtp_am824_midi_trigger(&dice->tx_stream[0],
56 					  substrm->number, substrm);
57 	else
58 		amdtp_am824_midi_trigger(&dice->tx_stream[0],
59 					  substrm->number, NULL);
60 
61 	spin_unlock_irqrestore(&dice->lock, flags);
62 }
63 
midi_playback_trigger(struct snd_rawmidi_substream * substrm,int up)64 static 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)
72 		amdtp_am824_midi_trigger(&dice->rx_stream[0],
73 					 substrm->number, substrm);
74 	else
75 		amdtp_am824_midi_trigger(&dice->rx_stream[0],
76 					 substrm->number, NULL);
77 
78 	spin_unlock_irqrestore(&dice->lock, flags);
79 }
80 
81 static struct snd_rawmidi_ops capture_ops = {
82 	.open		= midi_open,
83 	.close		= midi_close,
84 	.trigger	= midi_capture_trigger,
85 };
86 
87 static struct snd_rawmidi_ops playback_ops = {
88 	.open		= midi_open,
89 	.close		= midi_close,
90 	.trigger	= midi_playback_trigger,
91 };
92 
set_midi_substream_names(struct snd_dice * dice,struct snd_rawmidi_str * str)93 static void set_midi_substream_names(struct snd_dice *dice,
94 				     struct snd_rawmidi_str *str)
95 {
96 	struct snd_rawmidi_substream *subs;
97 
98 	list_for_each_entry(subs, &str->substreams, list) {
99 		snprintf(subs->name, sizeof(subs->name),
100 			 "%s MIDI %d", dice->card->shortname, subs->number + 1);
101 	}
102 }
103 
snd_dice_create_midi(struct snd_dice * dice)104 int snd_dice_create_midi(struct snd_dice *dice)
105 {
106 	__be32 reg;
107 	struct snd_rawmidi *rmidi;
108 	struct snd_rawmidi_str *str;
109 	unsigned int midi_in_ports, midi_out_ports;
110 	int err;
111 
112 	/*
113 	 * Use the number of MIDI conformant data channel at current sampling
114 	 * transfer frequency.
115 	 */
116 	err = snd_dice_transaction_read_tx(dice, TX_NUMBER_MIDI,
117 					   &reg, sizeof(reg));
118 	if (err < 0)
119 		return err;
120 	midi_in_ports = be32_to_cpu(reg);
121 
122 	err = snd_dice_transaction_read_rx(dice, RX_NUMBER_MIDI,
123 					   &reg, sizeof(reg));
124 	if (err < 0)
125 		return err;
126 	midi_out_ports = be32_to_cpu(reg);
127 
128 	if (midi_in_ports + midi_out_ports == 0)
129 		return 0;
130 
131 	/* create midi ports */
132 	err = snd_rawmidi_new(dice->card, dice->card->driver, 0,
133 			      midi_out_ports, midi_in_ports,
134 			      &rmidi);
135 	if (err < 0)
136 		return err;
137 
138 	snprintf(rmidi->name, sizeof(rmidi->name),
139 		 "%s MIDI", dice->card->shortname);
140 	rmidi->private_data = dice;
141 
142 	if (midi_in_ports > 0) {
143 		rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
144 
145 		snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
146 				    &capture_ops);
147 
148 		str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT];
149 
150 		set_midi_substream_names(dice, str);
151 	}
152 
153 	if (midi_out_ports > 0) {
154 		rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
155 
156 		snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
157 				    &playback_ops);
158 
159 		str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT];
160 
161 		set_midi_substream_names(dice, str);
162 	}
163 
164 	if ((midi_out_ports > 0) && (midi_in_ports > 0))
165 		rmidi->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX;
166 
167 	return 0;
168 }
169