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