ff-pcm.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 void limit_period_and_buffer(struct snd_pcm_hardware *hw)
  79. {
  80. hw->periods_min = 2; /* SNDRV_PCM_INFO_BATCH */
  81. hw->periods_max = UINT_MAX;
  82. hw->period_bytes_min = 4 * hw->channels_max; /* bytes for a frame */
  83. /* Just to prevent from allocating much pages. */
  84. hw->period_bytes_max = hw->period_bytes_min * 2048;
  85. hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
  86. }
  87. static int pcm_init_hw_params(struct snd_ff *ff,
  88. struct snd_pcm_substream *substream)
  89. {
  90. struct snd_pcm_runtime *runtime = substream->runtime;
  91. struct amdtp_stream *s;
  92. const unsigned int *pcm_channels;
  93. int err;
  94. runtime->hw.info = SNDRV_PCM_INFO_BATCH |
  95. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  96. SNDRV_PCM_INFO_INTERLEAVED |
  97. SNDRV_PCM_INFO_JOINT_DUPLEX |
  98. SNDRV_PCM_INFO_MMAP |
  99. SNDRV_PCM_INFO_MMAP_VALID;
  100. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  101. runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
  102. s = &ff->tx_stream;
  103. pcm_channels = ff->spec->pcm_capture_channels;
  104. } else {
  105. runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
  106. s = &ff->rx_stream;
  107. pcm_channels = ff->spec->pcm_playback_channels;
  108. }
  109. /* limit rates */
  110. limit_channels_and_rates(&runtime->hw, pcm_channels);
  111. limit_period_and_buffer(&runtime->hw);
  112. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  113. hw_rule_channels, (void *)pcm_channels,
  114. SNDRV_PCM_HW_PARAM_RATE, -1);
  115. if (err < 0)
  116. return err;
  117. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  118. hw_rule_rate, (void *)pcm_channels,
  119. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  120. if (err < 0)
  121. return err;
  122. return amdtp_ff_add_pcm_hw_constraints(s, runtime);
  123. }
  124. static int pcm_open(struct snd_pcm_substream *substream)
  125. {
  126. struct snd_ff *ff = substream->private_data;
  127. unsigned int rate;
  128. enum snd_ff_clock_src src;
  129. int i, err;
  130. err = pcm_init_hw_params(ff, substream);
  131. if (err < 0)
  132. return err;
  133. err = ff->spec->protocol->get_clock(ff, &rate, &src);
  134. if (err < 0)
  135. return err;
  136. if (src != SND_FF_CLOCK_SRC_INTERNAL) {
  137. for (i = 0; i < CIP_SFC_COUNT; ++i) {
  138. if (amdtp_rate_table[i] == rate)
  139. break;
  140. }
  141. /*
  142. * The unit is configured at sampling frequency which packet
  143. * streaming engine can't support.
  144. */
  145. if (i >= CIP_SFC_COUNT)
  146. return -EIO;
  147. substream->runtime->hw.rate_min = rate;
  148. substream->runtime->hw.rate_max = rate;
  149. } else {
  150. if (amdtp_stream_pcm_running(&ff->rx_stream) ||
  151. amdtp_stream_pcm_running(&ff->tx_stream)) {
  152. rate = amdtp_rate_table[ff->rx_stream.sfc];
  153. substream->runtime->hw.rate_min = rate;
  154. substream->runtime->hw.rate_max = rate;
  155. }
  156. }
  157. snd_pcm_set_sync(substream);
  158. return 0;
  159. }
  160. static int pcm_close(struct snd_pcm_substream *substream)
  161. {
  162. return 0;
  163. }
  164. static int pcm_capture_hw_params(struct snd_pcm_substream *substream,
  165. struct snd_pcm_hw_params *hw_params)
  166. {
  167. struct snd_ff *ff = substream->private_data;
  168. int err;
  169. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  170. params_buffer_bytes(hw_params));
  171. if (err < 0)
  172. return err;
  173. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  174. mutex_lock(&ff->mutex);
  175. ff->substreams_counter++;
  176. mutex_unlock(&ff->mutex);
  177. }
  178. return 0;
  179. }
  180. static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
  181. struct snd_pcm_hw_params *hw_params)
  182. {
  183. struct snd_ff *ff = substream->private_data;
  184. int err;
  185. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  186. params_buffer_bytes(hw_params));
  187. if (err < 0)
  188. return err;
  189. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  190. mutex_lock(&ff->mutex);
  191. ff->substreams_counter++;
  192. mutex_unlock(&ff->mutex);
  193. }
  194. return 0;
  195. }
  196. static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
  197. {
  198. struct snd_ff *ff = substream->private_data;
  199. mutex_lock(&ff->mutex);
  200. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  201. ff->substreams_counter--;
  202. snd_ff_stream_stop_duplex(ff);
  203. mutex_unlock(&ff->mutex);
  204. return snd_pcm_lib_free_vmalloc_buffer(substream);
  205. }
  206. static int pcm_playback_hw_free(struct snd_pcm_substream *substream)
  207. {
  208. struct snd_ff *ff = substream->private_data;
  209. mutex_lock(&ff->mutex);
  210. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  211. ff->substreams_counter--;
  212. snd_ff_stream_stop_duplex(ff);
  213. mutex_unlock(&ff->mutex);
  214. return snd_pcm_lib_free_vmalloc_buffer(substream);
  215. }
  216. static int pcm_capture_prepare(struct snd_pcm_substream *substream)
  217. {
  218. struct snd_ff *ff = substream->private_data;
  219. struct snd_pcm_runtime *runtime = substream->runtime;
  220. int err;
  221. mutex_lock(&ff->mutex);
  222. err = snd_ff_stream_start_duplex(ff, runtime->rate);
  223. if (err >= 0)
  224. amdtp_stream_pcm_prepare(&ff->tx_stream);
  225. mutex_unlock(&ff->mutex);
  226. return err;
  227. }
  228. static int pcm_playback_prepare(struct snd_pcm_substream *substream)
  229. {
  230. struct snd_ff *ff = substream->private_data;
  231. struct snd_pcm_runtime *runtime = substream->runtime;
  232. int err;
  233. mutex_lock(&ff->mutex);
  234. err = snd_ff_stream_start_duplex(ff, runtime->rate);
  235. if (err >= 0)
  236. amdtp_stream_pcm_prepare(&ff->rx_stream);
  237. mutex_unlock(&ff->mutex);
  238. return err;
  239. }
  240. static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  241. {
  242. struct snd_ff *ff = substream->private_data;
  243. switch (cmd) {
  244. case SNDRV_PCM_TRIGGER_START:
  245. amdtp_stream_pcm_trigger(&ff->tx_stream, substream);
  246. break;
  247. case SNDRV_PCM_TRIGGER_STOP:
  248. amdtp_stream_pcm_trigger(&ff->tx_stream, NULL);
  249. break;
  250. default:
  251. return -EINVAL;
  252. }
  253. return 0;
  254. }
  255. static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  256. {
  257. struct snd_ff *ff = substream->private_data;
  258. switch (cmd) {
  259. case SNDRV_PCM_TRIGGER_START:
  260. amdtp_stream_pcm_trigger(&ff->rx_stream, substream);
  261. break;
  262. case SNDRV_PCM_TRIGGER_STOP:
  263. amdtp_stream_pcm_trigger(&ff->rx_stream, NULL);
  264. break;
  265. default:
  266. return -EINVAL;
  267. }
  268. return 0;
  269. }
  270. static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
  271. {
  272. struct snd_ff *ff = sbstrm->private_data;
  273. return amdtp_stream_pcm_pointer(&ff->tx_stream);
  274. }
  275. static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
  276. {
  277. struct snd_ff *ff = sbstrm->private_data;
  278. return amdtp_stream_pcm_pointer(&ff->rx_stream);
  279. }
  280. static struct snd_pcm_ops pcm_capture_ops = {
  281. .open = pcm_open,
  282. .close = pcm_close,
  283. .ioctl = snd_pcm_lib_ioctl,
  284. .hw_params = pcm_capture_hw_params,
  285. .hw_free = pcm_capture_hw_free,
  286. .prepare = pcm_capture_prepare,
  287. .trigger = pcm_capture_trigger,
  288. .pointer = pcm_capture_pointer,
  289. .page = snd_pcm_lib_get_vmalloc_page,
  290. };
  291. static struct snd_pcm_ops pcm_playback_ops = {
  292. .open = pcm_open,
  293. .close = pcm_close,
  294. .ioctl = snd_pcm_lib_ioctl,
  295. .hw_params = pcm_playback_hw_params,
  296. .hw_free = pcm_playback_hw_free,
  297. .prepare = pcm_playback_prepare,
  298. .trigger = pcm_playback_trigger,
  299. .pointer = pcm_playback_pointer,
  300. .page = snd_pcm_lib_get_vmalloc_page,
  301. .mmap = snd_pcm_lib_mmap_vmalloc,
  302. };
  303. int snd_ff_create_pcm_devices(struct snd_ff *ff)
  304. {
  305. struct snd_pcm *pcm;
  306. int err;
  307. err = snd_pcm_new(ff->card, ff->card->driver, 0, 1, 1, &pcm);
  308. if (err < 0)
  309. return err;
  310. pcm->private_data = ff;
  311. snprintf(pcm->name, sizeof(pcm->name),
  312. "%s PCM", ff->card->shortname);
  313. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops);
  314. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);
  315. return 0;
  316. }