blob: be1aabcda0d256fb1d249e39fc81e941ae076de9 [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>
15#include "amdtp.h"
16
17#define TICKS_PER_CYCLE 3072
18#define CYCLES_PER_SECOND 8000
19#define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
20
21#define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 µs */
22
Takashi Sakamotob445db42014-04-25 22:44:43 +090023/* isochronous header parameters */
24#define ISO_DATA_LENGTH_SHIFT 16
Clemens Ladisch31ef9132011-03-15 07:53:21 +010025#define TAG_CIP 1
26
Takashi Sakamotob445db42014-04-25 22:44:43 +090027/* common isochronous packet header parameters */
Clemens Ladisch31ef9132011-03-15 07:53:21 +010028#define CIP_EOH (1u << 31)
Takashi Sakamotob445db42014-04-25 22:44:43 +090029#define CIP_EOH_MASK 0x80000000
Clemens Ladisch31ef9132011-03-15 07:53:21 +010030#define CIP_FMT_AM (0x10 << 24)
Takashi Sakamotob445db42014-04-25 22:44:43 +090031#define CIP_FMT_MASK 0x3f000000
32#define CIP_SYT_MASK 0x0000ffff
33#define CIP_SYT_NO_INFO 0xffff
34#define CIP_FDF_MASK 0x00ff0000
35#define CIP_FDF_SFC_SHIFT 16
36
37/*
38 * Audio and Music transfer protocol specific parameters
39 * only "Clock-based rate control mode" is supported
40 */
41#define AMDTP_FDF_AM824 (0 << (CIP_FDF_SFC_SHIFT + 3))
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090042#define AMDTP_FDF_NO_DATA 0xff
Takashi Sakamotob445db42014-04-25 22:44:43 +090043#define AMDTP_DBS_MASK 0x00ff0000
44#define AMDTP_DBS_SHIFT 16
45#define AMDTP_DBC_MASK 0x000000ff
Clemens Ladisch31ef9132011-03-15 07:53:21 +010046
47/* TODO: make these configurable */
48#define INTERRUPT_INTERVAL 16
49#define QUEUE_LENGTH 48
50
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090051#define IN_PACKET_HEADER_SIZE 4
Takashi Sakamoto4b7da112014-04-25 22:44:45 +090052#define OUT_PACKET_HEADER_SIZE 0
53
Clemens Ladisch76fb8782012-05-13 22:03:09 +020054static void pcm_period_tasklet(unsigned long data);
55
Clemens Ladisch31ef9132011-03-15 07:53:21 +010056/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090057 * amdtp_stream_init - initialize an AMDTP stream structure
58 * @s: the AMDTP stream to initialize
Clemens Ladisch31ef9132011-03-15 07:53:21 +010059 * @unit: the target of the stream
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090060 * @dir: the direction of stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +010061 * @flags: the packet transmission method to use
62 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090063int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090064 enum amdtp_stream_direction dir, enum cip_flags flags)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010065{
Clemens Ladisch31ef9132011-03-15 07:53:21 +010066 s->unit = fw_unit_get(unit);
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090067 s->direction = dir;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010068 s->flags = flags;
69 s->context = ERR_PTR(-1);
70 mutex_init(&s->mutex);
Clemens Ladisch76fb8782012-05-13 22:03:09 +020071 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +010072 s->packet_index = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010073
74 return 0;
75}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090076EXPORT_SYMBOL(amdtp_stream_init);
Clemens Ladisch31ef9132011-03-15 07:53:21 +010077
78/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090079 * amdtp_stream_destroy - free stream resources
80 * @s: the AMDTP stream to destroy
Clemens Ladisch31ef9132011-03-15 07:53:21 +010081 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090082void amdtp_stream_destroy(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010083{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090084 WARN_ON(amdtp_stream_running(s));
Clemens Ladisch31ef9132011-03-15 07:53:21 +010085 mutex_destroy(&s->mutex);
86 fw_unit_put(s->unit);
87}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090088EXPORT_SYMBOL(amdtp_stream_destroy);
Clemens Ladisch31ef9132011-03-15 07:53:21 +010089
Clemens Ladischc5280e92011-10-16 21:39:00 +020090const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
Clemens Ladischa7304e32011-09-04 22:16:10 +020091 [CIP_SFC_32000] = 8,
92 [CIP_SFC_44100] = 8,
93 [CIP_SFC_48000] = 8,
94 [CIP_SFC_88200] = 16,
95 [CIP_SFC_96000] = 16,
96 [CIP_SFC_176400] = 32,
97 [CIP_SFC_192000] = 32,
98};
99EXPORT_SYMBOL(amdtp_syt_intervals);
100
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100101/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900102 * amdtp_stream_set_parameters - set stream parameters
103 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100104 * @rate: the sample rate
Clemens Ladischa7304e32011-09-04 22:16:10 +0200105 * @pcm_channels: the number of PCM samples in each data block, to be encoded
106 * as AM824 multi-bit linear audio
107 * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100108 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200109 * The parameters must be set before the stream is started, and must not be
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100110 * changed while the stream is running.
111 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900112void amdtp_stream_set_parameters(struct amdtp_stream *s,
113 unsigned int rate,
114 unsigned int pcm_channels,
115 unsigned int midi_ports)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100116{
Clemens Ladischa7304e32011-09-04 22:16:10 +0200117 static const unsigned int rates[] = {
118 [CIP_SFC_32000] = 32000,
119 [CIP_SFC_44100] = 44100,
120 [CIP_SFC_48000] = 48000,
121 [CIP_SFC_88200] = 88200,
122 [CIP_SFC_96000] = 96000,
123 [CIP_SFC_176400] = 176400,
124 [CIP_SFC_192000] = 192000,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100125 };
126 unsigned int sfc;
127
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900128 if (WARN_ON(amdtp_stream_running(s)))
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100129 return;
130
Clemens Ladischa7304e32011-09-04 22:16:10 +0200131 for (sfc = 0; sfc < CIP_SFC_COUNT; ++sfc)
132 if (rates[sfc] == rate)
Clemens Ladische84d15f2011-09-04 22:12:48 +0200133 goto sfc_found;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100134 WARN_ON(1);
Clemens Ladische84d15f2011-09-04 22:12:48 +0200135 return;
136
137sfc_found:
Clemens Ladischa7304e32011-09-04 22:16:10 +0200138 s->dual_wire = (s->flags & CIP_HI_DUALWIRE) && sfc > CIP_SFC_96000;
139 if (s->dual_wire) {
140 sfc -= 2;
141 rate /= 2;
142 pcm_channels *= 2;
143 }
Clemens Ladische84d15f2011-09-04 22:12:48 +0200144 s->sfc = sfc;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200145 s->data_block_quadlets = pcm_channels + DIV_ROUND_UP(midi_ports, 8);
146 s->pcm_channels = pcm_channels;
147 s->midi_ports = midi_ports;
148
149 s->syt_interval = amdtp_syt_intervals[sfc];
Clemens Ladische84d15f2011-09-04 22:12:48 +0200150
151 /* default buffering in the device */
152 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
153 if (s->flags & CIP_BLOCKING)
154 /* additional buffering needed to adjust for no-data packets */
155 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100156}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900157EXPORT_SYMBOL(amdtp_stream_set_parameters);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100158
159/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900160 * amdtp_stream_get_max_payload - get the stream's packet size
161 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100162 *
163 * This function must not be called before the stream has been configured
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900164 * with amdtp_stream_set_parameters().
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100165 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900166unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100167{
Clemens Ladische84d15f2011-09-04 22:12:48 +0200168 return 8 + s->syt_interval * s->data_block_quadlets * 4;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100169}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900170EXPORT_SYMBOL(amdtp_stream_get_max_payload);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100171
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900172static void amdtp_write_s16(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100173 struct snd_pcm_substream *pcm,
174 __be32 *buffer, unsigned int frames);
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900175static void amdtp_write_s32(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100176 struct snd_pcm_substream *pcm,
177 __be32 *buffer, unsigned int frames);
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900178static void amdtp_write_s16_dualwire(struct amdtp_stream *s,
Clemens Ladischa7304e32011-09-04 22:16:10 +0200179 struct snd_pcm_substream *pcm,
180 __be32 *buffer, unsigned int frames);
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900181static void amdtp_write_s32_dualwire(struct amdtp_stream *s,
Clemens Ladischa7304e32011-09-04 22:16:10 +0200182 struct snd_pcm_substream *pcm,
183 __be32 *buffer, unsigned int frames);
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900184static void amdtp_read_s32(struct amdtp_stream *s,
185 struct snd_pcm_substream *pcm,
186 __be32 *buffer, unsigned int frames);
187static void amdtp_read_s32_dualwire(struct amdtp_stream *s,
188 struct snd_pcm_substream *pcm,
189 __be32 *buffer, unsigned int frames);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100190
191/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900192 * amdtp_stream_set_pcm_format - set the PCM format
193 * @s: the AMDTP stream to configure
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100194 * @format: the format of the ALSA PCM device
195 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200196 * The sample format must be set after the other paramters (rate/PCM channels/
197 * MIDI) and before the stream is started, and must not be changed while the
198 * stream is running.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100199 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900200void amdtp_stream_set_pcm_format(struct amdtp_stream *s,
201 snd_pcm_format_t format)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100202{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900203 if (WARN_ON(amdtp_stream_running(s)))
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100204 return;
205
206 switch (format) {
207 default:
208 WARN_ON(1);
209 /* fall through */
210 case SNDRV_PCM_FORMAT_S16:
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900211 if (s->direction == AMDTP_OUT_STREAM) {
212 if (s->dual_wire)
213 s->transfer_samples = amdtp_write_s16_dualwire;
214 else
215 s->transfer_samples = amdtp_write_s16;
216 break;
217 }
218 WARN_ON(1);
219 /* fall through */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100220 case SNDRV_PCM_FORMAT_S32:
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900221 if (s->direction == AMDTP_OUT_STREAM) {
222 if (s->dual_wire)
223 s->transfer_samples = amdtp_write_s32_dualwire;
224 else
225 s->transfer_samples = amdtp_write_s32;
226 } else {
227 if (s->dual_wire)
228 s->transfer_samples = amdtp_read_s32_dualwire;
229 else
230 s->transfer_samples = amdtp_read_s32;
231 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100232 break;
233 }
234}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900235EXPORT_SYMBOL(amdtp_stream_set_pcm_format);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100236
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200237/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900238 * amdtp_stream_pcm_prepare - prepare PCM device for running
239 * @s: the AMDTP stream
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200240 *
241 * This function should be called from the PCM device's .prepare callback.
242 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900243void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200244{
245 tasklet_kill(&s->period_tasklet);
246 s->pcm_buffer_pointer = 0;
247 s->pcm_period_pointer = 0;
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200248 s->pointer_flush = true;
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200249}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900250EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200251
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900252static unsigned int calculate_data_blocks(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100253{
254 unsigned int phase, data_blocks;
255
256 if (!cip_sfc_is_base_44100(s->sfc)) {
257 /* Sample_rate / 8000 is an integer, and precomputed. */
258 data_blocks = s->data_block_state;
259 } else {
260 phase = s->data_block_state;
261
262 /*
263 * This calculates the number of data blocks per packet so that
264 * 1) the overall rate is correct and exactly synchronized to
265 * the bus clock, and
266 * 2) packets with a rounded-up number of blocks occur as early
267 * as possible in the sequence (to prevent underruns of the
268 * device's buffer).
269 */
270 if (s->sfc == CIP_SFC_44100)
271 /* 6 6 5 6 5 6 5 ... */
272 data_blocks = 5 + ((phase & 1) ^
273 (phase == 0 || phase >= 40));
274 else
275 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
276 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
277 if (++phase >= (80 >> (s->sfc >> 1)))
278 phase = 0;
279 s->data_block_state = phase;
280 }
281
282 return data_blocks;
283}
284
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900285static unsigned int calculate_syt(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100286 unsigned int cycle)
287{
288 unsigned int syt_offset, phase, index, syt;
289
290 if (s->last_syt_offset < TICKS_PER_CYCLE) {
291 if (!cip_sfc_is_base_44100(s->sfc))
292 syt_offset = s->last_syt_offset + s->syt_offset_state;
293 else {
294 /*
295 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
296 * n * SYT_INTERVAL * 24576000 / sample_rate
297 * Modulo TICKS_PER_CYCLE, the difference between successive
298 * elements is about 1386.23. Rounding the results of this
299 * formula to the SYT precision results in a sequence of
300 * differences that begins with:
301 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
302 * This code generates _exactly_ the same sequence.
303 */
304 phase = s->syt_offset_state;
305 index = phase % 13;
306 syt_offset = s->last_syt_offset;
307 syt_offset += 1386 + ((index && !(index & 3)) ||
308 phase == 146);
309 if (++phase >= 147)
310 phase = 0;
311 s->syt_offset_state = phase;
312 }
313 } else
314 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
315 s->last_syt_offset = syt_offset;
316
Clemens Ladischbe454362011-03-15 07:55:02 +0100317 if (syt_offset < TICKS_PER_CYCLE) {
Clemens Ladische84d15f2011-09-04 22:12:48 +0200318 syt_offset += s->transfer_delay;
Clemens Ladischbe454362011-03-15 07:55:02 +0100319 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
320 syt += syt_offset % TICKS_PER_CYCLE;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100321
Takashi Sakamotob445db42014-04-25 22:44:43 +0900322 return syt & CIP_SYT_MASK;
Clemens Ladischbe454362011-03-15 07:55:02 +0100323 } else {
Takashi Sakamotob445db42014-04-25 22:44:43 +0900324 return CIP_SYT_NO_INFO;
Clemens Ladischbe454362011-03-15 07:55:02 +0100325 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100326}
327
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900328static void amdtp_write_s32(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100329 struct snd_pcm_substream *pcm,
330 __be32 *buffer, unsigned int frames)
331{
332 struct snd_pcm_runtime *runtime = pcm->runtime;
333 unsigned int channels, remaining_frames, frame_step, i, c;
334 const u32 *src;
335
336 channels = s->pcm_channels;
337 src = (void *)runtime->dma_area +
Takashi Sakamotoe84841f2013-09-14 00:35:47 +0900338 frames_to_bytes(runtime, s->pcm_buffer_pointer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100339 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
340 frame_step = s->data_block_quadlets - channels;
341
342 for (i = 0; i < frames; ++i) {
343 for (c = 0; c < channels; ++c) {
344 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
345 src++;
346 buffer++;
347 }
348 buffer += frame_step;
349 if (--remaining_frames == 0)
350 src = (void *)runtime->dma_area;
351 }
352}
353
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900354static void amdtp_write_s16(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100355 struct snd_pcm_substream *pcm,
356 __be32 *buffer, unsigned int frames)
357{
358 struct snd_pcm_runtime *runtime = pcm->runtime;
359 unsigned int channels, remaining_frames, frame_step, i, c;
360 const u16 *src;
361
362 channels = s->pcm_channels;
363 src = (void *)runtime->dma_area +
Takashi Sakamotoe84841f2013-09-14 00:35:47 +0900364 frames_to_bytes(runtime, s->pcm_buffer_pointer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100365 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
366 frame_step = s->data_block_quadlets - channels;
367
368 for (i = 0; i < frames; ++i) {
369 for (c = 0; c < channels; ++c) {
370 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
371 src++;
372 buffer++;
373 }
374 buffer += frame_step;
375 if (--remaining_frames == 0)
376 src = (void *)runtime->dma_area;
377 }
378}
379
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900380static void amdtp_write_s32_dualwire(struct amdtp_stream *s,
Clemens Ladischa7304e32011-09-04 22:16:10 +0200381 struct snd_pcm_substream *pcm,
382 __be32 *buffer, unsigned int frames)
383{
384 struct snd_pcm_runtime *runtime = pcm->runtime;
385 unsigned int channels, frame_adjust_1, frame_adjust_2, i, c;
386 const u32 *src;
387
388 channels = s->pcm_channels;
389 src = (void *)runtime->dma_area +
390 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
391 frame_adjust_1 = channels - 1;
392 frame_adjust_2 = 1 - (s->data_block_quadlets - channels);
393
394 channels /= 2;
395 for (i = 0; i < frames; ++i) {
396 for (c = 0; c < channels; ++c) {
397 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
398 src++;
399 buffer += 2;
400 }
401 buffer -= frame_adjust_1;
402 for (c = 0; c < channels; ++c) {
403 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
404 src++;
405 buffer += 2;
406 }
407 buffer -= frame_adjust_2;
408 }
409}
410
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900411static void amdtp_write_s16_dualwire(struct amdtp_stream *s,
Clemens Ladischa7304e32011-09-04 22:16:10 +0200412 struct snd_pcm_substream *pcm,
413 __be32 *buffer, unsigned int frames)
414{
415 struct snd_pcm_runtime *runtime = pcm->runtime;
416 unsigned int channels, frame_adjust_1, frame_adjust_2, i, c;
417 const u16 *src;
418
419 channels = s->pcm_channels;
420 src = (void *)runtime->dma_area +
421 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
422 frame_adjust_1 = channels - 1;
423 frame_adjust_2 = 1 - (s->data_block_quadlets - channels);
424
425 channels /= 2;
426 for (i = 0; i < frames; ++i) {
427 for (c = 0; c < channels; ++c) {
428 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
429 src++;
430 buffer += 2;
431 }
432 buffer -= frame_adjust_1;
433 for (c = 0; c < channels; ++c) {
434 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
435 src++;
436 buffer += 2;
437 }
438 buffer -= frame_adjust_2;
439 }
440}
441
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900442static void amdtp_read_s32(struct amdtp_stream *s,
443 struct snd_pcm_substream *pcm,
444 __be32 *buffer, unsigned int frames)
445{
446 struct snd_pcm_runtime *runtime = pcm->runtime;
447 unsigned int channels, remaining_frames, i, c;
448 u32 *dst;
449
450 channels = s->pcm_channels;
451 dst = (void *)runtime->dma_area +
452 frames_to_bytes(runtime, s->pcm_buffer_pointer);
453 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
454
455 for (i = 0; i < frames; ++i) {
456 for (c = 0; c < channels; ++c) {
457 *dst = be32_to_cpu(buffer[c]) << 8;
458 dst++;
459 }
460 buffer += s->data_block_quadlets;
461 if (--remaining_frames == 0)
462 dst = (void *)runtime->dma_area;
463 }
464}
465
466static void amdtp_read_s32_dualwire(struct amdtp_stream *s,
467 struct snd_pcm_substream *pcm,
468 __be32 *buffer, unsigned int frames)
469{
470 struct snd_pcm_runtime *runtime = pcm->runtime;
471 unsigned int channels, remaining_frames, i, c;
472 u32 *dst;
473
474 dst = (void *)runtime->dma_area +
475 frames_to_bytes(runtime, s->pcm_buffer_pointer);
476 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
477 channels = s->pcm_channels / 2;
478
479 for (i = 0; i < frames; ++i) {
480 for (c = 0; c < channels; ++c) {
481 *dst = be32_to_cpu(buffer[c * 2]) << 8;
482 dst++;
483 }
484 buffer += 1;
485 for (c = 0; c < channels; ++c) {
486 *dst = be32_to_cpu(buffer[c * 2]) << 8;
487 dst++;
488 }
489 buffer += s->data_block_quadlets - 1;
490 if (--remaining_frames == 0)
491 dst = (void *)runtime->dma_area;
492 }
493}
494
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900495static void amdtp_fill_pcm_silence(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100496 __be32 *buffer, unsigned int frames)
497{
498 unsigned int i, c;
499
500 for (i = 0; i < frames; ++i) {
501 for (c = 0; c < s->pcm_channels; ++c)
502 buffer[c] = cpu_to_be32(0x40000000);
503 buffer += s->data_block_quadlets;
504 }
505}
506
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900507static void amdtp_fill_midi(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100508 __be32 *buffer, unsigned int frames)
509{
510 unsigned int i;
511
512 for (i = 0; i < frames; ++i)
513 buffer[s->pcm_channels + i * s->data_block_quadlets] =
514 cpu_to_be32(0x80000000);
515}
516
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900517static void update_pcm_pointers(struct amdtp_stream *s,
518 struct snd_pcm_substream *pcm,
519 unsigned int frames)
520{ unsigned int ptr;
521
522 if (s->dual_wire)
523 frames *= 2;
524
525 ptr = s->pcm_buffer_pointer + frames;
526 if (ptr >= pcm->runtime->buffer_size)
527 ptr -= pcm->runtime->buffer_size;
528 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
529
530 s->pcm_period_pointer += frames;
531 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
532 s->pcm_period_pointer -= pcm->runtime->period_size;
533 s->pointer_flush = false;
534 tasklet_hi_schedule(&s->period_tasklet);
535 }
536}
537
538static void pcm_period_tasklet(unsigned long data)
539{
540 struct amdtp_stream *s = (void *)data;
541 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
542
543 if (pcm)
544 snd_pcm_period_elapsed(pcm);
545}
546
547static int queue_packet(struct amdtp_stream *s,
548 unsigned int header_length,
549 unsigned int payload_length, bool skip)
550{
551 struct fw_iso_packet p = {0};
552 int err;
553
554 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
555 p.tag = TAG_CIP;
556 p.header_length = header_length;
557 p.payload_length = (!skip) ? payload_length : 0;
558 p.skip = skip;
559 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
560 s->buffer.packets[s->packet_index].offset);
561 if (err < 0) {
562 dev_err(&s->unit->device, "queueing error: %d\n", err);
563 goto end;
564 }
565
566 if (++s->packet_index >= QUEUE_LENGTH)
567 s->packet_index = 0;
568end:
569 return err;
570}
571
572static inline int queue_out_packet(struct amdtp_stream *s,
573 unsigned int payload_length, bool skip)
574{
575 return queue_packet(s, OUT_PACKET_HEADER_SIZE,
576 payload_length, skip);
577}
578
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900579static inline int queue_in_packet(struct amdtp_stream *s)
580{
581 return queue_packet(s, IN_PACKET_HEADER_SIZE,
582 amdtp_stream_get_max_payload(s), false);
583}
584
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900585static void handle_out_packet(struct amdtp_stream *s, unsigned int cycle)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100586{
587 __be32 *buffer;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900588 unsigned int index, data_blocks, syt, payload_length;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100589 struct snd_pcm_substream *pcm;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100590
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100591 if (s->packet_index < 0)
592 return;
593 index = s->packet_index;
594
Takashi Sakamoto82755ab2013-11-21 14:21:06 +0900595 /* this module generate empty packet for 'no data' */
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100596 syt = calculate_syt(s, cycle);
Takashi Sakamoto82755ab2013-11-21 14:21:06 +0900597 if (!(s->flags & CIP_BLOCKING))
Clemens Ladische84d15f2011-09-04 22:12:48 +0200598 data_blocks = calculate_data_blocks(s);
Takashi Sakamotob445db42014-04-25 22:44:43 +0900599 else if (syt != CIP_SYT_NO_INFO)
Takashi Sakamoto82755ab2013-11-21 14:21:06 +0900600 data_blocks = s->syt_interval;
601 else
602 data_blocks = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100603
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100604 buffer = s->buffer.packets[index].buffer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100605 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
Takashi Sakamotob445db42014-04-25 22:44:43 +0900606 (s->data_block_quadlets << AMDTP_DBS_SHIFT) |
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100607 s->data_block_counter);
608 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
Takashi Sakamotob445db42014-04-25 22:44:43 +0900609 (s->sfc << CIP_FDF_SFC_SHIFT) | syt);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100610 buffer += 2;
611
612 pcm = ACCESS_ONCE(s->pcm);
613 if (pcm)
614 s->transfer_samples(s, pcm, buffer, data_blocks);
615 else
616 amdtp_fill_pcm_silence(s, buffer, data_blocks);
617 if (s->midi_ports)
618 amdtp_fill_midi(s, buffer, data_blocks);
619
620 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
621
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900622 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
623 if (queue_out_packet(s, payload_length, false) < 0) {
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100624 s->packet_index = -1;
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900625 amdtp_stream_pcm_abort(s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100626 return;
627 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100628
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200629 if (pcm)
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900630 update_pcm_pointers(s, pcm, data_blocks);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200631}
632
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900633static void handle_in_packet(struct amdtp_stream *s,
634 unsigned int payload_quadlets,
635 __be32 *buffer)
636{
637 u32 cip_header[2];
638 unsigned int data_blocks, data_block_quadlets, data_block_counter;
639 struct snd_pcm_substream *pcm = NULL;
640
641 cip_header[0] = be32_to_cpu(buffer[0]);
642 cip_header[1] = be32_to_cpu(buffer[1]);
643
644 /*
645 * This module supports 'Two-quadlet CIP header with SYT field'.
646 * For convinience, also check FMT field is AM824 or not.
647 */
648 if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
649 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH) ||
650 ((cip_header[1] & CIP_FMT_MASK) != CIP_FMT_AM)) {
651 dev_info_ratelimited(&s->unit->device,
652 "Invalid CIP header for AMDTP: %08X:%08X\n",
653 cip_header[0], cip_header[1]);
654 goto end;
655 }
656
657 /* Calculate data blocks */
658 if (payload_quadlets < 3 ||
659 ((cip_header[1] & CIP_FDF_MASK) ==
660 (AMDTP_FDF_NO_DATA << CIP_FDF_SFC_SHIFT))) {
661 data_blocks = 0;
662 } else {
663 data_block_quadlets =
664 (cip_header[0] & AMDTP_DBS_MASK) >> AMDTP_DBS_SHIFT;
665 /* avoid division by zero */
666 if (data_block_quadlets == 0) {
667 dev_info_ratelimited(&s->unit->device,
668 "Detect invalid value in dbs field: %08X\n",
669 cip_header[0]);
670 goto err;
671 }
672
673 data_blocks = (payload_quadlets - 2) / data_block_quadlets;
674 }
675
676 /* Check data block counter continuity */
677 data_block_counter = cip_header[0] & AMDTP_DBC_MASK;
678 if (data_block_counter != s->data_block_counter) {
679 dev_info(&s->unit->device,
680 "Detect discontinuity of CIP: %02X %02X\n",
681 s->data_block_counter, data_block_counter);
682 goto err;
683 }
684
685 if (data_blocks > 0) {
686 buffer += 2;
687
688 pcm = ACCESS_ONCE(s->pcm);
689 if (pcm)
690 s->transfer_samples(s, pcm, buffer, data_blocks);
691 }
692
693 s->data_block_counter = (data_block_counter + data_blocks) & 0xff;
694end:
695 if (queue_in_packet(s) < 0)
696 goto err;
697
698 if (pcm)
699 update_pcm_pointers(s, pcm, data_blocks);
700
701 return;
702err:
703 s->packet_index = -1;
704 amdtp_stream_pcm_abort(s);
705}
706
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900707static void out_stream_callback(struct fw_iso_context *context, u32 cycle,
708 size_t header_length, void *header,
709 void *private_data)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100710{
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900711 struct amdtp_stream *s = private_data;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100712 unsigned int i, packets = header_length / 4;
713
714 /*
715 * Compute the cycle of the last queued packet.
716 * (We need only the four lowest bits for the SYT, so we can ignore
717 * that bits 0-11 must wrap around at 3072.)
718 */
719 cycle += QUEUE_LENGTH - packets;
720
721 for (i = 0; i < packets; ++i)
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900722 handle_out_packet(s, ++cycle);
Clemens Ladisch13882a82011-05-02 09:33:56 +0200723 fw_iso_context_queue_flush(s->context);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100724}
725
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900726static void in_stream_callback(struct fw_iso_context *context, u32 cycle,
727 size_t header_length, void *header,
728 void *private_data)
729{
730 struct amdtp_stream *s = private_data;
731 unsigned int p, packets, payload_quadlets;
732 __be32 *buffer, *headers = header;
733
734 /* The number of packets in buffer */
735 packets = header_length / IN_PACKET_HEADER_SIZE;
736
737 for (p = 0; p < packets; p++) {
738 if (s->packet_index < 0)
739 return;
740 buffer = s->buffer.packets[s->packet_index].buffer;
741
742 /* The number of quadlets in this packet */
743 payload_quadlets =
744 (be32_to_cpu(headers[p]) >> ISO_DATA_LENGTH_SHIFT) / 4;
745 handle_in_packet(s, payload_quadlets, buffer);
746 }
747
748 fw_iso_context_queue_flush(s->context);
749}
750
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100751/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900752 * amdtp_stream_start - start transferring packets
753 * @s: the AMDTP stream to start
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100754 * @channel: the isochronous channel on the bus
755 * @speed: firewire speed code
756 *
757 * The stream cannot be started until it has been configured with
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900758 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
759 * device can be started.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100760 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900761int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100762{
763 static const struct {
764 unsigned int data_block;
765 unsigned int syt_offset;
766 } initial_state[] = {
767 [CIP_SFC_32000] = { 4, 3072 },
768 [CIP_SFC_48000] = { 6, 1024 },
769 [CIP_SFC_96000] = { 12, 1024 },
770 [CIP_SFC_192000] = { 24, 1024 },
771 [CIP_SFC_44100] = { 0, 67 },
772 [CIP_SFC_88200] = { 0, 67 },
773 [CIP_SFC_176400] = { 0, 67 },
774 };
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900775 unsigned int header_size;
776 enum dma_data_direction dir;
777 fw_iso_callback_t cb;
778 int type, err;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100779
780 mutex_lock(&s->mutex);
781
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900782 if (WARN_ON(amdtp_stream_running(s) ||
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900783 (s->data_block_quadlets < 1))) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100784 err = -EBADFD;
785 goto err_unlock;
786 }
787
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900788 s->data_block_counter = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100789 s->data_block_state = initial_state[s->sfc].data_block;
790 s->syt_offset_state = initial_state[s->sfc].syt_offset;
791 s->last_syt_offset = TICKS_PER_CYCLE;
792
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900793 /* initialize packet buffer */
794 if (s->direction == AMDTP_IN_STREAM) {
795 dir = DMA_FROM_DEVICE;
796 type = FW_ISO_CONTEXT_RECEIVE;
797 header_size = IN_PACKET_HEADER_SIZE;
798 cb = in_stream_callback;
799 } else {
800 dir = DMA_TO_DEVICE;
801 type = FW_ISO_CONTEXT_TRANSMIT;
802 header_size = OUT_PACKET_HEADER_SIZE;
803 cb = out_stream_callback;
804 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100805 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900806 amdtp_stream_get_max_payload(s), dir);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100807 if (err < 0)
808 goto err_unlock;
809
810 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900811 type, channel, speed, header_size,
812 cb, s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100813 if (IS_ERR(s->context)) {
814 err = PTR_ERR(s->context);
815 if (err == -EBUSY)
816 dev_err(&s->unit->device,
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900817 "no free stream on this controller\n");
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100818 goto err_buffer;
819 }
820
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900821 amdtp_stream_update(s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100822
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100823 s->packet_index = 0;
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900824 do {
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900825 if (s->direction == AMDTP_IN_STREAM)
826 err = queue_in_packet(s);
827 else
828 err = queue_out_packet(s, 0, true);
Takashi Sakamoto4b7da112014-04-25 22:44:45 +0900829 if (err < 0)
830 goto err_context;
831 } while (s->packet_index > 0);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100832
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +0900833 /* NOTE: TAG1 matches CIP. This just affects in stream. */
834 err = fw_iso_context_start(s->context, -1, 0,
835 FW_ISO_CONTEXT_MATCH_TAG1);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100836 if (err < 0)
837 goto err_context;
838
839 mutex_unlock(&s->mutex);
840
841 return 0;
842
843err_context:
844 fw_iso_context_destroy(s->context);
845 s->context = ERR_PTR(-1);
846err_buffer:
847 iso_packets_buffer_destroy(&s->buffer, s->unit);
848err_unlock:
849 mutex_unlock(&s->mutex);
850
851 return err;
852}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900853EXPORT_SYMBOL(amdtp_stream_start);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100854
855/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900856 * amdtp_stream_pcm_pointer - get the PCM buffer position
857 * @s: the AMDTP stream that transports the PCM data
Clemens Ladische9148dd2012-05-13 18:49:14 +0200858 *
859 * Returns the current buffer position, in frames.
860 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900861unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
Clemens Ladische9148dd2012-05-13 18:49:14 +0200862{
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200863 /* this optimization is allowed to be racy */
864 if (s->pointer_flush)
865 fw_iso_context_flush_completions(s->context);
866 else
867 s->pointer_flush = true;
Clemens Ladische9148dd2012-05-13 18:49:14 +0200868
869 return ACCESS_ONCE(s->pcm_buffer_pointer);
870}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900871EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
Clemens Ladische9148dd2012-05-13 18:49:14 +0200872
873/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900874 * amdtp_stream_update - update the stream after a bus reset
875 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100876 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900877void amdtp_stream_update(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100878{
879 ACCESS_ONCE(s->source_node_id_field) =
880 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
881}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900882EXPORT_SYMBOL(amdtp_stream_update);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100883
884/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900885 * amdtp_stream_stop - stop sending packets
886 * @s: the AMDTP stream to stop
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100887 *
888 * All PCM and MIDI devices of the stream must be stopped before the stream
889 * itself can be stopped.
890 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900891void amdtp_stream_stop(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100892{
893 mutex_lock(&s->mutex);
894
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900895 if (!amdtp_stream_running(s)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100896 mutex_unlock(&s->mutex);
897 return;
898 }
899
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200900 tasklet_kill(&s->period_tasklet);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100901 fw_iso_context_stop(s->context);
902 fw_iso_context_destroy(s->context);
903 s->context = ERR_PTR(-1);
904 iso_packets_buffer_destroy(&s->buffer, s->unit);
905
906 mutex_unlock(&s->mutex);
907}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900908EXPORT_SYMBOL(amdtp_stream_stop);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100909
910/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900911 * amdtp_stream_pcm_abort - abort the running PCM device
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100912 * @s: the AMDTP stream about to be stopped
913 *
914 * If the isochronous stream needs to be stopped asynchronously, call this
915 * function first to stop the PCM device.
916 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900917void amdtp_stream_pcm_abort(struct amdtp_stream *s)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100918{
919 struct snd_pcm_substream *pcm;
920
921 pcm = ACCESS_ONCE(s->pcm);
922 if (pcm) {
923 snd_pcm_stream_lock_irq(pcm);
924 if (snd_pcm_running(pcm))
925 snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
926 snd_pcm_stream_unlock_irq(pcm);
927 }
928}
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900929EXPORT_SYMBOL(amdtp_stream_pcm_abort);