misc.c 3.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Linus Torvalds 已提交
2 3
/*
 *  Misc and compatibility things
4
 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
L
Linus Torvalds 已提交
5 6 7
 */

#include <linux/init.h>
8
#include <linux/export.h>
9
#include <linux/moduleparam.h>
L
Linus Torvalds 已提交
10
#include <linux/time.h>
11
#include <linux/slab.h>
12
#include <linux/ioport.h>
L
Linus Torvalds 已提交
13 14
#include <sound/core.h>

T
Takashi Iwai 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28
#ifdef CONFIG_SND_DEBUG

#ifdef CONFIG_SND_DEBUG_VERBOSE
#define DEFAULT_DEBUG_LEVEL	2
#else
#define DEFAULT_DEBUG_LEVEL	1
#endif

static int debug = DEFAULT_DEBUG_LEVEL;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "Debug level (0 = disable)");

#endif /* CONFIG_SND_DEBUG */

29 30 31 32
void release_and_free_resource(struct resource *res)
{
	if (res) {
		release_resource(res);
T
Takashi Iwai 已提交
33
		kfree(res);
34 35
	}
}
36 37
EXPORT_SYMBOL(release_and_free_resource);

L
Linus Torvalds 已提交
38
#ifdef CONFIG_SND_VERBOSE_PRINTK
T
Takashi Iwai 已提交
39
/* strip the leading path if the given path is absolute */
40
static const char *sanity_file_name(const char *path)
L
Linus Torvalds 已提交
41
{
42 43 44 45 46
	if (*path == '/')
		return strrchr(path, '/') + 1;
	else
		return path;
}
L
Linus Torvalds 已提交
47 48
#endif

T
Takashi Iwai 已提交
49 50 51
#if defined(CONFIG_SND_DEBUG) || defined(CONFIG_SND_VERBOSE_PRINTK)
void __snd_printk(unsigned int level, const char *path, int line,
		  const char *format, ...)
L
Linus Torvalds 已提交
52 53
{
	va_list args;
T
Takashi Iwai 已提交
54
#ifdef CONFIG_SND_VERBOSE_PRINTK
55
	int kern_level;
T
Takashi Iwai 已提交
56 57
	struct va_format vaf;
	char verbose_fmt[] = KERN_DEFAULT "ALSA %s:%d %pV";
58
	bool level_found = false;
T
Takashi Iwai 已提交
59 60
#endif

61
#ifdef CONFIG_SND_DEBUG
T
Takashi Iwai 已提交
62 63 64
	if (debug < level)
		return;
#endif
T
Takashi Iwai 已提交
65

L
Linus Torvalds 已提交
66
	va_start(args, format);
T
Takashi Iwai 已提交
67 68 69
#ifdef CONFIG_SND_VERBOSE_PRINTK
	vaf.fmt = format;
	vaf.va = &args;
70

71 72 73 74 75 76 77 78 79
	while ((kern_level = printk_get_level(vaf.fmt)) != 0) {
		const char *end_of_header = printk_skip_level(vaf.fmt);

		/* Ignore KERN_CONT. We print filename:line for each piece. */
		if (kern_level >= '0' && kern_level <= '7') {
			memcpy(verbose_fmt, vaf.fmt, end_of_header - vaf.fmt);
			level_found = true;
		}

80
		vaf.fmt = end_of_header;
81 82 83
	}

	if (!level_found && level)
84 85
		memcpy(verbose_fmt, KERN_DEBUG, sizeof(KERN_DEBUG) - 1);

86
	printk(verbose_fmt, sanity_file_name(path), line, &vaf);
T
Takashi Iwai 已提交
87
#else
L
Linus Torvalds 已提交
88
	vprintk(format, args);
T
Takashi Iwai 已提交
89
#endif
L
Linus Torvalds 已提交
90 91
	va_end(args);
}
T
Takashi Iwai 已提交
92
EXPORT_SYMBOL_GPL(__snd_printk);
L
Linus Torvalds 已提交
93
#endif
94 95 96 97

#ifdef CONFIG_PCI
#include <linux/pci.h>
/**
98 99 100
 * snd_pci_quirk_lookup_id - look up a PCI SSID quirk list
 * @vendor: PCI SSV id
 * @device: PCI SSD id
101 102 103 104 105 106 107 108 109
 * @list: quirk list, terminated by a null entry
 *
 * Look through the given quirk list and finds a matching entry
 * with the same PCI SSID.  When subdevice is 0, all subdevice
 * values may match.
 *
 * Returns the matched entry pointer, or NULL if nothing matched.
 */
const struct snd_pci_quirk *
110 111
snd_pci_quirk_lookup_id(u16 vendor, u16 device,
			const struct snd_pci_quirk *list)
112 113 114
{
	const struct snd_pci_quirk *q;

115
	for (q = list; q->subvendor || q->subdevice; q++) {
116
		if (q->subvendor != vendor)
117 118
			continue;
		if (!q->subdevice ||
119
		    (device & q->subdevice_mask) == q->subdevice)
120
			return q;
121
	}
122 123
	return NULL;
}
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
EXPORT_SYMBOL(snd_pci_quirk_lookup_id);

/**
 * snd_pci_quirk_lookup - look up a PCI SSID quirk list
 * @pci: pci_dev handle
 * @list: quirk list, terminated by a null entry
 *
 * Look through the given quirk list and finds a matching entry
 * with the same PCI SSID.  When subdevice is 0, all subdevice
 * values may match.
 *
 * Returns the matched entry pointer, or NULL if nothing matched.
 */
const struct snd_pci_quirk *
snd_pci_quirk_lookup(struct pci_dev *pci, const struct snd_pci_quirk *list)
{
140 141
	if (!pci)
		return NULL;
142 143 144 145
	return snd_pci_quirk_lookup_id(pci->subsystem_vendor,
				       pci->subsystem_device,
				       list);
}
146 147
EXPORT_SYMBOL(snd_pci_quirk_lookup);
#endif