ctpcm.c 13 KB

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