blob: 95fc2eaf11dc0f91bcb235a2929b300c39448052 [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
24#define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 µs */
25
Takashi Sakamotob445db42014-04-25 22:44:43 +090026/* isochronous header parameters */
27#define ISO_DATA_LENGTH_SHIFT 16
Clemens Ladisch31ef9132011-03-15 07:53:21 +010028#define TAG_CIP 1
29
Takashi Sakamotob445db42014-04-25 22:44:43 +090030/* common isochronous packet header parameters */
Clemens Ladisch31ef9132011-03-15 07:53:21 +010031#define CIP_EOH (1u << 31)
Takashi Sakamotob445db42014-04-25 22:44:43 +090032#define CIP_EOH_MASK 0x80000000
Clemens Ladisch31ef9132011-03-15 07:53:21 +010033#define CIP_FMT_AM (0x10 << 24)
Takashi Sakamotob445db42014-04-25 22:44:43 +090034#define CIP_FMT_MASK 0x3f000000
35#define CIP_SYT_MASK 0x0000ffff
36#define CIP_SYT_NO_INFO 0xffff
37#define CIP_FDF_MASK 0x00ff0000
38#define CIP_FDF_SFC_SHIFT 16
39
40/*
41 * Audio and Music transfer protocol specific parameters
42 * only "Clock-based rate control mode" is supported
43 */
44#define AMDTP_FDF_AM824 (0 << (CIP_FDF_SFC_SHIFT + 3))
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090045#define AMDTP_FDF_NO_DATA 0xff
Takashi Sakamotob445db42014-04-25 22:44:43 +090046#define AMDTP_DBS_MASK 0x00ff0000
47#define AMDTP_DBS_SHIFT 16
48#define AMDTP_DBC_MASK 0x000000ff
Clemens Ladisch31ef9132011-03-15 07:53:21 +010049
50/* TODO: make these configurable */
51#define INTERRUPT_INTERVAL 16
52#define QUEUE_LENGTH 48
53
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090054#define IN_PACKET_HEADER_SIZE 4
Takashi Sakamoto4b7da112014-04-25 22:44:45 +090055#define OUT_PACKET_HEADER_SIZE 0
56
Clemens Ladisch76fb8782012-05-13 22:03:09 +020057static void pcm_period_tasklet(unsigned long data);
58
Clemens Ladisch31ef9132011-03-15 07:53:21 +010059/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090060 * amdtp_stream_init - initialize an AMDTP stream structure
61 * @s: the AMDTP stream to initialize
Clemens Ladisch31ef9132011-03-15 07:53:21 +010062 * @unit: the target of the stream
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090063 * @dir: the direction of stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +010064 * @flags: the packet transmission method to use
65 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090066int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090067 enum amdtp_stream_direction dir, enum cip_flags flags)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010068{
Clemens Ladisch31ef9132011-03-15 07:53:21 +010069 s->unit = fw_unit_get(unit);
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090070 s->direction = dir;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010071 s->flags = flags;
72 s->context = ERR_PTR(-1);
73 mutex_init(&s->mutex);
Clemens Ladisch76fb8782012-05-13 22:03:09 +020074 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +010075 s->packet_index = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010076
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +090077 init_waitqueue_head(&s->callback_wait);
78 s->callbacked = false;
79 s->sync_slave = NULL;
80
Takashi Sakamoto53111cd2014-04-25 22:45:10 +090081 s->rx_blocks_for_midi = UINT_MAX;
82
Clemens Ladisch31ef9132011-03-15 07:53:21 +010083 return 0;
84}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090085EXPORT_SYMBOL(amdtp_stream_init);
Clemens Ladisch31ef9132011-03-15 07:53:21 +010086
87/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090088 * amdtp_stream_destroy - free stream resources
89 * @s: the AMDTP stream to destroy
Clemens Ladisch31ef9132011-03-15 07:53:21 +010090 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090091void amdtp_stream_destroy(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010092{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090093 WARN_ON(amdtp_stream_running(s));
Clemens Ladisch31ef9132011-03-15 07:53:21 +010094 mutex_destroy(&s->mutex);
95 fw_unit_put(s->unit);
96}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090097EXPORT_SYMBOL(amdtp_stream_destroy);
Clemens Ladisch31ef9132011-03-15 07:53:21 +010098
Clemens Ladischc5280e92011-10-16 21:39:00 +020099const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
Clemens Ladischa7304e32011-09-04 22:16:10 +0200100 [CIP_SFC_32000] = 8,
101 [CIP_SFC_44100] = 8,
102 [CIP_SFC_48000] = 8,
103 [CIP_SFC_88200] = 16,
104 [CIP_SFC_96000] = 16,
105 [CIP_SFC_176400] = 32,
106 [CIP_SFC_192000] = 32,
107};
108EXPORT_SYMBOL(amdtp_syt_intervals);
109
Takashi Sakamotof9503a62014-05-28 00:14:36 +0900110const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900111 [CIP_SFC_32000] = 32000,
112 [CIP_SFC_44100] = 44100,
113 [CIP_SFC_48000] = 48000,
114 [CIP_SFC_88200] = 88200,
115 [CIP_SFC_96000] = 96000,
116 [CIP_SFC_176400] = 176400,
117 [CIP_SFC_192000] = 192000,
118};
119EXPORT_SYMBOL(amdtp_rate_table);
120
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100121/**
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900122 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
123 * @s: the AMDTP stream, which must be initialized.
124 * @runtime: the PCM substream runtime
125 */
126int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
127 struct snd_pcm_runtime *runtime)
128{
129 int err;
130
131 /* AM824 in IEC 61883-6 can deliver 24bit data */
132 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
133 if (err < 0)
134 goto end;
135
136 /*
137 * Currently firewire-lib processes 16 packets in one software
138 * interrupt callback. This equals to 2msec but actually the
139 * interval of the interrupts has a jitter.
140 * Additionally, even if adding a constraint to fit period size to
141 * 2msec, actual calculated frames per period doesn't equal to 2msec,
142 * depending on sampling rate.
143 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
144 * Here let us use 5msec for safe period interrupt.
145 */
146 err = snd_pcm_hw_constraint_minmax(runtime,
147 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
148 5000, UINT_MAX);
149 if (err < 0)
150 goto end;
151
152 /* Non-Blocking stream has no more constraints */
153 if (!(s->flags & CIP_BLOCKING))
154 goto end;
155
156 /*
157 * One AMDTP packet can include some frames. In blocking mode, the
158 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
159 * depending on its sampling rate. For accurate period interrupt, it's
160 * preferrable to aligh period/buffer sizes to current SYT_INTERVAL.
161 *
162 * TODO: These constraints can be improved with propper rules.
163 * Currently apply LCM of SYT_INTEVALs.
164 */
165 err = snd_pcm_hw_constraint_step(runtime, 0,
166 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
167 if (err < 0)
168 goto end;
169 err = snd_pcm_hw_constraint_step(runtime, 0,
170 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
171end:
172 return err;
173}
174EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
175
176/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900177 * amdtp_stream_set_parameters - set stream parameters
178 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100179 * @rate: the sample rate
Clemens Ladischa7304e32011-09-04 22:16:10 +0200180 * @pcm_channels: the number of PCM samples in each data block, to be encoded
181 * as AM824 multi-bit linear audio
182 * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100183 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200184 * The parameters must be set before the stream is started, and must not be
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100185 * changed while the stream is running.
186 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900187void amdtp_stream_set_parameters(struct amdtp_stream *s,
188 unsigned int rate,
189 unsigned int pcm_channels,
190 unsigned int midi_ports)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100191{
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900192 unsigned int i, sfc, midi_channels;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100193
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900194 midi_channels = DIV_ROUND_UP(midi_ports, 8);
195
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900196 if (WARN_ON(amdtp_stream_running(s)) |
197 WARN_ON(pcm_channels > AMDTP_MAX_CHANNELS_FOR_PCM) |
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900198 WARN_ON(midi_channels > AMDTP_MAX_CHANNELS_FOR_MIDI))
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100199 return;
200
Takashi Sakamotof9503a62014-05-28 00:14:36 +0900201 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc)
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900202 if (amdtp_rate_table[sfc] == rate)
Clemens Ladische84d15f2011-09-04 22:12:48 +0200203 goto sfc_found;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100204 WARN_ON(1);
Clemens Ladische84d15f2011-09-04 22:12:48 +0200205 return;
206
207sfc_found:
Takashi Sakamoto10550be2014-04-25 22:44:51 +0900208 s->pcm_channels = pcm_channels;
Clemens Ladische84d15f2011-09-04 22:12:48 +0200209 s->sfc = sfc;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900210 s->data_block_quadlets = s->pcm_channels + midi_channels;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200211 s->midi_ports = midi_ports;
212
213 s->syt_interval = amdtp_syt_intervals[sfc];
Clemens Ladische84d15f2011-09-04 22:12:48 +0200214
215 /* default buffering in the device */
216 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
217 if (s->flags & CIP_BLOCKING)
218 /* additional buffering needed to adjust for no-data packets */
219 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900220
221 /* init the position map for PCM and MIDI channels */
222 for (i = 0; i < pcm_channels; i++)
223 s->pcm_positions[i] = i;
224 s->midi_position = s->pcm_channels;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100225}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900226EXPORT_SYMBOL(amdtp_stream_set_parameters);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100227
228/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900229 * amdtp_stream_get_max_payload - get the stream's packet size
230 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100231 *
232 * This function must not be called before the stream has been configured
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900233 * with amdtp_stream_set_parameters().
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100234 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900235unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100236{
Clemens Ladische84d15f2011-09-04 22:12:48 +0200237 return 8 + s->syt_interval * s->data_block_quadlets * 4;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100238}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900239EXPORT_SYMBOL(amdtp_stream_get_max_payload);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100240
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900241static void amdtp_write_s16(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100242 struct snd_pcm_substream *pcm,
243 __be32 *buffer, unsigned int frames);
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900244static void amdtp_write_s32(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100245 struct snd_pcm_substream *pcm,
246 __be32 *buffer, unsigned int frames);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900247static void amdtp_read_s32(struct amdtp_stream *s,
248 struct snd_pcm_substream *pcm,
249 __be32 *buffer, unsigned int frames);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100250
251/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900252 * amdtp_stream_set_pcm_format - set the PCM format
253 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100254 * @format: the format of the ALSA PCM device
255 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200256 * The sample format must be set after the other paramters (rate/PCM channels/
257 * MIDI) and before the stream is started, and must not be changed while the
258 * stream is running.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100259 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900260void amdtp_stream_set_pcm_format(struct amdtp_stream *s,
261 snd_pcm_format_t format)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100262{
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900263 if (WARN_ON(amdtp_stream_pcm_running(s)))
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100264 return;
265
266 switch (format) {
267 default:
268 WARN_ON(1);
269 /* fall through */
270 case SNDRV_PCM_FORMAT_S16:
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900271 if (s->direction == AMDTP_OUT_STREAM) {
Takashi Sakamoto10550be2014-04-25 22:44:51 +0900272 s->transfer_samples = amdtp_write_s16;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900273 break;
274 }
275 WARN_ON(1);
276 /* fall through */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100277 case SNDRV_PCM_FORMAT_S32:
Takashi Sakamoto10550be2014-04-25 22:44:51 +0900278 if (s->direction == AMDTP_OUT_STREAM)
279 s->transfer_samples = amdtp_write_s32;
280 else
281 s->transfer_samples = amdtp_read_s32;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100282 break;
283 }
284}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900285EXPORT_SYMBOL(amdtp_stream_set_pcm_format);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100286
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200287/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900288 * amdtp_stream_pcm_prepare - prepare PCM device for running
289 * @s: the AMDTP stream
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200290 *
291 * This function should be called from the PCM device's .prepare callback.
292 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900293void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200294{
295 tasklet_kill(&s->period_tasklet);
296 s->pcm_buffer_pointer = 0;
297 s->pcm_period_pointer = 0;
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200298 s->pointer_flush = true;
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200299}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900300EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200301
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900302static unsigned int calculate_data_blocks(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100303{
304 unsigned int phase, data_blocks;
305
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900306 if (s->flags & CIP_BLOCKING)
307 data_blocks = s->syt_interval;
308 else if (!cip_sfc_is_base_44100(s->sfc)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100309 /* Sample_rate / 8000 is an integer, and precomputed. */
310 data_blocks = s->data_block_state;
311 } else {
312 phase = s->data_block_state;
313
314 /*
315 * This calculates the number of data blocks per packet so that
316 * 1) the overall rate is correct and exactly synchronized to
317 * the bus clock, and
318 * 2) packets with a rounded-up number of blocks occur as early
319 * as possible in the sequence (to prevent underruns of the
320 * device's buffer).
321 */
322 if (s->sfc == CIP_SFC_44100)
323 /* 6 6 5 6 5 6 5 ... */
324 data_blocks = 5 + ((phase & 1) ^
325 (phase == 0 || phase >= 40));
326 else
327 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
328 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
329 if (++phase >= (80 >> (s->sfc >> 1)))
330 phase = 0;
331 s->data_block_state = phase;
332 }
333
334 return data_blocks;
335}
336
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900337static unsigned int calculate_syt(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100338 unsigned int cycle)
339{
340 unsigned int syt_offset, phase, index, syt;
341
342 if (s->last_syt_offset < TICKS_PER_CYCLE) {
343 if (!cip_sfc_is_base_44100(s->sfc))
344 syt_offset = s->last_syt_offset + s->syt_offset_state;
345 else {
346 /*
347 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
348 * n * SYT_INTERVAL * 24576000 / sample_rate
349 * Modulo TICKS_PER_CYCLE, the difference between successive
350 * elements is about 1386.23. Rounding the results of this
351 * formula to the SYT precision results in a sequence of
352 * differences that begins with:
353 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
354 * This code generates _exactly_ the same sequence.
355 */
356 phase = s->syt_offset_state;
357 index = phase % 13;
358 syt_offset = s->last_syt_offset;
359 syt_offset += 1386 + ((index && !(index & 3)) ||
360 phase == 146);
361 if (++phase >= 147)
362 phase = 0;
363 s->syt_offset_state = phase;
364 }
365 } else
366 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
367 s->last_syt_offset = syt_offset;
368
Clemens Ladischbe454362011-03-15 07:55:02 +0100369 if (syt_offset < TICKS_PER_CYCLE) {
Clemens Ladische84d15f2011-09-04 22:12:48 +0200370 syt_offset += s->transfer_delay;
Clemens Ladischbe454362011-03-15 07:55:02 +0100371 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
372 syt += syt_offset % TICKS_PER_CYCLE;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100373
Takashi Sakamotob445db42014-04-25 22:44:43 +0900374 return syt & CIP_SYT_MASK;
Clemens Ladischbe454362011-03-15 07:55:02 +0100375 } else {
Takashi Sakamotob445db42014-04-25 22:44:43 +0900376 return CIP_SYT_NO_INFO;
Clemens Ladischbe454362011-03-15 07:55:02 +0100377 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100378}
379
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900380static void amdtp_write_s32(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100381 struct snd_pcm_substream *pcm,
382 __be32 *buffer, unsigned int frames)
383{
384 struct snd_pcm_runtime *runtime = pcm->runtime;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900385 unsigned int channels, remaining_frames, i, c;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100386 const u32 *src;
387
388 channels = s->pcm_channels;
389 src = (void *)runtime->dma_area +
Takashi Sakamotoe84841f2013-09-14 00:35:47 +0900390 frames_to_bytes(runtime, s->pcm_buffer_pointer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100391 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100392
393 for (i = 0; i < frames; ++i) {
394 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900395 buffer[s->pcm_positions[c]] =
396 cpu_to_be32((*src >> 8) | 0x40000000);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100397 src++;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100398 }
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900399 buffer += s->data_block_quadlets;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100400 if (--remaining_frames == 0)
401 src = (void *)runtime->dma_area;
402 }
403}
404
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900405static void amdtp_write_s16(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100406 struct snd_pcm_substream *pcm,
407 __be32 *buffer, unsigned int frames)
408{
409 struct snd_pcm_runtime *runtime = pcm->runtime;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900410 unsigned int channels, remaining_frames, i, c;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100411 const u16 *src;
412
413 channels = s->pcm_channels;
414 src = (void *)runtime->dma_area +
Takashi Sakamotoe84841f2013-09-14 00:35:47 +0900415 frames_to_bytes(runtime, s->pcm_buffer_pointer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100416 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100417
418 for (i = 0; i < frames; ++i) {
419 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900420 buffer[s->pcm_positions[c]] =
Takashi Sakamotoa6975f22014-06-02 01:50:16 +0900421 cpu_to_be32((*src << 8) | 0x42000000);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100422 src++;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100423 }
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900424 buffer += s->data_block_quadlets;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100425 if (--remaining_frames == 0)
426 src = (void *)runtime->dma_area;
427 }
428}
429
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900430static void amdtp_read_s32(struct amdtp_stream *s,
431 struct snd_pcm_substream *pcm,
432 __be32 *buffer, unsigned int frames)
433{
434 struct snd_pcm_runtime *runtime = pcm->runtime;
435 unsigned int channels, remaining_frames, i, c;
436 u32 *dst;
437
438 channels = s->pcm_channels;
439 dst = (void *)runtime->dma_area +
440 frames_to_bytes(runtime, s->pcm_buffer_pointer);
441 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
442
443 for (i = 0; i < frames; ++i) {
444 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900445 *dst = be32_to_cpu(buffer[s->pcm_positions[c]]) << 8;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900446 dst++;
447 }
448 buffer += s->data_block_quadlets;
449 if (--remaining_frames == 0)
450 dst = (void *)runtime->dma_area;
451 }
452}
453
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900454static void amdtp_fill_pcm_silence(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100455 __be32 *buffer, unsigned int frames)
456{
457 unsigned int i, c;
458
459 for (i = 0; i < frames; ++i) {
460 for (c = 0; c < s->pcm_channels; ++c)
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900461 buffer[s->pcm_positions[c]] = cpu_to_be32(0x40000000);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100462 buffer += s->data_block_quadlets;
463 }
464}
465
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900466static void amdtp_fill_midi(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100467 __be32 *buffer, unsigned int frames)
468{
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900469 unsigned int f, port;
470 u8 *b;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100471
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900472 for (f = 0; f < frames; f++) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900473 buffer[s->midi_position] = 0;
474 b = (u8 *)&buffer[s->midi_position];
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900475
476 port = (s->data_block_counter + f) % 8;
Takashi Sakamoto53111cd2014-04-25 22:45:10 +0900477 if ((f >= s->rx_blocks_for_midi) ||
478 (s->midi[port] == NULL) ||
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900479 (snd_rawmidi_transmit(s->midi[port], b + 1, 1) <= 0))
480 b[0] = 0x80;
481 else
482 b[0] = 0x81;
483
484 buffer += s->data_block_quadlets;
485 }
486}
487
488static void amdtp_pull_midi(struct amdtp_stream *s,
489 __be32 *buffer, unsigned int frames)
490{
491 unsigned int f, port;
492 int len;
493 u8 *b;
494
495 for (f = 0; f < frames; f++) {
496 port = (s->data_block_counter + f) % 8;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900497 b = (u8 *)&buffer[s->midi_position];
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900498
499 len = b[0] - 0x80;
500 if ((1 <= len) && (len <= 3) && (s->midi[port]))
501 snd_rawmidi_receive(s->midi[port], b + 1, len);
502
503 buffer += s->data_block_quadlets;
504 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100505}
506
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900507static void update_pcm_pointers(struct amdtp_stream *s,
508 struct snd_pcm_substream *pcm,
509 unsigned int frames)
Takashi Sakamoto65845f22014-08-29 13:40:45 +0900510{
511 unsigned int ptr;
512
513 /*
514 * In IEC 61883-6, one data block represents one event. In ALSA, one
515 * event equals to one PCM frame. But Dice has a quirk to transfer
516 * two PCM frames in one data block.
517 */
518 if (s->double_pcm_frames)
519 frames *= 2;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900520
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900521 ptr = s->pcm_buffer_pointer + frames;
522 if (ptr >= pcm->runtime->buffer_size)
523 ptr -= pcm->runtime->buffer_size;
524 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
525
526 s->pcm_period_pointer += frames;
527 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
528 s->pcm_period_pointer -= pcm->runtime->period_size;
529 s->pointer_flush = false;
530 tasklet_hi_schedule(&s->period_tasklet);
531 }
532}
533
534static void pcm_period_tasklet(unsigned long data)
535{
536 struct amdtp_stream *s = (void *)data;
537 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
538
539 if (pcm)
540 snd_pcm_period_elapsed(pcm);
541}
542
543static int queue_packet(struct amdtp_stream *s,
544 unsigned int header_length,
545 unsigned int payload_length, bool skip)
546{
547 struct fw_iso_packet p = {0};
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900548 int err = 0;
549
550 if (IS_ERR(s->context))
551 goto end;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900552
553 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
554 p.tag = TAG_CIP;
555 p.header_length = header_length;
556 p.payload_length = (!skip) ? payload_length : 0;
557 p.skip = skip;
558 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
559 s->buffer.packets[s->packet_index].offset);
560 if (err < 0) {
561 dev_err(&s->unit->device, "queueing error: %d\n", err);
562 goto end;
563 }
564
565 if (++s->packet_index >= QUEUE_LENGTH)
566 s->packet_index = 0;
567end:
568 return err;
569}
570
571static inline int queue_out_packet(struct amdtp_stream *s,
572 unsigned int payload_length, bool skip)
573{
574 return queue_packet(s, OUT_PACKET_HEADER_SIZE,
575 payload_length, skip);
576}
577
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900578static inline int queue_in_packet(struct amdtp_stream *s)
579{
580 return queue_packet(s, IN_PACKET_HEADER_SIZE,
581 amdtp_stream_get_max_payload(s), false);
582}
583
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900584static void handle_out_packet(struct amdtp_stream *s, unsigned int syt)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100585{
586 __be32 *buffer;
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900587 unsigned int data_blocks, payload_length;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100588 struct snd_pcm_substream *pcm;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100589
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100590 if (s->packet_index < 0)
591 return;
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100592
Takashi Sakamoto82755ab2013-11-21 14:21:06 +0900593 /* this module generate empty packet for 'no data' */
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900594 if (!(s->flags & CIP_BLOCKING) || (syt != CIP_SYT_NO_INFO))
Clemens Ladische84d15f2011-09-04 22:12:48 +0200595 data_blocks = calculate_data_blocks(s);
Takashi Sakamoto82755ab2013-11-21 14:21:06 +0900596 else
597 data_blocks = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100598
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900599 buffer = s->buffer.packets[s->packet_index].buffer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100600 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
Takashi Sakamotob445db42014-04-25 22:44:43 +0900601 (s->data_block_quadlets << AMDTP_DBS_SHIFT) |
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100602 s->data_block_counter);
603 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
Takashi Sakamotob445db42014-04-25 22:44:43 +0900604 (s->sfc << CIP_FDF_SFC_SHIFT) | syt);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100605 buffer += 2;
606
607 pcm = ACCESS_ONCE(s->pcm);
608 if (pcm)
609 s->transfer_samples(s, pcm, buffer, data_blocks);
610 else
611 amdtp_fill_pcm_silence(s, buffer, data_blocks);
612 if (s->midi_ports)
613 amdtp_fill_midi(s, buffer, data_blocks);
614
615 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
616
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900617 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
618 if (queue_out_packet(s, payload_length, false) < 0) {
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100619 s->packet_index = -1;
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900620 amdtp_stream_pcm_abort(s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100621 return;
622 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100623
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200624 if (pcm)
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900625 update_pcm_pointers(s, pcm, data_blocks);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200626}
627
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900628static void handle_in_packet(struct amdtp_stream *s,
629 unsigned int payload_quadlets,
630 __be32 *buffer)
631{
632 u32 cip_header[2];
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900633 unsigned int data_blocks, data_block_quadlets, data_block_counter,
634 dbc_interval;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900635 struct snd_pcm_substream *pcm = NULL;
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900636 bool lost;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900637
638 cip_header[0] = be32_to_cpu(buffer[0]);
639 cip_header[1] = be32_to_cpu(buffer[1]);
640
641 /*
642 * This module supports 'Two-quadlet CIP header with SYT field'.
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900643 * For convenience, also check FMT field is AM824 or not.
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900644 */
645 if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
646 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH) ||
647 ((cip_header[1] & CIP_FMT_MASK) != CIP_FMT_AM)) {
648 dev_info_ratelimited(&s->unit->device,
649 "Invalid CIP header for AMDTP: %08X:%08X\n",
650 cip_header[0], cip_header[1]);
651 goto end;
652 }
653
654 /* Calculate data blocks */
655 if (payload_quadlets < 3 ||
656 ((cip_header[1] & CIP_FDF_MASK) ==
657 (AMDTP_FDF_NO_DATA << CIP_FDF_SFC_SHIFT))) {
658 data_blocks = 0;
659 } else {
660 data_block_quadlets =
661 (cip_header[0] & AMDTP_DBS_MASK) >> AMDTP_DBS_SHIFT;
662 /* avoid division by zero */
663 if (data_block_quadlets == 0) {
664 dev_info_ratelimited(&s->unit->device,
665 "Detect invalid value in dbs field: %08X\n",
666 cip_header[0]);
667 goto err;
668 }
Takashi Sakamoto69702232014-04-25 22:45:05 +0900669 if (s->flags & CIP_WRONG_DBS)
670 data_block_quadlets = s->data_block_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900671
672 data_blocks = (payload_quadlets - 2) / data_block_quadlets;
673 }
674
675 /* Check data block counter continuity */
676 data_block_counter = cip_header[0] & AMDTP_DBC_MASK;
Takashi Sakamoto9d591242014-04-25 22:45:27 +0900677 if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
678 s->data_block_counter != UINT_MAX)
679 data_block_counter = s->data_block_counter;
680
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900681 if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) && data_block_counter == 0) ||
682 (s->data_block_counter == UINT_MAX)) {
Takashi Sakamotob84b1a22014-04-25 22:45:07 +0900683 lost = false;
684 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900685 lost = data_block_counter != s->data_block_counter;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900686 } else {
687 if ((data_blocks > 0) && (s->tx_dbc_interval > 0))
688 dbc_interval = s->tx_dbc_interval;
689 else
690 dbc_interval = data_blocks;
691
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900692 lost = data_block_counter !=
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900693 ((s->data_block_counter + dbc_interval) & 0xff);
694 }
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900695
696 if (lost) {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900697 dev_info(&s->unit->device,
698 "Detect discontinuity of CIP: %02X %02X\n",
699 s->data_block_counter, data_block_counter);
700 goto err;
701 }
702
703 if (data_blocks > 0) {
704 buffer += 2;
705
706 pcm = ACCESS_ONCE(s->pcm);
707 if (pcm)
708 s->transfer_samples(s, pcm, buffer, data_blocks);
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900709
710 if (s->midi_ports)
711 amdtp_pull_midi(s, buffer, data_blocks);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900712 }
713
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900714 if (s->flags & CIP_DBC_IS_END_EVENT)
715 s->data_block_counter = data_block_counter;
716 else
717 s->data_block_counter =
718 (data_block_counter + data_blocks) & 0xff;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900719end:
720 if (queue_in_packet(s) < 0)
721 goto err;
722
723 if (pcm)
724 update_pcm_pointers(s, pcm, data_blocks);
725
726 return;
727err:
728 s->packet_index = -1;
729 amdtp_stream_pcm_abort(s);
730}
731
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900732static void out_stream_callback(struct fw_iso_context *context, u32 cycle,
733 size_t header_length, void *header,
734 void *private_data)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100735{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900736 struct amdtp_stream *s = private_data;
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900737 unsigned int i, syt, packets = header_length / 4;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100738
739 /*
740 * Compute the cycle of the last queued packet.
741 * (We need only the four lowest bits for the SYT, so we can ignore
742 * that bits 0-11 must wrap around at 3072.)
743 */
744 cycle += QUEUE_LENGTH - packets;
745
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900746 for (i = 0; i < packets; ++i) {
747 syt = calculate_syt(s, ++cycle);
748 handle_out_packet(s, syt);
749 }
Clemens Ladisch13882a82011-05-02 09:33:56 +0200750 fw_iso_context_queue_flush(s->context);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100751}
752
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900753static void in_stream_callback(struct fw_iso_context *context, u32 cycle,
754 size_t header_length, void *header,
755 void *private_data)
756{
757 struct amdtp_stream *s = private_data;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900758 unsigned int p, syt, packets, payload_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900759 __be32 *buffer, *headers = header;
760
761 /* The number of packets in buffer */
762 packets = header_length / IN_PACKET_HEADER_SIZE;
763
764 for (p = 0; p < packets; p++) {
765 if (s->packet_index < 0)
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900766 break;
767
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900768 buffer = s->buffer.packets[s->packet_index].buffer;
769
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900770 /* Process sync slave stream */
771 if (s->sync_slave && s->sync_slave->callbacked) {
772 syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
773 handle_out_packet(s->sync_slave, syt);
774 }
775
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900776 /* The number of quadlets in this packet */
777 payload_quadlets =
778 (be32_to_cpu(headers[p]) >> ISO_DATA_LENGTH_SHIFT) / 4;
779 handle_in_packet(s, payload_quadlets, buffer);
780 }
781
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900782 /* Queueing error or detecting discontinuity */
783 if (s->packet_index < 0) {
784 /* Abort sync slave. */
785 if (s->sync_slave) {
786 s->sync_slave->packet_index = -1;
787 amdtp_stream_pcm_abort(s->sync_slave);
788 }
789 return;
790 }
791
792 /* when sync to device, flush the packets for slave stream */
793 if (s->sync_slave && s->sync_slave->callbacked)
794 fw_iso_context_queue_flush(s->sync_slave->context);
795
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900796 fw_iso_context_queue_flush(s->context);
797}
798
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900799/* processing is done by master callback */
800static void slave_stream_callback(struct fw_iso_context *context, u32 cycle,
801 size_t header_length, void *header,
802 void *private_data)
803{
804 return;
805}
806
807/* this is executed one time */
808static void amdtp_stream_first_callback(struct fw_iso_context *context,
809 u32 cycle, size_t header_length,
810 void *header, void *private_data)
811{
812 struct amdtp_stream *s = private_data;
813
814 /*
815 * For in-stream, first packet has come.
816 * For out-stream, prepared to transmit first packet
817 */
818 s->callbacked = true;
819 wake_up(&s->callback_wait);
820
821 if (s->direction == AMDTP_IN_STREAM)
822 context->callback.sc = in_stream_callback;
823 else if ((s->flags & CIP_BLOCKING) && (s->flags & CIP_SYNC_TO_DEVICE))
824 context->callback.sc = slave_stream_callback;
825 else
826 context->callback.sc = out_stream_callback;
827
828 context->callback.sc(context, cycle, header_length, header, s);
829}
830
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100831/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900832 * amdtp_stream_start - start transferring packets
833 * @s: the AMDTP stream to start
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100834 * @channel: the isochronous channel on the bus
835 * @speed: firewire speed code
836 *
837 * The stream cannot be started until it has been configured with
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900838 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
839 * device can be started.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100840 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900841int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100842{
843 static const struct {
844 unsigned int data_block;
845 unsigned int syt_offset;
846 } initial_state[] = {
847 [CIP_SFC_32000] = { 4, 3072 },
848 [CIP_SFC_48000] = { 6, 1024 },
849 [CIP_SFC_96000] = { 12, 1024 },
850 [CIP_SFC_192000] = { 24, 1024 },
851 [CIP_SFC_44100] = { 0, 67 },
852 [CIP_SFC_88200] = { 0, 67 },
853 [CIP_SFC_176400] = { 0, 67 },
854 };
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900855 unsigned int header_size;
856 enum dma_data_direction dir;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900857 int type, tag, err;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100858
859 mutex_lock(&s->mutex);
860
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900861 if (WARN_ON(amdtp_stream_running(s) ||
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900862 (s->data_block_quadlets < 1))) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100863 err = -EBADFD;
864 goto err_unlock;
865 }
866
Takashi Sakamotob6bc8122014-04-25 22:45:16 +0900867 if (s->direction == AMDTP_IN_STREAM &&
868 s->flags & CIP_SKIP_INIT_DBC_CHECK)
869 s->data_block_counter = UINT_MAX;
870 else
871 s->data_block_counter = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100872 s->data_block_state = initial_state[s->sfc].data_block;
873 s->syt_offset_state = initial_state[s->sfc].syt_offset;
874 s->last_syt_offset = TICKS_PER_CYCLE;
875
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900876 /* initialize packet buffer */
877 if (s->direction == AMDTP_IN_STREAM) {
878 dir = DMA_FROM_DEVICE;
879 type = FW_ISO_CONTEXT_RECEIVE;
880 header_size = IN_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900881 } else {
882 dir = DMA_TO_DEVICE;
883 type = FW_ISO_CONTEXT_TRANSMIT;
884 header_size = OUT_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900885 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100886 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900887 amdtp_stream_get_max_payload(s), dir);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100888 if (err < 0)
889 goto err_unlock;
890
891 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900892 type, channel, speed, header_size,
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900893 amdtp_stream_first_callback, s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100894 if (IS_ERR(s->context)) {
895 err = PTR_ERR(s->context);
896 if (err == -EBUSY)
897 dev_err(&s->unit->device,
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900898 "no free stream on this controller\n");
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100899 goto err_buffer;
900 }
901
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900902 amdtp_stream_update(s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100903
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100904 s->packet_index = 0;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900905 do {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900906 if (s->direction == AMDTP_IN_STREAM)
907 err = queue_in_packet(s);
908 else
909 err = queue_out_packet(s, 0, true);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900910 if (err < 0)
911 goto err_context;
912 } while (s->packet_index > 0);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100913
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900914 /* NOTE: TAG1 matches CIP. This just affects in stream. */
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900915 tag = FW_ISO_CONTEXT_MATCH_TAG1;
916 if (s->flags & CIP_EMPTY_WITH_TAG0)
917 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
918
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900919 s->callbacked = false;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900920 err = fw_iso_context_start(s->context, -1, 0, tag);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100921 if (err < 0)
922 goto err_context;
923
924 mutex_unlock(&s->mutex);
925
926 return 0;
927
928err_context:
929 fw_iso_context_destroy(s->context);
930 s->context = ERR_PTR(-1);
931err_buffer:
932 iso_packets_buffer_destroy(&s->buffer, s->unit);
933err_unlock:
934 mutex_unlock(&s->mutex);
935
936 return err;
937}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900938EXPORT_SYMBOL(amdtp_stream_start);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100939
940/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900941 * amdtp_stream_pcm_pointer - get the PCM buffer position
942 * @s: the AMDTP stream that transports the PCM data
Clemens Ladische9148dd2012-05-13 18:49:14 +0200943 *
944 * Returns the current buffer position, in frames.
945 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900946unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
Clemens Ladische9148dd2012-05-13 18:49:14 +0200947{
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200948 /* this optimization is allowed to be racy */
Takashi Sakamotoc8de6db2014-04-25 22:44:53 +0900949 if (s->pointer_flush && amdtp_stream_running(s))
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200950 fw_iso_context_flush_completions(s->context);
951 else
952 s->pointer_flush = true;
Clemens Ladische9148dd2012-05-13 18:49:14 +0200953
954 return ACCESS_ONCE(s->pcm_buffer_pointer);
955}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900956EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
Clemens Ladische9148dd2012-05-13 18:49:14 +0200957
958/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900959 * amdtp_stream_update - update the stream after a bus reset
960 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100961 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900962void amdtp_stream_update(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100963{
964 ACCESS_ONCE(s->source_node_id_field) =
965 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
966}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900967EXPORT_SYMBOL(amdtp_stream_update);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100968
969/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900970 * amdtp_stream_stop - stop sending packets
971 * @s: the AMDTP stream to stop
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100972 *
973 * All PCM and MIDI devices of the stream must be stopped before the stream
974 * itself can be stopped.
975 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900976void amdtp_stream_stop(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100977{
978 mutex_lock(&s->mutex);
979
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900980 if (!amdtp_stream_running(s)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100981 mutex_unlock(&s->mutex);
982 return;
983 }
984
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200985 tasklet_kill(&s->period_tasklet);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100986 fw_iso_context_stop(s->context);
987 fw_iso_context_destroy(s->context);
988 s->context = ERR_PTR(-1);
989 iso_packets_buffer_destroy(&s->buffer, s->unit);
990
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900991 s->callbacked = false;
992
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100993 mutex_unlock(&s->mutex);
994}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900995EXPORT_SYMBOL(amdtp_stream_stop);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100996
997/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900998 * amdtp_stream_pcm_abort - abort the running PCM device
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100999 * @s: the AMDTP stream about to be stopped
1000 *
1001 * If the isochronous stream needs to be stopped asynchronously, call this
1002 * function first to stop the PCM device.
1003 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001004void amdtp_stream_pcm_abort(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001005{
1006 struct snd_pcm_substream *pcm;
1007
1008 pcm = ACCESS_ONCE(s->pcm);
1009 if (pcm) {
1010 snd_pcm_stream_lock_irq(pcm);
1011 if (snd_pcm_running(pcm))
1012 snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
1013 snd_pcm_stream_unlock_irq(pcm);
1014 }
1015}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001016EXPORT_SYMBOL(amdtp_stream_pcm_abort);