xref: /linux/sound/core/control.c (revision d8327c784b51b57dac2c26cfad87dce0d68dfd98)
1 /*
2  *  Routines for driver control interface
3  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4  *
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21 
22 #include <sound/driver.h>
23 #include <linux/threads.h>
24 #include <linux/interrupt.h>
25 #include <linux/smp_lock.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/time.h>
29 #include <sound/core.h>
30 #include <sound/minors.h>
31 #include <sound/info.h>
32 #include <sound/control.h>
33 
34 /* max number of user-defined controls */
35 #define MAX_USER_CONTROLS	32
36 
37 struct snd_kctl_ioctl {
38 	struct list_head list;		/* list of all ioctls */
39 	snd_kctl_ioctl_func_t fioctl;
40 };
41 
42 static DECLARE_RWSEM(snd_ioctl_rwsem);
43 static LIST_HEAD(snd_control_ioctls);
44 #ifdef CONFIG_COMPAT
45 static LIST_HEAD(snd_control_compat_ioctls);
46 #endif
47 
48 static int snd_ctl_open(struct inode *inode, struct file *file)
49 {
50 	unsigned long flags;
51 	struct snd_card *card;
52 	struct snd_ctl_file *ctl;
53 	int err;
54 
55 	card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
56 	if (!card) {
57 		err = -ENODEV;
58 		goto __error1;
59 	}
60 	err = snd_card_file_add(card, file);
61 	if (err < 0) {
62 		err = -ENODEV;
63 		goto __error1;
64 	}
65 	if (!try_module_get(card->module)) {
66 		err = -EFAULT;
67 		goto __error2;
68 	}
69 	ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
70 	if (ctl == NULL) {
71 		err = -ENOMEM;
72 		goto __error;
73 	}
74 	INIT_LIST_HEAD(&ctl->events);
75 	init_waitqueue_head(&ctl->change_sleep);
76 	spin_lock_init(&ctl->read_lock);
77 	ctl->card = card;
78 	ctl->pid = current->pid;
79 	file->private_data = ctl;
80 	write_lock_irqsave(&card->ctl_files_rwlock, flags);
81 	list_add_tail(&ctl->list, &card->ctl_files);
82 	write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
83 	return 0;
84 
85       __error:
86 	module_put(card->module);
87       __error2:
88 	snd_card_file_remove(card, file);
89       __error1:
90       	return err;
91 }
92 
93 static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
94 {
95 	struct snd_kctl_event *cread;
96 
97 	spin_lock(&ctl->read_lock);
98 	while (!list_empty(&ctl->events)) {
99 		cread = snd_kctl_event(ctl->events.next);
100 		list_del(&cread->list);
101 		kfree(cread);
102 	}
103 	spin_unlock(&ctl->read_lock);
104 }
105 
106 static int snd_ctl_release(struct inode *inode, struct file *file)
107 {
108 	unsigned long flags;
109 	struct list_head *list;
110 	struct snd_card *card;
111 	struct snd_ctl_file *ctl;
112 	struct snd_kcontrol *control;
113 	unsigned int idx;
114 
115 	ctl = file->private_data;
116 	fasync_helper(-1, file, 0, &ctl->fasync);
117 	file->private_data = NULL;
118 	card = ctl->card;
119 	write_lock_irqsave(&card->ctl_files_rwlock, flags);
120 	list_del(&ctl->list);
121 	write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
122 	down_write(&card->controls_rwsem);
123 	list_for_each(list, &card->controls) {
124 		control = snd_kcontrol(list);
125 		for (idx = 0; idx < control->count; idx++)
126 			if (control->vd[idx].owner == ctl)
127 				control->vd[idx].owner = NULL;
128 	}
129 	up_write(&card->controls_rwsem);
130 	snd_ctl_empty_read_queue(ctl);
131 	kfree(ctl);
132 	module_put(card->module);
133 	snd_card_file_remove(card, file);
134 	return 0;
135 }
136 
137 void snd_ctl_notify(struct snd_card *card, unsigned int mask,
138 		    struct snd_ctl_elem_id *id)
139 {
140 	unsigned long flags;
141 	struct list_head *flist;
142 	struct snd_ctl_file *ctl;
143 	struct snd_kctl_event *ev;
144 
145 	snd_assert(card != NULL && id != NULL, return);
146 	read_lock(&card->ctl_files_rwlock);
147 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
148 	card->mixer_oss_change_count++;
149 #endif
150 	list_for_each(flist, &card->ctl_files) {
151 		struct list_head *elist;
152 		ctl = snd_ctl_file(flist);
153 		if (!ctl->subscribed)
154 			continue;
155 		spin_lock_irqsave(&ctl->read_lock, flags);
156 		list_for_each(elist, &ctl->events) {
157 			ev = snd_kctl_event(elist);
158 			if (ev->id.numid == id->numid) {
159 				ev->mask |= mask;
160 				goto _found;
161 			}
162 		}
163 		ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
164 		if (ev) {
165 			ev->id = *id;
166 			ev->mask = mask;
167 			list_add_tail(&ev->list, &ctl->events);
168 		} else {
169 			snd_printk(KERN_ERR "No memory available to allocate event\n");
170 		}
171 	_found:
172 		wake_up(&ctl->change_sleep);
173 		spin_unlock_irqrestore(&ctl->read_lock, flags);
174 		kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
175 	}
176 	read_unlock(&card->ctl_files_rwlock);
177 }
178 
179 /**
180  * snd_ctl_new - create a control instance from the template
181  * @control: the control template
182  * @access: the default control access
183  *
184  * Allocates a new struct snd_kcontrol instance and copies the given template
185  * to the new instance. It does not copy volatile data (access).
186  *
187  * Returns the pointer of the new instance, or NULL on failure.
188  */
189 struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control, unsigned int access)
190 {
191 	struct snd_kcontrol *kctl;
192 	unsigned int idx;
193 
194 	snd_assert(control != NULL, return NULL);
195 	snd_assert(control->count > 0, return NULL);
196 	kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
197 	if (kctl == NULL) {
198 		snd_printk(KERN_ERR "Cannot allocate control instance\n");
199 		return NULL;
200 	}
201 	*kctl = *control;
202 	for (idx = 0; idx < kctl->count; idx++)
203 		kctl->vd[idx].access = access;
204 	return kctl;
205 }
206 
207 /**
208  * snd_ctl_new1 - create a control instance from the template
209  * @ncontrol: the initialization record
210  * @private_data: the private data to set
211  *
212  * Allocates a new struct snd_kcontrol instance and initialize from the given
213  * template.  When the access field of ncontrol is 0, it's assumed as
214  * READWRITE access. When the count field is 0, it's assumes as one.
215  *
216  * Returns the pointer of the newly generated instance, or NULL on failure.
217  */
218 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
219 				  void *private_data)
220 {
221 	struct snd_kcontrol kctl;
222 	unsigned int access;
223 
224 	snd_assert(ncontrol != NULL, return NULL);
225 	snd_assert(ncontrol->info != NULL, return NULL);
226 	memset(&kctl, 0, sizeof(kctl));
227 	kctl.id.iface = ncontrol->iface;
228 	kctl.id.device = ncontrol->device;
229 	kctl.id.subdevice = ncontrol->subdevice;
230 	if (ncontrol->name)
231 		strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
232 	kctl.id.index = ncontrol->index;
233 	kctl.count = ncontrol->count ? ncontrol->count : 1;
234 	access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
235 		 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|SNDRV_CTL_ELEM_ACCESS_INACTIVE|
236 		 		      SNDRV_CTL_ELEM_ACCESS_DINDIRECT|SNDRV_CTL_ELEM_ACCESS_INDIRECT));
237 	kctl.info = ncontrol->info;
238 	kctl.get = ncontrol->get;
239 	kctl.put = ncontrol->put;
240 	kctl.private_value = ncontrol->private_value;
241 	kctl.private_data = private_data;
242 	return snd_ctl_new(&kctl, access);
243 }
244 
245 /**
246  * snd_ctl_free_one - release the control instance
247  * @kcontrol: the control instance
248  *
249  * Releases the control instance created via snd_ctl_new()
250  * or snd_ctl_new1().
251  * Don't call this after the control was added to the card.
252  */
253 void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
254 {
255 	if (kcontrol) {
256 		if (kcontrol->private_free)
257 			kcontrol->private_free(kcontrol);
258 		kfree(kcontrol);
259 	}
260 }
261 
262 static unsigned int snd_ctl_hole_check(struct snd_card *card,
263 				       unsigned int count)
264 {
265 	struct list_head *list;
266 	struct snd_kcontrol *kctl;
267 
268 	list_for_each(list, &card->controls) {
269 		kctl = snd_kcontrol(list);
270 		if ((kctl->id.numid <= card->last_numid &&
271 		     kctl->id.numid + kctl->count > card->last_numid) ||
272 		    (kctl->id.numid <= card->last_numid + count - 1 &&
273 		     kctl->id.numid + kctl->count > card->last_numid + count - 1))
274 		    	return card->last_numid = kctl->id.numid + kctl->count - 1;
275 	}
276 	return card->last_numid;
277 }
278 
279 static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
280 {
281 	unsigned int last_numid, iter = 100000;
282 
283 	last_numid = card->last_numid;
284 	while (last_numid != snd_ctl_hole_check(card, count)) {
285 		if (--iter == 0) {
286 			/* this situation is very unlikely */
287 			snd_printk(KERN_ERR "unable to allocate new control numid\n");
288 			return -ENOMEM;
289 		}
290 		last_numid = card->last_numid;
291 	}
292 	return 0;
293 }
294 
295 /**
296  * snd_ctl_add - add the control instance to the card
297  * @card: the card instance
298  * @kcontrol: the control instance to add
299  *
300  * Adds the control instance created via snd_ctl_new() or
301  * snd_ctl_new1() to the given card. Assigns also an unique
302  * numid used for fast search.
303  *
304  * Returns zero if successful, or a negative error code on failure.
305  *
306  * It frees automatically the control which cannot be added.
307  */
308 int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
309 {
310 	struct snd_ctl_elem_id id;
311 	unsigned int idx;
312 
313 	snd_assert(card != NULL, return -EINVAL);
314 	if (! kcontrol)
315 		return -EINVAL;
316 	snd_assert(kcontrol->info != NULL, return -EINVAL);
317 	id = kcontrol->id;
318 	down_write(&card->controls_rwsem);
319 	if (snd_ctl_find_id(card, &id)) {
320 		up_write(&card->controls_rwsem);
321 		snd_ctl_free_one(kcontrol);
322 		snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
323 					id.iface,
324 					id.device,
325 					id.subdevice,
326 					id.name,
327 					id.index);
328 		return -EBUSY;
329 	}
330 	if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
331 		up_write(&card->controls_rwsem);
332 		snd_ctl_free_one(kcontrol);
333 		return -ENOMEM;
334 	}
335 	list_add_tail(&kcontrol->list, &card->controls);
336 	card->controls_count += kcontrol->count;
337 	kcontrol->id.numid = card->last_numid + 1;
338 	card->last_numid += kcontrol->count;
339 	up_write(&card->controls_rwsem);
340 	for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
341 		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
342 	return 0;
343 }
344 
345 /**
346  * snd_ctl_remove - remove the control from the card and release it
347  * @card: the card instance
348  * @kcontrol: the control instance to remove
349  *
350  * Removes the control from the card and then releases the instance.
351  * You don't need to call snd_ctl_free_one(). You must be in
352  * the write lock - down_write(&card->controls_rwsem).
353  *
354  * Returns 0 if successful, or a negative error code on failure.
355  */
356 int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
357 {
358 	struct snd_ctl_elem_id id;
359 	unsigned int idx;
360 
361 	snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
362 	list_del(&kcontrol->list);
363 	card->controls_count -= kcontrol->count;
364 	id = kcontrol->id;
365 	for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
366 		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
367 	snd_ctl_free_one(kcontrol);
368 	return 0;
369 }
370 
371 /**
372  * snd_ctl_remove_id - remove the control of the given id and release it
373  * @card: the card instance
374  * @id: the control id to remove
375  *
376  * Finds the control instance with the given id, removes it from the
377  * card list and releases it.
378  *
379  * Returns 0 if successful, or a negative error code on failure.
380  */
381 int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
382 {
383 	struct snd_kcontrol *kctl;
384 	int ret;
385 
386 	down_write(&card->controls_rwsem);
387 	kctl = snd_ctl_find_id(card, id);
388 	if (kctl == NULL) {
389 		up_write(&card->controls_rwsem);
390 		return -ENOENT;
391 	}
392 	ret = snd_ctl_remove(card, kctl);
393 	up_write(&card->controls_rwsem);
394 	return ret;
395 }
396 
397 /**
398  * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it
399  * @file: active control handle
400  * @id: the control id to remove
401  *
402  * Finds the control instance with the given id, removes it from the
403  * card list and releases it.
404  *
405  * Returns 0 if successful, or a negative error code on failure.
406  */
407 static int snd_ctl_remove_unlocked_id(struct snd_ctl_file * file,
408 				      struct snd_ctl_elem_id *id)
409 {
410 	struct snd_card *card = file->card;
411 	struct snd_kcontrol *kctl;
412 	int idx, ret;
413 
414 	down_write(&card->controls_rwsem);
415 	kctl = snd_ctl_find_id(card, id);
416 	if (kctl == NULL) {
417 		up_write(&card->controls_rwsem);
418 		return -ENOENT;
419 	}
420 	for (idx = 0; idx < kctl->count; idx++)
421 		if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
422 			up_write(&card->controls_rwsem);
423 			return -EBUSY;
424 		}
425 	ret = snd_ctl_remove(card, kctl);
426 	up_write(&card->controls_rwsem);
427 	return ret;
428 }
429 
430 /**
431  * snd_ctl_rename_id - replace the id of a control on the card
432  * @card: the card instance
433  * @src_id: the old id
434  * @dst_id: the new id
435  *
436  * Finds the control with the old id from the card, and replaces the
437  * id with the new one.
438  *
439  * Returns zero if successful, or a negative error code on failure.
440  */
441 int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
442 		      struct snd_ctl_elem_id *dst_id)
443 {
444 	struct snd_kcontrol *kctl;
445 
446 	down_write(&card->controls_rwsem);
447 	kctl = snd_ctl_find_id(card, src_id);
448 	if (kctl == NULL) {
449 		up_write(&card->controls_rwsem);
450 		return -ENOENT;
451 	}
452 	kctl->id = *dst_id;
453 	kctl->id.numid = card->last_numid + 1;
454 	card->last_numid += kctl->count;
455 	up_write(&card->controls_rwsem);
456 	return 0;
457 }
458 
459 /**
460  * snd_ctl_find_numid - find the control instance with the given number-id
461  * @card: the card instance
462  * @numid: the number-id to search
463  *
464  * Finds the control instance with the given number-id from the card.
465  *
466  * Returns the pointer of the instance if found, or NULL if not.
467  *
468  * The caller must down card->controls_rwsem before calling this function
469  * (if the race condition can happen).
470  */
471 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
472 {
473 	struct list_head *list;
474 	struct snd_kcontrol *kctl;
475 
476 	snd_assert(card != NULL && numid != 0, return NULL);
477 	list_for_each(list, &card->controls) {
478 		kctl = snd_kcontrol(list);
479 		if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
480 			return kctl;
481 	}
482 	return NULL;
483 }
484 
485 /**
486  * snd_ctl_find_id - find the control instance with the given id
487  * @card: the card instance
488  * @id: the id to search
489  *
490  * Finds the control instance with the given id from the card.
491  *
492  * Returns the pointer of the instance if found, or NULL if not.
493  *
494  * The caller must down card->controls_rwsem before calling this function
495  * (if the race condition can happen).
496  */
497 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
498 				     struct snd_ctl_elem_id *id)
499 {
500 	struct list_head *list;
501 	struct snd_kcontrol *kctl;
502 
503 	snd_assert(card != NULL && id != NULL, return NULL);
504 	if (id->numid != 0)
505 		return snd_ctl_find_numid(card, id->numid);
506 	list_for_each(list, &card->controls) {
507 		kctl = snd_kcontrol(list);
508 		if (kctl->id.iface != id->iface)
509 			continue;
510 		if (kctl->id.device != id->device)
511 			continue;
512 		if (kctl->id.subdevice != id->subdevice)
513 			continue;
514 		if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
515 			continue;
516 		if (kctl->id.index > id->index)
517 			continue;
518 		if (kctl->id.index + kctl->count <= id->index)
519 			continue;
520 		return kctl;
521 	}
522 	return NULL;
523 }
524 
525 static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
526 			     unsigned int cmd, void __user *arg)
527 {
528 	struct snd_ctl_card_info *info;
529 
530 	info = kzalloc(sizeof(*info), GFP_KERNEL);
531 	if (! info)
532 		return -ENOMEM;
533 	down_read(&snd_ioctl_rwsem);
534 	info->card = card->number;
535 	strlcpy(info->id, card->id, sizeof(info->id));
536 	strlcpy(info->driver, card->driver, sizeof(info->driver));
537 	strlcpy(info->name, card->shortname, sizeof(info->name));
538 	strlcpy(info->longname, card->longname, sizeof(info->longname));
539 	strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
540 	strlcpy(info->components, card->components, sizeof(info->components));
541 	up_read(&snd_ioctl_rwsem);
542 	if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
543 		kfree(info);
544 		return -EFAULT;
545 	}
546 	kfree(info);
547 	return 0;
548 }
549 
550 static int snd_ctl_elem_list(struct snd_card *card,
551 			     struct snd_ctl_elem_list __user *_list)
552 {
553 	struct list_head *plist;
554 	struct snd_ctl_elem_list list;
555 	struct snd_kcontrol *kctl;
556 	struct snd_ctl_elem_id *dst, *id;
557 	unsigned int offset, space, first, jidx;
558 
559 	if (copy_from_user(&list, _list, sizeof(list)))
560 		return -EFAULT;
561 	offset = list.offset;
562 	space = list.space;
563 	first = 0;
564 	/* try limit maximum space */
565 	if (space > 16384)
566 		return -ENOMEM;
567 	if (space > 0) {
568 		/* allocate temporary buffer for atomic operation */
569 		dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
570 		if (dst == NULL)
571 			return -ENOMEM;
572 		down_read(&card->controls_rwsem);
573 		list.count = card->controls_count;
574 		plist = card->controls.next;
575 		while (plist != &card->controls) {
576 			if (offset == 0)
577 				break;
578 			kctl = snd_kcontrol(plist);
579 			if (offset < kctl->count)
580 				break;
581 			offset -= kctl->count;
582 			plist = plist->next;
583 		}
584 		list.used = 0;
585 		id = dst;
586 		while (space > 0 && plist != &card->controls) {
587 			kctl = snd_kcontrol(plist);
588 			for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
589 				snd_ctl_build_ioff(id, kctl, jidx);
590 				id++;
591 				space--;
592 				list.used++;
593 			}
594 			plist = plist->next;
595 			offset = 0;
596 		}
597 		up_read(&card->controls_rwsem);
598 		if (list.used > 0 &&
599 		    copy_to_user(list.pids, dst,
600 				 list.used * sizeof(struct snd_ctl_elem_id))) {
601 			vfree(dst);
602 			return -EFAULT;
603 		}
604 		vfree(dst);
605 	} else {
606 		down_read(&card->controls_rwsem);
607 		list.count = card->controls_count;
608 		up_read(&card->controls_rwsem);
609 	}
610 	if (copy_to_user(_list, &list, sizeof(list)))
611 		return -EFAULT;
612 	return 0;
613 }
614 
615 static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
616 			     struct snd_ctl_elem_info *info)
617 {
618 	struct snd_card *card = ctl->card;
619 	struct snd_kcontrol *kctl;
620 	struct snd_kcontrol_volatile *vd;
621 	unsigned int index_offset;
622 	int result;
623 
624 	down_read(&card->controls_rwsem);
625 	kctl = snd_ctl_find_id(card, &info->id);
626 	if (kctl == NULL) {
627 		up_read(&card->controls_rwsem);
628 		return -ENOENT;
629 	}
630 #ifdef CONFIG_SND_DEBUG
631 	info->access = 0;
632 #endif
633 	result = kctl->info(kctl, info);
634 	if (result >= 0) {
635 		snd_assert(info->access == 0, );
636 		index_offset = snd_ctl_get_ioff(kctl, &info->id);
637 		vd = &kctl->vd[index_offset];
638 		snd_ctl_build_ioff(&info->id, kctl, index_offset);
639 		info->access = vd->access;
640 		if (vd->owner) {
641 			info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
642 			if (vd->owner == ctl)
643 				info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
644 			info->owner = vd->owner_pid;
645 		} else {
646 			info->owner = -1;
647 		}
648 	}
649 	up_read(&card->controls_rwsem);
650 	return result;
651 }
652 
653 static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
654 				  struct snd_ctl_elem_info __user *_info)
655 {
656 	struct snd_ctl_elem_info info;
657 	int result;
658 
659 	if (copy_from_user(&info, _info, sizeof(info)))
660 		return -EFAULT;
661 	snd_power_lock(ctl->card);
662 	result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0, NULL);
663 	if (result >= 0)
664 		result = snd_ctl_elem_info(ctl, &info);
665 	snd_power_unlock(ctl->card);
666 	if (result >= 0)
667 		if (copy_to_user(_info, &info, sizeof(info)))
668 			return -EFAULT;
669 	return result;
670 }
671 
672 int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
673 {
674 	struct snd_kcontrol *kctl;
675 	struct snd_kcontrol_volatile *vd;
676 	unsigned int index_offset;
677 	int result, indirect;
678 
679 	down_read(&card->controls_rwsem);
680 	kctl = snd_ctl_find_id(card, &control->id);
681 	if (kctl == NULL) {
682 		result = -ENOENT;
683 	} else {
684 		index_offset = snd_ctl_get_ioff(kctl, &control->id);
685 		vd = &kctl->vd[index_offset];
686 		indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
687 		if (control->indirect != indirect) {
688 			result = -EACCES;
689 		} else {
690 			if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) {
691 				snd_ctl_build_ioff(&control->id, kctl, index_offset);
692 				result = kctl->get(kctl, control);
693 			} else {
694 				result = -EPERM;
695 			}
696 		}
697 	}
698 	up_read(&card->controls_rwsem);
699 	return result;
700 }
701 
702 static int snd_ctl_elem_read_user(struct snd_card *card,
703 				  struct snd_ctl_elem_value __user *_control)
704 {
705 	struct snd_ctl_elem_value *control;
706 	int result;
707 
708 	control = kmalloc(sizeof(*control), GFP_KERNEL);
709 	if (control == NULL)
710 		return -ENOMEM;
711 	if (copy_from_user(control, _control, sizeof(*control))) {
712 		kfree(control);
713 		return -EFAULT;
714 	}
715 	snd_power_lock(card);
716 	result = snd_power_wait(card, SNDRV_CTL_POWER_D0, NULL);
717 	if (result >= 0)
718 		result = snd_ctl_elem_read(card, control);
719 	snd_power_unlock(card);
720 	if (result >= 0)
721 		if (copy_to_user(_control, control, sizeof(*control)))
722 			result = -EFAULT;
723 	kfree(control);
724 	return result;
725 }
726 
727 int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
728 		       struct snd_ctl_elem_value *control)
729 {
730 	struct snd_kcontrol *kctl;
731 	struct snd_kcontrol_volatile *vd;
732 	unsigned int index_offset;
733 	int result, indirect;
734 
735 	down_read(&card->controls_rwsem);
736 	kctl = snd_ctl_find_id(card, &control->id);
737 	if (kctl == NULL) {
738 		result = -ENOENT;
739 	} else {
740 		index_offset = snd_ctl_get_ioff(kctl, &control->id);
741 		vd = &kctl->vd[index_offset];
742 		indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
743 		if (control->indirect != indirect) {
744 			result = -EACCES;
745 		} else {
746 			if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
747 			    kctl->put == NULL ||
748 			    (file && vd->owner != NULL && vd->owner != file)) {
749 				result = -EPERM;
750 			} else {
751 				snd_ctl_build_ioff(&control->id, kctl, index_offset);
752 				result = kctl->put(kctl, control);
753 			}
754 			if (result > 0) {
755 				up_read(&card->controls_rwsem);
756 				snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
757 				return 0;
758 			}
759 		}
760 	}
761 	up_read(&card->controls_rwsem);
762 	return result;
763 }
764 
765 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
766 				   struct snd_ctl_elem_value __user *_control)
767 {
768 	struct snd_ctl_elem_value *control;
769 	struct snd_card *card;
770 	int result;
771 
772 	control = kmalloc(sizeof(*control), GFP_KERNEL);
773 	if (control == NULL)
774 		return -ENOMEM;
775 	if (copy_from_user(control, _control, sizeof(*control))) {
776 		kfree(control);
777 		return -EFAULT;
778 	}
779 	card = file->card;
780 	snd_power_lock(card);
781 	result = snd_power_wait(card, SNDRV_CTL_POWER_D0, NULL);
782 	if (result >= 0)
783 		result = snd_ctl_elem_write(card, file, control);
784 	snd_power_unlock(card);
785 	if (result >= 0)
786 		if (copy_to_user(_control, control, sizeof(*control)))
787 			result = -EFAULT;
788 	kfree(control);
789 	return result;
790 }
791 
792 static int snd_ctl_elem_lock(struct snd_ctl_file *file,
793 			     struct snd_ctl_elem_id __user *_id)
794 {
795 	struct snd_card *card = file->card;
796 	struct snd_ctl_elem_id id;
797 	struct snd_kcontrol *kctl;
798 	struct snd_kcontrol_volatile *vd;
799 	int result;
800 
801 	if (copy_from_user(&id, _id, sizeof(id)))
802 		return -EFAULT;
803 	down_write(&card->controls_rwsem);
804 	kctl = snd_ctl_find_id(card, &id);
805 	if (kctl == NULL) {
806 		result = -ENOENT;
807 	} else {
808 		vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
809 		if (vd->owner != NULL)
810 			result = -EBUSY;
811 		else {
812 			vd->owner = file;
813 			vd->owner_pid = current->pid;
814 			result = 0;
815 		}
816 	}
817 	up_write(&card->controls_rwsem);
818 	return result;
819 }
820 
821 static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
822 			       struct snd_ctl_elem_id __user *_id)
823 {
824 	struct snd_card *card = file->card;
825 	struct snd_ctl_elem_id id;
826 	struct snd_kcontrol *kctl;
827 	struct snd_kcontrol_volatile *vd;
828 	int result;
829 
830 	if (copy_from_user(&id, _id, sizeof(id)))
831 		return -EFAULT;
832 	down_write(&card->controls_rwsem);
833 	kctl = snd_ctl_find_id(card, &id);
834 	if (kctl == NULL) {
835 		result = -ENOENT;
836 	} else {
837 		vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
838 		if (vd->owner == NULL)
839 			result = -EINVAL;
840 		else if (vd->owner != file)
841 			result = -EPERM;
842 		else {
843 			vd->owner = NULL;
844 			vd->owner_pid = 0;
845 			result = 0;
846 		}
847 	}
848 	up_write(&card->controls_rwsem);
849 	return result;
850 }
851 
852 struct user_element {
853 	struct snd_ctl_elem_info info;
854 	void *elem_data;		/* element data */
855 	unsigned long elem_data_size;	/* size of element data in bytes */
856 	void *priv_data;		/* private data (like strings for enumerated type) */
857 	unsigned long priv_data_size;	/* size of private data in bytes */
858 };
859 
860 static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
861 				  struct snd_ctl_elem_info *uinfo)
862 {
863 	struct user_element *ue = kcontrol->private_data;
864 
865 	*uinfo = ue->info;
866 	return 0;
867 }
868 
869 static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
870 				 struct snd_ctl_elem_value *ucontrol)
871 {
872 	struct user_element *ue = kcontrol->private_data;
873 
874 	memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
875 	return 0;
876 }
877 
878 static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
879 				 struct snd_ctl_elem_value *ucontrol)
880 {
881 	int change;
882 	struct user_element *ue = kcontrol->private_data;
883 
884 	change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
885 	if (change)
886 		memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
887 	return change;
888 }
889 
890 static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
891 {
892 	kfree(kcontrol->private_data);
893 }
894 
895 static int snd_ctl_elem_add(struct snd_ctl_file *file,
896 			    struct snd_ctl_elem_info *info, int replace)
897 {
898 	struct snd_card *card = file->card;
899 	struct snd_kcontrol kctl, *_kctl;
900 	unsigned int access;
901 	long private_size;
902 	struct user_element *ue;
903 	int idx, err;
904 
905 	if (card->user_ctl_count >= MAX_USER_CONTROLS)
906 		return -ENOMEM;
907 	if (info->count > 1024)
908 		return -EINVAL;
909 	access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
910 		(info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
911 				 SNDRV_CTL_ELEM_ACCESS_INACTIVE));
912 	info->id.numid = 0;
913 	memset(&kctl, 0, sizeof(kctl));
914 	down_write(&card->controls_rwsem);
915 	_kctl = snd_ctl_find_id(card, &info->id);
916 	err = 0;
917 	if (_kctl) {
918 		if (replace)
919 			err = snd_ctl_remove(card, _kctl);
920 		else
921 			err = -EBUSY;
922 	} else {
923 		if (replace)
924 			err = -ENOENT;
925 	}
926 	up_write(&card->controls_rwsem);
927 	if (err < 0)
928 		return err;
929 	memcpy(&kctl.id, &info->id, sizeof(info->id));
930 	kctl.count = info->owner ? info->owner : 1;
931 	access |= SNDRV_CTL_ELEM_ACCESS_USER;
932 	kctl.info = snd_ctl_elem_user_info;
933 	if (access & SNDRV_CTL_ELEM_ACCESS_READ)
934 		kctl.get = snd_ctl_elem_user_get;
935 	if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
936 		kctl.put = snd_ctl_elem_user_put;
937 	switch (info->type) {
938 	case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
939 		private_size = sizeof(char);
940 		if (info->count > 128)
941 			return -EINVAL;
942 		break;
943 	case SNDRV_CTL_ELEM_TYPE_INTEGER:
944 		private_size = sizeof(long);
945 		if (info->count > 128)
946 			return -EINVAL;
947 		break;
948 	case SNDRV_CTL_ELEM_TYPE_INTEGER64:
949 		private_size = sizeof(long long);
950 		if (info->count > 64)
951 			return -EINVAL;
952 		break;
953 	case SNDRV_CTL_ELEM_TYPE_BYTES:
954 		private_size = sizeof(unsigned char);
955 		if (info->count > 512)
956 			return -EINVAL;
957 		break;
958 	case SNDRV_CTL_ELEM_TYPE_IEC958:
959 		private_size = sizeof(struct snd_aes_iec958);
960 		if (info->count != 1)
961 			return -EINVAL;
962 		break;
963 	default:
964 		return -EINVAL;
965 	}
966 	private_size *= info->count;
967 	ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
968 	if (ue == NULL)
969 		return -ENOMEM;
970 	ue->info = *info;
971 	ue->elem_data = (char *)ue + sizeof(*ue);
972 	ue->elem_data_size = private_size;
973 	kctl.private_free = snd_ctl_elem_user_free;
974 	_kctl = snd_ctl_new(&kctl, access);
975 	if (_kctl == NULL) {
976 		kfree(ue);
977 		return -ENOMEM;
978 	}
979 	_kctl->private_data = ue;
980 	for (idx = 0; idx < _kctl->count; idx++)
981 		_kctl->vd[idx].owner = file;
982 	err = snd_ctl_add(card, _kctl);
983 	if (err < 0)
984 		return err;
985 
986 	down_write(&card->controls_rwsem);
987 	card->user_ctl_count++;
988 	up_write(&card->controls_rwsem);
989 
990 	return 0;
991 }
992 
993 static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
994 				 struct snd_ctl_elem_info __user *_info, int replace)
995 {
996 	struct snd_ctl_elem_info info;
997 	if (copy_from_user(&info, _info, sizeof(info)))
998 		return -EFAULT;
999 	return snd_ctl_elem_add(file, &info, replace);
1000 }
1001 
1002 static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1003 			       struct snd_ctl_elem_id __user *_id)
1004 {
1005 	struct snd_ctl_elem_id id;
1006 	int err;
1007 
1008 	if (copy_from_user(&id, _id, sizeof(id)))
1009 		return -EFAULT;
1010 	err = snd_ctl_remove_unlocked_id(file, &id);
1011 	if (! err) {
1012 		struct snd_card *card = file->card;
1013 		down_write(&card->controls_rwsem);
1014 		card->user_ctl_count--;
1015 		up_write(&card->controls_rwsem);
1016 	}
1017 	return err;
1018 }
1019 
1020 static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
1021 {
1022 	int subscribe;
1023 	if (get_user(subscribe, ptr))
1024 		return -EFAULT;
1025 	if (subscribe < 0) {
1026 		subscribe = file->subscribed;
1027 		if (put_user(subscribe, ptr))
1028 			return -EFAULT;
1029 		return 0;
1030 	}
1031 	if (subscribe) {
1032 		file->subscribed = 1;
1033 		return 0;
1034 	} else if (file->subscribed) {
1035 		snd_ctl_empty_read_queue(file);
1036 		file->subscribed = 0;
1037 	}
1038 	return 0;
1039 }
1040 
1041 static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1042 {
1043 	struct snd_ctl_file *ctl;
1044 	struct snd_card *card;
1045 	struct list_head *list;
1046 	struct snd_kctl_ioctl *p;
1047 	void __user *argp = (void __user *)arg;
1048 	int __user *ip = argp;
1049 	int err;
1050 
1051 	ctl = file->private_data;
1052 	card = ctl->card;
1053 	snd_assert(card != NULL, return -ENXIO);
1054 	switch (cmd) {
1055 	case SNDRV_CTL_IOCTL_PVERSION:
1056 		return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1057 	case SNDRV_CTL_IOCTL_CARD_INFO:
1058 		return snd_ctl_card_info(card, ctl, cmd, argp);
1059 	case SNDRV_CTL_IOCTL_ELEM_LIST:
1060 		return snd_ctl_elem_list(ctl->card, argp);
1061 	case SNDRV_CTL_IOCTL_ELEM_INFO:
1062 		return snd_ctl_elem_info_user(ctl, argp);
1063 	case SNDRV_CTL_IOCTL_ELEM_READ:
1064 		return snd_ctl_elem_read_user(ctl->card, argp);
1065 	case SNDRV_CTL_IOCTL_ELEM_WRITE:
1066 		return snd_ctl_elem_write_user(ctl, argp);
1067 	case SNDRV_CTL_IOCTL_ELEM_LOCK:
1068 		return snd_ctl_elem_lock(ctl, argp);
1069 	case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1070 		return snd_ctl_elem_unlock(ctl, argp);
1071 	case SNDRV_CTL_IOCTL_ELEM_ADD:
1072 		return snd_ctl_elem_add_user(ctl, argp, 0);
1073 	case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1074 		return snd_ctl_elem_add_user(ctl, argp, 1);
1075 	case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1076 		return snd_ctl_elem_remove(ctl, argp);
1077 	case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1078 		return snd_ctl_subscribe_events(ctl, ip);
1079 	case SNDRV_CTL_IOCTL_POWER:
1080 		return -ENOPROTOOPT;
1081 	case SNDRV_CTL_IOCTL_POWER_STATE:
1082 #ifdef CONFIG_PM
1083 		return put_user(card->power_state, ip) ? -EFAULT : 0;
1084 #else
1085 		return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1086 #endif
1087 	}
1088 	down_read(&snd_ioctl_rwsem);
1089 	list_for_each(list, &snd_control_ioctls) {
1090 		p = list_entry(list, struct snd_kctl_ioctl, list);
1091 		err = p->fioctl(card, ctl, cmd, arg);
1092 		if (err != -ENOIOCTLCMD) {
1093 			up_read(&snd_ioctl_rwsem);
1094 			return err;
1095 		}
1096 	}
1097 	up_read(&snd_ioctl_rwsem);
1098 	snd_printdd("unknown ioctl = 0x%x\n", cmd);
1099 	return -ENOTTY;
1100 }
1101 
1102 static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1103 			    size_t count, loff_t * offset)
1104 {
1105 	struct snd_ctl_file *ctl;
1106 	int err = 0;
1107 	ssize_t result = 0;
1108 
1109 	ctl = file->private_data;
1110 	snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
1111 	if (!ctl->subscribed)
1112 		return -EBADFD;
1113 	if (count < sizeof(struct snd_ctl_event))
1114 		return -EINVAL;
1115 	spin_lock_irq(&ctl->read_lock);
1116 	while (count >= sizeof(struct snd_ctl_event)) {
1117 		struct snd_ctl_event ev;
1118 		struct snd_kctl_event *kev;
1119 		while (list_empty(&ctl->events)) {
1120 			wait_queue_t wait;
1121 			if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1122 				err = -EAGAIN;
1123 				goto __end_lock;
1124 			}
1125 			init_waitqueue_entry(&wait, current);
1126 			add_wait_queue(&ctl->change_sleep, &wait);
1127 			set_current_state(TASK_INTERRUPTIBLE);
1128 			spin_unlock_irq(&ctl->read_lock);
1129 			schedule();
1130 			remove_wait_queue(&ctl->change_sleep, &wait);
1131 			if (signal_pending(current))
1132 				return result > 0 ? result : -ERESTARTSYS;
1133 			spin_lock_irq(&ctl->read_lock);
1134 		}
1135 		kev = snd_kctl_event(ctl->events.next);
1136 		ev.type = SNDRV_CTL_EVENT_ELEM;
1137 		ev.data.elem.mask = kev->mask;
1138 		ev.data.elem.id = kev->id;
1139 		list_del(&kev->list);
1140 		spin_unlock_irq(&ctl->read_lock);
1141 		kfree(kev);
1142 		if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
1143 			err = -EFAULT;
1144 			goto __end;
1145 		}
1146 		spin_lock_irq(&ctl->read_lock);
1147 		buffer += sizeof(struct snd_ctl_event);
1148 		count -= sizeof(struct snd_ctl_event);
1149 		result += sizeof(struct snd_ctl_event);
1150 	}
1151       __end_lock:
1152 	spin_unlock_irq(&ctl->read_lock);
1153       __end:
1154       	return result > 0 ? result : err;
1155 }
1156 
1157 static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1158 {
1159 	unsigned int mask;
1160 	struct snd_ctl_file *ctl;
1161 
1162 	ctl = file->private_data;
1163 	if (!ctl->subscribed)
1164 		return 0;
1165 	poll_wait(file, &ctl->change_sleep, wait);
1166 
1167 	mask = 0;
1168 	if (!list_empty(&ctl->events))
1169 		mask |= POLLIN | POLLRDNORM;
1170 
1171 	return mask;
1172 }
1173 
1174 /*
1175  * register the device-specific control-ioctls.
1176  * called from each device manager like pcm.c, hwdep.c, etc.
1177  */
1178 static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1179 {
1180 	struct snd_kctl_ioctl *pn;
1181 
1182 	pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
1183 	if (pn == NULL)
1184 		return -ENOMEM;
1185 	pn->fioctl = fcn;
1186 	down_write(&snd_ioctl_rwsem);
1187 	list_add_tail(&pn->list, lists);
1188 	up_write(&snd_ioctl_rwsem);
1189 	return 0;
1190 }
1191 
1192 int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1193 {
1194 	return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1195 }
1196 
1197 #ifdef CONFIG_COMPAT
1198 int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1199 {
1200 	return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1201 }
1202 #endif
1203 
1204 /*
1205  * de-register the device-specific control-ioctls.
1206  */
1207 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1208 				     struct list_head *lists)
1209 {
1210 	struct list_head *list;
1211 	struct snd_kctl_ioctl *p;
1212 
1213 	snd_assert(fcn != NULL, return -EINVAL);
1214 	down_write(&snd_ioctl_rwsem);
1215 	list_for_each(list, lists) {
1216 		p = list_entry(list, struct snd_kctl_ioctl, list);
1217 		if (p->fioctl == fcn) {
1218 			list_del(&p->list);
1219 			up_write(&snd_ioctl_rwsem);
1220 			kfree(p);
1221 			return 0;
1222 		}
1223 	}
1224 	up_write(&snd_ioctl_rwsem);
1225 	snd_BUG();
1226 	return -EINVAL;
1227 }
1228 
1229 int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1230 {
1231 	return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1232 }
1233 
1234 #ifdef CONFIG_COMPAT
1235 int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1236 {
1237 	return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1238 }
1239 
1240 #endif
1241 
1242 static int snd_ctl_fasync(int fd, struct file * file, int on)
1243 {
1244 	struct snd_ctl_file *ctl;
1245 	int err;
1246 	ctl = file->private_data;
1247 	err = fasync_helper(fd, file, on, &ctl->fasync);
1248 	if (err < 0)
1249 		return err;
1250 	return 0;
1251 }
1252 
1253 /*
1254  * ioctl32 compat
1255  */
1256 #ifdef CONFIG_COMPAT
1257 #include "control_compat.c"
1258 #else
1259 #define snd_ctl_ioctl_compat	NULL
1260 #endif
1261 
1262 /*
1263  *  INIT PART
1264  */
1265 
1266 static struct file_operations snd_ctl_f_ops =
1267 {
1268 	.owner =	THIS_MODULE,
1269 	.read =		snd_ctl_read,
1270 	.open =		snd_ctl_open,
1271 	.release =	snd_ctl_release,
1272 	.poll =		snd_ctl_poll,
1273 	.unlocked_ioctl =	snd_ctl_ioctl,
1274 	.compat_ioctl =	snd_ctl_ioctl_compat,
1275 	.fasync =	snd_ctl_fasync,
1276 };
1277 
1278 /*
1279  * registration of the control device
1280  */
1281 static int snd_ctl_dev_register(struct snd_device *device)
1282 {
1283 	struct snd_card *card = device->device_data;
1284 	int err, cardnum;
1285 	char name[16];
1286 
1287 	snd_assert(card != NULL, return -ENXIO);
1288 	cardnum = card->number;
1289 	snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1290 	sprintf(name, "controlC%i", cardnum);
1291 	if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1292 				       &snd_ctl_f_ops, card, name)) < 0)
1293 		return err;
1294 	return 0;
1295 }
1296 
1297 /*
1298  * disconnection of the control device
1299  */
1300 static int snd_ctl_dev_disconnect(struct snd_device *device)
1301 {
1302 	struct snd_card *card = device->device_data;
1303 	struct list_head *flist;
1304 	struct snd_ctl_file *ctl;
1305 
1306 	down_read(&card->controls_rwsem);
1307 	list_for_each(flist, &card->ctl_files) {
1308 		ctl = snd_ctl_file(flist);
1309 		wake_up(&ctl->change_sleep);
1310 		kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1311 	}
1312 	up_read(&card->controls_rwsem);
1313 	return 0;
1314 }
1315 
1316 /*
1317  * free all controls
1318  */
1319 static int snd_ctl_dev_free(struct snd_device *device)
1320 {
1321 	struct snd_card *card = device->device_data;
1322 	struct snd_kcontrol *control;
1323 
1324 	down_write(&card->controls_rwsem);
1325 	while (!list_empty(&card->controls)) {
1326 		control = snd_kcontrol(card->controls.next);
1327 		snd_ctl_remove(card, control);
1328 	}
1329 	up_write(&card->controls_rwsem);
1330 	return 0;
1331 }
1332 
1333 /*
1334  * de-registration of the control device
1335  */
1336 static int snd_ctl_dev_unregister(struct snd_device *device)
1337 {
1338 	struct snd_card *card = device->device_data;
1339 	int err, cardnum;
1340 
1341 	snd_assert(card != NULL, return -ENXIO);
1342 	cardnum = card->number;
1343 	snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1344 	if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1345 					 card, -1)) < 0)
1346 		return err;
1347 	return snd_ctl_dev_free(device);
1348 }
1349 
1350 /*
1351  * create control core:
1352  * called from init.c
1353  */
1354 int snd_ctl_create(struct snd_card *card)
1355 {
1356 	static struct snd_device_ops ops = {
1357 		.dev_free = snd_ctl_dev_free,
1358 		.dev_register =	snd_ctl_dev_register,
1359 		.dev_disconnect = snd_ctl_dev_disconnect,
1360 		.dev_unregister = snd_ctl_dev_unregister
1361 	};
1362 
1363 	snd_assert(card != NULL, return -ENXIO);
1364 	return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1365 }
1366