bebob_pcm.c 9.6 KB

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