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/smp_lock.h>
26 #include <linux/string.h>
27 #include <sound/core.h>
28 #include <sound/minors.h>
29 #include <sound/info.h>
30 #include <sound/version.h>
31 #include <linux/proc_fs.h>
32 #include <linux/mutex.h>
41 int snd_info_check_reserved_words(const char *str)
43 static char *reserved[] =
58 char **xstr = reserved;
61 if (!strcmp(*xstr, str))
65 if (!strncmp(str, "card", 4))
70 static DEFINE_MUTEX(info_mutex);
72 struct snd_info_private_data {
73 struct snd_info_buffer *rbuffer;
74 struct snd_info_buffer *wbuffer;
75 struct snd_info_entry *entry;
76 void *file_private_data;
79 static int snd_info_version_init(void);
80 static int snd_info_version_done(void);
81 static void snd_info_disconnect(struct snd_info_entry *entry);
84 /* resize the proc r/w buffer */
85 static int resize_info_buffer(struct snd_info_buffer *buffer,
90 nsize = PAGE_ALIGN(nsize);
91 nbuf = kmalloc(nsize, GFP_KERNEL);
95 memcpy(nbuf, buffer->buffer, buffer->len);
96 kfree(buffer->buffer);
97 buffer->buffer = nbuf;
103 * snd_iprintf - printf on the procfs buffer
104 * @buffer: the procfs buffer
105 * @fmt: the printf format
107 * Outputs the string on the procfs buffer just like printf().
109 * Returns the size of output string.
111 int snd_iprintf(struct snd_info_buffer *buffer, char *fmt,...)
118 if (buffer->stop || buffer->error)
120 len = buffer->len - buffer->size;
125 res = vsnprintf(buffer->buffer + buffer->curr, len, fmt, ap);
129 err = resize_info_buffer(buffer, buffer->len + PAGE_SIZE);
132 len = buffer->len - buffer->size;
143 EXPORT_SYMBOL(snd_iprintf);
149 static struct proc_dir_entry *snd_proc_root;
150 struct snd_info_entry *snd_seq_root;
151 EXPORT_SYMBOL(snd_seq_root);
153 #ifdef CONFIG_SND_OSSEMUL
154 struct snd_info_entry *snd_oss_root;
157 static inline void snd_info_entry_prepare(struct proc_dir_entry *de)
159 de->owner = THIS_MODULE;
162 static void snd_remove_proc_entry(struct proc_dir_entry *parent,
163 struct proc_dir_entry *de)
166 remove_proc_entry(de->name, parent);
169 static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
171 struct snd_info_private_data *data;
172 struct snd_info_entry *entry;
175 data = file->private_data;
178 switch (entry->content) {
179 case SNDRV_INFO_CONTENT_TEXT:
182 file->f_pos = offset;
186 file->f_pos += offset;
195 case SNDRV_INFO_CONTENT_DATA:
196 if (entry->c.ops->llseek) {
197 ret = entry->c.ops->llseek(entry,
198 data->file_private_data,
210 static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
211 size_t count, loff_t * offset)
213 struct snd_info_private_data *data;
214 struct snd_info_entry *entry;
215 struct snd_info_buffer *buf;
219 data = file->private_data;
220 if (snd_BUG_ON(!data))
223 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
225 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
228 switch (entry->content) {
229 case SNDRV_INFO_CONTENT_TEXT:
233 if (pos >= buf->size)
235 size = buf->size - pos;
236 size = min(count, size);
237 if (copy_to_user(buffer, buf->buffer + pos, size))
240 case SNDRV_INFO_CONTENT_DATA:
241 if (entry->c.ops->read)
242 size = entry->c.ops->read(entry,
243 data->file_private_data,
244 file, buffer, count, pos);
247 if ((ssize_t) size > 0)
248 *offset = pos + size;
252 static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
253 size_t count, loff_t * offset)
255 struct snd_info_private_data *data;
256 struct snd_info_entry *entry;
257 struct snd_info_buffer *buf;
261 data = file->private_data;
262 if (snd_BUG_ON(!data))
266 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
268 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
270 switch (entry->content) {
271 case SNDRV_INFO_CONTENT_TEXT:
275 mutex_lock(&entry->access);
276 if (pos + count >= buf->len) {
277 if (resize_info_buffer(buf, pos + count)) {
278 mutex_unlock(&entry->access);
282 if (copy_from_user(buf->buffer + pos, buffer, count)) {
283 mutex_unlock(&entry->access);
286 buf->size = pos + count;
287 mutex_unlock(&entry->access);
290 case SNDRV_INFO_CONTENT_DATA:
291 if (entry->c.ops->write)
292 size = entry->c.ops->write(entry,
293 data->file_private_data,
294 file, buffer, count, pos);
297 if ((ssize_t) size > 0)
298 *offset = pos + size;
302 static int snd_info_entry_open(struct inode *inode, struct file *file)
304 struct snd_info_entry *entry;
305 struct snd_info_private_data *data;
306 struct snd_info_buffer *buffer;
307 struct proc_dir_entry *p;
310 mutex_lock(&info_mutex);
312 entry = p == NULL ? NULL : (struct snd_info_entry *)p->data;
313 if (entry == NULL || ! entry->p) {
314 mutex_unlock(&info_mutex);
317 if (!try_module_get(entry->module)) {
321 mode = file->f_flags & O_ACCMODE;
322 if (mode == O_RDONLY || mode == O_RDWR) {
323 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
324 entry->c.ops->read == NULL)) {
329 if (mode == O_WRONLY || mode == O_RDWR) {
330 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
331 entry->c.ops->write == NULL)) {
336 data = kzalloc(sizeof(*data), GFP_KERNEL);
342 switch (entry->content) {
343 case SNDRV_INFO_CONTENT_TEXT:
344 if (mode == O_RDONLY || mode == O_RDWR) {
345 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
348 data->rbuffer = buffer;
349 buffer->len = PAGE_SIZE;
350 buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
351 if (buffer->buffer == NULL)
354 if (mode == O_WRONLY || mode == O_RDWR) {
355 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
358 data->wbuffer = buffer;
359 buffer->len = PAGE_SIZE;
360 buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
361 if (buffer->buffer == NULL)
365 case SNDRV_INFO_CONTENT_DATA: /* data */
366 if (entry->c.ops->open) {
367 if ((err = entry->c.ops->open(entry, mode,
368 &data->file_private_data)) < 0) {
375 file->private_data = data;
376 mutex_unlock(&info_mutex);
377 if (entry->content == SNDRV_INFO_CONTENT_TEXT &&
378 (mode == O_RDONLY || mode == O_RDWR)) {
379 if (entry->c.text.read) {
380 mutex_lock(&entry->access);
381 entry->c.text.read(entry, data->rbuffer);
382 mutex_unlock(&entry->access);
389 kfree(data->rbuffer->buffer);
390 kfree(data->rbuffer);
393 kfree(data->wbuffer->buffer);
394 kfree(data->wbuffer);
399 module_put(entry->module);
401 mutex_unlock(&info_mutex);
405 static int snd_info_entry_release(struct inode *inode, struct file *file)
407 struct snd_info_entry *entry;
408 struct snd_info_private_data *data;
411 mode = file->f_flags & O_ACCMODE;
412 data = file->private_data;
414 switch (entry->content) {
415 case SNDRV_INFO_CONTENT_TEXT:
417 kfree(data->rbuffer->buffer);
418 kfree(data->rbuffer);
421 if (entry->c.text.write) {
422 entry->c.text.write(entry, data->wbuffer);
423 if (data->wbuffer->error) {
424 snd_printk(KERN_WARNING "data write error to %s (%i)\n",
426 data->wbuffer->error);
429 kfree(data->wbuffer->buffer);
430 kfree(data->wbuffer);
433 case SNDRV_INFO_CONTENT_DATA:
434 if (entry->c.ops->release)
435 entry->c.ops->release(entry, mode,
436 data->file_private_data);
439 module_put(entry->module);
444 static unsigned int snd_info_entry_poll(struct file *file, poll_table * wait)
446 struct snd_info_private_data *data;
447 struct snd_info_entry *entry;
450 data = file->private_data;
455 switch (entry->content) {
456 case SNDRV_INFO_CONTENT_DATA:
457 if (entry->c.ops->poll)
458 return entry->c.ops->poll(entry,
459 data->file_private_data,
461 if (entry->c.ops->read)
462 mask |= POLLIN | POLLRDNORM;
463 if (entry->c.ops->write)
464 mask |= POLLOUT | POLLWRNORM;
470 static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
473 struct snd_info_private_data *data;
474 struct snd_info_entry *entry;
476 data = file->private_data;
480 switch (entry->content) {
481 case SNDRV_INFO_CONTENT_DATA:
482 if (entry->c.ops->ioctl)
483 return entry->c.ops->ioctl(entry,
484 data->file_private_data,
491 static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
493 struct inode *inode = file->f_path.dentry->d_inode;
494 struct snd_info_private_data *data;
495 struct snd_info_entry *entry;
497 data = file->private_data;
501 switch (entry->content) {
502 case SNDRV_INFO_CONTENT_DATA:
503 if (entry->c.ops->mmap)
504 return entry->c.ops->mmap(entry,
505 data->file_private_data,
512 static const struct file_operations snd_info_entry_operations =
514 .owner = THIS_MODULE,
515 .llseek = snd_info_entry_llseek,
516 .read = snd_info_entry_read,
517 .write = snd_info_entry_write,
518 .poll = snd_info_entry_poll,
519 .unlocked_ioctl = snd_info_entry_ioctl,
520 .mmap = snd_info_entry_mmap,
521 .open = snd_info_entry_open,
522 .release = snd_info_entry_release,
526 * snd_create_proc_entry - create a procfs entry
527 * @name: the name of the proc file
528 * @mode: the file permission bits, S_Ixxx
529 * @parent: the parent proc-directory entry
531 * Creates a new proc file entry with the given name and permission
532 * on the given directory.
534 * Returns the pointer of new instance or NULL on failure.
536 static struct proc_dir_entry *snd_create_proc_entry(const char *name, mode_t mode,
537 struct proc_dir_entry *parent)
539 struct proc_dir_entry *p;
540 p = create_proc_entry(name, mode, parent);
542 snd_info_entry_prepare(p);
546 int __init snd_info_init(void)
548 struct proc_dir_entry *p;
550 p = snd_create_proc_entry("asound", S_IFDIR | S_IRUGO | S_IXUGO, NULL);
554 #ifdef CONFIG_SND_OSSEMUL
556 struct snd_info_entry *entry;
557 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
559 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
560 if (snd_info_register(entry) < 0) {
561 snd_info_free_entry(entry);
564 snd_oss_root = entry;
567 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
569 struct snd_info_entry *entry;
570 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL)
572 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
573 if (snd_info_register(entry) < 0) {
574 snd_info_free_entry(entry);
577 snd_seq_root = entry;
580 snd_info_version_init();
581 snd_minor_info_init();
582 snd_minor_info_oss_init();
583 snd_card_info_init();
587 int __exit snd_info_done(void)
589 snd_card_info_done();
590 snd_minor_info_oss_done();
591 snd_minor_info_done();
592 snd_info_version_done();
594 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
595 snd_info_free_entry(snd_seq_root);
597 #ifdef CONFIG_SND_OSSEMUL
598 snd_info_free_entry(snd_oss_root);
600 snd_remove_proc_entry(NULL, snd_proc_root);
611 * create a card proc file
614 int snd_info_card_create(struct snd_card *card)
617 struct snd_info_entry *entry;
619 if (snd_BUG_ON(!card))
622 sprintf(str, "card%i", card->number);
623 if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
625 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
626 if (snd_info_register(entry) < 0) {
627 snd_info_free_entry(entry);
630 card->proc_root = entry;
635 * register the card proc file
638 int snd_info_card_register(struct snd_card *card)
640 struct proc_dir_entry *p;
642 if (snd_BUG_ON(!card))
645 if (!strcmp(card->id, card->proc_root->name))
648 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
651 card->proc_root_link = p;
656 * called on card->id change
658 void snd_info_card_id_change(struct snd_card *card)
660 mutex_lock(&info_mutex);
661 if (card->proc_root_link) {
662 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
663 card->proc_root_link = NULL;
665 if (strcmp(card->id, card->proc_root->name))
666 card->proc_root_link = proc_symlink(card->id,
668 card->proc_root->name);
669 mutex_unlock(&info_mutex);
673 * de-register the card proc file
676 void snd_info_card_disconnect(struct snd_card *card)
680 mutex_lock(&info_mutex);
681 if (card->proc_root_link) {
682 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
683 card->proc_root_link = NULL;
686 snd_info_disconnect(card->proc_root);
687 mutex_unlock(&info_mutex);
691 * release the card proc file resources
694 int snd_info_card_free(struct snd_card *card)
698 snd_info_free_entry(card->proc_root);
699 card->proc_root = NULL;
705 * snd_info_get_line - read one line from the procfs buffer
706 * @buffer: the procfs buffer
707 * @line: the buffer to store
708 * @len: the max. buffer size - 1
710 * Reads one line from the buffer and stores the string.
712 * Returns zero if successful, or 1 if error or EOF.
714 int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
718 if (len <= 0 || buffer->stop || buffer->error)
721 c = buffer->buffer[buffer->curr++];
723 if (buffer->curr >= buffer->size)
728 if (buffer->curr >= buffer->size) {
733 while (c != '\n' && !buffer->stop) {
734 c = buffer->buffer[buffer->curr++];
735 if (buffer->curr >= buffer->size)
742 EXPORT_SYMBOL(snd_info_get_line);
745 * snd_info_get_str - parse a string token
746 * @dest: the buffer to store the string token
747 * @src: the original string
748 * @len: the max. length of token - 1
750 * Parses the original string and copy a token to the given
753 * Returns the updated pointer of the original string so that
754 * it can be used for the next call.
756 char *snd_info_get_str(char *dest, char *src, int len)
760 while (*src == ' ' || *src == '\t')
762 if (*src == '"' || *src == '\'') {
764 while (--len > 0 && *src && *src != c) {
770 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
775 while (*src == ' ' || *src == '\t')
780 EXPORT_SYMBOL(snd_info_get_str);
783 * snd_info_create_entry - create an info entry
784 * @name: the proc file name
786 * Creates an info entry with the given file name and initializes as
789 * Usually called from other functions such as
790 * snd_info_create_card_entry().
792 * Returns the pointer of the new instance, or NULL on failure.
794 static struct snd_info_entry *snd_info_create_entry(const char *name)
796 struct snd_info_entry *entry;
797 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
800 entry->name = kstrdup(name, GFP_KERNEL);
801 if (entry->name == NULL) {
805 entry->mode = S_IFREG | S_IRUGO;
806 entry->content = SNDRV_INFO_CONTENT_TEXT;
807 mutex_init(&entry->access);
808 INIT_LIST_HEAD(&entry->children);
809 INIT_LIST_HEAD(&entry->list);
814 * snd_info_create_module_entry - create an info entry for the given module
815 * @module: the module pointer
816 * @name: the file name
817 * @parent: the parent directory
819 * Creates a new info entry and assigns it to the given module.
821 * Returns the pointer of the new instance, or NULL on failure.
823 struct snd_info_entry *snd_info_create_module_entry(struct module * module,
825 struct snd_info_entry *parent)
827 struct snd_info_entry *entry = snd_info_create_entry(name);
829 entry->module = module;
830 entry->parent = parent;
835 EXPORT_SYMBOL(snd_info_create_module_entry);
838 * snd_info_create_card_entry - create an info entry for the given card
839 * @card: the card instance
840 * @name: the file name
841 * @parent: the parent directory
843 * Creates a new info entry and assigns it to the given card.
845 * Returns the pointer of the new instance, or NULL on failure.
847 struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
849 struct snd_info_entry * parent)
851 struct snd_info_entry *entry = snd_info_create_entry(name);
853 entry->module = card->module;
855 entry->parent = parent;
860 EXPORT_SYMBOL(snd_info_create_card_entry);
862 static void snd_info_disconnect(struct snd_info_entry *entry)
864 struct list_head *p, *n;
865 struct proc_dir_entry *root;
867 list_for_each_safe(p, n, &entry->children) {
868 snd_info_disconnect(list_entry(p, struct snd_info_entry, list));
873 list_del_init(&entry->list);
874 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
876 snd_remove_proc_entry(root, entry->p);
880 static int snd_info_dev_free_entry(struct snd_device *device)
882 struct snd_info_entry *entry = device->device_data;
883 snd_info_free_entry(entry);
887 static int snd_info_dev_register_entry(struct snd_device *device)
889 struct snd_info_entry *entry = device->device_data;
890 return snd_info_register(entry);
894 * snd_card_proc_new - create an info entry for the given card
895 * @card: the card instance
896 * @name: the file name
897 * @entryp: the pointer to store the new info entry
899 * Creates a new info entry and assigns it to the given card.
900 * Unlike snd_info_create_card_entry(), this function registers the
901 * info entry as an ALSA device component, so that it can be
902 * unregistered/released without explicit call.
903 * Also, you don't have to register this entry via snd_info_register(),
904 * since this will be registered by snd_card_register() automatically.
906 * The parent is assumed as card->proc_root.
908 * For releasing this entry, use snd_device_free() instead of
909 * snd_info_free_entry().
911 * Returns zero if successful, or a negative error code on failure.
913 int snd_card_proc_new(struct snd_card *card, const char *name,
914 struct snd_info_entry **entryp)
916 static struct snd_device_ops ops = {
917 .dev_free = snd_info_dev_free_entry,
918 .dev_register = snd_info_dev_register_entry,
919 /* disconnect is done via snd_info_card_disconnect() */
921 struct snd_info_entry *entry;
924 entry = snd_info_create_card_entry(card, name, card->proc_root);
927 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
928 snd_info_free_entry(entry);
936 EXPORT_SYMBOL(snd_card_proc_new);
939 * snd_info_free_entry - release the info entry
940 * @entry: the info entry
942 * Releases the info entry. Don't call this after registered.
944 void snd_info_free_entry(struct snd_info_entry * entry)
949 mutex_lock(&info_mutex);
950 snd_info_disconnect(entry);
951 mutex_unlock(&info_mutex);
954 if (entry->private_free)
955 entry->private_free(entry);
959 EXPORT_SYMBOL(snd_info_free_entry);
962 * snd_info_register - register the info entry
963 * @entry: the info entry
965 * Registers the proc info entry.
967 * Returns zero if successful, or a negative error code on failure.
969 int snd_info_register(struct snd_info_entry * entry)
971 struct proc_dir_entry *root, *p = NULL;
973 if (snd_BUG_ON(!entry))
975 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
976 mutex_lock(&info_mutex);
977 p = snd_create_proc_entry(entry->name, entry->mode, root);
979 mutex_unlock(&info_mutex);
982 p->owner = entry->module;
983 if (!S_ISDIR(entry->mode))
984 p->proc_fops = &snd_info_entry_operations;
985 p->size = entry->size;
989 list_add_tail(&entry->list, &entry->parent->children);
990 mutex_unlock(&info_mutex);
994 EXPORT_SYMBOL(snd_info_register);
1000 static struct snd_info_entry *snd_info_version_entry;
1002 static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
1005 "Advanced Linux Sound Architecture Driver Version "
1006 CONFIG_SND_VERSION CONFIG_SND_DATE ".\n"
1010 static int __init snd_info_version_init(void)
1012 struct snd_info_entry *entry;
1014 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
1017 entry->c.text.read = snd_info_version_read;
1018 if (snd_info_register(entry) < 0) {
1019 snd_info_free_entry(entry);
1022 snd_info_version_entry = entry;
1026 static int __exit snd_info_version_done(void)
1028 snd_info_free_entry(snd_info_version_entry);
1032 #endif /* CONFIG_PROC_FS */