1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Initialization routines
4*4882a593Smuzhiyun * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <linux/init.h>
8*4882a593Smuzhiyun #include <linux/sched.h>
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/device.h>
11*4882a593Smuzhiyun #include <linux/file.h>
12*4882a593Smuzhiyun #include <linux/slab.h>
13*4882a593Smuzhiyun #include <linux/time.h>
14*4882a593Smuzhiyun #include <linux/ctype.h>
15*4882a593Smuzhiyun #include <linux/pm.h>
16*4882a593Smuzhiyun #include <linux/completion.h>
17*4882a593Smuzhiyun #include <linux/interrupt.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #include <sound/core.h>
20*4882a593Smuzhiyun #include <sound/control.h>
21*4882a593Smuzhiyun #include <sound/info.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /* monitor files for graceful shutdown (hotplug) */
24*4882a593Smuzhiyun struct snd_monitor_file {
25*4882a593Smuzhiyun struct file *file;
26*4882a593Smuzhiyun const struct file_operations *disconnected_f_op;
27*4882a593Smuzhiyun struct list_head shutdown_list; /* still need to shutdown */
28*4882a593Smuzhiyun struct list_head list; /* link of monitor files */
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun static DEFINE_SPINLOCK(shutdown_lock);
32*4882a593Smuzhiyun static LIST_HEAD(shutdown_files);
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun static const struct file_operations snd_shutdown_f_ops;
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun /* locked for registering/using */
37*4882a593Smuzhiyun static DECLARE_BITMAP(snd_cards_lock, SNDRV_CARDS);
38*4882a593Smuzhiyun static struct snd_card *snd_cards[SNDRV_CARDS];
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun static DEFINE_MUTEX(snd_card_mutex);
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun static char *slots[SNDRV_CARDS];
43*4882a593Smuzhiyun module_param_array(slots, charp, NULL, 0444);
44*4882a593Smuzhiyun MODULE_PARM_DESC(slots, "Module names assigned to the slots.");
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /* return non-zero if the given index is reserved for the given
47*4882a593Smuzhiyun * module via slots option
48*4882a593Smuzhiyun */
module_slot_match(struct module * module,int idx)49*4882a593Smuzhiyun static int module_slot_match(struct module *module, int idx)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun int match = 1;
52*4882a593Smuzhiyun #ifdef MODULE
53*4882a593Smuzhiyun const char *s1, *s2;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun if (!module || !*module->name || !slots[idx])
56*4882a593Smuzhiyun return 0;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun s1 = module->name;
59*4882a593Smuzhiyun s2 = slots[idx];
60*4882a593Smuzhiyun if (*s2 == '!') {
61*4882a593Smuzhiyun match = 0; /* negative match */
62*4882a593Smuzhiyun s2++;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun /* compare module name strings
65*4882a593Smuzhiyun * hyphens are handled as equivalent with underscore
66*4882a593Smuzhiyun */
67*4882a593Smuzhiyun for (;;) {
68*4882a593Smuzhiyun char c1 = *s1++;
69*4882a593Smuzhiyun char c2 = *s2++;
70*4882a593Smuzhiyun if (c1 == '-')
71*4882a593Smuzhiyun c1 = '_';
72*4882a593Smuzhiyun if (c2 == '-')
73*4882a593Smuzhiyun c2 = '_';
74*4882a593Smuzhiyun if (c1 != c2)
75*4882a593Smuzhiyun return !match;
76*4882a593Smuzhiyun if (!c1)
77*4882a593Smuzhiyun break;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun #endif /* MODULE */
80*4882a593Smuzhiyun return match;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
84*4882a593Smuzhiyun int (*snd_mixer_oss_notify_callback)(struct snd_card *card, int free_flag);
85*4882a593Smuzhiyun EXPORT_SYMBOL(snd_mixer_oss_notify_callback);
86*4882a593Smuzhiyun #endif
87*4882a593Smuzhiyun
check_empty_slot(struct module * module,int slot)88*4882a593Smuzhiyun static int check_empty_slot(struct module *module, int slot)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun return !slots[slot] || !*slots[slot];
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /* return an empty slot number (>= 0) found in the given bitmask @mask.
94*4882a593Smuzhiyun * @mask == -1 == 0xffffffff means: take any free slot up to 32
95*4882a593Smuzhiyun * when no slot is available, return the original @mask as is.
96*4882a593Smuzhiyun */
get_slot_from_bitmask(int mask,int (* check)(struct module *,int),struct module * module)97*4882a593Smuzhiyun static int get_slot_from_bitmask(int mask, int (*check)(struct module *, int),
98*4882a593Smuzhiyun struct module *module)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun int slot;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun for (slot = 0; slot < SNDRV_CARDS; slot++) {
103*4882a593Smuzhiyun if (slot < 32 && !(mask & (1U << slot)))
104*4882a593Smuzhiyun continue;
105*4882a593Smuzhiyun if (!test_bit(slot, snd_cards_lock)) {
106*4882a593Smuzhiyun if (check(module, slot))
107*4882a593Smuzhiyun return slot; /* found */
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun return mask; /* unchanged */
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun /* the default release callback set in snd_device_initialize() below;
114*4882a593Smuzhiyun * this is just NOP for now, as almost all jobs are already done in
115*4882a593Smuzhiyun * dev_free callback of snd_device chain instead.
116*4882a593Smuzhiyun */
default_release(struct device * dev)117*4882a593Smuzhiyun static void default_release(struct device *dev)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /**
122*4882a593Smuzhiyun * snd_device_initialize - Initialize struct device for sound devices
123*4882a593Smuzhiyun * @dev: device to initialize
124*4882a593Smuzhiyun * @card: card to assign, optional
125*4882a593Smuzhiyun */
snd_device_initialize(struct device * dev,struct snd_card * card)126*4882a593Smuzhiyun void snd_device_initialize(struct device *dev, struct snd_card *card)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun device_initialize(dev);
129*4882a593Smuzhiyun if (card)
130*4882a593Smuzhiyun dev->parent = &card->card_dev;
131*4882a593Smuzhiyun dev->class = sound_class;
132*4882a593Smuzhiyun dev->release = default_release;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_device_initialize);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun static int snd_card_do_free(struct snd_card *card);
137*4882a593Smuzhiyun static const struct attribute_group card_dev_attr_group;
138*4882a593Smuzhiyun
release_card_device(struct device * dev)139*4882a593Smuzhiyun static void release_card_device(struct device *dev)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun snd_card_do_free(dev_to_snd_card(dev));
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun /**
145*4882a593Smuzhiyun * snd_card_new - create and initialize a soundcard structure
146*4882a593Smuzhiyun * @parent: the parent device object
147*4882a593Smuzhiyun * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
148*4882a593Smuzhiyun * @xid: card identification (ASCII string)
149*4882a593Smuzhiyun * @module: top level module for locking
150*4882a593Smuzhiyun * @extra_size: allocate this extra size after the main soundcard structure
151*4882a593Smuzhiyun * @card_ret: the pointer to store the created card instance
152*4882a593Smuzhiyun *
153*4882a593Smuzhiyun * Creates and initializes a soundcard structure.
154*4882a593Smuzhiyun *
155*4882a593Smuzhiyun * The function allocates snd_card instance via kzalloc with the given
156*4882a593Smuzhiyun * space for the driver to use freely. The allocated struct is stored
157*4882a593Smuzhiyun * in the given card_ret pointer.
158*4882a593Smuzhiyun *
159*4882a593Smuzhiyun * Return: Zero if successful or a negative error code.
160*4882a593Smuzhiyun */
snd_card_new(struct device * parent,int idx,const char * xid,struct module * module,int extra_size,struct snd_card ** card_ret)161*4882a593Smuzhiyun int snd_card_new(struct device *parent, int idx, const char *xid,
162*4882a593Smuzhiyun struct module *module, int extra_size,
163*4882a593Smuzhiyun struct snd_card **card_ret)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun struct snd_card *card;
166*4882a593Smuzhiyun int err;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun if (snd_BUG_ON(!card_ret))
169*4882a593Smuzhiyun return -EINVAL;
170*4882a593Smuzhiyun *card_ret = NULL;
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun if (extra_size < 0)
173*4882a593Smuzhiyun extra_size = 0;
174*4882a593Smuzhiyun card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
175*4882a593Smuzhiyun if (!card)
176*4882a593Smuzhiyun return -ENOMEM;
177*4882a593Smuzhiyun if (extra_size > 0)
178*4882a593Smuzhiyun card->private_data = (char *)card + sizeof(struct snd_card);
179*4882a593Smuzhiyun if (xid)
180*4882a593Smuzhiyun strlcpy(card->id, xid, sizeof(card->id));
181*4882a593Smuzhiyun err = 0;
182*4882a593Smuzhiyun mutex_lock(&snd_card_mutex);
183*4882a593Smuzhiyun if (idx < 0) /* first check the matching module-name slot */
184*4882a593Smuzhiyun idx = get_slot_from_bitmask(idx, module_slot_match, module);
185*4882a593Smuzhiyun if (idx < 0) /* if not matched, assign an empty slot */
186*4882a593Smuzhiyun idx = get_slot_from_bitmask(idx, check_empty_slot, module);
187*4882a593Smuzhiyun if (idx < 0)
188*4882a593Smuzhiyun err = -ENODEV;
189*4882a593Smuzhiyun else if (idx < snd_ecards_limit) {
190*4882a593Smuzhiyun if (test_bit(idx, snd_cards_lock))
191*4882a593Smuzhiyun err = -EBUSY; /* invalid */
192*4882a593Smuzhiyun } else if (idx >= SNDRV_CARDS)
193*4882a593Smuzhiyun err = -ENODEV;
194*4882a593Smuzhiyun if (err < 0) {
195*4882a593Smuzhiyun mutex_unlock(&snd_card_mutex);
196*4882a593Smuzhiyun dev_err(parent, "cannot find the slot for index %d (range 0-%i), error: %d\n",
197*4882a593Smuzhiyun idx, snd_ecards_limit - 1, err);
198*4882a593Smuzhiyun kfree(card);
199*4882a593Smuzhiyun return err;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun set_bit(idx, snd_cards_lock); /* lock it */
202*4882a593Smuzhiyun if (idx >= snd_ecards_limit)
203*4882a593Smuzhiyun snd_ecards_limit = idx + 1; /* increase the limit */
204*4882a593Smuzhiyun mutex_unlock(&snd_card_mutex);
205*4882a593Smuzhiyun card->dev = parent;
206*4882a593Smuzhiyun card->number = idx;
207*4882a593Smuzhiyun #ifdef MODULE
208*4882a593Smuzhiyun WARN_ON(!module);
209*4882a593Smuzhiyun card->module = module;
210*4882a593Smuzhiyun #endif
211*4882a593Smuzhiyun INIT_LIST_HEAD(&card->devices);
212*4882a593Smuzhiyun init_rwsem(&card->controls_rwsem);
213*4882a593Smuzhiyun rwlock_init(&card->ctl_files_rwlock);
214*4882a593Smuzhiyun INIT_LIST_HEAD(&card->controls);
215*4882a593Smuzhiyun INIT_LIST_HEAD(&card->ctl_files);
216*4882a593Smuzhiyun spin_lock_init(&card->files_lock);
217*4882a593Smuzhiyun INIT_LIST_HEAD(&card->files_list);
218*4882a593Smuzhiyun mutex_init(&card->memory_mutex);
219*4882a593Smuzhiyun #ifdef CONFIG_PM
220*4882a593Smuzhiyun init_waitqueue_head(&card->power_sleep);
221*4882a593Smuzhiyun #endif
222*4882a593Smuzhiyun init_waitqueue_head(&card->remove_sleep);
223*4882a593Smuzhiyun card->sync_irq = -1;
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun device_initialize(&card->card_dev);
226*4882a593Smuzhiyun card->card_dev.parent = parent;
227*4882a593Smuzhiyun card->card_dev.class = sound_class;
228*4882a593Smuzhiyun card->card_dev.release = release_card_device;
229*4882a593Smuzhiyun card->card_dev.groups = card->dev_groups;
230*4882a593Smuzhiyun card->dev_groups[0] = &card_dev_attr_group;
231*4882a593Smuzhiyun err = kobject_set_name(&card->card_dev.kobj, "card%d", idx);
232*4882a593Smuzhiyun if (err < 0)
233*4882a593Smuzhiyun goto __error;
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun snprintf(card->irq_descr, sizeof(card->irq_descr), "%s:%s",
236*4882a593Smuzhiyun dev_driver_string(card->dev), dev_name(&card->card_dev));
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun /* the control interface cannot be accessed from the user space until */
239*4882a593Smuzhiyun /* snd_cards_bitmask and snd_cards are set with snd_card_register */
240*4882a593Smuzhiyun err = snd_ctl_create(card);
241*4882a593Smuzhiyun if (err < 0) {
242*4882a593Smuzhiyun dev_err(parent, "unable to register control minors\n");
243*4882a593Smuzhiyun goto __error;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun err = snd_info_card_create(card);
246*4882a593Smuzhiyun if (err < 0) {
247*4882a593Smuzhiyun dev_err(parent, "unable to create card info\n");
248*4882a593Smuzhiyun goto __error_ctl;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun *card_ret = card;
251*4882a593Smuzhiyun return 0;
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun __error_ctl:
254*4882a593Smuzhiyun snd_device_free_all(card);
255*4882a593Smuzhiyun __error:
256*4882a593Smuzhiyun put_device(&card->card_dev);
257*4882a593Smuzhiyun return err;
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun EXPORT_SYMBOL(snd_card_new);
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun /**
262*4882a593Smuzhiyun * snd_card_ref - Get the card object from the index
263*4882a593Smuzhiyun * @idx: the card index
264*4882a593Smuzhiyun *
265*4882a593Smuzhiyun * Returns a card object corresponding to the given index or NULL if not found.
266*4882a593Smuzhiyun * Release the object via snd_card_unref().
267*4882a593Smuzhiyun */
snd_card_ref(int idx)268*4882a593Smuzhiyun struct snd_card *snd_card_ref(int idx)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun struct snd_card *card;
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun mutex_lock(&snd_card_mutex);
273*4882a593Smuzhiyun card = snd_cards[idx];
274*4882a593Smuzhiyun if (card)
275*4882a593Smuzhiyun get_device(&card->card_dev);
276*4882a593Smuzhiyun mutex_unlock(&snd_card_mutex);
277*4882a593Smuzhiyun return card;
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_card_ref);
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun /* return non-zero if a card is already locked */
snd_card_locked(int card)282*4882a593Smuzhiyun int snd_card_locked(int card)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun int locked;
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun mutex_lock(&snd_card_mutex);
287*4882a593Smuzhiyun locked = test_bit(card, snd_cards_lock);
288*4882a593Smuzhiyun mutex_unlock(&snd_card_mutex);
289*4882a593Smuzhiyun return locked;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun
snd_disconnect_llseek(struct file * file,loff_t offset,int orig)292*4882a593Smuzhiyun static loff_t snd_disconnect_llseek(struct file *file, loff_t offset, int orig)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun return -ENODEV;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun
snd_disconnect_read(struct file * file,char __user * buf,size_t count,loff_t * offset)297*4882a593Smuzhiyun static ssize_t snd_disconnect_read(struct file *file, char __user *buf,
298*4882a593Smuzhiyun size_t count, loff_t *offset)
299*4882a593Smuzhiyun {
300*4882a593Smuzhiyun return -ENODEV;
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun
snd_disconnect_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)303*4882a593Smuzhiyun static ssize_t snd_disconnect_write(struct file *file, const char __user *buf,
304*4882a593Smuzhiyun size_t count, loff_t *offset)
305*4882a593Smuzhiyun {
306*4882a593Smuzhiyun return -ENODEV;
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun
snd_disconnect_release(struct inode * inode,struct file * file)309*4882a593Smuzhiyun static int snd_disconnect_release(struct inode *inode, struct file *file)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun struct snd_monitor_file *df = NULL, *_df;
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun spin_lock(&shutdown_lock);
314*4882a593Smuzhiyun list_for_each_entry(_df, &shutdown_files, shutdown_list) {
315*4882a593Smuzhiyun if (_df->file == file) {
316*4882a593Smuzhiyun df = _df;
317*4882a593Smuzhiyun list_del_init(&df->shutdown_list);
318*4882a593Smuzhiyun break;
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun spin_unlock(&shutdown_lock);
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun if (likely(df)) {
324*4882a593Smuzhiyun if ((file->f_flags & FASYNC) && df->disconnected_f_op->fasync)
325*4882a593Smuzhiyun df->disconnected_f_op->fasync(-1, file, 0);
326*4882a593Smuzhiyun return df->disconnected_f_op->release(inode, file);
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun panic("%s(%p, %p) failed!", __func__, inode, file);
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun
snd_disconnect_poll(struct file * file,poll_table * wait)332*4882a593Smuzhiyun static __poll_t snd_disconnect_poll(struct file * file, poll_table * wait)
333*4882a593Smuzhiyun {
334*4882a593Smuzhiyun return EPOLLERR | EPOLLNVAL;
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun
snd_disconnect_ioctl(struct file * file,unsigned int cmd,unsigned long arg)337*4882a593Smuzhiyun static long snd_disconnect_ioctl(struct file *file,
338*4882a593Smuzhiyun unsigned int cmd, unsigned long arg)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun return -ENODEV;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun
snd_disconnect_mmap(struct file * file,struct vm_area_struct * vma)343*4882a593Smuzhiyun static int snd_disconnect_mmap(struct file *file, struct vm_area_struct *vma)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun return -ENODEV;
346*4882a593Smuzhiyun }
347*4882a593Smuzhiyun
snd_disconnect_fasync(int fd,struct file * file,int on)348*4882a593Smuzhiyun static int snd_disconnect_fasync(int fd, struct file *file, int on)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun return -ENODEV;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun static const struct file_operations snd_shutdown_f_ops =
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun .owner = THIS_MODULE,
356*4882a593Smuzhiyun .llseek = snd_disconnect_llseek,
357*4882a593Smuzhiyun .read = snd_disconnect_read,
358*4882a593Smuzhiyun .write = snd_disconnect_write,
359*4882a593Smuzhiyun .release = snd_disconnect_release,
360*4882a593Smuzhiyun .poll = snd_disconnect_poll,
361*4882a593Smuzhiyun .unlocked_ioctl = snd_disconnect_ioctl,
362*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
363*4882a593Smuzhiyun .compat_ioctl = snd_disconnect_ioctl,
364*4882a593Smuzhiyun #endif
365*4882a593Smuzhiyun .mmap = snd_disconnect_mmap,
366*4882a593Smuzhiyun .fasync = snd_disconnect_fasync
367*4882a593Smuzhiyun };
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun /**
370*4882a593Smuzhiyun * snd_card_disconnect - disconnect all APIs from the file-operations (user space)
371*4882a593Smuzhiyun * @card: soundcard structure
372*4882a593Smuzhiyun *
373*4882a593Smuzhiyun * Disconnects all APIs from the file-operations (user space).
374*4882a593Smuzhiyun *
375*4882a593Smuzhiyun * Return: Zero, otherwise a negative error code.
376*4882a593Smuzhiyun *
377*4882a593Smuzhiyun * Note: The current implementation replaces all active file->f_op with special
378*4882a593Smuzhiyun * dummy file operations (they do nothing except release).
379*4882a593Smuzhiyun */
snd_card_disconnect(struct snd_card * card)380*4882a593Smuzhiyun int snd_card_disconnect(struct snd_card *card)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun struct snd_monitor_file *mfile;
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun if (!card)
385*4882a593Smuzhiyun return -EINVAL;
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun spin_lock(&card->files_lock);
388*4882a593Smuzhiyun if (card->shutdown) {
389*4882a593Smuzhiyun spin_unlock(&card->files_lock);
390*4882a593Smuzhiyun return 0;
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun card->shutdown = 1;
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun /* replace file->f_op with special dummy operations */
395*4882a593Smuzhiyun list_for_each_entry(mfile, &card->files_list, list) {
396*4882a593Smuzhiyun /* it's critical part, use endless loop */
397*4882a593Smuzhiyun /* we have no room to fail */
398*4882a593Smuzhiyun mfile->disconnected_f_op = mfile->file->f_op;
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun spin_lock(&shutdown_lock);
401*4882a593Smuzhiyun list_add(&mfile->shutdown_list, &shutdown_files);
402*4882a593Smuzhiyun spin_unlock(&shutdown_lock);
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun mfile->file->f_op = &snd_shutdown_f_ops;
405*4882a593Smuzhiyun fops_get(mfile->file->f_op);
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun spin_unlock(&card->files_lock);
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun /* notify all connected devices about disconnection */
410*4882a593Smuzhiyun /* at this point, they cannot respond to any calls except release() */
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
413*4882a593Smuzhiyun if (snd_mixer_oss_notify_callback)
414*4882a593Smuzhiyun snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_DISCONNECT);
415*4882a593Smuzhiyun #endif
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun /* notify all devices that we are disconnected */
418*4882a593Smuzhiyun snd_device_disconnect_all(card);
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun if (card->sync_irq > 0)
421*4882a593Smuzhiyun synchronize_irq(card->sync_irq);
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun snd_info_card_disconnect(card);
424*4882a593Smuzhiyun if (card->registered) {
425*4882a593Smuzhiyun device_del(&card->card_dev);
426*4882a593Smuzhiyun card->registered = false;
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun /* disable fops (user space) operations for ALSA API */
430*4882a593Smuzhiyun mutex_lock(&snd_card_mutex);
431*4882a593Smuzhiyun snd_cards[card->number] = NULL;
432*4882a593Smuzhiyun clear_bit(card->number, snd_cards_lock);
433*4882a593Smuzhiyun mutex_unlock(&snd_card_mutex);
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun #ifdef CONFIG_PM
436*4882a593Smuzhiyun wake_up(&card->power_sleep);
437*4882a593Smuzhiyun #endif
438*4882a593Smuzhiyun return 0;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun EXPORT_SYMBOL(snd_card_disconnect);
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun /**
443*4882a593Smuzhiyun * snd_card_disconnect_sync - disconnect card and wait until files get closed
444*4882a593Smuzhiyun * @card: card object to disconnect
445*4882a593Smuzhiyun *
446*4882a593Smuzhiyun * This calls snd_card_disconnect() for disconnecting all belonging components
447*4882a593Smuzhiyun * and waits until all pending files get closed.
448*4882a593Smuzhiyun * It assures that all accesses from user-space finished so that the driver
449*4882a593Smuzhiyun * can release its resources gracefully.
450*4882a593Smuzhiyun */
snd_card_disconnect_sync(struct snd_card * card)451*4882a593Smuzhiyun void snd_card_disconnect_sync(struct snd_card *card)
452*4882a593Smuzhiyun {
453*4882a593Smuzhiyun int err;
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun err = snd_card_disconnect(card);
456*4882a593Smuzhiyun if (err < 0) {
457*4882a593Smuzhiyun dev_err(card->dev,
458*4882a593Smuzhiyun "snd_card_disconnect error (%d), skipping sync\n",
459*4882a593Smuzhiyun err);
460*4882a593Smuzhiyun return;
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun spin_lock_irq(&card->files_lock);
464*4882a593Smuzhiyun wait_event_lock_irq(card->remove_sleep,
465*4882a593Smuzhiyun list_empty(&card->files_list),
466*4882a593Smuzhiyun card->files_lock);
467*4882a593Smuzhiyun spin_unlock_irq(&card->files_lock);
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_card_disconnect_sync);
470*4882a593Smuzhiyun
snd_card_do_free(struct snd_card * card)471*4882a593Smuzhiyun static int snd_card_do_free(struct snd_card *card)
472*4882a593Smuzhiyun {
473*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
474*4882a593Smuzhiyun if (snd_mixer_oss_notify_callback)
475*4882a593Smuzhiyun snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
476*4882a593Smuzhiyun #endif
477*4882a593Smuzhiyun snd_device_free_all(card);
478*4882a593Smuzhiyun if (card->private_free)
479*4882a593Smuzhiyun card->private_free(card);
480*4882a593Smuzhiyun if (snd_info_card_free(card) < 0) {
481*4882a593Smuzhiyun dev_warn(card->dev, "unable to free card info\n");
482*4882a593Smuzhiyun /* Not fatal error */
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun if (card->release_completion)
485*4882a593Smuzhiyun complete(card->release_completion);
486*4882a593Smuzhiyun kfree(card);
487*4882a593Smuzhiyun return 0;
488*4882a593Smuzhiyun }
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun /**
491*4882a593Smuzhiyun * snd_card_free_when_closed - Disconnect the card, free it later eventually
492*4882a593Smuzhiyun * @card: soundcard structure
493*4882a593Smuzhiyun *
494*4882a593Smuzhiyun * Unlike snd_card_free(), this function doesn't try to release the card
495*4882a593Smuzhiyun * resource immediately, but tries to disconnect at first. When the card
496*4882a593Smuzhiyun * is still in use, the function returns before freeing the resources.
497*4882a593Smuzhiyun * The card resources will be freed when the refcount gets to zero.
498*4882a593Smuzhiyun */
snd_card_free_when_closed(struct snd_card * card)499*4882a593Smuzhiyun int snd_card_free_when_closed(struct snd_card *card)
500*4882a593Smuzhiyun {
501*4882a593Smuzhiyun int ret = snd_card_disconnect(card);
502*4882a593Smuzhiyun if (ret)
503*4882a593Smuzhiyun return ret;
504*4882a593Smuzhiyun put_device(&card->card_dev);
505*4882a593Smuzhiyun return 0;
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun EXPORT_SYMBOL(snd_card_free_when_closed);
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun /**
510*4882a593Smuzhiyun * snd_card_free - frees given soundcard structure
511*4882a593Smuzhiyun * @card: soundcard structure
512*4882a593Smuzhiyun *
513*4882a593Smuzhiyun * This function releases the soundcard structure and the all assigned
514*4882a593Smuzhiyun * devices automatically. That is, you don't have to release the devices
515*4882a593Smuzhiyun * by yourself.
516*4882a593Smuzhiyun *
517*4882a593Smuzhiyun * This function waits until the all resources are properly released.
518*4882a593Smuzhiyun *
519*4882a593Smuzhiyun * Return: Zero. Frees all associated devices and frees the control
520*4882a593Smuzhiyun * interface associated to given soundcard.
521*4882a593Smuzhiyun */
snd_card_free(struct snd_card * card)522*4882a593Smuzhiyun int snd_card_free(struct snd_card *card)
523*4882a593Smuzhiyun {
524*4882a593Smuzhiyun DECLARE_COMPLETION_ONSTACK(released);
525*4882a593Smuzhiyun int ret;
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun card->release_completion = &released;
528*4882a593Smuzhiyun ret = snd_card_free_when_closed(card);
529*4882a593Smuzhiyun if (ret)
530*4882a593Smuzhiyun return ret;
531*4882a593Smuzhiyun /* wait, until all devices are ready for the free operation */
532*4882a593Smuzhiyun wait_for_completion(&released);
533*4882a593Smuzhiyun return 0;
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun EXPORT_SYMBOL(snd_card_free);
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun /* retrieve the last word of shortname or longname */
retrieve_id_from_card_name(const char * name)538*4882a593Smuzhiyun static const char *retrieve_id_from_card_name(const char *name)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun const char *spos = name;
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun while (*name) {
543*4882a593Smuzhiyun if (isspace(*name) && isalnum(name[1]))
544*4882a593Smuzhiyun spos = name + 1;
545*4882a593Smuzhiyun name++;
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun return spos;
548*4882a593Smuzhiyun }
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun /* return true if the given id string doesn't conflict any other card ids */
card_id_ok(struct snd_card * card,const char * id)551*4882a593Smuzhiyun static bool card_id_ok(struct snd_card *card, const char *id)
552*4882a593Smuzhiyun {
553*4882a593Smuzhiyun int i;
554*4882a593Smuzhiyun if (!snd_info_check_reserved_words(id))
555*4882a593Smuzhiyun return false;
556*4882a593Smuzhiyun for (i = 0; i < snd_ecards_limit; i++) {
557*4882a593Smuzhiyun if (snd_cards[i] && snd_cards[i] != card &&
558*4882a593Smuzhiyun !strcmp(snd_cards[i]->id, id))
559*4882a593Smuzhiyun return false;
560*4882a593Smuzhiyun }
561*4882a593Smuzhiyun return true;
562*4882a593Smuzhiyun }
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun /* copy to card->id only with valid letters from nid */
copy_valid_id_string(struct snd_card * card,const char * src,const char * nid)565*4882a593Smuzhiyun static void copy_valid_id_string(struct snd_card *card, const char *src,
566*4882a593Smuzhiyun const char *nid)
567*4882a593Smuzhiyun {
568*4882a593Smuzhiyun char *id = card->id;
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun while (*nid && !isalnum(*nid))
571*4882a593Smuzhiyun nid++;
572*4882a593Smuzhiyun if (isdigit(*nid))
573*4882a593Smuzhiyun *id++ = isalpha(*src) ? *src : 'D';
574*4882a593Smuzhiyun while (*nid && (size_t)(id - card->id) < sizeof(card->id) - 1) {
575*4882a593Smuzhiyun if (isalnum(*nid))
576*4882a593Smuzhiyun *id++ = *nid;
577*4882a593Smuzhiyun nid++;
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun *id = 0;
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun /* Set card->id from the given string
583*4882a593Smuzhiyun * If the string conflicts with other ids, add a suffix to make it unique.
584*4882a593Smuzhiyun */
snd_card_set_id_no_lock(struct snd_card * card,const char * src,const char * nid)585*4882a593Smuzhiyun static void snd_card_set_id_no_lock(struct snd_card *card, const char *src,
586*4882a593Smuzhiyun const char *nid)
587*4882a593Smuzhiyun {
588*4882a593Smuzhiyun int len, loops;
589*4882a593Smuzhiyun bool is_default = false;
590*4882a593Smuzhiyun char *id;
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun copy_valid_id_string(card, src, nid);
593*4882a593Smuzhiyun id = card->id;
594*4882a593Smuzhiyun
595*4882a593Smuzhiyun again:
596*4882a593Smuzhiyun /* use "Default" for obviously invalid strings
597*4882a593Smuzhiyun * ("card" conflicts with proc directories)
598*4882a593Smuzhiyun */
599*4882a593Smuzhiyun if (!*id || !strncmp(id, "card", 4)) {
600*4882a593Smuzhiyun strcpy(id, "Default");
601*4882a593Smuzhiyun is_default = true;
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun len = strlen(id);
605*4882a593Smuzhiyun for (loops = 0; loops < SNDRV_CARDS; loops++) {
606*4882a593Smuzhiyun char *spos;
607*4882a593Smuzhiyun char sfxstr[5]; /* "_012" */
608*4882a593Smuzhiyun int sfxlen;
609*4882a593Smuzhiyun
610*4882a593Smuzhiyun if (card_id_ok(card, id))
611*4882a593Smuzhiyun return; /* OK */
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun /* Add _XYZ suffix */
614*4882a593Smuzhiyun sprintf(sfxstr, "_%X", loops + 1);
615*4882a593Smuzhiyun sfxlen = strlen(sfxstr);
616*4882a593Smuzhiyun if (len + sfxlen >= sizeof(card->id))
617*4882a593Smuzhiyun spos = id + sizeof(card->id) - sfxlen - 1;
618*4882a593Smuzhiyun else
619*4882a593Smuzhiyun spos = id + len;
620*4882a593Smuzhiyun strcpy(spos, sfxstr);
621*4882a593Smuzhiyun }
622*4882a593Smuzhiyun /* fallback to the default id */
623*4882a593Smuzhiyun if (!is_default) {
624*4882a593Smuzhiyun *id = 0;
625*4882a593Smuzhiyun goto again;
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun /* last resort... */
628*4882a593Smuzhiyun dev_err(card->dev, "unable to set card id (%s)\n", id);
629*4882a593Smuzhiyun if (card->proc_root->name)
630*4882a593Smuzhiyun strlcpy(card->id, card->proc_root->name, sizeof(card->id));
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun /**
634*4882a593Smuzhiyun * snd_card_set_id - set card identification name
635*4882a593Smuzhiyun * @card: soundcard structure
636*4882a593Smuzhiyun * @nid: new identification string
637*4882a593Smuzhiyun *
638*4882a593Smuzhiyun * This function sets the card identification and checks for name
639*4882a593Smuzhiyun * collisions.
640*4882a593Smuzhiyun */
snd_card_set_id(struct snd_card * card,const char * nid)641*4882a593Smuzhiyun void snd_card_set_id(struct snd_card *card, const char *nid)
642*4882a593Smuzhiyun {
643*4882a593Smuzhiyun /* check if user specified own card->id */
644*4882a593Smuzhiyun if (card->id[0] != '\0')
645*4882a593Smuzhiyun return;
646*4882a593Smuzhiyun mutex_lock(&snd_card_mutex);
647*4882a593Smuzhiyun snd_card_set_id_no_lock(card, nid, nid);
648*4882a593Smuzhiyun mutex_unlock(&snd_card_mutex);
649*4882a593Smuzhiyun }
650*4882a593Smuzhiyun EXPORT_SYMBOL(snd_card_set_id);
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun static ssize_t
card_id_show_attr(struct device * dev,struct device_attribute * attr,char * buf)653*4882a593Smuzhiyun card_id_show_attr(struct device *dev,
654*4882a593Smuzhiyun struct device_attribute *attr, char *buf)
655*4882a593Smuzhiyun {
656*4882a593Smuzhiyun struct snd_card *card = container_of(dev, struct snd_card, card_dev);
657*4882a593Smuzhiyun return scnprintf(buf, PAGE_SIZE, "%s\n", card->id);
658*4882a593Smuzhiyun }
659*4882a593Smuzhiyun
660*4882a593Smuzhiyun static ssize_t
card_id_store_attr(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)661*4882a593Smuzhiyun card_id_store_attr(struct device *dev, struct device_attribute *attr,
662*4882a593Smuzhiyun const char *buf, size_t count)
663*4882a593Smuzhiyun {
664*4882a593Smuzhiyun struct snd_card *card = container_of(dev, struct snd_card, card_dev);
665*4882a593Smuzhiyun char buf1[sizeof(card->id)];
666*4882a593Smuzhiyun size_t copy = count > sizeof(card->id) - 1 ?
667*4882a593Smuzhiyun sizeof(card->id) - 1 : count;
668*4882a593Smuzhiyun size_t idx;
669*4882a593Smuzhiyun int c;
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun for (idx = 0; idx < copy; idx++) {
672*4882a593Smuzhiyun c = buf[idx];
673*4882a593Smuzhiyun if (!isalnum(c) && c != '_' && c != '-')
674*4882a593Smuzhiyun return -EINVAL;
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun memcpy(buf1, buf, copy);
677*4882a593Smuzhiyun buf1[copy] = '\0';
678*4882a593Smuzhiyun mutex_lock(&snd_card_mutex);
679*4882a593Smuzhiyun if (!card_id_ok(NULL, buf1)) {
680*4882a593Smuzhiyun mutex_unlock(&snd_card_mutex);
681*4882a593Smuzhiyun return -EEXIST;
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun strcpy(card->id, buf1);
684*4882a593Smuzhiyun snd_info_card_id_change(card);
685*4882a593Smuzhiyun mutex_unlock(&snd_card_mutex);
686*4882a593Smuzhiyun
687*4882a593Smuzhiyun return count;
688*4882a593Smuzhiyun }
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun static DEVICE_ATTR(id, 0644, card_id_show_attr, card_id_store_attr);
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun static ssize_t
card_number_show_attr(struct device * dev,struct device_attribute * attr,char * buf)693*4882a593Smuzhiyun card_number_show_attr(struct device *dev,
694*4882a593Smuzhiyun struct device_attribute *attr, char *buf)
695*4882a593Smuzhiyun {
696*4882a593Smuzhiyun struct snd_card *card = container_of(dev, struct snd_card, card_dev);
697*4882a593Smuzhiyun return scnprintf(buf, PAGE_SIZE, "%i\n", card->number);
698*4882a593Smuzhiyun }
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun static DEVICE_ATTR(number, 0444, card_number_show_attr, NULL);
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun static struct attribute *card_dev_attrs[] = {
703*4882a593Smuzhiyun &dev_attr_id.attr,
704*4882a593Smuzhiyun &dev_attr_number.attr,
705*4882a593Smuzhiyun NULL
706*4882a593Smuzhiyun };
707*4882a593Smuzhiyun
708*4882a593Smuzhiyun static const struct attribute_group card_dev_attr_group = {
709*4882a593Smuzhiyun .attrs = card_dev_attrs,
710*4882a593Smuzhiyun };
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun /**
713*4882a593Smuzhiyun * snd_card_add_dev_attr - Append a new sysfs attribute group to card
714*4882a593Smuzhiyun * @card: card instance
715*4882a593Smuzhiyun * @group: attribute group to append
716*4882a593Smuzhiyun */
snd_card_add_dev_attr(struct snd_card * card,const struct attribute_group * group)717*4882a593Smuzhiyun int snd_card_add_dev_attr(struct snd_card *card,
718*4882a593Smuzhiyun const struct attribute_group *group)
719*4882a593Smuzhiyun {
720*4882a593Smuzhiyun int i;
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun /* loop for (arraysize-1) here to keep NULL at the last entry */
723*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(card->dev_groups) - 1; i++) {
724*4882a593Smuzhiyun if (!card->dev_groups[i]) {
725*4882a593Smuzhiyun card->dev_groups[i] = group;
726*4882a593Smuzhiyun return 0;
727*4882a593Smuzhiyun }
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun
730*4882a593Smuzhiyun dev_err(card->dev, "Too many groups assigned\n");
731*4882a593Smuzhiyun return -ENOSPC;
732*4882a593Smuzhiyun }
733*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_card_add_dev_attr);
734*4882a593Smuzhiyun
735*4882a593Smuzhiyun /**
736*4882a593Smuzhiyun * snd_card_register - register the soundcard
737*4882a593Smuzhiyun * @card: soundcard structure
738*4882a593Smuzhiyun *
739*4882a593Smuzhiyun * This function registers all the devices assigned to the soundcard.
740*4882a593Smuzhiyun * Until calling this, the ALSA control interface is blocked from the
741*4882a593Smuzhiyun * external accesses. Thus, you should call this function at the end
742*4882a593Smuzhiyun * of the initialization of the card.
743*4882a593Smuzhiyun *
744*4882a593Smuzhiyun * Return: Zero otherwise a negative error code if the registration failed.
745*4882a593Smuzhiyun */
snd_card_register(struct snd_card * card)746*4882a593Smuzhiyun int snd_card_register(struct snd_card *card)
747*4882a593Smuzhiyun {
748*4882a593Smuzhiyun int err;
749*4882a593Smuzhiyun
750*4882a593Smuzhiyun if (snd_BUG_ON(!card))
751*4882a593Smuzhiyun return -EINVAL;
752*4882a593Smuzhiyun
753*4882a593Smuzhiyun if (!card->registered) {
754*4882a593Smuzhiyun err = device_add(&card->card_dev);
755*4882a593Smuzhiyun if (err < 0)
756*4882a593Smuzhiyun return err;
757*4882a593Smuzhiyun card->registered = true;
758*4882a593Smuzhiyun }
759*4882a593Smuzhiyun
760*4882a593Smuzhiyun if ((err = snd_device_register_all(card)) < 0)
761*4882a593Smuzhiyun return err;
762*4882a593Smuzhiyun mutex_lock(&snd_card_mutex);
763*4882a593Smuzhiyun if (snd_cards[card->number]) {
764*4882a593Smuzhiyun /* already registered */
765*4882a593Smuzhiyun mutex_unlock(&snd_card_mutex);
766*4882a593Smuzhiyun return snd_info_card_register(card); /* register pending info */
767*4882a593Smuzhiyun }
768*4882a593Smuzhiyun if (*card->id) {
769*4882a593Smuzhiyun /* make a unique id name from the given string */
770*4882a593Smuzhiyun char tmpid[sizeof(card->id)];
771*4882a593Smuzhiyun memcpy(tmpid, card->id, sizeof(card->id));
772*4882a593Smuzhiyun snd_card_set_id_no_lock(card, tmpid, tmpid);
773*4882a593Smuzhiyun } else {
774*4882a593Smuzhiyun /* create an id from either shortname or longname */
775*4882a593Smuzhiyun const char *src;
776*4882a593Smuzhiyun src = *card->shortname ? card->shortname : card->longname;
777*4882a593Smuzhiyun snd_card_set_id_no_lock(card, src,
778*4882a593Smuzhiyun retrieve_id_from_card_name(src));
779*4882a593Smuzhiyun }
780*4882a593Smuzhiyun snd_cards[card->number] = card;
781*4882a593Smuzhiyun mutex_unlock(&snd_card_mutex);
782*4882a593Smuzhiyun err = snd_info_card_register(card);
783*4882a593Smuzhiyun if (err < 0)
784*4882a593Smuzhiyun return err;
785*4882a593Smuzhiyun
786*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SND_MIXER_OSS)
787*4882a593Smuzhiyun if (snd_mixer_oss_notify_callback)
788*4882a593Smuzhiyun snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
789*4882a593Smuzhiyun #endif
790*4882a593Smuzhiyun return 0;
791*4882a593Smuzhiyun }
792*4882a593Smuzhiyun EXPORT_SYMBOL(snd_card_register);
793*4882a593Smuzhiyun
794*4882a593Smuzhiyun #ifdef CONFIG_SND_PROC_FS
snd_card_info_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)795*4882a593Smuzhiyun static void snd_card_info_read(struct snd_info_entry *entry,
796*4882a593Smuzhiyun struct snd_info_buffer *buffer)
797*4882a593Smuzhiyun {
798*4882a593Smuzhiyun int idx, count;
799*4882a593Smuzhiyun struct snd_card *card;
800*4882a593Smuzhiyun
801*4882a593Smuzhiyun for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
802*4882a593Smuzhiyun mutex_lock(&snd_card_mutex);
803*4882a593Smuzhiyun if ((card = snd_cards[idx]) != NULL) {
804*4882a593Smuzhiyun count++;
805*4882a593Smuzhiyun snd_iprintf(buffer, "%2i [%-15s]: %s - %s\n",
806*4882a593Smuzhiyun idx,
807*4882a593Smuzhiyun card->id,
808*4882a593Smuzhiyun card->driver,
809*4882a593Smuzhiyun card->shortname);
810*4882a593Smuzhiyun snd_iprintf(buffer, " %s\n",
811*4882a593Smuzhiyun card->longname);
812*4882a593Smuzhiyun }
813*4882a593Smuzhiyun mutex_unlock(&snd_card_mutex);
814*4882a593Smuzhiyun }
815*4882a593Smuzhiyun if (!count)
816*4882a593Smuzhiyun snd_iprintf(buffer, "--- no soundcards ---\n");
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun
819*4882a593Smuzhiyun #ifdef CONFIG_SND_OSSEMUL
snd_card_info_read_oss(struct snd_info_buffer * buffer)820*4882a593Smuzhiyun void snd_card_info_read_oss(struct snd_info_buffer *buffer)
821*4882a593Smuzhiyun {
822*4882a593Smuzhiyun int idx, count;
823*4882a593Smuzhiyun struct snd_card *card;
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun for (idx = count = 0; idx < SNDRV_CARDS; idx++) {
826*4882a593Smuzhiyun mutex_lock(&snd_card_mutex);
827*4882a593Smuzhiyun if ((card = snd_cards[idx]) != NULL) {
828*4882a593Smuzhiyun count++;
829*4882a593Smuzhiyun snd_iprintf(buffer, "%s\n", card->longname);
830*4882a593Smuzhiyun }
831*4882a593Smuzhiyun mutex_unlock(&snd_card_mutex);
832*4882a593Smuzhiyun }
833*4882a593Smuzhiyun if (!count) {
834*4882a593Smuzhiyun snd_iprintf(buffer, "--- no soundcards ---\n");
835*4882a593Smuzhiyun }
836*4882a593Smuzhiyun }
837*4882a593Smuzhiyun
838*4882a593Smuzhiyun #endif
839*4882a593Smuzhiyun
840*4882a593Smuzhiyun #ifdef MODULE
snd_card_module_info_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)841*4882a593Smuzhiyun static void snd_card_module_info_read(struct snd_info_entry *entry,
842*4882a593Smuzhiyun struct snd_info_buffer *buffer)
843*4882a593Smuzhiyun {
844*4882a593Smuzhiyun int idx;
845*4882a593Smuzhiyun struct snd_card *card;
846*4882a593Smuzhiyun
847*4882a593Smuzhiyun for (idx = 0; idx < SNDRV_CARDS; idx++) {
848*4882a593Smuzhiyun mutex_lock(&snd_card_mutex);
849*4882a593Smuzhiyun if ((card = snd_cards[idx]) != NULL)
850*4882a593Smuzhiyun snd_iprintf(buffer, "%2i %s\n",
851*4882a593Smuzhiyun idx, card->module->name);
852*4882a593Smuzhiyun mutex_unlock(&snd_card_mutex);
853*4882a593Smuzhiyun }
854*4882a593Smuzhiyun }
855*4882a593Smuzhiyun #endif
856*4882a593Smuzhiyun
snd_card_info_init(void)857*4882a593Smuzhiyun int __init snd_card_info_init(void)
858*4882a593Smuzhiyun {
859*4882a593Smuzhiyun struct snd_info_entry *entry;
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun entry = snd_info_create_module_entry(THIS_MODULE, "cards", NULL);
862*4882a593Smuzhiyun if (! entry)
863*4882a593Smuzhiyun return -ENOMEM;
864*4882a593Smuzhiyun entry->c.text.read = snd_card_info_read;
865*4882a593Smuzhiyun if (snd_info_register(entry) < 0)
866*4882a593Smuzhiyun return -ENOMEM; /* freed in error path */
867*4882a593Smuzhiyun
868*4882a593Smuzhiyun #ifdef MODULE
869*4882a593Smuzhiyun entry = snd_info_create_module_entry(THIS_MODULE, "modules", NULL);
870*4882a593Smuzhiyun if (!entry)
871*4882a593Smuzhiyun return -ENOMEM;
872*4882a593Smuzhiyun entry->c.text.read = snd_card_module_info_read;
873*4882a593Smuzhiyun if (snd_info_register(entry) < 0)
874*4882a593Smuzhiyun return -ENOMEM; /* freed in error path */
875*4882a593Smuzhiyun #endif
876*4882a593Smuzhiyun
877*4882a593Smuzhiyun return 0;
878*4882a593Smuzhiyun }
879*4882a593Smuzhiyun #endif /* CONFIG_SND_PROC_FS */
880*4882a593Smuzhiyun
881*4882a593Smuzhiyun /**
882*4882a593Smuzhiyun * snd_component_add - add a component string
883*4882a593Smuzhiyun * @card: soundcard structure
884*4882a593Smuzhiyun * @component: the component id string
885*4882a593Smuzhiyun *
886*4882a593Smuzhiyun * This function adds the component id string to the supported list.
887*4882a593Smuzhiyun * The component can be referred from the alsa-lib.
888*4882a593Smuzhiyun *
889*4882a593Smuzhiyun * Return: Zero otherwise a negative error code.
890*4882a593Smuzhiyun */
891*4882a593Smuzhiyun
snd_component_add(struct snd_card * card,const char * component)892*4882a593Smuzhiyun int snd_component_add(struct snd_card *card, const char *component)
893*4882a593Smuzhiyun {
894*4882a593Smuzhiyun char *ptr;
895*4882a593Smuzhiyun int len = strlen(component);
896*4882a593Smuzhiyun
897*4882a593Smuzhiyun ptr = strstr(card->components, component);
898*4882a593Smuzhiyun if (ptr != NULL) {
899*4882a593Smuzhiyun if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
900*4882a593Smuzhiyun return 1;
901*4882a593Smuzhiyun }
902*4882a593Smuzhiyun if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
903*4882a593Smuzhiyun snd_BUG();
904*4882a593Smuzhiyun return -ENOMEM;
905*4882a593Smuzhiyun }
906*4882a593Smuzhiyun if (card->components[0] != '\0')
907*4882a593Smuzhiyun strcat(card->components, " ");
908*4882a593Smuzhiyun strcat(card->components, component);
909*4882a593Smuzhiyun return 0;
910*4882a593Smuzhiyun }
911*4882a593Smuzhiyun EXPORT_SYMBOL(snd_component_add);
912*4882a593Smuzhiyun
913*4882a593Smuzhiyun /**
914*4882a593Smuzhiyun * snd_card_file_add - add the file to the file list of the card
915*4882a593Smuzhiyun * @card: soundcard structure
916*4882a593Smuzhiyun * @file: file pointer
917*4882a593Smuzhiyun *
918*4882a593Smuzhiyun * This function adds the file to the file linked-list of the card.
919*4882a593Smuzhiyun * This linked-list is used to keep tracking the connection state,
920*4882a593Smuzhiyun * and to avoid the release of busy resources by hotplug.
921*4882a593Smuzhiyun *
922*4882a593Smuzhiyun * Return: zero or a negative error code.
923*4882a593Smuzhiyun */
snd_card_file_add(struct snd_card * card,struct file * file)924*4882a593Smuzhiyun int snd_card_file_add(struct snd_card *card, struct file *file)
925*4882a593Smuzhiyun {
926*4882a593Smuzhiyun struct snd_monitor_file *mfile;
927*4882a593Smuzhiyun
928*4882a593Smuzhiyun mfile = kmalloc(sizeof(*mfile), GFP_KERNEL);
929*4882a593Smuzhiyun if (mfile == NULL)
930*4882a593Smuzhiyun return -ENOMEM;
931*4882a593Smuzhiyun mfile->file = file;
932*4882a593Smuzhiyun mfile->disconnected_f_op = NULL;
933*4882a593Smuzhiyun INIT_LIST_HEAD(&mfile->shutdown_list);
934*4882a593Smuzhiyun spin_lock(&card->files_lock);
935*4882a593Smuzhiyun if (card->shutdown) {
936*4882a593Smuzhiyun spin_unlock(&card->files_lock);
937*4882a593Smuzhiyun kfree(mfile);
938*4882a593Smuzhiyun return -ENODEV;
939*4882a593Smuzhiyun }
940*4882a593Smuzhiyun list_add(&mfile->list, &card->files_list);
941*4882a593Smuzhiyun get_device(&card->card_dev);
942*4882a593Smuzhiyun spin_unlock(&card->files_lock);
943*4882a593Smuzhiyun return 0;
944*4882a593Smuzhiyun }
945*4882a593Smuzhiyun EXPORT_SYMBOL(snd_card_file_add);
946*4882a593Smuzhiyun
947*4882a593Smuzhiyun /**
948*4882a593Smuzhiyun * snd_card_file_remove - remove the file from the file list
949*4882a593Smuzhiyun * @card: soundcard structure
950*4882a593Smuzhiyun * @file: file pointer
951*4882a593Smuzhiyun *
952*4882a593Smuzhiyun * This function removes the file formerly added to the card via
953*4882a593Smuzhiyun * snd_card_file_add() function.
954*4882a593Smuzhiyun * If all files are removed and snd_card_free_when_closed() was
955*4882a593Smuzhiyun * called beforehand, it processes the pending release of
956*4882a593Smuzhiyun * resources.
957*4882a593Smuzhiyun *
958*4882a593Smuzhiyun * Return: Zero or a negative error code.
959*4882a593Smuzhiyun */
snd_card_file_remove(struct snd_card * card,struct file * file)960*4882a593Smuzhiyun int snd_card_file_remove(struct snd_card *card, struct file *file)
961*4882a593Smuzhiyun {
962*4882a593Smuzhiyun struct snd_monitor_file *mfile, *found = NULL;
963*4882a593Smuzhiyun
964*4882a593Smuzhiyun spin_lock(&card->files_lock);
965*4882a593Smuzhiyun list_for_each_entry(mfile, &card->files_list, list) {
966*4882a593Smuzhiyun if (mfile->file == file) {
967*4882a593Smuzhiyun list_del(&mfile->list);
968*4882a593Smuzhiyun spin_lock(&shutdown_lock);
969*4882a593Smuzhiyun list_del(&mfile->shutdown_list);
970*4882a593Smuzhiyun spin_unlock(&shutdown_lock);
971*4882a593Smuzhiyun if (mfile->disconnected_f_op)
972*4882a593Smuzhiyun fops_put(mfile->disconnected_f_op);
973*4882a593Smuzhiyun found = mfile;
974*4882a593Smuzhiyun break;
975*4882a593Smuzhiyun }
976*4882a593Smuzhiyun }
977*4882a593Smuzhiyun if (list_empty(&card->files_list))
978*4882a593Smuzhiyun wake_up_all(&card->remove_sleep);
979*4882a593Smuzhiyun spin_unlock(&card->files_lock);
980*4882a593Smuzhiyun if (!found) {
981*4882a593Smuzhiyun dev_err(card->dev, "card file remove problem (%p)\n", file);
982*4882a593Smuzhiyun return -ENOENT;
983*4882a593Smuzhiyun }
984*4882a593Smuzhiyun kfree(found);
985*4882a593Smuzhiyun put_device(&card->card_dev);
986*4882a593Smuzhiyun return 0;
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun EXPORT_SYMBOL(snd_card_file_remove);
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun #ifdef CONFIG_PM
991*4882a593Smuzhiyun /**
992*4882a593Smuzhiyun * snd_power_wait - wait until the power-state is changed.
993*4882a593Smuzhiyun * @card: soundcard structure
994*4882a593Smuzhiyun * @power_state: expected power state
995*4882a593Smuzhiyun *
996*4882a593Smuzhiyun * Waits until the power-state is changed.
997*4882a593Smuzhiyun *
998*4882a593Smuzhiyun * Return: Zero if successful, or a negative error code.
999*4882a593Smuzhiyun */
snd_power_wait(struct snd_card * card,unsigned int power_state)1000*4882a593Smuzhiyun int snd_power_wait(struct snd_card *card, unsigned int power_state)
1001*4882a593Smuzhiyun {
1002*4882a593Smuzhiyun wait_queue_entry_t wait;
1003*4882a593Smuzhiyun int result = 0;
1004*4882a593Smuzhiyun
1005*4882a593Smuzhiyun /* fastpath */
1006*4882a593Smuzhiyun if (snd_power_get_state(card) == power_state)
1007*4882a593Smuzhiyun return 0;
1008*4882a593Smuzhiyun init_waitqueue_entry(&wait, current);
1009*4882a593Smuzhiyun add_wait_queue(&card->power_sleep, &wait);
1010*4882a593Smuzhiyun while (1) {
1011*4882a593Smuzhiyun if (card->shutdown) {
1012*4882a593Smuzhiyun result = -ENODEV;
1013*4882a593Smuzhiyun break;
1014*4882a593Smuzhiyun }
1015*4882a593Smuzhiyun if (snd_power_get_state(card) == power_state)
1016*4882a593Smuzhiyun break;
1017*4882a593Smuzhiyun set_current_state(TASK_UNINTERRUPTIBLE);
1018*4882a593Smuzhiyun schedule_timeout(30 * HZ);
1019*4882a593Smuzhiyun }
1020*4882a593Smuzhiyun remove_wait_queue(&card->power_sleep, &wait);
1021*4882a593Smuzhiyun return result;
1022*4882a593Smuzhiyun }
1023*4882a593Smuzhiyun EXPORT_SYMBOL(snd_power_wait);
1024*4882a593Smuzhiyun #endif /* CONFIG_PM */
1025