hrtimer.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ALSA timer back-end using hrtimer
  4. * Copyright (C) 2008 Takashi Iwai
  5. */
  6. #include <linux/init.h>
  7. #include <linux/slab.h>
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/hrtimer.h>
  11. #include <sound/core.h>
  12. #include <sound/timer.h>
  13. MODULE_AUTHOR("Takashi Iwai <[email protected]>");
  14. MODULE_DESCRIPTION("ALSA hrtimer backend");
  15. MODULE_LICENSE("GPL");
  16. MODULE_ALIAS("snd-timer-" __stringify(SNDRV_TIMER_GLOBAL_HRTIMER));
  17. #define NANO_SEC 1000000000UL /* 10^9 in sec */
  18. static unsigned int resolution;
  19. struct snd_hrtimer {
  20. struct snd_timer *timer;
  21. struct hrtimer hrt;
  22. bool in_callback;
  23. };
  24. static enum hrtimer_restart snd_hrtimer_callback(struct hrtimer *hrt)
  25. {
  26. struct snd_hrtimer *stime = container_of(hrt, struct snd_hrtimer, hrt);
  27. struct snd_timer *t = stime->timer;
  28. ktime_t delta;
  29. unsigned long ticks;
  30. enum hrtimer_restart ret = HRTIMER_NORESTART;
  31. spin_lock(&t->lock);
  32. if (!t->running)
  33. goto out; /* fast path */
  34. stime->in_callback = true;
  35. ticks = t->sticks;
  36. spin_unlock(&t->lock);
  37. /* calculate the drift */
  38. delta = ktime_sub(hrt->base->get_time(), hrtimer_get_expires(hrt));
  39. if (delta > 0)
  40. ticks += ktime_divns(delta, ticks * resolution);
  41. snd_timer_interrupt(stime->timer, ticks);
  42. spin_lock(&t->lock);
  43. if (t->running) {
  44. hrtimer_add_expires_ns(hrt, t->sticks * resolution);
  45. ret = HRTIMER_RESTART;
  46. }
  47. stime->in_callback = false;
  48. out:
  49. spin_unlock(&t->lock);
  50. return ret;
  51. }
  52. static int snd_hrtimer_open(struct snd_timer *t)
  53. {
  54. struct snd_hrtimer *stime;
  55. stime = kzalloc(sizeof(*stime), GFP_KERNEL);
  56. if (!stime)
  57. return -ENOMEM;
  58. hrtimer_init(&stime->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  59. stime->timer = t;
  60. stime->hrt.function = snd_hrtimer_callback;
  61. t->private_data = stime;
  62. return 0;
  63. }
  64. static int snd_hrtimer_close(struct snd_timer *t)
  65. {
  66. struct snd_hrtimer *stime = t->private_data;
  67. if (stime) {
  68. spin_lock_irq(&t->lock);
  69. t->running = 0; /* just to be sure */
  70. stime->in_callback = 1; /* skip start/stop */
  71. spin_unlock_irq(&t->lock);
  72. hrtimer_cancel(&stime->hrt);
  73. kfree(stime);
  74. t->private_data = NULL;
  75. }
  76. return 0;
  77. }
  78. static int snd_hrtimer_start(struct snd_timer *t)
  79. {
  80. struct snd_hrtimer *stime = t->private_data;
  81. if (stime->in_callback)
  82. return 0;
  83. hrtimer_start(&stime->hrt, ns_to_ktime(t->sticks * resolution),
  84. HRTIMER_MODE_REL);
  85. return 0;
  86. }
  87. static int snd_hrtimer_stop(struct snd_timer *t)
  88. {
  89. struct snd_hrtimer *stime = t->private_data;
  90. if (stime->in_callback)
  91. return 0;
  92. hrtimer_try_to_cancel(&stime->hrt);
  93. return 0;
  94. }
  95. static const struct snd_timer_hardware hrtimer_hw __initconst = {
  96. .flags = SNDRV_TIMER_HW_AUTO | SNDRV_TIMER_HW_WORK,
  97. .open = snd_hrtimer_open,
  98. .close = snd_hrtimer_close,
  99. .start = snd_hrtimer_start,
  100. .stop = snd_hrtimer_stop,
  101. };
  102. /*
  103. * entry functions
  104. */
  105. static struct snd_timer *mytimer;
  106. static int __init snd_hrtimer_init(void)
  107. {
  108. struct snd_timer *timer;
  109. int err;
  110. resolution = hrtimer_resolution;
  111. /* Create a new timer and set up the fields */
  112. err = snd_timer_global_new("hrtimer", SNDRV_TIMER_GLOBAL_HRTIMER,
  113. &timer);
  114. if (err < 0)
  115. return err;
  116. timer->module = THIS_MODULE;
  117. strcpy(timer->name, "HR timer");
  118. timer->hw = hrtimer_hw;
  119. timer->hw.resolution = resolution;
  120. timer->hw.ticks = NANO_SEC / resolution;
  121. timer->max_instances = 100; /* lower the limit */
  122. err = snd_timer_global_register(timer);
  123. if (err < 0) {
  124. snd_timer_global_free(timer);
  125. return err;
  126. }
  127. mytimer = timer; /* remember this */
  128. return 0;
  129. }
  130. static void __exit snd_hrtimer_exit(void)
  131. {
  132. if (mytimer) {
  133. snd_timer_global_free(mytimer);
  134. mytimer = NULL;
  135. }
  136. }
  137. module_init(snd_hrtimer_init);
  138. module_exit(snd_hrtimer_exit);