blob: ed6eafd109924013dbeaa231999899b7030e0008 [file] [log] [blame]
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001#ifndef SOUND_FIREWIRE_AMDTP_H_INCLUDED
2#define SOUND_FIREWIRE_AMDTP_H_INCLUDED
3
Clemens Ladisch20b65dd2011-09-04 22:15:44 +02004#include <linux/err.h>
Clemens Ladisch76fb8782012-05-13 22:03:09 +02005#include <linux/interrupt.h>
Clemens Ladisch31ef9132011-03-15 07:53:21 +01006#include <linux/mutex.h>
Takashi Sakamoto59558152015-09-19 11:21:55 +09007#include <linux/sched.h>
Takashi Sakamoto777fb572013-11-19 13:29:24 +09008#include <sound/asound.h>
Clemens Ladisch31ef9132011-03-15 07:53:21 +01009#include "packets-buffer.h"
10
11/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090012 * enum cip_flags - describes details of the streaming protocol
Clemens Ladisch31ef9132011-03-15 07:53:21 +010013 * @CIP_NONBLOCKING: In non-blocking mode, each packet contains
14 * sample_rate/8000 samples, with rounding up or down to adjust
15 * for clock skew and left-over fractional samples. This should
16 * be used if supported by the device.
Clemens Ladische84d15f2011-09-04 22:12:48 +020017 * @CIP_BLOCKING: In blocking mode, each packet contains either zero or
18 * SYT_INTERVAL samples, with these two types alternating so that
19 * the overall sample rate comes out right.
Takashi Sakamoto7ab56642014-04-25 22:45:03 +090020 * @CIP_EMPTY_WITH_TAG0: Only for in-stream. Empty in-packets have TAG0.
Takashi Sakamoto9dae0172017-03-22 21:30:17 +090021 * @CIP_DBC_IS_END_EVENT: The value of dbc in an packet corresponds to the end
22 * of event in the packet. Out of IEC 61883.
Takashi Sakamoto69702232014-04-25 22:45:05 +090023 * @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.
Takashi Sakamoto5f217f92014-11-18 23:59:40 +090025 * @CIP_SKIP_DBC_ZERO_CHECK: Only for in-stream. Packets with zero in dbc is
Takashi Sakamotob84b1a22014-04-25 22:45:07 +090026 * skipped for detecting discontinuity.
Takashi Sakamoto9d591242014-04-25 22:45:27 +090027 * @CIP_EMPTY_HAS_WRONG_DBC: Only for in-stream. The value of dbc in empty
28 * packet is wrong but the others are correct.
Takashi Sakamotoa2064712015-05-22 23:00:50 +090029 * @CIP_JUMBO_PAYLOAD: Only for in-stream. The number of data blocks in an
30 * packet is larger than IEC 61883-6 defines. Current implementation
31 * allows 5 times as large as IEC 61883-6 defines.
Takashi Sakamoto2128f782017-03-22 21:30:27 +090032 * @CIP_HEADER_WITHOUT_EOH: Only for in-stream. CIP Header doesn't include
33 * valid EOH.
Takashi Sakamoto3b196c392017-03-31 22:06:07 +090034 * @CIP_NO_HEADERS: a lack of headers in packets
Clemens Ladisch31ef9132011-03-15 07:53:21 +010035 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090036enum cip_flags {
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +090037 CIP_NONBLOCKING = 0x00,
38 CIP_BLOCKING = 0x01,
Takashi Sakamotodec63cc2016-05-09 23:15:53 +090039 CIP_EMPTY_WITH_TAG0 = 0x02,
40 CIP_DBC_IS_END_EVENT = 0x04,
41 CIP_WRONG_DBS = 0x08,
42 CIP_SKIP_DBC_ZERO_CHECK = 0x10,
Takashi Sakamoto62f00e42016-05-09 23:15:56 +090043 CIP_EMPTY_HAS_WRONG_DBC = 0x20,
44 CIP_JUMBO_PAYLOAD = 0x40,
Takashi Sakamoto2128f782017-03-22 21:30:27 +090045 CIP_HEADER_WITHOUT_EOH = 0x80,
Takashi Sakamoto3b196c392017-03-31 22:06:07 +090046 CIP_NO_HEADER = 0x100,
Clemens Ladisch31ef9132011-03-15 07:53:21 +010047};
48
49/**
Takashi Sakamoto5f217f92014-11-18 23:59:40 +090050 * enum cip_sfc - supported Sampling Frequency Codes (SFCs)
51 * @CIP_SFC_32000: 32,000 data blocks
52 * @CIP_SFC_44100: 44,100 data blocks
53 * @CIP_SFC_48000: 48,000 data blocks
54 * @CIP_SFC_88200: 88,200 data blocks
55 * @CIP_SFC_96000: 96,000 data blocks
56 * @CIP_SFC_176400: 176,400 data blocks
57 * @CIP_SFC_192000: 192,000 data blocks
58 * @CIP_SFC_COUNT: the number of supported SFCs
59 *
60 * These values are used to show nominal Sampling Frequency Code in
61 * Format Dependent Field (FDF) of AMDTP packet header. In IEC 61883-6:2002,
62 * this code means the number of events per second. Actually the code
63 * represents the number of data blocks transferred per second in an AMDTP
64 * stream.
65 *
66 * In IEC 61883-6:2005, some extensions were added to support more types of
67 * data such as 'One Bit LInear Audio', therefore the meaning of SFC became
68 * different depending on the types.
69 *
70 * Currently our implementation is compatible with IEC 61883-6:2002.
Clemens Ladisch31ef9132011-03-15 07:53:21 +010071 */
72enum cip_sfc {
73 CIP_SFC_32000 = 0,
74 CIP_SFC_44100 = 1,
75 CIP_SFC_48000 = 2,
76 CIP_SFC_88200 = 3,
77 CIP_SFC_96000 = 4,
78 CIP_SFC_176400 = 5,
79 CIP_SFC_192000 = 6,
Clemens Ladischa7304e32011-09-04 22:16:10 +020080 CIP_SFC_COUNT
Clemens Ladisch31ef9132011-03-15 07:53:21 +010081};
82
Clemens Ladisch31ef9132011-03-15 07:53:21 +010083struct fw_unit;
84struct fw_iso_context;
85struct snd_pcm_substream;
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +090086struct snd_pcm_runtime;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010087
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090088enum amdtp_stream_direction {
89 AMDTP_OUT_STREAM = 0,
90 AMDTP_IN_STREAM
91};
92
Takashi Sakamotodf075fe2015-09-19 11:22:02 +090093struct amdtp_stream;
94typedef unsigned int (*amdtp_stream_process_data_blocks_t)(
95 struct amdtp_stream *s,
96 __be32 *buffer,
97 unsigned int data_blocks,
98 unsigned int *syt);
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090099struct amdtp_stream {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100100 struct fw_unit *unit;
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900101 enum cip_flags flags;
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +0900102 enum amdtp_stream_direction direction;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100103 struct mutex mutex;
104
Takashi Sakamoto10b2b6d2015-09-19 11:21:48 +0900105 /* For packet processing. */
106 struct fw_iso_context *context;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100107 struct iso_packets_buffer buffer;
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100108 int packet_index;
Takashi Sakamoto3b196c392017-03-31 22:06:07 +0900109 int tag;
110 int (*handle_packet)(struct amdtp_stream *s,
111 unsigned int payload_quadlets, unsigned int cycle,
112 unsigned int index);
Takashi Sakamotof91c9d72017-04-11 20:33:18 +0900113 unsigned int max_payload_length;
Takashi Sakamoto10b2b6d2015-09-19 11:21:48 +0900114
115 /* For CIP headers. */
116 unsigned int source_node_id_field;
117 unsigned int data_block_quadlets;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100118 unsigned int data_block_counter;
Takashi Sakamoto98638742017-03-22 21:30:16 +0900119 unsigned int sph;
Takashi Sakamoto414ba022015-09-19 11:21:53 +0900120 unsigned int fmt;
121 unsigned int fdf;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900122 /* quirk: fixed interval of dbc between previos/current packets. */
123 unsigned int tx_dbc_interval;
Takashi Sakamoto18f5ed32015-08-05 09:21:05 +0900124 /* quirk: indicate the value of dbc field in a first packet. */
125 unsigned int tx_first_dbc;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900126
Takashi Sakamoto10b2b6d2015-09-19 11:21:48 +0900127 /* Internal flags. */
128 enum cip_sfc sfc;
129 unsigned int syt_interval;
130 unsigned int transfer_delay;
131 unsigned int data_block_state;
132 unsigned int last_syt_offset;
133 unsigned int syt_offset_state;
134
135 /* For a PCM substream processing. */
136 struct snd_pcm_substream *pcm;
137 struct tasklet_struct period_tasklet;
Takashi Sakamoto4a9bfaf2017-06-11 16:08:21 +0900138 snd_pcm_uframes_t pcm_buffer_pointer;
Takashi Sakamoto10b2b6d2015-09-19 11:21:48 +0900139 unsigned int pcm_period_pointer;
Takashi Sakamoto10b2b6d2015-09-19 11:21:48 +0900140
141 /* To wait for first packet. */
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900142 bool callbacked;
143 wait_queue_head_t callback_wait;
Takashi Sakamotoa04513f2017-03-22 21:30:15 +0900144 u32 start_cycle;
Takashi Sakamoto10b2b6d2015-09-19 11:21:48 +0900145
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900146 /* For backends to process data blocks. */
147 void *protocol;
148 amdtp_stream_process_data_blocks_t process_data_blocks;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100149};
150
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900151int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900152 enum amdtp_stream_direction dir, enum cip_flags flags,
153 unsigned int fmt,
154 amdtp_stream_process_data_blocks_t process_data_blocks,
155 unsigned int protocol_size);
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900156void amdtp_stream_destroy(struct amdtp_stream *s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100157
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900158int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
159 unsigned int data_block_quadlets);
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900160unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100161
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900162int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed);
163void amdtp_stream_update(struct amdtp_stream *s);
164void amdtp_stream_stop(struct amdtp_stream *s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100165
Takashi Sakamoto7b2d99f2014-04-25 22:44:52 +0900166int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
167 struct snd_pcm_runtime *runtime);
Takashi Sakamotodf075fe2015-09-19 11:22:02 +0900168
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900169void amdtp_stream_pcm_prepare(struct amdtp_stream *s);
170unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s);
Takashi Sakamoto875becf2017-06-07 09:38:05 +0900171int amdtp_stream_pcm_ack(struct amdtp_stream *s);
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900172void amdtp_stream_pcm_abort(struct amdtp_stream *s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100173
Clemens Ladischc5280e92011-10-16 21:39:00 +0200174extern const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT];
Takashi Sakamoto1017abe2014-04-25 22:44:59 +0900175extern const unsigned int amdtp_rate_table[CIP_SFC_COUNT];
Clemens Ladischa7304e32011-09-04 22:16:10 +0200176
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900177/**
178 * amdtp_stream_running - check stream is running or not
179 * @s: the AMDTP stream
180 *
181 * If this function returns true, the stream is running.
182 */
183static inline bool amdtp_stream_running(struct amdtp_stream *s)
Clemens Ladisch20b65dd2011-09-04 22:15:44 +0200184{
185 return !IS_ERR(s->context);
186}
187
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100188/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900189 * amdtp_streaming_error - check for streaming error
190 * @s: the AMDTP stream
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100191 *
192 * If this function returns true, the stream's packet queue has stopped due to
193 * an asynchronous error.
194 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900195static inline bool amdtp_streaming_error(struct amdtp_stream *s)
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100196{
197 return s->packet_index < 0;
198}
199
200/**
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900201 * amdtp_stream_pcm_running - check PCM substream is running or not
202 * @s: the AMDTP stream
203 *
204 * If this function returns true, PCM substream in the AMDTP stream is running.
205 */
206static inline bool amdtp_stream_pcm_running(struct amdtp_stream *s)
207{
208 return !!s->pcm;
209}
210
211/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900212 * amdtp_stream_pcm_trigger - start/stop playback from a PCM device
213 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100214 * @pcm: the PCM device to be started, or %NULL to stop the current device
215 *
216 * Call this function on a running isochronous stream to enable the actual
217 * transmission of PCM data. This function should be called from the PCM
218 * device's .trigger callback.
219 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900220static inline void amdtp_stream_pcm_trigger(struct amdtp_stream *s,
221 struct snd_pcm_substream *pcm)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100222{
223 ACCESS_ONCE(s->pcm) = pcm;
224}
225
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100226static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)
227{
228 return sfc & 1;
229}
230
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900231/**
232 * amdtp_stream_wait_callback - sleep till callbacked or timeout
233 * @s: the AMDTP stream
234 * @timeout: msec till timeout
235 *
236 * If this function return false, the AMDTP stream should be stopped.
237 */
238static inline bool amdtp_stream_wait_callback(struct amdtp_stream *s,
239 unsigned int timeout)
240{
241 return wait_event_timeout(s->callback_wait,
242 s->callbacked == true,
243 msecs_to_jiffies(timeout)) > 0;
244}
245
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100246#endif