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