blob: e061355f535f071f0fd096bfee62fb2702c0c94d [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
Takashi Sakamotoca5b5052015-02-21 11:50:17 +090036#define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */
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{
Takashi Sakamotoc6f224d2015-02-21 23:54:58 +090081 s->unit = 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);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100105}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900106EXPORT_SYMBOL(amdtp_stream_destroy);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100107
Clemens Ladischc5280e92011-10-16 21:39:00 +0200108const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
Clemens Ladischa7304e32011-09-04 22:16:10 +0200109 [CIP_SFC_32000] = 8,
110 [CIP_SFC_44100] = 8,
111 [CIP_SFC_48000] = 8,
112 [CIP_SFC_88200] = 16,
113 [CIP_SFC_96000] = 16,
114 [CIP_SFC_176400] = 32,
115 [CIP_SFC_192000] = 32,
116};
117EXPORT_SYMBOL(amdtp_syt_intervals);
118
Takashi Sakamotof9503a62014-05-28 00:14:36 +0900119const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900120 [CIP_SFC_32000] = 32000,
121 [CIP_SFC_44100] = 44100,
122 [CIP_SFC_48000] = 48000,
123 [CIP_SFC_88200] = 88200,
124 [CIP_SFC_96000] = 96000,
125 [CIP_SFC_176400] = 176400,
126 [CIP_SFC_192000] = 192000,
127};
128EXPORT_SYMBOL(amdtp_rate_table);
129
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100130/**
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900131 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
132 * @s: the AMDTP stream, which must be initialized.
133 * @runtime: the PCM substream runtime
134 */
135int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
136 struct snd_pcm_runtime *runtime)
137{
138 int err;
139
140 /* AM824 in IEC 61883-6 can deliver 24bit data */
141 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
142 if (err < 0)
143 goto end;
144
145 /*
146 * Currently firewire-lib processes 16 packets in one software
147 * interrupt callback. This equals to 2msec but actually the
148 * interval of the interrupts has a jitter.
149 * Additionally, even if adding a constraint to fit period size to
150 * 2msec, actual calculated frames per period doesn't equal to 2msec,
151 * depending on sampling rate.
152 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
153 * Here let us use 5msec for safe period interrupt.
154 */
155 err = snd_pcm_hw_constraint_minmax(runtime,
156 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
157 5000, UINT_MAX);
158 if (err < 0)
159 goto end;
160
161 /* Non-Blocking stream has no more constraints */
162 if (!(s->flags & CIP_BLOCKING))
163 goto end;
164
165 /*
166 * One AMDTP packet can include some frames. In blocking mode, the
167 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
168 * depending on its sampling rate. For accurate period interrupt, it's
Yannick Guerrinice991982015-03-09 22:13:03 +0100169 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900170 *
Yannick Guerrinice991982015-03-09 22:13:03 +0100171 * TODO: These constraints can be improved with proper rules.
172 * Currently apply LCM of SYT_INTERVALs.
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900173 */
174 err = snd_pcm_hw_constraint_step(runtime, 0,
175 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
176 if (err < 0)
177 goto end;
178 err = snd_pcm_hw_constraint_step(runtime, 0,
179 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
180end:
181 return err;
182}
183EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
184
185/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900186 * amdtp_stream_set_parameters - set stream parameters
187 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100188 * @rate: the sample rate
Clemens Ladischa7304e32011-09-04 22:16:10 +0200189 * @pcm_channels: the number of PCM samples in each data block, to be encoded
190 * as AM824 multi-bit linear audio
191 * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100192 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200193 * The parameters must be set before the stream is started, and must not be
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100194 * changed while the stream is running.
195 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900196void amdtp_stream_set_parameters(struct amdtp_stream *s,
197 unsigned int rate,
198 unsigned int pcm_channels,
199 unsigned int midi_ports)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100200{
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900201 unsigned int i, sfc, midi_channels;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100202
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900203 midi_channels = DIV_ROUND_UP(midi_ports, 8);
204
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900205 if (WARN_ON(amdtp_stream_running(s)) |
206 WARN_ON(pcm_channels > AMDTP_MAX_CHANNELS_FOR_PCM) |
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900207 WARN_ON(midi_channels > AMDTP_MAX_CHANNELS_FOR_MIDI))
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100208 return;
209
Takashi Sakamotof9503a62014-05-28 00:14:36 +0900210 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc)
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900211 if (amdtp_rate_table[sfc] == rate)
Clemens Ladische84d15f2011-09-04 22:12:48 +0200212 goto sfc_found;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100213 WARN_ON(1);
Clemens Ladische84d15f2011-09-04 22:12:48 +0200214 return;
215
216sfc_found:
Takashi Sakamoto10550be2014-04-25 22:44:51 +0900217 s->pcm_channels = pcm_channels;
Clemens Ladische84d15f2011-09-04 22:12:48 +0200218 s->sfc = sfc;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900219 s->data_block_quadlets = s->pcm_channels + midi_channels;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200220 s->midi_ports = midi_ports;
221
222 s->syt_interval = amdtp_syt_intervals[sfc];
Clemens Ladische84d15f2011-09-04 22:12:48 +0200223
224 /* default buffering in the device */
225 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
226 if (s->flags & CIP_BLOCKING)
227 /* additional buffering needed to adjust for no-data packets */
228 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900229
230 /* init the position map for PCM and MIDI channels */
231 for (i = 0; i < pcm_channels; i++)
232 s->pcm_positions[i] = i;
233 s->midi_position = s->pcm_channels;
Clemens Ladisch25ca9172014-11-25 22:54:10 +0100234
235 /*
236 * We do not know the actual MIDI FIFO size of most devices. Just
237 * assume two bytes, i.e., one byte can be received over the bus while
238 * the previous one is transmitted over MIDI.
239 * (The value here is adjusted for midi_ratelimit_per_packet().)
240 */
241 s->midi_fifo_limit = rate - MIDI_BYTES_PER_SECOND * s->syt_interval + 1;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100242}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900243EXPORT_SYMBOL(amdtp_stream_set_parameters);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100244
245/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900246 * amdtp_stream_get_max_payload - get the stream's packet size
247 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100248 *
249 * This function must not be called before the stream has been configured
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900250 * with amdtp_stream_set_parameters().
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100251 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900252unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100253{
Clemens Ladische84d15f2011-09-04 22:12:48 +0200254 return 8 + s->syt_interval * s->data_block_quadlets * 4;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100255}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900256EXPORT_SYMBOL(amdtp_stream_get_max_payload);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100257
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900258static void amdtp_write_s16(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100259 struct snd_pcm_substream *pcm,
260 __be32 *buffer, unsigned int frames);
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900261static void amdtp_write_s32(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100262 struct snd_pcm_substream *pcm,
263 __be32 *buffer, unsigned int frames);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900264static void amdtp_read_s32(struct amdtp_stream *s,
265 struct snd_pcm_substream *pcm,
266 __be32 *buffer, unsigned int frames);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100267
268/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900269 * amdtp_stream_set_pcm_format - set the PCM format
270 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100271 * @format: the format of the ALSA PCM device
272 *
Yannick Guerrinice991982015-03-09 22:13:03 +0100273 * The sample format must be set after the other parameters (rate/PCM channels/
Clemens Ladischa7304e32011-09-04 22:16:10 +0200274 * MIDI) and before the stream is started, and must not be changed while the
275 * stream is running.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100276 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900277void amdtp_stream_set_pcm_format(struct amdtp_stream *s,
278 snd_pcm_format_t format)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100279{
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900280 if (WARN_ON(amdtp_stream_pcm_running(s)))
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100281 return;
282
283 switch (format) {
284 default:
285 WARN_ON(1);
286 /* fall through */
287 case SNDRV_PCM_FORMAT_S16:
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900288 if (s->direction == AMDTP_OUT_STREAM) {
Takashi Sakamoto10550be2014-04-25 22:44:51 +0900289 s->transfer_samples = amdtp_write_s16;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900290 break;
291 }
292 WARN_ON(1);
293 /* fall through */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100294 case SNDRV_PCM_FORMAT_S32:
Takashi Sakamoto10550be2014-04-25 22:44:51 +0900295 if (s->direction == AMDTP_OUT_STREAM)
296 s->transfer_samples = amdtp_write_s32;
297 else
298 s->transfer_samples = amdtp_read_s32;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100299 break;
300 }
301}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900302EXPORT_SYMBOL(amdtp_stream_set_pcm_format);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100303
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200304/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900305 * amdtp_stream_pcm_prepare - prepare PCM device for running
306 * @s: the AMDTP stream
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200307 *
308 * This function should be called from the PCM device's .prepare callback.
309 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900310void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200311{
312 tasklet_kill(&s->period_tasklet);
313 s->pcm_buffer_pointer = 0;
314 s->pcm_period_pointer = 0;
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200315 s->pointer_flush = true;
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200316}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900317EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200318
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900319static unsigned int calculate_data_blocks(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100320{
321 unsigned int phase, data_blocks;
322
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900323 if (s->flags & CIP_BLOCKING)
324 data_blocks = s->syt_interval;
325 else if (!cip_sfc_is_base_44100(s->sfc)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100326 /* Sample_rate / 8000 is an integer, and precomputed. */
327 data_blocks = s->data_block_state;
328 } else {
329 phase = s->data_block_state;
330
331 /*
332 * This calculates the number of data blocks per packet so that
333 * 1) the overall rate is correct and exactly synchronized to
334 * the bus clock, and
335 * 2) packets with a rounded-up number of blocks occur as early
336 * as possible in the sequence (to prevent underruns of the
337 * device's buffer).
338 */
339 if (s->sfc == CIP_SFC_44100)
340 /* 6 6 5 6 5 6 5 ... */
341 data_blocks = 5 + ((phase & 1) ^
342 (phase == 0 || phase >= 40));
343 else
344 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
345 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
346 if (++phase >= (80 >> (s->sfc >> 1)))
347 phase = 0;
348 s->data_block_state = phase;
349 }
350
351 return data_blocks;
352}
353
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900354static unsigned int calculate_syt(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100355 unsigned int cycle)
356{
357 unsigned int syt_offset, phase, index, syt;
358
359 if (s->last_syt_offset < TICKS_PER_CYCLE) {
360 if (!cip_sfc_is_base_44100(s->sfc))
361 syt_offset = s->last_syt_offset + s->syt_offset_state;
362 else {
363 /*
364 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
365 * n * SYT_INTERVAL * 24576000 / sample_rate
366 * Modulo TICKS_PER_CYCLE, the difference between successive
367 * elements is about 1386.23. Rounding the results of this
368 * formula to the SYT precision results in a sequence of
369 * differences that begins with:
370 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
371 * This code generates _exactly_ the same sequence.
372 */
373 phase = s->syt_offset_state;
374 index = phase % 13;
375 syt_offset = s->last_syt_offset;
376 syt_offset += 1386 + ((index && !(index & 3)) ||
377 phase == 146);
378 if (++phase >= 147)
379 phase = 0;
380 s->syt_offset_state = phase;
381 }
382 } else
383 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
384 s->last_syt_offset = syt_offset;
385
Clemens Ladischbe454362011-03-15 07:55:02 +0100386 if (syt_offset < TICKS_PER_CYCLE) {
Clemens Ladische84d15f2011-09-04 22:12:48 +0200387 syt_offset += s->transfer_delay;
Clemens Ladischbe454362011-03-15 07:55:02 +0100388 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
389 syt += syt_offset % TICKS_PER_CYCLE;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100390
Takashi Sakamotob445db42014-04-25 22:44:43 +0900391 return syt & CIP_SYT_MASK;
Clemens Ladischbe454362011-03-15 07:55:02 +0100392 } else {
Takashi Sakamotob445db42014-04-25 22:44:43 +0900393 return CIP_SYT_NO_INFO;
Clemens Ladischbe454362011-03-15 07:55:02 +0100394 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100395}
396
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900397static void amdtp_write_s32(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100398 struct snd_pcm_substream *pcm,
399 __be32 *buffer, unsigned int frames)
400{
401 struct snd_pcm_runtime *runtime = pcm->runtime;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900402 unsigned int channels, remaining_frames, i, c;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100403 const u32 *src;
404
405 channels = s->pcm_channels;
406 src = (void *)runtime->dma_area +
Takashi Sakamotoe84841f2013-09-14 00:35:47 +0900407 frames_to_bytes(runtime, s->pcm_buffer_pointer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100408 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100409
410 for (i = 0; i < frames; ++i) {
411 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900412 buffer[s->pcm_positions[c]] =
413 cpu_to_be32((*src >> 8) | 0x40000000);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100414 src++;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100415 }
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900416 buffer += s->data_block_quadlets;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100417 if (--remaining_frames == 0)
418 src = (void *)runtime->dma_area;
419 }
420}
421
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900422static void amdtp_write_s16(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100423 struct snd_pcm_substream *pcm,
424 __be32 *buffer, unsigned int frames)
425{
426 struct snd_pcm_runtime *runtime = pcm->runtime;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900427 unsigned int channels, remaining_frames, i, c;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100428 const u16 *src;
429
430 channels = s->pcm_channels;
431 src = (void *)runtime->dma_area +
Takashi Sakamotoe84841f2013-09-14 00:35:47 +0900432 frames_to_bytes(runtime, s->pcm_buffer_pointer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100433 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100434
435 for (i = 0; i < frames; ++i) {
436 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900437 buffer[s->pcm_positions[c]] =
Takashi Sakamotoa6975f22014-06-02 01:50:16 +0900438 cpu_to_be32((*src << 8) | 0x42000000);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100439 src++;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100440 }
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900441 buffer += s->data_block_quadlets;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100442 if (--remaining_frames == 0)
443 src = (void *)runtime->dma_area;
444 }
445}
446
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900447static void amdtp_read_s32(struct amdtp_stream *s,
448 struct snd_pcm_substream *pcm,
449 __be32 *buffer, unsigned int frames)
450{
451 struct snd_pcm_runtime *runtime = pcm->runtime;
452 unsigned int channels, remaining_frames, i, c;
453 u32 *dst;
454
455 channels = s->pcm_channels;
456 dst = (void *)runtime->dma_area +
457 frames_to_bytes(runtime, s->pcm_buffer_pointer);
458 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
459
460 for (i = 0; i < frames; ++i) {
461 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900462 *dst = be32_to_cpu(buffer[s->pcm_positions[c]]) << 8;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900463 dst++;
464 }
465 buffer += s->data_block_quadlets;
466 if (--remaining_frames == 0)
467 dst = (void *)runtime->dma_area;
468 }
469}
470
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900471static void amdtp_fill_pcm_silence(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100472 __be32 *buffer, unsigned int frames)
473{
474 unsigned int i, c;
475
476 for (i = 0; i < frames; ++i) {
477 for (c = 0; c < s->pcm_channels; ++c)
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900478 buffer[s->pcm_positions[c]] = cpu_to_be32(0x40000000);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100479 buffer += s->data_block_quadlets;
480 }
481}
482
Clemens Ladisch25ca9172014-11-25 22:54:10 +0100483/*
484 * To avoid sending MIDI bytes at too high a rate, assume that the receiving
485 * device has a FIFO, and track how much it is filled. This values increases
486 * by one whenever we send one byte in a packet, but the FIFO empties at
487 * a constant rate independent of our packet rate. One packet has syt_interval
488 * samples, so the number of bytes that empty out of the FIFO, per packet(!),
489 * is MIDI_BYTES_PER_SECOND * syt_interval / sample_rate. To avoid storing
490 * fractional values, the values in midi_fifo_used[] are measured in bytes
491 * multiplied by the sample rate.
492 */
493static bool midi_ratelimit_per_packet(struct amdtp_stream *s, unsigned int port)
494{
495 int used;
496
497 used = s->midi_fifo_used[port];
498 if (used == 0) /* common shortcut */
499 return true;
500
501 used -= MIDI_BYTES_PER_SECOND * s->syt_interval;
502 used = max(used, 0);
503 s->midi_fifo_used[port] = used;
504
505 return used < s->midi_fifo_limit;
506}
507
508static void midi_rate_use_one_byte(struct amdtp_stream *s, unsigned int port)
509{
510 s->midi_fifo_used[port] += amdtp_rate_table[s->sfc];
511}
512
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900513static void amdtp_fill_midi(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100514 __be32 *buffer, unsigned int frames)
515{
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900516 unsigned int f, port;
517 u8 *b;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100518
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900519 for (f = 0; f < frames; f++) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900520 b = (u8 *)&buffer[s->midi_position];
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900521
522 port = (s->data_block_counter + f) % 8;
Clemens Ladisch25ca9172014-11-25 22:54:10 +0100523 if (f < MAX_MIDI_RX_BLOCKS &&
524 midi_ratelimit_per_packet(s, port) &&
525 s->midi[port] != NULL &&
526 snd_rawmidi_transmit(s->midi[port], &b[1], 1) == 1) {
527 midi_rate_use_one_byte(s, port);
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900528 b[0] = 0x81;
Clemens Ladisch25ca9172014-11-25 22:54:10 +0100529 } else {
530 b[0] = 0x80;
531 b[1] = 0;
532 }
533 b[2] = 0;
534 b[3] = 0;
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900535
536 buffer += s->data_block_quadlets;
537 }
538}
539
540static void amdtp_pull_midi(struct amdtp_stream *s,
541 __be32 *buffer, unsigned int frames)
542{
543 unsigned int f, port;
544 int len;
545 u8 *b;
546
547 for (f = 0; f < frames; f++) {
548 port = (s->data_block_counter + f) % 8;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900549 b = (u8 *)&buffer[s->midi_position];
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900550
551 len = b[0] - 0x80;
552 if ((1 <= len) && (len <= 3) && (s->midi[port]))
553 snd_rawmidi_receive(s->midi[port], b + 1, len);
554
555 buffer += s->data_block_quadlets;
556 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100557}
558
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900559static void update_pcm_pointers(struct amdtp_stream *s,
560 struct snd_pcm_substream *pcm,
561 unsigned int frames)
Takashi Sakamoto65845f22014-08-29 13:40:45 +0900562{
563 unsigned int ptr;
564
565 /*
566 * In IEC 61883-6, one data block represents one event. In ALSA, one
567 * event equals to one PCM frame. But Dice has a quirk to transfer
568 * two PCM frames in one data block.
569 */
570 if (s->double_pcm_frames)
571 frames *= 2;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900572
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900573 ptr = s->pcm_buffer_pointer + frames;
574 if (ptr >= pcm->runtime->buffer_size)
575 ptr -= pcm->runtime->buffer_size;
576 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
577
578 s->pcm_period_pointer += frames;
579 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
580 s->pcm_period_pointer -= pcm->runtime->period_size;
581 s->pointer_flush = false;
582 tasklet_hi_schedule(&s->period_tasklet);
583 }
584}
585
586static void pcm_period_tasklet(unsigned long data)
587{
588 struct amdtp_stream *s = (void *)data;
589 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
590
591 if (pcm)
592 snd_pcm_period_elapsed(pcm);
593}
594
595static int queue_packet(struct amdtp_stream *s,
596 unsigned int header_length,
597 unsigned int payload_length, bool skip)
598{
599 struct fw_iso_packet p = {0};
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900600 int err = 0;
601
602 if (IS_ERR(s->context))
603 goto end;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900604
605 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
606 p.tag = TAG_CIP;
607 p.header_length = header_length;
608 p.payload_length = (!skip) ? payload_length : 0;
609 p.skip = skip;
610 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
611 s->buffer.packets[s->packet_index].offset);
612 if (err < 0) {
613 dev_err(&s->unit->device, "queueing error: %d\n", err);
614 goto end;
615 }
616
617 if (++s->packet_index >= QUEUE_LENGTH)
618 s->packet_index = 0;
619end:
620 return err;
621}
622
623static inline int queue_out_packet(struct amdtp_stream *s,
624 unsigned int payload_length, bool skip)
625{
626 return queue_packet(s, OUT_PACKET_HEADER_SIZE,
627 payload_length, skip);
628}
629
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900630static inline int queue_in_packet(struct amdtp_stream *s)
631{
632 return queue_packet(s, IN_PACKET_HEADER_SIZE,
633 amdtp_stream_get_max_payload(s), false);
634}
635
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900636static void handle_out_packet(struct amdtp_stream *s, unsigned int syt)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100637{
638 __be32 *buffer;
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900639 unsigned int data_blocks, payload_length;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100640 struct snd_pcm_substream *pcm;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100641
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100642 if (s->packet_index < 0)
643 return;
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100644
Takashi Sakamoto82755ab2013-11-21 14:21:06 +0900645 /* this module generate empty packet for 'no data' */
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900646 if (!(s->flags & CIP_BLOCKING) || (syt != CIP_SYT_NO_INFO))
Clemens Ladische84d15f2011-09-04 22:12:48 +0200647 data_blocks = calculate_data_blocks(s);
Takashi Sakamoto82755ab2013-11-21 14:21:06 +0900648 else
649 data_blocks = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100650
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900651 buffer = s->buffer.packets[s->packet_index].buffer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100652 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
Takashi Sakamotob445db42014-04-25 22:44:43 +0900653 (s->data_block_quadlets << AMDTP_DBS_SHIFT) |
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100654 s->data_block_counter);
655 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
Takashi Sakamotob445db42014-04-25 22:44:43 +0900656 (s->sfc << CIP_FDF_SFC_SHIFT) | syt);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100657 buffer += 2;
658
659 pcm = ACCESS_ONCE(s->pcm);
660 if (pcm)
661 s->transfer_samples(s, pcm, buffer, data_blocks);
662 else
663 amdtp_fill_pcm_silence(s, buffer, data_blocks);
664 if (s->midi_ports)
665 amdtp_fill_midi(s, buffer, data_blocks);
666
667 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
668
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900669 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
670 if (queue_out_packet(s, payload_length, false) < 0) {
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100671 s->packet_index = -1;
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900672 amdtp_stream_pcm_abort(s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100673 return;
674 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100675
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200676 if (pcm)
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900677 update_pcm_pointers(s, pcm, data_blocks);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200678}
679
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900680static void handle_in_packet(struct amdtp_stream *s,
681 unsigned int payload_quadlets,
682 __be32 *buffer)
683{
684 u32 cip_header[2];
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900685 unsigned int data_blocks, data_block_quadlets, data_block_counter,
686 dbc_interval;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900687 struct snd_pcm_substream *pcm = NULL;
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900688 bool lost;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900689
690 cip_header[0] = be32_to_cpu(buffer[0]);
691 cip_header[1] = be32_to_cpu(buffer[1]);
692
693 /*
694 * This module supports 'Two-quadlet CIP header with SYT field'.
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900695 * For convenience, also check FMT field is AM824 or not.
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900696 */
697 if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
698 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH) ||
699 ((cip_header[1] & CIP_FMT_MASK) != CIP_FMT_AM)) {
700 dev_info_ratelimited(&s->unit->device,
701 "Invalid CIP header for AMDTP: %08X:%08X\n",
702 cip_header[0], cip_header[1]);
703 goto end;
704 }
705
706 /* Calculate data blocks */
707 if (payload_quadlets < 3 ||
708 ((cip_header[1] & CIP_FDF_MASK) ==
709 (AMDTP_FDF_NO_DATA << CIP_FDF_SFC_SHIFT))) {
710 data_blocks = 0;
711 } else {
712 data_block_quadlets =
713 (cip_header[0] & AMDTP_DBS_MASK) >> AMDTP_DBS_SHIFT;
714 /* avoid division by zero */
715 if (data_block_quadlets == 0) {
716 dev_info_ratelimited(&s->unit->device,
717 "Detect invalid value in dbs field: %08X\n",
718 cip_header[0]);
719 goto err;
720 }
Takashi Sakamoto69702232014-04-25 22:45:05 +0900721 if (s->flags & CIP_WRONG_DBS)
722 data_block_quadlets = s->data_block_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900723
724 data_blocks = (payload_quadlets - 2) / data_block_quadlets;
725 }
726
727 /* Check data block counter continuity */
728 data_block_counter = cip_header[0] & AMDTP_DBC_MASK;
Takashi Sakamoto9d591242014-04-25 22:45:27 +0900729 if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
730 s->data_block_counter != UINT_MAX)
731 data_block_counter = s->data_block_counter;
732
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900733 if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) && data_block_counter == 0) ||
734 (s->data_block_counter == UINT_MAX)) {
Takashi Sakamotob84b1a22014-04-25 22:45:07 +0900735 lost = false;
736 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900737 lost = data_block_counter != s->data_block_counter;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900738 } else {
739 if ((data_blocks > 0) && (s->tx_dbc_interval > 0))
740 dbc_interval = s->tx_dbc_interval;
741 else
742 dbc_interval = data_blocks;
743
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900744 lost = data_block_counter !=
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900745 ((s->data_block_counter + dbc_interval) & 0xff);
746 }
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900747
748 if (lost) {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900749 dev_info(&s->unit->device,
750 "Detect discontinuity of CIP: %02X %02X\n",
751 s->data_block_counter, data_block_counter);
752 goto err;
753 }
754
755 if (data_blocks > 0) {
756 buffer += 2;
757
758 pcm = ACCESS_ONCE(s->pcm);
759 if (pcm)
760 s->transfer_samples(s, pcm, buffer, data_blocks);
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900761
762 if (s->midi_ports)
763 amdtp_pull_midi(s, buffer, data_blocks);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900764 }
765
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900766 if (s->flags & CIP_DBC_IS_END_EVENT)
767 s->data_block_counter = data_block_counter;
768 else
769 s->data_block_counter =
770 (data_block_counter + data_blocks) & 0xff;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900771end:
772 if (queue_in_packet(s) < 0)
773 goto err;
774
775 if (pcm)
776 update_pcm_pointers(s, pcm, data_blocks);
777
778 return;
779err:
780 s->packet_index = -1;
781 amdtp_stream_pcm_abort(s);
782}
783
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900784static void out_stream_callback(struct fw_iso_context *context, u32 cycle,
785 size_t header_length, void *header,
786 void *private_data)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100787{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900788 struct amdtp_stream *s = private_data;
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900789 unsigned int i, syt, packets = header_length / 4;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100790
791 /*
792 * Compute the cycle of the last queued packet.
793 * (We need only the four lowest bits for the SYT, so we can ignore
794 * that bits 0-11 must wrap around at 3072.)
795 */
796 cycle += QUEUE_LENGTH - packets;
797
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900798 for (i = 0; i < packets; ++i) {
799 syt = calculate_syt(s, ++cycle);
800 handle_out_packet(s, syt);
801 }
Clemens Ladisch13882a82011-05-02 09:33:56 +0200802 fw_iso_context_queue_flush(s->context);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100803}
804
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900805static void in_stream_callback(struct fw_iso_context *context, u32 cycle,
806 size_t header_length, void *header,
807 void *private_data)
808{
809 struct amdtp_stream *s = private_data;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900810 unsigned int p, syt, packets, payload_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900811 __be32 *buffer, *headers = header;
812
813 /* The number of packets in buffer */
814 packets = header_length / IN_PACKET_HEADER_SIZE;
815
816 for (p = 0; p < packets; p++) {
817 if (s->packet_index < 0)
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900818 break;
819
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900820 buffer = s->buffer.packets[s->packet_index].buffer;
821
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900822 /* Process sync slave stream */
823 if (s->sync_slave && s->sync_slave->callbacked) {
824 syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
825 handle_out_packet(s->sync_slave, syt);
826 }
827
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900828 /* The number of quadlets in this packet */
829 payload_quadlets =
830 (be32_to_cpu(headers[p]) >> ISO_DATA_LENGTH_SHIFT) / 4;
831 handle_in_packet(s, payload_quadlets, buffer);
832 }
833
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900834 /* Queueing error or detecting discontinuity */
835 if (s->packet_index < 0) {
836 /* Abort sync slave. */
837 if (s->sync_slave) {
838 s->sync_slave->packet_index = -1;
839 amdtp_stream_pcm_abort(s->sync_slave);
840 }
841 return;
842 }
843
844 /* when sync to device, flush the packets for slave stream */
845 if (s->sync_slave && s->sync_slave->callbacked)
846 fw_iso_context_queue_flush(s->sync_slave->context);
847
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900848 fw_iso_context_queue_flush(s->context);
849}
850
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900851/* processing is done by master callback */
852static void slave_stream_callback(struct fw_iso_context *context, u32 cycle,
853 size_t header_length, void *header,
854 void *private_data)
855{
856 return;
857}
858
859/* this is executed one time */
860static void amdtp_stream_first_callback(struct fw_iso_context *context,
861 u32 cycle, size_t header_length,
862 void *header, void *private_data)
863{
864 struct amdtp_stream *s = private_data;
865
866 /*
867 * For in-stream, first packet has come.
868 * For out-stream, prepared to transmit first packet
869 */
870 s->callbacked = true;
871 wake_up(&s->callback_wait);
872
873 if (s->direction == AMDTP_IN_STREAM)
874 context->callback.sc = in_stream_callback;
875 else if ((s->flags & CIP_BLOCKING) && (s->flags & CIP_SYNC_TO_DEVICE))
876 context->callback.sc = slave_stream_callback;
877 else
878 context->callback.sc = out_stream_callback;
879
880 context->callback.sc(context, cycle, header_length, header, s);
881}
882
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100883/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900884 * amdtp_stream_start - start transferring packets
885 * @s: the AMDTP stream to start
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100886 * @channel: the isochronous channel on the bus
887 * @speed: firewire speed code
888 *
889 * The stream cannot be started until it has been configured with
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900890 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
891 * device can be started.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100892 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900893int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100894{
895 static const struct {
896 unsigned int data_block;
897 unsigned int syt_offset;
898 } initial_state[] = {
899 [CIP_SFC_32000] = { 4, 3072 },
900 [CIP_SFC_48000] = { 6, 1024 },
901 [CIP_SFC_96000] = { 12, 1024 },
902 [CIP_SFC_192000] = { 24, 1024 },
903 [CIP_SFC_44100] = { 0, 67 },
904 [CIP_SFC_88200] = { 0, 67 },
905 [CIP_SFC_176400] = { 0, 67 },
906 };
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900907 unsigned int header_size;
908 enum dma_data_direction dir;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900909 int type, tag, err;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100910
911 mutex_lock(&s->mutex);
912
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900913 if (WARN_ON(amdtp_stream_running(s) ||
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900914 (s->data_block_quadlets < 1))) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100915 err = -EBADFD;
916 goto err_unlock;
917 }
918
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900919 if (s->direction == AMDTP_IN_STREAM &&
920 s->flags & CIP_SKIP_INIT_DBC_CHECK)
921 s->data_block_counter = UINT_MAX;
922 else
923 s->data_block_counter = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100924 s->data_block_state = initial_state[s->sfc].data_block;
925 s->syt_offset_state = initial_state[s->sfc].syt_offset;
926 s->last_syt_offset = TICKS_PER_CYCLE;
927
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900928 /* initialize packet buffer */
929 if (s->direction == AMDTP_IN_STREAM) {
930 dir = DMA_FROM_DEVICE;
931 type = FW_ISO_CONTEXT_RECEIVE;
932 header_size = IN_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900933 } else {
934 dir = DMA_TO_DEVICE;
935 type = FW_ISO_CONTEXT_TRANSMIT;
936 header_size = OUT_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900937 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100938 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900939 amdtp_stream_get_max_payload(s), dir);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100940 if (err < 0)
941 goto err_unlock;
942
943 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900944 type, channel, speed, header_size,
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900945 amdtp_stream_first_callback, s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100946 if (IS_ERR(s->context)) {
947 err = PTR_ERR(s->context);
948 if (err == -EBUSY)
949 dev_err(&s->unit->device,
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900950 "no free stream on this controller\n");
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100951 goto err_buffer;
952 }
953
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900954 amdtp_stream_update(s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100955
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100956 s->packet_index = 0;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900957 do {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900958 if (s->direction == AMDTP_IN_STREAM)
959 err = queue_in_packet(s);
960 else
961 err = queue_out_packet(s, 0, true);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900962 if (err < 0)
963 goto err_context;
964 } while (s->packet_index > 0);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100965
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900966 /* NOTE: TAG1 matches CIP. This just affects in stream. */
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900967 tag = FW_ISO_CONTEXT_MATCH_TAG1;
968 if (s->flags & CIP_EMPTY_WITH_TAG0)
969 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
970
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900971 s->callbacked = false;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900972 err = fw_iso_context_start(s->context, -1, 0, tag);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100973 if (err < 0)
974 goto err_context;
975
976 mutex_unlock(&s->mutex);
977
978 return 0;
979
980err_context:
981 fw_iso_context_destroy(s->context);
982 s->context = ERR_PTR(-1);
983err_buffer:
984 iso_packets_buffer_destroy(&s->buffer, s->unit);
985err_unlock:
986 mutex_unlock(&s->mutex);
987
988 return err;
989}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900990EXPORT_SYMBOL(amdtp_stream_start);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100991
992/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900993 * amdtp_stream_pcm_pointer - get the PCM buffer position
994 * @s: the AMDTP stream that transports the PCM data
Clemens Ladische9148dd2012-05-13 18:49:14 +0200995 *
996 * Returns the current buffer position, in frames.
997 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900998unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
Clemens Ladische9148dd2012-05-13 18:49:14 +0200999{
Clemens Ladisch92b862c2012-05-13 19:07:22 +02001000 /* this optimization is allowed to be racy */
Takashi Sakamotoc8de6db2014-04-25 22:44:53 +09001001 if (s->pointer_flush && amdtp_stream_running(s))
Clemens Ladisch92b862c2012-05-13 19:07:22 +02001002 fw_iso_context_flush_completions(s->context);
1003 else
1004 s->pointer_flush = true;
Clemens Ladische9148dd2012-05-13 18:49:14 +02001005
1006 return ACCESS_ONCE(s->pcm_buffer_pointer);
1007}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001008EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
Clemens Ladische9148dd2012-05-13 18:49:14 +02001009
1010/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001011 * amdtp_stream_update - update the stream after a bus reset
1012 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001013 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001014void amdtp_stream_update(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001015{
1016 ACCESS_ONCE(s->source_node_id_field) =
1017 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
1018}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001019EXPORT_SYMBOL(amdtp_stream_update);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001020
1021/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001022 * amdtp_stream_stop - stop sending packets
1023 * @s: the AMDTP stream to stop
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001024 *
1025 * All PCM and MIDI devices of the stream must be stopped before the stream
1026 * itself can be stopped.
1027 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001028void amdtp_stream_stop(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001029{
1030 mutex_lock(&s->mutex);
1031
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001032 if (!amdtp_stream_running(s)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001033 mutex_unlock(&s->mutex);
1034 return;
1035 }
1036
Clemens Ladisch76fb8782012-05-13 22:03:09 +02001037 tasklet_kill(&s->period_tasklet);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001038 fw_iso_context_stop(s->context);
1039 fw_iso_context_destroy(s->context);
1040 s->context = ERR_PTR(-1);
1041 iso_packets_buffer_destroy(&s->buffer, s->unit);
1042
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +09001043 s->callbacked = false;
1044
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001045 mutex_unlock(&s->mutex);
1046}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001047EXPORT_SYMBOL(amdtp_stream_stop);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001048
1049/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001050 * amdtp_stream_pcm_abort - abort the running PCM device
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001051 * @s: the AMDTP stream about to be stopped
1052 *
1053 * If the isochronous stream needs to be stopped asynchronously, call this
1054 * function first to stop the PCM device.
1055 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001056void amdtp_stream_pcm_abort(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001057{
1058 struct snd_pcm_substream *pcm;
1059
1060 pcm = ACCESS_ONCE(s->pcm);
Takashi Iwai1fb85102014-11-07 17:08:28 +01001061 if (pcm)
1062 snd_pcm_stop_xrun(pcm);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001063}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001064EXPORT_SYMBOL(amdtp_stream_pcm_abort);