ALSA: core: Use seq_file for text proc file reads
[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
1da177e4
LT
449int __init snd_info_init(void)
450{
451 struct proc_dir_entry *p;
452
e55d92b9 453 p = proc_mkdir("asound", NULL);
1da177e4
LT
454 if (p == NULL)
455 return -ENOMEM;
456 snd_proc_root = p;
457#ifdef CONFIG_SND_OSSEMUL
458 {
24c1f931 459 struct snd_info_entry *entry;
1da177e4
LT
460 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
461 return -ENOMEM;
462 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
463 if (snd_info_register(entry) < 0) {
464 snd_info_free_entry(entry);
465 return -ENOMEM;
466 }
467 snd_oss_root = entry;
468 }
469#endif
8eeaa2f9 470#if IS_ENABLED(CONFIG_SND_SEQUENCER)
1da177e4 471 {
24c1f931 472 struct snd_info_entry *entry;
1da177e4
LT
473 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL)
474 return -ENOMEM;
475 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
476 if (snd_info_register(entry) < 0) {
477 snd_info_free_entry(entry);
478 return -ENOMEM;
479 }
480 snd_seq_root = entry;
481 }
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;
488}
489
490int __exit snd_info_done(void)
491{
492 snd_card_info_done();
493 snd_minor_info_oss_done();
494 snd_minor_info_done();
1da177e4
LT
495 snd_info_version_done();
496 if (snd_proc_root) {
8eeaa2f9 497#if IS_ENABLED(CONFIG_SND_SEQUENCER)
746d4a02 498 snd_info_free_entry(snd_seq_root);
1da177e4
LT
499#endif
500#ifdef CONFIG_SND_OSSEMUL
746d4a02 501 snd_info_free_entry(snd_oss_root);
1da177e4 502#endif
a8ca16ea 503 proc_remove(snd_proc_root);
1da177e4
LT
504 }
505 return 0;
506}
507
508/*
509
510 */
511
512
513/*
514 * create a card proc file
515 * called from init.c
516 */
24c1f931 517int snd_info_card_create(struct snd_card *card)
1da177e4
LT
518{
519 char str[8];
24c1f931 520 struct snd_info_entry *entry;
1da177e4 521
7eaa943c
TI
522 if (snd_BUG_ON(!card))
523 return -ENXIO;
1da177e4
LT
524
525 sprintf(str, "card%i", card->number);
526 if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
527 return -ENOMEM;
528 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
529 if (snd_info_register(entry) < 0) {
530 snd_info_free_entry(entry);
531 return -ENOMEM;
532 }
533 card->proc_root = entry;
534 return 0;
535}
536
537/*
538 * register the card proc file
539 * called from init.c
540 */
24c1f931 541int snd_info_card_register(struct snd_card *card)
1da177e4
LT
542{
543 struct proc_dir_entry *p;
544
7eaa943c
TI
545 if (snd_BUG_ON(!card))
546 return -ENXIO;
1da177e4
LT
547
548 if (!strcmp(card->id, card->proc_root->name))
549 return 0;
550
551 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
552 if (p == NULL)
553 return -ENOMEM;
554 card->proc_root_link = p;
555 return 0;
556}
557
c2eb9c4e
JK
558/*
559 * called on card->id change
560 */
561void snd_info_card_id_change(struct snd_card *card)
562{
563 mutex_lock(&info_mutex);
564 if (card->proc_root_link) {
a8ca16ea 565 proc_remove(card->proc_root_link);
c2eb9c4e
JK
566 card->proc_root_link = NULL;
567 }
568 if (strcmp(card->id, card->proc_root->name))
569 card->proc_root_link = proc_symlink(card->id,
570 snd_proc_root,
571 card->proc_root->name);
572 mutex_unlock(&info_mutex);
573}
574
1da177e4
LT
575/*
576 * de-register the card proc file
577 * called from init.c
578 */
746d4a02 579void snd_info_card_disconnect(struct snd_card *card)
1da177e4 580{
7eaa943c
TI
581 if (!card)
582 return;
746d4a02 583 mutex_lock(&info_mutex);
a8ca16ea
DH
584 proc_remove(card->proc_root_link);
585 card->proc_root_link = NULL;
746d4a02
TI
586 if (card->proc_root)
587 snd_info_disconnect(card->proc_root);
588 mutex_unlock(&info_mutex);
589}
590
591/*
592 * release the card proc file resources
593 * called from init.c
594 */
595int snd_info_card_free(struct snd_card *card)
596{
7eaa943c
TI
597 if (!card)
598 return 0;
746d4a02
TI
599 snd_info_free_entry(card->proc_root);
600 card->proc_root = NULL;
1da177e4
LT
601 return 0;
602}
603
604
605/**
606 * snd_info_get_line - read one line from the procfs buffer
607 * @buffer: the procfs buffer
608 * @line: the buffer to store
ddc64b27 609 * @len: the max. buffer size
1da177e4
LT
610 *
611 * Reads one line from the buffer and stores the string.
612 *
eb7c06e8 613 * Return: Zero if successful, or 1 if error or EOF.
1da177e4 614 */
24c1f931 615int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
1da177e4
LT
616{
617 int c = -1;
618
0bc0ec90
TI
619 if (snd_BUG_ON(!buffer || !buffer->buffer))
620 return 1;
1da177e4
LT
621 if (len <= 0 || buffer->stop || buffer->error)
622 return 1;
0bc0ec90 623 while (!buffer->stop) {
7e4eeec8 624 c = buffer->buffer[buffer->curr++];
0bc0ec90 625 if (buffer->curr >= buffer->size)
1da177e4 626 buffer->stop = 1;
0bc0ec90 627 if (c == '\n')
1da177e4 628 break;
ddc64b27 629 if (len > 1) {
0bc0ec90
TI
630 len--;
631 *line++ = c;
1da177e4
LT
632 }
633 }
1da177e4
LT
634 *line = '\0';
635 return 0;
636}
637
c0d3fb39
TI
638EXPORT_SYMBOL(snd_info_get_line);
639
1da177e4 640/**
856def8a 641 * snd_info_get_str - parse a string token
1da177e4
LT
642 * @dest: the buffer to store the string token
643 * @src: the original string
644 * @len: the max. length of token - 1
645 *
646 * Parses the original string and copy a token to the given
647 * string buffer.
648 *
eb7c06e8 649 * Return: The updated pointer of the original string so that
1da177e4
LT
650 * it can be used for the next call.
651 */
4f7454a9 652const char *snd_info_get_str(char *dest, const char *src, int len)
1da177e4
LT
653{
654 int c;
655
656 while (*src == ' ' || *src == '\t')
657 src++;
658 if (*src == '"' || *src == '\'') {
659 c = *src++;
660 while (--len > 0 && *src && *src != c) {
661 *dest++ = *src++;
662 }
663 if (*src == c)
664 src++;
665 } else {
666 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
667 *dest++ = *src++;
668 }
669 }
670 *dest = 0;
671 while (*src == ' ' || *src == '\t')
672 src++;
673 return src;
674}
675
c0d3fb39
TI
676EXPORT_SYMBOL(snd_info_get_str);
677
1da177e4
LT
678/**
679 * snd_info_create_entry - create an info entry
680 * @name: the proc file name
681 *
682 * Creates an info entry with the given file name and initializes as
683 * the default state.
684 *
685 * Usually called from other functions such as
686 * snd_info_create_card_entry().
687 *
eb7c06e8 688 * Return: The pointer of the new instance, or %NULL on failure.
1da177e4 689 */
24c1f931 690static struct snd_info_entry *snd_info_create_entry(const char *name)
1da177e4 691{
24c1f931 692 struct snd_info_entry *entry;
ca2c0966 693 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1da177e4
LT
694 if (entry == NULL)
695 return NULL;
543537bd 696 entry->name = kstrdup(name, GFP_KERNEL);
1da177e4
LT
697 if (entry->name == NULL) {
698 kfree(entry);
699 return NULL;
700 }
701 entry->mode = S_IFREG | S_IRUGO;
702 entry->content = SNDRV_INFO_CONTENT_TEXT;
1a60d4c5 703 mutex_init(&entry->access);
746d4a02
TI
704 INIT_LIST_HEAD(&entry->children);
705 INIT_LIST_HEAD(&entry->list);
1da177e4
LT
706 return entry;
707}
708
709/**
710 * snd_info_create_module_entry - create an info entry for the given module
711 * @module: the module pointer
712 * @name: the file name
713 * @parent: the parent directory
714 *
715 * Creates a new info entry and assigns it to the given module.
716 *
eb7c06e8 717 * Return: The pointer of the new instance, or %NULL on failure.
1da177e4 718 */
24c1f931 719struct snd_info_entry *snd_info_create_module_entry(struct module * module,
1da177e4 720 const char *name,
24c1f931 721 struct snd_info_entry *parent)
1da177e4 722{
24c1f931 723 struct snd_info_entry *entry = snd_info_create_entry(name);
1da177e4
LT
724 if (entry) {
725 entry->module = module;
726 entry->parent = parent;
727 }
728 return entry;
729}
730
c0d3fb39
TI
731EXPORT_SYMBOL(snd_info_create_module_entry);
732
1da177e4
LT
733/**
734 * snd_info_create_card_entry - create an info entry for the given card
735 * @card: the card instance
736 * @name: the file name
737 * @parent: the parent directory
738 *
739 * Creates a new info entry and assigns it to the given card.
740 *
eb7c06e8 741 * Return: The pointer of the new instance, or %NULL on failure.
1da177e4 742 */
24c1f931 743struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
1da177e4 744 const char *name,
24c1f931 745 struct snd_info_entry * parent)
1da177e4 746{
24c1f931 747 struct snd_info_entry *entry = snd_info_create_entry(name);
1da177e4
LT
748 if (entry) {
749 entry->module = card->module;
750 entry->card = card;
751 entry->parent = parent;
752 }
753 return entry;
754}
755
c0d3fb39
TI
756EXPORT_SYMBOL(snd_info_create_card_entry);
757
746d4a02 758static void snd_info_disconnect(struct snd_info_entry *entry)
1da177e4 759{
746d4a02
TI
760 struct list_head *p, *n;
761 struct proc_dir_entry *root;
1da177e4 762
746d4a02
TI
763 list_for_each_safe(p, n, &entry->children) {
764 snd_info_disconnect(list_entry(p, struct snd_info_entry, list));
765 }
766
767 if (! entry->p)
768 return;
769 list_del_init(&entry->list);
770 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
7eaa943c 771 snd_BUG_ON(!root);
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.846105 seconds and 5 git commands to generate.