blob: 0d580186ef1ac379bcd2cb699ac2f33baeac9029 [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>
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +090014#include <linux/sched.h>
Clemens Ladisch31ef9132011-03-15 07:53:21 +010015#include <sound/pcm.h>
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +090016#include <sound/pcm_params.h>
Takashi Sakamoto83d8d722014-04-25 22:44:47 +090017#include <sound/rawmidi.h>
Clemens Ladisch31ef9132011-03-15 07:53:21 +010018#include "amdtp.h"
19
20#define TICKS_PER_CYCLE 3072
21#define CYCLES_PER_SECOND 8000
22#define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
23
Clemens Ladisch5c697e52014-11-25 22:52:24 +010024/*
Clemens Ladisch25ca9172014-11-25 22:54:10 +010025 * Nominally 3125 bytes/second, but the MIDI port's clock might be
26 * 1% too slow, and the bus clock 100 ppm too fast.
27 */
28#define MIDI_BYTES_PER_SECOND 3093
29
30/*
Clemens Ladisch5c697e52014-11-25 22:52:24 +010031 * Several devices look only at the first eight data blocks.
32 * In any case, this is more than enough for the MIDI data rate.
33 */
34#define MAX_MIDI_RX_BLOCKS 8
35
36#define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 µs */
Clemens Ladisch31ef9132011-03-15 07:53:21 +010037
Takashi Sakamotob445db42014-04-25 22:44:43 +090038/* isochronous header parameters */
39#define ISO_DATA_LENGTH_SHIFT 16
Clemens Ladisch31ef9132011-03-15 07:53:21 +010040#define TAG_CIP 1
41
Takashi Sakamotob445db42014-04-25 22:44:43 +090042/* common isochronous packet header parameters */
Clemens Ladisch31ef9132011-03-15 07:53:21 +010043#define CIP_EOH (1u << 31)
Takashi Sakamotob445db42014-04-25 22:44:43 +090044#define CIP_EOH_MASK 0x80000000
Clemens Ladisch31ef9132011-03-15 07:53:21 +010045#define CIP_FMT_AM (0x10 << 24)
Takashi Sakamotob445db42014-04-25 22:44:43 +090046#define CIP_FMT_MASK 0x3f000000
47#define CIP_SYT_MASK 0x0000ffff
48#define CIP_SYT_NO_INFO 0xffff
49#define CIP_FDF_MASK 0x00ff0000
50#define CIP_FDF_SFC_SHIFT 16
51
52/*
53 * Audio and Music transfer protocol specific parameters
54 * only "Clock-based rate control mode" is supported
55 */
56#define AMDTP_FDF_AM824 (0 << (CIP_FDF_SFC_SHIFT + 3))
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090057#define AMDTP_FDF_NO_DATA 0xff
Takashi Sakamotob445db42014-04-25 22:44:43 +090058#define AMDTP_DBS_MASK 0x00ff0000
59#define AMDTP_DBS_SHIFT 16
60#define AMDTP_DBC_MASK 0x000000ff
Clemens Ladisch31ef9132011-03-15 07:53:21 +010061
62/* TODO: make these configurable */
63#define INTERRUPT_INTERVAL 16
64#define QUEUE_LENGTH 48
65
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090066#define IN_PACKET_HEADER_SIZE 4
Takashi Sakamoto4b7da112014-04-25 22:44:45 +090067#define OUT_PACKET_HEADER_SIZE 0
68
Clemens Ladisch76fb8782012-05-13 22:03:09 +020069static void pcm_period_tasklet(unsigned long data);
70
Clemens Ladisch31ef9132011-03-15 07:53:21 +010071/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090072 * amdtp_stream_init - initialize an AMDTP stream structure
73 * @s: the AMDTP stream to initialize
Clemens Ladisch31ef9132011-03-15 07:53:21 +010074 * @unit: the target of the stream
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090075 * @dir: the direction of stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +010076 * @flags: the packet transmission method to use
77 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090078int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090079 enum amdtp_stream_direction dir, enum cip_flags flags)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010080{
Clemens Ladisch31ef9132011-03-15 07:53:21 +010081 s->unit = fw_unit_get(unit);
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090082 s->direction = dir;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010083 s->flags = flags;
84 s->context = ERR_PTR(-1);
85 mutex_init(&s->mutex);
Clemens Ladisch76fb8782012-05-13 22:03:09 +020086 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +010087 s->packet_index = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010088
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +090089 init_waitqueue_head(&s->callback_wait);
90 s->callbacked = false;
91 s->sync_slave = NULL;
92
Clemens Ladisch31ef9132011-03-15 07:53:21 +010093 return 0;
94}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090095EXPORT_SYMBOL(amdtp_stream_init);
Clemens Ladisch31ef9132011-03-15 07:53:21 +010096
97/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090098 * amdtp_stream_destroy - free stream resources
99 * @s: the AMDTP stream to destroy
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100100 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900101void amdtp_stream_destroy(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100102{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900103 WARN_ON(amdtp_stream_running(s));
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100104 mutex_destroy(&s->mutex);
105 fw_unit_put(s->unit);
106}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900107EXPORT_SYMBOL(amdtp_stream_destroy);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100108
Clemens Ladischc5280e92011-10-16 21:39:00 +0200109const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
Clemens Ladischa7304e32011-09-04 22:16:10 +0200110 [CIP_SFC_32000] = 8,
111 [CIP_SFC_44100] = 8,
112 [CIP_SFC_48000] = 8,
113 [CIP_SFC_88200] = 16,
114 [CIP_SFC_96000] = 16,
115 [CIP_SFC_176400] = 32,
116 [CIP_SFC_192000] = 32,
117};
118EXPORT_SYMBOL(amdtp_syt_intervals);
119
Takashi Sakamotof9503a62014-05-28 00:14:36 +0900120const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900121 [CIP_SFC_32000] = 32000,
122 [CIP_SFC_44100] = 44100,
123 [CIP_SFC_48000] = 48000,
124 [CIP_SFC_88200] = 88200,
125 [CIP_SFC_96000] = 96000,
126 [CIP_SFC_176400] = 176400,
127 [CIP_SFC_192000] = 192000,
128};
129EXPORT_SYMBOL(amdtp_rate_table);
130
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100131/**
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900132 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
133 * @s: the AMDTP stream, which must be initialized.
134 * @runtime: the PCM substream runtime
135 */
136int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
137 struct snd_pcm_runtime *runtime)
138{
139 int err;
140
141 /* AM824 in IEC 61883-6 can deliver 24bit data */
142 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
143 if (err < 0)
144 goto end;
145
146 /*
147 * Currently firewire-lib processes 16 packets in one software
148 * interrupt callback. This equals to 2msec but actually the
149 * interval of the interrupts has a jitter.
150 * Additionally, even if adding a constraint to fit period size to
151 * 2msec, actual calculated frames per period doesn't equal to 2msec,
152 * depending on sampling rate.
153 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
154 * Here let us use 5msec for safe period interrupt.
155 */
156 err = snd_pcm_hw_constraint_minmax(runtime,
157 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
158 5000, UINT_MAX);
159 if (err < 0)
160 goto end;
161
162 /* Non-Blocking stream has no more constraints */
163 if (!(s->flags & CIP_BLOCKING))
164 goto end;
165
166 /*
167 * One AMDTP packet can include some frames. In blocking mode, the
168 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
169 * depending on its sampling rate. For accurate period interrupt, it's
170 * preferrable to aligh period/buffer sizes to current SYT_INTERVAL.
171 *
172 * TODO: These constraints can be improved with propper rules.
173 * Currently apply LCM of SYT_INTEVALs.
174 */
175 err = snd_pcm_hw_constraint_step(runtime, 0,
176 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
177 if (err < 0)
178 goto end;
179 err = snd_pcm_hw_constraint_step(runtime, 0,
180 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
181end:
182 return err;
183}
184EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
185
186/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900187 * amdtp_stream_set_parameters - set stream parameters
188 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100189 * @rate: the sample rate
Clemens Ladischa7304e32011-09-04 22:16:10 +0200190 * @pcm_channels: the number of PCM samples in each data block, to be encoded
191 * as AM824 multi-bit linear audio
192 * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100193 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200194 * The parameters must be set before the stream is started, and must not be
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100195 * changed while the stream is running.
196 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900197void amdtp_stream_set_parameters(struct amdtp_stream *s,
198 unsigned int rate,
199 unsigned int pcm_channels,
200 unsigned int midi_ports)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100201{
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900202 unsigned int i, sfc, midi_channels;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100203
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900204 midi_channels = DIV_ROUND_UP(midi_ports, 8);
205
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900206 if (WARN_ON(amdtp_stream_running(s)) |
207 WARN_ON(pcm_channels > AMDTP_MAX_CHANNELS_FOR_PCM) |
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900208 WARN_ON(midi_channels > AMDTP_MAX_CHANNELS_FOR_MIDI))
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100209 return;
210
Takashi Sakamotof9503a62014-05-28 00:14:36 +0900211 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc)
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900212 if (amdtp_rate_table[sfc] == rate)
Clemens Ladische84d15f2011-09-04 22:12:48 +0200213 goto sfc_found;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100214 WARN_ON(1);
Clemens Ladische84d15f2011-09-04 22:12:48 +0200215 return;
216
217sfc_found:
Takashi Sakamoto10550be2014-04-25 22:44:51 +0900218 s->pcm_channels = pcm_channels;
Clemens Ladische84d15f2011-09-04 22:12:48 +0200219 s->sfc = sfc;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900220 s->data_block_quadlets = s->pcm_channels + midi_channels;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200221 s->midi_ports = midi_ports;
222
223 s->syt_interval = amdtp_syt_intervals[sfc];
Clemens Ladische84d15f2011-09-04 22:12:48 +0200224
225 /* default buffering in the device */
226 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
227 if (s->flags & CIP_BLOCKING)
228 /* additional buffering needed to adjust for no-data packets */
229 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900230
231 /* init the position map for PCM and MIDI channels */
232 for (i = 0; i < pcm_channels; i++)
233 s->pcm_positions[i] = i;
234 s->midi_position = s->pcm_channels;
Clemens Ladisch25ca9172014-11-25 22:54:10 +0100235
236 /*
237 * We do not know the actual MIDI FIFO size of most devices. Just
238 * assume two bytes, i.e., one byte can be received over the bus while
239 * the previous one is transmitted over MIDI.
240 * (The value here is adjusted for midi_ratelimit_per_packet().)
241 */
242 s->midi_fifo_limit = rate - MIDI_BYTES_PER_SECOND * s->syt_interval + 1;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100243}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900244EXPORT_SYMBOL(amdtp_stream_set_parameters);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100245
246/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900247 * amdtp_stream_get_max_payload - get the stream's packet size
248 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100249 *
250 * This function must not be called before the stream has been configured
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900251 * with amdtp_stream_set_parameters().
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100252 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900253unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100254{
Clemens Ladische84d15f2011-09-04 22:12:48 +0200255 return 8 + s->syt_interval * s->data_block_quadlets * 4;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100256}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900257EXPORT_SYMBOL(amdtp_stream_get_max_payload);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100258
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900259static void amdtp_write_s16(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100260 struct snd_pcm_substream *pcm,
261 __be32 *buffer, unsigned int frames);
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900262static void amdtp_write_s32(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100263 struct snd_pcm_substream *pcm,
264 __be32 *buffer, unsigned int frames);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900265static void amdtp_read_s32(struct amdtp_stream *s,
266 struct snd_pcm_substream *pcm,
267 __be32 *buffer, unsigned int frames);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100268
269/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900270 * amdtp_stream_set_pcm_format - set the PCM format
271 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100272 * @format: the format of the ALSA PCM device
273 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200274 * The sample format must be set after the other paramters (rate/PCM channels/
275 * MIDI) and before the stream is started, and must not be changed while the
276 * stream is running.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100277 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900278void amdtp_stream_set_pcm_format(struct amdtp_stream *s,
279 snd_pcm_format_t format)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100280{
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900281 if (WARN_ON(amdtp_stream_pcm_running(s)))
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100282 return;
283
284 switch (format) {
285 default:
286 WARN_ON(1);
287 /* fall through */
288 case SNDRV_PCM_FORMAT_S16:
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900289 if (s->direction == AMDTP_OUT_STREAM) {
Takashi Sakamoto10550be2014-04-25 22:44:51 +0900290 s->transfer_samples = amdtp_write_s16;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900291 break;
292 }
293 WARN_ON(1);
294 /* fall through */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100295 case SNDRV_PCM_FORMAT_S32:
Takashi Sakamoto10550be2014-04-25 22:44:51 +0900296 if (s->direction == AMDTP_OUT_STREAM)
297 s->transfer_samples = amdtp_write_s32;
298 else
299 s->transfer_samples = amdtp_read_s32;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100300 break;
301 }
302}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900303EXPORT_SYMBOL(amdtp_stream_set_pcm_format);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100304
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200305/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900306 * amdtp_stream_pcm_prepare - prepare PCM device for running
307 * @s: the AMDTP stream
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200308 *
309 * This function should be called from the PCM device's .prepare callback.
310 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900311void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200312{
313 tasklet_kill(&s->period_tasklet);
314 s->pcm_buffer_pointer = 0;
315 s->pcm_period_pointer = 0;
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200316 s->pointer_flush = true;
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200317}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900318EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200319
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900320static unsigned int calculate_data_blocks(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100321{
322 unsigned int phase, data_blocks;
323
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900324 if (s->flags & CIP_BLOCKING)
325 data_blocks = s->syt_interval;
326 else if (!cip_sfc_is_base_44100(s->sfc)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100327 /* Sample_rate / 8000 is an integer, and precomputed. */
328 data_blocks = s->data_block_state;
329 } else {
330 phase = s->data_block_state;
331
332 /*
333 * This calculates the number of data blocks per packet so that
334 * 1) the overall rate is correct and exactly synchronized to
335 * the bus clock, and
336 * 2) packets with a rounded-up number of blocks occur as early
337 * as possible in the sequence (to prevent underruns of the
338 * device's buffer).
339 */
340 if (s->sfc == CIP_SFC_44100)
341 /* 6 6 5 6 5 6 5 ... */
342 data_blocks = 5 + ((phase & 1) ^
343 (phase == 0 || phase >= 40));
344 else
345 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
346 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
347 if (++phase >= (80 >> (s->sfc >> 1)))
348 phase = 0;
349 s->data_block_state = phase;
350 }
351
352 return data_blocks;
353}
354
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900355static unsigned int calculate_syt(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100356 unsigned int cycle)
357{
358 unsigned int syt_offset, phase, index, syt;
359
360 if (s->last_syt_offset < TICKS_PER_CYCLE) {
361 if (!cip_sfc_is_base_44100(s->sfc))
362 syt_offset = s->last_syt_offset + s->syt_offset_state;
363 else {
364 /*
365 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
366 * n * SYT_INTERVAL * 24576000 / sample_rate
367 * Modulo TICKS_PER_CYCLE, the difference between successive
368 * elements is about 1386.23. Rounding the results of this
369 * formula to the SYT precision results in a sequence of
370 * differences that begins with:
371 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
372 * This code generates _exactly_ the same sequence.
373 */
374 phase = s->syt_offset_state;
375 index = phase % 13;
376 syt_offset = s->last_syt_offset;
377 syt_offset += 1386 + ((index && !(index & 3)) ||
378 phase == 146);
379 if (++phase >= 147)
380 phase = 0;
381 s->syt_offset_state = phase;
382 }
383 } else
384 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
385 s->last_syt_offset = syt_offset;
386
Clemens Ladischbe454362011-03-15 07:55:02 +0100387 if (syt_offset < TICKS_PER_CYCLE) {
Clemens Ladische84d15f2011-09-04 22:12:48 +0200388 syt_offset += s->transfer_delay;
Clemens Ladischbe454362011-03-15 07:55:02 +0100389 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
390 syt += syt_offset % TICKS_PER_CYCLE;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100391
Takashi Sakamotob445db42014-04-25 22:44:43 +0900392 return syt & CIP_SYT_MASK;
Clemens Ladischbe454362011-03-15 07:55:02 +0100393 } else {
Takashi Sakamotob445db42014-04-25 22:44:43 +0900394 return CIP_SYT_NO_INFO;
Clemens Ladischbe454362011-03-15 07:55:02 +0100395 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100396}
397
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900398static void amdtp_write_s32(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100399 struct snd_pcm_substream *pcm,
400 __be32 *buffer, unsigned int frames)
401{
402 struct snd_pcm_runtime *runtime = pcm->runtime;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900403 unsigned int channels, remaining_frames, i, c;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100404 const u32 *src;
405
406 channels = s->pcm_channels;
407 src = (void *)runtime->dma_area +
Takashi Sakamotoe84841f2013-09-14 00:35:47 +0900408 frames_to_bytes(runtime, s->pcm_buffer_pointer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100409 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100410
411 for (i = 0; i < frames; ++i) {
412 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900413 buffer[s->pcm_positions[c]] =
414 cpu_to_be32((*src >> 8) | 0x40000000);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100415 src++;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100416 }
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900417 buffer += s->data_block_quadlets;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100418 if (--remaining_frames == 0)
419 src = (void *)runtime->dma_area;
420 }
421}
422
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900423static void amdtp_write_s16(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100424 struct snd_pcm_substream *pcm,
425 __be32 *buffer, unsigned int frames)
426{
427 struct snd_pcm_runtime *runtime = pcm->runtime;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900428 unsigned int channels, remaining_frames, i, c;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100429 const u16 *src;
430
431 channels = s->pcm_channels;
432 src = (void *)runtime->dma_area +
Takashi Sakamotoe84841f2013-09-14 00:35:47 +0900433 frames_to_bytes(runtime, s->pcm_buffer_pointer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100434 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100435
436 for (i = 0; i < frames; ++i) {
437 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900438 buffer[s->pcm_positions[c]] =
Takashi Sakamotoa6975f22014-06-02 01:50:16 +0900439 cpu_to_be32((*src << 8) | 0x42000000);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100440 src++;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100441 }
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900442 buffer += s->data_block_quadlets;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100443 if (--remaining_frames == 0)
444 src = (void *)runtime->dma_area;
445 }
446}
447
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900448static void amdtp_read_s32(struct amdtp_stream *s,
449 struct snd_pcm_substream *pcm,
450 __be32 *buffer, unsigned int frames)
451{
452 struct snd_pcm_runtime *runtime = pcm->runtime;
453 unsigned int channels, remaining_frames, i, c;
454 u32 *dst;
455
456 channels = s->pcm_channels;
457 dst = (void *)runtime->dma_area +
458 frames_to_bytes(runtime, s->pcm_buffer_pointer);
459 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
460
461 for (i = 0; i < frames; ++i) {
462 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900463 *dst = be32_to_cpu(buffer[s->pcm_positions[c]]) << 8;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900464 dst++;
465 }
466 buffer += s->data_block_quadlets;
467 if (--remaining_frames == 0)
468 dst = (void *)runtime->dma_area;
469 }
470}
471
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900472static void amdtp_fill_pcm_silence(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100473 __be32 *buffer, unsigned int frames)
474{
475 unsigned int i, c;
476
477 for (i = 0; i < frames; ++i) {
478 for (c = 0; c < s->pcm_channels; ++c)
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900479 buffer[s->pcm_positions[c]] = cpu_to_be32(0x40000000);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100480 buffer += s->data_block_quadlets;
481 }
482}
483
Clemens Ladisch25ca9172014-11-25 22:54:10 +0100484/*
485 * To avoid sending MIDI bytes at too high a rate, assume that the receiving
486 * device has a FIFO, and track how much it is filled. This values increases
487 * by one whenever we send one byte in a packet, but the FIFO empties at
488 * a constant rate independent of our packet rate. One packet has syt_interval
489 * samples, so the number of bytes that empty out of the FIFO, per packet(!),
490 * is MIDI_BYTES_PER_SECOND * syt_interval / sample_rate. To avoid storing
491 * fractional values, the values in midi_fifo_used[] are measured in bytes
492 * multiplied by the sample rate.
493 */
494static bool midi_ratelimit_per_packet(struct amdtp_stream *s, unsigned int port)
495{
496 int used;
497
498 used = s->midi_fifo_used[port];
499 if (used == 0) /* common shortcut */
500 return true;
501
502 used -= MIDI_BYTES_PER_SECOND * s->syt_interval;
503 used = max(used, 0);
504 s->midi_fifo_used[port] = used;
505
506 return used < s->midi_fifo_limit;
507}
508
509static void midi_rate_use_one_byte(struct amdtp_stream *s, unsigned int port)
510{
511 s->midi_fifo_used[port] += amdtp_rate_table[s->sfc];
512}
513
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900514static void amdtp_fill_midi(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100515 __be32 *buffer, unsigned int frames)
516{
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900517 unsigned int f, port;
518 u8 *b;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100519
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900520 for (f = 0; f < frames; f++) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900521 b = (u8 *)&buffer[s->midi_position];
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900522
523 port = (s->data_block_counter + f) % 8;
Clemens Ladisch25ca9172014-11-25 22:54:10 +0100524 if (f < MAX_MIDI_RX_BLOCKS &&
525 midi_ratelimit_per_packet(s, port) &&
526 s->midi[port] != NULL &&
527 snd_rawmidi_transmit(s->midi[port], &b[1], 1) == 1) {
528 midi_rate_use_one_byte(s, port);
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900529 b[0] = 0x81;
Clemens Ladisch25ca9172014-11-25 22:54:10 +0100530 } else {
531 b[0] = 0x80;
532 b[1] = 0;
533 }
534 b[2] = 0;
535 b[3] = 0;
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900536
537 buffer += s->data_block_quadlets;
538 }
539}
540
541static void amdtp_pull_midi(struct amdtp_stream *s,
542 __be32 *buffer, unsigned int frames)
543{
544 unsigned int f, port;
545 int len;
546 u8 *b;
547
548 for (f = 0; f < frames; f++) {
549 port = (s->data_block_counter + f) % 8;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900550 b = (u8 *)&buffer[s->midi_position];
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900551
552 len = b[0] - 0x80;
553 if ((1 <= len) && (len <= 3) && (s->midi[port]))
554 snd_rawmidi_receive(s->midi[port], b + 1, len);
555
556 buffer += s->data_block_quadlets;
557 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100558}
559
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900560static void update_pcm_pointers(struct amdtp_stream *s,
561 struct snd_pcm_substream *pcm,
562 unsigned int frames)
Takashi Sakamoto65845f22014-08-29 13:40:45 +0900563{
564 unsigned int ptr;
565
566 /*
567 * In IEC 61883-6, one data block represents one event. In ALSA, one
568 * event equals to one PCM frame. But Dice has a quirk to transfer
569 * two PCM frames in one data block.
570 */
571 if (s->double_pcm_frames)
572 frames *= 2;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900573
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900574 ptr = s->pcm_buffer_pointer + frames;
575 if (ptr >= pcm->runtime->buffer_size)
576 ptr -= pcm->runtime->buffer_size;
577 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
578
579 s->pcm_period_pointer += frames;
580 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
581 s->pcm_period_pointer -= pcm->runtime->period_size;
582 s->pointer_flush = false;
583 tasklet_hi_schedule(&s->period_tasklet);
584 }
585}
586
587static void pcm_period_tasklet(unsigned long data)
588{
589 struct amdtp_stream *s = (void *)data;
590 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
591
592 if (pcm)
593 snd_pcm_period_elapsed(pcm);
594}
595
596static int queue_packet(struct amdtp_stream *s,
597 unsigned int header_length,
598 unsigned int payload_length, bool skip)
599{
600 struct fw_iso_packet p = {0};
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900601 int err = 0;
602
603 if (IS_ERR(s->context))
604 goto end;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900605
606 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
607 p.tag = TAG_CIP;
608 p.header_length = header_length;
609 p.payload_length = (!skip) ? payload_length : 0;
610 p.skip = skip;
611 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
612 s->buffer.packets[s->packet_index].offset);
613 if (err < 0) {
614 dev_err(&s->unit->device, "queueing error: %d\n", err);
615 goto end;
616 }
617
618 if (++s->packet_index >= QUEUE_LENGTH)
619 s->packet_index = 0;
620end:
621 return err;
622}
623
624static inline int queue_out_packet(struct amdtp_stream *s,
625 unsigned int payload_length, bool skip)
626{
627 return queue_packet(s, OUT_PACKET_HEADER_SIZE,
628 payload_length, skip);
629}
630
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900631static inline int queue_in_packet(struct amdtp_stream *s)
632{
633 return queue_packet(s, IN_PACKET_HEADER_SIZE,
634 amdtp_stream_get_max_payload(s), false);
635}
636
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900637static void handle_out_packet(struct amdtp_stream *s, unsigned int syt)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100638{
639 __be32 *buffer;
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900640 unsigned int data_blocks, payload_length;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100641 struct snd_pcm_substream *pcm;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100642
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100643 if (s->packet_index < 0)
644 return;
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100645
Takashi Sakamoto82755ab2013-11-21 14:21:06 +0900646 /* this module generate empty packet for 'no data' */
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900647 if (!(s->flags & CIP_BLOCKING) || (syt != CIP_SYT_NO_INFO))
Clemens Ladische84d15f2011-09-04 22:12:48 +0200648 data_blocks = calculate_data_blocks(s);
Takashi Sakamoto82755ab2013-11-21 14:21:06 +0900649 else
650 data_blocks = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100651
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900652 buffer = s->buffer.packets[s->packet_index].buffer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100653 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
Takashi Sakamotob445db42014-04-25 22:44:43 +0900654 (s->data_block_quadlets << AMDTP_DBS_SHIFT) |
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100655 s->data_block_counter);
656 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
Takashi Sakamotob445db42014-04-25 22:44:43 +0900657 (s->sfc << CIP_FDF_SFC_SHIFT) | syt);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100658 buffer += 2;
659
660 pcm = ACCESS_ONCE(s->pcm);
661 if (pcm)
662 s->transfer_samples(s, pcm, buffer, data_blocks);
663 else
664 amdtp_fill_pcm_silence(s, buffer, data_blocks);
665 if (s->midi_ports)
666 amdtp_fill_midi(s, buffer, data_blocks);
667
668 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
669
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900670 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
671 if (queue_out_packet(s, payload_length, false) < 0) {
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100672 s->packet_index = -1;
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900673 amdtp_stream_pcm_abort(s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100674 return;
675 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100676
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200677 if (pcm)
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900678 update_pcm_pointers(s, pcm, data_blocks);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200679}
680
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900681static void handle_in_packet(struct amdtp_stream *s,
682 unsigned int payload_quadlets,
683 __be32 *buffer)
684{
685 u32 cip_header[2];
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900686 unsigned int data_blocks, data_block_quadlets, data_block_counter,
687 dbc_interval;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900688 struct snd_pcm_substream *pcm = NULL;
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900689 bool lost;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900690
691 cip_header[0] = be32_to_cpu(buffer[0]);
692 cip_header[1] = be32_to_cpu(buffer[1]);
693
694 /*
695 * This module supports 'Two-quadlet CIP header with SYT field'.
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900696 * For convenience, also check FMT field is AM824 or not.
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900697 */
698 if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
699 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH) ||
700 ((cip_header[1] & CIP_FMT_MASK) != CIP_FMT_AM)) {
701 dev_info_ratelimited(&s->unit->device,
702 "Invalid CIP header for AMDTP: %08X:%08X\n",
703 cip_header[0], cip_header[1]);
704 goto end;
705 }
706
707 /* Calculate data blocks */
708 if (payload_quadlets < 3 ||
709 ((cip_header[1] & CIP_FDF_MASK) ==
710 (AMDTP_FDF_NO_DATA << CIP_FDF_SFC_SHIFT))) {
711 data_blocks = 0;
712 } else {
713 data_block_quadlets =
714 (cip_header[0] & AMDTP_DBS_MASK) >> AMDTP_DBS_SHIFT;
715 /* avoid division by zero */
716 if (data_block_quadlets == 0) {
717 dev_info_ratelimited(&s->unit->device,
718 "Detect invalid value in dbs field: %08X\n",
719 cip_header[0]);
720 goto err;
721 }
Takashi Sakamoto69702232014-04-25 22:45:05 +0900722 if (s->flags & CIP_WRONG_DBS)
723 data_block_quadlets = s->data_block_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900724
725 data_blocks = (payload_quadlets - 2) / data_block_quadlets;
726 }
727
728 /* Check data block counter continuity */
729 data_block_counter = cip_header[0] & AMDTP_DBC_MASK;
Takashi Sakamoto9d591242014-04-25 22:45:27 +0900730 if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
731 s->data_block_counter != UINT_MAX)
732 data_block_counter = s->data_block_counter;
733
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900734 if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) && data_block_counter == 0) ||
735 (s->data_block_counter == UINT_MAX)) {
Takashi Sakamotob84b1a22014-04-25 22:45:07 +0900736 lost = false;
737 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900738 lost = data_block_counter != s->data_block_counter;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900739 } else {
740 if ((data_blocks > 0) && (s->tx_dbc_interval > 0))
741 dbc_interval = s->tx_dbc_interval;
742 else
743 dbc_interval = data_blocks;
744
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900745 lost = data_block_counter !=
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900746 ((s->data_block_counter + dbc_interval) & 0xff);
747 }
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900748
749 if (lost) {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900750 dev_info(&s->unit->device,
751 "Detect discontinuity of CIP: %02X %02X\n",
752 s->data_block_counter, data_block_counter);
753 goto err;
754 }
755
756 if (data_blocks > 0) {
757 buffer += 2;
758
759 pcm = ACCESS_ONCE(s->pcm);
760 if (pcm)
761 s->transfer_samples(s, pcm, buffer, data_blocks);
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900762
763 if (s->midi_ports)
764 amdtp_pull_midi(s, buffer, data_blocks);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900765 }
766
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900767 if (s->flags & CIP_DBC_IS_END_EVENT)
768 s->data_block_counter = data_block_counter;
769 else
770 s->data_block_counter =
771 (data_block_counter + data_blocks) & 0xff;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900772end:
773 if (queue_in_packet(s) < 0)
774 goto err;
775
776 if (pcm)
777 update_pcm_pointers(s, pcm, data_blocks);
778
779 return;
780err:
781 s->packet_index = -1;
782 amdtp_stream_pcm_abort(s);
783}
784
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900785static void out_stream_callback(struct fw_iso_context *context, u32 cycle,
786 size_t header_length, void *header,
787 void *private_data)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100788{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900789 struct amdtp_stream *s = private_data;
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900790 unsigned int i, syt, packets = header_length / 4;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100791
792 /*
793 * Compute the cycle of the last queued packet.
794 * (We need only the four lowest bits for the SYT, so we can ignore
795 * that bits 0-11 must wrap around at 3072.)
796 */
797 cycle += QUEUE_LENGTH - packets;
798
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900799 for (i = 0; i < packets; ++i) {
800 syt = calculate_syt(s, ++cycle);
801 handle_out_packet(s, syt);
802 }
Clemens Ladisch13882a82011-05-02 09:33:56 +0200803 fw_iso_context_queue_flush(s->context);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100804}
805
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900806static void in_stream_callback(struct fw_iso_context *context, u32 cycle,
807 size_t header_length, void *header,
808 void *private_data)
809{
810 struct amdtp_stream *s = private_data;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900811 unsigned int p, syt, packets, payload_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900812 __be32 *buffer, *headers = header;
813
814 /* The number of packets in buffer */
815 packets = header_length / IN_PACKET_HEADER_SIZE;
816
817 for (p = 0; p < packets; p++) {
818 if (s->packet_index < 0)
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900819 break;
820
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900821 buffer = s->buffer.packets[s->packet_index].buffer;
822
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900823 /* Process sync slave stream */
824 if (s->sync_slave && s->sync_slave->callbacked) {
825 syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
826 handle_out_packet(s->sync_slave, syt);
827 }
828
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900829 /* The number of quadlets in this packet */
830 payload_quadlets =
831 (be32_to_cpu(headers[p]) >> ISO_DATA_LENGTH_SHIFT) / 4;
832 handle_in_packet(s, payload_quadlets, buffer);
833 }
834
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900835 /* Queueing error or detecting discontinuity */
836 if (s->packet_index < 0) {
837 /* Abort sync slave. */
838 if (s->sync_slave) {
839 s->sync_slave->packet_index = -1;
840 amdtp_stream_pcm_abort(s->sync_slave);
841 }
842 return;
843 }
844
845 /* when sync to device, flush the packets for slave stream */
846 if (s->sync_slave && s->sync_slave->callbacked)
847 fw_iso_context_queue_flush(s->sync_slave->context);
848
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900849 fw_iso_context_queue_flush(s->context);
850}
851
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900852/* processing is done by master callback */
853static void slave_stream_callback(struct fw_iso_context *context, u32 cycle,
854 size_t header_length, void *header,
855 void *private_data)
856{
857 return;
858}
859
860/* this is executed one time */
861static void amdtp_stream_first_callback(struct fw_iso_context *context,
862 u32 cycle, size_t header_length,
863 void *header, void *private_data)
864{
865 struct amdtp_stream *s = private_data;
866
867 /*
868 * For in-stream, first packet has come.
869 * For out-stream, prepared to transmit first packet
870 */
871 s->callbacked = true;
872 wake_up(&s->callback_wait);
873
874 if (s->direction == AMDTP_IN_STREAM)
875 context->callback.sc = in_stream_callback;
876 else if ((s->flags & CIP_BLOCKING) && (s->flags & CIP_SYNC_TO_DEVICE))
877 context->callback.sc = slave_stream_callback;
878 else
879 context->callback.sc = out_stream_callback;
880
881 context->callback.sc(context, cycle, header_length, header, s);
882}
883
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100884/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900885 * amdtp_stream_start - start transferring packets
886 * @s: the AMDTP stream to start
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100887 * @channel: the isochronous channel on the bus
888 * @speed: firewire speed code
889 *
890 * The stream cannot be started until it has been configured with
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900891 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
892 * device can be started.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100893 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900894int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100895{
896 static const struct {
897 unsigned int data_block;
898 unsigned int syt_offset;
899 } initial_state[] = {
900 [CIP_SFC_32000] = { 4, 3072 },
901 [CIP_SFC_48000] = { 6, 1024 },
902 [CIP_SFC_96000] = { 12, 1024 },
903 [CIP_SFC_192000] = { 24, 1024 },
904 [CIP_SFC_44100] = { 0, 67 },
905 [CIP_SFC_88200] = { 0, 67 },
906 [CIP_SFC_176400] = { 0, 67 },
907 };
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900908 unsigned int header_size;
909 enum dma_data_direction dir;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900910 int type, tag, err;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100911
912 mutex_lock(&s->mutex);
913
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900914 if (WARN_ON(amdtp_stream_running(s) ||
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900915 (s->data_block_quadlets < 1))) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100916 err = -EBADFD;
917 goto err_unlock;
918 }
919
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900920 if (s->direction == AMDTP_IN_STREAM &&
921 s->flags & CIP_SKIP_INIT_DBC_CHECK)
922 s->data_block_counter = UINT_MAX;
923 else
924 s->data_block_counter = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100925 s->data_block_state = initial_state[s->sfc].data_block;
926 s->syt_offset_state = initial_state[s->sfc].syt_offset;
927 s->last_syt_offset = TICKS_PER_CYCLE;
928
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900929 /* initialize packet buffer */
930 if (s->direction == AMDTP_IN_STREAM) {
931 dir = DMA_FROM_DEVICE;
932 type = FW_ISO_CONTEXT_RECEIVE;
933 header_size = IN_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900934 } else {
935 dir = DMA_TO_DEVICE;
936 type = FW_ISO_CONTEXT_TRANSMIT;
937 header_size = OUT_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900938 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100939 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900940 amdtp_stream_get_max_payload(s), dir);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100941 if (err < 0)
942 goto err_unlock;
943
944 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900945 type, channel, speed, header_size,
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900946 amdtp_stream_first_callback, s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100947 if (IS_ERR(s->context)) {
948 err = PTR_ERR(s->context);
949 if (err == -EBUSY)
950 dev_err(&s->unit->device,
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900951 "no free stream on this controller\n");
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100952 goto err_buffer;
953 }
954
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900955 amdtp_stream_update(s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100956
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100957 s->packet_index = 0;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900958 do {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900959 if (s->direction == AMDTP_IN_STREAM)
960 err = queue_in_packet(s);
961 else
962 err = queue_out_packet(s, 0, true);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900963 if (err < 0)
964 goto err_context;
965 } while (s->packet_index > 0);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100966
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900967 /* NOTE: TAG1 matches CIP. This just affects in stream. */
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900968 tag = FW_ISO_CONTEXT_MATCH_TAG1;
969 if (s->flags & CIP_EMPTY_WITH_TAG0)
970 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
971
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900972 s->callbacked = false;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900973 err = fw_iso_context_start(s->context, -1, 0, tag);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100974 if (err < 0)
975 goto err_context;
976
977 mutex_unlock(&s->mutex);
978
979 return 0;
980
981err_context:
982 fw_iso_context_destroy(s->context);
983 s->context = ERR_PTR(-1);
984err_buffer:
985 iso_packets_buffer_destroy(&s->buffer, s->unit);
986err_unlock:
987 mutex_unlock(&s->mutex);
988
989 return err;
990}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900991EXPORT_SYMBOL(amdtp_stream_start);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100992
993/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900994 * amdtp_stream_pcm_pointer - get the PCM buffer position
995 * @s: the AMDTP stream that transports the PCM data
Clemens Ladische9148dd2012-05-13 18:49:14 +0200996 *
997 * Returns the current buffer position, in frames.
998 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900999unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
Clemens Ladische9148dd2012-05-13 18:49:14 +02001000{
Clemens Ladisch92b862c2012-05-13 19:07:22 +02001001 /* this optimization is allowed to be racy */
Takashi Sakamotoc8de6db2014-04-25 22:44:53 +09001002 if (s->pointer_flush && amdtp_stream_running(s))
Clemens Ladisch92b862c2012-05-13 19:07:22 +02001003 fw_iso_context_flush_completions(s->context);
1004 else
1005 s->pointer_flush = true;
Clemens Ladische9148dd2012-05-13 18:49:14 +02001006
1007 return ACCESS_ONCE(s->pcm_buffer_pointer);
1008}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001009EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
Clemens Ladische9148dd2012-05-13 18:49:14 +02001010
1011/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001012 * amdtp_stream_update - update the stream after a bus reset
1013 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001014 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001015void amdtp_stream_update(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001016{
1017 ACCESS_ONCE(s->source_node_id_field) =
1018 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
1019}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001020EXPORT_SYMBOL(amdtp_stream_update);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001021
1022/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001023 * amdtp_stream_stop - stop sending packets
1024 * @s: the AMDTP stream to stop
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001025 *
1026 * All PCM and MIDI devices of the stream must be stopped before the stream
1027 * itself can be stopped.
1028 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001029void amdtp_stream_stop(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001030{
1031 mutex_lock(&s->mutex);
1032
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001033 if (!amdtp_stream_running(s)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001034 mutex_unlock(&s->mutex);
1035 return;
1036 }
1037
Clemens Ladisch76fb8782012-05-13 22:03:09 +02001038 tasklet_kill(&s->period_tasklet);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001039 fw_iso_context_stop(s->context);
1040 fw_iso_context_destroy(s->context);
1041 s->context = ERR_PTR(-1);
1042 iso_packets_buffer_destroy(&s->buffer, s->unit);
1043
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +09001044 s->callbacked = false;
1045
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001046 mutex_unlock(&s->mutex);
1047}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001048EXPORT_SYMBOL(amdtp_stream_stop);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001049
1050/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001051 * amdtp_stream_pcm_abort - abort the running PCM device
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001052 * @s: the AMDTP stream about to be stopped
1053 *
1054 * If the isochronous stream needs to be stopped asynchronously, call this
1055 * function first to stop the PCM device.
1056 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001057void amdtp_stream_pcm_abort(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001058{
1059 struct snd_pcm_substream *pcm;
1060
1061 pcm = ACCESS_ONCE(s->pcm);
Takashi Iwai1fb85102014-11-07 17:08:28 +01001062 if (pcm)
1063 snd_pcm_stop_xrun(pcm);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001064}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001065EXPORT_SYMBOL(amdtp_stream_pcm_abort);