ALSA: fireworks/firewire-lib: Add a quirk for empty packet with TAG0
[deliverable/linux.git] / sound / firewire / amdtp.h
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 <sound/asound.h>
8 #include "packets-buffer.h"
9
10 /**
11 * enum cip_flags - describes details of the streaming protocol
12 * @CIP_NONBLOCKING: In non-blocking mode, each packet contains
13 * sample_rate/8000 samples, with rounding up or down to adjust
14 * for clock skew and left-over fractional samples. This should
15 * be used if supported by the device.
16 * @CIP_BLOCKING: In blocking mode, each packet contains either zero or
17 * SYT_INTERVAL samples, with these two types alternating so that
18 * the overall sample rate comes out right.
19 * @CIP_SYNC_TO_DEVICE: In sync to device mode, time stamp in out packets is
20 * generated by in packets. Defaultly this driver generates timestamp.
21 * @CIP_EMPTY_WITH_TAG0: Only for in-stream. Empty in-packets have TAG0.
22 */
23 enum cip_flags {
24 CIP_NONBLOCKING = 0x00,
25 CIP_BLOCKING = 0x01,
26 CIP_SYNC_TO_DEVICE = 0x02,
27 CIP_EMPTY_WITH_TAG0 = 0x04,
28 };
29
30 /**
31 * enum cip_sfc - a stream's sample rate
32 */
33 enum cip_sfc {
34 CIP_SFC_32000 = 0,
35 CIP_SFC_44100 = 1,
36 CIP_SFC_48000 = 2,
37 CIP_SFC_88200 = 3,
38 CIP_SFC_96000 = 4,
39 CIP_SFC_176400 = 5,
40 CIP_SFC_192000 = 6,
41 CIP_SFC_COUNT
42 };
43
44 #define AMDTP_IN_PCM_FORMAT_BITS SNDRV_PCM_FMTBIT_S32
45
46 #define AMDTP_OUT_PCM_FORMAT_BITS (SNDRV_PCM_FMTBIT_S16 | \
47 SNDRV_PCM_FMTBIT_S32)
48
49
50 /*
51 * This module supports maximum 64 PCM channels for one PCM stream
52 * This is for our convenience.
53 */
54 #define AMDTP_MAX_CHANNELS_FOR_PCM 64
55
56 /*
57 * AMDTP packet can include channels for MIDI conformant data.
58 * Each MIDI conformant data channel includes 8 MPX-MIDI data stream.
59 * Each MPX-MIDI data stream includes one data stream from/to MIDI ports.
60 *
61 * This module supports maximum 1 MIDI conformant data channels.
62 * Then this AMDTP packets can transfer maximum 8 MIDI data streams.
63 */
64 #define AMDTP_MAX_CHANNELS_FOR_MIDI 1
65
66 struct fw_unit;
67 struct fw_iso_context;
68 struct snd_pcm_substream;
69 struct snd_pcm_runtime;
70 struct snd_rawmidi_substream;
71
72 enum amdtp_stream_direction {
73 AMDTP_OUT_STREAM = 0,
74 AMDTP_IN_STREAM
75 };
76
77 struct amdtp_stream {
78 struct fw_unit *unit;
79 enum cip_flags flags;
80 enum amdtp_stream_direction direction;
81 struct fw_iso_context *context;
82 struct mutex mutex;
83
84 enum cip_sfc sfc;
85 unsigned int data_block_quadlets;
86 unsigned int pcm_channels;
87 unsigned int midi_ports;
88 void (*transfer_samples)(struct amdtp_stream *s,
89 struct snd_pcm_substream *pcm,
90 __be32 *buffer, unsigned int frames);
91 u8 pcm_positions[AMDTP_MAX_CHANNELS_FOR_PCM];
92 u8 midi_position;
93
94 unsigned int syt_interval;
95 unsigned int transfer_delay;
96 unsigned int source_node_id_field;
97 struct iso_packets_buffer buffer;
98
99 struct snd_pcm_substream *pcm;
100 struct tasklet_struct period_tasklet;
101
102 int packet_index;
103 unsigned int data_block_counter;
104
105 unsigned int data_block_state;
106
107 unsigned int last_syt_offset;
108 unsigned int syt_offset_state;
109
110 unsigned int pcm_buffer_pointer;
111 unsigned int pcm_period_pointer;
112 bool pointer_flush;
113
114 struct snd_rawmidi_substream *midi[AMDTP_MAX_CHANNELS_FOR_MIDI * 8];
115
116 bool callbacked;
117 wait_queue_head_t callback_wait;
118 struct amdtp_stream *sync_slave;
119 };
120
121 int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
122 enum amdtp_stream_direction dir,
123 enum cip_flags flags);
124 void amdtp_stream_destroy(struct amdtp_stream *s);
125
126 void amdtp_stream_set_parameters(struct amdtp_stream *s,
127 unsigned int rate,
128 unsigned int pcm_channels,
129 unsigned int midi_ports);
130 unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s);
131
132 int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed);
133 void amdtp_stream_update(struct amdtp_stream *s);
134 void amdtp_stream_stop(struct amdtp_stream *s);
135
136 int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
137 struct snd_pcm_runtime *runtime);
138 void amdtp_stream_set_pcm_format(struct amdtp_stream *s,
139 snd_pcm_format_t format);
140 void amdtp_stream_pcm_prepare(struct amdtp_stream *s);
141 unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s);
142 void amdtp_stream_pcm_abort(struct amdtp_stream *s);
143
144 extern const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT];
145 extern const unsigned int amdtp_rate_table[CIP_SFC_COUNT];
146
147 /**
148 * amdtp_stream_running - check stream is running or not
149 * @s: the AMDTP stream
150 *
151 * If this function returns true, the stream is running.
152 */
153 static inline bool amdtp_stream_running(struct amdtp_stream *s)
154 {
155 return !IS_ERR(s->context);
156 }
157
158 /**
159 * amdtp_streaming_error - check for streaming error
160 * @s: the AMDTP stream
161 *
162 * If this function returns true, the stream's packet queue has stopped due to
163 * an asynchronous error.
164 */
165 static inline bool amdtp_streaming_error(struct amdtp_stream *s)
166 {
167 return s->packet_index < 0;
168 }
169
170 /**
171 * amdtp_stream_pcm_running - check PCM substream is running or not
172 * @s: the AMDTP stream
173 *
174 * If this function returns true, PCM substream in the AMDTP stream is running.
175 */
176 static inline bool amdtp_stream_pcm_running(struct amdtp_stream *s)
177 {
178 return !!s->pcm;
179 }
180
181 /**
182 * amdtp_stream_pcm_trigger - start/stop playback from a PCM device
183 * @s: the AMDTP stream
184 * @pcm: the PCM device to be started, or %NULL to stop the current device
185 *
186 * Call this function on a running isochronous stream to enable the actual
187 * transmission of PCM data. This function should be called from the PCM
188 * device's .trigger callback.
189 */
190 static inline void amdtp_stream_pcm_trigger(struct amdtp_stream *s,
191 struct snd_pcm_substream *pcm)
192 {
193 ACCESS_ONCE(s->pcm) = pcm;
194 }
195
196 /**
197 * amdtp_stream_midi_trigger - start/stop playback/capture with a MIDI device
198 * @s: the AMDTP stream
199 * @port: index of MIDI port
200 * @midi: the MIDI device to be started, or %NULL to stop the current device
201 *
202 * Call this function on a running isochronous stream to enable the actual
203 * transmission of MIDI data. This function should be called from the MIDI
204 * device's .trigger callback.
205 */
206 static inline void amdtp_stream_midi_trigger(struct amdtp_stream *s,
207 unsigned int port,
208 struct snd_rawmidi_substream *midi)
209 {
210 if (port < s->midi_ports)
211 ACCESS_ONCE(s->midi[port]) = midi;
212 }
213
214 static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)
215 {
216 return sfc & 1;
217 }
218
219 static inline void amdtp_stream_set_sync(enum cip_flags sync_mode,
220 struct amdtp_stream *master,
221 struct amdtp_stream *slave)
222 {
223 if (sync_mode == CIP_SYNC_TO_DEVICE) {
224 master->flags |= CIP_SYNC_TO_DEVICE;
225 slave->flags |= CIP_SYNC_TO_DEVICE;
226 master->sync_slave = slave;
227 } else {
228 master->flags &= ~CIP_SYNC_TO_DEVICE;
229 slave->flags &= ~CIP_SYNC_TO_DEVICE;
230 master->sync_slave = NULL;
231 }
232
233 slave->sync_slave = NULL;
234 }
235
236 /**
237 * amdtp_stream_wait_callback - sleep till callbacked or timeout
238 * @s: the AMDTP stream
239 * @timeout: msec till timeout
240 *
241 * If this function return false, the AMDTP stream should be stopped.
242 */
243 static inline bool amdtp_stream_wait_callback(struct amdtp_stream *s,
244 unsigned int timeout)
245 {
246 return wait_event_timeout(s->callback_wait,
247 s->callbacked == true,
248 msecs_to_jiffies(timeout)) > 0;
249 }
250
251 #endif
This page took 0.040139 seconds and 6 git commands to generate.