blob: 9c6db5c24da726b3baa4870ccc19b9a9d7f6fbf5 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
85
86 */
87
Takashi Iwai6581f4e2006-05-17 17:14:51 +020088static struct proc_dir_entry *snd_proc_root;
89struct snd_info_entry *snd_seq_root;
Takashi Iwaic0d3fb32006-04-28 15:13:39 +020090EXPORT_SYMBOL(snd_seq_root);
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai6581f4e2006-05-17 17:14:51 +020093struct snd_info_entry *snd_oss_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#endif
95
Takashi Iwai4adb7bc2015-04-22 16:10:22 +020096static int alloc_info_private(struct snd_info_entry *entry,
97 struct snd_info_private_data **ret)
98{
99 struct snd_info_private_data *data;
100
101 if (!entry || !entry->p)
102 return -ENODEV;
103 if (!try_module_get(entry->module))
104 return -EFAULT;
105 data = kzalloc(sizeof(*data), GFP_KERNEL);
106 if (!data) {
107 module_put(entry->module);
108 return -ENOMEM;
109 }
110 data->entry = entry;
111 *ret = data;
112 return 0;
113}
114
115static bool valid_pos(loff_t pos, size_t count)
116{
117 if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
118 return false;
119 if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
120 return false;
121 return true;
122}
123
124/*
125 * file ops for binary proc files
126 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
128{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100129 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct snd_info_entry *entry;
Takashi Iwai73029e02010-04-13 11:39:47 +0200131 loff_t ret = -EINVAL, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 data = file->private_data;
134 entry = data->entry;
Takashi Iwai5b5cd552010-04-07 18:33:57 +0200135 mutex_lock(&entry->access);
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200136 if (entry->c.ops->llseek) {
Takashi Iwai73029e02010-04-13 11:39:47 +0200137 offset = entry->c.ops->llseek(entry,
138 data->file_private_data,
139 file, offset, orig);
140 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200142
143 size = entry->size;
Takashi Iwai73029e02010-04-13 11:39:47 +0200144 switch (orig) {
145 case SEEK_SET:
146 break;
147 case SEEK_CUR:
148 offset += file->f_pos;
149 break;
150 case SEEK_END:
151 if (!size)
152 goto out;
153 offset += size;
154 break;
155 default:
156 goto out;
157 }
158 if (offset < 0)
159 goto out;
160 if (size && offset > size)
161 offset = size;
162 file->f_pos = offset;
163 ret = offset;
164 out:
Takashi Iwai5b5cd552010-04-07 18:33:57 +0200165 mutex_unlock(&entry->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return ret;
167}
168
169static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
170 size_t count, loff_t * offset)
171{
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200172 struct snd_info_private_data *data = file->private_data;
173 struct snd_info_entry *entry = data->entry;
174 size_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 loff_t pos;
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 pos = *offset;
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200178 if (!valid_pos(pos, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return -EIO;
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200180 if (pos >= entry->size)
181 return 0;
182 size = entry->size - pos;
183 size = min(count, size);
184 size = entry->c.ops->read(entry, data->file_private_data,
185 file, buffer, size, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if ((ssize_t) size > 0)
187 *offset = pos + size;
188 return size;
189}
190
191static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
192 size_t count, loff_t * offset)
193{
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200194 struct snd_info_private_data *data = file->private_data;
195 struct snd_info_entry *entry = data->entry;
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200196 ssize_t size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 loff_t pos;
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 pos = *offset;
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200200 if (!valid_pos(pos, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return -EIO;
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200202 if (count > 0) {
203 size_t maxsize = entry->size - pos;
204 count = min(count, maxsize);
205 size = entry->c.ops->write(entry, data->file_private_data,
206 file, buffer, count, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200208 if (size > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 *offset = pos + size;
210 return size;
211}
212
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200213static unsigned int snd_info_entry_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200215 struct snd_info_private_data *data = file->private_data;
216 struct snd_info_entry *entry = data->entry;
217 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200219 if (entry->c.ops->poll)
220 return entry->c.ops->poll(entry,
221 data->file_private_data,
222 file, wait);
223 if (entry->c.ops->read)
224 mask |= POLLIN | POLLRDNORM;
225 if (entry->c.ops->write)
226 mask |= POLLOUT | POLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return mask;
228}
229
Ingo Molnard99e9882006-01-09 16:44:46 +0100230static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
231 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200233 struct snd_info_private_data *data = file->private_data;
234 struct snd_info_entry *entry = data->entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200236 if (!entry->c.ops->ioctl)
237 return -ENOTTY;
238 return entry->c.ops->ioctl(entry, data->file_private_data,
239 file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
243{
Al Viro496ad9a2013-01-23 17:07:38 -0500244 struct inode *inode = file_inode(file);
Takashi Iwai24c1f932005-11-17 13:58:48 +0100245 struct snd_info_private_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 struct snd_info_entry *entry;
247
248 data = file->private_data;
249 if (data == NULL)
250 return 0;
251 entry = data->entry;
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200252 if (!entry->c.ops->mmap)
253 return -ENXIO;
254 return entry->c.ops->mmap(entry, data->file_private_data,
255 inode, file, vma);
256}
257
258static int snd_info_entry_open(struct inode *inode, struct file *file)
259{
260 struct snd_info_entry *entry = PDE_DATA(inode);
261 struct snd_info_private_data *data;
262 int mode, err;
263
264 mutex_lock(&info_mutex);
265 err = alloc_info_private(entry, &data);
266 if (err < 0)
267 goto unlock;
268
269 mode = file->f_flags & O_ACCMODE;
270 if (((mode == O_RDONLY || mode == O_RDWR) && !entry->c.ops->read) ||
271 ((mode == O_WRONLY || mode == O_RDWR) && !entry->c.ops->write)) {
272 err = -ENODEV;
273 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200275
276 if (entry->c.ops->open) {
277 err = entry->c.ops->open(entry, mode, &data->file_private_data);
278 if (err < 0)
279 goto error;
280 }
281
282 file->private_data = data;
283 mutex_unlock(&info_mutex);
284 return 0;
285
286 error:
287 kfree(data);
288 module_put(entry->module);
289 unlock:
290 mutex_unlock(&info_mutex);
291 return err;
292}
293
294static int snd_info_entry_release(struct inode *inode, struct file *file)
295{
296 struct snd_info_private_data *data = file->private_data;
297 struct snd_info_entry *entry = data->entry;
298
299 if (entry->c.ops->release)
300 entry->c.ops->release(entry, file->f_flags & O_ACCMODE,
301 data->file_private_data);
302 module_put(entry->module);
303 kfree(data);
304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800307static const struct file_operations snd_info_entry_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Ingo Molnard99e9882006-01-09 16:44:46 +0100309 .owner = THIS_MODULE,
310 .llseek = snd_info_entry_llseek,
311 .read = snd_info_entry_read,
312 .write = snd_info_entry_write,
313 .poll = snd_info_entry_poll,
314 .unlocked_ioctl = snd_info_entry_ioctl,
315 .mmap = snd_info_entry_mmap,
316 .open = snd_info_entry_open,
317 .release = snd_info_entry_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318};
319
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200320/*
321 * file ops for text proc files
322 */
323static ssize_t snd_info_text_entry_write(struct file *file,
324 const char __user *buffer,
325 size_t count, loff_t *offset)
326{
327 struct seq_file *m = file->private_data;
328 struct snd_info_private_data *data = m->private;
329 struct snd_info_entry *entry = data->entry;
330 struct snd_info_buffer *buf;
331 loff_t pos;
332 size_t next;
333 int err = 0;
334
335 pos = *offset;
336 if (!valid_pos(pos, count))
337 return -EIO;
338 next = pos + count;
339 mutex_lock(&entry->access);
340 buf = data->wbuffer;
341 if (!buf) {
342 data->wbuffer = buf = kzalloc(sizeof(*buf), GFP_KERNEL);
343 if (!buf) {
344 err = -ENOMEM;
345 goto error;
346 }
347 }
348 if (next > buf->len) {
349 char *nbuf = krealloc(buf->buffer, PAGE_ALIGN(next),
350 GFP_KERNEL | __GFP_ZERO);
351 if (!nbuf) {
352 err = -ENOMEM;
353 goto error;
354 }
355 buf->buffer = nbuf;
356 buf->len = PAGE_ALIGN(next);
357 }
358 if (copy_from_user(buf->buffer + pos, buffer, count)) {
359 err = -EFAULT;
360 goto error;
361 }
362 buf->size = next;
363 error:
364 mutex_unlock(&entry->access);
365 if (err < 0)
366 return err;
367 *offset = next;
368 return count;
369}
370
371static int snd_info_seq_show(struct seq_file *seq, void *p)
372{
373 struct snd_info_private_data *data = seq->private;
374 struct snd_info_entry *entry = data->entry;
375
376 if (entry->c.text.read) {
377 data->rbuffer->buffer = (char *)seq; /* XXX hack! */
378 entry->c.text.read(entry, data->rbuffer);
379 }
380 return 0;
381}
382
383static int snd_info_text_entry_open(struct inode *inode, struct file *file)
384{
385 struct snd_info_entry *entry = PDE_DATA(inode);
386 struct snd_info_private_data *data;
387 int err;
388
389 mutex_lock(&info_mutex);
390 err = alloc_info_private(entry, &data);
391 if (err < 0)
392 goto unlock;
393
394 data->rbuffer = kzalloc(sizeof(*data->rbuffer), GFP_KERNEL);
395 if (!data->rbuffer) {
396 err = -ENOMEM;
397 goto error;
398 }
399 if (entry->size)
400 err = single_open_size(file, snd_info_seq_show, data,
401 entry->size);
402 else
403 err = single_open(file, snd_info_seq_show, data);
404 if (err < 0)
405 goto error;
406 mutex_unlock(&info_mutex);
407 return 0;
408
409 error:
410 kfree(data->rbuffer);
411 kfree(data);
412 module_put(entry->module);
413 unlock:
414 mutex_unlock(&info_mutex);
415 return err;
416}
417
418static int snd_info_text_entry_release(struct inode *inode, struct file *file)
419{
420 struct seq_file *m = file->private_data;
421 struct snd_info_private_data *data = m->private;
422 struct snd_info_entry *entry = data->entry;
423
424 if (data->wbuffer && entry->c.text.write)
425 entry->c.text.write(entry, data->wbuffer);
426
427 single_release(inode, file);
428 kfree(data->rbuffer);
429 if (data->wbuffer) {
430 kfree(data->wbuffer->buffer);
431 kfree(data->wbuffer);
432 }
433
434 module_put(entry->module);
435 kfree(data);
436 return 0;
437}
438
439static const struct file_operations snd_info_text_entry_ops =
440{
441 .owner = THIS_MODULE,
442 .open = snd_info_text_entry_open,
443 .release = snd_info_text_entry_release,
444 .write = snd_info_text_entry_write,
445 .llseek = seq_lseek,
446 .read = seq_read,
447};
448
Takashi Iwai886364f2015-04-22 17:54:25 +0200449static struct snd_info_entry *create_subdir(struct module *mod,
450 const char *name)
451{
452 struct snd_info_entry *entry;
453
454 entry = snd_info_create_module_entry(mod, name, NULL);
455 if (!entry)
456 return NULL;
457 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
458 if (snd_info_register(entry) < 0) {
459 snd_info_free_entry(entry);
460 return NULL;
461 }
462 return entry;
463}
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465int __init snd_info_init(void)
466{
467 struct proc_dir_entry *p;
468
Al Viroe55d92b2011-07-24 02:07:46 -0400469 p = proc_mkdir("asound", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (p == NULL)
471 return -ENOMEM;
472 snd_proc_root = p;
473#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai886364f2015-04-22 17:54:25 +0200474 snd_oss_root = create_subdir(THIS_MODULE, "oss");
475 if (!snd_oss_root)
476 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477#endif
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100478#if IS_ENABLED(CONFIG_SND_SEQUENCER)
Takashi Iwai886364f2015-04-22 17:54:25 +0200479 snd_seq_root = create_subdir(THIS_MODULE, "seq");
480 if (!snd_seq_root)
481 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482#endif
483 snd_info_version_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 snd_minor_info_init();
485 snd_minor_info_oss_init();
486 snd_card_info_init();
487 return 0;
Takashi Iwai886364f2015-04-22 17:54:25 +0200488
489 error:
490#ifdef CONFIG_SND_OSSEMUL
491 snd_info_free_entry(snd_oss_root);
492#endif
493 proc_remove(snd_proc_root);
494 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497int __exit snd_info_done(void)
498{
499 snd_card_info_done();
500 snd_minor_info_oss_done();
501 snd_minor_info_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 snd_info_version_done();
503 if (snd_proc_root) {
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100504#if IS_ENABLED(CONFIG_SND_SEQUENCER)
Takashi Iwai746d4a02006-06-23 14:37:59 +0200505 snd_info_free_entry(snd_seq_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506#endif
507#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai746d4a02006-06-23 14:37:59 +0200508 snd_info_free_entry(snd_oss_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509#endif
David Howellsa8ca16e2013-04-12 17:27:28 +0100510 proc_remove(snd_proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 return 0;
513}
514
515/*
516
517 */
518
519
520/*
521 * create a card proc file
522 * called from init.c
523 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100524int snd_info_card_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 char str[8];
Takashi Iwai24c1f932005-11-17 13:58:48 +0100527 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200529 if (snd_BUG_ON(!card))
530 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 sprintf(str, "card%i", card->number);
Takashi Iwai886364f2015-04-22 17:54:25 +0200533 entry = create_subdir(card->module, str);
534 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 card->proc_root = entry;
537 return 0;
538}
539
540/*
541 * register the card proc file
542 * called from init.c
543 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100544int snd_info_card_register(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 struct proc_dir_entry *p;
547
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200548 if (snd_BUG_ON(!card))
549 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 if (!strcmp(card->id, card->proc_root->name))
552 return 0;
553
554 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
555 if (p == NULL)
556 return -ENOMEM;
557 card->proc_root_link = p;
558 return 0;
559}
560
561/*
Jaroslav Kyselac2eb9c42008-11-12 16:31:37 +0100562 * called on card->id change
563 */
564void snd_info_card_id_change(struct snd_card *card)
565{
566 mutex_lock(&info_mutex);
567 if (card->proc_root_link) {
David Howellsa8ca16e2013-04-12 17:27:28 +0100568 proc_remove(card->proc_root_link);
Jaroslav Kyselac2eb9c42008-11-12 16:31:37 +0100569 card->proc_root_link = NULL;
570 }
571 if (strcmp(card->id, card->proc_root->name))
572 card->proc_root_link = proc_symlink(card->id,
573 snd_proc_root,
574 card->proc_root->name);
575 mutex_unlock(&info_mutex);
576}
577
578/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * de-register the card proc file
580 * called from init.c
581 */
Takashi Iwai746d4a02006-06-23 14:37:59 +0200582void snd_info_card_disconnect(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200584 if (!card)
585 return;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200586 mutex_lock(&info_mutex);
David Howellsa8ca16e2013-04-12 17:27:28 +0100587 proc_remove(card->proc_root_link);
588 card->proc_root_link = NULL;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200589 if (card->proc_root)
590 snd_info_disconnect(card->proc_root);
591 mutex_unlock(&info_mutex);
592}
593
594/*
595 * release the card proc file resources
596 * called from init.c
597 */
598int snd_info_card_free(struct snd_card *card)
599{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200600 if (!card)
601 return 0;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200602 snd_info_free_entry(card->proc_root);
603 card->proc_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return 0;
605}
606
607
608/**
609 * snd_info_get_line - read one line from the procfs buffer
610 * @buffer: the procfs buffer
611 * @line: the buffer to store
Clemens Ladischddc64b22014-08-21 20:55:21 +0200612 * @len: the max. buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 *
614 * Reads one line from the buffer and stores the string.
615 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100616 * Return: Zero if successful, or 1 if error or EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100618int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 int c = -1;
621
Takashi Iwai0bc0ec92013-03-13 12:11:13 +0100622 if (snd_BUG_ON(!buffer || !buffer->buffer))
623 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (len <= 0 || buffer->stop || buffer->error)
625 return 1;
Takashi Iwai0bc0ec92013-03-13 12:11:13 +0100626 while (!buffer->stop) {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200627 c = buffer->buffer[buffer->curr++];
628 if (buffer->curr >= buffer->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 buffer->stop = 1;
Takashi Iwai0bc0ec92013-03-13 12:11:13 +0100630 if (c == '\n')
631 break;
Clemens Ladischddc64b22014-08-21 20:55:21 +0200632 if (len > 1) {
Takashi Iwai0bc0ec92013-03-13 12:11:13 +0100633 len--;
634 *line++ = c;
635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637 *line = '\0';
638 return 0;
639}
640
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200641EXPORT_SYMBOL(snd_info_get_line);
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643/**
Henrik Kretzschmar856def82005-07-08 13:53:42 +0200644 * snd_info_get_str - parse a string token
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 * @dest: the buffer to store the string token
646 * @src: the original string
647 * @len: the max. length of token - 1
648 *
649 * Parses the original string and copy a token to the given
650 * string buffer.
651 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100652 * Return: The updated pointer of the original string so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 * it can be used for the next call.
654 */
Takashi Iwai4f7454a2009-09-08 14:29:58 +0200655const char *snd_info_get_str(char *dest, const char *src, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 int c;
658
659 while (*src == ' ' || *src == '\t')
660 src++;
661 if (*src == '"' || *src == '\'') {
662 c = *src++;
663 while (--len > 0 && *src && *src != c) {
664 *dest++ = *src++;
665 }
666 if (*src == c)
667 src++;
668 } else {
669 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
670 *dest++ = *src++;
671 }
672 }
673 *dest = 0;
674 while (*src == ' ' || *src == '\t')
675 src++;
676 return src;
677}
678
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200679EXPORT_SYMBOL(snd_info_get_str);
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681/**
682 * snd_info_create_entry - create an info entry
683 * @name: the proc file name
684 *
685 * Creates an info entry with the given file name and initializes as
686 * the default state.
687 *
688 * Usually called from other functions such as
689 * snd_info_create_card_entry().
690 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100691 * Return: The pointer of the new instance, or %NULL on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100693static struct snd_info_entry *snd_info_create_entry(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100695 struct snd_info_entry *entry;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200696 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (entry == NULL)
698 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -0700699 entry->name = kstrdup(name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (entry->name == NULL) {
701 kfree(entry);
702 return NULL;
703 }
704 entry->mode = S_IFREG | S_IRUGO;
705 entry->content = SNDRV_INFO_CONTENT_TEXT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100706 mutex_init(&entry->access);
Takashi Iwai746d4a02006-06-23 14:37:59 +0200707 INIT_LIST_HEAD(&entry->children);
708 INIT_LIST_HEAD(&entry->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return entry;
710}
711
712/**
713 * snd_info_create_module_entry - create an info entry for the given module
714 * @module: the module pointer
715 * @name: the file name
716 * @parent: the parent directory
717 *
718 * Creates a new info entry and assigns it to the given module.
719 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100720 * Return: The pointer of the new instance, or %NULL on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100722struct snd_info_entry *snd_info_create_module_entry(struct module * module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 const char *name,
Takashi Iwai24c1f932005-11-17 13:58:48 +0100724 struct snd_info_entry *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100726 struct snd_info_entry *entry = snd_info_create_entry(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (entry) {
728 entry->module = module;
729 entry->parent = parent;
730 }
731 return entry;
732}
733
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200734EXPORT_SYMBOL(snd_info_create_module_entry);
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/**
737 * snd_info_create_card_entry - create an info entry for the given card
738 * @card: the card instance
739 * @name: the file name
740 * @parent: the parent directory
741 *
742 * Creates a new info entry and assigns it to the given card.
743 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100744 * Return: The pointer of the new instance, or %NULL on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100746struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 const char *name,
Takashi Iwai24c1f932005-11-17 13:58:48 +0100748 struct snd_info_entry * parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100750 struct snd_info_entry *entry = snd_info_create_entry(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (entry) {
752 entry->module = card->module;
753 entry->card = card;
754 entry->parent = parent;
755 }
756 return entry;
757}
758
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200759EXPORT_SYMBOL(snd_info_create_card_entry);
760
Takashi Iwai746d4a02006-06-23 14:37:59 +0200761static void snd_info_disconnect(struct snd_info_entry *entry)
762{
763 struct list_head *p, *n;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200764
765 list_for_each_safe(p, n, &entry->children) {
766 snd_info_disconnect(list_entry(p, struct snd_info_entry, list));
767 }
768
769 if (! entry->p)
770 return;
771 list_del_init(&entry->list);
David Howellsa8ca16e2013-04-12 17:27:28 +0100772 proc_remove(entry->p);
Takashi Iwai746d4a02006-06-23 14:37:59 +0200773 entry->p = NULL;
774}
775
Takashi Iwai24c1f932005-11-17 13:58:48 +0100776static int snd_info_dev_free_entry(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100778 struct snd_info_entry *entry = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 snd_info_free_entry(entry);
780 return 0;
781}
782
Takashi Iwai24c1f932005-11-17 13:58:48 +0100783static int snd_info_dev_register_entry(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100785 struct snd_info_entry *entry = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return snd_info_register(entry);
787}
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789/**
790 * snd_card_proc_new - create an info entry for the given card
791 * @card: the card instance
792 * @name: the file name
793 * @entryp: the pointer to store the new info entry
794 *
795 * Creates a new info entry and assigns it to the given card.
796 * Unlike snd_info_create_card_entry(), this function registers the
797 * info entry as an ALSA device component, so that it can be
798 * unregistered/released without explicit call.
799 * Also, you don't have to register this entry via snd_info_register(),
800 * since this will be registered by snd_card_register() automatically.
801 *
802 * The parent is assumed as card->proc_root.
803 *
804 * For releasing this entry, use snd_device_free() instead of
805 * snd_info_free_entry().
806 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100807 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100809int snd_card_proc_new(struct snd_card *card, const char *name,
810 struct snd_info_entry **entryp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100812 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 .dev_free = snd_info_dev_free_entry,
814 .dev_register = snd_info_dev_register_entry,
Takashi Iwai746d4a02006-06-23 14:37:59 +0200815 /* disconnect is done via snd_info_card_disconnect() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 };
Takashi Iwai24c1f932005-11-17 13:58:48 +0100817 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 int err;
819
820 entry = snd_info_create_card_entry(card, name, card->proc_root);
821 if (! entry)
822 return -ENOMEM;
823 if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) {
824 snd_info_free_entry(entry);
825 return err;
826 }
827 if (entryp)
828 *entryp = entry;
829 return 0;
830}
831
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200832EXPORT_SYMBOL(snd_card_proc_new);
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834/**
835 * snd_info_free_entry - release the info entry
836 * @entry: the info entry
837 *
838 * Releases the info entry. Don't call this after registered.
839 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100840void snd_info_free_entry(struct snd_info_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 if (entry == NULL)
843 return;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200844 if (entry->p) {
845 mutex_lock(&info_mutex);
846 snd_info_disconnect(entry);
847 mutex_unlock(&info_mutex);
848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 kfree(entry->name);
850 if (entry->private_free)
851 entry->private_free(entry);
852 kfree(entry);
853}
854
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200855EXPORT_SYMBOL(snd_info_free_entry);
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857/**
858 * snd_info_register - register the info entry
859 * @entry: the info entry
860 *
861 * Registers the proc info entry.
862 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100863 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100865int snd_info_register(struct snd_info_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 struct proc_dir_entry *root, *p = NULL;
868
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200869 if (snd_BUG_ON(!entry))
870 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100872 mutex_lock(&info_mutex);
Al Viroaee0c612013-03-28 23:01:34 -0400873 if (S_ISDIR(entry->mode)) {
874 p = proc_mkdir_mode(entry->name, entry->mode, root);
875 if (!p) {
876 mutex_unlock(&info_mutex);
877 return -ENOMEM;
878 }
879 } else {
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200880 const struct file_operations *ops;
881 if (entry->content == SNDRV_INFO_CONTENT_DATA)
882 ops = &snd_info_entry_operations;
883 else
884 ops = &snd_info_text_entry_ops;
Al Viroaee0c612013-03-28 23:01:34 -0400885 p = proc_create_data(entry->name, entry->mode, root,
Takashi Iwai4adb7bc2015-04-22 16:10:22 +0200886 ops, entry);
Al Viroaee0c612013-03-28 23:01:34 -0400887 if (!p) {
888 mutex_unlock(&info_mutex);
889 return -ENOMEM;
890 }
David Howells271a15e2013-04-12 00:38:51 +0100891 proc_set_size(p, entry->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 entry->p = p;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200894 if (entry->parent)
895 list_add_tail(&entry->list, &entry->parent->children);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100896 mutex_unlock(&info_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return 0;
898}
899
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200900EXPORT_SYMBOL(snd_info_register);
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902/*
903
904 */
905
Takashi Iwai6581f4e2006-05-17 17:14:51 +0200906static struct snd_info_entry *snd_info_version_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Takashi Iwai24c1f932005-11-17 13:58:48 +0100908static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 snd_iprintf(buffer,
Jaroslav Kysela42662742012-09-04 11:21:45 +0200911 "Advanced Linux Sound Architecture Driver Version k%s.\n",
912 init_utsname()->release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915static int __init snd_info_version_init(void)
916{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100917 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
920 if (entry == NULL)
921 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 entry->c.text.read = snd_info_version_read;
923 if (snd_info_register(entry) < 0) {
924 snd_info_free_entry(entry);
925 return -ENOMEM;
926 }
927 snd_info_version_entry = entry;
928 return 0;
929}
930
931static int __exit snd_info_version_done(void)
932{
Takashi Iwai746d4a02006-06-23 14:37:59 +0200933 snd_info_free_entry(snd_info_version_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return 0;
935}
936
937#endif /* CONFIG_PROC_FS */