Merge nommu tree
[pandora-kernel.git] / sound / core / info.c
1 /*
2  *  Information interface for ALSA driver
3  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4  *
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
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>
35 #include <stdarg.h>
36
37 /*
38  *
39  */
40
41 #ifdef CONFIG_PROC_FS
42
43 int snd_info_check_reserved_words(const char *str)
44 {
45         static char *reserved[] =
46         {
47                 "version",
48                 "meminfo",
49                 "memdebug",
50                 "detect",
51                 "devices",
52                 "oss",
53                 "cards",
54                 "timers",
55                 "synth",
56                 "pcm",
57                 "seq",
58                 NULL
59         };
60         char **xstr = reserved;
61
62         while (*xstr) {
63                 if (!strcmp(*xstr, str))
64                         return 0;
65                 xstr++;
66         }
67         if (!strncmp(str, "card", 4))
68                 return 0;
69         return 1;
70 }
71
72 static DEFINE_MUTEX(info_mutex);
73
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;
79 };
80
81 static int snd_info_version_init(void);
82 static int snd_info_version_done(void);
83
84
85 /**
86  * snd_iprintf - printf on the procfs buffer
87  * @buffer: the procfs buffer
88  * @fmt: the printf format
89  *
90  * Outputs the string on the procfs buffer just like printf().
91  *
92  * Returns the size of output string.
93  */
94 int snd_iprintf(struct snd_info_buffer *buffer, char *fmt,...)
95 {
96         va_list args;
97         int len, res;
98
99         if (buffer->stop || buffer->error)
100                 return 0;
101         len = buffer->len - buffer->size;
102         va_start(args, fmt);
103         res = vsnprintf(buffer->curr, len, fmt, args);
104         va_end(args);
105         if (res >= len) {
106                 buffer->stop = 1;
107                 return 0;
108         }
109         buffer->curr += res;
110         buffer->size += res;
111         return res;
112 }
113
114 /*
115
116  */
117
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;
122 #endif
123
124 static inline void snd_info_entry_prepare(struct proc_dir_entry *de)
125 {
126         de->owner = THIS_MODULE;
127 }
128
129 static void snd_remove_proc_entry(struct proc_dir_entry *parent,
130                                   struct proc_dir_entry *de)
131 {
132         if (de)
133                 remove_proc_entry(de->name, parent);
134 }
135
136 static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
137 {
138         struct snd_info_private_data *data;
139         struct snd_info_entry *entry;
140         loff_t ret;
141
142         data = file->private_data;
143         entry = data->entry;
144         lock_kernel();
145         switch (entry->content) {
146         case SNDRV_INFO_CONTENT_TEXT:
147                 switch (orig) {
148                 case 0: /* SEEK_SET */
149                         file->f_pos = offset;
150                         ret = file->f_pos;
151                         goto out;
152                 case 1: /* SEEK_CUR */
153                         file->f_pos += offset;
154                         ret = file->f_pos;
155                         goto out;
156                 case 2: /* SEEK_END */
157                 default:
158                         ret = -EINVAL;
159                         goto out;
160                 }
161                 break;
162         case SNDRV_INFO_CONTENT_DATA:
163                 if (entry->c.ops->llseek) {
164                         ret = entry->c.ops->llseek(entry,
165                                                     data->file_private_data,
166                                                     file, offset, orig);
167                         goto out;
168                 }
169                 break;
170         }
171         ret = -ENXIO;
172 out:
173         unlock_kernel();
174         return ret;
175 }
176
177 static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
178                                    size_t count, loff_t * offset)
179 {
180         struct snd_info_private_data *data;
181         struct snd_info_entry *entry;
182         struct snd_info_buffer *buf;
183         size_t size = 0;
184         loff_t pos;
185
186         data = file->private_data;
187         snd_assert(data != NULL, return -ENXIO);
188         pos = *offset;
189         if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
190                 return -EIO;
191         if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
192                 return -EIO;
193         entry = data->entry;
194         switch (entry->content) {
195         case SNDRV_INFO_CONTENT_TEXT:
196                 buf = data->rbuffer;
197                 if (buf == NULL)
198                         return -EIO;
199                 if (pos >= buf->size)
200                         return 0;
201                 size = buf->size - pos;
202                 size = min(count, size);
203                 if (copy_to_user(buffer, buf->buffer + pos, size))
204                         return -EFAULT;
205                 break;
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);
211                 break;
212         }
213         if ((ssize_t) size > 0)
214                 *offset = pos + size;
215         return size;
216 }
217
218 static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
219                                     size_t count, loff_t * offset)
220 {
221         struct snd_info_private_data *data;
222         struct snd_info_entry *entry;
223         struct snd_info_buffer *buf;
224         size_t size = 0;
225         loff_t pos;
226
227         data = file->private_data;
228         snd_assert(data != NULL, return -ENXIO);
229         entry = data->entry;
230         pos = *offset;
231         if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
232                 return -EIO;
233         if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
234                 return -EIO;
235         switch (entry->content) {
236         case SNDRV_INFO_CONTENT_TEXT:
237                 buf = data->wbuffer;
238                 if (buf == NULL)
239                         return -EIO;
240                 if (pos >= buf->len)
241                         return -ENOMEM;
242                 size = buf->len - pos;
243                 size = min(count, size);
244                 if (copy_from_user(buf->buffer + pos, buffer, size))
245                         return -EFAULT;
246                 if ((long)buf->size < pos + size)
247                         buf->size = pos + size;
248                 break;
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);
254                 break;
255         }
256         if ((ssize_t) size > 0)
257                 *offset = pos + size;
258         return size;
259 }
260
261 static int snd_info_entry_open(struct inode *inode, struct file *file)
262 {
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;
267         int mode, err;
268
269         mutex_lock(&info_mutex);
270         p = PDE(inode);
271         entry = p == NULL ? NULL : (struct snd_info_entry *)p->data;
272         if (entry == NULL || entry->disconnected) {
273                 mutex_unlock(&info_mutex);
274                 return -ENODEV;
275         }
276         if (!try_module_get(entry->module)) {
277                 err = -EFAULT;
278                 goto __error1;
279         }
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)) {
286                         err = -ENODEV;
287                         goto __error;
288                 }
289         }
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)) {
295                         err = -ENODEV;
296                         goto __error;
297                 }
298         }
299         data = kzalloc(sizeof(*data), GFP_KERNEL);
300         if (data == NULL) {
301                 err = -ENOMEM;
302                 goto __error;
303         }
304         data->entry = entry;
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) {
310                                 kfree(data);
311                                 err = -ENOMEM;
312                                 goto __error;
313                         }
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) {
318                                 kfree(buffer);
319                                 kfree(data);
320                                 err = -ENOMEM;
321                                 goto __error;
322                         }
323                         buffer->curr = buffer->buffer;
324                         data->rbuffer = buffer;
325                 }
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);
332                                 }
333                                 kfree(data);
334                                 err = -ENOMEM;
335                                 goto __error;
336                         }
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);
344                                 }
345                                 kfree(buffer);
346                                 kfree(data);
347                                 err = -ENOMEM;
348                                 goto __error;
349                         }
350                         buffer->curr = buffer->buffer;
351                         data->wbuffer = buffer;
352                 }
353                 break;
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) {
358                                 kfree(data);
359                                 goto __error;
360                         }
361                 }
362                 break;
363         }
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);
372                 }
373         }
374         return 0;
375
376       __error:
377         module_put(entry->module);
378       __error1:
379         mutex_unlock(&info_mutex);
380         return err;
381 }
382
383 static int snd_info_entry_release(struct inode *inode, struct file *file)
384 {
385         struct snd_info_entry *entry;
386         struct snd_info_private_data *data;
387         int mode;
388
389         mode = file->f_flags & O_ACCMODE;
390         data = file->private_data;
391         entry = data->entry;
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);
397                 }
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",
403                                                 entry->name,
404                                                 data->wbuffer->error);
405                                 }
406                         }
407                         vfree(data->wbuffer->buffer);
408                         kfree(data->wbuffer);
409                 }
410                 break;
411         case SNDRV_INFO_CONTENT_DATA:
412                 if (entry->c.ops->release)
413                         entry->c.ops->release(entry, mode,
414                                               data->file_private_data);
415                 break;
416         }
417         module_put(entry->module);
418         kfree(data);
419         return 0;
420 }
421
422 static unsigned int snd_info_entry_poll(struct file *file, poll_table * wait)
423 {
424         struct snd_info_private_data *data;
425         struct snd_info_entry *entry;
426         unsigned int mask;
427
428         data = file->private_data;
429         if (data == NULL)
430                 return 0;
431         entry = data->entry;
432         mask = 0;
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,
438                                                   file, wait);
439                 if (entry->c.ops->read)
440                         mask |= POLLIN | POLLRDNORM;
441                 if (entry->c.ops->write)
442                         mask |= POLLOUT | POLLWRNORM;
443                 break;
444         }
445         return mask;
446 }
447
448 static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
449                                 unsigned long arg)
450 {
451         struct snd_info_private_data *data;
452         struct snd_info_entry *entry;
453
454         data = file->private_data;
455         if (data == NULL)
456                 return 0;
457         entry = data->entry;
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,
463                                                    file, cmd, arg);
464                 break;
465         }
466         return -ENOTTY;
467 }
468
469 static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
470 {
471         struct inode *inode = file->f_dentry->d_inode;
472         struct snd_info_private_data *data;
473         struct snd_info_entry *entry;
474
475         data = file->private_data;
476         if (data == NULL)
477                 return 0;
478         entry = data->entry;
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,
484                                                   inode, file, vma);
485                 break;
486         }
487         return -ENXIO;
488 }
489
490 static struct file_operations snd_info_entry_operations =
491 {
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,
501 };
502
503 /**
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
508  *
509  * Creates a new proc file entry with the given name and permission
510  * on the given directory.
511  *
512  * Returns the pointer of new instance or NULL on failure.
513  */
514 static struct proc_dir_entry *snd_create_proc_entry(const char *name, mode_t mode,
515                                                     struct proc_dir_entry *parent)
516 {
517         struct proc_dir_entry *p;
518         p = create_proc_entry(name, mode, parent);
519         if (p)
520                 snd_info_entry_prepare(p);
521         return p;
522 }
523
524 int __init snd_info_init(void)
525 {
526         struct proc_dir_entry *p;
527
528         p = snd_create_proc_entry("asound", S_IFDIR | S_IRUGO | S_IXUGO, &proc_root);
529         if (p == NULL)
530                 return -ENOMEM;
531         snd_proc_root = p;
532 #ifdef CONFIG_SND_OSSEMUL
533         {
534                 struct snd_info_entry *entry;
535                 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
536                         return -ENOMEM;
537                 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
538                 if (snd_info_register(entry) < 0) {
539                         snd_info_free_entry(entry);
540                         return -ENOMEM;
541                 }
542                 snd_oss_root = entry;
543         }
544 #endif
545 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
546         {
547                 struct snd_info_entry *entry;
548                 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL)
549                         return -ENOMEM;
550                 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
551                 if (snd_info_register(entry) < 0) {
552                         snd_info_free_entry(entry);
553                         return -ENOMEM;
554                 }
555                 snd_seq_root = entry;
556         }
557 #endif
558         snd_info_version_init();
559         snd_minor_info_init();
560         snd_minor_info_oss_init();
561         snd_card_info_init();
562         return 0;
563 }
564
565 int __exit snd_info_done(void)
566 {
567         snd_card_info_done();
568         snd_minor_info_oss_done();
569         snd_minor_info_done();
570         snd_info_version_done();
571         if (snd_proc_root) {
572 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
573                 snd_info_unregister(snd_seq_root);
574 #endif
575 #ifdef CONFIG_SND_OSSEMUL
576                 snd_info_unregister(snd_oss_root);
577 #endif
578                 snd_remove_proc_entry(&proc_root, snd_proc_root);
579         }
580         return 0;
581 }
582
583 /*
584
585  */
586
587
588 /*
589  * create a card proc file
590  * called from init.c
591  */
592 int snd_info_card_create(struct snd_card *card)
593 {
594         char str[8];
595         struct snd_info_entry *entry;
596
597         snd_assert(card != NULL, return -ENXIO);
598
599         sprintf(str, "card%i", card->number);
600         if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
601                 return -ENOMEM;
602         entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
603         if (snd_info_register(entry) < 0) {
604                 snd_info_free_entry(entry);
605                 return -ENOMEM;
606         }
607         card->proc_root = entry;
608         return 0;
609 }
610
611 /*
612  * register the card proc file
613  * called from init.c
614  */
615 int snd_info_card_register(struct snd_card *card)
616 {
617         struct proc_dir_entry *p;
618
619         snd_assert(card != NULL, return -ENXIO);
620
621         if (!strcmp(card->id, card->proc_root->name))
622                 return 0;
623
624         p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
625         if (p == NULL)
626                 return -ENOMEM;
627         card->proc_root_link = p;
628         return 0;
629 }
630
631 /*
632  * de-register the card proc file
633  * called from init.c
634  */
635 int snd_info_card_free(struct snd_card *card)
636 {
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;
641         }
642         if (card->proc_root) {
643                 snd_info_unregister(card->proc_root);
644                 card->proc_root = NULL;
645         }
646         return 0;
647 }
648
649
650 /**
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
655  *
656  * Reads one line from the buffer and stores the string.
657  *
658  * Returns zero if successful, or 1 if error or EOF.
659  */
660 int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
661 {
662         int c = -1;
663
664         if (len <= 0 || buffer->stop || buffer->error)
665                 return 1;
666         while (--len > 0) {
667                 c = *buffer->curr++;
668                 if (c == '\n') {
669                         if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
670                                 buffer->stop = 1;
671                         }
672                         break;
673                 }
674                 *line++ = c;
675                 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
676                         buffer->stop = 1;
677                         break;
678                 }
679         }
680         while (c != '\n' && !buffer->stop) {
681                 c = *buffer->curr++;
682                 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
683                         buffer->stop = 1;
684                 }
685         }
686         *line = '\0';
687         return 0;
688 }
689
690 /**
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
695  *
696  * Parses the original string and copy a token to the given
697  * string buffer.
698  *
699  * Returns the updated pointer of the original string so that
700  * it can be used for the next call.
701  */
702 char *snd_info_get_str(char *dest, char *src, int len)
703 {
704         int c;
705
706         while (*src == ' ' || *src == '\t')
707                 src++;
708         if (*src == '"' || *src == '\'') {
709                 c = *src++;
710                 while (--len > 0 && *src && *src != c) {
711                         *dest++ = *src++;
712                 }
713                 if (*src == c)
714                         src++;
715         } else {
716                 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
717                         *dest++ = *src++;
718                 }
719         }
720         *dest = 0;
721         while (*src == ' ' || *src == '\t')
722                 src++;
723         return src;
724 }
725
726 /**
727  * snd_info_create_entry - create an info entry
728  * @name: the proc file name
729  *
730  * Creates an info entry with the given file name and initializes as
731  * the default state.
732  *
733  * Usually called from other functions such as
734  * snd_info_create_card_entry().
735  *
736  * Returns the pointer of the new instance, or NULL on failure.
737  */
738 static struct snd_info_entry *snd_info_create_entry(const char *name)
739 {
740         struct snd_info_entry *entry;
741         entry = kzalloc(sizeof(*entry), GFP_KERNEL);
742         if (entry == NULL)
743                 return NULL;
744         entry->name = kstrdup(name, GFP_KERNEL);
745         if (entry->name == NULL) {
746                 kfree(entry);
747                 return NULL;
748         }
749         entry->mode = S_IFREG | S_IRUGO;
750         entry->content = SNDRV_INFO_CONTENT_TEXT;
751         mutex_init(&entry->access);
752         return entry;
753 }
754
755 /**
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
760  *
761  * Creates a new info entry and assigns it to the given module.
762  *
763  * Returns the pointer of the new instance, or NULL on failure.
764  */
765 struct snd_info_entry *snd_info_create_module_entry(struct module * module,
766                                                const char *name,
767                                                struct snd_info_entry *parent)
768 {
769         struct snd_info_entry *entry = snd_info_create_entry(name);
770         if (entry) {
771                 entry->module = module;
772                 entry->parent = parent;
773         }
774         return entry;
775 }
776
777 /**
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
782  *
783  * Creates a new info entry and assigns it to the given card.
784  *
785  * Returns the pointer of the new instance, or NULL on failure.
786  */
787 struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
788                                              const char *name,
789                                              struct snd_info_entry * parent)
790 {
791         struct snd_info_entry *entry = snd_info_create_entry(name);
792         if (entry) {
793                 entry->module = card->module;
794                 entry->card = card;
795                 entry->parent = parent;
796         }
797         return entry;
798 }
799
800 static int snd_info_dev_free_entry(struct snd_device *device)
801 {
802         struct snd_info_entry *entry = device->device_data;
803         snd_info_free_entry(entry);
804         return 0;
805 }
806
807 static int snd_info_dev_register_entry(struct snd_device *device)
808 {
809         struct snd_info_entry *entry = device->device_data;
810         return snd_info_register(entry);
811 }
812
813 static int snd_info_dev_disconnect_entry(struct snd_device *device)
814 {
815         struct snd_info_entry *entry = device->device_data;
816         entry->disconnected = 1;
817         return 0;
818 }
819
820 static int snd_info_dev_unregister_entry(struct snd_device *device)
821 {
822         struct snd_info_entry *entry = device->device_data;
823         return snd_info_unregister(entry);
824 }
825
826 /**
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
831  *
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.
838  *
839  * The parent is assumed as card->proc_root.
840  *
841  * For releasing this entry, use snd_device_free() instead of
842  * snd_info_free_entry(). 
843  *
844  * Returns zero if successful, or a negative error code on failure.
845  */
846 int snd_card_proc_new(struct snd_card *card, const char *name,
847                       struct snd_info_entry **entryp)
848 {
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
854         };
855         struct snd_info_entry *entry;
856         int err;
857
858         entry = snd_info_create_card_entry(card, name, card->proc_root);
859         if (! entry)
860                 return -ENOMEM;
861         if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
862                 snd_info_free_entry(entry);
863                 return err;
864         }
865         if (entryp)
866                 *entryp = entry;
867         return 0;
868 }
869
870 /**
871  * snd_info_free_entry - release the info entry
872  * @entry: the info entry
873  *
874  * Releases the info entry.  Don't call this after registered.
875  */
876 void snd_info_free_entry(struct snd_info_entry * entry)
877 {
878         if (entry == NULL)
879                 return;
880         kfree(entry->name);
881         if (entry->private_free)
882                 entry->private_free(entry);
883         kfree(entry);
884 }
885
886 /**
887  * snd_info_register - register the info entry
888  * @entry: the info entry
889  *
890  * Registers the proc info entry.
891  *
892  * Returns zero if successful, or a negative error code on failure.
893  */
894 int snd_info_register(struct snd_info_entry * entry)
895 {
896         struct proc_dir_entry *root, *p = NULL;
897
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);
902         if (!p) {
903                 mutex_unlock(&info_mutex);
904                 return -ENOMEM;
905         }
906         p->owner = entry->module;
907         if (!S_ISDIR(entry->mode))
908                 p->proc_fops = &snd_info_entry_operations;
909         p->size = entry->size;
910         p->data = entry;
911         entry->p = p;
912         mutex_unlock(&info_mutex);
913         return 0;
914 }
915
916 /**
917  * snd_info_unregister - de-register the info entry
918  * @entry: the info entry
919  *
920  * De-registers the info entry and releases the instance.
921  *
922  * Returns zero if successful, or a negative error code on failure.
923  */
924 int snd_info_unregister(struct snd_info_entry * entry)
925 {
926         struct proc_dir_entry *root;
927
928         if (! entry)
929                 return 0;
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);
937         return 0;
938 }
939
940 /*
941
942  */
943
944 static struct snd_info_entry *snd_info_version_entry = NULL;
945
946 static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
947 {
948         snd_iprintf(buffer,
949                     "Advanced Linux Sound Architecture Driver Version "
950                     CONFIG_SND_VERSION CONFIG_SND_DATE ".\n"
951                    );
952 }
953
954 static int __init snd_info_version_init(void)
955 {
956         struct snd_info_entry *entry;
957
958         entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
959         if (entry == NULL)
960                 return -ENOMEM;
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);
965                 return -ENOMEM;
966         }
967         snd_info_version_entry = entry;
968         return 0;
969 }
970
971 static int __exit snd_info_version_done(void)
972 {
973         if (snd_info_version_entry)
974                 snd_info_unregister(snd_info_version_entry);
975         return 0;
976 }
977
978 #endif /* CONFIG_PROC_FS */