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 { 151*55799c5aSTakashi Sakamoto struct snd_pcm_hardware *hw = &runtime->hw; 152d67c46b9STakashi Sakamoto int err; 153d67c46b9STakashi Sakamoto 154*55799c5aSTakashi Sakamoto hw->info = SNDRV_PCM_INFO_BATCH | 155*55799c5aSTakashi Sakamoto SNDRV_PCM_INFO_BLOCK_TRANSFER | 156*55799c5aSTakashi Sakamoto SNDRV_PCM_INFO_INTERLEAVED | 157*55799c5aSTakashi Sakamoto SNDRV_PCM_INFO_JOINT_DUPLEX | 158*55799c5aSTakashi Sakamoto SNDRV_PCM_INFO_MMAP | 159*55799c5aSTakashi Sakamoto SNDRV_PCM_INFO_MMAP_VALID; 160*55799c5aSTakashi Sakamoto 161*55799c5aSTakashi Sakamoto /* SNDRV_PCM_INFO_BATCH */ 162*55799c5aSTakashi Sakamoto hw->periods_min = 2; 163*55799c5aSTakashi Sakamoto hw->periods_max = UINT_MAX; 164*55799c5aSTakashi Sakamoto 165*55799c5aSTakashi Sakamoto /* bytes for a frame */ 166*55799c5aSTakashi Sakamoto hw->period_bytes_min = 4 * hw->channels_max; 167*55799c5aSTakashi Sakamoto 168*55799c5aSTakashi Sakamoto /* Just to prevent from allocating much pages. */ 169*55799c5aSTakashi Sakamoto hw->period_bytes_max = hw->period_bytes_min * 2048; 170*55799c5aSTakashi Sakamoto hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min; 171*55799c5aSTakashi Sakamoto 172d67c46b9STakashi Sakamoto /* 173d67c46b9STakashi Sakamoto * Currently firewire-lib processes 16 packets in one software 174d67c46b9STakashi Sakamoto * interrupt callback. This equals to 2msec but actually the 175d67c46b9STakashi Sakamoto * interval of the interrupts has a jitter. 176d67c46b9STakashi Sakamoto * Additionally, even if adding a constraint to fit period size to 177d67c46b9STakashi Sakamoto * 2msec, actual calculated frames per period doesn't equal to 2msec, 178d67c46b9STakashi Sakamoto * depending on sampling rate. 179d67c46b9STakashi Sakamoto * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec. 180d67c46b9STakashi Sakamoto * Here let us use 5msec for safe period interrupt. 181d67c46b9STakashi Sakamoto */ 182d67c46b9STakashi Sakamoto err = snd_pcm_hw_constraint_minmax(runtime, 183d67c46b9STakashi Sakamoto SNDRV_PCM_HW_PARAM_PERIOD_TIME, 184d67c46b9STakashi Sakamoto 5000, UINT_MAX); 185d67c46b9STakashi Sakamoto if (err < 0) 186d67c46b9STakashi Sakamoto goto end; 187d67c46b9STakashi Sakamoto 188d67c46b9STakashi Sakamoto /* Non-Blocking stream has no more constraints */ 189d67c46b9STakashi Sakamoto if (!(s->flags & CIP_BLOCKING)) 190d67c46b9STakashi Sakamoto goto end; 191d67c46b9STakashi Sakamoto 192d67c46b9STakashi Sakamoto /* 193d67c46b9STakashi Sakamoto * One AMDTP packet can include some frames. In blocking mode, the 194d67c46b9STakashi Sakamoto * number equals to SYT_INTERVAL. So the number is 8, 16 or 32, 195d67c46b9STakashi Sakamoto * depending on its sampling rate. For accurate period interrupt, it's 196d67c46b9STakashi Sakamoto * preferrable to align period/buffer sizes to current SYT_INTERVAL. 197d67c46b9STakashi Sakamoto * 198d67c46b9STakashi Sakamoto * TODO: These constraints can be improved with proper rules. 199d67c46b9STakashi Sakamoto * Currently apply LCM of SYT_INTERVALs. 200d67c46b9STakashi Sakamoto */ 201d67c46b9STakashi Sakamoto err = snd_pcm_hw_constraint_step(runtime, 0, 202d67c46b9STakashi Sakamoto SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32); 203d67c46b9STakashi Sakamoto if (err < 0) 204d67c46b9STakashi Sakamoto goto end; 205d67c46b9STakashi Sakamoto err = snd_pcm_hw_constraint_step(runtime, 0, 206d67c46b9STakashi Sakamoto SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32); 207d67c46b9STakashi Sakamoto end: 208d67c46b9STakashi Sakamoto return err; 209d67c46b9STakashi Sakamoto } 210d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints); 211d67c46b9STakashi Sakamoto 212d67c46b9STakashi Sakamoto /** 213d67c46b9STakashi Sakamoto * amdtp_stream_set_parameters - set stream parameters 214d67c46b9STakashi Sakamoto * @s: the AMDTP stream to configure 215d67c46b9STakashi Sakamoto * @rate: the sample rate 216df075feeSTakashi Sakamoto * @data_block_quadlets: the size of a data block in quadlet unit 217d67c46b9STakashi Sakamoto * 218d67c46b9STakashi Sakamoto * The parameters must be set before the stream is started, and must not be 219d67c46b9STakashi Sakamoto * changed while the stream is running. 220d67c46b9STakashi Sakamoto */ 221df075feeSTakashi Sakamoto int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate, 222df075feeSTakashi Sakamoto unsigned int data_block_quadlets) 223d67c46b9STakashi Sakamoto { 224df075feeSTakashi Sakamoto unsigned int sfc; 225d67c46b9STakashi Sakamoto 226d67c46b9STakashi Sakamoto for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) { 227d67c46b9STakashi Sakamoto if (amdtp_rate_table[sfc] == rate) 228d67c46b9STakashi Sakamoto break; 229d67c46b9STakashi Sakamoto } 230d67c46b9STakashi Sakamoto if (sfc == ARRAY_SIZE(amdtp_rate_table)) 231d67c46b9STakashi Sakamoto return -EINVAL; 232d67c46b9STakashi Sakamoto 233d67c46b9STakashi Sakamoto s->sfc = sfc; 234df075feeSTakashi Sakamoto s->data_block_quadlets = data_block_quadlets; 235d67c46b9STakashi Sakamoto s->syt_interval = amdtp_syt_intervals[sfc]; 236d67c46b9STakashi Sakamoto 237d67c46b9STakashi Sakamoto /* default buffering in the device */ 238d67c46b9STakashi Sakamoto s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE; 239d67c46b9STakashi Sakamoto if (s->flags & CIP_BLOCKING) 240d67c46b9STakashi Sakamoto /* additional buffering needed to adjust for no-data packets */ 241d67c46b9STakashi Sakamoto s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate; 242d67c46b9STakashi Sakamoto 243d67c46b9STakashi Sakamoto return 0; 244d67c46b9STakashi Sakamoto } 245d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_set_parameters); 246d67c46b9STakashi Sakamoto 247d67c46b9STakashi Sakamoto /** 248d67c46b9STakashi Sakamoto * amdtp_stream_get_max_payload - get the stream's packet size 249d67c46b9STakashi Sakamoto * @s: the AMDTP stream 250d67c46b9STakashi Sakamoto * 251d67c46b9STakashi Sakamoto * This function must not be called before the stream has been configured 252d67c46b9STakashi Sakamoto * with amdtp_stream_set_parameters(). 253d67c46b9STakashi Sakamoto */ 254d67c46b9STakashi Sakamoto unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s) 255d67c46b9STakashi Sakamoto { 256d67c46b9STakashi Sakamoto unsigned int multiplier = 1; 2573b196c39STakashi Sakamoto unsigned int header_size = 0; 258d67c46b9STakashi Sakamoto 259d67c46b9STakashi Sakamoto if (s->flags & CIP_JUMBO_PAYLOAD) 260d67c46b9STakashi Sakamoto multiplier = 5; 2613b196c39STakashi Sakamoto if (!(s->flags & CIP_NO_HEADER)) 2623b196c39STakashi Sakamoto header_size = 8; 263d67c46b9STakashi Sakamoto 2643b196c39STakashi Sakamoto return header_size + 2653b196c39STakashi Sakamoto s->syt_interval * s->data_block_quadlets * 4 * multiplier; 266d67c46b9STakashi Sakamoto } 267d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_get_max_payload); 268d67c46b9STakashi Sakamoto 269d67c46b9STakashi Sakamoto /** 270d67c46b9STakashi Sakamoto * amdtp_stream_pcm_prepare - prepare PCM device for running 271d67c46b9STakashi Sakamoto * @s: the AMDTP stream 272d67c46b9STakashi Sakamoto * 273d67c46b9STakashi Sakamoto * This function should be called from the PCM device's .prepare callback. 274d67c46b9STakashi Sakamoto */ 275d67c46b9STakashi Sakamoto void amdtp_stream_pcm_prepare(struct amdtp_stream *s) 276d67c46b9STakashi Sakamoto { 277d67c46b9STakashi Sakamoto tasklet_kill(&s->period_tasklet); 278d67c46b9STakashi Sakamoto s->pcm_buffer_pointer = 0; 279d67c46b9STakashi Sakamoto s->pcm_period_pointer = 0; 280d67c46b9STakashi Sakamoto } 281d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_prepare); 282d67c46b9STakashi Sakamoto 283d67c46b9STakashi Sakamoto static unsigned int calculate_data_blocks(struct amdtp_stream *s, 284d67c46b9STakashi Sakamoto unsigned int syt) 285d67c46b9STakashi Sakamoto { 286d67c46b9STakashi Sakamoto unsigned int phase, data_blocks; 287d67c46b9STakashi Sakamoto 288d67c46b9STakashi Sakamoto /* Blocking mode. */ 289d67c46b9STakashi Sakamoto if (s->flags & CIP_BLOCKING) { 290d67c46b9STakashi Sakamoto /* This module generate empty packet for 'no data'. */ 291d67c46b9STakashi Sakamoto if (syt == CIP_SYT_NO_INFO) 292d67c46b9STakashi Sakamoto data_blocks = 0; 293d67c46b9STakashi Sakamoto else 294d67c46b9STakashi Sakamoto data_blocks = s->syt_interval; 295d67c46b9STakashi Sakamoto /* Non-blocking mode. */ 296d67c46b9STakashi Sakamoto } else { 297d67c46b9STakashi Sakamoto if (!cip_sfc_is_base_44100(s->sfc)) { 298d67c46b9STakashi Sakamoto /* Sample_rate / 8000 is an integer, and precomputed. */ 299d67c46b9STakashi Sakamoto data_blocks = s->data_block_state; 300d67c46b9STakashi Sakamoto } else { 301d67c46b9STakashi Sakamoto phase = s->data_block_state; 302d67c46b9STakashi Sakamoto 303d67c46b9STakashi Sakamoto /* 304d67c46b9STakashi Sakamoto * This calculates the number of data blocks per packet so that 305d67c46b9STakashi Sakamoto * 1) the overall rate is correct and exactly synchronized to 306d67c46b9STakashi Sakamoto * the bus clock, and 307d67c46b9STakashi Sakamoto * 2) packets with a rounded-up number of blocks occur as early 308d67c46b9STakashi Sakamoto * as possible in the sequence (to prevent underruns of the 309d67c46b9STakashi Sakamoto * device's buffer). 310d67c46b9STakashi Sakamoto */ 311d67c46b9STakashi Sakamoto if (s->sfc == CIP_SFC_44100) 312d67c46b9STakashi Sakamoto /* 6 6 5 6 5 6 5 ... */ 313d67c46b9STakashi Sakamoto data_blocks = 5 + ((phase & 1) ^ 314d67c46b9STakashi Sakamoto (phase == 0 || phase >= 40)); 315d67c46b9STakashi Sakamoto else 316d67c46b9STakashi Sakamoto /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */ 317d67c46b9STakashi Sakamoto data_blocks = 11 * (s->sfc >> 1) + (phase == 0); 318d67c46b9STakashi Sakamoto if (++phase >= (80 >> (s->sfc >> 1))) 319d67c46b9STakashi Sakamoto phase = 0; 320d67c46b9STakashi Sakamoto s->data_block_state = phase; 321d67c46b9STakashi Sakamoto } 322d67c46b9STakashi Sakamoto } 323d67c46b9STakashi Sakamoto 324d67c46b9STakashi Sakamoto return data_blocks; 325d67c46b9STakashi Sakamoto } 326d67c46b9STakashi Sakamoto 327d67c46b9STakashi Sakamoto static unsigned int calculate_syt(struct amdtp_stream *s, 328d67c46b9STakashi Sakamoto unsigned int cycle) 329d67c46b9STakashi Sakamoto { 330d67c46b9STakashi Sakamoto unsigned int syt_offset, phase, index, syt; 331d67c46b9STakashi Sakamoto 332d67c46b9STakashi Sakamoto if (s->last_syt_offset < TICKS_PER_CYCLE) { 333d67c46b9STakashi Sakamoto if (!cip_sfc_is_base_44100(s->sfc)) 334d67c46b9STakashi Sakamoto syt_offset = s->last_syt_offset + s->syt_offset_state; 335d67c46b9STakashi Sakamoto else { 336d67c46b9STakashi Sakamoto /* 337d67c46b9STakashi Sakamoto * The time, in ticks, of the n'th SYT_INTERVAL sample is: 338d67c46b9STakashi Sakamoto * n * SYT_INTERVAL * 24576000 / sample_rate 339d67c46b9STakashi Sakamoto * Modulo TICKS_PER_CYCLE, the difference between successive 340d67c46b9STakashi Sakamoto * elements is about 1386.23. Rounding the results of this 341d67c46b9STakashi Sakamoto * formula to the SYT precision results in a sequence of 342d67c46b9STakashi Sakamoto * differences that begins with: 343d67c46b9STakashi Sakamoto * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ... 344d67c46b9STakashi Sakamoto * This code generates _exactly_ the same sequence. 345d67c46b9STakashi Sakamoto */ 346d67c46b9STakashi Sakamoto phase = s->syt_offset_state; 347d67c46b9STakashi Sakamoto index = phase % 13; 348d67c46b9STakashi Sakamoto syt_offset = s->last_syt_offset; 349d67c46b9STakashi Sakamoto syt_offset += 1386 + ((index && !(index & 3)) || 350d67c46b9STakashi Sakamoto phase == 146); 351d67c46b9STakashi Sakamoto if (++phase >= 147) 352d67c46b9STakashi Sakamoto phase = 0; 353d67c46b9STakashi Sakamoto s->syt_offset_state = phase; 354d67c46b9STakashi Sakamoto } 355d67c46b9STakashi Sakamoto } else 356d67c46b9STakashi Sakamoto syt_offset = s->last_syt_offset - TICKS_PER_CYCLE; 357d67c46b9STakashi Sakamoto s->last_syt_offset = syt_offset; 358d67c46b9STakashi Sakamoto 359d67c46b9STakashi Sakamoto if (syt_offset < TICKS_PER_CYCLE) { 360d67c46b9STakashi Sakamoto syt_offset += s->transfer_delay; 361d67c46b9STakashi Sakamoto syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12; 362d67c46b9STakashi Sakamoto syt += syt_offset % TICKS_PER_CYCLE; 363d67c46b9STakashi Sakamoto 364d67c46b9STakashi Sakamoto return syt & CIP_SYT_MASK; 365d67c46b9STakashi Sakamoto } else { 366d67c46b9STakashi Sakamoto return CIP_SYT_NO_INFO; 367d67c46b9STakashi Sakamoto } 368d67c46b9STakashi Sakamoto } 369d67c46b9STakashi Sakamoto 370d67c46b9STakashi Sakamoto static void update_pcm_pointers(struct amdtp_stream *s, 371d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm, 372d67c46b9STakashi Sakamoto unsigned int frames) 373d67c46b9STakashi Sakamoto { 374d67c46b9STakashi Sakamoto unsigned int ptr; 375d67c46b9STakashi Sakamoto 376d67c46b9STakashi Sakamoto ptr = s->pcm_buffer_pointer + frames; 377d67c46b9STakashi Sakamoto if (ptr >= pcm->runtime->buffer_size) 378d67c46b9STakashi Sakamoto ptr -= pcm->runtime->buffer_size; 379d67c46b9STakashi Sakamoto ACCESS_ONCE(s->pcm_buffer_pointer) = ptr; 380d67c46b9STakashi Sakamoto 381d67c46b9STakashi Sakamoto s->pcm_period_pointer += frames; 382d67c46b9STakashi Sakamoto if (s->pcm_period_pointer >= pcm->runtime->period_size) { 383d67c46b9STakashi Sakamoto s->pcm_period_pointer -= pcm->runtime->period_size; 384d67c46b9STakashi Sakamoto tasklet_hi_schedule(&s->period_tasklet); 385d67c46b9STakashi Sakamoto } 386d67c46b9STakashi Sakamoto } 387d67c46b9STakashi Sakamoto 388d67c46b9STakashi Sakamoto static void pcm_period_tasklet(unsigned long data) 389d67c46b9STakashi Sakamoto { 390d67c46b9STakashi Sakamoto struct amdtp_stream *s = (void *)data; 391d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm); 392d67c46b9STakashi Sakamoto 393d67c46b9STakashi Sakamoto if (pcm) 394d67c46b9STakashi Sakamoto snd_pcm_period_elapsed(pcm); 395d67c46b9STakashi Sakamoto } 396d67c46b9STakashi Sakamoto 397ff38e0c7STakashi Sakamoto static int queue_packet(struct amdtp_stream *s, unsigned int header_length, 398ff38e0c7STakashi Sakamoto unsigned int payload_length) 399d67c46b9STakashi Sakamoto { 400d67c46b9STakashi Sakamoto struct fw_iso_packet p = {0}; 401d67c46b9STakashi Sakamoto int err = 0; 402d67c46b9STakashi Sakamoto 403d67c46b9STakashi Sakamoto if (IS_ERR(s->context)) 404d67c46b9STakashi Sakamoto goto end; 405d67c46b9STakashi Sakamoto 406d67c46b9STakashi Sakamoto p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL); 4073b196c39STakashi Sakamoto p.tag = s->tag; 408d67c46b9STakashi Sakamoto p.header_length = header_length; 409ff38e0c7STakashi Sakamoto if (payload_length > 0) 410ff38e0c7STakashi Sakamoto p.payload_length = payload_length; 411ff38e0c7STakashi Sakamoto else 412ff38e0c7STakashi Sakamoto p.skip = true; 413d67c46b9STakashi Sakamoto err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer, 414d67c46b9STakashi Sakamoto s->buffer.packets[s->packet_index].offset); 415d67c46b9STakashi Sakamoto if (err < 0) { 416d67c46b9STakashi Sakamoto dev_err(&s->unit->device, "queueing error: %d\n", err); 417d67c46b9STakashi Sakamoto goto end; 418d67c46b9STakashi Sakamoto } 419d67c46b9STakashi Sakamoto 420d67c46b9STakashi Sakamoto if (++s->packet_index >= QUEUE_LENGTH) 421d67c46b9STakashi Sakamoto s->packet_index = 0; 422d67c46b9STakashi Sakamoto end: 423d67c46b9STakashi Sakamoto return err; 424d67c46b9STakashi Sakamoto } 425d67c46b9STakashi Sakamoto 426d67c46b9STakashi Sakamoto static inline int queue_out_packet(struct amdtp_stream *s, 427ff38e0c7STakashi Sakamoto unsigned int payload_length) 428d67c46b9STakashi Sakamoto { 429ff38e0c7STakashi Sakamoto return queue_packet(s, OUT_PACKET_HEADER_SIZE, payload_length); 430d67c46b9STakashi Sakamoto } 431d67c46b9STakashi Sakamoto 432d67c46b9STakashi Sakamoto static inline int queue_in_packet(struct amdtp_stream *s) 433d67c46b9STakashi Sakamoto { 434f91c9d76STakashi Sakamoto return queue_packet(s, IN_PACKET_HEADER_SIZE, s->max_payload_length); 435d67c46b9STakashi Sakamoto } 436d67c46b9STakashi Sakamoto 437ff0fb5aaSTakashi Sakamoto static int handle_out_packet(struct amdtp_stream *s, 438ff0fb5aaSTakashi Sakamoto unsigned int payload_length, unsigned int cycle, 439a9c4284bSTakashi Sakamoto unsigned int index) 440d67c46b9STakashi Sakamoto { 441d67c46b9STakashi Sakamoto __be32 *buffer; 442390a1512STakashi Sakamoto unsigned int syt; 443390a1512STakashi Sakamoto unsigned int data_blocks; 444d67c46b9STakashi Sakamoto unsigned int pcm_frames; 445d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm; 446d67c46b9STakashi Sakamoto 447d67c46b9STakashi Sakamoto buffer = s->buffer.packets[s->packet_index].buffer; 448390a1512STakashi Sakamoto syt = calculate_syt(s, cycle); 449390a1512STakashi Sakamoto data_blocks = calculate_data_blocks(s, syt); 450df075feeSTakashi Sakamoto pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt); 451d67c46b9STakashi Sakamoto 4529dae017bSTakashi Sakamoto if (s->flags & CIP_DBC_IS_END_EVENT) 4539dae017bSTakashi Sakamoto s->data_block_counter = 4549dae017bSTakashi Sakamoto (s->data_block_counter + data_blocks) & 0xff; 4559dae017bSTakashi Sakamoto 456d67c46b9STakashi Sakamoto buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) | 457d67c46b9STakashi Sakamoto (s->data_block_quadlets << CIP_DBS_SHIFT) | 4589863874fSTakashi Sakamoto ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) | 459d67c46b9STakashi Sakamoto s->data_block_counter); 460d67c46b9STakashi Sakamoto buffer[1] = cpu_to_be32(CIP_EOH | 461d67c46b9STakashi Sakamoto ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) | 462d67c46b9STakashi Sakamoto ((s->fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) | 463d67c46b9STakashi Sakamoto (syt & CIP_SYT_MASK)); 464d67c46b9STakashi Sakamoto 4659dae017bSTakashi Sakamoto if (!(s->flags & CIP_DBC_IS_END_EVENT)) 4669dae017bSTakashi Sakamoto s->data_block_counter = 4679dae017bSTakashi Sakamoto (s->data_block_counter + data_blocks) & 0xff; 468d67c46b9STakashi Sakamoto payload_length = 8 + data_blocks * 4 * s->data_block_quadlets; 4690c95c1d6STakashi Sakamoto 470a9c4284bSTakashi Sakamoto trace_out_packet(s, cycle, buffer, payload_length, index); 4710c95c1d6STakashi Sakamoto 472ff38e0c7STakashi Sakamoto if (queue_out_packet(s, payload_length) < 0) 473d67c46b9STakashi Sakamoto return -EIO; 474d67c46b9STakashi Sakamoto 475d67c46b9STakashi Sakamoto pcm = ACCESS_ONCE(s->pcm); 476d67c46b9STakashi Sakamoto if (pcm && pcm_frames > 0) 477d67c46b9STakashi Sakamoto update_pcm_pointers(s, pcm, pcm_frames); 478d67c46b9STakashi Sakamoto 479d67c46b9STakashi Sakamoto /* No need to return the number of handled data blocks. */ 480d67c46b9STakashi Sakamoto return 0; 481d67c46b9STakashi Sakamoto } 482d67c46b9STakashi Sakamoto 4833b196c39STakashi Sakamoto static int handle_out_packet_without_header(struct amdtp_stream *s, 4843b196c39STakashi Sakamoto unsigned int payload_length, unsigned int cycle, 4853b196c39STakashi Sakamoto unsigned int index) 4863b196c39STakashi Sakamoto { 4873b196c39STakashi Sakamoto __be32 *buffer; 4883b196c39STakashi Sakamoto unsigned int syt; 4893b196c39STakashi Sakamoto unsigned int data_blocks; 4903b196c39STakashi Sakamoto unsigned int pcm_frames; 4913b196c39STakashi Sakamoto struct snd_pcm_substream *pcm; 4923b196c39STakashi Sakamoto 4933b196c39STakashi Sakamoto buffer = s->buffer.packets[s->packet_index].buffer; 4943b196c39STakashi Sakamoto syt = calculate_syt(s, cycle); 4953b196c39STakashi Sakamoto data_blocks = calculate_data_blocks(s, syt); 4963b196c39STakashi Sakamoto pcm_frames = s->process_data_blocks(s, buffer, data_blocks, &syt); 4973b196c39STakashi Sakamoto s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff; 4983b196c39STakashi Sakamoto 4993b196c39STakashi Sakamoto payload_length = data_blocks * 4 * s->data_block_quadlets; 500b164d2fdSTakashi Sakamoto 501b164d2fdSTakashi Sakamoto trace_out_packet_without_header(s, cycle, payload_length, data_blocks, 502b164d2fdSTakashi Sakamoto index); 503b164d2fdSTakashi Sakamoto 5043b196c39STakashi Sakamoto if (queue_out_packet(s, payload_length) < 0) 5053b196c39STakashi Sakamoto return -EIO; 5063b196c39STakashi Sakamoto 5073b196c39STakashi Sakamoto pcm = ACCESS_ONCE(s->pcm); 5083b196c39STakashi Sakamoto if (pcm && pcm_frames > 0) 5093b196c39STakashi Sakamoto update_pcm_pointers(s, pcm, pcm_frames); 5103b196c39STakashi Sakamoto 5113b196c39STakashi Sakamoto /* No need to return the number of handled data blocks. */ 5123b196c39STakashi Sakamoto return 0; 5133b196c39STakashi Sakamoto } 5143b196c39STakashi Sakamoto 515d67c46b9STakashi Sakamoto static int handle_in_packet(struct amdtp_stream *s, 516ff0fb5aaSTakashi Sakamoto unsigned int payload_length, unsigned int cycle, 517a9c4284bSTakashi Sakamoto unsigned int index) 518d67c46b9STakashi Sakamoto { 519d9a16fc9STakashi Sakamoto __be32 *buffer; 520d67c46b9STakashi Sakamoto u32 cip_header[2]; 5219863874fSTakashi Sakamoto unsigned int sph, fmt, fdf, syt; 522d67c46b9STakashi Sakamoto unsigned int data_block_quadlets, data_block_counter, dbc_interval; 523d9a16fc9STakashi Sakamoto unsigned int data_blocks; 524d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm; 525d67c46b9STakashi Sakamoto unsigned int pcm_frames; 526d67c46b9STakashi Sakamoto bool lost; 527d67c46b9STakashi Sakamoto 528d9a16fc9STakashi Sakamoto buffer = s->buffer.packets[s->packet_index].buffer; 529d67c46b9STakashi Sakamoto cip_header[0] = be32_to_cpu(buffer[0]); 530d67c46b9STakashi Sakamoto cip_header[1] = be32_to_cpu(buffer[1]); 531d67c46b9STakashi Sakamoto 532ff0fb5aaSTakashi Sakamoto trace_in_packet(s, cycle, cip_header, payload_length, index); 5330c95c1d6STakashi Sakamoto 534d67c46b9STakashi Sakamoto /* 535d67c46b9STakashi Sakamoto * This module supports 'Two-quadlet CIP header with SYT field'. 536d67c46b9STakashi Sakamoto * For convenience, also check FMT field is AM824 or not. 537d67c46b9STakashi Sakamoto */ 5382128f78fSTakashi Sakamoto if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) || 5392128f78fSTakashi Sakamoto ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) && 5402128f78fSTakashi Sakamoto (!(s->flags & CIP_HEADER_WITHOUT_EOH))) { 541d67c46b9STakashi Sakamoto dev_info_ratelimited(&s->unit->device, 542d67c46b9STakashi Sakamoto "Invalid CIP header for AMDTP: %08X:%08X\n", 543d67c46b9STakashi Sakamoto cip_header[0], cip_header[1]); 544d9a16fc9STakashi Sakamoto data_blocks = 0; 545d67c46b9STakashi Sakamoto pcm_frames = 0; 546d67c46b9STakashi Sakamoto goto end; 547d67c46b9STakashi Sakamoto } 548d67c46b9STakashi Sakamoto 549d67c46b9STakashi Sakamoto /* Check valid protocol or not. */ 5509863874fSTakashi Sakamoto sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT; 551d67c46b9STakashi Sakamoto fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT; 5529863874fSTakashi Sakamoto if (sph != s->sph || fmt != s->fmt) { 5532a7e1713STakashi Sakamoto dev_info_ratelimited(&s->unit->device, 554d67c46b9STakashi Sakamoto "Detect unexpected protocol: %08x %08x\n", 555d67c46b9STakashi Sakamoto cip_header[0], cip_header[1]); 556d9a16fc9STakashi Sakamoto data_blocks = 0; 5572a7e1713STakashi Sakamoto pcm_frames = 0; 5582a7e1713STakashi Sakamoto goto end; 559d67c46b9STakashi Sakamoto } 560d67c46b9STakashi Sakamoto 561d67c46b9STakashi Sakamoto /* Calculate data blocks */ 562d67c46b9STakashi Sakamoto fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT; 563ff0fb5aaSTakashi Sakamoto if (payload_length < 12 || 564d67c46b9STakashi Sakamoto (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) { 565d9a16fc9STakashi Sakamoto data_blocks = 0; 566d67c46b9STakashi Sakamoto } else { 567d67c46b9STakashi Sakamoto data_block_quadlets = 568d67c46b9STakashi Sakamoto (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT; 569d67c46b9STakashi Sakamoto /* avoid division by zero */ 570d67c46b9STakashi Sakamoto if (data_block_quadlets == 0) { 571d67c46b9STakashi Sakamoto dev_err(&s->unit->device, 572d67c46b9STakashi Sakamoto "Detect invalid value in dbs field: %08X\n", 573d67c46b9STakashi Sakamoto cip_header[0]); 574d67c46b9STakashi Sakamoto return -EPROTO; 575d67c46b9STakashi Sakamoto } 576d67c46b9STakashi Sakamoto if (s->flags & CIP_WRONG_DBS) 577d67c46b9STakashi Sakamoto data_block_quadlets = s->data_block_quadlets; 578d67c46b9STakashi Sakamoto 579ff0fb5aaSTakashi Sakamoto data_blocks = (payload_length / 4 - 2) / 580ff0fb5aaSTakashi Sakamoto data_block_quadlets; 581d67c46b9STakashi Sakamoto } 582d67c46b9STakashi Sakamoto 583d67c46b9STakashi Sakamoto /* Check data block counter continuity */ 584d67c46b9STakashi Sakamoto data_block_counter = cip_header[0] & CIP_DBC_MASK; 585d9a16fc9STakashi Sakamoto if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) && 586d67c46b9STakashi Sakamoto s->data_block_counter != UINT_MAX) 587d67c46b9STakashi Sakamoto data_block_counter = s->data_block_counter; 588d67c46b9STakashi Sakamoto 589d67c46b9STakashi Sakamoto if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) && 590d67c46b9STakashi Sakamoto data_block_counter == s->tx_first_dbc) || 591d67c46b9STakashi Sakamoto s->data_block_counter == UINT_MAX) { 592d67c46b9STakashi Sakamoto lost = false; 593d67c46b9STakashi Sakamoto } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) { 594d67c46b9STakashi Sakamoto lost = data_block_counter != s->data_block_counter; 595d67c46b9STakashi Sakamoto } else { 596d9a16fc9STakashi Sakamoto if (data_blocks > 0 && s->tx_dbc_interval > 0) 597d67c46b9STakashi Sakamoto dbc_interval = s->tx_dbc_interval; 598d67c46b9STakashi Sakamoto else 599d9a16fc9STakashi Sakamoto dbc_interval = data_blocks; 600d67c46b9STakashi Sakamoto 601d67c46b9STakashi Sakamoto lost = data_block_counter != 602d67c46b9STakashi Sakamoto ((s->data_block_counter + dbc_interval) & 0xff); 603d67c46b9STakashi Sakamoto } 604d67c46b9STakashi Sakamoto 605d67c46b9STakashi Sakamoto if (lost) { 606d67c46b9STakashi Sakamoto dev_err(&s->unit->device, 607d67c46b9STakashi Sakamoto "Detect discontinuity of CIP: %02X %02X\n", 608d67c46b9STakashi Sakamoto s->data_block_counter, data_block_counter); 609d67c46b9STakashi Sakamoto return -EIO; 610d67c46b9STakashi Sakamoto } 611d67c46b9STakashi Sakamoto 612d9a16fc9STakashi Sakamoto syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK; 613d9a16fc9STakashi Sakamoto pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt); 614d67c46b9STakashi Sakamoto 615d67c46b9STakashi Sakamoto if (s->flags & CIP_DBC_IS_END_EVENT) 616d67c46b9STakashi Sakamoto s->data_block_counter = data_block_counter; 617d67c46b9STakashi Sakamoto else 618d67c46b9STakashi Sakamoto s->data_block_counter = 619d9a16fc9STakashi Sakamoto (data_block_counter + data_blocks) & 0xff; 620d67c46b9STakashi Sakamoto end: 621d67c46b9STakashi Sakamoto if (queue_in_packet(s) < 0) 622d67c46b9STakashi Sakamoto return -EIO; 623d67c46b9STakashi Sakamoto 624d67c46b9STakashi Sakamoto pcm = ACCESS_ONCE(s->pcm); 625d67c46b9STakashi Sakamoto if (pcm && pcm_frames > 0) 626d67c46b9STakashi Sakamoto update_pcm_pointers(s, pcm, pcm_frames); 627d67c46b9STakashi Sakamoto 628d67c46b9STakashi Sakamoto return 0; 629d67c46b9STakashi Sakamoto } 630d67c46b9STakashi Sakamoto 6313b196c39STakashi Sakamoto static int handle_in_packet_without_header(struct amdtp_stream *s, 6323b196c39STakashi Sakamoto unsigned int payload_quadlets, unsigned int cycle, 6333b196c39STakashi Sakamoto unsigned int index) 6343b196c39STakashi Sakamoto { 6353b196c39STakashi Sakamoto __be32 *buffer; 6363b196c39STakashi Sakamoto unsigned int data_blocks; 6373b196c39STakashi Sakamoto struct snd_pcm_substream *pcm; 6383b196c39STakashi Sakamoto unsigned int pcm_frames; 6393b196c39STakashi Sakamoto 6403b196c39STakashi Sakamoto buffer = s->buffer.packets[s->packet_index].buffer; 6413b196c39STakashi Sakamoto data_blocks = payload_quadlets / s->data_block_quadlets; 642b164d2fdSTakashi Sakamoto 643b164d2fdSTakashi Sakamoto trace_in_packet_without_header(s, cycle, payload_quadlets, data_blocks, 644b164d2fdSTakashi Sakamoto index); 645b164d2fdSTakashi Sakamoto 6463b196c39STakashi Sakamoto pcm_frames = s->process_data_blocks(s, buffer, data_blocks, NULL); 6473b196c39STakashi Sakamoto s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff; 6483b196c39STakashi Sakamoto 6493b196c39STakashi Sakamoto if (queue_in_packet(s) < 0) 6503b196c39STakashi Sakamoto return -EIO; 6513b196c39STakashi Sakamoto 6523b196c39STakashi Sakamoto pcm = ACCESS_ONCE(s->pcm); 6533b196c39STakashi Sakamoto if (pcm && pcm_frames > 0) 6543b196c39STakashi Sakamoto update_pcm_pointers(s, pcm, pcm_frames); 6553b196c39STakashi Sakamoto 6563b196c39STakashi Sakamoto return 0; 6573b196c39STakashi Sakamoto } 6583b196c39STakashi Sakamoto 65973fc7f08STakashi Sakamoto /* 66073fc7f08STakashi Sakamoto * In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On 66173fc7f08STakashi Sakamoto * the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent 66273fc7f08STakashi Sakamoto * it. Thus, via Linux firewire subsystem, we can get the 3 bits for second. 66373fc7f08STakashi Sakamoto */ 66473fc7f08STakashi Sakamoto static inline u32 compute_cycle_count(u32 tstamp) 66573fc7f08STakashi Sakamoto { 66673fc7f08STakashi Sakamoto return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff); 66773fc7f08STakashi Sakamoto } 66873fc7f08STakashi Sakamoto 66973fc7f08STakashi Sakamoto static inline u32 increment_cycle_count(u32 cycle, unsigned int addend) 67073fc7f08STakashi Sakamoto { 67173fc7f08STakashi Sakamoto cycle += addend; 67273fc7f08STakashi Sakamoto if (cycle >= 8 * CYCLES_PER_SECOND) 67373fc7f08STakashi Sakamoto cycle -= 8 * CYCLES_PER_SECOND; 67473fc7f08STakashi Sakamoto return cycle; 67573fc7f08STakashi Sakamoto } 67673fc7f08STakashi Sakamoto 677f90e2dedSTakashi Sakamoto static inline u32 decrement_cycle_count(u32 cycle, unsigned int subtrahend) 678f90e2dedSTakashi Sakamoto { 679f90e2dedSTakashi Sakamoto if (cycle < subtrahend) 680f90e2dedSTakashi Sakamoto cycle += 8 * CYCLES_PER_SECOND; 681f90e2dedSTakashi Sakamoto return cycle - subtrahend; 682f90e2dedSTakashi Sakamoto } 683f90e2dedSTakashi Sakamoto 68473fc7f08STakashi Sakamoto static void out_stream_callback(struct fw_iso_context *context, u32 tstamp, 685d67c46b9STakashi Sakamoto size_t header_length, void *header, 686d67c46b9STakashi Sakamoto void *private_data) 687d67c46b9STakashi Sakamoto { 688d67c46b9STakashi Sakamoto struct amdtp_stream *s = private_data; 689390a1512STakashi Sakamoto unsigned int i, packets = header_length / 4; 69073fc7f08STakashi Sakamoto u32 cycle; 691d67c46b9STakashi Sakamoto 692d67c46b9STakashi Sakamoto if (s->packet_index < 0) 693d67c46b9STakashi Sakamoto return; 694d67c46b9STakashi Sakamoto 69573fc7f08STakashi Sakamoto cycle = compute_cycle_count(tstamp); 69673fc7f08STakashi Sakamoto 69773fc7f08STakashi Sakamoto /* Align to actual cycle count for the last packet. */ 69873fc7f08STakashi Sakamoto cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets); 699d67c46b9STakashi Sakamoto 700d67c46b9STakashi Sakamoto for (i = 0; i < packets; ++i) { 70173fc7f08STakashi Sakamoto cycle = increment_cycle_count(cycle, 1); 7023b196c39STakashi Sakamoto if (s->handle_packet(s, 0, cycle, i) < 0) { 703d67c46b9STakashi Sakamoto s->packet_index = -1; 704d67c46b9STakashi Sakamoto amdtp_stream_pcm_abort(s); 705d67c46b9STakashi Sakamoto return; 706d67c46b9STakashi Sakamoto } 707d67c46b9STakashi Sakamoto } 708d67c46b9STakashi Sakamoto 709d67c46b9STakashi Sakamoto fw_iso_context_queue_flush(s->context); 710d67c46b9STakashi Sakamoto } 711d67c46b9STakashi Sakamoto 71273fc7f08STakashi Sakamoto static void in_stream_callback(struct fw_iso_context *context, u32 tstamp, 713d67c46b9STakashi Sakamoto size_t header_length, void *header, 714d67c46b9STakashi Sakamoto void *private_data) 715d67c46b9STakashi Sakamoto { 716d67c46b9STakashi Sakamoto struct amdtp_stream *s = private_data; 717d9a16fc9STakashi Sakamoto unsigned int i, packets; 718ff0fb5aaSTakashi Sakamoto unsigned int payload_length, max_payload_length; 719d9a16fc9STakashi Sakamoto __be32 *headers = header; 720f90e2dedSTakashi Sakamoto u32 cycle; 721d67c46b9STakashi Sakamoto 722d67c46b9STakashi Sakamoto if (s->packet_index < 0) 723d67c46b9STakashi Sakamoto return; 724d67c46b9STakashi Sakamoto 725d67c46b9STakashi Sakamoto /* The number of packets in buffer */ 726d67c46b9STakashi Sakamoto packets = header_length / IN_PACKET_HEADER_SIZE; 727d67c46b9STakashi Sakamoto 728f90e2dedSTakashi Sakamoto cycle = compute_cycle_count(tstamp); 729f90e2dedSTakashi Sakamoto 730f90e2dedSTakashi Sakamoto /* Align to actual cycle count for the last packet. */ 731f90e2dedSTakashi Sakamoto cycle = decrement_cycle_count(cycle, packets); 732f90e2dedSTakashi Sakamoto 733d67c46b9STakashi Sakamoto /* For buffer-over-run prevention. */ 734f91c9d76STakashi Sakamoto max_payload_length = s->max_payload_length; 735d67c46b9STakashi Sakamoto 736d9a16fc9STakashi Sakamoto for (i = 0; i < packets; i++) { 737f90e2dedSTakashi Sakamoto cycle = increment_cycle_count(cycle, 1); 738d67c46b9STakashi Sakamoto 739f91c9d76STakashi Sakamoto /* The number of bytes in this packet */ 740ff0fb5aaSTakashi Sakamoto payload_length = 741ff0fb5aaSTakashi Sakamoto (be32_to_cpu(headers[i]) >> ISO_DATA_LENGTH_SHIFT); 742ff0fb5aaSTakashi Sakamoto if (payload_length > max_payload_length) { 743d67c46b9STakashi Sakamoto dev_err(&s->unit->device, 744ff0fb5aaSTakashi Sakamoto "Detect jumbo payload: %04x %04x\n", 745ff0fb5aaSTakashi Sakamoto payload_length, max_payload_length); 746d67c46b9STakashi Sakamoto break; 747d67c46b9STakashi Sakamoto } 748d67c46b9STakashi Sakamoto 7493b196c39STakashi Sakamoto if (s->handle_packet(s, payload_length, cycle, i) < 0) 750d67c46b9STakashi Sakamoto break; 751d67c46b9STakashi Sakamoto } 752d67c46b9STakashi Sakamoto 753dec63cc8STakashi Sakamoto /* Queueing error or detecting invalid payload. */ 754d9a16fc9STakashi Sakamoto if (i < packets) { 755d67c46b9STakashi Sakamoto s->packet_index = -1; 756d67c46b9STakashi Sakamoto amdtp_stream_pcm_abort(s); 757d67c46b9STakashi Sakamoto return; 758d67c46b9STakashi Sakamoto } 759d67c46b9STakashi Sakamoto 760d67c46b9STakashi Sakamoto fw_iso_context_queue_flush(s->context); 761d67c46b9STakashi Sakamoto } 762d67c46b9STakashi Sakamoto 763d67c46b9STakashi Sakamoto /* this is executed one time */ 764d67c46b9STakashi Sakamoto static void amdtp_stream_first_callback(struct fw_iso_context *context, 76573fc7f08STakashi Sakamoto u32 tstamp, size_t header_length, 766d67c46b9STakashi Sakamoto void *header, void *private_data) 767d67c46b9STakashi Sakamoto { 768d67c46b9STakashi Sakamoto struct amdtp_stream *s = private_data; 769a04513f8STakashi Sakamoto u32 cycle; 770a04513f8STakashi Sakamoto unsigned int packets; 771d67c46b9STakashi Sakamoto 772f91c9d76STakashi Sakamoto s->max_payload_length = amdtp_stream_get_max_payload(s); 773f91c9d76STakashi Sakamoto 774d67c46b9STakashi Sakamoto /* 775d67c46b9STakashi Sakamoto * For in-stream, first packet has come. 776d67c46b9STakashi Sakamoto * For out-stream, prepared to transmit first packet 777d67c46b9STakashi Sakamoto */ 778d67c46b9STakashi Sakamoto s->callbacked = true; 779d67c46b9STakashi Sakamoto wake_up(&s->callback_wait); 780d67c46b9STakashi Sakamoto 781a04513f8STakashi Sakamoto cycle = compute_cycle_count(tstamp); 782a04513f8STakashi Sakamoto 783a04513f8STakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 784a04513f8STakashi Sakamoto packets = header_length / IN_PACKET_HEADER_SIZE; 785a04513f8STakashi Sakamoto cycle = decrement_cycle_count(cycle, packets); 786d67c46b9STakashi Sakamoto context->callback.sc = in_stream_callback; 7873b196c39STakashi Sakamoto if (s->flags & CIP_NO_HEADER) 7883b196c39STakashi Sakamoto s->handle_packet = handle_in_packet_without_header; 7893b196c39STakashi Sakamoto else 7903b196c39STakashi Sakamoto s->handle_packet = handle_in_packet; 791a04513f8STakashi Sakamoto } else { 792a04513f8STakashi Sakamoto packets = header_length / 4; 793a04513f8STakashi Sakamoto cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets); 794d67c46b9STakashi Sakamoto context->callback.sc = out_stream_callback; 7953b196c39STakashi Sakamoto if (s->flags & CIP_NO_HEADER) 7963b196c39STakashi Sakamoto s->handle_packet = handle_out_packet_without_header; 7973b196c39STakashi Sakamoto else 7983b196c39STakashi Sakamoto s->handle_packet = handle_out_packet; 799a04513f8STakashi Sakamoto } 800a04513f8STakashi Sakamoto 801a04513f8STakashi Sakamoto s->start_cycle = cycle; 802d67c46b9STakashi Sakamoto 80373fc7f08STakashi Sakamoto context->callback.sc(context, tstamp, header_length, header, s); 804d67c46b9STakashi Sakamoto } 805d67c46b9STakashi Sakamoto 806d67c46b9STakashi Sakamoto /** 807d67c46b9STakashi Sakamoto * amdtp_stream_start - start transferring packets 808d67c46b9STakashi Sakamoto * @s: the AMDTP stream to start 809d67c46b9STakashi Sakamoto * @channel: the isochronous channel on the bus 810d67c46b9STakashi Sakamoto * @speed: firewire speed code 811d67c46b9STakashi Sakamoto * 812d67c46b9STakashi Sakamoto * The stream cannot be started until it has been configured with 813d67c46b9STakashi Sakamoto * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI 814d67c46b9STakashi Sakamoto * device can be started. 815d67c46b9STakashi Sakamoto */ 816d67c46b9STakashi Sakamoto int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed) 817d67c46b9STakashi Sakamoto { 818d67c46b9STakashi Sakamoto static const struct { 819d67c46b9STakashi Sakamoto unsigned int data_block; 820d67c46b9STakashi Sakamoto unsigned int syt_offset; 821d67c46b9STakashi Sakamoto } initial_state[] = { 822d67c46b9STakashi Sakamoto [CIP_SFC_32000] = { 4, 3072 }, 823d67c46b9STakashi Sakamoto [CIP_SFC_48000] = { 6, 1024 }, 824d67c46b9STakashi Sakamoto [CIP_SFC_96000] = { 12, 1024 }, 825d67c46b9STakashi Sakamoto [CIP_SFC_192000] = { 24, 1024 }, 826d67c46b9STakashi Sakamoto [CIP_SFC_44100] = { 0, 67 }, 827d67c46b9STakashi Sakamoto [CIP_SFC_88200] = { 0, 67 }, 828d67c46b9STakashi Sakamoto [CIP_SFC_176400] = { 0, 67 }, 829d67c46b9STakashi Sakamoto }; 830d67c46b9STakashi Sakamoto unsigned int header_size; 831d67c46b9STakashi Sakamoto enum dma_data_direction dir; 832d67c46b9STakashi Sakamoto int type, tag, err; 833d67c46b9STakashi Sakamoto 834d67c46b9STakashi Sakamoto mutex_lock(&s->mutex); 835d67c46b9STakashi Sakamoto 836d67c46b9STakashi Sakamoto if (WARN_ON(amdtp_stream_running(s) || 837d67c46b9STakashi Sakamoto (s->data_block_quadlets < 1))) { 838d67c46b9STakashi Sakamoto err = -EBADFD; 839d67c46b9STakashi Sakamoto goto err_unlock; 840d67c46b9STakashi Sakamoto } 841d67c46b9STakashi Sakamoto 84262f00e40STakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) 843d67c46b9STakashi Sakamoto s->data_block_counter = UINT_MAX; 844d67c46b9STakashi Sakamoto else 845d67c46b9STakashi Sakamoto s->data_block_counter = 0; 846d67c46b9STakashi Sakamoto s->data_block_state = initial_state[s->sfc].data_block; 847d67c46b9STakashi Sakamoto s->syt_offset_state = initial_state[s->sfc].syt_offset; 848d67c46b9STakashi Sakamoto s->last_syt_offset = TICKS_PER_CYCLE; 849d67c46b9STakashi Sakamoto 850d67c46b9STakashi Sakamoto /* initialize packet buffer */ 851d67c46b9STakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) { 852d67c46b9STakashi Sakamoto dir = DMA_FROM_DEVICE; 853d67c46b9STakashi Sakamoto type = FW_ISO_CONTEXT_RECEIVE; 854d67c46b9STakashi Sakamoto header_size = IN_PACKET_HEADER_SIZE; 855d67c46b9STakashi Sakamoto } else { 856d67c46b9STakashi Sakamoto dir = DMA_TO_DEVICE; 857d67c46b9STakashi Sakamoto type = FW_ISO_CONTEXT_TRANSMIT; 858d67c46b9STakashi Sakamoto header_size = OUT_PACKET_HEADER_SIZE; 859d67c46b9STakashi Sakamoto } 860d67c46b9STakashi Sakamoto err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH, 861d67c46b9STakashi Sakamoto amdtp_stream_get_max_payload(s), dir); 862d67c46b9STakashi Sakamoto if (err < 0) 863d67c46b9STakashi Sakamoto goto err_unlock; 864d67c46b9STakashi Sakamoto 865d67c46b9STakashi Sakamoto s->context = fw_iso_context_create(fw_parent_device(s->unit)->card, 866d67c46b9STakashi Sakamoto type, channel, speed, header_size, 867d67c46b9STakashi Sakamoto amdtp_stream_first_callback, s); 868d67c46b9STakashi Sakamoto if (IS_ERR(s->context)) { 869d67c46b9STakashi Sakamoto err = PTR_ERR(s->context); 870d67c46b9STakashi Sakamoto if (err == -EBUSY) 871d67c46b9STakashi Sakamoto dev_err(&s->unit->device, 872d67c46b9STakashi Sakamoto "no free stream on this controller\n"); 873d67c46b9STakashi Sakamoto goto err_buffer; 874d67c46b9STakashi Sakamoto } 875d67c46b9STakashi Sakamoto 876d67c46b9STakashi Sakamoto amdtp_stream_update(s); 877d67c46b9STakashi Sakamoto 8783b196c39STakashi Sakamoto if (s->flags & CIP_NO_HEADER) 8793b196c39STakashi Sakamoto s->tag = TAG_NO_CIP_HEADER; 8803b196c39STakashi Sakamoto else 8813b196c39STakashi Sakamoto s->tag = TAG_CIP; 8823b196c39STakashi Sakamoto 883d67c46b9STakashi Sakamoto s->packet_index = 0; 884d67c46b9STakashi Sakamoto do { 885d67c46b9STakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) 886d67c46b9STakashi Sakamoto err = queue_in_packet(s); 887d67c46b9STakashi Sakamoto else 888ff38e0c7STakashi Sakamoto err = queue_out_packet(s, 0); 889d67c46b9STakashi Sakamoto if (err < 0) 890d67c46b9STakashi Sakamoto goto err_context; 891d67c46b9STakashi Sakamoto } while (s->packet_index > 0); 892d67c46b9STakashi Sakamoto 893d67c46b9STakashi Sakamoto /* NOTE: TAG1 matches CIP. This just affects in stream. */ 894d67c46b9STakashi Sakamoto tag = FW_ISO_CONTEXT_MATCH_TAG1; 8953b196c39STakashi Sakamoto if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER)) 896d67c46b9STakashi Sakamoto tag |= FW_ISO_CONTEXT_MATCH_TAG0; 897d67c46b9STakashi Sakamoto 898d67c46b9STakashi Sakamoto s->callbacked = false; 899d67c46b9STakashi Sakamoto err = fw_iso_context_start(s->context, -1, 0, tag); 900d67c46b9STakashi Sakamoto if (err < 0) 901d67c46b9STakashi Sakamoto goto err_context; 902d67c46b9STakashi Sakamoto 903d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 904d67c46b9STakashi Sakamoto 905d67c46b9STakashi Sakamoto return 0; 906d67c46b9STakashi Sakamoto 907d67c46b9STakashi Sakamoto err_context: 908d67c46b9STakashi Sakamoto fw_iso_context_destroy(s->context); 909d67c46b9STakashi Sakamoto s->context = ERR_PTR(-1); 910d67c46b9STakashi Sakamoto err_buffer: 911d67c46b9STakashi Sakamoto iso_packets_buffer_destroy(&s->buffer, s->unit); 912d67c46b9STakashi Sakamoto err_unlock: 913d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 914d67c46b9STakashi Sakamoto 915d67c46b9STakashi Sakamoto return err; 916d67c46b9STakashi Sakamoto } 917d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_start); 918d67c46b9STakashi Sakamoto 919d67c46b9STakashi Sakamoto /** 920d67c46b9STakashi Sakamoto * amdtp_stream_pcm_pointer - get the PCM buffer position 921d67c46b9STakashi Sakamoto * @s: the AMDTP stream that transports the PCM data 922d67c46b9STakashi Sakamoto * 923d67c46b9STakashi Sakamoto * Returns the current buffer position, in frames. 924d67c46b9STakashi Sakamoto */ 925d67c46b9STakashi Sakamoto unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s) 926d67c46b9STakashi Sakamoto { 9271dba9db0STakashi Sakamoto /* 9281dba9db0STakashi Sakamoto * This function is called in software IRQ context of period_tasklet or 9291dba9db0STakashi Sakamoto * process context. 9301dba9db0STakashi Sakamoto * 9311dba9db0STakashi Sakamoto * When the software IRQ context was scheduled by software IRQ context 9321dba9db0STakashi Sakamoto * of IR/IT contexts, queued packets were already handled. Therefore, 9331dba9db0STakashi Sakamoto * no need to flush the queue in buffer anymore. 9341dba9db0STakashi Sakamoto * 9351dba9db0STakashi Sakamoto * When the process context reach here, some packets will be already 9361dba9db0STakashi Sakamoto * queued in the buffer. These packets should be handled immediately 9371dba9db0STakashi Sakamoto * to keep better granularity of PCM pointer. 9381dba9db0STakashi Sakamoto * 9391dba9db0STakashi Sakamoto * Later, the process context will sometimes schedules software IRQ 9401dba9db0STakashi Sakamoto * context of the period_tasklet. Then, no need to flush the queue by 9411dba9db0STakashi Sakamoto * the same reason as described for IR/IT contexts. 9421dba9db0STakashi Sakamoto */ 9431dba9db0STakashi Sakamoto if (!in_interrupt() && amdtp_stream_running(s)) 944d67c46b9STakashi Sakamoto fw_iso_context_flush_completions(s->context); 945d67c46b9STakashi Sakamoto 946d67c46b9STakashi Sakamoto return ACCESS_ONCE(s->pcm_buffer_pointer); 947d67c46b9STakashi Sakamoto } 948d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_pointer); 949d67c46b9STakashi Sakamoto 950d67c46b9STakashi Sakamoto /** 951875becf8STakashi Sakamoto * amdtp_stream_pcm_ack - acknowledge queued PCM frames 952875becf8STakashi Sakamoto * @s: the AMDTP stream that transfers the PCM frames 953875becf8STakashi Sakamoto * 954875becf8STakashi Sakamoto * Returns zero always. 955875becf8STakashi Sakamoto */ 956875becf8STakashi Sakamoto int amdtp_stream_pcm_ack(struct amdtp_stream *s) 957875becf8STakashi Sakamoto { 958875becf8STakashi Sakamoto /* 959875becf8STakashi Sakamoto * Process isochronous packets for recent isochronous cycle to handle 960875becf8STakashi Sakamoto * queued PCM frames. 961875becf8STakashi Sakamoto */ 962875becf8STakashi Sakamoto if (amdtp_stream_running(s)) 963875becf8STakashi Sakamoto fw_iso_context_flush_completions(s->context); 964875becf8STakashi Sakamoto 965875becf8STakashi Sakamoto return 0; 966875becf8STakashi Sakamoto } 967875becf8STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_ack); 968875becf8STakashi Sakamoto 969875becf8STakashi Sakamoto /** 970d67c46b9STakashi Sakamoto * amdtp_stream_update - update the stream after a bus reset 971d67c46b9STakashi Sakamoto * @s: the AMDTP stream 972d67c46b9STakashi Sakamoto */ 973d67c46b9STakashi Sakamoto void amdtp_stream_update(struct amdtp_stream *s) 974d67c46b9STakashi Sakamoto { 975d67c46b9STakashi Sakamoto /* Precomputing. */ 976d67c46b9STakashi Sakamoto ACCESS_ONCE(s->source_node_id_field) = 977d67c46b9STakashi Sakamoto (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & 978d67c46b9STakashi Sakamoto CIP_SID_MASK; 979d67c46b9STakashi Sakamoto } 980d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_update); 981d67c46b9STakashi Sakamoto 982d67c46b9STakashi Sakamoto /** 983d67c46b9STakashi Sakamoto * amdtp_stream_stop - stop sending packets 984d67c46b9STakashi Sakamoto * @s: the AMDTP stream to stop 985d67c46b9STakashi Sakamoto * 986d67c46b9STakashi Sakamoto * All PCM and MIDI devices of the stream must be stopped before the stream 987d67c46b9STakashi Sakamoto * itself can be stopped. 988d67c46b9STakashi Sakamoto */ 989d67c46b9STakashi Sakamoto void amdtp_stream_stop(struct amdtp_stream *s) 990d67c46b9STakashi Sakamoto { 991d67c46b9STakashi Sakamoto mutex_lock(&s->mutex); 992d67c46b9STakashi Sakamoto 993d67c46b9STakashi Sakamoto if (!amdtp_stream_running(s)) { 994d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 995d67c46b9STakashi Sakamoto return; 996d67c46b9STakashi Sakamoto } 997d67c46b9STakashi Sakamoto 998d67c46b9STakashi Sakamoto tasklet_kill(&s->period_tasklet); 999d67c46b9STakashi Sakamoto fw_iso_context_stop(s->context); 1000d67c46b9STakashi Sakamoto fw_iso_context_destroy(s->context); 1001d67c46b9STakashi Sakamoto s->context = ERR_PTR(-1); 1002d67c46b9STakashi Sakamoto iso_packets_buffer_destroy(&s->buffer, s->unit); 1003d67c46b9STakashi Sakamoto 1004d67c46b9STakashi Sakamoto s->callbacked = false; 1005d67c46b9STakashi Sakamoto 1006d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex); 1007d67c46b9STakashi Sakamoto } 1008d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_stop); 1009d67c46b9STakashi Sakamoto 1010d67c46b9STakashi Sakamoto /** 1011d67c46b9STakashi Sakamoto * amdtp_stream_pcm_abort - abort the running PCM device 1012d67c46b9STakashi Sakamoto * @s: the AMDTP stream about to be stopped 1013d67c46b9STakashi Sakamoto * 1014d67c46b9STakashi Sakamoto * If the isochronous stream needs to be stopped asynchronously, call this 1015d67c46b9STakashi Sakamoto * function first to stop the PCM device. 1016d67c46b9STakashi Sakamoto */ 1017d67c46b9STakashi Sakamoto void amdtp_stream_pcm_abort(struct amdtp_stream *s) 1018d67c46b9STakashi Sakamoto { 1019d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm; 1020d67c46b9STakashi Sakamoto 1021d67c46b9STakashi Sakamoto pcm = ACCESS_ONCE(s->pcm); 1022d67c46b9STakashi Sakamoto if (pcm) 1023d67c46b9STakashi Sakamoto snd_pcm_stop_xrun(pcm); 1024d67c46b9STakashi Sakamoto } 1025d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_abort); 1026