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