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