/* SPDX-License-Identifier: GPL-2.0 */1#ifndef SOUND_FIREWIRE_AMDTP_H_INCLUDED2#define SOUND_FIREWIRE_AMDTP_H_INCLUDED34#include <linux/err.h>5#include <linux/interrupt.h>6#include <linux/mutex.h>7#include <linux/sched.h>8#include <sound/asound.h>9#include "packets-buffer.h"1011/**12* enum cip_flags - describes details of the streaming protocol13* @CIP_NONBLOCKING: In non-blocking mode, each packet contains14* sample_rate/8000 samples, with rounding up or down to adjust15* for clock skew and left-over fractional samples. This should16* be used if supported by the device.17* @CIP_BLOCKING: In blocking mode, each packet contains either zero or18* SYT_INTERVAL samples, with these two types alternating so that19* the overall sample rate comes out right.20* @CIP_EMPTY_WITH_TAG0: Only for in-stream. Empty in-packets have TAG0.21* @CIP_DBC_IS_END_EVENT: The value of dbc in an packet corresponds to the end22* of event in the packet. Out of IEC 61883.23* @CIP_WRONG_DBS: Only for in-stream. The value of dbs is wrong in in-packets.24* The value of data_block_quadlets is used instead of reported value.25* @CIP_SKIP_DBC_ZERO_CHECK: Only for in-stream. Packets with zero in dbc is26* skipped for detecting discontinuity.27* @CIP_EMPTY_HAS_WRONG_DBC: Only for in-stream. The value of dbc in empty28* packet is wrong but the others are correct.29* @CIP_JUMBO_PAYLOAD: Only for in-stream. The number of data blocks in an30* packet is larger than IEC 61883-6 defines. Current implementation31* allows 5 times as large as IEC 61883-6 defines.32* @CIP_HEADER_WITHOUT_EOH: Only for in-stream. CIP Header doesn't include33* valid EOH.34* @CIP_NO_HEADERS: a lack of headers in packets35* @CIP_UNALIGHED_DBC: Only for in-stream. The value of dbc is not alighed to36* the value of current SYT_INTERVAL; e.g. initial value is not zero.37* @CIP_UNAWARE_SYT: For outgoing packet, the value in SYT field of CIP is 0xffff.38* For incoming packet, the value in SYT field of CIP is not handled.39* @CIP_DBC_IS_PAYLOAD_QUADLETS: Available for incoming packet, and only effective with40* CIP_DBC_IS_END_EVENT flag. The value of dbc field is the number of accumulated quadlets41* in CIP payload, instead of the number of accumulated data blocks.42*/43enum cip_flags {44CIP_NONBLOCKING = 0x00,45CIP_BLOCKING = 0x01,46CIP_EMPTY_WITH_TAG0 = 0x02,47CIP_DBC_IS_END_EVENT = 0x04,48CIP_WRONG_DBS = 0x08,49CIP_SKIP_DBC_ZERO_CHECK = 0x10,50CIP_EMPTY_HAS_WRONG_DBC = 0x20,51CIP_JUMBO_PAYLOAD = 0x40,52CIP_HEADER_WITHOUT_EOH = 0x80,53CIP_NO_HEADER = 0x100,54CIP_UNALIGHED_DBC = 0x200,55CIP_UNAWARE_SYT = 0x400,56CIP_DBC_IS_PAYLOAD_QUADLETS = 0x800,57};5859/**60* enum cip_sfc - supported Sampling Frequency Codes (SFCs)61* @CIP_SFC_32000: 32,000 data blocks62* @CIP_SFC_44100: 44,100 data blocks63* @CIP_SFC_48000: 48,000 data blocks64* @CIP_SFC_88200: 88,200 data blocks65* @CIP_SFC_96000: 96,000 data blocks66* @CIP_SFC_176400: 176,400 data blocks67* @CIP_SFC_192000: 192,000 data blocks68* @CIP_SFC_COUNT: the number of supported SFCs69*70* These values are used to show nominal Sampling Frequency Code in71* Format Dependent Field (FDF) of AMDTP packet header. In IEC 61883-6:2002,72* this code means the number of events per second. Actually the code73* represents the number of data blocks transferred per second in an AMDTP74* stream.75*76* In IEC 61883-6:2005, some extensions were added to support more types of77* data such as 'One Bit LInear Audio', therefore the meaning of SFC became78* different depending on the types.79*80* Currently our implementation is compatible with IEC 61883-6:2002.81*/82enum cip_sfc {83CIP_SFC_32000 = 0,84CIP_SFC_44100 = 1,85CIP_SFC_48000 = 2,86CIP_SFC_88200 = 3,87CIP_SFC_96000 = 4,88CIP_SFC_176400 = 5,89CIP_SFC_192000 = 6,90CIP_SFC_COUNT91};9293struct fw_unit;94struct fw_iso_context;95struct snd_pcm_substream;96struct snd_pcm_runtime;9798enum amdtp_stream_direction {99AMDTP_OUT_STREAM = 0,100AMDTP_IN_STREAM101};102103struct pkt_desc {104u32 cycle;105u32 syt;106unsigned int data_blocks;107unsigned int data_block_counter;108__be32 *ctx_payload;109struct list_head link;110};111112struct amdtp_stream;113typedef void (*amdtp_stream_process_ctx_payloads_t)(struct amdtp_stream *s,114const struct pkt_desc *desc,115unsigned int count,116struct snd_pcm_substream *pcm);117118struct amdtp_domain;119struct amdtp_stream {120struct fw_unit *unit;121// The combination of cip_flags enumeration-constants.122unsigned int flags;123enum amdtp_stream_direction direction;124struct mutex mutex;125126/* For packet processing. */127struct fw_iso_context *context;128struct iso_packets_buffer buffer;129unsigned int queue_size;130int packet_index;131struct pkt_desc *packet_descs;132struct list_head packet_descs_list;133struct pkt_desc *packet_descs_cursor;134int tag;135union {136struct {137unsigned int ctx_header_size;138139// limit for payload of iso packet.140unsigned int max_ctx_payload_length;141142// For quirks of CIP headers.143// Fixed interval of dbc between previos/current144// packets.145unsigned int dbc_interval;146147// The device starts multiplexing events to the packet.148bool event_starts;149150struct {151struct seq_desc *descs;152unsigned int size;153unsigned int pos;154} cache;155} tx;156struct {157// To generate CIP header.158unsigned int fdf;159160// To generate constant hardware IRQ.161unsigned int event_count;162163// To calculate CIP data blocks and tstamp.164struct {165struct seq_desc *descs;166unsigned int size;167unsigned int pos;168} seq;169170unsigned int data_block_state;171unsigned int syt_offset_state;172unsigned int last_syt_offset;173174struct amdtp_stream *replay_target;175unsigned int cache_pos;176} rx;177} ctx_data;178179/* For CIP headers. */180unsigned int source_node_id_field;181unsigned int data_block_quadlets;182unsigned int data_block_counter;183unsigned int sph;184unsigned int fmt;185186// Internal flags.187unsigned int transfer_delay;188enum cip_sfc sfc;189unsigned int syt_interval;190191/* For a PCM substream processing. */192struct snd_pcm_substream *pcm;193struct work_struct period_work;194snd_pcm_uframes_t pcm_buffer_pointer;195unsigned int pcm_period_pointer;196unsigned int pcm_frame_multiplier;197198// To start processing content of packets at the same cycle in several contexts for199// each direction.200bool ready_processing;201wait_queue_head_t ready_wait;202unsigned int next_cycle;203204/* For backends to process data blocks. */205void *protocol;206amdtp_stream_process_ctx_payloads_t process_ctx_payloads;207208// For domain.209int channel;210int speed;211struct list_head list;212struct amdtp_domain *domain;213};214215int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,216enum amdtp_stream_direction dir, unsigned int flags,217unsigned int fmt,218amdtp_stream_process_ctx_payloads_t process_ctx_payloads,219unsigned int protocol_size);220void amdtp_stream_destroy(struct amdtp_stream *s);221222int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,223unsigned int data_block_quadlets, unsigned int pcm_frame_multiplier);224unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s);225226void amdtp_stream_update(struct amdtp_stream *s);227228int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,229struct snd_pcm_runtime *runtime);230231void amdtp_stream_pcm_prepare(struct amdtp_stream *s);232void amdtp_stream_pcm_abort(struct amdtp_stream *s);233234extern const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT];235extern const unsigned int amdtp_rate_table[CIP_SFC_COUNT];236237/**238* amdtp_stream_running - check stream is running or not239* @s: the AMDTP stream240*241* If this function returns true, the stream is running.242*/243static inline bool amdtp_stream_running(struct amdtp_stream *s)244{245return !IS_ERR(s->context);246}247248/**249* amdtp_streaming_error - check for streaming error250* @s: the AMDTP stream251*252* If this function returns true, the stream's packet queue has stopped due to253* an asynchronous error.254*/255static inline bool amdtp_streaming_error(struct amdtp_stream *s)256{257return s->packet_index < 0;258}259260/**261* amdtp_stream_pcm_running - check PCM substream is running or not262* @s: the AMDTP stream263*264* If this function returns true, PCM substream in the AMDTP stream is running.265*/266static inline bool amdtp_stream_pcm_running(struct amdtp_stream *s)267{268return !!s->pcm;269}270271/**272* amdtp_stream_pcm_trigger - start/stop playback from a PCM device273* @s: the AMDTP stream274* @pcm: the PCM device to be started, or %NULL to stop the current device275*276* Call this function on a running isochronous stream to enable the actual277* transmission of PCM data. This function should be called from the PCM278* device's .trigger callback.279*/280static inline void amdtp_stream_pcm_trigger(struct amdtp_stream *s,281struct snd_pcm_substream *pcm)282{283WRITE_ONCE(s->pcm, pcm);284}285286/**287* amdtp_stream_next_packet_desc - retrieve next descriptor for amdtp packet.288* @s: the AMDTP stream289* @desc: the descriptor of packet290*291* This macro computes next descriptor so that the list of descriptors behaves circular queue.292*/293#define amdtp_stream_next_packet_desc(s, desc) \294list_next_entry_circular(desc, &s->packet_descs_list, link)295296static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)297{298return sfc & 1;299}300301struct seq_desc {302unsigned int syt_offset;303unsigned int data_blocks;304};305306struct amdtp_domain {307struct list_head streams;308309unsigned int events_per_period;310unsigned int events_per_buffer;311312struct amdtp_stream *irq_target;313314struct {315unsigned int tx_init_skip;316unsigned int tx_start;317unsigned int rx_start;318} processing_cycle;319320struct {321bool enable:1;322bool on_the_fly:1;323} replay;324};325326int amdtp_domain_init(struct amdtp_domain *d);327void amdtp_domain_destroy(struct amdtp_domain *d);328329int amdtp_domain_add_stream(struct amdtp_domain *d, struct amdtp_stream *s,330int channel, int speed);331332int amdtp_domain_start(struct amdtp_domain *d, unsigned int tx_init_skip_cycles, bool replay_seq,333bool replay_on_the_fly);334void amdtp_domain_stop(struct amdtp_domain *d);335336static inline int amdtp_domain_set_events_per_period(struct amdtp_domain *d,337unsigned int events_per_period,338unsigned int events_per_buffer)339{340d->events_per_period = events_per_period;341d->events_per_buffer = events_per_buffer;342343return 0;344}345346unsigned long amdtp_domain_stream_pcm_pointer(struct amdtp_domain *d,347struct amdtp_stream *s);348int amdtp_domain_stream_pcm_ack(struct amdtp_domain *d, struct amdtp_stream *s);349350/**351* amdtp_domain_wait_ready - sleep till being ready to process packets or timeout352* @d: the AMDTP domain353* @timeout_ms: msec till timeout354*355* If this function return false, the AMDTP domain should be stopped.356*/357static inline bool amdtp_domain_wait_ready(struct amdtp_domain *d, unsigned int timeout_ms)358{359struct amdtp_stream *s;360361list_for_each_entry(s, &d->streams, list) {362unsigned int j = msecs_to_jiffies(timeout_ms);363364if (wait_event_interruptible_timeout(s->ready_wait, s->ready_processing, j) <= 0)365return false;366}367368return true;369}370371#endif372373374