blob: 9cf81b2b42980772311990c524fcf52c4f45bd55 [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 Sakamoto83d8d722014-04-25 22:44:47 +090016#include <sound/rawmidi.h>
Clemens Ladisch31ef9132011-03-15 07:53:21 +010017#include "amdtp.h"
18
19#define TICKS_PER_CYCLE 3072
20#define CYCLES_PER_SECOND 8000
21#define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
22
23#define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 µs */
24
Takashi Sakamotob445db42014-04-25 22:44:43 +090025/* isochronous header parameters */
26#define ISO_DATA_LENGTH_SHIFT 16
Clemens Ladisch31ef9132011-03-15 07:53:21 +010027#define TAG_CIP 1
28
Takashi Sakamotob445db42014-04-25 22:44:43 +090029/* common isochronous packet header parameters */
Clemens Ladisch31ef9132011-03-15 07:53:21 +010030#define CIP_EOH (1u << 31)
Takashi Sakamotob445db42014-04-25 22:44:43 +090031#define CIP_EOH_MASK 0x80000000
Clemens Ladisch31ef9132011-03-15 07:53:21 +010032#define CIP_FMT_AM (0x10 << 24)
Takashi Sakamotob445db42014-04-25 22:44:43 +090033#define CIP_FMT_MASK 0x3f000000
34#define CIP_SYT_MASK 0x0000ffff
35#define CIP_SYT_NO_INFO 0xffff
36#define CIP_FDF_MASK 0x00ff0000
37#define CIP_FDF_SFC_SHIFT 16
38
39/*
40 * Audio and Music transfer protocol specific parameters
41 * only "Clock-based rate control mode" is supported
42 */
43#define AMDTP_FDF_AM824 (0 << (CIP_FDF_SFC_SHIFT + 3))
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090044#define AMDTP_FDF_NO_DATA 0xff
Takashi Sakamotob445db42014-04-25 22:44:43 +090045#define AMDTP_DBS_MASK 0x00ff0000
46#define AMDTP_DBS_SHIFT 16
47#define AMDTP_DBC_MASK 0x000000ff
Clemens Ladisch31ef9132011-03-15 07:53:21 +010048
49/* TODO: make these configurable */
50#define INTERRUPT_INTERVAL 16
51#define QUEUE_LENGTH 48
52
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090053#define IN_PACKET_HEADER_SIZE 4
Takashi Sakamoto4b7da112014-04-25 22:44:45 +090054#define OUT_PACKET_HEADER_SIZE 0
55
Clemens Ladisch76fb8782012-05-13 22:03:09 +020056static void pcm_period_tasklet(unsigned long data);
57
Clemens Ladisch31ef9132011-03-15 07:53:21 +010058/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090059 * amdtp_stream_init - initialize an AMDTP stream structure
60 * @s: the AMDTP stream to initialize
Clemens Ladisch31ef9132011-03-15 07:53:21 +010061 * @unit: the target of the stream
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090062 * @dir: the direction of stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +010063 * @flags: the packet transmission method to use
64 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090065int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090066 enum amdtp_stream_direction dir, enum cip_flags flags)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010067{
Clemens Ladisch31ef9132011-03-15 07:53:21 +010068 s->unit = fw_unit_get(unit);
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090069 s->direction = dir;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010070 s->flags = flags;
71 s->context = ERR_PTR(-1);
72 mutex_init(&s->mutex);
Clemens Ladisch76fb8782012-05-13 22:03:09 +020073 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +010074 s->packet_index = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010075
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +090076 init_waitqueue_head(&s->callback_wait);
77 s->callbacked = false;
78 s->sync_slave = NULL;
79
Clemens Ladisch31ef9132011-03-15 07:53:21 +010080 return 0;
81}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090082EXPORT_SYMBOL(amdtp_stream_init);
Clemens Ladisch31ef9132011-03-15 07:53:21 +010083
84/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090085 * amdtp_stream_destroy - free stream resources
86 * @s: the AMDTP stream to destroy
Clemens Ladisch31ef9132011-03-15 07:53:21 +010087 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090088void amdtp_stream_destroy(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010089{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090090 WARN_ON(amdtp_stream_running(s));
Clemens Ladisch31ef9132011-03-15 07:53:21 +010091 mutex_destroy(&s->mutex);
92 fw_unit_put(s->unit);
93}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090094EXPORT_SYMBOL(amdtp_stream_destroy);
Clemens Ladisch31ef9132011-03-15 07:53:21 +010095
Clemens Ladischc5280e92011-10-16 21:39:00 +020096const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
Clemens Ladischa7304e32011-09-04 22:16:10 +020097 [CIP_SFC_32000] = 8,
98 [CIP_SFC_44100] = 8,
99 [CIP_SFC_48000] = 8,
100 [CIP_SFC_88200] = 16,
101 [CIP_SFC_96000] = 16,
102 [CIP_SFC_176400] = 32,
103 [CIP_SFC_192000] = 32,
104};
105EXPORT_SYMBOL(amdtp_syt_intervals);
106
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100107/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900108 * amdtp_stream_set_parameters - set stream parameters
109 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100110 * @rate: the sample rate
Clemens Ladischa7304e32011-09-04 22:16:10 +0200111 * @pcm_channels: the number of PCM samples in each data block, to be encoded
112 * as AM824 multi-bit linear audio
113 * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100114 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200115 * The parameters must be set before the stream is started, and must not be
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100116 * changed while the stream is running.
117 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900118void amdtp_stream_set_parameters(struct amdtp_stream *s,
119 unsigned int rate,
120 unsigned int pcm_channels,
121 unsigned int midi_ports)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100122{
Clemens Ladischa7304e32011-09-04 22:16:10 +0200123 static const unsigned int rates[] = {
124 [CIP_SFC_32000] = 32000,
125 [CIP_SFC_44100] = 44100,
126 [CIP_SFC_48000] = 48000,
127 [CIP_SFC_88200] = 88200,
128 [CIP_SFC_96000] = 96000,
129 [CIP_SFC_176400] = 176400,
130 [CIP_SFC_192000] = 192000,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100131 };
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900132 unsigned int i, sfc, midi_channels;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100133
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900134 midi_channels = DIV_ROUND_UP(midi_ports, 8);
135
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900136 if (WARN_ON(amdtp_stream_running(s)) |
137 WARN_ON(pcm_channels > AMDTP_MAX_CHANNELS_FOR_PCM) |
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900138 WARN_ON(midi_channels > AMDTP_MAX_CHANNELS_FOR_MIDI))
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100139 return;
140
Clemens Ladischa7304e32011-09-04 22:16:10 +0200141 for (sfc = 0; sfc < CIP_SFC_COUNT; ++sfc)
142 if (rates[sfc] == rate)
Clemens Ladische84d15f2011-09-04 22:12:48 +0200143 goto sfc_found;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100144 WARN_ON(1);
Clemens Ladische84d15f2011-09-04 22:12:48 +0200145 return;
146
147sfc_found:
Clemens Ladischa7304e32011-09-04 22:16:10 +0200148 s->dual_wire = (s->flags & CIP_HI_DUALWIRE) && sfc > CIP_SFC_96000;
149 if (s->dual_wire) {
150 sfc -= 2;
151 rate /= 2;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900152 s->pcm_channels = pcm_channels * 2;
153 } else {
154 s->pcm_channels = pcm_channels;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200155 }
Clemens Ladische84d15f2011-09-04 22:12:48 +0200156 s->sfc = sfc;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900157 s->data_block_quadlets = s->pcm_channels + midi_channels;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200158 s->midi_ports = midi_ports;
159
160 s->syt_interval = amdtp_syt_intervals[sfc];
Clemens Ladische84d15f2011-09-04 22:12:48 +0200161
162 /* default buffering in the device */
163 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
164 if (s->flags & CIP_BLOCKING)
165 /* additional buffering needed to adjust for no-data packets */
166 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900167
168 /* init the position map for PCM and MIDI channels */
169 for (i = 0; i < pcm_channels; i++)
170 s->pcm_positions[i] = i;
171 s->midi_position = s->pcm_channels;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100172}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900173EXPORT_SYMBOL(amdtp_stream_set_parameters);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100174
175/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900176 * amdtp_stream_get_max_payload - get the stream's packet size
177 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100178 *
179 * This function must not be called before the stream has been configured
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900180 * with amdtp_stream_set_parameters().
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100181 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900182unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100183{
Clemens Ladische84d15f2011-09-04 22:12:48 +0200184 return 8 + s->syt_interval * s->data_block_quadlets * 4;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100185}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900186EXPORT_SYMBOL(amdtp_stream_get_max_payload);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100187
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900188static void amdtp_write_s16(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100189 struct snd_pcm_substream *pcm,
190 __be32 *buffer, unsigned int frames);
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900191static void amdtp_write_s32(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100192 struct snd_pcm_substream *pcm,
193 __be32 *buffer, unsigned int frames);
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900194static void amdtp_write_s16_dualwire(struct amdtp_stream *s,
Clemens Ladischa7304e32011-09-04 22:16:10 +0200195 struct snd_pcm_substream *pcm,
196 __be32 *buffer, unsigned int frames);
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900197static void amdtp_write_s32_dualwire(struct amdtp_stream *s,
Clemens Ladischa7304e32011-09-04 22:16:10 +0200198 struct snd_pcm_substream *pcm,
199 __be32 *buffer, unsigned int frames);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900200static void amdtp_read_s32(struct amdtp_stream *s,
201 struct snd_pcm_substream *pcm,
202 __be32 *buffer, unsigned int frames);
203static void amdtp_read_s32_dualwire(struct amdtp_stream *s,
204 struct snd_pcm_substream *pcm,
205 __be32 *buffer, unsigned int frames);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100206
207/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900208 * amdtp_stream_set_pcm_format - set the PCM format
209 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100210 * @format: the format of the ALSA PCM device
211 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200212 * The sample format must be set after the other paramters (rate/PCM channels/
213 * MIDI) and before the stream is started, and must not be changed while the
214 * stream is running.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100215 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900216void amdtp_stream_set_pcm_format(struct amdtp_stream *s,
217 snd_pcm_format_t format)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100218{
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900219 if (WARN_ON(amdtp_stream_pcm_running(s)))
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100220 return;
221
222 switch (format) {
223 default:
224 WARN_ON(1);
225 /* fall through */
226 case SNDRV_PCM_FORMAT_S16:
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900227 if (s->direction == AMDTP_OUT_STREAM) {
228 if (s->dual_wire)
229 s->transfer_samples = amdtp_write_s16_dualwire;
230 else
231 s->transfer_samples = amdtp_write_s16;
232 break;
233 }
234 WARN_ON(1);
235 /* fall through */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100236 case SNDRV_PCM_FORMAT_S32:
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900237 if (s->direction == AMDTP_OUT_STREAM) {
238 if (s->dual_wire)
239 s->transfer_samples = amdtp_write_s32_dualwire;
240 else
241 s->transfer_samples = amdtp_write_s32;
242 } else {
243 if (s->dual_wire)
244 s->transfer_samples = amdtp_read_s32_dualwire;
245 else
246 s->transfer_samples = amdtp_read_s32;
247 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100248 break;
249 }
250}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900251EXPORT_SYMBOL(amdtp_stream_set_pcm_format);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100252
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200253/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900254 * amdtp_stream_pcm_prepare - prepare PCM device for running
255 * @s: the AMDTP stream
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200256 *
257 * This function should be called from the PCM device's .prepare callback.
258 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900259void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200260{
261 tasklet_kill(&s->period_tasklet);
262 s->pcm_buffer_pointer = 0;
263 s->pcm_period_pointer = 0;
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200264 s->pointer_flush = true;
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200265}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900266EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200267
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900268static unsigned int calculate_data_blocks(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100269{
270 unsigned int phase, data_blocks;
271
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900272 if (s->flags & CIP_BLOCKING)
273 data_blocks = s->syt_interval;
274 else if (!cip_sfc_is_base_44100(s->sfc)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100275 /* Sample_rate / 8000 is an integer, and precomputed. */
276 data_blocks = s->data_block_state;
277 } else {
278 phase = s->data_block_state;
279
280 /*
281 * This calculates the number of data blocks per packet so that
282 * 1) the overall rate is correct and exactly synchronized to
283 * the bus clock, and
284 * 2) packets with a rounded-up number of blocks occur as early
285 * as possible in the sequence (to prevent underruns of the
286 * device's buffer).
287 */
288 if (s->sfc == CIP_SFC_44100)
289 /* 6 6 5 6 5 6 5 ... */
290 data_blocks = 5 + ((phase & 1) ^
291 (phase == 0 || phase >= 40));
292 else
293 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
294 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
295 if (++phase >= (80 >> (s->sfc >> 1)))
296 phase = 0;
297 s->data_block_state = phase;
298 }
299
300 return data_blocks;
301}
302
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900303static unsigned int calculate_syt(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100304 unsigned int cycle)
305{
306 unsigned int syt_offset, phase, index, syt;
307
308 if (s->last_syt_offset < TICKS_PER_CYCLE) {
309 if (!cip_sfc_is_base_44100(s->sfc))
310 syt_offset = s->last_syt_offset + s->syt_offset_state;
311 else {
312 /*
313 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
314 * n * SYT_INTERVAL * 24576000 / sample_rate
315 * Modulo TICKS_PER_CYCLE, the difference between successive
316 * elements is about 1386.23. Rounding the results of this
317 * formula to the SYT precision results in a sequence of
318 * differences that begins with:
319 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
320 * This code generates _exactly_ the same sequence.
321 */
322 phase = s->syt_offset_state;
323 index = phase % 13;
324 syt_offset = s->last_syt_offset;
325 syt_offset += 1386 + ((index && !(index & 3)) ||
326 phase == 146);
327 if (++phase >= 147)
328 phase = 0;
329 s->syt_offset_state = phase;
330 }
331 } else
332 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
333 s->last_syt_offset = syt_offset;
334
Clemens Ladischbe454362011-03-15 07:55:02 +0100335 if (syt_offset < TICKS_PER_CYCLE) {
Clemens Ladische84d15f2011-09-04 22:12:48 +0200336 syt_offset += s->transfer_delay;
Clemens Ladischbe454362011-03-15 07:55:02 +0100337 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
338 syt += syt_offset % TICKS_PER_CYCLE;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100339
Takashi Sakamotob445db42014-04-25 22:44:43 +0900340 return syt & CIP_SYT_MASK;
Clemens Ladischbe454362011-03-15 07:55:02 +0100341 } else {
Takashi Sakamotob445db42014-04-25 22:44:43 +0900342 return CIP_SYT_NO_INFO;
Clemens Ladischbe454362011-03-15 07:55:02 +0100343 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100344}
345
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900346static void amdtp_write_s32(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100347 struct snd_pcm_substream *pcm,
348 __be32 *buffer, unsigned int frames)
349{
350 struct snd_pcm_runtime *runtime = pcm->runtime;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900351 unsigned int channels, remaining_frames, i, c;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100352 const u32 *src;
353
354 channels = s->pcm_channels;
355 src = (void *)runtime->dma_area +
Takashi Sakamotoe84841f2013-09-14 00:35:47 +0900356 frames_to_bytes(runtime, s->pcm_buffer_pointer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100357 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100358
359 for (i = 0; i < frames; ++i) {
360 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900361 buffer[s->pcm_positions[c]] =
362 cpu_to_be32((*src >> 8) | 0x40000000);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100363 src++;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100364 }
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900365 buffer += s->data_block_quadlets;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100366 if (--remaining_frames == 0)
367 src = (void *)runtime->dma_area;
368 }
369}
370
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900371static void amdtp_write_s16(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100372 struct snd_pcm_substream *pcm,
373 __be32 *buffer, unsigned int frames)
374{
375 struct snd_pcm_runtime *runtime = pcm->runtime;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900376 unsigned int channels, remaining_frames, i, c;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100377 const u16 *src;
378
379 channels = s->pcm_channels;
380 src = (void *)runtime->dma_area +
Takashi Sakamotoe84841f2013-09-14 00:35:47 +0900381 frames_to_bytes(runtime, s->pcm_buffer_pointer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100382 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100383
384 for (i = 0; i < frames; ++i) {
385 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900386 buffer[s->pcm_positions[c]] =
387 cpu_to_be32((*src << 8) | 0x40000000);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100388 src++;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100389 }
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900390 buffer += s->data_block_quadlets;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100391 if (--remaining_frames == 0)
392 src = (void *)runtime->dma_area;
393 }
394}
395
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900396static void amdtp_write_s32_dualwire(struct amdtp_stream *s,
Clemens Ladischa7304e32011-09-04 22:16:10 +0200397 struct snd_pcm_substream *pcm,
398 __be32 *buffer, unsigned int frames)
399{
400 struct snd_pcm_runtime *runtime = pcm->runtime;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900401 unsigned int channels, remaining_frames, i, c;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200402 const u32 *src;
403
Clemens Ladischa7304e32011-09-04 22:16:10 +0200404 src = (void *)runtime->dma_area +
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900405 frames_to_bytes(runtime, s->pcm_buffer_pointer);
406 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
407 channels = s->pcm_channels / 2;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200408
Clemens Ladischa7304e32011-09-04 22:16:10 +0200409 for (i = 0; i < frames; ++i) {
410 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900411 buffer[s->pcm_positions[c] * 2] =
412 cpu_to_be32((*src >> 8) | 0x40000000);
Clemens Ladischa7304e32011-09-04 22:16:10 +0200413 src++;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200414 }
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900415 buffer += 1;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200416 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900417 buffer[s->pcm_positions[c] * 2] =
418 cpu_to_be32((*src >> 8) | 0x40000000);
Clemens Ladischa7304e32011-09-04 22:16:10 +0200419 src++;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200420 }
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900421 buffer += s->data_block_quadlets - 1;
422 if (--remaining_frames == 0)
423 src = (void *)runtime->dma_area;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200424 }
425}
426
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900427static void amdtp_write_s16_dualwire(struct amdtp_stream *s,
Clemens Ladischa7304e32011-09-04 22:16:10 +0200428 struct snd_pcm_substream *pcm,
429 __be32 *buffer, unsigned int frames)
430{
431 struct snd_pcm_runtime *runtime = pcm->runtime;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900432 unsigned int channels, remaining_frames, i, c;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200433 const u16 *src;
434
Clemens Ladischa7304e32011-09-04 22:16:10 +0200435 src = (void *)runtime->dma_area +
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900436 frames_to_bytes(runtime, s->pcm_buffer_pointer);
437 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
438 channels = s->pcm_channels / 2;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200439
Clemens Ladischa7304e32011-09-04 22:16:10 +0200440 for (i = 0; i < frames; ++i) {
441 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900442 buffer[s->pcm_positions[c] * 2] =
443 cpu_to_be32((*src << 8) | 0x40000000);
Clemens Ladischa7304e32011-09-04 22:16:10 +0200444 src++;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200445 }
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900446 buffer += 1;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200447 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900448 buffer[s->pcm_positions[c] * 2] =
449 cpu_to_be32((*src << 8) | 0x40000000);
Clemens Ladischa7304e32011-09-04 22:16:10 +0200450 src++;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200451 }
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900452 buffer += s->data_block_quadlets - 1;
453 if (--remaining_frames == 0)
454 src = (void *)runtime->dma_area;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200455 }
456}
457
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900458static void amdtp_read_s32(struct amdtp_stream *s,
459 struct snd_pcm_substream *pcm,
460 __be32 *buffer, unsigned int frames)
461{
462 struct snd_pcm_runtime *runtime = pcm->runtime;
463 unsigned int channels, remaining_frames, i, c;
464 u32 *dst;
465
466 channels = s->pcm_channels;
467 dst = (void *)runtime->dma_area +
468 frames_to_bytes(runtime, s->pcm_buffer_pointer);
469 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
470
471 for (i = 0; i < frames; ++i) {
472 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900473 *dst = be32_to_cpu(buffer[s->pcm_positions[c]]) << 8;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900474 dst++;
475 }
476 buffer += s->data_block_quadlets;
477 if (--remaining_frames == 0)
478 dst = (void *)runtime->dma_area;
479 }
480}
481
482static void amdtp_read_s32_dualwire(struct amdtp_stream *s,
483 struct snd_pcm_substream *pcm,
484 __be32 *buffer, unsigned int frames)
485{
486 struct snd_pcm_runtime *runtime = pcm->runtime;
487 unsigned int channels, remaining_frames, i, c;
488 u32 *dst;
489
490 dst = (void *)runtime->dma_area +
491 frames_to_bytes(runtime, s->pcm_buffer_pointer);
492 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
493 channels = s->pcm_channels / 2;
494
495 for (i = 0; i < frames; ++i) {
496 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900497 *dst =
498 be32_to_cpu(buffer[s->pcm_positions[c] * 2]) << 8;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900499 dst++;
500 }
501 buffer += 1;
502 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900503 *dst =
504 be32_to_cpu(buffer[s->pcm_positions[c] * 2]) << 8;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900505 dst++;
506 }
507 buffer += s->data_block_quadlets - 1;
508 if (--remaining_frames == 0)
509 dst = (void *)runtime->dma_area;
510 }
511}
512
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900513static void amdtp_fill_pcm_silence(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100514 __be32 *buffer, unsigned int frames)
515{
516 unsigned int i, c;
517
518 for (i = 0; i < frames; ++i) {
519 for (c = 0; c < s->pcm_channels; ++c)
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900520 buffer[s->pcm_positions[c]] = cpu_to_be32(0x40000000);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100521 buffer += s->data_block_quadlets;
522 }
523}
524
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900525static void amdtp_fill_pcm_silence_dualwire(struct amdtp_stream *s,
526 __be32 *buffer, unsigned int frames)
527{
528 unsigned int i, c, channels;
529
530 channels = s->pcm_channels / 2;
531 for (i = 0; i < frames; ++i) {
532 for (c = 0; c < channels; ++c) {
533 buffer[s->pcm_positions[c] * 2] =
534 buffer[s->pcm_positions[c] * 2 + 1] =
535 cpu_to_be32(0x40000000);
536 }
537 buffer += s->data_block_quadlets;
538 }
539}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900540static void amdtp_fill_midi(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100541 __be32 *buffer, unsigned int frames)
542{
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900543 unsigned int f, port;
544 u8 *b;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100545
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900546 for (f = 0; f < frames; f++) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900547 buffer[s->midi_position] = 0;
548 b = (u8 *)&buffer[s->midi_position];
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900549
550 port = (s->data_block_counter + f) % 8;
551 if ((s->midi[port] == NULL) ||
552 (snd_rawmidi_transmit(s->midi[port], b + 1, 1) <= 0))
553 b[0] = 0x80;
554 else
555 b[0] = 0x81;
556
557 buffer += s->data_block_quadlets;
558 }
559}
560
561static void amdtp_pull_midi(struct amdtp_stream *s,
562 __be32 *buffer, unsigned int frames)
563{
564 unsigned int f, port;
565 int len;
566 u8 *b;
567
568 for (f = 0; f < frames; f++) {
569 port = (s->data_block_counter + f) % 8;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900570 b = (u8 *)&buffer[s->midi_position];
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900571
572 len = b[0] - 0x80;
573 if ((1 <= len) && (len <= 3) && (s->midi[port]))
574 snd_rawmidi_receive(s->midi[port], b + 1, len);
575
576 buffer += s->data_block_quadlets;
577 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100578}
579
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900580static void update_pcm_pointers(struct amdtp_stream *s,
581 struct snd_pcm_substream *pcm,
582 unsigned int frames)
583{ unsigned int ptr;
584
585 if (s->dual_wire)
586 frames *= 2;
587
588 ptr = s->pcm_buffer_pointer + frames;
589 if (ptr >= pcm->runtime->buffer_size)
590 ptr -= pcm->runtime->buffer_size;
591 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
592
593 s->pcm_period_pointer += frames;
594 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
595 s->pcm_period_pointer -= pcm->runtime->period_size;
596 s->pointer_flush = false;
597 tasklet_hi_schedule(&s->period_tasklet);
598 }
599}
600
601static void pcm_period_tasklet(unsigned long data)
602{
603 struct amdtp_stream *s = (void *)data;
604 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
605
606 if (pcm)
607 snd_pcm_period_elapsed(pcm);
608}
609
610static int queue_packet(struct amdtp_stream *s,
611 unsigned int header_length,
612 unsigned int payload_length, bool skip)
613{
614 struct fw_iso_packet p = {0};
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900615 int err = 0;
616
617 if (IS_ERR(s->context))
618 goto end;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900619
620 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
621 p.tag = TAG_CIP;
622 p.header_length = header_length;
623 p.payload_length = (!skip) ? payload_length : 0;
624 p.skip = skip;
625 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
626 s->buffer.packets[s->packet_index].offset);
627 if (err < 0) {
628 dev_err(&s->unit->device, "queueing error: %d\n", err);
629 goto end;
630 }
631
632 if (++s->packet_index >= QUEUE_LENGTH)
633 s->packet_index = 0;
634end:
635 return err;
636}
637
638static inline int queue_out_packet(struct amdtp_stream *s,
639 unsigned int payload_length, bool skip)
640{
641 return queue_packet(s, OUT_PACKET_HEADER_SIZE,
642 payload_length, skip);
643}
644
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900645static inline int queue_in_packet(struct amdtp_stream *s)
646{
647 return queue_packet(s, IN_PACKET_HEADER_SIZE,
648 amdtp_stream_get_max_payload(s), false);
649}
650
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900651static void handle_out_packet(struct amdtp_stream *s, unsigned int syt)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100652{
653 __be32 *buffer;
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900654 unsigned int data_blocks, payload_length;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100655 struct snd_pcm_substream *pcm;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100656
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100657 if (s->packet_index < 0)
658 return;
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100659
Takashi Sakamoto82755ab2013-11-21 14:21:06 +0900660 /* this module generate empty packet for 'no data' */
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900661 if (!(s->flags & CIP_BLOCKING) || (syt != CIP_SYT_NO_INFO))
Clemens Ladische84d15f2011-09-04 22:12:48 +0200662 data_blocks = calculate_data_blocks(s);
Takashi Sakamoto82755ab2013-11-21 14:21:06 +0900663 else
664 data_blocks = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100665
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900666 buffer = s->buffer.packets[s->packet_index].buffer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100667 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
Takashi Sakamotob445db42014-04-25 22:44:43 +0900668 (s->data_block_quadlets << AMDTP_DBS_SHIFT) |
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100669 s->data_block_counter);
670 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
Takashi Sakamotob445db42014-04-25 22:44:43 +0900671 (s->sfc << CIP_FDF_SFC_SHIFT) | syt);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100672 buffer += 2;
673
674 pcm = ACCESS_ONCE(s->pcm);
675 if (pcm)
676 s->transfer_samples(s, pcm, buffer, data_blocks);
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900677 else if (s->dual_wire)
678 amdtp_fill_pcm_silence_dualwire(s, buffer, data_blocks);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100679 else
680 amdtp_fill_pcm_silence(s, buffer, data_blocks);
681 if (s->midi_ports)
682 amdtp_fill_midi(s, buffer, data_blocks);
683
684 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
685
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900686 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
687 if (queue_out_packet(s, payload_length, false) < 0) {
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100688 s->packet_index = -1;
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900689 amdtp_stream_pcm_abort(s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100690 return;
691 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100692
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200693 if (pcm)
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900694 update_pcm_pointers(s, pcm, data_blocks);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200695}
696
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900697static void handle_in_packet(struct amdtp_stream *s,
698 unsigned int payload_quadlets,
699 __be32 *buffer)
700{
701 u32 cip_header[2];
702 unsigned int data_blocks, data_block_quadlets, data_block_counter;
703 struct snd_pcm_substream *pcm = NULL;
704
705 cip_header[0] = be32_to_cpu(buffer[0]);
706 cip_header[1] = be32_to_cpu(buffer[1]);
707
708 /*
709 * This module supports 'Two-quadlet CIP header with SYT field'.
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900710 * For convenience, also check FMT field is AM824 or not.
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900711 */
712 if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
713 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH) ||
714 ((cip_header[1] & CIP_FMT_MASK) != CIP_FMT_AM)) {
715 dev_info_ratelimited(&s->unit->device,
716 "Invalid CIP header for AMDTP: %08X:%08X\n",
717 cip_header[0], cip_header[1]);
718 goto end;
719 }
720
721 /* Calculate data blocks */
722 if (payload_quadlets < 3 ||
723 ((cip_header[1] & CIP_FDF_MASK) ==
724 (AMDTP_FDF_NO_DATA << CIP_FDF_SFC_SHIFT))) {
725 data_blocks = 0;
726 } else {
727 data_block_quadlets =
728 (cip_header[0] & AMDTP_DBS_MASK) >> AMDTP_DBS_SHIFT;
729 /* avoid division by zero */
730 if (data_block_quadlets == 0) {
731 dev_info_ratelimited(&s->unit->device,
732 "Detect invalid value in dbs field: %08X\n",
733 cip_header[0]);
734 goto err;
735 }
736
737 data_blocks = (payload_quadlets - 2) / data_block_quadlets;
738 }
739
740 /* Check data block counter continuity */
741 data_block_counter = cip_header[0] & AMDTP_DBC_MASK;
742 if (data_block_counter != s->data_block_counter) {
743 dev_info(&s->unit->device,
744 "Detect discontinuity of CIP: %02X %02X\n",
745 s->data_block_counter, data_block_counter);
746 goto err;
747 }
748
749 if (data_blocks > 0) {
750 buffer += 2;
751
752 pcm = ACCESS_ONCE(s->pcm);
753 if (pcm)
754 s->transfer_samples(s, pcm, buffer, data_blocks);
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900755
756 if (s->midi_ports)
757 amdtp_pull_midi(s, buffer, data_blocks);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900758 }
759
760 s->data_block_counter = (data_block_counter + data_blocks) & 0xff;
761end:
762 if (queue_in_packet(s) < 0)
763 goto err;
764
765 if (pcm)
766 update_pcm_pointers(s, pcm, data_blocks);
767
768 return;
769err:
770 s->packet_index = -1;
771 amdtp_stream_pcm_abort(s);
772}
773
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900774static void out_stream_callback(struct fw_iso_context *context, u32 cycle,
775 size_t header_length, void *header,
776 void *private_data)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100777{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900778 struct amdtp_stream *s = private_data;
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900779 unsigned int i, syt, packets = header_length / 4;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100780
781 /*
782 * Compute the cycle of the last queued packet.
783 * (We need only the four lowest bits for the SYT, so we can ignore
784 * that bits 0-11 must wrap around at 3072.)
785 */
786 cycle += QUEUE_LENGTH - packets;
787
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900788 for (i = 0; i < packets; ++i) {
789 syt = calculate_syt(s, ++cycle);
790 handle_out_packet(s, syt);
791 }
Clemens Ladisch13882a82011-05-02 09:33:56 +0200792 fw_iso_context_queue_flush(s->context);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100793}
794
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900795static void in_stream_callback(struct fw_iso_context *context, u32 cycle,
796 size_t header_length, void *header,
797 void *private_data)
798{
799 struct amdtp_stream *s = private_data;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900800 unsigned int p, syt, packets, payload_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900801 __be32 *buffer, *headers = header;
802
803 /* The number of packets in buffer */
804 packets = header_length / IN_PACKET_HEADER_SIZE;
805
806 for (p = 0; p < packets; p++) {
807 if (s->packet_index < 0)
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900808 break;
809
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900810 buffer = s->buffer.packets[s->packet_index].buffer;
811
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900812 /* Process sync slave stream */
813 if (s->sync_slave && s->sync_slave->callbacked) {
814 syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
815 handle_out_packet(s->sync_slave, syt);
816 }
817
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900818 /* The number of quadlets in this packet */
819 payload_quadlets =
820 (be32_to_cpu(headers[p]) >> ISO_DATA_LENGTH_SHIFT) / 4;
821 handle_in_packet(s, payload_quadlets, buffer);
822 }
823
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900824 /* Queueing error or detecting discontinuity */
825 if (s->packet_index < 0) {
826 /* Abort sync slave. */
827 if (s->sync_slave) {
828 s->sync_slave->packet_index = -1;
829 amdtp_stream_pcm_abort(s->sync_slave);
830 }
831 return;
832 }
833
834 /* when sync to device, flush the packets for slave stream */
835 if (s->sync_slave && s->sync_slave->callbacked)
836 fw_iso_context_queue_flush(s->sync_slave->context);
837
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900838 fw_iso_context_queue_flush(s->context);
839}
840
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900841/* processing is done by master callback */
842static void slave_stream_callback(struct fw_iso_context *context, u32 cycle,
843 size_t header_length, void *header,
844 void *private_data)
845{
846 return;
847}
848
849/* this is executed one time */
850static void amdtp_stream_first_callback(struct fw_iso_context *context,
851 u32 cycle, size_t header_length,
852 void *header, void *private_data)
853{
854 struct amdtp_stream *s = private_data;
855
856 /*
857 * For in-stream, first packet has come.
858 * For out-stream, prepared to transmit first packet
859 */
860 s->callbacked = true;
861 wake_up(&s->callback_wait);
862
863 if (s->direction == AMDTP_IN_STREAM)
864 context->callback.sc = in_stream_callback;
865 else if ((s->flags & CIP_BLOCKING) && (s->flags & CIP_SYNC_TO_DEVICE))
866 context->callback.sc = slave_stream_callback;
867 else
868 context->callback.sc = out_stream_callback;
869
870 context->callback.sc(context, cycle, header_length, header, s);
871}
872
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100873/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900874 * amdtp_stream_start - start transferring packets
875 * @s: the AMDTP stream to start
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100876 * @channel: the isochronous channel on the bus
877 * @speed: firewire speed code
878 *
879 * The stream cannot be started until it has been configured with
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900880 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
881 * device can be started.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100882 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900883int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100884{
885 static const struct {
886 unsigned int data_block;
887 unsigned int syt_offset;
888 } initial_state[] = {
889 [CIP_SFC_32000] = { 4, 3072 },
890 [CIP_SFC_48000] = { 6, 1024 },
891 [CIP_SFC_96000] = { 12, 1024 },
892 [CIP_SFC_192000] = { 24, 1024 },
893 [CIP_SFC_44100] = { 0, 67 },
894 [CIP_SFC_88200] = { 0, 67 },
895 [CIP_SFC_176400] = { 0, 67 },
896 };
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900897 unsigned int header_size;
898 enum dma_data_direction dir;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900899 int type, err;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100900
901 mutex_lock(&s->mutex);
902
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900903 if (WARN_ON(amdtp_stream_running(s) ||
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900904 (s->data_block_quadlets < 1))) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100905 err = -EBADFD;
906 goto err_unlock;
907 }
908
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900909 s->data_block_counter = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100910 s->data_block_state = initial_state[s->sfc].data_block;
911 s->syt_offset_state = initial_state[s->sfc].syt_offset;
912 s->last_syt_offset = TICKS_PER_CYCLE;
913
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900914 /* initialize packet buffer */
915 if (s->direction == AMDTP_IN_STREAM) {
916 dir = DMA_FROM_DEVICE;
917 type = FW_ISO_CONTEXT_RECEIVE;
918 header_size = IN_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900919 } else {
920 dir = DMA_TO_DEVICE;
921 type = FW_ISO_CONTEXT_TRANSMIT;
922 header_size = OUT_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900923 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100924 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900925 amdtp_stream_get_max_payload(s), dir);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100926 if (err < 0)
927 goto err_unlock;
928
929 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900930 type, channel, speed, header_size,
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900931 amdtp_stream_first_callback, s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100932 if (IS_ERR(s->context)) {
933 err = PTR_ERR(s->context);
934 if (err == -EBUSY)
935 dev_err(&s->unit->device,
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900936 "no free stream on this controller\n");
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100937 goto err_buffer;
938 }
939
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900940 amdtp_stream_update(s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100941
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100942 s->packet_index = 0;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900943 do {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900944 if (s->direction == AMDTP_IN_STREAM)
945 err = queue_in_packet(s);
946 else
947 err = queue_out_packet(s, 0, true);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900948 if (err < 0)
949 goto err_context;
950 } while (s->packet_index > 0);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100951
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900952 /* NOTE: TAG1 matches CIP. This just affects in stream. */
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900953 s->callbacked = false;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900954 err = fw_iso_context_start(s->context, -1, 0,
955 FW_ISO_CONTEXT_MATCH_TAG1);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100956 if (err < 0)
957 goto err_context;
958
959 mutex_unlock(&s->mutex);
960
961 return 0;
962
963err_context:
964 fw_iso_context_destroy(s->context);
965 s->context = ERR_PTR(-1);
966err_buffer:
967 iso_packets_buffer_destroy(&s->buffer, s->unit);
968err_unlock:
969 mutex_unlock(&s->mutex);
970
971 return err;
972}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900973EXPORT_SYMBOL(amdtp_stream_start);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100974
975/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900976 * amdtp_stream_pcm_pointer - get the PCM buffer position
977 * @s: the AMDTP stream that transports the PCM data
Clemens Ladische9148dd2012-05-13 18:49:14 +0200978 *
979 * Returns the current buffer position, in frames.
980 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900981unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
Clemens Ladische9148dd2012-05-13 18:49:14 +0200982{
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200983 /* this optimization is allowed to be racy */
984 if (s->pointer_flush)
985 fw_iso_context_flush_completions(s->context);
986 else
987 s->pointer_flush = true;
Clemens Ladische9148dd2012-05-13 18:49:14 +0200988
989 return ACCESS_ONCE(s->pcm_buffer_pointer);
990}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900991EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
Clemens Ladische9148dd2012-05-13 18:49:14 +0200992
993/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900994 * amdtp_stream_update - update the stream after a bus reset
995 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100996 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900997void amdtp_stream_update(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100998{
999 ACCESS_ONCE(s->source_node_id_field) =
1000 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
1001}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001002EXPORT_SYMBOL(amdtp_stream_update);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001003
1004/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001005 * amdtp_stream_stop - stop sending packets
1006 * @s: the AMDTP stream to stop
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001007 *
1008 * All PCM and MIDI devices of the stream must be stopped before the stream
1009 * itself can be stopped.
1010 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001011void amdtp_stream_stop(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001012{
1013 mutex_lock(&s->mutex);
1014
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001015 if (!amdtp_stream_running(s)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001016 mutex_unlock(&s->mutex);
1017 return;
1018 }
1019
Clemens Ladisch76fb8782012-05-13 22:03:09 +02001020 tasklet_kill(&s->period_tasklet);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001021 fw_iso_context_stop(s->context);
1022 fw_iso_context_destroy(s->context);
1023 s->context = ERR_PTR(-1);
1024 iso_packets_buffer_destroy(&s->buffer, s->unit);
1025
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +09001026 s->callbacked = false;
1027
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001028 mutex_unlock(&s->mutex);
1029}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001030EXPORT_SYMBOL(amdtp_stream_stop);
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001031
1032/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001033 * amdtp_stream_pcm_abort - abort the running PCM device
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001034 * @s: the AMDTP stream about to be stopped
1035 *
1036 * If the isochronous stream needs to be stopped asynchronously, call this
1037 * function first to stop the PCM device.
1038 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001039void amdtp_stream_pcm_abort(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001040{
1041 struct snd_pcm_substream *pcm;
1042
1043 pcm = ACCESS_ONCE(s->pcm);
1044 if (pcm) {
1045 snd_pcm_stream_lock_irq(pcm);
1046 if (snd_pcm_running(pcm))
1047 snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
1048 snd_pcm_stream_unlock_irq(pcm);
1049 }
1050}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +09001051EXPORT_SYMBOL(amdtp_stream_pcm_abort);