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