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