ctpcm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 (!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 (!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. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  160. struct ct_atc_pcm *apcm = substream->runtime->private_data;
  161. int err;
  162. err = snd_pcm_lib_malloc_pages(substream,
  163. params_buffer_bytes(hw_params));
  164. if (err < 0)
  165. return err;
  166. /* clear previous resources */
  167. atc->pcm_release_resources(atc, apcm);
  168. return err;
  169. }
  170. static int ct_pcm_hw_free(struct snd_pcm_substream *substream)
  171. {
  172. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  173. struct ct_atc_pcm *apcm = substream->runtime->private_data;
  174. /* clear previous resources */
  175. atc->pcm_release_resources(atc, apcm);
  176. /* Free snd-allocated pages */
  177. return snd_pcm_lib_free_pages(substream);
  178. }
  179. static int ct_pcm_playback_prepare(struct snd_pcm_substream *substream)
  180. {
  181. int err;
  182. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  183. struct snd_pcm_runtime *runtime = substream->runtime;
  184. struct ct_atc_pcm *apcm = runtime->private_data;
  185. if (IEC958 == substream->pcm->device)
  186. err = atc->spdif_passthru_playback_prepare(atc, apcm);
  187. else
  188. err = atc->pcm_playback_prepare(atc, apcm);
  189. if (err < 0) {
  190. printk(KERN_ERR "ctxfi: Preparing pcm playback failed!!!\n");
  191. return err;
  192. }
  193. return 0;
  194. }
  195. static int
  196. ct_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  197. {
  198. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  199. struct snd_pcm_runtime *runtime = substream->runtime;
  200. struct ct_atc_pcm *apcm = runtime->private_data;
  201. switch (cmd) {
  202. case SNDRV_PCM_TRIGGER_START:
  203. case SNDRV_PCM_TRIGGER_RESUME:
  204. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  205. atc->pcm_playback_start(atc, apcm);
  206. break;
  207. case SNDRV_PCM_TRIGGER_STOP:
  208. case SNDRV_PCM_TRIGGER_SUSPEND:
  209. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  210. atc->pcm_playback_stop(atc, apcm);
  211. break;
  212. default:
  213. break;
  214. }
  215. return 0;
  216. }
  217. static snd_pcm_uframes_t
  218. ct_pcm_playback_pointer(struct snd_pcm_substream *substream)
  219. {
  220. unsigned long position;
  221. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  222. struct snd_pcm_runtime *runtime = substream->runtime;
  223. struct ct_atc_pcm *apcm = runtime->private_data;
  224. /* Read out playback position */
  225. position = atc->pcm_playback_position(atc, apcm);
  226. position = bytes_to_frames(runtime, position);
  227. if (position >= runtime->buffer_size)
  228. position = 0;
  229. return position;
  230. }
  231. /* pcm capture operations */
  232. static int ct_pcm_capture_open(struct snd_pcm_substream *substream)
  233. {
  234. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  235. struct snd_pcm_runtime *runtime = substream->runtime;
  236. struct ct_atc_pcm *apcm;
  237. int err;
  238. apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
  239. if (!apcm)
  240. return -ENOMEM;
  241. apcm->started = 0;
  242. apcm->substream = substream;
  243. apcm->interrupt = ct_atc_pcm_interrupt;
  244. runtime->private_data = apcm;
  245. runtime->private_free = ct_atc_pcm_free_substream;
  246. runtime->hw = ct_pcm_capture_hw;
  247. runtime->hw.rate_max = atc->rsr * atc->msr;
  248. err = snd_pcm_hw_constraint_integer(runtime,
  249. SNDRV_PCM_HW_PARAM_PERIODS);
  250. if (err < 0) {
  251. kfree(apcm);
  252. return err;
  253. }
  254. err = snd_pcm_hw_constraint_minmax(runtime,
  255. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  256. 1024, UINT_MAX);
  257. if (err < 0) {
  258. kfree(apcm);
  259. return err;
  260. }
  261. apcm->timer = ct_timer_instance_new(atc->timer, apcm);
  262. if (!apcm->timer)
  263. return -ENOMEM;
  264. return 0;
  265. }
  266. static int ct_pcm_capture_close(struct snd_pcm_substream *substream)
  267. {
  268. /* The ct_atc_pcm object will be freed by runtime->private_free */
  269. /* TODO: Notify mixer inactive. */
  270. return 0;
  271. }
  272. static int ct_pcm_capture_prepare(struct snd_pcm_substream *substream)
  273. {
  274. int err;
  275. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  276. struct snd_pcm_runtime *runtime = substream->runtime;
  277. struct ct_atc_pcm *apcm = runtime->private_data;
  278. err = atc->pcm_capture_prepare(atc, apcm);
  279. if (err < 0) {
  280. printk(KERN_ERR "ctxfi: Preparing pcm capture failed!!!\n");
  281. return err;
  282. }
  283. return 0;
  284. }
  285. static int
  286. ct_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  287. {
  288. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  289. struct snd_pcm_runtime *runtime = substream->runtime;
  290. struct ct_atc_pcm *apcm = runtime->private_data;
  291. switch (cmd) {
  292. case SNDRV_PCM_TRIGGER_START:
  293. atc->pcm_capture_start(atc, apcm);
  294. break;
  295. case SNDRV_PCM_TRIGGER_STOP:
  296. atc->pcm_capture_stop(atc, apcm);
  297. break;
  298. default:
  299. atc->pcm_capture_stop(atc, apcm);
  300. break;
  301. }
  302. return 0;
  303. }
  304. static snd_pcm_uframes_t
  305. ct_pcm_capture_pointer(struct snd_pcm_substream *substream)
  306. {
  307. unsigned long position;
  308. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  309. struct snd_pcm_runtime *runtime = substream->runtime;
  310. struct ct_atc_pcm *apcm = runtime->private_data;
  311. /* Read out playback position */
  312. position = atc->pcm_capture_position(atc, apcm);
  313. position = bytes_to_frames(runtime, position);
  314. if (position >= runtime->buffer_size)
  315. position = 0;
  316. return position;
  317. }
  318. /* PCM operators for playback */
  319. static struct snd_pcm_ops ct_pcm_playback_ops = {
  320. .open = ct_pcm_playback_open,
  321. .close = ct_pcm_playback_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_playback_prepare,
  326. .trigger = ct_pcm_playback_trigger,
  327. .pointer = ct_pcm_playback_pointer,
  328. .page = snd_pcm_sgbuf_ops_page,
  329. };
  330. /* PCM operators for capture */
  331. static struct snd_pcm_ops ct_pcm_capture_ops = {
  332. .open = ct_pcm_capture_open,
  333. .close = ct_pcm_capture_close,
  334. .ioctl = snd_pcm_lib_ioctl,
  335. .hw_params = ct_pcm_hw_params,
  336. .hw_free = ct_pcm_hw_free,
  337. .prepare = ct_pcm_capture_prepare,
  338. .trigger = ct_pcm_capture_trigger,
  339. .pointer = ct_pcm_capture_pointer,
  340. .page = snd_pcm_sgbuf_ops_page,
  341. };
  342. /* Create ALSA pcm device */
  343. int ct_alsa_pcm_create(struct ct_atc *atc,
  344. enum CTALSADEVS device,
  345. const char *device_name)
  346. {
  347. struct snd_pcm *pcm;
  348. int err;
  349. int playback_count, capture_count;
  350. playback_count = (IEC958 == device) ? 1 : 8;
  351. capture_count = (FRONT == device) ? 1 : 0;
  352. err = snd_pcm_new(atc->card, "ctxfi", device,
  353. playback_count, capture_count, &pcm);
  354. if (err < 0) {
  355. printk(KERN_ERR "ctxfi: snd_pcm_new failed!! Err=%d\n", err);
  356. return err;
  357. }
  358. pcm->private_data = atc;
  359. pcm->info_flags = 0;
  360. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  361. strlcpy(pcm->name, device_name, sizeof(pcm->name));
  362. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &ct_pcm_playback_ops);
  363. if (FRONT == device)
  364. snd_pcm_set_ops(pcm,
  365. SNDRV_PCM_STREAM_CAPTURE, &ct_pcm_capture_ops);
  366. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  367. snd_dma_pci_data(atc->pci), 128*1024, 128*1024);
  368. #ifdef CONFIG_PM
  369. atc->pcms[device] = pcm;
  370. #endif
  371. return 0;
  372. }