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