oxfw-pcm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * oxfw_pcm.c - a part of driver for OXFW970/971 based devices
  4. *
  5. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  6. */
  7. #include "oxfw.h"
  8. static int hw_rule_rate(struct snd_pcm_hw_params *params,
  9. struct snd_pcm_hw_rule *rule)
  10. {
  11. u8 **formats = rule->private;
  12. struct snd_interval *r =
  13. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  14. const struct snd_interval *c =
  15. hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  16. struct snd_interval t = {
  17. .min = UINT_MAX, .max = 0, .integer = 1
  18. };
  19. struct snd_oxfw_stream_formation formation;
  20. int i, err;
  21. for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
  22. if (formats[i] == NULL)
  23. continue;
  24. err = snd_oxfw_stream_parse_format(formats[i], &formation);
  25. if (err < 0)
  26. continue;
  27. if (!snd_interval_test(c, formation.pcm))
  28. continue;
  29. t.min = min(t.min, formation.rate);
  30. t.max = max(t.max, formation.rate);
  31. }
  32. return snd_interval_refine(r, &t);
  33. }
  34. static int hw_rule_channels(struct snd_pcm_hw_params *params,
  35. struct snd_pcm_hw_rule *rule)
  36. {
  37. u8 **formats = rule->private;
  38. struct snd_interval *c =
  39. hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  40. const struct snd_interval *r =
  41. hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
  42. struct snd_oxfw_stream_formation formation;
  43. int i, j, err;
  44. unsigned int count, list[SND_OXFW_STREAM_FORMAT_ENTRIES] = {0};
  45. count = 0;
  46. for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
  47. if (formats[i] == NULL)
  48. break;
  49. err = snd_oxfw_stream_parse_format(formats[i], &formation);
  50. if (err < 0)
  51. continue;
  52. if (!snd_interval_test(r, formation.rate))
  53. continue;
  54. if (list[count] == formation.pcm)
  55. continue;
  56. for (j = 0; j < ARRAY_SIZE(list); j++) {
  57. if (list[j] == formation.pcm)
  58. break;
  59. }
  60. if (j == ARRAY_SIZE(list)) {
  61. list[count] = formation.pcm;
  62. if (++count == ARRAY_SIZE(list))
  63. break;
  64. }
  65. }
  66. return snd_interval_list(c, count, list, 0);
  67. }
  68. static void limit_channels_and_rates(struct snd_pcm_hardware *hw, u8 **formats)
  69. {
  70. struct snd_oxfw_stream_formation formation;
  71. int i, err;
  72. hw->channels_min = UINT_MAX;
  73. hw->channels_max = 0;
  74. hw->rate_min = UINT_MAX;
  75. hw->rate_max = 0;
  76. hw->rates = 0;
  77. for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
  78. if (formats[i] == NULL)
  79. break;
  80. err = snd_oxfw_stream_parse_format(formats[i], &formation);
  81. if (err < 0)
  82. continue;
  83. hw->channels_min = min(hw->channels_min, formation.pcm);
  84. hw->channels_max = max(hw->channels_max, formation.pcm);
  85. hw->rate_min = min(hw->rate_min, formation.rate);
  86. hw->rate_max = max(hw->rate_max, formation.rate);
  87. hw->rates |= snd_pcm_rate_to_rate_bit(formation.rate);
  88. }
  89. }
  90. static int init_hw_params(struct snd_oxfw *oxfw,
  91. struct snd_pcm_substream *substream)
  92. {
  93. struct snd_pcm_runtime *runtime = substream->runtime;
  94. u8 **formats;
  95. struct amdtp_stream *stream;
  96. int err;
  97. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  98. runtime->hw.formats = AM824_IN_PCM_FORMAT_BITS;
  99. stream = &oxfw->tx_stream;
  100. formats = oxfw->tx_stream_formats;
  101. } else {
  102. runtime->hw.formats = AM824_OUT_PCM_FORMAT_BITS;
  103. stream = &oxfw->rx_stream;
  104. formats = oxfw->rx_stream_formats;
  105. }
  106. limit_channels_and_rates(&runtime->hw, formats);
  107. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  108. hw_rule_channels, formats,
  109. SNDRV_PCM_HW_PARAM_RATE, -1);
  110. if (err < 0)
  111. goto end;
  112. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  113. hw_rule_rate, formats,
  114. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  115. if (err < 0)
  116. goto end;
  117. err = amdtp_am824_add_pcm_hw_constraints(stream, runtime);
  118. end:
  119. return err;
  120. }
  121. static int limit_to_current_params(struct snd_pcm_substream *substream)
  122. {
  123. struct snd_oxfw *oxfw = substream->private_data;
  124. struct snd_oxfw_stream_formation formation;
  125. enum avc_general_plug_dir dir;
  126. int err;
  127. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  128. dir = AVC_GENERAL_PLUG_DIR_OUT;
  129. else
  130. dir = AVC_GENERAL_PLUG_DIR_IN;
  131. err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
  132. if (err < 0)
  133. goto end;
  134. substream->runtime->hw.channels_min = formation.pcm;
  135. substream->runtime->hw.channels_max = formation.pcm;
  136. substream->runtime->hw.rate_min = formation.rate;
  137. substream->runtime->hw.rate_max = formation.rate;
  138. end:
  139. return err;
  140. }
  141. static int pcm_open(struct snd_pcm_substream *substream)
  142. {
  143. struct snd_oxfw *oxfw = substream->private_data;
  144. struct amdtp_domain *d = &oxfw->domain;
  145. int err;
  146. err = snd_oxfw_stream_lock_try(oxfw);
  147. if (err < 0)
  148. return err;
  149. err = init_hw_params(oxfw, substream);
  150. if (err < 0)
  151. goto err_locked;
  152. mutex_lock(&oxfw->mutex);
  153. // When source of clock is not internal or any stream is reserved for
  154. // transmission of PCM frames, the available sampling rate is limited
  155. // at current one.
  156. if (oxfw->substreams_count > 0 && d->events_per_period > 0) {
  157. unsigned int frames_per_period = d->events_per_period;
  158. unsigned int frames_per_buffer = d->events_per_buffer;
  159. err = limit_to_current_params(substream);
  160. if (err < 0) {
  161. mutex_unlock(&oxfw->mutex);
  162. goto err_locked;
  163. }
  164. if (frames_per_period > 0) {
  165. err = snd_pcm_hw_constraint_minmax(substream->runtime,
  166. SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  167. frames_per_period, frames_per_period);
  168. if (err < 0) {
  169. mutex_unlock(&oxfw->mutex);
  170. goto err_locked;
  171. }
  172. err = snd_pcm_hw_constraint_minmax(substream->runtime,
  173. SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  174. frames_per_buffer, frames_per_buffer);
  175. if (err < 0) {
  176. mutex_unlock(&oxfw->mutex);
  177. goto err_locked;
  178. }
  179. }
  180. }
  181. mutex_unlock(&oxfw->mutex);
  182. snd_pcm_set_sync(substream);
  183. return 0;
  184. err_locked:
  185. snd_oxfw_stream_lock_release(oxfw);
  186. return err;
  187. }
  188. static int pcm_close(struct snd_pcm_substream *substream)
  189. {
  190. struct snd_oxfw *oxfw = substream->private_data;
  191. snd_oxfw_stream_lock_release(oxfw);
  192. return 0;
  193. }
  194. static int pcm_capture_hw_params(struct snd_pcm_substream *substream,
  195. struct snd_pcm_hw_params *hw_params)
  196. {
  197. struct snd_oxfw *oxfw = substream->private_data;
  198. int err = 0;
  199. if (substream->runtime->state == SNDRV_PCM_STATE_OPEN) {
  200. unsigned int rate = params_rate(hw_params);
  201. unsigned int channels = params_channels(hw_params);
  202. unsigned int frames_per_period = params_period_size(hw_params);
  203. unsigned int frames_per_buffer = params_buffer_size(hw_params);
  204. mutex_lock(&oxfw->mutex);
  205. err = snd_oxfw_stream_reserve_duplex(oxfw, &oxfw->tx_stream,
  206. rate, channels, frames_per_period,
  207. frames_per_buffer);
  208. if (err >= 0)
  209. ++oxfw->substreams_count;
  210. mutex_unlock(&oxfw->mutex);
  211. }
  212. return err;
  213. }
  214. static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
  215. struct snd_pcm_hw_params *hw_params)
  216. {
  217. struct snd_oxfw *oxfw = substream->private_data;
  218. int err = 0;
  219. if (substream->runtime->state == SNDRV_PCM_STATE_OPEN) {
  220. unsigned int rate = params_rate(hw_params);
  221. unsigned int channels = params_channels(hw_params);
  222. unsigned int frames_per_period = params_period_size(hw_params);
  223. unsigned int frames_per_buffer = params_buffer_size(hw_params);
  224. mutex_lock(&oxfw->mutex);
  225. err = snd_oxfw_stream_reserve_duplex(oxfw, &oxfw->rx_stream,
  226. rate, channels, frames_per_period,
  227. frames_per_buffer);
  228. if (err >= 0)
  229. ++oxfw->substreams_count;
  230. mutex_unlock(&oxfw->mutex);
  231. }
  232. return err;
  233. }
  234. static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
  235. {
  236. struct snd_oxfw *oxfw = substream->private_data;
  237. mutex_lock(&oxfw->mutex);
  238. if (substream->runtime->state != SNDRV_PCM_STATE_OPEN)
  239. --oxfw->substreams_count;
  240. snd_oxfw_stream_stop_duplex(oxfw);
  241. mutex_unlock(&oxfw->mutex);
  242. return 0;
  243. }
  244. static int pcm_playback_hw_free(struct snd_pcm_substream *substream)
  245. {
  246. struct snd_oxfw *oxfw = substream->private_data;
  247. mutex_lock(&oxfw->mutex);
  248. if (substream->runtime->state != SNDRV_PCM_STATE_OPEN)
  249. --oxfw->substreams_count;
  250. snd_oxfw_stream_stop_duplex(oxfw);
  251. mutex_unlock(&oxfw->mutex);
  252. return 0;
  253. }
  254. static int pcm_capture_prepare(struct snd_pcm_substream *substream)
  255. {
  256. struct snd_oxfw *oxfw = substream->private_data;
  257. int err;
  258. mutex_lock(&oxfw->mutex);
  259. err = snd_oxfw_stream_start_duplex(oxfw);
  260. mutex_unlock(&oxfw->mutex);
  261. if (err < 0)
  262. goto end;
  263. amdtp_stream_pcm_prepare(&oxfw->tx_stream);
  264. end:
  265. return err;
  266. }
  267. static int pcm_playback_prepare(struct snd_pcm_substream *substream)
  268. {
  269. struct snd_oxfw *oxfw = substream->private_data;
  270. int err;
  271. mutex_lock(&oxfw->mutex);
  272. err = snd_oxfw_stream_start_duplex(oxfw);
  273. mutex_unlock(&oxfw->mutex);
  274. if (err < 0)
  275. goto end;
  276. amdtp_stream_pcm_prepare(&oxfw->rx_stream);
  277. end:
  278. return err;
  279. }
  280. static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  281. {
  282. struct snd_oxfw *oxfw = substream->private_data;
  283. struct snd_pcm_substream *pcm;
  284. switch (cmd) {
  285. case SNDRV_PCM_TRIGGER_START:
  286. pcm = substream;
  287. break;
  288. case SNDRV_PCM_TRIGGER_STOP:
  289. pcm = NULL;
  290. break;
  291. default:
  292. return -EINVAL;
  293. }
  294. amdtp_stream_pcm_trigger(&oxfw->tx_stream, pcm);
  295. return 0;
  296. }
  297. static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  298. {
  299. struct snd_oxfw *oxfw = substream->private_data;
  300. struct snd_pcm_substream *pcm;
  301. switch (cmd) {
  302. case SNDRV_PCM_TRIGGER_START:
  303. pcm = substream;
  304. break;
  305. case SNDRV_PCM_TRIGGER_STOP:
  306. pcm = NULL;
  307. break;
  308. default:
  309. return -EINVAL;
  310. }
  311. amdtp_stream_pcm_trigger(&oxfw->rx_stream, pcm);
  312. return 0;
  313. }
  314. static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstm)
  315. {
  316. struct snd_oxfw *oxfw = sbstm->private_data;
  317. return amdtp_domain_stream_pcm_pointer(&oxfw->domain, &oxfw->tx_stream);
  318. }
  319. static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstm)
  320. {
  321. struct snd_oxfw *oxfw = sbstm->private_data;
  322. return amdtp_domain_stream_pcm_pointer(&oxfw->domain, &oxfw->rx_stream);
  323. }
  324. static int pcm_capture_ack(struct snd_pcm_substream *substream)
  325. {
  326. struct snd_oxfw *oxfw = substream->private_data;
  327. return amdtp_domain_stream_pcm_ack(&oxfw->domain, &oxfw->tx_stream);
  328. }
  329. static int pcm_playback_ack(struct snd_pcm_substream *substream)
  330. {
  331. struct snd_oxfw *oxfw = substream->private_data;
  332. return amdtp_domain_stream_pcm_ack(&oxfw->domain, &oxfw->rx_stream);
  333. }
  334. int snd_oxfw_create_pcm(struct snd_oxfw *oxfw)
  335. {
  336. static const struct snd_pcm_ops capture_ops = {
  337. .open = pcm_open,
  338. .close = pcm_close,
  339. .hw_params = pcm_capture_hw_params,
  340. .hw_free = pcm_capture_hw_free,
  341. .prepare = pcm_capture_prepare,
  342. .trigger = pcm_capture_trigger,
  343. .pointer = pcm_capture_pointer,
  344. .ack = pcm_capture_ack,
  345. };
  346. static const struct snd_pcm_ops playback_ops = {
  347. .open = pcm_open,
  348. .close = pcm_close,
  349. .hw_params = pcm_playback_hw_params,
  350. .hw_free = pcm_playback_hw_free,
  351. .prepare = pcm_playback_prepare,
  352. .trigger = pcm_playback_trigger,
  353. .pointer = pcm_playback_pointer,
  354. .ack = pcm_playback_ack,
  355. };
  356. struct snd_pcm *pcm;
  357. unsigned int cap = 0;
  358. int err;
  359. if (oxfw->has_output)
  360. cap = 1;
  361. err = snd_pcm_new(oxfw->card, oxfw->card->driver, 0, 1, cap, &pcm);
  362. if (err < 0)
  363. return err;
  364. pcm->private_data = oxfw;
  365. pcm->nonatomic = true;
  366. strcpy(pcm->name, oxfw->card->shortname);
  367. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
  368. if (cap > 0)
  369. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
  370. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
  371. return 0;
  372. }