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