ctpcm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 <sound/pcm.h>
  19. /* Hardware descriptions for playback */
  20. static struct snd_pcm_hardware ct_pcm_playback_hw = {
  21. .info = (SNDRV_PCM_INFO_MMAP |
  22. SNDRV_PCM_INFO_INTERLEAVED |
  23. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  24. SNDRV_PCM_INFO_MMAP_VALID |
  25. SNDRV_PCM_INFO_PAUSE),
  26. .formats = (SNDRV_PCM_FMTBIT_U8 |
  27. SNDRV_PCM_FMTBIT_S16_LE |
  28. SNDRV_PCM_FMTBIT_S24_3LE |
  29. SNDRV_PCM_FMTBIT_S32_LE |
  30. SNDRV_PCM_FMTBIT_FLOAT_LE),
  31. .rates = (SNDRV_PCM_RATE_CONTINUOUS |
  32. SNDRV_PCM_RATE_8000_192000),
  33. .rate_min = 8000,
  34. .rate_max = 192000,
  35. .channels_min = 1,
  36. .channels_max = 2,
  37. .buffer_bytes_max = (128*1024),
  38. .period_bytes_min = (64),
  39. .period_bytes_max = (128*1024),
  40. .periods_min = 1,
  41. .periods_max = 1024,
  42. .fifo_size = 0,
  43. };
  44. static struct snd_pcm_hardware ct_spdif_passthru_playback_hw = {
  45. .info = (SNDRV_PCM_INFO_MMAP |
  46. SNDRV_PCM_INFO_INTERLEAVED |
  47. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  48. SNDRV_PCM_INFO_MMAP_VALID |
  49. SNDRV_PCM_INFO_PAUSE),
  50. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  51. .rates = (SNDRV_PCM_RATE_48000 |
  52. SNDRV_PCM_RATE_44100 |
  53. SNDRV_PCM_RATE_32000),
  54. .rate_min = 32000,
  55. .rate_max = 48000,
  56. .channels_min = 2,
  57. .channels_max = 2,
  58. .buffer_bytes_max = (128*1024),
  59. .period_bytes_min = (64),
  60. .period_bytes_max = (128*1024),
  61. .periods_min = 1,
  62. .periods_max = 1024,
  63. .fifo_size = 0,
  64. };
  65. /* Hardware descriptions for capture */
  66. static struct snd_pcm_hardware ct_pcm_capture_hw = {
  67. .info = (SNDRV_PCM_INFO_MMAP |
  68. SNDRV_PCM_INFO_INTERLEAVED |
  69. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  70. SNDRV_PCM_INFO_PAUSE |
  71. SNDRV_PCM_INFO_MMAP_VALID),
  72. .formats = (SNDRV_PCM_FMTBIT_U8 |
  73. SNDRV_PCM_FMTBIT_S16_LE |
  74. SNDRV_PCM_FMTBIT_S24_3LE |
  75. SNDRV_PCM_FMTBIT_S32_LE |
  76. SNDRV_PCM_FMTBIT_FLOAT_LE),
  77. .rates = (SNDRV_PCM_RATE_CONTINUOUS |
  78. SNDRV_PCM_RATE_8000_96000),
  79. .rate_min = 8000,
  80. .rate_max = 96000,
  81. .channels_min = 1,
  82. .channels_max = 2,
  83. .buffer_bytes_max = (128*1024),
  84. .period_bytes_min = (384),
  85. .period_bytes_max = (64*1024),
  86. .periods_min = 2,
  87. .periods_max = 1024,
  88. .fifo_size = 0,
  89. };
  90. static void ct_atc_pcm_interrupt(struct ct_atc_pcm *atc_pcm)
  91. {
  92. struct ct_atc_pcm *apcm = atc_pcm;
  93. if (NULL == apcm->substream)
  94. return;
  95. snd_pcm_period_elapsed(apcm->substream);
  96. }
  97. static void ct_atc_pcm_free_substream(struct snd_pcm_runtime *runtime)
  98. {
  99. struct ct_atc_pcm *apcm = runtime->private_data;
  100. struct ct_atc *atc = snd_pcm_substream_chip(apcm->substream);
  101. atc->pcm_release_resources(atc, apcm);
  102. kfree(apcm);
  103. runtime->private_data = NULL;
  104. }
  105. /* pcm playback operations */
  106. static int ct_pcm_playback_open(struct snd_pcm_substream *substream)
  107. {
  108. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  109. struct snd_pcm_runtime *runtime = substream->runtime;
  110. struct ct_atc_pcm *apcm;
  111. int err;
  112. apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
  113. if (NULL == apcm)
  114. return -ENOMEM;
  115. spin_lock_init(&apcm->timer_lock);
  116. apcm->stop_timer = 0;
  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. return 0;
  143. }
  144. static int ct_pcm_playback_close(struct snd_pcm_substream *substream)
  145. {
  146. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  147. /* TODO: Notify mixer inactive. */
  148. if (IEC958 == substream->pcm->device)
  149. atc->spdif_out_passthru(atc, 0);
  150. /* The ct_atc_pcm object will be freed by runtime->private_free */
  151. return 0;
  152. }
  153. static int ct_pcm_hw_params(struct snd_pcm_substream *substream,
  154. struct snd_pcm_hw_params *hw_params)
  155. {
  156. return snd_pcm_lib_malloc_pages(substream,
  157. params_buffer_bytes(hw_params));
  158. }
  159. static int ct_pcm_hw_free(struct snd_pcm_substream *substream)
  160. {
  161. /* Free snd-allocated pages */
  162. return snd_pcm_lib_free_pages(substream);
  163. }
  164. static void ct_pcm_timer_callback(unsigned long data)
  165. {
  166. struct ct_atc_pcm *apcm = (struct ct_atc_pcm *)data;
  167. struct snd_pcm_substream *substream = apcm->substream;
  168. struct snd_pcm_runtime *runtime = substream->runtime;
  169. unsigned int period_size = runtime->period_size;
  170. unsigned int buffer_size = runtime->buffer_size;
  171. unsigned long flags;
  172. unsigned int position = 0, dist = 0, interval = 0;
  173. position = substream->ops->pointer(substream);
  174. dist = (position + buffer_size - apcm->position) % buffer_size;
  175. if ((dist >= period_size) ||
  176. (position/period_size != apcm->position/period_size)) {
  177. apcm->interrupt(apcm);
  178. apcm->position = position;
  179. }
  180. /* Add extra HZ*5/1000 to avoid overrun issue when recording
  181. * at 8kHz in 8-bit format or at 88kHz in 24-bit format. */
  182. interval = ((period_size - (position % period_size))
  183. * HZ + (runtime->rate - 1)) / runtime->rate + HZ * 5 / 1000;
  184. spin_lock_irqsave(&apcm->timer_lock, flags);
  185. apcm->timer.expires = jiffies + interval;
  186. if (!apcm->stop_timer)
  187. add_timer(&apcm->timer);
  188. spin_unlock_irqrestore(&apcm->timer_lock, flags);
  189. }
  190. static int ct_pcm_timer_prepare(struct ct_atc_pcm *apcm)
  191. {
  192. unsigned long flags;
  193. spin_lock_irqsave(&apcm->timer_lock, flags);
  194. if (timer_pending(&apcm->timer)) {
  195. /* The timer has already been started. */
  196. spin_unlock_irqrestore(&apcm->timer_lock, flags);
  197. return 0;
  198. }
  199. init_timer(&apcm->timer);
  200. apcm->timer.data = (unsigned long)apcm;
  201. apcm->timer.function = ct_pcm_timer_callback;
  202. spin_unlock_irqrestore(&apcm->timer_lock, flags);
  203. apcm->position = 0;
  204. return 0;
  205. }
  206. static int ct_pcm_timer_start(struct ct_atc_pcm *apcm)
  207. {
  208. struct snd_pcm_runtime *runtime = apcm->substream->runtime;
  209. unsigned long flags;
  210. spin_lock_irqsave(&apcm->timer_lock, flags);
  211. if (timer_pending(&apcm->timer)) {
  212. /* The timer has already been started. */
  213. spin_unlock_irqrestore(&apcm->timer_lock, flags);
  214. return 0;
  215. }
  216. apcm->timer.expires = jiffies + (runtime->period_size * HZ +
  217. (runtime->rate - 1)) / runtime->rate;
  218. apcm->stop_timer = 0;
  219. add_timer(&apcm->timer);
  220. spin_unlock_irqrestore(&apcm->timer_lock, flags);
  221. return 0;
  222. }
  223. static int ct_pcm_timer_stop(struct ct_atc_pcm *apcm)
  224. {
  225. unsigned long flags;
  226. spin_lock_irqsave(&apcm->timer_lock, flags);
  227. apcm->stop_timer = 1;
  228. del_timer(&apcm->timer);
  229. spin_unlock_irqrestore(&apcm->timer_lock, flags);
  230. try_to_del_timer_sync(&apcm->timer);
  231. return 0;
  232. }
  233. static int ct_pcm_playback_prepare(struct snd_pcm_substream *substream)
  234. {
  235. int err;
  236. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  237. struct snd_pcm_runtime *runtime = substream->runtime;
  238. struct ct_atc_pcm *apcm = runtime->private_data;
  239. if (IEC958 == substream->pcm->device)
  240. err = atc->spdif_passthru_playback_prepare(atc, apcm);
  241. else
  242. err = atc->pcm_playback_prepare(atc, apcm);
  243. if (err < 0) {
  244. printk(KERN_ERR "ctxfi: Preparing pcm playback failed!!!\n");
  245. return err;
  246. }
  247. ct_pcm_timer_prepare(apcm);
  248. return 0;
  249. }
  250. static int
  251. ct_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  252. {
  253. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  254. struct snd_pcm_runtime *runtime = substream->runtime;
  255. struct ct_atc_pcm *apcm = runtime->private_data;
  256. switch (cmd) {
  257. case SNDRV_PCM_TRIGGER_START:
  258. case SNDRV_PCM_TRIGGER_RESUME:
  259. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  260. atc->pcm_playback_start(atc, apcm);
  261. ct_pcm_timer_start(apcm);
  262. break;
  263. case SNDRV_PCM_TRIGGER_STOP:
  264. case SNDRV_PCM_TRIGGER_SUSPEND:
  265. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  266. ct_pcm_timer_stop(apcm);
  267. atc->pcm_playback_stop(atc, apcm);
  268. break;
  269. default:
  270. break;
  271. }
  272. return 0;
  273. }
  274. static snd_pcm_uframes_t
  275. ct_pcm_playback_pointer(struct snd_pcm_substream *substream)
  276. {
  277. unsigned long position;
  278. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  279. struct snd_pcm_runtime *runtime = substream->runtime;
  280. struct ct_atc_pcm *apcm = runtime->private_data;
  281. /* Read out playback position */
  282. position = atc->pcm_playback_position(atc, apcm);
  283. position = bytes_to_frames(runtime, position);
  284. return position;
  285. }
  286. /* pcm capture operations */
  287. static int ct_pcm_capture_open(struct snd_pcm_substream *substream)
  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;
  292. int err;
  293. apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
  294. if (NULL == apcm)
  295. return -ENOMEM;
  296. spin_lock_init(&apcm->timer_lock);
  297. apcm->started = 0;
  298. apcm->stop_timer = 0;
  299. apcm->substream = substream;
  300. apcm->interrupt = ct_atc_pcm_interrupt;
  301. runtime->private_data = apcm;
  302. runtime->private_free = ct_atc_pcm_free_substream;
  303. runtime->hw = ct_pcm_capture_hw;
  304. runtime->hw.rate_max = atc->rsr * atc->msr;
  305. err = snd_pcm_hw_constraint_integer(runtime,
  306. SNDRV_PCM_HW_PARAM_PERIODS);
  307. if (err < 0) {
  308. kfree(apcm);
  309. return err;
  310. }
  311. err = snd_pcm_hw_constraint_minmax(runtime,
  312. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  313. 1024, UINT_MAX);
  314. if (err < 0) {
  315. kfree(apcm);
  316. return err;
  317. }
  318. return 0;
  319. }
  320. static int ct_pcm_capture_close(struct snd_pcm_substream *substream)
  321. {
  322. /* The ct_atc_pcm object will be freed by runtime->private_free */
  323. /* TODO: Notify mixer inactive. */
  324. return 0;
  325. }
  326. static int ct_pcm_capture_prepare(struct snd_pcm_substream *substream)
  327. {
  328. int err;
  329. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  330. struct snd_pcm_runtime *runtime = substream->runtime;
  331. struct ct_atc_pcm *apcm = runtime->private_data;
  332. err = atc->pcm_capture_prepare(atc, apcm);
  333. if (err < 0) {
  334. printk(KERN_ERR "ctxfi: Preparing pcm capture failed!!!\n");
  335. return err;
  336. }
  337. ct_pcm_timer_prepare(apcm);
  338. return 0;
  339. }
  340. static int
  341. ct_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  342. {
  343. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  344. struct snd_pcm_runtime *runtime = substream->runtime;
  345. struct ct_atc_pcm *apcm = runtime->private_data;
  346. switch (cmd) {
  347. case SNDRV_PCM_TRIGGER_START:
  348. atc->pcm_capture_start(atc, apcm);
  349. ct_pcm_timer_start(apcm);
  350. break;
  351. case SNDRV_PCM_TRIGGER_STOP:
  352. ct_pcm_timer_stop(apcm);
  353. atc->pcm_capture_stop(atc, apcm);
  354. break;
  355. default:
  356. ct_pcm_timer_stop(apcm);
  357. atc->pcm_capture_stop(atc, apcm);
  358. break;
  359. }
  360. return 0;
  361. }
  362. static snd_pcm_uframes_t
  363. ct_pcm_capture_pointer(struct snd_pcm_substream *substream)
  364. {
  365. unsigned long position;
  366. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  367. struct snd_pcm_runtime *runtime = substream->runtime;
  368. struct ct_atc_pcm *apcm = runtime->private_data;
  369. /* Read out playback position */
  370. position = atc->pcm_capture_position(atc, apcm);
  371. position = bytes_to_frames(runtime, position);
  372. return position;
  373. }
  374. /* PCM operators for playback */
  375. static struct snd_pcm_ops ct_pcm_playback_ops = {
  376. .open = ct_pcm_playback_open,
  377. .close = ct_pcm_playback_close,
  378. .ioctl = snd_pcm_lib_ioctl,
  379. .hw_params = ct_pcm_hw_params,
  380. .hw_free = ct_pcm_hw_free,
  381. .prepare = ct_pcm_playback_prepare,
  382. .trigger = ct_pcm_playback_trigger,
  383. .pointer = ct_pcm_playback_pointer,
  384. .page = snd_pcm_sgbuf_ops_page,
  385. };
  386. /* PCM operators for capture */
  387. static struct snd_pcm_ops ct_pcm_capture_ops = {
  388. .open = ct_pcm_capture_open,
  389. .close = ct_pcm_capture_close,
  390. .ioctl = snd_pcm_lib_ioctl,
  391. .hw_params = ct_pcm_hw_params,
  392. .hw_free = ct_pcm_hw_free,
  393. .prepare = ct_pcm_capture_prepare,
  394. .trigger = ct_pcm_capture_trigger,
  395. .pointer = ct_pcm_capture_pointer,
  396. .page = snd_pcm_sgbuf_ops_page,
  397. };
  398. /* Create ALSA pcm device */
  399. int ct_alsa_pcm_create(struct ct_atc *atc,
  400. enum CTALSADEVS device,
  401. const char *device_name)
  402. {
  403. struct snd_pcm *pcm;
  404. int err;
  405. int playback_count, capture_count;
  406. playback_count = (IEC958 == device) ? 1 : 8;
  407. capture_count = (FRONT == device) ? 1 : 0;
  408. err = snd_pcm_new(atc->card, "ctxfi", device,
  409. playback_count, capture_count, &pcm);
  410. if (err < 0) {
  411. printk(KERN_ERR "ctxfi: snd_pcm_new failed!! Err=%d\n", err);
  412. return err;
  413. }
  414. pcm->private_data = atc;
  415. pcm->info_flags = 0;
  416. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  417. strlcpy(pcm->name, device_name, sizeof(pcm->name));
  418. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &ct_pcm_playback_ops);
  419. if (FRONT == device)
  420. snd_pcm_set_ops(pcm,
  421. SNDRV_PCM_STREAM_CAPTURE, &ct_pcm_capture_ops);
  422. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  423. snd_dma_pci_data(atc->pci), 128*1024, 128*1024);
  424. return 0;
  425. }