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 void snd_info_disconnect(struct snd_info_entry
*entry
);
87 static struct snd_info_entry
*snd_proc_root
;
88 struct snd_info_entry
*snd_seq_root
;
89 EXPORT_SYMBOL(snd_seq_root
);
91 #ifdef CONFIG_SND_OSSEMUL
92 struct snd_info_entry
*snd_oss_root
;
95 static int alloc_info_private(struct snd_info_entry
*entry
,
96 struct snd_info_private_data
**ret
)
98 struct snd_info_private_data
*data
;
100 if (!entry
|| !entry
->p
)
102 if (!try_module_get(entry
->module
))
104 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
106 module_put(entry
->module
);
114 static bool valid_pos(loff_t pos
, size_t count
)
116 if (pos
< 0 || (long) pos
!= pos
|| (ssize_t
) count
< 0)
118 if ((unsigned long) pos
+ (unsigned long) count
< (unsigned long) pos
)
124 * file ops for binary proc files
126 static loff_t
snd_info_entry_llseek(struct file
*file
, loff_t offset
, int orig
)
128 struct snd_info_private_data
*data
;
129 struct snd_info_entry
*entry
;
130 loff_t ret
= -EINVAL
, size
;
132 data
= file
->private_data
;
134 mutex_lock(&entry
->access
);
135 if (entry
->c
.ops
->llseek
) {
136 offset
= entry
->c
.ops
->llseek(entry
,
137 data
->file_private_data
,
147 offset
+= file
->f_pos
;
159 if (size
&& offset
> size
)
161 file
->f_pos
= offset
;
164 mutex_unlock(&entry
->access
);
168 static ssize_t
snd_info_entry_read(struct file
*file
, char __user
*buffer
,
169 size_t count
, loff_t
* offset
)
171 struct snd_info_private_data
*data
= file
->private_data
;
172 struct snd_info_entry
*entry
= data
->entry
;
177 if (!valid_pos(pos
, count
))
179 if (pos
>= entry
->size
)
181 size
= entry
->size
- pos
;
182 size
= min(count
, size
);
183 size
= entry
->c
.ops
->read(entry
, data
->file_private_data
,
184 file
, buffer
, size
, pos
);
185 if ((ssize_t
) size
> 0)
186 *offset
= pos
+ size
;
190 static ssize_t
snd_info_entry_write(struct file
*file
, const char __user
*buffer
,
191 size_t count
, loff_t
* offset
)
193 struct snd_info_private_data
*data
= file
->private_data
;
194 struct snd_info_entry
*entry
= data
->entry
;
199 if (!valid_pos(pos
, count
))
202 size_t maxsize
= entry
->size
- pos
;
203 count
= min(count
, maxsize
);
204 size
= entry
->c
.ops
->write(entry
, data
->file_private_data
,
205 file
, buffer
, count
, pos
);
208 *offset
= pos
+ size
;
212 static unsigned int snd_info_entry_poll(struct file
*file
, poll_table
*wait
)
214 struct snd_info_private_data
*data
= file
->private_data
;
215 struct snd_info_entry
*entry
= data
->entry
;
216 unsigned int mask
= 0;
218 if (entry
->c
.ops
->poll
)
219 return entry
->c
.ops
->poll(entry
,
220 data
->file_private_data
,
222 if (entry
->c
.ops
->read
)
223 mask
|= POLLIN
| POLLRDNORM
;
224 if (entry
->c
.ops
->write
)
225 mask
|= POLLOUT
| POLLWRNORM
;
229 static long snd_info_entry_ioctl(struct file
*file
, unsigned int cmd
,
232 struct snd_info_private_data
*data
= file
->private_data
;
233 struct snd_info_entry
*entry
= data
->entry
;
235 if (!entry
->c
.ops
->ioctl
)
237 return entry
->c
.ops
->ioctl(entry
, data
->file_private_data
,
241 static int snd_info_entry_mmap(struct file
*file
, struct vm_area_struct
*vma
)
243 struct inode
*inode
= file_inode(file
);
244 struct snd_info_private_data
*data
;
245 struct snd_info_entry
*entry
;
247 data
= file
->private_data
;
251 if (!entry
->c
.ops
->mmap
)
253 return entry
->c
.ops
->mmap(entry
, data
->file_private_data
,
257 static int snd_info_entry_open(struct inode
*inode
, struct file
*file
)
259 struct snd_info_entry
*entry
= PDE_DATA(inode
);
260 struct snd_info_private_data
*data
;
263 mutex_lock(&info_mutex
);
264 err
= alloc_info_private(entry
, &data
);
268 mode
= file
->f_flags
& O_ACCMODE
;
269 if (((mode
== O_RDONLY
|| mode
== O_RDWR
) && !entry
->c
.ops
->read
) ||
270 ((mode
== O_WRONLY
|| mode
== O_RDWR
) && !entry
->c
.ops
->write
)) {
275 if (entry
->c
.ops
->open
) {
276 err
= entry
->c
.ops
->open(entry
, mode
, &data
->file_private_data
);
281 file
->private_data
= data
;
282 mutex_unlock(&info_mutex
);
287 module_put(entry
->module
);
289 mutex_unlock(&info_mutex
);
293 static int snd_info_entry_release(struct inode
*inode
, struct file
*file
)
295 struct snd_info_private_data
*data
= file
->private_data
;
296 struct snd_info_entry
*entry
= data
->entry
;
298 if (entry
->c
.ops
->release
)
299 entry
->c
.ops
->release(entry
, file
->f_flags
& O_ACCMODE
,
300 data
->file_private_data
);
301 module_put(entry
->module
);
306 static const struct file_operations snd_info_entry_operations
=
308 .owner
= THIS_MODULE
,
309 .llseek
= snd_info_entry_llseek
,
310 .read
= snd_info_entry_read
,
311 .write
= snd_info_entry_write
,
312 .poll
= snd_info_entry_poll
,
313 .unlocked_ioctl
= snd_info_entry_ioctl
,
314 .mmap
= snd_info_entry_mmap
,
315 .open
= snd_info_entry_open
,
316 .release
= snd_info_entry_release
,
320 * file ops for text proc files
322 static ssize_t
snd_info_text_entry_write(struct file
*file
,
323 const char __user
*buffer
,
324 size_t count
, loff_t
*offset
)
326 struct seq_file
*m
= file
->private_data
;
327 struct snd_info_private_data
*data
= m
->private;
328 struct snd_info_entry
*entry
= data
->entry
;
329 struct snd_info_buffer
*buf
;
335 if (!valid_pos(pos
, count
))
338 mutex_lock(&entry
->access
);
341 data
->wbuffer
= buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
347 if (next
> buf
->len
) {
348 char *nbuf
= krealloc(buf
->buffer
, PAGE_ALIGN(next
),
349 GFP_KERNEL
| __GFP_ZERO
);
355 buf
->len
= PAGE_ALIGN(next
);
357 if (copy_from_user(buf
->buffer
+ pos
, buffer
, count
)) {
363 mutex_unlock(&entry
->access
);
370 static int snd_info_seq_show(struct seq_file
*seq
, void *p
)
372 struct snd_info_private_data
*data
= seq
->private;
373 struct snd_info_entry
*entry
= data
->entry
;
375 if (entry
->c
.text
.read
) {
376 data
->rbuffer
->buffer
= (char *)seq
; /* XXX hack! */
377 entry
->c
.text
.read(entry
, data
->rbuffer
);
382 static int snd_info_text_entry_open(struct inode
*inode
, struct file
*file
)
384 struct snd_info_entry
*entry
= PDE_DATA(inode
);
385 struct snd_info_private_data
*data
;
388 mutex_lock(&info_mutex
);
389 err
= alloc_info_private(entry
, &data
);
393 data
->rbuffer
= kzalloc(sizeof(*data
->rbuffer
), GFP_KERNEL
);
394 if (!data
->rbuffer
) {
399 err
= single_open_size(file
, snd_info_seq_show
, data
,
402 err
= single_open(file
, snd_info_seq_show
, data
);
405 mutex_unlock(&info_mutex
);
409 kfree(data
->rbuffer
);
411 module_put(entry
->module
);
413 mutex_unlock(&info_mutex
);
417 static int snd_info_text_entry_release(struct inode
*inode
, struct file
*file
)
419 struct seq_file
*m
= file
->private_data
;
420 struct snd_info_private_data
*data
= m
->private;
421 struct snd_info_entry
*entry
= data
->entry
;
423 if (data
->wbuffer
&& entry
->c
.text
.write
)
424 entry
->c
.text
.write(entry
, data
->wbuffer
);
426 single_release(inode
, file
);
427 kfree(data
->rbuffer
);
429 kfree(data
->wbuffer
->buffer
);
430 kfree(data
->wbuffer
);
433 module_put(entry
->module
);
438 static const struct file_operations snd_info_text_entry_ops
=
440 .owner
= THIS_MODULE
,
441 .open
= snd_info_text_entry_open
,
442 .release
= snd_info_text_entry_release
,
443 .write
= snd_info_text_entry_write
,
448 static struct snd_info_entry
*create_subdir(struct module
*mod
,
451 struct snd_info_entry
*entry
;
453 entry
= snd_info_create_module_entry(mod
, name
, NULL
);
456 entry
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
457 if (snd_info_register(entry
) < 0) {
458 snd_info_free_entry(entry
);
464 static struct snd_info_entry
*snd_info_create_entry(const char *name
);
466 int __init
snd_info_init(void)
468 snd_proc_root
= snd_info_create_entry("asound");
471 snd_proc_root
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
472 snd_proc_root
->p
= proc_mkdir("asound", NULL
);
473 if (!snd_proc_root
->p
)
475 #ifdef CONFIG_SND_OSSEMUL
476 snd_oss_root
= create_subdir(THIS_MODULE
, "oss");
480 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
481 snd_seq_root
= create_subdir(THIS_MODULE
, "seq");
485 if (snd_info_version_init() < 0 ||
486 snd_minor_info_init() < 0 ||
487 snd_minor_info_oss_init() < 0 ||
488 snd_card_info_init() < 0)
493 snd_info_free_entry(snd_proc_root
);
497 int __exit
snd_info_done(void)
499 snd_info_free_entry(snd_proc_root
);
504 * create a card proc file
507 int snd_info_card_create(struct snd_card
*card
)
510 struct snd_info_entry
*entry
;
512 if (snd_BUG_ON(!card
))
515 sprintf(str
, "card%i", card
->number
);
516 entry
= create_subdir(card
->module
, str
);
519 card
->proc_root
= entry
;
524 * register the card proc file
527 int snd_info_card_register(struct snd_card
*card
)
529 struct proc_dir_entry
*p
;
531 if (snd_BUG_ON(!card
))
534 if (!strcmp(card
->id
, card
->proc_root
->name
))
537 p
= proc_symlink(card
->id
, snd_proc_root
->p
, card
->proc_root
->name
);
540 card
->proc_root_link
= p
;
545 * called on card->id change
547 void snd_info_card_id_change(struct snd_card
*card
)
549 mutex_lock(&info_mutex
);
550 if (card
->proc_root_link
) {
551 proc_remove(card
->proc_root_link
);
552 card
->proc_root_link
= NULL
;
554 if (strcmp(card
->id
, card
->proc_root
->name
))
555 card
->proc_root_link
= proc_symlink(card
->id
,
557 card
->proc_root
->name
);
558 mutex_unlock(&info_mutex
);
562 * de-register the card proc file
565 void snd_info_card_disconnect(struct snd_card
*card
)
569 mutex_lock(&info_mutex
);
570 proc_remove(card
->proc_root_link
);
571 card
->proc_root_link
= NULL
;
573 snd_info_disconnect(card
->proc_root
);
574 mutex_unlock(&info_mutex
);
578 * release the card proc file resources
581 int snd_info_card_free(struct snd_card
*card
)
585 snd_info_free_entry(card
->proc_root
);
586 card
->proc_root
= NULL
;
592 * snd_info_get_line - read one line from the procfs buffer
593 * @buffer: the procfs buffer
594 * @line: the buffer to store
595 * @len: the max. buffer size
597 * Reads one line from the buffer and stores the string.
599 * Return: Zero if successful, or 1 if error or EOF.
601 int snd_info_get_line(struct snd_info_buffer
*buffer
, char *line
, int len
)
605 if (snd_BUG_ON(!buffer
|| !buffer
->buffer
))
607 if (len
<= 0 || buffer
->stop
|| buffer
->error
)
609 while (!buffer
->stop
) {
610 c
= buffer
->buffer
[buffer
->curr
++];
611 if (buffer
->curr
>= buffer
->size
)
624 EXPORT_SYMBOL(snd_info_get_line
);
627 * snd_info_get_str - parse a string token
628 * @dest: the buffer to store the string token
629 * @src: the original string
630 * @len: the max. length of token - 1
632 * Parses the original string and copy a token to the given
635 * Return: The updated pointer of the original string so that
636 * it can be used for the next call.
638 const char *snd_info_get_str(char *dest
, const char *src
, int len
)
642 while (*src
== ' ' || *src
== '\t')
644 if (*src
== '"' || *src
== '\'') {
646 while (--len
> 0 && *src
&& *src
!= c
) {
652 while (--len
> 0 && *src
&& *src
!= ' ' && *src
!= '\t') {
657 while (*src
== ' ' || *src
== '\t')
662 EXPORT_SYMBOL(snd_info_get_str
);
665 * snd_info_create_entry - create an info entry
666 * @name: the proc file name
668 * Creates an info entry with the given file name and initializes as
671 * Usually called from other functions such as
672 * snd_info_create_card_entry().
674 * Return: The pointer of the new instance, or %NULL on failure.
676 static struct snd_info_entry
*snd_info_create_entry(const char *name
)
678 struct snd_info_entry
*entry
;
679 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
682 entry
->name
= kstrdup(name
, GFP_KERNEL
);
683 if (entry
->name
== NULL
) {
687 entry
->mode
= S_IFREG
| S_IRUGO
;
688 entry
->content
= SNDRV_INFO_CONTENT_TEXT
;
689 mutex_init(&entry
->access
);
690 INIT_LIST_HEAD(&entry
->children
);
691 INIT_LIST_HEAD(&entry
->list
);
696 * snd_info_create_module_entry - create an info entry for the given module
697 * @module: the module pointer
698 * @name: the file name
699 * @parent: the parent directory
701 * Creates a new info entry and assigns it to the given module.
703 * Return: The pointer of the new instance, or %NULL on failure.
705 struct snd_info_entry
*snd_info_create_module_entry(struct module
* module
,
707 struct snd_info_entry
*parent
)
709 struct snd_info_entry
*entry
= snd_info_create_entry(name
);
711 entry
->module
= module
;
712 entry
->parent
= parent
;
717 EXPORT_SYMBOL(snd_info_create_module_entry
);
720 * snd_info_create_card_entry - create an info entry for the given card
721 * @card: the card instance
722 * @name: the file name
723 * @parent: the parent directory
725 * Creates a new info entry and assigns it to the given card.
727 * Return: The pointer of the new instance, or %NULL on failure.
729 struct snd_info_entry
*snd_info_create_card_entry(struct snd_card
*card
,
731 struct snd_info_entry
* parent
)
733 struct snd_info_entry
*entry
= snd_info_create_entry(name
);
735 entry
->module
= card
->module
;
737 entry
->parent
= parent
;
742 EXPORT_SYMBOL(snd_info_create_card_entry
);
744 static void snd_info_disconnect(struct snd_info_entry
*entry
)
746 struct snd_info_entry
*p
, *n
;
750 list_for_each_entry_safe(p
, n
, &entry
->children
, list
)
751 snd_info_disconnect(p
);
752 list_del_init(&entry
->list
);
753 proc_remove(entry
->p
);
758 * snd_info_free_entry - release the info entry
759 * @entry: the info entry
761 * Releases the info entry.
763 void snd_info_free_entry(struct snd_info_entry
* entry
)
765 struct snd_info_entry
*p
, *n
;
770 mutex_lock(&info_mutex
);
771 snd_info_disconnect(entry
);
772 mutex_unlock(&info_mutex
);
775 /* free all children at first */
776 list_for_each_entry_safe(p
, n
, &entry
->children
, list
)
777 snd_info_free_entry(p
);
780 if (entry
->private_free
)
781 entry
->private_free(entry
);
785 EXPORT_SYMBOL(snd_info_free_entry
);
788 * snd_info_register - register the info entry
789 * @entry: the info entry
791 * Registers the proc info entry.
793 * Return: Zero if successful, or a negative error code on failure.
795 int snd_info_register(struct snd_info_entry
* entry
)
797 struct proc_dir_entry
*root
, *p
= NULL
;
799 if (snd_BUG_ON(!entry
))
801 root
= entry
->parent
== NULL
? snd_proc_root
->p
: entry
->parent
->p
;
802 mutex_lock(&info_mutex
);
803 if (S_ISDIR(entry
->mode
)) {
804 p
= proc_mkdir_mode(entry
->name
, entry
->mode
, root
);
806 mutex_unlock(&info_mutex
);
810 const struct file_operations
*ops
;
811 if (entry
->content
== SNDRV_INFO_CONTENT_DATA
)
812 ops
= &snd_info_entry_operations
;
814 ops
= &snd_info_text_entry_ops
;
815 p
= proc_create_data(entry
->name
, entry
->mode
, root
,
818 mutex_unlock(&info_mutex
);
821 proc_set_size(p
, entry
->size
);
825 list_add_tail(&entry
->list
, &entry
->parent
->children
);
826 mutex_unlock(&info_mutex
);
830 EXPORT_SYMBOL(snd_info_register
);
836 static void snd_info_version_read(struct snd_info_entry
*entry
, struct snd_info_buffer
*buffer
)
839 "Advanced Linux Sound Architecture Driver Version k%s.\n",
840 init_utsname()->release
);
843 static int __init
snd_info_version_init(void)
845 struct snd_info_entry
*entry
;
847 entry
= snd_info_create_module_entry(THIS_MODULE
, "version", NULL
);
850 entry
->c
.text
.read
= snd_info_version_read
;
851 return snd_info_register(entry
); /* freed in error path */
854 #endif /* CONFIG_PROC_FS */