blob: 43f28b813386e7b9f90273fb82da0b4b6ad012d0 [file] [log] [blame]
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001/*
2 * Audio and Music Data Transmission Protocol (IEC 61883-6) streams
3 * with Common Isochronous Packet (IEC 61883-1) headers
4 *
5 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
8
9#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/firewire.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <sound/pcm.h>
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +090015#include <sound/pcm_params.h>
Takashi Sakamotod67c46b2015-09-19 11:21:54 +090016#include "amdtp-stream.h"
Clemens Ladisch31ef9132011-03-15 07:53:21 +010017
18#define TICKS_PER_CYCLE 3072
19#define CYCLES_PER_SECOND 8000
20#define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
21
Takashi Sakamoto0c95c1d2016-05-09 21:12:46 +090022/* Always support Linux tracing subsystem. */
23#define CREATE_TRACE_POINTS
24#include "amdtp-stream-trace.h"
25
Takashi Sakamotoca5b5052015-02-21 11:50:17 +090026#define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */
Clemens Ladisch31ef9132011-03-15 07:53:21 +010027
Takashi Sakamotob445db42014-04-25 22:44:43 +090028/* isochronous header parameters */
29#define ISO_DATA_LENGTH_SHIFT 16
Takashi Sakamoto3b196c392017-03-31 22:06:07 +090030#define TAG_NO_CIP_HEADER 0
Clemens Ladisch31ef9132011-03-15 07:53:21 +010031#define TAG_CIP 1
32
Takashi Sakamotob445db42014-04-25 22:44:43 +090033/* common isochronous packet header parameters */
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090034#define CIP_EOH_SHIFT 31
35#define CIP_EOH (1u << CIP_EOH_SHIFT)
Takashi Sakamotob445db42014-04-25 22:44:43 +090036#define CIP_EOH_MASK 0x80000000
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090037#define CIP_SID_SHIFT 24
38#define CIP_SID_MASK 0x3f000000
39#define CIP_DBS_MASK 0x00ff0000
40#define CIP_DBS_SHIFT 16
Takashi Sakamoto98638742017-03-22 21:30:16 +090041#define CIP_SPH_MASK 0x00000400
42#define CIP_SPH_SHIFT 10
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090043#define CIP_DBC_MASK 0x000000ff
44#define CIP_FMT_SHIFT 24
Takashi Sakamotob445db42014-04-25 22:44:43 +090045#define CIP_FMT_MASK 0x3f000000
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090046#define CIP_FDF_MASK 0x00ff0000
47#define CIP_FDF_SHIFT 16
Takashi Sakamotob445db42014-04-25 22:44:43 +090048#define CIP_SYT_MASK 0x0000ffff
49#define CIP_SYT_NO_INFO 0xffff
Takashi Sakamotob445db42014-04-25 22:44:43 +090050
Takashi Sakamoto51c29fd2015-09-19 11:21:56 +090051/* Audio and Music transfer protocol specific parameters */
Takashi Sakamoto414ba022015-09-19 11:21:53 +090052#define CIP_FMT_AM 0x10
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090053#define AMDTP_FDF_NO_DATA 0xff
Clemens Ladisch31ef9132011-03-15 07:53:21 +010054
55/* TODO: make these configurable */
56#define INTERRUPT_INTERVAL 16
57#define QUEUE_LENGTH 48
58
Takashi Sakamotocc4f8e92019-03-17 20:25:06 +090059#define IR_HEADER_SIZE 8 // For header and timestamp.
Takashi Sakamoto4b7da112014-04-25 22:44:45 +090060#define OUT_PACKET_HEADER_SIZE 0
Takashi Sakamotocc4f8e92019-03-17 20:25:06 +090061#define HEADER_TSTAMP_MASK 0x0000ffff
Takashi Sakamoto4b7da112014-04-25 22:44:45 +090062
Clemens Ladisch76fb8782012-05-13 22:03:09 +020063static void pcm_period_tasklet(unsigned long data);
64
Clemens Ladisch31ef9132011-03-15 07:53:21 +010065/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090066 * amdtp_stream_init - initialize an AMDTP stream structure
67 * @s: the AMDTP stream to initialize
Clemens Ladisch31ef9132011-03-15 07:53:21 +010068 * @unit: the target of the stream
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090069 * @dir: the direction of stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +010070 * @flags: the packet transmission method to use
Takashi Sakamoto59558152015-09-19 11:21:55 +090071 * @fmt: the value of fmt field in CIP header
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090072 * @process_data_blocks: callback handler to process data blocks
73 * @protocol_size: the size to allocate newly for protocol
Clemens Ladisch31ef9132011-03-15 07:53:21 +010074 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090075int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
Takashi Sakamoto59558152015-09-19 11:21:55 +090076 enum amdtp_stream_direction dir, enum cip_flags flags,
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090077 unsigned int fmt,
78 amdtp_stream_process_data_blocks_t process_data_blocks,
79 unsigned int protocol_size)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010080{
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090081 if (process_data_blocks == NULL)
82 return -EINVAL;
83
84 s->protocol = kzalloc(protocol_size, GFP_KERNEL);
85 if (!s->protocol)
86 return -ENOMEM;
87
Takashi Sakamotoc6f224d2015-02-21 23:54:58 +090088 s->unit = unit;
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090089 s->direction = dir;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010090 s->flags = flags;
91 s->context = ERR_PTR(-1);
92 mutex_init(&s->mutex);
Clemens Ladisch76fb8782012-05-13 22:03:09 +020093 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +010094 s->packet_index = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010095
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +090096 init_waitqueue_head(&s->callback_wait);
97 s->callbacked = false;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +090098
Takashi Sakamoto59558152015-09-19 11:21:55 +090099 s->fmt = fmt;
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900100 s->process_data_blocks = process_data_blocks;
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900101
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100102 return 0;
103}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900104EXPORT_SYMBOL(amdtp_stream_init);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100105
106/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900107 * amdtp_stream_destroy - free stream resources
108 * @s: the AMDTP stream to destroy
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100109 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900110void amdtp_stream_destroy(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100111{
Takashi Sakamoto44c376b2016-03-31 08:47:02 +0900112 /* Not initialized. */
113 if (s->protocol == NULL)
114 return;
115
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900116 WARN_ON(amdtp_stream_running(s));
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900117 kfree(s->protocol);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100118 mutex_destroy(&s->mutex);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100119}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900120EXPORT_SYMBOL(amdtp_stream_destroy);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100121
Clemens Ladischc5280e92011-10-16 21:39:00 +0200122const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
Clemens Ladischa7304e32011-09-04 22:16:10 +0200123 [CIP_SFC_32000] = 8,
124 [CIP_SFC_44100] = 8,
125 [CIP_SFC_48000] = 8,
126 [CIP_SFC_88200] = 16,
127 [CIP_SFC_96000] = 16,
128 [CIP_SFC_176400] = 32,
129 [CIP_SFC_192000] = 32,
130};
131EXPORT_SYMBOL(amdtp_syt_intervals);
132
Takashi Sakamotof9503a62014-05-28 00:14:36 +0900133const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900134 [CIP_SFC_32000] = 32000,
135 [CIP_SFC_44100] = 44100,
136 [CIP_SFC_48000] = 48000,
137 [CIP_SFC_88200] = 88200,
138 [CIP_SFC_96000] = 96000,
139 [CIP_SFC_176400] = 176400,
140 [CIP_SFC_192000] = 192000,
141};
142EXPORT_SYMBOL(amdtp_rate_table);
143
Takashi Sakamoto59502292018-10-01 04:11:49 +0900144static int apply_constraint_to_size(struct snd_pcm_hw_params *params,
145 struct snd_pcm_hw_rule *rule)
146{
147 struct snd_interval *s = hw_param_interval(params, rule->var);
148 const struct snd_interval *r =
149 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
Takashi Sakamoto826b5de2018-10-30 15:31:15 +0900150 struct snd_interval t = {0};
151 unsigned int step = 0;
Takashi Sakamoto59502292018-10-01 04:11:49 +0900152 int i;
153
154 for (i = 0; i < CIP_SFC_COUNT; ++i) {
Takashi Sakamoto826b5de2018-10-30 15:31:15 +0900155 if (snd_interval_test(r, amdtp_rate_table[i]))
156 step = max(step, amdtp_syt_intervals[i]);
Takashi Sakamoto59502292018-10-01 04:11:49 +0900157 }
158
Takashi Sakamoto826b5de2018-10-30 15:31:15 +0900159 t.min = roundup(s->min, step);
160 t.max = rounddown(s->max, step);
161 t.integer = 1;
Takashi Sakamoto59502292018-10-01 04:11:49 +0900162
163 return snd_interval_refine(s, &t);
164}
165
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100166/**
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900167 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
168 * @s: the AMDTP stream, which must be initialized.
169 * @runtime: the PCM substream runtime
170 */
171int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
172 struct snd_pcm_runtime *runtime)
173{
Takashi Sakamoto55799c52017-06-08 09:11:03 +0900174 struct snd_pcm_hardware *hw = &runtime->hw;
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900175 int err;
176
Takashi Sakamoto55799c52017-06-08 09:11:03 +0900177 hw->info = SNDRV_PCM_INFO_BATCH |
178 SNDRV_PCM_INFO_BLOCK_TRANSFER |
179 SNDRV_PCM_INFO_INTERLEAVED |
180 SNDRV_PCM_INFO_JOINT_DUPLEX |
181 SNDRV_PCM_INFO_MMAP |
182 SNDRV_PCM_INFO_MMAP_VALID;
183
184 /* SNDRV_PCM_INFO_BATCH */
185 hw->periods_min = 2;
186 hw->periods_max = UINT_MAX;
187
188 /* bytes for a frame */
189 hw->period_bytes_min = 4 * hw->channels_max;
190
191 /* Just to prevent from allocating much pages. */
192 hw->period_bytes_max = hw->period_bytes_min * 2048;
193 hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
194
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900195 /*
196 * Currently firewire-lib processes 16 packets in one software
197 * interrupt callback. This equals to 2msec but actually the
198 * interval of the interrupts has a jitter.
199 * Additionally, even if adding a constraint to fit period size to
200 * 2msec, actual calculated frames per period doesn't equal to 2msec,
201 * depending on sampling rate.
202 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
203 * Here let us use 5msec for safe period interrupt.
204 */
205 err = snd_pcm_hw_constraint_minmax(runtime,
206 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
207 5000, UINT_MAX);
208 if (err < 0)
209 goto end;
210
211 /* Non-Blocking stream has no more constraints */
212 if (!(s->flags & CIP_BLOCKING))
213 goto end;
214
215 /*
216 * One AMDTP packet can include some frames. In blocking mode, the
217 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
218 * depending on its sampling rate. For accurate period interrupt, it's
Yannick Guerrinice991982015-03-09 22:13:03 +0100219 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900220 */
Takashi Sakamoto59502292018-10-01 04:11:49 +0900221 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
222 apply_constraint_to_size, NULL,
Takashi Sakamoto826b5de2018-10-30 15:31:15 +0900223 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Takashi Sakamoto59502292018-10-01 04:11:49 +0900224 SNDRV_PCM_HW_PARAM_RATE, -1);
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900225 if (err < 0)
226 goto end;
Takashi Sakamoto59502292018-10-01 04:11:49 +0900227 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
228 apply_constraint_to_size, NULL,
Takashi Sakamoto826b5de2018-10-30 15:31:15 +0900229 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
Takashi Sakamoto59502292018-10-01 04:11:49 +0900230 SNDRV_PCM_HW_PARAM_RATE, -1);
231 if (err < 0)
232 goto end;
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900233end:
234 return err;
235}
236EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
237
238/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900239 * amdtp_stream_set_parameters - set stream parameters
240 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100241 * @rate: the sample rate
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900242 * @data_block_quadlets: the size of a data block in quadlet unit
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100243 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200244 * The parameters must be set before the stream is started, and must not be
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100245 * changed while the stream is running.
246 */
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900247int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
248 unsigned int data_block_quadlets)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100249{
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900250 unsigned int sfc;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100251
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900252 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900253 if (amdtp_rate_table[sfc] == rate)
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900254 break;
255 }
256 if (sfc == ARRAY_SIZE(amdtp_rate_table))
257 return -EINVAL;
Clemens Ladische84d15f2011-09-04 22:12:48 +0200258
Clemens Ladische84d15f2011-09-04 22:12:48 +0200259 s->sfc = sfc;
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900260 s->data_block_quadlets = data_block_quadlets;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200261 s->syt_interval = amdtp_syt_intervals[sfc];
Clemens Ladische84d15f2011-09-04 22:12:48 +0200262
263 /* default buffering in the device */
264 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
265 if (s->flags & CIP_BLOCKING)
266 /* additional buffering needed to adjust for no-data packets */
267 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900268
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900269 return 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100270}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900271EXPORT_SYMBOL(amdtp_stream_set_parameters);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100272
273/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900274 * amdtp_stream_get_max_payload - get the stream's packet size
275 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100276 *
277 * This function must not be called before the stream has been configured
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900278 * with amdtp_stream_set_parameters().
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100279 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900280unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100281{
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900282 unsigned int multiplier = 1;
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900283 unsigned int header_size = 0;
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900284
285 if (s->flags & CIP_JUMBO_PAYLOAD)
286 multiplier = 5;
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900287 if (!(s->flags & CIP_NO_HEADER))
288 header_size = 8;
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900289
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900290 return header_size +
291 s->syt_interval * s->data_block_quadlets * 4 * multiplier;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100292}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900293EXPORT_SYMBOL(amdtp_stream_get_max_payload);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100294
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200295/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900296 * amdtp_stream_pcm_prepare - prepare PCM device for running
297 * @s: the AMDTP stream
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200298 *
299 * This function should be called from the PCM device's .prepare callback.
300 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900301void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200302{
303 tasklet_kill(&s->period_tasklet);
304 s->pcm_buffer_pointer = 0;
305 s->pcm_period_pointer = 0;
306}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900307EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200308
Takashi Sakamoto875be092015-05-22 23:00:51 +0900309static unsigned int calculate_data_blocks(struct amdtp_stream *s,
310 unsigned int syt)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100311{
312 unsigned int phase, data_blocks;
313
Takashi Sakamoto875be092015-05-22 23:00:51 +0900314 /* Blocking mode. */
315 if (s->flags & CIP_BLOCKING) {
316 /* This module generate empty packet for 'no data'. */
317 if (syt == CIP_SYT_NO_INFO)
318 data_blocks = 0;
319 else
320 data_blocks = s->syt_interval;
321 /* Non-blocking mode. */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100322 } else {
Takashi Sakamoto875be092015-05-22 23:00:51 +0900323 if (!cip_sfc_is_base_44100(s->sfc)) {
324 /* Sample_rate / 8000 is an integer, and precomputed. */
325 data_blocks = s->data_block_state;
326 } else {
327 phase = s->data_block_state;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100328
329 /*
330 * This calculates the number of data blocks per packet so that
331 * 1) the overall rate is correct and exactly synchronized to
332 * the bus clock, and
333 * 2) packets with a rounded-up number of blocks occur as early
334 * as possible in the sequence (to prevent underruns of the
335 * device's buffer).
336 */
Takashi Sakamoto875be092015-05-22 23:00:51 +0900337 if (s->sfc == CIP_SFC_44100)
338 /* 6 6 5 6 5 6 5 ... */
339 data_blocks = 5 + ((phase & 1) ^
340 (phase == 0 || phase >= 40));
341 else
342 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
343 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
344 if (++phase >= (80 >> (s->sfc >> 1)))
345 phase = 0;
346 s->data_block_state = phase;
347 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100348 }
349
350 return data_blocks;
351}
352
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900353static unsigned int calculate_syt(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100354 unsigned int cycle)
355{
356 unsigned int syt_offset, phase, index, syt;
357
358 if (s->last_syt_offset < TICKS_PER_CYCLE) {
359 if (!cip_sfc_is_base_44100(s->sfc))
360 syt_offset = s->last_syt_offset + s->syt_offset_state;
361 else {
362 /*
363 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
364 * n * SYT_INTERVAL * 24576000 / sample_rate
365 * Modulo TICKS_PER_CYCLE, the difference between successive
366 * elements is about 1386.23. Rounding the results of this
367 * formula to the SYT precision results in a sequence of
368 * differences that begins with:
369 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
370 * This code generates _exactly_ the same sequence.
371 */
372 phase = s->syt_offset_state;
373 index = phase % 13;
374 syt_offset = s->last_syt_offset;
375 syt_offset += 1386 + ((index && !(index & 3)) ||
376 phase == 146);
377 if (++phase >= 147)
378 phase = 0;
379 s->syt_offset_state = phase;
380 }
381 } else
382 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
383 s->last_syt_offset = syt_offset;
384
Clemens Ladischbe454362011-03-15 07:55:02 +0100385 if (syt_offset < TICKS_PER_CYCLE) {
Clemens Ladische84d15f2011-09-04 22:12:48 +0200386 syt_offset += s->transfer_delay;
Clemens Ladischbe454362011-03-15 07:55:02 +0100387 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
388 syt += syt_offset % TICKS_PER_CYCLE;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100389
Takashi Sakamotob445db42014-04-25 22:44:43 +0900390 return syt & CIP_SYT_MASK;
Clemens Ladischbe454362011-03-15 07:55:02 +0100391 } else {
Takashi Sakamotob445db42014-04-25 22:44:43 +0900392 return CIP_SYT_NO_INFO;
Clemens Ladischbe454362011-03-15 07:55:02 +0100393 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100394}
395
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900396static void update_pcm_pointers(struct amdtp_stream *s,
397 struct snd_pcm_substream *pcm,
398 unsigned int frames)
Takashi Sakamoto65845f22014-08-29 13:40:45 +0900399{
400 unsigned int ptr;
401
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900402 ptr = s->pcm_buffer_pointer + frames;
403 if (ptr >= pcm->runtime->buffer_size)
404 ptr -= pcm->runtime->buffer_size;
Mark Rutland6aa7de02017-10-23 14:07:29 -0700405 WRITE_ONCE(s->pcm_buffer_pointer, ptr);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900406
407 s->pcm_period_pointer += frames;
408 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
409 s->pcm_period_pointer -= pcm->runtime->period_size;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900410 tasklet_hi_schedule(&s->period_tasklet);
411 }
412}
413
414static void pcm_period_tasklet(unsigned long data)
415{
416 struct amdtp_stream *s = (void *)data;
Mark Rutland6aa7de02017-10-23 14:07:29 -0700417 struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900418
419 if (pcm)
420 snd_pcm_period_elapsed(pcm);
421}
422
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900423static int queue_packet(struct amdtp_stream *s, unsigned int header_length,
424 unsigned int payload_length)
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900425{
426 struct fw_iso_packet p = {0};
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900427 int err = 0;
428
429 if (IS_ERR(s->context))
430 goto end;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900431
432 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900433 p.tag = s->tag;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900434 p.header_length = header_length;
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900435 if (payload_length > 0)
436 p.payload_length = payload_length;
437 else
438 p.skip = true;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900439 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
440 s->buffer.packets[s->packet_index].offset);
441 if (err < 0) {
442 dev_err(&s->unit->device, "queueing error: %d\n", err);
443 goto end;
444 }
445
446 if (++s->packet_index >= QUEUE_LENGTH)
447 s->packet_index = 0;
448end:
449 return err;
450}
451
452static inline int queue_out_packet(struct amdtp_stream *s,
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900453 unsigned int payload_length)
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900454{
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900455 return queue_packet(s, OUT_PACKET_HEADER_SIZE, payload_length);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900456}
457
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900458static inline int queue_in_packet(struct amdtp_stream *s)
459{
Takashi Sakamotocc4f8e92019-03-17 20:25:06 +0900460 return queue_packet(s, IR_HEADER_SIZE, s->max_payload_length);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900461}
462
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900463static int handle_out_packet(struct amdtp_stream *s,
464 unsigned int payload_length, unsigned int cycle,
Takashi Sakamotoa9c42842016-05-11 07:33:27 +0900465 unsigned int index)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100466{
467 __be32 *buffer;
Takashi Sakamoto390a1512016-05-09 23:15:55 +0900468 unsigned int syt;
469 unsigned int data_blocks;
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900470 unsigned int pcm_frames;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100471 struct snd_pcm_substream *pcm;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100472
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900473 buffer = s->buffer.packets[s->packet_index].buffer;
Takashi Sakamoto390a1512016-05-09 23:15:55 +0900474 syt = calculate_syt(s, cycle);
475 data_blocks = calculate_data_blocks(s, syt);
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900476 pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900477
Takashi Sakamoto9dae0172017-03-22 21:30:17 +0900478 if (s->flags & CIP_DBC_IS_END_EVENT)
479 s->data_block_counter =
480 (s->data_block_counter + data_blocks) & 0xff;
481
Mark Rutland6aa7de02017-10-23 14:07:29 -0700482 buffer[0] = cpu_to_be32(READ_ONCE(s->source_node_id_field) |
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900483 (s->data_block_quadlets << CIP_DBS_SHIFT) |
Takashi Sakamoto98638742017-03-22 21:30:16 +0900484 ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) |
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100485 s->data_block_counter);
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900486 buffer[1] = cpu_to_be32(CIP_EOH |
487 ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
488 ((s->fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
489 (syt & CIP_SYT_MASK));
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100490
Takashi Sakamoto9dae0172017-03-22 21:30:17 +0900491 if (!(s->flags & CIP_DBC_IS_END_EVENT))
492 s->data_block_counter =
493 (s->data_block_counter + data_blocks) & 0xff;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900494 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
Takashi Sakamoto0c95c1d2016-05-09 21:12:46 +0900495
Takashi Sakamotoa9c42842016-05-11 07:33:27 +0900496 trace_out_packet(s, cycle, buffer, payload_length, index);
Takashi Sakamoto0c95c1d2016-05-09 21:12:46 +0900497
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900498 if (queue_out_packet(s, payload_length) < 0)
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900499 return -EIO;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100500
Mark Rutland6aa7de02017-10-23 14:07:29 -0700501 pcm = READ_ONCE(s->pcm);
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900502 if (pcm && pcm_frames > 0)
503 update_pcm_pointers(s, pcm, pcm_frames);
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900504
505 /* No need to return the number of handled data blocks. */
506 return 0;
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200507}
508
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900509static int handle_out_packet_without_header(struct amdtp_stream *s,
510 unsigned int payload_length, unsigned int cycle,
511 unsigned int index)
512{
513 __be32 *buffer;
514 unsigned int syt;
515 unsigned int data_blocks;
516 unsigned int pcm_frames;
517 struct snd_pcm_substream *pcm;
518
519 buffer = s->buffer.packets[s->packet_index].buffer;
520 syt = calculate_syt(s, cycle);
521 data_blocks = calculate_data_blocks(s, syt);
522 pcm_frames = s->process_data_blocks(s, buffer, data_blocks, &syt);
523 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
524
525 payload_length = data_blocks * 4 * s->data_block_quadlets;
Takashi Sakamotob164d2f2017-04-09 21:33:27 +0900526
527 trace_out_packet_without_header(s, cycle, payload_length, data_blocks,
528 index);
529
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900530 if (queue_out_packet(s, payload_length) < 0)
531 return -EIO;
532
Mark Rutland6aa7de02017-10-23 14:07:29 -0700533 pcm = READ_ONCE(s->pcm);
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900534 if (pcm && pcm_frames > 0)
535 update_pcm_pointers(s, pcm, pcm_frames);
536
537 /* No need to return the number of handled data blocks. */
538 return 0;
539}
540
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900541static int handle_in_packet(struct amdtp_stream *s,
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900542 unsigned int payload_length, unsigned int cycle,
Takashi Sakamotoa9c42842016-05-11 07:33:27 +0900543 unsigned int index)
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900544{
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900545 __be32 *buffer;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900546 u32 cip_header[2];
Takashi Sakamoto98638742017-03-22 21:30:16 +0900547 unsigned int sph, fmt, fdf, syt;
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900548 unsigned int data_block_quadlets, data_block_counter, dbc_interval;
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900549 unsigned int data_blocks;
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900550 struct snd_pcm_substream *pcm;
551 unsigned int pcm_frames;
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900552 bool lost;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900553
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900554 buffer = s->buffer.packets[s->packet_index].buffer;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900555 cip_header[0] = be32_to_cpu(buffer[0]);
556 cip_header[1] = be32_to_cpu(buffer[1]);
557
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900558 trace_in_packet(s, cycle, cip_header, payload_length, index);
Takashi Sakamoto0c95c1d2016-05-09 21:12:46 +0900559
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900560 /*
561 * This module supports 'Two-quadlet CIP header with SYT field'.
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900562 * For convenience, also check FMT field is AM824 or not.
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900563 */
Takashi Sakamoto2128f782017-03-22 21:30:27 +0900564 if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
565 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) &&
566 (!(s->flags & CIP_HEADER_WITHOUT_EOH))) {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900567 dev_info_ratelimited(&s->unit->device,
568 "Invalid CIP header for AMDTP: %08X:%08X\n",
569 cip_header[0], cip_header[1]);
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900570 data_blocks = 0;
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900571 pcm_frames = 0;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900572 goto end;
573 }
574
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900575 /* Check valid protocol or not. */
Takashi Sakamoto98638742017-03-22 21:30:16 +0900576 sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT;
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900577 fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
Takashi Sakamoto98638742017-03-22 21:30:16 +0900578 if (sph != s->sph || fmt != s->fmt) {
Takashi Sakamoto2a7e1712015-10-11 22:33:50 +0900579 dev_info_ratelimited(&s->unit->device,
580 "Detect unexpected protocol: %08x %08x\n",
581 cip_header[0], cip_header[1]);
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900582 data_blocks = 0;
Takashi Sakamoto2a7e1712015-10-11 22:33:50 +0900583 pcm_frames = 0;
584 goto end;
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900585 }
586
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900587 /* Calculate data blocks */
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900588 fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT;
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900589 if (payload_length < 12 ||
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900590 (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900591 data_blocks = 0;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900592 } else {
593 data_block_quadlets =
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900594 (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900595 /* avoid division by zero */
596 if (data_block_quadlets == 0) {
Takashi Sakamoto12e0f432015-05-22 23:21:13 +0900597 dev_err(&s->unit->device,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900598 "Detect invalid value in dbs field: %08X\n",
599 cip_header[0]);
Takashi Sakamotoa9007052015-05-22 23:21:14 +0900600 return -EPROTO;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900601 }
Takashi Sakamoto69702232014-04-25 22:45:05 +0900602 if (s->flags & CIP_WRONG_DBS)
603 data_block_quadlets = s->data_block_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900604
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900605 data_blocks = (payload_length / 4 - 2) /
606 data_block_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900607 }
608
609 /* Check data block counter continuity */
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900610 data_block_counter = cip_header[0] & CIP_DBC_MASK;
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900611 if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
Takashi Sakamoto9d591242014-04-25 22:45:27 +0900612 s->data_block_counter != UINT_MAX)
613 data_block_counter = s->data_block_counter;
614
Takashi Sakamoto18f5ed32015-08-05 09:21:05 +0900615 if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) &&
616 data_block_counter == s->tx_first_dbc) ||
617 s->data_block_counter == UINT_MAX) {
Takashi Sakamotob84b1a22014-04-25 22:45:07 +0900618 lost = false;
619 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900620 lost = data_block_counter != s->data_block_counter;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900621 } else {
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900622 if (data_blocks > 0 && s->tx_dbc_interval > 0)
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900623 dbc_interval = s->tx_dbc_interval;
624 else
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900625 dbc_interval = data_blocks;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900626
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900627 lost = data_block_counter !=
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900628 ((s->data_block_counter + dbc_interval) & 0xff);
629 }
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900630
631 if (lost) {
Takashi Sakamoto12e0f432015-05-22 23:21:13 +0900632 dev_err(&s->unit->device,
633 "Detect discontinuity of CIP: %02X %02X\n",
634 s->data_block_counter, data_block_counter);
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900635 return -EIO;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900636 }
637
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900638 syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
639 pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900640
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900641 if (s->flags & CIP_DBC_IS_END_EVENT)
642 s->data_block_counter = data_block_counter;
643 else
644 s->data_block_counter =
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900645 (data_block_counter + data_blocks) & 0xff;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900646end:
647 if (queue_in_packet(s) < 0)
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900648 return -EIO;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900649
Mark Rutland6aa7de02017-10-23 14:07:29 -0700650 pcm = READ_ONCE(s->pcm);
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900651 if (pcm && pcm_frames > 0)
652 update_pcm_pointers(s, pcm, pcm_frames);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900653
Takashi Sakamoto31ea49b2015-05-28 00:02:59 +0900654 return 0;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900655}
656
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900657static int handle_in_packet_without_header(struct amdtp_stream *s,
Takashi Sakamotoada79fa2018-12-15 19:03:19 +0900658 unsigned int payload_length, unsigned int cycle,
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900659 unsigned int index)
660{
661 __be32 *buffer;
Takashi Sakamotoada79fa2018-12-15 19:03:19 +0900662 unsigned int payload_quadlets;
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900663 unsigned int data_blocks;
664 struct snd_pcm_substream *pcm;
665 unsigned int pcm_frames;
666
667 buffer = s->buffer.packets[s->packet_index].buffer;
Takashi Sakamotoada79fa2018-12-15 19:03:19 +0900668 payload_quadlets = payload_length / 4;
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900669 data_blocks = payload_quadlets / s->data_block_quadlets;
Takashi Sakamotob164d2f2017-04-09 21:33:27 +0900670
671 trace_in_packet_without_header(s, cycle, payload_quadlets, data_blocks,
672 index);
673
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900674 pcm_frames = s->process_data_blocks(s, buffer, data_blocks, NULL);
675 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
676
677 if (queue_in_packet(s) < 0)
678 return -EIO;
679
Mark Rutland6aa7de02017-10-23 14:07:29 -0700680 pcm = READ_ONCE(s->pcm);
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900681 if (pcm && pcm_frames > 0)
682 update_pcm_pointers(s, pcm, pcm_frames);
683
684 return 0;
685}
686
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900687/*
688 * In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
689 * the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
690 * it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
691 */
692static inline u32 compute_cycle_count(u32 tstamp)
693{
694 return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff);
695}
696
697static inline u32 increment_cycle_count(u32 cycle, unsigned int addend)
698{
699 cycle += addend;
700 if (cycle >= 8 * CYCLES_PER_SECOND)
701 cycle -= 8 * CYCLES_PER_SECOND;
702 return cycle;
703}
704
705static void out_stream_callback(struct fw_iso_context *context, u32 tstamp,
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900706 size_t header_length, void *header,
707 void *private_data)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100708{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900709 struct amdtp_stream *s = private_data;
Takashi Sakamoto390a1512016-05-09 23:15:55 +0900710 unsigned int i, packets = header_length / 4;
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900711 u32 cycle;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100712
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900713 if (s->packet_index < 0)
714 return;
715
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900716 cycle = compute_cycle_count(tstamp);
717
718 /* Align to actual cycle count for the last packet. */
719 cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100720
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900721 for (i = 0; i < packets; ++i) {
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900722 cycle = increment_cycle_count(cycle, 1);
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900723 if (s->handle_packet(s, 0, cycle, i) < 0) {
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900724 s->packet_index = -1;
Takashi Sakamoto4a9bfaf2017-06-11 16:08:21 +0900725 if (in_interrupt())
726 amdtp_stream_pcm_abort(s);
727 WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900728 return;
729 }
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900730 }
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900731
Clemens Ladisch13882a82011-05-02 09:33:56 +0200732 fw_iso_context_queue_flush(s->context);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100733}
734
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900735static void in_stream_callback(struct fw_iso_context *context, u32 tstamp,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900736 size_t header_length, void *header,
737 void *private_data)
738{
739 struct amdtp_stream *s = private_data;
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900740 unsigned int i, packets;
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900741 unsigned int payload_length, max_payload_length;
Takashi Sakamotocc4f8e92019-03-17 20:25:06 +0900742 __be32 *ctx_header = header;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900743
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900744 if (s->packet_index < 0)
745 return;
746
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900747 /* The number of packets in buffer */
Takashi Sakamotocc4f8e92019-03-17 20:25:06 +0900748 packets = header_length / IR_HEADER_SIZE;
Takashi Sakamotof90e2de2016-05-09 21:12:45 +0900749
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900750 /* For buffer-over-run prevention. */
Takashi Sakamotof91c9d72017-04-11 20:33:18 +0900751 max_payload_length = s->max_payload_length;
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900752
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900753 for (i = 0; i < packets; i++) {
Takashi Sakamotocc4f8e92019-03-17 20:25:06 +0900754 u32 iso_header = be32_to_cpu(ctx_header[0]);
755 unsigned int cycle;
756
757 tstamp = be32_to_cpu(ctx_header[1]) & HEADER_TSTAMP_MASK;
758 cycle = compute_cycle_count(tstamp);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900759
Takashi Sakamotof91c9d72017-04-11 20:33:18 +0900760 /* The number of bytes in this packet */
Takashi Sakamotocc4f8e92019-03-17 20:25:06 +0900761 payload_length = iso_header >> ISO_DATA_LENGTH_SHIFT;
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900762 if (payload_length > max_payload_length) {
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900763 dev_err(&s->unit->device,
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900764 "Detect jumbo payload: %04x %04x\n",
765 payload_length, max_payload_length);
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900766 break;
767 }
768
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900769 if (s->handle_packet(s, payload_length, cycle, i) < 0)
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900770 break;
Takashi Sakamotocc4f8e92019-03-17 20:25:06 +0900771
772 ctx_header += IR_HEADER_SIZE / sizeof(__be32);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900773 }
774
Takashi Sakamotodec63cc2016-05-09 23:15:53 +0900775 /* Queueing error or detecting invalid payload. */
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900776 if (i < packets) {
Takashi Sakamotodec63cc2016-05-09 23:15:53 +0900777 s->packet_index = -1;
Takashi Sakamoto4a9bfaf2017-06-11 16:08:21 +0900778 if (in_interrupt())
779 amdtp_stream_pcm_abort(s);
780 WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900781 return;
782 }
783
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900784 fw_iso_context_queue_flush(s->context);
785}
786
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900787/* this is executed one time */
788static void amdtp_stream_first_callback(struct fw_iso_context *context,
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900789 u32 tstamp, size_t header_length,
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900790 void *header, void *private_data)
791{
792 struct amdtp_stream *s = private_data;
Takashi Sakamotocc4f8e92019-03-17 20:25:06 +0900793 __be32 *ctx_header = header;
Takashi Sakamotoa04513f2017-03-22 21:30:15 +0900794 u32 cycle;
795 unsigned int packets;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900796
797 /*
798 * For in-stream, first packet has come.
799 * For out-stream, prepared to transmit first packet
800 */
801 s->callbacked = true;
802 wake_up(&s->callback_wait);
803
Takashi Sakamotoa04513f2017-03-22 21:30:15 +0900804 if (s->direction == AMDTP_IN_STREAM) {
Takashi Sakamotocc4f8e92019-03-17 20:25:06 +0900805 tstamp = be32_to_cpu(ctx_header[1]) & HEADER_TSTAMP_MASK;
806 cycle = compute_cycle_count(tstamp);
807
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900808 context->callback.sc = in_stream_callback;
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900809 if (s->flags & CIP_NO_HEADER)
810 s->handle_packet = handle_in_packet_without_header;
811 else
812 s->handle_packet = handle_in_packet;
Takashi Sakamotoa04513f2017-03-22 21:30:15 +0900813 } else {
814 packets = header_length / 4;
Takashi Sakamotocc4f8e92019-03-17 20:25:06 +0900815 cycle = compute_cycle_count(tstamp);
Takashi Sakamotoa04513f2017-03-22 21:30:15 +0900816 cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900817 context->callback.sc = out_stream_callback;
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900818 if (s->flags & CIP_NO_HEADER)
819 s->handle_packet = handle_out_packet_without_header;
820 else
821 s->handle_packet = handle_out_packet;
Takashi Sakamotoa04513f2017-03-22 21:30:15 +0900822 }
823
824 s->start_cycle = cycle;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900825
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900826 context->callback.sc(context, tstamp, header_length, header, s);
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900827}
828
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100829/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900830 * amdtp_stream_start - start transferring packets
831 * @s: the AMDTP stream to start
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100832 * @channel: the isochronous channel on the bus
833 * @speed: firewire speed code
834 *
835 * The stream cannot be started until it has been configured with
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900836 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
837 * device can be started.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100838 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900839int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100840{
841 static const struct {
842 unsigned int data_block;
843 unsigned int syt_offset;
844 } initial_state[] = {
845 [CIP_SFC_32000] = { 4, 3072 },
846 [CIP_SFC_48000] = { 6, 1024 },
847 [CIP_SFC_96000] = { 12, 1024 },
848 [CIP_SFC_192000] = { 24, 1024 },
849 [CIP_SFC_44100] = { 0, 67 },
850 [CIP_SFC_88200] = { 0, 67 },
851 [CIP_SFC_176400] = { 0, 67 },
852 };
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900853 unsigned int header_size;
854 enum dma_data_direction dir;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900855 int type, tag, err;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100856
857 mutex_lock(&s->mutex);
858
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900859 if (WARN_ON(amdtp_stream_running(s) ||
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900860 (s->data_block_quadlets < 1))) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100861 err = -EBADFD;
862 goto err_unlock;
863 }
864
Takashi Sakamoto62f00e42016-05-09 23:15:56 +0900865 if (s->direction == AMDTP_IN_STREAM)
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900866 s->data_block_counter = UINT_MAX;
867 else
868 s->data_block_counter = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100869 s->data_block_state = initial_state[s->sfc].data_block;
870 s->syt_offset_state = initial_state[s->sfc].syt_offset;
871 s->last_syt_offset = TICKS_PER_CYCLE;
872
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900873 /* initialize packet buffer */
874 if (s->direction == AMDTP_IN_STREAM) {
875 dir = DMA_FROM_DEVICE;
876 type = FW_ISO_CONTEXT_RECEIVE;
Takashi Sakamotocc4f8e92019-03-17 20:25:06 +0900877 header_size = IR_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900878 } else {
879 dir = DMA_TO_DEVICE;
880 type = FW_ISO_CONTEXT_TRANSMIT;
881 header_size = OUT_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900882 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100883 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900884 amdtp_stream_get_max_payload(s), dir);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100885 if (err < 0)
886 goto err_unlock;
887
888 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900889 type, channel, speed, header_size,
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900890 amdtp_stream_first_callback, s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100891 if (IS_ERR(s->context)) {
892 err = PTR_ERR(s->context);
893 if (err == -EBUSY)
894 dev_err(&s->unit->device,
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900895 "no free stream on this controller\n");
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100896 goto err_buffer;
897 }
898
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900899 amdtp_stream_update(s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100900
Takashi Sakamoto52759c092018-04-29 15:01:46 +0900901 if (s->direction == AMDTP_IN_STREAM)
902 s->max_payload_length = amdtp_stream_get_max_payload(s);
903
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900904 if (s->flags & CIP_NO_HEADER)
905 s->tag = TAG_NO_CIP_HEADER;
906 else
907 s->tag = TAG_CIP;
908
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100909 s->packet_index = 0;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900910 do {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900911 if (s->direction == AMDTP_IN_STREAM)
912 err = queue_in_packet(s);
913 else
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900914 err = queue_out_packet(s, 0);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900915 if (err < 0)
916 goto err_context;
917 } while (s->packet_index > 0);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100918
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900919 /* NOTE: TAG1 matches CIP. This just affects in stream. */
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900920 tag = FW_ISO_CONTEXT_MATCH_TAG1;
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900921 if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER))
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900922 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
923
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900924 s->callbacked = false;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900925 err = fw_iso_context_start(s->context, -1, 0, tag);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100926 if (err < 0)
927 goto err_context;
928
929 mutex_unlock(&s->mutex);
930
931 return 0;
932
933err_context:
934 fw_iso_context_destroy(s->context);
935 s->context = ERR_PTR(-1);
936err_buffer:
937 iso_packets_buffer_destroy(&s->buffer, s->unit);
938err_unlock:
939 mutex_unlock(&s->mutex);
940
941 return err;
942}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900943EXPORT_SYMBOL(amdtp_stream_start);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100944
945/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900946 * amdtp_stream_pcm_pointer - get the PCM buffer position
947 * @s: the AMDTP stream that transports the PCM data
Clemens Ladische9148dd2012-05-13 18:49:14 +0200948 *
949 * Returns the current buffer position, in frames.
950 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900951unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
Clemens Ladische9148dd2012-05-13 18:49:14 +0200952{
Takashi Sakamoto1dba9db2016-05-12 02:17:39 +0900953 /*
954 * This function is called in software IRQ context of period_tasklet or
955 * process context.
956 *
957 * When the software IRQ context was scheduled by software IRQ context
958 * of IR/IT contexts, queued packets were already handled. Therefore,
959 * no need to flush the queue in buffer anymore.
960 *
961 * When the process context reach here, some packets will be already
962 * queued in the buffer. These packets should be handled immediately
963 * to keep better granularity of PCM pointer.
964 *
965 * Later, the process context will sometimes schedules software IRQ
966 * context of the period_tasklet. Then, no need to flush the queue by
967 * the same reason as described for IR/IT contexts.
968 */
969 if (!in_interrupt() && amdtp_stream_running(s))
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200970 fw_iso_context_flush_completions(s->context);
Clemens Ladische9148dd2012-05-13 18:49:14 +0200971
Mark Rutland6aa7de02017-10-23 14:07:29 -0700972 return READ_ONCE(s->pcm_buffer_pointer);
Clemens Ladische9148dd2012-05-13 18:49:14 +0200973}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900974EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
Clemens Ladische9148dd2012-05-13 18:49:14 +0200975
976/**
Takashi Sakamoto875becf2017-06-07 09:38:05 +0900977 * amdtp_stream_pcm_ack - acknowledge queued PCM frames
978 * @s: the AMDTP stream that transfers the PCM frames
979 *
980 * Returns zero always.
981 */
982int amdtp_stream_pcm_ack(struct amdtp_stream *s)
983{
984 /*
985 * Process isochronous packets for recent isochronous cycle to handle
986 * queued PCM frames.
987 */
988 if (amdtp_stream_running(s))
989 fw_iso_context_flush_completions(s->context);
990
991 return 0;
992}
993EXPORT_SYMBOL(amdtp_stream_pcm_ack);
994
995/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900996 * amdtp_stream_update - update the stream after a bus reset
997 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100998 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900999void amdtp_stream_update(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001000{
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +09001001 /* Precomputing. */
Mark Rutland6aa7de02017-10-23 14:07:29 -07001002 WRITE_ONCE(s->source_node_id_field,
1003 (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & CIP_SID_MASK);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001004}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001005EXPORT_SYMBOL(amdtp_stream_update);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001006
1007/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001008 * amdtp_stream_stop - stop sending packets
1009 * @s: the AMDTP stream to stop
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001010 *
1011 * All PCM and MIDI devices of the stream must be stopped before the stream
1012 * itself can be stopped.
1013 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001014void amdtp_stream_stop(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001015{
1016 mutex_lock(&s->mutex);
1017
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001018 if (!amdtp_stream_running(s)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001019 mutex_unlock(&s->mutex);
1020 return;
1021 }
1022
Clemens Ladisch76fb8782012-05-13 22:03:09 +02001023 tasklet_kill(&s->period_tasklet);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001024 fw_iso_context_stop(s->context);
1025 fw_iso_context_destroy(s->context);
1026 s->context = ERR_PTR(-1);
1027 iso_packets_buffer_destroy(&s->buffer, s->unit);
1028
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +09001029 s->callbacked = false;
1030
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001031 mutex_unlock(&s->mutex);
1032}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001033EXPORT_SYMBOL(amdtp_stream_stop);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001034
1035/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001036 * amdtp_stream_pcm_abort - abort the running PCM device
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001037 * @s: the AMDTP stream about to be stopped
1038 *
1039 * If the isochronous stream needs to be stopped asynchronously, call this
1040 * function first to stop the PCM device.
1041 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001042void amdtp_stream_pcm_abort(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001043{
1044 struct snd_pcm_substream *pcm;
1045
Mark Rutland6aa7de02017-10-23 14:07:29 -07001046 pcm = READ_ONCE(s->pcm);
Takashi Iwai1fb85102014-11-07 17:08:28 +01001047 if (pcm)
1048 snd_pcm_stop_xrun(pcm);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001049}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001050EXPORT_SYMBOL(amdtp_stream_pcm_abort);