ctpcm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 = 2,
  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 = 2,
  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. if (position >= runtime->buffer_size)
  216. position = 0;
  217. return position;
  218. }
  219. /* pcm capture operations */
  220. static int ct_pcm_capture_open(struct snd_pcm_substream *substream)
  221. {
  222. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  223. struct snd_pcm_runtime *runtime = substream->runtime;
  224. struct ct_atc_pcm *apcm;
  225. int err;
  226. apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
  227. if (NULL == apcm)
  228. return -ENOMEM;
  229. apcm->started = 0;
  230. apcm->substream = substream;
  231. apcm->interrupt = ct_atc_pcm_interrupt;
  232. runtime->private_data = apcm;
  233. runtime->private_free = ct_atc_pcm_free_substream;
  234. runtime->hw = ct_pcm_capture_hw;
  235. runtime->hw.rate_max = atc->rsr * atc->msr;
  236. err = snd_pcm_hw_constraint_integer(runtime,
  237. SNDRV_PCM_HW_PARAM_PERIODS);
  238. if (err < 0) {
  239. kfree(apcm);
  240. return err;
  241. }
  242. err = snd_pcm_hw_constraint_minmax(runtime,
  243. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  244. 1024, UINT_MAX);
  245. if (err < 0) {
  246. kfree(apcm);
  247. return err;
  248. }
  249. apcm->timer = ct_timer_instance_new(atc->timer, apcm);
  250. if (!apcm->timer)
  251. return -ENOMEM;
  252. return 0;
  253. }
  254. static int ct_pcm_capture_close(struct snd_pcm_substream *substream)
  255. {
  256. /* The ct_atc_pcm object will be freed by runtime->private_free */
  257. /* TODO: Notify mixer inactive. */
  258. return 0;
  259. }
  260. static int ct_pcm_capture_prepare(struct snd_pcm_substream *substream)
  261. {
  262. int err;
  263. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  264. struct snd_pcm_runtime *runtime = substream->runtime;
  265. struct ct_atc_pcm *apcm = runtime->private_data;
  266. err = atc->pcm_capture_prepare(atc, apcm);
  267. if (err < 0) {
  268. printk(KERN_ERR "ctxfi: Preparing pcm capture failed!!!\n");
  269. return err;
  270. }
  271. return 0;
  272. }
  273. static int
  274. ct_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  275. {
  276. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  277. struct snd_pcm_runtime *runtime = substream->runtime;
  278. struct ct_atc_pcm *apcm = runtime->private_data;
  279. switch (cmd) {
  280. case SNDRV_PCM_TRIGGER_START:
  281. atc->pcm_capture_start(atc, apcm);
  282. break;
  283. case SNDRV_PCM_TRIGGER_STOP:
  284. atc->pcm_capture_stop(atc, apcm);
  285. break;
  286. default:
  287. atc->pcm_capture_stop(atc, apcm);
  288. break;
  289. }
  290. return 0;
  291. }
  292. static snd_pcm_uframes_t
  293. ct_pcm_capture_pointer(struct snd_pcm_substream *substream)
  294. {
  295. unsigned long position;
  296. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  297. struct snd_pcm_runtime *runtime = substream->runtime;
  298. struct ct_atc_pcm *apcm = runtime->private_data;
  299. /* Read out playback position */
  300. position = atc->pcm_capture_position(atc, apcm);
  301. position = bytes_to_frames(runtime, position);
  302. if (position >= runtime->buffer_size)
  303. position = 0;
  304. return position;
  305. }
  306. /* PCM operators for playback */
  307. static struct snd_pcm_ops ct_pcm_playback_ops = {
  308. .open = ct_pcm_playback_open,
  309. .close = ct_pcm_playback_close,
  310. .ioctl = snd_pcm_lib_ioctl,
  311. .hw_params = ct_pcm_hw_params,
  312. .hw_free = ct_pcm_hw_free,
  313. .prepare = ct_pcm_playback_prepare,
  314. .trigger = ct_pcm_playback_trigger,
  315. .pointer = ct_pcm_playback_pointer,
  316. .page = snd_pcm_sgbuf_ops_page,
  317. };
  318. /* PCM operators for capture */
  319. static struct snd_pcm_ops ct_pcm_capture_ops = {
  320. .open = ct_pcm_capture_open,
  321. .close = ct_pcm_capture_close,
  322. .ioctl = snd_pcm_lib_ioctl,
  323. .hw_params = ct_pcm_hw_params,
  324. .hw_free = ct_pcm_hw_free,
  325. .prepare = ct_pcm_capture_prepare,
  326. .trigger = ct_pcm_capture_trigger,
  327. .pointer = ct_pcm_capture_pointer,
  328. .page = snd_pcm_sgbuf_ops_page,
  329. };
  330. /* Create ALSA pcm device */
  331. int ct_alsa_pcm_create(struct ct_atc *atc,
  332. enum CTALSADEVS device,
  333. const char *device_name)
  334. {
  335. struct snd_pcm *pcm;
  336. int err;
  337. int playback_count, capture_count;
  338. playback_count = (IEC958 == device) ? 1 : 8;
  339. capture_count = (FRONT == device) ? 1 : 0;
  340. err = snd_pcm_new(atc->card, "ctxfi", device,
  341. playback_count, capture_count, &pcm);
  342. if (err < 0) {
  343. printk(KERN_ERR "ctxfi: snd_pcm_new failed!! Err=%d\n", err);
  344. return err;
  345. }
  346. pcm->private_data = atc;
  347. pcm->info_flags = 0;
  348. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  349. strlcpy(pcm->name, device_name, sizeof(pcm->name));
  350. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &ct_pcm_playback_ops);
  351. if (FRONT == device)
  352. snd_pcm_set_ops(pcm,
  353. SNDRV_PCM_STREAM_CAPTURE, &ct_pcm_capture_ops);
  354. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  355. snd_dma_pci_data(atc->pci), 128*1024, 128*1024);
  356. return 0;
  357. }