| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * AM824 format in Audio and Music Data Transmission Protocol (IEC 61883-6) |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (c) Clemens Ladisch <clemens@ladisch.de> |
|---|
| 5 | 6 | * Copyright (c) 2015 Takashi Sakamoto <o-takashi@sakamocchi.jp> |
|---|
| 6 | | - * |
|---|
| 7 | | - * Licensed under the terms of the GNU General Public License, version 2. |
|---|
| 8 | 7 | */ |
|---|
| 9 | 8 | |
|---|
| 10 | 9 | #include <linux/slab.h> |
|---|
| .. | .. |
|---|
| 83 | 82 | if (err < 0) |
|---|
| 84 | 83 | return err; |
|---|
| 85 | 84 | |
|---|
| 86 | | - s->fdf = AMDTP_FDF_AM824 | s->sfc; |
|---|
| 85 | + if (s->direction == AMDTP_OUT_STREAM) |
|---|
| 86 | + s->ctx_data.rx.fdf = AMDTP_FDF_AM824 | s->sfc; |
|---|
| 87 | 87 | |
|---|
| 88 | 88 | p->pcm_channels = pcm_channels; |
|---|
| 89 | 89 | p->midi_ports = midi_ports; |
|---|
| .. | .. |
|---|
| 147 | 147 | } |
|---|
| 148 | 148 | EXPORT_SYMBOL_GPL(amdtp_am824_set_midi_position); |
|---|
| 149 | 149 | |
|---|
| 150 | | -static void write_pcm_s32(struct amdtp_stream *s, |
|---|
| 151 | | - struct snd_pcm_substream *pcm, |
|---|
| 152 | | - __be32 *buffer, unsigned int frames) |
|---|
| 150 | +static void write_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm, |
|---|
| 151 | + __be32 *buffer, unsigned int frames, |
|---|
| 152 | + unsigned int pcm_frames) |
|---|
| 153 | 153 | { |
|---|
| 154 | 154 | struct amdtp_am824 *p = s->protocol; |
|---|
| 155 | + unsigned int channels = p->pcm_channels; |
|---|
| 155 | 156 | struct snd_pcm_runtime *runtime = pcm->runtime; |
|---|
| 156 | | - unsigned int channels, remaining_frames, i, c; |
|---|
| 157 | + unsigned int pcm_buffer_pointer; |
|---|
| 158 | + int remaining_frames; |
|---|
| 157 | 159 | const u32 *src; |
|---|
| 160 | + int i, c; |
|---|
| 158 | 161 | |
|---|
| 159 | | - channels = p->pcm_channels; |
|---|
| 162 | + pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames; |
|---|
| 163 | + pcm_buffer_pointer %= runtime->buffer_size; |
|---|
| 164 | + |
|---|
| 160 | 165 | src = (void *)runtime->dma_area + |
|---|
| 161 | | - frames_to_bytes(runtime, s->pcm_buffer_pointer); |
|---|
| 162 | | - remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
|---|
| 166 | + frames_to_bytes(runtime, pcm_buffer_pointer); |
|---|
| 167 | + remaining_frames = runtime->buffer_size - pcm_buffer_pointer; |
|---|
| 163 | 168 | |
|---|
| 164 | 169 | for (i = 0; i < frames; ++i) { |
|---|
| 165 | 170 | for (c = 0; c < channels; ++c) { |
|---|
| .. | .. |
|---|
| 173 | 178 | } |
|---|
| 174 | 179 | } |
|---|
| 175 | 180 | |
|---|
| 176 | | -static void read_pcm_s32(struct amdtp_stream *s, |
|---|
| 177 | | - struct snd_pcm_substream *pcm, |
|---|
| 178 | | - __be32 *buffer, unsigned int frames) |
|---|
| 181 | +static void read_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm, |
|---|
| 182 | + __be32 *buffer, unsigned int frames, |
|---|
| 183 | + unsigned int pcm_frames) |
|---|
| 179 | 184 | { |
|---|
| 180 | 185 | struct amdtp_am824 *p = s->protocol; |
|---|
| 186 | + unsigned int channels = p->pcm_channels; |
|---|
| 181 | 187 | struct snd_pcm_runtime *runtime = pcm->runtime; |
|---|
| 182 | | - unsigned int channels, remaining_frames, i, c; |
|---|
| 188 | + unsigned int pcm_buffer_pointer; |
|---|
| 189 | + int remaining_frames; |
|---|
| 183 | 190 | u32 *dst; |
|---|
| 191 | + int i, c; |
|---|
| 184 | 192 | |
|---|
| 185 | | - channels = p->pcm_channels; |
|---|
| 193 | + pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames; |
|---|
| 194 | + pcm_buffer_pointer %= runtime->buffer_size; |
|---|
| 195 | + |
|---|
| 186 | 196 | dst = (void *)runtime->dma_area + |
|---|
| 187 | | - frames_to_bytes(runtime, s->pcm_buffer_pointer); |
|---|
| 188 | | - remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; |
|---|
| 197 | + frames_to_bytes(runtime, pcm_buffer_pointer); |
|---|
| 198 | + remaining_frames = runtime->buffer_size - pcm_buffer_pointer; |
|---|
| 189 | 199 | |
|---|
| 190 | 200 | for (i = 0; i < frames; ++i) { |
|---|
| 191 | 201 | for (c = 0; c < channels; ++c) { |
|---|
| .. | .. |
|---|
| 285 | 295 | } |
|---|
| 286 | 296 | |
|---|
| 287 | 297 | static void write_midi_messages(struct amdtp_stream *s, __be32 *buffer, |
|---|
| 288 | | - unsigned int frames) |
|---|
| 298 | + unsigned int frames, unsigned int data_block_counter) |
|---|
| 289 | 299 | { |
|---|
| 290 | 300 | struct amdtp_am824 *p = s->protocol; |
|---|
| 291 | 301 | unsigned int f, port; |
|---|
| .. | .. |
|---|
| 294 | 304 | for (f = 0; f < frames; f++) { |
|---|
| 295 | 305 | b = (u8 *)&buffer[p->midi_position]; |
|---|
| 296 | 306 | |
|---|
| 297 | | - port = (s->data_block_counter + f) % 8; |
|---|
| 307 | + port = (data_block_counter + f) % 8; |
|---|
| 298 | 308 | if (f < MAX_MIDI_RX_BLOCKS && |
|---|
| 299 | 309 | midi_ratelimit_per_packet(s, port) && |
|---|
| 300 | 310 | p->midi[port] != NULL && |
|---|
| .. | .. |
|---|
| 312 | 322 | } |
|---|
| 313 | 323 | } |
|---|
| 314 | 324 | |
|---|
| 315 | | -static void read_midi_messages(struct amdtp_stream *s, |
|---|
| 316 | | - __be32 *buffer, unsigned int frames) |
|---|
| 325 | +static void read_midi_messages(struct amdtp_stream *s, __be32 *buffer, |
|---|
| 326 | + unsigned int frames, unsigned int data_block_counter) |
|---|
| 317 | 327 | { |
|---|
| 318 | 328 | struct amdtp_am824 *p = s->protocol; |
|---|
| 319 | | - unsigned int f, port; |
|---|
| 320 | 329 | int len; |
|---|
| 321 | 330 | u8 *b; |
|---|
| 331 | + int f; |
|---|
| 322 | 332 | |
|---|
| 323 | 333 | for (f = 0; f < frames; f++) { |
|---|
| 324 | | - port = (8 - s->tx_first_dbc + s->data_block_counter + f) % 8; |
|---|
| 334 | + unsigned int port = f; |
|---|
| 335 | + |
|---|
| 336 | + if (!(s->flags & CIP_UNALIGHED_DBC)) |
|---|
| 337 | + port += data_block_counter; |
|---|
| 338 | + port %= 8; |
|---|
| 325 | 339 | b = (u8 *)&buffer[p->midi_position]; |
|---|
| 326 | 340 | |
|---|
| 327 | 341 | len = b[0] - 0x80; |
|---|
| .. | .. |
|---|
| 332 | 346 | } |
|---|
| 333 | 347 | } |
|---|
| 334 | 348 | |
|---|
| 335 | | -static unsigned int process_rx_data_blocks(struct amdtp_stream *s, __be32 *buffer, |
|---|
| 336 | | - unsigned int data_blocks, unsigned int *syt) |
|---|
| 349 | +static unsigned int process_it_ctx_payloads(struct amdtp_stream *s, |
|---|
| 350 | + const struct pkt_desc *descs, |
|---|
| 351 | + unsigned int packets, |
|---|
| 352 | + struct snd_pcm_substream *pcm) |
|---|
| 337 | 353 | { |
|---|
| 338 | 354 | struct amdtp_am824 *p = s->protocol; |
|---|
| 339 | | - struct snd_pcm_substream *pcm = READ_ONCE(s->pcm); |
|---|
| 340 | | - unsigned int pcm_frames; |
|---|
| 355 | + unsigned int pcm_frames = 0; |
|---|
| 356 | + int i; |
|---|
| 341 | 357 | |
|---|
| 342 | | - if (pcm) { |
|---|
| 343 | | - write_pcm_s32(s, pcm, buffer, data_blocks); |
|---|
| 344 | | - pcm_frames = data_blocks * p->frame_multiplier; |
|---|
| 345 | | - } else { |
|---|
| 346 | | - write_pcm_silence(s, buffer, data_blocks); |
|---|
| 347 | | - pcm_frames = 0; |
|---|
| 358 | + for (i = 0; i < packets; ++i) { |
|---|
| 359 | + const struct pkt_desc *desc = descs + i; |
|---|
| 360 | + __be32 *buf = desc->ctx_payload; |
|---|
| 361 | + unsigned int data_blocks = desc->data_blocks; |
|---|
| 362 | + |
|---|
| 363 | + if (pcm) { |
|---|
| 364 | + write_pcm_s32(s, pcm, buf, data_blocks, pcm_frames); |
|---|
| 365 | + pcm_frames += data_blocks * p->frame_multiplier; |
|---|
| 366 | + } else { |
|---|
| 367 | + write_pcm_silence(s, buf, data_blocks); |
|---|
| 368 | + } |
|---|
| 369 | + |
|---|
| 370 | + if (p->midi_ports) { |
|---|
| 371 | + write_midi_messages(s, buf, data_blocks, |
|---|
| 372 | + desc->data_block_counter); |
|---|
| 373 | + } |
|---|
| 348 | 374 | } |
|---|
| 349 | | - |
|---|
| 350 | | - if (p->midi_ports) |
|---|
| 351 | | - write_midi_messages(s, buffer, data_blocks); |
|---|
| 352 | 375 | |
|---|
| 353 | 376 | return pcm_frames; |
|---|
| 354 | 377 | } |
|---|
| 355 | 378 | |
|---|
| 356 | | -static unsigned int process_tx_data_blocks(struct amdtp_stream *s, __be32 *buffer, |
|---|
| 357 | | - unsigned int data_blocks, unsigned int *syt) |
|---|
| 379 | +static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s, |
|---|
| 380 | + const struct pkt_desc *descs, |
|---|
| 381 | + unsigned int packets, |
|---|
| 382 | + struct snd_pcm_substream *pcm) |
|---|
| 358 | 383 | { |
|---|
| 359 | 384 | struct amdtp_am824 *p = s->protocol; |
|---|
| 360 | | - struct snd_pcm_substream *pcm = READ_ONCE(s->pcm); |
|---|
| 361 | | - unsigned int pcm_frames; |
|---|
| 385 | + unsigned int pcm_frames = 0; |
|---|
| 386 | + int i; |
|---|
| 362 | 387 | |
|---|
| 363 | | - if (pcm) { |
|---|
| 364 | | - read_pcm_s32(s, pcm, buffer, data_blocks); |
|---|
| 365 | | - pcm_frames = data_blocks * p->frame_multiplier; |
|---|
| 366 | | - } else { |
|---|
| 367 | | - pcm_frames = 0; |
|---|
| 388 | + for (i = 0; i < packets; ++i) { |
|---|
| 389 | + const struct pkt_desc *desc = descs + i; |
|---|
| 390 | + __be32 *buf = desc->ctx_payload; |
|---|
| 391 | + unsigned int data_blocks = desc->data_blocks; |
|---|
| 392 | + |
|---|
| 393 | + if (pcm) { |
|---|
| 394 | + read_pcm_s32(s, pcm, buf, data_blocks, pcm_frames); |
|---|
| 395 | + pcm_frames += data_blocks * p->frame_multiplier; |
|---|
| 396 | + } |
|---|
| 397 | + |
|---|
| 398 | + if (p->midi_ports) { |
|---|
| 399 | + read_midi_messages(s, buf, data_blocks, |
|---|
| 400 | + desc->data_block_counter); |
|---|
| 401 | + } |
|---|
| 368 | 402 | } |
|---|
| 369 | | - |
|---|
| 370 | | - if (p->midi_ports) |
|---|
| 371 | | - read_midi_messages(s, buffer, data_blocks); |
|---|
| 372 | 403 | |
|---|
| 373 | 404 | return pcm_frames; |
|---|
| 374 | 405 | } |
|---|
| .. | .. |
|---|
| 384 | 415 | int amdtp_am824_init(struct amdtp_stream *s, struct fw_unit *unit, |
|---|
| 385 | 416 | enum amdtp_stream_direction dir, enum cip_flags flags) |
|---|
| 386 | 417 | { |
|---|
| 387 | | - amdtp_stream_process_data_blocks_t process_data_blocks; |
|---|
| 418 | + amdtp_stream_process_ctx_payloads_t process_ctx_payloads; |
|---|
| 388 | 419 | |
|---|
| 389 | 420 | if (dir == AMDTP_IN_STREAM) |
|---|
| 390 | | - process_data_blocks = process_tx_data_blocks; |
|---|
| 421 | + process_ctx_payloads = process_ir_ctx_payloads; |
|---|
| 391 | 422 | else |
|---|
| 392 | | - process_data_blocks = process_rx_data_blocks; |
|---|
| 423 | + process_ctx_payloads = process_it_ctx_payloads; |
|---|
| 393 | 424 | |
|---|
| 394 | 425 | return amdtp_stream_init(s, unit, dir, flags, CIP_FMT_AM, |
|---|
| 395 | | - process_data_blocks, |
|---|
| 396 | | - sizeof(struct amdtp_am824)); |
|---|
| 426 | + process_ctx_payloads, sizeof(struct amdtp_am824)); |
|---|
| 397 | 427 | } |
|---|
| 398 | 428 | EXPORT_SYMBOL_GPL(amdtp_am824_init); |
|---|