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/version.h>
24 #include <linux/init.h>
25 #include <linux/vmalloc.h>
26 #include <linux/time.h>
27 #include <linux/smp_lock.h>
28 #include <linux/utsname.h>
29 #include <linux/config.h>
31 #include <sound/core.h>
32 #include <sound/version.h>
33 #include <sound/minors.h>
34 #include <sound/info.h>
35 #include <linux/proc_fs.h>
36 #include <linux/devfs_fs_kernel.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))
74 static DECLARE_MUTEX(info_mutex);
76 typedef struct _snd_info_private_data {
77 snd_info_buffer_t *rbuffer;
78 snd_info_buffer_t *wbuffer;
79 snd_info_entry_t *entry;
80 void *file_private_data;
81 } snd_info_private_data_t;
83 static int snd_info_version_init(void);
84 static int snd_info_version_done(void);
88 * snd_iprintf - printf on the procfs buffer
89 * @buffer: the procfs buffer
90 * @fmt: the printf format
92 * Outputs the string on the procfs buffer just like printf().
94 * Returns the size of output string.
96 int snd_iprintf(snd_info_buffer_t * buffer, char *fmt,...)
102 if (buffer->stop || buffer->error)
105 res = vscnprintf(sbuffer, sizeof(sbuffer), fmt, args);
107 if (buffer->size + res >= buffer->len) {
111 strcpy(buffer->curr, sbuffer);
121 static struct proc_dir_entry *snd_proc_root = NULL;
122 snd_info_entry_t *snd_seq_root = NULL;
123 #ifdef CONFIG_SND_OSSEMUL
124 snd_info_entry_t *snd_oss_root = NULL;
127 static inline void snd_info_entry_prepare(struct proc_dir_entry *de)
129 de->owner = THIS_MODULE;
132 void snd_remove_proc_entry(struct proc_dir_entry *parent,
133 struct proc_dir_entry *de)
136 remove_proc_entry(de->name, parent);
139 static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
141 snd_info_private_data_t *data;
142 struct snd_info_entry *entry;
145 data = file->private_data;
148 switch (entry->content) {
149 case SNDRV_INFO_CONTENT_TEXT:
151 case 0: /* SEEK_SET */
152 file->f_pos = offset;
155 case 1: /* SEEK_CUR */
156 file->f_pos += offset;
159 case 2: /* SEEK_END */
165 case SNDRV_INFO_CONTENT_DATA:
166 if (entry->c.ops->llseek) {
167 ret = entry->c.ops->llseek(entry,
168 data->file_private_data,
180 static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
181 size_t count, loff_t * offset)
183 snd_info_private_data_t *data;
184 struct snd_info_entry *entry;
185 snd_info_buffer_t *buf;
189 data = file->private_data;
190 snd_assert(data != NULL, return -ENXIO);
192 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
194 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
197 switch (entry->content) {
198 case SNDRV_INFO_CONTENT_TEXT:
202 if (pos >= buf->size)
204 size = buf->size - pos;
205 size = min(count, size);
206 if (copy_to_user(buffer, buf->buffer + pos, size))
209 case SNDRV_INFO_CONTENT_DATA:
210 if (entry->c.ops->read)
211 size = entry->c.ops->read(entry,
212 data->file_private_data,
213 file, buffer, count, pos);
216 if ((ssize_t) size > 0)
217 *offset = pos + size;
221 static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
222 size_t count, loff_t * offset)
224 snd_info_private_data_t *data;
225 struct snd_info_entry *entry;
226 snd_info_buffer_t *buf;
230 data = file->private_data;
231 snd_assert(data != NULL, return -ENXIO);
234 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
236 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
238 switch (entry->content) {
239 case SNDRV_INFO_CONTENT_TEXT:
245 size = buf->len - pos;
246 size = min(count, size);
247 if (copy_from_user(buf->buffer + pos, buffer, size))
249 if ((long)buf->size < pos + size)
250 buf->size = pos + size;
252 case SNDRV_INFO_CONTENT_DATA:
253 if (entry->c.ops->write)
254 size = entry->c.ops->write(entry,
255 data->file_private_data,
256 file, buffer, count, pos);
259 if ((ssize_t) size > 0)
260 *offset = pos + size;
264 static int snd_info_entry_open(struct inode *inode, struct file *file)
266 snd_info_entry_t *entry;
267 snd_info_private_data_t *data;
268 snd_info_buffer_t *buffer;
269 struct proc_dir_entry *p;
274 entry = p == NULL ? NULL : (snd_info_entry_t *)p->data;
275 if (entry == NULL || entry->disconnected) {
279 if (!try_module_get(entry->module)) {
283 mode = file->f_flags & O_ACCMODE;
284 if (mode == O_RDONLY || mode == O_RDWR) {
285 if ((entry->content == SNDRV_INFO_CONTENT_TEXT &&
286 !entry->c.text.read_size) ||
287 (entry->content == SNDRV_INFO_CONTENT_DATA &&
288 entry->c.ops->read == NULL)) {
293 if (mode == O_WRONLY || mode == O_RDWR) {
294 if ((entry->content == SNDRV_INFO_CONTENT_TEXT &&
295 !entry->c.text.write_size) ||
296 (entry->content == SNDRV_INFO_CONTENT_DATA &&
297 entry->c.ops->write == NULL)) {
302 data = kcalloc(1, sizeof(*data), GFP_KERNEL);
308 switch (entry->content) {
309 case SNDRV_INFO_CONTENT_TEXT:
310 if (mode == O_RDONLY || mode == O_RDWR) {
311 buffer = kcalloc(1, sizeof(*buffer), GFP_KERNEL);
312 if (buffer == NULL) {
317 buffer->len = (entry->c.text.read_size +
318 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
319 buffer->buffer = vmalloc(buffer->len);
320 if (buffer->buffer == NULL) {
326 buffer->curr = buffer->buffer;
327 data->rbuffer = buffer;
329 if (mode == O_WRONLY || mode == O_RDWR) {
330 buffer = kcalloc(1, sizeof(*buffer), GFP_KERNEL);
331 if (buffer == NULL) {
332 if (mode == O_RDWR) {
333 vfree(data->rbuffer->buffer);
334 kfree(data->rbuffer);
340 buffer->len = (entry->c.text.write_size +
341 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
342 buffer->buffer = vmalloc(buffer->len);
343 if (buffer->buffer == NULL) {
344 if (mode == O_RDWR) {
345 vfree(data->rbuffer->buffer);
346 kfree(data->rbuffer);
353 buffer->curr = buffer->buffer;
354 data->wbuffer = buffer;
357 case SNDRV_INFO_CONTENT_DATA: /* data */
358 if (entry->c.ops->open) {
359 if ((err = entry->c.ops->open(entry, mode,
360 &data->file_private_data)) < 0) {
367 file->private_data = data;
369 if (entry->content == SNDRV_INFO_CONTENT_TEXT &&
370 (mode == O_RDONLY || mode == O_RDWR)) {
371 if (entry->c.text.read) {
372 down(&entry->access);
373 entry->c.text.read(entry, data->rbuffer);
380 module_put(entry->module);
386 static int snd_info_entry_release(struct inode *inode, struct file *file)
388 snd_info_entry_t *entry;
389 snd_info_private_data_t *data;
392 mode = file->f_flags & O_ACCMODE;
393 data = file->private_data;
395 switch (entry->content) {
396 case SNDRV_INFO_CONTENT_TEXT:
397 if (mode == O_RDONLY || mode == O_RDWR) {
398 vfree(data->rbuffer->buffer);
399 kfree(data->rbuffer);
401 if (mode == O_WRONLY || mode == O_RDWR) {
402 if (entry->c.text.write) {
403 entry->c.text.write(entry, data->wbuffer);
404 if (data->wbuffer->error) {
405 snd_printk(KERN_WARNING "data write error to %s (%i)\n",
407 data->wbuffer->error);
410 vfree(data->wbuffer->buffer);
411 kfree(data->wbuffer);
414 case SNDRV_INFO_CONTENT_DATA:
415 if (entry->c.ops->release)
416 entry->c.ops->release(entry, mode,
417 data->file_private_data);
420 module_put(entry->module);
425 static unsigned int snd_info_entry_poll(struct file *file, poll_table * wait)
427 snd_info_private_data_t *data;
428 struct snd_info_entry *entry;
431 data = file->private_data;
436 switch (entry->content) {
437 case SNDRV_INFO_CONTENT_DATA:
438 if (entry->c.ops->poll)
439 return entry->c.ops->poll(entry,
440 data->file_private_data,
442 if (entry->c.ops->read)
443 mask |= POLLIN | POLLRDNORM;
444 if (entry->c.ops->write)
445 mask |= POLLOUT | POLLWRNORM;
451 static inline int _snd_info_entry_ioctl(struct inode *inode, struct file *file,
452 unsigned int cmd, unsigned long arg)
454 snd_info_private_data_t *data;
455 struct snd_info_entry *entry;
457 data = file->private_data;
461 switch (entry->content) {
462 case SNDRV_INFO_CONTENT_DATA:
463 if (entry->c.ops->ioctl)
464 return entry->c.ops->ioctl(entry,
465 data->file_private_data,
472 /* FIXME: need to unlock BKL to allow preemption */
473 static int snd_info_entry_ioctl(struct inode *inode, struct file *file,
474 unsigned int cmd, unsigned long arg)
478 err = _snd_info_entry_ioctl(inode, file, cmd, arg);
483 static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
485 struct inode *inode = file->f_dentry->d_inode;
486 snd_info_private_data_t *data;
487 struct snd_info_entry *entry;
489 data = file->private_data;
493 switch (entry->content) {
494 case SNDRV_INFO_CONTENT_DATA:
495 if (entry->c.ops->mmap)
496 return entry->c.ops->mmap(entry,
497 data->file_private_data,
504 static struct file_operations snd_info_entry_operations =
506 .owner = THIS_MODULE,
507 .llseek = snd_info_entry_llseek,
508 .read = snd_info_entry_read,
509 .write = snd_info_entry_write,
510 .poll = snd_info_entry_poll,
511 .ioctl = snd_info_entry_ioctl,
512 .mmap = snd_info_entry_mmap,
513 .open = snd_info_entry_open,
514 .release = snd_info_entry_release,
518 * snd_create_proc_entry - create a procfs entry
519 * @name: the name of the proc file
520 * @mode: the file permission bits, S_Ixxx
521 * @parent: the parent proc-directory entry
523 * Creates a new proc file entry with the given name and permission
524 * on the given directory.
526 * Returns the pointer of new instance or NULL on failure.
528 struct proc_dir_entry *snd_create_proc_entry(const char *name, mode_t mode,
529 struct proc_dir_entry *parent)
531 struct proc_dir_entry *p;
532 p = create_proc_entry(name, mode, parent);
534 snd_info_entry_prepare(p);
538 int __init snd_info_init(void)
540 struct proc_dir_entry *p;
542 p = snd_create_proc_entry("asound", S_IFDIR | S_IRUGO | S_IXUGO, &proc_root);
546 #ifdef CONFIG_SND_OSSEMUL
548 snd_info_entry_t *entry;
549 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
551 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
552 if (snd_info_register(entry) < 0) {
553 snd_info_free_entry(entry);
556 snd_oss_root = entry;
559 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
561 snd_info_entry_t *entry;
562 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL)
564 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
565 if (snd_info_register(entry) < 0) {
566 snd_info_free_entry(entry);
569 snd_seq_root = entry;
572 snd_info_version_init();
573 snd_memory_info_init();
574 snd_minor_info_init();
575 snd_minor_info_oss_init();
576 snd_card_info_init();
580 int __exit snd_info_done(void)
582 snd_card_info_done();
583 snd_minor_info_oss_done();
584 snd_minor_info_done();
585 snd_memory_info_done();
586 snd_info_version_done();
588 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
590 snd_info_unregister(snd_seq_root);
592 #ifdef CONFIG_SND_OSSEMUL
594 snd_info_unregister(snd_oss_root);
596 snd_remove_proc_entry(&proc_root, snd_proc_root);
607 * create a card proc file
610 int snd_info_card_create(snd_card_t * card)
613 snd_info_entry_t *entry;
615 snd_assert(card != NULL, return -ENXIO);
617 sprintf(str, "card%i", card->number);
618 if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
620 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
621 if (snd_info_register(entry) < 0) {
622 snd_info_free_entry(entry);
625 card->proc_root = entry;
630 * register the card proc file
633 int snd_info_card_register(snd_card_t * card)
635 struct proc_dir_entry *p;
637 snd_assert(card != NULL, return -ENXIO);
639 if (!strcmp(card->id, card->proc_root->name))
642 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
645 card->proc_root_link = p;
650 * de-register the card proc file
653 int snd_info_card_free(snd_card_t * card)
655 snd_assert(card != NULL, return -ENXIO);
656 if (card->proc_root_link) {
657 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
658 card->proc_root_link = NULL;
660 if (card->proc_root) {
661 snd_info_unregister(card->proc_root);
662 card->proc_root = NULL;
669 * snd_info_get_line - read one line from the procfs buffer
670 * @buffer: the procfs buffer
671 * @line: the buffer to store
672 * @len: the max. buffer size - 1
674 * Reads one line from the buffer and stores the string.
676 * Returns zero if successful, or 1 if error or EOF.
678 int snd_info_get_line(snd_info_buffer_t * buffer, char *line, int len)
682 if (len <= 0 || buffer->stop || buffer->error)
687 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
693 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
698 while (c != '\n' && !buffer->stop) {
700 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
709 * snd_info_get_line - parse a string token
710 * @dest: the buffer to store the string token
711 * @src: the original string
712 * @len: the max. length of token - 1
714 * Parses the original string and copy a token to the given
717 * Returns the updated pointer of the original string so that
718 * it can be used for the next call.
720 char *snd_info_get_str(char *dest, char *src, int len)
724 while (*src == ' ' || *src == '\t')
726 if (*src == '"' || *src == '\'') {
728 while (--len > 0 && *src && *src != c) {
734 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
739 while (*src == ' ' || *src == '\t')
745 * snd_info_create_entry - create an info entry
746 * @name: the proc file name
748 * Creates an info entry with the given file name and initializes as
751 * Usually called from other functions such as
752 * snd_info_create_card_entry().
754 * Returns the pointer of the new instance, or NULL on failure.
756 static snd_info_entry_t *snd_info_create_entry(const char *name)
758 snd_info_entry_t *entry;
759 entry = kcalloc(1, sizeof(*entry), GFP_KERNEL);
762 entry->name = snd_kmalloc_strdup(name, GFP_KERNEL);
763 if (entry->name == NULL) {
767 entry->mode = S_IFREG | S_IRUGO;
768 entry->content = SNDRV_INFO_CONTENT_TEXT;
769 init_MUTEX(&entry->access);
774 * snd_info_create_module_entry - create an info entry for the given module
775 * @module: the module pointer
776 * @name: the file name
777 * @parent: the parent directory
779 * Creates a new info entry and assigns it to the given module.
781 * Returns the pointer of the new instance, or NULL on failure.
783 snd_info_entry_t *snd_info_create_module_entry(struct module * module,
785 snd_info_entry_t *parent)
787 snd_info_entry_t *entry = snd_info_create_entry(name);
789 entry->module = module;
790 entry->parent = parent;
796 * snd_info_create_card_entry - create an info entry for the given card
797 * @card: the card instance
798 * @name: the file name
799 * @parent: the parent directory
801 * Creates a new info entry and assigns it to the given card.
803 * Returns the pointer of the new instance, or NULL on failure.
805 snd_info_entry_t *snd_info_create_card_entry(snd_card_t * card,
807 snd_info_entry_t * parent)
809 snd_info_entry_t *entry = snd_info_create_entry(name);
811 entry->module = card->module;
813 entry->parent = parent;
818 static int snd_info_dev_free_entry(snd_device_t *device)
820 snd_info_entry_t *entry = device->device_data;
821 snd_info_free_entry(entry);
825 static int snd_info_dev_register_entry(snd_device_t *device)
827 snd_info_entry_t *entry = device->device_data;
828 return snd_info_register(entry);
831 static int snd_info_dev_disconnect_entry(snd_device_t *device)
833 snd_info_entry_t *entry = device->device_data;
834 entry->disconnected = 1;
838 static int snd_info_dev_unregister_entry(snd_device_t *device)
840 snd_info_entry_t *entry = device->device_data;
841 return snd_info_unregister(entry);
845 * snd_card_proc_new - create an info entry for the given card
846 * @card: the card instance
847 * @name: the file name
848 * @entryp: the pointer to store the new info entry
850 * Creates a new info entry and assigns it to the given card.
851 * Unlike snd_info_create_card_entry(), this function registers the
852 * info entry as an ALSA device component, so that it can be
853 * unregistered/released without explicit call.
854 * Also, you don't have to register this entry via snd_info_register(),
855 * since this will be registered by snd_card_register() automatically.
857 * The parent is assumed as card->proc_root.
859 * For releasing this entry, use snd_device_free() instead of
860 * snd_info_free_entry().
862 * Returns zero if successful, or a negative error code on failure.
864 int snd_card_proc_new(snd_card_t *card, const char *name,
865 snd_info_entry_t **entryp)
867 static snd_device_ops_t ops = {
868 .dev_free = snd_info_dev_free_entry,
869 .dev_register = snd_info_dev_register_entry,
870 .dev_disconnect = snd_info_dev_disconnect_entry,
871 .dev_unregister = snd_info_dev_unregister_entry
873 snd_info_entry_t *entry;
876 entry = snd_info_create_card_entry(card, name, card->proc_root);
879 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
880 snd_info_free_entry(entry);
889 * snd_info_free_entry - release the info entry
890 * @entry: the info entry
892 * Releases the info entry. Don't call this after registered.
894 void snd_info_free_entry(snd_info_entry_t * entry)
899 kfree((char *)entry->name);
900 if (entry->private_free)
901 entry->private_free(entry);
906 * snd_info_register - register the info entry
907 * @entry: the info entry
909 * Registers the proc info entry.
911 * Returns zero if successful, or a negative error code on failure.
913 int snd_info_register(snd_info_entry_t * entry)
915 struct proc_dir_entry *root, *p = NULL;
917 snd_assert(entry != NULL, return -ENXIO);
918 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
920 p = snd_create_proc_entry(entry->name, entry->mode, root);
925 p->owner = entry->module;
926 if (!S_ISDIR(entry->mode))
927 p->proc_fops = &snd_info_entry_operations;
928 p->size = entry->size;
936 * snd_info_unregister - de-register the info entry
937 * @entry: the info entry
939 * De-registers the info entry and releases the instance.
941 * Returns zero if successful, or a negative error code on failure.
943 int snd_info_unregister(snd_info_entry_t * entry)
945 struct proc_dir_entry *root;
947 snd_assert(entry != NULL && entry->p != NULL, return -ENXIO);
948 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
949 snd_assert(root, return -ENXIO);
951 snd_remove_proc_entry(root, entry->p);
953 snd_info_free_entry(entry);
961 static snd_info_entry_t *snd_info_version_entry = NULL;
963 static void snd_info_version_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
965 static char *kernel_version = system_utsname.release;
968 "Advanced Linux Sound Architecture Driver Version " CONFIG_SND_VERSION CONFIG_SND_DATE ".\n"
969 "Compiled on " __DATE__ " for kernel %s"
974 " with versioned symbols"
976 ".\n", kernel_version);
979 static int __init snd_info_version_init(void)
981 snd_info_entry_t *entry;
983 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
986 entry->c.text.read_size = 256;
987 entry->c.text.read = snd_info_version_read;
988 if (snd_info_register(entry) < 0) {
989 snd_info_free_entry(entry);
992 snd_info_version_entry = entry;
996 static int __exit snd_info_version_done(void)
998 if (snd_info_version_entry)
999 snd_info_unregister(snd_info_version_entry);
1003 #endif /* CONFIG_PROC_FS */