bebob_pcm.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * bebob_pcm.c - a part of driver for BeBoB based devices
  3. *
  4. * Copyright (c) 2013-2014 Takashi Sakamoto
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include "./bebob.h"
  9. static int
  10. hw_rule_rate(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
  11. {
  12. struct snd_bebob_stream_formation *formations = rule->private;
  13. struct snd_interval *r =
  14. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  15. const struct snd_interval *c =
  16. hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  17. struct snd_interval t = {
  18. .min = UINT_MAX, .max = 0, .integer = 1
  19. };
  20. unsigned int i;
  21. for (i = 0; i < SND_BEBOB_STRM_FMT_ENTRIES; i++) {
  22. /* entry is invalid */
  23. if (formations[i].pcm == 0)
  24. continue;
  25. if (!snd_interval_test(c, formations[i].pcm))
  26. continue;
  27. t.min = min(t.min, snd_bebob_rate_table[i]);
  28. t.max = max(t.max, snd_bebob_rate_table[i]);
  29. }
  30. return snd_interval_refine(r, &t);
  31. }
  32. static int
  33. hw_rule_channels(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
  34. {
  35. struct snd_bebob_stream_formation *formations = rule->private;
  36. struct snd_interval *c =
  37. hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  38. const struct snd_interval *r =
  39. hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
  40. struct snd_interval t = {
  41. .min = UINT_MAX, .max = 0, .integer = 1
  42. };
  43. unsigned int i;
  44. for (i = 0; i < SND_BEBOB_STRM_FMT_ENTRIES; i++) {
  45. /* entry is invalid */
  46. if (formations[i].pcm == 0)
  47. continue;
  48. if (!snd_interval_test(r, snd_bebob_rate_table[i]))
  49. continue;
  50. t.min = min(t.min, formations[i].pcm);
  51. t.max = max(t.max, formations[i].pcm);
  52. }
  53. return snd_interval_refine(c, &t);
  54. }
  55. static void
  56. limit_channels_and_rates(struct snd_pcm_hardware *hw,
  57. struct snd_bebob_stream_formation *formations)
  58. {
  59. unsigned int i;
  60. hw->channels_min = UINT_MAX;
  61. hw->channels_max = 0;
  62. hw->rate_min = UINT_MAX;
  63. hw->rate_max = 0;
  64. hw->rates = 0;
  65. for (i = 0; i < SND_BEBOB_STRM_FMT_ENTRIES; i++) {
  66. /* entry has no PCM channels */
  67. if (formations[i].pcm == 0)
  68. continue;
  69. hw->channels_min = min(hw->channels_min, formations[i].pcm);
  70. hw->channels_max = max(hw->channels_max, formations[i].pcm);
  71. hw->rate_min = min(hw->rate_min, snd_bebob_rate_table[i]);
  72. hw->rate_max = max(hw->rate_max, snd_bebob_rate_table[i]);
  73. hw->rates |= snd_pcm_rate_to_rate_bit(snd_bebob_rate_table[i]);
  74. }
  75. }
  76. static void
  77. limit_period_and_buffer(struct snd_pcm_hardware *hw)
  78. {
  79. hw->periods_min = 2; /* SNDRV_PCM_INFO_BATCH */
  80. hw->periods_max = UINT_MAX;
  81. hw->period_bytes_min = 4 * hw->channels_max; /* bytes for a frame */
  82. /* Just to prevent from allocating much pages. */
  83. hw->period_bytes_max = hw->period_bytes_min * 2048;
  84. hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
  85. }
  86. static int
  87. pcm_init_hw_params(struct snd_bebob *bebob,
  88. struct snd_pcm_substream *substream)
  89. {
  90. struct snd_pcm_runtime *runtime = substream->runtime;
  91. struct amdtp_stream *s;
  92. struct snd_bebob_stream_formation *formations;
  93. int err;
  94. runtime->hw.info = SNDRV_PCM_INFO_BATCH |
  95. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  96. SNDRV_PCM_INFO_INTERLEAVED |
  97. SNDRV_PCM_INFO_JOINT_DUPLEX |
  98. SNDRV_PCM_INFO_MMAP |
  99. SNDRV_PCM_INFO_MMAP_VALID;
  100. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  101. runtime->hw.formats = AM824_IN_PCM_FORMAT_BITS;
  102. s = &bebob->tx_stream;
  103. formations = bebob->tx_stream_formations;
  104. } else {
  105. runtime->hw.formats = AM824_OUT_PCM_FORMAT_BITS;
  106. s = &bebob->rx_stream;
  107. formations = bebob->rx_stream_formations;
  108. }
  109. limit_channels_and_rates(&runtime->hw, formations);
  110. limit_period_and_buffer(&runtime->hw);
  111. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  112. hw_rule_channels, formations,
  113. SNDRV_PCM_HW_PARAM_RATE, -1);
  114. if (err < 0)
  115. goto end;
  116. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  117. hw_rule_rate, formations,
  118. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  119. if (err < 0)
  120. goto end;
  121. err = amdtp_am824_add_pcm_hw_constraints(s, runtime);
  122. end:
  123. return err;
  124. }
  125. static int
  126. pcm_open(struct snd_pcm_substream *substream)
  127. {
  128. struct snd_bebob *bebob = substream->private_data;
  129. const struct snd_bebob_rate_spec *spec = bebob->spec->rate;
  130. unsigned int sampling_rate;
  131. enum snd_bebob_clock_type src;
  132. int err;
  133. err = snd_bebob_stream_lock_try(bebob);
  134. if (err < 0)
  135. goto end;
  136. err = pcm_init_hw_params(bebob, substream);
  137. if (err < 0)
  138. goto err_locked;
  139. err = snd_bebob_stream_get_clock_src(bebob, &src);
  140. if (err < 0)
  141. goto err_locked;
  142. /*
  143. * When source of clock is internal or any PCM stream are running,
  144. * the available sampling rate is limited at current sampling rate.
  145. */
  146. if (src == SND_BEBOB_CLOCK_TYPE_EXTERNAL ||
  147. amdtp_stream_pcm_running(&bebob->tx_stream) ||
  148. amdtp_stream_pcm_running(&bebob->rx_stream)) {
  149. err = spec->get(bebob, &sampling_rate);
  150. if (err < 0) {
  151. dev_err(&bebob->unit->device,
  152. "fail to get sampling rate: %d\n", err);
  153. goto err_locked;
  154. }
  155. substream->runtime->hw.rate_min = sampling_rate;
  156. substream->runtime->hw.rate_max = sampling_rate;
  157. }
  158. snd_pcm_set_sync(substream);
  159. end:
  160. return err;
  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
  173. pcm_capture_hw_params(struct snd_pcm_substream *substream,
  174. struct snd_pcm_hw_params *hw_params)
  175. {
  176. struct snd_bebob *bebob = substream->private_data;
  177. int err;
  178. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  179. params_buffer_bytes(hw_params));
  180. if (err < 0)
  181. return err;
  182. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
  183. atomic_inc(&bebob->substreams_counter);
  184. amdtp_am824_set_pcm_format(&bebob->tx_stream, params_format(hw_params));
  185. return 0;
  186. }
  187. static int
  188. pcm_playback_hw_params(struct snd_pcm_substream *substream,
  189. struct snd_pcm_hw_params *hw_params)
  190. {
  191. struct snd_bebob *bebob = substream->private_data;
  192. int err;
  193. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  194. params_buffer_bytes(hw_params));
  195. if (err < 0)
  196. return err;
  197. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
  198. atomic_inc(&bebob->substreams_counter);
  199. amdtp_am824_set_pcm_format(&bebob->rx_stream, params_format(hw_params));
  200. return 0;
  201. }
  202. static int
  203. pcm_capture_hw_free(struct snd_pcm_substream *substream)
  204. {
  205. struct snd_bebob *bebob = substream->private_data;
  206. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  207. atomic_dec(&bebob->substreams_counter);
  208. snd_bebob_stream_stop_duplex(bebob);
  209. return snd_pcm_lib_free_vmalloc_buffer(substream);
  210. }
  211. static int
  212. pcm_playback_hw_free(struct snd_pcm_substream *substream)
  213. {
  214. struct snd_bebob *bebob = substream->private_data;
  215. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  216. atomic_dec(&bebob->substreams_counter);
  217. snd_bebob_stream_stop_duplex(bebob);
  218. return snd_pcm_lib_free_vmalloc_buffer(substream);
  219. }
  220. static int
  221. pcm_capture_prepare(struct snd_pcm_substream *substream)
  222. {
  223. struct snd_bebob *bebob = substream->private_data;
  224. struct snd_pcm_runtime *runtime = substream->runtime;
  225. int err;
  226. err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
  227. if (err >= 0)
  228. amdtp_stream_pcm_prepare(&bebob->tx_stream);
  229. return err;
  230. }
  231. static int
  232. pcm_playback_prepare(struct snd_pcm_substream *substream)
  233. {
  234. struct snd_bebob *bebob = substream->private_data;
  235. struct snd_pcm_runtime *runtime = substream->runtime;
  236. int err;
  237. err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
  238. if (err >= 0)
  239. amdtp_stream_pcm_prepare(&bebob->rx_stream);
  240. return err;
  241. }
  242. static int
  243. pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  244. {
  245. struct snd_bebob *bebob = substream->private_data;
  246. switch (cmd) {
  247. case SNDRV_PCM_TRIGGER_START:
  248. amdtp_stream_pcm_trigger(&bebob->tx_stream, substream);
  249. break;
  250. case SNDRV_PCM_TRIGGER_STOP:
  251. amdtp_stream_pcm_trigger(&bebob->tx_stream, NULL);
  252. break;
  253. default:
  254. return -EINVAL;
  255. }
  256. return 0;
  257. }
  258. static int
  259. pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  260. {
  261. struct snd_bebob *bebob = substream->private_data;
  262. switch (cmd) {
  263. case SNDRV_PCM_TRIGGER_START:
  264. amdtp_stream_pcm_trigger(&bebob->rx_stream, substream);
  265. break;
  266. case SNDRV_PCM_TRIGGER_STOP:
  267. amdtp_stream_pcm_trigger(&bebob->rx_stream, NULL);
  268. break;
  269. default:
  270. return -EINVAL;
  271. }
  272. return 0;
  273. }
  274. static snd_pcm_uframes_t
  275. pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
  276. {
  277. struct snd_bebob *bebob = sbstrm->private_data;
  278. return amdtp_stream_pcm_pointer(&bebob->tx_stream);
  279. }
  280. static snd_pcm_uframes_t
  281. pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
  282. {
  283. struct snd_bebob *bebob = sbstrm->private_data;
  284. return amdtp_stream_pcm_pointer(&bebob->rx_stream);
  285. }
  286. static const struct snd_pcm_ops pcm_capture_ops = {
  287. .open = pcm_open,
  288. .close = pcm_close,
  289. .ioctl = snd_pcm_lib_ioctl,
  290. .hw_params = pcm_capture_hw_params,
  291. .hw_free = pcm_capture_hw_free,
  292. .prepare = pcm_capture_prepare,
  293. .trigger = pcm_capture_trigger,
  294. .pointer = pcm_capture_pointer,
  295. .page = snd_pcm_lib_get_vmalloc_page,
  296. };
  297. static const struct snd_pcm_ops pcm_playback_ops = {
  298. .open = pcm_open,
  299. .close = pcm_close,
  300. .ioctl = snd_pcm_lib_ioctl,
  301. .hw_params = pcm_playback_hw_params,
  302. .hw_free = pcm_playback_hw_free,
  303. .prepare = pcm_playback_prepare,
  304. .trigger = pcm_playback_trigger,
  305. .pointer = pcm_playback_pointer,
  306. .page = snd_pcm_lib_get_vmalloc_page,
  307. .mmap = snd_pcm_lib_mmap_vmalloc,
  308. };
  309. int snd_bebob_create_pcm_devices(struct snd_bebob *bebob)
  310. {
  311. struct snd_pcm *pcm;
  312. int err;
  313. err = snd_pcm_new(bebob->card, bebob->card->driver, 0, 1, 1, &pcm);
  314. if (err < 0)
  315. goto end;
  316. pcm->private_data = bebob;
  317. snprintf(pcm->name, sizeof(pcm->name),
  318. "%s PCM", bebob->card->shortname);
  319. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops);
  320. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);
  321. end:
  322. return err;
  323. }