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