pcm_timer.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Digital Audio (PCM) abstract layer
  4. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  5. */
  6. #include <linux/time.h>
  7. #include <linux/gcd.h>
  8. #include <sound/core.h>
  9. #include <sound/pcm.h>
  10. #include <sound/timer.h>
  11. #include "pcm_local.h"
  12. /*
  13. * Timer functions
  14. */
  15. void snd_pcm_timer_resolution_change(struct snd_pcm_substream *substream)
  16. {
  17. unsigned long rate, mult, fsize, l, post;
  18. struct snd_pcm_runtime *runtime = substream->runtime;
  19. mult = 1000000000;
  20. rate = runtime->rate;
  21. if (snd_BUG_ON(!rate))
  22. return;
  23. l = gcd(mult, rate);
  24. mult /= l;
  25. rate /= l;
  26. fsize = runtime->period_size;
  27. if (snd_BUG_ON(!fsize))
  28. return;
  29. l = gcd(rate, fsize);
  30. rate /= l;
  31. fsize /= l;
  32. post = 1;
  33. while ((mult * fsize) / fsize != mult) {
  34. mult /= 2;
  35. post *= 2;
  36. }
  37. if (rate == 0) {
  38. pcm_err(substream->pcm,
  39. "pcm timer resolution out of range (rate = %u, period_size = %lu)\n",
  40. runtime->rate, runtime->period_size);
  41. runtime->timer_resolution = -1;
  42. return;
  43. }
  44. runtime->timer_resolution = (mult * fsize / rate) * post;
  45. }
  46. static unsigned long snd_pcm_timer_resolution(struct snd_timer * timer)
  47. {
  48. struct snd_pcm_substream *substream;
  49. substream = timer->private_data;
  50. return substream->runtime ? substream->runtime->timer_resolution : 0;
  51. }
  52. static int snd_pcm_timer_start(struct snd_timer * timer)
  53. {
  54. struct snd_pcm_substream *substream;
  55. substream = snd_timer_chip(timer);
  56. substream->timer_running = 1;
  57. return 0;
  58. }
  59. static int snd_pcm_timer_stop(struct snd_timer * timer)
  60. {
  61. struct snd_pcm_substream *substream;
  62. substream = snd_timer_chip(timer);
  63. substream->timer_running = 0;
  64. return 0;
  65. }
  66. static const struct snd_timer_hardware snd_pcm_timer =
  67. {
  68. .flags = SNDRV_TIMER_HW_AUTO | SNDRV_TIMER_HW_SLAVE,
  69. .resolution = 0,
  70. .ticks = 1,
  71. .c_resolution = snd_pcm_timer_resolution,
  72. .start = snd_pcm_timer_start,
  73. .stop = snd_pcm_timer_stop,
  74. };
  75. /*
  76. * Init functions
  77. */
  78. static void snd_pcm_timer_free(struct snd_timer *timer)
  79. {
  80. struct snd_pcm_substream *substream = timer->private_data;
  81. substream->timer = NULL;
  82. }
  83. void snd_pcm_timer_init(struct snd_pcm_substream *substream)
  84. {
  85. struct snd_timer_id tid;
  86. struct snd_timer *timer;
  87. tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  88. tid.dev_class = SNDRV_TIMER_CLASS_PCM;
  89. tid.card = substream->pcm->card->number;
  90. tid.device = substream->pcm->device;
  91. tid.subdevice = (substream->number << 1) | (substream->stream & 1);
  92. if (snd_timer_new(substream->pcm->card, "PCM", &tid, &timer) < 0)
  93. return;
  94. sprintf(timer->name, "PCM %s %i-%i-%i",
  95. substream->stream == SNDRV_PCM_STREAM_CAPTURE ?
  96. "capture" : "playback",
  97. tid.card, tid.device, tid.subdevice);
  98. timer->hw = snd_pcm_timer;
  99. if (snd_device_register(timer->card, timer) < 0) {
  100. snd_device_free(timer->card, timer);
  101. return;
  102. }
  103. timer->private_data = substream;
  104. timer->private_free = snd_pcm_timer_free;
  105. substream->timer = timer;
  106. }
  107. void snd_pcm_timer_done(struct snd_pcm_substream *substream)
  108. {
  109. if (substream->timer) {
  110. snd_device_free(substream->pcm->card, substream->timer);
  111. substream->timer = NULL;
  112. }
  113. }