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