digi00x-pcm.c 9.0 KB

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