aboutsummaryrefslogtreecommitdiffstats
path: root/sound/firewire/amdtp.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/firewire/amdtp.c')
-rw-r--r--sound/firewire/amdtp.c797
1 files changed, 548 insertions, 249 deletions
diff --git a/sound/firewire/amdtp.c b/sound/firewire/amdtp.c
index 9048777228e2..31dd1cfc79b3 100644
--- a/sound/firewire/amdtp.c
+++ b/sound/firewire/amdtp.c
@@ -11,7 +11,10 @@
11#include <linux/firewire.h> 11#include <linux/firewire.h>
12#include <linux/module.h> 12#include <linux/module.h>
13#include <linux/slab.h> 13#include <linux/slab.h>
14#include <linux/sched.h>
14#include <sound/pcm.h> 15#include <sound/pcm.h>
16#include <sound/pcm_params.h>
17#include <sound/rawmidi.h>
15#include "amdtp.h" 18#include "amdtp.h"
16 19
17#define TICKS_PER_CYCLE 3072 20#define TICKS_PER_CYCLE 3072
@@ -20,50 +23,78 @@
20 23
21#define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 µs */ 24#define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 µs */
22 25
26/* isochronous header parameters */
27#define ISO_DATA_LENGTH_SHIFT 16
23#define TAG_CIP 1 28#define TAG_CIP 1
24 29
30/* common isochronous packet header parameters */
25#define CIP_EOH (1u << 31) 31#define CIP_EOH (1u << 31)
32#define CIP_EOH_MASK 0x80000000
26#define CIP_FMT_AM (0x10 << 24) 33#define CIP_FMT_AM (0x10 << 24)
27#define AMDTP_FDF_AM824 (0 << 19) 34#define CIP_FMT_MASK 0x3f000000
28#define AMDTP_FDF_SFC_SHIFT 16 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))
45#define AMDTP_FDF_NO_DATA 0xff
46#define AMDTP_DBS_MASK 0x00ff0000
47#define AMDTP_DBS_SHIFT 16
48#define AMDTP_DBC_MASK 0x000000ff
29 49
30/* TODO: make these configurable */ 50/* TODO: make these configurable */
31#define INTERRUPT_INTERVAL 16 51#define INTERRUPT_INTERVAL 16
32#define QUEUE_LENGTH 48 52#define QUEUE_LENGTH 48
33 53
54#define IN_PACKET_HEADER_SIZE 4
55#define OUT_PACKET_HEADER_SIZE 0
56
34static void pcm_period_tasklet(unsigned long data); 57static void pcm_period_tasklet(unsigned long data);
35 58
36/** 59/**
37 * amdtp_out_stream_init - initialize an AMDTP output stream structure 60 * amdtp_stream_init - initialize an AMDTP stream structure
38 * @s: the AMDTP output stream to initialize 61 * @s: the AMDTP stream to initialize
39 * @unit: the target of the stream 62 * @unit: the target of the stream
63 * @dir: the direction of stream
40 * @flags: the packet transmission method to use 64 * @flags: the packet transmission method to use
41 */ 65 */
42int amdtp_out_stream_init(struct amdtp_out_stream *s, struct fw_unit *unit, 66int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
43 enum cip_out_flags flags) 67 enum amdtp_stream_direction dir, enum cip_flags flags)
44{ 68{
45 s->unit = fw_unit_get(unit); 69 s->unit = fw_unit_get(unit);
70 s->direction = dir;
46 s->flags = flags; 71 s->flags = flags;
47 s->context = ERR_PTR(-1); 72 s->context = ERR_PTR(-1);
48 mutex_init(&s->mutex); 73 mutex_init(&s->mutex);
49 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s); 74 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
50 s->packet_index = 0; 75 s->packet_index = 0;
51 76
77 init_waitqueue_head(&s->callback_wait);
78 s->callbacked = false;
79 s->sync_slave = NULL;
80
81 s->rx_blocks_for_midi = UINT_MAX;
82
52 return 0; 83 return 0;
53} 84}
54EXPORT_SYMBOL(amdtp_out_stream_init); 85EXPORT_SYMBOL(amdtp_stream_init);
55 86
56/** 87/**
57 * amdtp_out_stream_destroy - free stream resources 88 * amdtp_stream_destroy - free stream resources
58 * @s: the AMDTP output stream to destroy 89 * @s: the AMDTP stream to destroy
59 */ 90 */
60void amdtp_out_stream_destroy(struct amdtp_out_stream *s) 91void amdtp_stream_destroy(struct amdtp_stream *s)
61{ 92{
62 WARN_ON(amdtp_out_stream_running(s)); 93 WARN_ON(amdtp_stream_running(s));
63 mutex_destroy(&s->mutex); 94 mutex_destroy(&s->mutex);
64 fw_unit_put(s->unit); 95 fw_unit_put(s->unit);
65} 96}
66EXPORT_SYMBOL(amdtp_out_stream_destroy); 97EXPORT_SYMBOL(amdtp_stream_destroy);
67 98
68const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = { 99const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
69 [CIP_SFC_32000] = 8, 100 [CIP_SFC_32000] = 8,
@@ -76,9 +107,75 @@ const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
76}; 107};
77EXPORT_SYMBOL(amdtp_syt_intervals); 108EXPORT_SYMBOL(amdtp_syt_intervals);
78 109
110const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
111 [CIP_SFC_32000] = 32000,
112 [CIP_SFC_44100] = 44100,
113 [CIP_SFC_48000] = 48000,
114 [CIP_SFC_88200] = 88200,
115 [CIP_SFC_96000] = 96000,
116 [CIP_SFC_176400] = 176400,
117 [CIP_SFC_192000] = 192000,
118};
119EXPORT_SYMBOL(amdtp_rate_table);
120
121/**
122 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
123 * @s: the AMDTP stream, which must be initialized.
124 * @runtime: the PCM substream runtime
125 */
126int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
127 struct snd_pcm_runtime *runtime)
128{
129 int err;
130
131 /* AM824 in IEC 61883-6 can deliver 24bit data */
132 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
133 if (err < 0)
134 goto end;
135
136 /*
137 * Currently firewire-lib processes 16 packets in one software
138 * interrupt callback. This equals to 2msec but actually the
139 * interval of the interrupts has a jitter.
140 * Additionally, even if adding a constraint to fit period size to
141 * 2msec, actual calculated frames per period doesn't equal to 2msec,
142 * depending on sampling rate.
143 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
144 * Here let us use 5msec for safe period interrupt.
145 */
146 err = snd_pcm_hw_constraint_minmax(runtime,
147 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
148 5000, UINT_MAX);
149 if (err < 0)
150 goto end;
151
152 /* Non-Blocking stream has no more constraints */
153 if (!(s->flags & CIP_BLOCKING))
154 goto end;
155
156 /*
157 * One AMDTP packet can include some frames. In blocking mode, the
158 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
159 * depending on its sampling rate. For accurate period interrupt, it's
160 * preferrable to aligh period/buffer sizes to current SYT_INTERVAL.
161 *
162 * TODO: These constraints can be improved with propper rules.
163 * Currently apply LCM of SYT_INTEVALs.
164 */
165 err = snd_pcm_hw_constraint_step(runtime, 0,
166 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
167 if (err < 0)
168 goto end;
169 err = snd_pcm_hw_constraint_step(runtime, 0,
170 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
171end:
172 return err;
173}
174EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
175
79/** 176/**
80 * amdtp_out_stream_set_parameters - set stream parameters 177 * amdtp_stream_set_parameters - set stream parameters
81 * @s: the AMDTP output stream to configure 178 * @s: the AMDTP stream to configure
82 * @rate: the sample rate 179 * @rate: the sample rate
83 * @pcm_channels: the number of PCM samples in each data block, to be encoded 180 * @pcm_channels: the number of PCM samples in each data block, to be encoded
84 * as AM824 multi-bit linear audio 181 * as AM824 multi-bit linear audio
@@ -87,41 +184,30 @@ EXPORT_SYMBOL(amdtp_syt_intervals);
87 * The parameters must be set before the stream is started, and must not be 184 * The parameters must be set before the stream is started, and must not be
88 * changed while the stream is running. 185 * changed while the stream is running.
89 */ 186 */
90void amdtp_out_stream_set_parameters(struct amdtp_out_stream *s, 187void amdtp_stream_set_parameters(struct amdtp_stream *s,
91 unsigned int rate, 188 unsigned int rate,
92 unsigned int pcm_channels, 189 unsigned int pcm_channels,
93 unsigned int midi_ports) 190 unsigned int midi_ports)
94{ 191{
95 static const unsigned int rates[] = { 192 unsigned int i, sfc, midi_channels;
96 [CIP_SFC_32000] = 32000, 193
97 [CIP_SFC_44100] = 44100, 194 midi_channels = DIV_ROUND_UP(midi_ports, 8);
98 [CIP_SFC_48000] = 48000,
99 [CIP_SFC_88200] = 88200,
100 [CIP_SFC_96000] = 96000,
101 [CIP_SFC_176400] = 176400,
102 [CIP_SFC_192000] = 192000,
103 };
104 unsigned int sfc;
105 195
106 if (WARN_ON(amdtp_out_stream_running(s))) 196 if (WARN_ON(amdtp_stream_running(s)) |
197 WARN_ON(pcm_channels > AMDTP_MAX_CHANNELS_FOR_PCM) |
198 WARN_ON(midi_channels > AMDTP_MAX_CHANNELS_FOR_MIDI))
107 return; 199 return;
108 200
109 for (sfc = 0; sfc < CIP_SFC_COUNT; ++sfc) 201 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc)
110 if (rates[sfc] == rate) 202 if (amdtp_rate_table[sfc] == rate)
111 goto sfc_found; 203 goto sfc_found;
112 WARN_ON(1); 204 WARN_ON(1);
113 return; 205 return;
114 206
115sfc_found: 207sfc_found:
116 s->dual_wire = (s->flags & CIP_HI_DUALWIRE) && sfc > CIP_SFC_96000;
117 if (s->dual_wire) {
118 sfc -= 2;
119 rate /= 2;
120 pcm_channels *= 2;
121 }
122 s->sfc = sfc;
123 s->data_block_quadlets = pcm_channels + DIV_ROUND_UP(midi_ports, 8);
124 s->pcm_channels = pcm_channels; 208 s->pcm_channels = pcm_channels;
209 s->sfc = sfc;
210 s->data_block_quadlets = s->pcm_channels + midi_channels;
125 s->midi_ports = midi_ports; 211 s->midi_ports = midi_ports;
126 212
127 s->syt_interval = amdtp_syt_intervals[sfc]; 213 s->syt_interval = amdtp_syt_intervals[sfc];
@@ -131,48 +217,50 @@ sfc_found:
131 if (s->flags & CIP_BLOCKING) 217 if (s->flags & CIP_BLOCKING)
132 /* additional buffering needed to adjust for no-data packets */ 218 /* additional buffering needed to adjust for no-data packets */
133 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate; 219 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
220
221 /* init the position map for PCM and MIDI channels */
222 for (i = 0; i < pcm_channels; i++)
223 s->pcm_positions[i] = i;
224 s->midi_position = s->pcm_channels;
134} 225}
135EXPORT_SYMBOL(amdtp_out_stream_set_parameters); 226EXPORT_SYMBOL(amdtp_stream_set_parameters);
136 227
137/** 228/**
138 * amdtp_out_stream_get_max_payload - get the stream's packet size 229 * amdtp_stream_get_max_payload - get the stream's packet size
139 * @s: the AMDTP output stream 230 * @s: the AMDTP stream
140 * 231 *
141 * This function must not be called before the stream has been configured 232 * This function must not be called before the stream has been configured
142 * with amdtp_out_stream_set_parameters(). 233 * with amdtp_stream_set_parameters().
143 */ 234 */
144unsigned int amdtp_out_stream_get_max_payload(struct amdtp_out_stream *s) 235unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
145{ 236{
146 return 8 + s->syt_interval * s->data_block_quadlets * 4; 237 return 8 + s->syt_interval * s->data_block_quadlets * 4;
147} 238}
148EXPORT_SYMBOL(amdtp_out_stream_get_max_payload); 239EXPORT_SYMBOL(amdtp_stream_get_max_payload);
149 240
150static void amdtp_write_s16(struct amdtp_out_stream *s, 241static void amdtp_write_s16(struct amdtp_stream *s,
151 struct snd_pcm_substream *pcm, 242 struct snd_pcm_substream *pcm,
152 __be32 *buffer, unsigned int frames); 243 __be32 *buffer, unsigned int frames);
153static void amdtp_write_s32(struct amdtp_out_stream *s, 244static void amdtp_write_s32(struct amdtp_stream *s,
154 struct snd_pcm_substream *pcm, 245 struct snd_pcm_substream *pcm,
155 __be32 *buffer, unsigned int frames); 246 __be32 *buffer, unsigned int frames);
156static void amdtp_write_s16_dualwire(struct amdtp_out_stream *s, 247static void amdtp_read_s32(struct amdtp_stream *s,
157 struct snd_pcm_substream *pcm, 248 struct snd_pcm_substream *pcm,
158 __be32 *buffer, unsigned int frames); 249 __be32 *buffer, unsigned int frames);
159static void amdtp_write_s32_dualwire(struct amdtp_out_stream *s,
160 struct snd_pcm_substream *pcm,
161 __be32 *buffer, unsigned int frames);
162 250
163/** 251/**
164 * amdtp_out_stream_set_pcm_format - set the PCM format 252 * amdtp_stream_set_pcm_format - set the PCM format
165 * @s: the AMDTP output stream to configure 253 * @s: the AMDTP stream to configure
166 * @format: the format of the ALSA PCM device 254 * @format: the format of the ALSA PCM device
167 * 255 *
168 * The sample format must be set after the other paramters (rate/PCM channels/ 256 * The sample format must be set after the other paramters (rate/PCM channels/
169 * MIDI) and before the stream is started, and must not be changed while the 257 * MIDI) and before the stream is started, and must not be changed while the
170 * stream is running. 258 * stream is running.
171 */ 259 */
172void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s, 260void amdtp_stream_set_pcm_format(struct amdtp_stream *s,
173 snd_pcm_format_t format) 261 snd_pcm_format_t format)
174{ 262{
175 if (WARN_ON(amdtp_out_stream_running(s))) 263 if (WARN_ON(amdtp_stream_pcm_running(s)))
176 return; 264 return;
177 265
178 switch (format) { 266 switch (format) {
@@ -180,41 +268,44 @@ void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s,
180 WARN_ON(1); 268 WARN_ON(1);
181 /* fall through */ 269 /* fall through */
182 case SNDRV_PCM_FORMAT_S16: 270 case SNDRV_PCM_FORMAT_S16:
183 if (s->dual_wire) 271 if (s->direction == AMDTP_OUT_STREAM) {
184 s->transfer_samples = amdtp_write_s16_dualwire;
185 else
186 s->transfer_samples = amdtp_write_s16; 272 s->transfer_samples = amdtp_write_s16;
187 break; 273 break;
274 }
275 WARN_ON(1);
276 /* fall through */
188 case SNDRV_PCM_FORMAT_S32: 277 case SNDRV_PCM_FORMAT_S32:
189 if (s->dual_wire) 278 if (s->direction == AMDTP_OUT_STREAM)
190 s->transfer_samples = amdtp_write_s32_dualwire;
191 else
192 s->transfer_samples = amdtp_write_s32; 279 s->transfer_samples = amdtp_write_s32;
280 else
281 s->transfer_samples = amdtp_read_s32;
193 break; 282 break;
194 } 283 }
195} 284}
196EXPORT_SYMBOL(amdtp_out_stream_set_pcm_format); 285EXPORT_SYMBOL(amdtp_stream_set_pcm_format);
197 286
198/** 287/**
199 * amdtp_out_stream_pcm_prepare - prepare PCM device for running 288 * amdtp_stream_pcm_prepare - prepare PCM device for running
200 * @s: the AMDTP output stream 289 * @s: the AMDTP stream
201 * 290 *
202 * This function should be called from the PCM device's .prepare callback. 291 * This function should be called from the PCM device's .prepare callback.
203 */ 292 */
204void amdtp_out_stream_pcm_prepare(struct amdtp_out_stream *s) 293void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
205{ 294{
206 tasklet_kill(&s->period_tasklet); 295 tasklet_kill(&s->period_tasklet);
207 s->pcm_buffer_pointer = 0; 296 s->pcm_buffer_pointer = 0;
208 s->pcm_period_pointer = 0; 297 s->pcm_period_pointer = 0;
209 s->pointer_flush = true; 298 s->pointer_flush = true;
210} 299}
211EXPORT_SYMBOL(amdtp_out_stream_pcm_prepare); 300EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
212 301
213static unsigned int calculate_data_blocks(struct amdtp_out_stream *s) 302static unsigned int calculate_data_blocks(struct amdtp_stream *s)
214{ 303{
215 unsigned int phase, data_blocks; 304 unsigned int phase, data_blocks;
216 305
217 if (!cip_sfc_is_base_44100(s->sfc)) { 306 if (s->flags & CIP_BLOCKING)
307 data_blocks = s->syt_interval;
308 else if (!cip_sfc_is_base_44100(s->sfc)) {
218 /* Sample_rate / 8000 is an integer, and precomputed. */ 309 /* Sample_rate / 8000 is an integer, and precomputed. */
219 data_blocks = s->data_block_state; 310 data_blocks = s->data_block_state;
220 } else { 311 } else {
@@ -243,7 +334,7 @@ static unsigned int calculate_data_blocks(struct amdtp_out_stream *s)
243 return data_blocks; 334 return data_blocks;
244} 335}
245 336
246static unsigned int calculate_syt(struct amdtp_out_stream *s, 337static unsigned int calculate_syt(struct amdtp_stream *s,
247 unsigned int cycle) 338 unsigned int cycle)
248{ 339{
249 unsigned int syt_offset, phase, index, syt; 340 unsigned int syt_offset, phase, index, syt;
@@ -280,175 +371,228 @@ static unsigned int calculate_syt(struct amdtp_out_stream *s,
280 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12; 371 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
281 syt += syt_offset % TICKS_PER_CYCLE; 372 syt += syt_offset % TICKS_PER_CYCLE;
282 373
283 return syt & 0xffff; 374 return syt & CIP_SYT_MASK;
284 } else { 375 } else {
285 return 0xffff; /* no info */ 376 return CIP_SYT_NO_INFO;
286 } 377 }
287} 378}
288 379
289static void amdtp_write_s32(struct amdtp_out_stream *s, 380static void amdtp_write_s32(struct amdtp_stream *s,
290 struct snd_pcm_substream *pcm, 381 struct snd_pcm_substream *pcm,
291 __be32 *buffer, unsigned int frames) 382 __be32 *buffer, unsigned int frames)
292{ 383{
293 struct snd_pcm_runtime *runtime = pcm->runtime; 384 struct snd_pcm_runtime *runtime = pcm->runtime;
294 unsigned int channels, remaining_frames, frame_step, i, c; 385 unsigned int channels, remaining_frames, i, c;
295 const u32 *src; 386 const u32 *src;
296 387
297 channels = s->pcm_channels; 388 channels = s->pcm_channels;
298 src = (void *)runtime->dma_area + 389 src = (void *)runtime->dma_area +
299 frames_to_bytes(runtime, s->pcm_buffer_pointer); 390 frames_to_bytes(runtime, s->pcm_buffer_pointer);
300 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; 391 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
301 frame_step = s->data_block_quadlets - channels;
302 392
303 for (i = 0; i < frames; ++i) { 393 for (i = 0; i < frames; ++i) {
304 for (c = 0; c < channels; ++c) { 394 for (c = 0; c < channels; ++c) {
305 *buffer = cpu_to_be32((*src >> 8) | 0x40000000); 395 buffer[s->pcm_positions[c]] =
396 cpu_to_be32((*src >> 8) | 0x40000000);
306 src++; 397 src++;
307 buffer++;
308 } 398 }
309 buffer += frame_step; 399 buffer += s->data_block_quadlets;
310 if (--remaining_frames == 0) 400 if (--remaining_frames == 0)
311 src = (void *)runtime->dma_area; 401 src = (void *)runtime->dma_area;
312 } 402 }
313} 403}
314 404
315static void amdtp_write_s16(struct amdtp_out_stream *s, 405static void amdtp_write_s16(struct amdtp_stream *s,
316 struct snd_pcm_substream *pcm, 406 struct snd_pcm_substream *pcm,
317 __be32 *buffer, unsigned int frames) 407 __be32 *buffer, unsigned int frames)
318{ 408{
319 struct snd_pcm_runtime *runtime = pcm->runtime; 409 struct snd_pcm_runtime *runtime = pcm->runtime;
320 unsigned int channels, remaining_frames, frame_step, i, c; 410 unsigned int channels, remaining_frames, i, c;
321 const u16 *src; 411 const u16 *src;
322 412
323 channels = s->pcm_channels; 413 channels = s->pcm_channels;
324 src = (void *)runtime->dma_area + 414 src = (void *)runtime->dma_area +
325 frames_to_bytes(runtime, s->pcm_buffer_pointer); 415 frames_to_bytes(runtime, s->pcm_buffer_pointer);
326 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer; 416 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
327 frame_step = s->data_block_quadlets - channels;
328 417
329 for (i = 0; i < frames; ++i) { 418 for (i = 0; i < frames; ++i) {
330 for (c = 0; c < channels; ++c) { 419 for (c = 0; c < channels; ++c) {
331 *buffer = cpu_to_be32((*src << 8) | 0x40000000); 420 buffer[s->pcm_positions[c]] =
421 cpu_to_be32((*src << 8) | 0x40000000);
332 src++; 422 src++;
333 buffer++;
334 } 423 }
335 buffer += frame_step; 424 buffer += s->data_block_quadlets;
336 if (--remaining_frames == 0) 425 if (--remaining_frames == 0)
337 src = (void *)runtime->dma_area; 426 src = (void *)runtime->dma_area;
338 } 427 }
339} 428}
340 429
341static void amdtp_write_s32_dualwire(struct amdtp_out_stream *s, 430static void amdtp_read_s32(struct amdtp_stream *s,
342 struct snd_pcm_substream *pcm, 431 struct snd_pcm_substream *pcm,
343 __be32 *buffer, unsigned int frames) 432 __be32 *buffer, unsigned int frames)
344{ 433{
345 struct snd_pcm_runtime *runtime = pcm->runtime; 434 struct snd_pcm_runtime *runtime = pcm->runtime;
346 unsigned int channels, frame_adjust_1, frame_adjust_2, i, c; 435 unsigned int channels, remaining_frames, i, c;
347 const u32 *src; 436 u32 *dst;
348 437
349 channels = s->pcm_channels; 438 channels = s->pcm_channels;
350 src = (void *)runtime->dma_area + 439 dst = (void *)runtime->dma_area +
351 s->pcm_buffer_pointer * (runtime->frame_bits / 8); 440 frames_to_bytes(runtime, s->pcm_buffer_pointer);
352 frame_adjust_1 = channels - 1; 441 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
353 frame_adjust_2 = 1 - (s->data_block_quadlets - channels);
354 442
355 channels /= 2;
356 for (i = 0; i < frames; ++i) { 443 for (i = 0; i < frames; ++i) {
357 for (c = 0; c < channels; ++c) { 444 for (c = 0; c < channels; ++c) {
358 *buffer = cpu_to_be32((*src >> 8) | 0x40000000); 445 *dst = be32_to_cpu(buffer[s->pcm_positions[c]]) << 8;
359 src++; 446 dst++;
360 buffer += 2;
361 }
362 buffer -= frame_adjust_1;
363 for (c = 0; c < channels; ++c) {
364 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
365 src++;
366 buffer += 2;
367 } 447 }
368 buffer -= frame_adjust_2; 448 buffer += s->data_block_quadlets;
449 if (--remaining_frames == 0)
450 dst = (void *)runtime->dma_area;
369 } 451 }
370} 452}
371 453
372static void amdtp_write_s16_dualwire(struct amdtp_out_stream *s, 454static void amdtp_fill_pcm_silence(struct amdtp_stream *s,
373 struct snd_pcm_substream *pcm, 455 __be32 *buffer, unsigned int frames)
374 __be32 *buffer, unsigned int frames)
375{ 456{
376 struct snd_pcm_runtime *runtime = pcm->runtime; 457 unsigned int i, c;
377 unsigned int channels, frame_adjust_1, frame_adjust_2, i, c;
378 const u16 *src;
379
380 channels = s->pcm_channels;
381 src = (void *)runtime->dma_area +
382 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
383 frame_adjust_1 = channels - 1;
384 frame_adjust_2 = 1 - (s->data_block_quadlets - channels);
385 458
386 channels /= 2;
387 for (i = 0; i < frames; ++i) { 459 for (i = 0; i < frames; ++i) {
388 for (c = 0; c < channels; ++c) { 460 for (c = 0; c < s->pcm_channels; ++c)
389 *buffer = cpu_to_be32((*src << 8) | 0x40000000); 461 buffer[s->pcm_positions[c]] = cpu_to_be32(0x40000000);
390 src++; 462 buffer += s->data_block_quadlets;
391 buffer += 2;
392 }
393 buffer -= frame_adjust_1;
394 for (c = 0; c < channels; ++c) {
395 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
396 src++;
397 buffer += 2;
398 }
399 buffer -= frame_adjust_2;
400 } 463 }
401} 464}
402 465
403static void amdtp_fill_pcm_silence(struct amdtp_out_stream *s, 466static void amdtp_fill_midi(struct amdtp_stream *s,
404 __be32 *buffer, unsigned int frames) 467 __be32 *buffer, unsigned int frames)
405{ 468{
406 unsigned int i, c; 469 unsigned int f, port;
470 u8 *b;
471
472 for (f = 0; f < frames; f++) {
473 buffer[s->midi_position] = 0;
474 b = (u8 *)&buffer[s->midi_position];
475
476 port = (s->data_block_counter + f) % 8;
477 if ((f >= s->rx_blocks_for_midi) ||
478 (s->midi[port] == NULL) ||
479 (snd_rawmidi_transmit(s->midi[port], b + 1, 1) <= 0))
480 b[0] = 0x80;
481 else
482 b[0] = 0x81;
407 483
408 for (i = 0; i < frames; ++i) {
409 for (c = 0; c < s->pcm_channels; ++c)
410 buffer[c] = cpu_to_be32(0x40000000);
411 buffer += s->data_block_quadlets; 484 buffer += s->data_block_quadlets;
412 } 485 }
413} 486}
414 487
415static void amdtp_fill_midi(struct amdtp_out_stream *s, 488static void amdtp_pull_midi(struct amdtp_stream *s,
416 __be32 *buffer, unsigned int frames) 489 __be32 *buffer, unsigned int frames)
417{ 490{
418 unsigned int i; 491 unsigned int f, port;
492 int len;
493 u8 *b;
494
495 for (f = 0; f < frames; f++) {
496 port = (s->data_block_counter + f) % 8;
497 b = (u8 *)&buffer[s->midi_position];
498
499 len = b[0] - 0x80;
500 if ((1 <= len) && (len <= 3) && (s->midi[port]))
501 snd_rawmidi_receive(s->midi[port], b + 1, len);
502
503 buffer += s->data_block_quadlets;
504 }
505}
506
507static void update_pcm_pointers(struct amdtp_stream *s,
508 struct snd_pcm_substream *pcm,
509 unsigned int frames)
510{ unsigned int ptr;
511
512 ptr = s->pcm_buffer_pointer + frames;
513 if (ptr >= pcm->runtime->buffer_size)
514 ptr -= pcm->runtime->buffer_size;
515 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
516
517 s->pcm_period_pointer += frames;
518 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
519 s->pcm_period_pointer -= pcm->runtime->period_size;
520 s->pointer_flush = false;
521 tasklet_hi_schedule(&s->period_tasklet);
522 }
523}
524
525static void pcm_period_tasklet(unsigned long data)
526{
527 struct amdtp_stream *s = (void *)data;
528 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
419 529
420 for (i = 0; i < frames; ++i) 530 if (pcm)
421 buffer[s->pcm_channels + i * s->data_block_quadlets] = 531 snd_pcm_period_elapsed(pcm);
422 cpu_to_be32(0x80000000);
423} 532}
424 533
425static void queue_out_packet(struct amdtp_out_stream *s, unsigned int cycle) 534static int queue_packet(struct amdtp_stream *s,
535 unsigned int header_length,
536 unsigned int payload_length, bool skip)
537{
538 struct fw_iso_packet p = {0};
539 int err = 0;
540
541 if (IS_ERR(s->context))
542 goto end;
543
544 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
545 p.tag = TAG_CIP;
546 p.header_length = header_length;
547 p.payload_length = (!skip) ? payload_length : 0;
548 p.skip = skip;
549 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
550 s->buffer.packets[s->packet_index].offset);
551 if (err < 0) {
552 dev_err(&s->unit->device, "queueing error: %d\n", err);
553 goto end;
554 }
555
556 if (++s->packet_index >= QUEUE_LENGTH)
557 s->packet_index = 0;
558end:
559 return err;
560}
561
562static inline int queue_out_packet(struct amdtp_stream *s,
563 unsigned int payload_length, bool skip)
564{
565 return queue_packet(s, OUT_PACKET_HEADER_SIZE,
566 payload_length, skip);
567}
568
569static inline int queue_in_packet(struct amdtp_stream *s)
570{
571 return queue_packet(s, IN_PACKET_HEADER_SIZE,
572 amdtp_stream_get_max_payload(s), false);
573}
574
575static void handle_out_packet(struct amdtp_stream *s, unsigned int syt)
426{ 576{
427 __be32 *buffer; 577 __be32 *buffer;
428 unsigned int index, data_blocks, syt, ptr; 578 unsigned int data_blocks, payload_length;
429 struct snd_pcm_substream *pcm; 579 struct snd_pcm_substream *pcm;
430 struct fw_iso_packet packet;
431 int err;
432 580
433 if (s->packet_index < 0) 581 if (s->packet_index < 0)
434 return; 582 return;
435 index = s->packet_index;
436 583
437 /* this module generate empty packet for 'no data' */ 584 /* this module generate empty packet for 'no data' */
438 syt = calculate_syt(s, cycle); 585 if (!(s->flags & CIP_BLOCKING) || (syt != CIP_SYT_NO_INFO))
439 if (!(s->flags & CIP_BLOCKING))
440 data_blocks = calculate_data_blocks(s); 586 data_blocks = calculate_data_blocks(s);
441 else if (syt != 0xffff)
442 data_blocks = s->syt_interval;
443 else 587 else
444 data_blocks = 0; 588 data_blocks = 0;
445 589
446 buffer = s->buffer.packets[index].buffer; 590 buffer = s->buffer.packets[s->packet_index].buffer;
447 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) | 591 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
448 (s->data_block_quadlets << 16) | 592 (s->data_block_quadlets << AMDTP_DBS_SHIFT) |
449 s->data_block_counter); 593 s->data_block_counter);
450 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 | 594 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
451 (s->sfc << AMDTP_FDF_SFC_SHIFT) | syt); 595 (s->sfc << CIP_FDF_SFC_SHIFT) | syt);
452 buffer += 2; 596 buffer += 2;
453 597
454 pcm = ACCESS_ONCE(s->pcm); 598 pcm = ACCESS_ONCE(s->pcm);
@@ -461,58 +605,127 @@ static void queue_out_packet(struct amdtp_out_stream *s, unsigned int cycle)
461 605
462 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff; 606 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
463 607
464 packet.payload_length = 8 + data_blocks * 4 * s->data_block_quadlets; 608 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
465 packet.interrupt = IS_ALIGNED(index + 1, INTERRUPT_INTERVAL); 609 if (queue_out_packet(s, payload_length, false) < 0) {
466 packet.skip = 0;
467 packet.tag = TAG_CIP;
468 packet.sy = 0;
469 packet.header_length = 0;
470
471 err = fw_iso_context_queue(s->context, &packet, &s->buffer.iso_buffer,
472 s->buffer.packets[index].offset);
473 if (err < 0) {
474 dev_err(&s->unit->device, "queueing error: %d\n", err);
475 s->packet_index = -1; 610 s->packet_index = -1;
476 amdtp_out_stream_pcm_abort(s); 611 amdtp_stream_pcm_abort(s);
477 return; 612 return;
478 } 613 }
479 614
480 if (++index >= QUEUE_LENGTH) 615 if (pcm)
481 index = 0; 616 update_pcm_pointers(s, pcm, data_blocks);
482 s->packet_index = index; 617}
483 618
484 if (pcm) { 619static void handle_in_packet(struct amdtp_stream *s,
485 if (s->dual_wire) 620 unsigned int payload_quadlets,
486 data_blocks *= 2; 621 __be32 *buffer)
487 622{
488 ptr = s->pcm_buffer_pointer + data_blocks; 623 u32 cip_header[2];
489 if (ptr >= pcm->runtime->buffer_size) 624 unsigned int data_blocks, data_block_quadlets, data_block_counter,
490 ptr -= pcm->runtime->buffer_size; 625 dbc_interval;
491 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr; 626 struct snd_pcm_substream *pcm = NULL;
492 627 bool lost;
493 s->pcm_period_pointer += data_blocks; 628
494 if (s->pcm_period_pointer >= pcm->runtime->period_size) { 629 cip_header[0] = be32_to_cpu(buffer[0]);
495 s->pcm_period_pointer -= pcm->runtime->period_size; 630 cip_header[1] = be32_to_cpu(buffer[1]);
496 s->pointer_flush = false; 631
497 tasklet_hi_schedule(&s->period_tasklet); 632 /*
633 * This module supports 'Two-quadlet CIP header with SYT field'.
634 * For convenience, also check FMT field is AM824 or not.
635 */
636 if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
637 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH) ||
638 ((cip_header[1] & CIP_FMT_MASK) != CIP_FMT_AM)) {
639 dev_info_ratelimited(&s->unit->device,
640 "Invalid CIP header for AMDTP: %08X:%08X\n",
641 cip_header[0], cip_header[1]);
642 goto end;
643 }
644
645 /* Calculate data blocks */
646 if (payload_quadlets < 3 ||
647 ((cip_header[1] & CIP_FDF_MASK) ==
648 (AMDTP_FDF_NO_DATA << CIP_FDF_SFC_SHIFT))) {
649 data_blocks = 0;
650 } else {
651 data_block_quadlets =
652 (cip_header[0] & AMDTP_DBS_MASK) >> AMDTP_DBS_SHIFT;
653 /* avoid division by zero */
654 if (data_block_quadlets == 0) {
655 dev_info_ratelimited(&s->unit->device,
656 "Detect invalid value in dbs field: %08X\n",
657 cip_header[0]);
658 goto err;
498 } 659 }
660 if (s->flags & CIP_WRONG_DBS)
661 data_block_quadlets = s->data_block_quadlets;
662
663 data_blocks = (payload_quadlets - 2) / data_block_quadlets;
499 } 664 }
500}
501 665
502static void pcm_period_tasklet(unsigned long data) 666 /* Check data block counter continuity */
503{ 667 data_block_counter = cip_header[0] & AMDTP_DBC_MASK;
504 struct amdtp_out_stream *s = (void *)data; 668 if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
505 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm); 669 s->data_block_counter != UINT_MAX)
670 data_block_counter = s->data_block_counter;
671
672 if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) && data_block_counter == 0) ||
673 (s->data_block_counter == UINT_MAX)) {
674 lost = false;
675 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
676 lost = data_block_counter != s->data_block_counter;
677 } else {
678 if ((data_blocks > 0) && (s->tx_dbc_interval > 0))
679 dbc_interval = s->tx_dbc_interval;
680 else
681 dbc_interval = data_blocks;
682
683 lost = data_block_counter !=
684 ((s->data_block_counter + dbc_interval) & 0xff);
685 }
686
687 if (lost) {
688 dev_info(&s->unit->device,
689 "Detect discontinuity of CIP: %02X %02X\n",
690 s->data_block_counter, data_block_counter);
691 goto err;
692 }
693
694 if (data_blocks > 0) {
695 buffer += 2;
696
697 pcm = ACCESS_ONCE(s->pcm);
698 if (pcm)
699 s->transfer_samples(s, pcm, buffer, data_blocks);
700
701 if (s->midi_ports)
702 amdtp_pull_midi(s, buffer, data_blocks);
703 }
704
705 if (s->flags & CIP_DBC_IS_END_EVENT)
706 s->data_block_counter = data_block_counter;
707 else
708 s->data_block_counter =
709 (data_block_counter + data_blocks) & 0xff;
710end:
711 if (queue_in_packet(s) < 0)
712 goto err;
506 713
507 if (pcm) 714 if (pcm)
508 snd_pcm_period_elapsed(pcm); 715 update_pcm_pointers(s, pcm, data_blocks);
716
717 return;
718err:
719 s->packet_index = -1;
720 amdtp_stream_pcm_abort(s);
509} 721}
510 722
511static void out_packet_callback(struct fw_iso_context *context, u32 cycle, 723static void out_stream_callback(struct fw_iso_context *context, u32 cycle,
512 size_t header_length, void *header, void *data) 724 size_t header_length, void *header,
725 void *private_data)
513{ 726{
514 struct amdtp_out_stream *s = data; 727 struct amdtp_stream *s = private_data;
515 unsigned int i, packets = header_length / 4; 728 unsigned int i, syt, packets = header_length / 4;
516 729
517 /* 730 /*
518 * Compute the cycle of the last queued packet. 731 * Compute the cycle of the last queued packet.
@@ -521,43 +734,102 @@ static void out_packet_callback(struct fw_iso_context *context, u32 cycle,
521 */ 734 */
522 cycle += QUEUE_LENGTH - packets; 735 cycle += QUEUE_LENGTH - packets;
523 736
524 for (i = 0; i < packets; ++i) 737 for (i = 0; i < packets; ++i) {
525 queue_out_packet(s, ++cycle); 738 syt = calculate_syt(s, ++cycle);
739 handle_out_packet(s, syt);
740 }
526 fw_iso_context_queue_flush(s->context); 741 fw_iso_context_queue_flush(s->context);
527} 742}
528 743
529static int queue_initial_skip_packets(struct amdtp_out_stream *s) 744static void in_stream_callback(struct fw_iso_context *context, u32 cycle,
745 size_t header_length, void *header,
746 void *private_data)
530{ 747{
531 struct fw_iso_packet skip_packet = { 748 struct amdtp_stream *s = private_data;
532 .skip = 1, 749 unsigned int p, syt, packets, payload_quadlets;
533 }; 750 __be32 *buffer, *headers = header;
534 unsigned int i;
535 int err;
536 751
537 for (i = 0; i < QUEUE_LENGTH; ++i) { 752 /* The number of packets in buffer */
538 skip_packet.interrupt = IS_ALIGNED(s->packet_index + 1, 753 packets = header_length / IN_PACKET_HEADER_SIZE;
539 INTERRUPT_INTERVAL); 754
540 err = fw_iso_context_queue(s->context, &skip_packet, NULL, 0); 755 for (p = 0; p < packets; p++) {
541 if (err < 0) 756 if (s->packet_index < 0)
542 return err; 757 break;
543 if (++s->packet_index >= QUEUE_LENGTH) 758
544 s->packet_index = 0; 759 buffer = s->buffer.packets[s->packet_index].buffer;
760
761 /* Process sync slave stream */
762 if (s->sync_slave && s->sync_slave->callbacked) {
763 syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
764 handle_out_packet(s->sync_slave, syt);
765 }
766
767 /* The number of quadlets in this packet */
768 payload_quadlets =
769 (be32_to_cpu(headers[p]) >> ISO_DATA_LENGTH_SHIFT) / 4;
770 handle_in_packet(s, payload_quadlets, buffer);
545 } 771 }
546 772
547 return 0; 773 /* Queueing error or detecting discontinuity */
774 if (s->packet_index < 0) {
775 /* Abort sync slave. */
776 if (s->sync_slave) {
777 s->sync_slave->packet_index = -1;
778 amdtp_stream_pcm_abort(s->sync_slave);
779 }
780 return;
781 }
782
783 /* when sync to device, flush the packets for slave stream */
784 if (s->sync_slave && s->sync_slave->callbacked)
785 fw_iso_context_queue_flush(s->sync_slave->context);
786
787 fw_iso_context_queue_flush(s->context);
788}
789
790/* processing is done by master callback */
791static void slave_stream_callback(struct fw_iso_context *context, u32 cycle,
792 size_t header_length, void *header,
793 void *private_data)
794{
795 return;
796}
797
798/* this is executed one time */
799static void amdtp_stream_first_callback(struct fw_iso_context *context,
800 u32 cycle, size_t header_length,
801 void *header, void *private_data)
802{
803 struct amdtp_stream *s = private_data;
804
805 /*
806 * For in-stream, first packet has come.
807 * For out-stream, prepared to transmit first packet
808 */
809 s->callbacked = true;
810 wake_up(&s->callback_wait);
811
812 if (s->direction == AMDTP_IN_STREAM)
813 context->callback.sc = in_stream_callback;
814 else if ((s->flags & CIP_BLOCKING) && (s->flags & CIP_SYNC_TO_DEVICE))
815 context->callback.sc = slave_stream_callback;
816 else
817 context->callback.sc = out_stream_callback;
818
819 context->callback.sc(context, cycle, header_length, header, s);
548} 820}
549 821
550/** 822/**
551 * amdtp_out_stream_start - start sending packets 823 * amdtp_stream_start - start transferring packets
552 * @s: the AMDTP output stream to start 824 * @s: the AMDTP stream to start
553 * @channel: the isochronous channel on the bus 825 * @channel: the isochronous channel on the bus
554 * @speed: firewire speed code 826 * @speed: firewire speed code
555 * 827 *
556 * The stream cannot be started until it has been configured with 828 * The stream cannot be started until it has been configured with
557 * amdtp_out_stream_set_parameters() and amdtp_out_stream_set_pcm_format(), 829 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
558 * and it must be started before any PCM or MIDI device can be started. 830 * device can be started.
559 */ 831 */
560int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed) 832int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
561{ 833{
562 static const struct { 834 static const struct {
563 unsigned int data_block; 835 unsigned int data_block;
@@ -571,47 +843,72 @@ int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed)
571 [CIP_SFC_88200] = { 0, 67 }, 843 [CIP_SFC_88200] = { 0, 67 },
572 [CIP_SFC_176400] = { 0, 67 }, 844 [CIP_SFC_176400] = { 0, 67 },
573 }; 845 };
574 int err; 846 unsigned int header_size;
847 enum dma_data_direction dir;
848 int type, tag, err;
575 849
576 mutex_lock(&s->mutex); 850 mutex_lock(&s->mutex);
577 851
578 if (WARN_ON(amdtp_out_stream_running(s) || 852 if (WARN_ON(amdtp_stream_running(s) ||
579 (!s->pcm_channels && !s->midi_ports))) { 853 (s->data_block_quadlets < 1))) {
580 err = -EBADFD; 854 err = -EBADFD;
581 goto err_unlock; 855 goto err_unlock;
582 } 856 }
583 857
858 if (s->direction == AMDTP_IN_STREAM &&
859 s->flags & CIP_SKIP_INIT_DBC_CHECK)
860 s->data_block_counter = UINT_MAX;
861 else
862 s->data_block_counter = 0;
584 s->data_block_state = initial_state[s->sfc].data_block; 863 s->data_block_state = initial_state[s->sfc].data_block;
585 s->syt_offset_state = initial_state[s->sfc].syt_offset; 864 s->syt_offset_state = initial_state[s->sfc].syt_offset;
586 s->last_syt_offset = TICKS_PER_CYCLE; 865 s->last_syt_offset = TICKS_PER_CYCLE;
587 866
867 /* initialize packet buffer */
868 if (s->direction == AMDTP_IN_STREAM) {
869 dir = DMA_FROM_DEVICE;
870 type = FW_ISO_CONTEXT_RECEIVE;
871 header_size = IN_PACKET_HEADER_SIZE;
872 } else {
873 dir = DMA_TO_DEVICE;
874 type = FW_ISO_CONTEXT_TRANSMIT;
875 header_size = OUT_PACKET_HEADER_SIZE;
876 }
588 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH, 877 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
589 amdtp_out_stream_get_max_payload(s), 878 amdtp_stream_get_max_payload(s), dir);
590 DMA_TO_DEVICE);
591 if (err < 0) 879 if (err < 0)
592 goto err_unlock; 880 goto err_unlock;
593 881
594 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card, 882 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
595 FW_ISO_CONTEXT_TRANSMIT, 883 type, channel, speed, header_size,
596 channel, speed, 0, 884 amdtp_stream_first_callback, s);
597 out_packet_callback, s);
598 if (IS_ERR(s->context)) { 885 if (IS_ERR(s->context)) {
599 err = PTR_ERR(s->context); 886 err = PTR_ERR(s->context);
600 if (err == -EBUSY) 887 if (err == -EBUSY)
601 dev_err(&s->unit->device, 888 dev_err(&s->unit->device,
602 "no free output stream on this controller\n"); 889 "no free stream on this controller\n");
603 goto err_buffer; 890 goto err_buffer;
604 } 891 }
605 892
606 amdtp_out_stream_update(s); 893 amdtp_stream_update(s);
607 894
608 s->packet_index = 0; 895 s->packet_index = 0;
609 s->data_block_counter = 0; 896 do {
610 err = queue_initial_skip_packets(s); 897 if (s->direction == AMDTP_IN_STREAM)
611 if (err < 0) 898 err = queue_in_packet(s);
612 goto err_context; 899 else
900 err = queue_out_packet(s, 0, true);
901 if (err < 0)
902 goto err_context;
903 } while (s->packet_index > 0);
613 904
614 err = fw_iso_context_start(s->context, -1, 0, 0); 905 /* NOTE: TAG1 matches CIP. This just affects in stream. */
906 tag = FW_ISO_CONTEXT_MATCH_TAG1;
907 if (s->flags & CIP_EMPTY_WITH_TAG0)
908 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
909
910 s->callbacked = false;
911 err = fw_iso_context_start(s->context, -1, 0, tag);
615 if (err < 0) 912 if (err < 0)
616 goto err_context; 913 goto err_context;
617 914
@@ -629,49 +926,49 @@ err_unlock:
629 926
630 return err; 927 return err;
631} 928}
632EXPORT_SYMBOL(amdtp_out_stream_start); 929EXPORT_SYMBOL(amdtp_stream_start);
633 930
634/** 931/**
635 * amdtp_out_stream_pcm_pointer - get the PCM buffer position 932 * amdtp_stream_pcm_pointer - get the PCM buffer position
636 * @s: the AMDTP output stream that transports the PCM data 933 * @s: the AMDTP stream that transports the PCM data
637 * 934 *
638 * Returns the current buffer position, in frames. 935 * Returns the current buffer position, in frames.
639 */ 936 */
640unsigned long amdtp_out_stream_pcm_pointer(struct amdtp_out_stream *s) 937unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
641{ 938{
642 /* this optimization is allowed to be racy */ 939 /* this optimization is allowed to be racy */
643 if (s->pointer_flush) 940 if (s->pointer_flush && amdtp_stream_running(s))
644 fw_iso_context_flush_completions(s->context); 941 fw_iso_context_flush_completions(s->context);
645 else 942 else
646 s->pointer_flush = true; 943 s->pointer_flush = true;
647 944
648 return ACCESS_ONCE(s->pcm_buffer_pointer); 945 return ACCESS_ONCE(s->pcm_buffer_pointer);
649} 946}
650EXPORT_SYMBOL(amdtp_out_stream_pcm_pointer); 947EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
651 948
652/** 949/**
653 * amdtp_out_stream_update - update the stream after a bus reset 950 * amdtp_stream_update - update the stream after a bus reset
654 * @s: the AMDTP output stream 951 * @s: the AMDTP stream
655 */ 952 */
656void amdtp_out_stream_update(struct amdtp_out_stream *s) 953void amdtp_stream_update(struct amdtp_stream *s)
657{ 954{
658 ACCESS_ONCE(s->source_node_id_field) = 955 ACCESS_ONCE(s->source_node_id_field) =
659 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24; 956 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
660} 957}
661EXPORT_SYMBOL(amdtp_out_stream_update); 958EXPORT_SYMBOL(amdtp_stream_update);
662 959
663/** 960/**
664 * amdtp_out_stream_stop - stop sending packets 961 * amdtp_stream_stop - stop sending packets
665 * @s: the AMDTP output stream to stop 962 * @s: the AMDTP stream to stop
666 * 963 *
667 * All PCM and MIDI devices of the stream must be stopped before the stream 964 * All PCM and MIDI devices of the stream must be stopped before the stream
668 * itself can be stopped. 965 * itself can be stopped.
669 */ 966 */
670void amdtp_out_stream_stop(struct amdtp_out_stream *s) 967void amdtp_stream_stop(struct amdtp_stream *s)
671{ 968{
672 mutex_lock(&s->mutex); 969 mutex_lock(&s->mutex);
673 970
674 if (!amdtp_out_stream_running(s)) { 971 if (!amdtp_stream_running(s)) {
675 mutex_unlock(&s->mutex); 972 mutex_unlock(&s->mutex);
676 return; 973 return;
677 } 974 }
@@ -682,18 +979,20 @@ void amdtp_out_stream_stop(struct amdtp_out_stream *s)
682 s->context = ERR_PTR(-1); 979 s->context = ERR_PTR(-1);
683 iso_packets_buffer_destroy(&s->buffer, s->unit); 980 iso_packets_buffer_destroy(&s->buffer, s->unit);
684 981
982 s->callbacked = false;
983
685 mutex_unlock(&s->mutex); 984 mutex_unlock(&s->mutex);
686} 985}
687EXPORT_SYMBOL(amdtp_out_stream_stop); 986EXPORT_SYMBOL(amdtp_stream_stop);
688 987
689/** 988/**
690 * amdtp_out_stream_pcm_abort - abort the running PCM device 989 * amdtp_stream_pcm_abort - abort the running PCM device
691 * @s: the AMDTP stream about to be stopped 990 * @s: the AMDTP stream about to be stopped
692 * 991 *
693 * If the isochronous stream needs to be stopped asynchronously, call this 992 * If the isochronous stream needs to be stopped asynchronously, call this
694 * function first to stop the PCM device. 993 * function first to stop the PCM device.
695 */ 994 */
696void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s) 995void amdtp_stream_pcm_abort(struct amdtp_stream *s)
697{ 996{
698 struct snd_pcm_substream *pcm; 997 struct snd_pcm_substream *pcm;
699 998
@@ -705,4 +1004,4 @@ void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s)
705 snd_pcm_stream_unlock_irq(pcm); 1004 snd_pcm_stream_unlock_irq(pcm);
706 } 1005 }
707} 1006}
708EXPORT_SYMBOL(amdtp_out_stream_pcm_abort); 1007EXPORT_SYMBOL(amdtp_stream_pcm_abort);