Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 1 | #ifndef SOUND_FIREWIRE_AMDTP_H_INCLUDED |
| 2 | #define SOUND_FIREWIRE_AMDTP_H_INCLUDED |
| 3 | |
Clemens Ladisch | 20b65dd | 2011-09-04 22:15:44 +0200 | [diff] [blame] | 4 | #include <linux/err.h> |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 5 | #include <linux/interrupt.h> |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 6 | #include <linux/mutex.h> |
Takashi Sakamoto | 777fb57 | 2013-11-19 13:29:24 +0900 | [diff] [blame] | 7 | #include <sound/asound.h> |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 8 | #include "packets-buffer.h" |
| 9 | |
| 10 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 11 | * enum cip_flags - describes details of the streaming protocol |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 12 | * @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 Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 16 | * @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 Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 19 | * @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 Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 23 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 24 | enum cip_flags { |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 25 | CIP_NONBLOCKING = 0x00, |
| 26 | CIP_BLOCKING = 0x01, |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 27 | CIP_HI_DUALWIRE = 0x02, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | /** |
| 31 | * enum cip_sfc - a stream's sample rate |
| 32 | */ |
| 33 | enum 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 Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 41 | CIP_SFC_COUNT |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 42 | }; |
| 43 | |
Takashi Sakamoto | 2b3fc45 | 2014-04-25 22:44:46 +0900 | [diff] [blame] | 44 | #define AMDTP_IN_PCM_FORMAT_BITS SNDRV_PCM_FMTBIT_S32 |
| 45 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 46 | #define AMDTP_OUT_PCM_FORMAT_BITS (SNDRV_PCM_FMTBIT_S16 | \ |
| 47 | SNDRV_PCM_FMTBIT_S32) |
| 48 | |
Takashi Sakamoto | 83d8d72 | 2014-04-25 22:44:47 +0900 | [diff] [blame^] | 49 | |
| 50 | /* |
| 51 | * AMDTP packet can include channels for MIDI conformant data. |
| 52 | * Each MIDI conformant data channel includes 8 MPX-MIDI data stream. |
| 53 | * Each MPX-MIDI data stream includes one data stream from/to MIDI ports. |
| 54 | * |
| 55 | * This module supports maximum 1 MIDI conformant data channels. |
| 56 | * Then this AMDTP packets can transfer maximum 8 MIDI data streams. |
| 57 | */ |
| 58 | #define AMDTP_MAX_CHANNELS_FOR_MIDI 1 |
| 59 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 60 | struct fw_unit; |
| 61 | struct fw_iso_context; |
| 62 | struct snd_pcm_substream; |
Takashi Sakamoto | 83d8d72 | 2014-04-25 22:44:47 +0900 | [diff] [blame^] | 63 | struct snd_rawmidi_substream; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 64 | |
Takashi Sakamoto | 3ff7e8f | 2014-04-25 22:44:44 +0900 | [diff] [blame] | 65 | enum amdtp_stream_direction { |
| 66 | AMDTP_OUT_STREAM = 0, |
| 67 | AMDTP_IN_STREAM |
| 68 | }; |
| 69 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 70 | struct amdtp_stream { |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 71 | struct fw_unit *unit; |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 72 | enum cip_flags flags; |
Takashi Sakamoto | 3ff7e8f | 2014-04-25 22:44:44 +0900 | [diff] [blame] | 73 | enum amdtp_stream_direction direction; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 74 | struct fw_iso_context *context; |
| 75 | struct mutex mutex; |
| 76 | |
| 77 | enum cip_sfc sfc; |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 78 | bool dual_wire; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 79 | unsigned int data_block_quadlets; |
| 80 | unsigned int pcm_channels; |
| 81 | unsigned int midi_ports; |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 82 | void (*transfer_samples)(struct amdtp_stream *s, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 83 | struct snd_pcm_substream *pcm, |
| 84 | __be32 *buffer, unsigned int frames); |
| 85 | |
| 86 | unsigned int syt_interval; |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 87 | unsigned int transfer_delay; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 88 | unsigned int source_node_id_field; |
| 89 | struct iso_packets_buffer buffer; |
| 90 | |
| 91 | struct snd_pcm_substream *pcm; |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 92 | struct tasklet_struct period_tasklet; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 93 | |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 94 | int packet_index; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 95 | unsigned int data_block_counter; |
| 96 | |
| 97 | unsigned int data_block_state; |
| 98 | |
| 99 | unsigned int last_syt_offset; |
| 100 | unsigned int syt_offset_state; |
| 101 | |
| 102 | unsigned int pcm_buffer_pointer; |
| 103 | unsigned int pcm_period_pointer; |
Clemens Ladisch | 92b862c | 2012-05-13 19:07:22 +0200 | [diff] [blame] | 104 | bool pointer_flush; |
Takashi Sakamoto | 83d8d72 | 2014-04-25 22:44:47 +0900 | [diff] [blame^] | 105 | |
| 106 | struct snd_rawmidi_substream *midi[AMDTP_MAX_CHANNELS_FOR_MIDI * 8]; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 107 | }; |
| 108 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 109 | int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit, |
Takashi Sakamoto | 3ff7e8f | 2014-04-25 22:44:44 +0900 | [diff] [blame] | 110 | enum amdtp_stream_direction dir, |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 111 | enum cip_flags flags); |
| 112 | void amdtp_stream_destroy(struct amdtp_stream *s); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 113 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 114 | void amdtp_stream_set_parameters(struct amdtp_stream *s, |
| 115 | unsigned int rate, |
| 116 | unsigned int pcm_channels, |
| 117 | unsigned int midi_ports); |
| 118 | unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 119 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 120 | int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed); |
| 121 | void amdtp_stream_update(struct amdtp_stream *s); |
| 122 | void amdtp_stream_stop(struct amdtp_stream *s); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 123 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 124 | void amdtp_stream_set_pcm_format(struct amdtp_stream *s, |
| 125 | snd_pcm_format_t format); |
| 126 | void amdtp_stream_pcm_prepare(struct amdtp_stream *s); |
| 127 | unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s); |
| 128 | void amdtp_stream_pcm_abort(struct amdtp_stream *s); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 129 | |
Clemens Ladisch | c5280e9 | 2011-10-16 21:39:00 +0200 | [diff] [blame] | 130 | extern const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT]; |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 131 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 132 | /** |
| 133 | * amdtp_stream_running - check stream is running or not |
| 134 | * @s: the AMDTP stream |
| 135 | * |
| 136 | * If this function returns true, the stream is running. |
| 137 | */ |
| 138 | static inline bool amdtp_stream_running(struct amdtp_stream *s) |
Clemens Ladisch | 20b65dd | 2011-09-04 22:15:44 +0200 | [diff] [blame] | 139 | { |
| 140 | return !IS_ERR(s->context); |
| 141 | } |
| 142 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 143 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 144 | * amdtp_streaming_error - check for streaming error |
| 145 | * @s: the AMDTP stream |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 146 | * |
| 147 | * If this function returns true, the stream's packet queue has stopped due to |
| 148 | * an asynchronous error. |
| 149 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 150 | static inline bool amdtp_streaming_error(struct amdtp_stream *s) |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 151 | { |
| 152 | return s->packet_index < 0; |
| 153 | } |
| 154 | |
| 155 | /** |
Takashi Sakamoto | 83d8d72 | 2014-04-25 22:44:47 +0900 | [diff] [blame^] | 156 | * amdtp_stream_pcm_running - check PCM substream is running or not |
| 157 | * @s: the AMDTP stream |
| 158 | * |
| 159 | * If this function returns true, PCM substream in the AMDTP stream is running. |
| 160 | */ |
| 161 | static inline bool amdtp_stream_pcm_running(struct amdtp_stream *s) |
| 162 | { |
| 163 | return !!s->pcm; |
| 164 | } |
| 165 | |
| 166 | /** |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 167 | * amdtp_stream_pcm_trigger - start/stop playback from a PCM device |
| 168 | * @s: the AMDTP stream |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 169 | * @pcm: the PCM device to be started, or %NULL to stop the current device |
| 170 | * |
| 171 | * Call this function on a running isochronous stream to enable the actual |
| 172 | * transmission of PCM data. This function should be called from the PCM |
| 173 | * device's .trigger callback. |
| 174 | */ |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 175 | static inline void amdtp_stream_pcm_trigger(struct amdtp_stream *s, |
| 176 | struct snd_pcm_substream *pcm) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 177 | { |
| 178 | ACCESS_ONCE(s->pcm) = pcm; |
| 179 | } |
| 180 | |
Takashi Sakamoto | 83d8d72 | 2014-04-25 22:44:47 +0900 | [diff] [blame^] | 181 | /** |
| 182 | * amdtp_stream_midi_trigger - start/stop playback/capture with a MIDI device |
| 183 | * @s: the AMDTP stream |
| 184 | * @port: index of MIDI port |
| 185 | * @midi: the MIDI device to be started, or %NULL to stop the current device |
| 186 | * |
| 187 | * Call this function on a running isochronous stream to enable the actual |
| 188 | * transmission of MIDI data. This function should be called from the MIDI |
| 189 | * device's .trigger callback. |
| 190 | */ |
| 191 | static inline void amdtp_stream_midi_trigger(struct amdtp_stream *s, |
| 192 | unsigned int port, |
| 193 | struct snd_rawmidi_substream *midi) |
| 194 | { |
| 195 | if (port < s->midi_ports) |
| 196 | ACCESS_ONCE(s->midi[port]) = midi; |
| 197 | } |
| 198 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 199 | static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc) |
| 200 | { |
| 201 | return sfc & 1; |
| 202 | } |
| 203 | |
| 204 | #endif |