| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * amdtp-motu.c - a part of driver for MOTU FireWire series |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * 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 | 6 | */ |
|---|
| 8 | 7 | |
|---|
| 9 | 8 | #include <linux/slab.h> |
|---|
| .. | .. |
|---|
| 77 | 76 | if (i == ARRAY_SIZE(snd_motu_clock_rates)) |
|---|
| 78 | 77 | return -EINVAL; |
|---|
| 79 | 78 | |
|---|
| 80 | | - pcm_chunks = formats->fixed_part_pcm_chunks[mode] + |
|---|
| 81 | | - formats->differed_part_pcm_chunks[mode]; |
|---|
| 79 | + // Each data block includes SPH in its head. Data chunks follow with |
|---|
| 80 | + // 3 byte alignment. Padding follows with zero to conform to quadlet |
|---|
| 81 | + // alignment. |
|---|
| 82 | + pcm_chunks = formats->pcm_chunks[mode]; |
|---|
| 82 | 83 | 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 | 84 | data_block_quadlets = 1 + DIV_ROUND_UP(data_chunks * 3, 4); |
|---|
| 90 | 85 | |
|---|
| 91 | 86 | err = amdtp_stream_set_parameters(s, rate, data_block_quadlets); |
|---|
| .. | .. |
|---|
| 118 | 113 | return 0; |
|---|
| 119 | 114 | } |
|---|
| 120 | 115 | |
|---|
| 121 | | -static void read_pcm_s32(struct amdtp_stream *s, |
|---|
| 122 | | - struct snd_pcm_runtime *runtime, |
|---|
| 123 | | - __be32 *buffer, unsigned int data_blocks) |
|---|
| 116 | +static void read_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm, |
|---|
| 117 | + __be32 *buffer, unsigned int data_blocks, |
|---|
| 118 | + unsigned int pcm_frames) |
|---|
| 124 | 119 | { |
|---|
| 125 | 120 | struct amdtp_motu *p = s->protocol; |
|---|
| 126 | | - unsigned int channels, remaining_frames, i, c; |
|---|
| 121 | + unsigned int channels = p->pcm_chunks; |
|---|
| 122 | + struct snd_pcm_runtime *runtime = pcm->runtime; |
|---|
| 123 | + unsigned int pcm_buffer_pointer; |
|---|
| 124 | + int remaining_frames; |
|---|
| 127 | 125 | u8 *byte; |
|---|
| 128 | 126 | u32 *dst; |
|---|
| 127 | + int i, c; |
|---|
| 129 | 128 | |
|---|
| 130 | | - channels = p->pcm_chunks; |
|---|
| 129 | + pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames; |
|---|
| 130 | + pcm_buffer_pointer %= runtime->buffer_size; |
|---|
| 131 | + |
|---|
| 131 | 132 | dst = (void *)runtime->dma_area + |
|---|
| 132 | | - frames_to_bytes(runtime, s->pcm_buffer_pointer); |
|---|
| 133 | | - remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
|---|
| 133 | + frames_to_bytes(runtime, pcm_buffer_pointer); |
|---|
| 134 | + remaining_frames = runtime->buffer_size - pcm_buffer_pointer; |
|---|
| 134 | 135 | |
|---|
| 135 | 136 | for (i = 0; i < data_blocks; ++i) { |
|---|
| 136 | 137 | byte = (u8 *)buffer + p->pcm_byte_offset; |
|---|
| .. | .. |
|---|
| 148 | 149 | } |
|---|
| 149 | 150 | } |
|---|
| 150 | 151 | |
|---|
| 151 | | -static void write_pcm_s32(struct amdtp_stream *s, |
|---|
| 152 | | - struct snd_pcm_runtime *runtime, |
|---|
| 153 | | - __be32 *buffer, unsigned int data_blocks) |
|---|
| 152 | +static void write_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm, |
|---|
| 153 | + __be32 *buffer, unsigned int data_blocks, |
|---|
| 154 | + unsigned int pcm_frames) |
|---|
| 154 | 155 | { |
|---|
| 155 | 156 | struct amdtp_motu *p = s->protocol; |
|---|
| 156 | | - unsigned int channels, remaining_frames, i, c; |
|---|
| 157 | + unsigned int channels = p->pcm_chunks; |
|---|
| 158 | + struct snd_pcm_runtime *runtime = pcm->runtime; |
|---|
| 159 | + unsigned int pcm_buffer_pointer; |
|---|
| 160 | + int remaining_frames; |
|---|
| 157 | 161 | u8 *byte; |
|---|
| 158 | 162 | const u32 *src; |
|---|
| 163 | + int i, c; |
|---|
| 159 | 164 | |
|---|
| 160 | | - channels = p->pcm_chunks; |
|---|
| 165 | + pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames; |
|---|
| 166 | + pcm_buffer_pointer %= runtime->buffer_size; |
|---|
| 167 | + |
|---|
| 161 | 168 | src = (void *)runtime->dma_area + |
|---|
| 162 | | - frames_to_bytes(runtime, s->pcm_buffer_pointer); |
|---|
| 163 | | - remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
|---|
| 169 | + frames_to_bytes(runtime, pcm_buffer_pointer); |
|---|
| 170 | + remaining_frames = runtime->buffer_size - pcm_buffer_pointer; |
|---|
| 164 | 171 | |
|---|
| 165 | 172 | for (i = 0; i < data_blocks; ++i) { |
|---|
| 166 | 173 | byte = (u8 *)buffer + p->pcm_byte_offset; |
|---|
| .. | .. |
|---|
| 299 | 306 | } |
|---|
| 300 | 307 | } |
|---|
| 301 | 308 | |
|---|
| 302 | | -static unsigned int process_tx_data_blocks(struct amdtp_stream *s, |
|---|
| 303 | | - __be32 *buffer, unsigned int data_blocks, |
|---|
| 304 | | - unsigned int *syt) |
|---|
| 309 | +static void probe_tracepoints_events(struct amdtp_stream *s, |
|---|
| 310 | + const struct pkt_desc *descs, |
|---|
| 311 | + unsigned int packets) |
|---|
| 312 | +{ |
|---|
| 313 | + int i; |
|---|
| 314 | + |
|---|
| 315 | + for (i = 0; i < packets; ++i) { |
|---|
| 316 | + const struct pkt_desc *desc = descs + i; |
|---|
| 317 | + __be32 *buf = desc->ctx_payload; |
|---|
| 318 | + unsigned int data_blocks = desc->data_blocks; |
|---|
| 319 | + |
|---|
| 320 | + trace_data_block_sph(s, data_blocks, buf); |
|---|
| 321 | + trace_data_block_message(s, data_blocks, buf); |
|---|
| 322 | + } |
|---|
| 323 | +} |
|---|
| 324 | + |
|---|
| 325 | +static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s, |
|---|
| 326 | + const struct pkt_desc *descs, |
|---|
| 327 | + unsigned int packets, |
|---|
| 328 | + struct snd_pcm_substream *pcm) |
|---|
| 305 | 329 | { |
|---|
| 306 | 330 | struct amdtp_motu *p = s->protocol; |
|---|
| 307 | | - struct snd_pcm_substream *pcm; |
|---|
| 331 | + unsigned int pcm_frames = 0; |
|---|
| 332 | + int i; |
|---|
| 308 | 333 | |
|---|
| 309 | | - trace_in_data_block_sph(s, data_blocks, buffer); |
|---|
| 310 | | - trace_in_data_block_message(s, data_blocks, buffer); |
|---|
| 334 | + // For data block processing. |
|---|
| 335 | + for (i = 0; i < packets; ++i) { |
|---|
| 336 | + const struct pkt_desc *desc = descs + i; |
|---|
| 337 | + __be32 *buf = desc->ctx_payload; |
|---|
| 338 | + unsigned int data_blocks = desc->data_blocks; |
|---|
| 311 | 339 | |
|---|
| 312 | | - if (p->midi_ports) |
|---|
| 313 | | - read_midi_messages(s, buffer, data_blocks); |
|---|
| 340 | + if (pcm) { |
|---|
| 341 | + read_pcm_s32(s, pcm, buf, data_blocks, pcm_frames); |
|---|
| 342 | + pcm_frames += data_blocks; |
|---|
| 343 | + } |
|---|
| 314 | 344 | |
|---|
| 315 | | - pcm = READ_ONCE(s->pcm); |
|---|
| 316 | | - if (data_blocks > 0 && pcm) |
|---|
| 317 | | - read_pcm_s32(s, pcm->runtime, buffer, data_blocks); |
|---|
| 345 | + if (p->midi_ports) |
|---|
| 346 | + read_midi_messages(s, buf, data_blocks); |
|---|
| 347 | + } |
|---|
| 318 | 348 | |
|---|
| 319 | | - return data_blocks; |
|---|
| 349 | + // For tracepoints. |
|---|
| 350 | + if (trace_data_block_sph_enabled() || |
|---|
| 351 | + trace_data_block_message_enabled()) |
|---|
| 352 | + probe_tracepoints_events(s, descs, packets); |
|---|
| 353 | + |
|---|
| 354 | + return pcm_frames; |
|---|
| 320 | 355 | } |
|---|
| 321 | 356 | |
|---|
| 322 | 357 | static inline void compute_next_elapse_from_start(struct amdtp_motu *p) |
|---|
| .. | .. |
|---|
| 361 | 396 | } |
|---|
| 362 | 397 | } |
|---|
| 363 | 398 | |
|---|
| 364 | | -static unsigned int process_rx_data_blocks(struct amdtp_stream *s, |
|---|
| 365 | | - __be32 *buffer, unsigned int data_blocks, |
|---|
| 366 | | - unsigned int *syt) |
|---|
| 399 | +static unsigned int process_it_ctx_payloads(struct amdtp_stream *s, |
|---|
| 400 | + const struct pkt_desc *descs, |
|---|
| 401 | + unsigned int packets, |
|---|
| 402 | + struct snd_pcm_substream *pcm) |
|---|
| 367 | 403 | { |
|---|
| 368 | | - struct amdtp_motu *p = (struct amdtp_motu *)s->protocol; |
|---|
| 369 | | - struct snd_pcm_substream *pcm; |
|---|
| 404 | + struct amdtp_motu *p = s->protocol; |
|---|
| 405 | + unsigned int pcm_frames = 0; |
|---|
| 406 | + int i; |
|---|
| 370 | 407 | |
|---|
| 371 | | - /* Not used. */ |
|---|
| 372 | | - *syt = 0xffff; |
|---|
| 408 | + // For data block processing. |
|---|
| 409 | + for (i = 0; i < packets; ++i) { |
|---|
| 410 | + const struct pkt_desc *desc = descs + i; |
|---|
| 411 | + __be32 *buf = desc->ctx_payload; |
|---|
| 412 | + unsigned int data_blocks = desc->data_blocks; |
|---|
| 373 | 413 | |
|---|
| 374 | | - /* TODO: how to interact control messages between userspace? */ |
|---|
| 414 | + if (pcm) { |
|---|
| 415 | + write_pcm_s32(s, pcm, buf, data_blocks, pcm_frames); |
|---|
| 416 | + pcm_frames += data_blocks; |
|---|
| 417 | + } else { |
|---|
| 418 | + write_pcm_silence(s, buf, data_blocks); |
|---|
| 419 | + } |
|---|
| 375 | 420 | |
|---|
| 376 | | - if (p->midi_ports) |
|---|
| 377 | | - write_midi_messages(s, buffer, data_blocks); |
|---|
| 421 | + if (p->midi_ports) |
|---|
| 422 | + write_midi_messages(s, buf, data_blocks); |
|---|
| 378 | 423 | |
|---|
| 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); |
|---|
| 424 | + // TODO: how to interact control messages between userspace? |
|---|
| 384 | 425 | |
|---|
| 385 | | - write_sph(s, buffer, data_blocks); |
|---|
| 426 | + write_sph(s, buf, data_blocks); |
|---|
| 427 | + } |
|---|
| 386 | 428 | |
|---|
| 387 | | - trace_out_data_block_sph(s, data_blocks, buffer); |
|---|
| 388 | | - trace_out_data_block_message(s, data_blocks, buffer); |
|---|
| 429 | + // For tracepoints. |
|---|
| 430 | + if (trace_data_block_sph_enabled() || |
|---|
| 431 | + trace_data_block_message_enabled()) |
|---|
| 432 | + probe_tracepoints_events(s, descs, packets); |
|---|
| 389 | 433 | |
|---|
| 390 | | - return data_blocks; |
|---|
| 434 | + return pcm_frames; |
|---|
| 391 | 435 | } |
|---|
| 392 | 436 | |
|---|
| 393 | 437 | int amdtp_motu_init(struct amdtp_stream *s, struct fw_unit *unit, |
|---|
| 394 | 438 | enum amdtp_stream_direction dir, |
|---|
| 395 | | - const struct snd_motu_protocol *const protocol) |
|---|
| 439 | + const struct snd_motu_spec *spec) |
|---|
| 396 | 440 | { |
|---|
| 397 | | - amdtp_stream_process_data_blocks_t process_data_blocks; |
|---|
| 441 | + amdtp_stream_process_ctx_payloads_t process_ctx_payloads; |
|---|
| 398 | 442 | int fmt = CIP_FMT_MOTU; |
|---|
| 399 | 443 | int flags = CIP_BLOCKING; |
|---|
| 400 | 444 | int err; |
|---|
| 401 | 445 | |
|---|
| 402 | 446 | if (dir == AMDTP_IN_STREAM) { |
|---|
| 403 | | - process_data_blocks = process_tx_data_blocks; |
|---|
| 447 | + process_ctx_payloads = process_ir_ctx_payloads; |
|---|
| 404 | 448 | |
|---|
| 405 | 449 | /* |
|---|
| 406 | 450 | * Units of version 3 transmits packets with invalid CIP header |
|---|
| 407 | 451 | * against IEC 61883-1. |
|---|
| 408 | 452 | */ |
|---|
| 409 | | - if (protocol == &snd_motu_protocol_v3) { |
|---|
| 453 | + if (spec->protocol_version == SND_MOTU_PROTOCOL_V3) { |
|---|
| 410 | 454 | flags |= CIP_WRONG_DBS | |
|---|
| 411 | 455 | CIP_SKIP_DBC_ZERO_CHECK | |
|---|
| 412 | 456 | CIP_HEADER_WITHOUT_EOH; |
|---|
| 413 | 457 | fmt = CIP_FMT_MOTU_TX_V3; |
|---|
| 414 | 458 | } |
|---|
| 459 | + |
|---|
| 460 | + if (spec == &snd_motu_spec_8pre || |
|---|
| 461 | + spec == &snd_motu_spec_ultralite) { |
|---|
| 462 | + // 8pre has some quirks. |
|---|
| 463 | + flags |= CIP_WRONG_DBS | |
|---|
| 464 | + CIP_SKIP_DBC_ZERO_CHECK; |
|---|
| 465 | + } |
|---|
| 415 | 466 | } else { |
|---|
| 416 | | - process_data_blocks = process_rx_data_blocks; |
|---|
| 467 | + process_ctx_payloads = process_it_ctx_payloads; |
|---|
| 417 | 468 | flags |= CIP_DBC_IS_END_EVENT; |
|---|
| 418 | 469 | } |
|---|
| 419 | 470 | |
|---|
| 420 | | - err = amdtp_stream_init(s, unit, dir, flags, fmt, process_data_blocks, |
|---|
| 471 | + err = amdtp_stream_init(s, unit, dir, flags, fmt, process_ctx_payloads, |
|---|
| 421 | 472 | sizeof(struct amdtp_motu)); |
|---|
| 422 | 473 | if (err < 0) |
|---|
| 423 | 474 | return err; |
|---|
| 424 | 475 | |
|---|
| 425 | 476 | s->sph = 1; |
|---|
| 426 | | - s->fdf = MOTU_FDF_AM824; |
|---|
| 477 | + |
|---|
| 478 | + if (dir == AMDTP_OUT_STREAM) { |
|---|
| 479 | + // Use fixed value for FDF field. |
|---|
| 480 | + s->ctx_data.rx.fdf = MOTU_FDF_AM824; |
|---|
| 481 | + // Not used. |
|---|
| 482 | + s->ctx_data.rx.syt_override = 0xffff; |
|---|
| 483 | + } |
|---|
| 427 | 484 | |
|---|
| 428 | 485 | return 0; |
|---|
| 429 | 486 | } |
|---|