ALSA: firewire-lib: Avoid -Wflex-array-member-not-at-end warning
[linux-2.6-block.git] / sound / firewire / amdtp-stream.c
CommitLineData
da607e19 1// SPDX-License-Identifier: GPL-2.0-only
31ef9134
CL
2/*
3 * Audio and Music Data Transmission Protocol (IEC 61883-6) streams
4 * with Common Isochronous Packet (IEC 61883-1) headers
5 *
6 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
31ef9134
CL
7 */
8
9#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/firewire.h>
acfedcbe 12#include <linux/firewire-constants.h>
31ef9134
CL
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <sound/pcm.h>
7b2d99fa 16#include <sound/pcm_params.h>
d67c46b9 17#include "amdtp-stream.h"
31ef9134
CL
18
19#define TICKS_PER_CYCLE 3072
20#define CYCLES_PER_SECOND 8000
21#define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
22
3e106f4f 23#define OHCI_SECOND_MODULUS 8
10aa8e4a 24
0c95c1d6
TS
25/* Always support Linux tracing subsystem. */
26#define CREATE_TRACE_POINTS
27#include "amdtp-stream-trace.h"
28
ca5b5050 29#define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */
31ef9134 30
b445db44
TS
31/* isochronous header parameters */
32#define ISO_DATA_LENGTH_SHIFT 16
3b196c39 33#define TAG_NO_CIP_HEADER 0
31ef9134
CL
34#define TAG_CIP 1
35
67d92ee7
TS
36// Common Isochronous Packet (CIP) header parameters. Use two quadlets CIP header when supported.
37#define CIP_HEADER_QUADLETS 2
9a2820c1
TS
38#define CIP_EOH_SHIFT 31
39#define CIP_EOH (1u << CIP_EOH_SHIFT)
b445db44 40#define CIP_EOH_MASK 0x80000000
9a2820c1
TS
41#define CIP_SID_SHIFT 24
42#define CIP_SID_MASK 0x3f000000
43#define CIP_DBS_MASK 0x00ff0000
44#define CIP_DBS_SHIFT 16
9863874f
TS
45#define CIP_SPH_MASK 0x00000400
46#define CIP_SPH_SHIFT 10
9a2820c1
TS
47#define CIP_DBC_MASK 0x000000ff
48#define CIP_FMT_SHIFT 24
b445db44 49#define CIP_FMT_MASK 0x3f000000
9a2820c1
TS
50#define CIP_FDF_MASK 0x00ff0000
51#define CIP_FDF_SHIFT 16
fb25dcc8 52#define CIP_FDF_NO_DATA 0xff
b445db44
TS
53#define CIP_SYT_MASK 0x0000ffff
54#define CIP_SYT_NO_INFO 0xffff
f9e5ecdf 55#define CIP_SYT_CYCLE_MODULUS 16
fb25dcc8 56#define CIP_NO_DATA ((CIP_FDF_NO_DATA << CIP_FDF_SHIFT) | CIP_SYT_NO_INFO)
b445db44 57
67d92ee7
TS
58#define CIP_HEADER_SIZE (sizeof(__be32) * CIP_HEADER_QUADLETS)
59
51c29fd2 60/* Audio and Music transfer protocol specific parameters */
414ba022 61#define CIP_FMT_AM 0x10
2b3fc456 62#define AMDTP_FDF_NO_DATA 0xff
31ef9134 63
f11453c7 64// For iso header and tstamp.
67d92ee7
TS
65#define IR_CTX_HEADER_DEFAULT_QUADLETS 2
66// Add nothing.
67#define IR_CTX_HEADER_SIZE_NO_CIP (sizeof(__be32) * IR_CTX_HEADER_DEFAULT_QUADLETS)
68// Add two quadlets CIP header.
69#define IR_CTX_HEADER_SIZE_CIP (IR_CTX_HEADER_SIZE_NO_CIP + CIP_HEADER_SIZE)
cc4f8e91 70#define HEADER_TSTAMP_MASK 0x0000ffff
4b7da117 71
67d92ee7 72#define IT_PKT_HEADER_SIZE_CIP CIP_HEADER_SIZE
b18f0cfa
TS
73#define IT_PKT_HEADER_SIZE_NO_CIP 0 // Nothing.
74
6a3ce97d
TS
75// The initial firmware of OXFW970 can postpone transmission of packet during finishing
76// asynchronous transaction. This module accepts 5 cycles to skip as maximum to avoid buffer
77// overrun. Actual device can skip more, then this module stops the packet streaming.
78#define IR_JUMBO_PAYLOAD_MAX_SKIP_CYCLES 5
79
31ef9134 80/**
be4a2894
TS
81 * amdtp_stream_init - initialize an AMDTP stream structure
82 * @s: the AMDTP stream to initialize
31ef9134 83 * @unit: the target of the stream
3ff7e8f0 84 * @dir: the direction of stream
ffe66bbe 85 * @flags: the details of the streaming protocol consist of cip_flags enumeration-constants.
5955815e 86 * @fmt: the value of fmt field in CIP header
9a738ad1 87 * @process_ctx_payloads: callback handler to process payloads of isoc context
df075fee 88 * @protocol_size: the size to allocate newly for protocol
31ef9134 89 */
be4a2894 90int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
ffe66bbe 91 enum amdtp_stream_direction dir, unsigned int flags,
df075fee 92 unsigned int fmt,
9a738ad1 93 amdtp_stream_process_ctx_payloads_t process_ctx_payloads,
df075fee 94 unsigned int protocol_size)
31ef9134 95{
9a738ad1 96 if (process_ctx_payloads == NULL)
df075fee
TS
97 return -EINVAL;
98
99 s->protocol = kzalloc(protocol_size, GFP_KERNEL);
100 if (!s->protocol)
101 return -ENOMEM;
102
c6f224dc 103 s->unit = unit;
3ff7e8f0 104 s->direction = dir;
31ef9134
CL
105 s->flags = flags;
106 s->context = ERR_PTR(-1);
107 mutex_init(&s->mutex);
ec00f5e4 108 s->packet_index = 0;
31ef9134 109
bdaedca7 110 init_waitqueue_head(&s->ready_wait);
7b3b0d85 111
5955815e 112 s->fmt = fmt;
9a738ad1 113 s->process_ctx_payloads = process_ctx_payloads;
414ba022 114
31ef9134
CL
115 return 0;
116}
be4a2894 117EXPORT_SYMBOL(amdtp_stream_init);
31ef9134
CL
118
119/**
be4a2894
TS
120 * amdtp_stream_destroy - free stream resources
121 * @s: the AMDTP stream to destroy
31ef9134 122 */
be4a2894 123void amdtp_stream_destroy(struct amdtp_stream *s)
31ef9134 124{
44c376b9
TS
125 /* Not initialized. */
126 if (s->protocol == NULL)
127 return;
128
be4a2894 129 WARN_ON(amdtp_stream_running(s));
df075fee 130 kfree(s->protocol);
31ef9134 131 mutex_destroy(&s->mutex);
31ef9134 132}
be4a2894 133EXPORT_SYMBOL(amdtp_stream_destroy);
31ef9134 134
c5280e99 135const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
a7304e3b
CL
136 [CIP_SFC_32000] = 8,
137 [CIP_SFC_44100] = 8,
138 [CIP_SFC_48000] = 8,
139 [CIP_SFC_88200] = 16,
140 [CIP_SFC_96000] = 16,
141 [CIP_SFC_176400] = 32,
142 [CIP_SFC_192000] = 32,
143};
144EXPORT_SYMBOL(amdtp_syt_intervals);
145
f9503a68 146const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
1017abed
TS
147 [CIP_SFC_32000] = 32000,
148 [CIP_SFC_44100] = 44100,
149 [CIP_SFC_48000] = 48000,
150 [CIP_SFC_88200] = 88200,
151 [CIP_SFC_96000] = 96000,
152 [CIP_SFC_176400] = 176400,
153 [CIP_SFC_192000] = 192000,
154};
155EXPORT_SYMBOL(amdtp_rate_table);
156
59502295
TS
157static int apply_constraint_to_size(struct snd_pcm_hw_params *params,
158 struct snd_pcm_hw_rule *rule)
159{
160 struct snd_interval *s = hw_param_interval(params, rule->var);
161 const struct snd_interval *r =
162 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
826b5de9
TS
163 struct snd_interval t = {0};
164 unsigned int step = 0;
59502295
TS
165 int i;
166
167 for (i = 0; i < CIP_SFC_COUNT; ++i) {
826b5de9
TS
168 if (snd_interval_test(r, amdtp_rate_table[i]))
169 step = max(step, amdtp_syt_intervals[i]);
59502295
TS
170 }
171
826b5de9
TS
172 t.min = roundup(s->min, step);
173 t.max = rounddown(s->max, step);
174 t.integer = 1;
59502295
TS
175
176 return snd_interval_refine(s, &t);
177}
178
7b2d99fa
TS
179/**
180 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
181 * @s: the AMDTP stream, which must be initialized.
182 * @runtime: the PCM substream runtime
183 */
184int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
185 struct snd_pcm_runtime *runtime)
186{
55799c5a 187 struct snd_pcm_hardware *hw = &runtime->hw;
99921ec6
TS
188 unsigned int ctx_header_size;
189 unsigned int maximum_usec_per_period;
7b2d99fa
TS
190 int err;
191
d360870a 192 hw->info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
55799c5a
TS
193 SNDRV_PCM_INFO_INTERLEAVED |
194 SNDRV_PCM_INFO_JOINT_DUPLEX |
195 SNDRV_PCM_INFO_MMAP |
d360870a
TS
196 SNDRV_PCM_INFO_MMAP_VALID |
197 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP;
55799c5a 198
55799c5a
TS
199 hw->periods_min = 2;
200 hw->periods_max = UINT_MAX;
201
202 /* bytes for a frame */
203 hw->period_bytes_min = 4 * hw->channels_max;
204
205 /* Just to prevent from allocating much pages. */
206 hw->period_bytes_max = hw->period_bytes_min * 2048;
207 hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
208
99921ec6
TS
209 // Linux driver for 1394 OHCI controller voluntarily flushes isoc
210 // context when total size of accumulated context header reaches
2b3d2987 211 // PAGE_SIZE. This kicks work for the isoc context and brings
99921ec6
TS
212 // callback in the middle of scheduled interrupts.
213 // Although AMDTP streams in the same domain use the same events per
214 // IRQ, use the largest size of context header between IT/IR contexts.
215 // Here, use the value of context header in IR context is for both
216 // contexts.
217 if (!(s->flags & CIP_NO_HEADER))
218 ctx_header_size = IR_CTX_HEADER_SIZE_CIP;
219 else
220 ctx_header_size = IR_CTX_HEADER_SIZE_NO_CIP;
221 maximum_usec_per_period = USEC_PER_SEC * PAGE_SIZE /
222 CYCLES_PER_SECOND / ctx_header_size;
223
f706df4f
TS
224 // In IEC 61883-6, one isoc packet can transfer events up to the value
225 // of syt interval. This comes from the interval of isoc cycle. As 1394
226 // OHCI controller can generate hardware IRQ per isoc packet, the
227 // interval is 125 usec.
228 // However, there are two ways of transmission in IEC 61883-6; blocking
229 // and non-blocking modes. In blocking mode, the sequence of isoc packet
230 // includes 'empty' or 'NODATA' packets which include no event. In
231 // non-blocking mode, the number of events per packet is variable up to
232 // the syt interval.
233 // Due to the above protocol design, the minimum PCM frames per
234 // interrupt should be double of the value of syt interval, thus it is
235 // 250 usec.
7b2d99fa
TS
236 err = snd_pcm_hw_constraint_minmax(runtime,
237 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
f706df4f 238 250, maximum_usec_per_period);
7b2d99fa
TS
239 if (err < 0)
240 goto end;
241
242 /* Non-Blocking stream has no more constraints */
243 if (!(s->flags & CIP_BLOCKING))
244 goto end;
245
246 /*
247 * One AMDTP packet can include some frames. In blocking mode, the
248 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
249 * depending on its sampling rate. For accurate period interrupt, it's
ce991981 250 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
7b2d99fa 251 */
59502295
TS
252 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
253 apply_constraint_to_size, NULL,
826b5de9 254 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
59502295
TS
255 SNDRV_PCM_HW_PARAM_RATE, -1);
256 if (err < 0)
257 goto end;
59502295
TS
258 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
259 apply_constraint_to_size, NULL,
826b5de9 260 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
59502295
TS
261 SNDRV_PCM_HW_PARAM_RATE, -1);
262 if (err < 0)
263 goto end;
7b2d99fa
TS
264end:
265 return err;
266}
267EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
268
31ef9134 269/**
be4a2894
TS
270 * amdtp_stream_set_parameters - set stream parameters
271 * @s: the AMDTP stream to configure
31ef9134 272 * @rate: the sample rate
df075fee 273 * @data_block_quadlets: the size of a data block in quadlet unit
a36183f6
TS
274 * @pcm_frame_multiplier: the multiplier to compute the number of PCM frames by the number of AMDTP
275 * events.
31ef9134 276 *
a7304e3b 277 * The parameters must be set before the stream is started, and must not be
31ef9134
CL
278 * changed while the stream is running.
279 */
df075fee 280int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
a36183f6 281 unsigned int data_block_quadlets, unsigned int pcm_frame_multiplier)
31ef9134 282{
df075fee 283 unsigned int sfc;
31ef9134 284
547e631c 285 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
1017abed 286 if (amdtp_rate_table[sfc] == rate)
547e631c
TS
287 break;
288 }
289 if (sfc == ARRAY_SIZE(amdtp_rate_table))
290 return -EINVAL;
e84d15f6 291
e84d15f6 292 s->sfc = sfc;
df075fee 293 s->data_block_quadlets = data_block_quadlets;
a7304e3b 294 s->syt_interval = amdtp_syt_intervals[sfc];
e84d15f6 295
d3d10a4a 296 // default buffering in the device.
13d11f14
TS
297 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
298
299 // additional buffering needed to adjust for no-data packets.
300 if (s->flags & CIP_BLOCKING)
301 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
77d2a8a4 302
a36183f6
TS
303 s->pcm_frame_multiplier = pcm_frame_multiplier;
304
547e631c 305 return 0;
31ef9134 306}
be4a2894 307EXPORT_SYMBOL(amdtp_stream_set_parameters);
31ef9134 308
c75f3678
TS
309// The CIP header is processed in context header apart from context payload.
310static int amdtp_stream_get_max_ctx_payload_size(struct amdtp_stream *s)
311{
312 unsigned int multiplier;
313
314 if (s->flags & CIP_JUMBO_PAYLOAD)
315 multiplier = IR_JUMBO_PAYLOAD_MAX_SKIP_CYCLES;
316 else
317 multiplier = 1;
318
319 return s->syt_interval * s->data_block_quadlets * sizeof(__be32) * multiplier;
320}
321
31ef9134 322/**
be4a2894
TS
323 * amdtp_stream_get_max_payload - get the stream's packet size
324 * @s: the AMDTP stream
31ef9134
CL
325 *
326 * This function must not be called before the stream has been configured
be4a2894 327 * with amdtp_stream_set_parameters().
31ef9134 328 */
be4a2894 329unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
31ef9134 330{
c75f3678 331 unsigned int cip_header_size;
a2064710 332
3b196c39 333 if (!(s->flags & CIP_NO_HEADER))
67d92ee7 334 cip_header_size = CIP_HEADER_SIZE;
c75f3678
TS
335 else
336 cip_header_size = 0;
a2064710 337
c75f3678 338 return cip_header_size + amdtp_stream_get_max_ctx_payload_size(s);
31ef9134 339}
be4a2894 340EXPORT_SYMBOL(amdtp_stream_get_max_payload);
31ef9134 341
76fb8789 342/**
be4a2894
TS
343 * amdtp_stream_pcm_prepare - prepare PCM device for running
344 * @s: the AMDTP stream
76fb8789
CL
345 *
346 * This function should be called from the PCM device's .prepare callback.
347 */
be4a2894 348void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
76fb8789 349{
76fb8789
CL
350 s->pcm_buffer_pointer = 0;
351 s->pcm_period_pointer = 0;
352}
be4a2894 353EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
76fb8789 354
af13842c
TS
355#define prev_packet_desc(s, desc) \
356 list_prev_entry_circular(desc, &s->packet_descs_list, link)
357
c9f3ac2a 358static void pool_blocking_data_blocks(struct amdtp_stream *s, struct seq_desc *descs,
119c446a 359 unsigned int size, unsigned int pos, unsigned int count)
31ef9134 360{
c9f3ac2a
TS
361 const unsigned int syt_interval = s->syt_interval;
362 int i;
31ef9134 363
c9f3ac2a 364 for (i = 0; i < count; ++i) {
119c446a 365 struct seq_desc *desc = descs + pos;
c9f3ac2a
TS
366
367 if (desc->syt_offset != CIP_SYT_NO_INFO)
368 desc->data_blocks = syt_interval;
875be091 369 else
c9f3ac2a
TS
370 desc->data_blocks = 0;
371
119c446a 372 pos = (pos + 1) % size;
c9f3ac2a
TS
373 }
374}
375
376static void pool_ideal_nonblocking_data_blocks(struct amdtp_stream *s, struct seq_desc *descs,
119c446a 377 unsigned int size, unsigned int pos,
c9f3ac2a
TS
378 unsigned int count)
379{
380 const enum cip_sfc sfc = s->sfc;
381 unsigned int state = s->ctx_data.rx.data_block_state;
382 int i;
383
384 for (i = 0; i < count; ++i) {
119c446a 385 struct seq_desc *desc = descs + pos;
c9f3ac2a 386
274fc355 387 if (!cip_sfc_is_base_44100(sfc)) {
d3d10a4a 388 // Sample_rate / 8000 is an integer, and precomputed.
c9f3ac2a 389 desc->data_blocks = state;
875be091 390 } else {
c9f3ac2a 391 unsigned int phase = state;
31ef9134
CL
392
393 /*
394 * This calculates the number of data blocks per packet so that
395 * 1) the overall rate is correct and exactly synchronized to
396 * the bus clock, and
397 * 2) packets with a rounded-up number of blocks occur as early
398 * as possible in the sequence (to prevent underruns of the
399 * device's buffer).
400 */
274fc355 401 if (sfc == CIP_SFC_44100)
875be091 402 /* 6 6 5 6 5 6 5 ... */
c9f3ac2a 403 desc->data_blocks = 5 + ((phase & 1) ^ (phase == 0 || phase >= 40));
875be091
TS
404 else
405 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
c9f3ac2a 406 desc->data_blocks = 11 * (sfc >> 1) + (phase == 0);
274fc355 407 if (++phase >= (80 >> (sfc >> 1)))
875be091 408 phase = 0;
c9f3ac2a 409 state = phase;
875be091 410 }
c9f3ac2a 411
119c446a 412 pos = (pos + 1) % size;
31ef9134
CL
413 }
414
c9f3ac2a 415 s->ctx_data.rx.data_block_state = state;
31ef9134
CL
416}
417
816d8482
TS
418static unsigned int calculate_syt_offset(unsigned int *last_syt_offset,
419 unsigned int *syt_offset_state, enum cip_sfc sfc)
31ef9134 420{
816d8482 421 unsigned int syt_offset;
31ef9134 422
816d8482
TS
423 if (*last_syt_offset < TICKS_PER_CYCLE) {
424 if (!cip_sfc_is_base_44100(sfc))
425 syt_offset = *last_syt_offset + *syt_offset_state;
31ef9134
CL
426 else {
427 /*
428 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
429 * n * SYT_INTERVAL * 24576000 / sample_rate
430 * Modulo TICKS_PER_CYCLE, the difference between successive
431 * elements is about 1386.23. Rounding the results of this
432 * formula to the SYT precision results in a sequence of
433 * differences that begins with:
434 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
435 * This code generates _exactly_ the same sequence.
436 */
816d8482
TS
437 unsigned int phase = *syt_offset_state;
438 unsigned int index = phase % 13;
439
440 syt_offset = *last_syt_offset;
31ef9134
CL
441 syt_offset += 1386 + ((index && !(index & 3)) ||
442 phase == 146);
443 if (++phase >= 147)
444 phase = 0;
816d8482 445 *syt_offset_state = phase;
31ef9134
CL
446 }
447 } else
816d8482
TS
448 syt_offset = *last_syt_offset - TICKS_PER_CYCLE;
449 *last_syt_offset = syt_offset;
31ef9134 450
83cfb5c5
TS
451 if (syt_offset >= TICKS_PER_CYCLE)
452 syt_offset = CIP_SYT_NO_INFO;
31ef9134 453
83cfb5c5 454 return syt_offset;
31ef9134
CL
455}
456
c79b7158 457static void pool_ideal_syt_offsets(struct amdtp_stream *s, struct seq_desc *descs,
119c446a 458 unsigned int size, unsigned int pos, unsigned int count)
c79b7158
TS
459{
460 const enum cip_sfc sfc = s->sfc;
461 unsigned int last = s->ctx_data.rx.last_syt_offset;
462 unsigned int state = s->ctx_data.rx.syt_offset_state;
463 int i;
464
465 for (i = 0; i < count; ++i) {
119c446a 466 struct seq_desc *desc = descs + pos;
c79b7158
TS
467
468 desc->syt_offset = calculate_syt_offset(&last, &state, sfc);
469
119c446a 470 pos = (pos + 1) % size;
c79b7158
TS
471 }
472
473 s->ctx_data.rx.last_syt_offset = last;
474 s->ctx_data.rx.syt_offset_state = state;
475}
476
f9e5ecdf
TS
477static unsigned int compute_syt_offset(unsigned int syt, unsigned int cycle,
478 unsigned int transfer_delay)
479{
480 unsigned int cycle_lo = (cycle % CYCLES_PER_SECOND) & 0x0f;
481 unsigned int syt_cycle_lo = (syt & 0xf000) >> 12;
482 unsigned int syt_offset;
483
484 // Round up.
485 if (syt_cycle_lo < cycle_lo)
486 syt_cycle_lo += CIP_SYT_CYCLE_MODULUS;
487 syt_cycle_lo -= cycle_lo;
488
489 // Subtract transfer delay so that the synchronization offset is not so large
490 // at transmission.
491 syt_offset = syt_cycle_lo * TICKS_PER_CYCLE + (syt & 0x0fff);
492 if (syt_offset < transfer_delay)
493 syt_offset += CIP_SYT_CYCLE_MODULUS * TICKS_PER_CYCLE;
494
495 return syt_offset - transfer_delay;
496}
497
39c2649c
TS
498// Both of the producer and consumer of the queue runs in the same clock of IEEE 1394 bus.
499// Additionally, the sequence of tx packets is severely checked against any discontinuity
500// before filling entries in the queue. The calculation is safe even if it looks fragile by
501// overrun.
502static unsigned int calculate_cached_cycle_count(struct amdtp_stream *s, unsigned int head)
503{
504 const unsigned int cache_size = s->ctx_data.tx.cache.size;
cccddec4 505 unsigned int cycles = s->ctx_data.tx.cache.pos;
39c2649c
TS
506
507 if (cycles < head)
508 cycles += cache_size;
509 cycles -= head;
510
511 return cycles;
512}
513
cec371ff 514static void cache_seq(struct amdtp_stream *s, const struct pkt_desc *src, unsigned int desc_count)
f9e5ecdf
TS
515{
516 const unsigned int transfer_delay = s->transfer_delay;
517 const unsigned int cache_size = s->ctx_data.tx.cache.size;
518 struct seq_desc *cache = s->ctx_data.tx.cache.descs;
cccddec4 519 unsigned int cache_pos = s->ctx_data.tx.cache.pos;
f9e5ecdf
TS
520 bool aware_syt = !(s->flags & CIP_UNAWARE_SYT);
521 int i;
522
523 for (i = 0; i < desc_count; ++i) {
cccddec4 524 struct seq_desc *dst = cache + cache_pos;
f9e5ecdf
TS
525
526 if (aware_syt && src->syt != CIP_SYT_NO_INFO)
527 dst->syt_offset = compute_syt_offset(src->syt, src->cycle, transfer_delay);
528 else
529 dst->syt_offset = CIP_SYT_NO_INFO;
530 dst->data_blocks = src->data_blocks;
531
cccddec4 532 cache_pos = (cache_pos + 1) % cache_size;
cec371ff 533 src = amdtp_stream_next_packet_desc(s, src);
f9e5ecdf
TS
534 }
535
cccddec4 536 s->ctx_data.tx.cache.pos = cache_pos;
f9e5ecdf
TS
537}
538
119c446a
TS
539static void pool_ideal_seq_descs(struct amdtp_stream *s, struct seq_desc *descs, unsigned int size,
540 unsigned int pos, unsigned int count)
6f24bb8a 541{
119c446a 542 pool_ideal_syt_offsets(s, descs, size, pos, count);
c79b7158 543
c9f3ac2a 544 if (s->flags & CIP_BLOCKING)
119c446a 545 pool_blocking_data_blocks(s, descs, size, pos, count);
c9f3ac2a 546 else
119c446a 547 pool_ideal_nonblocking_data_blocks(s, descs, size, pos, count);
6f24bb8a
TS
548}
549
119c446a
TS
550static void pool_replayed_seq(struct amdtp_stream *s, struct seq_desc *descs, unsigned int size,
551 unsigned int pos, unsigned int count)
39c2649c
TS
552{
553 struct amdtp_stream *target = s->ctx_data.rx.replay_target;
554 const struct seq_desc *cache = target->ctx_data.tx.cache.descs;
555 const unsigned int cache_size = target->ctx_data.tx.cache.size;
c38d8cff 556 unsigned int cache_pos = s->ctx_data.rx.cache_pos;
39c2649c
TS
557 int i;
558
559 for (i = 0; i < count; ++i) {
c38d8cff
TS
560 descs[pos] = cache[cache_pos];
561 cache_pos = (cache_pos + 1) % cache_size;
119c446a 562 pos = (pos + 1) % size;
39c2649c
TS
563 }
564
c38d8cff 565 s->ctx_data.rx.cache_pos = cache_pos;
39c2649c
TS
566}
567
f2bdee85
TS
568static void pool_seq_descs(struct amdtp_stream *s, struct seq_desc *descs, unsigned int size,
569 unsigned int pos, unsigned int count)
39c2649c
TS
570{
571 struct amdtp_domain *d = s->domain;
119c446a
TS
572 void (*pool_seq_descs)(struct amdtp_stream *s, struct seq_desc *descs, unsigned int size,
573 unsigned int pos, unsigned int count);
39c2649c 574
2f21a177 575 if (!d->replay.enable || !s->ctx_data.rx.replay_target) {
119c446a 576 pool_seq_descs = pool_ideal_seq_descs;
2f21a177
TS
577 } else {
578 if (!d->replay.on_the_fly) {
119c446a 579 pool_seq_descs = pool_replayed_seq;
2f21a177
TS
580 } else {
581 struct amdtp_stream *tx = s->ctx_data.rx.replay_target;
582 const unsigned int cache_size = tx->ctx_data.tx.cache.size;
c38d8cff
TS
583 const unsigned int cache_pos = s->ctx_data.rx.cache_pos;
584 unsigned int cached_cycles = calculate_cached_cycle_count(tx, cache_pos);
2f21a177
TS
585
586 if (cached_cycles > count && cached_cycles > cache_size / 2)
119c446a 587 pool_seq_descs = pool_replayed_seq;
2f21a177 588 else
119c446a 589 pool_seq_descs = pool_ideal_seq_descs;
2f21a177
TS
590 }
591 }
119c446a
TS
592
593 pool_seq_descs(s, descs, size, pos, count);
39c2649c
TS
594}
595
4b7da117
TS
596static void update_pcm_pointers(struct amdtp_stream *s,
597 struct snd_pcm_substream *pcm,
598 unsigned int frames)
65845f29
TS
599{
600 unsigned int ptr;
601
4b7da117
TS
602 ptr = s->pcm_buffer_pointer + frames;
603 if (ptr >= pcm->runtime->buffer_size)
604 ptr -= pcm->runtime->buffer_size;
6aa7de05 605 WRITE_ONCE(s->pcm_buffer_pointer, ptr);
4b7da117
TS
606
607 s->pcm_period_pointer += frames;
608 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
609 s->pcm_period_pointer -= pcm->runtime->period_size;
d360870a
TS
610
611 // The program in user process should periodically check the status of intermediate
612 // buffer associated to PCM substream to process PCM frames in the buffer, instead
613 // of receiving notification of period elapsed by poll wait.
7ba5ca32 614 if (!pcm->runtime->no_period_wakeup) {
3b86ec63 615 if (in_softirq()) {
7ba5ca32
TS
616 // In software IRQ context for 1394 OHCI.
617 snd_pcm_period_elapsed(pcm);
618 } else {
619 // In process context of ALSA PCM application under acquired lock of
620 // PCM substream.
621 snd_pcm_period_elapsed_under_stream_lock(pcm);
622 }
623 }
4b7da117
TS
624 }
625}
626
e229853d
TS
627static int queue_packet(struct amdtp_stream *s, struct fw_iso_packet *params,
628 bool sched_irq)
4b7da117 629{
6007bf54 630 int err;
df9160b9 631
e229853d 632 params->interrupt = sched_irq;
6007bf54
TS
633 params->tag = s->tag;
634 params->sy = 0;
df9160b9 635
6007bf54 636 err = fw_iso_context_queue(s->context, params, &s->buffer.iso_buffer,
4b7da117
TS
637 s->buffer.packets[s->packet_index].offset);
638 if (err < 0) {
639 dev_err(&s->unit->device, "queueing error: %d\n", err);
640 goto end;
641 }
642
a0e02331 643 if (++s->packet_index >= s->queue_size)
4b7da117
TS
644 s->packet_index = 0;
645end:
646 return err;
647}
648
649static inline int queue_out_packet(struct amdtp_stream *s,
e229853d 650 struct fw_iso_packet *params, bool sched_irq)
4b7da117 651{
b18f0cfa
TS
652 params->skip =
653 !!(params->header_length == 0 && params->payload_length == 0);
e229853d 654 return queue_packet(s, params, sched_irq);
4b7da117
TS
655}
656
6007bf54 657static inline int queue_in_packet(struct amdtp_stream *s,
60dd4929 658 struct fw_iso_packet *params)
2b3fc456 659{
6007bf54
TS
660 // Queue one packet for IR context.
661 params->header_length = s->ctx_data.tx.ctx_header_size;
662 params->payload_length = s->ctx_data.tx.max_ctx_payload_length;
663 params->skip = false;
60dd4929 664 return queue_packet(s, params, false);
2b3fc456
TS
665}
666
252219c7 667static void generate_cip_header(struct amdtp_stream *s, __be32 cip_header[2],
860d798c 668 unsigned int data_block_counter, unsigned int syt)
252219c7
TS
669{
670 cip_header[0] = cpu_to_be32(READ_ONCE(s->source_node_id_field) |
671 (s->data_block_quadlets << CIP_DBS_SHIFT) |
672 ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) |
860d798c 673 data_block_counter);
252219c7
TS
674 cip_header[1] = cpu_to_be32(CIP_EOH |
675 ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
676 ((s->ctx_data.rx.fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
677 (syt & CIP_SYT_MASK));
678}
679
6bc1a269 680static void build_it_pkt_header(struct amdtp_stream *s, unsigned int cycle,
233dbbc7 681 struct fw_iso_packet *params, unsigned int header_length,
860d798c
TS
682 unsigned int data_blocks,
683 unsigned int data_block_counter,
fef4e61b 684 unsigned int syt, unsigned int index, u32 curr_cycle_time)
31ef9134 685{
0ebf3ceb 686 unsigned int payload_length;
16be4589 687 __be32 *cip_header;
20e44577 688
0ebf3ceb
TS
689 payload_length = data_blocks * sizeof(__be32) * s->data_block_quadlets;
690 params->payload_length = payload_length;
691
233dbbc7 692 if (header_length > 0) {
6bc1a269 693 cip_header = (__be32 *)params->header;
860d798c 694 generate_cip_header(s, cip_header, data_block_counter, syt);
233dbbc7 695 params->header_length = header_length;
b18f0cfa
TS
696 } else {
697 cip_header = NULL;
698 }
31ef9134 699
233dbbc7 700 trace_amdtp_packet(s, cycle, cip_header, payload_length + header_length, data_blocks,
fef4e61b 701 data_block_counter, s->packet_index, index, curr_cycle_time);
3b196c39
TS
702}
703
e335425b
TS
704static int check_cip_header(struct amdtp_stream *s, const __be32 *buf,
705 unsigned int payload_length,
a35463d1
TS
706 unsigned int *data_blocks,
707 unsigned int *data_block_counter, unsigned int *syt)
2b3fc456
TS
708{
709 u32 cip_header[2];
e335425b
TS
710 unsigned int sph;
711 unsigned int fmt;
712 unsigned int fdf;
a35463d1 713 unsigned int dbc;
c8bdf49b 714 bool lost;
2b3fc456 715
e335425b
TS
716 cip_header[0] = be32_to_cpu(buf[0]);
717 cip_header[1] = be32_to_cpu(buf[1]);
2b3fc456
TS
718
719 /*
720 * This module supports 'Two-quadlet CIP header with SYT field'.
77d2a8a4 721 * For convenience, also check FMT field is AM824 or not.
2b3fc456 722 */
2128f78f
TS
723 if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
724 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) &&
725 (!(s->flags & CIP_HEADER_WITHOUT_EOH))) {
2b3fc456
TS
726 dev_info_ratelimited(&s->unit->device,
727 "Invalid CIP header for AMDTP: %08X:%08X\n",
728 cip_header[0], cip_header[1]);
e335425b 729 return -EAGAIN;
2b3fc456
TS
730 }
731
414ba022 732 /* Check valid protocol or not. */
9863874f 733 sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT;
414ba022 734 fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
9863874f 735 if (sph != s->sph || fmt != s->fmt) {
2a7e1713
TS
736 dev_info_ratelimited(&s->unit->device,
737 "Detect unexpected protocol: %08x %08x\n",
738 cip_header[0], cip_header[1]);
e335425b 739 return -EAGAIN;
414ba022
TS
740 }
741
2b3fc456 742 /* Calculate data blocks */
414ba022 743 fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT;
4fd18787 744 if (payload_length == 0 || (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
e335425b 745 *data_blocks = 0;
2b3fc456 746 } else {
e335425b
TS
747 unsigned int data_block_quadlets =
748 (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
2b3fc456
TS
749 /* avoid division by zero */
750 if (data_block_quadlets == 0) {
12e0f438 751 dev_err(&s->unit->device,
2b3fc456
TS
752 "Detect invalid value in dbs field: %08X\n",
753 cip_header[0]);
a9007054 754 return -EPROTO;
2b3fc456 755 }
69702239
TS
756 if (s->flags & CIP_WRONG_DBS)
757 data_block_quadlets = s->data_block_quadlets;
2b3fc456 758
4fd18787 759 *data_blocks = payload_length / sizeof(__be32) / data_block_quadlets;
2b3fc456
TS
760 }
761
762 /* Check data block counter continuity */
a35463d1 763 dbc = cip_header[0] & CIP_DBC_MASK;
e335425b 764 if (*data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
a35463d1
TS
765 *data_block_counter != UINT_MAX)
766 dbc = *data_block_counter;
9d59124c 767
a35463d1
TS
768 if ((dbc == 0x00 && (s->flags & CIP_SKIP_DBC_ZERO_CHECK)) ||
769 *data_block_counter == UINT_MAX) {
b84b1a27
TS
770 lost = false;
771 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
a35463d1 772 lost = dbc != *data_block_counter;
d9cd0065 773 } else {
e335425b
TS
774 unsigned int dbc_interval;
775
4a486439
TS
776 if (!(s->flags & CIP_DBC_IS_PAYLOAD_QUADLETS)) {
777 if (*data_blocks > 0 && s->ctx_data.tx.dbc_interval > 0)
778 dbc_interval = s->ctx_data.tx.dbc_interval;
779 else
780 dbc_interval = *data_blocks;
781 } else {
782 dbc_interval = payload_length / sizeof(__be32);
783 }
d9cd0065 784
a35463d1 785 lost = dbc != ((*data_block_counter + dbc_interval) & 0xff);
d9cd0065 786 }
c8bdf49b
TS
787
788 if (lost) {
12e0f438
TS
789 dev_err(&s->unit->device,
790 "Detect discontinuity of CIP: %02X %02X\n",
a35463d1 791 *data_block_counter, dbc);
6fc6b9ce 792 return -EIO;
2b3fc456
TS
793 }
794
753e7179
TS
795 *data_block_counter = dbc;
796
8070d265
TS
797 if (!(s->flags & CIP_UNAWARE_SYT))
798 *syt = cip_header[1] & CIP_SYT_MASK;
2b3fc456 799
e335425b
TS
800 return 0;
801}
802
98e3e43b
TS
803static int parse_ir_ctx_header(struct amdtp_stream *s, unsigned int cycle,
804 const __be32 *ctx_header,
a35463d1
TS
805 unsigned int *data_blocks,
806 unsigned int *data_block_counter,
fef4e61b
TS
807 unsigned int *syt, unsigned int packet_index, unsigned int index,
808 u32 curr_cycle_time)
e335425b 809{
ebd2a647 810 unsigned int payload_length;
f11453c7 811 const __be32 *cip_header;
395f41e2 812 unsigned int cip_header_size;
e335425b 813
ebd2a647 814 payload_length = be32_to_cpu(ctx_header[0]) >> ISO_DATA_LENGTH_SHIFT;
395f41e2
TS
815
816 if (!(s->flags & CIP_NO_HEADER))
67d92ee7 817 cip_header_size = CIP_HEADER_SIZE;
395f41e2
TS
818 else
819 cip_header_size = 0;
820
ebd2a647 821 if (payload_length > cip_header_size + s->ctx_data.tx.max_ctx_payload_length) {
e335425b
TS
822 dev_err(&s->unit->device,
823 "Detect jumbo payload: %04x %04x\n",
ebd2a647 824 payload_length, cip_header_size + s->ctx_data.tx.max_ctx_payload_length);
e335425b
TS
825 return -EIO;
826 }
827
395f41e2 828 if (cip_header_size > 0) {
ebd2a647 829 if (payload_length >= cip_header_size) {
344f0f82
TS
830 int err;
831
67d92ee7 832 cip_header = ctx_header + IR_CTX_HEADER_DEFAULT_QUADLETS;
4fd18787
TS
833 err = check_cip_header(s, cip_header, payload_length - cip_header_size,
834 data_blocks, data_block_counter, syt);
c09010ee
TS
835 if (err < 0)
836 return err;
837 } else {
838 // Handle the cycle so that empty packet arrives.
839 cip_header = NULL;
840 *data_blocks = 0;
841 *syt = 0;
842 }
947b437e
TS
843 } else {
844 cip_header = NULL;
ebd2a647 845 *data_blocks = payload_length / sizeof(__be32) / s->data_block_quadlets;
98e3e43b 846 *syt = 0;
7fbf9096 847
a35463d1
TS
848 if (*data_block_counter == UINT_MAX)
849 *data_block_counter = 0;
e335425b
TS
850 }
851
ebd2a647 852 trace_amdtp_packet(s, cycle, cip_header, payload_length, *data_blocks,
fef4e61b 853 *data_block_counter, packet_index, index, curr_cycle_time);
e335425b 854
344f0f82 855 return 0;
2b3fc456
TS
856}
857
26cd1e58
TS
858// In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
859// the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
860// it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
af13842c
TS
861static inline u32 compute_ohci_iso_ctx_cycle_count(u32 tstamp)
862{
863 return (((tstamp >> 13) & 0x07) * CYCLES_PER_SECOND) + (tstamp & 0x1fff);
864}
865
3e106f4f 866static inline u32 compute_ohci_cycle_count(__be32 ctx_header_tstamp)
73fc7f08 867{
26cd1e58 868 u32 tstamp = be32_to_cpu(ctx_header_tstamp) & HEADER_TSTAMP_MASK;
af13842c 869 return compute_ohci_iso_ctx_cycle_count(tstamp);
73fc7f08
TS
870}
871
3e106f4f 872static inline u32 increment_ohci_cycle_count(u32 cycle, unsigned int addend)
73fc7f08
TS
873{
874 cycle += addend;
3e106f4f
TS
875 if (cycle >= OHCI_SECOND_MODULUS * CYCLES_PER_SECOND)
876 cycle -= OHCI_SECOND_MODULUS * CYCLES_PER_SECOND;
73fc7f08
TS
877 return cycle;
878}
879
af13842c
TS
880static inline u32 decrement_ohci_cycle_count(u32 minuend, u32 subtrahend)
881{
882 if (minuend < subtrahend)
883 minuend += OHCI_SECOND_MODULUS * CYCLES_PER_SECOND;
884
885 return minuend - subtrahend;
886}
887
705794c5
TS
888static int compare_ohci_cycle_count(u32 lval, u32 rval)
889{
890 if (lval == rval)
891 return 0;
892 else if (lval < rval && rval - lval < OHCI_SECOND_MODULUS * CYCLES_PER_SECOND / 2)
893 return -1;
894 else
895 return 1;
896}
897
26cd1e58 898// Align to actual cycle count for the packet which is going to be scheduled.
a0e02331
TS
899// This module queued the same number of isochronous cycle as the size of queue
900// to kip isochronous cycle, therefore it's OK to just increment the cycle by
901// the size of queue for scheduled cycle.
3e106f4f
TS
902static inline u32 compute_ohci_it_cycle(const __be32 ctx_header_tstamp,
903 unsigned int queue_size)
26cd1e58 904{
3e106f4f
TS
905 u32 cycle = compute_ohci_cycle_count(ctx_header_tstamp);
906 return increment_ohci_cycle_count(cycle, queue_size);
26cd1e58
TS
907}
908
cec371ff 909static int generate_tx_packet_descs(struct amdtp_stream *s, struct pkt_desc *desc,
cccddec4
TS
910 const __be32 *ctx_header, unsigned int packet_count,
911 unsigned int *desc_count)
753e7179 912{
705794c5 913 unsigned int next_cycle = s->next_cycle;
753e7179 914 unsigned int dbc = s->data_block_counter;
814b4312
TS
915 unsigned int packet_index = s->packet_index;
916 unsigned int queue_size = s->queue_size;
d8dc8720 917 u32 curr_cycle_time = 0;
753e7179
TS
918 int i;
919 int err;
920
fef4e61b
TS
921 if (trace_amdtp_packet_enabled())
922 (void)fw_card_read_cycle_time(fw_parent_device(s->unit)->card, &curr_cycle_time);
923
73246fc4 924 *desc_count = 0;
cccddec4 925 for (i = 0; i < packet_count; ++i) {
753e7179 926 unsigned int cycle;
705794c5 927 bool lost;
753e7179
TS
928 unsigned int data_blocks;
929 unsigned int syt;
930
3e106f4f 931 cycle = compute_ohci_cycle_count(ctx_header[1]);
705794c5
TS
932 lost = (next_cycle != cycle);
933 if (lost) {
934 if (s->flags & CIP_NO_HEADER) {
935 // Fireface skips transmission just for an isoc cycle corresponding
936 // to empty packet.
73246fc4
TS
937 unsigned int prev_cycle = next_cycle;
938
705794c5
TS
939 next_cycle = increment_ohci_cycle_count(next_cycle, 1);
940 lost = (next_cycle != cycle);
73246fc4
TS
941 if (!lost) {
942 // Prepare a description for the skipped cycle for
943 // sequence replay.
944 desc->cycle = prev_cycle;
945 desc->syt = 0;
946 desc->data_blocks = 0;
947 desc->data_block_counter = dbc;
948 desc->ctx_payload = NULL;
cec371ff 949 desc = amdtp_stream_next_packet_desc(s, desc);
73246fc4
TS
950 ++(*desc_count);
951 }
705794c5
TS
952 } else if (s->flags & CIP_JUMBO_PAYLOAD) {
953 // OXFW970 skips transmission for several isoc cycles during
73246fc4
TS
954 // asynchronous transaction. The sequence replay is impossible due
955 // to the reason.
705794c5
TS
956 unsigned int safe_cycle = increment_ohci_cycle_count(next_cycle,
957 IR_JUMBO_PAYLOAD_MAX_SKIP_CYCLES);
77ce9654 958 lost = (compare_ohci_cycle_count(safe_cycle, cycle) < 0);
705794c5
TS
959 }
960 if (lost) {
961 dev_err(&s->unit->device, "Detect discontinuity of cycle: %d %d\n",
962 next_cycle, cycle);
963 return -EIO;
964 }
965 }
753e7179 966
ebd2a647 967 err = parse_ir_ctx_header(s, cycle, ctx_header, &data_blocks, &dbc, &syt,
fef4e61b 968 packet_index, i, curr_cycle_time);
753e7179
TS
969 if (err < 0)
970 return err;
971
972 desc->cycle = cycle;
973 desc->syt = syt;
974 desc->data_blocks = data_blocks;
975 desc->data_block_counter = dbc;
814b4312 976 desc->ctx_payload = s->buffer.packets[packet_index].buffer;
753e7179
TS
977
978 if (!(s->flags & CIP_DBC_IS_END_EVENT))
979 dbc = (dbc + desc->data_blocks) & 0xff;
980
705794c5 981 next_cycle = increment_ohci_cycle_count(next_cycle, 1);
cec371ff 982 desc = amdtp_stream_next_packet_desc(s, desc);
73246fc4 983 ++(*desc_count);
705794c5 984 ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header);
814b4312 985 packet_index = (packet_index + 1) % queue_size;
753e7179
TS
986 }
987
705794c5 988 s->next_cycle = next_cycle;
753e7179
TS
989 s->data_block_counter = dbc;
990
991 return 0;
992}
993
83cfb5c5
TS
994static unsigned int compute_syt(unsigned int syt_offset, unsigned int cycle,
995 unsigned int transfer_delay)
996{
997 unsigned int syt;
998
999 syt_offset += transfer_delay;
1000 syt = ((cycle + syt_offset / TICKS_PER_CYCLE) << 12) |
1001 (syt_offset % TICKS_PER_CYCLE);
1002 return syt & CIP_SYT_MASK;
1003}
1004
cec371ff 1005static void generate_rx_packet_descs(struct amdtp_stream *s, struct pkt_desc *desc,
f2bdee85 1006 const __be32 *ctx_header, unsigned int packet_count)
f4f6ae7b 1007{
f2bdee85
TS
1008 struct seq_desc *seq_descs = s->ctx_data.rx.seq.descs;
1009 unsigned int seq_size = s->ctx_data.rx.seq.size;
1010 unsigned int seq_pos = s->ctx_data.rx.seq.pos;
f4f6ae7b 1011 unsigned int dbc = s->data_block_counter;
8070d265 1012 bool aware_syt = !(s->flags & CIP_UNAWARE_SYT);
f4f6ae7b
TS
1013 int i;
1014
f2bdee85
TS
1015 pool_seq_descs(s, seq_descs, seq_size, seq_pos, packet_count);
1016
1017 for (i = 0; i < packet_count; ++i) {
a0e02331 1018 unsigned int index = (s->packet_index + i) % s->queue_size;
f2bdee85 1019 const struct seq_desc *seq = seq_descs + seq_pos;
f4f6ae7b 1020
3e106f4f 1021 desc->cycle = compute_ohci_it_cycle(*ctx_header, s->queue_size);
69efd5c4 1022
13d11f14
TS
1023 if (aware_syt && seq->syt_offset != CIP_SYT_NO_INFO)
1024 desc->syt = compute_syt(seq->syt_offset, desc->cycle, s->transfer_delay);
1025 else
8070d265 1026 desc->syt = CIP_SYT_NO_INFO;
8070d265 1027
69efd5c4 1028 desc->data_blocks = seq->data_blocks;
f4f6ae7b
TS
1029
1030 if (s->flags & CIP_DBC_IS_END_EVENT)
1031 dbc = (dbc + desc->data_blocks) & 0xff;
1032
1033 desc->data_block_counter = dbc;
1034
1035 if (!(s->flags & CIP_DBC_IS_END_EVENT))
1036 dbc = (dbc + desc->data_blocks) & 0xff;
1037
1038 desc->ctx_payload = s->buffer.packets[index].buffer;
1039
f2bdee85 1040 seq_pos = (seq_pos + 1) % seq_size;
cec371ff 1041 desc = amdtp_stream_next_packet_desc(s, desc);
69efd5c4 1042
f4f6ae7b
TS
1043 ++ctx_header;
1044 }
1045
1046 s->data_block_counter = dbc;
f2bdee85 1047 s->ctx_data.rx.seq.pos = seq_pos;
f4f6ae7b
TS
1048}
1049
fce9b013
TS
1050static inline void cancel_stream(struct amdtp_stream *s)
1051{
1052 s->packet_index = -1;
3b86ec63 1053 if (in_softirq())
fce9b013
TS
1054 amdtp_stream_pcm_abort(s);
1055 WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
1056}
1057
af13842c
TS
1058static snd_pcm_sframes_t compute_pcm_extra_delay(struct amdtp_stream *s,
1059 const struct pkt_desc *desc, unsigned int count)
1060{
1061 unsigned int data_block_count = 0;
1062 u32 latest_cycle;
1063 u32 cycle_time;
1064 u32 curr_cycle;
1065 u32 cycle_gap;
1066 int i, err;
1067
1068 if (count == 0)
1069 goto end;
1070
1071 // Forward to the latest record.
1072 for (i = 0; i < count - 1; ++i)
1073 desc = amdtp_stream_next_packet_desc(s, desc);
1074 latest_cycle = desc->cycle;
1075
1076 err = fw_card_read_cycle_time(fw_parent_device(s->unit)->card, &cycle_time);
1077 if (err < 0)
1078 goto end;
1079
1080 // Compute cycle count with lower 3 bits of second field and cycle field like timestamp
1081 // format of 1394 OHCI isochronous context.
1082 curr_cycle = compute_ohci_iso_ctx_cycle_count((cycle_time >> 12) & 0x0000ffff);
1083
1084 if (s->direction == AMDTP_IN_STREAM) {
1085 // NOTE: The AMDTP packet descriptor should be for the past isochronous cycle since
1086 // it corresponds to arrived isochronous packet.
1087 if (compare_ohci_cycle_count(latest_cycle, curr_cycle) > 0)
1088 goto end;
1089 cycle_gap = decrement_ohci_cycle_count(curr_cycle, latest_cycle);
1090
1091 // NOTE: estimate delay by recent history of arrived AMDTP packets. The estimated
1092 // value expectedly corresponds to a few packets (0-2) since the packet arrived at
1093 // the most recent isochronous cycle has been already processed.
1094 for (i = 0; i < cycle_gap; ++i) {
1095 desc = amdtp_stream_next_packet_desc(s, desc);
1096 data_block_count += desc->data_blocks;
1097 }
1098 } else {
1099 // NOTE: The AMDTP packet descriptor should be for the future isochronous cycle
1100 // since it was already scheduled.
1101 if (compare_ohci_cycle_count(latest_cycle, curr_cycle) < 0)
1102 goto end;
1103 cycle_gap = decrement_ohci_cycle_count(latest_cycle, curr_cycle);
1104
1105 // NOTE: use history of scheduled packets.
1106 for (i = 0; i < cycle_gap; ++i) {
1107 data_block_count += desc->data_blocks;
1108 desc = prev_packet_desc(s, desc);
1109 }
1110 }
1111end:
1112 return data_block_count * s->pcm_frame_multiplier;
1113}
1114
0f5cfcb2 1115static void process_ctx_payloads(struct amdtp_stream *s,
a36183f6 1116 const struct pkt_desc *desc,
0cac60c7 1117 unsigned int count)
31ef9134 1118{
9a738ad1 1119 struct snd_pcm_substream *pcm;
a36183f6 1120 int i;
5e2ece0f 1121
9a738ad1 1122 pcm = READ_ONCE(s->pcm);
7fc693e4 1123 s->process_ctx_payloads(s, desc, count, pcm);
a36183f6
TS
1124
1125 if (pcm) {
1126 unsigned int data_block_count = 0;
1127
af13842c
TS
1128 pcm->runtime->delay = compute_pcm_extra_delay(s, desc, count);
1129
a36183f6
TS
1130 for (i = 0; i < count; ++i) {
1131 data_block_count += desc->data_blocks;
1132 desc = amdtp_stream_next_packet_desc(s, desc);
1133 }
1134
1135 update_pcm_pointers(s, pcm, data_block_count * s->pcm_frame_multiplier);
1136 }
0f5cfcb2
TS
1137}
1138
9b1fcd9b
TS
1139static void process_rx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1140 void *header, void *private_data)
0f5cfcb2
TS
1141{
1142 struct amdtp_stream *s = private_data;
69efd5c4 1143 const struct amdtp_domain *d = s->domain;
0f5cfcb2 1144 const __be32 *ctx_header = header;
9b1fcd9b 1145 const unsigned int events_per_period = d->events_per_period;
60dd4929 1146 unsigned int event_count = s->ctx_data.rx.event_count;
f0117128 1147 struct pkt_desc *desc = s->packet_descs_cursor;
233dbbc7 1148 unsigned int pkt_header_length;
a0e02331 1149 unsigned int packets;
fef4e61b 1150 u32 curr_cycle_time;
d360870a 1151 bool need_hw_irq;
0f5cfcb2
TS
1152 int i;
1153
1154 if (s->packet_index < 0)
1155 return;
1156
a0e02331
TS
1157 // Calculate the number of packets in buffer and check XRUN.
1158 packets = header_length / sizeof(*ctx_header);
1159
cec371ff 1160 generate_rx_packet_descs(s, desc, ctx_header, packets);
0f5cfcb2 1161
cec371ff 1162 process_ctx_payloads(s, desc, packets);
5e2ece0f 1163
233dbbc7
TS
1164 if (!(s->flags & CIP_NO_HEADER))
1165 pkt_header_length = IT_PKT_HEADER_SIZE_CIP;
1166 else
1167 pkt_header_length = 0;
1168
d360870a
TS
1169 if (s == d->irq_target) {
1170 // At NO_PERIOD_WAKEUP mode, the packets for all IT/IR contexts are processed by
1171 // the tasks of user process operating ALSA PCM character device by calling ioctl(2)
1172 // with some requests, instead of scheduled hardware IRQ of an IT context.
1173 struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
1174 need_hw_irq = !pcm || !pcm->runtime->no_period_wakeup;
1175 } else {
1176 need_hw_irq = false;
1177 }
1178
fef4e61b
TS
1179 if (trace_amdtp_packet_enabled())
1180 (void)fw_card_read_cycle_time(fw_parent_device(s->unit)->card, &curr_cycle_time);
1181
5e2ece0f 1182 for (i = 0; i < packets; ++i) {
1d717123
GS
1183 DEFINE_FLEX(struct fw_iso_packet, template, header,
1184 header_length, CIP_HEADER_QUADLETS);
e229853d 1185 bool sched_irq = false;
31ef9134 1186
1d717123 1187 build_it_pkt_header(s, desc->cycle, template, pkt_header_length,
f4f6ae7b 1188 desc->data_blocks, desc->data_block_counter,
fef4e61b 1189 desc->syt, i, curr_cycle_time);
6bc1a269 1190
2472cfb3 1191 if (s == s->domain->irq_target) {
60dd4929
TS
1192 event_count += desc->data_blocks;
1193 if (event_count >= events_per_period) {
1194 event_count -= events_per_period;
d360870a 1195 sched_irq = need_hw_irq;
60dd4929 1196 }
e229853d
TS
1197 }
1198
1d717123 1199 if (queue_out_packet(s, template, sched_irq) < 0) {
fce9b013 1200 cancel_stream(s);
a4103bd7
TS
1201 return;
1202 }
cec371ff
TS
1203
1204 desc = amdtp_stream_next_packet_desc(s, desc);
ccccad86 1205 }
a4103bd7 1206
60dd4929 1207 s->ctx_data.rx.event_count = event_count;
f0117128 1208 s->packet_descs_cursor = desc;
31ef9134
CL
1209}
1210
9b1fcd9b
TS
1211static void skip_rx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1212 void *header, void *private_data)
1213{
1214 struct amdtp_stream *s = private_data;
1215 struct amdtp_domain *d = s->domain;
1216 const __be32 *ctx_header = header;
1217 unsigned int packets;
1218 unsigned int cycle;
1219 int i;
1220
1221 if (s->packet_index < 0)
1222 return;
1223
1224 packets = header_length / sizeof(*ctx_header);
1225
1226 cycle = compute_ohci_it_cycle(ctx_header[packets - 1], s->queue_size);
1227 s->next_cycle = increment_ohci_cycle_count(cycle, 1);
1228
1229 for (i = 0; i < packets; ++i) {
1230 struct fw_iso_packet params = {
1231 .header_length = 0,
1232 .payload_length = 0,
1233 };
1234 bool sched_irq = (s == d->irq_target && i == packets - 1);
1235
1236 if (queue_out_packet(s, &params, sched_irq) < 0) {
1237 cancel_stream(s);
1238 return;
1239 }
1240 }
1241}
1242
1243static void irq_target_callback(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1244 void *header, void *private_data);
1245
1246static void process_rx_packets_intermediately(struct fw_iso_context *context, u32 tstamp,
1247 size_t header_length, void *header, void *private_data)
1248{
1249 struct amdtp_stream *s = private_data;
1250 struct amdtp_domain *d = s->domain;
1251 __be32 *ctx_header = header;
1252 const unsigned int queue_size = s->queue_size;
1253 unsigned int packets;
1254 unsigned int offset;
1255
1256 if (s->packet_index < 0)
1257 return;
1258
1259 packets = header_length / sizeof(*ctx_header);
1260
1261 offset = 0;
1262 while (offset < packets) {
1263 unsigned int cycle = compute_ohci_it_cycle(ctx_header[offset], queue_size);
1264
1265 if (compare_ohci_cycle_count(cycle, d->processing_cycle.rx_start) >= 0)
1266 break;
1267
1268 ++offset;
1269 }
1270
1271 if (offset > 0) {
1272 unsigned int length = sizeof(*ctx_header) * offset;
1273
1274 skip_rx_packets(context, tstamp, length, ctx_header, private_data);
1275 if (amdtp_streaming_error(s))
1276 return;
1277
1278 ctx_header += offset;
1279 header_length -= length;
1280 }
1281
1282 if (offset < packets) {
bdaedca7
TS
1283 s->ready_processing = true;
1284 wake_up(&s->ready_wait);
1285
c38d8cff
TS
1286 if (d->replay.enable)
1287 s->ctx_data.rx.cache_pos = 0;
1288
9b1fcd9b
TS
1289 process_rx_packets(context, tstamp, header_length, ctx_header, private_data);
1290 if (amdtp_streaming_error(s))
1291 return;
1292
1293 if (s == d->irq_target)
1294 s->context->callback.sc = irq_target_callback;
1295 else
1296 s->context->callback.sc = process_rx_packets;
1297 }
1298}
1299
da3623ab
TS
1300static void process_tx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1301 void *header, void *private_data)
2b3fc456
TS
1302{
1303 struct amdtp_stream *s = private_data;
cc4f8e91 1304 __be32 *ctx_header = header;
f0117128 1305 struct pkt_desc *desc = s->packet_descs_cursor;
cccddec4 1306 unsigned int packet_count;
73246fc4 1307 unsigned int desc_count;
753e7179
TS
1308 int i;
1309 int err;
2b3fc456 1310
a4103bd7
TS
1311 if (s->packet_index < 0)
1312 return;
1313
a0e02331 1314 // Calculate the number of packets in buffer and check XRUN.
cccddec4 1315 packet_count = header_length / s->ctx_data.tx.ctx_header_size;
f90e2ded 1316
73246fc4 1317 desc_count = 0;
cec371ff 1318 err = generate_tx_packet_descs(s, desc, ctx_header, packet_count, &desc_count);
753e7179
TS
1319 if (err < 0) {
1320 if (err != -EAGAIN) {
1321 cancel_stream(s);
1322 return;
1323 }
5e2ece0f 1324 } else {
f9e5ecdf
TS
1325 struct amdtp_domain *d = s->domain;
1326
cec371ff 1327 process_ctx_payloads(s, desc, desc_count);
f9e5ecdf
TS
1328
1329 if (d->replay.enable)
cec371ff 1330 cache_seq(s, desc, desc_count);
f0117128
TS
1331
1332 for (i = 0; i < desc_count; ++i)
1333 desc = amdtp_stream_next_packet_desc(s, desc);
1334 s->packet_descs_cursor = desc;
5e2ece0f
TS
1335 }
1336
cccddec4 1337 for (i = 0; i < packet_count; ++i) {
5e2ece0f 1338 struct fw_iso_packet params = {0};
2b3fc456 1339
60dd4929 1340 if (queue_in_packet(s, &params) < 0) {
753e7179
TS
1341 cancel_stream(s);
1342 return;
1343 }
7b3b0d85 1344 }
60dd4929
TS
1345}
1346
da3623ab
TS
1347static void drop_tx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1348 void *header, void *private_data)
1349{
1350 struct amdtp_stream *s = private_data;
1351 const __be32 *ctx_header = header;
1352 unsigned int packets;
1353 unsigned int cycle;
1354 int i;
1355
1356 if (s->packet_index < 0)
1357 return;
1358
1359 packets = header_length / s->ctx_data.tx.ctx_header_size;
1360
1361 ctx_header += (packets - 1) * s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header);
1362 cycle = compute_ohci_cycle_count(ctx_header[1]);
1363 s->next_cycle = increment_ohci_cycle_count(cycle, 1);
1364
1365 for (i = 0; i < packets; ++i) {
1366 struct fw_iso_packet params = {0};
1367
1368 if (queue_in_packet(s, &params) < 0) {
1369 cancel_stream(s);
1370 return;
1371 }
1372 }
1373}
1374
1375static void process_tx_packets_intermediately(struct fw_iso_context *context, u32 tstamp,
1376 size_t header_length, void *header, void *private_data)
1377{
1378 struct amdtp_stream *s = private_data;
1379 struct amdtp_domain *d = s->domain;
1380 __be32 *ctx_header;
1381 unsigned int packets;
1382 unsigned int offset;
1383
1384 if (s->packet_index < 0)
1385 return;
1386
1387 packets = header_length / s->ctx_data.tx.ctx_header_size;
1388
1389 offset = 0;
1390 ctx_header = header;
1391 while (offset < packets) {
1392 unsigned int cycle = compute_ohci_cycle_count(ctx_header[1]);
1393
1394 if (compare_ohci_cycle_count(cycle, d->processing_cycle.tx_start) >= 0)
1395 break;
1396
1397 ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(__be32);
1398 ++offset;
1399 }
1400
1401 ctx_header = header;
1402
1403 if (offset > 0) {
1404 size_t length = s->ctx_data.tx.ctx_header_size * offset;
1405
1406 drop_tx_packets(context, tstamp, length, ctx_header, s);
1407 if (amdtp_streaming_error(s))
1408 return;
1409
1410 ctx_header += length / sizeof(*ctx_header);
1411 header_length -= length;
1412 }
1413
1414 if (offset < packets) {
bdaedca7
TS
1415 s->ready_processing = true;
1416 wake_up(&s->ready_wait);
1417
da3623ab
TS
1418 process_tx_packets(context, tstamp, header_length, ctx_header, s);
1419 if (amdtp_streaming_error(s))
1420 return;
1421
1422 context->callback.sc = process_tx_packets;
1423 }
1424}
1425
fb25dcc8
TS
1426static void drop_tx_packets_initially(struct fw_iso_context *context, u32 tstamp,
1427 size_t header_length, void *header, void *private_data)
1428{
1429 struct amdtp_stream *s = private_data;
1430 struct amdtp_domain *d = s->domain;
1431 __be32 *ctx_header;
1432 unsigned int count;
1433 unsigned int events;
1434 int i;
1435
1436 if (s->packet_index < 0)
1437 return;
1438
1439 count = header_length / s->ctx_data.tx.ctx_header_size;
1440
1441 // Attempt to detect any event in the batch of packets.
1442 events = 0;
1443 ctx_header = header;
1444 for (i = 0; i < count; ++i) {
1445 unsigned int payload_quads =
1446 (be32_to_cpu(*ctx_header) >> ISO_DATA_LENGTH_SHIFT) / sizeof(__be32);
1447 unsigned int data_blocks;
1448
1449 if (s->flags & CIP_NO_HEADER) {
1450 data_blocks = payload_quads / s->data_block_quadlets;
1451 } else {
1452 __be32 *cip_headers = ctx_header + IR_CTX_HEADER_DEFAULT_QUADLETS;
1453
1454 if (payload_quads < CIP_HEADER_QUADLETS) {
1455 data_blocks = 0;
1456 } else {
1457 payload_quads -= CIP_HEADER_QUADLETS;
1458
1459 if (s->flags & CIP_UNAWARE_SYT) {
1460 data_blocks = payload_quads / s->data_block_quadlets;
1461 } else {
1462 u32 cip1 = be32_to_cpu(cip_headers[1]);
1463
1464 // NODATA packet can includes any data blocks but they are
1465 // not available as event.
1466 if ((cip1 & CIP_NO_DATA) == CIP_NO_DATA)
1467 data_blocks = 0;
1468 else
1469 data_blocks = payload_quads / s->data_block_quadlets;
1470 }
1471 }
1472 }
1473
1474 events += data_blocks;
1475
1476 ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(__be32);
1477 }
1478
1479 drop_tx_packets(context, tstamp, header_length, header, s);
1480
1481 if (events > 0)
1482 s->ctx_data.tx.event_starts = true;
1483
1484 // Decide the cycle count to begin processing content of packet in IR contexts.
1485 {
1486 unsigned int stream_count = 0;
1487 unsigned int event_starts_count = 0;
1488 unsigned int cycle = UINT_MAX;
1489
1490 list_for_each_entry(s, &d->streams, list) {
1491 if (s->direction == AMDTP_IN_STREAM) {
1492 ++stream_count;
1493 if (s->ctx_data.tx.event_starts)
1494 ++event_starts_count;
1495 }
1496 }
1497
1498 if (stream_count == event_starts_count) {
1499 unsigned int next_cycle;
1500
1501 list_for_each_entry(s, &d->streams, list) {
1502 if (s->direction != AMDTP_IN_STREAM)
1503 continue;
1504
1505 next_cycle = increment_ohci_cycle_count(s->next_cycle,
1506 d->processing_cycle.tx_init_skip);
1507 if (cycle == UINT_MAX ||
1508 compare_ohci_cycle_count(next_cycle, cycle) > 0)
1509 cycle = next_cycle;
1510
1511 s->context->callback.sc = process_tx_packets_intermediately;
1512 }
1513
1514 d->processing_cycle.tx_start = cycle;
1515 }
1516 }
1517}
1518
9b1fcd9b 1519static void process_ctxs_in_domain(struct amdtp_domain *d)
60dd4929 1520{
60dd4929
TS
1521 struct amdtp_stream *s;
1522
60dd4929 1523 list_for_each_entry(s, &d->streams, list) {
9b1fcd9b 1524 if (s != d->irq_target && amdtp_stream_running(s))
60dd4929 1525 fw_iso_context_flush_completions(s->context);
9b1fcd9b
TS
1526
1527 if (amdtp_streaming_error(s))
1528 goto error;
60dd4929
TS
1529 }
1530
1531 return;
1532error:
9b1fcd9b
TS
1533 if (amdtp_stream_running(d->irq_target))
1534 cancel_stream(d->irq_target);
60dd4929
TS
1535
1536 list_for_each_entry(s, &d->streams, list) {
1537 if (amdtp_stream_running(s))
1538 cancel_stream(s);
1539 }
2b3fc456
TS
1540}
1541
9b1fcd9b
TS
1542static void irq_target_callback(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1543 void *header, void *private_data)
1544{
1545 struct amdtp_stream *s = private_data;
1546 struct amdtp_domain *d = s->domain;
9b1fcd9b
TS
1547
1548 process_rx_packets(context, tstamp, header_length, header, private_data);
1549 process_ctxs_in_domain(d);
1550}
1551
1552static void irq_target_callback_intermediately(struct fw_iso_context *context, u32 tstamp,
1553 size_t header_length, void *header, void *private_data)
1554{
1555 struct amdtp_stream *s = private_data;
1556 struct amdtp_domain *d = s->domain;
9b1fcd9b
TS
1557
1558 process_rx_packets_intermediately(context, tstamp, header_length, header, private_data);
1559 process_ctxs_in_domain(d);
1560}
1561
1562static void irq_target_callback_skip(struct fw_iso_context *context, u32 tstamp,
1563 size_t header_length, void *header, void *private_data)
1564{
1565 struct amdtp_stream *s = private_data;
1566 struct amdtp_domain *d = s->domain;
39c2649c 1567 bool ready_to_start;
9b1fcd9b
TS
1568
1569 skip_rx_packets(context, tstamp, header_length, header, private_data);
1570 process_ctxs_in_domain(d);
1571
2f21a177 1572 if (d->replay.enable && !d->replay.on_the_fly) {
39c2649c
TS
1573 unsigned int rx_count = 0;
1574 unsigned int rx_ready_count = 0;
1575 struct amdtp_stream *rx;
1576
1577 list_for_each_entry(rx, &d->streams, list) {
1578 struct amdtp_stream *tx;
1579 unsigned int cached_cycles;
1580
1581 if (rx->direction != AMDTP_OUT_STREAM)
1582 continue;
1583 ++rx_count;
1584
1585 tx = rx->ctx_data.rx.replay_target;
1586 cached_cycles = calculate_cached_cycle_count(tx, 0);
1587 if (cached_cycles > tx->ctx_data.tx.cache.size / 2)
1588 ++rx_ready_count;
1589 }
1590
1591 ready_to_start = (rx_count == rx_ready_count);
1592 } else {
1593 ready_to_start = true;
1594 }
1595
9b1fcd9b
TS
1596 // Decide the cycle count to begin processing content of packet in IT contexts. All of IT
1597 // contexts are expected to start and get callback when reaching here.
39c2649c
TS
1598 if (ready_to_start) {
1599 unsigned int cycle = s->next_cycle;
1600 list_for_each_entry(s, &d->streams, list) {
1601 if (s->direction != AMDTP_OUT_STREAM)
1602 continue;
9b1fcd9b 1603
39c2649c
TS
1604 if (compare_ohci_cycle_count(s->next_cycle, cycle) > 0)
1605 cycle = s->next_cycle;
9b1fcd9b 1606
39c2649c
TS
1607 if (s == d->irq_target)
1608 s->context->callback.sc = irq_target_callback_intermediately;
1609 else
1610 s->context->callback.sc = process_rx_packets_intermediately;
1611 }
9b1fcd9b 1612
39c2649c
TS
1613 d->processing_cycle.rx_start = cycle;
1614 }
9b1fcd9b
TS
1615}
1616
b7c7699b
TS
1617// This is executed one time. For in-stream, first packet has come. For out-stream, prepared to
1618// transmit first packet.
7b3b0d85 1619static void amdtp_stream_first_callback(struct fw_iso_context *context,
73fc7f08 1620 u32 tstamp, size_t header_length,
7b3b0d85
TS
1621 void *header, void *private_data)
1622{
1623 struct amdtp_stream *s = private_data;
da3623ab 1624 struct amdtp_domain *d = s->domain;
7b3b0d85 1625
a04513f8 1626 if (s->direction == AMDTP_IN_STREAM) {
fb25dcc8 1627 context->callback.sc = drop_tx_packets_initially;
a04513f8 1628 } else {
da3623ab 1629 if (s == d->irq_target)
9b1fcd9b 1630 context->callback.sc = irq_target_callback_skip;
2472cfb3 1631 else
9b1fcd9b 1632 context->callback.sc = skip_rx_packets;
a04513f8
TS
1633 }
1634
73fc7f08 1635 context->callback.sc(context, tstamp, header_length, header, s);
7b3b0d85
TS
1636}
1637
31ef9134 1638/**
be4a2894
TS
1639 * amdtp_stream_start - start transferring packets
1640 * @s: the AMDTP stream to start
31ef9134
CL
1641 * @channel: the isochronous channel on the bus
1642 * @speed: firewire speed code
af86b0b1
TS
1643 * @queue_size: The number of packets in the queue.
1644 * @idle_irq_interval: the interval to queue packet during initial state.
31ef9134
CL
1645 *
1646 * The stream cannot be started until it has been configured with
be4a2894
TS
1647 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
1648 * device can be started.
31ef9134 1649 */
a0e02331 1650static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed,
bd165079 1651 unsigned int queue_size, unsigned int idle_irq_interval)
31ef9134 1652{
2472cfb3 1653 bool is_irq_target = (s == s->domain->irq_target);
d3d10a4a 1654 unsigned int ctx_header_size;
f11453c7 1655 unsigned int max_ctx_payload_size;
2b3fc456 1656 enum dma_data_direction dir;
cec371ff
TS
1657 struct pkt_desc *descs;
1658 int i, type, tag, err;
31ef9134
CL
1659
1660 mutex_lock(&s->mutex);
1661
be4a2894 1662 if (WARN_ON(amdtp_stream_running(s) ||
4b7da117 1663 (s->data_block_quadlets < 1))) {
31ef9134
CL
1664 err = -EBADFD;
1665 goto err_unlock;
1666 }
1667
d3d10a4a 1668 if (s->direction == AMDTP_IN_STREAM) {
60dd4929
TS
1669 // NOTE: IT context should be used for constant IRQ.
1670 if (is_irq_target) {
1671 err = -EINVAL;
1672 goto err_unlock;
1673 }
1674
b6bc8123 1675 s->data_block_counter = UINT_MAX;
d3d10a4a 1676 } else {
b6bc8123 1677 s->data_block_counter = 0;
d3d10a4a 1678 }
31ef9134 1679
1be4f21d 1680 // initialize packet buffer.
2b3fc456
TS
1681 if (s->direction == AMDTP_IN_STREAM) {
1682 dir = DMA_FROM_DEVICE;
1683 type = FW_ISO_CONTEXT_RECEIVE;
c75f3678 1684 if (!(s->flags & CIP_NO_HEADER))
f11453c7 1685 ctx_header_size = IR_CTX_HEADER_SIZE_CIP;
c75f3678 1686 else
f11453c7 1687 ctx_header_size = IR_CTX_HEADER_SIZE_NO_CIP;
2b3fc456
TS
1688 } else {
1689 dir = DMA_TO_DEVICE;
1690 type = FW_ISO_CONTEXT_TRANSMIT;
df9160b9 1691 ctx_header_size = 0; // No effect for IT context.
b18f0cfa 1692 }
c75f3678 1693 max_ctx_payload_size = amdtp_stream_get_max_ctx_payload_size(s);
f11453c7 1694
c75f3678 1695 err = iso_packets_buffer_init(&s->buffer, s->unit, queue_size, max_ctx_payload_size, dir);
31ef9134
CL
1696 if (err < 0)
1697 goto err_unlock;
af86b0b1 1698 s->queue_size = queue_size;
60dd4929 1699
31ef9134 1700 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
d3d10a4a 1701 type, channel, speed, ctx_header_size,
2472cfb3 1702 amdtp_stream_first_callback, s);
31ef9134
CL
1703 if (IS_ERR(s->context)) {
1704 err = PTR_ERR(s->context);
1705 if (err == -EBUSY)
1706 dev_err(&s->unit->device,
be4a2894 1707 "no free stream on this controller\n");
31ef9134
CL
1708 goto err_buffer;
1709 }
1710
be4a2894 1711 amdtp_stream_update(s);
31ef9134 1712
d3d10a4a 1713 if (s->direction == AMDTP_IN_STREAM) {
f11453c7 1714 s->ctx_data.tx.max_ctx_payload_length = max_ctx_payload_size;
d3d10a4a 1715 s->ctx_data.tx.ctx_header_size = ctx_header_size;
fb25dcc8 1716 s->ctx_data.tx.event_starts = false;
f9e5ecdf
TS
1717
1718 if (s->domain->replay.enable) {
1719 // struct fw_iso_context.drop_overflow_headers is false therefore it's
1720 // possible to cache much unexpectedly.
1721 s->ctx_data.tx.cache.size = max_t(unsigned int, s->syt_interval * 2,
1722 queue_size * 3 / 2);
cccddec4 1723 s->ctx_data.tx.cache.pos = 0;
f9e5ecdf
TS
1724 s->ctx_data.tx.cache.descs = kcalloc(s->ctx_data.tx.cache.size,
1725 sizeof(*s->ctx_data.tx.cache.descs), GFP_KERNEL);
8b6e2193
DC
1726 if (!s->ctx_data.tx.cache.descs) {
1727 err = -ENOMEM;
f9e5ecdf 1728 goto err_context;
8b6e2193 1729 }
f9e5ecdf 1730 }
bd165079 1731 } else {
6f24bb8a
TS
1732 static const struct {
1733 unsigned int data_block;
1734 unsigned int syt_offset;
1735 } *entry, initial_state[] = {
1736 [CIP_SFC_32000] = { 4, 3072 },
1737 [CIP_SFC_48000] = { 6, 1024 },
1738 [CIP_SFC_96000] = { 12, 1024 },
1739 [CIP_SFC_192000] = { 24, 1024 },
1740 [CIP_SFC_44100] = { 0, 67 },
1741 [CIP_SFC_88200] = { 0, 67 },
1742 [CIP_SFC_176400] = { 0, 67 },
1743 };
1744
1745 s->ctx_data.rx.seq.descs = kcalloc(queue_size, sizeof(*s->ctx_data.rx.seq.descs), GFP_KERNEL);
8b6e2193
DC
1746 if (!s->ctx_data.rx.seq.descs) {
1747 err = -ENOMEM;
6f24bb8a 1748 goto err_context;
8b6e2193 1749 }
6f24bb8a 1750 s->ctx_data.rx.seq.size = queue_size;
119c446a 1751 s->ctx_data.rx.seq.pos = 0;
6f24bb8a
TS
1752
1753 entry = &initial_state[s->sfc];
1754 s->ctx_data.rx.data_block_state = entry->data_block;
1755 s->ctx_data.rx.syt_offset_state = entry->syt_offset;
1756 s->ctx_data.rx.last_syt_offset = TICKS_PER_CYCLE;
1757
bd165079 1758 s->ctx_data.rx.event_count = 0;
d3d10a4a 1759 }
52759c09 1760
3b196c39
TS
1761 if (s->flags & CIP_NO_HEADER)
1762 s->tag = TAG_NO_CIP_HEADER;
1763 else
1764 s->tag = TAG_CIP;
1765
af13842c
TS
1766 // NOTE: When operating without hardIRQ/softIRQ, applications tends to call ioctl request
1767 // for runtime of PCM substream in the interval equivalent to the size of PCM buffer. It
1768 // could take a round over queue of AMDTP packet descriptors and small loss of history. For
1769 // safe, keep more 8 elements for the queue, equivalent to 1 ms.
1770 descs = kcalloc(s->queue_size + 8, sizeof(*descs), GFP_KERNEL);
cec371ff 1771 if (!descs) {
04130cf8
TS
1772 err = -ENOMEM;
1773 goto err_context;
1774 }
f0117128 1775 s->packet_descs = descs;
cec371ff
TS
1776
1777 INIT_LIST_HEAD(&s->packet_descs_list);
1778 for (i = 0; i < s->queue_size; ++i) {
1779 INIT_LIST_HEAD(&descs->link);
1780 list_add_tail(&descs->link, &s->packet_descs_list);
1781 ++descs;
1782 }
f0117128 1783 s->packet_descs_cursor = list_first_entry(&s->packet_descs_list, struct pkt_desc, link);
04130cf8 1784
ec00f5e4 1785 s->packet_index = 0;
4b7da117 1786 do {
6007bf54 1787 struct fw_iso_packet params;
e229853d 1788
b18f0cfa 1789 if (s->direction == AMDTP_IN_STREAM) {
60dd4929 1790 err = queue_in_packet(s, &params);
b18f0cfa 1791 } else {
60dd4929
TS
1792 bool sched_irq = false;
1793
b18f0cfa
TS
1794 params.header_length = 0;
1795 params.payload_length = 0;
60dd4929
TS
1796
1797 if (is_irq_target) {
1798 sched_irq = !((s->packet_index + 1) %
1799 idle_irq_interval);
1800 }
1801
e229853d 1802 err = queue_out_packet(s, &params, sched_irq);
b18f0cfa 1803 }
4b7da117 1804 if (err < 0)
04130cf8 1805 goto err_pkt_descs;
4b7da117 1806 } while (s->packet_index > 0);
31ef9134 1807
2b3fc456 1808 /* NOTE: TAG1 matches CIP. This just affects in stream. */
7ab56645 1809 tag = FW_ISO_CONTEXT_MATCH_TAG1;
3b196c39 1810 if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER))
7ab56645
TS
1811 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
1812
bdaedca7 1813 s->ready_processing = false;
bd165079 1814 err = fw_iso_context_start(s->context, -1, 0, tag);
31ef9134 1815 if (err < 0)
04130cf8 1816 goto err_pkt_descs;
31ef9134
CL
1817
1818 mutex_unlock(&s->mutex);
1819
1820 return 0;
04130cf8 1821err_pkt_descs:
f0117128
TS
1822 kfree(s->packet_descs);
1823 s->packet_descs = NULL;
31ef9134 1824err_context:
f9e5ecdf 1825 if (s->direction == AMDTP_OUT_STREAM) {
6f24bb8a 1826 kfree(s->ctx_data.rx.seq.descs);
f9e5ecdf
TS
1827 } else {
1828 if (s->domain->replay.enable)
1829 kfree(s->ctx_data.tx.cache.descs);
1830 }
31ef9134
CL
1831 fw_iso_context_destroy(s->context);
1832 s->context = ERR_PTR(-1);
1833err_buffer:
1834 iso_packets_buffer_destroy(&s->buffer, s->unit);
1835err_unlock:
1836 mutex_unlock(&s->mutex);
1837
1838 return err;
1839}
31ef9134 1840
e9148ddd 1841/**
f890f9a0
TS
1842 * amdtp_domain_stream_pcm_pointer - get the PCM buffer position
1843 * @d: the AMDTP domain.
be4a2894 1844 * @s: the AMDTP stream that transports the PCM data
e9148ddd
CL
1845 *
1846 * Returns the current buffer position, in frames.
1847 */
f890f9a0
TS
1848unsigned long amdtp_domain_stream_pcm_pointer(struct amdtp_domain *d,
1849 struct amdtp_stream *s)
e9148ddd 1850{
f890f9a0
TS
1851 struct amdtp_stream *irq_target = d->irq_target;
1852
7ba5ca32 1853 // Process isochronous packets queued till recent isochronous cycle to handle PCM frames.
f890f9a0 1854 if (irq_target && amdtp_stream_running(irq_target)) {
7ba5ca32
TS
1855 // In software IRQ context, the call causes dead-lock to disable the tasklet
1856 // synchronously.
3b86ec63 1857 if (!in_softirq())
f890f9a0 1858 fw_iso_context_flush_completions(irq_target->context);
f890f9a0 1859 }
e9148ddd 1860
6aa7de05 1861 return READ_ONCE(s->pcm_buffer_pointer);
e9148ddd 1862}
f890f9a0 1863EXPORT_SYMBOL_GPL(amdtp_domain_stream_pcm_pointer);
e9148ddd 1864
875becf8 1865/**
e6dcc92f
TS
1866 * amdtp_domain_stream_pcm_ack - acknowledge queued PCM frames
1867 * @d: the AMDTP domain.
875becf8
TS
1868 * @s: the AMDTP stream that transfers the PCM frames
1869 *
1870 * Returns zero always.
1871 */
e6dcc92f 1872int amdtp_domain_stream_pcm_ack(struct amdtp_domain *d, struct amdtp_stream *s)
875becf8 1873{
e6dcc92f
TS
1874 struct amdtp_stream *irq_target = d->irq_target;
1875
1876 // Process isochronous packets for recent isochronous cycle to handle
1877 // queued PCM frames.
987b705b 1878 if (irq_target && amdtp_stream_running(irq_target))
e6dcc92f 1879 fw_iso_context_flush_completions(irq_target->context);
875becf8
TS
1880
1881 return 0;
1882}
e6dcc92f 1883EXPORT_SYMBOL_GPL(amdtp_domain_stream_pcm_ack);
875becf8 1884
31ef9134 1885/**
be4a2894
TS
1886 * amdtp_stream_update - update the stream after a bus reset
1887 * @s: the AMDTP stream
31ef9134 1888 */
be4a2894 1889void amdtp_stream_update(struct amdtp_stream *s)
31ef9134 1890{
9a2820c1 1891 /* Precomputing. */
6aa7de05
MR
1892 WRITE_ONCE(s->source_node_id_field,
1893 (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & CIP_SID_MASK);
31ef9134 1894}
be4a2894 1895EXPORT_SYMBOL(amdtp_stream_update);
31ef9134
CL
1896
1897/**
be4a2894
TS
1898 * amdtp_stream_stop - stop sending packets
1899 * @s: the AMDTP stream to stop
31ef9134
CL
1900 *
1901 * All PCM and MIDI devices of the stream must be stopped before the stream
1902 * itself can be stopped.
1903 */
74f94e41 1904static void amdtp_stream_stop(struct amdtp_stream *s)
31ef9134
CL
1905{
1906 mutex_lock(&s->mutex);
1907
be4a2894 1908 if (!amdtp_stream_running(s)) {
31ef9134
CL
1909 mutex_unlock(&s->mutex);
1910 return;
1911 }
1912
1913 fw_iso_context_stop(s->context);
1914 fw_iso_context_destroy(s->context);
1915 s->context = ERR_PTR(-1);
1916 iso_packets_buffer_destroy(&s->buffer, s->unit);
f0117128
TS
1917 kfree(s->packet_descs);
1918 s->packet_descs = NULL;
31ef9134 1919
f9e5ecdf 1920 if (s->direction == AMDTP_OUT_STREAM) {
6f24bb8a 1921 kfree(s->ctx_data.rx.seq.descs);
f9e5ecdf
TS
1922 } else {
1923 if (s->domain->replay.enable)
1924 kfree(s->ctx_data.tx.cache.descs);
1925 }
7b3b0d85 1926
31ef9134
CL
1927 mutex_unlock(&s->mutex);
1928}
31ef9134
CL
1929
1930/**
be4a2894 1931 * amdtp_stream_pcm_abort - abort the running PCM device
31ef9134
CL
1932 * @s: the AMDTP stream about to be stopped
1933 *
1934 * If the isochronous stream needs to be stopped asynchronously, call this
1935 * function first to stop the PCM device.
1936 */
be4a2894 1937void amdtp_stream_pcm_abort(struct amdtp_stream *s)
31ef9134
CL
1938{
1939 struct snd_pcm_substream *pcm;
1940
6aa7de05 1941 pcm = READ_ONCE(s->pcm);
1fb8510c
TI
1942 if (pcm)
1943 snd_pcm_stop_xrun(pcm);
31ef9134 1944}
be4a2894 1945EXPORT_SYMBOL(amdtp_stream_pcm_abort);
3ec3d7a3
TS
1946
1947/**
1948 * amdtp_domain_init - initialize an AMDTP domain structure
1949 * @d: the AMDTP domain to initialize.
1950 */
1951int amdtp_domain_init(struct amdtp_domain *d)
1952{
1953 INIT_LIST_HEAD(&d->streams);
1954
d68c3123
TS
1955 d->events_per_period = 0;
1956
3ec3d7a3
TS
1957 return 0;
1958}
1959EXPORT_SYMBOL_GPL(amdtp_domain_init);
1960
1961/**
1962 * amdtp_domain_destroy - destroy an AMDTP domain structure
1963 * @d: the AMDTP domain to destroy.
1964 */
1965void amdtp_domain_destroy(struct amdtp_domain *d)
1966{
8d0d5c3f
TS
1967 // At present nothing to do.
1968 return;
3ec3d7a3
TS
1969}
1970EXPORT_SYMBOL_GPL(amdtp_domain_destroy);
6261f90b 1971
157a53ee
TS
1972/**
1973 * amdtp_domain_add_stream - register isoc context into the domain.
1974 * @d: the AMDTP domain.
1975 * @s: the AMDTP stream.
1976 * @channel: the isochronous channel on the bus.
1977 * @speed: firewire speed code.
1978 */
1979int amdtp_domain_add_stream(struct amdtp_domain *d, struct amdtp_stream *s,
1980 int channel, int speed)
1981{
1982 struct amdtp_stream *tmp;
1983
1984 list_for_each_entry(tmp, &d->streams, list) {
1985 if (s == tmp)
1986 return -EBUSY;
1987 }
1988
1989 list_add(&s->list, &d->streams);
1990
1991 s->channel = channel;
1992 s->speed = speed;
2472cfb3 1993 s->domain = d;
157a53ee
TS
1994
1995 return 0;
1996}
1997EXPORT_SYMBOL_GPL(amdtp_domain_add_stream);
1998
39c2649c
TS
1999// Make the reference from rx stream to tx stream for sequence replay. When the number of tx streams
2000// is less than the number of rx streams, the first tx stream is selected.
2001static int make_association(struct amdtp_domain *d)
2002{
2003 unsigned int dst_index = 0;
2004 struct amdtp_stream *rx;
2005
2006 // Make association to replay target.
2007 list_for_each_entry(rx, &d->streams, list) {
2008 if (rx->direction == AMDTP_OUT_STREAM) {
2009 unsigned int src_index = 0;
2010 struct amdtp_stream *tx = NULL;
2011 struct amdtp_stream *s;
2012
2013 list_for_each_entry(s, &d->streams, list) {
2014 if (s->direction == AMDTP_IN_STREAM) {
2015 if (dst_index == src_index) {
2016 tx = s;
2017 break;
2018 }
2019
2020 ++src_index;
2021 }
2022 }
2023 if (!tx) {
2024 // Select the first entry.
2025 list_for_each_entry(s, &d->streams, list) {
2026 if (s->direction == AMDTP_IN_STREAM) {
2027 tx = s;
2028 break;
2029 }
2030 }
2031 // No target is available to replay sequence.
2032 if (!tx)
2033 return -EINVAL;
2034 }
2035
2036 rx->ctx_data.rx.replay_target = tx;
39c2649c
TS
2037
2038 ++dst_index;
2039 }
2040 }
2041
2042 return 0;
2043}
2044
9b4702b0
TS
2045/**
2046 * amdtp_domain_start - start sending packets for isoc context in the domain.
2047 * @d: the AMDTP domain.
26541cb1
TS
2048 * @tx_init_skip_cycles: the number of cycles to skip processing packets at initial stage of IR
2049 * contexts.
f9e5ecdf
TS
2050 * @replay_seq: whether to replay the sequence of packet in IR context for the sequence of packet in
2051 * IT context.
2f21a177
TS
2052 * @replay_on_the_fly: transfer rx packets according to nominal frequency, then begin to replay
2053 * according to arrival of events in tx packets.
9b4702b0 2054 */
2f21a177
TS
2055int amdtp_domain_start(struct amdtp_domain *d, unsigned int tx_init_skip_cycles, bool replay_seq,
2056 bool replay_on_the_fly)
9b4702b0 2057{
af86b0b1
TS
2058 unsigned int events_per_buffer = d->events_per_buffer;
2059 unsigned int events_per_period = d->events_per_period;
af86b0b1 2060 unsigned int queue_size;
9b4702b0 2061 struct amdtp_stream *s;
0cbbeaf3 2062 bool found = false;
acfedcbe 2063 int err;
9b4702b0 2064
39c2649c
TS
2065 if (replay_seq) {
2066 err = make_association(d);
2067 if (err < 0)
2068 return err;
2069 }
f9e5ecdf 2070 d->replay.enable = replay_seq;
2f21a177 2071 d->replay.on_the_fly = replay_on_the_fly;
f9e5ecdf 2072
60dd4929 2073 // Select an IT context as IRQ target.
9b4702b0 2074 list_for_each_entry(s, &d->streams, list) {
0cbbeaf3
CJ
2075 if (s->direction == AMDTP_OUT_STREAM) {
2076 found = true;
9b4702b0 2077 break;
0cbbeaf3 2078 }
9b4702b0 2079 }
0cbbeaf3 2080 if (!found)
60dd4929
TS
2081 return -ENXIO;
2082 d->irq_target = s;
9b4702b0 2083
26541cb1
TS
2084 d->processing_cycle.tx_init_skip = tx_init_skip_cycles;
2085
af86b0b1
TS
2086 // This is a case that AMDTP streams in domain run just for MIDI
2087 // substream. Use the number of events equivalent to 10 msec as
2088 // interval of hardware IRQ.
2089 if (events_per_period == 0)
2090 events_per_period = amdtp_rate_table[d->irq_target->sfc] / 100;
2091 if (events_per_buffer == 0)
2092 events_per_buffer = events_per_period * 3;
2093
2094 queue_size = DIV_ROUND_UP(CYCLES_PER_SECOND * events_per_buffer,
2095 amdtp_rate_table[d->irq_target->sfc]);
2096
60dd4929 2097 list_for_each_entry(s, &d->streams, list) {
bd165079 2098 unsigned int idle_irq_interval = 0;
acfedcbe 2099
bd165079
TS
2100 if (s->direction == AMDTP_OUT_STREAM && s == d->irq_target) {
2101 idle_irq_interval = DIV_ROUND_UP(CYCLES_PER_SECOND * events_per_period,
2102 amdtp_rate_table[d->irq_target->sfc]);
60dd4929 2103 }
9b4702b0 2104
bd165079
TS
2105 // Starts immediately but actually DMA context starts several hundred cycles later.
2106 err = amdtp_stream_start(s, s->channel, s->speed, queue_size, idle_irq_interval);
2107 if (err < 0)
2108 goto error;
2109 }
60dd4929
TS
2110
2111 return 0;
2112error:
2113 list_for_each_entry(s, &d->streams, list)
2114 amdtp_stream_stop(s);
9b4702b0
TS
2115 return err;
2116}
2117EXPORT_SYMBOL_GPL(amdtp_domain_start);
2118
6261f90b
TS
2119/**
2120 * amdtp_domain_stop - stop sending packets for isoc context in the same domain.
2121 * @d: the AMDTP domain to which the isoc contexts belong.
2122 */
2123void amdtp_domain_stop(struct amdtp_domain *d)
2124{
2125 struct amdtp_stream *s, *next;
2126
60dd4929
TS
2127 if (d->irq_target)
2128 amdtp_stream_stop(d->irq_target);
2129
6261f90b
TS
2130 list_for_each_entry_safe(s, next, &d->streams, list) {
2131 list_del(&s->list);
2132
60dd4929
TS
2133 if (s != d->irq_target)
2134 amdtp_stream_stop(s);
6261f90b 2135 }
d68c3123
TS
2136
2137 d->events_per_period = 0;
60dd4929 2138 d->irq_target = NULL;
6261f90b
TS
2139}
2140EXPORT_SYMBOL_GPL(amdtp_domain_stop);