Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Routines for driver control interface | |
3 | * Copyright (c) by Jaroslav Kysela <perex@suse.cz> | |
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 | ||
22 | #include <sound/driver.h> | |
23 | #include <linux/threads.h> | |
24 | #include <linux/interrupt.h> | |
25 | #include <linux/smp_lock.h> | |
26 | #include <linux/slab.h> | |
27 | #include <linux/vmalloc.h> | |
28 | #include <linux/time.h> | |
29 | #include <sound/core.h> | |
30 | #include <sound/minors.h> | |
31 | #include <sound/info.h> | |
32 | #include <sound/control.h> | |
33 | ||
34 | /* max number of user-defined controls */ | |
35 | #define MAX_USER_CONTROLS 32 | |
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 | ||
f87135f5 | 55 | card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL); |
1da177e4 LT |
56 | if (!card) { |
57 | err = -ENODEV; | |
58 | goto __error1; | |
59 | } | |
60 | err = snd_card_file_add(card, file); | |
61 | if (err < 0) { | |
62 | err = -ENODEV; | |
63 | goto __error1; | |
64 | } | |
65 | if (!try_module_get(card->module)) { | |
66 | err = -EFAULT; | |
67 | goto __error2; | |
68 | } | |
ca2c0966 | 69 | ctl = kzalloc(sizeof(*ctl), GFP_KERNEL); |
1da177e4 LT |
70 | if (ctl == NULL) { |
71 | err = -ENOMEM; | |
72 | goto __error; | |
73 | } | |
74 | INIT_LIST_HEAD(&ctl->events); | |
75 | init_waitqueue_head(&ctl->change_sleep); | |
76 | spin_lock_init(&ctl->read_lock); | |
77 | ctl->card = card; | |
78 | ctl->pid = current->pid; | |
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); | |
83 | return 0; | |
84 | ||
85 | __error: | |
86 | module_put(card->module); | |
87 | __error2: | |
88 | snd_card_file_remove(card, file); | |
89 | __error1: | |
90 | return err; | |
91 | } | |
92 | ||
82e9bae6 | 93 | static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl) |
1da177e4 | 94 | { |
82e9bae6 | 95 | struct snd_kctl_event *cread; |
1da177e4 LT |
96 | |
97 | spin_lock(&ctl->read_lock); | |
98 | while (!list_empty(&ctl->events)) { | |
99 | cread = snd_kctl_event(ctl->events.next); | |
100 | list_del(&cread->list); | |
101 | kfree(cread); | |
102 | } | |
103 | spin_unlock(&ctl->read_lock); | |
104 | } | |
105 | ||
106 | static int snd_ctl_release(struct inode *inode, struct file *file) | |
107 | { | |
108 | unsigned long flags; | |
109 | struct list_head *list; | |
82e9bae6 TI |
110 | struct snd_card *card; |
111 | struct snd_ctl_file *ctl; | |
112 | struct snd_kcontrol *control; | |
1da177e4 LT |
113 | unsigned int idx; |
114 | ||
115 | ctl = file->private_data; | |
116 | fasync_helper(-1, file, 0, &ctl->fasync); | |
117 | file->private_data = NULL; | |
118 | card = ctl->card; | |
119 | write_lock_irqsave(&card->ctl_files_rwlock, flags); | |
120 | list_del(&ctl->list); | |
121 | write_unlock_irqrestore(&card->ctl_files_rwlock, flags); | |
122 | down_write(&card->controls_rwsem); | |
123 | list_for_each(list, &card->controls) { | |
124 | control = snd_kcontrol(list); | |
125 | for (idx = 0; idx < control->count; idx++) | |
126 | if (control->vd[idx].owner == ctl) | |
127 | control->vd[idx].owner = NULL; | |
128 | } | |
129 | up_write(&card->controls_rwsem); | |
130 | snd_ctl_empty_read_queue(ctl); | |
131 | kfree(ctl); | |
132 | module_put(card->module); | |
133 | snd_card_file_remove(card, file); | |
134 | return 0; | |
135 | } | |
136 | ||
82e9bae6 TI |
137 | void snd_ctl_notify(struct snd_card *card, unsigned int mask, |
138 | struct snd_ctl_elem_id *id) | |
1da177e4 LT |
139 | { |
140 | unsigned long flags; | |
141 | struct list_head *flist; | |
82e9bae6 TI |
142 | struct snd_ctl_file *ctl; |
143 | struct snd_kctl_event *ev; | |
1da177e4 | 144 | |
7c22f1aa | 145 | snd_assert(card != NULL && id != NULL, return); |
1da177e4 LT |
146 | read_lock(&card->ctl_files_rwlock); |
147 | #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE) | |
148 | card->mixer_oss_change_count++; | |
149 | #endif | |
150 | list_for_each(flist, &card->ctl_files) { | |
151 | struct list_head *elist; | |
152 | ctl = snd_ctl_file(flist); | |
153 | if (!ctl->subscribed) | |
154 | continue; | |
155 | spin_lock_irqsave(&ctl->read_lock, flags); | |
156 | list_for_each(elist, &ctl->events) { | |
157 | ev = snd_kctl_event(elist); | |
158 | if (ev->id.numid == id->numid) { | |
159 | ev->mask |= mask; | |
160 | goto _found; | |
161 | } | |
162 | } | |
ca2c0966 | 163 | ev = kzalloc(sizeof(*ev), GFP_ATOMIC); |
1da177e4 LT |
164 | if (ev) { |
165 | ev->id = *id; | |
166 | ev->mask = mask; | |
167 | list_add_tail(&ev->list, &ctl->events); | |
168 | } else { | |
169 | snd_printk(KERN_ERR "No memory available to allocate event\n"); | |
170 | } | |
171 | _found: | |
172 | wake_up(&ctl->change_sleep); | |
173 | spin_unlock_irqrestore(&ctl->read_lock, flags); | |
174 | kill_fasync(&ctl->fasync, SIGIO, POLL_IN); | |
175 | } | |
176 | read_unlock(&card->ctl_files_rwlock); | |
177 | } | |
178 | ||
c0d3fb39 TI |
179 | EXPORT_SYMBOL(snd_ctl_notify); |
180 | ||
1da177e4 LT |
181 | /** |
182 | * snd_ctl_new - create a control instance from the template | |
183 | * @control: the control template | |
184 | * @access: the default control access | |
185 | * | |
82e9bae6 | 186 | * Allocates a new struct snd_kcontrol instance and copies the given template |
1da177e4 LT |
187 | * to the new instance. It does not copy volatile data (access). |
188 | * | |
189 | * Returns the pointer of the new instance, or NULL on failure. | |
190 | */ | |
82e9bae6 | 191 | struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control, unsigned int access) |
1da177e4 | 192 | { |
82e9bae6 | 193 | struct snd_kcontrol *kctl; |
1da177e4 LT |
194 | unsigned int idx; |
195 | ||
7c22f1aa TI |
196 | snd_assert(control != NULL, return NULL); |
197 | snd_assert(control->count > 0, return NULL); | |
82e9bae6 | 198 | kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL); |
73e77ba0 TI |
199 | if (kctl == NULL) { |
200 | snd_printk(KERN_ERR "Cannot allocate control instance\n"); | |
1da177e4 | 201 | return NULL; |
73e77ba0 | 202 | } |
1da177e4 LT |
203 | *kctl = *control; |
204 | for (idx = 0; idx < kctl->count; idx++) | |
205 | kctl->vd[idx].access = access; | |
206 | return kctl; | |
207 | } | |
208 | ||
c0d3fb39 TI |
209 | EXPORT_SYMBOL(snd_ctl_new); |
210 | ||
1da177e4 LT |
211 | /** |
212 | * snd_ctl_new1 - create a control instance from the template | |
213 | * @ncontrol: the initialization record | |
214 | * @private_data: the private data to set | |
215 | * | |
82e9bae6 | 216 | * Allocates a new struct snd_kcontrol instance and initialize from the given |
1da177e4 LT |
217 | * template. When the access field of ncontrol is 0, it's assumed as |
218 | * READWRITE access. When the count field is 0, it's assumes as one. | |
219 | * | |
220 | * Returns the pointer of the newly generated instance, or NULL on failure. | |
221 | */ | |
82e9bae6 TI |
222 | struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol, |
223 | void *private_data) | |
1da177e4 | 224 | { |
82e9bae6 | 225 | struct snd_kcontrol kctl; |
1da177e4 LT |
226 | unsigned int access; |
227 | ||
7c22f1aa | 228 | snd_assert(ncontrol != NULL, return NULL); |
1da177e4 LT |
229 | snd_assert(ncontrol->info != NULL, return NULL); |
230 | memset(&kctl, 0, sizeof(kctl)); | |
231 | kctl.id.iface = ncontrol->iface; | |
232 | kctl.id.device = ncontrol->device; | |
233 | kctl.id.subdevice = ncontrol->subdevice; | |
234 | if (ncontrol->name) | |
235 | strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name)); | |
236 | kctl.id.index = ncontrol->index; | |
237 | kctl.count = ncontrol->count ? ncontrol->count : 1; | |
238 | access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE : | |
239 | (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|SNDRV_CTL_ELEM_ACCESS_INACTIVE| | |
240 | SNDRV_CTL_ELEM_ACCESS_DINDIRECT|SNDRV_CTL_ELEM_ACCESS_INDIRECT)); | |
241 | kctl.info = ncontrol->info; | |
242 | kctl.get = ncontrol->get; | |
243 | kctl.put = ncontrol->put; | |
42750b04 | 244 | kctl.tlv = ncontrol->tlv; |
1da177e4 LT |
245 | kctl.private_value = ncontrol->private_value; |
246 | kctl.private_data = private_data; | |
247 | return snd_ctl_new(&kctl, access); | |
248 | } | |
249 | ||
c0d3fb39 TI |
250 | EXPORT_SYMBOL(snd_ctl_new1); |
251 | ||
1da177e4 LT |
252 | /** |
253 | * snd_ctl_free_one - release the control instance | |
254 | * @kcontrol: the control instance | |
255 | * | |
256 | * Releases the control instance created via snd_ctl_new() | |
257 | * or snd_ctl_new1(). | |
258 | * Don't call this after the control was added to the card. | |
259 | */ | |
82e9bae6 | 260 | void snd_ctl_free_one(struct snd_kcontrol *kcontrol) |
1da177e4 LT |
261 | { |
262 | if (kcontrol) { | |
263 | if (kcontrol->private_free) | |
264 | kcontrol->private_free(kcontrol); | |
265 | kfree(kcontrol); | |
266 | } | |
267 | } | |
268 | ||
c0d3fb39 TI |
269 | EXPORT_SYMBOL(snd_ctl_free_one); |
270 | ||
82e9bae6 | 271 | static unsigned int snd_ctl_hole_check(struct snd_card *card, |
1da177e4 LT |
272 | unsigned int count) |
273 | { | |
274 | struct list_head *list; | |
82e9bae6 | 275 | struct snd_kcontrol *kctl; |
1da177e4 LT |
276 | |
277 | list_for_each(list, &card->controls) { | |
278 | kctl = snd_kcontrol(list); | |
279 | if ((kctl->id.numid <= card->last_numid && | |
280 | kctl->id.numid + kctl->count > card->last_numid) || | |
281 | (kctl->id.numid <= card->last_numid + count - 1 && | |
282 | kctl->id.numid + kctl->count > card->last_numid + count - 1)) | |
283 | return card->last_numid = kctl->id.numid + kctl->count - 1; | |
284 | } | |
285 | return card->last_numid; | |
286 | } | |
287 | ||
82e9bae6 | 288 | static int snd_ctl_find_hole(struct snd_card *card, unsigned int count) |
1da177e4 LT |
289 | { |
290 | unsigned int last_numid, iter = 100000; | |
291 | ||
292 | last_numid = card->last_numid; | |
293 | while (last_numid != snd_ctl_hole_check(card, count)) { | |
294 | if (--iter == 0) { | |
295 | /* this situation is very unlikely */ | |
296 | snd_printk(KERN_ERR "unable to allocate new control numid\n"); | |
297 | return -ENOMEM; | |
298 | } | |
299 | last_numid = card->last_numid; | |
300 | } | |
301 | return 0; | |
302 | } | |
303 | ||
304 | /** | |
305 | * snd_ctl_add - add the control instance to the card | |
306 | * @card: the card instance | |
307 | * @kcontrol: the control instance to add | |
308 | * | |
309 | * Adds the control instance created via snd_ctl_new() or | |
310 | * snd_ctl_new1() to the given card. Assigns also an unique | |
311 | * numid used for fast search. | |
312 | * | |
313 | * Returns zero if successful, or a negative error code on failure. | |
314 | * | |
315 | * It frees automatically the control which cannot be added. | |
316 | */ | |
82e9bae6 | 317 | int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol) |
1da177e4 | 318 | { |
82e9bae6 | 319 | struct snd_ctl_elem_id id; |
1da177e4 | 320 | unsigned int idx; |
c6077b30 | 321 | int err = -EINVAL; |
1da177e4 | 322 | |
73e77ba0 | 323 | if (! kcontrol) |
c6077b30 TI |
324 | return err; |
325 | snd_assert(card != NULL, goto error); | |
326 | snd_assert(kcontrol->info != NULL, goto error); | |
1da177e4 LT |
327 | id = kcontrol->id; |
328 | down_write(&card->controls_rwsem); | |
329 | if (snd_ctl_find_id(card, &id)) { | |
330 | up_write(&card->controls_rwsem); | |
1da177e4 LT |
331 | snd_printd(KERN_ERR "control %i:%i:%i:%s:%i is already present\n", |
332 | id.iface, | |
333 | id.device, | |
334 | id.subdevice, | |
335 | id.name, | |
336 | id.index); | |
c6077b30 TI |
337 | err = -EBUSY; |
338 | goto error; | |
1da177e4 LT |
339 | } |
340 | if (snd_ctl_find_hole(card, kcontrol->count) < 0) { | |
341 | up_write(&card->controls_rwsem); | |
c6077b30 TI |
342 | err = -ENOMEM; |
343 | goto error; | |
1da177e4 LT |
344 | } |
345 | list_add_tail(&kcontrol->list, &card->controls); | |
346 | card->controls_count += kcontrol->count; | |
347 | kcontrol->id.numid = card->last_numid + 1; | |
348 | card->last_numid += kcontrol->count; | |
349 | up_write(&card->controls_rwsem); | |
350 | for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++) | |
351 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id); | |
352 | return 0; | |
c6077b30 TI |
353 | |
354 | error: | |
355 | snd_ctl_free_one(kcontrol); | |
356 | return err; | |
1da177e4 LT |
357 | } |
358 | ||
c0d3fb39 TI |
359 | EXPORT_SYMBOL(snd_ctl_add); |
360 | ||
1da177e4 LT |
361 | /** |
362 | * snd_ctl_remove - remove the control from the card and release it | |
363 | * @card: the card instance | |
364 | * @kcontrol: the control instance to remove | |
365 | * | |
366 | * Removes the control from the card and then releases the instance. | |
367 | * You don't need to call snd_ctl_free_one(). You must be in | |
368 | * the write lock - down_write(&card->controls_rwsem). | |
369 | * | |
370 | * Returns 0 if successful, or a negative error code on failure. | |
371 | */ | |
82e9bae6 | 372 | int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol) |
1da177e4 | 373 | { |
82e9bae6 | 374 | struct snd_ctl_elem_id id; |
1da177e4 LT |
375 | unsigned int idx; |
376 | ||
7c22f1aa | 377 | snd_assert(card != NULL && kcontrol != NULL, return -EINVAL); |
1da177e4 LT |
378 | list_del(&kcontrol->list); |
379 | card->controls_count -= kcontrol->count; | |
380 | id = kcontrol->id; | |
381 | for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++) | |
382 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id); | |
383 | snd_ctl_free_one(kcontrol); | |
384 | return 0; | |
385 | } | |
386 | ||
c0d3fb39 TI |
387 | EXPORT_SYMBOL(snd_ctl_remove); |
388 | ||
1da177e4 LT |
389 | /** |
390 | * snd_ctl_remove_id - remove the control of the given id and release it | |
391 | * @card: the card instance | |
392 | * @id: the control id to remove | |
393 | * | |
394 | * Finds the control instance with the given id, removes it from the | |
395 | * card list and releases it. | |
396 | * | |
397 | * Returns 0 if successful, or a negative error code on failure. | |
398 | */ | |
82e9bae6 | 399 | int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id) |
1da177e4 | 400 | { |
82e9bae6 | 401 | struct snd_kcontrol *kctl; |
1da177e4 LT |
402 | int ret; |
403 | ||
404 | down_write(&card->controls_rwsem); | |
405 | kctl = snd_ctl_find_id(card, id); | |
406 | if (kctl == NULL) { | |
407 | up_write(&card->controls_rwsem); | |
408 | return -ENOENT; | |
409 | } | |
410 | ret = snd_ctl_remove(card, kctl); | |
411 | up_write(&card->controls_rwsem); | |
412 | return ret; | |
413 | } | |
414 | ||
c0d3fb39 TI |
415 | EXPORT_SYMBOL(snd_ctl_remove_id); |
416 | ||
1da177e4 LT |
417 | /** |
418 | * snd_ctl_remove_unlocked_id - remove the unlocked control of the given id and release it | |
419 | * @file: active control handle | |
420 | * @id: the control id to remove | |
421 | * | |
422 | * Finds the control instance with the given id, removes it from the | |
423 | * card list and releases it. | |
424 | * | |
425 | * Returns 0 if successful, or a negative error code on failure. | |
426 | */ | |
82e9bae6 TI |
427 | static int snd_ctl_remove_unlocked_id(struct snd_ctl_file * file, |
428 | struct snd_ctl_elem_id *id) | |
1da177e4 | 429 | { |
82e9bae6 TI |
430 | struct snd_card *card = file->card; |
431 | struct snd_kcontrol *kctl; | |
1da177e4 LT |
432 | int idx, ret; |
433 | ||
434 | down_write(&card->controls_rwsem); | |
435 | kctl = snd_ctl_find_id(card, id); | |
436 | if (kctl == NULL) { | |
437 | up_write(&card->controls_rwsem); | |
438 | return -ENOENT; | |
439 | } | |
440 | for (idx = 0; idx < kctl->count; idx++) | |
441 | if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) { | |
442 | up_write(&card->controls_rwsem); | |
443 | return -EBUSY; | |
444 | } | |
445 | ret = snd_ctl_remove(card, kctl); | |
446 | up_write(&card->controls_rwsem); | |
447 | return ret; | |
448 | } | |
449 | ||
450 | /** | |
451 | * snd_ctl_rename_id - replace the id of a control on the card | |
452 | * @card: the card instance | |
453 | * @src_id: the old id | |
454 | * @dst_id: the new id | |
455 | * | |
456 | * Finds the control with the old id from the card, and replaces the | |
457 | * id with the new one. | |
458 | * | |
459 | * Returns zero if successful, or a negative error code on failure. | |
460 | */ | |
82e9bae6 TI |
461 | int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id, |
462 | struct snd_ctl_elem_id *dst_id) | |
1da177e4 | 463 | { |
82e9bae6 | 464 | struct snd_kcontrol *kctl; |
1da177e4 LT |
465 | |
466 | down_write(&card->controls_rwsem); | |
467 | kctl = snd_ctl_find_id(card, src_id); | |
468 | if (kctl == NULL) { | |
469 | up_write(&card->controls_rwsem); | |
470 | return -ENOENT; | |
471 | } | |
472 | kctl->id = *dst_id; | |
473 | kctl->id.numid = card->last_numid + 1; | |
474 | card->last_numid += kctl->count; | |
475 | up_write(&card->controls_rwsem); | |
476 | return 0; | |
477 | } | |
478 | ||
c0d3fb39 TI |
479 | EXPORT_SYMBOL(snd_ctl_rename_id); |
480 | ||
1da177e4 LT |
481 | /** |
482 | * snd_ctl_find_numid - find the control instance with the given number-id | |
483 | * @card: the card instance | |
484 | * @numid: the number-id to search | |
485 | * | |
486 | * Finds the control instance with the given number-id from the card. | |
487 | * | |
488 | * Returns the pointer of the instance if found, or NULL if not. | |
489 | * | |
490 | * The caller must down card->controls_rwsem before calling this function | |
491 | * (if the race condition can happen). | |
492 | */ | |
82e9bae6 | 493 | struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid) |
1da177e4 LT |
494 | { |
495 | struct list_head *list; | |
82e9bae6 | 496 | struct snd_kcontrol *kctl; |
1da177e4 | 497 | |
7c22f1aa | 498 | snd_assert(card != NULL && numid != 0, return NULL); |
1da177e4 LT |
499 | list_for_each(list, &card->controls) { |
500 | kctl = snd_kcontrol(list); | |
501 | if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid) | |
502 | return kctl; | |
503 | } | |
504 | return NULL; | |
505 | } | |
506 | ||
c0d3fb39 TI |
507 | EXPORT_SYMBOL(snd_ctl_find_numid); |
508 | ||
1da177e4 LT |
509 | /** |
510 | * snd_ctl_find_id - find the control instance with the given id | |
511 | * @card: the card instance | |
512 | * @id: the id to search | |
513 | * | |
514 | * Finds the control instance with the given id from the card. | |
515 | * | |
516 | * Returns the pointer of the instance if found, or NULL if not. | |
517 | * | |
518 | * The caller must down card->controls_rwsem before calling this function | |
519 | * (if the race condition can happen). | |
520 | */ | |
82e9bae6 TI |
521 | struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card, |
522 | struct snd_ctl_elem_id *id) | |
1da177e4 LT |
523 | { |
524 | struct list_head *list; | |
82e9bae6 | 525 | struct snd_kcontrol *kctl; |
1da177e4 | 526 | |
7c22f1aa | 527 | snd_assert(card != NULL && id != NULL, return NULL); |
1da177e4 LT |
528 | if (id->numid != 0) |
529 | return snd_ctl_find_numid(card, id->numid); | |
530 | list_for_each(list, &card->controls) { | |
531 | kctl = snd_kcontrol(list); | |
532 | if (kctl->id.iface != id->iface) | |
533 | continue; | |
534 | if (kctl->id.device != id->device) | |
535 | continue; | |
536 | if (kctl->id.subdevice != id->subdevice) | |
537 | continue; | |
538 | if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name))) | |
539 | continue; | |
540 | if (kctl->id.index > id->index) | |
541 | continue; | |
542 | if (kctl->id.index + kctl->count <= id->index) | |
543 | continue; | |
544 | return kctl; | |
545 | } | |
546 | return NULL; | |
547 | } | |
548 | ||
c0d3fb39 TI |
549 | EXPORT_SYMBOL(snd_ctl_find_id); |
550 | ||
82e9bae6 | 551 | static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl, |
1da177e4 LT |
552 | unsigned int cmd, void __user *arg) |
553 | { | |
82e9bae6 | 554 | struct snd_ctl_card_info *info; |
1da177e4 | 555 | |
ca2c0966 | 556 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
1da177e4 LT |
557 | if (! info) |
558 | return -ENOMEM; | |
559 | down_read(&snd_ioctl_rwsem); | |
560 | info->card = card->number; | |
561 | strlcpy(info->id, card->id, sizeof(info->id)); | |
562 | strlcpy(info->driver, card->driver, sizeof(info->driver)); | |
563 | strlcpy(info->name, card->shortname, sizeof(info->name)); | |
564 | strlcpy(info->longname, card->longname, sizeof(info->longname)); | |
565 | strlcpy(info->mixername, card->mixername, sizeof(info->mixername)); | |
566 | strlcpy(info->components, card->components, sizeof(info->components)); | |
567 | up_read(&snd_ioctl_rwsem); | |
82e9bae6 | 568 | if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) { |
1da177e4 LT |
569 | kfree(info); |
570 | return -EFAULT; | |
571 | } | |
572 | kfree(info); | |
573 | return 0; | |
574 | } | |
575 | ||
82e9bae6 TI |
576 | static int snd_ctl_elem_list(struct snd_card *card, |
577 | struct snd_ctl_elem_list __user *_list) | |
1da177e4 LT |
578 | { |
579 | struct list_head *plist; | |
82e9bae6 TI |
580 | struct snd_ctl_elem_list list; |
581 | struct snd_kcontrol *kctl; | |
582 | struct snd_ctl_elem_id *dst, *id; | |
1da177e4 LT |
583 | unsigned int offset, space, first, jidx; |
584 | ||
585 | if (copy_from_user(&list, _list, sizeof(list))) | |
586 | return -EFAULT; | |
587 | offset = list.offset; | |
588 | space = list.space; | |
589 | first = 0; | |
590 | /* try limit maximum space */ | |
591 | if (space > 16384) | |
592 | return -ENOMEM; | |
593 | if (space > 0) { | |
594 | /* allocate temporary buffer for atomic operation */ | |
82e9bae6 | 595 | dst = vmalloc(space * sizeof(struct snd_ctl_elem_id)); |
1da177e4 LT |
596 | if (dst == NULL) |
597 | return -ENOMEM; | |
598 | down_read(&card->controls_rwsem); | |
599 | list.count = card->controls_count; | |
600 | plist = card->controls.next; | |
601 | while (plist != &card->controls) { | |
602 | if (offset == 0) | |
603 | break; | |
604 | kctl = snd_kcontrol(plist); | |
605 | if (offset < kctl->count) | |
606 | break; | |
607 | offset -= kctl->count; | |
608 | plist = plist->next; | |
609 | } | |
610 | list.used = 0; | |
611 | id = dst; | |
612 | while (space > 0 && plist != &card->controls) { | |
613 | kctl = snd_kcontrol(plist); | |
614 | for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) { | |
615 | snd_ctl_build_ioff(id, kctl, jidx); | |
616 | id++; | |
617 | space--; | |
618 | list.used++; | |
619 | } | |
620 | plist = plist->next; | |
621 | offset = 0; | |
622 | } | |
623 | up_read(&card->controls_rwsem); | |
82e9bae6 TI |
624 | if (list.used > 0 && |
625 | copy_to_user(list.pids, dst, | |
626 | list.used * sizeof(struct snd_ctl_elem_id))) { | |
1da177e4 LT |
627 | vfree(dst); |
628 | return -EFAULT; | |
629 | } | |
630 | vfree(dst); | |
631 | } else { | |
632 | down_read(&card->controls_rwsem); | |
633 | list.count = card->controls_count; | |
634 | up_read(&card->controls_rwsem); | |
635 | } | |
636 | if (copy_to_user(_list, &list, sizeof(list))) | |
637 | return -EFAULT; | |
638 | return 0; | |
639 | } | |
640 | ||
82e9bae6 TI |
641 | static int snd_ctl_elem_info(struct snd_ctl_file *ctl, |
642 | struct snd_ctl_elem_info *info) | |
1da177e4 | 643 | { |
82e9bae6 TI |
644 | struct snd_card *card = ctl->card; |
645 | struct snd_kcontrol *kctl; | |
646 | struct snd_kcontrol_volatile *vd; | |
1da177e4 LT |
647 | unsigned int index_offset; |
648 | int result; | |
649 | ||
650 | down_read(&card->controls_rwsem); | |
651 | kctl = snd_ctl_find_id(card, &info->id); | |
652 | if (kctl == NULL) { | |
653 | up_read(&card->controls_rwsem); | |
654 | return -ENOENT; | |
655 | } | |
656 | #ifdef CONFIG_SND_DEBUG | |
657 | info->access = 0; | |
658 | #endif | |
659 | result = kctl->info(kctl, info); | |
660 | if (result >= 0) { | |
661 | snd_assert(info->access == 0, ); | |
662 | index_offset = snd_ctl_get_ioff(kctl, &info->id); | |
663 | vd = &kctl->vd[index_offset]; | |
664 | snd_ctl_build_ioff(&info->id, kctl, index_offset); | |
665 | info->access = vd->access; | |
666 | if (vd->owner) { | |
667 | info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK; | |
668 | if (vd->owner == ctl) | |
669 | info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER; | |
670 | info->owner = vd->owner_pid; | |
671 | } else { | |
672 | info->owner = -1; | |
673 | } | |
674 | } | |
675 | up_read(&card->controls_rwsem); | |
676 | return result; | |
677 | } | |
678 | ||
82e9bae6 TI |
679 | static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl, |
680 | struct snd_ctl_elem_info __user *_info) | |
1da177e4 | 681 | { |
82e9bae6 | 682 | struct snd_ctl_elem_info info; |
1da177e4 LT |
683 | int result; |
684 | ||
685 | if (copy_from_user(&info, _info, sizeof(info))) | |
686 | return -EFAULT; | |
64649400 | 687 | snd_power_lock(ctl->card); |
cbac4b0c | 688 | result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0); |
64649400 GP |
689 | if (result >= 0) |
690 | result = snd_ctl_elem_info(ctl, &info); | |
691 | snd_power_unlock(ctl->card); | |
1da177e4 LT |
692 | if (result >= 0) |
693 | if (copy_to_user(_info, &info, sizeof(info))) | |
694 | return -EFAULT; | |
695 | return result; | |
696 | } | |
697 | ||
82e9bae6 | 698 | int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control) |
1da177e4 | 699 | { |
82e9bae6 TI |
700 | struct snd_kcontrol *kctl; |
701 | struct snd_kcontrol_volatile *vd; | |
1da177e4 LT |
702 | unsigned int index_offset; |
703 | int result, indirect; | |
704 | ||
705 | down_read(&card->controls_rwsem); | |
706 | kctl = snd_ctl_find_id(card, &control->id); | |
707 | if (kctl == NULL) { | |
708 | result = -ENOENT; | |
709 | } else { | |
710 | index_offset = snd_ctl_get_ioff(kctl, &control->id); | |
711 | vd = &kctl->vd[index_offset]; | |
712 | indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0; | |
713 | if (control->indirect != indirect) { | |
714 | result = -EACCES; | |
715 | } else { | |
716 | if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) { | |
717 | snd_ctl_build_ioff(&control->id, kctl, index_offset); | |
718 | result = kctl->get(kctl, control); | |
719 | } else { | |
720 | result = -EPERM; | |
721 | } | |
722 | } | |
723 | } | |
724 | up_read(&card->controls_rwsem); | |
725 | return result; | |
726 | } | |
727 | ||
c0d3fb39 TI |
728 | EXPORT_SYMBOL(snd_ctl_elem_read); |
729 | ||
82e9bae6 TI |
730 | static int snd_ctl_elem_read_user(struct snd_card *card, |
731 | struct snd_ctl_elem_value __user *_control) | |
1da177e4 | 732 | { |
82e9bae6 | 733 | struct snd_ctl_elem_value *control; |
1da177e4 LT |
734 | int result; |
735 | ||
736 | control = kmalloc(sizeof(*control), GFP_KERNEL); | |
737 | if (control == NULL) | |
738 | return -ENOMEM; | |
739 | if (copy_from_user(control, _control, sizeof(*control))) { | |
740 | kfree(control); | |
741 | return -EFAULT; | |
742 | } | |
64649400 | 743 | snd_power_lock(card); |
cbac4b0c | 744 | result = snd_power_wait(card, SNDRV_CTL_POWER_D0); |
64649400 GP |
745 | if (result >= 0) |
746 | result = snd_ctl_elem_read(card, control); | |
747 | snd_power_unlock(card); | |
1da177e4 LT |
748 | if (result >= 0) |
749 | if (copy_to_user(_control, control, sizeof(*control))) | |
750 | result = -EFAULT; | |
751 | kfree(control); | |
752 | return result; | |
753 | } | |
754 | ||
82e9bae6 TI |
755 | int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file, |
756 | struct snd_ctl_elem_value *control) | |
1da177e4 | 757 | { |
82e9bae6 TI |
758 | struct snd_kcontrol *kctl; |
759 | struct snd_kcontrol_volatile *vd; | |
1da177e4 LT |
760 | unsigned int index_offset; |
761 | int result, indirect; | |
762 | ||
763 | down_read(&card->controls_rwsem); | |
764 | kctl = snd_ctl_find_id(card, &control->id); | |
765 | if (kctl == NULL) { | |
766 | result = -ENOENT; | |
767 | } else { | |
768 | index_offset = snd_ctl_get_ioff(kctl, &control->id); | |
769 | vd = &kctl->vd[index_offset]; | |
770 | indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0; | |
771 | if (control->indirect != indirect) { | |
772 | result = -EACCES; | |
773 | } else { | |
774 | if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) || | |
775 | kctl->put == NULL || | |
776 | (file && vd->owner != NULL && vd->owner != file)) { | |
777 | result = -EPERM; | |
778 | } else { | |
779 | snd_ctl_build_ioff(&control->id, kctl, index_offset); | |
780 | result = kctl->put(kctl, control); | |
781 | } | |
782 | if (result > 0) { | |
783 | up_read(&card->controls_rwsem); | |
784 | snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id); | |
785 | return 0; | |
786 | } | |
787 | } | |
788 | } | |
789 | up_read(&card->controls_rwsem); | |
790 | return result; | |
791 | } | |
792 | ||
c0d3fb39 TI |
793 | EXPORT_SYMBOL(snd_ctl_elem_write); |
794 | ||
82e9bae6 TI |
795 | static int snd_ctl_elem_write_user(struct snd_ctl_file *file, |
796 | struct snd_ctl_elem_value __user *_control) | |
1da177e4 | 797 | { |
82e9bae6 | 798 | struct snd_ctl_elem_value *control; |
64649400 | 799 | struct snd_card *card; |
1da177e4 LT |
800 | int result; |
801 | ||
802 | control = kmalloc(sizeof(*control), GFP_KERNEL); | |
803 | if (control == NULL) | |
804 | return -ENOMEM; | |
805 | if (copy_from_user(control, _control, sizeof(*control))) { | |
806 | kfree(control); | |
807 | return -EFAULT; | |
808 | } | |
64649400 GP |
809 | card = file->card; |
810 | snd_power_lock(card); | |
cbac4b0c | 811 | result = snd_power_wait(card, SNDRV_CTL_POWER_D0); |
64649400 GP |
812 | if (result >= 0) |
813 | result = snd_ctl_elem_write(card, file, control); | |
814 | snd_power_unlock(card); | |
1da177e4 LT |
815 | if (result >= 0) |
816 | if (copy_to_user(_control, control, sizeof(*control))) | |
817 | result = -EFAULT; | |
818 | kfree(control); | |
819 | return result; | |
820 | } | |
821 | ||
82e9bae6 TI |
822 | static int snd_ctl_elem_lock(struct snd_ctl_file *file, |
823 | struct snd_ctl_elem_id __user *_id) | |
1da177e4 | 824 | { |
82e9bae6 TI |
825 | struct snd_card *card = file->card; |
826 | struct snd_ctl_elem_id id; | |
827 | struct snd_kcontrol *kctl; | |
828 | struct snd_kcontrol_volatile *vd; | |
1da177e4 LT |
829 | int result; |
830 | ||
831 | if (copy_from_user(&id, _id, sizeof(id))) | |
832 | return -EFAULT; | |
833 | down_write(&card->controls_rwsem); | |
834 | kctl = snd_ctl_find_id(card, &id); | |
835 | if (kctl == NULL) { | |
836 | result = -ENOENT; | |
837 | } else { | |
838 | vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)]; | |
839 | if (vd->owner != NULL) | |
840 | result = -EBUSY; | |
841 | else { | |
842 | vd->owner = file; | |
843 | vd->owner_pid = current->pid; | |
844 | result = 0; | |
845 | } | |
846 | } | |
847 | up_write(&card->controls_rwsem); | |
848 | return result; | |
849 | } | |
850 | ||
82e9bae6 TI |
851 | static int snd_ctl_elem_unlock(struct snd_ctl_file *file, |
852 | struct snd_ctl_elem_id __user *_id) | |
1da177e4 | 853 | { |
82e9bae6 TI |
854 | struct snd_card *card = file->card; |
855 | struct snd_ctl_elem_id id; | |
856 | struct snd_kcontrol *kctl; | |
857 | struct snd_kcontrol_volatile *vd; | |
1da177e4 LT |
858 | int result; |
859 | ||
860 | if (copy_from_user(&id, _id, sizeof(id))) | |
861 | return -EFAULT; | |
862 | down_write(&card->controls_rwsem); | |
863 | kctl = snd_ctl_find_id(card, &id); | |
864 | if (kctl == NULL) { | |
865 | result = -ENOENT; | |
866 | } else { | |
867 | vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)]; | |
868 | if (vd->owner == NULL) | |
869 | result = -EINVAL; | |
870 | else if (vd->owner != file) | |
871 | result = -EPERM; | |
872 | else { | |
873 | vd->owner = NULL; | |
874 | vd->owner_pid = 0; | |
875 | result = 0; | |
876 | } | |
877 | } | |
878 | up_write(&card->controls_rwsem); | |
879 | return result; | |
880 | } | |
881 | ||
882 | struct user_element { | |
82e9bae6 | 883 | struct snd_ctl_elem_info info; |
1da177e4 LT |
884 | void *elem_data; /* element data */ |
885 | unsigned long elem_data_size; /* size of element data in bytes */ | |
886 | void *priv_data; /* private data (like strings for enumerated type) */ | |
887 | unsigned long priv_data_size; /* size of private data in bytes */ | |
888 | }; | |
889 | ||
82e9bae6 TI |
890 | static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol, |
891 | struct snd_ctl_elem_info *uinfo) | |
1da177e4 LT |
892 | { |
893 | struct user_element *ue = kcontrol->private_data; | |
894 | ||
895 | *uinfo = ue->info; | |
896 | return 0; | |
897 | } | |
898 | ||
82e9bae6 TI |
899 | static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol, |
900 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
901 | { |
902 | struct user_element *ue = kcontrol->private_data; | |
903 | ||
904 | memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size); | |
905 | return 0; | |
906 | } | |
907 | ||
82e9bae6 TI |
908 | static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol, |
909 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 LT |
910 | { |
911 | int change; | |
912 | struct user_element *ue = kcontrol->private_data; | |
913 | ||
914 | change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0; | |
915 | if (change) | |
916 | memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size); | |
917 | return change; | |
918 | } | |
919 | ||
82e9bae6 | 920 | static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol) |
1da177e4 LT |
921 | { |
922 | kfree(kcontrol->private_data); | |
923 | } | |
924 | ||
82e9bae6 TI |
925 | static int snd_ctl_elem_add(struct snd_ctl_file *file, |
926 | struct snd_ctl_elem_info *info, int replace) | |
1da177e4 | 927 | { |
82e9bae6 TI |
928 | struct snd_card *card = file->card; |
929 | struct snd_kcontrol kctl, *_kctl; | |
1da177e4 LT |
930 | unsigned int access; |
931 | long private_size; | |
932 | struct user_element *ue; | |
933 | int idx, err; | |
934 | ||
935 | if (card->user_ctl_count >= MAX_USER_CONTROLS) | |
936 | return -ENOMEM; | |
937 | if (info->count > 1024) | |
938 | return -EINVAL; | |
939 | access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE : | |
82e9bae6 TI |
940 | (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE| |
941 | SNDRV_CTL_ELEM_ACCESS_INACTIVE)); | |
1da177e4 LT |
942 | info->id.numid = 0; |
943 | memset(&kctl, 0, sizeof(kctl)); | |
944 | down_write(&card->controls_rwsem); | |
945 | _kctl = snd_ctl_find_id(card, &info->id); | |
946 | err = 0; | |
947 | if (_kctl) { | |
948 | if (replace) | |
949 | err = snd_ctl_remove(card, _kctl); | |
950 | else | |
951 | err = -EBUSY; | |
952 | } else { | |
953 | if (replace) | |
954 | err = -ENOENT; | |
955 | } | |
956 | up_write(&card->controls_rwsem); | |
957 | if (err < 0) | |
958 | return err; | |
959 | memcpy(&kctl.id, &info->id, sizeof(info->id)); | |
960 | kctl.count = info->owner ? info->owner : 1; | |
961 | access |= SNDRV_CTL_ELEM_ACCESS_USER; | |
962 | kctl.info = snd_ctl_elem_user_info; | |
963 | if (access & SNDRV_CTL_ELEM_ACCESS_READ) | |
964 | kctl.get = snd_ctl_elem_user_get; | |
965 | if (access & SNDRV_CTL_ELEM_ACCESS_WRITE) | |
966 | kctl.put = snd_ctl_elem_user_put; | |
967 | switch (info->type) { | |
968 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: | |
969 | private_size = sizeof(char); | |
970 | if (info->count > 128) | |
971 | return -EINVAL; | |
972 | break; | |
973 | case SNDRV_CTL_ELEM_TYPE_INTEGER: | |
974 | private_size = sizeof(long); | |
975 | if (info->count > 128) | |
976 | return -EINVAL; | |
977 | break; | |
978 | case SNDRV_CTL_ELEM_TYPE_INTEGER64: | |
979 | private_size = sizeof(long long); | |
980 | if (info->count > 64) | |
981 | return -EINVAL; | |
982 | break; | |
983 | case SNDRV_CTL_ELEM_TYPE_BYTES: | |
984 | private_size = sizeof(unsigned char); | |
985 | if (info->count > 512) | |
986 | return -EINVAL; | |
987 | break; | |
988 | case SNDRV_CTL_ELEM_TYPE_IEC958: | |
82e9bae6 | 989 | private_size = sizeof(struct snd_aes_iec958); |
1da177e4 LT |
990 | if (info->count != 1) |
991 | return -EINVAL; | |
992 | break; | |
993 | default: | |
994 | return -EINVAL; | |
995 | } | |
996 | private_size *= info->count; | |
ca2c0966 | 997 | ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL); |
1da177e4 LT |
998 | if (ue == NULL) |
999 | return -ENOMEM; | |
1000 | ue->info = *info; | |
1001 | ue->elem_data = (char *)ue + sizeof(*ue); | |
1002 | ue->elem_data_size = private_size; | |
1003 | kctl.private_free = snd_ctl_elem_user_free; | |
1004 | _kctl = snd_ctl_new(&kctl, access); | |
1005 | if (_kctl == NULL) { | |
2fbf182e | 1006 | kfree(ue); |
1da177e4 LT |
1007 | return -ENOMEM; |
1008 | } | |
1009 | _kctl->private_data = ue; | |
1010 | for (idx = 0; idx < _kctl->count; idx++) | |
1011 | _kctl->vd[idx].owner = file; | |
1012 | err = snd_ctl_add(card, _kctl); | |
2fbf182e | 1013 | if (err < 0) |
1da177e4 | 1014 | return err; |
1da177e4 LT |
1015 | |
1016 | down_write(&card->controls_rwsem); | |
1017 | card->user_ctl_count++; | |
1018 | up_write(&card->controls_rwsem); | |
1019 | ||
1020 | return 0; | |
1021 | } | |
1022 | ||
82e9bae6 TI |
1023 | static int snd_ctl_elem_add_user(struct snd_ctl_file *file, |
1024 | struct snd_ctl_elem_info __user *_info, int replace) | |
1da177e4 | 1025 | { |
82e9bae6 | 1026 | struct snd_ctl_elem_info info; |
1da177e4 LT |
1027 | if (copy_from_user(&info, _info, sizeof(info))) |
1028 | return -EFAULT; | |
1029 | return snd_ctl_elem_add(file, &info, replace); | |
1030 | } | |
1031 | ||
82e9bae6 TI |
1032 | static int snd_ctl_elem_remove(struct snd_ctl_file *file, |
1033 | struct snd_ctl_elem_id __user *_id) | |
1da177e4 | 1034 | { |
82e9bae6 | 1035 | struct snd_ctl_elem_id id; |
1da177e4 LT |
1036 | int err; |
1037 | ||
1038 | if (copy_from_user(&id, _id, sizeof(id))) | |
1039 | return -EFAULT; | |
1040 | err = snd_ctl_remove_unlocked_id(file, &id); | |
1041 | if (! err) { | |
82e9bae6 | 1042 | struct snd_card *card = file->card; |
1da177e4 LT |
1043 | down_write(&card->controls_rwsem); |
1044 | card->user_ctl_count--; | |
1045 | up_write(&card->controls_rwsem); | |
1046 | } | |
1047 | return err; | |
1048 | } | |
1049 | ||
82e9bae6 | 1050 | static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr) |
1da177e4 LT |
1051 | { |
1052 | int subscribe; | |
1053 | if (get_user(subscribe, ptr)) | |
1054 | return -EFAULT; | |
1055 | if (subscribe < 0) { | |
1056 | subscribe = file->subscribed; | |
1057 | if (put_user(subscribe, ptr)) | |
1058 | return -EFAULT; | |
1059 | return 0; | |
1060 | } | |
1061 | if (subscribe) { | |
1062 | file->subscribed = 1; | |
1063 | return 0; | |
1064 | } else if (file->subscribed) { | |
1065 | snd_ctl_empty_read_queue(file); | |
1066 | file->subscribed = 0; | |
1067 | } | |
1068 | return 0; | |
1069 | } | |
1070 | ||
42750b04 JK |
1071 | static int snd_ctl_tlv_read(struct snd_card *card, |
1072 | struct snd_ctl_tlv __user *_tlv) | |
1073 | { | |
1074 | struct snd_ctl_tlv tlv; | |
1075 | struct snd_kcontrol *kctl; | |
1076 | unsigned int len; | |
1077 | int err = 0; | |
1078 | ||
1079 | if (copy_from_user(&tlv, _tlv, sizeof(tlv))) | |
1080 | return -EFAULT; | |
1081 | if (tlv.length < sizeof(unsigned int) * 3) | |
1082 | return -EINVAL; | |
1083 | down_read(&card->controls_rwsem); | |
1084 | kctl = snd_ctl_find_numid(card, tlv.numid); | |
1085 | if (kctl == NULL) { | |
1086 | err = -ENOENT; | |
1087 | goto __kctl_end; | |
1088 | } | |
1089 | if (kctl->tlv == NULL) { | |
1090 | err = -ENXIO; | |
1091 | goto __kctl_end; | |
1092 | } | |
1093 | len = kctl->tlv[1] + 2 * sizeof(unsigned int); | |
1094 | if (tlv.length < len) { | |
1095 | err = -ENOMEM; | |
1096 | goto __kctl_end; | |
1097 | } | |
1098 | if (copy_to_user(_tlv->tlv, kctl->tlv, len)) | |
1099 | err = -EFAULT; | |
1100 | __kctl_end: | |
1101 | up_read(&card->controls_rwsem); | |
1102 | return err; | |
1103 | } | |
1104 | ||
1da177e4 LT |
1105 | static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
1106 | { | |
82e9bae6 TI |
1107 | struct snd_ctl_file *ctl; |
1108 | struct snd_card *card; | |
1da177e4 | 1109 | struct list_head *list; |
82e9bae6 | 1110 | struct snd_kctl_ioctl *p; |
1da177e4 LT |
1111 | void __user *argp = (void __user *)arg; |
1112 | int __user *ip = argp; | |
1113 | int err; | |
1114 | ||
1115 | ctl = file->private_data; | |
1116 | card = ctl->card; | |
1117 | snd_assert(card != NULL, return -ENXIO); | |
1118 | switch (cmd) { | |
1119 | case SNDRV_CTL_IOCTL_PVERSION: | |
1120 | return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0; | |
1121 | case SNDRV_CTL_IOCTL_CARD_INFO: | |
1122 | return snd_ctl_card_info(card, ctl, cmd, argp); | |
1123 | case SNDRV_CTL_IOCTL_ELEM_LIST: | |
42750b04 | 1124 | return snd_ctl_elem_list(card, argp); |
1da177e4 LT |
1125 | case SNDRV_CTL_IOCTL_ELEM_INFO: |
1126 | return snd_ctl_elem_info_user(ctl, argp); | |
1127 | case SNDRV_CTL_IOCTL_ELEM_READ: | |
42750b04 | 1128 | return snd_ctl_elem_read_user(card, argp); |
1da177e4 LT |
1129 | case SNDRV_CTL_IOCTL_ELEM_WRITE: |
1130 | return snd_ctl_elem_write_user(ctl, argp); | |
1131 | case SNDRV_CTL_IOCTL_ELEM_LOCK: | |
1132 | return snd_ctl_elem_lock(ctl, argp); | |
1133 | case SNDRV_CTL_IOCTL_ELEM_UNLOCK: | |
1134 | return snd_ctl_elem_unlock(ctl, argp); | |
1135 | case SNDRV_CTL_IOCTL_ELEM_ADD: | |
1136 | return snd_ctl_elem_add_user(ctl, argp, 0); | |
1137 | case SNDRV_CTL_IOCTL_ELEM_REPLACE: | |
1138 | return snd_ctl_elem_add_user(ctl, argp, 1); | |
1139 | case SNDRV_CTL_IOCTL_ELEM_REMOVE: | |
1140 | return snd_ctl_elem_remove(ctl, argp); | |
1141 | case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS: | |
1142 | return snd_ctl_subscribe_events(ctl, ip); | |
42750b04 JK |
1143 | case SNDRV_CTL_IOCTL_TLV_READ: |
1144 | return snd_ctl_tlv_read(card, argp); | |
1da177e4 | 1145 | case SNDRV_CTL_IOCTL_POWER: |
a381a7a6 | 1146 | return -ENOPROTOOPT; |
1da177e4 LT |
1147 | case SNDRV_CTL_IOCTL_POWER_STATE: |
1148 | #ifdef CONFIG_PM | |
1149 | return put_user(card->power_state, ip) ? -EFAULT : 0; | |
1150 | #else | |
1151 | return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0; | |
1152 | #endif | |
1153 | } | |
1154 | down_read(&snd_ioctl_rwsem); | |
1155 | list_for_each(list, &snd_control_ioctls) { | |
82e9bae6 | 1156 | p = list_entry(list, struct snd_kctl_ioctl, list); |
1da177e4 LT |
1157 | err = p->fioctl(card, ctl, cmd, arg); |
1158 | if (err != -ENOIOCTLCMD) { | |
1159 | up_read(&snd_ioctl_rwsem); | |
1160 | return err; | |
1161 | } | |
1162 | } | |
1163 | up_read(&snd_ioctl_rwsem); | |
6d85be61 | 1164 | snd_printdd("unknown ioctl = 0x%x\n", cmd); |
1da177e4 LT |
1165 | return -ENOTTY; |
1166 | } | |
1167 | ||
82e9bae6 TI |
1168 | static ssize_t snd_ctl_read(struct file *file, char __user *buffer, |
1169 | size_t count, loff_t * offset) | |
1da177e4 | 1170 | { |
82e9bae6 | 1171 | struct snd_ctl_file *ctl; |
1da177e4 LT |
1172 | int err = 0; |
1173 | ssize_t result = 0; | |
1174 | ||
1175 | ctl = file->private_data; | |
1176 | snd_assert(ctl != NULL && ctl->card != NULL, return -ENXIO); | |
1177 | if (!ctl->subscribed) | |
1178 | return -EBADFD; | |
82e9bae6 | 1179 | if (count < sizeof(struct snd_ctl_event)) |
1da177e4 LT |
1180 | return -EINVAL; |
1181 | spin_lock_irq(&ctl->read_lock); | |
82e9bae6 TI |
1182 | while (count >= sizeof(struct snd_ctl_event)) { |
1183 | struct snd_ctl_event ev; | |
1184 | struct snd_kctl_event *kev; | |
1da177e4 LT |
1185 | while (list_empty(&ctl->events)) { |
1186 | wait_queue_t wait; | |
1187 | if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) { | |
1188 | err = -EAGAIN; | |
1189 | goto __end_lock; | |
1190 | } | |
1191 | init_waitqueue_entry(&wait, current); | |
1192 | add_wait_queue(&ctl->change_sleep, &wait); | |
1193 | set_current_state(TASK_INTERRUPTIBLE); | |
1194 | spin_unlock_irq(&ctl->read_lock); | |
1195 | schedule(); | |
1196 | remove_wait_queue(&ctl->change_sleep, &wait); | |
1197 | if (signal_pending(current)) | |
1198 | return result > 0 ? result : -ERESTARTSYS; | |
1199 | spin_lock_irq(&ctl->read_lock); | |
1200 | } | |
1201 | kev = snd_kctl_event(ctl->events.next); | |
1202 | ev.type = SNDRV_CTL_EVENT_ELEM; | |
1203 | ev.data.elem.mask = kev->mask; | |
1204 | ev.data.elem.id = kev->id; | |
1205 | list_del(&kev->list); | |
1206 | spin_unlock_irq(&ctl->read_lock); | |
1207 | kfree(kev); | |
82e9bae6 | 1208 | if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) { |
1da177e4 LT |
1209 | err = -EFAULT; |
1210 | goto __end; | |
1211 | } | |
1212 | spin_lock_irq(&ctl->read_lock); | |
82e9bae6 TI |
1213 | buffer += sizeof(struct snd_ctl_event); |
1214 | count -= sizeof(struct snd_ctl_event); | |
1215 | result += sizeof(struct snd_ctl_event); | |
1da177e4 LT |
1216 | } |
1217 | __end_lock: | |
1218 | spin_unlock_irq(&ctl->read_lock); | |
1219 | __end: | |
1220 | return result > 0 ? result : err; | |
1221 | } | |
1222 | ||
1223 | static unsigned int snd_ctl_poll(struct file *file, poll_table * wait) | |
1224 | { | |
1225 | unsigned int mask; | |
82e9bae6 | 1226 | struct snd_ctl_file *ctl; |
1da177e4 LT |
1227 | |
1228 | ctl = file->private_data; | |
1229 | if (!ctl->subscribed) | |
1230 | return 0; | |
1231 | poll_wait(file, &ctl->change_sleep, wait); | |
1232 | ||
1233 | mask = 0; | |
1234 | if (!list_empty(&ctl->events)) | |
1235 | mask |= POLLIN | POLLRDNORM; | |
1236 | ||
1237 | return mask; | |
1238 | } | |
1239 | ||
1240 | /* | |
1241 | * register the device-specific control-ioctls. | |
1242 | * called from each device manager like pcm.c, hwdep.c, etc. | |
1243 | */ | |
1244 | static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists) | |
1245 | { | |
82e9bae6 | 1246 | struct snd_kctl_ioctl *pn; |
1da177e4 | 1247 | |
82e9bae6 | 1248 | pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL); |
1da177e4 LT |
1249 | if (pn == NULL) |
1250 | return -ENOMEM; | |
1251 | pn->fioctl = fcn; | |
1252 | down_write(&snd_ioctl_rwsem); | |
1253 | list_add_tail(&pn->list, lists); | |
1254 | up_write(&snd_ioctl_rwsem); | |
1255 | return 0; | |
1256 | } | |
1257 | ||
1258 | int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn) | |
1259 | { | |
1260 | return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls); | |
1261 | } | |
1262 | ||
c0d3fb39 TI |
1263 | EXPORT_SYMBOL(snd_ctl_register_ioctl); |
1264 | ||
1da177e4 LT |
1265 | #ifdef CONFIG_COMPAT |
1266 | int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn) | |
1267 | { | |
1268 | return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls); | |
1269 | } | |
c0d3fb39 TI |
1270 | |
1271 | EXPORT_SYMBOL(snd_ctl_register_ioctl_compat); | |
1da177e4 LT |
1272 | #endif |
1273 | ||
1274 | /* | |
1275 | * de-register the device-specific control-ioctls. | |
1276 | */ | |
82e9bae6 TI |
1277 | static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn, |
1278 | struct list_head *lists) | |
1da177e4 LT |
1279 | { |
1280 | struct list_head *list; | |
82e9bae6 | 1281 | struct snd_kctl_ioctl *p; |
1da177e4 | 1282 | |
7c22f1aa | 1283 | snd_assert(fcn != NULL, return -EINVAL); |
1da177e4 LT |
1284 | down_write(&snd_ioctl_rwsem); |
1285 | list_for_each(list, lists) { | |
82e9bae6 | 1286 | p = list_entry(list, struct snd_kctl_ioctl, list); |
1da177e4 LT |
1287 | if (p->fioctl == fcn) { |
1288 | list_del(&p->list); | |
1289 | up_write(&snd_ioctl_rwsem); | |
1290 | kfree(p); | |
1291 | return 0; | |
1292 | } | |
1293 | } | |
1294 | up_write(&snd_ioctl_rwsem); | |
1295 | snd_BUG(); | |
1296 | return -EINVAL; | |
1297 | } | |
1298 | ||
1299 | int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn) | |
1300 | { | |
1301 | return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls); | |
1302 | } | |
1303 | ||
c0d3fb39 TI |
1304 | EXPORT_SYMBOL(snd_ctl_unregister_ioctl); |
1305 | ||
1da177e4 LT |
1306 | #ifdef CONFIG_COMPAT |
1307 | int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn) | |
1308 | { | |
1309 | return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls); | |
1310 | } | |
1311 | ||
c0d3fb39 | 1312 | EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat); |
1da177e4 LT |
1313 | #endif |
1314 | ||
1315 | static int snd_ctl_fasync(int fd, struct file * file, int on) | |
1316 | { | |
82e9bae6 | 1317 | struct snd_ctl_file *ctl; |
1da177e4 LT |
1318 | int err; |
1319 | ctl = file->private_data; | |
1320 | err = fasync_helper(fd, file, on, &ctl->fasync); | |
1321 | if (err < 0) | |
1322 | return err; | |
1323 | return 0; | |
1324 | } | |
1325 | ||
1326 | /* | |
1327 | * ioctl32 compat | |
1328 | */ | |
1329 | #ifdef CONFIG_COMPAT | |
1330 | #include "control_compat.c" | |
1331 | #else | |
1332 | #define snd_ctl_ioctl_compat NULL | |
1333 | #endif | |
1334 | ||
1335 | /* | |
1336 | * INIT PART | |
1337 | */ | |
1338 | ||
1339 | static struct file_operations snd_ctl_f_ops = | |
1340 | { | |
1341 | .owner = THIS_MODULE, | |
1342 | .read = snd_ctl_read, | |
1343 | .open = snd_ctl_open, | |
1344 | .release = snd_ctl_release, | |
1345 | .poll = snd_ctl_poll, | |
1346 | .unlocked_ioctl = snd_ctl_ioctl, | |
1347 | .compat_ioctl = snd_ctl_ioctl_compat, | |
1348 | .fasync = snd_ctl_fasync, | |
1349 | }; | |
1350 | ||
1da177e4 LT |
1351 | /* |
1352 | * registration of the control device | |
1353 | */ | |
82e9bae6 | 1354 | static int snd_ctl_dev_register(struct snd_device *device) |
1da177e4 | 1355 | { |
82e9bae6 | 1356 | struct snd_card *card = device->device_data; |
1da177e4 LT |
1357 | int err, cardnum; |
1358 | char name[16]; | |
1359 | ||
1360 | snd_assert(card != NULL, return -ENXIO); | |
1361 | cardnum = card->number; | |
1362 | snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO); | |
1363 | sprintf(name, "controlC%i", cardnum); | |
f87135f5 CL |
1364 | if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1, |
1365 | &snd_ctl_f_ops, card, name)) < 0) | |
1da177e4 LT |
1366 | return err; |
1367 | return 0; | |
1368 | } | |
1369 | ||
1370 | /* | |
1371 | * disconnection of the control device | |
1372 | */ | |
82e9bae6 | 1373 | static int snd_ctl_dev_disconnect(struct snd_device *device) |
1da177e4 | 1374 | { |
82e9bae6 | 1375 | struct snd_card *card = device->device_data; |
1da177e4 | 1376 | struct list_head *flist; |
82e9bae6 | 1377 | struct snd_ctl_file *ctl; |
1da177e4 LT |
1378 | |
1379 | down_read(&card->controls_rwsem); | |
1380 | list_for_each(flist, &card->ctl_files) { | |
1381 | ctl = snd_ctl_file(flist); | |
1382 | wake_up(&ctl->change_sleep); | |
1383 | kill_fasync(&ctl->fasync, SIGIO, POLL_ERR); | |
1384 | } | |
1385 | up_read(&card->controls_rwsem); | |
1386 | return 0; | |
1387 | } | |
1388 | ||
1389 | /* | |
1390 | * free all controls | |
1391 | */ | |
82e9bae6 | 1392 | static int snd_ctl_dev_free(struct snd_device *device) |
1da177e4 | 1393 | { |
82e9bae6 TI |
1394 | struct snd_card *card = device->device_data; |
1395 | struct snd_kcontrol *control; | |
1da177e4 LT |
1396 | |
1397 | down_write(&card->controls_rwsem); | |
1398 | while (!list_empty(&card->controls)) { | |
1399 | control = snd_kcontrol(card->controls.next); | |
1400 | snd_ctl_remove(card, control); | |
1401 | } | |
1402 | up_write(&card->controls_rwsem); | |
1403 | return 0; | |
1404 | } | |
1405 | ||
1406 | /* | |
1407 | * de-registration of the control device | |
1408 | */ | |
82e9bae6 | 1409 | static int snd_ctl_dev_unregister(struct snd_device *device) |
1da177e4 | 1410 | { |
82e9bae6 | 1411 | struct snd_card *card = device->device_data; |
1da177e4 LT |
1412 | int err, cardnum; |
1413 | ||
1414 | snd_assert(card != NULL, return -ENXIO); | |
1415 | cardnum = card->number; | |
1416 | snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO); | |
2af677fc CL |
1417 | if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL, |
1418 | card, -1)) < 0) | |
1da177e4 LT |
1419 | return err; |
1420 | return snd_ctl_dev_free(device); | |
1421 | } | |
1422 | ||
1423 | /* | |
1424 | * create control core: | |
1425 | * called from init.c | |
1426 | */ | |
82e9bae6 | 1427 | int snd_ctl_create(struct snd_card *card) |
1da177e4 | 1428 | { |
82e9bae6 | 1429 | static struct snd_device_ops ops = { |
1da177e4 LT |
1430 | .dev_free = snd_ctl_dev_free, |
1431 | .dev_register = snd_ctl_dev_register, | |
1432 | .dev_disconnect = snd_ctl_dev_disconnect, | |
1433 | .dev_unregister = snd_ctl_dev_unregister | |
1434 | }; | |
1435 | ||
1436 | snd_assert(card != NULL, return -ENXIO); | |
1437 | return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops); | |
1438 | } |