timer.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef __SOUND_TIMER_H
  3. #define __SOUND_TIMER_H
  4. /*
  5. * Timer abstract layer
  6. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>,
  7. * Abramo Bagnara <abramo@alsa-project.org>
  8. */
  9. #include <sound/asound.h>
  10. #include <linux/interrupt.h>
  11. #define snd_timer_chip(timer) ((timer)->private_data)
  12. #define SNDRV_TIMER_DEVICES 16
  13. #define SNDRV_TIMER_DEV_FLG_PCM 0x10000000
  14. #define SNDRV_TIMER_HW_AUTO 0x00000001 /* auto trigger is supported */
  15. #define SNDRV_TIMER_HW_STOP 0x00000002 /* call stop before start */
  16. #define SNDRV_TIMER_HW_SLAVE 0x00000004 /* only slave timer (variable resolution) */
  17. #define SNDRV_TIMER_HW_FIRST 0x00000008 /* first tick can be incomplete */
  18. #define SNDRV_TIMER_HW_WORK 0x00000010 /* timer is called from work */
  19. #define SNDRV_TIMER_IFLG_SLAVE 0x00000001
  20. #define SNDRV_TIMER_IFLG_RUNNING 0x00000002
  21. #define SNDRV_TIMER_IFLG_START 0x00000004
  22. #define SNDRV_TIMER_IFLG_AUTO 0x00000008 /* auto restart */
  23. #define SNDRV_TIMER_IFLG_FAST 0x00000010 /* fast callback (do not use work) */
  24. #define SNDRV_TIMER_IFLG_CALLBACK 0x00000020 /* timer callback is active */
  25. #define SNDRV_TIMER_IFLG_EXCLUSIVE 0x00000040 /* exclusive owner - no more instances */
  26. #define SNDRV_TIMER_IFLG_EARLY_EVENT 0x00000080 /* write early event to the poll queue */
  27. #define SNDRV_TIMER_FLG_CHANGE 0x00000001
  28. #define SNDRV_TIMER_FLG_RESCHED 0x00000002 /* need reschedule */
  29. struct snd_timer;
  30. struct snd_timer_hardware {
  31. /* -- must be filled with low-level driver */
  32. unsigned int flags; /* various flags */
  33. unsigned long resolution; /* average timer resolution for one tick in nsec */
  34. unsigned long resolution_min; /* minimal resolution */
  35. unsigned long resolution_max; /* maximal resolution */
  36. unsigned long ticks; /* max timer ticks per interrupt */
  37. /* -- low-level functions -- */
  38. int (*open) (struct snd_timer * timer);
  39. int (*close) (struct snd_timer * timer);
  40. unsigned long (*c_resolution) (struct snd_timer * timer);
  41. int (*start) (struct snd_timer * timer);
  42. int (*stop) (struct snd_timer * timer);
  43. int (*set_period) (struct snd_timer * timer, unsigned long period_num, unsigned long period_den);
  44. int (*precise_resolution) (struct snd_timer * timer, unsigned long *num, unsigned long *den);
  45. };
  46. struct snd_timer {
  47. int tmr_class;
  48. struct snd_card *card;
  49. struct module *module;
  50. int tmr_device;
  51. int tmr_subdevice;
  52. char id[64];
  53. char name[80];
  54. unsigned int flags;
  55. int running; /* running instances */
  56. unsigned long sticks; /* schedule ticks */
  57. void *private_data;
  58. void (*private_free) (struct snd_timer *timer);
  59. struct snd_timer_hardware hw;
  60. spinlock_t lock;
  61. struct list_head device_list;
  62. struct list_head open_list_head;
  63. struct list_head active_list_head;
  64. struct list_head ack_list_head;
  65. struct list_head sack_list_head; /* slow ack list head */
  66. struct work_struct task_work;
  67. int max_instances; /* upper limit of timer instances */
  68. int num_instances; /* current number of timer instances */
  69. };
  70. struct snd_timer_instance {
  71. struct snd_timer *timer;
  72. char *owner;
  73. unsigned int flags;
  74. void *private_data;
  75. void (*private_free) (struct snd_timer_instance *ti);
  76. void (*callback) (struct snd_timer_instance *timeri,
  77. unsigned long ticks, unsigned long resolution);
  78. void (*ccallback) (struct snd_timer_instance * timeri,
  79. int event,
  80. struct timespec64 * tstamp,
  81. unsigned long resolution);
  82. void (*disconnect)(struct snd_timer_instance *timeri);
  83. void *callback_data;
  84. unsigned long ticks; /* auto-load ticks when expired */
  85. unsigned long cticks; /* current ticks */
  86. unsigned long pticks; /* accumulated ticks for callback */
  87. unsigned long resolution; /* current resolution for work */
  88. unsigned long lost; /* lost ticks */
  89. int slave_class;
  90. unsigned int slave_id;
  91. struct list_head open_list;
  92. struct list_head active_list;
  93. struct list_head ack_list;
  94. struct list_head slave_list_head;
  95. struct list_head slave_active_head;
  96. struct snd_timer_instance *master;
  97. };
  98. /*
  99. * Registering
  100. */
  101. int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid, struct snd_timer **rtimer);
  102. void snd_timer_notify(struct snd_timer *timer, int event, struct timespec64 *tstamp);
  103. int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer);
  104. int snd_timer_global_free(struct snd_timer *timer);
  105. int snd_timer_global_register(struct snd_timer *timer);
  106. struct snd_timer_instance *snd_timer_instance_new(const char *owner);
  107. void snd_timer_instance_free(struct snd_timer_instance *timeri);
  108. int snd_timer_open(struct snd_timer_instance *timeri, struct snd_timer_id *tid, unsigned int slave_id);
  109. void snd_timer_close(struct snd_timer_instance *timeri);
  110. unsigned long snd_timer_resolution(struct snd_timer_instance *timeri);
  111. int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks);
  112. int snd_timer_stop(struct snd_timer_instance *timeri);
  113. int snd_timer_continue(struct snd_timer_instance *timeri);
  114. int snd_timer_pause(struct snd_timer_instance *timeri);
  115. void snd_timer_interrupt(struct snd_timer *timer, unsigned long ticks_left);
  116. #endif /* __SOUND_TIMER_H */