dice-stream.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. __be32 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, sizeof(channel));
  46. else
  47. snd_dice_transaction_write_rx(dice, RX_ISOCHRONOUS,
  48. &channel, sizeof(channel));
  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. __be32 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, sizeof(channel));
  66. else
  67. err = snd_dice_transaction_write_rx(dice, RX_ISOCHRONOUS,
  68. &channel, sizeof(channel));
  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. bool double_pcm_frames;
  89. int err;
  90. err = snd_dice_stream_get_rate_mode(dice, rate, &mode);
  91. if (err < 0)
  92. goto end;
  93. if (stream == &dice->tx_stream) {
  94. resources = &dice->tx_resources;
  95. pcm_chs = dice->tx_channels[mode];
  96. midi_ports = dice->tx_midi_ports[mode];
  97. } else {
  98. resources = &dice->rx_resources;
  99. pcm_chs = dice->rx_channels[mode];
  100. midi_ports = dice->rx_midi_ports[mode];
  101. }
  102. /*
  103. * At 176.4/192.0 kHz, Dice has a quirk to transfer two PCM frames in
  104. * one data block of AMDTP packet. Thus sampling transfer frequency is
  105. * a half of PCM sampling frequency, i.e. PCM frames at 192.0 kHz are
  106. * transferred on AMDTP packets at 96 kHz. Two successive samples of a
  107. * channel are stored consecutively in the packet. This quirk is called
  108. * as 'Dual Wire'.
  109. * For this quirk, blocking mode is required and PCM buffer size should
  110. * be aligned to SYT_INTERVAL.
  111. */
  112. double_pcm_frames = mode > 1;
  113. if (double_pcm_frames) {
  114. rate /= 2;
  115. pcm_chs *= 2;
  116. }
  117. err = amdtp_am824_set_parameters(stream, rate, pcm_chs, midi_ports,
  118. double_pcm_frames);
  119. if (err < 0)
  120. goto end;
  121. if (double_pcm_frames) {
  122. pcm_chs /= 2;
  123. for (i = 0; i < pcm_chs; i++) {
  124. amdtp_am824_set_pcm_position(stream, i, i * 2);
  125. amdtp_am824_set_pcm_position(stream, i + pcm_chs,
  126. i * 2 + 1);
  127. }
  128. }
  129. err = keep_resources(dice, resources,
  130. amdtp_stream_get_max_payload(stream));
  131. if (err < 0) {
  132. dev_err(&dice->unit->device,
  133. "fail to keep isochronous resources\n");
  134. goto end;
  135. }
  136. err = amdtp_stream_start(stream, resources->channel,
  137. fw_parent_device(dice->unit)->max_speed);
  138. if (err < 0)
  139. release_resources(dice, resources);
  140. end:
  141. return err;
  142. }
  143. static int get_sync_mode(struct snd_dice *dice, enum cip_flags *sync_mode)
  144. {
  145. u32 source;
  146. int err;
  147. err = snd_dice_transaction_get_clock_source(dice, &source);
  148. if (err < 0)
  149. goto end;
  150. switch (source) {
  151. /* So-called 'SYT Match' modes, sync_to_syt value of packets received */
  152. case CLOCK_SOURCE_ARX4: /* in 4th stream */
  153. case CLOCK_SOURCE_ARX3: /* in 3rd stream */
  154. case CLOCK_SOURCE_ARX2: /* in 2nd stream */
  155. err = -ENOSYS;
  156. break;
  157. case CLOCK_SOURCE_ARX1: /* in 1st stream, which this driver uses */
  158. *sync_mode = 0;
  159. break;
  160. default:
  161. *sync_mode = CIP_SYNC_TO_DEVICE;
  162. break;
  163. }
  164. end:
  165. return err;
  166. }
  167. int snd_dice_stream_start_duplex(struct snd_dice *dice, unsigned int rate)
  168. {
  169. struct amdtp_stream *master, *slave;
  170. unsigned int curr_rate;
  171. enum cip_flags sync_mode;
  172. int err = 0;
  173. if (dice->substreams_counter == 0)
  174. goto end;
  175. err = get_sync_mode(dice, &sync_mode);
  176. if (err < 0)
  177. goto end;
  178. if (sync_mode == CIP_SYNC_TO_DEVICE) {
  179. master = &dice->tx_stream;
  180. slave = &dice->rx_stream;
  181. } else {
  182. master = &dice->rx_stream;
  183. slave = &dice->tx_stream;
  184. }
  185. /* Some packet queueing errors. */
  186. if (amdtp_streaming_error(master) || amdtp_streaming_error(slave))
  187. stop_stream(dice, master);
  188. /* Stop stream if rate is different. */
  189. err = snd_dice_transaction_get_rate(dice, &curr_rate);
  190. if (err < 0) {
  191. dev_err(&dice->unit->device,
  192. "fail to get sampling rate\n");
  193. goto end;
  194. }
  195. if (rate == 0)
  196. rate = curr_rate;
  197. if (rate != curr_rate)
  198. stop_stream(dice, master);
  199. if (!amdtp_stream_running(master)) {
  200. stop_stream(dice, slave);
  201. snd_dice_transaction_clear_enable(dice);
  202. amdtp_stream_set_sync(sync_mode, master, slave);
  203. err = snd_dice_transaction_set_rate(dice, rate);
  204. if (err < 0) {
  205. dev_err(&dice->unit->device,
  206. "fail to set sampling rate\n");
  207. goto end;
  208. }
  209. /* Start both streams. */
  210. err = start_stream(dice, master, rate);
  211. if (err < 0) {
  212. dev_err(&dice->unit->device,
  213. "fail to start AMDTP master stream\n");
  214. goto end;
  215. }
  216. err = start_stream(dice, slave, rate);
  217. if (err < 0) {
  218. dev_err(&dice->unit->device,
  219. "fail to start AMDTP slave stream\n");
  220. stop_stream(dice, master);
  221. goto end;
  222. }
  223. err = snd_dice_transaction_set_enable(dice);
  224. if (err < 0) {
  225. dev_err(&dice->unit->device,
  226. "fail to enable interface\n");
  227. stop_stream(dice, master);
  228. stop_stream(dice, slave);
  229. goto end;
  230. }
  231. /* Wait first callbacks */
  232. if (!amdtp_stream_wait_callback(master, CALLBACK_TIMEOUT) ||
  233. !amdtp_stream_wait_callback(slave, CALLBACK_TIMEOUT)) {
  234. snd_dice_transaction_clear_enable(dice);
  235. stop_stream(dice, master);
  236. stop_stream(dice, slave);
  237. err = -ETIMEDOUT;
  238. }
  239. }
  240. end:
  241. return err;
  242. }
  243. void snd_dice_stream_stop_duplex(struct snd_dice *dice)
  244. {
  245. if (dice->substreams_counter > 0)
  246. return;
  247. snd_dice_transaction_clear_enable(dice);
  248. stop_stream(dice, &dice->tx_stream);
  249. stop_stream(dice, &dice->rx_stream);
  250. }
  251. static int init_stream(struct snd_dice *dice, struct amdtp_stream *stream)
  252. {
  253. int err;
  254. struct fw_iso_resources *resources;
  255. enum amdtp_stream_direction dir;
  256. if (stream == &dice->tx_stream) {
  257. resources = &dice->tx_resources;
  258. dir = AMDTP_IN_STREAM;
  259. } else {
  260. resources = &dice->rx_resources;
  261. dir = AMDTP_OUT_STREAM;
  262. }
  263. err = fw_iso_resources_init(resources, dice->unit);
  264. if (err < 0)
  265. goto end;
  266. resources->channels_mask = 0x00000000ffffffffuLL;
  267. err = amdtp_am824_init(stream, dice->unit, dir, CIP_BLOCKING);
  268. if (err < 0) {
  269. amdtp_stream_destroy(stream);
  270. fw_iso_resources_destroy(resources);
  271. }
  272. end:
  273. return err;
  274. }
  275. /*
  276. * This function should be called before starting streams or after stopping
  277. * streams.
  278. */
  279. static void destroy_stream(struct snd_dice *dice, struct amdtp_stream *stream)
  280. {
  281. struct fw_iso_resources *resources;
  282. if (stream == &dice->tx_stream)
  283. resources = &dice->tx_resources;
  284. else
  285. resources = &dice->rx_resources;
  286. amdtp_stream_destroy(stream);
  287. fw_iso_resources_destroy(resources);
  288. }
  289. int snd_dice_stream_init_duplex(struct snd_dice *dice)
  290. {
  291. int err;
  292. dice->substreams_counter = 0;
  293. err = init_stream(dice, &dice->tx_stream);
  294. if (err < 0)
  295. goto end;
  296. err = init_stream(dice, &dice->rx_stream);
  297. if (err < 0)
  298. destroy_stream(dice, &dice->tx_stream);
  299. end:
  300. return err;
  301. }
  302. void snd_dice_stream_destroy_duplex(struct snd_dice *dice)
  303. {
  304. snd_dice_transaction_clear_enable(dice);
  305. destroy_stream(dice, &dice->tx_stream);
  306. destroy_stream(dice, &dice->rx_stream);
  307. dice->substreams_counter = 0;
  308. }
  309. void snd_dice_stream_update_duplex(struct snd_dice *dice)
  310. {
  311. /*
  312. * On a bus reset, the DICE firmware disables streaming and then goes
  313. * off contemplating its own navel for hundreds of milliseconds before
  314. * it can react to any of our attempts to reenable streaming. This
  315. * means that we lose synchronization anyway, so we force our streams
  316. * to stop so that the application can restart them in an orderly
  317. * manner.
  318. */
  319. dice->global_enabled = false;
  320. stop_stream(dice, &dice->rx_stream);
  321. stop_stream(dice, &dice->tx_stream);
  322. fw_iso_resources_update(&dice->rx_resources);
  323. fw_iso_resources_update(&dice->tx_resources);
  324. }
  325. static void dice_lock_changed(struct snd_dice *dice)
  326. {
  327. dice->dev_lock_changed = true;
  328. wake_up(&dice->hwdep_wait);
  329. }
  330. int snd_dice_stream_lock_try(struct snd_dice *dice)
  331. {
  332. int err;
  333. spin_lock_irq(&dice->lock);
  334. if (dice->dev_lock_count < 0) {
  335. err = -EBUSY;
  336. goto out;
  337. }
  338. if (dice->dev_lock_count++ == 0)
  339. dice_lock_changed(dice);
  340. err = 0;
  341. out:
  342. spin_unlock_irq(&dice->lock);
  343. return err;
  344. }
  345. void snd_dice_stream_lock_release(struct snd_dice *dice)
  346. {
  347. spin_lock_irq(&dice->lock);
  348. if (WARN_ON(dice->dev_lock_count <= 0))
  349. goto out;
  350. if (--dice->dev_lock_count == 0)
  351. dice_lock_changed(dice);
  352. out:
  353. spin_unlock_irq(&dice->lock);
  354. }