1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // motu-register-dsp-message-parser.c - a part of driver for MOTU FireWire series
4 //
5 // Copyright (c) 2021 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6 
7 // Below models allow software to configure their DSP functions by asynchronous transaction
8 // to access their internal registers.
9 // * 828 mk2
10 // * 896hd
11 // * Traveler
12 // * 8 pre
13 // * Ultralite
14 // * 4 pre
15 // * Audio Express
16 //
17 // Additionally, isochronous packets from the above models include messages to notify state of
18 // DSP. The messages are two set of 3 byte data in 2nd and 3rd quadlet of data block. When user
19 // operates hardware components such as dial and switch, corresponding messages are transferred.
20 // The messages include Hardware metering and MIDI messages as well.
21 
22 #include "motu.h"
23 
24 #define MSG_FLAG_POS                    4
25 #define MSG_FLAG_TYPE_MASK              0xf8
26 #define MSG_FLAG_MIDI_MASK              0x01
27 #define MSG_FLAG_MODEL_SPECIFIC_MASK    0x06
28 #define   MSG_FLAG_8PRE                 0x00
29 #define   MSG_FLAG_ULTRALITE            0x04
30 #define   MSG_FLAG_TRAVELER             0x04
31 #define   MSG_FLAG_828MK2               0x04
32 #define   MSG_FLAG_896HD                0x04
33 #define   MSG_FLAG_4PRE                 0x05 // MIDI mask is in 8th byte.
34 #define   MSG_FLAG_AUDIOEXPRESS         0x05 // MIDI mask is in 8th byte.
35 #define MSG_FLAG_TYPE_SHIFT             3
36 #define MSG_VALUE_POS                   5
37 #define MSG_MIDI_BYTE_POS		6
38 #define MSG_METER_IDX_POS               7
39 
40 // In 4 pre and Audio express, meter index is in 6th byte. MIDI flag is in 8th byte and MIDI byte
41 // is in 7th byte.
42 #define MSG_METER_IDX_POS_4PRE_AE	6
43 #define MSG_MIDI_BYTE_POS_4PRE_AE	7
44 #define MSG_FLAG_MIDI_POS_4PRE_AE	8
45 
46 enum register_dsp_msg_type {
47 	// Used for messages with no information.
48 	INVALID = 0x00,
49 	MIXER_SELECT = 0x01,
50 	MIXER_SRC_GAIN = 0x02,
51 	MIXER_SRC_PAN = 0x03,
52 	MIXER_SRC_FLAG = 0x04,
53 	MIXER_OUTPUT_PAIRED_VOLUME = 0x05,
54 	MIXER_OUTPUT_PAIRED_FLAG = 0x06,
55 	MAIN_OUTPUT_PAIRED_VOLUME = 0x07,
56 	HP_OUTPUT_PAIRED_VOLUME = 0x08,
57 	HP_OUTPUT_ASSIGN = 0x09,
58 	// Transferred by all models but the purpose is still unknown.
59 	UNKNOWN_0 = 0x0a,
60 	// Specific to 828mk2, 896hd, Traveler.
61 	UNKNOWN_2 = 0x0c,
62 	// Specific to 828mk2, Traveler, and 896hd (not functional).
63 	LINE_INPUT_BOOST = 0x0d,
64 	// Specific to 828mk2, Traveler, and 896hd (not functional).
65 	LINE_INPUT_NOMINAL_LEVEL = 0x0e,
66 	// Specific to Ultralite, 4 pre, Audio express, and 8 pre (not functional).
67 	INPUT_GAIN_AND_INVERT = 0x15,
68 	// Specific to 4 pre, and Audio express.
69 	INPUT_FLAG = 0x16,
70 	// Specific to 4 pre, and Audio express.
71 	MIXER_SRC_PAIRED_BALANCE = 0x17,
72 	// Specific to 4 pre, and Audio express.
73 	MIXER_SRC_PAIRED_WIDTH = 0x18,
74 	// Transferred by all models. This type of message interposes the series of the other
75 	// messages. The message delivers signal level up to 96.0 kHz. In 828mk2, 896hd, and
76 	// Traveler, one of physical outputs is selected for the message. The selection is done
77 	// by LSB one byte in asynchronous write quadlet transaction to 0x'ffff'f000'0b2c.
78 	METER = 0x1f,
79 };
80 
81 struct msg_parser {
82 	spinlock_t lock;
83 	struct snd_firewire_motu_register_dsp_meter meter;
84 	bool meter_pos_quirk;
85 
86 	struct snd_firewire_motu_register_dsp_parameter param;
87 	u8 prev_mixer_src_type;
88 	u8 mixer_ch;
89 	u8 mixer_src_ch;
90 };
91 
92 int snd_motu_register_dsp_message_parser_new(struct snd_motu *motu)
93 {
94 	struct msg_parser *parser;
95 	parser = devm_kzalloc(&motu->card->card_dev, sizeof(*parser), GFP_KERNEL);
96 	if (!parser)
97 		return -ENOMEM;
98 	spin_lock_init(&parser->lock);
99 	if (motu->spec == &snd_motu_spec_4pre || motu->spec == &snd_motu_spec_audio_express)
100 		parser->meter_pos_quirk = true;
101 	motu->message_parser = parser;
102 	return 0;
103 }
104 
105 int snd_motu_register_dsp_message_parser_init(struct snd_motu *motu)
106 {
107 	struct msg_parser *parser = motu->message_parser;
108 
109 	parser->prev_mixer_src_type = INVALID;
110 	parser->mixer_ch = 0xff;
111 	parser->mixer_src_ch = 0xff;
112 
113 	return 0;
114 }
115 
116 void snd_motu_register_dsp_message_parser_parse(struct snd_motu *motu, const struct pkt_desc *descs,
117 					unsigned int desc_count, unsigned int data_block_quadlets)
118 {
119 	struct msg_parser *parser = motu->message_parser;
120 	bool meter_pos_quirk = parser->meter_pos_quirk;
121 	unsigned long flags;
122 	int i;
123 
124 	spin_lock_irqsave(&parser->lock, flags);
125 
126 	for (i = 0; i < desc_count; ++i) {
127 		const struct pkt_desc *desc = descs + i;
128 		__be32 *buffer = desc->ctx_payload;
129 		unsigned int data_blocks = desc->data_blocks;
130 		int j;
131 
132 		for (j = 0; j < data_blocks; ++j) {
133 			u8 *b = (u8 *)buffer;
134 			u8 msg_type = (b[MSG_FLAG_POS] & MSG_FLAG_TYPE_MASK) >> MSG_FLAG_TYPE_SHIFT;
135 			u8 val = b[MSG_VALUE_POS];
136 
137 			buffer += data_block_quadlets;
138 
139 			switch (msg_type) {
140 			case MIXER_SELECT:
141 			{
142 				u8 mixer_ch = val / 0x20;
143 				if (mixer_ch < SNDRV_FIREWIRE_MOTU_REGISTER_DSP_MIXER_COUNT) {
144 					parser->mixer_src_ch = 0;
145 					parser->mixer_ch = mixer_ch;
146 				}
147 				break;
148 			}
149 			case MIXER_SRC_GAIN:
150 			case MIXER_SRC_PAN:
151 			case MIXER_SRC_FLAG:
152 			case MIXER_SRC_PAIRED_BALANCE:
153 			case MIXER_SRC_PAIRED_WIDTH:
154 			{
155 				struct snd_firewire_motu_register_dsp_parameter *param = &parser->param;
156 				u8 mixer_ch = parser->mixer_ch;
157 				u8 mixer_src_ch = parser->mixer_src_ch;
158 
159 				if (msg_type != parser->prev_mixer_src_type)
160 					mixer_src_ch = 0;
161 				else
162 					++mixer_src_ch;
163 				parser->prev_mixer_src_type = msg_type;
164 
165 				if (mixer_ch < SNDRV_FIREWIRE_MOTU_REGISTER_DSP_MIXER_COUNT &&
166 				    mixer_src_ch < SNDRV_FIREWIRE_MOTU_REGISTER_DSP_MIXER_SRC_COUNT) {
167 					u8 mixer_ch = parser->mixer_ch;
168 
169 					switch (msg_type) {
170 					case MIXER_SRC_GAIN:
171 						param->mixer.source[mixer_ch].gain[mixer_src_ch] = val;
172 						break;
173 					case MIXER_SRC_PAN:
174 						param->mixer.source[mixer_ch].pan[mixer_src_ch] = val;
175 						break;
176 					case MIXER_SRC_FLAG:
177 						param->mixer.source[mixer_ch].flag[mixer_src_ch] = val;
178 						break;
179 					case MIXER_SRC_PAIRED_BALANCE:
180 						param->mixer.source[mixer_ch].paired_balance[mixer_src_ch] = val;
181 						break;
182 					case MIXER_SRC_PAIRED_WIDTH:
183 						param->mixer.source[mixer_ch].paired_width[mixer_src_ch] = val;
184 						break;
185 					default:
186 						break;
187 					}
188 
189 					parser->mixer_src_ch = mixer_src_ch;
190 				}
191 				break;
192 			}
193 			case MIXER_OUTPUT_PAIRED_VOLUME:
194 			case MIXER_OUTPUT_PAIRED_FLAG:
195 			{
196 				struct snd_firewire_motu_register_dsp_parameter *param = &parser->param;
197 				u8 mixer_ch = parser->mixer_ch;
198 
199 				if (mixer_ch < SNDRV_FIREWIRE_MOTU_REGISTER_DSP_MIXER_COUNT) {
200 					switch (msg_type) {
201 					case MIXER_OUTPUT_PAIRED_VOLUME:
202 						param->mixer.output.paired_volume[mixer_ch] = val;
203 						break;
204 					case MIXER_OUTPUT_PAIRED_FLAG:
205 						param->mixer.output.paired_flag[mixer_ch] = val;
206 						break;
207 					default:
208 						break;
209 					}
210 				}
211 				break;
212 			}
213 			case METER:
214 			{
215 				u8 pos;
216 
217 				if (!meter_pos_quirk)
218 					pos = b[MSG_METER_IDX_POS];
219 				else
220 					pos = b[MSG_METER_IDX_POS_4PRE_AE];
221 
222 				if (pos < 0x80)
223 					pos &= 0x1f;
224 				else
225 					pos = (pos & 0x1f) + 20;
226 				parser->meter.data[pos] = val;
227 				break;
228 			}
229 			default:
230 				break;
231 			}
232 		}
233 	}
234 
235 	spin_unlock_irqrestore(&parser->lock, flags);
236 }
237 
238 void snd_motu_register_dsp_message_parser_copy_meter(struct snd_motu *motu,
239 						struct snd_firewire_motu_register_dsp_meter *meter)
240 {
241 	struct msg_parser *parser = motu->message_parser;
242 	unsigned long flags;
243 
244 	spin_lock_irqsave(&parser->lock, flags);
245 	memcpy(meter, &parser->meter, sizeof(*meter));
246 	spin_unlock_irqrestore(&parser->lock, flags);
247 }
248