Lines Matching full:timer

19 #include <sound/timer.h>
39 MODULE_DESCRIPTION("ALSA timer interface");
47 MODULE_ALIAS("devname:snd/timer");
116 /* Internal data structure for keeping the state of the userspace-driven timer */
119 struct snd_timer *timer;
140 static int snd_timer_free(struct snd_timer *timer);
145 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
148 * create a timer instance with the given owner string.
184 * find a timer instance from the given timer id
188 struct snd_timer *timer;
190 list_for_each_entry(timer, &snd_timer_list, device_list) {
191 if (timer->tmr_class != tid->dev_class)
193 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
194 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
195 (timer->card == NULL ||
196 timer->card->number != tid->card))
198 if (timer->tmr_device != tid->device)
200 if (timer->tmr_subdevice != tid->subdevice)
202 return timer;
214 request_module("snd-timer-%i", tid->device);
235 if (master->timer->num_instances >= master->timer->max_instances)
238 master->timer->num_instances++;
240 guard(spinlock)(&master->timer->lock);
242 slave->timer = master->timer;
256 struct snd_timer *timer;
261 list_for_each_entry(timer, &snd_timer_list, device_list) {
262 list_for_each_entry(master, &timer->open_list_head, open_list) {
296 * open a timer instance
303 struct snd_timer *timer;
312 pr_debug("ALSA: timer: invalid slave class %i\n",
331 timer = snd_timer_find(tid);
333 if (!timer) {
337 timer = snd_timer_find(tid);
340 if (!timer) {
344 if (!list_empty(&timer->open_list_head)) {
346 list_entry(timer->open_list_head.next,
353 if (timer->num_instances >= timer->max_instances) {
357 if (!try_module_get(timer->module)) {
362 if (timer->card) {
363 get_device(&timer->card->card_dev);
364 card_dev_to_put = &timer->card->card_dev;
367 if (list_empty(&timer->open_list_head) && timer->hw.open) {
368 err = timer->hw.open(timer);
370 module_put(timer->module);
375 timeri->timer = timer;
379 list_add_tail(&timeri->open_list, &timer->open_list_head);
380 timer->num_instances++;
397 struct snd_timer *timer)
402 guard(spinlock)(&timer->lock);
403 timeri->timer = NULL;
406 timer->num_instances--;
408 slave->timer = NULL;
415 * close a timer instance
421 struct snd_timer *timer = timeri->timer;
423 if (timer) {
424 guard(spinlock_irq)(&timer->lock);
434 /* force to stop the timer */
437 if (timer) {
438 timer->num_instances--;
440 spin_lock_irq(&timer->lock);
442 spin_unlock_irq(&timer->lock);
444 spin_lock_irq(&timer->lock);
446 spin_unlock_irq(&timer->lock);
448 remove_slave_links(timeri, timer);
450 /* slave doesn't need to release timer resources below */
452 timer = NULL;
455 if (timer) {
456 if (list_empty(&timer->open_list_head) && timer->hw.close)
457 timer->hw.close(timer);
459 if (timer->card)
460 *card_devp_to_put = &timer->card->card_dev;
461 module_put(timer->module);
466 * close a timer instance
483 static unsigned long snd_timer_hw_resolution(struct snd_timer *timer)
485 if (timer->hw.c_resolution)
486 return timer->hw.c_resolution(timer);
488 return timer->hw.resolution;
493 struct snd_timer * timer;
498 timer = timeri->timer;
499 if (timer) {
500 guard(spinlock_irqsave)(&timer->lock);
501 ret = snd_timer_hw_resolution(timer);
509 struct snd_timer *timer = ti->timer;
521 if (timer &&
524 resolution = snd_timer_hw_resolution(timer);
529 if (timer == NULL)
531 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
539 /* start/continue a master timer */
543 struct snd_timer *timer;
546 timer = timeri->timer;
547 if (!timer)
550 guard(spinlock_irqsave)(&timer->lock);
553 if (timer->card && timer->card->shutdown)
562 if (start && !(timer->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
563 if ((u64)snd_timer_hw_resolution(timer) * ticks < 100000)
573 list_move_tail(&timeri->active_list, &timer->active_list_head);
574 if (timer->running) {
575 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
577 timer->flags |= SNDRV_TIMER_FLG_RESCHED;
582 timer->sticks = ticks;
583 timer->hw.start(timer);
585 timer->running++;
594 /* start/continue a slave timer */
604 if (timeri->master && timeri->timer) {
605 guard(spinlock)(&timeri->timer->lock);
614 /* stop/pause a master timer */
617 struct snd_timer *timer;
619 timer = timeri->timer;
620 if (!timer)
622 guard(spinlock_irqsave)(&timer->lock);
628 if (timer->card && timer->card->shutdown)
635 !(--timer->running)) {
636 timer->hw.stop(timer);
637 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
638 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
639 snd_timer_reschedule(timer, 0);
640 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
641 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
642 timer->hw.start(timer);
656 /* stop/pause a slave timer */
664 if (timeri->timer) {
665 guard(spinlock)(&timeri->timer->lock);
676 * start the timer instance
690 * stop the timer instance.
692 * do not call this from the timer callback!
708 /* timer can continue only after pause */
732 * reschedule the timer
735 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
737 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
742 list_for_each_entry(ti, &timer->active_list_head, active_list) {
746 timer->running++;
754 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
757 if (ticks > timer->hw.ticks)
758 ticks = timer->hw.ticks;
760 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
761 timer->sticks = ticks;
764 /* call callbacks in timer ack list */
765 static void snd_timer_process_callbacks(struct snd_timer *timer,
783 spin_unlock(&timer->lock);
786 spin_lock(&timer->lock);
793 static void snd_timer_clear_callbacks(struct snd_timer *timer,
796 guard(spinlock_irqsave)(&timer->lock);
802 * timer work
807 struct snd_timer *timer = container_of(work, struct snd_timer, task_work);
809 if (timer->card && timer->card->shutdown) {
810 snd_timer_clear_callbacks(timer, &timer->sack_list_head);
814 guard(spinlock_irqsave)(&timer->lock);
815 snd_timer_process_callbacks(timer, &timer->sack_list_head);
819 * timer interrupt
821 * ticks_left is usually equal to timer->sticks.
824 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
830 if (timer == NULL)
833 if (timer->card && timer->card->shutdown) {
834 snd_timer_clear_callbacks(timer, &timer->ack_list_head);
838 guard(spinlock_irqsave)(&timer->lock);
841 resolution = snd_timer_hw_resolution(timer);
848 list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
866 --timer->running;
869 if ((timer->hw.flags & SNDRV_TIMER_HW_WORK) ||
871 ack_list_head = &timer->ack_list_head;
873 ack_list_head = &timer->sack_list_head;
883 if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
884 snd_timer_reschedule(timer, timer->sticks);
885 if (timer->running) {
886 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
887 timer->hw.stop(timer);
888 timer->flags |= SNDRV_TIMER_FLG_CHANGE;
890 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
891 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
892 /* restart timer */
893 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
894 timer->hw.start(timer);
897 timer->hw.stop(timer);
901 snd_timer_process_callbacks(timer, &timer->ack_list_head);
904 if (!list_empty(&timer->sack_list_head))
905 queue_work(system_highpri_wq, &timer->task_work);
916 struct snd_timer *timer;
933 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
934 if (!timer)
936 timer->tmr_class = tid->dev_class;
937 timer->card = card;
938 timer->tmr_device = tid->device;
939 timer->tmr_subdevice = tid->subdevice;
941 strscpy(timer->id, id, sizeof(timer->id));
942 timer->sticks = 1;
943 INIT_LIST_HEAD(&timer->device_list);
944 INIT_LIST_HEAD(&timer->open_list_head);
945 INIT_LIST_HEAD(&timer->active_list_head);
946 INIT_LIST_HEAD(&timer->ack_list_head);
947 INIT_LIST_HEAD(&timer->sack_list_head);
948 spin_lock_init(&timer->lock);
949 INIT_WORK(&timer->task_work, snd_timer_work);
950 timer->max_instances = 1000; /* default limit per timer */
952 timer->module = card->module;
953 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
955 snd_timer_free(timer);
960 *rtimer = timer;
965 static int snd_timer_free(struct snd_timer *timer)
967 if (!timer)
971 if (! list_empty(&timer->open_list_head)) {
974 pr_warn("ALSA: timer %p is busy?\n", timer);
975 list_for_each_safe(p, n, &timer->open_list_head) {
978 ti->timer = NULL;
981 list_del(&timer->device_list);
983 if (timer->private_free)
984 timer->private_free(timer);
985 kfree(timer);
991 struct snd_timer *timer = device->device_data;
992 return snd_timer_free(timer);
997 struct snd_timer *timer = dev->device_data;
1000 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
1002 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
1003 !timer->hw.resolution && timer->hw.c_resolution == NULL)
1008 if (timer1->tmr_class > timer->tmr_class)
1010 if (timer1->tmr_class < timer->tmr_class)
1012 if (timer1->card && timer->card) {
1013 if (timer1->card->number > timer->card->number)
1015 if (timer1->card->number < timer->card->number)
1018 if (timer1->tmr_device > timer->tmr_device)
1020 if (timer1->tmr_device < timer->tmr_device)
1022 if (timer1->tmr_subdevice > timer->tmr_subdevice)
1024 if (timer1->tmr_subdevice < timer->tmr_subdevice)
1029 list_add_tail(&timer->device_list, &timer1->device_list);
1035 struct snd_timer *timer = device->device_data;
1039 list_del_init(&timer->device_list);
1041 list_for_each_entry(ti, &timer->open_list_head, open_list) {
1048 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec64 *tstamp)
1053 if (timer->card && timer->card->shutdown)
1055 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
1060 guard(spinlock_irqsave)(&timer->lock);
1064 resolution = snd_timer_hw_resolution(timer);
1065 list_for_each_entry(ti, &timer->active_list_head, active_list) {
1091 int snd_timer_global_free(struct snd_timer *timer)
1093 return snd_timer_free(timer);
1097 int snd_timer_global_register(struct snd_timer *timer)
1102 dev.device_data = timer;
1108 * System timer
1123 struct snd_timer *timer = priv->snd_timer;
1127 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
1130 static int snd_timer_s_start(struct snd_timer * timer)
1135 priv = (struct snd_timer_system_private *) timer->private_data;
1137 if (priv->correction > timer->sticks - 1) {
1138 priv->correction -= timer->sticks - 1;
1141 njiff += timer->sticks - priv->correction;
1149 static int snd_timer_s_stop(struct snd_timer * timer)
1154 priv = (struct snd_timer_system_private *) timer->private_data;
1158 timer->sticks = priv->last_expires - jiff;
1160 timer->sticks = 1;
1165 static int snd_timer_s_close(struct snd_timer *timer)
1169 priv = (struct snd_timer_system_private *)timer->private_data;
1184 static void snd_timer_free_system(struct snd_timer *timer)
1186 kfree(timer->private_data);
1191 struct snd_timer *timer;
1195 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
1198 strscpy(timer->name, "system timer");
1199 timer->hw = snd_timer_system;
1202 snd_timer_free(timer);
1205 priv->snd_timer = timer;
1207 timer->private_data = priv;
1208 timer->private_free = snd_timer_free_system;
1209 return snd_timer_global_register(timer);
1220 struct snd_timer *timer;
1225 list_for_each_entry(timer, &snd_timer_list, device_list) {
1226 if (timer->card && timer->card->shutdown)
1228 switch (timer->tmr_class) {
1230 snd_iprintf(buffer, "G%i: ", timer->tmr_device);
1234 timer->card->number, timer->tmr_device);
1237 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
1238 timer->tmr_device, timer->tmr_subdevice);
1241 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
1242 timer->card ? timer->card->number : -1,
1243 timer->tmr_device, timer->tmr_subdevice);
1245 snd_iprintf(buffer, "%s :", timer->name);
1246 scoped_guard(spinlock_irq, &timer->lock)
1247 resolution = snd_timer_hw_resolution(timer);
1252 timer->hw.ticks);
1253 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
1256 list_for_each_entry(ti, &timer->open_list_head, open_list)
1509 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
1511 id->dev_class = timer->tmr_class;
1513 id->card = timer->card ? timer->card->number : -1;
1514 id->device = timer->tmr_device;
1515 id->subdevice = timer->tmr_subdevice;
1520 struct snd_timer *timer;
1527 timer = list_entry(snd_timer_list.next,
1529 snd_timer_user_copy_id(id, timer);
1536 timer = list_entry(p, struct snd_timer, device_list);
1537 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
1538 snd_timer_user_copy_id(id, timer);
1541 if (timer->tmr_device >= id->device) {
1542 snd_timer_user_copy_id(id, timer);
1564 timer = list_entry(p, struct snd_timer, device_list);
1565 if (timer->tmr_class > id->dev_class) {
1566 snd_timer_user_copy_id(id, timer);
1569 if (timer->tmr_class < id->dev_class)
1571 if (timer->card->number > id->card) {
1572 snd_timer_user_copy_id(id, timer);
1575 if (timer->card->number < id->card)
1577 if (timer->tmr_device > id->device) {
1578 snd_timer_user_copy_id(id, timer);
1581 if (timer->tmr_device < id->device)
1583 if (timer->tmr_subdevice > id->subdevice) {
1584 snd_timer_user_copy_id(id, timer);
1587 if (timer->tmr_subdevice < id->subdevice)
1589 snd_timer_user_copy_id(id, timer);
1765 t = tu->timeri->timer;
1795 t = tu->timeri->timer;
1970 /* start timer instead of continue if it's not used before */
2034 * which will allow us to use them (basically, the subdevice number of udriven timer).
2053 snd_timer_free(utimer->timer);
2071 snd_timer_interrupt(utimer->timer, utimer->timer->sticks);
2123 struct snd_timer *timer;
2156 err = snd_timer_new(NULL, utimer->name, &tid, &timer);
2158 pr_err("Can't create userspace-driven timer\n");
2162 timer->module = THIS_MODULE;
2163 timer->hw = timer_hw;
2164 timer->hw.resolution = utimer_info->resolution;
2165 timer->hw.ticks = 1;
2166 timer->max_instances = MAX_SLAVE_INSTANCES;
2168 utimer->timer = timer;
2170 err = snd_timer_global_register(timer);
2172 pr_err("Can't register a userspace-driven timer\n");
2180 snd_timer_free(timer);
2452 /* unregister the system timer */
2455 struct snd_timer *timer, *n;
2457 list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
2458 snd_timer_free(timer);
2474 dev_set_name(timer_dev, "timer");
2478 "system timer");
2483 pr_err("ALSA: unable to register system timer (%i)\n", err);
2490 pr_err("ALSA: unable to register timer device (%i)\n", err);