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