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