ALSA: core: Fix possible memory leaks at error path in info.c
[deliverable/linux.git] / sound / core / info.c
CommitLineData
1da177e4
LT
1/*
2 * Information interface for ALSA driver
c1017a4c 3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
1da177e4 22#include <linux/init.h>
1da177e4 23#include <linux/time.h>
27ac792c 24#include <linux/mm.h>
5a0e3ad6 25#include <linux/slab.h>
543537bd 26#include <linux/string.h>
da155d5b 27#include <linux/module.h>
1da177e4
LT
28#include <sound/core.h>
29#include <sound/minors.h>
30#include <sound/info.h>
42662748 31#include <linux/utsname.h>
1da177e4 32#include <linux/proc_fs.h>
1a60d4c5 33#include <linux/mutex.h>
1da177e4
LT
34#include <stdarg.h>
35
36/*
37 *
38 */
39
e28563cc
TI
40#ifdef CONFIG_PROC_FS
41
1da177e4
LT
42int snd_info_check_reserved_words(const char *str)
43{
44 static char *reserved[] =
45 {
46 "version",
47 "meminfo",
48 "memdebug",
49 "detect",
50 "devices",
51 "oss",
52 "cards",
53 "timers",
54 "synth",
55 "pcm",
56 "seq",
57 NULL
58 };
59 char **xstr = reserved;
60
61 while (*xstr) {
62 if (!strcmp(*xstr, str))
63 return 0;
64 xstr++;
65 }
66 if (!strncmp(str, "card", 4))
67 return 0;
68 return 1;
69}
70
1a60d4c5 71static DEFINE_MUTEX(info_mutex);
1da177e4 72
24c1f931
TI
73struct snd_info_private_data {
74 struct snd_info_buffer *rbuffer;
75 struct snd_info_buffer *wbuffer;
76 struct snd_info_entry *entry;
1da177e4 77 void *file_private_data;
24c1f931 78};
1da177e4
LT
79
80static int snd_info_version_init(void);
81static int snd_info_version_done(void);
746d4a02 82static void snd_info_disconnect(struct snd_info_entry *entry);
1da177e4 83
1da177e4
LT
84/*
85
86 */
87
6581f4e7
TI
88static struct proc_dir_entry *snd_proc_root;
89struct snd_info_entry *snd_seq_root;
c0d3fb39
TI
90EXPORT_SYMBOL(snd_seq_root);
91
1da177e4 92#ifdef CONFIG_SND_OSSEMUL
6581f4e7 93struct snd_info_entry *snd_oss_root;
1da177e4
LT
94#endif
95
4adb7bcb
TI
96static int alloc_info_private(struct snd_info_entry *entry,
97 struct snd_info_private_data **ret)
98{
99 struct snd_info_private_data *data;
100
101 if (!entry || !entry->p)
102 return -ENODEV;
103 if (!try_module_get(entry->module))
104 return -EFAULT;
105 data = kzalloc(sizeof(*data), GFP_KERNEL);
106 if (!data) {
107 module_put(entry->module);
108 return -ENOMEM;
109 }
110 data->entry = entry;
111 *ret = data;
112 return 0;
113}
114
115static bool valid_pos(loff_t pos, size_t count)
116{
117 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
118 return false;
119 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
120 return false;
121 return true;
122}
123
124/*
125 * file ops for binary proc files
126 */
1da177e4
LT
127static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
128{
24c1f931 129 struct snd_info_private_data *data;
1da177e4 130 struct snd_info_entry *entry;
73029e0f 131 loff_t ret = -EINVAL, size;
1da177e4
LT
132
133 data = file->private_data;
134 entry = data->entry;
5b5cd553 135 mutex_lock(&entry->access);
4adb7bcb 136 if (entry->c.ops->llseek) {
73029e0f
TI
137 offset = entry->c.ops->llseek(entry,
138 data->file_private_data,
139 file, offset, orig);
140 goto out;
141 }
4adb7bcb
TI
142
143 size = entry->size;
73029e0f
TI
144 switch (orig) {
145 case SEEK_SET:
146 break;
147 case SEEK_CUR:
148 offset += file->f_pos;
1da177e4 149 break;
73029e0f
TI
150 case SEEK_END:
151 if (!size)
1da177e4 152 goto out;
73029e0f 153 offset += size;
1da177e4 154 break;
73029e0f
TI
155 default:
156 goto out;
1da177e4 157 }
73029e0f
TI
158 if (offset < 0)
159 goto out;
160 if (size && offset > size)
161 offset = size;
162 file->f_pos = offset;
163 ret = offset;
164 out:
5b5cd553 165 mutex_unlock(&entry->access);
1da177e4
LT
166 return ret;
167}
168
169static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
170 size_t count, loff_t * offset)
171{
4adb7bcb
TI
172 struct snd_info_private_data *data = file->private_data;
173 struct snd_info_entry *entry = data->entry;
174 size_t size;
1da177e4
LT
175 loff_t pos;
176
1da177e4 177 pos = *offset;
4adb7bcb 178 if (!valid_pos(pos, count))
1da177e4 179 return -EIO;
4adb7bcb
TI
180 if (pos >= entry->size)
181 return 0;
182 size = entry->size - pos;
183 size = min(count, size);
184 size = entry->c.ops->read(entry, data->file_private_data,
185 file, buffer, size, pos);
1da177e4
LT
186 if ((ssize_t) size > 0)
187 *offset = pos + size;
188 return size;
189}
190
191static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
192 size_t count, loff_t * offset)
193{
4adb7bcb
TI
194 struct snd_info_private_data *data = file->private_data;
195 struct snd_info_entry *entry = data->entry;
7e4eeec8 196 ssize_t size = 0;
1da177e4
LT
197 loff_t pos;
198
1da177e4 199 pos = *offset;
4adb7bcb 200 if (!valid_pos(pos, count))
1da177e4 201 return -EIO;
4adb7bcb
TI
202 if (count > 0) {
203 size_t maxsize = entry->size - pos;
204 count = min(count, maxsize);
205 size = entry->c.ops->write(entry, data->file_private_data,
206 file, buffer, count, pos);
1da177e4 207 }
4adb7bcb 208 if (size > 0)
1da177e4
LT
209 *offset = pos + size;
210 return size;
211}
212
4adb7bcb
TI
213static unsigned int snd_info_entry_poll(struct file *file, poll_table *wait)
214{
215 struct snd_info_private_data *data = file->private_data;
216 struct snd_info_entry *entry = data->entry;
217 unsigned int mask = 0;
218
219 if (entry->c.ops->poll)
220 return entry->c.ops->poll(entry,
221 data->file_private_data,
222 file, wait);
223 if (entry->c.ops->read)
224 mask |= POLLIN | POLLRDNORM;
225 if (entry->c.ops->write)
226 mask |= POLLOUT | POLLWRNORM;
227 return mask;
228}
229
230static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
231 unsigned long arg)
232{
233 struct snd_info_private_data *data = file->private_data;
234 struct snd_info_entry *entry = data->entry;
235
236 if (!entry->c.ops->ioctl)
237 return -ENOTTY;
238 return entry->c.ops->ioctl(entry, data->file_private_data,
239 file, cmd, arg);
240}
241
242static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
1da177e4 243{
4adb7bcb
TI
244 struct inode *inode = file_inode(file);
245 struct snd_info_private_data *data;
24c1f931 246 struct snd_info_entry *entry;
4adb7bcb
TI
247
248 data = file->private_data;
249 if (data == NULL)
250 return 0;
251 entry = data->entry;
252 if (!entry->c.ops->mmap)
253 return -ENXIO;
254 return entry->c.ops->mmap(entry, data->file_private_data,
255 inode, file, vma);
256}
257
258static int snd_info_entry_open(struct inode *inode, struct file *file)
259{
260 struct snd_info_entry *entry = PDE_DATA(inode);
24c1f931 261 struct snd_info_private_data *data;
1da177e4
LT
262 int mode, err;
263
1a60d4c5 264 mutex_lock(&info_mutex);
4adb7bcb
TI
265 err = alloc_info_private(entry, &data);
266 if (err < 0)
267 goto unlock;
268
1da177e4 269 mode = file->f_flags & O_ACCMODE;
4adb7bcb
TI
270 if (((mode == O_RDONLY || mode == O_RDWR) && !entry->c.ops->read) ||
271 ((mode == O_WRONLY || mode == O_RDWR) && !entry->c.ops->write)) {
272 err = -ENODEV;
273 goto error;
1da177e4 274 }
4adb7bcb
TI
275
276 if (entry->c.ops->open) {
277 err = entry->c.ops->open(entry, mode, &data->file_private_data);
278 if (err < 0)
279 goto error;
1da177e4 280 }
4adb7bcb 281
1da177e4 282 file->private_data = data;
1a60d4c5 283 mutex_unlock(&info_mutex);
1da177e4
LT
284 return 0;
285
4adb7bcb 286 error:
7e4eeec8 287 kfree(data);
1da177e4 288 module_put(entry->module);
4adb7bcb 289 unlock:
1a60d4c5 290 mutex_unlock(&info_mutex);
1da177e4
LT
291 return err;
292}
293
294static int snd_info_entry_release(struct inode *inode, struct file *file)
295{
4adb7bcb
TI
296 struct snd_info_private_data *data = file->private_data;
297 struct snd_info_entry *entry = data->entry;
1da177e4 298
4adb7bcb
TI
299 if (entry->c.ops->release)
300 entry->c.ops->release(entry, file->f_flags & O_ACCMODE,
301 data->file_private_data);
1da177e4
LT
302 module_put(entry->module);
303 kfree(data);
304 return 0;
305}
306
4adb7bcb 307static const struct file_operations snd_info_entry_operations =
1da177e4 308{
4adb7bcb
TI
309 .owner = THIS_MODULE,
310 .llseek = snd_info_entry_llseek,
311 .read = snd_info_entry_read,
312 .write = snd_info_entry_write,
313 .poll = snd_info_entry_poll,
314 .unlocked_ioctl = snd_info_entry_ioctl,
315 .mmap = snd_info_entry_mmap,
316 .open = snd_info_entry_open,
317 .release = snd_info_entry_release,
318};
1da177e4 319
4adb7bcb
TI
320/*
321 * file ops for text proc files
322 */
323static ssize_t snd_info_text_entry_write(struct file *file,
324 const char __user *buffer,
325 size_t count, loff_t *offset)
326{
327 struct seq_file *m = file->private_data;
328 struct snd_info_private_data *data = m->private;
329 struct snd_info_entry *entry = data->entry;
330 struct snd_info_buffer *buf;
331 loff_t pos;
332 size_t next;
333 int err = 0;
334
335 pos = *offset;
336 if (!valid_pos(pos, count))
337 return -EIO;
338 next = pos + count;
339 mutex_lock(&entry->access);
340 buf = data->wbuffer;
341 if (!buf) {
342 data->wbuffer = buf = kzalloc(sizeof(*buf), GFP_KERNEL);
343 if (!buf) {
344 err = -ENOMEM;
345 goto error;
346 }
1da177e4 347 }
4adb7bcb
TI
348 if (next > buf->len) {
349 char *nbuf = krealloc(buf->buffer, PAGE_ALIGN(next),
350 GFP_KERNEL | __GFP_ZERO);
351 if (!nbuf) {
352 err = -ENOMEM;
353 goto error;
354 }
355 buf->buffer = nbuf;
356 buf->len = PAGE_ALIGN(next);
357 }
358 if (copy_from_user(buf->buffer + pos, buffer, count)) {
359 err = -EFAULT;
360 goto error;
361 }
362 buf->size = next;
363 error:
364 mutex_unlock(&entry->access);
365 if (err < 0)
366 return err;
367 *offset = next;
368 return count;
1da177e4
LT
369}
370
4adb7bcb 371static int snd_info_seq_show(struct seq_file *seq, void *p)
1da177e4 372{
4adb7bcb
TI
373 struct snd_info_private_data *data = seq->private;
374 struct snd_info_entry *entry = data->entry;
1da177e4 375
4adb7bcb
TI
376 if (entry->c.text.read) {
377 data->rbuffer->buffer = (char *)seq; /* XXX hack! */
378 entry->c.text.read(entry, data->rbuffer);
1da177e4 379 }
4adb7bcb 380 return 0;
1da177e4
LT
381}
382
4adb7bcb 383static int snd_info_text_entry_open(struct inode *inode, struct file *file)
1da177e4 384{
4adb7bcb 385 struct snd_info_entry *entry = PDE_DATA(inode);
24c1f931 386 struct snd_info_private_data *data;
4adb7bcb 387 int err;
1da177e4 388
4adb7bcb
TI
389 mutex_lock(&info_mutex);
390 err = alloc_info_private(entry, &data);
391 if (err < 0)
392 goto unlock;
393
394 data->rbuffer = kzalloc(sizeof(*data->rbuffer), GFP_KERNEL);
395 if (!data->rbuffer) {
396 err = -ENOMEM;
397 goto error;
398 }
399 if (entry->size)
400 err = single_open_size(file, snd_info_seq_show, data,
401 entry->size);
402 else
403 err = single_open(file, snd_info_seq_show, data);
404 if (err < 0)
405 goto error;
406 mutex_unlock(&info_mutex);
407 return 0;
408
409 error:
410 kfree(data->rbuffer);
411 kfree(data);
412 module_put(entry->module);
413 unlock:
414 mutex_unlock(&info_mutex);
415 return err;
416}
417
418static int snd_info_text_entry_release(struct inode *inode, struct file *file)
419{
420 struct seq_file *m = file->private_data;
421 struct snd_info_private_data *data = m->private;
422 struct snd_info_entry *entry = data->entry;
423
424 if (data->wbuffer && entry->c.text.write)
425 entry->c.text.write(entry, data->wbuffer);
426
427 single_release(inode, file);
428 kfree(data->rbuffer);
429 if (data->wbuffer) {
430 kfree(data->wbuffer->buffer);
431 kfree(data->wbuffer);
1da177e4 432 }
4adb7bcb
TI
433
434 module_put(entry->module);
435 kfree(data);
436 return 0;
1da177e4
LT
437}
438
4adb7bcb 439static const struct file_operations snd_info_text_entry_ops =
1da177e4 440{
d99e9889 441 .owner = THIS_MODULE,
4adb7bcb
TI
442 .open = snd_info_text_entry_open,
443 .release = snd_info_text_entry_release,
444 .write = snd_info_text_entry_write,
445 .llseek = seq_lseek,
446 .read = seq_read,
1da177e4
LT
447};
448
886364f6
TI
449static struct snd_info_entry *create_subdir(struct module *mod,
450 const char *name)
451{
452 struct snd_info_entry *entry;
453
454 entry = snd_info_create_module_entry(mod, name, NULL);
455 if (!entry)
456 return NULL;
457 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
458 if (snd_info_register(entry) < 0) {
459 snd_info_free_entry(entry);
460 return NULL;
461 }
462 return entry;
463}
464
1da177e4
LT
465int __init snd_info_init(void)
466{
467 struct proc_dir_entry *p;
468
e55d92b9 469 p = proc_mkdir("asound", NULL);
1da177e4
LT
470 if (p == NULL)
471 return -ENOMEM;
472 snd_proc_root = p;
473#ifdef CONFIG_SND_OSSEMUL
886364f6
TI
474 snd_oss_root = create_subdir(THIS_MODULE, "oss");
475 if (!snd_oss_root)
476 goto error;
1da177e4 477#endif
8eeaa2f9 478#if IS_ENABLED(CONFIG_SND_SEQUENCER)
886364f6
TI
479 snd_seq_root = create_subdir(THIS_MODULE, "seq");
480 if (!snd_seq_root)
481 goto error;
1da177e4
LT
482#endif
483 snd_info_version_init();
1da177e4
LT
484 snd_minor_info_init();
485 snd_minor_info_oss_init();
486 snd_card_info_init();
487 return 0;
886364f6
TI
488
489 error:
490#ifdef CONFIG_SND_OSSEMUL
491 snd_info_free_entry(snd_oss_root);
492#endif
493 proc_remove(snd_proc_root);
494 return -ENOMEM;
1da177e4
LT
495}
496
497int __exit snd_info_done(void)
498{
499 snd_card_info_done();
500 snd_minor_info_oss_done();
501 snd_minor_info_done();
1da177e4
LT
502 snd_info_version_done();
503 if (snd_proc_root) {
8eeaa2f9 504#if IS_ENABLED(CONFIG_SND_SEQUENCER)
746d4a02 505 snd_info_free_entry(snd_seq_root);
1da177e4
LT
506#endif
507#ifdef CONFIG_SND_OSSEMUL
746d4a02 508 snd_info_free_entry(snd_oss_root);
1da177e4 509#endif
a8ca16ea 510 proc_remove(snd_proc_root);
1da177e4
LT
511 }
512 return 0;
513}
514
515/*
516
517 */
518
519
520/*
521 * create a card proc file
522 * called from init.c
523 */
24c1f931 524int snd_info_card_create(struct snd_card *card)
1da177e4
LT
525{
526 char str[8];
24c1f931 527 struct snd_info_entry *entry;
1da177e4 528
7eaa943c
TI
529 if (snd_BUG_ON(!card))
530 return -ENXIO;
1da177e4
LT
531
532 sprintf(str, "card%i", card->number);
886364f6
TI
533 entry = create_subdir(card->module, str);
534 if (!entry)
1da177e4 535 return -ENOMEM;
1da177e4
LT
536 card->proc_root = entry;
537 return 0;
538}
539
540/*
541 * register the card proc file
542 * called from init.c
543 */
24c1f931 544int snd_info_card_register(struct snd_card *card)
1da177e4
LT
545{
546 struct proc_dir_entry *p;
547
7eaa943c
TI
548 if (snd_BUG_ON(!card))
549 return -ENXIO;
1da177e4
LT
550
551 if (!strcmp(card->id, card->proc_root->name))
552 return 0;
553
554 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
555 if (p == NULL)
556 return -ENOMEM;
557 card->proc_root_link = p;
558 return 0;
559}
560
c2eb9c4e
JK
561/*
562 * called on card->id change
563 */
564void snd_info_card_id_change(struct snd_card *card)
565{
566 mutex_lock(&info_mutex);
567 if (card->proc_root_link) {
a8ca16ea 568 proc_remove(card->proc_root_link);
c2eb9c4e
JK
569 card->proc_root_link = NULL;
570 }
571 if (strcmp(card->id, card->proc_root->name))
572 card->proc_root_link = proc_symlink(card->id,
573 snd_proc_root,
574 card->proc_root->name);
575 mutex_unlock(&info_mutex);
576}
577
1da177e4
LT
578/*
579 * de-register the card proc file
580 * called from init.c
581 */
746d4a02 582void snd_info_card_disconnect(struct snd_card *card)
1da177e4 583{
7eaa943c
TI
584 if (!card)
585 return;
746d4a02 586 mutex_lock(&info_mutex);
a8ca16ea
DH
587 proc_remove(card->proc_root_link);
588 card->proc_root_link = NULL;
746d4a02
TI
589 if (card->proc_root)
590 snd_info_disconnect(card->proc_root);
591 mutex_unlock(&info_mutex);
592}
593
594/*
595 * release the card proc file resources
596 * called from init.c
597 */
598int snd_info_card_free(struct snd_card *card)
599{
7eaa943c
TI
600 if (!card)
601 return 0;
746d4a02
TI
602 snd_info_free_entry(card->proc_root);
603 card->proc_root = NULL;
1da177e4
LT
604 return 0;
605}
606
607
608/**
609 * snd_info_get_line - read one line from the procfs buffer
610 * @buffer: the procfs buffer
611 * @line: the buffer to store
ddc64b27 612 * @len: the max. buffer size
1da177e4
LT
613 *
614 * Reads one line from the buffer and stores the string.
615 *
eb7c06e8 616 * Return: Zero if successful, or 1 if error or EOF.
1da177e4 617 */
24c1f931 618int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
1da177e4
LT
619{
620 int c = -1;
621
0bc0ec90
TI
622 if (snd_BUG_ON(!buffer || !buffer->buffer))
623 return 1;
1da177e4
LT
624 if (len <= 0 || buffer->stop || buffer->error)
625 return 1;
0bc0ec90 626 while (!buffer->stop) {
7e4eeec8 627 c = buffer->buffer[buffer->curr++];
0bc0ec90 628 if (buffer->curr >= buffer->size)
1da177e4 629 buffer->stop = 1;
0bc0ec90 630 if (c == '\n')
1da177e4 631 break;
ddc64b27 632 if (len > 1) {
0bc0ec90
TI
633 len--;
634 *line++ = c;
1da177e4
LT
635 }
636 }
1da177e4
LT
637 *line = '\0';
638 return 0;
639}
640
c0d3fb39
TI
641EXPORT_SYMBOL(snd_info_get_line);
642
1da177e4 643/**
856def8a 644 * snd_info_get_str - parse a string token
1da177e4
LT
645 * @dest: the buffer to store the string token
646 * @src: the original string
647 * @len: the max. length of token - 1
648 *
649 * Parses the original string and copy a token to the given
650 * string buffer.
651 *
eb7c06e8 652 * Return: The updated pointer of the original string so that
1da177e4
LT
653 * it can be used for the next call.
654 */
4f7454a9 655const char *snd_info_get_str(char *dest, const char *src, int len)
1da177e4
LT
656{
657 int c;
658
659 while (*src == ' ' || *src == '\t')
660 src++;
661 if (*src == '"' || *src == '\'') {
662 c = *src++;
663 while (--len > 0 && *src && *src != c) {
664 *dest++ = *src++;
665 }
666 if (*src == c)
667 src++;
668 } else {
669 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
670 *dest++ = *src++;
671 }
672 }
673 *dest = 0;
674 while (*src == ' ' || *src == '\t')
675 src++;
676 return src;
677}
678
c0d3fb39
TI
679EXPORT_SYMBOL(snd_info_get_str);
680
1da177e4
LT
681/**
682 * snd_info_create_entry - create an info entry
683 * @name: the proc file name
684 *
685 * Creates an info entry with the given file name and initializes as
686 * the default state.
687 *
688 * Usually called from other functions such as
689 * snd_info_create_card_entry().
690 *
eb7c06e8 691 * Return: The pointer of the new instance, or %NULL on failure.
1da177e4 692 */
24c1f931 693static struct snd_info_entry *snd_info_create_entry(const char *name)
1da177e4 694{
24c1f931 695 struct snd_info_entry *entry;
ca2c0966 696 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1da177e4
LT
697 if (entry == NULL)
698 return NULL;
543537bd 699 entry->name = kstrdup(name, GFP_KERNEL);
1da177e4
LT
700 if (entry->name == NULL) {
701 kfree(entry);
702 return NULL;
703 }
704 entry->mode = S_IFREG | S_IRUGO;
705 entry->content = SNDRV_INFO_CONTENT_TEXT;
1a60d4c5 706 mutex_init(&entry->access);
746d4a02
TI
707 INIT_LIST_HEAD(&entry->children);
708 INIT_LIST_HEAD(&entry->list);
1da177e4
LT
709 return entry;
710}
711
712/**
713 * snd_info_create_module_entry - create an info entry for the given module
714 * @module: the module pointer
715 * @name: the file name
716 * @parent: the parent directory
717 *
718 * Creates a new info entry and assigns it to the given module.
719 *
eb7c06e8 720 * Return: The pointer of the new instance, or %NULL on failure.
1da177e4 721 */
24c1f931 722struct snd_info_entry *snd_info_create_module_entry(struct module * module,
1da177e4 723 const char *name,
24c1f931 724 struct snd_info_entry *parent)
1da177e4 725{
24c1f931 726 struct snd_info_entry *entry = snd_info_create_entry(name);
1da177e4
LT
727 if (entry) {
728 entry->module = module;
729 entry->parent = parent;
730 }
731 return entry;
732}
733
c0d3fb39
TI
734EXPORT_SYMBOL(snd_info_create_module_entry);
735
1da177e4
LT
736/**
737 * snd_info_create_card_entry - create an info entry for the given card
738 * @card: the card instance
739 * @name: the file name
740 * @parent: the parent directory
741 *
742 * Creates a new info entry and assigns it to the given card.
743 *
eb7c06e8 744 * Return: The pointer of the new instance, or %NULL on failure.
1da177e4 745 */
24c1f931 746struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
1da177e4 747 const char *name,
24c1f931 748 struct snd_info_entry * parent)
1da177e4 749{
24c1f931 750 struct snd_info_entry *entry = snd_info_create_entry(name);
1da177e4
LT
751 if (entry) {
752 entry->module = card->module;
753 entry->card = card;
754 entry->parent = parent;
755 }
756 return entry;
757}
758
c0d3fb39
TI
759EXPORT_SYMBOL(snd_info_create_card_entry);
760
746d4a02 761static void snd_info_disconnect(struct snd_info_entry *entry)
1da177e4 762{
746d4a02 763 struct list_head *p, *n;
1da177e4 764
746d4a02
TI
765 list_for_each_safe(p, n, &entry->children) {
766 snd_info_disconnect(list_entry(p, struct snd_info_entry, list));
767 }
768
769 if (! entry->p)
770 return;
771 list_del_init(&entry->list);
a8ca16ea 772 proc_remove(entry->p);
746d4a02 773 entry->p = NULL;
1da177e4
LT
774}
775
746d4a02 776static int snd_info_dev_free_entry(struct snd_device *device)
1da177e4 777{
24c1f931 778 struct snd_info_entry *entry = device->device_data;
746d4a02 779 snd_info_free_entry(entry);
1da177e4
LT
780 return 0;
781}
782
746d4a02 783static int snd_info_dev_register_entry(struct snd_device *device)
1da177e4 784{
24c1f931 785 struct snd_info_entry *entry = device->device_data;
746d4a02 786 return snd_info_register(entry);
1da177e4
LT
787}
788
789/**
790 * snd_card_proc_new - create an info entry for the given card
791 * @card: the card instance
792 * @name: the file name
793 * @entryp: the pointer to store the new info entry
794 *
795 * Creates a new info entry and assigns it to the given card.
796 * Unlike snd_info_create_card_entry(), this function registers the
797 * info entry as an ALSA device component, so that it can be
798 * unregistered/released without explicit call.
799 * Also, you don't have to register this entry via snd_info_register(),
800 * since this will be registered by snd_card_register() automatically.
801 *
802 * The parent is assumed as card->proc_root.
803 *
804 * For releasing this entry, use snd_device_free() instead of
805 * snd_info_free_entry().
806 *
eb7c06e8 807 * Return: Zero if successful, or a negative error code on failure.
1da177e4 808 */
24c1f931
TI
809int snd_card_proc_new(struct snd_card *card, const char *name,
810 struct snd_info_entry **entryp)
1da177e4 811{
24c1f931 812 static struct snd_device_ops ops = {
1da177e4
LT
813 .dev_free = snd_info_dev_free_entry,
814 .dev_register = snd_info_dev_register_entry,
746d4a02 815 /* disconnect is done via snd_info_card_disconnect() */
1da177e4 816 };
24c1f931 817 struct snd_info_entry *entry;
1da177e4
LT
818 int err;
819
820 entry = snd_info_create_card_entry(card, name, card->proc_root);
821 if (! entry)
822 return -ENOMEM;
823 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
824 snd_info_free_entry(entry);
825 return err;
826 }
827 if (entryp)
828 *entryp = entry;
829 return 0;
830}
831
c0d3fb39
TI
832EXPORT_SYMBOL(snd_card_proc_new);
833
1da177e4
LT
834/**
835 * snd_info_free_entry - release the info entry
836 * @entry: the info entry
837 *
838 * Releases the info entry. Don't call this after registered.
839 */
24c1f931 840void snd_info_free_entry(struct snd_info_entry * entry)
1da177e4
LT
841{
842 if (entry == NULL)
843 return;
746d4a02
TI
844 if (entry->p) {
845 mutex_lock(&info_mutex);
846 snd_info_disconnect(entry);
847 mutex_unlock(&info_mutex);
848 }
1da177e4
LT
849 kfree(entry->name);
850 if (entry->private_free)
851 entry->private_free(entry);
852 kfree(entry);
853}
854
c0d3fb39
TI
855EXPORT_SYMBOL(snd_info_free_entry);
856
1da177e4
LT
857/**
858 * snd_info_register - register the info entry
859 * @entry: the info entry
860 *
861 * Registers the proc info entry.
862 *
eb7c06e8 863 * Return: Zero if successful, or a negative error code on failure.
1da177e4 864 */
24c1f931 865int snd_info_register(struct snd_info_entry * entry)
1da177e4
LT
866{
867 struct proc_dir_entry *root, *p = NULL;
868
7eaa943c
TI
869 if (snd_BUG_ON(!entry))
870 return -ENXIO;
1da177e4 871 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
1a60d4c5 872 mutex_lock(&info_mutex);
aee0c612
AV
873 if (S_ISDIR(entry->mode)) {
874 p = proc_mkdir_mode(entry->name, entry->mode, root);
875 if (!p) {
876 mutex_unlock(&info_mutex);
877 return -ENOMEM;
878 }
879 } else {
4adb7bcb
TI
880 const struct file_operations *ops;
881 if (entry->content == SNDRV_INFO_CONTENT_DATA)
882 ops = &snd_info_entry_operations;
883 else
884 ops = &snd_info_text_entry_ops;
aee0c612 885 p = proc_create_data(entry->name, entry->mode, root,
4adb7bcb 886 ops, entry);
aee0c612
AV
887 if (!p) {
888 mutex_unlock(&info_mutex);
889 return -ENOMEM;
890 }
271a15ea 891 proc_set_size(p, entry->size);
1da177e4 892 }
1da177e4 893 entry->p = p;
746d4a02
TI
894 if (entry->parent)
895 list_add_tail(&entry->list, &entry->parent->children);
1a60d4c5 896 mutex_unlock(&info_mutex);
1da177e4
LT
897 return 0;
898}
899
c0d3fb39
TI
900EXPORT_SYMBOL(snd_info_register);
901
1da177e4
LT
902/*
903
904 */
905
6581f4e7 906static struct snd_info_entry *snd_info_version_entry;
1da177e4 907
24c1f931 908static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
1da177e4
LT
909{
910 snd_iprintf(buffer,
42662748
JK
911 "Advanced Linux Sound Architecture Driver Version k%s.\n",
912 init_utsname()->release);
1da177e4
LT
913}
914
915static int __init snd_info_version_init(void)
916{
24c1f931 917 struct snd_info_entry *entry;
1da177e4
LT
918
919 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
920 if (entry == NULL)
921 return -ENOMEM;
1da177e4
LT
922 entry->c.text.read = snd_info_version_read;
923 if (snd_info_register(entry) < 0) {
924 snd_info_free_entry(entry);
925 return -ENOMEM;
926 }
927 snd_info_version_entry = entry;
928 return 0;
929}
930
931static int __exit snd_info_version_done(void)
932{
746d4a02 933 snd_info_free_entry(snd_info_version_entry);
1da177e4
LT
934 return 0;
935}
936
937#endif /* CONFIG_PROC_FS */
This page took 0.790968 seconds and 5 git commands to generate.