fireworks_stream.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * fireworks_stream.c - a part of driver for Fireworks based devices
  3. *
  4. * Copyright (c) 2013-2014 Takashi Sakamoto
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include "./fireworks.h"
  9. #define CALLBACK_TIMEOUT 100
  10. static int
  11. init_stream(struct snd_efw *efw, struct amdtp_stream *stream)
  12. {
  13. struct cmp_connection *conn;
  14. enum cmp_direction c_dir;
  15. enum amdtp_stream_direction s_dir;
  16. int err;
  17. if (stream == &efw->tx_stream) {
  18. conn = &efw->out_conn;
  19. c_dir = CMP_OUTPUT;
  20. s_dir = AMDTP_IN_STREAM;
  21. } else {
  22. conn = &efw->in_conn;
  23. c_dir = CMP_INPUT;
  24. s_dir = AMDTP_OUT_STREAM;
  25. }
  26. err = cmp_connection_init(conn, efw->unit, c_dir, 0);
  27. if (err < 0)
  28. goto end;
  29. err = amdtp_am824_init(stream, efw->unit, s_dir, CIP_BLOCKING);
  30. if (err < 0) {
  31. amdtp_stream_destroy(stream);
  32. cmp_connection_destroy(conn);
  33. }
  34. end:
  35. return err;
  36. }
  37. static void
  38. stop_stream(struct snd_efw *efw, struct amdtp_stream *stream)
  39. {
  40. amdtp_stream_pcm_abort(stream);
  41. amdtp_stream_stop(stream);
  42. if (stream == &efw->tx_stream)
  43. cmp_connection_break(&efw->out_conn);
  44. else
  45. cmp_connection_break(&efw->in_conn);
  46. }
  47. static int
  48. start_stream(struct snd_efw *efw, struct amdtp_stream *stream,
  49. unsigned int sampling_rate)
  50. {
  51. struct cmp_connection *conn;
  52. unsigned int mode, pcm_channels, midi_ports;
  53. int err;
  54. err = snd_efw_get_multiplier_mode(sampling_rate, &mode);
  55. if (err < 0)
  56. goto end;
  57. if (stream == &efw->tx_stream) {
  58. conn = &efw->out_conn;
  59. pcm_channels = efw->pcm_capture_channels[mode];
  60. midi_ports = efw->midi_out_ports;
  61. } else {
  62. conn = &efw->in_conn;
  63. pcm_channels = efw->pcm_playback_channels[mode];
  64. midi_ports = efw->midi_in_ports;
  65. }
  66. err = amdtp_am824_set_parameters(stream, sampling_rate,
  67. pcm_channels, midi_ports, false);
  68. if (err < 0)
  69. goto end;
  70. /* establish connection via CMP */
  71. err = cmp_connection_establish(conn,
  72. amdtp_stream_get_max_payload(stream));
  73. if (err < 0)
  74. goto end;
  75. /* start amdtp stream */
  76. err = amdtp_stream_start(stream,
  77. conn->resources.channel,
  78. conn->speed);
  79. if (err < 0) {
  80. stop_stream(efw, stream);
  81. goto end;
  82. }
  83. /* wait first callback */
  84. if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) {
  85. stop_stream(efw, stream);
  86. err = -ETIMEDOUT;
  87. }
  88. end:
  89. return err;
  90. }
  91. /*
  92. * This function should be called before starting the stream or after stopping
  93. * the streams.
  94. */
  95. static void
  96. destroy_stream(struct snd_efw *efw, struct amdtp_stream *stream)
  97. {
  98. struct cmp_connection *conn;
  99. if (stream == &efw->tx_stream)
  100. conn = &efw->out_conn;
  101. else
  102. conn = &efw->in_conn;
  103. amdtp_stream_destroy(stream);
  104. cmp_connection_destroy(&efw->out_conn);
  105. }
  106. static int
  107. check_connection_used_by_others(struct snd_efw *efw, struct amdtp_stream *s)
  108. {
  109. struct cmp_connection *conn;
  110. bool used;
  111. int err;
  112. if (s == &efw->tx_stream)
  113. conn = &efw->out_conn;
  114. else
  115. conn = &efw->in_conn;
  116. err = cmp_connection_check_used(conn, &used);
  117. if ((err >= 0) && used && !amdtp_stream_running(s)) {
  118. dev_err(&efw->unit->device,
  119. "Connection established by others: %cPCR[%d]\n",
  120. (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
  121. conn->pcr_index);
  122. err = -EBUSY;
  123. }
  124. return err;
  125. }
  126. int snd_efw_stream_init_duplex(struct snd_efw *efw)
  127. {
  128. int err;
  129. err = init_stream(efw, &efw->tx_stream);
  130. if (err < 0)
  131. goto end;
  132. /* Fireworks transmits NODATA packets with TAG0. */
  133. efw->tx_stream.flags |= CIP_EMPTY_WITH_TAG0;
  134. /* Fireworks has its own meaning for dbc. */
  135. efw->tx_stream.flags |= CIP_DBC_IS_END_EVENT;
  136. /* Fireworks reset dbc at bus reset. */
  137. efw->tx_stream.flags |= CIP_SKIP_DBC_ZERO_CHECK;
  138. /*
  139. * But Recent firmwares starts packets with non-zero dbc.
  140. * Driver version 5.7.6 installs firmware version 5.7.3.
  141. */
  142. if (efw->is_fireworks3 &&
  143. (efw->firmware_version == 0x5070000 ||
  144. efw->firmware_version == 0x5070300 ||
  145. efw->firmware_version == 0x5080000))
  146. efw->tx_stream.tx_first_dbc = 0x02;
  147. /* AudioFire9 always reports wrong dbs. */
  148. if (efw->is_af9)
  149. efw->tx_stream.flags |= CIP_WRONG_DBS;
  150. /* Firmware version 5.5 reports fixed interval for dbc. */
  151. if (efw->firmware_version == 0x5050000)
  152. efw->tx_stream.tx_dbc_interval = 8;
  153. err = init_stream(efw, &efw->rx_stream);
  154. if (err < 0) {
  155. destroy_stream(efw, &efw->tx_stream);
  156. goto end;
  157. }
  158. /* set IEC61883 compliant mode (actually not fully compliant...) */
  159. err = snd_efw_command_set_tx_mode(efw, SND_EFW_TRANSPORT_MODE_IEC61883);
  160. if (err < 0) {
  161. destroy_stream(efw, &efw->tx_stream);
  162. destroy_stream(efw, &efw->rx_stream);
  163. }
  164. end:
  165. return err;
  166. }
  167. int snd_efw_stream_start_duplex(struct snd_efw *efw, unsigned int rate)
  168. {
  169. unsigned int curr_rate;
  170. int err = 0;
  171. /* Need no substreams */
  172. if (efw->playback_substreams == 0 && efw->capture_substreams == 0)
  173. goto end;
  174. /*
  175. * Considering JACK/FFADO streaming:
  176. * TODO: This can be removed hwdep functionality becomes popular.
  177. */
  178. err = check_connection_used_by_others(efw, &efw->rx_stream);
  179. if (err < 0)
  180. goto end;
  181. /* packet queueing error */
  182. if (amdtp_streaming_error(&efw->tx_stream))
  183. stop_stream(efw, &efw->tx_stream);
  184. if (amdtp_streaming_error(&efw->rx_stream))
  185. stop_stream(efw, &efw->rx_stream);
  186. /* stop streams if rate is different */
  187. err = snd_efw_command_get_sampling_rate(efw, &curr_rate);
  188. if (err < 0)
  189. goto end;
  190. if (rate == 0)
  191. rate = curr_rate;
  192. if (rate != curr_rate) {
  193. stop_stream(efw, &efw->tx_stream);
  194. stop_stream(efw, &efw->rx_stream);
  195. }
  196. /* master should be always running */
  197. if (!amdtp_stream_running(&efw->rx_stream)) {
  198. err = snd_efw_command_set_sampling_rate(efw, rate);
  199. if (err < 0)
  200. goto end;
  201. err = start_stream(efw, &efw->rx_stream, rate);
  202. if (err < 0) {
  203. dev_err(&efw->unit->device,
  204. "fail to start AMDTP master stream:%d\n", err);
  205. goto end;
  206. }
  207. }
  208. /* start slave if needed */
  209. if (efw->capture_substreams > 0 &&
  210. !amdtp_stream_running(&efw->tx_stream)) {
  211. err = start_stream(efw, &efw->tx_stream, rate);
  212. if (err < 0) {
  213. dev_err(&efw->unit->device,
  214. "fail to start AMDTP slave stream:%d\n", err);
  215. stop_stream(efw, &efw->rx_stream);
  216. }
  217. }
  218. end:
  219. return err;
  220. }
  221. void snd_efw_stream_stop_duplex(struct snd_efw *efw)
  222. {
  223. if (efw->capture_substreams == 0) {
  224. stop_stream(efw, &efw->tx_stream);
  225. if (efw->playback_substreams == 0)
  226. stop_stream(efw, &efw->rx_stream);
  227. }
  228. }
  229. void snd_efw_stream_update_duplex(struct snd_efw *efw)
  230. {
  231. if (cmp_connection_update(&efw->out_conn) < 0 ||
  232. cmp_connection_update(&efw->in_conn) < 0) {
  233. stop_stream(efw, &efw->rx_stream);
  234. stop_stream(efw, &efw->tx_stream);
  235. } else {
  236. amdtp_stream_update(&efw->rx_stream);
  237. amdtp_stream_update(&efw->tx_stream);
  238. }
  239. }
  240. void snd_efw_stream_destroy_duplex(struct snd_efw *efw)
  241. {
  242. destroy_stream(efw, &efw->rx_stream);
  243. destroy_stream(efw, &efw->tx_stream);
  244. }
  245. void snd_efw_stream_lock_changed(struct snd_efw *efw)
  246. {
  247. efw->dev_lock_changed = true;
  248. wake_up(&efw->hwdep_wait);
  249. }
  250. int snd_efw_stream_lock_try(struct snd_efw *efw)
  251. {
  252. int err;
  253. spin_lock_irq(&efw->lock);
  254. /* user land lock this */
  255. if (efw->dev_lock_count < 0) {
  256. err = -EBUSY;
  257. goto end;
  258. }
  259. /* this is the first time */
  260. if (efw->dev_lock_count++ == 0)
  261. snd_efw_stream_lock_changed(efw);
  262. err = 0;
  263. end:
  264. spin_unlock_irq(&efw->lock);
  265. return err;
  266. }
  267. void snd_efw_stream_lock_release(struct snd_efw *efw)
  268. {
  269. spin_lock_irq(&efw->lock);
  270. if (WARN_ON(efw->dev_lock_count <= 0))
  271. goto end;
  272. if (--efw->dev_lock_count == 0)
  273. snd_efw_stream_lock_changed(efw);
  274. end:
  275. spin_unlock_irq(&efw->lock);
  276. }