ff-pcm.c 9.5 KB

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