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 | /** |
| 11 | * enum cip_out_flags - describes details of the streaming protocol |
| 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 | */ |
| 24 | enum cip_out_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 | |
| 44 | #define AMDTP_OUT_PCM_FORMAT_BITS (SNDRV_PCM_FMTBIT_S16 | \ |
| 45 | SNDRV_PCM_FMTBIT_S32) |
| 46 | |
| 47 | struct fw_unit; |
| 48 | struct fw_iso_context; |
| 49 | struct snd_pcm_substream; |
| 50 | |
| 51 | struct amdtp_out_stream { |
| 52 | struct fw_unit *unit; |
| 53 | enum cip_out_flags flags; |
| 54 | struct fw_iso_context *context; |
| 55 | struct mutex mutex; |
| 56 | |
| 57 | enum cip_sfc sfc; |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 58 | bool dual_wire; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 59 | unsigned int data_block_quadlets; |
| 60 | unsigned int pcm_channels; |
| 61 | unsigned int midi_ports; |
| 62 | void (*transfer_samples)(struct amdtp_out_stream *s, |
| 63 | struct snd_pcm_substream *pcm, |
| 64 | __be32 *buffer, unsigned int frames); |
| 65 | |
| 66 | unsigned int syt_interval; |
Clemens Ladisch | e84d15f | 2011-09-04 22:12:48 +0200 | [diff] [blame] | 67 | unsigned int transfer_delay; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 68 | unsigned int source_node_id_field; |
| 69 | struct iso_packets_buffer buffer; |
| 70 | |
| 71 | struct snd_pcm_substream *pcm; |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 72 | struct tasklet_struct period_tasklet; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 73 | |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 74 | int packet_index; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 75 | unsigned int data_block_counter; |
| 76 | |
| 77 | unsigned int data_block_state; |
| 78 | |
| 79 | unsigned int last_syt_offset; |
| 80 | unsigned int syt_offset_state; |
| 81 | |
| 82 | unsigned int pcm_buffer_pointer; |
| 83 | unsigned int pcm_period_pointer; |
Clemens Ladisch | 92b862c | 2012-05-13 19:07:22 +0200 | [diff] [blame] | 84 | bool pointer_flush; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | int amdtp_out_stream_init(struct amdtp_out_stream *s, struct fw_unit *unit, |
| 88 | enum cip_out_flags flags); |
| 89 | void amdtp_out_stream_destroy(struct amdtp_out_stream *s); |
| 90 | |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 91 | void amdtp_out_stream_set_parameters(struct amdtp_out_stream *s, |
| 92 | unsigned int rate, |
| 93 | unsigned int pcm_channels, |
| 94 | unsigned int midi_ports); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 95 | unsigned int amdtp_out_stream_get_max_payload(struct amdtp_out_stream *s); |
| 96 | |
| 97 | int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed); |
| 98 | void amdtp_out_stream_update(struct amdtp_out_stream *s); |
| 99 | void amdtp_out_stream_stop(struct amdtp_out_stream *s); |
| 100 | |
| 101 | void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s, |
| 102 | snd_pcm_format_t format); |
Clemens Ladisch | 76fb878 | 2012-05-13 22:03:09 +0200 | [diff] [blame] | 103 | void amdtp_out_stream_pcm_prepare(struct amdtp_out_stream *s); |
Clemens Ladisch | e9148dd | 2012-05-13 18:49:14 +0200 | [diff] [blame] | 104 | unsigned long amdtp_out_stream_pcm_pointer(struct amdtp_out_stream *s); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 105 | void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s); |
| 106 | |
Clemens Ladisch | c5280e9 | 2011-10-16 21:39:00 +0200 | [diff] [blame] | 107 | extern const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT]; |
Clemens Ladisch | a7304e3 | 2011-09-04 22:16:10 +0200 | [diff] [blame] | 108 | |
Clemens Ladisch | 20b65dd | 2011-09-04 22:15:44 +0200 | [diff] [blame] | 109 | static inline bool amdtp_out_stream_running(struct amdtp_out_stream *s) |
| 110 | { |
| 111 | return !IS_ERR(s->context); |
| 112 | } |
| 113 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 114 | /** |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 115 | * amdtp_out_streaming_error - check for streaming error |
| 116 | * @s: the AMDTP output stream |
| 117 | * |
| 118 | * If this function returns true, the stream's packet queue has stopped due to |
| 119 | * an asynchronous error. |
| 120 | */ |
| 121 | static inline bool amdtp_out_streaming_error(struct amdtp_out_stream *s) |
| 122 | { |
| 123 | return s->packet_index < 0; |
| 124 | } |
| 125 | |
| 126 | /** |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 127 | * amdtp_out_stream_pcm_trigger - start/stop playback from a PCM device |
| 128 | * @s: the AMDTP output stream |
| 129 | * @pcm: the PCM device to be started, or %NULL to stop the current device |
| 130 | * |
| 131 | * Call this function on a running isochronous stream to enable the actual |
| 132 | * transmission of PCM data. This function should be called from the PCM |
| 133 | * device's .trigger callback. |
| 134 | */ |
| 135 | static inline void amdtp_out_stream_pcm_trigger(struct amdtp_out_stream *s, |
| 136 | struct snd_pcm_substream *pcm) |
| 137 | { |
| 138 | ACCESS_ONCE(s->pcm) = pcm; |
| 139 | } |
| 140 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 141 | static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc) |
| 142 | { |
| 143 | return sfc & 1; |
| 144 | } |
| 145 | |
| 146 | #endif |