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);
118 static struct proc_dir_entry *snd_proc_root = NULL;
119 struct snd_info_entry *snd_seq_root = NULL;
120 #ifdef CONFIG_SND_OSSEMUL
121 struct snd_info_entry *snd_oss_root = NULL;
124 static inline void snd_info_entry_prepare(struct proc_dir_entry *de)
126 de->owner = THIS_MODULE;
129 static 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 struct snd_info_private_data *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 struct snd_info_private_data *data;
181 struct snd_info_entry *entry;
182 struct snd_info_buffer *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 struct snd_info_private_data *data;
222 struct snd_info_entry *entry;
223 struct snd_info_buffer *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 struct snd_info_entry *entry;
264 struct snd_info_private_data *data;
265 struct snd_info_buffer *buffer;
266 struct proc_dir_entry *p;
269 mutex_lock(&info_mutex);
271 entry = p == NULL ? NULL : (struct snd_info_entry *)p->data;
272 if (entry == NULL || entry->disconnected) {
273 mutex_unlock(&info_mutex);
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 = kzalloc(sizeof(*data), GFP_KERNEL);
305 switch (entry->content) {
306 case SNDRV_INFO_CONTENT_TEXT:
307 if (mode == O_RDONLY || mode == O_RDWR) {
308 buffer = kzalloc(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 = kzalloc(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;
365 mutex_unlock(&info_mutex);
366 if (entry->content == SNDRV_INFO_CONTENT_TEXT &&
367 (mode == O_RDONLY || mode == O_RDWR)) {
368 if (entry->c.text.read) {
369 mutex_lock(&entry->access);
370 entry->c.text.read(entry, data->rbuffer);
371 mutex_unlock(&entry->access);
377 module_put(entry->module);
379 mutex_unlock(&info_mutex);
383 static int snd_info_entry_release(struct inode *inode, struct file *file)
385 struct snd_info_entry *entry;
386 struct snd_info_private_data *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 struct snd_info_private_data *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 long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
451 struct snd_info_private_data *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 static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
471 struct inode *inode = file->f_dentry->d_inode;
472 struct snd_info_private_data *data;
473 struct snd_info_entry *entry;
475 data = file->private_data;
479 switch (entry->content) {
480 case SNDRV_INFO_CONTENT_DATA:
481 if (entry->c.ops->mmap)
482 return entry->c.ops->mmap(entry,
483 data->file_private_data,
490 static struct file_operations snd_info_entry_operations =
492 .owner = THIS_MODULE,
493 .llseek = snd_info_entry_llseek,
494 .read = snd_info_entry_read,
495 .write = snd_info_entry_write,
496 .poll = snd_info_entry_poll,
497 .unlocked_ioctl = snd_info_entry_ioctl,
498 .mmap = snd_info_entry_mmap,
499 .open = snd_info_entry_open,
500 .release = snd_info_entry_release,
504 * snd_create_proc_entry - create a procfs entry
505 * @name: the name of the proc file
506 * @mode: the file permission bits, S_Ixxx
507 * @parent: the parent proc-directory entry
509 * Creates a new proc file entry with the given name and permission
510 * on the given directory.
512 * Returns the pointer of new instance or NULL on failure.
514 static struct proc_dir_entry *snd_create_proc_entry(const char *name, mode_t mode,
515 struct proc_dir_entry *parent)
517 struct proc_dir_entry *p;
518 p = create_proc_entry(name, mode, parent);
520 snd_info_entry_prepare(p);
524 int __init snd_info_init(void)
526 struct proc_dir_entry *p;
528 p = snd_create_proc_entry("asound", S_IFDIR | S_IRUGO | S_IXUGO, &proc_root);
532 #ifdef CONFIG_SND_OSSEMUL
534 struct snd_info_entry *entry;
535 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
537 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
538 if (snd_info_register(entry) < 0) {
539 snd_info_free_entry(entry);
542 snd_oss_root = entry;
545 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
547 struct snd_info_entry *entry;
548 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL)
550 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
551 if (snd_info_register(entry) < 0) {
552 snd_info_free_entry(entry);
555 snd_seq_root = entry;
558 snd_info_version_init();
559 snd_minor_info_init();
560 snd_minor_info_oss_init();
561 snd_card_info_init();
565 int __exit snd_info_done(void)
567 snd_card_info_done();
568 snd_minor_info_oss_done();
569 snd_minor_info_done();
570 snd_info_version_done();
572 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
573 snd_info_unregister(snd_seq_root);
575 #ifdef CONFIG_SND_OSSEMUL
576 snd_info_unregister(snd_oss_root);
578 snd_remove_proc_entry(&proc_root, snd_proc_root);
589 * create a card proc file
592 int snd_info_card_create(struct snd_card *card)
595 struct snd_info_entry *entry;
597 snd_assert(card != NULL, return -ENXIO);
599 sprintf(str, "card%i", card->number);
600 if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
602 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
603 if (snd_info_register(entry) < 0) {
604 snd_info_free_entry(entry);
607 card->proc_root = entry;
612 * register the card proc file
615 int snd_info_card_register(struct snd_card *card)
617 struct proc_dir_entry *p;
619 snd_assert(card != NULL, return -ENXIO);
621 if (!strcmp(card->id, card->proc_root->name))
624 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
627 card->proc_root_link = p;
632 * de-register the card proc file
635 int snd_info_card_free(struct snd_card *card)
637 snd_assert(card != NULL, return -ENXIO);
638 if (card->proc_root_link) {
639 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
640 card->proc_root_link = NULL;
642 if (card->proc_root) {
643 snd_info_unregister(card->proc_root);
644 card->proc_root = NULL;
651 * snd_info_get_line - read one line from the procfs buffer
652 * @buffer: the procfs buffer
653 * @line: the buffer to store
654 * @len: the max. buffer size - 1
656 * Reads one line from the buffer and stores the string.
658 * Returns zero if successful, or 1 if error or EOF.
660 int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
664 if (len <= 0 || buffer->stop || buffer->error)
669 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
675 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
680 while (c != '\n' && !buffer->stop) {
682 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
691 * snd_info_get_str - parse a string token
692 * @dest: the buffer to store the string token
693 * @src: the original string
694 * @len: the max. length of token - 1
696 * Parses the original string and copy a token to the given
699 * Returns the updated pointer of the original string so that
700 * it can be used for the next call.
702 char *snd_info_get_str(char *dest, char *src, int len)
706 while (*src == ' ' || *src == '\t')
708 if (*src == '"' || *src == '\'') {
710 while (--len > 0 && *src && *src != c) {
716 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
721 while (*src == ' ' || *src == '\t')
727 * snd_info_create_entry - create an info entry
728 * @name: the proc file name
730 * Creates an info entry with the given file name and initializes as
733 * Usually called from other functions such as
734 * snd_info_create_card_entry().
736 * Returns the pointer of the new instance, or NULL on failure.
738 static struct snd_info_entry *snd_info_create_entry(const char *name)
740 struct snd_info_entry *entry;
741 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
744 entry->name = kstrdup(name, GFP_KERNEL);
745 if (entry->name == NULL) {
749 entry->mode = S_IFREG | S_IRUGO;
750 entry->content = SNDRV_INFO_CONTENT_TEXT;
751 mutex_init(&entry->access);
756 * snd_info_create_module_entry - create an info entry for the given module
757 * @module: the module pointer
758 * @name: the file name
759 * @parent: the parent directory
761 * Creates a new info entry and assigns it to the given module.
763 * Returns the pointer of the new instance, or NULL on failure.
765 struct snd_info_entry *snd_info_create_module_entry(struct module * module,
767 struct snd_info_entry *parent)
769 struct snd_info_entry *entry = snd_info_create_entry(name);
771 entry->module = module;
772 entry->parent = parent;
778 * snd_info_create_card_entry - create an info entry for the given card
779 * @card: the card instance
780 * @name: the file name
781 * @parent: the parent directory
783 * Creates a new info entry and assigns it to the given card.
785 * Returns the pointer of the new instance, or NULL on failure.
787 struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
789 struct snd_info_entry * parent)
791 struct snd_info_entry *entry = snd_info_create_entry(name);
793 entry->module = card->module;
795 entry->parent = parent;
800 static int snd_info_dev_free_entry(struct snd_device *device)
802 struct snd_info_entry *entry = device->device_data;
803 snd_info_free_entry(entry);
807 static int snd_info_dev_register_entry(struct snd_device *device)
809 struct snd_info_entry *entry = device->device_data;
810 return snd_info_register(entry);
813 static int snd_info_dev_disconnect_entry(struct snd_device *device)
815 struct snd_info_entry *entry = device->device_data;
816 entry->disconnected = 1;
820 static int snd_info_dev_unregister_entry(struct snd_device *device)
822 struct snd_info_entry *entry = device->device_data;
823 return snd_info_unregister(entry);
827 * snd_card_proc_new - create an info entry for the given card
828 * @card: the card instance
829 * @name: the file name
830 * @entryp: the pointer to store the new info entry
832 * Creates a new info entry and assigns it to the given card.
833 * Unlike snd_info_create_card_entry(), this function registers the
834 * info entry as an ALSA device component, so that it can be
835 * unregistered/released without explicit call.
836 * Also, you don't have to register this entry via snd_info_register(),
837 * since this will be registered by snd_card_register() automatically.
839 * The parent is assumed as card->proc_root.
841 * For releasing this entry, use snd_device_free() instead of
842 * snd_info_free_entry().
844 * Returns zero if successful, or a negative error code on failure.
846 int snd_card_proc_new(struct snd_card *card, const char *name,
847 struct snd_info_entry **entryp)
849 static struct snd_device_ops ops = {
850 .dev_free = snd_info_dev_free_entry,
851 .dev_register = snd_info_dev_register_entry,
852 .dev_disconnect = snd_info_dev_disconnect_entry,
853 .dev_unregister = snd_info_dev_unregister_entry
855 struct snd_info_entry *entry;
858 entry = snd_info_create_card_entry(card, name, card->proc_root);
861 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
862 snd_info_free_entry(entry);
871 * snd_info_free_entry - release the info entry
872 * @entry: the info entry
874 * Releases the info entry. Don't call this after registered.
876 void snd_info_free_entry(struct snd_info_entry * entry)
881 if (entry->private_free)
882 entry->private_free(entry);
887 * snd_info_register - register the info entry
888 * @entry: the info entry
890 * Registers the proc info entry.
892 * Returns zero if successful, or a negative error code on failure.
894 int snd_info_register(struct snd_info_entry * entry)
896 struct proc_dir_entry *root, *p = NULL;
898 snd_assert(entry != NULL, return -ENXIO);
899 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
900 mutex_lock(&info_mutex);
901 p = snd_create_proc_entry(entry->name, entry->mode, root);
903 mutex_unlock(&info_mutex);
906 p->owner = entry->module;
907 if (!S_ISDIR(entry->mode))
908 p->proc_fops = &snd_info_entry_operations;
909 p->size = entry->size;
912 mutex_unlock(&info_mutex);
917 * snd_info_unregister - de-register the info entry
918 * @entry: the info entry
920 * De-registers the info entry and releases the instance.
922 * Returns zero if successful, or a negative error code on failure.
924 int snd_info_unregister(struct snd_info_entry * entry)
926 struct proc_dir_entry *root;
930 snd_assert(entry->p != NULL, return -ENXIO);
931 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
932 snd_assert(root, return -ENXIO);
933 mutex_lock(&info_mutex);
934 snd_remove_proc_entry(root, entry->p);
935 mutex_unlock(&info_mutex);
936 snd_info_free_entry(entry);
944 static struct snd_info_entry *snd_info_version_entry = NULL;
946 static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
949 "Advanced Linux Sound Architecture Driver Version "
950 CONFIG_SND_VERSION CONFIG_SND_DATE ".\n"
954 static int __init snd_info_version_init(void)
956 struct snd_info_entry *entry;
958 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
961 entry->c.text.read_size = 256;
962 entry->c.text.read = snd_info_version_read;
963 if (snd_info_register(entry) < 0) {
964 snd_info_free_entry(entry);
967 snd_info_version_entry = entry;
971 static int __exit snd_info_version_done(void)
973 if (snd_info_version_entry)
974 snd_info_unregister(snd_info_version_entry);
978 #endif /* CONFIG_PROC_FS */