fireworks_stream.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * fireworks_stream.c - a part of driver for Fireworks based devices
  4. *
  5. * Copyright (c) 2013-2014 Takashi Sakamoto
  6. */
  7. #include "./fireworks.h"
  8. #define READY_TIMEOUT_MS 1000
  9. static int init_stream(struct snd_efw *efw, struct amdtp_stream *stream)
  10. {
  11. struct cmp_connection *conn;
  12. enum cmp_direction c_dir;
  13. enum amdtp_stream_direction s_dir;
  14. int err;
  15. if (stream == &efw->tx_stream) {
  16. conn = &efw->out_conn;
  17. c_dir = CMP_OUTPUT;
  18. s_dir = AMDTP_IN_STREAM;
  19. } else {
  20. conn = &efw->in_conn;
  21. c_dir = CMP_INPUT;
  22. s_dir = AMDTP_OUT_STREAM;
  23. }
  24. err = cmp_connection_init(conn, efw->unit, c_dir, 0);
  25. if (err < 0)
  26. return err;
  27. err = amdtp_am824_init(stream, efw->unit, s_dir, CIP_BLOCKING | CIP_UNAWARE_SYT);
  28. if (err < 0) {
  29. amdtp_stream_destroy(stream);
  30. cmp_connection_destroy(conn);
  31. return err;
  32. }
  33. if (stream == &efw->tx_stream) {
  34. // Fireworks transmits NODATA packets with TAG0.
  35. efw->tx_stream.flags |= CIP_EMPTY_WITH_TAG0;
  36. // Fireworks has its own meaning for dbc.
  37. efw->tx_stream.flags |= CIP_DBC_IS_END_EVENT;
  38. // Fireworks reset dbc at bus reset.
  39. efw->tx_stream.flags |= CIP_SKIP_DBC_ZERO_CHECK;
  40. // But Recent firmwares starts packets with non-zero dbc.
  41. // Driver version 5.7.6 installs firmware version 5.7.3.
  42. if (efw->is_fireworks3 &&
  43. (efw->firmware_version == 0x5070000 ||
  44. efw->firmware_version == 0x5070300 ||
  45. efw->firmware_version == 0x5080000))
  46. efw->tx_stream.flags |= CIP_UNALIGHED_DBC;
  47. // AudioFire9 always reports wrong dbs. Onyx 1200F with the latest firmware (v4.6.0)
  48. // also report wrong dbs at 88.2 kHz or greater.
  49. if (efw->is_af9 || efw->firmware_version == 0x4060000)
  50. efw->tx_stream.flags |= CIP_WRONG_DBS;
  51. // Firmware version 5.5 reports fixed interval for dbc.
  52. if (efw->firmware_version == 0x5050000)
  53. efw->tx_stream.ctx_data.tx.dbc_interval = 8;
  54. }
  55. return err;
  56. }
  57. static int start_stream(struct snd_efw *efw, struct amdtp_stream *stream,
  58. unsigned int rate)
  59. {
  60. struct cmp_connection *conn;
  61. int err;
  62. if (stream == &efw->tx_stream)
  63. conn = &efw->out_conn;
  64. else
  65. conn = &efw->in_conn;
  66. // Establish connection via CMP.
  67. err = cmp_connection_establish(conn);
  68. if (err < 0)
  69. return err;
  70. // Start amdtp stream.
  71. err = amdtp_domain_add_stream(&efw->domain, stream,
  72. conn->resources.channel, conn->speed);
  73. if (err < 0) {
  74. cmp_connection_break(conn);
  75. return err;
  76. }
  77. return 0;
  78. }
  79. // This function should be called before starting the stream or after stopping
  80. // the streams.
  81. static void destroy_stream(struct snd_efw *efw, struct amdtp_stream *stream)
  82. {
  83. amdtp_stream_destroy(stream);
  84. if (stream == &efw->tx_stream)
  85. cmp_connection_destroy(&efw->out_conn);
  86. else
  87. cmp_connection_destroy(&efw->in_conn);
  88. }
  89. static int
  90. check_connection_used_by_others(struct snd_efw *efw, struct amdtp_stream *s)
  91. {
  92. struct cmp_connection *conn;
  93. bool used;
  94. int err;
  95. if (s == &efw->tx_stream)
  96. conn = &efw->out_conn;
  97. else
  98. conn = &efw->in_conn;
  99. err = cmp_connection_check_used(conn, &used);
  100. if ((err >= 0) && used && !amdtp_stream_running(s)) {
  101. dev_err(&efw->unit->device,
  102. "Connection established by others: %cPCR[%d]\n",
  103. (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
  104. conn->pcr_index);
  105. err = -EBUSY;
  106. }
  107. return err;
  108. }
  109. int snd_efw_stream_init_duplex(struct snd_efw *efw)
  110. {
  111. int err;
  112. err = init_stream(efw, &efw->tx_stream);
  113. if (err < 0)
  114. return err;
  115. err = init_stream(efw, &efw->rx_stream);
  116. if (err < 0) {
  117. destroy_stream(efw, &efw->tx_stream);
  118. return err;
  119. }
  120. err = amdtp_domain_init(&efw->domain);
  121. if (err < 0) {
  122. destroy_stream(efw, &efw->tx_stream);
  123. destroy_stream(efw, &efw->rx_stream);
  124. return err;
  125. }
  126. // set IEC61883 compliant mode (actually not fully compliant...).
  127. err = snd_efw_command_set_tx_mode(efw, SND_EFW_TRANSPORT_MODE_IEC61883);
  128. if (err < 0) {
  129. destroy_stream(efw, &efw->tx_stream);
  130. destroy_stream(efw, &efw->rx_stream);
  131. }
  132. return err;
  133. }
  134. static int keep_resources(struct snd_efw *efw, struct amdtp_stream *stream,
  135. unsigned int rate, unsigned int mode)
  136. {
  137. unsigned int pcm_channels;
  138. unsigned int midi_ports;
  139. struct cmp_connection *conn;
  140. int err;
  141. if (stream == &efw->tx_stream) {
  142. pcm_channels = efw->pcm_capture_channels[mode];
  143. midi_ports = efw->midi_out_ports;
  144. conn = &efw->out_conn;
  145. } else {
  146. pcm_channels = efw->pcm_playback_channels[mode];
  147. midi_ports = efw->midi_in_ports;
  148. conn = &efw->in_conn;
  149. }
  150. err = amdtp_am824_set_parameters(stream, rate, pcm_channels,
  151. midi_ports, false);
  152. if (err < 0)
  153. return err;
  154. return cmp_connection_reserve(conn, amdtp_stream_get_max_payload(stream));
  155. }
  156. int snd_efw_stream_reserve_duplex(struct snd_efw *efw, unsigned int rate,
  157. unsigned int frames_per_period,
  158. unsigned int frames_per_buffer)
  159. {
  160. unsigned int curr_rate;
  161. int err;
  162. // Considering JACK/FFADO streaming:
  163. // TODO: This can be removed hwdep functionality becomes popular.
  164. err = check_connection_used_by_others(efw, &efw->rx_stream);
  165. if (err < 0)
  166. return err;
  167. // stop streams if rate is different.
  168. err = snd_efw_command_get_sampling_rate(efw, &curr_rate);
  169. if (err < 0)
  170. return err;
  171. if (rate == 0)
  172. rate = curr_rate;
  173. if (rate != curr_rate) {
  174. amdtp_domain_stop(&efw->domain);
  175. cmp_connection_break(&efw->out_conn);
  176. cmp_connection_break(&efw->in_conn);
  177. cmp_connection_release(&efw->out_conn);
  178. cmp_connection_release(&efw->in_conn);
  179. }
  180. if (efw->substreams_counter == 0 || rate != curr_rate) {
  181. unsigned int mode;
  182. err = snd_efw_command_set_sampling_rate(efw, rate);
  183. if (err < 0)
  184. return err;
  185. err = snd_efw_get_multiplier_mode(rate, &mode);
  186. if (err < 0)
  187. return err;
  188. err = keep_resources(efw, &efw->tx_stream, rate, mode);
  189. if (err < 0)
  190. return err;
  191. err = keep_resources(efw, &efw->rx_stream, rate, mode);
  192. if (err < 0) {
  193. cmp_connection_release(&efw->in_conn);
  194. return err;
  195. }
  196. err = amdtp_domain_set_events_per_period(&efw->domain,
  197. frames_per_period, frames_per_buffer);
  198. if (err < 0) {
  199. cmp_connection_release(&efw->in_conn);
  200. cmp_connection_release(&efw->out_conn);
  201. return err;
  202. }
  203. }
  204. return 0;
  205. }
  206. int snd_efw_stream_start_duplex(struct snd_efw *efw)
  207. {
  208. unsigned int rate;
  209. int err = 0;
  210. // Need no substreams.
  211. if (efw->substreams_counter == 0)
  212. return -EIO;
  213. if (amdtp_streaming_error(&efw->rx_stream) ||
  214. amdtp_streaming_error(&efw->tx_stream)) {
  215. amdtp_domain_stop(&efw->domain);
  216. cmp_connection_break(&efw->out_conn);
  217. cmp_connection_break(&efw->in_conn);
  218. }
  219. err = snd_efw_command_get_sampling_rate(efw, &rate);
  220. if (err < 0)
  221. return err;
  222. if (!amdtp_stream_running(&efw->rx_stream)) {
  223. unsigned int tx_init_skip_cycles;
  224. // Audiofire 2/4 skip an isochronous cycle several thousands after starting
  225. // packet transmission.
  226. if (efw->is_fireworks3 && !efw->is_af9)
  227. tx_init_skip_cycles = 6000;
  228. else
  229. tx_init_skip_cycles = 0;
  230. err = start_stream(efw, &efw->rx_stream, rate);
  231. if (err < 0)
  232. goto error;
  233. err = start_stream(efw, &efw->tx_stream, rate);
  234. if (err < 0)
  235. goto error;
  236. // NOTE: The device ignores presentation time expressed by the value of syt field
  237. // of CIP header in received packets. The sequence of the number of data blocks per
  238. // packet is important for media clock recovery.
  239. err = amdtp_domain_start(&efw->domain, tx_init_skip_cycles, true, false);
  240. if (err < 0)
  241. goto error;
  242. if (!amdtp_domain_wait_ready(&efw->domain, READY_TIMEOUT_MS)) {
  243. err = -ETIMEDOUT;
  244. goto error;
  245. }
  246. }
  247. return 0;
  248. error:
  249. amdtp_domain_stop(&efw->domain);
  250. cmp_connection_break(&efw->out_conn);
  251. cmp_connection_break(&efw->in_conn);
  252. return err;
  253. }
  254. void snd_efw_stream_stop_duplex(struct snd_efw *efw)
  255. {
  256. if (efw->substreams_counter == 0) {
  257. amdtp_domain_stop(&efw->domain);
  258. cmp_connection_break(&efw->out_conn);
  259. cmp_connection_break(&efw->in_conn);
  260. cmp_connection_release(&efw->out_conn);
  261. cmp_connection_release(&efw->in_conn);
  262. }
  263. }
  264. void snd_efw_stream_update_duplex(struct snd_efw *efw)
  265. {
  266. amdtp_domain_stop(&efw->domain);
  267. cmp_connection_break(&efw->out_conn);
  268. cmp_connection_break(&efw->in_conn);
  269. amdtp_stream_pcm_abort(&efw->rx_stream);
  270. amdtp_stream_pcm_abort(&efw->tx_stream);
  271. }
  272. void snd_efw_stream_destroy_duplex(struct snd_efw *efw)
  273. {
  274. amdtp_domain_destroy(&efw->domain);
  275. destroy_stream(efw, &efw->rx_stream);
  276. destroy_stream(efw, &efw->tx_stream);
  277. }
  278. void snd_efw_stream_lock_changed(struct snd_efw *efw)
  279. {
  280. efw->dev_lock_changed = true;
  281. wake_up(&efw->hwdep_wait);
  282. }
  283. int snd_efw_stream_lock_try(struct snd_efw *efw)
  284. {
  285. int err;
  286. spin_lock_irq(&efw->lock);
  287. /* user land lock this */
  288. if (efw->dev_lock_count < 0) {
  289. err = -EBUSY;
  290. goto end;
  291. }
  292. /* this is the first time */
  293. if (efw->dev_lock_count++ == 0)
  294. snd_efw_stream_lock_changed(efw);
  295. err = 0;
  296. end:
  297. spin_unlock_irq(&efw->lock);
  298. return err;
  299. }
  300. void snd_efw_stream_lock_release(struct snd_efw *efw)
  301. {
  302. spin_lock_irq(&efw->lock);
  303. if (WARN_ON(efw->dev_lock_count <= 0))
  304. goto end;
  305. if (--efw->dev_lock_count == 0)
  306. snd_efw_stream_lock_changed(efw);
  307. end:
  308. spin_unlock_irq(&efw->lock);
  309. }