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 <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/devfs_fs_kernel.h>
39 int snd_info_check_reserved_words(const char *str)
41 static char *reserved[] =
56 char **xstr = reserved;
59 if (!strcmp(*xstr, str))
63 if (!strncmp(str, "card", 4))
70 static DECLARE_MUTEX(info_mutex);
72 typedef struct _snd_info_private_data {
73 snd_info_buffer_t *rbuffer;
74 snd_info_buffer_t *wbuffer;
75 snd_info_entry_t *entry;
76 void *file_private_data;
77 } snd_info_private_data_t;
79 static int snd_info_version_init(void);
80 static int snd_info_version_done(void);
84 * snd_iprintf - printf on the procfs buffer
85 * @buffer: the procfs buffer
86 * @fmt: the printf format
88 * Outputs the string on the procfs buffer just like printf().
90 * Returns the size of output string.
92 int snd_iprintf(snd_info_buffer_t * buffer, char *fmt,...)
97 if (buffer->stop || buffer->error)
99 len = buffer->len - buffer->size;
101 res = vsnprintf(buffer->curr, len, fmt, args);
116 static struct proc_dir_entry *snd_proc_root = NULL;
117 snd_info_entry_t *snd_seq_root = NULL;
118 #ifdef CONFIG_SND_OSSEMUL
119 snd_info_entry_t *snd_oss_root = NULL;
122 static inline void snd_info_entry_prepare(struct proc_dir_entry *de)
124 de->owner = THIS_MODULE;
127 static void snd_remove_proc_entry(struct proc_dir_entry *parent,
128 struct proc_dir_entry *de)
131 remove_proc_entry(de->name, parent);
134 static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
136 snd_info_private_data_t *data;
137 struct snd_info_entry *entry;
140 data = file->private_data;
143 switch (entry->content) {
144 case SNDRV_INFO_CONTENT_TEXT:
146 case 0: /* SEEK_SET */
147 file->f_pos = offset;
150 case 1: /* SEEK_CUR */
151 file->f_pos += offset;
154 case 2: /* SEEK_END */
160 case SNDRV_INFO_CONTENT_DATA:
161 if (entry->c.ops->llseek) {
162 ret = entry->c.ops->llseek(entry,
163 data->file_private_data,
175 static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
176 size_t count, loff_t * offset)
178 snd_info_private_data_t *data;
179 struct snd_info_entry *entry;
180 snd_info_buffer_t *buf;
184 data = file->private_data;
185 snd_assert(data != NULL, return -ENXIO);
187 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
189 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
192 switch (entry->content) {
193 case SNDRV_INFO_CONTENT_TEXT:
197 if (pos >= buf->size)
199 size = buf->size - pos;
200 size = min(count, size);
201 if (copy_to_user(buffer, buf->buffer + pos, size))
204 case SNDRV_INFO_CONTENT_DATA:
205 if (entry->c.ops->read)
206 size = entry->c.ops->read(entry,
207 data->file_private_data,
208 file, buffer, count, pos);
211 if ((ssize_t) size > 0)
212 *offset = pos + size;
216 static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
217 size_t count, loff_t * offset)
219 snd_info_private_data_t *data;
220 struct snd_info_entry *entry;
221 snd_info_buffer_t *buf;
225 data = file->private_data;
226 snd_assert(data != NULL, return -ENXIO);
229 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
231 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
233 switch (entry->content) {
234 case SNDRV_INFO_CONTENT_TEXT:
240 size = buf->len - pos;
241 size = min(count, size);
242 if (copy_from_user(buf->buffer + pos, buffer, size))
244 if ((long)buf->size < pos + size)
245 buf->size = pos + size;
247 case SNDRV_INFO_CONTENT_DATA:
248 if (entry->c.ops->write)
249 size = entry->c.ops->write(entry,
250 data->file_private_data,
251 file, buffer, count, pos);
254 if ((ssize_t) size > 0)
255 *offset = pos + size;
259 static int snd_info_entry_open(struct inode *inode, struct file *file)
261 snd_info_entry_t *entry;
262 snd_info_private_data_t *data;
263 snd_info_buffer_t *buffer;
264 struct proc_dir_entry *p;
269 entry = p == NULL ? NULL : (snd_info_entry_t *)p->data;
270 if (entry == NULL || entry->disconnected) {
274 if (!try_module_get(entry->module)) {
278 mode = file->f_flags & O_ACCMODE;
279 if (mode == O_RDONLY || mode == O_RDWR) {
280 if ((entry->content == SNDRV_INFO_CONTENT_TEXT &&
281 !entry->c.text.read_size) ||
282 (entry->content == SNDRV_INFO_CONTENT_DATA &&
283 entry->c.ops->read == NULL)) {
288 if (mode == O_WRONLY || mode == O_RDWR) {
289 if ((entry->content == SNDRV_INFO_CONTENT_TEXT &&
290 !entry->c.text.write_size) ||
291 (entry->content == SNDRV_INFO_CONTENT_DATA &&
292 entry->c.ops->write == NULL)) {
297 data = kcalloc(1, sizeof(*data), GFP_KERNEL);
303 switch (entry->content) {
304 case SNDRV_INFO_CONTENT_TEXT:
305 if (mode == O_RDONLY || mode == O_RDWR) {
306 buffer = kcalloc(1, sizeof(*buffer), GFP_KERNEL);
307 if (buffer == NULL) {
312 buffer->len = (entry->c.text.read_size +
313 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
314 buffer->buffer = vmalloc(buffer->len);
315 if (buffer->buffer == NULL) {
321 buffer->curr = buffer->buffer;
322 data->rbuffer = buffer;
324 if (mode == O_WRONLY || mode == O_RDWR) {
325 buffer = kcalloc(1, sizeof(*buffer), GFP_KERNEL);
326 if (buffer == NULL) {
327 if (mode == O_RDWR) {
328 vfree(data->rbuffer->buffer);
329 kfree(data->rbuffer);
335 buffer->len = (entry->c.text.write_size +
336 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
337 buffer->buffer = vmalloc(buffer->len);
338 if (buffer->buffer == NULL) {
339 if (mode == O_RDWR) {
340 vfree(data->rbuffer->buffer);
341 kfree(data->rbuffer);
348 buffer->curr = buffer->buffer;
349 data->wbuffer = buffer;
352 case SNDRV_INFO_CONTENT_DATA: /* data */
353 if (entry->c.ops->open) {
354 if ((err = entry->c.ops->open(entry, mode,
355 &data->file_private_data)) < 0) {
362 file->private_data = data;
364 if (entry->content == SNDRV_INFO_CONTENT_TEXT &&
365 (mode == O_RDONLY || mode == O_RDWR)) {
366 if (entry->c.text.read) {
367 down(&entry->access);
368 entry->c.text.read(entry, data->rbuffer);
375 module_put(entry->module);
381 static int snd_info_entry_release(struct inode *inode, struct file *file)
383 snd_info_entry_t *entry;
384 snd_info_private_data_t *data;
387 mode = file->f_flags & O_ACCMODE;
388 data = file->private_data;
390 switch (entry->content) {
391 case SNDRV_INFO_CONTENT_TEXT:
392 if (mode == O_RDONLY || mode == O_RDWR) {
393 vfree(data->rbuffer->buffer);
394 kfree(data->rbuffer);
396 if (mode == O_WRONLY || mode == O_RDWR) {
397 if (entry->c.text.write) {
398 entry->c.text.write(entry, data->wbuffer);
399 if (data->wbuffer->error) {
400 snd_printk(KERN_WARNING "data write error to %s (%i)\n",
402 data->wbuffer->error);
405 vfree(data->wbuffer->buffer);
406 kfree(data->wbuffer);
409 case SNDRV_INFO_CONTENT_DATA:
410 if (entry->c.ops->release)
411 entry->c.ops->release(entry, mode,
412 data->file_private_data);
415 module_put(entry->module);
420 static unsigned int snd_info_entry_poll(struct file *file, poll_table * wait)
422 snd_info_private_data_t *data;
423 struct snd_info_entry *entry;
426 data = file->private_data;
431 switch (entry->content) {
432 case SNDRV_INFO_CONTENT_DATA:
433 if (entry->c.ops->poll)
434 return entry->c.ops->poll(entry,
435 data->file_private_data,
437 if (entry->c.ops->read)
438 mask |= POLLIN | POLLRDNORM;
439 if (entry->c.ops->write)
440 mask |= POLLOUT | POLLWRNORM;
446 static inline int _snd_info_entry_ioctl(struct inode *inode, struct file *file,
447 unsigned int cmd, unsigned long arg)
449 snd_info_private_data_t *data;
450 struct snd_info_entry *entry;
452 data = file->private_data;
456 switch (entry->content) {
457 case SNDRV_INFO_CONTENT_DATA:
458 if (entry->c.ops->ioctl)
459 return entry->c.ops->ioctl(entry,
460 data->file_private_data,
467 /* FIXME: need to unlock BKL to allow preemption */
468 static int snd_info_entry_ioctl(struct inode *inode, struct file *file,
469 unsigned int cmd, unsigned long arg)
473 err = _snd_info_entry_ioctl(inode, file, cmd, arg);
478 static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
480 struct inode *inode = file->f_dentry->d_inode;
481 snd_info_private_data_t *data;
482 struct snd_info_entry *entry;
484 data = file->private_data;
488 switch (entry->content) {
489 case SNDRV_INFO_CONTENT_DATA:
490 if (entry->c.ops->mmap)
491 return entry->c.ops->mmap(entry,
492 data->file_private_data,
499 static struct file_operations snd_info_entry_operations =
501 .owner = THIS_MODULE,
502 .llseek = snd_info_entry_llseek,
503 .read = snd_info_entry_read,
504 .write = snd_info_entry_write,
505 .poll = snd_info_entry_poll,
506 .ioctl = snd_info_entry_ioctl,
507 .mmap = snd_info_entry_mmap,
508 .open = snd_info_entry_open,
509 .release = snd_info_entry_release,
513 * snd_create_proc_entry - create a procfs entry
514 * @name: the name of the proc file
515 * @mode: the file permission bits, S_Ixxx
516 * @parent: the parent proc-directory entry
518 * Creates a new proc file entry with the given name and permission
519 * on the given directory.
521 * Returns the pointer of new instance or NULL on failure.
523 static struct proc_dir_entry *snd_create_proc_entry(const char *name, mode_t mode,
524 struct proc_dir_entry *parent)
526 struct proc_dir_entry *p;
527 p = create_proc_entry(name, mode, parent);
529 snd_info_entry_prepare(p);
533 int __init snd_info_init(void)
535 struct proc_dir_entry *p;
537 p = snd_create_proc_entry("asound", S_IFDIR | S_IRUGO | S_IXUGO, &proc_root);
541 #ifdef CONFIG_SND_OSSEMUL
543 snd_info_entry_t *entry;
544 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
546 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
547 if (snd_info_register(entry) < 0) {
548 snd_info_free_entry(entry);
551 snd_oss_root = entry;
554 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
556 snd_info_entry_t *entry;
557 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", 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_seq_root = entry;
567 snd_info_version_init();
568 snd_memory_info_init();
569 snd_minor_info_init();
570 snd_minor_info_oss_init();
571 snd_card_info_init();
575 int __exit snd_info_done(void)
577 snd_card_info_done();
578 snd_minor_info_oss_done();
579 snd_minor_info_done();
580 snd_memory_info_done();
581 snd_info_version_done();
583 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
585 snd_info_unregister(snd_seq_root);
587 #ifdef CONFIG_SND_OSSEMUL
589 snd_info_unregister(snd_oss_root);
591 snd_remove_proc_entry(&proc_root, snd_proc_root);
602 * create a card proc file
605 int snd_info_card_create(snd_card_t * card)
608 snd_info_entry_t *entry;
610 snd_assert(card != NULL, return -ENXIO);
612 sprintf(str, "card%i", card->number);
613 if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
615 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
616 if (snd_info_register(entry) < 0) {
617 snd_info_free_entry(entry);
620 card->proc_root = entry;
625 * register the card proc file
628 int snd_info_card_register(snd_card_t * card)
630 struct proc_dir_entry *p;
632 snd_assert(card != NULL, return -ENXIO);
634 if (!strcmp(card->id, card->proc_root->name))
637 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
640 card->proc_root_link = p;
645 * de-register the card proc file
648 int snd_info_card_free(snd_card_t * card)
650 snd_assert(card != NULL, return -ENXIO);
651 if (card->proc_root_link) {
652 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
653 card->proc_root_link = NULL;
655 if (card->proc_root) {
656 snd_info_unregister(card->proc_root);
657 card->proc_root = NULL;
664 * snd_info_get_line - read one line from the procfs buffer
665 * @buffer: the procfs buffer
666 * @line: the buffer to store
667 * @len: the max. buffer size - 1
669 * Reads one line from the buffer and stores the string.
671 * Returns zero if successful, or 1 if error or EOF.
673 int snd_info_get_line(snd_info_buffer_t * buffer, char *line, int len)
677 if (len <= 0 || buffer->stop || buffer->error)
682 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
688 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
693 while (c != '\n' && !buffer->stop) {
695 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
704 * snd_info_get_line - parse a string token
705 * @dest: the buffer to store the string token
706 * @src: the original string
707 * @len: the max. length of token - 1
709 * Parses the original string and copy a token to the given
712 * Returns the updated pointer of the original string so that
713 * it can be used for the next call.
715 char *snd_info_get_str(char *dest, char *src, int len)
719 while (*src == ' ' || *src == '\t')
721 if (*src == '"' || *src == '\'') {
723 while (--len > 0 && *src && *src != c) {
729 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
734 while (*src == ' ' || *src == '\t')
740 * snd_info_create_entry - create an info entry
741 * @name: the proc file name
743 * Creates an info entry with the given file name and initializes as
746 * Usually called from other functions such as
747 * snd_info_create_card_entry().
749 * Returns the pointer of the new instance, or NULL on failure.
751 static snd_info_entry_t *snd_info_create_entry(const char *name)
753 snd_info_entry_t *entry;
754 entry = kcalloc(1, sizeof(*entry), GFP_KERNEL);
757 entry->name = snd_kmalloc_strdup(name, GFP_KERNEL);
758 if (entry->name == NULL) {
762 entry->mode = S_IFREG | S_IRUGO;
763 entry->content = SNDRV_INFO_CONTENT_TEXT;
764 init_MUTEX(&entry->access);
769 * snd_info_create_module_entry - create an info entry for the given module
770 * @module: the module pointer
771 * @name: the file name
772 * @parent: the parent directory
774 * Creates a new info entry and assigns it to the given module.
776 * Returns the pointer of the new instance, or NULL on failure.
778 snd_info_entry_t *snd_info_create_module_entry(struct module * module,
780 snd_info_entry_t *parent)
782 snd_info_entry_t *entry = snd_info_create_entry(name);
784 entry->module = module;
785 entry->parent = parent;
791 * snd_info_create_card_entry - create an info entry for the given card
792 * @card: the card instance
793 * @name: the file name
794 * @parent: the parent directory
796 * Creates a new info entry and assigns it to the given card.
798 * Returns the pointer of the new instance, or NULL on failure.
800 snd_info_entry_t *snd_info_create_card_entry(snd_card_t * card,
802 snd_info_entry_t * parent)
804 snd_info_entry_t *entry = snd_info_create_entry(name);
806 entry->module = card->module;
808 entry->parent = parent;
813 static int snd_info_dev_free_entry(snd_device_t *device)
815 snd_info_entry_t *entry = device->device_data;
816 snd_info_free_entry(entry);
820 static int snd_info_dev_register_entry(snd_device_t *device)
822 snd_info_entry_t *entry = device->device_data;
823 return snd_info_register(entry);
826 static int snd_info_dev_disconnect_entry(snd_device_t *device)
828 snd_info_entry_t *entry = device->device_data;
829 entry->disconnected = 1;
833 static int snd_info_dev_unregister_entry(snd_device_t *device)
835 snd_info_entry_t *entry = device->device_data;
836 return snd_info_unregister(entry);
840 * snd_card_proc_new - create an info entry for the given card
841 * @card: the card instance
842 * @name: the file name
843 * @entryp: the pointer to store the new info entry
845 * Creates a new info entry and assigns it to the given card.
846 * Unlike snd_info_create_card_entry(), this function registers the
847 * info entry as an ALSA device component, so that it can be
848 * unregistered/released without explicit call.
849 * Also, you don't have to register this entry via snd_info_register(),
850 * since this will be registered by snd_card_register() automatically.
852 * The parent is assumed as card->proc_root.
854 * For releasing this entry, use snd_device_free() instead of
855 * snd_info_free_entry().
857 * Returns zero if successful, or a negative error code on failure.
859 int snd_card_proc_new(snd_card_t *card, const char *name,
860 snd_info_entry_t **entryp)
862 static snd_device_ops_t ops = {
863 .dev_free = snd_info_dev_free_entry,
864 .dev_register = snd_info_dev_register_entry,
865 .dev_disconnect = snd_info_dev_disconnect_entry,
866 .dev_unregister = snd_info_dev_unregister_entry
868 snd_info_entry_t *entry;
871 entry = snd_info_create_card_entry(card, name, card->proc_root);
874 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
875 snd_info_free_entry(entry);
884 * snd_info_free_entry - release the info entry
885 * @entry: the info entry
887 * Releases the info entry. Don't call this after registered.
889 void snd_info_free_entry(snd_info_entry_t * entry)
894 if (entry->private_free)
895 entry->private_free(entry);
900 * snd_info_register - register the info entry
901 * @entry: the info entry
903 * Registers the proc info entry.
905 * Returns zero if successful, or a negative error code on failure.
907 int snd_info_register(snd_info_entry_t * entry)
909 struct proc_dir_entry *root, *p = NULL;
911 snd_assert(entry != NULL, return -ENXIO);
912 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
914 p = snd_create_proc_entry(entry->name, entry->mode, root);
919 p->owner = entry->module;
920 if (!S_ISDIR(entry->mode))
921 p->proc_fops = &snd_info_entry_operations;
922 p->size = entry->size;
930 * snd_info_unregister - de-register the info entry
931 * @entry: the info entry
933 * De-registers the info entry and releases the instance.
935 * Returns zero if successful, or a negative error code on failure.
937 int snd_info_unregister(snd_info_entry_t * entry)
939 struct proc_dir_entry *root;
941 snd_assert(entry != NULL && entry->p != NULL, return -ENXIO);
942 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
943 snd_assert(root, return -ENXIO);
945 snd_remove_proc_entry(root, entry->p);
947 snd_info_free_entry(entry);
955 static snd_info_entry_t *snd_info_version_entry = NULL;
957 static void snd_info_version_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
960 "Advanced Linux Sound Architecture Driver Version "
961 CONFIG_SND_VERSION CONFIG_SND_DATE ".\n"
965 static int __init snd_info_version_init(void)
967 snd_info_entry_t *entry;
969 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
972 entry->c.text.read_size = 256;
973 entry->c.text.read = snd_info_version_read;
974 if (snd_info_register(entry) < 0) {
975 snd_info_free_entry(entry);
978 snd_info_version_entry = entry;
982 static int __exit snd_info_version_done(void)
984 if (snd_info_version_entry)
985 snd_info_unregister(snd_info_version_entry);
989 #endif /* CONFIG_PROC_FS */