blob: a4553ec1b63e31111f8c1ed51fdb945b73c74f7d [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>
14#include <sound/pcm.h>
Takashi Sakamoto83d8d722014-04-25 22:44:47 +090015#include <sound/rawmidi.h>
Clemens Ladisch31ef9132011-03-15 07:53:21 +010016#include "amdtp.h"
17
18#define TICKS_PER_CYCLE 3072
19#define CYCLES_PER_SECOND 8000
20#define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
21
22#define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 µs */
23
Takashi Sakamotob445db42014-04-25 22:44:43 +090024/* isochronous header parameters */
25#define ISO_DATA_LENGTH_SHIFT 16
Clemens Ladisch31ef9132011-03-15 07:53:21 +010026#define TAG_CIP 1
27
Takashi Sakamotob445db42014-04-25 22:44:43 +090028/* common isochronous packet header parameters */
Clemens Ladisch31ef9132011-03-15 07:53:21 +010029#define CIP_EOH (1u << 31)
Takashi Sakamotob445db42014-04-25 22:44:43 +090030#define CIP_EOH_MASK 0x80000000
Clemens Ladisch31ef9132011-03-15 07:53:21 +010031#define CIP_FMT_AM (0x10 << 24)
Takashi Sakamotob445db42014-04-25 22:44:43 +090032#define CIP_FMT_MASK 0x3f000000
33#define CIP_SYT_MASK 0x0000ffff
34#define CIP_SYT_NO_INFO 0xffff
35#define CIP_FDF_MASK 0x00ff0000
36#define CIP_FDF_SFC_SHIFT 16
37
38/*
39 * Audio and Music transfer protocol specific parameters
40 * only "Clock-based rate control mode" is supported
41 */
42#define AMDTP_FDF_AM824 (0 << (CIP_FDF_SFC_SHIFT + 3))
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090043#define AMDTP_FDF_NO_DATA 0xff
Takashi Sakamotob445db42014-04-25 22:44:43 +090044#define AMDTP_DBS_MASK 0x00ff0000
45#define AMDTP_DBS_SHIFT 16
46#define AMDTP_DBC_MASK 0x000000ff
Clemens Ladisch31ef9132011-03-15 07:53:21 +010047
48/* TODO: make these configurable */
49#define INTERRUPT_INTERVAL 16
50#define QUEUE_LENGTH 48
51
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090052#define IN_PACKET_HEADER_SIZE 4
Takashi Sakamoto4b7da112014-04-25 22:44:45 +090053#define OUT_PACKET_HEADER_SIZE 0
54
Clemens Ladisch76fb8782012-05-13 22:03:09 +020055static void pcm_period_tasklet(unsigned long data);
56
Clemens Ladisch31ef9132011-03-15 07:53:21 +010057/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090058 * amdtp_stream_init - initialize an AMDTP stream structure
59 * @s: the AMDTP stream to initialize
Clemens Ladisch31ef9132011-03-15 07:53:21 +010060 * @unit: the target of the stream
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090061 * @dir: the direction of stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +010062 * @flags: the packet transmission method to use
63 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090064int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090065 enum amdtp_stream_direction dir, enum cip_flags flags)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010066{
Clemens Ladisch31ef9132011-03-15 07:53:21 +010067 s->unit = fw_unit_get(unit);
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090068 s->direction = dir;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010069 s->flags = flags;
70 s->context = ERR_PTR(-1);
71 mutex_init(&s->mutex);
Clemens Ladisch76fb8782012-05-13 22:03:09 +020072 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +010073 s->packet_index = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010074
75 return 0;
76}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090077EXPORT_SYMBOL(amdtp_stream_init);
Clemens Ladisch31ef9132011-03-15 07:53:21 +010078
79/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090080 * amdtp_stream_destroy - free stream resources
81 * @s: the AMDTP stream to destroy
Clemens Ladisch31ef9132011-03-15 07:53:21 +010082 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090083void amdtp_stream_destroy(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010084{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090085 WARN_ON(amdtp_stream_running(s));
Clemens Ladisch31ef9132011-03-15 07:53:21 +010086 mutex_destroy(&s->mutex);
87 fw_unit_put(s->unit);
88}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090089EXPORT_SYMBOL(amdtp_stream_destroy);
Clemens Ladisch31ef9132011-03-15 07:53:21 +010090
Clemens Ladischc5280e92011-10-16 21:39:00 +020091const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
Clemens Ladischa7304e32011-09-04 22:16:10 +020092 [CIP_SFC_32000] = 8,
93 [CIP_SFC_44100] = 8,
94 [CIP_SFC_48000] = 8,
95 [CIP_SFC_88200] = 16,
96 [CIP_SFC_96000] = 16,
97 [CIP_SFC_176400] = 32,
98 [CIP_SFC_192000] = 32,
99};
100EXPORT_SYMBOL(amdtp_syt_intervals);
101
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100102/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900103 * amdtp_stream_set_parameters - set stream parameters
104 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100105 * @rate: the sample rate
Clemens Ladischa7304e32011-09-04 22:16:10 +0200106 * @pcm_channels: the number of PCM samples in each data block, to be encoded
107 * as AM824 multi-bit linear audio
108 * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100109 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200110 * The parameters must be set before the stream is started, and must not be
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100111 * changed while the stream is running.
112 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900113void amdtp_stream_set_parameters(struct amdtp_stream *s,
114 unsigned int rate,
115 unsigned int pcm_channels,
116 unsigned int midi_ports)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100117{
Clemens Ladischa7304e32011-09-04 22:16:10 +0200118 static const unsigned int rates[] = {
119 [CIP_SFC_32000] = 32000,
120 [CIP_SFC_44100] = 44100,
121 [CIP_SFC_48000] = 48000,
122 [CIP_SFC_88200] = 88200,
123 [CIP_SFC_96000] = 96000,
124 [CIP_SFC_176400] = 176400,
125 [CIP_SFC_192000] = 192000,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100126 };
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900127 unsigned int sfc, midi_channels;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100128
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900129 midi_channels = DIV_ROUND_UP(midi_ports, 8);
130
131 if (WARN_ON(amdtp_stream_running(s)) ||
132 WARN_ON(midi_channels > AMDTP_MAX_CHANNELS_FOR_MIDI))
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100133 return;
134
Clemens Ladischa7304e32011-09-04 22:16:10 +0200135 for (sfc = 0; sfc < CIP_SFC_COUNT; ++sfc)
136 if (rates[sfc] == rate)
Clemens Ladische84d15f2011-09-04 22:12:48 +0200137 goto sfc_found;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100138 WARN_ON(1);
Clemens Ladische84d15f2011-09-04 22:12:48 +0200139 return;
140
141sfc_found:
Clemens Ladischa7304e32011-09-04 22:16:10 +0200142 s->dual_wire = (s->flags & CIP_HI_DUALWIRE) && sfc > CIP_SFC_96000;
143 if (s->dual_wire) {
144 sfc -= 2;
145 rate /= 2;
146 pcm_channels *= 2;
147 }
Clemens Ladische84d15f2011-09-04 22:12:48 +0200148 s->sfc = sfc;
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900149 s->data_block_quadlets = pcm_channels + midi_channels;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200150 s->pcm_channels = pcm_channels;
151 s->midi_ports = midi_ports;
152
153 s->syt_interval = amdtp_syt_intervals[sfc];
Clemens Ladische84d15f2011-09-04 22:12:48 +0200154
155 /* default buffering in the device */
156 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
157 if (s->flags & CIP_BLOCKING)
158 /* additional buffering needed to adjust for no-data packets */
159 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100160}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900161EXPORT_SYMBOL(amdtp_stream_set_parameters);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100162
163/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900164 * amdtp_stream_get_max_payload - get the stream's packet size
165 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100166 *
167 * This function must not be called before the stream has been configured
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900168 * with amdtp_stream_set_parameters().
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100169 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900170unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100171{
Clemens Ladische84d15f2011-09-04 22:12:48 +0200172 return 8 + s->syt_interval * s->data_block_quadlets * 4;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100173}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900174EXPORT_SYMBOL(amdtp_stream_get_max_payload);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100175
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900176static void amdtp_write_s16(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100177 struct snd_pcm_substream *pcm,
178 __be32 *buffer, unsigned int frames);
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900179static void amdtp_write_s32(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100180 struct snd_pcm_substream *pcm,
181 __be32 *buffer, unsigned int frames);
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900182static void amdtp_write_s16_dualwire(struct amdtp_stream *s,
Clemens Ladischa7304e32011-09-04 22:16:10 +0200183 struct snd_pcm_substream *pcm,
184 __be32 *buffer, unsigned int frames);
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900185static void amdtp_write_s32_dualwire(struct amdtp_stream *s,
Clemens Ladischa7304e32011-09-04 22:16:10 +0200186 struct snd_pcm_substream *pcm,
187 __be32 *buffer, unsigned int frames);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900188static void amdtp_read_s32(struct amdtp_stream *s,
189 struct snd_pcm_substream *pcm,
190 __be32 *buffer, unsigned int frames);
191static void amdtp_read_s32_dualwire(struct amdtp_stream *s,
192 struct snd_pcm_substream *pcm,
193 __be32 *buffer, unsigned int frames);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100194
195/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900196 * amdtp_stream_set_pcm_format - set the PCM format
197 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100198 * @format: the format of the ALSA PCM device
199 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200200 * The sample format must be set after the other paramters (rate/PCM channels/
201 * MIDI) and before the stream is started, and must not be changed while the
202 * stream is running.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100203 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900204void amdtp_stream_set_pcm_format(struct amdtp_stream *s,
205 snd_pcm_format_t format)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100206{
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900207 if (WARN_ON(amdtp_stream_pcm_running(s)))
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100208 return;
209
210 switch (format) {
211 default:
212 WARN_ON(1);
213 /* fall through */
214 case SNDRV_PCM_FORMAT_S16:
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900215 if (s->direction == AMDTP_OUT_STREAM) {
216 if (s->dual_wire)
217 s->transfer_samples = amdtp_write_s16_dualwire;
218 else
219 s->transfer_samples = amdtp_write_s16;
220 break;
221 }
222 WARN_ON(1);
223 /* fall through */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100224 case SNDRV_PCM_FORMAT_S32:
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900225 if (s->direction == AMDTP_OUT_STREAM) {
226 if (s->dual_wire)
227 s->transfer_samples = amdtp_write_s32_dualwire;
228 else
229 s->transfer_samples = amdtp_write_s32;
230 } else {
231 if (s->dual_wire)
232 s->transfer_samples = amdtp_read_s32_dualwire;
233 else
234 s->transfer_samples = amdtp_read_s32;
235 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100236 break;
237 }
238}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900239EXPORT_SYMBOL(amdtp_stream_set_pcm_format);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100240
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200241/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900242 * amdtp_stream_pcm_prepare - prepare PCM device for running
243 * @s: the AMDTP stream
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200244 *
245 * This function should be called from the PCM device's .prepare callback.
246 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900247void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200248{
249 tasklet_kill(&s->period_tasklet);
250 s->pcm_buffer_pointer = 0;
251 s->pcm_period_pointer = 0;
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200252 s->pointer_flush = true;
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200253}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900254EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200255
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900256static unsigned int calculate_data_blocks(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100257{
258 unsigned int phase, data_blocks;
259
260 if (!cip_sfc_is_base_44100(s->sfc)) {
261 /* Sample_rate / 8000 is an integer, and precomputed. */
262 data_blocks = s->data_block_state;
263 } else {
264 phase = s->data_block_state;
265
266 /*
267 * This calculates the number of data blocks per packet so that
268 * 1) the overall rate is correct and exactly synchronized to
269 * the bus clock, and
270 * 2) packets with a rounded-up number of blocks occur as early
271 * as possible in the sequence (to prevent underruns of the
272 * device's buffer).
273 */
274 if (s->sfc == CIP_SFC_44100)
275 /* 6 6 5 6 5 6 5 ... */
276 data_blocks = 5 + ((phase & 1) ^
277 (phase == 0 || phase >= 40));
278 else
279 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
280 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
281 if (++phase >= (80 >> (s->sfc >> 1)))
282 phase = 0;
283 s->data_block_state = phase;
284 }
285
286 return data_blocks;
287}
288
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900289static unsigned int calculate_syt(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100290 unsigned int cycle)
291{
292 unsigned int syt_offset, phase, index, syt;
293
294 if (s->last_syt_offset < TICKS_PER_CYCLE) {
295 if (!cip_sfc_is_base_44100(s->sfc))
296 syt_offset = s->last_syt_offset + s->syt_offset_state;
297 else {
298 /*
299 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
300 * n * SYT_INTERVAL * 24576000 / sample_rate
301 * Modulo TICKS_PER_CYCLE, the difference between successive
302 * elements is about 1386.23. Rounding the results of this
303 * formula to the SYT precision results in a sequence of
304 * differences that begins with:
305 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
306 * This code generates _exactly_ the same sequence.
307 */
308 phase = s->syt_offset_state;
309 index = phase % 13;
310 syt_offset = s->last_syt_offset;
311 syt_offset += 1386 + ((index && !(index & 3)) ||
312 phase == 146);
313 if (++phase >= 147)
314 phase = 0;
315 s->syt_offset_state = phase;
316 }
317 } else
318 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
319 s->last_syt_offset = syt_offset;
320
Clemens Ladischbe454362011-03-15 07:55:02 +0100321 if (syt_offset < TICKS_PER_CYCLE) {
Clemens Ladische84d15f2011-09-04 22:12:48 +0200322 syt_offset += s->transfer_delay;
Clemens Ladischbe454362011-03-15 07:55:02 +0100323 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
324 syt += syt_offset % TICKS_PER_CYCLE;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100325
Takashi Sakamotob445db42014-04-25 22:44:43 +0900326 return syt & CIP_SYT_MASK;
Clemens Ladischbe454362011-03-15 07:55:02 +0100327 } else {
Takashi Sakamotob445db42014-04-25 22:44:43 +0900328 return CIP_SYT_NO_INFO;
Clemens Ladischbe454362011-03-15 07:55:02 +0100329 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100330}
331
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900332static void amdtp_write_s32(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100333 struct snd_pcm_substream *pcm,
334 __be32 *buffer, unsigned int frames)
335{
336 struct snd_pcm_runtime *runtime = pcm->runtime;
337 unsigned int channels, remaining_frames, frame_step, i, c;
338 const u32 *src;
339
340 channels = s->pcm_channels;
341 src = (void *)runtime->dma_area +
Takashi Sakamotoe84841f2013-09-14 00:35:47 +0900342 frames_to_bytes(runtime, s->pcm_buffer_pointer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100343 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
344 frame_step = s->data_block_quadlets - channels;
345
346 for (i = 0; i < frames; ++i) {
347 for (c = 0; c < channels; ++c) {
348 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
349 src++;
350 buffer++;
351 }
352 buffer += frame_step;
353 if (--remaining_frames == 0)
354 src = (void *)runtime->dma_area;
355 }
356}
357
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900358static void amdtp_write_s16(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100359 struct snd_pcm_substream *pcm,
360 __be32 *buffer, unsigned int frames)
361{
362 struct snd_pcm_runtime *runtime = pcm->runtime;
363 unsigned int channels, remaining_frames, frame_step, i, c;
364 const u16 *src;
365
366 channels = s->pcm_channels;
367 src = (void *)runtime->dma_area +
Takashi Sakamotoe84841f2013-09-14 00:35:47 +0900368 frames_to_bytes(runtime, s->pcm_buffer_pointer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100369 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
370 frame_step = s->data_block_quadlets - channels;
371
372 for (i = 0; i < frames; ++i) {
373 for (c = 0; c < channels; ++c) {
374 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
375 src++;
376 buffer++;
377 }
378 buffer += frame_step;
379 if (--remaining_frames == 0)
380 src = (void *)runtime->dma_area;
381 }
382}
383
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900384static void amdtp_write_s32_dualwire(struct amdtp_stream *s,
Clemens Ladischa7304e32011-09-04 22:16:10 +0200385 struct snd_pcm_substream *pcm,
386 __be32 *buffer, unsigned int frames)
387{
388 struct snd_pcm_runtime *runtime = pcm->runtime;
389 unsigned int channels, frame_adjust_1, frame_adjust_2, i, c;
390 const u32 *src;
391
392 channels = s->pcm_channels;
393 src = (void *)runtime->dma_area +
394 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
395 frame_adjust_1 = channels - 1;
396 frame_adjust_2 = 1 - (s->data_block_quadlets - channels);
397
398 channels /= 2;
399 for (i = 0; i < frames; ++i) {
400 for (c = 0; c < channels; ++c) {
401 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
402 src++;
403 buffer += 2;
404 }
405 buffer -= frame_adjust_1;
406 for (c = 0; c < channels; ++c) {
407 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
408 src++;
409 buffer += 2;
410 }
411 buffer -= frame_adjust_2;
412 }
413}
414
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900415static void amdtp_write_s16_dualwire(struct amdtp_stream *s,
Clemens Ladischa7304e32011-09-04 22:16:10 +0200416 struct snd_pcm_substream *pcm,
417 __be32 *buffer, unsigned int frames)
418{
419 struct snd_pcm_runtime *runtime = pcm->runtime;
420 unsigned int channels, frame_adjust_1, frame_adjust_2, i, c;
421 const u16 *src;
422
423 channels = s->pcm_channels;
424 src = (void *)runtime->dma_area +
425 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
426 frame_adjust_1 = channels - 1;
427 frame_adjust_2 = 1 - (s->data_block_quadlets - channels);
428
429 channels /= 2;
430 for (i = 0; i < frames; ++i) {
431 for (c = 0; c < channels; ++c) {
432 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
433 src++;
434 buffer += 2;
435 }
436 buffer -= frame_adjust_1;
437 for (c = 0; c < channels; ++c) {
438 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
439 src++;
440 buffer += 2;
441 }
442 buffer -= frame_adjust_2;
443 }
444}
445
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900446static void amdtp_read_s32(struct amdtp_stream *s,
447 struct snd_pcm_substream *pcm,
448 __be32 *buffer, unsigned int frames)
449{
450 struct snd_pcm_runtime *runtime = pcm->runtime;
451 unsigned int channels, remaining_frames, i, c;
452 u32 *dst;
453
454 channels = s->pcm_channels;
455 dst = (void *)runtime->dma_area +
456 frames_to_bytes(runtime, s->pcm_buffer_pointer);
457 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
458
459 for (i = 0; i < frames; ++i) {
460 for (c = 0; c < channels; ++c) {
461 *dst = be32_to_cpu(buffer[c]) << 8;
462 dst++;
463 }
464 buffer += s->data_block_quadlets;
465 if (--remaining_frames == 0)
466 dst = (void *)runtime->dma_area;
467 }
468}
469
470static void amdtp_read_s32_dualwire(struct amdtp_stream *s,
471 struct snd_pcm_substream *pcm,
472 __be32 *buffer, unsigned int frames)
473{
474 struct snd_pcm_runtime *runtime = pcm->runtime;
475 unsigned int channels, remaining_frames, i, c;
476 u32 *dst;
477
478 dst = (void *)runtime->dma_area +
479 frames_to_bytes(runtime, s->pcm_buffer_pointer);
480 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
481 channels = s->pcm_channels / 2;
482
483 for (i = 0; i < frames; ++i) {
484 for (c = 0; c < channels; ++c) {
485 *dst = be32_to_cpu(buffer[c * 2]) << 8;
486 dst++;
487 }
488 buffer += 1;
489 for (c = 0; c < channels; ++c) {
490 *dst = be32_to_cpu(buffer[c * 2]) << 8;
491 dst++;
492 }
493 buffer += s->data_block_quadlets - 1;
494 if (--remaining_frames == 0)
495 dst = (void *)runtime->dma_area;
496 }
497}
498
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900499static void amdtp_fill_pcm_silence(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100500 __be32 *buffer, unsigned int frames)
501{
502 unsigned int i, c;
503
504 for (i = 0; i < frames; ++i) {
505 for (c = 0; c < s->pcm_channels; ++c)
506 buffer[c] = cpu_to_be32(0x40000000);
507 buffer += s->data_block_quadlets;
508 }
509}
510
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900511static void amdtp_fill_midi(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100512 __be32 *buffer, unsigned int frames)
513{
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900514 unsigned int f, port;
515 u8 *b;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100516
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900517 for (f = 0; f < frames; f++) {
518 buffer[s->pcm_channels + 1] = 0;
519 b = (u8 *)&buffer[s->pcm_channels + 1];
520
521 port = (s->data_block_counter + f) % 8;
522 if ((s->midi[port] == NULL) ||
523 (snd_rawmidi_transmit(s->midi[port], b + 1, 1) <= 0))
524 b[0] = 0x80;
525 else
526 b[0] = 0x81;
527
528 buffer += s->data_block_quadlets;
529 }
530}
531
532static void amdtp_pull_midi(struct amdtp_stream *s,
533 __be32 *buffer, unsigned int frames)
534{
535 unsigned int f, port;
536 int len;
537 u8 *b;
538
539 for (f = 0; f < frames; f++) {
540 port = (s->data_block_counter + f) % 8;
541 b = (u8 *)&buffer[s->pcm_channels + 1];
542
543 len = b[0] - 0x80;
544 if ((1 <= len) && (len <= 3) && (s->midi[port]))
545 snd_rawmidi_receive(s->midi[port], b + 1, len);
546
547 buffer += s->data_block_quadlets;
548 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100549}
550
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900551static void update_pcm_pointers(struct amdtp_stream *s,
552 struct snd_pcm_substream *pcm,
553 unsigned int frames)
554{ unsigned int ptr;
555
556 if (s->dual_wire)
557 frames *= 2;
558
559 ptr = s->pcm_buffer_pointer + frames;
560 if (ptr >= pcm->runtime->buffer_size)
561 ptr -= pcm->runtime->buffer_size;
562 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
563
564 s->pcm_period_pointer += frames;
565 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
566 s->pcm_period_pointer -= pcm->runtime->period_size;
567 s->pointer_flush = false;
568 tasklet_hi_schedule(&s->period_tasklet);
569 }
570}
571
572static void pcm_period_tasklet(unsigned long data)
573{
574 struct amdtp_stream *s = (void *)data;
575 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
576
577 if (pcm)
578 snd_pcm_period_elapsed(pcm);
579}
580
581static int queue_packet(struct amdtp_stream *s,
582 unsigned int header_length,
583 unsigned int payload_length, bool skip)
584{
585 struct fw_iso_packet p = {0};
586 int err;
587
588 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
589 p.tag = TAG_CIP;
590 p.header_length = header_length;
591 p.payload_length = (!skip) ? payload_length : 0;
592 p.skip = skip;
593 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
594 s->buffer.packets[s->packet_index].offset);
595 if (err < 0) {
596 dev_err(&s->unit->device, "queueing error: %d\n", err);
597 goto end;
598 }
599
600 if (++s->packet_index >= QUEUE_LENGTH)
601 s->packet_index = 0;
602end:
603 return err;
604}
605
606static inline int queue_out_packet(struct amdtp_stream *s,
607 unsigned int payload_length, bool skip)
608{
609 return queue_packet(s, OUT_PACKET_HEADER_SIZE,
610 payload_length, skip);
611}
612
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900613static inline int queue_in_packet(struct amdtp_stream *s)
614{
615 return queue_packet(s, IN_PACKET_HEADER_SIZE,
616 amdtp_stream_get_max_payload(s), false);
617}
618
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900619static void handle_out_packet(struct amdtp_stream *s, unsigned int cycle)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100620{
621 __be32 *buffer;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900622 unsigned int index, data_blocks, syt, payload_length;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100623 struct snd_pcm_substream *pcm;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100624
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100625 if (s->packet_index < 0)
626 return;
627 index = s->packet_index;
628
Takashi Sakamoto82755ab2013-11-21 14:21:06 +0900629 /* this module generate empty packet for 'no data' */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100630 syt = calculate_syt(s, cycle);
Takashi Sakamoto82755ab2013-11-21 14:21:06 +0900631 if (!(s->flags & CIP_BLOCKING))
Clemens Ladische84d15f2011-09-04 22:12:48 +0200632 data_blocks = calculate_data_blocks(s);
Takashi Sakamotob445db42014-04-25 22:44:43 +0900633 else if (syt != CIP_SYT_NO_INFO)
Takashi Sakamoto82755ab2013-11-21 14:21:06 +0900634 data_blocks = s->syt_interval;
635 else
636 data_blocks = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100637
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100638 buffer = s->buffer.packets[index].buffer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100639 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
Takashi Sakamotob445db42014-04-25 22:44:43 +0900640 (s->data_block_quadlets << AMDTP_DBS_SHIFT) |
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100641 s->data_block_counter);
642 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
Takashi Sakamotob445db42014-04-25 22:44:43 +0900643 (s->sfc << CIP_FDF_SFC_SHIFT) | syt);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100644 buffer += 2;
645
646 pcm = ACCESS_ONCE(s->pcm);
647 if (pcm)
648 s->transfer_samples(s, pcm, buffer, data_blocks);
649 else
650 amdtp_fill_pcm_silence(s, buffer, data_blocks);
651 if (s->midi_ports)
652 amdtp_fill_midi(s, buffer, data_blocks);
653
654 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
655
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900656 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
657 if (queue_out_packet(s, payload_length, false) < 0) {
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100658 s->packet_index = -1;
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900659 amdtp_stream_pcm_abort(s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100660 return;
661 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100662
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200663 if (pcm)
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900664 update_pcm_pointers(s, pcm, data_blocks);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200665}
666
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900667static void handle_in_packet(struct amdtp_stream *s,
668 unsigned int payload_quadlets,
669 __be32 *buffer)
670{
671 u32 cip_header[2];
672 unsigned int data_blocks, data_block_quadlets, data_block_counter;
673 struct snd_pcm_substream *pcm = NULL;
674
675 cip_header[0] = be32_to_cpu(buffer[0]);
676 cip_header[1] = be32_to_cpu(buffer[1]);
677
678 /*
679 * This module supports 'Two-quadlet CIP header with SYT field'.
680 * For convinience, also check FMT field is AM824 or not.
681 */
682 if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
683 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH) ||
684 ((cip_header[1] & CIP_FMT_MASK) != CIP_FMT_AM)) {
685 dev_info_ratelimited(&s->unit->device,
686 "Invalid CIP header for AMDTP: %08X:%08X\n",
687 cip_header[0], cip_header[1]);
688 goto end;
689 }
690
691 /* Calculate data blocks */
692 if (payload_quadlets < 3 ||
693 ((cip_header[1] & CIP_FDF_MASK) ==
694 (AMDTP_FDF_NO_DATA << CIP_FDF_SFC_SHIFT))) {
695 data_blocks = 0;
696 } else {
697 data_block_quadlets =
698 (cip_header[0] & AMDTP_DBS_MASK) >> AMDTP_DBS_SHIFT;
699 /* avoid division by zero */
700 if (data_block_quadlets == 0) {
701 dev_info_ratelimited(&s->unit->device,
702 "Detect invalid value in dbs field: %08X\n",
703 cip_header[0]);
704 goto err;
705 }
706
707 data_blocks = (payload_quadlets - 2) / data_block_quadlets;
708 }
709
710 /* Check data block counter continuity */
711 data_block_counter = cip_header[0] & AMDTP_DBC_MASK;
712 if (data_block_counter != s->data_block_counter) {
713 dev_info(&s->unit->device,
714 "Detect discontinuity of CIP: %02X %02X\n",
715 s->data_block_counter, data_block_counter);
716 goto err;
717 }
718
719 if (data_blocks > 0) {
720 buffer += 2;
721
722 pcm = ACCESS_ONCE(s->pcm);
723 if (pcm)
724 s->transfer_samples(s, pcm, buffer, data_blocks);
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900725
726 if (s->midi_ports)
727 amdtp_pull_midi(s, buffer, data_blocks);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900728 }
729
730 s->data_block_counter = (data_block_counter + data_blocks) & 0xff;
731end:
732 if (queue_in_packet(s) < 0)
733 goto err;
734
735 if (pcm)
736 update_pcm_pointers(s, pcm, data_blocks);
737
738 return;
739err:
740 s->packet_index = -1;
741 amdtp_stream_pcm_abort(s);
742}
743
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900744static void out_stream_callback(struct fw_iso_context *context, u32 cycle,
745 size_t header_length, void *header,
746 void *private_data)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100747{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900748 struct amdtp_stream *s = private_data;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100749 unsigned int i, packets = header_length / 4;
750
751 /*
752 * Compute the cycle of the last queued packet.
753 * (We need only the four lowest bits for the SYT, so we can ignore
754 * that bits 0-11 must wrap around at 3072.)
755 */
756 cycle += QUEUE_LENGTH - packets;
757
758 for (i = 0; i < packets; ++i)
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900759 handle_out_packet(s, ++cycle);
Clemens Ladisch13882a82011-05-02 09:33:56 +0200760 fw_iso_context_queue_flush(s->context);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100761}
762
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900763static void in_stream_callback(struct fw_iso_context *context, u32 cycle,
764 size_t header_length, void *header,
765 void *private_data)
766{
767 struct amdtp_stream *s = private_data;
768 unsigned int p, packets, payload_quadlets;
769 __be32 *buffer, *headers = header;
770
771 /* The number of packets in buffer */
772 packets = header_length / IN_PACKET_HEADER_SIZE;
773
774 for (p = 0; p < packets; p++) {
775 if (s->packet_index < 0)
776 return;
777 buffer = s->buffer.packets[s->packet_index].buffer;
778
779 /* The number of quadlets in this packet */
780 payload_quadlets =
781 (be32_to_cpu(headers[p]) >> ISO_DATA_LENGTH_SHIFT) / 4;
782 handle_in_packet(s, payload_quadlets, buffer);
783 }
784
785 fw_iso_context_queue_flush(s->context);
786}
787
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100788/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900789 * amdtp_stream_start - start transferring packets
790 * @s: the AMDTP stream to start
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100791 * @channel: the isochronous channel on the bus
792 * @speed: firewire speed code
793 *
794 * The stream cannot be started until it has been configured with
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900795 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
796 * device can be started.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100797 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900798int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100799{
800 static const struct {
801 unsigned int data_block;
802 unsigned int syt_offset;
803 } initial_state[] = {
804 [CIP_SFC_32000] = { 4, 3072 },
805 [CIP_SFC_48000] = { 6, 1024 },
806 [CIP_SFC_96000] = { 12, 1024 },
807 [CIP_SFC_192000] = { 24, 1024 },
808 [CIP_SFC_44100] = { 0, 67 },
809 [CIP_SFC_88200] = { 0, 67 },
810 [CIP_SFC_176400] = { 0, 67 },
811 };
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900812 unsigned int header_size;
813 enum dma_data_direction dir;
814 fw_iso_callback_t cb;
815 int type, err;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100816
817 mutex_lock(&s->mutex);
818
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900819 if (WARN_ON(amdtp_stream_running(s) ||
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900820 (s->data_block_quadlets < 1))) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100821 err = -EBADFD;
822 goto err_unlock;
823 }
824
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900825 s->data_block_counter = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100826 s->data_block_state = initial_state[s->sfc].data_block;
827 s->syt_offset_state = initial_state[s->sfc].syt_offset;
828 s->last_syt_offset = TICKS_PER_CYCLE;
829
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900830 /* initialize packet buffer */
831 if (s->direction == AMDTP_IN_STREAM) {
832 dir = DMA_FROM_DEVICE;
833 type = FW_ISO_CONTEXT_RECEIVE;
834 header_size = IN_PACKET_HEADER_SIZE;
835 cb = in_stream_callback;
836 } else {
837 dir = DMA_TO_DEVICE;
838 type = FW_ISO_CONTEXT_TRANSMIT;
839 header_size = OUT_PACKET_HEADER_SIZE;
840 cb = out_stream_callback;
841 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100842 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900843 amdtp_stream_get_max_payload(s), dir);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100844 if (err < 0)
845 goto err_unlock;
846
847 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900848 type, channel, speed, header_size,
849 cb, s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100850 if (IS_ERR(s->context)) {
851 err = PTR_ERR(s->context);
852 if (err == -EBUSY)
853 dev_err(&s->unit->device,
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900854 "no free stream on this controller\n");
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100855 goto err_buffer;
856 }
857
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900858 amdtp_stream_update(s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100859
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100860 s->packet_index = 0;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900861 do {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900862 if (s->direction == AMDTP_IN_STREAM)
863 err = queue_in_packet(s);
864 else
865 err = queue_out_packet(s, 0, true);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900866 if (err < 0)
867 goto err_context;
868 } while (s->packet_index > 0);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100869
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900870 /* NOTE: TAG1 matches CIP. This just affects in stream. */
871 err = fw_iso_context_start(s->context, -1, 0,
872 FW_ISO_CONTEXT_MATCH_TAG1);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100873 if (err < 0)
874 goto err_context;
875
876 mutex_unlock(&s->mutex);
877
878 return 0;
879
880err_context:
881 fw_iso_context_destroy(s->context);
882 s->context = ERR_PTR(-1);
883err_buffer:
884 iso_packets_buffer_destroy(&s->buffer, s->unit);
885err_unlock:
886 mutex_unlock(&s->mutex);
887
888 return err;
889}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900890EXPORT_SYMBOL(amdtp_stream_start);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100891
892/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900893 * amdtp_stream_pcm_pointer - get the PCM buffer position
894 * @s: the AMDTP stream that transports the PCM data
Clemens Ladische9148dd2012-05-13 18:49:14 +0200895 *
896 * Returns the current buffer position, in frames.
897 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900898unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
Clemens Ladische9148dd2012-05-13 18:49:14 +0200899{
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200900 /* this optimization is allowed to be racy */
901 if (s->pointer_flush)
902 fw_iso_context_flush_completions(s->context);
903 else
904 s->pointer_flush = true;
Clemens Ladische9148dd2012-05-13 18:49:14 +0200905
906 return ACCESS_ONCE(s->pcm_buffer_pointer);
907}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900908EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
Clemens Ladische9148dd2012-05-13 18:49:14 +0200909
910/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900911 * amdtp_stream_update - update the stream after a bus reset
912 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100913 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900914void amdtp_stream_update(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100915{
916 ACCESS_ONCE(s->source_node_id_field) =
917 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
918}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900919EXPORT_SYMBOL(amdtp_stream_update);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100920
921/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900922 * amdtp_stream_stop - stop sending packets
923 * @s: the AMDTP stream to stop
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100924 *
925 * All PCM and MIDI devices of the stream must be stopped before the stream
926 * itself can be stopped.
927 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900928void amdtp_stream_stop(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100929{
930 mutex_lock(&s->mutex);
931
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900932 if (!amdtp_stream_running(s)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100933 mutex_unlock(&s->mutex);
934 return;
935 }
936
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200937 tasklet_kill(&s->period_tasklet);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100938 fw_iso_context_stop(s->context);
939 fw_iso_context_destroy(s->context);
940 s->context = ERR_PTR(-1);
941 iso_packets_buffer_destroy(&s->buffer, s->unit);
942
943 mutex_unlock(&s->mutex);
944}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900945EXPORT_SYMBOL(amdtp_stream_stop);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100946
947/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900948 * amdtp_stream_pcm_abort - abort the running PCM device
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100949 * @s: the AMDTP stream about to be stopped
950 *
951 * If the isochronous stream needs to be stopped asynchronously, call this
952 * function first to stop the PCM device.
953 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900954void amdtp_stream_pcm_abort(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100955{
956 struct snd_pcm_substream *pcm;
957
958 pcm = ACCESS_ONCE(s->pcm);
959 if (pcm) {
960 snd_pcm_stream_lock_irq(pcm);
961 if (snd_pcm_running(pcm))
962 snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
963 snd_pcm_stream_unlock_irq(pcm);
964 }
965}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900966EXPORT_SYMBOL(amdtp_stream_pcm_abort);