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