bebob_pcm.c 9.3 KB

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