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