1 // SPDX-License-Identifier: GPL-2.0-only
3 * Audio and Music Data Transmission Protocol (IEC 61883-6) streams
4 * with Common Isochronous Packet (IEC 61883-1) headers
6 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
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 IR_HEADER_SIZE 8 // For header and timestamp.
60 #define OUT_PACKET_HEADER_SIZE 0
61 #define HEADER_TSTAMP_MASK 0x0000ffff
63 static void pcm_period_tasklet(unsigned long data);
66 * amdtp_stream_init - initialize an AMDTP stream structure
67 * @s: the AMDTP stream to initialize
68 * @unit: the target of the stream
69 * @dir: the direction of stream
70 * @flags: the packet transmission method to use
71 * @fmt: the value of fmt field in CIP header
72 * @process_data_blocks: callback handler to process data blocks
73 * @protocol_size: the size to allocate newly for protocol
75 int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
76 enum amdtp_stream_direction dir, enum cip_flags flags,
78 amdtp_stream_process_data_blocks_t process_data_blocks,
79 unsigned int protocol_size)
81 if (process_data_blocks == NULL)
84 s->protocol = kzalloc(protocol_size, GFP_KERNEL);
91 s->context = ERR_PTR(-1);
92 mutex_init(&s->mutex);
93 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
96 init_waitqueue_head(&s->callback_wait);
97 s->callbacked = false;
100 s->process_data_blocks = process_data_blocks;
104 EXPORT_SYMBOL(amdtp_stream_init);
107 * amdtp_stream_destroy - free stream resources
108 * @s: the AMDTP stream to destroy
110 void amdtp_stream_destroy(struct amdtp_stream *s)
112 /* Not initialized. */
113 if (s->protocol == NULL)
116 WARN_ON(amdtp_stream_running(s));
118 mutex_destroy(&s->mutex);
120 EXPORT_SYMBOL(amdtp_stream_destroy);
122 const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
126 [CIP_SFC_88200] = 16,
127 [CIP_SFC_96000] = 16,
128 [CIP_SFC_176400] = 32,
129 [CIP_SFC_192000] = 32,
131 EXPORT_SYMBOL(amdtp_syt_intervals);
133 const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
134 [CIP_SFC_32000] = 32000,
135 [CIP_SFC_44100] = 44100,
136 [CIP_SFC_48000] = 48000,
137 [CIP_SFC_88200] = 88200,
138 [CIP_SFC_96000] = 96000,
139 [CIP_SFC_176400] = 176400,
140 [CIP_SFC_192000] = 192000,
142 EXPORT_SYMBOL(amdtp_rate_table);
144 static int apply_constraint_to_size(struct snd_pcm_hw_params *params,
145 struct snd_pcm_hw_rule *rule)
147 struct snd_interval *s = hw_param_interval(params, rule->var);
148 const struct snd_interval *r =
149 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
150 struct snd_interval t = {0};
151 unsigned int step = 0;
154 for (i = 0; i < CIP_SFC_COUNT; ++i) {
155 if (snd_interval_test(r, amdtp_rate_table[i]))
156 step = max(step, amdtp_syt_intervals[i]);
159 t.min = roundup(s->min, step);
160 t.max = rounddown(s->max, step);
163 return snd_interval_refine(s, &t);
167 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
168 * @s: the AMDTP stream, which must be initialized.
169 * @runtime: the PCM substream runtime
171 int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
172 struct snd_pcm_runtime *runtime)
174 struct snd_pcm_hardware *hw = &runtime->hw;
177 hw->info = SNDRV_PCM_INFO_BATCH |
178 SNDRV_PCM_INFO_BLOCK_TRANSFER |
179 SNDRV_PCM_INFO_INTERLEAVED |
180 SNDRV_PCM_INFO_JOINT_DUPLEX |
181 SNDRV_PCM_INFO_MMAP |
182 SNDRV_PCM_INFO_MMAP_VALID;
184 /* SNDRV_PCM_INFO_BATCH */
186 hw->periods_max = UINT_MAX;
188 /* bytes for a frame */
189 hw->period_bytes_min = 4 * hw->channels_max;
191 /* Just to prevent from allocating much pages. */
192 hw->period_bytes_max = hw->period_bytes_min * 2048;
193 hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
196 * Currently firewire-lib processes 16 packets in one software
197 * interrupt callback. This equals to 2msec but actually the
198 * interval of the interrupts has a jitter.
199 * Additionally, even if adding a constraint to fit period size to
200 * 2msec, actual calculated frames per period doesn't equal to 2msec,
201 * depending on sampling rate.
202 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
203 * Here let us use 5msec for safe period interrupt.
205 err = snd_pcm_hw_constraint_minmax(runtime,
206 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
211 /* Non-Blocking stream has no more constraints */
212 if (!(s->flags & CIP_BLOCKING))
216 * One AMDTP packet can include some frames. In blocking mode, the
217 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
218 * depending on its sampling rate. For accurate period interrupt, it's
219 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
221 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
222 apply_constraint_to_size, NULL,
223 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
224 SNDRV_PCM_HW_PARAM_RATE, -1);
227 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
228 apply_constraint_to_size, NULL,
229 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
230 SNDRV_PCM_HW_PARAM_RATE, -1);
236 EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
239 * amdtp_stream_set_parameters - set stream parameters
240 * @s: the AMDTP stream to configure
241 * @rate: the sample rate
242 * @data_block_quadlets: the size of a data block in quadlet unit
244 * The parameters must be set before the stream is started, and must not be
245 * changed while the stream is running.
247 int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
248 unsigned int data_block_quadlets)
252 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
253 if (amdtp_rate_table[sfc] == rate)
256 if (sfc == ARRAY_SIZE(amdtp_rate_table))
260 s->data_block_quadlets = data_block_quadlets;
261 s->syt_interval = amdtp_syt_intervals[sfc];
263 /* default buffering in the device */
264 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
265 if (s->flags & CIP_BLOCKING)
266 /* additional buffering needed to adjust for no-data packets */
267 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
271 EXPORT_SYMBOL(amdtp_stream_set_parameters);
274 * amdtp_stream_get_max_payload - get the stream's packet size
275 * @s: the AMDTP stream
277 * This function must not be called before the stream has been configured
278 * with amdtp_stream_set_parameters().
280 unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
282 unsigned int multiplier = 1;
283 unsigned int header_size = 0;
285 if (s->flags & CIP_JUMBO_PAYLOAD)
287 if (!(s->flags & CIP_NO_HEADER))
291 s->syt_interval * s->data_block_quadlets * 4 * multiplier;
293 EXPORT_SYMBOL(amdtp_stream_get_max_payload);
296 * amdtp_stream_pcm_prepare - prepare PCM device for running
297 * @s: the AMDTP stream
299 * This function should be called from the PCM device's .prepare callback.
301 void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
303 tasklet_kill(&s->period_tasklet);
304 s->pcm_buffer_pointer = 0;
305 s->pcm_period_pointer = 0;
307 EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
309 static unsigned int calculate_data_blocks(struct amdtp_stream *s,
312 unsigned int phase, data_blocks;
315 if (s->flags & CIP_BLOCKING) {
316 /* This module generate empty packet for 'no data'. */
317 if (syt == CIP_SYT_NO_INFO)
320 data_blocks = s->syt_interval;
321 /* Non-blocking mode. */
323 if (!cip_sfc_is_base_44100(s->sfc)) {
324 /* Sample_rate / 8000 is an integer, and precomputed. */
325 data_blocks = s->data_block_state;
327 phase = s->data_block_state;
330 * This calculates the number of data blocks per packet so that
331 * 1) the overall rate is correct and exactly synchronized to
333 * 2) packets with a rounded-up number of blocks occur as early
334 * as possible in the sequence (to prevent underruns of the
337 if (s->sfc == CIP_SFC_44100)
338 /* 6 6 5 6 5 6 5 ... */
339 data_blocks = 5 + ((phase & 1) ^
340 (phase == 0 || phase >= 40));
342 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
343 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
344 if (++phase >= (80 >> (s->sfc >> 1)))
346 s->data_block_state = phase;
353 static unsigned int calculate_syt(struct amdtp_stream *s,
356 unsigned int syt_offset, phase, index, syt;
358 if (s->last_syt_offset < TICKS_PER_CYCLE) {
359 if (!cip_sfc_is_base_44100(s->sfc))
360 syt_offset = s->last_syt_offset + s->syt_offset_state;
363 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
364 * n * SYT_INTERVAL * 24576000 / sample_rate
365 * Modulo TICKS_PER_CYCLE, the difference between successive
366 * elements is about 1386.23. Rounding the results of this
367 * formula to the SYT precision results in a sequence of
368 * differences that begins with:
369 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
370 * This code generates _exactly_ the same sequence.
372 phase = s->syt_offset_state;
374 syt_offset = s->last_syt_offset;
375 syt_offset += 1386 + ((index && !(index & 3)) ||
379 s->syt_offset_state = phase;
382 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
383 s->last_syt_offset = syt_offset;
385 if (syt_offset < TICKS_PER_CYCLE) {
386 syt_offset += s->transfer_delay;
387 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
388 syt += syt_offset % TICKS_PER_CYCLE;
390 return syt & CIP_SYT_MASK;
392 return CIP_SYT_NO_INFO;
396 static void update_pcm_pointers(struct amdtp_stream *s,
397 struct snd_pcm_substream *pcm,
402 ptr = s->pcm_buffer_pointer + frames;
403 if (ptr >= pcm->runtime->buffer_size)
404 ptr -= pcm->runtime->buffer_size;
405 WRITE_ONCE(s->pcm_buffer_pointer, ptr);
407 s->pcm_period_pointer += frames;
408 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
409 s->pcm_period_pointer -= pcm->runtime->period_size;
410 tasklet_hi_schedule(&s->period_tasklet);
414 static void pcm_period_tasklet(unsigned long data)
416 struct amdtp_stream *s = (void *)data;
417 struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
420 snd_pcm_period_elapsed(pcm);
423 static int queue_packet(struct amdtp_stream *s, unsigned int header_length,
424 unsigned int payload_length)
426 struct fw_iso_packet p = {0};
429 if (IS_ERR(s->context))
432 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
434 p.header_length = header_length;
435 if (payload_length > 0)
436 p.payload_length = payload_length;
439 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
440 s->buffer.packets[s->packet_index].offset);
442 dev_err(&s->unit->device, "queueing error: %d\n", err);
446 if (++s->packet_index >= QUEUE_LENGTH)
452 static inline int queue_out_packet(struct amdtp_stream *s,
453 unsigned int payload_length)
455 return queue_packet(s, OUT_PACKET_HEADER_SIZE, payload_length);
458 static inline int queue_in_packet(struct amdtp_stream *s)
460 return queue_packet(s, IR_HEADER_SIZE, s->max_payload_length);
463 static int handle_out_packet(struct amdtp_stream *s,
464 unsigned int payload_length, unsigned int cycle,
469 unsigned int data_blocks;
470 unsigned int pcm_frames;
471 struct snd_pcm_substream *pcm;
473 buffer = s->buffer.packets[s->packet_index].buffer;
474 syt = calculate_syt(s, cycle);
475 data_blocks = calculate_data_blocks(s, syt);
476 pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
478 if (s->flags & CIP_DBC_IS_END_EVENT)
479 s->data_block_counter =
480 (s->data_block_counter + data_blocks) & 0xff;
482 buffer[0] = cpu_to_be32(READ_ONCE(s->source_node_id_field) |
483 (s->data_block_quadlets << CIP_DBS_SHIFT) |
484 ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) |
485 s->data_block_counter);
486 buffer[1] = cpu_to_be32(CIP_EOH |
487 ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
488 ((s->fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
489 (syt & CIP_SYT_MASK));
491 if (!(s->flags & CIP_DBC_IS_END_EVENT))
492 s->data_block_counter =
493 (s->data_block_counter + data_blocks) & 0xff;
494 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
496 trace_out_packet(s, cycle, buffer, payload_length, index);
498 if (queue_out_packet(s, payload_length) < 0)
501 pcm = READ_ONCE(s->pcm);
502 if (pcm && pcm_frames > 0)
503 update_pcm_pointers(s, pcm, pcm_frames);
505 /* No need to return the number of handled data blocks. */
509 static int handle_out_packet_without_header(struct amdtp_stream *s,
510 unsigned int payload_length, unsigned int cycle,
515 unsigned int data_blocks;
516 unsigned int pcm_frames;
517 struct snd_pcm_substream *pcm;
519 buffer = s->buffer.packets[s->packet_index].buffer;
520 syt = calculate_syt(s, cycle);
521 data_blocks = calculate_data_blocks(s, syt);
522 pcm_frames = s->process_data_blocks(s, buffer, data_blocks, &syt);
523 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
525 payload_length = data_blocks * 4 * s->data_block_quadlets;
527 trace_out_packet_without_header(s, cycle, payload_length, data_blocks,
530 if (queue_out_packet(s, payload_length) < 0)
533 pcm = READ_ONCE(s->pcm);
534 if (pcm && pcm_frames > 0)
535 update_pcm_pointers(s, pcm, pcm_frames);
537 /* No need to return the number of handled data blocks. */
541 static int handle_in_packet(struct amdtp_stream *s,
542 unsigned int payload_length, unsigned int cycle,
547 unsigned int sph, fmt, fdf, syt;
548 unsigned int data_block_quadlets, data_block_counter, dbc_interval;
549 unsigned int data_blocks;
550 struct snd_pcm_substream *pcm;
551 unsigned int pcm_frames;
554 buffer = s->buffer.packets[s->packet_index].buffer;
555 cip_header[0] = be32_to_cpu(buffer[0]);
556 cip_header[1] = be32_to_cpu(buffer[1]);
558 trace_in_packet(s, cycle, cip_header, payload_length, index);
561 * This module supports 'Two-quadlet CIP header with SYT field'.
562 * For convenience, also check FMT field is AM824 or not.
564 if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
565 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) &&
566 (!(s->flags & CIP_HEADER_WITHOUT_EOH))) {
567 dev_info_ratelimited(&s->unit->device,
568 "Invalid CIP header for AMDTP: %08X:%08X\n",
569 cip_header[0], cip_header[1]);
575 /* Check valid protocol or not. */
576 sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT;
577 fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
578 if (sph != s->sph || fmt != s->fmt) {
579 dev_info_ratelimited(&s->unit->device,
580 "Detect unexpected protocol: %08x %08x\n",
581 cip_header[0], cip_header[1]);
587 /* Calculate data blocks */
588 fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT;
589 if (payload_length < 12 ||
590 (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
593 data_block_quadlets =
594 (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
595 /* avoid division by zero */
596 if (data_block_quadlets == 0) {
597 dev_err(&s->unit->device,
598 "Detect invalid value in dbs field: %08X\n",
602 if (s->flags & CIP_WRONG_DBS)
603 data_block_quadlets = s->data_block_quadlets;
605 data_blocks = (payload_length / 4 - 2) /
609 /* Check data block counter continuity */
610 data_block_counter = cip_header[0] & CIP_DBC_MASK;
611 if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
612 s->data_block_counter != UINT_MAX)
613 data_block_counter = s->data_block_counter;
615 if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) &&
616 data_block_counter == s->tx_first_dbc) ||
617 s->data_block_counter == UINT_MAX) {
619 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
620 lost = data_block_counter != s->data_block_counter;
622 if (data_blocks > 0 && s->tx_dbc_interval > 0)
623 dbc_interval = s->tx_dbc_interval;
625 dbc_interval = data_blocks;
627 lost = data_block_counter !=
628 ((s->data_block_counter + dbc_interval) & 0xff);
632 dev_err(&s->unit->device,
633 "Detect discontinuity of CIP: %02X %02X\n",
634 s->data_block_counter, data_block_counter);
638 syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
639 pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
641 if (s->flags & CIP_DBC_IS_END_EVENT)
642 s->data_block_counter = data_block_counter;
644 s->data_block_counter =
645 (data_block_counter + data_blocks) & 0xff;
647 if (queue_in_packet(s) < 0)
650 pcm = READ_ONCE(s->pcm);
651 if (pcm && pcm_frames > 0)
652 update_pcm_pointers(s, pcm, pcm_frames);
657 static int handle_in_packet_without_header(struct amdtp_stream *s,
658 unsigned int payload_length, unsigned int cycle,
662 unsigned int payload_quadlets;
663 unsigned int data_blocks;
664 struct snd_pcm_substream *pcm;
665 unsigned int pcm_frames;
667 buffer = s->buffer.packets[s->packet_index].buffer;
668 payload_quadlets = payload_length / 4;
669 data_blocks = payload_quadlets / s->data_block_quadlets;
671 trace_in_packet_without_header(s, cycle, payload_quadlets, data_blocks,
674 pcm_frames = s->process_data_blocks(s, buffer, data_blocks, NULL);
675 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
677 if (queue_in_packet(s) < 0)
680 pcm = READ_ONCE(s->pcm);
681 if (pcm && pcm_frames > 0)
682 update_pcm_pointers(s, pcm, pcm_frames);
688 * In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
689 * the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
690 * it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
692 static inline u32 compute_cycle_count(u32 tstamp)
694 return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff);
697 static inline u32 increment_cycle_count(u32 cycle, unsigned int addend)
700 if (cycle >= 8 * CYCLES_PER_SECOND)
701 cycle -= 8 * CYCLES_PER_SECOND;
705 static void out_stream_callback(struct fw_iso_context *context, u32 tstamp,
706 size_t header_length, void *header,
709 struct amdtp_stream *s = private_data;
710 unsigned int i, packets = header_length / 4;
713 if (s->packet_index < 0)
716 cycle = compute_cycle_count(tstamp);
718 /* Align to actual cycle count for the last packet. */
719 cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
721 for (i = 0; i < packets; ++i) {
722 cycle = increment_cycle_count(cycle, 1);
723 if (s->handle_packet(s, 0, cycle, i) < 0) {
724 s->packet_index = -1;
726 amdtp_stream_pcm_abort(s);
727 WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
732 fw_iso_context_queue_flush(s->context);
735 static void in_stream_callback(struct fw_iso_context *context, u32 tstamp,
736 size_t header_length, void *header,
739 struct amdtp_stream *s = private_data;
740 unsigned int i, packets;
741 unsigned int payload_length, max_payload_length;
742 __be32 *ctx_header = header;
744 if (s->packet_index < 0)
747 /* The number of packets in buffer */
748 packets = header_length / IR_HEADER_SIZE;
750 /* For buffer-over-run prevention. */
751 max_payload_length = s->max_payload_length;
753 for (i = 0; i < packets; i++) {
754 u32 iso_header = be32_to_cpu(ctx_header[0]);
757 tstamp = be32_to_cpu(ctx_header[1]) & HEADER_TSTAMP_MASK;
758 cycle = compute_cycle_count(tstamp);
760 /* The number of bytes in this packet */
761 payload_length = iso_header >> ISO_DATA_LENGTH_SHIFT;
762 if (payload_length > max_payload_length) {
763 dev_err(&s->unit->device,
764 "Detect jumbo payload: %04x %04x\n",
765 payload_length, max_payload_length);
769 if (s->handle_packet(s, payload_length, cycle, i) < 0)
772 ctx_header += IR_HEADER_SIZE / sizeof(__be32);
775 /* Queueing error or detecting invalid payload. */
777 s->packet_index = -1;
779 amdtp_stream_pcm_abort(s);
780 WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
784 fw_iso_context_queue_flush(s->context);
787 /* this is executed one time */
788 static void amdtp_stream_first_callback(struct fw_iso_context *context,
789 u32 tstamp, size_t header_length,
790 void *header, void *private_data)
792 struct amdtp_stream *s = private_data;
793 __be32 *ctx_header = header;
795 unsigned int packets;
798 * For in-stream, first packet has come.
799 * For out-stream, prepared to transmit first packet
801 s->callbacked = true;
802 wake_up(&s->callback_wait);
804 if (s->direction == AMDTP_IN_STREAM) {
805 tstamp = be32_to_cpu(ctx_header[1]) & HEADER_TSTAMP_MASK;
806 cycle = compute_cycle_count(tstamp);
808 context->callback.sc = in_stream_callback;
809 if (s->flags & CIP_NO_HEADER)
810 s->handle_packet = handle_in_packet_without_header;
812 s->handle_packet = handle_in_packet;
814 packets = header_length / 4;
815 cycle = compute_cycle_count(tstamp);
816 cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
817 context->callback.sc = out_stream_callback;
818 if (s->flags & CIP_NO_HEADER)
819 s->handle_packet = handle_out_packet_without_header;
821 s->handle_packet = handle_out_packet;
824 s->start_cycle = cycle;
826 context->callback.sc(context, tstamp, header_length, header, s);
830 * amdtp_stream_start - start transferring packets
831 * @s: the AMDTP stream to start
832 * @channel: the isochronous channel on the bus
833 * @speed: firewire speed code
835 * The stream cannot be started until it has been configured with
836 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
837 * device can be started.
839 int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
841 static const struct {
842 unsigned int data_block;
843 unsigned int syt_offset;
844 } initial_state[] = {
845 [CIP_SFC_32000] = { 4, 3072 },
846 [CIP_SFC_48000] = { 6, 1024 },
847 [CIP_SFC_96000] = { 12, 1024 },
848 [CIP_SFC_192000] = { 24, 1024 },
849 [CIP_SFC_44100] = { 0, 67 },
850 [CIP_SFC_88200] = { 0, 67 },
851 [CIP_SFC_176400] = { 0, 67 },
853 unsigned int header_size;
854 enum dma_data_direction dir;
857 mutex_lock(&s->mutex);
859 if (WARN_ON(amdtp_stream_running(s) ||
860 (s->data_block_quadlets < 1))) {
865 if (s->direction == AMDTP_IN_STREAM)
866 s->data_block_counter = UINT_MAX;
868 s->data_block_counter = 0;
869 s->data_block_state = initial_state[s->sfc].data_block;
870 s->syt_offset_state = initial_state[s->sfc].syt_offset;
871 s->last_syt_offset = TICKS_PER_CYCLE;
873 /* initialize packet buffer */
874 if (s->direction == AMDTP_IN_STREAM) {
875 dir = DMA_FROM_DEVICE;
876 type = FW_ISO_CONTEXT_RECEIVE;
877 header_size = IR_HEADER_SIZE;
880 type = FW_ISO_CONTEXT_TRANSMIT;
881 header_size = OUT_PACKET_HEADER_SIZE;
883 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
884 amdtp_stream_get_max_payload(s), dir);
888 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
889 type, channel, speed, header_size,
890 amdtp_stream_first_callback, s);
891 if (IS_ERR(s->context)) {
892 err = PTR_ERR(s->context);
894 dev_err(&s->unit->device,
895 "no free stream on this controller\n");
899 amdtp_stream_update(s);
901 if (s->direction == AMDTP_IN_STREAM)
902 s->max_payload_length = amdtp_stream_get_max_payload(s);
904 if (s->flags & CIP_NO_HEADER)
905 s->tag = TAG_NO_CIP_HEADER;
911 if (s->direction == AMDTP_IN_STREAM)
912 err = queue_in_packet(s);
914 err = queue_out_packet(s, 0);
917 } while (s->packet_index > 0);
919 /* NOTE: TAG1 matches CIP. This just affects in stream. */
920 tag = FW_ISO_CONTEXT_MATCH_TAG1;
921 if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER))
922 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
924 s->callbacked = false;
925 err = fw_iso_context_start(s->context, -1, 0, tag);
929 mutex_unlock(&s->mutex);
934 fw_iso_context_destroy(s->context);
935 s->context = ERR_PTR(-1);
937 iso_packets_buffer_destroy(&s->buffer, s->unit);
939 mutex_unlock(&s->mutex);
943 EXPORT_SYMBOL(amdtp_stream_start);
946 * amdtp_stream_pcm_pointer - get the PCM buffer position
947 * @s: the AMDTP stream that transports the PCM data
949 * Returns the current buffer position, in frames.
951 unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
954 * This function is called in software IRQ context of period_tasklet or
957 * When the software IRQ context was scheduled by software IRQ context
958 * of IR/IT contexts, queued packets were already handled. Therefore,
959 * no need to flush the queue in buffer anymore.
961 * When the process context reach here, some packets will be already
962 * queued in the buffer. These packets should be handled immediately
963 * to keep better granularity of PCM pointer.
965 * Later, the process context will sometimes schedules software IRQ
966 * context of the period_tasklet. Then, no need to flush the queue by
967 * the same reason as described for IR/IT contexts.
969 if (!in_interrupt() && amdtp_stream_running(s))
970 fw_iso_context_flush_completions(s->context);
972 return READ_ONCE(s->pcm_buffer_pointer);
974 EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
977 * amdtp_stream_pcm_ack - acknowledge queued PCM frames
978 * @s: the AMDTP stream that transfers the PCM frames
980 * Returns zero always.
982 int amdtp_stream_pcm_ack(struct amdtp_stream *s)
985 * Process isochronous packets for recent isochronous cycle to handle
988 if (amdtp_stream_running(s))
989 fw_iso_context_flush_completions(s->context);
993 EXPORT_SYMBOL(amdtp_stream_pcm_ack);
996 * amdtp_stream_update - update the stream after a bus reset
997 * @s: the AMDTP stream
999 void amdtp_stream_update(struct amdtp_stream *s)
1002 WRITE_ONCE(s->source_node_id_field,
1003 (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & CIP_SID_MASK);
1005 EXPORT_SYMBOL(amdtp_stream_update);
1008 * amdtp_stream_stop - stop sending packets
1009 * @s: the AMDTP stream to stop
1011 * All PCM and MIDI devices of the stream must be stopped before the stream
1012 * itself can be stopped.
1014 void amdtp_stream_stop(struct amdtp_stream *s)
1016 mutex_lock(&s->mutex);
1018 if (!amdtp_stream_running(s)) {
1019 mutex_unlock(&s->mutex);
1023 tasklet_kill(&s->period_tasklet);
1024 fw_iso_context_stop(s->context);
1025 fw_iso_context_destroy(s->context);
1026 s->context = ERR_PTR(-1);
1027 iso_packets_buffer_destroy(&s->buffer, s->unit);
1029 s->callbacked = false;
1031 mutex_unlock(&s->mutex);
1033 EXPORT_SYMBOL(amdtp_stream_stop);
1036 * amdtp_stream_pcm_abort - abort the running PCM device
1037 * @s: the AMDTP stream about to be stopped
1039 * If the isochronous stream needs to be stopped asynchronously, call this
1040 * function first to stop the PCM device.
1042 void amdtp_stream_pcm_abort(struct amdtp_stream *s)
1044 struct snd_pcm_substream *pcm;
1046 pcm = READ_ONCE(s->pcm);
1048 snd_pcm_stop_xrun(pcm);
1050 EXPORT_SYMBOL(amdtp_stream_pcm_abort);