2 * Audio and Music Data Transmission Protocol (IEC 61883-6) streams
3 * with Common Isochronous Packet (IEC 61883-1) headers
5 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6 * Licensed under the terms of the GNU General Public License, version 2.
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 <sound/pcm_params.h>
16 #include "amdtp-stream.h"
18 #define TICKS_PER_CYCLE 3072
19 #define CYCLES_PER_SECOND 8000
20 #define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
22 /* Always support Linux tracing subsystem. */
23 #define CREATE_TRACE_POINTS
24 #include "amdtp-stream-trace.h"
26 #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */
28 /* isochronous header parameters */
29 #define ISO_DATA_LENGTH_SHIFT 16
30 #define TAG_NO_CIP_HEADER 0
33 /* common isochronous packet header parameters */
34 #define CIP_EOH_SHIFT 31
35 #define CIP_EOH (1u << CIP_EOH_SHIFT)
36 #define CIP_EOH_MASK 0x80000000
37 #define CIP_SID_SHIFT 24
38 #define CIP_SID_MASK 0x3f000000
39 #define CIP_DBS_MASK 0x00ff0000
40 #define CIP_DBS_SHIFT 16
41 #define CIP_SPH_MASK 0x00000400
42 #define CIP_SPH_SHIFT 10
43 #define CIP_DBC_MASK 0x000000ff
44 #define CIP_FMT_SHIFT 24
45 #define CIP_FMT_MASK 0x3f000000
46 #define CIP_FDF_MASK 0x00ff0000
47 #define CIP_FDF_SHIFT 16
48 #define CIP_SYT_MASK 0x0000ffff
49 #define CIP_SYT_NO_INFO 0xffff
51 /* Audio and Music transfer protocol specific parameters */
52 #define CIP_FMT_AM 0x10
53 #define AMDTP_FDF_NO_DATA 0xff
55 /* TODO: make these configurable */
56 #define INTERRUPT_INTERVAL 16
57 #define QUEUE_LENGTH 48
59 #define IN_PACKET_HEADER_SIZE 4
60 #define OUT_PACKET_HEADER_SIZE 0
62 static void pcm_period_tasklet(unsigned long data);
65 * amdtp_stream_init - initialize an AMDTP stream structure
66 * @s: the AMDTP stream to initialize
67 * @unit: the target of the stream
68 * @dir: the direction of stream
69 * @flags: the packet transmission method to use
70 * @fmt: the value of fmt field in CIP header
71 * @process_data_blocks: callback handler to process data blocks
72 * @protocol_size: the size to allocate newly for protocol
74 int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
75 enum amdtp_stream_direction dir, enum cip_flags flags,
77 amdtp_stream_process_data_blocks_t process_data_blocks,
78 unsigned int protocol_size)
80 if (process_data_blocks == NULL)
83 s->protocol = kzalloc(protocol_size, GFP_KERNEL);
90 s->context = ERR_PTR(-1);
91 mutex_init(&s->mutex);
92 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
95 init_waitqueue_head(&s->callback_wait);
96 s->callbacked = false;
99 s->process_data_blocks = process_data_blocks;
103 EXPORT_SYMBOL(amdtp_stream_init);
106 * amdtp_stream_destroy - free stream resources
107 * @s: the AMDTP stream to destroy
109 void amdtp_stream_destroy(struct amdtp_stream *s)
111 /* Not initialized. */
112 if (s->protocol == NULL)
115 WARN_ON(amdtp_stream_running(s));
117 mutex_destroy(&s->mutex);
119 EXPORT_SYMBOL(amdtp_stream_destroy);
121 const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
125 [CIP_SFC_88200] = 16,
126 [CIP_SFC_96000] = 16,
127 [CIP_SFC_176400] = 32,
128 [CIP_SFC_192000] = 32,
130 EXPORT_SYMBOL(amdtp_syt_intervals);
132 const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
133 [CIP_SFC_32000] = 32000,
134 [CIP_SFC_44100] = 44100,
135 [CIP_SFC_48000] = 48000,
136 [CIP_SFC_88200] = 88200,
137 [CIP_SFC_96000] = 96000,
138 [CIP_SFC_176400] = 176400,
139 [CIP_SFC_192000] = 192000,
141 EXPORT_SYMBOL(amdtp_rate_table);
143 static int apply_constraint_to_size(struct snd_pcm_hw_params *params,
144 struct snd_pcm_hw_rule *rule)
146 struct snd_interval *s = hw_param_interval(params, rule->var);
147 const struct snd_interval *r =
148 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
149 struct snd_interval t = {
150 .min = s->min, .max = s->max, .integer = 1,
154 for (i = 0; i < CIP_SFC_COUNT; ++i) {
155 unsigned int rate = amdtp_rate_table[i];
156 unsigned int step = amdtp_syt_intervals[i];
158 if (!snd_interval_test(r, rate))
161 t.min = roundup(t.min, step);
162 t.max = rounddown(t.max, step);
165 if (snd_interval_checkempty(&t))
168 return snd_interval_refine(s, &t);
171 static int apply_constraint_to_rate(struct snd_pcm_hw_params *params,
172 struct snd_pcm_hw_rule *rule)
174 struct snd_interval *r =
175 hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
176 const struct snd_interval *s = hw_param_interval_c(params, rule->deps[0]);
177 struct snd_interval t = {
178 .min = UINT_MAX, .max = 0, .integer = 1,
182 for (i = 0; i < CIP_SFC_COUNT; ++i) {
183 unsigned int step = amdtp_syt_intervals[i];
184 unsigned int rate = amdtp_rate_table[i];
186 if (s->min % step || s->max % step)
189 t.min = min(t.min, rate);
190 t.max = max(t.max, rate);
193 return snd_interval_refine(r, &t);
197 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
198 * @s: the AMDTP stream, which must be initialized.
199 * @runtime: the PCM substream runtime
201 int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
202 struct snd_pcm_runtime *runtime)
204 struct snd_pcm_hardware *hw = &runtime->hw;
207 hw->info = SNDRV_PCM_INFO_BATCH |
208 SNDRV_PCM_INFO_BLOCK_TRANSFER |
209 SNDRV_PCM_INFO_INTERLEAVED |
210 SNDRV_PCM_INFO_JOINT_DUPLEX |
211 SNDRV_PCM_INFO_MMAP |
212 SNDRV_PCM_INFO_MMAP_VALID;
214 /* SNDRV_PCM_INFO_BATCH */
216 hw->periods_max = UINT_MAX;
218 /* bytes for a frame */
219 hw->period_bytes_min = 4 * hw->channels_max;
221 /* Just to prevent from allocating much pages. */
222 hw->period_bytes_max = hw->period_bytes_min * 2048;
223 hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
226 * Currently firewire-lib processes 16 packets in one software
227 * interrupt callback. This equals to 2msec but actually the
228 * interval of the interrupts has a jitter.
229 * Additionally, even if adding a constraint to fit period size to
230 * 2msec, actual calculated frames per period doesn't equal to 2msec,
231 * depending on sampling rate.
232 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
233 * Here let us use 5msec for safe period interrupt.
235 err = snd_pcm_hw_constraint_minmax(runtime,
236 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
241 /* Non-Blocking stream has no more constraints */
242 if (!(s->flags & CIP_BLOCKING))
246 * One AMDTP packet can include some frames. In blocking mode, the
247 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
248 * depending on its sampling rate. For accurate period interrupt, it's
249 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
251 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
252 apply_constraint_to_size, NULL,
253 SNDRV_PCM_HW_PARAM_RATE, -1);
256 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
257 apply_constraint_to_rate, NULL,
258 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
261 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
262 apply_constraint_to_size, NULL,
263 SNDRV_PCM_HW_PARAM_RATE, -1);
266 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
267 apply_constraint_to_rate, NULL,
268 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
274 EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
277 * amdtp_stream_set_parameters - set stream parameters
278 * @s: the AMDTP stream to configure
279 * @rate: the sample rate
280 * @data_block_quadlets: the size of a data block in quadlet unit
282 * The parameters must be set before the stream is started, and must not be
283 * changed while the stream is running.
285 int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
286 unsigned int data_block_quadlets)
290 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
291 if (amdtp_rate_table[sfc] == rate)
294 if (sfc == ARRAY_SIZE(amdtp_rate_table))
298 s->data_block_quadlets = data_block_quadlets;
299 s->syt_interval = amdtp_syt_intervals[sfc];
301 /* default buffering in the device */
302 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
303 if (s->flags & CIP_BLOCKING)
304 /* additional buffering needed to adjust for no-data packets */
305 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
309 EXPORT_SYMBOL(amdtp_stream_set_parameters);
312 * amdtp_stream_get_max_payload - get the stream's packet size
313 * @s: the AMDTP stream
315 * This function must not be called before the stream has been configured
316 * with amdtp_stream_set_parameters().
318 unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
320 unsigned int multiplier = 1;
321 unsigned int header_size = 0;
323 if (s->flags & CIP_JUMBO_PAYLOAD)
325 if (!(s->flags & CIP_NO_HEADER))
329 s->syt_interval * s->data_block_quadlets * 4 * multiplier;
331 EXPORT_SYMBOL(amdtp_stream_get_max_payload);
334 * amdtp_stream_pcm_prepare - prepare PCM device for running
335 * @s: the AMDTP stream
337 * This function should be called from the PCM device's .prepare callback.
339 void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
341 tasklet_kill(&s->period_tasklet);
342 s->pcm_buffer_pointer = 0;
343 s->pcm_period_pointer = 0;
345 EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
347 static unsigned int calculate_data_blocks(struct amdtp_stream *s,
350 unsigned int phase, data_blocks;
353 if (s->flags & CIP_BLOCKING) {
354 /* This module generate empty packet for 'no data'. */
355 if (syt == CIP_SYT_NO_INFO)
358 data_blocks = s->syt_interval;
359 /* Non-blocking mode. */
361 if (!cip_sfc_is_base_44100(s->sfc)) {
362 /* Sample_rate / 8000 is an integer, and precomputed. */
363 data_blocks = s->data_block_state;
365 phase = s->data_block_state;
368 * This calculates the number of data blocks per packet so that
369 * 1) the overall rate is correct and exactly synchronized to
371 * 2) packets with a rounded-up number of blocks occur as early
372 * as possible in the sequence (to prevent underruns of the
375 if (s->sfc == CIP_SFC_44100)
376 /* 6 6 5 6 5 6 5 ... */
377 data_blocks = 5 + ((phase & 1) ^
378 (phase == 0 || phase >= 40));
380 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
381 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
382 if (++phase >= (80 >> (s->sfc >> 1)))
384 s->data_block_state = phase;
391 static unsigned int calculate_syt(struct amdtp_stream *s,
394 unsigned int syt_offset, phase, index, syt;
396 if (s->last_syt_offset < TICKS_PER_CYCLE) {
397 if (!cip_sfc_is_base_44100(s->sfc))
398 syt_offset = s->last_syt_offset + s->syt_offset_state;
401 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
402 * n * SYT_INTERVAL * 24576000 / sample_rate
403 * Modulo TICKS_PER_CYCLE, the difference between successive
404 * elements is about 1386.23. Rounding the results of this
405 * formula to the SYT precision results in a sequence of
406 * differences that begins with:
407 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
408 * This code generates _exactly_ the same sequence.
410 phase = s->syt_offset_state;
412 syt_offset = s->last_syt_offset;
413 syt_offset += 1386 + ((index && !(index & 3)) ||
417 s->syt_offset_state = phase;
420 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
421 s->last_syt_offset = syt_offset;
423 if (syt_offset < TICKS_PER_CYCLE) {
424 syt_offset += s->transfer_delay;
425 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
426 syt += syt_offset % TICKS_PER_CYCLE;
428 return syt & CIP_SYT_MASK;
430 return CIP_SYT_NO_INFO;
434 static void update_pcm_pointers(struct amdtp_stream *s,
435 struct snd_pcm_substream *pcm,
440 ptr = s->pcm_buffer_pointer + frames;
441 if (ptr >= pcm->runtime->buffer_size)
442 ptr -= pcm->runtime->buffer_size;
443 WRITE_ONCE(s->pcm_buffer_pointer, ptr);
445 s->pcm_period_pointer += frames;
446 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
447 s->pcm_period_pointer -= pcm->runtime->period_size;
448 tasklet_hi_schedule(&s->period_tasklet);
452 static void pcm_period_tasklet(unsigned long data)
454 struct amdtp_stream *s = (void *)data;
455 struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
458 snd_pcm_period_elapsed(pcm);
461 static int queue_packet(struct amdtp_stream *s, unsigned int header_length,
462 unsigned int payload_length)
464 struct fw_iso_packet p = {0};
467 if (IS_ERR(s->context))
470 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
472 p.header_length = header_length;
473 if (payload_length > 0)
474 p.payload_length = payload_length;
477 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
478 s->buffer.packets[s->packet_index].offset);
480 dev_err(&s->unit->device, "queueing error: %d\n", err);
484 if (++s->packet_index >= QUEUE_LENGTH)
490 static inline int queue_out_packet(struct amdtp_stream *s,
491 unsigned int payload_length)
493 return queue_packet(s, OUT_PACKET_HEADER_SIZE, payload_length);
496 static inline int queue_in_packet(struct amdtp_stream *s)
498 return queue_packet(s, IN_PACKET_HEADER_SIZE, s->max_payload_length);
501 static int handle_out_packet(struct amdtp_stream *s,
502 unsigned int payload_length, unsigned int cycle,
507 unsigned int data_blocks;
508 unsigned int pcm_frames;
509 struct snd_pcm_substream *pcm;
511 buffer = s->buffer.packets[s->packet_index].buffer;
512 syt = calculate_syt(s, cycle);
513 data_blocks = calculate_data_blocks(s, syt);
514 pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
516 if (s->flags & CIP_DBC_IS_END_EVENT)
517 s->data_block_counter =
518 (s->data_block_counter + data_blocks) & 0xff;
520 buffer[0] = cpu_to_be32(READ_ONCE(s->source_node_id_field) |
521 (s->data_block_quadlets << CIP_DBS_SHIFT) |
522 ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) |
523 s->data_block_counter);
524 buffer[1] = cpu_to_be32(CIP_EOH |
525 ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
526 ((s->fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
527 (syt & CIP_SYT_MASK));
529 if (!(s->flags & CIP_DBC_IS_END_EVENT))
530 s->data_block_counter =
531 (s->data_block_counter + data_blocks) & 0xff;
532 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
534 trace_out_packet(s, cycle, buffer, payload_length, index);
536 if (queue_out_packet(s, payload_length) < 0)
539 pcm = READ_ONCE(s->pcm);
540 if (pcm && pcm_frames > 0)
541 update_pcm_pointers(s, pcm, pcm_frames);
543 /* No need to return the number of handled data blocks. */
547 static int handle_out_packet_without_header(struct amdtp_stream *s,
548 unsigned int payload_length, unsigned int cycle,
553 unsigned int data_blocks;
554 unsigned int pcm_frames;
555 struct snd_pcm_substream *pcm;
557 buffer = s->buffer.packets[s->packet_index].buffer;
558 syt = calculate_syt(s, cycle);
559 data_blocks = calculate_data_blocks(s, syt);
560 pcm_frames = s->process_data_blocks(s, buffer, data_blocks, &syt);
561 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
563 payload_length = data_blocks * 4 * s->data_block_quadlets;
565 trace_out_packet_without_header(s, cycle, payload_length, data_blocks,
568 if (queue_out_packet(s, payload_length) < 0)
571 pcm = READ_ONCE(s->pcm);
572 if (pcm && pcm_frames > 0)
573 update_pcm_pointers(s, pcm, pcm_frames);
575 /* No need to return the number of handled data blocks. */
579 static int handle_in_packet(struct amdtp_stream *s,
580 unsigned int payload_length, unsigned int cycle,
585 unsigned int sph, fmt, fdf, syt;
586 unsigned int data_block_quadlets, data_block_counter, dbc_interval;
587 unsigned int data_blocks;
588 struct snd_pcm_substream *pcm;
589 unsigned int pcm_frames;
592 buffer = s->buffer.packets[s->packet_index].buffer;
593 cip_header[0] = be32_to_cpu(buffer[0]);
594 cip_header[1] = be32_to_cpu(buffer[1]);
596 trace_in_packet(s, cycle, cip_header, payload_length, index);
599 * This module supports 'Two-quadlet CIP header with SYT field'.
600 * For convenience, also check FMT field is AM824 or not.
602 if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
603 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) &&
604 (!(s->flags & CIP_HEADER_WITHOUT_EOH))) {
605 dev_info_ratelimited(&s->unit->device,
606 "Invalid CIP header for AMDTP: %08X:%08X\n",
607 cip_header[0], cip_header[1]);
613 /* Check valid protocol or not. */
614 sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT;
615 fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
616 if (sph != s->sph || fmt != s->fmt) {
617 dev_info_ratelimited(&s->unit->device,
618 "Detect unexpected protocol: %08x %08x\n",
619 cip_header[0], cip_header[1]);
625 /* Calculate data blocks */
626 fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT;
627 if (payload_length < 12 ||
628 (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
631 data_block_quadlets =
632 (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
633 /* avoid division by zero */
634 if (data_block_quadlets == 0) {
635 dev_err(&s->unit->device,
636 "Detect invalid value in dbs field: %08X\n",
640 if (s->flags & CIP_WRONG_DBS)
641 data_block_quadlets = s->data_block_quadlets;
643 data_blocks = (payload_length / 4 - 2) /
647 /* Check data block counter continuity */
648 data_block_counter = cip_header[0] & CIP_DBC_MASK;
649 if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
650 s->data_block_counter != UINT_MAX)
651 data_block_counter = s->data_block_counter;
653 if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) &&
654 data_block_counter == s->tx_first_dbc) ||
655 s->data_block_counter == UINT_MAX) {
657 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
658 lost = data_block_counter != s->data_block_counter;
660 if (data_blocks > 0 && s->tx_dbc_interval > 0)
661 dbc_interval = s->tx_dbc_interval;
663 dbc_interval = data_blocks;
665 lost = data_block_counter !=
666 ((s->data_block_counter + dbc_interval) & 0xff);
670 dev_err(&s->unit->device,
671 "Detect discontinuity of CIP: %02X %02X\n",
672 s->data_block_counter, data_block_counter);
676 syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
677 pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
679 if (s->flags & CIP_DBC_IS_END_EVENT)
680 s->data_block_counter = data_block_counter;
682 s->data_block_counter =
683 (data_block_counter + data_blocks) & 0xff;
685 if (queue_in_packet(s) < 0)
688 pcm = READ_ONCE(s->pcm);
689 if (pcm && pcm_frames > 0)
690 update_pcm_pointers(s, pcm, pcm_frames);
695 static int handle_in_packet_without_header(struct amdtp_stream *s,
696 unsigned int payload_quadlets, unsigned int cycle,
700 unsigned int data_blocks;
701 struct snd_pcm_substream *pcm;
702 unsigned int pcm_frames;
704 buffer = s->buffer.packets[s->packet_index].buffer;
705 data_blocks = payload_quadlets / s->data_block_quadlets;
707 trace_in_packet_without_header(s, cycle, payload_quadlets, data_blocks,
710 pcm_frames = s->process_data_blocks(s, buffer, data_blocks, NULL);
711 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
713 if (queue_in_packet(s) < 0)
716 pcm = READ_ONCE(s->pcm);
717 if (pcm && pcm_frames > 0)
718 update_pcm_pointers(s, pcm, pcm_frames);
724 * In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
725 * the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
726 * it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
728 static inline u32 compute_cycle_count(u32 tstamp)
730 return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff);
733 static inline u32 increment_cycle_count(u32 cycle, unsigned int addend)
736 if (cycle >= 8 * CYCLES_PER_SECOND)
737 cycle -= 8 * CYCLES_PER_SECOND;
741 static inline u32 decrement_cycle_count(u32 cycle, unsigned int subtrahend)
743 if (cycle < subtrahend)
744 cycle += 8 * CYCLES_PER_SECOND;
745 return cycle - subtrahend;
748 static void out_stream_callback(struct fw_iso_context *context, u32 tstamp,
749 size_t header_length, void *header,
752 struct amdtp_stream *s = private_data;
753 unsigned int i, packets = header_length / 4;
756 if (s->packet_index < 0)
759 cycle = compute_cycle_count(tstamp);
761 /* Align to actual cycle count for the last packet. */
762 cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
764 for (i = 0; i < packets; ++i) {
765 cycle = increment_cycle_count(cycle, 1);
766 if (s->handle_packet(s, 0, cycle, i) < 0) {
767 s->packet_index = -1;
769 amdtp_stream_pcm_abort(s);
770 WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
775 fw_iso_context_queue_flush(s->context);
778 static void in_stream_callback(struct fw_iso_context *context, u32 tstamp,
779 size_t header_length, void *header,
782 struct amdtp_stream *s = private_data;
783 unsigned int i, packets;
784 unsigned int payload_length, max_payload_length;
785 __be32 *headers = header;
788 if (s->packet_index < 0)
791 /* The number of packets in buffer */
792 packets = header_length / IN_PACKET_HEADER_SIZE;
794 cycle = compute_cycle_count(tstamp);
796 /* Align to actual cycle count for the last packet. */
797 cycle = decrement_cycle_count(cycle, packets);
799 /* For buffer-over-run prevention. */
800 max_payload_length = s->max_payload_length;
802 for (i = 0; i < packets; i++) {
803 cycle = increment_cycle_count(cycle, 1);
805 /* The number of bytes in this packet */
807 (be32_to_cpu(headers[i]) >> ISO_DATA_LENGTH_SHIFT);
808 if (payload_length > max_payload_length) {
809 dev_err(&s->unit->device,
810 "Detect jumbo payload: %04x %04x\n",
811 payload_length, max_payload_length);
815 if (s->handle_packet(s, payload_length, cycle, i) < 0)
819 /* Queueing error or detecting invalid payload. */
821 s->packet_index = -1;
823 amdtp_stream_pcm_abort(s);
824 WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
828 fw_iso_context_queue_flush(s->context);
831 /* this is executed one time */
832 static void amdtp_stream_first_callback(struct fw_iso_context *context,
833 u32 tstamp, size_t header_length,
834 void *header, void *private_data)
836 struct amdtp_stream *s = private_data;
838 unsigned int packets;
841 * For in-stream, first packet has come.
842 * For out-stream, prepared to transmit first packet
844 s->callbacked = true;
845 wake_up(&s->callback_wait);
847 cycle = compute_cycle_count(tstamp);
849 if (s->direction == AMDTP_IN_STREAM) {
850 packets = header_length / IN_PACKET_HEADER_SIZE;
851 cycle = decrement_cycle_count(cycle, packets);
852 context->callback.sc = in_stream_callback;
853 if (s->flags & CIP_NO_HEADER)
854 s->handle_packet = handle_in_packet_without_header;
856 s->handle_packet = handle_in_packet;
858 packets = header_length / 4;
859 cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
860 context->callback.sc = out_stream_callback;
861 if (s->flags & CIP_NO_HEADER)
862 s->handle_packet = handle_out_packet_without_header;
864 s->handle_packet = handle_out_packet;
867 s->start_cycle = cycle;
869 context->callback.sc(context, tstamp, header_length, header, s);
873 * amdtp_stream_start - start transferring packets
874 * @s: the AMDTP stream to start
875 * @channel: the isochronous channel on the bus
876 * @speed: firewire speed code
878 * The stream cannot be started until it has been configured with
879 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
880 * device can be started.
882 int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
884 static const struct {
885 unsigned int data_block;
886 unsigned int syt_offset;
887 } initial_state[] = {
888 [CIP_SFC_32000] = { 4, 3072 },
889 [CIP_SFC_48000] = { 6, 1024 },
890 [CIP_SFC_96000] = { 12, 1024 },
891 [CIP_SFC_192000] = { 24, 1024 },
892 [CIP_SFC_44100] = { 0, 67 },
893 [CIP_SFC_88200] = { 0, 67 },
894 [CIP_SFC_176400] = { 0, 67 },
896 unsigned int header_size;
897 enum dma_data_direction dir;
900 mutex_lock(&s->mutex);
902 if (WARN_ON(amdtp_stream_running(s) ||
903 (s->data_block_quadlets < 1))) {
908 if (s->direction == AMDTP_IN_STREAM)
909 s->data_block_counter = UINT_MAX;
911 s->data_block_counter = 0;
912 s->data_block_state = initial_state[s->sfc].data_block;
913 s->syt_offset_state = initial_state[s->sfc].syt_offset;
914 s->last_syt_offset = TICKS_PER_CYCLE;
916 /* initialize packet buffer */
917 if (s->direction == AMDTP_IN_STREAM) {
918 dir = DMA_FROM_DEVICE;
919 type = FW_ISO_CONTEXT_RECEIVE;
920 header_size = IN_PACKET_HEADER_SIZE;
923 type = FW_ISO_CONTEXT_TRANSMIT;
924 header_size = OUT_PACKET_HEADER_SIZE;
926 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
927 amdtp_stream_get_max_payload(s), dir);
931 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
932 type, channel, speed, header_size,
933 amdtp_stream_first_callback, s);
934 if (IS_ERR(s->context)) {
935 err = PTR_ERR(s->context);
937 dev_err(&s->unit->device,
938 "no free stream on this controller\n");
942 amdtp_stream_update(s);
944 if (s->direction == AMDTP_IN_STREAM)
945 s->max_payload_length = amdtp_stream_get_max_payload(s);
947 if (s->flags & CIP_NO_HEADER)
948 s->tag = TAG_NO_CIP_HEADER;
954 if (s->direction == AMDTP_IN_STREAM)
955 err = queue_in_packet(s);
957 err = queue_out_packet(s, 0);
960 } while (s->packet_index > 0);
962 /* NOTE: TAG1 matches CIP. This just affects in stream. */
963 tag = FW_ISO_CONTEXT_MATCH_TAG1;
964 if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER))
965 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
967 s->callbacked = false;
968 err = fw_iso_context_start(s->context, -1, 0, tag);
972 mutex_unlock(&s->mutex);
977 fw_iso_context_destroy(s->context);
978 s->context = ERR_PTR(-1);
980 iso_packets_buffer_destroy(&s->buffer, s->unit);
982 mutex_unlock(&s->mutex);
986 EXPORT_SYMBOL(amdtp_stream_start);
989 * amdtp_stream_pcm_pointer - get the PCM buffer position
990 * @s: the AMDTP stream that transports the PCM data
992 * Returns the current buffer position, in frames.
994 unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
997 * This function is called in software IRQ context of period_tasklet or
1000 * When the software IRQ context was scheduled by software IRQ context
1001 * of IR/IT contexts, queued packets were already handled. Therefore,
1002 * no need to flush the queue in buffer anymore.
1004 * When the process context reach here, some packets will be already
1005 * queued in the buffer. These packets should be handled immediately
1006 * to keep better granularity of PCM pointer.
1008 * Later, the process context will sometimes schedules software IRQ
1009 * context of the period_tasklet. Then, no need to flush the queue by
1010 * the same reason as described for IR/IT contexts.
1012 if (!in_interrupt() && amdtp_stream_running(s))
1013 fw_iso_context_flush_completions(s->context);
1015 return READ_ONCE(s->pcm_buffer_pointer);
1017 EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
1020 * amdtp_stream_pcm_ack - acknowledge queued PCM frames
1021 * @s: the AMDTP stream that transfers the PCM frames
1023 * Returns zero always.
1025 int amdtp_stream_pcm_ack(struct amdtp_stream *s)
1028 * Process isochronous packets for recent isochronous cycle to handle
1029 * queued PCM frames.
1031 if (amdtp_stream_running(s))
1032 fw_iso_context_flush_completions(s->context);
1036 EXPORT_SYMBOL(amdtp_stream_pcm_ack);
1039 * amdtp_stream_update - update the stream after a bus reset
1040 * @s: the AMDTP stream
1042 void amdtp_stream_update(struct amdtp_stream *s)
1045 WRITE_ONCE(s->source_node_id_field,
1046 (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & CIP_SID_MASK);
1048 EXPORT_SYMBOL(amdtp_stream_update);
1051 * amdtp_stream_stop - stop sending packets
1052 * @s: the AMDTP stream to stop
1054 * All PCM and MIDI devices of the stream must be stopped before the stream
1055 * itself can be stopped.
1057 void amdtp_stream_stop(struct amdtp_stream *s)
1059 mutex_lock(&s->mutex);
1061 if (!amdtp_stream_running(s)) {
1062 mutex_unlock(&s->mutex);
1066 tasklet_kill(&s->period_tasklet);
1067 fw_iso_context_stop(s->context);
1068 fw_iso_context_destroy(s->context);
1069 s->context = ERR_PTR(-1);
1070 iso_packets_buffer_destroy(&s->buffer, s->unit);
1072 s->callbacked = false;
1074 mutex_unlock(&s->mutex);
1076 EXPORT_SYMBOL(amdtp_stream_stop);
1079 * amdtp_stream_pcm_abort - abort the running PCM device
1080 * @s: the AMDTP stream about to be stopped
1082 * If the isochronous stream needs to be stopped asynchronously, call this
1083 * function first to stop the PCM device.
1085 void amdtp_stream_pcm_abort(struct amdtp_stream *s)
1087 struct snd_pcm_substream *pcm;
1089 pcm = READ_ONCE(s->pcm);
1091 snd_pcm_stop_xrun(pcm);
1093 EXPORT_SYMBOL(amdtp_stream_pcm_abort);