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>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/module.h>
28 #include <sound/core.h>
29 #include <sound/minors.h>
30 #include <sound/info.h>
31 #include <linux/utsname.h>
32 #include <linux/proc_fs.h>
33 #include <linux/mutex.h>
42 int snd_info_check_reserved_words(const char *str)
44 static char *reserved[] =
59 char **xstr = reserved;
62 if (!strcmp(*xstr, str))
66 if (!strncmp(str, "card", 4))
71 static DEFINE_MUTEX(info_mutex);
73 struct snd_info_private_data {
74 struct snd_info_buffer *rbuffer;
75 struct snd_info_buffer *wbuffer;
76 struct snd_info_entry *entry;
77 void *file_private_data;
80 static int snd_info_version_init(void);
81 static int snd_info_version_done(void);
82 static void snd_info_disconnect(struct snd_info_entry *entry);
85 /* resize the proc r/w buffer */
86 static int resize_info_buffer(struct snd_info_buffer *buffer,
91 nsize = PAGE_ALIGN(nsize);
92 nbuf = krealloc(buffer->buffer, nsize, GFP_KERNEL);
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, const 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 void snd_remove_proc_entry(struct proc_dir_entry *parent,
157 struct proc_dir_entry *de)
160 remove_proc_entry(de->name, parent);
163 static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
165 struct snd_info_private_data *data;
166 struct snd_info_entry *entry;
167 loff_t ret = -EINVAL, size;
169 data = file->private_data;
171 mutex_lock(&entry->access);
172 if (entry->content == SNDRV_INFO_CONTENT_DATA &&
173 entry->c.ops->llseek) {
174 offset = entry->c.ops->llseek(entry,
175 data->file_private_data,
179 if (entry->content == SNDRV_INFO_CONTENT_DATA)
187 offset += file->f_pos;
199 if (size && offset > size)
201 file->f_pos = offset;
204 mutex_unlock(&entry->access);
208 static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
209 size_t count, loff_t * offset)
211 struct snd_info_private_data *data;
212 struct snd_info_entry *entry;
213 struct snd_info_buffer *buf;
217 data = file->private_data;
218 if (snd_BUG_ON(!data))
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 (pos >= entry->size)
241 if (entry->c.ops->read) {
242 size = entry->size - pos;
243 size = min(count, size);
244 size = entry->c.ops->read(entry,
245 data->file_private_data,
246 file, buffer, size, pos);
250 if ((ssize_t) size > 0)
251 *offset = pos + size;
255 static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
256 size_t count, loff_t * offset)
258 struct snd_info_private_data *data;
259 struct snd_info_entry *entry;
260 struct snd_info_buffer *buf;
264 data = file->private_data;
265 if (snd_BUG_ON(!data))
269 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
271 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
273 switch (entry->content) {
274 case SNDRV_INFO_CONTENT_TEXT:
278 mutex_lock(&entry->access);
279 if (pos + count >= buf->len) {
280 if (resize_info_buffer(buf, pos + count)) {
281 mutex_unlock(&entry->access);
285 if (copy_from_user(buf->buffer + pos, buffer, count)) {
286 mutex_unlock(&entry->access);
289 buf->size = pos + count;
290 mutex_unlock(&entry->access);
293 case SNDRV_INFO_CONTENT_DATA:
294 if (entry->c.ops->write && count > 0) {
295 size_t maxsize = entry->size - pos;
296 count = min(count, maxsize);
297 size = entry->c.ops->write(entry,
298 data->file_private_data,
299 file, buffer, count, pos);
303 if ((ssize_t) size > 0)
304 *offset = pos + size;
308 static int snd_info_entry_open(struct inode *inode, struct file *file)
310 struct snd_info_entry *entry;
311 struct snd_info_private_data *data;
312 struct snd_info_buffer *buffer;
313 struct proc_dir_entry *p;
316 mutex_lock(&info_mutex);
318 entry = p == NULL ? NULL : (struct snd_info_entry *)p->data;
319 if (entry == NULL || ! entry->p) {
320 mutex_unlock(&info_mutex);
323 if (!try_module_get(entry->module)) {
327 mode = file->f_flags & O_ACCMODE;
328 if (mode == O_RDONLY || mode == O_RDWR) {
329 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
330 entry->c.ops->read == NULL)) {
335 if (mode == O_WRONLY || mode == O_RDWR) {
336 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
337 entry->c.ops->write == NULL)) {
342 data = kzalloc(sizeof(*data), GFP_KERNEL);
348 switch (entry->content) {
349 case SNDRV_INFO_CONTENT_TEXT:
350 if (mode == O_RDONLY || mode == O_RDWR) {
351 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
354 data->rbuffer = buffer;
355 buffer->len = PAGE_SIZE;
356 buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
357 if (buffer->buffer == NULL)
360 if (mode == O_WRONLY || mode == O_RDWR) {
361 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
364 data->wbuffer = buffer;
365 buffer->len = PAGE_SIZE;
366 buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
367 if (buffer->buffer == NULL)
371 case SNDRV_INFO_CONTENT_DATA: /* data */
372 if (entry->c.ops->open) {
373 if ((err = entry->c.ops->open(entry, mode,
374 &data->file_private_data)) < 0) {
381 file->private_data = data;
382 mutex_unlock(&info_mutex);
383 if (entry->content == SNDRV_INFO_CONTENT_TEXT &&
384 (mode == O_RDONLY || mode == O_RDWR)) {
385 if (entry->c.text.read) {
386 mutex_lock(&entry->access);
387 entry->c.text.read(entry, data->rbuffer);
388 mutex_unlock(&entry->access);
395 kfree(data->rbuffer->buffer);
396 kfree(data->rbuffer);
399 kfree(data->wbuffer->buffer);
400 kfree(data->wbuffer);
405 module_put(entry->module);
407 mutex_unlock(&info_mutex);
411 static int snd_info_entry_release(struct inode *inode, struct file *file)
413 struct snd_info_entry *entry;
414 struct snd_info_private_data *data;
417 mode = file->f_flags & O_ACCMODE;
418 data = file->private_data;
420 switch (entry->content) {
421 case SNDRV_INFO_CONTENT_TEXT:
423 kfree(data->rbuffer->buffer);
424 kfree(data->rbuffer);
427 if (entry->c.text.write) {
428 entry->c.text.write(entry, data->wbuffer);
429 if (data->wbuffer->error) {
430 snd_printk(KERN_WARNING "data write error to %s (%i)\n",
432 data->wbuffer->error);
435 kfree(data->wbuffer->buffer);
436 kfree(data->wbuffer);
439 case SNDRV_INFO_CONTENT_DATA:
440 if (entry->c.ops->release)
441 entry->c.ops->release(entry, mode,
442 data->file_private_data);
445 module_put(entry->module);
450 static unsigned int snd_info_entry_poll(struct file *file, poll_table * wait)
452 struct snd_info_private_data *data;
453 struct snd_info_entry *entry;
456 data = file->private_data;
461 switch (entry->content) {
462 case SNDRV_INFO_CONTENT_DATA:
463 if (entry->c.ops->poll)
464 return entry->c.ops->poll(entry,
465 data->file_private_data,
467 if (entry->c.ops->read)
468 mask |= POLLIN | POLLRDNORM;
469 if (entry->c.ops->write)
470 mask |= POLLOUT | POLLWRNORM;
476 static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
479 struct snd_info_private_data *data;
480 struct snd_info_entry *entry;
482 data = file->private_data;
486 switch (entry->content) {
487 case SNDRV_INFO_CONTENT_DATA:
488 if (entry->c.ops->ioctl)
489 return entry->c.ops->ioctl(entry,
490 data->file_private_data,
497 static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
499 struct inode *inode = file_inode(file);
500 struct snd_info_private_data *data;
501 struct snd_info_entry *entry;
503 data = file->private_data;
507 switch (entry->content) {
508 case SNDRV_INFO_CONTENT_DATA:
509 if (entry->c.ops->mmap)
510 return entry->c.ops->mmap(entry,
511 data->file_private_data,
518 static const struct file_operations snd_info_entry_operations =
520 .owner = THIS_MODULE,
521 .llseek = snd_info_entry_llseek,
522 .read = snd_info_entry_read,
523 .write = snd_info_entry_write,
524 .poll = snd_info_entry_poll,
525 .unlocked_ioctl = snd_info_entry_ioctl,
526 .mmap = snd_info_entry_mmap,
527 .open = snd_info_entry_open,
528 .release = snd_info_entry_release,
531 int __init snd_info_init(void)
533 struct proc_dir_entry *p;
535 p = proc_mkdir("asound", NULL);
539 #ifdef CONFIG_SND_OSSEMUL
541 struct snd_info_entry *entry;
542 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
544 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
545 if (snd_info_register(entry) < 0) {
546 snd_info_free_entry(entry);
549 snd_oss_root = entry;
552 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
554 struct snd_info_entry *entry;
555 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL)
557 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
558 if (snd_info_register(entry) < 0) {
559 snd_info_free_entry(entry);
562 snd_seq_root = entry;
565 snd_info_version_init();
566 snd_minor_info_init();
567 snd_minor_info_oss_init();
568 snd_card_info_init();
572 int __exit snd_info_done(void)
574 snd_card_info_done();
575 snd_minor_info_oss_done();
576 snd_minor_info_done();
577 snd_info_version_done();
579 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
580 snd_info_free_entry(snd_seq_root);
582 #ifdef CONFIG_SND_OSSEMUL
583 snd_info_free_entry(snd_oss_root);
585 snd_remove_proc_entry(NULL, snd_proc_root);
596 * create a card proc file
599 int snd_info_card_create(struct snd_card *card)
602 struct snd_info_entry *entry;
604 if (snd_BUG_ON(!card))
607 sprintf(str, "card%i", card->number);
608 if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
610 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
611 if (snd_info_register(entry) < 0) {
612 snd_info_free_entry(entry);
615 card->proc_root = entry;
620 * register the card proc file
623 int snd_info_card_register(struct snd_card *card)
625 struct proc_dir_entry *p;
627 if (snd_BUG_ON(!card))
630 if (!strcmp(card->id, card->proc_root->name))
633 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
636 card->proc_root_link = p;
641 * called on card->id change
643 void snd_info_card_id_change(struct snd_card *card)
645 mutex_lock(&info_mutex);
646 if (card->proc_root_link) {
647 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
648 card->proc_root_link = NULL;
650 if (strcmp(card->id, card->proc_root->name))
651 card->proc_root_link = proc_symlink(card->id,
653 card->proc_root->name);
654 mutex_unlock(&info_mutex);
658 * de-register the card proc file
661 void snd_info_card_disconnect(struct snd_card *card)
665 mutex_lock(&info_mutex);
666 if (card->proc_root_link) {
667 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
668 card->proc_root_link = NULL;
671 snd_info_disconnect(card->proc_root);
672 mutex_unlock(&info_mutex);
676 * release the card proc file resources
679 int snd_info_card_free(struct snd_card *card)
683 snd_info_free_entry(card->proc_root);
684 card->proc_root = NULL;
690 * snd_info_get_line - read one line from the procfs buffer
691 * @buffer: the procfs buffer
692 * @line: the buffer to store
693 * @len: the max. buffer size - 1
695 * Reads one line from the buffer and stores the string.
697 * Returns zero if successful, or 1 if error or EOF.
699 int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
703 if (len <= 0 || buffer->stop || buffer->error)
706 c = buffer->buffer[buffer->curr++];
708 if (buffer->curr >= buffer->size)
713 if (buffer->curr >= buffer->size) {
718 while (c != '\n' && !buffer->stop) {
719 c = buffer->buffer[buffer->curr++];
720 if (buffer->curr >= buffer->size)
727 EXPORT_SYMBOL(snd_info_get_line);
730 * snd_info_get_str - parse a string token
731 * @dest: the buffer to store the string token
732 * @src: the original string
733 * @len: the max. length of token - 1
735 * Parses the original string and copy a token to the given
738 * Returns the updated pointer of the original string so that
739 * it can be used for the next call.
741 const char *snd_info_get_str(char *dest, const char *src, int len)
745 while (*src == ' ' || *src == '\t')
747 if (*src == '"' || *src == '\'') {
749 while (--len > 0 && *src && *src != c) {
755 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
760 while (*src == ' ' || *src == '\t')
765 EXPORT_SYMBOL(snd_info_get_str);
768 * snd_info_create_entry - create an info entry
769 * @name: the proc file name
771 * Creates an info entry with the given file name and initializes as
774 * Usually called from other functions such as
775 * snd_info_create_card_entry().
777 * Returns the pointer of the new instance, or NULL on failure.
779 static struct snd_info_entry *snd_info_create_entry(const char *name)
781 struct snd_info_entry *entry;
782 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
785 entry->name = kstrdup(name, GFP_KERNEL);
786 if (entry->name == NULL) {
790 entry->mode = S_IFREG | S_IRUGO;
791 entry->content = SNDRV_INFO_CONTENT_TEXT;
792 mutex_init(&entry->access);
793 INIT_LIST_HEAD(&entry->children);
794 INIT_LIST_HEAD(&entry->list);
799 * snd_info_create_module_entry - create an info entry for the given module
800 * @module: the module pointer
801 * @name: the file name
802 * @parent: the parent directory
804 * Creates a new info entry and assigns it to the given module.
806 * Returns the pointer of the new instance, or NULL on failure.
808 struct snd_info_entry *snd_info_create_module_entry(struct module * module,
810 struct snd_info_entry *parent)
812 struct snd_info_entry *entry = snd_info_create_entry(name);
814 entry->module = module;
815 entry->parent = parent;
820 EXPORT_SYMBOL(snd_info_create_module_entry);
823 * snd_info_create_card_entry - create an info entry for the given card
824 * @card: the card instance
825 * @name: the file name
826 * @parent: the parent directory
828 * Creates a new info entry and assigns it to the given card.
830 * Returns the pointer of the new instance, or NULL on failure.
832 struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
834 struct snd_info_entry * parent)
836 struct snd_info_entry *entry = snd_info_create_entry(name);
838 entry->module = card->module;
840 entry->parent = parent;
845 EXPORT_SYMBOL(snd_info_create_card_entry);
847 static void snd_info_disconnect(struct snd_info_entry *entry)
849 struct list_head *p, *n;
850 struct proc_dir_entry *root;
852 list_for_each_safe(p, n, &entry->children) {
853 snd_info_disconnect(list_entry(p, struct snd_info_entry, list));
858 list_del_init(&entry->list);
859 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
861 snd_remove_proc_entry(root, entry->p);
865 static int snd_info_dev_free_entry(struct snd_device *device)
867 struct snd_info_entry *entry = device->device_data;
868 snd_info_free_entry(entry);
872 static int snd_info_dev_register_entry(struct snd_device *device)
874 struct snd_info_entry *entry = device->device_data;
875 return snd_info_register(entry);
879 * snd_card_proc_new - create an info entry for the given card
880 * @card: the card instance
881 * @name: the file name
882 * @entryp: the pointer to store the new info entry
884 * Creates a new info entry and assigns it to the given card.
885 * Unlike snd_info_create_card_entry(), this function registers the
886 * info entry as an ALSA device component, so that it can be
887 * unregistered/released without explicit call.
888 * Also, you don't have to register this entry via snd_info_register(),
889 * since this will be registered by snd_card_register() automatically.
891 * The parent is assumed as card->proc_root.
893 * For releasing this entry, use snd_device_free() instead of
894 * snd_info_free_entry().
896 * Returns zero if successful, or a negative error code on failure.
898 int snd_card_proc_new(struct snd_card *card, const char *name,
899 struct snd_info_entry **entryp)
901 static struct snd_device_ops ops = {
902 .dev_free = snd_info_dev_free_entry,
903 .dev_register = snd_info_dev_register_entry,
904 /* disconnect is done via snd_info_card_disconnect() */
906 struct snd_info_entry *entry;
909 entry = snd_info_create_card_entry(card, name, card->proc_root);
912 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
913 snd_info_free_entry(entry);
921 EXPORT_SYMBOL(snd_card_proc_new);
924 * snd_info_free_entry - release the info entry
925 * @entry: the info entry
927 * Releases the info entry. Don't call this after registered.
929 void snd_info_free_entry(struct snd_info_entry * entry)
934 mutex_lock(&info_mutex);
935 snd_info_disconnect(entry);
936 mutex_unlock(&info_mutex);
939 if (entry->private_free)
940 entry->private_free(entry);
944 EXPORT_SYMBOL(snd_info_free_entry);
947 * snd_info_register - register the info entry
948 * @entry: the info entry
950 * Registers the proc info entry.
952 * Returns zero if successful, or a negative error code on failure.
954 int snd_info_register(struct snd_info_entry * entry)
956 struct proc_dir_entry *root, *p = NULL;
958 if (snd_BUG_ON(!entry))
960 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
961 mutex_lock(&info_mutex);
962 p = create_proc_entry(entry->name, entry->mode, root);
964 mutex_unlock(&info_mutex);
967 if (!S_ISDIR(entry->mode))
968 p->proc_fops = &snd_info_entry_operations;
969 p->size = entry->size;
973 list_add_tail(&entry->list, &entry->parent->children);
974 mutex_unlock(&info_mutex);
978 EXPORT_SYMBOL(snd_info_register);
984 static struct snd_info_entry *snd_info_version_entry;
986 static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
989 "Advanced Linux Sound Architecture Driver Version k%s.\n",
990 init_utsname()->release);
993 static int __init snd_info_version_init(void)
995 struct snd_info_entry *entry;
997 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
1000 entry->c.text.read = snd_info_version_read;
1001 if (snd_info_register(entry) < 0) {
1002 snd_info_free_entry(entry);
1005 snd_info_version_entry = entry;
1009 static int __exit snd_info_version_done(void)
1011 snd_info_free_entry(snd_info_version_entry);
1015 #endif /* CONFIG_PROC_FS */