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 snd_assert(data != NULL, return -ENXIO);
222 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
224 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
227 switch (entry->content) {
228 case SNDRV_INFO_CONTENT_TEXT:
232 if (pos >= buf->size)
234 size = buf->size - pos;
235 size = min(count, size);
236 if (copy_to_user(buffer, buf->buffer + pos, size))
239 case SNDRV_INFO_CONTENT_DATA:
240 if (entry->c.ops->read)
241 size = entry->c.ops->read(entry,
242 data->file_private_data,
243 file, buffer, count, pos);
246 if ((ssize_t) size > 0)
247 *offset = pos + size;
251 static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
252 size_t count, loff_t * offset)
254 struct snd_info_private_data *data;
255 struct snd_info_entry *entry;
256 struct snd_info_buffer *buf;
260 data = file->private_data;
261 snd_assert(data != NULL, return -ENXIO);
264 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
266 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
268 switch (entry->content) {
269 case SNDRV_INFO_CONTENT_TEXT:
273 mutex_lock(&entry->access);
274 if (pos + count >= buf->len) {
275 if (resize_info_buffer(buf, pos + count)) {
276 mutex_unlock(&entry->access);
280 if (copy_from_user(buf->buffer + pos, buffer, count)) {
281 mutex_unlock(&entry->access);
284 buf->size = pos + count;
285 mutex_unlock(&entry->access);
288 case SNDRV_INFO_CONTENT_DATA:
289 if (entry->c.ops->write)
290 size = entry->c.ops->write(entry,
291 data->file_private_data,
292 file, buffer, count, pos);
295 if ((ssize_t) size > 0)
296 *offset = pos + size;
300 static int snd_info_entry_open(struct inode *inode, struct file *file)
302 struct snd_info_entry *entry;
303 struct snd_info_private_data *data;
304 struct snd_info_buffer *buffer;
305 struct proc_dir_entry *p;
308 mutex_lock(&info_mutex);
310 entry = p == NULL ? NULL : (struct snd_info_entry *)p->data;
311 if (entry == NULL || ! entry->p) {
312 mutex_unlock(&info_mutex);
315 if (!try_module_get(entry->module)) {
319 mode = file->f_flags & O_ACCMODE;
320 if (mode == O_RDONLY || mode == O_RDWR) {
321 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
322 entry->c.ops->read == NULL)) {
327 if (mode == O_WRONLY || mode == O_RDWR) {
328 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
329 entry->c.ops->write == NULL)) {
334 data = kzalloc(sizeof(*data), GFP_KERNEL);
340 switch (entry->content) {
341 case SNDRV_INFO_CONTENT_TEXT:
342 if (mode == O_RDONLY || mode == O_RDWR) {
343 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
346 data->rbuffer = buffer;
347 buffer->len = PAGE_SIZE;
348 buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
349 if (buffer->buffer == NULL)
352 if (mode == O_WRONLY || mode == O_RDWR) {
353 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
356 data->wbuffer = buffer;
357 buffer->len = PAGE_SIZE;
358 buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
359 if (buffer->buffer == NULL)
363 case SNDRV_INFO_CONTENT_DATA: /* data */
364 if (entry->c.ops->open) {
365 if ((err = entry->c.ops->open(entry, mode,
366 &data->file_private_data)) < 0) {
373 file->private_data = data;
374 mutex_unlock(&info_mutex);
375 if (entry->content == SNDRV_INFO_CONTENT_TEXT &&
376 (mode == O_RDONLY || mode == O_RDWR)) {
377 if (entry->c.text.read) {
378 mutex_lock(&entry->access);
379 entry->c.text.read(entry, data->rbuffer);
380 mutex_unlock(&entry->access);
387 kfree(data->rbuffer->buffer);
388 kfree(data->rbuffer);
391 kfree(data->wbuffer->buffer);
392 kfree(data->wbuffer);
397 module_put(entry->module);
399 mutex_unlock(&info_mutex);
403 static int snd_info_entry_release(struct inode *inode, struct file *file)
405 struct snd_info_entry *entry;
406 struct snd_info_private_data *data;
409 mode = file->f_flags & O_ACCMODE;
410 data = file->private_data;
412 switch (entry->content) {
413 case SNDRV_INFO_CONTENT_TEXT:
415 kfree(data->rbuffer->buffer);
416 kfree(data->rbuffer);
419 if (entry->c.text.write) {
420 entry->c.text.write(entry, data->wbuffer);
421 if (data->wbuffer->error) {
422 snd_printk(KERN_WARNING "data write error to %s (%i)\n",
424 data->wbuffer->error);
427 kfree(data->wbuffer->buffer);
428 kfree(data->wbuffer);
431 case SNDRV_INFO_CONTENT_DATA:
432 if (entry->c.ops->release)
433 entry->c.ops->release(entry, mode,
434 data->file_private_data);
437 module_put(entry->module);
442 static unsigned int snd_info_entry_poll(struct file *file, poll_table * wait)
444 struct snd_info_private_data *data;
445 struct snd_info_entry *entry;
448 data = file->private_data;
453 switch (entry->content) {
454 case SNDRV_INFO_CONTENT_DATA:
455 if (entry->c.ops->poll)
456 return entry->c.ops->poll(entry,
457 data->file_private_data,
459 if (entry->c.ops->read)
460 mask |= POLLIN | POLLRDNORM;
461 if (entry->c.ops->write)
462 mask |= POLLOUT | POLLWRNORM;
468 static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
471 struct snd_info_private_data *data;
472 struct snd_info_entry *entry;
474 data = file->private_data;
478 switch (entry->content) {
479 case SNDRV_INFO_CONTENT_DATA:
480 if (entry->c.ops->ioctl)
481 return entry->c.ops->ioctl(entry,
482 data->file_private_data,
489 static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
491 struct inode *inode = file->f_path.dentry->d_inode;
492 struct snd_info_private_data *data;
493 struct snd_info_entry *entry;
495 data = file->private_data;
499 switch (entry->content) {
500 case SNDRV_INFO_CONTENT_DATA:
501 if (entry->c.ops->mmap)
502 return entry->c.ops->mmap(entry,
503 data->file_private_data,
510 static const struct file_operations snd_info_entry_operations =
512 .owner = THIS_MODULE,
513 .llseek = snd_info_entry_llseek,
514 .read = snd_info_entry_read,
515 .write = snd_info_entry_write,
516 .poll = snd_info_entry_poll,
517 .unlocked_ioctl = snd_info_entry_ioctl,
518 .mmap = snd_info_entry_mmap,
519 .open = snd_info_entry_open,
520 .release = snd_info_entry_release,
524 * snd_create_proc_entry - create a procfs entry
525 * @name: the name of the proc file
526 * @mode: the file permission bits, S_Ixxx
527 * @parent: the parent proc-directory entry
529 * Creates a new proc file entry with the given name and permission
530 * on the given directory.
532 * Returns the pointer of new instance or NULL on failure.
534 static struct proc_dir_entry *snd_create_proc_entry(const char *name, mode_t mode,
535 struct proc_dir_entry *parent)
537 struct proc_dir_entry *p;
538 p = create_proc_entry(name, mode, parent);
540 snd_info_entry_prepare(p);
544 int __init snd_info_init(void)
546 struct proc_dir_entry *p;
548 p = snd_create_proc_entry("asound", S_IFDIR | S_IRUGO | S_IXUGO, NULL);
552 #ifdef CONFIG_SND_OSSEMUL
554 struct snd_info_entry *entry;
555 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
557 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
558 if (snd_info_register(entry) < 0) {
559 snd_info_free_entry(entry);
562 snd_oss_root = entry;
565 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
567 struct snd_info_entry *entry;
568 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL)
570 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
571 if (snd_info_register(entry) < 0) {
572 snd_info_free_entry(entry);
575 snd_seq_root = entry;
578 snd_info_version_init();
579 snd_minor_info_init();
580 snd_minor_info_oss_init();
581 snd_card_info_init();
585 int __exit snd_info_done(void)
587 snd_card_info_done();
588 snd_minor_info_oss_done();
589 snd_minor_info_done();
590 snd_info_version_done();
592 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
593 snd_info_free_entry(snd_seq_root);
595 #ifdef CONFIG_SND_OSSEMUL
596 snd_info_free_entry(snd_oss_root);
598 snd_remove_proc_entry(NULL, snd_proc_root);
609 * create a card proc file
612 int snd_info_card_create(struct snd_card *card)
615 struct snd_info_entry *entry;
617 snd_assert(card != NULL, return -ENXIO);
619 sprintf(str, "card%i", card->number);
620 if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
622 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
623 if (snd_info_register(entry) < 0) {
624 snd_info_free_entry(entry);
627 card->proc_root = entry;
632 * register the card proc file
635 int snd_info_card_register(struct snd_card *card)
637 struct proc_dir_entry *p;
639 snd_assert(card != NULL, return -ENXIO);
641 if (!strcmp(card->id, card->proc_root->name))
644 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
647 card->proc_root_link = p;
652 * de-register the card proc file
655 void snd_info_card_disconnect(struct snd_card *card)
657 snd_assert(card != NULL, return);
658 mutex_lock(&info_mutex);
659 if (card->proc_root_link) {
660 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
661 card->proc_root_link = NULL;
664 snd_info_disconnect(card->proc_root);
665 mutex_unlock(&info_mutex);
669 * release the card proc file resources
672 int snd_info_card_free(struct snd_card *card)
674 snd_assert(card != NULL, return -ENXIO);
675 snd_info_free_entry(card->proc_root);
676 card->proc_root = NULL;
682 * snd_info_get_line - read one line from the procfs buffer
683 * @buffer: the procfs buffer
684 * @line: the buffer to store
685 * @len: the max. buffer size - 1
687 * Reads one line from the buffer and stores the string.
689 * Returns zero if successful, or 1 if error or EOF.
691 int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
695 if (len <= 0 || buffer->stop || buffer->error)
698 c = buffer->buffer[buffer->curr++];
700 if (buffer->curr >= buffer->size)
705 if (buffer->curr >= buffer->size) {
710 while (c != '\n' && !buffer->stop) {
711 c = buffer->buffer[buffer->curr++];
712 if (buffer->curr >= buffer->size)
719 EXPORT_SYMBOL(snd_info_get_line);
722 * snd_info_get_str - parse a string token
723 * @dest: the buffer to store the string token
724 * @src: the original string
725 * @len: the max. length of token - 1
727 * Parses the original string and copy a token to the given
730 * Returns the updated pointer of the original string so that
731 * it can be used for the next call.
733 char *snd_info_get_str(char *dest, char *src, int len)
737 while (*src == ' ' || *src == '\t')
739 if (*src == '"' || *src == '\'') {
741 while (--len > 0 && *src && *src != c) {
747 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
752 while (*src == ' ' || *src == '\t')
757 EXPORT_SYMBOL(snd_info_get_str);
760 * snd_info_create_entry - create an info entry
761 * @name: the proc file name
763 * Creates an info entry with the given file name and initializes as
766 * Usually called from other functions such as
767 * snd_info_create_card_entry().
769 * Returns the pointer of the new instance, or NULL on failure.
771 static struct snd_info_entry *snd_info_create_entry(const char *name)
773 struct snd_info_entry *entry;
774 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
777 entry->name = kstrdup(name, GFP_KERNEL);
778 if (entry->name == NULL) {
782 entry->mode = S_IFREG | S_IRUGO;
783 entry->content = SNDRV_INFO_CONTENT_TEXT;
784 mutex_init(&entry->access);
785 INIT_LIST_HEAD(&entry->children);
786 INIT_LIST_HEAD(&entry->list);
791 * snd_info_create_module_entry - create an info entry for the given module
792 * @module: the module pointer
793 * @name: the file name
794 * @parent: the parent directory
796 * Creates a new info entry and assigns it to the given module.
798 * Returns the pointer of the new instance, or NULL on failure.
800 struct snd_info_entry *snd_info_create_module_entry(struct module * module,
802 struct snd_info_entry *parent)
804 struct snd_info_entry *entry = snd_info_create_entry(name);
806 entry->module = module;
807 entry->parent = parent;
812 EXPORT_SYMBOL(snd_info_create_module_entry);
815 * snd_info_create_card_entry - create an info entry for the given card
816 * @card: the card instance
817 * @name: the file name
818 * @parent: the parent directory
820 * Creates a new info entry and assigns it to the given card.
822 * Returns the pointer of the new instance, or NULL on failure.
824 struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
826 struct snd_info_entry * parent)
828 struct snd_info_entry *entry = snd_info_create_entry(name);
830 entry->module = card->module;
832 entry->parent = parent;
837 EXPORT_SYMBOL(snd_info_create_card_entry);
839 static void snd_info_disconnect(struct snd_info_entry *entry)
841 struct list_head *p, *n;
842 struct proc_dir_entry *root;
844 list_for_each_safe(p, n, &entry->children) {
845 snd_info_disconnect(list_entry(p, struct snd_info_entry, list));
850 list_del_init(&entry->list);
851 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
852 snd_assert(root, return);
853 snd_remove_proc_entry(root, entry->p);
857 static int snd_info_dev_free_entry(struct snd_device *device)
859 struct snd_info_entry *entry = device->device_data;
860 snd_info_free_entry(entry);
864 static int snd_info_dev_register_entry(struct snd_device *device)
866 struct snd_info_entry *entry = device->device_data;
867 return snd_info_register(entry);
871 * snd_card_proc_new - create an info entry for the given card
872 * @card: the card instance
873 * @name: the file name
874 * @entryp: the pointer to store the new info entry
876 * Creates a new info entry and assigns it to the given card.
877 * Unlike snd_info_create_card_entry(), this function registers the
878 * info entry as an ALSA device component, so that it can be
879 * unregistered/released without explicit call.
880 * Also, you don't have to register this entry via snd_info_register(),
881 * since this will be registered by snd_card_register() automatically.
883 * The parent is assumed as card->proc_root.
885 * For releasing this entry, use snd_device_free() instead of
886 * snd_info_free_entry().
888 * Returns zero if successful, or a negative error code on failure.
890 int snd_card_proc_new(struct snd_card *card, const char *name,
891 struct snd_info_entry **entryp)
893 static struct snd_device_ops ops = {
894 .dev_free = snd_info_dev_free_entry,
895 .dev_register = snd_info_dev_register_entry,
896 /* disconnect is done via snd_info_card_disconnect() */
898 struct snd_info_entry *entry;
901 entry = snd_info_create_card_entry(card, name, card->proc_root);
904 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
905 snd_info_free_entry(entry);
913 EXPORT_SYMBOL(snd_card_proc_new);
916 * snd_info_free_entry - release the info entry
917 * @entry: the info entry
919 * Releases the info entry. Don't call this after registered.
921 void snd_info_free_entry(struct snd_info_entry * entry)
926 mutex_lock(&info_mutex);
927 snd_info_disconnect(entry);
928 mutex_unlock(&info_mutex);
931 if (entry->private_free)
932 entry->private_free(entry);
936 EXPORT_SYMBOL(snd_info_free_entry);
939 * snd_info_register - register the info entry
940 * @entry: the info entry
942 * Registers the proc info entry.
944 * Returns zero if successful, or a negative error code on failure.
946 int snd_info_register(struct snd_info_entry * entry)
948 struct proc_dir_entry *root, *p = NULL;
950 snd_assert(entry != NULL, return -ENXIO);
951 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
952 mutex_lock(&info_mutex);
953 p = snd_create_proc_entry(entry->name, entry->mode, root);
955 mutex_unlock(&info_mutex);
958 p->owner = entry->module;
959 if (!S_ISDIR(entry->mode))
960 p->proc_fops = &snd_info_entry_operations;
961 p->size = entry->size;
965 list_add_tail(&entry->list, &entry->parent->children);
966 mutex_unlock(&info_mutex);
970 EXPORT_SYMBOL(snd_info_register);
976 static struct snd_info_entry *snd_info_version_entry;
978 static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
981 "Advanced Linux Sound Architecture Driver Version "
982 CONFIG_SND_VERSION CONFIG_SND_DATE ".\n"
986 static int __init snd_info_version_init(void)
988 struct snd_info_entry *entry;
990 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
993 entry->c.text.read = snd_info_version_read;
994 if (snd_info_register(entry) < 0) {
995 snd_info_free_entry(entry);
998 snd_info_version_entry = entry;
1002 static int __exit snd_info_version_done(void)
1004 snd_info_free_entry(snd_info_version_entry);
1008 #endif /* CONFIG_PROC_FS */