blob: c831aaa2a811ea9fa0abfdf6be9bb8b2d295869a [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.
Clemens Ladisch31ef9132011-03-15 07:53:21 +010023 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090024enum cip_flags {
Clemens Ladische84d15f2011-09-04 22:12:48 +020025 CIP_NONBLOCKING = 0x00,
26 CIP_BLOCKING = 0x01,
Clemens Ladischa7304e32011-09-04 22:16:10 +020027 CIP_HI_DUALWIRE = 0x02,
Clemens Ladisch31ef9132011-03-15 07:53:21 +010028};
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,
Clemens Ladischa7304e32011-09-04 22:16:10 +020041 CIP_SFC_COUNT
Clemens Ladisch31ef9132011-03-15 07:53:21 +010042};
43
Takashi Sakamoto2b3fc452014-04-25 22:44:46 +090044#define AMDTP_IN_PCM_FORMAT_BITS SNDRV_PCM_FMTBIT_S32
45
Clemens Ladisch31ef9132011-03-15 07:53:21 +010046#define AMDTP_OUT_PCM_FORMAT_BITS (SNDRV_PCM_FMTBIT_S16 | \
47 SNDRV_PCM_FMTBIT_S32)
48
49struct fw_unit;
50struct fw_iso_context;
51struct snd_pcm_substream;
52
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090053enum amdtp_stream_direction {
54 AMDTP_OUT_STREAM = 0,
55 AMDTP_IN_STREAM
56};
57
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090058struct amdtp_stream {
Clemens Ladisch31ef9132011-03-15 07:53:21 +010059 struct fw_unit *unit;
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090060 enum cip_flags flags;
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090061 enum amdtp_stream_direction direction;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010062 struct fw_iso_context *context;
63 struct mutex mutex;
64
65 enum cip_sfc sfc;
Clemens Ladischa7304e32011-09-04 22:16:10 +020066 bool dual_wire;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010067 unsigned int data_block_quadlets;
68 unsigned int pcm_channels;
69 unsigned int midi_ports;
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090070 void (*transfer_samples)(struct amdtp_stream *s,
Clemens Ladisch31ef9132011-03-15 07:53:21 +010071 struct snd_pcm_substream *pcm,
72 __be32 *buffer, unsigned int frames);
73
74 unsigned int syt_interval;
Clemens Ladische84d15f2011-09-04 22:12:48 +020075 unsigned int transfer_delay;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010076 unsigned int source_node_id_field;
77 struct iso_packets_buffer buffer;
78
79 struct snd_pcm_substream *pcm;
Clemens Ladisch76fb8782012-05-13 22:03:09 +020080 struct tasklet_struct period_tasklet;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010081
Clemens Ladischec00f5e2011-03-15 07:57:24 +010082 int packet_index;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010083 unsigned int data_block_counter;
84
85 unsigned int data_block_state;
86
87 unsigned int last_syt_offset;
88 unsigned int syt_offset_state;
89
90 unsigned int pcm_buffer_pointer;
91 unsigned int pcm_period_pointer;
Clemens Ladisch92b862c2012-05-13 19:07:22 +020092 bool pointer_flush;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010093};
94
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090095int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
Takashi Sakamoto3ff7e8f2014-04-25 22:44:44 +090096 enum amdtp_stream_direction dir,
Takashi Sakamotobe4a2892014-04-25 22:44:42 +090097 enum cip_flags flags);
98void amdtp_stream_destroy(struct amdtp_stream *s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +010099
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900100void amdtp_stream_set_parameters(struct amdtp_stream *s,
101 unsigned int rate,
102 unsigned int pcm_channels,
103 unsigned int midi_ports);
104unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100105
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900106int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed);
107void amdtp_stream_update(struct amdtp_stream *s);
108void amdtp_stream_stop(struct amdtp_stream *s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100109
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900110void amdtp_stream_set_pcm_format(struct amdtp_stream *s,
111 snd_pcm_format_t format);
112void amdtp_stream_pcm_prepare(struct amdtp_stream *s);
113unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s);
114void amdtp_stream_pcm_abort(struct amdtp_stream *s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100115
Clemens Ladischc5280e92011-10-16 21:39:00 +0200116extern const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT];
Clemens Ladischa7304e32011-09-04 22:16:10 +0200117
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900118/**
119 * amdtp_stream_running - check stream is running or not
120 * @s: the AMDTP stream
121 *
122 * If this function returns true, the stream is running.
123 */
124static inline bool amdtp_stream_running(struct amdtp_stream *s)
Clemens Ladisch20b65dd2011-09-04 22:15:44 +0200125{
126 return !IS_ERR(s->context);
127}
128
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100129/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900130 * amdtp_streaming_error - check for streaming error
131 * @s: the AMDTP stream
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100132 *
133 * If this function returns true, the stream's packet queue has stopped due to
134 * an asynchronous error.
135 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900136static inline bool amdtp_streaming_error(struct amdtp_stream *s)
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100137{
138 return s->packet_index < 0;
139}
140
141/**
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900142 * amdtp_stream_pcm_trigger - start/stop playback from a PCM device
143 * @s: the AMDTP stream
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100144 * @pcm: the PCM device to be started, or %NULL to stop the current device
145 *
146 * Call this function on a running isochronous stream to enable the actual
147 * transmission of PCM data. This function should be called from the PCM
148 * device's .trigger callback.
149 */
Takashi Sakamotobe4a2892014-04-25 22:44:42 +0900150static inline void amdtp_stream_pcm_trigger(struct amdtp_stream *s,
151 struct snd_pcm_substream *pcm)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100152{
153 ACCESS_ONCE(s->pcm) = pcm;
154}
155
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100156static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)
157{
158 return sfc & 1;
159}
160
161#endif