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