blob: 839ebf812d79e47302e3c4c57b5f2f74695551b7 [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>
Clemens Ladisch31ef9132011-03-15 07:53:21 +01007#include "packets-buffer.h"
8
9/**
10 * enum cip_out_flags - describes details of the streaming protocol
11 * @CIP_NONBLOCKING: In non-blocking mode, each packet contains
12 * sample_rate/8000 samples, with rounding up or down to adjust
13 * for clock skew and left-over fractional samples. This should
14 * be used if supported by the device.
Clemens Ladische84d15f2011-09-04 22:12:48 +020015 * @CIP_BLOCKING: In blocking mode, each packet contains either zero or
16 * SYT_INTERVAL samples, with these two types alternating so that
17 * the overall sample rate comes out right.
Clemens Ladischa7304e32011-09-04 22:16:10 +020018 * @CIP_HI_DUALWIRE: At rates above 96 kHz, pretend that the stream runs
19 * at half the actual sample rate with twice the number of channels;
20 * two samples of a channel are stored consecutively in the packet.
21 * Requires blocking mode and SYT_INTERVAL-aligned PCM buffer size.
Clemens Ladisch31ef9132011-03-15 07:53:21 +010022 */
23enum cip_out_flags {
Clemens Ladische84d15f2011-09-04 22:12:48 +020024 CIP_NONBLOCKING = 0x00,
25 CIP_BLOCKING = 0x01,
Clemens Ladischa7304e32011-09-04 22:16:10 +020026 CIP_HI_DUALWIRE = 0x02,
Clemens Ladisch31ef9132011-03-15 07:53:21 +010027};
28
29/**
30 * enum cip_sfc - a stream's sample rate
31 */
32enum cip_sfc {
33 CIP_SFC_32000 = 0,
34 CIP_SFC_44100 = 1,
35 CIP_SFC_48000 = 2,
36 CIP_SFC_88200 = 3,
37 CIP_SFC_96000 = 4,
38 CIP_SFC_176400 = 5,
39 CIP_SFC_192000 = 6,
Clemens Ladischa7304e32011-09-04 22:16:10 +020040 CIP_SFC_COUNT
Clemens Ladisch31ef9132011-03-15 07:53:21 +010041};
42
43#define AMDTP_OUT_PCM_FORMAT_BITS (SNDRV_PCM_FMTBIT_S16 | \
44 SNDRV_PCM_FMTBIT_S32)
45
46struct fw_unit;
47struct fw_iso_context;
48struct snd_pcm_substream;
49
50struct amdtp_out_stream {
51 struct fw_unit *unit;
52 enum cip_out_flags flags;
53 struct fw_iso_context *context;
54 struct mutex mutex;
55
56 enum cip_sfc sfc;
Clemens Ladischa7304e32011-09-04 22:16:10 +020057 bool dual_wire;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010058 unsigned int data_block_quadlets;
59 unsigned int pcm_channels;
60 unsigned int midi_ports;
61 void (*transfer_samples)(struct amdtp_out_stream *s,
62 struct snd_pcm_substream *pcm,
63 __be32 *buffer, unsigned int frames);
64
65 unsigned int syt_interval;
Clemens Ladische84d15f2011-09-04 22:12:48 +020066 unsigned int transfer_delay;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010067 unsigned int source_node_id_field;
68 struct iso_packets_buffer buffer;
69
70 struct snd_pcm_substream *pcm;
Clemens Ladisch76fb8782012-05-13 22:03:09 +020071 struct tasklet_struct period_tasklet;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010072
Clemens Ladischec00f5e2011-03-15 07:57:24 +010073 int packet_index;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010074 unsigned int data_block_counter;
75
76 unsigned int data_block_state;
77
78 unsigned int last_syt_offset;
79 unsigned int syt_offset_state;
80
81 unsigned int pcm_buffer_pointer;
82 unsigned int pcm_period_pointer;
Clemens Ladisch92b862c2012-05-13 19:07:22 +020083 bool pointer_flush;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010084};
85
86int amdtp_out_stream_init(struct amdtp_out_stream *s, struct fw_unit *unit,
87 enum cip_out_flags flags);
88void amdtp_out_stream_destroy(struct amdtp_out_stream *s);
89
Clemens Ladischa7304e32011-09-04 22:16:10 +020090void amdtp_out_stream_set_parameters(struct amdtp_out_stream *s,
91 unsigned int rate,
92 unsigned int pcm_channels,
93 unsigned int midi_ports);
Clemens Ladisch31ef9132011-03-15 07:53:21 +010094unsigned int amdtp_out_stream_get_max_payload(struct amdtp_out_stream *s);
95
96int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed);
97void amdtp_out_stream_update(struct amdtp_out_stream *s);
98void amdtp_out_stream_stop(struct amdtp_out_stream *s);
99
100void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s,
101 snd_pcm_format_t format);
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200102void amdtp_out_stream_pcm_prepare(struct amdtp_out_stream *s);
Clemens Ladische9148dd2012-05-13 18:49:14 +0200103unsigned long amdtp_out_stream_pcm_pointer(struct amdtp_out_stream *s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100104void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s);
105
Clemens Ladischc5280e92011-10-16 21:39:00 +0200106extern const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT];
Clemens Ladischa7304e32011-09-04 22:16:10 +0200107
Clemens Ladisch20b65dd2011-09-04 22:15:44 +0200108static inline bool amdtp_out_stream_running(struct amdtp_out_stream *s)
109{
110 return !IS_ERR(s->context);
111}
112
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100113/**
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100114 * amdtp_out_streaming_error - check for streaming error
115 * @s: the AMDTP output stream
116 *
117 * If this function returns true, the stream's packet queue has stopped due to
118 * an asynchronous error.
119 */
120static inline bool amdtp_out_streaming_error(struct amdtp_out_stream *s)
121{
122 return s->packet_index < 0;
123}
124
125/**
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100126 * amdtp_out_stream_pcm_trigger - start/stop playback from a PCM device
127 * @s: the AMDTP output stream
128 * @pcm: the PCM device to be started, or %NULL to stop the current device
129 *
130 * Call this function on a running isochronous stream to enable the actual
131 * transmission of PCM data. This function should be called from the PCM
132 * device's .trigger callback.
133 */
134static inline void amdtp_out_stream_pcm_trigger(struct amdtp_out_stream *s,
135 struct snd_pcm_substream *pcm)
136{
137 ACCESS_ONCE(s->pcm) = pcm;
138}
139
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100140static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)
141{
142 return sfc & 1;
143}
144
145#endif