dice-pcm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 = AM824_IN_PCM_FORMAT_BITS;
  111. stream = &dice->tx_stream;
  112. pcm_channels = dice->tx_channels;
  113. } else {
  114. hw->formats = AM824_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_am824_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. int err;
  195. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  196. params_buffer_bytes(hw_params));
  197. if (err < 0)
  198. return err;
  199. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  200. mutex_lock(&dice->mutex);
  201. dice->substreams_counter++;
  202. mutex_unlock(&dice->mutex);
  203. }
  204. amdtp_am824_set_pcm_format(&dice->tx_stream, params_format(hw_params));
  205. return 0;
  206. }
  207. static int playback_hw_params(struct snd_pcm_substream *substream,
  208. struct snd_pcm_hw_params *hw_params)
  209. {
  210. struct snd_dice *dice = substream->private_data;
  211. int err;
  212. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  213. params_buffer_bytes(hw_params));
  214. if (err < 0)
  215. return err;
  216. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  217. mutex_lock(&dice->mutex);
  218. dice->substreams_counter++;
  219. mutex_unlock(&dice->mutex);
  220. }
  221. amdtp_am824_set_pcm_format(&dice->rx_stream, params_format(hw_params));
  222. return 0;
  223. }
  224. static int capture_hw_free(struct snd_pcm_substream *substream)
  225. {
  226. struct snd_dice *dice = substream->private_data;
  227. mutex_lock(&dice->mutex);
  228. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  229. dice->substreams_counter--;
  230. snd_dice_stream_stop_duplex(dice);
  231. mutex_unlock(&dice->mutex);
  232. return snd_pcm_lib_free_vmalloc_buffer(substream);
  233. }
  234. static int playback_hw_free(struct snd_pcm_substream *substream)
  235. {
  236. struct snd_dice *dice = substream->private_data;
  237. mutex_lock(&dice->mutex);
  238. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  239. dice->substreams_counter--;
  240. snd_dice_stream_stop_duplex(dice);
  241. mutex_unlock(&dice->mutex);
  242. return snd_pcm_lib_free_vmalloc_buffer(substream);
  243. }
  244. static int capture_prepare(struct snd_pcm_substream *substream)
  245. {
  246. struct snd_dice *dice = substream->private_data;
  247. int err;
  248. mutex_lock(&dice->mutex);
  249. err = snd_dice_stream_start_duplex(dice, substream->runtime->rate);
  250. mutex_unlock(&dice->mutex);
  251. if (err >= 0)
  252. amdtp_stream_pcm_prepare(&dice->tx_stream);
  253. return 0;
  254. }
  255. static int playback_prepare(struct snd_pcm_substream *substream)
  256. {
  257. struct snd_dice *dice = substream->private_data;
  258. int err;
  259. mutex_lock(&dice->mutex);
  260. err = snd_dice_stream_start_duplex(dice, substream->runtime->rate);
  261. mutex_unlock(&dice->mutex);
  262. if (err >= 0)
  263. amdtp_stream_pcm_prepare(&dice->rx_stream);
  264. return err;
  265. }
  266. static int capture_trigger(struct snd_pcm_substream *substream, int cmd)
  267. {
  268. struct snd_dice *dice = substream->private_data;
  269. switch (cmd) {
  270. case SNDRV_PCM_TRIGGER_START:
  271. amdtp_stream_pcm_trigger(&dice->tx_stream, substream);
  272. break;
  273. case SNDRV_PCM_TRIGGER_STOP:
  274. amdtp_stream_pcm_trigger(&dice->tx_stream, NULL);
  275. break;
  276. default:
  277. return -EINVAL;
  278. }
  279. return 0;
  280. }
  281. static int playback_trigger(struct snd_pcm_substream *substream, int cmd)
  282. {
  283. struct snd_dice *dice = substream->private_data;
  284. switch (cmd) {
  285. case SNDRV_PCM_TRIGGER_START:
  286. amdtp_stream_pcm_trigger(&dice->rx_stream, substream);
  287. break;
  288. case SNDRV_PCM_TRIGGER_STOP:
  289. amdtp_stream_pcm_trigger(&dice->rx_stream, NULL);
  290. break;
  291. default:
  292. return -EINVAL;
  293. }
  294. return 0;
  295. }
  296. static snd_pcm_uframes_t capture_pointer(struct snd_pcm_substream *substream)
  297. {
  298. struct snd_dice *dice = substream->private_data;
  299. return amdtp_stream_pcm_pointer(&dice->tx_stream);
  300. }
  301. static snd_pcm_uframes_t playback_pointer(struct snd_pcm_substream *substream)
  302. {
  303. struct snd_dice *dice = substream->private_data;
  304. return amdtp_stream_pcm_pointer(&dice->rx_stream);
  305. }
  306. int snd_dice_create_pcm(struct snd_dice *dice)
  307. {
  308. static struct snd_pcm_ops capture_ops = {
  309. .open = pcm_open,
  310. .close = pcm_close,
  311. .ioctl = snd_pcm_lib_ioctl,
  312. .hw_params = capture_hw_params,
  313. .hw_free = capture_hw_free,
  314. .prepare = capture_prepare,
  315. .trigger = capture_trigger,
  316. .pointer = capture_pointer,
  317. .page = snd_pcm_lib_get_vmalloc_page,
  318. .mmap = snd_pcm_lib_mmap_vmalloc,
  319. };
  320. static struct snd_pcm_ops playback_ops = {
  321. .open = pcm_open,
  322. .close = pcm_close,
  323. .ioctl = snd_pcm_lib_ioctl,
  324. .hw_params = playback_hw_params,
  325. .hw_free = playback_hw_free,
  326. .prepare = playback_prepare,
  327. .trigger = playback_trigger,
  328. .pointer = playback_pointer,
  329. .page = snd_pcm_lib_get_vmalloc_page,
  330. .mmap = snd_pcm_lib_mmap_vmalloc,
  331. };
  332. struct snd_pcm *pcm;
  333. unsigned int i, capture, playback;
  334. int err;
  335. capture = playback = 0;
  336. for (i = 0; i < 3; i++) {
  337. if (dice->tx_channels[i] > 0)
  338. capture = 1;
  339. if (dice->rx_channels[i] > 0)
  340. playback = 1;
  341. }
  342. err = snd_pcm_new(dice->card, "DICE", 0, playback, capture, &pcm);
  343. if (err < 0)
  344. return err;
  345. pcm->private_data = dice;
  346. strcpy(pcm->name, dice->card->shortname);
  347. if (capture > 0)
  348. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
  349. if (playback > 0)
  350. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
  351. return 0;
  352. }