ff-pcm.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ff-pcm.c - a part of driver for RME Fireface series
  4. *
  5. * Copyright (c) 2015-2017 Takashi Sakamoto
  6. */
  7. #include "ff.h"
  8. static int hw_rule_rate(struct snd_pcm_hw_params *params,
  9. struct snd_pcm_hw_rule *rule)
  10. {
  11. const unsigned int *pcm_channels = rule->private;
  12. struct snd_interval *r =
  13. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  14. const struct snd_interval *c =
  15. hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  16. struct snd_interval t = {
  17. .min = UINT_MAX, .max = 0, .integer = 1
  18. };
  19. unsigned int i;
  20. for (i = 0; i < ARRAY_SIZE(amdtp_rate_table); i++) {
  21. enum snd_ff_stream_mode mode;
  22. int err;
  23. err = snd_ff_stream_get_multiplier_mode(i, &mode);
  24. if (err < 0)
  25. continue;
  26. if (!snd_interval_test(c, pcm_channels[mode]))
  27. continue;
  28. t.min = min(t.min, amdtp_rate_table[i]);
  29. t.max = max(t.max, amdtp_rate_table[i]);
  30. }
  31. return snd_interval_refine(r, &t);
  32. }
  33. static int hw_rule_channels(struct snd_pcm_hw_params *params,
  34. struct snd_pcm_hw_rule *rule)
  35. {
  36. const unsigned int *pcm_channels = rule->private;
  37. struct snd_interval *c =
  38. hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  39. const struct snd_interval *r =
  40. hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
  41. struct snd_interval t = {
  42. .min = UINT_MAX, .max = 0, .integer = 1
  43. };
  44. unsigned int i;
  45. for (i = 0; i < ARRAY_SIZE(amdtp_rate_table); i++) {
  46. enum snd_ff_stream_mode mode;
  47. int err;
  48. err = snd_ff_stream_get_multiplier_mode(i, &mode);
  49. if (err < 0)
  50. continue;
  51. if (!snd_interval_test(r, amdtp_rate_table[i]))
  52. continue;
  53. t.min = min(t.min, pcm_channels[mode]);
  54. t.max = max(t.max, pcm_channels[mode]);
  55. }
  56. return snd_interval_refine(c, &t);
  57. }
  58. static void limit_channels_and_rates(struct snd_pcm_hardware *hw,
  59. const unsigned int *pcm_channels)
  60. {
  61. unsigned int rate, channels;
  62. int i;
  63. hw->channels_min = UINT_MAX;
  64. hw->channels_max = 0;
  65. hw->rate_min = UINT_MAX;
  66. hw->rate_max = 0;
  67. for (i = 0; i < ARRAY_SIZE(amdtp_rate_table); i++) {
  68. enum snd_ff_stream_mode mode;
  69. int err;
  70. err = snd_ff_stream_get_multiplier_mode(i, &mode);
  71. if (err < 0)
  72. continue;
  73. channels = pcm_channels[mode];
  74. if (pcm_channels[mode] == 0)
  75. continue;
  76. hw->channels_min = min(hw->channels_min, channels);
  77. hw->channels_max = max(hw->channels_max, channels);
  78. rate = amdtp_rate_table[i];
  79. hw->rates |= snd_pcm_rate_to_rate_bit(rate);
  80. hw->rate_min = min(hw->rate_min, rate);
  81. hw->rate_max = max(hw->rate_max, rate);
  82. }
  83. }
  84. static int pcm_init_hw_params(struct snd_ff *ff,
  85. struct snd_pcm_substream *substream)
  86. {
  87. struct snd_pcm_runtime *runtime = substream->runtime;
  88. struct amdtp_stream *s;
  89. const unsigned int *pcm_channels;
  90. int err;
  91. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  92. runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
  93. s = &ff->tx_stream;
  94. pcm_channels = ff->spec->pcm_capture_channels;
  95. } else {
  96. runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
  97. s = &ff->rx_stream;
  98. pcm_channels = ff->spec->pcm_playback_channels;
  99. }
  100. limit_channels_and_rates(&runtime->hw, pcm_channels);
  101. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  102. hw_rule_channels, (void *)pcm_channels,
  103. SNDRV_PCM_HW_PARAM_RATE, -1);
  104. if (err < 0)
  105. return err;
  106. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  107. hw_rule_rate, (void *)pcm_channels,
  108. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  109. if (err < 0)
  110. return err;
  111. return amdtp_ff_add_pcm_hw_constraints(s, runtime);
  112. }
  113. static int pcm_open(struct snd_pcm_substream *substream)
  114. {
  115. struct snd_ff *ff = substream->private_data;
  116. struct amdtp_domain *d = &ff->domain;
  117. unsigned int rate;
  118. enum snd_ff_clock_src src;
  119. int i, err;
  120. err = snd_ff_stream_lock_try(ff);
  121. if (err < 0)
  122. return err;
  123. err = pcm_init_hw_params(ff, substream);
  124. if (err < 0)
  125. goto release_lock;
  126. err = ff->spec->protocol->get_clock(ff, &rate, &src);
  127. if (err < 0)
  128. goto release_lock;
  129. mutex_lock(&ff->mutex);
  130. // When source of clock is not internal or any stream is reserved for
  131. // transmission of PCM frames, the available sampling rate is limited
  132. // at current one.
  133. if (src != SND_FF_CLOCK_SRC_INTERNAL) {
  134. for (i = 0; i < CIP_SFC_COUNT; ++i) {
  135. if (amdtp_rate_table[i] == rate)
  136. break;
  137. }
  138. // The unit is configured at sampling frequency which packet
  139. // streaming engine can't support.
  140. if (i >= CIP_SFC_COUNT) {
  141. mutex_unlock(&ff->mutex);
  142. err = -EIO;
  143. goto release_lock;
  144. }
  145. substream->runtime->hw.rate_min = rate;
  146. substream->runtime->hw.rate_max = rate;
  147. } else {
  148. if (ff->substreams_counter > 0) {
  149. unsigned int frames_per_period = d->events_per_period;
  150. unsigned int frames_per_buffer = d->events_per_buffer;
  151. rate = amdtp_rate_table[ff->rx_stream.sfc];
  152. substream->runtime->hw.rate_min = rate;
  153. substream->runtime->hw.rate_max = rate;
  154. err = snd_pcm_hw_constraint_minmax(substream->runtime,
  155. SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  156. frames_per_period, frames_per_period);
  157. if (err < 0) {
  158. mutex_unlock(&ff->mutex);
  159. goto release_lock;
  160. }
  161. err = snd_pcm_hw_constraint_minmax(substream->runtime,
  162. SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  163. frames_per_buffer, frames_per_buffer);
  164. if (err < 0) {
  165. mutex_unlock(&ff->mutex);
  166. goto release_lock;
  167. }
  168. }
  169. }
  170. mutex_unlock(&ff->mutex);
  171. snd_pcm_set_sync(substream);
  172. return 0;
  173. release_lock:
  174. snd_ff_stream_lock_release(ff);
  175. return err;
  176. }
  177. static int pcm_close(struct snd_pcm_substream *substream)
  178. {
  179. struct snd_ff *ff = substream->private_data;
  180. snd_ff_stream_lock_release(ff);
  181. return 0;
  182. }
  183. static int pcm_hw_params(struct snd_pcm_substream *substream,
  184. struct snd_pcm_hw_params *hw_params)
  185. {
  186. struct snd_ff *ff = substream->private_data;
  187. int err = 0;
  188. if (substream->runtime->state == SNDRV_PCM_STATE_OPEN) {
  189. unsigned int rate = params_rate(hw_params);
  190. unsigned int frames_per_period = params_period_size(hw_params);
  191. unsigned int frames_per_buffer = params_buffer_size(hw_params);
  192. mutex_lock(&ff->mutex);
  193. err = snd_ff_stream_reserve_duplex(ff, rate, frames_per_period,
  194. frames_per_buffer);
  195. if (err >= 0)
  196. ++ff->substreams_counter;
  197. mutex_unlock(&ff->mutex);
  198. }
  199. return err;
  200. }
  201. static int pcm_hw_free(struct snd_pcm_substream *substream)
  202. {
  203. struct snd_ff *ff = substream->private_data;
  204. mutex_lock(&ff->mutex);
  205. if (substream->runtime->state != SNDRV_PCM_STATE_OPEN)
  206. --ff->substreams_counter;
  207. snd_ff_stream_stop_duplex(ff);
  208. mutex_unlock(&ff->mutex);
  209. return 0;
  210. }
  211. static int pcm_capture_prepare(struct snd_pcm_substream *substream)
  212. {
  213. struct snd_ff *ff = substream->private_data;
  214. struct snd_pcm_runtime *runtime = substream->runtime;
  215. int err;
  216. mutex_lock(&ff->mutex);
  217. err = snd_ff_stream_start_duplex(ff, runtime->rate);
  218. if (err >= 0)
  219. amdtp_stream_pcm_prepare(&ff->tx_stream);
  220. mutex_unlock(&ff->mutex);
  221. return err;
  222. }
  223. static int pcm_playback_prepare(struct snd_pcm_substream *substream)
  224. {
  225. struct snd_ff *ff = substream->private_data;
  226. struct snd_pcm_runtime *runtime = substream->runtime;
  227. int err;
  228. mutex_lock(&ff->mutex);
  229. err = snd_ff_stream_start_duplex(ff, runtime->rate);
  230. if (err >= 0)
  231. amdtp_stream_pcm_prepare(&ff->rx_stream);
  232. mutex_unlock(&ff->mutex);
  233. return err;
  234. }
  235. static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  236. {
  237. struct snd_ff *ff = substream->private_data;
  238. switch (cmd) {
  239. case SNDRV_PCM_TRIGGER_START:
  240. amdtp_stream_pcm_trigger(&ff->tx_stream, substream);
  241. break;
  242. case SNDRV_PCM_TRIGGER_STOP:
  243. amdtp_stream_pcm_trigger(&ff->tx_stream, NULL);
  244. break;
  245. default:
  246. return -EINVAL;
  247. }
  248. return 0;
  249. }
  250. static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  251. {
  252. struct snd_ff *ff = substream->private_data;
  253. switch (cmd) {
  254. case SNDRV_PCM_TRIGGER_START:
  255. amdtp_stream_pcm_trigger(&ff->rx_stream, substream);
  256. break;
  257. case SNDRV_PCM_TRIGGER_STOP:
  258. amdtp_stream_pcm_trigger(&ff->rx_stream, NULL);
  259. break;
  260. default:
  261. return -EINVAL;
  262. }
  263. return 0;
  264. }
  265. static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
  266. {
  267. struct snd_ff *ff = sbstrm->private_data;
  268. return amdtp_domain_stream_pcm_pointer(&ff->domain, &ff->tx_stream);
  269. }
  270. static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
  271. {
  272. struct snd_ff *ff = sbstrm->private_data;
  273. return amdtp_domain_stream_pcm_pointer(&ff->domain, &ff->rx_stream);
  274. }
  275. static int pcm_capture_ack(struct snd_pcm_substream *substream)
  276. {
  277. struct snd_ff *ff = substream->private_data;
  278. return amdtp_domain_stream_pcm_ack(&ff->domain, &ff->tx_stream);
  279. }
  280. static int pcm_playback_ack(struct snd_pcm_substream *substream)
  281. {
  282. struct snd_ff *ff = substream->private_data;
  283. return amdtp_domain_stream_pcm_ack(&ff->domain, &ff->rx_stream);
  284. }
  285. int snd_ff_create_pcm_devices(struct snd_ff *ff)
  286. {
  287. static const struct snd_pcm_ops pcm_capture_ops = {
  288. .open = pcm_open,
  289. .close = pcm_close,
  290. .hw_params = pcm_hw_params,
  291. .hw_free = pcm_hw_free,
  292. .prepare = pcm_capture_prepare,
  293. .trigger = pcm_capture_trigger,
  294. .pointer = pcm_capture_pointer,
  295. .ack = pcm_capture_ack,
  296. };
  297. static const struct snd_pcm_ops pcm_playback_ops = {
  298. .open = pcm_open,
  299. .close = pcm_close,
  300. .hw_params = pcm_hw_params,
  301. .hw_free = pcm_hw_free,
  302. .prepare = pcm_playback_prepare,
  303. .trigger = pcm_playback_trigger,
  304. .pointer = pcm_playback_pointer,
  305. .ack = pcm_playback_ack,
  306. };
  307. struct snd_pcm *pcm;
  308. int err;
  309. err = snd_pcm_new(ff->card, ff->card->driver, 0, 1, 1, &pcm);
  310. if (err < 0)
  311. return err;
  312. pcm->private_data = ff;
  313. pcm->nonatomic = true;
  314. snprintf(pcm->name, sizeof(pcm->name),
  315. "%s PCM", ff->card->shortname);
  316. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops);
  317. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);
  318. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
  319. return 0;
  320. }