blob: 646e8e390773b2c4d2c86b3f0df3e5176f82f4e2 [file] [log] [blame]
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001/*
2 * Audio and Music Data Transmission Protocol (IEC 61883-6) streams
3 * with Common Isochronous Packet (IEC 61883-1) headers
4 *
5 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
8
9#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/firewire.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <sound/pcm.h>
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +090015#include <sound/pcm_params.h>
Takashi Sakamotod67c46b2015-09-19 11:21:54 +090016#include "amdtp-stream.h"
Clemens Ladisch31ef9132011-03-15 07:53:21 +010017
18#define TICKS_PER_CYCLE 3072
19#define CYCLES_PER_SECOND 8000
20#define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
21
Takashi Sakamoto0c95c1d2016-05-09 21:12:46 +090022/* Always support Linux tracing subsystem. */
23#define CREATE_TRACE_POINTS
24#include "amdtp-stream-trace.h"
25
Takashi Sakamotoca5b5052015-02-21 11:50:17 +090026#define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */
Clemens Ladisch31ef9132011-03-15 07:53:21 +010027
Takashi Sakamotob445db42014-04-25 22:44:43 +090028/* isochronous header parameters */
29#define ISO_DATA_LENGTH_SHIFT 16
Clemens Ladisch31ef9132011-03-15 07:53:21 +010030#define TAG_CIP 1
31
Takashi Sakamotob445db42014-04-25 22:44:43 +090032/* common isochronous packet header parameters */
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090033#define CIP_EOH_SHIFT 31
34#define CIP_EOH (1u << CIP_EOH_SHIFT)
Takashi Sakamotob445db42014-04-25 22:44:43 +090035#define CIP_EOH_MASK 0x80000000
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090036#define CIP_SID_SHIFT 24
37#define CIP_SID_MASK 0x3f000000
38#define CIP_DBS_MASK 0x00ff0000
39#define CIP_DBS_SHIFT 16
Takashi Sakamoto98638742017-03-22 21:30:16 +090040#define CIP_SPH_MASK 0x00000400
41#define CIP_SPH_SHIFT 10
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090042#define CIP_DBC_MASK 0x000000ff
43#define CIP_FMT_SHIFT 24
Takashi Sakamotob445db42014-04-25 22:44:43 +090044#define CIP_FMT_MASK 0x3f000000
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090045#define CIP_FDF_MASK 0x00ff0000
46#define CIP_FDF_SHIFT 16
Takashi Sakamotob445db42014-04-25 22:44:43 +090047#define CIP_SYT_MASK 0x0000ffff
48#define CIP_SYT_NO_INFO 0xffff
Takashi Sakamotob445db42014-04-25 22:44:43 +090049
Takashi Sakamoto51c29fd2015-09-19 11:21:56 +090050/* Audio and Music transfer protocol specific parameters */
Takashi Sakamoto414ba022015-09-19 11:21:53 +090051#define CIP_FMT_AM 0x10
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090052#define AMDTP_FDF_NO_DATA 0xff
Clemens Ladisch31ef9132011-03-15 07:53:21 +010053
54/* TODO: make these configurable */
55#define INTERRUPT_INTERVAL 16
56#define QUEUE_LENGTH 48
57
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090058#define IN_PACKET_HEADER_SIZE 4
Takashi Sakamoto4b7da112014-04-25 22:44:45 +090059#define OUT_PACKET_HEADER_SIZE 0
60
Clemens Ladisch76fb8782012-05-13 22:03:09 +020061static void pcm_period_tasklet(unsigned long data);
62
Clemens Ladisch31ef9132011-03-15 07:53:21 +010063/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090064 * amdtp_stream_init - initialize an AMDTP stream structure
65 * @s: the AMDTP stream to initialize
Clemens Ladisch31ef9132011-03-15 07:53:21 +010066 * @unit: the target of the stream
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090067 * @dir: the direction of stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +010068 * @flags: the packet transmission method to use
Takashi Sakamoto59558152015-09-19 11:21:55 +090069 * @fmt: the value of fmt field in CIP header
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090070 * @process_data_blocks: callback handler to process data blocks
71 * @protocol_size: the size to allocate newly for protocol
Clemens Ladisch31ef9132011-03-15 07:53:21 +010072 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090073int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
Takashi Sakamoto59558152015-09-19 11:21:55 +090074 enum amdtp_stream_direction dir, enum cip_flags flags,
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090075 unsigned int fmt,
76 amdtp_stream_process_data_blocks_t process_data_blocks,
77 unsigned int protocol_size)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010078{
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090079 if (process_data_blocks == NULL)
80 return -EINVAL;
81
82 s->protocol = kzalloc(protocol_size, GFP_KERNEL);
83 if (!s->protocol)
84 return -ENOMEM;
85
Takashi Sakamotoc6f224d2015-02-21 23:54:58 +090086 s->unit = unit;
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090087 s->direction = dir;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010088 s->flags = flags;
89 s->context = ERR_PTR(-1);
90 mutex_init(&s->mutex);
Clemens Ladisch76fb8782012-05-13 22:03:09 +020091 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +010092 s->packet_index = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010093
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +090094 init_waitqueue_head(&s->callback_wait);
95 s->callbacked = false;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +090096
Takashi Sakamoto59558152015-09-19 11:21:55 +090097 s->fmt = fmt;
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090098 s->process_data_blocks = process_data_blocks;
Takashi Sakamoto414ba022015-09-19 11:21:53 +090099
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100100 return 0;
101}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900102EXPORT_SYMBOL(amdtp_stream_init);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100103
104/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900105 * amdtp_stream_destroy - free stream resources
106 * @s: the AMDTP stream to destroy
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100107 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900108void amdtp_stream_destroy(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100109{
Takashi Sakamoto44c376b2016-03-31 08:47:02 +0900110 /* Not initialized. */
111 if (s->protocol == NULL)
112 return;
113
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900114 WARN_ON(amdtp_stream_running(s));
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900115 kfree(s->protocol);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100116 mutex_destroy(&s->mutex);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100117}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900118EXPORT_SYMBOL(amdtp_stream_destroy);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100119
Clemens Ladischc5280e92011-10-16 21:39:00 +0200120const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
Clemens Ladischa7304e32011-09-04 22:16:10 +0200121 [CIP_SFC_32000] = 8,
122 [CIP_SFC_44100] = 8,
123 [CIP_SFC_48000] = 8,
124 [CIP_SFC_88200] = 16,
125 [CIP_SFC_96000] = 16,
126 [CIP_SFC_176400] = 32,
127 [CIP_SFC_192000] = 32,
128};
129EXPORT_SYMBOL(amdtp_syt_intervals);
130
Takashi Sakamotof9503a62014-05-28 00:14:36 +0900131const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900132 [CIP_SFC_32000] = 32000,
133 [CIP_SFC_44100] = 44100,
134 [CIP_SFC_48000] = 48000,
135 [CIP_SFC_88200] = 88200,
136 [CIP_SFC_96000] = 96000,
137 [CIP_SFC_176400] = 176400,
138 [CIP_SFC_192000] = 192000,
139};
140EXPORT_SYMBOL(amdtp_rate_table);
141
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100142/**
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900143 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
144 * @s: the AMDTP stream, which must be initialized.
145 * @runtime: the PCM substream runtime
146 */
147int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
148 struct snd_pcm_runtime *runtime)
149{
150 int err;
151
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900152 /*
153 * Currently firewire-lib processes 16 packets in one software
154 * interrupt callback. This equals to 2msec but actually the
155 * interval of the interrupts has a jitter.
156 * Additionally, even if adding a constraint to fit period size to
157 * 2msec, actual calculated frames per period doesn't equal to 2msec,
158 * depending on sampling rate.
159 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
160 * Here let us use 5msec for safe period interrupt.
161 */
162 err = snd_pcm_hw_constraint_minmax(runtime,
163 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
164 5000, UINT_MAX);
165 if (err < 0)
166 goto end;
167
168 /* Non-Blocking stream has no more constraints */
169 if (!(s->flags & CIP_BLOCKING))
170 goto end;
171
172 /*
173 * One AMDTP packet can include some frames. In blocking mode, the
174 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
175 * depending on its sampling rate. For accurate period interrupt, it's
Yannick Guerrinice991982015-03-09 22:13:03 +0100176 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900177 *
Yannick Guerrinice991982015-03-09 22:13:03 +0100178 * TODO: These constraints can be improved with proper rules.
179 * Currently apply LCM of SYT_INTERVALs.
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900180 */
181 err = snd_pcm_hw_constraint_step(runtime, 0,
182 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
183 if (err < 0)
184 goto end;
185 err = snd_pcm_hw_constraint_step(runtime, 0,
186 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
187end:
188 return err;
189}
190EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
191
192/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900193 * amdtp_stream_set_parameters - set stream parameters
194 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100195 * @rate: the sample rate
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900196 * @data_block_quadlets: the size of a data block in quadlet unit
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100197 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200198 * The parameters must be set before the stream is started, and must not be
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100199 * changed while the stream is running.
200 */
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900201int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
202 unsigned int data_block_quadlets)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100203{
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900204 unsigned int sfc;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100205
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900206 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900207 if (amdtp_rate_table[sfc] == rate)
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900208 break;
209 }
210 if (sfc == ARRAY_SIZE(amdtp_rate_table))
211 return -EINVAL;
Clemens Ladische84d15f2011-09-04 22:12:48 +0200212
Clemens Ladische84d15f2011-09-04 22:12:48 +0200213 s->sfc = sfc;
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900214 s->data_block_quadlets = data_block_quadlets;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200215 s->syt_interval = amdtp_syt_intervals[sfc];
Clemens Ladische84d15f2011-09-04 22:12:48 +0200216
217 /* default buffering in the device */
218 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
219 if (s->flags & CIP_BLOCKING)
220 /* additional buffering needed to adjust for no-data packets */
221 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900222
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900223 return 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100224}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900225EXPORT_SYMBOL(amdtp_stream_set_parameters);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100226
227/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900228 * amdtp_stream_get_max_payload - get the stream's packet size
229 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100230 *
231 * This function must not be called before the stream has been configured
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900232 * with amdtp_stream_set_parameters().
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100233 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900234unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100235{
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900236 unsigned int multiplier = 1;
237
238 if (s->flags & CIP_JUMBO_PAYLOAD)
239 multiplier = 5;
240
241 return 8 + s->syt_interval * s->data_block_quadlets * 4 * multiplier;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100242}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900243EXPORT_SYMBOL(amdtp_stream_get_max_payload);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100244
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200245/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900246 * amdtp_stream_pcm_prepare - prepare PCM device for running
247 * @s: the AMDTP stream
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200248 *
249 * This function should be called from the PCM device's .prepare callback.
250 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900251void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200252{
253 tasklet_kill(&s->period_tasklet);
254 s->pcm_buffer_pointer = 0;
255 s->pcm_period_pointer = 0;
256}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900257EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200258
Takashi Sakamoto875be092015-05-22 23:00:51 +0900259static unsigned int calculate_data_blocks(struct amdtp_stream *s,
260 unsigned int syt)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100261{
262 unsigned int phase, data_blocks;
263
Takashi Sakamoto875be092015-05-22 23:00:51 +0900264 /* Blocking mode. */
265 if (s->flags & CIP_BLOCKING) {
266 /* This module generate empty packet for 'no data'. */
267 if (syt == CIP_SYT_NO_INFO)
268 data_blocks = 0;
269 else
270 data_blocks = s->syt_interval;
271 /* Non-blocking mode. */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100272 } else {
Takashi Sakamoto875be092015-05-22 23:00:51 +0900273 if (!cip_sfc_is_base_44100(s->sfc)) {
274 /* Sample_rate / 8000 is an integer, and precomputed. */
275 data_blocks = s->data_block_state;
276 } else {
277 phase = s->data_block_state;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100278
279 /*
280 * This calculates the number of data blocks per packet so that
281 * 1) the overall rate is correct and exactly synchronized to
282 * the bus clock, and
283 * 2) packets with a rounded-up number of blocks occur as early
284 * as possible in the sequence (to prevent underruns of the
285 * device's buffer).
286 */
Takashi Sakamoto875be092015-05-22 23:00:51 +0900287 if (s->sfc == CIP_SFC_44100)
288 /* 6 6 5 6 5 6 5 ... */
289 data_blocks = 5 + ((phase & 1) ^
290 (phase == 0 || phase >= 40));
291 else
292 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
293 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
294 if (++phase >= (80 >> (s->sfc >> 1)))
295 phase = 0;
296 s->data_block_state = phase;
297 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100298 }
299
300 return data_blocks;
301}
302
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900303static unsigned int calculate_syt(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100304 unsigned int cycle)
305{
306 unsigned int syt_offset, phase, index, syt;
307
308 if (s->last_syt_offset < TICKS_PER_CYCLE) {
309 if (!cip_sfc_is_base_44100(s->sfc))
310 syt_offset = s->last_syt_offset + s->syt_offset_state;
311 else {
312 /*
313 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
314 * n * SYT_INTERVAL * 24576000 / sample_rate
315 * Modulo TICKS_PER_CYCLE, the difference between successive
316 * elements is about 1386.23. Rounding the results of this
317 * formula to the SYT precision results in a sequence of
318 * differences that begins with:
319 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
320 * This code generates _exactly_ the same sequence.
321 */
322 phase = s->syt_offset_state;
323 index = phase % 13;
324 syt_offset = s->last_syt_offset;
325 syt_offset += 1386 + ((index && !(index & 3)) ||
326 phase == 146);
327 if (++phase >= 147)
328 phase = 0;
329 s->syt_offset_state = phase;
330 }
331 } else
332 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
333 s->last_syt_offset = syt_offset;
334
Clemens Ladischbe454362011-03-15 07:55:02 +0100335 if (syt_offset < TICKS_PER_CYCLE) {
Clemens Ladische84d15f2011-09-04 22:12:48 +0200336 syt_offset += s->transfer_delay;
Clemens Ladischbe454362011-03-15 07:55:02 +0100337 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
338 syt += syt_offset % TICKS_PER_CYCLE;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100339
Takashi Sakamotob445db42014-04-25 22:44:43 +0900340 return syt & CIP_SYT_MASK;
Clemens Ladischbe454362011-03-15 07:55:02 +0100341 } else {
Takashi Sakamotob445db42014-04-25 22:44:43 +0900342 return CIP_SYT_NO_INFO;
Clemens Ladischbe454362011-03-15 07:55:02 +0100343 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100344}
345
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900346static void update_pcm_pointers(struct amdtp_stream *s,
347 struct snd_pcm_substream *pcm,
348 unsigned int frames)
Takashi Sakamoto65845f22014-08-29 13:40:45 +0900349{
350 unsigned int ptr;
351
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900352 ptr = s->pcm_buffer_pointer + frames;
353 if (ptr >= pcm->runtime->buffer_size)
354 ptr -= pcm->runtime->buffer_size;
355 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
356
357 s->pcm_period_pointer += frames;
358 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
359 s->pcm_period_pointer -= pcm->runtime->period_size;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900360 tasklet_hi_schedule(&s->period_tasklet);
361 }
362}
363
364static void pcm_period_tasklet(unsigned long data)
365{
366 struct amdtp_stream *s = (void *)data;
367 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
368
369 if (pcm)
370 snd_pcm_period_elapsed(pcm);
371}
372
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900373static int queue_packet(struct amdtp_stream *s, unsigned int header_length,
374 unsigned int payload_length)
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900375{
376 struct fw_iso_packet p = {0};
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900377 int err = 0;
378
379 if (IS_ERR(s->context))
380 goto end;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900381
382 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
383 p.tag = TAG_CIP;
384 p.header_length = header_length;
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900385 if (payload_length > 0)
386 p.payload_length = payload_length;
387 else
388 p.skip = true;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900389 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
390 s->buffer.packets[s->packet_index].offset);
391 if (err < 0) {
392 dev_err(&s->unit->device, "queueing error: %d\n", err);
393 goto end;
394 }
395
396 if (++s->packet_index >= QUEUE_LENGTH)
397 s->packet_index = 0;
398end:
399 return err;
400}
401
402static inline int queue_out_packet(struct amdtp_stream *s,
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900403 unsigned int payload_length)
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900404{
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900405 return queue_packet(s, OUT_PACKET_HEADER_SIZE, payload_length);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900406}
407
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900408static inline int queue_in_packet(struct amdtp_stream *s)
409{
410 return queue_packet(s, IN_PACKET_HEADER_SIZE,
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900411 amdtp_stream_get_max_payload(s));
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900412}
413
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900414static int handle_out_packet(struct amdtp_stream *s,
415 unsigned int payload_length, unsigned int cycle,
Takashi Sakamotoa9c42842016-05-11 07:33:27 +0900416 unsigned int index)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100417{
418 __be32 *buffer;
Takashi Sakamoto390a1512016-05-09 23:15:55 +0900419 unsigned int syt;
420 unsigned int data_blocks;
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900421 unsigned int pcm_frames;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100422 struct snd_pcm_substream *pcm;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100423
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900424 buffer = s->buffer.packets[s->packet_index].buffer;
Takashi Sakamoto390a1512016-05-09 23:15:55 +0900425 syt = calculate_syt(s, cycle);
426 data_blocks = calculate_data_blocks(s, syt);
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900427 pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900428
Takashi Sakamoto9dae0172017-03-22 21:30:17 +0900429 if (s->flags & CIP_DBC_IS_END_EVENT)
430 s->data_block_counter =
431 (s->data_block_counter + data_blocks) & 0xff;
432
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100433 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900434 (s->data_block_quadlets << CIP_DBS_SHIFT) |
Takashi Sakamoto98638742017-03-22 21:30:16 +0900435 ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) |
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100436 s->data_block_counter);
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900437 buffer[1] = cpu_to_be32(CIP_EOH |
438 ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
439 ((s->fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
440 (syt & CIP_SYT_MASK));
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100441
Takashi Sakamoto9dae0172017-03-22 21:30:17 +0900442 if (!(s->flags & CIP_DBC_IS_END_EVENT))
443 s->data_block_counter =
444 (s->data_block_counter + data_blocks) & 0xff;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900445 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
Takashi Sakamoto0c95c1d2016-05-09 21:12:46 +0900446
Takashi Sakamotoa9c42842016-05-11 07:33:27 +0900447 trace_out_packet(s, cycle, buffer, payload_length, index);
Takashi Sakamoto0c95c1d2016-05-09 21:12:46 +0900448
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900449 if (queue_out_packet(s, payload_length) < 0)
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900450 return -EIO;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100451
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900452 pcm = ACCESS_ONCE(s->pcm);
453 if (pcm && pcm_frames > 0)
454 update_pcm_pointers(s, pcm, pcm_frames);
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900455
456 /* No need to return the number of handled data blocks. */
457 return 0;
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200458}
459
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900460static int handle_in_packet(struct amdtp_stream *s,
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900461 unsigned int payload_length, unsigned int cycle,
Takashi Sakamotoa9c42842016-05-11 07:33:27 +0900462 unsigned int index)
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900463{
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900464 __be32 *buffer;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900465 u32 cip_header[2];
Takashi Sakamoto98638742017-03-22 21:30:16 +0900466 unsigned int sph, fmt, fdf, syt;
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900467 unsigned int data_block_quadlets, data_block_counter, dbc_interval;
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900468 unsigned int data_blocks;
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900469 struct snd_pcm_substream *pcm;
470 unsigned int pcm_frames;
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900471 bool lost;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900472
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900473 buffer = s->buffer.packets[s->packet_index].buffer;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900474 cip_header[0] = be32_to_cpu(buffer[0]);
475 cip_header[1] = be32_to_cpu(buffer[1]);
476
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900477 trace_in_packet(s, cycle, cip_header, payload_length, index);
Takashi Sakamoto0c95c1d2016-05-09 21:12:46 +0900478
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900479 /*
480 * This module supports 'Two-quadlet CIP header with SYT field'.
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900481 * For convenience, also check FMT field is AM824 or not.
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900482 */
Takashi Sakamoto2128f782017-03-22 21:30:27 +0900483 if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
484 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) &&
485 (!(s->flags & CIP_HEADER_WITHOUT_EOH))) {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900486 dev_info_ratelimited(&s->unit->device,
487 "Invalid CIP header for AMDTP: %08X:%08X\n",
488 cip_header[0], cip_header[1]);
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900489 data_blocks = 0;
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900490 pcm_frames = 0;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900491 goto end;
492 }
493
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900494 /* Check valid protocol or not. */
Takashi Sakamoto98638742017-03-22 21:30:16 +0900495 sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT;
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900496 fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
Takashi Sakamoto98638742017-03-22 21:30:16 +0900497 if (sph != s->sph || fmt != s->fmt) {
Takashi Sakamoto2a7e1712015-10-11 22:33:50 +0900498 dev_info_ratelimited(&s->unit->device,
499 "Detect unexpected protocol: %08x %08x\n",
500 cip_header[0], cip_header[1]);
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900501 data_blocks = 0;
Takashi Sakamoto2a7e1712015-10-11 22:33:50 +0900502 pcm_frames = 0;
503 goto end;
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900504 }
505
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900506 /* Calculate data blocks */
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900507 fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT;
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900508 if (payload_length < 12 ||
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900509 (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900510 data_blocks = 0;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900511 } else {
512 data_block_quadlets =
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900513 (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900514 /* avoid division by zero */
515 if (data_block_quadlets == 0) {
Takashi Sakamoto12e0f432015-05-22 23:21:13 +0900516 dev_err(&s->unit->device,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900517 "Detect invalid value in dbs field: %08X\n",
518 cip_header[0]);
Takashi Sakamotoa9007052015-05-22 23:21:14 +0900519 return -EPROTO;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900520 }
Takashi Sakamoto69702232014-04-25 22:45:05 +0900521 if (s->flags & CIP_WRONG_DBS)
522 data_block_quadlets = s->data_block_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900523
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900524 data_blocks = (payload_length / 4 - 2) /
525 data_block_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900526 }
527
528 /* Check data block counter continuity */
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900529 data_block_counter = cip_header[0] & CIP_DBC_MASK;
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900530 if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
Takashi Sakamoto9d591242014-04-25 22:45:27 +0900531 s->data_block_counter != UINT_MAX)
532 data_block_counter = s->data_block_counter;
533
Takashi Sakamoto18f5ed32015-08-05 09:21:05 +0900534 if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) &&
535 data_block_counter == s->tx_first_dbc) ||
536 s->data_block_counter == UINT_MAX) {
Takashi Sakamotob84b1a22014-04-25 22:45:07 +0900537 lost = false;
538 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900539 lost = data_block_counter != s->data_block_counter;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900540 } else {
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900541 if (data_blocks > 0 && s->tx_dbc_interval > 0)
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900542 dbc_interval = s->tx_dbc_interval;
543 else
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900544 dbc_interval = data_blocks;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900545
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900546 lost = data_block_counter !=
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900547 ((s->data_block_counter + dbc_interval) & 0xff);
548 }
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900549
550 if (lost) {
Takashi Sakamoto12e0f432015-05-22 23:21:13 +0900551 dev_err(&s->unit->device,
552 "Detect discontinuity of CIP: %02X %02X\n",
553 s->data_block_counter, data_block_counter);
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900554 return -EIO;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900555 }
556
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900557 syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
558 pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900559
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900560 if (s->flags & CIP_DBC_IS_END_EVENT)
561 s->data_block_counter = data_block_counter;
562 else
563 s->data_block_counter =
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900564 (data_block_counter + data_blocks) & 0xff;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900565end:
566 if (queue_in_packet(s) < 0)
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900567 return -EIO;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900568
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900569 pcm = ACCESS_ONCE(s->pcm);
570 if (pcm && pcm_frames > 0)
571 update_pcm_pointers(s, pcm, pcm_frames);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900572
Takashi Sakamoto31ea49b2015-05-28 00:02:59 +0900573 return 0;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900574}
575
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900576/*
577 * In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
578 * the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
579 * it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
580 */
581static inline u32 compute_cycle_count(u32 tstamp)
582{
583 return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff);
584}
585
586static inline u32 increment_cycle_count(u32 cycle, unsigned int addend)
587{
588 cycle += addend;
589 if (cycle >= 8 * CYCLES_PER_SECOND)
590 cycle -= 8 * CYCLES_PER_SECOND;
591 return cycle;
592}
593
Takashi Sakamotof90e2de2016-05-09 21:12:45 +0900594static inline u32 decrement_cycle_count(u32 cycle, unsigned int subtrahend)
595{
596 if (cycle < subtrahend)
597 cycle += 8 * CYCLES_PER_SECOND;
598 return cycle - subtrahend;
599}
600
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900601static void out_stream_callback(struct fw_iso_context *context, u32 tstamp,
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900602 size_t header_length, void *header,
603 void *private_data)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100604{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900605 struct amdtp_stream *s = private_data;
Takashi Sakamoto390a1512016-05-09 23:15:55 +0900606 unsigned int i, packets = header_length / 4;
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900607 u32 cycle;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100608
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900609 if (s->packet_index < 0)
610 return;
611
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900612 cycle = compute_cycle_count(tstamp);
613
614 /* Align to actual cycle count for the last packet. */
615 cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100616
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900617 for (i = 0; i < packets; ++i) {
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900618 cycle = increment_cycle_count(cycle, 1);
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900619 if (handle_out_packet(s, 0, cycle, i) < 0) {
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900620 s->packet_index = -1;
621 amdtp_stream_pcm_abort(s);
622 return;
623 }
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900624 }
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900625
Clemens Ladisch13882a82011-05-02 09:33:56 +0200626 fw_iso_context_queue_flush(s->context);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100627}
628
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900629static void in_stream_callback(struct fw_iso_context *context, u32 tstamp,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900630 size_t header_length, void *header,
631 void *private_data)
632{
633 struct amdtp_stream *s = private_data;
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900634 unsigned int i, packets;
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900635 unsigned int payload_length, max_payload_length;
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900636 __be32 *headers = header;
Takashi Sakamotof90e2de2016-05-09 21:12:45 +0900637 u32 cycle;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900638
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900639 if (s->packet_index < 0)
640 return;
641
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900642 /* The number of packets in buffer */
643 packets = header_length / IN_PACKET_HEADER_SIZE;
644
Takashi Sakamotof90e2de2016-05-09 21:12:45 +0900645 cycle = compute_cycle_count(tstamp);
646
647 /* Align to actual cycle count for the last packet. */
648 cycle = decrement_cycle_count(cycle, packets);
649
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900650 /* For buffer-over-run prevention. */
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900651 max_payload_length = amdtp_stream_get_max_payload(s);
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900652
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900653 for (i = 0; i < packets; i++) {
Takashi Sakamotof90e2de2016-05-09 21:12:45 +0900654 cycle = increment_cycle_count(cycle, 1);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900655
656 /* The number of quadlets in this packet */
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900657 payload_length =
658 (be32_to_cpu(headers[i]) >> ISO_DATA_LENGTH_SHIFT);
659 if (payload_length > max_payload_length) {
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900660 dev_err(&s->unit->device,
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900661 "Detect jumbo payload: %04x %04x\n",
662 payload_length, max_payload_length);
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900663 break;
664 }
665
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900666 if (handle_in_packet(s, payload_length, cycle, i) < 0)
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900667 break;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900668 }
669
Takashi Sakamotodec63cc2016-05-09 23:15:53 +0900670 /* Queueing error or detecting invalid payload. */
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900671 if (i < packets) {
Takashi Sakamotodec63cc2016-05-09 23:15:53 +0900672 s->packet_index = -1;
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900673 amdtp_stream_pcm_abort(s);
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900674 return;
675 }
676
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900677 fw_iso_context_queue_flush(s->context);
678}
679
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900680/* this is executed one time */
681static void amdtp_stream_first_callback(struct fw_iso_context *context,
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900682 u32 tstamp, size_t header_length,
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900683 void *header, void *private_data)
684{
685 struct amdtp_stream *s = private_data;
Takashi Sakamotoa04513f2017-03-22 21:30:15 +0900686 u32 cycle;
687 unsigned int packets;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900688
689 /*
690 * For in-stream, first packet has come.
691 * For out-stream, prepared to transmit first packet
692 */
693 s->callbacked = true;
694 wake_up(&s->callback_wait);
695
Takashi Sakamotoa04513f2017-03-22 21:30:15 +0900696 cycle = compute_cycle_count(tstamp);
697
698 if (s->direction == AMDTP_IN_STREAM) {
699 packets = header_length / IN_PACKET_HEADER_SIZE;
700 cycle = decrement_cycle_count(cycle, packets);
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900701 context->callback.sc = in_stream_callback;
Takashi Sakamotoa04513f2017-03-22 21:30:15 +0900702 } else {
703 packets = header_length / 4;
704 cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900705 context->callback.sc = out_stream_callback;
Takashi Sakamotoa04513f2017-03-22 21:30:15 +0900706 }
707
708 s->start_cycle = cycle;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900709
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900710 context->callback.sc(context, tstamp, header_length, header, s);
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900711}
712
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100713/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900714 * amdtp_stream_start - start transferring packets
715 * @s: the AMDTP stream to start
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100716 * @channel: the isochronous channel on the bus
717 * @speed: firewire speed code
718 *
719 * The stream cannot be started until it has been configured with
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900720 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
721 * device can be started.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100722 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900723int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100724{
725 static const struct {
726 unsigned int data_block;
727 unsigned int syt_offset;
728 } initial_state[] = {
729 [CIP_SFC_32000] = { 4, 3072 },
730 [CIP_SFC_48000] = { 6, 1024 },
731 [CIP_SFC_96000] = { 12, 1024 },
732 [CIP_SFC_192000] = { 24, 1024 },
733 [CIP_SFC_44100] = { 0, 67 },
734 [CIP_SFC_88200] = { 0, 67 },
735 [CIP_SFC_176400] = { 0, 67 },
736 };
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900737 unsigned int header_size;
738 enum dma_data_direction dir;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900739 int type, tag, err;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100740
741 mutex_lock(&s->mutex);
742
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900743 if (WARN_ON(amdtp_stream_running(s) ||
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900744 (s->data_block_quadlets < 1))) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100745 err = -EBADFD;
746 goto err_unlock;
747 }
748
Takashi Sakamoto62f00e42016-05-09 23:15:56 +0900749 if (s->direction == AMDTP_IN_STREAM)
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900750 s->data_block_counter = UINT_MAX;
751 else
752 s->data_block_counter = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100753 s->data_block_state = initial_state[s->sfc].data_block;
754 s->syt_offset_state = initial_state[s->sfc].syt_offset;
755 s->last_syt_offset = TICKS_PER_CYCLE;
756
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900757 /* initialize packet buffer */
758 if (s->direction == AMDTP_IN_STREAM) {
759 dir = DMA_FROM_DEVICE;
760 type = FW_ISO_CONTEXT_RECEIVE;
761 header_size = IN_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900762 } else {
763 dir = DMA_TO_DEVICE;
764 type = FW_ISO_CONTEXT_TRANSMIT;
765 header_size = OUT_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900766 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100767 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900768 amdtp_stream_get_max_payload(s), dir);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100769 if (err < 0)
770 goto err_unlock;
771
772 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900773 type, channel, speed, header_size,
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900774 amdtp_stream_first_callback, s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100775 if (IS_ERR(s->context)) {
776 err = PTR_ERR(s->context);
777 if (err == -EBUSY)
778 dev_err(&s->unit->device,
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900779 "no free stream on this controller\n");
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100780 goto err_buffer;
781 }
782
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900783 amdtp_stream_update(s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100784
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100785 s->packet_index = 0;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900786 do {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900787 if (s->direction == AMDTP_IN_STREAM)
788 err = queue_in_packet(s);
789 else
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900790 err = queue_out_packet(s, 0);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900791 if (err < 0)
792 goto err_context;
793 } while (s->packet_index > 0);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100794
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900795 /* NOTE: TAG1 matches CIP. This just affects in stream. */
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900796 tag = FW_ISO_CONTEXT_MATCH_TAG1;
797 if (s->flags & CIP_EMPTY_WITH_TAG0)
798 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
799
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900800 s->callbacked = false;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900801 err = fw_iso_context_start(s->context, -1, 0, tag);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100802 if (err < 0)
803 goto err_context;
804
805 mutex_unlock(&s->mutex);
806
807 return 0;
808
809err_context:
810 fw_iso_context_destroy(s->context);
811 s->context = ERR_PTR(-1);
812err_buffer:
813 iso_packets_buffer_destroy(&s->buffer, s->unit);
814err_unlock:
815 mutex_unlock(&s->mutex);
816
817 return err;
818}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900819EXPORT_SYMBOL(amdtp_stream_start);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100820
821/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900822 * amdtp_stream_pcm_pointer - get the PCM buffer position
823 * @s: the AMDTP stream that transports the PCM data
Clemens Ladische9148dd2012-05-13 18:49:14 +0200824 *
825 * Returns the current buffer position, in frames.
826 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900827unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
Clemens Ladische9148dd2012-05-13 18:49:14 +0200828{
Takashi Sakamoto1dba9db2016-05-12 02:17:39 +0900829 /*
830 * This function is called in software IRQ context of period_tasklet or
831 * process context.
832 *
833 * When the software IRQ context was scheduled by software IRQ context
834 * of IR/IT contexts, queued packets were already handled. Therefore,
835 * no need to flush the queue in buffer anymore.
836 *
837 * When the process context reach here, some packets will be already
838 * queued in the buffer. These packets should be handled immediately
839 * to keep better granularity of PCM pointer.
840 *
841 * Later, the process context will sometimes schedules software IRQ
842 * context of the period_tasklet. Then, no need to flush the queue by
843 * the same reason as described for IR/IT contexts.
844 */
845 if (!in_interrupt() && amdtp_stream_running(s))
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200846 fw_iso_context_flush_completions(s->context);
Clemens Ladische9148dd2012-05-13 18:49:14 +0200847
848 return ACCESS_ONCE(s->pcm_buffer_pointer);
849}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900850EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
Clemens Ladische9148dd2012-05-13 18:49:14 +0200851
852/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900853 * amdtp_stream_update - update the stream after a bus reset
854 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100855 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900856void amdtp_stream_update(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100857{
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900858 /* Precomputing. */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100859 ACCESS_ONCE(s->source_node_id_field) =
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900860 (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) &
861 CIP_SID_MASK;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100862}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900863EXPORT_SYMBOL(amdtp_stream_update);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100864
865/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900866 * amdtp_stream_stop - stop sending packets
867 * @s: the AMDTP stream to stop
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100868 *
869 * All PCM and MIDI devices of the stream must be stopped before the stream
870 * itself can be stopped.
871 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900872void amdtp_stream_stop(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100873{
874 mutex_lock(&s->mutex);
875
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900876 if (!amdtp_stream_running(s)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100877 mutex_unlock(&s->mutex);
878 return;
879 }
880
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200881 tasklet_kill(&s->period_tasklet);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100882 fw_iso_context_stop(s->context);
883 fw_iso_context_destroy(s->context);
884 s->context = ERR_PTR(-1);
885 iso_packets_buffer_destroy(&s->buffer, s->unit);
886
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900887 s->callbacked = false;
888
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100889 mutex_unlock(&s->mutex);
890}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900891EXPORT_SYMBOL(amdtp_stream_stop);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100892
893/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900894 * amdtp_stream_pcm_abort - abort the running PCM device
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100895 * @s: the AMDTP stream about to be stopped
896 *
897 * If the isochronous stream needs to be stopped asynchronously, call this
898 * function first to stop the PCM device.
899 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900900void amdtp_stream_pcm_abort(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100901{
902 struct snd_pcm_substream *pcm;
903
904 pcm = ACCESS_ONCE(s->pcm);
Takashi Iwai1fb85102014-11-07 17:08:28 +0100905 if (pcm)
906 snd_pcm_stop_xrun(pcm);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100907}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900908EXPORT_SYMBOL(amdtp_stream_pcm_abort);