blob: 2bd3b27ac938f290b878752d828d7d6fa207a3cc [file] [log] [blame]
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001#ifndef SOUND_FIREWIRE_AMDTP_H_INCLUDED
2#define SOUND_FIREWIRE_AMDTP_H_INCLUDED
3
Clemens Ladisch20b65dd2011-09-04 22:15:44 +02004#include <linux/err.h>
Clemens Ladisch76fb8782012-05-13 22:03:09 +02005#include <linux/interrupt.h>
Clemens Ladisch31ef9132011-03-15 07:53:21 +01006#include <linux/mutex.h>
Takashi Sakamoto777fb572013-11-19 13:29:24 +09007#include <sound/asound.h>
Clemens Ladisch31ef9132011-03-15 07:53:21 +01008#include "packets-buffer.h"
9
10/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090011 * enum cip_flags - describes details of the streaming protocol
Clemens Ladisch31ef9132011-03-15 07:53:21 +010012 * @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.
Clemens Ladische84d15f2011-09-04 22:12:48 +020016 * @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.
Clemens Ladischa7304e32011-09-04 22:16:10 +020019 * @CIP_HI_DUALWIRE: At rates above 96 kHz, pretend that the stream runs
20 * at half the actual sample rate with twice the number of channels;
21 * two samples of a channel are stored consecutively in the packet.
22 * Requires blocking mode and SYT_INTERVAL-aligned PCM buffer size.
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +090023 * @CIP_SYNC_TO_DEVICE: In sync to device mode, time stamp in out packets is
24 * generated by in packets. Defaultly this driver generates timestamp.
Clemens Ladisch31ef9132011-03-15 07:53:21 +010025 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090026enum cip_flags {
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +090027 CIP_NONBLOCKING = 0x00,
28 CIP_BLOCKING = 0x01,
29 CIP_HI_DUALWIRE = 0x02,
30 CIP_SYNC_TO_DEVICE = 0x04,
Clemens Ladisch31ef9132011-03-15 07:53:21 +010031};
32
33/**
34 * enum cip_sfc - a stream's sample rate
35 */
36enum cip_sfc {
37 CIP_SFC_32000 = 0,
38 CIP_SFC_44100 = 1,
39 CIP_SFC_48000 = 2,
40 CIP_SFC_88200 = 3,
41 CIP_SFC_96000 = 4,
42 CIP_SFC_176400 = 5,
43 CIP_SFC_192000 = 6,
Clemens Ladischa7304e32011-09-04 22:16:10 +020044 CIP_SFC_COUNT
Clemens Ladisch31ef9132011-03-15 07:53:21 +010045};
46
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090047#define AMDTP_IN_PCM_FORMAT_BITS SNDRV_PCM_FMTBIT_S32
48
Clemens Ladisch31ef9132011-03-15 07:53:21 +010049#define AMDTP_OUT_PCM_FORMAT_BITS (SNDRV_PCM_FMTBIT_S16 | \
50 SNDRV_PCM_FMTBIT_S32)
51
Takashi Sakamoto83d8d722014-04-25 22:44:47 +090052
53/*
54 * AMDTP packet can include channels for MIDI conformant data.
55 * Each MIDI conformant data channel includes 8 MPX-MIDI data stream.
56 * Each MPX-MIDI data stream includes one data stream from/to MIDI ports.
57 *
58 * This module supports maximum 1 MIDI conformant data channels.
59 * Then this AMDTP packets can transfer maximum 8 MIDI data streams.
60 */
61#define AMDTP_MAX_CHANNELS_FOR_MIDI 1
62
Clemens Ladisch31ef9132011-03-15 07:53:21 +010063struct fw_unit;
64struct fw_iso_context;
65struct snd_pcm_substream;
Takashi Sakamoto83d8d722014-04-25 22:44:47 +090066struct snd_rawmidi_substream;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010067
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090068enum amdtp_stream_direction {
69 AMDTP_OUT_STREAM = 0,
70 AMDTP_IN_STREAM
71};
72
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090073struct amdtp_stream {
Clemens Ladisch31ef9132011-03-15 07:53:21 +010074 struct fw_unit *unit;
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090075 enum cip_flags flags;
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090076 enum amdtp_stream_direction direction;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010077 struct fw_iso_context *context;
78 struct mutex mutex;
79
80 enum cip_sfc sfc;
Clemens Ladischa7304e32011-09-04 22:16:10 +020081 bool dual_wire;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010082 unsigned int data_block_quadlets;
83 unsigned int pcm_channels;
84 unsigned int midi_ports;
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090085 void (*transfer_samples)(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +010086 struct snd_pcm_substream *pcm,
87 __be32 *buffer, unsigned int frames);
88
89 unsigned int syt_interval;
Clemens Ladische84d15f2011-09-04 22:12:48 +020090 unsigned int transfer_delay;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010091 unsigned int source_node_id_field;
92 struct iso_packets_buffer buffer;
93
94 struct snd_pcm_substream *pcm;
Clemens Ladisch76fb8782012-05-13 22:03:09 +020095 struct tasklet_struct period_tasklet;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010096
Clemens Ladischec00f5e2011-03-15 07:57:24 +010097 int packet_index;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010098 unsigned int data_block_counter;
99
100 unsigned int data_block_state;
101
102 unsigned int last_syt_offset;
103 unsigned int syt_offset_state;
104
105 unsigned int pcm_buffer_pointer;
106 unsigned int pcm_period_pointer;
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200107 bool pointer_flush;
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900108
109 struct snd_rawmidi_substream *midi[AMDTP_MAX_CHANNELS_FOR_MIDI * 8];
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900110
111 bool callbacked;
112 wait_queue_head_t callback_wait;
113 struct amdtp_stream *sync_slave;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100114};
115
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900116int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +0900117 enum amdtp_stream_direction dir,
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900118 enum cip_flags flags);
119void amdtp_stream_destroy(struct amdtp_stream *s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100120
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900121void amdtp_stream_set_parameters(struct amdtp_stream *s,
122 unsigned int rate,
123 unsigned int pcm_channels,
124 unsigned int midi_ports);
125unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100126
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900127int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed);
128void amdtp_stream_update(struct amdtp_stream *s);
129void amdtp_stream_stop(struct amdtp_stream *s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100130
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900131void amdtp_stream_set_pcm_format(struct amdtp_stream *s,
132 snd_pcm_format_t format);
133void amdtp_stream_pcm_prepare(struct amdtp_stream *s);
134unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s);
135void amdtp_stream_pcm_abort(struct amdtp_stream *s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100136
Clemens Ladischc5280e92011-10-16 21:39:00 +0200137extern const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT];
Clemens Ladischa7304e32011-09-04 22:16:10 +0200138
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900139/**
140 * amdtp_stream_running - check stream is running or not
141 * @s: the AMDTP stream
142 *
143 * If this function returns true, the stream is running.
144 */
145static inline bool amdtp_stream_running(struct amdtp_stream *s)
Clemens Ladisch20b65dd2011-09-04 22:15:44 +0200146{
147 return !IS_ERR(s->context);
148}
149
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100150/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900151 * amdtp_streaming_error - check for streaming error
152 * @s: the AMDTP stream
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100153 *
154 * If this function returns true, the stream's packet queue has stopped due to
155 * an asynchronous error.
156 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900157static inline bool amdtp_streaming_error(struct amdtp_stream *s)
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100158{
159 return s->packet_index < 0;
160}
161
162/**
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900163 * amdtp_stream_pcm_running - check PCM substream is running or not
164 * @s: the AMDTP stream
165 *
166 * If this function returns true, PCM substream in the AMDTP stream is running.
167 */
168static inline bool amdtp_stream_pcm_running(struct amdtp_stream *s)
169{
170 return !!s->pcm;
171}
172
173/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900174 * amdtp_stream_pcm_trigger - start/stop playback from a PCM device
175 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100176 * @pcm: the PCM device to be started, or %NULL to stop the current device
177 *
178 * Call this function on a running isochronous stream to enable the actual
179 * transmission of PCM data. This function should be called from the PCM
180 * device's .trigger callback.
181 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900182static inline void amdtp_stream_pcm_trigger(struct amdtp_stream *s,
183 struct snd_pcm_substream *pcm)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100184{
185 ACCESS_ONCE(s->pcm) = pcm;
186}
187
Takashi Sakamoto83d8d722014-04-25 22:44:47 +0900188/**
189 * amdtp_stream_midi_trigger - start/stop playback/capture with a MIDI device
190 * @s: the AMDTP stream
191 * @port: index of MIDI port
192 * @midi: the MIDI device to be started, or %NULL to stop the current device
193 *
194 * Call this function on a running isochronous stream to enable the actual
195 * transmission of MIDI data. This function should be called from the MIDI
196 * device's .trigger callback.
197 */
198static inline void amdtp_stream_midi_trigger(struct amdtp_stream *s,
199 unsigned int port,
200 struct snd_rawmidi_substream *midi)
201{
202 if (port < s->midi_ports)
203 ACCESS_ONCE(s->midi[port]) = midi;
204}
205
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100206static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)
207{
208 return sfc & 1;
209}
210
Takashi Sakamoto7b3b0d82014-04-25 22:44:49 +0900211static inline void amdtp_stream_set_sync(enum cip_flags sync_mode,
212 struct amdtp_stream *master,
213 struct amdtp_stream *slave)
214{
215 if (sync_mode == CIP_SYNC_TO_DEVICE) {
216 master->flags |= CIP_SYNC_TO_DEVICE;
217 slave->flags |= CIP_SYNC_TO_DEVICE;
218 master->sync_slave = slave;
219 } else {
220 master->flags &= ~CIP_SYNC_TO_DEVICE;
221 slave->flags &= ~CIP_SYNC_TO_DEVICE;
222 master->sync_slave = NULL;
223 }
224
225 slave->sync_slave = NULL;
226}
227
228/**
229 * amdtp_stream_wait_callback - sleep till callbacked or timeout
230 * @s: the AMDTP stream
231 * @timeout: msec till timeout
232 *
233 * If this function return false, the AMDTP stream should be stopped.
234 */
235static inline bool amdtp_stream_wait_callback(struct amdtp_stream *s,
236 unsigned int timeout)
237{
238 return wait_event_timeout(s->callback_wait,
239 s->callbacked == true,
240 msecs_to_jiffies(timeout)) > 0;
241}
242
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100243#endif