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