xref: /openbmc/linux/sound/firewire/amdtp-stream.c (revision 2f5947df)
1 // SPDX-License-Identifier: GPL-2.0-only
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>
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>
15 #include <sound/pcm_params.h>
16 #include "amdtp-stream.h"
17 
18 #define TICKS_PER_CYCLE		3072
19 #define CYCLES_PER_SECOND	8000
20 #define TICKS_PER_SECOND	(TICKS_PER_CYCLE * CYCLES_PER_SECOND)
21 
22 /* Always support Linux tracing subsystem. */
23 #define CREATE_TRACE_POINTS
24 #include "amdtp-stream-trace.h"
25 
26 #define TRANSFER_DELAY_TICKS	0x2e00 /* 479.17 microseconds */
27 
28 /* isochronous header parameters */
29 #define ISO_DATA_LENGTH_SHIFT	16
30 #define TAG_NO_CIP_HEADER	0
31 #define TAG_CIP			1
32 
33 /* common isochronous packet header parameters */
34 #define CIP_EOH_SHIFT		31
35 #define CIP_EOH			(1u << CIP_EOH_SHIFT)
36 #define CIP_EOH_MASK		0x80000000
37 #define CIP_SID_SHIFT		24
38 #define CIP_SID_MASK		0x3f000000
39 #define CIP_DBS_MASK		0x00ff0000
40 #define CIP_DBS_SHIFT		16
41 #define CIP_SPH_MASK		0x00000400
42 #define CIP_SPH_SHIFT		10
43 #define CIP_DBC_MASK		0x000000ff
44 #define CIP_FMT_SHIFT		24
45 #define CIP_FMT_MASK		0x3f000000
46 #define CIP_FDF_MASK		0x00ff0000
47 #define CIP_FDF_SHIFT		16
48 #define CIP_SYT_MASK		0x0000ffff
49 #define CIP_SYT_NO_INFO		0xffff
50 
51 /* Audio and Music transfer protocol specific parameters */
52 #define CIP_FMT_AM		0x10
53 #define AMDTP_FDF_NO_DATA	0xff
54 
55 /* TODO: make these configurable */
56 #define INTERRUPT_INTERVAL	16
57 #define QUEUE_LENGTH		48
58 
59 // For iso header, tstamp and 2 CIP header.
60 #define IR_CTX_HEADER_SIZE_CIP		16
61 // For iso header and tstamp.
62 #define IR_CTX_HEADER_SIZE_NO_CIP	8
63 #define HEADER_TSTAMP_MASK	0x0000ffff
64 
65 #define IT_PKT_HEADER_SIZE_CIP		8 // For 2 CIP header.
66 #define IT_PKT_HEADER_SIZE_NO_CIP	0 // Nothing.
67 
68 static void pcm_period_tasklet(unsigned long data);
69 
70 /**
71  * amdtp_stream_init - initialize an AMDTP stream structure
72  * @s: the AMDTP stream to initialize
73  * @unit: the target of the stream
74  * @dir: the direction of stream
75  * @flags: the packet transmission method to use
76  * @fmt: the value of fmt field in CIP header
77  * @process_data_blocks: callback handler to process data blocks
78  * @protocol_size: the size to allocate newly for protocol
79  */
80 int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
81 		      enum amdtp_stream_direction dir, enum cip_flags flags,
82 		      unsigned int fmt,
83 		      amdtp_stream_process_data_blocks_t process_data_blocks,
84 		      unsigned int protocol_size)
85 {
86 	if (process_data_blocks == NULL)
87 		return -EINVAL;
88 
89 	s->protocol = kzalloc(protocol_size, GFP_KERNEL);
90 	if (!s->protocol)
91 		return -ENOMEM;
92 
93 	s->unit = unit;
94 	s->direction = dir;
95 	s->flags = flags;
96 	s->context = ERR_PTR(-1);
97 	mutex_init(&s->mutex);
98 	tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
99 	s->packet_index = 0;
100 
101 	init_waitqueue_head(&s->callback_wait);
102 	s->callbacked = false;
103 
104 	s->fmt = fmt;
105 	s->process_data_blocks = process_data_blocks;
106 
107 	return 0;
108 }
109 EXPORT_SYMBOL(amdtp_stream_init);
110 
111 /**
112  * amdtp_stream_destroy - free stream resources
113  * @s: the AMDTP stream to destroy
114  */
115 void amdtp_stream_destroy(struct amdtp_stream *s)
116 {
117 	/* Not initialized. */
118 	if (s->protocol == NULL)
119 		return;
120 
121 	WARN_ON(amdtp_stream_running(s));
122 	kfree(s->protocol);
123 	mutex_destroy(&s->mutex);
124 }
125 EXPORT_SYMBOL(amdtp_stream_destroy);
126 
127 const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
128 	[CIP_SFC_32000]  =  8,
129 	[CIP_SFC_44100]  =  8,
130 	[CIP_SFC_48000]  =  8,
131 	[CIP_SFC_88200]  = 16,
132 	[CIP_SFC_96000]  = 16,
133 	[CIP_SFC_176400] = 32,
134 	[CIP_SFC_192000] = 32,
135 };
136 EXPORT_SYMBOL(amdtp_syt_intervals);
137 
138 const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
139 	[CIP_SFC_32000]  =  32000,
140 	[CIP_SFC_44100]  =  44100,
141 	[CIP_SFC_48000]  =  48000,
142 	[CIP_SFC_88200]  =  88200,
143 	[CIP_SFC_96000]  =  96000,
144 	[CIP_SFC_176400] = 176400,
145 	[CIP_SFC_192000] = 192000,
146 };
147 EXPORT_SYMBOL(amdtp_rate_table);
148 
149 static int apply_constraint_to_size(struct snd_pcm_hw_params *params,
150 				    struct snd_pcm_hw_rule *rule)
151 {
152 	struct snd_interval *s = hw_param_interval(params, rule->var);
153 	const struct snd_interval *r =
154 		hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
155 	struct snd_interval t = {0};
156 	unsigned int step = 0;
157 	int i;
158 
159 	for (i = 0; i < CIP_SFC_COUNT; ++i) {
160 		if (snd_interval_test(r, amdtp_rate_table[i]))
161 			step = max(step, amdtp_syt_intervals[i]);
162 	}
163 
164 	t.min = roundup(s->min, step);
165 	t.max = rounddown(s->max, step);
166 	t.integer = 1;
167 
168 	return snd_interval_refine(s, &t);
169 }
170 
171 /**
172  * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
173  * @s:		the AMDTP stream, which must be initialized.
174  * @runtime:	the PCM substream runtime
175  */
176 int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
177 					struct snd_pcm_runtime *runtime)
178 {
179 	struct snd_pcm_hardware *hw = &runtime->hw;
180 	int err;
181 
182 	hw->info = SNDRV_PCM_INFO_BATCH |
183 		   SNDRV_PCM_INFO_BLOCK_TRANSFER |
184 		   SNDRV_PCM_INFO_INTERLEAVED |
185 		   SNDRV_PCM_INFO_JOINT_DUPLEX |
186 		   SNDRV_PCM_INFO_MMAP |
187 		   SNDRV_PCM_INFO_MMAP_VALID;
188 
189 	/* SNDRV_PCM_INFO_BATCH */
190 	hw->periods_min = 2;
191 	hw->periods_max = UINT_MAX;
192 
193 	/* bytes for a frame */
194 	hw->period_bytes_min = 4 * hw->channels_max;
195 
196 	/* Just to prevent from allocating much pages. */
197 	hw->period_bytes_max = hw->period_bytes_min * 2048;
198 	hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
199 
200 	/*
201 	 * Currently firewire-lib processes 16 packets in one software
202 	 * interrupt callback. This equals to 2msec but actually the
203 	 * interval of the interrupts has a jitter.
204 	 * Additionally, even if adding a constraint to fit period size to
205 	 * 2msec, actual calculated frames per period doesn't equal to 2msec,
206 	 * depending on sampling rate.
207 	 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
208 	 * Here let us use 5msec for safe period interrupt.
209 	 */
210 	err = snd_pcm_hw_constraint_minmax(runtime,
211 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
212 					   5000, UINT_MAX);
213 	if (err < 0)
214 		goto end;
215 
216 	/* Non-Blocking stream has no more constraints */
217 	if (!(s->flags & CIP_BLOCKING))
218 		goto end;
219 
220 	/*
221 	 * One AMDTP packet can include some frames. In blocking mode, the
222 	 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
223 	 * depending on its sampling rate. For accurate period interrupt, it's
224 	 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
225 	 */
226 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
227 				  apply_constraint_to_size, NULL,
228 				  SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
229 				  SNDRV_PCM_HW_PARAM_RATE, -1);
230 	if (err < 0)
231 		goto end;
232 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
233 				  apply_constraint_to_size, NULL,
234 				  SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
235 				  SNDRV_PCM_HW_PARAM_RATE, -1);
236 	if (err < 0)
237 		goto end;
238 end:
239 	return err;
240 }
241 EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
242 
243 /**
244  * amdtp_stream_set_parameters - set stream parameters
245  * @s: the AMDTP stream to configure
246  * @rate: the sample rate
247  * @data_block_quadlets: the size of a data block in quadlet unit
248  *
249  * The parameters must be set before the stream is started, and must not be
250  * changed while the stream is running.
251  */
252 int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
253 				unsigned int data_block_quadlets)
254 {
255 	unsigned int sfc;
256 
257 	for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
258 		if (amdtp_rate_table[sfc] == rate)
259 			break;
260 	}
261 	if (sfc == ARRAY_SIZE(amdtp_rate_table))
262 		return -EINVAL;
263 
264 	s->sfc = sfc;
265 	s->data_block_quadlets = data_block_quadlets;
266 	s->syt_interval = amdtp_syt_intervals[sfc];
267 
268 	// default buffering in the device.
269 	if (s->direction == AMDTP_OUT_STREAM) {
270 		s->ctx_data.rx.transfer_delay =
271 					TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
272 
273 		if (s->flags & CIP_BLOCKING) {
274 			// additional buffering needed to adjust for no-data
275 			// packets.
276 			s->ctx_data.rx.transfer_delay +=
277 				TICKS_PER_SECOND * s->syt_interval / rate;
278 		}
279 	}
280 
281 	return 0;
282 }
283 EXPORT_SYMBOL(amdtp_stream_set_parameters);
284 
285 /**
286  * amdtp_stream_get_max_payload - get the stream's packet size
287  * @s: the AMDTP stream
288  *
289  * This function must not be called before the stream has been configured
290  * with amdtp_stream_set_parameters().
291  */
292 unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
293 {
294 	unsigned int multiplier = 1;
295 	unsigned int cip_header_size = 0;
296 
297 	if (s->flags & CIP_JUMBO_PAYLOAD)
298 		multiplier = 5;
299 	if (!(s->flags & CIP_NO_HEADER))
300 		cip_header_size = sizeof(__be32) * 2;
301 
302 	return cip_header_size +
303 		s->syt_interval * s->data_block_quadlets * sizeof(__be32) * multiplier;
304 }
305 EXPORT_SYMBOL(amdtp_stream_get_max_payload);
306 
307 /**
308  * amdtp_stream_pcm_prepare - prepare PCM device for running
309  * @s: the AMDTP stream
310  *
311  * This function should be called from the PCM device's .prepare callback.
312  */
313 void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
314 {
315 	tasklet_kill(&s->period_tasklet);
316 	s->pcm_buffer_pointer = 0;
317 	s->pcm_period_pointer = 0;
318 }
319 EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
320 
321 static unsigned int calculate_data_blocks(struct amdtp_stream *s,
322 					  unsigned int syt)
323 {
324 	unsigned int phase, data_blocks;
325 
326 	/* Blocking mode. */
327 	if (s->flags & CIP_BLOCKING) {
328 		/* This module generate empty packet for 'no data'. */
329 		if (syt == CIP_SYT_NO_INFO)
330 			data_blocks = 0;
331 		else
332 			data_blocks = s->syt_interval;
333 	/* Non-blocking mode. */
334 	} else {
335 		if (!cip_sfc_is_base_44100(s->sfc)) {
336 			// Sample_rate / 8000 is an integer, and precomputed.
337 			data_blocks = s->ctx_data.rx.data_block_state;
338 		} else {
339 			phase = s->ctx_data.rx.data_block_state;
340 
341 		/*
342 		 * This calculates the number of data blocks per packet so that
343 		 * 1) the overall rate is correct and exactly synchronized to
344 		 *    the bus clock, and
345 		 * 2) packets with a rounded-up number of blocks occur as early
346 		 *    as possible in the sequence (to prevent underruns of the
347 		 *    device's buffer).
348 		 */
349 			if (s->sfc == CIP_SFC_44100)
350 				/* 6 6 5 6 5 6 5 ... */
351 				data_blocks = 5 + ((phase & 1) ^
352 						   (phase == 0 || phase >= 40));
353 			else
354 				/* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
355 				data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
356 			if (++phase >= (80 >> (s->sfc >> 1)))
357 				phase = 0;
358 			s->ctx_data.rx.data_block_state = phase;
359 		}
360 	}
361 
362 	return data_blocks;
363 }
364 
365 static unsigned int calculate_syt(struct amdtp_stream *s,
366 				  unsigned int cycle)
367 {
368 	unsigned int syt_offset, phase, index, syt;
369 
370 	if (s->ctx_data.rx.last_syt_offset < TICKS_PER_CYCLE) {
371 		if (!cip_sfc_is_base_44100(s->sfc))
372 			syt_offset = s->ctx_data.rx.last_syt_offset +
373 				     s->ctx_data.rx.syt_offset_state;
374 		else {
375 		/*
376 		 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
377 		 *   n * SYT_INTERVAL * 24576000 / sample_rate
378 		 * Modulo TICKS_PER_CYCLE, the difference between successive
379 		 * elements is about 1386.23.  Rounding the results of this
380 		 * formula to the SYT precision results in a sequence of
381 		 * differences that begins with:
382 		 *   1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
383 		 * This code generates _exactly_ the same sequence.
384 		 */
385 			phase = s->ctx_data.rx.syt_offset_state;
386 			index = phase % 13;
387 			syt_offset = s->ctx_data.rx.last_syt_offset;
388 			syt_offset += 1386 + ((index && !(index & 3)) ||
389 					      phase == 146);
390 			if (++phase >= 147)
391 				phase = 0;
392 			s->ctx_data.rx.syt_offset_state = phase;
393 		}
394 	} else
395 		syt_offset = s->ctx_data.rx.last_syt_offset - TICKS_PER_CYCLE;
396 	s->ctx_data.rx.last_syt_offset = syt_offset;
397 
398 	if (syt_offset < TICKS_PER_CYCLE) {
399 		syt_offset += s->ctx_data.rx.transfer_delay;
400 		syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
401 		syt += syt_offset % TICKS_PER_CYCLE;
402 
403 		return syt & CIP_SYT_MASK;
404 	} else {
405 		return CIP_SYT_NO_INFO;
406 	}
407 }
408 
409 static void update_pcm_pointers(struct amdtp_stream *s,
410 				struct snd_pcm_substream *pcm,
411 				unsigned int frames)
412 {
413 	unsigned int ptr;
414 
415 	ptr = s->pcm_buffer_pointer + frames;
416 	if (ptr >= pcm->runtime->buffer_size)
417 		ptr -= pcm->runtime->buffer_size;
418 	WRITE_ONCE(s->pcm_buffer_pointer, ptr);
419 
420 	s->pcm_period_pointer += frames;
421 	if (s->pcm_period_pointer >= pcm->runtime->period_size) {
422 		s->pcm_period_pointer -= pcm->runtime->period_size;
423 		tasklet_hi_schedule(&s->period_tasklet);
424 	}
425 }
426 
427 static void pcm_period_tasklet(unsigned long data)
428 {
429 	struct amdtp_stream *s = (void *)data;
430 	struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
431 
432 	if (pcm)
433 		snd_pcm_period_elapsed(pcm);
434 }
435 
436 static int queue_packet(struct amdtp_stream *s, struct fw_iso_packet *params)
437 {
438 	int err;
439 
440 	params->interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
441 	params->tag = s->tag;
442 	params->sy = 0;
443 
444 	err = fw_iso_context_queue(s->context, params, &s->buffer.iso_buffer,
445 				   s->buffer.packets[s->packet_index].offset);
446 	if (err < 0) {
447 		dev_err(&s->unit->device, "queueing error: %d\n", err);
448 		goto end;
449 	}
450 
451 	if (++s->packet_index >= QUEUE_LENGTH)
452 		s->packet_index = 0;
453 end:
454 	return err;
455 }
456 
457 static inline int queue_out_packet(struct amdtp_stream *s,
458 				   struct fw_iso_packet *params)
459 {
460 	params->skip =
461 		!!(params->header_length == 0 && params->payload_length == 0);
462 	return queue_packet(s, params);
463 }
464 
465 static inline int queue_in_packet(struct amdtp_stream *s,
466 				  struct fw_iso_packet *params)
467 {
468 	// Queue one packet for IR context.
469 	params->header_length = s->ctx_data.tx.ctx_header_size;
470 	params->payload_length = s->ctx_data.tx.max_ctx_payload_length;
471 	params->skip = false;
472 	return queue_packet(s, params);
473 }
474 
475 static void generate_cip_header(struct amdtp_stream *s, __be32 cip_header[2],
476 				unsigned int syt)
477 {
478 	cip_header[0] = cpu_to_be32(READ_ONCE(s->source_node_id_field) |
479 				(s->data_block_quadlets << CIP_DBS_SHIFT) |
480 				((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) |
481 				s->data_block_counter);
482 	cip_header[1] = cpu_to_be32(CIP_EOH |
483 			((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
484 			((s->ctx_data.rx.fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
485 			(syt & CIP_SYT_MASK));
486 }
487 
488 static void build_it_pkt_header(struct amdtp_stream *s, unsigned int cycle,
489 				struct fw_iso_packet *params,
490 				unsigned int data_blocks, unsigned int syt,
491 				unsigned int index)
492 {
493 	unsigned int payload_length;
494 	__be32 *cip_header;
495 
496 	payload_length = data_blocks * sizeof(__be32) * s->data_block_quadlets;
497 	params->payload_length = payload_length;
498 
499 	if (s->flags & CIP_DBC_IS_END_EVENT) {
500 		s->data_block_counter =
501 				(s->data_block_counter + data_blocks) & 0xff;
502 	}
503 
504 	if (!(s->flags & CIP_NO_HEADER)) {
505 		cip_header = (__be32 *)params->header;
506 		generate_cip_header(s, cip_header, syt);
507 		params->header_length = 2 * sizeof(__be32);
508 		payload_length += params->header_length;
509 	} else {
510 		cip_header = NULL;
511 	}
512 
513 	trace_amdtp_packet(s, cycle, cip_header, payload_length, data_blocks,
514 			   index);
515 
516 	if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
517 		s->data_block_counter =
518 				(s->data_block_counter + data_blocks) & 0xff;
519 	}
520 }
521 
522 static int check_cip_header(struct amdtp_stream *s, const __be32 *buf,
523 			    unsigned int payload_length,
524 			    unsigned int *data_blocks, unsigned int *dbc,
525 			    unsigned int *syt)
526 {
527 	u32 cip_header[2];
528 	unsigned int sph;
529 	unsigned int fmt;
530 	unsigned int fdf;
531 	bool lost;
532 
533 	cip_header[0] = be32_to_cpu(buf[0]);
534 	cip_header[1] = be32_to_cpu(buf[1]);
535 
536 	/*
537 	 * This module supports 'Two-quadlet CIP header with SYT field'.
538 	 * For convenience, also check FMT field is AM824 or not.
539 	 */
540 	if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
541 	     ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) &&
542 	    (!(s->flags & CIP_HEADER_WITHOUT_EOH))) {
543 		dev_info_ratelimited(&s->unit->device,
544 				"Invalid CIP header for AMDTP: %08X:%08X\n",
545 				cip_header[0], cip_header[1]);
546 		return -EAGAIN;
547 	}
548 
549 	/* Check valid protocol or not. */
550 	sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT;
551 	fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
552 	if (sph != s->sph || fmt != s->fmt) {
553 		dev_info_ratelimited(&s->unit->device,
554 				     "Detect unexpected protocol: %08x %08x\n",
555 				     cip_header[0], cip_header[1]);
556 		return -EAGAIN;
557 	}
558 
559 	/* Calculate data blocks */
560 	fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT;
561 	if (payload_length < sizeof(__be32) * 2 ||
562 	    (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
563 		*data_blocks = 0;
564 	} else {
565 		unsigned int data_block_quadlets =
566 				(cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
567 		/* avoid division by zero */
568 		if (data_block_quadlets == 0) {
569 			dev_err(&s->unit->device,
570 				"Detect invalid value in dbs field: %08X\n",
571 				cip_header[0]);
572 			return -EPROTO;
573 		}
574 		if (s->flags & CIP_WRONG_DBS)
575 			data_block_quadlets = s->data_block_quadlets;
576 
577 		*data_blocks = (payload_length / sizeof(__be32) - 2) /
578 							data_block_quadlets;
579 	}
580 
581 	/* Check data block counter continuity */
582 	*dbc = cip_header[0] & CIP_DBC_MASK;
583 	if (*data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
584 	    s->data_block_counter != UINT_MAX)
585 		*dbc = s->data_block_counter;
586 
587 	if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) &&
588 	     *dbc == s->ctx_data.tx.first_dbc) ||
589 	    s->data_block_counter == UINT_MAX) {
590 		lost = false;
591 	} else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
592 		lost = *dbc != s->data_block_counter;
593 	} else {
594 		unsigned int dbc_interval;
595 
596 		if (*data_blocks > 0 && s->ctx_data.tx.dbc_interval > 0)
597 			dbc_interval = s->ctx_data.tx.dbc_interval;
598 		else
599 			dbc_interval = *data_blocks;
600 
601 		lost = *dbc != ((s->data_block_counter + dbc_interval) & 0xff);
602 	}
603 
604 	if (lost) {
605 		dev_err(&s->unit->device,
606 			"Detect discontinuity of CIP: %02X %02X\n",
607 			s->data_block_counter, *dbc);
608 		return -EIO;
609 	}
610 
611 	*syt = cip_header[1] & CIP_SYT_MASK;
612 
613 	return 0;
614 }
615 
616 static int parse_ir_ctx_header(struct amdtp_stream *s, unsigned int cycle,
617 			       const __be32 *ctx_header,
618 			       unsigned int *payload_length,
619 			       unsigned int *data_blocks, unsigned int *syt,
620 			       unsigned int index)
621 {
622 	unsigned int dbc;
623 	const __be32 *cip_header;
624 	int err;
625 
626 	*payload_length = be32_to_cpu(ctx_header[0]) >> ISO_DATA_LENGTH_SHIFT;
627 	if (*payload_length > s->ctx_data.tx.ctx_header_size +
628 					s->ctx_data.tx.max_ctx_payload_length) {
629 		dev_err(&s->unit->device,
630 			"Detect jumbo payload: %04x %04x\n",
631 			*payload_length, s->ctx_data.tx.max_ctx_payload_length);
632 		return -EIO;
633 	}
634 
635 	if (!(s->flags & CIP_NO_HEADER)) {
636 		cip_header = ctx_header + 2;
637 		err = check_cip_header(s, cip_header, *payload_length,
638 				       data_blocks, &dbc, syt);
639 		if (err < 0)
640 			return err;
641 	} else {
642 		cip_header = NULL;
643 		err = 0;
644 		*data_blocks = *payload_length / sizeof(__be32) /
645 			       s->data_block_quadlets;
646 		*syt = 0;
647 
648 		if (s->data_block_counter != UINT_MAX)
649 			dbc = s->data_block_counter;
650 		else
651 			dbc = 0;
652 	}
653 
654 	s->data_block_counter = dbc;
655 
656 	trace_amdtp_packet(s, cycle, cip_header, *payload_length, *data_blocks,
657 			   index);
658 
659 	return err;
660 }
661 
662 // In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
663 // the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
664 // it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
665 static inline u32 compute_cycle_count(__be32 ctx_header_tstamp)
666 {
667 	u32 tstamp = be32_to_cpu(ctx_header_tstamp) & HEADER_TSTAMP_MASK;
668 	return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff);
669 }
670 
671 static inline u32 increment_cycle_count(u32 cycle, unsigned int addend)
672 {
673 	cycle += addend;
674 	if (cycle >= 8 * CYCLES_PER_SECOND)
675 		cycle -= 8 * CYCLES_PER_SECOND;
676 	return cycle;
677 }
678 
679 // Align to actual cycle count for the packet which is going to be scheduled.
680 // This module queued the same number of isochronous cycle as QUEUE_LENGTH to
681 // skip isochronous cycle, therefore it's OK to just increment the cycle by
682 // QUEUE_LENGTH for scheduled cycle.
683 static inline u32 compute_it_cycle(const __be32 ctx_header_tstamp)
684 {
685 	u32 cycle = compute_cycle_count(ctx_header_tstamp);
686 	return increment_cycle_count(cycle, QUEUE_LENGTH);
687 }
688 
689 static inline void cancel_stream(struct amdtp_stream *s)
690 {
691 	s->packet_index = -1;
692 	if (in_interrupt())
693 		amdtp_stream_pcm_abort(s);
694 	WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
695 }
696 
697 static void out_stream_callback(struct fw_iso_context *context, u32 tstamp,
698 				size_t header_length, void *header,
699 				void *private_data)
700 {
701 	struct amdtp_stream *s = private_data;
702 	const __be32 *ctx_header = header;
703 	unsigned int packets = header_length / sizeof(*ctx_header);
704 	int i;
705 
706 	if (s->packet_index < 0)
707 		return;
708 
709 	for (i = 0; i < packets; ++i) {
710 		u32 cycle;
711 		unsigned int syt;
712 		unsigned int data_blocks;
713 		__be32 *buffer;
714 		unsigned int pcm_frames;
715 		struct {
716 			struct fw_iso_packet params;
717 			__be32 header[IT_PKT_HEADER_SIZE_CIP / sizeof(__be32)];
718 		} template = { {0}, {0} };
719 		struct snd_pcm_substream *pcm;
720 
721 		cycle = compute_it_cycle(*ctx_header);
722 		syt = calculate_syt(s, cycle);
723 		data_blocks = calculate_data_blocks(s, syt);
724 		buffer = s->buffer.packets[s->packet_index].buffer;
725 		pcm_frames = s->process_data_blocks(s, buffer, data_blocks,
726 						    &syt);
727 
728 		build_it_pkt_header(s, cycle, &template.params, data_blocks,
729 				    syt, i);
730 
731 		if (queue_out_packet(s, &template.params) < 0) {
732 			cancel_stream(s);
733 			return;
734 		}
735 
736 		pcm = READ_ONCE(s->pcm);
737 		if (pcm && pcm_frames > 0)
738 			update_pcm_pointers(s, pcm, pcm_frames);
739 
740 		++ctx_header;
741 	}
742 
743 	fw_iso_context_queue_flush(s->context);
744 }
745 
746 static void in_stream_callback(struct fw_iso_context *context, u32 tstamp,
747 			       size_t header_length, void *header,
748 			       void *private_data)
749 {
750 	struct amdtp_stream *s = private_data;
751 	unsigned int i, packets;
752 	__be32 *ctx_header = header;
753 
754 	if (s->packet_index < 0)
755 		return;
756 
757 	// The number of packets in buffer.
758 	packets = header_length / s->ctx_data.tx.ctx_header_size;
759 
760 	for (i = 0; i < packets; i++) {
761 		u32 cycle;
762 		unsigned int payload_length;
763 		unsigned int data_blocks;
764 		unsigned int syt;
765 		__be32 *buffer;
766 		unsigned int pcm_frames = 0;
767 		struct fw_iso_packet params = {0};
768 		struct snd_pcm_substream *pcm;
769 		int err;
770 
771 		cycle = compute_cycle_count(ctx_header[1]);
772 		err = parse_ir_ctx_header(s, cycle, ctx_header, &payload_length,
773 					  &data_blocks, &syt, i);
774 		if (err < 0 && err != -EAGAIN)
775 			break;
776 
777 		if (err >= 0) {
778 			buffer = s->buffer.packets[s->packet_index].buffer;
779 			pcm_frames = s->process_data_blocks(s, buffer,
780 							    data_blocks, &syt);
781 
782 			if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
783 				s->data_block_counter += data_blocks;
784 				s->data_block_counter &= 0xff;
785 			}
786 		}
787 
788 		if (queue_in_packet(s, &params) < 0)
789 			break;
790 
791 		pcm = READ_ONCE(s->pcm);
792 		if (pcm && pcm_frames > 0)
793 			update_pcm_pointers(s, pcm, pcm_frames);
794 
795 		ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header);
796 	}
797 
798 	/* Queueing error or detecting invalid payload. */
799 	if (i < packets) {
800 		cancel_stream(s);
801 		return;
802 	}
803 
804 	fw_iso_context_queue_flush(s->context);
805 }
806 
807 /* this is executed one time */
808 static void amdtp_stream_first_callback(struct fw_iso_context *context,
809 					u32 tstamp, size_t header_length,
810 					void *header, void *private_data)
811 {
812 	struct amdtp_stream *s = private_data;
813 	const __be32 *ctx_header = header;
814 	u32 cycle;
815 
816 	/*
817 	 * For in-stream, first packet has come.
818 	 * For out-stream, prepared to transmit first packet
819 	 */
820 	s->callbacked = true;
821 	wake_up(&s->callback_wait);
822 
823 	if (s->direction == AMDTP_IN_STREAM) {
824 		cycle = compute_cycle_count(ctx_header[1]);
825 
826 		context->callback.sc = in_stream_callback;
827 	} else {
828 		cycle = compute_it_cycle(*ctx_header);
829 
830 		context->callback.sc = out_stream_callback;
831 	}
832 
833 	s->start_cycle = cycle;
834 
835 	context->callback.sc(context, tstamp, header_length, header, s);
836 }
837 
838 /**
839  * amdtp_stream_start - start transferring packets
840  * @s: the AMDTP stream to start
841  * @channel: the isochronous channel on the bus
842  * @speed: firewire speed code
843  *
844  * The stream cannot be started until it has been configured with
845  * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
846  * device can be started.
847  */
848 int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
849 {
850 	static const struct {
851 		unsigned int data_block;
852 		unsigned int syt_offset;
853 	} *entry, initial_state[] = {
854 		[CIP_SFC_32000]  = {  4, 3072 },
855 		[CIP_SFC_48000]  = {  6, 1024 },
856 		[CIP_SFC_96000]  = { 12, 1024 },
857 		[CIP_SFC_192000] = { 24, 1024 },
858 		[CIP_SFC_44100]  = {  0,   67 },
859 		[CIP_SFC_88200]  = {  0,   67 },
860 		[CIP_SFC_176400] = {  0,   67 },
861 	};
862 	unsigned int ctx_header_size;
863 	unsigned int max_ctx_payload_size;
864 	enum dma_data_direction dir;
865 	int type, tag, err;
866 
867 	mutex_lock(&s->mutex);
868 
869 	if (WARN_ON(amdtp_stream_running(s) ||
870 		    (s->data_block_quadlets < 1))) {
871 		err = -EBADFD;
872 		goto err_unlock;
873 	}
874 
875 	if (s->direction == AMDTP_IN_STREAM) {
876 		s->data_block_counter = UINT_MAX;
877 	} else {
878 		entry = &initial_state[s->sfc];
879 
880 		s->data_block_counter = 0;
881 		s->ctx_data.rx.data_block_state = entry->data_block;
882 		s->ctx_data.rx.syt_offset_state = entry->syt_offset;
883 		s->ctx_data.rx.last_syt_offset = TICKS_PER_CYCLE;
884 	}
885 
886 	/* initialize packet buffer */
887 	if (s->direction == AMDTP_IN_STREAM) {
888 		dir = DMA_FROM_DEVICE;
889 		type = FW_ISO_CONTEXT_RECEIVE;
890 		if (!(s->flags & CIP_NO_HEADER))
891 			ctx_header_size = IR_CTX_HEADER_SIZE_CIP;
892 		else
893 			ctx_header_size = IR_CTX_HEADER_SIZE_NO_CIP;
894 
895 		max_ctx_payload_size = amdtp_stream_get_max_payload(s) -
896 				       ctx_header_size;
897 	} else {
898 		dir = DMA_TO_DEVICE;
899 		type = FW_ISO_CONTEXT_TRANSMIT;
900 		ctx_header_size = 0;	// No effect for IT context.
901 
902 		max_ctx_payload_size = amdtp_stream_get_max_payload(s);
903 		if (!(s->flags & CIP_NO_HEADER))
904 			max_ctx_payload_size -= IT_PKT_HEADER_SIZE_CIP;
905 	}
906 
907 	err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
908 				      max_ctx_payload_size, dir);
909 	if (err < 0)
910 		goto err_unlock;
911 
912 	s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
913 					  type, channel, speed, ctx_header_size,
914 					  amdtp_stream_first_callback, s);
915 	if (IS_ERR(s->context)) {
916 		err = PTR_ERR(s->context);
917 		if (err == -EBUSY)
918 			dev_err(&s->unit->device,
919 				"no free stream on this controller\n");
920 		goto err_buffer;
921 	}
922 
923 	amdtp_stream_update(s);
924 
925 	if (s->direction == AMDTP_IN_STREAM) {
926 		s->ctx_data.tx.max_ctx_payload_length = max_ctx_payload_size;
927 		s->ctx_data.tx.ctx_header_size = ctx_header_size;
928 	}
929 
930 	if (s->flags & CIP_NO_HEADER)
931 		s->tag = TAG_NO_CIP_HEADER;
932 	else
933 		s->tag = TAG_CIP;
934 
935 	s->packet_index = 0;
936 	do {
937 		struct fw_iso_packet params;
938 		if (s->direction == AMDTP_IN_STREAM) {
939 			err = queue_in_packet(s, &params);
940 		} else {
941 			params.header_length = 0;
942 			params.payload_length = 0;
943 			err = queue_out_packet(s, &params);
944 		}
945 		if (err < 0)
946 			goto err_context;
947 	} while (s->packet_index > 0);
948 
949 	/* NOTE: TAG1 matches CIP. This just affects in stream. */
950 	tag = FW_ISO_CONTEXT_MATCH_TAG1;
951 	if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER))
952 		tag |= FW_ISO_CONTEXT_MATCH_TAG0;
953 
954 	s->callbacked = false;
955 	err = fw_iso_context_start(s->context, -1, 0, tag);
956 	if (err < 0)
957 		goto err_context;
958 
959 	mutex_unlock(&s->mutex);
960 
961 	return 0;
962 
963 err_context:
964 	fw_iso_context_destroy(s->context);
965 	s->context = ERR_PTR(-1);
966 err_buffer:
967 	iso_packets_buffer_destroy(&s->buffer, s->unit);
968 err_unlock:
969 	mutex_unlock(&s->mutex);
970 
971 	return err;
972 }
973 EXPORT_SYMBOL(amdtp_stream_start);
974 
975 /**
976  * amdtp_stream_pcm_pointer - get the PCM buffer position
977  * @s: the AMDTP stream that transports the PCM data
978  *
979  * Returns the current buffer position, in frames.
980  */
981 unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
982 {
983 	/*
984 	 * This function is called in software IRQ context of period_tasklet or
985 	 * process context.
986 	 *
987 	 * When the software IRQ context was scheduled by software IRQ context
988 	 * of IR/IT contexts, queued packets were already handled. Therefore,
989 	 * no need to flush the queue in buffer anymore.
990 	 *
991 	 * When the process context reach here, some packets will be already
992 	 * queued in the buffer. These packets should be handled immediately
993 	 * to keep better granularity of PCM pointer.
994 	 *
995 	 * Later, the process context will sometimes schedules software IRQ
996 	 * context of the period_tasklet. Then, no need to flush the queue by
997 	 * the same reason as described for IR/IT contexts.
998 	 */
999 	if (!in_interrupt() && amdtp_stream_running(s))
1000 		fw_iso_context_flush_completions(s->context);
1001 
1002 	return READ_ONCE(s->pcm_buffer_pointer);
1003 }
1004 EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
1005 
1006 /**
1007  * amdtp_stream_pcm_ack - acknowledge queued PCM frames
1008  * @s: the AMDTP stream that transfers the PCM frames
1009  *
1010  * Returns zero always.
1011  */
1012 int amdtp_stream_pcm_ack(struct amdtp_stream *s)
1013 {
1014 	/*
1015 	 * Process isochronous packets for recent isochronous cycle to handle
1016 	 * queued PCM frames.
1017 	 */
1018 	if (amdtp_stream_running(s))
1019 		fw_iso_context_flush_completions(s->context);
1020 
1021 	return 0;
1022 }
1023 EXPORT_SYMBOL(amdtp_stream_pcm_ack);
1024 
1025 /**
1026  * amdtp_stream_update - update the stream after a bus reset
1027  * @s: the AMDTP stream
1028  */
1029 void amdtp_stream_update(struct amdtp_stream *s)
1030 {
1031 	/* Precomputing. */
1032 	WRITE_ONCE(s->source_node_id_field,
1033                    (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & CIP_SID_MASK);
1034 }
1035 EXPORT_SYMBOL(amdtp_stream_update);
1036 
1037 /**
1038  * amdtp_stream_stop - stop sending packets
1039  * @s: the AMDTP stream to stop
1040  *
1041  * All PCM and MIDI devices of the stream must be stopped before the stream
1042  * itself can be stopped.
1043  */
1044 void amdtp_stream_stop(struct amdtp_stream *s)
1045 {
1046 	mutex_lock(&s->mutex);
1047 
1048 	if (!amdtp_stream_running(s)) {
1049 		mutex_unlock(&s->mutex);
1050 		return;
1051 	}
1052 
1053 	tasklet_kill(&s->period_tasklet);
1054 	fw_iso_context_stop(s->context);
1055 	fw_iso_context_destroy(s->context);
1056 	s->context = ERR_PTR(-1);
1057 	iso_packets_buffer_destroy(&s->buffer, s->unit);
1058 
1059 	s->callbacked = false;
1060 
1061 	mutex_unlock(&s->mutex);
1062 }
1063 EXPORT_SYMBOL(amdtp_stream_stop);
1064 
1065 /**
1066  * amdtp_stream_pcm_abort - abort the running PCM device
1067  * @s: the AMDTP stream about to be stopped
1068  *
1069  * If the isochronous stream needs to be stopped asynchronously, call this
1070  * function first to stop the PCM device.
1071  */
1072 void amdtp_stream_pcm_abort(struct amdtp_stream *s)
1073 {
1074 	struct snd_pcm_substream *pcm;
1075 
1076 	pcm = READ_ONCE(s->pcm);
1077 	if (pcm)
1078 		snd_pcm_stop_xrun(pcm);
1079 }
1080 EXPORT_SYMBOL(amdtp_stream_pcm_abort);
1081