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