oxfw-pcm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 = AM824_IN_PCM_FORMAT_BITS;
  114. stream = &oxfw->tx_stream;
  115. formats = oxfw->tx_stream_formats;
  116. } else {
  117. runtime->hw.formats = AM824_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_am824_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. 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(&oxfw->mutex);
  201. oxfw->capture_substreams++;
  202. mutex_unlock(&oxfw->mutex);
  203. }
  204. amdtp_am824_set_pcm_format(&oxfw->tx_stream, params_format(hw_params));
  205. return 0;
  206. }
  207. static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
  208. struct snd_pcm_hw_params *hw_params)
  209. {
  210. struct snd_oxfw *oxfw = 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. mutex_lock(&oxfw->mutex);
  218. oxfw->playback_substreams++;
  219. mutex_unlock(&oxfw->mutex);
  220. }
  221. amdtp_am824_set_pcm_format(&oxfw->rx_stream, params_format(hw_params));
  222. return 0;
  223. }
  224. static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
  225. {
  226. struct snd_oxfw *oxfw = substream->private_data;
  227. mutex_lock(&oxfw->mutex);
  228. if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  229. oxfw->capture_substreams--;
  230. snd_oxfw_stream_stop_simplex(oxfw, &oxfw->tx_stream);
  231. mutex_unlock(&oxfw->mutex);
  232. return snd_pcm_lib_free_vmalloc_buffer(substream);
  233. }
  234. static int pcm_playback_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->status->state != SNDRV_PCM_STATE_OPEN)
  239. oxfw->playback_substreams--;
  240. snd_oxfw_stream_stop_simplex(oxfw, &oxfw->rx_stream);
  241. mutex_unlock(&oxfw->mutex);
  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_oxfw *oxfw = substream->private_data;
  247. struct snd_pcm_runtime *runtime = substream->runtime;
  248. int err;
  249. mutex_lock(&oxfw->mutex);
  250. err = snd_oxfw_stream_start_simplex(oxfw, &oxfw->tx_stream,
  251. runtime->rate, runtime->channels);
  252. mutex_unlock(&oxfw->mutex);
  253. if (err < 0)
  254. goto end;
  255. amdtp_stream_pcm_prepare(&oxfw->tx_stream);
  256. end:
  257. return err;
  258. }
  259. static int pcm_playback_prepare(struct snd_pcm_substream *substream)
  260. {
  261. struct snd_oxfw *oxfw = substream->private_data;
  262. struct snd_pcm_runtime *runtime = substream->runtime;
  263. int err;
  264. mutex_lock(&oxfw->mutex);
  265. err = snd_oxfw_stream_start_simplex(oxfw, &oxfw->rx_stream,
  266. runtime->rate, runtime->channels);
  267. mutex_unlock(&oxfw->mutex);
  268. if (err < 0)
  269. goto end;
  270. amdtp_stream_pcm_prepare(&oxfw->rx_stream);
  271. end:
  272. return err;
  273. }
  274. static int pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  275. {
  276. struct snd_oxfw *oxfw = substream->private_data;
  277. struct snd_pcm_substream *pcm;
  278. switch (cmd) {
  279. case SNDRV_PCM_TRIGGER_START:
  280. pcm = substream;
  281. break;
  282. case SNDRV_PCM_TRIGGER_STOP:
  283. pcm = NULL;
  284. break;
  285. default:
  286. return -EINVAL;
  287. }
  288. amdtp_stream_pcm_trigger(&oxfw->tx_stream, pcm);
  289. return 0;
  290. }
  291. static int pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  292. {
  293. struct snd_oxfw *oxfw = substream->private_data;
  294. struct snd_pcm_substream *pcm;
  295. switch (cmd) {
  296. case SNDRV_PCM_TRIGGER_START:
  297. pcm = substream;
  298. break;
  299. case SNDRV_PCM_TRIGGER_STOP:
  300. pcm = NULL;
  301. break;
  302. default:
  303. return -EINVAL;
  304. }
  305. amdtp_stream_pcm_trigger(&oxfw->rx_stream, pcm);
  306. return 0;
  307. }
  308. static snd_pcm_uframes_t pcm_capture_pointer(struct snd_pcm_substream *sbstm)
  309. {
  310. struct snd_oxfw *oxfw = sbstm->private_data;
  311. return amdtp_stream_pcm_pointer(&oxfw->tx_stream);
  312. }
  313. static snd_pcm_uframes_t pcm_playback_pointer(struct snd_pcm_substream *sbstm)
  314. {
  315. struct snd_oxfw *oxfw = sbstm->private_data;
  316. return amdtp_stream_pcm_pointer(&oxfw->rx_stream);
  317. }
  318. int snd_oxfw_create_pcm(struct snd_oxfw *oxfw)
  319. {
  320. static struct snd_pcm_ops capture_ops = {
  321. .open = pcm_open,
  322. .close = pcm_close,
  323. .ioctl = snd_pcm_lib_ioctl,
  324. .hw_params = pcm_capture_hw_params,
  325. .hw_free = pcm_capture_hw_free,
  326. .prepare = pcm_capture_prepare,
  327. .trigger = pcm_capture_trigger,
  328. .pointer = pcm_capture_pointer,
  329. .page = snd_pcm_lib_get_vmalloc_page,
  330. .mmap = snd_pcm_lib_mmap_vmalloc,
  331. };
  332. static struct snd_pcm_ops playback_ops = {
  333. .open = pcm_open,
  334. .close = pcm_close,
  335. .ioctl = snd_pcm_lib_ioctl,
  336. .hw_params = pcm_playback_hw_params,
  337. .hw_free = pcm_playback_hw_free,
  338. .prepare = pcm_playback_prepare,
  339. .trigger = pcm_playback_trigger,
  340. .pointer = pcm_playback_pointer,
  341. .page = snd_pcm_lib_get_vmalloc_page,
  342. .mmap = snd_pcm_lib_mmap_vmalloc,
  343. };
  344. struct snd_pcm *pcm;
  345. unsigned int cap = 0;
  346. int err;
  347. if (oxfw->has_output)
  348. cap = 1;
  349. err = snd_pcm_new(oxfw->card, oxfw->card->driver, 0, 1, cap, &pcm);
  350. if (err < 0)
  351. return err;
  352. pcm->private_data = oxfw;
  353. strcpy(pcm->name, oxfw->card->shortname);
  354. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
  355. if (cap > 0)
  356. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
  357. return 0;
  358. }