fireworks_pcm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 = AMDTP_IN_PCM_FORMAT_BITS;
  140. s = &efw->tx_stream;
  141. pcm_channels = efw->pcm_capture_channels;
  142. } else {
  143. runtime->hw.formats = AMDTP_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_stream_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. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
  212. atomic_inc(&efw->capture_substreams);
  213. amdtp_stream_set_pcm_format(&efw->tx_stream, params_format(hw_params));
  214. return snd_pcm_lib_alloc_vmalloc_buffer(substream,
  215. params_buffer_bytes(hw_params));
  216. }
  217. static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
  218. struct snd_pcm_hw_params *hw_params)
  219. {
  220. struct snd_efw *efw = substream->private_data;
  221. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN)
  222. atomic_inc(&efw->playback_substreams);
  223. amdtp_stream_set_pcm_format(&efw->rx_stream, params_format(hw_params));
  224. return snd_pcm_lib_alloc_vmalloc_buffer(substream,
  225. params_buffer_bytes(hw_params));
  226. }
  227. static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
  228. {
  229. struct snd_efw *efw = substream->private_data;
  230. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  231. atomic_dec(&efw->capture_substreams);
  232. snd_efw_stream_stop_duplex(efw);
  233. return snd_pcm_lib_free_vmalloc_buffer(substream);
  234. }
  235. static int pcm_playback_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->playback_substreams);
  240. snd_efw_stream_stop_duplex(efw);
  241. return snd_pcm_lib_free_vmalloc_buffer(substream);
  242. }
  243. static int pcm_capture_prepare(struct snd_pcm_substream *substream)
  244. {
  245. struct snd_efw *efw = substream->private_data;
  246. struct snd_pcm_runtime *runtime = substream->runtime;
  247. int err;
  248. err = snd_efw_stream_start_duplex(efw, runtime->rate);
  249. if (err >= 0)
  250. amdtp_stream_pcm_prepare(&efw->tx_stream);
  251. return err;
  252. }
  253. static int pcm_playback_prepare(struct snd_pcm_substream *substream)
  254. {
  255. struct snd_efw *efw = substream->private_data;
  256. struct snd_pcm_runtime *runtime = substream->runtime;
  257. int err;
  258. err = snd_efw_stream_start_duplex(efw, runtime->rate);
  259. if (err >= 0)
  260. amdtp_stream_pcm_prepare(&efw->rx_stream);
  261. return err;
  262. }
  263. static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  264. {
  265. struct snd_efw *efw = substream->private_data;
  266. switch (cmd) {
  267. case SNDRV_PCM_TRIGGER_START:
  268. amdtp_stream_pcm_trigger(&efw->tx_stream, substream);
  269. break;
  270. case SNDRV_PCM_TRIGGER_STOP:
  271. amdtp_stream_pcm_trigger(&efw->tx_stream, NULL);
  272. break;
  273. default:
  274. return -EINVAL;
  275. }
  276. return 0;
  277. }
  278. static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  279. {
  280. struct snd_efw *efw = substream->private_data;
  281. switch (cmd) {
  282. case SNDRV_PCM_TRIGGER_START:
  283. amdtp_stream_pcm_trigger(&efw->rx_stream, substream);
  284. break;
  285. case SNDRV_PCM_TRIGGER_STOP:
  286. amdtp_stream_pcm_trigger(&efw->rx_stream, NULL);
  287. break;
  288. default:
  289. return -EINVAL;
  290. }
  291. return 0;
  292. }
  293. static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
  294. {
  295. struct snd_efw *efw = sbstrm->private_data;
  296. return amdtp_stream_pcm_pointer(&efw->tx_stream);
  297. }
  298. static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
  299. {
  300. struct snd_efw *efw = sbstrm->private_data;
  301. return amdtp_stream_pcm_pointer(&efw->rx_stream);
  302. }
  303. static const struct snd_pcm_ops pcm_capture_ops = {
  304. .open = pcm_open,
  305. .close = pcm_close,
  306. .ioctl = snd_pcm_lib_ioctl,
  307. .hw_params = pcm_capture_hw_params,
  308. .hw_free = pcm_capture_hw_free,
  309. .prepare = pcm_capture_prepare,
  310. .trigger = pcm_capture_trigger,
  311. .pointer = pcm_capture_pointer,
  312. .page = snd_pcm_lib_get_vmalloc_page,
  313. };
  314. static const struct snd_pcm_ops pcm_playback_ops = {
  315. .open = pcm_open,
  316. .close = pcm_close,
  317. .ioctl = snd_pcm_lib_ioctl,
  318. .hw_params = pcm_playback_hw_params,
  319. .hw_free = pcm_playback_hw_free,
  320. .prepare = pcm_playback_prepare,
  321. .trigger = pcm_playback_trigger,
  322. .pointer = pcm_playback_pointer,
  323. .page = snd_pcm_lib_get_vmalloc_page,
  324. .mmap = snd_pcm_lib_mmap_vmalloc,
  325. };
  326. int snd_efw_create_pcm_devices(struct snd_efw *efw)
  327. {
  328. struct snd_pcm *pcm;
  329. int err;
  330. err = snd_pcm_new(efw->card, efw->card->driver, 0, 1, 1, &pcm);
  331. if (err < 0)
  332. goto end;
  333. pcm->private_data = efw;
  334. snprintf(pcm->name, sizeof(pcm->name), "%s PCM", efw->card->shortname);
  335. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops);
  336. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_capture_ops);
  337. end:
  338. return err;
  339. }