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