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