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