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