2019-05-27 14:55:05 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Timers abstract layer
|
2007-10-15 15:50:19 +08:00
|
|
|
* Copyright (c) by Jaroslav Kysela <perex@perex.cz>
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/time.h>
|
2006-01-16 23:29:08 +08:00
|
|
|
#include <linux/mutex.h>
|
2012-01-23 00:23:42 +08:00
|
|
|
#include <linux/device.h>
|
2011-07-16 01:13:37 +08:00
|
|
|
#include <linux/module.h>
|
2005-06-23 15:09:02 +08:00
|
|
|
#include <linux/string.h>
|
2017-02-03 02:15:33 +08:00
|
|
|
#include <linux/sched/signal.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <sound/core.h>
|
|
|
|
#include <sound/timer.h>
|
|
|
|
#include <sound/control.h>
|
|
|
|
#include <sound/info.h>
|
|
|
|
#include <sound/minors.h>
|
|
|
|
#include <sound/initval.h>
|
|
|
|
#include <linux/kmod.h>
|
|
|
|
|
2016-09-07 21:45:31 +08:00
|
|
|
/* internal flags */
|
|
|
|
#define SNDRV_TIMER_IFLG_PAUSED 0x00010000
|
2019-03-28 00:02:40 +08:00
|
|
|
#define SNDRV_TIMER_IFLG_DEAD 0x00020000
|
2016-09-07 21:45:31 +08:00
|
|
|
|
2014-02-10 16:48:47 +08:00
|
|
|
#if IS_ENABLED(CONFIG_SND_HRTIMER)
|
2010-11-18 16:53:54 +08:00
|
|
|
#define DEFAULT_TIMER_LIMIT 4
|
2005-04-17 06:20:36 +08:00
|
|
|
#else
|
|
|
|
#define DEFAULT_TIMER_LIMIT 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int timer_limit = DEFAULT_TIMER_LIMIT;
|
2007-12-13 17:19:42 +08:00
|
|
|
static int timer_tstamp_monotonic = 1;
|
2007-10-15 15:50:19 +08:00
|
|
|
MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
|
2005-04-17 06:20:36 +08:00
|
|
|
MODULE_DESCRIPTION("ALSA timer interface");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
module_param(timer_limit, int, 0444);
|
|
|
|
MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
|
2007-12-13 17:19:42 +08:00
|
|
|
module_param(timer_tstamp_monotonic, int, 0444);
|
|
|
|
MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-11-24 00:43:19 +08:00
|
|
|
MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
|
|
|
|
MODULE_ALIAS("devname:snd/timer");
|
|
|
|
|
2018-04-24 20:06:13 +08:00
|
|
|
enum timer_tread_format {
|
|
|
|
TREAD_FORMAT_NONE = 0,
|
|
|
|
TREAD_FORMAT_TIME64,
|
|
|
|
TREAD_FORMAT_TIME32,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct snd_timer_tread32 {
|
|
|
|
int event;
|
|
|
|
s32 tstamp_sec;
|
|
|
|
s32 tstamp_nsec;
|
|
|
|
unsigned int val;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct snd_timer_tread64 {
|
|
|
|
int event;
|
|
|
|
u8 pad1[4];
|
|
|
|
s64 tstamp_sec;
|
|
|
|
s64 tstamp_nsec;
|
|
|
|
unsigned int val;
|
|
|
|
u8 pad2[4];
|
|
|
|
};
|
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_user {
|
|
|
|
struct snd_timer_instance *timeri;
|
2005-10-12 23:20:21 +08:00
|
|
|
int tread; /* enhanced read with timestamps and events */
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long ticks;
|
|
|
|
unsigned long overrun;
|
|
|
|
int qhead;
|
|
|
|
int qtail;
|
|
|
|
int qused;
|
|
|
|
int queue_size;
|
2016-01-22 00:19:31 +08:00
|
|
|
bool disconnected;
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_read *queue;
|
2018-04-24 20:06:13 +08:00
|
|
|
struct snd_timer_tread64 *tqueue;
|
2005-04-17 06:20:36 +08:00
|
|
|
spinlock_t qlock;
|
|
|
|
unsigned long last_resolution;
|
|
|
|
unsigned int filter;
|
2018-04-24 20:06:08 +08:00
|
|
|
struct timespec64 tstamp; /* trigger tstamp */
|
2005-04-17 06:20:36 +08:00
|
|
|
wait_queue_head_t qchange_sleep;
|
2022-07-28 20:59:43 +08:00
|
|
|
struct snd_fasync *fasync;
|
2016-01-14 00:48:01 +08:00
|
|
|
struct mutex ioctl_lock;
|
2005-11-17 20:56:05 +08:00
|
|
|
};
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-04-24 20:06:09 +08:00
|
|
|
struct snd_timer_status32 {
|
|
|
|
s32 tstamp_sec; /* Timestamp - last update */
|
|
|
|
s32 tstamp_nsec;
|
|
|
|
unsigned int resolution; /* current period resolution in ns */
|
|
|
|
unsigned int lost; /* counter of master tick lost */
|
|
|
|
unsigned int overrun; /* count of read queue overruns */
|
|
|
|
unsigned int queue; /* used queue size */
|
|
|
|
unsigned char reserved[64]; /* reserved */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SNDRV_TIMER_IOCTL_STATUS32 _IOR('T', 0x14, struct snd_timer_status32)
|
|
|
|
|
|
|
|
struct snd_timer_status64 {
|
|
|
|
s64 tstamp_sec; /* Timestamp - last update */
|
|
|
|
s64 tstamp_nsec;
|
|
|
|
unsigned int resolution; /* current period resolution in ns */
|
|
|
|
unsigned int lost; /* counter of master tick lost */
|
|
|
|
unsigned int overrun; /* count of read queue overruns */
|
|
|
|
unsigned int queue; /* used queue size */
|
|
|
|
unsigned char reserved[64]; /* reserved */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SNDRV_TIMER_IOCTL_STATUS64 _IOR('T', 0x14, struct snd_timer_status64)
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* list of timers */
|
|
|
|
static LIST_HEAD(snd_timer_list);
|
|
|
|
|
|
|
|
/* list of slave instances */
|
|
|
|
static LIST_HEAD(snd_timer_slave_list);
|
|
|
|
|
|
|
|
/* lock for slave active lists */
|
|
|
|
static DEFINE_SPINLOCK(slave_active_lock);
|
|
|
|
|
2019-11-06 23:42:57 +08:00
|
|
|
#define MAX_SLAVE_INSTANCES 1000
|
|
|
|
static int num_slaves;
|
|
|
|
|
2006-01-16 23:29:08 +08:00
|
|
|
static DEFINE_MUTEX(register_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
static int snd_timer_free(struct snd_timer *timer);
|
|
|
|
static int snd_timer_dev_free(struct snd_device *device);
|
|
|
|
static int snd_timer_dev_register(struct snd_device *device);
|
2006-06-23 20:38:23 +08:00
|
|
|
static int snd_timer_dev_disconnect(struct snd_device *device);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* create a timer instance with the given owner string.
|
|
|
|
*/
|
2019-11-08 03:20:08 +08:00
|
|
|
struct snd_timer_instance *snd_timer_instance_new(const char *owner)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_instance *timeri;
|
2019-11-08 03:20:08 +08:00
|
|
|
|
2005-09-09 20:20:23 +08:00
|
|
|
timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (timeri == NULL)
|
|
|
|
return NULL;
|
2005-06-23 15:09:02 +08:00
|
|
|
timeri->owner = kstrdup(owner, GFP_KERNEL);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (! timeri->owner) {
|
|
|
|
kfree(timeri);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
INIT_LIST_HEAD(&timeri->open_list);
|
|
|
|
INIT_LIST_HEAD(&timeri->active_list);
|
|
|
|
INIT_LIST_HEAD(&timeri->ack_list);
|
|
|
|
INIT_LIST_HEAD(&timeri->slave_list_head);
|
|
|
|
INIT_LIST_HEAD(&timeri->slave_active_head);
|
|
|
|
|
2019-11-08 03:20:08 +08:00
|
|
|
return timeri;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(snd_timer_instance_new);
|
|
|
|
|
|
|
|
void snd_timer_instance_free(struct snd_timer_instance *timeri)
|
|
|
|
{
|
|
|
|
if (timeri) {
|
|
|
|
if (timeri->private_free)
|
|
|
|
timeri->private_free(timeri);
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree(timeri->owner);
|
|
|
|
kfree(timeri);
|
|
|
|
}
|
|
|
|
}
|
2019-11-08 03:20:08 +08:00
|
|
|
EXPORT_SYMBOL(snd_timer_instance_free);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* find a timer instance from the given timer id
|
|
|
|
*/
|
2005-11-17 20:56:05 +08:00
|
|
|
static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2020-09-03 05:21:19 +08:00
|
|
|
struct snd_timer *timer;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-10-05 22:02:22 +08:00
|
|
|
list_for_each_entry(timer, &snd_timer_list, device_list) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (timer->tmr_class != tid->dev_class)
|
|
|
|
continue;
|
|
|
|
if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
|
|
|
|
timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
|
|
|
|
(timer->card == NULL ||
|
|
|
|
timer->card->number != tid->card))
|
|
|
|
continue;
|
|
|
|
if (timer->tmr_device != tid->device)
|
|
|
|
continue;
|
|
|
|
if (timer->tmr_subdevice != tid->subdevice)
|
|
|
|
continue;
|
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-07-09 16:28:41 +08:00
|
|
|
#ifdef CONFIG_MODULES
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
static void snd_timer_request(struct snd_timer_id *tid)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
switch (tid->dev_class) {
|
|
|
|
case SNDRV_TIMER_CLASS_GLOBAL:
|
|
|
|
if (tid->device < timer_limit)
|
|
|
|
request_module("snd-timer-%i", tid->device);
|
|
|
|
break;
|
|
|
|
case SNDRV_TIMER_CLASS_CARD:
|
|
|
|
case SNDRV_TIMER_CLASS_PCM:
|
|
|
|
if (tid->card < snd_ecards_limit)
|
|
|
|
request_module("snd-card-%i", tid->card);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-11-08 03:20:06 +08:00
|
|
|
/* move the slave if it belongs to the master; return 1 if match */
|
|
|
|
static int check_matching_master_slave(struct snd_timer_instance *master,
|
|
|
|
struct snd_timer_instance *slave)
|
|
|
|
{
|
|
|
|
if (slave->slave_class != master->slave_class ||
|
|
|
|
slave->slave_id != master->slave_id)
|
|
|
|
return 0;
|
|
|
|
if (master->timer->num_instances >= master->timer->max_instances)
|
|
|
|
return -EBUSY;
|
|
|
|
list_move_tail(&slave->open_list, &master->slave_list_head);
|
|
|
|
master->timer->num_instances++;
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(spinlock_irq)(&slave_active_lock);
|
|
|
|
guard(spinlock)(&master->timer->lock);
|
2019-11-08 03:20:06 +08:00
|
|
|
slave->master = master;
|
|
|
|
slave->timer = master->timer;
|
|
|
|
if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
|
|
|
|
list_add_tail(&slave->active_list, &master->slave_active_head);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* look for a master instance matching with the slave id of the given slave.
|
|
|
|
* when found, relink the open_link of the slave.
|
|
|
|
*
|
|
|
|
* call this with register_mutex down.
|
|
|
|
*/
|
2017-11-05 17:07:43 +08:00
|
|
|
static int snd_timer_check_slave(struct snd_timer_instance *slave)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer *timer;
|
|
|
|
struct snd_timer_instance *master;
|
2019-11-08 03:20:06 +08:00
|
|
|
int err = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* FIXME: it's really dumb to look up all entries.. */
|
2006-10-05 22:02:22 +08:00
|
|
|
list_for_each_entry(timer, &snd_timer_list, device_list) {
|
|
|
|
list_for_each_entry(master, &timer->open_list_head, open_list) {
|
2019-11-08 03:20:06 +08:00
|
|
|
err = check_matching_master_slave(master, slave);
|
|
|
|
if (err != 0) /* match found or error */
|
|
|
|
goto out;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
2019-11-08 03:20:06 +08:00
|
|
|
out:
|
|
|
|
return err < 0 ? err : 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* look for slave instances matching with the slave id of the given master.
|
|
|
|
* when found, relink the open_link of slaves.
|
|
|
|
*
|
|
|
|
* call this with register_mutex down.
|
|
|
|
*/
|
2017-11-05 17:07:43 +08:00
|
|
|
static int snd_timer_check_master(struct snd_timer_instance *master)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-10-05 22:02:22 +08:00
|
|
|
struct snd_timer_instance *slave, *tmp;
|
2019-11-08 03:20:06 +08:00
|
|
|
int err = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* check all pending slaves */
|
2006-10-05 22:02:22 +08:00
|
|
|
list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
|
2019-11-08 03:20:06 +08:00
|
|
|
err = check_matching_master_slave(master, slave);
|
|
|
|
if (err < 0)
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2019-11-08 03:20:06 +08:00
|
|
|
return err < 0 ? err : 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2019-11-08 03:20:07 +08:00
|
|
|
static void snd_timer_close_locked(struct snd_timer_instance *timeri,
|
|
|
|
struct device **card_devp_to_put);
|
2017-11-05 17:07:43 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* open a timer instance
|
|
|
|
* when opening a master, the slave id must be here given.
|
|
|
|
*/
|
2019-11-08 03:20:08 +08:00
|
|
|
int snd_timer_open(struct snd_timer_instance *timeri,
|
|
|
|
struct snd_timer_id *tid,
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned int slave_id)
|
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer *timer;
|
2019-10-31 05:42:57 +08:00
|
|
|
struct device *card_dev_to_put = NULL;
|
2017-11-05 17:07:43 +08:00
|
|
|
int err;
|
2005-10-12 23:20:21 +08:00
|
|
|
|
2019-03-29 00:11:10 +08:00
|
|
|
mutex_lock(®ister_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
|
|
|
|
/* open a slave instance */
|
|
|
|
if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
|
|
|
|
tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
|
2014-02-05 01:22:39 +08:00
|
|
|
pr_debug("ALSA: timer: invalid slave class %i\n",
|
|
|
|
tid->dev_sclass);
|
2019-03-29 00:11:10 +08:00
|
|
|
err = -EINVAL;
|
|
|
|
goto unlock;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2019-11-06 23:42:57 +08:00
|
|
|
if (num_slaves >= MAX_SLAVE_INSTANCES) {
|
|
|
|
err = -EBUSY;
|
|
|
|
goto unlock;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
timeri->slave_class = tid->dev_sclass;
|
|
|
|
timeri->slave_id = tid->device;
|
|
|
|
timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
|
|
|
|
list_add_tail(&timeri->open_list, &snd_timer_slave_list);
|
2019-11-06 23:42:57 +08:00
|
|
|
num_slaves++;
|
2017-11-05 17:07:43 +08:00
|
|
|
err = snd_timer_check_slave(timeri);
|
2019-11-12 01:36:42 +08:00
|
|
|
goto list_added;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* open a master instance */
|
|
|
|
timer = snd_timer_find(tid);
|
2008-07-09 16:28:41 +08:00
|
|
|
#ifdef CONFIG_MODULES
|
|
|
|
if (!timer) {
|
2006-01-16 23:29:08 +08:00
|
|
|
mutex_unlock(®ister_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
snd_timer_request(tid);
|
2006-01-16 23:29:08 +08:00
|
|
|
mutex_lock(®ister_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
timer = snd_timer_find(tid);
|
|
|
|
}
|
|
|
|
#endif
|
2005-10-12 23:10:35 +08:00
|
|
|
if (!timer) {
|
2019-03-29 00:11:10 +08:00
|
|
|
err = -ENODEV;
|
|
|
|
goto unlock;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2005-10-12 23:10:35 +08:00
|
|
|
if (!list_empty(&timer->open_list_head)) {
|
ALSA: timer: Fix incorrectly assigned timer instance
The clean up commit 41672c0c24a6 ("ALSA: timer: Simplify error path in
snd_timer_open()") unified the error handling code paths with the
standard goto, but it introduced a subtle bug: the timer instance is
stored in snd_timer_open() incorrectly even if it returns an error.
This may eventually lead to UAF, as spotted by fuzzer.
The culprit is the snd_timer_open() code checks the
SNDRV_TIMER_IFLG_EXCLUSIVE flag with the common variable timeri.
This variable is supposed to be the newly created instance, but we
(ab-)used it for a temporary check before the actual creation of a
timer instance. After that point, there is another check for the max
number of instances, and it bails out if over the threshold. Before
the refactoring above, it worked fine because the code returned
directly from that point. After the refactoring, however, it jumps to
the unified error path that stores the timeri variable in return --
even if it returns an error. Unfortunately this stored value is kept
in the caller side (snd_timer_user_tselect()) in tu->timeri. This
causes inconsistency later, as if the timer was successfully
assigned.
In this patch, we fix it by not re-using timeri variable but a
temporary variable for testing the exclusive connection, so timeri
remains NULL at that point.
Fixes: 41672c0c24a6 ("ALSA: timer: Simplify error path in snd_timer_open()")
Reported-and-tested-by: Tristan Madani <tristmd@gmail.com>
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20191106165547.23518-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2019-11-07 00:55:47 +08:00
|
|
|
struct snd_timer_instance *t =
|
|
|
|
list_entry(timer->open_list_head.next,
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_instance, open_list);
|
ALSA: timer: Fix incorrectly assigned timer instance
The clean up commit 41672c0c24a6 ("ALSA: timer: Simplify error path in
snd_timer_open()") unified the error handling code paths with the
standard goto, but it introduced a subtle bug: the timer instance is
stored in snd_timer_open() incorrectly even if it returns an error.
This may eventually lead to UAF, as spotted by fuzzer.
The culprit is the snd_timer_open() code checks the
SNDRV_TIMER_IFLG_EXCLUSIVE flag with the common variable timeri.
This variable is supposed to be the newly created instance, but we
(ab-)used it for a temporary check before the actual creation of a
timer instance. After that point, there is another check for the max
number of instances, and it bails out if over the threshold. Before
the refactoring above, it worked fine because the code returned
directly from that point. After the refactoring, however, it jumps to
the unified error path that stores the timeri variable in return --
even if it returns an error. Unfortunately this stored value is kept
in the caller side (snd_timer_user_tselect()) in tu->timeri. This
causes inconsistency later, as if the timer was successfully
assigned.
In this patch, we fix it by not re-using timeri variable but a
temporary variable for testing the exclusive connection, so timeri
remains NULL at that point.
Fixes: 41672c0c24a6 ("ALSA: timer: Simplify error path in snd_timer_open()")
Reported-and-tested-by: Tristan Madani <tristmd@gmail.com>
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20191106165547.23518-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2019-11-07 00:55:47 +08:00
|
|
|
if (t->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
|
2019-03-29 00:11:10 +08:00
|
|
|
err = -EBUSY;
|
|
|
|
goto unlock;
|
2005-10-12 23:10:35 +08:00
|
|
|
}
|
|
|
|
}
|
2017-11-05 17:07:43 +08:00
|
|
|
if (timer->num_instances >= timer->max_instances) {
|
2019-03-29 00:11:10 +08:00
|
|
|
err = -EBUSY;
|
|
|
|
goto unlock;
|
2017-11-05 17:07:43 +08:00
|
|
|
}
|
2019-11-08 03:20:08 +08:00
|
|
|
if (!try_module_get(timer->module)) {
|
|
|
|
err = -EBUSY;
|
2019-03-29 00:11:10 +08:00
|
|
|
goto unlock;
|
2005-10-12 23:10:35 +08:00
|
|
|
}
|
2016-01-22 00:19:31 +08:00
|
|
|
/* take a card refcount for safe disconnection */
|
2019-11-08 03:20:08 +08:00
|
|
|
if (timer->card) {
|
2016-01-22 00:19:31 +08:00
|
|
|
get_device(&timer->card->card_dev);
|
2019-11-08 03:20:08 +08:00
|
|
|
card_dev_to_put = &timer->card->card_dev;
|
|
|
|
}
|
2016-08-29 06:33:51 +08:00
|
|
|
|
|
|
|
if (list_empty(&timer->open_list_head) && timer->hw.open) {
|
2019-03-29 00:11:10 +08:00
|
|
|
err = timer->hw.open(timer);
|
2016-08-29 06:33:51 +08:00
|
|
|
if (err) {
|
|
|
|
module_put(timer->module);
|
2019-03-29 00:11:10 +08:00
|
|
|
goto unlock;
|
2016-08-29 06:33:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 03:20:08 +08:00
|
|
|
timeri->timer = timer;
|
|
|
|
timeri->slave_class = tid->dev_sclass;
|
|
|
|
timeri->slave_id = slave_id;
|
|
|
|
|
2005-10-12 23:10:35 +08:00
|
|
|
list_add_tail(&timeri->open_list, &timer->open_list_head);
|
2017-11-05 17:07:43 +08:00
|
|
|
timer->num_instances++;
|
|
|
|
err = snd_timer_check_master(timeri);
|
2019-11-12 01:36:42 +08:00
|
|
|
list_added:
|
2019-11-08 03:20:08 +08:00
|
|
|
if (err < 0)
|
2019-10-31 05:42:57 +08:00
|
|
|
snd_timer_close_locked(timeri, &card_dev_to_put);
|
2019-03-29 00:11:10 +08:00
|
|
|
|
|
|
|
unlock:
|
2006-01-16 23:29:08 +08:00
|
|
|
mutex_unlock(®ister_mutex);
|
2019-10-31 05:42:57 +08:00
|
|
|
/* put_device() is called after unlock for avoiding deadlock */
|
2019-11-08 03:20:08 +08:00
|
|
|
if (err < 0 && card_dev_to_put)
|
2019-10-31 05:42:57 +08:00
|
|
|
put_device(card_dev_to_put);
|
2017-11-05 17:07:43 +08:00
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2017-06-16 22:16:05 +08:00
|
|
|
EXPORT_SYMBOL(snd_timer_open);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2024-02-27 16:52:45 +08:00
|
|
|
/* remove slave links, called from snd_timer_close_locked() below */
|
|
|
|
static void remove_slave_links(struct snd_timer_instance *timeri,
|
|
|
|
struct snd_timer *timer)
|
|
|
|
{
|
|
|
|
struct snd_timer_instance *slave, *tmp;
|
|
|
|
|
|
|
|
guard(spinlock_irq)(&slave_active_lock);
|
|
|
|
guard(spinlock)(&timer->lock);
|
|
|
|
timeri->timer = NULL;
|
|
|
|
list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head, open_list) {
|
|
|
|
list_move_tail(&slave->open_list, &snd_timer_slave_list);
|
|
|
|
timer->num_instances--;
|
|
|
|
slave->master = NULL;
|
|
|
|
slave->timer = NULL;
|
|
|
|
list_del_init(&slave->ack_list);
|
|
|
|
list_del_init(&slave->active_list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* close a timer instance
|
2017-11-05 17:07:43 +08:00
|
|
|
* call this with register_mutex down.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2019-11-08 03:20:07 +08:00
|
|
|
static void snd_timer_close_locked(struct snd_timer_instance *timeri,
|
|
|
|
struct device **card_devp_to_put)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2019-03-28 00:02:40 +08:00
|
|
|
struct snd_timer *timer = timeri->timer;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-03-28 00:02:40 +08:00
|
|
|
if (timer) {
|
2024-03-15 18:14:42 +08:00
|
|
|
guard(spinlock_irq)(&timer->lock);
|
2019-03-28 00:02:40 +08:00
|
|
|
timeri->flags |= SNDRV_TIMER_IFLG_DEAD;
|
|
|
|
}
|
|
|
|
|
2019-11-08 03:20:08 +08:00
|
|
|
if (!list_empty(&timeri->open_list)) {
|
|
|
|
list_del_init(&timeri->open_list);
|
|
|
|
if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
|
|
|
|
num_slaves--;
|
|
|
|
}
|
2016-02-10 18:53:30 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* force to stop the timer */
|
|
|
|
snd_timer_stop(timeri);
|
|
|
|
|
2016-02-10 18:53:30 +08:00
|
|
|
if (timer) {
|
2017-11-05 17:07:43 +08:00
|
|
|
timer->num_instances--;
|
2005-04-17 06:20:36 +08:00
|
|
|
/* wait, until the active callback is finished */
|
|
|
|
spin_lock_irq(&timer->lock);
|
|
|
|
while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
|
|
|
|
spin_unlock_irq(&timer->lock);
|
|
|
|
udelay(10);
|
|
|
|
spin_lock_irq(&timer->lock);
|
|
|
|
}
|
|
|
|
spin_unlock_irq(&timer->lock);
|
2016-02-10 18:53:30 +08:00
|
|
|
|
2024-02-27 16:52:45 +08:00
|
|
|
remove_slave_links(timeri, timer);
|
2016-02-10 18:53:30 +08:00
|
|
|
|
|
|
|
/* slave doesn't need to release timer resources below */
|
|
|
|
if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
|
|
|
|
timer = NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2016-02-10 18:53:30 +08:00
|
|
|
|
|
|
|
if (timer) {
|
|
|
|
if (list_empty(&timer->open_list_head) && timer->hw.close)
|
|
|
|
timer->hw.close(timer);
|
|
|
|
/* release a card refcount for safe disconnection */
|
|
|
|
if (timer->card)
|
2019-10-31 05:42:57 +08:00
|
|
|
*card_devp_to_put = &timer->card->card_dev;
|
2005-10-12 23:12:31 +08:00
|
|
|
module_put(timer->module);
|
2016-02-10 18:53:30 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2017-11-05 17:07:43 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* close a timer instance
|
|
|
|
*/
|
2019-11-08 03:20:07 +08:00
|
|
|
void snd_timer_close(struct snd_timer_instance *timeri)
|
2017-11-05 17:07:43 +08:00
|
|
|
{
|
2019-10-31 05:42:57 +08:00
|
|
|
struct device *card_dev_to_put = NULL;
|
2017-11-05 17:07:43 +08:00
|
|
|
|
|
|
|
if (snd_BUG_ON(!timeri))
|
2019-11-08 03:20:07 +08:00
|
|
|
return;
|
2017-11-05 17:07:43 +08:00
|
|
|
|
2024-02-27 16:52:45 +08:00
|
|
|
scoped_guard(mutex, ®ister_mutex)
|
|
|
|
snd_timer_close_locked(timeri, &card_dev_to_put);
|
2019-10-31 05:42:57 +08:00
|
|
|
/* put_device() is called after unlock for avoiding deadlock */
|
|
|
|
if (card_dev_to_put)
|
|
|
|
put_device(card_dev_to_put);
|
2017-11-05 17:07:43 +08:00
|
|
|
}
|
2017-06-16 22:16:05 +08:00
|
|
|
EXPORT_SYMBOL(snd_timer_close);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-05-17 05:45:33 +08:00
|
|
|
static unsigned long snd_timer_hw_resolution(struct snd_timer *timer)
|
|
|
|
{
|
|
|
|
if (timer->hw.c_resolution)
|
|
|
|
return timer->hw.c_resolution(timer);
|
|
|
|
else
|
|
|
|
return timer->hw.resolution;
|
|
|
|
}
|
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer * timer;
|
2018-05-17 05:52:42 +08:00
|
|
|
unsigned long ret = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (timeri == NULL)
|
|
|
|
return 0;
|
2017-08-23 15:45:06 +08:00
|
|
|
timer = timeri->timer;
|
2018-05-17 05:52:42 +08:00
|
|
|
if (timer) {
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(spinlock_irqsave)(&timer->lock);
|
2018-05-17 05:52:42 +08:00
|
|
|
ret = snd_timer_hw_resolution(timer);
|
|
|
|
}
|
|
|
|
return ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2017-06-16 22:16:05 +08:00
|
|
|
EXPORT_SYMBOL(snd_timer_resolution);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2018-05-17 05:52:42 +08:00
|
|
|
struct snd_timer *timer = ti->timer;
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long resolution = 0;
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_instance *ts;
|
2018-04-24 20:06:08 +08:00
|
|
|
struct timespec64 tstamp;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-12-13 17:19:42 +08:00
|
|
|
if (timer_tstamp_monotonic)
|
2018-04-24 20:06:08 +08:00
|
|
|
ktime_get_ts64(&tstamp);
|
2007-12-13 17:19:42 +08:00
|
|
|
else
|
2018-04-24 20:06:08 +08:00
|
|
|
ktime_get_real_ts64(&tstamp);
|
2008-08-08 23:09:09 +08:00
|
|
|
if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
|
|
|
|
event > SNDRV_TIMER_EVENT_PAUSE))
|
|
|
|
return;
|
2018-05-17 05:52:42 +08:00
|
|
|
if (timer &&
|
|
|
|
(event == SNDRV_TIMER_EVENT_START ||
|
|
|
|
event == SNDRV_TIMER_EVENT_CONTINUE))
|
|
|
|
resolution = snd_timer_hw_resolution(timer);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ti->ccallback)
|
2010-03-03 18:05:55 +08:00
|
|
|
ti->ccallback(ti, event, &tstamp, resolution);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
|
|
|
|
return;
|
|
|
|
if (timer == NULL)
|
|
|
|
return;
|
|
|
|
if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
|
|
|
|
return;
|
2021-06-02 19:38:23 +08:00
|
|
|
event += 10; /* convert to SNDRV_TIMER_EVENT_MXXX */
|
2006-10-05 22:02:22 +08:00
|
|
|
list_for_each_entry(ts, &ti->slave_active_head, active_list)
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ts->ccallback)
|
2021-06-02 19:38:23 +08:00
|
|
|
ts->ccallback(ts, event, &tstamp, resolution);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2016-02-10 19:47:03 +08:00
|
|
|
/* start/continue a master timer */
|
|
|
|
static int snd_timer_start1(struct snd_timer_instance *timeri,
|
|
|
|
bool start, unsigned long ticks)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2016-02-10 19:47:03 +08:00
|
|
|
struct snd_timer *timer;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
timer = timeri->timer;
|
|
|
|
if (!timer)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(spinlock_irqsave)(&timer->lock);
|
|
|
|
if (timeri->flags & SNDRV_TIMER_IFLG_DEAD)
|
|
|
|
return -EINVAL;
|
|
|
|
if (timer->card && timer->card->shutdown)
|
|
|
|
return -ENODEV;
|
2016-02-10 19:47:03 +08:00
|
|
|
if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
|
2024-02-27 16:52:45 +08:00
|
|
|
SNDRV_TIMER_IFLG_START))
|
|
|
|
return -EBUSY;
|
2016-02-10 19:47:03 +08:00
|
|
|
|
2024-05-15 02:27:36 +08:00
|
|
|
/* check the actual time for the start tick;
|
|
|
|
* bail out as error if it's way too low (< 100us)
|
|
|
|
*/
|
|
|
|
if (start) {
|
|
|
|
if ((u64)snd_timer_hw_resolution(timer) * ticks < 100000)
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-02-10 19:47:03 +08:00
|
|
|
if (start)
|
|
|
|
timeri->ticks = timeri->cticks = ticks;
|
|
|
|
else if (!timeri->cticks)
|
|
|
|
timeri->cticks = 1;
|
|
|
|
timeri->pticks = 0;
|
|
|
|
|
2011-03-17 00:10:11 +08:00
|
|
|
list_move_tail(&timeri->active_list, &timer->active_list_head);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (timer->running) {
|
|
|
|
if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
|
|
|
|
goto __start_now;
|
|
|
|
timer->flags |= SNDRV_TIMER_FLG_RESCHED;
|
|
|
|
timeri->flags |= SNDRV_TIMER_IFLG_START;
|
2016-02-10 19:47:03 +08:00
|
|
|
result = 1; /* delayed start */
|
2005-04-17 06:20:36 +08:00
|
|
|
} else {
|
2016-02-10 19:47:03 +08:00
|
|
|
if (start)
|
|
|
|
timer->sticks = ticks;
|
2005-04-17 06:20:36 +08:00
|
|
|
timer->hw.start(timer);
|
|
|
|
__start_now:
|
|
|
|
timer->running++;
|
|
|
|
timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
|
2016-02-10 19:47:03 +08:00
|
|
|
result = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2016-02-10 19:47:03 +08:00
|
|
|
snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
|
|
|
|
SNDRV_TIMER_EVENT_CONTINUE);
|
|
|
|
return result;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2016-02-10 19:47:03 +08:00
|
|
|
/* start/continue a slave timer */
|
|
|
|
static int snd_timer_start_slave(struct snd_timer_instance *timeri,
|
|
|
|
bool start)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(spinlock_irqsave)(&slave_active_lock);
|
|
|
|
if (timeri->flags & SNDRV_TIMER_IFLG_DEAD)
|
|
|
|
return -EINVAL;
|
|
|
|
if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING)
|
|
|
|
return -EBUSY;
|
2005-04-17 06:20:36 +08:00
|
|
|
timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
|
ALSA: timer: Harden slave timer list handling
A slave timer instance might be still accessible in a racy way while
operating the master instance as it lacks of locking. Since the
master operation is mostly protected with timer->lock, we should cope
with it while changing the slave instance, too. Also, some linked
lists (active_list and ack_list) of slave instances aren't unlinked
immediately at stopping or closing, and this may lead to unexpected
accesses.
This patch tries to address these issues. It adds spin lock of
timer->lock (either from master or slave, which is equivalent) in a
few places. For avoiding a deadlock, we ensure that the global
slave_active_lock is always locked at first before each timer lock.
Also, ack and active_list of slave instances are properly unlinked at
snd_timer_stop() and snd_timer_close().
Last but not least, remove the superfluous call of _snd_timer_stop()
at removing slave links. This is a noop, and calling it may confuse
readers wrt locking. Further cleanup will follow in a later patch.
Actually we've got reports of use-after-free by syzkaller fuzzer, and
this hopefully fixes these issues.
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2016-01-14 23:30:58 +08:00
|
|
|
if (timeri->master && timeri->timer) {
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(spinlock)(&timeri->timer->lock);
|
2005-10-12 23:20:21 +08:00
|
|
|
list_add_tail(&timeri->active_list,
|
|
|
|
&timeri->master->slave_active_head);
|
2016-02-10 19:47:03 +08:00
|
|
|
snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START :
|
|
|
|
SNDRV_TIMER_EVENT_CONTINUE);
|
ALSA: timer: Harden slave timer list handling
A slave timer instance might be still accessible in a racy way while
operating the master instance as it lacks of locking. Since the
master operation is mostly protected with timer->lock, we should cope
with it while changing the slave instance, too. Also, some linked
lists (active_list and ack_list) of slave instances aren't unlinked
immediately at stopping or closing, and this may lead to unexpected
accesses.
This patch tries to address these issues. It adds spin lock of
timer->lock (either from master or slave, which is equivalent) in a
few places. For avoiding a deadlock, we ensure that the global
slave_active_lock is always locked at first before each timer lock.
Also, ack and active_list of slave instances are properly unlinked at
snd_timer_stop() and snd_timer_close().
Last but not least, remove the superfluous call of _snd_timer_stop()
at removing slave links. This is a noop, and calling it may confuse
readers wrt locking. Further cleanup will follow in a later patch.
Actually we've got reports of use-after-free by syzkaller fuzzer, and
this hopefully fixes these issues.
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2016-01-14 23:30:58 +08:00
|
|
|
}
|
2024-02-27 16:52:45 +08:00
|
|
|
return 1; /* delayed start */
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2016-02-10 19:47:03 +08:00
|
|
|
/* stop/pause a master timer */
|
|
|
|
static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer *timer;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
timer = timeri->timer;
|
|
|
|
if (!timer)
|
|
|
|
return -EINVAL;
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(spinlock_irqsave)(&timer->lock);
|
2021-11-03 11:35:17 +08:00
|
|
|
list_del_init(&timeri->ack_list);
|
|
|
|
list_del_init(&timeri->active_list);
|
2016-01-31 06:09:08 +08:00
|
|
|
if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
|
2024-02-27 16:52:45 +08:00
|
|
|
SNDRV_TIMER_IFLG_START)))
|
|
|
|
return -EBUSY;
|
2016-02-10 19:47:03 +08:00
|
|
|
if (timer->card && timer->card->shutdown)
|
2024-02-27 16:52:45 +08:00
|
|
|
return 0;
|
2016-02-10 19:47:03 +08:00
|
|
|
if (stop) {
|
|
|
|
timeri->cticks = timeri->ticks;
|
|
|
|
timeri->pticks = 0;
|
2016-01-22 00:19:31 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
|
|
|
|
!(--timer->running)) {
|
|
|
|
timer->hw.stop(timer);
|
|
|
|
if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
|
|
|
|
timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
|
|
|
|
snd_timer_reschedule(timer, 0);
|
|
|
|
if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
|
|
|
|
timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
|
|
|
|
timer->hw.start(timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-15 00:01:46 +08:00
|
|
|
timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
|
2016-09-07 21:45:31 +08:00
|
|
|
if (stop)
|
|
|
|
timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED;
|
|
|
|
else
|
|
|
|
timeri->flags |= SNDRV_TIMER_IFLG_PAUSED;
|
2016-02-10 19:47:03 +08:00
|
|
|
snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
|
2018-05-18 05:34:39 +08:00
|
|
|
SNDRV_TIMER_EVENT_PAUSE);
|
2024-02-27 16:52:45 +08:00
|
|
|
return 0;
|
2016-02-10 19:47:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* stop/pause a slave timer */
|
|
|
|
static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop)
|
|
|
|
{
|
2021-11-05 17:15:17 +08:00
|
|
|
bool running;
|
2016-02-10 19:47:03 +08:00
|
|
|
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(spinlock_irqsave)(&slave_active_lock);
|
2021-11-05 17:15:17 +08:00
|
|
|
running = timeri->flags & SNDRV_TIMER_IFLG_RUNNING;
|
2016-02-10 19:47:03 +08:00
|
|
|
timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
|
|
|
|
if (timeri->timer) {
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(spinlock)(&timeri->timer->lock);
|
2016-02-10 19:47:03 +08:00
|
|
|
list_del_init(&timeri->ack_list);
|
|
|
|
list_del_init(&timeri->active_list);
|
2021-11-05 17:15:17 +08:00
|
|
|
if (running)
|
|
|
|
snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP :
|
|
|
|
SNDRV_TIMER_EVENT_PAUSE);
|
2016-02-10 19:47:03 +08:00
|
|
|
}
|
2021-11-05 17:15:17 +08:00
|
|
|
return running ? 0 : -EBUSY;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2016-02-10 19:47:03 +08:00
|
|
|
/*
|
|
|
|
* start the timer instance
|
|
|
|
*/
|
|
|
|
int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
|
|
|
|
{
|
|
|
|
if (timeri == NULL || ticks < 1)
|
|
|
|
return -EINVAL;
|
|
|
|
if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
|
|
|
|
return snd_timer_start_slave(timeri, true);
|
|
|
|
else
|
|
|
|
return snd_timer_start1(timeri, true, ticks);
|
|
|
|
}
|
2017-06-16 22:16:05 +08:00
|
|
|
EXPORT_SYMBOL(snd_timer_start);
|
2016-02-10 19:47:03 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* stop the timer instance.
|
|
|
|
*
|
|
|
|
* do not call this from the timer callback!
|
|
|
|
*/
|
2005-11-17 20:56:05 +08:00
|
|
|
int snd_timer_stop(struct snd_timer_instance *timeri)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2016-02-10 19:47:03 +08:00
|
|
|
if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
|
|
|
|
return snd_timer_stop_slave(timeri, true);
|
|
|
|
else
|
|
|
|
return snd_timer_stop1(timeri, true);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2017-06-16 22:16:05 +08:00
|
|
|
EXPORT_SYMBOL(snd_timer_stop);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* start again.. the tick is kept.
|
|
|
|
*/
|
2005-11-17 20:56:05 +08:00
|
|
|
int snd_timer_continue(struct snd_timer_instance *timeri)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2016-09-07 21:45:31 +08:00
|
|
|
/* timer can continue only after pause */
|
|
|
|
if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
|
2016-02-10 19:47:03 +08:00
|
|
|
return snd_timer_start_slave(timeri, false);
|
|
|
|
else
|
|
|
|
return snd_timer_start1(timeri, false, 0);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2017-06-16 22:16:05 +08:00
|
|
|
EXPORT_SYMBOL(snd_timer_continue);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* pause.. remember the ticks left
|
|
|
|
*/
|
2005-11-17 20:56:05 +08:00
|
|
|
int snd_timer_pause(struct snd_timer_instance * timeri)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2016-02-10 19:47:03 +08:00
|
|
|
if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
|
|
|
|
return snd_timer_stop_slave(timeri, false);
|
|
|
|
else
|
|
|
|
return snd_timer_stop1(timeri, false);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2017-06-16 22:16:05 +08:00
|
|
|
EXPORT_SYMBOL(snd_timer_pause);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* reschedule the timer
|
|
|
|
*
|
|
|
|
* start pending instances and check the scheduling ticks.
|
|
|
|
* when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
|
|
|
|
*/
|
2005-11-17 20:56:05 +08:00
|
|
|
static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_instance *ti;
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long ticks = ~0UL;
|
|
|
|
|
2006-10-05 22:02:22 +08:00
|
|
|
list_for_each_entry(ti, &timer->active_list_head, active_list) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ti->flags & SNDRV_TIMER_IFLG_START) {
|
|
|
|
ti->flags &= ~SNDRV_TIMER_IFLG_START;
|
|
|
|
ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
|
|
|
|
timer->running++;
|
|
|
|
}
|
|
|
|
if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
|
|
|
|
if (ticks > ti->cticks)
|
|
|
|
ticks = ti->cticks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ticks == ~0UL) {
|
|
|
|
timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ticks > timer->hw.ticks)
|
|
|
|
ticks = timer->hw.ticks;
|
|
|
|
if (ticks_left != ticks)
|
|
|
|
timer->flags |= SNDRV_TIMER_FLG_CHANGE;
|
|
|
|
timer->sticks = ticks;
|
|
|
|
}
|
|
|
|
|
2019-03-27 23:42:51 +08:00
|
|
|
/* call callbacks in timer ack list */
|
|
|
|
static void snd_timer_process_callbacks(struct snd_timer *timer,
|
|
|
|
struct list_head *head)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_instance *ti;
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long resolution, ticks;
|
2016-01-22 00:19:31 +08:00
|
|
|
|
2019-03-27 23:42:51 +08:00
|
|
|
while (!list_empty(head)) {
|
|
|
|
ti = list_first_entry(head, struct snd_timer_instance,
|
|
|
|
ack_list);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-04-09 18:25:32 +08:00
|
|
|
/* remove from ack_list and make empty */
|
|
|
|
list_del_init(&ti->ack_list);
|
|
|
|
|
2019-03-28 00:02:40 +08:00
|
|
|
if (!(ti->flags & SNDRV_TIMER_IFLG_DEAD)) {
|
|
|
|
ticks = ti->pticks;
|
|
|
|
ti->pticks = 0;
|
|
|
|
resolution = ti->resolution;
|
2019-04-09 18:25:32 +08:00
|
|
|
ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
|
2019-03-28 00:02:40 +08:00
|
|
|
spin_unlock(&timer->lock);
|
|
|
|
if (ti->callback)
|
|
|
|
ti->callback(ti, resolution, ticks);
|
|
|
|
spin_lock(&timer->lock);
|
2019-04-09 18:25:32 +08:00
|
|
|
ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
|
2019-03-28 00:02:40 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2019-03-27 23:42:51 +08:00
|
|
|
}
|
|
|
|
|
2019-03-27 23:51:58 +08:00
|
|
|
/* clear pending instances from ack list */
|
|
|
|
static void snd_timer_clear_callbacks(struct snd_timer *timer,
|
|
|
|
struct list_head *head)
|
|
|
|
{
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(spinlock_irqsave)(&timer->lock);
|
2019-03-27 23:51:58 +08:00
|
|
|
while (!list_empty(head))
|
|
|
|
list_del_init(head->next);
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2020-09-03 18:41:22 +08:00
|
|
|
* timer work
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
*/
|
2020-09-03 18:41:22 +08:00
|
|
|
static void snd_timer_work(struct work_struct *work)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2020-09-03 18:41:22 +08:00
|
|
|
struct snd_timer *timer = container_of(work, struct snd_timer, task_work);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-03-27 23:51:58 +08:00
|
|
|
if (timer->card && timer->card->shutdown) {
|
|
|
|
snd_timer_clear_callbacks(timer, &timer->sack_list_head);
|
2016-01-22 00:19:31 +08:00
|
|
|
return;
|
2019-03-27 23:51:58 +08:00
|
|
|
}
|
2016-01-22 00:19:31 +08:00
|
|
|
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(spinlock_irqsave)(&timer->lock);
|
2019-03-27 23:42:51 +08:00
|
|
|
snd_timer_process_callbacks(timer, &timer->sack_list_head);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* timer interrupt
|
|
|
|
*
|
|
|
|
* ticks_left is usually equal to timer->sticks.
|
|
|
|
*
|
|
|
|
*/
|
2005-11-17 20:56:05 +08:00
|
|
|
void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-10-05 22:02:22 +08:00
|
|
|
struct snd_timer_instance *ti, *ts, *tmp;
|
2019-03-27 23:42:51 +08:00
|
|
|
unsigned long resolution;
|
|
|
|
struct list_head *ack_list_head;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (timer == NULL)
|
|
|
|
return;
|
|
|
|
|
2019-03-27 23:51:58 +08:00
|
|
|
if (timer->card && timer->card->shutdown) {
|
|
|
|
snd_timer_clear_callbacks(timer, &timer->ack_list_head);
|
2016-01-22 00:19:31 +08:00
|
|
|
return;
|
2019-03-27 23:51:58 +08:00
|
|
|
}
|
2016-01-22 00:19:31 +08:00
|
|
|
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(spinlock_irqsave)(&timer->lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* remember the current resolution */
|
2018-05-17 05:45:33 +08:00
|
|
|
resolution = snd_timer_hw_resolution(timer);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* loop for all active instances
|
2006-10-05 22:02:22 +08:00
|
|
|
* Here we cannot use list_for_each_entry because the active_list of a
|
2005-10-12 23:20:21 +08:00
|
|
|
* processed instance is relinked to done_list_head before the callback
|
|
|
|
* is called.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2006-10-05 22:02:22 +08:00
|
|
|
list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
|
|
|
|
active_list) {
|
2019-03-28 00:02:40 +08:00
|
|
|
if (ti->flags & SNDRV_TIMER_IFLG_DEAD)
|
|
|
|
continue;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
|
|
|
|
continue;
|
|
|
|
ti->pticks += ticks_left;
|
|
|
|
ti->resolution = resolution;
|
|
|
|
if (ti->cticks < ticks_left)
|
|
|
|
ti->cticks = 0;
|
|
|
|
else
|
|
|
|
ti->cticks -= ticks_left;
|
|
|
|
if (ti->cticks) /* not expired */
|
|
|
|
continue;
|
|
|
|
if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
|
|
|
|
ti->cticks = ti->ticks;
|
|
|
|
} else {
|
|
|
|
ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
|
2016-02-05 00:06:13 +08:00
|
|
|
--timer->running;
|
|
|
|
list_del_init(&ti->active_list);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2020-09-03 18:41:22 +08:00
|
|
|
if ((timer->hw.flags & SNDRV_TIMER_HW_WORK) ||
|
2005-10-12 23:20:21 +08:00
|
|
|
(ti->flags & SNDRV_TIMER_IFLG_FAST))
|
|
|
|
ack_list_head = &timer->ack_list_head;
|
|
|
|
else
|
|
|
|
ack_list_head = &timer->sack_list_head;
|
|
|
|
if (list_empty(&ti->ack_list))
|
|
|
|
list_add_tail(&ti->ack_list, ack_list_head);
|
2006-10-05 22:02:22 +08:00
|
|
|
list_for_each_entry(ts, &ti->slave_active_head, active_list) {
|
2005-04-17 06:20:36 +08:00
|
|
|
ts->pticks = ti->pticks;
|
|
|
|
ts->resolution = resolution;
|
2005-10-12 23:20:21 +08:00
|
|
|
if (list_empty(&ts->ack_list))
|
|
|
|
list_add_tail(&ts->ack_list, ack_list_head);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
|
2006-07-17 22:53:57 +08:00
|
|
|
snd_timer_reschedule(timer, timer->sticks);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (timer->running) {
|
|
|
|
if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
|
|
|
|
timer->hw.stop(timer);
|
|
|
|
timer->flags |= SNDRV_TIMER_FLG_CHANGE;
|
|
|
|
}
|
|
|
|
if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
|
|
|
|
(timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
|
|
|
|
/* restart timer */
|
|
|
|
timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
|
|
|
|
timer->hw.start(timer);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
timer->hw.stop(timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now process all fast callbacks */
|
2019-03-27 23:42:51 +08:00
|
|
|
snd_timer_process_callbacks(timer, &timer->ack_list_head);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* do we have any slow callbacks? */
|
2024-02-27 16:52:45 +08:00
|
|
|
if (!list_empty(&timer->sack_list_head))
|
2020-09-03 18:41:22 +08:00
|
|
|
queue_work(system_highpri_wq, &timer->task_work);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2017-06-16 22:16:05 +08:00
|
|
|
EXPORT_SYMBOL(snd_timer_interrupt);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
|
|
|
|
struct snd_timer **rtimer)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer *timer;
|
2005-04-17 06:20:36 +08:00
|
|
|
int err;
|
2020-01-03 16:16:20 +08:00
|
|
|
static const struct snd_device_ops ops = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.dev_free = snd_timer_dev_free,
|
|
|
|
.dev_register = snd_timer_dev_register,
|
2006-06-23 20:38:23 +08:00
|
|
|
.dev_disconnect = snd_timer_dev_disconnect,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2008-08-08 23:09:09 +08:00
|
|
|
if (snd_BUG_ON(!tid))
|
|
|
|
return -EINVAL;
|
2018-07-20 13:43:51 +08:00
|
|
|
if (tid->dev_class == SNDRV_TIMER_CLASS_CARD ||
|
|
|
|
tid->dev_class == SNDRV_TIMER_CLASS_PCM) {
|
|
|
|
if (WARN_ON(!card))
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2008-08-08 23:09:09 +08:00
|
|
|
if (rtimer)
|
|
|
|
*rtimer = NULL;
|
2005-09-09 20:20:23 +08:00
|
|
|
timer = kzalloc(sizeof(*timer), GFP_KERNEL);
|
2015-03-10 22:42:14 +08:00
|
|
|
if (!timer)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
timer->tmr_class = tid->dev_class;
|
|
|
|
timer->card = card;
|
|
|
|
timer->tmr_device = tid->device;
|
|
|
|
timer->tmr_subdevice = tid->subdevice;
|
|
|
|
if (id)
|
ALSA: Convert strlcpy to strscpy when return value is unused
strlcpy is deprecated. see: Documentation/process/deprecated.rst
Change the calls that do not use the strlcpy return value to the
preferred strscpy.
Done with cocci script:
@@
expression e1, e2, e3;
@@
- strlcpy(
+ strscpy(
e1, e2, e3);
This cocci script leaves the instances where the return value is
used unchanged.
After this patch, sound/ has 3 uses of strlcpy() that need to be
manually inspected for conversion and changed one day.
$ git grep -w strlcpy sound/
sound/usb/card.c: len = strlcpy(card->longname, s, sizeof(card->longname));
sound/usb/mixer.c: return strlcpy(buf, p->name, buflen);
sound/usb/mixer.c: return strlcpy(buf, p->names[index], buflen);
Miscellenea:
o Remove trailing whitespace in conversion of sound/core/hwdep.c
Link: https://lore.kernel.org/lkml/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw@mail.gmail.com/
Signed-off-by: Joe Perches <joe@perches.com>
Acked-by: Mark Brown <broonie@kernel.org>
Link: https://lore.kernel.org/r/22b393d1790bb268769d0bab7bacf0866dcb0c14.camel@perches.com
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2021-01-05 01:17:34 +08:00
|
|
|
strscpy(timer->id, id, sizeof(timer->id));
|
2016-08-29 06:33:50 +08:00
|
|
|
timer->sticks = 1;
|
2005-04-17 06:20:36 +08:00
|
|
|
INIT_LIST_HEAD(&timer->device_list);
|
|
|
|
INIT_LIST_HEAD(&timer->open_list_head);
|
|
|
|
INIT_LIST_HEAD(&timer->active_list_head);
|
|
|
|
INIT_LIST_HEAD(&timer->ack_list_head);
|
|
|
|
INIT_LIST_HEAD(&timer->sack_list_head);
|
|
|
|
spin_lock_init(&timer->lock);
|
2020-09-03 18:41:22 +08:00
|
|
|
INIT_WORK(&timer->task_work, snd_timer_work);
|
2017-11-05 17:07:43 +08:00
|
|
|
timer->max_instances = 1000; /* default limit per timer */
|
2005-04-17 06:20:36 +08:00
|
|
|
if (card != NULL) {
|
2005-10-12 23:12:31 +08:00
|
|
|
timer->module = card->module;
|
2005-10-12 23:20:21 +08:00
|
|
|
err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
|
|
|
|
if (err < 0) {
|
2005-04-17 06:20:36 +08:00
|
|
|
snd_timer_free(timer);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
2008-08-08 23:09:09 +08:00
|
|
|
if (rtimer)
|
|
|
|
*rtimer = timer;
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2017-06-16 22:16:05 +08:00
|
|
|
EXPORT_SYMBOL(snd_timer_new);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
static int snd_timer_free(struct snd_timer *timer)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-08-08 23:09:09 +08:00
|
|
|
if (!timer)
|
|
|
|
return 0;
|
2006-06-23 20:38:23 +08:00
|
|
|
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(mutex)(®ister_mutex);
|
2006-06-23 20:38:23 +08:00
|
|
|
if (! list_empty(&timer->open_list_head)) {
|
|
|
|
struct list_head *p, *n;
|
|
|
|
struct snd_timer_instance *ti;
|
2014-02-05 01:22:39 +08:00
|
|
|
pr_warn("ALSA: timer %p is busy?\n", timer);
|
2006-06-23 20:38:23 +08:00
|
|
|
list_for_each_safe(p, n, &timer->open_list_head) {
|
|
|
|
list_del_init(p);
|
|
|
|
ti = list_entry(p, struct snd_timer_instance, open_list);
|
|
|
|
ti->timer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list_del(&timer->device_list);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (timer->private_free)
|
|
|
|
timer->private_free(timer);
|
|
|
|
kfree(timer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
static int snd_timer_dev_free(struct snd_device *device)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer *timer = device->device_data;
|
2005-04-17 06:20:36 +08:00
|
|
|
return snd_timer_free(timer);
|
|
|
|
}
|
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
static int snd_timer_dev_register(struct snd_device *dev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer *timer = dev->device_data;
|
|
|
|
struct snd_timer *timer1;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-08-08 23:09:09 +08:00
|
|
|
if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
|
|
|
|
return -ENXIO;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
|
|
|
|
!timer->hw.resolution && timer->hw.c_resolution == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(mutex)(®ister_mutex);
|
2006-10-05 22:02:22 +08:00
|
|
|
list_for_each_entry(timer1, &snd_timer_list, device_list) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (timer1->tmr_class > timer->tmr_class)
|
|
|
|
break;
|
|
|
|
if (timer1->tmr_class < timer->tmr_class)
|
|
|
|
continue;
|
|
|
|
if (timer1->card && timer->card) {
|
|
|
|
if (timer1->card->number > timer->card->number)
|
|
|
|
break;
|
|
|
|
if (timer1->card->number < timer->card->number)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (timer1->tmr_device > timer->tmr_device)
|
|
|
|
break;
|
|
|
|
if (timer1->tmr_device < timer->tmr_device)
|
|
|
|
continue;
|
|
|
|
if (timer1->tmr_subdevice > timer->tmr_subdevice)
|
|
|
|
break;
|
|
|
|
if (timer1->tmr_subdevice < timer->tmr_subdevice)
|
|
|
|
continue;
|
|
|
|
/* conflicts.. */
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
2006-10-05 22:02:22 +08:00
|
|
|
list_add_tail(&timer->device_list, &timer1->device_list);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-06-23 20:38:23 +08:00
|
|
|
static int snd_timer_dev_disconnect(struct snd_device *device)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-06-23 20:38:23 +08:00
|
|
|
struct snd_timer *timer = device->device_data;
|
2016-01-22 00:19:31 +08:00
|
|
|
struct snd_timer_instance *ti;
|
|
|
|
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(mutex)(®ister_mutex);
|
2006-06-23 20:38:23 +08:00
|
|
|
list_del_init(&timer->device_list);
|
2016-01-22 00:19:31 +08:00
|
|
|
/* wake up pending sleepers */
|
|
|
|
list_for_each_entry(ti, &timer->open_list_head, open_list) {
|
2016-01-22 00:43:08 +08:00
|
|
|
if (ti->disconnect)
|
|
|
|
ti->disconnect(ti);
|
2016-01-22 00:19:31 +08:00
|
|
|
}
|
2006-06-23 20:38:23 +08:00
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2018-04-24 20:06:08 +08:00
|
|
|
void snd_timer_notify(struct snd_timer *timer, int event, struct timespec64 *tstamp)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
unsigned long resolution = 0;
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_instance *ti, *ts;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2016-01-22 00:19:31 +08:00
|
|
|
if (timer->card && timer->card->shutdown)
|
|
|
|
return;
|
2005-10-10 17:46:31 +08:00
|
|
|
if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
|
|
|
|
return;
|
2008-08-08 23:09:09 +08:00
|
|
|
if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
|
|
|
|
event > SNDRV_TIMER_EVENT_MRESUME))
|
|
|
|
return;
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(spinlock_irqsave)(&timer->lock);
|
2005-08-16 17:09:05 +08:00
|
|
|
if (event == SNDRV_TIMER_EVENT_MSTART ||
|
|
|
|
event == SNDRV_TIMER_EVENT_MCONTINUE ||
|
2018-05-17 05:45:33 +08:00
|
|
|
event == SNDRV_TIMER_EVENT_MRESUME)
|
|
|
|
resolution = snd_timer_hw_resolution(timer);
|
2006-10-05 22:02:22 +08:00
|
|
|
list_for_each_entry(ti, &timer->active_list_head, active_list) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ti->ccallback)
|
|
|
|
ti->ccallback(ti, event, tstamp, resolution);
|
2006-10-05 22:02:22 +08:00
|
|
|
list_for_each_entry(ts, &ti->slave_active_head, active_list)
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ts->ccallback)
|
|
|
|
ts->ccallback(ts, event, tstamp, resolution);
|
|
|
|
}
|
|
|
|
}
|
2017-06-16 22:16:05 +08:00
|
|
|
EXPORT_SYMBOL(snd_timer_notify);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* exported functions for global timers
|
|
|
|
*/
|
2005-11-17 20:56:05 +08:00
|
|
|
int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_id tid;
|
2005-10-12 23:20:21 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
|
|
|
|
tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
|
|
|
|
tid.card = -1;
|
|
|
|
tid.device = device;
|
|
|
|
tid.subdevice = 0;
|
|
|
|
return snd_timer_new(NULL, id, &tid, rtimer);
|
|
|
|
}
|
2017-06-16 22:16:05 +08:00
|
|
|
EXPORT_SYMBOL(snd_timer_global_new);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
int snd_timer_global_free(struct snd_timer *timer)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
return snd_timer_free(timer);
|
|
|
|
}
|
2017-06-16 22:16:05 +08:00
|
|
|
EXPORT_SYMBOL(snd_timer_global_free);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
int snd_timer_global_register(struct snd_timer *timer)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_device dev;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
memset(&dev, 0, sizeof(dev));
|
|
|
|
dev.device_data = timer;
|
|
|
|
return snd_timer_dev_register(&dev);
|
|
|
|
}
|
2017-06-16 22:16:05 +08:00
|
|
|
EXPORT_SYMBOL(snd_timer_global_register);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-10-12 23:20:21 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* System timer
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct snd_timer_system_private {
|
|
|
|
struct timer_list tlist;
|
2017-10-05 08:53:33 +08:00
|
|
|
struct snd_timer *snd_timer;
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long last_expires;
|
|
|
|
unsigned long last_jiffies;
|
|
|
|
unsigned long correction;
|
|
|
|
};
|
|
|
|
|
2017-10-05 08:53:33 +08:00
|
|
|
static void snd_timer_s_function(struct timer_list *t)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2017-10-05 08:53:33 +08:00
|
|
|
struct snd_timer_system_private *priv = from_timer(priv, t,
|
|
|
|
tlist);
|
|
|
|
struct snd_timer *timer = priv->snd_timer;
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long jiff = jiffies;
|
|
|
|
if (time_after(jiff, priv->last_expires))
|
2006-07-17 22:51:37 +08:00
|
|
|
priv->correction += (long)jiff - (long)priv->last_expires;
|
2005-04-17 06:20:36 +08:00
|
|
|
snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
|
|
|
|
}
|
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
static int snd_timer_s_start(struct snd_timer * timer)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct snd_timer_system_private *priv;
|
|
|
|
unsigned long njiff;
|
|
|
|
|
|
|
|
priv = (struct snd_timer_system_private *) timer->private_data;
|
|
|
|
njiff = (priv->last_jiffies = jiffies);
|
|
|
|
if (priv->correction > timer->sticks - 1) {
|
|
|
|
priv->correction -= timer->sticks - 1;
|
|
|
|
njiff++;
|
|
|
|
} else {
|
|
|
|
njiff += timer->sticks - priv->correction;
|
2006-07-17 22:50:56 +08:00
|
|
|
priv->correction = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2016-04-01 18:28:16 +08:00
|
|
|
priv->last_expires = njiff;
|
|
|
|
mod_timer(&priv->tlist, njiff);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
static int snd_timer_s_stop(struct snd_timer * timer)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct snd_timer_system_private *priv;
|
|
|
|
unsigned long jiff;
|
|
|
|
|
|
|
|
priv = (struct snd_timer_system_private *) timer->private_data;
|
|
|
|
del_timer(&priv->tlist);
|
|
|
|
jiff = jiffies;
|
|
|
|
if (time_before(jiff, priv->last_expires))
|
|
|
|
timer->sticks = priv->last_expires - jiff;
|
|
|
|
else
|
|
|
|
timer->sticks = 1;
|
2006-07-17 22:52:09 +08:00
|
|
|
priv->correction = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:14:10 +08:00
|
|
|
static int snd_timer_s_close(struct snd_timer *timer)
|
|
|
|
{
|
|
|
|
struct snd_timer_system_private *priv;
|
|
|
|
|
|
|
|
priv = (struct snd_timer_system_private *)timer->private_data;
|
|
|
|
del_timer_sync(&priv->tlist);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-03 16:16:36 +08:00
|
|
|
static const struct snd_timer_hardware snd_timer_system =
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2020-09-03 18:41:22 +08:00
|
|
|
.flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_WORK,
|
2005-04-17 06:20:36 +08:00
|
|
|
.resolution = 1000000000L / HZ,
|
|
|
|
.ticks = 10000000L,
|
2016-02-02 21:14:10 +08:00
|
|
|
.close = snd_timer_s_close,
|
2005-04-17 06:20:36 +08:00
|
|
|
.start = snd_timer_s_start,
|
|
|
|
.stop = snd_timer_s_stop
|
|
|
|
};
|
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
static void snd_timer_free_system(struct snd_timer *timer)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
kfree(timer->private_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int snd_timer_register_system(void)
|
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer *timer;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct snd_timer_system_private *priv;
|
|
|
|
int err;
|
|
|
|
|
2005-10-12 23:20:21 +08:00
|
|
|
err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
|
|
|
|
if (err < 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
return err;
|
|
|
|
strcpy(timer->name, "system timer");
|
|
|
|
timer->hw = snd_timer_system;
|
2005-09-09 20:20:23 +08:00
|
|
|
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (priv == NULL) {
|
|
|
|
snd_timer_free(timer);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2017-10-05 08:53:33 +08:00
|
|
|
priv->snd_timer = timer;
|
|
|
|
timer_setup(&priv->tlist, snd_timer_s_function, 0);
|
2005-04-17 06:20:36 +08:00
|
|
|
timer->private_data = priv;
|
|
|
|
timer->private_free = snd_timer_free_system;
|
|
|
|
return snd_timer_global_register(timer);
|
|
|
|
}
|
|
|
|
|
2015-05-27 19:45:45 +08:00
|
|
|
#ifdef CONFIG_SND_PROC_FS
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Info interface
|
|
|
|
*/
|
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
static void snd_timer_proc_read(struct snd_info_entry *entry,
|
|
|
|
struct snd_info_buffer *buffer)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer *timer;
|
|
|
|
struct snd_timer_instance *ti;
|
2023-06-13 03:13:22 +08:00
|
|
|
unsigned long resolution;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(mutex)(®ister_mutex);
|
2006-10-05 22:02:22 +08:00
|
|
|
list_for_each_entry(timer, &snd_timer_list, device_list) {
|
2016-01-22 00:19:31 +08:00
|
|
|
if (timer->card && timer->card->shutdown)
|
|
|
|
continue;
|
2005-04-17 06:20:36 +08:00
|
|
|
switch (timer->tmr_class) {
|
|
|
|
case SNDRV_TIMER_CLASS_GLOBAL:
|
|
|
|
snd_iprintf(buffer, "G%i: ", timer->tmr_device);
|
|
|
|
break;
|
|
|
|
case SNDRV_TIMER_CLASS_CARD:
|
2005-10-12 23:20:21 +08:00
|
|
|
snd_iprintf(buffer, "C%i-%i: ",
|
|
|
|
timer->card->number, timer->tmr_device);
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
case SNDRV_TIMER_CLASS_PCM:
|
2005-10-12 23:20:21 +08:00
|
|
|
snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
|
|
|
|
timer->tmr_device, timer->tmr_subdevice);
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
default:
|
2005-10-12 23:20:21 +08:00
|
|
|
snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
|
|
|
|
timer->card ? timer->card->number : -1,
|
|
|
|
timer->tmr_device, timer->tmr_subdevice);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
snd_iprintf(buffer, "%s :", timer->name);
|
2024-02-27 16:52:45 +08:00
|
|
|
scoped_guard(spinlock_irq, &timer->lock)
|
|
|
|
resolution = snd_timer_hw_resolution(timer);
|
2023-06-13 03:13:22 +08:00
|
|
|
if (resolution)
|
2005-10-12 23:20:21 +08:00
|
|
|
snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
|
2023-06-13 03:13:22 +08:00
|
|
|
resolution / 1000,
|
|
|
|
resolution % 1000,
|
2005-10-12 23:20:21 +08:00
|
|
|
timer->hw.ticks);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
|
|
|
|
snd_iprintf(buffer, " SLAVE");
|
|
|
|
snd_iprintf(buffer, "\n");
|
2006-10-05 22:02:22 +08:00
|
|
|
list_for_each_entry(ti, &timer->open_list_head, open_list)
|
2005-10-12 23:20:21 +08:00
|
|
|
snd_iprintf(buffer, " Client %s : %s\n",
|
|
|
|
ti->owner ? ti->owner : "unknown",
|
2020-09-03 05:21:20 +08:00
|
|
|
(ti->flags & (SNDRV_TIMER_IFLG_START |
|
|
|
|
SNDRV_TIMER_IFLG_RUNNING))
|
2005-10-12 23:20:21 +08:00
|
|
|
? "running" : "stopped");
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-17 23:14:51 +08:00
|
|
|
static struct snd_info_entry *snd_timer_proc_entry;
|
2005-12-01 17:42:42 +08:00
|
|
|
|
|
|
|
static void __init snd_timer_proc_init(void)
|
|
|
|
{
|
|
|
|
struct snd_info_entry *entry;
|
|
|
|
|
|
|
|
entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
|
|
|
|
if (entry != NULL) {
|
|
|
|
entry->c.text.read = snd_timer_proc_read;
|
|
|
|
if (snd_info_register(entry) < 0) {
|
|
|
|
snd_info_free_entry(entry);
|
|
|
|
entry = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
snd_timer_proc_entry = entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit snd_timer_proc_done(void)
|
|
|
|
{
|
2006-06-23 20:37:59 +08:00
|
|
|
snd_info_free_entry(snd_timer_proc_entry);
|
2005-12-01 17:42:42 +08:00
|
|
|
}
|
2015-05-27 19:45:45 +08:00
|
|
|
#else /* !CONFIG_SND_PROC_FS */
|
2005-12-01 17:42:42 +08:00
|
|
|
#define snd_timer_proc_init()
|
|
|
|
#define snd_timer_proc_done()
|
|
|
|
#endif
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* USER SPACE interface
|
|
|
|
*/
|
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long resolution,
|
|
|
|
unsigned long ticks)
|
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_user *tu = timeri->callback_data;
|
|
|
|
struct snd_timer_read *r;
|
2005-04-17 06:20:36 +08:00
|
|
|
int prev;
|
2005-10-12 23:20:21 +08:00
|
|
|
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(spinlock)(&tu->qlock);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (tu->qused > 0) {
|
|
|
|
prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
|
|
|
|
r = &tu->queue[prev];
|
|
|
|
if (r->resolution == resolution) {
|
|
|
|
r->ticks += ticks;
|
|
|
|
goto __wake;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tu->qused >= tu->queue_size) {
|
|
|
|
tu->overrun++;
|
|
|
|
} else {
|
|
|
|
r = &tu->queue[tu->qtail++];
|
|
|
|
tu->qtail %= tu->queue_size;
|
|
|
|
r->resolution = resolution;
|
|
|
|
r->ticks = ticks;
|
|
|
|
tu->qused++;
|
|
|
|
}
|
|
|
|
__wake:
|
2022-07-28 20:59:43 +08:00
|
|
|
snd_kill_fasync(tu->fasync, SIGIO, POLL_IN);
|
2005-04-17 06:20:36 +08:00
|
|
|
wake_up(&tu->qchange_sleep);
|
|
|
|
}
|
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
|
2018-04-24 20:06:13 +08:00
|
|
|
struct snd_timer_tread64 *tread)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
if (tu->qused >= tu->queue_size) {
|
|
|
|
tu->overrun++;
|
|
|
|
} else {
|
|
|
|
memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
|
|
|
|
tu->qtail %= tu->queue_size;
|
|
|
|
tu->qused++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
|
|
|
|
int event,
|
2018-04-24 20:06:08 +08:00
|
|
|
struct timespec64 *tstamp,
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long resolution)
|
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_user *tu = timeri->callback_data;
|
2018-04-24 20:06:13 +08:00
|
|
|
struct snd_timer_tread64 r1;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-10-12 23:20:21 +08:00
|
|
|
if (event >= SNDRV_TIMER_EVENT_START &&
|
|
|
|
event <= SNDRV_TIMER_EVENT_PAUSE)
|
2005-04-17 06:20:36 +08:00
|
|
|
tu->tstamp = *tstamp;
|
|
|
|
if ((tu->filter & (1 << event)) == 0 || !tu->tread)
|
|
|
|
return;
|
2016-05-04 04:44:20 +08:00
|
|
|
memset(&r1, 0, sizeof(r1));
|
2005-04-17 06:20:36 +08:00
|
|
|
r1.event = event;
|
2018-04-24 20:06:13 +08:00
|
|
|
r1.tstamp_sec = tstamp->tv_sec;
|
|
|
|
r1.tstamp_nsec = tstamp->tv_nsec;
|
2005-04-17 06:20:36 +08:00
|
|
|
r1.val = resolution;
|
2024-02-27 16:52:45 +08:00
|
|
|
scoped_guard(spinlock_irqsave, &tu->qlock)
|
|
|
|
snd_timer_user_append_to_tqueue(tu, &r1);
|
2022-07-28 20:59:43 +08:00
|
|
|
snd_kill_fasync(tu->fasync, SIGIO, POLL_IN);
|
2005-04-17 06:20:36 +08:00
|
|
|
wake_up(&tu->qchange_sleep);
|
|
|
|
}
|
|
|
|
|
2016-01-22 00:43:08 +08:00
|
|
|
static void snd_timer_user_disconnect(struct snd_timer_instance *timeri)
|
|
|
|
{
|
|
|
|
struct snd_timer_user *tu = timeri->callback_data;
|
|
|
|
|
|
|
|
tu->disconnected = true;
|
|
|
|
wake_up(&tu->qchange_sleep);
|
|
|
|
}
|
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long resolution,
|
|
|
|
unsigned long ticks)
|
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_user *tu = timeri->callback_data;
|
2018-04-24 20:06:13 +08:00
|
|
|
struct snd_timer_tread64 *r, r1;
|
2018-04-24 20:06:08 +08:00
|
|
|
struct timespec64 tstamp;
|
2005-04-17 06:20:36 +08:00
|
|
|
int prev, append = 0;
|
|
|
|
|
2017-03-31 23:22:23 +08:00
|
|
|
memset(&r1, 0, sizeof(r1));
|
2005-10-10 17:49:49 +08:00
|
|
|
memset(&tstamp, 0, sizeof(tstamp));
|
2024-02-27 16:52:45 +08:00
|
|
|
scoped_guard(spinlock, &tu->qlock) {
|
|
|
|
if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
|
|
|
|
(1 << SNDRV_TIMER_EVENT_TICK))) == 0)
|
|
|
|
return;
|
|
|
|
if (tu->last_resolution != resolution || ticks > 0) {
|
|
|
|
if (timer_tstamp_monotonic)
|
|
|
|
ktime_get_ts64(&tstamp);
|
|
|
|
else
|
|
|
|
ktime_get_real_ts64(&tstamp);
|
|
|
|
}
|
|
|
|
if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
|
|
|
|
tu->last_resolution != resolution) {
|
|
|
|
r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
|
|
|
|
r1.tstamp_sec = tstamp.tv_sec;
|
|
|
|
r1.tstamp_nsec = tstamp.tv_nsec;
|
|
|
|
r1.val = resolution;
|
|
|
|
snd_timer_user_append_to_tqueue(tu, &r1);
|
|
|
|
tu->last_resolution = resolution;
|
|
|
|
append++;
|
|
|
|
}
|
|
|
|
if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
|
|
|
|
break;
|
|
|
|
if (ticks == 0)
|
|
|
|
break;
|
|
|
|
if (tu->qused > 0) {
|
|
|
|
prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
|
|
|
|
r = &tu->tqueue[prev];
|
|
|
|
if (r->event == SNDRV_TIMER_EVENT_TICK) {
|
|
|
|
r->tstamp_sec = tstamp.tv_sec;
|
|
|
|
r->tstamp_nsec = tstamp.tv_nsec;
|
|
|
|
r->val += ticks;
|
|
|
|
append++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r1.event = SNDRV_TIMER_EVENT_TICK;
|
2018-04-24 20:06:13 +08:00
|
|
|
r1.tstamp_sec = tstamp.tv_sec;
|
|
|
|
r1.tstamp_nsec = tstamp.tv_nsec;
|
2024-02-27 16:52:45 +08:00
|
|
|
r1.val = ticks;
|
2005-04-17 06:20:36 +08:00
|
|
|
snd_timer_user_append_to_tqueue(tu, &r1);
|
|
|
|
append++;
|
|
|
|
}
|
|
|
|
if (append == 0)
|
|
|
|
return;
|
2022-07-28 20:59:43 +08:00
|
|
|
snd_kill_fasync(tu->fasync, SIGIO, POLL_IN);
|
2005-04-17 06:20:36 +08:00
|
|
|
wake_up(&tu->qchange_sleep);
|
|
|
|
}
|
|
|
|
|
ALSA: timer: Improve user queue reallocation
ALSA timer may reallocate the user queue upon request, and it happens
at three places for now: at opening, at SNDRV_TIMER_IOCTL_PARAMS, and
at SNDRV_TIMER_IOCTL_SELECT. However, the last one,
snd_timer_user_tselect(), doesn't need to reallocate the buffer since
it doesn't change the queue size. It does just because tu->tread
might have been changed before starting the timer.
Instead of *_SELECT ioctl, we should reallocate the queue at
SNDRV_TIMER_IOCTL_TREAD; then the timer is guaranteed to be stopped,
thus we can reassign the buffer more safely.
This patch implements that with a slight code refactoring.
Essentially, the patch achieves:
- Introduce realloc_user_queue() for (re-)allocating the ring buffer,
and call it from all places. Also, realloc_user_queue() uses
kcalloc() for avoiding possible leaks.
- Add the buffer reallocation at SNDRV_TIMER_IOCTL_TREAD. When it
fails, tu->tread is restored to the old value, too.
- Drop the buffer reallocation at snd_timer_user_tselect().
Tested-by: Alexander Potapenko <glider@google.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2017-06-02 23:16:59 +08:00
|
|
|
static int realloc_user_queue(struct snd_timer_user *tu, int size)
|
|
|
|
{
|
|
|
|
struct snd_timer_read *queue = NULL;
|
2018-04-24 20:06:13 +08:00
|
|
|
struct snd_timer_tread64 *tqueue = NULL;
|
ALSA: timer: Improve user queue reallocation
ALSA timer may reallocate the user queue upon request, and it happens
at three places for now: at opening, at SNDRV_TIMER_IOCTL_PARAMS, and
at SNDRV_TIMER_IOCTL_SELECT. However, the last one,
snd_timer_user_tselect(), doesn't need to reallocate the buffer since
it doesn't change the queue size. It does just because tu->tread
might have been changed before starting the timer.
Instead of *_SELECT ioctl, we should reallocate the queue at
SNDRV_TIMER_IOCTL_TREAD; then the timer is guaranteed to be stopped,
thus we can reassign the buffer more safely.
This patch implements that with a slight code refactoring.
Essentially, the patch achieves:
- Introduce realloc_user_queue() for (re-)allocating the ring buffer,
and call it from all places. Also, realloc_user_queue() uses
kcalloc() for avoiding possible leaks.
- Add the buffer reallocation at SNDRV_TIMER_IOCTL_TREAD. When it
fails, tu->tread is restored to the old value, too.
- Drop the buffer reallocation at snd_timer_user_tselect().
Tested-by: Alexander Potapenko <glider@google.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2017-06-02 23:16:59 +08:00
|
|
|
|
|
|
|
if (tu->tread) {
|
|
|
|
tqueue = kcalloc(size, sizeof(*tqueue), GFP_KERNEL);
|
|
|
|
if (!tqueue)
|
|
|
|
return -ENOMEM;
|
|
|
|
} else {
|
|
|
|
queue = kcalloc(size, sizeof(*queue), GFP_KERNEL);
|
|
|
|
if (!queue)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(spinlock_irq)(&tu->qlock);
|
ALSA: timer: Improve user queue reallocation
ALSA timer may reallocate the user queue upon request, and it happens
at three places for now: at opening, at SNDRV_TIMER_IOCTL_PARAMS, and
at SNDRV_TIMER_IOCTL_SELECT. However, the last one,
snd_timer_user_tselect(), doesn't need to reallocate the buffer since
it doesn't change the queue size. It does just because tu->tread
might have been changed before starting the timer.
Instead of *_SELECT ioctl, we should reallocate the queue at
SNDRV_TIMER_IOCTL_TREAD; then the timer is guaranteed to be stopped,
thus we can reassign the buffer more safely.
This patch implements that with a slight code refactoring.
Essentially, the patch achieves:
- Introduce realloc_user_queue() for (re-)allocating the ring buffer,
and call it from all places. Also, realloc_user_queue() uses
kcalloc() for avoiding possible leaks.
- Add the buffer reallocation at SNDRV_TIMER_IOCTL_TREAD. When it
fails, tu->tread is restored to the old value, too.
- Drop the buffer reallocation at snd_timer_user_tselect().
Tested-by: Alexander Potapenko <glider@google.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2017-06-02 23:16:59 +08:00
|
|
|
kfree(tu->queue);
|
|
|
|
kfree(tu->tqueue);
|
|
|
|
tu->queue_size = size;
|
|
|
|
tu->queue = queue;
|
|
|
|
tu->tqueue = tqueue;
|
|
|
|
tu->qhead = tu->qtail = tu->qused = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static int snd_timer_user_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_user *tu;
|
2010-04-13 17:49:04 +08:00
|
|
|
int err;
|
|
|
|
|
2019-03-27 04:51:19 +08:00
|
|
|
err = stream_open(inode, file);
|
2010-04-13 17:49:04 +08:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2005-10-12 23:20:21 +08:00
|
|
|
|
2005-09-09 20:20:23 +08:00
|
|
|
tu = kzalloc(sizeof(*tu), GFP_KERNEL);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (tu == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
spin_lock_init(&tu->qlock);
|
|
|
|
init_waitqueue_head(&tu->qchange_sleep);
|
2016-01-14 00:48:01 +08:00
|
|
|
mutex_init(&tu->ioctl_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
tu->ticks = 1;
|
ALSA: timer: Improve user queue reallocation
ALSA timer may reallocate the user queue upon request, and it happens
at three places for now: at opening, at SNDRV_TIMER_IOCTL_PARAMS, and
at SNDRV_TIMER_IOCTL_SELECT. However, the last one,
snd_timer_user_tselect(), doesn't need to reallocate the buffer since
it doesn't change the queue size. It does just because tu->tread
might have been changed before starting the timer.
Instead of *_SELECT ioctl, we should reallocate the queue at
SNDRV_TIMER_IOCTL_TREAD; then the timer is guaranteed to be stopped,
thus we can reassign the buffer more safely.
This patch implements that with a slight code refactoring.
Essentially, the patch achieves:
- Introduce realloc_user_queue() for (re-)allocating the ring buffer,
and call it from all places. Also, realloc_user_queue() uses
kcalloc() for avoiding possible leaks.
- Add the buffer reallocation at SNDRV_TIMER_IOCTL_TREAD. When it
fails, tu->tread is restored to the old value, too.
- Drop the buffer reallocation at snd_timer_user_tselect().
Tested-by: Alexander Potapenko <glider@google.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2017-06-02 23:16:59 +08:00
|
|
|
if (realloc_user_queue(tu, 128) < 0) {
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree(tu);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
file->private_data = tu;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int snd_timer_user_release(struct inode *inode, struct file *file)
|
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_user *tu;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (file->private_data) {
|
|
|
|
tu = file->private_data;
|
|
|
|
file->private_data = NULL;
|
2024-02-27 16:52:45 +08:00
|
|
|
scoped_guard(mutex, &tu->ioctl_lock) {
|
|
|
|
if (tu->timeri) {
|
|
|
|
snd_timer_close(tu->timeri);
|
|
|
|
snd_timer_instance_free(tu->timeri);
|
|
|
|
}
|
2019-11-08 03:20:08 +08:00
|
|
|
}
|
2022-07-28 20:59:43 +08:00
|
|
|
snd_fasync_free(tu->fasync);
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree(tu->queue);
|
|
|
|
kfree(tu->tqueue);
|
|
|
|
kfree(tu);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
static void snd_timer_user_zero_id(struct snd_timer_id *id)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
id->dev_class = SNDRV_TIMER_CLASS_NONE;
|
|
|
|
id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
|
|
|
|
id->card = -1;
|
|
|
|
id->device = -1;
|
|
|
|
id->subdevice = -1;
|
|
|
|
}
|
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
id->dev_class = timer->tmr_class;
|
|
|
|
id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
|
|
|
|
id->card = timer->card ? timer->card->number : -1;
|
|
|
|
id->device = timer->tmr_device;
|
|
|
|
id->subdevice = timer->tmr_subdevice;
|
|
|
|
}
|
|
|
|
|
2005-11-17 20:56:05 +08:00
|
|
|
static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_id id;
|
|
|
|
struct snd_timer *timer;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct list_head *p;
|
2005-10-12 23:20:21 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (copy_from_user(&id, _tid, sizeof(id)))
|
|
|
|
return -EFAULT;
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(mutex)(®ister_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (id.dev_class < 0) { /* first item */
|
|
|
|
if (list_empty(&snd_timer_list))
|
|
|
|
snd_timer_user_zero_id(&id);
|
|
|
|
else {
|
2005-10-12 23:14:55 +08:00
|
|
|
timer = list_entry(snd_timer_list.next,
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer, device_list);
|
2005-04-17 06:20:36 +08:00
|
|
|
snd_timer_user_copy_id(&id, timer);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (id.dev_class) {
|
|
|
|
case SNDRV_TIMER_CLASS_GLOBAL:
|
|
|
|
id.device = id.device < 0 ? 0 : id.device + 1;
|
|
|
|
list_for_each(p, &snd_timer_list) {
|
2005-11-17 20:56:05 +08:00
|
|
|
timer = list_entry(p, struct snd_timer, device_list);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
|
|
|
|
snd_timer_user_copy_id(&id, timer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (timer->tmr_device >= id.device) {
|
|
|
|
snd_timer_user_copy_id(&id, timer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p == &snd_timer_list)
|
|
|
|
snd_timer_user_zero_id(&id);
|
|
|
|
break;
|
|
|
|
case SNDRV_TIMER_CLASS_CARD:
|
|
|
|
case SNDRV_TIMER_CLASS_PCM:
|
|
|
|
if (id.card < 0) {
|
|
|
|
id.card = 0;
|
|
|
|
} else {
|
2017-03-31 23:21:41 +08:00
|
|
|
if (id.device < 0) {
|
|
|
|
id.device = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
} else {
|
2017-03-31 23:21:41 +08:00
|
|
|
if (id.subdevice < 0)
|
|
|
|
id.subdevice = 0;
|
2018-06-25 17:09:11 +08:00
|
|
|
else if (id.subdevice < INT_MAX)
|
2017-03-31 23:21:41 +08:00
|
|
|
id.subdevice++;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
list_for_each(p, &snd_timer_list) {
|
2005-11-17 20:56:05 +08:00
|
|
|
timer = list_entry(p, struct snd_timer, device_list);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (timer->tmr_class > id.dev_class) {
|
|
|
|
snd_timer_user_copy_id(&id, timer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (timer->tmr_class < id.dev_class)
|
|
|
|
continue;
|
|
|
|
if (timer->card->number > id.card) {
|
|
|
|
snd_timer_user_copy_id(&id, timer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (timer->card->number < id.card)
|
|
|
|
continue;
|
|
|
|
if (timer->tmr_device > id.device) {
|
|
|
|
snd_timer_user_copy_id(&id, timer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (timer->tmr_device < id.device)
|
|
|
|
continue;
|
|
|
|
if (timer->tmr_subdevice > id.subdevice) {
|
|
|
|
snd_timer_user_copy_id(&id, timer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (timer->tmr_subdevice < id.subdevice)
|
|
|
|
continue;
|
|
|
|
snd_timer_user_copy_id(&id, timer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (p == &snd_timer_list)
|
|
|
|
snd_timer_user_zero_id(&id);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
snd_timer_user_zero_id(&id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (copy_to_user(_tid, &id, sizeof(*_tid)))
|
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
2005-10-12 23:20:21 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-10-12 23:20:21 +08:00
|
|
|
static int snd_timer_user_ginfo(struct file *file,
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_ginfo __user *_ginfo)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2024-02-22 19:15:04 +08:00
|
|
|
struct snd_timer_ginfo *ginfo __free(kfree) = NULL;
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_id tid;
|
|
|
|
struct snd_timer *t;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct list_head *p;
|
|
|
|
|
2009-04-10 09:43:08 +08:00
|
|
|
ginfo = memdup_user(_ginfo, sizeof(*ginfo));
|
|
|
|
if (IS_ERR(ginfo))
|
2024-02-22 19:15:04 +08:00
|
|
|
return PTR_ERR(no_free_ptr(ginfo));
|
2009-04-10 09:43:08 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
tid = ginfo->tid;
|
|
|
|
memset(ginfo, 0, sizeof(*ginfo));
|
|
|
|
ginfo->tid = tid;
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(mutex)(®ister_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
t = snd_timer_find(&tid);
|
2024-02-27 16:52:45 +08:00
|
|
|
if (!t)
|
|
|
|
return -ENODEV;
|
|
|
|
ginfo->card = t->card ? t->card->number : -1;
|
|
|
|
if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
|
|
|
|
ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
|
|
|
|
strscpy(ginfo->id, t->id, sizeof(ginfo->id));
|
|
|
|
strscpy(ginfo->name, t->name, sizeof(ginfo->name));
|
|
|
|
scoped_guard(spinlock_irq, &t->lock)
|
2023-06-13 03:13:22 +08:00
|
|
|
ginfo->resolution = snd_timer_hw_resolution(t);
|
2024-02-27 16:52:45 +08:00
|
|
|
if (t->hw.resolution_min > 0) {
|
|
|
|
ginfo->resolution_min = t->hw.resolution_min;
|
|
|
|
ginfo->resolution_max = t->hw.resolution_max;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2024-02-27 16:52:45 +08:00
|
|
|
list_for_each(p, &t->open_list_head) {
|
|
|
|
ginfo->clients++;
|
|
|
|
}
|
|
|
|
if (copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
|
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2016-03-23 07:03:59 +08:00
|
|
|
static int timer_set_gparams(struct snd_timer_gparams *gparams)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer *t;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(mutex)(®ister_mutex);
|
2016-03-23 07:03:59 +08:00
|
|
|
t = snd_timer_find(&gparams->tid);
|
2024-02-27 16:52:45 +08:00
|
|
|
if (!t)
|
|
|
|
return -ENODEV;
|
|
|
|
if (!list_empty(&t->open_list_head))
|
|
|
|
return -EBUSY;
|
|
|
|
if (!t->hw.set_period)
|
|
|
|
return -ENOSYS;
|
|
|
|
return t->hw.set_period(t, gparams->period_num, gparams->period_den);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2016-03-23 07:03:59 +08:00
|
|
|
static int snd_timer_user_gparams(struct file *file,
|
|
|
|
struct snd_timer_gparams __user *_gparams)
|
|
|
|
{
|
|
|
|
struct snd_timer_gparams gparams;
|
|
|
|
|
|
|
|
if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
|
|
|
|
return -EFAULT;
|
|
|
|
return timer_set_gparams(&gparams);
|
|
|
|
}
|
|
|
|
|
2005-10-12 23:20:21 +08:00
|
|
|
static int snd_timer_user_gstatus(struct file *file,
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_gstatus __user *_gstatus)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_gstatus gstatus;
|
|
|
|
struct snd_timer_id tid;
|
|
|
|
struct snd_timer *t;
|
2005-04-17 06:20:36 +08:00
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
|
|
|
|
return -EFAULT;
|
|
|
|
tid = gstatus.tid;
|
|
|
|
memset(&gstatus, 0, sizeof(gstatus));
|
|
|
|
gstatus.tid = tid;
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(mutex)(®ister_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
t = snd_timer_find(&tid);
|
|
|
|
if (t != NULL) {
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(spinlock_irq)(&t->lock);
|
2018-05-17 05:45:33 +08:00
|
|
|
gstatus.resolution = snd_timer_hw_resolution(t);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (t->hw.precise_resolution) {
|
2005-10-12 23:20:21 +08:00
|
|
|
t->hw.precise_resolution(t, &gstatus.resolution_num,
|
|
|
|
&gstatus.resolution_den);
|
2005-04-17 06:20:36 +08:00
|
|
|
} else {
|
|
|
|
gstatus.resolution_num = gstatus.resolution;
|
|
|
|
gstatus.resolution_den = 1000000000uL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = -ENODEV;
|
|
|
|
}
|
|
|
|
if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
|
|
|
|
err = -EFAULT;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2005-10-12 23:20:21 +08:00
|
|
|
static int snd_timer_user_tselect(struct file *file,
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_select __user *_tselect)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_user *tu;
|
|
|
|
struct snd_timer_select tselect;
|
2005-04-17 06:20:36 +08:00
|
|
|
char str[32];
|
2005-04-04 22:44:58 +08:00
|
|
|
int err = 0;
|
2005-10-12 23:20:21 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
tu = file->private_data;
|
2005-04-04 22:44:58 +08:00
|
|
|
if (tu->timeri) {
|
2005-04-17 06:20:36 +08:00
|
|
|
snd_timer_close(tu->timeri);
|
2019-11-08 03:20:08 +08:00
|
|
|
snd_timer_instance_free(tu->timeri);
|
2005-04-04 22:44:58 +08:00
|
|
|
tu->timeri = NULL;
|
|
|
|
}
|
|
|
|
if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
|
|
|
|
err = -EFAULT;
|
|
|
|
goto __err;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
sprintf(str, "application %i", current->pid);
|
|
|
|
if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
|
|
|
|
tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
|
2019-11-08 03:20:08 +08:00
|
|
|
tu->timeri = snd_timer_instance_new(str);
|
|
|
|
if (!tu->timeri) {
|
|
|
|
err = -ENOMEM;
|
2005-04-04 22:44:58 +08:00
|
|
|
goto __err;
|
2019-11-08 03:20:08 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
ALSA: timer: Improve user queue reallocation
ALSA timer may reallocate the user queue upon request, and it happens
at three places for now: at opening, at SNDRV_TIMER_IOCTL_PARAMS, and
at SNDRV_TIMER_IOCTL_SELECT. However, the last one,
snd_timer_user_tselect(), doesn't need to reallocate the buffer since
it doesn't change the queue size. It does just because tu->tread
might have been changed before starting the timer.
Instead of *_SELECT ioctl, we should reallocate the queue at
SNDRV_TIMER_IOCTL_TREAD; then the timer is guaranteed to be stopped,
thus we can reassign the buffer more safely.
This patch implements that with a slight code refactoring.
Essentially, the patch achieves:
- Introduce realloc_user_queue() for (re-)allocating the ring buffer,
and call it from all places. Also, realloc_user_queue() uses
kcalloc() for avoiding possible leaks.
- Add the buffer reallocation at SNDRV_TIMER_IOCTL_TREAD. When it
fails, tu->tread is restored to the old value, too.
- Drop the buffer reallocation at snd_timer_user_tselect().
Tested-by: Alexander Potapenko <glider@google.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2017-06-02 23:16:59 +08:00
|
|
|
tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
|
|
|
|
tu->timeri->callback = tu->tread
|
2005-10-12 23:20:21 +08:00
|
|
|
? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
|
ALSA: timer: Improve user queue reallocation
ALSA timer may reallocate the user queue upon request, and it happens
at three places for now: at opening, at SNDRV_TIMER_IOCTL_PARAMS, and
at SNDRV_TIMER_IOCTL_SELECT. However, the last one,
snd_timer_user_tselect(), doesn't need to reallocate the buffer since
it doesn't change the queue size. It does just because tu->tread
might have been changed before starting the timer.
Instead of *_SELECT ioctl, we should reallocate the queue at
SNDRV_TIMER_IOCTL_TREAD; then the timer is guaranteed to be stopped,
thus we can reassign the buffer more safely.
This patch implements that with a slight code refactoring.
Essentially, the patch achieves:
- Introduce realloc_user_queue() for (re-)allocating the ring buffer,
and call it from all places. Also, realloc_user_queue() uses
kcalloc() for avoiding possible leaks.
- Add the buffer reallocation at SNDRV_TIMER_IOCTL_TREAD. When it
fails, tu->tread is restored to the old value, too.
- Drop the buffer reallocation at snd_timer_user_tselect().
Tested-by: Alexander Potapenko <glider@google.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2017-06-02 23:16:59 +08:00
|
|
|
tu->timeri->ccallback = snd_timer_user_ccallback;
|
|
|
|
tu->timeri->callback_data = (void *)tu;
|
|
|
|
tu->timeri->disconnect = snd_timer_user_disconnect;
|
2005-04-04 22:44:58 +08:00
|
|
|
|
2019-11-08 03:20:08 +08:00
|
|
|
err = snd_timer_open(tu->timeri, &tselect.id, current->pid);
|
|
|
|
if (err < 0) {
|
|
|
|
snd_timer_instance_free(tu->timeri);
|
|
|
|
tu->timeri = NULL;
|
|
|
|
}
|
|
|
|
|
2005-04-04 22:44:58 +08:00
|
|
|
__err:
|
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2005-10-12 23:20:21 +08:00
|
|
|
static int snd_timer_user_info(struct file *file,
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_info __user *_info)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_user *tu;
|
2024-02-22 19:15:04 +08:00
|
|
|
struct snd_timer_info *info __free(kfree) = NULL;
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer *t;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
tu = file->private_data;
|
2007-07-16 17:01:30 +08:00
|
|
|
if (!tu->timeri)
|
|
|
|
return -EBADFD;
|
2005-04-17 06:20:36 +08:00
|
|
|
t = tu->timeri->timer;
|
2007-07-16 17:01:30 +08:00
|
|
|
if (!t)
|
|
|
|
return -EBADFD;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-09-09 20:20:23 +08:00
|
|
|
info = kzalloc(sizeof(*info), GFP_KERNEL);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (! info)
|
|
|
|
return -ENOMEM;
|
|
|
|
info->card = t->card ? t->card->number : -1;
|
|
|
|
if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
|
|
|
|
info->flags |= SNDRV_TIMER_FLG_SLAVE;
|
ALSA: Convert strlcpy to strscpy when return value is unused
strlcpy is deprecated. see: Documentation/process/deprecated.rst
Change the calls that do not use the strlcpy return value to the
preferred strscpy.
Done with cocci script:
@@
expression e1, e2, e3;
@@
- strlcpy(
+ strscpy(
e1, e2, e3);
This cocci script leaves the instances where the return value is
used unchanged.
After this patch, sound/ has 3 uses of strlcpy() that need to be
manually inspected for conversion and changed one day.
$ git grep -w strlcpy sound/
sound/usb/card.c: len = strlcpy(card->longname, s, sizeof(card->longname));
sound/usb/mixer.c: return strlcpy(buf, p->name, buflen);
sound/usb/mixer.c: return strlcpy(buf, p->names[index], buflen);
Miscellenea:
o Remove trailing whitespace in conversion of sound/core/hwdep.c
Link: https://lore.kernel.org/lkml/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw@mail.gmail.com/
Signed-off-by: Joe Perches <joe@perches.com>
Acked-by: Mark Brown <broonie@kernel.org>
Link: https://lore.kernel.org/r/22b393d1790bb268769d0bab7bacf0866dcb0c14.camel@perches.com
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2021-01-05 01:17:34 +08:00
|
|
|
strscpy(info->id, t->id, sizeof(info->id));
|
|
|
|
strscpy(info->name, t->name, sizeof(info->name));
|
2024-02-27 16:52:45 +08:00
|
|
|
scoped_guard(spinlock_irq, &t->lock)
|
|
|
|
info->resolution = snd_timer_hw_resolution(t);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (copy_to_user(_info, info, sizeof(*_info)))
|
2024-02-22 19:15:04 +08:00
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2005-10-12 23:20:21 +08:00
|
|
|
static int snd_timer_user_params(struct file *file,
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_params __user *_params)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_user *tu;
|
|
|
|
struct snd_timer_params params;
|
|
|
|
struct snd_timer *t;
|
2005-04-17 06:20:36 +08:00
|
|
|
int err;
|
2005-10-12 23:20:21 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
tu = file->private_data;
|
2007-07-16 17:01:30 +08:00
|
|
|
if (!tu->timeri)
|
|
|
|
return -EBADFD;
|
2005-04-17 06:20:36 +08:00
|
|
|
t = tu->timeri->timer;
|
2007-07-16 17:01:30 +08:00
|
|
|
if (!t)
|
|
|
|
return -EBADFD;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (copy_from_user(¶ms, _params, sizeof(params)))
|
|
|
|
return -EFAULT;
|
2017-02-28 21:49:07 +08:00
|
|
|
if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
|
|
|
|
u64 resolution;
|
|
|
|
|
|
|
|
if (params.ticks < 1) {
|
|
|
|
err = -EINVAL;
|
|
|
|
goto _end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't allow resolution less than 1ms */
|
|
|
|
resolution = snd_timer_resolution(tu->timeri);
|
|
|
|
resolution *= params.ticks;
|
|
|
|
if (resolution < 1000000) {
|
|
|
|
err = -EINVAL;
|
|
|
|
goto _end;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2005-10-12 23:20:21 +08:00
|
|
|
if (params.queue_size > 0 &&
|
|
|
|
(params.queue_size < 32 || params.queue_size > 1024)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
err = -EINVAL;
|
|
|
|
goto _end;
|
|
|
|
}
|
|
|
|
if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
|
|
|
|
(1<<SNDRV_TIMER_EVENT_TICK)|
|
|
|
|
(1<<SNDRV_TIMER_EVENT_START)|
|
|
|
|
(1<<SNDRV_TIMER_EVENT_STOP)|
|
|
|
|
(1<<SNDRV_TIMER_EVENT_CONTINUE)|
|
|
|
|
(1<<SNDRV_TIMER_EVENT_PAUSE)|
|
2005-08-16 17:09:05 +08:00
|
|
|
(1<<SNDRV_TIMER_EVENT_SUSPEND)|
|
|
|
|
(1<<SNDRV_TIMER_EVENT_RESUME)|
|
2005-04-17 06:20:36 +08:00
|
|
|
(1<<SNDRV_TIMER_EVENT_MSTART)|
|
|
|
|
(1<<SNDRV_TIMER_EVENT_MSTOP)|
|
|
|
|
(1<<SNDRV_TIMER_EVENT_MCONTINUE)|
|
2005-08-16 19:05:43 +08:00
|
|
|
(1<<SNDRV_TIMER_EVENT_MPAUSE)|
|
|
|
|
(1<<SNDRV_TIMER_EVENT_MSUSPEND)|
|
2005-08-16 17:09:05 +08:00
|
|
|
(1<<SNDRV_TIMER_EVENT_MRESUME))) {
|
2005-04-17 06:20:36 +08:00
|
|
|
err = -EINVAL;
|
|
|
|
goto _end;
|
|
|
|
}
|
|
|
|
snd_timer_stop(tu->timeri);
|
2024-02-27 16:52:45 +08:00
|
|
|
scoped_guard(spinlock_irq, &t->lock) {
|
|
|
|
tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
|
|
|
|
SNDRV_TIMER_IFLG_EXCLUSIVE|
|
|
|
|
SNDRV_TIMER_IFLG_EARLY_EVENT);
|
|
|
|
if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
|
|
|
|
tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
|
|
|
|
if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
|
|
|
|
tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
|
|
|
|
if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
|
|
|
|
tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
|
|
|
|
}
|
2005-10-12 23:20:21 +08:00
|
|
|
if (params.queue_size > 0 &&
|
|
|
|
(unsigned int)tu->queue_size != params.queue_size) {
|
ALSA: timer: Improve user queue reallocation
ALSA timer may reallocate the user queue upon request, and it happens
at three places for now: at opening, at SNDRV_TIMER_IOCTL_PARAMS, and
at SNDRV_TIMER_IOCTL_SELECT. However, the last one,
snd_timer_user_tselect(), doesn't need to reallocate the buffer since
it doesn't change the queue size. It does just because tu->tread
might have been changed before starting the timer.
Instead of *_SELECT ioctl, we should reallocate the queue at
SNDRV_TIMER_IOCTL_TREAD; then the timer is guaranteed to be stopped,
thus we can reassign the buffer more safely.
This patch implements that with a slight code refactoring.
Essentially, the patch achieves:
- Introduce realloc_user_queue() for (re-)allocating the ring buffer,
and call it from all places. Also, realloc_user_queue() uses
kcalloc() for avoiding possible leaks.
- Add the buffer reallocation at SNDRV_TIMER_IOCTL_TREAD. When it
fails, tu->tread is restored to the old value, too.
- Drop the buffer reallocation at snd_timer_user_tselect().
Tested-by: Alexander Potapenko <glider@google.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2017-06-02 23:16:59 +08:00
|
|
|
err = realloc_user_queue(tu, params.queue_size);
|
|
|
|
if (err < 0)
|
|
|
|
goto _end;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2024-02-27 16:52:45 +08:00
|
|
|
scoped_guard(spinlock_irq, &tu->qlock) {
|
|
|
|
tu->qhead = tu->qtail = tu->qused = 0;
|
|
|
|
if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
|
|
|
|
if (tu->tread) {
|
|
|
|
struct snd_timer_tread64 tread;
|
|
|
|
|
|
|
|
memset(&tread, 0, sizeof(tread));
|
|
|
|
tread.event = SNDRV_TIMER_EVENT_EARLY;
|
|
|
|
tread.tstamp_sec = 0;
|
|
|
|
tread.tstamp_nsec = 0;
|
|
|
|
tread.val = 0;
|
|
|
|
snd_timer_user_append_to_tqueue(tu, &tread);
|
|
|
|
} else {
|
|
|
|
struct snd_timer_read *r = &tu->queue[0];
|
|
|
|
|
|
|
|
r->resolution = 0;
|
|
|
|
r->ticks = 0;
|
|
|
|
tu->qused++;
|
|
|
|
tu->qtail++;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2024-02-27 16:52:45 +08:00
|
|
|
tu->filter = params.filter;
|
|
|
|
tu->ticks = params.ticks;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
err = 0;
|
|
|
|
_end:
|
|
|
|
if (copy_to_user(_params, ¶ms, sizeof(params)))
|
|
|
|
return -EFAULT;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-04-24 20:06:09 +08:00
|
|
|
static int snd_timer_user_status32(struct file *file,
|
|
|
|
struct snd_timer_status32 __user *_status)
|
|
|
|
{
|
|
|
|
struct snd_timer_user *tu;
|
|
|
|
struct snd_timer_status32 status;
|
|
|
|
|
|
|
|
tu = file->private_data;
|
|
|
|
if (!tu->timeri)
|
|
|
|
return -EBADFD;
|
|
|
|
memset(&status, 0, sizeof(status));
|
|
|
|
status.tstamp_sec = tu->tstamp.tv_sec;
|
|
|
|
status.tstamp_nsec = tu->tstamp.tv_nsec;
|
|
|
|
status.resolution = snd_timer_resolution(tu->timeri);
|
|
|
|
status.lost = tu->timeri->lost;
|
|
|
|
status.overrun = tu->overrun;
|
2024-02-27 16:52:45 +08:00
|
|
|
scoped_guard(spinlock_irq, &tu->qlock)
|
|
|
|
status.queue = tu->qused;
|
2018-04-24 20:06:09 +08:00
|
|
|
if (copy_to_user(_status, &status, sizeof(status)))
|
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int snd_timer_user_status64(struct file *file,
|
|
|
|
struct snd_timer_status64 __user *_status)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_user *tu;
|
2018-04-24 20:06:09 +08:00
|
|
|
struct snd_timer_status64 status;
|
2005-10-12 23:20:21 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
tu = file->private_data;
|
2007-07-16 17:01:30 +08:00
|
|
|
if (!tu->timeri)
|
|
|
|
return -EBADFD;
|
2005-04-17 06:20:36 +08:00
|
|
|
memset(&status, 0, sizeof(status));
|
2018-04-24 20:06:09 +08:00
|
|
|
status.tstamp_sec = tu->tstamp.tv_sec;
|
|
|
|
status.tstamp_nsec = tu->tstamp.tv_nsec;
|
2005-04-17 06:20:36 +08:00
|
|
|
status.resolution = snd_timer_resolution(tu->timeri);
|
|
|
|
status.lost = tu->timeri->lost;
|
|
|
|
status.overrun = tu->overrun;
|
2024-02-27 16:52:45 +08:00
|
|
|
scoped_guard(spinlock_irq, &tu->qlock)
|
|
|
|
status.queue = tu->qused;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (copy_to_user(_status, &status, sizeof(status)))
|
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int snd_timer_user_start(struct file *file)
|
|
|
|
{
|
|
|
|
int err;
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_user *tu;
|
2005-10-12 23:20:21 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
tu = file->private_data;
|
2007-07-16 17:01:30 +08:00
|
|
|
if (!tu->timeri)
|
|
|
|
return -EBADFD;
|
2005-04-17 06:20:36 +08:00
|
|
|
snd_timer_stop(tu->timeri);
|
|
|
|
tu->timeri->lost = 0;
|
|
|
|
tu->last_resolution = 0;
|
2019-03-29 00:44:53 +08:00
|
|
|
err = snd_timer_start(tu->timeri, tu->ticks);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int snd_timer_user_stop(struct file *file)
|
|
|
|
{
|
|
|
|
int err;
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_user *tu;
|
2005-10-12 23:20:21 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
tu = file->private_data;
|
2007-07-16 17:01:30 +08:00
|
|
|
if (!tu->timeri)
|
|
|
|
return -EBADFD;
|
2019-03-29 00:44:53 +08:00
|
|
|
err = snd_timer_stop(tu->timeri);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int snd_timer_user_continue(struct file *file)
|
|
|
|
{
|
|
|
|
int err;
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_user *tu;
|
2005-10-12 23:20:21 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
tu = file->private_data;
|
2007-07-16 17:01:30 +08:00
|
|
|
if (!tu->timeri)
|
|
|
|
return -EBADFD;
|
2016-09-07 21:45:31 +08:00
|
|
|
/* start timer instead of continue if it's not used before */
|
|
|
|
if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED))
|
|
|
|
return snd_timer_user_start(file);
|
2005-04-17 06:20:36 +08:00
|
|
|
tu->timeri->lost = 0;
|
2019-03-29 00:44:53 +08:00
|
|
|
err = snd_timer_continue(tu->timeri);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2005-05-15 21:04:14 +08:00
|
|
|
static int snd_timer_user_pause(struct file *file)
|
|
|
|
{
|
|
|
|
int err;
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_user *tu;
|
2005-10-12 23:20:21 +08:00
|
|
|
|
2005-05-15 21:04:14 +08:00
|
|
|
tu = file->private_data;
|
2007-07-16 17:01:30 +08:00
|
|
|
if (!tu->timeri)
|
|
|
|
return -EBADFD;
|
2019-03-29 00:44:53 +08:00
|
|
|
err = snd_timer_pause(tu->timeri);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
return 0;
|
2005-05-15 21:04:14 +08:00
|
|
|
}
|
|
|
|
|
2018-04-24 20:06:13 +08:00
|
|
|
static int snd_timer_user_tread(void __user *argp, struct snd_timer_user *tu,
|
|
|
|
unsigned int cmd, bool compat)
|
|
|
|
{
|
|
|
|
int __user *p = argp;
|
|
|
|
int xarg, old_tread;
|
|
|
|
|
|
|
|
if (tu->timeri) /* too late */
|
|
|
|
return -EBUSY;
|
|
|
|
if (get_user(xarg, p))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
old_tread = tu->tread;
|
|
|
|
|
|
|
|
if (!xarg)
|
|
|
|
tu->tread = TREAD_FORMAT_NONE;
|
|
|
|
else if (cmd == SNDRV_TIMER_IOCTL_TREAD64 ||
|
|
|
|
(IS_ENABLED(CONFIG_64BIT) && !compat))
|
|
|
|
tu->tread = TREAD_FORMAT_TIME64;
|
|
|
|
else
|
|
|
|
tu->tread = TREAD_FORMAT_TIME32;
|
|
|
|
|
|
|
|
if (tu->tread != old_tread &&
|
|
|
|
realloc_user_queue(tu, tu->queue_size) < 0) {
|
|
|
|
tu->tread = old_tread;
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-05-15 21:43:54 +08:00
|
|
|
enum {
|
|
|
|
SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
|
|
|
|
SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
|
|
|
|
SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
|
|
|
|
SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
|
|
|
|
};
|
|
|
|
|
2016-01-14 00:48:01 +08:00
|
|
|
static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd,
|
2018-04-24 20:06:13 +08:00
|
|
|
unsigned long arg, bool compat)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_user *tu;
|
2005-04-17 06:20:36 +08:00
|
|
|
void __user *argp = (void __user *)arg;
|
|
|
|
int __user *p = argp;
|
2005-10-12 23:20:21 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
tu = file->private_data;
|
|
|
|
switch (cmd) {
|
|
|
|
case SNDRV_TIMER_IOCTL_PVERSION:
|
|
|
|
return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
|
|
|
|
case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
|
|
|
|
return snd_timer_user_next_device(argp);
|
2018-04-24 20:06:13 +08:00
|
|
|
case SNDRV_TIMER_IOCTL_TREAD_OLD:
|
|
|
|
case SNDRV_TIMER_IOCTL_TREAD64:
|
|
|
|
return snd_timer_user_tread(argp, tu, cmd, compat);
|
2005-04-17 06:20:36 +08:00
|
|
|
case SNDRV_TIMER_IOCTL_GINFO:
|
|
|
|
return snd_timer_user_ginfo(file, argp);
|
|
|
|
case SNDRV_TIMER_IOCTL_GPARAMS:
|
|
|
|
return snd_timer_user_gparams(file, argp);
|
|
|
|
case SNDRV_TIMER_IOCTL_GSTATUS:
|
|
|
|
return snd_timer_user_gstatus(file, argp);
|
|
|
|
case SNDRV_TIMER_IOCTL_SELECT:
|
|
|
|
return snd_timer_user_tselect(file, argp);
|
|
|
|
case SNDRV_TIMER_IOCTL_INFO:
|
|
|
|
return snd_timer_user_info(file, argp);
|
|
|
|
case SNDRV_TIMER_IOCTL_PARAMS:
|
|
|
|
return snd_timer_user_params(file, argp);
|
2018-04-24 20:06:09 +08:00
|
|
|
case SNDRV_TIMER_IOCTL_STATUS32:
|
|
|
|
return snd_timer_user_status32(file, argp);
|
|
|
|
case SNDRV_TIMER_IOCTL_STATUS64:
|
|
|
|
return snd_timer_user_status64(file, argp);
|
2005-04-17 06:20:36 +08:00
|
|
|
case SNDRV_TIMER_IOCTL_START:
|
2005-05-15 21:43:54 +08:00
|
|
|
case SNDRV_TIMER_IOCTL_START_OLD:
|
2005-04-17 06:20:36 +08:00
|
|
|
return snd_timer_user_start(file);
|
|
|
|
case SNDRV_TIMER_IOCTL_STOP:
|
2005-05-15 21:43:54 +08:00
|
|
|
case SNDRV_TIMER_IOCTL_STOP_OLD:
|
2005-04-17 06:20:36 +08:00
|
|
|
return snd_timer_user_stop(file);
|
|
|
|
case SNDRV_TIMER_IOCTL_CONTINUE:
|
2005-05-15 21:43:54 +08:00
|
|
|
case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
|
2005-04-17 06:20:36 +08:00
|
|
|
return snd_timer_user_continue(file);
|
2005-05-15 21:04:14 +08:00
|
|
|
case SNDRV_TIMER_IOCTL_PAUSE:
|
2005-05-15 21:43:54 +08:00
|
|
|
case SNDRV_TIMER_IOCTL_PAUSE_OLD:
|
2005-05-15 21:04:14 +08:00
|
|
|
return snd_timer_user_pause(file);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
return -ENOTTY;
|
|
|
|
}
|
|
|
|
|
2016-01-14 00:48:01 +08:00
|
|
|
static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
|
|
|
{
|
|
|
|
struct snd_timer_user *tu = file->private_data;
|
|
|
|
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(mutex)(&tu->ioctl_lock);
|
|
|
|
return __snd_timer_user_ioctl(file, cmd, arg, false);
|
2016-01-14 00:48:01 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static int snd_timer_user_fasync(int fd, struct file * file, int on)
|
|
|
|
{
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_user *tu;
|
2005-10-12 23:20:21 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
tu = file->private_data;
|
2022-07-28 20:59:43 +08:00
|
|
|
return snd_fasync_helper(fd, file, on, &tu->fasync);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2005-10-12 23:20:21 +08:00
|
|
|
static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
|
|
|
|
size_t count, loff_t *offset)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2018-04-24 20:06:13 +08:00
|
|
|
struct snd_timer_tread64 *tread;
|
|
|
|
struct snd_timer_tread32 tread32;
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_user *tu;
|
2005-04-17 06:20:36 +08:00
|
|
|
long result = 0, unit;
|
2016-02-09 00:26:58 +08:00
|
|
|
int qhead;
|
2005-04-17 06:20:36 +08:00
|
|
|
int err = 0;
|
2005-10-12 23:20:21 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
tu = file->private_data;
|
2018-04-24 20:06:13 +08:00
|
|
|
switch (tu->tread) {
|
|
|
|
case TREAD_FORMAT_TIME64:
|
|
|
|
unit = sizeof(struct snd_timer_tread64);
|
|
|
|
break;
|
|
|
|
case TREAD_FORMAT_TIME32:
|
|
|
|
unit = sizeof(struct snd_timer_tread32);
|
|
|
|
break;
|
|
|
|
case TREAD_FORMAT_NONE:
|
|
|
|
unit = sizeof(struct snd_timer_read);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WARN_ONCE(1, "Corrupt snd_timer_user\n");
|
|
|
|
return -ENOTSUPP;
|
|
|
|
}
|
|
|
|
|
2017-06-02 21:03:38 +08:00
|
|
|
mutex_lock(&tu->ioctl_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_lock_irq(&tu->qlock);
|
|
|
|
while ((long)count - result >= unit) {
|
|
|
|
while (!tu->qused) {
|
2017-06-20 18:06:13 +08:00
|
|
|
wait_queue_entry_t wait;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
|
|
|
|
err = -EAGAIN;
|
2016-02-09 00:26:58 +08:00
|
|
|
goto _error;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
|
|
|
init_waitqueue_entry(&wait, current);
|
|
|
|
add_wait_queue(&tu->qchange_sleep, &wait);
|
|
|
|
|
|
|
|
spin_unlock_irq(&tu->qlock);
|
2017-06-02 21:03:38 +08:00
|
|
|
mutex_unlock(&tu->ioctl_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
schedule();
|
2017-06-02 21:03:38 +08:00
|
|
|
mutex_lock(&tu->ioctl_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_lock_irq(&tu->qlock);
|
|
|
|
|
|
|
|
remove_wait_queue(&tu->qchange_sleep, &wait);
|
|
|
|
|
2016-01-22 00:19:31 +08:00
|
|
|
if (tu->disconnected) {
|
|
|
|
err = -ENODEV;
|
2016-02-09 00:26:58 +08:00
|
|
|
goto _error;
|
2016-01-22 00:19:31 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
if (signal_pending(current)) {
|
|
|
|
err = -ERESTARTSYS;
|
2016-02-09 00:26:58 +08:00
|
|
|
goto _error;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 00:26:58 +08:00
|
|
|
qhead = tu->qhead++;
|
|
|
|
tu->qhead %= tu->queue_size;
|
2016-07-04 20:02:15 +08:00
|
|
|
tu->qused--;
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_unlock_irq(&tu->qlock);
|
|
|
|
|
2018-04-24 20:06:13 +08:00
|
|
|
tread = &tu->tqueue[qhead];
|
|
|
|
|
|
|
|
switch (tu->tread) {
|
|
|
|
case TREAD_FORMAT_TIME64:
|
|
|
|
if (copy_to_user(buffer, tread,
|
|
|
|
sizeof(struct snd_timer_tread64)))
|
2005-04-17 06:20:36 +08:00
|
|
|
err = -EFAULT;
|
2018-04-24 20:06:13 +08:00
|
|
|
break;
|
|
|
|
case TREAD_FORMAT_TIME32:
|
|
|
|
memset(&tread32, 0, sizeof(tread32));
|
|
|
|
tread32 = (struct snd_timer_tread32) {
|
|
|
|
.event = tread->event,
|
|
|
|
.tstamp_sec = tread->tstamp_sec,
|
2020-01-12 04:33:25 +08:00
|
|
|
.tstamp_nsec = tread->tstamp_nsec,
|
2018-04-24 20:06:13 +08:00
|
|
|
.val = tread->val,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (copy_to_user(buffer, &tread32, sizeof(tread32)))
|
|
|
|
err = -EFAULT;
|
|
|
|
break;
|
|
|
|
case TREAD_FORMAT_NONE:
|
2016-02-09 00:26:58 +08:00
|
|
|
if (copy_to_user(buffer, &tu->queue[qhead],
|
|
|
|
sizeof(struct snd_timer_read)))
|
2005-04-17 06:20:36 +08:00
|
|
|
err = -EFAULT;
|
2018-04-24 20:06:13 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err = -ENOTSUPP;
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock_irq(&tu->qlock);
|
2016-02-09 00:26:58 +08:00
|
|
|
if (err < 0)
|
|
|
|
goto _error;
|
|
|
|
result += unit;
|
|
|
|
buffer += unit;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
_error:
|
2016-02-09 00:26:58 +08:00
|
|
|
spin_unlock_irq(&tu->qlock);
|
2017-06-02 21:03:38 +08:00
|
|
|
mutex_unlock(&tu->ioctl_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
return result > 0 ? result : err;
|
|
|
|
}
|
|
|
|
|
2017-07-03 11:27:36 +08:00
|
|
|
static __poll_t snd_timer_user_poll(struct file *file, poll_table * wait)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2017-07-03 11:27:36 +08:00
|
|
|
__poll_t mask;
|
2005-11-17 20:56:05 +08:00
|
|
|
struct snd_timer_user *tu;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
tu = file->private_data;
|
|
|
|
|
|
|
|
poll_wait(file, &tu->qchange_sleep, wait);
|
2005-10-12 23:20:21 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
mask = 0;
|
2024-02-27 16:52:45 +08:00
|
|
|
guard(spinlock_irq)(&tu->qlock);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (tu->qused)
|
2018-02-12 06:34:03 +08:00
|
|
|
mask |= EPOLLIN | EPOLLRDNORM;
|
2016-01-22 00:19:31 +08:00
|
|
|
if (tu->disconnected)
|
2018-02-12 06:34:03 +08:00
|
|
|
mask |= EPOLLERR;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
#include "timer_compat.c"
|
|
|
|
#else
|
|
|
|
#define snd_timer_user_ioctl_compat NULL
|
|
|
|
#endif
|
|
|
|
|
2007-02-12 16:55:37 +08:00
|
|
|
static const struct file_operations snd_timer_f_ops =
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.read = snd_timer_user_read,
|
|
|
|
.open = snd_timer_user_open,
|
|
|
|
.release = snd_timer_user_release,
|
2010-04-13 17:49:04 +08:00
|
|
|
.llseek = no_llseek,
|
2005-04-17 06:20:36 +08:00
|
|
|
.poll = snd_timer_user_poll,
|
|
|
|
.unlocked_ioctl = snd_timer_user_ioctl,
|
|
|
|
.compat_ioctl = snd_timer_user_ioctl_compat,
|
|
|
|
.fasync = snd_timer_user_fasync,
|
|
|
|
};
|
|
|
|
|
2015-01-30 01:09:05 +08:00
|
|
|
/* unregister the system timer */
|
|
|
|
static void snd_timer_free_all(void)
|
|
|
|
{
|
|
|
|
struct snd_timer *timer, *n;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(timer, n, &snd_timer_list, device_list)
|
|
|
|
snd_timer_free(timer);
|
|
|
|
}
|
|
|
|
|
2023-08-17 00:02:50 +08:00
|
|
|
static struct device *timer_dev;
|
2015-01-30 01:12:26 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* ENTRY functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int __init alsa_timer_init(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2023-08-17 00:02:50 +08:00
|
|
|
err = snd_device_alloc(&timer_dev, NULL);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
dev_set_name(timer_dev, "timer");
|
2015-01-30 01:12:26 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
#ifdef SNDRV_OSS_INFO_DEV_TIMERS
|
2005-10-12 23:20:21 +08:00
|
|
|
snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
|
|
|
|
"system timer");
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
2005-12-01 17:42:42 +08:00
|
|
|
|
2015-01-30 01:09:05 +08:00
|
|
|
err = snd_timer_register_system();
|
|
|
|
if (err < 0) {
|
2014-02-05 01:22:39 +08:00
|
|
|
pr_err("ALSA: unable to register system timer (%i)\n", err);
|
2017-08-23 15:30:41 +08:00
|
|
|
goto put_timer;
|
2015-01-30 01:09:05 +08:00
|
|
|
}
|
|
|
|
|
2015-01-30 15:34:58 +08:00
|
|
|
err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
|
2023-08-17 00:02:50 +08:00
|
|
|
&snd_timer_f_ops, NULL, timer_dev);
|
2015-01-30 01:09:05 +08:00
|
|
|
if (err < 0) {
|
2014-02-05 01:22:39 +08:00
|
|
|
pr_err("ALSA: unable to register timer device (%i)\n", err);
|
2015-01-30 01:09:05 +08:00
|
|
|
snd_timer_free_all();
|
2017-08-23 15:30:41 +08:00
|
|
|
goto put_timer;
|
2015-01-30 01:09:05 +08:00
|
|
|
}
|
|
|
|
|
2005-12-01 17:42:42 +08:00
|
|
|
snd_timer_proc_init();
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
2017-08-23 15:30:41 +08:00
|
|
|
|
|
|
|
put_timer:
|
2023-08-17 00:02:50 +08:00
|
|
|
put_device(timer_dev);
|
2017-08-23 15:30:41 +08:00
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit alsa_timer_exit(void)
|
|
|
|
{
|
2023-08-17 00:02:50 +08:00
|
|
|
snd_unregister_device(timer_dev);
|
2015-01-30 01:09:05 +08:00
|
|
|
snd_timer_free_all();
|
2023-08-17 00:02:50 +08:00
|
|
|
put_device(timer_dev);
|
2005-12-01 17:42:42 +08:00
|
|
|
snd_timer_proc_done();
|
2005-04-17 06:20:36 +08:00
|
|
|
#ifdef SNDRV_OSS_INFO_DEV_TIMERS
|
|
|
|
snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(alsa_timer_init)
|
|
|
|
module_exit(alsa_timer_exit)
|