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