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