^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * AM824 format in Audio and Music Data Transmission Protocol (IEC 61883-6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2015 Takashi Sakamoto <o-takashi@sakamocchi.jp>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "amdtp-am824.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define CIP_FMT_AM 0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /* "Clock-based rate control mode" is just supported. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define AMDTP_FDF_AM824 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Nominally 3125 bytes/second, but the MIDI port's clock might be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * 1% too slow, and the bus clock 100 ppm too fast.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define MIDI_BYTES_PER_SECOND 3093
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * Several devices look only at the first eight data blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * In any case, this is more than enough for the MIDI data rate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define MAX_MIDI_RX_BLOCKS 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct amdtp_am824 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct snd_rawmidi_substream *midi[AM824_MAX_CHANNELS_FOR_MIDI * 8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int midi_fifo_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int midi_fifo_used[AM824_MAX_CHANNELS_FOR_MIDI * 8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) unsigned int pcm_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) unsigned int midi_ports;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) u8 pcm_positions[AM824_MAX_CHANNELS_FOR_PCM];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) u8 midi_position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) unsigned int frame_multiplier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * amdtp_am824_set_parameters - set stream parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * @s: the AMDTP stream to configure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * @rate: the sample rate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * @pcm_channels: the number of PCM samples in each data block, to be encoded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * as AM824 multi-bit linear audio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * @double_pcm_frames: one data block transfers two PCM frames
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * The parameters must be set before the stream is started, and must not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * changed while the stream is running.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int amdtp_am824_set_parameters(struct amdtp_stream *s, unsigned int rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) unsigned int pcm_channels,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) unsigned int midi_ports,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) bool double_pcm_frames)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct amdtp_am824 *p = s->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) unsigned int midi_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (amdtp_stream_running(s))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (pcm_channels > AM824_MAX_CHANNELS_FOR_PCM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) midi_channels = DIV_ROUND_UP(midi_ports, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (midi_channels > AM824_MAX_CHANNELS_FOR_MIDI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (WARN_ON(amdtp_stream_running(s)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) WARN_ON(pcm_channels > AM824_MAX_CHANNELS_FOR_PCM) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) WARN_ON(midi_channels > AM824_MAX_CHANNELS_FOR_MIDI))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) err = amdtp_stream_set_parameters(s, rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) pcm_channels + midi_channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (s->direction == AMDTP_OUT_STREAM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) s->ctx_data.rx.fdf = AMDTP_FDF_AM824 | s->sfc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) p->pcm_channels = pcm_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) p->midi_ports = midi_ports;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * In IEC 61883-6, one data block represents one event. In ALSA, one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * event equals to one PCM frame. But Dice has a quirk at higher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * sampling rate to transfer two PCM frames in one data block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (double_pcm_frames)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) p->frame_multiplier = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) p->frame_multiplier = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /* init the position map for PCM and MIDI channels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) for (i = 0; i < pcm_channels; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) p->pcm_positions[i] = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) p->midi_position = p->pcm_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * We do not know the actual MIDI FIFO size of most devices. Just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * assume two bytes, i.e., one byte can be received over the bus while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * the previous one is transmitted over MIDI.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * (The value here is adjusted for midi_ratelimit_per_packet().)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) p->midi_fifo_limit = rate - MIDI_BYTES_PER_SECOND * s->syt_interval + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) EXPORT_SYMBOL_GPL(amdtp_am824_set_parameters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * amdtp_am824_set_pcm_position - set an index of data channel for a channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * of PCM frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * @s: the AMDTP stream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * @index: the index of data channel in an data block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * @position: the channel of PCM frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) void amdtp_am824_set_pcm_position(struct amdtp_stream *s, unsigned int index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) unsigned int position)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct amdtp_am824 *p = s->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (index < p->pcm_channels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) p->pcm_positions[index] = position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) EXPORT_SYMBOL_GPL(amdtp_am824_set_pcm_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * amdtp_am824_set_midi_position - set a index of data channel for MIDI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * conformant data channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * @s: the AMDTP stream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * @position: the index of data channel in an data block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) void amdtp_am824_set_midi_position(struct amdtp_stream *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) unsigned int position)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct amdtp_am824 *p = s->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) p->midi_position = position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) EXPORT_SYMBOL_GPL(amdtp_am824_set_midi_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static void write_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) __be32 *buffer, unsigned int frames,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) unsigned int pcm_frames)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct amdtp_am824 *p = s->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) unsigned int channels = p->pcm_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct snd_pcm_runtime *runtime = pcm->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) unsigned int pcm_buffer_pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) int remaining_frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) const u32 *src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int i, c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) pcm_buffer_pointer %= runtime->buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) src = (void *)runtime->dma_area +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) frames_to_bytes(runtime, pcm_buffer_pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) remaining_frames = runtime->buffer_size - pcm_buffer_pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) for (i = 0; i < frames; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) for (c = 0; c < channels; ++c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) buffer[p->pcm_positions[c]] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) cpu_to_be32((*src >> 8) | 0x40000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) src++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) buffer += s->data_block_quadlets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (--remaining_frames == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) src = (void *)runtime->dma_area;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static void read_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) __be32 *buffer, unsigned int frames,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) unsigned int pcm_frames)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct amdtp_am824 *p = s->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) unsigned int channels = p->pcm_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct snd_pcm_runtime *runtime = pcm->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) unsigned int pcm_buffer_pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) int remaining_frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) u32 *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) int i, c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) pcm_buffer_pointer %= runtime->buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) dst = (void *)runtime->dma_area +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) frames_to_bytes(runtime, pcm_buffer_pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) remaining_frames = runtime->buffer_size - pcm_buffer_pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) for (i = 0; i < frames; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) for (c = 0; c < channels; ++c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) *dst = be32_to_cpu(buffer[p->pcm_positions[c]]) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) dst++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) buffer += s->data_block_quadlets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (--remaining_frames == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) dst = (void *)runtime->dma_area;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static void write_pcm_silence(struct amdtp_stream *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) __be32 *buffer, unsigned int frames)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct amdtp_am824 *p = s->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) unsigned int i, c, channels = p->pcm_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) for (i = 0; i < frames; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) for (c = 0; c < channels; ++c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) buffer[p->pcm_positions[c]] = cpu_to_be32(0x40000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) buffer += s->data_block_quadlets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * amdtp_am824_add_pcm_hw_constraints - add hw constraints for PCM substream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * @s: the AMDTP stream for AM824 data block, must be initialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * @runtime: the PCM substream runtime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) int amdtp_am824_add_pcm_hw_constraints(struct amdtp_stream *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct snd_pcm_runtime *runtime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) err = amdtp_stream_add_pcm_hw_constraints(s, runtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /* AM824 in IEC 61883-6 can deliver 24bit data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) EXPORT_SYMBOL_GPL(amdtp_am824_add_pcm_hw_constraints);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * amdtp_am824_midi_trigger - start/stop playback/capture with a MIDI device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * @s: the AMDTP stream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * @port: index of MIDI port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * @midi: the MIDI device to be started, or %NULL to stop the current device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * Call this function on a running isochronous stream to enable the actual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * transmission of MIDI data. This function should be called from the MIDI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * device's .trigger callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) void amdtp_am824_midi_trigger(struct amdtp_stream *s, unsigned int port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct snd_rawmidi_substream *midi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct amdtp_am824 *p = s->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (port < p->midi_ports)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) WRITE_ONCE(p->midi[port], midi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) EXPORT_SYMBOL_GPL(amdtp_am824_midi_trigger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * To avoid sending MIDI bytes at too high a rate, assume that the receiving
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * device has a FIFO, and track how much it is filled. This values increases
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) * by one whenever we send one byte in a packet, but the FIFO empties at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * a constant rate independent of our packet rate. One packet has syt_interval
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * samples, so the number of bytes that empty out of the FIFO, per packet(!),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * is MIDI_BYTES_PER_SECOND * syt_interval / sample_rate. To avoid storing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * fractional values, the values in midi_fifo_used[] are measured in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * multiplied by the sample rate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static bool midi_ratelimit_per_packet(struct amdtp_stream *s, unsigned int port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct amdtp_am824 *p = s->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) int used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) used = p->midi_fifo_used[port];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (used == 0) /* common shortcut */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) used -= MIDI_BYTES_PER_SECOND * s->syt_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) used = max(used, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) p->midi_fifo_used[port] = used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return used < p->midi_fifo_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static void midi_rate_use_one_byte(struct amdtp_stream *s, unsigned int port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct amdtp_am824 *p = s->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) p->midi_fifo_used[port] += amdtp_rate_table[s->sfc];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) static void write_midi_messages(struct amdtp_stream *s, __be32 *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) unsigned int frames, unsigned int data_block_counter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) struct amdtp_am824 *p = s->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) unsigned int f, port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) u8 *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) for (f = 0; f < frames; f++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) b = (u8 *)&buffer[p->midi_position];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) port = (data_block_counter + f) % 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (f < MAX_MIDI_RX_BLOCKS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) midi_ratelimit_per_packet(s, port) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) p->midi[port] != NULL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) snd_rawmidi_transmit(p->midi[port], &b[1], 1) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) midi_rate_use_one_byte(s, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) b[0] = 0x81;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) b[0] = 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) b[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) b[2] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) b[3] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) buffer += s->data_block_quadlets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static void read_midi_messages(struct amdtp_stream *s, __be32 *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) unsigned int frames, unsigned int data_block_counter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) struct amdtp_am824 *p = s->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) u8 *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) int f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) for (f = 0; f < frames; f++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) unsigned int port = f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (!(s->flags & CIP_UNALIGHED_DBC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) port += data_block_counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) port %= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) b = (u8 *)&buffer[p->midi_position];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) len = b[0] - 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if ((1 <= len) && (len <= 3) && (p->midi[port]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) snd_rawmidi_receive(p->midi[port], b + 1, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) buffer += s->data_block_quadlets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static unsigned int process_it_ctx_payloads(struct amdtp_stream *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) const struct pkt_desc *descs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) unsigned int packets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) struct snd_pcm_substream *pcm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) struct amdtp_am824 *p = s->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) unsigned int pcm_frames = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) for (i = 0; i < packets; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) const struct pkt_desc *desc = descs + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) __be32 *buf = desc->ctx_payload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) unsigned int data_blocks = desc->data_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (pcm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) write_pcm_s32(s, pcm, buf, data_blocks, pcm_frames);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) pcm_frames += data_blocks * p->frame_multiplier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) write_pcm_silence(s, buf, data_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (p->midi_ports) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) write_midi_messages(s, buf, data_blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) desc->data_block_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) return pcm_frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) const struct pkt_desc *descs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) unsigned int packets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) struct snd_pcm_substream *pcm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) struct amdtp_am824 *p = s->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) unsigned int pcm_frames = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) for (i = 0; i < packets; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) const struct pkt_desc *desc = descs + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) __be32 *buf = desc->ctx_payload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) unsigned int data_blocks = desc->data_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (pcm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) read_pcm_s32(s, pcm, buf, data_blocks, pcm_frames);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) pcm_frames += data_blocks * p->frame_multiplier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (p->midi_ports) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) read_midi_messages(s, buf, data_blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) desc->data_block_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return pcm_frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * amdtp_am824_init - initialize an AMDTP stream structure to handle AM824
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * data block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * @s: the AMDTP stream to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * @unit: the target of the stream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * @dir: the direction of stream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * @flags: the packet transmission method to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) int amdtp_am824_init(struct amdtp_stream *s, struct fw_unit *unit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) enum amdtp_stream_direction dir, enum cip_flags flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) amdtp_stream_process_ctx_payloads_t process_ctx_payloads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) if (dir == AMDTP_IN_STREAM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) process_ctx_payloads = process_ir_ctx_payloads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) process_ctx_payloads = process_it_ctx_payloads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) return amdtp_stream_init(s, unit, dir, flags, CIP_FMT_AM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) process_ctx_payloads, sizeof(struct amdtp_am824));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) EXPORT_SYMBOL_GPL(amdtp_am824_init);