Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef SOUND_FIREWIRE_AMDTP_H_INCLUDED
0003 #define SOUND_FIREWIRE_AMDTP_H_INCLUDED
0004 
0005 #include <linux/err.h>
0006 #include <linux/interrupt.h>
0007 #include <linux/mutex.h>
0008 #include <linux/sched.h>
0009 #include <sound/asound.h>
0010 #include "packets-buffer.h"
0011 
0012 /**
0013  * enum cip_flags - describes details of the streaming protocol
0014  * @CIP_NONBLOCKING: In non-blocking mode, each packet contains
0015  *  sample_rate/8000 samples, with rounding up or down to adjust
0016  *  for clock skew and left-over fractional samples.  This should
0017  *  be used if supported by the device.
0018  * @CIP_BLOCKING: In blocking mode, each packet contains either zero or
0019  *  SYT_INTERVAL samples, with these two types alternating so that
0020  *  the overall sample rate comes out right.
0021  * @CIP_EMPTY_WITH_TAG0: Only for in-stream. Empty in-packets have TAG0.
0022  * @CIP_DBC_IS_END_EVENT: The value of dbc in an packet corresponds to the end
0023  * of event in the packet. Out of IEC 61883.
0024  * @CIP_WRONG_DBS: Only for in-stream. The value of dbs is wrong in in-packets.
0025  *  The value of data_block_quadlets is used instead of reported value.
0026  * @CIP_SKIP_DBC_ZERO_CHECK: Only for in-stream.  Packets with zero in dbc is
0027  *  skipped for detecting discontinuity.
0028  * @CIP_EMPTY_HAS_WRONG_DBC: Only for in-stream. The value of dbc in empty
0029  *  packet is wrong but the others are correct.
0030  * @CIP_JUMBO_PAYLOAD: Only for in-stream. The number of data blocks in an
0031  *  packet is larger than IEC 61883-6 defines. Current implementation
0032  *  allows 5 times as large as IEC 61883-6 defines.
0033  * @CIP_HEADER_WITHOUT_EOH: Only for in-stream. CIP Header doesn't include
0034  *  valid EOH.
0035  * @CIP_NO_HEADERS: a lack of headers in packets
0036  * @CIP_UNALIGHED_DBC: Only for in-stream. The value of dbc is not alighed to
0037  *  the value of current SYT_INTERVAL; e.g. initial value is not zero.
0038  * @CIP_UNAWARE_SYT: For outgoing packet, the value in SYT field of CIP is 0xffff.
0039  *  For incoming packet, the value in SYT field of CIP is not handled.
0040  */
0041 enum cip_flags {
0042     CIP_NONBLOCKING     = 0x00,
0043     CIP_BLOCKING        = 0x01,
0044     CIP_EMPTY_WITH_TAG0 = 0x02,
0045     CIP_DBC_IS_END_EVENT    = 0x04,
0046     CIP_WRONG_DBS       = 0x08,
0047     CIP_SKIP_DBC_ZERO_CHECK = 0x10,
0048     CIP_EMPTY_HAS_WRONG_DBC = 0x20,
0049     CIP_JUMBO_PAYLOAD   = 0x40,
0050     CIP_HEADER_WITHOUT_EOH  = 0x80,
0051     CIP_NO_HEADER       = 0x100,
0052     CIP_UNALIGHED_DBC   = 0x200,
0053     CIP_UNAWARE_SYT     = 0x400,
0054 };
0055 
0056 /**
0057  * enum cip_sfc - supported Sampling Frequency Codes (SFCs)
0058  * @CIP_SFC_32000:   32,000 data blocks
0059  * @CIP_SFC_44100:   44,100 data blocks
0060  * @CIP_SFC_48000:   48,000 data blocks
0061  * @CIP_SFC_88200:   88,200 data blocks
0062  * @CIP_SFC_96000:   96,000 data blocks
0063  * @CIP_SFC_176400: 176,400 data blocks
0064  * @CIP_SFC_192000: 192,000 data blocks
0065  * @CIP_SFC_COUNT: the number of supported SFCs
0066  *
0067  * These values are used to show nominal Sampling Frequency Code in
0068  * Format Dependent Field (FDF) of AMDTP packet header. In IEC 61883-6:2002,
0069  * this code means the number of events per second. Actually the code
0070  * represents the number of data blocks transferred per second in an AMDTP
0071  * stream.
0072  *
0073  * In IEC 61883-6:2005, some extensions were added to support more types of
0074  * data such as 'One Bit LInear Audio', therefore the meaning of SFC became
0075  * different depending on the types.
0076  *
0077  * Currently our implementation is compatible with IEC 61883-6:2002.
0078  */
0079 enum cip_sfc {
0080     CIP_SFC_32000  = 0,
0081     CIP_SFC_44100  = 1,
0082     CIP_SFC_48000  = 2,
0083     CIP_SFC_88200  = 3,
0084     CIP_SFC_96000  = 4,
0085     CIP_SFC_176400 = 5,
0086     CIP_SFC_192000 = 6,
0087     CIP_SFC_COUNT
0088 };
0089 
0090 struct fw_unit;
0091 struct fw_iso_context;
0092 struct snd_pcm_substream;
0093 struct snd_pcm_runtime;
0094 
0095 enum amdtp_stream_direction {
0096     AMDTP_OUT_STREAM = 0,
0097     AMDTP_IN_STREAM
0098 };
0099 
0100 struct pkt_desc {
0101     u32 cycle;
0102     u32 syt;
0103     unsigned int data_blocks;
0104     unsigned int data_block_counter;
0105     __be32 *ctx_payload;
0106 };
0107 
0108 struct amdtp_stream;
0109 typedef unsigned int (*amdtp_stream_process_ctx_payloads_t)(
0110                         struct amdtp_stream *s,
0111                         const struct pkt_desc *desc,
0112                         unsigned int packets,
0113                         struct snd_pcm_substream *pcm);
0114 
0115 struct amdtp_domain;
0116 struct amdtp_stream {
0117     struct fw_unit *unit;
0118     // The combination of cip_flags enumeration-constants.
0119     unsigned int flags;
0120     enum amdtp_stream_direction direction;
0121     struct mutex mutex;
0122 
0123     /* For packet processing. */
0124     struct fw_iso_context *context;
0125     struct iso_packets_buffer buffer;
0126     unsigned int queue_size;
0127     int packet_index;
0128     struct pkt_desc *pkt_descs;
0129     int tag;
0130     union {
0131         struct {
0132             unsigned int ctx_header_size;
0133 
0134             // limit for payload of iso packet.
0135             unsigned int max_ctx_payload_length;
0136 
0137             // For quirks of CIP headers.
0138             // Fixed interval of dbc between previos/current
0139             // packets.
0140             unsigned int dbc_interval;
0141 
0142             // The device starts multiplexing events to the packet.
0143             bool event_starts;
0144 
0145             struct {
0146                 struct seq_desc *descs;
0147                 unsigned int size;
0148                 unsigned int tail;
0149             } cache;
0150         } tx;
0151         struct {
0152             // To generate CIP header.
0153             unsigned int fdf;
0154 
0155             // To generate constant hardware IRQ.
0156             unsigned int event_count;
0157 
0158             // To calculate CIP data blocks and tstamp.
0159             struct {
0160                 struct seq_desc *descs;
0161                 unsigned int size;
0162                 unsigned int tail;
0163                 unsigned int head;
0164             } seq;
0165 
0166             unsigned int data_block_state;
0167             unsigned int syt_offset_state;
0168             unsigned int last_syt_offset;
0169 
0170             struct amdtp_stream *replay_target;
0171             unsigned int cache_head;
0172         } rx;
0173     } ctx_data;
0174 
0175     /* For CIP headers. */
0176     unsigned int source_node_id_field;
0177     unsigned int data_block_quadlets;
0178     unsigned int data_block_counter;
0179     unsigned int sph;
0180     unsigned int fmt;
0181 
0182     // Internal flags.
0183     unsigned int transfer_delay;
0184     enum cip_sfc sfc;
0185     unsigned int syt_interval;
0186 
0187     /* For a PCM substream processing. */
0188     struct snd_pcm_substream *pcm;
0189     snd_pcm_uframes_t pcm_buffer_pointer;
0190     unsigned int pcm_period_pointer;
0191 
0192     // To start processing content of packets at the same cycle in several contexts for
0193     // each direction.
0194     bool ready_processing;
0195     wait_queue_head_t ready_wait;
0196     unsigned int next_cycle;
0197 
0198     /* For backends to process data blocks. */
0199     void *protocol;
0200     amdtp_stream_process_ctx_payloads_t process_ctx_payloads;
0201 
0202     // For domain.
0203     int channel;
0204     int speed;
0205     struct list_head list;
0206     struct amdtp_domain *domain;
0207 };
0208 
0209 int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
0210               enum amdtp_stream_direction dir, unsigned int flags,
0211               unsigned int fmt,
0212               amdtp_stream_process_ctx_payloads_t process_ctx_payloads,
0213               unsigned int protocol_size);
0214 void amdtp_stream_destroy(struct amdtp_stream *s);
0215 
0216 int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
0217                 unsigned int data_block_quadlets);
0218 unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s);
0219 
0220 void amdtp_stream_update(struct amdtp_stream *s);
0221 
0222 int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
0223                     struct snd_pcm_runtime *runtime);
0224 
0225 void amdtp_stream_pcm_prepare(struct amdtp_stream *s);
0226 void amdtp_stream_pcm_abort(struct amdtp_stream *s);
0227 
0228 extern const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT];
0229 extern const unsigned int amdtp_rate_table[CIP_SFC_COUNT];
0230 
0231 /**
0232  * amdtp_stream_running - check stream is running or not
0233  * @s: the AMDTP stream
0234  *
0235  * If this function returns true, the stream is running.
0236  */
0237 static inline bool amdtp_stream_running(struct amdtp_stream *s)
0238 {
0239     return !IS_ERR(s->context);
0240 }
0241 
0242 /**
0243  * amdtp_streaming_error - check for streaming error
0244  * @s: the AMDTP stream
0245  *
0246  * If this function returns true, the stream's packet queue has stopped due to
0247  * an asynchronous error.
0248  */
0249 static inline bool amdtp_streaming_error(struct amdtp_stream *s)
0250 {
0251     return s->packet_index < 0;
0252 }
0253 
0254 /**
0255  * amdtp_stream_pcm_running - check PCM substream is running or not
0256  * @s: the AMDTP stream
0257  *
0258  * If this function returns true, PCM substream in the AMDTP stream is running.
0259  */
0260 static inline bool amdtp_stream_pcm_running(struct amdtp_stream *s)
0261 {
0262     return !!s->pcm;
0263 }
0264 
0265 /**
0266  * amdtp_stream_pcm_trigger - start/stop playback from a PCM device
0267  * @s: the AMDTP stream
0268  * @pcm: the PCM device to be started, or %NULL to stop the current device
0269  *
0270  * Call this function on a running isochronous stream to enable the actual
0271  * transmission of PCM data.  This function should be called from the PCM
0272  * device's .trigger callback.
0273  */
0274 static inline void amdtp_stream_pcm_trigger(struct amdtp_stream *s,
0275                         struct snd_pcm_substream *pcm)
0276 {
0277     WRITE_ONCE(s->pcm, pcm);
0278 }
0279 
0280 static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)
0281 {
0282     return sfc & 1;
0283 }
0284 
0285 struct seq_desc {
0286     unsigned int syt_offset;
0287     unsigned int data_blocks;
0288 };
0289 
0290 struct amdtp_domain {
0291     struct list_head streams;
0292 
0293     unsigned int events_per_period;
0294     unsigned int events_per_buffer;
0295 
0296     struct amdtp_stream *irq_target;
0297 
0298     struct {
0299         unsigned int tx_init_skip;
0300         unsigned int tx_start;
0301         unsigned int rx_start;
0302     } processing_cycle;
0303 
0304     struct {
0305         bool enable:1;
0306         bool on_the_fly:1;
0307     } replay;
0308 };
0309 
0310 int amdtp_domain_init(struct amdtp_domain *d);
0311 void amdtp_domain_destroy(struct amdtp_domain *d);
0312 
0313 int amdtp_domain_add_stream(struct amdtp_domain *d, struct amdtp_stream *s,
0314                 int channel, int speed);
0315 
0316 int amdtp_domain_start(struct amdtp_domain *d, unsigned int tx_init_skip_cycles, bool replay_seq,
0317                bool replay_on_the_fly);
0318 void amdtp_domain_stop(struct amdtp_domain *d);
0319 
0320 static inline int amdtp_domain_set_events_per_period(struct amdtp_domain *d,
0321                         unsigned int events_per_period,
0322                         unsigned int events_per_buffer)
0323 {
0324     d->events_per_period = events_per_period;
0325     d->events_per_buffer = events_per_buffer;
0326 
0327     return 0;
0328 }
0329 
0330 unsigned long amdtp_domain_stream_pcm_pointer(struct amdtp_domain *d,
0331                           struct amdtp_stream *s);
0332 int amdtp_domain_stream_pcm_ack(struct amdtp_domain *d, struct amdtp_stream *s);
0333 
0334 /**
0335  * amdtp_domain_wait_ready - sleep till being ready to process packets or timeout
0336  * @d: the AMDTP domain
0337  * @timeout_ms: msec till timeout
0338  *
0339  * If this function return false, the AMDTP domain should be stopped.
0340  */
0341 static inline bool amdtp_domain_wait_ready(struct amdtp_domain *d, unsigned int timeout_ms)
0342 {
0343     struct amdtp_stream *s;
0344 
0345     list_for_each_entry(s, &d->streams, list) {
0346         unsigned int j = msecs_to_jiffies(timeout_ms);
0347 
0348         if (wait_event_interruptible_timeout(s->ready_wait, s->ready_processing, j) <= 0)
0349             return false;
0350     }
0351 
0352     return true;
0353 }
0354 
0355 #endif