blob: 024ab7fa84c9f5315f921354143cc84d438073cd [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
40#define CIP_DBC_MASK 0x000000ff
41#define CIP_FMT_SHIFT 24
Takashi Sakamotob445db42014-04-25 22:44:43 +090042#define CIP_FMT_MASK 0x3f000000
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090043#define CIP_FDF_MASK 0x00ff0000
44#define CIP_FDF_SHIFT 16
Takashi Sakamotob445db42014-04-25 22:44:43 +090045#define CIP_SYT_MASK 0x0000ffff
46#define CIP_SYT_NO_INFO 0xffff
Takashi Sakamotob445db42014-04-25 22:44:43 +090047
Takashi Sakamoto51c29fd2015-09-19 11:21:56 +090048/* Audio and Music transfer protocol specific parameters */
Takashi Sakamoto414ba022015-09-19 11:21:53 +090049#define CIP_FMT_AM 0x10
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090050#define AMDTP_FDF_NO_DATA 0xff
Clemens Ladisch31ef9132011-03-15 07:53:21 +010051
52/* TODO: make these configurable */
53#define INTERRUPT_INTERVAL 16
54#define QUEUE_LENGTH 48
55
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090056#define IN_PACKET_HEADER_SIZE 4
Takashi Sakamoto4b7da112014-04-25 22:44:45 +090057#define OUT_PACKET_HEADER_SIZE 0
58
Clemens Ladisch76fb8782012-05-13 22:03:09 +020059static void pcm_period_tasklet(unsigned long data);
60
Clemens Ladisch31ef9132011-03-15 07:53:21 +010061/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090062 * amdtp_stream_init - initialize an AMDTP stream structure
63 * @s: the AMDTP stream to initialize
Clemens Ladisch31ef9132011-03-15 07:53:21 +010064 * @unit: the target of the stream
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090065 * @dir: the direction of stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +010066 * @flags: the packet transmission method to use
Takashi Sakamoto59558152015-09-19 11:21:55 +090067 * @fmt: the value of fmt field in CIP header
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090068 * @process_data_blocks: callback handler to process data blocks
69 * @protocol_size: the size to allocate newly for protocol
Clemens Ladisch31ef9132011-03-15 07:53:21 +010070 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090071int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
Takashi Sakamoto59558152015-09-19 11:21:55 +090072 enum amdtp_stream_direction dir, enum cip_flags flags,
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090073 unsigned int fmt,
74 amdtp_stream_process_data_blocks_t process_data_blocks,
75 unsigned int protocol_size)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010076{
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090077 if (process_data_blocks == NULL)
78 return -EINVAL;
79
80 s->protocol = kzalloc(protocol_size, GFP_KERNEL);
81 if (!s->protocol)
82 return -ENOMEM;
83
Takashi Sakamotoc6f224d2015-02-21 23:54:58 +090084 s->unit = unit;
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090085 s->direction = dir;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010086 s->flags = flags;
87 s->context = ERR_PTR(-1);
88 mutex_init(&s->mutex);
Clemens Ladisch76fb8782012-05-13 22:03:09 +020089 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +010090 s->packet_index = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010091
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +090092 init_waitqueue_head(&s->callback_wait);
93 s->callbacked = false;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +090094
Takashi Sakamoto59558152015-09-19 11:21:55 +090095 s->fmt = fmt;
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090096 s->process_data_blocks = process_data_blocks;
Takashi Sakamoto414ba022015-09-19 11:21:53 +090097
Clemens Ladisch31ef9132011-03-15 07:53:21 +010098 return 0;
99}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900100EXPORT_SYMBOL(amdtp_stream_init);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100101
102/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900103 * amdtp_stream_destroy - free stream resources
104 * @s: the AMDTP stream to destroy
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100105 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900106void amdtp_stream_destroy(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100107{
Takashi Sakamoto44c376b2016-03-31 08:47:02 +0900108 /* Not initialized. */
109 if (s->protocol == NULL)
110 return;
111
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900112 WARN_ON(amdtp_stream_running(s));
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900113 kfree(s->protocol);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100114 mutex_destroy(&s->mutex);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100115}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900116EXPORT_SYMBOL(amdtp_stream_destroy);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100117
Clemens Ladischc5280e92011-10-16 21:39:00 +0200118const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
Clemens Ladischa7304e32011-09-04 22:16:10 +0200119 [CIP_SFC_32000] = 8,
120 [CIP_SFC_44100] = 8,
121 [CIP_SFC_48000] = 8,
122 [CIP_SFC_88200] = 16,
123 [CIP_SFC_96000] = 16,
124 [CIP_SFC_176400] = 32,
125 [CIP_SFC_192000] = 32,
126};
127EXPORT_SYMBOL(amdtp_syt_intervals);
128
Takashi Sakamotof9503a62014-05-28 00:14:36 +0900129const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900130 [CIP_SFC_32000] = 32000,
131 [CIP_SFC_44100] = 44100,
132 [CIP_SFC_48000] = 48000,
133 [CIP_SFC_88200] = 88200,
134 [CIP_SFC_96000] = 96000,
135 [CIP_SFC_176400] = 176400,
136 [CIP_SFC_192000] = 192000,
137};
138EXPORT_SYMBOL(amdtp_rate_table);
139
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100140/**
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900141 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
142 * @s: the AMDTP stream, which must be initialized.
143 * @runtime: the PCM substream runtime
144 */
145int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
146 struct snd_pcm_runtime *runtime)
147{
148 int err;
149
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900150 /*
151 * Currently firewire-lib processes 16 packets in one software
152 * interrupt callback. This equals to 2msec but actually the
153 * interval of the interrupts has a jitter.
154 * Additionally, even if adding a constraint to fit period size to
155 * 2msec, actual calculated frames per period doesn't equal to 2msec,
156 * depending on sampling rate.
157 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
158 * Here let us use 5msec for safe period interrupt.
159 */
160 err = snd_pcm_hw_constraint_minmax(runtime,
161 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
162 5000, UINT_MAX);
163 if (err < 0)
164 goto end;
165
166 /* Non-Blocking stream has no more constraints */
167 if (!(s->flags & CIP_BLOCKING))
168 goto end;
169
170 /*
171 * One AMDTP packet can include some frames. In blocking mode, the
172 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
173 * depending on its sampling rate. For accurate period interrupt, it's
Yannick Guerrinice991982015-03-09 22:13:03 +0100174 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900175 *
Yannick Guerrinice991982015-03-09 22:13:03 +0100176 * TODO: These constraints can be improved with proper rules.
177 * Currently apply LCM of SYT_INTERVALs.
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900178 */
179 err = snd_pcm_hw_constraint_step(runtime, 0,
180 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
181 if (err < 0)
182 goto end;
183 err = snd_pcm_hw_constraint_step(runtime, 0,
184 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
185end:
186 return err;
187}
188EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
189
190/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900191 * amdtp_stream_set_parameters - set stream parameters
192 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100193 * @rate: the sample rate
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900194 * @data_block_quadlets: the size of a data block in quadlet unit
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100195 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200196 * The parameters must be set before the stream is started, and must not be
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100197 * changed while the stream is running.
198 */
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900199int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
200 unsigned int data_block_quadlets)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100201{
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900202 unsigned int sfc;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100203
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900204 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900205 if (amdtp_rate_table[sfc] == rate)
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900206 break;
207 }
208 if (sfc == ARRAY_SIZE(amdtp_rate_table))
209 return -EINVAL;
Clemens Ladische84d15f2011-09-04 22:12:48 +0200210
Clemens Ladische84d15f2011-09-04 22:12:48 +0200211 s->sfc = sfc;
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900212 s->data_block_quadlets = data_block_quadlets;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200213 s->syt_interval = amdtp_syt_intervals[sfc];
Clemens Ladische84d15f2011-09-04 22:12:48 +0200214
215 /* default buffering in the device */
216 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
217 if (s->flags & CIP_BLOCKING)
218 /* additional buffering needed to adjust for no-data packets */
219 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900220
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900221 return 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100222}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900223EXPORT_SYMBOL(amdtp_stream_set_parameters);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100224
225/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900226 * amdtp_stream_get_max_payload - get the stream's packet size
227 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100228 *
229 * This function must not be called before the stream has been configured
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900230 * with amdtp_stream_set_parameters().
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100231 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900232unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100233{
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900234 unsigned int multiplier = 1;
235
236 if (s->flags & CIP_JUMBO_PAYLOAD)
237 multiplier = 5;
238
239 return 8 + s->syt_interval * s->data_block_quadlets * 4 * multiplier;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100240}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900241EXPORT_SYMBOL(amdtp_stream_get_max_payload);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100242
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200243/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900244 * amdtp_stream_pcm_prepare - prepare PCM device for running
245 * @s: the AMDTP stream
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200246 *
247 * This function should be called from the PCM device's .prepare callback.
248 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900249void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200250{
251 tasklet_kill(&s->period_tasklet);
252 s->pcm_buffer_pointer = 0;
253 s->pcm_period_pointer = 0;
254}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900255EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200256
Takashi Sakamoto875be092015-05-22 23:00:51 +0900257static unsigned int calculate_data_blocks(struct amdtp_stream *s,
258 unsigned int syt)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100259{
260 unsigned int phase, data_blocks;
261
Takashi Sakamoto875be092015-05-22 23:00:51 +0900262 /* Blocking mode. */
263 if (s->flags & CIP_BLOCKING) {
264 /* This module generate empty packet for 'no data'. */
265 if (syt == CIP_SYT_NO_INFO)
266 data_blocks = 0;
267 else
268 data_blocks = s->syt_interval;
269 /* Non-blocking mode. */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100270 } else {
Takashi Sakamoto875be092015-05-22 23:00:51 +0900271 if (!cip_sfc_is_base_44100(s->sfc)) {
272 /* Sample_rate / 8000 is an integer, and precomputed. */
273 data_blocks = s->data_block_state;
274 } else {
275 phase = s->data_block_state;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100276
277 /*
278 * This calculates the number of data blocks per packet so that
279 * 1) the overall rate is correct and exactly synchronized to
280 * the bus clock, and
281 * 2) packets with a rounded-up number of blocks occur as early
282 * as possible in the sequence (to prevent underruns of the
283 * device's buffer).
284 */
Takashi Sakamoto875be092015-05-22 23:00:51 +0900285 if (s->sfc == CIP_SFC_44100)
286 /* 6 6 5 6 5 6 5 ... */
287 data_blocks = 5 + ((phase & 1) ^
288 (phase == 0 || phase >= 40));
289 else
290 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
291 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
292 if (++phase >= (80 >> (s->sfc >> 1)))
293 phase = 0;
294 s->data_block_state = phase;
295 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100296 }
297
298 return data_blocks;
299}
300
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900301static unsigned int calculate_syt(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100302 unsigned int cycle)
303{
304 unsigned int syt_offset, phase, index, syt;
305
306 if (s->last_syt_offset < TICKS_PER_CYCLE) {
307 if (!cip_sfc_is_base_44100(s->sfc))
308 syt_offset = s->last_syt_offset + s->syt_offset_state;
309 else {
310 /*
311 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
312 * n * SYT_INTERVAL * 24576000 / sample_rate
313 * Modulo TICKS_PER_CYCLE, the difference between successive
314 * elements is about 1386.23. Rounding the results of this
315 * formula to the SYT precision results in a sequence of
316 * differences that begins with:
317 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
318 * This code generates _exactly_ the same sequence.
319 */
320 phase = s->syt_offset_state;
321 index = phase % 13;
322 syt_offset = s->last_syt_offset;
323 syt_offset += 1386 + ((index && !(index & 3)) ||
324 phase == 146);
325 if (++phase >= 147)
326 phase = 0;
327 s->syt_offset_state = phase;
328 }
329 } else
330 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
331 s->last_syt_offset = syt_offset;
332
Clemens Ladischbe454362011-03-15 07:55:02 +0100333 if (syt_offset < TICKS_PER_CYCLE) {
Clemens Ladische84d15f2011-09-04 22:12:48 +0200334 syt_offset += s->transfer_delay;
Clemens Ladischbe454362011-03-15 07:55:02 +0100335 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
336 syt += syt_offset % TICKS_PER_CYCLE;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100337
Takashi Sakamotob445db42014-04-25 22:44:43 +0900338 return syt & CIP_SYT_MASK;
Clemens Ladischbe454362011-03-15 07:55:02 +0100339 } else {
Takashi Sakamotob445db42014-04-25 22:44:43 +0900340 return CIP_SYT_NO_INFO;
Clemens Ladischbe454362011-03-15 07:55:02 +0100341 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100342}
343
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900344static void update_pcm_pointers(struct amdtp_stream *s,
345 struct snd_pcm_substream *pcm,
346 unsigned int frames)
Takashi Sakamoto65845f22014-08-29 13:40:45 +0900347{
348 unsigned int ptr;
349
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900350 ptr = s->pcm_buffer_pointer + frames;
351 if (ptr >= pcm->runtime->buffer_size)
352 ptr -= pcm->runtime->buffer_size;
353 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
354
355 s->pcm_period_pointer += frames;
356 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
357 s->pcm_period_pointer -= pcm->runtime->period_size;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900358 tasklet_hi_schedule(&s->period_tasklet);
359 }
360}
361
362static void pcm_period_tasklet(unsigned long data)
363{
364 struct amdtp_stream *s = (void *)data;
365 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
366
367 if (pcm)
368 snd_pcm_period_elapsed(pcm);
369}
370
371static int queue_packet(struct amdtp_stream *s,
372 unsigned int header_length,
373 unsigned int payload_length, bool skip)
374{
375 struct fw_iso_packet p = {0};
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900376 int err = 0;
377
378 if (IS_ERR(s->context))
379 goto end;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900380
381 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
382 p.tag = TAG_CIP;
383 p.header_length = header_length;
384 p.payload_length = (!skip) ? payload_length : 0;
385 p.skip = skip;
386 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
387 s->buffer.packets[s->packet_index].offset);
388 if (err < 0) {
389 dev_err(&s->unit->device, "queueing error: %d\n", err);
390 goto end;
391 }
392
393 if (++s->packet_index >= QUEUE_LENGTH)
394 s->packet_index = 0;
395end:
396 return err;
397}
398
399static inline int queue_out_packet(struct amdtp_stream *s,
400 unsigned int payload_length, bool skip)
401{
402 return queue_packet(s, OUT_PACKET_HEADER_SIZE,
403 payload_length, skip);
404}
405
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900406static inline int queue_in_packet(struct amdtp_stream *s)
407{
408 return queue_packet(s, IN_PACKET_HEADER_SIZE,
409 amdtp_stream_get_max_payload(s), false);
410}
411
Takashi Sakamoto390a1512016-05-09 23:15:55 +0900412static int handle_out_packet(struct amdtp_stream *s, unsigned int cycle)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100413{
414 __be32 *buffer;
Takashi Sakamoto390a1512016-05-09 23:15:55 +0900415 unsigned int syt;
416 unsigned int data_blocks;
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900417 unsigned int payload_length;
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900418 unsigned int pcm_frames;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100419 struct snd_pcm_substream *pcm;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100420
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900421 buffer = s->buffer.packets[s->packet_index].buffer;
Takashi Sakamoto390a1512016-05-09 23:15:55 +0900422 syt = calculate_syt(s, cycle);
423 data_blocks = calculate_data_blocks(s, syt);
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900424 pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900425
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100426 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900427 (s->data_block_quadlets << CIP_DBS_SHIFT) |
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100428 s->data_block_counter);
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900429 buffer[1] = cpu_to_be32(CIP_EOH |
430 ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
431 ((s->fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
432 (syt & CIP_SYT_MASK));
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100433
434 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900435 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
Takashi Sakamoto0c95c1d2016-05-09 21:12:46 +0900436
437 trace_out_packet(s, cycle, buffer, payload_length);
438
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900439 if (queue_out_packet(s, payload_length, false) < 0)
440 return -EIO;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100441
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900442 pcm = ACCESS_ONCE(s->pcm);
443 if (pcm && pcm_frames > 0)
444 update_pcm_pointers(s, pcm, pcm_frames);
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900445
446 /* No need to return the number of handled data blocks. */
447 return 0;
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200448}
449
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900450static int handle_in_packet(struct amdtp_stream *s,
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900451 unsigned int payload_quadlets, unsigned int cycle)
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900452{
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900453 __be32 *buffer;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900454 u32 cip_header[2];
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900455 unsigned int fmt, fdf, syt;
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900456 unsigned int data_block_quadlets, data_block_counter, dbc_interval;
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900457 unsigned int data_blocks;
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900458 struct snd_pcm_substream *pcm;
459 unsigned int pcm_frames;
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900460 bool lost;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900461
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900462 buffer = s->buffer.packets[s->packet_index].buffer;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900463 cip_header[0] = be32_to_cpu(buffer[0]);
464 cip_header[1] = be32_to_cpu(buffer[1]);
465
Takashi Sakamoto0c95c1d2016-05-09 21:12:46 +0900466 trace_in_packet(s, cycle, cip_header, payload_quadlets);
467
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900468 /*
469 * This module supports 'Two-quadlet CIP header with SYT field'.
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900470 * For convenience, also check FMT field is AM824 or not.
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900471 */
472 if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900473 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900474 dev_info_ratelimited(&s->unit->device,
475 "Invalid CIP header for AMDTP: %08X:%08X\n",
476 cip_header[0], cip_header[1]);
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900477 data_blocks = 0;
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900478 pcm_frames = 0;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900479 goto end;
480 }
481
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900482 /* Check valid protocol or not. */
483 fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
484 if (fmt != s->fmt) {
Takashi Sakamoto2a7e1712015-10-11 22:33:50 +0900485 dev_info_ratelimited(&s->unit->device,
486 "Detect unexpected protocol: %08x %08x\n",
487 cip_header[0], cip_header[1]);
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900488 data_blocks = 0;
Takashi Sakamoto2a7e1712015-10-11 22:33:50 +0900489 pcm_frames = 0;
490 goto end;
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900491 }
492
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900493 /* Calculate data blocks */
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900494 fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900495 if (payload_quadlets < 3 ||
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900496 (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900497 data_blocks = 0;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900498 } else {
499 data_block_quadlets =
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900500 (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900501 /* avoid division by zero */
502 if (data_block_quadlets == 0) {
Takashi Sakamoto12e0f432015-05-22 23:21:13 +0900503 dev_err(&s->unit->device,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900504 "Detect invalid value in dbs field: %08X\n",
505 cip_header[0]);
Takashi Sakamotoa9007052015-05-22 23:21:14 +0900506 return -EPROTO;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900507 }
Takashi Sakamoto69702232014-04-25 22:45:05 +0900508 if (s->flags & CIP_WRONG_DBS)
509 data_block_quadlets = s->data_block_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900510
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900511 data_blocks = (payload_quadlets - 2) / data_block_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900512 }
513
514 /* Check data block counter continuity */
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900515 data_block_counter = cip_header[0] & CIP_DBC_MASK;
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900516 if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
Takashi Sakamoto9d591242014-04-25 22:45:27 +0900517 s->data_block_counter != UINT_MAX)
518 data_block_counter = s->data_block_counter;
519
Takashi Sakamoto18f5ed32015-08-05 09:21:05 +0900520 if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) &&
521 data_block_counter == s->tx_first_dbc) ||
522 s->data_block_counter == UINT_MAX) {
Takashi Sakamotob84b1a22014-04-25 22:45:07 +0900523 lost = false;
524 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900525 lost = data_block_counter != s->data_block_counter;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900526 } else {
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900527 if (data_blocks > 0 && s->tx_dbc_interval > 0)
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900528 dbc_interval = s->tx_dbc_interval;
529 else
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900530 dbc_interval = data_blocks;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900531
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900532 lost = data_block_counter !=
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900533 ((s->data_block_counter + dbc_interval) & 0xff);
534 }
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900535
536 if (lost) {
Takashi Sakamoto12e0f432015-05-22 23:21:13 +0900537 dev_err(&s->unit->device,
538 "Detect discontinuity of CIP: %02X %02X\n",
539 s->data_block_counter, data_block_counter);
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900540 return -EIO;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900541 }
542
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900543 syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
544 pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900545
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900546 if (s->flags & CIP_DBC_IS_END_EVENT)
547 s->data_block_counter = data_block_counter;
548 else
549 s->data_block_counter =
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900550 (data_block_counter + data_blocks) & 0xff;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900551end:
552 if (queue_in_packet(s) < 0)
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900553 return -EIO;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900554
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900555 pcm = ACCESS_ONCE(s->pcm);
556 if (pcm && pcm_frames > 0)
557 update_pcm_pointers(s, pcm, pcm_frames);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900558
Takashi Sakamoto31ea49b2015-05-28 00:02:59 +0900559 return 0;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900560}
561
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900562/*
563 * In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
564 * the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
565 * it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
566 */
567static inline u32 compute_cycle_count(u32 tstamp)
568{
569 return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff);
570}
571
572static inline u32 increment_cycle_count(u32 cycle, unsigned int addend)
573{
574 cycle += addend;
575 if (cycle >= 8 * CYCLES_PER_SECOND)
576 cycle -= 8 * CYCLES_PER_SECOND;
577 return cycle;
578}
579
Takashi Sakamotof90e2de2016-05-09 21:12:45 +0900580static inline u32 decrement_cycle_count(u32 cycle, unsigned int subtrahend)
581{
582 if (cycle < subtrahend)
583 cycle += 8 * CYCLES_PER_SECOND;
584 return cycle - subtrahend;
585}
586
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900587static void out_stream_callback(struct fw_iso_context *context, u32 tstamp,
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900588 size_t header_length, void *header,
589 void *private_data)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100590{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900591 struct amdtp_stream *s = private_data;
Takashi Sakamoto390a1512016-05-09 23:15:55 +0900592 unsigned int i, packets = header_length / 4;
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900593 u32 cycle;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100594
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900595 if (s->packet_index < 0)
596 return;
597
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900598 cycle = compute_cycle_count(tstamp);
599
600 /* Align to actual cycle count for the last packet. */
601 cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100602
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900603 for (i = 0; i < packets; ++i) {
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900604 cycle = increment_cycle_count(cycle, 1);
Takashi Sakamoto390a1512016-05-09 23:15:55 +0900605 if (handle_out_packet(s, cycle) < 0) {
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900606 s->packet_index = -1;
607 amdtp_stream_pcm_abort(s);
608 return;
609 }
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900610 }
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900611
Clemens Ladisch13882a82011-05-02 09:33:56 +0200612 fw_iso_context_queue_flush(s->context);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100613}
614
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900615static void in_stream_callback(struct fw_iso_context *context, u32 tstamp,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900616 size_t header_length, void *header,
617 void *private_data)
618{
619 struct amdtp_stream *s = private_data;
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900620 unsigned int i, packets;
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900621 unsigned int payload_quadlets, max_payload_quadlets;
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900622 __be32 *headers = header;
Takashi Sakamotof90e2de2016-05-09 21:12:45 +0900623 u32 cycle;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900624
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900625 if (s->packet_index < 0)
626 return;
627
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900628 /* The number of packets in buffer */
629 packets = header_length / IN_PACKET_HEADER_SIZE;
630
Takashi Sakamotof90e2de2016-05-09 21:12:45 +0900631 cycle = compute_cycle_count(tstamp);
632
633 /* Align to actual cycle count for the last packet. */
634 cycle = decrement_cycle_count(cycle, packets);
635
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900636 /* For buffer-over-run prevention. */
637 max_payload_quadlets = amdtp_stream_get_max_payload(s) / 4;
638
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900639 for (i = 0; i < packets; i++) {
Takashi Sakamotof90e2de2016-05-09 21:12:45 +0900640 cycle = increment_cycle_count(cycle, 1);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900641
642 /* The number of quadlets in this packet */
643 payload_quadlets =
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900644 (be32_to_cpu(headers[i]) >> ISO_DATA_LENGTH_SHIFT) / 4;
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900645 if (payload_quadlets > max_payload_quadlets) {
646 dev_err(&s->unit->device,
647 "Detect jumbo payload: %02x %02x\n",
648 payload_quadlets, max_payload_quadlets);
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900649 break;
650 }
651
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900652 if (handle_in_packet(s, payload_quadlets, cycle) < 0)
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900653 break;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900654 }
655
Takashi Sakamotodec63cc2016-05-09 23:15:53 +0900656 /* Queueing error or detecting invalid payload. */
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900657 if (i < packets) {
Takashi Sakamotodec63cc2016-05-09 23:15:53 +0900658 s->packet_index = -1;
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900659 amdtp_stream_pcm_abort(s);
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900660 return;
661 }
662
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900663 fw_iso_context_queue_flush(s->context);
664}
665
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900666/* this is executed one time */
667static void amdtp_stream_first_callback(struct fw_iso_context *context,
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900668 u32 tstamp, size_t header_length,
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900669 void *header, void *private_data)
670{
671 struct amdtp_stream *s = private_data;
672
673 /*
674 * For in-stream, first packet has come.
675 * For out-stream, prepared to transmit first packet
676 */
677 s->callbacked = true;
678 wake_up(&s->callback_wait);
679
680 if (s->direction == AMDTP_IN_STREAM)
681 context->callback.sc = in_stream_callback;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900682 else
683 context->callback.sc = out_stream_callback;
684
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900685 context->callback.sc(context, tstamp, header_length, header, s);
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900686}
687
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100688/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900689 * amdtp_stream_start - start transferring packets
690 * @s: the AMDTP stream to start
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100691 * @channel: the isochronous channel on the bus
692 * @speed: firewire speed code
693 *
694 * The stream cannot be started until it has been configured with
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900695 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
696 * device can be started.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100697 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900698int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100699{
700 static const struct {
701 unsigned int data_block;
702 unsigned int syt_offset;
703 } initial_state[] = {
704 [CIP_SFC_32000] = { 4, 3072 },
705 [CIP_SFC_48000] = { 6, 1024 },
706 [CIP_SFC_96000] = { 12, 1024 },
707 [CIP_SFC_192000] = { 24, 1024 },
708 [CIP_SFC_44100] = { 0, 67 },
709 [CIP_SFC_88200] = { 0, 67 },
710 [CIP_SFC_176400] = { 0, 67 },
711 };
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900712 unsigned int header_size;
713 enum dma_data_direction dir;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900714 int type, tag, err;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100715
716 mutex_lock(&s->mutex);
717
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900718 if (WARN_ON(amdtp_stream_running(s) ||
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900719 (s->data_block_quadlets < 1))) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100720 err = -EBADFD;
721 goto err_unlock;
722 }
723
Takashi Sakamoto62f00e42016-05-09 23:15:56 +0900724 if (s->direction == AMDTP_IN_STREAM)
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900725 s->data_block_counter = UINT_MAX;
726 else
727 s->data_block_counter = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100728 s->data_block_state = initial_state[s->sfc].data_block;
729 s->syt_offset_state = initial_state[s->sfc].syt_offset;
730 s->last_syt_offset = TICKS_PER_CYCLE;
731
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900732 /* initialize packet buffer */
733 if (s->direction == AMDTP_IN_STREAM) {
734 dir = DMA_FROM_DEVICE;
735 type = FW_ISO_CONTEXT_RECEIVE;
736 header_size = IN_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900737 } else {
738 dir = DMA_TO_DEVICE;
739 type = FW_ISO_CONTEXT_TRANSMIT;
740 header_size = OUT_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900741 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100742 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900743 amdtp_stream_get_max_payload(s), dir);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100744 if (err < 0)
745 goto err_unlock;
746
747 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900748 type, channel, speed, header_size,
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900749 amdtp_stream_first_callback, s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100750 if (IS_ERR(s->context)) {
751 err = PTR_ERR(s->context);
752 if (err == -EBUSY)
753 dev_err(&s->unit->device,
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900754 "no free stream on this controller\n");
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100755 goto err_buffer;
756 }
757
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900758 amdtp_stream_update(s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100759
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100760 s->packet_index = 0;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900761 do {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900762 if (s->direction == AMDTP_IN_STREAM)
763 err = queue_in_packet(s);
764 else
765 err = queue_out_packet(s, 0, true);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900766 if (err < 0)
767 goto err_context;
768 } while (s->packet_index > 0);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100769
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900770 /* NOTE: TAG1 matches CIP. This just affects in stream. */
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900771 tag = FW_ISO_CONTEXT_MATCH_TAG1;
772 if (s->flags & CIP_EMPTY_WITH_TAG0)
773 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
774
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900775 s->callbacked = false;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900776 err = fw_iso_context_start(s->context, -1, 0, tag);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100777 if (err < 0)
778 goto err_context;
779
780 mutex_unlock(&s->mutex);
781
782 return 0;
783
784err_context:
785 fw_iso_context_destroy(s->context);
786 s->context = ERR_PTR(-1);
787err_buffer:
788 iso_packets_buffer_destroy(&s->buffer, s->unit);
789err_unlock:
790 mutex_unlock(&s->mutex);
791
792 return err;
793}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900794EXPORT_SYMBOL(amdtp_stream_start);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100795
796/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900797 * amdtp_stream_pcm_pointer - get the PCM buffer position
798 * @s: the AMDTP stream that transports the PCM data
Clemens Ladische9148dd2012-05-13 18:49:14 +0200799 *
800 * Returns the current buffer position, in frames.
801 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900802unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
Clemens Ladische9148dd2012-05-13 18:49:14 +0200803{
Takashi Sakamoto1dba9db2016-05-12 02:17:39 +0900804 /*
805 * This function is called in software IRQ context of period_tasklet or
806 * process context.
807 *
808 * When the software IRQ context was scheduled by software IRQ context
809 * of IR/IT contexts, queued packets were already handled. Therefore,
810 * no need to flush the queue in buffer anymore.
811 *
812 * When the process context reach here, some packets will be already
813 * queued in the buffer. These packets should be handled immediately
814 * to keep better granularity of PCM pointer.
815 *
816 * Later, the process context will sometimes schedules software IRQ
817 * context of the period_tasklet. Then, no need to flush the queue by
818 * the same reason as described for IR/IT contexts.
819 */
820 if (!in_interrupt() && amdtp_stream_running(s))
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200821 fw_iso_context_flush_completions(s->context);
Clemens Ladische9148dd2012-05-13 18:49:14 +0200822
823 return ACCESS_ONCE(s->pcm_buffer_pointer);
824}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900825EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
Clemens Ladische9148dd2012-05-13 18:49:14 +0200826
827/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900828 * amdtp_stream_update - update the stream after a bus reset
829 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100830 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900831void amdtp_stream_update(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100832{
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900833 /* Precomputing. */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100834 ACCESS_ONCE(s->source_node_id_field) =
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900835 (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) &
836 CIP_SID_MASK;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100837}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900838EXPORT_SYMBOL(amdtp_stream_update);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100839
840/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900841 * amdtp_stream_stop - stop sending packets
842 * @s: the AMDTP stream to stop
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100843 *
844 * All PCM and MIDI devices of the stream must be stopped before the stream
845 * itself can be stopped.
846 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900847void amdtp_stream_stop(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100848{
849 mutex_lock(&s->mutex);
850
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900851 if (!amdtp_stream_running(s)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100852 mutex_unlock(&s->mutex);
853 return;
854 }
855
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200856 tasklet_kill(&s->period_tasklet);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100857 fw_iso_context_stop(s->context);
858 fw_iso_context_destroy(s->context);
859 s->context = ERR_PTR(-1);
860 iso_packets_buffer_destroy(&s->buffer, s->unit);
861
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900862 s->callbacked = false;
863
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100864 mutex_unlock(&s->mutex);
865}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900866EXPORT_SYMBOL(amdtp_stream_stop);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100867
868/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900869 * amdtp_stream_pcm_abort - abort the running PCM device
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100870 * @s: the AMDTP stream about to be stopped
871 *
872 * If the isochronous stream needs to be stopped asynchronously, call this
873 * function first to stop the PCM device.
874 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900875void amdtp_stream_pcm_abort(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100876{
877 struct snd_pcm_substream *pcm;
878
879 pcm = ACCESS_ONCE(s->pcm);
Takashi Iwai1fb85102014-11-07 17:08:28 +0100880 if (pcm)
881 snd_pcm_stop_xrun(pcm);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100882}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900883EXPORT_SYMBOL(amdtp_stream_pcm_abort);