ctpcm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /**
  2. * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
  3. *
  4. * This source file is released under GPL v2 license (no other versions).
  5. * See the COPYING file included in the main directory of this source
  6. * distribution for the license terms and conditions.
  7. *
  8. * @File ctpcm.c
  9. *
  10. * @Brief
  11. * This file contains the definition of the pcm device functions.
  12. *
  13. * @Author Liu Chun
  14. * @Date Apr 2 2008
  15. *
  16. */
  17. #include "ctpcm.h"
  18. #include "cttimer.h"
  19. #include <sound/pcm.h>
  20. /* Hardware descriptions for playback */
  21. static struct snd_pcm_hardware ct_pcm_playback_hw = {
  22. .info = (SNDRV_PCM_INFO_MMAP |
  23. SNDRV_PCM_INFO_INTERLEAVED |
  24. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  25. SNDRV_PCM_INFO_MMAP_VALID |
  26. SNDRV_PCM_INFO_PAUSE),
  27. .formats = (SNDRV_PCM_FMTBIT_U8 |
  28. SNDRV_PCM_FMTBIT_S16_LE |
  29. SNDRV_PCM_FMTBIT_S24_3LE |
  30. SNDRV_PCM_FMTBIT_S32_LE |
  31. SNDRV_PCM_FMTBIT_FLOAT_LE),
  32. .rates = (SNDRV_PCM_RATE_CONTINUOUS |
  33. SNDRV_PCM_RATE_8000_192000),
  34. .rate_min = 8000,
  35. .rate_max = 192000,
  36. .channels_min = 1,
  37. .channels_max = 2,
  38. .buffer_bytes_max = (128*1024),
  39. .period_bytes_min = (64),
  40. .period_bytes_max = (128*1024),
  41. .periods_min = 1,
  42. .periods_max = 1024,
  43. .fifo_size = 0,
  44. };
  45. static struct snd_pcm_hardware ct_spdif_passthru_playback_hw = {
  46. .info = (SNDRV_PCM_INFO_MMAP |
  47. SNDRV_PCM_INFO_INTERLEAVED |
  48. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  49. SNDRV_PCM_INFO_MMAP_VALID |
  50. SNDRV_PCM_INFO_PAUSE),
  51. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  52. .rates = (SNDRV_PCM_RATE_48000 |
  53. SNDRV_PCM_RATE_44100 |
  54. SNDRV_PCM_RATE_32000),
  55. .rate_min = 32000,
  56. .rate_max = 48000,
  57. .channels_min = 2,
  58. .channels_max = 2,
  59. .buffer_bytes_max = (128*1024),
  60. .period_bytes_min = (64),
  61. .period_bytes_max = (128*1024),
  62. .periods_min = 1,
  63. .periods_max = 1024,
  64. .fifo_size = 0,
  65. };
  66. /* Hardware descriptions for capture */
  67. static struct snd_pcm_hardware ct_pcm_capture_hw = {
  68. .info = (SNDRV_PCM_INFO_MMAP |
  69. SNDRV_PCM_INFO_INTERLEAVED |
  70. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  71. SNDRV_PCM_INFO_PAUSE |
  72. SNDRV_PCM_INFO_MMAP_VALID),
  73. .formats = (SNDRV_PCM_FMTBIT_U8 |
  74. SNDRV_PCM_FMTBIT_S16_LE |
  75. SNDRV_PCM_FMTBIT_S24_3LE |
  76. SNDRV_PCM_FMTBIT_S32_LE |
  77. SNDRV_PCM_FMTBIT_FLOAT_LE),
  78. .rates = (SNDRV_PCM_RATE_CONTINUOUS |
  79. SNDRV_PCM_RATE_8000_96000),
  80. .rate_min = 8000,
  81. .rate_max = 96000,
  82. .channels_min = 1,
  83. .channels_max = 2,
  84. .buffer_bytes_max = (128*1024),
  85. .period_bytes_min = (384),
  86. .period_bytes_max = (64*1024),
  87. .periods_min = 2,
  88. .periods_max = 1024,
  89. .fifo_size = 0,
  90. };
  91. static void ct_atc_pcm_interrupt(struct ct_atc_pcm *atc_pcm)
  92. {
  93. struct ct_atc_pcm *apcm = atc_pcm;
  94. if (NULL == apcm->substream)
  95. return;
  96. snd_pcm_period_elapsed(apcm->substream);
  97. }
  98. static void ct_atc_pcm_free_substream(struct snd_pcm_runtime *runtime)
  99. {
  100. struct ct_atc_pcm *apcm = runtime->private_data;
  101. struct ct_atc *atc = snd_pcm_substream_chip(apcm->substream);
  102. atc->pcm_release_resources(atc, apcm);
  103. ct_timer_instance_free(apcm->timer);
  104. kfree(apcm);
  105. runtime->private_data = NULL;
  106. }
  107. /* pcm playback operations */
  108. static int ct_pcm_playback_open(struct snd_pcm_substream *substream)
  109. {
  110. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  111. struct snd_pcm_runtime *runtime = substream->runtime;
  112. struct ct_atc_pcm *apcm;
  113. int err;
  114. apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
  115. if (NULL == apcm)
  116. return -ENOMEM;
  117. apcm->substream = substream;
  118. apcm->interrupt = ct_atc_pcm_interrupt;
  119. runtime->private_data = apcm;
  120. runtime->private_free = ct_atc_pcm_free_substream;
  121. if (IEC958 == substream->pcm->device) {
  122. runtime->hw = ct_spdif_passthru_playback_hw;
  123. atc->spdif_out_passthru(atc, 1);
  124. } else {
  125. runtime->hw = ct_pcm_playback_hw;
  126. if (FRONT == substream->pcm->device)
  127. runtime->hw.channels_max = 8;
  128. }
  129. err = snd_pcm_hw_constraint_integer(runtime,
  130. SNDRV_PCM_HW_PARAM_PERIODS);
  131. if (err < 0) {
  132. kfree(apcm);
  133. return err;
  134. }
  135. err = snd_pcm_hw_constraint_minmax(runtime,
  136. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  137. 1024, UINT_MAX);
  138. if (err < 0) {
  139. kfree(apcm);
  140. return err;
  141. }
  142. apcm->timer = ct_timer_instance_new(atc->timer, apcm);
  143. if (!apcm->timer)
  144. return -ENOMEM;
  145. return 0;
  146. }
  147. static int ct_pcm_playback_close(struct snd_pcm_substream *substream)
  148. {
  149. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  150. /* TODO: Notify mixer inactive. */
  151. if (IEC958 == substream->pcm->device)
  152. atc->spdif_out_passthru(atc, 0);
  153. /* The ct_atc_pcm object will be freed by runtime->private_free */
  154. return 0;
  155. }
  156. static int ct_pcm_hw_params(struct snd_pcm_substream *substream,
  157. struct snd_pcm_hw_params *hw_params)
  158. {
  159. return snd_pcm_lib_malloc_pages(substream,
  160. params_buffer_bytes(hw_params));
  161. }
  162. static int ct_pcm_hw_free(struct snd_pcm_substream *substream)
  163. {
  164. /* Free snd-allocated pages */
  165. return snd_pcm_lib_free_pages(substream);
  166. }
  167. static int ct_pcm_playback_prepare(struct snd_pcm_substream *substream)
  168. {
  169. int err;
  170. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  171. struct snd_pcm_runtime *runtime = substream->runtime;
  172. struct ct_atc_pcm *apcm = runtime->private_data;
  173. if (IEC958 == substream->pcm->device)
  174. err = atc->spdif_passthru_playback_prepare(atc, apcm);
  175. else
  176. err = atc->pcm_playback_prepare(atc, apcm);
  177. if (err < 0) {
  178. printk(KERN_ERR "ctxfi: Preparing pcm playback failed!!!\n");
  179. return err;
  180. }
  181. return 0;
  182. }
  183. static int
  184. ct_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  185. {
  186. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  187. struct snd_pcm_runtime *runtime = substream->runtime;
  188. struct ct_atc_pcm *apcm = runtime->private_data;
  189. switch (cmd) {
  190. case SNDRV_PCM_TRIGGER_START:
  191. case SNDRV_PCM_TRIGGER_RESUME:
  192. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  193. atc->pcm_playback_start(atc, apcm);
  194. break;
  195. case SNDRV_PCM_TRIGGER_STOP:
  196. case SNDRV_PCM_TRIGGER_SUSPEND:
  197. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  198. atc->pcm_playback_stop(atc, apcm);
  199. break;
  200. default:
  201. break;
  202. }
  203. return 0;
  204. }
  205. static snd_pcm_uframes_t
  206. ct_pcm_playback_pointer(struct snd_pcm_substream *substream)
  207. {
  208. unsigned long position;
  209. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  210. struct snd_pcm_runtime *runtime = substream->runtime;
  211. struct ct_atc_pcm *apcm = runtime->private_data;
  212. /* Read out playback position */
  213. position = atc->pcm_playback_position(atc, apcm);
  214. position = bytes_to_frames(runtime, position);
  215. return position;
  216. }
  217. /* pcm capture operations */
  218. static int ct_pcm_capture_open(struct snd_pcm_substream *substream)
  219. {
  220. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  221. struct snd_pcm_runtime *runtime = substream->runtime;
  222. struct ct_atc_pcm *apcm;
  223. int err;
  224. apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
  225. if (NULL == apcm)
  226. return -ENOMEM;
  227. apcm->started = 0;
  228. apcm->substream = substream;
  229. apcm->interrupt = ct_atc_pcm_interrupt;
  230. runtime->private_data = apcm;
  231. runtime->private_free = ct_atc_pcm_free_substream;
  232. runtime->hw = ct_pcm_capture_hw;
  233. runtime->hw.rate_max = atc->rsr * atc->msr;
  234. err = snd_pcm_hw_constraint_integer(runtime,
  235. SNDRV_PCM_HW_PARAM_PERIODS);
  236. if (err < 0) {
  237. kfree(apcm);
  238. return err;
  239. }
  240. err = snd_pcm_hw_constraint_minmax(runtime,
  241. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  242. 1024, UINT_MAX);
  243. if (err < 0) {
  244. kfree(apcm);
  245. return err;
  246. }
  247. apcm->timer = ct_timer_instance_new(atc->timer, apcm);
  248. if (!apcm->timer)
  249. return -ENOMEM;
  250. return 0;
  251. }
  252. static int ct_pcm_capture_close(struct snd_pcm_substream *substream)
  253. {
  254. /* The ct_atc_pcm object will be freed by runtime->private_free */
  255. /* TODO: Notify mixer inactive. */
  256. return 0;
  257. }
  258. static int ct_pcm_capture_prepare(struct snd_pcm_substream *substream)
  259. {
  260. int err;
  261. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  262. struct snd_pcm_runtime *runtime = substream->runtime;
  263. struct ct_atc_pcm *apcm = runtime->private_data;
  264. err = atc->pcm_capture_prepare(atc, apcm);
  265. if (err < 0) {
  266. printk(KERN_ERR "ctxfi: Preparing pcm capture failed!!!\n");
  267. return err;
  268. }
  269. return 0;
  270. }
  271. static int
  272. ct_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  273. {
  274. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  275. struct snd_pcm_runtime *runtime = substream->runtime;
  276. struct ct_atc_pcm *apcm = runtime->private_data;
  277. switch (cmd) {
  278. case SNDRV_PCM_TRIGGER_START:
  279. atc->pcm_capture_start(atc, apcm);
  280. break;
  281. case SNDRV_PCM_TRIGGER_STOP:
  282. atc->pcm_capture_stop(atc, apcm);
  283. break;
  284. default:
  285. atc->pcm_capture_stop(atc, apcm);
  286. break;
  287. }
  288. return 0;
  289. }
  290. static snd_pcm_uframes_t
  291. ct_pcm_capture_pointer(struct snd_pcm_substream *substream)
  292. {
  293. unsigned long position;
  294. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  295. struct snd_pcm_runtime *runtime = substream->runtime;
  296. struct ct_atc_pcm *apcm = runtime->private_data;
  297. /* Read out playback position */
  298. position = atc->pcm_capture_position(atc, apcm);
  299. position = bytes_to_frames(runtime, position);
  300. return position;
  301. }
  302. /* PCM operators for playback */
  303. static struct snd_pcm_ops ct_pcm_playback_ops = {
  304. .open = ct_pcm_playback_open,
  305. .close = ct_pcm_playback_close,
  306. .ioctl = snd_pcm_lib_ioctl,
  307. .hw_params = ct_pcm_hw_params,
  308. .hw_free = ct_pcm_hw_free,
  309. .prepare = ct_pcm_playback_prepare,
  310. .trigger = ct_pcm_playback_trigger,
  311. .pointer = ct_pcm_playback_pointer,
  312. .page = snd_pcm_sgbuf_ops_page,
  313. };
  314. /* PCM operators for capture */
  315. static struct snd_pcm_ops ct_pcm_capture_ops = {
  316. .open = ct_pcm_capture_open,
  317. .close = ct_pcm_capture_close,
  318. .ioctl = snd_pcm_lib_ioctl,
  319. .hw_params = ct_pcm_hw_params,
  320. .hw_free = ct_pcm_hw_free,
  321. .prepare = ct_pcm_capture_prepare,
  322. .trigger = ct_pcm_capture_trigger,
  323. .pointer = ct_pcm_capture_pointer,
  324. .page = snd_pcm_sgbuf_ops_page,
  325. };
  326. /* Create ALSA pcm device */
  327. int ct_alsa_pcm_create(struct ct_atc *atc,
  328. enum CTALSADEVS device,
  329. const char *device_name)
  330. {
  331. struct snd_pcm *pcm;
  332. int err;
  333. int playback_count, capture_count;
  334. playback_count = (IEC958 == device) ? 1 : 8;
  335. capture_count = (FRONT == device) ? 1 : 0;
  336. err = snd_pcm_new(atc->card, "ctxfi", device,
  337. playback_count, capture_count, &pcm);
  338. if (err < 0) {
  339. printk(KERN_ERR "ctxfi: snd_pcm_new failed!! Err=%d\n", err);
  340. return err;
  341. }
  342. pcm->private_data = atc;
  343. pcm->info_flags = 0;
  344. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  345. strlcpy(pcm->name, device_name, sizeof(pcm->name));
  346. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &ct_pcm_playback_ops);
  347. if (FRONT == device)
  348. snd_pcm_set_ops(pcm,
  349. SNDRV_PCM_STREAM_CAPTURE, &ct_pcm_capture_ops);
  350. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  351. snd_dma_pci_data(atc->pci), 128*1024, 128*1024);
  352. return 0;
  353. }