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 = krealloc(buffer->buffer, nsize, GFP_KERNEL);
95 buffer->buffer = nbuf;
101 * snd_iprintf - printf on the procfs buffer
102 * @buffer: the procfs buffer
103 * @fmt: the printf format
105 * Outputs the string on the procfs buffer just like printf().
107 * Returns the size of output string.
109 int snd_iprintf(struct snd_info_buffer *buffer, const char *fmt, ...)
116 if (buffer->stop || buffer->error)
118 len = buffer->len - buffer->size;
123 res = vsnprintf(buffer->buffer + buffer->curr, len, fmt, ap);
127 err = resize_info_buffer(buffer, buffer->len + PAGE_SIZE);
130 len = buffer->len - buffer->size;
141 EXPORT_SYMBOL(snd_iprintf);
147 static struct proc_dir_entry *snd_proc_root;
148 struct snd_info_entry *snd_seq_root;
149 EXPORT_SYMBOL(snd_seq_root);
151 #ifdef CONFIG_SND_OSSEMUL
152 struct snd_info_entry *snd_oss_root;
155 static void snd_remove_proc_entry(struct proc_dir_entry *parent,
156 struct proc_dir_entry *de)
159 remove_proc_entry(de->name, parent);
162 static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
164 struct snd_info_private_data *data;
165 struct snd_info_entry *entry;
168 data = file->private_data;
171 switch (entry->content) {
172 case SNDRV_INFO_CONTENT_TEXT:
175 file->f_pos = offset;
179 file->f_pos += offset;
188 case SNDRV_INFO_CONTENT_DATA:
189 if (entry->c.ops->llseek) {
190 ret = entry->c.ops->llseek(entry,
191 data->file_private_data,
203 static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
204 size_t count, loff_t * offset)
206 struct snd_info_private_data *data;
207 struct snd_info_entry *entry;
208 struct snd_info_buffer *buf;
212 data = file->private_data;
213 if (snd_BUG_ON(!data))
216 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
218 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
221 switch (entry->content) {
222 case SNDRV_INFO_CONTENT_TEXT:
226 if (pos >= buf->size)
228 size = buf->size - pos;
229 size = min(count, size);
230 if (copy_to_user(buffer, buf->buffer + pos, size))
233 case SNDRV_INFO_CONTENT_DATA:
234 if (entry->c.ops->read)
235 size = entry->c.ops->read(entry,
236 data->file_private_data,
237 file, buffer, count, pos);
240 if ((ssize_t) size > 0)
241 *offset = pos + size;
245 static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
246 size_t count, loff_t * offset)
248 struct snd_info_private_data *data;
249 struct snd_info_entry *entry;
250 struct snd_info_buffer *buf;
254 data = file->private_data;
255 if (snd_BUG_ON(!data))
259 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
261 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
263 switch (entry->content) {
264 case SNDRV_INFO_CONTENT_TEXT:
268 mutex_lock(&entry->access);
269 if (pos + count >= buf->len) {
270 if (resize_info_buffer(buf, pos + count)) {
271 mutex_unlock(&entry->access);
275 if (copy_from_user(buf->buffer + pos, buffer, count)) {
276 mutex_unlock(&entry->access);
279 buf->size = pos + count;
280 mutex_unlock(&entry->access);
283 case SNDRV_INFO_CONTENT_DATA:
284 if (entry->c.ops->write)
285 size = entry->c.ops->write(entry,
286 data->file_private_data,
287 file, buffer, count, pos);
290 if ((ssize_t) size > 0)
291 *offset = pos + size;
295 static int snd_info_entry_open(struct inode *inode, struct file *file)
297 struct snd_info_entry *entry;
298 struct snd_info_private_data *data;
299 struct snd_info_buffer *buffer;
300 struct proc_dir_entry *p;
303 mutex_lock(&info_mutex);
305 entry = p == NULL ? NULL : (struct snd_info_entry *)p->data;
306 if (entry == NULL || ! entry->p) {
307 mutex_unlock(&info_mutex);
310 if (!try_module_get(entry->module)) {
314 mode = file->f_flags & O_ACCMODE;
315 if (mode == O_RDONLY || mode == O_RDWR) {
316 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
317 entry->c.ops->read == NULL)) {
322 if (mode == O_WRONLY || mode == O_RDWR) {
323 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
324 entry->c.ops->write == NULL)) {
329 data = kzalloc(sizeof(*data), GFP_KERNEL);
335 switch (entry->content) {
336 case SNDRV_INFO_CONTENT_TEXT:
337 if (mode == O_RDONLY || mode == O_RDWR) {
338 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
341 data->rbuffer = buffer;
342 buffer->len = PAGE_SIZE;
343 buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
344 if (buffer->buffer == NULL)
347 if (mode == O_WRONLY || mode == O_RDWR) {
348 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
351 data->wbuffer = buffer;
352 buffer->len = PAGE_SIZE;
353 buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
354 if (buffer->buffer == NULL)
358 case SNDRV_INFO_CONTENT_DATA: /* data */
359 if (entry->c.ops->open) {
360 if ((err = entry->c.ops->open(entry, mode,
361 &data->file_private_data)) < 0) {
368 file->private_data = data;
369 mutex_unlock(&info_mutex);
370 if (entry->content == SNDRV_INFO_CONTENT_TEXT &&
371 (mode == O_RDONLY || mode == O_RDWR)) {
372 if (entry->c.text.read) {
373 mutex_lock(&entry->access);
374 entry->c.text.read(entry, data->rbuffer);
375 mutex_unlock(&entry->access);
382 kfree(data->rbuffer->buffer);
383 kfree(data->rbuffer);
386 kfree(data->wbuffer->buffer);
387 kfree(data->wbuffer);
392 module_put(entry->module);
394 mutex_unlock(&info_mutex);
398 static int snd_info_entry_release(struct inode *inode, struct file *file)
400 struct snd_info_entry *entry;
401 struct snd_info_private_data *data;
404 mode = file->f_flags & O_ACCMODE;
405 data = file->private_data;
407 switch (entry->content) {
408 case SNDRV_INFO_CONTENT_TEXT:
410 kfree(data->rbuffer->buffer);
411 kfree(data->rbuffer);
414 if (entry->c.text.write) {
415 entry->c.text.write(entry, data->wbuffer);
416 if (data->wbuffer->error) {
417 snd_printk(KERN_WARNING "data write error to %s (%i)\n",
419 data->wbuffer->error);
422 kfree(data->wbuffer->buffer);
423 kfree(data->wbuffer);
426 case SNDRV_INFO_CONTENT_DATA:
427 if (entry->c.ops->release)
428 entry->c.ops->release(entry, mode,
429 data->file_private_data);
432 module_put(entry->module);
437 static unsigned int snd_info_entry_poll(struct file *file, poll_table * wait)
439 struct snd_info_private_data *data;
440 struct snd_info_entry *entry;
443 data = file->private_data;
448 switch (entry->content) {
449 case SNDRV_INFO_CONTENT_DATA:
450 if (entry->c.ops->poll)
451 return entry->c.ops->poll(entry,
452 data->file_private_data,
454 if (entry->c.ops->read)
455 mask |= POLLIN | POLLRDNORM;
456 if (entry->c.ops->write)
457 mask |= POLLOUT | POLLWRNORM;
463 static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
466 struct snd_info_private_data *data;
467 struct snd_info_entry *entry;
469 data = file->private_data;
473 switch (entry->content) {
474 case SNDRV_INFO_CONTENT_DATA:
475 if (entry->c.ops->ioctl)
476 return entry->c.ops->ioctl(entry,
477 data->file_private_data,
484 static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
486 struct inode *inode = file->f_path.dentry->d_inode;
487 struct snd_info_private_data *data;
488 struct snd_info_entry *entry;
490 data = file->private_data;
494 switch (entry->content) {
495 case SNDRV_INFO_CONTENT_DATA:
496 if (entry->c.ops->mmap)
497 return entry->c.ops->mmap(entry,
498 data->file_private_data,
505 static const struct file_operations snd_info_entry_operations =
507 .owner = THIS_MODULE,
508 .llseek = snd_info_entry_llseek,
509 .read = snd_info_entry_read,
510 .write = snd_info_entry_write,
511 .poll = snd_info_entry_poll,
512 .unlocked_ioctl = snd_info_entry_ioctl,
513 .mmap = snd_info_entry_mmap,
514 .open = snd_info_entry_open,
515 .release = snd_info_entry_release,
518 int __init snd_info_init(void)
520 struct proc_dir_entry *p;
522 p = create_proc_entry("asound", S_IFDIR | S_IRUGO | S_IXUGO, NULL);
526 #ifdef CONFIG_SND_OSSEMUL
528 struct snd_info_entry *entry;
529 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
531 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
532 if (snd_info_register(entry) < 0) {
533 snd_info_free_entry(entry);
536 snd_oss_root = entry;
539 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
541 struct snd_info_entry *entry;
542 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL)
544 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
545 if (snd_info_register(entry) < 0) {
546 snd_info_free_entry(entry);
549 snd_seq_root = entry;
552 snd_info_version_init();
553 snd_minor_info_init();
554 snd_minor_info_oss_init();
555 snd_card_info_init();
559 int __exit snd_info_done(void)
561 snd_card_info_done();
562 snd_minor_info_oss_done();
563 snd_minor_info_done();
564 snd_info_version_done();
566 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
567 snd_info_free_entry(snd_seq_root);
569 #ifdef CONFIG_SND_OSSEMUL
570 snd_info_free_entry(snd_oss_root);
572 snd_remove_proc_entry(NULL, snd_proc_root);
583 * create a card proc file
586 int snd_info_card_create(struct snd_card *card)
589 struct snd_info_entry *entry;
591 if (snd_BUG_ON(!card))
594 sprintf(str, "card%i", card->number);
595 if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
597 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
598 if (snd_info_register(entry) < 0) {
599 snd_info_free_entry(entry);
602 card->proc_root = entry;
607 * register the card proc file
610 int snd_info_card_register(struct snd_card *card)
612 struct proc_dir_entry *p;
614 if (snd_BUG_ON(!card))
617 if (!strcmp(card->id, card->proc_root->name))
620 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
623 card->proc_root_link = p;
628 * called on card->id change
630 void snd_info_card_id_change(struct snd_card *card)
632 mutex_lock(&info_mutex);
633 if (card->proc_root_link) {
634 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
635 card->proc_root_link = NULL;
637 if (strcmp(card->id, card->proc_root->name))
638 card->proc_root_link = proc_symlink(card->id,
640 card->proc_root->name);
641 mutex_unlock(&info_mutex);
645 * de-register the card proc file
648 void snd_info_card_disconnect(struct snd_card *card)
652 mutex_lock(&info_mutex);
653 if (card->proc_root_link) {
654 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
655 card->proc_root_link = NULL;
658 snd_info_disconnect(card->proc_root);
659 mutex_unlock(&info_mutex);
663 * release the card proc file resources
666 int snd_info_card_free(struct snd_card *card)
670 snd_info_free_entry(card->proc_root);
671 card->proc_root = NULL;
677 * snd_info_get_line - read one line from the procfs buffer
678 * @buffer: the procfs buffer
679 * @line: the buffer to store
680 * @len: the max. buffer size - 1
682 * Reads one line from the buffer and stores the string.
684 * Returns zero if successful, or 1 if error or EOF.
686 int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
690 if (len <= 0 || buffer->stop || buffer->error)
693 c = buffer->buffer[buffer->curr++];
695 if (buffer->curr >= buffer->size)
700 if (buffer->curr >= buffer->size) {
705 while (c != '\n' && !buffer->stop) {
706 c = buffer->buffer[buffer->curr++];
707 if (buffer->curr >= buffer->size)
714 EXPORT_SYMBOL(snd_info_get_line);
717 * snd_info_get_str - parse a string token
718 * @dest: the buffer to store the string token
719 * @src: the original string
720 * @len: the max. length of token - 1
722 * Parses the original string and copy a token to the given
725 * Returns the updated pointer of the original string so that
726 * it can be used for the next call.
728 const char *snd_info_get_str(char *dest, const char *src, int len)
732 while (*src == ' ' || *src == '\t')
734 if (*src == '"' || *src == '\'') {
736 while (--len > 0 && *src && *src != c) {
742 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
747 while (*src == ' ' || *src == '\t')
752 EXPORT_SYMBOL(snd_info_get_str);
755 * snd_info_create_entry - create an info entry
756 * @name: the proc file name
758 * Creates an info entry with the given file name and initializes as
761 * Usually called from other functions such as
762 * snd_info_create_card_entry().
764 * Returns the pointer of the new instance, or NULL on failure.
766 static struct snd_info_entry *snd_info_create_entry(const char *name)
768 struct snd_info_entry *entry;
769 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
772 entry->name = kstrdup(name, GFP_KERNEL);
773 if (entry->name == NULL) {
777 entry->mode = S_IFREG | S_IRUGO;
778 entry->content = SNDRV_INFO_CONTENT_TEXT;
779 mutex_init(&entry->access);
780 INIT_LIST_HEAD(&entry->children);
781 INIT_LIST_HEAD(&entry->list);
786 * snd_info_create_module_entry - create an info entry for the given module
787 * @module: the module pointer
788 * @name: the file name
789 * @parent: the parent directory
791 * Creates a new info entry and assigns it to the given module.
793 * Returns the pointer of the new instance, or NULL on failure.
795 struct snd_info_entry *snd_info_create_module_entry(struct module * module,
797 struct snd_info_entry *parent)
799 struct snd_info_entry *entry = snd_info_create_entry(name);
801 entry->module = module;
802 entry->parent = parent;
807 EXPORT_SYMBOL(snd_info_create_module_entry);
810 * snd_info_create_card_entry - create an info entry for the given card
811 * @card: the card instance
812 * @name: the file name
813 * @parent: the parent directory
815 * Creates a new info entry and assigns it to the given card.
817 * Returns the pointer of the new instance, or NULL on failure.
819 struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
821 struct snd_info_entry * parent)
823 struct snd_info_entry *entry = snd_info_create_entry(name);
825 entry->module = card->module;
827 entry->parent = parent;
832 EXPORT_SYMBOL(snd_info_create_card_entry);
834 static void snd_info_disconnect(struct snd_info_entry *entry)
836 struct list_head *p, *n;
837 struct proc_dir_entry *root;
839 list_for_each_safe(p, n, &entry->children) {
840 snd_info_disconnect(list_entry(p, struct snd_info_entry, list));
845 list_del_init(&entry->list);
846 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
848 snd_remove_proc_entry(root, entry->p);
852 static int snd_info_dev_free_entry(struct snd_device *device)
854 struct snd_info_entry *entry = device->device_data;
855 snd_info_free_entry(entry);
859 static int snd_info_dev_register_entry(struct snd_device *device)
861 struct snd_info_entry *entry = device->device_data;
862 return snd_info_register(entry);
866 * snd_card_proc_new - create an info entry for the given card
867 * @card: the card instance
868 * @name: the file name
869 * @entryp: the pointer to store the new info entry
871 * Creates a new info entry and assigns it to the given card.
872 * Unlike snd_info_create_card_entry(), this function registers the
873 * info entry as an ALSA device component, so that it can be
874 * unregistered/released without explicit call.
875 * Also, you don't have to register this entry via snd_info_register(),
876 * since this will be registered by snd_card_register() automatically.
878 * The parent is assumed as card->proc_root.
880 * For releasing this entry, use snd_device_free() instead of
881 * snd_info_free_entry().
883 * Returns zero if successful, or a negative error code on failure.
885 int snd_card_proc_new(struct snd_card *card, const char *name,
886 struct snd_info_entry **entryp)
888 static struct snd_device_ops ops = {
889 .dev_free = snd_info_dev_free_entry,
890 .dev_register = snd_info_dev_register_entry,
891 /* disconnect is done via snd_info_card_disconnect() */
893 struct snd_info_entry *entry;
896 entry = snd_info_create_card_entry(card, name, card->proc_root);
899 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
900 snd_info_free_entry(entry);
908 EXPORT_SYMBOL(snd_card_proc_new);
911 * snd_info_free_entry - release the info entry
912 * @entry: the info entry
914 * Releases the info entry. Don't call this after registered.
916 void snd_info_free_entry(struct snd_info_entry * entry)
921 mutex_lock(&info_mutex);
922 snd_info_disconnect(entry);
923 mutex_unlock(&info_mutex);
926 if (entry->private_free)
927 entry->private_free(entry);
931 EXPORT_SYMBOL(snd_info_free_entry);
934 * snd_info_register - register the info entry
935 * @entry: the info entry
937 * Registers the proc info entry.
939 * Returns zero if successful, or a negative error code on failure.
941 int snd_info_register(struct snd_info_entry * entry)
943 struct proc_dir_entry *root, *p = NULL;
945 if (snd_BUG_ON(!entry))
947 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
948 mutex_lock(&info_mutex);
949 p = create_proc_entry(entry->name, entry->mode, root);
951 mutex_unlock(&info_mutex);
954 if (!S_ISDIR(entry->mode))
955 p->proc_fops = &snd_info_entry_operations;
956 p->size = entry->size;
960 list_add_tail(&entry->list, &entry->parent->children);
961 mutex_unlock(&info_mutex);
965 EXPORT_SYMBOL(snd_info_register);
971 static struct snd_info_entry *snd_info_version_entry;
973 static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
976 "Advanced Linux Sound Architecture Driver Version "
977 CONFIG_SND_VERSION CONFIG_SND_DATE ".\n"
981 static int __init snd_info_version_init(void)
983 struct snd_info_entry *entry;
985 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
988 entry->c.text.read = snd_info_version_read;
989 if (snd_info_register(entry) < 0) {
990 snd_info_free_entry(entry);
993 snd_info_version_entry = entry;
997 static int __exit snd_info_version_done(void)
999 snd_info_free_entry(snd_info_version_entry);
1003 #endif /* CONFIG_PROC_FS */