pcm_timer.c 3.7 KB

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