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> 12acfedcbeSTakashi Sakamoto #include <linux/firewire-constants.h> 13d67c46b9STakashi Sakamoto #include <linux/module.h> 14d67c46b9STakashi Sakamoto #include <linux/slab.h> 15d67c46b9STakashi Sakamoto #include <sound/pcm.h> 16d67c46b9STakashi Sakamoto #include <sound/pcm_params.h> 17d67c46b9STakashi Sakamoto #include "amdtp-stream.h" 18d67c46b9STakashi Sakamoto 19d67c46b9STakashi Sakamoto #define TICKS_PER_CYCLE 3072 20d67c46b9STakashi Sakamoto #define CYCLES_PER_SECOND 8000 21d67c46b9STakashi Sakamoto #define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND) 22d67c46b9STakashi Sakamoto 233e106f4fSTakashi Sakamoto #define OHCI_SECOND_MODULUS 8 2410aa8e4aSTakashi Sakamoto 250c95c1d6STakashi Sakamoto /* Always support Linux tracing subsystem. */ 260c95c1d6STakashi Sakamoto #define CREATE_TRACE_POINTS 270c95c1d6STakashi Sakamoto #include "amdtp-stream-trace.h" 280c95c1d6STakashi Sakamoto 29d67c46b9STakashi Sakamoto #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */ 30d67c46b9STakashi Sakamoto 31d67c46b9STakashi Sakamoto /* isochronous header parameters */ 32d67c46b9STakashi Sakamoto #define ISO_DATA_LENGTH_SHIFT 16 333b196c39STakashi Sakamoto #define TAG_NO_CIP_HEADER 0 34d67c46b9STakashi Sakamoto #define TAG_CIP 1 35d67c46b9STakashi Sakamoto 3667d92ee7STakashi Sakamoto // Common Isochronous Packet (CIP) header parameters. Use two quadlets CIP header when supported. 3767d92ee7STakashi Sakamoto #define CIP_HEADER_QUADLETS 2 38d67c46b9STakashi Sakamoto #define CIP_EOH_SHIFT 31 39d67c46b9STakashi Sakamoto #define CIP_EOH (1u << CIP_EOH_SHIFT) 40d67c46b9STakashi Sakamoto #define CIP_EOH_MASK 0x80000000 41d67c46b9STakashi Sakamoto #define CIP_SID_SHIFT 24 42d67c46b9STakashi Sakamoto #define CIP_SID_MASK 0x3f000000 43d67c46b9STakashi Sakamoto #define CIP_DBS_MASK 0x00ff0000 44d67c46b9STakashi Sakamoto #define CIP_DBS_SHIFT 16 459863874fSTakashi Sakamoto #define CIP_SPH_MASK 0x00000400 469863874fSTakashi Sakamoto #define CIP_SPH_SHIFT 10 47d67c46b9STakashi Sakamoto #define CIP_DBC_MASK 0x000000ff 48d67c46b9STakashi Sakamoto #define CIP_FMT_SHIFT 24 49d67c46b9STakashi Sakamoto #define CIP_FMT_MASK 0x3f000000 50d67c46b9STakashi Sakamoto #define CIP_FDF_MASK 0x00ff0000 51d67c46b9STakashi Sakamoto #define CIP_FDF_SHIFT 16 52fb25dcc8STakashi Sakamoto #define CIP_FDF_NO_DATA 0xff 53d67c46b9STakashi Sakamoto #define CIP_SYT_MASK 0x0000ffff 54d67c46b9STakashi Sakamoto #define CIP_SYT_NO_INFO 0xffff 55f9e5ecdfSTakashi Sakamoto #define CIP_SYT_CYCLE_MODULUS 16 56fb25dcc8STakashi Sakamoto #define CIP_NO_DATA ((CIP_FDF_NO_DATA << CIP_FDF_SHIFT) | CIP_SYT_NO_INFO) 57d67c46b9STakashi Sakamoto 5867d92ee7STakashi Sakamoto #define CIP_HEADER_SIZE (sizeof(__be32) * CIP_HEADER_QUADLETS) 5967d92ee7STakashi Sakamoto 6051c29fd2STakashi Sakamoto /* Audio and Music transfer protocol specific parameters */ 61d67c46b9STakashi Sakamoto #define CIP_FMT_AM 0x10 62d67c46b9STakashi Sakamoto #define AMDTP_FDF_NO_DATA 0xff 63d67c46b9STakashi Sakamoto 64f11453c7STakashi Sakamoto // For iso header and tstamp. 6567d92ee7STakashi Sakamoto #define IR_CTX_HEADER_DEFAULT_QUADLETS 2 6667d92ee7STakashi Sakamoto // Add nothing. 6767d92ee7STakashi Sakamoto #define IR_CTX_HEADER_SIZE_NO_CIP (sizeof(__be32) * IR_CTX_HEADER_DEFAULT_QUADLETS) 6867d92ee7STakashi Sakamoto // Add two quadlets CIP header. 6967d92ee7STakashi Sakamoto #define IR_CTX_HEADER_SIZE_CIP (IR_CTX_HEADER_SIZE_NO_CIP + CIP_HEADER_SIZE) 70cc4f8e91STakashi Sakamoto #define HEADER_TSTAMP_MASK 0x0000ffff 71d67c46b9STakashi Sakamoto 7267d92ee7STakashi Sakamoto #define IT_PKT_HEADER_SIZE_CIP CIP_HEADER_SIZE 73b18f0cfaSTakashi Sakamoto #define IT_PKT_HEADER_SIZE_NO_CIP 0 // Nothing. 74b18f0cfaSTakashi Sakamoto 756a3ce97dSTakashi Sakamoto // The initial firmware of OXFW970 can postpone transmission of packet during finishing 766a3ce97dSTakashi Sakamoto // asynchronous transaction. This module accepts 5 cycles to skip as maximum to avoid buffer 776a3ce97dSTakashi Sakamoto // overrun. Actual device can skip more, then this module stops the packet streaming. 786a3ce97dSTakashi Sakamoto #define IR_JUMBO_PAYLOAD_MAX_SKIP_CYCLES 5 796a3ce97dSTakashi Sakamoto 802b3d2987STakashi Iwai static void pcm_period_work(struct work_struct *work); 81d67c46b9STakashi Sakamoto 82d67c46b9STakashi Sakamoto /** 83d67c46b9STakashi Sakamoto * amdtp_stream_init - initialize an AMDTP stream structure 84d67c46b9STakashi Sakamoto * @s: the AMDTP stream to initialize 85d67c46b9STakashi Sakamoto * @unit: the target of the stream 86d67c46b9STakashi Sakamoto * @dir: the direction of stream 87ffe66bbeSTakashi Sakamoto * @flags: the details of the streaming protocol consist of cip_flags enumeration-constants. 885955815eSTakashi Sakamoto * @fmt: the value of fmt field in CIP header 899a738ad1STakashi Sakamoto * @process_ctx_payloads: callback handler to process payloads of isoc context 90df075feeSTakashi Sakamoto * @protocol_size: the size to allocate newly for protocol 91d67c46b9STakashi Sakamoto */ 92d67c46b9STakashi Sakamoto int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit, 93ffe66bbeSTakashi Sakamoto enum amdtp_stream_direction dir, unsigned int flags, 94df075feeSTakashi Sakamoto unsigned int fmt, 959a738ad1STakashi Sakamoto amdtp_stream_process_ctx_payloads_t process_ctx_payloads, 96df075feeSTakashi Sakamoto unsigned int protocol_size) 97d67c46b9STakashi Sakamoto { 989a738ad1STakashi Sakamoto if (process_ctx_payloads == NULL) 99df075feeSTakashi Sakamoto return -EINVAL; 100df075feeSTakashi Sakamoto 101df075feeSTakashi Sakamoto s->protocol = kzalloc(protocol_size, GFP_KERNEL); 102df075feeSTakashi Sakamoto if (!s->protocol) 103df075feeSTakashi Sakamoto return -ENOMEM; 104df075feeSTakashi Sakamoto 105d67c46b9STakashi Sakamoto s->unit = unit; 106d67c46b9STakashi Sakamoto s->direction = dir; 107d67c46b9STakashi Sakamoto s->flags = flags; 108d67c46b9STakashi Sakamoto s->context = ERR_PTR(-1); 109d67c46b9STakashi Sakamoto mutex_init(&s->mutex); 1102b3d2987STakashi Iwai INIT_WORK(&s->period_work, pcm_period_work); 111d67c46b9STakashi Sakamoto s->packet_index = 0; 112d67c46b9STakashi Sakamoto 113bdaedca7STakashi Sakamoto init_waitqueue_head(&s->ready_wait); 114d67c46b9STakashi Sakamoto 1155955815eSTakashi Sakamoto s->fmt = fmt; 1169a738ad1STakashi Sakamoto s->process_ctx_payloads = process_ctx_payloads; 117d67c46b9STakashi Sakamoto 118d67c46b9STakashi Sakamoto return 0; 119d67c46b9STakashi Sakamoto } 120d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_init); 121d67c46b9STakashi Sakamoto 122d67c46b9STakashi Sakamoto /** 123d67c46b9STakashi Sakamoto * amdtp_stream_destroy - free stream resources 124d67c46b9STakashi Sakamoto * @s: the AMDTP stream to destroy 125d67c46b9STakashi Sakamoto */ 126d67c46b9STakashi Sakamoto void amdtp_stream_destroy(struct amdtp_stream *s) 127d67c46b9STakashi Sakamoto { 12844c376b9STakashi Sakamoto /* Not initialized. */ 12944c376b9STakashi Sakamoto if (s->protocol == NULL) 13044c376b9STakashi Sakamoto return; 13144c376b9STakashi Sakamoto 132d67c46b9STakashi Sakamoto WARN_ON(amdtp_stream_running(s)); 133df075feeSTakashi Sakamoto kfree(s->protocol); 134d67c46b9STakashi Sakamoto mutex_destroy(&s->mutex); 135d67c46b9STakashi Sakamoto } 136d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_destroy); 137d67c46b9STakashi Sakamoto 138d67c46b9STakashi Sakamoto const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = { 139d67c46b9STakashi Sakamoto [CIP_SFC_32000] = 8, 140d67c46b9STakashi Sakamoto [CIP_SFC_44100] = 8, 141d67c46b9STakashi Sakamoto [CIP_SFC_48000] = 8, 142d67c46b9STakashi Sakamoto [CIP_SFC_88200] = 16, 143d67c46b9STakashi Sakamoto [CIP_SFC_96000] = 16, 144d67c46b9STakashi Sakamoto [CIP_SFC_176400] = 32, 145d67c46b9STakashi Sakamoto [CIP_SFC_192000] = 32, 146d67c46b9STakashi Sakamoto }; 147d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_syt_intervals); 148d67c46b9STakashi Sakamoto 149d67c46b9STakashi Sakamoto const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = { 150d67c46b9STakashi Sakamoto [CIP_SFC_32000] = 32000, 151d67c46b9STakashi Sakamoto [CIP_SFC_44100] = 44100, 152d67c46b9STakashi Sakamoto [CIP_SFC_48000] = 48000, 153d67c46b9STakashi Sakamoto [CIP_SFC_88200] = 88200, 154d67c46b9STakashi Sakamoto [CIP_SFC_96000] = 96000, 155d67c46b9STakashi Sakamoto [CIP_SFC_176400] = 176400, 156d67c46b9STakashi Sakamoto [CIP_SFC_192000] = 192000, 157d67c46b9STakashi Sakamoto }; 158d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_rate_table); 159d67c46b9STakashi Sakamoto 16059502295STakashi Sakamoto static int apply_constraint_to_size(struct snd_pcm_hw_params *params, 16159502295STakashi Sakamoto struct snd_pcm_hw_rule *rule) 16259502295STakashi Sakamoto { 16359502295STakashi Sakamoto struct snd_interval *s = hw_param_interval(params, rule->var); 16459502295STakashi Sakamoto const struct snd_interval *r = 16559502295STakashi Sakamoto hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE); 166826b5de9STakashi Sakamoto struct snd_interval t = {0}; 167826b5de9STakashi Sakamoto unsigned int step = 0; 16859502295STakashi Sakamoto int i; 16959502295STakashi Sakamoto 17059502295STakashi Sakamoto for (i = 0; i < CIP_SFC_COUNT; ++i) { 171826b5de9STakashi Sakamoto if (snd_interval_test(r, amdtp_rate_table[i])) 172826b5de9STakashi Sakamoto step = max(step, amdtp_syt_intervals[i]); 17359502295STakashi Sakamoto } 17459502295STakashi Sakamoto 175826b5de9STakashi Sakamoto t.min = roundup(s->min, step); 176826b5de9STakashi Sakamoto t.max = rounddown(s->max, step); 177826b5de9STakashi Sakamoto t.integer = 1; 17859502295STakashi Sakamoto 17959502295STakashi Sakamoto return snd_interval_refine(s, &t); 18059502295STakashi Sakamoto } 18159502295STakashi Sakamoto 182d67c46b9STakashi Sakamoto /** 183d67c46b9STakashi Sakamoto * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream 184d67c46b9STakashi Sakamoto * @s: the AMDTP stream, which must be initialized. 185d67c46b9STakashi Sakamoto * @runtime: the PCM substream runtime 186d67c46b9STakashi Sakamoto */ 187d67c46b9STakashi Sakamoto int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s, 188d67c46b9STakashi Sakamoto struct snd_pcm_runtime *runtime) 189d67c46b9STakashi Sakamoto { 19055799c5aSTakashi Sakamoto struct snd_pcm_hardware *hw = &runtime->hw; 19199921ec6STakashi Sakamoto unsigned int ctx_header_size; 19299921ec6STakashi Sakamoto unsigned int maximum_usec_per_period; 193d67c46b9STakashi Sakamoto int err; 194d67c46b9STakashi Sakamoto 195d360870aSTakashi Sakamoto hw->info = SNDRV_PCM_INFO_BLOCK_TRANSFER | 19655799c5aSTakashi Sakamoto SNDRV_PCM_INFO_INTERLEAVED | 19755799c5aSTakashi Sakamoto SNDRV_PCM_INFO_JOINT_DUPLEX | 19855799c5aSTakashi Sakamoto SNDRV_PCM_INFO_MMAP | 199d360870aSTakashi Sakamoto SNDRV_PCM_INFO_MMAP_VALID | 200d360870aSTakashi Sakamoto SNDRV_PCM_INFO_NO_PERIOD_WAKEUP; 20155799c5aSTakashi Sakamoto 20255799c5aSTakashi Sakamoto hw->periods_min = 2; 20355799c5aSTakashi Sakamoto hw->periods_max = UINT_MAX; 20455799c5aSTakashi Sakamoto 20555799c5aSTakashi Sakamoto /* bytes for a frame */ 20655799c5aSTakashi Sakamoto hw->period_bytes_min = 4 * hw->channels_max; 20755799c5aSTakashi Sakamoto 20855799c5aSTakashi Sakamoto /* Just to prevent from allocating much pages. */ 20955799c5aSTakashi Sakamoto hw->period_bytes_max = hw->period_bytes_min * 2048; 21055799c5aSTakashi Sakamoto hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min; 21155799c5aSTakashi Sakamoto 21299921ec6STakashi Sakamoto // Linux driver for 1394 OHCI controller voluntarily flushes isoc 21399921ec6STakashi Sakamoto // context when total size of accumulated context header reaches 2142b3d2987STakashi Iwai // PAGE_SIZE. This kicks work for the isoc context and brings 21599921ec6STakashi Sakamoto // callback in the middle of scheduled interrupts. 21699921ec6STakashi Sakamoto // Although AMDTP streams in the same domain use the same events per 21799921ec6STakashi Sakamoto // IRQ, use the largest size of context header between IT/IR contexts. 21899921ec6STakashi Sakamoto // Here, use the value of context header in IR context is for both 21999921ec6STakashi Sakamoto // contexts. 22099921ec6STakashi Sakamoto if (!(s->flags & CIP_NO_HEADER)) 22199921ec6STakashi Sakamoto ctx_header_size = IR_CTX_HEADER_SIZE_CIP; 22299921ec6STakashi Sakamoto else 22399921ec6STakashi Sakamoto ctx_header_size = IR_CTX_HEADER_SIZE_NO_CIP; 22499921ec6STakashi Sakamoto maximum_usec_per_period = USEC_PER_SEC * PAGE_SIZE / 22599921ec6STakashi Sakamoto CYCLES_PER_SECOND / ctx_header_size; 22699921ec6STakashi Sakamoto 227f706df4fSTakashi Sakamoto // In IEC 61883-6, one isoc packet can transfer events up to the value 228f706df4fSTakashi Sakamoto // of syt interval. This comes from the interval of isoc cycle. As 1394 229f706df4fSTakashi Sakamoto // OHCI controller can generate hardware IRQ per isoc packet, the 230f706df4fSTakashi Sakamoto // interval is 125 usec. 231f706df4fSTakashi Sakamoto // However, there are two ways of transmission in IEC 61883-6; blocking 232f706df4fSTakashi Sakamoto // and non-blocking modes. In blocking mode, the sequence of isoc packet 233f706df4fSTakashi Sakamoto // includes 'empty' or 'NODATA' packets which include no event. In 234f706df4fSTakashi Sakamoto // non-blocking mode, the number of events per packet is variable up to 235f706df4fSTakashi Sakamoto // the syt interval. 236f706df4fSTakashi Sakamoto // Due to the above protocol design, the minimum PCM frames per 237f706df4fSTakashi Sakamoto // interrupt should be double of the value of syt interval, thus it is 238f706df4fSTakashi Sakamoto // 250 usec. 239d67c46b9STakashi Sakamoto err = snd_pcm_hw_constraint_minmax(runtime, 240d67c46b9STakashi Sakamoto SNDRV_PCM_HW_PARAM_PERIOD_TIME, 241f706df4fSTakashi Sakamoto 250, maximum_usec_per_period); 242d67c46b9STakashi Sakamoto if (err < 0) 243d67c46b9STakashi Sakamoto goto end; 244d67c46b9STakashi Sakamoto 245d67c46b9STakashi Sakamoto /* Non-Blocking stream has no more constraints */ 246d67c46b9STakashi Sakamoto if (!(s->flags & CIP_BLOCKING)) 247d67c46b9STakashi Sakamoto goto end; 248d67c46b9STakashi Sakamoto 249d67c46b9STakashi Sakamoto /* 250d67c46b9STakashi Sakamoto * One AMDTP packet can include some frames. In blocking mode, the 251d67c46b9STakashi Sakamoto * number equals to SYT_INTERVAL. So the number is 8, 16 or 32, 252d67c46b9STakashi Sakamoto * depending on its sampling rate. For accurate period interrupt, it's 253d67c46b9STakashi Sakamoto * preferrable to align period/buffer sizes to current SYT_INTERVAL. 254d67c46b9STakashi Sakamoto */ 25559502295STakashi Sakamoto err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 25659502295STakashi Sakamoto apply_constraint_to_size, NULL, 257826b5de9STakashi Sakamoto SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 25859502295STakashi Sakamoto SNDRV_PCM_HW_PARAM_RATE, -1); 259d67c46b9STakashi Sakamoto if (err < 0) 260d67c46b9STakashi Sakamoto goto end; 26159502295STakashi Sakamoto err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 26259502295STakashi Sakamoto apply_constraint_to_size, NULL, 263826b5de9STakashi Sakamoto SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 26459502295STakashi Sakamoto SNDRV_PCM_HW_PARAM_RATE, -1); 26559502295STakashi Sakamoto if (err < 0) 26659502295STakashi Sakamoto goto end; 267d67c46b9STakashi Sakamoto end: 268d67c46b9STakashi Sakamoto return err; 269d67c46b9STakashi Sakamoto } 270d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints); 271d67c46b9STakashi Sakamoto 272d67c46b9STakashi Sakamoto /** 273d67c46b9STakashi Sakamoto * amdtp_stream_set_parameters - set stream parameters 274d67c46b9STakashi Sakamoto * @s: the AMDTP stream to configure 275d67c46b9STakashi Sakamoto * @rate: the sample rate 276df075feeSTakashi Sakamoto * @data_block_quadlets: the size of a data block in quadlet unit 277d67c46b9STakashi Sakamoto * 278d67c46b9STakashi Sakamoto * The parameters must be set before the stream is started, and must not be 279d67c46b9STakashi Sakamoto * changed while the stream is running. 280d67c46b9STakashi Sakamoto */ 281df075feeSTakashi Sakamoto int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate, 282df075feeSTakashi Sakamoto unsigned int data_block_quadlets) 283d67c46b9STakashi Sakamoto { 284df075feeSTakashi Sakamoto unsigned int sfc; 285d67c46b9STakashi Sakamoto 286d67c46b9STakashi Sakamoto for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) { 287d67c46b9STakashi Sakamoto if (amdtp_rate_table[sfc] == rate) 288d67c46b9STakashi Sakamoto break; 289d67c46b9STakashi Sakamoto } 290d67c46b9STakashi Sakamoto if (sfc == ARRAY_SIZE(amdtp_rate_table)) 291d67c46b9STakashi Sakamoto return -EINVAL; 292d67c46b9STakashi Sakamoto 293d67c46b9STakashi Sakamoto s->sfc = sfc; 294df075feeSTakashi Sakamoto s->data_block_quadlets = data_block_quadlets; 295d67c46b9STakashi Sakamoto s->syt_interval = amdtp_syt_intervals[sfc]; 296d67c46b9STakashi Sakamoto 297d3d10a4aSTakashi Sakamoto // default buffering in the device. 29813d11f14STakashi Sakamoto s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE; 299d3d10a4aSTakashi Sakamoto 30013d11f14STakashi Sakamoto // additional buffering needed to adjust for no-data packets. 30113d11f14STakashi Sakamoto if (s->flags & CIP_BLOCKING) 30213d11f14STakashi Sakamoto s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate; 303d67c46b9STakashi Sakamoto 304d67c46b9STakashi Sakamoto return 0; 305d67c46b9STakashi Sakamoto } 306d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_set_parameters); 307d67c46b9STakashi Sakamoto 308c75f3678STakashi Sakamoto // The CIP header is processed in context header apart from context payload. 309c75f3678STakashi Sakamoto static int amdtp_stream_get_max_ctx_payload_size(struct amdtp_stream *s) 310c75f3678STakashi Sakamoto { 311c75f3678STakashi Sakamoto unsigned int multiplier; 312c75f3678STakashi Sakamoto 313c75f3678STakashi Sakamoto if (s->flags & CIP_JUMBO_PAYLOAD) 314c75f3678STakashi Sakamoto multiplier = IR_JUMBO_PAYLOAD_MAX_SKIP_CYCLES; 315c75f3678STakashi Sakamoto else 316c75f3678STakashi Sakamoto multiplier = 1; 317c75f3678STakashi Sakamoto 318c75f3678STakashi Sakamoto return s->syt_interval * s->data_block_quadlets * sizeof(__be32) * multiplier; 319c75f3678STakashi Sakamoto } 320c75f3678STakashi Sakamoto 321d67c46b9STakashi Sakamoto /** 322d67c46b9STakashi Sakamoto * amdtp_stream_get_max_payload - get the stream's packet size 323d67c46b9STakashi Sakamoto * @s: the AMDTP stream 324d67c46b9STakashi Sakamoto * 325d67c46b9STakashi Sakamoto * This function must not be called before the stream has been configured 326d67c46b9STakashi Sakamoto * with amdtp_stream_set_parameters(). 327d67c46b9STakashi Sakamoto */ 328d67c46b9STakashi Sakamoto unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s) 329d67c46b9STakashi Sakamoto { 330c75f3678STakashi Sakamoto unsigned int cip_header_size; 331d67c46b9STakashi Sakamoto 3323b196c39STakashi Sakamoto if (!(s->flags & CIP_NO_HEADER)) 33367d92ee7STakashi Sakamoto cip_header_size = CIP_HEADER_SIZE; 334c75f3678STakashi Sakamoto else 335c75f3678STakashi Sakamoto cip_header_size = 0; 336d67c46b9STakashi Sakamoto 337c75f3678STakashi Sakamoto return cip_header_size + amdtp_stream_get_max_ctx_payload_size(s); 338d67c46b9STakashi Sakamoto } 339d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_get_max_payload); 340d67c46b9STakashi Sakamoto 341d67c46b9STakashi Sakamoto /** 342d67c46b9STakashi Sakamoto * amdtp_stream_pcm_prepare - prepare PCM device for running 343d67c46b9STakashi Sakamoto * @s: the AMDTP stream 344d67c46b9STakashi Sakamoto * 345d67c46b9STakashi Sakamoto * This function should be called from the PCM device's .prepare callback. 346d67c46b9STakashi Sakamoto */ 347d67c46b9STakashi Sakamoto void amdtp_stream_pcm_prepare(struct amdtp_stream *s) 348d67c46b9STakashi Sakamoto { 3492b3d2987STakashi Iwai cancel_work_sync(&s->period_work); 350d67c46b9STakashi Sakamoto s->pcm_buffer_pointer = 0; 351d67c46b9STakashi Sakamoto s->pcm_period_pointer = 0; 352d67c46b9STakashi Sakamoto } 353d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_prepare); 354d67c46b9STakashi Sakamoto 355c9f3ac2aSTakashi Sakamoto static void pool_blocking_data_blocks(struct amdtp_stream *s, struct seq_desc *descs, 356c9f3ac2aSTakashi Sakamoto const unsigned int seq_size, unsigned int seq_tail, 357c9f3ac2aSTakashi Sakamoto unsigned int count) 358d67c46b9STakashi Sakamoto { 359c9f3ac2aSTakashi Sakamoto const unsigned int syt_interval = s->syt_interval; 360c9f3ac2aSTakashi Sakamoto int i; 361d67c46b9STakashi Sakamoto 362c9f3ac2aSTakashi Sakamoto for (i = 0; i < count; ++i) { 363c9f3ac2aSTakashi Sakamoto struct seq_desc *desc = descs + seq_tail; 364c9f3ac2aSTakashi Sakamoto 365c9f3ac2aSTakashi Sakamoto if (desc->syt_offset != CIP_SYT_NO_INFO) 366c9f3ac2aSTakashi Sakamoto desc->data_blocks = syt_interval; 367d67c46b9STakashi Sakamoto else 368c9f3ac2aSTakashi Sakamoto desc->data_blocks = 0; 369c9f3ac2aSTakashi Sakamoto 370c9f3ac2aSTakashi Sakamoto seq_tail = (seq_tail + 1) % seq_size; 371c9f3ac2aSTakashi Sakamoto } 372c9f3ac2aSTakashi Sakamoto } 373c9f3ac2aSTakashi Sakamoto 374c9f3ac2aSTakashi Sakamoto static void pool_ideal_nonblocking_data_blocks(struct amdtp_stream *s, struct seq_desc *descs, 375c9f3ac2aSTakashi Sakamoto const unsigned int seq_size, unsigned int seq_tail, 376c9f3ac2aSTakashi Sakamoto unsigned int count) 377c9f3ac2aSTakashi Sakamoto { 378c9f3ac2aSTakashi Sakamoto const enum cip_sfc sfc = s->sfc; 379c9f3ac2aSTakashi Sakamoto unsigned int state = s->ctx_data.rx.data_block_state; 380c9f3ac2aSTakashi Sakamoto int i; 381c9f3ac2aSTakashi Sakamoto 382c9f3ac2aSTakashi Sakamoto for (i = 0; i < count; ++i) { 383c9f3ac2aSTakashi Sakamoto struct seq_desc *desc = descs + seq_tail; 384c9f3ac2aSTakashi Sakamoto 385274fc355STakashi Sakamoto if (!cip_sfc_is_base_44100(sfc)) { 386d3d10a4aSTakashi Sakamoto // Sample_rate / 8000 is an integer, and precomputed. 387c9f3ac2aSTakashi Sakamoto desc->data_blocks = state; 388d67c46b9STakashi Sakamoto } else { 389c9f3ac2aSTakashi Sakamoto unsigned int phase = state; 390d67c46b9STakashi Sakamoto 391d67c46b9STakashi Sakamoto /* 392d67c46b9STakashi Sakamoto * This calculates the number of data blocks per packet so that 393d67c46b9STakashi Sakamoto * 1) the overall rate is correct and exactly synchronized to 394d67c46b9STakashi Sakamoto * the bus clock, and 395d67c46b9STakashi Sakamoto * 2) packets with a rounded-up number of blocks occur as early 396d67c46b9STakashi Sakamoto * as possible in the sequence (to prevent underruns of the 397d67c46b9STakashi Sakamoto * device's buffer). 398d67c46b9STakashi Sakamoto */ 399274fc355STakashi Sakamoto if (sfc == CIP_SFC_44100) 400d67c46b9STakashi Sakamoto /* 6 6 5 6 5 6 5 ... */ 401c9f3ac2aSTakashi Sakamoto desc->data_blocks = 5 + ((phase & 1) ^ (phase == 0 || phase >= 40)); 402d67c46b9STakashi Sakamoto else 403d67c46b9STakashi Sakamoto /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */ 404c9f3ac2aSTakashi Sakamoto desc->data_blocks = 11 * (sfc >> 1) + (phase == 0); 405274fc355STakashi Sakamoto if (++phase >= (80 >> (sfc >> 1))) 406d67c46b9STakashi Sakamoto phase = 0; 407c9f3ac2aSTakashi Sakamoto state = phase; 408d67c46b9STakashi Sakamoto } 409d67c46b9STakashi Sakamoto 410c9f3ac2aSTakashi Sakamoto seq_tail = (seq_tail + 1) % seq_size; 411c9f3ac2aSTakashi Sakamoto } 412c9f3ac2aSTakashi Sakamoto 413c9f3ac2aSTakashi Sakamoto s->ctx_data.rx.data_block_state = state; 414d67c46b9STakashi Sakamoto } 415d67c46b9STakashi Sakamoto 416816d8482STakashi Sakamoto static unsigned int calculate_syt_offset(unsigned int *last_syt_offset, 417816d8482STakashi Sakamoto unsigned int *syt_offset_state, enum cip_sfc sfc) 418d67c46b9STakashi Sakamoto { 419816d8482STakashi Sakamoto unsigned int syt_offset; 420d67c46b9STakashi Sakamoto 421816d8482STakashi Sakamoto if (*last_syt_offset < TICKS_PER_CYCLE) { 422816d8482STakashi Sakamoto if (!cip_sfc_is_base_44100(sfc)) 423816d8482STakashi Sakamoto syt_offset = *last_syt_offset + *syt_offset_state; 424d67c46b9STakashi Sakamoto else { 425d67c46b9STakashi Sakamoto /* 426d67c46b9STakashi Sakamoto * The time, in ticks, of the n'th SYT_INTERVAL sample is: 427d67c46b9STakashi Sakamoto * n * SYT_INTERVAL * 24576000 / sample_rate 428d67c46b9STakashi Sakamoto * Modulo TICKS_PER_CYCLE, the difference between successive 429d67c46b9STakashi Sakamoto * elements is about 1386.23. Rounding the results of this 430d67c46b9STakashi Sakamoto * formula to the SYT precision results in a sequence of 431d67c46b9STakashi Sakamoto * differences that begins with: 432d67c46b9STakashi Sakamoto * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ... 433d67c46b9STakashi Sakamoto * This code generates _exactly_ the same sequence. 434d67c46b9STakashi Sakamoto */ 435816d8482STakashi Sakamoto unsigned int phase = *syt_offset_state; 436816d8482STakashi Sakamoto unsigned int index = phase % 13; 437816d8482STakashi Sakamoto 438816d8482STakashi Sakamoto syt_offset = *last_syt_offset; 439d67c46b9STakashi Sakamoto syt_offset += 1386 + ((index && !(index & 3)) || 440d67c46b9STakashi Sakamoto phase == 146); 441d67c46b9STakashi Sakamoto if (++phase >= 147) 442d67c46b9STakashi Sakamoto phase = 0; 443816d8482STakashi Sakamoto *syt_offset_state = phase; 444d67c46b9STakashi Sakamoto } 445d67c46b9STakashi Sakamoto } else 446816d8482STakashi Sakamoto syt_offset = *last_syt_offset - TICKS_PER_CYCLE; 447816d8482STakashi Sakamoto *last_syt_offset = syt_offset; 448d67c46b9STakashi Sakamoto 44983cfb5c5STakashi Sakamoto if (syt_offset >= TICKS_PER_CYCLE) 45083cfb5c5STakashi Sakamoto syt_offset = CIP_SYT_NO_INFO; 451d67c46b9STakashi Sakamoto 45283cfb5c5STakashi Sakamoto return syt_offset; 453d67c46b9STakashi Sakamoto } 454d67c46b9STakashi Sakamoto 455c79b7158STakashi Sakamoto static void pool_ideal_syt_offsets(struct amdtp_stream *s, struct seq_desc *descs, 456c79b7158STakashi Sakamoto const unsigned int seq_size, unsigned int seq_tail, 457c79b7158STakashi Sakamoto unsigned int count) 458c79b7158STakashi Sakamoto { 459c79b7158STakashi Sakamoto const enum cip_sfc sfc = s->sfc; 460c79b7158STakashi Sakamoto unsigned int last = s->ctx_data.rx.last_syt_offset; 461c79b7158STakashi Sakamoto unsigned int state = s->ctx_data.rx.syt_offset_state; 462c79b7158STakashi Sakamoto int i; 463c79b7158STakashi Sakamoto 464c79b7158STakashi Sakamoto for (i = 0; i < count; ++i) { 465c79b7158STakashi Sakamoto struct seq_desc *desc = descs + seq_tail; 466c79b7158STakashi Sakamoto 467c79b7158STakashi Sakamoto desc->syt_offset = calculate_syt_offset(&last, &state, sfc); 468c79b7158STakashi Sakamoto 469c79b7158STakashi Sakamoto seq_tail = (seq_tail + 1) % seq_size; 470c79b7158STakashi Sakamoto } 471c79b7158STakashi Sakamoto 472c79b7158STakashi Sakamoto s->ctx_data.rx.last_syt_offset = last; 473c79b7158STakashi Sakamoto s->ctx_data.rx.syt_offset_state = state; 474c79b7158STakashi Sakamoto } 475c79b7158STakashi Sakamoto 476f9e5ecdfSTakashi Sakamoto static unsigned int compute_syt_offset(unsigned int syt, unsigned int cycle, 477f9e5ecdfSTakashi Sakamoto unsigned int transfer_delay) 478f9e5ecdfSTakashi Sakamoto { 479f9e5ecdfSTakashi Sakamoto unsigned int cycle_lo = (cycle % CYCLES_PER_SECOND) & 0x0f; 480f9e5ecdfSTakashi Sakamoto unsigned int syt_cycle_lo = (syt & 0xf000) >> 12; 481f9e5ecdfSTakashi Sakamoto unsigned int syt_offset; 482f9e5ecdfSTakashi Sakamoto 483f9e5ecdfSTakashi Sakamoto // Round up. 484f9e5ecdfSTakashi Sakamoto if (syt_cycle_lo < cycle_lo) 485f9e5ecdfSTakashi Sakamoto syt_cycle_lo += CIP_SYT_CYCLE_MODULUS; 486f9e5ecdfSTakashi Sakamoto syt_cycle_lo -= cycle_lo; 487f9e5ecdfSTakashi Sakamoto 488f9e5ecdfSTakashi Sakamoto // Subtract transfer delay so that the synchronization offset is not so large 489f9e5ecdfSTakashi Sakamoto // at transmission. 490f9e5ecdfSTakashi Sakamoto syt_offset = syt_cycle_lo * TICKS_PER_CYCLE + (syt & 0x0fff); 491f9e5ecdfSTakashi Sakamoto if (syt_offset < transfer_delay) 492f9e5ecdfSTakashi Sakamoto syt_offset += CIP_SYT_CYCLE_MODULUS * TICKS_PER_CYCLE; 493f9e5ecdfSTakashi Sakamoto 494f9e5ecdfSTakashi Sakamoto return syt_offset - transfer_delay; 495f9e5ecdfSTakashi Sakamoto } 496f9e5ecdfSTakashi Sakamoto 49739c2649cSTakashi Sakamoto // Both of the producer and consumer of the queue runs in the same clock of IEEE 1394 bus. 49839c2649cSTakashi Sakamoto // Additionally, the sequence of tx packets is severely checked against any discontinuity 49939c2649cSTakashi Sakamoto // before filling entries in the queue. The calculation is safe even if it looks fragile by 50039c2649cSTakashi Sakamoto // overrun. 50139c2649cSTakashi Sakamoto static unsigned int calculate_cached_cycle_count(struct amdtp_stream *s, unsigned int head) 50239c2649cSTakashi Sakamoto { 50339c2649cSTakashi Sakamoto const unsigned int cache_size = s->ctx_data.tx.cache.size; 50439c2649cSTakashi Sakamoto unsigned int cycles = s->ctx_data.tx.cache.tail; 50539c2649cSTakashi Sakamoto 50639c2649cSTakashi Sakamoto if (cycles < head) 50739c2649cSTakashi Sakamoto cycles += cache_size; 50839c2649cSTakashi Sakamoto cycles -= head; 50939c2649cSTakashi Sakamoto 51039c2649cSTakashi Sakamoto return cycles; 51139c2649cSTakashi Sakamoto } 51239c2649cSTakashi Sakamoto 513f9e5ecdfSTakashi Sakamoto static void cache_seq(struct amdtp_stream *s, const struct pkt_desc *descs, unsigned int desc_count) 514f9e5ecdfSTakashi Sakamoto { 515f9e5ecdfSTakashi Sakamoto const unsigned int transfer_delay = s->transfer_delay; 516f9e5ecdfSTakashi Sakamoto const unsigned int cache_size = s->ctx_data.tx.cache.size; 517f9e5ecdfSTakashi Sakamoto struct seq_desc *cache = s->ctx_data.tx.cache.descs; 518f9e5ecdfSTakashi Sakamoto unsigned int cache_tail = s->ctx_data.tx.cache.tail; 519f9e5ecdfSTakashi Sakamoto bool aware_syt = !(s->flags & CIP_UNAWARE_SYT); 520f9e5ecdfSTakashi Sakamoto int i; 521f9e5ecdfSTakashi Sakamoto 522f9e5ecdfSTakashi Sakamoto for (i = 0; i < desc_count; ++i) { 523f9e5ecdfSTakashi Sakamoto struct seq_desc *dst = cache + cache_tail; 524f9e5ecdfSTakashi Sakamoto const struct pkt_desc *src = descs + i; 525f9e5ecdfSTakashi Sakamoto 526f9e5ecdfSTakashi Sakamoto if (aware_syt && src->syt != CIP_SYT_NO_INFO) 527f9e5ecdfSTakashi Sakamoto dst->syt_offset = compute_syt_offset(src->syt, src->cycle, transfer_delay); 528f9e5ecdfSTakashi Sakamoto else 529f9e5ecdfSTakashi Sakamoto dst->syt_offset = CIP_SYT_NO_INFO; 530f9e5ecdfSTakashi Sakamoto dst->data_blocks = src->data_blocks; 531f9e5ecdfSTakashi Sakamoto 532f9e5ecdfSTakashi Sakamoto cache_tail = (cache_tail + 1) % cache_size; 533f9e5ecdfSTakashi Sakamoto } 534f9e5ecdfSTakashi Sakamoto 535f9e5ecdfSTakashi Sakamoto s->ctx_data.tx.cache.tail = cache_tail; 536f9e5ecdfSTakashi Sakamoto } 537f9e5ecdfSTakashi Sakamoto 5386f24bb8aSTakashi Sakamoto static void pool_ideal_seq_descs(struct amdtp_stream *s, unsigned int count) 5396f24bb8aSTakashi Sakamoto { 540c79b7158STakashi Sakamoto struct seq_desc *descs = s->ctx_data.rx.seq.descs; 5416f24bb8aSTakashi Sakamoto unsigned int seq_tail = s->ctx_data.rx.seq.tail; 5426f24bb8aSTakashi Sakamoto const unsigned int seq_size = s->ctx_data.rx.seq.size; 5436f24bb8aSTakashi Sakamoto 544c79b7158STakashi Sakamoto pool_ideal_syt_offsets(s, descs, seq_size, seq_tail, count); 545c79b7158STakashi Sakamoto 546c9f3ac2aSTakashi Sakamoto if (s->flags & CIP_BLOCKING) 547c9f3ac2aSTakashi Sakamoto pool_blocking_data_blocks(s, descs, seq_size, seq_tail, count); 548c9f3ac2aSTakashi Sakamoto else 549c9f3ac2aSTakashi Sakamoto pool_ideal_nonblocking_data_blocks(s, descs, seq_size, seq_tail, count); 5506f24bb8aSTakashi Sakamoto 551c9f3ac2aSTakashi Sakamoto s->ctx_data.rx.seq.tail = (seq_tail + count) % seq_size; 5526f24bb8aSTakashi Sakamoto } 5536f24bb8aSTakashi Sakamoto 55439c2649cSTakashi Sakamoto static void pool_replayed_seq(struct amdtp_stream *s, unsigned int count) 55539c2649cSTakashi Sakamoto { 55639c2649cSTakashi Sakamoto struct amdtp_stream *target = s->ctx_data.rx.replay_target; 55739c2649cSTakashi Sakamoto const struct seq_desc *cache = target->ctx_data.tx.cache.descs; 55839c2649cSTakashi Sakamoto const unsigned int cache_size = target->ctx_data.tx.cache.size; 55939c2649cSTakashi Sakamoto unsigned int cache_head = s->ctx_data.rx.cache_head; 56039c2649cSTakashi Sakamoto struct seq_desc *descs = s->ctx_data.rx.seq.descs; 56139c2649cSTakashi Sakamoto const unsigned int seq_size = s->ctx_data.rx.seq.size; 56239c2649cSTakashi Sakamoto unsigned int seq_tail = s->ctx_data.rx.seq.tail; 56339c2649cSTakashi Sakamoto int i; 56439c2649cSTakashi Sakamoto 56539c2649cSTakashi Sakamoto for (i = 0; i < count; ++i) { 56639c2649cSTakashi Sakamoto descs[seq_tail] = cache[cache_head]; 56739c2649cSTakashi Sakamoto seq_tail = (seq_tail + 1) % seq_size; 56839c2649cSTakashi Sakamoto cache_head = (cache_head + 1) % cache_size; 56939c2649cSTakashi Sakamoto } 57039c2649cSTakashi Sakamoto 57139c2649cSTakashi Sakamoto s->ctx_data.rx.seq.tail = seq_tail; 57239c2649cSTakashi Sakamoto s->ctx_data.rx.cache_head = cache_head; 57339c2649cSTakashi Sakamoto } 57439c2649cSTakashi Sakamoto 57539c2649cSTakashi Sakamoto static void pool_seq_descs(struct amdtp_stream *s, unsigned int count) 57639c2649cSTakashi Sakamoto { 57739c2649cSTakashi Sakamoto struct amdtp_domain *d = s->domain; 57839c2649cSTakashi Sakamoto 5792f21a177STakashi Sakamoto if (!d->replay.enable || !s->ctx_data.rx.replay_target) { 58039c2649cSTakashi Sakamoto pool_ideal_seq_descs(s, count); 5812f21a177STakashi Sakamoto } else { 5822f21a177STakashi Sakamoto if (!d->replay.on_the_fly) { 58339c2649cSTakashi Sakamoto pool_replayed_seq(s, count); 5842f21a177STakashi Sakamoto } else { 5852f21a177STakashi Sakamoto struct amdtp_stream *tx = s->ctx_data.rx.replay_target; 5862f21a177STakashi Sakamoto const unsigned int cache_size = tx->ctx_data.tx.cache.size; 5872f21a177STakashi Sakamoto const unsigned int cache_head = s->ctx_data.rx.cache_head; 5882f21a177STakashi Sakamoto unsigned int cached_cycles = calculate_cached_cycle_count(tx, cache_head); 5892f21a177STakashi Sakamoto 5902f21a177STakashi Sakamoto if (cached_cycles > count && cached_cycles > cache_size / 2) 5912f21a177STakashi Sakamoto pool_replayed_seq(s, count); 5922f21a177STakashi Sakamoto else 5932f21a177STakashi Sakamoto pool_ideal_seq_descs(s, count); 5942f21a177STakashi Sakamoto } 5952f21a177STakashi Sakamoto } 59639c2649cSTakashi Sakamoto } 59739c2649cSTakashi Sakamoto 598d67c46b9STakashi Sakamoto static void update_pcm_pointers(struct amdtp_stream *s, 599d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm, 600d67c46b9STakashi Sakamoto unsigned int frames) 601d67c46b9STakashi Sakamoto { 602d67c46b9STakashi Sakamoto unsigned int ptr; 603d67c46b9STakashi Sakamoto 604d67c46b9STakashi Sakamoto ptr = s->pcm_buffer_pointer + frames; 605d67c46b9STakashi Sakamoto if (ptr >= pcm->runtime->buffer_size) 606d67c46b9STakashi Sakamoto ptr -= pcm->runtime->buffer_size; 6076aa7de05SMark Rutland WRITE_ONCE(s->pcm_buffer_pointer, ptr); 608d67c46b9STakashi Sakamoto 609d67c46b9STakashi Sakamoto s->pcm_period_pointer += frames; 610d67c46b9STakashi Sakamoto if (s->pcm_period_pointer >= pcm->runtime->period_size) { 611d67c46b9STakashi Sakamoto s->pcm_period_pointer -= pcm->runtime->period_size; 612d360870aSTakashi Sakamoto 613d360870aSTakashi Sakamoto // The program in user process should periodically check the status of intermediate 614d360870aSTakashi Sakamoto // buffer associated to PCM substream to process PCM frames in the buffer, instead 615d360870aSTakashi Sakamoto // of receiving notification of period elapsed by poll wait. 616*7ba5ca32STakashi Sakamoto if (!pcm->runtime->no_period_wakeup) { 617*7ba5ca32STakashi Sakamoto if (in_interrupt()) { 618*7ba5ca32STakashi Sakamoto // In software IRQ context for 1394 OHCI. 619*7ba5ca32STakashi Sakamoto snd_pcm_period_elapsed(pcm); 620*7ba5ca32STakashi Sakamoto } else { 621*7ba5ca32STakashi Sakamoto // In process context of ALSA PCM application under acquired lock of 622*7ba5ca32STakashi Sakamoto // PCM substream. 623*7ba5ca32STakashi Sakamoto snd_pcm_period_elapsed_under_stream_lock(pcm); 624*7ba5ca32STakashi Sakamoto } 625*7ba5ca32STakashi Sakamoto } 626d67c46b9STakashi Sakamoto } 627d67c46b9STakashi Sakamoto } 628d67c46b9STakashi Sakamoto 6292b3d2987STakashi Iwai static void pcm_period_work(struct work_struct *work) 630d67c46b9STakashi Sakamoto { 6312b3d2987STakashi Iwai struct amdtp_stream *s = container_of(work, struct amdtp_stream, 6322b3d2987STakashi Iwai period_work); 6336aa7de05SMark Rutland struct snd_pcm_substream *pcm = READ_ONCE(s->pcm); 634d67c46b9STakashi Sakamoto 635d67c46b9STakashi Sakamoto if (pcm) 636d67c46b9STakashi Sakamoto snd_pcm_period_elapsed(pcm); 637d67c46b9STakashi Sakamoto } 638d67c46b9STakashi Sakamoto 639e229853dSTakashi Sakamoto static int queue_packet(struct amdtp_stream *s, struct fw_iso_packet *params, 640e229853dSTakashi Sakamoto bool sched_irq) 641d67c46b9STakashi Sakamoto { 6426007bf54STakashi Sakamoto int err; 643d67c46b9STakashi Sakamoto 644e229853dSTakashi Sakamoto params->interrupt = sched_irq; 6456007bf54STakashi Sakamoto params->tag = s->tag; 6466007bf54STakashi Sakamoto params->sy = 0; 647d67c46b9STakashi Sakamoto 6486007bf54STakashi Sakamoto err = fw_iso_context_queue(s->context, params, &s->buffer.iso_buffer, 649d67c46b9STakashi Sakamoto s->buffer.packets[s->packet_index].offset); 650d67c46b9STakashi Sakamoto if (err < 0) { 651d67c46b9STakashi Sakamoto dev_err(&s->unit->device, "queueing error: %d\n", err); 652d67c46b9STakashi Sakamoto goto end; 653d67c46b9STakashi Sakamoto } 654d67c46b9STakashi Sakamoto 655a0e02331STakashi Sakamoto if (++s->packet_index >= s->queue_size) 656d67c46b9STakashi Sakamoto s->packet_index = 0; 657d67c46b9STakashi Sakamoto end: 658d67c46b9STakashi Sakamoto return err; 659d67c46b9STakashi Sakamoto } 660d67c46b9STakashi Sakamoto 661d67c46b9STakashi Sakamoto static inline int queue_out_packet(struct amdtp_stream *s, 662e229853dSTakashi Sakamoto struct fw_iso_packet *params, bool sched_irq) 663d67c46b9STakashi Sakamoto { 664b18f0cfaSTakashi Sakamoto params->skip = 665b18f0cfaSTakashi Sakamoto !!(params->header_length == 0 && params->payload_length == 0); 666e229853dSTakashi Sakamoto return queue_packet(s, params, sched_irq); 667d67c46b9STakashi Sakamoto } 668d67c46b9STakashi Sakamoto 6696007bf54STakashi Sakamoto static inline int queue_in_packet(struct amdtp_stream *s, 67060dd4929STakashi Sakamoto struct fw_iso_packet *params) 671d67c46b9STakashi Sakamoto { 6726007bf54STakashi Sakamoto // Queue one packet for IR context. 6736007bf54STakashi Sakamoto params->header_length = s->ctx_data.tx.ctx_header_size; 6746007bf54STakashi Sakamoto params->payload_length = s->ctx_data.tx.max_ctx_payload_length; 6756007bf54STakashi Sakamoto params->skip = false; 67660dd4929STakashi Sakamoto return queue_packet(s, params, false); 677d67c46b9STakashi Sakamoto } 678d67c46b9STakashi Sakamoto 679252219c7STakashi Sakamoto static void generate_cip_header(struct amdtp_stream *s, __be32 cip_header[2], 680860d798cSTakashi Sakamoto unsigned int data_block_counter, unsigned int syt) 681252219c7STakashi Sakamoto { 682252219c7STakashi Sakamoto cip_header[0] = cpu_to_be32(READ_ONCE(s->source_node_id_field) | 683252219c7STakashi Sakamoto (s->data_block_quadlets << CIP_DBS_SHIFT) | 684252219c7STakashi Sakamoto ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) | 685860d798cSTakashi Sakamoto data_block_counter); 686252219c7STakashi Sakamoto cip_header[1] = cpu_to_be32(CIP_EOH | 687252219c7STakashi Sakamoto ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) | 688252219c7STakashi Sakamoto ((s->ctx_data.rx.fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) | 689252219c7STakashi Sakamoto (syt & CIP_SYT_MASK)); 690252219c7STakashi Sakamoto } 691252219c7STakashi Sakamoto 6926bc1a269STakashi Sakamoto static void build_it_pkt_header(struct amdtp_stream *s, unsigned int cycle, 693233dbbc7STakashi Sakamoto struct fw_iso_packet *params, unsigned int header_length, 694860d798cSTakashi Sakamoto unsigned int data_blocks, 695860d798cSTakashi Sakamoto unsigned int data_block_counter, 696860d798cSTakashi Sakamoto unsigned int syt, unsigned int index) 697d67c46b9STakashi Sakamoto { 6980ebf3cebSTakashi Sakamoto unsigned int payload_length; 69916be4589STakashi Sakamoto __be32 *cip_header; 700d67c46b9STakashi Sakamoto 7010ebf3cebSTakashi Sakamoto payload_length = data_blocks * sizeof(__be32) * s->data_block_quadlets; 7020ebf3cebSTakashi Sakamoto params->payload_length = payload_length; 7030ebf3cebSTakashi Sakamoto 704233dbbc7STakashi Sakamoto if (header_length > 0) { 7056bc1a269STakashi Sakamoto cip_header = (__be32 *)params->header; 706860d798cSTakashi Sakamoto generate_cip_header(s, cip_header, data_block_counter, syt); 707233dbbc7STakashi Sakamoto params->header_length = header_length; 708b18f0cfaSTakashi Sakamoto } else { 709b18f0cfaSTakashi Sakamoto cip_header = NULL; 710b18f0cfaSTakashi Sakamoto } 711d67c46b9STakashi Sakamoto 712233dbbc7STakashi Sakamoto trace_amdtp_packet(s, cycle, cip_header, payload_length + header_length, data_blocks, 713814b4312STakashi Sakamoto data_block_counter, s->packet_index, index); 7143b196c39STakashi Sakamoto } 7153b196c39STakashi Sakamoto 716e335425bSTakashi Sakamoto static int check_cip_header(struct amdtp_stream *s, const __be32 *buf, 717e335425bSTakashi Sakamoto unsigned int payload_length, 718a35463d1STakashi Sakamoto unsigned int *data_blocks, 719a35463d1STakashi Sakamoto unsigned int *data_block_counter, unsigned int *syt) 720d67c46b9STakashi Sakamoto { 721d67c46b9STakashi Sakamoto u32 cip_header[2]; 722e335425bSTakashi Sakamoto unsigned int sph; 723e335425bSTakashi Sakamoto unsigned int fmt; 724e335425bSTakashi Sakamoto unsigned int fdf; 725a35463d1STakashi Sakamoto unsigned int dbc; 726d67c46b9STakashi Sakamoto bool lost; 727d67c46b9STakashi Sakamoto 728e335425bSTakashi Sakamoto cip_header[0] = be32_to_cpu(buf[0]); 729e335425bSTakashi Sakamoto cip_header[1] = be32_to_cpu(buf[1]); 730d67c46b9STakashi Sakamoto 731d67c46b9STakashi Sakamoto /* 732d67c46b9STakashi Sakamoto * This module supports 'Two-quadlet CIP header with SYT field'. 733d67c46b9STakashi Sakamoto * For convenience, also check FMT field is AM824 or not. 734d67c46b9STakashi Sakamoto */ 7352128f78fSTakashi Sakamoto if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) || 7362128f78fSTakashi Sakamoto ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) && 7372128f78fSTakashi Sakamoto (!(s->flags & CIP_HEADER_WITHOUT_EOH))) { 738d67c46b9STakashi Sakamoto dev_info_ratelimited(&s->unit->device, 739d67c46b9STakashi Sakamoto "Invalid CIP header for AMDTP: %08X:%08X\n", 740d67c46b9STakashi Sakamoto cip_header[0], cip_header[1]); 741e335425bSTakashi Sakamoto return -EAGAIN; 742d67c46b9STakashi Sakamoto } 743d67c46b9STakashi Sakamoto 744d67c46b9STakashi Sakamoto /* Check valid protocol or not. */ 7459863874fSTakashi Sakamoto sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT; 746d67c46b9STakashi Sakamoto fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT; 7479863874fSTakashi Sakamoto if (sph != s->sph || fmt != s->fmt) { 7482a7e1713STakashi Sakamoto dev_info_ratelimited(&s->unit->device, 749d67c46b9STakashi Sakamoto "Detect unexpected protocol: %08x %08x\n", 750d67c46b9STakashi Sakamoto cip_header[0], cip_header[1]); 751e335425bSTakashi Sakamoto return -EAGAIN; 752d67c46b9STakashi Sakamoto } 753d67c46b9STakashi Sakamoto 754d67c46b9STakashi Sakamoto /* Calculate data blocks */ 755d67c46b9STakashi Sakamoto fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT; 7564fd18787STakashi Sakamoto if (payload_length == 0 || (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) { 757e335425bSTakashi Sakamoto *data_blocks = 0; 758d67c46b9STakashi Sakamoto } else { 759e335425bSTakashi Sakamoto unsigned int data_block_quadlets = 760d67c46b9STakashi Sakamoto (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT; 761d67c46b9STakashi Sakamoto /* avoid division by zero */ 762d67c46b9STakashi Sakamoto if (data_block_quadlets == 0) { 763d67c46b9STakashi Sakamoto dev_err(&s->unit->device, 764d67c46b9STakashi Sakamoto "Detect invalid value in dbs field: %08X\n", 765d67c46b9STakashi Sakamoto cip_header[0]); 766d67c46b9STakashi Sakamoto return -EPROTO; 767d67c46b9STakashi Sakamoto } 768d67c46b9STakashi Sakamoto if (s->flags & CIP_WRONG_DBS) 769d67c46b9STakashi Sakamoto data_block_quadlets = s->data_block_quadlets; 770d67c46b9STakashi Sakamoto 7714fd18787STakashi Sakamoto *data_blocks = payload_length / sizeof(__be32) / data_block_quadlets; 772d67c46b9STakashi Sakamoto } 773d67c46b9STakashi Sakamoto 774d67c46b9STakashi Sakamoto /* Check data block counter continuity */ 775a35463d1STakashi Sakamoto dbc = cip_header[0] & CIP_DBC_MASK; 776e335425bSTakashi Sakamoto if (*data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) && 777a35463d1STakashi Sakamoto *data_block_counter != UINT_MAX) 778a35463d1STakashi Sakamoto dbc = *data_block_counter; 779d67c46b9STakashi Sakamoto 780a35463d1STakashi Sakamoto if ((dbc == 0x00 && (s->flags & CIP_SKIP_DBC_ZERO_CHECK)) || 781a35463d1STakashi Sakamoto *data_block_counter == UINT_MAX) { 782d67c46b9STakashi Sakamoto lost = false; 783d67c46b9STakashi Sakamoto } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) { 784a35463d1STakashi Sakamoto lost = dbc != *data_block_counter; 785d67c46b9STakashi Sakamoto } else { 786e335425bSTakashi Sakamoto unsigned int dbc_interval; 787e335425bSTakashi Sakamoto 788e335425bSTakashi Sakamoto if (*data_blocks > 0 && s->ctx_data.tx.dbc_interval > 0) 789d3d10a4aSTakashi Sakamoto dbc_interval = s->ctx_data.tx.dbc_interval; 790d67c46b9STakashi Sakamoto else 791e335425bSTakashi Sakamoto dbc_interval = *data_blocks; 792d67c46b9STakashi Sakamoto 793a35463d1STakashi Sakamoto lost = dbc != ((*data_block_counter + dbc_interval) & 0xff); 794d67c46b9STakashi Sakamoto } 795d67c46b9STakashi Sakamoto 796d67c46b9STakashi Sakamoto if (lost) { 797d67c46b9STakashi Sakamoto dev_err(&s->unit->device, 798d67c46b9STakashi Sakamoto "Detect discontinuity of CIP: %02X %02X\n", 799a35463d1STakashi Sakamoto *data_block_counter, dbc); 800d67c46b9STakashi Sakamoto return -EIO; 801d67c46b9STakashi Sakamoto } 802d67c46b9STakashi Sakamoto 803753e7179STakashi Sakamoto *data_block_counter = dbc; 804753e7179STakashi Sakamoto 8058070d265STakashi Sakamoto if (!(s->flags & CIP_UNAWARE_SYT)) 806e335425bSTakashi Sakamoto *syt = cip_header[1] & CIP_SYT_MASK; 807e335425bSTakashi Sakamoto 808e335425bSTakashi Sakamoto return 0; 809e335425bSTakashi Sakamoto } 810e335425bSTakashi Sakamoto 81198e3e43bSTakashi Sakamoto static int parse_ir_ctx_header(struct amdtp_stream *s, unsigned int cycle, 81298e3e43bSTakashi Sakamoto const __be32 *ctx_header, 813a35463d1STakashi Sakamoto unsigned int *data_blocks, 814a35463d1STakashi Sakamoto unsigned int *data_block_counter, 815814b4312STakashi Sakamoto unsigned int *syt, unsigned int packet_index, unsigned int index) 816e335425bSTakashi Sakamoto { 817ebd2a647STakashi Sakamoto unsigned int payload_length; 818f11453c7STakashi Sakamoto const __be32 *cip_header; 819395f41e2STakashi Sakamoto unsigned int cip_header_size; 820e335425bSTakashi Sakamoto 821ebd2a647STakashi Sakamoto payload_length = be32_to_cpu(ctx_header[0]) >> ISO_DATA_LENGTH_SHIFT; 822395f41e2STakashi Sakamoto 823395f41e2STakashi Sakamoto if (!(s->flags & CIP_NO_HEADER)) 82467d92ee7STakashi Sakamoto cip_header_size = CIP_HEADER_SIZE; 825395f41e2STakashi Sakamoto else 826395f41e2STakashi Sakamoto cip_header_size = 0; 827395f41e2STakashi Sakamoto 828ebd2a647STakashi Sakamoto if (payload_length > cip_header_size + s->ctx_data.tx.max_ctx_payload_length) { 829e335425bSTakashi Sakamoto dev_err(&s->unit->device, 830e335425bSTakashi Sakamoto "Detect jumbo payload: %04x %04x\n", 831ebd2a647STakashi Sakamoto payload_length, cip_header_size + s->ctx_data.tx.max_ctx_payload_length); 832e335425bSTakashi Sakamoto return -EIO; 833e335425bSTakashi Sakamoto } 834e335425bSTakashi Sakamoto 835395f41e2STakashi Sakamoto if (cip_header_size > 0) { 836ebd2a647STakashi Sakamoto if (payload_length >= cip_header_size) { 837344f0f82STakashi Sakamoto int err; 838344f0f82STakashi Sakamoto 83967d92ee7STakashi Sakamoto cip_header = ctx_header + IR_CTX_HEADER_DEFAULT_QUADLETS; 8404fd18787STakashi Sakamoto err = check_cip_header(s, cip_header, payload_length - cip_header_size, 8414fd18787STakashi Sakamoto data_blocks, data_block_counter, syt); 842b8b0e24cSTakashi Sakamoto if (err < 0) 843e335425bSTakashi Sakamoto return err; 844947b437eSTakashi Sakamoto } else { 845c09010eeSTakashi Sakamoto // Handle the cycle so that empty packet arrives. 846c09010eeSTakashi Sakamoto cip_header = NULL; 847c09010eeSTakashi Sakamoto *data_blocks = 0; 848c09010eeSTakashi Sakamoto *syt = 0; 849c09010eeSTakashi Sakamoto } 850c09010eeSTakashi Sakamoto } else { 851947b437eSTakashi Sakamoto cip_header = NULL; 852ebd2a647STakashi Sakamoto *data_blocks = payload_length / sizeof(__be32) / s->data_block_quadlets; 85398e3e43bSTakashi Sakamoto *syt = 0; 8547fbf9096STakashi Sakamoto 855a35463d1STakashi Sakamoto if (*data_block_counter == UINT_MAX) 856a35463d1STakashi Sakamoto *data_block_counter = 0; 857947b437eSTakashi Sakamoto } 858e335425bSTakashi Sakamoto 859ebd2a647STakashi Sakamoto trace_amdtp_packet(s, cycle, cip_header, payload_length, *data_blocks, 860814b4312STakashi Sakamoto *data_block_counter, packet_index, index); 86164d0bf4dSTakashi Sakamoto 862344f0f82STakashi Sakamoto return 0; 863d67c46b9STakashi Sakamoto } 864d67c46b9STakashi Sakamoto 86526cd1e58STakashi Sakamoto // In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On 86626cd1e58STakashi Sakamoto // the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent 86726cd1e58STakashi Sakamoto // it. Thus, via Linux firewire subsystem, we can get the 3 bits for second. 8683e106f4fSTakashi Sakamoto static inline u32 compute_ohci_cycle_count(__be32 ctx_header_tstamp) 86973fc7f08STakashi Sakamoto { 87026cd1e58STakashi Sakamoto u32 tstamp = be32_to_cpu(ctx_header_tstamp) & HEADER_TSTAMP_MASK; 87173fc7f08STakashi Sakamoto return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff); 87273fc7f08STakashi Sakamoto } 87373fc7f08STakashi Sakamoto 8743e106f4fSTakashi Sakamoto static inline u32 increment_ohci_cycle_count(u32 cycle, unsigned int addend) 87573fc7f08STakashi Sakamoto { 87673fc7f08STakashi Sakamoto cycle += addend; 8773e106f4fSTakashi Sakamoto if (cycle >= OHCI_SECOND_MODULUS * CYCLES_PER_SECOND) 8783e106f4fSTakashi Sakamoto cycle -= OHCI_SECOND_MODULUS * CYCLES_PER_SECOND; 87973fc7f08STakashi Sakamoto return cycle; 88073fc7f08STakashi Sakamoto } 88173fc7f08STakashi Sakamoto 882705794c5STakashi Sakamoto static int compare_ohci_cycle_count(u32 lval, u32 rval) 883705794c5STakashi Sakamoto { 884705794c5STakashi Sakamoto if (lval == rval) 885705794c5STakashi Sakamoto return 0; 886705794c5STakashi Sakamoto else if (lval < rval && rval - lval < OHCI_SECOND_MODULUS * CYCLES_PER_SECOND / 2) 887705794c5STakashi Sakamoto return -1; 888705794c5STakashi Sakamoto else 889705794c5STakashi Sakamoto return 1; 890705794c5STakashi Sakamoto } 891705794c5STakashi Sakamoto 89226cd1e58STakashi Sakamoto // Align to actual cycle count for the packet which is going to be scheduled. 893a0e02331STakashi Sakamoto // This module queued the same number of isochronous cycle as the size of queue 894a0e02331STakashi Sakamoto // to kip isochronous cycle, therefore it's OK to just increment the cycle by 895a0e02331STakashi Sakamoto // the size of queue for scheduled cycle. 8963e106f4fSTakashi Sakamoto static inline u32 compute_ohci_it_cycle(const __be32 ctx_header_tstamp, 897a0e02331STakashi Sakamoto unsigned int queue_size) 89826cd1e58STakashi Sakamoto { 8993e106f4fSTakashi Sakamoto u32 cycle = compute_ohci_cycle_count(ctx_header_tstamp); 9003e106f4fSTakashi Sakamoto return increment_ohci_cycle_count(cycle, queue_size); 90126cd1e58STakashi Sakamoto } 90226cd1e58STakashi Sakamoto 903753e7179STakashi Sakamoto static int generate_device_pkt_descs(struct amdtp_stream *s, 904753e7179STakashi Sakamoto struct pkt_desc *descs, 905753e7179STakashi Sakamoto const __be32 *ctx_header, 90673246fc4STakashi Sakamoto unsigned int packets, 90773246fc4STakashi Sakamoto unsigned int *desc_count) 908753e7179STakashi Sakamoto { 909705794c5STakashi Sakamoto unsigned int next_cycle = s->next_cycle; 910753e7179STakashi Sakamoto unsigned int dbc = s->data_block_counter; 911814b4312STakashi Sakamoto unsigned int packet_index = s->packet_index; 912814b4312STakashi Sakamoto unsigned int queue_size = s->queue_size; 913753e7179STakashi Sakamoto int i; 914753e7179STakashi Sakamoto int err; 915753e7179STakashi Sakamoto 91673246fc4STakashi Sakamoto *desc_count = 0; 917753e7179STakashi Sakamoto for (i = 0; i < packets; ++i) { 91873246fc4STakashi Sakamoto struct pkt_desc *desc = descs + *desc_count; 919753e7179STakashi Sakamoto unsigned int cycle; 920705794c5STakashi Sakamoto bool lost; 921753e7179STakashi Sakamoto unsigned int data_blocks; 922753e7179STakashi Sakamoto unsigned int syt; 923753e7179STakashi Sakamoto 9243e106f4fSTakashi Sakamoto cycle = compute_ohci_cycle_count(ctx_header[1]); 925705794c5STakashi Sakamoto lost = (next_cycle != cycle); 926705794c5STakashi Sakamoto if (lost) { 927705794c5STakashi Sakamoto if (s->flags & CIP_NO_HEADER) { 928705794c5STakashi Sakamoto // Fireface skips transmission just for an isoc cycle corresponding 929705794c5STakashi Sakamoto // to empty packet. 93073246fc4STakashi Sakamoto unsigned int prev_cycle = next_cycle; 93173246fc4STakashi Sakamoto 932705794c5STakashi Sakamoto next_cycle = increment_ohci_cycle_count(next_cycle, 1); 933705794c5STakashi Sakamoto lost = (next_cycle != cycle); 93473246fc4STakashi Sakamoto if (!lost) { 93573246fc4STakashi Sakamoto // Prepare a description for the skipped cycle for 93673246fc4STakashi Sakamoto // sequence replay. 93773246fc4STakashi Sakamoto desc->cycle = prev_cycle; 93873246fc4STakashi Sakamoto desc->syt = 0; 93973246fc4STakashi Sakamoto desc->data_blocks = 0; 94073246fc4STakashi Sakamoto desc->data_block_counter = dbc; 94173246fc4STakashi Sakamoto desc->ctx_payload = NULL; 94273246fc4STakashi Sakamoto ++desc; 94373246fc4STakashi Sakamoto ++(*desc_count); 94473246fc4STakashi Sakamoto } 945705794c5STakashi Sakamoto } else if (s->flags & CIP_JUMBO_PAYLOAD) { 946705794c5STakashi Sakamoto // OXFW970 skips transmission for several isoc cycles during 94773246fc4STakashi Sakamoto // asynchronous transaction. The sequence replay is impossible due 94873246fc4STakashi Sakamoto // to the reason. 949705794c5STakashi Sakamoto unsigned int safe_cycle = increment_ohci_cycle_count(next_cycle, 950705794c5STakashi Sakamoto IR_JUMBO_PAYLOAD_MAX_SKIP_CYCLES); 951705794c5STakashi Sakamoto lost = (compare_ohci_cycle_count(safe_cycle, cycle) > 0); 952705794c5STakashi Sakamoto } 953705794c5STakashi Sakamoto if (lost) { 954705794c5STakashi Sakamoto dev_err(&s->unit->device, "Detect discontinuity of cycle: %d %d\n", 955705794c5STakashi Sakamoto next_cycle, cycle); 956705794c5STakashi Sakamoto return -EIO; 957705794c5STakashi Sakamoto } 958705794c5STakashi Sakamoto } 959753e7179STakashi Sakamoto 960ebd2a647STakashi Sakamoto err = parse_ir_ctx_header(s, cycle, ctx_header, &data_blocks, &dbc, &syt, 961ebd2a647STakashi Sakamoto packet_index, i); 962753e7179STakashi Sakamoto if (err < 0) 963753e7179STakashi Sakamoto return err; 964753e7179STakashi Sakamoto 965753e7179STakashi Sakamoto desc->cycle = cycle; 966753e7179STakashi Sakamoto desc->syt = syt; 967753e7179STakashi Sakamoto desc->data_blocks = data_blocks; 968753e7179STakashi Sakamoto desc->data_block_counter = dbc; 969814b4312STakashi Sakamoto desc->ctx_payload = s->buffer.packets[packet_index].buffer; 970753e7179STakashi Sakamoto 971753e7179STakashi Sakamoto if (!(s->flags & CIP_DBC_IS_END_EVENT)) 972753e7179STakashi Sakamoto dbc = (dbc + desc->data_blocks) & 0xff; 973753e7179STakashi Sakamoto 974705794c5STakashi Sakamoto next_cycle = increment_ohci_cycle_count(next_cycle, 1); 97573246fc4STakashi Sakamoto ++(*desc_count); 976705794c5STakashi Sakamoto ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header); 977814b4312STakashi Sakamoto packet_index = (packet_index + 1) % queue_size; 978753e7179STakashi Sakamoto } 979753e7179STakashi Sakamoto 980705794c5STakashi Sakamoto s->next_cycle = next_cycle; 981753e7179STakashi Sakamoto s->data_block_counter = dbc; 982753e7179STakashi Sakamoto 983753e7179STakashi Sakamoto return 0; 984753e7179STakashi Sakamoto } 985753e7179STakashi Sakamoto 98683cfb5c5STakashi Sakamoto static unsigned int compute_syt(unsigned int syt_offset, unsigned int cycle, 98783cfb5c5STakashi Sakamoto unsigned int transfer_delay) 98883cfb5c5STakashi Sakamoto { 98983cfb5c5STakashi Sakamoto unsigned int syt; 99083cfb5c5STakashi Sakamoto 99183cfb5c5STakashi Sakamoto syt_offset += transfer_delay; 99283cfb5c5STakashi Sakamoto syt = ((cycle + syt_offset / TICKS_PER_CYCLE) << 12) | 99383cfb5c5STakashi Sakamoto (syt_offset % TICKS_PER_CYCLE); 99483cfb5c5STakashi Sakamoto return syt & CIP_SYT_MASK; 99583cfb5c5STakashi Sakamoto } 99683cfb5c5STakashi Sakamoto 9977ca7cddaSTakashi Sakamoto static void generate_pkt_descs(struct amdtp_stream *s, const __be32 *ctx_header, unsigned int packets) 998f4f6ae7bSTakashi Sakamoto { 9997ca7cddaSTakashi Sakamoto struct pkt_desc *descs = s->pkt_descs; 10007ca7cddaSTakashi Sakamoto const struct seq_desc *seq_descs = s->ctx_data.rx.seq.descs; 10017ca7cddaSTakashi Sakamoto const unsigned int seq_size = s->ctx_data.rx.seq.size; 1002f4f6ae7bSTakashi Sakamoto unsigned int dbc = s->data_block_counter; 10036f24bb8aSTakashi Sakamoto unsigned int seq_head = s->ctx_data.rx.seq.head; 10048070d265STakashi Sakamoto bool aware_syt = !(s->flags & CIP_UNAWARE_SYT); 1005f4f6ae7bSTakashi Sakamoto int i; 1006f4f6ae7bSTakashi Sakamoto 1007f4f6ae7bSTakashi Sakamoto for (i = 0; i < packets; ++i) { 1008f4f6ae7bSTakashi Sakamoto struct pkt_desc *desc = descs + i; 1009a0e02331STakashi Sakamoto unsigned int index = (s->packet_index + i) % s->queue_size; 10106f24bb8aSTakashi Sakamoto const struct seq_desc *seq = seq_descs + seq_head; 1011f4f6ae7bSTakashi Sakamoto 10123e106f4fSTakashi Sakamoto desc->cycle = compute_ohci_it_cycle(*ctx_header, s->queue_size); 101369efd5c4STakashi Sakamoto 101413d11f14STakashi Sakamoto if (aware_syt && seq->syt_offset != CIP_SYT_NO_INFO) 101513d11f14STakashi Sakamoto desc->syt = compute_syt(seq->syt_offset, desc->cycle, s->transfer_delay); 101613d11f14STakashi Sakamoto else 10178070d265STakashi Sakamoto desc->syt = CIP_SYT_NO_INFO; 10188070d265STakashi Sakamoto 101969efd5c4STakashi Sakamoto desc->data_blocks = seq->data_blocks; 1020f4f6ae7bSTakashi Sakamoto 1021f4f6ae7bSTakashi Sakamoto if (s->flags & CIP_DBC_IS_END_EVENT) 1022f4f6ae7bSTakashi Sakamoto dbc = (dbc + desc->data_blocks) & 0xff; 1023f4f6ae7bSTakashi Sakamoto 1024f4f6ae7bSTakashi Sakamoto desc->data_block_counter = dbc; 1025f4f6ae7bSTakashi Sakamoto 1026f4f6ae7bSTakashi Sakamoto if (!(s->flags & CIP_DBC_IS_END_EVENT)) 1027f4f6ae7bSTakashi Sakamoto dbc = (dbc + desc->data_blocks) & 0xff; 1028f4f6ae7bSTakashi Sakamoto 1029f4f6ae7bSTakashi Sakamoto desc->ctx_payload = s->buffer.packets[index].buffer; 1030f4f6ae7bSTakashi Sakamoto 10316f24bb8aSTakashi Sakamoto seq_head = (seq_head + 1) % seq_size; 103269efd5c4STakashi Sakamoto 1033f4f6ae7bSTakashi Sakamoto ++ctx_header; 1034f4f6ae7bSTakashi Sakamoto } 1035f4f6ae7bSTakashi Sakamoto 1036f4f6ae7bSTakashi Sakamoto s->data_block_counter = dbc; 10376f24bb8aSTakashi Sakamoto s->ctx_data.rx.seq.head = seq_head; 1038f4f6ae7bSTakashi Sakamoto } 1039f4f6ae7bSTakashi Sakamoto 1040fce9b013STakashi Sakamoto static inline void cancel_stream(struct amdtp_stream *s) 1041fce9b013STakashi Sakamoto { 1042fce9b013STakashi Sakamoto s->packet_index = -1; 10439981b20aSTakashi Sakamoto if (in_interrupt()) 1044fce9b013STakashi Sakamoto amdtp_stream_pcm_abort(s); 1045fce9b013STakashi Sakamoto WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN); 1046fce9b013STakashi Sakamoto } 1047fce9b013STakashi Sakamoto 10480f5cfcb2STakashi Sakamoto static void process_ctx_payloads(struct amdtp_stream *s, 10490f5cfcb2STakashi Sakamoto const struct pkt_desc *descs, 10500f5cfcb2STakashi Sakamoto unsigned int packets) 10510f5cfcb2STakashi Sakamoto { 10529a738ad1STakashi Sakamoto struct snd_pcm_substream *pcm; 10530f5cfcb2STakashi Sakamoto unsigned int pcm_frames; 10540f5cfcb2STakashi Sakamoto 10559a738ad1STakashi Sakamoto pcm = READ_ONCE(s->pcm); 10569a738ad1STakashi Sakamoto pcm_frames = s->process_ctx_payloads(s, descs, packets, pcm); 10579a738ad1STakashi Sakamoto if (pcm) 10580f5cfcb2STakashi Sakamoto update_pcm_pointers(s, pcm, pcm_frames); 10590f5cfcb2STakashi Sakamoto } 10600f5cfcb2STakashi Sakamoto 10619b1fcd9bSTakashi Sakamoto static void process_rx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length, 10629b1fcd9bSTakashi Sakamoto void *header, void *private_data) 1063d67c46b9STakashi Sakamoto { 1064d67c46b9STakashi Sakamoto struct amdtp_stream *s = private_data; 106569efd5c4STakashi Sakamoto const struct amdtp_domain *d = s->domain; 106626cd1e58STakashi Sakamoto const __be32 *ctx_header = header; 10679b1fcd9bSTakashi Sakamoto const unsigned int events_per_period = d->events_per_period; 106860dd4929STakashi Sakamoto unsigned int event_count = s->ctx_data.rx.event_count; 1069233dbbc7STakashi Sakamoto unsigned int pkt_header_length; 1070a0e02331STakashi Sakamoto unsigned int packets; 1071d360870aSTakashi Sakamoto bool need_hw_irq; 10720dcb4efbSTakashi Sakamoto int i; 1073d67c46b9STakashi Sakamoto 1074d67c46b9STakashi Sakamoto if (s->packet_index < 0) 1075d67c46b9STakashi Sakamoto return; 1076d67c46b9STakashi Sakamoto 1077a0e02331STakashi Sakamoto // Calculate the number of packets in buffer and check XRUN. 1078a0e02331STakashi Sakamoto packets = header_length / sizeof(*ctx_header); 1079a0e02331STakashi Sakamoto 108039c2649cSTakashi Sakamoto pool_seq_descs(s, packets); 10816f24bb8aSTakashi Sakamoto 10827ca7cddaSTakashi Sakamoto generate_pkt_descs(s, ctx_header, packets); 1083f4f6ae7bSTakashi Sakamoto 10840f5cfcb2STakashi Sakamoto process_ctx_payloads(s, s->pkt_descs, packets); 10855e2ece0fSTakashi Sakamoto 1086233dbbc7STakashi Sakamoto if (!(s->flags & CIP_NO_HEADER)) 1087233dbbc7STakashi Sakamoto pkt_header_length = IT_PKT_HEADER_SIZE_CIP; 1088233dbbc7STakashi Sakamoto else 1089233dbbc7STakashi Sakamoto pkt_header_length = 0; 1090233dbbc7STakashi Sakamoto 1091d360870aSTakashi Sakamoto if (s == d->irq_target) { 1092d360870aSTakashi Sakamoto // At NO_PERIOD_WAKEUP mode, the packets for all IT/IR contexts are processed by 1093d360870aSTakashi Sakamoto // the tasks of user process operating ALSA PCM character device by calling ioctl(2) 1094d360870aSTakashi Sakamoto // with some requests, instead of scheduled hardware IRQ of an IT context. 1095d360870aSTakashi Sakamoto struct snd_pcm_substream *pcm = READ_ONCE(s->pcm); 1096d360870aSTakashi Sakamoto need_hw_irq = !pcm || !pcm->runtime->no_period_wakeup; 1097d360870aSTakashi Sakamoto } else { 1098d360870aSTakashi Sakamoto need_hw_irq = false; 1099d360870aSTakashi Sakamoto } 1100d360870aSTakashi Sakamoto 11015e2ece0fSTakashi Sakamoto for (i = 0; i < packets; ++i) { 11025e2ece0fSTakashi Sakamoto const struct pkt_desc *desc = s->pkt_descs + i; 11036bc1a269STakashi Sakamoto struct { 11046bc1a269STakashi Sakamoto struct fw_iso_packet params; 110567d92ee7STakashi Sakamoto __be32 header[CIP_HEADER_QUADLETS]; 11066bc1a269STakashi Sakamoto } template = { {0}, {0} }; 1107e229853dSTakashi Sakamoto bool sched_irq = false; 1108860d798cSTakashi Sakamoto 1109233dbbc7STakashi Sakamoto build_it_pkt_header(s, desc->cycle, &template.params, pkt_header_length, 1110f4f6ae7bSTakashi Sakamoto desc->data_blocks, desc->data_block_counter, 11118070d265STakashi Sakamoto desc->syt, i); 11126bc1a269STakashi Sakamoto 11132472cfb3STakashi Sakamoto if (s == s->domain->irq_target) { 1114e229853dSTakashi Sakamoto event_count += desc->data_blocks; 1115e229853dSTakashi Sakamoto if (event_count >= events_per_period) { 1116e229853dSTakashi Sakamoto event_count -= events_per_period; 1117d360870aSTakashi Sakamoto sched_irq = need_hw_irq; 1118e229853dSTakashi Sakamoto } 111960dd4929STakashi Sakamoto } 1120e229853dSTakashi Sakamoto 1121e229853dSTakashi Sakamoto if (queue_out_packet(s, &template.params, sched_irq) < 0) { 1122fce9b013STakashi Sakamoto cancel_stream(s); 1123d67c46b9STakashi Sakamoto return; 1124d67c46b9STakashi Sakamoto } 1125d67c46b9STakashi Sakamoto } 1126d67c46b9STakashi Sakamoto 112760dd4929STakashi Sakamoto s->ctx_data.rx.event_count = event_count; 1128d67c46b9STakashi Sakamoto } 1129d67c46b9STakashi Sakamoto 11309b1fcd9bSTakashi Sakamoto static void skip_rx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length, 11319b1fcd9bSTakashi Sakamoto void *header, void *private_data) 11329b1fcd9bSTakashi Sakamoto { 11339b1fcd9bSTakashi Sakamoto struct amdtp_stream *s = private_data; 11349b1fcd9bSTakashi Sakamoto struct amdtp_domain *d = s->domain; 11359b1fcd9bSTakashi Sakamoto const __be32 *ctx_header = header; 11369b1fcd9bSTakashi Sakamoto unsigned int packets; 11379b1fcd9bSTakashi Sakamoto unsigned int cycle; 11389b1fcd9bSTakashi Sakamoto int i; 11399b1fcd9bSTakashi Sakamoto 11409b1fcd9bSTakashi Sakamoto if (s->packet_index < 0) 11419b1fcd9bSTakashi Sakamoto return; 11429b1fcd9bSTakashi Sakamoto 11439b1fcd9bSTakashi Sakamoto packets = header_length / sizeof(*ctx_header); 11449b1fcd9bSTakashi Sakamoto 11459b1fcd9bSTakashi Sakamoto cycle = compute_ohci_it_cycle(ctx_header[packets - 1], s->queue_size); 11469b1fcd9bSTakashi Sakamoto s->next_cycle = increment_ohci_cycle_count(cycle, 1); 11479b1fcd9bSTakashi Sakamoto 11489b1fcd9bSTakashi Sakamoto for (i = 0; i < packets; ++i) { 11499b1fcd9bSTakashi Sakamoto struct fw_iso_packet params = { 11509b1fcd9bSTakashi Sakamoto .header_length = 0, 11519b1fcd9bSTakashi Sakamoto .payload_length = 0, 11529b1fcd9bSTakashi Sakamoto }; 11539b1fcd9bSTakashi Sakamoto bool sched_irq = (s == d->irq_target && i == packets - 1); 11549b1fcd9bSTakashi Sakamoto 11559b1fcd9bSTakashi Sakamoto if (queue_out_packet(s, ¶ms, sched_irq) < 0) { 11569b1fcd9bSTakashi Sakamoto cancel_stream(s); 11579b1fcd9bSTakashi Sakamoto return; 11589b1fcd9bSTakashi Sakamoto } 11599b1fcd9bSTakashi Sakamoto } 11609b1fcd9bSTakashi Sakamoto } 11619b1fcd9bSTakashi Sakamoto 11629b1fcd9bSTakashi Sakamoto static void irq_target_callback(struct fw_iso_context *context, u32 tstamp, size_t header_length, 11639b1fcd9bSTakashi Sakamoto void *header, void *private_data); 11649b1fcd9bSTakashi Sakamoto 11659b1fcd9bSTakashi Sakamoto static void process_rx_packets_intermediately(struct fw_iso_context *context, u32 tstamp, 11669b1fcd9bSTakashi Sakamoto size_t header_length, void *header, void *private_data) 11679b1fcd9bSTakashi Sakamoto { 11689b1fcd9bSTakashi Sakamoto struct amdtp_stream *s = private_data; 11699b1fcd9bSTakashi Sakamoto struct amdtp_domain *d = s->domain; 11709b1fcd9bSTakashi Sakamoto __be32 *ctx_header = header; 11719b1fcd9bSTakashi Sakamoto const unsigned int queue_size = s->queue_size; 11729b1fcd9bSTakashi Sakamoto unsigned int packets; 11739b1fcd9bSTakashi Sakamoto unsigned int offset; 11749b1fcd9bSTakashi Sakamoto 11759b1fcd9bSTakashi Sakamoto if (s->packet_index < 0) 11769b1fcd9bSTakashi Sakamoto return; 11779b1fcd9bSTakashi Sakamoto 11789b1fcd9bSTakashi Sakamoto packets = header_length / sizeof(*ctx_header); 11799b1fcd9bSTakashi Sakamoto 11809b1fcd9bSTakashi Sakamoto offset = 0; 11819b1fcd9bSTakashi Sakamoto while (offset < packets) { 11829b1fcd9bSTakashi Sakamoto unsigned int cycle = compute_ohci_it_cycle(ctx_header[offset], queue_size); 11839b1fcd9bSTakashi Sakamoto 11849b1fcd9bSTakashi Sakamoto if (compare_ohci_cycle_count(cycle, d->processing_cycle.rx_start) >= 0) 11859b1fcd9bSTakashi Sakamoto break; 11869b1fcd9bSTakashi Sakamoto 11879b1fcd9bSTakashi Sakamoto ++offset; 11889b1fcd9bSTakashi Sakamoto } 11899b1fcd9bSTakashi Sakamoto 11909b1fcd9bSTakashi Sakamoto if (offset > 0) { 11919b1fcd9bSTakashi Sakamoto unsigned int length = sizeof(*ctx_header) * offset; 11929b1fcd9bSTakashi Sakamoto 11939b1fcd9bSTakashi Sakamoto skip_rx_packets(context, tstamp, length, ctx_header, private_data); 11949b1fcd9bSTakashi Sakamoto if (amdtp_streaming_error(s)) 11959b1fcd9bSTakashi Sakamoto return; 11969b1fcd9bSTakashi Sakamoto 11979b1fcd9bSTakashi Sakamoto ctx_header += offset; 11989b1fcd9bSTakashi Sakamoto header_length -= length; 11999b1fcd9bSTakashi Sakamoto } 12009b1fcd9bSTakashi Sakamoto 12019b1fcd9bSTakashi Sakamoto if (offset < packets) { 1202bdaedca7STakashi Sakamoto s->ready_processing = true; 1203bdaedca7STakashi Sakamoto wake_up(&s->ready_wait); 1204bdaedca7STakashi Sakamoto 12059b1fcd9bSTakashi Sakamoto process_rx_packets(context, tstamp, header_length, ctx_header, private_data); 12069b1fcd9bSTakashi Sakamoto if (amdtp_streaming_error(s)) 12079b1fcd9bSTakashi Sakamoto return; 12089b1fcd9bSTakashi Sakamoto 12099b1fcd9bSTakashi Sakamoto if (s == d->irq_target) 12109b1fcd9bSTakashi Sakamoto s->context->callback.sc = irq_target_callback; 12119b1fcd9bSTakashi Sakamoto else 12129b1fcd9bSTakashi Sakamoto s->context->callback.sc = process_rx_packets; 12139b1fcd9bSTakashi Sakamoto } 12149b1fcd9bSTakashi Sakamoto } 12159b1fcd9bSTakashi Sakamoto 1216da3623abSTakashi Sakamoto static void process_tx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length, 1217da3623abSTakashi Sakamoto void *header, void *private_data) 1218d67c46b9STakashi Sakamoto { 1219d67c46b9STakashi Sakamoto struct amdtp_stream *s = private_data; 1220cc4f8e91STakashi Sakamoto __be32 *ctx_header = header; 1221e229853dSTakashi Sakamoto unsigned int packets; 122273246fc4STakashi Sakamoto unsigned int desc_count; 1223753e7179STakashi Sakamoto int i; 1224753e7179STakashi Sakamoto int err; 1225d67c46b9STakashi Sakamoto 1226d67c46b9STakashi Sakamoto if (s->packet_index < 0) 1227d67c46b9STakashi Sakamoto return; 1228d67c46b9STakashi Sakamoto 1229a0e02331STakashi Sakamoto // Calculate the number of packets in buffer and check XRUN. 1230d3d10a4aSTakashi Sakamoto packets = header_length / s->ctx_data.tx.ctx_header_size; 1231f90e2dedSTakashi Sakamoto 123273246fc4STakashi Sakamoto desc_count = 0; 123373246fc4STakashi Sakamoto err = generate_device_pkt_descs(s, s->pkt_descs, ctx_header, packets, &desc_count); 1234753e7179STakashi Sakamoto if (err < 0) { 1235753e7179STakashi Sakamoto if (err != -EAGAIN) { 1236753e7179STakashi Sakamoto cancel_stream(s); 1237753e7179STakashi Sakamoto return; 1238753e7179STakashi Sakamoto } 12395e2ece0fSTakashi Sakamoto } else { 1240f9e5ecdfSTakashi Sakamoto struct amdtp_domain *d = s->domain; 1241f9e5ecdfSTakashi Sakamoto 124273246fc4STakashi Sakamoto process_ctx_payloads(s, s->pkt_descs, desc_count); 1243f9e5ecdfSTakashi Sakamoto 1244f9e5ecdfSTakashi Sakamoto if (d->replay.enable) 1245f9e5ecdfSTakashi Sakamoto cache_seq(s, s->pkt_descs, desc_count); 12465e2ece0fSTakashi Sakamoto } 12475e2ece0fSTakashi Sakamoto 12485e2ece0fSTakashi Sakamoto for (i = 0; i < packets; ++i) { 12495e2ece0fSTakashi Sakamoto struct fw_iso_packet params = {0}; 1250a35463d1STakashi Sakamoto 125160dd4929STakashi Sakamoto if (queue_in_packet(s, ¶ms) < 0) { 1252753e7179STakashi Sakamoto cancel_stream(s); 1253753e7179STakashi Sakamoto return; 1254753e7179STakashi Sakamoto } 1255d67c46b9STakashi Sakamoto } 1256d67c46b9STakashi Sakamoto } 1257d67c46b9STakashi Sakamoto 1258da3623abSTakashi Sakamoto static void drop_tx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length, 1259da3623abSTakashi Sakamoto void *header, void *private_data) 1260da3623abSTakashi Sakamoto { 1261da3623abSTakashi Sakamoto struct amdtp_stream *s = private_data; 1262da3623abSTakashi Sakamoto const __be32 *ctx_header = header; 1263da3623abSTakashi Sakamoto unsigned int packets; 1264da3623abSTakashi Sakamoto unsigned int cycle; 1265da3623abSTakashi Sakamoto int i; 1266da3623abSTakashi Sakamoto 1267da3623abSTakashi Sakamoto if (s->packet_index < 0) 1268da3623abSTakashi Sakamoto return; 1269da3623abSTakashi Sakamoto 1270da3623abSTakashi Sakamoto packets = header_length / s->ctx_data.tx.ctx_header_size; 1271da3623abSTakashi Sakamoto 1272da3623abSTakashi Sakamoto ctx_header += (packets - 1) * s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header); 1273da3623abSTakashi Sakamoto cycle = compute_ohci_cycle_count(ctx_header[1]); 1274da3623abSTakashi Sakamoto s->next_cycle = increment_ohci_cycle_count(cycle, 1); 1275da3623abSTakashi Sakamoto 1276da3623abSTakashi Sakamoto for (i = 0; i < packets; ++i) { 1277da3623abSTakashi Sakamoto struct fw_iso_packet params = {0}; 1278da3623abSTakashi Sakamoto 1279da3623abSTakashi Sakamoto if (queue_in_packet(s, ¶ms) < 0) { 1280da3623abSTakashi Sakamoto cancel_stream(s); 1281da3623abSTakashi Sakamoto return; 1282da3623abSTakashi Sakamoto } 1283da3623abSTakashi Sakamoto } 1284da3623abSTakashi Sakamoto } 1285da3623abSTakashi Sakamoto 1286da3623abSTakashi Sakamoto static void process_tx_packets_intermediately(struct fw_iso_context *context, u32 tstamp, 1287da3623abSTakashi Sakamoto size_t header_length, void *header, void *private_data) 1288da3623abSTakashi Sakamoto { 1289da3623abSTakashi Sakamoto struct amdtp_stream *s = private_data; 1290da3623abSTakashi Sakamoto struct amdtp_domain *d = s->domain; 1291da3623abSTakashi Sakamoto __be32 *ctx_header; 1292da3623abSTakashi Sakamoto unsigned int packets; 1293da3623abSTakashi Sakamoto unsigned int offset; 1294da3623abSTakashi Sakamoto 1295da3623abSTakashi Sakamoto if (s->packet_index < 0) 1296da3623abSTakashi Sakamoto return; 1297da3623abSTakashi Sakamoto 1298da3623abSTakashi Sakamoto packets = header_length / s->ctx_data.tx.ctx_header_size; 1299da3623abSTakashi Sakamoto 1300da3623abSTakashi Sakamoto offset = 0; 1301da3623abSTakashi Sakamoto ctx_header = header; 1302da3623abSTakashi Sakamoto while (offset < packets) { 1303da3623abSTakashi Sakamoto unsigned int cycle = compute_ohci_cycle_count(ctx_header[1]); 1304da3623abSTakashi Sakamoto 1305da3623abSTakashi Sakamoto if (compare_ohci_cycle_count(cycle, d->processing_cycle.tx_start) >= 0) 1306da3623abSTakashi Sakamoto break; 1307da3623abSTakashi Sakamoto 1308da3623abSTakashi Sakamoto ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(__be32); 1309da3623abSTakashi Sakamoto ++offset; 1310da3623abSTakashi Sakamoto } 1311da3623abSTakashi Sakamoto 1312da3623abSTakashi Sakamoto ctx_header = header; 1313da3623abSTakashi Sakamoto 1314da3623abSTakashi Sakamoto if (offset > 0) { 1315da3623abSTakashi Sakamoto size_t length = s->ctx_data.tx.ctx_header_size * offset; 1316da3623abSTakashi Sakamoto 1317da3623abSTakashi Sakamoto drop_tx_packets(context, tstamp, length, ctx_header, s); 1318da3623abSTakashi Sakamoto if (amdtp_streaming_error(s)) 1319da3623abSTakashi Sakamoto return; 1320da3623abSTakashi Sakamoto 1321da3623abSTakashi Sakamoto ctx_header += length / sizeof(*ctx_header); 1322da3623abSTakashi Sakamoto header_length -= length; 1323da3623abSTakashi Sakamoto } 1324da3623abSTakashi Sakamoto 1325da3623abSTakashi Sakamoto if (offset < packets) { 1326bdaedca7STakashi Sakamoto s->ready_processing = true; 1327bdaedca7STakashi Sakamoto wake_up(&s->ready_wait); 1328bdaedca7STakashi Sakamoto 1329da3623abSTakashi Sakamoto process_tx_packets(context, tstamp, header_length, ctx_header, s); 1330da3623abSTakashi Sakamoto if (amdtp_streaming_error(s)) 1331da3623abSTakashi Sakamoto return; 1332da3623abSTakashi Sakamoto 1333da3623abSTakashi Sakamoto context->callback.sc = process_tx_packets; 1334da3623abSTakashi Sakamoto } 1335da3623abSTakashi Sakamoto } 1336da3623abSTakashi Sakamoto 1337fb25dcc8STakashi Sakamoto static void drop_tx_packets_initially(struct fw_iso_context *context, u32 tstamp, 1338fb25dcc8STakashi Sakamoto size_t header_length, void *header, void *private_data) 1339fb25dcc8STakashi Sakamoto { 1340fb25dcc8STakashi Sakamoto struct amdtp_stream *s = private_data; 1341fb25dcc8STakashi Sakamoto struct amdtp_domain *d = s->domain; 1342fb25dcc8STakashi Sakamoto __be32 *ctx_header; 1343fb25dcc8STakashi Sakamoto unsigned int count; 1344fb25dcc8STakashi Sakamoto unsigned int events; 1345fb25dcc8STakashi Sakamoto int i; 1346fb25dcc8STakashi Sakamoto 1347fb25dcc8STakashi Sakamoto if (s->packet_index < 0) 1348fb25dcc8STakashi Sakamoto return; 1349fb25dcc8STakashi Sakamoto 1350fb25dcc8STakashi Sakamoto count = header_length / s->ctx_data.tx.ctx_header_size; 1351fb25dcc8STakashi Sakamoto 1352fb25dcc8STakashi Sakamoto // Attempt to detect any event in the batch of packets. 1353fb25dcc8STakashi Sakamoto events = 0; 1354fb25dcc8STakashi Sakamoto ctx_header = header; 1355fb25dcc8STakashi Sakamoto for (i = 0; i < count; ++i) { 1356fb25dcc8STakashi Sakamoto unsigned int payload_quads = 1357fb25dcc8STakashi Sakamoto (be32_to_cpu(*ctx_header) >> ISO_DATA_LENGTH_SHIFT) / sizeof(__be32); 1358fb25dcc8STakashi Sakamoto unsigned int data_blocks; 1359fb25dcc8STakashi Sakamoto 1360fb25dcc8STakashi Sakamoto if (s->flags & CIP_NO_HEADER) { 1361fb25dcc8STakashi Sakamoto data_blocks = payload_quads / s->data_block_quadlets; 1362fb25dcc8STakashi Sakamoto } else { 1363fb25dcc8STakashi Sakamoto __be32 *cip_headers = ctx_header + IR_CTX_HEADER_DEFAULT_QUADLETS; 1364fb25dcc8STakashi Sakamoto 1365fb25dcc8STakashi Sakamoto if (payload_quads < CIP_HEADER_QUADLETS) { 1366fb25dcc8STakashi Sakamoto data_blocks = 0; 1367fb25dcc8STakashi Sakamoto } else { 1368fb25dcc8STakashi Sakamoto payload_quads -= CIP_HEADER_QUADLETS; 1369fb25dcc8STakashi Sakamoto 1370fb25dcc8STakashi Sakamoto if (s->flags & CIP_UNAWARE_SYT) { 1371fb25dcc8STakashi Sakamoto data_blocks = payload_quads / s->data_block_quadlets; 1372fb25dcc8STakashi Sakamoto } else { 1373fb25dcc8STakashi Sakamoto u32 cip1 = be32_to_cpu(cip_headers[1]); 1374fb25dcc8STakashi Sakamoto 1375fb25dcc8STakashi Sakamoto // NODATA packet can includes any data blocks but they are 1376fb25dcc8STakashi Sakamoto // not available as event. 1377fb25dcc8STakashi Sakamoto if ((cip1 & CIP_NO_DATA) == CIP_NO_DATA) 1378fb25dcc8STakashi Sakamoto data_blocks = 0; 1379fb25dcc8STakashi Sakamoto else 1380fb25dcc8STakashi Sakamoto data_blocks = payload_quads / s->data_block_quadlets; 1381fb25dcc8STakashi Sakamoto } 1382fb25dcc8STakashi Sakamoto } 1383fb25dcc8STakashi Sakamoto } 1384fb25dcc8STakashi Sakamoto 1385fb25dcc8STakashi Sakamoto events += data_blocks; 1386fb25dcc8STakashi Sakamoto 1387fb25dcc8STakashi Sakamoto ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(__be32); 1388fb25dcc8STakashi Sakamoto } 1389fb25dcc8STakashi Sakamoto 1390fb25dcc8STakashi Sakamoto drop_tx_packets(context, tstamp, header_length, header, s); 1391fb25dcc8STakashi Sakamoto 1392fb25dcc8STakashi Sakamoto if (events > 0) 1393fb25dcc8STakashi Sakamoto s->ctx_data.tx.event_starts = true; 1394fb25dcc8STakashi Sakamoto 1395fb25dcc8STakashi Sakamoto // Decide the cycle count to begin processing content of packet in IR contexts. 1396fb25dcc8STakashi Sakamoto { 1397fb25dcc8STakashi Sakamoto unsigned int stream_count = 0; 1398fb25dcc8STakashi Sakamoto unsigned int event_starts_count = 0; 1399fb25dcc8STakashi Sakamoto unsigned int cycle = UINT_MAX; 1400fb25dcc8STakashi Sakamoto 1401fb25dcc8STakashi Sakamoto list_for_each_entry(s, &d->streams, list) { 1402fb25dcc8STakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 1403fb25dcc8STakashi Sakamoto ++stream_count; 1404fb25dcc8STakashi Sakamoto if (s->ctx_data.tx.event_starts) 1405fb25dcc8STakashi Sakamoto ++event_starts_count; 1406fb25dcc8STakashi Sakamoto } 1407fb25dcc8STakashi Sakamoto } 1408fb25dcc8STakashi Sakamoto 1409fb25dcc8STakashi Sakamoto if (stream_count == event_starts_count) { 1410fb25dcc8STakashi Sakamoto unsigned int next_cycle; 1411fb25dcc8STakashi Sakamoto 1412fb25dcc8STakashi Sakamoto list_for_each_entry(s, &d->streams, list) { 1413fb25dcc8STakashi Sakamoto if (s->direction != AMDTP_IN_STREAM) 1414fb25dcc8STakashi Sakamoto continue; 1415fb25dcc8STakashi Sakamoto 1416fb25dcc8STakashi Sakamoto next_cycle = increment_ohci_cycle_count(s->next_cycle, 1417fb25dcc8STakashi Sakamoto d->processing_cycle.tx_init_skip); 1418fb25dcc8STakashi Sakamoto if (cycle == UINT_MAX || 1419fb25dcc8STakashi Sakamoto compare_ohci_cycle_count(next_cycle, cycle) > 0) 1420fb25dcc8STakashi Sakamoto cycle = next_cycle; 1421fb25dcc8STakashi Sakamoto 1422fb25dcc8STakashi Sakamoto s->context->callback.sc = process_tx_packets_intermediately; 1423fb25dcc8STakashi Sakamoto } 1424fb25dcc8STakashi Sakamoto 1425fb25dcc8STakashi Sakamoto d->processing_cycle.tx_start = cycle; 1426fb25dcc8STakashi Sakamoto } 1427fb25dcc8STakashi Sakamoto } 1428fb25dcc8STakashi Sakamoto } 1429fb25dcc8STakashi Sakamoto 14309b1fcd9bSTakashi Sakamoto static void process_ctxs_in_domain(struct amdtp_domain *d) 143160dd4929STakashi Sakamoto { 143260dd4929STakashi Sakamoto struct amdtp_stream *s; 143360dd4929STakashi Sakamoto 143460dd4929STakashi Sakamoto list_for_each_entry(s, &d->streams, list) { 14359b1fcd9bSTakashi Sakamoto if (s != d->irq_target && amdtp_stream_running(s)) 143660dd4929STakashi Sakamoto fw_iso_context_flush_completions(s->context); 14379b1fcd9bSTakashi Sakamoto 143860dd4929STakashi Sakamoto if (amdtp_streaming_error(s)) 143960dd4929STakashi Sakamoto goto error; 144060dd4929STakashi Sakamoto } 144160dd4929STakashi Sakamoto 144260dd4929STakashi Sakamoto return; 144360dd4929STakashi Sakamoto error: 14449b1fcd9bSTakashi Sakamoto if (amdtp_stream_running(d->irq_target)) 14459b1fcd9bSTakashi Sakamoto cancel_stream(d->irq_target); 144660dd4929STakashi Sakamoto 144760dd4929STakashi Sakamoto list_for_each_entry(s, &d->streams, list) { 144860dd4929STakashi Sakamoto if (amdtp_stream_running(s)) 144960dd4929STakashi Sakamoto cancel_stream(s); 145060dd4929STakashi Sakamoto } 145160dd4929STakashi Sakamoto } 145260dd4929STakashi Sakamoto 14539b1fcd9bSTakashi Sakamoto static void irq_target_callback(struct fw_iso_context *context, u32 tstamp, size_t header_length, 14549b1fcd9bSTakashi Sakamoto void *header, void *private_data) 14559b1fcd9bSTakashi Sakamoto { 14569b1fcd9bSTakashi Sakamoto struct amdtp_stream *s = private_data; 14579b1fcd9bSTakashi Sakamoto struct amdtp_domain *d = s->domain; 14589b1fcd9bSTakashi Sakamoto 14599b1fcd9bSTakashi Sakamoto process_rx_packets(context, tstamp, header_length, header, private_data); 14609b1fcd9bSTakashi Sakamoto process_ctxs_in_domain(d); 14619b1fcd9bSTakashi Sakamoto } 14629b1fcd9bSTakashi Sakamoto 14639b1fcd9bSTakashi Sakamoto static void irq_target_callback_intermediately(struct fw_iso_context *context, u32 tstamp, 14649b1fcd9bSTakashi Sakamoto size_t header_length, void *header, void *private_data) 14659b1fcd9bSTakashi Sakamoto { 14669b1fcd9bSTakashi Sakamoto struct amdtp_stream *s = private_data; 14679b1fcd9bSTakashi Sakamoto struct amdtp_domain *d = s->domain; 14689b1fcd9bSTakashi Sakamoto 14699b1fcd9bSTakashi Sakamoto process_rx_packets_intermediately(context, tstamp, header_length, header, private_data); 14709b1fcd9bSTakashi Sakamoto process_ctxs_in_domain(d); 14719b1fcd9bSTakashi Sakamoto } 14729b1fcd9bSTakashi Sakamoto 14739b1fcd9bSTakashi Sakamoto static void irq_target_callback_skip(struct fw_iso_context *context, u32 tstamp, 14749b1fcd9bSTakashi Sakamoto size_t header_length, void *header, void *private_data) 14759b1fcd9bSTakashi Sakamoto { 14769b1fcd9bSTakashi Sakamoto struct amdtp_stream *s = private_data; 14779b1fcd9bSTakashi Sakamoto struct amdtp_domain *d = s->domain; 147839c2649cSTakashi Sakamoto bool ready_to_start; 14799b1fcd9bSTakashi Sakamoto 14809b1fcd9bSTakashi Sakamoto skip_rx_packets(context, tstamp, header_length, header, private_data); 14819b1fcd9bSTakashi Sakamoto process_ctxs_in_domain(d); 14829b1fcd9bSTakashi Sakamoto 14832f21a177STakashi Sakamoto if (d->replay.enable && !d->replay.on_the_fly) { 148439c2649cSTakashi Sakamoto unsigned int rx_count = 0; 148539c2649cSTakashi Sakamoto unsigned int rx_ready_count = 0; 148639c2649cSTakashi Sakamoto struct amdtp_stream *rx; 148739c2649cSTakashi Sakamoto 148839c2649cSTakashi Sakamoto list_for_each_entry(rx, &d->streams, list) { 148939c2649cSTakashi Sakamoto struct amdtp_stream *tx; 149039c2649cSTakashi Sakamoto unsigned int cached_cycles; 149139c2649cSTakashi Sakamoto 149239c2649cSTakashi Sakamoto if (rx->direction != AMDTP_OUT_STREAM) 149339c2649cSTakashi Sakamoto continue; 149439c2649cSTakashi Sakamoto ++rx_count; 149539c2649cSTakashi Sakamoto 149639c2649cSTakashi Sakamoto tx = rx->ctx_data.rx.replay_target; 149739c2649cSTakashi Sakamoto cached_cycles = calculate_cached_cycle_count(tx, 0); 149839c2649cSTakashi Sakamoto if (cached_cycles > tx->ctx_data.tx.cache.size / 2) 149939c2649cSTakashi Sakamoto ++rx_ready_count; 150039c2649cSTakashi Sakamoto } 150139c2649cSTakashi Sakamoto 150239c2649cSTakashi Sakamoto ready_to_start = (rx_count == rx_ready_count); 150339c2649cSTakashi Sakamoto } else { 150439c2649cSTakashi Sakamoto ready_to_start = true; 150539c2649cSTakashi Sakamoto } 150639c2649cSTakashi Sakamoto 15079b1fcd9bSTakashi Sakamoto // Decide the cycle count to begin processing content of packet in IT contexts. All of IT 15089b1fcd9bSTakashi Sakamoto // contexts are expected to start and get callback when reaching here. 150939c2649cSTakashi Sakamoto if (ready_to_start) { 151039c2649cSTakashi Sakamoto unsigned int cycle = s->next_cycle; 15119b1fcd9bSTakashi Sakamoto list_for_each_entry(s, &d->streams, list) { 15129b1fcd9bSTakashi Sakamoto if (s->direction != AMDTP_OUT_STREAM) 15139b1fcd9bSTakashi Sakamoto continue; 15149b1fcd9bSTakashi Sakamoto 15159b1fcd9bSTakashi Sakamoto if (compare_ohci_cycle_count(s->next_cycle, cycle) > 0) 15169b1fcd9bSTakashi Sakamoto cycle = s->next_cycle; 15179b1fcd9bSTakashi Sakamoto 15189b1fcd9bSTakashi Sakamoto if (s == d->irq_target) 15199b1fcd9bSTakashi Sakamoto s->context->callback.sc = irq_target_callback_intermediately; 15209b1fcd9bSTakashi Sakamoto else 15219b1fcd9bSTakashi Sakamoto s->context->callback.sc = process_rx_packets_intermediately; 15229b1fcd9bSTakashi Sakamoto } 15239b1fcd9bSTakashi Sakamoto 15249b1fcd9bSTakashi Sakamoto d->processing_cycle.rx_start = cycle; 15259b1fcd9bSTakashi Sakamoto } 152639c2649cSTakashi Sakamoto } 15279b1fcd9bSTakashi Sakamoto 1528b7c7699bSTakashi Sakamoto // This is executed one time. For in-stream, first packet has come. For out-stream, prepared to 1529b7c7699bSTakashi Sakamoto // transmit first packet. 1530d67c46b9STakashi Sakamoto static void amdtp_stream_first_callback(struct fw_iso_context *context, 153173fc7f08STakashi Sakamoto u32 tstamp, size_t header_length, 1532d67c46b9STakashi Sakamoto void *header, void *private_data) 1533d67c46b9STakashi Sakamoto { 1534d67c46b9STakashi Sakamoto struct amdtp_stream *s = private_data; 1535da3623abSTakashi Sakamoto struct amdtp_domain *d = s->domain; 1536d67c46b9STakashi Sakamoto 1537cc4f8e91STakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 1538fb25dcc8STakashi Sakamoto context->callback.sc = drop_tx_packets_initially; 1539a04513f8STakashi Sakamoto } else { 1540da3623abSTakashi Sakamoto if (s == d->irq_target) 15419b1fcd9bSTakashi Sakamoto context->callback.sc = irq_target_callback_skip; 15422472cfb3STakashi Sakamoto else 15439b1fcd9bSTakashi Sakamoto context->callback.sc = skip_rx_packets; 1544a04513f8STakashi Sakamoto } 1545a04513f8STakashi Sakamoto 154673fc7f08STakashi Sakamoto context->callback.sc(context, tstamp, header_length, header, s); 1547d67c46b9STakashi Sakamoto } 1548d67c46b9STakashi Sakamoto 1549d67c46b9STakashi Sakamoto /** 1550d67c46b9STakashi Sakamoto * amdtp_stream_start - start transferring packets 1551d67c46b9STakashi Sakamoto * @s: the AMDTP stream to start 1552d67c46b9STakashi Sakamoto * @channel: the isochronous channel on the bus 1553d67c46b9STakashi Sakamoto * @speed: firewire speed code 1554af86b0b1STakashi Sakamoto * @queue_size: The number of packets in the queue. 1555af86b0b1STakashi Sakamoto * @idle_irq_interval: the interval to queue packet during initial state. 1556d67c46b9STakashi Sakamoto * 1557d67c46b9STakashi Sakamoto * The stream cannot be started until it has been configured with 1558d67c46b9STakashi Sakamoto * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI 1559d67c46b9STakashi Sakamoto * device can be started. 1560d67c46b9STakashi Sakamoto */ 1561a0e02331STakashi Sakamoto static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed, 1562bd165079STakashi Sakamoto unsigned int queue_size, unsigned int idle_irq_interval) 1563d67c46b9STakashi Sakamoto { 15642472cfb3STakashi Sakamoto bool is_irq_target = (s == s->domain->irq_target); 1565d3d10a4aSTakashi Sakamoto unsigned int ctx_header_size; 1566f11453c7STakashi Sakamoto unsigned int max_ctx_payload_size; 1567d67c46b9STakashi Sakamoto enum dma_data_direction dir; 1568d67c46b9STakashi Sakamoto int type, tag, err; 1569d67c46b9STakashi Sakamoto 1570d67c46b9STakashi Sakamoto mutex_lock(&s->mutex); 1571d67c46b9STakashi Sakamoto 1572d67c46b9STakashi Sakamoto if (WARN_ON(amdtp_stream_running(s) || 1573d67c46b9STakashi Sakamoto (s->data_block_quadlets < 1))) { 1574d67c46b9STakashi Sakamoto err = -EBADFD; 1575d67c46b9STakashi Sakamoto goto err_unlock; 1576d67c46b9STakashi Sakamoto } 1577d67c46b9STakashi Sakamoto 1578d3d10a4aSTakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 157960dd4929STakashi Sakamoto // NOTE: IT context should be used for constant IRQ. 158060dd4929STakashi Sakamoto if (is_irq_target) { 158160dd4929STakashi Sakamoto err = -EINVAL; 158260dd4929STakashi Sakamoto goto err_unlock; 158360dd4929STakashi Sakamoto } 158460dd4929STakashi Sakamoto 1585d67c46b9STakashi Sakamoto s->data_block_counter = UINT_MAX; 1586d3d10a4aSTakashi Sakamoto } else { 1587d67c46b9STakashi Sakamoto s->data_block_counter = 0; 1588d3d10a4aSTakashi Sakamoto } 1589d67c46b9STakashi Sakamoto 15901be4f21dSTakashi Sakamoto // initialize packet buffer. 1591d67c46b9STakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 1592d67c46b9STakashi Sakamoto dir = DMA_FROM_DEVICE; 1593d67c46b9STakashi Sakamoto type = FW_ISO_CONTEXT_RECEIVE; 1594c75f3678STakashi Sakamoto if (!(s->flags & CIP_NO_HEADER)) 1595f11453c7STakashi Sakamoto ctx_header_size = IR_CTX_HEADER_SIZE_CIP; 1596c75f3678STakashi Sakamoto else 1597f11453c7STakashi Sakamoto ctx_header_size = IR_CTX_HEADER_SIZE_NO_CIP; 1598d67c46b9STakashi Sakamoto } else { 1599d67c46b9STakashi Sakamoto dir = DMA_TO_DEVICE; 1600d67c46b9STakashi Sakamoto type = FW_ISO_CONTEXT_TRANSMIT; 1601df9160b9STakashi Sakamoto ctx_header_size = 0; // No effect for IT context. 1602b18f0cfaSTakashi Sakamoto } 1603c75f3678STakashi Sakamoto max_ctx_payload_size = amdtp_stream_get_max_ctx_payload_size(s); 1604f11453c7STakashi Sakamoto 1605c75f3678STakashi Sakamoto err = iso_packets_buffer_init(&s->buffer, s->unit, queue_size, max_ctx_payload_size, dir); 1606d67c46b9STakashi Sakamoto if (err < 0) 1607d67c46b9STakashi Sakamoto goto err_unlock; 1608af86b0b1STakashi Sakamoto s->queue_size = queue_size; 160960dd4929STakashi Sakamoto 1610d67c46b9STakashi Sakamoto s->context = fw_iso_context_create(fw_parent_device(s->unit)->card, 1611d3d10a4aSTakashi Sakamoto type, channel, speed, ctx_header_size, 16122472cfb3STakashi Sakamoto amdtp_stream_first_callback, s); 1613d67c46b9STakashi Sakamoto if (IS_ERR(s->context)) { 1614d67c46b9STakashi Sakamoto err = PTR_ERR(s->context); 1615d67c46b9STakashi Sakamoto if (err == -EBUSY) 1616d67c46b9STakashi Sakamoto dev_err(&s->unit->device, 1617d67c46b9STakashi Sakamoto "no free stream on this controller\n"); 1618d67c46b9STakashi Sakamoto goto err_buffer; 1619d67c46b9STakashi Sakamoto } 1620d67c46b9STakashi Sakamoto 1621d67c46b9STakashi Sakamoto amdtp_stream_update(s); 1622d67c46b9STakashi Sakamoto 1623d3d10a4aSTakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 1624f11453c7STakashi Sakamoto s->ctx_data.tx.max_ctx_payload_length = max_ctx_payload_size; 1625d3d10a4aSTakashi Sakamoto s->ctx_data.tx.ctx_header_size = ctx_header_size; 1626fb25dcc8STakashi Sakamoto s->ctx_data.tx.event_starts = false; 1627f9e5ecdfSTakashi Sakamoto 1628f9e5ecdfSTakashi Sakamoto if (s->domain->replay.enable) { 1629f9e5ecdfSTakashi Sakamoto // struct fw_iso_context.drop_overflow_headers is false therefore it's 1630f9e5ecdfSTakashi Sakamoto // possible to cache much unexpectedly. 1631f9e5ecdfSTakashi Sakamoto s->ctx_data.tx.cache.size = max_t(unsigned int, s->syt_interval * 2, 1632f9e5ecdfSTakashi Sakamoto queue_size * 3 / 2); 1633f9e5ecdfSTakashi Sakamoto s->ctx_data.tx.cache.tail = 0; 1634f9e5ecdfSTakashi Sakamoto s->ctx_data.tx.cache.descs = kcalloc(s->ctx_data.tx.cache.size, 1635f9e5ecdfSTakashi Sakamoto sizeof(*s->ctx_data.tx.cache.descs), GFP_KERNEL); 16368b6e2193SDan Carpenter if (!s->ctx_data.tx.cache.descs) { 16378b6e2193SDan Carpenter err = -ENOMEM; 1638f9e5ecdfSTakashi Sakamoto goto err_context; 1639f9e5ecdfSTakashi Sakamoto } 16408b6e2193SDan Carpenter } 1641bd165079STakashi Sakamoto } else { 16426f24bb8aSTakashi Sakamoto static const struct { 16436f24bb8aSTakashi Sakamoto unsigned int data_block; 16446f24bb8aSTakashi Sakamoto unsigned int syt_offset; 16456f24bb8aSTakashi Sakamoto } *entry, initial_state[] = { 16466f24bb8aSTakashi Sakamoto [CIP_SFC_32000] = { 4, 3072 }, 16476f24bb8aSTakashi Sakamoto [CIP_SFC_48000] = { 6, 1024 }, 16486f24bb8aSTakashi Sakamoto [CIP_SFC_96000] = { 12, 1024 }, 16496f24bb8aSTakashi Sakamoto [CIP_SFC_192000] = { 24, 1024 }, 16506f24bb8aSTakashi Sakamoto [CIP_SFC_44100] = { 0, 67 }, 16516f24bb8aSTakashi Sakamoto [CIP_SFC_88200] = { 0, 67 }, 16526f24bb8aSTakashi Sakamoto [CIP_SFC_176400] = { 0, 67 }, 16536f24bb8aSTakashi Sakamoto }; 16546f24bb8aSTakashi Sakamoto 16556f24bb8aSTakashi Sakamoto s->ctx_data.rx.seq.descs = kcalloc(queue_size, sizeof(*s->ctx_data.rx.seq.descs), GFP_KERNEL); 16568b6e2193SDan Carpenter if (!s->ctx_data.rx.seq.descs) { 16578b6e2193SDan Carpenter err = -ENOMEM; 16586f24bb8aSTakashi Sakamoto goto err_context; 16598b6e2193SDan Carpenter } 16606f24bb8aSTakashi Sakamoto s->ctx_data.rx.seq.size = queue_size; 16616f24bb8aSTakashi Sakamoto s->ctx_data.rx.seq.tail = 0; 16626f24bb8aSTakashi Sakamoto s->ctx_data.rx.seq.head = 0; 16636f24bb8aSTakashi Sakamoto 16646f24bb8aSTakashi Sakamoto entry = &initial_state[s->sfc]; 16656f24bb8aSTakashi Sakamoto s->ctx_data.rx.data_block_state = entry->data_block; 16666f24bb8aSTakashi Sakamoto s->ctx_data.rx.syt_offset_state = entry->syt_offset; 16676f24bb8aSTakashi Sakamoto s->ctx_data.rx.last_syt_offset = TICKS_PER_CYCLE; 16686f24bb8aSTakashi Sakamoto 1669bd165079STakashi Sakamoto s->ctx_data.rx.event_count = 0; 1670d3d10a4aSTakashi Sakamoto } 167152759c09STakashi Sakamoto 16723b196c39STakashi Sakamoto if (s->flags & CIP_NO_HEADER) 16733b196c39STakashi Sakamoto s->tag = TAG_NO_CIP_HEADER; 16743b196c39STakashi Sakamoto else 16753b196c39STakashi Sakamoto s->tag = TAG_CIP; 16763b196c39STakashi Sakamoto 1677a0e02331STakashi Sakamoto s->pkt_descs = kcalloc(s->queue_size, sizeof(*s->pkt_descs), 167804130cf8STakashi Sakamoto GFP_KERNEL); 167904130cf8STakashi Sakamoto if (!s->pkt_descs) { 168004130cf8STakashi Sakamoto err = -ENOMEM; 168104130cf8STakashi Sakamoto goto err_context; 168204130cf8STakashi Sakamoto } 168304130cf8STakashi Sakamoto 1684d67c46b9STakashi Sakamoto s->packet_index = 0; 1685d67c46b9STakashi Sakamoto do { 16866007bf54STakashi Sakamoto struct fw_iso_packet params; 1687e229853dSTakashi Sakamoto 1688b18f0cfaSTakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 168960dd4929STakashi Sakamoto err = queue_in_packet(s, ¶ms); 1690b18f0cfaSTakashi Sakamoto } else { 169160dd4929STakashi Sakamoto bool sched_irq = false; 169260dd4929STakashi Sakamoto 1693b18f0cfaSTakashi Sakamoto params.header_length = 0; 1694b18f0cfaSTakashi Sakamoto params.payload_length = 0; 169560dd4929STakashi Sakamoto 169660dd4929STakashi Sakamoto if (is_irq_target) { 169760dd4929STakashi Sakamoto sched_irq = !((s->packet_index + 1) % 169860dd4929STakashi Sakamoto idle_irq_interval); 169960dd4929STakashi Sakamoto } 170060dd4929STakashi Sakamoto 1701e229853dSTakashi Sakamoto err = queue_out_packet(s, ¶ms, sched_irq); 1702b18f0cfaSTakashi Sakamoto } 1703d67c46b9STakashi Sakamoto if (err < 0) 170404130cf8STakashi Sakamoto goto err_pkt_descs; 1705d67c46b9STakashi Sakamoto } while (s->packet_index > 0); 1706d67c46b9STakashi Sakamoto 1707d67c46b9STakashi Sakamoto /* NOTE: TAG1 matches CIP. This just affects in stream. */ 1708d67c46b9STakashi Sakamoto tag = FW_ISO_CONTEXT_MATCH_TAG1; 17093b196c39STakashi Sakamoto if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER)) 1710d67c46b9STakashi Sakamoto tag |= FW_ISO_CONTEXT_MATCH_TAG0; 1711d67c46b9STakashi Sakamoto 1712bdaedca7STakashi Sakamoto s->ready_processing = false; 1713bd165079STakashi Sakamoto err = fw_iso_context_start(s->context, -1, 0, tag); 1714d67c46b9STakashi Sakamoto if (err < 0) 171504130cf8STakashi Sakamoto goto err_pkt_descs; 1716d67c46b9STakashi Sakamoto 1717d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 1718d67c46b9STakashi Sakamoto 1719d67c46b9STakashi Sakamoto return 0; 172004130cf8STakashi Sakamoto err_pkt_descs: 172104130cf8STakashi Sakamoto kfree(s->pkt_descs); 1722d67c46b9STakashi Sakamoto err_context: 1723f9e5ecdfSTakashi Sakamoto if (s->direction == AMDTP_OUT_STREAM) { 17246f24bb8aSTakashi Sakamoto kfree(s->ctx_data.rx.seq.descs); 1725f9e5ecdfSTakashi Sakamoto } else { 1726f9e5ecdfSTakashi Sakamoto if (s->domain->replay.enable) 1727f9e5ecdfSTakashi Sakamoto kfree(s->ctx_data.tx.cache.descs); 1728f9e5ecdfSTakashi Sakamoto } 1729d67c46b9STakashi Sakamoto fw_iso_context_destroy(s->context); 1730d67c46b9STakashi Sakamoto s->context = ERR_PTR(-1); 1731d67c46b9STakashi Sakamoto err_buffer: 1732d67c46b9STakashi Sakamoto iso_packets_buffer_destroy(&s->buffer, s->unit); 1733d67c46b9STakashi Sakamoto err_unlock: 1734d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 1735d67c46b9STakashi Sakamoto 1736d67c46b9STakashi Sakamoto return err; 1737d67c46b9STakashi Sakamoto } 1738d67c46b9STakashi Sakamoto 1739d67c46b9STakashi Sakamoto /** 1740f890f9a0STakashi Sakamoto * amdtp_domain_stream_pcm_pointer - get the PCM buffer position 1741f890f9a0STakashi Sakamoto * @d: the AMDTP domain. 1742d67c46b9STakashi Sakamoto * @s: the AMDTP stream that transports the PCM data 1743d67c46b9STakashi Sakamoto * 1744d67c46b9STakashi Sakamoto * Returns the current buffer position, in frames. 1745d67c46b9STakashi Sakamoto */ 1746f890f9a0STakashi Sakamoto unsigned long amdtp_domain_stream_pcm_pointer(struct amdtp_domain *d, 1747f890f9a0STakashi Sakamoto struct amdtp_stream *s) 1748d67c46b9STakashi Sakamoto { 1749f890f9a0STakashi Sakamoto struct amdtp_stream *irq_target = d->irq_target; 1750f890f9a0STakashi Sakamoto 1751*7ba5ca32STakashi Sakamoto // Process isochronous packets queued till recent isochronous cycle to handle PCM frames. 1752f890f9a0STakashi Sakamoto if (irq_target && amdtp_stream_running(irq_target)) { 1753*7ba5ca32STakashi Sakamoto // In software IRQ context, the call causes dead-lock to disable the tasklet 1754*7ba5ca32STakashi Sakamoto // synchronously. 1755*7ba5ca32STakashi Sakamoto if (!in_interrupt()) 1756f890f9a0STakashi Sakamoto fw_iso_context_flush_completions(irq_target->context); 1757f890f9a0STakashi Sakamoto } 1758d67c46b9STakashi Sakamoto 17596aa7de05SMark Rutland return READ_ONCE(s->pcm_buffer_pointer); 1760d67c46b9STakashi Sakamoto } 1761f890f9a0STakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_stream_pcm_pointer); 1762d67c46b9STakashi Sakamoto 1763d67c46b9STakashi Sakamoto /** 1764e6dcc92fSTakashi Sakamoto * amdtp_domain_stream_pcm_ack - acknowledge queued PCM frames 1765e6dcc92fSTakashi Sakamoto * @d: the AMDTP domain. 1766875becf8STakashi Sakamoto * @s: the AMDTP stream that transfers the PCM frames 1767875becf8STakashi Sakamoto * 1768875becf8STakashi Sakamoto * Returns zero always. 1769875becf8STakashi Sakamoto */ 1770e6dcc92fSTakashi Sakamoto int amdtp_domain_stream_pcm_ack(struct amdtp_domain *d, struct amdtp_stream *s) 1771875becf8STakashi Sakamoto { 1772e6dcc92fSTakashi Sakamoto struct amdtp_stream *irq_target = d->irq_target; 1773e6dcc92fSTakashi Sakamoto 1774e6dcc92fSTakashi Sakamoto // Process isochronous packets for recent isochronous cycle to handle 1775e6dcc92fSTakashi Sakamoto // queued PCM frames. 1776987b705bSTakashi Sakamoto if (irq_target && amdtp_stream_running(irq_target)) 1777e6dcc92fSTakashi Sakamoto fw_iso_context_flush_completions(irq_target->context); 1778875becf8STakashi Sakamoto 1779875becf8STakashi Sakamoto return 0; 1780875becf8STakashi Sakamoto } 1781e6dcc92fSTakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_stream_pcm_ack); 1782875becf8STakashi Sakamoto 1783875becf8STakashi Sakamoto /** 1784d67c46b9STakashi Sakamoto * amdtp_stream_update - update the stream after a bus reset 1785d67c46b9STakashi Sakamoto * @s: the AMDTP stream 1786d67c46b9STakashi Sakamoto */ 1787d67c46b9STakashi Sakamoto void amdtp_stream_update(struct amdtp_stream *s) 1788d67c46b9STakashi Sakamoto { 1789d67c46b9STakashi Sakamoto /* Precomputing. */ 17906aa7de05SMark Rutland WRITE_ONCE(s->source_node_id_field, 17916aa7de05SMark Rutland (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & CIP_SID_MASK); 1792d67c46b9STakashi Sakamoto } 1793d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_update); 1794d67c46b9STakashi Sakamoto 1795d67c46b9STakashi Sakamoto /** 1796d67c46b9STakashi Sakamoto * amdtp_stream_stop - stop sending packets 1797d67c46b9STakashi Sakamoto * @s: the AMDTP stream to stop 1798d67c46b9STakashi Sakamoto * 1799d67c46b9STakashi Sakamoto * All PCM and MIDI devices of the stream must be stopped before the stream 1800d67c46b9STakashi Sakamoto * itself can be stopped. 1801d67c46b9STakashi Sakamoto */ 180274f94e41STakashi Sakamoto static void amdtp_stream_stop(struct amdtp_stream *s) 1803d67c46b9STakashi Sakamoto { 1804d67c46b9STakashi Sakamoto mutex_lock(&s->mutex); 1805d67c46b9STakashi Sakamoto 1806d67c46b9STakashi Sakamoto if (!amdtp_stream_running(s)) { 1807d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 1808d67c46b9STakashi Sakamoto return; 1809d67c46b9STakashi Sakamoto } 1810d67c46b9STakashi Sakamoto 18112b3d2987STakashi Iwai cancel_work_sync(&s->period_work); 1812d67c46b9STakashi Sakamoto fw_iso_context_stop(s->context); 1813d67c46b9STakashi Sakamoto fw_iso_context_destroy(s->context); 1814d67c46b9STakashi Sakamoto s->context = ERR_PTR(-1); 1815d67c46b9STakashi Sakamoto iso_packets_buffer_destroy(&s->buffer, s->unit); 181604130cf8STakashi Sakamoto kfree(s->pkt_descs); 1817d67c46b9STakashi Sakamoto 1818f9e5ecdfSTakashi Sakamoto if (s->direction == AMDTP_OUT_STREAM) { 18196f24bb8aSTakashi Sakamoto kfree(s->ctx_data.rx.seq.descs); 1820f9e5ecdfSTakashi Sakamoto } else { 1821f9e5ecdfSTakashi Sakamoto if (s->domain->replay.enable) 1822f9e5ecdfSTakashi Sakamoto kfree(s->ctx_data.tx.cache.descs); 1823f9e5ecdfSTakashi Sakamoto } 1824d67c46b9STakashi Sakamoto 1825d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 1826d67c46b9STakashi Sakamoto } 1827d67c46b9STakashi Sakamoto 1828d67c46b9STakashi Sakamoto /** 1829d67c46b9STakashi Sakamoto * amdtp_stream_pcm_abort - abort the running PCM device 1830d67c46b9STakashi Sakamoto * @s: the AMDTP stream about to be stopped 1831d67c46b9STakashi Sakamoto * 1832d67c46b9STakashi Sakamoto * If the isochronous stream needs to be stopped asynchronously, call this 1833d67c46b9STakashi Sakamoto * function first to stop the PCM device. 1834d67c46b9STakashi Sakamoto */ 1835d67c46b9STakashi Sakamoto void amdtp_stream_pcm_abort(struct amdtp_stream *s) 1836d67c46b9STakashi Sakamoto { 1837d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm; 1838d67c46b9STakashi Sakamoto 18396aa7de05SMark Rutland pcm = READ_ONCE(s->pcm); 1840d67c46b9STakashi Sakamoto if (pcm) 1841d67c46b9STakashi Sakamoto snd_pcm_stop_xrun(pcm); 1842d67c46b9STakashi Sakamoto } 1843d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_abort); 18443ec3d7a3STakashi Sakamoto 18453ec3d7a3STakashi Sakamoto /** 18463ec3d7a3STakashi Sakamoto * amdtp_domain_init - initialize an AMDTP domain structure 18473ec3d7a3STakashi Sakamoto * @d: the AMDTP domain to initialize. 18483ec3d7a3STakashi Sakamoto */ 18493ec3d7a3STakashi Sakamoto int amdtp_domain_init(struct amdtp_domain *d) 18503ec3d7a3STakashi Sakamoto { 18513ec3d7a3STakashi Sakamoto INIT_LIST_HEAD(&d->streams); 18523ec3d7a3STakashi Sakamoto 1853d68c3123STakashi Sakamoto d->events_per_period = 0; 1854d68c3123STakashi Sakamoto 18553ec3d7a3STakashi Sakamoto return 0; 18563ec3d7a3STakashi Sakamoto } 18573ec3d7a3STakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_init); 18583ec3d7a3STakashi Sakamoto 18593ec3d7a3STakashi Sakamoto /** 18603ec3d7a3STakashi Sakamoto * amdtp_domain_destroy - destroy an AMDTP domain structure 18613ec3d7a3STakashi Sakamoto * @d: the AMDTP domain to destroy. 18623ec3d7a3STakashi Sakamoto */ 18633ec3d7a3STakashi Sakamoto void amdtp_domain_destroy(struct amdtp_domain *d) 18643ec3d7a3STakashi Sakamoto { 18658d0d5c3fSTakashi Sakamoto // At present nothing to do. 18668d0d5c3fSTakashi Sakamoto return; 18673ec3d7a3STakashi Sakamoto } 18683ec3d7a3STakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_destroy); 18696261f90bSTakashi Sakamoto 18706261f90bSTakashi Sakamoto /** 1871157a53eeSTakashi Sakamoto * amdtp_domain_add_stream - register isoc context into the domain. 1872157a53eeSTakashi Sakamoto * @d: the AMDTP domain. 1873157a53eeSTakashi Sakamoto * @s: the AMDTP stream. 1874157a53eeSTakashi Sakamoto * @channel: the isochronous channel on the bus. 1875157a53eeSTakashi Sakamoto * @speed: firewire speed code. 1876157a53eeSTakashi Sakamoto */ 1877157a53eeSTakashi Sakamoto int amdtp_domain_add_stream(struct amdtp_domain *d, struct amdtp_stream *s, 1878157a53eeSTakashi Sakamoto int channel, int speed) 1879157a53eeSTakashi Sakamoto { 1880157a53eeSTakashi Sakamoto struct amdtp_stream *tmp; 1881157a53eeSTakashi Sakamoto 1882157a53eeSTakashi Sakamoto list_for_each_entry(tmp, &d->streams, list) { 1883157a53eeSTakashi Sakamoto if (s == tmp) 1884157a53eeSTakashi Sakamoto return -EBUSY; 1885157a53eeSTakashi Sakamoto } 1886157a53eeSTakashi Sakamoto 1887157a53eeSTakashi Sakamoto list_add(&s->list, &d->streams); 1888157a53eeSTakashi Sakamoto 1889157a53eeSTakashi Sakamoto s->channel = channel; 1890157a53eeSTakashi Sakamoto s->speed = speed; 18912472cfb3STakashi Sakamoto s->domain = d; 1892157a53eeSTakashi Sakamoto 1893157a53eeSTakashi Sakamoto return 0; 1894157a53eeSTakashi Sakamoto } 1895157a53eeSTakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_add_stream); 1896157a53eeSTakashi Sakamoto 189739c2649cSTakashi Sakamoto // Make the reference from rx stream to tx stream for sequence replay. When the number of tx streams 189839c2649cSTakashi Sakamoto // is less than the number of rx streams, the first tx stream is selected. 189939c2649cSTakashi Sakamoto static int make_association(struct amdtp_domain *d) 190039c2649cSTakashi Sakamoto { 190139c2649cSTakashi Sakamoto unsigned int dst_index = 0; 190239c2649cSTakashi Sakamoto struct amdtp_stream *rx; 190339c2649cSTakashi Sakamoto 190439c2649cSTakashi Sakamoto // Make association to replay target. 190539c2649cSTakashi Sakamoto list_for_each_entry(rx, &d->streams, list) { 190639c2649cSTakashi Sakamoto if (rx->direction == AMDTP_OUT_STREAM) { 190739c2649cSTakashi Sakamoto unsigned int src_index = 0; 190839c2649cSTakashi Sakamoto struct amdtp_stream *tx = NULL; 190939c2649cSTakashi Sakamoto struct amdtp_stream *s; 191039c2649cSTakashi Sakamoto 191139c2649cSTakashi Sakamoto list_for_each_entry(s, &d->streams, list) { 191239c2649cSTakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 191339c2649cSTakashi Sakamoto if (dst_index == src_index) { 191439c2649cSTakashi Sakamoto tx = s; 191539c2649cSTakashi Sakamoto break; 191639c2649cSTakashi Sakamoto } 191739c2649cSTakashi Sakamoto 191839c2649cSTakashi Sakamoto ++src_index; 191939c2649cSTakashi Sakamoto } 192039c2649cSTakashi Sakamoto } 192139c2649cSTakashi Sakamoto if (!tx) { 192239c2649cSTakashi Sakamoto // Select the first entry. 192339c2649cSTakashi Sakamoto list_for_each_entry(s, &d->streams, list) { 192439c2649cSTakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 192539c2649cSTakashi Sakamoto tx = s; 192639c2649cSTakashi Sakamoto break; 192739c2649cSTakashi Sakamoto } 192839c2649cSTakashi Sakamoto } 192939c2649cSTakashi Sakamoto // No target is available to replay sequence. 193039c2649cSTakashi Sakamoto if (!tx) 193139c2649cSTakashi Sakamoto return -EINVAL; 193239c2649cSTakashi Sakamoto } 193339c2649cSTakashi Sakamoto 193439c2649cSTakashi Sakamoto rx->ctx_data.rx.replay_target = tx; 193539c2649cSTakashi Sakamoto rx->ctx_data.rx.cache_head = 0; 193639c2649cSTakashi Sakamoto 193739c2649cSTakashi Sakamoto ++dst_index; 193839c2649cSTakashi Sakamoto } 193939c2649cSTakashi Sakamoto } 194039c2649cSTakashi Sakamoto 194139c2649cSTakashi Sakamoto return 0; 194239c2649cSTakashi Sakamoto } 194339c2649cSTakashi Sakamoto 1944157a53eeSTakashi Sakamoto /** 19459b4702b0STakashi Sakamoto * amdtp_domain_start - start sending packets for isoc context in the domain. 19469b4702b0STakashi Sakamoto * @d: the AMDTP domain. 194726541cb1STakashi Sakamoto * @tx_init_skip_cycles: the number of cycles to skip processing packets at initial stage of IR 194826541cb1STakashi Sakamoto * contexts. 1949f9e5ecdfSTakashi Sakamoto * @replay_seq: whether to replay the sequence of packet in IR context for the sequence of packet in 1950f9e5ecdfSTakashi Sakamoto * IT context. 19512f21a177STakashi Sakamoto * @replay_on_the_fly: transfer rx packets according to nominal frequency, then begin to replay 19522f21a177STakashi Sakamoto * according to arrival of events in tx packets. 19539b4702b0STakashi Sakamoto */ 19542f21a177STakashi Sakamoto int amdtp_domain_start(struct amdtp_domain *d, unsigned int tx_init_skip_cycles, bool replay_seq, 19552f21a177STakashi Sakamoto bool replay_on_the_fly) 19569b4702b0STakashi Sakamoto { 1957af86b0b1STakashi Sakamoto unsigned int events_per_buffer = d->events_per_buffer; 1958af86b0b1STakashi Sakamoto unsigned int events_per_period = d->events_per_period; 1959af86b0b1STakashi Sakamoto unsigned int queue_size; 19609b4702b0STakashi Sakamoto struct amdtp_stream *s; 1961acfedcbeSTakashi Sakamoto int err; 19629b4702b0STakashi Sakamoto 196339c2649cSTakashi Sakamoto if (replay_seq) { 196439c2649cSTakashi Sakamoto err = make_association(d); 196539c2649cSTakashi Sakamoto if (err < 0) 196639c2649cSTakashi Sakamoto return err; 196739c2649cSTakashi Sakamoto } 1968f9e5ecdfSTakashi Sakamoto d->replay.enable = replay_seq; 19692f21a177STakashi Sakamoto d->replay.on_the_fly = replay_on_the_fly; 1970f9e5ecdfSTakashi Sakamoto 197160dd4929STakashi Sakamoto // Select an IT context as IRQ target. 19729b4702b0STakashi Sakamoto list_for_each_entry(s, &d->streams, list) { 197360dd4929STakashi Sakamoto if (s->direction == AMDTP_OUT_STREAM) 19749b4702b0STakashi Sakamoto break; 19759b4702b0STakashi Sakamoto } 197660dd4929STakashi Sakamoto if (!s) 197760dd4929STakashi Sakamoto return -ENXIO; 197860dd4929STakashi Sakamoto d->irq_target = s; 19799b4702b0STakashi Sakamoto 198026541cb1STakashi Sakamoto d->processing_cycle.tx_init_skip = tx_init_skip_cycles; 198126541cb1STakashi Sakamoto 1982af86b0b1STakashi Sakamoto // This is a case that AMDTP streams in domain run just for MIDI 1983af86b0b1STakashi Sakamoto // substream. Use the number of events equivalent to 10 msec as 1984af86b0b1STakashi Sakamoto // interval of hardware IRQ. 1985af86b0b1STakashi Sakamoto if (events_per_period == 0) 1986af86b0b1STakashi Sakamoto events_per_period = amdtp_rate_table[d->irq_target->sfc] / 100; 1987af86b0b1STakashi Sakamoto if (events_per_buffer == 0) 1988af86b0b1STakashi Sakamoto events_per_buffer = events_per_period * 3; 1989af86b0b1STakashi Sakamoto 1990af86b0b1STakashi Sakamoto queue_size = DIV_ROUND_UP(CYCLES_PER_SECOND * events_per_buffer, 1991af86b0b1STakashi Sakamoto amdtp_rate_table[d->irq_target->sfc]); 1992af86b0b1STakashi Sakamoto 199360dd4929STakashi Sakamoto list_for_each_entry(s, &d->streams, list) { 1994bd165079STakashi Sakamoto unsigned int idle_irq_interval = 0; 1995acfedcbeSTakashi Sakamoto 1996bd165079STakashi Sakamoto if (s->direction == AMDTP_OUT_STREAM && s == d->irq_target) { 1997af86b0b1STakashi Sakamoto idle_irq_interval = DIV_ROUND_UP(CYCLES_PER_SECOND * events_per_period, 1998af86b0b1STakashi Sakamoto amdtp_rate_table[d->irq_target->sfc]); 1999bd165079STakashi Sakamoto } 2000bd165079STakashi Sakamoto 2001bd165079STakashi Sakamoto // Starts immediately but actually DMA context starts several hundred cycles later. 2002bd165079STakashi Sakamoto err = amdtp_stream_start(s, s->channel, s->speed, queue_size, idle_irq_interval); 200360dd4929STakashi Sakamoto if (err < 0) 200460dd4929STakashi Sakamoto goto error; 2005bd165079STakashi Sakamoto } 200660dd4929STakashi Sakamoto 200760dd4929STakashi Sakamoto return 0; 200860dd4929STakashi Sakamoto error: 200960dd4929STakashi Sakamoto list_for_each_entry(s, &d->streams, list) 201060dd4929STakashi Sakamoto amdtp_stream_stop(s); 20119b4702b0STakashi Sakamoto return err; 20129b4702b0STakashi Sakamoto } 20139b4702b0STakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_start); 20149b4702b0STakashi Sakamoto 20159b4702b0STakashi Sakamoto /** 20166261f90bSTakashi Sakamoto * amdtp_domain_stop - stop sending packets for isoc context in the same domain. 20176261f90bSTakashi Sakamoto * @d: the AMDTP domain to which the isoc contexts belong. 20186261f90bSTakashi Sakamoto */ 20196261f90bSTakashi Sakamoto void amdtp_domain_stop(struct amdtp_domain *d) 20206261f90bSTakashi Sakamoto { 20216261f90bSTakashi Sakamoto struct amdtp_stream *s, *next; 20226261f90bSTakashi Sakamoto 202360dd4929STakashi Sakamoto if (d->irq_target) 202460dd4929STakashi Sakamoto amdtp_stream_stop(d->irq_target); 202560dd4929STakashi Sakamoto 20266261f90bSTakashi Sakamoto list_for_each_entry_safe(s, next, &d->streams, list) { 20276261f90bSTakashi Sakamoto list_del(&s->list); 20286261f90bSTakashi Sakamoto 202960dd4929STakashi Sakamoto if (s != d->irq_target) 20306261f90bSTakashi Sakamoto amdtp_stream_stop(s); 20316261f90bSTakashi Sakamoto } 2032d68c3123STakashi Sakamoto 2033d68c3123STakashi Sakamoto d->events_per_period = 0; 203460dd4929STakashi Sakamoto d->irq_target = NULL; 20356261f90bSTakashi Sakamoto } 20366261f90bSTakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_stop); 2037