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