pcm_timer.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Digital Audio (PCM) abstract layer
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/time.h>
  22. #include <linux/gcd.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/timer.h>
  26. /*
  27. * Timer functions
  28. */
  29. void snd_pcm_timer_resolution_change(struct snd_pcm_substream *substream)
  30. {
  31. unsigned long rate, mult, fsize, l, post;
  32. struct snd_pcm_runtime *runtime = substream->runtime;
  33. mult = 1000000000;
  34. rate = runtime->rate;
  35. if (snd_BUG_ON(!rate))
  36. return;
  37. l = gcd(mult, rate);
  38. mult /= l;
  39. rate /= l;
  40. fsize = runtime->period_size;
  41. if (snd_BUG_ON(!fsize))
  42. return;
  43. l = gcd(rate, fsize);
  44. rate /= l;
  45. fsize /= l;
  46. post = 1;
  47. while ((mult * fsize) / fsize != mult) {
  48. mult /= 2;
  49. post *= 2;
  50. }
  51. if (rate == 0) {
  52. snd_printk(KERN_ERR "pcm timer resolution out of range (rate = %u, period_size = %lu)\n", runtime->rate, runtime->period_size);
  53. runtime->timer_resolution = -1;
  54. return;
  55. }
  56. runtime->timer_resolution = (mult * fsize / rate) * post;
  57. }
  58. static unsigned long snd_pcm_timer_resolution(struct snd_timer * timer)
  59. {
  60. struct snd_pcm_substream *substream;
  61. substream = timer->private_data;
  62. return substream->runtime ? substream->runtime->timer_resolution : 0;
  63. }
  64. static int snd_pcm_timer_start(struct snd_timer * timer)
  65. {
  66. struct snd_pcm_substream *substream;
  67. substream = snd_timer_chip(timer);
  68. substream->timer_running = 1;
  69. return 0;
  70. }
  71. static int snd_pcm_timer_stop(struct snd_timer * timer)
  72. {
  73. struct snd_pcm_substream *substream;
  74. substream = snd_timer_chip(timer);
  75. substream->timer_running = 0;
  76. return 0;
  77. }
  78. static struct snd_timer_hardware snd_pcm_timer =
  79. {
  80. .flags = SNDRV_TIMER_HW_AUTO | SNDRV_TIMER_HW_SLAVE,
  81. .resolution = 0,
  82. .ticks = 1,
  83. .c_resolution = snd_pcm_timer_resolution,
  84. .start = snd_pcm_timer_start,
  85. .stop = snd_pcm_timer_stop,
  86. };
  87. /*
  88. * Init functions
  89. */
  90. static void snd_pcm_timer_free(struct snd_timer *timer)
  91. {
  92. struct snd_pcm_substream *substream = timer->private_data;
  93. substream->timer = NULL;
  94. }
  95. void snd_pcm_timer_init(struct snd_pcm_substream *substream)
  96. {
  97. struct snd_timer_id tid;
  98. struct snd_timer *timer;
  99. tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  100. tid.dev_class = SNDRV_TIMER_CLASS_PCM;
  101. tid.card = substream->pcm->card->number;
  102. tid.device = substream->pcm->device;
  103. tid.subdevice = (substream->number << 1) | (substream->stream & 1);
  104. if (snd_timer_new(substream->pcm->card, "PCM", &tid, &timer) < 0)
  105. return;
  106. sprintf(timer->name, "PCM %s %i-%i-%i",
  107. substream->stream == SNDRV_PCM_STREAM_CAPTURE ?
  108. "capture" : "playback",
  109. tid.card, tid.device, tid.subdevice);
  110. timer->hw = snd_pcm_timer;
  111. if (snd_device_register(timer->card, timer) < 0) {
  112. snd_device_free(timer->card, timer);
  113. return;
  114. }
  115. timer->private_data = substream;
  116. timer->private_free = snd_pcm_timer_free;
  117. substream->timer = timer;
  118. }
  119. void snd_pcm_timer_done(struct snd_pcm_substream *substream)
  120. {
  121. if (substream->timer) {
  122. snd_device_free(substream->pcm->card, substream->timer);
  123. substream->timer = NULL;
  124. }
  125. }