digi00x-pcm.c 8.8 KB

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