dice-pcm.c 11 KB

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