xref: /linux/sound/firewire/amdtp-stream.h (revision 507e190946297c34a27d9366b0661d5e506fdd03)
1 #ifndef SOUND_FIREWIRE_AMDTP_H_INCLUDED
2 #define SOUND_FIREWIRE_AMDTP_H_INCLUDED
3 
4 #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"
10 
11 /**
12  * enum cip_flags - describes details of the streaming protocol
13  * @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.
17  * @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.
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 end
22  * 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 is
26  *	skipped for detecting discontinuity.
27  * @CIP_EMPTY_HAS_WRONG_DBC: Only for in-stream. The value of dbc in empty
28  *	packet is wrong but the others are correct.
29  * @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.
32  * @CIP_HEADER_WITHOUT_EOH: Only for in-stream. CIP Header doesn't include
33  *	valid EOH.
34  * @CIP_NO_HEADERS: a lack of headers in packets
35  */
36 enum cip_flags {
37 	CIP_NONBLOCKING		= 0x00,
38 	CIP_BLOCKING		= 0x01,
39 	CIP_EMPTY_WITH_TAG0	= 0x02,
40 	CIP_DBC_IS_END_EVENT	= 0x04,
41 	CIP_WRONG_DBS		= 0x08,
42 	CIP_SKIP_DBC_ZERO_CHECK	= 0x10,
43 	CIP_EMPTY_HAS_WRONG_DBC	= 0x20,
44 	CIP_JUMBO_PAYLOAD	= 0x40,
45 	CIP_HEADER_WITHOUT_EOH	= 0x80,
46 	CIP_NO_HEADER		= 0x100,
47 };
48 
49 /**
50  * 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.
71  */
72 enum 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,
80 	CIP_SFC_COUNT
81 };
82 
83 struct fw_unit;
84 struct fw_iso_context;
85 struct snd_pcm_substream;
86 struct snd_pcm_runtime;
87 
88 enum amdtp_stream_direction {
89 	AMDTP_OUT_STREAM = 0,
90 	AMDTP_IN_STREAM
91 };
92 
93 struct amdtp_stream;
94 typedef 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);
99 struct amdtp_stream {
100 	struct fw_unit *unit;
101 	enum cip_flags flags;
102 	enum amdtp_stream_direction direction;
103 	struct mutex mutex;
104 
105 	/* For packet processing. */
106 	struct fw_iso_context *context;
107 	struct iso_packets_buffer buffer;
108 	int packet_index;
109 	int tag;
110 	int (*handle_packet)(struct amdtp_stream *s,
111 			unsigned int payload_quadlets, unsigned int cycle,
112 			unsigned int index);
113 	unsigned int max_payload_length;
114 
115 	/* For CIP headers. */
116 	unsigned int source_node_id_field;
117 	unsigned int data_block_quadlets;
118 	unsigned int data_block_counter;
119 	unsigned int sph;
120 	unsigned int fmt;
121 	unsigned int fdf;
122 	/* quirk: fixed interval of dbc between previos/current packets. */
123 	unsigned int tx_dbc_interval;
124 	/* quirk: indicate the value of dbc field in a first packet. */
125 	unsigned int tx_first_dbc;
126 
127 	/* 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;
138 	snd_pcm_uframes_t pcm_buffer_pointer;
139 	unsigned int pcm_period_pointer;
140 
141 	/* To wait for first packet. */
142 	bool callbacked;
143 	wait_queue_head_t callback_wait;
144 	u32 start_cycle;
145 
146 	/* For backends to process data blocks. */
147 	void *protocol;
148 	amdtp_stream_process_data_blocks_t process_data_blocks;
149 };
150 
151 int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
152 		      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);
156 void amdtp_stream_destroy(struct amdtp_stream *s);
157 
158 int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
159 				unsigned int data_block_quadlets);
160 unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s);
161 
162 int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed);
163 void amdtp_stream_update(struct amdtp_stream *s);
164 void amdtp_stream_stop(struct amdtp_stream *s);
165 
166 int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
167 					struct snd_pcm_runtime *runtime);
168 
169 void amdtp_stream_pcm_prepare(struct amdtp_stream *s);
170 unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s);
171 int amdtp_stream_pcm_ack(struct amdtp_stream *s);
172 void amdtp_stream_pcm_abort(struct amdtp_stream *s);
173 
174 extern const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT];
175 extern const unsigned int amdtp_rate_table[CIP_SFC_COUNT];
176 
177 /**
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  */
183 static inline bool amdtp_stream_running(struct amdtp_stream *s)
184 {
185 	return !IS_ERR(s->context);
186 }
187 
188 /**
189  * amdtp_streaming_error - check for streaming error
190  * @s: the AMDTP stream
191  *
192  * If this function returns true, the stream's packet queue has stopped due to
193  * an asynchronous error.
194  */
195 static inline bool amdtp_streaming_error(struct amdtp_stream *s)
196 {
197 	return s->packet_index < 0;
198 }
199 
200 /**
201  * 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  */
206 static inline bool amdtp_stream_pcm_running(struct amdtp_stream *s)
207 {
208 	return !!s->pcm;
209 }
210 
211 /**
212  * amdtp_stream_pcm_trigger - start/stop playback from a PCM device
213  * @s: the AMDTP stream
214  * @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  */
220 static inline void amdtp_stream_pcm_trigger(struct amdtp_stream *s,
221 					    struct snd_pcm_substream *pcm)
222 {
223 	ACCESS_ONCE(s->pcm) = pcm;
224 }
225 
226 static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)
227 {
228 	return sfc & 1;
229 }
230 
231 /**
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  */
238 static 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 
246 #endif
247