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>
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 DECLARE_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);
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(struct snd_info_buffer
*buffer
, char *fmt
,...)
98 if (buffer
->stop
|| buffer
->error
)
100 len
= buffer
->len
- buffer
->size
;
102 res
= vsnprintf(buffer
->curr
, len
, fmt
, args
);
117 static struct proc_dir_entry
*snd_proc_root
= NULL
;
118 struct snd_info_entry
*snd_seq_root
= NULL
;
119 #ifdef CONFIG_SND_OSSEMUL
120 struct snd_info_entry
*snd_oss_root
= NULL
;
123 static inline void snd_info_entry_prepare(struct proc_dir_entry
*de
)
125 de
->owner
= THIS_MODULE
;
128 static void snd_remove_proc_entry(struct proc_dir_entry
*parent
,
129 struct proc_dir_entry
*de
)
132 remove_proc_entry(de
->name
, parent
);
135 static loff_t
snd_info_entry_llseek(struct file
*file
, loff_t offset
, int orig
)
137 struct snd_info_private_data
*data
;
138 struct snd_info_entry
*entry
;
141 data
= file
->private_data
;
144 switch (entry
->content
) {
145 case SNDRV_INFO_CONTENT_TEXT
:
147 case 0: /* SEEK_SET */
148 file
->f_pos
= offset
;
151 case 1: /* SEEK_CUR */
152 file
->f_pos
+= offset
;
155 case 2: /* SEEK_END */
161 case SNDRV_INFO_CONTENT_DATA
:
162 if (entry
->c
.ops
->llseek
) {
163 ret
= entry
->c
.ops
->llseek(entry
,
164 data
->file_private_data
,
176 static ssize_t
snd_info_entry_read(struct file
*file
, char __user
*buffer
,
177 size_t count
, loff_t
* offset
)
179 struct snd_info_private_data
*data
;
180 struct snd_info_entry
*entry
;
181 struct snd_info_buffer
*buf
;
185 data
= file
->private_data
;
186 snd_assert(data
!= NULL
, return -ENXIO
);
188 if (pos
< 0 || (long) pos
!= pos
|| (ssize_t
) count
< 0)
190 if ((unsigned long) pos
+ (unsigned long) count
< (unsigned long) pos
)
193 switch (entry
->content
) {
194 case SNDRV_INFO_CONTENT_TEXT
:
198 if (pos
>= buf
->size
)
200 size
= buf
->size
- pos
;
201 size
= min(count
, size
);
202 if (copy_to_user(buffer
, buf
->buffer
+ pos
, size
))
205 case SNDRV_INFO_CONTENT_DATA
:
206 if (entry
->c
.ops
->read
)
207 size
= entry
->c
.ops
->read(entry
,
208 data
->file_private_data
,
209 file
, buffer
, count
, pos
);
212 if ((ssize_t
) size
> 0)
213 *offset
= pos
+ size
;
217 static ssize_t
snd_info_entry_write(struct file
*file
, const char __user
*buffer
,
218 size_t count
, loff_t
* offset
)
220 struct snd_info_private_data
*data
;
221 struct snd_info_entry
*entry
;
222 struct snd_info_buffer
*buf
;
226 data
= file
->private_data
;
227 snd_assert(data
!= NULL
, return -ENXIO
);
230 if (pos
< 0 || (long) pos
!= pos
|| (ssize_t
) count
< 0)
232 if ((unsigned long) pos
+ (unsigned long) count
< (unsigned long) pos
)
234 switch (entry
->content
) {
235 case SNDRV_INFO_CONTENT_TEXT
:
241 size
= buf
->len
- pos
;
242 size
= min(count
, size
);
243 if (copy_from_user(buf
->buffer
+ pos
, buffer
, size
))
245 if ((long)buf
->size
< pos
+ size
)
246 buf
->size
= pos
+ size
;
248 case SNDRV_INFO_CONTENT_DATA
:
249 if (entry
->c
.ops
->write
)
250 size
= entry
->c
.ops
->write(entry
,
251 data
->file_private_data
,
252 file
, buffer
, count
, pos
);
255 if ((ssize_t
) size
> 0)
256 *offset
= pos
+ size
;
260 static int snd_info_entry_open(struct inode
*inode
, struct file
*file
)
262 struct snd_info_entry
*entry
;
263 struct snd_info_private_data
*data
;
264 struct snd_info_buffer
*buffer
;
265 struct proc_dir_entry
*p
;
270 entry
= p
== NULL
? NULL
: (struct snd_info_entry
*)p
->data
;
271 if (entry
== NULL
|| entry
->disconnected
) {
275 if (!try_module_get(entry
->module
)) {
279 mode
= file
->f_flags
& O_ACCMODE
;
280 if (mode
== O_RDONLY
|| mode
== O_RDWR
) {
281 if ((entry
->content
== SNDRV_INFO_CONTENT_TEXT
&&
282 !entry
->c
.text
.read_size
) ||
283 (entry
->content
== SNDRV_INFO_CONTENT_DATA
&&
284 entry
->c
.ops
->read
== NULL
)) {
289 if (mode
== O_WRONLY
|| mode
== O_RDWR
) {
290 if ((entry
->content
== SNDRV_INFO_CONTENT_TEXT
&&
291 !entry
->c
.text
.write_size
) ||
292 (entry
->content
== SNDRV_INFO_CONTENT_DATA
&&
293 entry
->c
.ops
->write
== NULL
)) {
298 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
304 switch (entry
->content
) {
305 case SNDRV_INFO_CONTENT_TEXT
:
306 if (mode
== O_RDONLY
|| mode
== O_RDWR
) {
307 buffer
= kzalloc(sizeof(*buffer
), GFP_KERNEL
);
308 if (buffer
== NULL
) {
313 buffer
->len
= (entry
->c
.text
.read_size
+
314 (PAGE_SIZE
- 1)) & ~(PAGE_SIZE
- 1);
315 buffer
->buffer
= vmalloc(buffer
->len
);
316 if (buffer
->buffer
== NULL
) {
322 buffer
->curr
= buffer
->buffer
;
323 data
->rbuffer
= buffer
;
325 if (mode
== O_WRONLY
|| mode
== O_RDWR
) {
326 buffer
= kzalloc(sizeof(*buffer
), GFP_KERNEL
);
327 if (buffer
== NULL
) {
328 if (mode
== O_RDWR
) {
329 vfree(data
->rbuffer
->buffer
);
330 kfree(data
->rbuffer
);
336 buffer
->len
= (entry
->c
.text
.write_size
+
337 (PAGE_SIZE
- 1)) & ~(PAGE_SIZE
- 1);
338 buffer
->buffer
= vmalloc(buffer
->len
);
339 if (buffer
->buffer
== NULL
) {
340 if (mode
== O_RDWR
) {
341 vfree(data
->rbuffer
->buffer
);
342 kfree(data
->rbuffer
);
349 buffer
->curr
= buffer
->buffer
;
350 data
->wbuffer
= buffer
;
353 case SNDRV_INFO_CONTENT_DATA
: /* data */
354 if (entry
->c
.ops
->open
) {
355 if ((err
= entry
->c
.ops
->open(entry
, mode
,
356 &data
->file_private_data
)) < 0) {
363 file
->private_data
= data
;
365 if (entry
->content
== SNDRV_INFO_CONTENT_TEXT
&&
366 (mode
== O_RDONLY
|| mode
== O_RDWR
)) {
367 if (entry
->c
.text
.read
) {
368 down(&entry
->access
);
369 entry
->c
.text
.read(entry
, data
->rbuffer
);
376 module_put(entry
->module
);
382 static int snd_info_entry_release(struct inode
*inode
, struct file
*file
)
384 struct snd_info_entry
*entry
;
385 struct snd_info_private_data
*data
;
388 mode
= file
->f_flags
& O_ACCMODE
;
389 data
= file
->private_data
;
391 switch (entry
->content
) {
392 case SNDRV_INFO_CONTENT_TEXT
:
393 if (mode
== O_RDONLY
|| mode
== O_RDWR
) {
394 vfree(data
->rbuffer
->buffer
);
395 kfree(data
->rbuffer
);
397 if (mode
== O_WRONLY
|| mode
== O_RDWR
) {
398 if (entry
->c
.text
.write
) {
399 entry
->c
.text
.write(entry
, data
->wbuffer
);
400 if (data
->wbuffer
->error
) {
401 snd_printk(KERN_WARNING
"data write error to %s (%i)\n",
403 data
->wbuffer
->error
);
406 vfree(data
->wbuffer
->buffer
);
407 kfree(data
->wbuffer
);
410 case SNDRV_INFO_CONTENT_DATA
:
411 if (entry
->c
.ops
->release
)
412 entry
->c
.ops
->release(entry
, mode
,
413 data
->file_private_data
);
416 module_put(entry
->module
);
421 static unsigned int snd_info_entry_poll(struct file
*file
, poll_table
* wait
)
423 struct snd_info_private_data
*data
;
424 struct snd_info_entry
*entry
;
427 data
= file
->private_data
;
432 switch (entry
->content
) {
433 case SNDRV_INFO_CONTENT_DATA
:
434 if (entry
->c
.ops
->poll
)
435 return entry
->c
.ops
->poll(entry
,
436 data
->file_private_data
,
438 if (entry
->c
.ops
->read
)
439 mask
|= POLLIN
| POLLRDNORM
;
440 if (entry
->c
.ops
->write
)
441 mask
|= POLLOUT
| POLLWRNORM
;
447 static long snd_info_entry_ioctl(struct file
*file
, unsigned int cmd
,
450 struct snd_info_private_data
*data
;
451 struct snd_info_entry
*entry
;
453 data
= file
->private_data
;
457 switch (entry
->content
) {
458 case SNDRV_INFO_CONTENT_DATA
:
459 if (entry
->c
.ops
->ioctl
)
460 return entry
->c
.ops
->ioctl(entry
,
461 data
->file_private_data
,
468 static int snd_info_entry_mmap(struct file
*file
, struct vm_area_struct
*vma
)
470 struct inode
*inode
= file
->f_dentry
->d_inode
;
471 struct snd_info_private_data
*data
;
472 struct snd_info_entry
*entry
;
474 data
= file
->private_data
;
478 switch (entry
->content
) {
479 case SNDRV_INFO_CONTENT_DATA
:
480 if (entry
->c
.ops
->mmap
)
481 return entry
->c
.ops
->mmap(entry
,
482 data
->file_private_data
,
489 static struct file_operations snd_info_entry_operations
=
491 .owner
= THIS_MODULE
,
492 .llseek
= snd_info_entry_llseek
,
493 .read
= snd_info_entry_read
,
494 .write
= snd_info_entry_write
,
495 .poll
= snd_info_entry_poll
,
496 .unlocked_ioctl
= snd_info_entry_ioctl
,
497 .mmap
= snd_info_entry_mmap
,
498 .open
= snd_info_entry_open
,
499 .release
= snd_info_entry_release
,
503 * snd_create_proc_entry - create a procfs entry
504 * @name: the name of the proc file
505 * @mode: the file permission bits, S_Ixxx
506 * @parent: the parent proc-directory entry
508 * Creates a new proc file entry with the given name and permission
509 * on the given directory.
511 * Returns the pointer of new instance or NULL on failure.
513 static struct proc_dir_entry
*snd_create_proc_entry(const char *name
, mode_t mode
,
514 struct proc_dir_entry
*parent
)
516 struct proc_dir_entry
*p
;
517 p
= create_proc_entry(name
, mode
, parent
);
519 snd_info_entry_prepare(p
);
523 int __init
snd_info_init(void)
525 struct proc_dir_entry
*p
;
527 p
= snd_create_proc_entry("asound", S_IFDIR
| S_IRUGO
| S_IXUGO
, &proc_root
);
531 #ifdef CONFIG_SND_OSSEMUL
533 struct snd_info_entry
*entry
;
534 if ((entry
= snd_info_create_module_entry(THIS_MODULE
, "oss", NULL
)) == NULL
)
536 entry
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
537 if (snd_info_register(entry
) < 0) {
538 snd_info_free_entry(entry
);
541 snd_oss_root
= entry
;
544 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
546 struct snd_info_entry
*entry
;
547 if ((entry
= snd_info_create_module_entry(THIS_MODULE
, "seq", NULL
)) == NULL
)
549 entry
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
550 if (snd_info_register(entry
) < 0) {
551 snd_info_free_entry(entry
);
554 snd_seq_root
= entry
;
557 snd_info_version_init();
558 snd_minor_info_init();
559 snd_minor_info_oss_init();
560 snd_card_info_init();
564 int __exit
snd_info_done(void)
566 snd_card_info_done();
567 snd_minor_info_oss_done();
568 snd_minor_info_done();
569 snd_info_version_done();
571 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
572 snd_info_unregister(snd_seq_root
);
574 #ifdef CONFIG_SND_OSSEMUL
575 snd_info_unregister(snd_oss_root
);
577 snd_remove_proc_entry(&proc_root
, snd_proc_root
);
588 * create a card proc file
591 int snd_info_card_create(struct snd_card
*card
)
594 struct snd_info_entry
*entry
;
596 snd_assert(card
!= NULL
, return -ENXIO
);
598 sprintf(str
, "card%i", card
->number
);
599 if ((entry
= snd_info_create_module_entry(card
->module
, str
, NULL
)) == NULL
)
601 entry
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
602 if (snd_info_register(entry
) < 0) {
603 snd_info_free_entry(entry
);
606 card
->proc_root
= entry
;
611 * register the card proc file
614 int snd_info_card_register(struct snd_card
*card
)
616 struct proc_dir_entry
*p
;
618 snd_assert(card
!= NULL
, return -ENXIO
);
620 if (!strcmp(card
->id
, card
->proc_root
->name
))
623 p
= proc_symlink(card
->id
, snd_proc_root
, card
->proc_root
->name
);
626 card
->proc_root_link
= p
;
631 * de-register the card proc file
634 int snd_info_card_free(struct snd_card
*card
)
636 snd_assert(card
!= NULL
, return -ENXIO
);
637 if (card
->proc_root_link
) {
638 snd_remove_proc_entry(snd_proc_root
, card
->proc_root_link
);
639 card
->proc_root_link
= NULL
;
641 if (card
->proc_root
) {
642 snd_info_unregister(card
->proc_root
);
643 card
->proc_root
= NULL
;
650 * snd_info_get_line - read one line from the procfs buffer
651 * @buffer: the procfs buffer
652 * @line: the buffer to store
653 * @len: the max. buffer size - 1
655 * Reads one line from the buffer and stores the string.
657 * Returns zero if successful, or 1 if error or EOF.
659 int snd_info_get_line(struct snd_info_buffer
*buffer
, char *line
, int len
)
663 if (len
<= 0 || buffer
->stop
|| buffer
->error
)
668 if ((buffer
->curr
- buffer
->buffer
) >= (long)buffer
->size
) {
674 if ((buffer
->curr
- buffer
->buffer
) >= (long)buffer
->size
) {
679 while (c
!= '\n' && !buffer
->stop
) {
681 if ((buffer
->curr
- buffer
->buffer
) >= (long)buffer
->size
) {
690 * snd_info_get_str - parse a string token
691 * @dest: the buffer to store the string token
692 * @src: the original string
693 * @len: the max. length of token - 1
695 * Parses the original string and copy a token to the given
698 * Returns the updated pointer of the original string so that
699 * it can be used for the next call.
701 char *snd_info_get_str(char *dest
, char *src
, int len
)
705 while (*src
== ' ' || *src
== '\t')
707 if (*src
== '"' || *src
== '\'') {
709 while (--len
> 0 && *src
&& *src
!= c
) {
715 while (--len
> 0 && *src
&& *src
!= ' ' && *src
!= '\t') {
720 while (*src
== ' ' || *src
== '\t')
726 * snd_info_create_entry - create an info entry
727 * @name: the proc file name
729 * Creates an info entry with the given file name and initializes as
732 * Usually called from other functions such as
733 * snd_info_create_card_entry().
735 * Returns the pointer of the new instance, or NULL on failure.
737 static struct snd_info_entry
*snd_info_create_entry(const char *name
)
739 struct snd_info_entry
*entry
;
740 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
743 entry
->name
= kstrdup(name
, GFP_KERNEL
);
744 if (entry
->name
== NULL
) {
748 entry
->mode
= S_IFREG
| S_IRUGO
;
749 entry
->content
= SNDRV_INFO_CONTENT_TEXT
;
750 init_MUTEX(&entry
->access
);
755 * snd_info_create_module_entry - create an info entry for the given module
756 * @module: the module pointer
757 * @name: the file name
758 * @parent: the parent directory
760 * Creates a new info entry and assigns it to the given module.
762 * Returns the pointer of the new instance, or NULL on failure.
764 struct snd_info_entry
*snd_info_create_module_entry(struct module
* module
,
766 struct snd_info_entry
*parent
)
768 struct snd_info_entry
*entry
= snd_info_create_entry(name
);
770 entry
->module
= module
;
771 entry
->parent
= parent
;
777 * snd_info_create_card_entry - create an info entry for the given card
778 * @card: the card instance
779 * @name: the file name
780 * @parent: the parent directory
782 * Creates a new info entry and assigns it to the given card.
784 * Returns the pointer of the new instance, or NULL on failure.
786 struct snd_info_entry
*snd_info_create_card_entry(struct snd_card
*card
,
788 struct snd_info_entry
* parent
)
790 struct snd_info_entry
*entry
= snd_info_create_entry(name
);
792 entry
->module
= card
->module
;
794 entry
->parent
= parent
;
799 static int snd_info_dev_free_entry(struct snd_device
*device
)
801 struct snd_info_entry
*entry
= device
->device_data
;
802 snd_info_free_entry(entry
);
806 static int snd_info_dev_register_entry(struct snd_device
*device
)
808 struct snd_info_entry
*entry
= device
->device_data
;
809 return snd_info_register(entry
);
812 static int snd_info_dev_disconnect_entry(struct snd_device
*device
)
814 struct snd_info_entry
*entry
= device
->device_data
;
815 entry
->disconnected
= 1;
819 static int snd_info_dev_unregister_entry(struct snd_device
*device
)
821 struct snd_info_entry
*entry
= device
->device_data
;
822 return snd_info_unregister(entry
);
826 * snd_card_proc_new - create an info entry for the given card
827 * @card: the card instance
828 * @name: the file name
829 * @entryp: the pointer to store the new info entry
831 * Creates a new info entry and assigns it to the given card.
832 * Unlike snd_info_create_card_entry(), this function registers the
833 * info entry as an ALSA device component, so that it can be
834 * unregistered/released without explicit call.
835 * Also, you don't have to register this entry via snd_info_register(),
836 * since this will be registered by snd_card_register() automatically.
838 * The parent is assumed as card->proc_root.
840 * For releasing this entry, use snd_device_free() instead of
841 * snd_info_free_entry().
843 * Returns zero if successful, or a negative error code on failure.
845 int snd_card_proc_new(struct snd_card
*card
, const char *name
,
846 struct snd_info_entry
**entryp
)
848 static struct snd_device_ops ops
= {
849 .dev_free
= snd_info_dev_free_entry
,
850 .dev_register
= snd_info_dev_register_entry
,
851 .dev_disconnect
= snd_info_dev_disconnect_entry
,
852 .dev_unregister
= snd_info_dev_unregister_entry
854 struct snd_info_entry
*entry
;
857 entry
= snd_info_create_card_entry(card
, name
, card
->proc_root
);
860 if ((err
= snd_device_new(card
, SNDRV_DEV_INFO
, entry
, &ops
)) < 0) {
861 snd_info_free_entry(entry
);
870 * snd_info_free_entry - release the info entry
871 * @entry: the info entry
873 * Releases the info entry. Don't call this after registered.
875 void snd_info_free_entry(struct snd_info_entry
* entry
)
880 if (entry
->private_free
)
881 entry
->private_free(entry
);
886 * snd_info_register - register the info entry
887 * @entry: the info entry
889 * Registers the proc info entry.
891 * Returns zero if successful, or a negative error code on failure.
893 int snd_info_register(struct snd_info_entry
* entry
)
895 struct proc_dir_entry
*root
, *p
= NULL
;
897 snd_assert(entry
!= NULL
, return -ENXIO
);
898 root
= entry
->parent
== NULL
? snd_proc_root
: entry
->parent
->p
;
900 p
= snd_create_proc_entry(entry
->name
, entry
->mode
, root
);
905 p
->owner
= entry
->module
;
906 if (!S_ISDIR(entry
->mode
))
907 p
->proc_fops
= &snd_info_entry_operations
;
908 p
->size
= entry
->size
;
916 * snd_info_unregister - de-register the info entry
917 * @entry: the info entry
919 * De-registers the info entry and releases the instance.
921 * Returns zero if successful, or a negative error code on failure.
923 int snd_info_unregister(struct snd_info_entry
* entry
)
925 struct proc_dir_entry
*root
;
929 snd_assert(entry
->p
!= NULL
, return -ENXIO
);
930 root
= entry
->parent
== NULL
? snd_proc_root
: entry
->parent
->p
;
931 snd_assert(root
, return -ENXIO
);
933 snd_remove_proc_entry(root
, entry
->p
);
935 snd_info_free_entry(entry
);
943 static struct snd_info_entry
*snd_info_version_entry
= NULL
;
945 static void snd_info_version_read(struct snd_info_entry
*entry
, struct snd_info_buffer
*buffer
)
948 "Advanced Linux Sound Architecture Driver Version "
949 CONFIG_SND_VERSION CONFIG_SND_DATE
".\n"
953 static int __init
snd_info_version_init(void)
955 struct snd_info_entry
*entry
;
957 entry
= snd_info_create_module_entry(THIS_MODULE
, "version", NULL
);
960 entry
->c
.text
.read_size
= 256;
961 entry
->c
.text
.read
= snd_info_version_read
;
962 if (snd_info_register(entry
) < 0) {
963 snd_info_free_entry(entry
);
966 snd_info_version_entry
= entry
;
970 static int __exit
snd_info_version_done(void)
972 if (snd_info_version_entry
)
973 snd_info_unregister(snd_info_version_entry
);
977 #endif /* CONFIG_PROC_FS */