pcm_timer.c 4.1 KB

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