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 55f11453c7STakashi Sakamoto // For iso header, tstamp and 2 CIP header. 56f11453c7STakashi Sakamoto #define IR_CTX_HEADER_SIZE_CIP 16 57f11453c7STakashi Sakamoto // For iso header and tstamp. 58f11453c7STakashi Sakamoto #define IR_CTX_HEADER_SIZE_NO_CIP 8 59cc4f8e91STakashi Sakamoto #define HEADER_TSTAMP_MASK 0x0000ffff 60d67c46b9STakashi Sakamoto 61b18f0cfaSTakashi Sakamoto #define IT_PKT_HEADER_SIZE_CIP 8 // For 2 CIP header. 62b18f0cfaSTakashi Sakamoto #define IT_PKT_HEADER_SIZE_NO_CIP 0 // Nothing. 63b18f0cfaSTakashi Sakamoto 64d67c46b9STakashi Sakamoto static void pcm_period_tasklet(unsigned long data); 65d67c46b9STakashi Sakamoto 66d67c46b9STakashi Sakamoto /** 67d67c46b9STakashi Sakamoto * amdtp_stream_init - initialize an AMDTP stream structure 68d67c46b9STakashi Sakamoto * @s: the AMDTP stream to initialize 69d67c46b9STakashi Sakamoto * @unit: the target of the stream 70d67c46b9STakashi Sakamoto * @dir: the direction of stream 71d67c46b9STakashi Sakamoto * @flags: the packet transmission method to use 725955815eSTakashi Sakamoto * @fmt: the value of fmt field in CIP header 739a738ad1STakashi Sakamoto * @process_ctx_payloads: callback handler to process payloads of isoc context 74df075feeSTakashi Sakamoto * @protocol_size: the size to allocate newly for protocol 75d67c46b9STakashi Sakamoto */ 76d67c46b9STakashi Sakamoto int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit, 775955815eSTakashi Sakamoto enum amdtp_stream_direction dir, enum cip_flags flags, 78df075feeSTakashi Sakamoto unsigned int fmt, 799a738ad1STakashi Sakamoto amdtp_stream_process_ctx_payloads_t process_ctx_payloads, 80df075feeSTakashi Sakamoto unsigned int protocol_size) 81d67c46b9STakashi Sakamoto { 829a738ad1STakashi Sakamoto if (process_ctx_payloads == NULL) 83df075feeSTakashi Sakamoto return -EINVAL; 84df075feeSTakashi Sakamoto 85df075feeSTakashi Sakamoto s->protocol = kzalloc(protocol_size, GFP_KERNEL); 86df075feeSTakashi Sakamoto if (!s->protocol) 87df075feeSTakashi Sakamoto return -ENOMEM; 88df075feeSTakashi Sakamoto 89d67c46b9STakashi Sakamoto s->unit = unit; 90d67c46b9STakashi Sakamoto s->direction = dir; 91d67c46b9STakashi Sakamoto s->flags = flags; 92d67c46b9STakashi Sakamoto s->context = ERR_PTR(-1); 93d67c46b9STakashi Sakamoto mutex_init(&s->mutex); 94d67c46b9STakashi Sakamoto tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s); 95d67c46b9STakashi Sakamoto s->packet_index = 0; 96d67c46b9STakashi Sakamoto 97d67c46b9STakashi Sakamoto init_waitqueue_head(&s->callback_wait); 98d67c46b9STakashi Sakamoto s->callbacked = false; 99d67c46b9STakashi Sakamoto 1005955815eSTakashi Sakamoto s->fmt = fmt; 1019a738ad1STakashi Sakamoto s->process_ctx_payloads = process_ctx_payloads; 102d67c46b9STakashi Sakamoto 1033baf3053STakashi Sakamoto if (dir == AMDTP_OUT_STREAM) 1043baf3053STakashi Sakamoto s->ctx_data.rx.syt_override = -1; 1053baf3053STakashi Sakamoto 106d67c46b9STakashi Sakamoto return 0; 107d67c46b9STakashi Sakamoto } 108d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_init); 109d67c46b9STakashi Sakamoto 110d67c46b9STakashi Sakamoto /** 111d67c46b9STakashi Sakamoto * amdtp_stream_destroy - free stream resources 112d67c46b9STakashi Sakamoto * @s: the AMDTP stream to destroy 113d67c46b9STakashi Sakamoto */ 114d67c46b9STakashi Sakamoto void amdtp_stream_destroy(struct amdtp_stream *s) 115d67c46b9STakashi Sakamoto { 11644c376b9STakashi Sakamoto /* Not initialized. */ 11744c376b9STakashi Sakamoto if (s->protocol == NULL) 11844c376b9STakashi Sakamoto return; 11944c376b9STakashi Sakamoto 120d67c46b9STakashi Sakamoto WARN_ON(amdtp_stream_running(s)); 121df075feeSTakashi Sakamoto kfree(s->protocol); 122d67c46b9STakashi Sakamoto mutex_destroy(&s->mutex); 123d67c46b9STakashi Sakamoto } 124d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_destroy); 125d67c46b9STakashi Sakamoto 126d67c46b9STakashi Sakamoto const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = { 127d67c46b9STakashi Sakamoto [CIP_SFC_32000] = 8, 128d67c46b9STakashi Sakamoto [CIP_SFC_44100] = 8, 129d67c46b9STakashi Sakamoto [CIP_SFC_48000] = 8, 130d67c46b9STakashi Sakamoto [CIP_SFC_88200] = 16, 131d67c46b9STakashi Sakamoto [CIP_SFC_96000] = 16, 132d67c46b9STakashi Sakamoto [CIP_SFC_176400] = 32, 133d67c46b9STakashi Sakamoto [CIP_SFC_192000] = 32, 134d67c46b9STakashi Sakamoto }; 135d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_syt_intervals); 136d67c46b9STakashi Sakamoto 137d67c46b9STakashi Sakamoto const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = { 138d67c46b9STakashi Sakamoto [CIP_SFC_32000] = 32000, 139d67c46b9STakashi Sakamoto [CIP_SFC_44100] = 44100, 140d67c46b9STakashi Sakamoto [CIP_SFC_48000] = 48000, 141d67c46b9STakashi Sakamoto [CIP_SFC_88200] = 88200, 142d67c46b9STakashi Sakamoto [CIP_SFC_96000] = 96000, 143d67c46b9STakashi Sakamoto [CIP_SFC_176400] = 176400, 144d67c46b9STakashi Sakamoto [CIP_SFC_192000] = 192000, 145d67c46b9STakashi Sakamoto }; 146d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_rate_table); 147d67c46b9STakashi Sakamoto 14859502295STakashi Sakamoto static int apply_constraint_to_size(struct snd_pcm_hw_params *params, 14959502295STakashi Sakamoto struct snd_pcm_hw_rule *rule) 15059502295STakashi Sakamoto { 15159502295STakashi Sakamoto struct snd_interval *s = hw_param_interval(params, rule->var); 15259502295STakashi Sakamoto const struct snd_interval *r = 15359502295STakashi Sakamoto hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE); 154826b5de9STakashi Sakamoto struct snd_interval t = {0}; 155826b5de9STakashi Sakamoto unsigned int step = 0; 15659502295STakashi Sakamoto int i; 15759502295STakashi Sakamoto 15859502295STakashi Sakamoto for (i = 0; i < CIP_SFC_COUNT; ++i) { 159826b5de9STakashi Sakamoto if (snd_interval_test(r, amdtp_rate_table[i])) 160826b5de9STakashi Sakamoto step = max(step, amdtp_syt_intervals[i]); 16159502295STakashi Sakamoto } 16259502295STakashi Sakamoto 163826b5de9STakashi Sakamoto t.min = roundup(s->min, step); 164826b5de9STakashi Sakamoto t.max = rounddown(s->max, step); 165826b5de9STakashi Sakamoto t.integer = 1; 16659502295STakashi Sakamoto 16759502295STakashi Sakamoto return snd_interval_refine(s, &t); 16859502295STakashi Sakamoto } 16959502295STakashi Sakamoto 170d67c46b9STakashi Sakamoto /** 171d67c46b9STakashi Sakamoto * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream 172d67c46b9STakashi Sakamoto * @s: the AMDTP stream, which must be initialized. 173d67c46b9STakashi Sakamoto * @runtime: the PCM substream runtime 174d67c46b9STakashi Sakamoto */ 175d67c46b9STakashi Sakamoto int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s, 176d67c46b9STakashi Sakamoto struct snd_pcm_runtime *runtime) 177d67c46b9STakashi Sakamoto { 17855799c5aSTakashi Sakamoto struct snd_pcm_hardware *hw = &runtime->hw; 179d67c46b9STakashi Sakamoto int err; 180d67c46b9STakashi Sakamoto 18155799c5aSTakashi Sakamoto hw->info = SNDRV_PCM_INFO_BATCH | 18255799c5aSTakashi Sakamoto SNDRV_PCM_INFO_BLOCK_TRANSFER | 18355799c5aSTakashi Sakamoto SNDRV_PCM_INFO_INTERLEAVED | 18455799c5aSTakashi Sakamoto SNDRV_PCM_INFO_JOINT_DUPLEX | 18555799c5aSTakashi Sakamoto SNDRV_PCM_INFO_MMAP | 18655799c5aSTakashi Sakamoto SNDRV_PCM_INFO_MMAP_VALID; 18755799c5aSTakashi Sakamoto 18855799c5aSTakashi Sakamoto /* SNDRV_PCM_INFO_BATCH */ 18955799c5aSTakashi Sakamoto hw->periods_min = 2; 19055799c5aSTakashi Sakamoto hw->periods_max = UINT_MAX; 19155799c5aSTakashi Sakamoto 19255799c5aSTakashi Sakamoto /* bytes for a frame */ 19355799c5aSTakashi Sakamoto hw->period_bytes_min = 4 * hw->channels_max; 19455799c5aSTakashi Sakamoto 19555799c5aSTakashi Sakamoto /* Just to prevent from allocating much pages. */ 19655799c5aSTakashi Sakamoto hw->period_bytes_max = hw->period_bytes_min * 2048; 19755799c5aSTakashi Sakamoto hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min; 19855799c5aSTakashi Sakamoto 199d67c46b9STakashi Sakamoto /* 200d67c46b9STakashi Sakamoto * Currently firewire-lib processes 16 packets in one software 201d67c46b9STakashi Sakamoto * interrupt callback. This equals to 2msec but actually the 202d67c46b9STakashi Sakamoto * interval of the interrupts has a jitter. 203d67c46b9STakashi Sakamoto * Additionally, even if adding a constraint to fit period size to 204d67c46b9STakashi Sakamoto * 2msec, actual calculated frames per period doesn't equal to 2msec, 205d67c46b9STakashi Sakamoto * depending on sampling rate. 206d67c46b9STakashi Sakamoto * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec. 207d67c46b9STakashi Sakamoto * Here let us use 5msec for safe period interrupt. 208d67c46b9STakashi Sakamoto */ 209d67c46b9STakashi Sakamoto err = snd_pcm_hw_constraint_minmax(runtime, 210d67c46b9STakashi Sakamoto SNDRV_PCM_HW_PARAM_PERIOD_TIME, 211d67c46b9STakashi Sakamoto 5000, UINT_MAX); 212d67c46b9STakashi Sakamoto if (err < 0) 213d67c46b9STakashi Sakamoto goto end; 214d67c46b9STakashi Sakamoto 215d67c46b9STakashi Sakamoto /* Non-Blocking stream has no more constraints */ 216d67c46b9STakashi Sakamoto if (!(s->flags & CIP_BLOCKING)) 217d67c46b9STakashi Sakamoto goto end; 218d67c46b9STakashi Sakamoto 219d67c46b9STakashi Sakamoto /* 220d67c46b9STakashi Sakamoto * One AMDTP packet can include some frames. In blocking mode, the 221d67c46b9STakashi Sakamoto * number equals to SYT_INTERVAL. So the number is 8, 16 or 32, 222d67c46b9STakashi Sakamoto * depending on its sampling rate. For accurate period interrupt, it's 223d67c46b9STakashi Sakamoto * preferrable to align period/buffer sizes to current SYT_INTERVAL. 224d67c46b9STakashi Sakamoto */ 22559502295STakashi Sakamoto err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 22659502295STakashi Sakamoto apply_constraint_to_size, NULL, 227826b5de9STakashi Sakamoto SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 22859502295STakashi Sakamoto SNDRV_PCM_HW_PARAM_RATE, -1); 229d67c46b9STakashi Sakamoto if (err < 0) 230d67c46b9STakashi Sakamoto goto end; 23159502295STakashi Sakamoto err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 23259502295STakashi Sakamoto apply_constraint_to_size, NULL, 233826b5de9STakashi Sakamoto SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 23459502295STakashi Sakamoto SNDRV_PCM_HW_PARAM_RATE, -1); 23559502295STakashi Sakamoto if (err < 0) 23659502295STakashi Sakamoto goto end; 237d67c46b9STakashi Sakamoto end: 238d67c46b9STakashi Sakamoto return err; 239d67c46b9STakashi Sakamoto } 240d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints); 241d67c46b9STakashi Sakamoto 242d67c46b9STakashi Sakamoto /** 243d67c46b9STakashi Sakamoto * amdtp_stream_set_parameters - set stream parameters 244d67c46b9STakashi Sakamoto * @s: the AMDTP stream to configure 245d67c46b9STakashi Sakamoto * @rate: the sample rate 246df075feeSTakashi Sakamoto * @data_block_quadlets: the size of a data block in quadlet unit 247d67c46b9STakashi Sakamoto * 248d67c46b9STakashi Sakamoto * The parameters must be set before the stream is started, and must not be 249d67c46b9STakashi Sakamoto * changed while the stream is running. 250d67c46b9STakashi Sakamoto */ 251df075feeSTakashi Sakamoto int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate, 252df075feeSTakashi Sakamoto unsigned int data_block_quadlets) 253d67c46b9STakashi Sakamoto { 254df075feeSTakashi Sakamoto unsigned int sfc; 255d67c46b9STakashi Sakamoto 256d67c46b9STakashi Sakamoto for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) { 257d67c46b9STakashi Sakamoto if (amdtp_rate_table[sfc] == rate) 258d67c46b9STakashi Sakamoto break; 259d67c46b9STakashi Sakamoto } 260d67c46b9STakashi Sakamoto if (sfc == ARRAY_SIZE(amdtp_rate_table)) 261d67c46b9STakashi Sakamoto return -EINVAL; 262d67c46b9STakashi Sakamoto 263d67c46b9STakashi Sakamoto s->sfc = sfc; 264df075feeSTakashi Sakamoto s->data_block_quadlets = data_block_quadlets; 265d67c46b9STakashi Sakamoto s->syt_interval = amdtp_syt_intervals[sfc]; 266d67c46b9STakashi Sakamoto 267d3d10a4aSTakashi Sakamoto // default buffering in the device. 268d3d10a4aSTakashi Sakamoto if (s->direction == AMDTP_OUT_STREAM) { 269d3d10a4aSTakashi Sakamoto s->ctx_data.rx.transfer_delay = 270d3d10a4aSTakashi Sakamoto TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE; 271d3d10a4aSTakashi Sakamoto 272d3d10a4aSTakashi Sakamoto if (s->flags & CIP_BLOCKING) { 273d3d10a4aSTakashi Sakamoto // additional buffering needed to adjust for no-data 274d3d10a4aSTakashi Sakamoto // packets. 275d3d10a4aSTakashi Sakamoto s->ctx_data.rx.transfer_delay += 276d3d10a4aSTakashi Sakamoto TICKS_PER_SECOND * s->syt_interval / rate; 277d3d10a4aSTakashi Sakamoto } 278d3d10a4aSTakashi Sakamoto } 279d67c46b9STakashi Sakamoto 280d67c46b9STakashi Sakamoto return 0; 281d67c46b9STakashi Sakamoto } 282d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_set_parameters); 283d67c46b9STakashi Sakamoto 284d67c46b9STakashi Sakamoto /** 285d67c46b9STakashi Sakamoto * amdtp_stream_get_max_payload - get the stream's packet size 286d67c46b9STakashi Sakamoto * @s: the AMDTP stream 287d67c46b9STakashi Sakamoto * 288d67c46b9STakashi Sakamoto * This function must not be called before the stream has been configured 289d67c46b9STakashi Sakamoto * with amdtp_stream_set_parameters(). 290d67c46b9STakashi Sakamoto */ 291d67c46b9STakashi Sakamoto unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s) 292d67c46b9STakashi Sakamoto { 293d67c46b9STakashi Sakamoto unsigned int multiplier = 1; 29407ea238cSTakashi Sakamoto unsigned int cip_header_size = 0; 295d67c46b9STakashi Sakamoto 296d67c46b9STakashi Sakamoto if (s->flags & CIP_JUMBO_PAYLOAD) 297d67c46b9STakashi Sakamoto multiplier = 5; 2983b196c39STakashi Sakamoto if (!(s->flags & CIP_NO_HEADER)) 29907ea238cSTakashi Sakamoto cip_header_size = sizeof(__be32) * 2; 300d67c46b9STakashi Sakamoto 30107ea238cSTakashi Sakamoto return cip_header_size + 30207ea238cSTakashi Sakamoto s->syt_interval * s->data_block_quadlets * sizeof(__be32) * multiplier; 303d67c46b9STakashi Sakamoto } 304d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_get_max_payload); 305d67c46b9STakashi Sakamoto 306d67c46b9STakashi Sakamoto /** 307d67c46b9STakashi Sakamoto * amdtp_stream_pcm_prepare - prepare PCM device for running 308d67c46b9STakashi Sakamoto * @s: the AMDTP stream 309d67c46b9STakashi Sakamoto * 310d67c46b9STakashi Sakamoto * This function should be called from the PCM device's .prepare callback. 311d67c46b9STakashi Sakamoto */ 312d67c46b9STakashi Sakamoto void amdtp_stream_pcm_prepare(struct amdtp_stream *s) 313d67c46b9STakashi Sakamoto { 314d67c46b9STakashi Sakamoto tasklet_kill(&s->period_tasklet); 315d67c46b9STakashi Sakamoto s->pcm_buffer_pointer = 0; 316d67c46b9STakashi Sakamoto s->pcm_period_pointer = 0; 317d67c46b9STakashi Sakamoto } 318d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_prepare); 319d67c46b9STakashi Sakamoto 320d67c46b9STakashi Sakamoto static unsigned int calculate_data_blocks(struct amdtp_stream *s, 321d67c46b9STakashi Sakamoto unsigned int syt) 322d67c46b9STakashi Sakamoto { 323d67c46b9STakashi Sakamoto unsigned int phase, data_blocks; 324d67c46b9STakashi Sakamoto 325d67c46b9STakashi Sakamoto /* Blocking mode. */ 326d67c46b9STakashi Sakamoto if (s->flags & CIP_BLOCKING) { 327d67c46b9STakashi Sakamoto /* This module generate empty packet for 'no data'. */ 328d67c46b9STakashi Sakamoto if (syt == CIP_SYT_NO_INFO) 329d67c46b9STakashi Sakamoto data_blocks = 0; 330d67c46b9STakashi Sakamoto else 331d67c46b9STakashi Sakamoto data_blocks = s->syt_interval; 332d67c46b9STakashi Sakamoto /* Non-blocking mode. */ 333d67c46b9STakashi Sakamoto } else { 334d67c46b9STakashi Sakamoto if (!cip_sfc_is_base_44100(s->sfc)) { 335d3d10a4aSTakashi Sakamoto // Sample_rate / 8000 is an integer, and precomputed. 336d3d10a4aSTakashi Sakamoto data_blocks = s->ctx_data.rx.data_block_state; 337d67c46b9STakashi Sakamoto } else { 338d3d10a4aSTakashi Sakamoto phase = s->ctx_data.rx.data_block_state; 339d67c46b9STakashi Sakamoto 340d67c46b9STakashi Sakamoto /* 341d67c46b9STakashi Sakamoto * This calculates the number of data blocks per packet so that 342d67c46b9STakashi Sakamoto * 1) the overall rate is correct and exactly synchronized to 343d67c46b9STakashi Sakamoto * the bus clock, and 344d67c46b9STakashi Sakamoto * 2) packets with a rounded-up number of blocks occur as early 345d67c46b9STakashi Sakamoto * as possible in the sequence (to prevent underruns of the 346d67c46b9STakashi Sakamoto * device's buffer). 347d67c46b9STakashi Sakamoto */ 348d67c46b9STakashi Sakamoto if (s->sfc == CIP_SFC_44100) 349d67c46b9STakashi Sakamoto /* 6 6 5 6 5 6 5 ... */ 350d67c46b9STakashi Sakamoto data_blocks = 5 + ((phase & 1) ^ 351d67c46b9STakashi Sakamoto (phase == 0 || phase >= 40)); 352d67c46b9STakashi Sakamoto else 353d67c46b9STakashi Sakamoto /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */ 354d67c46b9STakashi Sakamoto data_blocks = 11 * (s->sfc >> 1) + (phase == 0); 355d67c46b9STakashi Sakamoto if (++phase >= (80 >> (s->sfc >> 1))) 356d67c46b9STakashi Sakamoto phase = 0; 357d3d10a4aSTakashi Sakamoto s->ctx_data.rx.data_block_state = phase; 358d67c46b9STakashi Sakamoto } 359d67c46b9STakashi Sakamoto } 360d67c46b9STakashi Sakamoto 361d67c46b9STakashi Sakamoto return data_blocks; 362d67c46b9STakashi Sakamoto } 363d67c46b9STakashi Sakamoto 364d67c46b9STakashi Sakamoto static unsigned int calculate_syt(struct amdtp_stream *s, 365d67c46b9STakashi Sakamoto unsigned int cycle) 366d67c46b9STakashi Sakamoto { 367d67c46b9STakashi Sakamoto unsigned int syt_offset, phase, index, syt; 368d67c46b9STakashi Sakamoto 369d3d10a4aSTakashi Sakamoto if (s->ctx_data.rx.last_syt_offset < TICKS_PER_CYCLE) { 370d67c46b9STakashi Sakamoto if (!cip_sfc_is_base_44100(s->sfc)) 371d3d10a4aSTakashi Sakamoto syt_offset = s->ctx_data.rx.last_syt_offset + 372d3d10a4aSTakashi Sakamoto s->ctx_data.rx.syt_offset_state; 373d67c46b9STakashi Sakamoto else { 374d67c46b9STakashi Sakamoto /* 375d67c46b9STakashi Sakamoto * The time, in ticks, of the n'th SYT_INTERVAL sample is: 376d67c46b9STakashi Sakamoto * n * SYT_INTERVAL * 24576000 / sample_rate 377d67c46b9STakashi Sakamoto * Modulo TICKS_PER_CYCLE, the difference between successive 378d67c46b9STakashi Sakamoto * elements is about 1386.23. Rounding the results of this 379d67c46b9STakashi Sakamoto * formula to the SYT precision results in a sequence of 380d67c46b9STakashi Sakamoto * differences that begins with: 381d67c46b9STakashi Sakamoto * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ... 382d67c46b9STakashi Sakamoto * This code generates _exactly_ the same sequence. 383d67c46b9STakashi Sakamoto */ 384d3d10a4aSTakashi Sakamoto phase = s->ctx_data.rx.syt_offset_state; 385d67c46b9STakashi Sakamoto index = phase % 13; 386d3d10a4aSTakashi Sakamoto syt_offset = s->ctx_data.rx.last_syt_offset; 387d67c46b9STakashi Sakamoto syt_offset += 1386 + ((index && !(index & 3)) || 388d67c46b9STakashi Sakamoto phase == 146); 389d67c46b9STakashi Sakamoto if (++phase >= 147) 390d67c46b9STakashi Sakamoto phase = 0; 391d3d10a4aSTakashi Sakamoto s->ctx_data.rx.syt_offset_state = phase; 392d67c46b9STakashi Sakamoto } 393d67c46b9STakashi Sakamoto } else 394d3d10a4aSTakashi Sakamoto syt_offset = s->ctx_data.rx.last_syt_offset - TICKS_PER_CYCLE; 395d3d10a4aSTakashi Sakamoto s->ctx_data.rx.last_syt_offset = syt_offset; 396d67c46b9STakashi Sakamoto 397d67c46b9STakashi Sakamoto if (syt_offset < TICKS_PER_CYCLE) { 398d3d10a4aSTakashi Sakamoto syt_offset += s->ctx_data.rx.transfer_delay; 399d67c46b9STakashi Sakamoto syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12; 400d67c46b9STakashi Sakamoto syt += syt_offset % TICKS_PER_CYCLE; 401d67c46b9STakashi Sakamoto 402d67c46b9STakashi Sakamoto return syt & CIP_SYT_MASK; 403d67c46b9STakashi Sakamoto } else { 404d67c46b9STakashi Sakamoto return CIP_SYT_NO_INFO; 405d67c46b9STakashi Sakamoto } 406d67c46b9STakashi Sakamoto } 407d67c46b9STakashi Sakamoto 408d67c46b9STakashi Sakamoto static void update_pcm_pointers(struct amdtp_stream *s, 409d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm, 410d67c46b9STakashi Sakamoto unsigned int frames) 411d67c46b9STakashi Sakamoto { 412d67c46b9STakashi Sakamoto unsigned int ptr; 413d67c46b9STakashi Sakamoto 414d67c46b9STakashi Sakamoto ptr = s->pcm_buffer_pointer + frames; 415d67c46b9STakashi Sakamoto if (ptr >= pcm->runtime->buffer_size) 416d67c46b9STakashi Sakamoto ptr -= pcm->runtime->buffer_size; 4176aa7de05SMark Rutland WRITE_ONCE(s->pcm_buffer_pointer, ptr); 418d67c46b9STakashi Sakamoto 419d67c46b9STakashi Sakamoto s->pcm_period_pointer += frames; 420d67c46b9STakashi Sakamoto if (s->pcm_period_pointer >= pcm->runtime->period_size) { 421d67c46b9STakashi Sakamoto s->pcm_period_pointer -= pcm->runtime->period_size; 422d67c46b9STakashi Sakamoto tasklet_hi_schedule(&s->period_tasklet); 423d67c46b9STakashi Sakamoto } 424d67c46b9STakashi Sakamoto } 425d67c46b9STakashi Sakamoto 426d67c46b9STakashi Sakamoto static void pcm_period_tasklet(unsigned long data) 427d67c46b9STakashi Sakamoto { 428d67c46b9STakashi Sakamoto struct amdtp_stream *s = (void *)data; 4296aa7de05SMark Rutland struct snd_pcm_substream *pcm = READ_ONCE(s->pcm); 430d67c46b9STakashi Sakamoto 431d67c46b9STakashi Sakamoto if (pcm) 432d67c46b9STakashi Sakamoto snd_pcm_period_elapsed(pcm); 433d67c46b9STakashi Sakamoto } 434d67c46b9STakashi Sakamoto 435*e229853dSTakashi Sakamoto static int queue_packet(struct amdtp_stream *s, struct fw_iso_packet *params, 436*e229853dSTakashi Sakamoto bool sched_irq) 437d67c46b9STakashi Sakamoto { 4386007bf54STakashi Sakamoto int err; 439d67c46b9STakashi Sakamoto 440*e229853dSTakashi Sakamoto params->interrupt = sched_irq; 4416007bf54STakashi Sakamoto params->tag = s->tag; 4426007bf54STakashi Sakamoto params->sy = 0; 443d67c46b9STakashi Sakamoto 4446007bf54STakashi Sakamoto err = fw_iso_context_queue(s->context, params, &s->buffer.iso_buffer, 445d67c46b9STakashi Sakamoto s->buffer.packets[s->packet_index].offset); 446d67c46b9STakashi Sakamoto if (err < 0) { 447d67c46b9STakashi Sakamoto dev_err(&s->unit->device, "queueing error: %d\n", err); 448d67c46b9STakashi Sakamoto goto end; 449d67c46b9STakashi Sakamoto } 450d67c46b9STakashi Sakamoto 451a0e02331STakashi Sakamoto if (++s->packet_index >= s->queue_size) 452d67c46b9STakashi Sakamoto s->packet_index = 0; 453d67c46b9STakashi Sakamoto end: 454d67c46b9STakashi Sakamoto return err; 455d67c46b9STakashi Sakamoto } 456d67c46b9STakashi Sakamoto 457d67c46b9STakashi Sakamoto static inline int queue_out_packet(struct amdtp_stream *s, 458*e229853dSTakashi Sakamoto struct fw_iso_packet *params, bool sched_irq) 459d67c46b9STakashi Sakamoto { 460b18f0cfaSTakashi Sakamoto params->skip = 461b18f0cfaSTakashi Sakamoto !!(params->header_length == 0 && params->payload_length == 0); 462*e229853dSTakashi Sakamoto return queue_packet(s, params, sched_irq); 463d67c46b9STakashi Sakamoto } 464d67c46b9STakashi Sakamoto 4656007bf54STakashi Sakamoto static inline int queue_in_packet(struct amdtp_stream *s, 466*e229853dSTakashi Sakamoto struct fw_iso_packet *params, bool sched_irq) 467d67c46b9STakashi Sakamoto { 4686007bf54STakashi Sakamoto // Queue one packet for IR context. 4696007bf54STakashi Sakamoto params->header_length = s->ctx_data.tx.ctx_header_size; 4706007bf54STakashi Sakamoto params->payload_length = s->ctx_data.tx.max_ctx_payload_length; 4716007bf54STakashi Sakamoto params->skip = false; 472*e229853dSTakashi Sakamoto return queue_packet(s, params, sched_irq); 473d67c46b9STakashi Sakamoto } 474d67c46b9STakashi Sakamoto 475252219c7STakashi Sakamoto static void generate_cip_header(struct amdtp_stream *s, __be32 cip_header[2], 476860d798cSTakashi Sakamoto unsigned int data_block_counter, unsigned int syt) 477252219c7STakashi Sakamoto { 478252219c7STakashi Sakamoto cip_header[0] = cpu_to_be32(READ_ONCE(s->source_node_id_field) | 479252219c7STakashi Sakamoto (s->data_block_quadlets << CIP_DBS_SHIFT) | 480252219c7STakashi Sakamoto ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) | 481860d798cSTakashi Sakamoto data_block_counter); 482252219c7STakashi Sakamoto cip_header[1] = cpu_to_be32(CIP_EOH | 483252219c7STakashi Sakamoto ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) | 484252219c7STakashi Sakamoto ((s->ctx_data.rx.fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) | 485252219c7STakashi Sakamoto (syt & CIP_SYT_MASK)); 486252219c7STakashi Sakamoto } 487252219c7STakashi Sakamoto 4886bc1a269STakashi Sakamoto static void build_it_pkt_header(struct amdtp_stream *s, unsigned int cycle, 4896bc1a269STakashi Sakamoto struct fw_iso_packet *params, 490860d798cSTakashi Sakamoto unsigned int data_blocks, 491860d798cSTakashi Sakamoto unsigned int data_block_counter, 492860d798cSTakashi Sakamoto unsigned int syt, unsigned int index) 493d67c46b9STakashi Sakamoto { 4940ebf3cebSTakashi Sakamoto unsigned int payload_length; 49516be4589STakashi Sakamoto __be32 *cip_header; 496d67c46b9STakashi Sakamoto 4970ebf3cebSTakashi Sakamoto payload_length = data_blocks * sizeof(__be32) * s->data_block_quadlets; 4980ebf3cebSTakashi Sakamoto params->payload_length = payload_length; 4990ebf3cebSTakashi Sakamoto 500b18f0cfaSTakashi Sakamoto if (!(s->flags & CIP_NO_HEADER)) { 5016bc1a269STakashi Sakamoto cip_header = (__be32 *)params->header; 502860d798cSTakashi Sakamoto generate_cip_header(s, cip_header, data_block_counter, syt); 5036bc1a269STakashi Sakamoto params->header_length = 2 * sizeof(__be32); 5040ebf3cebSTakashi Sakamoto payload_length += params->header_length; 505b18f0cfaSTakashi Sakamoto } else { 506b18f0cfaSTakashi Sakamoto cip_header = NULL; 507b18f0cfaSTakashi Sakamoto } 508d67c46b9STakashi Sakamoto 509213fa989STakashi Sakamoto trace_amdtp_packet(s, cycle, cip_header, payload_length, data_blocks, 510860d798cSTakashi Sakamoto data_block_counter, index); 5113b196c39STakashi Sakamoto } 5123b196c39STakashi Sakamoto 513e335425bSTakashi Sakamoto static int check_cip_header(struct amdtp_stream *s, const __be32 *buf, 514e335425bSTakashi Sakamoto unsigned int payload_length, 515a35463d1STakashi Sakamoto unsigned int *data_blocks, 516a35463d1STakashi Sakamoto unsigned int *data_block_counter, unsigned int *syt) 517d67c46b9STakashi Sakamoto { 518d67c46b9STakashi Sakamoto u32 cip_header[2]; 519e335425bSTakashi Sakamoto unsigned int sph; 520e335425bSTakashi Sakamoto unsigned int fmt; 521e335425bSTakashi Sakamoto unsigned int fdf; 522a35463d1STakashi Sakamoto unsigned int dbc; 523d67c46b9STakashi Sakamoto bool lost; 524d67c46b9STakashi Sakamoto 525e335425bSTakashi Sakamoto cip_header[0] = be32_to_cpu(buf[0]); 526e335425bSTakashi Sakamoto cip_header[1] = be32_to_cpu(buf[1]); 527d67c46b9STakashi Sakamoto 528d67c46b9STakashi Sakamoto /* 529d67c46b9STakashi Sakamoto * This module supports 'Two-quadlet CIP header with SYT field'. 530d67c46b9STakashi Sakamoto * For convenience, also check FMT field is AM824 or not. 531d67c46b9STakashi Sakamoto */ 5322128f78fSTakashi Sakamoto if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) || 5332128f78fSTakashi Sakamoto ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) && 5342128f78fSTakashi Sakamoto (!(s->flags & CIP_HEADER_WITHOUT_EOH))) { 535d67c46b9STakashi Sakamoto dev_info_ratelimited(&s->unit->device, 536d67c46b9STakashi Sakamoto "Invalid CIP header for AMDTP: %08X:%08X\n", 537d67c46b9STakashi Sakamoto cip_header[0], cip_header[1]); 538e335425bSTakashi Sakamoto return -EAGAIN; 539d67c46b9STakashi Sakamoto } 540d67c46b9STakashi Sakamoto 541d67c46b9STakashi Sakamoto /* Check valid protocol or not. */ 5429863874fSTakashi Sakamoto sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT; 543d67c46b9STakashi Sakamoto fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT; 5449863874fSTakashi Sakamoto if (sph != s->sph || fmt != s->fmt) { 5452a7e1713STakashi Sakamoto dev_info_ratelimited(&s->unit->device, 546d67c46b9STakashi Sakamoto "Detect unexpected protocol: %08x %08x\n", 547d67c46b9STakashi Sakamoto cip_header[0], cip_header[1]); 548e335425bSTakashi Sakamoto return -EAGAIN; 549d67c46b9STakashi Sakamoto } 550d67c46b9STakashi Sakamoto 551d67c46b9STakashi Sakamoto /* Calculate data blocks */ 552d67c46b9STakashi Sakamoto fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT; 553e335425bSTakashi Sakamoto if (payload_length < sizeof(__be32) * 2 || 554d67c46b9STakashi Sakamoto (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) { 555e335425bSTakashi Sakamoto *data_blocks = 0; 556d67c46b9STakashi Sakamoto } else { 557e335425bSTakashi Sakamoto unsigned int data_block_quadlets = 558d67c46b9STakashi Sakamoto (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT; 559d67c46b9STakashi Sakamoto /* avoid division by zero */ 560d67c46b9STakashi Sakamoto if (data_block_quadlets == 0) { 561d67c46b9STakashi Sakamoto dev_err(&s->unit->device, 562d67c46b9STakashi Sakamoto "Detect invalid value in dbs field: %08X\n", 563d67c46b9STakashi Sakamoto cip_header[0]); 564d67c46b9STakashi Sakamoto return -EPROTO; 565d67c46b9STakashi Sakamoto } 566d67c46b9STakashi Sakamoto if (s->flags & CIP_WRONG_DBS) 567d67c46b9STakashi Sakamoto data_block_quadlets = s->data_block_quadlets; 568d67c46b9STakashi Sakamoto 569e335425bSTakashi Sakamoto *data_blocks = (payload_length / sizeof(__be32) - 2) / 570ff0fb5aaSTakashi Sakamoto data_block_quadlets; 571d67c46b9STakashi Sakamoto } 572d67c46b9STakashi Sakamoto 573d67c46b9STakashi Sakamoto /* Check data block counter continuity */ 574a35463d1STakashi Sakamoto dbc = cip_header[0] & CIP_DBC_MASK; 575e335425bSTakashi Sakamoto if (*data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) && 576a35463d1STakashi Sakamoto *data_block_counter != UINT_MAX) 577a35463d1STakashi Sakamoto dbc = *data_block_counter; 578d67c46b9STakashi Sakamoto 579a35463d1STakashi Sakamoto if ((dbc == 0x00 && (s->flags & CIP_SKIP_DBC_ZERO_CHECK)) || 580a35463d1STakashi Sakamoto *data_block_counter == UINT_MAX) { 581d67c46b9STakashi Sakamoto lost = false; 582d67c46b9STakashi Sakamoto } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) { 583a35463d1STakashi Sakamoto lost = dbc != *data_block_counter; 584d67c46b9STakashi Sakamoto } else { 585e335425bSTakashi Sakamoto unsigned int dbc_interval; 586e335425bSTakashi Sakamoto 587e335425bSTakashi Sakamoto if (*data_blocks > 0 && s->ctx_data.tx.dbc_interval > 0) 588d3d10a4aSTakashi Sakamoto dbc_interval = s->ctx_data.tx.dbc_interval; 589d67c46b9STakashi Sakamoto else 590e335425bSTakashi Sakamoto dbc_interval = *data_blocks; 591d67c46b9STakashi Sakamoto 592a35463d1STakashi Sakamoto lost = dbc != ((*data_block_counter + dbc_interval) & 0xff); 593d67c46b9STakashi Sakamoto } 594d67c46b9STakashi Sakamoto 595d67c46b9STakashi Sakamoto if (lost) { 596d67c46b9STakashi Sakamoto dev_err(&s->unit->device, 597d67c46b9STakashi Sakamoto "Detect discontinuity of CIP: %02X %02X\n", 598a35463d1STakashi Sakamoto *data_block_counter, dbc); 599d67c46b9STakashi Sakamoto return -EIO; 600d67c46b9STakashi Sakamoto } 601d67c46b9STakashi Sakamoto 602753e7179STakashi Sakamoto *data_block_counter = dbc; 603753e7179STakashi Sakamoto 604e335425bSTakashi Sakamoto *syt = cip_header[1] & CIP_SYT_MASK; 605e335425bSTakashi Sakamoto 606e335425bSTakashi Sakamoto return 0; 607e335425bSTakashi Sakamoto } 608e335425bSTakashi Sakamoto 60998e3e43bSTakashi Sakamoto static int parse_ir_ctx_header(struct amdtp_stream *s, unsigned int cycle, 61098e3e43bSTakashi Sakamoto const __be32 *ctx_header, 61198e3e43bSTakashi Sakamoto unsigned int *payload_length, 612a35463d1STakashi Sakamoto unsigned int *data_blocks, 613a35463d1STakashi Sakamoto unsigned int *data_block_counter, 614a35463d1STakashi Sakamoto unsigned int *syt, unsigned int index) 615e335425bSTakashi Sakamoto { 616f11453c7STakashi Sakamoto const __be32 *cip_header; 617e335425bSTakashi Sakamoto int err; 618e335425bSTakashi Sakamoto 61998e3e43bSTakashi Sakamoto *payload_length = be32_to_cpu(ctx_header[0]) >> ISO_DATA_LENGTH_SHIFT; 62098e3e43bSTakashi Sakamoto if (*payload_length > s->ctx_data.tx.ctx_header_size + 621f11453c7STakashi Sakamoto s->ctx_data.tx.max_ctx_payload_length) { 622e335425bSTakashi Sakamoto dev_err(&s->unit->device, 623e335425bSTakashi Sakamoto "Detect jumbo payload: %04x %04x\n", 62498e3e43bSTakashi Sakamoto *payload_length, s->ctx_data.tx.max_ctx_payload_length); 625e335425bSTakashi Sakamoto return -EIO; 626e335425bSTakashi Sakamoto } 627e335425bSTakashi Sakamoto 628947b437eSTakashi Sakamoto if (!(s->flags & CIP_NO_HEADER)) { 62998e3e43bSTakashi Sakamoto cip_header = ctx_header + 2; 63098e3e43bSTakashi Sakamoto err = check_cip_header(s, cip_header, *payload_length, 631a35463d1STakashi Sakamoto data_blocks, data_block_counter, syt); 632b8b0e24cSTakashi Sakamoto if (err < 0) 633e335425bSTakashi Sakamoto return err; 634947b437eSTakashi Sakamoto } else { 635947b437eSTakashi Sakamoto cip_header = NULL; 63676864868STakashi Sakamoto err = 0; 63798e3e43bSTakashi Sakamoto *data_blocks = *payload_length / sizeof(__be32) / 63898e3e43bSTakashi Sakamoto s->data_block_quadlets; 63998e3e43bSTakashi Sakamoto *syt = 0; 6407fbf9096STakashi Sakamoto 641a35463d1STakashi Sakamoto if (*data_block_counter == UINT_MAX) 642a35463d1STakashi Sakamoto *data_block_counter = 0; 643947b437eSTakashi Sakamoto } 644e335425bSTakashi Sakamoto 64598e3e43bSTakashi Sakamoto trace_amdtp_packet(s, cycle, cip_header, *payload_length, *data_blocks, 646a35463d1STakashi Sakamoto *data_block_counter, index); 64764d0bf4dSTakashi Sakamoto 64876864868STakashi Sakamoto return err; 649d67c46b9STakashi Sakamoto } 650d67c46b9STakashi Sakamoto 65126cd1e58STakashi Sakamoto // In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On 65226cd1e58STakashi Sakamoto // the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent 65326cd1e58STakashi Sakamoto // it. Thus, via Linux firewire subsystem, we can get the 3 bits for second. 65426cd1e58STakashi Sakamoto static inline u32 compute_cycle_count(__be32 ctx_header_tstamp) 65573fc7f08STakashi Sakamoto { 65626cd1e58STakashi Sakamoto u32 tstamp = be32_to_cpu(ctx_header_tstamp) & HEADER_TSTAMP_MASK; 65773fc7f08STakashi Sakamoto return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff); 65873fc7f08STakashi Sakamoto } 65973fc7f08STakashi Sakamoto 66073fc7f08STakashi Sakamoto static inline u32 increment_cycle_count(u32 cycle, unsigned int addend) 66173fc7f08STakashi Sakamoto { 66273fc7f08STakashi Sakamoto cycle += addend; 66373fc7f08STakashi Sakamoto if (cycle >= 8 * CYCLES_PER_SECOND) 66473fc7f08STakashi Sakamoto cycle -= 8 * CYCLES_PER_SECOND; 66573fc7f08STakashi Sakamoto return cycle; 66673fc7f08STakashi Sakamoto } 66773fc7f08STakashi Sakamoto 66826cd1e58STakashi Sakamoto // Align to actual cycle count for the packet which is going to be scheduled. 669a0e02331STakashi Sakamoto // This module queued the same number of isochronous cycle as the size of queue 670a0e02331STakashi Sakamoto // to kip isochronous cycle, therefore it's OK to just increment the cycle by 671a0e02331STakashi Sakamoto // the size of queue for scheduled cycle. 672a0e02331STakashi Sakamoto static inline u32 compute_it_cycle(const __be32 ctx_header_tstamp, 673a0e02331STakashi Sakamoto unsigned int queue_size) 67426cd1e58STakashi Sakamoto { 67526cd1e58STakashi Sakamoto u32 cycle = compute_cycle_count(ctx_header_tstamp); 676a0e02331STakashi Sakamoto return increment_cycle_count(cycle, queue_size); 67726cd1e58STakashi Sakamoto } 67826cd1e58STakashi Sakamoto 679753e7179STakashi Sakamoto static int generate_device_pkt_descs(struct amdtp_stream *s, 680753e7179STakashi Sakamoto struct pkt_desc *descs, 681753e7179STakashi Sakamoto const __be32 *ctx_header, 682753e7179STakashi Sakamoto unsigned int packets) 683753e7179STakashi Sakamoto { 684753e7179STakashi Sakamoto unsigned int dbc = s->data_block_counter; 685753e7179STakashi Sakamoto int i; 686753e7179STakashi Sakamoto int err; 687753e7179STakashi Sakamoto 688753e7179STakashi Sakamoto for (i = 0; i < packets; ++i) { 689753e7179STakashi Sakamoto struct pkt_desc *desc = descs + i; 690a0e02331STakashi Sakamoto unsigned int index = (s->packet_index + i) % s->queue_size; 691753e7179STakashi Sakamoto unsigned int cycle; 692753e7179STakashi Sakamoto unsigned int payload_length; 693753e7179STakashi Sakamoto unsigned int data_blocks; 694753e7179STakashi Sakamoto unsigned int syt; 695753e7179STakashi Sakamoto 696753e7179STakashi Sakamoto cycle = compute_cycle_count(ctx_header[1]); 697753e7179STakashi Sakamoto 698753e7179STakashi Sakamoto err = parse_ir_ctx_header(s, cycle, ctx_header, &payload_length, 699753e7179STakashi Sakamoto &data_blocks, &dbc, &syt, i); 700753e7179STakashi Sakamoto if (err < 0) 701753e7179STakashi Sakamoto return err; 702753e7179STakashi Sakamoto 703753e7179STakashi Sakamoto desc->cycle = cycle; 704753e7179STakashi Sakamoto desc->syt = syt; 705753e7179STakashi Sakamoto desc->data_blocks = data_blocks; 706753e7179STakashi Sakamoto desc->data_block_counter = dbc; 707753e7179STakashi Sakamoto desc->ctx_payload = s->buffer.packets[index].buffer; 708753e7179STakashi Sakamoto 709753e7179STakashi Sakamoto if (!(s->flags & CIP_DBC_IS_END_EVENT)) 710753e7179STakashi Sakamoto dbc = (dbc + desc->data_blocks) & 0xff; 711753e7179STakashi Sakamoto 712753e7179STakashi Sakamoto ctx_header += 713753e7179STakashi Sakamoto s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header); 714753e7179STakashi Sakamoto } 715753e7179STakashi Sakamoto 716753e7179STakashi Sakamoto s->data_block_counter = dbc; 717753e7179STakashi Sakamoto 718753e7179STakashi Sakamoto return 0; 719753e7179STakashi Sakamoto } 720753e7179STakashi Sakamoto 721f4f6ae7bSTakashi Sakamoto static void generate_ideal_pkt_descs(struct amdtp_stream *s, 722f4f6ae7bSTakashi Sakamoto struct pkt_desc *descs, 723f4f6ae7bSTakashi Sakamoto const __be32 *ctx_header, 724f4f6ae7bSTakashi Sakamoto unsigned int packets) 725f4f6ae7bSTakashi Sakamoto { 726f4f6ae7bSTakashi Sakamoto unsigned int dbc = s->data_block_counter; 727f4f6ae7bSTakashi Sakamoto int i; 728f4f6ae7bSTakashi Sakamoto 729f4f6ae7bSTakashi Sakamoto for (i = 0; i < packets; ++i) { 730f4f6ae7bSTakashi Sakamoto struct pkt_desc *desc = descs + i; 731a0e02331STakashi Sakamoto unsigned int index = (s->packet_index + i) % s->queue_size; 732f4f6ae7bSTakashi Sakamoto 733a0e02331STakashi Sakamoto desc->cycle = compute_it_cycle(*ctx_header, s->queue_size); 734f4f6ae7bSTakashi Sakamoto desc->syt = calculate_syt(s, desc->cycle); 735f4f6ae7bSTakashi Sakamoto desc->data_blocks = calculate_data_blocks(s, desc->syt); 736f4f6ae7bSTakashi Sakamoto 737f4f6ae7bSTakashi Sakamoto if (s->flags & CIP_DBC_IS_END_EVENT) 738f4f6ae7bSTakashi Sakamoto dbc = (dbc + desc->data_blocks) & 0xff; 739f4f6ae7bSTakashi Sakamoto 740f4f6ae7bSTakashi Sakamoto desc->data_block_counter = dbc; 741f4f6ae7bSTakashi Sakamoto 742f4f6ae7bSTakashi Sakamoto if (!(s->flags & CIP_DBC_IS_END_EVENT)) 743f4f6ae7bSTakashi Sakamoto dbc = (dbc + desc->data_blocks) & 0xff; 744f4f6ae7bSTakashi Sakamoto 745f4f6ae7bSTakashi Sakamoto desc->ctx_payload = s->buffer.packets[index].buffer; 746f4f6ae7bSTakashi Sakamoto 747f4f6ae7bSTakashi Sakamoto ++ctx_header; 748f4f6ae7bSTakashi Sakamoto } 749f4f6ae7bSTakashi Sakamoto 750f4f6ae7bSTakashi Sakamoto s->data_block_counter = dbc; 751f4f6ae7bSTakashi Sakamoto } 752f4f6ae7bSTakashi Sakamoto 753fce9b013STakashi Sakamoto static inline void cancel_stream(struct amdtp_stream *s) 754fce9b013STakashi Sakamoto { 755fce9b013STakashi Sakamoto s->packet_index = -1; 756fce9b013STakashi Sakamoto if (in_interrupt()) 757fce9b013STakashi Sakamoto amdtp_stream_pcm_abort(s); 758fce9b013STakashi Sakamoto WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN); 759fce9b013STakashi Sakamoto } 760fce9b013STakashi Sakamoto 7610f5cfcb2STakashi Sakamoto static void process_ctx_payloads(struct amdtp_stream *s, 7620f5cfcb2STakashi Sakamoto const struct pkt_desc *descs, 7630f5cfcb2STakashi Sakamoto unsigned int packets) 7640f5cfcb2STakashi Sakamoto { 7659a738ad1STakashi Sakamoto struct snd_pcm_substream *pcm; 7660f5cfcb2STakashi Sakamoto unsigned int pcm_frames; 7670f5cfcb2STakashi Sakamoto 7689a738ad1STakashi Sakamoto pcm = READ_ONCE(s->pcm); 7699a738ad1STakashi Sakamoto pcm_frames = s->process_ctx_payloads(s, descs, packets, pcm); 7709a738ad1STakashi Sakamoto if (pcm) 7710f5cfcb2STakashi Sakamoto update_pcm_pointers(s, pcm, pcm_frames); 7720f5cfcb2STakashi Sakamoto } 7730f5cfcb2STakashi Sakamoto 77473fc7f08STakashi Sakamoto static void out_stream_callback(struct fw_iso_context *context, u32 tstamp, 775d67c46b9STakashi Sakamoto size_t header_length, void *header, 776d67c46b9STakashi Sakamoto void *private_data) 777d67c46b9STakashi Sakamoto { 778d67c46b9STakashi Sakamoto struct amdtp_stream *s = private_data; 77926cd1e58STakashi Sakamoto const __be32 *ctx_header = header; 780*e229853dSTakashi Sakamoto unsigned int events_per_period = s->events_per_period; 781*e229853dSTakashi Sakamoto unsigned int event_count = s->event_count; 782a0e02331STakashi Sakamoto unsigned int packets; 7830dcb4efbSTakashi Sakamoto int i; 784d67c46b9STakashi Sakamoto 785d67c46b9STakashi Sakamoto if (s->packet_index < 0) 786d67c46b9STakashi Sakamoto return; 787d67c46b9STakashi Sakamoto 788a0e02331STakashi Sakamoto // Calculate the number of packets in buffer and check XRUN. 789a0e02331STakashi Sakamoto packets = header_length / sizeof(*ctx_header); 790a0e02331STakashi Sakamoto 791f4f6ae7bSTakashi Sakamoto generate_ideal_pkt_descs(s, s->pkt_descs, ctx_header, packets); 792f4f6ae7bSTakashi Sakamoto 7930f5cfcb2STakashi Sakamoto process_ctx_payloads(s, s->pkt_descs, packets); 7945e2ece0fSTakashi Sakamoto 7955e2ece0fSTakashi Sakamoto for (i = 0; i < packets; ++i) { 7965e2ece0fSTakashi Sakamoto const struct pkt_desc *desc = s->pkt_descs + i; 797f4f6ae7bSTakashi Sakamoto unsigned int syt; 7986bc1a269STakashi Sakamoto struct { 7996bc1a269STakashi Sakamoto struct fw_iso_packet params; 8006bc1a269STakashi Sakamoto __be32 header[IT_PKT_HEADER_SIZE_CIP / sizeof(__be32)]; 8016bc1a269STakashi Sakamoto } template = { {0}, {0} }; 802*e229853dSTakashi Sakamoto bool sched_irq = false; 803860d798cSTakashi Sakamoto 804f4f6ae7bSTakashi Sakamoto if (s->ctx_data.rx.syt_override < 0) 805f4f6ae7bSTakashi Sakamoto syt = desc->syt; 806f4f6ae7bSTakashi Sakamoto else 8073baf3053STakashi Sakamoto syt = s->ctx_data.rx.syt_override; 8083baf3053STakashi Sakamoto 809f4f6ae7bSTakashi Sakamoto build_it_pkt_header(s, desc->cycle, &template.params, 810f4f6ae7bSTakashi Sakamoto desc->data_blocks, desc->data_block_counter, 811f4f6ae7bSTakashi Sakamoto syt, i); 8126bc1a269STakashi Sakamoto 813*e229853dSTakashi Sakamoto event_count += desc->data_blocks; 814*e229853dSTakashi Sakamoto if (event_count >= events_per_period) { 815*e229853dSTakashi Sakamoto event_count -= events_per_period; 816*e229853dSTakashi Sakamoto sched_irq = true; 817*e229853dSTakashi Sakamoto } 818*e229853dSTakashi Sakamoto 819*e229853dSTakashi Sakamoto if (queue_out_packet(s, &template.params, sched_irq) < 0) { 820fce9b013STakashi Sakamoto cancel_stream(s); 821d67c46b9STakashi Sakamoto return; 822d67c46b9STakashi Sakamoto } 823d67c46b9STakashi Sakamoto } 824d67c46b9STakashi Sakamoto 825*e229853dSTakashi Sakamoto s->event_count = event_count; 826*e229853dSTakashi Sakamoto 827d67c46b9STakashi Sakamoto fw_iso_context_queue_flush(s->context); 828d67c46b9STakashi Sakamoto } 829d67c46b9STakashi Sakamoto 83073fc7f08STakashi Sakamoto static void in_stream_callback(struct fw_iso_context *context, u32 tstamp, 831d67c46b9STakashi Sakamoto size_t header_length, void *header, 832d67c46b9STakashi Sakamoto void *private_data) 833d67c46b9STakashi Sakamoto { 834d67c46b9STakashi Sakamoto struct amdtp_stream *s = private_data; 835cc4f8e91STakashi Sakamoto __be32 *ctx_header = header; 836*e229853dSTakashi Sakamoto unsigned int events_per_period = s->events_per_period; 837*e229853dSTakashi Sakamoto unsigned int event_count = s->event_count; 838*e229853dSTakashi Sakamoto unsigned int packets; 839753e7179STakashi Sakamoto int i; 840753e7179STakashi Sakamoto int err; 841d67c46b9STakashi Sakamoto 842d67c46b9STakashi Sakamoto if (s->packet_index < 0) 843d67c46b9STakashi Sakamoto return; 844d67c46b9STakashi Sakamoto 845a0e02331STakashi Sakamoto // Calculate the number of packets in buffer and check XRUN. 846d3d10a4aSTakashi Sakamoto packets = header_length / s->ctx_data.tx.ctx_header_size; 847f90e2dedSTakashi Sakamoto 848753e7179STakashi Sakamoto err = generate_device_pkt_descs(s, s->pkt_descs, ctx_header, packets); 849753e7179STakashi Sakamoto if (err < 0) { 850753e7179STakashi Sakamoto if (err != -EAGAIN) { 851753e7179STakashi Sakamoto cancel_stream(s); 852753e7179STakashi Sakamoto return; 853753e7179STakashi Sakamoto } 8545e2ece0fSTakashi Sakamoto } else { 8550f5cfcb2STakashi Sakamoto process_ctx_payloads(s, s->pkt_descs, packets); 8565e2ece0fSTakashi Sakamoto } 8575e2ece0fSTakashi Sakamoto 8585e2ece0fSTakashi Sakamoto for (i = 0; i < packets; ++i) { 859*e229853dSTakashi Sakamoto const struct pkt_desc *desc = s->pkt_descs + i; 8605e2ece0fSTakashi Sakamoto struct fw_iso_packet params = {0}; 861*e229853dSTakashi Sakamoto bool sched_irq = false; 862a35463d1STakashi Sakamoto 863*e229853dSTakashi Sakamoto if (err >= 0) { 864*e229853dSTakashi Sakamoto event_count += desc->data_blocks; 865*e229853dSTakashi Sakamoto if (event_count >= events_per_period) { 866*e229853dSTakashi Sakamoto event_count -= events_per_period; 867*e229853dSTakashi Sakamoto sched_irq = true; 868*e229853dSTakashi Sakamoto } 869*e229853dSTakashi Sakamoto } else { 870*e229853dSTakashi Sakamoto sched_irq = 871*e229853dSTakashi Sakamoto !((s->packet_index + 1) % s->idle_irq_interval); 872*e229853dSTakashi Sakamoto } 873*e229853dSTakashi Sakamoto 874*e229853dSTakashi Sakamoto if (queue_in_packet(s, ¶ms, sched_irq) < 0) { 875753e7179STakashi Sakamoto cancel_stream(s); 876753e7179STakashi Sakamoto return; 877753e7179STakashi Sakamoto } 878d67c46b9STakashi Sakamoto } 879d67c46b9STakashi Sakamoto 880*e229853dSTakashi Sakamoto s->event_count = event_count; 881*e229853dSTakashi Sakamoto 882d67c46b9STakashi Sakamoto fw_iso_context_queue_flush(s->context); 883d67c46b9STakashi Sakamoto } 884d67c46b9STakashi Sakamoto 885d67c46b9STakashi Sakamoto /* this is executed one time */ 886d67c46b9STakashi Sakamoto static void amdtp_stream_first_callback(struct fw_iso_context *context, 88773fc7f08STakashi Sakamoto u32 tstamp, size_t header_length, 888d67c46b9STakashi Sakamoto void *header, void *private_data) 889d67c46b9STakashi Sakamoto { 890d67c46b9STakashi Sakamoto struct amdtp_stream *s = private_data; 89126cd1e58STakashi Sakamoto const __be32 *ctx_header = header; 892a04513f8STakashi Sakamoto u32 cycle; 893d67c46b9STakashi Sakamoto 894d67c46b9STakashi Sakamoto /* 895d67c46b9STakashi Sakamoto * For in-stream, first packet has come. 896d67c46b9STakashi Sakamoto * For out-stream, prepared to transmit first packet 897d67c46b9STakashi Sakamoto */ 898d67c46b9STakashi Sakamoto s->callbacked = true; 899d67c46b9STakashi Sakamoto wake_up(&s->callback_wait); 900d67c46b9STakashi Sakamoto 901cc4f8e91STakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 90226cd1e58STakashi Sakamoto cycle = compute_cycle_count(ctx_header[1]); 903a04513f8STakashi Sakamoto 904d67c46b9STakashi Sakamoto context->callback.sc = in_stream_callback; 905a04513f8STakashi Sakamoto } else { 906a0e02331STakashi Sakamoto cycle = compute_it_cycle(*ctx_header, s->queue_size); 90726cd1e58STakashi Sakamoto 908d67c46b9STakashi Sakamoto context->callback.sc = out_stream_callback; 909a04513f8STakashi Sakamoto } 910a04513f8STakashi Sakamoto 911a04513f8STakashi Sakamoto s->start_cycle = cycle; 912d67c46b9STakashi Sakamoto 91373fc7f08STakashi Sakamoto context->callback.sc(context, tstamp, header_length, header, s); 914d67c46b9STakashi Sakamoto } 915d67c46b9STakashi Sakamoto 916d67c46b9STakashi Sakamoto /** 917d67c46b9STakashi Sakamoto * amdtp_stream_start - start transferring packets 918d67c46b9STakashi Sakamoto * @s: the AMDTP stream to start 919d67c46b9STakashi Sakamoto * @channel: the isochronous channel on the bus 920d67c46b9STakashi Sakamoto * @speed: firewire speed code 921d67c46b9STakashi Sakamoto * 922d67c46b9STakashi Sakamoto * The stream cannot be started until it has been configured with 923d67c46b9STakashi Sakamoto * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI 924d67c46b9STakashi Sakamoto * device can be started. 925d67c46b9STakashi Sakamoto */ 926a0e02331STakashi Sakamoto static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed, 927a0e02331STakashi Sakamoto struct amdtp_domain *d) 928d67c46b9STakashi Sakamoto { 929d67c46b9STakashi Sakamoto static const struct { 930d67c46b9STakashi Sakamoto unsigned int data_block; 931d67c46b9STakashi Sakamoto unsigned int syt_offset; 932d3d10a4aSTakashi Sakamoto } *entry, initial_state[] = { 933d67c46b9STakashi Sakamoto [CIP_SFC_32000] = { 4, 3072 }, 934d67c46b9STakashi Sakamoto [CIP_SFC_48000] = { 6, 1024 }, 935d67c46b9STakashi Sakamoto [CIP_SFC_96000] = { 12, 1024 }, 936d67c46b9STakashi Sakamoto [CIP_SFC_192000] = { 24, 1024 }, 937d67c46b9STakashi Sakamoto [CIP_SFC_44100] = { 0, 67 }, 938d67c46b9STakashi Sakamoto [CIP_SFC_88200] = { 0, 67 }, 939d67c46b9STakashi Sakamoto [CIP_SFC_176400] = { 0, 67 }, 940d67c46b9STakashi Sakamoto }; 941a0e02331STakashi Sakamoto unsigned int events_per_buffer = d->events_per_buffer; 942*e229853dSTakashi Sakamoto unsigned int events_per_period = d->events_per_period; 943d3d10a4aSTakashi Sakamoto unsigned int ctx_header_size; 944f11453c7STakashi Sakamoto unsigned int max_ctx_payload_size; 945d67c46b9STakashi Sakamoto enum dma_data_direction dir; 946d67c46b9STakashi Sakamoto int type, tag, err; 947d67c46b9STakashi Sakamoto 948d67c46b9STakashi Sakamoto mutex_lock(&s->mutex); 949d67c46b9STakashi Sakamoto 950d67c46b9STakashi Sakamoto if (WARN_ON(amdtp_stream_running(s) || 951d67c46b9STakashi Sakamoto (s->data_block_quadlets < 1))) { 952d67c46b9STakashi Sakamoto err = -EBADFD; 953d67c46b9STakashi Sakamoto goto err_unlock; 954d67c46b9STakashi Sakamoto } 955d67c46b9STakashi Sakamoto 956d3d10a4aSTakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 957d67c46b9STakashi Sakamoto s->data_block_counter = UINT_MAX; 958d3d10a4aSTakashi Sakamoto } else { 959d3d10a4aSTakashi Sakamoto entry = &initial_state[s->sfc]; 960d3d10a4aSTakashi Sakamoto 961d67c46b9STakashi Sakamoto s->data_block_counter = 0; 962d3d10a4aSTakashi Sakamoto s->ctx_data.rx.data_block_state = entry->data_block; 963d3d10a4aSTakashi Sakamoto s->ctx_data.rx.syt_offset_state = entry->syt_offset; 964d3d10a4aSTakashi Sakamoto s->ctx_data.rx.last_syt_offset = TICKS_PER_CYCLE; 965d3d10a4aSTakashi Sakamoto } 966d67c46b9STakashi Sakamoto 967d67c46b9STakashi Sakamoto /* initialize packet buffer */ 968d67c46b9STakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 969d67c46b9STakashi Sakamoto dir = DMA_FROM_DEVICE; 970d67c46b9STakashi Sakamoto type = FW_ISO_CONTEXT_RECEIVE; 971f11453c7STakashi Sakamoto if (!(s->flags & CIP_NO_HEADER)) 972f11453c7STakashi Sakamoto ctx_header_size = IR_CTX_HEADER_SIZE_CIP; 973f11453c7STakashi Sakamoto else 974f11453c7STakashi Sakamoto ctx_header_size = IR_CTX_HEADER_SIZE_NO_CIP; 975b18f0cfaSTakashi Sakamoto 976b18f0cfaSTakashi Sakamoto max_ctx_payload_size = amdtp_stream_get_max_payload(s) - 977b18f0cfaSTakashi Sakamoto ctx_header_size; 978d67c46b9STakashi Sakamoto } else { 979d67c46b9STakashi Sakamoto dir = DMA_TO_DEVICE; 980d67c46b9STakashi Sakamoto type = FW_ISO_CONTEXT_TRANSMIT; 981df9160b9STakashi Sakamoto ctx_header_size = 0; // No effect for IT context. 982f11453c7STakashi Sakamoto 983b18f0cfaSTakashi Sakamoto max_ctx_payload_size = amdtp_stream_get_max_payload(s); 984b18f0cfaSTakashi Sakamoto if (!(s->flags & CIP_NO_HEADER)) 985b18f0cfaSTakashi Sakamoto max_ctx_payload_size -= IT_PKT_HEADER_SIZE_CIP; 986b18f0cfaSTakashi Sakamoto } 987f11453c7STakashi Sakamoto 988*e229853dSTakashi Sakamoto // This is a case that AMDTP streams in domain run just for MIDI 989*e229853dSTakashi Sakamoto // substream. Use the number of events equivalent to 10 msec as 990*e229853dSTakashi Sakamoto // interval of hardware IRQ. 991*e229853dSTakashi Sakamoto if (events_per_period == 0) 992*e229853dSTakashi Sakamoto events_per_period = amdtp_rate_table[s->sfc] / 100; 993a0e02331STakashi Sakamoto if (events_per_buffer == 0) 994*e229853dSTakashi Sakamoto events_per_buffer = events_per_period * 3; 995a0e02331STakashi Sakamoto 996*e229853dSTakashi Sakamoto s->idle_irq_interval = 997*e229853dSTakashi Sakamoto DIV_ROUND_UP(CYCLES_PER_SECOND * events_per_period, 998*e229853dSTakashi Sakamoto amdtp_rate_table[s->sfc]); 999a0e02331STakashi Sakamoto s->queue_size = DIV_ROUND_UP(CYCLES_PER_SECOND * events_per_buffer, 1000a0e02331STakashi Sakamoto amdtp_rate_table[s->sfc]); 1001*e229853dSTakashi Sakamoto s->events_per_period = events_per_period; 1002*e229853dSTakashi Sakamoto s->event_count = 0; 1003a0e02331STakashi Sakamoto 1004a0e02331STakashi Sakamoto err = iso_packets_buffer_init(&s->buffer, s->unit, s->queue_size, 1005f11453c7STakashi Sakamoto max_ctx_payload_size, dir); 1006d67c46b9STakashi Sakamoto if (err < 0) 1007d67c46b9STakashi Sakamoto goto err_unlock; 1008d67c46b9STakashi Sakamoto 1009d67c46b9STakashi Sakamoto s->context = fw_iso_context_create(fw_parent_device(s->unit)->card, 1010d3d10a4aSTakashi Sakamoto type, channel, speed, ctx_header_size, 1011d67c46b9STakashi Sakamoto amdtp_stream_first_callback, s); 1012d67c46b9STakashi Sakamoto if (IS_ERR(s->context)) { 1013d67c46b9STakashi Sakamoto err = PTR_ERR(s->context); 1014d67c46b9STakashi Sakamoto if (err == -EBUSY) 1015d67c46b9STakashi Sakamoto dev_err(&s->unit->device, 1016d67c46b9STakashi Sakamoto "no free stream on this controller\n"); 1017d67c46b9STakashi Sakamoto goto err_buffer; 1018d67c46b9STakashi Sakamoto } 1019d67c46b9STakashi Sakamoto 1020d67c46b9STakashi Sakamoto amdtp_stream_update(s); 1021d67c46b9STakashi Sakamoto 1022d3d10a4aSTakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 1023f11453c7STakashi Sakamoto s->ctx_data.tx.max_ctx_payload_length = max_ctx_payload_size; 1024d3d10a4aSTakashi Sakamoto s->ctx_data.tx.ctx_header_size = ctx_header_size; 1025d3d10a4aSTakashi Sakamoto } 102652759c09STakashi Sakamoto 10273b196c39STakashi Sakamoto if (s->flags & CIP_NO_HEADER) 10283b196c39STakashi Sakamoto s->tag = TAG_NO_CIP_HEADER; 10293b196c39STakashi Sakamoto else 10303b196c39STakashi Sakamoto s->tag = TAG_CIP; 10313b196c39STakashi Sakamoto 1032a0e02331STakashi Sakamoto s->pkt_descs = kcalloc(s->queue_size, sizeof(*s->pkt_descs), 103304130cf8STakashi Sakamoto GFP_KERNEL); 103404130cf8STakashi Sakamoto if (!s->pkt_descs) { 103504130cf8STakashi Sakamoto err = -ENOMEM; 103604130cf8STakashi Sakamoto goto err_context; 103704130cf8STakashi Sakamoto } 103804130cf8STakashi Sakamoto 1039d67c46b9STakashi Sakamoto s->packet_index = 0; 1040d67c46b9STakashi Sakamoto do { 10416007bf54STakashi Sakamoto struct fw_iso_packet params; 1042*e229853dSTakashi Sakamoto bool sched_irq; 1043*e229853dSTakashi Sakamoto 1044*e229853dSTakashi Sakamoto sched_irq = !((s->packet_index + 1) % s->idle_irq_interval); 1045b18f0cfaSTakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 1046*e229853dSTakashi Sakamoto err = queue_in_packet(s, ¶ms, sched_irq); 1047b18f0cfaSTakashi Sakamoto } else { 1048b18f0cfaSTakashi Sakamoto params.header_length = 0; 1049b18f0cfaSTakashi Sakamoto params.payload_length = 0; 1050*e229853dSTakashi Sakamoto err = queue_out_packet(s, ¶ms, sched_irq); 1051b18f0cfaSTakashi Sakamoto } 1052d67c46b9STakashi Sakamoto if (err < 0) 105304130cf8STakashi Sakamoto goto err_pkt_descs; 1054d67c46b9STakashi Sakamoto } while (s->packet_index > 0); 1055d67c46b9STakashi Sakamoto 1056d67c46b9STakashi Sakamoto /* NOTE: TAG1 matches CIP. This just affects in stream. */ 1057d67c46b9STakashi Sakamoto tag = FW_ISO_CONTEXT_MATCH_TAG1; 10583b196c39STakashi Sakamoto if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER)) 1059d67c46b9STakashi Sakamoto tag |= FW_ISO_CONTEXT_MATCH_TAG0; 1060d67c46b9STakashi Sakamoto 1061d67c46b9STakashi Sakamoto s->callbacked = false; 1062d67c46b9STakashi Sakamoto err = fw_iso_context_start(s->context, -1, 0, tag); 1063d67c46b9STakashi Sakamoto if (err < 0) 106404130cf8STakashi Sakamoto goto err_pkt_descs; 1065d67c46b9STakashi Sakamoto 1066d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 1067d67c46b9STakashi Sakamoto 1068d67c46b9STakashi Sakamoto return 0; 106904130cf8STakashi Sakamoto err_pkt_descs: 107004130cf8STakashi Sakamoto kfree(s->pkt_descs); 1071d67c46b9STakashi Sakamoto err_context: 1072d67c46b9STakashi Sakamoto fw_iso_context_destroy(s->context); 1073d67c46b9STakashi Sakamoto s->context = ERR_PTR(-1); 1074d67c46b9STakashi Sakamoto err_buffer: 1075d67c46b9STakashi Sakamoto iso_packets_buffer_destroy(&s->buffer, s->unit); 1076d67c46b9STakashi Sakamoto err_unlock: 1077d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 1078d67c46b9STakashi Sakamoto 1079d67c46b9STakashi Sakamoto return err; 1080d67c46b9STakashi Sakamoto } 1081d67c46b9STakashi Sakamoto 1082d67c46b9STakashi Sakamoto /** 1083d67c46b9STakashi Sakamoto * amdtp_stream_pcm_pointer - get the PCM buffer position 1084d67c46b9STakashi Sakamoto * @s: the AMDTP stream that transports the PCM data 1085d67c46b9STakashi Sakamoto * 1086d67c46b9STakashi Sakamoto * Returns the current buffer position, in frames. 1087d67c46b9STakashi Sakamoto */ 1088d67c46b9STakashi Sakamoto unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s) 1089d67c46b9STakashi Sakamoto { 10901dba9db0STakashi Sakamoto /* 10911dba9db0STakashi Sakamoto * This function is called in software IRQ context of period_tasklet or 10921dba9db0STakashi Sakamoto * process context. 10931dba9db0STakashi Sakamoto * 10941dba9db0STakashi Sakamoto * When the software IRQ context was scheduled by software IRQ context 10951dba9db0STakashi Sakamoto * of IR/IT contexts, queued packets were already handled. Therefore, 10961dba9db0STakashi Sakamoto * no need to flush the queue in buffer anymore. 10971dba9db0STakashi Sakamoto * 10981dba9db0STakashi Sakamoto * When the process context reach here, some packets will be already 10991dba9db0STakashi Sakamoto * queued in the buffer. These packets should be handled immediately 11001dba9db0STakashi Sakamoto * to keep better granularity of PCM pointer. 11011dba9db0STakashi Sakamoto * 11021dba9db0STakashi Sakamoto * Later, the process context will sometimes schedules software IRQ 11031dba9db0STakashi Sakamoto * context of the period_tasklet. Then, no need to flush the queue by 11041dba9db0STakashi Sakamoto * the same reason as described for IR/IT contexts. 11051dba9db0STakashi Sakamoto */ 11061dba9db0STakashi Sakamoto if (!in_interrupt() && amdtp_stream_running(s)) 1107d67c46b9STakashi Sakamoto fw_iso_context_flush_completions(s->context); 1108d67c46b9STakashi Sakamoto 11096aa7de05SMark Rutland return READ_ONCE(s->pcm_buffer_pointer); 1110d67c46b9STakashi Sakamoto } 1111d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_pointer); 1112d67c46b9STakashi Sakamoto 1113d67c46b9STakashi Sakamoto /** 1114875becf8STakashi Sakamoto * amdtp_stream_pcm_ack - acknowledge queued PCM frames 1115875becf8STakashi Sakamoto * @s: the AMDTP stream that transfers the PCM frames 1116875becf8STakashi Sakamoto * 1117875becf8STakashi Sakamoto * Returns zero always. 1118875becf8STakashi Sakamoto */ 1119875becf8STakashi Sakamoto int amdtp_stream_pcm_ack(struct amdtp_stream *s) 1120875becf8STakashi Sakamoto { 1121875becf8STakashi Sakamoto /* 1122875becf8STakashi Sakamoto * Process isochronous packets for recent isochronous cycle to handle 1123875becf8STakashi Sakamoto * queued PCM frames. 1124875becf8STakashi Sakamoto */ 1125875becf8STakashi Sakamoto if (amdtp_stream_running(s)) 1126875becf8STakashi Sakamoto fw_iso_context_flush_completions(s->context); 1127875becf8STakashi Sakamoto 1128875becf8STakashi Sakamoto return 0; 1129875becf8STakashi Sakamoto } 1130875becf8STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_ack); 1131875becf8STakashi Sakamoto 1132875becf8STakashi Sakamoto /** 1133d67c46b9STakashi Sakamoto * amdtp_stream_update - update the stream after a bus reset 1134d67c46b9STakashi Sakamoto * @s: the AMDTP stream 1135d67c46b9STakashi Sakamoto */ 1136d67c46b9STakashi Sakamoto void amdtp_stream_update(struct amdtp_stream *s) 1137d67c46b9STakashi Sakamoto { 1138d67c46b9STakashi Sakamoto /* Precomputing. */ 11396aa7de05SMark Rutland WRITE_ONCE(s->source_node_id_field, 11406aa7de05SMark Rutland (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & CIP_SID_MASK); 1141d67c46b9STakashi Sakamoto } 1142d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_update); 1143d67c46b9STakashi Sakamoto 1144d67c46b9STakashi Sakamoto /** 1145d67c46b9STakashi Sakamoto * amdtp_stream_stop - stop sending packets 1146d67c46b9STakashi Sakamoto * @s: the AMDTP stream to stop 1147d67c46b9STakashi Sakamoto * 1148d67c46b9STakashi Sakamoto * All PCM and MIDI devices of the stream must be stopped before the stream 1149d67c46b9STakashi Sakamoto * itself can be stopped. 1150d67c46b9STakashi Sakamoto */ 115174f94e41STakashi Sakamoto static void amdtp_stream_stop(struct amdtp_stream *s) 1152d67c46b9STakashi Sakamoto { 1153d67c46b9STakashi Sakamoto mutex_lock(&s->mutex); 1154d67c46b9STakashi Sakamoto 1155d67c46b9STakashi Sakamoto if (!amdtp_stream_running(s)) { 1156d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 1157d67c46b9STakashi Sakamoto return; 1158d67c46b9STakashi Sakamoto } 1159d67c46b9STakashi Sakamoto 1160d67c46b9STakashi Sakamoto tasklet_kill(&s->period_tasklet); 1161d67c46b9STakashi Sakamoto fw_iso_context_stop(s->context); 1162d67c46b9STakashi Sakamoto fw_iso_context_destroy(s->context); 1163d67c46b9STakashi Sakamoto s->context = ERR_PTR(-1); 1164d67c46b9STakashi Sakamoto iso_packets_buffer_destroy(&s->buffer, s->unit); 116504130cf8STakashi Sakamoto kfree(s->pkt_descs); 1166d67c46b9STakashi Sakamoto 1167d67c46b9STakashi Sakamoto s->callbacked = false; 1168d67c46b9STakashi Sakamoto 1169d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 1170d67c46b9STakashi Sakamoto } 1171d67c46b9STakashi Sakamoto 1172d67c46b9STakashi Sakamoto /** 1173d67c46b9STakashi Sakamoto * amdtp_stream_pcm_abort - abort the running PCM device 1174d67c46b9STakashi Sakamoto * @s: the AMDTP stream about to be stopped 1175d67c46b9STakashi Sakamoto * 1176d67c46b9STakashi Sakamoto * If the isochronous stream needs to be stopped asynchronously, call this 1177d67c46b9STakashi Sakamoto * function first to stop the PCM device. 1178d67c46b9STakashi Sakamoto */ 1179d67c46b9STakashi Sakamoto void amdtp_stream_pcm_abort(struct amdtp_stream *s) 1180d67c46b9STakashi Sakamoto { 1181d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm; 1182d67c46b9STakashi Sakamoto 11836aa7de05SMark Rutland pcm = READ_ONCE(s->pcm); 1184d67c46b9STakashi Sakamoto if (pcm) 1185d67c46b9STakashi Sakamoto snd_pcm_stop_xrun(pcm); 1186d67c46b9STakashi Sakamoto } 1187d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_abort); 11883ec3d7a3STakashi Sakamoto 11893ec3d7a3STakashi Sakamoto /** 11903ec3d7a3STakashi Sakamoto * amdtp_domain_init - initialize an AMDTP domain structure 11913ec3d7a3STakashi Sakamoto * @d: the AMDTP domain to initialize. 11923ec3d7a3STakashi Sakamoto */ 11933ec3d7a3STakashi Sakamoto int amdtp_domain_init(struct amdtp_domain *d) 11943ec3d7a3STakashi Sakamoto { 11953ec3d7a3STakashi Sakamoto INIT_LIST_HEAD(&d->streams); 11963ec3d7a3STakashi Sakamoto 1197d68c3123STakashi Sakamoto d->events_per_period = 0; 1198d68c3123STakashi Sakamoto 11993ec3d7a3STakashi Sakamoto return 0; 12003ec3d7a3STakashi Sakamoto } 12013ec3d7a3STakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_init); 12023ec3d7a3STakashi Sakamoto 12033ec3d7a3STakashi Sakamoto /** 12043ec3d7a3STakashi Sakamoto * amdtp_domain_destroy - destroy an AMDTP domain structure 12053ec3d7a3STakashi Sakamoto * @d: the AMDTP domain to destroy. 12063ec3d7a3STakashi Sakamoto */ 12073ec3d7a3STakashi Sakamoto void amdtp_domain_destroy(struct amdtp_domain *d) 12083ec3d7a3STakashi Sakamoto { 12098d0d5c3fSTakashi Sakamoto // At present nothing to do. 12108d0d5c3fSTakashi Sakamoto return; 12113ec3d7a3STakashi Sakamoto } 12123ec3d7a3STakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_destroy); 12136261f90bSTakashi Sakamoto 12146261f90bSTakashi Sakamoto /** 1215157a53eeSTakashi Sakamoto * amdtp_domain_add_stream - register isoc context into the domain. 1216157a53eeSTakashi Sakamoto * @d: the AMDTP domain. 1217157a53eeSTakashi Sakamoto * @s: the AMDTP stream. 1218157a53eeSTakashi Sakamoto * @channel: the isochronous channel on the bus. 1219157a53eeSTakashi Sakamoto * @speed: firewire speed code. 1220157a53eeSTakashi Sakamoto */ 1221157a53eeSTakashi Sakamoto int amdtp_domain_add_stream(struct amdtp_domain *d, struct amdtp_stream *s, 1222157a53eeSTakashi Sakamoto int channel, int speed) 1223157a53eeSTakashi Sakamoto { 1224157a53eeSTakashi Sakamoto struct amdtp_stream *tmp; 1225157a53eeSTakashi Sakamoto 1226157a53eeSTakashi Sakamoto list_for_each_entry(tmp, &d->streams, list) { 1227157a53eeSTakashi Sakamoto if (s == tmp) 1228157a53eeSTakashi Sakamoto return -EBUSY; 1229157a53eeSTakashi Sakamoto } 1230157a53eeSTakashi Sakamoto 1231157a53eeSTakashi Sakamoto list_add(&s->list, &d->streams); 1232157a53eeSTakashi Sakamoto 1233157a53eeSTakashi Sakamoto s->channel = channel; 1234157a53eeSTakashi Sakamoto s->speed = speed; 1235157a53eeSTakashi Sakamoto 1236157a53eeSTakashi Sakamoto return 0; 1237157a53eeSTakashi Sakamoto } 1238157a53eeSTakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_add_stream); 1239157a53eeSTakashi Sakamoto 1240157a53eeSTakashi Sakamoto /** 12419b4702b0STakashi Sakamoto * amdtp_domain_start - start sending packets for isoc context in the domain. 12429b4702b0STakashi Sakamoto * @d: the AMDTP domain. 12439b4702b0STakashi Sakamoto */ 12449b4702b0STakashi Sakamoto int amdtp_domain_start(struct amdtp_domain *d) 12459b4702b0STakashi Sakamoto { 12469b4702b0STakashi Sakamoto struct amdtp_stream *s; 12479b4702b0STakashi Sakamoto int err = 0; 12489b4702b0STakashi Sakamoto 12499b4702b0STakashi Sakamoto list_for_each_entry(s, &d->streams, list) { 1250a0e02331STakashi Sakamoto err = amdtp_stream_start(s, s->channel, s->speed, d); 12519b4702b0STakashi Sakamoto if (err < 0) 12529b4702b0STakashi Sakamoto break; 12539b4702b0STakashi Sakamoto } 12549b4702b0STakashi Sakamoto 12559b4702b0STakashi Sakamoto if (err < 0) { 12569b4702b0STakashi Sakamoto list_for_each_entry(s, &d->streams, list) 12579b4702b0STakashi Sakamoto amdtp_stream_stop(s); 12589b4702b0STakashi Sakamoto } 12599b4702b0STakashi Sakamoto 12609b4702b0STakashi Sakamoto return err; 12619b4702b0STakashi Sakamoto } 12629b4702b0STakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_start); 12639b4702b0STakashi Sakamoto 12649b4702b0STakashi Sakamoto /** 12656261f90bSTakashi Sakamoto * amdtp_domain_stop - stop sending packets for isoc context in the same domain. 12666261f90bSTakashi Sakamoto * @d: the AMDTP domain to which the isoc contexts belong. 12676261f90bSTakashi Sakamoto */ 12686261f90bSTakashi Sakamoto void amdtp_domain_stop(struct amdtp_domain *d) 12696261f90bSTakashi Sakamoto { 12706261f90bSTakashi Sakamoto struct amdtp_stream *s, *next; 12716261f90bSTakashi Sakamoto 12726261f90bSTakashi Sakamoto list_for_each_entry_safe(s, next, &d->streams, list) { 12736261f90bSTakashi Sakamoto list_del(&s->list); 12746261f90bSTakashi Sakamoto 12756261f90bSTakashi Sakamoto amdtp_stream_stop(s); 12766261f90bSTakashi Sakamoto } 1277d68c3123STakashi Sakamoto 1278d68c3123STakashi Sakamoto d->events_per_period = 0; 12796261f90bSTakashi Sakamoto } 12806261f90bSTakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_stop); 1281