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