tascam-pcm.c 7.1 KB

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