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