blob: 03ae9bb7d38ee33bea2bd5965fb4db153970e3f2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Takashi Iwai82e9bae2005-11-17 13:53:23 +010037struct snd_kctl_ioctl {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct list_head list; /* list of all ioctls */
39 snd_kctl_ioctl_func_t fioctl;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010040};
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42static DECLARE_RWSEM(snd_ioctl_rwsem);
43static LIST_HEAD(snd_control_ioctls);
44#ifdef CONFIG_COMPAT
45static LIST_HEAD(snd_control_compat_ioctls);
46#endif
47
48static int snd_ctl_open(struct inode *inode, struct file *file)
49{
50 int cardnum = SNDRV_MINOR_CARD(iminor(inode));
51 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010052 struct snd_card *card;
53 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 int err;
55
56 card = snd_cards[cardnum];
57 if (!card) {
58 err = -ENODEV;
59 goto __error1;
60 }
61 err = snd_card_file_add(card, file);
62 if (err < 0) {
63 err = -ENODEV;
64 goto __error1;
65 }
66 if (!try_module_get(card->module)) {
67 err = -EFAULT;
68 goto __error2;
69 }
Takashi Iwaica2c0962005-09-09 14:20:23 +020070 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (ctl == NULL) {
72 err = -ENOMEM;
73 goto __error;
74 }
75 INIT_LIST_HEAD(&ctl->events);
76 init_waitqueue_head(&ctl->change_sleep);
77 spin_lock_init(&ctl->read_lock);
78 ctl->card = card;
79 ctl->pid = current->pid;
80 file->private_data = ctl;
81 write_lock_irqsave(&card->ctl_files_rwlock, flags);
82 list_add_tail(&ctl->list, &card->ctl_files);
83 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
84 return 0;
85
86 __error:
87 module_put(card->module);
88 __error2:
89 snd_card_file_remove(card, file);
90 __error1:
91 return err;
92}
93
Takashi Iwai82e9bae2005-11-17 13:53:23 +010094static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Takashi Iwai82e9bae2005-11-17 13:53:23 +010096 struct snd_kctl_event *cread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 spin_lock(&ctl->read_lock);
99 while (!list_empty(&ctl->events)) {
100 cread = snd_kctl_event(ctl->events.next);
101 list_del(&cread->list);
102 kfree(cread);
103 }
104 spin_unlock(&ctl->read_lock);
105}
106
107static int snd_ctl_release(struct inode *inode, struct file *file)
108{
109 unsigned long flags;
110 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100111 struct snd_card *card;
112 struct snd_ctl_file *ctl;
113 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 unsigned int idx;
115
116 ctl = file->private_data;
117 fasync_helper(-1, file, 0, &ctl->fasync);
118 file->private_data = NULL;
119 card = ctl->card;
120 write_lock_irqsave(&card->ctl_files_rwlock, flags);
121 list_del(&ctl->list);
122 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
123 down_write(&card->controls_rwsem);
124 list_for_each(list, &card->controls) {
125 control = snd_kcontrol(list);
126 for (idx = 0; idx < control->count; idx++)
127 if (control->vd[idx].owner == ctl)
128 control->vd[idx].owner = NULL;
129 }
130 up_write(&card->controls_rwsem);
131 snd_ctl_empty_read_queue(ctl);
132 kfree(ctl);
133 module_put(card->module);
134 snd_card_file_remove(card, file);
135 return 0;
136}
137
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100138void snd_ctl_notify(struct snd_card *card, unsigned int mask,
139 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 unsigned long flags;
142 struct list_head *flist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100143 struct snd_ctl_file *ctl;
144 struct snd_kctl_event *ev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200146 snd_assert(card != NULL && id != NULL, return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 read_lock(&card->ctl_files_rwlock);
148#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
149 card->mixer_oss_change_count++;
150#endif
151 list_for_each(flist, &card->ctl_files) {
152 struct list_head *elist;
153 ctl = snd_ctl_file(flist);
154 if (!ctl->subscribed)
155 continue;
156 spin_lock_irqsave(&ctl->read_lock, flags);
157 list_for_each(elist, &ctl->events) {
158 ev = snd_kctl_event(elist);
159 if (ev->id.numid == id->numid) {
160 ev->mask |= mask;
161 goto _found;
162 }
163 }
Takashi Iwaica2c0962005-09-09 14:20:23 +0200164 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (ev) {
166 ev->id = *id;
167 ev->mask = mask;
168 list_add_tail(&ev->list, &ctl->events);
169 } else {
170 snd_printk(KERN_ERR "No memory available to allocate event\n");
171 }
172 _found:
173 wake_up(&ctl->change_sleep);
174 spin_unlock_irqrestore(&ctl->read_lock, flags);
175 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
176 }
177 read_unlock(&card->ctl_files_rwlock);
178}
179
180/**
181 * snd_ctl_new - create a control instance from the template
182 * @control: the control template
183 * @access: the default control access
184 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100185 * Allocates a new struct snd_kcontrol instance and copies the given template
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * to the new instance. It does not copy volatile data (access).
187 *
188 * Returns the pointer of the new instance, or NULL on failure.
189 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100190struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control, unsigned int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100192 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 unsigned int idx;
194
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200195 snd_assert(control != NULL, return NULL);
196 snd_assert(control->count > 0, return NULL);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100197 kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (kctl == NULL)
199 return NULL;
200 *kctl = *control;
201 for (idx = 0; idx < kctl->count; idx++)
202 kctl->vd[idx].access = access;
203 return kctl;
204}
205
206/**
207 * snd_ctl_new1 - create a control instance from the template
208 * @ncontrol: the initialization record
209 * @private_data: the private data to set
210 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100211 * Allocates a new struct snd_kcontrol instance and initialize from the given
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * template. When the access field of ncontrol is 0, it's assumed as
213 * READWRITE access. When the count field is 0, it's assumes as one.
214 *
215 * Returns the pointer of the newly generated instance, or NULL on failure.
216 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100217struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
218 void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100220 struct snd_kcontrol kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 unsigned int access;
222
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200223 snd_assert(ncontrol != NULL, return NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 snd_assert(ncontrol->info != NULL, return NULL);
225 memset(&kctl, 0, sizeof(kctl));
226 kctl.id.iface = ncontrol->iface;
227 kctl.id.device = ncontrol->device;
228 kctl.id.subdevice = ncontrol->subdevice;
229 if (ncontrol->name)
230 strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
231 kctl.id.index = ncontrol->index;
232 kctl.count = ncontrol->count ? ncontrol->count : 1;
233 access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
234 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|SNDRV_CTL_ELEM_ACCESS_INACTIVE|
235 SNDRV_CTL_ELEM_ACCESS_DINDIRECT|SNDRV_CTL_ELEM_ACCESS_INDIRECT));
236 kctl.info = ncontrol->info;
237 kctl.get = ncontrol->get;
238 kctl.put = ncontrol->put;
239 kctl.private_value = ncontrol->private_value;
240 kctl.private_data = private_data;
241 return snd_ctl_new(&kctl, access);
242}
243
244/**
245 * snd_ctl_free_one - release the control instance
246 * @kcontrol: the control instance
247 *
248 * Releases the control instance created via snd_ctl_new()
249 * or snd_ctl_new1().
250 * Don't call this after the control was added to the card.
251 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100252void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 if (kcontrol) {
255 if (kcontrol->private_free)
256 kcontrol->private_free(kcontrol);
257 kfree(kcontrol);
258 }
259}
260
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100261static unsigned int snd_ctl_hole_check(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 unsigned int count)
263{
264 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100265 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 list_for_each(list, &card->controls) {
268 kctl = snd_kcontrol(list);
269 if ((kctl->id.numid <= card->last_numid &&
270 kctl->id.numid + kctl->count > card->last_numid) ||
271 (kctl->id.numid <= card->last_numid + count - 1 &&
272 kctl->id.numid + kctl->count > card->last_numid + count - 1))
273 return card->last_numid = kctl->id.numid + kctl->count - 1;
274 }
275 return card->last_numid;
276}
277
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100278static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 unsigned int last_numid, iter = 100000;
281
282 last_numid = card->last_numid;
283 while (last_numid != snd_ctl_hole_check(card, count)) {
284 if (--iter == 0) {
285 /* this situation is very unlikely */
286 snd_printk(KERN_ERR "unable to allocate new control numid\n");
287 return -ENOMEM;
288 }
289 last_numid = card->last_numid;
290 }
291 return 0;
292}
293
294/**
295 * snd_ctl_add - add the control instance to the card
296 * @card: the card instance
297 * @kcontrol: the control instance to add
298 *
299 * Adds the control instance created via snd_ctl_new() or
300 * snd_ctl_new1() to the given card. Assigns also an unique
301 * numid used for fast search.
302 *
303 * Returns zero if successful, or a negative error code on failure.
304 *
305 * It frees automatically the control which cannot be added.
306 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100307int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100309 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 unsigned int idx;
311
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200312 snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 snd_assert(kcontrol->info != NULL, return -EINVAL);
314 id = kcontrol->id;
315 down_write(&card->controls_rwsem);
316 if (snd_ctl_find_id(card, &id)) {
317 up_write(&card->controls_rwsem);
318 snd_ctl_free_one(kcontrol);
319 snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n",
320 id.iface,
321 id.device,
322 id.subdevice,
323 id.name,
324 id.index);
325 return -EBUSY;
326 }
327 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
328 up_write(&card->controls_rwsem);
329 snd_ctl_free_one(kcontrol);
330 return -ENOMEM;
331 }
332 list_add_tail(&kcontrol->list, &card->controls);
333 card->controls_count += kcontrol->count;
334 kcontrol->id.numid = card->last_numid + 1;
335 card->last_numid += kcontrol->count;
336 up_write(&card->controls_rwsem);
337 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
338 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
339 return 0;
340}
341
342/**
343 * snd_ctl_remove - remove the control from the card and release it
344 * @card: the card instance
345 * @kcontrol: the control instance to remove
346 *
347 * Removes the control from the card and then releases the instance.
348 * You don't need to call snd_ctl_free_one(). You must be in
349 * the write lock - down_write(&card->controls_rwsem).
350 *
351 * Returns 0 if successful, or a negative error code on failure.
352 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100353int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100355 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 unsigned int idx;
357
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200358 snd_assert(card != NULL && kcontrol != NULL, return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 list_del(&kcontrol->list);
360 card->controls_count -= kcontrol->count;
361 id = kcontrol->id;
362 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
363 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
364 snd_ctl_free_one(kcontrol);
365 return 0;
366}
367
368/**
369 * snd_ctl_remove_id - remove the control of the given id and release it
370 * @card: the card instance
371 * @id: the control id to remove
372 *
373 * Finds the control instance with the given id, removes it from the
374 * card list and releases it.
375 *
376 * Returns 0 if successful, or a negative error code on failure.
377 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100378int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100380 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int ret;
382
383 down_write(&card->controls_rwsem);
384 kctl = snd_ctl_find_id(card, id);
385 if (kctl == NULL) {
386 up_write(&card->controls_rwsem);
387 return -ENOENT;
388 }
389 ret = snd_ctl_remove(card, kctl);
390 up_write(&card->controls_rwsem);
391 return ret;
392}
393
394/**
395 * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it
396 * @file: active control handle
397 * @id: the control id to remove
398 *
399 * Finds the control instance with the given id, removes it from the
400 * card list and releases it.
401 *
402 * Returns 0 if successful, or a negative error code on failure.
403 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100404static int snd_ctl_remove_unlocked_id(struct snd_ctl_file * file,
405 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100407 struct snd_card *card = file->card;
408 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 int idx, ret;
410
411 down_write(&card->controls_rwsem);
412 kctl = snd_ctl_find_id(card, id);
413 if (kctl == NULL) {
414 up_write(&card->controls_rwsem);
415 return -ENOENT;
416 }
417 for (idx = 0; idx < kctl->count; idx++)
418 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
419 up_write(&card->controls_rwsem);
420 return -EBUSY;
421 }
422 ret = snd_ctl_remove(card, kctl);
423 up_write(&card->controls_rwsem);
424 return ret;
425}
426
427/**
428 * snd_ctl_rename_id - replace the id of a control on the card
429 * @card: the card instance
430 * @src_id: the old id
431 * @dst_id: the new id
432 *
433 * Finds the control with the old id from the card, and replaces the
434 * id with the new one.
435 *
436 * Returns zero if successful, or a negative error code on failure.
437 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100438int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
439 struct snd_ctl_elem_id *dst_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100441 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 down_write(&card->controls_rwsem);
444 kctl = snd_ctl_find_id(card, src_id);
445 if (kctl == NULL) {
446 up_write(&card->controls_rwsem);
447 return -ENOENT;
448 }
449 kctl->id = *dst_id;
450 kctl->id.numid = card->last_numid + 1;
451 card->last_numid += kctl->count;
452 up_write(&card->controls_rwsem);
453 return 0;
454}
455
456/**
457 * snd_ctl_find_numid - find the control instance with the given number-id
458 * @card: the card instance
459 * @numid: the number-id to search
460 *
461 * Finds the control instance with the given number-id from the card.
462 *
463 * Returns the pointer of the instance if found, or NULL if not.
464 *
465 * The caller must down card->controls_rwsem before calling this function
466 * (if the race condition can happen).
467 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100468struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100471 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200473 snd_assert(card != NULL && numid != 0, return NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 list_for_each(list, &card->controls) {
475 kctl = snd_kcontrol(list);
476 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
477 return kctl;
478 }
479 return NULL;
480}
481
482/**
483 * snd_ctl_find_id - find the control instance with the given id
484 * @card: the card instance
485 * @id: the id to search
486 *
487 * Finds the control instance with the given id from the card.
488 *
489 * Returns the pointer of the instance if found, or NULL if not.
490 *
491 * The caller must down card->controls_rwsem before calling this function
492 * (if the race condition can happen).
493 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100494struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
495 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100498 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Takashi Iwai7c22f1a2005-10-10 11:46:31 +0200500 snd_assert(card != NULL && id != NULL, return NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (id->numid != 0)
502 return snd_ctl_find_numid(card, id->numid);
503 list_for_each(list, &card->controls) {
504 kctl = snd_kcontrol(list);
505 if (kctl->id.iface != id->iface)
506 continue;
507 if (kctl->id.device != id->device)
508 continue;
509 if (kctl->id.subdevice != id->subdevice)
510 continue;
511 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
512 continue;
513 if (kctl->id.index > id->index)
514 continue;
515 if (kctl->id.index + kctl->count <= id->index)
516 continue;
517 return kctl;
518 }
519 return NULL;
520}
521
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100522static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 unsigned int cmd, void __user *arg)
524{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100525 struct snd_ctl_card_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Takashi Iwaica2c0962005-09-09 14:20:23 +0200527 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (! info)
529 return -ENOMEM;
530 down_read(&snd_ioctl_rwsem);
531 info->card = card->number;
532 strlcpy(info->id, card->id, sizeof(info->id));
533 strlcpy(info->driver, card->driver, sizeof(info->driver));
534 strlcpy(info->name, card->shortname, sizeof(info->name));
535 strlcpy(info->longname, card->longname, sizeof(info->longname));
536 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
537 strlcpy(info->components, card->components, sizeof(info->components));
538 up_read(&snd_ioctl_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100539 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 kfree(info);
541 return -EFAULT;
542 }
543 kfree(info);
544 return 0;
545}
546
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100547static int snd_ctl_elem_list(struct snd_card *card,
548 struct snd_ctl_elem_list __user *_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 struct list_head *plist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100551 struct snd_ctl_elem_list list;
552 struct snd_kcontrol *kctl;
553 struct snd_ctl_elem_id *dst, *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 unsigned int offset, space, first, jidx;
555
556 if (copy_from_user(&list, _list, sizeof(list)))
557 return -EFAULT;
558 offset = list.offset;
559 space = list.space;
560 first = 0;
561 /* try limit maximum space */
562 if (space > 16384)
563 return -ENOMEM;
564 if (space > 0) {
565 /* allocate temporary buffer for atomic operation */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100566 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (dst == NULL)
568 return -ENOMEM;
569 down_read(&card->controls_rwsem);
570 list.count = card->controls_count;
571 plist = card->controls.next;
572 while (plist != &card->controls) {
573 if (offset == 0)
574 break;
575 kctl = snd_kcontrol(plist);
576 if (offset < kctl->count)
577 break;
578 offset -= kctl->count;
579 plist = plist->next;
580 }
581 list.used = 0;
582 id = dst;
583 while (space > 0 && plist != &card->controls) {
584 kctl = snd_kcontrol(plist);
585 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
586 snd_ctl_build_ioff(id, kctl, jidx);
587 id++;
588 space--;
589 list.used++;
590 }
591 plist = plist->next;
592 offset = 0;
593 }
594 up_read(&card->controls_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100595 if (list.used > 0 &&
596 copy_to_user(list.pids, dst,
597 list.used * sizeof(struct snd_ctl_elem_id))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 vfree(dst);
599 return -EFAULT;
600 }
601 vfree(dst);
602 } else {
603 down_read(&card->controls_rwsem);
604 list.count = card->controls_count;
605 up_read(&card->controls_rwsem);
606 }
607 if (copy_to_user(_list, &list, sizeof(list)))
608 return -EFAULT;
609 return 0;
610}
611
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100612static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
613 struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100615 struct snd_card *card = ctl->card;
616 struct snd_kcontrol *kctl;
617 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 unsigned int index_offset;
619 int result;
620
621 down_read(&card->controls_rwsem);
622 kctl = snd_ctl_find_id(card, &info->id);
623 if (kctl == NULL) {
624 up_read(&card->controls_rwsem);
625 return -ENOENT;
626 }
627#ifdef CONFIG_SND_DEBUG
628 info->access = 0;
629#endif
630 result = kctl->info(kctl, info);
631 if (result >= 0) {
632 snd_assert(info->access == 0, );
633 index_offset = snd_ctl_get_ioff(kctl, &info->id);
634 vd = &kctl->vd[index_offset];
635 snd_ctl_build_ioff(&info->id, kctl, index_offset);
636 info->access = vd->access;
637 if (vd->owner) {
638 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
639 if (vd->owner == ctl)
640 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
641 info->owner = vd->owner_pid;
642 } else {
643 info->owner = -1;
644 }
645 }
646 up_read(&card->controls_rwsem);
647 return result;
648}
649
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100650static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
651 struct snd_ctl_elem_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100653 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 int result;
655
656 if (copy_from_user(&info, _info, sizeof(info)))
657 return -EFAULT;
658 result = snd_ctl_elem_info(ctl, &info);
659 if (result >= 0)
660 if (copy_to_user(_info, &info, sizeof(info)))
661 return -EFAULT;
662 return result;
663}
664
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100665int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100667 struct snd_kcontrol *kctl;
668 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 unsigned int index_offset;
670 int result, indirect;
671
672 down_read(&card->controls_rwsem);
673 kctl = snd_ctl_find_id(card, &control->id);
674 if (kctl == NULL) {
675 result = -ENOENT;
676 } else {
677 index_offset = snd_ctl_get_ioff(kctl, &control->id);
678 vd = &kctl->vd[index_offset];
679 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
680 if (control->indirect != indirect) {
681 result = -EACCES;
682 } else {
683 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) {
684 snd_ctl_build_ioff(&control->id, kctl, index_offset);
685 result = kctl->get(kctl, control);
686 } else {
687 result = -EPERM;
688 }
689 }
690 }
691 up_read(&card->controls_rwsem);
692 return result;
693}
694
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100695static int snd_ctl_elem_read_user(struct snd_card *card,
696 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100698 struct snd_ctl_elem_value *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 int result;
700
701 control = kmalloc(sizeof(*control), GFP_KERNEL);
702 if (control == NULL)
703 return -ENOMEM;
704 if (copy_from_user(control, _control, sizeof(*control))) {
705 kfree(control);
706 return -EFAULT;
707 }
708 result = snd_ctl_elem_read(card, control);
709 if (result >= 0)
710 if (copy_to_user(_control, control, sizeof(*control)))
711 result = -EFAULT;
712 kfree(control);
713 return result;
714}
715
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100716int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
717 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100719 struct snd_kcontrol *kctl;
720 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 unsigned int index_offset;
722 int result, indirect;
723
724 down_read(&card->controls_rwsem);
725 kctl = snd_ctl_find_id(card, &control->id);
726 if (kctl == NULL) {
727 result = -ENOENT;
728 } else {
729 index_offset = snd_ctl_get_ioff(kctl, &control->id);
730 vd = &kctl->vd[index_offset];
731 indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
732 if (control->indirect != indirect) {
733 result = -EACCES;
734 } else {
735 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
736 kctl->put == NULL ||
737 (file && vd->owner != NULL && vd->owner != file)) {
738 result = -EPERM;
739 } else {
740 snd_ctl_build_ioff(&control->id, kctl, index_offset);
741 result = kctl->put(kctl, control);
742 }
743 if (result > 0) {
744 up_read(&card->controls_rwsem);
745 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
746 return 0;
747 }
748 }
749 }
750 up_read(&card->controls_rwsem);
751 return result;
752}
753
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100754static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
755 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100757 struct snd_ctl_elem_value *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 int result;
759
760 control = kmalloc(sizeof(*control), GFP_KERNEL);
761 if (control == NULL)
762 return -ENOMEM;
763 if (copy_from_user(control, _control, sizeof(*control))) {
764 kfree(control);
765 return -EFAULT;
766 }
767 result = snd_ctl_elem_write(file->card, file, control);
768 if (result >= 0)
769 if (copy_to_user(_control, control, sizeof(*control)))
770 result = -EFAULT;
771 kfree(control);
772 return result;
773}
774
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100775static int snd_ctl_elem_lock(struct snd_ctl_file *file,
776 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100778 struct snd_card *card = file->card;
779 struct snd_ctl_elem_id id;
780 struct snd_kcontrol *kctl;
781 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 int result;
783
784 if (copy_from_user(&id, _id, sizeof(id)))
785 return -EFAULT;
786 down_write(&card->controls_rwsem);
787 kctl = snd_ctl_find_id(card, &id);
788 if (kctl == NULL) {
789 result = -ENOENT;
790 } else {
791 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
792 if (vd->owner != NULL)
793 result = -EBUSY;
794 else {
795 vd->owner = file;
796 vd->owner_pid = current->pid;
797 result = 0;
798 }
799 }
800 up_write(&card->controls_rwsem);
801 return result;
802}
803
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100804static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
805 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100807 struct snd_card *card = file->card;
808 struct snd_ctl_elem_id id;
809 struct snd_kcontrol *kctl;
810 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 int result;
812
813 if (copy_from_user(&id, _id, sizeof(id)))
814 return -EFAULT;
815 down_write(&card->controls_rwsem);
816 kctl = snd_ctl_find_id(card, &id);
817 if (kctl == NULL) {
818 result = -ENOENT;
819 } else {
820 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
821 if (vd->owner == NULL)
822 result = -EINVAL;
823 else if (vd->owner != file)
824 result = -EPERM;
825 else {
826 vd->owner = NULL;
827 vd->owner_pid = 0;
828 result = 0;
829 }
830 }
831 up_write(&card->controls_rwsem);
832 return result;
833}
834
835struct user_element {
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100836 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 void *elem_data; /* element data */
838 unsigned long elem_data_size; /* size of element data in bytes */
839 void *priv_data; /* private data (like strings for enumerated type) */
840 unsigned long priv_data_size; /* size of private data in bytes */
841};
842
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100843static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
844 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 struct user_element *ue = kcontrol->private_data;
847
848 *uinfo = ue->info;
849 return 0;
850}
851
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100852static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
853 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 struct user_element *ue = kcontrol->private_data;
856
857 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
858 return 0;
859}
860
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100861static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
862 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 int change;
865 struct user_element *ue = kcontrol->private_data;
866
867 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
868 if (change)
869 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
870 return change;
871}
872
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100873static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 kfree(kcontrol->private_data);
876}
877
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100878static int snd_ctl_elem_add(struct snd_ctl_file *file,
879 struct snd_ctl_elem_info *info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100881 struct snd_card *card = file->card;
882 struct snd_kcontrol kctl, *_kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 unsigned int access;
884 long private_size;
885 struct user_element *ue;
886 int idx, err;
887
888 if (card->user_ctl_count >= MAX_USER_CONTROLS)
889 return -ENOMEM;
890 if (info->count > 1024)
891 return -EINVAL;
892 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100893 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
894 SNDRV_CTL_ELEM_ACCESS_INACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 info->id.numid = 0;
896 memset(&kctl, 0, sizeof(kctl));
897 down_write(&card->controls_rwsem);
898 _kctl = snd_ctl_find_id(card, &info->id);
899 err = 0;
900 if (_kctl) {
901 if (replace)
902 err = snd_ctl_remove(card, _kctl);
903 else
904 err = -EBUSY;
905 } else {
906 if (replace)
907 err = -ENOENT;
908 }
909 up_write(&card->controls_rwsem);
910 if (err < 0)
911 return err;
912 memcpy(&kctl.id, &info->id, sizeof(info->id));
913 kctl.count = info->owner ? info->owner : 1;
914 access |= SNDRV_CTL_ELEM_ACCESS_USER;
915 kctl.info = snd_ctl_elem_user_info;
916 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
917 kctl.get = snd_ctl_elem_user_get;
918 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
919 kctl.put = snd_ctl_elem_user_put;
920 switch (info->type) {
921 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
922 private_size = sizeof(char);
923 if (info->count > 128)
924 return -EINVAL;
925 break;
926 case SNDRV_CTL_ELEM_TYPE_INTEGER:
927 private_size = sizeof(long);
928 if (info->count > 128)
929 return -EINVAL;
930 break;
931 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
932 private_size = sizeof(long long);
933 if (info->count > 64)
934 return -EINVAL;
935 break;
936 case SNDRV_CTL_ELEM_TYPE_BYTES:
937 private_size = sizeof(unsigned char);
938 if (info->count > 512)
939 return -EINVAL;
940 break;
941 case SNDRV_CTL_ELEM_TYPE_IEC958:
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100942 private_size = sizeof(struct snd_aes_iec958);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (info->count != 1)
944 return -EINVAL;
945 break;
946 default:
947 return -EINVAL;
948 }
949 private_size *= info->count;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200950 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (ue == NULL)
952 return -ENOMEM;
953 ue->info = *info;
954 ue->elem_data = (char *)ue + sizeof(*ue);
955 ue->elem_data_size = private_size;
956 kctl.private_free = snd_ctl_elem_user_free;
957 _kctl = snd_ctl_new(&kctl, access);
958 if (_kctl == NULL) {
959 kfree(_kctl->private_data);
960 return -ENOMEM;
961 }
962 _kctl->private_data = ue;
963 for (idx = 0; idx < _kctl->count; idx++)
964 _kctl->vd[idx].owner = file;
965 err = snd_ctl_add(card, _kctl);
966 if (err < 0) {
967 snd_ctl_free_one(_kctl);
968 return err;
969 }
970
971 down_write(&card->controls_rwsem);
972 card->user_ctl_count++;
973 up_write(&card->controls_rwsem);
974
975 return 0;
976}
977
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100978static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
979 struct snd_ctl_elem_info __user *_info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100981 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (copy_from_user(&info, _info, sizeof(info)))
983 return -EFAULT;
984 return snd_ctl_elem_add(file, &info, replace);
985}
986
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100987static int snd_ctl_elem_remove(struct snd_ctl_file *file,
988 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100990 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 int err;
992
993 if (copy_from_user(&id, _id, sizeof(id)))
994 return -EFAULT;
995 err = snd_ctl_remove_unlocked_id(file, &id);
996 if (! err) {
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100997 struct snd_card *card = file->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 down_write(&card->controls_rwsem);
999 card->user_ctl_count--;
1000 up_write(&card->controls_rwsem);
1001 }
1002 return err;
1003}
1004
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001005static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 int subscribe;
1008 if (get_user(subscribe, ptr))
1009 return -EFAULT;
1010 if (subscribe < 0) {
1011 subscribe = file->subscribed;
1012 if (put_user(subscribe, ptr))
1013 return -EFAULT;
1014 return 0;
1015 }
1016 if (subscribe) {
1017 file->subscribed = 1;
1018 return 0;
1019 } else if (file->subscribed) {
1020 snd_ctl_empty_read_queue(file);
1021 file->subscribed = 0;
1022 }
1023 return 0;
1024}
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1027{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001028 struct snd_ctl_file *ctl;
1029 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001031 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 void __user *argp = (void __user *)arg;
1033 int __user *ip = argp;
1034 int err;
1035
1036 ctl = file->private_data;
1037 card = ctl->card;
1038 snd_assert(card != NULL, return -ENXIO);
1039 switch (cmd) {
1040 case SNDRV_CTL_IOCTL_PVERSION:
1041 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1042 case SNDRV_CTL_IOCTL_CARD_INFO:
1043 return snd_ctl_card_info(card, ctl, cmd, argp);
1044 case SNDRV_CTL_IOCTL_ELEM_LIST:
1045 return snd_ctl_elem_list(ctl->card, argp);
1046 case SNDRV_CTL_IOCTL_ELEM_INFO:
1047 return snd_ctl_elem_info_user(ctl, argp);
1048 case SNDRV_CTL_IOCTL_ELEM_READ:
1049 return snd_ctl_elem_read_user(ctl->card, argp);
1050 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1051 return snd_ctl_elem_write_user(ctl, argp);
1052 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1053 return snd_ctl_elem_lock(ctl, argp);
1054 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1055 return snd_ctl_elem_unlock(ctl, argp);
1056 case SNDRV_CTL_IOCTL_ELEM_ADD:
1057 return snd_ctl_elem_add_user(ctl, argp, 0);
1058 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1059 return snd_ctl_elem_add_user(ctl, argp, 1);
1060 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1061 return snd_ctl_elem_remove(ctl, argp);
1062 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1063 return snd_ctl_subscribe_events(ctl, ip);
1064 case SNDRV_CTL_IOCTL_POWER:
Takashi Iwaia381a7a2005-11-17 15:55:49 +01001065 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 case SNDRV_CTL_IOCTL_POWER_STATE:
1067#ifdef CONFIG_PM
1068 return put_user(card->power_state, ip) ? -EFAULT : 0;
1069#else
1070 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1071#endif
1072 }
1073 down_read(&snd_ioctl_rwsem);
1074 list_for_each(list, &snd_control_ioctls) {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001075 p = list_entry(list, struct snd_kctl_ioctl, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 err = p->fioctl(card, ctl, cmd, arg);
1077 if (err != -ENOIOCTLCMD) {
1078 up_read(&snd_ioctl_rwsem);
1079 return err;
1080 }
1081 }
1082 up_read(&snd_ioctl_rwsem);
Takashi Iwai6d85be62005-05-15 14:32:50 +02001083 snd_printdd("unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return -ENOTTY;
1085}
1086
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001087static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1088 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001090 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 int err = 0;
1092 ssize_t result = 0;
1093
1094 ctl = file->private_data;
1095 snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO);
1096 if (!ctl->subscribed)
1097 return -EBADFD;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001098 if (count < sizeof(struct snd_ctl_event))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return -EINVAL;
1100 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001101 while (count >= sizeof(struct snd_ctl_event)) {
1102 struct snd_ctl_event ev;
1103 struct snd_kctl_event *kev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 while (list_empty(&ctl->events)) {
1105 wait_queue_t wait;
1106 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1107 err = -EAGAIN;
1108 goto __end_lock;
1109 }
1110 init_waitqueue_entry(&wait, current);
1111 add_wait_queue(&ctl->change_sleep, &wait);
1112 set_current_state(TASK_INTERRUPTIBLE);
1113 spin_unlock_irq(&ctl->read_lock);
1114 schedule();
1115 remove_wait_queue(&ctl->change_sleep, &wait);
1116 if (signal_pending(current))
1117 return result > 0 ? result : -ERESTARTSYS;
1118 spin_lock_irq(&ctl->read_lock);
1119 }
1120 kev = snd_kctl_event(ctl->events.next);
1121 ev.type = SNDRV_CTL_EVENT_ELEM;
1122 ev.data.elem.mask = kev->mask;
1123 ev.data.elem.id = kev->id;
1124 list_del(&kev->list);
1125 spin_unlock_irq(&ctl->read_lock);
1126 kfree(kev);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001127 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 err = -EFAULT;
1129 goto __end;
1130 }
1131 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001132 buffer += sizeof(struct snd_ctl_event);
1133 count -= sizeof(struct snd_ctl_event);
1134 result += sizeof(struct snd_ctl_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136 __end_lock:
1137 spin_unlock_irq(&ctl->read_lock);
1138 __end:
1139 return result > 0 ? result : err;
1140}
1141
1142static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1143{
1144 unsigned int mask;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001145 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 ctl = file->private_data;
1148 if (!ctl->subscribed)
1149 return 0;
1150 poll_wait(file, &ctl->change_sleep, wait);
1151
1152 mask = 0;
1153 if (!list_empty(&ctl->events))
1154 mask |= POLLIN | POLLRDNORM;
1155
1156 return mask;
1157}
1158
1159/*
1160 * register the device-specific control-ioctls.
1161 * called from each device manager like pcm.c, hwdep.c, etc.
1162 */
1163static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1164{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001165 struct snd_kctl_ioctl *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001167 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (pn == NULL)
1169 return -ENOMEM;
1170 pn->fioctl = fcn;
1171 down_write(&snd_ioctl_rwsem);
1172 list_add_tail(&pn->list, lists);
1173 up_write(&snd_ioctl_rwsem);
1174 return 0;
1175}
1176
1177int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1178{
1179 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1180}
1181
1182#ifdef CONFIG_COMPAT
1183int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1184{
1185 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1186}
1187#endif
1188
1189/*
1190 * de-register the device-specific control-ioctls.
1191 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001192static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1193 struct list_head *lists)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 struct list_head *list;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001196 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02001198 snd_assert(fcn != NULL, return -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 down_write(&snd_ioctl_rwsem);
1200 list_for_each(list, lists) {
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001201 p = list_entry(list, struct snd_kctl_ioctl, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (p->fioctl == fcn) {
1203 list_del(&p->list);
1204 up_write(&snd_ioctl_rwsem);
1205 kfree(p);
1206 return 0;
1207 }
1208 }
1209 up_write(&snd_ioctl_rwsem);
1210 snd_BUG();
1211 return -EINVAL;
1212}
1213
1214int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1215{
1216 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1217}
1218
1219#ifdef CONFIG_COMPAT
1220int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1221{
1222 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1223}
1224
1225#endif
1226
1227static int snd_ctl_fasync(int fd, struct file * file, int on)
1228{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001229 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int err;
1231 ctl = file->private_data;
1232 err = fasync_helper(fd, file, on, &ctl->fasync);
1233 if (err < 0)
1234 return err;
1235 return 0;
1236}
1237
1238/*
1239 * ioctl32 compat
1240 */
1241#ifdef CONFIG_COMPAT
1242#include "control_compat.c"
1243#else
1244#define snd_ctl_ioctl_compat NULL
1245#endif
1246
1247/*
1248 * INIT PART
1249 */
1250
1251static struct file_operations snd_ctl_f_ops =
1252{
1253 .owner = THIS_MODULE,
1254 .read = snd_ctl_read,
1255 .open = snd_ctl_open,
1256 .release = snd_ctl_release,
1257 .poll = snd_ctl_poll,
1258 .unlocked_ioctl = snd_ctl_ioctl,
1259 .compat_ioctl = snd_ctl_ioctl_compat,
1260 .fasync = snd_ctl_fasync,
1261};
1262
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001263static struct snd_minor snd_ctl_reg =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
1265 .comment = "ctl",
1266 .f_ops = &snd_ctl_f_ops,
1267};
1268
1269/*
1270 * registration of the control device
1271 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001272static int snd_ctl_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001274 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int err, cardnum;
1276 char name[16];
1277
1278 snd_assert(card != NULL, return -ENXIO);
1279 cardnum = card->number;
1280 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1281 sprintf(name, "controlC%i", cardnum);
1282 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL,
1283 card, 0, &snd_ctl_reg, name)) < 0)
1284 return err;
1285 return 0;
1286}
1287
1288/*
1289 * disconnection of the control device
1290 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001291static int snd_ctl_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001293 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 struct list_head *flist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001295 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 down_read(&card->controls_rwsem);
1298 list_for_each(flist, &card->ctl_files) {
1299 ctl = snd_ctl_file(flist);
1300 wake_up(&ctl->change_sleep);
1301 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1302 }
1303 up_read(&card->controls_rwsem);
1304 return 0;
1305}
1306
1307/*
1308 * free all controls
1309 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001310static int snd_ctl_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001312 struct snd_card *card = device->device_data;
1313 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 down_write(&card->controls_rwsem);
1316 while (!list_empty(&card->controls)) {
1317 control = snd_kcontrol(card->controls.next);
1318 snd_ctl_remove(card, control);
1319 }
1320 up_write(&card->controls_rwsem);
1321 return 0;
1322}
1323
1324/*
1325 * de-registration of the control device
1326 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001327static int snd_ctl_dev_unregister(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001329 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 int err, cardnum;
1331
1332 snd_assert(card != NULL, return -ENXIO);
1333 cardnum = card->number;
1334 snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1335 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL, card, 0)) < 0)
1336 return err;
1337 return snd_ctl_dev_free(device);
1338}
1339
1340/*
1341 * create control core:
1342 * called from init.c
1343 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001344int snd_ctl_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001346 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 .dev_free = snd_ctl_dev_free,
1348 .dev_register = snd_ctl_dev_register,
1349 .dev_disconnect = snd_ctl_dev_disconnect,
1350 .dev_unregister = snd_ctl_dev_unregister
1351 };
1352
1353 snd_assert(card != NULL, return -ENXIO);
1354 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1355}