fireworks_stream.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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_stream_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. amdtp_stream_set_parameters(stream, sampling_rate,
  67. pcm_channels, midi_ports);
  68. /* establish connection via CMP */
  69. err = cmp_connection_establish(conn,
  70. amdtp_stream_get_max_payload(stream));
  71. if (err < 0)
  72. goto end;
  73. /* start amdtp stream */
  74. err = amdtp_stream_start(stream,
  75. conn->resources.channel,
  76. conn->speed);
  77. if (err < 0) {
  78. stop_stream(efw, stream);
  79. goto end;
  80. }
  81. /* wait first callback */
  82. if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) {
  83. stop_stream(efw, stream);
  84. err = -ETIMEDOUT;
  85. }
  86. end:
  87. return err;
  88. }
  89. /*
  90. * This function should be called before starting the stream or after stopping
  91. * the streams.
  92. */
  93. static void
  94. destroy_stream(struct snd_efw *efw, struct amdtp_stream *stream)
  95. {
  96. struct cmp_connection *conn;
  97. if (stream == &efw->tx_stream)
  98. conn = &efw->out_conn;
  99. else
  100. conn = &efw->in_conn;
  101. amdtp_stream_destroy(stream);
  102. cmp_connection_destroy(&efw->out_conn);
  103. }
  104. static int
  105. get_sync_mode(struct snd_efw *efw, enum cip_flags *sync_mode)
  106. {
  107. enum snd_efw_clock_source clock_source;
  108. int err;
  109. err = snd_efw_command_get_clock_source(efw, &clock_source);
  110. if (err < 0)
  111. return err;
  112. if (clock_source == SND_EFW_CLOCK_SOURCE_SYTMATCH)
  113. return -ENOSYS;
  114. *sync_mode = CIP_SYNC_TO_DEVICE;
  115. return 0;
  116. }
  117. static int
  118. check_connection_used_by_others(struct snd_efw *efw, struct amdtp_stream *s)
  119. {
  120. struct cmp_connection *conn;
  121. bool used;
  122. int err;
  123. if (s == &efw->tx_stream)
  124. conn = &efw->out_conn;
  125. else
  126. conn = &efw->in_conn;
  127. err = cmp_connection_check_used(conn, &used);
  128. if ((err >= 0) && used && !amdtp_stream_running(s)) {
  129. dev_err(&efw->unit->device,
  130. "Connection established by others: %cPCR[%d]\n",
  131. (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
  132. conn->pcr_index);
  133. err = -EBUSY;
  134. }
  135. return err;
  136. }
  137. int snd_efw_stream_init_duplex(struct snd_efw *efw)
  138. {
  139. int err;
  140. err = init_stream(efw, &efw->tx_stream);
  141. if (err < 0)
  142. goto end;
  143. /* Fireworks transmits NODATA packets with TAG0. */
  144. efw->tx_stream.flags |= CIP_EMPTY_WITH_TAG0;
  145. /* Fireworks has its own meaning for dbc. */
  146. efw->tx_stream.flags |= CIP_DBC_IS_END_EVENT;
  147. /* Fireworks reset dbc at bus reset. */
  148. efw->tx_stream.flags |= CIP_SKIP_DBC_ZERO_CHECK;
  149. /* AudioFire9 always reports wrong dbs. */
  150. if (efw->is_af9)
  151. efw->tx_stream.flags |= CIP_WRONG_DBS;
  152. /* Firmware version 5.5 reports fixed interval for dbc. */
  153. if (efw->firmware_version == 0x5050000)
  154. efw->tx_stream.tx_dbc_interval = 8;
  155. err = init_stream(efw, &efw->rx_stream);
  156. if (err < 0) {
  157. destroy_stream(efw, &efw->tx_stream);
  158. goto end;
  159. }
  160. /* set IEC61883 compliant mode (actually not fully compliant...) */
  161. err = snd_efw_command_set_tx_mode(efw, SND_EFW_TRANSPORT_MODE_IEC61883);
  162. if (err < 0) {
  163. destroy_stream(efw, &efw->tx_stream);
  164. destroy_stream(efw, &efw->rx_stream);
  165. }
  166. end:
  167. return err;
  168. }
  169. int snd_efw_stream_start_duplex(struct snd_efw *efw, unsigned int rate)
  170. {
  171. struct amdtp_stream *master, *slave;
  172. atomic_t *slave_substreams;
  173. enum cip_flags sync_mode;
  174. unsigned int curr_rate;
  175. int err = 0;
  176. mutex_lock(&efw->mutex);
  177. /* Need no substreams */
  178. if ((atomic_read(&efw->playback_substreams) == 0) &&
  179. (atomic_read(&efw->capture_substreams) == 0))
  180. goto end;
  181. err = get_sync_mode(efw, &sync_mode);
  182. if (err < 0)
  183. goto end;
  184. if (sync_mode == CIP_SYNC_TO_DEVICE) {
  185. master = &efw->tx_stream;
  186. slave = &efw->rx_stream;
  187. slave_substreams = &efw->playback_substreams;
  188. } else {
  189. master = &efw->rx_stream;
  190. slave = &efw->tx_stream;
  191. slave_substreams = &efw->capture_substreams;
  192. }
  193. /*
  194. * Considering JACK/FFADO streaming:
  195. * TODO: This can be removed hwdep functionality becomes popular.
  196. */
  197. err = check_connection_used_by_others(efw, master);
  198. if (err < 0)
  199. goto end;
  200. /* packet queueing error */
  201. if (amdtp_streaming_error(slave))
  202. stop_stream(efw, slave);
  203. if (amdtp_streaming_error(master))
  204. stop_stream(efw, master);
  205. /* stop streams if rate is different */
  206. err = snd_efw_command_get_sampling_rate(efw, &curr_rate);
  207. if (err < 0)
  208. goto end;
  209. if (rate == 0)
  210. rate = curr_rate;
  211. if (rate != curr_rate) {
  212. stop_stream(efw, slave);
  213. stop_stream(efw, master);
  214. }
  215. /* master should be always running */
  216. if (!amdtp_stream_running(master)) {
  217. amdtp_stream_set_sync(sync_mode, master, slave);
  218. efw->master = master;
  219. err = snd_efw_command_set_sampling_rate(efw, rate);
  220. if (err < 0)
  221. goto end;
  222. err = start_stream(efw, master, rate);
  223. if (err < 0) {
  224. dev_err(&efw->unit->device,
  225. "fail to start AMDTP master stream:%d\n", err);
  226. goto end;
  227. }
  228. }
  229. /* start slave if needed */
  230. if (atomic_read(slave_substreams) > 0 && !amdtp_stream_running(slave)) {
  231. err = start_stream(efw, slave, rate);
  232. if (err < 0) {
  233. dev_err(&efw->unit->device,
  234. "fail to start AMDTP slave stream:%d\n", err);
  235. stop_stream(efw, master);
  236. }
  237. }
  238. end:
  239. mutex_unlock(&efw->mutex);
  240. return err;
  241. }
  242. void snd_efw_stream_stop_duplex(struct snd_efw *efw)
  243. {
  244. struct amdtp_stream *master, *slave;
  245. atomic_t *master_substreams, *slave_substreams;
  246. if (efw->master == &efw->rx_stream) {
  247. slave = &efw->tx_stream;
  248. master = &efw->rx_stream;
  249. slave_substreams = &efw->capture_substreams;
  250. master_substreams = &efw->playback_substreams;
  251. } else {
  252. slave = &efw->rx_stream;
  253. master = &efw->tx_stream;
  254. slave_substreams = &efw->playback_substreams;
  255. master_substreams = &efw->capture_substreams;
  256. }
  257. mutex_lock(&efw->mutex);
  258. if (atomic_read(slave_substreams) == 0) {
  259. stop_stream(efw, slave);
  260. if (atomic_read(master_substreams) == 0)
  261. stop_stream(efw, master);
  262. }
  263. mutex_unlock(&efw->mutex);
  264. }
  265. void snd_efw_stream_update_duplex(struct snd_efw *efw)
  266. {
  267. if ((cmp_connection_update(&efw->out_conn) < 0) ||
  268. (cmp_connection_update(&efw->in_conn) < 0)) {
  269. mutex_lock(&efw->mutex);
  270. stop_stream(efw, &efw->rx_stream);
  271. stop_stream(efw, &efw->tx_stream);
  272. mutex_unlock(&efw->mutex);
  273. } else {
  274. amdtp_stream_update(&efw->rx_stream);
  275. amdtp_stream_update(&efw->tx_stream);
  276. }
  277. }
  278. void snd_efw_stream_destroy_duplex(struct snd_efw *efw)
  279. {
  280. destroy_stream(efw, &efw->rx_stream);
  281. destroy_stream(efw, &efw->tx_stream);
  282. }
  283. void snd_efw_stream_lock_changed(struct snd_efw *efw)
  284. {
  285. efw->dev_lock_changed = true;
  286. wake_up(&efw->hwdep_wait);
  287. }
  288. int snd_efw_stream_lock_try(struct snd_efw *efw)
  289. {
  290. int err;
  291. spin_lock_irq(&efw->lock);
  292. /* user land lock this */
  293. if (efw->dev_lock_count < 0) {
  294. err = -EBUSY;
  295. goto end;
  296. }
  297. /* this is the first time */
  298. if (efw->dev_lock_count++ == 0)
  299. snd_efw_stream_lock_changed(efw);
  300. err = 0;
  301. end:
  302. spin_unlock_irq(&efw->lock);
  303. return err;
  304. }
  305. void snd_efw_stream_lock_release(struct snd_efw *efw)
  306. {
  307. spin_lock_irq(&efw->lock);
  308. if (WARN_ON(efw->dev_lock_count <= 0))
  309. goto end;
  310. if (--efw->dev_lock_count == 0)
  311. snd_efw_stream_lock_changed(efw);
  312. end:
  313. spin_unlock_irq(&efw->lock);
  314. }