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 87 int snd_motu_register_dsp_message_parser_new(struct snd_motu *motu) 88 { 89 struct msg_parser *parser; 90 parser = devm_kzalloc(&motu->card->card_dev, sizeof(*parser), GFP_KERNEL); 91 if (!parser) 92 return -ENOMEM; 93 spin_lock_init(&parser->lock); 94 if (motu->spec == &snd_motu_spec_4pre || motu->spec == &snd_motu_spec_audio_express) 95 parser->meter_pos_quirk = true; 96 motu->message_parser = parser; 97 return 0; 98 } 99 100 int snd_motu_register_dsp_message_parser_init(struct snd_motu *motu) 101 { 102 return 0; 103 } 104 105 void snd_motu_register_dsp_message_parser_parse(struct snd_motu *motu, const struct pkt_desc *descs, 106 unsigned int desc_count, unsigned int data_block_quadlets) 107 { 108 struct msg_parser *parser = motu->message_parser; 109 bool meter_pos_quirk = parser->meter_pos_quirk; 110 unsigned long flags; 111 int i; 112 113 spin_lock_irqsave(&parser->lock, flags); 114 115 for (i = 0; i < desc_count; ++i) { 116 const struct pkt_desc *desc = descs + i; 117 __be32 *buffer = desc->ctx_payload; 118 unsigned int data_blocks = desc->data_blocks; 119 int j; 120 121 for (j = 0; j < data_blocks; ++j) { 122 u8 *b = (u8 *)buffer; 123 u8 msg_type = (b[MSG_FLAG_POS] & MSG_FLAG_TYPE_MASK) >> MSG_FLAG_TYPE_SHIFT; 124 u8 val = b[MSG_VALUE_POS]; 125 126 buffer += data_block_quadlets; 127 128 switch (msg_type) { 129 case METER: 130 { 131 u8 pos; 132 133 if (!meter_pos_quirk) 134 pos = b[MSG_METER_IDX_POS]; 135 else 136 pos = b[MSG_METER_IDX_POS_4PRE_AE]; 137 138 if (pos < 0x80) 139 pos &= 0x1f; 140 else 141 pos = (pos & 0x1f) + 20; 142 parser->meter.data[pos] = val; 143 break; 144 } 145 default: 146 break; 147 } 148 } 149 } 150 151 spin_unlock_irqrestore(&parser->lock, flags); 152 } 153 154 void snd_motu_register_dsp_message_parser_copy_meter(struct snd_motu *motu, 155 struct snd_firewire_motu_register_dsp_meter *meter) 156 { 157 struct msg_parser *parser = motu->message_parser; 158 unsigned long flags; 159 160 spin_lock_irqsave(&parser->lock, flags); 161 memcpy(meter, &parser->meter, sizeof(*meter)); 162 spin_unlock_irqrestore(&parser->lock, flags); 163 } 164