]> Pileus Git - ~andy/linux/blob - sound/firewire/amdtp.h
ALSA: firewire-lib: taskletize the snd_pcm_period_elapsed() call
[~andy/linux] / sound / firewire / amdtp.h
1 #ifndef SOUND_FIREWIRE_AMDTP_H_INCLUDED
2 #define SOUND_FIREWIRE_AMDTP_H_INCLUDED
3
4 #include <linux/interrupt.h>
5 #include <linux/mutex.h>
6 #include <linux/spinlock.h>
7 #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.
15  */
16 enum cip_out_flags {
17         CIP_NONBLOCKING = 0,
18 };
19
20 /**
21  * enum cip_sfc - a stream's sample rate
22  */
23 enum cip_sfc {
24         CIP_SFC_32000  = 0,
25         CIP_SFC_44100  = 1,
26         CIP_SFC_48000  = 2,
27         CIP_SFC_88200  = 3,
28         CIP_SFC_96000  = 4,
29         CIP_SFC_176400 = 5,
30         CIP_SFC_192000 = 6,
31 };
32
33 #define AMDTP_OUT_PCM_FORMAT_BITS       (SNDRV_PCM_FMTBIT_S16 | \
34                                          SNDRV_PCM_FMTBIT_S32)
35
36 struct fw_unit;
37 struct fw_iso_context;
38 struct snd_pcm_substream;
39
40 struct amdtp_out_stream {
41         struct fw_unit *unit;
42         enum cip_out_flags flags;
43         struct fw_iso_context *context;
44         struct mutex mutex;
45
46         enum cip_sfc sfc;
47         unsigned int data_block_quadlets;
48         unsigned int pcm_channels;
49         unsigned int midi_ports;
50         void (*transfer_samples)(struct amdtp_out_stream *s,
51                                  struct snd_pcm_substream *pcm,
52                                  __be32 *buffer, unsigned int frames);
53
54         unsigned int syt_interval;
55         unsigned int source_node_id_field;
56         struct iso_packets_buffer buffer;
57
58         struct snd_pcm_substream *pcm;
59         struct tasklet_struct period_tasklet;
60
61         int packet_index;
62         unsigned int data_block_counter;
63
64         unsigned int data_block_state;
65
66         unsigned int last_syt_offset;
67         unsigned int syt_offset_state;
68
69         unsigned int pcm_buffer_pointer;
70         unsigned int pcm_period_pointer;
71 };
72
73 int amdtp_out_stream_init(struct amdtp_out_stream *s, struct fw_unit *unit,
74                           enum cip_out_flags flags);
75 void amdtp_out_stream_destroy(struct amdtp_out_stream *s);
76
77 void amdtp_out_stream_set_rate(struct amdtp_out_stream *s, unsigned int rate);
78 unsigned int amdtp_out_stream_get_max_payload(struct amdtp_out_stream *s);
79
80 int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed);
81 void amdtp_out_stream_update(struct amdtp_out_stream *s);
82 void amdtp_out_stream_stop(struct amdtp_out_stream *s);
83
84 void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s,
85                                      snd_pcm_format_t format);
86 void amdtp_out_stream_pcm_prepare(struct amdtp_out_stream *s);
87 void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s);
88
89 /**
90  * amdtp_out_stream_set_pcm - configure format of PCM samples
91  * @s: the AMDTP output stream to be configured
92  * @pcm_channels: the number of PCM samples in each data block, to be encoded
93  *                as AM824 multi-bit linear audio
94  *
95  * This function must not be called while the stream is running.
96  */
97 static inline void amdtp_out_stream_set_pcm(struct amdtp_out_stream *s,
98                                             unsigned int pcm_channels)
99 {
100         s->pcm_channels = pcm_channels;
101 }
102
103 /**
104  * amdtp_out_stream_set_midi - configure format of MIDI data
105  * @s: the AMDTP output stream to be configured
106  * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
107  *
108  * This function must not be called while the stream is running.
109  */
110 static inline void amdtp_out_stream_set_midi(struct amdtp_out_stream *s,
111                                              unsigned int midi_ports)
112 {
113         s->midi_ports = midi_ports;
114 }
115
116 /**
117  * amdtp_out_streaming_error - check for streaming error
118  * @s: the AMDTP output stream
119  *
120  * If this function returns true, the stream's packet queue has stopped due to
121  * an asynchronous error.
122  */
123 static inline bool amdtp_out_streaming_error(struct amdtp_out_stream *s)
124 {
125         return s->packet_index < 0;
126 }
127
128 /**
129  * amdtp_out_stream_pcm_trigger - start/stop playback from a PCM device
130  * @s: the AMDTP output stream
131  * @pcm: the PCM device to be started, or %NULL to stop the current device
132  *
133  * Call this function on a running isochronous stream to enable the actual
134  * transmission of PCM data.  This function should be called from the PCM
135  * device's .trigger callback.
136  */
137 static inline void amdtp_out_stream_pcm_trigger(struct amdtp_out_stream *s,
138                                                 struct snd_pcm_substream *pcm)
139 {
140         ACCESS_ONCE(s->pcm) = pcm;
141 }
142
143 /**
144  * amdtp_out_stream_pcm_pointer - get the PCM buffer position
145  * @s: the AMDTP output stream that transports the PCM data
146  *
147  * Returns the current buffer position, in frames.
148  */
149 static inline unsigned long
150 amdtp_out_stream_pcm_pointer(struct amdtp_out_stream *s)
151 {
152         return ACCESS_ONCE(s->pcm_buffer_pointer);
153 }
154
155 static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)
156 {
157         return sfc & 1;
158 }
159
160 #endif