blob: 4188f76add75562d22bfe3a579c433eee76482ad [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/time.h>
25#include <linux/smp_lock.h>
Paulo Marques543537b2005-06-23 00:09:02 -070026#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/core.h>
28#include <sound/minors.h>
29#include <sound/info.h>
30#include <sound/version.h>
31#include <linux/proc_fs.h>
32#include <linux/devfs_fs_kernel.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010033#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#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
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010071static DEFINE_MUTEX(info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
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
Takashi Iwai7e4eeec2006-04-28 15:13:40 +020084/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/**
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 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100111int snd_iprintf(struct snd_info_buffer *buffer, char *fmt,...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 va_list args;
114 int len, res;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200115 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Takashi Iwaif001c3a2006-04-28 15:13:41 +0200117 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (buffer->stop || buffer->error)
119 return 0;
120 len = buffer->len - buffer->size;
121 va_start(args, fmt);
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200122 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200131 va_end(args);
132
133 if (err < 0)
134 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 buffer->curr += res;
136 buffer->size += res;
137 return res;
138}
139
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200140EXPORT_SYMBOL(snd_iprintf);
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/*
143
144 */
145
146static struct proc_dir_entry *snd_proc_root = NULL;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100147struct snd_info_entry *snd_seq_root = NULL;
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200148EXPORT_SYMBOL(snd_seq_root);
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai24c1f932005-11-17 13:58:48 +0100151struct snd_info_entry *snd_oss_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#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{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100168 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 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) {
178 case 0: /* SEEK_SET */
179 file->f_pos = offset;
180 ret = file->f_pos;
181 goto out;
182 case 1: /* SEEK_CUR */
183 file->f_pos += offset;
184 ret = file->f_pos;
185 goto out;
186 case 2: /* SEEK_END */
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{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100210 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 struct snd_info_entry *entry;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100212 struct snd_info_buffer *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 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{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100251 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 struct snd_info_entry *entry;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100253 struct snd_info_buffer *buf;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200254 ssize_t size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 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;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200270 mutex_unlock(&entry->access);
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return -EFAULT;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200280 }
281 buf->size = pos + count;
282 mutex_unlock(&entry->access);
283 size = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 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{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100299 struct snd_info_entry *entry;
300 struct snd_info_private_data *data;
301 struct snd_info_buffer *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 struct proc_dir_entry *p;
303 int mode, err;
304
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100305 mutex_lock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 p = PDE(inode);
Takashi Iwai24c1f932005-11-17 13:58:48 +0100307 entry = p == NULL ? NULL : (struct snd_info_entry *)p->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (entry == NULL || entry->disconnected) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100309 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 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) {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200318 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 entry->c.ops->read == NULL)) {
320 err = -ENODEV;
321 goto __error;
322 }
323 }
324 if (mode == O_WRONLY || mode == O_RDWR) {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200325 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 entry->c.ops->write == NULL)) {
327 err = -ENODEV;
328 goto __error;
329 }
330 }
Takashi Iwaica2c0962005-09-09 14:20:23 +0200331 data = kzalloc(sizeof(*data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 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) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200340 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200341 if (buffer == NULL)
342 goto __nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 data->rbuffer = buffer;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200344 buffer->len = PAGE_SIZE;
345 buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
346 if (buffer->buffer == NULL)
347 goto __nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349 if (mode == O_WRONLY || mode == O_RDWR) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200350 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200351 if (buffer == NULL)
352 goto __nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 data->wbuffer = buffer;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200354 buffer->len = PAGE_SIZE;
355 buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
356 if (buffer->buffer == NULL)
357 goto __nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
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;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100371 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (entry->content == SNDRV_INFO_CONTENT_TEXT &&
373 (mode == O_RDONLY || mode == O_RDWR)) {
374 if (entry->c.text.read) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100375 mutex_lock(&entry->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 entry->c.text.read(entry, data->rbuffer);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100377 mutex_unlock(&entry->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379 }
380 return 0;
381
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200382 __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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 __error:
394 module_put(entry->module);
395 __error1:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100396 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return err;
398}
399
400static int snd_info_entry_release(struct inode *inode, struct file *file)
401{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100402 struct snd_info_entry *entry;
403 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 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:
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200411 if (data->rbuffer) {
412 kfree(data->rbuffer->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 kfree(data->rbuffer);
414 }
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200415 if (data->wbuffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 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 }
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200424 kfree(data->wbuffer->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 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{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100441 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 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
Ingo Molnard99e9882006-01-09 16:44:46 +0100465static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
466 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100468 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
487{
488 struct inode *inode = file->f_dentry->d_inode;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100489 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 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{
Ingo Molnard99e9882006-01-09 16:44:46 +0100509 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518};
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 {
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, "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 {
Takashi Iwai24c1f932005-11-17 13:58:48 +0100564 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 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();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 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();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 snd_info_version_done();
588 if (snd_proc_root) {
589#if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
Takashi Iwaie28563c2005-12-01 10:42:42 +0100590 snd_info_unregister(snd_seq_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591#endif
592#ifdef CONFIG_SND_OSSEMUL
Takashi Iwaie28563c2005-12-01 10:42:42 +0100593 snd_info_unregister(snd_oss_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594#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 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100609int snd_info_card_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 char str[8];
Takashi Iwai24c1f932005-11-17 13:58:48 +0100612 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
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 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100632int snd_info_card_register(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
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 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100652int snd_info_card_free(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 snd_assert(card != NULL, return -ENXIO);
655 if (card->proc_root_link) {
656 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
657 card->proc_root_link = NULL;
658 }
659 if (card->proc_root) {
660 snd_info_unregister(card->proc_root);
661 card->proc_root = NULL;
662 }
663 return 0;
664}
665
666
667/**
668 * snd_info_get_line - read one line from the procfs buffer
669 * @buffer: the procfs buffer
670 * @line: the buffer to store
671 * @len: the max. buffer size - 1
672 *
673 * Reads one line from the buffer and stores the string.
674 *
675 * Returns zero if successful, or 1 if error or EOF.
676 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100677int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 int c = -1;
680
681 if (len <= 0 || buffer->stop || buffer->error)
682 return 1;
683 while (--len > 0) {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200684 c = buffer->buffer[buffer->curr++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (c == '\n') {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200686 if (buffer->curr >= buffer->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 buffer->stop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 break;
689 }
690 *line++ = c;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200691 if (buffer->curr >= buffer->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 buffer->stop = 1;
693 break;
694 }
695 }
696 while (c != '\n' && !buffer->stop) {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200697 c = buffer->buffer[buffer->curr++];
698 if (buffer->curr >= buffer->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 buffer->stop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701 *line = '\0';
702 return 0;
703}
704
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200705EXPORT_SYMBOL(snd_info_get_line);
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707/**
Henrik Kretzschmar856def82005-07-08 13:53:42 +0200708 * snd_info_get_str - parse a string token
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 * @dest: the buffer to store the string token
710 * @src: the original string
711 * @len: the max. length of token - 1
712 *
713 * Parses the original string and copy a token to the given
714 * string buffer.
715 *
716 * Returns the updated pointer of the original string so that
717 * it can be used for the next call.
718 */
719char *snd_info_get_str(char *dest, char *src, int len)
720{
721 int c;
722
723 while (*src == ' ' || *src == '\t')
724 src++;
725 if (*src == '"' || *src == '\'') {
726 c = *src++;
727 while (--len > 0 && *src && *src != c) {
728 *dest++ = *src++;
729 }
730 if (*src == c)
731 src++;
732 } else {
733 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
734 *dest++ = *src++;
735 }
736 }
737 *dest = 0;
738 while (*src == ' ' || *src == '\t')
739 src++;
740 return src;
741}
742
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200743EXPORT_SYMBOL(snd_info_get_str);
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745/**
746 * snd_info_create_entry - create an info entry
747 * @name: the proc file name
748 *
749 * Creates an info entry with the given file name and initializes as
750 * the default state.
751 *
752 * Usually called from other functions such as
753 * snd_info_create_card_entry().
754 *
755 * Returns the pointer of the new instance, or NULL on failure.
756 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100757static struct snd_info_entry *snd_info_create_entry(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100759 struct snd_info_entry *entry;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200760 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (entry == NULL)
762 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -0700763 entry->name = kstrdup(name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (entry->name == NULL) {
765 kfree(entry);
766 return NULL;
767 }
768 entry->mode = S_IFREG | S_IRUGO;
769 entry->content = SNDRV_INFO_CONTENT_TEXT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100770 mutex_init(&entry->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return entry;
772}
773
774/**
775 * snd_info_create_module_entry - create an info entry for the given module
776 * @module: the module pointer
777 * @name: the file name
778 * @parent: the parent directory
779 *
780 * Creates a new info entry and assigns it to the given module.
781 *
782 * Returns the pointer of the new instance, or NULL on failure.
783 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100784struct snd_info_entry *snd_info_create_module_entry(struct module * module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 const char *name,
Takashi Iwai24c1f932005-11-17 13:58:48 +0100786 struct snd_info_entry *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100788 struct snd_info_entry *entry = snd_info_create_entry(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (entry) {
790 entry->module = module;
791 entry->parent = parent;
792 }
793 return entry;
794}
795
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200796EXPORT_SYMBOL(snd_info_create_module_entry);
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798/**
799 * snd_info_create_card_entry - create an info entry for the given card
800 * @card: the card instance
801 * @name: the file name
802 * @parent: the parent directory
803 *
804 * Creates a new info entry and assigns it to the given card.
805 *
806 * Returns the pointer of the new instance, or NULL on failure.
807 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100808struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 const char *name,
Takashi Iwai24c1f932005-11-17 13:58:48 +0100810 struct snd_info_entry * parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100812 struct snd_info_entry *entry = snd_info_create_entry(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (entry) {
814 entry->module = card->module;
815 entry->card = card;
816 entry->parent = parent;
817 }
818 return entry;
819}
820
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200821EXPORT_SYMBOL(snd_info_create_card_entry);
822
Takashi Iwai24c1f932005-11-17 13:58:48 +0100823static int snd_info_dev_free_entry(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100825 struct snd_info_entry *entry = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 snd_info_free_entry(entry);
827 return 0;
828}
829
Takashi Iwai24c1f932005-11-17 13:58:48 +0100830static int snd_info_dev_register_entry(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100832 struct snd_info_entry *entry = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return snd_info_register(entry);
834}
835
Takashi Iwai24c1f932005-11-17 13:58:48 +0100836static int snd_info_dev_disconnect_entry(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100838 struct snd_info_entry *entry = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 entry->disconnected = 1;
840 return 0;
841}
842
Takashi Iwai24c1f932005-11-17 13:58:48 +0100843static int snd_info_dev_unregister_entry(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100845 struct snd_info_entry *entry = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return snd_info_unregister(entry);
847}
848
849/**
850 * snd_card_proc_new - create an info entry for the given card
851 * @card: the card instance
852 * @name: the file name
853 * @entryp: the pointer to store the new info entry
854 *
855 * Creates a new info entry and assigns it to the given card.
856 * Unlike snd_info_create_card_entry(), this function registers the
857 * info entry as an ALSA device component, so that it can be
858 * unregistered/released without explicit call.
859 * Also, you don't have to register this entry via snd_info_register(),
860 * since this will be registered by snd_card_register() automatically.
861 *
862 * The parent is assumed as card->proc_root.
863 *
864 * For releasing this entry, use snd_device_free() instead of
865 * snd_info_free_entry().
866 *
867 * Returns zero if successful, or a negative error code on failure.
868 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100869int snd_card_proc_new(struct snd_card *card, const char *name,
870 struct snd_info_entry **entryp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100872 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 .dev_free = snd_info_dev_free_entry,
874 .dev_register = snd_info_dev_register_entry,
875 .dev_disconnect = snd_info_dev_disconnect_entry,
876 .dev_unregister = snd_info_dev_unregister_entry
877 };
Takashi Iwai24c1f932005-11-17 13:58:48 +0100878 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 int err;
880
881 entry = snd_info_create_card_entry(card, name, card->proc_root);
882 if (! entry)
883 return -ENOMEM;
884 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
885 snd_info_free_entry(entry);
886 return err;
887 }
888 if (entryp)
889 *entryp = entry;
890 return 0;
891}
892
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200893EXPORT_SYMBOL(snd_card_proc_new);
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895/**
896 * snd_info_free_entry - release the info entry
897 * @entry: the info entry
898 *
899 * Releases the info entry. Don't call this after registered.
900 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100901void snd_info_free_entry(struct snd_info_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 if (entry == NULL)
904 return;
905 kfree(entry->name);
906 if (entry->private_free)
907 entry->private_free(entry);
908 kfree(entry);
909}
910
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200911EXPORT_SYMBOL(snd_info_free_entry);
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913/**
914 * snd_info_register - register the info entry
915 * @entry: the info entry
916 *
917 * Registers the proc info entry.
918 *
919 * Returns zero if successful, or a negative error code on failure.
920 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100921int snd_info_register(struct snd_info_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 struct proc_dir_entry *root, *p = NULL;
924
925 snd_assert(entry != NULL, return -ENXIO);
926 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100927 mutex_lock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 p = snd_create_proc_entry(entry->name, entry->mode, root);
929 if (!p) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100930 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return -ENOMEM;
932 }
933 p->owner = entry->module;
934 if (!S_ISDIR(entry->mode))
935 p->proc_fops = &snd_info_entry_operations;
936 p->size = entry->size;
937 p->data = entry;
938 entry->p = p;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100939 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return 0;
941}
942
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200943EXPORT_SYMBOL(snd_info_register);
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945/**
946 * snd_info_unregister - de-register the info entry
947 * @entry: the info entry
948 *
949 * De-registers the info entry and releases the instance.
950 *
951 * Returns zero if successful, or a negative error code on failure.
952 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100953int snd_info_unregister(struct snd_info_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 struct proc_dir_entry *root;
956
Takashi Iwaie28563c2005-12-01 10:42:42 +0100957 if (! entry)
958 return 0;
Henrik Kretzschmar856def82005-07-08 13:53:42 +0200959 snd_assert(entry->p != NULL, return -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
961 snd_assert(root, return -ENXIO);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100962 mutex_lock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 snd_remove_proc_entry(root, entry->p);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100964 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 snd_info_free_entry(entry);
966 return 0;
967}
968
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200969EXPORT_SYMBOL(snd_info_unregister);
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971/*
972
973 */
974
Takashi Iwai24c1f932005-11-17 13:58:48 +0100975static struct snd_info_entry *snd_info_version_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Takashi Iwai24c1f932005-11-17 13:58:48 +0100977static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 snd_iprintf(buffer,
980 "Advanced Linux Sound Architecture Driver Version "
981 CONFIG_SND_VERSION CONFIG_SND_DATE ".\n"
982 );
983}
984
985static int __init snd_info_version_init(void)
986{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100987 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
990 if (entry == NULL)
991 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 entry->c.text.read = snd_info_version_read;
993 if (snd_info_register(entry) < 0) {
994 snd_info_free_entry(entry);
995 return -ENOMEM;
996 }
997 snd_info_version_entry = entry;
998 return 0;
999}
1000
1001static int __exit snd_info_version_done(void)
1002{
1003 if (snd_info_version_entry)
1004 snd_info_unregister(snd_info_version_entry);
1005 return 0;
1006}
1007
1008#endif /* CONFIG_PROC_FS */