xref: /openbmc/linux/sound/firewire/motu/motu.h (revision b566e972f59443adfd9b5d0cea83c2c0cf1e1bdc)
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 	bool registered;
58 	struct delayed_work dwork;
59 
60 	/* Model dependent information. */
61 	const struct snd_motu_spec *spec;
62 
63 	/* For packet streaming */
64 	struct snd_motu_packet_format tx_packet_formats;
65 	struct snd_motu_packet_format rx_packet_formats;
66 	struct amdtp_stream tx_stream;
67 	struct amdtp_stream rx_stream;
68 	struct fw_iso_resources tx_resources;
69 	struct fw_iso_resources rx_resources;
70 	unsigned int substreams_counter;
71 
72 	/* For notification. */
73 	struct fw_address_handler async_handler;
74 	u32 msg;
75 
76 	/* For uapi */
77 	int dev_lock_count;
78 	bool dev_lock_changed;
79 	wait_queue_head_t hwdep_wait;
80 
81 	struct amdtp_domain domain;
82 
83 	struct amdtp_motu_cache cache;
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 };
92 
93 #define SND_MOTU_CLOCK_RATE_COUNT	6
94 extern const unsigned int snd_motu_clock_rates[SND_MOTU_CLOCK_RATE_COUNT];
95 
96 enum snd_motu_clock_source {
97 	SND_MOTU_CLOCK_SOURCE_INTERNAL,
98 	SND_MOTU_CLOCK_SOURCE_ADAT_ON_DSUB,
99 	SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT,
100 	SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT_A,
101 	SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT_B,
102 	SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT,
103 	SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT_A,
104 	SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT_B,
105 	SND_MOTU_CLOCK_SOURCE_SPDIF_ON_COAX,
106 	SND_MOTU_CLOCK_SOURCE_AESEBU_ON_XLR,
107 	SND_MOTU_CLOCK_SOURCE_WORD_ON_BNC,
108 	SND_MOTU_CLOCK_SOURCE_SPH,
109 	SND_MOTU_CLOCK_SOURCE_UNKNOWN,
110 };
111 
112 enum snd_motu_protocol_version {
113 	SND_MOTU_PROTOCOL_V2,
114 	SND_MOTU_PROTOCOL_V3,
115 };
116 
117 struct snd_motu_spec {
118 	const char *const name;
119 	enum snd_motu_protocol_version protocol_version;
120 	// The combination of snd_motu_spec_flags enumeration-constants.
121 	unsigned int flags;
122 
123 	unsigned char tx_fixed_pcm_chunks[3];
124 	unsigned char rx_fixed_pcm_chunks[3];
125 };
126 
127 extern const struct snd_motu_spec snd_motu_spec_828mk2;
128 extern const struct snd_motu_spec snd_motu_spec_traveler;
129 extern const struct snd_motu_spec snd_motu_spec_ultralite;
130 extern const struct snd_motu_spec snd_motu_spec_8pre;
131 
132 extern const struct snd_motu_spec snd_motu_spec_828mk3;
133 extern const struct snd_motu_spec snd_motu_spec_ultralite_mk3;
134 extern const struct snd_motu_spec snd_motu_spec_audio_express;
135 extern const struct snd_motu_spec snd_motu_spec_4pre;
136 
137 int amdtp_motu_init(struct amdtp_stream *s, struct fw_unit *unit,
138 		    enum amdtp_stream_direction dir,
139 		    const struct snd_motu_spec *spec,
140 		    struct amdtp_motu_cache *cache);
141 int amdtp_motu_set_parameters(struct amdtp_stream *s, unsigned int rate,
142 			      unsigned int midi_ports,
143 			      struct snd_motu_packet_format *formats);
144 int amdtp_motu_add_pcm_hw_constraints(struct amdtp_stream *s,
145 				      struct snd_pcm_runtime *runtime);
146 void amdtp_motu_midi_trigger(struct amdtp_stream *s, unsigned int port,
147 			     struct snd_rawmidi_substream *midi);
148 
149 int snd_motu_transaction_read(struct snd_motu *motu, u32 offset, __be32 *reg,
150 			      size_t size);
151 int snd_motu_transaction_write(struct snd_motu *motu, u32 offset, __be32 *reg,
152 			       size_t size);
153 int snd_motu_transaction_register(struct snd_motu *motu);
154 int snd_motu_transaction_reregister(struct snd_motu *motu);
155 void snd_motu_transaction_unregister(struct snd_motu *motu);
156 
157 int snd_motu_stream_init_duplex(struct snd_motu *motu);
158 void snd_motu_stream_destroy_duplex(struct snd_motu *motu);
159 int snd_motu_stream_cache_packet_formats(struct snd_motu *motu);
160 int snd_motu_stream_reserve_duplex(struct snd_motu *motu, unsigned int rate,
161 				   unsigned int frames_per_period,
162 				   unsigned int frames_per_buffer);
163 int snd_motu_stream_start_duplex(struct snd_motu *motu);
164 void snd_motu_stream_stop_duplex(struct snd_motu *motu);
165 int snd_motu_stream_lock_try(struct snd_motu *motu);
166 void snd_motu_stream_lock_release(struct snd_motu *motu);
167 
168 void snd_motu_proc_init(struct snd_motu *motu);
169 
170 int snd_motu_create_pcm_devices(struct snd_motu *motu);
171 
172 int snd_motu_create_midi_devices(struct snd_motu *motu);
173 
174 int snd_motu_create_hwdep_device(struct snd_motu *motu);
175 
176 int snd_motu_protocol_v2_get_clock_rate(struct snd_motu *motu,
177 					unsigned int *rate);
178 int snd_motu_protocol_v2_set_clock_rate(struct snd_motu *motu,
179 					unsigned int rate);
180 int snd_motu_protocol_v2_get_clock_source(struct snd_motu *motu,
181 					  enum snd_motu_clock_source *src);
182 int snd_motu_protocol_v2_switch_fetching_mode(struct snd_motu *motu,
183 					      bool enable);
184 int snd_motu_protocol_v2_cache_packet_formats(struct snd_motu *motu);
185 
186 int snd_motu_protocol_v3_get_clock_rate(struct snd_motu *motu,
187 					unsigned int *rate);
188 int snd_motu_protocol_v3_set_clock_rate(struct snd_motu *motu,
189 					unsigned int rate);
190 int snd_motu_protocol_v3_get_clock_source(struct snd_motu *motu,
191 					  enum snd_motu_clock_source *src);
192 int snd_motu_protocol_v3_switch_fetching_mode(struct snd_motu *motu,
193 					      bool enable);
194 int snd_motu_protocol_v3_cache_packet_formats(struct snd_motu *motu);
195 
196 static inline int snd_motu_protocol_get_clock_rate(struct snd_motu *motu,
197 						   unsigned int *rate)
198 {
199 	if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2)
200 		return snd_motu_protocol_v2_get_clock_rate(motu, rate);
201 	else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3)
202 		return snd_motu_protocol_v3_get_clock_rate(motu, rate);
203 	else
204 		return -ENXIO;
205 }
206 
207 static inline int snd_motu_protocol_set_clock_rate(struct snd_motu *motu,
208 						   unsigned int rate)
209 {
210 	if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2)
211 		return snd_motu_protocol_v2_set_clock_rate(motu, rate);
212 	else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3)
213 		return snd_motu_protocol_v3_set_clock_rate(motu, rate);
214 	else
215 		return -ENXIO;
216 }
217 
218 static inline int snd_motu_protocol_get_clock_source(struct snd_motu *motu,
219 					enum snd_motu_clock_source *source)
220 {
221 	if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2)
222 		return snd_motu_protocol_v2_get_clock_source(motu, source);
223 	else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3)
224 		return snd_motu_protocol_v3_get_clock_source(motu, source);
225 	else
226 		return -ENXIO;
227 }
228 
229 static inline int snd_motu_protocol_switch_fetching_mode(struct snd_motu *motu,
230 							 bool enable)
231 {
232 	if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2)
233 		return snd_motu_protocol_v2_switch_fetching_mode(motu, enable);
234 	else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3)
235 		return snd_motu_protocol_v3_switch_fetching_mode(motu, enable);
236 	else
237 		return -ENXIO;
238 }
239 
240 static inline int snd_motu_protocol_cache_packet_formats(struct snd_motu *motu)
241 {
242 	if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2)
243 		return snd_motu_protocol_v2_cache_packet_formats(motu);
244 	else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3)
245 		return snd_motu_protocol_v3_cache_packet_formats(motu);
246 	else
247 		return -ENXIO;
248 }
249 
250 #endif
251