]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - sound/firewire/amdtp-stream.h
Merge tag '4.14-smb3-multidialect-support-and-fixes-for-stable' of git://git.samba...
[mirror_ubuntu-bionic-kernel.git] / sound / firewire / amdtp-stream.h
CommitLineData
31ef9134
CL
1#ifndef SOUND_FIREWIRE_AMDTP_H_INCLUDED
2#define SOUND_FIREWIRE_AMDTP_H_INCLUDED
3
20b65dd0 4#include <linux/err.h>
76fb8789 5#include <linux/interrupt.h>
31ef9134 6#include <linux/mutex.h>
5955815e 7#include <linux/sched.h>
777fb574 8#include <sound/asound.h>
31ef9134
CL
9#include "packets-buffer.h"
10
11/**
be4a2894 12 * enum cip_flags - describes details of the streaming protocol
31ef9134
CL
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.
e84d15f6
CL
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.
7ab56645 20 * @CIP_EMPTY_WITH_TAG0: Only for in-stream. Empty in-packets have TAG0.
9dae017b
TS
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.
69702239
TS
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.
5f217f90 25 * @CIP_SKIP_DBC_ZERO_CHECK: Only for in-stream. Packets with zero in dbc is
b84b1a27 26 * skipped for detecting discontinuity.
9d59124c
TS
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.
a2064710
TS
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.
2128f78f
TS
32 * @CIP_HEADER_WITHOUT_EOH: Only for in-stream. CIP Header doesn't include
33 * valid EOH.
3b196c39 34 * @CIP_NO_HEADERS: a lack of headers in packets
31ef9134 35 */
be4a2894 36enum cip_flags {
7b3b0d85
TS
37 CIP_NONBLOCKING = 0x00,
38 CIP_BLOCKING = 0x01,
dec63cc8
TS
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,
62f00e40
TS
43 CIP_EMPTY_HAS_WRONG_DBC = 0x20,
44 CIP_JUMBO_PAYLOAD = 0x40,
2128f78f 45 CIP_HEADER_WITHOUT_EOH = 0x80,
3b196c39 46 CIP_NO_HEADER = 0x100,
31ef9134
CL
47};
48
49/**
5f217f90
TS
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.
31ef9134
CL
71 */
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,
a7304e3b 80 CIP_SFC_COUNT
31ef9134
CL
81};
82
31ef9134
CL
83struct fw_unit;
84struct fw_iso_context;
85struct snd_pcm_substream;
7b2d99fa 86struct snd_pcm_runtime;
31ef9134 87
3ff7e8f0
TS
88enum amdtp_stream_direction {
89 AMDTP_OUT_STREAM = 0,
90 AMDTP_IN_STREAM
91};
92
df075fee
TS
93struct 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);
be4a2894 99struct amdtp_stream {
31ef9134 100 struct fw_unit *unit;
be4a2894 101 enum cip_flags flags;
3ff7e8f0 102 enum amdtp_stream_direction direction;
31ef9134
CL
103 struct mutex mutex;
104
10b2b6dc
TS
105 /* For packet processing. */
106 struct fw_iso_context *context;
31ef9134 107 struct iso_packets_buffer buffer;
ec00f5e4 108 int packet_index;
3b196c39
TS
109 int tag;
110 int (*handle_packet)(struct amdtp_stream *s,
111 unsigned int payload_quadlets, unsigned int cycle,
112 unsigned int index);
f91c9d76 113 unsigned int max_payload_length;
10b2b6dc
TS
114
115 /* For CIP headers. */
116 unsigned int source_node_id_field;
117 unsigned int data_block_quadlets;
31ef9134 118 unsigned int data_block_counter;
9863874f 119 unsigned int sph;
414ba022
TS
120 unsigned int fmt;
121 unsigned int fdf;
10b2b6dc
TS
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;
31ef9134 126
10b2b6dc
TS
127 /* Internal flags. */
128 enum cip_sfc sfc;
129 unsigned int syt_interval;
130 unsigned int transfer_delay;
31ef9134 131 unsigned int data_block_state;
31ef9134
CL
132 unsigned int last_syt_offset;
133 unsigned int syt_offset_state;
134
10b2b6dc
TS
135 /* For a PCM substream processing. */
136 struct snd_pcm_substream *pcm;
137 struct tasklet_struct period_tasklet;
4a9bfafc 138 snd_pcm_uframes_t pcm_buffer_pointer;
31ef9134 139 unsigned int pcm_period_pointer;
83d8d72d 140
10b2b6dc
TS
141 /* To wait for first packet. */
142 bool callbacked;
143 wait_queue_head_t callback_wait;
a04513f8 144 u32 start_cycle;
10b2b6dc 145
df075fee
TS
146 /* For backends to process data blocks. */
147 void *protocol;
148 amdtp_stream_process_data_blocks_t process_data_blocks;
31ef9134
CL
149};
150
be4a2894 151int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
df075fee
TS
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);
be4a2894 156void amdtp_stream_destroy(struct amdtp_stream *s);
31ef9134 157
df075fee
TS
158int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
159 unsigned int data_block_quadlets);
be4a2894 160unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s);
31ef9134 161
be4a2894
TS
162int 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);
31ef9134 165
7b2d99fa
TS
166int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
167 struct snd_pcm_runtime *runtime);
df075fee 168
be4a2894
TS
169void amdtp_stream_pcm_prepare(struct amdtp_stream *s);
170unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s);
875becf8 171int amdtp_stream_pcm_ack(struct amdtp_stream *s);
be4a2894 172void amdtp_stream_pcm_abort(struct amdtp_stream *s);
31ef9134 173
c5280e99 174extern const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT];
1017abed 175extern const unsigned int amdtp_rate_table[CIP_SFC_COUNT];
a7304e3b 176
be4a2894
TS
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 */
183static inline bool amdtp_stream_running(struct amdtp_stream *s)
20b65dd0
CL
184{
185 return !IS_ERR(s->context);
186}
187
ec00f5e4 188/**
be4a2894
TS
189 * amdtp_streaming_error - check for streaming error
190 * @s: the AMDTP stream
ec00f5e4
CL
191 *
192 * If this function returns true, the stream's packet queue has stopped due to
193 * an asynchronous error.
194 */
be4a2894 195static inline bool amdtp_streaming_error(struct amdtp_stream *s)
ec00f5e4
CL
196{
197 return s->packet_index < 0;
198}
199
83d8d72d
TS
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 */
206static inline bool amdtp_stream_pcm_running(struct amdtp_stream *s)
207{
208 return !!s->pcm;
209}
210
31ef9134 211/**
be4a2894
TS
212 * amdtp_stream_pcm_trigger - start/stop playback from a PCM device
213 * @s: the AMDTP stream
31ef9134
CL
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 */
be4a2894
TS
220static inline void amdtp_stream_pcm_trigger(struct amdtp_stream *s,
221 struct snd_pcm_substream *pcm)
31ef9134
CL
222{
223 ACCESS_ONCE(s->pcm) = pcm;
224}
225
31ef9134
CL
226static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)
227{
228 return sfc & 1;
229}
230
7b3b0d85
TS
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 */
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
31ef9134 246#endif