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