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 int
  115. pcm_init_hw_params(struct snd_efw *efw,
  116. struct snd_pcm_substream *substream)
  117. {
  118. struct snd_pcm_runtime *runtime = substream->runtime;
  119. struct amdtp_stream *s;
  120. unsigned int *pcm_channels;
  121. int err;
  122. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  123. runtime->hw.formats = AM824_IN_PCM_FORMAT_BITS;
  124. s = &efw->tx_stream;
  125. pcm_channels = efw->pcm_capture_channels;
  126. } else {
  127. runtime->hw.formats = AM824_OUT_PCM_FORMAT_BITS;
  128. s = &efw->rx_stream;
  129. pcm_channels = efw->pcm_playback_channels;
  130. }
  131. /* limit rates */
  132. runtime->hw.rates = efw->supported_sampling_rate,
  133. snd_pcm_limit_hw_rates(runtime);
  134. limit_channels(&runtime->hw, pcm_channels);
  135. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  136. hw_rule_channels, pcm_channels,
  137. SNDRV_PCM_HW_PARAM_RATE, -1);
  138. if (err < 0)
  139. goto end;
  140. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  141. hw_rule_rate, pcm_channels,
  142. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  143. if (err < 0)
  144. goto end;
  145. err = amdtp_am824_add_pcm_hw_constraints(s, runtime);
  146. end:
  147. return err;
  148. }
  149. static int pcm_open(struct snd_pcm_substream *substream)
  150. {
  151. struct snd_efw *efw = substream->private_data;
  152. unsigned int sampling_rate;
  153. enum snd_efw_clock_source clock_source;
  154. int err;
  155. err = snd_efw_stream_lock_try(efw);
  156. if (err < 0)
  157. goto end;
  158. err = pcm_init_hw_params(efw, substream);
  159. if (err < 0)
  160. goto err_locked;
  161. err = snd_efw_command_get_clock_source(efw, &clock_source);
  162. if (err < 0)
  163. goto err_locked;
  164. /*
  165. * When source of clock is not internal or any PCM streams are running,
  166. * available sampling rate is limited at current sampling rate.
  167. */
  168. if ((clock_source != SND_EFW_CLOCK_SOURCE_INTERNAL) ||
  169. amdtp_stream_pcm_running(&efw->tx_stream) ||
  170. amdtp_stream_pcm_running(&efw->rx_stream)) {
  171. err = snd_efw_command_get_sampling_rate(efw, &sampling_rate);
  172. if (err < 0)
  173. goto err_locked;
  174. substream->runtime->hw.rate_min = sampling_rate;
  175. substream->runtime->hw.rate_max = sampling_rate;
  176. }
  177. snd_pcm_set_sync(substream);
  178. end:
  179. return err;
  180. err_locked:
  181. snd_efw_stream_lock_release(efw);
  182. return err;
  183. }
  184. static int pcm_close(struct snd_pcm_substream *substream)
  185. {
  186. struct snd_efw *efw = substream->private_data;
  187. snd_efw_stream_lock_release(efw);
  188. return 0;
  189. }
  190. static int pcm_capture_hw_params(struct snd_pcm_substream *substream,
  191. struct snd_pcm_hw_params *hw_params)
  192. {
  193. struct snd_efw *efw = substream->private_data;
  194. int err;
  195. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  196. params_buffer_bytes(hw_params));
  197. if (err < 0)
  198. return err;
  199. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  200. mutex_lock(&efw->mutex);
  201. efw->capture_substreams++;
  202. mutex_unlock(&efw->mutex);
  203. }
  204. return 0;
  205. }
  206. static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
  207. struct snd_pcm_hw_params *hw_params)
  208. {
  209. struct snd_efw *efw = substream->private_data;
  210. int err;
  211. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  212. params_buffer_bytes(hw_params));
  213. if (err < 0)
  214. return err;
  215. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  216. mutex_lock(&efw->mutex);
  217. efw->playback_substreams++;
  218. mutex_unlock(&efw->mutex);
  219. }
  220. return 0;
  221. }
  222. static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
  223. {
  224. struct snd_efw *efw = substream->private_data;
  225. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) {
  226. mutex_lock(&efw->mutex);
  227. efw->capture_substreams--;
  228. mutex_unlock(&efw->mutex);
  229. }
  230. snd_efw_stream_stop_duplex(efw);
  231. return snd_pcm_lib_free_vmalloc_buffer(substream);
  232. }
  233. static int pcm_playback_hw_free(struct snd_pcm_substream *substream)
  234. {
  235. struct snd_efw *efw = substream->private_data;
  236. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) {
  237. mutex_lock(&efw->mutex);
  238. efw->playback_substreams--;
  239. mutex_unlock(&efw->mutex);
  240. }
  241. snd_efw_stream_stop_duplex(efw);
  242. return snd_pcm_lib_free_vmalloc_buffer(substream);
  243. }
  244. static int pcm_capture_prepare(struct snd_pcm_substream *substream)
  245. {
  246. struct snd_efw *efw = substream->private_data;
  247. struct snd_pcm_runtime *runtime = substream->runtime;
  248. int err;
  249. err = snd_efw_stream_start_duplex(efw, runtime->rate);
  250. if (err >= 0)
  251. amdtp_stream_pcm_prepare(&efw->tx_stream);
  252. return err;
  253. }
  254. static int pcm_playback_prepare(struct snd_pcm_substream *substream)
  255. {
  256. struct snd_efw *efw = substream->private_data;
  257. struct snd_pcm_runtime *runtime = substream->runtime;
  258. int err;
  259. err = snd_efw_stream_start_duplex(efw, runtime->rate);
  260. if (err >= 0)
  261. amdtp_stream_pcm_prepare(&efw->rx_stream);
  262. return err;
  263. }
  264. static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  265. {
  266. struct snd_efw *efw = substream->private_data;
  267. switch (cmd) {
  268. case SNDRV_PCM_TRIGGER_START:
  269. amdtp_stream_pcm_trigger(&efw->tx_stream, substream);
  270. break;
  271. case SNDRV_PCM_TRIGGER_STOP:
  272. amdtp_stream_pcm_trigger(&efw->tx_stream, NULL);
  273. break;
  274. default:
  275. return -EINVAL;
  276. }
  277. return 0;
  278. }
  279. static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  280. {
  281. struct snd_efw *efw = substream->private_data;
  282. switch (cmd) {
  283. case SNDRV_PCM_TRIGGER_START:
  284. amdtp_stream_pcm_trigger(&efw->rx_stream, substream);
  285. break;
  286. case SNDRV_PCM_TRIGGER_STOP:
  287. amdtp_stream_pcm_trigger(&efw->rx_stream, NULL);
  288. break;
  289. default:
  290. return -EINVAL;
  291. }
  292. return 0;
  293. }
  294. static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstrm)
  295. {
  296. struct snd_efw *efw = sbstrm->private_data;
  297. return amdtp_stream_pcm_pointer(&efw->tx_stream);
  298. }
  299. static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstrm)
  300. {
  301. struct snd_efw *efw = sbstrm->private_data;
  302. return amdtp_stream_pcm_pointer(&efw->rx_stream);
  303. }
  304. static int pcm_capture_ack(struct snd_pcm_substream *substream)
  305. {
  306. struct snd_efw *efw = substream->private_data;
  307. return amdtp_stream_pcm_ack(&efw->tx_stream);
  308. }
  309. static int pcm_playback_ack(struct snd_pcm_substream *substream)
  310. {
  311. struct snd_efw *efw = substream->private_data;
  312. return amdtp_stream_pcm_ack(&efw->rx_stream);
  313. }
  314. int snd_efw_create_pcm_devices(struct snd_efw *efw)
  315. {
  316. static const struct snd_pcm_ops capture_ops = {
  317. .open = pcm_open,
  318. .close = pcm_close,
  319. .ioctl = snd_pcm_lib_ioctl,
  320. .hw_params = pcm_capture_hw_params,
  321. .hw_free = pcm_capture_hw_free,
  322. .prepare = pcm_capture_prepare,
  323. .trigger = pcm_capture_trigger,
  324. .pointer = pcm_capture_pointer,
  325. .ack = pcm_capture_ack,
  326. .page = snd_pcm_lib_get_vmalloc_page,
  327. };
  328. static const struct snd_pcm_ops playback_ops = {
  329. .open = pcm_open,
  330. .close = pcm_close,
  331. .ioctl = snd_pcm_lib_ioctl,
  332. .hw_params = pcm_playback_hw_params,
  333. .hw_free = pcm_playback_hw_free,
  334. .prepare = pcm_playback_prepare,
  335. .trigger = pcm_playback_trigger,
  336. .pointer = pcm_playback_pointer,
  337. .ack = pcm_playback_ack,
  338. .page = snd_pcm_lib_get_vmalloc_page,
  339. };
  340. struct snd_pcm *pcm;
  341. int err;
  342. err = snd_pcm_new(efw->card, efw->card->driver, 0, 1, 1, &pcm);
  343. if (err < 0)
  344. goto end;
  345. pcm->private_data = efw;
  346. snprintf(pcm->name, sizeof(pcm->name), "%s PCM", efw->card->shortname);
  347. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
  348. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
  349. end:
  350. return err;
  351. }