pcm_timer.c 3.9 KB

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