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