2 * Information interface for ALSA driver
3 * Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/vmalloc.h>
25 #include <linux/time.h>
26 #include <linux/smp_lock.h>
27 #include <linux/string.h>
28 #include <sound/core.h>
29 #include <sound/minors.h>
30 #include <sound/info.h>
31 #include <sound/version.h>
32 #include <linux/proc_fs.h>
33 #include <linux/devfs_fs_kernel.h>
34 #include <linux/mutex.h>
43 int snd_info_check_reserved_words(const char *str)
45 static char *reserved[] =
60 char **xstr = reserved;
63 if (!strcmp(*xstr, str))
67 if (!strncmp(str, "card", 4))
72 static DEFINE_MUTEX(info_mutex);
74 struct snd_info_private_data {
75 struct snd_info_buffer *rbuffer;
76 struct snd_info_buffer *wbuffer;
77 struct snd_info_entry *entry;
78 void *file_private_data;
81 static int snd_info_version_init(void);
82 static int snd_info_version_done(void);
86 * snd_iprintf - printf on the procfs buffer
87 * @buffer: the procfs buffer
88 * @fmt: the printf format
90 * Outputs the string on the procfs buffer just like printf().
92 * Returns the size of output string.
94 int snd_iprintf(struct snd_info_buffer *buffer, char *fmt,...)
99 if (buffer->stop || buffer->error)
101 len = buffer->len - buffer->size;
103 res = vsnprintf(buffer->curr, len, fmt, args);
114 EXPORT_SYMBOL(snd_iprintf);
120 static struct proc_dir_entry *snd_proc_root = NULL;
121 struct snd_info_entry *snd_seq_root = NULL;
122 EXPORT_SYMBOL(snd_seq_root);
124 #ifdef CONFIG_SND_OSSEMUL
125 struct snd_info_entry *snd_oss_root = NULL;
128 static inline void snd_info_entry_prepare(struct proc_dir_entry *de)
130 de->owner = THIS_MODULE;
133 static void snd_remove_proc_entry(struct proc_dir_entry *parent,
134 struct proc_dir_entry *de)
137 remove_proc_entry(de->name, parent);
140 static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
142 struct snd_info_private_data *data;
143 struct snd_info_entry *entry;
146 data = file->private_data;
149 switch (entry->content) {
150 case SNDRV_INFO_CONTENT_TEXT:
152 case 0: /* SEEK_SET */
153 file->f_pos = offset;
156 case 1: /* SEEK_CUR */
157 file->f_pos += offset;
160 case 2: /* SEEK_END */
166 case SNDRV_INFO_CONTENT_DATA:
167 if (entry->c.ops->llseek) {
168 ret = entry->c.ops->llseek(entry,
169 data->file_private_data,
181 static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
182 size_t count, loff_t * offset)
184 struct snd_info_private_data *data;
185 struct snd_info_entry *entry;
186 struct snd_info_buffer *buf;
190 data = file->private_data;
191 snd_assert(data != NULL, return -ENXIO);
193 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
195 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
198 switch (entry->content) {
199 case SNDRV_INFO_CONTENT_TEXT:
203 if (pos >= buf->size)
205 size = buf->size - pos;
206 size = min(count, size);
207 if (copy_to_user(buffer, buf->buffer + pos, size))
210 case SNDRV_INFO_CONTENT_DATA:
211 if (entry->c.ops->read)
212 size = entry->c.ops->read(entry,
213 data->file_private_data,
214 file, buffer, count, pos);
217 if ((ssize_t) size > 0)
218 *offset = pos + size;
222 static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
223 size_t count, loff_t * offset)
225 struct snd_info_private_data *data;
226 struct snd_info_entry *entry;
227 struct snd_info_buffer *buf;
231 data = file->private_data;
232 snd_assert(data != NULL, return -ENXIO);
235 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
237 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
239 switch (entry->content) {
240 case SNDRV_INFO_CONTENT_TEXT:
246 size = buf->len - pos;
247 size = min(count, size);
248 if (copy_from_user(buf->buffer + pos, buffer, size))
250 if ((long)buf->size < pos + size)
251 buf->size = pos + size;
253 case SNDRV_INFO_CONTENT_DATA:
254 if (entry->c.ops->write)
255 size = entry->c.ops->write(entry,
256 data->file_private_data,
257 file, buffer, count, pos);
260 if ((ssize_t) size > 0)
261 *offset = pos + size;
265 static int snd_info_entry_open(struct inode *inode, struct file *file)
267 struct snd_info_entry *entry;
268 struct snd_info_private_data *data;
269 struct snd_info_buffer *buffer;
270 struct proc_dir_entry *p;
273 mutex_lock(&info_mutex);
275 entry = p == NULL ? NULL : (struct snd_info_entry *)p->data;
276 if (entry == NULL || entry->disconnected) {
277 mutex_unlock(&info_mutex);
280 if (!try_module_get(entry->module)) {
284 mode = file->f_flags & O_ACCMODE;
285 if (mode == O_RDONLY || mode == O_RDWR) {
286 if ((entry->content == SNDRV_INFO_CONTENT_TEXT &&
287 !entry->c.text.read_size) ||
288 (entry->content == SNDRV_INFO_CONTENT_DATA &&
289 entry->c.ops->read == NULL)) {
294 if (mode == O_WRONLY || mode == O_RDWR) {
295 if ((entry->content == SNDRV_INFO_CONTENT_TEXT &&
296 !entry->c.text.write_size) ||
297 (entry->content == SNDRV_INFO_CONTENT_DATA &&
298 entry->c.ops->write == NULL)) {
303 data = kzalloc(sizeof(*data), GFP_KERNEL);
309 switch (entry->content) {
310 case SNDRV_INFO_CONTENT_TEXT:
311 if (mode == O_RDONLY || mode == O_RDWR) {
312 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
313 if (buffer == NULL) {
318 buffer->len = (entry->c.text.read_size +
319 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
320 buffer->buffer = vmalloc(buffer->len);
321 if (buffer->buffer == NULL) {
327 buffer->curr = buffer->buffer;
328 data->rbuffer = buffer;
330 if (mode == O_WRONLY || mode == O_RDWR) {
331 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
332 if (buffer == NULL) {
333 if (mode == O_RDWR) {
334 vfree(data->rbuffer->buffer);
335 kfree(data->rbuffer);
341 buffer->len = (entry->c.text.write_size +
342 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
343 buffer->buffer = vmalloc(buffer->len);
344 if (buffer->buffer == NULL) {
345 if (mode == O_RDWR) {
346 vfree(data->rbuffer->buffer);
347 kfree(data->rbuffer);
354 buffer->curr = buffer->buffer;
355 data->wbuffer = buffer;
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);
381 module_put(entry->module);
383 mutex_unlock(&info_mutex);
387 static int snd_info_entry_release(struct inode *inode, struct file *file)
389 struct snd_info_entry *entry;
390 struct snd_info_private_data *data;
393 mode = file->f_flags & O_ACCMODE;
394 data = file->private_data;
396 switch (entry->content) {
397 case SNDRV_INFO_CONTENT_TEXT:
398 if (mode == O_RDONLY || mode == O_RDWR) {
399 vfree(data->rbuffer->buffer);
400 kfree(data->rbuffer);
402 if (mode == O_WRONLY || mode == O_RDWR) {
403 if (entry->c.text.write) {
404 entry->c.text.write(entry, data->wbuffer);
405 if (data->wbuffer->error) {
406 snd_printk(KERN_WARNING "data write error to %s (%i)\n",
408 data->wbuffer->error);
411 vfree(data->wbuffer->buffer);
412 kfree(data->wbuffer);
415 case SNDRV_INFO_CONTENT_DATA:
416 if (entry->c.ops->release)
417 entry->c.ops->release(entry, mode,
418 data->file_private_data);
421 module_put(entry->module);
426 static unsigned int snd_info_entry_poll(struct file *file, poll_table * wait)
428 struct snd_info_private_data *data;
429 struct snd_info_entry *entry;
432 data = file->private_data;
437 switch (entry->content) {
438 case SNDRV_INFO_CONTENT_DATA:
439 if (entry->c.ops->poll)
440 return entry->c.ops->poll(entry,
441 data->file_private_data,
443 if (entry->c.ops->read)
444 mask |= POLLIN | POLLRDNORM;
445 if (entry->c.ops->write)
446 mask |= POLLOUT | POLLWRNORM;
452 static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
455 struct snd_info_private_data *data;
456 struct snd_info_entry *entry;
458 data = file->private_data;
462 switch (entry->content) {
463 case SNDRV_INFO_CONTENT_DATA:
464 if (entry->c.ops->ioctl)
465 return entry->c.ops->ioctl(entry,
466 data->file_private_data,
473 static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
475 struct inode *inode = file->f_dentry->d_inode;
476 struct snd_info_private_data *data;
477 struct snd_info_entry *entry;
479 data = file->private_data;
483 switch (entry->content) {
484 case SNDRV_INFO_CONTENT_DATA:
485 if (entry->c.ops->mmap)
486 return entry->c.ops->mmap(entry,
487 data->file_private_data,
494 static struct file_operations snd_info_entry_operations =
496 .owner = THIS_MODULE,
497 .llseek = snd_info_entry_llseek,
498 .read = snd_info_entry_read,
499 .write = snd_info_entry_write,
500 .poll = snd_info_entry_poll,
501 .unlocked_ioctl = snd_info_entry_ioctl,
502 .mmap = snd_info_entry_mmap,
503 .open = snd_info_entry_open,
504 .release = snd_info_entry_release,
508 * snd_create_proc_entry - create a procfs entry
509 * @name: the name of the proc file
510 * @mode: the file permission bits, S_Ixxx
511 * @parent: the parent proc-directory entry
513 * Creates a new proc file entry with the given name and permission
514 * on the given directory.
516 * Returns the pointer of new instance or NULL on failure.
518 static struct proc_dir_entry *snd_create_proc_entry(const char *name, mode_t mode,
519 struct proc_dir_entry *parent)
521 struct proc_dir_entry *p;
522 p = create_proc_entry(name, mode, parent);
524 snd_info_entry_prepare(p);
528 int __init snd_info_init(void)
530 struct proc_dir_entry *p;
532 p = snd_create_proc_entry("asound", S_IFDIR | S_IRUGO | S_IXUGO, &proc_root);
536 #ifdef CONFIG_SND_OSSEMUL
538 struct snd_info_entry *entry;
539 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
541 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
542 if (snd_info_register(entry) < 0) {
543 snd_info_free_entry(entry);
546 snd_oss_root = entry;
549 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
551 struct snd_info_entry *entry;
552 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL)
554 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
555 if (snd_info_register(entry) < 0) {
556 snd_info_free_entry(entry);
559 snd_seq_root = entry;
562 snd_info_version_init();
563 snd_minor_info_init();
564 snd_minor_info_oss_init();
565 snd_card_info_init();
569 int __exit snd_info_done(void)
571 snd_card_info_done();
572 snd_minor_info_oss_done();
573 snd_minor_info_done();
574 snd_info_version_done();
576 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
577 snd_info_unregister(snd_seq_root);
579 #ifdef CONFIG_SND_OSSEMUL
580 snd_info_unregister(snd_oss_root);
582 snd_remove_proc_entry(&proc_root, snd_proc_root);
593 * create a card proc file
596 int snd_info_card_create(struct snd_card *card)
599 struct snd_info_entry *entry;
601 snd_assert(card != NULL, return -ENXIO);
603 sprintf(str, "card%i", card->number);
604 if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
606 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
607 if (snd_info_register(entry) < 0) {
608 snd_info_free_entry(entry);
611 card->proc_root = entry;
616 * register the card proc file
619 int snd_info_card_register(struct snd_card *card)
621 struct proc_dir_entry *p;
623 snd_assert(card != NULL, return -ENXIO);
625 if (!strcmp(card->id, card->proc_root->name))
628 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
631 card->proc_root_link = p;
636 * de-register the card proc file
639 int snd_info_card_free(struct snd_card *card)
641 snd_assert(card != NULL, return -ENXIO);
642 if (card->proc_root_link) {
643 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
644 card->proc_root_link = NULL;
646 if (card->proc_root) {
647 snd_info_unregister(card->proc_root);
648 card->proc_root = NULL;
655 * snd_info_get_line - read one line from the procfs buffer
656 * @buffer: the procfs buffer
657 * @line: the buffer to store
658 * @len: the max. buffer size - 1
660 * Reads one line from the buffer and stores the string.
662 * Returns zero if successful, or 1 if error or EOF.
664 int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
668 if (len <= 0 || buffer->stop || buffer->error)
673 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
679 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
684 while (c != '\n' && !buffer->stop) {
686 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
694 EXPORT_SYMBOL(snd_info_get_line);
697 * snd_info_get_str - parse a string token
698 * @dest: the buffer to store the string token
699 * @src: the original string
700 * @len: the max. length of token - 1
702 * Parses the original string and copy a token to the given
705 * Returns the updated pointer of the original string so that
706 * it can be used for the next call.
708 char *snd_info_get_str(char *dest, char *src, int len)
712 while (*src == ' ' || *src == '\t')
714 if (*src == '"' || *src == '\'') {
716 while (--len > 0 && *src && *src != c) {
722 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
727 while (*src == ' ' || *src == '\t')
732 EXPORT_SYMBOL(snd_info_get_str);
735 * snd_info_create_entry - create an info entry
736 * @name: the proc file name
738 * Creates an info entry with the given file name and initializes as
741 * Usually called from other functions such as
742 * snd_info_create_card_entry().
744 * Returns the pointer of the new instance, or NULL on failure.
746 static struct snd_info_entry *snd_info_create_entry(const char *name)
748 struct snd_info_entry *entry;
749 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
752 entry->name = kstrdup(name, GFP_KERNEL);
753 if (entry->name == NULL) {
757 entry->mode = S_IFREG | S_IRUGO;
758 entry->content = SNDRV_INFO_CONTENT_TEXT;
759 mutex_init(&entry->access);
764 * snd_info_create_module_entry - create an info entry for the given module
765 * @module: the module pointer
766 * @name: the file name
767 * @parent: the parent directory
769 * Creates a new info entry and assigns it to the given module.
771 * Returns the pointer of the new instance, or NULL on failure.
773 struct snd_info_entry *snd_info_create_module_entry(struct module * module,
775 struct snd_info_entry *parent)
777 struct snd_info_entry *entry = snd_info_create_entry(name);
779 entry->module = module;
780 entry->parent = parent;
785 EXPORT_SYMBOL(snd_info_create_module_entry);
788 * snd_info_create_card_entry - create an info entry for the given card
789 * @card: the card instance
790 * @name: the file name
791 * @parent: the parent directory
793 * Creates a new info entry and assigns it to the given card.
795 * Returns the pointer of the new instance, or NULL on failure.
797 struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
799 struct snd_info_entry * parent)
801 struct snd_info_entry *entry = snd_info_create_entry(name);
803 entry->module = card->module;
805 entry->parent = parent;
810 EXPORT_SYMBOL(snd_info_create_card_entry);
812 static int snd_info_dev_free_entry(struct snd_device *device)
814 struct snd_info_entry *entry = device->device_data;
815 snd_info_free_entry(entry);
819 static int snd_info_dev_register_entry(struct snd_device *device)
821 struct snd_info_entry *entry = device->device_data;
822 return snd_info_register(entry);
825 static int snd_info_dev_disconnect_entry(struct snd_device *device)
827 struct snd_info_entry *entry = device->device_data;
828 entry->disconnected = 1;
832 static int snd_info_dev_unregister_entry(struct snd_device *device)
834 struct snd_info_entry *entry = device->device_data;
835 return snd_info_unregister(entry);
839 * snd_card_proc_new - create an info entry for the given card
840 * @card: the card instance
841 * @name: the file name
842 * @entryp: the pointer to store the new info entry
844 * Creates a new info entry and assigns it to the given card.
845 * Unlike snd_info_create_card_entry(), this function registers the
846 * info entry as an ALSA device component, so that it can be
847 * unregistered/released without explicit call.
848 * Also, you don't have to register this entry via snd_info_register(),
849 * since this will be registered by snd_card_register() automatically.
851 * The parent is assumed as card->proc_root.
853 * For releasing this entry, use snd_device_free() instead of
854 * snd_info_free_entry().
856 * Returns zero if successful, or a negative error code on failure.
858 int snd_card_proc_new(struct snd_card *card, const char *name,
859 struct snd_info_entry **entryp)
861 static struct snd_device_ops ops = {
862 .dev_free = snd_info_dev_free_entry,
863 .dev_register = snd_info_dev_register_entry,
864 .dev_disconnect = snd_info_dev_disconnect_entry,
865 .dev_unregister = snd_info_dev_unregister_entry
867 struct snd_info_entry *entry;
870 entry = snd_info_create_card_entry(card, name, card->proc_root);
873 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
874 snd_info_free_entry(entry);
882 EXPORT_SYMBOL(snd_card_proc_new);
885 * snd_info_free_entry - release the info entry
886 * @entry: the info entry
888 * Releases the info entry. Don't call this after registered.
890 void snd_info_free_entry(struct snd_info_entry * entry)
895 if (entry->private_free)
896 entry->private_free(entry);
900 EXPORT_SYMBOL(snd_info_free_entry);
903 * snd_info_register - register the info entry
904 * @entry: the info entry
906 * Registers the proc info entry.
908 * Returns zero if successful, or a negative error code on failure.
910 int snd_info_register(struct snd_info_entry * entry)
912 struct proc_dir_entry *root, *p = NULL;
914 snd_assert(entry != NULL, return -ENXIO);
915 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
916 mutex_lock(&info_mutex);
917 p = snd_create_proc_entry(entry->name, entry->mode, root);
919 mutex_unlock(&info_mutex);
922 p->owner = entry->module;
923 if (!S_ISDIR(entry->mode))
924 p->proc_fops = &snd_info_entry_operations;
925 p->size = entry->size;
928 mutex_unlock(&info_mutex);
932 EXPORT_SYMBOL(snd_info_register);
935 * snd_info_unregister - de-register the info entry
936 * @entry: the info entry
938 * De-registers the info entry and releases the instance.
940 * Returns zero if successful, or a negative error code on failure.
942 int snd_info_unregister(struct snd_info_entry * entry)
944 struct proc_dir_entry *root;
948 snd_assert(entry->p != NULL, return -ENXIO);
949 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
950 snd_assert(root, return -ENXIO);
951 mutex_lock(&info_mutex);
952 snd_remove_proc_entry(root, entry->p);
953 mutex_unlock(&info_mutex);
954 snd_info_free_entry(entry);
958 EXPORT_SYMBOL(snd_info_unregister);
964 static struct snd_info_entry *snd_info_version_entry = NULL;
966 static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
969 "Advanced Linux Sound Architecture Driver Version "
970 CONFIG_SND_VERSION CONFIG_SND_DATE ".\n"
974 static int __init snd_info_version_init(void)
976 struct snd_info_entry *entry;
978 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
981 entry->c.text.read_size = 256;
982 entry->c.text.read = snd_info_version_read;
983 if (snd_info_register(entry) < 0) {
984 snd_info_free_entry(entry);
987 snd_info_version_entry = entry;
991 static int __exit snd_info_version_done(void)
993 if (snd_info_version_entry)
994 snd_info_unregister(snd_info_version_entry);
998 #endif /* CONFIG_PROC_FS */