blob: 5e13b3f1551659231d43a886fbacb147434c571e [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
Clemens Ladisch31ef9132011-03-15 07:53:21 +010081 return 0;
82}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090083EXPORT_SYMBOL(amdtp_stream_init);
Clemens Ladisch31ef9132011-03-15 07:53:21 +010084
85/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090086 * amdtp_stream_destroy - free stream resources
87 * @s: the AMDTP stream to destroy
Clemens Ladisch31ef9132011-03-15 07:53:21 +010088 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090089void amdtp_stream_destroy(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010090{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090091 WARN_ON(amdtp_stream_running(s));
Clemens Ladisch31ef9132011-03-15 07:53:21 +010092 mutex_destroy(&s->mutex);
93 fw_unit_put(s->unit);
94}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090095EXPORT_SYMBOL(amdtp_stream_destroy);
Clemens Ladisch31ef9132011-03-15 07:53:21 +010096
Clemens Ladischc5280e92011-10-16 21:39:00 +020097const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
Clemens Ladischa7304e32011-09-04 22:16:10 +020098 [CIP_SFC_32000] = 8,
99 [CIP_SFC_44100] = 8,
100 [CIP_SFC_48000] = 8,
101 [CIP_SFC_88200] = 16,
102 [CIP_SFC_96000] = 16,
103 [CIP_SFC_176400] = 32,
104 [CIP_SFC_192000] = 32,
105};
106EXPORT_SYMBOL(amdtp_syt_intervals);
107
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900108const unsigned int amdtp_rate_table[] = {
109 [CIP_SFC_32000] = 32000,
110 [CIP_SFC_44100] = 44100,
111 [CIP_SFC_48000] = 48000,
112 [CIP_SFC_88200] = 88200,
113 [CIP_SFC_96000] = 96000,
114 [CIP_SFC_176400] = 176400,
115 [CIP_SFC_192000] = 192000,
116};
117EXPORT_SYMBOL(amdtp_rate_table);
118
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100119/**
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900120 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
121 * @s: the AMDTP stream, which must be initialized.
122 * @runtime: the PCM substream runtime
123 */
124int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
125 struct snd_pcm_runtime *runtime)
126{
127 int err;
128
129 /* AM824 in IEC 61883-6 can deliver 24bit data */
130 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
131 if (err < 0)
132 goto end;
133
134 /*
135 * Currently firewire-lib processes 16 packets in one software
136 * interrupt callback. This equals to 2msec but actually the
137 * interval of the interrupts has a jitter.
138 * Additionally, even if adding a constraint to fit period size to
139 * 2msec, actual calculated frames per period doesn't equal to 2msec,
140 * depending on sampling rate.
141 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
142 * Here let us use 5msec for safe period interrupt.
143 */
144 err = snd_pcm_hw_constraint_minmax(runtime,
145 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
146 5000, UINT_MAX);
147 if (err < 0)
148 goto end;
149
150 /* Non-Blocking stream has no more constraints */
151 if (!(s->flags & CIP_BLOCKING))
152 goto end;
153
154 /*
155 * One AMDTP packet can include some frames. In blocking mode, the
156 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
157 * depending on its sampling rate. For accurate period interrupt, it's
158 * preferrable to aligh period/buffer sizes to current SYT_INTERVAL.
159 *
160 * TODO: These constraints can be improved with propper rules.
161 * Currently apply LCM of SYT_INTEVALs.
162 */
163 err = snd_pcm_hw_constraint_step(runtime, 0,
164 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
165 if (err < 0)
166 goto end;
167 err = snd_pcm_hw_constraint_step(runtime, 0,
168 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
169end:
170 return err;
171}
172EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
173
174/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900175 * amdtp_stream_set_parameters - set stream parameters
176 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100177 * @rate: the sample rate
Clemens Ladischa7304e32011-09-04 22:16:10 +0200178 * @pcm_channels: the number of PCM samples in each data block, to be encoded
179 * as AM824 multi-bit linear audio
180 * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100181 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200182 * The parameters must be set before the stream is started, and must not be
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100183 * changed while the stream is running.
184 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900185void amdtp_stream_set_parameters(struct amdtp_stream *s,
186 unsigned int rate,
187 unsigned int pcm_channels,
188 unsigned int midi_ports)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100189{
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900190 unsigned int i, sfc, midi_channels;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100191
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900192 midi_channels = DIV_ROUND_UP(midi_ports, 8);
193
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900194 if (WARN_ON(amdtp_stream_running(s)) |
195 WARN_ON(pcm_channels > AMDTP_MAX_CHANNELS_FOR_PCM) |
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900196 WARN_ON(midi_channels > AMDTP_MAX_CHANNELS_FOR_MIDI))
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100197 return;
198
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900199 for (sfc = 0; sfc < sizeof(amdtp_rate_table); ++sfc)
200 if (amdtp_rate_table[sfc] == rate)
Clemens Ladische84d15f2011-09-04 22:12:48 +0200201 goto sfc_found;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100202 WARN_ON(1);
Clemens Ladische84d15f2011-09-04 22:12:48 +0200203 return;
204
205sfc_found:
Takashi Sakamoto10550be2014-04-25 22:44:51 +0900206 s->pcm_channels = pcm_channels;
Clemens Ladische84d15f2011-09-04 22:12:48 +0200207 s->sfc = sfc;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900208 s->data_block_quadlets = s->pcm_channels + midi_channels;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200209 s->midi_ports = midi_ports;
210
211 s->syt_interval = amdtp_syt_intervals[sfc];
Clemens Ladische84d15f2011-09-04 22:12:48 +0200212
213 /* default buffering in the device */
214 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
215 if (s->flags & CIP_BLOCKING)
216 /* additional buffering needed to adjust for no-data packets */
217 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900218
219 /* init the position map for PCM and MIDI channels */
220 for (i = 0; i < pcm_channels; i++)
221 s->pcm_positions[i] = i;
222 s->midi_position = s->pcm_channels;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100223}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900224EXPORT_SYMBOL(amdtp_stream_set_parameters);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100225
226/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900227 * amdtp_stream_get_max_payload - get the stream's packet size
228 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100229 *
230 * This function must not be called before the stream has been configured
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900231 * with amdtp_stream_set_parameters().
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100232 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900233unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100234{
Clemens Ladische84d15f2011-09-04 22:12:48 +0200235 return 8 + s->syt_interval * s->data_block_quadlets * 4;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100236}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900237EXPORT_SYMBOL(amdtp_stream_get_max_payload);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100238
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900239static void amdtp_write_s16(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100240 struct snd_pcm_substream *pcm,
241 __be32 *buffer, unsigned int frames);
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900242static void amdtp_write_s32(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100243 struct snd_pcm_substream *pcm,
244 __be32 *buffer, unsigned int frames);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900245static void amdtp_read_s32(struct amdtp_stream *s,
246 struct snd_pcm_substream *pcm,
247 __be32 *buffer, unsigned int frames);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100248
249/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900250 * amdtp_stream_set_pcm_format - set the PCM format
251 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100252 * @format: the format of the ALSA PCM device
253 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200254 * The sample format must be set after the other paramters (rate/PCM channels/
255 * MIDI) and before the stream is started, and must not be changed while the
256 * stream is running.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100257 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900258void amdtp_stream_set_pcm_format(struct amdtp_stream *s,
259 snd_pcm_format_t format)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100260{
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900261 if (WARN_ON(amdtp_stream_pcm_running(s)))
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100262 return;
263
264 switch (format) {
265 default:
266 WARN_ON(1);
267 /* fall through */
268 case SNDRV_PCM_FORMAT_S16:
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900269 if (s->direction == AMDTP_OUT_STREAM) {
Takashi Sakamoto10550be2014-04-25 22:44:51 +0900270 s->transfer_samples = amdtp_write_s16;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900271 break;
272 }
273 WARN_ON(1);
274 /* fall through */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100275 case SNDRV_PCM_FORMAT_S32:
Takashi Sakamoto10550be2014-04-25 22:44:51 +0900276 if (s->direction == AMDTP_OUT_STREAM)
277 s->transfer_samples = amdtp_write_s32;
278 else
279 s->transfer_samples = amdtp_read_s32;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100280 break;
281 }
282}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900283EXPORT_SYMBOL(amdtp_stream_set_pcm_format);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100284
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200285/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900286 * amdtp_stream_pcm_prepare - prepare PCM device for running
287 * @s: the AMDTP stream
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200288 *
289 * This function should be called from the PCM device's .prepare callback.
290 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900291void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200292{
293 tasklet_kill(&s->period_tasklet);
294 s->pcm_buffer_pointer = 0;
295 s->pcm_period_pointer = 0;
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200296 s->pointer_flush = true;
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200297}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900298EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200299
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900300static unsigned int calculate_data_blocks(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100301{
302 unsigned int phase, data_blocks;
303
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900304 if (s->flags & CIP_BLOCKING)
305 data_blocks = s->syt_interval;
306 else if (!cip_sfc_is_base_44100(s->sfc)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100307 /* Sample_rate / 8000 is an integer, and precomputed. */
308 data_blocks = s->data_block_state;
309 } else {
310 phase = s->data_block_state;
311
312 /*
313 * This calculates the number of data blocks per packet so that
314 * 1) the overall rate is correct and exactly synchronized to
315 * the bus clock, and
316 * 2) packets with a rounded-up number of blocks occur as early
317 * as possible in the sequence (to prevent underruns of the
318 * device's buffer).
319 */
320 if (s->sfc == CIP_SFC_44100)
321 /* 6 6 5 6 5 6 5 ... */
322 data_blocks = 5 + ((phase & 1) ^
323 (phase == 0 || phase >= 40));
324 else
325 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
326 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
327 if (++phase >= (80 >> (s->sfc >> 1)))
328 phase = 0;
329 s->data_block_state = phase;
330 }
331
332 return data_blocks;
333}
334
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900335static unsigned int calculate_syt(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100336 unsigned int cycle)
337{
338 unsigned int syt_offset, phase, index, syt;
339
340 if (s->last_syt_offset < TICKS_PER_CYCLE) {
341 if (!cip_sfc_is_base_44100(s->sfc))
342 syt_offset = s->last_syt_offset + s->syt_offset_state;
343 else {
344 /*
345 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
346 * n * SYT_INTERVAL * 24576000 / sample_rate
347 * Modulo TICKS_PER_CYCLE, the difference between successive
348 * elements is about 1386.23. Rounding the results of this
349 * formula to the SYT precision results in a sequence of
350 * differences that begins with:
351 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
352 * This code generates _exactly_ the same sequence.
353 */
354 phase = s->syt_offset_state;
355 index = phase % 13;
356 syt_offset = s->last_syt_offset;
357 syt_offset += 1386 + ((index && !(index & 3)) ||
358 phase == 146);
359 if (++phase >= 147)
360 phase = 0;
361 s->syt_offset_state = phase;
362 }
363 } else
364 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
365 s->last_syt_offset = syt_offset;
366
Clemens Ladischbe454362011-03-15 07:55:02 +0100367 if (syt_offset < TICKS_PER_CYCLE) {
Clemens Ladische84d15f2011-09-04 22:12:48 +0200368 syt_offset += s->transfer_delay;
Clemens Ladischbe454362011-03-15 07:55:02 +0100369 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
370 syt += syt_offset % TICKS_PER_CYCLE;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100371
Takashi Sakamotob445db42014-04-25 22:44:43 +0900372 return syt & CIP_SYT_MASK;
Clemens Ladischbe454362011-03-15 07:55:02 +0100373 } else {
Takashi Sakamotob445db42014-04-25 22:44:43 +0900374 return CIP_SYT_NO_INFO;
Clemens Ladischbe454362011-03-15 07:55:02 +0100375 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100376}
377
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900378static void amdtp_write_s32(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100379 struct snd_pcm_substream *pcm,
380 __be32 *buffer, unsigned int frames)
381{
382 struct snd_pcm_runtime *runtime = pcm->runtime;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900383 unsigned int channels, remaining_frames, i, c;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100384 const u32 *src;
385
386 channels = s->pcm_channels;
387 src = (void *)runtime->dma_area +
Takashi Sakamotoe84841f2013-09-14 00:35:47 +0900388 frames_to_bytes(runtime, s->pcm_buffer_pointer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100389 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100390
391 for (i = 0; i < frames; ++i) {
392 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900393 buffer[s->pcm_positions[c]] =
394 cpu_to_be32((*src >> 8) | 0x40000000);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100395 src++;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100396 }
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900397 buffer += s->data_block_quadlets;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100398 if (--remaining_frames == 0)
399 src = (void *)runtime->dma_area;
400 }
401}
402
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900403static void amdtp_write_s16(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100404 struct snd_pcm_substream *pcm,
405 __be32 *buffer, unsigned int frames)
406{
407 struct snd_pcm_runtime *runtime = pcm->runtime;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900408 unsigned int channels, remaining_frames, i, c;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100409 const u16 *src;
410
411 channels = s->pcm_channels;
412 src = (void *)runtime->dma_area +
Takashi Sakamotoe84841f2013-09-14 00:35:47 +0900413 frames_to_bytes(runtime, s->pcm_buffer_pointer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100414 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100415
416 for (i = 0; i < frames; ++i) {
417 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900418 buffer[s->pcm_positions[c]] =
419 cpu_to_be32((*src << 8) | 0x40000000);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100420 src++;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100421 }
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900422 buffer += s->data_block_quadlets;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100423 if (--remaining_frames == 0)
424 src = (void *)runtime->dma_area;
425 }
426}
427
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900428static void amdtp_read_s32(struct amdtp_stream *s,
429 struct snd_pcm_substream *pcm,
430 __be32 *buffer, unsigned int frames)
431{
432 struct snd_pcm_runtime *runtime = pcm->runtime;
433 unsigned int channels, remaining_frames, i, c;
434 u32 *dst;
435
436 channels = s->pcm_channels;
437 dst = (void *)runtime->dma_area +
438 frames_to_bytes(runtime, s->pcm_buffer_pointer);
439 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
440
441 for (i = 0; i < frames; ++i) {
442 for (c = 0; c < channels; ++c) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900443 *dst = be32_to_cpu(buffer[s->pcm_positions[c]]) << 8;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900444 dst++;
445 }
446 buffer += s->data_block_quadlets;
447 if (--remaining_frames == 0)
448 dst = (void *)runtime->dma_area;
449 }
450}
451
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900452static void amdtp_fill_pcm_silence(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100453 __be32 *buffer, unsigned int frames)
454{
455 unsigned int i, c;
456
457 for (i = 0; i < frames; ++i) {
458 for (c = 0; c < s->pcm_channels; ++c)
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900459 buffer[s->pcm_positions[c]] = cpu_to_be32(0x40000000);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100460 buffer += s->data_block_quadlets;
461 }
462}
463
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900464static void amdtp_fill_midi(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100465 __be32 *buffer, unsigned int frames)
466{
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900467 unsigned int f, port;
468 u8 *b;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100469
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900470 for (f = 0; f < frames; f++) {
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900471 buffer[s->midi_position] = 0;
472 b = (u8 *)&buffer[s->midi_position];
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900473
474 port = (s->data_block_counter + f) % 8;
475 if ((s->midi[port] == NULL) ||
476 (snd_rawmidi_transmit(s->midi[port], b + 1, 1) <= 0))
477 b[0] = 0x80;
478 else
479 b[0] = 0x81;
480
481 buffer += s->data_block_quadlets;
482 }
483}
484
485static void amdtp_pull_midi(struct amdtp_stream *s,
486 __be32 *buffer, unsigned int frames)
487{
488 unsigned int f, port;
489 int len;
490 u8 *b;
491
492 for (f = 0; f < frames; f++) {
493 port = (s->data_block_counter + f) % 8;
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900494 b = (u8 *)&buffer[s->midi_position];
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900495
496 len = b[0] - 0x80;
497 if ((1 <= len) && (len <= 3) && (s->midi[port]))
498 snd_rawmidi_receive(s->midi[port], b + 1, len);
499
500 buffer += s->data_block_quadlets;
501 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100502}
503
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900504static void update_pcm_pointers(struct amdtp_stream *s,
505 struct snd_pcm_substream *pcm,
506 unsigned int frames)
507{ unsigned int ptr;
508
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900509 ptr = s->pcm_buffer_pointer + frames;
510 if (ptr >= pcm->runtime->buffer_size)
511 ptr -= pcm->runtime->buffer_size;
512 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
513
514 s->pcm_period_pointer += frames;
515 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
516 s->pcm_period_pointer -= pcm->runtime->period_size;
517 s->pointer_flush = false;
518 tasklet_hi_schedule(&s->period_tasklet);
519 }
520}
521
522static void pcm_period_tasklet(unsigned long data)
523{
524 struct amdtp_stream *s = (void *)data;
525 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
526
527 if (pcm)
528 snd_pcm_period_elapsed(pcm);
529}
530
531static int queue_packet(struct amdtp_stream *s,
532 unsigned int header_length,
533 unsigned int payload_length, bool skip)
534{
535 struct fw_iso_packet p = {0};
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900536 int err = 0;
537
538 if (IS_ERR(s->context))
539 goto end;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900540
541 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
542 p.tag = TAG_CIP;
543 p.header_length = header_length;
544 p.payload_length = (!skip) ? payload_length : 0;
545 p.skip = skip;
546 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
547 s->buffer.packets[s->packet_index].offset);
548 if (err < 0) {
549 dev_err(&s->unit->device, "queueing error: %d\n", err);
550 goto end;
551 }
552
553 if (++s->packet_index >= QUEUE_LENGTH)
554 s->packet_index = 0;
555end:
556 return err;
557}
558
559static inline int queue_out_packet(struct amdtp_stream *s,
560 unsigned int payload_length, bool skip)
561{
562 return queue_packet(s, OUT_PACKET_HEADER_SIZE,
563 payload_length, skip);
564}
565
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900566static inline int queue_in_packet(struct amdtp_stream *s)
567{
568 return queue_packet(s, IN_PACKET_HEADER_SIZE,
569 amdtp_stream_get_max_payload(s), false);
570}
571
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900572static void handle_out_packet(struct amdtp_stream *s, unsigned int syt)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100573{
574 __be32 *buffer;
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900575 unsigned int data_blocks, payload_length;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100576 struct snd_pcm_substream *pcm;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100577
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100578 if (s->packet_index < 0)
579 return;
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100580
Takashi Sakamoto82755ab2013-11-21 14:21:06 +0900581 /* this module generate empty packet for 'no data' */
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900582 if (!(s->flags & CIP_BLOCKING) || (syt != CIP_SYT_NO_INFO))
Clemens Ladische84d15f2011-09-04 22:12:48 +0200583 data_blocks = calculate_data_blocks(s);
Takashi Sakamoto82755ab2013-11-21 14:21:06 +0900584 else
585 data_blocks = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100586
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900587 buffer = s->buffer.packets[s->packet_index].buffer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100588 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
Takashi Sakamotob445db42014-04-25 22:44:43 +0900589 (s->data_block_quadlets << AMDTP_DBS_SHIFT) |
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100590 s->data_block_counter);
591 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
Takashi Sakamotob445db42014-04-25 22:44:43 +0900592 (s->sfc << CIP_FDF_SFC_SHIFT) | syt);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100593 buffer += 2;
594
595 pcm = ACCESS_ONCE(s->pcm);
596 if (pcm)
597 s->transfer_samples(s, pcm, buffer, data_blocks);
598 else
599 amdtp_fill_pcm_silence(s, buffer, data_blocks);
600 if (s->midi_ports)
601 amdtp_fill_midi(s, buffer, data_blocks);
602
603 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
604
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900605 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
606 if (queue_out_packet(s, payload_length, false) < 0) {
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100607 s->packet_index = -1;
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900608 amdtp_stream_pcm_abort(s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100609 return;
610 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100611
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200612 if (pcm)
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900613 update_pcm_pointers(s, pcm, data_blocks);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200614}
615
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900616static void handle_in_packet(struct amdtp_stream *s,
617 unsigned int payload_quadlets,
618 __be32 *buffer)
619{
620 u32 cip_header[2];
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900621 unsigned int data_blocks, data_block_quadlets, data_block_counter,
622 dbc_interval;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900623 struct snd_pcm_substream *pcm = NULL;
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900624 bool lost;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900625
626 cip_header[0] = be32_to_cpu(buffer[0]);
627 cip_header[1] = be32_to_cpu(buffer[1]);
628
629 /*
630 * This module supports 'Two-quadlet CIP header with SYT field'.
Takashi Sakamoto77d2a8a2014-04-25 22:44:50 +0900631 * For convenience, also check FMT field is AM824 or not.
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900632 */
633 if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
634 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH) ||
635 ((cip_header[1] & CIP_FMT_MASK) != CIP_FMT_AM)) {
636 dev_info_ratelimited(&s->unit->device,
637 "Invalid CIP header for AMDTP: %08X:%08X\n",
638 cip_header[0], cip_header[1]);
639 goto end;
640 }
641
642 /* Calculate data blocks */
643 if (payload_quadlets < 3 ||
644 ((cip_header[1] & CIP_FDF_MASK) ==
645 (AMDTP_FDF_NO_DATA << CIP_FDF_SFC_SHIFT))) {
646 data_blocks = 0;
647 } else {
648 data_block_quadlets =
649 (cip_header[0] & AMDTP_DBS_MASK) >> AMDTP_DBS_SHIFT;
650 /* avoid division by zero */
651 if (data_block_quadlets == 0) {
652 dev_info_ratelimited(&s->unit->device,
653 "Detect invalid value in dbs field: %08X\n",
654 cip_header[0]);
655 goto err;
656 }
Takashi Sakamoto69702232014-04-25 22:45:05 +0900657 if (s->flags & CIP_WRONG_DBS)
658 data_block_quadlets = s->data_block_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900659
660 data_blocks = (payload_quadlets - 2) / data_block_quadlets;
661 }
662
663 /* Check data block counter continuity */
664 data_block_counter = cip_header[0] & AMDTP_DBC_MASK;
Takashi Sakamotob84b1a22014-04-25 22:45:07 +0900665 if ((s->flags & CIP_SKIP_DBC_ZERO_CHECK) && data_block_counter == 0) {
666 lost = false;
667 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900668 lost = data_block_counter != s->data_block_counter;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900669 } else {
670 if ((data_blocks > 0) && (s->tx_dbc_interval > 0))
671 dbc_interval = s->tx_dbc_interval;
672 else
673 dbc_interval = data_blocks;
674
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900675 lost = data_block_counter !=
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900676 ((s->data_block_counter + dbc_interval) & 0xff);
677 }
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900678
679 if (lost) {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900680 dev_info(&s->unit->device,
681 "Detect discontinuity of CIP: %02X %02X\n",
682 s->data_block_counter, data_block_counter);
683 goto err;
684 }
685
686 if (data_blocks > 0) {
687 buffer += 2;
688
689 pcm = ACCESS_ONCE(s->pcm);
690 if (pcm)
691 s->transfer_samples(s, pcm, buffer, data_blocks);
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900692
693 if (s->midi_ports)
694 amdtp_pull_midi(s, buffer, data_blocks);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900695 }
696
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900697 if (s->flags & CIP_DBC_IS_END_EVENT)
698 s->data_block_counter = data_block_counter;
699 else
700 s->data_block_counter =
701 (data_block_counter + data_blocks) & 0xff;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900702end:
703 if (queue_in_packet(s) < 0)
704 goto err;
705
706 if (pcm)
707 update_pcm_pointers(s, pcm, data_blocks);
708
709 return;
710err:
711 s->packet_index = -1;
712 amdtp_stream_pcm_abort(s);
713}
714
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900715static void out_stream_callback(struct fw_iso_context *context, u32 cycle,
716 size_t header_length, void *header,
717 void *private_data)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100718{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900719 struct amdtp_stream *s = private_data;
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900720 unsigned int i, syt, packets = header_length / 4;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100721
722 /*
723 * Compute the cycle of the last queued packet.
724 * (We need only the four lowest bits for the SYT, so we can ignore
725 * that bits 0-11 must wrap around at 3072.)
726 */
727 cycle += QUEUE_LENGTH - packets;
728
Takashi Sakamotoccccad82014-04-25 22:44:48 +0900729 for (i = 0; i < packets; ++i) {
730 syt = calculate_syt(s, ++cycle);
731 handle_out_packet(s, syt);
732 }
Clemens Ladisch13882a82011-05-02 09:33:56 +0200733 fw_iso_context_queue_flush(s->context);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100734}
735
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900736static void in_stream_callback(struct fw_iso_context *context, u32 cycle,
737 size_t header_length, void *header,
738 void *private_data)
739{
740 struct amdtp_stream *s = private_data;
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900741 unsigned int p, syt, packets, payload_quadlets;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900742 __be32 *buffer, *headers = header;
743
744 /* The number of packets in buffer */
745 packets = header_length / IN_PACKET_HEADER_SIZE;
746
747 for (p = 0; p < packets; p++) {
748 if (s->packet_index < 0)
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900749 break;
750
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900751 buffer = s->buffer.packets[s->packet_index].buffer;
752
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900753 /* Process sync slave stream */
754 if (s->sync_slave && s->sync_slave->callbacked) {
755 syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
756 handle_out_packet(s->sync_slave, syt);
757 }
758
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900759 /* The number of quadlets in this packet */
760 payload_quadlets =
761 (be32_to_cpu(headers[p]) >> ISO_DATA_LENGTH_SHIFT) / 4;
762 handle_in_packet(s, payload_quadlets, buffer);
763 }
764
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900765 /* Queueing error or detecting discontinuity */
766 if (s->packet_index < 0) {
767 /* Abort sync slave. */
768 if (s->sync_slave) {
769 s->sync_slave->packet_index = -1;
770 amdtp_stream_pcm_abort(s->sync_slave);
771 }
772 return;
773 }
774
775 /* when sync to device, flush the packets for slave stream */
776 if (s->sync_slave && s->sync_slave->callbacked)
777 fw_iso_context_queue_flush(s->sync_slave->context);
778
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900779 fw_iso_context_queue_flush(s->context);
780}
781
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900782/* processing is done by master callback */
783static void slave_stream_callback(struct fw_iso_context *context, u32 cycle,
784 size_t header_length, void *header,
785 void *private_data)
786{
787 return;
788}
789
790/* this is executed one time */
791static void amdtp_stream_first_callback(struct fw_iso_context *context,
792 u32 cycle, size_t header_length,
793 void *header, void *private_data)
794{
795 struct amdtp_stream *s = private_data;
796
797 /*
798 * For in-stream, first packet has come.
799 * For out-stream, prepared to transmit first packet
800 */
801 s->callbacked = true;
802 wake_up(&s->callback_wait);
803
804 if (s->direction == AMDTP_IN_STREAM)
805 context->callback.sc = in_stream_callback;
806 else if ((s->flags & CIP_BLOCKING) && (s->flags & CIP_SYNC_TO_DEVICE))
807 context->callback.sc = slave_stream_callback;
808 else
809 context->callback.sc = out_stream_callback;
810
811 context->callback.sc(context, cycle, header_length, header, s);
812}
813
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100814/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900815 * amdtp_stream_start - start transferring packets
816 * @s: the AMDTP stream to start
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100817 * @channel: the isochronous channel on the bus
818 * @speed: firewire speed code
819 *
820 * The stream cannot be started until it has been configured with
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900821 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
822 * device can be started.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100823 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900824int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100825{
826 static const struct {
827 unsigned int data_block;
828 unsigned int syt_offset;
829 } initial_state[] = {
830 [CIP_SFC_32000] = { 4, 3072 },
831 [CIP_SFC_48000] = { 6, 1024 },
832 [CIP_SFC_96000] = { 12, 1024 },
833 [CIP_SFC_192000] = { 24, 1024 },
834 [CIP_SFC_44100] = { 0, 67 },
835 [CIP_SFC_88200] = { 0, 67 },
836 [CIP_SFC_176400] = { 0, 67 },
837 };
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900838 unsigned int header_size;
839 enum dma_data_direction dir;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900840 int type, tag, err;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100841
842 mutex_lock(&s->mutex);
843
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900844 if (WARN_ON(amdtp_stream_running(s) ||
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900845 (s->data_block_quadlets < 1))) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100846 err = -EBADFD;
847 goto err_unlock;
848 }
849
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900850 s->data_block_counter = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100851 s->data_block_state = initial_state[s->sfc].data_block;
852 s->syt_offset_state = initial_state[s->sfc].syt_offset;
853 s->last_syt_offset = TICKS_PER_CYCLE;
854
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900855 /* initialize packet buffer */
856 if (s->direction == AMDTP_IN_STREAM) {
857 dir = DMA_FROM_DEVICE;
858 type = FW_ISO_CONTEXT_RECEIVE;
859 header_size = IN_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900860 } else {
861 dir = DMA_TO_DEVICE;
862 type = FW_ISO_CONTEXT_TRANSMIT;
863 header_size = OUT_PACKET_HEADER_SIZE;
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900864 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100865 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900866 amdtp_stream_get_max_payload(s), dir);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100867 if (err < 0)
868 goto err_unlock;
869
870 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900871 type, channel, speed, header_size,
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900872 amdtp_stream_first_callback, s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100873 if (IS_ERR(s->context)) {
874 err = PTR_ERR(s->context);
875 if (err == -EBUSY)
876 dev_err(&s->unit->device,
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900877 "no free stream on this controller\n");
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100878 goto err_buffer;
879 }
880
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900881 amdtp_stream_update(s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100882
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100883 s->packet_index = 0;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900884 do {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900885 if (s->direction == AMDTP_IN_STREAM)
886 err = queue_in_packet(s);
887 else
888 err = queue_out_packet(s, 0, true);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900889 if (err < 0)
890 goto err_context;
891 } while (s->packet_index > 0);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100892
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900893 /* NOTE: TAG1 matches CIP. This just affects in stream. */
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900894 tag = FW_ISO_CONTEXT_MATCH_TAG1;
895 if (s->flags & CIP_EMPTY_WITH_TAG0)
896 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
897
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900898 s->callbacked = false;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900899 err = fw_iso_context_start(s->context, -1, 0, tag);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100900 if (err < 0)
901 goto err_context;
902
903 mutex_unlock(&s->mutex);
904
905 return 0;
906
907err_context:
908 fw_iso_context_destroy(s->context);
909 s->context = ERR_PTR(-1);
910err_buffer:
911 iso_packets_buffer_destroy(&s->buffer, s->unit);
912err_unlock:
913 mutex_unlock(&s->mutex);
914
915 return err;
916}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900917EXPORT_SYMBOL(amdtp_stream_start);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100918
919/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900920 * amdtp_stream_pcm_pointer - get the PCM buffer position
921 * @s: the AMDTP stream that transports the PCM data
Clemens Ladische9148dd2012-05-13 18:49:14 +0200922 *
923 * Returns the current buffer position, in frames.
924 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900925unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
Clemens Ladische9148dd2012-05-13 18:49:14 +0200926{
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200927 /* this optimization is allowed to be racy */
Takashi Sakamotoc8de6db2014-04-25 22:44:53 +0900928 if (s->pointer_flush && amdtp_stream_running(s))
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200929 fw_iso_context_flush_completions(s->context);
930 else
931 s->pointer_flush = true;
Clemens Ladische9148dd2012-05-13 18:49:14 +0200932
933 return ACCESS_ONCE(s->pcm_buffer_pointer);
934}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900935EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
Clemens Ladische9148dd2012-05-13 18:49:14 +0200936
937/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900938 * amdtp_stream_update - update the stream after a bus reset
939 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100940 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900941void amdtp_stream_update(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100942{
943 ACCESS_ONCE(s->source_node_id_field) =
944 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
945}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900946EXPORT_SYMBOL(amdtp_stream_update);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100947
948/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900949 * amdtp_stream_stop - stop sending packets
950 * @s: the AMDTP stream to stop
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100951 *
952 * All PCM and MIDI devices of the stream must be stopped before the stream
953 * itself can be stopped.
954 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900955void amdtp_stream_stop(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100956{
957 mutex_lock(&s->mutex);
958
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900959 if (!amdtp_stream_running(s)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100960 mutex_unlock(&s->mutex);
961 return;
962 }
963
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200964 tasklet_kill(&s->period_tasklet);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100965 fw_iso_context_stop(s->context);
966 fw_iso_context_destroy(s->context);
967 s->context = ERR_PTR(-1);
968 iso_packets_buffer_destroy(&s->buffer, s->unit);
969
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900970 s->callbacked = false;
971
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100972 mutex_unlock(&s->mutex);
973}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900974EXPORT_SYMBOL(amdtp_stream_stop);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100975
976/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900977 * amdtp_stream_pcm_abort - abort the running PCM device
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100978 * @s: the AMDTP stream about to be stopped
979 *
980 * If the isochronous stream needs to be stopped asynchronously, call this
981 * function first to stop the PCM device.
982 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900983void amdtp_stream_pcm_abort(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100984{
985 struct snd_pcm_substream *pcm;
986
987 pcm = ACCESS_ONCE(s->pcm);
988 if (pcm) {
989 snd_pcm_stream_lock_irq(pcm);
990 if (snd_pcm_running(pcm))
991 snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
992 snd_pcm_stream_unlock_irq(pcm);
993 }
994}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900995EXPORT_SYMBOL(amdtp_stream_pcm_abort);