Merge branches 'release' and 'ppc-workaround' into release
[linux-2.6] / sound / core / info.c
1 /*
2  *  Information interface for ALSA driver
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/init.h>
23 #include <linux/time.h>
24 #include <linux/smp_lock.h>
25 #include <linux/string.h>
26 #include <sound/core.h>
27 #include <sound/minors.h>
28 #include <sound/info.h>
29 #include <sound/version.h>
30 #include <linux/proc_fs.h>
31 #include <linux/mutex.h>
32 #include <stdarg.h>
33
34 /*
35  *
36  */
37
38 #ifdef CONFIG_PROC_FS
39
40 int snd_info_check_reserved_words(const char *str)
41 {
42         static char *reserved[] =
43         {
44                 "version",
45                 "meminfo",
46                 "memdebug",
47                 "detect",
48                 "devices",
49                 "oss",
50                 "cards",
51                 "timers",
52                 "synth",
53                 "pcm",
54                 "seq",
55                 NULL
56         };
57         char **xstr = reserved;
58
59         while (*xstr) {
60                 if (!strcmp(*xstr, str))
61                         return 0;
62                 xstr++;
63         }
64         if (!strncmp(str, "card", 4))
65                 return 0;
66         return 1;
67 }
68
69 static DEFINE_MUTEX(info_mutex);
70
71 struct snd_info_private_data {
72         struct snd_info_buffer *rbuffer;
73         struct snd_info_buffer *wbuffer;
74         struct snd_info_entry *entry;
75         void *file_private_data;
76 };
77
78 static int snd_info_version_init(void);
79 static int snd_info_version_done(void);
80 static void snd_info_disconnect(struct snd_info_entry *entry);
81
82
83 /* resize the proc r/w buffer */
84 static int resize_info_buffer(struct snd_info_buffer *buffer,
85                               unsigned int nsize)
86 {
87         char *nbuf;
88
89         nsize = PAGE_ALIGN(nsize);
90         nbuf = kmalloc(nsize, GFP_KERNEL);
91         if (! nbuf)
92                 return -ENOMEM;
93
94         memcpy(nbuf, buffer->buffer, buffer->len);
95         kfree(buffer->buffer);
96         buffer->buffer = nbuf;
97         buffer->len = nsize;
98         return 0;
99 }
100
101 /**
102  * snd_iprintf - printf on the procfs buffer
103  * @buffer: the procfs buffer
104  * @fmt: the printf format
105  *
106  * Outputs the string on the procfs buffer just like printf().
107  *
108  * Returns the size of output string.
109  */
110 int snd_iprintf(struct snd_info_buffer *buffer, char *fmt,...)
111 {
112         va_list args;
113         int len, res;
114         int err = 0;
115
116         might_sleep();
117         if (buffer->stop || buffer->error)
118                 return 0;
119         len = buffer->len - buffer->size;
120         va_start(args, fmt);
121         for (;;) {
122                 va_list ap;
123                 va_copy(ap, args);
124                 res = vsnprintf(buffer->buffer + buffer->curr, len, fmt, ap);
125                 va_end(ap);
126                 if (res < len)
127                         break;
128                 err = resize_info_buffer(buffer, buffer->len + PAGE_SIZE);
129                 if (err < 0)
130                         break;
131                 len = buffer->len - buffer->size;
132         }
133         va_end(args);
134
135         if (err < 0)
136                 return err;
137         buffer->curr += res;
138         buffer->size += res;
139         return res;
140 }
141
142 EXPORT_SYMBOL(snd_iprintf);
143
144 /*
145
146  */
147
148 static struct proc_dir_entry *snd_proc_root;
149 struct snd_info_entry *snd_seq_root;
150 EXPORT_SYMBOL(snd_seq_root);
151
152 #ifdef CONFIG_SND_OSSEMUL
153 struct snd_info_entry *snd_oss_root;
154 #endif
155
156 static inline void snd_info_entry_prepare(struct proc_dir_entry *de)
157 {
158         de->owner = THIS_MODULE;
159 }
160
161 static void snd_remove_proc_entry(struct proc_dir_entry *parent,
162                                   struct proc_dir_entry *de)
163 {
164         if (de)
165                 remove_proc_entry(de->name, parent);
166 }
167
168 static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
169 {
170         struct snd_info_private_data *data;
171         struct snd_info_entry *entry;
172         loff_t ret;
173
174         data = file->private_data;
175         entry = data->entry;
176         lock_kernel();
177         switch (entry->content) {
178         case SNDRV_INFO_CONTENT_TEXT:
179                 switch (orig) {
180                 case SEEK_SET:
181                         file->f_pos = offset;
182                         ret = file->f_pos;
183                         goto out;
184                 case SEEK_CUR:
185                         file->f_pos += offset;
186                         ret = file->f_pos;
187                         goto out;
188                 case SEEK_END:
189                 default:
190                         ret = -EINVAL;
191                         goto out;
192                 }
193                 break;
194         case SNDRV_INFO_CONTENT_DATA:
195                 if (entry->c.ops->llseek) {
196                         ret = entry->c.ops->llseek(entry,
197                                                     data->file_private_data,
198                                                     file, offset, orig);
199                         goto out;
200                 }
201                 break;
202         }
203         ret = -ENXIO;
204 out:
205         unlock_kernel();
206         return ret;
207 }
208
209 static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
210                                    size_t count, loff_t * offset)
211 {
212         struct snd_info_private_data *data;
213         struct snd_info_entry *entry;
214         struct snd_info_buffer *buf;
215         size_t size = 0;
216         loff_t pos;
217
218         data = file->private_data;
219         snd_assert(data != NULL, return -ENXIO);
220         pos = *offset;
221         if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
222                 return -EIO;
223         if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
224                 return -EIO;
225         entry = data->entry;
226         switch (entry->content) {
227         case SNDRV_INFO_CONTENT_TEXT:
228                 buf = data->rbuffer;
229                 if (buf == NULL)
230                         return -EIO;
231                 if (pos >= buf->size)
232                         return 0;
233                 size = buf->size - pos;
234                 size = min(count, size);
235                 if (copy_to_user(buffer, buf->buffer + pos, size))
236                         return -EFAULT;
237                 break;
238         case SNDRV_INFO_CONTENT_DATA:
239                 if (entry->c.ops->read)
240                         size = entry->c.ops->read(entry,
241                                                   data->file_private_data,
242                                                   file, buffer, count, pos);
243                 break;
244         }
245         if ((ssize_t) size > 0)
246                 *offset = pos + size;
247         return size;
248 }
249
250 static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
251                                     size_t count, loff_t * offset)
252 {
253         struct snd_info_private_data *data;
254         struct snd_info_entry *entry;
255         struct snd_info_buffer *buf;
256         ssize_t size = 0;
257         loff_t pos;
258
259         data = file->private_data;
260         snd_assert(data != NULL, return -ENXIO);
261         entry = data->entry;
262         pos = *offset;
263         if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
264                 return -EIO;
265         if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
266                 return -EIO;
267         switch (entry->content) {
268         case SNDRV_INFO_CONTENT_TEXT:
269                 buf = data->wbuffer;
270                 if (buf == NULL)
271                         return -EIO;
272                 mutex_lock(&entry->access);
273                 if (pos + count >= buf->len) {
274                         if (resize_info_buffer(buf, pos + count)) {
275                                 mutex_unlock(&entry->access);
276                                 return -ENOMEM;
277                         }
278                 }
279                 if (copy_from_user(buf->buffer + pos, buffer, count)) {
280                         mutex_unlock(&entry->access);
281                         return -EFAULT;
282                 }
283                 buf->size = pos + count;
284                 mutex_unlock(&entry->access);
285                 size = count;
286                 break;
287         case SNDRV_INFO_CONTENT_DATA:
288                 if (entry->c.ops->write)
289                         size = entry->c.ops->write(entry,
290                                                    data->file_private_data,
291                                                    file, buffer, count, pos);
292                 break;
293         }
294         if ((ssize_t) size > 0)
295                 *offset = pos + size;
296         return size;
297 }
298
299 static int snd_info_entry_open(struct inode *inode, struct file *file)
300 {
301         struct snd_info_entry *entry;
302         struct snd_info_private_data *data;
303         struct snd_info_buffer *buffer;
304         struct proc_dir_entry *p;
305         int mode, err;
306
307         mutex_lock(&info_mutex);
308         p = PDE(inode);
309         entry = p == NULL ? NULL : (struct snd_info_entry *)p->data;
310         if (entry == NULL || ! entry->p) {
311                 mutex_unlock(&info_mutex);
312                 return -ENODEV;
313         }
314         if (!try_module_get(entry->module)) {
315                 err = -EFAULT;
316                 goto __error1;
317         }
318         mode = file->f_flags & O_ACCMODE;
319         if (mode == O_RDONLY || mode == O_RDWR) {
320                 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
321                      entry->c.ops->read == NULL)) {
322                         err = -ENODEV;
323                         goto __error;
324                 }
325         }
326         if (mode == O_WRONLY || mode == O_RDWR) {
327                 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
328                      entry->c.ops->write == NULL)) {
329                         err = -ENODEV;
330                         goto __error;
331                 }
332         }
333         data = kzalloc(sizeof(*data), GFP_KERNEL);
334         if (data == NULL) {
335                 err = -ENOMEM;
336                 goto __error;
337         }
338         data->entry = entry;
339         switch (entry->content) {
340         case SNDRV_INFO_CONTENT_TEXT:
341                 if (mode == O_RDONLY || mode == O_RDWR) {
342                         buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
343                         if (buffer == NULL)
344                                 goto __nomem;
345                         data->rbuffer = buffer;
346                         buffer->len = PAGE_SIZE;
347                         buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
348                         if (buffer->buffer == NULL)
349                                 goto __nomem;
350                 }
351                 if (mode == O_WRONLY || mode == O_RDWR) {
352                         buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
353                         if (buffer == NULL)
354                                 goto __nomem;
355                         data->wbuffer = buffer;
356                         buffer->len = PAGE_SIZE;
357                         buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
358                         if (buffer->buffer == NULL)
359                                 goto __nomem;
360                 }
361                 break;
362         case SNDRV_INFO_CONTENT_DATA:   /* data */
363                 if (entry->c.ops->open) {
364                         if ((err = entry->c.ops->open(entry, mode,
365                                                       &data->file_private_data)) < 0) {
366                                 kfree(data);
367                                 goto __error;
368                         }
369                 }
370                 break;
371         }
372         file->private_data = data;
373         mutex_unlock(&info_mutex);
374         if (entry->content == SNDRV_INFO_CONTENT_TEXT &&
375             (mode == O_RDONLY || mode == O_RDWR)) {
376                 if (entry->c.text.read) {
377                         mutex_lock(&entry->access);
378                         entry->c.text.read(entry, data->rbuffer);
379                         mutex_unlock(&entry->access);
380                 }
381         }
382         return 0;
383
384  __nomem:
385         if (data->rbuffer) {
386                 kfree(data->rbuffer->buffer);
387                 kfree(data->rbuffer);
388         }
389         if (data->wbuffer) {
390                 kfree(data->wbuffer->buffer);
391                 kfree(data->wbuffer);
392         }
393         kfree(data);
394         err = -ENOMEM;
395       __error:
396         module_put(entry->module);
397       __error1:
398         mutex_unlock(&info_mutex);
399         return err;
400 }
401
402 static int snd_info_entry_release(struct inode *inode, struct file *file)
403 {
404         struct snd_info_entry *entry;
405         struct snd_info_private_data *data;
406         int mode;
407
408         mode = file->f_flags & O_ACCMODE;
409         data = file->private_data;
410         entry = data->entry;
411         switch (entry->content) {
412         case SNDRV_INFO_CONTENT_TEXT:
413                 if (data->rbuffer) {
414                         kfree(data->rbuffer->buffer);
415                         kfree(data->rbuffer);
416                 }
417                 if (data->wbuffer) {
418                         if (entry->c.text.write) {
419                                 entry->c.text.write(entry, data->wbuffer);
420                                 if (data->wbuffer->error) {
421                                         snd_printk(KERN_WARNING "data write error to %s (%i)\n",
422                                                 entry->name,
423                                                 data->wbuffer->error);
424                                 }
425                         }
426                         kfree(data->wbuffer->buffer);
427                         kfree(data->wbuffer);
428                 }
429                 break;
430         case SNDRV_INFO_CONTENT_DATA:
431                 if (entry->c.ops->release)
432                         entry->c.ops->release(entry, mode,
433                                               data->file_private_data);
434                 break;
435         }
436         module_put(entry->module);
437         kfree(data);
438         return 0;
439 }
440
441 static unsigned int snd_info_entry_poll(struct file *file, poll_table * wait)
442 {
443         struct snd_info_private_data *data;
444         struct snd_info_entry *entry;
445         unsigned int mask;
446
447         data = file->private_data;
448         if (data == NULL)
449                 return 0;
450         entry = data->entry;
451         mask = 0;
452         switch (entry->content) {
453         case SNDRV_INFO_CONTENT_DATA:
454                 if (entry->c.ops->poll)
455                         return entry->c.ops->poll(entry,
456                                                   data->file_private_data,
457                                                   file, wait);
458                 if (entry->c.ops->read)
459                         mask |= POLLIN | POLLRDNORM;
460                 if (entry->c.ops->write)
461                         mask |= POLLOUT | POLLWRNORM;
462                 break;
463         }
464         return mask;
465 }
466
467 static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
468                                 unsigned long arg)
469 {
470         struct snd_info_private_data *data;
471         struct snd_info_entry *entry;
472
473         data = file->private_data;
474         if (data == NULL)
475                 return 0;
476         entry = data->entry;
477         switch (entry->content) {
478         case SNDRV_INFO_CONTENT_DATA:
479                 if (entry->c.ops->ioctl)
480                         return entry->c.ops->ioctl(entry,
481                                                    data->file_private_data,
482                                                    file, cmd, arg);
483                 break;
484         }
485         return -ENOTTY;
486 }
487
488 static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
489 {
490         struct inode *inode = file->f_path.dentry->d_inode;
491         struct snd_info_private_data *data;
492         struct snd_info_entry *entry;
493
494         data = file->private_data;
495         if (data == NULL)
496                 return 0;
497         entry = data->entry;
498         switch (entry->content) {
499         case SNDRV_INFO_CONTENT_DATA:
500                 if (entry->c.ops->mmap)
501                         return entry->c.ops->mmap(entry,
502                                                   data->file_private_data,
503                                                   inode, file, vma);
504                 break;
505         }
506         return -ENXIO;
507 }
508
509 static const struct file_operations snd_info_entry_operations =
510 {
511         .owner =                THIS_MODULE,
512         .llseek =               snd_info_entry_llseek,
513         .read =                 snd_info_entry_read,
514         .write =                snd_info_entry_write,
515         .poll =                 snd_info_entry_poll,
516         .unlocked_ioctl =       snd_info_entry_ioctl,
517         .mmap =                 snd_info_entry_mmap,
518         .open =                 snd_info_entry_open,
519         .release =              snd_info_entry_release,
520 };
521
522 /**
523  * snd_create_proc_entry - create a procfs entry
524  * @name: the name of the proc file
525  * @mode: the file permission bits, S_Ixxx
526  * @parent: the parent proc-directory entry
527  *
528  * Creates a new proc file entry with the given name and permission
529  * on the given directory.
530  *
531  * Returns the pointer of new instance or NULL on failure.
532  */
533 static struct proc_dir_entry *snd_create_proc_entry(const char *name, mode_t mode,
534                                                     struct proc_dir_entry *parent)
535 {
536         struct proc_dir_entry *p;
537         p = create_proc_entry(name, mode, parent);
538         if (p)
539                 snd_info_entry_prepare(p);
540         return p;
541 }
542
543 int __init snd_info_init(void)
544 {
545         struct proc_dir_entry *p;
546
547         p = snd_create_proc_entry("asound", S_IFDIR | S_IRUGO | S_IXUGO, &proc_root);
548         if (p == NULL)
549                 return -ENOMEM;
550         snd_proc_root = p;
551 #ifdef CONFIG_SND_OSSEMUL
552         {
553                 struct snd_info_entry *entry;
554                 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
555                         return -ENOMEM;
556                 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
557                 if (snd_info_register(entry) < 0) {
558                         snd_info_free_entry(entry);
559                         return -ENOMEM;
560                 }
561                 snd_oss_root = entry;
562         }
563 #endif
564 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
565         {
566                 struct snd_info_entry *entry;
567                 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL)
568                         return -ENOMEM;
569                 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
570                 if (snd_info_register(entry) < 0) {
571                         snd_info_free_entry(entry);
572                         return -ENOMEM;
573                 }
574                 snd_seq_root = entry;
575         }
576 #endif
577         snd_info_version_init();
578         snd_minor_info_init();
579         snd_minor_info_oss_init();
580         snd_card_info_init();
581         return 0;
582 }
583
584 int __exit snd_info_done(void)
585 {
586         snd_card_info_done();
587         snd_minor_info_oss_done();
588         snd_minor_info_done();
589         snd_info_version_done();
590         if (snd_proc_root) {
591 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
592                 snd_info_free_entry(snd_seq_root);
593 #endif
594 #ifdef CONFIG_SND_OSSEMUL
595                 snd_info_free_entry(snd_oss_root);
596 #endif
597                 snd_remove_proc_entry(&proc_root, snd_proc_root);
598         }
599         return 0;
600 }
601
602 /*
603
604  */
605
606
607 /*
608  * create a card proc file
609  * called from init.c
610  */
611 int snd_info_card_create(struct snd_card *card)
612 {
613         char str[8];
614         struct snd_info_entry *entry;
615
616         snd_assert(card != NULL, return -ENXIO);
617
618         sprintf(str, "card%i", card->number);
619         if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
620                 return -ENOMEM;
621         entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
622         if (snd_info_register(entry) < 0) {
623                 snd_info_free_entry(entry);
624                 return -ENOMEM;
625         }
626         card->proc_root = entry;
627         return 0;
628 }
629
630 /*
631  * register the card proc file
632  * called from init.c
633  */
634 int snd_info_card_register(struct snd_card *card)
635 {
636         struct proc_dir_entry *p;
637
638         snd_assert(card != NULL, return -ENXIO);
639
640         if (!strcmp(card->id, card->proc_root->name))
641                 return 0;
642
643         p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
644         if (p == NULL)
645                 return -ENOMEM;
646         card->proc_root_link = p;
647         return 0;
648 }
649
650 /*
651  * de-register the card proc file
652  * called from init.c
653  */
654 void snd_info_card_disconnect(struct snd_card *card)
655 {
656         snd_assert(card != NULL, return);
657         mutex_lock(&info_mutex);
658         if (card->proc_root_link) {
659                 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
660                 card->proc_root_link = NULL;
661         }
662         if (card->proc_root)
663                 snd_info_disconnect(card->proc_root);
664         mutex_unlock(&info_mutex);
665 }
666
667 /*
668  * release the card proc file resources
669  * called from init.c
670  */
671 int snd_info_card_free(struct snd_card *card)
672 {
673         snd_assert(card != NULL, return -ENXIO);
674         snd_info_free_entry(card->proc_root);
675         card->proc_root = NULL;
676         return 0;
677 }
678
679
680 /**
681  * snd_info_get_line - read one line from the procfs buffer
682  * @buffer: the procfs buffer
683  * @line: the buffer to store
684  * @len: the max. buffer size - 1
685  *
686  * Reads one line from the buffer and stores the string.
687  *
688  * Returns zero if successful, or 1 if error or EOF.
689  */
690 int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
691 {
692         int c = -1;
693
694         if (len <= 0 || buffer->stop || buffer->error)
695                 return 1;
696         while (--len > 0) {
697                 c = buffer->buffer[buffer->curr++];
698                 if (c == '\n') {
699                         if (buffer->curr >= buffer->size)
700                                 buffer->stop = 1;
701                         break;
702                 }
703                 *line++ = c;
704                 if (buffer->curr >= buffer->size) {
705                         buffer->stop = 1;
706                         break;
707                 }
708         }
709         while (c != '\n' && !buffer->stop) {
710                 c = buffer->buffer[buffer->curr++];
711                 if (buffer->curr >= buffer->size)
712                         buffer->stop = 1;
713         }
714         *line = '\0';
715         return 0;
716 }
717
718 EXPORT_SYMBOL(snd_info_get_line);
719
720 /**
721  * snd_info_get_str - parse a string token
722  * @dest: the buffer to store the string token
723  * @src: the original string
724  * @len: the max. length of token - 1
725  *
726  * Parses the original string and copy a token to the given
727  * string buffer.
728  *
729  * Returns the updated pointer of the original string so that
730  * it can be used for the next call.
731  */
732 char *snd_info_get_str(char *dest, char *src, int len)
733 {
734         int c;
735
736         while (*src == ' ' || *src == '\t')
737                 src++;
738         if (*src == '"' || *src == '\'') {
739                 c = *src++;
740                 while (--len > 0 && *src && *src != c) {
741                         *dest++ = *src++;
742                 }
743                 if (*src == c)
744                         src++;
745         } else {
746                 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
747                         *dest++ = *src++;
748                 }
749         }
750         *dest = 0;
751         while (*src == ' ' || *src == '\t')
752                 src++;
753         return src;
754 }
755
756 EXPORT_SYMBOL(snd_info_get_str);
757
758 /**
759  * snd_info_create_entry - create an info entry
760  * @name: the proc file name
761  *
762  * Creates an info entry with the given file name and initializes as
763  * the default state.
764  *
765  * Usually called from other functions such as
766  * snd_info_create_card_entry().
767  *
768  * Returns the pointer of the new instance, or NULL on failure.
769  */
770 static struct snd_info_entry *snd_info_create_entry(const char *name)
771 {
772         struct snd_info_entry *entry;
773         entry = kzalloc(sizeof(*entry), GFP_KERNEL);
774         if (entry == NULL)
775                 return NULL;
776         entry->name = kstrdup(name, GFP_KERNEL);
777         if (entry->name == NULL) {
778                 kfree(entry);
779                 return NULL;
780         }
781         entry->mode = S_IFREG | S_IRUGO;
782         entry->content = SNDRV_INFO_CONTENT_TEXT;
783         mutex_init(&entry->access);
784         INIT_LIST_HEAD(&entry->children);
785         INIT_LIST_HEAD(&entry->list);
786         return entry;
787 }
788
789 /**
790  * snd_info_create_module_entry - create an info entry for the given module
791  * @module: the module pointer
792  * @name: the file name
793  * @parent: the parent directory
794  *
795  * Creates a new info entry and assigns it to the given module.
796  *
797  * Returns the pointer of the new instance, or NULL on failure.
798  */
799 struct snd_info_entry *snd_info_create_module_entry(struct module * module,
800                                                const char *name,
801                                                struct snd_info_entry *parent)
802 {
803         struct snd_info_entry *entry = snd_info_create_entry(name);
804         if (entry) {
805                 entry->module = module;
806                 entry->parent = parent;
807         }
808         return entry;
809 }
810
811 EXPORT_SYMBOL(snd_info_create_module_entry);
812
813 /**
814  * snd_info_create_card_entry - create an info entry for the given card
815  * @card: the card instance
816  * @name: the file name
817  * @parent: the parent directory
818  *
819  * Creates a new info entry and assigns it to the given card.
820  *
821  * Returns the pointer of the new instance, or NULL on failure.
822  */
823 struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
824                                              const char *name,
825                                              struct snd_info_entry * parent)
826 {
827         struct snd_info_entry *entry = snd_info_create_entry(name);
828         if (entry) {
829                 entry->module = card->module;
830                 entry->card = card;
831                 entry->parent = parent;
832         }
833         return entry;
834 }
835
836 EXPORT_SYMBOL(snd_info_create_card_entry);
837
838 static void snd_info_disconnect(struct snd_info_entry *entry)
839 {
840         struct list_head *p, *n;
841         struct proc_dir_entry *root;
842
843         list_for_each_safe(p, n, &entry->children) {
844                 snd_info_disconnect(list_entry(p, struct snd_info_entry, list));
845         }
846
847         if (! entry->p)
848                 return;
849         list_del_init(&entry->list);
850         root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
851         snd_assert(root, return);
852         snd_remove_proc_entry(root, entry->p);
853         entry->p = NULL;
854 }
855
856 static int snd_info_dev_free_entry(struct snd_device *device)
857 {
858         struct snd_info_entry *entry = device->device_data;
859         snd_info_free_entry(entry);
860         return 0;
861 }
862
863 static int snd_info_dev_register_entry(struct snd_device *device)
864 {
865         struct snd_info_entry *entry = device->device_data;
866         return snd_info_register(entry);
867 }
868
869 /**
870  * snd_card_proc_new - create an info entry for the given card
871  * @card: the card instance
872  * @name: the file name
873  * @entryp: the pointer to store the new info entry
874  *
875  * Creates a new info entry and assigns it to the given card.
876  * Unlike snd_info_create_card_entry(), this function registers the
877  * info entry as an ALSA device component, so that it can be
878  * unregistered/released without explicit call.
879  * Also, you don't have to register this entry via snd_info_register(),
880  * since this will be registered by snd_card_register() automatically.
881  *
882  * The parent is assumed as card->proc_root.
883  *
884  * For releasing this entry, use snd_device_free() instead of
885  * snd_info_free_entry(). 
886  *
887  * Returns zero if successful, or a negative error code on failure.
888  */
889 int snd_card_proc_new(struct snd_card *card, const char *name,
890                       struct snd_info_entry **entryp)
891 {
892         static struct snd_device_ops ops = {
893                 .dev_free = snd_info_dev_free_entry,
894                 .dev_register = snd_info_dev_register_entry,
895                 /* disconnect is done via snd_info_card_disconnect() */
896         };
897         struct snd_info_entry *entry;
898         int err;
899
900         entry = snd_info_create_card_entry(card, name, card->proc_root);
901         if (! entry)
902                 return -ENOMEM;
903         if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
904                 snd_info_free_entry(entry);
905                 return err;
906         }
907         if (entryp)
908                 *entryp = entry;
909         return 0;
910 }
911
912 EXPORT_SYMBOL(snd_card_proc_new);
913
914 /**
915  * snd_info_free_entry - release the info entry
916  * @entry: the info entry
917  *
918  * Releases the info entry.  Don't call this after registered.
919  */
920 void snd_info_free_entry(struct snd_info_entry * entry)
921 {
922         if (entry == NULL)
923                 return;
924         if (entry->p) {
925                 mutex_lock(&info_mutex);
926                 snd_info_disconnect(entry);
927                 mutex_unlock(&info_mutex);
928         }
929         kfree(entry->name);
930         if (entry->private_free)
931                 entry->private_free(entry);
932         kfree(entry);
933 }
934
935 EXPORT_SYMBOL(snd_info_free_entry);
936
937 /**
938  * snd_info_register - register the info entry
939  * @entry: the info entry
940  *
941  * Registers the proc info entry.
942  *
943  * Returns zero if successful, or a negative error code on failure.
944  */
945 int snd_info_register(struct snd_info_entry * entry)
946 {
947         struct proc_dir_entry *root, *p = NULL;
948
949         snd_assert(entry != NULL, return -ENXIO);
950         root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
951         mutex_lock(&info_mutex);
952         p = snd_create_proc_entry(entry->name, entry->mode, root);
953         if (!p) {
954                 mutex_unlock(&info_mutex);
955                 return -ENOMEM;
956         }
957         p->owner = entry->module;
958         if (!S_ISDIR(entry->mode))
959                 p->proc_fops = &snd_info_entry_operations;
960         p->size = entry->size;
961         p->data = entry;
962         entry->p = p;
963         if (entry->parent)
964                 list_add_tail(&entry->list, &entry->parent->children);
965         mutex_unlock(&info_mutex);
966         return 0;
967 }
968
969 EXPORT_SYMBOL(snd_info_register);
970
971 /*
972
973  */
974
975 static struct snd_info_entry *snd_info_version_entry;
976
977 static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
978 {
979         snd_iprintf(buffer,
980                     "Advanced Linux Sound Architecture Driver Version "
981                     CONFIG_SND_VERSION CONFIG_SND_DATE ".\n"
982                    );
983 }
984
985 static int __init snd_info_version_init(void)
986 {
987         struct snd_info_entry *entry;
988
989         entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
990         if (entry == NULL)
991                 return -ENOMEM;
992         entry->c.text.read = snd_info_version_read;
993         if (snd_info_register(entry) < 0) {
994                 snd_info_free_entry(entry);
995                 return -ENOMEM;
996         }
997         snd_info_version_entry = entry;
998         return 0;
999 }
1000
1001 static int __exit snd_info_version_done(void)
1002 {
1003         snd_info_free_entry(snd_info_version_entry);
1004         return 0;
1005 }
1006
1007 #endif /* CONFIG_PROC_FS */