Lines Matching +full:lock +full:- +full:detect +full:- +full:function +full:- +full:integer +full:- +full:n +full:- +full:enable
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()
180 t.integer = 1; 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()
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
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
332 * This function must not be called before the stream has been configured
339 if (!(s->flags & CIP_NO_HEADER)) in amdtp_stream_get_max_payload()
349 * amdtp_stream_pcm_prepare - prepare PCM device for running
352 * This function should be called from the PCM device's .prepare callback.
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()
388 unsigned int state = s->ctx_data.rx.data_block_state; in pool_ideal_nonblocking_data_blocks()
395 // Sample_rate / 8000 is an integer, and precomputed. 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()
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()
422 s->ctx_data.rx.data_block_state = state; in pool_ideal_nonblocking_data_blocks()
435 * The time, in ticks, of the n'th SYT_INTERVAL sample is: in calculate_syt_offset()
436 * n * SYT_INTERVAL * 24576000 / sample_rate 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()
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()
629 // process_rx_packets() B.PCM stream lock in update_pcm_pointers()
637 if (!pcm->runtime->no_period_wakeup) in update_pcm_pointers()
638 queue_work(system_highpri_wq, &s->period_work); in update_pcm_pointers()
646 struct snd_pcm_substream *pcm = READ_ONCE(s->pcm); in pcm_period_work()
657 params->interrupt = sched_irq; in queue_packet()
658 params->tag = s->tag; in queue_packet()
659 params->sy = 0; in queue_packet()
661 err = fw_iso_context_queue(s->context, params, &s->buffer.iso_buffer, in queue_packet()
662 s->buffer.packets[s->packet_index].offset); in queue_packet()
664 dev_err(&s->unit->device, "queueing error: %d\n", err); in queue_packet()
668 if (++s->packet_index >= s->queue_size) in queue_packet()
669 s->packet_index = 0; in queue_packet()
677 params->skip = in queue_out_packet()
678 !!(params->header_length == 0 && params->payload_length == 0); in queue_out_packet()
686 params->header_length = s->ctx_data.tx.ctx_header_size; in queue_in_packet()
687 params->payload_length = s->ctx_data.tx.max_ctx_payload_length; in queue_in_packet()
688 params->skip = false; in queue_in_packet()
695 cip_header[0] = cpu_to_be32(READ_ONCE(s->source_node_id_field) | in generate_cip_header()
696 (s->data_block_quadlets << CIP_DBS_SHIFT) | in generate_cip_header()
697 ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) | in generate_cip_header()
700 ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) | in generate_cip_header()
701 ((s->ctx_data.rx.fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) | in generate_cip_header()
714 payload_length = data_blocks * sizeof(__be32) * s->data_block_quadlets; in build_it_pkt_header()
715 params->payload_length = payload_length; in build_it_pkt_header()
718 cip_header = (__be32 *)params->header; in build_it_pkt_header()
720 params->header_length = header_length; in build_it_pkt_header()
726 data_block_counter, s->packet_index, index, curr_cycle_time); in build_it_pkt_header()
745 * This module supports 'Two-quadlet CIP header with SYT field'. in check_cip_header()
750 (!(s->flags & CIP_HEADER_WITHOUT_EOH))) { in check_cip_header()
751 dev_info_ratelimited(&s->unit->device, in check_cip_header()
752 "Invalid CIP header for AMDTP: %08X:%08X\n", in check_cip_header()
754 return -EAGAIN; in check_cip_header()
760 if (sph != s->sph || fmt != s->fmt) { in check_cip_header()
761 dev_info_ratelimited(&s->unit->device, in check_cip_header()
762 "Detect unexpected protocol: %08x %08x\n", in check_cip_header()
764 return -EAGAIN; in check_cip_header()
776 dev_err(&s->unit->device, in check_cip_header()
777 "Detect invalid value in dbs field: %08X\n", in check_cip_header()
779 return -EPROTO; in check_cip_header()
781 if (s->flags & CIP_WRONG_DBS) in check_cip_header()
782 data_block_quadlets = s->data_block_quadlets; in check_cip_header()
789 if (*data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) && in check_cip_header()
793 if ((dbc == 0x00 && (s->flags & CIP_SKIP_DBC_ZERO_CHECK)) || in check_cip_header()
796 } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) { in check_cip_header()
801 if (!(s->flags & CIP_DBC_IS_PAYLOAD_QUADLETS)) { in check_cip_header()
802 if (*data_blocks > 0 && s->ctx_data.tx.dbc_interval > 0) in check_cip_header()
803 dbc_interval = s->ctx_data.tx.dbc_interval; in check_cip_header()
814 dev_err(&s->unit->device, in check_cip_header()
815 "Detect discontinuity of CIP: %02X %02X\n", in check_cip_header()
817 return -EIO; in check_cip_header()
822 if (!(s->flags & CIP_UNAWARE_SYT)) in check_cip_header()
841 if (!(s->flags & CIP_NO_HEADER)) in parse_ir_ctx_header()
846 if (payload_length > cip_header_size + s->ctx_data.tx.max_ctx_payload_length) { in parse_ir_ctx_header()
847 dev_err(&s->unit->device, in parse_ir_ctx_header()
848 "Detect jumbo payload: %04x %04x\n", in parse_ir_ctx_header()
849 payload_length, cip_header_size + s->ctx_data.tx.max_ctx_payload_length); in parse_ir_ctx_header()
850 return -EIO; in parse_ir_ctx_header()
858 err = check_cip_header(s, cip_header, payload_length - cip_header_size, in parse_ir_ctx_header()
870 *data_blocks = payload_length / sizeof(__be32) / s->data_block_quadlets; in parse_ir_ctx_header()
901 cycle -= OHCI_SECOND_MODULUS * CYCLES_PER_SECOND; in increment_ohci_cycle_count()
910 return minuend - subtrahend; in decrement_ohci_cycle_count()
917 else if (lval < rval && rval - lval < OHCI_SECOND_MODULUS * CYCLES_PER_SECOND / 2) in compare_ohci_cycle_count()
918 return -1; in compare_ohci_cycle_count()
938 unsigned int next_cycle = s->next_cycle; in generate_tx_packet_descs()
939 unsigned int dbc = s->data_block_counter; in generate_tx_packet_descs()
940 unsigned int packet_index = s->packet_index; in generate_tx_packet_descs()
941 unsigned int queue_size = s->queue_size; in generate_tx_packet_descs()
947 (void)fw_card_read_cycle_time(fw_parent_device(s->unit)->card, &curr_cycle_time); in generate_tx_packet_descs()
959 if (s->flags & CIP_NO_HEADER) { in generate_tx_packet_descs()
969 desc->cycle = prev_cycle; in generate_tx_packet_descs()
970 desc->syt = 0; in generate_tx_packet_descs()
971 desc->data_blocks = 0; in generate_tx_packet_descs()
972 desc->data_block_counter = dbc; in generate_tx_packet_descs()
973 desc->ctx_payload = NULL; in generate_tx_packet_descs()
977 } else if (s->flags & CIP_JUMBO_PAYLOAD) { in generate_tx_packet_descs()
986 dev_err(&s->unit->device, "Detect discontinuity of cycle: %d %d\n", in generate_tx_packet_descs()
988 return -EIO; in generate_tx_packet_descs()
997 desc->cycle = cycle; in generate_tx_packet_descs()
998 desc->syt = syt; in generate_tx_packet_descs()
999 desc->data_blocks = data_blocks; in generate_tx_packet_descs()
1000 desc->data_block_counter = dbc; in generate_tx_packet_descs()
1001 desc->ctx_payload = s->buffer.packets[packet_index].buffer; in generate_tx_packet_descs()
1003 if (!(s->flags & CIP_DBC_IS_END_EVENT)) in generate_tx_packet_descs()
1004 dbc = (dbc + desc->data_blocks) & 0xff; in generate_tx_packet_descs()
1009 ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header); in generate_tx_packet_descs()
1013 s->next_cycle = next_cycle; in generate_tx_packet_descs()
1014 s->data_block_counter = dbc; in generate_tx_packet_descs()
1033 struct seq_desc *seq_descs = s->ctx_data.rx.seq.descs; in generate_rx_packet_descs()
1034 unsigned int seq_size = s->ctx_data.rx.seq.size; in generate_rx_packet_descs()
1035 unsigned int seq_pos = s->ctx_data.rx.seq.pos; in generate_rx_packet_descs()
1036 unsigned int dbc = s->data_block_counter; in generate_rx_packet_descs()
1037 bool aware_syt = !(s->flags & CIP_UNAWARE_SYT); in generate_rx_packet_descs()
1043 unsigned int index = (s->packet_index + i) % s->queue_size; in generate_rx_packet_descs()
1046 desc->cycle = compute_ohci_it_cycle(*ctx_header, s->queue_size); in generate_rx_packet_descs()
1048 if (aware_syt && seq->syt_offset != CIP_SYT_NO_INFO) in generate_rx_packet_descs()
1049 desc->syt = compute_syt(seq->syt_offset, desc->cycle, s->transfer_delay); in generate_rx_packet_descs()
1051 desc->syt = CIP_SYT_NO_INFO; in generate_rx_packet_descs()
1053 desc->data_blocks = seq->data_blocks; in generate_rx_packet_descs()
1055 if (s->flags & CIP_DBC_IS_END_EVENT) in generate_rx_packet_descs()
1056 dbc = (dbc + desc->data_blocks) & 0xff; in generate_rx_packet_descs()
1058 desc->data_block_counter = dbc; in generate_rx_packet_descs()
1060 if (!(s->flags & CIP_DBC_IS_END_EVENT)) in generate_rx_packet_descs()
1061 dbc = (dbc + desc->data_blocks) & 0xff; in generate_rx_packet_descs()
1063 desc->ctx_payload = s->buffer.packets[index].buffer; in generate_rx_packet_descs()
1071 s->data_block_counter = dbc; in generate_rx_packet_descs()
1072 s->ctx_data.rx.seq.pos = seq_pos; in generate_rx_packet_descs()
1079 s->packet_index = -1; in cancel_stream()
1081 // Detect work items for any isochronous context. The work item for pcm_period_work() in cancel_stream()
1083 // snd_pcm_ops.pointer() under acquiring PCM stream(group) lock and causes dead lock at in cancel_stream()
1085 if (work && work != &s->period_work) in cancel_stream()
1087 WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN); in cancel_stream()
1104 for (i = 0; i < count - 1; ++i) in compute_pcm_extra_delay()
1106 latest_cycle = desc->cycle; in compute_pcm_extra_delay()
1108 err = fw_card_read_cycle_time(fw_parent_device(s->unit)->card, &cycle_time); in compute_pcm_extra_delay()
1116 if (s->direction == AMDTP_IN_STREAM) { in compute_pcm_extra_delay()
1124 // value expectedly corresponds to a few packets (0-2) since the packet arrived at in compute_pcm_extra_delay()
1128 data_block_count += desc->data_blocks; in compute_pcm_extra_delay()
1139 data_block_count += desc->data_blocks; in compute_pcm_extra_delay()
1144 return data_block_count * s->pcm_frame_multiplier; in compute_pcm_extra_delay()
1154 pcm = READ_ONCE(s->pcm); in process_ctx_payloads()
1155 s->process_ctx_payloads(s, desc, count, pcm); in process_ctx_payloads()
1160 pcm->runtime->delay = compute_pcm_extra_delay(s, desc, count); in process_ctx_payloads()
1163 data_block_count += desc->data_blocks; in process_ctx_payloads()
1167 update_pcm_pointers(s, pcm, data_block_count * s->pcm_frame_multiplier); in process_ctx_payloads()
1175 const struct amdtp_domain *d = s->domain; in process_rx_packets()
1177 const unsigned int events_per_period = d->events_per_period; in process_rx_packets()
1178 unsigned int event_count = s->ctx_data.rx.event_count; in process_rx_packets()
1179 struct pkt_desc *desc = s->packet_descs_cursor; in process_rx_packets()
1186 if (s->packet_index < 0) in process_rx_packets()
1196 if (!(s->flags & CIP_NO_HEADER)) in process_rx_packets()
1201 if (s == d->irq_target) { in process_rx_packets()
1205 struct snd_pcm_substream *pcm = READ_ONCE(s->pcm); in process_rx_packets()
1206 need_hw_irq = !pcm || !pcm->runtime->no_period_wakeup; in process_rx_packets()
1212 (void)fw_card_read_cycle_time(fw_parent_device(s->unit)->card, &curr_cycle_time); in process_rx_packets()
1218 build_it_pkt_header(s, desc->cycle, template, pkt_header_length, in process_rx_packets()
1219 desc->data_blocks, desc->data_block_counter, in process_rx_packets()
1220 desc->syt, i, curr_cycle_time); in process_rx_packets()
1222 if (s == s->domain->irq_target) { in process_rx_packets()
1223 event_count += desc->data_blocks; in process_rx_packets()
1225 event_count -= events_per_period; in process_rx_packets()
1238 s->ctx_data.rx.event_count = event_count; in process_rx_packets()
1239 s->packet_descs_cursor = desc; in process_rx_packets()
1246 struct amdtp_domain *d = s->domain; in skip_rx_packets()
1252 if (s->packet_index < 0) in skip_rx_packets()
1257 cycle = compute_ohci_it_cycle(ctx_header[packets - 1], s->queue_size); in skip_rx_packets()
1258 s->next_cycle = increment_ohci_cycle_count(cycle, 1); in skip_rx_packets()
1265 bool sched_irq = (s == d->irq_target && i == packets - 1); in skip_rx_packets()
1281 struct amdtp_domain *d = s->domain; in process_rx_packets_intermediately()
1283 const unsigned int queue_size = s->queue_size; in process_rx_packets_intermediately()
1287 if (s->packet_index < 0) in process_rx_packets_intermediately()
1296 if (compare_ohci_cycle_count(cycle, d->processing_cycle.rx_start) >= 0) in process_rx_packets_intermediately()
1310 header_length -= length; in process_rx_packets_intermediately()
1314 s->ready_processing = true; in process_rx_packets_intermediately()
1315 wake_up(&s->ready_wait); in process_rx_packets_intermediately()
1317 if (d->replay.enable) in process_rx_packets_intermediately()
1318 s->ctx_data.rx.cache_pos = 0; in process_rx_packets_intermediately()
1324 if (s == d->irq_target) in process_rx_packets_intermediately()
1325 s->context->callback.sc = irq_target_callback; in process_rx_packets_intermediately()
1327 s->context->callback.sc = process_rx_packets; in process_rx_packets_intermediately()
1336 struct pkt_desc *desc = s->packet_descs_cursor; in process_tx_packets()
1342 if (s->packet_index < 0) in process_tx_packets()
1346 packet_count = header_length / s->ctx_data.tx.ctx_header_size; in process_tx_packets()
1351 if (err != -EAGAIN) { in process_tx_packets()
1356 struct amdtp_domain *d = s->domain; in process_tx_packets()
1360 if (d->replay.enable) in process_tx_packets()
1365 s->packet_descs_cursor = desc; in process_tx_packets()
1387 if (s->packet_index < 0) in drop_tx_packets()
1390 packets = header_length / s->ctx_data.tx.ctx_header_size; in drop_tx_packets()
1392 ctx_header += (packets - 1) * s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header); in drop_tx_packets()
1394 s->next_cycle = increment_ohci_cycle_count(cycle, 1); in drop_tx_packets()
1410 struct amdtp_domain *d = s->domain; in process_tx_packets_intermediately()
1415 if (s->packet_index < 0) in process_tx_packets_intermediately()
1418 packets = header_length / s->ctx_data.tx.ctx_header_size; in process_tx_packets_intermediately()
1425 if (compare_ohci_cycle_count(cycle, d->processing_cycle.tx_start) >= 0) in process_tx_packets_intermediately()
1428 ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(__be32); in process_tx_packets_intermediately()
1435 size_t length = s->ctx_data.tx.ctx_header_size * offset; in process_tx_packets_intermediately()
1442 header_length -= length; in process_tx_packets_intermediately()
1446 s->ready_processing = true; in process_tx_packets_intermediately()
1447 wake_up(&s->ready_wait); in process_tx_packets_intermediately()
1453 context->callback.sc = process_tx_packets; in process_tx_packets_intermediately()
1461 struct amdtp_domain *d = s->domain; in drop_tx_packets_initially()
1467 if (s->packet_index < 0) in drop_tx_packets_initially()
1470 count = header_length / s->ctx_data.tx.ctx_header_size; in drop_tx_packets_initially()
1472 // Attempt to detect any event in the batch of packets. in drop_tx_packets_initially()
1480 if (s->flags & CIP_NO_HEADER) { in drop_tx_packets_initially()
1481 data_blocks = payload_quads / s->data_block_quadlets; in drop_tx_packets_initially()
1488 payload_quads -= CIP_HEADER_QUADLETS; in drop_tx_packets_initially()
1490 if (s->flags & CIP_UNAWARE_SYT) { in drop_tx_packets_initially()
1491 data_blocks = payload_quads / s->data_block_quadlets; in drop_tx_packets_initially()
1500 data_blocks = payload_quads / s->data_block_quadlets; in drop_tx_packets_initially()
1507 ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(__be32); in drop_tx_packets_initially()
1513 s->ctx_data.tx.event_starts = true; in drop_tx_packets_initially()
1521 list_for_each_entry(s, &d->streams, list) { in drop_tx_packets_initially()
1522 if (s->direction == AMDTP_IN_STREAM) { in drop_tx_packets_initially()
1524 if (s->ctx_data.tx.event_starts) in drop_tx_packets_initially()
1532 list_for_each_entry(s, &d->streams, list) { in drop_tx_packets_initially()
1533 if (s->direction != AMDTP_IN_STREAM) in drop_tx_packets_initially()
1536 next_cycle = increment_ohci_cycle_count(s->next_cycle, in drop_tx_packets_initially()
1537 d->processing_cycle.tx_init_skip); in drop_tx_packets_initially()
1542 s->context->callback.sc = process_tx_packets_intermediately; in drop_tx_packets_initially()
1545 d->processing_cycle.tx_start = cycle; in drop_tx_packets_initially()
1554 list_for_each_entry(s, &d->streams, list) { in process_ctxs_in_domain()
1555 if (s != d->irq_target && amdtp_stream_running(s)) in process_ctxs_in_domain()
1556 fw_iso_context_flush_completions(s->context); in process_ctxs_in_domain()
1564 if (amdtp_stream_running(d->irq_target)) in process_ctxs_in_domain()
1565 cancel_stream(d->irq_target); in process_ctxs_in_domain()
1567 list_for_each_entry(s, &d->streams, list) { in process_ctxs_in_domain()
1577 struct amdtp_domain *d = s->domain; in irq_target_callback()
1587 struct amdtp_domain *d = s->domain; in irq_target_callback_intermediately()
1597 struct amdtp_domain *d = s->domain; in irq_target_callback_skip()
1603 if (d->replay.enable && !d->replay.on_the_fly) { in irq_target_callback_skip()
1608 list_for_each_entry(rx, &d->streams, list) { in irq_target_callback_skip()
1612 if (rx->direction != AMDTP_OUT_STREAM) in irq_target_callback_skip()
1616 tx = rx->ctx_data.rx.replay_target; in irq_target_callback_skip()
1618 if (cached_cycles > tx->ctx_data.tx.cache.size / 2) in irq_target_callback_skip()
1630 unsigned int cycle = s->next_cycle; in irq_target_callback_skip()
1631 list_for_each_entry(s, &d->streams, list) { in irq_target_callback_skip()
1632 if (s->direction != AMDTP_OUT_STREAM) in irq_target_callback_skip()
1635 if (compare_ohci_cycle_count(s->next_cycle, cycle) > 0) in irq_target_callback_skip()
1636 cycle = s->next_cycle; in irq_target_callback_skip()
1638 if (s == d->irq_target) in irq_target_callback_skip()
1639 s->context->callback.sc = irq_target_callback_intermediately; in irq_target_callback_skip()
1641 s->context->callback.sc = process_rx_packets_intermediately; in irq_target_callback_skip()
1644 d->processing_cycle.rx_start = cycle; in irq_target_callback_skip()
1648 // This is executed one time. For in-stream, first packet has come. For out-stream, prepared to
1655 struct amdtp_domain *d = s->domain; in amdtp_stream_first_callback()
1657 if (s->direction == AMDTP_IN_STREAM) { in amdtp_stream_first_callback()
1658 context->callback.sc = drop_tx_packets_initially; in amdtp_stream_first_callback()
1660 if (s == d->irq_target) in amdtp_stream_first_callback()
1661 context->callback.sc = irq_target_callback_skip; in amdtp_stream_first_callback()
1663 context->callback.sc = skip_rx_packets; in amdtp_stream_first_callback()
1666 context->callback.sc(context, tstamp, header_length, header, s); in amdtp_stream_first_callback()
1670 * amdtp_stream_start - start transferring packets
1684 bool is_irq_target = (s == s->domain->irq_target); in amdtp_stream_start()
1691 mutex_lock(&s->mutex); in amdtp_stream_start()
1694 (s->data_block_quadlets < 1))) { in amdtp_stream_start()
1695 err = -EBADFD; in amdtp_stream_start()
1699 if (s->direction == AMDTP_IN_STREAM) { in amdtp_stream_start()
1702 err = -EINVAL; in amdtp_stream_start()
1706 s->data_block_counter = UINT_MAX; in amdtp_stream_start()
1708 s->data_block_counter = 0; in amdtp_stream_start()
1712 if (s->direction == AMDTP_IN_STREAM) { in amdtp_stream_start()
1715 if (!(s->flags & CIP_NO_HEADER)) in amdtp_stream_start()
1726 err = iso_packets_buffer_init(&s->buffer, s->unit, queue_size, max_ctx_payload_size, dir); in amdtp_stream_start()
1729 s->queue_size = queue_size; in amdtp_stream_start()
1731 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card, in amdtp_stream_start()
1734 if (IS_ERR(s->context)) { in amdtp_stream_start()
1735 err = PTR_ERR(s->context); in amdtp_stream_start()
1736 if (err == -EBUSY) in amdtp_stream_start()
1737 dev_err(&s->unit->device, in amdtp_stream_start()
1738 "no free stream on this controller\n"); in amdtp_stream_start()
1744 if (s->direction == AMDTP_IN_STREAM) { in amdtp_stream_start()
1745 s->ctx_data.tx.max_ctx_payload_length = max_ctx_payload_size; in amdtp_stream_start()
1746 s->ctx_data.tx.ctx_header_size = ctx_header_size; in amdtp_stream_start()
1747 s->ctx_data.tx.event_starts = false; in amdtp_stream_start()
1749 if (s->domain->replay.enable) { in amdtp_stream_start()
1752 s->ctx_data.tx.cache.size = max_t(unsigned int, s->syt_interval * 2, in amdtp_stream_start()
1754 s->ctx_data.tx.cache.pos = 0; in amdtp_stream_start()
1755 s->ctx_data.tx.cache.descs = kcalloc(s->ctx_data.tx.cache.size, in amdtp_stream_start()
1756 sizeof(*s->ctx_data.tx.cache.descs), GFP_KERNEL); in amdtp_stream_start()
1757 if (!s->ctx_data.tx.cache.descs) { in amdtp_stream_start()
1758 err = -ENOMEM; in amdtp_stream_start()
1776 s->ctx_data.rx.seq.descs = kcalloc(queue_size, sizeof(*s->ctx_data.rx.seq.descs), GFP_KERNEL); in amdtp_stream_start()
1777 if (!s->ctx_data.rx.seq.descs) { in amdtp_stream_start()
1778 err = -ENOMEM; in amdtp_stream_start()
1781 s->ctx_data.rx.seq.size = queue_size; in amdtp_stream_start()
1782 s->ctx_data.rx.seq.pos = 0; in amdtp_stream_start()
1784 entry = &initial_state[s->sfc]; in amdtp_stream_start()
1785 s->ctx_data.rx.data_block_state = entry->data_block; in amdtp_stream_start()
1786 s->ctx_data.rx.syt_offset_state = entry->syt_offset; in amdtp_stream_start()
1787 s->ctx_data.rx.last_syt_offset = TICKS_PER_CYCLE; in amdtp_stream_start()
1789 s->ctx_data.rx.event_count = 0; in amdtp_stream_start()
1792 if (s->flags & CIP_NO_HEADER) in amdtp_stream_start()
1793 s->tag = TAG_NO_CIP_HEADER; in amdtp_stream_start()
1795 s->tag = TAG_CIP; in amdtp_stream_start()
1801 descs = kcalloc(s->queue_size + 8, sizeof(*descs), GFP_KERNEL); in amdtp_stream_start()
1803 err = -ENOMEM; in amdtp_stream_start()
1806 s->packet_descs = descs; in amdtp_stream_start()
1808 INIT_LIST_HEAD(&s->packet_descs_list); in amdtp_stream_start()
1809 for (i = 0; i < s->queue_size; ++i) { in amdtp_stream_start()
1810 INIT_LIST_HEAD(&descs->link); in amdtp_stream_start()
1811 list_add_tail(&descs->link, &s->packet_descs_list); in amdtp_stream_start()
1814 s->packet_descs_cursor = list_first_entry(&s->packet_descs_list, struct pkt_desc, link); in amdtp_stream_start()
1816 s->packet_index = 0; in amdtp_stream_start()
1820 if (s->direction == AMDTP_IN_STREAM) { in amdtp_stream_start()
1829 sched_irq = !((s->packet_index + 1) % in amdtp_stream_start()
1837 } while (s->packet_index > 0); in amdtp_stream_start()
1841 if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER)) in amdtp_stream_start()
1844 s->ready_processing = false; in amdtp_stream_start()
1845 err = fw_iso_context_start(s->context, -1, 0, tag); in amdtp_stream_start()
1849 mutex_unlock(&s->mutex); in amdtp_stream_start()
1853 kfree(s->packet_descs); in amdtp_stream_start()
1854 s->packet_descs = NULL; in amdtp_stream_start()
1856 if (s->direction == AMDTP_OUT_STREAM) { in amdtp_stream_start()
1857 kfree(s->ctx_data.rx.seq.descs); in amdtp_stream_start()
1859 if (s->domain->replay.enable) in amdtp_stream_start()
1860 kfree(s->ctx_data.tx.cache.descs); in amdtp_stream_start()
1862 fw_iso_context_destroy(s->context); in amdtp_stream_start()
1863 s->context = ERR_PTR(-1); in amdtp_stream_start()
1865 iso_packets_buffer_destroy(&s->buffer, s->unit); in amdtp_stream_start()
1867 mutex_unlock(&s->mutex); in amdtp_stream_start()
1873 * amdtp_domain_stream_pcm_pointer - get the PCM buffer position
1882 struct amdtp_stream *irq_target = d->irq_target; in amdtp_domain_stream_pcm_pointer()
1888 if (current_work() != &s->period_work) in amdtp_domain_stream_pcm_pointer()
1889 fw_iso_context_flush_completions(irq_target->context); in amdtp_domain_stream_pcm_pointer()
1892 return READ_ONCE(s->pcm_buffer_pointer); in amdtp_domain_stream_pcm_pointer()
1897 * amdtp_domain_stream_pcm_ack - acknowledge queued PCM frames
1905 struct amdtp_stream *irq_target = d->irq_target; in amdtp_domain_stream_pcm_ack()
1910 fw_iso_context_flush_completions(irq_target->context); in amdtp_domain_stream_pcm_ack()
1917 * amdtp_stream_update - update the stream after a bus reset
1923 WRITE_ONCE(s->source_node_id_field, in amdtp_stream_update()
1924 (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & CIP_SID_MASK); in amdtp_stream_update()
1929 * amdtp_stream_stop - stop sending packets
1937 mutex_lock(&s->mutex); in amdtp_stream_stop()
1940 mutex_unlock(&s->mutex); in amdtp_stream_stop()
1944 cancel_work_sync(&s->period_work); in amdtp_stream_stop()
1945 fw_iso_context_stop(s->context); in amdtp_stream_stop()
1946 fw_iso_context_destroy(s->context); in amdtp_stream_stop()
1947 s->context = ERR_PTR(-1); in amdtp_stream_stop()
1948 iso_packets_buffer_destroy(&s->buffer, s->unit); in amdtp_stream_stop()
1949 kfree(s->packet_descs); in amdtp_stream_stop()
1950 s->packet_descs = NULL; in amdtp_stream_stop()
1952 if (s->direction == AMDTP_OUT_STREAM) { in amdtp_stream_stop()
1953 kfree(s->ctx_data.rx.seq.descs); in amdtp_stream_stop()
1955 if (s->domain->replay.enable) in amdtp_stream_stop()
1956 kfree(s->ctx_data.tx.cache.descs); in amdtp_stream_stop()
1959 mutex_unlock(&s->mutex); in amdtp_stream_stop()
1963 * amdtp_stream_pcm_abort - abort the running PCM device
1967 * function first to stop the PCM device.
1973 pcm = READ_ONCE(s->pcm); in amdtp_stream_pcm_abort()
1980 * amdtp_domain_init - initialize an AMDTP domain structure
1985 INIT_LIST_HEAD(&d->streams); in amdtp_domain_init()
1987 d->events_per_period = 0; in amdtp_domain_init()
1994 * amdtp_domain_destroy - destroy an AMDTP domain structure
2005 * amdtp_domain_add_stream - register isoc context into the domain.
2016 list_for_each_entry(tmp, &d->streams, list) { in amdtp_domain_add_stream()
2018 return -EBUSY; in amdtp_domain_add_stream()
2021 list_add(&s->list, &d->streams); in amdtp_domain_add_stream()
2023 s->channel = channel; in amdtp_domain_add_stream()
2024 s->speed = speed; in amdtp_domain_add_stream()
2025 s->domain = d; in amdtp_domain_add_stream()
2039 list_for_each_entry(rx, &d->streams, list) { in make_association()
2040 if (rx->direction == AMDTP_OUT_STREAM) { in make_association()
2045 list_for_each_entry(s, &d->streams, list) { in make_association()
2046 if (s->direction == AMDTP_IN_STREAM) { in make_association()
2057 list_for_each_entry(s, &d->streams, list) { in make_association()
2058 if (s->direction == AMDTP_IN_STREAM) { in make_association()
2065 return -EINVAL; in make_association()
2068 rx->ctx_data.rx.replay_target = tx; in make_association()
2078 * amdtp_domain_start - start sending packets for isoc context in the domain.
2090 unsigned int events_per_buffer = d->events_per_buffer; in amdtp_domain_start()
2091 unsigned int events_per_period = d->events_per_period; in amdtp_domain_start()
2102 d->replay.enable = replay_seq; in amdtp_domain_start()
2103 d->replay.on_the_fly = replay_on_the_fly; in amdtp_domain_start()
2106 list_for_each_entry(s, &d->streams, list) { in amdtp_domain_start()
2107 if (s->direction == AMDTP_OUT_STREAM) { in amdtp_domain_start()
2113 return -ENXIO; in amdtp_domain_start()
2114 d->irq_target = s; in amdtp_domain_start()
2116 d->processing_cycle.tx_init_skip = tx_init_skip_cycles; in amdtp_domain_start()
2122 events_per_period = amdtp_rate_table[d->irq_target->sfc] / 100; in amdtp_domain_start()
2127 amdtp_rate_table[d->irq_target->sfc]); in amdtp_domain_start()
2129 list_for_each_entry(s, &d->streams, list) { in amdtp_domain_start()
2132 if (s->direction == AMDTP_OUT_STREAM && s == d->irq_target) { in amdtp_domain_start()
2134 amdtp_rate_table[d->irq_target->sfc]); in amdtp_domain_start()
2138 err = amdtp_stream_start(s, s->channel, s->speed, queue_size, idle_irq_interval); in amdtp_domain_start()
2145 list_for_each_entry(s, &d->streams, list) in amdtp_domain_start()
2152 * amdtp_domain_stop - stop sending packets for isoc context in the same domain.
2159 if (d->irq_target) in amdtp_domain_stop()
2160 amdtp_stream_stop(d->irq_target); in amdtp_domain_stop()
2162 list_for_each_entry_safe(s, next, &d->streams, list) { in amdtp_domain_stop()
2163 list_del(&s->list); in amdtp_domain_stop()
2165 if (s != d->irq_target) in amdtp_domain_stop()
2169 d->events_per_period = 0; in amdtp_domain_stop()
2170 d->irq_target = NULL; in amdtp_domain_stop()