pcm_timer.c 4.1 KB

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