1 #ifndef SOUND_FIREWIRE_AMDTP_H_INCLUDED
2 #define SOUND_FIREWIRE_AMDTP_H_INCLUDED
5 #include <linux/interrupt.h>
6 #include <linux/mutex.h>
7 #include <sound/asound.h>
8 #include "packets-buffer.h"
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.
24 CIP_NONBLOCKING
= 0x00,
26 CIP_SYNC_TO_DEVICE
= 0x02,
27 CIP_EMPTY_WITH_TAG0
= 0x04,
31 * enum cip_sfc - a stream's sample rate
44 #define AMDTP_IN_PCM_FORMAT_BITS SNDRV_PCM_FMTBIT_S32
46 #define AMDTP_OUT_PCM_FORMAT_BITS (SNDRV_PCM_FMTBIT_S16 | \
51 * This module supports maximum 64 PCM channels for one PCM stream
52 * This is for our convenience.
54 #define AMDTP_MAX_CHANNELS_FOR_PCM 64
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.
61 * This module supports maximum 1 MIDI conformant data channels.
62 * Then this AMDTP packets can transfer maximum 8 MIDI data streams.
64 #define AMDTP_MAX_CHANNELS_FOR_MIDI 1
67 struct fw_iso_context
;
68 struct snd_pcm_substream
;
69 struct snd_pcm_runtime
;
70 struct snd_rawmidi_substream
;
72 enum amdtp_stream_direction
{
80 enum amdtp_stream_direction direction
;
81 struct fw_iso_context
*context
;
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
];
94 unsigned int syt_interval
;
95 unsigned int transfer_delay
;
96 unsigned int source_node_id_field
;
97 struct iso_packets_buffer buffer
;
99 struct snd_pcm_substream
*pcm
;
100 struct tasklet_struct period_tasklet
;
103 unsigned int data_block_counter
;
105 unsigned int data_block_state
;
107 unsigned int last_syt_offset
;
108 unsigned int syt_offset_state
;
110 unsigned int pcm_buffer_pointer
;
111 unsigned int pcm_period_pointer
;
114 struct snd_rawmidi_substream
*midi
[AMDTP_MAX_CHANNELS_FOR_MIDI
* 8];
117 wait_queue_head_t callback_wait
;
118 struct amdtp_stream
*sync_slave
;
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
);
126 void amdtp_stream_set_parameters(struct amdtp_stream
*s
,
128 unsigned int pcm_channels
,
129 unsigned int midi_ports
);
130 unsigned int amdtp_stream_get_max_payload(struct amdtp_stream
*s
);
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
);
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
);
144 extern const unsigned int amdtp_syt_intervals
[CIP_SFC_COUNT
];
145 extern const unsigned int amdtp_rate_table
[CIP_SFC_COUNT
];
148 * amdtp_stream_running - check stream is running or not
149 * @s: the AMDTP stream
151 * If this function returns true, the stream is running.
153 static inline bool amdtp_stream_running(struct amdtp_stream
*s
)
155 return !IS_ERR(s
->context
);
159 * amdtp_streaming_error - check for streaming error
160 * @s: the AMDTP stream
162 * If this function returns true, the stream's packet queue has stopped due to
163 * an asynchronous error.
165 static inline bool amdtp_streaming_error(struct amdtp_stream
*s
)
167 return s
->packet_index
< 0;
171 * amdtp_stream_pcm_running - check PCM substream is running or not
172 * @s: the AMDTP stream
174 * If this function returns true, PCM substream in the AMDTP stream is running.
176 static inline bool amdtp_stream_pcm_running(struct amdtp_stream
*s
)
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
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.
190 static inline void amdtp_stream_pcm_trigger(struct amdtp_stream
*s
,
191 struct snd_pcm_substream
*pcm
)
193 ACCESS_ONCE(s
->pcm
) = pcm
;
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
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.
206 static inline void amdtp_stream_midi_trigger(struct amdtp_stream
*s
,
208 struct snd_rawmidi_substream
*midi
)
210 if (port
< s
->midi_ports
)
211 ACCESS_ONCE(s
->midi
[port
]) = midi
;
214 static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc
)
219 static inline void amdtp_stream_set_sync(enum cip_flags sync_mode
,
220 struct amdtp_stream
*master
,
221 struct amdtp_stream
*slave
)
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
;
228 master
->flags
&= ~CIP_SYNC_TO_DEVICE
;
229 slave
->flags
&= ~CIP_SYNC_TO_DEVICE
;
230 master
->sync_slave
= NULL
;
233 slave
->sync_slave
= NULL
;
237 * amdtp_stream_wait_callback - sleep till callbacked or timeout
238 * @s: the AMDTP stream
239 * @timeout: msec till timeout
241 * If this function return false, the AMDTP stream should be stopped.
243 static inline bool amdtp_stream_wait_callback(struct amdtp_stream
*s
,
244 unsigned int timeout
)
246 return wait_event_timeout(s
->callback_wait
,
247 s
->callbacked
== true,
248 msecs_to_jiffies(timeout
)) > 0;