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/firewire-constants.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <sound/pcm.h>
16 #include <sound/pcm_params.h>
17 #include "amdtp-stream.h"
19 #define TICKS_PER_CYCLE 3072
20 #define CYCLES_PER_SECOND 8000
21 #define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
23 #define OHCI_SECOND_MODULUS 8
25 /* Always support Linux tracing subsystem. */
26 #define CREATE_TRACE_POINTS
27 #include "amdtp-stream-trace.h"
29 #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */
31 /* isochronous header parameters */
32 #define ISO_DATA_LENGTH_SHIFT 16
33 #define TAG_NO_CIP_HEADER 0
36 // Common Isochronous Packet (CIP) header parameters. Use two quadlets CIP header when supported.
37 #define CIP_HEADER_QUADLETS 2
38 #define CIP_EOH_SHIFT 31
39 #define CIP_EOH (1u << CIP_EOH_SHIFT)
40 #define CIP_EOH_MASK 0x80000000
41 #define CIP_SID_SHIFT 24
42 #define CIP_SID_MASK 0x3f000000
43 #define CIP_DBS_MASK 0x00ff0000
44 #define CIP_DBS_SHIFT 16
45 #define CIP_SPH_MASK 0x00000400
46 #define CIP_SPH_SHIFT 10
47 #define CIP_DBC_MASK 0x000000ff
48 #define CIP_FMT_SHIFT 24
49 #define CIP_FMT_MASK 0x3f000000
50 #define CIP_FDF_MASK 0x00ff0000
51 #define CIP_FDF_SHIFT 16
52 #define CIP_SYT_MASK 0x0000ffff
53 #define CIP_SYT_NO_INFO 0xffff
55 #define CIP_HEADER_SIZE (sizeof(__be32) * CIP_HEADER_QUADLETS)
57 /* Audio and Music transfer protocol specific parameters */
58 #define CIP_FMT_AM 0x10
59 #define AMDTP_FDF_NO_DATA 0xff
61 // For iso header and tstamp.
62 #define IR_CTX_HEADER_DEFAULT_QUADLETS 2
64 #define IR_CTX_HEADER_SIZE_NO_CIP (sizeof(__be32) * IR_CTX_HEADER_DEFAULT_QUADLETS)
65 // Add two quadlets CIP header.
66 #define IR_CTX_HEADER_SIZE_CIP (IR_CTX_HEADER_SIZE_NO_CIP + CIP_HEADER_SIZE)
67 #define HEADER_TSTAMP_MASK 0x0000ffff
69 #define IT_PKT_HEADER_SIZE_CIP CIP_HEADER_SIZE
70 #define IT_PKT_HEADER_SIZE_NO_CIP 0 // Nothing.
72 // The initial firmware of OXFW970 can postpone transmission of packet during finishing
73 // asynchronous transaction. This module accepts 5 cycles to skip as maximum to avoid buffer
74 // overrun. Actual device can skip more, then this module stops the packet streaming.
75 #define IR_JUMBO_PAYLOAD_MAX_SKIP_CYCLES 5
77 static void pcm_period_work(struct work_struct *work);
80 * amdtp_stream_init - initialize an AMDTP stream structure
81 * @s: the AMDTP stream to initialize
82 * @unit: the target of the stream
83 * @dir: the direction of stream
84 * @flags: the details of the streaming protocol consist of cip_flags enumeration-constants.
85 * @fmt: the value of fmt field in CIP header
86 * @process_ctx_payloads: callback handler to process payloads of isoc context
87 * @protocol_size: the size to allocate newly for protocol
89 int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
90 enum amdtp_stream_direction dir, unsigned int flags,
92 amdtp_stream_process_ctx_payloads_t process_ctx_payloads,
93 unsigned int protocol_size)
95 if (process_ctx_payloads == NULL)
98 s->protocol = kzalloc(protocol_size, GFP_KERNEL);
105 s->context = ERR_PTR(-1);
106 mutex_init(&s->mutex);
107 INIT_WORK(&s->period_work, pcm_period_work);
110 init_waitqueue_head(&s->ready_wait);
111 s->callbacked = false;
114 s->process_ctx_payloads = process_ctx_payloads;
118 EXPORT_SYMBOL(amdtp_stream_init);
121 * amdtp_stream_destroy - free stream resources
122 * @s: the AMDTP stream to destroy
124 void amdtp_stream_destroy(struct amdtp_stream *s)
126 /* Not initialized. */
127 if (s->protocol == NULL)
130 WARN_ON(amdtp_stream_running(s));
132 mutex_destroy(&s->mutex);
134 EXPORT_SYMBOL(amdtp_stream_destroy);
136 const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
140 [CIP_SFC_88200] = 16,
141 [CIP_SFC_96000] = 16,
142 [CIP_SFC_176400] = 32,
143 [CIP_SFC_192000] = 32,
145 EXPORT_SYMBOL(amdtp_syt_intervals);
147 const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
148 [CIP_SFC_32000] = 32000,
149 [CIP_SFC_44100] = 44100,
150 [CIP_SFC_48000] = 48000,
151 [CIP_SFC_88200] = 88200,
152 [CIP_SFC_96000] = 96000,
153 [CIP_SFC_176400] = 176400,
154 [CIP_SFC_192000] = 192000,
156 EXPORT_SYMBOL(amdtp_rate_table);
158 static int apply_constraint_to_size(struct snd_pcm_hw_params *params,
159 struct snd_pcm_hw_rule *rule)
161 struct snd_interval *s = hw_param_interval(params, rule->var);
162 const struct snd_interval *r =
163 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
164 struct snd_interval t = {0};
165 unsigned int step = 0;
168 for (i = 0; i < CIP_SFC_COUNT; ++i) {
169 if (snd_interval_test(r, amdtp_rate_table[i]))
170 step = max(step, amdtp_syt_intervals[i]);
173 t.min = roundup(s->min, step);
174 t.max = rounddown(s->max, step);
177 return snd_interval_refine(s, &t);
181 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
182 * @s: the AMDTP stream, which must be initialized.
183 * @runtime: the PCM substream runtime
185 int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
186 struct snd_pcm_runtime *runtime)
188 struct snd_pcm_hardware *hw = &runtime->hw;
189 unsigned int ctx_header_size;
190 unsigned int maximum_usec_per_period;
193 hw->info = SNDRV_PCM_INFO_BATCH |
194 SNDRV_PCM_INFO_BLOCK_TRANSFER |
195 SNDRV_PCM_INFO_INTERLEAVED |
196 SNDRV_PCM_INFO_JOINT_DUPLEX |
197 SNDRV_PCM_INFO_MMAP |
198 SNDRV_PCM_INFO_MMAP_VALID;
200 /* SNDRV_PCM_INFO_BATCH */
202 hw->periods_max = UINT_MAX;
204 /* bytes for a frame */
205 hw->period_bytes_min = 4 * hw->channels_max;
207 /* Just to prevent from allocating much pages. */
208 hw->period_bytes_max = hw->period_bytes_min * 2048;
209 hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
211 // Linux driver for 1394 OHCI controller voluntarily flushes isoc
212 // context when total size of accumulated context header reaches
213 // PAGE_SIZE. This kicks work for the isoc context and brings
214 // callback in the middle of scheduled interrupts.
215 // Although AMDTP streams in the same domain use the same events per
216 // IRQ, use the largest size of context header between IT/IR contexts.
217 // Here, use the value of context header in IR context is for both
219 if (!(s->flags & CIP_NO_HEADER))
220 ctx_header_size = IR_CTX_HEADER_SIZE_CIP;
222 ctx_header_size = IR_CTX_HEADER_SIZE_NO_CIP;
223 maximum_usec_per_period = USEC_PER_SEC * PAGE_SIZE /
224 CYCLES_PER_SECOND / ctx_header_size;
226 // In IEC 61883-6, one isoc packet can transfer events up to the value
227 // of syt interval. This comes from the interval of isoc cycle. As 1394
228 // OHCI controller can generate hardware IRQ per isoc packet, the
229 // interval is 125 usec.
230 // However, there are two ways of transmission in IEC 61883-6; blocking
231 // and non-blocking modes. In blocking mode, the sequence of isoc packet
232 // includes 'empty' or 'NODATA' packets which include no event. In
233 // non-blocking mode, the number of events per packet is variable up to
235 // Due to the above protocol design, the minimum PCM frames per
236 // interrupt should be double of the value of syt interval, thus it is
238 err = snd_pcm_hw_constraint_minmax(runtime,
239 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
240 250, maximum_usec_per_period);
244 /* Non-Blocking stream has no more constraints */
245 if (!(s->flags & CIP_BLOCKING))
249 * One AMDTP packet can include some frames. In blocking mode, the
250 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
251 * depending on its sampling rate. For accurate period interrupt, it's
252 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
254 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
255 apply_constraint_to_size, NULL,
256 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
257 SNDRV_PCM_HW_PARAM_RATE, -1);
260 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
261 apply_constraint_to_size, NULL,
262 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
263 SNDRV_PCM_HW_PARAM_RATE, -1);
269 EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
272 * amdtp_stream_set_parameters - set stream parameters
273 * @s: the AMDTP stream to configure
274 * @rate: the sample rate
275 * @data_block_quadlets: the size of a data block in quadlet unit
277 * The parameters must be set before the stream is started, and must not be
278 * changed while the stream is running.
280 int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
281 unsigned int data_block_quadlets)
285 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
286 if (amdtp_rate_table[sfc] == rate)
289 if (sfc == ARRAY_SIZE(amdtp_rate_table))
293 s->data_block_quadlets = data_block_quadlets;
294 s->syt_interval = amdtp_syt_intervals[sfc];
296 // default buffering in the device.
297 if (s->direction == AMDTP_OUT_STREAM) {
298 s->ctx_data.rx.transfer_delay =
299 TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
301 if (s->flags & CIP_BLOCKING) {
302 // additional buffering needed to adjust for no-data
304 s->ctx_data.rx.transfer_delay +=
305 TICKS_PER_SECOND * s->syt_interval / rate;
311 EXPORT_SYMBOL(amdtp_stream_set_parameters);
313 // The CIP header is processed in context header apart from context payload.
314 static int amdtp_stream_get_max_ctx_payload_size(struct amdtp_stream *s)
316 unsigned int multiplier;
318 if (s->flags & CIP_JUMBO_PAYLOAD)
319 multiplier = IR_JUMBO_PAYLOAD_MAX_SKIP_CYCLES;
323 return s->syt_interval * s->data_block_quadlets * sizeof(__be32) * multiplier;
327 * amdtp_stream_get_max_payload - get the stream's packet size
328 * @s: the AMDTP stream
330 * This function must not be called before the stream has been configured
331 * with amdtp_stream_set_parameters().
333 unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
335 unsigned int cip_header_size;
337 if (!(s->flags & CIP_NO_HEADER))
338 cip_header_size = CIP_HEADER_SIZE;
342 return cip_header_size + amdtp_stream_get_max_ctx_payload_size(s);
344 EXPORT_SYMBOL(amdtp_stream_get_max_payload);
347 * amdtp_stream_pcm_prepare - prepare PCM device for running
348 * @s: the AMDTP stream
350 * This function should be called from the PCM device's .prepare callback.
352 void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
354 cancel_work_sync(&s->period_work);
355 s->pcm_buffer_pointer = 0;
356 s->pcm_period_pointer = 0;
358 EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
360 static unsigned int calculate_data_blocks(unsigned int *data_block_state,
361 bool is_blocking, bool is_no_info,
362 unsigned int syt_interval, enum cip_sfc sfc)
364 unsigned int data_blocks;
368 /* This module generate empty packet for 'no data'. */
372 data_blocks = syt_interval;
373 /* Non-blocking mode. */
375 if (!cip_sfc_is_base_44100(sfc)) {
376 // Sample_rate / 8000 is an integer, and precomputed.
377 data_blocks = *data_block_state;
379 unsigned int phase = *data_block_state;
382 * This calculates the number of data blocks per packet so that
383 * 1) the overall rate is correct and exactly synchronized to
385 * 2) packets with a rounded-up number of blocks occur as early
386 * as possible in the sequence (to prevent underruns of the
389 if (sfc == CIP_SFC_44100)
390 /* 6 6 5 6 5 6 5 ... */
391 data_blocks = 5 + ((phase & 1) ^
392 (phase == 0 || phase >= 40));
394 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
395 data_blocks = 11 * (sfc >> 1) + (phase == 0);
396 if (++phase >= (80 >> (sfc >> 1)))
398 *data_block_state = phase;
405 static unsigned int calculate_syt_offset(unsigned int *last_syt_offset,
406 unsigned int *syt_offset_state, enum cip_sfc sfc)
408 unsigned int syt_offset;
410 if (*last_syt_offset < TICKS_PER_CYCLE) {
411 if (!cip_sfc_is_base_44100(sfc))
412 syt_offset = *last_syt_offset + *syt_offset_state;
415 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
416 * n * SYT_INTERVAL * 24576000 / sample_rate
417 * Modulo TICKS_PER_CYCLE, the difference between successive
418 * elements is about 1386.23. Rounding the results of this
419 * formula to the SYT precision results in a sequence of
420 * differences that begins with:
421 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
422 * This code generates _exactly_ the same sequence.
424 unsigned int phase = *syt_offset_state;
425 unsigned int index = phase % 13;
427 syt_offset = *last_syt_offset;
428 syt_offset += 1386 + ((index && !(index & 3)) ||
432 *syt_offset_state = phase;
435 syt_offset = *last_syt_offset - TICKS_PER_CYCLE;
436 *last_syt_offset = syt_offset;
438 if (syt_offset >= TICKS_PER_CYCLE)
439 syt_offset = CIP_SYT_NO_INFO;
444 static void update_pcm_pointers(struct amdtp_stream *s,
445 struct snd_pcm_substream *pcm,
450 ptr = s->pcm_buffer_pointer + frames;
451 if (ptr >= pcm->runtime->buffer_size)
452 ptr -= pcm->runtime->buffer_size;
453 WRITE_ONCE(s->pcm_buffer_pointer, ptr);
455 s->pcm_period_pointer += frames;
456 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
457 s->pcm_period_pointer -= pcm->runtime->period_size;
458 queue_work(system_highpri_wq, &s->period_work);
462 static void pcm_period_work(struct work_struct *work)
464 struct amdtp_stream *s = container_of(work, struct amdtp_stream,
466 struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
469 snd_pcm_period_elapsed(pcm);
472 static int queue_packet(struct amdtp_stream *s, struct fw_iso_packet *params,
477 params->interrupt = sched_irq;
478 params->tag = s->tag;
481 err = fw_iso_context_queue(s->context, params, &s->buffer.iso_buffer,
482 s->buffer.packets[s->packet_index].offset);
484 dev_err(&s->unit->device, "queueing error: %d\n", err);
488 if (++s->packet_index >= s->queue_size)
494 static inline int queue_out_packet(struct amdtp_stream *s,
495 struct fw_iso_packet *params, bool sched_irq)
498 !!(params->header_length == 0 && params->payload_length == 0);
499 return queue_packet(s, params, sched_irq);
502 static inline int queue_in_packet(struct amdtp_stream *s,
503 struct fw_iso_packet *params)
505 // Queue one packet for IR context.
506 params->header_length = s->ctx_data.tx.ctx_header_size;
507 params->payload_length = s->ctx_data.tx.max_ctx_payload_length;
508 params->skip = false;
509 return queue_packet(s, params, false);
512 static void generate_cip_header(struct amdtp_stream *s, __be32 cip_header[2],
513 unsigned int data_block_counter, unsigned int syt)
515 cip_header[0] = cpu_to_be32(READ_ONCE(s->source_node_id_field) |
516 (s->data_block_quadlets << CIP_DBS_SHIFT) |
517 ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) |
519 cip_header[1] = cpu_to_be32(CIP_EOH |
520 ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
521 ((s->ctx_data.rx.fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
522 (syt & CIP_SYT_MASK));
525 static void build_it_pkt_header(struct amdtp_stream *s, unsigned int cycle,
526 struct fw_iso_packet *params, unsigned int header_length,
527 unsigned int data_blocks,
528 unsigned int data_block_counter,
529 unsigned int syt, unsigned int index)
531 unsigned int payload_length;
534 payload_length = data_blocks * sizeof(__be32) * s->data_block_quadlets;
535 params->payload_length = payload_length;
537 if (header_length > 0) {
538 cip_header = (__be32 *)params->header;
539 generate_cip_header(s, cip_header, data_block_counter, syt);
540 params->header_length = header_length;
545 trace_amdtp_packet(s, cycle, cip_header, payload_length + header_length, data_blocks,
546 data_block_counter, s->packet_index, index);
549 static int check_cip_header(struct amdtp_stream *s, const __be32 *buf,
550 unsigned int payload_length,
551 unsigned int *data_blocks,
552 unsigned int *data_block_counter, unsigned int *syt)
561 cip_header[0] = be32_to_cpu(buf[0]);
562 cip_header[1] = be32_to_cpu(buf[1]);
565 * This module supports 'Two-quadlet CIP header with SYT field'.
566 * For convenience, also check FMT field is AM824 or not.
568 if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
569 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) &&
570 (!(s->flags & CIP_HEADER_WITHOUT_EOH))) {
571 dev_info_ratelimited(&s->unit->device,
572 "Invalid CIP header for AMDTP: %08X:%08X\n",
573 cip_header[0], cip_header[1]);
577 /* Check valid protocol or not. */
578 sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT;
579 fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
580 if (sph != s->sph || fmt != s->fmt) {
581 dev_info_ratelimited(&s->unit->device,
582 "Detect unexpected protocol: %08x %08x\n",
583 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 == 0 || (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
592 unsigned int data_block_quadlets =
593 (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
594 /* avoid division by zero */
595 if (data_block_quadlets == 0) {
596 dev_err(&s->unit->device,
597 "Detect invalid value in dbs field: %08X\n",
601 if (s->flags & CIP_WRONG_DBS)
602 data_block_quadlets = s->data_block_quadlets;
604 *data_blocks = payload_length / sizeof(__be32) / data_block_quadlets;
607 /* Check data block counter continuity */
608 dbc = cip_header[0] & CIP_DBC_MASK;
609 if (*data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
610 *data_block_counter != UINT_MAX)
611 dbc = *data_block_counter;
613 if ((dbc == 0x00 && (s->flags & CIP_SKIP_DBC_ZERO_CHECK)) ||
614 *data_block_counter == UINT_MAX) {
616 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
617 lost = dbc != *data_block_counter;
619 unsigned int dbc_interval;
621 if (*data_blocks > 0 && s->ctx_data.tx.dbc_interval > 0)
622 dbc_interval = s->ctx_data.tx.dbc_interval;
624 dbc_interval = *data_blocks;
626 lost = dbc != ((*data_block_counter + dbc_interval) & 0xff);
630 dev_err(&s->unit->device,
631 "Detect discontinuity of CIP: %02X %02X\n",
632 *data_block_counter, dbc);
636 *data_block_counter = dbc;
638 if (!(s->flags & CIP_UNAWARE_SYT))
639 *syt = cip_header[1] & CIP_SYT_MASK;
644 static int parse_ir_ctx_header(struct amdtp_stream *s, unsigned int cycle,
645 const __be32 *ctx_header,
646 unsigned int *data_blocks,
647 unsigned int *data_block_counter,
648 unsigned int *syt, unsigned int packet_index, unsigned int index)
650 unsigned int payload_length;
651 const __be32 *cip_header;
652 unsigned int cip_header_size;
654 payload_length = be32_to_cpu(ctx_header[0]) >> ISO_DATA_LENGTH_SHIFT;
656 if (!(s->flags & CIP_NO_HEADER))
657 cip_header_size = CIP_HEADER_SIZE;
661 if (payload_length > cip_header_size + s->ctx_data.tx.max_ctx_payload_length) {
662 dev_err(&s->unit->device,
663 "Detect jumbo payload: %04x %04x\n",
664 payload_length, cip_header_size + s->ctx_data.tx.max_ctx_payload_length);
668 if (cip_header_size > 0) {
669 if (payload_length >= cip_header_size) {
672 cip_header = ctx_header + IR_CTX_HEADER_DEFAULT_QUADLETS;
673 err = check_cip_header(s, cip_header, payload_length - cip_header_size,
674 data_blocks, data_block_counter, syt);
678 // Handle the cycle so that empty packet arrives.
685 *data_blocks = payload_length / sizeof(__be32) / s->data_block_quadlets;
688 if (*data_block_counter == UINT_MAX)
689 *data_block_counter = 0;
692 trace_amdtp_packet(s, cycle, cip_header, payload_length, *data_blocks,
693 *data_block_counter, packet_index, index);
698 // In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
699 // the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
700 // it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
701 static inline u32 compute_ohci_cycle_count(__be32 ctx_header_tstamp)
703 u32 tstamp = be32_to_cpu(ctx_header_tstamp) & HEADER_TSTAMP_MASK;
704 return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff);
707 static inline u32 increment_ohci_cycle_count(u32 cycle, unsigned int addend)
710 if (cycle >= OHCI_SECOND_MODULUS * CYCLES_PER_SECOND)
711 cycle -= OHCI_SECOND_MODULUS * CYCLES_PER_SECOND;
715 static int compare_ohci_cycle_count(u32 lval, u32 rval)
719 else if (lval < rval && rval - lval < OHCI_SECOND_MODULUS * CYCLES_PER_SECOND / 2)
725 // Align to actual cycle count for the packet which is going to be scheduled.
726 // This module queued the same number of isochronous cycle as the size of queue
727 // to kip isochronous cycle, therefore it's OK to just increment the cycle by
728 // the size of queue for scheduled cycle.
729 static inline u32 compute_ohci_it_cycle(const __be32 ctx_header_tstamp,
730 unsigned int queue_size)
732 u32 cycle = compute_ohci_cycle_count(ctx_header_tstamp);
733 return increment_ohci_cycle_count(cycle, queue_size);
736 static int generate_device_pkt_descs(struct amdtp_stream *s,
737 struct pkt_desc *descs,
738 const __be32 *ctx_header,
739 unsigned int packets,
740 unsigned int *desc_count)
742 unsigned int next_cycle = s->next_cycle;
743 unsigned int dbc = s->data_block_counter;
744 unsigned int packet_index = s->packet_index;
745 unsigned int queue_size = s->queue_size;
750 for (i = 0; i < packets; ++i) {
751 struct pkt_desc *desc = descs + *desc_count;
754 unsigned int data_blocks;
757 cycle = compute_ohci_cycle_count(ctx_header[1]);
758 lost = (next_cycle != cycle);
760 if (s->flags & CIP_NO_HEADER) {
761 // Fireface skips transmission just for an isoc cycle corresponding
763 unsigned int prev_cycle = next_cycle;
765 next_cycle = increment_ohci_cycle_count(next_cycle, 1);
766 lost = (next_cycle != cycle);
768 // Prepare a description for the skipped cycle for
770 desc->cycle = prev_cycle;
772 desc->data_blocks = 0;
773 desc->data_block_counter = dbc;
774 desc->ctx_payload = NULL;
778 } else if (s->flags & CIP_JUMBO_PAYLOAD) {
779 // OXFW970 skips transmission for several isoc cycles during
780 // asynchronous transaction. The sequence replay is impossible due
782 unsigned int safe_cycle = increment_ohci_cycle_count(next_cycle,
783 IR_JUMBO_PAYLOAD_MAX_SKIP_CYCLES);
784 lost = (compare_ohci_cycle_count(safe_cycle, cycle) > 0);
787 dev_err(&s->unit->device, "Detect discontinuity of cycle: %d %d\n",
793 err = parse_ir_ctx_header(s, cycle, ctx_header, &data_blocks, &dbc, &syt,
800 desc->data_blocks = data_blocks;
801 desc->data_block_counter = dbc;
802 desc->ctx_payload = s->buffer.packets[packet_index].buffer;
804 if (!(s->flags & CIP_DBC_IS_END_EVENT))
805 dbc = (dbc + desc->data_blocks) & 0xff;
807 next_cycle = increment_ohci_cycle_count(next_cycle, 1);
809 ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header);
810 packet_index = (packet_index + 1) % queue_size;
813 s->next_cycle = next_cycle;
814 s->data_block_counter = dbc;
819 static unsigned int compute_syt(unsigned int syt_offset, unsigned int cycle,
820 unsigned int transfer_delay)
824 syt_offset += transfer_delay;
825 syt = ((cycle + syt_offset / TICKS_PER_CYCLE) << 12) |
826 (syt_offset % TICKS_PER_CYCLE);
827 return syt & CIP_SYT_MASK;
830 static void generate_pkt_descs(struct amdtp_stream *s, struct pkt_desc *descs,
831 const __be32 *ctx_header, unsigned int packets,
832 const struct seq_desc *seq_descs,
833 unsigned int seq_size)
835 unsigned int dbc = s->data_block_counter;
836 unsigned int seq_index = s->ctx_data.rx.seq_index;
837 bool aware_syt = !(s->flags & CIP_UNAWARE_SYT);
840 for (i = 0; i < packets; ++i) {
841 struct pkt_desc *desc = descs + i;
842 unsigned int index = (s->packet_index + i) % s->queue_size;
843 const struct seq_desc *seq = seq_descs + seq_index;
845 desc->cycle = compute_ohci_it_cycle(*ctx_header, s->queue_size);
847 if (aware_syt && seq->syt_offset != CIP_SYT_NO_INFO) {
848 desc->syt = compute_syt(seq->syt_offset, desc->cycle,
849 s->ctx_data.rx.transfer_delay);
851 desc->syt = CIP_SYT_NO_INFO;
854 desc->data_blocks = seq->data_blocks;
856 if (s->flags & CIP_DBC_IS_END_EVENT)
857 dbc = (dbc + desc->data_blocks) & 0xff;
859 desc->data_block_counter = dbc;
861 if (!(s->flags & CIP_DBC_IS_END_EVENT))
862 dbc = (dbc + desc->data_blocks) & 0xff;
864 desc->ctx_payload = s->buffer.packets[index].buffer;
866 seq_index = (seq_index + 1) % seq_size;
871 s->data_block_counter = dbc;
872 s->ctx_data.rx.seq_index = seq_index;
875 static inline void cancel_stream(struct amdtp_stream *s)
877 s->packet_index = -1;
878 if (current_work() == &s->period_work)
879 amdtp_stream_pcm_abort(s);
880 WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
883 static void process_ctx_payloads(struct amdtp_stream *s,
884 const struct pkt_desc *descs,
885 unsigned int packets)
887 struct snd_pcm_substream *pcm;
888 unsigned int pcm_frames;
890 pcm = READ_ONCE(s->pcm);
891 pcm_frames = s->process_ctx_payloads(s, descs, packets, pcm);
893 update_pcm_pointers(s, pcm, pcm_frames);
896 static void process_rx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
897 void *header, void *private_data)
899 struct amdtp_stream *s = private_data;
900 const struct amdtp_domain *d = s->domain;
901 const __be32 *ctx_header = header;
902 const unsigned int events_per_period = d->events_per_period;
903 unsigned int event_count = s->ctx_data.rx.event_count;
904 unsigned int pkt_header_length;
905 unsigned int packets;
908 if (s->packet_index < 0)
911 // Calculate the number of packets in buffer and check XRUN.
912 packets = header_length / sizeof(*ctx_header);
914 generate_pkt_descs(s, s->pkt_descs, ctx_header, packets, d->seq.descs,
917 process_ctx_payloads(s, s->pkt_descs, packets);
919 if (!(s->flags & CIP_NO_HEADER))
920 pkt_header_length = IT_PKT_HEADER_SIZE_CIP;
922 pkt_header_length = 0;
924 for (i = 0; i < packets; ++i) {
925 const struct pkt_desc *desc = s->pkt_descs + i;
927 struct fw_iso_packet params;
928 __be32 header[CIP_HEADER_QUADLETS];
929 } template = { {0}, {0} };
930 bool sched_irq = false;
932 build_it_pkt_header(s, desc->cycle, &template.params, pkt_header_length,
933 desc->data_blocks, desc->data_block_counter,
936 if (s == s->domain->irq_target) {
937 event_count += desc->data_blocks;
938 if (event_count >= events_per_period) {
939 event_count -= events_per_period;
944 if (queue_out_packet(s, &template.params, sched_irq) < 0) {
950 s->ctx_data.rx.event_count = event_count;
953 static void skip_rx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
954 void *header, void *private_data)
956 struct amdtp_stream *s = private_data;
957 struct amdtp_domain *d = s->domain;
958 const __be32 *ctx_header = header;
959 unsigned int packets;
963 if (s->packet_index < 0)
966 packets = header_length / sizeof(*ctx_header);
968 cycle = compute_ohci_it_cycle(ctx_header[packets - 1], s->queue_size);
969 s->next_cycle = increment_ohci_cycle_count(cycle, 1);
971 for (i = 0; i < packets; ++i) {
972 struct fw_iso_packet params = {
976 bool sched_irq = (s == d->irq_target && i == packets - 1);
978 if (queue_out_packet(s, ¶ms, sched_irq) < 0) {
985 static void irq_target_callback(struct fw_iso_context *context, u32 tstamp, size_t header_length,
986 void *header, void *private_data);
988 static void process_rx_packets_intermediately(struct fw_iso_context *context, u32 tstamp,
989 size_t header_length, void *header, void *private_data)
991 struct amdtp_stream *s = private_data;
992 struct amdtp_domain *d = s->domain;
993 __be32 *ctx_header = header;
994 const unsigned int queue_size = s->queue_size;
995 unsigned int packets;
998 if (s->packet_index < 0)
1001 packets = header_length / sizeof(*ctx_header);
1004 while (offset < packets) {
1005 unsigned int cycle = compute_ohci_it_cycle(ctx_header[offset], queue_size);
1007 if (compare_ohci_cycle_count(cycle, d->processing_cycle.rx_start) >= 0)
1014 unsigned int length = sizeof(*ctx_header) * offset;
1016 skip_rx_packets(context, tstamp, length, ctx_header, private_data);
1017 if (amdtp_streaming_error(s))
1020 ctx_header += offset;
1021 header_length -= length;
1024 if (offset < packets) {
1025 s->ready_processing = true;
1026 wake_up(&s->ready_wait);
1028 process_rx_packets(context, tstamp, header_length, ctx_header, private_data);
1029 if (amdtp_streaming_error(s))
1032 if (s == d->irq_target)
1033 s->context->callback.sc = irq_target_callback;
1035 s->context->callback.sc = process_rx_packets;
1039 static void process_tx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1040 void *header, void *private_data)
1042 struct amdtp_stream *s = private_data;
1043 __be32 *ctx_header = header;
1044 unsigned int packets;
1045 unsigned int desc_count;
1049 if (s->packet_index < 0)
1052 // Calculate the number of packets in buffer and check XRUN.
1053 packets = header_length / s->ctx_data.tx.ctx_header_size;
1056 err = generate_device_pkt_descs(s, s->pkt_descs, ctx_header, packets, &desc_count);
1058 if (err != -EAGAIN) {
1063 process_ctx_payloads(s, s->pkt_descs, desc_count);
1066 for (i = 0; i < packets; ++i) {
1067 struct fw_iso_packet params = {0};
1069 if (queue_in_packet(s, ¶ms) < 0) {
1076 static void drop_tx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1077 void *header, void *private_data)
1079 struct amdtp_stream *s = private_data;
1080 const __be32 *ctx_header = header;
1081 unsigned int packets;
1085 if (s->packet_index < 0)
1088 packets = header_length / s->ctx_data.tx.ctx_header_size;
1090 ctx_header += (packets - 1) * s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header);
1091 cycle = compute_ohci_cycle_count(ctx_header[1]);
1092 s->next_cycle = increment_ohci_cycle_count(cycle, 1);
1094 for (i = 0; i < packets; ++i) {
1095 struct fw_iso_packet params = {0};
1097 if (queue_in_packet(s, ¶ms) < 0) {
1104 static void process_tx_packets_intermediately(struct fw_iso_context *context, u32 tstamp,
1105 size_t header_length, void *header, void *private_data)
1107 struct amdtp_stream *s = private_data;
1108 struct amdtp_domain *d = s->domain;
1110 unsigned int packets;
1111 unsigned int offset;
1113 if (s->packet_index < 0)
1116 packets = header_length / s->ctx_data.tx.ctx_header_size;
1119 ctx_header = header;
1120 while (offset < packets) {
1121 unsigned int cycle = compute_ohci_cycle_count(ctx_header[1]);
1123 if (compare_ohci_cycle_count(cycle, d->processing_cycle.tx_start) >= 0)
1126 ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(__be32);
1130 ctx_header = header;
1133 size_t length = s->ctx_data.tx.ctx_header_size * offset;
1135 drop_tx_packets(context, tstamp, length, ctx_header, s);
1136 if (amdtp_streaming_error(s))
1139 ctx_header += length / sizeof(*ctx_header);
1140 header_length -= length;
1143 if (offset < packets) {
1144 s->ready_processing = true;
1145 wake_up(&s->ready_wait);
1147 process_tx_packets(context, tstamp, header_length, ctx_header, s);
1148 if (amdtp_streaming_error(s))
1151 context->callback.sc = process_tx_packets;
1155 static void pool_ideal_seq_descs(struct amdtp_domain *d, unsigned int packets)
1157 struct amdtp_stream *irq_target = d->irq_target;
1158 unsigned int seq_tail = d->seq.tail;
1159 unsigned int seq_size = d->seq.size;
1160 unsigned int min_avail;
1161 struct amdtp_stream *s;
1163 min_avail = d->seq.size;
1164 list_for_each_entry(s, &d->streams, list) {
1165 unsigned int seq_index;
1168 if (s->direction == AMDTP_IN_STREAM)
1171 seq_index = s->ctx_data.rx.seq_index;
1172 avail = d->seq.tail;
1173 if (seq_index > avail)
1174 avail += d->seq.size;
1177 if (avail < min_avail)
1181 while (min_avail < packets) {
1182 struct seq_desc *desc = d->seq.descs + seq_tail;
1184 desc->syt_offset = calculate_syt_offset(&d->last_syt_offset,
1185 &d->syt_offset_state, irq_target->sfc);
1186 desc->data_blocks = calculate_data_blocks(&d->data_block_state,
1187 !!(irq_target->flags & CIP_BLOCKING),
1188 desc->syt_offset == CIP_SYT_NO_INFO,
1189 irq_target->syt_interval, irq_target->sfc);
1192 seq_tail %= seq_size;
1197 d->seq.tail = seq_tail;
1200 static void process_ctxs_in_domain(struct amdtp_domain *d)
1202 struct amdtp_stream *s;
1204 list_for_each_entry(s, &d->streams, list) {
1205 if (s != d->irq_target && amdtp_stream_running(s))
1206 fw_iso_context_flush_completions(s->context);
1208 if (amdtp_streaming_error(s))
1214 if (amdtp_stream_running(d->irq_target))
1215 cancel_stream(d->irq_target);
1217 list_for_each_entry(s, &d->streams, list) {
1218 if (amdtp_stream_running(s))
1223 static void irq_target_callback(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1224 void *header, void *private_data)
1226 struct amdtp_stream *s = private_data;
1227 struct amdtp_domain *d = s->domain;
1228 unsigned int packets = header_length / sizeof(__be32);
1230 pool_ideal_seq_descs(d, packets);
1232 process_rx_packets(context, tstamp, header_length, header, private_data);
1233 process_ctxs_in_domain(d);
1236 static void irq_target_callback_intermediately(struct fw_iso_context *context, u32 tstamp,
1237 size_t header_length, void *header, void *private_data)
1239 struct amdtp_stream *s = private_data;
1240 struct amdtp_domain *d = s->domain;
1241 unsigned int packets = header_length / sizeof(__be32);
1243 pool_ideal_seq_descs(d, packets);
1245 process_rx_packets_intermediately(context, tstamp, header_length, header, private_data);
1246 process_ctxs_in_domain(d);
1249 static void irq_target_callback_skip(struct fw_iso_context *context, u32 tstamp,
1250 size_t header_length, void *header, void *private_data)
1252 struct amdtp_stream *s = private_data;
1253 struct amdtp_domain *d = s->domain;
1256 skip_rx_packets(context, tstamp, header_length, header, private_data);
1257 process_ctxs_in_domain(d);
1259 // Decide the cycle count to begin processing content of packet in IT contexts. All of IT
1260 // contexts are expected to start and get callback when reaching here.
1261 cycle = s->next_cycle;
1262 list_for_each_entry(s, &d->streams, list) {
1263 if (s->direction != AMDTP_OUT_STREAM)
1266 if (compare_ohci_cycle_count(s->next_cycle, cycle) > 0)
1267 cycle = s->next_cycle;
1269 if (s == d->irq_target)
1270 s->context->callback.sc = irq_target_callback_intermediately;
1272 s->context->callback.sc = process_rx_packets_intermediately;
1275 d->processing_cycle.rx_start = cycle;
1278 // this is executed one time.
1279 static void amdtp_stream_first_callback(struct fw_iso_context *context,
1280 u32 tstamp, size_t header_length,
1281 void *header, void *private_data)
1283 struct amdtp_stream *s = private_data;
1284 struct amdtp_domain *d = s->domain;
1285 const __be32 *ctx_header = header;
1288 // For in-stream, first packet has come.
1289 // For out-stream, prepared to transmit first packet
1290 s->callbacked = true;
1292 if (s->direction == AMDTP_IN_STREAM) {
1293 cycle = compute_ohci_cycle_count(ctx_header[1]);
1295 context->callback.sc = drop_tx_packets;
1297 cycle = compute_ohci_it_cycle(*ctx_header, s->queue_size);
1299 if (s == d->irq_target)
1300 context->callback.sc = irq_target_callback_skip;
1302 context->callback.sc = skip_rx_packets;
1305 context->callback.sc(context, tstamp, header_length, header, s);
1307 // Decide the cycle count to begin processing content of packet in IR contexts.
1308 if (s->direction == AMDTP_IN_STREAM) {
1309 unsigned int stream_count = 0;
1310 unsigned int callbacked_count = 0;
1312 list_for_each_entry(s, &d->streams, list) {
1313 if (s->direction == AMDTP_IN_STREAM) {
1320 if (stream_count == callbacked_count) {
1321 unsigned int next_cycle;
1323 list_for_each_entry(s, &d->streams, list) {
1324 if (s->direction != AMDTP_IN_STREAM)
1327 next_cycle = increment_ohci_cycle_count(s->next_cycle,
1328 d->processing_cycle.tx_init_skip);
1329 if (compare_ohci_cycle_count(next_cycle, cycle) > 0)
1332 s->context->callback.sc = process_tx_packets_intermediately;
1335 d->processing_cycle.tx_start = cycle;
1341 * amdtp_stream_start - start transferring packets
1342 * @s: the AMDTP stream to start
1343 * @channel: the isochronous channel on the bus
1344 * @speed: firewire speed code
1345 * @queue_size: The number of packets in the queue.
1346 * @idle_irq_interval: the interval to queue packet during initial state.
1348 * The stream cannot be started until it has been configured with
1349 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
1350 * device can be started.
1352 static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed,
1353 unsigned int queue_size, unsigned int idle_irq_interval)
1355 bool is_irq_target = (s == s->domain->irq_target);
1356 unsigned int ctx_header_size;
1357 unsigned int max_ctx_payload_size;
1358 enum dma_data_direction dir;
1361 mutex_lock(&s->mutex);
1363 if (WARN_ON(amdtp_stream_running(s) ||
1364 (s->data_block_quadlets < 1))) {
1369 if (s->direction == AMDTP_IN_STREAM) {
1370 // NOTE: IT context should be used for constant IRQ.
1371 if (is_irq_target) {
1376 s->data_block_counter = UINT_MAX;
1378 s->data_block_counter = 0;
1381 // initialize packet buffer.
1382 if (s->direction == AMDTP_IN_STREAM) {
1383 dir = DMA_FROM_DEVICE;
1384 type = FW_ISO_CONTEXT_RECEIVE;
1385 if (!(s->flags & CIP_NO_HEADER))
1386 ctx_header_size = IR_CTX_HEADER_SIZE_CIP;
1388 ctx_header_size = IR_CTX_HEADER_SIZE_NO_CIP;
1390 dir = DMA_TO_DEVICE;
1391 type = FW_ISO_CONTEXT_TRANSMIT;
1392 ctx_header_size = 0; // No effect for IT context.
1394 max_ctx_payload_size = amdtp_stream_get_max_ctx_payload_size(s);
1396 err = iso_packets_buffer_init(&s->buffer, s->unit, queue_size, max_ctx_payload_size, dir);
1399 s->queue_size = queue_size;
1401 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
1402 type, channel, speed, ctx_header_size,
1403 amdtp_stream_first_callback, s);
1404 if (IS_ERR(s->context)) {
1405 err = PTR_ERR(s->context);
1407 dev_err(&s->unit->device,
1408 "no free stream on this controller\n");
1412 amdtp_stream_update(s);
1414 if (s->direction == AMDTP_IN_STREAM) {
1415 s->ctx_data.tx.max_ctx_payload_length = max_ctx_payload_size;
1416 s->ctx_data.tx.ctx_header_size = ctx_header_size;
1418 s->ctx_data.rx.seq_index = 0;
1419 s->ctx_data.rx.event_count = 0;
1422 if (s->flags & CIP_NO_HEADER)
1423 s->tag = TAG_NO_CIP_HEADER;
1427 s->pkt_descs = kcalloc(s->queue_size, sizeof(*s->pkt_descs),
1429 if (!s->pkt_descs) {
1434 s->packet_index = 0;
1436 struct fw_iso_packet params;
1438 if (s->direction == AMDTP_IN_STREAM) {
1439 err = queue_in_packet(s, ¶ms);
1441 bool sched_irq = false;
1443 params.header_length = 0;
1444 params.payload_length = 0;
1446 if (is_irq_target) {
1447 sched_irq = !((s->packet_index + 1) %
1451 err = queue_out_packet(s, ¶ms, sched_irq);
1455 } while (s->packet_index > 0);
1457 /* NOTE: TAG1 matches CIP. This just affects in stream. */
1458 tag = FW_ISO_CONTEXT_MATCH_TAG1;
1459 if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER))
1460 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
1462 s->callbacked = false;
1463 s->ready_processing = false;
1464 err = fw_iso_context_start(s->context, -1, 0, tag);
1468 mutex_unlock(&s->mutex);
1472 kfree(s->pkt_descs);
1474 fw_iso_context_destroy(s->context);
1475 s->context = ERR_PTR(-1);
1477 iso_packets_buffer_destroy(&s->buffer, s->unit);
1479 mutex_unlock(&s->mutex);
1485 * amdtp_domain_stream_pcm_pointer - get the PCM buffer position
1486 * @d: the AMDTP domain.
1487 * @s: the AMDTP stream that transports the PCM data
1489 * Returns the current buffer position, in frames.
1491 unsigned long amdtp_domain_stream_pcm_pointer(struct amdtp_domain *d,
1492 struct amdtp_stream *s)
1494 struct amdtp_stream *irq_target = d->irq_target;
1496 if (irq_target && amdtp_stream_running(irq_target)) {
1497 // This function is called in software IRQ context of
1498 // period_work or process context.
1500 // When the software IRQ context was scheduled by software IRQ
1501 // context of IT contexts, queued packets were already handled.
1502 // Therefore, no need to flush the queue in buffer furthermore.
1504 // When the process context reach here, some packets will be
1505 // already queued in the buffer. These packets should be handled
1506 // immediately to keep better granularity of PCM pointer.
1508 // Later, the process context will sometimes schedules software
1509 // IRQ context of the period_work. Then, no need to flush the
1510 // queue by the same reason as described in the above
1511 if (current_work() != &s->period_work) {
1512 // Queued packet should be processed without any kernel
1513 // preemption to keep latency against bus cycle.
1515 fw_iso_context_flush_completions(irq_target->context);
1520 return READ_ONCE(s->pcm_buffer_pointer);
1522 EXPORT_SYMBOL_GPL(amdtp_domain_stream_pcm_pointer);
1525 * amdtp_domain_stream_pcm_ack - acknowledge queued PCM frames
1526 * @d: the AMDTP domain.
1527 * @s: the AMDTP stream that transfers the PCM frames
1529 * Returns zero always.
1531 int amdtp_domain_stream_pcm_ack(struct amdtp_domain *d, struct amdtp_stream *s)
1533 struct amdtp_stream *irq_target = d->irq_target;
1535 // Process isochronous packets for recent isochronous cycle to handle
1536 // queued PCM frames.
1537 if (irq_target && amdtp_stream_running(irq_target)) {
1538 // Queued packet should be processed without any kernel
1539 // preemption to keep latency against bus cycle.
1541 fw_iso_context_flush_completions(irq_target->context);
1547 EXPORT_SYMBOL_GPL(amdtp_domain_stream_pcm_ack);
1550 * amdtp_stream_update - update the stream after a bus reset
1551 * @s: the AMDTP stream
1553 void amdtp_stream_update(struct amdtp_stream *s)
1556 WRITE_ONCE(s->source_node_id_field,
1557 (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & CIP_SID_MASK);
1559 EXPORT_SYMBOL(amdtp_stream_update);
1562 * amdtp_stream_stop - stop sending packets
1563 * @s: the AMDTP stream to stop
1565 * All PCM and MIDI devices of the stream must be stopped before the stream
1566 * itself can be stopped.
1568 static void amdtp_stream_stop(struct amdtp_stream *s)
1570 mutex_lock(&s->mutex);
1572 if (!amdtp_stream_running(s)) {
1573 mutex_unlock(&s->mutex);
1577 cancel_work_sync(&s->period_work);
1578 fw_iso_context_stop(s->context);
1579 fw_iso_context_destroy(s->context);
1580 s->context = ERR_PTR(-1);
1581 iso_packets_buffer_destroy(&s->buffer, s->unit);
1582 kfree(s->pkt_descs);
1584 s->callbacked = false;
1586 mutex_unlock(&s->mutex);
1590 * amdtp_stream_pcm_abort - abort the running PCM device
1591 * @s: the AMDTP stream about to be stopped
1593 * If the isochronous stream needs to be stopped asynchronously, call this
1594 * function first to stop the PCM device.
1596 void amdtp_stream_pcm_abort(struct amdtp_stream *s)
1598 struct snd_pcm_substream *pcm;
1600 pcm = READ_ONCE(s->pcm);
1602 snd_pcm_stop_xrun(pcm);
1604 EXPORT_SYMBOL(amdtp_stream_pcm_abort);
1607 * amdtp_domain_init - initialize an AMDTP domain structure
1608 * @d: the AMDTP domain to initialize.
1610 int amdtp_domain_init(struct amdtp_domain *d)
1612 INIT_LIST_HEAD(&d->streams);
1614 d->events_per_period = 0;
1616 d->seq.descs = NULL;
1620 EXPORT_SYMBOL_GPL(amdtp_domain_init);
1623 * amdtp_domain_destroy - destroy an AMDTP domain structure
1624 * @d: the AMDTP domain to destroy.
1626 void amdtp_domain_destroy(struct amdtp_domain *d)
1628 // At present nothing to do.
1631 EXPORT_SYMBOL_GPL(amdtp_domain_destroy);
1634 * amdtp_domain_add_stream - register isoc context into the domain.
1635 * @d: the AMDTP domain.
1636 * @s: the AMDTP stream.
1637 * @channel: the isochronous channel on the bus.
1638 * @speed: firewire speed code.
1640 int amdtp_domain_add_stream(struct amdtp_domain *d, struct amdtp_stream *s,
1641 int channel, int speed)
1643 struct amdtp_stream *tmp;
1645 list_for_each_entry(tmp, &d->streams, list) {
1650 list_add(&s->list, &d->streams);
1652 s->channel = channel;
1658 EXPORT_SYMBOL_GPL(amdtp_domain_add_stream);
1661 * amdtp_domain_start - start sending packets for isoc context in the domain.
1662 * @d: the AMDTP domain.
1663 * @tx_init_skip_cycles: the number of cycles to skip processing packets at initial stage of IR
1666 int amdtp_domain_start(struct amdtp_domain *d, unsigned int tx_init_skip_cycles)
1668 static const struct {
1669 unsigned int data_block;
1670 unsigned int syt_offset;
1671 } *entry, initial_state[] = {
1672 [CIP_SFC_32000] = { 4, 3072 },
1673 [CIP_SFC_48000] = { 6, 1024 },
1674 [CIP_SFC_96000] = { 12, 1024 },
1675 [CIP_SFC_192000] = { 24, 1024 },
1676 [CIP_SFC_44100] = { 0, 67 },
1677 [CIP_SFC_88200] = { 0, 67 },
1678 [CIP_SFC_176400] = { 0, 67 },
1680 unsigned int events_per_buffer = d->events_per_buffer;
1681 unsigned int events_per_period = d->events_per_period;
1682 unsigned int queue_size;
1683 struct amdtp_stream *s;
1686 // Select an IT context as IRQ target.
1687 list_for_each_entry(s, &d->streams, list) {
1688 if (s->direction == AMDTP_OUT_STREAM)
1695 d->processing_cycle.tx_init_skip = tx_init_skip_cycles;
1697 // This is a case that AMDTP streams in domain run just for MIDI
1698 // substream. Use the number of events equivalent to 10 msec as
1699 // interval of hardware IRQ.
1700 if (events_per_period == 0)
1701 events_per_period = amdtp_rate_table[d->irq_target->sfc] / 100;
1702 if (events_per_buffer == 0)
1703 events_per_buffer = events_per_period * 3;
1705 queue_size = DIV_ROUND_UP(CYCLES_PER_SECOND * events_per_buffer,
1706 amdtp_rate_table[d->irq_target->sfc]);
1708 d->seq.descs = kcalloc(queue_size, sizeof(*d->seq.descs), GFP_KERNEL);
1711 d->seq.size = queue_size;
1714 entry = &initial_state[s->sfc];
1715 d->data_block_state = entry->data_block;
1716 d->syt_offset_state = entry->syt_offset;
1717 d->last_syt_offset = TICKS_PER_CYCLE;
1719 list_for_each_entry(s, &d->streams, list) {
1720 unsigned int idle_irq_interval = 0;
1722 if (s->direction == AMDTP_OUT_STREAM && s == d->irq_target) {
1723 idle_irq_interval = DIV_ROUND_UP(CYCLES_PER_SECOND * events_per_period,
1724 amdtp_rate_table[d->irq_target->sfc]);
1727 // Starts immediately but actually DMA context starts several hundred cycles later.
1728 err = amdtp_stream_start(s, s->channel, s->speed, queue_size, idle_irq_interval);
1735 list_for_each_entry(s, &d->streams, list)
1736 amdtp_stream_stop(s);
1737 kfree(d->seq.descs);
1738 d->seq.descs = NULL;
1741 EXPORT_SYMBOL_GPL(amdtp_domain_start);
1744 * amdtp_domain_stop - stop sending packets for isoc context in the same domain.
1745 * @d: the AMDTP domain to which the isoc contexts belong.
1747 void amdtp_domain_stop(struct amdtp_domain *d)
1749 struct amdtp_stream *s, *next;
1752 amdtp_stream_stop(d->irq_target);
1754 list_for_each_entry_safe(s, next, &d->streams, list) {
1757 if (s != d->irq_target)
1758 amdtp_stream_stop(s);
1761 d->events_per_period = 0;
1762 d->irq_target = NULL;
1764 kfree(d->seq.descs);
1765 d->seq.descs = NULL;
1767 EXPORT_SYMBOL_GPL(amdtp_domain_stop);