amdtp.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #ifndef SOUND_FIREWIRE_AMDTP_H_INCLUDED
  2. #define SOUND_FIREWIRE_AMDTP_H_INCLUDED
  3. #include <linux/err.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/mutex.h>
  6. #include <sound/asound.h>
  7. #include "packets-buffer.h"
  8. /**
  9. * enum cip_flags - describes details of the streaming protocol
  10. * @CIP_NONBLOCKING: In non-blocking mode, each packet contains
  11. * sample_rate/8000 samples, with rounding up or down to adjust
  12. * for clock skew and left-over fractional samples. This should
  13. * be used if supported by the device.
  14. * @CIP_BLOCKING: In blocking mode, each packet contains either zero or
  15. * SYT_INTERVAL samples, with these two types alternating so that
  16. * the overall sample rate comes out right.
  17. * @CIP_SYNC_TO_DEVICE: In sync to device mode, time stamp in out packets is
  18. * generated by in packets. Defaultly this driver generates timestamp.
  19. * @CIP_EMPTY_WITH_TAG0: Only for in-stream. Empty in-packets have TAG0.
  20. * @CIP_DBC_IS_END_EVENT: Only for in-stream. The value of dbc in an in-packet
  21. * corresponds to the end of event in the packet. Out of IEC 61883.
  22. * @CIP_WRONG_DBS: Only for in-stream. The value of dbs is wrong in in-packets.
  23. * The value of data_block_quadlets is used instead of reported value.
  24. * @CIP_SKIP_DBC_ZERO_CHECK: Only for in-stream. Packets with zero in dbc is
  25. * skipped for detecting discontinuity.
  26. * @CIP_SKIP_INIT_DBC_CHECK: Only for in-stream. The value of dbc in first
  27. * packet is not continuous from an initial value.
  28. * @CIP_EMPTY_HAS_WRONG_DBC: Only for in-stream. The value of dbc in empty
  29. * packet is wrong but the others are correct.
  30. */
  31. enum cip_flags {
  32. CIP_NONBLOCKING = 0x00,
  33. CIP_BLOCKING = 0x01,
  34. CIP_SYNC_TO_DEVICE = 0x02,
  35. CIP_EMPTY_WITH_TAG0 = 0x04,
  36. CIP_DBC_IS_END_EVENT = 0x08,
  37. CIP_WRONG_DBS = 0x10,
  38. CIP_SKIP_DBC_ZERO_CHECK = 0x20,
  39. CIP_SKIP_INIT_DBC_CHECK = 0x40,
  40. CIP_EMPTY_HAS_WRONG_DBC = 0x80,
  41. };
  42. /**
  43. * enum cip_sfc - supported Sampling Frequency Codes (SFCs)
  44. * @CIP_SFC_32000: 32,000 data blocks
  45. * @CIP_SFC_44100: 44,100 data blocks
  46. * @CIP_SFC_48000: 48,000 data blocks
  47. * @CIP_SFC_88200: 88,200 data blocks
  48. * @CIP_SFC_96000: 96,000 data blocks
  49. * @CIP_SFC_176400: 176,400 data blocks
  50. * @CIP_SFC_192000: 192,000 data blocks
  51. * @CIP_SFC_COUNT: the number of supported SFCs
  52. *
  53. * These values are used to show nominal Sampling Frequency Code in
  54. * Format Dependent Field (FDF) of AMDTP packet header. In IEC 61883-6:2002,
  55. * this code means the number of events per second. Actually the code
  56. * represents the number of data blocks transferred per second in an AMDTP
  57. * stream.
  58. *
  59. * In IEC 61883-6:2005, some extensions were added to support more types of
  60. * data such as 'One Bit LInear Audio', therefore the meaning of SFC became
  61. * different depending on the types.
  62. *
  63. * Currently our implementation is compatible with IEC 61883-6:2002.
  64. */
  65. enum cip_sfc {
  66. CIP_SFC_32000 = 0,
  67. CIP_SFC_44100 = 1,
  68. CIP_SFC_48000 = 2,
  69. CIP_SFC_88200 = 3,
  70. CIP_SFC_96000 = 4,
  71. CIP_SFC_176400 = 5,
  72. CIP_SFC_192000 = 6,
  73. CIP_SFC_COUNT
  74. };
  75. #define AMDTP_IN_PCM_FORMAT_BITS SNDRV_PCM_FMTBIT_S32
  76. #define AMDTP_OUT_PCM_FORMAT_BITS (SNDRV_PCM_FMTBIT_S16 | \
  77. SNDRV_PCM_FMTBIT_S32)
  78. /*
  79. * This module supports maximum 64 PCM channels for one PCM stream
  80. * This is for our convenience.
  81. */
  82. #define AMDTP_MAX_CHANNELS_FOR_PCM 64
  83. /*
  84. * AMDTP packet can include channels for MIDI conformant data.
  85. * Each MIDI conformant data channel includes 8 MPX-MIDI data stream.
  86. * Each MPX-MIDI data stream includes one data stream from/to MIDI ports.
  87. *
  88. * This module supports maximum 1 MIDI conformant data channels.
  89. * Then this AMDTP packets can transfer maximum 8 MIDI data streams.
  90. */
  91. #define AMDTP_MAX_CHANNELS_FOR_MIDI 1
  92. struct fw_unit;
  93. struct fw_iso_context;
  94. struct snd_pcm_substream;
  95. struct snd_pcm_runtime;
  96. struct snd_rawmidi_substream;
  97. enum amdtp_stream_direction {
  98. AMDTP_OUT_STREAM = 0,
  99. AMDTP_IN_STREAM
  100. };
  101. struct amdtp_stream {
  102. struct fw_unit *unit;
  103. enum cip_flags flags;
  104. enum amdtp_stream_direction direction;
  105. struct fw_iso_context *context;
  106. struct mutex mutex;
  107. enum cip_sfc sfc;
  108. unsigned int data_block_quadlets;
  109. unsigned int pcm_channels;
  110. unsigned int midi_ports;
  111. void (*transfer_samples)(struct amdtp_stream *s,
  112. struct snd_pcm_substream *pcm,
  113. __be32 *buffer, unsigned int frames);
  114. u8 pcm_positions[AMDTP_MAX_CHANNELS_FOR_PCM];
  115. u8 midi_position;
  116. unsigned int syt_interval;
  117. unsigned int transfer_delay;
  118. unsigned int source_node_id_field;
  119. struct iso_packets_buffer buffer;
  120. struct snd_pcm_substream *pcm;
  121. struct tasklet_struct period_tasklet;
  122. int packet_index;
  123. unsigned int data_block_counter;
  124. unsigned int data_block_state;
  125. unsigned int last_syt_offset;
  126. unsigned int syt_offset_state;
  127. unsigned int pcm_buffer_pointer;
  128. unsigned int pcm_period_pointer;
  129. bool pointer_flush;
  130. bool double_pcm_frames;
  131. struct snd_rawmidi_substream *midi[AMDTP_MAX_CHANNELS_FOR_MIDI * 8];
  132. int midi_fifo_limit;
  133. int midi_fifo_used[AMDTP_MAX_CHANNELS_FOR_MIDI * 8];
  134. /* quirk: fixed interval of dbc between previos/current packets. */
  135. unsigned int tx_dbc_interval;
  136. bool callbacked;
  137. wait_queue_head_t callback_wait;
  138. struct amdtp_stream *sync_slave;
  139. };
  140. int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
  141. enum amdtp_stream_direction dir,
  142. enum cip_flags flags);
  143. void amdtp_stream_destroy(struct amdtp_stream *s);
  144. void amdtp_stream_set_parameters(struct amdtp_stream *s,
  145. unsigned int rate,
  146. unsigned int pcm_channels,
  147. unsigned int midi_ports);
  148. unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s);
  149. int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed);
  150. void amdtp_stream_update(struct amdtp_stream *s);
  151. void amdtp_stream_stop(struct amdtp_stream *s);
  152. int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
  153. struct snd_pcm_runtime *runtime);
  154. void amdtp_stream_set_pcm_format(struct amdtp_stream *s,
  155. snd_pcm_format_t format);
  156. void amdtp_stream_pcm_prepare(struct amdtp_stream *s);
  157. unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s);
  158. void amdtp_stream_pcm_abort(struct amdtp_stream *s);
  159. extern const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT];
  160. extern const unsigned int amdtp_rate_table[CIP_SFC_COUNT];
  161. /**
  162. * amdtp_stream_running - check stream is running or not
  163. * @s: the AMDTP stream
  164. *
  165. * If this function returns true, the stream is running.
  166. */
  167. static inline bool amdtp_stream_running(struct amdtp_stream *s)
  168. {
  169. return !IS_ERR(s->context);
  170. }
  171. /**
  172. * amdtp_streaming_error - check for streaming error
  173. * @s: the AMDTP stream
  174. *
  175. * If this function returns true, the stream's packet queue has stopped due to
  176. * an asynchronous error.
  177. */
  178. static inline bool amdtp_streaming_error(struct amdtp_stream *s)
  179. {
  180. return s->packet_index < 0;
  181. }
  182. /**
  183. * amdtp_stream_pcm_running - check PCM substream is running or not
  184. * @s: the AMDTP stream
  185. *
  186. * If this function returns true, PCM substream in the AMDTP stream is running.
  187. */
  188. static inline bool amdtp_stream_pcm_running(struct amdtp_stream *s)
  189. {
  190. return !!s->pcm;
  191. }
  192. /**
  193. * amdtp_stream_pcm_trigger - start/stop playback from a PCM device
  194. * @s: the AMDTP stream
  195. * @pcm: the PCM device to be started, or %NULL to stop the current device
  196. *
  197. * Call this function on a running isochronous stream to enable the actual
  198. * transmission of PCM data. This function should be called from the PCM
  199. * device's .trigger callback.
  200. */
  201. static inline void amdtp_stream_pcm_trigger(struct amdtp_stream *s,
  202. struct snd_pcm_substream *pcm)
  203. {
  204. ACCESS_ONCE(s->pcm) = pcm;
  205. }
  206. /**
  207. * amdtp_stream_midi_trigger - start/stop playback/capture with a MIDI device
  208. * @s: the AMDTP stream
  209. * @port: index of MIDI port
  210. * @midi: the MIDI device to be started, or %NULL to stop the current device
  211. *
  212. * Call this function on a running isochronous stream to enable the actual
  213. * transmission of MIDI data. This function should be called from the MIDI
  214. * device's .trigger callback.
  215. */
  216. static inline void amdtp_stream_midi_trigger(struct amdtp_stream *s,
  217. unsigned int port,
  218. struct snd_rawmidi_substream *midi)
  219. {
  220. if (port < s->midi_ports)
  221. ACCESS_ONCE(s->midi[port]) = midi;
  222. }
  223. static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)
  224. {
  225. return sfc & 1;
  226. }
  227. static inline void amdtp_stream_set_sync(enum cip_flags sync_mode,
  228. struct amdtp_stream *master,
  229. struct amdtp_stream *slave)
  230. {
  231. if (sync_mode == CIP_SYNC_TO_DEVICE) {
  232. master->flags |= CIP_SYNC_TO_DEVICE;
  233. slave->flags |= CIP_SYNC_TO_DEVICE;
  234. master->sync_slave = slave;
  235. } else {
  236. master->flags &= ~CIP_SYNC_TO_DEVICE;
  237. slave->flags &= ~CIP_SYNC_TO_DEVICE;
  238. master->sync_slave = NULL;
  239. }
  240. slave->sync_slave = NULL;
  241. }
  242. /**
  243. * amdtp_stream_wait_callback - sleep till callbacked or timeout
  244. * @s: the AMDTP stream
  245. * @timeout: msec till timeout
  246. *
  247. * If this function return false, the AMDTP stream should be stopped.
  248. */
  249. static inline bool amdtp_stream_wait_callback(struct amdtp_stream *s,
  250. unsigned int timeout)
  251. {
  252. return wait_event_timeout(s->callback_wait,
  253. s->callbacked == true,
  254. msecs_to_jiffies(timeout)) > 0;
  255. }
  256. #endif