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