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