• Home
  • Raw
  • Download

Lines Matching +full:sfc +full:- +full:no +full:- +full:dma

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
12 #include <linux/firewire-constants.h>
17 #include "amdtp-stream.h"
27 #include "amdtp-stream-trace.h"
83 * amdtp_stream_init - initialize an AMDTP stream structure
87 * @flags: the details of the streaming protocol consist of cip_flags enumeration-constants.
99 return -EINVAL; in amdtp_stream_init()
101 s->protocol = kzalloc(protocol_size, GFP_KERNEL); in amdtp_stream_init()
102 if (!s->protocol) in amdtp_stream_init()
103 return -ENOMEM; in amdtp_stream_init()
105 s->unit = unit; in amdtp_stream_init()
106 s->direction = dir; in amdtp_stream_init()
107 s->flags = flags; in amdtp_stream_init()
108 s->context = ERR_PTR(-1); in amdtp_stream_init()
109 mutex_init(&s->mutex); in amdtp_stream_init()
110 INIT_WORK(&s->period_work, pcm_period_work); in amdtp_stream_init()
111 s->packet_index = 0; in amdtp_stream_init()
113 init_waitqueue_head(&s->ready_wait); in amdtp_stream_init()
115 s->fmt = fmt; in amdtp_stream_init()
116 s->process_ctx_payloads = process_ctx_payloads; in amdtp_stream_init()
123 * amdtp_stream_destroy - free stream resources
129 if (s->protocol == NULL) in amdtp_stream_destroy()
133 kfree(s->protocol); in amdtp_stream_destroy()
134 mutex_destroy(&s->mutex); in amdtp_stream_destroy()
163 struct snd_interval *s = hw_param_interval(params, rule->var); in apply_constraint_to_size()
176 return -EINVAL; in apply_constraint_to_size()
178 t.min = roundup(s->min, step); in apply_constraint_to_size()
179 t.max = rounddown(s->max, step); in apply_constraint_to_size()
186 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
193 struct snd_pcm_hardware *hw = &runtime->hw; in amdtp_stream_add_pcm_hw_constraints()
198 hw->info = SNDRV_PCM_INFO_BLOCK_TRANSFER | in amdtp_stream_add_pcm_hw_constraints()
205 hw->periods_min = 2; in amdtp_stream_add_pcm_hw_constraints()
206 hw->periods_max = UINT_MAX; in amdtp_stream_add_pcm_hw_constraints()
209 hw->period_bytes_min = 4 * hw->channels_max; in amdtp_stream_add_pcm_hw_constraints()
212 hw->period_bytes_max = hw->period_bytes_min * 2048; in amdtp_stream_add_pcm_hw_constraints()
213 hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min; in amdtp_stream_add_pcm_hw_constraints()
223 if (!(s->flags & CIP_NO_HEADER)) in amdtp_stream_add_pcm_hw_constraints()
230 // In IEC 61883-6, one isoc packet can transfer events up to the value in amdtp_stream_add_pcm_hw_constraints()
234 // However, there are two ways of transmission in IEC 61883-6; blocking in amdtp_stream_add_pcm_hw_constraints()
235 // and non-blocking modes. In blocking mode, the sequence of isoc packet in amdtp_stream_add_pcm_hw_constraints()
236 // includes 'empty' or 'NODATA' packets which include no event. In in amdtp_stream_add_pcm_hw_constraints()
237 // non-blocking mode, the number of events per packet is variable up to in amdtp_stream_add_pcm_hw_constraints()
248 /* Non-Blocking stream has no more constraints */ in amdtp_stream_add_pcm_hw_constraints()
249 if (!(s->flags & CIP_BLOCKING)) in amdtp_stream_add_pcm_hw_constraints()
261 SNDRV_PCM_HW_PARAM_RATE, -1); in amdtp_stream_add_pcm_hw_constraints()
267 SNDRV_PCM_HW_PARAM_RATE, -1); in amdtp_stream_add_pcm_hw_constraints()
276 * amdtp_stream_set_parameters - set stream parameters
289 unsigned int sfc; in amdtp_stream_set_parameters() local
291 for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) { in amdtp_stream_set_parameters()
292 if (amdtp_rate_table[sfc] == rate) in amdtp_stream_set_parameters()
295 if (sfc == ARRAY_SIZE(amdtp_rate_table)) in amdtp_stream_set_parameters()
296 return -EINVAL; in amdtp_stream_set_parameters()
298 s->sfc = sfc; in amdtp_stream_set_parameters()
299 s->data_block_quadlets = data_block_quadlets; in amdtp_stream_set_parameters()
300 s->syt_interval = amdtp_syt_intervals[sfc]; in amdtp_stream_set_parameters()
303 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE; in amdtp_stream_set_parameters()
305 // additional buffering needed to adjust for no-data packets. in amdtp_stream_set_parameters()
306 if (s->flags & CIP_BLOCKING) in amdtp_stream_set_parameters()
307 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate; in amdtp_stream_set_parameters()
309 s->pcm_frame_multiplier = pcm_frame_multiplier; in amdtp_stream_set_parameters()
320 if (s->flags & CIP_JUMBO_PAYLOAD) in amdtp_stream_get_max_ctx_payload_size()
325 return s->syt_interval * s->data_block_quadlets * sizeof(__be32) * multiplier; in amdtp_stream_get_max_ctx_payload_size()
329 * amdtp_stream_get_max_payload - get the stream's packet size
339 if (!(s->flags & CIP_NO_HEADER)) in amdtp_stream_get_max_payload()
349 * amdtp_stream_pcm_prepare - prepare PCM device for running
356 cancel_work_sync(&s->period_work); in amdtp_stream_pcm_prepare()
357 s->pcm_buffer_pointer = 0; in amdtp_stream_pcm_prepare()
358 s->pcm_period_pointer = 0; in amdtp_stream_pcm_prepare()
363 list_prev_entry_circular(desc, &s->packet_descs_list, link)
368 const unsigned int syt_interval = s->syt_interval; in pool_blocking_data_blocks()
374 if (desc->syt_offset != CIP_SYT_NO_INFO) in pool_blocking_data_blocks()
375 desc->data_blocks = syt_interval; in pool_blocking_data_blocks()
377 desc->data_blocks = 0; in pool_blocking_data_blocks()
387 const enum cip_sfc sfc = s->sfc; in pool_ideal_nonblocking_data_blocks() local
388 unsigned int state = s->ctx_data.rx.data_block_state; in pool_ideal_nonblocking_data_blocks()
394 if (!cip_sfc_is_base_44100(sfc)) { in pool_ideal_nonblocking_data_blocks()
396 desc->data_blocks = state; in pool_ideal_nonblocking_data_blocks()
404 * 2) packets with a rounded-up number of blocks occur as early in pool_ideal_nonblocking_data_blocks()
408 if (sfc == CIP_SFC_44100) in pool_ideal_nonblocking_data_blocks()
410 desc->data_blocks = 5 + ((phase & 1) ^ (phase == 0 || phase >= 40)); in pool_ideal_nonblocking_data_blocks()
413 desc->data_blocks = 11 * (sfc >> 1) + (phase == 0); in pool_ideal_nonblocking_data_blocks()
414 if (++phase >= (80 >> (sfc >> 1))) in pool_ideal_nonblocking_data_blocks()
422 s->ctx_data.rx.data_block_state = state; in pool_ideal_nonblocking_data_blocks()
426 unsigned int *syt_offset_state, enum cip_sfc sfc) in calculate_syt_offset() argument
431 if (!cip_sfc_is_base_44100(sfc)) in calculate_syt_offset()
455 syt_offset = *last_syt_offset - TICKS_PER_CYCLE; in calculate_syt_offset()
467 const enum cip_sfc sfc = s->sfc; in pool_ideal_syt_offsets() local
468 unsigned int last = s->ctx_data.rx.last_syt_offset; in pool_ideal_syt_offsets()
469 unsigned int state = s->ctx_data.rx.syt_offset_state; in pool_ideal_syt_offsets()
475 desc->syt_offset = calculate_syt_offset(&last, &state, sfc); in pool_ideal_syt_offsets()
480 s->ctx_data.rx.last_syt_offset = last; in pool_ideal_syt_offsets()
481 s->ctx_data.rx.syt_offset_state = state; in pool_ideal_syt_offsets()
494 syt_cycle_lo -= cycle_lo; in compute_syt_offset()
502 return syt_offset - transfer_delay; in compute_syt_offset()
511 const unsigned int cache_size = s->ctx_data.tx.cache.size; in calculate_cached_cycle_count()
512 unsigned int cycles = s->ctx_data.tx.cache.pos; in calculate_cached_cycle_count()
516 cycles -= head; in calculate_cached_cycle_count()
523 const unsigned int transfer_delay = s->transfer_delay; in cache_seq()
524 const unsigned int cache_size = s->ctx_data.tx.cache.size; in cache_seq()
525 struct seq_desc *cache = s->ctx_data.tx.cache.descs; in cache_seq()
526 unsigned int cache_pos = s->ctx_data.tx.cache.pos; in cache_seq()
527 bool aware_syt = !(s->flags & CIP_UNAWARE_SYT); in cache_seq()
533 if (aware_syt && src->syt != CIP_SYT_NO_INFO) in cache_seq()
534 dst->syt_offset = compute_syt_offset(src->syt, src->cycle, transfer_delay); in cache_seq()
536 dst->syt_offset = CIP_SYT_NO_INFO; in cache_seq()
537 dst->data_blocks = src->data_blocks; in cache_seq()
543 s->ctx_data.tx.cache.pos = cache_pos; in cache_seq()
551 if (s->flags & CIP_BLOCKING) in pool_ideal_seq_descs()
560 struct amdtp_stream *target = s->ctx_data.rx.replay_target; in pool_replayed_seq()
561 const struct seq_desc *cache = target->ctx_data.tx.cache.descs; in pool_replayed_seq()
562 const unsigned int cache_size = target->ctx_data.tx.cache.size; in pool_replayed_seq()
563 unsigned int cache_pos = s->ctx_data.rx.cache_pos; in pool_replayed_seq()
572 s->ctx_data.rx.cache_pos = cache_pos; in pool_replayed_seq()
578 struct amdtp_domain *d = s->domain; in pool_seq_descs()
582 if (!d->replay.enable || !s->ctx_data.rx.replay_target) { in pool_seq_descs()
585 if (!d->replay.on_the_fly) { in pool_seq_descs()
588 struct amdtp_stream *tx = s->ctx_data.rx.replay_target; in pool_seq_descs()
589 const unsigned int cache_size = tx->ctx_data.tx.cache.size; in pool_seq_descs()
590 const unsigned int cache_pos = s->ctx_data.rx.cache_pos; in pool_seq_descs()
609 ptr = s->pcm_buffer_pointer + frames; in update_pcm_pointers()
610 if (ptr >= pcm->runtime->buffer_size) in update_pcm_pointers()
611 ptr -= pcm->runtime->buffer_size; in update_pcm_pointers()
612 WRITE_ONCE(s->pcm_buffer_pointer, ptr); in update_pcm_pointers()
614 s->pcm_period_pointer += frames; in update_pcm_pointers()
615 if (s->pcm_period_pointer >= pcm->runtime->period_size) { in update_pcm_pointers()
616 s->pcm_period_pointer -= pcm->runtime->period_size; in update_pcm_pointers()
621 if (!pcm->runtime->no_period_wakeup) in update_pcm_pointers()
622 queue_work(system_highpri_wq, &s->period_work); in update_pcm_pointers()
630 struct snd_pcm_substream *pcm = READ_ONCE(s->pcm); in pcm_period_work()
641 params->interrupt = sched_irq; in queue_packet()
642 params->tag = s->tag; in queue_packet()
643 params->sy = 0; in queue_packet()
645 err = fw_iso_context_queue(s->context, params, &s->buffer.iso_buffer, in queue_packet()
646 s->buffer.packets[s->packet_index].offset); in queue_packet()
648 dev_err(&s->unit->device, "queueing error: %d\n", err); in queue_packet()
652 if (++s->packet_index >= s->queue_size) in queue_packet()
653 s->packet_index = 0; in queue_packet()
661 params->skip = in queue_out_packet()
662 !!(params->header_length == 0 && params->payload_length == 0); in queue_out_packet()
670 params->header_length = s->ctx_data.tx.ctx_header_size; in queue_in_packet()
671 params->payload_length = s->ctx_data.tx.max_ctx_payload_length; in queue_in_packet()
672 params->skip = false; in queue_in_packet()
679 cip_header[0] = cpu_to_be32(READ_ONCE(s->source_node_id_field) | in generate_cip_header()
680 (s->data_block_quadlets << CIP_DBS_SHIFT) | in generate_cip_header()
681 ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) | in generate_cip_header()
684 ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) | in generate_cip_header()
685 ((s->ctx_data.rx.fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) | in generate_cip_header()
698 payload_length = data_blocks * sizeof(__be32) * s->data_block_quadlets; in build_it_pkt_header()
699 params->payload_length = payload_length; in build_it_pkt_header()
702 cip_header = (__be32 *)params->header; in build_it_pkt_header()
704 params->header_length = header_length; in build_it_pkt_header()
710 data_block_counter, s->packet_index, index, curr_cycle_time); in build_it_pkt_header()
729 * This module supports 'Two-quadlet CIP header with SYT field'. in check_cip_header()
734 (!(s->flags & CIP_HEADER_WITHOUT_EOH))) { in check_cip_header()
735 dev_info_ratelimited(&s->unit->device, in check_cip_header()
738 return -EAGAIN; in check_cip_header()
744 if (sph != s->sph || fmt != s->fmt) { in check_cip_header()
745 dev_info_ratelimited(&s->unit->device, in check_cip_header()
748 return -EAGAIN; in check_cip_header()
760 dev_err(&s->unit->device, in check_cip_header()
763 return -EPROTO; in check_cip_header()
765 if (s->flags & CIP_WRONG_DBS) in check_cip_header()
766 data_block_quadlets = s->data_block_quadlets; in check_cip_header()
773 if (*data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) && in check_cip_header()
777 if ((dbc == 0x00 && (s->flags & CIP_SKIP_DBC_ZERO_CHECK)) || in check_cip_header()
780 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) { in check_cip_header()
785 if (!(s->flags & CIP_DBC_IS_PAYLOAD_QUADLETS)) { in check_cip_header()
786 if (*data_blocks > 0 && s->ctx_data.tx.dbc_interval > 0) in check_cip_header()
787 dbc_interval = s->ctx_data.tx.dbc_interval; in check_cip_header()
798 dev_err(&s->unit->device, in check_cip_header()
801 return -EIO; in check_cip_header()
806 if (!(s->flags & CIP_UNAWARE_SYT)) in check_cip_header()
825 if (!(s->flags & CIP_NO_HEADER)) in parse_ir_ctx_header()
830 if (payload_length > cip_header_size + s->ctx_data.tx.max_ctx_payload_length) { in parse_ir_ctx_header()
831 dev_err(&s->unit->device, in parse_ir_ctx_header()
833 payload_length, cip_header_size + s->ctx_data.tx.max_ctx_payload_length); in parse_ir_ctx_header()
834 return -EIO; in parse_ir_ctx_header()
842 err = check_cip_header(s, cip_header, payload_length - cip_header_size, in parse_ir_ctx_header()
854 *data_blocks = payload_length / sizeof(__be32) / s->data_block_quadlets; in parse_ir_ctx_header()
868 // the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
885 cycle -= OHCI_SECOND_MODULUS * CYCLES_PER_SECOND; in increment_ohci_cycle_count()
894 return minuend - subtrahend; in decrement_ohci_cycle_count()
901 else if (lval < rval && rval - lval < OHCI_SECOND_MODULUS * CYCLES_PER_SECOND / 2) in compare_ohci_cycle_count()
902 return -1; in compare_ohci_cycle_count()
922 unsigned int next_cycle = s->next_cycle; in generate_tx_packet_descs()
923 unsigned int dbc = s->data_block_counter; in generate_tx_packet_descs()
924 unsigned int packet_index = s->packet_index; in generate_tx_packet_descs()
925 unsigned int queue_size = s->queue_size; in generate_tx_packet_descs()
931 (void)fw_card_read_cycle_time(fw_parent_device(s->unit)->card, &curr_cycle_time); in generate_tx_packet_descs()
943 if (s->flags & CIP_NO_HEADER) { in generate_tx_packet_descs()
953 desc->cycle = prev_cycle; in generate_tx_packet_descs()
954 desc->syt = 0; in generate_tx_packet_descs()
955 desc->data_blocks = 0; in generate_tx_packet_descs()
956 desc->data_block_counter = dbc; in generate_tx_packet_descs()
957 desc->ctx_payload = NULL; in generate_tx_packet_descs()
961 } else if (s->flags & CIP_JUMBO_PAYLOAD) { in generate_tx_packet_descs()
970 dev_err(&s->unit->device, "Detect discontinuity of cycle: %d %d\n", in generate_tx_packet_descs()
972 return -EIO; in generate_tx_packet_descs()
981 desc->cycle = cycle; in generate_tx_packet_descs()
982 desc->syt = syt; in generate_tx_packet_descs()
983 desc->data_blocks = data_blocks; in generate_tx_packet_descs()
984 desc->data_block_counter = dbc; in generate_tx_packet_descs()
985 desc->ctx_payload = s->buffer.packets[packet_index].buffer; in generate_tx_packet_descs()
987 if (!(s->flags & CIP_DBC_IS_END_EVENT)) in generate_tx_packet_descs()
988 dbc = (dbc + desc->data_blocks) & 0xff; in generate_tx_packet_descs()
993 ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header); in generate_tx_packet_descs()
997 s->next_cycle = next_cycle; in generate_tx_packet_descs()
998 s->data_block_counter = dbc; in generate_tx_packet_descs()
1017 struct seq_desc *seq_descs = s->ctx_data.rx.seq.descs; in generate_rx_packet_descs()
1018 unsigned int seq_size = s->ctx_data.rx.seq.size; in generate_rx_packet_descs()
1019 unsigned int seq_pos = s->ctx_data.rx.seq.pos; in generate_rx_packet_descs()
1020 unsigned int dbc = s->data_block_counter; in generate_rx_packet_descs()
1021 bool aware_syt = !(s->flags & CIP_UNAWARE_SYT); in generate_rx_packet_descs()
1027 unsigned int index = (s->packet_index + i) % s->queue_size; in generate_rx_packet_descs()
1030 desc->cycle = compute_ohci_it_cycle(*ctx_header, s->queue_size); in generate_rx_packet_descs()
1032 if (aware_syt && seq->syt_offset != CIP_SYT_NO_INFO) in generate_rx_packet_descs()
1033 desc->syt = compute_syt(seq->syt_offset, desc->cycle, s->transfer_delay); in generate_rx_packet_descs()
1035 desc->syt = CIP_SYT_NO_INFO; in generate_rx_packet_descs()
1037 desc->data_blocks = seq->data_blocks; in generate_rx_packet_descs()
1039 if (s->flags & CIP_DBC_IS_END_EVENT) in generate_rx_packet_descs()
1040 dbc = (dbc + desc->data_blocks) & 0xff; in generate_rx_packet_descs()
1042 desc->data_block_counter = dbc; in generate_rx_packet_descs()
1044 if (!(s->flags & CIP_DBC_IS_END_EVENT)) in generate_rx_packet_descs()
1045 dbc = (dbc + desc->data_blocks) & 0xff; in generate_rx_packet_descs()
1047 desc->ctx_payload = s->buffer.packets[index].buffer; in generate_rx_packet_descs()
1055 s->data_block_counter = dbc; in generate_rx_packet_descs()
1056 s->ctx_data.rx.seq.pos = seq_pos; in generate_rx_packet_descs()
1061 s->packet_index = -1; in cancel_stream()
1064 WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN); in cancel_stream()
1081 for (i = 0; i < count - 1; ++i) in compute_pcm_extra_delay()
1083 latest_cycle = desc->cycle; in compute_pcm_extra_delay()
1085 err = fw_card_read_cycle_time(fw_parent_device(s->unit)->card, &cycle_time); in compute_pcm_extra_delay()
1093 if (s->direction == AMDTP_IN_STREAM) { in compute_pcm_extra_delay()
1101 // value expectedly corresponds to a few packets (0-2) since the packet arrived at in compute_pcm_extra_delay()
1105 data_block_count += desc->data_blocks; in compute_pcm_extra_delay()
1116 data_block_count += desc->data_blocks; in compute_pcm_extra_delay()
1121 return data_block_count * s->pcm_frame_multiplier; in compute_pcm_extra_delay()
1131 pcm = READ_ONCE(s->pcm); in process_ctx_payloads()
1132 s->process_ctx_payloads(s, desc, count, pcm); in process_ctx_payloads()
1137 pcm->runtime->delay = compute_pcm_extra_delay(s, desc, count); in process_ctx_payloads()
1140 data_block_count += desc->data_blocks; in process_ctx_payloads()
1144 update_pcm_pointers(s, pcm, data_block_count * s->pcm_frame_multiplier); in process_ctx_payloads()
1152 const struct amdtp_domain *d = s->domain; in process_rx_packets()
1154 const unsigned int events_per_period = d->events_per_period; in process_rx_packets()
1155 unsigned int event_count = s->ctx_data.rx.event_count; in process_rx_packets()
1156 struct pkt_desc *desc = s->packet_descs_cursor; in process_rx_packets()
1163 if (s->packet_index < 0) in process_rx_packets()
1173 if (!(s->flags & CIP_NO_HEADER)) in process_rx_packets()
1178 if (s == d->irq_target) { in process_rx_packets()
1182 struct snd_pcm_substream *pcm = READ_ONCE(s->pcm); in process_rx_packets()
1183 need_hw_irq = !pcm || !pcm->runtime->no_period_wakeup; in process_rx_packets()
1189 (void)fw_card_read_cycle_time(fw_parent_device(s->unit)->card, &curr_cycle_time); in process_rx_packets()
1198 build_it_pkt_header(s, desc->cycle, &template.params, pkt_header_length, in process_rx_packets()
1199 desc->data_blocks, desc->data_block_counter, in process_rx_packets()
1200 desc->syt, i, curr_cycle_time); in process_rx_packets()
1202 if (s == s->domain->irq_target) { in process_rx_packets()
1203 event_count += desc->data_blocks; in process_rx_packets()
1205 event_count -= events_per_period; in process_rx_packets()
1218 s->ctx_data.rx.event_count = event_count; in process_rx_packets()
1219 s->packet_descs_cursor = desc; in process_rx_packets()
1226 struct amdtp_domain *d = s->domain; in skip_rx_packets()
1232 if (s->packet_index < 0) in skip_rx_packets()
1237 cycle = compute_ohci_it_cycle(ctx_header[packets - 1], s->queue_size); in skip_rx_packets()
1238 s->next_cycle = increment_ohci_cycle_count(cycle, 1); in skip_rx_packets()
1245 bool sched_irq = (s == d->irq_target && i == packets - 1); in skip_rx_packets()
1261 struct amdtp_domain *d = s->domain; in process_rx_packets_intermediately()
1263 const unsigned int queue_size = s->queue_size; in process_rx_packets_intermediately()
1267 if (s->packet_index < 0) in process_rx_packets_intermediately()
1276 if (compare_ohci_cycle_count(cycle, d->processing_cycle.rx_start) >= 0) in process_rx_packets_intermediately()
1290 header_length -= length; in process_rx_packets_intermediately()
1294 s->ready_processing = true; in process_rx_packets_intermediately()
1295 wake_up(&s->ready_wait); in process_rx_packets_intermediately()
1297 if (d->replay.enable) in process_rx_packets_intermediately()
1298 s->ctx_data.rx.cache_pos = 0; in process_rx_packets_intermediately()
1304 if (s == d->irq_target) in process_rx_packets_intermediately()
1305 s->context->callback.sc = irq_target_callback; in process_rx_packets_intermediately()
1307 s->context->callback.sc = process_rx_packets; in process_rx_packets_intermediately()
1316 struct pkt_desc *desc = s->packet_descs_cursor; in process_tx_packets()
1322 if (s->packet_index < 0) in process_tx_packets()
1326 packet_count = header_length / s->ctx_data.tx.ctx_header_size; in process_tx_packets()
1331 if (err != -EAGAIN) { in process_tx_packets()
1336 struct amdtp_domain *d = s->domain; in process_tx_packets()
1340 if (d->replay.enable) in process_tx_packets()
1345 s->packet_descs_cursor = desc; in process_tx_packets()
1367 if (s->packet_index < 0) in drop_tx_packets()
1370 packets = header_length / s->ctx_data.tx.ctx_header_size; in drop_tx_packets()
1372 ctx_header += (packets - 1) * s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header); in drop_tx_packets()
1374 s->next_cycle = increment_ohci_cycle_count(cycle, 1); in drop_tx_packets()
1390 struct amdtp_domain *d = s->domain; in process_tx_packets_intermediately()
1395 if (s->packet_index < 0) in process_tx_packets_intermediately()
1398 packets = header_length / s->ctx_data.tx.ctx_header_size; in process_tx_packets_intermediately()
1405 if (compare_ohci_cycle_count(cycle, d->processing_cycle.tx_start) >= 0) in process_tx_packets_intermediately()
1408 ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(__be32); in process_tx_packets_intermediately()
1415 size_t length = s->ctx_data.tx.ctx_header_size * offset; in process_tx_packets_intermediately()
1422 header_length -= length; in process_tx_packets_intermediately()
1426 s->ready_processing = true; in process_tx_packets_intermediately()
1427 wake_up(&s->ready_wait); in process_tx_packets_intermediately()
1433 context->callback.sc = process_tx_packets; in process_tx_packets_intermediately()
1441 struct amdtp_domain *d = s->domain; in drop_tx_packets_initially()
1447 if (s->packet_index < 0) in drop_tx_packets_initially()
1450 count = header_length / s->ctx_data.tx.ctx_header_size; in drop_tx_packets_initially()
1460 if (s->flags & CIP_NO_HEADER) { in drop_tx_packets_initially()
1461 data_blocks = payload_quads / s->data_block_quadlets; in drop_tx_packets_initially()
1468 payload_quads -= CIP_HEADER_QUADLETS; in drop_tx_packets_initially()
1470 if (s->flags & CIP_UNAWARE_SYT) { in drop_tx_packets_initially()
1471 data_blocks = payload_quads / s->data_block_quadlets; in drop_tx_packets_initially()
1480 data_blocks = payload_quads / s->data_block_quadlets; in drop_tx_packets_initially()
1487 ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(__be32); in drop_tx_packets_initially()
1493 s->ctx_data.tx.event_starts = true; in drop_tx_packets_initially()
1501 list_for_each_entry(s, &d->streams, list) { in drop_tx_packets_initially()
1502 if (s->direction == AMDTP_IN_STREAM) { in drop_tx_packets_initially()
1504 if (s->ctx_data.tx.event_starts) in drop_tx_packets_initially()
1512 list_for_each_entry(s, &d->streams, list) { in drop_tx_packets_initially()
1513 if (s->direction != AMDTP_IN_STREAM) in drop_tx_packets_initially()
1516 next_cycle = increment_ohci_cycle_count(s->next_cycle, in drop_tx_packets_initially()
1517 d->processing_cycle.tx_init_skip); in drop_tx_packets_initially()
1522 s->context->callback.sc = process_tx_packets_intermediately; in drop_tx_packets_initially()
1525 d->processing_cycle.tx_start = cycle; in drop_tx_packets_initially()
1534 list_for_each_entry(s, &d->streams, list) { in process_ctxs_in_domain()
1535 if (s != d->irq_target && amdtp_stream_running(s)) in process_ctxs_in_domain()
1536 fw_iso_context_flush_completions(s->context); in process_ctxs_in_domain()
1544 if (amdtp_stream_running(d->irq_target)) in process_ctxs_in_domain()
1545 cancel_stream(d->irq_target); in process_ctxs_in_domain()
1547 list_for_each_entry(s, &d->streams, list) { in process_ctxs_in_domain()
1557 struct amdtp_domain *d = s->domain; in irq_target_callback()
1567 struct amdtp_domain *d = s->domain; in irq_target_callback_intermediately()
1577 struct amdtp_domain *d = s->domain; in irq_target_callback_skip()
1583 if (d->replay.enable && !d->replay.on_the_fly) { in irq_target_callback_skip()
1588 list_for_each_entry(rx, &d->streams, list) { in irq_target_callback_skip()
1592 if (rx->direction != AMDTP_OUT_STREAM) in irq_target_callback_skip()
1596 tx = rx->ctx_data.rx.replay_target; in irq_target_callback_skip()
1598 if (cached_cycles > tx->ctx_data.tx.cache.size / 2) in irq_target_callback_skip()
1610 unsigned int cycle = s->next_cycle; in irq_target_callback_skip()
1611 list_for_each_entry(s, &d->streams, list) { in irq_target_callback_skip()
1612 if (s->direction != AMDTP_OUT_STREAM) in irq_target_callback_skip()
1615 if (compare_ohci_cycle_count(s->next_cycle, cycle) > 0) in irq_target_callback_skip()
1616 cycle = s->next_cycle; in irq_target_callback_skip()
1618 if (s == d->irq_target) in irq_target_callback_skip()
1619 s->context->callback.sc = irq_target_callback_intermediately; in irq_target_callback_skip()
1621 s->context->callback.sc = process_rx_packets_intermediately; in irq_target_callback_skip()
1624 d->processing_cycle.rx_start = cycle; in irq_target_callback_skip()
1628 // This is executed one time. For in-stream, first packet has come. For out-stream, prepared to
1635 struct amdtp_domain *d = s->domain; in amdtp_stream_first_callback()
1637 if (s->direction == AMDTP_IN_STREAM) { in amdtp_stream_first_callback()
1638 context->callback.sc = drop_tx_packets_initially; in amdtp_stream_first_callback()
1640 if (s == d->irq_target) in amdtp_stream_first_callback()
1641 context->callback.sc = irq_target_callback_skip; in amdtp_stream_first_callback()
1643 context->callback.sc = skip_rx_packets; in amdtp_stream_first_callback()
1646 context->callback.sc(context, tstamp, header_length, header, s); in amdtp_stream_first_callback()
1650 * amdtp_stream_start - start transferring packets
1664 bool is_irq_target = (s == s->domain->irq_target); in amdtp_stream_start()
1671 mutex_lock(&s->mutex); in amdtp_stream_start()
1674 (s->data_block_quadlets < 1))) { in amdtp_stream_start()
1675 err = -EBADFD; in amdtp_stream_start()
1679 if (s->direction == AMDTP_IN_STREAM) { in amdtp_stream_start()
1682 err = -EINVAL; in amdtp_stream_start()
1686 s->data_block_counter = UINT_MAX; in amdtp_stream_start()
1688 s->data_block_counter = 0; in amdtp_stream_start()
1692 if (s->direction == AMDTP_IN_STREAM) { in amdtp_stream_start()
1695 if (!(s->flags & CIP_NO_HEADER)) in amdtp_stream_start()
1702 ctx_header_size = 0; // No effect for IT context. in amdtp_stream_start()
1706 err = iso_packets_buffer_init(&s->buffer, s->unit, queue_size, max_ctx_payload_size, dir); in amdtp_stream_start()
1709 s->queue_size = queue_size; in amdtp_stream_start()
1711 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card, in amdtp_stream_start()
1714 if (IS_ERR(s->context)) { in amdtp_stream_start()
1715 err = PTR_ERR(s->context); in amdtp_stream_start()
1716 if (err == -EBUSY) in amdtp_stream_start()
1717 dev_err(&s->unit->device, in amdtp_stream_start()
1718 "no free stream on this controller\n"); in amdtp_stream_start()
1724 if (s->direction == AMDTP_IN_STREAM) { in amdtp_stream_start()
1725 s->ctx_data.tx.max_ctx_payload_length = max_ctx_payload_size; in amdtp_stream_start()
1726 s->ctx_data.tx.ctx_header_size = ctx_header_size; in amdtp_stream_start()
1727 s->ctx_data.tx.event_starts = false; in amdtp_stream_start()
1729 if (s->domain->replay.enable) { in amdtp_stream_start()
1732 s->ctx_data.tx.cache.size = max_t(unsigned int, s->syt_interval * 2, in amdtp_stream_start()
1734 s->ctx_data.tx.cache.pos = 0; in amdtp_stream_start()
1735 s->ctx_data.tx.cache.descs = kcalloc(s->ctx_data.tx.cache.size, in amdtp_stream_start()
1736 sizeof(*s->ctx_data.tx.cache.descs), GFP_KERNEL); in amdtp_stream_start()
1737 if (!s->ctx_data.tx.cache.descs) { in amdtp_stream_start()
1738 err = -ENOMEM; in amdtp_stream_start()
1756 s->ctx_data.rx.seq.descs = kcalloc(queue_size, sizeof(*s->ctx_data.rx.seq.descs), GFP_KERNEL); in amdtp_stream_start()
1757 if (!s->ctx_data.rx.seq.descs) { in amdtp_stream_start()
1758 err = -ENOMEM; in amdtp_stream_start()
1761 s->ctx_data.rx.seq.size = queue_size; in amdtp_stream_start()
1762 s->ctx_data.rx.seq.pos = 0; in amdtp_stream_start()
1764 entry = &initial_state[s->sfc]; in amdtp_stream_start()
1765 s->ctx_data.rx.data_block_state = entry->data_block; in amdtp_stream_start()
1766 s->ctx_data.rx.syt_offset_state = entry->syt_offset; in amdtp_stream_start()
1767 s->ctx_data.rx.last_syt_offset = TICKS_PER_CYCLE; in amdtp_stream_start()
1769 s->ctx_data.rx.event_count = 0; in amdtp_stream_start()
1772 if (s->flags & CIP_NO_HEADER) in amdtp_stream_start()
1773 s->tag = TAG_NO_CIP_HEADER; in amdtp_stream_start()
1775 s->tag = TAG_CIP; in amdtp_stream_start()
1781 descs = kcalloc(s->queue_size + 8, sizeof(*descs), GFP_KERNEL); in amdtp_stream_start()
1783 err = -ENOMEM; in amdtp_stream_start()
1786 s->packet_descs = descs; in amdtp_stream_start()
1788 INIT_LIST_HEAD(&s->packet_descs_list); in amdtp_stream_start()
1789 for (i = 0; i < s->queue_size; ++i) { in amdtp_stream_start()
1790 INIT_LIST_HEAD(&descs->link); in amdtp_stream_start()
1791 list_add_tail(&descs->link, &s->packet_descs_list); in amdtp_stream_start()
1794 s->packet_descs_cursor = list_first_entry(&s->packet_descs_list, struct pkt_desc, link); in amdtp_stream_start()
1796 s->packet_index = 0; in amdtp_stream_start()
1800 if (s->direction == AMDTP_IN_STREAM) { in amdtp_stream_start()
1809 sched_irq = !((s->packet_index + 1) % in amdtp_stream_start()
1817 } while (s->packet_index > 0); in amdtp_stream_start()
1821 if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER)) in amdtp_stream_start()
1824 s->ready_processing = false; in amdtp_stream_start()
1825 err = fw_iso_context_start(s->context, -1, 0, tag); in amdtp_stream_start()
1829 mutex_unlock(&s->mutex); in amdtp_stream_start()
1833 kfree(s->packet_descs); in amdtp_stream_start()
1834 s->packet_descs = NULL; in amdtp_stream_start()
1836 if (s->direction == AMDTP_OUT_STREAM) { in amdtp_stream_start()
1837 kfree(s->ctx_data.rx.seq.descs); in amdtp_stream_start()
1839 if (s->domain->replay.enable) in amdtp_stream_start()
1840 kfree(s->ctx_data.tx.cache.descs); in amdtp_stream_start()
1842 fw_iso_context_destroy(s->context); in amdtp_stream_start()
1843 s->context = ERR_PTR(-1); in amdtp_stream_start()
1845 iso_packets_buffer_destroy(&s->buffer, s->unit); in amdtp_stream_start()
1847 mutex_unlock(&s->mutex); in amdtp_stream_start()
1853 * amdtp_domain_stream_pcm_pointer - get the PCM buffer position
1862 struct amdtp_stream *irq_target = d->irq_target; in amdtp_domain_stream_pcm_pointer()
1869 // amdtp-stream process_rx_packets() attempts to in amdtp_domain_stream_pcm_pointer()
1871 if (current_work() != &s->period_work) in amdtp_domain_stream_pcm_pointer()
1872 fw_iso_context_flush_completions(irq_target->context); in amdtp_domain_stream_pcm_pointer()
1875 return READ_ONCE(s->pcm_buffer_pointer); in amdtp_domain_stream_pcm_pointer()
1880 * amdtp_domain_stream_pcm_ack - acknowledge queued PCM frames
1888 struct amdtp_stream *irq_target = d->irq_target; in amdtp_domain_stream_pcm_ack()
1893 fw_iso_context_flush_completions(irq_target->context); in amdtp_domain_stream_pcm_ack()
1900 * amdtp_stream_update - update the stream after a bus reset
1906 WRITE_ONCE(s->source_node_id_field, in amdtp_stream_update()
1907 (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & CIP_SID_MASK); in amdtp_stream_update()
1912 * amdtp_stream_stop - stop sending packets
1920 mutex_lock(&s->mutex); in amdtp_stream_stop()
1923 mutex_unlock(&s->mutex); in amdtp_stream_stop()
1927 cancel_work_sync(&s->period_work); in amdtp_stream_stop()
1928 fw_iso_context_stop(s->context); in amdtp_stream_stop()
1929 fw_iso_context_destroy(s->context); in amdtp_stream_stop()
1930 s->context = ERR_PTR(-1); in amdtp_stream_stop()
1931 iso_packets_buffer_destroy(&s->buffer, s->unit); in amdtp_stream_stop()
1932 kfree(s->packet_descs); in amdtp_stream_stop()
1933 s->packet_descs = NULL; in amdtp_stream_stop()
1935 if (s->direction == AMDTP_OUT_STREAM) { in amdtp_stream_stop()
1936 kfree(s->ctx_data.rx.seq.descs); in amdtp_stream_stop()
1938 if (s->domain->replay.enable) in amdtp_stream_stop()
1939 kfree(s->ctx_data.tx.cache.descs); in amdtp_stream_stop()
1942 mutex_unlock(&s->mutex); in amdtp_stream_stop()
1946 * amdtp_stream_pcm_abort - abort the running PCM device
1956 pcm = READ_ONCE(s->pcm); in amdtp_stream_pcm_abort()
1963 * amdtp_domain_init - initialize an AMDTP domain structure
1968 INIT_LIST_HEAD(&d->streams); in amdtp_domain_init()
1970 d->events_per_period = 0; in amdtp_domain_init()
1977 * amdtp_domain_destroy - destroy an AMDTP domain structure
1988 * amdtp_domain_add_stream - register isoc context into the domain.
1999 list_for_each_entry(tmp, &d->streams, list) { in amdtp_domain_add_stream()
2001 return -EBUSY; in amdtp_domain_add_stream()
2004 list_add(&s->list, &d->streams); in amdtp_domain_add_stream()
2006 s->channel = channel; in amdtp_domain_add_stream()
2007 s->speed = speed; in amdtp_domain_add_stream()
2008 s->domain = d; in amdtp_domain_add_stream()
2022 list_for_each_entry(rx, &d->streams, list) { in make_association()
2023 if (rx->direction == AMDTP_OUT_STREAM) { in make_association()
2028 list_for_each_entry(s, &d->streams, list) { in make_association()
2029 if (s->direction == AMDTP_IN_STREAM) { in make_association()
2040 list_for_each_entry(s, &d->streams, list) { in make_association()
2041 if (s->direction == AMDTP_IN_STREAM) { in make_association()
2046 // No target is available to replay sequence. in make_association()
2048 return -EINVAL; in make_association()
2051 rx->ctx_data.rx.replay_target = tx; in make_association()
2061 * amdtp_domain_start - start sending packets for isoc context in the domain.
2073 unsigned int events_per_buffer = d->events_per_buffer; in amdtp_domain_start()
2074 unsigned int events_per_period = d->events_per_period; in amdtp_domain_start()
2085 d->replay.enable = replay_seq; in amdtp_domain_start()
2086 d->replay.on_the_fly = replay_on_the_fly; in amdtp_domain_start()
2089 list_for_each_entry(s, &d->streams, list) { in amdtp_domain_start()
2090 if (s->direction == AMDTP_OUT_STREAM) { in amdtp_domain_start()
2096 return -ENXIO; in amdtp_domain_start()
2097 d->irq_target = s; in amdtp_domain_start()
2099 d->processing_cycle.tx_init_skip = tx_init_skip_cycles; in amdtp_domain_start()
2105 events_per_period = amdtp_rate_table[d->irq_target->sfc] / 100; in amdtp_domain_start()
2110 amdtp_rate_table[d->irq_target->sfc]); in amdtp_domain_start()
2112 list_for_each_entry(s, &d->streams, list) { in amdtp_domain_start()
2115 if (s->direction == AMDTP_OUT_STREAM && s == d->irq_target) { in amdtp_domain_start()
2117 amdtp_rate_table[d->irq_target->sfc]); in amdtp_domain_start()
2120 // Starts immediately but actually DMA context starts several hundred cycles later. in amdtp_domain_start()
2121 err = amdtp_stream_start(s, s->channel, s->speed, queue_size, idle_irq_interval); in amdtp_domain_start()
2128 list_for_each_entry(s, &d->streams, list) in amdtp_domain_start()
2135 * amdtp_domain_stop - stop sending packets for isoc context in the same domain.
2142 if (d->irq_target) in amdtp_domain_stop()
2143 amdtp_stream_stop(d->irq_target); in amdtp_domain_stop()
2145 list_for_each_entry_safe(s, next, &d->streams, list) { in amdtp_domain_stop()
2146 list_del(&s->list); in amdtp_domain_stop()
2148 if (s != d->irq_target) in amdtp_domain_stop()
2152 d->events_per_period = 0; in amdtp_domain_stop()
2153 d->irq_target = NULL; in amdtp_domain_stop()