amdtp-stream.h 7.3 KB

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