dice-pcm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * dice_pcm.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. static int dice_rate_constraint(struct snd_pcm_hw_params *params,
  11. struct snd_pcm_hw_rule *rule)
  12. {
  13. struct snd_pcm_substream *substream = rule->private;
  14. struct snd_dice *dice = substream->private_data;
  15. const struct snd_interval *c =
  16. hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  17. struct snd_interval *r =
  18. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  19. struct snd_interval rates = {
  20. .min = UINT_MAX, .max = 0, .integer = 1
  21. };
  22. unsigned int i, rate, mode, *pcm_channels;
  23. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  24. pcm_channels = dice->tx_channels;
  25. else
  26. pcm_channels = dice->rx_channels;
  27. for (i = 0; i < ARRAY_SIZE(snd_dice_rates); ++i) {
  28. rate = snd_dice_rates[i];
  29. if (snd_dice_stream_get_rate_mode(dice, rate, &mode) < 0)
  30. continue;
  31. if (!snd_interval_test(c, pcm_channels[mode]))
  32. continue;
  33. rates.min = min(rates.min, rate);
  34. rates.max = max(rates.max, rate);
  35. }
  36. return snd_interval_refine(r, &rates);
  37. }
  38. static int dice_channels_constraint(struct snd_pcm_hw_params *params,
  39. struct snd_pcm_hw_rule *rule)
  40. {
  41. struct snd_pcm_substream *substream = rule->private;
  42. struct snd_dice *dice = substream->private_data;
  43. const struct snd_interval *r =
  44. hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
  45. struct snd_interval *c =
  46. hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  47. struct snd_interval channels = {
  48. .min = UINT_MAX, .max = 0, .integer = 1
  49. };
  50. unsigned int i, rate, mode, *pcm_channels;
  51. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  52. pcm_channels = dice->tx_channels;
  53. else
  54. pcm_channels = dice->rx_channels;
  55. for (i = 0; i < ARRAY_SIZE(snd_dice_rates); ++i) {
  56. rate = snd_dice_rates[i];
  57. if (snd_dice_stream_get_rate_mode(dice, rate, &mode) < 0)
  58. continue;
  59. if (!snd_interval_test(r, rate))
  60. continue;
  61. channels.min = min(channels.min, pcm_channels[mode]);
  62. channels.max = max(channels.max, pcm_channels[mode]);
  63. }
  64. return snd_interval_refine(c, &channels);
  65. }
  66. static void limit_channels_and_rates(struct snd_dice *dice,
  67. struct snd_pcm_runtime *runtime,
  68. unsigned int *pcm_channels)
  69. {
  70. struct snd_pcm_hardware *hw = &runtime->hw;
  71. unsigned int i, rate, mode;
  72. hw->channels_min = UINT_MAX;
  73. hw->channels_max = 0;
  74. for (i = 0; i < ARRAY_SIZE(snd_dice_rates); ++i) {
  75. rate = snd_dice_rates[i];
  76. if (snd_dice_stream_get_rate_mode(dice, rate, &mode) < 0)
  77. continue;
  78. hw->rates |= snd_pcm_rate_to_rate_bit(rate);
  79. if (pcm_channels[mode] == 0)
  80. continue;
  81. hw->channels_min = min(hw->channels_min, pcm_channels[mode]);
  82. hw->channels_max = max(hw->channels_max, pcm_channels[mode]);
  83. }
  84. snd_pcm_limit_hw_rates(runtime);
  85. }
  86. static void limit_period_and_buffer(struct snd_pcm_hardware *hw)
  87. {
  88. hw->periods_min = 2; /* SNDRV_PCM_INFO_BATCH */
  89. hw->periods_max = UINT_MAX;
  90. hw->period_bytes_min = 4 * hw->channels_max; /* byte for a frame */
  91. /* Just to prevent from allocating much pages. */
  92. hw->period_bytes_max = hw->period_bytes_min * 2048;
  93. hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
  94. }
  95. static int init_hw_info(struct snd_dice *dice,
  96. struct snd_pcm_substream *substream)
  97. {
  98. struct snd_pcm_runtime *runtime = substream->runtime;
  99. struct snd_pcm_hardware *hw = &runtime->hw;
  100. struct amdtp_stream *stream;
  101. unsigned int *pcm_channels;
  102. int err;
  103. hw->info = SNDRV_PCM_INFO_MMAP |
  104. SNDRV_PCM_INFO_MMAP_VALID |
  105. SNDRV_PCM_INFO_BATCH |
  106. SNDRV_PCM_INFO_INTERLEAVED |
  107. SNDRV_PCM_INFO_JOINT_DUPLEX |
  108. SNDRV_PCM_INFO_BLOCK_TRANSFER;
  109. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  110. hw->formats = AMDTP_IN_PCM_FORMAT_BITS;
  111. stream = &dice->tx_stream;
  112. pcm_channels = dice->tx_channels;
  113. } else {
  114. hw->formats = AMDTP_OUT_PCM_FORMAT_BITS;
  115. stream = &dice->rx_stream;
  116. pcm_channels = dice->rx_channels;
  117. }
  118. limit_channels_and_rates(dice, runtime, pcm_channels);
  119. limit_period_and_buffer(hw);
  120. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  121. dice_rate_constraint, substream,
  122. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  123. if (err < 0)
  124. goto end;
  125. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  126. dice_channels_constraint, substream,
  127. SNDRV_PCM_HW_PARAM_RATE, -1);
  128. if (err < 0)
  129. goto end;
  130. err = amdtp_stream_add_pcm_hw_constraints(stream, runtime);
  131. end:
  132. return err;
  133. }
  134. static int pcm_open(struct snd_pcm_substream *substream)
  135. {
  136. struct snd_dice *dice = substream->private_data;
  137. unsigned int source, rate;
  138. bool internal;
  139. int err;
  140. err = snd_dice_stream_lock_try(dice);
  141. if (err < 0)
  142. goto end;
  143. err = init_hw_info(dice, substream);
  144. if (err < 0)
  145. goto err_locked;
  146. err = snd_dice_transaction_get_clock_source(dice, &source);
  147. if (err < 0)
  148. goto err_locked;
  149. switch (source) {
  150. case CLOCK_SOURCE_AES1:
  151. case CLOCK_SOURCE_AES2:
  152. case CLOCK_SOURCE_AES3:
  153. case CLOCK_SOURCE_AES4:
  154. case CLOCK_SOURCE_AES_ANY:
  155. case CLOCK_SOURCE_ADAT:
  156. case CLOCK_SOURCE_TDIF:
  157. case CLOCK_SOURCE_WC:
  158. internal = false;
  159. break;
  160. default:
  161. internal = true;
  162. break;
  163. }
  164. /*
  165. * When source of clock is not internal or any PCM streams are running,
  166. * available sampling rate is limited at current sampling rate.
  167. */
  168. if (!internal ||
  169. amdtp_stream_pcm_running(&dice->tx_stream) ||
  170. amdtp_stream_pcm_running(&dice->rx_stream)) {
  171. err = snd_dice_transaction_get_rate(dice, &rate);
  172. if (err < 0)
  173. goto err_locked;
  174. substream->runtime->hw.rate_min = rate;
  175. substream->runtime->hw.rate_max = rate;
  176. }
  177. snd_pcm_set_sync(substream);
  178. end:
  179. return err;
  180. err_locked:
  181. snd_dice_stream_lock_release(dice);
  182. return err;
  183. }
  184. static int pcm_close(struct snd_pcm_substream *substream)
  185. {
  186. struct snd_dice *dice = substream->private_data;
  187. snd_dice_stream_lock_release(dice);
  188. return 0;
  189. }
  190. static int capture_hw_params(struct snd_pcm_substream *substream,
  191. struct snd_pcm_hw_params *hw_params)
  192. {
  193. struct snd_dice *dice = substream->private_data;
  194. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  195. mutex_lock(&dice->mutex);
  196. dice->substreams_counter++;
  197. mutex_unlock(&dice->mutex);
  198. }
  199. amdtp_stream_set_pcm_format(&dice->tx_stream,
  200. params_format(hw_params));
  201. return snd_pcm_lib_alloc_vmalloc_buffer(substream,
  202. params_buffer_bytes(hw_params));
  203. }
  204. static int playback_hw_params(struct snd_pcm_substream *substream,
  205. struct snd_pcm_hw_params *hw_params)
  206. {
  207. struct snd_dice *dice = substream->private_data;
  208. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  209. mutex_lock(&dice->mutex);
  210. dice->substreams_counter++;
  211. mutex_unlock(&dice->mutex);
  212. }
  213. amdtp_stream_set_pcm_format(&dice->rx_stream,
  214. params_format(hw_params));
  215. return snd_pcm_lib_alloc_vmalloc_buffer(substream,
  216. params_buffer_bytes(hw_params));
  217. }
  218. static int capture_hw_free(struct snd_pcm_substream *substream)
  219. {
  220. struct snd_dice *dice = substream->private_data;
  221. mutex_lock(&dice->mutex);
  222. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  223. dice->substreams_counter--;
  224. snd_dice_stream_stop_duplex(dice);
  225. mutex_unlock(&dice->mutex);
  226. return snd_pcm_lib_free_vmalloc_buffer(substream);
  227. }
  228. static int playback_hw_free(struct snd_pcm_substream *substream)
  229. {
  230. struct snd_dice *dice = substream->private_data;
  231. mutex_lock(&dice->mutex);
  232. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  233. dice->substreams_counter--;
  234. snd_dice_stream_stop_duplex(dice);
  235. mutex_unlock(&dice->mutex);
  236. return snd_pcm_lib_free_vmalloc_buffer(substream);
  237. }
  238. static int capture_prepare(struct snd_pcm_substream *substream)
  239. {
  240. struct snd_dice *dice = substream->private_data;
  241. int err;
  242. mutex_lock(&dice->mutex);
  243. err = snd_dice_stream_start_duplex(dice, substream->runtime->rate);
  244. mutex_unlock(&dice->mutex);
  245. if (err >= 0)
  246. amdtp_stream_pcm_prepare(&dice->tx_stream);
  247. return 0;
  248. }
  249. static int playback_prepare(struct snd_pcm_substream *substream)
  250. {
  251. struct snd_dice *dice = substream->private_data;
  252. int err;
  253. mutex_lock(&dice->mutex);
  254. err = snd_dice_stream_start_duplex(dice, substream->runtime->rate);
  255. mutex_unlock(&dice->mutex);
  256. if (err >= 0)
  257. amdtp_stream_pcm_prepare(&dice->rx_stream);
  258. return err;
  259. }
  260. static int capture_trigger(struct snd_pcm_substream *substream, int cmd)
  261. {
  262. struct snd_dice *dice = substream->private_data;
  263. switch (cmd) {
  264. case SNDRV_PCM_TRIGGER_START:
  265. amdtp_stream_pcm_trigger(&dice->tx_stream, substream);
  266. break;
  267. case SNDRV_PCM_TRIGGER_STOP:
  268. amdtp_stream_pcm_trigger(&dice->tx_stream, NULL);
  269. break;
  270. default:
  271. return -EINVAL;
  272. }
  273. return 0;
  274. }
  275. static int playback_trigger(struct snd_pcm_substream *substream, int cmd)
  276. {
  277. struct snd_dice *dice = substream->private_data;
  278. switch (cmd) {
  279. case SNDRV_PCM_TRIGGER_START:
  280. amdtp_stream_pcm_trigger(&dice->rx_stream, substream);
  281. break;
  282. case SNDRV_PCM_TRIGGER_STOP:
  283. amdtp_stream_pcm_trigger(&dice->rx_stream, NULL);
  284. break;
  285. default:
  286. return -EINVAL;
  287. }
  288. return 0;
  289. }
  290. static snd_pcm_uframes_t capture_pointer(struct snd_pcm_substream *substream)
  291. {
  292. struct snd_dice *dice = substream->private_data;
  293. return amdtp_stream_pcm_pointer(&dice->tx_stream);
  294. }
  295. static snd_pcm_uframes_t playback_pointer(struct snd_pcm_substream *substream)
  296. {
  297. struct snd_dice *dice = substream->private_data;
  298. return amdtp_stream_pcm_pointer(&dice->rx_stream);
  299. }
  300. int snd_dice_create_pcm(struct snd_dice *dice)
  301. {
  302. static struct snd_pcm_ops capture_ops = {
  303. .open = pcm_open,
  304. .close = pcm_close,
  305. .ioctl = snd_pcm_lib_ioctl,
  306. .hw_params = capture_hw_params,
  307. .hw_free = capture_hw_free,
  308. .prepare = capture_prepare,
  309. .trigger = capture_trigger,
  310. .pointer = capture_pointer,
  311. .page = snd_pcm_lib_get_vmalloc_page,
  312. .mmap = snd_pcm_lib_mmap_vmalloc,
  313. };
  314. static struct snd_pcm_ops playback_ops = {
  315. .open = pcm_open,
  316. .close = pcm_close,
  317. .ioctl = snd_pcm_lib_ioctl,
  318. .hw_params = playback_hw_params,
  319. .hw_free = playback_hw_free,
  320. .prepare = playback_prepare,
  321. .trigger = playback_trigger,
  322. .pointer = playback_pointer,
  323. .page = snd_pcm_lib_get_vmalloc_page,
  324. .mmap = snd_pcm_lib_mmap_vmalloc,
  325. };
  326. struct snd_pcm *pcm;
  327. unsigned int i, capture, playback;
  328. int err;
  329. capture = playback = 0;
  330. for (i = 0; i < 3; i++) {
  331. if (dice->tx_channels[i] > 0)
  332. capture = 1;
  333. if (dice->rx_channels[i] > 0)
  334. playback = 1;
  335. }
  336. err = snd_pcm_new(dice->card, "DICE", 0, playback, capture, &pcm);
  337. if (err < 0)
  338. return err;
  339. pcm->private_data = dice;
  340. strcpy(pcm->name, dice->card->shortname);
  341. if (capture > 0)
  342. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
  343. if (playback > 0)
  344. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
  345. return 0;
  346. }