amdtp-stream.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 <linux/sched.h>
  7. #include <sound/asound.h>
  8. #include "packets-buffer.h"
  9. /**
  10. * enum cip_flags - describes details of the streaming protocol
  11. * @CIP_NONBLOCKING: In non-blocking mode, each packet contains
  12. * sample_rate/8000 samples, with rounding up or down to adjust
  13. * for clock skew and left-over fractional samples. This should
  14. * be used if supported by the device.
  15. * @CIP_BLOCKING: In blocking mode, each packet contains either zero or
  16. * SYT_INTERVAL samples, with these two types alternating so that
  17. * the overall sample rate comes out right.
  18. * @CIP_EMPTY_WITH_TAG0: Only for in-stream. Empty in-packets have TAG0.
  19. * @CIP_DBC_IS_END_EVENT: Only for in-stream. The value of dbc in an in-packet
  20. * corresponds to the end of event in the packet. Out of IEC 61883.
  21. * @CIP_WRONG_DBS: Only for in-stream. The value of dbs is wrong in in-packets.
  22. * The value of data_block_quadlets is used instead of reported value.
  23. * @CIP_SKIP_DBC_ZERO_CHECK: Only for in-stream. Packets with zero in dbc is
  24. * skipped for detecting discontinuity.
  25. * @CIP_EMPTY_HAS_WRONG_DBC: Only for in-stream. The value of dbc in empty
  26. * packet is wrong but the others are correct.
  27. * @CIP_JUMBO_PAYLOAD: Only for in-stream. The number of data blocks in an
  28. * packet is larger than IEC 61883-6 defines. Current implementation
  29. * allows 5 times as large as IEC 61883-6 defines.
  30. */
  31. enum cip_flags {
  32. CIP_NONBLOCKING = 0x00,
  33. CIP_BLOCKING = 0x01,
  34. CIP_EMPTY_WITH_TAG0 = 0x02,
  35. CIP_DBC_IS_END_EVENT = 0x04,
  36. CIP_WRONG_DBS = 0x08,
  37. CIP_SKIP_DBC_ZERO_CHECK = 0x10,
  38. CIP_EMPTY_HAS_WRONG_DBC = 0x20,
  39. CIP_JUMBO_PAYLOAD = 0x40,
  40. };
  41. /**
  42. * enum cip_sfc - supported Sampling Frequency Codes (SFCs)
  43. * @CIP_SFC_32000: 32,000 data blocks
  44. * @CIP_SFC_44100: 44,100 data blocks
  45. * @CIP_SFC_48000: 48,000 data blocks
  46. * @CIP_SFC_88200: 88,200 data blocks
  47. * @CIP_SFC_96000: 96,000 data blocks
  48. * @CIP_SFC_176400: 176,400 data blocks
  49. * @CIP_SFC_192000: 192,000 data blocks
  50. * @CIP_SFC_COUNT: the number of supported SFCs
  51. *
  52. * These values are used to show nominal Sampling Frequency Code in
  53. * Format Dependent Field (FDF) of AMDTP packet header. In IEC 61883-6:2002,
  54. * this code means the number of events per second. Actually the code
  55. * represents the number of data blocks transferred per second in an AMDTP
  56. * stream.
  57. *
  58. * In IEC 61883-6:2005, some extensions were added to support more types of
  59. * data such as 'One Bit LInear Audio', therefore the meaning of SFC became
  60. * different depending on the types.
  61. *
  62. * Currently our implementation is compatible with IEC 61883-6:2002.
  63. */
  64. enum cip_sfc {
  65. CIP_SFC_32000 = 0,
  66. CIP_SFC_44100 = 1,
  67. CIP_SFC_48000 = 2,
  68. CIP_SFC_88200 = 3,
  69. CIP_SFC_96000 = 4,
  70. CIP_SFC_176400 = 5,
  71. CIP_SFC_192000 = 6,
  72. CIP_SFC_COUNT
  73. };
  74. struct fw_unit;
  75. struct fw_iso_context;
  76. struct snd_pcm_substream;
  77. struct snd_pcm_runtime;
  78. enum amdtp_stream_direction {
  79. AMDTP_OUT_STREAM = 0,
  80. AMDTP_IN_STREAM
  81. };
  82. struct amdtp_stream;
  83. typedef unsigned int (*amdtp_stream_process_data_blocks_t)(
  84. struct amdtp_stream *s,
  85. __be32 *buffer,
  86. unsigned int data_blocks,
  87. unsigned int *syt);
  88. struct amdtp_stream {
  89. struct fw_unit *unit;
  90. enum cip_flags flags;
  91. enum amdtp_stream_direction direction;
  92. struct mutex mutex;
  93. /* For packet processing. */
  94. struct fw_iso_context *context;
  95. struct iso_packets_buffer buffer;
  96. int packet_index;
  97. /* For CIP headers. */
  98. unsigned int source_node_id_field;
  99. unsigned int data_block_quadlets;
  100. unsigned int data_block_counter;
  101. unsigned int fmt;
  102. unsigned int fdf;
  103. /* quirk: fixed interval of dbc between previos/current packets. */
  104. unsigned int tx_dbc_interval;
  105. /* quirk: indicate the value of dbc field in a first packet. */
  106. unsigned int tx_first_dbc;
  107. /* Internal flags. */
  108. enum cip_sfc sfc;
  109. unsigned int syt_interval;
  110. unsigned int transfer_delay;
  111. unsigned int data_block_state;
  112. unsigned int last_syt_offset;
  113. unsigned int syt_offset_state;
  114. /* For a PCM substream processing. */
  115. struct snd_pcm_substream *pcm;
  116. struct tasklet_struct period_tasklet;
  117. snd_pcm_uframes_t pcm_buffer_pointer;
  118. unsigned int pcm_period_pointer;
  119. /* To wait for first packet. */
  120. bool callbacked;
  121. wait_queue_head_t callback_wait;
  122. /* For backends to process data blocks. */
  123. void *protocol;
  124. amdtp_stream_process_data_blocks_t process_data_blocks;
  125. };
  126. int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
  127. enum amdtp_stream_direction dir, enum cip_flags flags,
  128. unsigned int fmt,
  129. amdtp_stream_process_data_blocks_t process_data_blocks,
  130. unsigned int protocol_size);
  131. void amdtp_stream_destroy(struct amdtp_stream *s);
  132. int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
  133. unsigned int data_block_quadlets);
  134. unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s);
  135. int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed);
  136. void amdtp_stream_update(struct amdtp_stream *s);
  137. void amdtp_stream_stop(struct amdtp_stream *s);
  138. int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
  139. struct snd_pcm_runtime *runtime);
  140. void amdtp_stream_pcm_prepare(struct amdtp_stream *s);
  141. unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s);
  142. void amdtp_stream_pcm_abort(struct amdtp_stream *s);
  143. extern const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT];
  144. extern const unsigned int amdtp_rate_table[CIP_SFC_COUNT];
  145. /**
  146. * amdtp_stream_running - check stream is running or not
  147. * @s: the AMDTP stream
  148. *
  149. * If this function returns true, the stream is running.
  150. */
  151. static inline bool amdtp_stream_running(struct amdtp_stream *s)
  152. {
  153. return !IS_ERR(s->context);
  154. }
  155. /**
  156. * amdtp_streaming_error - check for streaming error
  157. * @s: the AMDTP stream
  158. *
  159. * If this function returns true, the stream's packet queue has stopped due to
  160. * an asynchronous error.
  161. */
  162. static inline bool amdtp_streaming_error(struct amdtp_stream *s)
  163. {
  164. return s->packet_index < 0;
  165. }
  166. /**
  167. * amdtp_stream_pcm_running - check PCM substream is running or not
  168. * @s: the AMDTP stream
  169. *
  170. * If this function returns true, PCM substream in the AMDTP stream is running.
  171. */
  172. static inline bool amdtp_stream_pcm_running(struct amdtp_stream *s)
  173. {
  174. return !!s->pcm;
  175. }
  176. /**
  177. * amdtp_stream_pcm_trigger - start/stop playback from a PCM device
  178. * @s: the AMDTP stream
  179. * @pcm: the PCM device to be started, or %NULL to stop the current device
  180. *
  181. * Call this function on a running isochronous stream to enable the actual
  182. * transmission of PCM data. This function should be called from the PCM
  183. * device's .trigger callback.
  184. */
  185. static inline void amdtp_stream_pcm_trigger(struct amdtp_stream *s,
  186. struct snd_pcm_substream *pcm)
  187. {
  188. ACCESS_ONCE(s->pcm) = pcm;
  189. }
  190. static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)
  191. {
  192. return sfc & 1;
  193. }
  194. /**
  195. * amdtp_stream_wait_callback - sleep till callbacked or timeout
  196. * @s: the AMDTP stream
  197. * @timeout: msec till timeout
  198. *
  199. * If this function return false, the AMDTP stream should be stopped.
  200. */
  201. static inline bool amdtp_stream_wait_callback(struct amdtp_stream *s,
  202. unsigned int timeout)
  203. {
  204. return wait_event_timeout(s->callback_wait,
  205. s->callbacked == true,
  206. msecs_to_jiffies(timeout)) > 0;
  207. }
  208. #endif