xref: /openbmc/linux/sound/core/misc.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  Misc and compatibility things
4c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds #include <linux/init.h>
8d81a6d71SPaul Gortmaker #include <linux/export.h>
931623caaSPaul Gortmaker #include <linux/moduleparam.h>
101da177e4SLinus Torvalds #include <linux/time.h>
115a0e3ad6STejun Heo #include <linux/slab.h>
12b1d5776dSTakashi Iwai #include <linux/ioport.h>
13*ef34a0aeSTakashi Iwai #include <linux/fs.h>
141da177e4SLinus Torvalds #include <sound/core.h>
151da177e4SLinus Torvalds 
1636ce99c1STakashi Iwai #ifdef CONFIG_SND_DEBUG
1736ce99c1STakashi Iwai 
1836ce99c1STakashi Iwai #ifdef CONFIG_SND_DEBUG_VERBOSE
1936ce99c1STakashi Iwai #define DEFAULT_DEBUG_LEVEL	2
2036ce99c1STakashi Iwai #else
2136ce99c1STakashi Iwai #define DEFAULT_DEBUG_LEVEL	1
2236ce99c1STakashi Iwai #endif
2336ce99c1STakashi Iwai 
2436ce99c1STakashi Iwai static int debug = DEFAULT_DEBUG_LEVEL;
2536ce99c1STakashi Iwai module_param(debug, int, 0644);
2636ce99c1STakashi Iwai MODULE_PARM_DESC(debug, "Debug level (0 = disable)");
2736ce99c1STakashi Iwai 
2836ce99c1STakashi Iwai #endif /* CONFIG_SND_DEBUG */
2936ce99c1STakashi Iwai 
release_and_free_resource(struct resource * res)30b1d5776dSTakashi Iwai void release_and_free_resource(struct resource *res)
31b1d5776dSTakashi Iwai {
32b1d5776dSTakashi Iwai 	if (res) {
33b1d5776dSTakashi Iwai 		release_resource(res);
34e38e0cfaSTakashi Iwai 		kfree(res);
35b1d5776dSTakashi Iwai 	}
36b1d5776dSTakashi Iwai }
37c0d3fb39STakashi Iwai EXPORT_SYMBOL(release_and_free_resource);
38c0d3fb39STakashi Iwai 
391da177e4SLinus Torvalds #ifdef CONFIG_SND_VERBOSE_PRINTK
4036ce99c1STakashi Iwai /* strip the leading path if the given path is absolute */
sanity_file_name(const char * path)411b0053a0STakashi Iwai static const char *sanity_file_name(const char *path)
421da177e4SLinus Torvalds {
431b0053a0STakashi Iwai 	if (*path == '/')
441b0053a0STakashi Iwai 		return strrchr(path, '/') + 1;
451b0053a0STakashi Iwai 	else
461b0053a0STakashi Iwai 		return path;
471b0053a0STakashi Iwai }
481da177e4SLinus Torvalds #endif
491da177e4SLinus Torvalds 
5036ce99c1STakashi Iwai #if defined(CONFIG_SND_DEBUG) || defined(CONFIG_SND_VERBOSE_PRINTK)
__snd_printk(unsigned int level,const char * path,int line,const char * format,...)5136ce99c1STakashi Iwai void __snd_printk(unsigned int level, const char *path, int line,
5236ce99c1STakashi Iwai 		  const char *format, ...)
531da177e4SLinus Torvalds {
541da177e4SLinus Torvalds 	va_list args;
55890ee02aSTakashi Iwai #ifdef CONFIG_SND_VERBOSE_PRINTK
56b778b3f2SJoe Perches 	int kern_level;
57890ee02aSTakashi Iwai 	struct va_format vaf;
58890ee02aSTakashi Iwai 	char verbose_fmt[] = KERN_DEFAULT "ALSA %s:%d %pV";
590a4824bfSPetr Mladek 	bool level_found = false;
60890ee02aSTakashi Iwai #endif
611da177e4SLinus Torvalds 
6236ce99c1STakashi Iwai #ifdef CONFIG_SND_DEBUG
6336ce99c1STakashi Iwai 	if (debug < level)
6436ce99c1STakashi Iwai 		return;
6536ce99c1STakashi Iwai #endif
66890ee02aSTakashi Iwai 
671da177e4SLinus Torvalds 	va_start(args, format);
68890ee02aSTakashi Iwai #ifdef CONFIG_SND_VERBOSE_PRINTK
69890ee02aSTakashi Iwai 	vaf.fmt = format;
70890ee02aSTakashi Iwai 	vaf.va = &args;
71b778b3f2SJoe Perches 
720a4824bfSPetr Mladek 	while ((kern_level = printk_get_level(vaf.fmt)) != 0) {
730a4824bfSPetr Mladek 		const char *end_of_header = printk_skip_level(vaf.fmt);
74b778b3f2SJoe Perches 
750a4824bfSPetr Mladek 		/* Ignore KERN_CONT. We print filename:line for each piece. */
760a4824bfSPetr Mladek 		if (kern_level >= '0' && kern_level <= '7') {
770a4824bfSPetr Mladek 			memcpy(verbose_fmt, vaf.fmt, end_of_header - vaf.fmt);
780a4824bfSPetr Mladek 			level_found = true;
790a4824bfSPetr Mladek 		}
800a4824bfSPetr Mladek 
810a4824bfSPetr Mladek 		vaf.fmt = end_of_header;
820a4824bfSPetr Mladek 	}
830a4824bfSPetr Mladek 
840a4824bfSPetr Mladek 	if (!level_found && level)
850a4824bfSPetr Mladek 		memcpy(verbose_fmt, KERN_DEBUG, sizeof(KERN_DEBUG) - 1);
860a4824bfSPetr Mladek 
870a4824bfSPetr Mladek 	printk(verbose_fmt, sanity_file_name(path), line, &vaf);
88890ee02aSTakashi Iwai #else
891da177e4SLinus Torvalds 	vprintk(format, args);
90890ee02aSTakashi Iwai #endif
911da177e4SLinus Torvalds 	va_end(args);
921da177e4SLinus Torvalds }
9336ce99c1STakashi Iwai EXPORT_SYMBOL_GPL(__snd_printk);
941da177e4SLinus Torvalds #endif
95d9ea472cSTakashi Iwai 
96d9ea472cSTakashi Iwai #ifdef CONFIG_PCI
97d9ea472cSTakashi Iwai #include <linux/pci.h>
98d9ea472cSTakashi Iwai /**
99d1458279STakashi Iwai  * snd_pci_quirk_lookup_id - look up a PCI SSID quirk list
100d1458279STakashi Iwai  * @vendor: PCI SSV id
101d1458279STakashi Iwai  * @device: PCI SSD id
102d1458279STakashi Iwai  * @list: quirk list, terminated by a null entry
103d1458279STakashi Iwai  *
104d1458279STakashi Iwai  * Look through the given quirk list and finds a matching entry
105d1458279STakashi Iwai  * with the same PCI SSID.  When subdevice is 0, all subdevice
106d1458279STakashi Iwai  * values may match.
107d1458279STakashi Iwai  *
108d1458279STakashi Iwai  * Returns the matched entry pointer, or NULL if nothing matched.
109d1458279STakashi Iwai  */
110d1458279STakashi Iwai const struct snd_pci_quirk *
snd_pci_quirk_lookup_id(u16 vendor,u16 device,const struct snd_pci_quirk * list)111d1458279STakashi Iwai snd_pci_quirk_lookup_id(u16 vendor, u16 device,
112d1458279STakashi Iwai 			const struct snd_pci_quirk *list)
113d1458279STakashi Iwai {
114d1458279STakashi Iwai 	const struct snd_pci_quirk *q;
115d1458279STakashi Iwai 
1165576c4f2STakashi Iwai 	for (q = list; q->subvendor || q->subdevice; q++) {
117d1458279STakashi Iwai 		if (q->subvendor != vendor)
118d1458279STakashi Iwai 			continue;
119d1458279STakashi Iwai 		if (!q->subdevice ||
120d1458279STakashi Iwai 		    (device & q->subdevice_mask) == q->subdevice)
121d1458279STakashi Iwai 			return q;
122d1458279STakashi Iwai 	}
123d1458279STakashi Iwai 	return NULL;
124d1458279STakashi Iwai }
125d1458279STakashi Iwai EXPORT_SYMBOL(snd_pci_quirk_lookup_id);
126d1458279STakashi Iwai 
127d1458279STakashi Iwai /**
128d9ea472cSTakashi Iwai  * snd_pci_quirk_lookup - look up a PCI SSID quirk list
129d9ea472cSTakashi Iwai  * @pci: pci_dev handle
130d9ea472cSTakashi Iwai  * @list: quirk list, terminated by a null entry
131d9ea472cSTakashi Iwai  *
132d9ea472cSTakashi Iwai  * Look through the given quirk list and finds a matching entry
133d9ea472cSTakashi Iwai  * with the same PCI SSID.  When subdevice is 0, all subdevice
134d9ea472cSTakashi Iwai  * values may match.
135d9ea472cSTakashi Iwai  *
136d9ea472cSTakashi Iwai  * Returns the matched entry pointer, or NULL if nothing matched.
137d9ea472cSTakashi Iwai  */
138d9ea472cSTakashi Iwai const struct snd_pci_quirk *
snd_pci_quirk_lookup(struct pci_dev * pci,const struct snd_pci_quirk * list)139d9ea472cSTakashi Iwai snd_pci_quirk_lookup(struct pci_dev *pci, const struct snd_pci_quirk *list)
140d9ea472cSTakashi Iwai {
141e5b50adaSTakashi Iwai 	if (!pci)
142e5b50adaSTakashi Iwai 		return NULL;
143d1458279STakashi Iwai 	return snd_pci_quirk_lookup_id(pci->subsystem_vendor,
144d1458279STakashi Iwai 				       pci->subsystem_device,
145d1458279STakashi Iwai 				       list);
146d9ea472cSTakashi Iwai }
147d9ea472cSTakashi Iwai EXPORT_SYMBOL(snd_pci_quirk_lookup);
148d9ea472cSTakashi Iwai #endif
149*ef34a0aeSTakashi Iwai 
150*ef34a0aeSTakashi Iwai /*
151*ef34a0aeSTakashi Iwai  * Deferred async signal helpers
152*ef34a0aeSTakashi Iwai  *
153*ef34a0aeSTakashi Iwai  * Below are a few helper functions to wrap the async signal handling
154*ef34a0aeSTakashi Iwai  * in the deferred work.  The main purpose is to avoid the messy deadlock
155*ef34a0aeSTakashi Iwai  * around tasklist_lock and co at the kill_fasync() invocation.
156*ef34a0aeSTakashi Iwai  * fasync_helper() and kill_fasync() are replaced with snd_fasync_helper()
157*ef34a0aeSTakashi Iwai  * and snd_kill_fasync(), respectively.  In addition, snd_fasync_free() has
158*ef34a0aeSTakashi Iwai  * to be called at releasing the relevant file object.
159*ef34a0aeSTakashi Iwai  */
160*ef34a0aeSTakashi Iwai struct snd_fasync {
161*ef34a0aeSTakashi Iwai 	struct fasync_struct *fasync;
162*ef34a0aeSTakashi Iwai 	int signal;
163*ef34a0aeSTakashi Iwai 	int poll;
164*ef34a0aeSTakashi Iwai 	int on;
165*ef34a0aeSTakashi Iwai 	struct list_head list;
166*ef34a0aeSTakashi Iwai };
167*ef34a0aeSTakashi Iwai 
168*ef34a0aeSTakashi Iwai static DEFINE_SPINLOCK(snd_fasync_lock);
169*ef34a0aeSTakashi Iwai static LIST_HEAD(snd_fasync_list);
170*ef34a0aeSTakashi Iwai 
snd_fasync_work_fn(struct work_struct * work)171*ef34a0aeSTakashi Iwai static void snd_fasync_work_fn(struct work_struct *work)
172*ef34a0aeSTakashi Iwai {
173*ef34a0aeSTakashi Iwai 	struct snd_fasync *fasync;
174*ef34a0aeSTakashi Iwai 
175*ef34a0aeSTakashi Iwai 	spin_lock_irq(&snd_fasync_lock);
176*ef34a0aeSTakashi Iwai 	while (!list_empty(&snd_fasync_list)) {
177*ef34a0aeSTakashi Iwai 		fasync = list_first_entry(&snd_fasync_list, struct snd_fasync, list);
178*ef34a0aeSTakashi Iwai 		list_del_init(&fasync->list);
179*ef34a0aeSTakashi Iwai 		spin_unlock_irq(&snd_fasync_lock);
180*ef34a0aeSTakashi Iwai 		if (fasync->on)
181*ef34a0aeSTakashi Iwai 			kill_fasync(&fasync->fasync, fasync->signal, fasync->poll);
182*ef34a0aeSTakashi Iwai 		spin_lock_irq(&snd_fasync_lock);
183*ef34a0aeSTakashi Iwai 	}
184*ef34a0aeSTakashi Iwai 	spin_unlock_irq(&snd_fasync_lock);
185*ef34a0aeSTakashi Iwai }
186*ef34a0aeSTakashi Iwai 
187*ef34a0aeSTakashi Iwai static DECLARE_WORK(snd_fasync_work, snd_fasync_work_fn);
188*ef34a0aeSTakashi Iwai 
snd_fasync_helper(int fd,struct file * file,int on,struct snd_fasync ** fasyncp)189*ef34a0aeSTakashi Iwai int snd_fasync_helper(int fd, struct file *file, int on,
190*ef34a0aeSTakashi Iwai 		      struct snd_fasync **fasyncp)
191*ef34a0aeSTakashi Iwai {
192*ef34a0aeSTakashi Iwai 	struct snd_fasync *fasync = NULL;
193*ef34a0aeSTakashi Iwai 
194*ef34a0aeSTakashi Iwai 	if (on) {
195*ef34a0aeSTakashi Iwai 		fasync = kzalloc(sizeof(*fasync), GFP_KERNEL);
196*ef34a0aeSTakashi Iwai 		if (!fasync)
197*ef34a0aeSTakashi Iwai 			return -ENOMEM;
198*ef34a0aeSTakashi Iwai 		INIT_LIST_HEAD(&fasync->list);
199*ef34a0aeSTakashi Iwai 	}
200*ef34a0aeSTakashi Iwai 
201*ef34a0aeSTakashi Iwai 	spin_lock_irq(&snd_fasync_lock);
202*ef34a0aeSTakashi Iwai 	if (*fasyncp) {
203*ef34a0aeSTakashi Iwai 		kfree(fasync);
204*ef34a0aeSTakashi Iwai 		fasync = *fasyncp;
205*ef34a0aeSTakashi Iwai 	} else {
206*ef34a0aeSTakashi Iwai 		if (!fasync) {
207*ef34a0aeSTakashi Iwai 			spin_unlock_irq(&snd_fasync_lock);
208*ef34a0aeSTakashi Iwai 			return 0;
209*ef34a0aeSTakashi Iwai 		}
210*ef34a0aeSTakashi Iwai 		*fasyncp = fasync;
211*ef34a0aeSTakashi Iwai 	}
212*ef34a0aeSTakashi Iwai 	fasync->on = on;
213*ef34a0aeSTakashi Iwai 	spin_unlock_irq(&snd_fasync_lock);
214*ef34a0aeSTakashi Iwai 	return fasync_helper(fd, file, on, &fasync->fasync);
215*ef34a0aeSTakashi Iwai }
216*ef34a0aeSTakashi Iwai EXPORT_SYMBOL_GPL(snd_fasync_helper);
217*ef34a0aeSTakashi Iwai 
snd_kill_fasync(struct snd_fasync * fasync,int signal,int poll)218*ef34a0aeSTakashi Iwai void snd_kill_fasync(struct snd_fasync *fasync, int signal, int poll)
219*ef34a0aeSTakashi Iwai {
220*ef34a0aeSTakashi Iwai 	unsigned long flags;
221*ef34a0aeSTakashi Iwai 
222*ef34a0aeSTakashi Iwai 	if (!fasync || !fasync->on)
223*ef34a0aeSTakashi Iwai 		return;
224*ef34a0aeSTakashi Iwai 	spin_lock_irqsave(&snd_fasync_lock, flags);
225*ef34a0aeSTakashi Iwai 	fasync->signal = signal;
226*ef34a0aeSTakashi Iwai 	fasync->poll = poll;
227*ef34a0aeSTakashi Iwai 	list_move(&fasync->list, &snd_fasync_list);
228*ef34a0aeSTakashi Iwai 	schedule_work(&snd_fasync_work);
229*ef34a0aeSTakashi Iwai 	spin_unlock_irqrestore(&snd_fasync_lock, flags);
230*ef34a0aeSTakashi Iwai }
231*ef34a0aeSTakashi Iwai EXPORT_SYMBOL_GPL(snd_kill_fasync);
232*ef34a0aeSTakashi Iwai 
snd_fasync_free(struct snd_fasync * fasync)233*ef34a0aeSTakashi Iwai void snd_fasync_free(struct snd_fasync *fasync)
234*ef34a0aeSTakashi Iwai {
235*ef34a0aeSTakashi Iwai 	if (!fasync)
236*ef34a0aeSTakashi Iwai 		return;
237*ef34a0aeSTakashi Iwai 	fasync->on = 0;
238*ef34a0aeSTakashi Iwai 	flush_work(&snd_fasync_work);
239*ef34a0aeSTakashi Iwai 	kfree(fasync);
240*ef34a0aeSTakashi Iwai }
241*ef34a0aeSTakashi Iwai EXPORT_SYMBOL_GPL(snd_fasync_free);
242