dice-stream.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * dice_stream.c - a part of driver for DICE based devices
  3. *
  4. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  5. * Copyright (c) 2014 Takashi Sakamoto <o-takashi@sakamocchi.jp>
  6. *
  7. * Licensed under the terms of the GNU General Public License, version 2.
  8. */
  9. #include "dice.h"
  10. #define CALLBACK_TIMEOUT 200
  11. const unsigned int snd_dice_rates[SND_DICE_RATES_COUNT] = {
  12. /* mode 0 */
  13. [0] = 32000,
  14. [1] = 44100,
  15. [2] = 48000,
  16. /* mode 1 */
  17. [3] = 88200,
  18. [4] = 96000,
  19. /* mode 2 */
  20. [5] = 176400,
  21. [6] = 192000,
  22. };
  23. int snd_dice_stream_get_rate_mode(struct snd_dice *dice, unsigned int rate,
  24. unsigned int *mode)
  25. {
  26. int i;
  27. for (i = 0; i < ARRAY_SIZE(snd_dice_rates); i++) {
  28. if (!(dice->clock_caps & BIT(i)))
  29. continue;
  30. if (snd_dice_rates[i] != rate)
  31. continue;
  32. *mode = (i - 1) / 2;
  33. return 0;
  34. }
  35. return -EINVAL;
  36. }
  37. static void release_resources(struct snd_dice *dice,
  38. struct fw_iso_resources *resources)
  39. {
  40. unsigned int channel;
  41. /* Reset channel number */
  42. channel = cpu_to_be32((u32)-1);
  43. if (resources == &dice->tx_resources)
  44. snd_dice_transaction_write_tx(dice, TX_ISOCHRONOUS,
  45. &channel, 4);
  46. else
  47. snd_dice_transaction_write_rx(dice, RX_ISOCHRONOUS,
  48. &channel, 4);
  49. fw_iso_resources_free(resources);
  50. }
  51. static int keep_resources(struct snd_dice *dice,
  52. struct fw_iso_resources *resources,
  53. unsigned int max_payload_bytes)
  54. {
  55. unsigned int channel;
  56. int err;
  57. err = fw_iso_resources_allocate(resources, max_payload_bytes,
  58. fw_parent_device(dice->unit)->max_speed);
  59. if (err < 0)
  60. goto end;
  61. /* Set channel number */
  62. channel = cpu_to_be32(resources->channel);
  63. if (resources == &dice->tx_resources)
  64. err = snd_dice_transaction_write_tx(dice, TX_ISOCHRONOUS,
  65. &channel, 4);
  66. else
  67. err = snd_dice_transaction_write_rx(dice, RX_ISOCHRONOUS,
  68. &channel, 4);
  69. if (err < 0)
  70. release_resources(dice, resources);
  71. end:
  72. return err;
  73. }
  74. static void stop_stream(struct snd_dice *dice, struct amdtp_stream *stream)
  75. {
  76. amdtp_stream_pcm_abort(stream);
  77. amdtp_stream_stop(stream);
  78. if (stream == &dice->tx_stream)
  79. release_resources(dice, &dice->tx_resources);
  80. else
  81. release_resources(dice, &dice->rx_resources);
  82. }
  83. static int start_stream(struct snd_dice *dice, struct amdtp_stream *stream,
  84. unsigned int rate)
  85. {
  86. struct fw_iso_resources *resources;
  87. unsigned int i, mode, pcm_chs, midi_ports;
  88. int err;
  89. err = snd_dice_stream_get_rate_mode(dice, rate, &mode);
  90. if (err < 0)
  91. goto end;
  92. if (stream == &dice->tx_stream) {
  93. resources = &dice->tx_resources;
  94. pcm_chs = dice->tx_channels[mode];
  95. midi_ports = dice->tx_midi_ports[mode];
  96. } else {
  97. resources = &dice->rx_resources;
  98. pcm_chs = dice->rx_channels[mode];
  99. midi_ports = dice->rx_midi_ports[mode];
  100. }
  101. /*
  102. * At 176.4/192.0 kHz, Dice has a quirk to transfer two PCM frames in
  103. * one data block of AMDTP packet. Thus sampling transfer frequency is
  104. * a half of PCM sampling frequency, i.e. PCM frames at 192.0 kHz are
  105. * transferred on AMDTP packets at 96 kHz. Two successive samples of a
  106. * channel are stored consecutively in the packet. This quirk is called
  107. * as 'Dual Wire'.
  108. * For this quirk, blocking mode is required and PCM buffer size should
  109. * be aligned to SYT_INTERVAL.
  110. */
  111. if (mode > 1) {
  112. rate /= 2;
  113. pcm_chs *= 2;
  114. stream->double_pcm_frames = true;
  115. } else {
  116. stream->double_pcm_frames = false;
  117. }
  118. amdtp_stream_set_parameters(stream, rate, pcm_chs, midi_ports);
  119. if (mode > 1) {
  120. pcm_chs /= 2;
  121. for (i = 0; i < pcm_chs; i++) {
  122. stream->pcm_positions[i] = i * 2;
  123. stream->pcm_positions[i + pcm_chs] = i * 2 + 1;
  124. }
  125. }
  126. err = keep_resources(dice, resources,
  127. amdtp_stream_get_max_payload(stream));
  128. if (err < 0) {
  129. dev_err(&dice->unit->device,
  130. "fail to keep isochronous resources\n");
  131. goto end;
  132. }
  133. err = amdtp_stream_start(stream, resources->channel,
  134. fw_parent_device(dice->unit)->max_speed);
  135. if (err < 0)
  136. release_resources(dice, resources);
  137. end:
  138. return err;
  139. }
  140. static int get_sync_mode(struct snd_dice *dice, enum cip_flags *sync_mode)
  141. {
  142. u32 source;
  143. int err;
  144. err = snd_dice_transaction_get_clock_source(dice, &source);
  145. if (err < 0)
  146. goto end;
  147. switch (source) {
  148. /* So-called 'SYT Match' modes, sync_to_syt value of packets received */
  149. case CLOCK_SOURCE_ARX4: /* in 4th stream */
  150. case CLOCK_SOURCE_ARX3: /* in 3rd stream */
  151. case CLOCK_SOURCE_ARX2: /* in 2nd stream */
  152. err = -ENOSYS;
  153. break;
  154. case CLOCK_SOURCE_ARX1: /* in 1st stream, which this driver uses */
  155. *sync_mode = 0;
  156. break;
  157. default:
  158. *sync_mode = CIP_SYNC_TO_DEVICE;
  159. break;
  160. }
  161. end:
  162. return err;
  163. }
  164. int snd_dice_stream_start_duplex(struct snd_dice *dice, unsigned int rate)
  165. {
  166. struct amdtp_stream *master, *slave;
  167. unsigned int curr_rate;
  168. enum cip_flags sync_mode;
  169. int err = 0;
  170. if (dice->substreams_counter == 0)
  171. goto end;
  172. err = get_sync_mode(dice, &sync_mode);
  173. if (err < 0)
  174. goto end;
  175. if (sync_mode == CIP_SYNC_TO_DEVICE) {
  176. master = &dice->tx_stream;
  177. slave = &dice->rx_stream;
  178. } else {
  179. master = &dice->rx_stream;
  180. slave = &dice->tx_stream;
  181. }
  182. /* Some packet queueing errors. */
  183. if (amdtp_streaming_error(master) || amdtp_streaming_error(slave))
  184. stop_stream(dice, master);
  185. /* Stop stream if rate is different. */
  186. err = snd_dice_transaction_get_rate(dice, &curr_rate);
  187. if (err < 0) {
  188. dev_err(&dice->unit->device,
  189. "fail to get sampling rate\n");
  190. goto end;
  191. }
  192. if (rate == 0)
  193. rate = curr_rate;
  194. if (rate != curr_rate)
  195. stop_stream(dice, master);
  196. if (!amdtp_stream_running(master)) {
  197. stop_stream(dice, slave);
  198. snd_dice_transaction_clear_enable(dice);
  199. amdtp_stream_set_sync(sync_mode, master, slave);
  200. err = snd_dice_transaction_set_rate(dice, rate);
  201. if (err < 0) {
  202. dev_err(&dice->unit->device,
  203. "fail to set sampling rate\n");
  204. goto end;
  205. }
  206. /* Start both streams. */
  207. err = start_stream(dice, master, rate);
  208. if (err < 0) {
  209. dev_err(&dice->unit->device,
  210. "fail to start AMDTP master stream\n");
  211. goto end;
  212. }
  213. err = start_stream(dice, slave, rate);
  214. if (err < 0) {
  215. dev_err(&dice->unit->device,
  216. "fail to start AMDTP slave stream\n");
  217. stop_stream(dice, master);
  218. goto end;
  219. }
  220. err = snd_dice_transaction_set_enable(dice);
  221. if (err < 0) {
  222. dev_err(&dice->unit->device,
  223. "fail to enable interface\n");
  224. stop_stream(dice, master);
  225. stop_stream(dice, slave);
  226. goto end;
  227. }
  228. /* Wait first callbacks */
  229. if (!amdtp_stream_wait_callback(master, CALLBACK_TIMEOUT) ||
  230. !amdtp_stream_wait_callback(slave, CALLBACK_TIMEOUT)) {
  231. snd_dice_transaction_clear_enable(dice);
  232. stop_stream(dice, master);
  233. stop_stream(dice, slave);
  234. err = -ETIMEDOUT;
  235. }
  236. }
  237. end:
  238. return err;
  239. }
  240. void snd_dice_stream_stop_duplex(struct snd_dice *dice)
  241. {
  242. if (dice->substreams_counter > 0)
  243. return;
  244. snd_dice_transaction_clear_enable(dice);
  245. stop_stream(dice, &dice->tx_stream);
  246. stop_stream(dice, &dice->rx_stream);
  247. }
  248. static int init_stream(struct snd_dice *dice, struct amdtp_stream *stream)
  249. {
  250. int err;
  251. struct fw_iso_resources *resources;
  252. enum amdtp_stream_direction dir;
  253. if (stream == &dice->tx_stream) {
  254. resources = &dice->tx_resources;
  255. dir = AMDTP_IN_STREAM;
  256. } else {
  257. resources = &dice->rx_resources;
  258. dir = AMDTP_OUT_STREAM;
  259. }
  260. err = fw_iso_resources_init(resources, dice->unit);
  261. if (err < 0)
  262. goto end;
  263. resources->channels_mask = 0x00000000ffffffffuLL;
  264. err = amdtp_stream_init(stream, dice->unit, dir, CIP_BLOCKING);
  265. if (err < 0) {
  266. amdtp_stream_destroy(stream);
  267. fw_iso_resources_destroy(resources);
  268. }
  269. end:
  270. return err;
  271. }
  272. /*
  273. * This function should be called before starting streams or after stopping
  274. * streams.
  275. */
  276. static void destroy_stream(struct snd_dice *dice, struct amdtp_stream *stream)
  277. {
  278. struct fw_iso_resources *resources;
  279. if (stream == &dice->tx_stream)
  280. resources = &dice->tx_resources;
  281. else
  282. resources = &dice->rx_resources;
  283. amdtp_stream_destroy(stream);
  284. fw_iso_resources_destroy(resources);
  285. }
  286. int snd_dice_stream_init_duplex(struct snd_dice *dice)
  287. {
  288. int err;
  289. dice->substreams_counter = 0;
  290. err = init_stream(dice, &dice->tx_stream);
  291. if (err < 0)
  292. goto end;
  293. err = init_stream(dice, &dice->rx_stream);
  294. if (err < 0)
  295. destroy_stream(dice, &dice->tx_stream);
  296. end:
  297. return err;
  298. }
  299. void snd_dice_stream_destroy_duplex(struct snd_dice *dice)
  300. {
  301. snd_dice_transaction_clear_enable(dice);
  302. destroy_stream(dice, &dice->tx_stream);
  303. destroy_stream(dice, &dice->rx_stream);
  304. dice->substreams_counter = 0;
  305. }
  306. void snd_dice_stream_update_duplex(struct snd_dice *dice)
  307. {
  308. /*
  309. * On a bus reset, the DICE firmware disables streaming and then goes
  310. * off contemplating its own navel for hundreds of milliseconds before
  311. * it can react to any of our attempts to reenable streaming. This
  312. * means that we lose synchronization anyway, so we force our streams
  313. * to stop so that the application can restart them in an orderly
  314. * manner.
  315. */
  316. dice->global_enabled = false;
  317. stop_stream(dice, &dice->rx_stream);
  318. stop_stream(dice, &dice->tx_stream);
  319. fw_iso_resources_update(&dice->rx_resources);
  320. fw_iso_resources_update(&dice->tx_resources);
  321. }
  322. static void dice_lock_changed(struct snd_dice *dice)
  323. {
  324. dice->dev_lock_changed = true;
  325. wake_up(&dice->hwdep_wait);
  326. }
  327. int snd_dice_stream_lock_try(struct snd_dice *dice)
  328. {
  329. int err;
  330. spin_lock_irq(&dice->lock);
  331. if (dice->dev_lock_count < 0) {
  332. err = -EBUSY;
  333. goto out;
  334. }
  335. if (dice->dev_lock_count++ == 0)
  336. dice_lock_changed(dice);
  337. err = 0;
  338. out:
  339. spin_unlock_irq(&dice->lock);
  340. return err;
  341. }
  342. void snd_dice_stream_lock_release(struct snd_dice *dice)
  343. {
  344. spin_lock_irq(&dice->lock);
  345. if (WARN_ON(dice->dev_lock_count <= 0))
  346. goto out;
  347. if (--dice->dev_lock_count == 0)
  348. dice_lock_changed(dice);
  349. out:
  350. spin_unlock_irq(&dice->lock);
  351. }