2 * Information interface for ALSA driver
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
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.
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.
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
22 #include <linux/init.h>
23 #include <linux/time.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/module.h>
28 #include <sound/core.h>
29 #include <sound/minors.h>
30 #include <sound/info.h>
31 #include <linux/utsname.h>
32 #include <linux/proc_fs.h>
33 #include <linux/mutex.h>
42 int snd_info_check_reserved_words(const char *str)
44 static char *reserved[] =
59 char **xstr = reserved;
62 if (!strcmp(*xstr, str))
66 if (!strncmp(str, "card", 4))
71 static DEFINE_MUTEX(info_mutex);
73 struct snd_info_private_data {
74 struct snd_info_buffer *rbuffer;
75 struct snd_info_buffer *wbuffer;
76 struct snd_info_entry *entry;
77 void *file_private_data;
80 static int snd_info_version_init(void);
81 static int snd_info_version_done(void);
82 static void snd_info_disconnect(struct snd_info_entry *entry);
88 static struct proc_dir_entry *snd_proc_root;
89 struct snd_info_entry *snd_seq_root;
90 EXPORT_SYMBOL(snd_seq_root);
92 #ifdef CONFIG_SND_OSSEMUL
93 struct snd_info_entry *snd_oss_root;
96 static int alloc_info_private(struct snd_info_entry *entry,
97 struct snd_info_private_data **ret)
99 struct snd_info_private_data *data;
101 if (!entry || !entry->p)
103 if (!try_module_get(entry->module))
105 data = kzalloc(sizeof(*data), GFP_KERNEL);
107 module_put(entry->module);
115 static bool valid_pos(loff_t pos, size_t count)
117 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
119 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
125 * file ops for binary proc files
127 static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
129 struct snd_info_private_data *data;
130 struct snd_info_entry *entry;
131 loff_t ret = -EINVAL, size;
133 data = file->private_data;
135 mutex_lock(&entry->access);
136 if (entry->c.ops->llseek) {
137 offset = entry->c.ops->llseek(entry,
138 data->file_private_data,
148 offset += file->f_pos;
160 if (size && offset > size)
162 file->f_pos = offset;
165 mutex_unlock(&entry->access);
169 static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
170 size_t count, loff_t * offset)
172 struct snd_info_private_data *data = file->private_data;
173 struct snd_info_entry *entry = data->entry;
178 if (!valid_pos(pos, count))
180 if (pos >= entry->size)
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);
186 if ((ssize_t) size > 0)
187 *offset = pos + size;
191 static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
192 size_t count, loff_t * offset)
194 struct snd_info_private_data *data = file->private_data;
195 struct snd_info_entry *entry = data->entry;
200 if (!valid_pos(pos, count))
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);
209 *offset = pos + size;
213 static unsigned int snd_info_entry_poll(struct file *file, poll_table *wait)
215 struct snd_info_private_data *data = file->private_data;
216 struct snd_info_entry *entry = data->entry;
217 unsigned int mask = 0;
219 if (entry->c.ops->poll)
220 return entry->c.ops->poll(entry,
221 data->file_private_data,
223 if (entry->c.ops->read)
224 mask |= POLLIN | POLLRDNORM;
225 if (entry->c.ops->write)
226 mask |= POLLOUT | POLLWRNORM;
230 static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
233 struct snd_info_private_data *data = file->private_data;
234 struct snd_info_entry *entry = data->entry;
236 if (!entry->c.ops->ioctl)
238 return entry->c.ops->ioctl(entry, data->file_private_data,
242 static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
244 struct inode *inode = file_inode(file);
245 struct snd_info_private_data *data;
246 struct snd_info_entry *entry;
248 data = file->private_data;
252 if (!entry->c.ops->mmap)
254 return entry->c.ops->mmap(entry, data->file_private_data,
258 static int snd_info_entry_open(struct inode *inode, struct file *file)
260 struct snd_info_entry *entry = PDE_DATA(inode);
261 struct snd_info_private_data *data;
264 mutex_lock(&info_mutex);
265 err = alloc_info_private(entry, &data);
269 mode = file->f_flags & O_ACCMODE;
270 if (((mode == O_RDONLY || mode == O_RDWR) && !entry->c.ops->read) ||
271 ((mode == O_WRONLY || mode == O_RDWR) && !entry->c.ops->write)) {
276 if (entry->c.ops->open) {
277 err = entry->c.ops->open(entry, mode, &data->file_private_data);
282 file->private_data = data;
283 mutex_unlock(&info_mutex);
288 module_put(entry->module);
290 mutex_unlock(&info_mutex);
294 static int snd_info_entry_release(struct inode *inode, struct file *file)
296 struct snd_info_private_data *data = file->private_data;
297 struct snd_info_entry *entry = data->entry;
299 if (entry->c.ops->release)
300 entry->c.ops->release(entry, file->f_flags & O_ACCMODE,
301 data->file_private_data);
302 module_put(entry->module);
307 static const struct file_operations snd_info_entry_operations =
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,
321 * file ops for text proc files
323 static ssize_t snd_info_text_entry_write(struct file *file,
324 const char __user *buffer,
325 size_t count, loff_t *offset)
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;
336 if (!valid_pos(pos, count))
339 mutex_lock(&entry->access);
342 data->wbuffer = buf = kzalloc(sizeof(*buf), GFP_KERNEL);
348 if (next > buf->len) {
349 char *nbuf = krealloc(buf->buffer, PAGE_ALIGN(next),
350 GFP_KERNEL | __GFP_ZERO);
356 buf->len = PAGE_ALIGN(next);
358 if (copy_from_user(buf->buffer + pos, buffer, count)) {
364 mutex_unlock(&entry->access);
371 static int snd_info_seq_show(struct seq_file *seq, void *p)
373 struct snd_info_private_data *data = seq->private;
374 struct snd_info_entry *entry = data->entry;
376 if (entry->c.text.read) {
377 data->rbuffer->buffer = (char *)seq; /* XXX hack! */
378 entry->c.text.read(entry, data->rbuffer);
383 static int snd_info_text_entry_open(struct inode *inode, struct file *file)
385 struct snd_info_entry *entry = PDE_DATA(inode);
386 struct snd_info_private_data *data;
389 mutex_lock(&info_mutex);
390 err = alloc_info_private(entry, &data);
394 data->rbuffer = kzalloc(sizeof(*data->rbuffer), GFP_KERNEL);
395 if (!data->rbuffer) {
400 err = single_open_size(file, snd_info_seq_show, data,
403 err = single_open(file, snd_info_seq_show, data);
406 mutex_unlock(&info_mutex);
410 kfree(data->rbuffer);
412 module_put(entry->module);
414 mutex_unlock(&info_mutex);
418 static int snd_info_text_entry_release(struct inode *inode, struct file *file)
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;
424 if (data->wbuffer && entry->c.text.write)
425 entry->c.text.write(entry, data->wbuffer);
427 single_release(inode, file);
428 kfree(data->rbuffer);
430 kfree(data->wbuffer->buffer);
431 kfree(data->wbuffer);
434 module_put(entry->module);
439 static const struct file_operations snd_info_text_entry_ops =
441 .owner = THIS_MODULE,
442 .open = snd_info_text_entry_open,
443 .release = snd_info_text_entry_release,
444 .write = snd_info_text_entry_write,
449 static struct snd_info_entry *create_subdir(struct module *mod,
452 struct snd_info_entry *entry;
454 entry = snd_info_create_module_entry(mod, name, NULL);
457 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
458 if (snd_info_register(entry) < 0) {
459 snd_info_free_entry(entry);
465 int __init snd_info_init(void)
467 struct proc_dir_entry *p;
469 p = proc_mkdir("asound", NULL);
473 #ifdef CONFIG_SND_OSSEMUL
474 snd_oss_root = create_subdir(THIS_MODULE, "oss");
478 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
479 snd_seq_root = create_subdir(THIS_MODULE, "seq");
483 snd_info_version_init();
484 snd_minor_info_init();
485 snd_minor_info_oss_init();
486 snd_card_info_init();
490 #ifdef CONFIG_SND_OSSEMUL
491 snd_info_free_entry(snd_oss_root);
493 proc_remove(snd_proc_root);
497 int __exit snd_info_done(void)
499 snd_card_info_done();
500 snd_minor_info_oss_done();
501 snd_minor_info_done();
502 snd_info_version_done();
504 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
505 snd_info_free_entry(snd_seq_root);
507 #ifdef CONFIG_SND_OSSEMUL
508 snd_info_free_entry(snd_oss_root);
510 proc_remove(snd_proc_root);
521 * create a card proc file
524 int snd_info_card_create(struct snd_card *card)
527 struct snd_info_entry *entry;
529 if (snd_BUG_ON(!card))
532 sprintf(str, "card%i", card->number);
533 entry = create_subdir(card->module, str);
536 card->proc_root = entry;
541 * register the card proc file
544 int snd_info_card_register(struct snd_card *card)
546 struct proc_dir_entry *p;
548 if (snd_BUG_ON(!card))
551 if (!strcmp(card->id, card->proc_root->name))
554 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
557 card->proc_root_link = p;
562 * called on card->id change
564 void snd_info_card_id_change(struct snd_card *card)
566 mutex_lock(&info_mutex);
567 if (card->proc_root_link) {
568 proc_remove(card->proc_root_link);
569 card->proc_root_link = NULL;
571 if (strcmp(card->id, card->proc_root->name))
572 card->proc_root_link = proc_symlink(card->id,
574 card->proc_root->name);
575 mutex_unlock(&info_mutex);
579 * de-register the card proc file
582 void snd_info_card_disconnect(struct snd_card *card)
586 mutex_lock(&info_mutex);
587 proc_remove(card->proc_root_link);
588 card->proc_root_link = NULL;
590 snd_info_disconnect(card->proc_root);
591 mutex_unlock(&info_mutex);
595 * release the card proc file resources
598 int snd_info_card_free(struct snd_card *card)
602 snd_info_free_entry(card->proc_root);
603 card->proc_root = NULL;
609 * snd_info_get_line - read one line from the procfs buffer
610 * @buffer: the procfs buffer
611 * @line: the buffer to store
612 * @len: the max. buffer size
614 * Reads one line from the buffer and stores the string.
616 * Return: Zero if successful, or 1 if error or EOF.
618 int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
622 if (snd_BUG_ON(!buffer || !buffer->buffer))
624 if (len <= 0 || buffer->stop || buffer->error)
626 while (!buffer->stop) {
627 c = buffer->buffer[buffer->curr++];
628 if (buffer->curr >= buffer->size)
641 EXPORT_SYMBOL(snd_info_get_line);
644 * snd_info_get_str - parse a string token
645 * @dest: the buffer to store the string token
646 * @src: the original string
647 * @len: the max. length of token - 1
649 * Parses the original string and copy a token to the given
652 * Return: The updated pointer of the original string so that
653 * it can be used for the next call.
655 const char *snd_info_get_str(char *dest, const char *src, int len)
659 while (*src == ' ' || *src == '\t')
661 if (*src == '"' || *src == '\'') {
663 while (--len > 0 && *src && *src != c) {
669 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
674 while (*src == ' ' || *src == '\t')
679 EXPORT_SYMBOL(snd_info_get_str);
682 * snd_info_create_entry - create an info entry
683 * @name: the proc file name
685 * Creates an info entry with the given file name and initializes as
688 * Usually called from other functions such as
689 * snd_info_create_card_entry().
691 * Return: The pointer of the new instance, or %NULL on failure.
693 static struct snd_info_entry *snd_info_create_entry(const char *name)
695 struct snd_info_entry *entry;
696 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
699 entry->name = kstrdup(name, GFP_KERNEL);
700 if (entry->name == NULL) {
704 entry->mode = S_IFREG | S_IRUGO;
705 entry->content = SNDRV_INFO_CONTENT_TEXT;
706 mutex_init(&entry->access);
707 INIT_LIST_HEAD(&entry->children);
708 INIT_LIST_HEAD(&entry->list);
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
718 * Creates a new info entry and assigns it to the given module.
720 * Return: The pointer of the new instance, or %NULL on failure.
722 struct snd_info_entry *snd_info_create_module_entry(struct module * module,
724 struct snd_info_entry *parent)
726 struct snd_info_entry *entry = snd_info_create_entry(name);
728 entry->module = module;
729 entry->parent = parent;
734 EXPORT_SYMBOL(snd_info_create_module_entry);
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
742 * Creates a new info entry and assigns it to the given card.
744 * Return: The pointer of the new instance, or %NULL on failure.
746 struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
748 struct snd_info_entry * parent)
750 struct snd_info_entry *entry = snd_info_create_entry(name);
752 entry->module = card->module;
754 entry->parent = parent;
759 EXPORT_SYMBOL(snd_info_create_card_entry);
761 static void snd_info_disconnect(struct snd_info_entry *entry)
763 struct list_head *p, *n;
765 list_for_each_safe(p, n, &entry->children) {
766 snd_info_disconnect(list_entry(p, struct snd_info_entry, list));
771 list_del_init(&entry->list);
772 proc_remove(entry->p);
776 static int snd_info_dev_free_entry(struct snd_device *device)
778 struct snd_info_entry *entry = device->device_data;
779 snd_info_free_entry(entry);
783 static int snd_info_dev_register_entry(struct snd_device *device)
785 struct snd_info_entry *entry = device->device_data;
786 return snd_info_register(entry);
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
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.
802 * The parent is assumed as card->proc_root.
804 * For releasing this entry, use snd_device_free() instead of
805 * snd_info_free_entry().
807 * Return: Zero if successful, or a negative error code on failure.
809 int snd_card_proc_new(struct snd_card *card, const char *name,
810 struct snd_info_entry **entryp)
812 static struct snd_device_ops ops = {
813 .dev_free = snd_info_dev_free_entry,
814 .dev_register = snd_info_dev_register_entry,
815 /* disconnect is done via snd_info_card_disconnect() */
817 struct snd_info_entry *entry;
820 entry = snd_info_create_card_entry(card, name, card->proc_root);
823 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
824 snd_info_free_entry(entry);
832 EXPORT_SYMBOL(snd_card_proc_new);
835 * snd_info_free_entry - release the info entry
836 * @entry: the info entry
838 * Releases the info entry. Don't call this after registered.
840 void snd_info_free_entry(struct snd_info_entry * entry)
845 mutex_lock(&info_mutex);
846 snd_info_disconnect(entry);
847 mutex_unlock(&info_mutex);
850 if (entry->private_free)
851 entry->private_free(entry);
855 EXPORT_SYMBOL(snd_info_free_entry);
858 * snd_info_register - register the info entry
859 * @entry: the info entry
861 * Registers the proc info entry.
863 * Return: Zero if successful, or a negative error code on failure.
865 int snd_info_register(struct snd_info_entry * entry)
867 struct proc_dir_entry *root, *p = NULL;
869 if (snd_BUG_ON(!entry))
871 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
872 mutex_lock(&info_mutex);
873 if (S_ISDIR(entry->mode)) {
874 p = proc_mkdir_mode(entry->name, entry->mode, root);
876 mutex_unlock(&info_mutex);
880 const struct file_operations *ops;
881 if (entry->content == SNDRV_INFO_CONTENT_DATA)
882 ops = &snd_info_entry_operations;
884 ops = &snd_info_text_entry_ops;
885 p = proc_create_data(entry->name, entry->mode, root,
888 mutex_unlock(&info_mutex);
891 proc_set_size(p, entry->size);
895 list_add_tail(&entry->list, &entry->parent->children);
896 mutex_unlock(&info_mutex);
900 EXPORT_SYMBOL(snd_info_register);
906 static struct snd_info_entry *snd_info_version_entry;
908 static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
911 "Advanced Linux Sound Architecture Driver Version k%s.\n",
912 init_utsname()->release);
915 static int __init snd_info_version_init(void)
917 struct snd_info_entry *entry;
919 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
922 entry->c.text.read = snd_info_version_read;
923 if (snd_info_register(entry) < 0) {
924 snd_info_free_entry(entry);
927 snd_info_version_entry = entry;
931 static int __exit snd_info_version_done(void)
933 snd_info_free_entry(snd_info_version_entry);
937 #endif /* CONFIG_PROC_FS */