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