motu-pcm.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * motu-pcm.c - a part of driver for MOTU FireWire series
  3. *
  4. * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include <sound/pcm_params.h>
  9. #include "motu.h"
  10. static int motu_rate_constraint(struct snd_pcm_hw_params *params,
  11. struct snd_pcm_hw_rule *rule)
  12. {
  13. struct snd_motu_packet_format *formats = rule->private;
  14. const struct snd_interval *c =
  15. hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  16. struct snd_interval *r =
  17. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  18. struct snd_interval rates = {
  19. .min = UINT_MAX, .max = 0, .integer = 1
  20. };
  21. unsigned int i, pcm_channels, rate, mode;
  22. for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) {
  23. rate = snd_motu_clock_rates[i];
  24. mode = i / 2;
  25. pcm_channels = formats->fixed_part_pcm_chunks[mode] +
  26. formats->differed_part_pcm_chunks[mode];
  27. if (!snd_interval_test(c, pcm_channels))
  28. continue;
  29. rates.min = min(rates.min, rate);
  30. rates.max = max(rates.max, rate);
  31. }
  32. return snd_interval_refine(r, &rates);
  33. }
  34. static int motu_channels_constraint(struct snd_pcm_hw_params *params,
  35. struct snd_pcm_hw_rule *rule)
  36. {
  37. struct snd_motu_packet_format *formats = rule->private;
  38. const struct snd_interval *r =
  39. hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
  40. struct snd_interval *c =
  41. hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  42. struct snd_interval channels = {
  43. .min = UINT_MAX, .max = 0, .integer = 1
  44. };
  45. unsigned int i, pcm_channels, rate, mode;
  46. for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) {
  47. rate = snd_motu_clock_rates[i];
  48. mode = i / 2;
  49. if (!snd_interval_test(r, rate))
  50. continue;
  51. pcm_channels = formats->fixed_part_pcm_chunks[mode] +
  52. formats->differed_part_pcm_chunks[mode];
  53. channels.min = min(channels.min, pcm_channels);
  54. channels.max = max(channels.max, pcm_channels);
  55. }
  56. return snd_interval_refine(c, &channels);
  57. }
  58. static void limit_channels_and_rates(struct snd_motu *motu,
  59. struct snd_pcm_runtime *runtime,
  60. struct snd_motu_packet_format *formats)
  61. {
  62. struct snd_pcm_hardware *hw = &runtime->hw;
  63. unsigned int i, pcm_channels, rate, mode;
  64. hw->channels_min = UINT_MAX;
  65. hw->channels_max = 0;
  66. for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) {
  67. rate = snd_motu_clock_rates[i];
  68. mode = i / 2;
  69. pcm_channels = formats->fixed_part_pcm_chunks[mode] +
  70. formats->differed_part_pcm_chunks[mode];
  71. if (pcm_channels == 0)
  72. continue;
  73. hw->rates |= snd_pcm_rate_to_rate_bit(rate);
  74. hw->channels_min = min(hw->channels_min, pcm_channels);
  75. hw->channels_max = max(hw->channels_max, pcm_channels);
  76. }
  77. snd_pcm_limit_hw_rates(runtime);
  78. }
  79. static int init_hw_info(struct snd_motu *motu,
  80. struct snd_pcm_substream *substream)
  81. {
  82. struct snd_pcm_runtime *runtime = substream->runtime;
  83. struct snd_pcm_hardware *hw = &runtime->hw;
  84. struct amdtp_stream *stream;
  85. struct snd_motu_packet_format *formats;
  86. int err;
  87. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  88. hw->formats = SNDRV_PCM_FMTBIT_S32;
  89. stream = &motu->tx_stream;
  90. formats = &motu->tx_packet_formats;
  91. } else {
  92. hw->formats = SNDRV_PCM_FMTBIT_S32;
  93. stream = &motu->rx_stream;
  94. formats = &motu->rx_packet_formats;
  95. }
  96. limit_channels_and_rates(motu, runtime, formats);
  97. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  98. motu_rate_constraint, formats,
  99. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  100. if (err < 0)
  101. return err;
  102. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  103. motu_channels_constraint, formats,
  104. SNDRV_PCM_HW_PARAM_RATE, -1);
  105. if (err < 0)
  106. return err;
  107. return amdtp_motu_add_pcm_hw_constraints(stream, runtime);
  108. }
  109. static int pcm_open(struct snd_pcm_substream *substream)
  110. {
  111. struct snd_motu *motu = substream->private_data;
  112. const struct snd_motu_protocol *const protocol = motu->spec->protocol;
  113. enum snd_motu_clock_source src;
  114. unsigned int rate;
  115. int err;
  116. err = snd_motu_stream_lock_try(motu);
  117. if (err < 0)
  118. return err;
  119. mutex_lock(&motu->mutex);
  120. err = snd_motu_stream_cache_packet_formats(motu);
  121. if (err < 0)
  122. goto err_locked;
  123. err = init_hw_info(motu, substream);
  124. if (err < 0)
  125. goto err_locked;
  126. /*
  127. * When source of clock is not internal or any PCM streams are running,
  128. * available sampling rate is limited at current sampling rate.
  129. */
  130. err = protocol->get_clock_source(motu, &src);
  131. if (err < 0)
  132. goto err_locked;
  133. if (src != SND_MOTU_CLOCK_SOURCE_INTERNAL ||
  134. amdtp_stream_pcm_running(&motu->tx_stream) ||
  135. amdtp_stream_pcm_running(&motu->rx_stream)) {
  136. err = protocol->get_clock_rate(motu, &rate);
  137. if (err < 0)
  138. goto err_locked;
  139. substream->runtime->hw.rate_min = rate;
  140. substream->runtime->hw.rate_max = rate;
  141. }
  142. snd_pcm_set_sync(substream);
  143. mutex_unlock(&motu->mutex);
  144. return err;
  145. err_locked:
  146. mutex_unlock(&motu->mutex);
  147. snd_motu_stream_lock_release(motu);
  148. return err;
  149. }
  150. static int pcm_close(struct snd_pcm_substream *substream)
  151. {
  152. struct snd_motu *motu = substream->private_data;
  153. snd_motu_stream_lock_release(motu);
  154. return 0;
  155. }
  156. static int capture_hw_params(struct snd_pcm_substream *substream,
  157. struct snd_pcm_hw_params *hw_params)
  158. {
  159. struct snd_motu *motu = substream->private_data;
  160. int err;
  161. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  162. params_buffer_bytes(hw_params));
  163. if (err < 0)
  164. return err;
  165. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  166. mutex_lock(&motu->mutex);
  167. motu->capture_substreams++;
  168. mutex_unlock(&motu->mutex);
  169. }
  170. return 0;
  171. }
  172. static int playback_hw_params(struct snd_pcm_substream *substream,
  173. struct snd_pcm_hw_params *hw_params)
  174. {
  175. struct snd_motu *motu = substream->private_data;
  176. int err;
  177. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  178. params_buffer_bytes(hw_params));
  179. if (err < 0)
  180. return err;
  181. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  182. mutex_lock(&motu->mutex);
  183. motu->playback_substreams++;
  184. mutex_unlock(&motu->mutex);
  185. }
  186. return 0;
  187. }
  188. static int capture_hw_free(struct snd_pcm_substream *substream)
  189. {
  190. struct snd_motu *motu = substream->private_data;
  191. mutex_lock(&motu->mutex);
  192. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  193. motu->capture_substreams--;
  194. snd_motu_stream_stop_duplex(motu);
  195. mutex_unlock(&motu->mutex);
  196. return snd_pcm_lib_free_vmalloc_buffer(substream);
  197. }
  198. static int playback_hw_free(struct snd_pcm_substream *substream)
  199. {
  200. struct snd_motu *motu = substream->private_data;
  201. mutex_lock(&motu->mutex);
  202. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  203. motu->playback_substreams--;
  204. snd_motu_stream_stop_duplex(motu);
  205. mutex_unlock(&motu->mutex);
  206. return snd_pcm_lib_free_vmalloc_buffer(substream);
  207. }
  208. static int capture_prepare(struct snd_pcm_substream *substream)
  209. {
  210. struct snd_motu *motu = substream->private_data;
  211. int err;
  212. mutex_lock(&motu->mutex);
  213. err = snd_motu_stream_start_duplex(motu, substream->runtime->rate);
  214. mutex_unlock(&motu->mutex);
  215. if (err >= 0)
  216. amdtp_stream_pcm_prepare(&motu->tx_stream);
  217. return 0;
  218. }
  219. static int playback_prepare(struct snd_pcm_substream *substream)
  220. {
  221. struct snd_motu *motu = substream->private_data;
  222. int err;
  223. mutex_lock(&motu->mutex);
  224. err = snd_motu_stream_start_duplex(motu, substream->runtime->rate);
  225. mutex_unlock(&motu->mutex);
  226. if (err >= 0)
  227. amdtp_stream_pcm_prepare(&motu->rx_stream);
  228. return err;
  229. }
  230. static int capture_trigger(struct snd_pcm_substream *substream, int cmd)
  231. {
  232. struct snd_motu *motu = substream->private_data;
  233. switch (cmd) {
  234. case SNDRV_PCM_TRIGGER_START:
  235. amdtp_stream_pcm_trigger(&motu->tx_stream, substream);
  236. break;
  237. case SNDRV_PCM_TRIGGER_STOP:
  238. amdtp_stream_pcm_trigger(&motu->tx_stream, NULL);
  239. break;
  240. default:
  241. return -EINVAL;
  242. }
  243. return 0;
  244. }
  245. static int playback_trigger(struct snd_pcm_substream *substream, int cmd)
  246. {
  247. struct snd_motu *motu = substream->private_data;
  248. switch (cmd) {
  249. case SNDRV_PCM_TRIGGER_START:
  250. amdtp_stream_pcm_trigger(&motu->rx_stream, substream);
  251. break;
  252. case SNDRV_PCM_TRIGGER_STOP:
  253. amdtp_stream_pcm_trigger(&motu->rx_stream, NULL);
  254. break;
  255. default:
  256. return -EINVAL;
  257. }
  258. return 0;
  259. }
  260. static snd_pcm_uframes_t capture_pointer(struct snd_pcm_substream *substream)
  261. {
  262. struct snd_motu *motu = substream->private_data;
  263. return amdtp_stream_pcm_pointer(&motu->tx_stream);
  264. }
  265. static snd_pcm_uframes_t playback_pointer(struct snd_pcm_substream *substream)
  266. {
  267. struct snd_motu *motu = substream->private_data;
  268. return amdtp_stream_pcm_pointer(&motu->rx_stream);
  269. }
  270. static int capture_ack(struct snd_pcm_substream *substream)
  271. {
  272. struct snd_motu *motu = substream->private_data;
  273. return amdtp_stream_pcm_ack(&motu->tx_stream);
  274. }
  275. static int playback_ack(struct snd_pcm_substream *substream)
  276. {
  277. struct snd_motu *motu = substream->private_data;
  278. return amdtp_stream_pcm_ack(&motu->rx_stream);
  279. }
  280. int snd_motu_create_pcm_devices(struct snd_motu *motu)
  281. {
  282. static const struct snd_pcm_ops capture_ops = {
  283. .open = pcm_open,
  284. .close = pcm_close,
  285. .ioctl = snd_pcm_lib_ioctl,
  286. .hw_params = capture_hw_params,
  287. .hw_free = capture_hw_free,
  288. .prepare = capture_prepare,
  289. .trigger = capture_trigger,
  290. .pointer = capture_pointer,
  291. .ack = capture_ack,
  292. .page = snd_pcm_lib_get_vmalloc_page,
  293. };
  294. static const struct snd_pcm_ops playback_ops = {
  295. .open = pcm_open,
  296. .close = pcm_close,
  297. .ioctl = snd_pcm_lib_ioctl,
  298. .hw_params = playback_hw_params,
  299. .hw_free = playback_hw_free,
  300. .prepare = playback_prepare,
  301. .trigger = playback_trigger,
  302. .pointer = playback_pointer,
  303. .ack = playback_ack,
  304. .page = snd_pcm_lib_get_vmalloc_page,
  305. };
  306. struct snd_pcm *pcm;
  307. int err;
  308. err = snd_pcm_new(motu->card, motu->card->driver, 0, 1, 1, &pcm);
  309. if (err < 0)
  310. return err;
  311. pcm->private_data = motu;
  312. strcpy(pcm->name, motu->card->shortname);
  313. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
  314. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
  315. return 0;
  316. }