Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Misc and compatibility things
0004  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
0005  */
0006 
0007 #include <linux/init.h>
0008 #include <linux/export.h>
0009 #include <linux/moduleparam.h>
0010 #include <linux/time.h>
0011 #include <linux/slab.h>
0012 #include <linux/ioport.h>
0013 #include <linux/fs.h>
0014 #include <sound/core.h>
0015 
0016 #ifdef CONFIG_SND_DEBUG
0017 
0018 #ifdef CONFIG_SND_DEBUG_VERBOSE
0019 #define DEFAULT_DEBUG_LEVEL 2
0020 #else
0021 #define DEFAULT_DEBUG_LEVEL 1
0022 #endif
0023 
0024 static int debug = DEFAULT_DEBUG_LEVEL;
0025 module_param(debug, int, 0644);
0026 MODULE_PARM_DESC(debug, "Debug level (0 = disable)");
0027 
0028 #endif /* CONFIG_SND_DEBUG */
0029 
0030 void release_and_free_resource(struct resource *res)
0031 {
0032     if (res) {
0033         release_resource(res);
0034         kfree(res);
0035     }
0036 }
0037 EXPORT_SYMBOL(release_and_free_resource);
0038 
0039 #ifdef CONFIG_SND_VERBOSE_PRINTK
0040 /* strip the leading path if the given path is absolute */
0041 static const char *sanity_file_name(const char *path)
0042 {
0043     if (*path == '/')
0044         return strrchr(path, '/') + 1;
0045     else
0046         return path;
0047 }
0048 #endif
0049 
0050 #if defined(CONFIG_SND_DEBUG) || defined(CONFIG_SND_VERBOSE_PRINTK)
0051 void __snd_printk(unsigned int level, const char *path, int line,
0052           const char *format, ...)
0053 {
0054     va_list args;
0055 #ifdef CONFIG_SND_VERBOSE_PRINTK
0056     int kern_level;
0057     struct va_format vaf;
0058     char verbose_fmt[] = KERN_DEFAULT "ALSA %s:%d %pV";
0059     bool level_found = false;
0060 #endif
0061 
0062 #ifdef CONFIG_SND_DEBUG
0063     if (debug < level)
0064         return;
0065 #endif
0066 
0067     va_start(args, format);
0068 #ifdef CONFIG_SND_VERBOSE_PRINTK
0069     vaf.fmt = format;
0070     vaf.va = &args;
0071 
0072     while ((kern_level = printk_get_level(vaf.fmt)) != 0) {
0073         const char *end_of_header = printk_skip_level(vaf.fmt);
0074 
0075         /* Ignore KERN_CONT. We print filename:line for each piece. */
0076         if (kern_level >= '0' && kern_level <= '7') {
0077             memcpy(verbose_fmt, vaf.fmt, end_of_header - vaf.fmt);
0078             level_found = true;
0079         }
0080 
0081         vaf.fmt = end_of_header;
0082     }
0083 
0084     if (!level_found && level)
0085         memcpy(verbose_fmt, KERN_DEBUG, sizeof(KERN_DEBUG) - 1);
0086 
0087     printk(verbose_fmt, sanity_file_name(path), line, &vaf);
0088 #else
0089     vprintk(format, args);
0090 #endif
0091     va_end(args);
0092 }
0093 EXPORT_SYMBOL_GPL(__snd_printk);
0094 #endif
0095 
0096 #ifdef CONFIG_PCI
0097 #include <linux/pci.h>
0098 /**
0099  * snd_pci_quirk_lookup_id - look up a PCI SSID quirk list
0100  * @vendor: PCI SSV id
0101  * @device: PCI SSD id
0102  * @list: quirk list, terminated by a null entry
0103  *
0104  * Look through the given quirk list and finds a matching entry
0105  * with the same PCI SSID.  When subdevice is 0, all subdevice
0106  * values may match.
0107  *
0108  * Returns the matched entry pointer, or NULL if nothing matched.
0109  */
0110 const struct snd_pci_quirk *
0111 snd_pci_quirk_lookup_id(u16 vendor, u16 device,
0112             const struct snd_pci_quirk *list)
0113 {
0114     const struct snd_pci_quirk *q;
0115 
0116     for (q = list; q->subvendor || q->subdevice; q++) {
0117         if (q->subvendor != vendor)
0118             continue;
0119         if (!q->subdevice ||
0120             (device & q->subdevice_mask) == q->subdevice)
0121             return q;
0122     }
0123     return NULL;
0124 }
0125 EXPORT_SYMBOL(snd_pci_quirk_lookup_id);
0126 
0127 /**
0128  * snd_pci_quirk_lookup - look up a PCI SSID quirk list
0129  * @pci: pci_dev handle
0130  * @list: quirk list, terminated by a null entry
0131  *
0132  * Look through the given quirk list and finds a matching entry
0133  * with the same PCI SSID.  When subdevice is 0, all subdevice
0134  * values may match.
0135  *
0136  * Returns the matched entry pointer, or NULL if nothing matched.
0137  */
0138 const struct snd_pci_quirk *
0139 snd_pci_quirk_lookup(struct pci_dev *pci, const struct snd_pci_quirk *list)
0140 {
0141     if (!pci)
0142         return NULL;
0143     return snd_pci_quirk_lookup_id(pci->subsystem_vendor,
0144                        pci->subsystem_device,
0145                        list);
0146 }
0147 EXPORT_SYMBOL(snd_pci_quirk_lookup);
0148 #endif
0149 
0150 /*
0151  * Deferred async signal helpers
0152  *
0153  * Below are a few helper functions to wrap the async signal handling
0154  * in the deferred work.  The main purpose is to avoid the messy deadlock
0155  * around tasklist_lock and co at the kill_fasync() invocation.
0156  * fasync_helper() and kill_fasync() are replaced with snd_fasync_helper()
0157  * and snd_kill_fasync(), respectively.  In addition, snd_fasync_free() has
0158  * to be called at releasing the relevant file object.
0159  */
0160 struct snd_fasync {
0161     struct fasync_struct *fasync;
0162     int signal;
0163     int poll;
0164     int on;
0165     struct list_head list;
0166 };
0167 
0168 static DEFINE_SPINLOCK(snd_fasync_lock);
0169 static LIST_HEAD(snd_fasync_list);
0170 
0171 static void snd_fasync_work_fn(struct work_struct *work)
0172 {
0173     struct snd_fasync *fasync;
0174 
0175     spin_lock_irq(&snd_fasync_lock);
0176     while (!list_empty(&snd_fasync_list)) {
0177         fasync = list_first_entry(&snd_fasync_list, struct snd_fasync, list);
0178         list_del_init(&fasync->list);
0179         spin_unlock_irq(&snd_fasync_lock);
0180         if (fasync->on)
0181             kill_fasync(&fasync->fasync, fasync->signal, fasync->poll);
0182         spin_lock_irq(&snd_fasync_lock);
0183     }
0184     spin_unlock_irq(&snd_fasync_lock);
0185 }
0186 
0187 static DECLARE_WORK(snd_fasync_work, snd_fasync_work_fn);
0188 
0189 int snd_fasync_helper(int fd, struct file *file, int on,
0190               struct snd_fasync **fasyncp)
0191 {
0192     struct snd_fasync *fasync = NULL;
0193 
0194     if (on) {
0195         fasync = kzalloc(sizeof(*fasync), GFP_KERNEL);
0196         if (!fasync)
0197             return -ENOMEM;
0198         INIT_LIST_HEAD(&fasync->list);
0199     }
0200 
0201     spin_lock_irq(&snd_fasync_lock);
0202     if (*fasyncp) {
0203         kfree(fasync);
0204         fasync = *fasyncp;
0205     } else {
0206         if (!fasync) {
0207             spin_unlock_irq(&snd_fasync_lock);
0208             return 0;
0209         }
0210         *fasyncp = fasync;
0211     }
0212     fasync->on = on;
0213     spin_unlock_irq(&snd_fasync_lock);
0214     return fasync_helper(fd, file, on, &fasync->fasync);
0215 }
0216 EXPORT_SYMBOL_GPL(snd_fasync_helper);
0217 
0218 void snd_kill_fasync(struct snd_fasync *fasync, int signal, int poll)
0219 {
0220     unsigned long flags;
0221 
0222     if (!fasync || !fasync->on)
0223         return;
0224     spin_lock_irqsave(&snd_fasync_lock, flags);
0225     fasync->signal = signal;
0226     fasync->poll = poll;
0227     list_move(&fasync->list, &snd_fasync_list);
0228     schedule_work(&snd_fasync_work);
0229     spin_unlock_irqrestore(&snd_fasync_lock, flags);
0230 }
0231 EXPORT_SYMBOL_GPL(snd_kill_fasync);
0232 
0233 void snd_fasync_free(struct snd_fasync *fasync)
0234 {
0235     if (!fasync)
0236         return;
0237     fasync->on = 0;
0238     flush_work(&snd_fasync_work);
0239     kfree(fasync);
0240 }
0241 EXPORT_SYMBOL_GPL(snd_fasync_free);