ctpcm.c 11 KB

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