1da607e19SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2d67c46b9STakashi Sakamoto /* 3d67c46b9STakashi Sakamoto * Audio and Music Data Transmission Protocol (IEC 61883-6) streams 4d67c46b9STakashi Sakamoto * with Common Isochronous Packet (IEC 61883-1) headers 5d67c46b9STakashi Sakamoto * 6d67c46b9STakashi Sakamoto * Copyright (c) Clemens Ladisch <clemens@ladisch.de> 7d67c46b9STakashi Sakamoto */ 8d67c46b9STakashi Sakamoto 9d67c46b9STakashi Sakamoto #include <linux/device.h> 10d67c46b9STakashi Sakamoto #include <linux/err.h> 11d67c46b9STakashi Sakamoto #include <linux/firewire.h> 12d67c46b9STakashi Sakamoto #include <linux/module.h> 13d67c46b9STakashi Sakamoto #include <linux/slab.h> 14d67c46b9STakashi Sakamoto #include <sound/pcm.h> 15d67c46b9STakashi Sakamoto #include <sound/pcm_params.h> 16d67c46b9STakashi Sakamoto #include "amdtp-stream.h" 17d67c46b9STakashi Sakamoto 18d67c46b9STakashi Sakamoto #define TICKS_PER_CYCLE 3072 19d67c46b9STakashi Sakamoto #define CYCLES_PER_SECOND 8000 20d67c46b9STakashi Sakamoto #define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND) 21d67c46b9STakashi Sakamoto 220c95c1d6STakashi Sakamoto /* Always support Linux tracing subsystem. */ 230c95c1d6STakashi Sakamoto #define CREATE_TRACE_POINTS 240c95c1d6STakashi Sakamoto #include "amdtp-stream-trace.h" 250c95c1d6STakashi Sakamoto 26d67c46b9STakashi Sakamoto #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */ 27d67c46b9STakashi Sakamoto 28d67c46b9STakashi Sakamoto /* isochronous header parameters */ 29d67c46b9STakashi Sakamoto #define ISO_DATA_LENGTH_SHIFT 16 303b196c39STakashi Sakamoto #define TAG_NO_CIP_HEADER 0 31d67c46b9STakashi Sakamoto #define TAG_CIP 1 32d67c46b9STakashi Sakamoto 33d67c46b9STakashi Sakamoto /* common isochronous packet header parameters */ 34d67c46b9STakashi Sakamoto #define CIP_EOH_SHIFT 31 35d67c46b9STakashi Sakamoto #define CIP_EOH (1u << CIP_EOH_SHIFT) 36d67c46b9STakashi Sakamoto #define CIP_EOH_MASK 0x80000000 37d67c46b9STakashi Sakamoto #define CIP_SID_SHIFT 24 38d67c46b9STakashi Sakamoto #define CIP_SID_MASK 0x3f000000 39d67c46b9STakashi Sakamoto #define CIP_DBS_MASK 0x00ff0000 40d67c46b9STakashi Sakamoto #define CIP_DBS_SHIFT 16 419863874fSTakashi Sakamoto #define CIP_SPH_MASK 0x00000400 429863874fSTakashi Sakamoto #define CIP_SPH_SHIFT 10 43d67c46b9STakashi Sakamoto #define CIP_DBC_MASK 0x000000ff 44d67c46b9STakashi Sakamoto #define CIP_FMT_SHIFT 24 45d67c46b9STakashi Sakamoto #define CIP_FMT_MASK 0x3f000000 46d67c46b9STakashi Sakamoto #define CIP_FDF_MASK 0x00ff0000 47d67c46b9STakashi Sakamoto #define CIP_FDF_SHIFT 16 48d67c46b9STakashi Sakamoto #define CIP_SYT_MASK 0x0000ffff 49d67c46b9STakashi Sakamoto #define CIP_SYT_NO_INFO 0xffff 50d67c46b9STakashi Sakamoto 5151c29fd2STakashi Sakamoto /* Audio and Music transfer protocol specific parameters */ 52d67c46b9STakashi Sakamoto #define CIP_FMT_AM 0x10 53d67c46b9STakashi Sakamoto #define AMDTP_FDF_NO_DATA 0xff 54d67c46b9STakashi Sakamoto 55d67c46b9STakashi Sakamoto /* TODO: make these configurable */ 56d67c46b9STakashi Sakamoto #define INTERRUPT_INTERVAL 16 57d67c46b9STakashi Sakamoto #define QUEUE_LENGTH 48 58d67c46b9STakashi Sakamoto 59f11453c7STakashi Sakamoto // For iso header, tstamp and 2 CIP header. 60f11453c7STakashi Sakamoto #define IR_CTX_HEADER_SIZE_CIP 16 61f11453c7STakashi Sakamoto // For iso header and tstamp. 62f11453c7STakashi Sakamoto #define IR_CTX_HEADER_SIZE_NO_CIP 8 63cc4f8e91STakashi Sakamoto #define HEADER_TSTAMP_MASK 0x0000ffff 64d67c46b9STakashi Sakamoto 65b18f0cfaSTakashi Sakamoto #define IT_PKT_HEADER_SIZE_CIP 8 // For 2 CIP header. 66b18f0cfaSTakashi Sakamoto #define IT_PKT_HEADER_SIZE_NO_CIP 0 // Nothing. 67b18f0cfaSTakashi Sakamoto 68d67c46b9STakashi Sakamoto static void pcm_period_tasklet(unsigned long data); 69d67c46b9STakashi Sakamoto 70d67c46b9STakashi Sakamoto /** 71d67c46b9STakashi Sakamoto * amdtp_stream_init - initialize an AMDTP stream structure 72d67c46b9STakashi Sakamoto * @s: the AMDTP stream to initialize 73d67c46b9STakashi Sakamoto * @unit: the target of the stream 74d67c46b9STakashi Sakamoto * @dir: the direction of stream 75d67c46b9STakashi Sakamoto * @flags: the packet transmission method to use 765955815eSTakashi Sakamoto * @fmt: the value of fmt field in CIP header 77df075feeSTakashi Sakamoto * @process_data_blocks: callback handler to process data blocks 78df075feeSTakashi Sakamoto * @protocol_size: the size to allocate newly for protocol 79d67c46b9STakashi Sakamoto */ 80d67c46b9STakashi Sakamoto int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit, 815955815eSTakashi Sakamoto enum amdtp_stream_direction dir, enum cip_flags flags, 82df075feeSTakashi Sakamoto unsigned int fmt, 83df075feeSTakashi Sakamoto amdtp_stream_process_data_blocks_t process_data_blocks, 84df075feeSTakashi Sakamoto unsigned int protocol_size) 85d67c46b9STakashi Sakamoto { 86df075feeSTakashi Sakamoto if (process_data_blocks == NULL) 87df075feeSTakashi Sakamoto return -EINVAL; 88df075feeSTakashi Sakamoto 89df075feeSTakashi Sakamoto s->protocol = kzalloc(protocol_size, GFP_KERNEL); 90df075feeSTakashi Sakamoto if (!s->protocol) 91df075feeSTakashi Sakamoto return -ENOMEM; 92df075feeSTakashi Sakamoto 93d67c46b9STakashi Sakamoto s->unit = unit; 94d67c46b9STakashi Sakamoto s->direction = dir; 95d67c46b9STakashi Sakamoto s->flags = flags; 96d67c46b9STakashi Sakamoto s->context = ERR_PTR(-1); 97d67c46b9STakashi Sakamoto mutex_init(&s->mutex); 98d67c46b9STakashi Sakamoto tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s); 99d67c46b9STakashi Sakamoto s->packet_index = 0; 100d67c46b9STakashi Sakamoto 101d67c46b9STakashi Sakamoto init_waitqueue_head(&s->callback_wait); 102d67c46b9STakashi Sakamoto s->callbacked = false; 103d67c46b9STakashi Sakamoto 1045955815eSTakashi Sakamoto s->fmt = fmt; 105df075feeSTakashi Sakamoto s->process_data_blocks = process_data_blocks; 106d67c46b9STakashi Sakamoto 107*3baf3053STakashi Sakamoto if (dir == AMDTP_OUT_STREAM) 108*3baf3053STakashi Sakamoto s->ctx_data.rx.syt_override = -1; 109*3baf3053STakashi Sakamoto 110d67c46b9STakashi Sakamoto return 0; 111d67c46b9STakashi Sakamoto } 112d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_init); 113d67c46b9STakashi Sakamoto 114d67c46b9STakashi Sakamoto /** 115d67c46b9STakashi Sakamoto * amdtp_stream_destroy - free stream resources 116d67c46b9STakashi Sakamoto * @s: the AMDTP stream to destroy 117d67c46b9STakashi Sakamoto */ 118d67c46b9STakashi Sakamoto void amdtp_stream_destroy(struct amdtp_stream *s) 119d67c46b9STakashi Sakamoto { 12044c376b9STakashi Sakamoto /* Not initialized. */ 12144c376b9STakashi Sakamoto if (s->protocol == NULL) 12244c376b9STakashi Sakamoto return; 12344c376b9STakashi Sakamoto 124d67c46b9STakashi Sakamoto WARN_ON(amdtp_stream_running(s)); 125df075feeSTakashi Sakamoto kfree(s->protocol); 126d67c46b9STakashi Sakamoto mutex_destroy(&s->mutex); 127d67c46b9STakashi Sakamoto } 128d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_destroy); 129d67c46b9STakashi Sakamoto 130d67c46b9STakashi Sakamoto const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = { 131d67c46b9STakashi Sakamoto [CIP_SFC_32000] = 8, 132d67c46b9STakashi Sakamoto [CIP_SFC_44100] = 8, 133d67c46b9STakashi Sakamoto [CIP_SFC_48000] = 8, 134d67c46b9STakashi Sakamoto [CIP_SFC_88200] = 16, 135d67c46b9STakashi Sakamoto [CIP_SFC_96000] = 16, 136d67c46b9STakashi Sakamoto [CIP_SFC_176400] = 32, 137d67c46b9STakashi Sakamoto [CIP_SFC_192000] = 32, 138d67c46b9STakashi Sakamoto }; 139d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_syt_intervals); 140d67c46b9STakashi Sakamoto 141d67c46b9STakashi Sakamoto const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = { 142d67c46b9STakashi Sakamoto [CIP_SFC_32000] = 32000, 143d67c46b9STakashi Sakamoto [CIP_SFC_44100] = 44100, 144d67c46b9STakashi Sakamoto [CIP_SFC_48000] = 48000, 145d67c46b9STakashi Sakamoto [CIP_SFC_88200] = 88200, 146d67c46b9STakashi Sakamoto [CIP_SFC_96000] = 96000, 147d67c46b9STakashi Sakamoto [CIP_SFC_176400] = 176400, 148d67c46b9STakashi Sakamoto [CIP_SFC_192000] = 192000, 149d67c46b9STakashi Sakamoto }; 150d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_rate_table); 151d67c46b9STakashi Sakamoto 15259502295STakashi Sakamoto static int apply_constraint_to_size(struct snd_pcm_hw_params *params, 15359502295STakashi Sakamoto struct snd_pcm_hw_rule *rule) 15459502295STakashi Sakamoto { 15559502295STakashi Sakamoto struct snd_interval *s = hw_param_interval(params, rule->var); 15659502295STakashi Sakamoto const struct snd_interval *r = 15759502295STakashi Sakamoto hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE); 158826b5de9STakashi Sakamoto struct snd_interval t = {0}; 159826b5de9STakashi Sakamoto unsigned int step = 0; 16059502295STakashi Sakamoto int i; 16159502295STakashi Sakamoto 16259502295STakashi Sakamoto for (i = 0; i < CIP_SFC_COUNT; ++i) { 163826b5de9STakashi Sakamoto if (snd_interval_test(r, amdtp_rate_table[i])) 164826b5de9STakashi Sakamoto step = max(step, amdtp_syt_intervals[i]); 16559502295STakashi Sakamoto } 16659502295STakashi Sakamoto 167826b5de9STakashi Sakamoto t.min = roundup(s->min, step); 168826b5de9STakashi Sakamoto t.max = rounddown(s->max, step); 169826b5de9STakashi Sakamoto t.integer = 1; 17059502295STakashi Sakamoto 17159502295STakashi Sakamoto return snd_interval_refine(s, &t); 17259502295STakashi Sakamoto } 17359502295STakashi Sakamoto 174d67c46b9STakashi Sakamoto /** 175d67c46b9STakashi Sakamoto * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream 176d67c46b9STakashi Sakamoto * @s: the AMDTP stream, which must be initialized. 177d67c46b9STakashi Sakamoto * @runtime: the PCM substream runtime 178d67c46b9STakashi Sakamoto */ 179d67c46b9STakashi Sakamoto int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s, 180d67c46b9STakashi Sakamoto struct snd_pcm_runtime *runtime) 181d67c46b9STakashi Sakamoto { 18255799c5aSTakashi Sakamoto struct snd_pcm_hardware *hw = &runtime->hw; 183d67c46b9STakashi Sakamoto int err; 184d67c46b9STakashi Sakamoto 18555799c5aSTakashi Sakamoto hw->info = SNDRV_PCM_INFO_BATCH | 18655799c5aSTakashi Sakamoto SNDRV_PCM_INFO_BLOCK_TRANSFER | 18755799c5aSTakashi Sakamoto SNDRV_PCM_INFO_INTERLEAVED | 18855799c5aSTakashi Sakamoto SNDRV_PCM_INFO_JOINT_DUPLEX | 18955799c5aSTakashi Sakamoto SNDRV_PCM_INFO_MMAP | 19055799c5aSTakashi Sakamoto SNDRV_PCM_INFO_MMAP_VALID; 19155799c5aSTakashi Sakamoto 19255799c5aSTakashi Sakamoto /* SNDRV_PCM_INFO_BATCH */ 19355799c5aSTakashi Sakamoto hw->periods_min = 2; 19455799c5aSTakashi Sakamoto hw->periods_max = UINT_MAX; 19555799c5aSTakashi Sakamoto 19655799c5aSTakashi Sakamoto /* bytes for a frame */ 19755799c5aSTakashi Sakamoto hw->period_bytes_min = 4 * hw->channels_max; 19855799c5aSTakashi Sakamoto 19955799c5aSTakashi Sakamoto /* Just to prevent from allocating much pages. */ 20055799c5aSTakashi Sakamoto hw->period_bytes_max = hw->period_bytes_min * 2048; 20155799c5aSTakashi Sakamoto hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min; 20255799c5aSTakashi Sakamoto 203d67c46b9STakashi Sakamoto /* 204d67c46b9STakashi Sakamoto * Currently firewire-lib processes 16 packets in one software 205d67c46b9STakashi Sakamoto * interrupt callback. This equals to 2msec but actually the 206d67c46b9STakashi Sakamoto * interval of the interrupts has a jitter. 207d67c46b9STakashi Sakamoto * Additionally, even if adding a constraint to fit period size to 208d67c46b9STakashi Sakamoto * 2msec, actual calculated frames per period doesn't equal to 2msec, 209d67c46b9STakashi Sakamoto * depending on sampling rate. 210d67c46b9STakashi Sakamoto * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec. 211d67c46b9STakashi Sakamoto * Here let us use 5msec for safe period interrupt. 212d67c46b9STakashi Sakamoto */ 213d67c46b9STakashi Sakamoto err = snd_pcm_hw_constraint_minmax(runtime, 214d67c46b9STakashi Sakamoto SNDRV_PCM_HW_PARAM_PERIOD_TIME, 215d67c46b9STakashi Sakamoto 5000, UINT_MAX); 216d67c46b9STakashi Sakamoto if (err < 0) 217d67c46b9STakashi Sakamoto goto end; 218d67c46b9STakashi Sakamoto 219d67c46b9STakashi Sakamoto /* Non-Blocking stream has no more constraints */ 220d67c46b9STakashi Sakamoto if (!(s->flags & CIP_BLOCKING)) 221d67c46b9STakashi Sakamoto goto end; 222d67c46b9STakashi Sakamoto 223d67c46b9STakashi Sakamoto /* 224d67c46b9STakashi Sakamoto * One AMDTP packet can include some frames. In blocking mode, the 225d67c46b9STakashi Sakamoto * number equals to SYT_INTERVAL. So the number is 8, 16 or 32, 226d67c46b9STakashi Sakamoto * depending on its sampling rate. For accurate period interrupt, it's 227d67c46b9STakashi Sakamoto * preferrable to align period/buffer sizes to current SYT_INTERVAL. 228d67c46b9STakashi Sakamoto */ 22959502295STakashi Sakamoto err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 23059502295STakashi Sakamoto apply_constraint_to_size, NULL, 231826b5de9STakashi Sakamoto SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 23259502295STakashi Sakamoto SNDRV_PCM_HW_PARAM_RATE, -1); 233d67c46b9STakashi Sakamoto if (err < 0) 234d67c46b9STakashi Sakamoto goto end; 23559502295STakashi Sakamoto err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 23659502295STakashi Sakamoto apply_constraint_to_size, NULL, 237826b5de9STakashi Sakamoto SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 23859502295STakashi Sakamoto SNDRV_PCM_HW_PARAM_RATE, -1); 23959502295STakashi Sakamoto if (err < 0) 24059502295STakashi Sakamoto goto end; 241d67c46b9STakashi Sakamoto end: 242d67c46b9STakashi Sakamoto return err; 243d67c46b9STakashi Sakamoto } 244d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints); 245d67c46b9STakashi Sakamoto 246d67c46b9STakashi Sakamoto /** 247d67c46b9STakashi Sakamoto * amdtp_stream_set_parameters - set stream parameters 248d67c46b9STakashi Sakamoto * @s: the AMDTP stream to configure 249d67c46b9STakashi Sakamoto * @rate: the sample rate 250df075feeSTakashi Sakamoto * @data_block_quadlets: the size of a data block in quadlet unit 251d67c46b9STakashi Sakamoto * 252d67c46b9STakashi Sakamoto * The parameters must be set before the stream is started, and must not be 253d67c46b9STakashi Sakamoto * changed while the stream is running. 254d67c46b9STakashi Sakamoto */ 255df075feeSTakashi Sakamoto int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate, 256df075feeSTakashi Sakamoto unsigned int data_block_quadlets) 257d67c46b9STakashi Sakamoto { 258df075feeSTakashi Sakamoto unsigned int sfc; 259d67c46b9STakashi Sakamoto 260d67c46b9STakashi Sakamoto for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) { 261d67c46b9STakashi Sakamoto if (amdtp_rate_table[sfc] == rate) 262d67c46b9STakashi Sakamoto break; 263d67c46b9STakashi Sakamoto } 264d67c46b9STakashi Sakamoto if (sfc == ARRAY_SIZE(amdtp_rate_table)) 265d67c46b9STakashi Sakamoto return -EINVAL; 266d67c46b9STakashi Sakamoto 267d67c46b9STakashi Sakamoto s->sfc = sfc; 268df075feeSTakashi Sakamoto s->data_block_quadlets = data_block_quadlets; 269d67c46b9STakashi Sakamoto s->syt_interval = amdtp_syt_intervals[sfc]; 270d67c46b9STakashi Sakamoto 271d3d10a4aSTakashi Sakamoto // default buffering in the device. 272d3d10a4aSTakashi Sakamoto if (s->direction == AMDTP_OUT_STREAM) { 273d3d10a4aSTakashi Sakamoto s->ctx_data.rx.transfer_delay = 274d3d10a4aSTakashi Sakamoto TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE; 275d3d10a4aSTakashi Sakamoto 276d3d10a4aSTakashi Sakamoto if (s->flags & CIP_BLOCKING) { 277d3d10a4aSTakashi Sakamoto // additional buffering needed to adjust for no-data 278d3d10a4aSTakashi Sakamoto // packets. 279d3d10a4aSTakashi Sakamoto s->ctx_data.rx.transfer_delay += 280d3d10a4aSTakashi Sakamoto TICKS_PER_SECOND * s->syt_interval / rate; 281d3d10a4aSTakashi Sakamoto } 282d3d10a4aSTakashi Sakamoto } 283d67c46b9STakashi Sakamoto 284d67c46b9STakashi Sakamoto return 0; 285d67c46b9STakashi Sakamoto } 286d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_set_parameters); 287d67c46b9STakashi Sakamoto 288d67c46b9STakashi Sakamoto /** 289d67c46b9STakashi Sakamoto * amdtp_stream_get_max_payload - get the stream's packet size 290d67c46b9STakashi Sakamoto * @s: the AMDTP stream 291d67c46b9STakashi Sakamoto * 292d67c46b9STakashi Sakamoto * This function must not be called before the stream has been configured 293d67c46b9STakashi Sakamoto * with amdtp_stream_set_parameters(). 294d67c46b9STakashi Sakamoto */ 295d67c46b9STakashi Sakamoto unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s) 296d67c46b9STakashi Sakamoto { 297d67c46b9STakashi Sakamoto unsigned int multiplier = 1; 29807ea238cSTakashi Sakamoto unsigned int cip_header_size = 0; 299d67c46b9STakashi Sakamoto 300d67c46b9STakashi Sakamoto if (s->flags & CIP_JUMBO_PAYLOAD) 301d67c46b9STakashi Sakamoto multiplier = 5; 3023b196c39STakashi Sakamoto if (!(s->flags & CIP_NO_HEADER)) 30307ea238cSTakashi Sakamoto cip_header_size = sizeof(__be32) * 2; 304d67c46b9STakashi Sakamoto 30507ea238cSTakashi Sakamoto return cip_header_size + 30607ea238cSTakashi Sakamoto s->syt_interval * s->data_block_quadlets * sizeof(__be32) * multiplier; 307d67c46b9STakashi Sakamoto } 308d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_get_max_payload); 309d67c46b9STakashi Sakamoto 310d67c46b9STakashi Sakamoto /** 311d67c46b9STakashi Sakamoto * amdtp_stream_pcm_prepare - prepare PCM device for running 312d67c46b9STakashi Sakamoto * @s: the AMDTP stream 313d67c46b9STakashi Sakamoto * 314d67c46b9STakashi Sakamoto * This function should be called from the PCM device's .prepare callback. 315d67c46b9STakashi Sakamoto */ 316d67c46b9STakashi Sakamoto void amdtp_stream_pcm_prepare(struct amdtp_stream *s) 317d67c46b9STakashi Sakamoto { 318d67c46b9STakashi Sakamoto tasklet_kill(&s->period_tasklet); 319d67c46b9STakashi Sakamoto s->pcm_buffer_pointer = 0; 320d67c46b9STakashi Sakamoto s->pcm_period_pointer = 0; 321d67c46b9STakashi Sakamoto } 322d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_prepare); 323d67c46b9STakashi Sakamoto 324d67c46b9STakashi Sakamoto static unsigned int calculate_data_blocks(struct amdtp_stream *s, 325d67c46b9STakashi Sakamoto unsigned int syt) 326d67c46b9STakashi Sakamoto { 327d67c46b9STakashi Sakamoto unsigned int phase, data_blocks; 328d67c46b9STakashi Sakamoto 329d67c46b9STakashi Sakamoto /* Blocking mode. */ 330d67c46b9STakashi Sakamoto if (s->flags & CIP_BLOCKING) { 331d67c46b9STakashi Sakamoto /* This module generate empty packet for 'no data'. */ 332d67c46b9STakashi Sakamoto if (syt == CIP_SYT_NO_INFO) 333d67c46b9STakashi Sakamoto data_blocks = 0; 334d67c46b9STakashi Sakamoto else 335d67c46b9STakashi Sakamoto data_blocks = s->syt_interval; 336d67c46b9STakashi Sakamoto /* Non-blocking mode. */ 337d67c46b9STakashi Sakamoto } else { 338d67c46b9STakashi Sakamoto if (!cip_sfc_is_base_44100(s->sfc)) { 339d3d10a4aSTakashi Sakamoto // Sample_rate / 8000 is an integer, and precomputed. 340d3d10a4aSTakashi Sakamoto data_blocks = s->ctx_data.rx.data_block_state; 341d67c46b9STakashi Sakamoto } else { 342d3d10a4aSTakashi Sakamoto phase = s->ctx_data.rx.data_block_state; 343d67c46b9STakashi Sakamoto 344d67c46b9STakashi Sakamoto /* 345d67c46b9STakashi Sakamoto * This calculates the number of data blocks per packet so that 346d67c46b9STakashi Sakamoto * 1) the overall rate is correct and exactly synchronized to 347d67c46b9STakashi Sakamoto * the bus clock, and 348d67c46b9STakashi Sakamoto * 2) packets with a rounded-up number of blocks occur as early 349d67c46b9STakashi Sakamoto * as possible in the sequence (to prevent underruns of the 350d67c46b9STakashi Sakamoto * device's buffer). 351d67c46b9STakashi Sakamoto */ 352d67c46b9STakashi Sakamoto if (s->sfc == CIP_SFC_44100) 353d67c46b9STakashi Sakamoto /* 6 6 5 6 5 6 5 ... */ 354d67c46b9STakashi Sakamoto data_blocks = 5 + ((phase & 1) ^ 355d67c46b9STakashi Sakamoto (phase == 0 || phase >= 40)); 356d67c46b9STakashi Sakamoto else 357d67c46b9STakashi Sakamoto /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */ 358d67c46b9STakashi Sakamoto data_blocks = 11 * (s->sfc >> 1) + (phase == 0); 359d67c46b9STakashi Sakamoto if (++phase >= (80 >> (s->sfc >> 1))) 360d67c46b9STakashi Sakamoto phase = 0; 361d3d10a4aSTakashi Sakamoto s->ctx_data.rx.data_block_state = phase; 362d67c46b9STakashi Sakamoto } 363d67c46b9STakashi Sakamoto } 364d67c46b9STakashi Sakamoto 365d67c46b9STakashi Sakamoto return data_blocks; 366d67c46b9STakashi Sakamoto } 367d67c46b9STakashi Sakamoto 368d67c46b9STakashi Sakamoto static unsigned int calculate_syt(struct amdtp_stream *s, 369d67c46b9STakashi Sakamoto unsigned int cycle) 370d67c46b9STakashi Sakamoto { 371d67c46b9STakashi Sakamoto unsigned int syt_offset, phase, index, syt; 372d67c46b9STakashi Sakamoto 373d3d10a4aSTakashi Sakamoto if (s->ctx_data.rx.last_syt_offset < TICKS_PER_CYCLE) { 374d67c46b9STakashi Sakamoto if (!cip_sfc_is_base_44100(s->sfc)) 375d3d10a4aSTakashi Sakamoto syt_offset = s->ctx_data.rx.last_syt_offset + 376d3d10a4aSTakashi Sakamoto s->ctx_data.rx.syt_offset_state; 377d67c46b9STakashi Sakamoto else { 378d67c46b9STakashi Sakamoto /* 379d67c46b9STakashi Sakamoto * The time, in ticks, of the n'th SYT_INTERVAL sample is: 380d67c46b9STakashi Sakamoto * n * SYT_INTERVAL * 24576000 / sample_rate 381d67c46b9STakashi Sakamoto * Modulo TICKS_PER_CYCLE, the difference between successive 382d67c46b9STakashi Sakamoto * elements is about 1386.23. Rounding the results of this 383d67c46b9STakashi Sakamoto * formula to the SYT precision results in a sequence of 384d67c46b9STakashi Sakamoto * differences that begins with: 385d67c46b9STakashi Sakamoto * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ... 386d67c46b9STakashi Sakamoto * This code generates _exactly_ the same sequence. 387d67c46b9STakashi Sakamoto */ 388d3d10a4aSTakashi Sakamoto phase = s->ctx_data.rx.syt_offset_state; 389d67c46b9STakashi Sakamoto index = phase % 13; 390d3d10a4aSTakashi Sakamoto syt_offset = s->ctx_data.rx.last_syt_offset; 391d67c46b9STakashi Sakamoto syt_offset += 1386 + ((index && !(index & 3)) || 392d67c46b9STakashi Sakamoto phase == 146); 393d67c46b9STakashi Sakamoto if (++phase >= 147) 394d67c46b9STakashi Sakamoto phase = 0; 395d3d10a4aSTakashi Sakamoto s->ctx_data.rx.syt_offset_state = phase; 396d67c46b9STakashi Sakamoto } 397d67c46b9STakashi Sakamoto } else 398d3d10a4aSTakashi Sakamoto syt_offset = s->ctx_data.rx.last_syt_offset - TICKS_PER_CYCLE; 399d3d10a4aSTakashi Sakamoto s->ctx_data.rx.last_syt_offset = syt_offset; 400d67c46b9STakashi Sakamoto 401d67c46b9STakashi Sakamoto if (syt_offset < TICKS_PER_CYCLE) { 402d3d10a4aSTakashi Sakamoto syt_offset += s->ctx_data.rx.transfer_delay; 403d67c46b9STakashi Sakamoto syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12; 404d67c46b9STakashi Sakamoto syt += syt_offset % TICKS_PER_CYCLE; 405d67c46b9STakashi Sakamoto 406d67c46b9STakashi Sakamoto return syt & CIP_SYT_MASK; 407d67c46b9STakashi Sakamoto } else { 408d67c46b9STakashi Sakamoto return CIP_SYT_NO_INFO; 409d67c46b9STakashi Sakamoto } 410d67c46b9STakashi Sakamoto } 411d67c46b9STakashi Sakamoto 412d67c46b9STakashi Sakamoto static void update_pcm_pointers(struct amdtp_stream *s, 413d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm, 414d67c46b9STakashi Sakamoto unsigned int frames) 415d67c46b9STakashi Sakamoto { 416d67c46b9STakashi Sakamoto unsigned int ptr; 417d67c46b9STakashi Sakamoto 418d67c46b9STakashi Sakamoto ptr = s->pcm_buffer_pointer + frames; 419d67c46b9STakashi Sakamoto if (ptr >= pcm->runtime->buffer_size) 420d67c46b9STakashi Sakamoto ptr -= pcm->runtime->buffer_size; 4216aa7de05SMark Rutland WRITE_ONCE(s->pcm_buffer_pointer, ptr); 422d67c46b9STakashi Sakamoto 423d67c46b9STakashi Sakamoto s->pcm_period_pointer += frames; 424d67c46b9STakashi Sakamoto if (s->pcm_period_pointer >= pcm->runtime->period_size) { 425d67c46b9STakashi Sakamoto s->pcm_period_pointer -= pcm->runtime->period_size; 426d67c46b9STakashi Sakamoto tasklet_hi_schedule(&s->period_tasklet); 427d67c46b9STakashi Sakamoto } 428d67c46b9STakashi Sakamoto } 429d67c46b9STakashi Sakamoto 430d67c46b9STakashi Sakamoto static void pcm_period_tasklet(unsigned long data) 431d67c46b9STakashi Sakamoto { 432d67c46b9STakashi Sakamoto struct amdtp_stream *s = (void *)data; 4336aa7de05SMark Rutland struct snd_pcm_substream *pcm = READ_ONCE(s->pcm); 434d67c46b9STakashi Sakamoto 435d67c46b9STakashi Sakamoto if (pcm) 436d67c46b9STakashi Sakamoto snd_pcm_period_elapsed(pcm); 437d67c46b9STakashi Sakamoto } 438d67c46b9STakashi Sakamoto 4396007bf54STakashi Sakamoto static int queue_packet(struct amdtp_stream *s, struct fw_iso_packet *params) 440d67c46b9STakashi Sakamoto { 4416007bf54STakashi Sakamoto int err; 442d67c46b9STakashi Sakamoto 4436007bf54STakashi Sakamoto params->interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL); 4446007bf54STakashi Sakamoto params->tag = s->tag; 4456007bf54STakashi Sakamoto params->sy = 0; 446d67c46b9STakashi Sakamoto 4476007bf54STakashi Sakamoto err = fw_iso_context_queue(s->context, params, &s->buffer.iso_buffer, 448d67c46b9STakashi Sakamoto s->buffer.packets[s->packet_index].offset); 449d67c46b9STakashi Sakamoto if (err < 0) { 450d67c46b9STakashi Sakamoto dev_err(&s->unit->device, "queueing error: %d\n", err); 451d67c46b9STakashi Sakamoto goto end; 452d67c46b9STakashi Sakamoto } 453d67c46b9STakashi Sakamoto 454d67c46b9STakashi Sakamoto if (++s->packet_index >= QUEUE_LENGTH) 455d67c46b9STakashi Sakamoto s->packet_index = 0; 456d67c46b9STakashi Sakamoto end: 457d67c46b9STakashi Sakamoto return err; 458d67c46b9STakashi Sakamoto } 459d67c46b9STakashi Sakamoto 460d67c46b9STakashi Sakamoto static inline int queue_out_packet(struct amdtp_stream *s, 461b18f0cfaSTakashi Sakamoto struct fw_iso_packet *params) 462d67c46b9STakashi Sakamoto { 463b18f0cfaSTakashi Sakamoto params->skip = 464b18f0cfaSTakashi Sakamoto !!(params->header_length == 0 && params->payload_length == 0); 4656007bf54STakashi Sakamoto return queue_packet(s, params); 466d67c46b9STakashi Sakamoto } 467d67c46b9STakashi Sakamoto 4686007bf54STakashi Sakamoto static inline int queue_in_packet(struct amdtp_stream *s, 4696007bf54STakashi Sakamoto struct fw_iso_packet *params) 470d67c46b9STakashi Sakamoto { 4716007bf54STakashi Sakamoto // Queue one packet for IR context. 4726007bf54STakashi Sakamoto params->header_length = s->ctx_data.tx.ctx_header_size; 4736007bf54STakashi Sakamoto params->payload_length = s->ctx_data.tx.max_ctx_payload_length; 4746007bf54STakashi Sakamoto params->skip = false; 4756007bf54STakashi Sakamoto return queue_packet(s, params); 476d67c46b9STakashi Sakamoto } 477d67c46b9STakashi Sakamoto 478252219c7STakashi Sakamoto static void generate_cip_header(struct amdtp_stream *s, __be32 cip_header[2], 479860d798cSTakashi Sakamoto unsigned int data_block_counter, unsigned int syt) 480252219c7STakashi Sakamoto { 481252219c7STakashi Sakamoto cip_header[0] = cpu_to_be32(READ_ONCE(s->source_node_id_field) | 482252219c7STakashi Sakamoto (s->data_block_quadlets << CIP_DBS_SHIFT) | 483252219c7STakashi Sakamoto ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) | 484860d798cSTakashi Sakamoto data_block_counter); 485252219c7STakashi Sakamoto cip_header[1] = cpu_to_be32(CIP_EOH | 486252219c7STakashi Sakamoto ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) | 487252219c7STakashi Sakamoto ((s->ctx_data.rx.fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) | 488252219c7STakashi Sakamoto (syt & CIP_SYT_MASK)); 489252219c7STakashi Sakamoto } 490252219c7STakashi Sakamoto 4916bc1a269STakashi Sakamoto static void build_it_pkt_header(struct amdtp_stream *s, unsigned int cycle, 4926bc1a269STakashi Sakamoto struct fw_iso_packet *params, 493860d798cSTakashi Sakamoto unsigned int data_blocks, 494860d798cSTakashi Sakamoto unsigned int data_block_counter, 495860d798cSTakashi Sakamoto unsigned int syt, unsigned int index) 496d67c46b9STakashi Sakamoto { 4970ebf3cebSTakashi Sakamoto unsigned int payload_length; 49816be4589STakashi Sakamoto __be32 *cip_header; 499d67c46b9STakashi Sakamoto 5000ebf3cebSTakashi Sakamoto payload_length = data_blocks * sizeof(__be32) * s->data_block_quadlets; 5010ebf3cebSTakashi Sakamoto params->payload_length = payload_length; 5020ebf3cebSTakashi Sakamoto 503b18f0cfaSTakashi Sakamoto if (!(s->flags & CIP_NO_HEADER)) { 5046bc1a269STakashi Sakamoto cip_header = (__be32 *)params->header; 505860d798cSTakashi Sakamoto generate_cip_header(s, cip_header, data_block_counter, syt); 5066bc1a269STakashi Sakamoto params->header_length = 2 * sizeof(__be32); 5070ebf3cebSTakashi Sakamoto payload_length += params->header_length; 508b18f0cfaSTakashi Sakamoto } else { 509b18f0cfaSTakashi Sakamoto cip_header = NULL; 510b18f0cfaSTakashi Sakamoto } 511d67c46b9STakashi Sakamoto 512213fa989STakashi Sakamoto trace_amdtp_packet(s, cycle, cip_header, payload_length, data_blocks, 513860d798cSTakashi Sakamoto data_block_counter, index); 5143b196c39STakashi Sakamoto } 5153b196c39STakashi Sakamoto 516e335425bSTakashi Sakamoto static int check_cip_header(struct amdtp_stream *s, const __be32 *buf, 517e335425bSTakashi Sakamoto unsigned int payload_length, 518a35463d1STakashi Sakamoto unsigned int *data_blocks, 519a35463d1STakashi Sakamoto unsigned int *data_block_counter, unsigned int *syt) 520d67c46b9STakashi Sakamoto { 521d67c46b9STakashi Sakamoto u32 cip_header[2]; 522e335425bSTakashi Sakamoto unsigned int sph; 523e335425bSTakashi Sakamoto unsigned int fmt; 524e335425bSTakashi Sakamoto unsigned int fdf; 525a35463d1STakashi Sakamoto unsigned int dbc; 526d67c46b9STakashi Sakamoto bool lost; 527d67c46b9STakashi Sakamoto 528e335425bSTakashi Sakamoto cip_header[0] = be32_to_cpu(buf[0]); 529e335425bSTakashi Sakamoto cip_header[1] = be32_to_cpu(buf[1]); 530d67c46b9STakashi Sakamoto 531d67c46b9STakashi Sakamoto /* 532d67c46b9STakashi Sakamoto * This module supports 'Two-quadlet CIP header with SYT field'. 533d67c46b9STakashi Sakamoto * For convenience, also check FMT field is AM824 or not. 534d67c46b9STakashi Sakamoto */ 5352128f78fSTakashi Sakamoto if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) || 5362128f78fSTakashi Sakamoto ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) && 5372128f78fSTakashi Sakamoto (!(s->flags & CIP_HEADER_WITHOUT_EOH))) { 538d67c46b9STakashi Sakamoto dev_info_ratelimited(&s->unit->device, 539d67c46b9STakashi Sakamoto "Invalid CIP header for AMDTP: %08X:%08X\n", 540d67c46b9STakashi Sakamoto cip_header[0], cip_header[1]); 541e335425bSTakashi Sakamoto return -EAGAIN; 542d67c46b9STakashi Sakamoto } 543d67c46b9STakashi Sakamoto 544d67c46b9STakashi Sakamoto /* Check valid protocol or not. */ 5459863874fSTakashi Sakamoto sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT; 546d67c46b9STakashi Sakamoto fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT; 5479863874fSTakashi Sakamoto if (sph != s->sph || fmt != s->fmt) { 5482a7e1713STakashi Sakamoto dev_info_ratelimited(&s->unit->device, 549d67c46b9STakashi Sakamoto "Detect unexpected protocol: %08x %08x\n", 550d67c46b9STakashi Sakamoto cip_header[0], cip_header[1]); 551e335425bSTakashi Sakamoto return -EAGAIN; 552d67c46b9STakashi Sakamoto } 553d67c46b9STakashi Sakamoto 554d67c46b9STakashi Sakamoto /* Calculate data blocks */ 555d67c46b9STakashi Sakamoto fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT; 556e335425bSTakashi Sakamoto if (payload_length < sizeof(__be32) * 2 || 557d67c46b9STakashi Sakamoto (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) { 558e335425bSTakashi Sakamoto *data_blocks = 0; 559d67c46b9STakashi Sakamoto } else { 560e335425bSTakashi Sakamoto unsigned int data_block_quadlets = 561d67c46b9STakashi Sakamoto (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT; 562d67c46b9STakashi Sakamoto /* avoid division by zero */ 563d67c46b9STakashi Sakamoto if (data_block_quadlets == 0) { 564d67c46b9STakashi Sakamoto dev_err(&s->unit->device, 565d67c46b9STakashi Sakamoto "Detect invalid value in dbs field: %08X\n", 566d67c46b9STakashi Sakamoto cip_header[0]); 567d67c46b9STakashi Sakamoto return -EPROTO; 568d67c46b9STakashi Sakamoto } 569d67c46b9STakashi Sakamoto if (s->flags & CIP_WRONG_DBS) 570d67c46b9STakashi Sakamoto data_block_quadlets = s->data_block_quadlets; 571d67c46b9STakashi Sakamoto 572e335425bSTakashi Sakamoto *data_blocks = (payload_length / sizeof(__be32) - 2) / 573ff0fb5aaSTakashi Sakamoto data_block_quadlets; 574d67c46b9STakashi Sakamoto } 575d67c46b9STakashi Sakamoto 576d67c46b9STakashi Sakamoto /* Check data block counter continuity */ 577a35463d1STakashi Sakamoto dbc = cip_header[0] & CIP_DBC_MASK; 578e335425bSTakashi Sakamoto if (*data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) && 579a35463d1STakashi Sakamoto *data_block_counter != UINT_MAX) 580a35463d1STakashi Sakamoto dbc = *data_block_counter; 581d67c46b9STakashi Sakamoto 582a35463d1STakashi Sakamoto if ((dbc == 0x00 && (s->flags & CIP_SKIP_DBC_ZERO_CHECK)) || 583a35463d1STakashi Sakamoto *data_block_counter == UINT_MAX) { 584d67c46b9STakashi Sakamoto lost = false; 585d67c46b9STakashi Sakamoto } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) { 586a35463d1STakashi Sakamoto lost = dbc != *data_block_counter; 587d67c46b9STakashi Sakamoto } else { 588e335425bSTakashi Sakamoto unsigned int dbc_interval; 589e335425bSTakashi Sakamoto 590e335425bSTakashi Sakamoto if (*data_blocks > 0 && s->ctx_data.tx.dbc_interval > 0) 591d3d10a4aSTakashi Sakamoto dbc_interval = s->ctx_data.tx.dbc_interval; 592d67c46b9STakashi Sakamoto else 593e335425bSTakashi Sakamoto dbc_interval = *data_blocks; 594d67c46b9STakashi Sakamoto 595a35463d1STakashi Sakamoto lost = dbc != ((*data_block_counter + dbc_interval) & 0xff); 596d67c46b9STakashi Sakamoto } 597d67c46b9STakashi Sakamoto 598d67c46b9STakashi Sakamoto if (lost) { 599d67c46b9STakashi Sakamoto dev_err(&s->unit->device, 600d67c46b9STakashi Sakamoto "Detect discontinuity of CIP: %02X %02X\n", 601a35463d1STakashi Sakamoto *data_block_counter, dbc); 602d67c46b9STakashi Sakamoto return -EIO; 603d67c46b9STakashi Sakamoto } 604d67c46b9STakashi Sakamoto 605e335425bSTakashi Sakamoto *syt = cip_header[1] & CIP_SYT_MASK; 606e335425bSTakashi Sakamoto 607e335425bSTakashi Sakamoto return 0; 608e335425bSTakashi Sakamoto } 609e335425bSTakashi Sakamoto 61098e3e43bSTakashi Sakamoto static int parse_ir_ctx_header(struct amdtp_stream *s, unsigned int cycle, 61198e3e43bSTakashi Sakamoto const __be32 *ctx_header, 61298e3e43bSTakashi Sakamoto unsigned int *payload_length, 613a35463d1STakashi Sakamoto unsigned int *data_blocks, 614a35463d1STakashi Sakamoto unsigned int *data_block_counter, 615a35463d1STakashi Sakamoto unsigned int *syt, unsigned int index) 616e335425bSTakashi Sakamoto { 617f11453c7STakashi Sakamoto const __be32 *cip_header; 618e335425bSTakashi Sakamoto int err; 619e335425bSTakashi Sakamoto 62098e3e43bSTakashi Sakamoto *payload_length = be32_to_cpu(ctx_header[0]) >> ISO_DATA_LENGTH_SHIFT; 62198e3e43bSTakashi Sakamoto if (*payload_length > s->ctx_data.tx.ctx_header_size + 622f11453c7STakashi Sakamoto s->ctx_data.tx.max_ctx_payload_length) { 623e335425bSTakashi Sakamoto dev_err(&s->unit->device, 624e335425bSTakashi Sakamoto "Detect jumbo payload: %04x %04x\n", 62598e3e43bSTakashi Sakamoto *payload_length, s->ctx_data.tx.max_ctx_payload_length); 626e335425bSTakashi Sakamoto return -EIO; 627e335425bSTakashi Sakamoto } 628e335425bSTakashi Sakamoto 629947b437eSTakashi Sakamoto if (!(s->flags & CIP_NO_HEADER)) { 63098e3e43bSTakashi Sakamoto cip_header = ctx_header + 2; 63198e3e43bSTakashi Sakamoto err = check_cip_header(s, cip_header, *payload_length, 632a35463d1STakashi Sakamoto data_blocks, data_block_counter, syt); 633b8b0e24cSTakashi Sakamoto if (err < 0) 634e335425bSTakashi Sakamoto return err; 635947b437eSTakashi Sakamoto } else { 636947b437eSTakashi Sakamoto cip_header = NULL; 63776864868STakashi Sakamoto err = 0; 63898e3e43bSTakashi Sakamoto *data_blocks = *payload_length / sizeof(__be32) / 63998e3e43bSTakashi Sakamoto s->data_block_quadlets; 64098e3e43bSTakashi Sakamoto *syt = 0; 6417fbf9096STakashi Sakamoto 642a35463d1STakashi Sakamoto if (*data_block_counter == UINT_MAX) 643a35463d1STakashi Sakamoto *data_block_counter = 0; 644947b437eSTakashi Sakamoto } 645e335425bSTakashi Sakamoto 64698e3e43bSTakashi Sakamoto trace_amdtp_packet(s, cycle, cip_header, *payload_length, *data_blocks, 647a35463d1STakashi Sakamoto *data_block_counter, index); 64864d0bf4dSTakashi Sakamoto 64976864868STakashi Sakamoto return err; 650d67c46b9STakashi Sakamoto } 651d67c46b9STakashi Sakamoto 65226cd1e58STakashi Sakamoto // In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On 65326cd1e58STakashi Sakamoto // the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent 65426cd1e58STakashi Sakamoto // it. Thus, via Linux firewire subsystem, we can get the 3 bits for second. 65526cd1e58STakashi Sakamoto static inline u32 compute_cycle_count(__be32 ctx_header_tstamp) 65673fc7f08STakashi Sakamoto { 65726cd1e58STakashi Sakamoto u32 tstamp = be32_to_cpu(ctx_header_tstamp) & HEADER_TSTAMP_MASK; 65873fc7f08STakashi Sakamoto return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff); 65973fc7f08STakashi Sakamoto } 66073fc7f08STakashi Sakamoto 66173fc7f08STakashi Sakamoto static inline u32 increment_cycle_count(u32 cycle, unsigned int addend) 66273fc7f08STakashi Sakamoto { 66373fc7f08STakashi Sakamoto cycle += addend; 66473fc7f08STakashi Sakamoto if (cycle >= 8 * CYCLES_PER_SECOND) 66573fc7f08STakashi Sakamoto cycle -= 8 * CYCLES_PER_SECOND; 66673fc7f08STakashi Sakamoto return cycle; 66773fc7f08STakashi Sakamoto } 66873fc7f08STakashi Sakamoto 66926cd1e58STakashi Sakamoto // Align to actual cycle count for the packet which is going to be scheduled. 67026cd1e58STakashi Sakamoto // This module queued the same number of isochronous cycle as QUEUE_LENGTH to 67126cd1e58STakashi Sakamoto // skip isochronous cycle, therefore it's OK to just increment the cycle by 67226cd1e58STakashi Sakamoto // QUEUE_LENGTH for scheduled cycle. 67326cd1e58STakashi Sakamoto static inline u32 compute_it_cycle(const __be32 ctx_header_tstamp) 67426cd1e58STakashi Sakamoto { 67526cd1e58STakashi Sakamoto u32 cycle = compute_cycle_count(ctx_header_tstamp); 67626cd1e58STakashi Sakamoto return increment_cycle_count(cycle, QUEUE_LENGTH); 67726cd1e58STakashi Sakamoto } 67826cd1e58STakashi Sakamoto 679fce9b013STakashi Sakamoto static inline void cancel_stream(struct amdtp_stream *s) 680fce9b013STakashi Sakamoto { 681fce9b013STakashi Sakamoto s->packet_index = -1; 682fce9b013STakashi Sakamoto if (in_interrupt()) 683fce9b013STakashi Sakamoto amdtp_stream_pcm_abort(s); 684fce9b013STakashi Sakamoto WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN); 685fce9b013STakashi Sakamoto } 686fce9b013STakashi Sakamoto 68773fc7f08STakashi Sakamoto static void out_stream_callback(struct fw_iso_context *context, u32 tstamp, 688d67c46b9STakashi Sakamoto size_t header_length, void *header, 689d67c46b9STakashi Sakamoto void *private_data) 690d67c46b9STakashi Sakamoto { 691d67c46b9STakashi Sakamoto struct amdtp_stream *s = private_data; 69226cd1e58STakashi Sakamoto const __be32 *ctx_header = header; 6930dcb4efbSTakashi Sakamoto unsigned int packets = header_length / sizeof(*ctx_header); 6940dcb4efbSTakashi Sakamoto int i; 695d67c46b9STakashi Sakamoto 696d67c46b9STakashi Sakamoto if (s->packet_index < 0) 697d67c46b9STakashi Sakamoto return; 698d67c46b9STakashi Sakamoto 699d67c46b9STakashi Sakamoto for (i = 0; i < packets; ++i) { 70026cd1e58STakashi Sakamoto u32 cycle; 7016bc1a269STakashi Sakamoto unsigned int syt; 7020dcb4efbSTakashi Sakamoto unsigned int data_blocks; 703860d798cSTakashi Sakamoto unsigned int dbc; 7048a400b99STakashi Sakamoto __be32 *buffer; 7056bc1a269STakashi Sakamoto unsigned int pcm_frames; 7066bc1a269STakashi Sakamoto struct { 7076bc1a269STakashi Sakamoto struct fw_iso_packet params; 7086bc1a269STakashi Sakamoto __be32 header[IT_PKT_HEADER_SIZE_CIP / sizeof(__be32)]; 7096bc1a269STakashi Sakamoto } template = { {0}, {0} }; 7106bc1a269STakashi Sakamoto struct snd_pcm_substream *pcm; 71126cd1e58STakashi Sakamoto 71226cd1e58STakashi Sakamoto cycle = compute_it_cycle(*ctx_header); 7136bc1a269STakashi Sakamoto syt = calculate_syt(s, cycle); 7140dcb4efbSTakashi Sakamoto data_blocks = calculate_data_blocks(s, syt); 7158a400b99STakashi Sakamoto buffer = s->buffer.packets[s->packet_index].buffer; 716860d798cSTakashi Sakamoto dbc = s->data_block_counter; 717860d798cSTakashi Sakamoto pcm_frames = s->process_data_blocks(s, buffer, data_blocks, dbc, 718860d798cSTakashi Sakamoto &syt); 719860d798cSTakashi Sakamoto 720860d798cSTakashi Sakamoto if (s->flags & CIP_DBC_IS_END_EVENT) 721860d798cSTakashi Sakamoto dbc = (dbc + data_blocks) & 0xff; 72226cd1e58STakashi Sakamoto 723*3baf3053STakashi Sakamoto if (s->ctx_data.rx.syt_override >= 0) 724*3baf3053STakashi Sakamoto syt = s->ctx_data.rx.syt_override; 725*3baf3053STakashi Sakamoto 7260dcb4efbSTakashi Sakamoto build_it_pkt_header(s, cycle, &template.params, data_blocks, 727860d798cSTakashi Sakamoto dbc, syt, i); 728860d798cSTakashi Sakamoto 729860d798cSTakashi Sakamoto if (!(s->flags & CIP_DBC_IS_END_EVENT)) 730860d798cSTakashi Sakamoto dbc = (dbc + data_blocks) & 0xff; 731860d798cSTakashi Sakamoto 732860d798cSTakashi Sakamoto s->data_block_counter = dbc; 7336bc1a269STakashi Sakamoto 7346bc1a269STakashi Sakamoto if (queue_out_packet(s, &template.params) < 0) { 735fce9b013STakashi Sakamoto cancel_stream(s); 736d67c46b9STakashi Sakamoto return; 737d67c46b9STakashi Sakamoto } 73826cd1e58STakashi Sakamoto 7396bc1a269STakashi Sakamoto pcm = READ_ONCE(s->pcm); 7406bc1a269STakashi Sakamoto if (pcm && pcm_frames > 0) 7416bc1a269STakashi Sakamoto update_pcm_pointers(s, pcm, pcm_frames); 7426bc1a269STakashi Sakamoto 74326cd1e58STakashi Sakamoto ++ctx_header; 744d67c46b9STakashi Sakamoto } 745d67c46b9STakashi Sakamoto 746d67c46b9STakashi Sakamoto fw_iso_context_queue_flush(s->context); 747d67c46b9STakashi Sakamoto } 748d67c46b9STakashi Sakamoto 74973fc7f08STakashi Sakamoto static void in_stream_callback(struct fw_iso_context *context, u32 tstamp, 750d67c46b9STakashi Sakamoto size_t header_length, void *header, 751d67c46b9STakashi Sakamoto void *private_data) 752d67c46b9STakashi Sakamoto { 753d67c46b9STakashi Sakamoto struct amdtp_stream *s = private_data; 754d9a16fc9STakashi Sakamoto unsigned int i, packets; 755cc4f8e91STakashi Sakamoto __be32 *ctx_header = header; 756d67c46b9STakashi Sakamoto 757d67c46b9STakashi Sakamoto if (s->packet_index < 0) 758d67c46b9STakashi Sakamoto return; 759d67c46b9STakashi Sakamoto 760d3d10a4aSTakashi Sakamoto // The number of packets in buffer. 761d3d10a4aSTakashi Sakamoto packets = header_length / s->ctx_data.tx.ctx_header_size; 762f90e2dedSTakashi Sakamoto 763d9a16fc9STakashi Sakamoto for (i = 0; i < packets; i++) { 76426cd1e58STakashi Sakamoto u32 cycle; 76598e3e43bSTakashi Sakamoto unsigned int payload_length; 766973b059cSTakashi Sakamoto unsigned int data_blocks; 767a35463d1STakashi Sakamoto unsigned int dbc; 76898e3e43bSTakashi Sakamoto unsigned int syt; 7698a400b99STakashi Sakamoto __be32 *buffer; 77098e3e43bSTakashi Sakamoto unsigned int pcm_frames = 0; 77198e3e43bSTakashi Sakamoto struct fw_iso_packet params = {0}; 77298e3e43bSTakashi Sakamoto struct snd_pcm_substream *pcm; 77398e3e43bSTakashi Sakamoto int err; 774cc4f8e91STakashi Sakamoto 77526cd1e58STakashi Sakamoto cycle = compute_cycle_count(ctx_header[1]); 776a35463d1STakashi Sakamoto dbc = s->data_block_counter; 77798e3e43bSTakashi Sakamoto err = parse_ir_ctx_header(s, cycle, ctx_header, &payload_length, 778a35463d1STakashi Sakamoto &data_blocks, &dbc, &syt, i); 77998e3e43bSTakashi Sakamoto if (err < 0 && err != -EAGAIN) 780d67c46b9STakashi Sakamoto break; 781973b059cSTakashi Sakamoto 78298e3e43bSTakashi Sakamoto if (err >= 0) { 78398e3e43bSTakashi Sakamoto buffer = s->buffer.packets[s->packet_index].buffer; 78498e3e43bSTakashi Sakamoto pcm_frames = s->process_data_blocks(s, buffer, 785a35463d1STakashi Sakamoto data_blocks, dbc, &syt); 786973b059cSTakashi Sakamoto 787a35463d1STakashi Sakamoto if (!(s->flags & CIP_DBC_IS_END_EVENT)) 788a35463d1STakashi Sakamoto dbc = (dbc + data_blocks) & 0xff; 789973b059cSTakashi Sakamoto } 790a35463d1STakashi Sakamoto 791a35463d1STakashi Sakamoto s->data_block_counter = dbc; 79298e3e43bSTakashi Sakamoto 79398e3e43bSTakashi Sakamoto if (queue_in_packet(s, ¶ms) < 0) 79498e3e43bSTakashi Sakamoto break; 79598e3e43bSTakashi Sakamoto 79698e3e43bSTakashi Sakamoto pcm = READ_ONCE(s->pcm); 79798e3e43bSTakashi Sakamoto if (pcm && pcm_frames > 0) 79898e3e43bSTakashi Sakamoto update_pcm_pointers(s, pcm, pcm_frames); 799cc4f8e91STakashi Sakamoto 800d3d10a4aSTakashi Sakamoto ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header); 801d67c46b9STakashi Sakamoto } 802d67c46b9STakashi Sakamoto 803dec63cc8STakashi Sakamoto /* Queueing error or detecting invalid payload. */ 804d9a16fc9STakashi Sakamoto if (i < packets) { 805fce9b013STakashi Sakamoto cancel_stream(s); 806d67c46b9STakashi Sakamoto return; 807d67c46b9STakashi Sakamoto } 808d67c46b9STakashi Sakamoto 809d67c46b9STakashi Sakamoto fw_iso_context_queue_flush(s->context); 810d67c46b9STakashi Sakamoto } 811d67c46b9STakashi Sakamoto 812d67c46b9STakashi Sakamoto /* this is executed one time */ 813d67c46b9STakashi Sakamoto static void amdtp_stream_first_callback(struct fw_iso_context *context, 81473fc7f08STakashi Sakamoto u32 tstamp, size_t header_length, 815d67c46b9STakashi Sakamoto void *header, void *private_data) 816d67c46b9STakashi Sakamoto { 817d67c46b9STakashi Sakamoto struct amdtp_stream *s = private_data; 81826cd1e58STakashi Sakamoto const __be32 *ctx_header = header; 819a04513f8STakashi Sakamoto u32 cycle; 820d67c46b9STakashi Sakamoto 821d67c46b9STakashi Sakamoto /* 822d67c46b9STakashi Sakamoto * For in-stream, first packet has come. 823d67c46b9STakashi Sakamoto * For out-stream, prepared to transmit first packet 824d67c46b9STakashi Sakamoto */ 825d67c46b9STakashi Sakamoto s->callbacked = true; 826d67c46b9STakashi Sakamoto wake_up(&s->callback_wait); 827d67c46b9STakashi Sakamoto 828cc4f8e91STakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 82926cd1e58STakashi Sakamoto cycle = compute_cycle_count(ctx_header[1]); 830a04513f8STakashi Sakamoto 831d67c46b9STakashi Sakamoto context->callback.sc = in_stream_callback; 832a04513f8STakashi Sakamoto } else { 83326cd1e58STakashi Sakamoto cycle = compute_it_cycle(*ctx_header); 83426cd1e58STakashi Sakamoto 835d67c46b9STakashi Sakamoto context->callback.sc = out_stream_callback; 836a04513f8STakashi Sakamoto } 837a04513f8STakashi Sakamoto 838a04513f8STakashi Sakamoto s->start_cycle = cycle; 839d67c46b9STakashi Sakamoto 84073fc7f08STakashi Sakamoto context->callback.sc(context, tstamp, header_length, header, s); 841d67c46b9STakashi Sakamoto } 842d67c46b9STakashi Sakamoto 843d67c46b9STakashi Sakamoto /** 844d67c46b9STakashi Sakamoto * amdtp_stream_start - start transferring packets 845d67c46b9STakashi Sakamoto * @s: the AMDTP stream to start 846d67c46b9STakashi Sakamoto * @channel: the isochronous channel on the bus 847d67c46b9STakashi Sakamoto * @speed: firewire speed code 848d67c46b9STakashi Sakamoto * 849d67c46b9STakashi Sakamoto * The stream cannot be started until it has been configured with 850d67c46b9STakashi Sakamoto * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI 851d67c46b9STakashi Sakamoto * device can be started. 852d67c46b9STakashi Sakamoto */ 853d67c46b9STakashi Sakamoto int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed) 854d67c46b9STakashi Sakamoto { 855d67c46b9STakashi Sakamoto static const struct { 856d67c46b9STakashi Sakamoto unsigned int data_block; 857d67c46b9STakashi Sakamoto unsigned int syt_offset; 858d3d10a4aSTakashi Sakamoto } *entry, initial_state[] = { 859d67c46b9STakashi Sakamoto [CIP_SFC_32000] = { 4, 3072 }, 860d67c46b9STakashi Sakamoto [CIP_SFC_48000] = { 6, 1024 }, 861d67c46b9STakashi Sakamoto [CIP_SFC_96000] = { 12, 1024 }, 862d67c46b9STakashi Sakamoto [CIP_SFC_192000] = { 24, 1024 }, 863d67c46b9STakashi Sakamoto [CIP_SFC_44100] = { 0, 67 }, 864d67c46b9STakashi Sakamoto [CIP_SFC_88200] = { 0, 67 }, 865d67c46b9STakashi Sakamoto [CIP_SFC_176400] = { 0, 67 }, 866d67c46b9STakashi Sakamoto }; 867d3d10a4aSTakashi Sakamoto unsigned int ctx_header_size; 868f11453c7STakashi Sakamoto unsigned int max_ctx_payload_size; 869d67c46b9STakashi Sakamoto enum dma_data_direction dir; 870d67c46b9STakashi Sakamoto int type, tag, err; 871d67c46b9STakashi Sakamoto 872d67c46b9STakashi Sakamoto mutex_lock(&s->mutex); 873d67c46b9STakashi Sakamoto 874d67c46b9STakashi Sakamoto if (WARN_ON(amdtp_stream_running(s) || 875d67c46b9STakashi Sakamoto (s->data_block_quadlets < 1))) { 876d67c46b9STakashi Sakamoto err = -EBADFD; 877d67c46b9STakashi Sakamoto goto err_unlock; 878d67c46b9STakashi Sakamoto } 879d67c46b9STakashi Sakamoto 880d3d10a4aSTakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 881d67c46b9STakashi Sakamoto s->data_block_counter = UINT_MAX; 882d3d10a4aSTakashi Sakamoto } else { 883d3d10a4aSTakashi Sakamoto entry = &initial_state[s->sfc]; 884d3d10a4aSTakashi Sakamoto 885d67c46b9STakashi Sakamoto s->data_block_counter = 0; 886d3d10a4aSTakashi Sakamoto s->ctx_data.rx.data_block_state = entry->data_block; 887d3d10a4aSTakashi Sakamoto s->ctx_data.rx.syt_offset_state = entry->syt_offset; 888d3d10a4aSTakashi Sakamoto s->ctx_data.rx.last_syt_offset = TICKS_PER_CYCLE; 889d3d10a4aSTakashi Sakamoto } 890d67c46b9STakashi Sakamoto 891d67c46b9STakashi Sakamoto /* initialize packet buffer */ 892d67c46b9STakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 893d67c46b9STakashi Sakamoto dir = DMA_FROM_DEVICE; 894d67c46b9STakashi Sakamoto type = FW_ISO_CONTEXT_RECEIVE; 895f11453c7STakashi Sakamoto if (!(s->flags & CIP_NO_HEADER)) 896f11453c7STakashi Sakamoto ctx_header_size = IR_CTX_HEADER_SIZE_CIP; 897f11453c7STakashi Sakamoto else 898f11453c7STakashi Sakamoto ctx_header_size = IR_CTX_HEADER_SIZE_NO_CIP; 899b18f0cfaSTakashi Sakamoto 900b18f0cfaSTakashi Sakamoto max_ctx_payload_size = amdtp_stream_get_max_payload(s) - 901b18f0cfaSTakashi Sakamoto ctx_header_size; 902d67c46b9STakashi Sakamoto } else { 903d67c46b9STakashi Sakamoto dir = DMA_TO_DEVICE; 904d67c46b9STakashi Sakamoto type = FW_ISO_CONTEXT_TRANSMIT; 905df9160b9STakashi Sakamoto ctx_header_size = 0; // No effect for IT context. 906f11453c7STakashi Sakamoto 907b18f0cfaSTakashi Sakamoto max_ctx_payload_size = amdtp_stream_get_max_payload(s); 908b18f0cfaSTakashi Sakamoto if (!(s->flags & CIP_NO_HEADER)) 909b18f0cfaSTakashi Sakamoto max_ctx_payload_size -= IT_PKT_HEADER_SIZE_CIP; 910b18f0cfaSTakashi Sakamoto } 911f11453c7STakashi Sakamoto 912d67c46b9STakashi Sakamoto err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH, 913f11453c7STakashi Sakamoto max_ctx_payload_size, dir); 914d67c46b9STakashi Sakamoto if (err < 0) 915d67c46b9STakashi Sakamoto goto err_unlock; 916d67c46b9STakashi Sakamoto 917d67c46b9STakashi Sakamoto s->context = fw_iso_context_create(fw_parent_device(s->unit)->card, 918d3d10a4aSTakashi Sakamoto type, channel, speed, ctx_header_size, 919d67c46b9STakashi Sakamoto amdtp_stream_first_callback, s); 920d67c46b9STakashi Sakamoto if (IS_ERR(s->context)) { 921d67c46b9STakashi Sakamoto err = PTR_ERR(s->context); 922d67c46b9STakashi Sakamoto if (err == -EBUSY) 923d67c46b9STakashi Sakamoto dev_err(&s->unit->device, 924d67c46b9STakashi Sakamoto "no free stream on this controller\n"); 925d67c46b9STakashi Sakamoto goto err_buffer; 926d67c46b9STakashi Sakamoto } 927d67c46b9STakashi Sakamoto 928d67c46b9STakashi Sakamoto amdtp_stream_update(s); 929d67c46b9STakashi Sakamoto 930d3d10a4aSTakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 931f11453c7STakashi Sakamoto s->ctx_data.tx.max_ctx_payload_length = max_ctx_payload_size; 932d3d10a4aSTakashi Sakamoto s->ctx_data.tx.ctx_header_size = ctx_header_size; 933d3d10a4aSTakashi Sakamoto } 93452759c09STakashi Sakamoto 9353b196c39STakashi Sakamoto if (s->flags & CIP_NO_HEADER) 9363b196c39STakashi Sakamoto s->tag = TAG_NO_CIP_HEADER; 9373b196c39STakashi Sakamoto else 9383b196c39STakashi Sakamoto s->tag = TAG_CIP; 9393b196c39STakashi Sakamoto 940d67c46b9STakashi Sakamoto s->packet_index = 0; 941d67c46b9STakashi Sakamoto do { 9426007bf54STakashi Sakamoto struct fw_iso_packet params; 943b18f0cfaSTakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 9446007bf54STakashi Sakamoto err = queue_in_packet(s, ¶ms); 945b18f0cfaSTakashi Sakamoto } else { 946b18f0cfaSTakashi Sakamoto params.header_length = 0; 947b18f0cfaSTakashi Sakamoto params.payload_length = 0; 948b18f0cfaSTakashi Sakamoto err = queue_out_packet(s, ¶ms); 949b18f0cfaSTakashi Sakamoto } 950d67c46b9STakashi Sakamoto if (err < 0) 951d67c46b9STakashi Sakamoto goto err_context; 952d67c46b9STakashi Sakamoto } while (s->packet_index > 0); 953d67c46b9STakashi Sakamoto 954d67c46b9STakashi Sakamoto /* NOTE: TAG1 matches CIP. This just affects in stream. */ 955d67c46b9STakashi Sakamoto tag = FW_ISO_CONTEXT_MATCH_TAG1; 9563b196c39STakashi Sakamoto if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER)) 957d67c46b9STakashi Sakamoto tag |= FW_ISO_CONTEXT_MATCH_TAG0; 958d67c46b9STakashi Sakamoto 959d67c46b9STakashi Sakamoto s->callbacked = false; 960d67c46b9STakashi Sakamoto err = fw_iso_context_start(s->context, -1, 0, tag); 961d67c46b9STakashi Sakamoto if (err < 0) 962d67c46b9STakashi Sakamoto goto err_context; 963d67c46b9STakashi Sakamoto 964d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 965d67c46b9STakashi Sakamoto 966d67c46b9STakashi Sakamoto return 0; 967d67c46b9STakashi Sakamoto 968d67c46b9STakashi Sakamoto err_context: 969d67c46b9STakashi Sakamoto fw_iso_context_destroy(s->context); 970d67c46b9STakashi Sakamoto s->context = ERR_PTR(-1); 971d67c46b9STakashi Sakamoto err_buffer: 972d67c46b9STakashi Sakamoto iso_packets_buffer_destroy(&s->buffer, s->unit); 973d67c46b9STakashi Sakamoto err_unlock: 974d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 975d67c46b9STakashi Sakamoto 976d67c46b9STakashi Sakamoto return err; 977d67c46b9STakashi Sakamoto } 978d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_start); 979d67c46b9STakashi Sakamoto 980d67c46b9STakashi Sakamoto /** 981d67c46b9STakashi Sakamoto * amdtp_stream_pcm_pointer - get the PCM buffer position 982d67c46b9STakashi Sakamoto * @s: the AMDTP stream that transports the PCM data 983d67c46b9STakashi Sakamoto * 984d67c46b9STakashi Sakamoto * Returns the current buffer position, in frames. 985d67c46b9STakashi Sakamoto */ 986d67c46b9STakashi Sakamoto unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s) 987d67c46b9STakashi Sakamoto { 9881dba9db0STakashi Sakamoto /* 9891dba9db0STakashi Sakamoto * This function is called in software IRQ context of period_tasklet or 9901dba9db0STakashi Sakamoto * process context. 9911dba9db0STakashi Sakamoto * 9921dba9db0STakashi Sakamoto * When the software IRQ context was scheduled by software IRQ context 9931dba9db0STakashi Sakamoto * of IR/IT contexts, queued packets were already handled. Therefore, 9941dba9db0STakashi Sakamoto * no need to flush the queue in buffer anymore. 9951dba9db0STakashi Sakamoto * 9961dba9db0STakashi Sakamoto * When the process context reach here, some packets will be already 9971dba9db0STakashi Sakamoto * queued in the buffer. These packets should be handled immediately 9981dba9db0STakashi Sakamoto * to keep better granularity of PCM pointer. 9991dba9db0STakashi Sakamoto * 10001dba9db0STakashi Sakamoto * Later, the process context will sometimes schedules software IRQ 10011dba9db0STakashi Sakamoto * context of the period_tasklet. Then, no need to flush the queue by 10021dba9db0STakashi Sakamoto * the same reason as described for IR/IT contexts. 10031dba9db0STakashi Sakamoto */ 10041dba9db0STakashi Sakamoto if (!in_interrupt() && amdtp_stream_running(s)) 1005d67c46b9STakashi Sakamoto fw_iso_context_flush_completions(s->context); 1006d67c46b9STakashi Sakamoto 10076aa7de05SMark Rutland return READ_ONCE(s->pcm_buffer_pointer); 1008d67c46b9STakashi Sakamoto } 1009d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_pointer); 1010d67c46b9STakashi Sakamoto 1011d67c46b9STakashi Sakamoto /** 1012875becf8STakashi Sakamoto * amdtp_stream_pcm_ack - acknowledge queued PCM frames 1013875becf8STakashi Sakamoto * @s: the AMDTP stream that transfers the PCM frames 1014875becf8STakashi Sakamoto * 1015875becf8STakashi Sakamoto * Returns zero always. 1016875becf8STakashi Sakamoto */ 1017875becf8STakashi Sakamoto int amdtp_stream_pcm_ack(struct amdtp_stream *s) 1018875becf8STakashi Sakamoto { 1019875becf8STakashi Sakamoto /* 1020875becf8STakashi Sakamoto * Process isochronous packets for recent isochronous cycle to handle 1021875becf8STakashi Sakamoto * queued PCM frames. 1022875becf8STakashi Sakamoto */ 1023875becf8STakashi Sakamoto if (amdtp_stream_running(s)) 1024875becf8STakashi Sakamoto fw_iso_context_flush_completions(s->context); 1025875becf8STakashi Sakamoto 1026875becf8STakashi Sakamoto return 0; 1027875becf8STakashi Sakamoto } 1028875becf8STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_ack); 1029875becf8STakashi Sakamoto 1030875becf8STakashi Sakamoto /** 1031d67c46b9STakashi Sakamoto * amdtp_stream_update - update the stream after a bus reset 1032d67c46b9STakashi Sakamoto * @s: the AMDTP stream 1033d67c46b9STakashi Sakamoto */ 1034d67c46b9STakashi Sakamoto void amdtp_stream_update(struct amdtp_stream *s) 1035d67c46b9STakashi Sakamoto { 1036d67c46b9STakashi Sakamoto /* Precomputing. */ 10376aa7de05SMark Rutland WRITE_ONCE(s->source_node_id_field, 10386aa7de05SMark Rutland (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & CIP_SID_MASK); 1039d67c46b9STakashi Sakamoto } 1040d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_update); 1041d67c46b9STakashi Sakamoto 1042d67c46b9STakashi Sakamoto /** 1043d67c46b9STakashi Sakamoto * amdtp_stream_stop - stop sending packets 1044d67c46b9STakashi Sakamoto * @s: the AMDTP stream to stop 1045d67c46b9STakashi Sakamoto * 1046d67c46b9STakashi Sakamoto * All PCM and MIDI devices of the stream must be stopped before the stream 1047d67c46b9STakashi Sakamoto * itself can be stopped. 1048d67c46b9STakashi Sakamoto */ 1049d67c46b9STakashi Sakamoto void amdtp_stream_stop(struct amdtp_stream *s) 1050d67c46b9STakashi Sakamoto { 1051d67c46b9STakashi Sakamoto mutex_lock(&s->mutex); 1052d67c46b9STakashi Sakamoto 1053d67c46b9STakashi Sakamoto if (!amdtp_stream_running(s)) { 1054d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 1055d67c46b9STakashi Sakamoto return; 1056d67c46b9STakashi Sakamoto } 1057d67c46b9STakashi Sakamoto 1058d67c46b9STakashi Sakamoto tasklet_kill(&s->period_tasklet); 1059d67c46b9STakashi Sakamoto fw_iso_context_stop(s->context); 1060d67c46b9STakashi Sakamoto fw_iso_context_destroy(s->context); 1061d67c46b9STakashi Sakamoto s->context = ERR_PTR(-1); 1062d67c46b9STakashi Sakamoto iso_packets_buffer_destroy(&s->buffer, s->unit); 1063d67c46b9STakashi Sakamoto 1064d67c46b9STakashi Sakamoto s->callbacked = false; 1065d67c46b9STakashi Sakamoto 1066d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 1067d67c46b9STakashi Sakamoto } 1068d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_stop); 1069d67c46b9STakashi Sakamoto 1070d67c46b9STakashi Sakamoto /** 1071d67c46b9STakashi Sakamoto * amdtp_stream_pcm_abort - abort the running PCM device 1072d67c46b9STakashi Sakamoto * @s: the AMDTP stream about to be stopped 1073d67c46b9STakashi Sakamoto * 1074d67c46b9STakashi Sakamoto * If the isochronous stream needs to be stopped asynchronously, call this 1075d67c46b9STakashi Sakamoto * function first to stop the PCM device. 1076d67c46b9STakashi Sakamoto */ 1077d67c46b9STakashi Sakamoto void amdtp_stream_pcm_abort(struct amdtp_stream *s) 1078d67c46b9STakashi Sakamoto { 1079d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm; 1080d67c46b9STakashi Sakamoto 10816aa7de05SMark Rutland pcm = READ_ONCE(s->pcm); 1082d67c46b9STakashi Sakamoto if (pcm) 1083d67c46b9STakashi Sakamoto snd_pcm_stop_xrun(pcm); 1084d67c46b9STakashi Sakamoto } 1085d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_abort); 1086