ff-stream.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ff-stream.c - a part of driver for RME Fireface series
  4. *
  5. * Copyright (c) 2015-2017 Takashi Sakamoto
  6. */
  7. #include "ff.h"
  8. #define READY_TIMEOUT_MS 200
  9. int snd_ff_stream_get_multiplier_mode(enum cip_sfc sfc,
  10. enum snd_ff_stream_mode *mode)
  11. {
  12. static const enum snd_ff_stream_mode modes[] = {
  13. [CIP_SFC_32000] = SND_FF_STREAM_MODE_LOW,
  14. [CIP_SFC_44100] = SND_FF_STREAM_MODE_LOW,
  15. [CIP_SFC_48000] = SND_FF_STREAM_MODE_LOW,
  16. [CIP_SFC_88200] = SND_FF_STREAM_MODE_MID,
  17. [CIP_SFC_96000] = SND_FF_STREAM_MODE_MID,
  18. [CIP_SFC_176400] = SND_FF_STREAM_MODE_HIGH,
  19. [CIP_SFC_192000] = SND_FF_STREAM_MODE_HIGH,
  20. };
  21. if (sfc >= CIP_SFC_COUNT)
  22. return -EINVAL;
  23. *mode = modes[sfc];
  24. return 0;
  25. }
  26. static inline void finish_session(struct snd_ff *ff)
  27. {
  28. ff->spec->protocol->finish_session(ff);
  29. ff->spec->protocol->switch_fetching_mode(ff, false);
  30. }
  31. static int init_stream(struct snd_ff *ff, struct amdtp_stream *s)
  32. {
  33. struct fw_iso_resources *resources;
  34. enum amdtp_stream_direction dir;
  35. int err;
  36. if (s == &ff->tx_stream) {
  37. resources = &ff->tx_resources;
  38. dir = AMDTP_IN_STREAM;
  39. } else {
  40. resources = &ff->rx_resources;
  41. dir = AMDTP_OUT_STREAM;
  42. }
  43. err = fw_iso_resources_init(resources, ff->unit);
  44. if (err < 0)
  45. return err;
  46. err = amdtp_ff_init(s, ff->unit, dir);
  47. if (err < 0)
  48. fw_iso_resources_destroy(resources);
  49. return err;
  50. }
  51. static void destroy_stream(struct snd_ff *ff, struct amdtp_stream *s)
  52. {
  53. amdtp_stream_destroy(s);
  54. if (s == &ff->tx_stream)
  55. fw_iso_resources_destroy(&ff->tx_resources);
  56. else
  57. fw_iso_resources_destroy(&ff->rx_resources);
  58. }
  59. int snd_ff_stream_init_duplex(struct snd_ff *ff)
  60. {
  61. int err;
  62. err = init_stream(ff, &ff->rx_stream);
  63. if (err < 0)
  64. return err;
  65. err = init_stream(ff, &ff->tx_stream);
  66. if (err < 0) {
  67. destroy_stream(ff, &ff->rx_stream);
  68. return err;
  69. }
  70. err = amdtp_domain_init(&ff->domain);
  71. if (err < 0) {
  72. destroy_stream(ff, &ff->rx_stream);
  73. destroy_stream(ff, &ff->tx_stream);
  74. }
  75. return err;
  76. }
  77. /*
  78. * This function should be called before starting streams or after stopping
  79. * streams.
  80. */
  81. void snd_ff_stream_destroy_duplex(struct snd_ff *ff)
  82. {
  83. amdtp_domain_destroy(&ff->domain);
  84. destroy_stream(ff, &ff->rx_stream);
  85. destroy_stream(ff, &ff->tx_stream);
  86. }
  87. int snd_ff_stream_reserve_duplex(struct snd_ff *ff, unsigned int rate,
  88. unsigned int frames_per_period,
  89. unsigned int frames_per_buffer)
  90. {
  91. unsigned int curr_rate;
  92. enum snd_ff_clock_src src;
  93. int err;
  94. err = ff->spec->protocol->get_clock(ff, &curr_rate, &src);
  95. if (err < 0)
  96. return err;
  97. if (ff->substreams_counter == 0 || curr_rate != rate) {
  98. enum snd_ff_stream_mode mode;
  99. int i;
  100. amdtp_domain_stop(&ff->domain);
  101. finish_session(ff);
  102. fw_iso_resources_free(&ff->tx_resources);
  103. fw_iso_resources_free(&ff->rx_resources);
  104. for (i = 0; i < CIP_SFC_COUNT; ++i) {
  105. if (amdtp_rate_table[i] == rate)
  106. break;
  107. }
  108. if (i >= CIP_SFC_COUNT)
  109. return -EINVAL;
  110. err = snd_ff_stream_get_multiplier_mode(i, &mode);
  111. if (err < 0)
  112. return err;
  113. err = amdtp_ff_set_parameters(&ff->tx_stream, rate,
  114. ff->spec->pcm_capture_channels[mode]);
  115. if (err < 0)
  116. return err;
  117. err = amdtp_ff_set_parameters(&ff->rx_stream, rate,
  118. ff->spec->pcm_playback_channels[mode]);
  119. if (err < 0)
  120. return err;
  121. err = ff->spec->protocol->allocate_resources(ff, rate);
  122. if (err < 0)
  123. return err;
  124. err = amdtp_domain_set_events_per_period(&ff->domain,
  125. frames_per_period, frames_per_buffer);
  126. if (err < 0) {
  127. fw_iso_resources_free(&ff->tx_resources);
  128. fw_iso_resources_free(&ff->rx_resources);
  129. return err;
  130. }
  131. }
  132. return 0;
  133. }
  134. int snd_ff_stream_start_duplex(struct snd_ff *ff, unsigned int rate)
  135. {
  136. int err;
  137. if (ff->substreams_counter == 0)
  138. return 0;
  139. if (amdtp_streaming_error(&ff->tx_stream) ||
  140. amdtp_streaming_error(&ff->rx_stream)) {
  141. amdtp_domain_stop(&ff->domain);
  142. finish_session(ff);
  143. }
  144. /*
  145. * Regardless of current source of clock signal, drivers transfer some
  146. * packets. Then, the device transfers packets.
  147. */
  148. if (!amdtp_stream_running(&ff->rx_stream)) {
  149. int spd = fw_parent_device(ff->unit)->max_speed;
  150. err = ff->spec->protocol->begin_session(ff, rate);
  151. if (err < 0)
  152. goto error;
  153. err = amdtp_domain_add_stream(&ff->domain, &ff->rx_stream,
  154. ff->rx_resources.channel, spd);
  155. if (err < 0)
  156. goto error;
  157. err = amdtp_domain_add_stream(&ff->domain, &ff->tx_stream,
  158. ff->tx_resources.channel, spd);
  159. if (err < 0)
  160. goto error;
  161. // NOTE: The device doesn't transfer packets unless receiving any packet. The
  162. // sequence of tx packets includes cycle skip corresponding to empty packet or
  163. // NODATA packet in IEC 61883-1/6. The sequence of the number of data blocks per
  164. // packet is important for media clock recovery.
  165. err = amdtp_domain_start(&ff->domain, 0, true, true);
  166. if (err < 0)
  167. goto error;
  168. if (!amdtp_domain_wait_ready(&ff->domain, READY_TIMEOUT_MS)) {
  169. err = -ETIMEDOUT;
  170. goto error;
  171. }
  172. err = ff->spec->protocol->switch_fetching_mode(ff, true);
  173. if (err < 0)
  174. goto error;
  175. }
  176. return 0;
  177. error:
  178. amdtp_domain_stop(&ff->domain);
  179. finish_session(ff);
  180. return err;
  181. }
  182. void snd_ff_stream_stop_duplex(struct snd_ff *ff)
  183. {
  184. if (ff->substreams_counter == 0) {
  185. amdtp_domain_stop(&ff->domain);
  186. finish_session(ff);
  187. fw_iso_resources_free(&ff->tx_resources);
  188. fw_iso_resources_free(&ff->rx_resources);
  189. }
  190. }
  191. void snd_ff_stream_update_duplex(struct snd_ff *ff)
  192. {
  193. amdtp_domain_stop(&ff->domain);
  194. // The device discontinue to transfer packets.
  195. amdtp_stream_pcm_abort(&ff->tx_stream);
  196. amdtp_stream_pcm_abort(&ff->rx_stream);
  197. }
  198. void snd_ff_stream_lock_changed(struct snd_ff *ff)
  199. {
  200. ff->dev_lock_changed = true;
  201. wake_up(&ff->hwdep_wait);
  202. }
  203. int snd_ff_stream_lock_try(struct snd_ff *ff)
  204. {
  205. int err;
  206. spin_lock_irq(&ff->lock);
  207. /* user land lock this */
  208. if (ff->dev_lock_count < 0) {
  209. err = -EBUSY;
  210. goto end;
  211. }
  212. /* this is the first time */
  213. if (ff->dev_lock_count++ == 0)
  214. snd_ff_stream_lock_changed(ff);
  215. err = 0;
  216. end:
  217. spin_unlock_irq(&ff->lock);
  218. return err;
  219. }
  220. void snd_ff_stream_lock_release(struct snd_ff *ff)
  221. {
  222. spin_lock_irq(&ff->lock);
  223. if (WARN_ON(ff->dev_lock_count <= 0))
  224. goto end;
  225. if (--ff->dev_lock_count == 0)
  226. snd_ff_stream_lock_changed(ff);
  227. end:
  228. spin_unlock_irq(&ff->lock);
  229. }