1d67c46b9STakashi Sakamoto #ifndef SOUND_FIREWIRE_AMDTP_H_INCLUDED 2d67c46b9STakashi Sakamoto #define SOUND_FIREWIRE_AMDTP_H_INCLUDED 3d67c46b9STakashi Sakamoto 4d67c46b9STakashi Sakamoto #include <linux/err.h> 5d67c46b9STakashi Sakamoto #include <linux/interrupt.h> 6d67c46b9STakashi Sakamoto #include <linux/mutex.h> 75955815eSTakashi Sakamoto #include <linux/sched.h> 8d67c46b9STakashi Sakamoto #include <sound/asound.h> 9d67c46b9STakashi Sakamoto #include "packets-buffer.h" 10d67c46b9STakashi Sakamoto 11d67c46b9STakashi Sakamoto /** 12d67c46b9STakashi Sakamoto * enum cip_flags - describes details of the streaming protocol 13d67c46b9STakashi Sakamoto * @CIP_NONBLOCKING: In non-blocking mode, each packet contains 14d67c46b9STakashi Sakamoto * sample_rate/8000 samples, with rounding up or down to adjust 15d67c46b9STakashi Sakamoto * for clock skew and left-over fractional samples. This should 16d67c46b9STakashi Sakamoto * be used if supported by the device. 17d67c46b9STakashi Sakamoto * @CIP_BLOCKING: In blocking mode, each packet contains either zero or 18d67c46b9STakashi Sakamoto * SYT_INTERVAL samples, with these two types alternating so that 19d67c46b9STakashi Sakamoto * the overall sample rate comes out right. 20d67c46b9STakashi Sakamoto * @CIP_EMPTY_WITH_TAG0: Only for in-stream. Empty in-packets have TAG0. 219dae017bSTakashi Sakamoto * @CIP_DBC_IS_END_EVENT: The value of dbc in an packet corresponds to the end 229dae017bSTakashi Sakamoto * of event in the packet. Out of IEC 61883. 23d67c46b9STakashi Sakamoto * @CIP_WRONG_DBS: Only for in-stream. The value of dbs is wrong in in-packets. 24d67c46b9STakashi Sakamoto * The value of data_block_quadlets is used instead of reported value. 25d67c46b9STakashi Sakamoto * @CIP_SKIP_DBC_ZERO_CHECK: Only for in-stream. Packets with zero in dbc is 26d67c46b9STakashi Sakamoto * skipped for detecting discontinuity. 27d67c46b9STakashi Sakamoto * @CIP_EMPTY_HAS_WRONG_DBC: Only for in-stream. The value of dbc in empty 28d67c46b9STakashi Sakamoto * packet is wrong but the others are correct. 29d67c46b9STakashi Sakamoto * @CIP_JUMBO_PAYLOAD: Only for in-stream. The number of data blocks in an 30d67c46b9STakashi Sakamoto * packet is larger than IEC 61883-6 defines. Current implementation 31d67c46b9STakashi Sakamoto * allows 5 times as large as IEC 61883-6 defines. 322128f78fSTakashi Sakamoto * @CIP_HEADER_WITHOUT_EOH: Only for in-stream. CIP Header doesn't include 332128f78fSTakashi Sakamoto * valid EOH. 343b196c39STakashi Sakamoto * @CIP_NO_HEADERS: a lack of headers in packets 35d67c46b9STakashi Sakamoto */ 36d67c46b9STakashi Sakamoto enum cip_flags { 37d67c46b9STakashi Sakamoto CIP_NONBLOCKING = 0x00, 38d67c46b9STakashi Sakamoto CIP_BLOCKING = 0x01, 39dec63cc8STakashi Sakamoto CIP_EMPTY_WITH_TAG0 = 0x02, 40dec63cc8STakashi Sakamoto CIP_DBC_IS_END_EVENT = 0x04, 41dec63cc8STakashi Sakamoto CIP_WRONG_DBS = 0x08, 42dec63cc8STakashi Sakamoto CIP_SKIP_DBC_ZERO_CHECK = 0x10, 4362f00e40STakashi Sakamoto CIP_EMPTY_HAS_WRONG_DBC = 0x20, 4462f00e40STakashi Sakamoto CIP_JUMBO_PAYLOAD = 0x40, 452128f78fSTakashi Sakamoto CIP_HEADER_WITHOUT_EOH = 0x80, 463b196c39STakashi Sakamoto CIP_NO_HEADER = 0x100, 47d67c46b9STakashi Sakamoto }; 48d67c46b9STakashi Sakamoto 49d67c46b9STakashi Sakamoto /** 50d67c46b9STakashi Sakamoto * enum cip_sfc - supported Sampling Frequency Codes (SFCs) 51d67c46b9STakashi Sakamoto * @CIP_SFC_32000: 32,000 data blocks 52d67c46b9STakashi Sakamoto * @CIP_SFC_44100: 44,100 data blocks 53d67c46b9STakashi Sakamoto * @CIP_SFC_48000: 48,000 data blocks 54d67c46b9STakashi Sakamoto * @CIP_SFC_88200: 88,200 data blocks 55d67c46b9STakashi Sakamoto * @CIP_SFC_96000: 96,000 data blocks 56d67c46b9STakashi Sakamoto * @CIP_SFC_176400: 176,400 data blocks 57d67c46b9STakashi Sakamoto * @CIP_SFC_192000: 192,000 data blocks 58d67c46b9STakashi Sakamoto * @CIP_SFC_COUNT: the number of supported SFCs 59d67c46b9STakashi Sakamoto * 60d67c46b9STakashi Sakamoto * These values are used to show nominal Sampling Frequency Code in 61d67c46b9STakashi Sakamoto * Format Dependent Field (FDF) of AMDTP packet header. In IEC 61883-6:2002, 62d67c46b9STakashi Sakamoto * this code means the number of events per second. Actually the code 63d67c46b9STakashi Sakamoto * represents the number of data blocks transferred per second in an AMDTP 64d67c46b9STakashi Sakamoto * stream. 65d67c46b9STakashi Sakamoto * 66d67c46b9STakashi Sakamoto * In IEC 61883-6:2005, some extensions were added to support more types of 67d67c46b9STakashi Sakamoto * data such as 'One Bit LInear Audio', therefore the meaning of SFC became 68d67c46b9STakashi Sakamoto * different depending on the types. 69d67c46b9STakashi Sakamoto * 70d67c46b9STakashi Sakamoto * Currently our implementation is compatible with IEC 61883-6:2002. 71d67c46b9STakashi Sakamoto */ 72d67c46b9STakashi Sakamoto enum cip_sfc { 73d67c46b9STakashi Sakamoto CIP_SFC_32000 = 0, 74d67c46b9STakashi Sakamoto CIP_SFC_44100 = 1, 75d67c46b9STakashi Sakamoto CIP_SFC_48000 = 2, 76d67c46b9STakashi Sakamoto CIP_SFC_88200 = 3, 77d67c46b9STakashi Sakamoto CIP_SFC_96000 = 4, 78d67c46b9STakashi Sakamoto CIP_SFC_176400 = 5, 79d67c46b9STakashi Sakamoto CIP_SFC_192000 = 6, 80d67c46b9STakashi Sakamoto CIP_SFC_COUNT 81d67c46b9STakashi Sakamoto }; 82d67c46b9STakashi Sakamoto 83d67c46b9STakashi Sakamoto struct fw_unit; 84d67c46b9STakashi Sakamoto struct fw_iso_context; 85d67c46b9STakashi Sakamoto struct snd_pcm_substream; 86d67c46b9STakashi Sakamoto struct snd_pcm_runtime; 87d67c46b9STakashi Sakamoto 88d67c46b9STakashi Sakamoto enum amdtp_stream_direction { 89d67c46b9STakashi Sakamoto AMDTP_OUT_STREAM = 0, 90d67c46b9STakashi Sakamoto AMDTP_IN_STREAM 91d67c46b9STakashi Sakamoto }; 92d67c46b9STakashi Sakamoto 93df075feeSTakashi Sakamoto struct amdtp_stream; 94df075feeSTakashi Sakamoto typedef unsigned int (*amdtp_stream_process_data_blocks_t)( 95df075feeSTakashi Sakamoto struct amdtp_stream *s, 96df075feeSTakashi Sakamoto __be32 *buffer, 97df075feeSTakashi Sakamoto unsigned int data_blocks, 98df075feeSTakashi Sakamoto unsigned int *syt); 99d67c46b9STakashi Sakamoto struct amdtp_stream { 100d67c46b9STakashi Sakamoto struct fw_unit *unit; 101d67c46b9STakashi Sakamoto enum cip_flags flags; 102d67c46b9STakashi Sakamoto enum amdtp_stream_direction direction; 103d67c46b9STakashi Sakamoto struct mutex mutex; 104d67c46b9STakashi Sakamoto 105d67c46b9STakashi Sakamoto /* For packet processing. */ 106d67c46b9STakashi Sakamoto struct fw_iso_context *context; 107d67c46b9STakashi Sakamoto struct iso_packets_buffer buffer; 108d67c46b9STakashi Sakamoto int packet_index; 1093b196c39STakashi Sakamoto int tag; 1103b196c39STakashi Sakamoto int (*handle_packet)(struct amdtp_stream *s, 1113b196c39STakashi Sakamoto unsigned int payload_quadlets, unsigned int cycle, 1123b196c39STakashi Sakamoto unsigned int index); 113f91c9d76STakashi Sakamoto unsigned int max_payload_length; 114d67c46b9STakashi Sakamoto 115d67c46b9STakashi Sakamoto /* For CIP headers. */ 116d67c46b9STakashi Sakamoto unsigned int source_node_id_field; 117d67c46b9STakashi Sakamoto unsigned int data_block_quadlets; 118d67c46b9STakashi Sakamoto unsigned int data_block_counter; 1199863874fSTakashi Sakamoto unsigned int sph; 120d67c46b9STakashi Sakamoto unsigned int fmt; 121d67c46b9STakashi Sakamoto unsigned int fdf; 122d67c46b9STakashi Sakamoto /* quirk: fixed interval of dbc between previos/current packets. */ 123d67c46b9STakashi Sakamoto unsigned int tx_dbc_interval; 124d67c46b9STakashi Sakamoto /* quirk: indicate the value of dbc field in a first packet. */ 125d67c46b9STakashi Sakamoto unsigned int tx_first_dbc; 126d67c46b9STakashi Sakamoto 127d67c46b9STakashi Sakamoto /* Internal flags. */ 128d67c46b9STakashi Sakamoto enum cip_sfc sfc; 129d67c46b9STakashi Sakamoto unsigned int syt_interval; 130d67c46b9STakashi Sakamoto unsigned int transfer_delay; 131d67c46b9STakashi Sakamoto unsigned int data_block_state; 132d67c46b9STakashi Sakamoto unsigned int last_syt_offset; 133d67c46b9STakashi Sakamoto unsigned int syt_offset_state; 134d67c46b9STakashi Sakamoto 135d67c46b9STakashi Sakamoto /* For a PCM substream processing. */ 136d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm; 137d67c46b9STakashi Sakamoto struct tasklet_struct period_tasklet; 138d67c46b9STakashi Sakamoto unsigned int pcm_buffer_pointer; 139d67c46b9STakashi Sakamoto unsigned int pcm_period_pointer; 140d67c46b9STakashi Sakamoto 141d67c46b9STakashi Sakamoto /* To wait for first packet. */ 142d67c46b9STakashi Sakamoto bool callbacked; 143d67c46b9STakashi Sakamoto wait_queue_head_t callback_wait; 144a04513f8STakashi Sakamoto u32 start_cycle; 145d67c46b9STakashi Sakamoto 146df075feeSTakashi Sakamoto /* For backends to process data blocks. */ 147df075feeSTakashi Sakamoto void *protocol; 148df075feeSTakashi Sakamoto amdtp_stream_process_data_blocks_t process_data_blocks; 149d67c46b9STakashi Sakamoto }; 150d67c46b9STakashi Sakamoto 151d67c46b9STakashi Sakamoto int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit, 152df075feeSTakashi Sakamoto enum amdtp_stream_direction dir, enum cip_flags flags, 153df075feeSTakashi Sakamoto unsigned int fmt, 154df075feeSTakashi Sakamoto amdtp_stream_process_data_blocks_t process_data_blocks, 155df075feeSTakashi Sakamoto unsigned int protocol_size); 156d67c46b9STakashi Sakamoto void amdtp_stream_destroy(struct amdtp_stream *s); 157d67c46b9STakashi Sakamoto 158df075feeSTakashi Sakamoto int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate, 159df075feeSTakashi Sakamoto unsigned int data_block_quadlets); 160d67c46b9STakashi Sakamoto unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s); 161d67c46b9STakashi Sakamoto 162d67c46b9STakashi Sakamoto int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed); 163d67c46b9STakashi Sakamoto void amdtp_stream_update(struct amdtp_stream *s); 164d67c46b9STakashi Sakamoto void amdtp_stream_stop(struct amdtp_stream *s); 165d67c46b9STakashi Sakamoto 166d67c46b9STakashi Sakamoto int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s, 167d67c46b9STakashi Sakamoto struct snd_pcm_runtime *runtime); 168df075feeSTakashi Sakamoto 169d67c46b9STakashi Sakamoto void amdtp_stream_pcm_prepare(struct amdtp_stream *s); 170d67c46b9STakashi Sakamoto unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s); 171d67c46b9STakashi Sakamoto void amdtp_stream_pcm_abort(struct amdtp_stream *s); 172d67c46b9STakashi Sakamoto 173d67c46b9STakashi Sakamoto extern const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT]; 174d67c46b9STakashi Sakamoto extern const unsigned int amdtp_rate_table[CIP_SFC_COUNT]; 175d67c46b9STakashi Sakamoto 176d67c46b9STakashi Sakamoto /** 177d67c46b9STakashi Sakamoto * amdtp_stream_running - check stream is running or not 178d67c46b9STakashi Sakamoto * @s: the AMDTP stream 179d67c46b9STakashi Sakamoto * 180d67c46b9STakashi Sakamoto * If this function returns true, the stream is running. 181d67c46b9STakashi Sakamoto */ 182d67c46b9STakashi Sakamoto static inline bool amdtp_stream_running(struct amdtp_stream *s) 183d67c46b9STakashi Sakamoto { 184d67c46b9STakashi Sakamoto return !IS_ERR(s->context); 185d67c46b9STakashi Sakamoto } 186d67c46b9STakashi Sakamoto 187d67c46b9STakashi Sakamoto /** 188d67c46b9STakashi Sakamoto * amdtp_streaming_error - check for streaming error 189d67c46b9STakashi Sakamoto * @s: the AMDTP stream 190d67c46b9STakashi Sakamoto * 191d67c46b9STakashi Sakamoto * If this function returns true, the stream's packet queue has stopped due to 192d67c46b9STakashi Sakamoto * an asynchronous error. 193d67c46b9STakashi Sakamoto */ 194d67c46b9STakashi Sakamoto static inline bool amdtp_streaming_error(struct amdtp_stream *s) 195d67c46b9STakashi Sakamoto { 196d67c46b9STakashi Sakamoto return s->packet_index < 0; 197d67c46b9STakashi Sakamoto } 198d67c46b9STakashi Sakamoto 199d67c46b9STakashi Sakamoto /** 200d67c46b9STakashi Sakamoto * amdtp_stream_pcm_running - check PCM substream is running or not 201d67c46b9STakashi Sakamoto * @s: the AMDTP stream 202d67c46b9STakashi Sakamoto * 203d67c46b9STakashi Sakamoto * If this function returns true, PCM substream in the AMDTP stream is running. 204d67c46b9STakashi Sakamoto */ 205d67c46b9STakashi Sakamoto static inline bool amdtp_stream_pcm_running(struct amdtp_stream *s) 206d67c46b9STakashi Sakamoto { 207d67c46b9STakashi Sakamoto return !!s->pcm; 208d67c46b9STakashi Sakamoto } 209d67c46b9STakashi Sakamoto 210d67c46b9STakashi Sakamoto /** 211d67c46b9STakashi Sakamoto * amdtp_stream_pcm_trigger - start/stop playback from a PCM device 212d67c46b9STakashi Sakamoto * @s: the AMDTP stream 213d67c46b9STakashi Sakamoto * @pcm: the PCM device to be started, or %NULL to stop the current device 214d67c46b9STakashi Sakamoto * 215d67c46b9STakashi Sakamoto * Call this function on a running isochronous stream to enable the actual 216d67c46b9STakashi Sakamoto * transmission of PCM data. This function should be called from the PCM 217d67c46b9STakashi Sakamoto * device's .trigger callback. 218d67c46b9STakashi Sakamoto */ 219d67c46b9STakashi Sakamoto static inline void amdtp_stream_pcm_trigger(struct amdtp_stream *s, 220d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm) 221d67c46b9STakashi Sakamoto { 222d67c46b9STakashi Sakamoto ACCESS_ONCE(s->pcm) = pcm; 223d67c46b9STakashi Sakamoto } 224d67c46b9STakashi Sakamoto 225d67c46b9STakashi Sakamoto static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc) 226d67c46b9STakashi Sakamoto { 227d67c46b9STakashi Sakamoto return sfc & 1; 228d67c46b9STakashi Sakamoto } 229d67c46b9STakashi Sakamoto 230d67c46b9STakashi Sakamoto /** 231d67c46b9STakashi Sakamoto * amdtp_stream_wait_callback - sleep till callbacked or timeout 232d67c46b9STakashi Sakamoto * @s: the AMDTP stream 233d67c46b9STakashi Sakamoto * @timeout: msec till timeout 234d67c46b9STakashi Sakamoto * 235d67c46b9STakashi Sakamoto * If this function return false, the AMDTP stream should be stopped. 236d67c46b9STakashi Sakamoto */ 237d67c46b9STakashi Sakamoto static inline bool amdtp_stream_wait_callback(struct amdtp_stream *s, 238d67c46b9STakashi Sakamoto unsigned int timeout) 239d67c46b9STakashi Sakamoto { 240d67c46b9STakashi Sakamoto return wait_event_timeout(s->callback_wait, 241d67c46b9STakashi Sakamoto s->callbacked == true, 242d67c46b9STakashi Sakamoto msecs_to_jiffies(timeout)) > 0; 243d67c46b9STakashi Sakamoto } 244d67c46b9STakashi Sakamoto 245d67c46b9STakashi Sakamoto #endif 246