amdtp.h 9.1 KB

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