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