blob: a03b37bdc27426515c24e83b863cc0a376bd9c70 [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
Takashi Sakamoto3b196c392017-03-31 22:06:07 +090030#define TAG_NO_CIP_HEADER 0
Clemens Ladisch31ef9132011-03-15 07:53:21 +010031#define TAG_CIP 1
32
Takashi Sakamotob445db42014-04-25 22:44:43 +090033/* common isochronous packet header parameters */
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090034#define CIP_EOH_SHIFT 31
35#define CIP_EOH (1u << CIP_EOH_SHIFT)
Takashi Sakamotob445db42014-04-25 22:44:43 +090036#define CIP_EOH_MASK 0x80000000
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090037#define CIP_SID_SHIFT 24
38#define CIP_SID_MASK 0x3f000000
39#define CIP_DBS_MASK 0x00ff0000
40#define CIP_DBS_SHIFT 16
Takashi Sakamoto98638742017-03-22 21:30:16 +090041#define CIP_SPH_MASK 0x00000400
42#define CIP_SPH_SHIFT 10
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090043#define CIP_DBC_MASK 0x000000ff
44#define CIP_FMT_SHIFT 24
Takashi Sakamotob445db42014-04-25 22:44:43 +090045#define CIP_FMT_MASK 0x3f000000
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +090046#define CIP_FDF_MASK 0x00ff0000
47#define CIP_FDF_SHIFT 16
Takashi Sakamotob445db42014-04-25 22:44:43 +090048#define CIP_SYT_MASK 0x0000ffff
49#define CIP_SYT_NO_INFO 0xffff
Takashi Sakamotob445db42014-04-25 22:44:43 +090050
Takashi Sakamoto51c29fd2015-09-19 11:21:56 +090051/* Audio and Music transfer protocol specific parameters */
Takashi Sakamoto414ba022015-09-19 11:21:53 +090052#define CIP_FMT_AM 0x10
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090053#define AMDTP_FDF_NO_DATA 0xff
Clemens Ladisch31ef9132011-03-15 07:53:21 +010054
55/* TODO: make these configurable */
56#define INTERRUPT_INTERVAL 16
57#define QUEUE_LENGTH 48
58
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090059#define IN_PACKET_HEADER_SIZE 4
Takashi Sakamoto4b7da112014-04-25 22:44:45 +090060#define OUT_PACKET_HEADER_SIZE 0
61
Clemens Ladisch76fb8782012-05-13 22:03:09 +020062static void pcm_period_tasklet(unsigned long data);
63
Clemens Ladisch31ef9132011-03-15 07:53:21 +010064/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090065 * amdtp_stream_init - initialize an AMDTP stream structure
66 * @s: the AMDTP stream to initialize
Clemens Ladisch31ef9132011-03-15 07:53:21 +010067 * @unit: the target of the stream
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090068 * @dir: the direction of stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +010069 * @flags: the packet transmission method to use
Takashi Sakamoto59558152015-09-19 11:21:55 +090070 * @fmt: the value of fmt field in CIP header
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090071 * @process_data_blocks: callback handler to process data blocks
72 * @protocol_size: the size to allocate newly for protocol
Clemens Ladisch31ef9132011-03-15 07:53:21 +010073 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090074int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
Takashi Sakamoto59558152015-09-19 11:21:55 +090075 enum amdtp_stream_direction dir, enum cip_flags flags,
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090076 unsigned int fmt,
77 amdtp_stream_process_data_blocks_t process_data_blocks,
78 unsigned int protocol_size)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010079{
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090080 if (process_data_blocks == NULL)
81 return -EINVAL;
82
83 s->protocol = kzalloc(protocol_size, GFP_KERNEL);
84 if (!s->protocol)
85 return -ENOMEM;
86
Takashi Sakamotoc6f224d2015-02-21 23:54:58 +090087 s->unit = unit;
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090088 s->direction = dir;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010089 s->flags = flags;
90 s->context = ERR_PTR(-1);
91 mutex_init(&s->mutex);
Clemens Ladisch76fb8782012-05-13 22:03:09 +020092 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +010093 s->packet_index = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010094
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +090095 init_waitqueue_head(&s->callback_wait);
96 s->callbacked = false;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +090097
Takashi Sakamoto59558152015-09-19 11:21:55 +090098 s->fmt = fmt;
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090099 s->process_data_blocks = process_data_blocks;
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900100
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100101 return 0;
102}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900103EXPORT_SYMBOL(amdtp_stream_init);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100104
105/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900106 * amdtp_stream_destroy - free stream resources
107 * @s: the AMDTP stream to destroy
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100108 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900109void amdtp_stream_destroy(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100110{
Takashi Sakamoto44c376b2016-03-31 08:47:02 +0900111 /* Not initialized. */
112 if (s->protocol == NULL)
113 return;
114
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900115 WARN_ON(amdtp_stream_running(s));
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900116 kfree(s->protocol);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100117 mutex_destroy(&s->mutex);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100118}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900119EXPORT_SYMBOL(amdtp_stream_destroy);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100120
Clemens Ladischc5280e92011-10-16 21:39:00 +0200121const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
Clemens Ladischa7304e32011-09-04 22:16:10 +0200122 [CIP_SFC_32000] = 8,
123 [CIP_SFC_44100] = 8,
124 [CIP_SFC_48000] = 8,
125 [CIP_SFC_88200] = 16,
126 [CIP_SFC_96000] = 16,
127 [CIP_SFC_176400] = 32,
128 [CIP_SFC_192000] = 32,
129};
130EXPORT_SYMBOL(amdtp_syt_intervals);
131
Takashi Sakamotof9503a62014-05-28 00:14:36 +0900132const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900133 [CIP_SFC_32000] = 32000,
134 [CIP_SFC_44100] = 44100,
135 [CIP_SFC_48000] = 48000,
136 [CIP_SFC_88200] = 88200,
137 [CIP_SFC_96000] = 96000,
138 [CIP_SFC_176400] = 176400,
139 [CIP_SFC_192000] = 192000,
140};
141EXPORT_SYMBOL(amdtp_rate_table);
142
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100143/**
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900144 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
145 * @s: the AMDTP stream, which must be initialized.
146 * @runtime: the PCM substream runtime
147 */
148int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
149 struct snd_pcm_runtime *runtime)
150{
151 int err;
152
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900153 /*
154 * Currently firewire-lib processes 16 packets in one software
155 * interrupt callback. This equals to 2msec but actually the
156 * interval of the interrupts has a jitter.
157 * Additionally, even if adding a constraint to fit period size to
158 * 2msec, actual calculated frames per period doesn't equal to 2msec,
159 * depending on sampling rate.
160 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
161 * Here let us use 5msec for safe period interrupt.
162 */
163 err = snd_pcm_hw_constraint_minmax(runtime,
164 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
165 5000, UINT_MAX);
166 if (err < 0)
167 goto end;
168
169 /* Non-Blocking stream has no more constraints */
170 if (!(s->flags & CIP_BLOCKING))
171 goto end;
172
173 /*
174 * One AMDTP packet can include some frames. In blocking mode, the
175 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
176 * depending on its sampling rate. For accurate period interrupt, it's
Yannick Guerrinice991982015-03-09 22:13:03 +0100177 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900178 *
Yannick Guerrinice991982015-03-09 22:13:03 +0100179 * TODO: These constraints can be improved with proper rules.
180 * Currently apply LCM of SYT_INTERVALs.
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900181 */
182 err = snd_pcm_hw_constraint_step(runtime, 0,
183 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
184 if (err < 0)
185 goto end;
186 err = snd_pcm_hw_constraint_step(runtime, 0,
187 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
188end:
189 return err;
190}
191EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
192
193/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900194 * amdtp_stream_set_parameters - set stream parameters
195 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100196 * @rate: the sample rate
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900197 * @data_block_quadlets: the size of a data block in quadlet unit
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100198 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200199 * The parameters must be set before the stream is started, and must not be
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100200 * changed while the stream is running.
201 */
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900202int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
203 unsigned int data_block_quadlets)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100204{
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900205 unsigned int sfc;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100206
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900207 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900208 if (amdtp_rate_table[sfc] == rate)
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900209 break;
210 }
211 if (sfc == ARRAY_SIZE(amdtp_rate_table))
212 return -EINVAL;
Clemens Ladische84d15f2011-09-04 22:12:48 +0200213
Clemens Ladische84d15f2011-09-04 22:12:48 +0200214 s->sfc = sfc;
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900215 s->data_block_quadlets = data_block_quadlets;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200216 s->syt_interval = amdtp_syt_intervals[sfc];
Clemens Ladische84d15f2011-09-04 22:12:48 +0200217
218 /* default buffering in the device */
219 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
220 if (s->flags & CIP_BLOCKING)
221 /* additional buffering needed to adjust for no-data packets */
222 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900223
Takashi Sakamoto547e6312015-09-19 11:21:49 +0900224 return 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100225}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900226EXPORT_SYMBOL(amdtp_stream_set_parameters);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100227
228/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900229 * amdtp_stream_get_max_payload - get the stream's packet size
230 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100231 *
232 * This function must not be called before the stream has been configured
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900233 * with amdtp_stream_set_parameters().
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100234 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900235unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100236{
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900237 unsigned int multiplier = 1;
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900238 unsigned int header_size = 0;
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900239
240 if (s->flags & CIP_JUMBO_PAYLOAD)
241 multiplier = 5;
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900242 if (!(s->flags & CIP_NO_HEADER))
243 header_size = 8;
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900244
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900245 return header_size +
246 s->syt_interval * s->data_block_quadlets * 4 * multiplier;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100247}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900248EXPORT_SYMBOL(amdtp_stream_get_max_payload);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100249
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200250/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900251 * amdtp_stream_pcm_prepare - prepare PCM device for running
252 * @s: the AMDTP stream
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200253 *
254 * This function should be called from the PCM device's .prepare callback.
255 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900256void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200257{
258 tasklet_kill(&s->period_tasklet);
259 s->pcm_buffer_pointer = 0;
260 s->pcm_period_pointer = 0;
261}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900262EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200263
Takashi Sakamoto875be092015-05-22 23:00:51 +0900264static unsigned int calculate_data_blocks(struct amdtp_stream *s,
265 unsigned int syt)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100266{
267 unsigned int phase, data_blocks;
268
Takashi Sakamoto875be092015-05-22 23:00:51 +0900269 /* Blocking mode. */
270 if (s->flags & CIP_BLOCKING) {
271 /* This module generate empty packet for 'no data'. */
272 if (syt == CIP_SYT_NO_INFO)
273 data_blocks = 0;
274 else
275 data_blocks = s->syt_interval;
276 /* Non-blocking mode. */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100277 } else {
Takashi Sakamoto875be092015-05-22 23:00:51 +0900278 if (!cip_sfc_is_base_44100(s->sfc)) {
279 /* Sample_rate / 8000 is an integer, and precomputed. */
280 data_blocks = s->data_block_state;
281 } else {
282 phase = s->data_block_state;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100283
284 /*
285 * This calculates the number of data blocks per packet so that
286 * 1) the overall rate is correct and exactly synchronized to
287 * the bus clock, and
288 * 2) packets with a rounded-up number of blocks occur as early
289 * as possible in the sequence (to prevent underruns of the
290 * device's buffer).
291 */
Takashi Sakamoto875be092015-05-22 23:00:51 +0900292 if (s->sfc == CIP_SFC_44100)
293 /* 6 6 5 6 5 6 5 ... */
294 data_blocks = 5 + ((phase & 1) ^
295 (phase == 0 || phase >= 40));
296 else
297 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
298 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
299 if (++phase >= (80 >> (s->sfc >> 1)))
300 phase = 0;
301 s->data_block_state = phase;
302 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100303 }
304
305 return data_blocks;
306}
307
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900308static unsigned int calculate_syt(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100309 unsigned int cycle)
310{
311 unsigned int syt_offset, phase, index, syt;
312
313 if (s->last_syt_offset < TICKS_PER_CYCLE) {
314 if (!cip_sfc_is_base_44100(s->sfc))
315 syt_offset = s->last_syt_offset + s->syt_offset_state;
316 else {
317 /*
318 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
319 * n * SYT_INTERVAL * 24576000 / sample_rate
320 * Modulo TICKS_PER_CYCLE, the difference between successive
321 * elements is about 1386.23. Rounding the results of this
322 * formula to the SYT precision results in a sequence of
323 * differences that begins with:
324 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
325 * This code generates _exactly_ the same sequence.
326 */
327 phase = s->syt_offset_state;
328 index = phase % 13;
329 syt_offset = s->last_syt_offset;
330 syt_offset += 1386 + ((index && !(index & 3)) ||
331 phase == 146);
332 if (++phase >= 147)
333 phase = 0;
334 s->syt_offset_state = phase;
335 }
336 } else
337 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
338 s->last_syt_offset = syt_offset;
339
Clemens Ladischbe454362011-03-15 07:55:02 +0100340 if (syt_offset < TICKS_PER_CYCLE) {
Clemens Ladische84d15f2011-09-04 22:12:48 +0200341 syt_offset += s->transfer_delay;
Clemens Ladischbe454362011-03-15 07:55:02 +0100342 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
343 syt += syt_offset % TICKS_PER_CYCLE;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100344
Takashi Sakamotob445db42014-04-25 22:44:43 +0900345 return syt & CIP_SYT_MASK;
Clemens Ladischbe454362011-03-15 07:55:02 +0100346 } else {
Takashi Sakamotob445db42014-04-25 22:44:43 +0900347 return CIP_SYT_NO_INFO;
Clemens Ladischbe454362011-03-15 07:55:02 +0100348 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100349}
350
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900351static void update_pcm_pointers(struct amdtp_stream *s,
352 struct snd_pcm_substream *pcm,
353 unsigned int frames)
Takashi Sakamoto65845f22014-08-29 13:40:45 +0900354{
355 unsigned int ptr;
356
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900357 ptr = s->pcm_buffer_pointer + frames;
358 if (ptr >= pcm->runtime->buffer_size)
359 ptr -= pcm->runtime->buffer_size;
360 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
361
362 s->pcm_period_pointer += frames;
363 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
364 s->pcm_period_pointer -= pcm->runtime->period_size;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900365 tasklet_hi_schedule(&s->period_tasklet);
366 }
367}
368
369static void pcm_period_tasklet(unsigned long data)
370{
371 struct amdtp_stream *s = (void *)data;
372 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
373
374 if (pcm)
375 snd_pcm_period_elapsed(pcm);
376}
377
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900378static int queue_packet(struct amdtp_stream *s, unsigned int header_length,
379 unsigned int payload_length)
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900380{
381 struct fw_iso_packet p = {0};
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900382 int err = 0;
383
384 if (IS_ERR(s->context))
385 goto end;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900386
387 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900388 p.tag = s->tag;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900389 p.header_length = header_length;
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900390 if (payload_length > 0)
391 p.payload_length = payload_length;
392 else
393 p.skip = true;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900394 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
395 s->buffer.packets[s->packet_index].offset);
396 if (err < 0) {
397 dev_err(&s->unit->device, "queueing error: %d\n", err);
398 goto end;
399 }
400
401 if (++s->packet_index >= QUEUE_LENGTH)
402 s->packet_index = 0;
403end:
404 return err;
405}
406
407static inline int queue_out_packet(struct amdtp_stream *s,
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900408 unsigned int payload_length)
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900409{
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900410 return queue_packet(s, OUT_PACKET_HEADER_SIZE, payload_length);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900411}
412
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900413static inline int queue_in_packet(struct amdtp_stream *s)
414{
415 return queue_packet(s, IN_PACKET_HEADER_SIZE,
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900416 amdtp_stream_get_max_payload(s));
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900417}
418
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900419static int handle_out_packet(struct amdtp_stream *s,
420 unsigned int payload_length, unsigned int cycle,
Takashi Sakamotoa9c42842016-05-11 07:33:27 +0900421 unsigned int index)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100422{
423 __be32 *buffer;
Takashi Sakamoto390a1512016-05-09 23:15:55 +0900424 unsigned int syt;
425 unsigned int data_blocks;
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900426 unsigned int pcm_frames;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100427 struct snd_pcm_substream *pcm;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100428
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900429 buffer = s->buffer.packets[s->packet_index].buffer;
Takashi Sakamoto390a1512016-05-09 23:15:55 +0900430 syt = calculate_syt(s, cycle);
431 data_blocks = calculate_data_blocks(s, syt);
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900432 pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900433
Takashi Sakamoto9dae0172017-03-22 21:30:17 +0900434 if (s->flags & CIP_DBC_IS_END_EVENT)
435 s->data_block_counter =
436 (s->data_block_counter + data_blocks) & 0xff;
437
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100438 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900439 (s->data_block_quadlets << CIP_DBS_SHIFT) |
Takashi Sakamoto98638742017-03-22 21:30:16 +0900440 ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) |
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100441 s->data_block_counter);
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900442 buffer[1] = cpu_to_be32(CIP_EOH |
443 ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
444 ((s->fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
445 (syt & CIP_SYT_MASK));
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100446
Takashi Sakamoto9dae0172017-03-22 21:30:17 +0900447 if (!(s->flags & CIP_DBC_IS_END_EVENT))
448 s->data_block_counter =
449 (s->data_block_counter + data_blocks) & 0xff;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900450 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
Takashi Sakamoto0c95c1d2016-05-09 21:12:46 +0900451
Takashi Sakamotoa9c42842016-05-11 07:33:27 +0900452 trace_out_packet(s, cycle, buffer, payload_length, index);
Takashi Sakamoto0c95c1d2016-05-09 21:12:46 +0900453
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900454 if (queue_out_packet(s, payload_length) < 0)
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900455 return -EIO;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100456
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900457 pcm = ACCESS_ONCE(s->pcm);
458 if (pcm && pcm_frames > 0)
459 update_pcm_pointers(s, pcm, pcm_frames);
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900460
461 /* No need to return the number of handled data blocks. */
462 return 0;
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200463}
464
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900465static int handle_out_packet_without_header(struct amdtp_stream *s,
466 unsigned int payload_length, unsigned int cycle,
467 unsigned int index)
468{
469 __be32 *buffer;
470 unsigned int syt;
471 unsigned int data_blocks;
472 unsigned int pcm_frames;
473 struct snd_pcm_substream *pcm;
474
475 buffer = s->buffer.packets[s->packet_index].buffer;
476 syt = calculate_syt(s, cycle);
477 data_blocks = calculate_data_blocks(s, syt);
478 pcm_frames = s->process_data_blocks(s, buffer, data_blocks, &syt);
479 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
480
481 payload_length = data_blocks * 4 * s->data_block_quadlets;
482 if (queue_out_packet(s, payload_length) < 0)
483 return -EIO;
484
485 pcm = ACCESS_ONCE(s->pcm);
486 if (pcm && pcm_frames > 0)
487 update_pcm_pointers(s, pcm, pcm_frames);
488
489 /* No need to return the number of handled data blocks. */
490 return 0;
491}
492
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900493static int handle_in_packet(struct amdtp_stream *s,
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900494 unsigned int payload_length, unsigned int cycle,
Takashi Sakamotoa9c42842016-05-11 07:33:27 +0900495 unsigned int index)
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900496{
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900497 __be32 *buffer;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900498 u32 cip_header[2];
Takashi Sakamoto98638742017-03-22 21:30:16 +0900499 unsigned int sph, fmt, fdf, syt;
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900500 unsigned int data_block_quadlets, data_block_counter, dbc_interval;
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900501 unsigned int data_blocks;
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900502 struct snd_pcm_substream *pcm;
503 unsigned int pcm_frames;
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900504 bool lost;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900505
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900506 buffer = s->buffer.packets[s->packet_index].buffer;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900507 cip_header[0] = be32_to_cpu(buffer[0]);
508 cip_header[1] = be32_to_cpu(buffer[1]);
509
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900510 trace_in_packet(s, cycle, cip_header, payload_length, index);
Takashi Sakamoto0c95c1d2016-05-09 21:12:46 +0900511
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900512 /*
513 * This module supports 'Two-quadlet CIP header with SYT field'.
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900514 * For convenience, also check FMT field is AM824 or not.
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900515 */
Takashi Sakamoto2128f782017-03-22 21:30:27 +0900516 if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
517 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) &&
518 (!(s->flags & CIP_HEADER_WITHOUT_EOH))) {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900519 dev_info_ratelimited(&s->unit->device,
520 "Invalid CIP header for AMDTP: %08X:%08X\n",
521 cip_header[0], cip_header[1]);
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900522 data_blocks = 0;
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900523 pcm_frames = 0;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900524 goto end;
525 }
526
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900527 /* Check valid protocol or not. */
Takashi Sakamoto98638742017-03-22 21:30:16 +0900528 sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT;
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900529 fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
Takashi Sakamoto98638742017-03-22 21:30:16 +0900530 if (sph != s->sph || fmt != s->fmt) {
Takashi Sakamoto2a7e1712015-10-11 22:33:50 +0900531 dev_info_ratelimited(&s->unit->device,
532 "Detect unexpected protocol: %08x %08x\n",
533 cip_header[0], cip_header[1]);
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900534 data_blocks = 0;
Takashi Sakamoto2a7e1712015-10-11 22:33:50 +0900535 pcm_frames = 0;
536 goto end;
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900537 }
538
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900539 /* Calculate data blocks */
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900540 fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT;
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900541 if (payload_length < 12 ||
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900542 (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900543 data_blocks = 0;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900544 } else {
545 data_block_quadlets =
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900546 (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900547 /* avoid division by zero */
548 if (data_block_quadlets == 0) {
Takashi Sakamoto12e0f432015-05-22 23:21:13 +0900549 dev_err(&s->unit->device,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900550 "Detect invalid value in dbs field: %08X\n",
551 cip_header[0]);
Takashi Sakamotoa9007052015-05-22 23:21:14 +0900552 return -EPROTO;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900553 }
Takashi Sakamoto69702232014-04-25 22:45:05 +0900554 if (s->flags & CIP_WRONG_DBS)
555 data_block_quadlets = s->data_block_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900556
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900557 data_blocks = (payload_length / 4 - 2) /
558 data_block_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900559 }
560
561 /* Check data block counter continuity */
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900562 data_block_counter = cip_header[0] & CIP_DBC_MASK;
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900563 if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
Takashi Sakamoto9d591242014-04-25 22:45:27 +0900564 s->data_block_counter != UINT_MAX)
565 data_block_counter = s->data_block_counter;
566
Takashi Sakamoto18f5ed32015-08-05 09:21:05 +0900567 if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) &&
568 data_block_counter == s->tx_first_dbc) ||
569 s->data_block_counter == UINT_MAX) {
Takashi Sakamotob84b1a22014-04-25 22:45:07 +0900570 lost = false;
571 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900572 lost = data_block_counter != s->data_block_counter;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900573 } else {
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900574 if (data_blocks > 0 && s->tx_dbc_interval > 0)
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900575 dbc_interval = s->tx_dbc_interval;
576 else
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900577 dbc_interval = data_blocks;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900578
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900579 lost = data_block_counter !=
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900580 ((s->data_block_counter + dbc_interval) & 0xff);
581 }
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900582
583 if (lost) {
Takashi Sakamoto12e0f432015-05-22 23:21:13 +0900584 dev_err(&s->unit->device,
585 "Detect discontinuity of CIP: %02X %02X\n",
586 s->data_block_counter, data_block_counter);
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900587 return -EIO;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900588 }
589
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900590 syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
591 pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900592
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900593 if (s->flags & CIP_DBC_IS_END_EVENT)
594 s->data_block_counter = data_block_counter;
595 else
596 s->data_block_counter =
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900597 (data_block_counter + data_blocks) & 0xff;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900598end:
599 if (queue_in_packet(s) < 0)
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900600 return -EIO;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900601
Takashi Sakamoto20e44572015-09-19 11:21:52 +0900602 pcm = ACCESS_ONCE(s->pcm);
603 if (pcm && pcm_frames > 0)
604 update_pcm_pointers(s, pcm, pcm_frames);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900605
Takashi Sakamoto31ea49b2015-05-28 00:02:59 +0900606 return 0;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900607}
608
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900609static int handle_in_packet_without_header(struct amdtp_stream *s,
610 unsigned int payload_quadlets, unsigned int cycle,
611 unsigned int index)
612{
613 __be32 *buffer;
614 unsigned int data_blocks;
615 struct snd_pcm_substream *pcm;
616 unsigned int pcm_frames;
617
618 buffer = s->buffer.packets[s->packet_index].buffer;
619 data_blocks = payload_quadlets / s->data_block_quadlets;
620 pcm_frames = s->process_data_blocks(s, buffer, data_blocks, NULL);
621 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
622
623 if (queue_in_packet(s) < 0)
624 return -EIO;
625
626 pcm = ACCESS_ONCE(s->pcm);
627 if (pcm && pcm_frames > 0)
628 update_pcm_pointers(s, pcm, pcm_frames);
629
630 return 0;
631}
632
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900633/*
634 * In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
635 * the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
636 * it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
637 */
638static inline u32 compute_cycle_count(u32 tstamp)
639{
640 return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff);
641}
642
643static inline u32 increment_cycle_count(u32 cycle, unsigned int addend)
644{
645 cycle += addend;
646 if (cycle >= 8 * CYCLES_PER_SECOND)
647 cycle -= 8 * CYCLES_PER_SECOND;
648 return cycle;
649}
650
Takashi Sakamotof90e2de2016-05-09 21:12:45 +0900651static inline u32 decrement_cycle_count(u32 cycle, unsigned int subtrahend)
652{
653 if (cycle < subtrahend)
654 cycle += 8 * CYCLES_PER_SECOND;
655 return cycle - subtrahend;
656}
657
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900658static void out_stream_callback(struct fw_iso_context *context, u32 tstamp,
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900659 size_t header_length, void *header,
660 void *private_data)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100661{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900662 struct amdtp_stream *s = private_data;
Takashi Sakamoto390a1512016-05-09 23:15:55 +0900663 unsigned int i, packets = header_length / 4;
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900664 u32 cycle;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100665
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900666 if (s->packet_index < 0)
667 return;
668
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900669 cycle = compute_cycle_count(tstamp);
670
671 /* Align to actual cycle count for the last packet. */
672 cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100673
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900674 for (i = 0; i < packets; ++i) {
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900675 cycle = increment_cycle_count(cycle, 1);
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900676 if (s->handle_packet(s, 0, cycle, i) < 0) {
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900677 s->packet_index = -1;
678 amdtp_stream_pcm_abort(s);
679 return;
680 }
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900681 }
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900682
Clemens Ladisch13882a82011-05-02 09:33:56 +0200683 fw_iso_context_queue_flush(s->context);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100684}
685
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900686static void in_stream_callback(struct fw_iso_context *context, u32 tstamp,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900687 size_t header_length, void *header,
688 void *private_data)
689{
690 struct amdtp_stream *s = private_data;
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900691 unsigned int i, packets;
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900692 unsigned int payload_length, max_payload_length;
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900693 __be32 *headers = header;
Takashi Sakamotof90e2de2016-05-09 21:12:45 +0900694 u32 cycle;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900695
Takashi Sakamotoa4103bd2015-05-22 23:00:53 +0900696 if (s->packet_index < 0)
697 return;
698
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900699 /* The number of packets in buffer */
700 packets = header_length / IN_PACKET_HEADER_SIZE;
701
Takashi Sakamotof90e2de2016-05-09 21:12:45 +0900702 cycle = compute_cycle_count(tstamp);
703
704 /* Align to actual cycle count for the last packet. */
705 cycle = decrement_cycle_count(cycle, packets);
706
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900707 /* For buffer-over-run prevention. */
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900708 max_payload_length = amdtp_stream_get_max_payload(s);
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900709
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900710 for (i = 0; i < packets; i++) {
Takashi Sakamotof90e2de2016-05-09 21:12:45 +0900711 cycle = increment_cycle_count(cycle, 1);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900712
713 /* The number of quadlets in this packet */
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900714 payload_length =
715 (be32_to_cpu(headers[i]) >> ISO_DATA_LENGTH_SHIFT);
716 if (payload_length > max_payload_length) {
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900717 dev_err(&s->unit->device,
Takashi Sakamotoff0fb5a2017-03-31 22:06:06 +0900718 "Detect jumbo payload: %04x %04x\n",
719 payload_length, max_payload_length);
Takashi Sakamotoa2064712015-05-22 23:00:50 +0900720 break;
721 }
722
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900723 if (s->handle_packet(s, payload_length, cycle, i) < 0)
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900724 break;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900725 }
726
Takashi Sakamotodec63cc2016-05-09 23:15:53 +0900727 /* Queueing error or detecting invalid payload. */
Takashi Sakamotod9a16fc2016-05-09 23:15:54 +0900728 if (i < packets) {
Takashi Sakamotodec63cc2016-05-09 23:15:53 +0900729 s->packet_index = -1;
Takashi Sakamoto6fc6b9c2015-05-22 23:00:52 +0900730 amdtp_stream_pcm_abort(s);
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900731 return;
732 }
733
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900734 fw_iso_context_queue_flush(s->context);
735}
736
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900737/* this is executed one time */
738static void amdtp_stream_first_callback(struct fw_iso_context *context,
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900739 u32 tstamp, size_t header_length,
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900740 void *header, void *private_data)
741{
742 struct amdtp_stream *s = private_data;
Takashi Sakamotoa04513f2017-03-22 21:30:15 +0900743 u32 cycle;
744 unsigned int packets;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900745
746 /*
747 * For in-stream, first packet has come.
748 * For out-stream, prepared to transmit first packet
749 */
750 s->callbacked = true;
751 wake_up(&s->callback_wait);
752
Takashi Sakamotoa04513f2017-03-22 21:30:15 +0900753 cycle = compute_cycle_count(tstamp);
754
755 if (s->direction == AMDTP_IN_STREAM) {
756 packets = header_length / IN_PACKET_HEADER_SIZE;
757 cycle = decrement_cycle_count(cycle, packets);
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900758 context->callback.sc = in_stream_callback;
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900759 if (s->flags & CIP_NO_HEADER)
760 s->handle_packet = handle_in_packet_without_header;
761 else
762 s->handle_packet = handle_in_packet;
Takashi Sakamotoa04513f2017-03-22 21:30:15 +0900763 } else {
764 packets = header_length / 4;
765 cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900766 context->callback.sc = out_stream_callback;
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900767 if (s->flags & CIP_NO_HEADER)
768 s->handle_packet = handle_out_packet_without_header;
769 else
770 s->handle_packet = handle_out_packet;
Takashi Sakamotoa04513f2017-03-22 21:30:15 +0900771 }
772
773 s->start_cycle = cycle;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900774
Takashi Sakamoto73fc7f02016-05-09 21:12:44 +0900775 context->callback.sc(context, tstamp, header_length, header, s);
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900776}
777
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100778/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900779 * amdtp_stream_start - start transferring packets
780 * @s: the AMDTP stream to start
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100781 * @channel: the isochronous channel on the bus
782 * @speed: firewire speed code
783 *
784 * The stream cannot be started until it has been configured with
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900785 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
786 * device can be started.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100787 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900788int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100789{
790 static const struct {
791 unsigned int data_block;
792 unsigned int syt_offset;
793 } initial_state[] = {
794 [CIP_SFC_32000] = { 4, 3072 },
795 [CIP_SFC_48000] = { 6, 1024 },
796 [CIP_SFC_96000] = { 12, 1024 },
797 [CIP_SFC_192000] = { 24, 1024 },
798 [CIP_SFC_44100] = { 0, 67 },
799 [CIP_SFC_88200] = { 0, 67 },
800 [CIP_SFC_176400] = { 0, 67 },
801 };
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900802 unsigned int header_size;
803 enum dma_data_direction dir;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900804 int type, tag, err;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100805
806 mutex_lock(&s->mutex);
807
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900808 if (WARN_ON(amdtp_stream_running(s) ||
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900809 (s->data_block_quadlets < 1))) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100810 err = -EBADFD;
811 goto err_unlock;
812 }
813
Takashi Sakamoto62f00e42016-05-09 23:15:56 +0900814 if (s->direction == AMDTP_IN_STREAM)
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900815 s->data_block_counter = UINT_MAX;
816 else
817 s->data_block_counter = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100818 s->data_block_state = initial_state[s->sfc].data_block;
819 s->syt_offset_state = initial_state[s->sfc].syt_offset;
820 s->last_syt_offset = TICKS_PER_CYCLE;
821
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900822 /* initialize packet buffer */
823 if (s->direction == AMDTP_IN_STREAM) {
824 dir = DMA_FROM_DEVICE;
825 type = FW_ISO_CONTEXT_RECEIVE;
826 header_size = IN_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900827 } else {
828 dir = DMA_TO_DEVICE;
829 type = FW_ISO_CONTEXT_TRANSMIT;
830 header_size = OUT_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900831 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100832 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900833 amdtp_stream_get_max_payload(s), dir);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100834 if (err < 0)
835 goto err_unlock;
836
837 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900838 type, channel, speed, header_size,
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900839 amdtp_stream_first_callback, s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100840 if (IS_ERR(s->context)) {
841 err = PTR_ERR(s->context);
842 if (err == -EBUSY)
843 dev_err(&s->unit->device,
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900844 "no free stream on this controller\n");
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100845 goto err_buffer;
846 }
847
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900848 amdtp_stream_update(s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100849
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900850 if (s->flags & CIP_NO_HEADER)
851 s->tag = TAG_NO_CIP_HEADER;
852 else
853 s->tag = TAG_CIP;
854
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100855 s->packet_index = 0;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900856 do {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900857 if (s->direction == AMDTP_IN_STREAM)
858 err = queue_in_packet(s);
859 else
Takashi Sakamotoff38e0c2016-05-11 07:35:09 +0900860 err = queue_out_packet(s, 0);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900861 if (err < 0)
862 goto err_context;
863 } while (s->packet_index > 0);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100864
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900865 /* NOTE: TAG1 matches CIP. This just affects in stream. */
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900866 tag = FW_ISO_CONTEXT_MATCH_TAG1;
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900867 if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER))
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900868 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
869
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900870 s->callbacked = false;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900871 err = fw_iso_context_start(s->context, -1, 0, tag);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100872 if (err < 0)
873 goto err_context;
874
875 mutex_unlock(&s->mutex);
876
877 return 0;
878
879err_context:
880 fw_iso_context_destroy(s->context);
881 s->context = ERR_PTR(-1);
882err_buffer:
883 iso_packets_buffer_destroy(&s->buffer, s->unit);
884err_unlock:
885 mutex_unlock(&s->mutex);
886
887 return err;
888}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900889EXPORT_SYMBOL(amdtp_stream_start);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100890
891/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900892 * amdtp_stream_pcm_pointer - get the PCM buffer position
893 * @s: the AMDTP stream that transports the PCM data
Clemens Ladische9148dd2012-05-13 18:49:14 +0200894 *
895 * Returns the current buffer position, in frames.
896 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900897unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
Clemens Ladische9148dd2012-05-13 18:49:14 +0200898{
Takashi Sakamoto1dba9db2016-05-12 02:17:39 +0900899 /*
900 * This function is called in software IRQ context of period_tasklet or
901 * process context.
902 *
903 * When the software IRQ context was scheduled by software IRQ context
904 * of IR/IT contexts, queued packets were already handled. Therefore,
905 * no need to flush the queue in buffer anymore.
906 *
907 * When the process context reach here, some packets will be already
908 * queued in the buffer. These packets should be handled immediately
909 * to keep better granularity of PCM pointer.
910 *
911 * Later, the process context will sometimes schedules software IRQ
912 * context of the period_tasklet. Then, no need to flush the queue by
913 * the same reason as described for IR/IT contexts.
914 */
915 if (!in_interrupt() && amdtp_stream_running(s))
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200916 fw_iso_context_flush_completions(s->context);
Clemens Ladische9148dd2012-05-13 18:49:14 +0200917
918 return ACCESS_ONCE(s->pcm_buffer_pointer);
919}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900920EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
Clemens Ladische9148dd2012-05-13 18:49:14 +0200921
922/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900923 * amdtp_stream_update - update the stream after a bus reset
924 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100925 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900926void amdtp_stream_update(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100927{
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900928 /* Precomputing. */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100929 ACCESS_ONCE(s->source_node_id_field) =
Takashi Sakamoto9a2820c2015-05-22 23:21:12 +0900930 (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) &
931 CIP_SID_MASK;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100932}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900933EXPORT_SYMBOL(amdtp_stream_update);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100934
935/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900936 * amdtp_stream_stop - stop sending packets
937 * @s: the AMDTP stream to stop
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100938 *
939 * All PCM and MIDI devices of the stream must be stopped before the stream
940 * itself can be stopped.
941 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900942void amdtp_stream_stop(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100943{
944 mutex_lock(&s->mutex);
945
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900946 if (!amdtp_stream_running(s)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100947 mutex_unlock(&s->mutex);
948 return;
949 }
950
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200951 tasklet_kill(&s->period_tasklet);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100952 fw_iso_context_stop(s->context);
953 fw_iso_context_destroy(s->context);
954 s->context = ERR_PTR(-1);
955 iso_packets_buffer_destroy(&s->buffer, s->unit);
956
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900957 s->callbacked = false;
958
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100959 mutex_unlock(&s->mutex);
960}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900961EXPORT_SYMBOL(amdtp_stream_stop);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100962
963/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900964 * amdtp_stream_pcm_abort - abort the running PCM device
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100965 * @s: the AMDTP stream about to be stopped
966 *
967 * If the isochronous stream needs to be stopped asynchronously, call this
968 * function first to stop the PCM device.
969 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900970void amdtp_stream_pcm_abort(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100971{
972 struct snd_pcm_substream *pcm;
973
974 pcm = ACCESS_ONCE(s->pcm);
Takashi Iwai1fb85102014-11-07 17:08:28 +0100975 if (pcm)
976 snd_pcm_stop_xrun(pcm);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100977}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900978EXPORT_SYMBOL(amdtp_stream_pcm_abort);