xref: /openbmc/linux/sound/firewire/motu/motu.h (revision cddcd5472abb7b8a9c37ccbcf0908b79740a01b5)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * motu.h - a part of driver for MOTU FireWire series
4  *
5  * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6  */
7 
8 #ifndef SOUND_FIREWIRE_MOTU_H_INCLUDED
9 #define SOUND_FIREWIRE_MOTU_H_INCLUDED
10 
11 #include <linux/device.h>
12 #include <linux/firewire.h>
13 #include <linux/firewire-constants.h>
14 #include <linux/module.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18 #include <linux/compat.h>
19 #include <linux/sched/signal.h>
20 
21 #include <sound/control.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/info.h>
25 #include <sound/rawmidi.h>
26 #include <sound/firewire.h>
27 #include <sound/hwdep.h>
28 
29 #include "../lib.h"
30 #include "../amdtp-stream.h"
31 #include "../iso-resources.h"
32 
33 struct snd_motu_packet_format {
34 	unsigned char midi_flag_offset;
35 	unsigned char midi_byte_offset;
36 	unsigned char pcm_byte_offset;
37 
38 	unsigned char msg_chunks;
39 	unsigned char pcm_chunks[3];
40 };
41 
42 struct amdtp_motu_cache {
43 	unsigned int *event_offsets;
44 	unsigned int size;
45 	unsigned int tail;
46 	unsigned int tx_cycle_count;
47 	unsigned int head;
48 	unsigned int rx_cycle_count;
49 };
50 
51 struct snd_motu {
52 	struct snd_card *card;
53 	struct fw_unit *unit;
54 	struct mutex mutex;
55 	spinlock_t lock;
56 
57 	/* Model dependent information. */
58 	const struct snd_motu_spec *spec;
59 
60 	/* For packet streaming */
61 	struct snd_motu_packet_format tx_packet_formats;
62 	struct snd_motu_packet_format rx_packet_formats;
63 	struct amdtp_stream tx_stream;
64 	struct amdtp_stream rx_stream;
65 	struct fw_iso_resources tx_resources;
66 	struct fw_iso_resources rx_resources;
67 	unsigned int substreams_counter;
68 
69 	/* For notification. */
70 	struct fw_address_handler async_handler;
71 	u32 msg;
72 
73 	/* For uapi */
74 	int dev_lock_count;
75 	bool dev_lock_changed;
76 	wait_queue_head_t hwdep_wait;
77 	struct snd_hwdep *hwdep;
78 
79 	struct amdtp_domain domain;
80 
81 	struct amdtp_motu_cache cache;
82 
83 	void *message_parser;
84 };
85 
86 enum snd_motu_spec_flags {
87 	SND_MOTU_SPEC_RX_MIDI_2ND_Q	= 0x0001,
88 	SND_MOTU_SPEC_RX_MIDI_3RD_Q	= 0x0002,
89 	SND_MOTU_SPEC_TX_MIDI_2ND_Q	= 0x0004,
90 	SND_MOTU_SPEC_TX_MIDI_3RD_Q	= 0x0008,
91 	SND_MOTU_SPEC_REGISTER_DSP	= 0x0010,
92 	SND_MOTU_SPEC_COMMAND_DSP	= 0x0020,
93 };
94 
95 #define SND_MOTU_CLOCK_RATE_COUNT	6
96 extern const unsigned int snd_motu_clock_rates[SND_MOTU_CLOCK_RATE_COUNT];
97 
98 enum snd_motu_clock_source {
99 	SND_MOTU_CLOCK_SOURCE_INTERNAL,
100 	SND_MOTU_CLOCK_SOURCE_ADAT_ON_DSUB,
101 	SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT,
102 	SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT_A,
103 	SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT_B,
104 	SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT,
105 	SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT_A,
106 	SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT_B,
107 	SND_MOTU_CLOCK_SOURCE_SPDIF_ON_COAX,
108 	SND_MOTU_CLOCK_SOURCE_AESEBU_ON_XLR,
109 	SND_MOTU_CLOCK_SOURCE_WORD_ON_BNC,
110 	SND_MOTU_CLOCK_SOURCE_SPH,
111 	SND_MOTU_CLOCK_SOURCE_UNKNOWN,
112 };
113 
114 enum snd_motu_protocol_version {
115 	SND_MOTU_PROTOCOL_V1,
116 	SND_MOTU_PROTOCOL_V2,
117 	SND_MOTU_PROTOCOL_V3,
118 };
119 
120 struct snd_motu_spec {
121 	const char *const name;
122 	enum snd_motu_protocol_version protocol_version;
123 	// The combination of snd_motu_spec_flags enumeration-constants.
124 	unsigned int flags;
125 
126 	unsigned char tx_fixed_pcm_chunks[3];
127 	unsigned char rx_fixed_pcm_chunks[3];
128 };
129 
130 extern const struct snd_motu_spec snd_motu_spec_828;
131 extern const struct snd_motu_spec snd_motu_spec_896;
132 
133 extern const struct snd_motu_spec snd_motu_spec_828mk2;
134 extern const struct snd_motu_spec snd_motu_spec_896hd;
135 extern const struct snd_motu_spec snd_motu_spec_traveler;
136 extern const struct snd_motu_spec snd_motu_spec_ultralite;
137 extern const struct snd_motu_spec snd_motu_spec_8pre;
138 
139 extern const struct snd_motu_spec snd_motu_spec_828mk3_fw;
140 extern const struct snd_motu_spec snd_motu_spec_828mk3_hybrid;
141 extern const struct snd_motu_spec snd_motu_spec_ultralite_mk3;
142 extern const struct snd_motu_spec snd_motu_spec_audio_express;
143 extern const struct snd_motu_spec snd_motu_spec_4pre;
144 
145 int amdtp_motu_init(struct amdtp_stream *s, struct fw_unit *unit,
146 		    enum amdtp_stream_direction dir,
147 		    const struct snd_motu_spec *spec,
148 		    struct amdtp_motu_cache *cache);
149 int amdtp_motu_set_parameters(struct amdtp_stream *s, unsigned int rate,
150 			      unsigned int midi_ports,
151 			      struct snd_motu_packet_format *formats);
152 int amdtp_motu_add_pcm_hw_constraints(struct amdtp_stream *s,
153 				      struct snd_pcm_runtime *runtime);
154 void amdtp_motu_midi_trigger(struct amdtp_stream *s, unsigned int port,
155 			     struct snd_rawmidi_substream *midi);
156 
157 int snd_motu_transaction_read(struct snd_motu *motu, u32 offset, __be32 *reg,
158 			      size_t size);
159 int snd_motu_transaction_write(struct snd_motu *motu, u32 offset, __be32 *reg,
160 			       size_t size);
161 int snd_motu_transaction_register(struct snd_motu *motu);
162 int snd_motu_transaction_reregister(struct snd_motu *motu);
163 void snd_motu_transaction_unregister(struct snd_motu *motu);
164 
165 int snd_motu_stream_init_duplex(struct snd_motu *motu);
166 void snd_motu_stream_destroy_duplex(struct snd_motu *motu);
167 int snd_motu_stream_cache_packet_formats(struct snd_motu *motu);
168 int snd_motu_stream_reserve_duplex(struct snd_motu *motu, unsigned int rate,
169 				   unsigned int frames_per_period,
170 				   unsigned int frames_per_buffer);
171 int snd_motu_stream_start_duplex(struct snd_motu *motu);
172 void snd_motu_stream_stop_duplex(struct snd_motu *motu);
173 int snd_motu_stream_lock_try(struct snd_motu *motu);
174 void snd_motu_stream_lock_release(struct snd_motu *motu);
175 
176 void snd_motu_proc_init(struct snd_motu *motu);
177 
178 int snd_motu_create_pcm_devices(struct snd_motu *motu);
179 
180 int snd_motu_create_midi_devices(struct snd_motu *motu);
181 
182 int snd_motu_create_hwdep_device(struct snd_motu *motu);
183 
184 int snd_motu_protocol_v1_get_clock_rate(struct snd_motu *motu,
185 					unsigned int *rate);
186 int snd_motu_protocol_v1_set_clock_rate(struct snd_motu *motu,
187 					unsigned int rate);
188 int snd_motu_protocol_v1_get_clock_source(struct snd_motu *motu,
189 					  enum snd_motu_clock_source *src);
190 int snd_motu_protocol_v1_switch_fetching_mode(struct snd_motu *motu,
191 					      bool enable);
192 int snd_motu_protocol_v1_cache_packet_formats(struct snd_motu *motu);
193 
194 int snd_motu_protocol_v2_get_clock_rate(struct snd_motu *motu,
195 					unsigned int *rate);
196 int snd_motu_protocol_v2_set_clock_rate(struct snd_motu *motu,
197 					unsigned int rate);
198 int snd_motu_protocol_v2_get_clock_source(struct snd_motu *motu,
199 					  enum snd_motu_clock_source *src);
200 int snd_motu_protocol_v2_switch_fetching_mode(struct snd_motu *motu,
201 					      bool enable);
202 int snd_motu_protocol_v2_cache_packet_formats(struct snd_motu *motu);
203 
204 int snd_motu_protocol_v3_get_clock_rate(struct snd_motu *motu,
205 					unsigned int *rate);
206 int snd_motu_protocol_v3_set_clock_rate(struct snd_motu *motu,
207 					unsigned int rate);
208 int snd_motu_protocol_v3_get_clock_source(struct snd_motu *motu,
209 					  enum snd_motu_clock_source *src);
210 int snd_motu_protocol_v3_switch_fetching_mode(struct snd_motu *motu,
211 					      bool enable);
212 int snd_motu_protocol_v3_cache_packet_formats(struct snd_motu *motu);
213 
214 static inline int snd_motu_protocol_get_clock_rate(struct snd_motu *motu,
215 						   unsigned int *rate)
216 {
217 	if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2)
218 		return snd_motu_protocol_v2_get_clock_rate(motu, rate);
219 	else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3)
220 		return snd_motu_protocol_v3_get_clock_rate(motu, rate);
221 	else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V1)
222 		return snd_motu_protocol_v1_get_clock_rate(motu, rate);
223 	else
224 		return -ENXIO;
225 }
226 
227 static inline int snd_motu_protocol_set_clock_rate(struct snd_motu *motu,
228 						   unsigned int rate)
229 {
230 	if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2)
231 		return snd_motu_protocol_v2_set_clock_rate(motu, rate);
232 	else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3)
233 		return snd_motu_protocol_v3_set_clock_rate(motu, rate);
234 	else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V1)
235 		return snd_motu_protocol_v1_set_clock_rate(motu, rate);
236 	else
237 		return -ENXIO;
238 }
239 
240 static inline int snd_motu_protocol_get_clock_source(struct snd_motu *motu,
241 					enum snd_motu_clock_source *source)
242 {
243 	if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2)
244 		return snd_motu_protocol_v2_get_clock_source(motu, source);
245 	else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3)
246 		return snd_motu_protocol_v3_get_clock_source(motu, source);
247 	else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V1)
248 		return snd_motu_protocol_v1_get_clock_source(motu, source);
249 	else
250 		return -ENXIO;
251 }
252 
253 static inline int snd_motu_protocol_switch_fetching_mode(struct snd_motu *motu,
254 							 bool enable)
255 {
256 	if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2)
257 		return snd_motu_protocol_v2_switch_fetching_mode(motu, enable);
258 	else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3)
259 		return snd_motu_protocol_v3_switch_fetching_mode(motu, enable);
260 	else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V1)
261 		return snd_motu_protocol_v1_switch_fetching_mode(motu, enable);
262 	else
263 		return -ENXIO;
264 }
265 
266 static inline int snd_motu_protocol_cache_packet_formats(struct snd_motu *motu)
267 {
268 	if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2)
269 		return snd_motu_protocol_v2_cache_packet_formats(motu);
270 	else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3)
271 		return snd_motu_protocol_v3_cache_packet_formats(motu);
272 	else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V1)
273 		return snd_motu_protocol_v1_cache_packet_formats(motu);
274 	else
275 		return -ENXIO;
276 }
277 
278 int snd_motu_register_dsp_message_parser_new(struct snd_motu *motu);
279 int snd_motu_register_dsp_message_parser_init(struct snd_motu *motu);
280 void snd_motu_register_dsp_message_parser_parse(struct snd_motu *motu, const struct pkt_desc *descs,
281 					unsigned int desc_count, unsigned int data_block_quadlets);
282 void snd_motu_register_dsp_message_parser_copy_meter(struct snd_motu *motu,
283 					struct snd_firewire_motu_register_dsp_meter *meter);
284 void snd_motu_register_dsp_message_parser_copy_parameter(struct snd_motu *motu,
285 					struct snd_firewire_motu_register_dsp_parameter *params);
286 unsigned int snd_motu_register_dsp_message_parser_count_event(struct snd_motu *motu);
287 bool snd_motu_register_dsp_message_parser_copy_event(struct snd_motu *motu, u32 *event);
288 
289 int snd_motu_command_dsp_message_parser_new(struct snd_motu *motu);
290 int snd_motu_command_dsp_message_parser_init(struct snd_motu *motu, enum cip_sfc sfc);
291 void snd_motu_command_dsp_message_parser_parse(struct snd_motu *motu, const struct pkt_desc *descs,
292 					unsigned int desc_count, unsigned int data_block_quadlets);
293 void snd_motu_command_dsp_message_parser_copy_meter(struct snd_motu *motu,
294 					struct snd_firewire_motu_command_dsp_meter *meter);
295 
296 #endif
297