ALSA: fireworks/firewire-lib: Add a quirk for empty packet with TAG0
[deliverable/linux.git] / sound / firewire / amdtp.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>
777fb574 7#include <sound/asound.h>
31ef9134
CL
8#include "packets-buffer.h"
9
10/**
be4a2894 11 * enum cip_flags - describes details of the streaming protocol
31ef9134
CL
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.
e84d15f6
CL
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.
7b3b0d85
TS
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.
7ab56645 21 * @CIP_EMPTY_WITH_TAG0: Only for in-stream. Empty in-packets have TAG0.
31ef9134 22 */
be4a2894 23enum cip_flags {
7b3b0d85
TS
24 CIP_NONBLOCKING = 0x00,
25 CIP_BLOCKING = 0x01,
10550bea 26 CIP_SYNC_TO_DEVICE = 0x02,
7ab56645 27 CIP_EMPTY_WITH_TAG0 = 0x04,
31ef9134
CL
28};
29
30/**
31 * enum cip_sfc - a stream's sample rate
32 */
33enum 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,
a7304e3b 41 CIP_SFC_COUNT
31ef9134
CL
42};
43
2b3fc456
TS
44#define AMDTP_IN_PCM_FORMAT_BITS SNDRV_PCM_FMTBIT_S32
45
31ef9134
CL
46#define AMDTP_OUT_PCM_FORMAT_BITS (SNDRV_PCM_FMTBIT_S16 | \
47 SNDRV_PCM_FMTBIT_S32)
48
83d8d72d 49
77d2a8a4
TS
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
83d8d72d
TS
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
31ef9134
CL
66struct fw_unit;
67struct fw_iso_context;
68struct snd_pcm_substream;
7b2d99fa 69struct snd_pcm_runtime;
83d8d72d 70struct snd_rawmidi_substream;
31ef9134 71
3ff7e8f0
TS
72enum amdtp_stream_direction {
73 AMDTP_OUT_STREAM = 0,
74 AMDTP_IN_STREAM
75};
76
be4a2894 77struct amdtp_stream {
31ef9134 78 struct fw_unit *unit;
be4a2894 79 enum cip_flags flags;
3ff7e8f0 80 enum amdtp_stream_direction direction;
31ef9134
CL
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;
be4a2894 88 void (*transfer_samples)(struct amdtp_stream *s,
31ef9134
CL
89 struct snd_pcm_substream *pcm,
90 __be32 *buffer, unsigned int frames);
77d2a8a4
TS
91 u8 pcm_positions[AMDTP_MAX_CHANNELS_FOR_PCM];
92 u8 midi_position;
31ef9134
CL
93
94 unsigned int syt_interval;
e84d15f6 95 unsigned int transfer_delay;
31ef9134
CL
96 unsigned int source_node_id_field;
97 struct iso_packets_buffer buffer;
98
99 struct snd_pcm_substream *pcm;
76fb8789 100 struct tasklet_struct period_tasklet;
31ef9134 101
ec00f5e4 102 int packet_index;
31ef9134
CL
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;
92b862c7 112 bool pointer_flush;
83d8d72d
TS
113
114 struct snd_rawmidi_substream *midi[AMDTP_MAX_CHANNELS_FOR_MIDI * 8];
7b3b0d85
TS
115
116 bool callbacked;
117 wait_queue_head_t callback_wait;
118 struct amdtp_stream *sync_slave;
31ef9134
CL
119};
120
be4a2894 121int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
3ff7e8f0 122 enum amdtp_stream_direction dir,
be4a2894
TS
123 enum cip_flags flags);
124void amdtp_stream_destroy(struct amdtp_stream *s);
31ef9134 125
be4a2894
TS
126void amdtp_stream_set_parameters(struct amdtp_stream *s,
127 unsigned int rate,
128 unsigned int pcm_channels,
129 unsigned int midi_ports);
130unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s);
31ef9134 131
be4a2894
TS
132int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed);
133void amdtp_stream_update(struct amdtp_stream *s);
134void amdtp_stream_stop(struct amdtp_stream *s);
31ef9134 135
7b2d99fa
TS
136int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
137 struct snd_pcm_runtime *runtime);
be4a2894
TS
138void amdtp_stream_set_pcm_format(struct amdtp_stream *s,
139 snd_pcm_format_t format);
140void amdtp_stream_pcm_prepare(struct amdtp_stream *s);
141unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s);
142void amdtp_stream_pcm_abort(struct amdtp_stream *s);
31ef9134 143
c5280e99 144extern const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT];
1017abed 145extern const unsigned int amdtp_rate_table[CIP_SFC_COUNT];
a7304e3b 146
be4a2894
TS
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 */
153static inline bool amdtp_stream_running(struct amdtp_stream *s)
20b65dd0
CL
154{
155 return !IS_ERR(s->context);
156}
157
ec00f5e4 158/**
be4a2894
TS
159 * amdtp_streaming_error - check for streaming error
160 * @s: the AMDTP stream
ec00f5e4
CL
161 *
162 * If this function returns true, the stream's packet queue has stopped due to
163 * an asynchronous error.
164 */
be4a2894 165static inline bool amdtp_streaming_error(struct amdtp_stream *s)
ec00f5e4
CL
166{
167 return s->packet_index < 0;
168}
169
83d8d72d
TS
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 */
176static inline bool amdtp_stream_pcm_running(struct amdtp_stream *s)
177{
178 return !!s->pcm;
179}
180
31ef9134 181/**
be4a2894
TS
182 * amdtp_stream_pcm_trigger - start/stop playback from a PCM device
183 * @s: the AMDTP stream
31ef9134
CL
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 */
be4a2894
TS
190static inline void amdtp_stream_pcm_trigger(struct amdtp_stream *s,
191 struct snd_pcm_substream *pcm)
31ef9134
CL
192{
193 ACCESS_ONCE(s->pcm) = pcm;
194}
195
83d8d72d
TS
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 */
206static 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
31ef9134
CL
214static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)
215{
216 return sfc & 1;
217}
218
7b3b0d85
TS
219static 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 */
243static 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
31ef9134 251#endif
This page took 0.130916 seconds and 5 git commands to generate.