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