fireworks_stream.c 8.6 KB

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