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