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 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 75 enum 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 83 extern const unsigned int snd_motu_clock_rates[SND_MOTU_CLOCK_RATE_COUNT]; 84 85 enum 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 101 enum snd_motu_protocol_version { 102 SND_MOTU_PROTOCOL_V2, 103 SND_MOTU_PROTOCOL_V3, 104 }; 105 106 struct snd_motu_spec { 107 const char *const name; 108 enum snd_motu_protocol_version protocol_version; 109 // The combination of snd_motu_spec_flags enumeration-constants. 110 unsigned int flags; 111 112 unsigned char tx_fixed_pcm_chunks[3]; 113 unsigned char rx_fixed_pcm_chunks[3]; 114 }; 115 116 extern const struct snd_motu_spec snd_motu_spec_828mk2; 117 extern const struct snd_motu_spec snd_motu_spec_traveler; 118 extern const struct snd_motu_spec snd_motu_spec_ultralite; 119 extern const struct snd_motu_spec snd_motu_spec_8pre; 120 121 extern const struct snd_motu_spec snd_motu_spec_828mk3; 122 extern const struct snd_motu_spec snd_motu_spec_ultralite_mk3; 123 extern const struct snd_motu_spec snd_motu_spec_audio_express; 124 extern const struct snd_motu_spec snd_motu_spec_4pre; 125 126 int amdtp_motu_init(struct amdtp_stream *s, struct fw_unit *unit, 127 enum amdtp_stream_direction dir, 128 const struct snd_motu_spec *spec); 129 int amdtp_motu_set_parameters(struct amdtp_stream *s, unsigned int rate, 130 unsigned int midi_ports, 131 struct snd_motu_packet_format *formats); 132 int amdtp_motu_add_pcm_hw_constraints(struct amdtp_stream *s, 133 struct snd_pcm_runtime *runtime); 134 void amdtp_motu_midi_trigger(struct amdtp_stream *s, unsigned int port, 135 struct snd_rawmidi_substream *midi); 136 137 int snd_motu_transaction_read(struct snd_motu *motu, u32 offset, __be32 *reg, 138 size_t size); 139 int snd_motu_transaction_write(struct snd_motu *motu, u32 offset, __be32 *reg, 140 size_t size); 141 int snd_motu_transaction_register(struct snd_motu *motu); 142 int snd_motu_transaction_reregister(struct snd_motu *motu); 143 void snd_motu_transaction_unregister(struct snd_motu *motu); 144 145 int snd_motu_stream_init_duplex(struct snd_motu *motu); 146 void snd_motu_stream_destroy_duplex(struct snd_motu *motu); 147 int snd_motu_stream_cache_packet_formats(struct snd_motu *motu); 148 int snd_motu_stream_reserve_duplex(struct snd_motu *motu, unsigned int rate, 149 unsigned int frames_per_period, 150 unsigned int frames_per_buffer); 151 int snd_motu_stream_start_duplex(struct snd_motu *motu); 152 void snd_motu_stream_stop_duplex(struct snd_motu *motu); 153 int snd_motu_stream_lock_try(struct snd_motu *motu); 154 void snd_motu_stream_lock_release(struct snd_motu *motu); 155 156 void snd_motu_proc_init(struct snd_motu *motu); 157 158 int snd_motu_create_pcm_devices(struct snd_motu *motu); 159 160 int snd_motu_create_midi_devices(struct snd_motu *motu); 161 162 int snd_motu_create_hwdep_device(struct snd_motu *motu); 163 164 int snd_motu_protocol_v2_get_clock_rate(struct snd_motu *motu, 165 unsigned int *rate); 166 int snd_motu_protocol_v2_set_clock_rate(struct snd_motu *motu, 167 unsigned int rate); 168 int snd_motu_protocol_v2_get_clock_source(struct snd_motu *motu, 169 enum snd_motu_clock_source *src); 170 int snd_motu_protocol_v2_switch_fetching_mode(struct snd_motu *motu, 171 bool enable); 172 int snd_motu_protocol_v2_cache_packet_formats(struct snd_motu *motu); 173 174 int snd_motu_protocol_v3_get_clock_rate(struct snd_motu *motu, 175 unsigned int *rate); 176 int snd_motu_protocol_v3_set_clock_rate(struct snd_motu *motu, 177 unsigned int rate); 178 int snd_motu_protocol_v3_get_clock_source(struct snd_motu *motu, 179 enum snd_motu_clock_source *src); 180 int snd_motu_protocol_v3_switch_fetching_mode(struct snd_motu *motu, 181 bool enable); 182 int snd_motu_protocol_v3_cache_packet_formats(struct snd_motu *motu); 183 184 static inline int snd_motu_protocol_get_clock_rate(struct snd_motu *motu, 185 unsigned int *rate) 186 { 187 if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2) 188 return snd_motu_protocol_v2_get_clock_rate(motu, rate); 189 else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3) 190 return snd_motu_protocol_v3_get_clock_rate(motu, rate); 191 else 192 return -ENXIO; 193 } 194 195 static inline int snd_motu_protocol_set_clock_rate(struct snd_motu *motu, 196 unsigned int rate) 197 { 198 if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2) 199 return snd_motu_protocol_v2_set_clock_rate(motu, rate); 200 else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3) 201 return snd_motu_protocol_v3_set_clock_rate(motu, rate); 202 else 203 return -ENXIO; 204 } 205 206 static inline int snd_motu_protocol_get_clock_source(struct snd_motu *motu, 207 enum snd_motu_clock_source *source) 208 { 209 if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2) 210 return snd_motu_protocol_v2_get_clock_source(motu, source); 211 else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3) 212 return snd_motu_protocol_v3_get_clock_source(motu, source); 213 else 214 return -ENXIO; 215 } 216 217 static inline int snd_motu_protocol_switch_fetching_mode(struct snd_motu *motu, 218 bool enable) 219 { 220 if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2) 221 return snd_motu_protocol_v2_switch_fetching_mode(motu, enable); 222 else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3) 223 return snd_motu_protocol_v3_switch_fetching_mode(motu, enable); 224 else 225 return -ENXIO; 226 } 227 228 static inline int snd_motu_protocol_cache_packet_formats(struct snd_motu *motu) 229 { 230 if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V2) 231 return snd_motu_protocol_v2_cache_packet_formats(motu); 232 else if (motu->spec->protocol_version == SND_MOTU_PROTOCOL_V3) 233 return snd_motu_protocol_v3_cache_packet_formats(motu); 234 else 235 return -ENXIO; 236 } 237 238 #endif 239