amdtp.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. * @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 - a stream's sample rate
  44. */
  45. enum cip_sfc {
  46. CIP_SFC_32000 = 0,
  47. CIP_SFC_44100 = 1,
  48. CIP_SFC_48000 = 2,
  49. CIP_SFC_88200 = 3,
  50. CIP_SFC_96000 = 4,
  51. CIP_SFC_176400 = 5,
  52. CIP_SFC_192000 = 6,
  53. CIP_SFC_COUNT
  54. };
  55. #define AMDTP_IN_PCM_FORMAT_BITS SNDRV_PCM_FMTBIT_S32
  56. #define AMDTP_OUT_PCM_FORMAT_BITS (SNDRV_PCM_FMTBIT_S16 | \
  57. SNDRV_PCM_FMTBIT_S32)
  58. /*
  59. * This module supports maximum 64 PCM channels for one PCM stream
  60. * This is for our convenience.
  61. */
  62. #define AMDTP_MAX_CHANNELS_FOR_PCM 64
  63. /*
  64. * AMDTP packet can include channels for MIDI conformant data.
  65. * Each MIDI conformant data channel includes 8 MPX-MIDI data stream.
  66. * Each MPX-MIDI data stream includes one data stream from/to MIDI ports.
  67. *
  68. * This module supports maximum 1 MIDI conformant data channels.
  69. * Then this AMDTP packets can transfer maximum 8 MIDI data streams.
  70. */
  71. #define AMDTP_MAX_CHANNELS_FOR_MIDI 1
  72. struct fw_unit;
  73. struct fw_iso_context;
  74. struct snd_pcm_substream;
  75. struct snd_pcm_runtime;
  76. struct snd_rawmidi_substream;
  77. enum amdtp_stream_direction {
  78. AMDTP_OUT_STREAM = 0,
  79. AMDTP_IN_STREAM
  80. };
  81. struct amdtp_stream {
  82. struct fw_unit *unit;
  83. enum cip_flags flags;
  84. enum amdtp_stream_direction direction;
  85. struct fw_iso_context *context;
  86. struct mutex mutex;
  87. enum cip_sfc sfc;
  88. unsigned int data_block_quadlets;
  89. unsigned int pcm_channels;
  90. unsigned int midi_ports;
  91. void (*transfer_samples)(struct amdtp_stream *s,
  92. struct snd_pcm_substream *pcm,
  93. __be32 *buffer, unsigned int frames);
  94. u8 pcm_positions[AMDTP_MAX_CHANNELS_FOR_PCM];
  95. u8 midi_position;
  96. unsigned int syt_interval;
  97. unsigned int transfer_delay;
  98. unsigned int source_node_id_field;
  99. struct iso_packets_buffer buffer;
  100. struct snd_pcm_substream *pcm;
  101. struct tasklet_struct period_tasklet;
  102. int packet_index;
  103. unsigned int data_block_counter;
  104. unsigned int data_block_state;
  105. unsigned int last_syt_offset;
  106. unsigned int syt_offset_state;
  107. unsigned int pcm_buffer_pointer;
  108. unsigned int pcm_period_pointer;
  109. bool pointer_flush;
  110. struct snd_rawmidi_substream *midi[AMDTP_MAX_CHANNELS_FOR_MIDI * 8];
  111. /* quirk: fixed interval of dbc between previos/current packets. */
  112. unsigned int tx_dbc_interval;
  113. /* quirk: the first count of data blocks in an rx packet for MIDI */
  114. unsigned int rx_blocks_for_midi;
  115. bool callbacked;
  116. wait_queue_head_t callback_wait;
  117. struct amdtp_stream *sync_slave;
  118. };
  119. int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
  120. enum amdtp_stream_direction dir,
  121. enum cip_flags flags);
  122. void amdtp_stream_destroy(struct amdtp_stream *s);
  123. void amdtp_stream_set_parameters(struct amdtp_stream *s,
  124. unsigned int rate,
  125. unsigned int pcm_channels,
  126. unsigned int midi_ports);
  127. unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s);
  128. int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed);
  129. void amdtp_stream_update(struct amdtp_stream *s);
  130. void amdtp_stream_stop(struct amdtp_stream *s);
  131. int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
  132. struct snd_pcm_runtime *runtime);
  133. void amdtp_stream_set_pcm_format(struct amdtp_stream *s,
  134. snd_pcm_format_t format);
  135. void amdtp_stream_pcm_prepare(struct amdtp_stream *s);
  136. unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s);
  137. void amdtp_stream_pcm_abort(struct amdtp_stream *s);
  138. extern const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT];
  139. extern const unsigned int amdtp_rate_table[CIP_SFC_COUNT];
  140. /**
  141. * amdtp_stream_running - check stream is running or not
  142. * @s: the AMDTP stream
  143. *
  144. * If this function returns true, the stream is running.
  145. */
  146. static inline bool amdtp_stream_running(struct amdtp_stream *s)
  147. {
  148. return !IS_ERR(s->context);
  149. }
  150. /**
  151. * amdtp_streaming_error - check for streaming error
  152. * @s: the AMDTP stream
  153. *
  154. * If this function returns true, the stream's packet queue has stopped due to
  155. * an asynchronous error.
  156. */
  157. static inline bool amdtp_streaming_error(struct amdtp_stream *s)
  158. {
  159. return s->packet_index < 0;
  160. }
  161. /**
  162. * amdtp_stream_pcm_running - check PCM substream is running or not
  163. * @s: the AMDTP stream
  164. *
  165. * If this function returns true, PCM substream in the AMDTP stream is running.
  166. */
  167. static inline bool amdtp_stream_pcm_running(struct amdtp_stream *s)
  168. {
  169. return !!s->pcm;
  170. }
  171. /**
  172. * amdtp_stream_pcm_trigger - start/stop playback from a PCM device
  173. * @s: the AMDTP stream
  174. * @pcm: the PCM device to be started, or %NULL to stop the current device
  175. *
  176. * Call this function on a running isochronous stream to enable the actual
  177. * transmission of PCM data. This function should be called from the PCM
  178. * device's .trigger callback.
  179. */
  180. static inline void amdtp_stream_pcm_trigger(struct amdtp_stream *s,
  181. struct snd_pcm_substream *pcm)
  182. {
  183. ACCESS_ONCE(s->pcm) = pcm;
  184. }
  185. /**
  186. * amdtp_stream_midi_trigger - start/stop playback/capture with a MIDI device
  187. * @s: the AMDTP stream
  188. * @port: index of MIDI port
  189. * @midi: the MIDI device to be started, or %NULL to stop the current device
  190. *
  191. * Call this function on a running isochronous stream to enable the actual
  192. * transmission of MIDI data. This function should be called from the MIDI
  193. * device's .trigger callback.
  194. */
  195. static inline void amdtp_stream_midi_trigger(struct amdtp_stream *s,
  196. unsigned int port,
  197. struct snd_rawmidi_substream *midi)
  198. {
  199. if (port < s->midi_ports)
  200. ACCESS_ONCE(s->midi[port]) = midi;
  201. }
  202. static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)
  203. {
  204. return sfc & 1;
  205. }
  206. static inline void amdtp_stream_set_sync(enum cip_flags sync_mode,
  207. struct amdtp_stream *master,
  208. struct amdtp_stream *slave)
  209. {
  210. if (sync_mode == CIP_SYNC_TO_DEVICE) {
  211. master->flags |= CIP_SYNC_TO_DEVICE;
  212. slave->flags |= CIP_SYNC_TO_DEVICE;
  213. master->sync_slave = slave;
  214. } else {
  215. master->flags &= ~CIP_SYNC_TO_DEVICE;
  216. slave->flags &= ~CIP_SYNC_TO_DEVICE;
  217. master->sync_slave = NULL;
  218. }
  219. slave->sync_slave = NULL;
  220. }
  221. /**
  222. * amdtp_stream_wait_callback - sleep till callbacked or timeout
  223. * @s: the AMDTP stream
  224. * @timeout: msec till timeout
  225. *
  226. * If this function return false, the AMDTP stream should be stopped.
  227. */
  228. static inline bool amdtp_stream_wait_callback(struct amdtp_stream *s,
  229. unsigned int timeout)
  230. {
  231. return wait_event_timeout(s->callback_wait,
  232. s->callbacked == true,
  233. msecs_to_jiffies(timeout)) > 0;
  234. }
  235. #endif