oxfw-pcm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * oxfw_pcm.c - a part of driver for OXFW970/971 based devices
  3. *
  4. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  5. * Licensed under the terms of the GNU General Public License, version 2.
  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 void limit_period_and_buffer(struct snd_pcm_hardware *hw)
  91. {
  92. hw->periods_min = 2; /* SNDRV_PCM_INFO_BATCH */
  93. hw->periods_max = UINT_MAX;
  94. hw->period_bytes_min = 4 * hw->channels_max; /* bytes for a frame */
  95. /* Just to prevent from allocating much pages. */
  96. hw->period_bytes_max = hw->period_bytes_min * 2048;
  97. hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
  98. }
  99. static int init_hw_params(struct snd_oxfw *oxfw,
  100. struct snd_pcm_substream *substream)
  101. {
  102. struct snd_pcm_runtime *runtime = substream->runtime;
  103. u8 **formats;
  104. struct amdtp_stream *stream;
  105. int err;
  106. runtime->hw.info = SNDRV_PCM_INFO_BATCH |
  107. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  108. SNDRV_PCM_INFO_INTERLEAVED |
  109. SNDRV_PCM_INFO_JOINT_DUPLEX |
  110. SNDRV_PCM_INFO_MMAP |
  111. SNDRV_PCM_INFO_MMAP_VALID;
  112. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  113. runtime->hw.formats = AMDTP_IN_PCM_FORMAT_BITS;
  114. stream = &oxfw->tx_stream;
  115. formats = oxfw->tx_stream_formats;
  116. } else {
  117. runtime->hw.formats = AMDTP_OUT_PCM_FORMAT_BITS;
  118. stream = &oxfw->rx_stream;
  119. formats = oxfw->rx_stream_formats;
  120. }
  121. limit_channels_and_rates(&runtime->hw, formats);
  122. limit_period_and_buffer(&runtime->hw);
  123. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  124. hw_rule_channels, formats,
  125. SNDRV_PCM_HW_PARAM_RATE, -1);
  126. if (err < 0)
  127. goto end;
  128. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  129. hw_rule_rate, formats,
  130. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  131. if (err < 0)
  132. goto end;
  133. err = amdtp_stream_add_pcm_hw_constraints(stream, runtime);
  134. end:
  135. return err;
  136. }
  137. static int limit_to_current_params(struct snd_pcm_substream *substream)
  138. {
  139. struct snd_oxfw *oxfw = substream->private_data;
  140. struct snd_oxfw_stream_formation formation;
  141. enum avc_general_plug_dir dir;
  142. int err;
  143. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  144. dir = AVC_GENERAL_PLUG_DIR_OUT;
  145. else
  146. dir = AVC_GENERAL_PLUG_DIR_IN;
  147. err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation);
  148. if (err < 0)
  149. goto end;
  150. substream->runtime->hw.channels_min = formation.pcm;
  151. substream->runtime->hw.channels_max = formation.pcm;
  152. substream->runtime->hw.rate_min = formation.rate;
  153. substream->runtime->hw.rate_max = formation.rate;
  154. end:
  155. return err;
  156. }
  157. static int pcm_open(struct snd_pcm_substream *substream)
  158. {
  159. struct snd_oxfw *oxfw = substream->private_data;
  160. int err;
  161. err = snd_oxfw_stream_lock_try(oxfw);
  162. if (err < 0)
  163. goto end;
  164. err = init_hw_params(oxfw, substream);
  165. if (err < 0)
  166. goto err_locked;
  167. /*
  168. * When any PCM streams are already running, the available sampling
  169. * rate is limited at current value.
  170. */
  171. if (amdtp_stream_pcm_running(&oxfw->tx_stream) ||
  172. amdtp_stream_pcm_running(&oxfw->rx_stream)) {
  173. err = limit_to_current_params(substream);
  174. if (err < 0)
  175. goto end;
  176. }
  177. snd_pcm_set_sync(substream);
  178. end:
  179. return err;
  180. err_locked:
  181. snd_oxfw_stream_lock_release(oxfw);
  182. return err;
  183. }
  184. static int pcm_close(struct snd_pcm_substream *substream)
  185. {
  186. struct snd_oxfw *oxfw = substream->private_data;
  187. snd_oxfw_stream_lock_release(oxfw);
  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_oxfw *oxfw = substream->private_data;
  194. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  195. mutex_lock(&oxfw->mutex);
  196. oxfw->capture_substreams++;
  197. mutex_unlock(&oxfw->mutex);
  198. }
  199. amdtp_stream_set_pcm_format(&oxfw->tx_stream, params_format(hw_params));
  200. return snd_pcm_lib_alloc_vmalloc_buffer(substream,
  201. params_buffer_bytes(hw_params));
  202. }
  203. static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
  204. struct snd_pcm_hw_params *hw_params)
  205. {
  206. struct snd_oxfw *oxfw = substream->private_data;
  207. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  208. mutex_lock(&oxfw->mutex);
  209. oxfw->playback_substreams++;
  210. mutex_unlock(&oxfw->mutex);
  211. }
  212. amdtp_stream_set_pcm_format(&oxfw->rx_stream, params_format(hw_params));
  213. return snd_pcm_lib_alloc_vmalloc_buffer(substream,
  214. params_buffer_bytes(hw_params));
  215. }
  216. static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
  217. {
  218. struct snd_oxfw *oxfw = substream->private_data;
  219. mutex_lock(&oxfw->mutex);
  220. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  221. oxfw->capture_substreams--;
  222. snd_oxfw_stream_stop_simplex(oxfw, &oxfw->tx_stream);
  223. mutex_unlock(&oxfw->mutex);
  224. return snd_pcm_lib_free_vmalloc_buffer(substream);
  225. }
  226. static int pcm_playback_hw_free(struct snd_pcm_substream *substream)
  227. {
  228. struct snd_oxfw *oxfw = substream->private_data;
  229. mutex_lock(&oxfw->mutex);
  230. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  231. oxfw->playback_substreams--;
  232. snd_oxfw_stream_stop_simplex(oxfw, &oxfw->rx_stream);
  233. mutex_unlock(&oxfw->mutex);
  234. return snd_pcm_lib_free_vmalloc_buffer(substream);
  235. }
  236. static int pcm_capture_prepare(struct snd_pcm_substream *substream)
  237. {
  238. struct snd_oxfw *oxfw = substream->private_data;
  239. struct snd_pcm_runtime *runtime = substream->runtime;
  240. int err;
  241. mutex_lock(&oxfw->mutex);
  242. err = snd_oxfw_stream_start_simplex(oxfw, &oxfw->tx_stream,
  243. runtime->rate, runtime->channels);
  244. mutex_unlock(&oxfw->mutex);
  245. if (err < 0)
  246. goto end;
  247. amdtp_stream_pcm_prepare(&oxfw->tx_stream);
  248. end:
  249. return err;
  250. }
  251. static int pcm_playback_prepare(struct snd_pcm_substream *substream)
  252. {
  253. struct snd_oxfw *oxfw = substream->private_data;
  254. struct snd_pcm_runtime *runtime = substream->runtime;
  255. int err;
  256. mutex_lock(&oxfw->mutex);
  257. err = snd_oxfw_stream_start_simplex(oxfw, &oxfw->rx_stream,
  258. runtime->rate, runtime->channels);
  259. mutex_unlock(&oxfw->mutex);
  260. if (err < 0)
  261. goto end;
  262. amdtp_stream_pcm_prepare(&oxfw->rx_stream);
  263. end:
  264. return err;
  265. }
  266. static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  267. {
  268. struct snd_oxfw *oxfw = substream->private_data;
  269. struct snd_pcm_substream *pcm;
  270. switch (cmd) {
  271. case SNDRV_PCM_TRIGGER_START:
  272. pcm = substream;
  273. break;
  274. case SNDRV_PCM_TRIGGER_STOP:
  275. pcm = NULL;
  276. break;
  277. default:
  278. return -EINVAL;
  279. }
  280. amdtp_stream_pcm_trigger(&oxfw->tx_stream, pcm);
  281. return 0;
  282. }
  283. static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  284. {
  285. struct snd_oxfw *oxfw = substream->private_data;
  286. struct snd_pcm_substream *pcm;
  287. switch (cmd) {
  288. case SNDRV_PCM_TRIGGER_START:
  289. pcm = substream;
  290. break;
  291. case SNDRV_PCM_TRIGGER_STOP:
  292. pcm = NULL;
  293. break;
  294. default:
  295. return -EINVAL;
  296. }
  297. amdtp_stream_pcm_trigger(&oxfw->rx_stream, pcm);
  298. return 0;
  299. }
  300. static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstm)
  301. {
  302. struct snd_oxfw *oxfw = sbstm->private_data;
  303. return amdtp_stream_pcm_pointer(&oxfw->tx_stream);
  304. }
  305. static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstm)
  306. {
  307. struct snd_oxfw *oxfw = sbstm->private_data;
  308. return amdtp_stream_pcm_pointer(&oxfw->rx_stream);
  309. }
  310. int snd_oxfw_create_pcm(struct snd_oxfw *oxfw)
  311. {
  312. static struct snd_pcm_ops capture_ops = {
  313. .open = pcm_open,
  314. .close = pcm_close,
  315. .ioctl = snd_pcm_lib_ioctl,
  316. .hw_params = pcm_capture_hw_params,
  317. .hw_free = pcm_capture_hw_free,
  318. .prepare = pcm_capture_prepare,
  319. .trigger = pcm_capture_trigger,
  320. .pointer = pcm_capture_pointer,
  321. .page = snd_pcm_lib_get_vmalloc_page,
  322. .mmap = snd_pcm_lib_mmap_vmalloc,
  323. };
  324. static struct snd_pcm_ops playback_ops = {
  325. .open = pcm_open,
  326. .close = pcm_close,
  327. .ioctl = snd_pcm_lib_ioctl,
  328. .hw_params = pcm_playback_hw_params,
  329. .hw_free = pcm_playback_hw_free,
  330. .prepare = pcm_playback_prepare,
  331. .trigger = pcm_playback_trigger,
  332. .pointer = pcm_playback_pointer,
  333. .page = snd_pcm_lib_get_vmalloc_page,
  334. .mmap = snd_pcm_lib_mmap_vmalloc,
  335. };
  336. struct snd_pcm *pcm;
  337. unsigned int cap = 0;
  338. int err;
  339. if (oxfw->has_output)
  340. cap = 1;
  341. err = snd_pcm_new(oxfw->card, oxfw->card->driver, 0, 1, cap, &pcm);
  342. if (err < 0)
  343. return err;
  344. pcm->private_data = oxfw;
  345. strcpy(pcm->name, oxfw->card->shortname);
  346. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
  347. if (cap > 0)
  348. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
  349. return 0;
  350. }