blob: 9977ec2eace35d547772db031c0db29bed15fe37 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/time.h>
24#include <linux/smp_lock.h>
Paulo Marques543537b2005-06-23 00:09:02 -070025#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <sound/core.h>
27#include <sound/minors.h>
28#include <sound/info.h>
29#include <sound/version.h>
30#include <linux/proc_fs.h>
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010031#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <stdarg.h>
33
34/*
35 *
36 */
37
Takashi Iwaie28563c2005-12-01 10:42:42 +010038#ifdef CONFIG_PROC_FS
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040int snd_info_check_reserved_words(const char *str)
41{
42 static char *reserved[] =
43 {
44 "version",
45 "meminfo",
46 "memdebug",
47 "detect",
48 "devices",
49 "oss",
50 "cards",
51 "timers",
52 "synth",
53 "pcm",
54 "seq",
55 NULL
56 };
57 char **xstr = reserved;
58
59 while (*xstr) {
60 if (!strcmp(*xstr, str))
61 return 0;
62 xstr++;
63 }
64 if (!strncmp(str, "card", 4))
65 return 0;
66 return 1;
67}
68
Ingo Molnar1a60d4c2006-01-16 16:29:08 +010069static DEFINE_MUTEX(info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Takashi Iwai24c1f932005-11-17 13:58:48 +010071struct snd_info_private_data {
72 struct snd_info_buffer *rbuffer;
73 struct snd_info_buffer *wbuffer;
74 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 void *file_private_data;
Takashi Iwai24c1f932005-11-17 13:58:48 +010076};
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78static int snd_info_version_init(void);
79static int snd_info_version_done(void);
Takashi Iwai746d4a02006-06-23 14:37:59 +020080static void snd_info_disconnect(struct snd_info_entry *entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82
Takashi Iwai7e4eeec2006-04-28 15:13:40 +020083/* resize the proc r/w buffer */
84static int resize_info_buffer(struct snd_info_buffer *buffer,
85 unsigned int nsize)
86{
87 char *nbuf;
88
89 nsize = PAGE_ALIGN(nsize);
90 nbuf = kmalloc(nsize, GFP_KERNEL);
91 if (! nbuf)
92 return -ENOMEM;
93
94 memcpy(nbuf, buffer->buffer, buffer->len);
95 kfree(buffer->buffer);
96 buffer->buffer = nbuf;
97 buffer->len = nsize;
98 return 0;
99}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/**
102 * snd_iprintf - printf on the procfs buffer
103 * @buffer: the procfs buffer
104 * @fmt: the printf format
105 *
106 * Outputs the string on the procfs buffer just like printf().
107 *
108 * Returns the size of output string.
109 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100110int snd_iprintf(struct snd_info_buffer *buffer, char *fmt,...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 va_list args;
113 int len, res;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200114 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Takashi Iwaif001c3a2006-04-28 15:13:41 +0200116 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (buffer->stop || buffer->error)
118 return 0;
119 len = buffer->len - buffer->size;
120 va_start(args, fmt);
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200121 for (;;) {
Takashi Iwaidbedca32006-10-18 19:09:46 +0200122 va_list ap;
123 va_copy(ap, args);
124 res = vsnprintf(buffer->buffer + buffer->curr, len, fmt, ap);
125 va_end(ap);
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200126 if (res < len)
127 break;
128 err = resize_info_buffer(buffer, buffer->len + PAGE_SIZE);
129 if (err < 0)
130 break;
131 len = buffer->len - buffer->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200133 va_end(args);
134
135 if (err < 0)
136 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 buffer->curr += res;
138 buffer->size += res;
139 return res;
140}
141
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200142EXPORT_SYMBOL(snd_iprintf);
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/*
145
146 */
147
Takashi Iwai6581f4e2006-05-17 17:14:51 +0200148static struct proc_dir_entry *snd_proc_root;
149struct snd_info_entry *snd_seq_root;
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200150EXPORT_SYMBOL(snd_seq_root);
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai6581f4e2006-05-17 17:14:51 +0200153struct snd_info_entry *snd_oss_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#endif
155
156static inline void snd_info_entry_prepare(struct proc_dir_entry *de)
157{
158 de->owner = THIS_MODULE;
159}
160
161static void snd_remove_proc_entry(struct proc_dir_entry *parent,
162 struct proc_dir_entry *de)
163{
164 if (de)
165 remove_proc_entry(de->name, parent);
166}
167
168static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
169{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100170 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 struct snd_info_entry *entry;
172 loff_t ret;
173
174 data = file->private_data;
175 entry = data->entry;
176 lock_kernel();
177 switch (entry->content) {
178 case SNDRV_INFO_CONTENT_TEXT:
179 switch (orig) {
Josef 'Jeff' Sipeke6f8f102006-09-21 11:31:58 +0200180 case SEEK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 file->f_pos = offset;
182 ret = file->f_pos;
183 goto out;
Josef 'Jeff' Sipeke6f8f102006-09-21 11:31:58 +0200184 case SEEK_CUR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 file->f_pos += offset;
186 ret = file->f_pos;
187 goto out;
Josef 'Jeff' Sipeke6f8f102006-09-21 11:31:58 +0200188 case SEEK_END:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 default:
190 ret = -EINVAL;
191 goto out;
192 }
193 break;
194 case SNDRV_INFO_CONTENT_DATA:
195 if (entry->c.ops->llseek) {
196 ret = entry->c.ops->llseek(entry,
197 data->file_private_data,
198 file, offset, orig);
199 goto out;
200 }
201 break;
202 }
203 ret = -ENXIO;
204out:
205 unlock_kernel();
206 return ret;
207}
208
209static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
210 size_t count, loff_t * offset)
211{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100212 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 struct snd_info_entry *entry;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100214 struct snd_info_buffer *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 size_t size = 0;
216 loff_t pos;
217
218 data = file->private_data;
219 snd_assert(data != NULL, return -ENXIO);
220 pos = *offset;
221 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
222 return -EIO;
223 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
224 return -EIO;
225 entry = data->entry;
226 switch (entry->content) {
227 case SNDRV_INFO_CONTENT_TEXT:
228 buf = data->rbuffer;
229 if (buf == NULL)
230 return -EIO;
231 if (pos >= buf->size)
232 return 0;
233 size = buf->size - pos;
234 size = min(count, size);
235 if (copy_to_user(buffer, buf->buffer + pos, size))
236 return -EFAULT;
237 break;
238 case SNDRV_INFO_CONTENT_DATA:
239 if (entry->c.ops->read)
240 size = entry->c.ops->read(entry,
241 data->file_private_data,
242 file, buffer, count, pos);
243 break;
244 }
245 if ((ssize_t) size > 0)
246 *offset = pos + size;
247 return size;
248}
249
250static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
251 size_t count, loff_t * offset)
252{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100253 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 struct snd_info_entry *entry;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100255 struct snd_info_buffer *buf;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200256 ssize_t size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 loff_t pos;
258
259 data = file->private_data;
260 snd_assert(data != NULL, return -ENXIO);
261 entry = data->entry;
262 pos = *offset;
263 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
264 return -EIO;
265 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
266 return -EIO;
267 switch (entry->content) {
268 case SNDRV_INFO_CONTENT_TEXT:
269 buf = data->wbuffer;
270 if (buf == NULL)
271 return -EIO;
Clemens Ladischf4a747f2006-05-02 15:33:25 +0200272 mutex_lock(&entry->access);
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200273 if (pos + count >= buf->len) {
274 if (resize_info_buffer(buf, pos + count)) {
275 mutex_unlock(&entry->access);
276 return -ENOMEM;
277 }
278 }
279 if (copy_from_user(buf->buffer + pos, buffer, count)) {
280 mutex_unlock(&entry->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return -EFAULT;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200282 }
283 buf->size = pos + count;
284 mutex_unlock(&entry->access);
285 size = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 break;
287 case SNDRV_INFO_CONTENT_DATA:
288 if (entry->c.ops->write)
289 size = entry->c.ops->write(entry,
290 data->file_private_data,
291 file, buffer, count, pos);
292 break;
293 }
294 if ((ssize_t) size > 0)
295 *offset = pos + size;
296 return size;
297}
298
299static int snd_info_entry_open(struct inode *inode, struct file *file)
300{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100301 struct snd_info_entry *entry;
302 struct snd_info_private_data *data;
303 struct snd_info_buffer *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 struct proc_dir_entry *p;
305 int mode, err;
306
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100307 mutex_lock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 p = PDE(inode);
Takashi Iwai24c1f932005-11-17 13:58:48 +0100309 entry = p == NULL ? NULL : (struct snd_info_entry *)p->data;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200310 if (entry == NULL || ! entry->p) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100311 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return -ENODEV;
313 }
314 if (!try_module_get(entry->module)) {
315 err = -EFAULT;
316 goto __error1;
317 }
318 mode = file->f_flags & O_ACCMODE;
319 if (mode == O_RDONLY || mode == O_RDWR) {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200320 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 entry->c.ops->read == NULL)) {
322 err = -ENODEV;
323 goto __error;
324 }
325 }
326 if (mode == O_WRONLY || mode == O_RDWR) {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200327 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 entry->c.ops->write == NULL)) {
329 err = -ENODEV;
330 goto __error;
331 }
332 }
Takashi Iwaica2c0962005-09-09 14:20:23 +0200333 data = kzalloc(sizeof(*data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (data == NULL) {
335 err = -ENOMEM;
336 goto __error;
337 }
338 data->entry = entry;
339 switch (entry->content) {
340 case SNDRV_INFO_CONTENT_TEXT:
341 if (mode == O_RDONLY || mode == O_RDWR) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200342 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200343 if (buffer == NULL)
344 goto __nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 data->rbuffer = buffer;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200346 buffer->len = PAGE_SIZE;
347 buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
348 if (buffer->buffer == NULL)
349 goto __nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351 if (mode == O_WRONLY || mode == O_RDWR) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200352 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200353 if (buffer == NULL)
354 goto __nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 data->wbuffer = buffer;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200356 buffer->len = PAGE_SIZE;
357 buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
358 if (buffer->buffer == NULL)
359 goto __nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361 break;
362 case SNDRV_INFO_CONTENT_DATA: /* data */
363 if (entry->c.ops->open) {
364 if ((err = entry->c.ops->open(entry, mode,
365 &data->file_private_data)) < 0) {
366 kfree(data);
367 goto __error;
368 }
369 }
370 break;
371 }
372 file->private_data = data;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100373 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (entry->content == SNDRV_INFO_CONTENT_TEXT &&
375 (mode == O_RDONLY || mode == O_RDWR)) {
376 if (entry->c.text.read) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100377 mutex_lock(&entry->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 entry->c.text.read(entry, data->rbuffer);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100379 mutex_unlock(&entry->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381 }
382 return 0;
383
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200384 __nomem:
385 if (data->rbuffer) {
386 kfree(data->rbuffer->buffer);
387 kfree(data->rbuffer);
388 }
389 if (data->wbuffer) {
390 kfree(data->wbuffer->buffer);
391 kfree(data->wbuffer);
392 }
393 kfree(data);
394 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 __error:
396 module_put(entry->module);
397 __error1:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100398 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return err;
400}
401
402static int snd_info_entry_release(struct inode *inode, struct file *file)
403{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100404 struct snd_info_entry *entry;
405 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 int mode;
407
408 mode = file->f_flags & O_ACCMODE;
409 data = file->private_data;
410 entry = data->entry;
411 switch (entry->content) {
412 case SNDRV_INFO_CONTENT_TEXT:
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200413 if (data->rbuffer) {
414 kfree(data->rbuffer->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 kfree(data->rbuffer);
416 }
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200417 if (data->wbuffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (entry->c.text.write) {
419 entry->c.text.write(entry, data->wbuffer);
420 if (data->wbuffer->error) {
421 snd_printk(KERN_WARNING "data write error to %s (%i)\n",
422 entry->name,
423 data->wbuffer->error);
424 }
425 }
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200426 kfree(data->wbuffer->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 kfree(data->wbuffer);
428 }
429 break;
430 case SNDRV_INFO_CONTENT_DATA:
431 if (entry->c.ops->release)
432 entry->c.ops->release(entry, mode,
433 data->file_private_data);
434 break;
435 }
436 module_put(entry->module);
437 kfree(data);
438 return 0;
439}
440
441static unsigned int snd_info_entry_poll(struct file *file, poll_table * wait)
442{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100443 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 struct snd_info_entry *entry;
445 unsigned int mask;
446
447 data = file->private_data;
448 if (data == NULL)
449 return 0;
450 entry = data->entry;
451 mask = 0;
452 switch (entry->content) {
453 case SNDRV_INFO_CONTENT_DATA:
454 if (entry->c.ops->poll)
455 return entry->c.ops->poll(entry,
456 data->file_private_data,
457 file, wait);
458 if (entry->c.ops->read)
459 mask |= POLLIN | POLLRDNORM;
460 if (entry->c.ops->write)
461 mask |= POLLOUT | POLLWRNORM;
462 break;
463 }
464 return mask;
465}
466
Ingo Molnard99e9882006-01-09 16:44:46 +0100467static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
468 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100470 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct snd_info_entry *entry;
472
473 data = file->private_data;
474 if (data == NULL)
475 return 0;
476 entry = data->entry;
477 switch (entry->content) {
478 case SNDRV_INFO_CONTENT_DATA:
479 if (entry->c.ops->ioctl)
480 return entry->c.ops->ioctl(entry,
481 data->file_private_data,
482 file, cmd, arg);
483 break;
484 }
485 return -ENOTTY;
486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
489{
Josef Sipek7bc56322006-12-08 02:37:40 -0800490 struct inode *inode = file->f_path.dentry->d_inode;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100491 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 struct snd_info_entry *entry;
493
494 data = file->private_data;
495 if (data == NULL)
496 return 0;
497 entry = data->entry;
498 switch (entry->content) {
499 case SNDRV_INFO_CONTENT_DATA:
500 if (entry->c.ops->mmap)
501 return entry->c.ops->mmap(entry,
502 data->file_private_data,
503 inode, file, vma);
504 break;
505 }
506 return -ENXIO;
507}
508
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800509static const struct file_operations snd_info_entry_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Ingo Molnard99e9882006-01-09 16:44:46 +0100511 .owner = THIS_MODULE,
512 .llseek = snd_info_entry_llseek,
513 .read = snd_info_entry_read,
514 .write = snd_info_entry_write,
515 .poll = snd_info_entry_poll,
516 .unlocked_ioctl = snd_info_entry_ioctl,
517 .mmap = snd_info_entry_mmap,
518 .open = snd_info_entry_open,
519 .release = snd_info_entry_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520};
521
522/**
523 * snd_create_proc_entry - create a procfs entry
524 * @name: the name of the proc file
525 * @mode: the file permission bits, S_Ixxx
526 * @parent: the parent proc-directory entry
527 *
528 * Creates a new proc file entry with the given name and permission
529 * on the given directory.
530 *
531 * Returns the pointer of new instance or NULL on failure.
532 */
533static struct proc_dir_entry *snd_create_proc_entry(const char *name, mode_t mode,
534 struct proc_dir_entry *parent)
535{
536 struct proc_dir_entry *p;
537 p = create_proc_entry(name, mode, parent);
538 if (p)
539 snd_info_entry_prepare(p);
540 return p;
541}
542
543int __init snd_info_init(void)
544{
545 struct proc_dir_entry *p;
546
547 p = snd_create_proc_entry("asound", S_IFDIR | S_IRUGO | S_IXUGO, &proc_root);
548 if (p == NULL)
549 return -ENOMEM;
550 snd_proc_root = p;
551#ifdef CONFIG_SND_OSSEMUL
552 {
Takashi Iwai24c1f932005-11-17 13:58:48 +0100553 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
555 return -ENOMEM;
556 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
557 if (snd_info_register(entry) < 0) {
558 snd_info_free_entry(entry);
559 return -ENOMEM;
560 }
561 snd_oss_root = entry;
562 }
563#endif
564#if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
565 {
Takashi Iwai24c1f932005-11-17 13:58:48 +0100566 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL)
568 return -ENOMEM;
569 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
570 if (snd_info_register(entry) < 0) {
571 snd_info_free_entry(entry);
572 return -ENOMEM;
573 }
574 snd_seq_root = entry;
575 }
576#endif
577 snd_info_version_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 snd_minor_info_init();
579 snd_minor_info_oss_init();
580 snd_card_info_init();
581 return 0;
582}
583
584int __exit snd_info_done(void)
585{
586 snd_card_info_done();
587 snd_minor_info_oss_done();
588 snd_minor_info_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 snd_info_version_done();
590 if (snd_proc_root) {
591#if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
Takashi Iwai746d4a02006-06-23 14:37:59 +0200592 snd_info_free_entry(snd_seq_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593#endif
594#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai746d4a02006-06-23 14:37:59 +0200595 snd_info_free_entry(snd_oss_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596#endif
597 snd_remove_proc_entry(&proc_root, snd_proc_root);
598 }
599 return 0;
600}
601
602/*
603
604 */
605
606
607/*
608 * create a card proc file
609 * called from init.c
610 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100611int snd_info_card_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 char str[8];
Takashi Iwai24c1f932005-11-17 13:58:48 +0100614 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 snd_assert(card != NULL, return -ENXIO);
617
618 sprintf(str, "card%i", card->number);
619 if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
620 return -ENOMEM;
621 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
622 if (snd_info_register(entry) < 0) {
623 snd_info_free_entry(entry);
624 return -ENOMEM;
625 }
626 card->proc_root = entry;
627 return 0;
628}
629
630/*
631 * register the card proc file
632 * called from init.c
633 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100634int snd_info_card_register(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 struct proc_dir_entry *p;
637
638 snd_assert(card != NULL, return -ENXIO);
639
640 if (!strcmp(card->id, card->proc_root->name))
641 return 0;
642
643 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
644 if (p == NULL)
645 return -ENOMEM;
646 card->proc_root_link = p;
647 return 0;
648}
649
650/*
651 * de-register the card proc file
652 * called from init.c
653 */
Takashi Iwai746d4a02006-06-23 14:37:59 +0200654void snd_info_card_disconnect(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Takashi Iwai746d4a02006-06-23 14:37:59 +0200656 snd_assert(card != NULL, return);
657 mutex_lock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (card->proc_root_link) {
659 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
660 card->proc_root_link = NULL;
661 }
Takashi Iwai746d4a02006-06-23 14:37:59 +0200662 if (card->proc_root)
663 snd_info_disconnect(card->proc_root);
664 mutex_unlock(&info_mutex);
665}
666
667/*
668 * release the card proc file resources
669 * called from init.c
670 */
671int snd_info_card_free(struct snd_card *card)
672{
673 snd_assert(card != NULL, return -ENXIO);
674 snd_info_free_entry(card->proc_root);
675 card->proc_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return 0;
677}
678
679
680/**
681 * snd_info_get_line - read one line from the procfs buffer
682 * @buffer: the procfs buffer
683 * @line: the buffer to store
684 * @len: the max. buffer size - 1
685 *
686 * Reads one line from the buffer and stores the string.
687 *
688 * Returns zero if successful, or 1 if error or EOF.
689 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100690int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 int c = -1;
693
694 if (len <= 0 || buffer->stop || buffer->error)
695 return 1;
696 while (--len > 0) {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200697 c = buffer->buffer[buffer->curr++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (c == '\n') {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200699 if (buffer->curr >= buffer->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 buffer->stop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
702 }
703 *line++ = c;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200704 if (buffer->curr >= buffer->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 buffer->stop = 1;
706 break;
707 }
708 }
709 while (c != '\n' && !buffer->stop) {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200710 c = buffer->buffer[buffer->curr++];
711 if (buffer->curr >= buffer->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 buffer->stop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714 *line = '\0';
715 return 0;
716}
717
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200718EXPORT_SYMBOL(snd_info_get_line);
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720/**
Henrik Kretzschmar856def82005-07-08 13:53:42 +0200721 * snd_info_get_str - parse a string token
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 * @dest: the buffer to store the string token
723 * @src: the original string
724 * @len: the max. length of token - 1
725 *
726 * Parses the original string and copy a token to the given
727 * string buffer.
728 *
729 * Returns the updated pointer of the original string so that
730 * it can be used for the next call.
731 */
732char *snd_info_get_str(char *dest, char *src, int len)
733{
734 int c;
735
736 while (*src == ' ' || *src == '\t')
737 src++;
738 if (*src == '"' || *src == '\'') {
739 c = *src++;
740 while (--len > 0 && *src && *src != c) {
741 *dest++ = *src++;
742 }
743 if (*src == c)
744 src++;
745 } else {
746 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
747 *dest++ = *src++;
748 }
749 }
750 *dest = 0;
751 while (*src == ' ' || *src == '\t')
752 src++;
753 return src;
754}
755
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200756EXPORT_SYMBOL(snd_info_get_str);
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/**
759 * snd_info_create_entry - create an info entry
760 * @name: the proc file name
761 *
762 * Creates an info entry with the given file name and initializes as
763 * the default state.
764 *
765 * Usually called from other functions such as
766 * snd_info_create_card_entry().
767 *
768 * Returns the pointer of the new instance, or NULL on failure.
769 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100770static struct snd_info_entry *snd_info_create_entry(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100772 struct snd_info_entry *entry;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200773 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (entry == NULL)
775 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -0700776 entry->name = kstrdup(name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (entry->name == NULL) {
778 kfree(entry);
779 return NULL;
780 }
781 entry->mode = S_IFREG | S_IRUGO;
782 entry->content = SNDRV_INFO_CONTENT_TEXT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100783 mutex_init(&entry->access);
Takashi Iwai746d4a02006-06-23 14:37:59 +0200784 INIT_LIST_HEAD(&entry->children);
785 INIT_LIST_HEAD(&entry->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return entry;
787}
788
789/**
790 * snd_info_create_module_entry - create an info entry for the given module
791 * @module: the module pointer
792 * @name: the file name
793 * @parent: the parent directory
794 *
795 * Creates a new info entry and assigns it to the given module.
796 *
797 * Returns the pointer of the new instance, or NULL on failure.
798 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100799struct snd_info_entry *snd_info_create_module_entry(struct module * module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 const char *name,
Takashi Iwai24c1f932005-11-17 13:58:48 +0100801 struct snd_info_entry *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100803 struct snd_info_entry *entry = snd_info_create_entry(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (entry) {
805 entry->module = module;
806 entry->parent = parent;
807 }
808 return entry;
809}
810
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200811EXPORT_SYMBOL(snd_info_create_module_entry);
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813/**
814 * snd_info_create_card_entry - create an info entry for the given card
815 * @card: the card instance
816 * @name: the file name
817 * @parent: the parent directory
818 *
819 * Creates a new info entry and assigns it to the given card.
820 *
821 * Returns the pointer of the new instance, or NULL on failure.
822 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100823struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 const char *name,
Takashi Iwai24c1f932005-11-17 13:58:48 +0100825 struct snd_info_entry * parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100827 struct snd_info_entry *entry = snd_info_create_entry(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (entry) {
829 entry->module = card->module;
830 entry->card = card;
831 entry->parent = parent;
832 }
833 return entry;
834}
835
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200836EXPORT_SYMBOL(snd_info_create_card_entry);
837
Takashi Iwai746d4a02006-06-23 14:37:59 +0200838static void snd_info_disconnect(struct snd_info_entry *entry)
839{
840 struct list_head *p, *n;
841 struct proc_dir_entry *root;
842
843 list_for_each_safe(p, n, &entry->children) {
844 snd_info_disconnect(list_entry(p, struct snd_info_entry, list));
845 }
846
847 if (! entry->p)
848 return;
849 list_del_init(&entry->list);
850 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
851 snd_assert(root, return);
852 snd_remove_proc_entry(root, entry->p);
853 entry->p = NULL;
854}
855
Takashi Iwai24c1f932005-11-17 13:58:48 +0100856static int snd_info_dev_free_entry(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100858 struct snd_info_entry *entry = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 snd_info_free_entry(entry);
860 return 0;
861}
862
Takashi Iwai24c1f932005-11-17 13:58:48 +0100863static int snd_info_dev_register_entry(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100865 struct snd_info_entry *entry = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return snd_info_register(entry);
867}
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869/**
870 * snd_card_proc_new - create an info entry for the given card
871 * @card: the card instance
872 * @name: the file name
873 * @entryp: the pointer to store the new info entry
874 *
875 * Creates a new info entry and assigns it to the given card.
876 * Unlike snd_info_create_card_entry(), this function registers the
877 * info entry as an ALSA device component, so that it can be
878 * unregistered/released without explicit call.
879 * Also, you don't have to register this entry via snd_info_register(),
880 * since this will be registered by snd_card_register() automatically.
881 *
882 * The parent is assumed as card->proc_root.
883 *
884 * For releasing this entry, use snd_device_free() instead of
885 * snd_info_free_entry().
886 *
887 * Returns zero if successful, or a negative error code on failure.
888 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100889int snd_card_proc_new(struct snd_card *card, const char *name,
890 struct snd_info_entry **entryp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100892 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 .dev_free = snd_info_dev_free_entry,
894 .dev_register = snd_info_dev_register_entry,
Takashi Iwai746d4a02006-06-23 14:37:59 +0200895 /* disconnect is done via snd_info_card_disconnect() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 };
Takashi Iwai24c1f932005-11-17 13:58:48 +0100897 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 int err;
899
900 entry = snd_info_create_card_entry(card, name, card->proc_root);
901 if (! entry)
902 return -ENOMEM;
903 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
904 snd_info_free_entry(entry);
905 return err;
906 }
907 if (entryp)
908 *entryp = entry;
909 return 0;
910}
911
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200912EXPORT_SYMBOL(snd_card_proc_new);
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914/**
915 * snd_info_free_entry - release the info entry
916 * @entry: the info entry
917 *
918 * Releases the info entry. Don't call this after registered.
919 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100920void snd_info_free_entry(struct snd_info_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 if (entry == NULL)
923 return;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200924 if (entry->p) {
925 mutex_lock(&info_mutex);
926 snd_info_disconnect(entry);
927 mutex_unlock(&info_mutex);
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 kfree(entry->name);
930 if (entry->private_free)
931 entry->private_free(entry);
932 kfree(entry);
933}
934
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200935EXPORT_SYMBOL(snd_info_free_entry);
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937/**
938 * snd_info_register - register the info entry
939 * @entry: the info entry
940 *
941 * Registers the proc info entry.
942 *
943 * Returns zero if successful, or a negative error code on failure.
944 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100945int snd_info_register(struct snd_info_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 struct proc_dir_entry *root, *p = NULL;
948
949 snd_assert(entry != NULL, return -ENXIO);
950 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100951 mutex_lock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 p = snd_create_proc_entry(entry->name, entry->mode, root);
953 if (!p) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100954 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return -ENOMEM;
956 }
957 p->owner = entry->module;
958 if (!S_ISDIR(entry->mode))
959 p->proc_fops = &snd_info_entry_operations;
960 p->size = entry->size;
961 p->data = entry;
962 entry->p = p;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200963 if (entry->parent)
964 list_add_tail(&entry->list, &entry->parent->children);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100965 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return 0;
967}
968
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200969EXPORT_SYMBOL(snd_info_register);
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971/*
972
973 */
974
Takashi Iwai6581f4e2006-05-17 17:14:51 +0200975static struct snd_info_entry *snd_info_version_entry;
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{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001003 snd_info_free_entry(snd_info_version_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return 0;
1005}
1006
1007#endif /* CONFIG_PROC_FS */