tascam-pcm.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 (clock != SND_TSCM_CLOCK_INTERNAL ||
  51. amdtp_stream_pcm_running(&tscm->rx_stream) ||
  52. amdtp_stream_pcm_running(&tscm->tx_stream)) {
  53. err = snd_tscm_stream_get_rate(tscm, &rate);
  54. if (err < 0)
  55. goto err_locked;
  56. substream->runtime->hw.rate_min = rate;
  57. substream->runtime->hw.rate_max = rate;
  58. }
  59. snd_pcm_set_sync(substream);
  60. end:
  61. return err;
  62. err_locked:
  63. snd_tscm_stream_lock_release(tscm);
  64. return err;
  65. }
  66. static int pcm_close(struct snd_pcm_substream *substream)
  67. {
  68. struct snd_tscm *tscm = substream->private_data;
  69. snd_tscm_stream_lock_release(tscm);
  70. return 0;
  71. }
  72. static int pcm_capture_hw_params(struct snd_pcm_substream *substream,
  73. struct snd_pcm_hw_params *hw_params)
  74. {
  75. struct snd_tscm *tscm = substream->private_data;
  76. int err;
  77. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  78. params_buffer_bytes(hw_params));
  79. if (err < 0)
  80. return err;
  81. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  82. mutex_lock(&tscm->mutex);
  83. tscm->substreams_counter++;
  84. mutex_unlock(&tscm->mutex);
  85. }
  86. return 0;
  87. }
  88. static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
  89. struct snd_pcm_hw_params *hw_params)
  90. {
  91. struct snd_tscm *tscm = substream->private_data;
  92. int err;
  93. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  94. params_buffer_bytes(hw_params));
  95. if (err < 0)
  96. return err;
  97. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  98. mutex_lock(&tscm->mutex);
  99. tscm->substreams_counter++;
  100. mutex_unlock(&tscm->mutex);
  101. }
  102. return 0;
  103. }
  104. static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
  105. {
  106. struct snd_tscm *tscm = substream->private_data;
  107. mutex_lock(&tscm->mutex);
  108. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  109. tscm->substreams_counter--;
  110. snd_tscm_stream_stop_duplex(tscm);
  111. mutex_unlock(&tscm->mutex);
  112. return snd_pcm_lib_free_vmalloc_buffer(substream);
  113. }
  114. static int pcm_playback_hw_free(struct snd_pcm_substream *substream)
  115. {
  116. struct snd_tscm *tscm = substream->private_data;
  117. mutex_lock(&tscm->mutex);
  118. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  119. tscm->substreams_counter--;
  120. snd_tscm_stream_stop_duplex(tscm);
  121. mutex_unlock(&tscm->mutex);
  122. return snd_pcm_lib_free_vmalloc_buffer(substream);
  123. }
  124. static int pcm_capture_prepare(struct snd_pcm_substream *substream)
  125. {
  126. struct snd_tscm *tscm = substream->private_data;
  127. struct snd_pcm_runtime *runtime = substream->runtime;
  128. int err;
  129. mutex_lock(&tscm->mutex);
  130. err = snd_tscm_stream_start_duplex(tscm, runtime->rate);
  131. if (err >= 0)
  132. amdtp_stream_pcm_prepare(&tscm->tx_stream);
  133. mutex_unlock(&tscm->mutex);
  134. return err;
  135. }
  136. static int pcm_playback_prepare(struct snd_pcm_substream *substream)
  137. {
  138. struct snd_tscm *tscm = substream->private_data;
  139. struct snd_pcm_runtime *runtime = substream->runtime;
  140. int err;
  141. mutex_lock(&tscm->mutex);
  142. err = snd_tscm_stream_start_duplex(tscm, runtime->rate);
  143. if (err >= 0)
  144. amdtp_stream_pcm_prepare(&tscm->rx_stream);
  145. mutex_unlock(&tscm->mutex);
  146. return err;
  147. }
  148. static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  149. {
  150. struct snd_tscm *tscm = substream->private_data;
  151. switch (cmd) {
  152. case SNDRV_PCM_TRIGGER_START:
  153. amdtp_stream_pcm_trigger(&tscm->tx_stream, substream);
  154. break;
  155. case SNDRV_PCM_TRIGGER_STOP:
  156. amdtp_stream_pcm_trigger(&tscm->tx_stream, NULL);
  157. break;
  158. default:
  159. return -EINVAL;
  160. }
  161. return 0;
  162. }
  163. static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  164. {
  165. struct snd_tscm *tscm = substream->private_data;
  166. switch (cmd) {
  167. case SNDRV_PCM_TRIGGER_START:
  168. amdtp_stream_pcm_trigger(&tscm->rx_stream, substream);
  169. break;
  170. case SNDRV_PCM_TRIGGER_STOP:
  171. amdtp_stream_pcm_trigger(&tscm->rx_stream, NULL);
  172. break;
  173. default:
  174. return -EINVAL;
  175. }
  176. return 0;
  177. }
  178. static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
  179. {
  180. struct snd_tscm *tscm = sbstrm->private_data;
  181. return amdtp_stream_pcm_pointer(&tscm->tx_stream);
  182. }
  183. static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
  184. {
  185. struct snd_tscm *tscm = sbstrm->private_data;
  186. return amdtp_stream_pcm_pointer(&tscm->rx_stream);
  187. }
  188. static int pcm_capture_ack(struct snd_pcm_substream *substream)
  189. {
  190. struct snd_tscm *tscm = substream->private_data;
  191. return amdtp_stream_pcm_ack(&tscm->tx_stream);
  192. }
  193. static int pcm_playback_ack(struct snd_pcm_substream *substream)
  194. {
  195. struct snd_tscm *tscm = substream->private_data;
  196. return amdtp_stream_pcm_ack(&tscm->rx_stream);
  197. }
  198. int snd_tscm_create_pcm_devices(struct snd_tscm *tscm)
  199. {
  200. static const struct snd_pcm_ops capture_ops = {
  201. .open = pcm_open,
  202. .close = pcm_close,
  203. .ioctl = snd_pcm_lib_ioctl,
  204. .hw_params = pcm_capture_hw_params,
  205. .hw_free = pcm_capture_hw_free,
  206. .prepare = pcm_capture_prepare,
  207. .trigger = pcm_capture_trigger,
  208. .pointer = pcm_capture_pointer,
  209. .ack = pcm_capture_ack,
  210. .page = snd_pcm_lib_get_vmalloc_page,
  211. };
  212. static const struct snd_pcm_ops playback_ops = {
  213. .open = pcm_open,
  214. .close = pcm_close,
  215. .ioctl = snd_pcm_lib_ioctl,
  216. .hw_params = pcm_playback_hw_params,
  217. .hw_free = pcm_playback_hw_free,
  218. .prepare = pcm_playback_prepare,
  219. .trigger = pcm_playback_trigger,
  220. .pointer = pcm_playback_pointer,
  221. .ack = pcm_playback_ack,
  222. .page = snd_pcm_lib_get_vmalloc_page,
  223. };
  224. struct snd_pcm *pcm;
  225. int err;
  226. err = snd_pcm_new(tscm->card, tscm->card->driver, 0, 1, 1, &pcm);
  227. if (err < 0)
  228. return err;
  229. pcm->private_data = tscm;
  230. snprintf(pcm->name, sizeof(pcm->name),
  231. "%s PCM", tscm->card->shortname);
  232. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
  233. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
  234. return 0;
  235. }