1 /* 2 * amdtp-motu.c - a part of driver for MOTU FireWire series 3 * 4 * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp> 5 * 6 * Licensed under the terms of the GNU General Public License, version 2. 7 */ 8 9 #include <linux/slab.h> 10 #include <sound/pcm.h> 11 #include "motu.h" 12 13 #define CREATE_TRACE_POINTS 14 #include "amdtp-motu-trace.h" 15 16 #define CIP_FMT_MOTU 0x02 17 #define CIP_FMT_MOTU_TX_V3 0x22 18 #define MOTU_FDF_AM824 0x22 19 20 /* 21 * Nominally 3125 bytes/second, but the MIDI port's clock might be 22 * 1% too slow, and the bus clock 100 ppm too fast. 23 */ 24 #define MIDI_BYTES_PER_SECOND 3093 25 26 struct amdtp_motu { 27 /* For timestamp processing. */ 28 unsigned int quotient_ticks_per_event; 29 unsigned int remainder_ticks_per_event; 30 unsigned int next_ticks; 31 unsigned int next_accumulated; 32 unsigned int next_cycles; 33 unsigned int next_seconds; 34 35 unsigned int pcm_chunks; 36 unsigned int pcm_byte_offset; 37 38 struct snd_rawmidi_substream *midi; 39 unsigned int midi_ports; 40 unsigned int midi_flag_offset; 41 unsigned int midi_byte_offset; 42 43 int midi_db_count; 44 unsigned int midi_db_interval; 45 }; 46 47 int amdtp_motu_set_parameters(struct amdtp_stream *s, unsigned int rate, 48 unsigned int midi_ports, 49 struct snd_motu_packet_format *formats) 50 { 51 static const struct { 52 unsigned int quotient_ticks_per_event; 53 unsigned int remainder_ticks_per_event; 54 } params[] = { 55 [CIP_SFC_44100] = { 557, 123 }, 56 [CIP_SFC_48000] = { 512, 0 }, 57 [CIP_SFC_88200] = { 278, 282 }, 58 [CIP_SFC_96000] = { 256, 0 }, 59 [CIP_SFC_176400] = { 139, 141 }, 60 [CIP_SFC_192000] = { 128, 0 }, 61 }; 62 struct amdtp_motu *p = s->protocol; 63 unsigned int pcm_chunks, data_chunks, data_block_quadlets; 64 unsigned int delay; 65 unsigned int mode; 66 int i, err; 67 68 if (amdtp_stream_running(s)) 69 return -EBUSY; 70 71 for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) { 72 if (snd_motu_clock_rates[i] == rate) { 73 mode = i >> 1; 74 break; 75 } 76 } 77 if (i == ARRAY_SIZE(snd_motu_clock_rates)) 78 return -EINVAL; 79 80 pcm_chunks = formats->fixed_part_pcm_chunks[mode] + 81 formats->differed_part_pcm_chunks[mode]; 82 data_chunks = formats->msg_chunks + pcm_chunks; 83 84 /* 85 * Each data block includes SPH in its head. Data chunks follow with 86 * 3 byte alignment. Padding follows with zero to conform to quadlet 87 * alignment. 88 */ 89 data_block_quadlets = 1 + DIV_ROUND_UP(data_chunks * 3, 4); 90 91 err = amdtp_stream_set_parameters(s, rate, data_block_quadlets); 92 if (err < 0) 93 return err; 94 95 p->pcm_chunks = pcm_chunks; 96 p->pcm_byte_offset = formats->pcm_byte_offset; 97 98 p->midi_ports = midi_ports; 99 p->midi_flag_offset = formats->midi_flag_offset; 100 p->midi_byte_offset = formats->midi_byte_offset; 101 102 p->midi_db_count = 0; 103 p->midi_db_interval = rate / MIDI_BYTES_PER_SECOND; 104 105 /* IEEE 1394 bus requires. */ 106 delay = 0x2e00; 107 108 /* For no-data or empty packets to adjust PCM sampling frequency. */ 109 delay += 8000 * 3072 * s->syt_interval / rate; 110 111 p->next_seconds = 0; 112 p->next_cycles = delay / 3072; 113 p->quotient_ticks_per_event = params[s->sfc].quotient_ticks_per_event; 114 p->remainder_ticks_per_event = params[s->sfc].remainder_ticks_per_event; 115 p->next_ticks = delay % 3072; 116 p->next_accumulated = 0; 117 118 return 0; 119 } 120 121 static void read_pcm_s32(struct amdtp_stream *s, 122 struct snd_pcm_runtime *runtime, 123 __be32 *buffer, unsigned int data_blocks) 124 { 125 struct amdtp_motu *p = s->protocol; 126 unsigned int channels, remaining_frames, i, c; 127 u8 *byte; 128 u32 *dst; 129 130 channels = p->pcm_chunks; 131 dst = (void *)runtime->dma_area + 132 frames_to_bytes(runtime, s->pcm_buffer_pointer); 133 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; 134 135 for (i = 0; i < data_blocks; ++i) { 136 byte = (u8 *)buffer + p->pcm_byte_offset; 137 138 for (c = 0; c < channels; ++c) { 139 *dst = (byte[0] << 24) | 140 (byte[1] << 16) | 141 (byte[2] << 8); 142 byte += 3; 143 dst++; 144 } 145 buffer += s->data_block_quadlets; 146 if (--remaining_frames == 0) 147 dst = (void *)runtime->dma_area; 148 } 149 } 150 151 static void write_pcm_s32(struct amdtp_stream *s, 152 struct snd_pcm_runtime *runtime, 153 __be32 *buffer, unsigned int data_blocks) 154 { 155 struct amdtp_motu *p = s->protocol; 156 unsigned int channels, remaining_frames, i, c; 157 u8 *byte; 158 const u32 *src; 159 160 channels = p->pcm_chunks; 161 src = (void *)runtime->dma_area + 162 frames_to_bytes(runtime, s->pcm_buffer_pointer); 163 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; 164 165 for (i = 0; i < data_blocks; ++i) { 166 byte = (u8 *)buffer + p->pcm_byte_offset; 167 168 for (c = 0; c < channels; ++c) { 169 byte[0] = (*src >> 24) & 0xff; 170 byte[1] = (*src >> 16) & 0xff; 171 byte[2] = (*src >> 8) & 0xff; 172 byte += 3; 173 src++; 174 } 175 176 buffer += s->data_block_quadlets; 177 if (--remaining_frames == 0) 178 src = (void *)runtime->dma_area; 179 } 180 } 181 182 static void write_pcm_silence(struct amdtp_stream *s, __be32 *buffer, 183 unsigned int data_blocks) 184 { 185 struct amdtp_motu *p = s->protocol; 186 unsigned int channels, i, c; 187 u8 *byte; 188 189 channels = p->pcm_chunks; 190 191 for (i = 0; i < data_blocks; ++i) { 192 byte = (u8 *)buffer + p->pcm_byte_offset; 193 194 for (c = 0; c < channels; ++c) { 195 byte[0] = 0; 196 byte[1] = 0; 197 byte[2] = 0; 198 byte += 3; 199 } 200 201 buffer += s->data_block_quadlets; 202 } 203 } 204 205 int amdtp_motu_add_pcm_hw_constraints(struct amdtp_stream *s, 206 struct snd_pcm_runtime *runtime) 207 { 208 int err; 209 210 /* TODO: how to set an constraint for exactly 24bit PCM sample? */ 211 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24); 212 if (err < 0) 213 return err; 214 215 return amdtp_stream_add_pcm_hw_constraints(s, runtime); 216 } 217 218 void amdtp_motu_midi_trigger(struct amdtp_stream *s, unsigned int port, 219 struct snd_rawmidi_substream *midi) 220 { 221 struct amdtp_motu *p = s->protocol; 222 223 if (port < p->midi_ports) 224 WRITE_ONCE(p->midi, midi); 225 } 226 227 static void write_midi_messages(struct amdtp_stream *s, __be32 *buffer, 228 unsigned int data_blocks) 229 { 230 struct amdtp_motu *p = s->protocol; 231 struct snd_rawmidi_substream *midi = READ_ONCE(p->midi); 232 u8 *b; 233 int i; 234 235 for (i = 0; i < data_blocks; i++) { 236 b = (u8 *)buffer; 237 238 if (midi && p->midi_db_count == 0 && 239 snd_rawmidi_transmit(midi, b + p->midi_byte_offset, 1) == 1) { 240 b[p->midi_flag_offset] = 0x01; 241 } else { 242 b[p->midi_byte_offset] = 0x00; 243 b[p->midi_flag_offset] = 0x00; 244 } 245 246 buffer += s->data_block_quadlets; 247 248 if (--p->midi_db_count < 0) 249 p->midi_db_count = p->midi_db_interval; 250 } 251 } 252 253 static void read_midi_messages(struct amdtp_stream *s, __be32 *buffer, 254 unsigned int data_blocks) 255 { 256 struct amdtp_motu *p = s->protocol; 257 struct snd_rawmidi_substream *midi; 258 u8 *b; 259 int i; 260 261 for (i = 0; i < data_blocks; i++) { 262 b = (u8 *)buffer; 263 midi = READ_ONCE(p->midi); 264 265 if (midi && (b[p->midi_flag_offset] & 0x01)) 266 snd_rawmidi_receive(midi, b + p->midi_byte_offset, 1); 267 268 buffer += s->data_block_quadlets; 269 } 270 } 271 272 /* For tracepoints. */ 273 static void __maybe_unused copy_sph(u32 *frames, __be32 *buffer, 274 unsigned int data_blocks, 275 unsigned int data_block_quadlets) 276 { 277 unsigned int i; 278 279 for (i = 0; i < data_blocks; ++i) { 280 *frames = be32_to_cpu(*buffer); 281 buffer += data_block_quadlets; 282 frames++; 283 } 284 } 285 286 /* For tracepoints. */ 287 static void __maybe_unused copy_message(u64 *frames, __be32 *buffer, 288 unsigned int data_blocks, 289 unsigned int data_block_quadlets) 290 { 291 unsigned int i; 292 293 /* This is just for v2/v3 protocol. */ 294 for (i = 0; i < data_blocks; ++i) { 295 *frames = (be32_to_cpu(buffer[1]) << 16) | 296 (be32_to_cpu(buffer[2]) >> 16); 297 buffer += data_block_quadlets; 298 frames++; 299 } 300 } 301 302 static unsigned int process_tx_data_blocks(struct amdtp_stream *s, 303 __be32 *buffer, unsigned int data_blocks, 304 unsigned int *syt) 305 { 306 struct amdtp_motu *p = s->protocol; 307 struct snd_pcm_substream *pcm; 308 309 trace_in_data_block_sph(s, data_blocks, buffer); 310 trace_in_data_block_message(s, data_blocks, buffer); 311 312 if (p->midi_ports) 313 read_midi_messages(s, buffer, data_blocks); 314 315 pcm = READ_ONCE(s->pcm); 316 if (data_blocks > 0 && pcm) 317 read_pcm_s32(s, pcm->runtime, buffer, data_blocks); 318 319 return data_blocks; 320 } 321 322 static inline void compute_next_elapse_from_start(struct amdtp_motu *p) 323 { 324 p->next_accumulated += p->remainder_ticks_per_event; 325 if (p->next_accumulated >= 441) { 326 p->next_accumulated -= 441; 327 p->next_ticks++; 328 } 329 330 p->next_ticks += p->quotient_ticks_per_event; 331 if (p->next_ticks >= 3072) { 332 p->next_ticks -= 3072; 333 p->next_cycles++; 334 } 335 336 if (p->next_cycles >= 8000) { 337 p->next_cycles -= 8000; 338 p->next_seconds++; 339 } 340 341 if (p->next_seconds >= 128) 342 p->next_seconds -= 128; 343 } 344 345 static void write_sph(struct amdtp_stream *s, __be32 *buffer, 346 unsigned int data_blocks) 347 { 348 struct amdtp_motu *p = s->protocol; 349 unsigned int next_cycles; 350 unsigned int i; 351 u32 sph; 352 353 for (i = 0; i < data_blocks; i++) { 354 next_cycles = (s->start_cycle + p->next_cycles) % 8000; 355 sph = ((next_cycles << 12) | p->next_ticks) & 0x01ffffff; 356 *buffer = cpu_to_be32(sph); 357 358 compute_next_elapse_from_start(p); 359 360 buffer += s->data_block_quadlets; 361 } 362 } 363 364 static unsigned int process_rx_data_blocks(struct amdtp_stream *s, 365 __be32 *buffer, unsigned int data_blocks, 366 unsigned int *syt) 367 { 368 struct amdtp_motu *p = (struct amdtp_motu *)s->protocol; 369 struct snd_pcm_substream *pcm; 370 371 /* Not used. */ 372 *syt = 0xffff; 373 374 /* TODO: how to interact control messages between userspace? */ 375 376 if (p->midi_ports) 377 write_midi_messages(s, buffer, data_blocks); 378 379 pcm = READ_ONCE(s->pcm); 380 if (pcm) 381 write_pcm_s32(s, pcm->runtime, buffer, data_blocks); 382 else 383 write_pcm_silence(s, buffer, data_blocks); 384 385 write_sph(s, buffer, data_blocks); 386 387 trace_out_data_block_sph(s, data_blocks, buffer); 388 trace_out_data_block_message(s, data_blocks, buffer); 389 390 return data_blocks; 391 } 392 393 int amdtp_motu_init(struct amdtp_stream *s, struct fw_unit *unit, 394 enum amdtp_stream_direction dir, 395 const struct snd_motu_protocol *const protocol) 396 { 397 amdtp_stream_process_data_blocks_t process_data_blocks; 398 int fmt = CIP_FMT_MOTU; 399 int flags = CIP_BLOCKING; 400 int err; 401 402 if (dir == AMDTP_IN_STREAM) { 403 process_data_blocks = process_tx_data_blocks; 404 405 /* 406 * Units of version 3 transmits packets with invalid CIP header 407 * against IEC 61883-1. 408 */ 409 if (protocol == &snd_motu_protocol_v3) { 410 flags |= CIP_WRONG_DBS | 411 CIP_SKIP_DBC_ZERO_CHECK | 412 CIP_HEADER_WITHOUT_EOH; 413 fmt = CIP_FMT_MOTU_TX_V3; 414 } 415 } else { 416 process_data_blocks = process_rx_data_blocks; 417 flags |= CIP_DBC_IS_END_EVENT; 418 } 419 420 err = amdtp_stream_init(s, unit, dir, flags, fmt, process_data_blocks, 421 sizeof(struct amdtp_motu)); 422 if (err < 0) 423 return err; 424 425 s->sph = 1; 426 s->fdf = MOTU_FDF_AM824; 427 428 return 0; 429 } 430