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>
24 #include <linux/smp_lock.h>
25 #include <linux/string.h>
26 #include <sound/core.h>
27 #include <sound/minors.h>
28 #include <sound/info.h>
29 #include <sound/version.h>
30 #include <linux/proc_fs.h>
31 #include <linux/mutex.h>
40 int snd_info_check_reserved_words(const char *str)
42 static char *reserved[] =
57 char **xstr = reserved;
60 if (!strcmp(*xstr, str))
64 if (!strncmp(str, "card", 4))
69 static DEFINE_MUTEX(info_mutex);
71 struct snd_info_private_data {
72 struct snd_info_buffer *rbuffer;
73 struct snd_info_buffer *wbuffer;
74 struct snd_info_entry *entry;
75 void *file_private_data;
78 static int snd_info_version_init(void);
79 static int snd_info_version_done(void);
80 static void snd_info_disconnect(struct snd_info_entry *entry);
83 /* resize the proc r/w buffer */
84 static int resize_info_buffer(struct snd_info_buffer *buffer,
89 nsize = PAGE_ALIGN(nsize);
90 nbuf = kmalloc(nsize, GFP_KERNEL);
94 memcpy(nbuf, buffer->buffer, buffer->len);
95 kfree(buffer->buffer);
96 buffer->buffer = nbuf;
102 * snd_iprintf - printf on the procfs buffer
103 * @buffer: the procfs buffer
104 * @fmt: the printf format
106 * Outputs the string on the procfs buffer just like printf().
108 * Returns the size of output string.
110 int snd_iprintf(struct snd_info_buffer *buffer, char *fmt,...)
117 if (buffer->stop || buffer->error)
119 len = buffer->len - buffer->size;
124 res = vsnprintf(buffer->buffer + buffer->curr, len, fmt, ap);
128 err = resize_info_buffer(buffer, buffer->len + PAGE_SIZE);
131 len = buffer->len - buffer->size;
142 EXPORT_SYMBOL(snd_iprintf);
148 static struct proc_dir_entry *snd_proc_root;
149 struct snd_info_entry *snd_seq_root;
150 EXPORT_SYMBOL(snd_seq_root);
152 #ifdef CONFIG_SND_OSSEMUL
153 struct snd_info_entry *snd_oss_root;
156 static inline void snd_info_entry_prepare(struct proc_dir_entry *de)
158 de->owner = THIS_MODULE;
161 static void snd_remove_proc_entry(struct proc_dir_entry *parent,
162 struct proc_dir_entry *de)
165 remove_proc_entry(de->name, parent);
168 static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
170 struct snd_info_private_data *data;
171 struct snd_info_entry *entry;
174 data = file->private_data;
177 switch (entry->content) {
178 case SNDRV_INFO_CONTENT_TEXT:
181 file->f_pos = offset;
185 file->f_pos += offset;
194 case SNDRV_INFO_CONTENT_DATA:
195 if (entry->c.ops->llseek) {
196 ret = entry->c.ops->llseek(entry,
197 data->file_private_data,
209 static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
210 size_t count, loff_t * offset)
212 struct snd_info_private_data *data;
213 struct snd_info_entry *entry;
214 struct snd_info_buffer *buf;
218 data = file->private_data;
219 snd_assert(data != NULL, return -ENXIO);
221 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
223 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
226 switch (entry->content) {
227 case SNDRV_INFO_CONTENT_TEXT:
231 if (pos >= buf->size)
233 size = buf->size - pos;
234 size = min(count, size);
235 if (copy_to_user(buffer, buf->buffer + pos, size))
238 case SNDRV_INFO_CONTENT_DATA:
239 if (entry->c.ops->read)
240 size = entry->c.ops->read(entry,
241 data->file_private_data,
242 file, buffer, count, pos);
245 if ((ssize_t) size > 0)
246 *offset = pos + size;
250 static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
251 size_t count, loff_t * offset)
253 struct snd_info_private_data *data;
254 struct snd_info_entry *entry;
255 struct snd_info_buffer *buf;
259 data = file->private_data;
260 snd_assert(data != NULL, return -ENXIO);
263 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
265 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
267 switch (entry->content) {
268 case SNDRV_INFO_CONTENT_TEXT:
272 mutex_lock(&entry->access);
273 if (pos + count >= buf->len) {
274 if (resize_info_buffer(buf, pos + count)) {
275 mutex_unlock(&entry->access);
279 if (copy_from_user(buf->buffer + pos, buffer, count)) {
280 mutex_unlock(&entry->access);
283 buf->size = pos + count;
284 mutex_unlock(&entry->access);
287 case SNDRV_INFO_CONTENT_DATA:
288 if (entry->c.ops->write)
289 size = entry->c.ops->write(entry,
290 data->file_private_data,
291 file, buffer, count, pos);
294 if ((ssize_t) size > 0)
295 *offset = pos + size;
299 static int snd_info_entry_open(struct inode *inode, struct file *file)
301 struct snd_info_entry *entry;
302 struct snd_info_private_data *data;
303 struct snd_info_buffer *buffer;
304 struct proc_dir_entry *p;
307 mutex_lock(&info_mutex);
309 entry = p == NULL ? NULL : (struct snd_info_entry *)p->data;
310 if (entry == NULL || ! entry->p) {
311 mutex_unlock(&info_mutex);
314 if (!try_module_get(entry->module)) {
318 mode = file->f_flags & O_ACCMODE;
319 if (mode == O_RDONLY || mode == O_RDWR) {
320 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
321 entry->c.ops->read == NULL)) {
326 if (mode == O_WRONLY || mode == O_RDWR) {
327 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
328 entry->c.ops->write == NULL)) {
333 data = kzalloc(sizeof(*data), GFP_KERNEL);
339 switch (entry->content) {
340 case SNDRV_INFO_CONTENT_TEXT:
341 if (mode == O_RDONLY || mode == O_RDWR) {
342 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
345 data->rbuffer = buffer;
346 buffer->len = PAGE_SIZE;
347 buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
348 if (buffer->buffer == NULL)
351 if (mode == O_WRONLY || mode == O_RDWR) {
352 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
355 data->wbuffer = buffer;
356 buffer->len = PAGE_SIZE;
357 buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
358 if (buffer->buffer == NULL)
362 case SNDRV_INFO_CONTENT_DATA: /* data */
363 if (entry->c.ops->open) {
364 if ((err = entry->c.ops->open(entry, mode,
365 &data->file_private_data)) < 0) {
372 file->private_data = data;
373 mutex_unlock(&info_mutex);
374 if (entry->content == SNDRV_INFO_CONTENT_TEXT &&
375 (mode == O_RDONLY || mode == O_RDWR)) {
376 if (entry->c.text.read) {
377 mutex_lock(&entry->access);
378 entry->c.text.read(entry, data->rbuffer);
379 mutex_unlock(&entry->access);
386 kfree(data->rbuffer->buffer);
387 kfree(data->rbuffer);
390 kfree(data->wbuffer->buffer);
391 kfree(data->wbuffer);
396 module_put(entry->module);
398 mutex_unlock(&info_mutex);
402 static int snd_info_entry_release(struct inode *inode, struct file *file)
404 struct snd_info_entry *entry;
405 struct snd_info_private_data *data;
408 mode = file->f_flags & O_ACCMODE;
409 data = file->private_data;
411 switch (entry->content) {
412 case SNDRV_INFO_CONTENT_TEXT:
414 kfree(data->rbuffer->buffer);
415 kfree(data->rbuffer);
418 if (entry->c.text.write) {
419 entry->c.text.write(entry, data->wbuffer);
420 if (data->wbuffer->error) {
421 snd_printk(KERN_WARNING "data write error to %s (%i)\n",
423 data->wbuffer->error);
426 kfree(data->wbuffer->buffer);
427 kfree(data->wbuffer);
430 case SNDRV_INFO_CONTENT_DATA:
431 if (entry->c.ops->release)
432 entry->c.ops->release(entry, mode,
433 data->file_private_data);
436 module_put(entry->module);
441 static unsigned int snd_info_entry_poll(struct file *file, poll_table * wait)
443 struct snd_info_private_data *data;
444 struct snd_info_entry *entry;
447 data = file->private_data;
452 switch (entry->content) {
453 case SNDRV_INFO_CONTENT_DATA:
454 if (entry->c.ops->poll)
455 return entry->c.ops->poll(entry,
456 data->file_private_data,
458 if (entry->c.ops->read)
459 mask |= POLLIN | POLLRDNORM;
460 if (entry->c.ops->write)
461 mask |= POLLOUT | POLLWRNORM;
467 static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
470 struct snd_info_private_data *data;
471 struct snd_info_entry *entry;
473 data = file->private_data;
477 switch (entry->content) {
478 case SNDRV_INFO_CONTENT_DATA:
479 if (entry->c.ops->ioctl)
480 return entry->c.ops->ioctl(entry,
481 data->file_private_data,
488 static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
490 struct inode *inode = file->f_path.dentry->d_inode;
491 struct snd_info_private_data *data;
492 struct snd_info_entry *entry;
494 data = file->private_data;
498 switch (entry->content) {
499 case SNDRV_INFO_CONTENT_DATA:
500 if (entry->c.ops->mmap)
501 return entry->c.ops->mmap(entry,
502 data->file_private_data,
509 static const struct file_operations snd_info_entry_operations =
511 .owner = THIS_MODULE,
512 .llseek = snd_info_entry_llseek,
513 .read = snd_info_entry_read,
514 .write = snd_info_entry_write,
515 .poll = snd_info_entry_poll,
516 .unlocked_ioctl = snd_info_entry_ioctl,
517 .mmap = snd_info_entry_mmap,
518 .open = snd_info_entry_open,
519 .release = snd_info_entry_release,
523 * snd_create_proc_entry - create a procfs entry
524 * @name: the name of the proc file
525 * @mode: the file permission bits, S_Ixxx
526 * @parent: the parent proc-directory entry
528 * Creates a new proc file entry with the given name and permission
529 * on the given directory.
531 * Returns the pointer of new instance or NULL on failure.
533 static struct proc_dir_entry *snd_create_proc_entry(const char *name, mode_t mode,
534 struct proc_dir_entry *parent)
536 struct proc_dir_entry *p;
537 p = create_proc_entry(name, mode, parent);
539 snd_info_entry_prepare(p);
543 int __init snd_info_init(void)
545 struct proc_dir_entry *p;
547 p = snd_create_proc_entry("asound", S_IFDIR | S_IRUGO | S_IXUGO, NULL);
551 #ifdef CONFIG_SND_OSSEMUL
553 struct snd_info_entry *entry;
554 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
556 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
557 if (snd_info_register(entry) < 0) {
558 snd_info_free_entry(entry);
561 snd_oss_root = entry;
564 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
566 struct snd_info_entry *entry;
567 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL)
569 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
570 if (snd_info_register(entry) < 0) {
571 snd_info_free_entry(entry);
574 snd_seq_root = entry;
577 snd_info_version_init();
578 snd_minor_info_init();
579 snd_minor_info_oss_init();
580 snd_card_info_init();
584 int __exit snd_info_done(void)
586 snd_card_info_done();
587 snd_minor_info_oss_done();
588 snd_minor_info_done();
589 snd_info_version_done();
591 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
592 snd_info_free_entry(snd_seq_root);
594 #ifdef CONFIG_SND_OSSEMUL
595 snd_info_free_entry(snd_oss_root);
597 snd_remove_proc_entry(NULL, snd_proc_root);
608 * create a card proc file
611 int snd_info_card_create(struct snd_card *card)
614 struct snd_info_entry *entry;
616 snd_assert(card != NULL, return -ENXIO);
618 sprintf(str, "card%i", card->number);
619 if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
621 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
622 if (snd_info_register(entry) < 0) {
623 snd_info_free_entry(entry);
626 card->proc_root = entry;
631 * register the card proc file
634 int snd_info_card_register(struct snd_card *card)
636 struct proc_dir_entry *p;
638 snd_assert(card != NULL, return -ENXIO);
640 if (!strcmp(card->id, card->proc_root->name))
643 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
646 card->proc_root_link = p;
651 * de-register the card proc file
654 void snd_info_card_disconnect(struct snd_card *card)
656 snd_assert(card != NULL, return);
657 mutex_lock(&info_mutex);
658 if (card->proc_root_link) {
659 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
660 card->proc_root_link = NULL;
663 snd_info_disconnect(card->proc_root);
664 mutex_unlock(&info_mutex);
668 * release the card proc file resources
671 int snd_info_card_free(struct snd_card *card)
673 snd_assert(card != NULL, return -ENXIO);
674 snd_info_free_entry(card->proc_root);
675 card->proc_root = NULL;
681 * snd_info_get_line - read one line from the procfs buffer
682 * @buffer: the procfs buffer
683 * @line: the buffer to store
684 * @len: the max. buffer size - 1
686 * Reads one line from the buffer and stores the string.
688 * Returns zero if successful, or 1 if error or EOF.
690 int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
694 if (len <= 0 || buffer->stop || buffer->error)
697 c = buffer->buffer[buffer->curr++];
699 if (buffer->curr >= buffer->size)
704 if (buffer->curr >= buffer->size) {
709 while (c != '\n' && !buffer->stop) {
710 c = buffer->buffer[buffer->curr++];
711 if (buffer->curr >= buffer->size)
718 EXPORT_SYMBOL(snd_info_get_line);
721 * snd_info_get_str - parse a string token
722 * @dest: the buffer to store the string token
723 * @src: the original string
724 * @len: the max. length of token - 1
726 * Parses the original string and copy a token to the given
729 * Returns the updated pointer of the original string so that
730 * it can be used for the next call.
732 char *snd_info_get_str(char *dest, char *src, int len)
736 while (*src == ' ' || *src == '\t')
738 if (*src == '"' || *src == '\'') {
740 while (--len > 0 && *src && *src != c) {
746 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
751 while (*src == ' ' || *src == '\t')
756 EXPORT_SYMBOL(snd_info_get_str);
759 * snd_info_create_entry - create an info entry
760 * @name: the proc file name
762 * Creates an info entry with the given file name and initializes as
765 * Usually called from other functions such as
766 * snd_info_create_card_entry().
768 * Returns the pointer of the new instance, or NULL on failure.
770 static struct snd_info_entry *snd_info_create_entry(const char *name)
772 struct snd_info_entry *entry;
773 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
776 entry->name = kstrdup(name, GFP_KERNEL);
777 if (entry->name == NULL) {
781 entry->mode = S_IFREG | S_IRUGO;
782 entry->content = SNDRV_INFO_CONTENT_TEXT;
783 mutex_init(&entry->access);
784 INIT_LIST_HEAD(&entry->children);
785 INIT_LIST_HEAD(&entry->list);
790 * snd_info_create_module_entry - create an info entry for the given module
791 * @module: the module pointer
792 * @name: the file name
793 * @parent: the parent directory
795 * Creates a new info entry and assigns it to the given module.
797 * Returns the pointer of the new instance, or NULL on failure.
799 struct snd_info_entry *snd_info_create_module_entry(struct module * module,
801 struct snd_info_entry *parent)
803 struct snd_info_entry *entry = snd_info_create_entry(name);
805 entry->module = module;
806 entry->parent = parent;
811 EXPORT_SYMBOL(snd_info_create_module_entry);
814 * snd_info_create_card_entry - create an info entry for the given card
815 * @card: the card instance
816 * @name: the file name
817 * @parent: the parent directory
819 * Creates a new info entry and assigns it to the given card.
821 * Returns the pointer of the new instance, or NULL on failure.
823 struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
825 struct snd_info_entry * parent)
827 struct snd_info_entry *entry = snd_info_create_entry(name);
829 entry->module = card->module;
831 entry->parent = parent;
836 EXPORT_SYMBOL(snd_info_create_card_entry);
838 static void snd_info_disconnect(struct snd_info_entry *entry)
840 struct list_head *p, *n;
841 struct proc_dir_entry *root;
843 list_for_each_safe(p, n, &entry->children) {
844 snd_info_disconnect(list_entry(p, struct snd_info_entry, list));
849 list_del_init(&entry->list);
850 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
851 snd_assert(root, return);
852 snd_remove_proc_entry(root, entry->p);
856 static int snd_info_dev_free_entry(struct snd_device *device)
858 struct snd_info_entry *entry = device->device_data;
859 snd_info_free_entry(entry);
863 static int snd_info_dev_register_entry(struct snd_device *device)
865 struct snd_info_entry *entry = device->device_data;
866 return snd_info_register(entry);
870 * snd_card_proc_new - create an info entry for the given card
871 * @card: the card instance
872 * @name: the file name
873 * @entryp: the pointer to store the new info entry
875 * Creates a new info entry and assigns it to the given card.
876 * Unlike snd_info_create_card_entry(), this function registers the
877 * info entry as an ALSA device component, so that it can be
878 * unregistered/released without explicit call.
879 * Also, you don't have to register this entry via snd_info_register(),
880 * since this will be registered by snd_card_register() automatically.
882 * The parent is assumed as card->proc_root.
884 * For releasing this entry, use snd_device_free() instead of
885 * snd_info_free_entry().
887 * Returns zero if successful, or a negative error code on failure.
889 int snd_card_proc_new(struct snd_card *card, const char *name,
890 struct snd_info_entry **entryp)
892 static struct snd_device_ops ops = {
893 .dev_free = snd_info_dev_free_entry,
894 .dev_register = snd_info_dev_register_entry,
895 /* disconnect is done via snd_info_card_disconnect() */
897 struct snd_info_entry *entry;
900 entry = snd_info_create_card_entry(card, name, card->proc_root);
903 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
904 snd_info_free_entry(entry);
912 EXPORT_SYMBOL(snd_card_proc_new);
915 * snd_info_free_entry - release the info entry
916 * @entry: the info entry
918 * Releases the info entry. Don't call this after registered.
920 void snd_info_free_entry(struct snd_info_entry * entry)
925 mutex_lock(&info_mutex);
926 snd_info_disconnect(entry);
927 mutex_unlock(&info_mutex);
930 if (entry->private_free)
931 entry->private_free(entry);
935 EXPORT_SYMBOL(snd_info_free_entry);
938 * snd_info_register - register the info entry
939 * @entry: the info entry
941 * Registers the proc info entry.
943 * Returns zero if successful, or a negative error code on failure.
945 int snd_info_register(struct snd_info_entry * entry)
947 struct proc_dir_entry *root, *p = NULL;
949 snd_assert(entry != NULL, return -ENXIO);
950 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
951 mutex_lock(&info_mutex);
952 p = snd_create_proc_entry(entry->name, entry->mode, root);
954 mutex_unlock(&info_mutex);
957 p->owner = entry->module;
958 if (!S_ISDIR(entry->mode))
959 p->proc_fops = &snd_info_entry_operations;
960 p->size = entry->size;
964 list_add_tail(&entry->list, &entry->parent->children);
965 mutex_unlock(&info_mutex);
969 EXPORT_SYMBOL(snd_info_register);
975 static struct snd_info_entry *snd_info_version_entry;
977 static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
980 "Advanced Linux Sound Architecture Driver Version "
981 CONFIG_SND_VERSION CONFIG_SND_DATE ".\n"
985 static int __init snd_info_version_init(void)
987 struct snd_info_entry *entry;
989 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
992 entry->c.text.read = snd_info_version_read;
993 if (snd_info_register(entry) < 0) {
994 snd_info_free_entry(entry);
997 snd_info_version_entry = entry;
1001 static int __exit snd_info_version_done(void)
1003 snd_info_free_entry(snd_info_version_entry);
1007 #endif /* CONFIG_PROC_FS */