1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Information interface for ALSA driver
7 #include <linux/init.h>
8 #include <linux/time.h>
10 #include <linux/slab.h>
11 #include <linux/string.h>
12 #include <linux/module.h>
13 #include <sound/core.h>
14 #include <sound/minors.h>
15 #include <sound/info.h>
16 #include <linux/utsname.h>
17 #include <linux/proc_fs.h>
18 #include <linux/mutex.h>
20 int snd_info_check_reserved_words(const char *str)
22 static const char * const reserved[] =
37 const char * const *xstr = reserved;
40 if (!strcmp(*xstr, str))
44 if (!strncmp(str, "card", 4))
49 static DEFINE_MUTEX(info_mutex);
51 struct snd_info_private_data {
52 struct snd_info_buffer *rbuffer;
53 struct snd_info_buffer *wbuffer;
54 struct snd_info_entry *entry;
55 void *file_private_data;
58 static int snd_info_version_init(void);
59 static void snd_info_clear_entries(struct snd_info_entry *entry);
65 static struct snd_info_entry *snd_proc_root;
66 struct snd_info_entry *snd_seq_root;
67 EXPORT_SYMBOL(snd_seq_root);
69 #ifdef CONFIG_SND_OSSEMUL
70 struct snd_info_entry *snd_oss_root;
73 static int alloc_info_private(struct snd_info_entry *entry,
74 struct snd_info_private_data **ret)
76 struct snd_info_private_data *data;
78 if (!entry || !entry->p)
80 if (!try_module_get(entry->module))
82 data = kzalloc(sizeof(*data), GFP_KERNEL);
84 module_put(entry->module);
92 static bool valid_pos(loff_t pos, size_t count)
94 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
96 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
102 * file ops for binary proc files
104 static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
106 struct snd_info_private_data *data;
107 struct snd_info_entry *entry;
110 data = file->private_data;
112 guard(mutex)(&entry->access);
113 if (entry->c.ops->llseek)
114 return entry->c.ops->llseek(entry,
115 data->file_private_data,
123 offset += file->f_pos;
135 if (size && offset > size)
137 file->f_pos = offset;
141 static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
142 size_t count, loff_t * offset)
144 struct snd_info_private_data *data = file->private_data;
145 struct snd_info_entry *entry = data->entry;
150 if (!valid_pos(pos, count))
152 if (pos >= entry->size)
154 size = entry->size - pos;
155 size = min(count, size);
156 size = entry->c.ops->read(entry, data->file_private_data,
157 file, buffer, size, pos);
158 if ((ssize_t) size > 0)
159 *offset = pos + size;
163 static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
164 size_t count, loff_t * offset)
166 struct snd_info_private_data *data = file->private_data;
167 struct snd_info_entry *entry = data->entry;
172 if (!valid_pos(pos, count))
175 size_t maxsize = entry->size - pos;
176 count = min(count, maxsize);
177 size = entry->c.ops->write(entry, data->file_private_data,
178 file, buffer, count, pos);
181 *offset = pos + size;
185 static __poll_t snd_info_entry_poll(struct file *file, poll_table *wait)
187 struct snd_info_private_data *data = file->private_data;
188 struct snd_info_entry *entry = data->entry;
191 if (entry->c.ops->poll)
192 return entry->c.ops->poll(entry,
193 data->file_private_data,
195 if (entry->c.ops->read)
196 mask |= EPOLLIN | EPOLLRDNORM;
197 if (entry->c.ops->write)
198 mask |= EPOLLOUT | EPOLLWRNORM;
202 static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
205 struct snd_info_private_data *data = file->private_data;
206 struct snd_info_entry *entry = data->entry;
208 if (!entry->c.ops->ioctl)
210 return entry->c.ops->ioctl(entry, data->file_private_data,
214 static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
216 struct inode *inode = file_inode(file);
217 struct snd_info_private_data *data;
218 struct snd_info_entry *entry;
220 data = file->private_data;
224 if (!entry->c.ops->mmap)
226 return entry->c.ops->mmap(entry, data->file_private_data,
230 static int snd_info_entry_open(struct inode *inode, struct file *file)
232 struct snd_info_entry *entry = pde_data(inode);
233 struct snd_info_private_data *data;
236 guard(mutex)(&info_mutex);
237 err = alloc_info_private(entry, &data);
241 mode = file->f_flags & O_ACCMODE;
242 if (((mode == O_RDONLY || mode == O_RDWR) && !entry->c.ops->read) ||
243 ((mode == O_WRONLY || mode == O_RDWR) && !entry->c.ops->write)) {
248 if (entry->c.ops->open) {
249 err = entry->c.ops->open(entry, mode, &data->file_private_data);
254 file->private_data = data;
259 module_put(entry->module);
263 static int snd_info_entry_release(struct inode *inode, struct file *file)
265 struct snd_info_private_data *data = file->private_data;
266 struct snd_info_entry *entry = data->entry;
268 if (entry->c.ops->release)
269 entry->c.ops->release(entry, file->f_flags & O_ACCMODE,
270 data->file_private_data);
271 module_put(entry->module);
276 static const struct proc_ops snd_info_entry_operations =
278 .proc_lseek = snd_info_entry_llseek,
279 .proc_read = snd_info_entry_read,
280 .proc_write = snd_info_entry_write,
281 .proc_poll = snd_info_entry_poll,
282 .proc_ioctl = snd_info_entry_ioctl,
283 .proc_mmap = snd_info_entry_mmap,
284 .proc_open = snd_info_entry_open,
285 .proc_release = snd_info_entry_release,
289 * file ops for text proc files
291 static ssize_t snd_info_text_entry_write(struct file *file,
292 const char __user *buffer,
293 size_t count, loff_t *offset)
295 struct seq_file *m = file->private_data;
296 struct snd_info_private_data *data = m->private;
297 struct snd_info_entry *entry = data->entry;
298 struct snd_info_buffer *buf;
302 if (!entry->c.text.write)
305 if (!valid_pos(pos, count))
308 /* don't handle too large text inputs */
309 if (next > 16 * 1024)
311 guard(mutex)(&entry->access);
314 data->wbuffer = buf = kzalloc(sizeof(*buf), GFP_KERNEL);
318 if (next > buf->len) {
319 char *nbuf = kvzalloc(PAGE_ALIGN(next), GFP_KERNEL);
324 buf->len = PAGE_ALIGN(next);
326 if (copy_from_user(buf->buffer + pos, buffer, count))
333 static int snd_info_seq_show(struct seq_file *seq, void *p)
335 struct snd_info_private_data *data = seq->private;
336 struct snd_info_entry *entry = data->entry;
338 if (!entry->c.text.read) {
341 data->rbuffer->buffer = (char *)seq; /* XXX hack! */
342 entry->c.text.read(entry, data->rbuffer);
347 static int snd_info_text_entry_open(struct inode *inode, struct file *file)
349 struct snd_info_entry *entry = pde_data(inode);
350 struct snd_info_private_data *data;
353 guard(mutex)(&info_mutex);
354 err = alloc_info_private(entry, &data);
358 data->rbuffer = kzalloc(sizeof(*data->rbuffer), GFP_KERNEL);
359 if (!data->rbuffer) {
364 err = single_open_size(file, snd_info_seq_show, data,
367 err = single_open(file, snd_info_seq_show, data);
373 kfree(data->rbuffer);
375 module_put(entry->module);
379 static int snd_info_text_entry_release(struct inode *inode, struct file *file)
381 struct seq_file *m = file->private_data;
382 struct snd_info_private_data *data = m->private;
383 struct snd_info_entry *entry = data->entry;
385 if (data->wbuffer && entry->c.text.write)
386 entry->c.text.write(entry, data->wbuffer);
388 single_release(inode, file);
389 kfree(data->rbuffer);
391 kvfree(data->wbuffer->buffer);
392 kfree(data->wbuffer);
395 module_put(entry->module);
400 static const struct proc_ops snd_info_text_entry_ops =
402 .proc_open = snd_info_text_entry_open,
403 .proc_release = snd_info_text_entry_release,
404 .proc_write = snd_info_text_entry_write,
405 .proc_lseek = seq_lseek,
406 .proc_read = seq_read,
409 static struct snd_info_entry *create_subdir(struct module *mod,
412 struct snd_info_entry *entry;
414 entry = snd_info_create_module_entry(mod, name, NULL);
417 entry->mode = S_IFDIR | 0555;
418 if (snd_info_register(entry) < 0) {
419 snd_info_free_entry(entry);
425 static struct snd_info_entry *
426 snd_info_create_entry(const char *name, struct snd_info_entry *parent,
427 struct module *module);
429 int __init snd_info_init(void)
431 snd_proc_root = snd_info_create_entry("asound", NULL, THIS_MODULE);
434 snd_proc_root->mode = S_IFDIR | 0555;
435 snd_proc_root->p = proc_mkdir("asound", NULL);
436 if (!snd_proc_root->p)
438 #ifdef CONFIG_SND_OSSEMUL
439 snd_oss_root = create_subdir(THIS_MODULE, "oss");
443 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
444 snd_seq_root = create_subdir(THIS_MODULE, "seq");
448 if (snd_info_version_init() < 0 ||
449 snd_minor_info_init() < 0 ||
450 snd_minor_info_oss_init() < 0 ||
451 snd_card_info_init() < 0 ||
452 snd_info_minor_register() < 0)
457 snd_info_free_entry(snd_proc_root);
461 int __exit snd_info_done(void)
463 snd_info_free_entry(snd_proc_root);
467 static void snd_card_id_read(struct snd_info_entry *entry,
468 struct snd_info_buffer *buffer)
470 struct snd_card *card = entry->private_data;
472 snd_iprintf(buffer, "%s\n", card->id);
476 * create a card proc file
479 int snd_info_card_create(struct snd_card *card)
482 struct snd_info_entry *entry;
484 if (snd_BUG_ON(!card))
487 sprintf(str, "card%i", card->number);
488 entry = create_subdir(card->module, str);
491 card->proc_root = entry;
493 return snd_card_ro_proc_new(card, "id", card, snd_card_id_read);
497 * register the card proc file
499 * can be called multiple times for reinitialization
501 int snd_info_card_register(struct snd_card *card)
503 struct proc_dir_entry *p;
506 if (snd_BUG_ON(!card))
509 err = snd_info_register(card->proc_root);
513 if (!strcmp(card->id, card->proc_root->name))
516 if (card->proc_root_link)
518 p = proc_symlink(card->id, snd_proc_root->p, card->proc_root->name);
521 card->proc_root_link = p;
526 * called on card->id change
528 void snd_info_card_id_change(struct snd_card *card)
530 guard(mutex)(&info_mutex);
531 if (card->proc_root_link) {
532 proc_remove(card->proc_root_link);
533 card->proc_root_link = NULL;
535 if (strcmp(card->id, card->proc_root->name))
536 card->proc_root_link = proc_symlink(card->id,
538 card->proc_root->name);
542 * de-register the card proc file
545 void snd_info_card_disconnect(struct snd_card *card)
550 proc_remove(card->proc_root_link);
552 proc_remove(card->proc_root->p);
554 guard(mutex)(&info_mutex);
556 snd_info_clear_entries(card->proc_root);
557 card->proc_root_link = NULL;
558 card->proc_root = NULL;
562 * release the card proc file resources
565 int snd_info_card_free(struct snd_card *card)
569 snd_info_free_entry(card->proc_root);
570 card->proc_root = NULL;
576 * snd_info_get_line - read one line from the procfs buffer
577 * @buffer: the procfs buffer
578 * @line: the buffer to store
579 * @len: the max. buffer size
581 * Reads one line from the buffer and stores the string.
583 * Return: Zero if successful, or 1 if error or EOF.
585 int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
589 if (snd_BUG_ON(!buffer))
593 if (len <= 0 || buffer->stop || buffer->error)
595 while (!buffer->stop) {
596 c = buffer->buffer[buffer->curr++];
597 if (buffer->curr >= buffer->size)
609 EXPORT_SYMBOL(snd_info_get_line);
612 * snd_info_get_str - parse a string token
613 * @dest: the buffer to store the string token
614 * @src: the original string
615 * @len: the max. length of token - 1
617 * Parses the original string and copy a token to the given
620 * Return: The updated pointer of the original string so that
621 * it can be used for the next call.
623 const char *snd_info_get_str(char *dest, const char *src, int len)
627 while (*src == ' ' || *src == '\t')
629 if (*src == '"' || *src == '\'') {
631 while (--len > 0 && *src && *src != c) {
637 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
642 while (*src == ' ' || *src == '\t')
646 EXPORT_SYMBOL(snd_info_get_str);
649 * snd_info_create_entry - create an info entry
650 * @name: the proc file name
651 * @parent: the parent directory
653 * Creates an info entry with the given file name and initializes as
656 * Usually called from other functions such as
657 * snd_info_create_card_entry().
659 * Return: The pointer of the new instance, or %NULL on failure.
661 static struct snd_info_entry *
662 snd_info_create_entry(const char *name, struct snd_info_entry *parent,
663 struct module *module)
665 struct snd_info_entry *entry;
666 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
669 entry->name = kstrdup(name, GFP_KERNEL);
670 if (entry->name == NULL) {
674 entry->mode = S_IFREG | 0444;
675 entry->content = SNDRV_INFO_CONTENT_TEXT;
676 mutex_init(&entry->access);
677 INIT_LIST_HEAD(&entry->children);
678 INIT_LIST_HEAD(&entry->list);
679 entry->parent = parent;
680 entry->module = module;
682 guard(mutex)(&parent->access);
683 list_add_tail(&entry->list, &parent->children);
689 * snd_info_create_module_entry - create an info entry for the given module
690 * @module: the module pointer
691 * @name: the file name
692 * @parent: the parent directory
694 * Creates a new info entry and assigns it to the given module.
696 * Return: The pointer of the new instance, or %NULL on failure.
698 struct snd_info_entry *snd_info_create_module_entry(struct module * module,
700 struct snd_info_entry *parent)
703 parent = snd_proc_root;
704 return snd_info_create_entry(name, parent, module);
706 EXPORT_SYMBOL(snd_info_create_module_entry);
709 * snd_info_create_card_entry - create an info entry for the given card
710 * @card: the card instance
711 * @name: the file name
712 * @parent: the parent directory
714 * Creates a new info entry and assigns it to the given card.
716 * Return: The pointer of the new instance, or %NULL on failure.
718 struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
720 struct snd_info_entry * parent)
723 parent = card->proc_root;
724 return snd_info_create_entry(name, parent, card->module);
726 EXPORT_SYMBOL(snd_info_create_card_entry);
728 static void snd_info_clear_entries(struct snd_info_entry *entry)
730 struct snd_info_entry *p;
734 list_for_each_entry(p, &entry->children, list)
735 snd_info_clear_entries(p);
740 * snd_info_free_entry - release the info entry
741 * @entry: the info entry
743 * Releases the info entry.
745 void snd_info_free_entry(struct snd_info_entry * entry)
747 struct snd_info_entry *p, *n;
752 proc_remove(entry->p);
753 guard(mutex)(&info_mutex);
754 snd_info_clear_entries(entry);
757 /* free all children at first */
758 list_for_each_entry_safe(p, n, &entry->children, list)
759 snd_info_free_entry(p);
763 guard(mutex)(&p->access);
764 list_del(&entry->list);
767 if (entry->private_free)
768 entry->private_free(entry);
771 EXPORT_SYMBOL(snd_info_free_entry);
773 static int __snd_info_register(struct snd_info_entry *entry)
775 struct proc_dir_entry *root, *p = NULL;
777 if (snd_BUG_ON(!entry))
779 root = entry->parent == NULL ? snd_proc_root->p : entry->parent->p;
780 guard(mutex)(&info_mutex);
781 if (entry->p || !root)
783 if (S_ISDIR(entry->mode)) {
784 p = proc_mkdir_mode(entry->name, entry->mode, root);
788 const struct proc_ops *ops;
789 if (entry->content == SNDRV_INFO_CONTENT_DATA)
790 ops = &snd_info_entry_operations;
792 ops = &snd_info_text_entry_ops;
793 p = proc_create_data(entry->name, entry->mode, root,
797 proc_set_size(p, entry->size);
804 * snd_info_register - register the info entry
805 * @entry: the info entry
807 * Registers the proc info entry.
808 * The all children entries are registered recursively.
810 * Return: Zero if successful, or a negative error code on failure.
812 int snd_info_register(struct snd_info_entry *entry)
814 struct snd_info_entry *p;
818 err = __snd_info_register(entry);
823 list_for_each_entry(p, &entry->children, list) {
824 err = snd_info_register(p);
831 EXPORT_SYMBOL(snd_info_register);
834 * snd_card_rw_proc_new - Create a read/write text proc file entry for the card
835 * @card: the card instance
836 * @name: the file name
837 * @private_data: the arbitrary private data
838 * @read: the read callback
839 * @write: the write callback, NULL for read-only
841 * This proc file entry will be registered via snd_card_register() call, and
842 * it will be removed automatically at the card removal, too.
844 * Return: zero if successful, or a negative error code
846 int snd_card_rw_proc_new(struct snd_card *card, const char *name,
848 void (*read)(struct snd_info_entry *,
849 struct snd_info_buffer *),
850 void (*write)(struct snd_info_entry *entry,
851 struct snd_info_buffer *buffer))
853 struct snd_info_entry *entry;
855 entry = snd_info_create_card_entry(card, name, card->proc_root);
858 snd_info_set_text_ops(entry, private_data, read);
861 entry->c.text.write = write;
865 EXPORT_SYMBOL_GPL(snd_card_rw_proc_new);
871 static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
874 "Advanced Linux Sound Architecture Driver Version k%s.\n",
875 init_utsname()->release);
878 static int __init snd_info_version_init(void)
880 struct snd_info_entry *entry;
882 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
885 entry->c.text.read = snd_info_version_read;
886 return snd_info_register(entry); /* freed in error path */