amdtp-stream.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef SOUND_FIREWIRE_AMDTP_H_INCLUDED
  3. #define SOUND_FIREWIRE_AMDTP_H_INCLUDED
  4. #include <linux/err.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/mutex.h>
  7. #include <linux/sched.h>
  8. #include <sound/asound.h>
  9. #include "packets-buffer.h"
  10. /**
  11. * enum cip_flags - describes details of the streaming protocol
  12. * @CIP_NONBLOCKING: In non-blocking mode, each packet contains
  13. * sample_rate/8000 samples, with rounding up or down to adjust
  14. * for clock skew and left-over fractional samples. This should
  15. * be used if supported by the device.
  16. * @CIP_BLOCKING: In blocking mode, each packet contains either zero or
  17. * SYT_INTERVAL samples, with these two types alternating so that
  18. * the overall sample rate comes out right.
  19. * @CIP_EMPTY_WITH_TAG0: Only for in-stream. Empty in-packets have TAG0.
  20. * @CIP_DBC_IS_END_EVENT: The value of dbc in an packet corresponds to the end
  21. * 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_EMPTY_HAS_WRONG_DBC: Only for in-stream. The value of dbc in empty
  27. * packet is wrong but the others are correct.
  28. * @CIP_JUMBO_PAYLOAD: Only for in-stream. The number of data blocks in an
  29. * packet is larger than IEC 61883-6 defines. Current implementation
  30. * allows 5 times as large as IEC 61883-6 defines.
  31. * @CIP_HEADER_WITHOUT_EOH: Only for in-stream. CIP Header doesn't include
  32. * valid EOH.
  33. * @CIP_NO_HEADERS: a lack of headers in packets
  34. * @CIP_UNALIGHED_DBC: Only for in-stream. The value of dbc is not alighed to
  35. * the value of current SYT_INTERVAL; e.g. initial value is not zero.
  36. * @CIP_UNAWARE_SYT: For outgoing packet, the value in SYT field of CIP is 0xffff.
  37. * For incoming packet, the value in SYT field of CIP is not handled.
  38. * @CIP_DBC_IS_PAYLOAD_QUADLETS: Available for incoming packet, and only effective with
  39. * CIP_DBC_IS_END_EVENT flag. The value of dbc field is the number of accumulated quadlets
  40. * in CIP payload, instead of the number of accumulated data blocks.
  41. */
  42. enum cip_flags {
  43. CIP_NONBLOCKING = 0x00,
  44. CIP_BLOCKING = 0x01,
  45. CIP_EMPTY_WITH_TAG0 = 0x02,
  46. CIP_DBC_IS_END_EVENT = 0x04,
  47. CIP_WRONG_DBS = 0x08,
  48. CIP_SKIP_DBC_ZERO_CHECK = 0x10,
  49. CIP_EMPTY_HAS_WRONG_DBC = 0x20,
  50. CIP_JUMBO_PAYLOAD = 0x40,
  51. CIP_HEADER_WITHOUT_EOH = 0x80,
  52. CIP_NO_HEADER = 0x100,
  53. CIP_UNALIGHED_DBC = 0x200,
  54. CIP_UNAWARE_SYT = 0x400,
  55. CIP_DBC_IS_PAYLOAD_QUADLETS = 0x800,
  56. };
  57. /**
  58. * enum cip_sfc - supported Sampling Frequency Codes (SFCs)
  59. * @CIP_SFC_32000: 32,000 data blocks
  60. * @CIP_SFC_44100: 44,100 data blocks
  61. * @CIP_SFC_48000: 48,000 data blocks
  62. * @CIP_SFC_88200: 88,200 data blocks
  63. * @CIP_SFC_96000: 96,000 data blocks
  64. * @CIP_SFC_176400: 176,400 data blocks
  65. * @CIP_SFC_192000: 192,000 data blocks
  66. * @CIP_SFC_COUNT: the number of supported SFCs
  67. *
  68. * These values are used to show nominal Sampling Frequency Code in
  69. * Format Dependent Field (FDF) of AMDTP packet header. In IEC 61883-6:2002,
  70. * this code means the number of events per second. Actually the code
  71. * represents the number of data blocks transferred per second in an AMDTP
  72. * stream.
  73. *
  74. * In IEC 61883-6:2005, some extensions were added to support more types of
  75. * data such as 'One Bit LInear Audio', therefore the meaning of SFC became
  76. * different depending on the types.
  77. *
  78. * Currently our implementation is compatible with IEC 61883-6:2002.
  79. */
  80. enum cip_sfc {
  81. CIP_SFC_32000 = 0,
  82. CIP_SFC_44100 = 1,
  83. CIP_SFC_48000 = 2,
  84. CIP_SFC_88200 = 3,
  85. CIP_SFC_96000 = 4,
  86. CIP_SFC_176400 = 5,
  87. CIP_SFC_192000 = 6,
  88. CIP_SFC_COUNT
  89. };
  90. struct fw_unit;
  91. struct fw_iso_context;
  92. struct snd_pcm_substream;
  93. struct snd_pcm_runtime;
  94. enum amdtp_stream_direction {
  95. AMDTP_OUT_STREAM = 0,
  96. AMDTP_IN_STREAM
  97. };
  98. struct pkt_desc {
  99. u32 cycle;
  100. u32 syt;
  101. unsigned int data_blocks;
  102. unsigned int data_block_counter;
  103. __be32 *ctx_payload;
  104. struct list_head link;
  105. };
  106. struct amdtp_stream;
  107. typedef void (*amdtp_stream_process_ctx_payloads_t)(struct amdtp_stream *s,
  108. const struct pkt_desc *desc,
  109. unsigned int count,
  110. struct snd_pcm_substream *pcm);
  111. struct amdtp_domain;
  112. struct amdtp_stream {
  113. struct fw_unit *unit;
  114. // The combination of cip_flags enumeration-constants.
  115. unsigned int flags;
  116. enum amdtp_stream_direction direction;
  117. struct mutex mutex;
  118. /* For packet processing. */
  119. struct fw_iso_context *context;
  120. struct iso_packets_buffer buffer;
  121. unsigned int queue_size;
  122. int packet_index;
  123. struct pkt_desc *packet_descs;
  124. struct list_head packet_descs_list;
  125. struct pkt_desc *packet_descs_cursor;
  126. int tag;
  127. union {
  128. struct {
  129. unsigned int ctx_header_size;
  130. // limit for payload of iso packet.
  131. unsigned int max_ctx_payload_length;
  132. // For quirks of CIP headers.
  133. // Fixed interval of dbc between previos/current
  134. // packets.
  135. unsigned int dbc_interval;
  136. // The device starts multiplexing events to the packet.
  137. bool event_starts;
  138. struct {
  139. struct seq_desc *descs;
  140. unsigned int size;
  141. unsigned int pos;
  142. } cache;
  143. } tx;
  144. struct {
  145. // To generate CIP header.
  146. unsigned int fdf;
  147. // To generate constant hardware IRQ.
  148. unsigned int event_count;
  149. // To calculate CIP data blocks and tstamp.
  150. struct {
  151. struct seq_desc *descs;
  152. unsigned int size;
  153. unsigned int pos;
  154. } seq;
  155. unsigned int data_block_state;
  156. unsigned int syt_offset_state;
  157. unsigned int last_syt_offset;
  158. struct amdtp_stream *replay_target;
  159. unsigned int cache_pos;
  160. } rx;
  161. } ctx_data;
  162. /* For CIP headers. */
  163. unsigned int source_node_id_field;
  164. unsigned int data_block_quadlets;
  165. unsigned int data_block_counter;
  166. unsigned int sph;
  167. unsigned int fmt;
  168. // Internal flags.
  169. unsigned int transfer_delay;
  170. enum cip_sfc sfc;
  171. unsigned int syt_interval;
  172. /* For a PCM substream processing. */
  173. struct snd_pcm_substream *pcm;
  174. struct work_struct period_work;
  175. snd_pcm_uframes_t pcm_buffer_pointer;
  176. unsigned int pcm_period_pointer;
  177. unsigned int pcm_frame_multiplier;
  178. // To start processing content of packets at the same cycle in several contexts for
  179. // each direction.
  180. bool ready_processing;
  181. wait_queue_head_t ready_wait;
  182. unsigned int next_cycle;
  183. /* For backends to process data blocks. */
  184. void *protocol;
  185. amdtp_stream_process_ctx_payloads_t process_ctx_payloads;
  186. // For domain.
  187. int channel;
  188. int speed;
  189. struct list_head list;
  190. struct amdtp_domain *domain;
  191. };
  192. int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
  193. enum amdtp_stream_direction dir, unsigned int flags,
  194. unsigned int fmt,
  195. amdtp_stream_process_ctx_payloads_t process_ctx_payloads,
  196. unsigned int protocol_size);
  197. void amdtp_stream_destroy(struct amdtp_stream *s);
  198. int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
  199. unsigned int data_block_quadlets, unsigned int pcm_frame_multiplier);
  200. unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s);
  201. void amdtp_stream_update(struct amdtp_stream *s);
  202. int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
  203. struct snd_pcm_runtime *runtime);
  204. void amdtp_stream_pcm_prepare(struct amdtp_stream *s);
  205. void amdtp_stream_pcm_abort(struct amdtp_stream *s);
  206. extern const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT];
  207. extern const unsigned int amdtp_rate_table[CIP_SFC_COUNT];
  208. /**
  209. * amdtp_stream_running - check stream is running or not
  210. * @s: the AMDTP stream
  211. *
  212. * If this function returns true, the stream is running.
  213. */
  214. static inline bool amdtp_stream_running(struct amdtp_stream *s)
  215. {
  216. return !IS_ERR(s->context);
  217. }
  218. /**
  219. * amdtp_streaming_error - check for streaming error
  220. * @s: the AMDTP stream
  221. *
  222. * If this function returns true, the stream's packet queue has stopped due to
  223. * an asynchronous error.
  224. */
  225. static inline bool amdtp_streaming_error(struct amdtp_stream *s)
  226. {
  227. return s->packet_index < 0;
  228. }
  229. /**
  230. * amdtp_stream_pcm_running - check PCM substream is running or not
  231. * @s: the AMDTP stream
  232. *
  233. * If this function returns true, PCM substream in the AMDTP stream is running.
  234. */
  235. static inline bool amdtp_stream_pcm_running(struct amdtp_stream *s)
  236. {
  237. return !!s->pcm;
  238. }
  239. /**
  240. * amdtp_stream_pcm_trigger - start/stop playback from a PCM device
  241. * @s: the AMDTP stream
  242. * @pcm: the PCM device to be started, or %NULL to stop the current device
  243. *
  244. * Call this function on a running isochronous stream to enable the actual
  245. * transmission of PCM data. This function should be called from the PCM
  246. * device's .trigger callback.
  247. */
  248. static inline void amdtp_stream_pcm_trigger(struct amdtp_stream *s,
  249. struct snd_pcm_substream *pcm)
  250. {
  251. WRITE_ONCE(s->pcm, pcm);
  252. }
  253. /**
  254. * amdtp_stream_next_packet_desc - retrieve next descriptor for amdtp packet.
  255. * @s: the AMDTP stream
  256. * @desc: the descriptor of packet
  257. *
  258. * This macro computes next descriptor so that the list of descriptors behaves circular queue.
  259. */
  260. #define amdtp_stream_next_packet_desc(s, desc) \
  261. list_next_entry_circular(desc, &s->packet_descs_list, link)
  262. static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)
  263. {
  264. return sfc & 1;
  265. }
  266. struct seq_desc {
  267. unsigned int syt_offset;
  268. unsigned int data_blocks;
  269. };
  270. struct amdtp_domain {
  271. struct list_head streams;
  272. unsigned int events_per_period;
  273. unsigned int events_per_buffer;
  274. struct amdtp_stream *irq_target;
  275. struct {
  276. unsigned int tx_init_skip;
  277. unsigned int tx_start;
  278. unsigned int rx_start;
  279. } processing_cycle;
  280. struct {
  281. bool enable:1;
  282. bool on_the_fly:1;
  283. } replay;
  284. };
  285. int amdtp_domain_init(struct amdtp_domain *d);
  286. void amdtp_domain_destroy(struct amdtp_domain *d);
  287. int amdtp_domain_add_stream(struct amdtp_domain *d, struct amdtp_stream *s,
  288. int channel, int speed);
  289. int amdtp_domain_start(struct amdtp_domain *d, unsigned int tx_init_skip_cycles, bool replay_seq,
  290. bool replay_on_the_fly);
  291. void amdtp_domain_stop(struct amdtp_domain *d);
  292. static inline int amdtp_domain_set_events_per_period(struct amdtp_domain *d,
  293. unsigned int events_per_period,
  294. unsigned int events_per_buffer)
  295. {
  296. d->events_per_period = events_per_period;
  297. d->events_per_buffer = events_per_buffer;
  298. return 0;
  299. }
  300. unsigned long amdtp_domain_stream_pcm_pointer(struct amdtp_domain *d,
  301. struct amdtp_stream *s);
  302. int amdtp_domain_stream_pcm_ack(struct amdtp_domain *d, struct amdtp_stream *s);
  303. /**
  304. * amdtp_domain_wait_ready - sleep till being ready to process packets or timeout
  305. * @d: the AMDTP domain
  306. * @timeout_ms: msec till timeout
  307. *
  308. * If this function return false, the AMDTP domain should be stopped.
  309. */
  310. static inline bool amdtp_domain_wait_ready(struct amdtp_domain *d, unsigned int timeout_ms)
  311. {
  312. struct amdtp_stream *s;
  313. list_for_each_entry(s, &d->streams, list) {
  314. unsigned int j = msecs_to_jiffies(timeout_ms);
  315. if (wait_event_interruptible_timeout(s->ready_wait, s->ready_processing, j) <= 0)
  316. return false;
  317. }
  318. return true;
  319. }
  320. #endif