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