tascam-pcm.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * tascam-pcm.c - a part of driver for TASCAM FireWire series
  4. *
  5. * Copyright (c) 2015 Takashi Sakamoto
  6. */
  7. #include "tascam.h"
  8. static int pcm_init_hw_params(struct snd_tscm *tscm,
  9. struct snd_pcm_substream *substream)
  10. {
  11. struct snd_pcm_runtime *runtime = substream->runtime;
  12. struct snd_pcm_hardware *hw = &runtime->hw;
  13. struct amdtp_stream *stream;
  14. unsigned int pcm_channels;
  15. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  16. runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
  17. stream = &tscm->tx_stream;
  18. pcm_channels = tscm->spec->pcm_capture_analog_channels;
  19. } else {
  20. runtime->hw.formats = SNDRV_PCM_FMTBIT_S32;
  21. stream = &tscm->rx_stream;
  22. pcm_channels = tscm->spec->pcm_playback_analog_channels;
  23. }
  24. if (tscm->spec->has_adat)
  25. pcm_channels += 8;
  26. if (tscm->spec->has_spdif)
  27. pcm_channels += 2;
  28. runtime->hw.channels_min = runtime->hw.channels_max = pcm_channels;
  29. hw->rates = SNDRV_PCM_RATE_44100 |
  30. SNDRV_PCM_RATE_48000 |
  31. SNDRV_PCM_RATE_88200 |
  32. SNDRV_PCM_RATE_96000;
  33. snd_pcm_limit_hw_rates(runtime);
  34. return amdtp_tscm_add_pcm_hw_constraints(stream, runtime);
  35. }
  36. static int pcm_open(struct snd_pcm_substream *substream)
  37. {
  38. struct snd_tscm *tscm = substream->private_data;
  39. struct amdtp_domain *d = &tscm->domain;
  40. enum snd_tscm_clock clock;
  41. int err;
  42. err = snd_tscm_stream_lock_try(tscm);
  43. if (err < 0)
  44. return err;
  45. err = pcm_init_hw_params(tscm, substream);
  46. if (err < 0)
  47. goto err_locked;
  48. err = snd_tscm_stream_get_clock(tscm, &clock);
  49. if (err < 0)
  50. goto err_locked;
  51. mutex_lock(&tscm->mutex);
  52. // When source of clock is not internal or any stream is reserved for
  53. // transmission of PCM frames, the available sampling rate is limited
  54. // at current one.
  55. if (clock != SND_TSCM_CLOCK_INTERNAL || tscm->substreams_counter > 0) {
  56. unsigned int frames_per_period = d->events_per_period;
  57. unsigned int frames_per_buffer = d->events_per_buffer;
  58. unsigned int rate;
  59. err = snd_tscm_stream_get_rate(tscm, &rate);
  60. if (err < 0) {
  61. mutex_unlock(&tscm->mutex);
  62. goto err_locked;
  63. }
  64. substream->runtime->hw.rate_min = rate;
  65. substream->runtime->hw.rate_max = rate;
  66. err = snd_pcm_hw_constraint_minmax(substream->runtime,
  67. SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  68. frames_per_period, frames_per_period);
  69. if (err < 0) {
  70. mutex_unlock(&tscm->mutex);
  71. goto err_locked;
  72. }
  73. err = snd_pcm_hw_constraint_minmax(substream->runtime,
  74. SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  75. frames_per_buffer, frames_per_buffer);
  76. if (err < 0) {
  77. mutex_unlock(&tscm->mutex);
  78. goto err_locked;
  79. }
  80. }
  81. mutex_unlock(&tscm->mutex);
  82. snd_pcm_set_sync(substream);
  83. return 0;
  84. err_locked:
  85. snd_tscm_stream_lock_release(tscm);
  86. return err;
  87. }
  88. static int pcm_close(struct snd_pcm_substream *substream)
  89. {
  90. struct snd_tscm *tscm = substream->private_data;
  91. snd_tscm_stream_lock_release(tscm);
  92. return 0;
  93. }
  94. static int pcm_hw_params(struct snd_pcm_substream *substream,
  95. struct snd_pcm_hw_params *hw_params)
  96. {
  97. struct snd_tscm *tscm = substream->private_data;
  98. int err = 0;
  99. if (substream->runtime->state == SNDRV_PCM_STATE_OPEN) {
  100. unsigned int rate = params_rate(hw_params);
  101. unsigned int frames_per_period = params_period_size(hw_params);
  102. unsigned int frames_per_buffer = params_buffer_size(hw_params);
  103. mutex_lock(&tscm->mutex);
  104. err = snd_tscm_stream_reserve_duplex(tscm, rate,
  105. frames_per_period, frames_per_buffer);
  106. if (err >= 0)
  107. ++tscm->substreams_counter;
  108. mutex_unlock(&tscm->mutex);
  109. }
  110. return err;
  111. }
  112. static int pcm_hw_free(struct snd_pcm_substream *substream)
  113. {
  114. struct snd_tscm *tscm = substream->private_data;
  115. mutex_lock(&tscm->mutex);
  116. if (substream->runtime->state != SNDRV_PCM_STATE_OPEN)
  117. --tscm->substreams_counter;
  118. snd_tscm_stream_stop_duplex(tscm);
  119. mutex_unlock(&tscm->mutex);
  120. return 0;
  121. }
  122. static int pcm_capture_prepare(struct snd_pcm_substream *substream)
  123. {
  124. struct snd_tscm *tscm = substream->private_data;
  125. struct snd_pcm_runtime *runtime = substream->runtime;
  126. int err;
  127. mutex_lock(&tscm->mutex);
  128. err = snd_tscm_stream_start_duplex(tscm, runtime->rate);
  129. if (err >= 0)
  130. amdtp_stream_pcm_prepare(&tscm->tx_stream);
  131. mutex_unlock(&tscm->mutex);
  132. return err;
  133. }
  134. static int pcm_playback_prepare(struct snd_pcm_substream *substream)
  135. {
  136. struct snd_tscm *tscm = substream->private_data;
  137. struct snd_pcm_runtime *runtime = substream->runtime;
  138. int err;
  139. mutex_lock(&tscm->mutex);
  140. err = snd_tscm_stream_start_duplex(tscm, runtime->rate);
  141. if (err >= 0)
  142. amdtp_stream_pcm_prepare(&tscm->rx_stream);
  143. mutex_unlock(&tscm->mutex);
  144. return err;
  145. }
  146. static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  147. {
  148. struct snd_tscm *tscm = substream->private_data;
  149. switch (cmd) {
  150. case SNDRV_PCM_TRIGGER_START:
  151. amdtp_stream_pcm_trigger(&tscm->tx_stream, substream);
  152. break;
  153. case SNDRV_PCM_TRIGGER_STOP:
  154. amdtp_stream_pcm_trigger(&tscm->tx_stream, NULL);
  155. break;
  156. default:
  157. return -EINVAL;
  158. }
  159. return 0;
  160. }
  161. static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  162. {
  163. struct snd_tscm *tscm = substream->private_data;
  164. switch (cmd) {
  165. case SNDRV_PCM_TRIGGER_START:
  166. amdtp_stream_pcm_trigger(&tscm->rx_stream, substream);
  167. break;
  168. case SNDRV_PCM_TRIGGER_STOP:
  169. amdtp_stream_pcm_trigger(&tscm->rx_stream, NULL);
  170. break;
  171. default:
  172. return -EINVAL;
  173. }
  174. return 0;
  175. }
  176. static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
  177. {
  178. struct snd_tscm *tscm = sbstrm->private_data;
  179. return amdtp_domain_stream_pcm_pointer(&tscm->domain, &tscm->tx_stream);
  180. }
  181. static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
  182. {
  183. struct snd_tscm *tscm = sbstrm->private_data;
  184. return amdtp_domain_stream_pcm_pointer(&tscm->domain, &tscm->rx_stream);
  185. }
  186. static int pcm_capture_ack(struct snd_pcm_substream *substream)
  187. {
  188. struct snd_tscm *tscm = substream->private_data;
  189. return amdtp_domain_stream_pcm_ack(&tscm->domain, &tscm->tx_stream);
  190. }
  191. static int pcm_playback_ack(struct snd_pcm_substream *substream)
  192. {
  193. struct snd_tscm *tscm = substream->private_data;
  194. return amdtp_domain_stream_pcm_ack(&tscm->domain, &tscm->rx_stream);
  195. }
  196. int snd_tscm_create_pcm_devices(struct snd_tscm *tscm)
  197. {
  198. static const struct snd_pcm_ops capture_ops = {
  199. .open = pcm_open,
  200. .close = pcm_close,
  201. .hw_params = pcm_hw_params,
  202. .hw_free = pcm_hw_free,
  203. .prepare = pcm_capture_prepare,
  204. .trigger = pcm_capture_trigger,
  205. .pointer = pcm_capture_pointer,
  206. .ack = pcm_capture_ack,
  207. };
  208. static const struct snd_pcm_ops playback_ops = {
  209. .open = pcm_open,
  210. .close = pcm_close,
  211. .hw_params = pcm_hw_params,
  212. .hw_free = pcm_hw_free,
  213. .prepare = pcm_playback_prepare,
  214. .trigger = pcm_playback_trigger,
  215. .pointer = pcm_playback_pointer,
  216. .ack = pcm_playback_ack,
  217. };
  218. struct snd_pcm *pcm;
  219. int err;
  220. err = snd_pcm_new(tscm->card, tscm->card->driver, 0, 1, 1, &pcm);
  221. if (err < 0)
  222. return err;
  223. pcm->private_data = tscm;
  224. pcm->nonatomic = true;
  225. snprintf(pcm->name, sizeof(pcm->name),
  226. "%s PCM", tscm->card->shortname);
  227. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
  228. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
  229. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
  230. return 0;
  231. }