Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
[linux-2.6-block.git] / sound / firewire / amdtp.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>
7b3b0d85 14#include <linux/sched.h>
31ef9134 15#include <sound/pcm.h>
7b2d99fa 16#include <sound/pcm_params.h>
83d8d72d 17#include <sound/rawmidi.h>
31ef9134
CL
18#include "amdtp.h"
19
20#define TICKS_PER_CYCLE 3072
21#define CYCLES_PER_SECOND 8000
22#define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
23
25ca917c
CL
24/*
25 * Nominally 3125 bytes/second, but the MIDI port's clock might be
26 * 1% too slow, and the bus clock 100 ppm too fast.
27 */
28#define MIDI_BYTES_PER_SECOND 3093
29
5c697e5b
CL
30/*
31 * Several devices look only at the first eight data blocks.
32 * In any case, this is more than enough for the MIDI data rate.
33 */
34#define MAX_MIDI_RX_BLOCKS 8
35
ca5b5050 36#define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */
31ef9134 37
b445db44
TS
38/* isochronous header parameters */
39#define ISO_DATA_LENGTH_SHIFT 16
31ef9134
CL
40#define TAG_CIP 1
41
b445db44 42/* common isochronous packet header parameters */
9a2820c1
TS
43#define CIP_EOH_SHIFT 31
44#define CIP_EOH (1u << CIP_EOH_SHIFT)
b445db44 45#define CIP_EOH_MASK 0x80000000
9a2820c1
TS
46#define CIP_SID_SHIFT 24
47#define CIP_SID_MASK 0x3f000000
48#define CIP_DBS_MASK 0x00ff0000
49#define CIP_DBS_SHIFT 16
50#define CIP_DBC_MASK 0x000000ff
51#define CIP_FMT_SHIFT 24
b445db44 52#define CIP_FMT_MASK 0x3f000000
9a2820c1
TS
53#define CIP_FDF_MASK 0x00ff0000
54#define CIP_FDF_SHIFT 16
b445db44
TS
55#define CIP_SYT_MASK 0x0000ffff
56#define CIP_SYT_NO_INFO 0xffff
b445db44
TS
57
58/*
59 * Audio and Music transfer protocol specific parameters
60 * only "Clock-based rate control mode" is supported
61 */
9a2820c1
TS
62#define CIP_FMT_AM (0x10 << CIP_FMT_SHIFT)
63#define AMDTP_FDF_AM824 (0 << (CIP_FDF_SHIFT + 3))
2b3fc456 64#define AMDTP_FDF_NO_DATA 0xff
31ef9134
CL
65
66/* TODO: make these configurable */
67#define INTERRUPT_INTERVAL 16
68#define QUEUE_LENGTH 48
69
2b3fc456 70#define IN_PACKET_HEADER_SIZE 4
4b7da117
TS
71#define OUT_PACKET_HEADER_SIZE 0
72
76fb8789
CL
73static void pcm_period_tasklet(unsigned long data);
74
31ef9134 75/**
be4a2894
TS
76 * amdtp_stream_init - initialize an AMDTP stream structure
77 * @s: the AMDTP stream to initialize
31ef9134 78 * @unit: the target of the stream
3ff7e8f0 79 * @dir: the direction of stream
31ef9134
CL
80 * @flags: the packet transmission method to use
81 */
be4a2894 82int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
3ff7e8f0 83 enum amdtp_stream_direction dir, enum cip_flags flags)
31ef9134 84{
c6f224dc 85 s->unit = unit;
3ff7e8f0 86 s->direction = dir;
31ef9134
CL
87 s->flags = flags;
88 s->context = ERR_PTR(-1);
89 mutex_init(&s->mutex);
76fb8789 90 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
ec00f5e4 91 s->packet_index = 0;
31ef9134 92
7b3b0d85
TS
93 init_waitqueue_head(&s->callback_wait);
94 s->callbacked = false;
95 s->sync_slave = NULL;
96
31ef9134
CL
97 return 0;
98}
be4a2894 99EXPORT_SYMBOL(amdtp_stream_init);
31ef9134
CL
100
101/**
be4a2894
TS
102 * amdtp_stream_destroy - free stream resources
103 * @s: the AMDTP stream to destroy
31ef9134 104 */
be4a2894 105void amdtp_stream_destroy(struct amdtp_stream *s)
31ef9134 106{
be4a2894 107 WARN_ON(amdtp_stream_running(s));
31ef9134 108 mutex_destroy(&s->mutex);
31ef9134 109}
be4a2894 110EXPORT_SYMBOL(amdtp_stream_destroy);
31ef9134 111
c5280e99 112const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
a7304e3b
CL
113 [CIP_SFC_32000] = 8,
114 [CIP_SFC_44100] = 8,
115 [CIP_SFC_48000] = 8,
116 [CIP_SFC_88200] = 16,
117 [CIP_SFC_96000] = 16,
118 [CIP_SFC_176400] = 32,
119 [CIP_SFC_192000] = 32,
120};
121EXPORT_SYMBOL(amdtp_syt_intervals);
122
f9503a68 123const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
1017abed
TS
124 [CIP_SFC_32000] = 32000,
125 [CIP_SFC_44100] = 44100,
126 [CIP_SFC_48000] = 48000,
127 [CIP_SFC_88200] = 88200,
128 [CIP_SFC_96000] = 96000,
129 [CIP_SFC_176400] = 176400,
130 [CIP_SFC_192000] = 192000,
131};
132EXPORT_SYMBOL(amdtp_rate_table);
133
7b2d99fa
TS
134/**
135 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
136 * @s: the AMDTP stream, which must be initialized.
137 * @runtime: the PCM substream runtime
138 */
139int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
140 struct snd_pcm_runtime *runtime)
141{
142 int err;
143
144 /* AM824 in IEC 61883-6 can deliver 24bit data */
145 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
146 if (err < 0)
147 goto end;
148
149 /*
150 * Currently firewire-lib processes 16 packets in one software
151 * interrupt callback. This equals to 2msec but actually the
152 * interval of the interrupts has a jitter.
153 * Additionally, even if adding a constraint to fit period size to
154 * 2msec, actual calculated frames per period doesn't equal to 2msec,
155 * depending on sampling rate.
156 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
157 * Here let us use 5msec for safe period interrupt.
158 */
159 err = snd_pcm_hw_constraint_minmax(runtime,
160 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
161 5000, UINT_MAX);
162 if (err < 0)
163 goto end;
164
165 /* Non-Blocking stream has no more constraints */
166 if (!(s->flags & CIP_BLOCKING))
167 goto end;
168
169 /*
170 * One AMDTP packet can include some frames. In blocking mode, the
171 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
172 * depending on its sampling rate. For accurate period interrupt, it's
ce991981 173 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
7b2d99fa 174 *
ce991981
YG
175 * TODO: These constraints can be improved with proper rules.
176 * Currently apply LCM of SYT_INTERVALs.
7b2d99fa
TS
177 */
178 err = snd_pcm_hw_constraint_step(runtime, 0,
179 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
180 if (err < 0)
181 goto end;
182 err = snd_pcm_hw_constraint_step(runtime, 0,
183 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
184end:
185 return err;
186}
187EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
188
31ef9134 189/**
be4a2894
TS
190 * amdtp_stream_set_parameters - set stream parameters
191 * @s: the AMDTP stream to configure
31ef9134 192 * @rate: the sample rate
a7304e3b
CL
193 * @pcm_channels: the number of PCM samples in each data block, to be encoded
194 * as AM824 multi-bit linear audio
195 * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
31ef9134 196 *
a7304e3b 197 * The parameters must be set before the stream is started, and must not be
31ef9134
CL
198 * changed while the stream is running.
199 */
be4a2894
TS
200void amdtp_stream_set_parameters(struct amdtp_stream *s,
201 unsigned int rate,
202 unsigned int pcm_channels,
203 unsigned int midi_ports)
31ef9134 204{
77d2a8a4 205 unsigned int i, sfc, midi_channels;
31ef9134 206
83d8d72d
TS
207 midi_channels = DIV_ROUND_UP(midi_ports, 8);
208
77d2a8a4
TS
209 if (WARN_ON(amdtp_stream_running(s)) |
210 WARN_ON(pcm_channels > AMDTP_MAX_CHANNELS_FOR_PCM) |
83d8d72d 211 WARN_ON(midi_channels > AMDTP_MAX_CHANNELS_FOR_MIDI))
31ef9134
CL
212 return;
213
f9503a68 214 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc)
1017abed 215 if (amdtp_rate_table[sfc] == rate)
e84d15f6 216 goto sfc_found;
31ef9134 217 WARN_ON(1);
e84d15f6
CL
218 return;
219
220sfc_found:
10550bea 221 s->pcm_channels = pcm_channels;
e84d15f6 222 s->sfc = sfc;
77d2a8a4 223 s->data_block_quadlets = s->pcm_channels + midi_channels;
a7304e3b
CL
224 s->midi_ports = midi_ports;
225
226 s->syt_interval = amdtp_syt_intervals[sfc];
e84d15f6
CL
227
228 /* default buffering in the device */
229 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
230 if (s->flags & CIP_BLOCKING)
231 /* additional buffering needed to adjust for no-data packets */
232 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
77d2a8a4
TS
233
234 /* init the position map for PCM and MIDI channels */
235 for (i = 0; i < pcm_channels; i++)
236 s->pcm_positions[i] = i;
237 s->midi_position = s->pcm_channels;
25ca917c
CL
238
239 /*
240 * We do not know the actual MIDI FIFO size of most devices. Just
241 * assume two bytes, i.e., one byte can be received over the bus while
242 * the previous one is transmitted over MIDI.
243 * (The value here is adjusted for midi_ratelimit_per_packet().)
244 */
245 s->midi_fifo_limit = rate - MIDI_BYTES_PER_SECOND * s->syt_interval + 1;
31ef9134 246}
be4a2894 247EXPORT_SYMBOL(amdtp_stream_set_parameters);
31ef9134
CL
248
249/**
be4a2894
TS
250 * amdtp_stream_get_max_payload - get the stream's packet size
251 * @s: the AMDTP stream
31ef9134
CL
252 *
253 * This function must not be called before the stream has been configured
be4a2894 254 * with amdtp_stream_set_parameters().
31ef9134 255 */
be4a2894 256unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
31ef9134 257{
a2064710
TS
258 unsigned int multiplier = 1;
259
260 if (s->flags & CIP_JUMBO_PAYLOAD)
261 multiplier = 5;
262
263 return 8 + s->syt_interval * s->data_block_quadlets * 4 * multiplier;
31ef9134 264}
be4a2894 265EXPORT_SYMBOL(amdtp_stream_get_max_payload);
31ef9134 266
29bcae20
TS
267static void write_pcm_s16(struct amdtp_stream *s,
268 struct snd_pcm_substream *pcm,
269 __be32 *buffer, unsigned int frames);
270static void write_pcm_s32(struct amdtp_stream *s,
271 struct snd_pcm_substream *pcm,
272 __be32 *buffer, unsigned int frames);
273static void read_pcm_s32(struct amdtp_stream *s,
274 struct snd_pcm_substream *pcm,
275 __be32 *buffer, unsigned int frames);
31ef9134
CL
276
277/**
be4a2894
TS
278 * amdtp_stream_set_pcm_format - set the PCM format
279 * @s: the AMDTP stream to configure
31ef9134
CL
280 * @format: the format of the ALSA PCM device
281 *
ce991981 282 * The sample format must be set after the other parameters (rate/PCM channels/
a7304e3b
CL
283 * MIDI) and before the stream is started, and must not be changed while the
284 * stream is running.
31ef9134 285 */
be4a2894
TS
286void amdtp_stream_set_pcm_format(struct amdtp_stream *s,
287 snd_pcm_format_t format)
31ef9134 288{
83d8d72d 289 if (WARN_ON(amdtp_stream_pcm_running(s)))
31ef9134
CL
290 return;
291
292 switch (format) {
293 default:
294 WARN_ON(1);
295 /* fall through */
296 case SNDRV_PCM_FORMAT_S16:
2b3fc456 297 if (s->direction == AMDTP_OUT_STREAM) {
29bcae20 298 s->transfer_samples = write_pcm_s16;
2b3fc456
TS
299 break;
300 }
301 WARN_ON(1);
302 /* fall through */
31ef9134 303 case SNDRV_PCM_FORMAT_S32:
10550bea 304 if (s->direction == AMDTP_OUT_STREAM)
29bcae20 305 s->transfer_samples = write_pcm_s32;
10550bea 306 else
29bcae20 307 s->transfer_samples = read_pcm_s32;
31ef9134
CL
308 break;
309 }
310}
be4a2894 311EXPORT_SYMBOL(amdtp_stream_set_pcm_format);
31ef9134 312
76fb8789 313/**
be4a2894
TS
314 * amdtp_stream_pcm_prepare - prepare PCM device for running
315 * @s: the AMDTP stream
76fb8789
CL
316 *
317 * This function should be called from the PCM device's .prepare callback.
318 */
be4a2894 319void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
76fb8789
CL
320{
321 tasklet_kill(&s->period_tasklet);
322 s->pcm_buffer_pointer = 0;
323 s->pcm_period_pointer = 0;
92b862c7 324 s->pointer_flush = true;
76fb8789 325}
be4a2894 326EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
76fb8789 327
875be091
TS
328static unsigned int calculate_data_blocks(struct amdtp_stream *s,
329 unsigned int syt)
31ef9134
CL
330{
331 unsigned int phase, data_blocks;
332
875be091
TS
333 /* Blocking mode. */
334 if (s->flags & CIP_BLOCKING) {
335 /* This module generate empty packet for 'no data'. */
336 if (syt == CIP_SYT_NO_INFO)
337 data_blocks = 0;
338 else
339 data_blocks = s->syt_interval;
340 /* Non-blocking mode. */
31ef9134 341 } else {
875be091
TS
342 if (!cip_sfc_is_base_44100(s->sfc)) {
343 /* Sample_rate / 8000 is an integer, and precomputed. */
344 data_blocks = s->data_block_state;
345 } else {
346 phase = s->data_block_state;
31ef9134
CL
347
348 /*
349 * This calculates the number of data blocks per packet so that
350 * 1) the overall rate is correct and exactly synchronized to
351 * the bus clock, and
352 * 2) packets with a rounded-up number of blocks occur as early
353 * as possible in the sequence (to prevent underruns of the
354 * device's buffer).
355 */
875be091
TS
356 if (s->sfc == CIP_SFC_44100)
357 /* 6 6 5 6 5 6 5 ... */
358 data_blocks = 5 + ((phase & 1) ^
359 (phase == 0 || phase >= 40));
360 else
361 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
362 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
363 if (++phase >= (80 >> (s->sfc >> 1)))
364 phase = 0;
365 s->data_block_state = phase;
366 }
31ef9134
CL
367 }
368
369 return data_blocks;
370}
371
be4a2894 372static unsigned int calculate_syt(struct amdtp_stream *s,
31ef9134
CL
373 unsigned int cycle)
374{
375 unsigned int syt_offset, phase, index, syt;
376
377 if (s->last_syt_offset < TICKS_PER_CYCLE) {
378 if (!cip_sfc_is_base_44100(s->sfc))
379 syt_offset = s->last_syt_offset + s->syt_offset_state;
380 else {
381 /*
382 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
383 * n * SYT_INTERVAL * 24576000 / sample_rate
384 * Modulo TICKS_PER_CYCLE, the difference between successive
385 * elements is about 1386.23. Rounding the results of this
386 * formula to the SYT precision results in a sequence of
387 * differences that begins with:
388 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
389 * This code generates _exactly_ the same sequence.
390 */
391 phase = s->syt_offset_state;
392 index = phase % 13;
393 syt_offset = s->last_syt_offset;
394 syt_offset += 1386 + ((index && !(index & 3)) ||
395 phase == 146);
396 if (++phase >= 147)
397 phase = 0;
398 s->syt_offset_state = phase;
399 }
400 } else
401 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
402 s->last_syt_offset = syt_offset;
403
be454366 404 if (syt_offset < TICKS_PER_CYCLE) {
e84d15f6 405 syt_offset += s->transfer_delay;
be454366
CL
406 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
407 syt += syt_offset % TICKS_PER_CYCLE;
31ef9134 408
b445db44 409 return syt & CIP_SYT_MASK;
be454366 410 } else {
b445db44 411 return CIP_SYT_NO_INFO;
be454366 412 }
31ef9134
CL
413}
414
29bcae20
TS
415static void write_pcm_s32(struct amdtp_stream *s,
416 struct snd_pcm_substream *pcm,
417 __be32 *buffer, unsigned int frames)
31ef9134
CL
418{
419 struct snd_pcm_runtime *runtime = pcm->runtime;
77d2a8a4 420 unsigned int channels, remaining_frames, i, c;
31ef9134
CL
421 const u32 *src;
422
423 channels = s->pcm_channels;
424 src = (void *)runtime->dma_area +
e84841f9 425 frames_to_bytes(runtime, s->pcm_buffer_pointer);
31ef9134 426 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
31ef9134
CL
427
428 for (i = 0; i < frames; ++i) {
429 for (c = 0; c < channels; ++c) {
77d2a8a4
TS
430 buffer[s->pcm_positions[c]] =
431 cpu_to_be32((*src >> 8) | 0x40000000);
31ef9134 432 src++;
31ef9134 433 }
77d2a8a4 434 buffer += s->data_block_quadlets;
31ef9134
CL
435 if (--remaining_frames == 0)
436 src = (void *)runtime->dma_area;
437 }
438}
439
29bcae20
TS
440static void write_pcm_s16(struct amdtp_stream *s,
441 struct snd_pcm_substream *pcm,
442 __be32 *buffer, unsigned int frames)
31ef9134
CL
443{
444 struct snd_pcm_runtime *runtime = pcm->runtime;
77d2a8a4 445 unsigned int channels, remaining_frames, i, c;
31ef9134
CL
446 const u16 *src;
447
448 channels = s->pcm_channels;
449 src = (void *)runtime->dma_area +
e84841f9 450 frames_to_bytes(runtime, s->pcm_buffer_pointer);
31ef9134 451 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
31ef9134
CL
452
453 for (i = 0; i < frames; ++i) {
454 for (c = 0; c < channels; ++c) {
77d2a8a4 455 buffer[s->pcm_positions[c]] =
a6975f2a 456 cpu_to_be32((*src << 8) | 0x42000000);
31ef9134 457 src++;
31ef9134 458 }
77d2a8a4 459 buffer += s->data_block_quadlets;
31ef9134
CL
460 if (--remaining_frames == 0)
461 src = (void *)runtime->dma_area;
462 }
463}
464
29bcae20
TS
465static void read_pcm_s32(struct amdtp_stream *s,
466 struct snd_pcm_substream *pcm,
467 __be32 *buffer, unsigned int frames)
2b3fc456
TS
468{
469 struct snd_pcm_runtime *runtime = pcm->runtime;
470 unsigned int channels, remaining_frames, i, c;
471 u32 *dst;
472
473 channels = s->pcm_channels;
474 dst = (void *)runtime->dma_area +
475 frames_to_bytes(runtime, s->pcm_buffer_pointer);
476 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
477
478 for (i = 0; i < frames; ++i) {
479 for (c = 0; c < channels; ++c) {
77d2a8a4 480 *dst = be32_to_cpu(buffer[s->pcm_positions[c]]) << 8;
2b3fc456
TS
481 dst++;
482 }
483 buffer += s->data_block_quadlets;
484 if (--remaining_frames == 0)
485 dst = (void *)runtime->dma_area;
486 }
487}
488
29bcae20
TS
489static void write_pcm_silence(struct amdtp_stream *s,
490 __be32 *buffer, unsigned int frames)
31ef9134
CL
491{
492 unsigned int i, c;
493
494 for (i = 0; i < frames; ++i) {
495 for (c = 0; c < s->pcm_channels; ++c)
77d2a8a4 496 buffer[s->pcm_positions[c]] = cpu_to_be32(0x40000000);
31ef9134
CL
497 buffer += s->data_block_quadlets;
498 }
499}
500
25ca917c
CL
501/*
502 * To avoid sending MIDI bytes at too high a rate, assume that the receiving
503 * device has a FIFO, and track how much it is filled. This values increases
504 * by one whenever we send one byte in a packet, but the FIFO empties at
505 * a constant rate independent of our packet rate. One packet has syt_interval
506 * samples, so the number of bytes that empty out of the FIFO, per packet(!),
507 * is MIDI_BYTES_PER_SECOND * syt_interval / sample_rate. To avoid storing
508 * fractional values, the values in midi_fifo_used[] are measured in bytes
509 * multiplied by the sample rate.
510 */
511static bool midi_ratelimit_per_packet(struct amdtp_stream *s, unsigned int port)
512{
513 int used;
514
515 used = s->midi_fifo_used[port];
516 if (used == 0) /* common shortcut */
517 return true;
518
519 used -= MIDI_BYTES_PER_SECOND * s->syt_interval;
520 used = max(used, 0);
521 s->midi_fifo_used[port] = used;
522
523 return used < s->midi_fifo_limit;
524}
525
526static void midi_rate_use_one_byte(struct amdtp_stream *s, unsigned int port)
527{
528 s->midi_fifo_used[port] += amdtp_rate_table[s->sfc];
529}
530
29bcae20
TS
531static void write_midi_messages(struct amdtp_stream *s,
532 __be32 *buffer, unsigned int frames)
31ef9134 533{
83d8d72d
TS
534 unsigned int f, port;
535 u8 *b;
536
537 for (f = 0; f < frames; f++) {
77d2a8a4 538 b = (u8 *)&buffer[s->midi_position];
83d8d72d
TS
539
540 port = (s->data_block_counter + f) % 8;
25ca917c
CL
541 if (f < MAX_MIDI_RX_BLOCKS &&
542 midi_ratelimit_per_packet(s, port) &&
543 s->midi[port] != NULL &&
544 snd_rawmidi_transmit(s->midi[port], &b[1], 1) == 1) {
545 midi_rate_use_one_byte(s, port);
83d8d72d 546 b[0] = 0x81;
25ca917c
CL
547 } else {
548 b[0] = 0x80;
549 b[1] = 0;
550 }
551 b[2] = 0;
552 b[3] = 0;
83d8d72d
TS
553
554 buffer += s->data_block_quadlets;
555 }
556}
557
29bcae20
TS
558static void read_midi_messages(struct amdtp_stream *s,
559 __be32 *buffer, unsigned int frames)
83d8d72d
TS
560{
561 unsigned int f, port;
562 int len;
563 u8 *b;
564
565 for (f = 0; f < frames; f++) {
566 port = (s->data_block_counter + f) % 8;
77d2a8a4 567 b = (u8 *)&buffer[s->midi_position];
31ef9134 568
83d8d72d
TS
569 len = b[0] - 0x80;
570 if ((1 <= len) && (len <= 3) && (s->midi[port]))
571 snd_rawmidi_receive(s->midi[port], b + 1, len);
572
573 buffer += s->data_block_quadlets;
574 }
31ef9134
CL
575}
576
4b7da117
TS
577static void update_pcm_pointers(struct amdtp_stream *s,
578 struct snd_pcm_substream *pcm,
579 unsigned int frames)
65845f29
TS
580{
581 unsigned int ptr;
582
583 /*
584 * In IEC 61883-6, one data block represents one event. In ALSA, one
585 * event equals to one PCM frame. But Dice has a quirk to transfer
586 * two PCM frames in one data block.
587 */
588 if (s->double_pcm_frames)
589 frames *= 2;
4b7da117 590
4b7da117
TS
591 ptr = s->pcm_buffer_pointer + frames;
592 if (ptr >= pcm->runtime->buffer_size)
593 ptr -= pcm->runtime->buffer_size;
594 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
595
596 s->pcm_period_pointer += frames;
597 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
598 s->pcm_period_pointer -= pcm->runtime->period_size;
599 s->pointer_flush = false;
600 tasklet_hi_schedule(&s->period_tasklet);
601 }
602}
603
604static void pcm_period_tasklet(unsigned long data)
605{
606 struct amdtp_stream *s = (void *)data;
607 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
608
609 if (pcm)
610 snd_pcm_period_elapsed(pcm);
611}
612
613static int queue_packet(struct amdtp_stream *s,
614 unsigned int header_length,
615 unsigned int payload_length, bool skip)
616{
617 struct fw_iso_packet p = {0};
7b3b0d85
TS
618 int err = 0;
619
620 if (IS_ERR(s->context))
621 goto end;
4b7da117
TS
622
623 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
624 p.tag = TAG_CIP;
625 p.header_length = header_length;
626 p.payload_length = (!skip) ? payload_length : 0;
627 p.skip = skip;
628 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
629 s->buffer.packets[s->packet_index].offset);
630 if (err < 0) {
631 dev_err(&s->unit->device, "queueing error: %d\n", err);
632 goto end;
633 }
634
635 if (++s->packet_index >= QUEUE_LENGTH)
636 s->packet_index = 0;
637end:
638 return err;
639}
640
641static inline int queue_out_packet(struct amdtp_stream *s,
642 unsigned int payload_length, bool skip)
643{
644 return queue_packet(s, OUT_PACKET_HEADER_SIZE,
645 payload_length, skip);
646}
647
2b3fc456
TS
648static inline int queue_in_packet(struct amdtp_stream *s)
649{
650 return queue_packet(s, IN_PACKET_HEADER_SIZE,
651 amdtp_stream_get_max_payload(s), false);
652}
653
a4103bd7
TS
654static int handle_out_packet(struct amdtp_stream *s, unsigned int data_blocks,
655 unsigned int syt)
31ef9134
CL
656{
657 __be32 *buffer;
6fc6b9ce 658 unsigned int payload_length;
31ef9134 659 struct snd_pcm_substream *pcm;
31ef9134 660
ccccad86 661 buffer = s->buffer.packets[s->packet_index].buffer;
31ef9134 662 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
9a2820c1 663 (s->data_block_quadlets << CIP_DBS_SHIFT) |
31ef9134
CL
664 s->data_block_counter);
665 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
9a2820c1 666 (s->sfc << CIP_FDF_SHIFT) | syt);
31ef9134
CL
667 buffer += 2;
668
669 pcm = ACCESS_ONCE(s->pcm);
670 if (pcm)
671 s->transfer_samples(s, pcm, buffer, data_blocks);
672 else
29bcae20 673 write_pcm_silence(s, buffer, data_blocks);
31ef9134 674 if (s->midi_ports)
29bcae20 675 write_midi_messages(s, buffer, data_blocks);
31ef9134
CL
676
677 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
678
4b7da117 679 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
a4103bd7
TS
680 if (queue_out_packet(s, payload_length, false) < 0)
681 return -EIO;
31ef9134 682
76fb8789 683 if (pcm)
4b7da117 684 update_pcm_pointers(s, pcm, data_blocks);
a4103bd7
TS
685
686 /* No need to return the number of handled data blocks. */
687 return 0;
76fb8789
CL
688}
689
6fc6b9ce 690static int handle_in_packet(struct amdtp_stream *s,
31ea49ba
TS
691 unsigned int payload_quadlets, __be32 *buffer,
692 unsigned int *data_blocks)
2b3fc456
TS
693{
694 u32 cip_header[2];
6fc6b9ce 695 unsigned int data_block_quadlets, data_block_counter, dbc_interval;
2b3fc456 696 struct snd_pcm_substream *pcm = NULL;
c8bdf49b 697 bool lost;
2b3fc456
TS
698
699 cip_header[0] = be32_to_cpu(buffer[0]);
700 cip_header[1] = be32_to_cpu(buffer[1]);
701
702 /*
703 * This module supports 'Two-quadlet CIP header with SYT field'.
77d2a8a4 704 * For convenience, also check FMT field is AM824 or not.
2b3fc456
TS
705 */
706 if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
707 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH) ||
708 ((cip_header[1] & CIP_FMT_MASK) != CIP_FMT_AM)) {
709 dev_info_ratelimited(&s->unit->device,
710 "Invalid CIP header for AMDTP: %08X:%08X\n",
711 cip_header[0], cip_header[1]);
31ea49ba 712 *data_blocks = 0;
2b3fc456
TS
713 goto end;
714 }
715
716 /* Calculate data blocks */
717 if (payload_quadlets < 3 ||
718 ((cip_header[1] & CIP_FDF_MASK) ==
9a2820c1 719 (AMDTP_FDF_NO_DATA << CIP_FDF_SHIFT))) {
31ea49ba 720 *data_blocks = 0;
2b3fc456
TS
721 } else {
722 data_block_quadlets =
9a2820c1 723 (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
2b3fc456
TS
724 /* avoid division by zero */
725 if (data_block_quadlets == 0) {
12e0f438 726 dev_err(&s->unit->device,
2b3fc456
TS
727 "Detect invalid value in dbs field: %08X\n",
728 cip_header[0]);
a9007054 729 return -EPROTO;
2b3fc456 730 }
69702239
TS
731 if (s->flags & CIP_WRONG_DBS)
732 data_block_quadlets = s->data_block_quadlets;
2b3fc456 733
31ea49ba 734 *data_blocks = (payload_quadlets - 2) / data_block_quadlets;
2b3fc456
TS
735 }
736
737 /* Check data block counter continuity */
9a2820c1 738 data_block_counter = cip_header[0] & CIP_DBC_MASK;
31ea49ba 739 if (*data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
9d59124c
TS
740 s->data_block_counter != UINT_MAX)
741 data_block_counter = s->data_block_counter;
742
18f5ed36
TS
743 if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) &&
744 data_block_counter == s->tx_first_dbc) ||
745 s->data_block_counter == UINT_MAX) {
b84b1a27
TS
746 lost = false;
747 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
c8bdf49b 748 lost = data_block_counter != s->data_block_counter;
d9cd0065 749 } else {
31ea49ba 750 if ((*data_blocks > 0) && (s->tx_dbc_interval > 0))
d9cd0065
TS
751 dbc_interval = s->tx_dbc_interval;
752 else
31ea49ba 753 dbc_interval = *data_blocks;
d9cd0065 754
c8bdf49b 755 lost = data_block_counter !=
d9cd0065
TS
756 ((s->data_block_counter + dbc_interval) & 0xff);
757 }
c8bdf49b
TS
758
759 if (lost) {
12e0f438
TS
760 dev_err(&s->unit->device,
761 "Detect discontinuity of CIP: %02X %02X\n",
762 s->data_block_counter, data_block_counter);
6fc6b9ce 763 return -EIO;
2b3fc456
TS
764 }
765
31ea49ba 766 if (*data_blocks > 0) {
2b3fc456
TS
767 buffer += 2;
768
769 pcm = ACCESS_ONCE(s->pcm);
770 if (pcm)
31ea49ba 771 s->transfer_samples(s, pcm, buffer, *data_blocks);
83d8d72d
TS
772
773 if (s->midi_ports)
31ea49ba 774 read_midi_messages(s, buffer, *data_blocks);
2b3fc456
TS
775 }
776
c8bdf49b
TS
777 if (s->flags & CIP_DBC_IS_END_EVENT)
778 s->data_block_counter = data_block_counter;
779 else
780 s->data_block_counter =
31ea49ba 781 (data_block_counter + *data_blocks) & 0xff;
2b3fc456
TS
782end:
783 if (queue_in_packet(s) < 0)
6fc6b9ce 784 return -EIO;
2b3fc456
TS
785
786 if (pcm)
31ea49ba 787 update_pcm_pointers(s, pcm, *data_blocks);
2b3fc456 788
31ea49ba 789 return 0;
2b3fc456
TS
790}
791
4b7da117
TS
792static void out_stream_callback(struct fw_iso_context *context, u32 cycle,
793 size_t header_length, void *header,
794 void *private_data)
31ef9134 795{
be4a2894 796 struct amdtp_stream *s = private_data;
ccccad86 797 unsigned int i, syt, packets = header_length / 4;
6fc6b9ce 798 unsigned int data_blocks;
31ef9134 799
a4103bd7
TS
800 if (s->packet_index < 0)
801 return;
802
31ef9134
CL
803 /*
804 * Compute the cycle of the last queued packet.
805 * (We need only the four lowest bits for the SYT, so we can ignore
806 * that bits 0-11 must wrap around at 3072.)
807 */
808 cycle += QUEUE_LENGTH - packets;
809
ccccad86
TS
810 for (i = 0; i < packets; ++i) {
811 syt = calculate_syt(s, ++cycle);
6fc6b9ce
TS
812 data_blocks = calculate_data_blocks(s, syt);
813
a4103bd7
TS
814 if (handle_out_packet(s, data_blocks, syt) < 0) {
815 s->packet_index = -1;
816 amdtp_stream_pcm_abort(s);
817 return;
818 }
ccccad86 819 }
a4103bd7 820
13882a82 821 fw_iso_context_queue_flush(s->context);
31ef9134
CL
822}
823
2b3fc456
TS
824static void in_stream_callback(struct fw_iso_context *context, u32 cycle,
825 size_t header_length, void *header,
826 void *private_data)
827{
828 struct amdtp_stream *s = private_data;
a2064710
TS
829 unsigned int p, syt, packets;
830 unsigned int payload_quadlets, max_payload_quadlets;
6fc6b9ce 831 unsigned int data_blocks;
2b3fc456
TS
832 __be32 *buffer, *headers = header;
833
a4103bd7
TS
834 if (s->packet_index < 0)
835 return;
836
2b3fc456
TS
837 /* The number of packets in buffer */
838 packets = header_length / IN_PACKET_HEADER_SIZE;
839
a2064710
TS
840 /* For buffer-over-run prevention. */
841 max_payload_quadlets = amdtp_stream_get_max_payload(s) / 4;
842
2b3fc456 843 for (p = 0; p < packets; p++) {
2b3fc456
TS
844 buffer = s->buffer.packets[s->packet_index].buffer;
845
846 /* The number of quadlets in this packet */
847 payload_quadlets =
848 (be32_to_cpu(headers[p]) >> ISO_DATA_LENGTH_SHIFT) / 4;
a2064710
TS
849 if (payload_quadlets > max_payload_quadlets) {
850 dev_err(&s->unit->device,
851 "Detect jumbo payload: %02x %02x\n",
852 payload_quadlets, max_payload_quadlets);
853 s->packet_index = -1;
854 break;
855 }
856
31ea49ba
TS
857 if (handle_in_packet(s, payload_quadlets, buffer,
858 &data_blocks) < 0) {
6fc6b9ce
TS
859 s->packet_index = -1;
860 break;
861 }
862
863 /* Process sync slave stream */
864 if (s->sync_slave && s->sync_slave->callbacked) {
865 syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
a4103bd7
TS
866 if (handle_out_packet(s->sync_slave,
867 data_blocks, syt) < 0) {
868 s->packet_index = -1;
869 break;
870 }
6fc6b9ce 871 }
2b3fc456
TS
872 }
873
7b3b0d85
TS
874 /* Queueing error or detecting discontinuity */
875 if (s->packet_index < 0) {
6fc6b9ce
TS
876 amdtp_stream_pcm_abort(s);
877
7b3b0d85
TS
878 /* Abort sync slave. */
879 if (s->sync_slave) {
880 s->sync_slave->packet_index = -1;
881 amdtp_stream_pcm_abort(s->sync_slave);
882 }
883 return;
884 }
885
886 /* when sync to device, flush the packets for slave stream */
887 if (s->sync_slave && s->sync_slave->callbacked)
888 fw_iso_context_queue_flush(s->sync_slave->context);
889
2b3fc456
TS
890 fw_iso_context_queue_flush(s->context);
891}
892
7b3b0d85
TS
893/* processing is done by master callback */
894static void slave_stream_callback(struct fw_iso_context *context, u32 cycle,
895 size_t header_length, void *header,
896 void *private_data)
897{
898 return;
899}
900
901/* this is executed one time */
902static void amdtp_stream_first_callback(struct fw_iso_context *context,
903 u32 cycle, size_t header_length,
904 void *header, void *private_data)
905{
906 struct amdtp_stream *s = private_data;
907
908 /*
909 * For in-stream, first packet has come.
910 * For out-stream, prepared to transmit first packet
911 */
912 s->callbacked = true;
913 wake_up(&s->callback_wait);
914
915 if (s->direction == AMDTP_IN_STREAM)
916 context->callback.sc = in_stream_callback;
727d3a0b 917 else if (s->flags & CIP_SYNC_TO_DEVICE)
7b3b0d85
TS
918 context->callback.sc = slave_stream_callback;
919 else
920 context->callback.sc = out_stream_callback;
921
922 context->callback.sc(context, cycle, header_length, header, s);
923}
924
31ef9134 925/**
be4a2894
TS
926 * amdtp_stream_start - start transferring packets
927 * @s: the AMDTP stream to start
31ef9134
CL
928 * @channel: the isochronous channel on the bus
929 * @speed: firewire speed code
930 *
931 * The stream cannot be started until it has been configured with
be4a2894
TS
932 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
933 * device can be started.
31ef9134 934 */
be4a2894 935int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
31ef9134
CL
936{
937 static const struct {
938 unsigned int data_block;
939 unsigned int syt_offset;
940 } initial_state[] = {
941 [CIP_SFC_32000] = { 4, 3072 },
942 [CIP_SFC_48000] = { 6, 1024 },
943 [CIP_SFC_96000] = { 12, 1024 },
944 [CIP_SFC_192000] = { 24, 1024 },
945 [CIP_SFC_44100] = { 0, 67 },
946 [CIP_SFC_88200] = { 0, 67 },
947 [CIP_SFC_176400] = { 0, 67 },
948 };
2b3fc456
TS
949 unsigned int header_size;
950 enum dma_data_direction dir;
7ab56645 951 int type, tag, err;
31ef9134
CL
952
953 mutex_lock(&s->mutex);
954
be4a2894 955 if (WARN_ON(amdtp_stream_running(s) ||
4b7da117 956 (s->data_block_quadlets < 1))) {
31ef9134
CL
957 err = -EBADFD;
958 goto err_unlock;
959 }
960
b6bc8123
TS
961 if (s->direction == AMDTP_IN_STREAM &&
962 s->flags & CIP_SKIP_INIT_DBC_CHECK)
963 s->data_block_counter = UINT_MAX;
964 else
965 s->data_block_counter = 0;
31ef9134
CL
966 s->data_block_state = initial_state[s->sfc].data_block;
967 s->syt_offset_state = initial_state[s->sfc].syt_offset;
968 s->last_syt_offset = TICKS_PER_CYCLE;
969
2b3fc456
TS
970 /* initialize packet buffer */
971 if (s->direction == AMDTP_IN_STREAM) {
972 dir = DMA_FROM_DEVICE;
973 type = FW_ISO_CONTEXT_RECEIVE;
974 header_size = IN_PACKET_HEADER_SIZE;
2b3fc456
TS
975 } else {
976 dir = DMA_TO_DEVICE;
977 type = FW_ISO_CONTEXT_TRANSMIT;
978 header_size = OUT_PACKET_HEADER_SIZE;
2b3fc456 979 }
31ef9134 980 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
2b3fc456 981 amdtp_stream_get_max_payload(s), dir);
31ef9134
CL
982 if (err < 0)
983 goto err_unlock;
984
985 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
2b3fc456 986 type, channel, speed, header_size,
7b3b0d85 987 amdtp_stream_first_callback, s);
31ef9134
CL
988 if (IS_ERR(s->context)) {
989 err = PTR_ERR(s->context);
990 if (err == -EBUSY)
991 dev_err(&s->unit->device,
be4a2894 992 "no free stream on this controller\n");
31ef9134
CL
993 goto err_buffer;
994 }
995
be4a2894 996 amdtp_stream_update(s);
31ef9134 997
ec00f5e4 998 s->packet_index = 0;
4b7da117 999 do {
2b3fc456
TS
1000 if (s->direction == AMDTP_IN_STREAM)
1001 err = queue_in_packet(s);
1002 else
1003 err = queue_out_packet(s, 0, true);
4b7da117
TS
1004 if (err < 0)
1005 goto err_context;
1006 } while (s->packet_index > 0);
31ef9134 1007
2b3fc456 1008 /* NOTE: TAG1 matches CIP. This just affects in stream. */
7ab56645
TS
1009 tag = FW_ISO_CONTEXT_MATCH_TAG1;
1010 if (s->flags & CIP_EMPTY_WITH_TAG0)
1011 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
1012
7b3b0d85 1013 s->callbacked = false;
7ab56645 1014 err = fw_iso_context_start(s->context, -1, 0, tag);
31ef9134
CL
1015 if (err < 0)
1016 goto err_context;
1017
1018 mutex_unlock(&s->mutex);
1019
1020 return 0;
1021
1022err_context:
1023 fw_iso_context_destroy(s->context);
1024 s->context = ERR_PTR(-1);
1025err_buffer:
1026 iso_packets_buffer_destroy(&s->buffer, s->unit);
1027err_unlock:
1028 mutex_unlock(&s->mutex);
1029
1030 return err;
1031}
be4a2894 1032EXPORT_SYMBOL(amdtp_stream_start);
31ef9134 1033
e9148ddd 1034/**
be4a2894
TS
1035 * amdtp_stream_pcm_pointer - get the PCM buffer position
1036 * @s: the AMDTP stream that transports the PCM data
e9148ddd
CL
1037 *
1038 * Returns the current buffer position, in frames.
1039 */
be4a2894 1040unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
e9148ddd 1041{
92b862c7 1042 /* this optimization is allowed to be racy */
c8de6dbb 1043 if (s->pointer_flush && amdtp_stream_running(s))
92b862c7
CL
1044 fw_iso_context_flush_completions(s->context);
1045 else
1046 s->pointer_flush = true;
e9148ddd
CL
1047
1048 return ACCESS_ONCE(s->pcm_buffer_pointer);
1049}
be4a2894 1050EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
e9148ddd 1051
31ef9134 1052/**
be4a2894
TS
1053 * amdtp_stream_update - update the stream after a bus reset
1054 * @s: the AMDTP stream
31ef9134 1055 */
be4a2894 1056void amdtp_stream_update(struct amdtp_stream *s)
31ef9134 1057{
9a2820c1 1058 /* Precomputing. */
31ef9134 1059 ACCESS_ONCE(s->source_node_id_field) =
9a2820c1
TS
1060 (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) &
1061 CIP_SID_MASK;
31ef9134 1062}
be4a2894 1063EXPORT_SYMBOL(amdtp_stream_update);
31ef9134
CL
1064
1065/**
be4a2894
TS
1066 * amdtp_stream_stop - stop sending packets
1067 * @s: the AMDTP stream to stop
31ef9134
CL
1068 *
1069 * All PCM and MIDI devices of the stream must be stopped before the stream
1070 * itself can be stopped.
1071 */
be4a2894 1072void amdtp_stream_stop(struct amdtp_stream *s)
31ef9134
CL
1073{
1074 mutex_lock(&s->mutex);
1075
be4a2894 1076 if (!amdtp_stream_running(s)) {
31ef9134
CL
1077 mutex_unlock(&s->mutex);
1078 return;
1079 }
1080
76fb8789 1081 tasklet_kill(&s->period_tasklet);
31ef9134
CL
1082 fw_iso_context_stop(s->context);
1083 fw_iso_context_destroy(s->context);
1084 s->context = ERR_PTR(-1);
1085 iso_packets_buffer_destroy(&s->buffer, s->unit);
1086
7b3b0d85
TS
1087 s->callbacked = false;
1088
31ef9134
CL
1089 mutex_unlock(&s->mutex);
1090}
be4a2894 1091EXPORT_SYMBOL(amdtp_stream_stop);
31ef9134
CL
1092
1093/**
be4a2894 1094 * amdtp_stream_pcm_abort - abort the running PCM device
31ef9134
CL
1095 * @s: the AMDTP stream about to be stopped
1096 *
1097 * If the isochronous stream needs to be stopped asynchronously, call this
1098 * function first to stop the PCM device.
1099 */
be4a2894 1100void amdtp_stream_pcm_abort(struct amdtp_stream *s)
31ef9134
CL
1101{
1102 struct snd_pcm_substream *pcm;
1103
1104 pcm = ACCESS_ONCE(s->pcm);
1fb8510c
TI
1105 if (pcm)
1106 snd_pcm_stop_xrun(pcm);
31ef9134 1107}
be4a2894 1108EXPORT_SYMBOL(amdtp_stream_pcm_abort);