11a59d1b8SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */ 21da177e4SLinus Torvalds #ifndef __SOUND_TIMER_H 31da177e4SLinus Torvalds #define __SOUND_TIMER_H 41da177e4SLinus Torvalds 51da177e4SLinus Torvalds /* 61da177e4SLinus Torvalds * Timer abstract layer 7c1017a4cSJaroslav Kysela * Copyright (c) by Jaroslav Kysela <perex@perex.cz>, 81da177e4SLinus Torvalds * Abramo Bagnara <abramo@alsa-project.org> 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds #include <sound/asound.h> 121da177e4SLinus Torvalds #include <linux/interrupt.h> 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds #define snd_timer_chip(timer) ((timer)->private_data) 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #define SNDRV_TIMER_DEVICES 16 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds #define SNDRV_TIMER_DEV_FLG_PCM 0x10000000 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds #define SNDRV_TIMER_HW_AUTO 0x00000001 /* auto trigger is supported */ 211da177e4SLinus Torvalds #define SNDRV_TIMER_HW_STOP 0x00000002 /* call stop before start */ 221da177e4SLinus Torvalds #define SNDRV_TIMER_HW_SLAVE 0x00000004 /* only slave timer (variable resolution) */ 231da177e4SLinus Torvalds #define SNDRV_TIMER_HW_FIRST 0x00000008 /* first tick can be incomplete */ 24*bf083595STakashi Iwai #define SNDRV_TIMER_HW_WORK 0x00000010 /* timer is called from work */ 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #define SNDRV_TIMER_IFLG_SLAVE 0x00000001 271da177e4SLinus Torvalds #define SNDRV_TIMER_IFLG_RUNNING 0x00000002 281da177e4SLinus Torvalds #define SNDRV_TIMER_IFLG_START 0x00000004 291da177e4SLinus Torvalds #define SNDRV_TIMER_IFLG_AUTO 0x00000008 /* auto restart */ 30*bf083595STakashi Iwai #define SNDRV_TIMER_IFLG_FAST 0x00000010 /* fast callback (do not use work) */ 311da177e4SLinus Torvalds #define SNDRV_TIMER_IFLG_CALLBACK 0x00000020 /* timer callback is active */ 321da177e4SLinus Torvalds #define SNDRV_TIMER_IFLG_EXCLUSIVE 0x00000040 /* exclusive owner - no more instances */ 331da177e4SLinus Torvalds #define SNDRV_TIMER_IFLG_EARLY_EVENT 0x00000080 /* write early event to the poll queue */ 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds #define SNDRV_TIMER_FLG_CHANGE 0x00000001 361da177e4SLinus Torvalds #define SNDRV_TIMER_FLG_RESCHED 0x00000002 /* need reschedule */ 371da177e4SLinus Torvalds 3853d2f744STakashi Iwai struct snd_timer; 391da177e4SLinus Torvalds 4053d2f744STakashi Iwai struct snd_timer_hardware { 411da177e4SLinus Torvalds /* -- must be filled with low-level driver */ 421da177e4SLinus Torvalds unsigned int flags; /* various flags */ 431da177e4SLinus Torvalds unsigned long resolution; /* average timer resolution for one tick in nsec */ 441da177e4SLinus Torvalds unsigned long resolution_min; /* minimal resolution */ 451da177e4SLinus Torvalds unsigned long resolution_max; /* maximal resolution */ 461da177e4SLinus Torvalds unsigned long ticks; /* max timer ticks per interrupt */ 471da177e4SLinus Torvalds /* -- low-level functions -- */ 4853d2f744STakashi Iwai int (*open) (struct snd_timer * timer); 4953d2f744STakashi Iwai int (*close) (struct snd_timer * timer); 5053d2f744STakashi Iwai unsigned long (*c_resolution) (struct snd_timer * timer); 5153d2f744STakashi Iwai int (*start) (struct snd_timer * timer); 5253d2f744STakashi Iwai int (*stop) (struct snd_timer * timer); 5353d2f744STakashi Iwai int (*set_period) (struct snd_timer * timer, unsigned long period_num, unsigned long period_den); 5453d2f744STakashi Iwai int (*precise_resolution) (struct snd_timer * timer, unsigned long *num, unsigned long *den); 551da177e4SLinus Torvalds }; 561da177e4SLinus Torvalds 5753d2f744STakashi Iwai struct snd_timer { 5853d2f744STakashi Iwai int tmr_class; 5953d2f744STakashi Iwai struct snd_card *card; 60de24214dSClemens Ladisch struct module *module; 611da177e4SLinus Torvalds int tmr_device; 621da177e4SLinus Torvalds int tmr_subdevice; 631da177e4SLinus Torvalds char id[64]; 641da177e4SLinus Torvalds char name[80]; 651da177e4SLinus Torvalds unsigned int flags; 661da177e4SLinus Torvalds int running; /* running instances */ 671da177e4SLinus Torvalds unsigned long sticks; /* schedule ticks */ 681da177e4SLinus Torvalds void *private_data; 6953d2f744STakashi Iwai void (*private_free) (struct snd_timer *timer); 7053d2f744STakashi Iwai struct snd_timer_hardware hw; 711da177e4SLinus Torvalds spinlock_t lock; 721da177e4SLinus Torvalds struct list_head device_list; 731da177e4SLinus Torvalds struct list_head open_list_head; 741da177e4SLinus Torvalds struct list_head active_list_head; 751da177e4SLinus Torvalds struct list_head ack_list_head; 761da177e4SLinus Torvalds struct list_head sack_list_head; /* slow ack list head */ 77*bf083595STakashi Iwai struct work_struct task_work; 789b7d869eSTakashi Iwai int max_instances; /* upper limit of timer instances */ 799b7d869eSTakashi Iwai int num_instances; /* current number of timer instances */ 801da177e4SLinus Torvalds }; 811da177e4SLinus Torvalds 8253d2f744STakashi Iwai struct snd_timer_instance { 8353d2f744STakashi Iwai struct snd_timer *timer; 841da177e4SLinus Torvalds char *owner; 851da177e4SLinus Torvalds unsigned int flags; 861da177e4SLinus Torvalds void *private_data; 8753d2f744STakashi Iwai void (*private_free) (struct snd_timer_instance *ti); 8853d2f744STakashi Iwai void (*callback) (struct snd_timer_instance *timeri, 8953d2f744STakashi Iwai unsigned long ticks, unsigned long resolution); 9053d2f744STakashi Iwai void (*ccallback) (struct snd_timer_instance * timeri, 9153d2f744STakashi Iwai int event, 92fcae40c9SBaolin Wang struct timespec64 * tstamp, 9353d2f744STakashi Iwai unsigned long resolution); 9440ed9444STakashi Iwai void (*disconnect)(struct snd_timer_instance *timeri); 951da177e4SLinus Torvalds void *callback_data; 961da177e4SLinus Torvalds unsigned long ticks; /* auto-load ticks when expired */ 971da177e4SLinus Torvalds unsigned long cticks; /* current ticks */ 981da177e4SLinus Torvalds unsigned long pticks; /* accumulated ticks for callback */ 99*bf083595STakashi Iwai unsigned long resolution; /* current resolution for work */ 1001da177e4SLinus Torvalds unsigned long lost; /* lost ticks */ 10153d2f744STakashi Iwai int slave_class; 1021da177e4SLinus Torvalds unsigned int slave_id; 1031da177e4SLinus Torvalds struct list_head open_list; 1041da177e4SLinus Torvalds struct list_head active_list; 1051da177e4SLinus Torvalds struct list_head ack_list; 1061da177e4SLinus Torvalds struct list_head slave_list_head; 1071da177e4SLinus Torvalds struct list_head slave_active_head; 10853d2f744STakashi Iwai struct snd_timer_instance *master; 1091da177e4SLinus Torvalds }; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds /* 1121da177e4SLinus Torvalds * Registering 1131da177e4SLinus Torvalds */ 1141da177e4SLinus Torvalds 11553d2f744STakashi Iwai int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid, struct snd_timer **rtimer); 116fcae40c9SBaolin Wang void snd_timer_notify(struct snd_timer *timer, int event, struct timespec64 *tstamp); 11753d2f744STakashi Iwai int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer); 11853d2f744STakashi Iwai int snd_timer_global_free(struct snd_timer *timer); 11953d2f744STakashi Iwai int snd_timer_global_register(struct snd_timer *timer); 1201da177e4SLinus Torvalds 1216a34367eSTakashi Iwai struct snd_timer_instance *snd_timer_instance_new(const char *owner); 1226a34367eSTakashi Iwai void snd_timer_instance_free(struct snd_timer_instance *timeri); 1236a34367eSTakashi Iwai int snd_timer_open(struct snd_timer_instance *timeri, struct snd_timer_id *tid, unsigned int slave_id); 12433bbb8a0STakashi Iwai void snd_timer_close(struct snd_timer_instance *timeri); 12553d2f744STakashi Iwai unsigned long snd_timer_resolution(struct snd_timer_instance *timeri); 12653d2f744STakashi Iwai int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks); 12753d2f744STakashi Iwai int snd_timer_stop(struct snd_timer_instance *timeri); 12853d2f744STakashi Iwai int snd_timer_continue(struct snd_timer_instance *timeri); 12953d2f744STakashi Iwai int snd_timer_pause(struct snd_timer_instance *timeri); 1301da177e4SLinus Torvalds 13153d2f744STakashi Iwai void snd_timer_interrupt(struct snd_timer *timer, unsigned long ticks_left); 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds #endif /* __SOUND_TIMER_H */ 134