ALSA: firewire-motu: add support for MOTU 828mk2 as a model with protocol version 2
[linux-block.git] / sound / firewire / amdtp-stream.c
CommitLineData
31ef9134
CL
1/*
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>
7b2d99fa 15#include <sound/pcm_params.h>
d67c46b9 16#include "amdtp-stream.h"
31ef9134
CL
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
0c95c1d6
TS
22/* Always support Linux tracing subsystem. */
23#define CREATE_TRACE_POINTS
24#include "amdtp-stream-trace.h"
25
ca5b5050 26#define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */
31ef9134 27
b445db44
TS
28/* isochronous header parameters */
29#define ISO_DATA_LENGTH_SHIFT 16
31ef9134
CL
30#define TAG_CIP 1
31
b445db44 32/* common isochronous packet header parameters */
9a2820c1
TS
33#define CIP_EOH_SHIFT 31
34#define CIP_EOH (1u << CIP_EOH_SHIFT)
b445db44 35#define CIP_EOH_MASK 0x80000000
9a2820c1
TS
36#define CIP_SID_SHIFT 24
37#define CIP_SID_MASK 0x3f000000
38#define CIP_DBS_MASK 0x00ff0000
39#define CIP_DBS_SHIFT 16
9863874f
TS
40#define CIP_SPH_MASK 0x00000400
41#define CIP_SPH_SHIFT 10
9a2820c1
TS
42#define CIP_DBC_MASK 0x000000ff
43#define CIP_FMT_SHIFT 24
b445db44 44#define CIP_FMT_MASK 0x3f000000
9a2820c1
TS
45#define CIP_FDF_MASK 0x00ff0000
46#define CIP_FDF_SHIFT 16
b445db44
TS
47#define CIP_SYT_MASK 0x0000ffff
48#define CIP_SYT_NO_INFO 0xffff
b445db44 49
51c29fd2 50/* Audio and Music transfer protocol specific parameters */
414ba022 51#define CIP_FMT_AM 0x10
2b3fc456 52#define AMDTP_FDF_NO_DATA 0xff
31ef9134
CL
53
54/* TODO: make these configurable */
55#define INTERRUPT_INTERVAL 16
56#define QUEUE_LENGTH 48
57
2b3fc456 58#define IN_PACKET_HEADER_SIZE 4
4b7da117
TS
59#define OUT_PACKET_HEADER_SIZE 0
60
76fb8789
CL
61static void pcm_period_tasklet(unsigned long data);
62
31ef9134 63/**
be4a2894
TS
64 * amdtp_stream_init - initialize an AMDTP stream structure
65 * @s: the AMDTP stream to initialize
31ef9134 66 * @unit: the target of the stream
3ff7e8f0 67 * @dir: the direction of stream
31ef9134 68 * @flags: the packet transmission method to use
5955815e 69 * @fmt: the value of fmt field in CIP header
df075fee
TS
70 * @process_data_blocks: callback handler to process data blocks
71 * @protocol_size: the size to allocate newly for protocol
31ef9134 72 */
be4a2894 73int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
5955815e 74 enum amdtp_stream_direction dir, enum cip_flags flags,
df075fee
TS
75 unsigned int fmt,
76 amdtp_stream_process_data_blocks_t process_data_blocks,
77 unsigned int protocol_size)
31ef9134 78{
df075fee
TS
79 if (process_data_blocks == NULL)
80 return -EINVAL;
81
82 s->protocol = kzalloc(protocol_size, GFP_KERNEL);
83 if (!s->protocol)
84 return -ENOMEM;
85
c6f224dc 86 s->unit = unit;
3ff7e8f0 87 s->direction = dir;
31ef9134
CL
88 s->flags = flags;
89 s->context = ERR_PTR(-1);
90 mutex_init(&s->mutex);
76fb8789 91 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
ec00f5e4 92 s->packet_index = 0;
31ef9134 93
7b3b0d85
TS
94 init_waitqueue_head(&s->callback_wait);
95 s->callbacked = false;
7b3b0d85 96
5955815e 97 s->fmt = fmt;
df075fee 98 s->process_data_blocks = process_data_blocks;
414ba022 99
31ef9134
CL
100 return 0;
101}
be4a2894 102EXPORT_SYMBOL(amdtp_stream_init);
31ef9134
CL
103
104/**
be4a2894
TS
105 * amdtp_stream_destroy - free stream resources
106 * @s: the AMDTP stream to destroy
31ef9134 107 */
be4a2894 108void amdtp_stream_destroy(struct amdtp_stream *s)
31ef9134 109{
44c376b9
TS
110 /* Not initialized. */
111 if (s->protocol == NULL)
112 return;
113
be4a2894 114 WARN_ON(amdtp_stream_running(s));
df075fee 115 kfree(s->protocol);
31ef9134 116 mutex_destroy(&s->mutex);
31ef9134 117}
be4a2894 118EXPORT_SYMBOL(amdtp_stream_destroy);
31ef9134 119
c5280e99 120const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
a7304e3b
CL
121 [CIP_SFC_32000] = 8,
122 [CIP_SFC_44100] = 8,
123 [CIP_SFC_48000] = 8,
124 [CIP_SFC_88200] = 16,
125 [CIP_SFC_96000] = 16,
126 [CIP_SFC_176400] = 32,
127 [CIP_SFC_192000] = 32,
128};
129EXPORT_SYMBOL(amdtp_syt_intervals);
130
f9503a68 131const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
1017abed
TS
132 [CIP_SFC_32000] = 32000,
133 [CIP_SFC_44100] = 44100,
134 [CIP_SFC_48000] = 48000,
135 [CIP_SFC_88200] = 88200,
136 [CIP_SFC_96000] = 96000,
137 [CIP_SFC_176400] = 176400,
138 [CIP_SFC_192000] = 192000,
139};
140EXPORT_SYMBOL(amdtp_rate_table);
141
7b2d99fa
TS
142/**
143 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
144 * @s: the AMDTP stream, which must be initialized.
145 * @runtime: the PCM substream runtime
146 */
147int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
148 struct snd_pcm_runtime *runtime)
149{
150 int err;
151
7b2d99fa
TS
152 /*
153 * Currently firewire-lib processes 16 packets in one software
154 * interrupt callback. This equals to 2msec but actually the
155 * interval of the interrupts has a jitter.
156 * Additionally, even if adding a constraint to fit period size to
157 * 2msec, actual calculated frames per period doesn't equal to 2msec,
158 * depending on sampling rate.
159 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
160 * Here let us use 5msec for safe period interrupt.
161 */
162 err = snd_pcm_hw_constraint_minmax(runtime,
163 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
164 5000, UINT_MAX);
165 if (err < 0)
166 goto end;
167
168 /* Non-Blocking stream has no more constraints */
169 if (!(s->flags & CIP_BLOCKING))
170 goto end;
171
172 /*
173 * One AMDTP packet can include some frames. In blocking mode, the
174 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
175 * depending on its sampling rate. For accurate period interrupt, it's
ce991981 176 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
7b2d99fa 177 *
ce991981
YG
178 * TODO: These constraints can be improved with proper rules.
179 * Currently apply LCM of SYT_INTERVALs.
7b2d99fa
TS
180 */
181 err = snd_pcm_hw_constraint_step(runtime, 0,
182 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
183 if (err < 0)
184 goto end;
185 err = snd_pcm_hw_constraint_step(runtime, 0,
186 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
187end:
188 return err;
189}
190EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
191
31ef9134 192/**
be4a2894
TS
193 * amdtp_stream_set_parameters - set stream parameters
194 * @s: the AMDTP stream to configure
31ef9134 195 * @rate: the sample rate
df075fee 196 * @data_block_quadlets: the size of a data block in quadlet unit
31ef9134 197 *
a7304e3b 198 * The parameters must be set before the stream is started, and must not be
31ef9134
CL
199 * changed while the stream is running.
200 */
df075fee
TS
201int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
202 unsigned int data_block_quadlets)
31ef9134 203{
df075fee 204 unsigned int sfc;
31ef9134 205
547e631c 206 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
1017abed 207 if (amdtp_rate_table[sfc] == rate)
547e631c
TS
208 break;
209 }
210 if (sfc == ARRAY_SIZE(amdtp_rate_table))
211 return -EINVAL;
e84d15f6 212
e84d15f6 213 s->sfc = sfc;
df075fee 214 s->data_block_quadlets = data_block_quadlets;
a7304e3b 215 s->syt_interval = amdtp_syt_intervals[sfc];
e84d15f6
CL
216
217 /* default buffering in the device */
218 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
219 if (s->flags & CIP_BLOCKING)
220 /* additional buffering needed to adjust for no-data packets */
221 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
77d2a8a4 222
547e631c 223 return 0;
31ef9134 224}
be4a2894 225EXPORT_SYMBOL(amdtp_stream_set_parameters);
31ef9134
CL
226
227/**
be4a2894
TS
228 * amdtp_stream_get_max_payload - get the stream's packet size
229 * @s: the AMDTP stream
31ef9134
CL
230 *
231 * This function must not be called before the stream has been configured
be4a2894 232 * with amdtp_stream_set_parameters().
31ef9134 233 */
be4a2894 234unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
31ef9134 235{
a2064710
TS
236 unsigned int multiplier = 1;
237
238 if (s->flags & CIP_JUMBO_PAYLOAD)
239 multiplier = 5;
240
241 return 8 + s->syt_interval * s->data_block_quadlets * 4 * multiplier;
31ef9134 242}
be4a2894 243EXPORT_SYMBOL(amdtp_stream_get_max_payload);
31ef9134 244
76fb8789 245/**
be4a2894
TS
246 * amdtp_stream_pcm_prepare - prepare PCM device for running
247 * @s: the AMDTP stream
76fb8789
CL
248 *
249 * This function should be called from the PCM device's .prepare callback.
250 */
be4a2894 251void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
76fb8789
CL
252{
253 tasklet_kill(&s->period_tasklet);
254 s->pcm_buffer_pointer = 0;
255 s->pcm_period_pointer = 0;
256}
be4a2894 257EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
76fb8789 258
875be091
TS
259static unsigned int calculate_data_blocks(struct amdtp_stream *s,
260 unsigned int syt)
31ef9134
CL
261{
262 unsigned int phase, data_blocks;
263
875be091
TS
264 /* Blocking mode. */
265 if (s->flags & CIP_BLOCKING) {
266 /* This module generate empty packet for 'no data'. */
267 if (syt == CIP_SYT_NO_INFO)
268 data_blocks = 0;
269 else
270 data_blocks = s->syt_interval;
271 /* Non-blocking mode. */
31ef9134 272 } else {
875be091
TS
273 if (!cip_sfc_is_base_44100(s->sfc)) {
274 /* Sample_rate / 8000 is an integer, and precomputed. */
275 data_blocks = s->data_block_state;
276 } else {
277 phase = s->data_block_state;
31ef9134
CL
278
279 /*
280 * This calculates the number of data blocks per packet so that
281 * 1) the overall rate is correct and exactly synchronized to
282 * the bus clock, and
283 * 2) packets with a rounded-up number of blocks occur as early
284 * as possible in the sequence (to prevent underruns of the
285 * device's buffer).
286 */
875be091
TS
287 if (s->sfc == CIP_SFC_44100)
288 /* 6 6 5 6 5 6 5 ... */
289 data_blocks = 5 + ((phase & 1) ^
290 (phase == 0 || phase >= 40));
291 else
292 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
293 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
294 if (++phase >= (80 >> (s->sfc >> 1)))
295 phase = 0;
296 s->data_block_state = phase;
297 }
31ef9134
CL
298 }
299
300 return data_blocks;
301}
302
be4a2894 303static unsigned int calculate_syt(struct amdtp_stream *s,
31ef9134
CL
304 unsigned int cycle)
305{
306 unsigned int syt_offset, phase, index, syt;
307
308 if (s->last_syt_offset < TICKS_PER_CYCLE) {
309 if (!cip_sfc_is_base_44100(s->sfc))
310 syt_offset = s->last_syt_offset + s->syt_offset_state;
311 else {
312 /*
313 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
314 * n * SYT_INTERVAL * 24576000 / sample_rate
315 * Modulo TICKS_PER_CYCLE, the difference between successive
316 * elements is about 1386.23. Rounding the results of this
317 * formula to the SYT precision results in a sequence of
318 * differences that begins with:
319 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
320 * This code generates _exactly_ the same sequence.
321 */
322 phase = s->syt_offset_state;
323 index = phase % 13;
324 syt_offset = s->last_syt_offset;
325 syt_offset += 1386 + ((index && !(index & 3)) ||
326 phase == 146);
327 if (++phase >= 147)
328 phase = 0;
329 s->syt_offset_state = phase;
330 }
331 } else
332 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
333 s->last_syt_offset = syt_offset;
334
be454366 335 if (syt_offset < TICKS_PER_CYCLE) {
e84d15f6 336 syt_offset += s->transfer_delay;
be454366
CL
337 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
338 syt += syt_offset % TICKS_PER_CYCLE;
31ef9134 339
b445db44 340 return syt & CIP_SYT_MASK;
be454366 341 } else {
b445db44 342 return CIP_SYT_NO_INFO;
be454366 343 }
31ef9134
CL
344}
345
4b7da117
TS
346static void update_pcm_pointers(struct amdtp_stream *s,
347 struct snd_pcm_substream *pcm,
348 unsigned int frames)
65845f29
TS
349{
350 unsigned int ptr;
351
4b7da117
TS
352 ptr = s->pcm_buffer_pointer + frames;
353 if (ptr >= pcm->runtime->buffer_size)
354 ptr -= pcm->runtime->buffer_size;
355 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
356
357 s->pcm_period_pointer += frames;
358 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
359 s->pcm_period_pointer -= pcm->runtime->period_size;
4b7da117
TS
360 tasklet_hi_schedule(&s->period_tasklet);
361 }
362}
363
364static void pcm_period_tasklet(unsigned long data)
365{
366 struct amdtp_stream *s = (void *)data;
367 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
368
369 if (pcm)
370 snd_pcm_period_elapsed(pcm);
371}
372
ff38e0c7
TS
373static int queue_packet(struct amdtp_stream *s, unsigned int header_length,
374 unsigned int payload_length)
4b7da117
TS
375{
376 struct fw_iso_packet p = {0};
7b3b0d85
TS
377 int err = 0;
378
379 if (IS_ERR(s->context))
380 goto end;
4b7da117
TS
381
382 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
383 p.tag = TAG_CIP;
384 p.header_length = header_length;
ff38e0c7
TS
385 if (payload_length > 0)
386 p.payload_length = payload_length;
387 else
388 p.skip = true;
4b7da117
TS
389 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
390 s->buffer.packets[s->packet_index].offset);
391 if (err < 0) {
392 dev_err(&s->unit->device, "queueing error: %d\n", err);
393 goto end;
394 }
395
396 if (++s->packet_index >= QUEUE_LENGTH)
397 s->packet_index = 0;
398end:
399 return err;
400}
401
402static inline int queue_out_packet(struct amdtp_stream *s,
ff38e0c7 403 unsigned int payload_length)
4b7da117 404{
ff38e0c7 405 return queue_packet(s, OUT_PACKET_HEADER_SIZE, payload_length);
4b7da117
TS
406}
407
2b3fc456
TS
408static inline int queue_in_packet(struct amdtp_stream *s)
409{
410 return queue_packet(s, IN_PACKET_HEADER_SIZE,
ff38e0c7 411 amdtp_stream_get_max_payload(s));
2b3fc456
TS
412}
413
a9c4284b
TS
414static int handle_out_packet(struct amdtp_stream *s, unsigned int cycle,
415 unsigned int index)
31ef9134
CL
416{
417 __be32 *buffer;
390a1512
TS
418 unsigned int syt;
419 unsigned int data_blocks;
6fc6b9ce 420 unsigned int payload_length;
20e44577 421 unsigned int pcm_frames;
31ef9134 422 struct snd_pcm_substream *pcm;
31ef9134 423
ccccad86 424 buffer = s->buffer.packets[s->packet_index].buffer;
390a1512
TS
425 syt = calculate_syt(s, cycle);
426 data_blocks = calculate_data_blocks(s, syt);
df075fee 427 pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
20e44577 428
9dae017b
TS
429 if (s->flags & CIP_DBC_IS_END_EVENT)
430 s->data_block_counter =
431 (s->data_block_counter + data_blocks) & 0xff;
432
31ef9134 433 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
9a2820c1 434 (s->data_block_quadlets << CIP_DBS_SHIFT) |
9863874f 435 ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) |
31ef9134 436 s->data_block_counter);
414ba022
TS
437 buffer[1] = cpu_to_be32(CIP_EOH |
438 ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
439 ((s->fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
440 (syt & CIP_SYT_MASK));
31ef9134 441
9dae017b
TS
442 if (!(s->flags & CIP_DBC_IS_END_EVENT))
443 s->data_block_counter =
444 (s->data_block_counter + data_blocks) & 0xff;
4b7da117 445 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
0c95c1d6 446
a9c4284b 447 trace_out_packet(s, cycle, buffer, payload_length, index);
0c95c1d6 448
ff38e0c7 449 if (queue_out_packet(s, payload_length) < 0)
a4103bd7 450 return -EIO;
31ef9134 451
20e44577
TS
452 pcm = ACCESS_ONCE(s->pcm);
453 if (pcm && pcm_frames > 0)
454 update_pcm_pointers(s, pcm, pcm_frames);
a4103bd7
TS
455
456 /* No need to return the number of handled data blocks. */
457 return 0;
76fb8789
CL
458}
459
6fc6b9ce 460static int handle_in_packet(struct amdtp_stream *s,
a9c4284b
TS
461 unsigned int payload_quadlets, unsigned int cycle,
462 unsigned int index)
2b3fc456 463{
d9a16fc9 464 __be32 *buffer;
2b3fc456 465 u32 cip_header[2];
9863874f 466 unsigned int sph, fmt, fdf, syt;
6fc6b9ce 467 unsigned int data_block_quadlets, data_block_counter, dbc_interval;
d9a16fc9 468 unsigned int data_blocks;
20e44577
TS
469 struct snd_pcm_substream *pcm;
470 unsigned int pcm_frames;
c8bdf49b 471 bool lost;
2b3fc456 472
d9a16fc9 473 buffer = s->buffer.packets[s->packet_index].buffer;
2b3fc456
TS
474 cip_header[0] = be32_to_cpu(buffer[0]);
475 cip_header[1] = be32_to_cpu(buffer[1]);
476
a9c4284b 477 trace_in_packet(s, cycle, cip_header, payload_quadlets, index);
0c95c1d6 478
2b3fc456
TS
479 /*
480 * This module supports 'Two-quadlet CIP header with SYT field'.
77d2a8a4 481 * For convenience, also check FMT field is AM824 or not.
2b3fc456
TS
482 */
483 if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
414ba022 484 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) {
2b3fc456
TS
485 dev_info_ratelimited(&s->unit->device,
486 "Invalid CIP header for AMDTP: %08X:%08X\n",
487 cip_header[0], cip_header[1]);
d9a16fc9 488 data_blocks = 0;
20e44577 489 pcm_frames = 0;
2b3fc456
TS
490 goto end;
491 }
492
414ba022 493 /* Check valid protocol or not. */
9863874f 494 sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT;
414ba022 495 fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
9863874f 496 if (sph != s->sph || fmt != s->fmt) {
2a7e1713
TS
497 dev_info_ratelimited(&s->unit->device,
498 "Detect unexpected protocol: %08x %08x\n",
499 cip_header[0], cip_header[1]);
d9a16fc9 500 data_blocks = 0;
2a7e1713
TS
501 pcm_frames = 0;
502 goto end;
414ba022
TS
503 }
504
2b3fc456 505 /* Calculate data blocks */
414ba022 506 fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT;
2b3fc456 507 if (payload_quadlets < 3 ||
414ba022 508 (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
d9a16fc9 509 data_blocks = 0;
2b3fc456
TS
510 } else {
511 data_block_quadlets =
9a2820c1 512 (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
2b3fc456
TS
513 /* avoid division by zero */
514 if (data_block_quadlets == 0) {
12e0f438 515 dev_err(&s->unit->device,
2b3fc456
TS
516 "Detect invalid value in dbs field: %08X\n",
517 cip_header[0]);
a9007054 518 return -EPROTO;
2b3fc456 519 }
69702239
TS
520 if (s->flags & CIP_WRONG_DBS)
521 data_block_quadlets = s->data_block_quadlets;
2b3fc456 522
d9a16fc9 523 data_blocks = (payload_quadlets - 2) / data_block_quadlets;
2b3fc456
TS
524 }
525
526 /* Check data block counter continuity */
9a2820c1 527 data_block_counter = cip_header[0] & CIP_DBC_MASK;
d9a16fc9 528 if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
9d59124c
TS
529 s->data_block_counter != UINT_MAX)
530 data_block_counter = s->data_block_counter;
531
18f5ed36
TS
532 if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) &&
533 data_block_counter == s->tx_first_dbc) ||
534 s->data_block_counter == UINT_MAX) {
b84b1a27
TS
535 lost = false;
536 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
c8bdf49b 537 lost = data_block_counter != s->data_block_counter;
d9cd0065 538 } else {
d9a16fc9 539 if (data_blocks > 0 && s->tx_dbc_interval > 0)
d9cd0065
TS
540 dbc_interval = s->tx_dbc_interval;
541 else
d9a16fc9 542 dbc_interval = data_blocks;
d9cd0065 543
c8bdf49b 544 lost = data_block_counter !=
d9cd0065
TS
545 ((s->data_block_counter + dbc_interval) & 0xff);
546 }
c8bdf49b
TS
547
548 if (lost) {
12e0f438
TS
549 dev_err(&s->unit->device,
550 "Detect discontinuity of CIP: %02X %02X\n",
551 s->data_block_counter, data_block_counter);
6fc6b9ce 552 return -EIO;
2b3fc456
TS
553 }
554
d9a16fc9
TS
555 syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
556 pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
2b3fc456 557
c8bdf49b
TS
558 if (s->flags & CIP_DBC_IS_END_EVENT)
559 s->data_block_counter = data_block_counter;
560 else
561 s->data_block_counter =
d9a16fc9 562 (data_block_counter + data_blocks) & 0xff;
2b3fc456
TS
563end:
564 if (queue_in_packet(s) < 0)
6fc6b9ce 565 return -EIO;
2b3fc456 566
20e44577
TS
567 pcm = ACCESS_ONCE(s->pcm);
568 if (pcm && pcm_frames > 0)
569 update_pcm_pointers(s, pcm, pcm_frames);
2b3fc456 570
31ea49ba 571 return 0;
2b3fc456
TS
572}
573
73fc7f08
TS
574/*
575 * In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
576 * the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
577 * it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
578 */
579static inline u32 compute_cycle_count(u32 tstamp)
580{
581 return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff);
582}
583
584static inline u32 increment_cycle_count(u32 cycle, unsigned int addend)
585{
586 cycle += addend;
587 if (cycle >= 8 * CYCLES_PER_SECOND)
588 cycle -= 8 * CYCLES_PER_SECOND;
589 return cycle;
590}
591
f90e2ded
TS
592static inline u32 decrement_cycle_count(u32 cycle, unsigned int subtrahend)
593{
594 if (cycle < subtrahend)
595 cycle += 8 * CYCLES_PER_SECOND;
596 return cycle - subtrahend;
597}
598
73fc7f08 599static void out_stream_callback(struct fw_iso_context *context, u32 tstamp,
4b7da117
TS
600 size_t header_length, void *header,
601 void *private_data)
31ef9134 602{
be4a2894 603 struct amdtp_stream *s = private_data;
390a1512 604 unsigned int i, packets = header_length / 4;
73fc7f08 605 u32 cycle;
31ef9134 606
a4103bd7
TS
607 if (s->packet_index < 0)
608 return;
609
73fc7f08
TS
610 cycle = compute_cycle_count(tstamp);
611
612 /* Align to actual cycle count for the last packet. */
613 cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
31ef9134 614
ccccad86 615 for (i = 0; i < packets; ++i) {
73fc7f08 616 cycle = increment_cycle_count(cycle, 1);
a9c4284b 617 if (handle_out_packet(s, cycle, i) < 0) {
a4103bd7
TS
618 s->packet_index = -1;
619 amdtp_stream_pcm_abort(s);
620 return;
621 }
ccccad86 622 }
a4103bd7 623
13882a82 624 fw_iso_context_queue_flush(s->context);
31ef9134
CL
625}
626
73fc7f08 627static void in_stream_callback(struct fw_iso_context *context, u32 tstamp,
2b3fc456
TS
628 size_t header_length, void *header,
629 void *private_data)
630{
631 struct amdtp_stream *s = private_data;
d9a16fc9 632 unsigned int i, packets;
a2064710 633 unsigned int payload_quadlets, max_payload_quadlets;
d9a16fc9 634 __be32 *headers = header;
f90e2ded 635 u32 cycle;
2b3fc456 636
a4103bd7
TS
637 if (s->packet_index < 0)
638 return;
639
2b3fc456
TS
640 /* The number of packets in buffer */
641 packets = header_length / IN_PACKET_HEADER_SIZE;
642
f90e2ded
TS
643 cycle = compute_cycle_count(tstamp);
644
645 /* Align to actual cycle count for the last packet. */
646 cycle = decrement_cycle_count(cycle, packets);
647
a2064710
TS
648 /* For buffer-over-run prevention. */
649 max_payload_quadlets = amdtp_stream_get_max_payload(s) / 4;
650
d9a16fc9 651 for (i = 0; i < packets; i++) {
f90e2ded 652 cycle = increment_cycle_count(cycle, 1);
2b3fc456
TS
653
654 /* The number of quadlets in this packet */
655 payload_quadlets =
d9a16fc9 656 (be32_to_cpu(headers[i]) >> ISO_DATA_LENGTH_SHIFT) / 4;
a2064710
TS
657 if (payload_quadlets > max_payload_quadlets) {
658 dev_err(&s->unit->device,
659 "Detect jumbo payload: %02x %02x\n",
660 payload_quadlets, max_payload_quadlets);
a2064710
TS
661 break;
662 }
663
a9c4284b 664 if (handle_in_packet(s, payload_quadlets, cycle, i) < 0)
6fc6b9ce 665 break;
2b3fc456
TS
666 }
667
dec63cc8 668 /* Queueing error or detecting invalid payload. */
d9a16fc9 669 if (i < packets) {
dec63cc8 670 s->packet_index = -1;
6fc6b9ce 671 amdtp_stream_pcm_abort(s);
7b3b0d85
TS
672 return;
673 }
674
2b3fc456
TS
675 fw_iso_context_queue_flush(s->context);
676}
677
7b3b0d85
TS
678/* this is executed one time */
679static void amdtp_stream_first_callback(struct fw_iso_context *context,
73fc7f08 680 u32 tstamp, size_t header_length,
7b3b0d85
TS
681 void *header, void *private_data)
682{
683 struct amdtp_stream *s = private_data;
a04513f8
TS
684 u32 cycle;
685 unsigned int packets;
7b3b0d85
TS
686
687 /*
688 * For in-stream, first packet has come.
689 * For out-stream, prepared to transmit first packet
690 */
691 s->callbacked = true;
692 wake_up(&s->callback_wait);
693
a04513f8
TS
694 cycle = compute_cycle_count(tstamp);
695
696 if (s->direction == AMDTP_IN_STREAM) {
697 packets = header_length / IN_PACKET_HEADER_SIZE;
698 cycle = decrement_cycle_count(cycle, packets);
7b3b0d85 699 context->callback.sc = in_stream_callback;
a04513f8
TS
700 } else {
701 packets = header_length / 4;
702 cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
7b3b0d85 703 context->callback.sc = out_stream_callback;
a04513f8
TS
704 }
705
706 s->start_cycle = cycle;
7b3b0d85 707
73fc7f08 708 context->callback.sc(context, tstamp, header_length, header, s);
7b3b0d85
TS
709}
710
31ef9134 711/**
be4a2894
TS
712 * amdtp_stream_start - start transferring packets
713 * @s: the AMDTP stream to start
31ef9134
CL
714 * @channel: the isochronous channel on the bus
715 * @speed: firewire speed code
716 *
717 * The stream cannot be started until it has been configured with
be4a2894
TS
718 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
719 * device can be started.
31ef9134 720 */
be4a2894 721int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
31ef9134
CL
722{
723 static const struct {
724 unsigned int data_block;
725 unsigned int syt_offset;
726 } initial_state[] = {
727 [CIP_SFC_32000] = { 4, 3072 },
728 [CIP_SFC_48000] = { 6, 1024 },
729 [CIP_SFC_96000] = { 12, 1024 },
730 [CIP_SFC_192000] = { 24, 1024 },
731 [CIP_SFC_44100] = { 0, 67 },
732 [CIP_SFC_88200] = { 0, 67 },
733 [CIP_SFC_176400] = { 0, 67 },
734 };
2b3fc456
TS
735 unsigned int header_size;
736 enum dma_data_direction dir;
7ab56645 737 int type, tag, err;
31ef9134
CL
738
739 mutex_lock(&s->mutex);
740
be4a2894 741 if (WARN_ON(amdtp_stream_running(s) ||
4b7da117 742 (s->data_block_quadlets < 1))) {
31ef9134
CL
743 err = -EBADFD;
744 goto err_unlock;
745 }
746
62f00e40 747 if (s->direction == AMDTP_IN_STREAM)
b6bc8123
TS
748 s->data_block_counter = UINT_MAX;
749 else
750 s->data_block_counter = 0;
31ef9134
CL
751 s->data_block_state = initial_state[s->sfc].data_block;
752 s->syt_offset_state = initial_state[s->sfc].syt_offset;
753 s->last_syt_offset = TICKS_PER_CYCLE;
754
2b3fc456
TS
755 /* initialize packet buffer */
756 if (s->direction == AMDTP_IN_STREAM) {
757 dir = DMA_FROM_DEVICE;
758 type = FW_ISO_CONTEXT_RECEIVE;
759 header_size = IN_PACKET_HEADER_SIZE;
2b3fc456
TS
760 } else {
761 dir = DMA_TO_DEVICE;
762 type = FW_ISO_CONTEXT_TRANSMIT;
763 header_size = OUT_PACKET_HEADER_SIZE;
2b3fc456 764 }
31ef9134 765 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
2b3fc456 766 amdtp_stream_get_max_payload(s), dir);
31ef9134
CL
767 if (err < 0)
768 goto err_unlock;
769
770 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
2b3fc456 771 type, channel, speed, header_size,
7b3b0d85 772 amdtp_stream_first_callback, s);
31ef9134
CL
773 if (IS_ERR(s->context)) {
774 err = PTR_ERR(s->context);
775 if (err == -EBUSY)
776 dev_err(&s->unit->device,
be4a2894 777 "no free stream on this controller\n");
31ef9134
CL
778 goto err_buffer;
779 }
780
be4a2894 781 amdtp_stream_update(s);
31ef9134 782
ec00f5e4 783 s->packet_index = 0;
4b7da117 784 do {
2b3fc456
TS
785 if (s->direction == AMDTP_IN_STREAM)
786 err = queue_in_packet(s);
787 else
ff38e0c7 788 err = queue_out_packet(s, 0);
4b7da117
TS
789 if (err < 0)
790 goto err_context;
791 } while (s->packet_index > 0);
31ef9134 792
2b3fc456 793 /* NOTE: TAG1 matches CIP. This just affects in stream. */
7ab56645
TS
794 tag = FW_ISO_CONTEXT_MATCH_TAG1;
795 if (s->flags & CIP_EMPTY_WITH_TAG0)
796 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
797
7b3b0d85 798 s->callbacked = false;
7ab56645 799 err = fw_iso_context_start(s->context, -1, 0, tag);
31ef9134
CL
800 if (err < 0)
801 goto err_context;
802
803 mutex_unlock(&s->mutex);
804
805 return 0;
806
807err_context:
808 fw_iso_context_destroy(s->context);
809 s->context = ERR_PTR(-1);
810err_buffer:
811 iso_packets_buffer_destroy(&s->buffer, s->unit);
812err_unlock:
813 mutex_unlock(&s->mutex);
814
815 return err;
816}
be4a2894 817EXPORT_SYMBOL(amdtp_stream_start);
31ef9134 818
e9148ddd 819/**
be4a2894
TS
820 * amdtp_stream_pcm_pointer - get the PCM buffer position
821 * @s: the AMDTP stream that transports the PCM data
e9148ddd
CL
822 *
823 * Returns the current buffer position, in frames.
824 */
be4a2894 825unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
e9148ddd 826{
1dba9db0
TS
827 /*
828 * This function is called in software IRQ context of period_tasklet or
829 * process context.
830 *
831 * When the software IRQ context was scheduled by software IRQ context
832 * of IR/IT contexts, queued packets were already handled. Therefore,
833 * no need to flush the queue in buffer anymore.
834 *
835 * When the process context reach here, some packets will be already
836 * queued in the buffer. These packets should be handled immediately
837 * to keep better granularity of PCM pointer.
838 *
839 * Later, the process context will sometimes schedules software IRQ
840 * context of the period_tasklet. Then, no need to flush the queue by
841 * the same reason as described for IR/IT contexts.
842 */
843 if (!in_interrupt() && amdtp_stream_running(s))
92b862c7 844 fw_iso_context_flush_completions(s->context);
e9148ddd
CL
845
846 return ACCESS_ONCE(s->pcm_buffer_pointer);
847}
be4a2894 848EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
e9148ddd 849
31ef9134 850/**
be4a2894
TS
851 * amdtp_stream_update - update the stream after a bus reset
852 * @s: the AMDTP stream
31ef9134 853 */
be4a2894 854void amdtp_stream_update(struct amdtp_stream *s)
31ef9134 855{
9a2820c1 856 /* Precomputing. */
31ef9134 857 ACCESS_ONCE(s->source_node_id_field) =
9a2820c1
TS
858 (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) &
859 CIP_SID_MASK;
31ef9134 860}
be4a2894 861EXPORT_SYMBOL(amdtp_stream_update);
31ef9134
CL
862
863/**
be4a2894
TS
864 * amdtp_stream_stop - stop sending packets
865 * @s: the AMDTP stream to stop
31ef9134
CL
866 *
867 * All PCM and MIDI devices of the stream must be stopped before the stream
868 * itself can be stopped.
869 */
be4a2894 870void amdtp_stream_stop(struct amdtp_stream *s)
31ef9134
CL
871{
872 mutex_lock(&s->mutex);
873
be4a2894 874 if (!amdtp_stream_running(s)) {
31ef9134
CL
875 mutex_unlock(&s->mutex);
876 return;
877 }
878
76fb8789 879 tasklet_kill(&s->period_tasklet);
31ef9134
CL
880 fw_iso_context_stop(s->context);
881 fw_iso_context_destroy(s->context);
882 s->context = ERR_PTR(-1);
883 iso_packets_buffer_destroy(&s->buffer, s->unit);
884
7b3b0d85
TS
885 s->callbacked = false;
886
31ef9134
CL
887 mutex_unlock(&s->mutex);
888}
be4a2894 889EXPORT_SYMBOL(amdtp_stream_stop);
31ef9134
CL
890
891/**
be4a2894 892 * amdtp_stream_pcm_abort - abort the running PCM device
31ef9134
CL
893 * @s: the AMDTP stream about to be stopped
894 *
895 * If the isochronous stream needs to be stopped asynchronously, call this
896 * function first to stop the PCM device.
897 */
be4a2894 898void amdtp_stream_pcm_abort(struct amdtp_stream *s)
31ef9134
CL
899{
900 struct snd_pcm_substream *pcm;
901
902 pcm = ACCESS_ONCE(s->pcm);
1fb8510c
TI
903 if (pcm)
904 snd_pcm_stop_xrun(pcm);
31ef9134 905}
be4a2894 906EXPORT_SYMBOL(amdtp_stream_pcm_abort);