blob: c7f41c3bbd5cd3379169d8882df5f25eac0c1118 [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>
Andrea Righi27ac7922008-07-23 21:28:13 -070024#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Paulo Marques543537b2005-06-23 00:09:02 -070026#include <linux/string.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040027#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <sound/core.h>
29#include <sound/minors.h>
30#include <sound/info.h>
Jaroslav Kysela42662742012-09-04 11:21:45 +020031#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/proc_fs.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);
Takashi Iwai746d4a02006-06-23 14:37:59 +020082static void snd_info_disconnect(struct snd_info_entry *entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84
Takashi Iwai7e4eeec2006-04-28 15:13:40 +020085/* resize the proc r/w buffer */
86static int resize_info_buffer(struct snd_info_buffer *buffer,
87 unsigned int nsize)
88{
89 char *nbuf;
90
91 nsize = PAGE_ALIGN(nsize);
Takashi Iwai9983aa62009-07-06 14:31:59 +020092 nbuf = krealloc(buffer->buffer, nsize, GFP_KERNEL);
Takashi Iwai7e4eeec2006-04-28 15:13:40 +020093 if (! nbuf)
94 return -ENOMEM;
95
Takashi Iwai7e4eeec2006-04-28 15:13:40 +020096 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 Iwai4f7454a2009-09-08 14:29:58 +0200110int snd_iprintf(struct snd_info_buffer *buffer, const 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static void snd_remove_proc_entry(struct proc_dir_entry *parent,
157 struct proc_dir_entry *de)
158{
159 if (de)
160 remove_proc_entry(de->name, parent);
161}
162
163static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
164{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100165 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 struct snd_info_entry *entry;
Takashi Iwai73029e02010-04-13 11:39:47 +0200167 loff_t ret = -EINVAL, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 data = file->private_data;
170 entry = data->entry;
Takashi Iwai5b5cd552010-04-07 18:33:57 +0200171 mutex_lock(&entry->access);
Takashi Iwai73029e02010-04-13 11:39:47 +0200172 if (entry->content == SNDRV_INFO_CONTENT_DATA &&
173 entry->c.ops->llseek) {
174 offset = entry->c.ops->llseek(entry,
175 data->file_private_data,
176 file, offset, orig);
177 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Takashi Iwai73029e02010-04-13 11:39:47 +0200179 if (entry->content == SNDRV_INFO_CONTENT_DATA)
180 size = entry->size;
181 else
182 size = 0;
183 switch (orig) {
184 case SEEK_SET:
185 break;
186 case SEEK_CUR:
187 offset += file->f_pos;
188 break;
189 case SEEK_END:
190 if (!size)
191 goto out;
192 offset += size;
193 break;
194 default:
195 goto out;
196 }
197 if (offset < 0)
198 goto out;
199 if (size && offset > size)
200 offset = size;
201 file->f_pos = offset;
202 ret = offset;
203 out:
Takashi Iwai5b5cd552010-04-07 18:33:57 +0200204 mutex_unlock(&entry->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return ret;
206}
207
208static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
209 size_t count, loff_t * offset)
210{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100211 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 struct snd_info_entry *entry;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100213 struct snd_info_buffer *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 size_t size = 0;
215 loff_t pos;
216
217 data = file->private_data;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200218 if (snd_BUG_ON(!data))
219 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 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:
Takashi Iwaid97e1b72010-04-13 11:33:54 +0200239 if (pos >= entry->size)
240 return 0;
241 if (entry->c.ops->read) {
242 size = entry->size - pos;
243 size = min(count, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 size = entry->c.ops->read(entry,
245 data->file_private_data,
Takashi Iwaid97e1b72010-04-13 11:33:54 +0200246 file, buffer, size, pos);
247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 break;
249 }
250 if ((ssize_t) size > 0)
251 *offset = pos + size;
252 return size;
253}
254
255static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
256 size_t count, loff_t * offset)
257{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100258 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 struct snd_info_entry *entry;
Takashi Iwai24c1f932005-11-17 13:58:48 +0100260 struct snd_info_buffer *buf;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200261 ssize_t size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 loff_t pos;
263
264 data = file->private_data;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200265 if (snd_BUG_ON(!data))
266 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 entry = data->entry;
268 pos = *offset;
269 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
270 return -EIO;
271 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
272 return -EIO;
273 switch (entry->content) {
274 case SNDRV_INFO_CONTENT_TEXT:
275 buf = data->wbuffer;
276 if (buf == NULL)
277 return -EIO;
Clemens Ladischf4a747f2006-05-02 15:33:25 +0200278 mutex_lock(&entry->access);
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200279 if (pos + count >= buf->len) {
280 if (resize_info_buffer(buf, pos + count)) {
281 mutex_unlock(&entry->access);
282 return -ENOMEM;
283 }
284 }
285 if (copy_from_user(buf->buffer + pos, buffer, count)) {
286 mutex_unlock(&entry->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return -EFAULT;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200288 }
289 buf->size = pos + count;
290 mutex_unlock(&entry->access);
291 size = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 break;
293 case SNDRV_INFO_CONTENT_DATA:
Takashi Iwaid97e1b72010-04-13 11:33:54 +0200294 if (entry->c.ops->write && count > 0) {
295 size_t maxsize = entry->size - pos;
296 count = min(count, maxsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 size = entry->c.ops->write(entry,
298 data->file_private_data,
299 file, buffer, count, pos);
Takashi Iwaid97e1b72010-04-13 11:33:54 +0200300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 break;
302 }
303 if ((ssize_t) size > 0)
304 *offset = pos + size;
305 return size;
306}
307
308static int snd_info_entry_open(struct inode *inode, struct file *file)
309{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100310 struct snd_info_entry *entry;
311 struct snd_info_private_data *data;
312 struct snd_info_buffer *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 int mode, err;
314
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100315 mutex_lock(&info_mutex);
Al Virod9dda782013-03-31 18:16:14 -0400316 entry = PDE_DATA(inode);
Takashi Iwai746d4a02006-06-23 14:37:59 +0200317 if (entry == NULL || ! entry->p) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100318 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return -ENODEV;
320 }
321 if (!try_module_get(entry->module)) {
322 err = -EFAULT;
323 goto __error1;
324 }
325 mode = file->f_flags & O_ACCMODE;
326 if (mode == O_RDONLY || 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->read == NULL)) {
329 err = -ENODEV;
330 goto __error;
331 }
332 }
333 if (mode == O_WRONLY || mode == O_RDWR) {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200334 if ((entry->content == SNDRV_INFO_CONTENT_DATA &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 entry->c.ops->write == NULL)) {
336 err = -ENODEV;
337 goto __error;
338 }
339 }
Takashi Iwaica2c0962005-09-09 14:20:23 +0200340 data = kzalloc(sizeof(*data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (data == NULL) {
342 err = -ENOMEM;
343 goto __error;
344 }
345 data->entry = entry;
346 switch (entry->content) {
347 case SNDRV_INFO_CONTENT_TEXT:
348 if (mode == O_RDONLY || mode == O_RDWR) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200349 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200350 if (buffer == NULL)
351 goto __nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 data->rbuffer = buffer;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200353 buffer->len = PAGE_SIZE;
354 buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
355 if (buffer->buffer == NULL)
356 goto __nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358 if (mode == O_WRONLY || mode == O_RDWR) {
Takashi Iwaica2c0962005-09-09 14:20:23 +0200359 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200360 if (buffer == NULL)
361 goto __nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 data->wbuffer = buffer;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200363 buffer->len = PAGE_SIZE;
364 buffer->buffer = kmalloc(buffer->len, GFP_KERNEL);
365 if (buffer->buffer == NULL)
366 goto __nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368 break;
369 case SNDRV_INFO_CONTENT_DATA: /* data */
370 if (entry->c.ops->open) {
371 if ((err = entry->c.ops->open(entry, mode,
372 &data->file_private_data)) < 0) {
373 kfree(data);
374 goto __error;
375 }
376 }
377 break;
378 }
379 file->private_data = data;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100380 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (entry->content == SNDRV_INFO_CONTENT_TEXT &&
382 (mode == O_RDONLY || mode == O_RDWR)) {
383 if (entry->c.text.read) {
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100384 mutex_lock(&entry->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 entry->c.text.read(entry, data->rbuffer);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100386 mutex_unlock(&entry->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 }
389 return 0;
390
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200391 __nomem:
392 if (data->rbuffer) {
393 kfree(data->rbuffer->buffer);
394 kfree(data->rbuffer);
395 }
396 if (data->wbuffer) {
397 kfree(data->wbuffer->buffer);
398 kfree(data->wbuffer);
399 }
400 kfree(data);
401 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 __error:
403 module_put(entry->module);
404 __error1:
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100405 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return err;
407}
408
409static int snd_info_entry_release(struct inode *inode, struct file *file)
410{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100411 struct snd_info_entry *entry;
412 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 int mode;
414
415 mode = file->f_flags & O_ACCMODE;
416 data = file->private_data;
417 entry = data->entry;
418 switch (entry->content) {
419 case SNDRV_INFO_CONTENT_TEXT:
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200420 if (data->rbuffer) {
421 kfree(data->rbuffer->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 kfree(data->rbuffer);
423 }
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200424 if (data->wbuffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (entry->c.text.write) {
426 entry->c.text.write(entry, data->wbuffer);
427 if (data->wbuffer->error) {
428 snd_printk(KERN_WARNING "data write error to %s (%i)\n",
429 entry->name,
430 data->wbuffer->error);
431 }
432 }
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200433 kfree(data->wbuffer->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 kfree(data->wbuffer);
435 }
436 break;
437 case SNDRV_INFO_CONTENT_DATA:
438 if (entry->c.ops->release)
439 entry->c.ops->release(entry, mode,
440 data->file_private_data);
441 break;
442 }
443 module_put(entry->module);
444 kfree(data);
445 return 0;
446}
447
448static unsigned int snd_info_entry_poll(struct file *file, poll_table * wait)
449{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100450 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 struct snd_info_entry *entry;
452 unsigned int mask;
453
454 data = file->private_data;
455 if (data == NULL)
456 return 0;
457 entry = data->entry;
458 mask = 0;
459 switch (entry->content) {
460 case SNDRV_INFO_CONTENT_DATA:
461 if (entry->c.ops->poll)
462 return entry->c.ops->poll(entry,
463 data->file_private_data,
464 file, wait);
465 if (entry->c.ops->read)
466 mask |= POLLIN | POLLRDNORM;
467 if (entry->c.ops->write)
468 mask |= POLLOUT | POLLWRNORM;
469 break;
470 }
471 return mask;
472}
473
Ingo Molnard99e9882006-01-09 16:44:46 +0100474static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
475 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100477 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct snd_info_entry *entry;
479
480 data = file->private_data;
481 if (data == NULL)
482 return 0;
483 entry = data->entry;
484 switch (entry->content) {
485 case SNDRV_INFO_CONTENT_DATA:
486 if (entry->c.ops->ioctl)
487 return entry->c.ops->ioctl(entry,
488 data->file_private_data,
489 file, cmd, arg);
490 break;
491 }
492 return -ENOTTY;
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
496{
Al Viro496ad9a2013-01-23 17:07:38 -0500497 struct inode *inode = file_inode(file);
Takashi Iwai24c1f932005-11-17 13:58:48 +0100498 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct snd_info_entry *entry;
500
501 data = file->private_data;
502 if (data == NULL)
503 return 0;
504 entry = data->entry;
505 switch (entry->content) {
506 case SNDRV_INFO_CONTENT_DATA:
507 if (entry->c.ops->mmap)
508 return entry->c.ops->mmap(entry,
509 data->file_private_data,
510 inode, file, vma);
511 break;
512 }
513 return -ENXIO;
514}
515
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800516static const struct file_operations snd_info_entry_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Ingo Molnard99e9882006-01-09 16:44:46 +0100518 .owner = THIS_MODULE,
519 .llseek = snd_info_entry_llseek,
520 .read = snd_info_entry_read,
521 .write = snd_info_entry_write,
522 .poll = snd_info_entry_poll,
523 .unlocked_ioctl = snd_info_entry_ioctl,
524 .mmap = snd_info_entry_mmap,
525 .open = snd_info_entry_open,
526 .release = snd_info_entry_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527};
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529int __init snd_info_init(void)
530{
531 struct proc_dir_entry *p;
532
Al Viroe55d92b2011-07-24 02:07:46 -0400533 p = proc_mkdir("asound", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (p == NULL)
535 return -ENOMEM;
536 snd_proc_root = p;
537#ifdef CONFIG_SND_OSSEMUL
538 {
Takashi Iwai24c1f932005-11-17 13:58:48 +0100539 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
541 return -ENOMEM;
542 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
543 if (snd_info_register(entry) < 0) {
544 snd_info_free_entry(entry);
545 return -ENOMEM;
546 }
547 snd_oss_root = entry;
548 }
549#endif
550#if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
551 {
Takashi Iwai24c1f932005-11-17 13:58:48 +0100552 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL)
554 return -ENOMEM;
555 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
556 if (snd_info_register(entry) < 0) {
557 snd_info_free_entry(entry);
558 return -ENOMEM;
559 }
560 snd_seq_root = entry;
561 }
562#endif
563 snd_info_version_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 snd_minor_info_init();
565 snd_minor_info_oss_init();
566 snd_card_info_init();
567 return 0;
568}
569
570int __exit snd_info_done(void)
571{
572 snd_card_info_done();
573 snd_minor_info_oss_done();
574 snd_minor_info_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 snd_info_version_done();
576 if (snd_proc_root) {
577#if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
Takashi Iwai746d4a02006-06-23 14:37:59 +0200578 snd_info_free_entry(snd_seq_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579#endif
580#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai746d4a02006-06-23 14:37:59 +0200581 snd_info_free_entry(snd_oss_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582#endif
Alexey Dobriyanc74c1202008-04-29 01:01:44 -0700583 snd_remove_proc_entry(NULL, snd_proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585 return 0;
586}
587
588/*
589
590 */
591
592
593/*
594 * create a card proc file
595 * called from init.c
596 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100597int snd_info_card_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 char str[8];
Takashi Iwai24c1f932005-11-17 13:58:48 +0100600 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200602 if (snd_BUG_ON(!card))
603 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 sprintf(str, "card%i", card->number);
606 if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
607 return -ENOMEM;
608 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
609 if (snd_info_register(entry) < 0) {
610 snd_info_free_entry(entry);
611 return -ENOMEM;
612 }
613 card->proc_root = entry;
614 return 0;
615}
616
617/*
618 * register the card proc file
619 * called from init.c
620 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100621int snd_info_card_register(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 struct proc_dir_entry *p;
624
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200625 if (snd_BUG_ON(!card))
626 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 if (!strcmp(card->id, card->proc_root->name))
629 return 0;
630
631 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
632 if (p == NULL)
633 return -ENOMEM;
634 card->proc_root_link = p;
635 return 0;
636}
637
638/*
Jaroslav Kyselac2eb9c42008-11-12 16:31:37 +0100639 * called on card->id change
640 */
641void snd_info_card_id_change(struct snd_card *card)
642{
643 mutex_lock(&info_mutex);
644 if (card->proc_root_link) {
645 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
646 card->proc_root_link = NULL;
647 }
648 if (strcmp(card->id, card->proc_root->name))
649 card->proc_root_link = proc_symlink(card->id,
650 snd_proc_root,
651 card->proc_root->name);
652 mutex_unlock(&info_mutex);
653}
654
655/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 * de-register the card proc file
657 * called from init.c
658 */
Takashi Iwai746d4a02006-06-23 14:37:59 +0200659void snd_info_card_disconnect(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200661 if (!card)
662 return;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200663 mutex_lock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (card->proc_root_link) {
665 snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
666 card->proc_root_link = NULL;
667 }
Takashi Iwai746d4a02006-06-23 14:37:59 +0200668 if (card->proc_root)
669 snd_info_disconnect(card->proc_root);
670 mutex_unlock(&info_mutex);
671}
672
673/*
674 * release the card proc file resources
675 * called from init.c
676 */
677int snd_info_card_free(struct snd_card *card)
678{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200679 if (!card)
680 return 0;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200681 snd_info_free_entry(card->proc_root);
682 card->proc_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 0;
684}
685
686
687/**
688 * snd_info_get_line - read one line from the procfs buffer
689 * @buffer: the procfs buffer
690 * @line: the buffer to store
691 * @len: the max. buffer size - 1
692 *
693 * Reads one line from the buffer and stores the string.
694 *
695 * Returns zero if successful, or 1 if error or EOF.
696 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100697int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 int c = -1;
700
701 if (len <= 0 || buffer->stop || buffer->error)
702 return 1;
703 while (--len > 0) {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200704 c = buffer->buffer[buffer->curr++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (c == '\n') {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200706 if (buffer->curr >= buffer->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 buffer->stop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 break;
709 }
710 *line++ = c;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200711 if (buffer->curr >= buffer->size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 buffer->stop = 1;
713 break;
714 }
715 }
716 while (c != '\n' && !buffer->stop) {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200717 c = buffer->buffer[buffer->curr++];
718 if (buffer->curr >= buffer->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 buffer->stop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721 *line = '\0';
722 return 0;
723}
724
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200725EXPORT_SYMBOL(snd_info_get_line);
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727/**
Henrik Kretzschmar856def82005-07-08 13:53:42 +0200728 * snd_info_get_str - parse a string token
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 * @dest: the buffer to store the string token
730 * @src: the original string
731 * @len: the max. length of token - 1
732 *
733 * Parses the original string and copy a token to the given
734 * string buffer.
735 *
736 * Returns the updated pointer of the original string so that
737 * it can be used for the next call.
738 */
Takashi Iwai4f7454a2009-09-08 14:29:58 +0200739const char *snd_info_get_str(char *dest, const char *src, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 int c;
742
743 while (*src == ' ' || *src == '\t')
744 src++;
745 if (*src == '"' || *src == '\'') {
746 c = *src++;
747 while (--len > 0 && *src && *src != c) {
748 *dest++ = *src++;
749 }
750 if (*src == c)
751 src++;
752 } else {
753 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
754 *dest++ = *src++;
755 }
756 }
757 *dest = 0;
758 while (*src == ' ' || *src == '\t')
759 src++;
760 return src;
761}
762
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200763EXPORT_SYMBOL(snd_info_get_str);
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765/**
766 * snd_info_create_entry - create an info entry
767 * @name: the proc file name
768 *
769 * Creates an info entry with the given file name and initializes as
770 * the default state.
771 *
772 * Usually called from other functions such as
773 * snd_info_create_card_entry().
774 *
775 * Returns the pointer of the new instance, or NULL on failure.
776 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100777static struct snd_info_entry *snd_info_create_entry(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100779 struct snd_info_entry *entry;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200780 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (entry == NULL)
782 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -0700783 entry->name = kstrdup(name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (entry->name == NULL) {
785 kfree(entry);
786 return NULL;
787 }
788 entry->mode = S_IFREG | S_IRUGO;
789 entry->content = SNDRV_INFO_CONTENT_TEXT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100790 mutex_init(&entry->access);
Takashi Iwai746d4a02006-06-23 14:37:59 +0200791 INIT_LIST_HEAD(&entry->children);
792 INIT_LIST_HEAD(&entry->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return entry;
794}
795
796/**
797 * snd_info_create_module_entry - create an info entry for the given module
798 * @module: the module pointer
799 * @name: the file name
800 * @parent: the parent directory
801 *
802 * Creates a new info entry and assigns it to the given module.
803 *
804 * Returns the pointer of the new instance, or NULL on failure.
805 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100806struct snd_info_entry *snd_info_create_module_entry(struct module * module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 const char *name,
Takashi Iwai24c1f932005-11-17 13:58:48 +0100808 struct snd_info_entry *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100810 struct snd_info_entry *entry = snd_info_create_entry(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (entry) {
812 entry->module = module;
813 entry->parent = parent;
814 }
815 return entry;
816}
817
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200818EXPORT_SYMBOL(snd_info_create_module_entry);
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820/**
821 * snd_info_create_card_entry - create an info entry for the given card
822 * @card: the card instance
823 * @name: the file name
824 * @parent: the parent directory
825 *
826 * Creates a new info entry and assigns it to the given card.
827 *
828 * Returns the pointer of the new instance, or NULL on failure.
829 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100830struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 const char *name,
Takashi Iwai24c1f932005-11-17 13:58:48 +0100832 struct snd_info_entry * parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100834 struct snd_info_entry *entry = snd_info_create_entry(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (entry) {
836 entry->module = card->module;
837 entry->card = card;
838 entry->parent = parent;
839 }
840 return entry;
841}
842
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200843EXPORT_SYMBOL(snd_info_create_card_entry);
844
Takashi Iwai746d4a02006-06-23 14:37:59 +0200845static void snd_info_disconnect(struct snd_info_entry *entry)
846{
847 struct list_head *p, *n;
848 struct proc_dir_entry *root;
849
850 list_for_each_safe(p, n, &entry->children) {
851 snd_info_disconnect(list_entry(p, struct snd_info_entry, list));
852 }
853
854 if (! entry->p)
855 return;
856 list_del_init(&entry->list);
857 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200858 snd_BUG_ON(!root);
Takashi Iwai746d4a02006-06-23 14:37:59 +0200859 snd_remove_proc_entry(root, entry->p);
860 entry->p = NULL;
861}
862
Takashi Iwai24c1f932005-11-17 13:58:48 +0100863static int snd_info_dev_free_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 snd_info_free_entry(entry);
867 return 0;
868}
869
Takashi Iwai24c1f932005-11-17 13:58:48 +0100870static int snd_info_dev_register_entry(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100872 struct snd_info_entry *entry = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return snd_info_register(entry);
874}
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876/**
877 * snd_card_proc_new - create an info entry for the given card
878 * @card: the card instance
879 * @name: the file name
880 * @entryp: the pointer to store the new info entry
881 *
882 * Creates a new info entry and assigns it to the given card.
883 * Unlike snd_info_create_card_entry(), this function registers the
884 * info entry as an ALSA device component, so that it can be
885 * unregistered/released without explicit call.
886 * Also, you don't have to register this entry via snd_info_register(),
887 * since this will be registered by snd_card_register() automatically.
888 *
889 * The parent is assumed as card->proc_root.
890 *
891 * For releasing this entry, use snd_device_free() instead of
892 * snd_info_free_entry().
893 *
894 * Returns zero if successful, or a negative error code on failure.
895 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100896int snd_card_proc_new(struct snd_card *card, const char *name,
897 struct snd_info_entry **entryp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100899 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 .dev_free = snd_info_dev_free_entry,
901 .dev_register = snd_info_dev_register_entry,
Takashi Iwai746d4a02006-06-23 14:37:59 +0200902 /* disconnect is done via snd_info_card_disconnect() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 };
Takashi Iwai24c1f932005-11-17 13:58:48 +0100904 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 int err;
906
907 entry = snd_info_create_card_entry(card, name, card->proc_root);
908 if (! entry)
909 return -ENOMEM;
910 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
911 snd_info_free_entry(entry);
912 return err;
913 }
914 if (entryp)
915 *entryp = entry;
916 return 0;
917}
918
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200919EXPORT_SYMBOL(snd_card_proc_new);
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921/**
922 * snd_info_free_entry - release the info entry
923 * @entry: the info entry
924 *
925 * Releases the info entry. Don't call this after registered.
926 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100927void snd_info_free_entry(struct snd_info_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
929 if (entry == NULL)
930 return;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200931 if (entry->p) {
932 mutex_lock(&info_mutex);
933 snd_info_disconnect(entry);
934 mutex_unlock(&info_mutex);
935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 kfree(entry->name);
937 if (entry->private_free)
938 entry->private_free(entry);
939 kfree(entry);
940}
941
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200942EXPORT_SYMBOL(snd_info_free_entry);
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944/**
945 * snd_info_register - register the info entry
946 * @entry: the info entry
947 *
948 * Registers the proc info entry.
949 *
950 * Returns zero if successful, or a negative error code on failure.
951 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100952int snd_info_register(struct snd_info_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 struct proc_dir_entry *root, *p = NULL;
955
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200956 if (snd_BUG_ON(!entry))
957 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100959 mutex_lock(&info_mutex);
Al Viroaee0c612013-03-28 23:01:34 -0400960 if (S_ISDIR(entry->mode)) {
961 p = proc_mkdir_mode(entry->name, entry->mode, root);
962 if (!p) {
963 mutex_unlock(&info_mutex);
964 return -ENOMEM;
965 }
966 } else {
967 p = proc_create_data(entry->name, entry->mode, root,
968 &snd_info_entry_operations, entry);
969 if (!p) {
970 mutex_unlock(&info_mutex);
971 return -ENOMEM;
972 }
David Howells271a15e2013-04-12 00:38:51 +0100973 proc_set_size(p, entry->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 entry->p = p;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200976 if (entry->parent)
977 list_add_tail(&entry->list, &entry->parent->children);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100978 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return 0;
980}
981
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200982EXPORT_SYMBOL(snd_info_register);
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984/*
985
986 */
987
Takashi Iwai6581f4e2006-05-17 17:14:51 +0200988static struct snd_info_entry *snd_info_version_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Takashi Iwai24c1f932005-11-17 13:58:48 +0100990static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 snd_iprintf(buffer,
Jaroslav Kysela42662742012-09-04 11:21:45 +0200993 "Advanced Linux Sound Architecture Driver Version k%s.\n",
994 init_utsname()->release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
997static int __init snd_info_version_init(void)
998{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100999 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
1002 if (entry == NULL)
1003 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 entry->c.text.read = snd_info_version_read;
1005 if (snd_info_register(entry) < 0) {
1006 snd_info_free_entry(entry);
1007 return -ENOMEM;
1008 }
1009 snd_info_version_entry = entry;
1010 return 0;
1011}
1012
1013static int __exit snd_info_version_done(void)
1014{
Takashi Iwai746d4a02006-06-23 14:37:59 +02001015 snd_info_free_entry(snd_info_version_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return 0;
1017}
1018
1019#endif /* CONFIG_PROC_FS */