bebob_pcm.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. mutex_lock(&bebob->mutex);
  184. bebob->substreams_counter++;
  185. mutex_unlock(&bebob->mutex);
  186. }
  187. amdtp_am824_set_pcm_format(&bebob->tx_stream, params_format(hw_params));
  188. return 0;
  189. }
  190. static int
  191. pcm_playback_hw_params(struct snd_pcm_substream *substream,
  192. struct snd_pcm_hw_params *hw_params)
  193. {
  194. struct snd_bebob *bebob = substream->private_data;
  195. int err;
  196. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  197. params_buffer_bytes(hw_params));
  198. if (err < 0)
  199. return err;
  200. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  201. mutex_lock(&bebob->mutex);
  202. bebob->substreams_counter++;
  203. mutex_unlock(&bebob->mutex);
  204. }
  205. amdtp_am824_set_pcm_format(&bebob->rx_stream, params_format(hw_params));
  206. return 0;
  207. }
  208. static int
  209. pcm_capture_hw_free(struct snd_pcm_substream *substream)
  210. {
  211. struct snd_bebob *bebob = substream->private_data;
  212. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) {
  213. mutex_lock(&bebob->mutex);
  214. bebob->substreams_counter--;
  215. mutex_unlock(&bebob->mutex);
  216. }
  217. snd_bebob_stream_stop_duplex(bebob);
  218. return snd_pcm_lib_free_vmalloc_buffer(substream);
  219. }
  220. static int
  221. pcm_playback_hw_free(struct snd_pcm_substream *substream)
  222. {
  223. struct snd_bebob *bebob = substream->private_data;
  224. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) {
  225. mutex_lock(&bebob->mutex);
  226. bebob->substreams_counter--;
  227. mutex_unlock(&bebob->mutex);
  228. }
  229. snd_bebob_stream_stop_duplex(bebob);
  230. return snd_pcm_lib_free_vmalloc_buffer(substream);
  231. }
  232. static int
  233. pcm_capture_prepare(struct snd_pcm_substream *substream)
  234. {
  235. struct snd_bebob *bebob = substream->private_data;
  236. struct snd_pcm_runtime *runtime = substream->runtime;
  237. int err;
  238. err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
  239. if (err >= 0)
  240. amdtp_stream_pcm_prepare(&bebob->tx_stream);
  241. return err;
  242. }
  243. static int
  244. pcm_playback_prepare(struct snd_pcm_substream *substream)
  245. {
  246. struct snd_bebob *bebob = substream->private_data;
  247. struct snd_pcm_runtime *runtime = substream->runtime;
  248. int err;
  249. err = snd_bebob_stream_start_duplex(bebob, runtime->rate);
  250. if (err >= 0)
  251. amdtp_stream_pcm_prepare(&bebob->rx_stream);
  252. return err;
  253. }
  254. static int
  255. pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  256. {
  257. struct snd_bebob *bebob = substream->private_data;
  258. switch (cmd) {
  259. case SNDRV_PCM_TRIGGER_START:
  260. amdtp_stream_pcm_trigger(&bebob->tx_stream, substream);
  261. break;
  262. case SNDRV_PCM_TRIGGER_STOP:
  263. amdtp_stream_pcm_trigger(&bebob->tx_stream, NULL);
  264. break;
  265. default:
  266. return -EINVAL;
  267. }
  268. return 0;
  269. }
  270. static int
  271. pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  272. {
  273. struct snd_bebob *bebob = substream->private_data;
  274. switch (cmd) {
  275. case SNDRV_PCM_TRIGGER_START:
  276. amdtp_stream_pcm_trigger(&bebob->rx_stream, substream);
  277. break;
  278. case SNDRV_PCM_TRIGGER_STOP:
  279. amdtp_stream_pcm_trigger(&bebob->rx_stream, NULL);
  280. break;
  281. default:
  282. return -EINVAL;
  283. }
  284. return 0;
  285. }
  286. static snd_pcm_uframes_t
  287. pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
  288. {
  289. struct snd_bebob *bebob = sbstrm->private_data;
  290. return amdtp_stream_pcm_pointer(&bebob->tx_stream);
  291. }
  292. static snd_pcm_uframes_t
  293. pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
  294. {
  295. struct snd_bebob *bebob = sbstrm->private_data;
  296. return amdtp_stream_pcm_pointer(&bebob->rx_stream);
  297. }
  298. static const struct snd_pcm_ops pcm_capture_ops = {
  299. .open = pcm_open,
  300. .close = pcm_close,
  301. .ioctl = snd_pcm_lib_ioctl,
  302. .hw_params = pcm_capture_hw_params,
  303. .hw_free = pcm_capture_hw_free,
  304. .prepare = pcm_capture_prepare,
  305. .trigger = pcm_capture_trigger,
  306. .pointer = pcm_capture_pointer,
  307. .page = snd_pcm_lib_get_vmalloc_page,
  308. };
  309. static const struct snd_pcm_ops pcm_playback_ops = {
  310. .open = pcm_open,
  311. .close = pcm_close,
  312. .ioctl = snd_pcm_lib_ioctl,
  313. .hw_params = pcm_playback_hw_params,
  314. .hw_free = pcm_playback_hw_free,
  315. .prepare = pcm_playback_prepare,
  316. .trigger = pcm_playback_trigger,
  317. .pointer = pcm_playback_pointer,
  318. .page = snd_pcm_lib_get_vmalloc_page,
  319. .mmap = snd_pcm_lib_mmap_vmalloc,
  320. };
  321. int snd_bebob_create_pcm_devices(struct snd_bebob *bebob)
  322. {
  323. struct snd_pcm *pcm;
  324. int err;
  325. err = snd_pcm_new(bebob->card, bebob->card->driver, 0, 1, 1, &pcm);
  326. if (err < 0)
  327. goto end;
  328. pcm->private_data = bebob;
  329. snprintf(pcm->name, sizeof(pcm->name),
  330. "%s PCM", bebob->card->shortname);
  331. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops);
  332. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);
  333. end:
  334. return err;
  335. }