blob: 1ffd29bb4cd06c25759d7959005d19e1ab1e0427 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Information interface for ALSA driver
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010032#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <stdarg.h>
34
35/*
36 *
37 */
38
Takashi Iwaie28563c2005-12-01 10:42:42 +010039#ifdef CONFIG_PROC_FS
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041int snd_info_check_reserved_words(const char *str)
42{
43 static char *reserved[] =
44 {
45 "version",
46 "meminfo",
47 "memdebug",
48 "detect",
49 "devices",
50 "oss",
51 "cards",
52 "timers",
53 "synth",
54 "pcm",
55 "seq",
56 NULL
57 };
58 char **xstr = reserved;
59
60 while (*xstr) {
61 if (!strcmp(*xstr, str))
62 return 0;
63 xstr++;
64 }
65 if (!strncmp(str, "card", 4))
66 return 0;
67 return 1;
68}
69
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010070static DEFINE_MUTEX(info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Takashi Iwai24c1f932005-11-17 13:58:48 +010072struct snd_info_private_data {
73 struct snd_info_buffer *rbuffer;
74 struct snd_info_buffer *wbuffer;
75 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 void *file_private_data;
Takashi Iwai24c1f932005-11-17 13:58:48 +010077};
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79static int snd_info_version_init(void);
80static int snd_info_version_done(void);
Takashi Iwai746d4a02006-06-23 14:37:59 +020081static void snd_info_disconnect(struct snd_info_entry *entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
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 (;;) {
Takashi Iwaidbedca32006-10-18 19:09:46 +0200123 va_list ap;
124 va_copy(ap, args);
125 res = vsnprintf(buffer->buffer + buffer->curr, len, fmt, ap);
126 va_end(ap);
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200127 if (res < len)
128 break;
129 err = resize_info_buffer(buffer, buffer->len + PAGE_SIZE);
130 if (err < 0)
131 break;
132 len = buffer->len - buffer->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200134 va_end(args);
135
136 if (err < 0)
137 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 buffer->curr += res;
139 buffer->size += res;
140 return res;
141}
142
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200143EXPORT_SYMBOL(snd_iprintf);
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
146
147 */
148
Takashi Iwai6581f4e2006-05-17 17:14:51 +0200149static struct proc_dir_entry *snd_proc_root;
150struct snd_info_entry *snd_seq_root;
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200151EXPORT_SYMBOL(snd_seq_root);
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai6581f4e2006-05-17 17:14:51 +0200154struct snd_info_entry *snd_oss_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#endif
156
157static inline void snd_info_entry_prepare(struct proc_dir_entry *de)
158{
159 de->owner = THIS_MODULE;
160}
161
162static void snd_remove_proc_entry(struct proc_dir_entry *parent,
163 struct proc_dir_entry *de)
164{
165 if (de)
166 remove_proc_entry(de->name, parent);
167}
168
169static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
170{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100171 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct snd_info_entry *entry;
173 loff_t ret;
174
175 data = file->private_data;
176 entry = data->entry;
177 lock_kernel();
178 switch (entry->content) {
179 case SNDRV_INFO_CONTENT_TEXT:
180 switch (orig) {
Josef 'Jeff' Sipeke6f8f102006-09-21 11:31:58 +0200181 case SEEK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 file->f_pos = offset;
183 ret = file->f_pos;
184 goto out;
Josef 'Jeff' Sipeke6f8f102006-09-21 11:31:58 +0200185 case SEEK_CUR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 file->f_pos += offset;
187 ret = file->f_pos;
188 goto out;
Josef 'Jeff' Sipeke6f8f102006-09-21 11:31:58 +0200189 case SEEK_END:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 default:
191 ret = -EINVAL;
192 goto out;
193 }
194 break;
195 case SNDRV_INFO_CONTENT_DATA:
196 if (entry->c.ops->llseek) {
197 ret = entry->c.ops->llseek(entry,
198 data->file_private_data,
199 file, offset, orig);
200 goto out;
201 }
202 break;
203 }
204 ret = -ENXIO;
205out:
206 unlock_kernel();
207 return ret;
208}
209
210static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
211 size_t count, loff_t * offset)
212{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100213 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 struct snd_info_entry *entry;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100215 struct snd_info_buffer *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 size_t size = 0;
217 loff_t pos;
218
219 data = file->private_data;
220 snd_assert(data != NULL, return -ENXIO);
221 pos = *offset;
222 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
223 return -EIO;
224 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
225 return -EIO;
226 entry = data->entry;
227 switch (entry->content) {
228 case SNDRV_INFO_CONTENT_TEXT:
229 buf = data->rbuffer;
230 if (buf == NULL)
231 return -EIO;
232 if (pos >= buf->size)
233 return 0;
234 size = buf->size - pos;
235 size = min(count, size);
236 if (copy_to_user(buffer, buf->buffer + pos, size))
237 return -EFAULT;
238 break;
239 case SNDRV_INFO_CONTENT_DATA:
240 if (entry->c.ops->read)
241 size = entry->c.ops->read(entry,
242 data->file_private_data,
243 file, buffer, count, pos);
244 break;
245 }
246 if ((ssize_t) size > 0)
247 *offset = pos + size;
248 return size;
249}
250
251static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
252 size_t count, loff_t * offset)
253{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100254 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 struct snd_info_entry *entry;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100256 struct snd_info_buffer *buf;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200257 ssize_t size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 loff_t pos;
259
260 data = file->private_data;
261 snd_assert(data != NULL, return -ENXIO);
262 entry = data->entry;
263 pos = *offset;
264 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
265 return -EIO;
266 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
267 return -EIO;
268 switch (entry->content) {
269 case SNDRV_INFO_CONTENT_TEXT:
270 buf = data->wbuffer;
271 if (buf == NULL)
272 return -EIO;
Clemens Ladischf4a747f2006-05-02 15:33:25 +0200273 mutex_lock(&entry->access);
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200274 if (pos + count >= buf->len) {
275 if (resize_info_buffer(buf, pos + count)) {
276 mutex_unlock(&entry->access);
277 return -ENOMEM;
278 }
279 }
280 if (copy_from_user(buf->buffer + pos, buffer, count)) {
281 mutex_unlock(&entry->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return -EFAULT;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200283 }
284 buf->size = pos + count;
285 mutex_unlock(&entry->access);
286 size = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 break;
288 case SNDRV_INFO_CONTENT_DATA:
289 if (entry->c.ops->write)
290 size = entry->c.ops->write(entry,
291 data->file_private_data,
292 file, buffer, count, pos);
293 break;
294 }
295 if ((ssize_t) size > 0)
296 *offset = pos + size;
297 return size;
298}
299
300static int snd_info_entry_open(struct inode *inode, struct file *file)
301{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100302 struct snd_info_entry *entry;
303 struct snd_info_private_data *data;
304 struct snd_info_buffer *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct proc_dir_entry *p;
306 int mode, err;
307
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100308 mutex_lock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 p = PDE(inode);
Takashi Iwai24c1f932005-11-17 13:58:48 +0100310 entry = p == NULL ? NULL : (struct snd_info_entry *)p->data;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200311 if (entry == NULL || ! entry->p) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100312 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return -ENODEV;
314 }
315 if (!try_module_get(entry->module)) {
316 err = -EFAULT;
317 goto __error1;
318 }
319 mode = file->f_flags & O_ACCMODE;
320 if (mode == O_RDONLY || mode == O_RDWR) {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200321 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 entry->c.ops->read == NULL)) {
323 err = -ENODEV;
324 goto __error;
325 }
326 }
327 if (mode == O_WRONLY || mode == O_RDWR) {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200328 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 entry->c.ops->write == NULL)) {
330 err = -ENODEV;
331 goto __error;
332 }
333 }
Takashi Iwaica2c0962005-09-09 14:20:23 +0200334 data = kzalloc(sizeof(*data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (data == NULL) {
336 err = -ENOMEM;
337 goto __error;
338 }
339 data->entry = entry;
340 switch (entry->content) {
341 case SNDRV_INFO_CONTENT_TEXT:
342 if (mode == O_RDONLY || mode == O_RDWR) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200343 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200344 if (buffer == NULL)
345 goto __nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 data->rbuffer = buffer;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200347 buffer->len = PAGE_SIZE;
348 buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
349 if (buffer->buffer == NULL)
350 goto __nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352 if (mode == O_WRONLY || mode == O_RDWR) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200353 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200354 if (buffer == NULL)
355 goto __nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 data->wbuffer = buffer;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200357 buffer->len = PAGE_SIZE;
358 buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
359 if (buffer->buffer == NULL)
360 goto __nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362 break;
363 case SNDRV_INFO_CONTENT_DATA: /* data */
364 if (entry->c.ops->open) {
365 if ((err = entry->c.ops->open(entry, mode,
366 &data->file_private_data)) < 0) {
367 kfree(data);
368 goto __error;
369 }
370 }
371 break;
372 }
373 file->private_data = data;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100374 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (entry->content == SNDRV_INFO_CONTENT_TEXT &&
376 (mode == O_RDONLY || mode == O_RDWR)) {
377 if (entry->c.text.read) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100378 mutex_lock(&entry->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 entry->c.text.read(entry, data->rbuffer);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100380 mutex_unlock(&entry->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382 }
383 return 0;
384
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200385 __nomem:
386 if (data->rbuffer) {
387 kfree(data->rbuffer->buffer);
388 kfree(data->rbuffer);
389 }
390 if (data->wbuffer) {
391 kfree(data->wbuffer->buffer);
392 kfree(data->wbuffer);
393 }
394 kfree(data);
395 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 __error:
397 module_put(entry->module);
398 __error1:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100399 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return err;
401}
402
403static int snd_info_entry_release(struct inode *inode, struct file *file)
404{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100405 struct snd_info_entry *entry;
406 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 int mode;
408
409 mode = file->f_flags & O_ACCMODE;
410 data = file->private_data;
411 entry = data->entry;
412 switch (entry->content) {
413 case SNDRV_INFO_CONTENT_TEXT:
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200414 if (data->rbuffer) {
415 kfree(data->rbuffer->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 kfree(data->rbuffer);
417 }
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200418 if (data->wbuffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (entry->c.text.write) {
420 entry->c.text.write(entry, data->wbuffer);
421 if (data->wbuffer->error) {
422 snd_printk(KERN_WARNING "data write error to %s (%i)\n",
423 entry->name,
424 data->wbuffer->error);
425 }
426 }
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200427 kfree(data->wbuffer->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 kfree(data->wbuffer);
429 }
430 break;
431 case SNDRV_INFO_CONTENT_DATA:
432 if (entry->c.ops->release)
433 entry->c.ops->release(entry, mode,
434 data->file_private_data);
435 break;
436 }
437 module_put(entry->module);
438 kfree(data);
439 return 0;
440}
441
442static unsigned int snd_info_entry_poll(struct file *file, poll_table * wait)
443{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100444 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 struct snd_info_entry *entry;
446 unsigned int mask;
447
448 data = file->private_data;
449 if (data == NULL)
450 return 0;
451 entry = data->entry;
452 mask = 0;
453 switch (entry->content) {
454 case SNDRV_INFO_CONTENT_DATA:
455 if (entry->c.ops->poll)
456 return entry->c.ops->poll(entry,
457 data->file_private_data,
458 file, wait);
459 if (entry->c.ops->read)
460 mask |= POLLIN | POLLRDNORM;
461 if (entry->c.ops->write)
462 mask |= POLLOUT | POLLWRNORM;
463 break;
464 }
465 return mask;
466}
467
Ingo Molnard99e9882006-01-09 16:44:46 +0100468static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
469 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
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->ioctl)
481 return entry->c.ops->ioctl(entry,
482 data->file_private_data,
483 file, cmd, arg);
484 break;
485 }
486 return -ENOTTY;
487}
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
490{
Josef Sipek7bc56322006-12-08 02:37:40 -0800491 struct inode *inode = file->f_path.dentry->d_inode;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100492 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct snd_info_entry *entry;
494
495 data = file->private_data;
496 if (data == NULL)
497 return 0;
498 entry = data->entry;
499 switch (entry->content) {
500 case SNDRV_INFO_CONTENT_DATA:
501 if (entry->c.ops->mmap)
502 return entry->c.ops->mmap(entry,
503 data->file_private_data,
504 inode, file, vma);
505 break;
506 }
507 return -ENXIO;
508}
509
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800510static const struct file_operations snd_info_entry_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Ingo Molnard99e9882006-01-09 16:44:46 +0100512 .owner = THIS_MODULE,
513 .llseek = snd_info_entry_llseek,
514 .read = snd_info_entry_read,
515 .write = snd_info_entry_write,
516 .poll = snd_info_entry_poll,
517 .unlocked_ioctl = snd_info_entry_ioctl,
518 .mmap = snd_info_entry_mmap,
519 .open = snd_info_entry_open,
520 .release = snd_info_entry_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521};
522
523/**
524 * snd_create_proc_entry - create a procfs entry
525 * @name: the name of the proc file
526 * @mode: the file permission bits, S_Ixxx
527 * @parent: the parent proc-directory entry
528 *
529 * Creates a new proc file entry with the given name and permission
530 * on the given directory.
531 *
532 * Returns the pointer of new instance or NULL on failure.
533 */
534static struct proc_dir_entry *snd_create_proc_entry(const char *name, mode_t mode,
535 struct proc_dir_entry *parent)
536{
537 struct proc_dir_entry *p;
538 p = create_proc_entry(name, mode, parent);
539 if (p)
540 snd_info_entry_prepare(p);
541 return p;
542}
543
544int __init snd_info_init(void)
545{
546 struct proc_dir_entry *p;
547
548 p = snd_create_proc_entry("asound", S_IFDIR | S_IRUGO | S_IXUGO, &proc_root);
549 if (p == NULL)
550 return -ENOMEM;
551 snd_proc_root = p;
552#ifdef CONFIG_SND_OSSEMUL
553 {
Takashi Iwai24c1f932005-11-17 13:58:48 +0100554 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
556 return -ENOMEM;
557 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
558 if (snd_info_register(entry) < 0) {
559 snd_info_free_entry(entry);
560 return -ENOMEM;
561 }
562 snd_oss_root = entry;
563 }
564#endif
565#if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
566 {
Takashi Iwai24c1f932005-11-17 13:58:48 +0100567 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL)
569 return -ENOMEM;
570 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
571 if (snd_info_register(entry) < 0) {
572 snd_info_free_entry(entry);
573 return -ENOMEM;
574 }
575 snd_seq_root = entry;
576 }
577#endif
578 snd_info_version_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 snd_minor_info_init();
580 snd_minor_info_oss_init();
581 snd_card_info_init();
582 return 0;
583}
584
585int __exit snd_info_done(void)
586{
587 snd_card_info_done();
588 snd_minor_info_oss_done();
589 snd_minor_info_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 snd_info_version_done();
591 if (snd_proc_root) {
592#if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
Takashi Iwai746d4a02006-06-23 14:37:59 +0200593 snd_info_free_entry(snd_seq_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594#endif
595#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai746d4a02006-06-23 14:37:59 +0200596 snd_info_free_entry(snd_oss_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597#endif
598 snd_remove_proc_entry(&proc_root, snd_proc_root);
599 }
600 return 0;
601}
602
603/*
604
605 */
606
607
608/*
609 * create a card proc file
610 * called from init.c
611 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100612int snd_info_card_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 char str[8];
Takashi Iwai24c1f932005-11-17 13:58:48 +0100615 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 snd_assert(card != NULL, return -ENXIO);
618
619 sprintf(str, "card%i", card->number);
620 if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
621 return -ENOMEM;
622 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
623 if (snd_info_register(entry) < 0) {
624 snd_info_free_entry(entry);
625 return -ENOMEM;
626 }
627 card->proc_root = entry;
628 return 0;
629}
630
631/*
632 * register the card proc file
633 * called from init.c
634 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100635int snd_info_card_register(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 struct proc_dir_entry *p;
638
639 snd_assert(card != NULL, return -ENXIO);
640
641 if (!strcmp(card->id, card->proc_root->name))
642 return 0;
643
644 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
645 if (p == NULL)
646 return -ENOMEM;
647 card->proc_root_link = p;
648 return 0;
649}
650
651/*
652 * de-register the card proc file
653 * called from init.c
654 */
Takashi Iwai746d4a02006-06-23 14:37:59 +0200655void snd_info_card_disconnect(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Takashi Iwai746d4a02006-06-23 14:37:59 +0200657 snd_assert(card != NULL, return);
658 mutex_lock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (card->proc_root_link) {
660 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
661 card->proc_root_link = NULL;
662 }
Takashi Iwai746d4a02006-06-23 14:37:59 +0200663 if (card->proc_root)
664 snd_info_disconnect(card->proc_root);
665 mutex_unlock(&info_mutex);
666}
667
668/*
669 * release the card proc file resources
670 * called from init.c
671 */
672int snd_info_card_free(struct snd_card *card)
673{
674 snd_assert(card != NULL, return -ENXIO);
675 snd_info_free_entry(card->proc_root);
676 card->proc_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return 0;
678}
679
680
681/**
682 * snd_info_get_line - read one line from the procfs buffer
683 * @buffer: the procfs buffer
684 * @line: the buffer to store
685 * @len: the max. buffer size - 1
686 *
687 * Reads one line from the buffer and stores the string.
688 *
689 * Returns zero if successful, or 1 if error or EOF.
690 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100691int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 int c = -1;
694
695 if (len <= 0 || buffer->stop || buffer->error)
696 return 1;
697 while (--len > 0) {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200698 c = buffer->buffer[buffer->curr++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (c == '\n') {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200700 if (buffer->curr >= buffer->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 buffer->stop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 break;
703 }
704 *line++ = c;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200705 if (buffer->curr >= buffer->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 buffer->stop = 1;
707 break;
708 }
709 }
710 while (c != '\n' && !buffer->stop) {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200711 c = buffer->buffer[buffer->curr++];
712 if (buffer->curr >= buffer->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 buffer->stop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 *line = '\0';
716 return 0;
717}
718
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200719EXPORT_SYMBOL(snd_info_get_line);
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721/**
Henrik Kretzschmar856def82005-07-08 13:53:42 +0200722 * snd_info_get_str - parse a string token
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 * @dest: the buffer to store the string token
724 * @src: the original string
725 * @len: the max. length of token - 1
726 *
727 * Parses the original string and copy a token to the given
728 * string buffer.
729 *
730 * Returns the updated pointer of the original string so that
731 * it can be used for the next call.
732 */
733char *snd_info_get_str(char *dest, char *src, int len)
734{
735 int c;
736
737 while (*src == ' ' || *src == '\t')
738 src++;
739 if (*src == '"' || *src == '\'') {
740 c = *src++;
741 while (--len > 0 && *src && *src != c) {
742 *dest++ = *src++;
743 }
744 if (*src == c)
745 src++;
746 } else {
747 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
748 *dest++ = *src++;
749 }
750 }
751 *dest = 0;
752 while (*src == ' ' || *src == '\t')
753 src++;
754 return src;
755}
756
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200757EXPORT_SYMBOL(snd_info_get_str);
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759/**
760 * snd_info_create_entry - create an info entry
761 * @name: the proc file name
762 *
763 * Creates an info entry with the given file name and initializes as
764 * the default state.
765 *
766 * Usually called from other functions such as
767 * snd_info_create_card_entry().
768 *
769 * Returns the pointer of the new instance, or NULL on failure.
770 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100771static struct snd_info_entry *snd_info_create_entry(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100773 struct snd_info_entry *entry;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200774 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (entry == NULL)
776 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -0700777 entry->name = kstrdup(name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (entry->name == NULL) {
779 kfree(entry);
780 return NULL;
781 }
782 entry->mode = S_IFREG | S_IRUGO;
783 entry->content = SNDRV_INFO_CONTENT_TEXT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100784 mutex_init(&entry->access);
Takashi Iwai746d4a02006-06-23 14:37:59 +0200785 INIT_LIST_HEAD(&entry->children);
786 INIT_LIST_HEAD(&entry->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return entry;
788}
789
790/**
791 * snd_info_create_module_entry - create an info entry for the given module
792 * @module: the module pointer
793 * @name: the file name
794 * @parent: the parent directory
795 *
796 * Creates a new info entry and assigns it to the given module.
797 *
798 * Returns the pointer of the new instance, or NULL on failure.
799 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100800struct snd_info_entry *snd_info_create_module_entry(struct module * module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 const char *name,
Takashi Iwai24c1f932005-11-17 13:58:48 +0100802 struct snd_info_entry *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100804 struct snd_info_entry *entry = snd_info_create_entry(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (entry) {
806 entry->module = module;
807 entry->parent = parent;
808 }
809 return entry;
810}
811
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200812EXPORT_SYMBOL(snd_info_create_module_entry);
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814/**
815 * snd_info_create_card_entry - create an info entry for the given card
816 * @card: the card instance
817 * @name: the file name
818 * @parent: the parent directory
819 *
820 * Creates a new info entry and assigns it to the given card.
821 *
822 * Returns the pointer of the new instance, or NULL on failure.
823 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100824struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 const char *name,
Takashi Iwai24c1f932005-11-17 13:58:48 +0100826 struct snd_info_entry * parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100828 struct snd_info_entry *entry = snd_info_create_entry(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (entry) {
830 entry->module = card->module;
831 entry->card = card;
832 entry->parent = parent;
833 }
834 return entry;
835}
836
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200837EXPORT_SYMBOL(snd_info_create_card_entry);
838
Takashi Iwai746d4a02006-06-23 14:37:59 +0200839static void snd_info_disconnect(struct snd_info_entry *entry)
840{
841 struct list_head *p, *n;
842 struct proc_dir_entry *root;
843
844 list_for_each_safe(p, n, &entry->children) {
845 snd_info_disconnect(list_entry(p, struct snd_info_entry, list));
846 }
847
848 if (! entry->p)
849 return;
850 list_del_init(&entry->list);
851 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
852 snd_assert(root, return);
853 snd_remove_proc_entry(root, entry->p);
854 entry->p = NULL;
855}
856
Takashi Iwai24c1f932005-11-17 13:58:48 +0100857static int snd_info_dev_free_entry(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100859 struct snd_info_entry *entry = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 snd_info_free_entry(entry);
861 return 0;
862}
863
Takashi Iwai24c1f932005-11-17 13:58:48 +0100864static int snd_info_dev_register_entry(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100866 struct snd_info_entry *entry = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return snd_info_register(entry);
868}
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870/**
871 * snd_card_proc_new - create an info entry for the given card
872 * @card: the card instance
873 * @name: the file name
874 * @entryp: the pointer to store the new info entry
875 *
876 * Creates a new info entry and assigns it to the given card.
877 * Unlike snd_info_create_card_entry(), this function registers the
878 * info entry as an ALSA device component, so that it can be
879 * unregistered/released without explicit call.
880 * Also, you don't have to register this entry via snd_info_register(),
881 * since this will be registered by snd_card_register() automatically.
882 *
883 * The parent is assumed as card->proc_root.
884 *
885 * For releasing this entry, use snd_device_free() instead of
886 * snd_info_free_entry().
887 *
888 * Returns zero if successful, or a negative error code on failure.
889 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100890int snd_card_proc_new(struct snd_card *card, const char *name,
891 struct snd_info_entry **entryp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100893 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 .dev_free = snd_info_dev_free_entry,
895 .dev_register = snd_info_dev_register_entry,
Takashi Iwai746d4a02006-06-23 14:37:59 +0200896 /* disconnect is done via snd_info_card_disconnect() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 };
Takashi Iwai24c1f932005-11-17 13:58:48 +0100898 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 int err;
900
901 entry = snd_info_create_card_entry(card, name, card->proc_root);
902 if (! entry)
903 return -ENOMEM;
904 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
905 snd_info_free_entry(entry);
906 return err;
907 }
908 if (entryp)
909 *entryp = entry;
910 return 0;
911}
912
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200913EXPORT_SYMBOL(snd_card_proc_new);
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915/**
916 * snd_info_free_entry - release the info entry
917 * @entry: the info entry
918 *
919 * Releases the info entry. Don't call this after registered.
920 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100921void snd_info_free_entry(struct snd_info_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 if (entry == NULL)
924 return;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200925 if (entry->p) {
926 mutex_lock(&info_mutex);
927 snd_info_disconnect(entry);
928 mutex_unlock(&info_mutex);
929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 kfree(entry->name);
931 if (entry->private_free)
932 entry->private_free(entry);
933 kfree(entry);
934}
935
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200936EXPORT_SYMBOL(snd_info_free_entry);
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938/**
939 * snd_info_register - register the info entry
940 * @entry: the info entry
941 *
942 * Registers the proc info entry.
943 *
944 * Returns zero if successful, or a negative error code on failure.
945 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100946int snd_info_register(struct snd_info_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
948 struct proc_dir_entry *root, *p = NULL;
949
950 snd_assert(entry != NULL, return -ENXIO);
951 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100952 mutex_lock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 p = snd_create_proc_entry(entry->name, entry->mode, root);
954 if (!p) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100955 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return -ENOMEM;
957 }
958 p->owner = entry->module;
959 if (!S_ISDIR(entry->mode))
960 p->proc_fops = &snd_info_entry_operations;
961 p->size = entry->size;
962 p->data = entry;
963 entry->p = p;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200964 if (entry->parent)
965 list_add_tail(&entry->list, &entry->parent->children);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100966 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return 0;
968}
969
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200970EXPORT_SYMBOL(snd_info_register);
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972/*
973
974 */
975
Takashi Iwai6581f4e2006-05-17 17:14:51 +0200976static struct snd_info_entry *snd_info_version_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Takashi Iwai24c1f932005-11-17 13:58:48 +0100978static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 snd_iprintf(buffer,
981 "Advanced Linux Sound Architecture Driver Version "
982 CONFIG_SND_VERSION CONFIG_SND_DATE ".\n"
983 );
984}
985
986static int __init snd_info_version_init(void)
987{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100988 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
991 if (entry == NULL)
992 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 entry->c.text.read = snd_info_version_read;
994 if (snd_info_register(entry) < 0) {
995 snd_info_free_entry(entry);
996 return -ENOMEM;
997 }
998 snd_info_version_entry = entry;
999 return 0;
1000}
1001
1002static int __exit snd_info_version_done(void)
1003{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001004 snd_info_free_entry(snd_info_version_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return 0;
1006}
1007
1008#endif /* CONFIG_PROC_FS */