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_PAIRED_ASSIGNMENT = 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 #define EVENT_QUEUE_SIZE 16 82 83 struct msg_parser { 84 spinlock_t lock; 85 struct snd_firewire_motu_register_dsp_meter meter; 86 bool meter_pos_quirk; 87 88 struct snd_firewire_motu_register_dsp_parameter param; 89 u8 prev_mixer_src_type; 90 u8 mixer_ch; 91 u8 mixer_src_ch; 92 93 u8 input_ch; 94 u8 prev_msg_type; 95 96 u32 event_queue[EVENT_QUEUE_SIZE]; 97 unsigned int push_pos; 98 unsigned int pull_pos; 99 }; 100 101 int snd_motu_register_dsp_message_parser_new(struct snd_motu *motu) 102 { 103 struct msg_parser *parser; 104 parser = devm_kzalloc(&motu->card->card_dev, sizeof(*parser), GFP_KERNEL); 105 if (!parser) 106 return -ENOMEM; 107 spin_lock_init(&parser->lock); 108 if (motu->spec == &snd_motu_spec_4pre || motu->spec == &snd_motu_spec_audio_express) 109 parser->meter_pos_quirk = true; 110 motu->message_parser = parser; 111 return 0; 112 } 113 114 int snd_motu_register_dsp_message_parser_init(struct snd_motu *motu) 115 { 116 struct msg_parser *parser = motu->message_parser; 117 118 parser->prev_mixer_src_type = INVALID; 119 parser->mixer_ch = 0xff; 120 parser->mixer_src_ch = 0xff; 121 parser->prev_msg_type = INVALID; 122 123 return 0; 124 } 125 126 // Rough implementaion of queue without overrun check. 127 static void queue_event(struct snd_motu *motu, u8 msg_type, u8 identifier0, u8 identifier1, u8 val) 128 { 129 struct msg_parser *parser = motu->message_parser; 130 unsigned int pos = parser->push_pos; 131 u32 entry; 132 133 if (!motu->hwdep || motu->hwdep->used == 0) 134 return; 135 136 entry = (msg_type << 24) | (identifier0 << 16) | (identifier1 << 8) | val; 137 parser->event_queue[pos] = entry; 138 139 ++pos; 140 if (pos >= EVENT_QUEUE_SIZE) 141 pos = 0; 142 parser->push_pos = pos; 143 } 144 145 void snd_motu_register_dsp_message_parser_parse(const struct amdtp_stream *s, 146 const struct pkt_desc *desc, unsigned int count) 147 { 148 struct snd_motu *motu = container_of(s, struct snd_motu, tx_stream); 149 unsigned int data_block_quadlets = s->data_block_quadlets; 150 struct msg_parser *parser = motu->message_parser; 151 bool meter_pos_quirk = parser->meter_pos_quirk; 152 unsigned int pos = parser->push_pos; 153 int i; 154 155 guard(spinlock_irqsave)(&parser->lock); 156 157 for (i = 0; i < count; ++i) { 158 __be32 *buffer = desc->ctx_payload; 159 unsigned int data_blocks = desc->data_blocks; 160 int j; 161 162 desc = amdtp_stream_next_packet_desc(s, desc); 163 164 for (j = 0; j < data_blocks; ++j) { 165 u8 *b = (u8 *)buffer; 166 u8 msg_type = (b[MSG_FLAG_POS] & MSG_FLAG_TYPE_MASK) >> MSG_FLAG_TYPE_SHIFT; 167 u8 val = b[MSG_VALUE_POS]; 168 169 buffer += data_block_quadlets; 170 171 switch (msg_type) { 172 case MIXER_SELECT: 173 { 174 u8 mixer_ch = val / 0x20; 175 if (mixer_ch < SNDRV_FIREWIRE_MOTU_REGISTER_DSP_MIXER_COUNT) { 176 parser->mixer_src_ch = 0; 177 parser->mixer_ch = mixer_ch; 178 } 179 break; 180 } 181 case MIXER_SRC_GAIN: 182 case MIXER_SRC_PAN: 183 case MIXER_SRC_FLAG: 184 case MIXER_SRC_PAIRED_BALANCE: 185 case MIXER_SRC_PAIRED_WIDTH: 186 { 187 struct snd_firewire_motu_register_dsp_parameter *param = &parser->param; 188 u8 mixer_ch = parser->mixer_ch; 189 u8 mixer_src_ch = parser->mixer_src_ch; 190 191 if (msg_type != parser->prev_mixer_src_type) 192 mixer_src_ch = 0; 193 else 194 ++mixer_src_ch; 195 parser->prev_mixer_src_type = msg_type; 196 197 if (mixer_ch < SNDRV_FIREWIRE_MOTU_REGISTER_DSP_MIXER_COUNT && 198 mixer_src_ch < SNDRV_FIREWIRE_MOTU_REGISTER_DSP_MIXER_SRC_COUNT) { 199 u8 mixer_ch = parser->mixer_ch; 200 201 switch (msg_type) { 202 case MIXER_SRC_GAIN: 203 if (param->mixer.source[mixer_ch].gain[mixer_src_ch] != val) { 204 queue_event(motu, msg_type, mixer_ch, mixer_src_ch, val); 205 param->mixer.source[mixer_ch].gain[mixer_src_ch] = val; 206 } 207 break; 208 case MIXER_SRC_PAN: 209 if (param->mixer.source[mixer_ch].pan[mixer_src_ch] != val) { 210 queue_event(motu, msg_type, mixer_ch, mixer_src_ch, val); 211 param->mixer.source[mixer_ch].pan[mixer_src_ch] = val; 212 } 213 break; 214 case MIXER_SRC_FLAG: 215 if (param->mixer.source[mixer_ch].flag[mixer_src_ch] != val) { 216 queue_event(motu, msg_type, mixer_ch, mixer_src_ch, val); 217 param->mixer.source[mixer_ch].flag[mixer_src_ch] = val; 218 } 219 break; 220 case MIXER_SRC_PAIRED_BALANCE: 221 if (param->mixer.source[mixer_ch].paired_balance[mixer_src_ch] != val) { 222 queue_event(motu, msg_type, mixer_ch, mixer_src_ch, val); 223 param->mixer.source[mixer_ch].paired_balance[mixer_src_ch] = val; 224 } 225 break; 226 case MIXER_SRC_PAIRED_WIDTH: 227 if (param->mixer.source[mixer_ch].paired_width[mixer_src_ch] != val) { 228 queue_event(motu, msg_type, mixer_ch, mixer_src_ch, val); 229 param->mixer.source[mixer_ch].paired_width[mixer_src_ch] = val; 230 } 231 break; 232 default: 233 break; 234 } 235 236 parser->mixer_src_ch = mixer_src_ch; 237 } 238 break; 239 } 240 case MIXER_OUTPUT_PAIRED_VOLUME: 241 case MIXER_OUTPUT_PAIRED_FLAG: 242 { 243 struct snd_firewire_motu_register_dsp_parameter *param = &parser->param; 244 u8 mixer_ch = parser->mixer_ch; 245 246 if (mixer_ch < SNDRV_FIREWIRE_MOTU_REGISTER_DSP_MIXER_COUNT) { 247 switch (msg_type) { 248 case MIXER_OUTPUT_PAIRED_VOLUME: 249 if (param->mixer.output.paired_volume[mixer_ch] != val) { 250 queue_event(motu, msg_type, mixer_ch, 0, val); 251 param->mixer.output.paired_volume[mixer_ch] = val; 252 } 253 break; 254 case MIXER_OUTPUT_PAIRED_FLAG: 255 if (param->mixer.output.paired_flag[mixer_ch] != val) { 256 queue_event(motu, msg_type, mixer_ch, 0, val); 257 param->mixer.output.paired_flag[mixer_ch] = val; 258 } 259 break; 260 default: 261 break; 262 } 263 } 264 break; 265 } 266 case MAIN_OUTPUT_PAIRED_VOLUME: 267 if (parser->param.output.main_paired_volume != val) { 268 queue_event(motu, msg_type, 0, 0, val); 269 parser->param.output.main_paired_volume = val; 270 } 271 break; 272 case HP_OUTPUT_PAIRED_VOLUME: 273 if (parser->param.output.hp_paired_volume != val) { 274 queue_event(motu, msg_type, 0, 0, val); 275 parser->param.output.hp_paired_volume = val; 276 } 277 break; 278 case HP_OUTPUT_PAIRED_ASSIGNMENT: 279 if (parser->param.output.hp_paired_assignment != val) { 280 queue_event(motu, msg_type, 0, 0, val); 281 parser->param.output.hp_paired_assignment = val; 282 } 283 break; 284 case LINE_INPUT_BOOST: 285 if (parser->param.line_input.boost_flag != val) { 286 queue_event(motu, msg_type, 0, 0, val); 287 parser->param.line_input.boost_flag = val; 288 } 289 break; 290 case LINE_INPUT_NOMINAL_LEVEL: 291 if (parser->param.line_input.nominal_level_flag != val) { 292 queue_event(motu, msg_type, 0, 0, val); 293 parser->param.line_input.nominal_level_flag = val; 294 } 295 break; 296 case INPUT_GAIN_AND_INVERT: 297 case INPUT_FLAG: 298 { 299 struct snd_firewire_motu_register_dsp_parameter *param = &parser->param; 300 u8 input_ch = parser->input_ch; 301 302 if (parser->prev_msg_type != msg_type) 303 input_ch = 0; 304 else 305 ++input_ch; 306 307 if (input_ch < SNDRV_FIREWIRE_MOTU_REGISTER_DSP_INPUT_COUNT) { 308 switch (msg_type) { 309 case INPUT_GAIN_AND_INVERT: 310 if (param->input.gain_and_invert[input_ch] != val) { 311 queue_event(motu, msg_type, input_ch, 0, val); 312 param->input.gain_and_invert[input_ch] = val; 313 } 314 break; 315 case INPUT_FLAG: 316 if (param->input.flag[input_ch] != val) { 317 queue_event(motu, msg_type, input_ch, 0, val); 318 param->input.flag[input_ch] = val; 319 } 320 break; 321 default: 322 break; 323 } 324 parser->input_ch = input_ch; 325 } 326 break; 327 } 328 case UNKNOWN_0: 329 case UNKNOWN_2: 330 break; 331 case METER: 332 { 333 u8 pos; 334 335 if (!meter_pos_quirk) 336 pos = b[MSG_METER_IDX_POS]; 337 else 338 pos = b[MSG_METER_IDX_POS_4PRE_AE]; 339 340 if (pos < SNDRV_FIREWIRE_MOTU_REGISTER_DSP_METER_INPUT_COUNT) { 341 parser->meter.data[pos] = val; 342 } else if (pos >= 0x80) { 343 pos -= (0x80 - SNDRV_FIREWIRE_MOTU_REGISTER_DSP_METER_INPUT_COUNT); 344 345 if (pos < SNDRV_FIREWIRE_MOTU_REGISTER_DSP_METER_COUNT) 346 parser->meter.data[pos] = val; 347 } 348 349 // The message for meter is interruptible to the series of other 350 // types of messages. Don't cache it. 351 fallthrough; 352 } 353 case INVALID: 354 default: 355 // Don't cache it. 356 continue; 357 } 358 359 parser->prev_msg_type = msg_type; 360 } 361 } 362 363 if (pos != parser->push_pos) 364 wake_up(&motu->hwdep_wait); 365 } 366 367 void snd_motu_register_dsp_message_parser_copy_meter(struct snd_motu *motu, 368 struct snd_firewire_motu_register_dsp_meter *meter) 369 { 370 struct msg_parser *parser = motu->message_parser; 371 372 guard(spinlock_irqsave)(&parser->lock); 373 memcpy(meter, &parser->meter, sizeof(*meter)); 374 } 375 376 void snd_motu_register_dsp_message_parser_copy_parameter(struct snd_motu *motu, 377 struct snd_firewire_motu_register_dsp_parameter *param) 378 { 379 struct msg_parser *parser = motu->message_parser; 380 381 guard(spinlock_irqsave)(&parser->lock); 382 memcpy(param, &parser->param, sizeof(*param)); 383 } 384 385 unsigned int snd_motu_register_dsp_message_parser_count_event(struct snd_motu *motu) 386 { 387 struct msg_parser *parser = motu->message_parser; 388 389 if (parser->pull_pos > parser->push_pos) 390 return EVENT_QUEUE_SIZE - parser->pull_pos + parser->push_pos; 391 else 392 return parser->push_pos - parser->pull_pos; 393 } 394 395 bool snd_motu_register_dsp_message_parser_copy_event(struct snd_motu *motu, u32 *event) 396 { 397 struct msg_parser *parser = motu->message_parser; 398 unsigned int pos = parser->pull_pos; 399 400 if (pos == parser->push_pos) 401 return false; 402 403 guard(spinlock_irqsave)(&parser->lock); 404 405 *event = parser->event_queue[pos]; 406 407 ++pos; 408 if (pos >= EVENT_QUEUE_SIZE) 409 pos = 0; 410 parser->pull_pos = pos; 411 412 return true; 413 } 414