blob: 9c288539e99d3eef16b16428ec99d652cbd0bb64 [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>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010034#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <stdarg.h>
36
37/*
38 *
39 */
40
Takashi Iwaie28563c2005-12-01 10:42:42 +010041#ifdef CONFIG_PROC_FS
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043int snd_info_check_reserved_words(const char *str)
44{
45 static char *reserved[] =
46 {
47 "version",
48 "meminfo",
49 "memdebug",
50 "detect",
51 "devices",
52 "oss",
53 "cards",
54 "timers",
55 "synth",
56 "pcm",
57 "seq",
58 NULL
59 };
60 char **xstr = reserved;
61
62 while (*xstr) {
63 if (!strcmp(*xstr, str))
64 return 0;
65 xstr++;
66 }
67 if (!strncmp(str, "card", 4))
68 return 0;
69 return 1;
70}
71
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010072static DEFINE_MUTEX(info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Takashi Iwai24c1f932005-11-17 13:58:48 +010074struct snd_info_private_data {
75 struct snd_info_buffer *rbuffer;
76 struct snd_info_buffer *wbuffer;
77 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 void *file_private_data;
Takashi Iwai24c1f932005-11-17 13:58:48 +010079};
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81static int snd_info_version_init(void);
82static int snd_info_version_done(void);
83
84
85/**
86 * snd_iprintf - printf on the procfs buffer
87 * @buffer: the procfs buffer
88 * @fmt: the printf format
89 *
90 * Outputs the string on the procfs buffer just like printf().
91 *
92 * Returns the size of output string.
93 */
Takashi Iwai24c1f932005-11-17 13:58:48 +010094int snd_iprintf(struct snd_info_buffer *buffer, char *fmt,...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 va_list args;
97 int len, res;
98
99 if (buffer->stop || buffer->error)
100 return 0;
101 len = buffer->len - buffer->size;
102 va_start(args, fmt);
103 res = vsnprintf(buffer->curr, len, fmt, args);
104 va_end(args);
105 if (res >= len) {
106 buffer->stop = 1;
107 return 0;
108 }
109 buffer->curr += res;
110 buffer->size += res;
111 return res;
112}
113
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200114EXPORT_SYMBOL(snd_iprintf);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/*
117
118 */
119
120static struct proc_dir_entry *snd_proc_root = NULL;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100121struct snd_info_entry *snd_seq_root = NULL;
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200122EXPORT_SYMBOL(snd_seq_root);
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai24c1f932005-11-17 13:58:48 +0100125struct snd_info_entry *snd_oss_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#endif
127
128static inline void snd_info_entry_prepare(struct proc_dir_entry *de)
129{
130 de->owner = THIS_MODULE;
131}
132
133static void snd_remove_proc_entry(struct proc_dir_entry *parent,
134 struct proc_dir_entry *de)
135{
136 if (de)
137 remove_proc_entry(de->name, parent);
138}
139
140static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
141{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100142 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 struct snd_info_entry *entry;
144 loff_t ret;
145
146 data = file->private_data;
147 entry = data->entry;
148 lock_kernel();
149 switch (entry->content) {
150 case SNDRV_INFO_CONTENT_TEXT:
151 switch (orig) {
152 case 0: /* SEEK_SET */
153 file->f_pos = offset;
154 ret = file->f_pos;
155 goto out;
156 case 1: /* SEEK_CUR */
157 file->f_pos += offset;
158 ret = file->f_pos;
159 goto out;
160 case 2: /* SEEK_END */
161 default:
162 ret = -EINVAL;
163 goto out;
164 }
165 break;
166 case SNDRV_INFO_CONTENT_DATA:
167 if (entry->c.ops->llseek) {
168 ret = entry->c.ops->llseek(entry,
169 data->file_private_data,
170 file, offset, orig);
171 goto out;
172 }
173 break;
174 }
175 ret = -ENXIO;
176out:
177 unlock_kernel();
178 return ret;
179}
180
181static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
182 size_t count, loff_t * offset)
183{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100184 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 struct snd_info_entry *entry;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100186 struct snd_info_buffer *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 size_t size = 0;
188 loff_t pos;
189
190 data = file->private_data;
191 snd_assert(data != NULL, return -ENXIO);
192 pos = *offset;
193 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
194 return -EIO;
195 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
196 return -EIO;
197 entry = data->entry;
198 switch (entry->content) {
199 case SNDRV_INFO_CONTENT_TEXT:
200 buf = data->rbuffer;
201 if (buf == NULL)
202 return -EIO;
203 if (pos >= buf->size)
204 return 0;
205 size = buf->size - pos;
206 size = min(count, size);
207 if (copy_to_user(buffer, buf->buffer + pos, size))
208 return -EFAULT;
209 break;
210 case SNDRV_INFO_CONTENT_DATA:
211 if (entry->c.ops->read)
212 size = entry->c.ops->read(entry,
213 data->file_private_data,
214 file, buffer, count, pos);
215 break;
216 }
217 if ((ssize_t) size > 0)
218 *offset = pos + size;
219 return size;
220}
221
222static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
223 size_t count, loff_t * offset)
224{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100225 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 struct snd_info_entry *entry;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100227 struct snd_info_buffer *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 size_t size = 0;
229 loff_t pos;
230
231 data = file->private_data;
232 snd_assert(data != NULL, return -ENXIO);
233 entry = data->entry;
234 pos = *offset;
235 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
236 return -EIO;
237 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
238 return -EIO;
239 switch (entry->content) {
240 case SNDRV_INFO_CONTENT_TEXT:
241 buf = data->wbuffer;
242 if (buf == NULL)
243 return -EIO;
244 if (pos >= buf->len)
245 return -ENOMEM;
246 size = buf->len - pos;
247 size = min(count, size);
248 if (copy_from_user(buf->buffer + pos, buffer, size))
249 return -EFAULT;
250 if ((long)buf->size < pos + size)
251 buf->size = pos + size;
252 break;
253 case SNDRV_INFO_CONTENT_DATA:
254 if (entry->c.ops->write)
255 size = entry->c.ops->write(entry,
256 data->file_private_data,
257 file, buffer, count, pos);
258 break;
259 }
260 if ((ssize_t) size > 0)
261 *offset = pos + size;
262 return size;
263}
264
265static int snd_info_entry_open(struct inode *inode, struct file *file)
266{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100267 struct snd_info_entry *entry;
268 struct snd_info_private_data *data;
269 struct snd_info_buffer *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 struct proc_dir_entry *p;
271 int mode, err;
272
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100273 mutex_lock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 p = PDE(inode);
Takashi Iwai24c1f932005-11-17 13:58:48 +0100275 entry = p == NULL ? NULL : (struct snd_info_entry *)p->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (entry == NULL || entry->disconnected) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100277 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return -ENODEV;
279 }
280 if (!try_module_get(entry->module)) {
281 err = -EFAULT;
282 goto __error1;
283 }
284 mode = file->f_flags & O_ACCMODE;
285 if (mode == O_RDONLY || mode == O_RDWR) {
286 if ((entry->content == SNDRV_INFO_CONTENT_TEXT &&
287 !entry->c.text.read_size) ||
288 (entry->content == SNDRV_INFO_CONTENT_DATA &&
289 entry->c.ops->read == NULL)) {
290 err = -ENODEV;
291 goto __error;
292 }
293 }
294 if (mode == O_WRONLY || mode == O_RDWR) {
295 if ((entry->content == SNDRV_INFO_CONTENT_TEXT &&
296 !entry->c.text.write_size) ||
297 (entry->content == SNDRV_INFO_CONTENT_DATA &&
298 entry->c.ops->write == NULL)) {
299 err = -ENODEV;
300 goto __error;
301 }
302 }
Takashi Iwaica2c0962005-09-09 14:20:23 +0200303 data = kzalloc(sizeof(*data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (data == NULL) {
305 err = -ENOMEM;
306 goto __error;
307 }
308 data->entry = entry;
309 switch (entry->content) {
310 case SNDRV_INFO_CONTENT_TEXT:
311 if (mode == O_RDONLY || mode == O_RDWR) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200312 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (buffer == NULL) {
314 kfree(data);
315 err = -ENOMEM;
316 goto __error;
317 }
318 buffer->len = (entry->c.text.read_size +
319 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
320 buffer->buffer = vmalloc(buffer->len);
321 if (buffer->buffer == NULL) {
322 kfree(buffer);
323 kfree(data);
324 err = -ENOMEM;
325 goto __error;
326 }
327 buffer->curr = buffer->buffer;
328 data->rbuffer = buffer;
329 }
330 if (mode == O_WRONLY || mode == O_RDWR) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200331 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (buffer == NULL) {
333 if (mode == O_RDWR) {
334 vfree(data->rbuffer->buffer);
335 kfree(data->rbuffer);
336 }
337 kfree(data);
338 err = -ENOMEM;
339 goto __error;
340 }
341 buffer->len = (entry->c.text.write_size +
342 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
343 buffer->buffer = vmalloc(buffer->len);
344 if (buffer->buffer == NULL) {
345 if (mode == O_RDWR) {
346 vfree(data->rbuffer->buffer);
347 kfree(data->rbuffer);
348 }
349 kfree(buffer);
350 kfree(data);
351 err = -ENOMEM;
352 goto __error;
353 }
354 buffer->curr = buffer->buffer;
355 data->wbuffer = buffer;
356 }
357 break;
358 case SNDRV_INFO_CONTENT_DATA: /* data */
359 if (entry->c.ops->open) {
360 if ((err = entry->c.ops->open(entry, mode,
361 &data->file_private_data)) < 0) {
362 kfree(data);
363 goto __error;
364 }
365 }
366 break;
367 }
368 file->private_data = data;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100369 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (entry->content == SNDRV_INFO_CONTENT_TEXT &&
371 (mode == O_RDONLY || mode == O_RDWR)) {
372 if (entry->c.text.read) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100373 mutex_lock(&entry->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 entry->c.text.read(entry, data->rbuffer);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100375 mutex_unlock(&entry->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377 }
378 return 0;
379
380 __error:
381 module_put(entry->module);
382 __error1:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100383 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return err;
385}
386
387static int snd_info_entry_release(struct inode *inode, struct file *file)
388{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100389 struct snd_info_entry *entry;
390 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 int mode;
392
393 mode = file->f_flags & O_ACCMODE;
394 data = file->private_data;
395 entry = data->entry;
396 switch (entry->content) {
397 case SNDRV_INFO_CONTENT_TEXT:
398 if (mode == O_RDONLY || mode == O_RDWR) {
399 vfree(data->rbuffer->buffer);
400 kfree(data->rbuffer);
401 }
402 if (mode == O_WRONLY || mode == O_RDWR) {
403 if (entry->c.text.write) {
404 entry->c.text.write(entry, data->wbuffer);
405 if (data->wbuffer->error) {
406 snd_printk(KERN_WARNING "data write error to %s (%i)\n",
407 entry->name,
408 data->wbuffer->error);
409 }
410 }
411 vfree(data->wbuffer->buffer);
412 kfree(data->wbuffer);
413 }
414 break;
415 case SNDRV_INFO_CONTENT_DATA:
416 if (entry->c.ops->release)
417 entry->c.ops->release(entry, mode,
418 data->file_private_data);
419 break;
420 }
421 module_put(entry->module);
422 kfree(data);
423 return 0;
424}
425
426static unsigned int snd_info_entry_poll(struct file *file, poll_table * wait)
427{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100428 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 struct snd_info_entry *entry;
430 unsigned int mask;
431
432 data = file->private_data;
433 if (data == NULL)
434 return 0;
435 entry = data->entry;
436 mask = 0;
437 switch (entry->content) {
438 case SNDRV_INFO_CONTENT_DATA:
439 if (entry->c.ops->poll)
440 return entry->c.ops->poll(entry,
441 data->file_private_data,
442 file, wait);
443 if (entry->c.ops->read)
444 mask |= POLLIN | POLLRDNORM;
445 if (entry->c.ops->write)
446 mask |= POLLOUT | POLLWRNORM;
447 break;
448 }
449 return mask;
450}
451
Ingo Molnard99e9882006-01-09 16:44:46 +0100452static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
453 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100455 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct snd_info_entry *entry;
457
458 data = file->private_data;
459 if (data == NULL)
460 return 0;
461 entry = data->entry;
462 switch (entry->content) {
463 case SNDRV_INFO_CONTENT_DATA:
464 if (entry->c.ops->ioctl)
465 return entry->c.ops->ioctl(entry,
466 data->file_private_data,
467 file, cmd, arg);
468 break;
469 }
470 return -ENOTTY;
471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
474{
475 struct inode *inode = file->f_dentry->d_inode;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100476 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 struct snd_info_entry *entry;
478
479 data = file->private_data;
480 if (data == NULL)
481 return 0;
482 entry = data->entry;
483 switch (entry->content) {
484 case SNDRV_INFO_CONTENT_DATA:
485 if (entry->c.ops->mmap)
486 return entry->c.ops->mmap(entry,
487 data->file_private_data,
488 inode, file, vma);
489 break;
490 }
491 return -ENXIO;
492}
493
494static struct file_operations snd_info_entry_operations =
495{
Ingo Molnard99e9882006-01-09 16:44:46 +0100496 .owner = THIS_MODULE,
497 .llseek = snd_info_entry_llseek,
498 .read = snd_info_entry_read,
499 .write = snd_info_entry_write,
500 .poll = snd_info_entry_poll,
501 .unlocked_ioctl = snd_info_entry_ioctl,
502 .mmap = snd_info_entry_mmap,
503 .open = snd_info_entry_open,
504 .release = snd_info_entry_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505};
506
507/**
508 * snd_create_proc_entry - create a procfs entry
509 * @name: the name of the proc file
510 * @mode: the file permission bits, S_Ixxx
511 * @parent: the parent proc-directory entry
512 *
513 * Creates a new proc file entry with the given name and permission
514 * on the given directory.
515 *
516 * Returns the pointer of new instance or NULL on failure.
517 */
518static struct proc_dir_entry *snd_create_proc_entry(const char *name, mode_t mode,
519 struct proc_dir_entry *parent)
520{
521 struct proc_dir_entry *p;
522 p = create_proc_entry(name, mode, parent);
523 if (p)
524 snd_info_entry_prepare(p);
525 return p;
526}
527
528int __init snd_info_init(void)
529{
530 struct proc_dir_entry *p;
531
532 p = snd_create_proc_entry("asound", S_IFDIR | S_IRUGO | S_IXUGO, &proc_root);
533 if (p == NULL)
534 return -ENOMEM;
535 snd_proc_root = p;
536#ifdef CONFIG_SND_OSSEMUL
537 {
Takashi Iwai24c1f932005-11-17 13:58:48 +0100538 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
540 return -ENOMEM;
541 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
542 if (snd_info_register(entry) < 0) {
543 snd_info_free_entry(entry);
544 return -ENOMEM;
545 }
546 snd_oss_root = entry;
547 }
548#endif
549#if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
550 {
Takashi Iwai24c1f932005-11-17 13:58:48 +0100551 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", 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_seq_root = entry;
560 }
561#endif
562 snd_info_version_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 snd_minor_info_init();
564 snd_minor_info_oss_init();
565 snd_card_info_init();
566 return 0;
567}
568
569int __exit snd_info_done(void)
570{
571 snd_card_info_done();
572 snd_minor_info_oss_done();
573 snd_minor_info_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 snd_info_version_done();
575 if (snd_proc_root) {
576#if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
Takashi Iwaie28563c2005-12-01 10:42:42 +0100577 snd_info_unregister(snd_seq_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578#endif
579#ifdef CONFIG_SND_OSSEMUL
Takashi Iwaie28563c2005-12-01 10:42:42 +0100580 snd_info_unregister(snd_oss_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581#endif
582 snd_remove_proc_entry(&proc_root, snd_proc_root);
583 }
584 return 0;
585}
586
587/*
588
589 */
590
591
592/*
593 * create a card proc file
594 * called from init.c
595 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100596int snd_info_card_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
598 char str[8];
Takashi Iwai24c1f932005-11-17 13:58:48 +0100599 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 snd_assert(card != NULL, return -ENXIO);
602
603 sprintf(str, "card%i", card->number);
604 if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
605 return -ENOMEM;
606 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
607 if (snd_info_register(entry) < 0) {
608 snd_info_free_entry(entry);
609 return -ENOMEM;
610 }
611 card->proc_root = entry;
612 return 0;
613}
614
615/*
616 * register the card proc file
617 * called from init.c
618 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100619int snd_info_card_register(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 struct proc_dir_entry *p;
622
623 snd_assert(card != NULL, return -ENXIO);
624
625 if (!strcmp(card->id, card->proc_root->name))
626 return 0;
627
628 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
629 if (p == NULL)
630 return -ENOMEM;
631 card->proc_root_link = p;
632 return 0;
633}
634
635/*
636 * de-register the card proc file
637 * called from init.c
638 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100639int snd_info_card_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 snd_assert(card != NULL, return -ENXIO);
642 if (card->proc_root_link) {
643 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
644 card->proc_root_link = NULL;
645 }
646 if (card->proc_root) {
647 snd_info_unregister(card->proc_root);
648 card->proc_root = NULL;
649 }
650 return 0;
651}
652
653
654/**
655 * snd_info_get_line - read one line from the procfs buffer
656 * @buffer: the procfs buffer
657 * @line: the buffer to store
658 * @len: the max. buffer size - 1
659 *
660 * Reads one line from the buffer and stores the string.
661 *
662 * Returns zero if successful, or 1 if error or EOF.
663 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100664int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 int c = -1;
667
668 if (len <= 0 || buffer->stop || buffer->error)
669 return 1;
670 while (--len > 0) {
671 c = *buffer->curr++;
672 if (c == '\n') {
673 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
674 buffer->stop = 1;
675 }
676 break;
677 }
678 *line++ = c;
679 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
680 buffer->stop = 1;
681 break;
682 }
683 }
684 while (c != '\n' && !buffer->stop) {
685 c = *buffer->curr++;
686 if ((buffer->curr - buffer->buffer) >= (long)buffer->size) {
687 buffer->stop = 1;
688 }
689 }
690 *line = '\0';
691 return 0;
692}
693
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200694EXPORT_SYMBOL(snd_info_get_line);
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696/**
Henrik Kretzschmar856def82005-07-08 13:53:42 +0200697 * snd_info_get_str - parse a string token
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 * @dest: the buffer to store the string token
699 * @src: the original string
700 * @len: the max. length of token - 1
701 *
702 * Parses the original string and copy a token to the given
703 * string buffer.
704 *
705 * Returns the updated pointer of the original string so that
706 * it can be used for the next call.
707 */
708char *snd_info_get_str(char *dest, char *src, int len)
709{
710 int c;
711
712 while (*src == ' ' || *src == '\t')
713 src++;
714 if (*src == '"' || *src == '\'') {
715 c = *src++;
716 while (--len > 0 && *src && *src != c) {
717 *dest++ = *src++;
718 }
719 if (*src == c)
720 src++;
721 } else {
722 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
723 *dest++ = *src++;
724 }
725 }
726 *dest = 0;
727 while (*src == ' ' || *src == '\t')
728 src++;
729 return src;
730}
731
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200732EXPORT_SYMBOL(snd_info_get_str);
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734/**
735 * snd_info_create_entry - create an info entry
736 * @name: the proc file name
737 *
738 * Creates an info entry with the given file name and initializes as
739 * the default state.
740 *
741 * Usually called from other functions such as
742 * snd_info_create_card_entry().
743 *
744 * Returns the pointer of the new instance, or NULL on failure.
745 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100746static struct snd_info_entry *snd_info_create_entry(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100748 struct snd_info_entry *entry;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200749 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (entry == NULL)
751 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -0700752 entry->name = kstrdup(name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (entry->name == NULL) {
754 kfree(entry);
755 return NULL;
756 }
757 entry->mode = S_IFREG | S_IRUGO;
758 entry->content = SNDRV_INFO_CONTENT_TEXT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100759 mutex_init(&entry->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return entry;
761}
762
763/**
764 * snd_info_create_module_entry - create an info entry for the given module
765 * @module: the module pointer
766 * @name: the file name
767 * @parent: the parent directory
768 *
769 * Creates a new info entry and assigns it to the given module.
770 *
771 * Returns the pointer of the new instance, or NULL on failure.
772 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100773struct snd_info_entry *snd_info_create_module_entry(struct module * module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 const char *name,
Takashi Iwai24c1f932005-11-17 13:58:48 +0100775 struct snd_info_entry *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100777 struct snd_info_entry *entry = snd_info_create_entry(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (entry) {
779 entry->module = module;
780 entry->parent = parent;
781 }
782 return entry;
783}
784
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200785EXPORT_SYMBOL(snd_info_create_module_entry);
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787/**
788 * snd_info_create_card_entry - create an info entry for the given card
789 * @card: the card instance
790 * @name: the file name
791 * @parent: the parent directory
792 *
793 * Creates a new info entry and assigns it to the given card.
794 *
795 * Returns the pointer of the new instance, or NULL on failure.
796 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100797struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 const char *name,
Takashi Iwai24c1f932005-11-17 13:58:48 +0100799 struct snd_info_entry * parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100801 struct snd_info_entry *entry = snd_info_create_entry(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (entry) {
803 entry->module = card->module;
804 entry->card = card;
805 entry->parent = parent;
806 }
807 return entry;
808}
809
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200810EXPORT_SYMBOL(snd_info_create_card_entry);
811
Takashi Iwai24c1f932005-11-17 13:58:48 +0100812static int snd_info_dev_free_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 snd_info_free_entry(entry);
816 return 0;
817}
818
Takashi Iwai24c1f932005-11-17 13:58:48 +0100819static int snd_info_dev_register_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_register(entry);
823}
824
Takashi Iwai24c1f932005-11-17 13:58:48 +0100825static int snd_info_dev_disconnect_entry(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100827 struct snd_info_entry *entry = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 entry->disconnected = 1;
829 return 0;
830}
831
Takashi Iwai24c1f932005-11-17 13:58:48 +0100832static int snd_info_dev_unregister_entry(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100834 struct snd_info_entry *entry = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return snd_info_unregister(entry);
836}
837
838/**
839 * snd_card_proc_new - create an info entry for the given card
840 * @card: the card instance
841 * @name: the file name
842 * @entryp: the pointer to store the new info entry
843 *
844 * Creates a new info entry and assigns it to the given card.
845 * Unlike snd_info_create_card_entry(), this function registers the
846 * info entry as an ALSA device component, so that it can be
847 * unregistered/released without explicit call.
848 * Also, you don't have to register this entry via snd_info_register(),
849 * since this will be registered by snd_card_register() automatically.
850 *
851 * The parent is assumed as card->proc_root.
852 *
853 * For releasing this entry, use snd_device_free() instead of
854 * snd_info_free_entry().
855 *
856 * Returns zero if successful, or a negative error code on failure.
857 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100858int snd_card_proc_new(struct snd_card *card, const char *name,
859 struct snd_info_entry **entryp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100861 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 .dev_free = snd_info_dev_free_entry,
863 .dev_register = snd_info_dev_register_entry,
864 .dev_disconnect = snd_info_dev_disconnect_entry,
865 .dev_unregister = snd_info_dev_unregister_entry
866 };
Takashi Iwai24c1f932005-11-17 13:58:48 +0100867 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 int err;
869
870 entry = snd_info_create_card_entry(card, name, card->proc_root);
871 if (! entry)
872 return -ENOMEM;
873 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
874 snd_info_free_entry(entry);
875 return err;
876 }
877 if (entryp)
878 *entryp = entry;
879 return 0;
880}
881
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200882EXPORT_SYMBOL(snd_card_proc_new);
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884/**
885 * snd_info_free_entry - release the info entry
886 * @entry: the info entry
887 *
888 * Releases the info entry. Don't call this after registered.
889 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100890void snd_info_free_entry(struct snd_info_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 if (entry == NULL)
893 return;
894 kfree(entry->name);
895 if (entry->private_free)
896 entry->private_free(entry);
897 kfree(entry);
898}
899
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200900EXPORT_SYMBOL(snd_info_free_entry);
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902/**
903 * snd_info_register - register the info entry
904 * @entry: the info entry
905 *
906 * Registers the proc info entry.
907 *
908 * Returns zero if successful, or a negative error code on failure.
909 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100910int snd_info_register(struct snd_info_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 struct proc_dir_entry *root, *p = NULL;
913
914 snd_assert(entry != NULL, return -ENXIO);
915 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100916 mutex_lock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 p = snd_create_proc_entry(entry->name, entry->mode, root);
918 if (!p) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100919 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return -ENOMEM;
921 }
922 p->owner = entry->module;
923 if (!S_ISDIR(entry->mode))
924 p->proc_fops = &snd_info_entry_operations;
925 p->size = entry->size;
926 p->data = entry;
927 entry->p = p;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100928 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return 0;
930}
931
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200932EXPORT_SYMBOL(snd_info_register);
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934/**
935 * snd_info_unregister - de-register the info entry
936 * @entry: the info entry
937 *
938 * De-registers the info entry and releases the instance.
939 *
940 * Returns zero if successful, or a negative error code on failure.
941 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100942int snd_info_unregister(struct snd_info_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 struct proc_dir_entry *root;
945
Takashi Iwaie28563c2005-12-01 10:42:42 +0100946 if (! entry)
947 return 0;
Henrik Kretzschmar856def82005-07-08 13:53:42 +0200948 snd_assert(entry->p != NULL, return -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
950 snd_assert(root, return -ENXIO);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100951 mutex_lock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 snd_remove_proc_entry(root, entry->p);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100953 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 snd_info_free_entry(entry);
955 return 0;
956}
957
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200958EXPORT_SYMBOL(snd_info_unregister);
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960/*
961
962 */
963
Takashi Iwai24c1f932005-11-17 13:58:48 +0100964static struct snd_info_entry *snd_info_version_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Takashi Iwai24c1f932005-11-17 13:58:48 +0100966static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 snd_iprintf(buffer,
969 "Advanced Linux Sound Architecture Driver Version "
970 CONFIG_SND_VERSION CONFIG_SND_DATE ".\n"
971 );
972}
973
974static int __init snd_info_version_init(void)
975{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100976 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
979 if (entry == NULL)
980 return -ENOMEM;
981 entry->c.text.read_size = 256;
982 entry->c.text.read = snd_info_version_read;
983 if (snd_info_register(entry) < 0) {
984 snd_info_free_entry(entry);
985 return -ENOMEM;
986 }
987 snd_info_version_entry = entry;
988 return 0;
989}
990
991static int __exit snd_info_version_done(void)
992{
993 if (snd_info_version_entry)
994 snd_info_unregister(snd_info_version_entry);
995 return 0;
996}
997
998#endif /* CONFIG_PROC_FS */