1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * motu.h - a part of driver for MOTU FireWire series
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #ifndef SOUND_FIREWIRE_MOTU_H_INCLUDED
9*4882a593Smuzhiyun #define SOUND_FIREWIRE_MOTU_H_INCLUDED
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/device.h>
12*4882a593Smuzhiyun #include <linux/firewire.h>
13*4882a593Smuzhiyun #include <linux/firewire-constants.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/mod_devicetable.h>
16*4882a593Smuzhiyun #include <linux/mutex.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun #include <linux/compat.h>
19*4882a593Smuzhiyun #include <linux/sched/signal.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #include <sound/control.h>
22*4882a593Smuzhiyun #include <sound/core.h>
23*4882a593Smuzhiyun #include <sound/pcm.h>
24*4882a593Smuzhiyun #include <sound/info.h>
25*4882a593Smuzhiyun #include <sound/rawmidi.h>
26*4882a593Smuzhiyun #include <sound/firewire.h>
27*4882a593Smuzhiyun #include <sound/hwdep.h>
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include "../lib.h"
30*4882a593Smuzhiyun #include "../amdtp-stream.h"
31*4882a593Smuzhiyun #include "../iso-resources.h"
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun struct snd_motu_packet_format {
34*4882a593Smuzhiyun unsigned char midi_flag_offset;
35*4882a593Smuzhiyun unsigned char midi_byte_offset;
36*4882a593Smuzhiyun unsigned char pcm_byte_offset;
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun unsigned char msg_chunks;
39*4882a593Smuzhiyun unsigned char pcm_chunks[3];
40*4882a593Smuzhiyun };
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun struct snd_motu {
43*4882a593Smuzhiyun struct snd_card *card;
44*4882a593Smuzhiyun struct fw_unit *unit;
45*4882a593Smuzhiyun struct mutex mutex;
46*4882a593Smuzhiyun spinlock_t lock;
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun bool registered;
49*4882a593Smuzhiyun struct delayed_work dwork;
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun /* Model dependent information. */
52*4882a593Smuzhiyun const struct snd_motu_spec *spec;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /* For packet streaming */
55*4882a593Smuzhiyun struct snd_motu_packet_format tx_packet_formats;
56*4882a593Smuzhiyun struct snd_motu_packet_format rx_packet_formats;
57*4882a593Smuzhiyun struct amdtp_stream tx_stream;
58*4882a593Smuzhiyun struct amdtp_stream rx_stream;
59*4882a593Smuzhiyun struct fw_iso_resources tx_resources;
60*4882a593Smuzhiyun struct fw_iso_resources rx_resources;
61*4882a593Smuzhiyun unsigned int substreams_counter;
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /* For notification. */
64*4882a593Smuzhiyun struct fw_address_handler async_handler;
65*4882a593Smuzhiyun u32 msg;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /* For uapi */
68*4882a593Smuzhiyun int dev_lock_count;
69*4882a593Smuzhiyun bool dev_lock_changed;
70*4882a593Smuzhiyun wait_queue_head_t hwdep_wait;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun struct amdtp_domain domain;
73*4882a593Smuzhiyun };
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun enum snd_motu_spec_flags {
76*4882a593Smuzhiyun SND_MOTU_SPEC_RX_MIDI_2ND_Q = 0x0001,
77*4882a593Smuzhiyun SND_MOTU_SPEC_RX_MIDI_3RD_Q = 0x0002,
78*4882a593Smuzhiyun SND_MOTU_SPEC_TX_MIDI_2ND_Q = 0x0004,
79*4882a593Smuzhiyun SND_MOTU_SPEC_TX_MIDI_3RD_Q = 0x0008,
80*4882a593Smuzhiyun };
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun #define SND_MOTU_CLOCK_RATE_COUNT 6
83*4882a593Smuzhiyun extern const unsigned int snd_motu_clock_rates[SND_MOTU_CLOCK_RATE_COUNT];
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun enum snd_motu_clock_source {
86*4882a593Smuzhiyun SND_MOTU_CLOCK_SOURCE_INTERNAL,
87*4882a593Smuzhiyun SND_MOTU_CLOCK_SOURCE_ADAT_ON_DSUB,
88*4882a593Smuzhiyun SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT,
89*4882a593Smuzhiyun SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT_A,
90*4882a593Smuzhiyun SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT_B,
91*4882a593Smuzhiyun SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT,
92*4882a593Smuzhiyun SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT_A,
93*4882a593Smuzhiyun SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT_B,
94*4882a593Smuzhiyun SND_MOTU_CLOCK_SOURCE_SPDIF_ON_COAX,
95*4882a593Smuzhiyun SND_MOTU_CLOCK_SOURCE_AESEBU_ON_XLR,
96*4882a593Smuzhiyun SND_MOTU_CLOCK_SOURCE_WORD_ON_BNC,
97*4882a593Smuzhiyun SND_MOTU_CLOCK_SOURCE_SPH,
98*4882a593Smuzhiyun SND_MOTU_CLOCK_SOURCE_UNKNOWN,
99*4882a593Smuzhiyun };
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun enum snd_motu_protocol_version {
102*4882a593Smuzhiyun SND_MOTU_PROTOCOL_V2,
103*4882a593Smuzhiyun SND_MOTU_PROTOCOL_V3,
104*4882a593Smuzhiyun };
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun struct snd_motu_spec {
107*4882a593Smuzhiyun const char *const name;
108*4882a593Smuzhiyun enum snd_motu_protocol_version protocol_version;
109*4882a593Smuzhiyun enum snd_motu_spec_flags flags;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun unsigned char tx_fixed_pcm_chunks[3];
112*4882a593Smuzhiyun unsigned char rx_fixed_pcm_chunks[3];
113*4882a593Smuzhiyun };
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun extern const struct snd_motu_spec snd_motu_spec_828mk2;
116*4882a593Smuzhiyun extern const struct snd_motu_spec snd_motu_spec_traveler;
117*4882a593Smuzhiyun extern const struct snd_motu_spec snd_motu_spec_ultralite;
118*4882a593Smuzhiyun extern const struct snd_motu_spec snd_motu_spec_8pre;
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun extern const struct snd_motu_spec snd_motu_spec_828mk3;
121*4882a593Smuzhiyun extern const struct snd_motu_spec snd_motu_spec_ultralite_mk3;
122*4882a593Smuzhiyun extern const struct snd_motu_spec snd_motu_spec_audio_express;
123*4882a593Smuzhiyun extern const struct snd_motu_spec snd_motu_spec_4pre;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun int amdtp_motu_init(struct amdtp_stream *s, struct fw_unit *unit,
126*4882a593Smuzhiyun enum amdtp_stream_direction dir,
127*4882a593Smuzhiyun const struct snd_motu_spec *spec);
128*4882a593Smuzhiyun int amdtp_motu_set_parameters(struct amdtp_stream *s, unsigned int rate,
129*4882a593Smuzhiyun unsigned int midi_ports,
130*4882a593Smuzhiyun struct snd_motu_packet_format *formats);
131*4882a593Smuzhiyun int amdtp_motu_add_pcm_hw_constraints(struct amdtp_stream *s,
132*4882a593Smuzhiyun struct snd_pcm_runtime *runtime);
133*4882a593Smuzhiyun void amdtp_motu_midi_trigger(struct amdtp_stream *s, unsigned int port,
134*4882a593Smuzhiyun struct snd_rawmidi_substream *midi);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun int snd_motu_transaction_read(struct snd_motu *motu, u32 offset, __be32 *reg,
137*4882a593Smuzhiyun size_t size);
138*4882a593Smuzhiyun int snd_motu_transaction_write(struct snd_motu *motu, u32 offset, __be32 *reg,
139*4882a593Smuzhiyun size_t size);
140*4882a593Smuzhiyun int snd_motu_transaction_register(struct snd_motu *motu);
141*4882a593Smuzhiyun int snd_motu_transaction_reregister(struct snd_motu *motu);
142*4882a593Smuzhiyun void snd_motu_transaction_unregister(struct snd_motu *motu);
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun int snd_motu_stream_init_duplex(struct snd_motu *motu);
145*4882a593Smuzhiyun void snd_motu_stream_destroy_duplex(struct snd_motu *motu);
146*4882a593Smuzhiyun int snd_motu_stream_cache_packet_formats(struct snd_motu *motu);
147*4882a593Smuzhiyun int snd_motu_stream_reserve_duplex(struct snd_motu *motu, unsigned int rate,
148*4882a593Smuzhiyun unsigned int frames_per_period,
149*4882a593Smuzhiyun unsigned int frames_per_buffer);
150*4882a593Smuzhiyun int snd_motu_stream_start_duplex(struct snd_motu *motu);
151*4882a593Smuzhiyun void snd_motu_stream_stop_duplex(struct snd_motu *motu);
152*4882a593Smuzhiyun int snd_motu_stream_lock_try(struct snd_motu *motu);
153*4882a593Smuzhiyun void snd_motu_stream_lock_release(struct snd_motu *motu);
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun void snd_motu_proc_init(struct snd_motu *motu);
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun int snd_motu_create_pcm_devices(struct snd_motu *motu);
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun int snd_motu_create_midi_devices(struct snd_motu *motu);
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun int snd_motu_create_hwdep_device(struct snd_motu *motu);
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun int snd_motu_protocol_v2_get_clock_rate(struct snd_motu *motu,
164*4882a593Smuzhiyun unsigned int *rate);
165*4882a593Smuzhiyun int snd_motu_protocol_v2_set_clock_rate(struct snd_motu *motu,
166*4882a593Smuzhiyun unsigned int rate);
167*4882a593Smuzhiyun int snd_motu_protocol_v2_get_clock_source(struct snd_motu *motu,
168*4882a593Smuzhiyun enum snd_motu_clock_source *src);
169*4882a593Smuzhiyun int snd_motu_protocol_v2_switch_fetching_mode(struct snd_motu *motu,
170*4882a593Smuzhiyun bool enable);
171*4882a593Smuzhiyun int snd_motu_protocol_v2_cache_packet_formats(struct snd_motu *motu);
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun int snd_motu_protocol_v3_get_clock_rate(struct snd_motu *motu,
174*4882a593Smuzhiyun unsigned int *rate);
175*4882a593Smuzhiyun int snd_motu_protocol_v3_set_clock_rate(struct snd_motu *motu,
176*4882a593Smuzhiyun unsigned int rate);
177*4882a593Smuzhiyun int snd_motu_protocol_v3_get_clock_source(struct snd_motu *motu,
178*4882a593Smuzhiyun enum snd_motu_clock_source *src);
179*4882a593Smuzhiyun int snd_motu_protocol_v3_switch_fetching_mode(struct snd_motu *motu,
180*4882a593Smuzhiyun bool enable);
181*4882a593Smuzhiyun int snd_motu_protocol_v3_cache_packet_formats(struct snd_motu *motu);
182*4882a593Smuzhiyun
snd_motu_protocol_get_clock_rate(struct snd_motu * motu,unsigned int * rate)183*4882a593Smuzhiyun static inline int snd_motu_protocol_get_clock_rate(struct snd_motu *motu,
184*4882a593Smuzhiyun unsigned int *rate)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2)
187*4882a593Smuzhiyun return snd_motu_protocol_v2_get_clock_rate(motu, rate);
188*4882a593Smuzhiyun else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3)
189*4882a593Smuzhiyun return snd_motu_protocol_v3_get_clock_rate(motu, rate);
190*4882a593Smuzhiyun else
191*4882a593Smuzhiyun return -ENXIO;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
snd_motu_protocol_set_clock_rate(struct snd_motu * motu,unsigned int rate)194*4882a593Smuzhiyun static inline int snd_motu_protocol_set_clock_rate(struct snd_motu *motu,
195*4882a593Smuzhiyun unsigned int rate)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2)
198*4882a593Smuzhiyun return snd_motu_protocol_v2_set_clock_rate(motu, rate);
199*4882a593Smuzhiyun else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3)
200*4882a593Smuzhiyun return snd_motu_protocol_v3_set_clock_rate(motu, rate);
201*4882a593Smuzhiyun else
202*4882a593Smuzhiyun return -ENXIO;
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
snd_motu_protocol_get_clock_source(struct snd_motu * motu,enum snd_motu_clock_source * source)205*4882a593Smuzhiyun static inline int snd_motu_protocol_get_clock_source(struct snd_motu *motu,
206*4882a593Smuzhiyun enum snd_motu_clock_source *source)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2)
209*4882a593Smuzhiyun return snd_motu_protocol_v2_get_clock_source(motu, source);
210*4882a593Smuzhiyun else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3)
211*4882a593Smuzhiyun return snd_motu_protocol_v3_get_clock_source(motu, source);
212*4882a593Smuzhiyun else
213*4882a593Smuzhiyun return -ENXIO;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
snd_motu_protocol_switch_fetching_mode(struct snd_motu * motu,bool enable)216*4882a593Smuzhiyun static inline int snd_motu_protocol_switch_fetching_mode(struct snd_motu *motu,
217*4882a593Smuzhiyun bool enable)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2)
220*4882a593Smuzhiyun return snd_motu_protocol_v2_switch_fetching_mode(motu, enable);
221*4882a593Smuzhiyun else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3)
222*4882a593Smuzhiyun return snd_motu_protocol_v3_switch_fetching_mode(motu, enable);
223*4882a593Smuzhiyun else
224*4882a593Smuzhiyun return -ENXIO;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun
snd_motu_protocol_cache_packet_formats(struct snd_motu * motu)227*4882a593Smuzhiyun static inline int snd_motu_protocol_cache_packet_formats(struct snd_motu *motu)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2)
230*4882a593Smuzhiyun return snd_motu_protocol_v2_cache_packet_formats(motu);
231*4882a593Smuzhiyun else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3)
232*4882a593Smuzhiyun return snd_motu_protocol_v3_cache_packet_formats(motu);
233*4882a593Smuzhiyun else
234*4882a593Smuzhiyun return -ENXIO;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun #endif
238