xref: /linux/sound/firewire/amdtp-stream.c (revision d9a16fc926a950c9e481cb7e89c554593b8e29e2)
1d67c46b9STakashi Sakamoto /*
2d67c46b9STakashi Sakamoto  * Audio and Music Data Transmission Protocol (IEC 61883-6) streams
3d67c46b9STakashi Sakamoto  * with Common Isochronous Packet (IEC 61883-1) headers
4d67c46b9STakashi Sakamoto  *
5d67c46b9STakashi Sakamoto  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6d67c46b9STakashi Sakamoto  * Licensed under the terms of the GNU General Public License, version 2.
7d67c46b9STakashi Sakamoto  */
8d67c46b9STakashi Sakamoto 
9d67c46b9STakashi Sakamoto #include <linux/device.h>
10d67c46b9STakashi Sakamoto #include <linux/err.h>
11d67c46b9STakashi Sakamoto #include <linux/firewire.h>
12d67c46b9STakashi Sakamoto #include <linux/module.h>
13d67c46b9STakashi Sakamoto #include <linux/slab.h>
14d67c46b9STakashi Sakamoto #include <sound/pcm.h>
15d67c46b9STakashi Sakamoto #include <sound/pcm_params.h>
16d67c46b9STakashi Sakamoto #include "amdtp-stream.h"
17d67c46b9STakashi Sakamoto 
18d67c46b9STakashi Sakamoto #define TICKS_PER_CYCLE		3072
19d67c46b9STakashi Sakamoto #define CYCLES_PER_SECOND	8000
20d67c46b9STakashi Sakamoto #define TICKS_PER_SECOND	(TICKS_PER_CYCLE * CYCLES_PER_SECOND)
21d67c46b9STakashi Sakamoto 
220c95c1d6STakashi Sakamoto /* Always support Linux tracing subsystem. */
230c95c1d6STakashi Sakamoto #define CREATE_TRACE_POINTS
240c95c1d6STakashi Sakamoto #include "amdtp-stream-trace.h"
250c95c1d6STakashi Sakamoto 
26d67c46b9STakashi Sakamoto #define TRANSFER_DELAY_TICKS	0x2e00 /* 479.17 microseconds */
27d67c46b9STakashi Sakamoto 
28d67c46b9STakashi Sakamoto /* isochronous header parameters */
29d67c46b9STakashi Sakamoto #define ISO_DATA_LENGTH_SHIFT	16
30d67c46b9STakashi Sakamoto #define TAG_CIP			1
31d67c46b9STakashi Sakamoto 
32d67c46b9STakashi Sakamoto /* common isochronous packet header parameters */
33d67c46b9STakashi Sakamoto #define CIP_EOH_SHIFT		31
34d67c46b9STakashi Sakamoto #define CIP_EOH			(1u << CIP_EOH_SHIFT)
35d67c46b9STakashi Sakamoto #define CIP_EOH_MASK		0x80000000
36d67c46b9STakashi Sakamoto #define CIP_SID_SHIFT		24
37d67c46b9STakashi Sakamoto #define CIP_SID_MASK		0x3f000000
38d67c46b9STakashi Sakamoto #define CIP_DBS_MASK		0x00ff0000
39d67c46b9STakashi Sakamoto #define CIP_DBS_SHIFT		16
40d67c46b9STakashi Sakamoto #define CIP_DBC_MASK		0x000000ff
41d67c46b9STakashi Sakamoto #define CIP_FMT_SHIFT		24
42d67c46b9STakashi Sakamoto #define CIP_FMT_MASK		0x3f000000
43d67c46b9STakashi Sakamoto #define CIP_FDF_MASK		0x00ff0000
44d67c46b9STakashi Sakamoto #define CIP_FDF_SHIFT		16
45d67c46b9STakashi Sakamoto #define CIP_SYT_MASK		0x0000ffff
46d67c46b9STakashi Sakamoto #define CIP_SYT_NO_INFO		0xffff
47d67c46b9STakashi Sakamoto 
4851c29fd2STakashi Sakamoto /* Audio and Music transfer protocol specific parameters */
49d67c46b9STakashi Sakamoto #define CIP_FMT_AM		0x10
50d67c46b9STakashi Sakamoto #define AMDTP_FDF_NO_DATA	0xff
51d67c46b9STakashi Sakamoto 
52d67c46b9STakashi Sakamoto /* TODO: make these configurable */
53d67c46b9STakashi Sakamoto #define INTERRUPT_INTERVAL	16
54d67c46b9STakashi Sakamoto #define QUEUE_LENGTH		48
55d67c46b9STakashi Sakamoto 
56d67c46b9STakashi Sakamoto #define IN_PACKET_HEADER_SIZE	4
57d67c46b9STakashi Sakamoto #define OUT_PACKET_HEADER_SIZE	0
58d67c46b9STakashi Sakamoto 
59d67c46b9STakashi Sakamoto static void pcm_period_tasklet(unsigned long data);
60d67c46b9STakashi Sakamoto 
61d67c46b9STakashi Sakamoto /**
62d67c46b9STakashi Sakamoto  * amdtp_stream_init - initialize an AMDTP stream structure
63d67c46b9STakashi Sakamoto  * @s: the AMDTP stream to initialize
64d67c46b9STakashi Sakamoto  * @unit: the target of the stream
65d67c46b9STakashi Sakamoto  * @dir: the direction of stream
66d67c46b9STakashi Sakamoto  * @flags: the packet transmission method to use
675955815eSTakashi Sakamoto  * @fmt: the value of fmt field in CIP header
68df075feeSTakashi Sakamoto  * @process_data_blocks: callback handler to process data blocks
69df075feeSTakashi Sakamoto  * @protocol_size: the size to allocate newly for protocol
70d67c46b9STakashi Sakamoto  */
71d67c46b9STakashi Sakamoto int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
725955815eSTakashi Sakamoto 		      enum amdtp_stream_direction dir, enum cip_flags flags,
73df075feeSTakashi Sakamoto 		      unsigned int fmt,
74df075feeSTakashi Sakamoto 		      amdtp_stream_process_data_blocks_t process_data_blocks,
75df075feeSTakashi Sakamoto 		      unsigned int protocol_size)
76d67c46b9STakashi Sakamoto {
77df075feeSTakashi Sakamoto 	if (process_data_blocks == NULL)
78df075feeSTakashi Sakamoto 		return -EINVAL;
79df075feeSTakashi Sakamoto 
80df075feeSTakashi Sakamoto 	s->protocol = kzalloc(protocol_size, GFP_KERNEL);
81df075feeSTakashi Sakamoto 	if (!s->protocol)
82df075feeSTakashi Sakamoto 		return -ENOMEM;
83df075feeSTakashi Sakamoto 
84d67c46b9STakashi Sakamoto 	s->unit = unit;
85d67c46b9STakashi Sakamoto 	s->direction = dir;
86d67c46b9STakashi Sakamoto 	s->flags = flags;
87d67c46b9STakashi Sakamoto 	s->context = ERR_PTR(-1);
88d67c46b9STakashi Sakamoto 	mutex_init(&s->mutex);
89d67c46b9STakashi Sakamoto 	tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
90d67c46b9STakashi Sakamoto 	s->packet_index = 0;
91d67c46b9STakashi Sakamoto 
92d67c46b9STakashi Sakamoto 	init_waitqueue_head(&s->callback_wait);
93d67c46b9STakashi Sakamoto 	s->callbacked = false;
94d67c46b9STakashi Sakamoto 
955955815eSTakashi Sakamoto 	s->fmt = fmt;
96df075feeSTakashi Sakamoto 	s->process_data_blocks = process_data_blocks;
97d67c46b9STakashi Sakamoto 
98d67c46b9STakashi Sakamoto 	return 0;
99d67c46b9STakashi Sakamoto }
100d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_init);
101d67c46b9STakashi Sakamoto 
102d67c46b9STakashi Sakamoto /**
103d67c46b9STakashi Sakamoto  * amdtp_stream_destroy - free stream resources
104d67c46b9STakashi Sakamoto  * @s: the AMDTP stream to destroy
105d67c46b9STakashi Sakamoto  */
106d67c46b9STakashi Sakamoto void amdtp_stream_destroy(struct amdtp_stream *s)
107d67c46b9STakashi Sakamoto {
10844c376b9STakashi Sakamoto 	/* Not initialized. */
10944c376b9STakashi Sakamoto 	if (s->protocol == NULL)
11044c376b9STakashi Sakamoto 		return;
11144c376b9STakashi Sakamoto 
112d67c46b9STakashi Sakamoto 	WARN_ON(amdtp_stream_running(s));
113df075feeSTakashi Sakamoto 	kfree(s->protocol);
114d67c46b9STakashi Sakamoto 	mutex_destroy(&s->mutex);
115d67c46b9STakashi Sakamoto }
116d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_destroy);
117d67c46b9STakashi Sakamoto 
118d67c46b9STakashi Sakamoto const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
119d67c46b9STakashi Sakamoto 	[CIP_SFC_32000]  =  8,
120d67c46b9STakashi Sakamoto 	[CIP_SFC_44100]  =  8,
121d67c46b9STakashi Sakamoto 	[CIP_SFC_48000]  =  8,
122d67c46b9STakashi Sakamoto 	[CIP_SFC_88200]  = 16,
123d67c46b9STakashi Sakamoto 	[CIP_SFC_96000]  = 16,
124d67c46b9STakashi Sakamoto 	[CIP_SFC_176400] = 32,
125d67c46b9STakashi Sakamoto 	[CIP_SFC_192000] = 32,
126d67c46b9STakashi Sakamoto };
127d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_syt_intervals);
128d67c46b9STakashi Sakamoto 
129d67c46b9STakashi Sakamoto const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
130d67c46b9STakashi Sakamoto 	[CIP_SFC_32000]  =  32000,
131d67c46b9STakashi Sakamoto 	[CIP_SFC_44100]  =  44100,
132d67c46b9STakashi Sakamoto 	[CIP_SFC_48000]  =  48000,
133d67c46b9STakashi Sakamoto 	[CIP_SFC_88200]  =  88200,
134d67c46b9STakashi Sakamoto 	[CIP_SFC_96000]  =  96000,
135d67c46b9STakashi Sakamoto 	[CIP_SFC_176400] = 176400,
136d67c46b9STakashi Sakamoto 	[CIP_SFC_192000] = 192000,
137d67c46b9STakashi Sakamoto };
138d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_rate_table);
139d67c46b9STakashi Sakamoto 
140d67c46b9STakashi Sakamoto /**
141d67c46b9STakashi Sakamoto  * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
142d67c46b9STakashi Sakamoto  * @s:		the AMDTP stream, which must be initialized.
143d67c46b9STakashi Sakamoto  * @runtime:	the PCM substream runtime
144d67c46b9STakashi Sakamoto  */
145d67c46b9STakashi Sakamoto int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
146d67c46b9STakashi Sakamoto 					struct snd_pcm_runtime *runtime)
147d67c46b9STakashi Sakamoto {
148d67c46b9STakashi Sakamoto 	int err;
149d67c46b9STakashi Sakamoto 
150d67c46b9STakashi Sakamoto 	/*
151d67c46b9STakashi Sakamoto 	 * Currently firewire-lib processes 16 packets in one software
152d67c46b9STakashi Sakamoto 	 * interrupt callback. This equals to 2msec but actually the
153d67c46b9STakashi Sakamoto 	 * interval of the interrupts has a jitter.
154d67c46b9STakashi Sakamoto 	 * Additionally, even if adding a constraint to fit period size to
155d67c46b9STakashi Sakamoto 	 * 2msec, actual calculated frames per period doesn't equal to 2msec,
156d67c46b9STakashi Sakamoto 	 * depending on sampling rate.
157d67c46b9STakashi Sakamoto 	 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
158d67c46b9STakashi Sakamoto 	 * Here let us use 5msec for safe period interrupt.
159d67c46b9STakashi Sakamoto 	 */
160d67c46b9STakashi Sakamoto 	err = snd_pcm_hw_constraint_minmax(runtime,
161d67c46b9STakashi Sakamoto 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
162d67c46b9STakashi Sakamoto 					   5000, UINT_MAX);
163d67c46b9STakashi Sakamoto 	if (err < 0)
164d67c46b9STakashi Sakamoto 		goto end;
165d67c46b9STakashi Sakamoto 
166d67c46b9STakashi Sakamoto 	/* Non-Blocking stream has no more constraints */
167d67c46b9STakashi Sakamoto 	if (!(s->flags & CIP_BLOCKING))
168d67c46b9STakashi Sakamoto 		goto end;
169d67c46b9STakashi Sakamoto 
170d67c46b9STakashi Sakamoto 	/*
171d67c46b9STakashi Sakamoto 	 * One AMDTP packet can include some frames. In blocking mode, the
172d67c46b9STakashi Sakamoto 	 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
173d67c46b9STakashi Sakamoto 	 * depending on its sampling rate. For accurate period interrupt, it's
174d67c46b9STakashi Sakamoto 	 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
175d67c46b9STakashi Sakamoto 	 *
176d67c46b9STakashi Sakamoto 	 * TODO: These constraints can be improved with proper rules.
177d67c46b9STakashi Sakamoto 	 * Currently apply LCM of SYT_INTERVALs.
178d67c46b9STakashi Sakamoto 	 */
179d67c46b9STakashi Sakamoto 	err = snd_pcm_hw_constraint_step(runtime, 0,
180d67c46b9STakashi Sakamoto 					 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
181d67c46b9STakashi Sakamoto 	if (err < 0)
182d67c46b9STakashi Sakamoto 		goto end;
183d67c46b9STakashi Sakamoto 	err = snd_pcm_hw_constraint_step(runtime, 0,
184d67c46b9STakashi Sakamoto 					 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
185d67c46b9STakashi Sakamoto end:
186d67c46b9STakashi Sakamoto 	return err;
187d67c46b9STakashi Sakamoto }
188d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
189d67c46b9STakashi Sakamoto 
190d67c46b9STakashi Sakamoto /**
191d67c46b9STakashi Sakamoto  * amdtp_stream_set_parameters - set stream parameters
192d67c46b9STakashi Sakamoto  * @s: the AMDTP stream to configure
193d67c46b9STakashi Sakamoto  * @rate: the sample rate
194df075feeSTakashi Sakamoto  * @data_block_quadlets: the size of a data block in quadlet unit
195d67c46b9STakashi Sakamoto  *
196d67c46b9STakashi Sakamoto  * The parameters must be set before the stream is started, and must not be
197d67c46b9STakashi Sakamoto  * changed while the stream is running.
198d67c46b9STakashi Sakamoto  */
199df075feeSTakashi Sakamoto int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
200df075feeSTakashi Sakamoto 				unsigned int data_block_quadlets)
201d67c46b9STakashi Sakamoto {
202df075feeSTakashi Sakamoto 	unsigned int sfc;
203d67c46b9STakashi Sakamoto 
204d67c46b9STakashi Sakamoto 	for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
205d67c46b9STakashi Sakamoto 		if (amdtp_rate_table[sfc] == rate)
206d67c46b9STakashi Sakamoto 			break;
207d67c46b9STakashi Sakamoto 	}
208d67c46b9STakashi Sakamoto 	if (sfc == ARRAY_SIZE(amdtp_rate_table))
209d67c46b9STakashi Sakamoto 		return -EINVAL;
210d67c46b9STakashi Sakamoto 
211d67c46b9STakashi Sakamoto 	s->sfc = sfc;
212df075feeSTakashi Sakamoto 	s->data_block_quadlets = data_block_quadlets;
213d67c46b9STakashi Sakamoto 	s->syt_interval = amdtp_syt_intervals[sfc];
214d67c46b9STakashi Sakamoto 
215d67c46b9STakashi Sakamoto 	/* default buffering in the device */
216d67c46b9STakashi Sakamoto 	s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
217d67c46b9STakashi Sakamoto 	if (s->flags & CIP_BLOCKING)
218d67c46b9STakashi Sakamoto 		/* additional buffering needed to adjust for no-data packets */
219d67c46b9STakashi Sakamoto 		s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
220d67c46b9STakashi Sakamoto 
221d67c46b9STakashi Sakamoto 	return 0;
222d67c46b9STakashi Sakamoto }
223d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_set_parameters);
224d67c46b9STakashi Sakamoto 
225d67c46b9STakashi Sakamoto /**
226d67c46b9STakashi Sakamoto  * amdtp_stream_get_max_payload - get the stream's packet size
227d67c46b9STakashi Sakamoto  * @s: the AMDTP stream
228d67c46b9STakashi Sakamoto  *
229d67c46b9STakashi Sakamoto  * This function must not be called before the stream has been configured
230d67c46b9STakashi Sakamoto  * with amdtp_stream_set_parameters().
231d67c46b9STakashi Sakamoto  */
232d67c46b9STakashi Sakamoto unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
233d67c46b9STakashi Sakamoto {
234d67c46b9STakashi Sakamoto 	unsigned int multiplier = 1;
235d67c46b9STakashi Sakamoto 
236d67c46b9STakashi Sakamoto 	if (s->flags & CIP_JUMBO_PAYLOAD)
237d67c46b9STakashi Sakamoto 		multiplier = 5;
238d67c46b9STakashi Sakamoto 
239d67c46b9STakashi Sakamoto 	return 8 + s->syt_interval * s->data_block_quadlets * 4 * multiplier;
240d67c46b9STakashi Sakamoto }
241d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_get_max_payload);
242d67c46b9STakashi Sakamoto 
243d67c46b9STakashi Sakamoto /**
244d67c46b9STakashi Sakamoto  * amdtp_stream_pcm_prepare - prepare PCM device for running
245d67c46b9STakashi Sakamoto  * @s: the AMDTP stream
246d67c46b9STakashi Sakamoto  *
247d67c46b9STakashi Sakamoto  * This function should be called from the PCM device's .prepare callback.
248d67c46b9STakashi Sakamoto  */
249d67c46b9STakashi Sakamoto void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
250d67c46b9STakashi Sakamoto {
251d67c46b9STakashi Sakamoto 	tasklet_kill(&s->period_tasklet);
252d67c46b9STakashi Sakamoto 	s->pcm_buffer_pointer = 0;
253d67c46b9STakashi Sakamoto 	s->pcm_period_pointer = 0;
254d67c46b9STakashi Sakamoto 	s->pointer_flush = true;
255d67c46b9STakashi Sakamoto }
256d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
257d67c46b9STakashi Sakamoto 
258d67c46b9STakashi Sakamoto static unsigned int calculate_data_blocks(struct amdtp_stream *s,
259d67c46b9STakashi Sakamoto 					  unsigned int syt)
260d67c46b9STakashi Sakamoto {
261d67c46b9STakashi Sakamoto 	unsigned int phase, data_blocks;
262d67c46b9STakashi Sakamoto 
263d67c46b9STakashi Sakamoto 	/* Blocking mode. */
264d67c46b9STakashi Sakamoto 	if (s->flags & CIP_BLOCKING) {
265d67c46b9STakashi Sakamoto 		/* This module generate empty packet for 'no data'. */
266d67c46b9STakashi Sakamoto 		if (syt == CIP_SYT_NO_INFO)
267d67c46b9STakashi Sakamoto 			data_blocks = 0;
268d67c46b9STakashi Sakamoto 		else
269d67c46b9STakashi Sakamoto 			data_blocks = s->syt_interval;
270d67c46b9STakashi Sakamoto 	/* Non-blocking mode. */
271d67c46b9STakashi Sakamoto 	} else {
272d67c46b9STakashi Sakamoto 		if (!cip_sfc_is_base_44100(s->sfc)) {
273d67c46b9STakashi Sakamoto 			/* Sample_rate / 8000 is an integer, and precomputed. */
274d67c46b9STakashi Sakamoto 			data_blocks = s->data_block_state;
275d67c46b9STakashi Sakamoto 		} else {
276d67c46b9STakashi Sakamoto 			phase = s->data_block_state;
277d67c46b9STakashi Sakamoto 
278d67c46b9STakashi Sakamoto 		/*
279d67c46b9STakashi Sakamoto 		 * This calculates the number of data blocks per packet so that
280d67c46b9STakashi Sakamoto 		 * 1) the overall rate is correct and exactly synchronized to
281d67c46b9STakashi Sakamoto 		 *    the bus clock, and
282d67c46b9STakashi Sakamoto 		 * 2) packets with a rounded-up number of blocks occur as early
283d67c46b9STakashi Sakamoto 		 *    as possible in the sequence (to prevent underruns of the
284d67c46b9STakashi Sakamoto 		 *    device's buffer).
285d67c46b9STakashi Sakamoto 		 */
286d67c46b9STakashi Sakamoto 			if (s->sfc == CIP_SFC_44100)
287d67c46b9STakashi Sakamoto 				/* 6 6 5 6 5 6 5 ... */
288d67c46b9STakashi Sakamoto 				data_blocks = 5 + ((phase & 1) ^
289d67c46b9STakashi Sakamoto 						   (phase == 0 || phase >= 40));
290d67c46b9STakashi Sakamoto 			else
291d67c46b9STakashi Sakamoto 				/* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
292d67c46b9STakashi Sakamoto 				data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
293d67c46b9STakashi Sakamoto 			if (++phase >= (80 >> (s->sfc >> 1)))
294d67c46b9STakashi Sakamoto 				phase = 0;
295d67c46b9STakashi Sakamoto 			s->data_block_state = phase;
296d67c46b9STakashi Sakamoto 		}
297d67c46b9STakashi Sakamoto 	}
298d67c46b9STakashi Sakamoto 
299d67c46b9STakashi Sakamoto 	return data_blocks;
300d67c46b9STakashi Sakamoto }
301d67c46b9STakashi Sakamoto 
302d67c46b9STakashi Sakamoto static unsigned int calculate_syt(struct amdtp_stream *s,
303d67c46b9STakashi Sakamoto 				  unsigned int cycle)
304d67c46b9STakashi Sakamoto {
305d67c46b9STakashi Sakamoto 	unsigned int syt_offset, phase, index, syt;
306d67c46b9STakashi Sakamoto 
307d67c46b9STakashi Sakamoto 	if (s->last_syt_offset < TICKS_PER_CYCLE) {
308d67c46b9STakashi Sakamoto 		if (!cip_sfc_is_base_44100(s->sfc))
309d67c46b9STakashi Sakamoto 			syt_offset = s->last_syt_offset + s->syt_offset_state;
310d67c46b9STakashi Sakamoto 		else {
311d67c46b9STakashi Sakamoto 		/*
312d67c46b9STakashi Sakamoto 		 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
313d67c46b9STakashi Sakamoto 		 *   n * SYT_INTERVAL * 24576000 / sample_rate
314d67c46b9STakashi Sakamoto 		 * Modulo TICKS_PER_CYCLE, the difference between successive
315d67c46b9STakashi Sakamoto 		 * elements is about 1386.23.  Rounding the results of this
316d67c46b9STakashi Sakamoto 		 * formula to the SYT precision results in a sequence of
317d67c46b9STakashi Sakamoto 		 * differences that begins with:
318d67c46b9STakashi Sakamoto 		 *   1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
319d67c46b9STakashi Sakamoto 		 * This code generates _exactly_ the same sequence.
320d67c46b9STakashi Sakamoto 		 */
321d67c46b9STakashi Sakamoto 			phase = s->syt_offset_state;
322d67c46b9STakashi Sakamoto 			index = phase % 13;
323d67c46b9STakashi Sakamoto 			syt_offset = s->last_syt_offset;
324d67c46b9STakashi Sakamoto 			syt_offset += 1386 + ((index && !(index & 3)) ||
325d67c46b9STakashi Sakamoto 					      phase == 146);
326d67c46b9STakashi Sakamoto 			if (++phase >= 147)
327d67c46b9STakashi Sakamoto 				phase = 0;
328d67c46b9STakashi Sakamoto 			s->syt_offset_state = phase;
329d67c46b9STakashi Sakamoto 		}
330d67c46b9STakashi Sakamoto 	} else
331d67c46b9STakashi Sakamoto 		syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
332d67c46b9STakashi Sakamoto 	s->last_syt_offset = syt_offset;
333d67c46b9STakashi Sakamoto 
334d67c46b9STakashi Sakamoto 	if (syt_offset < TICKS_PER_CYCLE) {
335d67c46b9STakashi Sakamoto 		syt_offset += s->transfer_delay;
336d67c46b9STakashi Sakamoto 		syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
337d67c46b9STakashi Sakamoto 		syt += syt_offset % TICKS_PER_CYCLE;
338d67c46b9STakashi Sakamoto 
339d67c46b9STakashi Sakamoto 		return syt & CIP_SYT_MASK;
340d67c46b9STakashi Sakamoto 	} else {
341d67c46b9STakashi Sakamoto 		return CIP_SYT_NO_INFO;
342d67c46b9STakashi Sakamoto 	}
343d67c46b9STakashi Sakamoto }
344d67c46b9STakashi Sakamoto 
345d67c46b9STakashi Sakamoto static void update_pcm_pointers(struct amdtp_stream *s,
346d67c46b9STakashi Sakamoto 				struct snd_pcm_substream *pcm,
347d67c46b9STakashi Sakamoto 				unsigned int frames)
348d67c46b9STakashi Sakamoto {
349d67c46b9STakashi Sakamoto 	unsigned int ptr;
350d67c46b9STakashi Sakamoto 
351d67c46b9STakashi Sakamoto 	ptr = s->pcm_buffer_pointer + frames;
352d67c46b9STakashi Sakamoto 	if (ptr >= pcm->runtime->buffer_size)
353d67c46b9STakashi Sakamoto 		ptr -= pcm->runtime->buffer_size;
354d67c46b9STakashi Sakamoto 	ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
355d67c46b9STakashi Sakamoto 
356d67c46b9STakashi Sakamoto 	s->pcm_period_pointer += frames;
357d67c46b9STakashi Sakamoto 	if (s->pcm_period_pointer >= pcm->runtime->period_size) {
358d67c46b9STakashi Sakamoto 		s->pcm_period_pointer -= pcm->runtime->period_size;
359d67c46b9STakashi Sakamoto 		s->pointer_flush = false;
360d67c46b9STakashi Sakamoto 		tasklet_hi_schedule(&s->period_tasklet);
361d67c46b9STakashi Sakamoto 	}
362d67c46b9STakashi Sakamoto }
363d67c46b9STakashi Sakamoto 
364d67c46b9STakashi Sakamoto static void pcm_period_tasklet(unsigned long data)
365d67c46b9STakashi Sakamoto {
366d67c46b9STakashi Sakamoto 	struct amdtp_stream *s = (void *)data;
367d67c46b9STakashi Sakamoto 	struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
368d67c46b9STakashi Sakamoto 
369d67c46b9STakashi Sakamoto 	if (pcm)
370d67c46b9STakashi Sakamoto 		snd_pcm_period_elapsed(pcm);
371d67c46b9STakashi Sakamoto }
372d67c46b9STakashi Sakamoto 
373d67c46b9STakashi Sakamoto static int queue_packet(struct amdtp_stream *s,
374d67c46b9STakashi Sakamoto 			unsigned int header_length,
375d67c46b9STakashi Sakamoto 			unsigned int payload_length, bool skip)
376d67c46b9STakashi Sakamoto {
377d67c46b9STakashi Sakamoto 	struct fw_iso_packet p = {0};
378d67c46b9STakashi Sakamoto 	int err = 0;
379d67c46b9STakashi Sakamoto 
380d67c46b9STakashi Sakamoto 	if (IS_ERR(s->context))
381d67c46b9STakashi Sakamoto 		goto end;
382d67c46b9STakashi Sakamoto 
383d67c46b9STakashi Sakamoto 	p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
384d67c46b9STakashi Sakamoto 	p.tag = TAG_CIP;
385d67c46b9STakashi Sakamoto 	p.header_length = header_length;
386d67c46b9STakashi Sakamoto 	p.payload_length = (!skip) ? payload_length : 0;
387d67c46b9STakashi Sakamoto 	p.skip = skip;
388d67c46b9STakashi Sakamoto 	err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
389d67c46b9STakashi Sakamoto 				   s->buffer.packets[s->packet_index].offset);
390d67c46b9STakashi Sakamoto 	if (err < 0) {
391d67c46b9STakashi Sakamoto 		dev_err(&s->unit->device, "queueing error: %d\n", err);
392d67c46b9STakashi Sakamoto 		goto end;
393d67c46b9STakashi Sakamoto 	}
394d67c46b9STakashi Sakamoto 
395d67c46b9STakashi Sakamoto 	if (++s->packet_index >= QUEUE_LENGTH)
396d67c46b9STakashi Sakamoto 		s->packet_index = 0;
397d67c46b9STakashi Sakamoto end:
398d67c46b9STakashi Sakamoto 	return err;
399d67c46b9STakashi Sakamoto }
400d67c46b9STakashi Sakamoto 
401d67c46b9STakashi Sakamoto static inline int queue_out_packet(struct amdtp_stream *s,
402d67c46b9STakashi Sakamoto 				   unsigned int payload_length, bool skip)
403d67c46b9STakashi Sakamoto {
404d67c46b9STakashi Sakamoto 	return queue_packet(s, OUT_PACKET_HEADER_SIZE,
405d67c46b9STakashi Sakamoto 			    payload_length, skip);
406d67c46b9STakashi Sakamoto }
407d67c46b9STakashi Sakamoto 
408d67c46b9STakashi Sakamoto static inline int queue_in_packet(struct amdtp_stream *s)
409d67c46b9STakashi Sakamoto {
410d67c46b9STakashi Sakamoto 	return queue_packet(s, IN_PACKET_HEADER_SIZE,
411d67c46b9STakashi Sakamoto 			    amdtp_stream_get_max_payload(s), false);
412d67c46b9STakashi Sakamoto }
413d67c46b9STakashi Sakamoto 
414d67c46b9STakashi Sakamoto static int handle_out_packet(struct amdtp_stream *s, unsigned int data_blocks,
4150c95c1d6STakashi Sakamoto 			     unsigned int cycle, unsigned int syt)
416d67c46b9STakashi Sakamoto {
417d67c46b9STakashi Sakamoto 	__be32 *buffer;
418d67c46b9STakashi Sakamoto 	unsigned int payload_length;
419d67c46b9STakashi Sakamoto 	unsigned int pcm_frames;
420d67c46b9STakashi Sakamoto 	struct snd_pcm_substream *pcm;
421d67c46b9STakashi Sakamoto 
422d67c46b9STakashi Sakamoto 	buffer = s->buffer.packets[s->packet_index].buffer;
423df075feeSTakashi Sakamoto 	pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
424d67c46b9STakashi Sakamoto 
425d67c46b9STakashi Sakamoto 	buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
426d67c46b9STakashi Sakamoto 				(s->data_block_quadlets << CIP_DBS_SHIFT) |
427d67c46b9STakashi Sakamoto 				s->data_block_counter);
428d67c46b9STakashi Sakamoto 	buffer[1] = cpu_to_be32(CIP_EOH |
429d67c46b9STakashi Sakamoto 				((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
430d67c46b9STakashi Sakamoto 				((s->fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
431d67c46b9STakashi Sakamoto 				(syt & CIP_SYT_MASK));
432d67c46b9STakashi Sakamoto 
433d67c46b9STakashi Sakamoto 	s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
434d67c46b9STakashi Sakamoto 	payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
4350c95c1d6STakashi Sakamoto 
4360c95c1d6STakashi Sakamoto 	trace_out_packet(s, cycle, buffer, payload_length);
4370c95c1d6STakashi Sakamoto 
438d67c46b9STakashi Sakamoto 	if (queue_out_packet(s, payload_length, false) < 0)
439d67c46b9STakashi Sakamoto 		return -EIO;
440d67c46b9STakashi Sakamoto 
441d67c46b9STakashi Sakamoto 	pcm = ACCESS_ONCE(s->pcm);
442d67c46b9STakashi Sakamoto 	if (pcm && pcm_frames > 0)
443d67c46b9STakashi Sakamoto 		update_pcm_pointers(s, pcm, pcm_frames);
444d67c46b9STakashi Sakamoto 
445d67c46b9STakashi Sakamoto 	/* No need to return the number of handled data blocks. */
446d67c46b9STakashi Sakamoto 	return 0;
447d67c46b9STakashi Sakamoto }
448d67c46b9STakashi Sakamoto 
449d67c46b9STakashi Sakamoto static int handle_in_packet(struct amdtp_stream *s,
450*d9a16fc9STakashi Sakamoto 			    unsigned int payload_quadlets, unsigned int cycle)
451d67c46b9STakashi Sakamoto {
452*d9a16fc9STakashi Sakamoto 	__be32 *buffer;
453d67c46b9STakashi Sakamoto 	u32 cip_header[2];
454*d9a16fc9STakashi Sakamoto 	unsigned int fmt, fdf, syt;
455d67c46b9STakashi Sakamoto 	unsigned int data_block_quadlets, data_block_counter, dbc_interval;
456*d9a16fc9STakashi Sakamoto 	unsigned int data_blocks;
457d67c46b9STakashi Sakamoto 	struct snd_pcm_substream *pcm;
458d67c46b9STakashi Sakamoto 	unsigned int pcm_frames;
459d67c46b9STakashi Sakamoto 	bool lost;
460d67c46b9STakashi Sakamoto 
461*d9a16fc9STakashi Sakamoto 	buffer = s->buffer.packets[s->packet_index].buffer;
462d67c46b9STakashi Sakamoto 	cip_header[0] = be32_to_cpu(buffer[0]);
463d67c46b9STakashi Sakamoto 	cip_header[1] = be32_to_cpu(buffer[1]);
464d67c46b9STakashi Sakamoto 
4650c95c1d6STakashi Sakamoto 	trace_in_packet(s, cycle, cip_header, payload_quadlets);
4660c95c1d6STakashi Sakamoto 
467d67c46b9STakashi Sakamoto 	/*
468d67c46b9STakashi Sakamoto 	 * This module supports 'Two-quadlet CIP header with SYT field'.
469d67c46b9STakashi Sakamoto 	 * For convenience, also check FMT field is AM824 or not.
470d67c46b9STakashi Sakamoto 	 */
471d67c46b9STakashi Sakamoto 	if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
472d67c46b9STakashi Sakamoto 	    ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) {
473d67c46b9STakashi Sakamoto 		dev_info_ratelimited(&s->unit->device,
474d67c46b9STakashi Sakamoto 				"Invalid CIP header for AMDTP: %08X:%08X\n",
475d67c46b9STakashi Sakamoto 				cip_header[0], cip_header[1]);
476*d9a16fc9STakashi Sakamoto 		data_blocks = 0;
477d67c46b9STakashi Sakamoto 		pcm_frames = 0;
478d67c46b9STakashi Sakamoto 		goto end;
479d67c46b9STakashi Sakamoto 	}
480d67c46b9STakashi Sakamoto 
481d67c46b9STakashi Sakamoto 	/* Check valid protocol or not. */
482d67c46b9STakashi Sakamoto 	fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
483d67c46b9STakashi Sakamoto 	if (fmt != s->fmt) {
4842a7e1713STakashi Sakamoto 		dev_info_ratelimited(&s->unit->device,
485d67c46b9STakashi Sakamoto 				     "Detect unexpected protocol: %08x %08x\n",
486d67c46b9STakashi Sakamoto 				     cip_header[0], cip_header[1]);
487*d9a16fc9STakashi Sakamoto 		data_blocks = 0;
4882a7e1713STakashi Sakamoto 		pcm_frames = 0;
4892a7e1713STakashi Sakamoto 		goto end;
490d67c46b9STakashi Sakamoto 	}
491d67c46b9STakashi Sakamoto 
492d67c46b9STakashi Sakamoto 	/* Calculate data blocks */
493d67c46b9STakashi Sakamoto 	fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT;
494d67c46b9STakashi Sakamoto 	if (payload_quadlets < 3 ||
495d67c46b9STakashi Sakamoto 	    (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
496*d9a16fc9STakashi Sakamoto 		data_blocks = 0;
497d67c46b9STakashi Sakamoto 	} else {
498d67c46b9STakashi Sakamoto 		data_block_quadlets =
499d67c46b9STakashi Sakamoto 			(cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
500d67c46b9STakashi Sakamoto 		/* avoid division by zero */
501d67c46b9STakashi Sakamoto 		if (data_block_quadlets == 0) {
502d67c46b9STakashi Sakamoto 			dev_err(&s->unit->device,
503d67c46b9STakashi Sakamoto 				"Detect invalid value in dbs field: %08X\n",
504d67c46b9STakashi Sakamoto 				cip_header[0]);
505d67c46b9STakashi Sakamoto 			return -EPROTO;
506d67c46b9STakashi Sakamoto 		}
507d67c46b9STakashi Sakamoto 		if (s->flags & CIP_WRONG_DBS)
508d67c46b9STakashi Sakamoto 			data_block_quadlets = s->data_block_quadlets;
509d67c46b9STakashi Sakamoto 
510*d9a16fc9STakashi Sakamoto 		data_blocks = (payload_quadlets - 2) / data_block_quadlets;
511d67c46b9STakashi Sakamoto 	}
512d67c46b9STakashi Sakamoto 
513d67c46b9STakashi Sakamoto 	/* Check data block counter continuity */
514d67c46b9STakashi Sakamoto 	data_block_counter = cip_header[0] & CIP_DBC_MASK;
515*d9a16fc9STakashi Sakamoto 	if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
516d67c46b9STakashi Sakamoto 	    s->data_block_counter != UINT_MAX)
517d67c46b9STakashi Sakamoto 		data_block_counter = s->data_block_counter;
518d67c46b9STakashi Sakamoto 
519d67c46b9STakashi Sakamoto 	if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) &&
520d67c46b9STakashi Sakamoto 	     data_block_counter == s->tx_first_dbc) ||
521d67c46b9STakashi Sakamoto 	    s->data_block_counter == UINT_MAX) {
522d67c46b9STakashi Sakamoto 		lost = false;
523d67c46b9STakashi Sakamoto 	} else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
524d67c46b9STakashi Sakamoto 		lost = data_block_counter != s->data_block_counter;
525d67c46b9STakashi Sakamoto 	} else {
526*d9a16fc9STakashi Sakamoto 		if (data_blocks > 0 && s->tx_dbc_interval > 0)
527d67c46b9STakashi Sakamoto 			dbc_interval = s->tx_dbc_interval;
528d67c46b9STakashi Sakamoto 		else
529*d9a16fc9STakashi Sakamoto 			dbc_interval = data_blocks;
530d67c46b9STakashi Sakamoto 
531d67c46b9STakashi Sakamoto 		lost = data_block_counter !=
532d67c46b9STakashi Sakamoto 		       ((s->data_block_counter + dbc_interval) & 0xff);
533d67c46b9STakashi Sakamoto 	}
534d67c46b9STakashi Sakamoto 
535d67c46b9STakashi Sakamoto 	if (lost) {
536d67c46b9STakashi Sakamoto 		dev_err(&s->unit->device,
537d67c46b9STakashi Sakamoto 			"Detect discontinuity of CIP: %02X %02X\n",
538d67c46b9STakashi Sakamoto 			s->data_block_counter, data_block_counter);
539d67c46b9STakashi Sakamoto 		return -EIO;
540d67c46b9STakashi Sakamoto 	}
541d67c46b9STakashi Sakamoto 
542*d9a16fc9STakashi Sakamoto 	syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
543*d9a16fc9STakashi Sakamoto 	pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
544d67c46b9STakashi Sakamoto 
545d67c46b9STakashi Sakamoto 	if (s->flags & CIP_DBC_IS_END_EVENT)
546d67c46b9STakashi Sakamoto 		s->data_block_counter = data_block_counter;
547d67c46b9STakashi Sakamoto 	else
548d67c46b9STakashi Sakamoto 		s->data_block_counter =
549*d9a16fc9STakashi Sakamoto 				(data_block_counter + data_blocks) & 0xff;
550d67c46b9STakashi Sakamoto end:
551d67c46b9STakashi Sakamoto 	if (queue_in_packet(s) < 0)
552d67c46b9STakashi Sakamoto 		return -EIO;
553d67c46b9STakashi Sakamoto 
554d67c46b9STakashi Sakamoto 	pcm = ACCESS_ONCE(s->pcm);
555d67c46b9STakashi Sakamoto 	if (pcm && pcm_frames > 0)
556d67c46b9STakashi Sakamoto 		update_pcm_pointers(s, pcm, pcm_frames);
557d67c46b9STakashi Sakamoto 
558d67c46b9STakashi Sakamoto 	return 0;
559d67c46b9STakashi Sakamoto }
560d67c46b9STakashi Sakamoto 
56173fc7f08STakashi Sakamoto /*
56273fc7f08STakashi Sakamoto  * In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
56373fc7f08STakashi Sakamoto  * the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
56473fc7f08STakashi Sakamoto  * it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
56573fc7f08STakashi Sakamoto  */
56673fc7f08STakashi Sakamoto static inline u32 compute_cycle_count(u32 tstamp)
56773fc7f08STakashi Sakamoto {
56873fc7f08STakashi Sakamoto 	return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff);
56973fc7f08STakashi Sakamoto }
57073fc7f08STakashi Sakamoto 
57173fc7f08STakashi Sakamoto static inline u32 increment_cycle_count(u32 cycle, unsigned int addend)
57273fc7f08STakashi Sakamoto {
57373fc7f08STakashi Sakamoto 	cycle += addend;
57473fc7f08STakashi Sakamoto 	if (cycle >= 8 * CYCLES_PER_SECOND)
57573fc7f08STakashi Sakamoto 		cycle -= 8 * CYCLES_PER_SECOND;
57673fc7f08STakashi Sakamoto 	return cycle;
57773fc7f08STakashi Sakamoto }
57873fc7f08STakashi Sakamoto 
579f90e2dedSTakashi Sakamoto static inline u32 decrement_cycle_count(u32 cycle, unsigned int subtrahend)
580f90e2dedSTakashi Sakamoto {
581f90e2dedSTakashi Sakamoto 	if (cycle < subtrahend)
582f90e2dedSTakashi Sakamoto 		cycle += 8 * CYCLES_PER_SECOND;
583f90e2dedSTakashi Sakamoto 	return cycle - subtrahend;
584f90e2dedSTakashi Sakamoto }
585f90e2dedSTakashi Sakamoto 
58673fc7f08STakashi Sakamoto static void out_stream_callback(struct fw_iso_context *context, u32 tstamp,
587d67c46b9STakashi Sakamoto 				size_t header_length, void *header,
588d67c46b9STakashi Sakamoto 				void *private_data)
589d67c46b9STakashi Sakamoto {
590d67c46b9STakashi Sakamoto 	struct amdtp_stream *s = private_data;
591d67c46b9STakashi Sakamoto 	unsigned int i, syt, packets = header_length / 4;
592d67c46b9STakashi Sakamoto 	unsigned int data_blocks;
59373fc7f08STakashi Sakamoto 	u32 cycle;
594d67c46b9STakashi Sakamoto 
595d67c46b9STakashi Sakamoto 	if (s->packet_index < 0)
596d67c46b9STakashi Sakamoto 		return;
597d67c46b9STakashi Sakamoto 
59873fc7f08STakashi Sakamoto 	cycle = compute_cycle_count(tstamp);
59973fc7f08STakashi Sakamoto 
60073fc7f08STakashi Sakamoto 	/* Align to actual cycle count for the last packet. */
60173fc7f08STakashi Sakamoto 	cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
602d67c46b9STakashi Sakamoto 
603d67c46b9STakashi Sakamoto 	for (i = 0; i < packets; ++i) {
60473fc7f08STakashi Sakamoto 		cycle = increment_cycle_count(cycle, 1);
60573fc7f08STakashi Sakamoto 		syt = calculate_syt(s, cycle);
606d67c46b9STakashi Sakamoto 		data_blocks = calculate_data_blocks(s, syt);
607d67c46b9STakashi Sakamoto 
6080c95c1d6STakashi Sakamoto 		if (handle_out_packet(s, data_blocks, cycle, syt) < 0) {
609d67c46b9STakashi Sakamoto 			s->packet_index = -1;
610d67c46b9STakashi Sakamoto 			amdtp_stream_pcm_abort(s);
611d67c46b9STakashi Sakamoto 			return;
612d67c46b9STakashi Sakamoto 		}
613d67c46b9STakashi Sakamoto 	}
614d67c46b9STakashi Sakamoto 
615d67c46b9STakashi Sakamoto 	fw_iso_context_queue_flush(s->context);
616d67c46b9STakashi Sakamoto }
617d67c46b9STakashi Sakamoto 
61873fc7f08STakashi Sakamoto static void in_stream_callback(struct fw_iso_context *context, u32 tstamp,
619d67c46b9STakashi Sakamoto 			       size_t header_length, void *header,
620d67c46b9STakashi Sakamoto 			       void *private_data)
621d67c46b9STakashi Sakamoto {
622d67c46b9STakashi Sakamoto 	struct amdtp_stream *s = private_data;
623*d9a16fc9STakashi Sakamoto 	unsigned int i, packets;
624d67c46b9STakashi Sakamoto 	unsigned int payload_quadlets, max_payload_quadlets;
625*d9a16fc9STakashi Sakamoto 	__be32 *headers = header;
626f90e2dedSTakashi Sakamoto 	u32 cycle;
627d67c46b9STakashi Sakamoto 
628d67c46b9STakashi Sakamoto 	if (s->packet_index < 0)
629d67c46b9STakashi Sakamoto 		return;
630d67c46b9STakashi Sakamoto 
631d67c46b9STakashi Sakamoto 	/* The number of packets in buffer */
632d67c46b9STakashi Sakamoto 	packets = header_length / IN_PACKET_HEADER_SIZE;
633d67c46b9STakashi Sakamoto 
634f90e2dedSTakashi Sakamoto 	cycle = compute_cycle_count(tstamp);
635f90e2dedSTakashi Sakamoto 
636f90e2dedSTakashi Sakamoto 	/* Align to actual cycle count for the last packet. */
637f90e2dedSTakashi Sakamoto 	cycle = decrement_cycle_count(cycle, packets);
638f90e2dedSTakashi Sakamoto 
639d67c46b9STakashi Sakamoto 	/* For buffer-over-run prevention. */
640d67c46b9STakashi Sakamoto 	max_payload_quadlets = amdtp_stream_get_max_payload(s) / 4;
641d67c46b9STakashi Sakamoto 
642*d9a16fc9STakashi Sakamoto 	for (i = 0; i < packets; i++) {
643f90e2dedSTakashi Sakamoto 		cycle = increment_cycle_count(cycle, 1);
644d67c46b9STakashi Sakamoto 
645d67c46b9STakashi Sakamoto 		/* The number of quadlets in this packet */
646d67c46b9STakashi Sakamoto 		payload_quadlets =
647*d9a16fc9STakashi Sakamoto 			(be32_to_cpu(headers[i]) >> ISO_DATA_LENGTH_SHIFT) / 4;
648d67c46b9STakashi Sakamoto 		if (payload_quadlets > max_payload_quadlets) {
649d67c46b9STakashi Sakamoto 			dev_err(&s->unit->device,
650d67c46b9STakashi Sakamoto 				"Detect jumbo payload: %02x %02x\n",
651d67c46b9STakashi Sakamoto 				payload_quadlets, max_payload_quadlets);
652d67c46b9STakashi Sakamoto 			break;
653d67c46b9STakashi Sakamoto 		}
654d67c46b9STakashi Sakamoto 
655*d9a16fc9STakashi Sakamoto 		if (handle_in_packet(s, payload_quadlets, cycle) < 0)
656d67c46b9STakashi Sakamoto 			break;
657d67c46b9STakashi Sakamoto 	}
658d67c46b9STakashi Sakamoto 
659dec63cc8STakashi Sakamoto 	/* Queueing error or detecting invalid payload. */
660*d9a16fc9STakashi Sakamoto 	if (i < packets) {
661d67c46b9STakashi Sakamoto 		s->packet_index = -1;
662d67c46b9STakashi Sakamoto 		amdtp_stream_pcm_abort(s);
663d67c46b9STakashi Sakamoto 		return;
664d67c46b9STakashi Sakamoto 	}
665d67c46b9STakashi Sakamoto 
666d67c46b9STakashi Sakamoto 	fw_iso_context_queue_flush(s->context);
667d67c46b9STakashi Sakamoto }
668d67c46b9STakashi Sakamoto 
669d67c46b9STakashi Sakamoto /* this is executed one time */
670d67c46b9STakashi Sakamoto static void amdtp_stream_first_callback(struct fw_iso_context *context,
67173fc7f08STakashi Sakamoto 					u32 tstamp, size_t header_length,
672d67c46b9STakashi Sakamoto 					void *header, void *private_data)
673d67c46b9STakashi Sakamoto {
674d67c46b9STakashi Sakamoto 	struct amdtp_stream *s = private_data;
675d67c46b9STakashi Sakamoto 
676d67c46b9STakashi Sakamoto 	/*
677d67c46b9STakashi Sakamoto 	 * For in-stream, first packet has come.
678d67c46b9STakashi Sakamoto 	 * For out-stream, prepared to transmit first packet
679d67c46b9STakashi Sakamoto 	 */
680d67c46b9STakashi Sakamoto 	s->callbacked = true;
681d67c46b9STakashi Sakamoto 	wake_up(&s->callback_wait);
682d67c46b9STakashi Sakamoto 
683d67c46b9STakashi Sakamoto 	if (s->direction == AMDTP_IN_STREAM)
684d67c46b9STakashi Sakamoto 		context->callback.sc = in_stream_callback;
685d67c46b9STakashi Sakamoto 	else
686d67c46b9STakashi Sakamoto 		context->callback.sc = out_stream_callback;
687d67c46b9STakashi Sakamoto 
68873fc7f08STakashi Sakamoto 	context->callback.sc(context, tstamp, header_length, header, s);
689d67c46b9STakashi Sakamoto }
690d67c46b9STakashi Sakamoto 
691d67c46b9STakashi Sakamoto /**
692d67c46b9STakashi Sakamoto  * amdtp_stream_start - start transferring packets
693d67c46b9STakashi Sakamoto  * @s: the AMDTP stream to start
694d67c46b9STakashi Sakamoto  * @channel: the isochronous channel on the bus
695d67c46b9STakashi Sakamoto  * @speed: firewire speed code
696d67c46b9STakashi Sakamoto  *
697d67c46b9STakashi Sakamoto  * The stream cannot be started until it has been configured with
698d67c46b9STakashi Sakamoto  * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
699d67c46b9STakashi Sakamoto  * device can be started.
700d67c46b9STakashi Sakamoto  */
701d67c46b9STakashi Sakamoto int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
702d67c46b9STakashi Sakamoto {
703d67c46b9STakashi Sakamoto 	static const struct {
704d67c46b9STakashi Sakamoto 		unsigned int data_block;
705d67c46b9STakashi Sakamoto 		unsigned int syt_offset;
706d67c46b9STakashi Sakamoto 	} initial_state[] = {
707d67c46b9STakashi Sakamoto 		[CIP_SFC_32000]  = {  4, 3072 },
708d67c46b9STakashi Sakamoto 		[CIP_SFC_48000]  = {  6, 1024 },
709d67c46b9STakashi Sakamoto 		[CIP_SFC_96000]  = { 12, 1024 },
710d67c46b9STakashi Sakamoto 		[CIP_SFC_192000] = { 24, 1024 },
711d67c46b9STakashi Sakamoto 		[CIP_SFC_44100]  = {  0,   67 },
712d67c46b9STakashi Sakamoto 		[CIP_SFC_88200]  = {  0,   67 },
713d67c46b9STakashi Sakamoto 		[CIP_SFC_176400] = {  0,   67 },
714d67c46b9STakashi Sakamoto 	};
715d67c46b9STakashi Sakamoto 	unsigned int header_size;
716d67c46b9STakashi Sakamoto 	enum dma_data_direction dir;
717d67c46b9STakashi Sakamoto 	int type, tag, err;
718d67c46b9STakashi Sakamoto 
719d67c46b9STakashi Sakamoto 	mutex_lock(&s->mutex);
720d67c46b9STakashi Sakamoto 
721d67c46b9STakashi Sakamoto 	if (WARN_ON(amdtp_stream_running(s) ||
722d67c46b9STakashi Sakamoto 		    (s->data_block_quadlets < 1))) {
723d67c46b9STakashi Sakamoto 		err = -EBADFD;
724d67c46b9STakashi Sakamoto 		goto err_unlock;
725d67c46b9STakashi Sakamoto 	}
726d67c46b9STakashi Sakamoto 
727d67c46b9STakashi Sakamoto 	if (s->direction == AMDTP_IN_STREAM &&
728d67c46b9STakashi Sakamoto 	    s->flags & CIP_SKIP_INIT_DBC_CHECK)
729d67c46b9STakashi Sakamoto 		s->data_block_counter = UINT_MAX;
730d67c46b9STakashi Sakamoto 	else
731d67c46b9STakashi Sakamoto 		s->data_block_counter = 0;
732d67c46b9STakashi Sakamoto 	s->data_block_state = initial_state[s->sfc].data_block;
733d67c46b9STakashi Sakamoto 	s->syt_offset_state = initial_state[s->sfc].syt_offset;
734d67c46b9STakashi Sakamoto 	s->last_syt_offset = TICKS_PER_CYCLE;
735d67c46b9STakashi Sakamoto 
736d67c46b9STakashi Sakamoto 	/* initialize packet buffer */
737d67c46b9STakashi Sakamoto 	if (s->direction == AMDTP_IN_STREAM) {
738d67c46b9STakashi Sakamoto 		dir = DMA_FROM_DEVICE;
739d67c46b9STakashi Sakamoto 		type = FW_ISO_CONTEXT_RECEIVE;
740d67c46b9STakashi Sakamoto 		header_size = IN_PACKET_HEADER_SIZE;
741d67c46b9STakashi Sakamoto 	} else {
742d67c46b9STakashi Sakamoto 		dir = DMA_TO_DEVICE;
743d67c46b9STakashi Sakamoto 		type = FW_ISO_CONTEXT_TRANSMIT;
744d67c46b9STakashi Sakamoto 		header_size = OUT_PACKET_HEADER_SIZE;
745d67c46b9STakashi Sakamoto 	}
746d67c46b9STakashi Sakamoto 	err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
747d67c46b9STakashi Sakamoto 				      amdtp_stream_get_max_payload(s), dir);
748d67c46b9STakashi Sakamoto 	if (err < 0)
749d67c46b9STakashi Sakamoto 		goto err_unlock;
750d67c46b9STakashi Sakamoto 
751d67c46b9STakashi Sakamoto 	s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
752d67c46b9STakashi Sakamoto 					   type, channel, speed, header_size,
753d67c46b9STakashi Sakamoto 					   amdtp_stream_first_callback, s);
754d67c46b9STakashi Sakamoto 	if (IS_ERR(s->context)) {
755d67c46b9STakashi Sakamoto 		err = PTR_ERR(s->context);
756d67c46b9STakashi Sakamoto 		if (err == -EBUSY)
757d67c46b9STakashi Sakamoto 			dev_err(&s->unit->device,
758d67c46b9STakashi Sakamoto 				"no free stream on this controller\n");
759d67c46b9STakashi Sakamoto 		goto err_buffer;
760d67c46b9STakashi Sakamoto 	}
761d67c46b9STakashi Sakamoto 
762d67c46b9STakashi Sakamoto 	amdtp_stream_update(s);
763d67c46b9STakashi Sakamoto 
764d67c46b9STakashi Sakamoto 	s->packet_index = 0;
765d67c46b9STakashi Sakamoto 	do {
766d67c46b9STakashi Sakamoto 		if (s->direction == AMDTP_IN_STREAM)
767d67c46b9STakashi Sakamoto 			err = queue_in_packet(s);
768d67c46b9STakashi Sakamoto 		else
769d67c46b9STakashi Sakamoto 			err = queue_out_packet(s, 0, true);
770d67c46b9STakashi Sakamoto 		if (err < 0)
771d67c46b9STakashi Sakamoto 			goto err_context;
772d67c46b9STakashi Sakamoto 	} while (s->packet_index > 0);
773d67c46b9STakashi Sakamoto 
774d67c46b9STakashi Sakamoto 	/* NOTE: TAG1 matches CIP. This just affects in stream. */
775d67c46b9STakashi Sakamoto 	tag = FW_ISO_CONTEXT_MATCH_TAG1;
776d67c46b9STakashi Sakamoto 	if (s->flags & CIP_EMPTY_WITH_TAG0)
777d67c46b9STakashi Sakamoto 		tag |= FW_ISO_CONTEXT_MATCH_TAG0;
778d67c46b9STakashi Sakamoto 
779d67c46b9STakashi Sakamoto 	s->callbacked = false;
780d67c46b9STakashi Sakamoto 	err = fw_iso_context_start(s->context, -1, 0, tag);
781d67c46b9STakashi Sakamoto 	if (err < 0)
782d67c46b9STakashi Sakamoto 		goto err_context;
783d67c46b9STakashi Sakamoto 
784d67c46b9STakashi Sakamoto 	mutex_unlock(&s->mutex);
785d67c46b9STakashi Sakamoto 
786d67c46b9STakashi Sakamoto 	return 0;
787d67c46b9STakashi Sakamoto 
788d67c46b9STakashi Sakamoto err_context:
789d67c46b9STakashi Sakamoto 	fw_iso_context_destroy(s->context);
790d67c46b9STakashi Sakamoto 	s->context = ERR_PTR(-1);
791d67c46b9STakashi Sakamoto err_buffer:
792d67c46b9STakashi Sakamoto 	iso_packets_buffer_destroy(&s->buffer, s->unit);
793d67c46b9STakashi Sakamoto err_unlock:
794d67c46b9STakashi Sakamoto 	mutex_unlock(&s->mutex);
795d67c46b9STakashi Sakamoto 
796d67c46b9STakashi Sakamoto 	return err;
797d67c46b9STakashi Sakamoto }
798d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_start);
799d67c46b9STakashi Sakamoto 
800d67c46b9STakashi Sakamoto /**
801d67c46b9STakashi Sakamoto  * amdtp_stream_pcm_pointer - get the PCM buffer position
802d67c46b9STakashi Sakamoto  * @s: the AMDTP stream that transports the PCM data
803d67c46b9STakashi Sakamoto  *
804d67c46b9STakashi Sakamoto  * Returns the current buffer position, in frames.
805d67c46b9STakashi Sakamoto  */
806d67c46b9STakashi Sakamoto unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
807d67c46b9STakashi Sakamoto {
808d67c46b9STakashi Sakamoto 	/* this optimization is allowed to be racy */
809d67c46b9STakashi Sakamoto 	if (s->pointer_flush && amdtp_stream_running(s))
810d67c46b9STakashi Sakamoto 		fw_iso_context_flush_completions(s->context);
811d67c46b9STakashi Sakamoto 	else
812d67c46b9STakashi Sakamoto 		s->pointer_flush = true;
813d67c46b9STakashi Sakamoto 
814d67c46b9STakashi Sakamoto 	return ACCESS_ONCE(s->pcm_buffer_pointer);
815d67c46b9STakashi Sakamoto }
816d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
817d67c46b9STakashi Sakamoto 
818d67c46b9STakashi Sakamoto /**
819d67c46b9STakashi Sakamoto  * amdtp_stream_update - update the stream after a bus reset
820d67c46b9STakashi Sakamoto  * @s: the AMDTP stream
821d67c46b9STakashi Sakamoto  */
822d67c46b9STakashi Sakamoto void amdtp_stream_update(struct amdtp_stream *s)
823d67c46b9STakashi Sakamoto {
824d67c46b9STakashi Sakamoto 	/* Precomputing. */
825d67c46b9STakashi Sakamoto 	ACCESS_ONCE(s->source_node_id_field) =
826d67c46b9STakashi Sakamoto 		(fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) &
827d67c46b9STakashi Sakamoto 								CIP_SID_MASK;
828d67c46b9STakashi Sakamoto }
829d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_update);
830d67c46b9STakashi Sakamoto 
831d67c46b9STakashi Sakamoto /**
832d67c46b9STakashi Sakamoto  * amdtp_stream_stop - stop sending packets
833d67c46b9STakashi Sakamoto  * @s: the AMDTP stream to stop
834d67c46b9STakashi Sakamoto  *
835d67c46b9STakashi Sakamoto  * All PCM and MIDI devices of the stream must be stopped before the stream
836d67c46b9STakashi Sakamoto  * itself can be stopped.
837d67c46b9STakashi Sakamoto  */
838d67c46b9STakashi Sakamoto void amdtp_stream_stop(struct amdtp_stream *s)
839d67c46b9STakashi Sakamoto {
840d67c46b9STakashi Sakamoto 	mutex_lock(&s->mutex);
841d67c46b9STakashi Sakamoto 
842d67c46b9STakashi Sakamoto 	if (!amdtp_stream_running(s)) {
843d67c46b9STakashi Sakamoto 		mutex_unlock(&s->mutex);
844d67c46b9STakashi Sakamoto 		return;
845d67c46b9STakashi Sakamoto 	}
846d67c46b9STakashi Sakamoto 
847d67c46b9STakashi Sakamoto 	tasklet_kill(&s->period_tasklet);
848d67c46b9STakashi Sakamoto 	fw_iso_context_stop(s->context);
849d67c46b9STakashi Sakamoto 	fw_iso_context_destroy(s->context);
850d67c46b9STakashi Sakamoto 	s->context = ERR_PTR(-1);
851d67c46b9STakashi Sakamoto 	iso_packets_buffer_destroy(&s->buffer, s->unit);
852d67c46b9STakashi Sakamoto 
853d67c46b9STakashi Sakamoto 	s->callbacked = false;
854d67c46b9STakashi Sakamoto 
855d67c46b9STakashi Sakamoto 	mutex_unlock(&s->mutex);
856d67c46b9STakashi Sakamoto }
857d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_stop);
858d67c46b9STakashi Sakamoto 
859d67c46b9STakashi Sakamoto /**
860d67c46b9STakashi Sakamoto  * amdtp_stream_pcm_abort - abort the running PCM device
861d67c46b9STakashi Sakamoto  * @s: the AMDTP stream about to be stopped
862d67c46b9STakashi Sakamoto  *
863d67c46b9STakashi Sakamoto  * If the isochronous stream needs to be stopped asynchronously, call this
864d67c46b9STakashi Sakamoto  * function first to stop the PCM device.
865d67c46b9STakashi Sakamoto  */
866d67c46b9STakashi Sakamoto void amdtp_stream_pcm_abort(struct amdtp_stream *s)
867d67c46b9STakashi Sakamoto {
868d67c46b9STakashi Sakamoto 	struct snd_pcm_substream *pcm;
869d67c46b9STakashi Sakamoto 
870d67c46b9STakashi Sakamoto 	pcm = ACCESS_ONCE(s->pcm);
871d67c46b9STakashi Sakamoto 	if (pcm)
872d67c46b9STakashi Sakamoto 		snd_pcm_stop_xrun(pcm);
873d67c46b9STakashi Sakamoto }
874d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_abort);
875