fireworks_pcm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * fireworks_pcm.c - a part of driver for Fireworks based devices
  3. *
  4. * Copyright (c) 2009-2010 Clemens Ladisch
  5. * Copyright (c) 2013-2014 Takashi Sakamoto
  6. *
  7. * Licensed under the terms of the GNU General Public License, version 2.
  8. */
  9. #include "./fireworks.h"
  10. /*
  11. * NOTE:
  12. * Fireworks changes its AMDTP channels for PCM data according to its sampling
  13. * rate. There are three modes. Here _XX is either _rx or _tx.
  14. * 0: 32.0- 48.0 kHz then snd_efw_hwinfo.amdtp_XX_pcm_channels applied
  15. * 1: 88.2- 96.0 kHz then snd_efw_hwinfo.amdtp_XX_pcm_channels_2x applied
  16. * 2: 176.4-192.0 kHz then snd_efw_hwinfo.amdtp_XX_pcm_channels_4x applied
  17. *
  18. * The number of PCM channels for analog input and output are always fixed but
  19. * the number of PCM channels for digital input and output are differed.
  20. *
  21. * Additionally, according to "AudioFire Owner's Manual Version 2.2", in some
  22. * model, the number of PCM channels for digital input has more restriction
  23. * depending on which digital interface is selected.
  24. * - S/PDIF coaxial and optical : use input 1-2
  25. * - ADAT optical at 32.0-48.0 kHz : use input 1-8
  26. * - ADAT optical at 88.2-96.0 kHz : use input 1-4 (S/MUX format)
  27. *
  28. * The data in AMDTP channels for blank PCM channels are zero.
  29. */
  30. static const unsigned int freq_table[] = {
  31. /* multiplier mode 0 */
  32. [0] = 32000,
  33. [1] = 44100,
  34. [2] = 48000,
  35. /* multiplier mode 1 */
  36. [3] = 88200,
  37. [4] = 96000,
  38. /* multiplier mode 2 */
  39. [5] = 176400,
  40. [6] = 192000,
  41. };
  42. static inline unsigned int
  43. get_multiplier_mode_with_index(unsigned int index)
  44. {
  45. return ((int)index - 1) / 2;
  46. }
  47. int snd_efw_get_multiplier_mode(unsigned int sampling_rate, unsigned int *mode)
  48. {
  49. unsigned int i;
  50. for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
  51. if (freq_table[i] == sampling_rate) {
  52. *mode = get_multiplier_mode_with_index(i);
  53. return 0;
  54. }
  55. }
  56. return -EINVAL;
  57. }
  58. static int
  59. hw_rule_rate(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
  60. {
  61. unsigned int *pcm_channels = rule->private;
  62. struct snd_interval *r =
  63. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  64. const struct snd_interval *c =
  65. hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  66. struct snd_interval t = {
  67. .min = UINT_MAX, .max = 0, .integer = 1
  68. };
  69. unsigned int i, mode;
  70. for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
  71. mode = get_multiplier_mode_with_index(i);
  72. if (!snd_interval_test(c, pcm_channels[mode]))
  73. continue;
  74. t.min = min(t.min, freq_table[i]);
  75. t.max = max(t.max, freq_table[i]);
  76. }
  77. return snd_interval_refine(r, &t);
  78. }
  79. static int
  80. hw_rule_channels(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
  81. {
  82. unsigned int *pcm_channels = rule->private;
  83. struct snd_interval *c =
  84. hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  85. const struct snd_interval *r =
  86. hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
  87. struct snd_interval t = {
  88. .min = UINT_MAX, .max = 0, .integer = 1
  89. };
  90. unsigned int i, mode;
  91. for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
  92. mode = get_multiplier_mode_with_index(i);
  93. if (!snd_interval_test(r, freq_table[i]))
  94. continue;
  95. t.min = min(t.min, pcm_channels[mode]);
  96. t.max = max(t.max, pcm_channels[mode]);
  97. }
  98. return snd_interval_refine(c, &t);
  99. }
  100. static void
  101. limit_channels(struct snd_pcm_hardware *hw, unsigned int *pcm_channels)
  102. {
  103. unsigned int i, mode;
  104. hw->channels_min = UINT_MAX;
  105. hw->channels_max = 0;
  106. for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
  107. mode = get_multiplier_mode_with_index(i);
  108. if (pcm_channels[mode] == 0)
  109. continue;
  110. hw->channels_min = min(hw->channels_min, pcm_channels[mode]);
  111. hw->channels_max = max(hw->channels_max, pcm_channels[mode]);
  112. }
  113. }
  114. static void
  115. limit_period_and_buffer(struct snd_pcm_hardware *hw)
  116. {
  117. hw->periods_min = 2; /* SNDRV_PCM_INFO_BATCH */
  118. hw->periods_max = UINT_MAX;
  119. hw->period_bytes_min = 4 * hw->channels_max; /* bytes for a frame */
  120. /* Just to prevent from allocating much pages. */
  121. hw->period_bytes_max = hw->period_bytes_min * 2048;
  122. hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
  123. }
  124. static int
  125. pcm_init_hw_params(struct snd_efw *efw,
  126. struct snd_pcm_substream *substream)
  127. {
  128. struct snd_pcm_runtime *runtime = substream->runtime;
  129. struct amdtp_stream *s;
  130. unsigned int *pcm_channels;
  131. int err;
  132. runtime->hw.info = SNDRV_PCM_INFO_BATCH |
  133. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  134. SNDRV_PCM_INFO_INTERLEAVED |
  135. SNDRV_PCM_INFO_JOINT_DUPLEX |
  136. SNDRV_PCM_INFO_MMAP |
  137. SNDRV_PCM_INFO_MMAP_VALID;
  138. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  139. runtime->hw.formats = AM824_IN_PCM_FORMAT_BITS;
  140. s = &efw->tx_stream;
  141. pcm_channels = efw->pcm_capture_channels;
  142. } else {
  143. runtime->hw.formats = AM824_OUT_PCM_FORMAT_BITS;
  144. s = &efw->rx_stream;
  145. pcm_channels = efw->pcm_playback_channels;
  146. }
  147. /* limit rates */
  148. runtime->hw.rates = efw->supported_sampling_rate,
  149. snd_pcm_limit_hw_rates(runtime);
  150. limit_channels(&runtime->hw, pcm_channels);
  151. limit_period_and_buffer(&runtime->hw);
  152. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  153. hw_rule_channels, pcm_channels,
  154. SNDRV_PCM_HW_PARAM_RATE, -1);
  155. if (err < 0)
  156. goto end;
  157. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  158. hw_rule_rate, pcm_channels,
  159. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  160. if (err < 0)
  161. goto end;
  162. err = amdtp_am824_add_pcm_hw_constraints(s, runtime);
  163. end:
  164. return err;
  165. }
  166. static int pcm_open(struct snd_pcm_substream *substream)
  167. {
  168. struct snd_efw *efw = substream->private_data;
  169. unsigned int sampling_rate;
  170. enum snd_efw_clock_source clock_source;
  171. int err;
  172. err = snd_efw_stream_lock_try(efw);
  173. if (err < 0)
  174. goto end;
  175. err = pcm_init_hw_params(efw, substream);
  176. if (err < 0)
  177. goto err_locked;
  178. err = snd_efw_command_get_clock_source(efw, &clock_source);
  179. if (err < 0)
  180. goto err_locked;
  181. /*
  182. * When source of clock is not internal or any PCM streams are running,
  183. * available sampling rate is limited at current sampling rate.
  184. */
  185. if ((clock_source != SND_EFW_CLOCK_SOURCE_INTERNAL) ||
  186. amdtp_stream_pcm_running(&efw->tx_stream) ||
  187. amdtp_stream_pcm_running(&efw->rx_stream)) {
  188. err = snd_efw_command_get_sampling_rate(efw, &sampling_rate);
  189. if (err < 0)
  190. goto err_locked;
  191. substream->runtime->hw.rate_min = sampling_rate;
  192. substream->runtime->hw.rate_max = sampling_rate;
  193. }
  194. snd_pcm_set_sync(substream);
  195. end:
  196. return err;
  197. err_locked:
  198. snd_efw_stream_lock_release(efw);
  199. return err;
  200. }
  201. static int pcm_close(struct snd_pcm_substream *substream)
  202. {
  203. struct snd_efw *efw = substream->private_data;
  204. snd_efw_stream_lock_release(efw);
  205. return 0;
  206. }
  207. static int pcm_capture_hw_params(struct snd_pcm_substream *substream,
  208. struct snd_pcm_hw_params *hw_params)
  209. {
  210. struct snd_efw *efw = substream->private_data;
  211. int err;
  212. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  213. params_buffer_bytes(hw_params));
  214. if (err < 0)
  215. return err;
  216. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
  217. atomic_inc(&efw->capture_substreams);
  218. amdtp_am824_set_pcm_format(&efw->tx_stream, params_format(hw_params));
  219. return 0;
  220. }
  221. static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
  222. struct snd_pcm_hw_params *hw_params)
  223. {
  224. struct snd_efw *efw = substream->private_data;
  225. int err;
  226. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  227. params_buffer_bytes(hw_params));
  228. if (err < 0)
  229. return err;
  230. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
  231. atomic_inc(&efw->playback_substreams);
  232. amdtp_am824_set_pcm_format(&efw->rx_stream, params_format(hw_params));
  233. return 0;
  234. }
  235. static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
  236. {
  237. struct snd_efw *efw = substream->private_data;
  238. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  239. atomic_dec(&efw->capture_substreams);
  240. snd_efw_stream_stop_duplex(efw);
  241. return snd_pcm_lib_free_vmalloc_buffer(substream);
  242. }
  243. static int pcm_playback_hw_free(struct snd_pcm_substream *substream)
  244. {
  245. struct snd_efw *efw = substream->private_data;
  246. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  247. atomic_dec(&efw->playback_substreams);
  248. snd_efw_stream_stop_duplex(efw);
  249. return snd_pcm_lib_free_vmalloc_buffer(substream);
  250. }
  251. static int pcm_capture_prepare(struct snd_pcm_substream *substream)
  252. {
  253. struct snd_efw *efw = substream->private_data;
  254. struct snd_pcm_runtime *runtime = substream->runtime;
  255. int err;
  256. err = snd_efw_stream_start_duplex(efw, runtime->rate);
  257. if (err >= 0)
  258. amdtp_stream_pcm_prepare(&efw->tx_stream);
  259. return err;
  260. }
  261. static int pcm_playback_prepare(struct snd_pcm_substream *substream)
  262. {
  263. struct snd_efw *efw = substream->private_data;
  264. struct snd_pcm_runtime *runtime = substream->runtime;
  265. int err;
  266. err = snd_efw_stream_start_duplex(efw, runtime->rate);
  267. if (err >= 0)
  268. amdtp_stream_pcm_prepare(&efw->rx_stream);
  269. return err;
  270. }
  271. static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  272. {
  273. struct snd_efw *efw = substream->private_data;
  274. switch (cmd) {
  275. case SNDRV_PCM_TRIGGER_START:
  276. amdtp_stream_pcm_trigger(&efw->tx_stream, substream);
  277. break;
  278. case SNDRV_PCM_TRIGGER_STOP:
  279. amdtp_stream_pcm_trigger(&efw->tx_stream, NULL);
  280. break;
  281. default:
  282. return -EINVAL;
  283. }
  284. return 0;
  285. }
  286. static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  287. {
  288. struct snd_efw *efw = substream->private_data;
  289. switch (cmd) {
  290. case SNDRV_PCM_TRIGGER_START:
  291. amdtp_stream_pcm_trigger(&efw->rx_stream, substream);
  292. break;
  293. case SNDRV_PCM_TRIGGER_STOP:
  294. amdtp_stream_pcm_trigger(&efw->rx_stream, NULL);
  295. break;
  296. default:
  297. return -EINVAL;
  298. }
  299. return 0;
  300. }
  301. static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
  302. {
  303. struct snd_efw *efw = sbstrm->private_data;
  304. return amdtp_stream_pcm_pointer(&efw->tx_stream);
  305. }
  306. static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
  307. {
  308. struct snd_efw *efw = sbstrm->private_data;
  309. return amdtp_stream_pcm_pointer(&efw->rx_stream);
  310. }
  311. static const struct snd_pcm_ops pcm_capture_ops = {
  312. .open = pcm_open,
  313. .close = pcm_close,
  314. .ioctl = snd_pcm_lib_ioctl,
  315. .hw_params = pcm_capture_hw_params,
  316. .hw_free = pcm_capture_hw_free,
  317. .prepare = pcm_capture_prepare,
  318. .trigger = pcm_capture_trigger,
  319. .pointer = pcm_capture_pointer,
  320. .page = snd_pcm_lib_get_vmalloc_page,
  321. };
  322. static const struct snd_pcm_ops pcm_playback_ops = {
  323. .open = pcm_open,
  324. .close = pcm_close,
  325. .ioctl = snd_pcm_lib_ioctl,
  326. .hw_params = pcm_playback_hw_params,
  327. .hw_free = pcm_playback_hw_free,
  328. .prepare = pcm_playback_prepare,
  329. .trigger = pcm_playback_trigger,
  330. .pointer = pcm_playback_pointer,
  331. .page = snd_pcm_lib_get_vmalloc_page,
  332. .mmap = snd_pcm_lib_mmap_vmalloc,
  333. };
  334. int snd_efw_create_pcm_devices(struct snd_efw *efw)
  335. {
  336. struct snd_pcm *pcm;
  337. int err;
  338. err = snd_pcm_new(efw->card, efw->card->driver, 0, 1, 1, &pcm);
  339. if (err < 0)
  340. goto end;
  341. pcm->private_data = efw;
  342. snprintf(pcm->name, sizeof(pcm->name), "%s PCM", efw->card->shortname);
  343. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops);
  344. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);
  345. end:
  346. return err;
  347. }