1d67c46b9STakashi Sakamoto /* 2d67c46b9STakashi Sakamoto * Audio and Music Data Transmission Protocol (IEC 61883-6) streams 3d67c46b9STakashi Sakamoto * with Common Isochronous Packet (IEC 61883-1) headers 4d67c46b9STakashi Sakamoto * 5d67c46b9STakashi Sakamoto * Copyright (c) Clemens Ladisch <clemens@ladisch.de> 6d67c46b9STakashi Sakamoto * Licensed under the terms of the GNU General Public License, version 2. 7d67c46b9STakashi Sakamoto */ 8d67c46b9STakashi Sakamoto 9d67c46b9STakashi Sakamoto #include <linux/device.h> 10d67c46b9STakashi Sakamoto #include <linux/err.h> 11d67c46b9STakashi Sakamoto #include <linux/firewire.h> 12d67c46b9STakashi Sakamoto #include <linux/module.h> 13d67c46b9STakashi Sakamoto #include <linux/slab.h> 14d67c46b9STakashi Sakamoto #include <sound/pcm.h> 15d67c46b9STakashi Sakamoto #include <sound/pcm_params.h> 16d67c46b9STakashi Sakamoto #include "amdtp-stream.h" 17d67c46b9STakashi Sakamoto 18d67c46b9STakashi Sakamoto #define TICKS_PER_CYCLE 3072 19d67c46b9STakashi Sakamoto #define CYCLES_PER_SECOND 8000 20d67c46b9STakashi Sakamoto #define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND) 21d67c46b9STakashi Sakamoto 220c95c1d6STakashi Sakamoto /* Always support Linux tracing subsystem. */ 230c95c1d6STakashi Sakamoto #define CREATE_TRACE_POINTS 240c95c1d6STakashi Sakamoto #include "amdtp-stream-trace.h" 250c95c1d6STakashi Sakamoto 26d67c46b9STakashi Sakamoto #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */ 27d67c46b9STakashi Sakamoto 28d67c46b9STakashi Sakamoto /* isochronous header parameters */ 29d67c46b9STakashi Sakamoto #define ISO_DATA_LENGTH_SHIFT 16 303b196c39STakashi Sakamoto #define TAG_NO_CIP_HEADER 0 31d67c46b9STakashi Sakamoto #define TAG_CIP 1 32d67c46b9STakashi Sakamoto 33d67c46b9STakashi Sakamoto /* common isochronous packet header parameters */ 34d67c46b9STakashi Sakamoto #define CIP_EOH_SHIFT 31 35d67c46b9STakashi Sakamoto #define CIP_EOH (1u << CIP_EOH_SHIFT) 36d67c46b9STakashi Sakamoto #define CIP_EOH_MASK 0x80000000 37d67c46b9STakashi Sakamoto #define CIP_SID_SHIFT 24 38d67c46b9STakashi Sakamoto #define CIP_SID_MASK 0x3f000000 39d67c46b9STakashi Sakamoto #define CIP_DBS_MASK 0x00ff0000 40d67c46b9STakashi Sakamoto #define CIP_DBS_SHIFT 16 419863874fSTakashi Sakamoto #define CIP_SPH_MASK 0x00000400 429863874fSTakashi Sakamoto #define CIP_SPH_SHIFT 10 43d67c46b9STakashi Sakamoto #define CIP_DBC_MASK 0x000000ff 44d67c46b9STakashi Sakamoto #define CIP_FMT_SHIFT 24 45d67c46b9STakashi Sakamoto #define CIP_FMT_MASK 0x3f000000 46d67c46b9STakashi Sakamoto #define CIP_FDF_MASK 0x00ff0000 47d67c46b9STakashi Sakamoto #define CIP_FDF_SHIFT 16 48d67c46b9STakashi Sakamoto #define CIP_SYT_MASK 0x0000ffff 49d67c46b9STakashi Sakamoto #define CIP_SYT_NO_INFO 0xffff 50d67c46b9STakashi Sakamoto 5151c29fd2STakashi Sakamoto /* Audio and Music transfer protocol specific parameters */ 52d67c46b9STakashi Sakamoto #define CIP_FMT_AM 0x10 53d67c46b9STakashi Sakamoto #define AMDTP_FDF_NO_DATA 0xff 54d67c46b9STakashi Sakamoto 55d67c46b9STakashi Sakamoto /* TODO: make these configurable */ 56d67c46b9STakashi Sakamoto #define INTERRUPT_INTERVAL 16 57d67c46b9STakashi Sakamoto #define QUEUE_LENGTH 48 58d67c46b9STakashi Sakamoto 59d67c46b9STakashi Sakamoto #define IN_PACKET_HEADER_SIZE 4 60d67c46b9STakashi Sakamoto #define OUT_PACKET_HEADER_SIZE 0 61d67c46b9STakashi Sakamoto 62d67c46b9STakashi Sakamoto static void pcm_period_tasklet(unsigned long data); 63d67c46b9STakashi Sakamoto 64d67c46b9STakashi Sakamoto /** 65d67c46b9STakashi Sakamoto * amdtp_stream_init - initialize an AMDTP stream structure 66d67c46b9STakashi Sakamoto * @s: the AMDTP stream to initialize 67d67c46b9STakashi Sakamoto * @unit: the target of the stream 68d67c46b9STakashi Sakamoto * @dir: the direction of stream 69d67c46b9STakashi Sakamoto * @flags: the packet transmission method to use 705955815eSTakashi Sakamoto * @fmt: the value of fmt field in CIP header 71df075feeSTakashi Sakamoto * @process_data_blocks: callback handler to process data blocks 72df075feeSTakashi Sakamoto * @protocol_size: the size to allocate newly for protocol 73d67c46b9STakashi Sakamoto */ 74d67c46b9STakashi Sakamoto int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit, 755955815eSTakashi Sakamoto enum amdtp_stream_direction dir, enum cip_flags flags, 76df075feeSTakashi Sakamoto unsigned int fmt, 77df075feeSTakashi Sakamoto amdtp_stream_process_data_blocks_t process_data_blocks, 78df075feeSTakashi Sakamoto unsigned int protocol_size) 79d67c46b9STakashi Sakamoto { 80df075feeSTakashi Sakamoto if (process_data_blocks == NULL) 81df075feeSTakashi Sakamoto return -EINVAL; 82df075feeSTakashi Sakamoto 83df075feeSTakashi Sakamoto s->protocol = kzalloc(protocol_size, GFP_KERNEL); 84df075feeSTakashi Sakamoto if (!s->protocol) 85df075feeSTakashi Sakamoto return -ENOMEM; 86df075feeSTakashi Sakamoto 87d67c46b9STakashi Sakamoto s->unit = unit; 88d67c46b9STakashi Sakamoto s->direction = dir; 89d67c46b9STakashi Sakamoto s->flags = flags; 90d67c46b9STakashi Sakamoto s->context = ERR_PTR(-1); 91d67c46b9STakashi Sakamoto mutex_init(&s->mutex); 92d67c46b9STakashi Sakamoto tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s); 93d67c46b9STakashi Sakamoto s->packet_index = 0; 94d67c46b9STakashi Sakamoto 95d67c46b9STakashi Sakamoto init_waitqueue_head(&s->callback_wait); 96d67c46b9STakashi Sakamoto s->callbacked = false; 97d67c46b9STakashi Sakamoto 985955815eSTakashi Sakamoto s->fmt = fmt; 99df075feeSTakashi Sakamoto s->process_data_blocks = process_data_blocks; 100d67c46b9STakashi Sakamoto 101d67c46b9STakashi Sakamoto return 0; 102d67c46b9STakashi Sakamoto } 103d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_init); 104d67c46b9STakashi Sakamoto 105d67c46b9STakashi Sakamoto /** 106d67c46b9STakashi Sakamoto * amdtp_stream_destroy - free stream resources 107d67c46b9STakashi Sakamoto * @s: the AMDTP stream to destroy 108d67c46b9STakashi Sakamoto */ 109d67c46b9STakashi Sakamoto void amdtp_stream_destroy(struct amdtp_stream *s) 110d67c46b9STakashi Sakamoto { 11144c376b9STakashi Sakamoto /* Not initialized. */ 11244c376b9STakashi Sakamoto if (s->protocol == NULL) 11344c376b9STakashi Sakamoto return; 11444c376b9STakashi Sakamoto 115d67c46b9STakashi Sakamoto WARN_ON(amdtp_stream_running(s)); 116df075feeSTakashi Sakamoto kfree(s->protocol); 117d67c46b9STakashi Sakamoto mutex_destroy(&s->mutex); 118d67c46b9STakashi Sakamoto } 119d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_destroy); 120d67c46b9STakashi Sakamoto 121d67c46b9STakashi Sakamoto const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = { 122d67c46b9STakashi Sakamoto [CIP_SFC_32000] = 8, 123d67c46b9STakashi Sakamoto [CIP_SFC_44100] = 8, 124d67c46b9STakashi Sakamoto [CIP_SFC_48000] = 8, 125d67c46b9STakashi Sakamoto [CIP_SFC_88200] = 16, 126d67c46b9STakashi Sakamoto [CIP_SFC_96000] = 16, 127d67c46b9STakashi Sakamoto [CIP_SFC_176400] = 32, 128d67c46b9STakashi Sakamoto [CIP_SFC_192000] = 32, 129d67c46b9STakashi Sakamoto }; 130d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_syt_intervals); 131d67c46b9STakashi Sakamoto 132d67c46b9STakashi Sakamoto const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = { 133d67c46b9STakashi Sakamoto [CIP_SFC_32000] = 32000, 134d67c46b9STakashi Sakamoto [CIP_SFC_44100] = 44100, 135d67c46b9STakashi Sakamoto [CIP_SFC_48000] = 48000, 136d67c46b9STakashi Sakamoto [CIP_SFC_88200] = 88200, 137d67c46b9STakashi Sakamoto [CIP_SFC_96000] = 96000, 138d67c46b9STakashi Sakamoto [CIP_SFC_176400] = 176400, 139d67c46b9STakashi Sakamoto [CIP_SFC_192000] = 192000, 140d67c46b9STakashi Sakamoto }; 141d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_rate_table); 142d67c46b9STakashi Sakamoto 143d67c46b9STakashi Sakamoto /** 144d67c46b9STakashi Sakamoto * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream 145d67c46b9STakashi Sakamoto * @s: the AMDTP stream, which must be initialized. 146d67c46b9STakashi Sakamoto * @runtime: the PCM substream runtime 147d67c46b9STakashi Sakamoto */ 148d67c46b9STakashi Sakamoto int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s, 149d67c46b9STakashi Sakamoto struct snd_pcm_runtime *runtime) 150d67c46b9STakashi Sakamoto { 151d67c46b9STakashi Sakamoto int err; 152d67c46b9STakashi Sakamoto 153d67c46b9STakashi Sakamoto /* 154d67c46b9STakashi Sakamoto * Currently firewire-lib processes 16 packets in one software 155d67c46b9STakashi Sakamoto * interrupt callback. This equals to 2msec but actually the 156d67c46b9STakashi Sakamoto * interval of the interrupts has a jitter. 157d67c46b9STakashi Sakamoto * Additionally, even if adding a constraint to fit period size to 158d67c46b9STakashi Sakamoto * 2msec, actual calculated frames per period doesn't equal to 2msec, 159d67c46b9STakashi Sakamoto * depending on sampling rate. 160d67c46b9STakashi Sakamoto * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec. 161d67c46b9STakashi Sakamoto * Here let us use 5msec for safe period interrupt. 162d67c46b9STakashi Sakamoto */ 163d67c46b9STakashi Sakamoto err = snd_pcm_hw_constraint_minmax(runtime, 164d67c46b9STakashi Sakamoto SNDRV_PCM_HW_PARAM_PERIOD_TIME, 165d67c46b9STakashi Sakamoto 5000, UINT_MAX); 166d67c46b9STakashi Sakamoto if (err < 0) 167d67c46b9STakashi Sakamoto goto end; 168d67c46b9STakashi Sakamoto 169d67c46b9STakashi Sakamoto /* Non-Blocking stream has no more constraints */ 170d67c46b9STakashi Sakamoto if (!(s->flags & CIP_BLOCKING)) 171d67c46b9STakashi Sakamoto goto end; 172d67c46b9STakashi Sakamoto 173d67c46b9STakashi Sakamoto /* 174d67c46b9STakashi Sakamoto * One AMDTP packet can include some frames. In blocking mode, the 175d67c46b9STakashi Sakamoto * number equals to SYT_INTERVAL. So the number is 8, 16 or 32, 176d67c46b9STakashi Sakamoto * depending on its sampling rate. For accurate period interrupt, it's 177d67c46b9STakashi Sakamoto * preferrable to align period/buffer sizes to current SYT_INTERVAL. 178d67c46b9STakashi Sakamoto * 179d67c46b9STakashi Sakamoto * TODO: These constraints can be improved with proper rules. 180d67c46b9STakashi Sakamoto * Currently apply LCM of SYT_INTERVALs. 181d67c46b9STakashi Sakamoto */ 182d67c46b9STakashi Sakamoto err = snd_pcm_hw_constraint_step(runtime, 0, 183d67c46b9STakashi Sakamoto SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32); 184d67c46b9STakashi Sakamoto if (err < 0) 185d67c46b9STakashi Sakamoto goto end; 186d67c46b9STakashi Sakamoto err = snd_pcm_hw_constraint_step(runtime, 0, 187d67c46b9STakashi Sakamoto SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32); 188d67c46b9STakashi Sakamoto end: 189d67c46b9STakashi Sakamoto return err; 190d67c46b9STakashi Sakamoto } 191d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints); 192d67c46b9STakashi Sakamoto 193d67c46b9STakashi Sakamoto /** 194d67c46b9STakashi Sakamoto * amdtp_stream_set_parameters - set stream parameters 195d67c46b9STakashi Sakamoto * @s: the AMDTP stream to configure 196d67c46b9STakashi Sakamoto * @rate: the sample rate 197df075feeSTakashi Sakamoto * @data_block_quadlets: the size of a data block in quadlet unit 198d67c46b9STakashi Sakamoto * 199d67c46b9STakashi Sakamoto * The parameters must be set before the stream is started, and must not be 200d67c46b9STakashi Sakamoto * changed while the stream is running. 201d67c46b9STakashi Sakamoto */ 202df075feeSTakashi Sakamoto int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate, 203df075feeSTakashi Sakamoto unsigned int data_block_quadlets) 204d67c46b9STakashi Sakamoto { 205df075feeSTakashi Sakamoto unsigned int sfc; 206d67c46b9STakashi Sakamoto 207d67c46b9STakashi Sakamoto for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) { 208d67c46b9STakashi Sakamoto if (amdtp_rate_table[sfc] == rate) 209d67c46b9STakashi Sakamoto break; 210d67c46b9STakashi Sakamoto } 211d67c46b9STakashi Sakamoto if (sfc == ARRAY_SIZE(amdtp_rate_table)) 212d67c46b9STakashi Sakamoto return -EINVAL; 213d67c46b9STakashi Sakamoto 214d67c46b9STakashi Sakamoto s->sfc = sfc; 215df075feeSTakashi Sakamoto s->data_block_quadlets = data_block_quadlets; 216d67c46b9STakashi Sakamoto s->syt_interval = amdtp_syt_intervals[sfc]; 217d67c46b9STakashi Sakamoto 218d67c46b9STakashi Sakamoto /* default buffering in the device */ 219d67c46b9STakashi Sakamoto s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE; 220d67c46b9STakashi Sakamoto if (s->flags & CIP_BLOCKING) 221d67c46b9STakashi Sakamoto /* additional buffering needed to adjust for no-data packets */ 222d67c46b9STakashi Sakamoto s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate; 223d67c46b9STakashi Sakamoto 224d67c46b9STakashi Sakamoto return 0; 225d67c46b9STakashi Sakamoto } 226d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_set_parameters); 227d67c46b9STakashi Sakamoto 228d67c46b9STakashi Sakamoto /** 229d67c46b9STakashi Sakamoto * amdtp_stream_get_max_payload - get the stream's packet size 230d67c46b9STakashi Sakamoto * @s: the AMDTP stream 231d67c46b9STakashi Sakamoto * 232d67c46b9STakashi Sakamoto * This function must not be called before the stream has been configured 233d67c46b9STakashi Sakamoto * with amdtp_stream_set_parameters(). 234d67c46b9STakashi Sakamoto */ 235d67c46b9STakashi Sakamoto unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s) 236d67c46b9STakashi Sakamoto { 237d67c46b9STakashi Sakamoto unsigned int multiplier = 1; 2383b196c39STakashi Sakamoto unsigned int header_size = 0; 239d67c46b9STakashi Sakamoto 240d67c46b9STakashi Sakamoto if (s->flags & CIP_JUMBO_PAYLOAD) 241d67c46b9STakashi Sakamoto multiplier = 5; 2423b196c39STakashi Sakamoto if (!(s->flags & CIP_NO_HEADER)) 2433b196c39STakashi Sakamoto header_size = 8; 244d67c46b9STakashi Sakamoto 2453b196c39STakashi Sakamoto return header_size + 2463b196c39STakashi Sakamoto s->syt_interval * s->data_block_quadlets * 4 * multiplier; 247d67c46b9STakashi Sakamoto } 248d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_get_max_payload); 249d67c46b9STakashi Sakamoto 250d67c46b9STakashi Sakamoto /** 251d67c46b9STakashi Sakamoto * amdtp_stream_pcm_prepare - prepare PCM device for running 252d67c46b9STakashi Sakamoto * @s: the AMDTP stream 253d67c46b9STakashi Sakamoto * 254d67c46b9STakashi Sakamoto * This function should be called from the PCM device's .prepare callback. 255d67c46b9STakashi Sakamoto */ 256d67c46b9STakashi Sakamoto void amdtp_stream_pcm_prepare(struct amdtp_stream *s) 257d67c46b9STakashi Sakamoto { 258d67c46b9STakashi Sakamoto tasklet_kill(&s->period_tasklet); 259d67c46b9STakashi Sakamoto s->pcm_buffer_pointer = 0; 260d67c46b9STakashi Sakamoto s->pcm_period_pointer = 0; 261d67c46b9STakashi Sakamoto } 262d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_prepare); 263d67c46b9STakashi Sakamoto 264d67c46b9STakashi Sakamoto static unsigned int calculate_data_blocks(struct amdtp_stream *s, 265d67c46b9STakashi Sakamoto unsigned int syt) 266d67c46b9STakashi Sakamoto { 267d67c46b9STakashi Sakamoto unsigned int phase, data_blocks; 268d67c46b9STakashi Sakamoto 269d67c46b9STakashi Sakamoto /* Blocking mode. */ 270d67c46b9STakashi Sakamoto if (s->flags & CIP_BLOCKING) { 271d67c46b9STakashi Sakamoto /* This module generate empty packet for 'no data'. */ 272d67c46b9STakashi Sakamoto if (syt == CIP_SYT_NO_INFO) 273d67c46b9STakashi Sakamoto data_blocks = 0; 274d67c46b9STakashi Sakamoto else 275d67c46b9STakashi Sakamoto data_blocks = s->syt_interval; 276d67c46b9STakashi Sakamoto /* Non-blocking mode. */ 277d67c46b9STakashi Sakamoto } else { 278d67c46b9STakashi Sakamoto if (!cip_sfc_is_base_44100(s->sfc)) { 279d67c46b9STakashi Sakamoto /* Sample_rate / 8000 is an integer, and precomputed. */ 280d67c46b9STakashi Sakamoto data_blocks = s->data_block_state; 281d67c46b9STakashi Sakamoto } else { 282d67c46b9STakashi Sakamoto phase = s->data_block_state; 283d67c46b9STakashi Sakamoto 284d67c46b9STakashi Sakamoto /* 285d67c46b9STakashi Sakamoto * This calculates the number of data blocks per packet so that 286d67c46b9STakashi Sakamoto * 1) the overall rate is correct and exactly synchronized to 287d67c46b9STakashi Sakamoto * the bus clock, and 288d67c46b9STakashi Sakamoto * 2) packets with a rounded-up number of blocks occur as early 289d67c46b9STakashi Sakamoto * as possible in the sequence (to prevent underruns of the 290d67c46b9STakashi Sakamoto * device's buffer). 291d67c46b9STakashi Sakamoto */ 292d67c46b9STakashi Sakamoto if (s->sfc == CIP_SFC_44100) 293d67c46b9STakashi Sakamoto /* 6 6 5 6 5 6 5 ... */ 294d67c46b9STakashi Sakamoto data_blocks = 5 + ((phase & 1) ^ 295d67c46b9STakashi Sakamoto (phase == 0 || phase >= 40)); 296d67c46b9STakashi Sakamoto else 297d67c46b9STakashi Sakamoto /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */ 298d67c46b9STakashi Sakamoto data_blocks = 11 * (s->sfc >> 1) + (phase == 0); 299d67c46b9STakashi Sakamoto if (++phase >= (80 >> (s->sfc >> 1))) 300d67c46b9STakashi Sakamoto phase = 0; 301d67c46b9STakashi Sakamoto s->data_block_state = phase; 302d67c46b9STakashi Sakamoto } 303d67c46b9STakashi Sakamoto } 304d67c46b9STakashi Sakamoto 305d67c46b9STakashi Sakamoto return data_blocks; 306d67c46b9STakashi Sakamoto } 307d67c46b9STakashi Sakamoto 308d67c46b9STakashi Sakamoto static unsigned int calculate_syt(struct amdtp_stream *s, 309d67c46b9STakashi Sakamoto unsigned int cycle) 310d67c46b9STakashi Sakamoto { 311d67c46b9STakashi Sakamoto unsigned int syt_offset, phase, index, syt; 312d67c46b9STakashi Sakamoto 313d67c46b9STakashi Sakamoto if (s->last_syt_offset < TICKS_PER_CYCLE) { 314d67c46b9STakashi Sakamoto if (!cip_sfc_is_base_44100(s->sfc)) 315d67c46b9STakashi Sakamoto syt_offset = s->last_syt_offset + s->syt_offset_state; 316d67c46b9STakashi Sakamoto else { 317d67c46b9STakashi Sakamoto /* 318d67c46b9STakashi Sakamoto * The time, in ticks, of the n'th SYT_INTERVAL sample is: 319d67c46b9STakashi Sakamoto * n * SYT_INTERVAL * 24576000 / sample_rate 320d67c46b9STakashi Sakamoto * Modulo TICKS_PER_CYCLE, the difference between successive 321d67c46b9STakashi Sakamoto * elements is about 1386.23. Rounding the results of this 322d67c46b9STakashi Sakamoto * formula to the SYT precision results in a sequence of 323d67c46b9STakashi Sakamoto * differences that begins with: 324d67c46b9STakashi Sakamoto * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ... 325d67c46b9STakashi Sakamoto * This code generates _exactly_ the same sequence. 326d67c46b9STakashi Sakamoto */ 327d67c46b9STakashi Sakamoto phase = s->syt_offset_state; 328d67c46b9STakashi Sakamoto index = phase % 13; 329d67c46b9STakashi Sakamoto syt_offset = s->last_syt_offset; 330d67c46b9STakashi Sakamoto syt_offset += 1386 + ((index && !(index & 3)) || 331d67c46b9STakashi Sakamoto phase == 146); 332d67c46b9STakashi Sakamoto if (++phase >= 147) 333d67c46b9STakashi Sakamoto phase = 0; 334d67c46b9STakashi Sakamoto s->syt_offset_state = phase; 335d67c46b9STakashi Sakamoto } 336d67c46b9STakashi Sakamoto } else 337d67c46b9STakashi Sakamoto syt_offset = s->last_syt_offset - TICKS_PER_CYCLE; 338d67c46b9STakashi Sakamoto s->last_syt_offset = syt_offset; 339d67c46b9STakashi Sakamoto 340d67c46b9STakashi Sakamoto if (syt_offset < TICKS_PER_CYCLE) { 341d67c46b9STakashi Sakamoto syt_offset += s->transfer_delay; 342d67c46b9STakashi Sakamoto syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12; 343d67c46b9STakashi Sakamoto syt += syt_offset % TICKS_PER_CYCLE; 344d67c46b9STakashi Sakamoto 345d67c46b9STakashi Sakamoto return syt & CIP_SYT_MASK; 346d67c46b9STakashi Sakamoto } else { 347d67c46b9STakashi Sakamoto return CIP_SYT_NO_INFO; 348d67c46b9STakashi Sakamoto } 349d67c46b9STakashi Sakamoto } 350d67c46b9STakashi Sakamoto 351d67c46b9STakashi Sakamoto static void update_pcm_pointers(struct amdtp_stream *s, 352d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm, 353d67c46b9STakashi Sakamoto unsigned int frames) 354d67c46b9STakashi Sakamoto { 355d67c46b9STakashi Sakamoto unsigned int ptr; 356d67c46b9STakashi Sakamoto 357d67c46b9STakashi Sakamoto ptr = s->pcm_buffer_pointer + frames; 358d67c46b9STakashi Sakamoto if (ptr >= pcm->runtime->buffer_size) 359d67c46b9STakashi Sakamoto ptr -= pcm->runtime->buffer_size; 360d67c46b9STakashi Sakamoto ACCESS_ONCE(s->pcm_buffer_pointer) = ptr; 361d67c46b9STakashi Sakamoto 362d67c46b9STakashi Sakamoto s->pcm_period_pointer += frames; 363d67c46b9STakashi Sakamoto if (s->pcm_period_pointer >= pcm->runtime->period_size) { 364d67c46b9STakashi Sakamoto s->pcm_period_pointer -= pcm->runtime->period_size; 365d67c46b9STakashi Sakamoto tasklet_hi_schedule(&s->period_tasklet); 366d67c46b9STakashi Sakamoto } 367d67c46b9STakashi Sakamoto } 368d67c46b9STakashi Sakamoto 369d67c46b9STakashi Sakamoto static void pcm_period_tasklet(unsigned long data) 370d67c46b9STakashi Sakamoto { 371d67c46b9STakashi Sakamoto struct amdtp_stream *s = (void *)data; 372d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm); 373d67c46b9STakashi Sakamoto 374d67c46b9STakashi Sakamoto if (pcm) 375d67c46b9STakashi Sakamoto snd_pcm_period_elapsed(pcm); 376d67c46b9STakashi Sakamoto } 377d67c46b9STakashi Sakamoto 378ff38e0c7STakashi Sakamoto static int queue_packet(struct amdtp_stream *s, unsigned int header_length, 379ff38e0c7STakashi Sakamoto unsigned int payload_length) 380d67c46b9STakashi Sakamoto { 381d67c46b9STakashi Sakamoto struct fw_iso_packet p = {0}; 382d67c46b9STakashi Sakamoto int err = 0; 383d67c46b9STakashi Sakamoto 384d67c46b9STakashi Sakamoto if (IS_ERR(s->context)) 385d67c46b9STakashi Sakamoto goto end; 386d67c46b9STakashi Sakamoto 387d67c46b9STakashi Sakamoto p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL); 3883b196c39STakashi Sakamoto p.tag = s->tag; 389d67c46b9STakashi Sakamoto p.header_length = header_length; 390ff38e0c7STakashi Sakamoto if (payload_length > 0) 391ff38e0c7STakashi Sakamoto p.payload_length = payload_length; 392ff38e0c7STakashi Sakamoto else 393ff38e0c7STakashi Sakamoto p.skip = true; 394d67c46b9STakashi Sakamoto err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer, 395d67c46b9STakashi Sakamoto s->buffer.packets[s->packet_index].offset); 396d67c46b9STakashi Sakamoto if (err < 0) { 397d67c46b9STakashi Sakamoto dev_err(&s->unit->device, "queueing error: %d\n", err); 398d67c46b9STakashi Sakamoto goto end; 399d67c46b9STakashi Sakamoto } 400d67c46b9STakashi Sakamoto 401d67c46b9STakashi Sakamoto if (++s->packet_index >= QUEUE_LENGTH) 402d67c46b9STakashi Sakamoto s->packet_index = 0; 403d67c46b9STakashi Sakamoto end: 404d67c46b9STakashi Sakamoto return err; 405d67c46b9STakashi Sakamoto } 406d67c46b9STakashi Sakamoto 407d67c46b9STakashi Sakamoto static inline int queue_out_packet(struct amdtp_stream *s, 408ff38e0c7STakashi Sakamoto unsigned int payload_length) 409d67c46b9STakashi Sakamoto { 410ff38e0c7STakashi Sakamoto return queue_packet(s, OUT_PACKET_HEADER_SIZE, payload_length); 411d67c46b9STakashi Sakamoto } 412d67c46b9STakashi Sakamoto 413d67c46b9STakashi Sakamoto static inline int queue_in_packet(struct amdtp_stream *s) 414d67c46b9STakashi Sakamoto { 415*f91c9d76STakashi Sakamoto return queue_packet(s, IN_PACKET_HEADER_SIZE, s->max_payload_length); 416d67c46b9STakashi Sakamoto } 417d67c46b9STakashi Sakamoto 418ff0fb5aaSTakashi Sakamoto static int handle_out_packet(struct amdtp_stream *s, 419ff0fb5aaSTakashi Sakamoto unsigned int payload_length, unsigned int cycle, 420a9c4284bSTakashi Sakamoto unsigned int index) 421d67c46b9STakashi Sakamoto { 422d67c46b9STakashi Sakamoto __be32 *buffer; 423390a1512STakashi Sakamoto unsigned int syt; 424390a1512STakashi Sakamoto unsigned int data_blocks; 425d67c46b9STakashi Sakamoto unsigned int pcm_frames; 426d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm; 427d67c46b9STakashi Sakamoto 428d67c46b9STakashi Sakamoto buffer = s->buffer.packets[s->packet_index].buffer; 429390a1512STakashi Sakamoto syt = calculate_syt(s, cycle); 430390a1512STakashi Sakamoto data_blocks = calculate_data_blocks(s, syt); 431df075feeSTakashi Sakamoto pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt); 432d67c46b9STakashi Sakamoto 4339dae017bSTakashi Sakamoto if (s->flags & CIP_DBC_IS_END_EVENT) 4349dae017bSTakashi Sakamoto s->data_block_counter = 4359dae017bSTakashi Sakamoto (s->data_block_counter + data_blocks) & 0xff; 4369dae017bSTakashi Sakamoto 437d67c46b9STakashi Sakamoto buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) | 438d67c46b9STakashi Sakamoto (s->data_block_quadlets << CIP_DBS_SHIFT) | 4399863874fSTakashi Sakamoto ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) | 440d67c46b9STakashi Sakamoto s->data_block_counter); 441d67c46b9STakashi Sakamoto buffer[1] = cpu_to_be32(CIP_EOH | 442d67c46b9STakashi Sakamoto ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) | 443d67c46b9STakashi Sakamoto ((s->fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) | 444d67c46b9STakashi Sakamoto (syt & CIP_SYT_MASK)); 445d67c46b9STakashi Sakamoto 4469dae017bSTakashi Sakamoto if (!(s->flags & CIP_DBC_IS_END_EVENT)) 4479dae017bSTakashi Sakamoto s->data_block_counter = 4489dae017bSTakashi Sakamoto (s->data_block_counter + data_blocks) & 0xff; 449d67c46b9STakashi Sakamoto payload_length = 8 + data_blocks * 4 * s->data_block_quadlets; 4500c95c1d6STakashi Sakamoto 451a9c4284bSTakashi Sakamoto trace_out_packet(s, cycle, buffer, payload_length, index); 4520c95c1d6STakashi Sakamoto 453ff38e0c7STakashi Sakamoto if (queue_out_packet(s, payload_length) < 0) 454d67c46b9STakashi Sakamoto return -EIO; 455d67c46b9STakashi Sakamoto 456d67c46b9STakashi Sakamoto pcm = ACCESS_ONCE(s->pcm); 457d67c46b9STakashi Sakamoto if (pcm && pcm_frames > 0) 458d67c46b9STakashi Sakamoto update_pcm_pointers(s, pcm, pcm_frames); 459d67c46b9STakashi Sakamoto 460d67c46b9STakashi Sakamoto /* No need to return the number of handled data blocks. */ 461d67c46b9STakashi Sakamoto return 0; 462d67c46b9STakashi Sakamoto } 463d67c46b9STakashi Sakamoto 4643b196c39STakashi Sakamoto static int handle_out_packet_without_header(struct amdtp_stream *s, 4653b196c39STakashi Sakamoto unsigned int payload_length, unsigned int cycle, 4663b196c39STakashi Sakamoto unsigned int index) 4673b196c39STakashi Sakamoto { 4683b196c39STakashi Sakamoto __be32 *buffer; 4693b196c39STakashi Sakamoto unsigned int syt; 4703b196c39STakashi Sakamoto unsigned int data_blocks; 4713b196c39STakashi Sakamoto unsigned int pcm_frames; 4723b196c39STakashi Sakamoto struct snd_pcm_substream *pcm; 4733b196c39STakashi Sakamoto 4743b196c39STakashi Sakamoto buffer = s->buffer.packets[s->packet_index].buffer; 4753b196c39STakashi Sakamoto syt = calculate_syt(s, cycle); 4763b196c39STakashi Sakamoto data_blocks = calculate_data_blocks(s, syt); 4773b196c39STakashi Sakamoto pcm_frames = s->process_data_blocks(s, buffer, data_blocks, &syt); 4783b196c39STakashi Sakamoto s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff; 4793b196c39STakashi Sakamoto 4803b196c39STakashi Sakamoto payload_length = data_blocks * 4 * s->data_block_quadlets; 481b164d2fdSTakashi Sakamoto 482b164d2fdSTakashi Sakamoto trace_out_packet_without_header(s, cycle, payload_length, data_blocks, 483b164d2fdSTakashi Sakamoto index); 484b164d2fdSTakashi Sakamoto 4853b196c39STakashi Sakamoto if (queue_out_packet(s, payload_length) < 0) 4863b196c39STakashi Sakamoto return -EIO; 4873b196c39STakashi Sakamoto 4883b196c39STakashi Sakamoto pcm = ACCESS_ONCE(s->pcm); 4893b196c39STakashi Sakamoto if (pcm && pcm_frames > 0) 4903b196c39STakashi Sakamoto update_pcm_pointers(s, pcm, pcm_frames); 4913b196c39STakashi Sakamoto 4923b196c39STakashi Sakamoto /* No need to return the number of handled data blocks. */ 4933b196c39STakashi Sakamoto return 0; 4943b196c39STakashi Sakamoto } 4953b196c39STakashi Sakamoto 496d67c46b9STakashi Sakamoto static int handle_in_packet(struct amdtp_stream *s, 497ff0fb5aaSTakashi Sakamoto unsigned int payload_length, unsigned int cycle, 498a9c4284bSTakashi Sakamoto unsigned int index) 499d67c46b9STakashi Sakamoto { 500d9a16fc9STakashi Sakamoto __be32 *buffer; 501d67c46b9STakashi Sakamoto u32 cip_header[2]; 5029863874fSTakashi Sakamoto unsigned int sph, fmt, fdf, syt; 503d67c46b9STakashi Sakamoto unsigned int data_block_quadlets, data_block_counter, dbc_interval; 504d9a16fc9STakashi Sakamoto unsigned int data_blocks; 505d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm; 506d67c46b9STakashi Sakamoto unsigned int pcm_frames; 507d67c46b9STakashi Sakamoto bool lost; 508d67c46b9STakashi Sakamoto 509d9a16fc9STakashi Sakamoto buffer = s->buffer.packets[s->packet_index].buffer; 510d67c46b9STakashi Sakamoto cip_header[0] = be32_to_cpu(buffer[0]); 511d67c46b9STakashi Sakamoto cip_header[1] = be32_to_cpu(buffer[1]); 512d67c46b9STakashi Sakamoto 513ff0fb5aaSTakashi Sakamoto trace_in_packet(s, cycle, cip_header, payload_length, index); 5140c95c1d6STakashi Sakamoto 515d67c46b9STakashi Sakamoto /* 516d67c46b9STakashi Sakamoto * This module supports 'Two-quadlet CIP header with SYT field'. 517d67c46b9STakashi Sakamoto * For convenience, also check FMT field is AM824 or not. 518d67c46b9STakashi Sakamoto */ 5192128f78fSTakashi Sakamoto if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) || 5202128f78fSTakashi Sakamoto ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) && 5212128f78fSTakashi Sakamoto (!(s->flags & CIP_HEADER_WITHOUT_EOH))) { 522d67c46b9STakashi Sakamoto dev_info_ratelimited(&s->unit->device, 523d67c46b9STakashi Sakamoto "Invalid CIP header for AMDTP: %08X:%08X\n", 524d67c46b9STakashi Sakamoto cip_header[0], cip_header[1]); 525d9a16fc9STakashi Sakamoto data_blocks = 0; 526d67c46b9STakashi Sakamoto pcm_frames = 0; 527d67c46b9STakashi Sakamoto goto end; 528d67c46b9STakashi Sakamoto } 529d67c46b9STakashi Sakamoto 530d67c46b9STakashi Sakamoto /* Check valid protocol or not. */ 5319863874fSTakashi Sakamoto sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT; 532d67c46b9STakashi Sakamoto fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT; 5339863874fSTakashi Sakamoto if (sph != s->sph || fmt != s->fmt) { 5342a7e1713STakashi Sakamoto dev_info_ratelimited(&s->unit->device, 535d67c46b9STakashi Sakamoto "Detect unexpected protocol: %08x %08x\n", 536d67c46b9STakashi Sakamoto cip_header[0], cip_header[1]); 537d9a16fc9STakashi Sakamoto data_blocks = 0; 5382a7e1713STakashi Sakamoto pcm_frames = 0; 5392a7e1713STakashi Sakamoto goto end; 540d67c46b9STakashi Sakamoto } 541d67c46b9STakashi Sakamoto 542d67c46b9STakashi Sakamoto /* Calculate data blocks */ 543d67c46b9STakashi Sakamoto fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT; 544ff0fb5aaSTakashi Sakamoto if (payload_length < 12 || 545d67c46b9STakashi Sakamoto (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) { 546d9a16fc9STakashi Sakamoto data_blocks = 0; 547d67c46b9STakashi Sakamoto } else { 548d67c46b9STakashi Sakamoto data_block_quadlets = 549d67c46b9STakashi Sakamoto (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT; 550d67c46b9STakashi Sakamoto /* avoid division by zero */ 551d67c46b9STakashi Sakamoto if (data_block_quadlets == 0) { 552d67c46b9STakashi Sakamoto dev_err(&s->unit->device, 553d67c46b9STakashi Sakamoto "Detect invalid value in dbs field: %08X\n", 554d67c46b9STakashi Sakamoto cip_header[0]); 555d67c46b9STakashi Sakamoto return -EPROTO; 556d67c46b9STakashi Sakamoto } 557d67c46b9STakashi Sakamoto if (s->flags & CIP_WRONG_DBS) 558d67c46b9STakashi Sakamoto data_block_quadlets = s->data_block_quadlets; 559d67c46b9STakashi Sakamoto 560ff0fb5aaSTakashi Sakamoto data_blocks = (payload_length / 4 - 2) / 561ff0fb5aaSTakashi Sakamoto data_block_quadlets; 562d67c46b9STakashi Sakamoto } 563d67c46b9STakashi Sakamoto 564d67c46b9STakashi Sakamoto /* Check data block counter continuity */ 565d67c46b9STakashi Sakamoto data_block_counter = cip_header[0] & CIP_DBC_MASK; 566d9a16fc9STakashi Sakamoto if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) && 567d67c46b9STakashi Sakamoto s->data_block_counter != UINT_MAX) 568d67c46b9STakashi Sakamoto data_block_counter = s->data_block_counter; 569d67c46b9STakashi Sakamoto 570d67c46b9STakashi Sakamoto if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) && 571d67c46b9STakashi Sakamoto data_block_counter == s->tx_first_dbc) || 572d67c46b9STakashi Sakamoto s->data_block_counter == UINT_MAX) { 573d67c46b9STakashi Sakamoto lost = false; 574d67c46b9STakashi Sakamoto } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) { 575d67c46b9STakashi Sakamoto lost = data_block_counter != s->data_block_counter; 576d67c46b9STakashi Sakamoto } else { 577d9a16fc9STakashi Sakamoto if (data_blocks > 0 && s->tx_dbc_interval > 0) 578d67c46b9STakashi Sakamoto dbc_interval = s->tx_dbc_interval; 579d67c46b9STakashi Sakamoto else 580d9a16fc9STakashi Sakamoto dbc_interval = data_blocks; 581d67c46b9STakashi Sakamoto 582d67c46b9STakashi Sakamoto lost = data_block_counter != 583d67c46b9STakashi Sakamoto ((s->data_block_counter + dbc_interval) & 0xff); 584d67c46b9STakashi Sakamoto } 585d67c46b9STakashi Sakamoto 586d67c46b9STakashi Sakamoto if (lost) { 587d67c46b9STakashi Sakamoto dev_err(&s->unit->device, 588d67c46b9STakashi Sakamoto "Detect discontinuity of CIP: %02X %02X\n", 589d67c46b9STakashi Sakamoto s->data_block_counter, data_block_counter); 590d67c46b9STakashi Sakamoto return -EIO; 591d67c46b9STakashi Sakamoto } 592d67c46b9STakashi Sakamoto 593d9a16fc9STakashi Sakamoto syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK; 594d9a16fc9STakashi Sakamoto pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt); 595d67c46b9STakashi Sakamoto 596d67c46b9STakashi Sakamoto if (s->flags & CIP_DBC_IS_END_EVENT) 597d67c46b9STakashi Sakamoto s->data_block_counter = data_block_counter; 598d67c46b9STakashi Sakamoto else 599d67c46b9STakashi Sakamoto s->data_block_counter = 600d9a16fc9STakashi Sakamoto (data_block_counter + data_blocks) & 0xff; 601d67c46b9STakashi Sakamoto end: 602d67c46b9STakashi Sakamoto if (queue_in_packet(s) < 0) 603d67c46b9STakashi Sakamoto return -EIO; 604d67c46b9STakashi Sakamoto 605d67c46b9STakashi Sakamoto pcm = ACCESS_ONCE(s->pcm); 606d67c46b9STakashi Sakamoto if (pcm && pcm_frames > 0) 607d67c46b9STakashi Sakamoto update_pcm_pointers(s, pcm, pcm_frames); 608d67c46b9STakashi Sakamoto 609d67c46b9STakashi Sakamoto return 0; 610d67c46b9STakashi Sakamoto } 611d67c46b9STakashi Sakamoto 6123b196c39STakashi Sakamoto static int handle_in_packet_without_header(struct amdtp_stream *s, 6133b196c39STakashi Sakamoto unsigned int payload_quadlets, unsigned int cycle, 6143b196c39STakashi Sakamoto unsigned int index) 6153b196c39STakashi Sakamoto { 6163b196c39STakashi Sakamoto __be32 *buffer; 6173b196c39STakashi Sakamoto unsigned int data_blocks; 6183b196c39STakashi Sakamoto struct snd_pcm_substream *pcm; 6193b196c39STakashi Sakamoto unsigned int pcm_frames; 6203b196c39STakashi Sakamoto 6213b196c39STakashi Sakamoto buffer = s->buffer.packets[s->packet_index].buffer; 6223b196c39STakashi Sakamoto data_blocks = payload_quadlets / s->data_block_quadlets; 623b164d2fdSTakashi Sakamoto 624b164d2fdSTakashi Sakamoto trace_in_packet_without_header(s, cycle, payload_quadlets, data_blocks, 625b164d2fdSTakashi Sakamoto index); 626b164d2fdSTakashi Sakamoto 6273b196c39STakashi Sakamoto pcm_frames = s->process_data_blocks(s, buffer, data_blocks, NULL); 6283b196c39STakashi Sakamoto s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff; 6293b196c39STakashi Sakamoto 6303b196c39STakashi Sakamoto if (queue_in_packet(s) < 0) 6313b196c39STakashi Sakamoto return -EIO; 6323b196c39STakashi Sakamoto 6333b196c39STakashi Sakamoto pcm = ACCESS_ONCE(s->pcm); 6343b196c39STakashi Sakamoto if (pcm && pcm_frames > 0) 6353b196c39STakashi Sakamoto update_pcm_pointers(s, pcm, pcm_frames); 6363b196c39STakashi Sakamoto 6373b196c39STakashi Sakamoto return 0; 6383b196c39STakashi Sakamoto } 6393b196c39STakashi Sakamoto 64073fc7f08STakashi Sakamoto /* 64173fc7f08STakashi Sakamoto * In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On 64273fc7f08STakashi Sakamoto * the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent 64373fc7f08STakashi Sakamoto * it. Thus, via Linux firewire subsystem, we can get the 3 bits for second. 64473fc7f08STakashi Sakamoto */ 64573fc7f08STakashi Sakamoto static inline u32 compute_cycle_count(u32 tstamp) 64673fc7f08STakashi Sakamoto { 64773fc7f08STakashi Sakamoto return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff); 64873fc7f08STakashi Sakamoto } 64973fc7f08STakashi Sakamoto 65073fc7f08STakashi Sakamoto static inline u32 increment_cycle_count(u32 cycle, unsigned int addend) 65173fc7f08STakashi Sakamoto { 65273fc7f08STakashi Sakamoto cycle += addend; 65373fc7f08STakashi Sakamoto if (cycle >= 8 * CYCLES_PER_SECOND) 65473fc7f08STakashi Sakamoto cycle -= 8 * CYCLES_PER_SECOND; 65573fc7f08STakashi Sakamoto return cycle; 65673fc7f08STakashi Sakamoto } 65773fc7f08STakashi Sakamoto 658f90e2dedSTakashi Sakamoto static inline u32 decrement_cycle_count(u32 cycle, unsigned int subtrahend) 659f90e2dedSTakashi Sakamoto { 660f90e2dedSTakashi Sakamoto if (cycle < subtrahend) 661f90e2dedSTakashi Sakamoto cycle += 8 * CYCLES_PER_SECOND; 662f90e2dedSTakashi Sakamoto return cycle - subtrahend; 663f90e2dedSTakashi Sakamoto } 664f90e2dedSTakashi Sakamoto 66573fc7f08STakashi Sakamoto static void out_stream_callback(struct fw_iso_context *context, u32 tstamp, 666d67c46b9STakashi Sakamoto size_t header_length, void *header, 667d67c46b9STakashi Sakamoto void *private_data) 668d67c46b9STakashi Sakamoto { 669d67c46b9STakashi Sakamoto struct amdtp_stream *s = private_data; 670390a1512STakashi Sakamoto unsigned int i, packets = header_length / 4; 67173fc7f08STakashi Sakamoto u32 cycle; 672d67c46b9STakashi Sakamoto 673d67c46b9STakashi Sakamoto if (s->packet_index < 0) 674d67c46b9STakashi Sakamoto return; 675d67c46b9STakashi Sakamoto 67673fc7f08STakashi Sakamoto cycle = compute_cycle_count(tstamp); 67773fc7f08STakashi Sakamoto 67873fc7f08STakashi Sakamoto /* Align to actual cycle count for the last packet. */ 67973fc7f08STakashi Sakamoto cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets); 680d67c46b9STakashi Sakamoto 681d67c46b9STakashi Sakamoto for (i = 0; i < packets; ++i) { 68273fc7f08STakashi Sakamoto cycle = increment_cycle_count(cycle, 1); 6833b196c39STakashi Sakamoto if (s->handle_packet(s, 0, cycle, i) < 0) { 684d67c46b9STakashi Sakamoto s->packet_index = -1; 685d67c46b9STakashi Sakamoto amdtp_stream_pcm_abort(s); 686d67c46b9STakashi Sakamoto return; 687d67c46b9STakashi Sakamoto } 688d67c46b9STakashi Sakamoto } 689d67c46b9STakashi Sakamoto 690d67c46b9STakashi Sakamoto fw_iso_context_queue_flush(s->context); 691d67c46b9STakashi Sakamoto } 692d67c46b9STakashi Sakamoto 69373fc7f08STakashi Sakamoto static void in_stream_callback(struct fw_iso_context *context, u32 tstamp, 694d67c46b9STakashi Sakamoto size_t header_length, void *header, 695d67c46b9STakashi Sakamoto void *private_data) 696d67c46b9STakashi Sakamoto { 697d67c46b9STakashi Sakamoto struct amdtp_stream *s = private_data; 698d9a16fc9STakashi Sakamoto unsigned int i, packets; 699ff0fb5aaSTakashi Sakamoto unsigned int payload_length, max_payload_length; 700d9a16fc9STakashi Sakamoto __be32 *headers = header; 701f90e2dedSTakashi Sakamoto u32 cycle; 702d67c46b9STakashi Sakamoto 703d67c46b9STakashi Sakamoto if (s->packet_index < 0) 704d67c46b9STakashi Sakamoto return; 705d67c46b9STakashi Sakamoto 706d67c46b9STakashi Sakamoto /* The number of packets in buffer */ 707d67c46b9STakashi Sakamoto packets = header_length / IN_PACKET_HEADER_SIZE; 708d67c46b9STakashi Sakamoto 709f90e2dedSTakashi Sakamoto cycle = compute_cycle_count(tstamp); 710f90e2dedSTakashi Sakamoto 711f90e2dedSTakashi Sakamoto /* Align to actual cycle count for the last packet. */ 712f90e2dedSTakashi Sakamoto cycle = decrement_cycle_count(cycle, packets); 713f90e2dedSTakashi Sakamoto 714d67c46b9STakashi Sakamoto /* For buffer-over-run prevention. */ 715*f91c9d76STakashi Sakamoto max_payload_length = s->max_payload_length; 716d67c46b9STakashi Sakamoto 717d9a16fc9STakashi Sakamoto for (i = 0; i < packets; i++) { 718f90e2dedSTakashi Sakamoto cycle = increment_cycle_count(cycle, 1); 719d67c46b9STakashi Sakamoto 720*f91c9d76STakashi Sakamoto /* The number of bytes in this packet */ 721ff0fb5aaSTakashi Sakamoto payload_length = 722ff0fb5aaSTakashi Sakamoto (be32_to_cpu(headers[i]) >> ISO_DATA_LENGTH_SHIFT); 723ff0fb5aaSTakashi Sakamoto if (payload_length > max_payload_length) { 724d67c46b9STakashi Sakamoto dev_err(&s->unit->device, 725ff0fb5aaSTakashi Sakamoto "Detect jumbo payload: %04x %04x\n", 726ff0fb5aaSTakashi Sakamoto payload_length, max_payload_length); 727d67c46b9STakashi Sakamoto break; 728d67c46b9STakashi Sakamoto } 729d67c46b9STakashi Sakamoto 7303b196c39STakashi Sakamoto if (s->handle_packet(s, payload_length, cycle, i) < 0) 731d67c46b9STakashi Sakamoto break; 732d67c46b9STakashi Sakamoto } 733d67c46b9STakashi Sakamoto 734dec63cc8STakashi Sakamoto /* Queueing error or detecting invalid payload. */ 735d9a16fc9STakashi Sakamoto if (i < packets) { 736d67c46b9STakashi Sakamoto s->packet_index = -1; 737d67c46b9STakashi Sakamoto amdtp_stream_pcm_abort(s); 738d67c46b9STakashi Sakamoto return; 739d67c46b9STakashi Sakamoto } 740d67c46b9STakashi Sakamoto 741d67c46b9STakashi Sakamoto fw_iso_context_queue_flush(s->context); 742d67c46b9STakashi Sakamoto } 743d67c46b9STakashi Sakamoto 744d67c46b9STakashi Sakamoto /* this is executed one time */ 745d67c46b9STakashi Sakamoto static void amdtp_stream_first_callback(struct fw_iso_context *context, 74673fc7f08STakashi Sakamoto u32 tstamp, size_t header_length, 747d67c46b9STakashi Sakamoto void *header, void *private_data) 748d67c46b9STakashi Sakamoto { 749d67c46b9STakashi Sakamoto struct amdtp_stream *s = private_data; 750a04513f8STakashi Sakamoto u32 cycle; 751a04513f8STakashi Sakamoto unsigned int packets; 752d67c46b9STakashi Sakamoto 753*f91c9d76STakashi Sakamoto s->max_payload_length = amdtp_stream_get_max_payload(s); 754*f91c9d76STakashi Sakamoto 755d67c46b9STakashi Sakamoto /* 756d67c46b9STakashi Sakamoto * For in-stream, first packet has come. 757d67c46b9STakashi Sakamoto * For out-stream, prepared to transmit first packet 758d67c46b9STakashi Sakamoto */ 759d67c46b9STakashi Sakamoto s->callbacked = true; 760d67c46b9STakashi Sakamoto wake_up(&s->callback_wait); 761d67c46b9STakashi Sakamoto 762a04513f8STakashi Sakamoto cycle = compute_cycle_count(tstamp); 763a04513f8STakashi Sakamoto 764a04513f8STakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 765a04513f8STakashi Sakamoto packets = header_length / IN_PACKET_HEADER_SIZE; 766a04513f8STakashi Sakamoto cycle = decrement_cycle_count(cycle, packets); 767d67c46b9STakashi Sakamoto context->callback.sc = in_stream_callback; 7683b196c39STakashi Sakamoto if (s->flags & CIP_NO_HEADER) 7693b196c39STakashi Sakamoto s->handle_packet = handle_in_packet_without_header; 7703b196c39STakashi Sakamoto else 7713b196c39STakashi Sakamoto s->handle_packet = handle_in_packet; 772a04513f8STakashi Sakamoto } else { 773a04513f8STakashi Sakamoto packets = header_length / 4; 774a04513f8STakashi Sakamoto cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets); 775d67c46b9STakashi Sakamoto context->callback.sc = out_stream_callback; 7763b196c39STakashi Sakamoto if (s->flags & CIP_NO_HEADER) 7773b196c39STakashi Sakamoto s->handle_packet = handle_out_packet_without_header; 7783b196c39STakashi Sakamoto else 7793b196c39STakashi Sakamoto s->handle_packet = handle_out_packet; 780a04513f8STakashi Sakamoto } 781a04513f8STakashi Sakamoto 782a04513f8STakashi Sakamoto s->start_cycle = cycle; 783d67c46b9STakashi Sakamoto 78473fc7f08STakashi Sakamoto context->callback.sc(context, tstamp, header_length, header, s); 785d67c46b9STakashi Sakamoto } 786d67c46b9STakashi Sakamoto 787d67c46b9STakashi Sakamoto /** 788d67c46b9STakashi Sakamoto * amdtp_stream_start - start transferring packets 789d67c46b9STakashi Sakamoto * @s: the AMDTP stream to start 790d67c46b9STakashi Sakamoto * @channel: the isochronous channel on the bus 791d67c46b9STakashi Sakamoto * @speed: firewire speed code 792d67c46b9STakashi Sakamoto * 793d67c46b9STakashi Sakamoto * The stream cannot be started until it has been configured with 794d67c46b9STakashi Sakamoto * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI 795d67c46b9STakashi Sakamoto * device can be started. 796d67c46b9STakashi Sakamoto */ 797d67c46b9STakashi Sakamoto int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed) 798d67c46b9STakashi Sakamoto { 799d67c46b9STakashi Sakamoto static const struct { 800d67c46b9STakashi Sakamoto unsigned int data_block; 801d67c46b9STakashi Sakamoto unsigned int syt_offset; 802d67c46b9STakashi Sakamoto } initial_state[] = { 803d67c46b9STakashi Sakamoto [CIP_SFC_32000] = { 4, 3072 }, 804d67c46b9STakashi Sakamoto [CIP_SFC_48000] = { 6, 1024 }, 805d67c46b9STakashi Sakamoto [CIP_SFC_96000] = { 12, 1024 }, 806d67c46b9STakashi Sakamoto [CIP_SFC_192000] = { 24, 1024 }, 807d67c46b9STakashi Sakamoto [CIP_SFC_44100] = { 0, 67 }, 808d67c46b9STakashi Sakamoto [CIP_SFC_88200] = { 0, 67 }, 809d67c46b9STakashi Sakamoto [CIP_SFC_176400] = { 0, 67 }, 810d67c46b9STakashi Sakamoto }; 811d67c46b9STakashi Sakamoto unsigned int header_size; 812d67c46b9STakashi Sakamoto enum dma_data_direction dir; 813d67c46b9STakashi Sakamoto int type, tag, err; 814d67c46b9STakashi Sakamoto 815d67c46b9STakashi Sakamoto mutex_lock(&s->mutex); 816d67c46b9STakashi Sakamoto 817d67c46b9STakashi Sakamoto if (WARN_ON(amdtp_stream_running(s) || 818d67c46b9STakashi Sakamoto (s->data_block_quadlets < 1))) { 819d67c46b9STakashi Sakamoto err = -EBADFD; 820d67c46b9STakashi Sakamoto goto err_unlock; 821d67c46b9STakashi Sakamoto } 822d67c46b9STakashi Sakamoto 82362f00e40STakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) 824d67c46b9STakashi Sakamoto s->data_block_counter = UINT_MAX; 825d67c46b9STakashi Sakamoto else 826d67c46b9STakashi Sakamoto s->data_block_counter = 0; 827d67c46b9STakashi Sakamoto s->data_block_state = initial_state[s->sfc].data_block; 828d67c46b9STakashi Sakamoto s->syt_offset_state = initial_state[s->sfc].syt_offset; 829d67c46b9STakashi Sakamoto s->last_syt_offset = TICKS_PER_CYCLE; 830d67c46b9STakashi Sakamoto 831d67c46b9STakashi Sakamoto /* initialize packet buffer */ 832d67c46b9STakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 833d67c46b9STakashi Sakamoto dir = DMA_FROM_DEVICE; 834d67c46b9STakashi Sakamoto type = FW_ISO_CONTEXT_RECEIVE; 835d67c46b9STakashi Sakamoto header_size = IN_PACKET_HEADER_SIZE; 836d67c46b9STakashi Sakamoto } else { 837d67c46b9STakashi Sakamoto dir = DMA_TO_DEVICE; 838d67c46b9STakashi Sakamoto type = FW_ISO_CONTEXT_TRANSMIT; 839d67c46b9STakashi Sakamoto header_size = OUT_PACKET_HEADER_SIZE; 840d67c46b9STakashi Sakamoto } 841d67c46b9STakashi Sakamoto err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH, 842d67c46b9STakashi Sakamoto amdtp_stream_get_max_payload(s), dir); 843d67c46b9STakashi Sakamoto if (err < 0) 844d67c46b9STakashi Sakamoto goto err_unlock; 845d67c46b9STakashi Sakamoto 846d67c46b9STakashi Sakamoto s->context = fw_iso_context_create(fw_parent_device(s->unit)->card, 847d67c46b9STakashi Sakamoto type, channel, speed, header_size, 848d67c46b9STakashi Sakamoto amdtp_stream_first_callback, s); 849d67c46b9STakashi Sakamoto if (IS_ERR(s->context)) { 850d67c46b9STakashi Sakamoto err = PTR_ERR(s->context); 851d67c46b9STakashi Sakamoto if (err == -EBUSY) 852d67c46b9STakashi Sakamoto dev_err(&s->unit->device, 853d67c46b9STakashi Sakamoto "no free stream on this controller\n"); 854d67c46b9STakashi Sakamoto goto err_buffer; 855d67c46b9STakashi Sakamoto } 856d67c46b9STakashi Sakamoto 857d67c46b9STakashi Sakamoto amdtp_stream_update(s); 858d67c46b9STakashi Sakamoto 8593b196c39STakashi Sakamoto if (s->flags & CIP_NO_HEADER) 8603b196c39STakashi Sakamoto s->tag = TAG_NO_CIP_HEADER; 8613b196c39STakashi Sakamoto else 8623b196c39STakashi Sakamoto s->tag = TAG_CIP; 8633b196c39STakashi Sakamoto 864d67c46b9STakashi Sakamoto s->packet_index = 0; 865d67c46b9STakashi Sakamoto do { 866d67c46b9STakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) 867d67c46b9STakashi Sakamoto err = queue_in_packet(s); 868d67c46b9STakashi Sakamoto else 869ff38e0c7STakashi Sakamoto err = queue_out_packet(s, 0); 870d67c46b9STakashi Sakamoto if (err < 0) 871d67c46b9STakashi Sakamoto goto err_context; 872d67c46b9STakashi Sakamoto } while (s->packet_index > 0); 873d67c46b9STakashi Sakamoto 874d67c46b9STakashi Sakamoto /* NOTE: TAG1 matches CIP. This just affects in stream. */ 875d67c46b9STakashi Sakamoto tag = FW_ISO_CONTEXT_MATCH_TAG1; 8763b196c39STakashi Sakamoto if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER)) 877d67c46b9STakashi Sakamoto tag |= FW_ISO_CONTEXT_MATCH_TAG0; 878d67c46b9STakashi Sakamoto 879d67c46b9STakashi Sakamoto s->callbacked = false; 880d67c46b9STakashi Sakamoto err = fw_iso_context_start(s->context, -1, 0, tag); 881d67c46b9STakashi Sakamoto if (err < 0) 882d67c46b9STakashi Sakamoto goto err_context; 883d67c46b9STakashi Sakamoto 884d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 885d67c46b9STakashi Sakamoto 886d67c46b9STakashi Sakamoto return 0; 887d67c46b9STakashi Sakamoto 888d67c46b9STakashi Sakamoto err_context: 889d67c46b9STakashi Sakamoto fw_iso_context_destroy(s->context); 890d67c46b9STakashi Sakamoto s->context = ERR_PTR(-1); 891d67c46b9STakashi Sakamoto err_buffer: 892d67c46b9STakashi Sakamoto iso_packets_buffer_destroy(&s->buffer, s->unit); 893d67c46b9STakashi Sakamoto err_unlock: 894d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 895d67c46b9STakashi Sakamoto 896d67c46b9STakashi Sakamoto return err; 897d67c46b9STakashi Sakamoto } 898d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_start); 899d67c46b9STakashi Sakamoto 900d67c46b9STakashi Sakamoto /** 901d67c46b9STakashi Sakamoto * amdtp_stream_pcm_pointer - get the PCM buffer position 902d67c46b9STakashi Sakamoto * @s: the AMDTP stream that transports the PCM data 903d67c46b9STakashi Sakamoto * 904d67c46b9STakashi Sakamoto * Returns the current buffer position, in frames. 905d67c46b9STakashi Sakamoto */ 906d67c46b9STakashi Sakamoto unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s) 907d67c46b9STakashi Sakamoto { 9081dba9db0STakashi Sakamoto /* 9091dba9db0STakashi Sakamoto * This function is called in software IRQ context of period_tasklet or 9101dba9db0STakashi Sakamoto * process context. 9111dba9db0STakashi Sakamoto * 9121dba9db0STakashi Sakamoto * When the software IRQ context was scheduled by software IRQ context 9131dba9db0STakashi Sakamoto * of IR/IT contexts, queued packets were already handled. Therefore, 9141dba9db0STakashi Sakamoto * no need to flush the queue in buffer anymore. 9151dba9db0STakashi Sakamoto * 9161dba9db0STakashi Sakamoto * When the process context reach here, some packets will be already 9171dba9db0STakashi Sakamoto * queued in the buffer. These packets should be handled immediately 9181dba9db0STakashi Sakamoto * to keep better granularity of PCM pointer. 9191dba9db0STakashi Sakamoto * 9201dba9db0STakashi Sakamoto * Later, the process context will sometimes schedules software IRQ 9211dba9db0STakashi Sakamoto * context of the period_tasklet. Then, no need to flush the queue by 9221dba9db0STakashi Sakamoto * the same reason as described for IR/IT contexts. 9231dba9db0STakashi Sakamoto */ 9241dba9db0STakashi Sakamoto if (!in_interrupt() && amdtp_stream_running(s)) 925d67c46b9STakashi Sakamoto fw_iso_context_flush_completions(s->context); 926d67c46b9STakashi Sakamoto 927d67c46b9STakashi Sakamoto return ACCESS_ONCE(s->pcm_buffer_pointer); 928d67c46b9STakashi Sakamoto } 929d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_pointer); 930d67c46b9STakashi Sakamoto 931d67c46b9STakashi Sakamoto /** 932d67c46b9STakashi Sakamoto * amdtp_stream_update - update the stream after a bus reset 933d67c46b9STakashi Sakamoto * @s: the AMDTP stream 934d67c46b9STakashi Sakamoto */ 935d67c46b9STakashi Sakamoto void amdtp_stream_update(struct amdtp_stream *s) 936d67c46b9STakashi Sakamoto { 937d67c46b9STakashi Sakamoto /* Precomputing. */ 938d67c46b9STakashi Sakamoto ACCESS_ONCE(s->source_node_id_field) = 939d67c46b9STakashi Sakamoto (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & 940d67c46b9STakashi Sakamoto CIP_SID_MASK; 941d67c46b9STakashi Sakamoto } 942d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_update); 943d67c46b9STakashi Sakamoto 944d67c46b9STakashi Sakamoto /** 945d67c46b9STakashi Sakamoto * amdtp_stream_stop - stop sending packets 946d67c46b9STakashi Sakamoto * @s: the AMDTP stream to stop 947d67c46b9STakashi Sakamoto * 948d67c46b9STakashi Sakamoto * All PCM and MIDI devices of the stream must be stopped before the stream 949d67c46b9STakashi Sakamoto * itself can be stopped. 950d67c46b9STakashi Sakamoto */ 951d67c46b9STakashi Sakamoto void amdtp_stream_stop(struct amdtp_stream *s) 952d67c46b9STakashi Sakamoto { 953d67c46b9STakashi Sakamoto mutex_lock(&s->mutex); 954d67c46b9STakashi Sakamoto 955d67c46b9STakashi Sakamoto if (!amdtp_stream_running(s)) { 956d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 957d67c46b9STakashi Sakamoto return; 958d67c46b9STakashi Sakamoto } 959d67c46b9STakashi Sakamoto 960d67c46b9STakashi Sakamoto tasklet_kill(&s->period_tasklet); 961d67c46b9STakashi Sakamoto fw_iso_context_stop(s->context); 962d67c46b9STakashi Sakamoto fw_iso_context_destroy(s->context); 963d67c46b9STakashi Sakamoto s->context = ERR_PTR(-1); 964d67c46b9STakashi Sakamoto iso_packets_buffer_destroy(&s->buffer, s->unit); 965d67c46b9STakashi Sakamoto 966d67c46b9STakashi Sakamoto s->callbacked = false; 967d67c46b9STakashi Sakamoto 968d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 969d67c46b9STakashi Sakamoto } 970d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_stop); 971d67c46b9STakashi Sakamoto 972d67c46b9STakashi Sakamoto /** 973d67c46b9STakashi Sakamoto * amdtp_stream_pcm_abort - abort the running PCM device 974d67c46b9STakashi Sakamoto * @s: the AMDTP stream about to be stopped 975d67c46b9STakashi Sakamoto * 976d67c46b9STakashi Sakamoto * If the isochronous stream needs to be stopped asynchronously, call this 977d67c46b9STakashi Sakamoto * function first to stop the PCM device. 978d67c46b9STakashi Sakamoto */ 979d67c46b9STakashi Sakamoto void amdtp_stream_pcm_abort(struct amdtp_stream *s) 980d67c46b9STakashi Sakamoto { 981d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm; 982d67c46b9STakashi Sakamoto 983d67c46b9STakashi Sakamoto pcm = ACCESS_ONCE(s->pcm); 984d67c46b9STakashi Sakamoto if (pcm) 985d67c46b9STakashi Sakamoto snd_pcm_stop_xrun(pcm); 986d67c46b9STakashi Sakamoto } 987d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_abort); 988