pcm_timer.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. pcm_err(substream->pcm,
  53. "pcm timer resolution out of range (rate = %u, period_size = %lu)\n",
  54. runtime->rate, runtime->period_size);
  55. runtime->timer_resolution = -1;
  56. return;
  57. }
  58. runtime->timer_resolution = (mult * fsize / rate) * post;
  59. }
  60. static unsigned long snd_pcm_timer_resolution(struct snd_timer * timer)
  61. {
  62. struct snd_pcm_substream *substream;
  63. substream = timer->private_data;
  64. return substream->runtime ? substream->runtime->timer_resolution : 0;
  65. }
  66. static int snd_pcm_timer_start(struct snd_timer * timer)
  67. {
  68. struct snd_pcm_substream *substream;
  69. substream = snd_timer_chip(timer);
  70. substream->timer_running = 1;
  71. return 0;
  72. }
  73. static int snd_pcm_timer_stop(struct snd_timer * timer)
  74. {
  75. struct snd_pcm_substream *substream;
  76. substream = snd_timer_chip(timer);
  77. substream->timer_running = 0;
  78. return 0;
  79. }
  80. static struct snd_timer_hardware snd_pcm_timer =
  81. {
  82. .flags = SNDRV_TIMER_HW_AUTO | SNDRV_TIMER_HW_SLAVE,
  83. .resolution = 0,
  84. .ticks = 1,
  85. .c_resolution = snd_pcm_timer_resolution,
  86. .start = snd_pcm_timer_start,
  87. .stop = snd_pcm_timer_stop,
  88. };
  89. /*
  90. * Init functions
  91. */
  92. static void snd_pcm_timer_free(struct snd_timer *timer)
  93. {
  94. struct snd_pcm_substream *substream = timer->private_data;
  95. substream->timer = NULL;
  96. }
  97. void snd_pcm_timer_init(struct snd_pcm_substream *substream)
  98. {
  99. struct snd_timer_id tid;
  100. struct snd_timer *timer;
  101. tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  102. tid.dev_class = SNDRV_TIMER_CLASS_PCM;
  103. tid.card = substream->pcm->card->number;
  104. tid.device = substream->pcm->device;
  105. tid.subdevice = (substream->number << 1) | (substream->stream & 1);
  106. if (snd_timer_new(substream->pcm->card, "PCM", &tid, &timer) < 0)
  107. return;
  108. sprintf(timer->name, "PCM %s %i-%i-%i",
  109. substream->stream == SNDRV_PCM_STREAM_CAPTURE ?
  110. "capture" : "playback",
  111. tid.card, tid.device, tid.subdevice);
  112. timer->hw = snd_pcm_timer;
  113. if (snd_device_register(timer->card, timer) < 0) {
  114. snd_device_free(timer->card, timer);
  115. return;
  116. }
  117. timer->private_data = substream;
  118. timer->private_free = snd_pcm_timer_free;
  119. substream->timer = timer;
  120. }
  121. void snd_pcm_timer_done(struct snd_pcm_substream *substream)
  122. {
  123. if (substream->timer) {
  124. snd_device_free(substream->pcm->card, substream->timer);
  125. substream->timer = NULL;
  126. }
  127. }