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 <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>
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))
71 static DECLARE_MUTEX(info_mutex);
73 typedef struct _snd_info_private_data {
74 snd_info_buffer_t *rbuffer;
75 snd_info_buffer_t *wbuffer;
76 snd_info_entry_t *entry;
77 void *file_private_data;
78 } snd_info_private_data_t;
80 static int snd_info_version_init(void);
81 static int snd_info_version_done(void);
85 * snd_iprintf - printf on the procfs buffer
86 * @buffer: the procfs buffer
87 * @fmt: the printf format
89 * Outputs the string on the procfs buffer just like printf().
91 * Returns the size of output string.
93 int snd_iprintf(snd_info_buffer_t * buffer, char *fmt,...)
99 if (buffer->stop || buffer->error)
102 res = vscnprintf(sbuffer, sizeof(sbuffer), fmt, args);
104 if (buffer->size + res >= buffer->len) {
108 strcpy(buffer->curr, sbuffer);
118 static struct proc_dir_entry *snd_proc_root = NULL;
119 snd_info_entry_t *snd_seq_root = NULL;
120 #ifdef CONFIG_SND_OSSEMUL
121 snd_info_entry_t *snd_oss_root = NULL;
124 static inline void snd_info_entry_prepare(struct proc_dir_entry *de)
126 de->owner = THIS_MODULE;
129 void snd_remove_proc_entry(struct proc_dir_entry *parent,
130 struct proc_dir_entry *de)
133 remove_proc_entry(de->name, parent);
136 static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
138 snd_info_private_data_t *data;
139 struct snd_info_entry *entry;
142 data = file->private_data;
145 switch (entry->content) {
146 case SNDRV_INFO_CONTENT_TEXT:
148 case 0: /* SEEK_SET */
149 file->f_pos = offset;
152 case 1: /* SEEK_CUR */
153 file->f_pos += offset;
156 case 2: /* SEEK_END */
162 case SNDRV_INFO_CONTENT_DATA:
163 if (entry->c.ops->llseek) {
164 ret = entry->c.ops->llseek(entry,
165 data->file_private_data,
177 static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
178 size_t count, loff_t * offset)
180 snd_info_private_data_t *data;
181 struct snd_info_entry *entry;
182 snd_info_buffer_t *buf;
186 data = file->private_data;
187 snd_assert(data != NULL, return -ENXIO);
189 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
191 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
194 switch (entry->content) {
195 case SNDRV_INFO_CONTENT_TEXT:
199 if (pos >= buf->size)
201 size = buf->size - pos;
202 size = min(count, size);
203 if (copy_to_user(buffer, buf->buffer + pos, size))
206 case SNDRV_INFO_CONTENT_DATA:
207 if (entry->c.ops->read)
208 size = entry->c.ops->read(entry,
209 data->file_private_data,
210 file, buffer, count, pos);
213 if ((ssize_t) size > 0)
214 *offset = pos + size;
218 static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
219 size_t count, loff_t * offset)
221 snd_info_private_data_t *data;
222 struct snd_info_entry *entry;
223 snd_info_buffer_t *buf;
227 data = file->private_data;
228 snd_assert(data != NULL, return -ENXIO);
231 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
233 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
235 switch (entry->content) {
236 case SNDRV_INFO_CONTENT_TEXT:
242 size = buf->len - pos;
243 size = min(count, size);
244 if (copy_from_user(buf->buffer + pos, buffer, size))
246 if ((long)buf->size < pos + size)
247 buf->size = pos + size;
249 case SNDRV_INFO_CONTENT_DATA:
250 if (entry->c.ops->write)
251 size = entry->c.ops->write(entry,
252 data->file_private_data,
253 file, buffer, count, pos);
256 if ((ssize_t) size > 0)
257 *offset = pos + size;
261 static int snd_info_entry_open(struct inode *inode, struct file *file)
263 snd_info_entry_t *entry;
264 snd_info_private_data_t *data;
265 snd_info_buffer_t *buffer;
266 struct proc_dir_entry *p;
271 entry = p == NULL ? NULL : (snd_info_entry_t *)p->data;
272 if (entry == NULL || entry->disconnected) {
276 if (!try_module_get(entry->module)) {
280 mode = file->f_flags & O_ACCMODE;
281 if (mode == O_RDONLY || mode == O_RDWR) {
282 if ((entry->content == SNDRV_INFO_CONTENT_TEXT &&
283 !entry->c.text.read_size) ||
284 (entry->content == SNDRV_INFO_CONTENT_DATA &&
285 entry->c.ops->read == NULL)) {
290 if (mode == O_WRONLY || mode == O_RDWR) {
291 if ((entry->content == SNDRV_INFO_CONTENT_TEXT &&
292 !entry->c.text.write_size) ||
293 (entry->content == SNDRV_INFO_CONTENT_DATA &&
294 entry->c.ops->write == NULL)) {
299 data = kcalloc(1, sizeof(*data), GFP_KERNEL);
305 switch (entry->content) {
306 case SNDRV_INFO_CONTENT_TEXT:
307 if (mode == O_RDONLY || mode == O_RDWR) {
308 buffer = kcalloc(1, sizeof(*buffer), GFP_KERNEL);
309 if (buffer == NULL) {
314 buffer->len = (entry->c.text.read_size +
315 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
316 buffer->buffer = vmalloc(buffer->len);
317 if (buffer->buffer == NULL) {
323 buffer->curr = buffer->buffer;
324 data->rbuffer = buffer;
326 if (mode == O_WRONLY || mode == O_RDWR) {
327 buffer = kcalloc(1, sizeof(*buffer), GFP_KERNEL);
328 if (buffer == NULL) {
329 if (mode == O_RDWR) {
330 vfree(data->rbuffer->buffer);
331 kfree(data->rbuffer);
337 buffer->len = (entry->c.text.write_size +
338 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
339 buffer->buffer = vmalloc(buffer->len);
340 if (buffer->buffer == NULL) {
341 if (mode == O_RDWR) {
342 vfree(data->rbuffer->buffer);
343 kfree(data->rbuffer);
350 buffer->curr = buffer->buffer;
351 data->wbuffer = buffer;
354 case SNDRV_INFO_CONTENT_DATA: /* data */
355 if (entry->c.ops->open) {
356 if ((err = entry->c.ops->open(entry, mode,
357 &data->file_private_data)) < 0) {
364 file->private_data = data;
366 if (entry->content == SNDRV_INFO_CONTENT_TEXT &&
367 (mode == O_RDONLY || mode == O_RDWR)) {
368 if (entry->c.text.read) {
369 down(&entry->access);
370 entry->c.text.read(entry, data->rbuffer);
377 module_put(entry->module);
383 static int snd_info_entry_release(struct inode *inode, struct file *file)
385 snd_info_entry_t *entry;
386 snd_info_private_data_t *data;
389 mode = file->f_flags & O_ACCMODE;
390 data = file->private_data;
392 switch (entry->content) {
393 case SNDRV_INFO_CONTENT_TEXT:
394 if (mode == O_RDONLY || mode == O_RDWR) {
395 vfree(data->rbuffer->buffer);
396 kfree(data->rbuffer);
398 if (mode == O_WRONLY || mode == O_RDWR) {
399 if (entry->c.text.write) {
400 entry->c.text.write(entry, data->wbuffer);
401 if (data->wbuffer->error) {
402 snd_printk(KERN_WARNING "data write error to %s (%i)\n",
404 data->wbuffer->error);
407 vfree(data->wbuffer->buffer);
408 kfree(data->wbuffer);
411 case SNDRV_INFO_CONTENT_DATA:
412 if (entry->c.ops->release)
413 entry->c.ops->release(entry, mode,
414 data->file_private_data);
417 module_put(entry->module);
422 static unsigned int snd_info_entry_poll(struct file *file, poll_table * wait)
424 snd_info_private_data_t *data;
425 struct snd_info_entry *entry;
428 data = file->private_data;
433 switch (entry->content) {
434 case SNDRV_INFO_CONTENT_DATA:
435 if (entry->c.ops->poll)
436 return entry->c.ops->poll(entry,
437 data->file_private_data,
439 if (entry->c.ops->read)
440 mask |= POLLIN | POLLRDNORM;
441 if (entry->c.ops->write)
442 mask |= POLLOUT | POLLWRNORM;
448 static inline int _snd_info_entry_ioctl(struct inode *inode, struct file *file,
449 unsigned int cmd, unsigned long arg)
451 snd_info_private_data_t *data;
452 struct snd_info_entry *entry;
454 data = file->private_data;
458 switch (entry->content) {
459 case SNDRV_INFO_CONTENT_DATA:
460 if (entry->c.ops->ioctl)
461 return entry->c.ops->ioctl(entry,
462 data->file_private_data,
469 /* FIXME: need to unlock BKL to allow preemption */
470 static int snd_info_entry_ioctl(struct inode *inode, struct file *file,
471 unsigned int cmd, unsigned long arg)
475 err = _snd_info_entry_ioctl(inode, file, cmd, arg);
480 static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
482 struct inode *inode = file->f_dentry->d_inode;
483 snd_info_private_data_t *data;
484 struct snd_info_entry *entry;
486 data = file->private_data;
490 switch (entry->content) {
491 case SNDRV_INFO_CONTENT_DATA:
492 if (entry->c.ops->mmap)
493 return entry->c.ops->mmap(entry,
494 data->file_private_data,
501 static struct file_operations snd_info_entry_operations =
503 .owner = THIS_MODULE,
504 .llseek = snd_info_entry_llseek,
505 .read = snd_info_entry_read,
506 .write = snd_info_entry_write,
507 .poll = snd_info_entry_poll,
508 .ioctl = snd_info_entry_ioctl,
509 .mmap = snd_info_entry_mmap,
510 .open = snd_info_entry_open,
511 .release = snd_info_entry_release,
515 * snd_create_proc_entry - create a procfs entry
516 * @name: the name of the proc file
517 * @mode: the file permission bits, S_Ixxx
518 * @parent: the parent proc-directory entry
520 * Creates a new proc file entry with the given name and permission
521 * on the given directory.
523 * Returns the pointer of new instance or NULL on failure.
525 struct proc_dir_entry *snd_create_proc_entry(const char *name, mode_t mode,
526 struct proc_dir_entry *parent)
528 struct proc_dir_entry *p;
529 p = create_proc_entry(name, mode, parent);
531 snd_info_entry_prepare(p);
535 int __init snd_info_init(void)
537 struct proc_dir_entry *p;
539 p = snd_create_proc_entry("asound", S_IFDIR | S_IRUGO | S_IXUGO, &proc_root);
543 #ifdef CONFIG_SND_OSSEMUL
545 snd_info_entry_t *entry;
546 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
548 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
549 if (snd_info_register(entry) < 0) {
550 snd_info_free_entry(entry);
553 snd_oss_root = entry;
556 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
558 snd_info_entry_t *entry;
559 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL)
561 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
562 if (snd_info_register(entry) < 0) {
563 snd_info_free_entry(entry);
566 snd_seq_root = entry;
569 snd_info_version_init();
570 snd_memory_info_init();
571 snd_minor_info_init();
572 snd_minor_info_oss_init();
573 snd_card_info_init();
577 int __exit snd_info_done(void)
579 snd_card_info_done();
580 snd_minor_info_oss_done();
581 snd_minor_info_done();
582 snd_memory_info_done();
583 snd_info_version_done();
585 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
587 snd_info_unregister(snd_seq_root);
589 #ifdef CONFIG_SND_OSSEMUL
591 snd_info_unregister(snd_oss_root);
593 snd_remove_proc_entry(&proc_root, snd_proc_root);
604 * create a card proc file
607 int snd_info_card_create(snd_card_t * card)
610 snd_info_entry_t *entry;
612 snd_assert(card != NULL, return -ENXIO);
614 sprintf(str, "card%i", card->number);
615 if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
617 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
618 if (snd_info_register(entry) < 0) {
619 snd_info_free_entry(entry);
622 card->proc_root = entry;
627 * register the card proc file
630 int snd_info_card_register(snd_card_t * card)
632 struct proc_dir_entry *p;
634 snd_assert(card != NULL, return -ENXIO);
636 if (!strcmp(card->id, card->proc_root->name))
639 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
642 card->proc_root_link = p;
647 * de-register the card proc file
650 int snd_info_card_free(snd_card_t * card)
652 snd_assert(card != NULL, return -ENXIO);
653 if (card->proc_root_link) {
654 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
655 card->proc_root_link = NULL;
657 if (card->proc_root) {
658 snd_info_unregister(card->proc_root);
659 card->proc_root = NULL;
666 * snd_info_get_line - read one line from the procfs buffer
667 * @buffer: the procfs buffer
668 * @line: the buffer to store
669 * @len: the max. buffer size - 1
671 * Reads one line from the buffer and stores the string.
673 * Returns zero if successful, or 1 if error or EOF.
675 int snd_info_get_line(snd_info_buffer_t * buffer, char *line, int len)
679 if (len <= 0 || buffer->stop || buffer->error)
684 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
690 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
695 while (c != '\n' && !buffer->stop) {
697 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
706 * snd_info_get_line - parse a string token
707 * @dest: the buffer to store the string token
708 * @src: the original string
709 * @len: the max. length of token - 1
711 * Parses the original string and copy a token to the given
714 * Returns the updated pointer of the original string so that
715 * it can be used for the next call.
717 char *snd_info_get_str(char *dest, char *src, int len)
721 while (*src == ' ' || *src == '\t')
723 if (*src == '"' || *src == '\'') {
725 while (--len > 0 && *src && *src != c) {
731 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
736 while (*src == ' ' || *src == '\t')
742 * snd_info_create_entry - create an info entry
743 * @name: the proc file name
745 * Creates an info entry with the given file name and initializes as
748 * Usually called from other functions such as
749 * snd_info_create_card_entry().
751 * Returns the pointer of the new instance, or NULL on failure.
753 static snd_info_entry_t *snd_info_create_entry(const char *name)
755 snd_info_entry_t *entry;
756 entry = kcalloc(1, sizeof(*entry), GFP_KERNEL);
759 entry->name = snd_kmalloc_strdup(name, GFP_KERNEL);
760 if (entry->name == NULL) {
764 entry->mode = S_IFREG | S_IRUGO;
765 entry->content = SNDRV_INFO_CONTENT_TEXT;
766 init_MUTEX(&entry->access);
771 * snd_info_create_module_entry - create an info entry for the given module
772 * @module: the module pointer
773 * @name: the file name
774 * @parent: the parent directory
776 * Creates a new info entry and assigns it to the given module.
778 * Returns the pointer of the new instance, or NULL on failure.
780 snd_info_entry_t *snd_info_create_module_entry(struct module * module,
782 snd_info_entry_t *parent)
784 snd_info_entry_t *entry = snd_info_create_entry(name);
786 entry->module = module;
787 entry->parent = parent;
793 * snd_info_create_card_entry - create an info entry for the given card
794 * @card: the card instance
795 * @name: the file name
796 * @parent: the parent directory
798 * Creates a new info entry and assigns it to the given card.
800 * Returns the pointer of the new instance, or NULL on failure.
802 snd_info_entry_t *snd_info_create_card_entry(snd_card_t * card,
804 snd_info_entry_t * parent)
806 snd_info_entry_t *entry = snd_info_create_entry(name);
808 entry->module = card->module;
810 entry->parent = parent;
815 static int snd_info_dev_free_entry(snd_device_t *device)
817 snd_info_entry_t *entry = device->device_data;
818 snd_info_free_entry(entry);
822 static int snd_info_dev_register_entry(snd_device_t *device)
824 snd_info_entry_t *entry = device->device_data;
825 return snd_info_register(entry);
828 static int snd_info_dev_disconnect_entry(snd_device_t *device)
830 snd_info_entry_t *entry = device->device_data;
831 entry->disconnected = 1;
835 static int snd_info_dev_unregister_entry(snd_device_t *device)
837 snd_info_entry_t *entry = device->device_data;
838 return snd_info_unregister(entry);
842 * snd_card_proc_new - create an info entry for the given card
843 * @card: the card instance
844 * @name: the file name
845 * @entryp: the pointer to store the new info entry
847 * Creates a new info entry and assigns it to the given card.
848 * Unlike snd_info_create_card_entry(), this function registers the
849 * info entry as an ALSA device component, so that it can be
850 * unregistered/released without explicit call.
851 * Also, you don't have to register this entry via snd_info_register(),
852 * since this will be registered by snd_card_register() automatically.
854 * The parent is assumed as card->proc_root.
856 * For releasing this entry, use snd_device_free() instead of
857 * snd_info_free_entry().
859 * Returns zero if successful, or a negative error code on failure.
861 int snd_card_proc_new(snd_card_t *card, const char *name,
862 snd_info_entry_t **entryp)
864 static snd_device_ops_t ops = {
865 .dev_free = snd_info_dev_free_entry,
866 .dev_register = snd_info_dev_register_entry,
867 .dev_disconnect = snd_info_dev_disconnect_entry,
868 .dev_unregister = snd_info_dev_unregister_entry
870 snd_info_entry_t *entry;
873 entry = snd_info_create_card_entry(card, name, card->proc_root);
876 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
877 snd_info_free_entry(entry);
886 * snd_info_free_entry - release the info entry
887 * @entry: the info entry
889 * Releases the info entry. Don't call this after registered.
891 void snd_info_free_entry(snd_info_entry_t * entry)
896 kfree((char *)entry->name);
897 if (entry->private_free)
898 entry->private_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(snd_info_entry_t * 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;
917 p = snd_create_proc_entry(entry->name, entry->mode, root);
922 p->owner = entry->module;
923 if (!S_ISDIR(entry->mode))
924 p->proc_fops = &snd_info_entry_operations;
925 p->size = entry->size;
933 * snd_info_unregister - de-register the info entry
934 * @entry: the info entry
936 * De-registers the info entry and releases the instance.
938 * Returns zero if successful, or a negative error code on failure.
940 int snd_info_unregister(snd_info_entry_t * entry)
942 struct proc_dir_entry *root;
944 snd_assert(entry != NULL && entry->p != NULL, return -ENXIO);
945 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
946 snd_assert(root, return -ENXIO);
948 snd_remove_proc_entry(root, entry->p);
950 snd_info_free_entry(entry);
958 static snd_info_entry_t *snd_info_version_entry = NULL;
960 static void snd_info_version_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
962 static char *kernel_version = UTS_RELEASE;
965 "Advanced Linux Sound Architecture Driver Version " CONFIG_SND_VERSION CONFIG_SND_DATE ".\n"
966 "Compiled on " __DATE__ " for kernel %s"
971 " with versioned symbols"
973 ".\n", kernel_version);
976 static int __init snd_info_version_init(void)
978 snd_info_entry_t *entry;
980 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
983 entry->c.text.read_size = 256;
984 entry->c.text.read = snd_info_version_read;
985 if (snd_info_register(entry) < 0) {
986 snd_info_free_entry(entry);
989 snd_info_version_entry = entry;
993 static int __exit snd_info_version_done(void)
995 if (snd_info_version_entry)
996 snd_info_unregister(snd_info_version_entry);
1000 #endif /* CONFIG_PROC_FS */