blob: af123e3bdb24780aa9964275c46f61eac8ac9a8c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/vmalloc.h>
25#include <linux/time.h>
26#include <linux/smp_lock.h>
Paulo Marques543537b2005-06-23 00:09:02 -070027#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <sound/core.h>
29#include <sound/minors.h>
30#include <sound/info.h>
31#include <sound/version.h>
32#include <linux/proc_fs.h>
33#include <linux/devfs_fs_kernel.h>
34#include <stdarg.h>
35
36/*
37 *
38 */
39
Takashi Iwaie28563c2005-12-01 10:42:42 +010040#ifdef CONFIG_PROC_FS
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042int snd_info_check_reserved_words(const char *str)
43{
44 static char *reserved[] =
45 {
46 "version",
47 "meminfo",
48 "memdebug",
49 "detect",
50 "devices",
51 "oss",
52 "cards",
53 "timers",
54 "synth",
55 "pcm",
56 "seq",
57 NULL
58 };
59 char **xstr = reserved;
60
61 while (*xstr) {
62 if (!strcmp(*xstr, str))
63 return 0;
64 xstr++;
65 }
66 if (!strncmp(str, "card", 4))
67 return 0;
68 return 1;
69}
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static DECLARE_MUTEX(info_mutex);
72
Takashi Iwai24c1f932005-11-17 13:58:48 +010073struct snd_info_private_data {
74 struct snd_info_buffer *rbuffer;
75 struct snd_info_buffer *wbuffer;
76 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 void *file_private_data;
Takashi Iwai24c1f932005-11-17 13:58:48 +010078};
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80static int snd_info_version_init(void);
81static int snd_info_version_done(void);
82
83
84/**
85 * snd_iprintf - printf on the procfs buffer
86 * @buffer: the procfs buffer
87 * @fmt: the printf format
88 *
89 * Outputs the string on the procfs buffer just like printf().
90 *
91 * Returns the size of output string.
92 */
Takashi Iwai24c1f932005-11-17 13:58:48 +010093int snd_iprintf(struct snd_info_buffer *buffer, char *fmt,...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 va_list args;
96 int len, res;
97
98 if (buffer->stop || buffer->error)
99 return 0;
100 len = buffer->len - buffer->size;
101 va_start(args, fmt);
102 res = vsnprintf(buffer->curr, len, fmt, args);
103 va_end(args);
104 if (res >= len) {
105 buffer->stop = 1;
106 return 0;
107 }
108 buffer->curr += res;
109 buffer->size += res;
110 return res;
111}
112
113/*
114
115 */
116
117static struct proc_dir_entry *snd_proc_root = NULL;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100118struct snd_info_entry *snd_seq_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai24c1f932005-11-17 13:58:48 +0100120struct snd_info_entry *snd_oss_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#endif
122
123static inline void snd_info_entry_prepare(struct proc_dir_entry *de)
124{
125 de->owner = THIS_MODULE;
126}
127
128static void snd_remove_proc_entry(struct proc_dir_entry *parent,
129 struct proc_dir_entry *de)
130{
131 if (de)
132 remove_proc_entry(de->name, parent);
133}
134
135static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
136{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100137 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 struct snd_info_entry *entry;
139 loff_t ret;
140
141 data = file->private_data;
142 entry = data->entry;
143 lock_kernel();
144 switch (entry->content) {
145 case SNDRV_INFO_CONTENT_TEXT:
146 switch (orig) {
147 case 0: /* SEEK_SET */
148 file->f_pos = offset;
149 ret = file->f_pos;
150 goto out;
151 case 1: /* SEEK_CUR */
152 file->f_pos += offset;
153 ret = file->f_pos;
154 goto out;
155 case 2: /* SEEK_END */
156 default:
157 ret = -EINVAL;
158 goto out;
159 }
160 break;
161 case SNDRV_INFO_CONTENT_DATA:
162 if (entry->c.ops->llseek) {
163 ret = entry->c.ops->llseek(entry,
164 data->file_private_data,
165 file, offset, orig);
166 goto out;
167 }
168 break;
169 }
170 ret = -ENXIO;
171out:
172 unlock_kernel();
173 return ret;
174}
175
176static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
177 size_t count, loff_t * offset)
178{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100179 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 struct snd_info_entry *entry;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100181 struct snd_info_buffer *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 size_t size = 0;
183 loff_t pos;
184
185 data = file->private_data;
186 snd_assert(data != NULL, return -ENXIO);
187 pos = *offset;
188 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
189 return -EIO;
190 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
191 return -EIO;
192 entry = data->entry;
193 switch (entry->content) {
194 case SNDRV_INFO_CONTENT_TEXT:
195 buf = data->rbuffer;
196 if (buf == NULL)
197 return -EIO;
198 if (pos >= buf->size)
199 return 0;
200 size = buf->size - pos;
201 size = min(count, size);
202 if (copy_to_user(buffer, buf->buffer + pos, size))
203 return -EFAULT;
204 break;
205 case SNDRV_INFO_CONTENT_DATA:
206 if (entry->c.ops->read)
207 size = entry->c.ops->read(entry,
208 data->file_private_data,
209 file, buffer, count, pos);
210 break;
211 }
212 if ((ssize_t) size > 0)
213 *offset = pos + size;
214 return size;
215}
216
217static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
218 size_t count, loff_t * offset)
219{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100220 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 struct snd_info_entry *entry;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100222 struct snd_info_buffer *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 size_t size = 0;
224 loff_t pos;
225
226 data = file->private_data;
227 snd_assert(data != NULL, return -ENXIO);
228 entry = data->entry;
229 pos = *offset;
230 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
231 return -EIO;
232 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
233 return -EIO;
234 switch (entry->content) {
235 case SNDRV_INFO_CONTENT_TEXT:
236 buf = data->wbuffer;
237 if (buf == NULL)
238 return -EIO;
239 if (pos >= buf->len)
240 return -ENOMEM;
241 size = buf->len - pos;
242 size = min(count, size);
243 if (copy_from_user(buf->buffer + pos, buffer, size))
244 return -EFAULT;
245 if ((long)buf->size < pos + size)
246 buf->size = pos + size;
247 break;
248 case SNDRV_INFO_CONTENT_DATA:
249 if (entry->c.ops->write)
250 size = entry->c.ops->write(entry,
251 data->file_private_data,
252 file, buffer, count, pos);
253 break;
254 }
255 if ((ssize_t) size > 0)
256 *offset = pos + size;
257 return size;
258}
259
260static int snd_info_entry_open(struct inode *inode, struct file *file)
261{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100262 struct snd_info_entry *entry;
263 struct snd_info_private_data *data;
264 struct snd_info_buffer *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 struct proc_dir_entry *p;
266 int mode, err;
267
268 down(&info_mutex);
269 p = PDE(inode);
Takashi Iwai24c1f932005-11-17 13:58:48 +0100270 entry = p == NULL ? NULL : (struct snd_info_entry *)p->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (entry == NULL || entry->disconnected) {
272 up(&info_mutex);
273 return -ENODEV;
274 }
275 if (!try_module_get(entry->module)) {
276 err = -EFAULT;
277 goto __error1;
278 }
279 mode = file->f_flags & O_ACCMODE;
280 if (mode == O_RDONLY || mode == O_RDWR) {
281 if ((entry->content == SNDRV_INFO_CONTENT_TEXT &&
282 !entry->c.text.read_size) ||
283 (entry->content == SNDRV_INFO_CONTENT_DATA &&
284 entry->c.ops->read == NULL)) {
285 err = -ENODEV;
286 goto __error;
287 }
288 }
289 if (mode == O_WRONLY || mode == O_RDWR) {
290 if ((entry->content == SNDRV_INFO_CONTENT_TEXT &&
291 !entry->c.text.write_size) ||
292 (entry->content == SNDRV_INFO_CONTENT_DATA &&
293 entry->c.ops->write == NULL)) {
294 err = -ENODEV;
295 goto __error;
296 }
297 }
Takashi Iwaica2c0962005-09-09 14:20:23 +0200298 data = kzalloc(sizeof(*data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (data == NULL) {
300 err = -ENOMEM;
301 goto __error;
302 }
303 data->entry = entry;
304 switch (entry->content) {
305 case SNDRV_INFO_CONTENT_TEXT:
306 if (mode == O_RDONLY || mode == O_RDWR) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200307 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (buffer == NULL) {
309 kfree(data);
310 err = -ENOMEM;
311 goto __error;
312 }
313 buffer->len = (entry->c.text.read_size +
314 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
315 buffer->buffer = vmalloc(buffer->len);
316 if (buffer->buffer == NULL) {
317 kfree(buffer);
318 kfree(data);
319 err = -ENOMEM;
320 goto __error;
321 }
322 buffer->curr = buffer->buffer;
323 data->rbuffer = buffer;
324 }
325 if (mode == O_WRONLY || mode == O_RDWR) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200326 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (buffer == NULL) {
328 if (mode == O_RDWR) {
329 vfree(data->rbuffer->buffer);
330 kfree(data->rbuffer);
331 }
332 kfree(data);
333 err = -ENOMEM;
334 goto __error;
335 }
336 buffer->len = (entry->c.text.write_size +
337 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
338 buffer->buffer = vmalloc(buffer->len);
339 if (buffer->buffer == NULL) {
340 if (mode == O_RDWR) {
341 vfree(data->rbuffer->buffer);
342 kfree(data->rbuffer);
343 }
344 kfree(buffer);
345 kfree(data);
346 err = -ENOMEM;
347 goto __error;
348 }
349 buffer->curr = buffer->buffer;
350 data->wbuffer = buffer;
351 }
352 break;
353 case SNDRV_INFO_CONTENT_DATA: /* data */
354 if (entry->c.ops->open) {
355 if ((err = entry->c.ops->open(entry, mode,
356 &data->file_private_data)) < 0) {
357 kfree(data);
358 goto __error;
359 }
360 }
361 break;
362 }
363 file->private_data = data;
364 up(&info_mutex);
365 if (entry->content == SNDRV_INFO_CONTENT_TEXT &&
366 (mode == O_RDONLY || mode == O_RDWR)) {
367 if (entry->c.text.read) {
368 down(&entry->access);
369 entry->c.text.read(entry, data->rbuffer);
370 up(&entry->access);
371 }
372 }
373 return 0;
374
375 __error:
376 module_put(entry->module);
377 __error1:
378 up(&info_mutex);
379 return err;
380}
381
382static int snd_info_entry_release(struct inode *inode, struct file *file)
383{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100384 struct snd_info_entry *entry;
385 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 int mode;
387
388 mode = file->f_flags & O_ACCMODE;
389 data = file->private_data;
390 entry = data->entry;
391 switch (entry->content) {
392 case SNDRV_INFO_CONTENT_TEXT:
393 if (mode == O_RDONLY || mode == O_RDWR) {
394 vfree(data->rbuffer->buffer);
395 kfree(data->rbuffer);
396 }
397 if (mode == O_WRONLY || mode == O_RDWR) {
398 if (entry->c.text.write) {
399 entry->c.text.write(entry, data->wbuffer);
400 if (data->wbuffer->error) {
401 snd_printk(KERN_WARNING "data write error to %s (%i)\n",
402 entry->name,
403 data->wbuffer->error);
404 }
405 }
406 vfree(data->wbuffer->buffer);
407 kfree(data->wbuffer);
408 }
409 break;
410 case SNDRV_INFO_CONTENT_DATA:
411 if (entry->c.ops->release)
412 entry->c.ops->release(entry, mode,
413 data->file_private_data);
414 break;
415 }
416 module_put(entry->module);
417 kfree(data);
418 return 0;
419}
420
421static unsigned int snd_info_entry_poll(struct file *file, poll_table * wait)
422{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100423 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 struct snd_info_entry *entry;
425 unsigned int mask;
426
427 data = file->private_data;
428 if (data == NULL)
429 return 0;
430 entry = data->entry;
431 mask = 0;
432 switch (entry->content) {
433 case SNDRV_INFO_CONTENT_DATA:
434 if (entry->c.ops->poll)
435 return entry->c.ops->poll(entry,
436 data->file_private_data,
437 file, wait);
438 if (entry->c.ops->read)
439 mask |= POLLIN | POLLRDNORM;
440 if (entry->c.ops->write)
441 mask |= POLLOUT | POLLWRNORM;
442 break;
443 }
444 return mask;
445}
446
Ingo Molnard99e9882006-01-09 16:44:46 +0100447static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
448 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100450 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 struct snd_info_entry *entry;
452
453 data = file->private_data;
454 if (data == NULL)
455 return 0;
456 entry = data->entry;
457 switch (entry->content) {
458 case SNDRV_INFO_CONTENT_DATA:
459 if (entry->c.ops->ioctl)
460 return entry->c.ops->ioctl(entry,
461 data->file_private_data,
462 file, cmd, arg);
463 break;
464 }
465 return -ENOTTY;
466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
469{
470 struct inode *inode = file->f_dentry->d_inode;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100471 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 struct snd_info_entry *entry;
473
474 data = file->private_data;
475 if (data == NULL)
476 return 0;
477 entry = data->entry;
478 switch (entry->content) {
479 case SNDRV_INFO_CONTENT_DATA:
480 if (entry->c.ops->mmap)
481 return entry->c.ops->mmap(entry,
482 data->file_private_data,
483 inode, file, vma);
484 break;
485 }
486 return -ENXIO;
487}
488
489static struct file_operations snd_info_entry_operations =
490{
Ingo Molnard99e9882006-01-09 16:44:46 +0100491 .owner = THIS_MODULE,
492 .llseek = snd_info_entry_llseek,
493 .read = snd_info_entry_read,
494 .write = snd_info_entry_write,
495 .poll = snd_info_entry_poll,
496 .unlocked_ioctl = snd_info_entry_ioctl,
497 .mmap = snd_info_entry_mmap,
498 .open = snd_info_entry_open,
499 .release = snd_info_entry_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500};
501
502/**
503 * snd_create_proc_entry - create a procfs entry
504 * @name: the name of the proc file
505 * @mode: the file permission bits, S_Ixxx
506 * @parent: the parent proc-directory entry
507 *
508 * Creates a new proc file entry with the given name and permission
509 * on the given directory.
510 *
511 * Returns the pointer of new instance or NULL on failure.
512 */
513static struct proc_dir_entry *snd_create_proc_entry(const char *name, mode_t mode,
514 struct proc_dir_entry *parent)
515{
516 struct proc_dir_entry *p;
517 p = create_proc_entry(name, mode, parent);
518 if (p)
519 snd_info_entry_prepare(p);
520 return p;
521}
522
523int __init snd_info_init(void)
524{
525 struct proc_dir_entry *p;
526
527 p = snd_create_proc_entry("asound", S_IFDIR | S_IRUGO | S_IXUGO, &proc_root);
528 if (p == NULL)
529 return -ENOMEM;
530 snd_proc_root = p;
531#ifdef CONFIG_SND_OSSEMUL
532 {
Takashi Iwai24c1f932005-11-17 13:58:48 +0100533 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
535 return -ENOMEM;
536 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
537 if (snd_info_register(entry) < 0) {
538 snd_info_free_entry(entry);
539 return -ENOMEM;
540 }
541 snd_oss_root = entry;
542 }
543#endif
544#if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
545 {
Takashi Iwai24c1f932005-11-17 13:58:48 +0100546 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL)
548 return -ENOMEM;
549 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
550 if (snd_info_register(entry) < 0) {
551 snd_info_free_entry(entry);
552 return -ENOMEM;
553 }
554 snd_seq_root = entry;
555 }
556#endif
557 snd_info_version_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 snd_minor_info_init();
559 snd_minor_info_oss_init();
560 snd_card_info_init();
561 return 0;
562}
563
564int __exit snd_info_done(void)
565{
566 snd_card_info_done();
567 snd_minor_info_oss_done();
568 snd_minor_info_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 snd_info_version_done();
570 if (snd_proc_root) {
571#if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
Takashi Iwaie28563c2005-12-01 10:42:42 +0100572 snd_info_unregister(snd_seq_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573#endif
574#ifdef CONFIG_SND_OSSEMUL
Takashi Iwaie28563c2005-12-01 10:42:42 +0100575 snd_info_unregister(snd_oss_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576#endif
577 snd_remove_proc_entry(&proc_root, snd_proc_root);
578 }
579 return 0;
580}
581
582/*
583
584 */
585
586
587/*
588 * create a card proc file
589 * called from init.c
590 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100591int snd_info_card_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 char str[8];
Takashi Iwai24c1f932005-11-17 13:58:48 +0100594 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 snd_assert(card != NULL, return -ENXIO);
597
598 sprintf(str, "card%i", card->number);
599 if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
600 return -ENOMEM;
601 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
602 if (snd_info_register(entry) < 0) {
603 snd_info_free_entry(entry);
604 return -ENOMEM;
605 }
606 card->proc_root = entry;
607 return 0;
608}
609
610/*
611 * register the card proc file
612 * called from init.c
613 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100614int snd_info_card_register(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 struct proc_dir_entry *p;
617
618 snd_assert(card != NULL, return -ENXIO);
619
620 if (!strcmp(card->id, card->proc_root->name))
621 return 0;
622
623 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
624 if (p == NULL)
625 return -ENOMEM;
626 card->proc_root_link = p;
627 return 0;
628}
629
630/*
631 * de-register the card proc file
632 * called from init.c
633 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100634int snd_info_card_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 snd_assert(card != NULL, return -ENXIO);
637 if (card->proc_root_link) {
638 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
639 card->proc_root_link = NULL;
640 }
641 if (card->proc_root) {
642 snd_info_unregister(card->proc_root);
643 card->proc_root = NULL;
644 }
645 return 0;
646}
647
648
649/**
650 * snd_info_get_line - read one line from the procfs buffer
651 * @buffer: the procfs buffer
652 * @line: the buffer to store
653 * @len: the max. buffer size - 1
654 *
655 * Reads one line from the buffer and stores the string.
656 *
657 * Returns zero if successful, or 1 if error or EOF.
658 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100659int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 int c = -1;
662
663 if (len <= 0 || buffer->stop || buffer->error)
664 return 1;
665 while (--len > 0) {
666 c = *buffer->curr++;
667 if (c == '\n') {
668 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
669 buffer->stop = 1;
670 }
671 break;
672 }
673 *line++ = c;
674 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
675 buffer->stop = 1;
676 break;
677 }
678 }
679 while (c != '\n' && !buffer->stop) {
680 c = *buffer->curr++;
681 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
682 buffer->stop = 1;
683 }
684 }
685 *line = '\0';
686 return 0;
687}
688
689/**
Henrik Kretzschmar856def82005-07-08 13:53:42 +0200690 * snd_info_get_str - parse a string token
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 * @dest: the buffer to store the string token
692 * @src: the original string
693 * @len: the max. length of token - 1
694 *
695 * Parses the original string and copy a token to the given
696 * string buffer.
697 *
698 * Returns the updated pointer of the original string so that
699 * it can be used for the next call.
700 */
701char *snd_info_get_str(char *dest, char *src, int len)
702{
703 int c;
704
705 while (*src == ' ' || *src == '\t')
706 src++;
707 if (*src == '"' || *src == '\'') {
708 c = *src++;
709 while (--len > 0 && *src && *src != c) {
710 *dest++ = *src++;
711 }
712 if (*src == c)
713 src++;
714 } else {
715 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
716 *dest++ = *src++;
717 }
718 }
719 *dest = 0;
720 while (*src == ' ' || *src == '\t')
721 src++;
722 return src;
723}
724
725/**
726 * snd_info_create_entry - create an info entry
727 * @name: the proc file name
728 *
729 * Creates an info entry with the given file name and initializes as
730 * the default state.
731 *
732 * Usually called from other functions such as
733 * snd_info_create_card_entry().
734 *
735 * Returns the pointer of the new instance, or NULL on failure.
736 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100737static struct snd_info_entry *snd_info_create_entry(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100739 struct snd_info_entry *entry;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200740 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (entry == NULL)
742 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -0700743 entry->name = kstrdup(name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (entry->name == NULL) {
745 kfree(entry);
746 return NULL;
747 }
748 entry->mode = S_IFREG | S_IRUGO;
749 entry->content = SNDRV_INFO_CONTENT_TEXT;
750 init_MUTEX(&entry->access);
751 return entry;
752}
753
754/**
755 * snd_info_create_module_entry - create an info entry for the given module
756 * @module: the module pointer
757 * @name: the file name
758 * @parent: the parent directory
759 *
760 * Creates a new info entry and assigns it to the given module.
761 *
762 * Returns the pointer of the new instance, or NULL on failure.
763 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100764struct snd_info_entry *snd_info_create_module_entry(struct module * module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 const char *name,
Takashi Iwai24c1f932005-11-17 13:58:48 +0100766 struct snd_info_entry *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100768 struct snd_info_entry *entry = snd_info_create_entry(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (entry) {
770 entry->module = module;
771 entry->parent = parent;
772 }
773 return entry;
774}
775
776/**
777 * snd_info_create_card_entry - create an info entry for the given card
778 * @card: the card instance
779 * @name: the file name
780 * @parent: the parent directory
781 *
782 * Creates a new info entry and assigns it to the given card.
783 *
784 * Returns the pointer of the new instance, or NULL on failure.
785 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100786struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 const char *name,
Takashi Iwai24c1f932005-11-17 13:58:48 +0100788 struct snd_info_entry * parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100790 struct snd_info_entry *entry = snd_info_create_entry(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (entry) {
792 entry->module = card->module;
793 entry->card = card;
794 entry->parent = parent;
795 }
796 return entry;
797}
798
Takashi Iwai24c1f932005-11-17 13:58:48 +0100799static int snd_info_dev_free_entry(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100801 struct snd_info_entry *entry = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 snd_info_free_entry(entry);
803 return 0;
804}
805
Takashi Iwai24c1f932005-11-17 13:58:48 +0100806static int snd_info_dev_register_entry(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100808 struct snd_info_entry *entry = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return snd_info_register(entry);
810}
811
Takashi Iwai24c1f932005-11-17 13:58:48 +0100812static int snd_info_dev_disconnect_entry(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100814 struct snd_info_entry *entry = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 entry->disconnected = 1;
816 return 0;
817}
818
Takashi Iwai24c1f932005-11-17 13:58:48 +0100819static int snd_info_dev_unregister_entry(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100821 struct snd_info_entry *entry = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return snd_info_unregister(entry);
823}
824
825/**
826 * snd_card_proc_new - create an info entry for the given card
827 * @card: the card instance
828 * @name: the file name
829 * @entryp: the pointer to store the new info entry
830 *
831 * Creates a new info entry and assigns it to the given card.
832 * Unlike snd_info_create_card_entry(), this function registers the
833 * info entry as an ALSA device component, so that it can be
834 * unregistered/released without explicit call.
835 * Also, you don't have to register this entry via snd_info_register(),
836 * since this will be registered by snd_card_register() automatically.
837 *
838 * The parent is assumed as card->proc_root.
839 *
840 * For releasing this entry, use snd_device_free() instead of
841 * snd_info_free_entry().
842 *
843 * Returns zero if successful, or a negative error code on failure.
844 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100845int snd_card_proc_new(struct snd_card *card, const char *name,
846 struct snd_info_entry **entryp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100848 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 .dev_free = snd_info_dev_free_entry,
850 .dev_register = snd_info_dev_register_entry,
851 .dev_disconnect = snd_info_dev_disconnect_entry,
852 .dev_unregister = snd_info_dev_unregister_entry
853 };
Takashi Iwai24c1f932005-11-17 13:58:48 +0100854 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 int err;
856
857 entry = snd_info_create_card_entry(card, name, card->proc_root);
858 if (! entry)
859 return -ENOMEM;
860 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
861 snd_info_free_entry(entry);
862 return err;
863 }
864 if (entryp)
865 *entryp = entry;
866 return 0;
867}
868
869/**
870 * snd_info_free_entry - release the info entry
871 * @entry: the info entry
872 *
873 * Releases the info entry. Don't call this after registered.
874 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100875void snd_info_free_entry(struct snd_info_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877 if (entry == NULL)
878 return;
879 kfree(entry->name);
880 if (entry->private_free)
881 entry->private_free(entry);
882 kfree(entry);
883}
884
885/**
886 * snd_info_register - register the info entry
887 * @entry: the info entry
888 *
889 * Registers the proc info entry.
890 *
891 * Returns zero if successful, or a negative error code on failure.
892 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100893int snd_info_register(struct snd_info_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 struct proc_dir_entry *root, *p = NULL;
896
897 snd_assert(entry != NULL, return -ENXIO);
898 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
899 down(&info_mutex);
900 p = snd_create_proc_entry(entry->name, entry->mode, root);
901 if (!p) {
902 up(&info_mutex);
903 return -ENOMEM;
904 }
905 p->owner = entry->module;
906 if (!S_ISDIR(entry->mode))
907 p->proc_fops = &snd_info_entry_operations;
908 p->size = entry->size;
909 p->data = entry;
910 entry->p = p;
911 up(&info_mutex);
912 return 0;
913}
914
915/**
916 * snd_info_unregister - de-register the info entry
917 * @entry: the info entry
918 *
919 * De-registers the info entry and releases the instance.
920 *
921 * Returns zero if successful, or a negative error code on failure.
922 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100923int snd_info_unregister(struct snd_info_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
925 struct proc_dir_entry *root;
926
Takashi Iwaie28563c2005-12-01 10:42:42 +0100927 if (! entry)
928 return 0;
Henrik Kretzschmar856def82005-07-08 13:53:42 +0200929 snd_assert(entry->p != NULL, return -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
931 snd_assert(root, return -ENXIO);
932 down(&info_mutex);
933 snd_remove_proc_entry(root, entry->p);
934 up(&info_mutex);
935 snd_info_free_entry(entry);
936 return 0;
937}
938
939/*
940
941 */
942
Takashi Iwai24c1f932005-11-17 13:58:48 +0100943static struct snd_info_entry *snd_info_version_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Takashi Iwai24c1f932005-11-17 13:58:48 +0100945static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 snd_iprintf(buffer,
948 "Advanced Linux Sound Architecture Driver Version "
949 CONFIG_SND_VERSION CONFIG_SND_DATE ".\n"
950 );
951}
952
953static int __init snd_info_version_init(void)
954{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100955 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
958 if (entry == NULL)
959 return -ENOMEM;
960 entry->c.text.read_size = 256;
961 entry->c.text.read = snd_info_version_read;
962 if (snd_info_register(entry) < 0) {
963 snd_info_free_entry(entry);
964 return -ENOMEM;
965 }
966 snd_info_version_entry = entry;
967 return 0;
968}
969
970static int __exit snd_info_version_done(void)
971{
972 if (snd_info_version_entry)
973 snd_info_unregister(snd_info_version_entry);
974 return 0;
975}
976
977#endif /* CONFIG_PROC_FS */