Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *   ALSA sequencer Timer
0004  *   Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
0005  *                              Jaroslav Kysela <perex@perex.cz>
0006  */
0007 
0008 #include <sound/core.h>
0009 #include <linux/slab.h>
0010 #include "seq_timer.h"
0011 #include "seq_queue.h"
0012 #include "seq_info.h"
0013 
0014 /* allowed sequencer timer frequencies, in Hz */
0015 #define MIN_FREQUENCY       10
0016 #define MAX_FREQUENCY       6250
0017 #define DEFAULT_FREQUENCY   1000
0018 
0019 #define SKEW_BASE   0x10000 /* 16bit shift */
0020 
0021 static void snd_seq_timer_set_tick_resolution(struct snd_seq_timer *tmr)
0022 {
0023     if (tmr->tempo < 1000000)
0024         tmr->tick.resolution = (tmr->tempo * 1000) / tmr->ppq;
0025     else {
0026         /* might overflow.. */
0027         unsigned int s;
0028         s = tmr->tempo % tmr->ppq;
0029         s = (s * 1000) / tmr->ppq;
0030         tmr->tick.resolution = (tmr->tempo / tmr->ppq) * 1000;
0031         tmr->tick.resolution += s;
0032     }
0033     if (tmr->tick.resolution <= 0)
0034         tmr->tick.resolution = 1;
0035     snd_seq_timer_update_tick(&tmr->tick, 0);
0036 }
0037 
0038 /* create new timer (constructor) */
0039 struct snd_seq_timer *snd_seq_timer_new(void)
0040 {
0041     struct snd_seq_timer *tmr;
0042     
0043     tmr = kzalloc(sizeof(*tmr), GFP_KERNEL);
0044     if (!tmr)
0045         return NULL;
0046     spin_lock_init(&tmr->lock);
0047 
0048     /* reset setup to defaults */
0049     snd_seq_timer_defaults(tmr);
0050     
0051     /* reset time */
0052     snd_seq_timer_reset(tmr);
0053     
0054     return tmr;
0055 }
0056 
0057 /* delete timer (destructor) */
0058 void snd_seq_timer_delete(struct snd_seq_timer **tmr)
0059 {
0060     struct snd_seq_timer *t = *tmr;
0061     *tmr = NULL;
0062 
0063     if (t == NULL) {
0064         pr_debug("ALSA: seq: snd_seq_timer_delete() called with NULL timer\n");
0065         return;
0066     }
0067     t->running = 0;
0068 
0069     /* reset time */
0070     snd_seq_timer_stop(t);
0071     snd_seq_timer_reset(t);
0072 
0073     kfree(t);
0074 }
0075 
0076 void snd_seq_timer_defaults(struct snd_seq_timer * tmr)
0077 {
0078     unsigned long flags;
0079 
0080     spin_lock_irqsave(&tmr->lock, flags);
0081     /* setup defaults */
0082     tmr->ppq = 96;      /* 96 PPQ */
0083     tmr->tempo = 500000;    /* 120 BPM */
0084     snd_seq_timer_set_tick_resolution(tmr);
0085     tmr->running = 0;
0086 
0087     tmr->type = SNDRV_SEQ_TIMER_ALSA;
0088     tmr->alsa_id.dev_class = seq_default_timer_class;
0089     tmr->alsa_id.dev_sclass = seq_default_timer_sclass;
0090     tmr->alsa_id.card = seq_default_timer_card;
0091     tmr->alsa_id.device = seq_default_timer_device;
0092     tmr->alsa_id.subdevice = seq_default_timer_subdevice;
0093     tmr->preferred_resolution = seq_default_timer_resolution;
0094 
0095     tmr->skew = tmr->skew_base = SKEW_BASE;
0096     spin_unlock_irqrestore(&tmr->lock, flags);
0097 }
0098 
0099 static void seq_timer_reset(struct snd_seq_timer *tmr)
0100 {
0101     /* reset time & songposition */
0102     tmr->cur_time.tv_sec = 0;
0103     tmr->cur_time.tv_nsec = 0;
0104 
0105     tmr->tick.cur_tick = 0;
0106     tmr->tick.fraction = 0;
0107 }
0108 
0109 void snd_seq_timer_reset(struct snd_seq_timer *tmr)
0110 {
0111     unsigned long flags;
0112 
0113     spin_lock_irqsave(&tmr->lock, flags);
0114     seq_timer_reset(tmr);
0115     spin_unlock_irqrestore(&tmr->lock, flags);
0116 }
0117 
0118 
0119 /* called by timer interrupt routine. the period time since previous invocation is passed */
0120 static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri,
0121                     unsigned long resolution,
0122                     unsigned long ticks)
0123 {
0124     unsigned long flags;
0125     struct snd_seq_queue *q = timeri->callback_data;
0126     struct snd_seq_timer *tmr;
0127 
0128     if (q == NULL)
0129         return;
0130     tmr = q->timer;
0131     if (tmr == NULL)
0132         return;
0133     spin_lock_irqsave(&tmr->lock, flags);
0134     if (!tmr->running) {
0135         spin_unlock_irqrestore(&tmr->lock, flags);
0136         return;
0137     }
0138 
0139     resolution *= ticks;
0140     if (tmr->skew != tmr->skew_base) {
0141         /* FIXME: assuming skew_base = 0x10000 */
0142         resolution = (resolution >> 16) * tmr->skew +
0143             (((resolution & 0xffff) * tmr->skew) >> 16);
0144     }
0145 
0146     /* update timer */
0147     snd_seq_inc_time_nsec(&tmr->cur_time, resolution);
0148 
0149     /* calculate current tick */
0150     snd_seq_timer_update_tick(&tmr->tick, resolution);
0151 
0152     /* register actual time of this timer update */
0153     ktime_get_ts64(&tmr->last_update);
0154 
0155     spin_unlock_irqrestore(&tmr->lock, flags);
0156 
0157     /* check queues and dispatch events */
0158     snd_seq_check_queue(q, 1, 0);
0159 }
0160 
0161 /* set current tempo */
0162 int snd_seq_timer_set_tempo(struct snd_seq_timer * tmr, int tempo)
0163 {
0164     unsigned long flags;
0165 
0166     if (snd_BUG_ON(!tmr))
0167         return -EINVAL;
0168     if (tempo <= 0)
0169         return -EINVAL;
0170     spin_lock_irqsave(&tmr->lock, flags);
0171     if ((unsigned int)tempo != tmr->tempo) {
0172         tmr->tempo = tempo;
0173         snd_seq_timer_set_tick_resolution(tmr);
0174     }
0175     spin_unlock_irqrestore(&tmr->lock, flags);
0176     return 0;
0177 }
0178 
0179 /* set current tempo and ppq in a shot */
0180 int snd_seq_timer_set_tempo_ppq(struct snd_seq_timer *tmr, int tempo, int ppq)
0181 {
0182     int changed;
0183     unsigned long flags;
0184 
0185     if (snd_BUG_ON(!tmr))
0186         return -EINVAL;
0187     if (tempo <= 0 || ppq <= 0)
0188         return -EINVAL;
0189     spin_lock_irqsave(&tmr->lock, flags);
0190     if (tmr->running && (ppq != tmr->ppq)) {
0191         /* refuse to change ppq on running timers */
0192         /* because it will upset the song position (ticks) */
0193         spin_unlock_irqrestore(&tmr->lock, flags);
0194         pr_debug("ALSA: seq: cannot change ppq of a running timer\n");
0195         return -EBUSY;
0196     }
0197     changed = (tempo != tmr->tempo) || (ppq != tmr->ppq);
0198     tmr->tempo = tempo;
0199     tmr->ppq = ppq;
0200     if (changed)
0201         snd_seq_timer_set_tick_resolution(tmr);
0202     spin_unlock_irqrestore(&tmr->lock, flags);
0203     return 0;
0204 }
0205 
0206 /* set current tick position */
0207 int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr,
0208                     snd_seq_tick_time_t position)
0209 {
0210     unsigned long flags;
0211 
0212     if (snd_BUG_ON(!tmr))
0213         return -EINVAL;
0214 
0215     spin_lock_irqsave(&tmr->lock, flags);
0216     tmr->tick.cur_tick = position;
0217     tmr->tick.fraction = 0;
0218     spin_unlock_irqrestore(&tmr->lock, flags);
0219     return 0;
0220 }
0221 
0222 /* set current real-time position */
0223 int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr,
0224                     snd_seq_real_time_t position)
0225 {
0226     unsigned long flags;
0227 
0228     if (snd_BUG_ON(!tmr))
0229         return -EINVAL;
0230 
0231     snd_seq_sanity_real_time(&position);
0232     spin_lock_irqsave(&tmr->lock, flags);
0233     tmr->cur_time = position;
0234     spin_unlock_irqrestore(&tmr->lock, flags);
0235     return 0;
0236 }
0237 
0238 /* set timer skew */
0239 int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew,
0240                unsigned int base)
0241 {
0242     unsigned long flags;
0243 
0244     if (snd_BUG_ON(!tmr))
0245         return -EINVAL;
0246 
0247     /* FIXME */
0248     if (base != SKEW_BASE) {
0249         pr_debug("ALSA: seq: invalid skew base 0x%x\n", base);
0250         return -EINVAL;
0251     }
0252     spin_lock_irqsave(&tmr->lock, flags);
0253     tmr->skew = skew;
0254     spin_unlock_irqrestore(&tmr->lock, flags);
0255     return 0;
0256 }
0257 
0258 int snd_seq_timer_open(struct snd_seq_queue *q)
0259 {
0260     struct snd_timer_instance *t;
0261     struct snd_seq_timer *tmr;
0262     char str[32];
0263     int err;
0264 
0265     tmr = q->timer;
0266     if (snd_BUG_ON(!tmr))
0267         return -EINVAL;
0268     if (tmr->timeri)
0269         return -EBUSY;
0270     sprintf(str, "sequencer queue %i", q->queue);
0271     if (tmr->type != SNDRV_SEQ_TIMER_ALSA)  /* standard ALSA timer */
0272         return -EINVAL;
0273     if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
0274         tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
0275     t = snd_timer_instance_new(str);
0276     if (!t)
0277         return -ENOMEM;
0278     t->callback = snd_seq_timer_interrupt;
0279     t->callback_data = q;
0280     t->flags |= SNDRV_TIMER_IFLG_AUTO;
0281     err = snd_timer_open(t, &tmr->alsa_id, q->queue);
0282     if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) {
0283         if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL ||
0284             tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) {
0285             struct snd_timer_id tid;
0286             memset(&tid, 0, sizeof(tid));
0287             tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
0288             tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
0289             tid.card = -1;
0290             tid.device = SNDRV_TIMER_GLOBAL_SYSTEM;
0291             err = snd_timer_open(t, &tid, q->queue);
0292         }
0293     }
0294     if (err < 0) {
0295         pr_err("ALSA: seq fatal error: cannot create timer (%i)\n", err);
0296         snd_timer_instance_free(t);
0297         return err;
0298     }
0299     spin_lock_irq(&tmr->lock);
0300     if (tmr->timeri)
0301         err = -EBUSY;
0302     else
0303         tmr->timeri = t;
0304     spin_unlock_irq(&tmr->lock);
0305     if (err < 0) {
0306         snd_timer_close(t);
0307         snd_timer_instance_free(t);
0308         return err;
0309     }
0310     return 0;
0311 }
0312 
0313 int snd_seq_timer_close(struct snd_seq_queue *q)
0314 {
0315     struct snd_seq_timer *tmr;
0316     struct snd_timer_instance *t;
0317     
0318     tmr = q->timer;
0319     if (snd_BUG_ON(!tmr))
0320         return -EINVAL;
0321     spin_lock_irq(&tmr->lock);
0322     t = tmr->timeri;
0323     tmr->timeri = NULL;
0324     spin_unlock_irq(&tmr->lock);
0325     if (t) {
0326         snd_timer_close(t);
0327         snd_timer_instance_free(t);
0328     }
0329     return 0;
0330 }
0331 
0332 static int seq_timer_stop(struct snd_seq_timer *tmr)
0333 {
0334     if (! tmr->timeri)
0335         return -EINVAL;
0336     if (!tmr->running)
0337         return 0;
0338     tmr->running = 0;
0339     snd_timer_pause(tmr->timeri);
0340     return 0;
0341 }
0342 
0343 int snd_seq_timer_stop(struct snd_seq_timer *tmr)
0344 {
0345     unsigned long flags;
0346     int err;
0347 
0348     spin_lock_irqsave(&tmr->lock, flags);
0349     err = seq_timer_stop(tmr);
0350     spin_unlock_irqrestore(&tmr->lock, flags);
0351     return err;
0352 }
0353 
0354 static int initialize_timer(struct snd_seq_timer *tmr)
0355 {
0356     struct snd_timer *t;
0357     unsigned long freq;
0358 
0359     t = tmr->timeri->timer;
0360     if (!t)
0361         return -EINVAL;
0362 
0363     freq = tmr->preferred_resolution;
0364     if (!freq)
0365         freq = DEFAULT_FREQUENCY;
0366     else if (freq < MIN_FREQUENCY)
0367         freq = MIN_FREQUENCY;
0368     else if (freq > MAX_FREQUENCY)
0369         freq = MAX_FREQUENCY;
0370 
0371     tmr->ticks = 1;
0372     if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
0373         unsigned long r = snd_timer_resolution(tmr->timeri);
0374         if (r) {
0375             tmr->ticks = (unsigned int)(1000000000uL / (r * freq));
0376             if (! tmr->ticks)
0377                 tmr->ticks = 1;
0378         }
0379     }
0380     tmr->initialized = 1;
0381     return 0;
0382 }
0383 
0384 static int seq_timer_start(struct snd_seq_timer *tmr)
0385 {
0386     if (! tmr->timeri)
0387         return -EINVAL;
0388     if (tmr->running)
0389         seq_timer_stop(tmr);
0390     seq_timer_reset(tmr);
0391     if (initialize_timer(tmr) < 0)
0392         return -EINVAL;
0393     snd_timer_start(tmr->timeri, tmr->ticks);
0394     tmr->running = 1;
0395     ktime_get_ts64(&tmr->last_update);
0396     return 0;
0397 }
0398 
0399 int snd_seq_timer_start(struct snd_seq_timer *tmr)
0400 {
0401     unsigned long flags;
0402     int err;
0403 
0404     spin_lock_irqsave(&tmr->lock, flags);
0405     err = seq_timer_start(tmr);
0406     spin_unlock_irqrestore(&tmr->lock, flags);
0407     return err;
0408 }
0409 
0410 static int seq_timer_continue(struct snd_seq_timer *tmr)
0411 {
0412     if (! tmr->timeri)
0413         return -EINVAL;
0414     if (tmr->running)
0415         return -EBUSY;
0416     if (! tmr->initialized) {
0417         seq_timer_reset(tmr);
0418         if (initialize_timer(tmr) < 0)
0419             return -EINVAL;
0420     }
0421     snd_timer_start(tmr->timeri, tmr->ticks);
0422     tmr->running = 1;
0423     ktime_get_ts64(&tmr->last_update);
0424     return 0;
0425 }
0426 
0427 int snd_seq_timer_continue(struct snd_seq_timer *tmr)
0428 {
0429     unsigned long flags;
0430     int err;
0431 
0432     spin_lock_irqsave(&tmr->lock, flags);
0433     err = seq_timer_continue(tmr);
0434     spin_unlock_irqrestore(&tmr->lock, flags);
0435     return err;
0436 }
0437 
0438 /* return current 'real' time. use timeofday() to get better granularity. */
0439 snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr,
0440                            bool adjust_ktime)
0441 {
0442     snd_seq_real_time_t cur_time;
0443     unsigned long flags;
0444 
0445     spin_lock_irqsave(&tmr->lock, flags);
0446     cur_time = tmr->cur_time;
0447     if (adjust_ktime && tmr->running) {
0448         struct timespec64 tm;
0449 
0450         ktime_get_ts64(&tm);
0451         tm = timespec64_sub(tm, tmr->last_update);
0452         cur_time.tv_nsec += tm.tv_nsec;
0453         cur_time.tv_sec += tm.tv_sec;
0454         snd_seq_sanity_real_time(&cur_time);
0455     }
0456     spin_unlock_irqrestore(&tmr->lock, flags);
0457     return cur_time;    
0458 }
0459 
0460 /* TODO: use interpolation on tick queue (will only be useful for very
0461  high PPQ values) */
0462 snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr)
0463 {
0464     snd_seq_tick_time_t cur_tick;
0465     unsigned long flags;
0466 
0467     spin_lock_irqsave(&tmr->lock, flags);
0468     cur_tick = tmr->tick.cur_tick;
0469     spin_unlock_irqrestore(&tmr->lock, flags);
0470     return cur_tick;
0471 }
0472 
0473 
0474 #ifdef CONFIG_SND_PROC_FS
0475 /* exported to seq_info.c */
0476 void snd_seq_info_timer_read(struct snd_info_entry *entry,
0477                  struct snd_info_buffer *buffer)
0478 {
0479     int idx;
0480     struct snd_seq_queue *q;
0481     struct snd_seq_timer *tmr;
0482     struct snd_timer_instance *ti;
0483     unsigned long resolution;
0484     
0485     for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) {
0486         q = queueptr(idx);
0487         if (q == NULL)
0488             continue;
0489         mutex_lock(&q->timer_mutex);
0490         tmr = q->timer;
0491         if (!tmr)
0492             goto unlock;
0493         ti = tmr->timeri;
0494         if (!ti)
0495             goto unlock;
0496         snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name);
0497         resolution = snd_timer_resolution(ti) * tmr->ticks;
0498         snd_iprintf(buffer, "  Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000);
0499         snd_iprintf(buffer, "  Skew : %u / %u\n", tmr->skew, tmr->skew_base);
0500 unlock:
0501         mutex_unlock(&q->timer_mutex);
0502         queuefree(q);
0503     }
0504 }
0505 #endif /* CONFIG_SND_PROC_FS */
0506