Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Routines for driver control interface |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/threads.h> |
| 23 | #include <linux/interrupt.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 24 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/slab.h> |
| 26 | #include <linux/vmalloc.h> |
| 27 | #include <linux/time.h> |
| 28 | #include <sound/core.h> |
| 29 | #include <sound/minors.h> |
| 30 | #include <sound/info.h> |
| 31 | #include <sound/control.h> |
| 32 | |
| 33 | /* max number of user-defined controls */ |
| 34 | #define MAX_USER_CONTROLS 32 |
Dan Rosenberg | 5591bf0 | 2010-09-28 14:18:20 -0400 | [diff] [blame] | 35 | #define MAX_CONTROL_COUNT 1028 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 37 | struct snd_kctl_ioctl { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | struct list_head list; /* list of all ioctls */ |
| 39 | snd_kctl_ioctl_func_t fioctl; |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 40 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | unsigned long flags; |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 51 | struct snd_card *card; |
| 52 | struct snd_ctl_file *ctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | int err; |
| 54 | |
Takashi Iwai | 02f4865 | 2010-04-13 11:49:04 +0200 | [diff] [blame] | 55 | err = nonseekable_open(inode, file); |
| 56 | if (err < 0) |
| 57 | return err; |
| 58 | |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 59 | card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | if (!card) { |
| 61 | err = -ENODEV; |
| 62 | goto __error1; |
| 63 | } |
| 64 | err = snd_card_file_add(card, file); |
| 65 | if (err < 0) { |
| 66 | err = -ENODEV; |
| 67 | goto __error1; |
| 68 | } |
| 69 | if (!try_module_get(card->module)) { |
| 70 | err = -EFAULT; |
| 71 | goto __error2; |
| 72 | } |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 73 | ctl = kzalloc(sizeof(*ctl), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | if (ctl == NULL) { |
| 75 | err = -ENOMEM; |
| 76 | goto __error; |
| 77 | } |
| 78 | INIT_LIST_HEAD(&ctl->events); |
| 79 | init_waitqueue_head(&ctl->change_sleep); |
| 80 | spin_lock_init(&ctl->read_lock); |
| 81 | ctl->card = card; |
Takashi Iwai | 2529bba | 2006-08-04 12:57:19 +0200 | [diff] [blame] | 82 | ctl->prefer_pcm_subdevice = -1; |
| 83 | ctl->prefer_rawmidi_subdevice = -1; |
Clemens Ladisch | 25d27ed | 2009-11-02 09:35:44 +0100 | [diff] [blame] | 84 | ctl->pid = get_pid(task_pid(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | file->private_data = ctl; |
| 86 | write_lock_irqsave(&card->ctl_files_rwlock, flags); |
| 87 | list_add_tail(&ctl->list, &card->ctl_files); |
| 88 | write_unlock_irqrestore(&card->ctl_files_rwlock, flags); |
| 89 | return 0; |
| 90 | |
| 91 | __error: |
| 92 | module_put(card->module); |
| 93 | __error2: |
| 94 | snd_card_file_remove(card, file); |
| 95 | __error1: |
| 96 | return err; |
| 97 | } |
| 98 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 99 | static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
Borislav Petkov | 7507e8d | 2007-10-22 17:11:09 +0200 | [diff] [blame] | 101 | unsigned long flags; |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 102 | struct snd_kctl_event *cread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Borislav Petkov | 7507e8d | 2007-10-22 17:11:09 +0200 | [diff] [blame] | 104 | spin_lock_irqsave(&ctl->read_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | while (!list_empty(&ctl->events)) { |
| 106 | cread = snd_kctl_event(ctl->events.next); |
| 107 | list_del(&cread->list); |
| 108 | kfree(cread); |
| 109 | } |
Borislav Petkov | 7507e8d | 2007-10-22 17:11:09 +0200 | [diff] [blame] | 110 | spin_unlock_irqrestore(&ctl->read_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static int snd_ctl_release(struct inode *inode, struct file *file) |
| 114 | { |
| 115 | unsigned long flags; |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 116 | struct snd_card *card; |
| 117 | struct snd_ctl_file *ctl; |
| 118 | struct snd_kcontrol *control; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | unsigned int idx; |
| 120 | |
| 121 | ctl = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | file->private_data = NULL; |
| 123 | card = ctl->card; |
| 124 | write_lock_irqsave(&card->ctl_files_rwlock, flags); |
| 125 | list_del(&ctl->list); |
| 126 | write_unlock_irqrestore(&card->ctl_files_rwlock, flags); |
| 127 | down_write(&card->controls_rwsem); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 128 | list_for_each_entry(control, &card->controls, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | for (idx = 0; idx < control->count; idx++) |
| 130 | if (control->vd[idx].owner == ctl) |
| 131 | control->vd[idx].owner = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | up_write(&card->controls_rwsem); |
| 133 | snd_ctl_empty_read_queue(ctl); |
Clemens Ladisch | 25d27ed | 2009-11-02 09:35:44 +0100 | [diff] [blame] | 134 | put_pid(ctl->pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | kfree(ctl); |
| 136 | module_put(card->module); |
| 137 | snd_card_file_remove(card, file); |
| 138 | return 0; |
| 139 | } |
| 140 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 141 | void snd_ctl_notify(struct snd_card *card, unsigned int mask, |
| 142 | struct snd_ctl_elem_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
| 144 | unsigned long flags; |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 145 | struct snd_ctl_file *ctl; |
| 146 | struct snd_kctl_event *ev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 148 | if (snd_BUG_ON(!card || !id)) |
| 149 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | read_lock(&card->ctl_files_rwlock); |
| 151 | #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE) |
| 152 | card->mixer_oss_change_count++; |
| 153 | #endif |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 154 | list_for_each_entry(ctl, &card->ctl_files, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | if (!ctl->subscribed) |
| 156 | continue; |
| 157 | spin_lock_irqsave(&ctl->read_lock, flags); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 158 | list_for_each_entry(ev, &ctl->events, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | if (ev->id.numid == id->numid) { |
| 160 | ev->mask |= mask; |
| 161 | goto _found; |
| 162 | } |
| 163 | } |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 164 | ev = kzalloc(sizeof(*ev), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | 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 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 180 | EXPORT_SYMBOL(snd_ctl_notify); |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | /** |
| 183 | * snd_ctl_new - create a control instance from the template |
| 184 | * @control: the control template |
| 185 | * @access: the default control access |
| 186 | * |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 187 | * Allocates a new struct snd_kcontrol instance and copies the given template |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | * to the new instance. It does not copy volatile data (access). |
| 189 | * |
| 190 | * Returns the pointer of the new instance, or NULL on failure. |
| 191 | */ |
Adrian Bunk | 0b51ba0 | 2006-11-20 17:50:17 +0100 | [diff] [blame] | 192 | static struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control, |
| 193 | unsigned int access) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 195 | struct snd_kcontrol *kctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | unsigned int idx; |
| 197 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 198 | if (snd_BUG_ON(!control || !control->count)) |
| 199 | return NULL; |
Dan Rosenberg | 5591bf0 | 2010-09-28 14:18:20 -0400 | [diff] [blame] | 200 | |
| 201 | if (control->count > MAX_CONTROL_COUNT) |
| 202 | return NULL; |
| 203 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 204 | kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL); |
Takashi Iwai | 73e77ba | 2005-11-17 17:44:01 +0100 | [diff] [blame] | 205 | if (kctl == NULL) { |
| 206 | snd_printk(KERN_ERR "Cannot allocate control instance\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | return NULL; |
Takashi Iwai | 73e77ba | 2005-11-17 17:44:01 +0100 | [diff] [blame] | 208 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | *kctl = *control; |
| 210 | for (idx = 0; idx < kctl->count; idx++) |
| 211 | kctl->vd[idx].access = access; |
| 212 | return kctl; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * snd_ctl_new1 - create a control instance from the template |
| 217 | * @ncontrol: the initialization record |
| 218 | * @private_data: the private data to set |
| 219 | * |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 220 | * Allocates a new struct snd_kcontrol instance and initialize from the given |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | * template. When the access field of ncontrol is 0, it's assumed as |
| 222 | * READWRITE access. When the count field is 0, it's assumes as one. |
| 223 | * |
| 224 | * Returns the pointer of the newly generated instance, or NULL on failure. |
| 225 | */ |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 226 | struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol, |
| 227 | void *private_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 229 | struct snd_kcontrol kctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | unsigned int access; |
| 231 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 232 | if (snd_BUG_ON(!ncontrol || !ncontrol->info)) |
| 233 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | memset(&kctl, 0, sizeof(kctl)); |
| 235 | kctl.id.iface = ncontrol->iface; |
| 236 | kctl.id.device = ncontrol->device; |
| 237 | kctl.id.subdevice = ncontrol->subdevice; |
Mark Brown | 366840d | 2008-10-29 14:40:30 +0000 | [diff] [blame] | 238 | if (ncontrol->name) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name)); |
Mark Brown | 366840d | 2008-10-29 14:40:30 +0000 | [diff] [blame] | 240 | if (strcmp(ncontrol->name, kctl.id.name) != 0) |
| 241 | snd_printk(KERN_WARNING |
| 242 | "Control name '%s' truncated to '%s'\n", |
| 243 | ncontrol->name, kctl.id.name); |
| 244 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | kctl.id.index = ncontrol->index; |
| 246 | kctl.count = ncontrol->count ? ncontrol->count : 1; |
| 247 | access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE : |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 248 | (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE| |
Takashi Iwai | a8d372f | 2012-07-30 13:47:07 +0200 | [diff] [blame] | 249 | SNDRV_CTL_ELEM_ACCESS_VOLATILE| |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 250 | SNDRV_CTL_ELEM_ACCESS_INACTIVE| |
Clemens Ladisch | a75d7a4 | 2010-02-01 13:29:50 +0100 | [diff] [blame] | 251 | SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE| |
| 252 | SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND| |
| 253 | SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | kctl.info = ncontrol->info; |
| 255 | kctl.get = ncontrol->get; |
| 256 | kctl.put = ncontrol->put; |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 257 | kctl.tlv.p = ncontrol->tlv.p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | kctl.private_value = ncontrol->private_value; |
| 259 | kctl.private_data = private_data; |
| 260 | return snd_ctl_new(&kctl, access); |
| 261 | } |
| 262 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 263 | EXPORT_SYMBOL(snd_ctl_new1); |
| 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | /** |
| 266 | * snd_ctl_free_one - release the control instance |
| 267 | * @kcontrol: the control instance |
| 268 | * |
| 269 | * Releases the control instance created via snd_ctl_new() |
| 270 | * or snd_ctl_new1(). |
| 271 | * Don't call this after the control was added to the card. |
| 272 | */ |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 273 | void snd_ctl_free_one(struct snd_kcontrol *kcontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
| 275 | if (kcontrol) { |
| 276 | if (kcontrol->private_free) |
| 277 | kcontrol->private_free(kcontrol); |
| 278 | kfree(kcontrol); |
| 279 | } |
| 280 | } |
| 281 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 282 | EXPORT_SYMBOL(snd_ctl_free_one); |
| 283 | |
Clemens Ladisch | 0e82e5f | 2011-03-07 13:24:30 +0100 | [diff] [blame] | 284 | static bool snd_ctl_remove_numid_conflict(struct snd_card *card, |
| 285 | unsigned int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 287 | struct snd_kcontrol *kctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 289 | list_for_each_entry(kctl, &card->controls, list) { |
Clemens Ladisch | 7c73358 | 2011-03-07 13:22:50 +0100 | [diff] [blame] | 290 | if (kctl->id.numid < card->last_numid + 1 + count && |
Clemens Ladisch | 0e82e5f | 2011-03-07 13:24:30 +0100 | [diff] [blame] | 291 | kctl->id.numid + kctl->count > card->last_numid + 1) { |
| 292 | card->last_numid = kctl->id.numid + kctl->count - 1; |
| 293 | return true; |
| 294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
Clemens Ladisch | 0e82e5f | 2011-03-07 13:24:30 +0100 | [diff] [blame] | 296 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 299 | static int snd_ctl_find_hole(struct snd_card *card, unsigned int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
Clemens Ladisch | 0e82e5f | 2011-03-07 13:24:30 +0100 | [diff] [blame] | 301 | unsigned int iter = 100000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Clemens Ladisch | 0e82e5f | 2011-03-07 13:24:30 +0100 | [diff] [blame] | 303 | while (snd_ctl_remove_numid_conflict(card, count)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | if (--iter == 0) { |
| 305 | /* this situation is very unlikely */ |
| 306 | snd_printk(KERN_ERR "unable to allocate new control numid\n"); |
| 307 | return -ENOMEM; |
| 308 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * snd_ctl_add - add the control instance to the card |
| 315 | * @card: the card instance |
| 316 | * @kcontrol: the control instance to add |
| 317 | * |
| 318 | * Adds the control instance created via snd_ctl_new() or |
| 319 | * snd_ctl_new1() to the given card. Assigns also an unique |
| 320 | * numid used for fast search. |
| 321 | * |
| 322 | * Returns zero if successful, or a negative error code on failure. |
| 323 | * |
| 324 | * It frees automatically the control which cannot be added. |
| 325 | */ |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 326 | int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 328 | struct snd_ctl_elem_id id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | unsigned int idx; |
Takashi Iwai | c6077b3 | 2006-03-21 16:07:13 +0100 | [diff] [blame] | 330 | int err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
Takashi Iwai | 73e77ba | 2005-11-17 17:44:01 +0100 | [diff] [blame] | 332 | if (! kcontrol) |
Takashi Iwai | c6077b3 | 2006-03-21 16:07:13 +0100 | [diff] [blame] | 333 | return err; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 334 | if (snd_BUG_ON(!card || !kcontrol->info)) |
| 335 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | id = kcontrol->id; |
| 337 | down_write(&card->controls_rwsem); |
| 338 | if (snd_ctl_find_id(card, &id)) { |
| 339 | up_write(&card->controls_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n", |
| 341 | id.iface, |
| 342 | id.device, |
| 343 | id.subdevice, |
| 344 | id.name, |
| 345 | id.index); |
Takashi Iwai | c6077b3 | 2006-03-21 16:07:13 +0100 | [diff] [blame] | 346 | err = -EBUSY; |
| 347 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
| 349 | if (snd_ctl_find_hole(card, kcontrol->count) < 0) { |
| 350 | up_write(&card->controls_rwsem); |
Takashi Iwai | c6077b3 | 2006-03-21 16:07:13 +0100 | [diff] [blame] | 351 | err = -ENOMEM; |
| 352 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
| 354 | list_add_tail(&kcontrol->list, &card->controls); |
| 355 | card->controls_count += kcontrol->count; |
| 356 | kcontrol->id.numid = card->last_numid + 1; |
| 357 | card->last_numid += kcontrol->count; |
| 358 | up_write(&card->controls_rwsem); |
| 359 | for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++) |
| 360 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id); |
| 361 | return 0; |
Takashi Iwai | c6077b3 | 2006-03-21 16:07:13 +0100 | [diff] [blame] | 362 | |
| 363 | error: |
| 364 | snd_ctl_free_one(kcontrol); |
| 365 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 368 | EXPORT_SYMBOL(snd_ctl_add); |
| 369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | /** |
Dimitris Papastamos | 66b5b97 | 2011-03-16 12:16:39 +0000 | [diff] [blame] | 371 | * snd_ctl_replace - replace the control instance of the card |
| 372 | * @card: the card instance |
| 373 | * @kcontrol: the control instance to replace |
| 374 | * @add_on_replace: add the control if not already added |
| 375 | * |
| 376 | * Replaces the given control. If the given control does not exist |
| 377 | * and the add_on_replace flag is set, the control is added. If the |
| 378 | * control exists, it is destroyed first. |
| 379 | * |
| 380 | * Returns zero if successful, or a negative error code on failure. |
| 381 | * |
| 382 | * It frees automatically the control which cannot be added or replaced. |
| 383 | */ |
| 384 | int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol, |
| 385 | bool add_on_replace) |
| 386 | { |
| 387 | struct snd_ctl_elem_id id; |
| 388 | unsigned int idx; |
| 389 | struct snd_kcontrol *old; |
| 390 | int ret; |
| 391 | |
| 392 | if (!kcontrol) |
| 393 | return -EINVAL; |
| 394 | if (snd_BUG_ON(!card || !kcontrol->info)) { |
| 395 | ret = -EINVAL; |
| 396 | goto error; |
| 397 | } |
| 398 | id = kcontrol->id; |
| 399 | down_write(&card->controls_rwsem); |
| 400 | old = snd_ctl_find_id(card, &id); |
| 401 | if (!old) { |
| 402 | if (add_on_replace) |
| 403 | goto add; |
| 404 | up_write(&card->controls_rwsem); |
| 405 | ret = -EINVAL; |
| 406 | goto error; |
| 407 | } |
| 408 | ret = snd_ctl_remove(card, old); |
| 409 | if (ret < 0) { |
| 410 | up_write(&card->controls_rwsem); |
| 411 | goto error; |
| 412 | } |
| 413 | add: |
| 414 | if (snd_ctl_find_hole(card, kcontrol->count) < 0) { |
| 415 | up_write(&card->controls_rwsem); |
| 416 | ret = -ENOMEM; |
| 417 | goto error; |
| 418 | } |
| 419 | list_add_tail(&kcontrol->list, &card->controls); |
| 420 | card->controls_count += kcontrol->count; |
| 421 | kcontrol->id.numid = card->last_numid + 1; |
| 422 | card->last_numid += kcontrol->count; |
| 423 | up_write(&card->controls_rwsem); |
| 424 | for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++) |
| 425 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id); |
| 426 | return 0; |
| 427 | |
| 428 | error: |
| 429 | snd_ctl_free_one(kcontrol); |
| 430 | return ret; |
| 431 | } |
| 432 | EXPORT_SYMBOL(snd_ctl_replace); |
| 433 | |
| 434 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | * snd_ctl_remove - remove the control from the card and release it |
| 436 | * @card: the card instance |
| 437 | * @kcontrol: the control instance to remove |
| 438 | * |
| 439 | * Removes the control from the card and then releases the instance. |
| 440 | * You don't need to call snd_ctl_free_one(). You must be in |
| 441 | * the write lock - down_write(&card->controls_rwsem). |
| 442 | * |
| 443 | * Returns 0 if successful, or a negative error code on failure. |
| 444 | */ |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 445 | int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 447 | struct snd_ctl_elem_id id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | unsigned int idx; |
| 449 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 450 | if (snd_BUG_ON(!card || !kcontrol)) |
| 451 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | list_del(&kcontrol->list); |
| 453 | card->controls_count -= kcontrol->count; |
| 454 | id = kcontrol->id; |
| 455 | for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++) |
| 456 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id); |
| 457 | snd_ctl_free_one(kcontrol); |
| 458 | return 0; |
| 459 | } |
| 460 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 461 | EXPORT_SYMBOL(snd_ctl_remove); |
| 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | /** |
| 464 | * snd_ctl_remove_id - remove the control of the given id and release it |
| 465 | * @card: the card instance |
| 466 | * @id: the control id to remove |
| 467 | * |
| 468 | * Finds the control instance with the given id, removes it from the |
| 469 | * card list and releases it. |
| 470 | * |
| 471 | * Returns 0 if successful, or a negative error code on failure. |
| 472 | */ |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 473 | int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 475 | struct snd_kcontrol *kctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | int ret; |
| 477 | |
| 478 | down_write(&card->controls_rwsem); |
| 479 | kctl = snd_ctl_find_id(card, id); |
| 480 | if (kctl == NULL) { |
| 481 | up_write(&card->controls_rwsem); |
| 482 | return -ENOENT; |
| 483 | } |
| 484 | ret = snd_ctl_remove(card, kctl); |
| 485 | up_write(&card->controls_rwsem); |
| 486 | return ret; |
| 487 | } |
| 488 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 489 | EXPORT_SYMBOL(snd_ctl_remove_id); |
| 490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | /** |
Clemens Ladisch | f217ac5 | 2009-08-17 12:27:22 +0200 | [diff] [blame] | 492 | * snd_ctl_remove_user_ctl - remove and release the unlocked user control |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | * @file: active control handle |
| 494 | * @id: the control id to remove |
| 495 | * |
| 496 | * Finds the control instance with the given id, removes it from the |
| 497 | * card list and releases it. |
| 498 | * |
| 499 | * Returns 0 if successful, or a negative error code on failure. |
| 500 | */ |
Clemens Ladisch | f217ac5 | 2009-08-17 12:27:22 +0200 | [diff] [blame] | 501 | static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file, |
| 502 | struct snd_ctl_elem_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 504 | struct snd_card *card = file->card; |
| 505 | struct snd_kcontrol *kctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | int idx, ret; |
| 507 | |
| 508 | down_write(&card->controls_rwsem); |
| 509 | kctl = snd_ctl_find_id(card, id); |
| 510 | if (kctl == NULL) { |
Clemens Ladisch | 317b808 | 2009-08-17 12:26:34 +0200 | [diff] [blame] | 511 | ret = -ENOENT; |
| 512 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | } |
Clemens Ladisch | 18dd0aa | 2009-08-17 12:28:09 +0200 | [diff] [blame] | 514 | if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) { |
| 515 | ret = -EINVAL; |
| 516 | goto error; |
| 517 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | for (idx = 0; idx < kctl->count; idx++) |
| 519 | if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) { |
Clemens Ladisch | 317b808 | 2009-08-17 12:26:34 +0200 | [diff] [blame] | 520 | ret = -EBUSY; |
| 521 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | } |
| 523 | ret = snd_ctl_remove(card, kctl); |
Clemens Ladisch | f217ac5 | 2009-08-17 12:27:22 +0200 | [diff] [blame] | 524 | if (ret < 0) |
| 525 | goto error; |
| 526 | card->user_ctl_count--; |
Clemens Ladisch | 317b808 | 2009-08-17 12:26:34 +0200 | [diff] [blame] | 527 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | up_write(&card->controls_rwsem); |
| 529 | return ret; |
| 530 | } |
| 531 | |
| 532 | /** |
Takashi Iwai | 3cbdd75 | 2008-08-29 16:09:01 +0200 | [diff] [blame] | 533 | * snd_ctl_activate_id - activate/inactivate the control of the given id |
| 534 | * @card: the card instance |
| 535 | * @id: the control id to activate/inactivate |
| 536 | * @active: non-zero to activate |
| 537 | * |
| 538 | * Finds the control instance with the given id, and activate or |
| 539 | * inactivate the control together with notification, if changed. |
| 540 | * |
| 541 | * Returns 0 if unchanged, 1 if changed, or a negative error code on failure. |
| 542 | */ |
| 543 | int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id, |
| 544 | int active) |
| 545 | { |
| 546 | struct snd_kcontrol *kctl; |
| 547 | struct snd_kcontrol_volatile *vd; |
| 548 | unsigned int index_offset; |
| 549 | int ret; |
| 550 | |
| 551 | down_write(&card->controls_rwsem); |
| 552 | kctl = snd_ctl_find_id(card, id); |
| 553 | if (kctl == NULL) { |
| 554 | ret = -ENOENT; |
| 555 | goto unlock; |
| 556 | } |
| 557 | index_offset = snd_ctl_get_ioff(kctl, &kctl->id); |
| 558 | vd = &kctl->vd[index_offset]; |
| 559 | ret = 0; |
| 560 | if (active) { |
| 561 | if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)) |
| 562 | goto unlock; |
| 563 | vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE; |
| 564 | } else { |
| 565 | if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE) |
| 566 | goto unlock; |
| 567 | vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE; |
| 568 | } |
| 569 | ret = 1; |
| 570 | unlock: |
| 571 | up_write(&card->controls_rwsem); |
| 572 | if (ret > 0) |
| 573 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, id); |
| 574 | return ret; |
| 575 | } |
| 576 | EXPORT_SYMBOL_GPL(snd_ctl_activate_id); |
| 577 | |
| 578 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | * snd_ctl_rename_id - replace the id of a control on the card |
| 580 | * @card: the card instance |
| 581 | * @src_id: the old id |
| 582 | * @dst_id: the new id |
| 583 | * |
| 584 | * Finds the control with the old id from the card, and replaces the |
| 585 | * id with the new one. |
| 586 | * |
| 587 | * Returns zero if successful, or a negative error code on failure. |
| 588 | */ |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 589 | int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id, |
| 590 | struct snd_ctl_elem_id *dst_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 592 | struct snd_kcontrol *kctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
| 594 | down_write(&card->controls_rwsem); |
| 595 | kctl = snd_ctl_find_id(card, src_id); |
| 596 | if (kctl == NULL) { |
| 597 | up_write(&card->controls_rwsem); |
| 598 | return -ENOENT; |
| 599 | } |
| 600 | kctl->id = *dst_id; |
| 601 | kctl->id.numid = card->last_numid + 1; |
| 602 | card->last_numid += kctl->count; |
| 603 | up_write(&card->controls_rwsem); |
| 604 | return 0; |
| 605 | } |
| 606 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 607 | EXPORT_SYMBOL(snd_ctl_rename_id); |
| 608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | /** |
| 610 | * snd_ctl_find_numid - find the control instance with the given number-id |
| 611 | * @card: the card instance |
| 612 | * @numid: the number-id to search |
| 613 | * |
| 614 | * Finds the control instance with the given number-id from the card. |
| 615 | * |
| 616 | * Returns the pointer of the instance if found, or NULL if not. |
| 617 | * |
| 618 | * The caller must down card->controls_rwsem before calling this function |
| 619 | * (if the race condition can happen). |
| 620 | */ |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 621 | struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 623 | struct snd_kcontrol *kctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 625 | if (snd_BUG_ON(!card || !numid)) |
| 626 | return NULL; |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 627 | list_for_each_entry(kctl, &card->controls, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid) |
| 629 | return kctl; |
| 630 | } |
| 631 | return NULL; |
| 632 | } |
| 633 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 634 | EXPORT_SYMBOL(snd_ctl_find_numid); |
| 635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | /** |
| 637 | * snd_ctl_find_id - find the control instance with the given id |
| 638 | * @card: the card instance |
| 639 | * @id: the id to search |
| 640 | * |
| 641 | * Finds the control instance with the given id from the card. |
| 642 | * |
| 643 | * Returns the pointer of the instance if found, or NULL if not. |
| 644 | * |
| 645 | * The caller must down card->controls_rwsem before calling this function |
| 646 | * (if the race condition can happen). |
| 647 | */ |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 648 | struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card, |
| 649 | struct snd_ctl_elem_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 651 | struct snd_kcontrol *kctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 653 | if (snd_BUG_ON(!card || !id)) |
| 654 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | if (id->numid != 0) |
| 656 | return snd_ctl_find_numid(card, id->numid); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 657 | list_for_each_entry(kctl, &card->controls, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | if (kctl->id.iface != id->iface) |
| 659 | continue; |
| 660 | if (kctl->id.device != id->device) |
| 661 | continue; |
| 662 | if (kctl->id.subdevice != id->subdevice) |
| 663 | continue; |
| 664 | if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name))) |
| 665 | continue; |
| 666 | if (kctl->id.index > id->index) |
| 667 | continue; |
| 668 | if (kctl->id.index + kctl->count <= id->index) |
| 669 | continue; |
| 670 | return kctl; |
| 671 | } |
| 672 | return NULL; |
| 673 | } |
| 674 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 675 | EXPORT_SYMBOL(snd_ctl_find_id); |
| 676 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 677 | static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | unsigned int cmd, void __user *arg) |
| 679 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 680 | struct snd_ctl_card_info *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 682 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | if (! info) |
| 684 | return -ENOMEM; |
| 685 | down_read(&snd_ioctl_rwsem); |
| 686 | info->card = card->number; |
| 687 | strlcpy(info->id, card->id, sizeof(info->id)); |
| 688 | strlcpy(info->driver, card->driver, sizeof(info->driver)); |
| 689 | strlcpy(info->name, card->shortname, sizeof(info->name)); |
| 690 | strlcpy(info->longname, card->longname, sizeof(info->longname)); |
| 691 | strlcpy(info->mixername, card->mixername, sizeof(info->mixername)); |
| 692 | strlcpy(info->components, card->components, sizeof(info->components)); |
| 693 | up_read(&snd_ioctl_rwsem); |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 694 | if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | kfree(info); |
| 696 | return -EFAULT; |
| 697 | } |
| 698 | kfree(info); |
| 699 | return 0; |
| 700 | } |
| 701 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 702 | static int snd_ctl_elem_list(struct snd_card *card, |
| 703 | struct snd_ctl_elem_list __user *_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | { |
| 705 | struct list_head *plist; |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 706 | struct snd_ctl_elem_list list; |
| 707 | struct snd_kcontrol *kctl; |
| 708 | struct snd_ctl_elem_id *dst, *id; |
Luca Tettamanti | 78fa2c4 | 2011-05-25 22:43:27 +0200 | [diff] [blame] | 709 | unsigned int offset, space, jidx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | |
| 711 | if (copy_from_user(&list, _list, sizeof(list))) |
| 712 | return -EFAULT; |
| 713 | offset = list.offset; |
| 714 | space = list.space; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | /* try limit maximum space */ |
| 716 | if (space > 16384) |
| 717 | return -ENOMEM; |
| 718 | if (space > 0) { |
| 719 | /* allocate temporary buffer for atomic operation */ |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 720 | dst = vmalloc(space * sizeof(struct snd_ctl_elem_id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | if (dst == NULL) |
| 722 | return -ENOMEM; |
| 723 | down_read(&card->controls_rwsem); |
| 724 | list.count = card->controls_count; |
| 725 | plist = card->controls.next; |
| 726 | while (plist != &card->controls) { |
| 727 | if (offset == 0) |
| 728 | break; |
| 729 | kctl = snd_kcontrol(plist); |
| 730 | if (offset < kctl->count) |
| 731 | break; |
| 732 | offset -= kctl->count; |
| 733 | plist = plist->next; |
| 734 | } |
| 735 | list.used = 0; |
| 736 | id = dst; |
| 737 | while (space > 0 && plist != &card->controls) { |
| 738 | kctl = snd_kcontrol(plist); |
| 739 | for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) { |
| 740 | snd_ctl_build_ioff(id, kctl, jidx); |
| 741 | id++; |
| 742 | space--; |
| 743 | list.used++; |
| 744 | } |
| 745 | plist = plist->next; |
| 746 | offset = 0; |
| 747 | } |
| 748 | up_read(&card->controls_rwsem); |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 749 | if (list.used > 0 && |
| 750 | copy_to_user(list.pids, dst, |
| 751 | list.used * sizeof(struct snd_ctl_elem_id))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | vfree(dst); |
| 753 | return -EFAULT; |
| 754 | } |
| 755 | vfree(dst); |
| 756 | } else { |
| 757 | down_read(&card->controls_rwsem); |
| 758 | list.count = card->controls_count; |
| 759 | up_read(&card->controls_rwsem); |
| 760 | } |
| 761 | if (copy_to_user(_list, &list, sizeof(list))) |
| 762 | return -EFAULT; |
| 763 | return 0; |
| 764 | } |
| 765 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 766 | static int snd_ctl_elem_info(struct snd_ctl_file *ctl, |
| 767 | struct snd_ctl_elem_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 769 | struct snd_card *card = ctl->card; |
| 770 | struct snd_kcontrol *kctl; |
| 771 | struct snd_kcontrol_volatile *vd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | unsigned int index_offset; |
| 773 | int result; |
| 774 | |
| 775 | down_read(&card->controls_rwsem); |
| 776 | kctl = snd_ctl_find_id(card, &info->id); |
| 777 | if (kctl == NULL) { |
| 778 | up_read(&card->controls_rwsem); |
| 779 | return -ENOENT; |
| 780 | } |
| 781 | #ifdef CONFIG_SND_DEBUG |
| 782 | info->access = 0; |
| 783 | #endif |
| 784 | result = kctl->info(kctl, info); |
| 785 | if (result >= 0) { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 786 | snd_BUG_ON(info->access); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | index_offset = snd_ctl_get_ioff(kctl, &info->id); |
| 788 | vd = &kctl->vd[index_offset]; |
| 789 | snd_ctl_build_ioff(&info->id, kctl, index_offset); |
| 790 | info->access = vd->access; |
| 791 | if (vd->owner) { |
| 792 | info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK; |
| 793 | if (vd->owner == ctl) |
| 794 | info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER; |
Clemens Ladisch | 25d27ed | 2009-11-02 09:35:44 +0100 | [diff] [blame] | 795 | info->owner = pid_vnr(vd->owner->pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | } else { |
| 797 | info->owner = -1; |
| 798 | } |
| 799 | } |
| 800 | up_read(&card->controls_rwsem); |
| 801 | return result; |
| 802 | } |
| 803 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 804 | static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl, |
| 805 | struct snd_ctl_elem_info __user *_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 807 | struct snd_ctl_elem_info info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | int result; |
| 809 | |
| 810 | if (copy_from_user(&info, _info, sizeof(info))) |
| 811 | return -EFAULT; |
Giuliano Pochini | 6464940 | 2006-03-13 14:11:11 +0100 | [diff] [blame] | 812 | snd_power_lock(ctl->card); |
Takashi Iwai | cbac4b0 | 2006-03-27 12:38:07 +0200 | [diff] [blame] | 813 | result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0); |
Giuliano Pochini | 6464940 | 2006-03-13 14:11:11 +0100 | [diff] [blame] | 814 | if (result >= 0) |
| 815 | result = snd_ctl_elem_info(ctl, &info); |
| 816 | snd_power_unlock(ctl->card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | if (result >= 0) |
| 818 | if (copy_to_user(_info, &info, sizeof(info))) |
| 819 | return -EFAULT; |
| 820 | return result; |
| 821 | } |
| 822 | |
Takashi Iwai | d3bd67c | 2008-06-12 18:17:26 +0200 | [diff] [blame] | 823 | static int snd_ctl_elem_read(struct snd_card *card, |
| 824 | struct snd_ctl_elem_value *control) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 826 | struct snd_kcontrol *kctl; |
| 827 | struct snd_kcontrol_volatile *vd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | unsigned int index_offset; |
Takashi Iwai | 8ace4f3 | 2008-01-08 17:57:26 +0100 | [diff] [blame] | 829 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | |
| 831 | down_read(&card->controls_rwsem); |
| 832 | kctl = snd_ctl_find_id(card, &control->id); |
| 833 | if (kctl == NULL) { |
| 834 | result = -ENOENT; |
| 835 | } else { |
| 836 | index_offset = snd_ctl_get_ioff(kctl, &control->id); |
| 837 | vd = &kctl->vd[index_offset]; |
Takashi Iwai | 8ace4f3 | 2008-01-08 17:57:26 +0100 | [diff] [blame] | 838 | if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && |
| 839 | kctl->get != NULL) { |
| 840 | snd_ctl_build_ioff(&control->id, kctl, index_offset); |
| 841 | result = kctl->get(kctl, control); |
| 842 | } else |
| 843 | result = -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | } |
| 845 | up_read(&card->controls_rwsem); |
| 846 | return result; |
| 847 | } |
| 848 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 849 | static int snd_ctl_elem_read_user(struct snd_card *card, |
| 850 | struct snd_ctl_elem_value __user *_control) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 852 | struct snd_ctl_elem_value *control; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | int result; |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 854 | |
| 855 | control = memdup_user(_control, sizeof(*control)); |
| 856 | if (IS_ERR(control)) |
| 857 | return PTR_ERR(control); |
| 858 | |
Giuliano Pochini | 6464940 | 2006-03-13 14:11:11 +0100 | [diff] [blame] | 859 | snd_power_lock(card); |
Takashi Iwai | cbac4b0 | 2006-03-27 12:38:07 +0200 | [diff] [blame] | 860 | result = snd_power_wait(card, SNDRV_CTL_POWER_D0); |
Giuliano Pochini | 6464940 | 2006-03-13 14:11:11 +0100 | [diff] [blame] | 861 | if (result >= 0) |
| 862 | result = snd_ctl_elem_read(card, control); |
| 863 | snd_power_unlock(card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | if (result >= 0) |
| 865 | if (copy_to_user(_control, control, sizeof(*control))) |
| 866 | result = -EFAULT; |
| 867 | kfree(control); |
| 868 | return result; |
| 869 | } |
| 870 | |
Takashi Iwai | d3bd67c | 2008-06-12 18:17:26 +0200 | [diff] [blame] | 871 | static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file, |
| 872 | struct snd_ctl_elem_value *control) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 874 | struct snd_kcontrol *kctl; |
| 875 | struct snd_kcontrol_volatile *vd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | unsigned int index_offset; |
Takashi Iwai | 8ace4f3 | 2008-01-08 17:57:26 +0100 | [diff] [blame] | 877 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | |
| 879 | down_read(&card->controls_rwsem); |
| 880 | kctl = snd_ctl_find_id(card, &control->id); |
| 881 | if (kctl == NULL) { |
| 882 | result = -ENOENT; |
| 883 | } else { |
| 884 | index_offset = snd_ctl_get_ioff(kctl, &control->id); |
| 885 | vd = &kctl->vd[index_offset]; |
Takashi Iwai | 8ace4f3 | 2008-01-08 17:57:26 +0100 | [diff] [blame] | 886 | if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) || |
| 887 | kctl->put == NULL || |
| 888 | (file && vd->owner && vd->owner != file)) { |
| 889 | result = -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | } else { |
Takashi Iwai | 8ace4f3 | 2008-01-08 17:57:26 +0100 | [diff] [blame] | 891 | snd_ctl_build_ioff(&control->id, kctl, index_offset); |
| 892 | result = kctl->put(kctl, control); |
| 893 | } |
| 894 | if (result > 0) { |
| 895 | up_read(&card->controls_rwsem); |
| 896 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 897 | &control->id); |
| 898 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | up_read(&card->controls_rwsem); |
| 902 | return result; |
| 903 | } |
| 904 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 905 | static int snd_ctl_elem_write_user(struct snd_ctl_file *file, |
| 906 | struct snd_ctl_elem_value __user *_control) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 908 | struct snd_ctl_elem_value *control; |
Giuliano Pochini | 6464940 | 2006-03-13 14:11:11 +0100 | [diff] [blame] | 909 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | int result; |
| 911 | |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 912 | control = memdup_user(_control, sizeof(*control)); |
| 913 | if (IS_ERR(control)) |
| 914 | return PTR_ERR(control); |
| 915 | |
Giuliano Pochini | 6464940 | 2006-03-13 14:11:11 +0100 | [diff] [blame] | 916 | card = file->card; |
| 917 | snd_power_lock(card); |
Takashi Iwai | cbac4b0 | 2006-03-27 12:38:07 +0200 | [diff] [blame] | 918 | result = snd_power_wait(card, SNDRV_CTL_POWER_D0); |
Giuliano Pochini | 6464940 | 2006-03-13 14:11:11 +0100 | [diff] [blame] | 919 | if (result >= 0) |
| 920 | result = snd_ctl_elem_write(card, file, control); |
| 921 | snd_power_unlock(card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | if (result >= 0) |
| 923 | if (copy_to_user(_control, control, sizeof(*control))) |
| 924 | result = -EFAULT; |
| 925 | kfree(control); |
| 926 | return result; |
| 927 | } |
| 928 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 929 | static int snd_ctl_elem_lock(struct snd_ctl_file *file, |
| 930 | struct snd_ctl_elem_id __user *_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 932 | struct snd_card *card = file->card; |
| 933 | struct snd_ctl_elem_id id; |
| 934 | struct snd_kcontrol *kctl; |
| 935 | struct snd_kcontrol_volatile *vd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | int result; |
| 937 | |
| 938 | if (copy_from_user(&id, _id, sizeof(id))) |
| 939 | return -EFAULT; |
| 940 | down_write(&card->controls_rwsem); |
| 941 | kctl = snd_ctl_find_id(card, &id); |
| 942 | if (kctl == NULL) { |
| 943 | result = -ENOENT; |
| 944 | } else { |
| 945 | vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)]; |
| 946 | if (vd->owner != NULL) |
| 947 | result = -EBUSY; |
| 948 | else { |
| 949 | vd->owner = file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | result = 0; |
| 951 | } |
| 952 | } |
| 953 | up_write(&card->controls_rwsem); |
| 954 | return result; |
| 955 | } |
| 956 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 957 | static int snd_ctl_elem_unlock(struct snd_ctl_file *file, |
| 958 | struct snd_ctl_elem_id __user *_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 960 | struct snd_card *card = file->card; |
| 961 | struct snd_ctl_elem_id id; |
| 962 | struct snd_kcontrol *kctl; |
| 963 | struct snd_kcontrol_volatile *vd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | int result; |
| 965 | |
| 966 | if (copy_from_user(&id, _id, sizeof(id))) |
| 967 | return -EFAULT; |
| 968 | down_write(&card->controls_rwsem); |
| 969 | kctl = snd_ctl_find_id(card, &id); |
| 970 | if (kctl == NULL) { |
| 971 | result = -ENOENT; |
| 972 | } else { |
| 973 | vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)]; |
| 974 | if (vd->owner == NULL) |
| 975 | result = -EINVAL; |
| 976 | else if (vd->owner != file) |
| 977 | result = -EPERM; |
| 978 | else { |
| 979 | vd->owner = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | result = 0; |
| 981 | } |
| 982 | } |
| 983 | up_write(&card->controls_rwsem); |
| 984 | return result; |
| 985 | } |
| 986 | |
| 987 | struct user_element { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 988 | struct snd_ctl_elem_info info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | void *elem_data; /* element data */ |
| 990 | unsigned long elem_data_size; /* size of element data in bytes */ |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 991 | void *tlv_data; /* TLV data */ |
| 992 | unsigned long tlv_data_size; /* TLV data size */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | void *priv_data; /* private data (like strings for enumerated type) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | }; |
| 995 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 996 | static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol, |
| 997 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | { |
| 999 | struct user_element *ue = kcontrol->private_data; |
| 1000 | |
| 1001 | *uinfo = ue->info; |
| 1002 | return 0; |
| 1003 | } |
| 1004 | |
Clemens Ladisch | 8d44816 | 2011-10-07 22:38:59 +0200 | [diff] [blame] | 1005 | static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol, |
| 1006 | struct snd_ctl_elem_info *uinfo) |
| 1007 | { |
| 1008 | struct user_element *ue = kcontrol->private_data; |
| 1009 | const char *names; |
| 1010 | unsigned int item; |
| 1011 | |
| 1012 | item = uinfo->value.enumerated.item; |
| 1013 | |
| 1014 | *uinfo = ue->info; |
| 1015 | |
| 1016 | item = min(item, uinfo->value.enumerated.items - 1); |
| 1017 | uinfo->value.enumerated.item = item; |
| 1018 | |
| 1019 | names = ue->priv_data; |
| 1020 | for (; item > 0; --item) |
| 1021 | names += strlen(names) + 1; |
| 1022 | strcpy(uinfo->value.enumerated.name, names); |
| 1023 | |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1027 | static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol, |
| 1028 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | { |
| 1030 | struct user_element *ue = kcontrol->private_data; |
| 1031 | |
| 1032 | memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size); |
| 1033 | return 0; |
| 1034 | } |
| 1035 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1036 | static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol, |
| 1037 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | { |
| 1039 | int change; |
| 1040 | struct user_element *ue = kcontrol->private_data; |
| 1041 | |
| 1042 | change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0; |
| 1043 | if (change) |
| 1044 | memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size); |
| 1045 | return change; |
| 1046 | } |
| 1047 | |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 1048 | static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol, |
| 1049 | int op_flag, |
| 1050 | unsigned int size, |
| 1051 | unsigned int __user *tlv) |
| 1052 | { |
| 1053 | struct user_element *ue = kcontrol->private_data; |
| 1054 | int change = 0; |
| 1055 | void *new_data; |
| 1056 | |
| 1057 | if (op_flag > 0) { |
| 1058 | if (size > 1024 * 128) /* sane value */ |
| 1059 | return -EINVAL; |
Li Zefan | ef44a1e | 2009-04-10 09:43:08 +0800 | [diff] [blame] | 1060 | |
| 1061 | new_data = memdup_user(tlv, size); |
| 1062 | if (IS_ERR(new_data)) |
| 1063 | return PTR_ERR(new_data); |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 1064 | change = ue->tlv_data_size != size; |
| 1065 | if (!change) |
| 1066 | change = memcmp(ue->tlv_data, new_data, size); |
| 1067 | kfree(ue->tlv_data); |
| 1068 | ue->tlv_data = new_data; |
| 1069 | ue->tlv_data_size = size; |
| 1070 | } else { |
Takashi Iwai | 18c1c3f | 2006-08-25 11:39:34 +0200 | [diff] [blame] | 1071 | if (! ue->tlv_data_size || ! ue->tlv_data) |
| 1072 | return -ENXIO; |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 1073 | if (size < ue->tlv_data_size) |
| 1074 | return -ENOSPC; |
| 1075 | if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size)) |
| 1076 | return -EFAULT; |
| 1077 | } |
| 1078 | return change; |
| 1079 | } |
| 1080 | |
Clemens Ladisch | 8d44816 | 2011-10-07 22:38:59 +0200 | [diff] [blame] | 1081 | static int snd_ctl_elem_init_enum_names(struct user_element *ue) |
| 1082 | { |
| 1083 | char *names, *p; |
| 1084 | size_t buf_len, name_len; |
| 1085 | unsigned int i; |
Olof Johansson | 447c6f9 | 2011-11-05 22:51:54 +0100 | [diff] [blame] | 1086 | const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr; |
Clemens Ladisch | 8d44816 | 2011-10-07 22:38:59 +0200 | [diff] [blame] | 1087 | |
| 1088 | if (ue->info.value.enumerated.names_length > 64 * 1024) |
| 1089 | return -EINVAL; |
| 1090 | |
Olof Johansson | 447c6f9 | 2011-11-05 22:51:54 +0100 | [diff] [blame] | 1091 | names = memdup_user((const void __user *)user_ptrval, |
Clemens Ladisch | 8d44816 | 2011-10-07 22:38:59 +0200 | [diff] [blame] | 1092 | ue->info.value.enumerated.names_length); |
| 1093 | if (IS_ERR(names)) |
| 1094 | return PTR_ERR(names); |
| 1095 | |
| 1096 | /* check that there are enough valid names */ |
| 1097 | buf_len = ue->info.value.enumerated.names_length; |
| 1098 | p = names; |
| 1099 | for (i = 0; i < ue->info.value.enumerated.items; ++i) { |
| 1100 | name_len = strnlen(p, buf_len); |
| 1101 | if (name_len == 0 || name_len >= 64 || name_len == buf_len) { |
| 1102 | kfree(names); |
| 1103 | return -EINVAL; |
| 1104 | } |
| 1105 | p += name_len + 1; |
| 1106 | buf_len -= name_len + 1; |
| 1107 | } |
| 1108 | |
| 1109 | ue->priv_data = names; |
| 1110 | ue->info.value.enumerated.names_ptr = 0; |
| 1111 | |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1115 | static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | { |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 1117 | struct user_element *ue = kcontrol->private_data; |
Clemens Ladisch | 8d44816 | 2011-10-07 22:38:59 +0200 | [diff] [blame] | 1118 | |
| 1119 | kfree(ue->tlv_data); |
| 1120 | kfree(ue->priv_data); |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 1121 | kfree(ue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | } |
| 1123 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1124 | static int snd_ctl_elem_add(struct snd_ctl_file *file, |
| 1125 | struct snd_ctl_elem_info *info, int replace) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1127 | struct snd_card *card = file->card; |
| 1128 | struct snd_kcontrol kctl, *_kctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | unsigned int access; |
| 1130 | long private_size; |
| 1131 | struct user_element *ue; |
| 1132 | int idx, err; |
Lu Guanqun | 983929c | 2011-08-24 11:12:34 +0800 | [diff] [blame] | 1133 | |
Lu Guanqun | 08ede03 | 2011-08-24 14:45:10 +0800 | [diff] [blame] | 1134 | if (!replace && card->user_ctl_count >= MAX_USER_CONTROLS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | return -ENOMEM; |
Clemens Ladisch | 2a031ae | 2009-08-17 12:25:52 +0200 | [diff] [blame] | 1136 | if (info->count < 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | return -EINVAL; |
| 1138 | access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE : |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1139 | (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE| |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 1140 | SNDRV_CTL_ELEM_ACCESS_INACTIVE| |
| 1141 | SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | info->id.numid = 0; |
| 1143 | memset(&kctl, 0, sizeof(kctl)); |
| 1144 | down_write(&card->controls_rwsem); |
| 1145 | _kctl = snd_ctl_find_id(card, &info->id); |
| 1146 | err = 0; |
| 1147 | if (_kctl) { |
| 1148 | if (replace) |
| 1149 | err = snd_ctl_remove(card, _kctl); |
| 1150 | else |
| 1151 | err = -EBUSY; |
| 1152 | } else { |
| 1153 | if (replace) |
| 1154 | err = -ENOENT; |
| 1155 | } |
| 1156 | up_write(&card->controls_rwsem); |
| 1157 | if (err < 0) |
| 1158 | return err; |
| 1159 | memcpy(&kctl.id, &info->id, sizeof(info->id)); |
| 1160 | kctl.count = info->owner ? info->owner : 1; |
| 1161 | access |= SNDRV_CTL_ELEM_ACCESS_USER; |
Clemens Ladisch | 8d44816 | 2011-10-07 22:38:59 +0200 | [diff] [blame] | 1162 | if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) |
| 1163 | kctl.info = snd_ctl_elem_user_enum_info; |
| 1164 | else |
| 1165 | kctl.info = snd_ctl_elem_user_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | if (access & SNDRV_CTL_ELEM_ACCESS_READ) |
| 1167 | kctl.get = snd_ctl_elem_user_get; |
| 1168 | if (access & SNDRV_CTL_ELEM_ACCESS_WRITE) |
| 1169 | kctl.put = snd_ctl_elem_user_put; |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 1170 | if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) { |
| 1171 | kctl.tlv.c = snd_ctl_elem_user_tlv; |
| 1172 | access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK; |
| 1173 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | switch (info->type) { |
| 1175 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
| 1177 | private_size = sizeof(long); |
| 1178 | if (info->count > 128) |
| 1179 | return -EINVAL; |
| 1180 | break; |
| 1181 | case SNDRV_CTL_ELEM_TYPE_INTEGER64: |
| 1182 | private_size = sizeof(long long); |
| 1183 | if (info->count > 64) |
| 1184 | return -EINVAL; |
| 1185 | break; |
Clemens Ladisch | 8d44816 | 2011-10-07 22:38:59 +0200 | [diff] [blame] | 1186 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: |
| 1187 | private_size = sizeof(unsigned int); |
| 1188 | if (info->count > 128 || info->value.enumerated.items == 0) |
| 1189 | return -EINVAL; |
| 1190 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
| 1192 | private_size = sizeof(unsigned char); |
| 1193 | if (info->count > 512) |
| 1194 | return -EINVAL; |
| 1195 | break; |
| 1196 | case SNDRV_CTL_ELEM_TYPE_IEC958: |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1197 | private_size = sizeof(struct snd_aes_iec958); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | if (info->count != 1) |
| 1199 | return -EINVAL; |
| 1200 | break; |
| 1201 | default: |
| 1202 | return -EINVAL; |
| 1203 | } |
| 1204 | private_size *= info->count; |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 1205 | ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | if (ue == NULL) |
| 1207 | return -ENOMEM; |
| 1208 | ue->info = *info; |
Takashi Iwai | 86148e8 | 2006-08-24 12:36:36 +0200 | [diff] [blame] | 1209 | ue->info.access = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | ue->elem_data = (char *)ue + sizeof(*ue); |
| 1211 | ue->elem_data_size = private_size; |
Clemens Ladisch | 8d44816 | 2011-10-07 22:38:59 +0200 | [diff] [blame] | 1212 | if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) { |
| 1213 | err = snd_ctl_elem_init_enum_names(ue); |
| 1214 | if (err < 0) { |
| 1215 | kfree(ue); |
| 1216 | return err; |
| 1217 | } |
| 1218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | kctl.private_free = snd_ctl_elem_user_free; |
| 1220 | _kctl = snd_ctl_new(&kctl, access); |
| 1221 | if (_kctl == NULL) { |
Clemens Ladisch | 8d44816 | 2011-10-07 22:38:59 +0200 | [diff] [blame] | 1222 | kfree(ue->priv_data); |
Takashi Iwai | 2fbf182 | 2006-03-06 15:42:51 -0800 | [diff] [blame] | 1223 | kfree(ue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | return -ENOMEM; |
| 1225 | } |
| 1226 | _kctl->private_data = ue; |
| 1227 | for (idx = 0; idx < _kctl->count; idx++) |
| 1228 | _kctl->vd[idx].owner = file; |
| 1229 | err = snd_ctl_add(card, _kctl); |
Takashi Iwai | 2fbf182 | 2006-03-06 15:42:51 -0800 | [diff] [blame] | 1230 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | |
| 1233 | down_write(&card->controls_rwsem); |
| 1234 | card->user_ctl_count++; |
| 1235 | up_write(&card->controls_rwsem); |
| 1236 | |
| 1237 | return 0; |
| 1238 | } |
| 1239 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1240 | static int snd_ctl_elem_add_user(struct snd_ctl_file *file, |
| 1241 | struct snd_ctl_elem_info __user *_info, int replace) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1243 | struct snd_ctl_elem_info info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | if (copy_from_user(&info, _info, sizeof(info))) |
| 1245 | return -EFAULT; |
| 1246 | return snd_ctl_elem_add(file, &info, replace); |
| 1247 | } |
| 1248 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1249 | static int snd_ctl_elem_remove(struct snd_ctl_file *file, |
| 1250 | struct snd_ctl_elem_id __user *_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1252 | struct snd_ctl_elem_id id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | |
| 1254 | if (copy_from_user(&id, _id, sizeof(id))) |
| 1255 | return -EFAULT; |
Clemens Ladisch | f217ac5 | 2009-08-17 12:27:22 +0200 | [diff] [blame] | 1256 | return snd_ctl_remove_user_ctl(file, &id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1259 | static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | { |
| 1261 | int subscribe; |
| 1262 | if (get_user(subscribe, ptr)) |
| 1263 | return -EFAULT; |
| 1264 | if (subscribe < 0) { |
| 1265 | subscribe = file->subscribed; |
| 1266 | if (put_user(subscribe, ptr)) |
| 1267 | return -EFAULT; |
| 1268 | return 0; |
| 1269 | } |
| 1270 | if (subscribe) { |
| 1271 | file->subscribed = 1; |
| 1272 | return 0; |
| 1273 | } else if (file->subscribed) { |
| 1274 | snd_ctl_empty_read_queue(file); |
| 1275 | file->subscribed = 0; |
| 1276 | } |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 1280 | static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file, |
| 1281 | struct snd_ctl_tlv __user *_tlv, |
| 1282 | int op_flag) |
Jaroslav Kysela | 42750b0 | 2006-06-01 18:34:01 +0200 | [diff] [blame] | 1283 | { |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 1284 | struct snd_card *card = file->card; |
Jaroslav Kysela | 42750b0 | 2006-06-01 18:34:01 +0200 | [diff] [blame] | 1285 | struct snd_ctl_tlv tlv; |
| 1286 | struct snd_kcontrol *kctl; |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 1287 | struct snd_kcontrol_volatile *vd; |
Jaroslav Kysela | 42750b0 | 2006-06-01 18:34:01 +0200 | [diff] [blame] | 1288 | unsigned int len; |
| 1289 | int err = 0; |
| 1290 | |
| 1291 | if (copy_from_user(&tlv, _tlv, sizeof(tlv))) |
| 1292 | return -EFAULT; |
Clemens Ladisch | 6123637 | 2010-02-01 13:30:56 +0100 | [diff] [blame] | 1293 | if (tlv.length < sizeof(unsigned int) * 2) |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 1294 | return -EINVAL; |
| 1295 | down_read(&card->controls_rwsem); |
| 1296 | kctl = snd_ctl_find_numid(card, tlv.numid); |
| 1297 | if (kctl == NULL) { |
| 1298 | err = -ENOENT; |
| 1299 | goto __kctl_end; |
| 1300 | } |
| 1301 | if (kctl->tlv.p == NULL) { |
| 1302 | err = -ENXIO; |
| 1303 | goto __kctl_end; |
| 1304 | } |
| 1305 | vd = &kctl->vd[tlv.numid - kctl->id.numid]; |
| 1306 | if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) || |
| 1307 | (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) || |
| 1308 | (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) { |
| 1309 | err = -ENXIO; |
| 1310 | goto __kctl_end; |
| 1311 | } |
| 1312 | if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
Dan Carpenter | bec145a | 2009-11-18 10:31:57 +0200 | [diff] [blame] | 1313 | if (vd->owner != NULL && vd->owner != file) { |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 1314 | err = -EPERM; |
| 1315 | goto __kctl_end; |
| 1316 | } |
Jeffrin Jose | bd483d4 | 2012-03-07 22:57:39 +0530 | [diff] [blame] | 1317 | err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv); |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 1318 | if (err > 0) { |
| 1319 | up_read(&card->controls_rwsem); |
| 1320 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &kctl->id); |
| 1321 | return 0; |
| 1322 | } |
| 1323 | } else { |
| 1324 | if (op_flag) { |
| 1325 | err = -ENXIO; |
| 1326 | goto __kctl_end; |
| 1327 | } |
| 1328 | len = kctl->tlv.p[1] + 2 * sizeof(unsigned int); |
| 1329 | if (tlv.length < len) { |
| 1330 | err = -ENOMEM; |
| 1331 | goto __kctl_end; |
| 1332 | } |
| 1333 | if (copy_to_user(_tlv->tlv, kctl->tlv.p, len)) |
| 1334 | err = -EFAULT; |
| 1335 | } |
Jaroslav Kysela | 42750b0 | 2006-06-01 18:34:01 +0200 | [diff] [blame] | 1336 | __kctl_end: |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 1337 | up_read(&card->controls_rwsem); |
| 1338 | return err; |
Jaroslav Kysela | 42750b0 | 2006-06-01 18:34:01 +0200 | [diff] [blame] | 1339 | } |
| 1340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 1342 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1343 | struct snd_ctl_file *ctl; |
| 1344 | struct snd_card *card; |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1345 | struct snd_kctl_ioctl *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | void __user *argp = (void __user *)arg; |
| 1347 | int __user *ip = argp; |
| 1348 | int err; |
| 1349 | |
| 1350 | ctl = file->private_data; |
| 1351 | card = ctl->card; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1352 | if (snd_BUG_ON(!card)) |
| 1353 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | switch (cmd) { |
| 1355 | case SNDRV_CTL_IOCTL_PVERSION: |
| 1356 | return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0; |
| 1357 | case SNDRV_CTL_IOCTL_CARD_INFO: |
| 1358 | return snd_ctl_card_info(card, ctl, cmd, argp); |
| 1359 | case SNDRV_CTL_IOCTL_ELEM_LIST: |
Jaroslav Kysela | 42750b0 | 2006-06-01 18:34:01 +0200 | [diff] [blame] | 1360 | return snd_ctl_elem_list(card, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | case SNDRV_CTL_IOCTL_ELEM_INFO: |
| 1362 | return snd_ctl_elem_info_user(ctl, argp); |
| 1363 | case SNDRV_CTL_IOCTL_ELEM_READ: |
Jaroslav Kysela | 42750b0 | 2006-06-01 18:34:01 +0200 | [diff] [blame] | 1364 | return snd_ctl_elem_read_user(card, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | case SNDRV_CTL_IOCTL_ELEM_WRITE: |
| 1366 | return snd_ctl_elem_write_user(ctl, argp); |
| 1367 | case SNDRV_CTL_IOCTL_ELEM_LOCK: |
| 1368 | return snd_ctl_elem_lock(ctl, argp); |
| 1369 | case SNDRV_CTL_IOCTL_ELEM_UNLOCK: |
| 1370 | return snd_ctl_elem_unlock(ctl, argp); |
| 1371 | case SNDRV_CTL_IOCTL_ELEM_ADD: |
| 1372 | return snd_ctl_elem_add_user(ctl, argp, 0); |
| 1373 | case SNDRV_CTL_IOCTL_ELEM_REPLACE: |
| 1374 | return snd_ctl_elem_add_user(ctl, argp, 1); |
| 1375 | case SNDRV_CTL_IOCTL_ELEM_REMOVE: |
| 1376 | return snd_ctl_elem_remove(ctl, argp); |
| 1377 | case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS: |
| 1378 | return snd_ctl_subscribe_events(ctl, ip); |
Jaroslav Kysela | 8aa9b58 | 2006-07-05 17:34:51 +0200 | [diff] [blame] | 1379 | case SNDRV_CTL_IOCTL_TLV_READ: |
| 1380 | return snd_ctl_tlv_ioctl(ctl, argp, 0); |
| 1381 | case SNDRV_CTL_IOCTL_TLV_WRITE: |
| 1382 | return snd_ctl_tlv_ioctl(ctl, argp, 1); |
| 1383 | case SNDRV_CTL_IOCTL_TLV_COMMAND: |
| 1384 | return snd_ctl_tlv_ioctl(ctl, argp, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | case SNDRV_CTL_IOCTL_POWER: |
Takashi Iwai | a381a7a | 2005-11-17 15:55:49 +0100 | [diff] [blame] | 1386 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | case SNDRV_CTL_IOCTL_POWER_STATE: |
| 1388 | #ifdef CONFIG_PM |
| 1389 | return put_user(card->power_state, ip) ? -EFAULT : 0; |
| 1390 | #else |
| 1391 | return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0; |
| 1392 | #endif |
| 1393 | } |
| 1394 | down_read(&snd_ioctl_rwsem); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 1395 | list_for_each_entry(p, &snd_control_ioctls, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | err = p->fioctl(card, ctl, cmd, arg); |
| 1397 | if (err != -ENOIOCTLCMD) { |
| 1398 | up_read(&snd_ioctl_rwsem); |
| 1399 | return err; |
| 1400 | } |
| 1401 | } |
| 1402 | up_read(&snd_ioctl_rwsem); |
Takashi Iwai | 6d85be6 | 2005-05-15 14:32:50 +0200 | [diff] [blame] | 1403 | snd_printdd("unknown ioctl = 0x%x\n", cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | return -ENOTTY; |
| 1405 | } |
| 1406 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1407 | static ssize_t snd_ctl_read(struct file *file, char __user *buffer, |
| 1408 | size_t count, loff_t * offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1410 | struct snd_ctl_file *ctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | int err = 0; |
| 1412 | ssize_t result = 0; |
| 1413 | |
| 1414 | ctl = file->private_data; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1415 | if (snd_BUG_ON(!ctl || !ctl->card)) |
| 1416 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | if (!ctl->subscribed) |
| 1418 | return -EBADFD; |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1419 | if (count < sizeof(struct snd_ctl_event)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | return -EINVAL; |
| 1421 | spin_lock_irq(&ctl->read_lock); |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1422 | while (count >= sizeof(struct snd_ctl_event)) { |
| 1423 | struct snd_ctl_event ev; |
| 1424 | struct snd_kctl_event *kev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | while (list_empty(&ctl->events)) { |
| 1426 | wait_queue_t wait; |
| 1427 | if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) { |
| 1428 | err = -EAGAIN; |
| 1429 | goto __end_lock; |
| 1430 | } |
| 1431 | init_waitqueue_entry(&wait, current); |
| 1432 | add_wait_queue(&ctl->change_sleep, &wait); |
| 1433 | set_current_state(TASK_INTERRUPTIBLE); |
| 1434 | spin_unlock_irq(&ctl->read_lock); |
| 1435 | schedule(); |
| 1436 | remove_wait_queue(&ctl->change_sleep, &wait); |
| 1437 | if (signal_pending(current)) |
Adrian Bunk | 0e5d720 | 2006-11-07 13:42:54 +0100 | [diff] [blame] | 1438 | return -ERESTARTSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | spin_lock_irq(&ctl->read_lock); |
| 1440 | } |
| 1441 | kev = snd_kctl_event(ctl->events.next); |
| 1442 | ev.type = SNDRV_CTL_EVENT_ELEM; |
| 1443 | ev.data.elem.mask = kev->mask; |
| 1444 | ev.data.elem.id = kev->id; |
| 1445 | list_del(&kev->list); |
| 1446 | spin_unlock_irq(&ctl->read_lock); |
| 1447 | kfree(kev); |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1448 | if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | err = -EFAULT; |
| 1450 | goto __end; |
| 1451 | } |
| 1452 | spin_lock_irq(&ctl->read_lock); |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1453 | buffer += sizeof(struct snd_ctl_event); |
| 1454 | count -= sizeof(struct snd_ctl_event); |
| 1455 | result += sizeof(struct snd_ctl_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | } |
| 1457 | __end_lock: |
| 1458 | spin_unlock_irq(&ctl->read_lock); |
| 1459 | __end: |
| 1460 | return result > 0 ? result : err; |
| 1461 | } |
| 1462 | |
| 1463 | static unsigned int snd_ctl_poll(struct file *file, poll_table * wait) |
| 1464 | { |
| 1465 | unsigned int mask; |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1466 | struct snd_ctl_file *ctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | |
| 1468 | ctl = file->private_data; |
| 1469 | if (!ctl->subscribed) |
| 1470 | return 0; |
| 1471 | poll_wait(file, &ctl->change_sleep, wait); |
| 1472 | |
| 1473 | mask = 0; |
| 1474 | if (!list_empty(&ctl->events)) |
| 1475 | mask |= POLLIN | POLLRDNORM; |
| 1476 | |
| 1477 | return mask; |
| 1478 | } |
| 1479 | |
| 1480 | /* |
| 1481 | * register the device-specific control-ioctls. |
| 1482 | * called from each device manager like pcm.c, hwdep.c, etc. |
| 1483 | */ |
| 1484 | static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists) |
| 1485 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1486 | struct snd_kctl_ioctl *pn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1488 | pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | if (pn == NULL) |
| 1490 | return -ENOMEM; |
| 1491 | pn->fioctl = fcn; |
| 1492 | down_write(&snd_ioctl_rwsem); |
| 1493 | list_add_tail(&pn->list, lists); |
| 1494 | up_write(&snd_ioctl_rwsem); |
| 1495 | return 0; |
| 1496 | } |
| 1497 | |
| 1498 | int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn) |
| 1499 | { |
| 1500 | return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls); |
| 1501 | } |
| 1502 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 1503 | EXPORT_SYMBOL(snd_ctl_register_ioctl); |
| 1504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | #ifdef CONFIG_COMPAT |
| 1506 | int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn) |
| 1507 | { |
| 1508 | return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls); |
| 1509 | } |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 1510 | |
| 1511 | EXPORT_SYMBOL(snd_ctl_register_ioctl_compat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | #endif |
| 1513 | |
| 1514 | /* |
| 1515 | * de-register the device-specific control-ioctls. |
| 1516 | */ |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1517 | static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn, |
| 1518 | struct list_head *lists) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1520 | struct snd_kctl_ioctl *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1522 | if (snd_BUG_ON(!fcn)) |
| 1523 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | down_write(&snd_ioctl_rwsem); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 1525 | list_for_each_entry(p, lists, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | if (p->fioctl == fcn) { |
| 1527 | list_del(&p->list); |
| 1528 | up_write(&snd_ioctl_rwsem); |
| 1529 | kfree(p); |
| 1530 | return 0; |
| 1531 | } |
| 1532 | } |
| 1533 | up_write(&snd_ioctl_rwsem); |
| 1534 | snd_BUG(); |
| 1535 | return -EINVAL; |
| 1536 | } |
| 1537 | |
| 1538 | int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn) |
| 1539 | { |
| 1540 | return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls); |
| 1541 | } |
| 1542 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 1543 | EXPORT_SYMBOL(snd_ctl_unregister_ioctl); |
| 1544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | #ifdef CONFIG_COMPAT |
| 1546 | int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn) |
| 1547 | { |
| 1548 | return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls); |
| 1549 | } |
| 1550 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 1551 | EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | #endif |
| 1553 | |
| 1554 | static int snd_ctl_fasync(int fd, struct file * file, int on) |
| 1555 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1556 | struct snd_ctl_file *ctl; |
Jonathan Corbet | 60aa492 | 2009-02-01 14:52:56 -0700 | [diff] [blame] | 1557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | ctl = file->private_data; |
Jonathan Corbet | 60aa492 | 2009-02-01 14:52:56 -0700 | [diff] [blame] | 1559 | return fasync_helper(fd, file, on, &ctl->fasync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | /* |
| 1563 | * ioctl32 compat |
| 1564 | */ |
| 1565 | #ifdef CONFIG_COMPAT |
| 1566 | #include "control_compat.c" |
| 1567 | #else |
| 1568 | #define snd_ctl_ioctl_compat NULL |
| 1569 | #endif |
| 1570 | |
| 1571 | /* |
| 1572 | * INIT PART |
| 1573 | */ |
| 1574 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 1575 | static const struct file_operations snd_ctl_f_ops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | { |
| 1577 | .owner = THIS_MODULE, |
| 1578 | .read = snd_ctl_read, |
| 1579 | .open = snd_ctl_open, |
| 1580 | .release = snd_ctl_release, |
Takashi Iwai | 02f4865 | 2010-04-13 11:49:04 +0200 | [diff] [blame] | 1581 | .llseek = no_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | .poll = snd_ctl_poll, |
| 1583 | .unlocked_ioctl = snd_ctl_ioctl, |
| 1584 | .compat_ioctl = snd_ctl_ioctl_compat, |
| 1585 | .fasync = snd_ctl_fasync, |
| 1586 | }; |
| 1587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | /* |
| 1589 | * registration of the control device |
| 1590 | */ |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1591 | static int snd_ctl_dev_register(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1593 | struct snd_card *card = device->device_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | int err, cardnum; |
| 1595 | char name[16]; |
| 1596 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1597 | if (snd_BUG_ON(!card)) |
| 1598 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | cardnum = card->number; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1600 | if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS)) |
| 1601 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | sprintf(name, "controlC%i", cardnum); |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 1603 | if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1, |
| 1604 | &snd_ctl_f_ops, card, name)) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | return err; |
| 1606 | return 0; |
| 1607 | } |
| 1608 | |
| 1609 | /* |
| 1610 | * disconnection of the control device |
| 1611 | */ |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1612 | static int snd_ctl_dev_disconnect(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1614 | struct snd_card *card = device->device_data; |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1615 | struct snd_ctl_file *ctl; |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 1616 | int err, cardnum; |
| 1617 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1618 | if (snd_BUG_ON(!card)) |
| 1619 | return -ENXIO; |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 1620 | cardnum = card->number; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1621 | if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS)) |
| 1622 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | |
Takashi Iwai | d800988 | 2008-09-07 12:51:13 +0200 | [diff] [blame] | 1624 | read_lock(&card->ctl_files_rwlock); |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 1625 | list_for_each_entry(ctl, &card->ctl_files, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | wake_up(&ctl->change_sleep); |
| 1627 | kill_fasync(&ctl->fasync, SIGIO, POLL_ERR); |
| 1628 | } |
Takashi Iwai | d800988 | 2008-09-07 12:51:13 +0200 | [diff] [blame] | 1629 | read_unlock(&card->ctl_files_rwlock); |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 1630 | |
| 1631 | if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL, |
| 1632 | card, -1)) < 0) |
| 1633 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | return 0; |
| 1635 | } |
| 1636 | |
| 1637 | /* |
| 1638 | * free all controls |
| 1639 | */ |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1640 | static int snd_ctl_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1642 | struct snd_card *card = device->device_data; |
| 1643 | struct snd_kcontrol *control; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | |
| 1645 | down_write(&card->controls_rwsem); |
| 1646 | while (!list_empty(&card->controls)) { |
| 1647 | control = snd_kcontrol(card->controls.next); |
| 1648 | snd_ctl_remove(card, control); |
| 1649 | } |
| 1650 | up_write(&card->controls_rwsem); |
| 1651 | return 0; |
| 1652 | } |
| 1653 | |
| 1654 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | * create control core: |
| 1656 | * called from init.c |
| 1657 | */ |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1658 | int snd_ctl_create(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | { |
Takashi Iwai | 82e9bae | 2005-11-17 13:53:23 +0100 | [diff] [blame] | 1660 | static struct snd_device_ops ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | .dev_free = snd_ctl_dev_free, |
| 1662 | .dev_register = snd_ctl_dev_register, |
| 1663 | .dev_disconnect = snd_ctl_dev_disconnect, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | }; |
| 1665 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1666 | if (snd_BUG_ON(!card)) |
| 1667 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops); |
| 1669 | } |
Takashi Iwai | b9ed4f2 | 2007-07-23 15:41:34 +0200 | [diff] [blame] | 1670 | |
| 1671 | /* |
Clemens Ladisch | 9600732 | 2011-01-10 16:25:44 +0100 | [diff] [blame] | 1672 | * Frequently used control callbacks/helpers |
Takashi Iwai | b9ed4f2 | 2007-07-23 15:41:34 +0200 | [diff] [blame] | 1673 | */ |
| 1674 | int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol, |
| 1675 | struct snd_ctl_elem_info *uinfo) |
| 1676 | { |
| 1677 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 1678 | uinfo->count = 1; |
| 1679 | uinfo->value.integer.min = 0; |
| 1680 | uinfo->value.integer.max = 1; |
| 1681 | return 0; |
| 1682 | } |
| 1683 | |
| 1684 | EXPORT_SYMBOL(snd_ctl_boolean_mono_info); |
| 1685 | |
| 1686 | int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol, |
| 1687 | struct snd_ctl_elem_info *uinfo) |
| 1688 | { |
| 1689 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 1690 | uinfo->count = 2; |
| 1691 | uinfo->value.integer.min = 0; |
| 1692 | uinfo->value.integer.max = 1; |
| 1693 | return 0; |
| 1694 | } |
| 1695 | |
| 1696 | EXPORT_SYMBOL(snd_ctl_boolean_stereo_info); |
Clemens Ladisch | 9600732 | 2011-01-10 16:25:44 +0100 | [diff] [blame] | 1697 | |
| 1698 | /** |
| 1699 | * snd_ctl_enum_info - fills the info structure for an enumerated control |
| 1700 | * @info: the structure to be filled |
| 1701 | * @channels: the number of the control's channels; often one |
| 1702 | * @items: the number of control values; also the size of @names |
| 1703 | * @names: an array containing the names of all control values |
| 1704 | * |
| 1705 | * Sets all required fields in @info to their appropriate values. |
| 1706 | * If the control's accessibility is not the default (readable and writable), |
| 1707 | * the caller has to fill @info->access. |
| 1708 | */ |
| 1709 | int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels, |
| 1710 | unsigned int items, const char *const names[]) |
| 1711 | { |
| 1712 | info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 1713 | info->count = channels; |
| 1714 | info->value.enumerated.items = items; |
| 1715 | if (info->value.enumerated.item >= items) |
| 1716 | info->value.enumerated.item = items - 1; |
| 1717 | strlcpy(info->value.enumerated.name, |
| 1718 | names[info->value.enumerated.item], |
| 1719 | sizeof(info->value.enumerated.name)); |
| 1720 | return 0; |
| 1721 | } |
| 1722 | EXPORT_SYMBOL(snd_ctl_enum_info); |