blob: 28b1bf5891e68306c917ecd01c6c778fea29b0a6 [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 Ladisch31ef9132011-03-15 07:53:21 +010018 */
19enum cip_out_flags {
Clemens Ladische84d15f2011-09-04 22:12:48 +020020 CIP_NONBLOCKING = 0x00,
21 CIP_BLOCKING = 0x01,
Clemens Ladisch31ef9132011-03-15 07:53:21 +010022};
23
24/**
25 * enum cip_sfc - a stream's sample rate
26 */
27enum cip_sfc {
28 CIP_SFC_32000 = 0,
29 CIP_SFC_44100 = 1,
30 CIP_SFC_48000 = 2,
31 CIP_SFC_88200 = 3,
32 CIP_SFC_96000 = 4,
33 CIP_SFC_176400 = 5,
34 CIP_SFC_192000 = 6,
35};
36
37#define AMDTP_OUT_PCM_FORMAT_BITS (SNDRV_PCM_FMTBIT_S16 | \
38 SNDRV_PCM_FMTBIT_S32)
39
40struct fw_unit;
41struct fw_iso_context;
42struct snd_pcm_substream;
43
44struct amdtp_out_stream {
45 struct fw_unit *unit;
46 enum cip_out_flags flags;
47 struct fw_iso_context *context;
48 struct mutex mutex;
49
50 enum cip_sfc sfc;
51 unsigned int data_block_quadlets;
52 unsigned int pcm_channels;
53 unsigned int midi_ports;
54 void (*transfer_samples)(struct amdtp_out_stream *s,
55 struct snd_pcm_substream *pcm,
56 __be32 *buffer, unsigned int frames);
57
58 unsigned int syt_interval;
Clemens Ladische84d15f2011-09-04 22:12:48 +020059 unsigned int transfer_delay;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010060 unsigned int source_node_id_field;
61 struct iso_packets_buffer buffer;
62
63 struct snd_pcm_substream *pcm;
Clemens Ladisch76fb8782012-05-13 22:03:09 +020064 struct tasklet_struct period_tasklet;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010065
Clemens Ladischec00f5e2011-03-15 07:57:24 +010066 int packet_index;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010067 unsigned int data_block_counter;
68
69 unsigned int data_block_state;
70
71 unsigned int last_syt_offset;
72 unsigned int syt_offset_state;
73
74 unsigned int pcm_buffer_pointer;
75 unsigned int pcm_period_pointer;
Clemens Ladisch92b862c2012-05-13 19:07:22 +020076 bool pointer_flush;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010077};
78
79int amdtp_out_stream_init(struct amdtp_out_stream *s, struct fw_unit *unit,
80 enum cip_out_flags flags);
81void amdtp_out_stream_destroy(struct amdtp_out_stream *s);
82
83void amdtp_out_stream_set_rate(struct amdtp_out_stream *s, unsigned int rate);
84unsigned int amdtp_out_stream_get_max_payload(struct amdtp_out_stream *s);
85
86int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed);
87void amdtp_out_stream_update(struct amdtp_out_stream *s);
88void amdtp_out_stream_stop(struct amdtp_out_stream *s);
89
90void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s,
91 snd_pcm_format_t format);
Clemens Ladisch76fb8782012-05-13 22:03:09 +020092void amdtp_out_stream_pcm_prepare(struct amdtp_out_stream *s);
Clemens Ladische9148dd2012-05-13 18:49:14 +020093unsigned long amdtp_out_stream_pcm_pointer(struct amdtp_out_stream *s);
Clemens Ladisch31ef9132011-03-15 07:53:21 +010094void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s);
95
Clemens Ladisch20b65dd2011-09-04 22:15:44 +020096static inline bool amdtp_out_stream_running(struct amdtp_out_stream *s)
97{
98 return !IS_ERR(s->context);
99}
100
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100101/**
102 * amdtp_out_stream_set_pcm - configure format of PCM samples
103 * @s: the AMDTP output stream to be configured
104 * @pcm_channels: the number of PCM samples in each data block, to be encoded
105 * as AM824 multi-bit linear audio
106 *
107 * This function must not be called while the stream is running.
108 */
109static inline void amdtp_out_stream_set_pcm(struct amdtp_out_stream *s,
110 unsigned int pcm_channels)
111{
112 s->pcm_channels = pcm_channels;
113}
114
115/**
116 * amdtp_out_stream_set_midi - configure format of MIDI data
117 * @s: the AMDTP output stream to be configured
118 * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
119 *
120 * This function must not be called while the stream is running.
121 */
122static inline void amdtp_out_stream_set_midi(struct amdtp_out_stream *s,
123 unsigned int midi_ports)
124{
125 s->midi_ports = midi_ports;
126}
127
128/**
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100129 * amdtp_out_streaming_error - check for streaming error
130 * @s: the AMDTP output stream
131 *
132 * If this function returns true, the stream's packet queue has stopped due to
133 * an asynchronous error.
134 */
135static inline bool amdtp_out_streaming_error(struct amdtp_out_stream *s)
136{
137 return s->packet_index < 0;
138}
139
140/**
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100141 * amdtp_out_stream_pcm_trigger - start/stop playback from a PCM device
142 * @s: the AMDTP output stream
143 * @pcm: the PCM device to be started, or %NULL to stop the current device
144 *
145 * Call this function on a running isochronous stream to enable the actual
146 * transmission of PCM data. This function should be called from the PCM
147 * device's .trigger callback.
148 */
149static inline void amdtp_out_stream_pcm_trigger(struct amdtp_out_stream *s,
150 struct snd_pcm_substream *pcm)
151{
152 ACCESS_ONCE(s->pcm) = pcm;
153}
154
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100155static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)
156{
157 return sfc & 1;
158}
159
160#endif